@tanstack/ai 0.41.0 → 0.43.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.
- package/README.md +15 -1
- package/dist/esm/activities/chat/adapter.js +23 -16
- package/dist/esm/activities/chat/adapter.js.map +1 -1
- package/dist/esm/activities/chat/agent-loop-strategies.d.ts +10 -4
- package/dist/esm/activities/chat/agent-loop-strategies.js +75 -17
- package/dist/esm/activities/chat/agent-loop-strategies.js.map +1 -1
- package/dist/esm/activities/chat/cancel.d.ts +40 -0
- package/dist/esm/activities/chat/cancel.js +54 -0
- package/dist/esm/activities/chat/cancel.js.map +1 -0
- package/dist/esm/activities/chat/index.d.ts +28 -16
- package/dist/esm/activities/chat/index.js +2100 -1744
- package/dist/esm/activities/chat/index.js.map +1 -1
- package/dist/esm/activities/chat/mcp/manager.d.ts +2 -2
- package/dist/esm/activities/chat/mcp/manager.js +90 -77
- package/dist/esm/activities/chat/mcp/manager.js.map +1 -1
- package/dist/esm/activities/chat/mcp/types.d.ts +2 -2
- package/dist/esm/activities/chat/messages.js +397 -346
- package/dist/esm/activities/chat/messages.js.map +1 -1
- package/dist/esm/activities/chat/middleware/builder.js +17 -15
- package/dist/esm/activities/chat/middleware/builder.js.map +1 -1
- package/dist/esm/activities/chat/middleware/capabilities.js +78 -43
- package/dist/esm/activities/chat/middleware/capabilities.js.map +1 -1
- package/dist/esm/activities/chat/middleware/compose.d.ts +94 -1
- package/dist/esm/activities/chat/middleware/compose.js +623 -531
- package/dist/esm/activities/chat/middleware/compose.js.map +1 -1
- package/dist/esm/activities/chat/middleware/define.js +12 -5
- package/dist/esm/activities/chat/middleware/define.js.map +1 -1
- package/dist/esm/activities/chat/middleware/index.d.ts +5 -1
- package/dist/esm/activities/chat/middleware/locks.d.ts +50 -0
- package/dist/esm/activities/chat/middleware/locks.js +71 -0
- package/dist/esm/activities/chat/middleware/locks.js.map +1 -0
- package/dist/esm/activities/chat/middleware/pending-turn.d.ts +15 -0
- package/dist/esm/activities/chat/middleware/pending-turn.js +35 -0
- package/dist/esm/activities/chat/middleware/pending-turn.js.map +1 -0
- package/dist/esm/activities/chat/middleware/run-disconnect.d.ts +23 -0
- package/dist/esm/activities/chat/middleware/run-disconnect.js +42 -0
- package/dist/esm/activities/chat/middleware/run-disconnect.js.map +1 -0
- package/dist/esm/activities/chat/middleware/run-store.d.ts +283 -0
- package/dist/esm/activities/chat/middleware/run-store.js +176 -0
- package/dist/esm/activities/chat/middleware/run-store.js.map +1 -0
- package/dist/esm/activities/chat/middleware/sandbox-runtime.js +14 -8
- package/dist/esm/activities/chat/middleware/sandbox-runtime.js.map +1 -1
- package/dist/esm/activities/chat/middleware/tool-cache-middleware.js +79 -70
- package/dist/esm/activities/chat/middleware/tool-cache-middleware.js.map +1 -1
- package/dist/esm/activities/chat/middleware/types.d.ts +59 -2
- package/dist/esm/activities/chat/middleware/validate.js +23 -28
- package/dist/esm/activities/chat/middleware/validate.js.map +1 -1
- package/dist/esm/activities/chat/stream/json-parser.js +39 -25
- package/dist/esm/activities/chat/stream/json-parser.js.map +1 -1
- package/dist/esm/activities/chat/stream/message-updaters.js +275 -234
- package/dist/esm/activities/chat/stream/message-updaters.js.map +1 -1
- package/dist/esm/activities/chat/stream/processor.d.ts +24 -4
- package/dist/esm/activities/chat/stream/processor.js +1341 -1542
- package/dist/esm/activities/chat/stream/processor.js.map +1 -1
- package/dist/esm/activities/chat/stream/strategies.js +69 -53
- package/dist/esm/activities/chat/stream/strategies.js.map +1 -1
- package/dist/esm/activities/chat/tools/approval-schema.d.ts +19 -0
- package/dist/esm/activities/chat/tools/approval-schema.js +117 -0
- package/dist/esm/activities/chat/tools/approval-schema.js.map +1 -0
- package/dist/esm/activities/chat/tools/lazy-tool-manager.js +164 -191
- package/dist/esm/activities/chat/tools/lazy-tool-manager.js.map +1 -1
- package/dist/esm/activities/chat/tools/lazy-tools.js +24 -12
- package/dist/esm/activities/chat/tools/lazy-tools.js.map +1 -1
- package/dist/esm/activities/chat/tools/schema-converter.js +293 -146
- package/dist/esm/activities/chat/tools/schema-converter.js.map +1 -1
- package/dist/esm/activities/chat/tools/tool-calls.d.ts +18 -2
- package/dist/esm/activities/chat/tools/tool-calls.js +522 -531
- package/dist/esm/activities/chat/tools/tool-calls.js.map +1 -1
- package/dist/esm/activities/chat/tools/tool-definition.d.ts +75 -16
- package/dist/esm/activities/chat/tools/tool-definition.js +95 -23
- package/dist/esm/activities/chat/tools/tool-definition.js.map +1 -1
- package/dist/esm/activities/error-payload.js +85 -47
- package/dist/esm/activities/error-payload.js.map +1 -1
- package/dist/esm/activities/generateAudio/adapter.js +22 -15
- package/dist/esm/activities/generateAudio/adapter.js.map +1 -1
- package/dist/esm/activities/generateAudio/index.d.ts +4 -0
- package/dist/esm/activities/generateAudio/index.js +141 -105
- package/dist/esm/activities/generateAudio/index.js.map +1 -1
- package/dist/esm/activities/generateImage/adapter.js +22 -15
- package/dist/esm/activities/generateImage/adapter.js.map +1 -1
- package/dist/esm/activities/generateImage/index.d.ts +4 -0
- package/dist/esm/activities/generateImage/index.js +155 -111
- package/dist/esm/activities/generateImage/index.js.map +1 -1
- package/dist/esm/activities/generateSpeech/adapter.js +22 -15
- package/dist/esm/activities/generateSpeech/adapter.js.map +1 -1
- package/dist/esm/activities/generateSpeech/index.d.ts +4 -0
- package/dist/esm/activities/generateSpeech/index.js +159 -110
- package/dist/esm/activities/generateSpeech/index.js.map +1 -1
- package/dist/esm/activities/generateTranscription/adapter.js +22 -15
- package/dist/esm/activities/generateTranscription/adapter.js.map +1 -1
- package/dist/esm/activities/generateTranscription/index.d.ts +4 -0
- package/dist/esm/activities/generateTranscription/index.js +159 -100
- package/dist/esm/activities/generateTranscription/index.js.map +1 -1
- package/dist/esm/activities/generateVideo/adapter.js +36 -29
- package/dist/esm/activities/generateVideo/adapter.js.map +1 -1
- package/dist/esm/activities/generateVideo/index.d.ts +143 -19
- package/dist/esm/activities/generateVideo/index.js +456 -279
- package/dist/esm/activities/generateVideo/index.js.map +1 -1
- package/dist/esm/activities/generateVideo/snap.js +60 -48
- package/dist/esm/activities/generateVideo/snap.js.map +1 -1
- package/dist/esm/activities/index.js +8 -34
- package/dist/esm/activities/middleware/index.d.ts +1 -1
- package/dist/esm/activities/middleware/run.d.ts +10 -0
- package/dist/esm/activities/middleware/run.js +53 -29
- package/dist/esm/activities/middleware/run.js.map +1 -1
- package/dist/esm/activities/middleware/types.d.ts +44 -6
- package/dist/esm/activities/stream-generation-result.d.ts +4 -1
- package/dist/esm/activities/stream-generation-result.js +79 -44
- package/dist/esm/activities/stream-generation-result.js.map +1 -1
- package/dist/esm/activities/summarize/adapter.js +22 -15
- package/dist/esm/activities/summarize/adapter.js.map +1 -1
- package/dist/esm/activities/summarize/chat-stream-summarize.js +252 -202
- package/dist/esm/activities/summarize/chat-stream-summarize.js.map +1 -1
- package/dist/esm/activities/summarize/index.d.ts +27 -0
- package/dist/esm/activities/summarize/index.js +268 -102
- package/dist/esm/activities/summarize/index.js.map +1 -1
- package/dist/esm/adapter-internals.d.ts +2 -1
- package/dist/esm/adapter-internals.js +4 -11
- package/dist/esm/client.d.ts +25 -3
- package/dist/esm/client.js +131 -64
- package/dist/esm/client.js.map +1 -1
- package/dist/esm/custom-events.d.ts +76 -0
- package/dist/esm/custom-events.js +37 -0
- package/dist/esm/custom-events.js.map +1 -0
- package/dist/esm/delivery-detach.d.ts +50 -0
- package/dist/esm/delivery-detach.js +71 -0
- package/dist/esm/delivery-detach.js.map +1 -0
- package/dist/esm/delivery-disconnect.d.ts +62 -0
- package/dist/esm/delivery-disconnect.js +81 -0
- package/dist/esm/delivery-disconnect.js.map +1 -0
- package/dist/esm/extend-adapter.js +19 -17
- package/dist/esm/extend-adapter.js.map +1 -1
- package/dist/esm/index.d.ts +23 -5
- package/dist/esm/index.js +30 -97
- package/dist/esm/interrupt-resume.d.ts +71 -0
- package/dist/esm/interrupt-resume.js +438 -0
- package/dist/esm/interrupt-resume.js.map +1 -0
- package/dist/esm/interrupt-serialization.d.ts +12 -0
- package/dist/esm/interrupt-serialization.js +178 -0
- package/dist/esm/interrupt-serialization.js.map +1 -0
- package/dist/esm/interrupts.d.ts +84 -0
- package/dist/esm/interrupts.js +31 -0
- package/dist/esm/interrupts.js.map +1 -0
- package/dist/esm/locks.d.ts +10 -0
- package/dist/esm/locks.js +2 -0
- package/dist/esm/logger/console-logger.js +101 -78
- package/dist/esm/logger/console-logger.js.map +1 -1
- package/dist/esm/logger/internal-logger.js +104 -89
- package/dist/esm/logger/internal-logger.js.map +1 -1
- package/dist/esm/logger/resolve.js +54 -49
- package/dist/esm/logger/resolve.js.map +1 -1
- package/dist/esm/logger/types.d.ts +1 -1
- package/dist/esm/middlewares/content-guard.js +142 -148
- package/dist/esm/middlewares/content-guard.js.map +1 -1
- package/dist/esm/middlewares/index.js +2 -6
- package/dist/esm/middlewares/otel.js +598 -732
- package/dist/esm/middlewares/otel.js.map +1 -1
- package/dist/esm/middlewares/usage-attributes.js +47 -40
- package/dist/esm/middlewares/usage-attributes.js.map +1 -1
- package/dist/esm/realtime/event-emitter.js +24 -25
- package/dist/esm/realtime/event-emitter.js.map +1 -1
- package/dist/esm/realtime/index.d.ts +5 -9
- package/dist/esm/realtime/index.js +29 -6
- package/dist/esm/realtime/index.js.map +1 -1
- package/dist/esm/scope.d.ts +47 -0
- package/dist/esm/stream-durability.d.ts +171 -0
- package/dist/esm/stream-durability.js +295 -0
- package/dist/esm/stream-durability.js.map +1 -0
- package/dist/esm/stream-to-response.d.ts +178 -13
- package/dist/esm/stream-to-response.js +663 -115
- package/dist/esm/stream-to-response.js.map +1 -1
- package/dist/esm/strip-to-spec-middleware.js +30 -16
- package/dist/esm/strip-to-spec-middleware.js.map +1 -1
- package/dist/esm/system-prompts.js +27 -21
- package/dist/esm/system-prompts.js.map +1 -1
- package/dist/esm/tool-registry.js +72 -45
- package/dist/esm/tool-registry.js.map +1 -1
- package/dist/esm/tools/provider-tool.js +14 -5
- package/dist/esm/tools/provider-tool.js.map +1 -1
- package/dist/esm/types.d.ts +332 -21
- package/dist/esm/types.js +2 -0
- package/dist/esm/utilities/ag-ui-wire.js +79 -93
- package/dist/esm/utilities/ag-ui-wire.js.map +1 -1
- package/dist/esm/utilities/chat-params.d.ts +26 -4
- package/dist/esm/utilities/chat-params.js +218 -92
- package/dist/esm/utilities/chat-params.js.map +1 -1
- package/dist/esm/utilities/errors.js +28 -18
- package/dist/esm/utilities/errors.js.map +1 -1
- package/dist/esm/utilities/media-prompt.js +46 -41
- package/dist/esm/utilities/media-prompt.js.map +1 -1
- package/dist/esm/utilities/numbers.js +13 -10
- package/dist/esm/utilities/numbers.js.map +1 -1
- package/dist/esm/utilities/provider-executed.js +20 -11
- package/dist/esm/utilities/provider-executed.js.map +1 -1
- package/dist/esm/utilities/sampling-keys.js +31 -19
- package/dist/esm/utilities/sampling-keys.js.map +1 -1
- package/dist/esm/utilities/tool-result.js +42 -30
- package/dist/esm/utilities/tool-result.js.map +1 -1
- package/dist/esm/utilities/usage.js +27 -9
- package/dist/esm/utilities/usage.js.map +1 -1
- package/dist/esm/utils.js +26 -18
- package/dist/esm/utils.js.map +1 -1
- package/package.json +10 -6
- package/skills/ai-core/SKILL.md +69 -18
- package/skills/ai-core/adapter-configuration/SKILL.md +44 -21
- package/skills/ai-core/adapter-configuration/references/anthropic-adapter.md +1 -3
- package/skills/ai-core/adapter-configuration/references/byteplus-adapter.md +148 -0
- package/skills/ai-core/adapter-configuration/references/gemini-adapter.md +2 -6
- package/skills/ai-core/adapter-configuration/references/groq-adapter.md +2 -6
- package/skills/ai-core/adapter-configuration/references/openai-adapter.md +1 -3
- package/skills/ai-core/ag-ui-protocol/SKILL.md +1 -1
- package/skills/ai-core/chat-experience/SKILL.md +156 -11
- package/skills/ai-core/client-persistence/SKILL.md +277 -0
- package/skills/ai-core/custom-backend-integration/SKILL.md +1 -1
- package/skills/ai-core/debug-logging/SKILL.md +1 -1
- package/skills/ai-core/locks/SKILL.md +143 -0
- package/skills/ai-core/media-generation/SKILL.md +144 -12
- package/skills/ai-core/middleware/SKILL.md +258 -33
- package/skills/ai-core/structured-outputs/SKILL.md +1 -1
- package/skills/ai-core/tool-calling/SKILL.md +54 -59
- package/src/activities/chat/agent-loop-strategies.ts +10 -4
- package/src/activities/chat/cancel.ts +81 -0
- package/src/activities/chat/index.ts +1152 -153
- package/src/activities/chat/mcp/manager.ts +4 -4
- package/src/activities/chat/mcp/types.ts +2 -2
- package/src/activities/chat/messages.ts +5 -3
- package/src/activities/chat/middleware/builder.ts +1 -1
- package/src/activities/chat/middleware/compose.ts +186 -9
- package/src/activities/chat/middleware/index.ts +26 -0
- package/src/activities/chat/middleware/locks.ts +102 -0
- package/src/activities/chat/middleware/pending-turn.ts +47 -0
- package/src/activities/chat/middleware/run-disconnect.ts +62 -0
- package/src/activities/chat/middleware/run-store.ts +412 -0
- package/src/activities/chat/middleware/types.ts +62 -1
- package/src/activities/chat/stream/processor.ts +189 -5
- package/src/activities/chat/tools/approval-schema.ts +205 -0
- package/src/activities/chat/tools/tool-calls.ts +106 -13
- package/src/activities/chat/tools/tool-definition.ts +210 -39
- package/src/activities/generateAudio/index.ts +20 -3
- package/src/activities/generateImage/index.ts +20 -3
- package/src/activities/generateSpeech/index.ts +25 -3
- package/src/activities/generateTranscription/index.ts +26 -3
- package/src/activities/generateVideo/index.ts +345 -82
- package/src/activities/middleware/index.ts +2 -0
- package/src/activities/middleware/run.ts +31 -0
- package/src/activities/middleware/types.ts +49 -5
- package/src/activities/stream-generation-result.ts +30 -2
- package/src/activities/summarize/chat-stream-summarize.ts +5 -0
- package/src/activities/summarize/index.ts +200 -10
- package/src/adapter-internals.ts +10 -1
- package/src/client.ts +244 -0
- package/src/custom-events.ts +107 -0
- package/src/delivery-detach.ts +72 -0
- package/src/delivery-disconnect.ts +84 -0
- package/src/index.ts +138 -0
- package/src/interrupt-resume.ts +824 -0
- package/src/interrupt-serialization.ts +183 -0
- package/src/interrupts.ts +146 -0
- package/src/locks.ts +17 -0
- package/src/logger/types.ts +1 -1
- package/src/middlewares/otel.ts +1 -0
- package/src/realtime/index.ts +5 -9
- package/src/scope.ts +47 -0
- package/src/stream-durability.ts +598 -0
- package/src/stream-to-response.ts +1051 -95
- package/src/strip-to-spec-middleware.ts +3 -3
- package/src/types.ts +416 -24
- package/src/utilities/chat-params.ts +245 -55
- package/dist/esm/activities/index.js.map +0 -1
- package/dist/esm/adapter-internals.js.map +0 -1
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/middlewares/index.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
<div align="center">
|
|
2
|
-
<
|
|
2
|
+
<picture>
|
|
3
|
+
<source
|
|
4
|
+
media="(prefers-color-scheme: dark)"
|
|
5
|
+
srcset="https://tanstack.com/api/readme/ai.png?theme=dark"
|
|
6
|
+
/>
|
|
7
|
+
<source
|
|
8
|
+
media="(prefers-color-scheme: light)"
|
|
9
|
+
srcset="https://tanstack.com/api/readme/ai.png"
|
|
10
|
+
/>
|
|
11
|
+
<img
|
|
12
|
+
src="https://tanstack.com/api/readme/ai.png"
|
|
13
|
+
alt="TanStack AI"
|
|
14
|
+
width="900"
|
|
15
|
+
/>
|
|
16
|
+
</picture>
|
|
3
17
|
</div>
|
|
4
18
|
|
|
5
19
|
<br />
|
|
@@ -1,17 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
//#region src/activities/chat/adapter.ts
|
|
2
|
+
/**
|
|
3
|
+
* Abstract base class for text adapters.
|
|
4
|
+
* Extend this class to implement a text adapter for a specific provider.
|
|
5
|
+
*
|
|
6
|
+
* Generic parameters match TextAdapter - all pre-resolved by the provider function.
|
|
7
|
+
*/
|
|
8
|
+
var BaseTextAdapter = class {
|
|
9
|
+
kind = "text";
|
|
10
|
+
model;
|
|
11
|
+
requires = void 0;
|
|
12
|
+
config;
|
|
13
|
+
constructor(config = {}, model) {
|
|
14
|
+
this.config = config;
|
|
15
|
+
this.model = model;
|
|
16
|
+
}
|
|
17
|
+
generateId() {
|
|
18
|
+
return `${this.name}-${Date.now()}-${Math.random().toString(36).substring(7)}`;
|
|
19
|
+
}
|
|
16
20
|
};
|
|
17
|
-
//#
|
|
21
|
+
//#endregion
|
|
22
|
+
export { BaseTextAdapter };
|
|
23
|
+
|
|
24
|
+
//# sourceMappingURL=adapter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"adapter.js","sources":["../../../../src/activities/chat/adapter.ts"],"sourcesContent":["import type {\n DefaultMessageMetadataByModality,\n JSONSchema,\n Modality,\n StreamChunk,\n TextOptions,\n TokenUsage,\n} from '../../types'\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<StreamChunk>\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? }`.\n */\n structuredOutputStream?: (\n options: StructuredOutputOptions<TProviderOptions>,\n ) => AsyncIterable<StreamChunk>\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/**\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<StreamChunk>\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"],"
|
|
1
|
+
{"version":3,"file":"adapter.js","names":[],"sources":["../../../../src/activities/chat/adapter.ts"],"sourcesContent":["import type {\n DefaultMessageMetadataByModality,\n JSONSchema,\n Modality,\n StreamChunk,\n TextOptions,\n TokenUsage,\n} from '../../types'\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<StreamChunk>\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? }`.\n */\n structuredOutputStream?: (\n options: StructuredOutputOptions<TProviderOptions>,\n ) => AsyncIterable<StreamChunk>\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/**\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<StreamChunk>\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":";;;;;;;AA+KA,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,8 +1,14 @@
|
|
|
1
1
|
import { AgentLoopStrategy } from '../../types.js';
|
|
2
2
|
/**
|
|
3
|
-
* Creates a strategy that continues for a maximum number of
|
|
3
|
+
* Creates a strategy that continues for a maximum number of **model turns**
|
|
4
|
+
* (iterations), not tool calls.
|
|
4
5
|
*
|
|
5
|
-
*
|
|
6
|
+
* One iteration can still emit many parallel tool calls. For a tool-call
|
|
7
|
+
* budget, use middleware with `onBeforeToolCall` (per-turn cap) and
|
|
8
|
+
* `onShouldContinue` (cumulative run budget) — see the docs recipe under
|
|
9
|
+
* Agentic Cycle.
|
|
10
|
+
*
|
|
11
|
+
* @param max - Maximum number of model turns to allow
|
|
6
12
|
* @returns AgentLoopStrategy that stops after max iterations
|
|
7
13
|
*
|
|
8
14
|
* @example
|
|
@@ -12,7 +18,7 @@ import { AgentLoopStrategy } from '../../types.js';
|
|
|
12
18
|
* model: "gpt-4o",
|
|
13
19
|
* messages: [...],
|
|
14
20
|
* tools: [weatherTool],
|
|
15
|
-
* agentLoopStrategy: maxIterations(3), // Max 3
|
|
21
|
+
* agentLoopStrategy: maxIterations(3), // Max 3 model turns
|
|
16
22
|
* });
|
|
17
23
|
* ```
|
|
18
24
|
*/
|
|
@@ -40,7 +46,7 @@ export declare function untilFinishReason(stopReasons: Array<string>): AgentLoop
|
|
|
40
46
|
* All strategies must return true to continue
|
|
41
47
|
*
|
|
42
48
|
* @param strategies - Array of strategies to combine
|
|
43
|
-
* @returns AgentLoopStrategy that continues only if all strategies
|
|
49
|
+
* @returns AgentLoopStrategy that continues only if all strategies agree
|
|
44
50
|
*
|
|
45
51
|
* @example
|
|
46
52
|
* ```typescript
|
|
@@ -1,23 +1,81 @@
|
|
|
1
|
+
//#region src/activities/chat/agent-loop-strategies.ts
|
|
2
|
+
/**
|
|
3
|
+
* Creates a strategy that continues for a maximum number of **model turns**
|
|
4
|
+
* (iterations), not tool calls.
|
|
5
|
+
*
|
|
6
|
+
* One iteration can still emit many parallel tool calls. For a tool-call
|
|
7
|
+
* budget, use middleware with `onBeforeToolCall` (per-turn cap) and
|
|
8
|
+
* `onShouldContinue` (cumulative run budget) — see the docs recipe under
|
|
9
|
+
* Agentic Cycle.
|
|
10
|
+
*
|
|
11
|
+
* @param max - Maximum number of model turns to allow
|
|
12
|
+
* @returns AgentLoopStrategy that stops after max iterations
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const stream = chat({
|
|
17
|
+
* adapter: openaiText(),
|
|
18
|
+
* model: "gpt-4o",
|
|
19
|
+
* messages: [...],
|
|
20
|
+
* tools: [weatherTool],
|
|
21
|
+
* agentLoopStrategy: maxIterations(3), // Max 3 model turns
|
|
22
|
+
* });
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
1
25
|
function maxIterations(max) {
|
|
2
|
-
|
|
26
|
+
return ({ iterationCount }) => iterationCount < max;
|
|
3
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Creates a strategy that continues until a specific finish reason is encountered
|
|
30
|
+
*
|
|
31
|
+
* @param stopReasons - Finish reasons that should stop the loop
|
|
32
|
+
* @returns AgentLoopStrategy that stops on specific finish reasons
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* const stream = chat({
|
|
37
|
+
* adapter: openaiText(),
|
|
38
|
+
* model: "gpt-4o",
|
|
39
|
+
* messages: [...],
|
|
40
|
+
* tools: [weatherTool],
|
|
41
|
+
* agentLoopStrategy: untilFinishReason(["stop", "length"]),
|
|
42
|
+
* });
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
4
45
|
function untilFinishReason(stopReasons) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
return true;
|
|
11
|
-
};
|
|
46
|
+
return ({ finishReason, iterationCount }) => {
|
|
47
|
+
if (iterationCount === 0) return true;
|
|
48
|
+
if (finishReason && stopReasons.includes(finishReason)) return false;
|
|
49
|
+
return true;
|
|
50
|
+
};
|
|
12
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* Creates a strategy that combines multiple strategies with AND logic
|
|
54
|
+
* All strategies must return true to continue
|
|
55
|
+
*
|
|
56
|
+
* @param strategies - Array of strategies to combine
|
|
57
|
+
* @returns AgentLoopStrategy that continues only if all strategies agree
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* const stream = chat({
|
|
62
|
+
* adapter: openaiText(),
|
|
63
|
+
* model: "gpt-4o",
|
|
64
|
+
* messages: [...],
|
|
65
|
+
* tools: [weatherTool],
|
|
66
|
+
* agentLoopStrategy: combineStrategies([
|
|
67
|
+
* maxIterations(10),
|
|
68
|
+
* ({ messages }) => messages.length < 100,
|
|
69
|
+
* ]),
|
|
70
|
+
* });
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
13
73
|
function combineStrategies(strategies) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
74
|
+
return (state) => {
|
|
75
|
+
return strategies.every((strategy) => strategy(state));
|
|
76
|
+
};
|
|
17
77
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
};
|
|
23
|
-
//# sourceMappingURL=agent-loop-strategies.js.map
|
|
78
|
+
//#endregion
|
|
79
|
+
export { combineStrategies, maxIterations, untilFinishReason };
|
|
80
|
+
|
|
81
|
+
//# sourceMappingURL=agent-loop-strategies.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent-loop-strategies.js","sources":["../../../../src/activities/chat/agent-loop-strategies.ts"],"sourcesContent":["import type { AgentLoopStrategy } from '../../types'\n\n/**\n * Creates a strategy that continues for a maximum number of iterations\n *\n * @param max - Maximum number of
|
|
1
|
+
{"version":3,"file":"agent-loop-strategies.js","names":[],"sources":["../../../../src/activities/chat/agent-loop-strategies.ts"],"sourcesContent":["import type { AgentLoopStrategy } from '../../types'\n\n/**\n * Creates a strategy that continues for a maximum number of **model turns**\n * (iterations), not tool calls.\n *\n * One iteration can still emit many parallel tool calls. For a tool-call\n * budget, use middleware with `onBeforeToolCall` (per-turn cap) and\n * `onShouldContinue` (cumulative run budget) — see the docs recipe under\n * Agentic Cycle.\n *\n * @param max - Maximum number of model turns to allow\n * @returns AgentLoopStrategy that stops after max iterations\n *\n * @example\n * ```typescript\n * const stream = chat({\n * adapter: openaiText(),\n * model: \"gpt-4o\",\n * messages: [...],\n * tools: [weatherTool],\n * agentLoopStrategy: maxIterations(3), // Max 3 model turns\n * });\n * ```\n */\nexport function maxIterations(max: number): AgentLoopStrategy {\n return ({ iterationCount }) => iterationCount < max\n}\n\n/**\n * Creates a strategy that continues until a specific finish reason is encountered\n *\n * @param stopReasons - Finish reasons that should stop the loop\n * @returns AgentLoopStrategy that stops on specific finish reasons\n *\n * @example\n * ```typescript\n * const stream = chat({\n * adapter: openaiText(),\n * model: \"gpt-4o\",\n * messages: [...],\n * tools: [weatherTool],\n * agentLoopStrategy: untilFinishReason([\"stop\", \"length\"]),\n * });\n * ```\n */\nexport function untilFinishReason(\n stopReasons: Array<string>,\n): AgentLoopStrategy {\n return ({ finishReason, iterationCount }) => {\n // Always allow at least one iteration\n if (iterationCount === 0) return true\n\n // Stop if we hit a stop reason\n if (finishReason && stopReasons.includes(finishReason)) {\n return false\n }\n\n // Otherwise continue\n return true\n }\n}\n\n/**\n * Creates a strategy that combines multiple strategies with AND logic\n * All strategies must return true to continue\n *\n * @param strategies - Array of strategies to combine\n * @returns AgentLoopStrategy that continues only if all strategies agree\n *\n * @example\n * ```typescript\n * const stream = chat({\n * adapter: openaiText(),\n * model: \"gpt-4o\",\n * messages: [...],\n * tools: [weatherTool],\n * agentLoopStrategy: combineStrategies([\n * maxIterations(10),\n * ({ messages }) => messages.length < 100,\n * ]),\n * });\n * ```\n */\nexport function combineStrategies(\n strategies: Array<AgentLoopStrategy>,\n): AgentLoopStrategy {\n return (state) => {\n return strategies.every((strategy) => strategy(state))\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAyBA,SAAgB,cAAc,KAAgC;CAC5D,QAAQ,EAAE,qBAAqB,iBAAiB;AAClD;;;;;;;;;;;;;;;;;;AAmBA,SAAgB,kBACd,aACmB;CACnB,QAAQ,EAAE,cAAc,qBAAqB;EAE3C,IAAI,mBAAmB,GAAG,OAAO;EAGjC,IAAI,gBAAgB,YAAY,SAAS,YAAY,GACnD,OAAO;EAIT,OAAO;CACT;AACF;;;;;;;;;;;;;;;;;;;;;;AAuBA,SAAgB,kBACd,YACmB;CACnB,QAAQ,UAAU;EAChB,OAAO,WAAW,OAAO,aAAa,SAAS,KAAK,CAAC;CACvD;AACF"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { RunStore } from './middleware/run-store.js';
|
|
2
|
+
/**
|
|
3
|
+
* Abort reason that marks an abort as an explicit cancellation.
|
|
4
|
+
*
|
|
5
|
+
* Namespaced so an application's own reason string cannot collide with it by
|
|
6
|
+
* accident, and matched with `===` (never a substring test) so an arbitrary
|
|
7
|
+
* provider error message can never be read as a deliberate cancel.
|
|
8
|
+
*/
|
|
9
|
+
export declare const RUN_CANCEL_REASON = "tanstack-ai:cancel-requested";
|
|
10
|
+
/** Whether an abort reason means "the user explicitly cancelled this run". */
|
|
11
|
+
export declare function isCancelRequestedReason(reason: string | undefined): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Record an explicit cancel on the run record.
|
|
14
|
+
*
|
|
15
|
+
* Deliberately does NOT set a status. The driver is the only actor that knows
|
|
16
|
+
* when the agent has actually stopped and the sandbox has been torn down, so it
|
|
17
|
+
* owns the transition to `'aborted'`. Writing a terminal status here would tell
|
|
18
|
+
* every reader the run is over while the agent is still burning tokens.
|
|
19
|
+
*
|
|
20
|
+
* A no-op for an unknown `runId`, inheriting `RunStore.update`'s documented
|
|
21
|
+
* invariant.
|
|
22
|
+
*/
|
|
23
|
+
export declare function requestRunCancel(runs: RunStore, runId: string): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Whether an explicit cancel has been recorded for `runId`.
|
|
26
|
+
*
|
|
27
|
+
* Answers `false` rather than throwing when the store cannot be read. Callers
|
|
28
|
+
* are middleware abort hooks, which are already on a teardown path, and a
|
|
29
|
+
* store failure there must not replace the caller's own reason for tearing
|
|
30
|
+
* down with a store error. The cost of a false negative is that a cancel
|
|
31
|
+
* degrades into a detach — the run record gains `detachedSince`/`sandboxKey`
|
|
32
|
+
* instead of transitioning to `'aborted'`. `@tanstack/ai-sandbox`'s
|
|
33
|
+
* `reapDetachedRuns` recovers that run once the `detachedRunTtlMs` the
|
|
34
|
+
* application passes to that sweep has elapsed — nothing derives it from
|
|
35
|
+
* `withSandbox`, which has no TTL option — so the cost is a delayed teardown
|
|
36
|
+
* rather than a lost one, provided the application actually schedules the
|
|
37
|
+
* sweep, which is its job and not the framework's. Still strictly better than
|
|
38
|
+
* failing the teardown.
|
|
39
|
+
*/
|
|
40
|
+
export declare function wasCancelRequested(runs: RunStore, runId: string): Promise<boolean>;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
//#region src/activities/chat/cancel.ts
|
|
2
|
+
/**
|
|
3
|
+
* Abort reason that marks an abort as an explicit cancellation.
|
|
4
|
+
*
|
|
5
|
+
* Namespaced so an application's own reason string cannot collide with it by
|
|
6
|
+
* accident, and matched with `===` (never a substring test) so an arbitrary
|
|
7
|
+
* provider error message can never be read as a deliberate cancel.
|
|
8
|
+
*/
|
|
9
|
+
var RUN_CANCEL_REASON = "tanstack-ai:cancel-requested";
|
|
10
|
+
/** Whether an abort reason means "the user explicitly cancelled this run". */
|
|
11
|
+
function isCancelRequestedReason(reason) {
|
|
12
|
+
return reason === RUN_CANCEL_REASON;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Record an explicit cancel on the run record.
|
|
16
|
+
*
|
|
17
|
+
* Deliberately does NOT set a status. The driver is the only actor that knows
|
|
18
|
+
* when the agent has actually stopped and the sandbox has been torn down, so it
|
|
19
|
+
* owns the transition to `'aborted'`. Writing a terminal status here would tell
|
|
20
|
+
* every reader the run is over while the agent is still burning tokens.
|
|
21
|
+
*
|
|
22
|
+
* A no-op for an unknown `runId`, inheriting `RunStore.update`'s documented
|
|
23
|
+
* invariant.
|
|
24
|
+
*/
|
|
25
|
+
async function requestRunCancel(runs, runId) {
|
|
26
|
+
await runs.update(runId, { cancelRequested: true });
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Whether an explicit cancel has been recorded for `runId`.
|
|
30
|
+
*
|
|
31
|
+
* Answers `false` rather than throwing when the store cannot be read. Callers
|
|
32
|
+
* are middleware abort hooks, which are already on a teardown path, and a
|
|
33
|
+
* store failure there must not replace the caller's own reason for tearing
|
|
34
|
+
* down with a store error. The cost of a false negative is that a cancel
|
|
35
|
+
* degrades into a detach — the run record gains `detachedSince`/`sandboxKey`
|
|
36
|
+
* instead of transitioning to `'aborted'`. `@tanstack/ai-sandbox`'s
|
|
37
|
+
* `reapDetachedRuns` recovers that run once the `detachedRunTtlMs` the
|
|
38
|
+
* application passes to that sweep has elapsed — nothing derives it from
|
|
39
|
+
* `withSandbox`, which has no TTL option — so the cost is a delayed teardown
|
|
40
|
+
* rather than a lost one, provided the application actually schedules the
|
|
41
|
+
* sweep, which is its job and not the framework's. Still strictly better than
|
|
42
|
+
* failing the teardown.
|
|
43
|
+
*/
|
|
44
|
+
async function wasCancelRequested(runs, runId) {
|
|
45
|
+
try {
|
|
46
|
+
return (await runs.get(runId))?.cancelRequested === true;
|
|
47
|
+
} catch {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
export { RUN_CANCEL_REASON, isCancelRequestedReason, requestRunCancel, wasCancelRequested };
|
|
53
|
+
|
|
54
|
+
//# sourceMappingURL=cancel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cancel.js","names":[],"sources":["../../../../src/activities/chat/cancel.ts"],"sourcesContent":["/**\n * Out-of-band run cancellation.\n *\n * `ChatClient.stop()` only aborts a local `AbortController`; it sends nothing to\n * the server. A user pressing Stop and a user refreshing the page produce the\n * IDENTICAL TCP close, so intent is **not inferable** from a disconnect. It has\n * to arrive out of band, and there are exactly two bands:\n *\n * 1. **Durable** — {@link requestRunCancel} records the intent on the run's\n * `RunRecord`. This is the only channel that works when the run is being\n * driven by a DIFFERENT host than the one the cancel request reached, which\n * is the normal case for a detached run.\n * 2. **In-process** — abort the run's signal with {@link RUN_CANCEL_REASON}.\n * Core reads the reason back when it builds `AbortInfo`, so\n * `AbortInfo.cancelRequested` is `true` for that abort and `false` for a\n * disconnect. This is the fast path when the cancel reaches the driving host.\n *\n * A cancel endpoint SHOULD do both: record it (so a remote driver observes it)\n * and abort locally (so a co-located driver stops immediately).\n */\nimport type { RunStore } from './middleware/run-store'\n\n/**\n * Abort reason that marks an abort as an explicit cancellation.\n *\n * Namespaced so an application's own reason string cannot collide with it by\n * accident, and matched with `===` (never a substring test) so an arbitrary\n * provider error message can never be read as a deliberate cancel.\n */\nexport const RUN_CANCEL_REASON = 'tanstack-ai:cancel-requested'\n\n/** Whether an abort reason means \"the user explicitly cancelled this run\". */\nexport function isCancelRequestedReason(reason: string | undefined): boolean {\n return reason === RUN_CANCEL_REASON\n}\n\n/**\n * Record an explicit cancel on the run record.\n *\n * Deliberately does NOT set a status. The driver is the only actor that knows\n * when the agent has actually stopped and the sandbox has been torn down, so it\n * owns the transition to `'aborted'`. Writing a terminal status here would tell\n * every reader the run is over while the agent is still burning tokens.\n *\n * A no-op for an unknown `runId`, inheriting `RunStore.update`'s documented\n * invariant.\n */\nexport async function requestRunCancel(\n runs: RunStore,\n runId: string,\n): Promise<void> {\n await runs.update(runId, { cancelRequested: true })\n}\n\n/**\n * Whether an explicit cancel has been recorded for `runId`.\n *\n * Answers `false` rather than throwing when the store cannot be read. Callers\n * are middleware abort hooks, which are already on a teardown path, and a\n * store failure there must not replace the caller's own reason for tearing\n * down with a store error. The cost of a false negative is that a cancel\n * degrades into a detach — the run record gains `detachedSince`/`sandboxKey`\n * instead of transitioning to `'aborted'`. `@tanstack/ai-sandbox`'s\n * `reapDetachedRuns` recovers that run once the `detachedRunTtlMs` the\n * application passes to that sweep has elapsed — nothing derives it from\n * `withSandbox`, which has no TTL option — so the cost is a delayed teardown\n * rather than a lost one, provided the application actually schedules the\n * sweep, which is its job and not the framework's. Still strictly better than\n * failing the teardown.\n */\nexport async function wasCancelRequested(\n runs: RunStore,\n runId: string,\n): Promise<boolean> {\n try {\n const record = await runs.get(runId)\n return record?.cancelRequested === true\n } catch {\n return false\n }\n}\n"],"mappings":";;;;;;;;AA6BA,IAAa,oBAAoB;;AAGjC,SAAgB,wBAAwB,QAAqC;CAC3E,OAAO,WAAW;AACpB;;;;;;;;;;;;AAaA,eAAsB,iBACpB,MACA,OACe;CACf,MAAM,KAAK,OAAO,OAAO,EAAE,iBAAiB,KAAK,CAAC;AACpD;;;;;;;;;;;;;;;;;AAkBA,eAAsB,mBACpB,MACA,OACkB;CAClB,IAAI;EAEF,QAAO,MADc,KAAK,IAAI,KAAK,EAAA,EACpB,oBAAoB;CACrC,QAAQ;EACN,OAAO;CACT;AACF"}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { AnyTextAdapter } from './adapter.js';
|
|
2
|
-
import { AnyTool,
|
|
2
|
+
import { AnyTool, ConstrainedModelMessage, InferSchemaType, LazyToolsConfig, ModelMessage, ProviderTool, SchemaInput, StructuredOutputStream, TextOptions, TypedStreamChunk, UIMessage } from '../../types.js';
|
|
3
3
|
import { AnyChatMiddleware, ChatMiddleware } from './middleware/types.js';
|
|
4
4
|
import { CheckCoverage } from './middleware/builder.js';
|
|
5
5
|
import { SystemPrompt } from '../../system-prompts.js';
|
|
6
6
|
import { DebugOption } from '../../logger/types.js';
|
|
7
|
-
import { ProviderTool } from '../../tools/provider-tool.js';
|
|
8
7
|
import { ContextFromMiddleware, ContextFromTool, DefinedContext, MergeContext, UnionToIntersection } from './runtime-context-types.js';
|
|
9
8
|
import { ChatMCPOptions } from './mcp/types.js';
|
|
10
9
|
/** The adapter kind this activity handles */
|
|
@@ -40,9 +39,10 @@ type TextActivityOptionsWithContext<TAdapter extends AnyTextAdapter, TSchema ext
|
|
|
40
39
|
* @template TAdapter - The text adapter type (created by a provider function)
|
|
41
40
|
* @template TSchema - Optional Standard Schema for structured output
|
|
42
41
|
* @template TStream - Whether to stream the output (default: true)
|
|
42
|
+
* @template TContext - Runtime context value threaded to middleware hooks and server tools
|
|
43
43
|
*/
|
|
44
44
|
export interface TextActivityOptions<TAdapter extends AnyTextAdapter, TSchema extends SchemaInput | undefined, TStream extends boolean, TContext = unknown> {
|
|
45
|
-
/** The text adapter to use (created by a provider function like openaiText('gpt-
|
|
45
|
+
/** The text adapter to use (created by a provider function like openaiText('gpt-5.5')) */
|
|
46
46
|
adapter: TAdapter;
|
|
47
47
|
/**
|
|
48
48
|
* Conversation messages. Accepts:
|
|
@@ -76,7 +76,7 @@ export interface TextActivityOptions<TAdapter extends AnyTextAdapter, TSchema ex
|
|
|
76
76
|
* `supports.tools` list. Passing an unsupported tool produces a
|
|
77
77
|
* compile-time error on the array element.
|
|
78
78
|
*/
|
|
79
|
-
tools?:
|
|
79
|
+
tools?: ReadonlyArray<(AnyRuntimeTool & {
|
|
80
80
|
readonly '~toolKind'?: never;
|
|
81
81
|
}) | ProviderTool<string, TAdapter['~types']['toolCapabilities'][number]>> | undefined;
|
|
82
82
|
/**
|
|
@@ -107,6 +107,13 @@ export interface TextActivityOptions<TAdapter extends AnyTextAdapter, TSchema ex
|
|
|
107
107
|
runId?: TextOptions['runId'];
|
|
108
108
|
/** Parent run ID for AG-UI protocol nested run correlation. */
|
|
109
109
|
parentRunId?: TextOptions['parentRunId'];
|
|
110
|
+
/** Application state mirrored in a STATE_SNAPSHOT before an interrupt terminal. */
|
|
111
|
+
state?: TextOptions['state'];
|
|
112
|
+
/**
|
|
113
|
+
* AG-UI interrupt resume responses. Persistence middleware validates these
|
|
114
|
+
* before accepting new input on a thread with pending interrupts.
|
|
115
|
+
*/
|
|
116
|
+
resume?: TextOptions['resume'];
|
|
110
117
|
/**
|
|
111
118
|
* Optional Standard Schema for structured output.
|
|
112
119
|
* When provided, the activity will:
|
|
@@ -118,7 +125,7 @@ export interface TextActivityOptions<TAdapter extends AnyTextAdapter, TSchema ex
|
|
|
118
125
|
* @example
|
|
119
126
|
* ```ts
|
|
120
127
|
* const result = await chat({
|
|
121
|
-
* adapter: openaiText('gpt-
|
|
128
|
+
* adapter: openaiText('gpt-5.5'),
|
|
122
129
|
* messages: [{ role: 'user', content: 'Generate a person' }],
|
|
123
130
|
* outputSchema: z.object({ name: z.string(), age: z.number() })
|
|
124
131
|
* })
|
|
@@ -128,7 +135,7 @@ export interface TextActivityOptions<TAdapter extends AnyTextAdapter, TSchema ex
|
|
|
128
135
|
outputSchema?: TSchema;
|
|
129
136
|
/**
|
|
130
137
|
* Whether to stream the text result.
|
|
131
|
-
* When true (default), returns an AsyncIterable<
|
|
138
|
+
* When true (default), returns an AsyncIterable<TypedStreamChunk<TTools>> for streaming output.
|
|
132
139
|
* When false, returns a Promise<string> with the collected text content.
|
|
133
140
|
*
|
|
134
141
|
* Note: If outputSchema is provided, this option is ignored and the result
|
|
@@ -139,7 +146,7 @@ export interface TextActivityOptions<TAdapter extends AnyTextAdapter, TSchema ex
|
|
|
139
146
|
* @example Non-streaming text
|
|
140
147
|
* ```ts
|
|
141
148
|
* const text = await chat({
|
|
142
|
-
* adapter: openaiText('gpt-
|
|
149
|
+
* adapter: openaiText('gpt-5.5'),
|
|
143
150
|
* messages: [{ role: 'user', content: 'Hello!' }],
|
|
144
151
|
* stream: false
|
|
145
152
|
* })
|
|
@@ -154,7 +161,7 @@ export interface TextActivityOptions<TAdapter extends AnyTextAdapter, TSchema ex
|
|
|
154
161
|
* @example
|
|
155
162
|
* ```ts
|
|
156
163
|
* const stream = chat({
|
|
157
|
-
* adapter: openaiText('gpt-
|
|
164
|
+
* adapter: openaiText('gpt-5.5'),
|
|
158
165
|
* messages: [...],
|
|
159
166
|
* middleware: [loggingMiddleware, redactionMiddleware],
|
|
160
167
|
* })
|
|
@@ -186,7 +193,9 @@ export interface TextActivityOptions<TAdapter extends AnyTextAdapter, TSchema ex
|
|
|
186
193
|
* const stream = chat({ ...chatOptions, messages })
|
|
187
194
|
* ```
|
|
188
195
|
*/
|
|
189
|
-
export declare function createChatOptions<TAdapter extends AnyTextAdapter, TSchema extends SchemaInput | undefined = undefined, TStream extends boolean = true, const TTools extends TextActivityOptions<TAdapter, TSchema, TStream, any>['tools'] = TextActivityOptions<TAdapter, TSchema, TStream, any>['tools'], const TMiddleware extends TextActivityOptions<TAdapter, TSchema, TStream, any>['middleware'] = TextActivityOptions<TAdapter, TSchema, TStream, any>['middleware']>(options: TextActivityOptionsWithContext<TAdapter, TSchema, TStream, TTools, TMiddleware>): TextActivityOptions<TAdapter, TSchema, TStream, InferredContext<TTools, TMiddleware
|
|
196
|
+
export declare function createChatOptions<TAdapter extends AnyTextAdapter, TSchema extends SchemaInput | undefined = undefined, TStream extends boolean = true, const TTools extends TextActivityOptions<TAdapter, TSchema, TStream, any>['tools'] = TextActivityOptions<TAdapter, TSchema, TStream, any>['tools'], const TMiddleware extends TextActivityOptions<TAdapter, TSchema, TStream, any>['middleware'] = TextActivityOptions<TAdapter, TSchema, TStream, any>['middleware']>(options: TextActivityOptionsWithContext<TAdapter, TSchema, TStream, TTools, TMiddleware>): Omit<TextActivityOptions<TAdapter, TSchema, TStream, InferredContext<TTools, TMiddleware>>, 'tools'> & {
|
|
197
|
+
tools?: TTools;
|
|
198
|
+
};
|
|
190
199
|
/**
|
|
191
200
|
* Result type for the text activity.
|
|
192
201
|
* - If outputSchema is provided AND stream is explicitly true:
|
|
@@ -196,14 +205,17 @@ export declare function createChatOptions<TAdapter extends AnyTextAdapter, TSche
|
|
|
196
205
|
* - If outputSchema is provided without explicit stream:true:
|
|
197
206
|
* Promise<InferSchemaType<TSchema>>.
|
|
198
207
|
* - If stream is explicitly false (no schema): Promise<string>.
|
|
199
|
-
* - Otherwise (default): AsyncIterable<
|
|
208
|
+
* - Otherwise (default): AsyncIterable<TypedStreamChunk<TTools>>.
|
|
209
|
+
*
|
|
210
|
+
* When tools with typed schemas are provided, the stream chunks include
|
|
211
|
+
* type-safe `toolName` and `input` fields on tool call events.
|
|
200
212
|
*
|
|
201
213
|
* `[TStream] extends [true]` is used (not `TStream extends true`) so that the
|
|
202
214
|
* default `boolean` value of `TStream` does *not* match the streaming branch.
|
|
203
215
|
* Without this, plain `chat({ outputSchema })` would type as a stream while
|
|
204
216
|
* the runtime returns a Promise — see issue #526.
|
|
205
217
|
*/
|
|
206
|
-
export type TextActivityResult<TSchema extends SchemaInput | undefined, TStream extends boolean = boolean
|
|
218
|
+
export type TextActivityResult<TSchema extends SchemaInput | undefined, TStream extends boolean = boolean, TTools = ReadonlyArray<AnyTool>> = TSchema extends SchemaInput ? [TStream] extends [true] ? StructuredOutputStream<InferSchemaType<TSchema>> : Promise<InferSchemaType<TSchema>> : [TStream] extends [false] ? Promise<string> : AsyncIterable<TypedStreamChunk<TTools extends ReadonlyArray<AnyTool> ? TTools : ReadonlyArray<AnyTool>>>;
|
|
207
219
|
/**
|
|
208
220
|
* Text activity - handles agentic text generation, one-shot text generation, and agentic structured output.
|
|
209
221
|
*
|
|
@@ -219,7 +231,7 @@ export type TextActivityResult<TSchema extends SchemaInput | undefined, TStream
|
|
|
219
231
|
* import { openaiText } from '@tanstack/ai-openai'
|
|
220
232
|
*
|
|
221
233
|
* for await (const chunk of chat({
|
|
222
|
-
* adapter: openaiText('gpt-
|
|
234
|
+
* adapter: openaiText('gpt-5.5'),
|
|
223
235
|
* messages: [{ role: 'user', content: 'What is the weather?' }],
|
|
224
236
|
* tools: [weatherTool]
|
|
225
237
|
* })) {
|
|
@@ -232,7 +244,7 @@ export type TextActivityResult<TSchema extends SchemaInput | undefined, TStream
|
|
|
232
244
|
* @example One-shot text (streaming without tools)
|
|
233
245
|
* ```ts
|
|
234
246
|
* for await (const chunk of chat({
|
|
235
|
-
* adapter: openaiText('gpt-
|
|
247
|
+
* adapter: openaiText('gpt-5.5'),
|
|
236
248
|
* messages: [{ role: 'user', content: 'Hello!' }]
|
|
237
249
|
* })) {
|
|
238
250
|
* console.log(chunk)
|
|
@@ -242,7 +254,7 @@ export type TextActivityResult<TSchema extends SchemaInput | undefined, TStream
|
|
|
242
254
|
* @example Non-streaming text (stream: false)
|
|
243
255
|
* ```ts
|
|
244
256
|
* const text = await chat({
|
|
245
|
-
* adapter: openaiText('gpt-
|
|
257
|
+
* adapter: openaiText('gpt-5.5'),
|
|
246
258
|
* messages: [{ role: 'user', content: 'Hello!' }],
|
|
247
259
|
* stream: false
|
|
248
260
|
* })
|
|
@@ -254,7 +266,7 @@ export type TextActivityResult<TSchema extends SchemaInput | undefined, TStream
|
|
|
254
266
|
* import { z } from 'zod'
|
|
255
267
|
*
|
|
256
268
|
* const result = await chat({
|
|
257
|
-
* adapter: openaiText('gpt-
|
|
269
|
+
* adapter: openaiText('gpt-5.5'),
|
|
258
270
|
* messages: [{ role: 'user', content: 'Research and summarize the topic' }],
|
|
259
271
|
* tools: [researchTool, analyzeTool],
|
|
260
272
|
* outputSchema: z.object({
|
|
@@ -265,6 +277,6 @@ export type TextActivityResult<TSchema extends SchemaInput | undefined, TStream
|
|
|
265
277
|
* // result is { summary: string, keyPoints: string[] }
|
|
266
278
|
* ```
|
|
267
279
|
*/
|
|
268
|
-
export declare function chat<TAdapter extends AnyTextAdapter, TSchema extends SchemaInput | undefined = undefined, TStream extends boolean = boolean, const TTools extends TextActivityOptions<TAdapter, TSchema, TStream, any>['tools'] = TextActivityOptions<TAdapter, TSchema, TStream, any>['tools'], const TMiddleware extends TextActivityOptions<TAdapter, TSchema, TStream, any>['middleware'] = TextActivityOptions<TAdapter, TSchema, TStream, any>['middleware']>(options: TextActivityOptionsWithContext<TAdapter, TSchema, TStream, TTools, TMiddleware>): TextActivityResult<TSchema, TStream>;
|
|
280
|
+
export declare function chat<TAdapter extends AnyTextAdapter, TSchema extends SchemaInput | undefined = undefined, TStream extends boolean = boolean, const TTools extends TextActivityOptions<TAdapter, TSchema, TStream, any>['tools'] = TextActivityOptions<TAdapter, TSchema, TStream, any>['tools'], const TMiddleware extends TextActivityOptions<TAdapter, TSchema, TStream, any>['middleware'] = TextActivityOptions<TAdapter, TSchema, TStream, any>['middleware']>(options: TextActivityOptionsWithContext<TAdapter, TSchema, TStream, TTools, TMiddleware>): TextActivityResult<TSchema, TStream, TTools>;
|
|
269
281
|
export type { TextAdapter, TextAdapterConfig, StructuredOutputOptions, StructuredOutputResult, } from './adapter.js';
|
|
270
282
|
export { BaseTextAdapter } from './adapter.js';
|