Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
feat(document_cognition_service): migrate document cognition service to gateway (#6216)
Will Hutchinsone86f711656b6
diff --git a/apps/web/src/lib/core/constant/servers.ts b/apps/web/src/lib/core/constant/servers.tsindex 5d632441d2..2ca0b42594 100644--- a/apps/web/src/lib/core/constant/servers.ts+++ b/apps/web/src/lib/core/constant/servers.ts@@ -34,7 +34,7 @@ const serverHostRemote = {'pdf-service': `https://pdf-service${devServerSuffix}.macro.com`,'document-storage-service': `${gatewayHost}/dss`,'websocket-service': `wss://services${devServerSuffix}.macro.com`,- 'cognition-service': `https://document-cognition${devServerSuffix}.macro.com`,+ 'cognition-service': `${gatewayHost}/cognition`,'connection-gateway': `${gatewayHost.replace(/^http/, 'ws')}/connection-gateway`,'notification-service': `${gatewayHost}/notification`,'static-file': `https://static-file-service${devServerSuffix}.macro.com`,diff --git a/crates/macro_service_urls/src/lib.rs b/crates/macro_service_urls/src/lib.rsindex 98b8a77ba1..33d4f6ceca 100644--- a/crates/macro_service_urls/src/lib.rs+++ b/crates/macro_service_urls/src/lib.rs@@ -539,8 +539,8 @@ service_url! {/// Document cognition service API URL.pub DocumentCognitionServiceUrl {local: "http://localhost:8085",- dev: "https://document-cognition-dev.macro.com",- prod: "https://document-cognition.macro.com",+ dev: "https://dev-gateway.macro.com/cognition",+ prod: "https://gateway.macro.com/cognition",},/// Notification service API URL.pub NotificationServiceUrl {diff --git a/crates/macro_service_urls/src/test.rs b/crates/macro_service_urls/src/test.rsindex da78b835e3..fbfa769300 100644--- a/crates/macro_service_urls/src/test.rs+++ b/crates/macro_service_urls/src/test.rs@@ -122,6 +122,18 @@ fn document_cognition_service_url_parses() {assert_parses_for_all_environments(DocumentCognitionServiceUrl::default_for_environment);}+#[test]+fn document_cognition_service_url_has_no_trailing_slash() {+ for environment in ENVS {+ let url = DocumentCognitionServiceUrl::default_for_environment(environment);+ assert!(+ !url.as_ref().ends_with('/'),+ "clients concatenate paths, so {} must not end with /",+ url.as_ref()+ );+ }+}+#[test]fn notification_service_url_parses() {assert_parses_for_all_environments(NotificationServiceUrl::default_for_environment);@@ -461,7 +473,7 @@ fn exported_service_urls_match_dev_values() {);assert_eq!(service_urls.document_cognition_service_url.as_ref(),- "https://document-cognition-dev.macro.com",+ "https://dev-gateway.macro.com/cognition",);assert_eq!(service_urls.notification_service_url.as_ref(),@@ -532,7 +544,7 @@ fn exported_service_urls_match_prod_values() {);assert_eq!(service_urls.document_cognition_service_url.as_ref(),- "https://document-cognition.macro.com",+ "https://gateway.macro.com/cognition",);assert_eq!(service_urls.notification_service_url.as_ref(),diff --git a/infra/packages/shared/src/gateway_priorities.ts b/infra/packages/shared/src/gateway_priorities.tsindex b768bbaac7..890ff3d3bb 100644--- a/infra/packages/shared/src/gateway_priorities.ts+++ b/infra/packages/shared/src/gateway_priorities.ts@@ -15,6 +15,7 @@ export enum GatewayService {CONNECTION_GATEWAY = 'CONNECTION_GATEWAY',AUTHENTICATION_SERVICE = 'AUTHENTICATION_SERVICE',EMAIL_SERVICE = 'EMAIL_SERVICE',+ DOCUMENT_COGNITION_SERVICE = 'DOCUMENT_COGNITION_SERVICE',}/**@@ -38,6 +39,7 @@ export const GATEWAY_PRIORITIES: GatewayPriorityMap = {[GatewayService.CONNECTION_GATEWAY]: 90,[GatewayService.AUTHENTICATION_SERVICE]: 100,[GatewayService.EMAIL_SERVICE]: 110,+ [GatewayService.DOCUMENT_COGNITION_SERVICE]: 120,[GatewayService.CONVERT_SERVICE]: 3000,};diff --git a/infra/packages/shared/src/service_urls.ts b/infra/packages/shared/src/service_urls.tsindex f7476cc685..d84c37281c 100644--- a/infra/packages/shared/src/service_urls.ts+++ b/infra/packages/shared/src/service_urls.ts@@ -41,7 +41,7 @@ const DEV_SERVICE_URLS: ServiceUrlMap = {[ServiceUrl.CONNECTION_GATEWAY_URL]:'https://dev-gateway.macro.com/connection-gateway',[ServiceUrl.DOCUMENT_COGNITION_SERVICE_URL]:- 'https://document-cognition-dev.macro.com',+ 'https://dev-gateway.macro.com/cognition',[ServiceUrl.LEXICAL_SERVICE_URL]:'https://lexical-service-dev.macroverse.workers.dev',[ServiceUrl.UNFURL_SERVICE_URL]: 'https://dev-gateway.macro.com/unfurl',@@ -64,7 +64,7 @@ const PROD_SERVICE_URLS: ServiceUrlMap = {[ServiceUrl.CONNECTION_GATEWAY_URL]:'https://gateway.macro.com/connection-gateway',[ServiceUrl.DOCUMENT_COGNITION_SERVICE_URL]:- 'https://document-cognition.macro.com',+ 'https://gateway.macro.com/cognition',[ServiceUrl.LEXICAL_SERVICE_URL]:'https://lexical-service-prod.macroverse.workers.dev',[ServiceUrl.UNFURL_SERVICE_URL]: 'https://gateway.macro.com/unfurl',diff --git a/infra/stacks/document-cognition-service/document-cognition-service.ts b/infra/stacks/document-cognition-service/document-cognition-service.tsindex ae90104f6d..6d04a5528c 100644--- a/infra/stacks/document-cognition-service/document-cognition-service.ts+++ b/infra/stacks/document-cognition-service/document-cognition-service.ts@@ -9,6 +9,7 @@ import {datadogAgentContainer,fargateLogRouterSidecarContainer,serviceLoadBalancer,+ ServiceTargetGroup,} from '../../packages/resources';import { EcrImage } from '../../packages/service';import {@@ -16,9 +17,13 @@ import {CLOUD_TRAIL_SNS_TOPIC_ARN,DopplerEcsEnvironment,getKafkaClusterPolicy,+ getGatewayAlb,+ GatewayService,stack,} from '../../packages/shared';+const gatewayLoadBalancer = getGatewayAlb();+const BASE_NAME = 'document-cognition';const REPO_ROOT = '../../..';@@ -110,6 +115,22 @@ export class DocumentCognitionService extends pulumi.ComponentResource {this.serviceAlbSg = sg.serviceAlbSg;this.serviceSg = sg.serviceSg;+ const gatewayTargetGroup = new ServiceTargetGroup(+ `${stack}-${BASE_NAME}`,+ {+ tags: this.tags,+ listenerArn: gatewayLoadBalancer.httpsListenerArn,+ vpcId: vpc.vpcId,+ containerPort: serviceContainerPort,+ service: GatewayService.DOCUMENT_COGNITION_SERVICE,+ healthCheckPath,+ pathPatterns: ['/cognition', '/cognition/*'],+ serviceSecurityGroupId: this.serviceSg.id,+ albSecurityGroupId: gatewayLoadBalancer.albSecurityGroupId,+ },+ { parent: this }+ );+// lbconst { targetGroup, lb, listener } = serviceLoadBalancer(this, {serviceName: BASE_NAME, // service name@@ -249,6 +270,23 @@ export class DocumentCognitionService extends pulumi.ComponentResource {enable: true,rollback: true,},+ // Register tasks in both the legacy ALB's target group and the gateway+ // target group while we migrate to the gateway. An explicit+ // `loadBalancers` replaces the list awsx derives from+ // `portMappings.targetGroup`, so the legacy entry must be listed here+ // too.+ loadBalancers: [+ {+ targetGroupArn: targetGroup.arn,+ containerName: 'service',+ containerPort: serviceContainerPort,+ },+ {+ targetGroupArn: gatewayTargetGroup.target_group.arn,+ containerName: 'service',+ containerPort: serviceContainerPort,+ },+ ],taskDefinitionArgs: {taskRole: {roleArn: this.role.arn,@@ -303,12 +341,19 @@ export class DocumentCognitionService extends pulumi.ComponentResource {},{parent: this,+ // ECS refuses a service whose target group is not yet associated with+ // a load balancer; it is the listener rule that creates that+ // association+ dependsOn: [gatewayTargetGroup.listener_rule],});this.service = service;- this.setupAutoScaling();+ this.setupAutoScaling({+ gatewayAlbArnSuffix: gatewayLoadBalancer.albArnSuffix,+ gatewayTargetGroup: gatewayTargetGroup.target_group,+ });this.setupServiceAlarms();@@ -443,7 +488,13 @@ export class DocumentCognitionService extends pulumi.ComponentResource {return { serviceAlbSg, serviceSg };}- setupAutoScaling() {+ setupAutoScaling({+ gatewayAlbArnSuffix,+ gatewayTargetGroup,+ }: {+ gatewayAlbArnSuffix: pulumi.Output<string>;+ gatewayTargetGroup: aws.lb.TargetGroup;+ }) {if (!this.service) return;const serviceScalableTarget = new aws.appautoscaling.Target(@@ -472,8 +523,8 @@ export class DocumentCognitionService extends pulumi.ComponentResource {);const resourceLabel = pulumi.interpolate`${lbPortion}/${tgPortion}`;+ const gatewayResourceLabel = pulumi.interpolate`${gatewayAlbArnSuffix}/${gatewayTargetGroup.arnSuffix}`;- // Create an Auto Scaling policy for request count.new aws.appautoscaling.Policy(`${BASE_NAME}-scaling-policy-request-count-${stack}`,{@@ -494,6 +545,26 @@ export class DocumentCognitionService extends pulumi.ComponentResource {{ parent: this });+ new aws.appautoscaling.Policy(+ `${BASE_NAME}-scaling-policy-gateway-request-count-${stack}`,+ {+ policyType: 'TargetTrackingScaling',+ resourceId: serviceScalableTarget.resourceId,+ scalableDimension: serviceScalableTarget.scalableDimension,+ serviceNamespace: serviceScalableTarget.serviceNamespace,+ targetTrackingScalingPolicyConfiguration: {+ targetValue: 1000,+ predefinedMetricSpecification: {+ predefinedMetricType: 'ALBRequestCountPerTarget',+ resourceLabel: gatewayResourceLabel,+ },+ scaleInCooldown: 100,+ scaleOutCooldown: 120,+ },+ },+ { parent: this }+ );+// Create an Auto Scaling policy for CPU utilization.new aws.appautoscaling.Policy(`${BASE_NAME}-scaling-policy-cpu-${stack}`,diff --git a/packages/sdk/src/config.ts b/packages/sdk/src/config.tsindex a9450aaa04..7a8b9e7272 100644--- a/packages/sdk/src/config.ts+++ b/packages/sdk/src/config.ts@@ -29,7 +29,7 @@ export const HOSTS: Record<Env, Record<ServiceName, string>> = {storage: 'https://dev-gateway.macro.com/dss',auth: 'https://dev-gateway.macro.com/auth',email: 'https://dev-gateway.macro.com/email',- cognition: 'https://document-cognition-dev.macro.com',+ cognition: 'https://dev-gateway.macro.com/cognition',notification: 'https://dev-gateway.macro.com/notification',properties: 'https://dev-gateway.macro.com/dss',search: 'https://dev-gateway.macro.com/dss',@@ -44,7 +44,7 @@ export const HOSTS: Record<Env, Record<ServiceName, string>> = {storage: 'https://gateway.macro.com/dss',auth: 'https://gateway.macro.com/auth',email: 'https://gateway.macro.com/email',- cognition: 'https://document-cognition.macro.com',+ cognition: 'https://gateway.macro.com/cognition',notification: 'https://gateway.macro.com/notification',properties: 'https://gateway.macro.com/dss',search: 'https://gateway.macro.com/dss',diff --git a/services/document_cognition_service/src/api.rs b/services/document_cognition_service/src/api.rsindex f3ac7b0607..aa8554deba 100644--- a/services/document_cognition_service/src/api.rs+++ b/services/document_cognition_service/src/api.rs@@ -26,6 +26,11 @@ mod attachments;mod chats;pub mod structured_completion;+#[cfg(test)]+mod test;++const GATEWAY_PATH_PREFIX: &str = "/cognition";+#[tracing::instrument(err, skip(state))]pub async fn setup_and_serve(state: ApiContext) -> anyhow::Result<()> {let cors = macro_cors::cors_layer();@@ -39,22 +44,24 @@ pub async fn setup_and_serve(state: ApiContext) -> anyhow::Result<()> {let port = state.config.port;let environment = state.config.environment;- let app = api_router(state.clone())- .layer(cors.clone())- .layer(DefaultBodyLimit::disable())- .layer(RequestBodyLimitLayer::new(1024 * 1024 * 1024)) // 1GB- .layer(- ServiceBuilder::new()- .layer(TraceLayer::new_for_http())- .layer(axum::middleware::from_fn_with_state(- ServiceNameState {- service_name: VersionedApiServiceName::DocumentCognitionService,- },- validate_api_version,- )),- )- .merge(health::router().layer(cors))- .merge(SwaggerUi::new("/docs").url("/api-doc/openapi.json", swagger::ApiDoc::openapi()));+ let app = mount_at_root_and_prefix(+ api_router(state.clone())+ .layer(cors.clone())+ .layer(DefaultBodyLimit::disable())+ .layer(RequestBodyLimitLayer::new(1024 * 1024 * 1024))+ .layer(+ ServiceBuilder::new()+ .layer(TraceLayer::new_for_http())+ .layer(axum::middleware::from_fn_with_state(+ ServiceNameState {+ service_name: VersionedApiServiceName::DocumentCognitionService,+ },+ validate_api_version,+ )),+ )+ .merge(health::router().layer(cors)),+ )+ .merge(swagger_ui());let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", port)).await@@ -70,6 +77,27 @@ pub async fn setup_and_serve(state: ApiContext) -> anyhow::Result<()> {.context("error starting service")}+fn mount_at_root_and_prefix(inner: Router) -> Router {+ Router::new()+ .merge(inner.clone())+ .nest(GATEWAY_PATH_PREFIX, inner)+}++fn swagger_ui() -> Router {+ Router::new()+ .merge(SwaggerUi::new("/docs").url("/api-doc/openapi.json", swagger::ApiDoc::openapi()))+ .merge(SwaggerUi::new(format!("{GATEWAY_PATH_PREFIX}/docs")).url(+ format!("{GATEWAY_PATH_PREFIX}/api-doc/openapi.json"),+ prefixed_openapi(),+ ))+}++fn prefixed_openapi() -> utoipa::openapi::OpenApi {+ let mut openapi = swagger::ApiDoc::openapi();+ openapi.servers = Some(vec![utoipa::openapi::Server::new(GATEWAY_PATH_PREFIX)]);+ openapi+}+fn api_router(api_context: ApiContext) -> Router {let memory_service = api_context.memory_service.clone();let usage_service = api_context.usage_service.clone();diff --git a/services/document_cognition_service/src/api/test.rs b/services/document_cognition_service/src/api/test.rsnew file mode 100644index 0000000000..b695340b10--- /dev/null+++ b/services/document_cognition_service/src/api/test.rs@@ -0,0 +1,72 @@+use super::*;+use axum::{+ body::Body,+ http::{Request, StatusCode},+};+use http_body_util::BodyExt;+use tower::ServiceExt;++async fn get_status(app: Router, path: &str) -> StatusCode {+ app.oneshot(+ Request::builder()+ .uri(path)+ .method("GET")+ .body(Body::empty())+ .unwrap(),+ )+ .await+ .unwrap()+ .status()+}++#[tokio::test]+async fn health_is_reachable_at_root_and_gateway_prefix() {+ for path in ["/health", "/cognition/health"] {+ assert_eq!(+ get_status(mount_at_root_and_prefix(health::router()), path).await,+ StatusCode::OK,+ "{path}"+ );+ }+}++#[tokio::test]+async fn unprefixed_unknown_path_is_not_rewritten_onto_the_prefix() {+ assert_eq!(+ get_status(mount_at_root_and_prefix(health::router()), "/missing").await,+ StatusCode::NOT_FOUND+ );+}++#[tokio::test]+async fn openapi_is_served_at_root_and_gateway_prefix() {+ for path in ["/api-doc/openapi.json", "/cognition/api-doc/openapi.json"] {+ assert_eq!(+ get_status(swagger_ui(), path).await,+ StatusCode::OK,+ "{path}"+ );+ }+}++#[tokio::test]+async fn prefixed_openapi_declares_the_cognition_server() {+ let response = swagger_ui()+ .oneshot(+ Request::builder()+ .uri("/cognition/api-doc/openapi.json")+ .method("GET")+ .body(Body::empty())+ .unwrap(),+ )+ .await+ .unwrap();++ assert_eq!(response.status(), StatusCode::OK);+ let spec: serde_json::Value =+ serde_json::from_slice(&response.into_body().collect().await.unwrap().to_bytes()).unwrap();+ assert_eq!(+ spec["servers"][0]["url"].as_str(),+ Some(GATEWAY_PATH_PREFIX)+ );+}diff --git a/services/document_cognition_service/src/config.rs b/services/document_cognition_service/src/config.rsindex 9eec2784b5..eceb82012a 100644--- a/services/document_cognition_service/src/config.rs+++ b/services/document_cognition_service/src/config.rs@@ -2,11 +2,14 @@ use anyhow::Context;use macro_auth::InternalApiKey;pub use macro_env::Environment;use macro_env_var::{env_vars, maybe_env_vars};-use macro_service_urls::{AiEditingWorkerUrl, DocumentCognitionServiceUrl};+use macro_service_urls::AiEditingWorkerUrl;use secretsmanager_client::LocalOrRemoteSecret;use crate::core::constants::DEFAULT_DOCUMENT_BATCH_LIMIT;+#[cfg(test)]+mod test;+env_vars!(pub struct DatabaseUrl;pub struct DocumentStorageBucket;@@ -108,7 +111,7 @@ pub struct Config {#[macro_config_default(AiEditingWorkerUrl::unwrap_new().to_string())]pub ai_editing_worker_url: String,/// Browser-facing base URL used for MCP OAuth redirects and client metadata.- #[macro_config_default(DocumentCognitionServiceUrl::unwrap_new().to_string())]+ #[macro_config_default(default_mcp_public_url(Environment::new_or_prod()).to_string())]pub mcp_public_url: String,/// JWT secret for minting document permission tokens for the editing worker.pub document_permission_jwt: DocumentPermissionJwt,@@ -116,6 +119,14 @@ pub struct Config {pub kafka_brokers: KafkaBrokers,}+fn default_mcp_public_url(environment: Environment) -> &'static str {+ match environment {+ Environment::Production => "https://document-cognition.macro.com",+ Environment::Develop => "https://document-cognition-dev.macro.com",+ Environment::Local => "http://localhost:8085",+ }+}+impl Config {#[tracing::instrument(err, skip_all)]pub fn from_env() -> anyhow::Result<Self> {@@ -168,7 +179,7 @@ impl Config {pipedream_allowed_origins: PipedreamAllowedOrigins::Unset,internal_api_key: InternalApiKey::Comptime(""),ai_editing_worker_url: AiEditingWorkerUrl::unwrap_new().to_string(),- mcp_public_url: DocumentCognitionServiceUrl::unwrap_new().to_string(),+ mcp_public_url: default_mcp_public_url(Environment::Local).to_string(),document_permission_jwt: DocumentPermissionJwt::Comptime("DOCUMENT_PERMISSION_JWT"),kafka_brokers: KafkaBrokers::Comptime("localhost:9092"),}diff --git a/services/document_cognition_service/src/config/test.rs b/services/document_cognition_service/src/config/test.rsnew file mode 100644index 0000000000..1d090cc68e--- /dev/null+++ b/services/document_cognition_service/src/config/test.rs@@ -0,0 +1,26 @@+use super::*;++#[test]+fn default_mcp_public_url_is_a_slack_registered_redirect_host() {+ let manifest: serde_json::Value = serde_json::from_str(include_str!(+ "../../../../crates/mcp_client/src/domain/provider_registry/slack/manifest.json"+ ))+ .unwrap();+ let registered: Vec<&str> = manifest["oauth_config"]["redirect_urls"]+ .as_array()+ .unwrap()+ .iter()+ .map(|url| url.as_str().unwrap())+ .collect();++ for environment in [Environment::Production, Environment::Develop] {+ let callback = format!(+ "{}/mcp/servers/auth/callback",+ default_mcp_public_url(environment)+ );+ assert!(+ registered.contains(&callback.as_str()),+ "{environment:?}: {callback} is not registered in the Slack manifest"+ );+ }+}