@segmentstream/ontology 0.161.0 → 0.163.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/dist/agent-draft.d.ts +21 -0
  3. package/dist/agent-draft.js +57 -1
  4. package/dist/deployment.d.ts +13 -1
  5. package/dist/deployment.js +8 -0
  6. package/dist/index.d.ts +6 -5
  7. package/dist/index.js +7 -6
  8. package/dist/manifest.js +1 -0
  9. package/dist/runtime/agent-web.js +42 -3
  10. package/dist/runtime/agents.js +6 -1
  11. package/dist/service/agent/authentication.js +9 -2
  12. package/dist/service/agent/eval-tokens.d.ts +25 -0
  13. package/dist/service/agent/eval-tokens.js +130 -0
  14. package/dist/service/agent/protocol.d.ts +20 -5
  15. package/dist/service/agent/protocol.js +25 -3
  16. package/dist/service/agent/server.d.ts +11 -0
  17. package/dist/service/agent/server.js +114 -5
  18. package/dist/service/agent/web-tokens.d.ts +13 -2
  19. package/dist/service/agent/web-tokens.js +20 -5
  20. package/dist/service/authentication.js +3 -2
  21. package/dist/service/chat/archive.d.ts +15 -1
  22. package/dist/service/chat/draft-files.d.ts +56 -0
  23. package/dist/service/chat/draft-files.js +137 -0
  24. package/dist/service/chat/idempotency.d.ts +6 -1
  25. package/dist/service/chat/idempotency.js +7 -1
  26. package/dist/service/chat/providers/custom.js +12 -2
  27. package/dist/service/chat/providers/email.d.ts +2 -1
  28. package/dist/service/chat/providers/email.js +48 -4
  29. package/dist/service/chat/providers/provider.d.ts +49 -1
  30. package/dist/service/chat/providers/provider.js +18 -1
  31. package/dist/service/chat/review-delivery.d.ts +2 -0
  32. package/dist/service/chat/review-delivery.js +73 -4
  33. package/dist/service/chat/store-delivery.js +13 -0
  34. package/dist/service/config.js +8 -2
  35. package/dist/service/runtime-contract.d.ts +33 -0
  36. package/dist/service/runtime-identity.d.ts +17 -1
  37. package/dist/service/runtime-identity.js +15 -2
  38. package/dist/service/service.js +64 -11
  39. package/package.json +1 -1
@@ -6,6 +6,7 @@ import { validateDeploymentStorage } from "../storage/provider.js";
6
6
  import { captureRuntimeFailure } from "../internal/diagnostics.js";
7
7
  import { deploymentFromEnv, DeploymentAdmissionError, requireDeploymentCapability, } from "../deployment.js";
8
8
  import { RuntimeDeployment } from "../runtime/deployment.js";
9
+ import { previewRunsAgent } from "../deployment.js";
9
10
  import { nameToWire } from "../authoring-names.js";
10
11
  import { bundledNavigationCatalog } from "../navigation-artifact.js";
11
12
  import { parsePageCatalog } from "../navigation-catalog.js";
@@ -56,7 +57,7 @@ import { vercelWorkflowEngine, workflowStarter, workflowsLayer } from "./workflo
56
57
  import { deploymentWorkflowEngine } from "./workflow-deployment.js";
57
58
  import { OntologyBackend } from "./runtime-contract.js";
58
59
  import { currentEntityPath } from "../earlier-entity-names.js";
59
- import { runtimeCredential, runtimeTokenVerifier, } from "./runtime-identity.js";
60
+ import { ONTOLOGY_DEPLOYMENT_HEADER, claimsRuntimeIssuer, runtimeCredential, runtimeTokenVerifier, } from "./runtime-identity.js";
60
61
  /** The header every response carries: the hash of the manifest the service serves. */
61
62
  export const ONTOLOGY_HASH_HEADER = "x-ontology-hash";
62
63
  /** The header every response carries: the correlation id quoted in error bodies and logs. */
@@ -158,6 +159,23 @@ function decorate(response, requestId, hash, durationMs) {
158
159
  headers,
159
160
  });
160
161
  }
162
+ /**
163
+ * The agent routes a preview that runs the agent serves: agent tokens for the preview app's web chat,
164
+ * their introspection, eval sessions, the chat store, held replies, and members' attachments and
165
+ * files.
166
+ */
167
+ const PREVIEW_AGENT_ROUTE = /^\/api\/ontology\/agent\/(?:tokens|eval-tokens|chats|drafts|reviews|attachments|files|conversations)(?:\/|$)/;
168
+ /**
169
+ * Whether a request is not the runtime's, or is the runtime's and names this deployment in
170
+ * `x-segmentstream-ontology-deployment-id`. Every preview of a workspace shares one hosting project,
171
+ * whose function tokens name no deployment, so the header keeps another preview's agent out.
172
+ */
173
+ function sameDeployment(request, identity, deploymentId) {
174
+ const match = /^Bearer\s+(.+)$/iu.exec(request.headers.get("authorization")?.trim() ?? "");
175
+ return (match === null ||
176
+ !claimsRuntimeIssuer(identity, match[1]) ||
177
+ request.headers.get(ONTOLOGY_DEPLOYMENT_HEADER)?.trim() === deploymentId);
178
+ }
161
179
  /**
162
180
  * The ontology service: the derived HttpApi, the MCP server at `/mcp` and the agent's pairing routes,
163
181
  * wired to the runtime, behind Neon Auth, app-token, API-key and runtime-token authentication.
@@ -167,27 +185,45 @@ function decorate(response, requestId, hash, durationMs) {
167
185
  export function createOntologyService(ontology, config) {
168
186
  const ambient = deploymentFromEnv();
169
187
  const requested = ambient.target === "preview" ? ambient : (config.deployment ?? ambient);
188
+ // The agent runs in a hosted preview only where the platform said so and gave the service the
189
+ // preview's runtime identity; production never trusts a preview's tokens.
190
+ const runtimeIdentity = requested.target === "preview"
191
+ ? requested.agent === true && config.runtimeIdentity?.environment === "preview"
192
+ ? config.runtimeIdentity
193
+ : undefined
194
+ : config.runtimeIdentity === undefined
195
+ ? undefined
196
+ : { ...config.runtimeIdentity, environment: "production" };
170
197
  const deployment = deploymentFromEnv({
171
198
  SEGMENTSTREAM_DEPLOYMENT_TARGET: requested.target,
172
199
  SEGMENTSTREAM_ONTOLOGY_DEPLOYMENT_ID: requested.deploymentId,
173
200
  SEGMENTSTREAM_PREVIEW_EXPIRES_AT: requested.target === "preview" ? requested.expiresAt : undefined,
174
201
  SEGMENTSTREAM_PREVIEW_HOST: requested.target === "preview" ? requested.host : undefined,
202
+ ...(requested.target === "preview" && runtimeIdentity !== undefined
203
+ ? {
204
+ SEGMENTSTREAM_RUNTIME_OIDC_ISSUER: runtimeIdentity.issuer,
205
+ SEGMENTSTREAM_RUNTIME_OIDC_AUDIENCE: runtimeIdentity.audience,
206
+ }
207
+ : {}),
175
208
  });
176
209
  const preview = deployment.target === "preview";
210
+ const previewAgent = previewRunsAgent(deployment);
177
211
  config = {
178
212
  ...config,
179
213
  deployment,
180
214
  ...(preview
181
215
  ? {
182
216
  oidc: ontology.signIn.browser ? config.oidc : undefined,
183
- runtimeIdentity: undefined,
217
+ runtimeIdentity,
184
218
  localConnections: undefined,
185
219
  cronSecret: undefined,
186
220
  agentOrigin: undefined,
187
221
  channels: undefined,
188
222
  channelAppProviders: undefined,
189
223
  }
190
- : {}),
224
+ : runtimeIdentity === undefined
225
+ ? {}
226
+ : { runtimeIdentity }),
191
227
  };
192
228
  if (preview && !config.neonAuth && !ontology.signIn.browser?.methods.includes("segmentstream")) {
193
229
  throw new DeploymentAdmissionError("preview_configuration_invalid");
@@ -345,7 +381,8 @@ export function createOntologyService(ontology, config) {
345
381
  ? deployedConnections(config)
346
382
  : localConnections(config.localConnections)));
347
383
  const replicate = deployedReplication(config);
348
- const chatHistory = config.runtimeIdentity === undefined ? undefined : platformChatArchive(config);
384
+ // The platform keeps chat history for production agents only.
385
+ const chatHistory = config.runtimeIdentity === undefined || preview ? undefined : platformChatArchive(config);
349
386
  const chatDelivery = makeChatDelivery(ontology, connections,
350
387
  // @legacy agent.cloud-chat-archive Deliveries reserve here while the store holds no message.
351
388
  config.chatArchive ?? platformDeliveryArchive(config), { settings: config.channels?.telegram, ownOrigins: config.appOrigins },
@@ -410,15 +447,18 @@ export function createOntologyService(ontology, config) {
410
447
  verifyToken,
411
448
  })
412
449
  : undefined);
413
- const agent = preview
450
+ // A preview runs the agent for the preview app's web chat and evals only: no visitors, sign-ups,
451
+ // channels or tasks.
452
+ const withoutAgent = preview && !previewAgent;
453
+ const agent = withoutAgent
414
454
  ? Layer.empty
415
455
  : agentLayer({
416
456
  verifyToken,
417
457
  verifyRuntime,
418
458
  exchangeSession,
419
- visitorWebsites,
459
+ visitorWebsites: preview ? { guests: [], signUp: [] } : visitorWebsites,
420
460
  agentTools,
421
- guest,
461
+ guest: preview ? undefined : guest,
422
462
  botCheckVerifier: config.botCheckVerifier ??
423
463
  deployedBotCheckVerifier({
424
464
  segmentstreamUrl: config.segmentstreamUrl,
@@ -427,9 +467,11 @@ export function createOntologyService(ontology, config) {
427
467
  deploymentId: deployment.deploymentId,
428
468
  }),
429
469
  emailSignUp: signUpRolesOf(ontology.manifest, "email") !== undefined,
470
+ manifest: ontology.manifest,
430
471
  emailCodes,
431
472
  cronSecret: config.cronSecret,
432
473
  chatRetentionDays: chatRetentionDaysOf(ontology.manifest),
474
+ ...(previewAgent ? { evalTokens: deployment } : {}),
433
475
  });
434
476
  const agentTasks = preview
435
477
  ? Layer.empty
@@ -441,7 +483,7 @@ export function createOntologyService(ontology, config) {
441
483
  agentTools,
442
484
  implicitMember: implicitMemberOf(ontology.roles) !== undefined,
443
485
  });
444
- const attachments = preview
486
+ const attachments = withoutAgent
445
487
  ? Layer.empty
446
488
  : attachmentsLayer({
447
489
  plan: built.plan,
@@ -453,7 +495,7 @@ export function createOntologyService(ontology, config) {
453
495
  });
454
496
  const application = Layer.mergeAll(channelApps.length > 0
455
497
  ? Layer.merge(channelSessionLayer(config), channelSignInLayer(channelApps, config, connections))
456
- : Layer.empty, federationLayer(federationConfig, verifyToken, { ontology, browserRevision }), routes, preview
498
+ : Layer.empty, federationLayer(federationConfig, verifyToken, { ontology, browserRevision }), routes, withoutAgent
457
499
  ? Layer.empty
458
500
  : chatRoutes({
459
501
  built,
@@ -473,7 +515,7 @@ export function createOntologyService(ontology, config) {
473
515
  chatDelivery,
474
516
  verifyToken,
475
517
  exchangeSession,
476
- }), preview ? Layer.empty : filesLayer({ plan: built.plan, verifyRuntime }), preview ? Layer.empty : workspaceSourceLayer({ environment: config, verifyRuntime }), chatHistory === undefined
518
+ }), withoutAgent ? Layer.empty : filesLayer({ plan: built.plan, verifyRuntime }), preview ? Layer.empty : workspaceSourceLayer({ environment: config, verifyRuntime }), chatHistory === undefined
477
519
  ? Layer.empty
478
520
  : archiveRoutes({
479
521
  transport: chatHistory,
@@ -546,12 +588,23 @@ export function createOntologyService(ontology, config) {
546
588
  requireDeploymentCapability(deployment);
547
589
  if (preview) {
548
590
  const path = new URL(request.url).pathname;
549
- const blocked = /^\/api\/ontology\/(?:agent|agents|chat|attachments|channels)(?:\/|$)/.test(path)
591
+ // A preview that runs the agent serves the routes its web chat and evals use, and still
592
+ // none of channels, pairings, accounts, visitors or tasks.
593
+ const agentRoute = previewAgent &&
594
+ (PREVIEW_AGENT_ROUTE.test(path) ||
595
+ /^\/api\/ontology\/(?:chat|attachments)(?:\/|$)/.test(path));
596
+ const blocked = !agentRoute &&
597
+ /^\/api\/ontology\/(?:agent|agents|chat|attachments|channels)(?:\/|$)/.test(path)
550
598
  ? "agents"
551
599
  : undefined;
552
600
  if (blocked) {
553
601
  throw new DeploymentAdmissionError("preview_capability_unavailable", blocked);
554
602
  }
603
+ if (previewAgent &&
604
+ runtimeIdentity !== undefined &&
605
+ !sameDeployment(request, runtimeIdentity, deployment.deploymentId)) {
606
+ throw new DeploymentAdmissionError("preview_deployment_mismatch");
607
+ }
555
608
  if (/^\/api\/ontology\/(?:oauth|oidc)(?:\/|$)/.test(path) ||
556
609
  (!browser && /^\/api\/ontology\/federation(?:\/|$)/.test(path)) ||
557
610
  path.startsWith("/.well-known/oauth")) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@segmentstream/ontology",
3
- "version": "0.161.0",
3
+ "version": "0.163.0",
4
4
  "description": "Define a SegmentStream workspace app's ontology: entities, relationships, roles and actions, written with Effect.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "SegmentStream",