@outputai/llm 0.11.0 → 0.11.1-next.2223fa5.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/package.json +2 -2
- package/src/agent.js +105 -83
- package/src/agent.spec.js +312 -225
- package/src/ai_provider.js +2 -2
- package/src/ai_sdk_options.js +93 -39
- package/src/ai_sdk_options.spec.js +314 -154
- package/src/consts.js +6 -0
- package/src/generate.js +81 -0
- package/src/generate.spec.js +466 -0
- package/src/index.d.ts +222 -242
- package/src/index.js +3 -4
- package/src/prompt/content.js +70 -0
- package/src/prompt/content.spec.js +208 -0
- package/src/prompt/{escape.js → interpolations.js} +11 -18
- package/src/prompt/interpolations.spec.js +109 -0
- package/src/prompt/loader.full.spec.js +293 -0
- package/src/prompt/loader.js +45 -39
- package/src/prompt/loader.spec.js +186 -289
- package/src/prompt/markup/attributes.js +52 -0
- package/src/prompt/markup/attributes.spec.js +132 -0
- package/src/prompt/markup/nodes.js +71 -0
- package/src/prompt/markup/nodes.spec.js +333 -0
- package/src/prompt/markup/tokenizer.js +3 -0
- package/src/prompt/markup/tokenizer.spec.js +149 -0
- package/src/prompt/markup/tokens.js +26 -0
- package/src/prompt/markup/tokens.spec.js +250 -0
- package/src/prompt/validations.js +98 -67
- package/src/prompt/validations.spec.js +204 -47
- package/src/{prompt/load_content.js → utils/file.js} +12 -15
- package/src/utils/file.spec.js +89 -0
- package/src/utils/models.js +15 -0
- package/src/utils/models.spec.js +119 -0
- package/src/utils/skills.js +74 -0
- package/src/utils/skills.spec.js +168 -0
- package/src/utils/sources.js +48 -0
- package/src/utils/sources.spec.js +122 -0
- package/src/utils/stream.js +22 -0
- package/src/utils/stream.spec.js +55 -0
- package/src/utils/tools.js +47 -0
- package/src/utils/tools.spec.js +167 -0
- package/src/utils/wrap.js +148 -0
- package/src/utils/wrap.spec.js +359 -0
- package/src/validations.js +149 -27
- package/src/validations.spec.js +451 -53
- package/src/ai_model.js +0 -60
- package/src/ai_model.spec.js +0 -259
- package/src/ai_sdk.js +0 -92
- package/src/ai_sdk.spec.js +0 -564
- package/src/prompt/block_options.js +0 -58
- package/src/prompt/block_options.spec.js +0 -71
- package/src/prompt/blocks.js +0 -47
- package/src/prompt/blocks.spec.js +0 -63
- package/src/prompt/escape.spec.js +0 -159
- package/src/prompt/load_content.spec.js +0 -83
- package/src/prompt/loader_validation.spec.js +0 -128
- package/src/prompt/parser.js +0 -16
- package/src/prompt/parser.spec.js +0 -186
- package/src/prompt/prepare_text.js +0 -27
- package/src/prompt/prepare_text.spec.js +0 -141
- package/src/prompt/skill.js +0 -128
- package/src/prompt/skill.spec.js +0 -172
- package/src/utils/message.js +0 -3
- package/src/utils/message.spec.js +0 -29
- package/src/utils/response_wrappers.js +0 -100
- package/src/utils/response_wrappers.spec.js +0 -240
- package/src/utils/source_extraction.js +0 -53
- package/src/utils/source_extraction.spec.js +0 -194
- package/src/utils/trace.js +0 -19
- package/src/utils/trace.spec.js +0 -112
package/src/index.d.ts
CHANGED
|
@@ -1,45 +1,24 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
AgentCallParameters,
|
|
3
|
-
AgentStreamParameters,
|
|
4
2
|
GenerateTextResult as AIGenerateTextResult,
|
|
5
3
|
GenerateImageResult as AIGenerateImageResult,
|
|
6
4
|
StreamTextResult as AIStreamTextResult,
|
|
7
5
|
ToolLoopAgent as AIToolLoopAgent,
|
|
8
6
|
ToolSet,
|
|
9
|
-
ModelMessage,
|
|
10
|
-
StreamTextOnFinishCallback,
|
|
11
|
-
generateText as aiGenerateText,
|
|
12
|
-
streamText as aiStreamText,
|
|
13
|
-
generateImage as aiGenerateImage
|
|
14
|
-
} from 'ai';
|
|
15
|
-
import type { Output as AIOutputNamespace } from 'ai';
|
|
16
|
-
|
|
17
|
-
// Re-export AI SDK types directly (auto-synced with AI SDK updates)
|
|
18
|
-
export type {
|
|
19
|
-
LanguageModelUsage,
|
|
20
|
-
FinishReason,
|
|
21
|
-
LanguageModelResponseMetadata,
|
|
22
|
-
ProviderMetadata,
|
|
23
|
-
CallWarning,
|
|
24
|
-
Warning,
|
|
25
|
-
CallSettings,
|
|
26
|
-
ToolSet,
|
|
27
7
|
ToolChoice,
|
|
28
|
-
Tool,
|
|
29
8
|
StopCondition,
|
|
30
|
-
|
|
31
|
-
GenerateTextOnStepFinishCallback,
|
|
32
|
-
PrepareStepFunction,
|
|
33
|
-
PrepareStepResult,
|
|
9
|
+
ModelMessage,
|
|
34
10
|
StreamTextOnChunkCallback,
|
|
35
11
|
StreamTextOnFinishCallback,
|
|
36
|
-
StreamTextOnErrorCallback
|
|
37
|
-
StreamTextTransform,
|
|
38
|
-
TextStreamPart
|
|
12
|
+
StreamTextOnErrorCallback
|
|
39
13
|
} from 'ai';
|
|
14
|
+
import type { Output as AIOutputNamespace } from 'ai';
|
|
15
|
+
import type { Tracing } from '@outputai/core/sdk/runtime';
|
|
16
|
+
|
|
17
|
+
/** Full AI SDK module (values and types). Use `aiSdk.Output`, `aiSdk.tool`, `aiSdk.stepCountIs`, `aiSdk.ToolSet`, and other AI SDK APIs. */
|
|
18
|
+
export * as aiSdk from 'ai';
|
|
40
19
|
|
|
41
|
-
|
|
42
|
-
export
|
|
20
|
+
/** Liquid interpolation variables, including nested objects and arrays. */
|
|
21
|
+
export type PromptVariables = Record<string, unknown>;
|
|
43
22
|
|
|
44
23
|
/**
|
|
45
24
|
* Represents a single message in a prompt conversation.
|
|
@@ -53,16 +32,17 @@ export { tool, Output, smoothStream, stepCountIs, hasToolCall, jsonSchema } from
|
|
|
53
32
|
* ```
|
|
54
33
|
*/
|
|
55
34
|
export type PromptMessage = {
|
|
56
|
-
/** The role
|
|
57
|
-
role:
|
|
35
|
+
/** The message role. Authored prompt blocks support 'system', 'user', and 'assistant'. */
|
|
36
|
+
role: 'system' | 'user' | 'assistant';
|
|
58
37
|
/** The content of the message */
|
|
59
38
|
content: string;
|
|
60
39
|
/**
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
40
|
+
* Per-message provider options resolved at load from the role tag's `options` attribute and
|
|
41
|
+
* matching `config.messageOptions` sets. Authored as `<system options="set_a set_b">`.
|
|
42
|
+
* `options` is the only supported role-tag attribute and must have a value. Omitted when the tag
|
|
43
|
+
* has no effective `options`.
|
|
64
44
|
*/
|
|
65
|
-
|
|
45
|
+
providerOptions?: Record<string, Record<string, unknown>>;
|
|
66
46
|
};
|
|
67
47
|
|
|
68
48
|
/**
|
|
@@ -72,13 +52,18 @@ export type PromptMessage = {
|
|
|
72
52
|
* ```ts
|
|
73
53
|
* const prompt: Prompt = {
|
|
74
54
|
* name: 'summarizePrompt',
|
|
55
|
+
* fileDir: '/app/prompts',
|
|
56
|
+
* variables: {},
|
|
75
57
|
* config: {
|
|
76
58
|
* provider: 'anthropic',
|
|
77
59
|
* model: 'claude-opus-4-1',
|
|
78
60
|
* temperature: 0.7,
|
|
79
|
-
*
|
|
61
|
+
* maxOutputTokens: 2048,
|
|
62
|
+
* maxSteps: 10,
|
|
63
|
+
* skills: []
|
|
80
64
|
* },
|
|
81
|
-
* messages: [
|
|
65
|
+
* messages: [{ role: 'user', content: 'Summarize this document.' }],
|
|
66
|
+
* instructions: null
|
|
82
67
|
* };
|
|
83
68
|
* ```
|
|
84
69
|
*/
|
|
@@ -87,7 +72,10 @@ export type Prompt = {
|
|
|
87
72
|
name: string;
|
|
88
73
|
|
|
89
74
|
/** Directory containing the resolved prompt file */
|
|
90
|
-
|
|
75
|
+
fileDir: string;
|
|
76
|
+
|
|
77
|
+
/** Interpolation values used when the prompt was loaded. Defaults to `{}`. */
|
|
78
|
+
variables: PromptVariables;
|
|
91
79
|
|
|
92
80
|
/** General configuration for the LLM */
|
|
93
81
|
config: {
|
|
@@ -103,12 +91,37 @@ export type Prompt = {
|
|
|
103
91
|
/** Model name/identifier */
|
|
104
92
|
model: string;
|
|
105
93
|
|
|
106
|
-
/** Generation temperature
|
|
94
|
+
/** Generation temperature. Supported range and behavior vary by provider. */
|
|
107
95
|
temperature?: number;
|
|
108
96
|
|
|
109
|
-
/** Maximum number of tokens in the response */
|
|
97
|
+
/** Maximum number of tokens in the response. */
|
|
98
|
+
maxOutputTokens?: number;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Maximum number of tokens in the response.
|
|
102
|
+
*
|
|
103
|
+
* @deprecated Use `maxOutputTokens`.
|
|
104
|
+
*/
|
|
110
105
|
maxTokens?: number;
|
|
111
106
|
|
|
107
|
+
/** Nucleus sampling value. Usually set instead of `temperature`. */
|
|
108
|
+
topP?: number;
|
|
109
|
+
|
|
110
|
+
/** Limits sampling to the top K token choices. */
|
|
111
|
+
topK?: number;
|
|
112
|
+
|
|
113
|
+
/** Penalizes tokens that already appear in the prompt or generated output. */
|
|
114
|
+
presencePenalty?: number;
|
|
115
|
+
|
|
116
|
+
/** Penalizes tokens according to how often they appear. */
|
|
117
|
+
frequencyPenalty?: number;
|
|
118
|
+
|
|
119
|
+
/** Sequences that stop text generation when produced. */
|
|
120
|
+
stopSequences?: string[];
|
|
121
|
+
|
|
122
|
+
/** Tool-loop iterations when `stopWhen` is omitted. Always a positive integer after load (default 10). */
|
|
123
|
+
maxSteps: number;
|
|
124
|
+
|
|
112
125
|
/** Number of images to generate */
|
|
113
126
|
n?: number;
|
|
114
127
|
|
|
@@ -121,11 +134,11 @@ export type Prompt = {
|
|
|
121
134
|
/** Image aspect ratio, for example `16:9` */
|
|
122
135
|
aspectRatio?: `${number}:${number}`;
|
|
123
136
|
|
|
124
|
-
/** Random seed for deterministic image generation when supported */
|
|
137
|
+
/** Random seed for deterministic text or image generation when supported */
|
|
125
138
|
seed?: number;
|
|
126
139
|
|
|
127
|
-
/** Skill file or directory paths
|
|
128
|
-
skills
|
|
140
|
+
/** Skill file or directory paths relative to the prompt file. Always a `string[]` after load (`[]` if unset). */
|
|
141
|
+
skills: string[];
|
|
129
142
|
|
|
130
143
|
/**
|
|
131
144
|
* Provider-specific tools with configuration.
|
|
@@ -163,30 +176,14 @@ export type Prompt = {
|
|
|
163
176
|
/** Array of messages in the conversation */
|
|
164
177
|
messages: PromptMessage[];
|
|
165
178
|
|
|
166
|
-
/**
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
*
|
|
173
|
-
* Skills are declared in prompt frontmatter or passed inline to generation APIs.
|
|
174
|
-
*/
|
|
175
|
-
export type Skill = {
|
|
176
|
-
name: string;
|
|
177
|
-
description?: string;
|
|
178
|
-
instructions: string;
|
|
179
|
+
/**
|
|
180
|
+
* The whole trimmed prompt body when its first meaningful token is plain text. Always set after
|
|
181
|
+
* `loadPrompt`: a non-empty string with `messages: []` for instruction mode, or `null` when the
|
|
182
|
+
* body starts with markup and is parsed into messages.
|
|
183
|
+
*/
|
|
184
|
+
instructions: string | null;
|
|
179
185
|
};
|
|
180
186
|
|
|
181
|
-
/**
|
|
182
|
-
* The skills argument for async generation APIs. Either a static list or a function
|
|
183
|
-
* that receives the call input and may resolve skills asynchronously.
|
|
184
|
-
*/
|
|
185
|
-
export type SkillsArg<Input = unknown> = Skill[] |
|
|
186
|
-
( ( input: Input ) => Skill[] | Promise<Skill[]> );
|
|
187
|
-
|
|
188
|
-
/** Prompt-owned AI SDK fields supplied by Output prompt files. */
|
|
189
|
-
type PromptOwnedTextOptions = 'model' | 'messages' | 'prompt' | 'tools';
|
|
190
187
|
type AnyAiOutput = AIOutputNamespace.Output<unknown, unknown, unknown>;
|
|
191
188
|
type CompatibleToolFunction = ( ...args: never[] ) => unknown | PromiseLike<unknown>;
|
|
192
189
|
type CompatibleApprovalFunction = ( ...args: never[] ) => boolean | PromiseLike<boolean>;
|
|
@@ -217,165 +214,141 @@ export type CompatibleTool = {
|
|
|
217
214
|
/** AI SDK tools accepted by Output APIs without requiring one exact Zod peer instance. */
|
|
218
215
|
export type CompatibleToolSet = Record<string, CompatibleTool>;
|
|
219
216
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
* `tools` is accepted separately as {@link CompatibleToolSet} to support third-party tool packages.
|
|
223
|
-
*
|
|
224
|
-
* @typeParam Tools - The tools available for the model to call
|
|
225
|
-
*/
|
|
226
|
-
export type GenerateTextAiSdkOptions<
|
|
227
|
-
Tools extends ToolSet = ToolSet,
|
|
228
|
-
OutputSpec extends AnyAiOutput = AnyAiOutput
|
|
229
|
-
> = Omit<Parameters<typeof aiGenerateText<Tools, OutputSpec>>[0], PromptOwnedTextOptions>;
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* AI SDK options accepted by streamText, with prompt-owned fields supplied by Output prompt files.
|
|
233
|
-
* `tools` is accepted separately as {@link CompatibleToolSet} to support third-party tool packages.
|
|
234
|
-
*
|
|
235
|
-
* @typeParam Tools - The tools available for the model to call
|
|
236
|
-
*/
|
|
237
|
-
export type StreamTextAiSdkOptions<
|
|
238
|
-
Tools extends ToolSet = ToolSet,
|
|
239
|
-
OutputSpec extends AnyAiOutput = AnyAiOutput
|
|
240
|
-
> = Omit<Parameters<typeof aiStreamText<Tools, OutputSpec>>[0], PromptOwnedTextOptions>;
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* AI SDK options specific to generateImage.
|
|
244
|
-
*
|
|
245
|
-
* `model` and `prompt` are omitted because Output supplies them from the prompt file.
|
|
246
|
-
*/
|
|
247
|
-
export type GenerateImageAiSdkOptions = Omit<Parameters<typeof aiGenerateImage>[0], 'model' | 'prompt'>;
|
|
248
|
-
type GenerateImagePrompt = Parameters<typeof aiGenerateImage>[0]['prompt'];
|
|
249
|
-
type GenerateImagePromptWithImages = Exclude<GenerateImagePrompt, string>;
|
|
250
|
-
type GenerateImageInput = GenerateImagePromptWithImages['images'][number];
|
|
251
|
-
|
|
252
|
-
/** Agent {@link Agent.stream} options: same as AI SDK plus wrapped `onFinish` (adds `cost`). */
|
|
253
|
-
export type OutputAgentStreamParameters = Omit<AgentStreamParameters<never, ToolSet>, 'onFinish' | 'tools'> & {
|
|
254
|
-
tools?: CompatibleToolSet;
|
|
255
|
-
onFinish?: WrappedStreamTextOnFinishCallback<ToolSet>;
|
|
256
|
-
};
|
|
257
|
-
|
|
258
|
-
/** Agent constructor options, with prompt-owned model/instructions/tools supplied by Output prompt files and skills. */
|
|
259
|
-
export type OutputAgentConstructorParameters<
|
|
260
|
-
OutputSpec extends AnyAiOutput = AnyAiOutput
|
|
261
|
-
> = Omit<ConstructorParameters<typeof AIToolLoopAgent>[0], 'model' | 'instructions' | 'tools' | 'output'> & {
|
|
262
|
-
/** Prompt file name (e.g. 'my_agent@v1') */
|
|
217
|
+
type PromptFileCallOptions = {
|
|
218
|
+
/** Prompt file name (e.g. 'summary@v1') */
|
|
263
219
|
prompt: string;
|
|
220
|
+
/** Variables to interpolate into the prompt file */
|
|
221
|
+
variables?: PromptVariables;
|
|
264
222
|
/** Override the stack-resolved prompt directory */
|
|
265
223
|
promptDir?: string;
|
|
266
|
-
/** Variables to render the prompt template at construction time */
|
|
267
|
-
variables?: Record<string, unknown>;
|
|
268
|
-
/** Structured output specification */
|
|
269
|
-
output?: OutputSpec;
|
|
270
|
-
/** Static skill packages made available to the LLM */
|
|
271
|
-
skills?: Skill[];
|
|
272
|
-
/** AI SDK tools available during the reasoning loop */
|
|
273
|
-
tools?: CompatibleToolSet;
|
|
274
|
-
/** Maximum tool-loop iterations when stopWhen is not specified (default: 10) */
|
|
275
|
-
maxSteps?: number;
|
|
276
|
-
/** Pluggable conversation store — opt-in, stateless by default */
|
|
277
|
-
conversationStore?: ConversationStore;
|
|
278
224
|
};
|
|
279
225
|
|
|
280
|
-
|
|
281
|
-
|
|
226
|
+
type StopWhen<Tools extends ToolSet = ToolSet> =
|
|
227
|
+
StopCondition<NoInfer<Tools>> | Array<StopCondition<NoInfer<Tools>>>;
|
|
228
|
+
|
|
229
|
+
type TextCallOptions<
|
|
230
|
+
Tools extends ToolSet = ToolSet,
|
|
231
|
+
OutputSpec extends AnyAiOutput = AnyAiOutput
|
|
232
|
+
> = PromptFileCallOptions & {
|
|
233
|
+
/** AI SDK tools, accepted structurally to tolerate different Zod peer versions. */
|
|
282
234
|
tools?: CompatibleToolSet;
|
|
235
|
+
/** Structured output specification */
|
|
236
|
+
output?: OutputSpec;
|
|
237
|
+
/** Tool choice, applied only when tools exist */
|
|
238
|
+
toolChoice?: ToolChoice<Tools>;
|
|
239
|
+
/** Caller stop condition; otherwise prompt `maxSteps` when tools exist */
|
|
240
|
+
stopWhen?: StopWhen<Tools>;
|
|
241
|
+
/** Abort signal for the request */
|
|
242
|
+
abortSignal?: AbortSignal;
|
|
283
243
|
};
|
|
284
244
|
|
|
285
245
|
/** Parameters accepted by {@link generateText}. */
|
|
286
246
|
export type GenerateTextParameters<
|
|
287
247
|
Tools extends ToolSet = ToolSet,
|
|
288
248
|
OutputSpec extends AnyAiOutput = AnyAiOutput
|
|
289
|
-
> =
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
maxSteps?: number;
|
|
300
|
-
/** AI SDK tools, accepted structurally to tolerate different Zod peer versions. */
|
|
301
|
-
tools?: CompatibleToolSet;
|
|
302
|
-
} & GenerateTextAiSdkOptions<Tools, OutputSpec>;
|
|
249
|
+
> = TextCallOptions<Tools, OutputSpec>;
|
|
250
|
+
|
|
251
|
+
/** Parameters accepted by {@link generateTextWithStreaming}. */
|
|
252
|
+
export type GenerateTextWithStreamingParameters<
|
|
253
|
+
Tools extends ToolSet = ToolSet,
|
|
254
|
+
OutputSpec extends AnyAiOutput = AnyAiOutput
|
|
255
|
+
> = TextCallOptions<Tools, OutputSpec> & {
|
|
256
|
+
/** Callback for each streamed chunk */
|
|
257
|
+
onChunk?: StreamTextOnChunkCallback<Tools>;
|
|
258
|
+
};
|
|
303
259
|
|
|
304
260
|
/** Parameters accepted by {@link streamText}. */
|
|
305
261
|
export type StreamTextParameters<
|
|
306
262
|
Tools extends ToolSet = ToolSet,
|
|
307
263
|
OutputSpec extends AnyAiOutput = AnyAiOutput
|
|
308
|
-
> = {
|
|
309
|
-
/**
|
|
310
|
-
|
|
311
|
-
/**
|
|
312
|
-
|
|
313
|
-
/**
|
|
314
|
-
promptDir?: string;
|
|
315
|
-
/** Skill packages to provide to the LLM through the `load_skill` tool. Function resolvers must be synchronous. */
|
|
316
|
-
skills?: Skill[] | ( ( input: Record<string, string | number | boolean> | undefined ) => Skill[] );
|
|
317
|
-
/** Used to create a default `stepCountIs(maxSteps)` when tools are present and `stopWhen` is omitted */
|
|
318
|
-
maxSteps?: number;
|
|
319
|
-
/** AI SDK tools, accepted structurally to tolerate different Zod peer versions. */
|
|
320
|
-
tools?: CompatibleToolSet;
|
|
321
|
-
/** Callback when stream finishes. Receives the wrapped event with optional `cost`. */
|
|
264
|
+
> = TextCallOptions<Tools, OutputSpec> & {
|
|
265
|
+
/** Callback for each streamed chunk */
|
|
266
|
+
onChunk?: StreamTextOnChunkCallback<Tools>;
|
|
267
|
+
/** Callback when a stream error occurs */
|
|
268
|
+
onError?: StreamTextOnErrorCallback;
|
|
269
|
+
/** Callback when stream finishes. Receives the wrapped event with `result`, `cost`, and `sources`. */
|
|
322
270
|
onFinish?: WrappedStreamTextOnFinishCallback<Tools>;
|
|
323
|
-
}
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
/** Runtime image bytes or an object with optional media type. */
|
|
274
|
+
export type GenerateImageInput =
|
|
275
|
+
| Buffer |
|
|
276
|
+
Uint8Array |
|
|
277
|
+
ArrayBuffer |
|
|
278
|
+
string |
|
|
279
|
+
{
|
|
280
|
+
data: Buffer | Uint8Array | ArrayBuffer | string;
|
|
281
|
+
mediaType?: string;
|
|
282
|
+
};
|
|
324
283
|
|
|
325
284
|
/** Parameters accepted by {@link generateImage}. */
|
|
326
|
-
export type GenerateImageParameters = {
|
|
327
|
-
/** Prompt file name */
|
|
328
|
-
prompt: string;
|
|
329
|
-
/** Variables to interpolate into the prompt file */
|
|
330
|
-
variables?: Record<string, string | number | boolean>;
|
|
331
|
-
/** Override the stack-resolved prompt directory */
|
|
332
|
-
promptDir?: string;
|
|
285
|
+
export type GenerateImageParameters = PromptFileCallOptions & {
|
|
333
286
|
/** Runtime image inputs for image-to-image generation */
|
|
334
287
|
images?: GenerateImageInput[];
|
|
335
|
-
/** Optional mask for image editing */
|
|
336
|
-
mask?:
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
/** A source extracted from search tool results during multi-step LLM execution. */
|
|
340
|
-
export type ExtractedSource = {
|
|
341
|
-
type: 'source';
|
|
342
|
-
sourceType: 'url';
|
|
343
|
-
id: string;
|
|
344
|
-
url: string;
|
|
345
|
-
title: string;
|
|
288
|
+
/** Optional mask for image editing; requires `images` */
|
|
289
|
+
mask?: GenerateImageInput;
|
|
290
|
+
/** Abort signal for the request */
|
|
291
|
+
abortSignal?: AbortSignal;
|
|
346
292
|
};
|
|
347
293
|
|
|
348
|
-
/**
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
294
|
+
/** Agent constructor options. */
|
|
295
|
+
export type OutputAgentConstructorParameters<
|
|
296
|
+
OutputSpec extends AnyAiOutput = AnyAiOutput
|
|
297
|
+
> = PromptFileCallOptions & {
|
|
298
|
+
/** Structured output specification */
|
|
299
|
+
output?: OutputSpec;
|
|
300
|
+
/** AI SDK tools available during the reasoning loop */
|
|
301
|
+
tools?: CompatibleToolSet;
|
|
302
|
+
/** Caller stop condition; otherwise prompt `maxSteps` when tools exist */
|
|
303
|
+
stopWhen?: StopWhen;
|
|
304
|
+
/** Pluggable message store. Opt-in; stateless by default. */
|
|
305
|
+
messageStore?: MessageStore;
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
/** Agent {@link Agent.generate} options. */
|
|
309
|
+
export type OutputAgentGenerateParameters = {
|
|
310
|
+
messages?: ModelMessage[];
|
|
311
|
+
abortSignal?: AbortSignal;
|
|
312
|
+
toolChoice?: ToolChoice<ToolSet>;
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
/** Agent {@link Agent.generateWithStreaming} options. Completion is the returned promise; use `onChunk` for progress. */
|
|
316
|
+
export type OutputAgentGenerateWithStreamingParameters = OutputAgentGenerateParameters & {
|
|
317
|
+
onChunk?: StreamTextOnChunkCallback<ToolSet>;
|
|
358
318
|
};
|
|
359
319
|
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
type: string;
|
|
365
|
-
ppm: number;
|
|
366
|
-
amount: number;
|
|
367
|
-
total: number;
|
|
368
|
-
}>;
|
|
369
|
-
total: number;
|
|
370
|
-
tokensUsed: number;
|
|
320
|
+
/** Agent {@link Agent.stream} options. `onFinish` receives {@link WrappedStreamTextOnFinishEvent} (`result`, `cost`, `sources`). */
|
|
321
|
+
export type OutputAgentStreamParameters = OutputAgentGenerateWithStreamingParameters & {
|
|
322
|
+
onFinish?: WrappedStreamTextOnFinishCallback<ToolSet>;
|
|
323
|
+
onError?: StreamTextOnErrorCallback;
|
|
371
324
|
};
|
|
372
325
|
|
|
326
|
+
/**
|
|
327
|
+
* One entry of `generateText().sources` after merge (tool URLs plus provider sources).
|
|
328
|
+
* Same item type as AI SDK `GenerateTextResult['sources']` (`sourceType: 'url' | 'document'`).
|
|
329
|
+
* `ai` does not export a named `Source` type.
|
|
330
|
+
*/
|
|
331
|
+
export type ExtractedSource = AIGenerateTextResult<ToolSet, AnyAiOutput>['sources'][number];
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Cost on a wrapped LLM response (`response.cost`, stream `onFinish` `cost`) and the
|
|
335
|
+
* `cost:llm:request` payload. This is a `Tracing.Attribute.LLMUsage` instance.
|
|
336
|
+
* `calculateLLMCallCost` returns it, or `null` when pricing data is missing.
|
|
337
|
+
*/
|
|
338
|
+
export type LLMCallCost = InstanceType<( typeof Tracing.Attribute )['LLMUsage']>;
|
|
339
|
+
|
|
340
|
+
export type LLMUsageEvent = LLMCallCost;
|
|
341
|
+
|
|
373
342
|
/**
|
|
374
343
|
* `streamText` and agent `stream` `onFinish` event after the stream response wrapper: same as the AI SDK
|
|
375
|
-
* finish payload plus
|
|
344
|
+
* finish payload plus `result`, `cost`, and merged `sources`.
|
|
376
345
|
*/
|
|
377
346
|
export type WrappedStreamTextOnFinishEvent<Tools extends ToolSet = ToolSet> =
|
|
378
|
-
Parameters<StreamTextOnFinishCallback<Tools>>[0] & {
|
|
347
|
+
Parameters<StreamTextOnFinishCallback<Tools>>[0] & {
|
|
348
|
+
result: string;
|
|
349
|
+
cost: LLMCallCost | null;
|
|
350
|
+
sources: ExtractedSource[];
|
|
351
|
+
};
|
|
379
352
|
|
|
380
353
|
export type WrappedStreamTextOnFinishCallback<Tools extends ToolSet = ToolSet> = (
|
|
381
354
|
event: WrappedStreamTextOnFinishEvent<Tools>
|
|
@@ -392,18 +365,24 @@ export type GenerateTextResult<
|
|
|
392
365
|
> = AIGenerateTextResult<Tools, OutputSpec> & {
|
|
393
366
|
/** Unified field name alias for 'text' */
|
|
394
367
|
result: string;
|
|
395
|
-
/** Calculated cost
|
|
396
|
-
cost
|
|
397
|
-
/**
|
|
368
|
+
/** Calculated cost for the LLM call; `null` when pricing data is not available */
|
|
369
|
+
cost: LLMCallCost | null;
|
|
370
|
+
/** Merged tool + provider sources (url and document). Always an array. */
|
|
398
371
|
sources: ExtractedSource[];
|
|
399
372
|
};
|
|
400
373
|
|
|
374
|
+
/** Completed result from {@link generateTextWithStreaming}. */
|
|
375
|
+
export type GenerateTextWithStreamingResult<
|
|
376
|
+
Tools extends ToolSet = ToolSet,
|
|
377
|
+
OutputSpec extends AnyAiOutput = AnyAiOutput
|
|
378
|
+
> = Omit<GenerateTextResult<Tools, OutputSpec>, 'experimental_output'>;
|
|
379
|
+
|
|
401
380
|
/** Result from generateImage including a unified `result` field pointing at the first image. */
|
|
402
381
|
export type GenerateImageResult = AIGenerateImageResult & {
|
|
403
382
|
/** Unified field name alias for `image` */
|
|
404
383
|
result: AIGenerateImageResult['image'];
|
|
405
|
-
/** Calculated cost for the image generation call when pricing data is available. */
|
|
406
|
-
cost
|
|
384
|
+
/** Calculated cost for the image generation call; `null` when pricing data is not available. */
|
|
385
|
+
cost: LLMCallCost | null;
|
|
407
386
|
};
|
|
408
387
|
|
|
409
388
|
/**
|
|
@@ -415,7 +394,7 @@ export type GenerateImageResult = AIGenerateImageResult & {
|
|
|
415
394
|
*/
|
|
416
395
|
export function loadPrompt(
|
|
417
396
|
name: string,
|
|
418
|
-
variables?:
|
|
397
|
+
variables?: PromptVariables,
|
|
419
398
|
promptDir?: string
|
|
420
399
|
): Prompt;
|
|
421
400
|
|
|
@@ -449,10 +428,9 @@ export function getProviderNames(): string[];
|
|
|
449
428
|
* Use an LLM model to generate text.
|
|
450
429
|
*
|
|
451
430
|
* This function is a wrapper over the AI SDK's `generateText`.
|
|
452
|
-
* The prompt file sets `model`, `messages`, `
|
|
453
|
-
*
|
|
454
|
-
* `
|
|
455
|
-
* structured output, callbacks, retries, and sampling settings.
|
|
431
|
+
* The prompt file sets `model`, `messages`, generation settings, `maxSteps`, `skills`, and
|
|
432
|
+
* `providerOptions`. Call arguments are `prompt`, `promptDir`, `variables`, `tools`, `output`,
|
|
433
|
+
* `toolChoice`, `stopWhen`, and `abortSignal`.
|
|
456
434
|
*
|
|
457
435
|
* @param args - Generation arguments. See {@link GenerateTextParameters}.
|
|
458
436
|
* @returns AI SDK response with text and metadata.
|
|
@@ -464,14 +442,30 @@ export function generateText<
|
|
|
464
442
|
args: GenerateTextParameters<Tools, OutputSpec>
|
|
465
443
|
): Promise<GenerateTextResult<Tools, OutputSpec>>;
|
|
466
444
|
|
|
445
|
+
/**
|
|
446
|
+
* Generate text over streaming transport and return a completed response.
|
|
447
|
+
*
|
|
448
|
+
* The stream is consumed internally. `onChunk` runs as parts arrive. Provider or
|
|
449
|
+
* transport errors reject the returned promise with the mapped error. Use {@link streamText}
|
|
450
|
+
* when you need `onFinish` / `onError` stream observers.
|
|
451
|
+
*
|
|
452
|
+
* @param args - Streaming arguments. See {@link GenerateTextWithStreamingParameters}.
|
|
453
|
+
* @returns Completed response with parsed output and generateText-compatible metadata.
|
|
454
|
+
*/
|
|
455
|
+
export function generateTextWithStreaming<
|
|
456
|
+
Tools extends ToolSet = ToolSet,
|
|
457
|
+
OutputSpec extends AnyAiOutput = AnyAiOutput
|
|
458
|
+
>(
|
|
459
|
+
args: GenerateTextWithStreamingParameters<Tools, OutputSpec>
|
|
460
|
+
): Promise<GenerateTextWithStreamingResult<Tools, OutputSpec>>;
|
|
461
|
+
|
|
467
462
|
/**
|
|
468
463
|
* Use an LLM model to stream text generation.
|
|
469
464
|
*
|
|
470
465
|
* This function is a wrapper over the AI SDK's `streamText`.
|
|
471
|
-
* The prompt file sets `model`, `messages`, `
|
|
472
|
-
*
|
|
473
|
-
* `
|
|
474
|
-
* Output wraps to add optional cost data.
|
|
466
|
+
* The prompt file sets `model`, `messages`, generation settings, `maxSteps`, `skills`, and
|
|
467
|
+
* `providerOptions`. Call arguments match {@link generateText}, plus `onChunk`, `onFinish`, and
|
|
468
|
+
* `onError`. `onFinish` is wrapped to add `result`, `cost`, and merged `sources`.
|
|
475
469
|
*
|
|
476
470
|
* @param args - Streaming arguments. See {@link StreamTextParameters}.
|
|
477
471
|
* @returns AI SDK stream result with textStream, fullStream, and metadata promises.
|
|
@@ -486,9 +480,9 @@ export function streamText<
|
|
|
486
480
|
/**
|
|
487
481
|
* Use an image model to generate images from a prompt file.
|
|
488
482
|
*
|
|
489
|
-
* The prompt file supplies
|
|
490
|
-
*
|
|
491
|
-
* `
|
|
483
|
+
* The prompt file supplies `model`, instructions, `n`, `size`, `aspectRatio`, `seed`,
|
|
484
|
+
* `maxImagesPerCall`, and `providerOptions`. Call arguments are `prompt`, `promptDir`, `variables`,
|
|
485
|
+
* `images`, `mask`, and `abortSignal`.
|
|
492
486
|
*
|
|
493
487
|
* @param args - Image generation arguments. See {@link GenerateImageParameters}.
|
|
494
488
|
* @returns AI SDK image response with `result` aliasing the first image.
|
|
@@ -497,52 +491,29 @@ export function generateImage(
|
|
|
497
491
|
args: GenerateImageParameters
|
|
498
492
|
): Promise<GenerateImageResult>;
|
|
499
493
|
|
|
500
|
-
/**
|
|
501
|
-
|
|
502
|
-
*
|
|
503
|
-
* @example
|
|
504
|
-
* ```ts
|
|
505
|
-
* const researchSkill = skill( {
|
|
506
|
-
* name: 'web_research',
|
|
507
|
-
* description: 'Search and synthesize web information',
|
|
508
|
-
* instructions: '# Web Research\n1. Break into queries\n2. Search\n3. Cite sources'
|
|
509
|
-
* } );
|
|
510
|
-
* ```
|
|
511
|
-
*/
|
|
512
|
-
export function skill( params: {
|
|
513
|
-
name: string;
|
|
514
|
-
description?: string;
|
|
515
|
-
instructions: string;
|
|
516
|
-
} ): Skill;
|
|
517
|
-
|
|
518
|
-
/** Pluggable conversation store for multi-turn Agent interactions. */
|
|
519
|
-
export interface ConversationStore {
|
|
494
|
+
/** Pluggable message store for multi-turn Agent interactions. */
|
|
495
|
+
export interface MessageStore {
|
|
520
496
|
getMessages(): ModelMessage[] | Promise<ModelMessage[]>;
|
|
521
497
|
addMessages( messages: ModelMessage[] ): void | Promise<void>;
|
|
522
498
|
}
|
|
523
499
|
|
|
524
|
-
/** Create an in-memory conversation store backed by a closure array. */
|
|
525
|
-
export function createMemoryConversationStore(): ConversationStore;
|
|
526
|
-
|
|
527
500
|
/**
|
|
528
|
-
* Agent extends AI SDK's ToolLoopAgent with Output.ai prompt file rendering
|
|
529
|
-
* and the skill system.
|
|
501
|
+
* Agent extends AI SDK's ToolLoopAgent with Output.ai prompt file rendering.
|
|
530
502
|
*
|
|
531
|
-
* @example Workflow step
|
|
503
|
+
* @example Workflow step - variables per call, stateless
|
|
532
504
|
* ```ts
|
|
533
505
|
* const reviewer = new Agent({
|
|
534
506
|
* prompt: 'reviewer@v1',
|
|
535
|
-
* output: Output.object({ schema: z.object({ summary: z.string() }) })
|
|
536
|
-
* maxSteps: 5
|
|
507
|
+
* output: aiSdk.Output.object({ schema: z.object({ summary: z.string() }) })
|
|
537
508
|
* });
|
|
538
509
|
* const result = await reviewer.generate();
|
|
539
510
|
* ```
|
|
540
511
|
*
|
|
541
|
-
* @example Interactive
|
|
512
|
+
* @example Interactive - fixed setup, message history
|
|
542
513
|
* ```ts
|
|
543
514
|
* const chatbot = new Agent({
|
|
544
515
|
* prompt: 'chatbot@v1',
|
|
545
|
-
*
|
|
516
|
+
* messageStore: { getMessages() { return []; }, addMessages() {} }
|
|
546
517
|
* });
|
|
547
518
|
* const r1 = await chatbot.generate({ messages: [{ role: 'user', content: 'Hello' }] });
|
|
548
519
|
* ```
|
|
@@ -554,13 +525,22 @@ export declare class Agent<
|
|
|
554
525
|
|
|
555
526
|
/**
|
|
556
527
|
* Run the agent and return when complete.
|
|
557
|
-
* Same augmented shape as {@link generateText}: `result`,
|
|
528
|
+
* Same augmented shape as {@link generateText}: `result`, `cost`, merged `sources`.
|
|
558
529
|
*/
|
|
559
530
|
generate( options?: OutputAgentGenerateParameters ): Promise<GenerateTextResult<ToolSet, OutputSpec>>;
|
|
560
531
|
|
|
532
|
+
/**
|
|
533
|
+
* Run the agent over streaming transport and return a completed response.
|
|
534
|
+
* `onChunk` runs as parts arrive. Provider and transport errors reject with the mapped error.
|
|
535
|
+
* Use {@link Agent.stream} when you need `onFinish` / `onError` stream observers.
|
|
536
|
+
*/
|
|
537
|
+
generateWithStreaming(
|
|
538
|
+
options?: OutputAgentGenerateWithStreamingParameters
|
|
539
|
+
): Promise<GenerateTextWithStreamingResult<ToolSet, OutputSpec>>;
|
|
540
|
+
|
|
561
541
|
/**
|
|
562
542
|
* Stream the agent's response.
|
|
563
|
-
* `onFinish` receives {@link WrappedStreamTextOnFinishEvent} (`cost`
|
|
543
|
+
* `onFinish` receives {@link WrappedStreamTextOnFinishEvent} (`result`, `cost`, `sources`), matching {@link streamText}.
|
|
564
544
|
*/
|
|
565
545
|
stream( options?: OutputAgentStreamParameters ): Promise<
|
|
566
546
|
AIStreamTextResult<ToolSet, OutputSpec>
|
package/src/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
export { generateText, streamText, generateImage } from './
|
|
2
|
-
export { Agent
|
|
1
|
+
export { generateText, generateTextWithStreaming, streamText, generateImage } from './generate.js';
|
|
2
|
+
export { Agent } from './agent.js';
|
|
3
3
|
export { loadPrompt } from './prompt/loader.js';
|
|
4
4
|
export { registerProvider, getProviderNames } from './ai_provider.js';
|
|
5
|
-
export
|
|
6
|
-
export * as ai from 'ai';
|
|
5
|
+
export * as aiSdk from 'ai';
|