@tanstack/ai 0.58.0 → 0.61.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 (144) hide show
  1. package/dist/esm/activities/chat/adapter.d.ts +9 -0
  2. package/dist/esm/activities/chat/adapter.js +1 -0
  3. package/dist/esm/activities/chat/adapter.js.map +1 -1
  4. package/dist/esm/activities/chat/agents/define-agent.d.ts +81 -0
  5. package/dist/esm/activities/chat/agents/define-agent.js +34 -0
  6. package/dist/esm/activities/chat/agents/define-agent.js.map +1 -0
  7. package/dist/esm/activities/chat/agents/route.d.ts +53 -0
  8. package/dist/esm/activities/chat/agents/route.js +59 -0
  9. package/dist/esm/activities/chat/agents/route.js.map +1 -0
  10. package/dist/esm/activities/chat/agents/spawn.d.ts +124 -0
  11. package/dist/esm/activities/chat/agents/spawn.js +490 -0
  12. package/dist/esm/activities/chat/agents/spawn.js.map +1 -0
  13. package/dist/esm/activities/chat/agents/turn.d.ts +36 -0
  14. package/dist/esm/activities/chat/agents/turn.js +78 -0
  15. package/dist/esm/activities/chat/agents/turn.js.map +1 -0
  16. package/dist/esm/activities/chat/index.d.ts +13 -3
  17. package/dist/esm/activities/chat/index.js +462 -26
  18. package/dist/esm/activities/chat/index.js.map +1 -1
  19. package/dist/esm/activities/chat/messages.d.ts +7 -1
  20. package/dist/esm/activities/chat/messages.js +99 -19
  21. package/dist/esm/activities/chat/messages.js.map +1 -1
  22. package/dist/esm/activities/chat/middleware/run-store.d.ts +43 -7
  23. package/dist/esm/activities/chat/middleware/run-store.js +8 -1
  24. package/dist/esm/activities/chat/middleware/run-store.js.map +1 -1
  25. package/dist/esm/activities/chat/middleware/types.d.ts +47 -1
  26. package/dist/esm/activities/chat/middleware/types.js.map +1 -1
  27. package/dist/esm/activities/chat/stream/message-updaters.js +9 -2
  28. package/dist/esm/activities/chat/stream/message-updaters.js.map +1 -1
  29. package/dist/esm/activities/chat/stream/processor.d.ts +46 -1
  30. package/dist/esm/activities/chat/stream/processor.js +294 -18
  31. package/dist/esm/activities/chat/stream/processor.js.map +1 -1
  32. package/dist/esm/activities/chat/tools/tool-calls.d.ts +17 -3
  33. package/dist/esm/activities/chat/tools/tool-calls.js +56 -5
  34. package/dist/esm/activities/chat/tools/tool-calls.js.map +1 -1
  35. package/dist/esm/activities/embed/adapter.d.ts +7 -0
  36. package/dist/esm/activities/embed/adapter.js +1 -0
  37. package/dist/esm/activities/embed/adapter.js.map +1 -1
  38. package/dist/esm/activities/embed/index.js +2 -0
  39. package/dist/esm/activities/embed/index.js.map +1 -1
  40. package/dist/esm/activities/files/adapter.d.ts +97 -0
  41. package/dist/esm/activities/files/adapter.js +45 -0
  42. package/dist/esm/activities/files/adapter.js.map +1 -0
  43. package/dist/esm/activities/files/index.d.ts +66 -0
  44. package/dist/esm/activities/files/index.js +78 -0
  45. package/dist/esm/activities/files/index.js.map +1 -0
  46. package/dist/esm/activities/generateAudio/index.js +1 -1
  47. package/dist/esm/activities/generateImage/adapter.d.ts +8 -0
  48. package/dist/esm/activities/generateImage/adapter.js +1 -0
  49. package/dist/esm/activities/generateImage/adapter.js.map +1 -1
  50. package/dist/esm/activities/generateImage/index.js +3 -1
  51. package/dist/esm/activities/generateImage/index.js.map +1 -1
  52. package/dist/esm/activities/generateLiveVideo/index.js +1 -1
  53. package/dist/esm/activities/generateSpeech/index.js +1 -1
  54. package/dist/esm/activities/generateTranscription/index.js +1 -1
  55. package/dist/esm/activities/generateVideo/adapter.d.ts +8 -0
  56. package/dist/esm/activities/generateVideo/adapter.js +1 -0
  57. package/dist/esm/activities/generateVideo/adapter.js.map +1 -1
  58. package/dist/esm/activities/generateVideo/index.js +3 -0
  59. package/dist/esm/activities/generateVideo/index.js.map +1 -1
  60. package/dist/esm/activities/generateVoice/index.js +1 -1
  61. package/dist/esm/activities/generateWorld/adapter.d.ts +4 -2
  62. package/dist/esm/activities/generateWorld/adapter.js.map +1 -1
  63. package/dist/esm/activities/generateWorld/index.d.ts +4 -3
  64. package/dist/esm/activities/generateWorld/index.js +6 -5
  65. package/dist/esm/activities/generateWorld/index.js.map +1 -1
  66. package/dist/esm/activities/index.d.ts +9 -3
  67. package/dist/esm/activities/index.js +17 -13
  68. package/dist/esm/activities/summarize/chat-stream-summarize.d.ts +2 -0
  69. package/dist/esm/activities/summarize/chat-stream-summarize.js +8 -8
  70. package/dist/esm/activities/summarize/chat-stream-summarize.js.map +1 -1
  71. package/dist/esm/activities/summarize/index.js +1 -1
  72. package/dist/esm/client.d.ts +7 -36
  73. package/dist/esm/client.js +5 -37
  74. package/dist/esm/client.js.map +1 -1
  75. package/dist/esm/index.d.ts +8 -2
  76. package/dist/esm/index.js +9 -4
  77. package/dist/esm/middlewares/content-guard.js.map +1 -1
  78. package/dist/esm/types.d.ts +179 -98
  79. package/dist/esm/utilities/adapter-yield-chunk.d.ts +5 -1
  80. package/dist/esm/utilities/ag-ui-usage.d.ts +9 -9
  81. package/dist/esm/utilities/ag-ui-usage.js +66 -3
  82. package/dist/esm/utilities/ag-ui-usage.js.map +1 -1
  83. package/dist/esm/utilities/ag-ui-wire.js +90 -13
  84. package/dist/esm/utilities/ag-ui-wire.js.map +1 -1
  85. package/dist/esm/utilities/content-source.d.ts +60 -0
  86. package/dist/esm/utilities/content-source.js +85 -0
  87. package/dist/esm/utilities/content-source.js.map +1 -0
  88. package/dist/esm/utilities/normalize-stream-chunk.js +7 -2
  89. package/dist/esm/utilities/normalize-stream-chunk.js.map +1 -1
  90. package/dist/esm/utilities/provider-executed.d.ts +7 -0
  91. package/dist/esm/utilities/provider-executed.js +10 -1
  92. package/dist/esm/utilities/provider-executed.js.map +1 -1
  93. package/dist/esm/utilities/spec-event-keys.js +13 -8
  94. package/dist/esm/utilities/spec-event-keys.js.map +1 -1
  95. package/dist/esm/utilities/subagent-wire.d.ts +36 -0
  96. package/dist/esm/utilities/subagent-wire.js +131 -0
  97. package/dist/esm/utilities/subagent-wire.js.map +1 -0
  98. package/dist/esm/utilities/tool-result.d.ts +12 -2
  99. package/dist/esm/utilities/tool-result.js +23 -3
  100. package/dist/esm/utilities/tool-result.js.map +1 -1
  101. package/package.json +3 -3
  102. package/skills/ai-core/adapter-configuration/SKILL.md +62 -0
  103. package/skills/ai-core/adapter-configuration/references/grok-adapter.md +1 -1
  104. package/skills/ai-core/chat-experience/SKILL.md +14 -0
  105. package/skills/ai-core/media-generation/SKILL.md +10 -2
  106. package/skills/ai-core/middleware/SKILL.md +7 -4
  107. package/src/activities/chat/adapter.ts +10 -0
  108. package/src/activities/chat/agents/define-agent.ts +121 -0
  109. package/src/activities/chat/agents/route.ts +115 -0
  110. package/src/activities/chat/agents/spawn.ts +806 -0
  111. package/src/activities/chat/agents/turn.ts +151 -0
  112. package/src/activities/chat/index.ts +734 -30
  113. package/src/activities/chat/messages.ts +137 -16
  114. package/src/activities/chat/middleware/run-store.ts +56 -7
  115. package/src/activities/chat/middleware/types.ts +47 -0
  116. package/src/activities/chat/stream/message-updaters.ts +24 -2
  117. package/src/activities/chat/stream/processor.ts +452 -30
  118. package/src/activities/chat/tools/tool-calls.ts +83 -11
  119. package/src/activities/embed/adapter.ts +7 -0
  120. package/src/activities/embed/index.ts +5 -0
  121. package/src/activities/files/adapter.ts +120 -0
  122. package/src/activities/files/index.ts +113 -0
  123. package/src/activities/generateImage/adapter.ts +8 -0
  124. package/src/activities/generateImage/index.ts +4 -0
  125. package/src/activities/generateVideo/adapter.ts +8 -0
  126. package/src/activities/generateVideo/index.ts +7 -0
  127. package/src/activities/generateWorld/adapter.ts +4 -2
  128. package/src/activities/generateWorld/index.ts +7 -6
  129. package/src/activities/index.ts +40 -1
  130. package/src/activities/summarize/chat-stream-summarize.ts +22 -12
  131. package/src/client.ts +29 -35
  132. package/src/index.ts +39 -0
  133. package/src/middlewares/content-guard.ts +7 -5
  134. package/src/types.ts +226 -103
  135. package/src/utilities/adapter-yield-chunk.ts +10 -2
  136. package/src/utilities/ag-ui-usage.test.ts +38 -0
  137. package/src/utilities/ag-ui-usage.ts +98 -11
  138. package/src/utilities/ag-ui-wire.ts +134 -16
  139. package/src/utilities/content-source.ts +138 -0
  140. package/src/utilities/normalize-stream-chunk.ts +10 -2
  141. package/src/utilities/provider-executed.ts +13 -0
  142. package/src/utilities/spec-event-keys.ts +34 -7
  143. package/src/utilities/subagent-wire.ts +184 -0
  144. package/src/utilities/tool-result.ts +38 -2
@@ -69,6 +69,14 @@ export interface TextAdapter<TModel extends string, TProviderOptions extends Rec
69
69
  * this is the declaration/validation surface only.
70
70
  */
71
71
  readonly requires?: ReadonlyArray<CapabilityHandle>;
72
+ /**
73
+ * Declares that this adapter can consume `{ type: 'file' }` content sources
74
+ * (provider Files API references). `chat()` rejects file sources in preflight
75
+ * for adapters that don't declare this, so an adapter written before the
76
+ * file arm existed fails closed instead of silently mis-mapping a reference
77
+ * onto its URL/data branch.
78
+ */
79
+ readonly supportsFileSources?: boolean;
72
80
  /**
73
81
  * @internal Type-only properties for inference. Not assigned at runtime.
74
82
  */
@@ -156,6 +164,7 @@ export declare abstract class BaseTextAdapter<TModel extends string, TProviderOp
156
164
  abstract readonly name: string;
157
165
  readonly model: TModel;
158
166
  readonly requires?: ReadonlyArray<CapabilityHandle>;
167
+ readonly supportsFileSources: boolean;
159
168
  '~types': {
160
169
  providerOptions: TProviderOptions;
161
170
  inputModalities: TInputModalities;
@@ -9,6 +9,7 @@ var BaseTextAdapter = class {
9
9
  kind = "text";
10
10
  model;
11
11
  requires = void 0;
12
+ supportsFileSources = false;
12
13
  config;
13
14
  constructor(config = {}, model) {
14
15
  this.config = config;
@@ -1 +1 @@
1
- {"version":3,"file":"adapter.js","names":[],"sources":["../../../../src/activities/chat/adapter.ts"],"sourcesContent":["import type {\n DefaultMessageMetadataByModality,\n JSONSchema,\n Modality,\n TextOptions,\n TokenUsage,\n} from '../../types'\nimport type { AdapterYieldChunk } from '../../utilities/adapter-yield-chunk'\nimport type { CapabilityHandle } from './middleware/capabilities'\n\n/**\n * Configuration for adapter instances\n */\nexport interface TextAdapterConfig {\n apiKey?: string\n baseUrl?: string\n timeout?: number\n maxRetries?: number\n headers?: Record<string, string>\n}\n\n/**\n * Options for structured output generation.\n *\n * The internal logger is threaded through `chatOptions.logger` (inherited from\n * `TextOptions`). Adapter implementations must call `logger.request()` before\n * SDK calls, `logger.provider()` for each chunk received, and `logger.errors()`\n * in catch blocks.\n */\nexport interface StructuredOutputOptions<TProviderOptions extends object> {\n /** Text options for the request */\n chatOptions: TextOptions<TProviderOptions>\n /** JSON Schema for structured output - already converted from Zod in the ai layer */\n outputSchema: JSONSchema\n}\n\n/**\n * Result from structured output generation\n */\nexport interface StructuredOutputResult<T = unknown> {\n /** The parsed data conforming to the schema */\n data: T\n /** The raw text response from the model before parsing */\n rawText: string\n /** Token usage information (if provided by the adapter) */\n usage?: TokenUsage\n}\n\n/**\n * Text adapter interface with pre-resolved generics.\n *\n * An adapter is created by a provider function: `provider('model')` → `adapter`\n * All type resolution happens at the provider call site, not in this interface.\n *\n * Generic parameters:\n * - TModel: The specific model name (e.g., 'gpt-4o')\n * - TProviderOptions: Provider-specific options for this model (already resolved)\n * - TInputModalities: Supported input modalities for this model (already resolved)\n * - TMessageMetadata: Metadata types for content parts (already resolved)\n * - TToolCapabilities: Tuple of tool-kind strings supported by this model, resolved from `supports.tools`\n * - TToolCallMetadata: Metadata type that round-trips with tool calls (e.g. Gemini's `thoughtSignature`)\n * - TSystemPromptMetadata: Provider-typed metadata accepted on each\n * `systemPrompts[i]` entry (e.g. Anthropic `cache_control`). Defaults to\n * `never` — adapters without per-prompt metadata reject the `metadata`\n * field at the call site.\n */\nexport interface TextAdapter<\n TModel extends string,\n TProviderOptions extends Record<string, any>,\n TInputModalities extends ReadonlyArray<Modality>,\n TMessageMetadataByModality extends DefaultMessageMetadataByModality,\n TToolCapabilities extends ReadonlyArray<string> = ReadonlyArray<string>,\n TToolCallMetadata = unknown,\n TSystemPromptMetadata = never,\n> {\n /** Discriminator for adapter kind */\n readonly kind: 'text'\n /** Provider name identifier (e.g., 'openai', 'anthropic') */\n readonly name: string\n /** The model this adapter is configured for */\n readonly model: TModel\n\n /**\n * Capabilities this adapter requires at runtime. `chat()` validates that the\n * configured middleware provides each one. Model adapters omit this; harness\n * adapters (e.g. a future `claudeCode()`) declare e.g. `[sandboxCapability]`.\n * Runtime access to capabilities from inside the adapter is not yet wired —\n * this is the declaration/validation surface only.\n */\n readonly requires?: ReadonlyArray<CapabilityHandle>\n\n /**\n * @internal Type-only properties for inference. Not assigned at runtime.\n */\n '~types': {\n providerOptions: TProviderOptions\n inputModalities: TInputModalities\n messageMetadataByModality: TMessageMetadataByModality\n toolCapabilities: TToolCapabilities\n toolCallMetadata: TToolCallMetadata\n systemPromptMetadata: TSystemPromptMetadata\n }\n\n /**\n * Stream text completions from the model\n */\n chatStream: (\n options: TextOptions<TProviderOptions>,\n ) => AsyncIterable<AdapterYieldChunk>\n\n /**\n * Generate structured output using the provider's native structured output API.\n * This method uses stream: false and sends the JSON schema to the provider\n * to ensure the response conforms to the expected structure.\n *\n * @param options - Structured output options containing chat options and JSON schema\n * @returns Promise with the raw data (validation is done in the chat function)\n */\n structuredOutput: (\n options: StructuredOutputOptions<TProviderOptions>,\n ) => Promise<StructuredOutputResult<unknown>>\n\n /**\n * Stream structured output using the provider's native streaming structured\n * output API (stream + response_format json_schema in a single request).\n *\n * Optional — adapters without native streaming JSON omit this method and the\n * activity layer synthesizes a stream around the non-streaming\n * `structuredOutput` call.\n *\n * Implementations must emit standard AG-UI lifecycle events (RUN_STARTED,\n * TEXT_MESSAGE_*, RUN_FINISHED) carrying raw JSON text deltas, plus a final\n * `CUSTOM` event named `structured-output.complete` whose `value` is\n * `{ object, raw, reasoning? }`. Events must be timestamped when emitted so\n * their timestamps follow stream order.\n */\n structuredOutputStream?: (\n options: StructuredOutputOptions<TProviderOptions>,\n ) => AsyncIterable<AdapterYieldChunk>\n\n /**\n * Declares whether the adapter supports combining `tools` and a\n * schema-constrained final answer in a single streaming request.\n *\n * When `true`, the engine wires `outputSchema` into the regular\n * `chatStream()` call and skips the separate `runStructuredFinalization`\n * round-trip. The model's natural final turn carries the\n * schema-constrained JSON text and the engine harvests it from the agent\n * loop's accumulated content.\n *\n * When `false`, `undefined`, or the method is omitted, the engine runs\n * the agent loop without `outputSchema` and then issues a separate\n * `structuredOutput` / `structuredOutputStream` call against the JSON\n * schema for finalization (the legacy path).\n *\n * The method receives the per-call `modelOptions` so providers whose\n * support depends on the resolved upstream model (e.g. OpenRouter) can\n * answer per-request. Most adapters can return a constant.\n */\n supportsCombinedToolsAndSchema?: (\n modelOptions?: TProviderOptions | undefined,\n ) => boolean\n\n /**\n * Where native-combined structured output is taken from.\n *\n * - `'text'` (default when omitted): the agent loop's accumulated\n * assistant text is schema JSON. The engine parses it after the loop.\n * HTTP adapters use this.\n * - `'event'`: the adapter emits `structured-output.complete` during\n * `chatStream`. The engine must not parse accumulated prose. Harness\n * adapters use this.\n */\n combinedStructuredOutputSource?: (\n modelOptions?: TProviderOptions | undefined,\n ) => 'text' | 'event'\n}\n\n/**\n * A TextAdapter with any/unknown type parameters.\n * Useful as a constraint in generic functions and interfaces.\n */\nexport type AnyTextAdapter = TextAdapter<any, any, any, any, any, any, any>\n\n/**\n * Abstract base class for text adapters.\n * Extend this class to implement a text adapter for a specific provider.\n *\n * Generic parameters match TextAdapter - all pre-resolved by the provider function.\n */\nexport abstract class BaseTextAdapter<\n TModel extends string,\n TProviderOptions extends Record<string, any>,\n TInputModalities extends ReadonlyArray<Modality>,\n TMessageMetadataByModality extends DefaultMessageMetadataByModality,\n TToolCapabilities extends ReadonlyArray<string> = ReadonlyArray<string>,\n TToolCallMetadata = unknown,\n TSystemPromptMetadata = never,\n> implements TextAdapter<\n TModel,\n TProviderOptions,\n TInputModalities,\n TMessageMetadataByModality,\n TToolCapabilities,\n TToolCallMetadata,\n TSystemPromptMetadata\n> {\n readonly kind = 'text' as const\n abstract readonly name: string\n readonly model: TModel\n readonly requires?: ReadonlyArray<CapabilityHandle> = undefined\n\n // Type-only property - never assigned at runtime\n declare '~types': {\n providerOptions: TProviderOptions\n inputModalities: TInputModalities\n messageMetadataByModality: TMessageMetadataByModality\n toolCapabilities: TToolCapabilities\n toolCallMetadata: TToolCallMetadata\n systemPromptMetadata: TSystemPromptMetadata\n }\n\n protected config: TextAdapterConfig\n\n constructor(config: TextAdapterConfig = {}, model: TModel) {\n this.config = config\n this.model = model\n }\n\n abstract chatStream(\n options: TextOptions<TProviderOptions>,\n ): AsyncIterable<AdapterYieldChunk>\n\n /**\n * Generate structured output using the provider's native structured output API.\n * Concrete implementations should override this to use provider-specific structured output.\n */\n abstract structuredOutput(\n options: StructuredOutputOptions<TProviderOptions>,\n ): Promise<StructuredOutputResult<unknown>>\n\n protected generateId(): string {\n return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(7)}`\n }\n}\n"],"mappings":";;;;;;;AA8LA,IAAsB,kBAAtB,MAgBE;CACA,OAAgB;CAEhB;CACA,WAAsD,KAAA;CAYtD;CAEA,YAAY,SAA4B,CAAC,GAAG,OAAe;EACzD,KAAK,SAAS;EACd,KAAK,QAAQ;CACf;CAcA,aAA+B;EAC7B,OAAO,GAAG,KAAK,KAAK,GAAG,KAAK,IAAI,EAAE,GAAG,KAAK,OAAO,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,UAAU,CAAC;CAC7E;AACF"}
1
+ {"version":3,"file":"adapter.js","names":[],"sources":["../../../../src/activities/chat/adapter.ts"],"sourcesContent":["import type {\n DefaultMessageMetadataByModality,\n JSONSchema,\n Modality,\n TextOptions,\n TokenUsage,\n} from '../../types'\nimport type { AdapterYieldChunk } from '../../utilities/adapter-yield-chunk'\nimport type { CapabilityHandle } from './middleware/capabilities'\n\n/**\n * Configuration for adapter instances\n */\nexport interface TextAdapterConfig {\n apiKey?: string\n baseUrl?: string\n timeout?: number\n maxRetries?: number\n headers?: Record<string, string>\n}\n\n/**\n * Options for structured output generation.\n *\n * The internal logger is threaded through `chatOptions.logger` (inherited from\n * `TextOptions`). Adapter implementations must call `logger.request()` before\n * SDK calls, `logger.provider()` for each chunk received, and `logger.errors()`\n * in catch blocks.\n */\nexport interface StructuredOutputOptions<TProviderOptions extends object> {\n /** Text options for the request */\n chatOptions: TextOptions<TProviderOptions>\n /** JSON Schema for structured output - already converted from Zod in the ai layer */\n outputSchema: JSONSchema\n}\n\n/**\n * Result from structured output generation\n */\nexport interface StructuredOutputResult<T = unknown> {\n /** The parsed data conforming to the schema */\n data: T\n /** The raw text response from the model before parsing */\n rawText: string\n /** Token usage information (if provided by the adapter) */\n usage?: TokenUsage\n}\n\n/**\n * Text adapter interface with pre-resolved generics.\n *\n * An adapter is created by a provider function: `provider('model')` → `adapter`\n * All type resolution happens at the provider call site, not in this interface.\n *\n * Generic parameters:\n * - TModel: The specific model name (e.g., 'gpt-4o')\n * - TProviderOptions: Provider-specific options for this model (already resolved)\n * - TInputModalities: Supported input modalities for this model (already resolved)\n * - TMessageMetadata: Metadata types for content parts (already resolved)\n * - TToolCapabilities: Tuple of tool-kind strings supported by this model, resolved from `supports.tools`\n * - TToolCallMetadata: Metadata type that round-trips with tool calls (e.g. Gemini's `thoughtSignature`)\n * - TSystemPromptMetadata: Provider-typed metadata accepted on each\n * `systemPrompts[i]` entry (e.g. Anthropic `cache_control`). Defaults to\n * `never` — adapters without per-prompt metadata reject the `metadata`\n * field at the call site.\n */\nexport interface TextAdapter<\n TModel extends string,\n TProviderOptions extends Record<string, any>,\n TInputModalities extends ReadonlyArray<Modality>,\n TMessageMetadataByModality extends DefaultMessageMetadataByModality,\n TToolCapabilities extends ReadonlyArray<string> = ReadonlyArray<string>,\n TToolCallMetadata = unknown,\n TSystemPromptMetadata = never,\n> {\n /** Discriminator for adapter kind */\n readonly kind: 'text'\n /** Provider name identifier (e.g., 'openai', 'anthropic') */\n readonly name: string\n /** The model this adapter is configured for */\n readonly model: TModel\n\n /**\n * Capabilities this adapter requires at runtime. `chat()` validates that the\n * configured middleware provides each one. Model adapters omit this; harness\n * adapters (e.g. a future `claudeCode()`) declare e.g. `[sandboxCapability]`.\n * Runtime access to capabilities from inside the adapter is not yet wired —\n * this is the declaration/validation surface only.\n */\n readonly requires?: ReadonlyArray<CapabilityHandle>\n\n /**\n * Declares that this adapter can consume `{ type: 'file' }` content sources\n * (provider Files API references). `chat()` rejects file sources in preflight\n * for adapters that don't declare this, so an adapter written before the\n * file arm existed fails closed instead of silently mis-mapping a reference\n * onto its URL/data branch.\n */\n readonly supportsFileSources?: boolean\n\n /**\n * @internal Type-only properties for inference. Not assigned at runtime.\n */\n '~types': {\n providerOptions: TProviderOptions\n inputModalities: TInputModalities\n messageMetadataByModality: TMessageMetadataByModality\n toolCapabilities: TToolCapabilities\n toolCallMetadata: TToolCallMetadata\n systemPromptMetadata: TSystemPromptMetadata\n }\n\n /**\n * Stream text completions from the model\n */\n chatStream: (\n options: TextOptions<TProviderOptions>,\n ) => AsyncIterable<AdapterYieldChunk>\n\n /**\n * Generate structured output using the provider's native structured output API.\n * This method uses stream: false and sends the JSON schema to the provider\n * to ensure the response conforms to the expected structure.\n *\n * @param options - Structured output options containing chat options and JSON schema\n * @returns Promise with the raw data (validation is done in the chat function)\n */\n structuredOutput: (\n options: StructuredOutputOptions<TProviderOptions>,\n ) => Promise<StructuredOutputResult<unknown>>\n\n /**\n * Stream structured output using the provider's native streaming structured\n * output API (stream + response_format json_schema in a single request).\n *\n * Optional — adapters without native streaming JSON omit this method and the\n * activity layer synthesizes a stream around the non-streaming\n * `structuredOutput` call.\n *\n * Implementations must emit standard AG-UI lifecycle events (RUN_STARTED,\n * TEXT_MESSAGE_*, RUN_FINISHED) carrying raw JSON text deltas, plus a final\n * `CUSTOM` event named `structured-output.complete` whose `value` is\n * `{ object, raw, reasoning? }`. Events must be timestamped when emitted so\n * their timestamps follow stream order.\n */\n structuredOutputStream?: (\n options: StructuredOutputOptions<TProviderOptions>,\n ) => AsyncIterable<AdapterYieldChunk>\n\n /**\n * Declares whether the adapter supports combining `tools` and a\n * schema-constrained final answer in a single streaming request.\n *\n * When `true`, the engine wires `outputSchema` into the regular\n * `chatStream()` call and skips the separate `runStructuredFinalization`\n * round-trip. The model's natural final turn carries the\n * schema-constrained JSON text and the engine harvests it from the agent\n * loop's accumulated content.\n *\n * When `false`, `undefined`, or the method is omitted, the engine runs\n * the agent loop without `outputSchema` and then issues a separate\n * `structuredOutput` / `structuredOutputStream` call against the JSON\n * schema for finalization (the legacy path).\n *\n * The method receives the per-call `modelOptions` so providers whose\n * support depends on the resolved upstream model (e.g. OpenRouter) can\n * answer per-request. Most adapters can return a constant.\n */\n supportsCombinedToolsAndSchema?: (\n modelOptions?: TProviderOptions | undefined,\n ) => boolean\n\n /**\n * Where native-combined structured output is taken from.\n *\n * - `'text'` (default when omitted): the agent loop's accumulated\n * assistant text is schema JSON. The engine parses it after the loop.\n * HTTP adapters use this.\n * - `'event'`: the adapter emits `structured-output.complete` during\n * `chatStream`. The engine must not parse accumulated prose. Harness\n * adapters use this.\n */\n combinedStructuredOutputSource?: (\n modelOptions?: TProviderOptions | undefined,\n ) => 'text' | 'event'\n}\n\n/**\n * A TextAdapter with any/unknown type parameters.\n * Useful as a constraint in generic functions and interfaces.\n */\nexport type AnyTextAdapter = TextAdapter<any, any, any, any, any, any, any>\n\n/**\n * Abstract base class for text adapters.\n * Extend this class to implement a text adapter for a specific provider.\n *\n * Generic parameters match TextAdapter - all pre-resolved by the provider function.\n */\nexport abstract class BaseTextAdapter<\n TModel extends string,\n TProviderOptions extends Record<string, any>,\n TInputModalities extends ReadonlyArray<Modality>,\n TMessageMetadataByModality extends DefaultMessageMetadataByModality,\n TToolCapabilities extends ReadonlyArray<string> = ReadonlyArray<string>,\n TToolCallMetadata = unknown,\n TSystemPromptMetadata = never,\n> implements TextAdapter<\n TModel,\n TProviderOptions,\n TInputModalities,\n TMessageMetadataByModality,\n TToolCapabilities,\n TToolCallMetadata,\n TSystemPromptMetadata\n> {\n readonly kind = 'text' as const\n abstract readonly name: string\n readonly model: TModel\n readonly requires?: ReadonlyArray<CapabilityHandle> = undefined\n readonly supportsFileSources: boolean = false\n\n // Type-only property - never assigned at runtime\n declare '~types': {\n providerOptions: TProviderOptions\n inputModalities: TInputModalities\n messageMetadataByModality: TMessageMetadataByModality\n toolCapabilities: TToolCapabilities\n toolCallMetadata: TToolCallMetadata\n systemPromptMetadata: TSystemPromptMetadata\n }\n\n protected config: TextAdapterConfig\n\n constructor(config: TextAdapterConfig = {}, model: TModel) {\n this.config = config\n this.model = model\n }\n\n abstract chatStream(\n options: TextOptions<TProviderOptions>,\n ): AsyncIterable<AdapterYieldChunk>\n\n /**\n * Generate structured output using the provider's native structured output API.\n * Concrete implementations should override this to use provider-specific structured output.\n */\n abstract structuredOutput(\n options: StructuredOutputOptions<TProviderOptions>,\n ): Promise<StructuredOutputResult<unknown>>\n\n protected generateId(): string {\n return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(7)}`\n }\n}\n"],"mappings":";;;;;;;AAuMA,IAAsB,kBAAtB,MAgBE;CACA,OAAgB;CAEhB;CACA,WAAsD,KAAA;CACtD,sBAAwC;CAYxC;CAEA,YAAY,SAA4B,CAAC,GAAG,OAAe;EACzD,KAAK,SAAS;EACd,KAAK,QAAQ;CACf;CAcA,aAA+B;EAC7B,OAAO,GAAG,KAAK,KAAK,GAAG,KAAK,IAAI,EAAE,GAAG,KAAK,OAAO,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,UAAU,CAAC;CAC7E;AACF"}
@@ -0,0 +1,81 @@
1
+ import { SubagentInfo as AGUISubagentInfo } from '@ag-ui/core';
2
+ import { InterruptDefinition } from '../../../interrupt-definition.js';
3
+ import { AnyTool, ModelMessage, RunAgentResumeItem, SchemaInput, StreamChunk, UIMessage } from '../../../types.js';
4
+ import { AnyClientTool } from '../tools/tool-definition.js';
5
+ /**
6
+ * Context the library passes into {@link defineAgent} `run`.
7
+ */
8
+ export interface SubagentRunContext {
9
+ messages: Array<UIMessage | ModelMessage>;
10
+ abortSignal?: AbortSignal;
11
+ threadId: string;
12
+ /** Run id for the child `chat()`. */
13
+ runId: string;
14
+ /**
15
+ * The run this child run continues. It is the parent chat run on the first
16
+ * run, and the interrupted parent run on a resume. Pass it to the child
17
+ * `chat()`.
18
+ */
19
+ parentRunId: string;
20
+ /** Answers to this child's interrupts. Pass it to the child `chat()`. */
21
+ resume?: Array<RunAgentResumeItem>;
22
+ /**
23
+ * The child's AG-UI run id. Stays the same when an interrupted child
24
+ * continues. Pass it to the child `chat()` so its middleware sees
25
+ * `ctx.subagentRunId`.
26
+ */
27
+ subagentRunId: string;
28
+ parentSubagentRunId?: string;
29
+ }
30
+ /**
31
+ * A tool a child agent can carry into client part types.
32
+ * Server tools and client tools both qualify.
33
+ */
34
+ export type SubagentTool = AnyTool | AnyClientTool;
35
+ /**
36
+ * A named child agent. `run` is a `chat()` call (or any stream of AG-UI chunks).
37
+ * `TTools` and `TSchema` stay on the object so `useChat({ subagents })` can
38
+ * type that child's parts.
39
+ */
40
+ export interface DefinedAgent<TName extends string = string, TTools extends ReadonlyArray<SubagentTool> = ReadonlyArray<SubagentTool>, TSchema extends SchemaInput | undefined = SchemaInput | undefined, TInterrupts extends ReadonlyArray<InterruptDefinition<any, any, any, any>> = ReadonlyArray<InterruptDefinition<any, any, any, any>>> extends AGUISubagentInfo {
41
+ name: TName;
42
+ /** Required here: the router and the synthetic tool both read it. */
43
+ description: string;
44
+ run: (ctx: SubagentRunContext) => AsyncIterable<StreamChunk> | Promise<AsyncIterable<StreamChunk>>;
45
+ tools?: TTools;
46
+ interrupts?: TInterrupts;
47
+ outputSchema?: TSchema;
48
+ subagents?: unknown;
49
+ }
50
+ /**
51
+ * Choice options for a `decide()` router. `main` is required plus every agent name.
52
+ */
53
+ export type SubagentChoiceOptions<TAgents extends ReadonlyArray<DefinedAgent>> = {
54
+ main: string;
55
+ } & {
56
+ [K in TAgents[number]['name']]: string;
57
+ };
58
+ /**
59
+ * Define a named child agent. Pass the same object to `chat({ subagents })`.
60
+ * Pass the agents array to `useChat({ subagents })` when you render parts
61
+ * yourself. The hook uses it for types only. It does not call `run`.
62
+ *
63
+ * @example
64
+ * ```ts
65
+ * const researcher = defineAgent({
66
+ * name: 'researcher',
67
+ * description: 'Looks up facts',
68
+ * run: (ctx) =>
69
+ * chat({
70
+ * adapter: openaiText('gpt-5.6'),
71
+ * messages: ctx.messages,
72
+ * threadId: ctx.threadId,
73
+ * runId: ctx.runId,
74
+ * parentRunId: ctx.parentRunId,
75
+ * subagentRunId: ctx.subagentRunId,
76
+ * resume: ctx.resume,
77
+ * }),
78
+ * })
79
+ * ```
80
+ */
81
+ export declare function defineAgent<const TName extends string, const TTools extends ReadonlyArray<SubagentTool> = readonly [], TSchema extends SchemaInput | undefined = undefined, const TInterrupts extends ReadonlyArray<InterruptDefinition<any, any, any, any>> = readonly []>(agent: DefinedAgent<TName, TTools, TSchema, TInterrupts>): DefinedAgent<TName, TTools, TSchema, TInterrupts>;
@@ -0,0 +1,34 @@
1
+ //#region src/activities/chat/agents/define-agent.ts
2
+ /**
3
+ * Define a named child agent. Pass the same object to `chat({ subagents })`.
4
+ * Pass the agents array to `useChat({ subagents })` when you render parts
5
+ * yourself. The hook uses it for types only. It does not call `run`.
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const researcher = defineAgent({
10
+ * name: 'researcher',
11
+ * description: 'Looks up facts',
12
+ * run: (ctx) =>
13
+ * chat({
14
+ * adapter: openaiText('gpt-5.6'),
15
+ * messages: ctx.messages,
16
+ * threadId: ctx.threadId,
17
+ * runId: ctx.runId,
18
+ * parentRunId: ctx.parentRunId,
19
+ * subagentRunId: ctx.subagentRunId,
20
+ * resume: ctx.resume,
21
+ * }),
22
+ * })
23
+ * ```
24
+ */
25
+ function defineAgent(agent) {
26
+ if (agent.name.trim() === "") throw new Error("defineAgent requires a non-empty name");
27
+ if (agent.name.trim() === "main") throw new Error("defineAgent cannot use the name 'main'. A router uses it for the parent.");
28
+ if (agent.description.trim() === "") throw new Error("defineAgent requires a non-empty description");
29
+ return agent;
30
+ }
31
+ //#endregion
32
+ export { defineAgent };
33
+
34
+ //# sourceMappingURL=define-agent.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"define-agent.js","names":[],"sources":["../../../../../src/activities/chat/agents/define-agent.ts"],"sourcesContent":["import type { SubagentInfo as AGUISubagentInfo } from '@ag-ui/core'\nimport type { InterruptDefinition } from '../../../interrupt-definition'\nimport type {\n AnyTool,\n ModelMessage,\n RunAgentResumeItem,\n SchemaInput,\n StreamChunk,\n UIMessage,\n} from '../../../types'\nimport type { AnyClientTool } from '../tools/tool-definition'\n\n/**\n * Context the library passes into {@link defineAgent} `run`.\n */\nexport interface SubagentRunContext {\n messages: Array<UIMessage | ModelMessage>\n abortSignal?: AbortSignal\n threadId: string\n /** Run id for the child `chat()`. */\n runId: string\n /**\n * The run this child run continues. It is the parent chat run on the first\n * run, and the interrupted parent run on a resume. Pass it to the child\n * `chat()`.\n */\n parentRunId: string\n /** Answers to this child's interrupts. Pass it to the child `chat()`. */\n resume?: Array<RunAgentResumeItem>\n /**\n * The child's AG-UI run id. Stays the same when an interrupted child\n * continues. Pass it to the child `chat()` so its middleware sees\n * `ctx.subagentRunId`.\n */\n subagentRunId: string\n parentSubagentRunId?: string\n}\n\n/**\n * A tool a child agent can carry into client part types.\n * Server tools and client tools both qualify.\n */\nexport type SubagentTool = AnyTool | AnyClientTool\n\n/**\n * A named child agent. `run` is a `chat()` call (or any stream of AG-UI chunks).\n * `TTools` and `TSchema` stay on the object so `useChat({ subagents })` can\n * type that child's parts.\n */\nexport interface DefinedAgent<\n TName extends string = string,\n TTools extends ReadonlyArray<SubagentTool> = ReadonlyArray<SubagentTool>,\n TSchema extends SchemaInput | undefined = SchemaInput | undefined,\n TInterrupts extends ReadonlyArray<InterruptDefinition<any, any, any, any>> =\n ReadonlyArray<InterruptDefinition<any, any, any, any>>,\n> extends AGUISubagentInfo {\n name: TName\n /** Required here: the router and the synthetic tool both read it. */\n description: string\n run: (\n ctx: SubagentRunContext,\n ) => AsyncIterable<StreamChunk> | Promise<AsyncIterable<StreamChunk>>\n tools?: TTools\n interrupts?: TInterrupts\n outputSchema?: TSchema\n subagents?: unknown\n}\n\n/**\n * Choice options for a `decide()` router. `main` is required plus every agent name.\n */\nexport type SubagentChoiceOptions<TAgents extends ReadonlyArray<DefinedAgent>> =\n { main: string } & {\n [K in TAgents[number]['name']]: string\n }\n\n/**\n * Define a named child agent. Pass the same object to `chat({ subagents })`.\n * Pass the agents array to `useChat({ subagents })` when you render parts\n * yourself. The hook uses it for types only. It does not call `run`.\n *\n * @example\n * ```ts\n * const researcher = defineAgent({\n * name: 'researcher',\n * description: 'Looks up facts',\n * run: (ctx) =>\n * chat({\n * adapter: openaiText('gpt-5.6'),\n * messages: ctx.messages,\n * threadId: ctx.threadId,\n * runId: ctx.runId,\n * parentRunId: ctx.parentRunId,\n * subagentRunId: ctx.subagentRunId,\n * resume: ctx.resume,\n * }),\n * })\n * ```\n */\nexport function defineAgent<\n const TName extends string,\n const TTools extends ReadonlyArray<SubagentTool> = readonly [],\n TSchema extends SchemaInput | undefined = undefined,\n const TInterrupts extends ReadonlyArray<\n InterruptDefinition<any, any, any, any>\n > = readonly [],\n>(agent: DefinedAgent<TName, TTools, TSchema, TInterrupts>) {\n if (agent.name.trim() === '') {\n throw new Error('defineAgent requires a non-empty name')\n }\n // A router returns 'main' to keep the turn on the parent.\n if (agent.name.trim() === 'main') {\n throw new Error(\n \"defineAgent cannot use the name 'main'. A router uses it for the parent.\",\n )\n }\n if (agent.description.trim() === '') {\n throw new Error('defineAgent requires a non-empty description')\n }\n return agent\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAmGA,SAAgB,YAOd,OAA0D;CAC1D,IAAI,MAAM,KAAK,KAAK,MAAM,IACxB,MAAM,IAAI,MAAM,uCAAuC;CAGzD,IAAI,MAAM,KAAK,KAAK,MAAM,QACxB,MAAM,IAAI,MACR,0EACF;CAEF,IAAI,MAAM,YAAY,KAAK,MAAM,IAC/B,MAAM,IAAI,MAAM,8CAA8C;CAEhE,OAAO;AACT"}
@@ -0,0 +1,53 @@
1
+ import { boolean, choice } from '../../evaluate/index.js';
2
+ import { DefinedAgent } from './define-agent.js';
3
+ import { SubagentOrder, SubagentRouterPick } from './spawn.js';
4
+ export interface SubagentRouteOptions<TAgents extends ReadonlyArray<DefinedAgent>> {
5
+ /**
6
+ * Question text for every agent. The key is the agent name.
7
+ * Omit this and each question uses that agent's description.
8
+ */
9
+ when?: {
10
+ [K in TAgents[number]['name']]: string;
11
+ };
12
+ /**
13
+ * Agents that run after the other selected agents. The lead group starts
14
+ * together. The `then` agents then run one after another in list order, and
15
+ * each reads the text so far. Used only when the router picks at least one
16
+ * agent from each group. Otherwise `pick` returns `{ names, order }`.
17
+ */
18
+ then?: ReadonlyArray<TAgents[number]['name']>;
19
+ }
20
+ type RouteQuestions<TAgents extends ReadonlyArray<DefinedAgent>> = {
21
+ [K in TAgents[number]['name']]: ReturnType<typeof boolean>;
22
+ } & {
23
+ order: ReturnType<typeof choice<{
24
+ parallel: string;
25
+ sequence: string;
26
+ }>>;
27
+ };
28
+ type RouteAnswers<TAgents extends ReadonlyArray<DefinedAgent>> = {
29
+ [K in TAgents[number]['name']]: {
30
+ value: boolean;
31
+ };
32
+ } & {
33
+ order: {
34
+ value: SubagentOrder;
35
+ };
36
+ };
37
+ /**
38
+ * Build `decide()` questions for a subagent router.
39
+ *
40
+ * One yes/no question per agent, plus an `order` choice.
41
+ * `pick` returns `main`, one name, `{ names, order }`, or `{ steps }`.
42
+ * Names follow the `agents` array order.
43
+ * `{ names, order }` overrides `subagents.order` for that turn.
44
+ * `then`: agents that run after the other selected agents. The lead group
45
+ * starts together. The `then` agents then run one after another in list
46
+ * order, and each reads the text so far. Used only when the router picks at
47
+ * least one agent from each group. Otherwise `pick` returns `{ names, order }`.
48
+ */
49
+ export declare function subagentRoute<const TAgents extends ReadonlyArray<DefinedAgent>>(agents: TAgents, options?: SubagentRouteOptions<TAgents>): {
50
+ questions: RouteQuestions<TAgents>;
51
+ pick: (result: RouteAnswers<TAgents>) => SubagentRouterPick;
52
+ };
53
+ export {};
@@ -0,0 +1,59 @@
1
+ import { boolean, choice } from "../../evaluate/index.js";
2
+ //#region src/activities/chat/agents/route.ts
3
+ var ORDER_KEY = "order";
4
+ /**
5
+ * Build `decide()` questions for a subagent router.
6
+ *
7
+ * One yes/no question per agent, plus an `order` choice.
8
+ * `pick` returns `main`, one name, `{ names, order }`, or `{ steps }`.
9
+ * Names follow the `agents` array order.
10
+ * `{ names, order }` overrides `subagents.order` for that turn.
11
+ * `then`: agents that run after the other selected agents. The lead group
12
+ * starts together. The `then` agents then run one after another in list
13
+ * order, and each reads the text so far. Used only when the router picks at
14
+ * least one agent from each group. Otherwise `pick` returns `{ names, order }`.
15
+ */
16
+ function subagentRoute(agents, options) {
17
+ const namesInList = new Set(agents.map((agent) => agent.name));
18
+ for (const agent of agents) if (agent.name === ORDER_KEY) throw new Error("subagentRoute cannot use an agent named \"order\". Rename that agent.");
19
+ for (const name of options?.then ?? []) if (!namesInList.has(name)) throw new Error(`subagentRoute then includes unknown agent "${name}".`);
20
+ const questions = { order: choice({
21
+ instructions: "When more than one agent runs, how must they run?",
22
+ options: {
23
+ parallel: "Start them together. Use this when no agent must read text from another agent. Working on the same topic is not a reason to wait.",
24
+ sequence: "Run them in agent-list order. Use this only when a later agent must read the earlier agent text, such as research notes and then a draft article."
25
+ }
26
+ }) };
27
+ for (const agent of agents) {
28
+ const when = options?.when?.[agent.name];
29
+ questions[agent.name] = boolean({ instructions: when ?? agent.description });
30
+ }
31
+ function pick(result) {
32
+ const names = agents.map((agent) => agent.name).filter((name) => result[name].value);
33
+ if (names.length === 0) return "main";
34
+ const only = names.length === 1 ? names[0] : void 0;
35
+ if (only !== void 0) return only;
36
+ const later = new Set(options?.then ?? []);
37
+ const lead = names.filter((name) => !later.has(name));
38
+ const tail = names.filter((name) => later.has(name));
39
+ if (lead.length === 0 || tail.length === 0) return {
40
+ names,
41
+ order: result.order.value
42
+ };
43
+ return { steps: [lead.length > 1 ? {
44
+ names: lead,
45
+ order: "parallel"
46
+ } : { names: lead }, tail.length > 1 ? {
47
+ names: tail,
48
+ order: "sequence"
49
+ } : { names: tail }] };
50
+ }
51
+ return {
52
+ questions,
53
+ pick
54
+ };
55
+ }
56
+ //#endregion
57
+ export { subagentRoute };
58
+
59
+ //# sourceMappingURL=route.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"route.js","names":[],"sources":["../../../../../src/activities/chat/agents/route.ts"],"sourcesContent":["import { boolean, choice } from '../../evaluate/index'\nimport type { DefinedAgent } from './define-agent'\nimport type { SubagentOrder, SubagentRouterPick } from './spawn'\n\nconst ORDER_KEY = 'order'\n\nexport interface SubagentRouteOptions<\n TAgents extends ReadonlyArray<DefinedAgent>,\n> {\n /**\n * Question text for every agent. The key is the agent name.\n * Omit this and each question uses that agent's description.\n */\n when?: { [K in TAgents[number]['name']]: string }\n /**\n * Agents that run after the other selected agents. The lead group starts\n * together. The `then` agents then run one after another in list order, and\n * each reads the text so far. Used only when the router picks at least one\n * agent from each group. Otherwise `pick` returns `{ names, order }`.\n */\n then?: ReadonlyArray<TAgents[number]['name']>\n}\n\ntype RouteQuestions<TAgents extends ReadonlyArray<DefinedAgent>> = {\n [K in TAgents[number]['name']]: ReturnType<typeof boolean>\n} & {\n order: ReturnType<\n typeof choice<{\n parallel: string\n sequence: string\n }>\n >\n}\n\ntype RouteAnswers<TAgents extends ReadonlyArray<DefinedAgent>> = {\n [K in TAgents[number]['name']]: { value: boolean }\n} & {\n order: { value: SubagentOrder }\n}\n\n/**\n * Build `decide()` questions for a subagent router.\n *\n * One yes/no question per agent, plus an `order` choice.\n * `pick` returns `main`, one name, `{ names, order }`, or `{ steps }`.\n * Names follow the `agents` array order.\n * `{ names, order }` overrides `subagents.order` for that turn.\n * `then`: agents that run after the other selected agents. The lead group\n * starts together. The `then` agents then run one after another in list\n * order, and each reads the text so far. Used only when the router picks at\n * least one agent from each group. Otherwise `pick` returns `{ names, order }`.\n */\nexport function subagentRoute<\n const TAgents extends ReadonlyArray<DefinedAgent>,\n>(agents: TAgents, options?: SubagentRouteOptions<TAgents>) {\n const namesInList = new Set(agents.map((agent) => agent.name))\n for (const agent of agents) {\n if (agent.name === ORDER_KEY) {\n throw new Error(\n 'subagentRoute cannot use an agent named \"order\". Rename that agent.',\n )\n }\n }\n for (const name of options?.then ?? []) {\n if (!namesInList.has(name)) {\n throw new Error(`subagentRoute then includes unknown agent \"${name}\".`)\n }\n }\n\n const questions: Record<string, unknown> = {\n order: choice({\n instructions: 'When more than one agent runs, how must they run?',\n options: {\n parallel:\n 'Start them together. Use this when no agent must read text from another agent. Working on the same topic is not a reason to wait.',\n sequence:\n 'Run them in agent-list order. Use this only when a later agent must read the earlier agent text, such as research notes and then a draft article.',\n },\n }),\n }\n\n for (const agent of agents) {\n const when = options?.when?.[agent.name as TAgents[number]['name']]\n questions[agent.name] = boolean({\n instructions: when ?? agent.description,\n })\n }\n\n function pick(result: RouteAnswers<TAgents>): SubagentRouterPick {\n const names = agents\n .map((agent) => agent.name)\n .filter((name) => result[name as TAgents[number]['name']].value)\n if (names.length === 0) return 'main'\n const only = names.length === 1 ? names[0] : undefined\n if (only !== undefined) return only\n const later = new Set(options?.then ?? [])\n const lead = names.filter((name) => !later.has(name))\n const tail = names.filter((name) => later.has(name))\n if (lead.length === 0 || tail.length === 0) {\n return { names, order: result.order.value }\n }\n return {\n steps: [\n lead.length > 1\n ? { names: lead, order: 'parallel' as const }\n : { names: lead },\n tail.length > 1\n ? { names: tail, order: 'sequence' as const }\n : { names: tail },\n ],\n }\n }\n\n return { questions: questions as RouteQuestions<TAgents>, pick }\n}\n"],"mappings":";;AAIA,IAAM,YAAY;;;;;;;;;;;;;AAgDlB,SAAgB,cAEd,QAAiB,SAAyC;CAC1D,MAAM,cAAc,IAAI,IAAI,OAAO,KAAK,UAAU,MAAM,IAAI,CAAC;CAC7D,KAAK,MAAM,SAAS,QAClB,IAAI,MAAM,SAAS,WACjB,MAAM,IAAI,MACR,uEACF;CAGJ,KAAK,MAAM,QAAQ,SAAS,QAAQ,CAAC,GACnC,IAAI,CAAC,YAAY,IAAI,IAAI,GACvB,MAAM,IAAI,MAAM,8CAA8C,KAAK,GAAG;CAI1E,MAAM,YAAqC,EACzC,OAAO,OAAO;EACZ,cAAc;EACd,SAAS;GACP,UACE;GACF,UACE;EACJ;CACF,CAAC,EACH;CAEA,KAAK,MAAM,SAAS,QAAQ;EAC1B,MAAM,OAAO,SAAS,OAAO,MAAM;EACnC,UAAU,MAAM,QAAQ,QAAQ,EAC9B,cAAc,QAAQ,MAAM,YAC9B,CAAC;CACH;CAEA,SAAS,KAAK,QAAmD;EAC/D,MAAM,QAAQ,OACX,KAAK,UAAU,MAAM,IAAI,CAAC,CAC1B,QAAQ,SAAS,OAAO,KAAgC,CAAC,KAAK;EACjE,IAAI,MAAM,WAAW,GAAG,OAAO;EAC/B,MAAM,OAAO,MAAM,WAAW,IAAI,MAAM,KAAK,KAAA;EAC7C,IAAI,SAAS,KAAA,GAAW,OAAO;EAC/B,MAAM,QAAQ,IAAI,IAAI,SAAS,QAAQ,CAAC,CAAC;EACzC,MAAM,OAAO,MAAM,QAAQ,SAAS,CAAC,MAAM,IAAI,IAAI,CAAC;EACpD,MAAM,OAAO,MAAM,QAAQ,SAAS,MAAM,IAAI,IAAI,CAAC;EACnD,IAAI,KAAK,WAAW,KAAK,KAAK,WAAW,GACvC,OAAO;GAAE;GAAO,OAAO,OAAO,MAAM;EAAM;EAE5C,OAAO,EACL,OAAO,CACL,KAAK,SAAS,IACV;GAAE,OAAO;GAAM,OAAO;EAAoB,IAC1C,EAAE,OAAO,KAAK,GAClB,KAAK,SAAS,IACV;GAAE,OAAO;GAAM,OAAO;EAAoB,IAC1C,EAAE,OAAO,KAAK,CACpB,EACF;CACF;CAEA,OAAO;EAAa;EAAsC;CAAK;AACjE"}
@@ -0,0 +1,124 @@
1
+ import { EventType, Interrupt, ModelMessage, RunAgentResumeItem, StreamChunk, TokenUsage, Tool, UIMessage } from '../../../types.js';
2
+ import { SpecTokenUsage } from '../../../utilities/ag-ui-usage.js';
3
+ import { DefinedAgent, SubagentRunContext } from './define-agent.js';
4
+ import { ChatMiddleware } from '../middleware/types.js';
5
+ import { SubagentTurn } from './turn.js';
6
+ export declare const SUBAGENT_STARTED = EventType.SUBAGENT_STARTED;
7
+ export declare const SUBAGENT_FINISHED = EventType.SUBAGENT_FINISHED;
8
+ export declare const SUBAGENT_ERROR = EventType.SUBAGENT_ERROR;
9
+ export type SubagentOrder = 'parallel' | 'sequence';
10
+ export interface SubagentRouterPlan {
11
+ names: ReadonlyArray<string>;
12
+ /** Overrides `subagents.order` for this turn. */
13
+ order?: SubagentOrder;
14
+ }
15
+ export interface SubagentStep {
16
+ names: ReadonlyArray<string>;
17
+ /** Overrides `subagents.order` for this step. */
18
+ order?: SubagentOrder;
19
+ }
20
+ export interface SubagentStepsPlan {
21
+ steps: ReadonlyArray<SubagentStep>;
22
+ }
23
+ export type SubagentRouterPick = 'main' | string | ReadonlyArray<string> | SubagentRouterPlan | SubagentStepsPlan;
24
+ export interface SubagentsBag<TAgents extends ReadonlyArray<DefinedAgent> = ReadonlyArray<DefinedAgent>> {
25
+ agents: TAgents;
26
+ router?: (ctx: {
27
+ messages: SubagentRunContext['messages'];
28
+ agents: NoInfer<TAgents>;
29
+ abortSignal?: AbortSignal;
30
+ }) => SubagentRouterPick | Promise<SubagentRouterPick>;
31
+ strategy?: 'exclusive' | 'handoff';
32
+ /**
33
+ * How a router list runs. `parallel` starts every name together.
34
+ * `sequence` runs each name after the previous one finishes, and passes
35
+ * that child's text to the next child.
36
+ */
37
+ order?: 'parallel' | 'sequence';
38
+ sandbox?: 'own' | 'inherit';
39
+ }
40
+ /** What the children of one parent run left behind for the parent terminal. */
41
+ export interface SubagentSink {
42
+ interrupts: Array<Interrupt>;
43
+ /** One AG-UI entry per child model call. */
44
+ usage: Array<SpecTokenUsage>;
45
+ /** Summed full usage of the children, including cost. */
46
+ total?: TokenUsage;
47
+ }
48
+ export declare function createSubagentSink(): SubagentSink;
49
+ /** One child to start, or a suspended child to continue. */
50
+ export interface SpawnEntry {
51
+ name: string;
52
+ resume?: {
53
+ subagentRunId: string;
54
+ /** The child's own messages from the interrupted run. */
55
+ messages: Array<UIMessage | ModelMessage>;
56
+ entries: Array<RunAgentResumeItem>;
57
+ /** Text the child wrote before it stopped. */
58
+ text: string;
59
+ };
60
+ }
61
+ interface SpawnContext {
62
+ messages: SubagentRunContext['messages'];
63
+ abortSignal?: AbortSignal;
64
+ threadId: string;
65
+ /** The parent chat run. */
66
+ parentRunId: string;
67
+ /** The interrupted parent run, on a resume. */
68
+ interruptedRunId?: string;
69
+ }
70
+ export declare function createSubagentId(): string;
71
+ /**
72
+ * Bind child interrupts to the parent run. The client resumes the parent run,
73
+ * so each binding must name that run. The resumed child then validates with
74
+ * the parent's interrupted run id.
75
+ */
76
+ export declare function rebindInterrupts(interrupts: ReadonlyArray<Interrupt>, runId: string): Array<Interrupt>;
77
+ export declare function normalizeRouterPick(pick: SubagentRouterPick, agents: ReadonlyArray<DefinedAgent>): {
78
+ steps: ReadonlyArray<SubagentStep>;
79
+ };
80
+ /** Add a finished child run's usage to the sink. */
81
+ export declare function collectUsage(sink: SubagentSink, finished?: StreamChunk): void;
82
+ /** A parent run's last chunk: it completed, or it failed. */
83
+ type ParentTerminal = Extract<StreamChunk, {
84
+ type: 'RUN_FINISHED' | 'RUN_ERROR';
85
+ }>;
86
+ /**
87
+ * Put the children's usage on a parent terminal. `usage[]` keeps one entry per
88
+ * model call. `metadata.tanstack.usage` holds the summed cost and the other
89
+ * TanStack fields, so `fromSpecTokenUsage` reads the full total. Empties the
90
+ * sink, so the next parent terminal does not count it again.
91
+ *
92
+ * `RUN_ERROR` is accepted too: a turn that failed still spent whatever its
93
+ * children spent. Such a chunk carries no usage of its own, so `runUsage` and
94
+ * `fullUsage` return empty for it and the children's total stands alone.
95
+ */
96
+ export declare function withChildUsage(chunk: ParentTerminal, sink: SubagentSink): ParentTerminal;
97
+ export declare function spawnAgentStream(agent: DefinedAgent, ctx: SubagentRunContext, sink?: SubagentSink, parentToolCallId?: string): AsyncIterable<StreamChunk>;
98
+ export declare function spawnNamedAgents(entries: ReadonlyArray<SpawnEntry>, bag: SubagentsBag, ctx: SpawnContext, sink?: SubagentSink): AsyncGenerator<import('../../..').AGUIEvent, void, any>;
99
+ /**
100
+ * Text of the named direct children, in `names` order. Text from nested
101
+ * children stays out: their chunks carry their own id.
102
+ */
103
+ export declare function collectNamedText(chunks: Array<StreamChunk>, names: ReadonlyArray<string>): string;
104
+ /**
105
+ * Record the parent messages when the model calls a subagent tool, so the
106
+ * child reads the conversation as it is at that call.
107
+ */
108
+ export declare function subagentCallMessages(names: ReadonlySet<string>): {
109
+ middleware: ChatMiddleware<unknown, never>;
110
+ messagesFor: (toolCallId: string | undefined) => ModelMessage<string | import('../../..').ContentPart<unknown, unknown, unknown, unknown, unknown>[] | null>[] | undefined;
111
+ };
112
+ export declare function createSyntheticSubagentTools(bag: SubagentsBag, parent: {
113
+ /** Messages the parent run started with. Used when no call was recorded. */
114
+ messages: SubagentRunContext['messages'];
115
+ /** The parent messages at a tool call. See subagentCallMessages. */
116
+ messagesFor?: (toolCallId: string | undefined) => SubagentRunContext['messages'] | undefined;
117
+ threadId: string;
118
+ runId: string;
119
+ interruptedRunId?: string;
120
+ abortSignal?: AbortSignal;
121
+ turn?: SubagentTurn;
122
+ sink: SubagentSink;
123
+ }): Array<Tool>;
124
+ export {};