@tanstack/ai 0.19.1 → 0.20.1
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/dist/esm/activities/chat/adapter.d.ts +9 -3
- package/dist/esm/activities/chat/adapter.js +3 -1
- package/dist/esm/activities/chat/adapter.js.map +1 -1
- package/dist/esm/activities/chat/index.d.ts +13 -4
- package/dist/esm/activities/chat/index.js +46 -18
- package/dist/esm/activities/chat/index.js.map +1 -1
- package/dist/esm/activities/chat/messages.js +1 -1
- package/dist/esm/activities/chat/messages.js.map +1 -1
- package/dist/esm/activities/chat/middleware/compose.js +2 -0
- package/dist/esm/activities/chat/middleware/compose.js.map +1 -1
- package/dist/esm/activities/chat/middleware/types.d.ts +8 -7
- package/dist/esm/activities/chat/stream/processor.d.ts +8 -8
- package/dist/esm/activities/chat/stream/processor.js +29 -21
- package/dist/esm/activities/chat/stream/processor.js.map +1 -1
- package/dist/esm/activities/chat/stream/strategies.d.ts +3 -3
- package/dist/esm/activities/chat/stream/strategies.js +4 -4
- package/dist/esm/activities/chat/stream/strategies.js.map +1 -1
- package/dist/esm/activities/chat/tools/lazy-tool-manager.js +5 -0
- package/dist/esm/activities/chat/tools/lazy-tool-manager.js.map +1 -1
- package/dist/esm/activities/chat/tools/schema-converter.d.ts +1 -1
- package/dist/esm/activities/chat/tools/schema-converter.js +36 -35
- package/dist/esm/activities/chat/tools/schema-converter.js.map +1 -1
- package/dist/esm/activities/chat/tools/tool-calls.d.ts +2 -2
- package/dist/esm/activities/chat/tools/tool-calls.js +17 -9
- package/dist/esm/activities/chat/tools/tool-calls.js.map +1 -1
- package/dist/esm/activities/chat/tools/tool-definition.js +1 -1
- package/dist/esm/activities/chat/tools/tool-definition.js.map +1 -1
- package/dist/esm/activities/generateAudio/adapter.js +3 -1
- package/dist/esm/activities/generateAudio/adapter.js.map +1 -1
- package/dist/esm/activities/generateAudio/index.d.ts +5 -1
- package/dist/esm/activities/generateAudio/index.js.map +1 -1
- package/dist/esm/activities/generateImage/adapter.js +3 -1
- package/dist/esm/activities/generateImage/adapter.js.map +1 -1
- package/dist/esm/activities/generateImage/index.js +5 -0
- package/dist/esm/activities/generateImage/index.js.map +1 -1
- package/dist/esm/activities/generateSpeech/adapter.js +3 -1
- package/dist/esm/activities/generateSpeech/adapter.js.map +1 -1
- package/dist/esm/activities/generateTranscription/adapter.js +3 -1
- package/dist/esm/activities/generateTranscription/adapter.js.map +1 -1
- package/dist/esm/activities/generateVideo/adapter.js +3 -1
- package/dist/esm/activities/generateVideo/adapter.js.map +1 -1
- package/dist/esm/activities/stream-generation-result.js +6 -2
- package/dist/esm/activities/stream-generation-result.js.map +1 -1
- package/dist/esm/activities/summarize/adapter.js +3 -1
- package/dist/esm/activities/summarize/adapter.js.map +1 -1
- package/dist/esm/activities/summarize/chat-stream-summarize.d.ts +1 -1
- package/dist/esm/activities/summarize/chat-stream-summarize.js +5 -0
- package/dist/esm/activities/summarize/chat-stream-summarize.js.map +1 -1
- package/dist/esm/activities/summarize/index.js.map +1 -1
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/logger/internal-logger.js +2 -0
- package/dist/esm/logger/internal-logger.js.map +1 -1
- package/dist/esm/middlewares/content-guard.js +5 -4
- package/dist/esm/middlewares/content-guard.js.map +1 -1
- package/dist/esm/middlewares/otel.js +38 -16
- package/dist/esm/middlewares/otel.js.map +1 -1
- package/dist/esm/realtime/index.d.ts +1 -1
- package/dist/esm/realtime/index.js.map +1 -1
- package/dist/esm/system-prompts.d.ts +66 -0
- package/dist/esm/system-prompts.js +23 -0
- package/dist/esm/system-prompts.js.map +1 -0
- package/dist/esm/tools/provider-tool.d.ts +9 -0
- package/dist/esm/tools/provider-tool.js +7 -0
- package/dist/esm/tools/provider-tool.js.map +1 -0
- package/dist/esm/types.d.ts +22 -7
- package/dist/esm/utilities/ag-ui-wire.js.map +1 -1
- package/dist/esm/utilities/chat-params.js.map +1 -1
- package/package.json +2 -2
- package/src/activities/chat/adapter.ts +11 -2
- package/src/activities/chat/index.ts +46 -27
- package/src/activities/chat/messages.ts +2 -1
- package/src/activities/chat/middleware/types.ts +14 -11
- package/src/activities/chat/stream/processor.ts +19 -19
- package/src/activities/chat/stream/strategies.ts +3 -3
- package/src/activities/chat/tools/schema-converter.ts +77 -52
- package/src/activities/chat/tools/tool-calls.ts +18 -11
- package/src/activities/chat/tools/tool-definition.ts +6 -1
- package/src/activities/generateAudio/index.ts +5 -4
- package/src/activities/generateImage/index.ts +5 -0
- package/src/activities/stream-generation-result.ts +11 -2
- package/src/activities/summarize/chat-stream-summarize.ts +5 -2
- package/src/activities/summarize/index.ts +2 -2
- package/src/index.ts +5 -0
- package/src/middlewares/content-guard.ts +11 -3
- package/src/middlewares/otel.ts +47 -16
- package/src/realtime/index.ts +1 -1
- package/src/system-prompts.ts +98 -0
- package/src/tools/provider-tool.ts +14 -0
- package/src/types.ts +28 -9
- package/src/utilities/ag-ui-wire.ts +2 -2
- package/src/utilities/chat-params.ts +2 -2
|
@@ -45,8 +45,12 @@ export interface StructuredOutputResult<T = unknown> {
|
|
|
45
45
|
* - TMessageMetadata: Metadata types for content parts (already resolved)
|
|
46
46
|
* - TToolCapabilities: Tuple of tool-kind strings supported by this model, resolved from `supports.tools`
|
|
47
47
|
* - TToolCallMetadata: Metadata type that round-trips with tool calls (e.g. Gemini's `thoughtSignature`)
|
|
48
|
+
* - TSystemPromptMetadata: Provider-typed metadata accepted on each
|
|
49
|
+
* `systemPrompts[i]` entry (e.g. Anthropic `cache_control`). Defaults to
|
|
50
|
+
* `never` — adapters without per-prompt metadata reject the `metadata`
|
|
51
|
+
* field at the call site.
|
|
48
52
|
*/
|
|
49
|
-
export interface TextAdapter<TModel extends string, TProviderOptions extends Record<string, any>, TInputModalities extends ReadonlyArray<Modality>, TMessageMetadataByModality extends DefaultMessageMetadataByModality, TToolCapabilities extends ReadonlyArray<string> = ReadonlyArray<string>, TToolCallMetadata = unknown> {
|
|
53
|
+
export interface TextAdapter<TModel extends string, TProviderOptions extends Record<string, any>, TInputModalities extends ReadonlyArray<Modality>, TMessageMetadataByModality extends DefaultMessageMetadataByModality, TToolCapabilities extends ReadonlyArray<string> = ReadonlyArray<string>, TToolCallMetadata = unknown, TSystemPromptMetadata = never> {
|
|
50
54
|
/** Discriminator for adapter kind */
|
|
51
55
|
readonly kind: 'text';
|
|
52
56
|
/** Provider name identifier (e.g., 'openai', 'anthropic') */
|
|
@@ -62,6 +66,7 @@ export interface TextAdapter<TModel extends string, TProviderOptions extends Rec
|
|
|
62
66
|
messageMetadataByModality: TMessageMetadataByModality;
|
|
63
67
|
toolCapabilities: TToolCapabilities;
|
|
64
68
|
toolCallMetadata: TToolCallMetadata;
|
|
69
|
+
systemPromptMetadata: TSystemPromptMetadata;
|
|
65
70
|
};
|
|
66
71
|
/**
|
|
67
72
|
* Stream text completions from the model
|
|
@@ -95,14 +100,14 @@ export interface TextAdapter<TModel extends string, TProviderOptions extends Rec
|
|
|
95
100
|
* A TextAdapter with any/unknown type parameters.
|
|
96
101
|
* Useful as a constraint in generic functions and interfaces.
|
|
97
102
|
*/
|
|
98
|
-
export type AnyTextAdapter = TextAdapter<any, any, any, any, any, any>;
|
|
103
|
+
export type AnyTextAdapter = TextAdapter<any, any, any, any, any, any, any>;
|
|
99
104
|
/**
|
|
100
105
|
* Abstract base class for text adapters.
|
|
101
106
|
* Extend this class to implement a text adapter for a specific provider.
|
|
102
107
|
*
|
|
103
108
|
* Generic parameters match TextAdapter - all pre-resolved by the provider function.
|
|
104
109
|
*/
|
|
105
|
-
export declare abstract class BaseTextAdapter<TModel extends string, TProviderOptions extends Record<string, any>, TInputModalities extends ReadonlyArray<Modality>, TMessageMetadataByModality extends DefaultMessageMetadataByModality, TToolCapabilities extends ReadonlyArray<string> = ReadonlyArray<string>, TToolCallMetadata = unknown> implements TextAdapter<TModel, TProviderOptions, TInputModalities, TMessageMetadataByModality, TToolCapabilities, TToolCallMetadata> {
|
|
110
|
+
export declare abstract class BaseTextAdapter<TModel extends string, TProviderOptions extends Record<string, any>, TInputModalities extends ReadonlyArray<Modality>, TMessageMetadataByModality extends DefaultMessageMetadataByModality, TToolCapabilities extends ReadonlyArray<string> = ReadonlyArray<string>, TToolCallMetadata = unknown, TSystemPromptMetadata = never> implements TextAdapter<TModel, TProviderOptions, TInputModalities, TMessageMetadataByModality, TToolCapabilities, TToolCallMetadata, TSystemPromptMetadata> {
|
|
106
111
|
readonly kind: "text";
|
|
107
112
|
abstract readonly name: string;
|
|
108
113
|
readonly model: TModel;
|
|
@@ -112,6 +117,7 @@ export declare abstract class BaseTextAdapter<TModel extends string, TProviderOp
|
|
|
112
117
|
messageMetadataByModality: TMessageMetadataByModality;
|
|
113
118
|
toolCapabilities: TToolCapabilities;
|
|
114
119
|
toolCallMetadata: TToolCallMetadata;
|
|
120
|
+
systemPromptMetadata: TSystemPromptMetadata;
|
|
115
121
|
};
|
|
116
122
|
protected config: TextAdapterConfig;
|
|
117
123
|
constructor(config: TextAdapterConfig | undefined, model: TModel);
|
|
@@ -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} from '../../types'\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}\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 */\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> {\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 * @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 }\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/**\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>\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> implements TextAdapter<\n TModel,\n TProviderOptions,\n TInputModalities,\n TMessageMetadataByModality,\n TToolCapabilities,\n TToolCallMetadata\n> {\n readonly kind = 'text' as const\n abstract readonly name: string\n readonly model: TModel\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 }\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"],"names":[],"mappings":"
|
|
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} from '../../types'\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}\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 * @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/**\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\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"],"names":[],"mappings":"AA2IO,MAAe,gBAgBpB;AAAA,EACS,OAAO;AAAA,EAEP;AAAA,EAYC;AAAA,EAEV,YAAY,SAA4B,CAAA,GAAI,OAAe;AACzD,SAAK,SAAS;AACd,SAAK,QAAQ;AAAA,EACf;AAAA,EAcU,aAAqB;AAC7B,WAAO,GAAG,KAAK,IAAI,IAAI,KAAK,KAAK,IAAI,KAAK,OAAA,EAAS,SAAS,EAAE,EAAE,UAAU,CAAC,CAAC;AAAA,EAC9E;AACF;"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { AnyTextAdapter } from './adapter.js';
|
|
2
2
|
import { ConstrainedModelMessage, InferSchemaType, ModelMessage, SchemaInput, StreamChunk, StructuredOutputStream, TextOptions, Tool, UIMessage } from '../../types.js';
|
|
3
3
|
import { ChatMiddleware } from './middleware/types.js';
|
|
4
|
+
import { SystemPrompt } from '../../system-prompts.js';
|
|
4
5
|
import { DebugOption } from '../../logger/types.js';
|
|
5
6
|
import { ProviderTool } from '../../tools/provider-tool.js';
|
|
6
7
|
/** The adapter kind this activity handles */
|
|
@@ -27,9 +28,17 @@ export interface TextActivityOptions<TAdapter extends AnyTextAdapter, TSchema ex
|
|
|
27
28
|
messages?: Array<UIMessage | ModelMessage | ConstrainedModelMessage<{
|
|
28
29
|
inputModalities: TAdapter['~types']['inputModalities'];
|
|
29
30
|
messageMetadataByModality: TAdapter['~types']['messageMetadataByModality'];
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
|
|
31
|
+
}>> | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* System prompts to prepend to the conversation.
|
|
34
|
+
*
|
|
35
|
+
* Accepts plain strings or `{ content, metadata }` objects. The `metadata`
|
|
36
|
+
* field is typed by the adapter — Anthropic narrows it to
|
|
37
|
+
* `AnthropicSystemPromptMetadata` (with `cache_control` for prompt
|
|
38
|
+
* caching), providers without per-prompt metadata reject the field
|
|
39
|
+
* entirely.
|
|
40
|
+
*/
|
|
41
|
+
systemPrompts?: Array<SystemPrompt<TAdapter['~types']['systemPromptMetadata']>> | undefined;
|
|
33
42
|
/**
|
|
34
43
|
* Tools for function calling (auto-executed when called).
|
|
35
44
|
*
|
|
@@ -42,7 +51,7 @@ export interface TextActivityOptions<TAdapter extends AnyTextAdapter, TSchema ex
|
|
|
42
51
|
*/
|
|
43
52
|
tools?: Array<(Tool & {
|
|
44
53
|
readonly '~toolKind'?: never;
|
|
45
|
-
}) | ProviderTool<string, TAdapter['~types']['toolCapabilities'][number]
|
|
54
|
+
}) | ProviderTool<string, TAdapter['~types']['toolCapabilities'][number]>> | undefined;
|
|
46
55
|
/** Controls the randomness of the output. Higher values make output more random. Range: [0.0, 2.0] */
|
|
47
56
|
temperature?: TextOptions['temperature'];
|
|
48
57
|
/** Nucleus sampling parameter. The model considers tokens with topP probability mass. */
|
|
@@ -14,22 +14,50 @@ function createChatOptions(options) {
|
|
|
14
14
|
return options;
|
|
15
15
|
}
|
|
16
16
|
class TextEngine {
|
|
17
|
+
adapter;
|
|
18
|
+
params;
|
|
19
|
+
systemPrompts;
|
|
20
|
+
tools;
|
|
21
|
+
loopStrategy;
|
|
22
|
+
toolCallManager;
|
|
23
|
+
lazyToolManager;
|
|
24
|
+
initialMessageCount;
|
|
25
|
+
requestId;
|
|
26
|
+
streamId;
|
|
27
|
+
effectiveRequest;
|
|
28
|
+
effectiveSignal;
|
|
29
|
+
messages;
|
|
30
|
+
iterationCount = 0;
|
|
31
|
+
lastFinishReason = null;
|
|
32
|
+
streamStartTime = 0;
|
|
33
|
+
totalChunkCount = 0;
|
|
34
|
+
currentMessageId = null;
|
|
35
|
+
accumulatedContent = "";
|
|
36
|
+
accumulatedThinking = [];
|
|
37
|
+
currentThinkingContent = "";
|
|
38
|
+
currentThinkingSignature = "";
|
|
39
|
+
eventOptions;
|
|
40
|
+
eventToolNames;
|
|
41
|
+
finishedEvent = null;
|
|
42
|
+
earlyTermination = false;
|
|
43
|
+
toolPhase = "continue";
|
|
44
|
+
cyclePhase = "processText";
|
|
45
|
+
// Client state extracted from initial messages (before conversion to ModelMessage)
|
|
46
|
+
initialApprovals;
|
|
47
|
+
initialClientToolResults;
|
|
48
|
+
// AG-UI protocol IDs
|
|
49
|
+
threadId;
|
|
50
|
+
runIdOverride;
|
|
51
|
+
parentRunIdOverride;
|
|
52
|
+
// Middleware support
|
|
53
|
+
middlewareRunner;
|
|
54
|
+
middlewareCtx;
|
|
55
|
+
deferredPromises = [];
|
|
56
|
+
abortReason;
|
|
57
|
+
middlewareAbortController;
|
|
58
|
+
terminalHookCalled = false;
|
|
59
|
+
logger;
|
|
17
60
|
constructor(config, logger) {
|
|
18
|
-
this.iterationCount = 0;
|
|
19
|
-
this.lastFinishReason = null;
|
|
20
|
-
this.streamStartTime = 0;
|
|
21
|
-
this.totalChunkCount = 0;
|
|
22
|
-
this.currentMessageId = null;
|
|
23
|
-
this.accumulatedContent = "";
|
|
24
|
-
this.accumulatedThinking = [];
|
|
25
|
-
this.currentThinkingContent = "";
|
|
26
|
-
this.currentThinkingSignature = "";
|
|
27
|
-
this.finishedEvent = null;
|
|
28
|
-
this.earlyTermination = false;
|
|
29
|
-
this.toolPhase = "continue";
|
|
30
|
-
this.cyclePhase = "processText";
|
|
31
|
-
this.deferredPromises = [];
|
|
32
|
-
this.terminalHookCalled = false;
|
|
33
61
|
this.logger = logger;
|
|
34
62
|
this.adapter = config.adapter;
|
|
35
63
|
this.params = config.params;
|
|
@@ -527,11 +555,10 @@ class TextEngine {
|
|
|
527
555
|
}
|
|
528
556
|
return true;
|
|
529
557
|
});
|
|
530
|
-
if (undiscoveredLazyResults.length > 0) {
|
|
531
|
-
const finishEvt = this.finishedEvent;
|
|
558
|
+
if (undiscoveredLazyResults.length > 0 && this.finishedEvent) {
|
|
532
559
|
for (const chunk of this.buildToolResultChunks(
|
|
533
560
|
undiscoveredLazyResults,
|
|
534
|
-
|
|
561
|
+
this.finishedEvent
|
|
535
562
|
)) {
|
|
536
563
|
yield* this.pipeThroughMiddleware(chunk);
|
|
537
564
|
}
|
|
@@ -977,6 +1004,7 @@ async function* runStreamingText(options) {
|
|
|
977
1004
|
}
|
|
978
1005
|
function runNonStreamingText(options) {
|
|
979
1006
|
const stream = runStreamingText(
|
|
1007
|
+
// eslint-disable-next-line no-restricted-syntax -- generic-stream remap: caller is non-streaming (false), but runStreamingText is invoked internally to collect text; concrete `false`→`true` literals don't structurally overlap.
|
|
980
1008
|
options
|
|
981
1009
|
);
|
|
982
1010
|
return streamToText(stream);
|