@statelyai/agent 1.1.5 → 2.0.0-alpha.5
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/LICENSE +21 -0
- package/dist/ai-sdk.cjs +249 -0
- package/dist/ai-sdk.d.cts +168 -0
- package/dist/ai-sdk.d.mts +168 -0
- package/dist/ai-sdk.mjs +241 -0
- package/dist/cli.cjs +63 -0
- package/dist/cli.d.cts +1 -0
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +64 -0
- package/dist/decision-FTmbqSEe.mjs +938 -0
- package/dist/decision-pC-bY2DE.cjs +1231 -0
- package/dist/index.cjs +54 -0
- package/dist/index.d.cts +1217 -0
- package/dist/index.d.mts +1194 -405
- package/dist/index.mjs +3 -583
- package/dist/openai-compat.cjs +319 -0
- package/dist/openai-compat.d.cts +98 -0
- package/dist/openai-compat.d.mts +98 -0
- package/dist/openai-compat.mjs +312 -0
- package/dist/src-CjpHDU8F.mjs +2445 -0
- package/dist/src-DcRsWPfV.cjs +2564 -0
- package/dist/text-logic-1ZQkO3zr.d.cts +682 -0
- package/dist/text-logic-2EMJIS-n.d.mts +682 -0
- package/dist/types-BHjeDdch.d.cts +208 -0
- package/dist/types-Cq1YlAQ6.d.mts +208 -0
- package/dist/utils-CWUCa3pF.d.mts +108 -0
- package/dist/utils-lK1wnL2i.d.cts +108 -0
- package/dist/zod.cjs +31 -0
- package/dist/zod.d.cts +30 -0
- package/dist/zod.d.mts +30 -0
- package/dist/zod.mjs +30 -0
- package/package.json +110 -29
- package/readme.md +144 -6
- package/schemas/agent-workflow.json +527 -0
- package/.changeset/README.md +0 -8
- package/.changeset/config.json +0 -11
- package/.env.template +0 -3
- package/.github/actions/ci-setup/action.yml +0 -24
- package/.github/workflows/release.yml +0 -46
- package/.vscode/launch.json +0 -28
- package/CHANGELOG.md +0 -215
- package/dist/index.d.ts +0 -428
- package/dist/index.js +0 -616
- package/examples/chatbot.ts +0 -71
- package/examples/cot.ts +0 -89
- package/examples/email.ts +0 -118
- package/examples/example.ts +0 -81
- package/examples/goal.ts +0 -94
- package/examples/helpers/helpers.ts +0 -17
- package/examples/helpers/loader.ts +0 -32
- package/examples/helpers/runner.ts +0 -27
- package/examples/joke.ts +0 -225
- package/examples/multi.ts +0 -103
- package/examples/newspaper.ts +0 -324
- package/examples/number.ts +0 -102
- package/examples/raffle.ts +0 -105
- package/examples/sandbox.ts +0 -28
- package/examples/simple.ts +0 -39
- package/examples/support.ts +0 -147
- package/examples/ticTacToe.ts +0 -224
- package/examples/todo.ts +0 -137
- package/examples/tutor.ts +0 -100
- package/examples/verify.ts +0 -120
- package/examples/weather.ts +0 -178
- package/examples/wiki.ts +0 -30
- package/examples/word.ts +0 -171
- package/src/adapters/vercel.ts +0 -7
- package/src/agent-experimental.ts +0 -221
- package/src/agent.test.ts +0 -506
- package/src/agent.ts +0 -300
- package/src/decision.test.ts +0 -179
- package/src/decision.ts +0 -84
- package/src/index.ts +0 -4
- package/src/memory.ts +0 -25
- package/src/planners/shortestPathPlanner.ts +0 -22
- package/src/planners/simplePlanner.ts +0 -139
- package/src/schemas.ts +0 -11
- package/src/strategies/chain-of-note.ts +0 -155
- package/src/templates/defaultText.ts +0 -18
- package/src/text.ts +0 -236
- package/src/types.ts +0 -499
- package/src/utils.ts +0 -72
- package/tsconfig.json +0 -109
- package/vitest.config.ts +0 -9
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License Copyright (c) 2025 Stately Software, Inc.
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted,
|
|
4
|
+
free of charge, to any person obtaining a copy of this software and associated
|
|
5
|
+
documentation files (the "Software"), to deal in the Software without
|
|
6
|
+
restriction, including without limitation the rights to use, copy, modify, merge,
|
|
7
|
+
publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to the
|
|
9
|
+
following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice
|
|
12
|
+
(including the next paragraph) shall be included in all copies or substantial
|
|
13
|
+
portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
|
16
|
+
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
|
|
18
|
+
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
19
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
20
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/dist/ai-sdk.cjs
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_decision = require("./decision-pC-bY2DE.cjs");
|
|
3
|
+
let ai = require("ai");
|
|
4
|
+
//#region src/ai-sdk/index.ts
|
|
5
|
+
/**
|
|
6
|
+
* Maps an {@link AgentTools} map onto AI SDK `tool()` definitions. A tool that
|
|
7
|
+
* already carries its own Standard Schema `inputSchema` — an AI SDK
|
|
8
|
+
* `tool({...})`, or any equivalent descriptor — is passed through **unchanged**,
|
|
9
|
+
* so the SDK applies its own validation, calls `execute(input, options)`, and
|
|
10
|
+
* every extra property (`providerOptions`, `toModelOutput`, …) survives. A bare
|
|
11
|
+
* `AgentToolExecute` function becomes a tool with an unconstrained input schema;
|
|
12
|
+
* a minimal descriptor with no readable schema is converted with a permissive
|
|
13
|
+
* fallback. Used internally by {@link toAiSdkCallSettings}; exported for callers
|
|
14
|
+
* building AI SDK calls by hand outside `createAiSdkExecutors`.
|
|
15
|
+
*/
|
|
16
|
+
function toAiSdkTools(tools) {
|
|
17
|
+
const entries = [];
|
|
18
|
+
for (const [name, descriptor] of Object.entries(tools)) {
|
|
19
|
+
if (!descriptor) continue;
|
|
20
|
+
if (typeof descriptor === "function") {
|
|
21
|
+
entries.push([name, (0, ai.tool)({
|
|
22
|
+
inputSchema: unknownSchema,
|
|
23
|
+
execute: (input) => descriptor(input)
|
|
24
|
+
})]);
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
if (require_decision.isStandardSchema(descriptor.inputSchema)) {
|
|
28
|
+
entries.push([name, descriptor]);
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
const inputSchema = descriptor.inputSchema ?? descriptor.schemas?.input;
|
|
32
|
+
const toolOptions = {
|
|
33
|
+
description: descriptor.description,
|
|
34
|
+
inputSchema: inputSchema ? inputSchema : unknownSchema
|
|
35
|
+
};
|
|
36
|
+
if (descriptor.execute) {
|
|
37
|
+
entries.push([name, (0, ai.tool)({
|
|
38
|
+
...toolOptions,
|
|
39
|
+
execute: (input) => descriptor.execute?.(input)
|
|
40
|
+
})]);
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
entries.push([name, (0, ai.tool)(toolOptions)]);
|
|
44
|
+
}
|
|
45
|
+
return Object.fromEntries(entries);
|
|
46
|
+
}
|
|
47
|
+
function maxStepsSetting(request) {
|
|
48
|
+
return typeof request.metadata?.maxSteps === "number" ? { stopWhen: (0, ai.stepCountIs)(request.metadata.maxSteps) } : {};
|
|
49
|
+
}
|
|
50
|
+
const unknownSchema = { "~standard": {
|
|
51
|
+
version: 1,
|
|
52
|
+
vendor: "statelyai-agent",
|
|
53
|
+
validate: (value) => ({ value }),
|
|
54
|
+
jsonSchema: { input: () => ({}) }
|
|
55
|
+
} };
|
|
56
|
+
/**
|
|
57
|
+
* Identity helper for a `models` map whose value is exported. Returns the map
|
|
58
|
+
* unchanged, but types it as {@link AiSdkModelMap}`<keyof T & string>` — a
|
|
59
|
+
* portable, nameable type — so an exported `const models = defineModels({...})`
|
|
60
|
+
* needs no `Record<'a' | 'b', LanguageModel>` annotation and never triggers
|
|
61
|
+
* TS2742 ("inferred type cannot be named without a reference to …"). The exact
|
|
62
|
+
* ref keys survive, so `createAiSdkExecutors({ models })` and
|
|
63
|
+
* `setupAgent({ models })` still infer/autocomplete them.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* export const models = defineModels({
|
|
68
|
+
* quick: openai('gpt-5.4-mini'),
|
|
69
|
+
* deep: openai('gpt-5.4'),
|
|
70
|
+
* });
|
|
71
|
+
* // typeof models === AiSdkModelMap<'quick' | 'deep'>
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
function defineModels(models) {
|
|
75
|
+
return models;
|
|
76
|
+
}
|
|
77
|
+
function resolveAiSdkModel(options, modelRef) {
|
|
78
|
+
if (options.resolveModel) return options.resolveModel(modelRef);
|
|
79
|
+
const models = options.models;
|
|
80
|
+
if (!models) throw new Error(`createAiSdkExecutors: no model resolver configured for '${modelRef}'.`);
|
|
81
|
+
const model = models[modelRef];
|
|
82
|
+
if (!model) throw new Error(`createAiSdkExecutors: unknown model '${modelRef}'.`);
|
|
83
|
+
return model;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* AI SDK request-mapping settings shared by `generateText`/`streamText`.
|
|
87
|
+
* `AgentTextRequest.messages` (`AgentMessage[]`) and AI SDK's `ModelMessage[]`
|
|
88
|
+
* are structurally compatible by design (§1 of docs/p0-design.md) — the cast
|
|
89
|
+
* below is a typed identity mapping, not a semantic conversion.
|
|
90
|
+
*/
|
|
91
|
+
function toAiSdkCallSettings(request) {
|
|
92
|
+
const messages = request.messages;
|
|
93
|
+
return {
|
|
94
|
+
system: request.system,
|
|
95
|
+
...messages ? { messages } : { prompt: request.prompt ?? "" },
|
|
96
|
+
temperature: request.temperature,
|
|
97
|
+
maxOutputTokens: request.maxOutputTokens,
|
|
98
|
+
topP: request.topP,
|
|
99
|
+
topK: request.topK,
|
|
100
|
+
seed: request.seed,
|
|
101
|
+
stopSequences: request.stopSequences,
|
|
102
|
+
tools: request.tools ? toAiSdkTools(request.tools) : void 0,
|
|
103
|
+
toolChoice: toAiSdkToolChoice(request.toolChoice)
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/** Maps an {@link AgentToolChoice} to AI SDK's tool-choice shape — `{ type: 'tool'; name }` becomes `{ type: 'tool'; toolName }`; `'auto'`/`'none'`/`'required'`/`undefined` pass through unchanged. */
|
|
107
|
+
function toAiSdkToolChoice(toolChoice) {
|
|
108
|
+
return typeof toolChoice === "object" ? {
|
|
109
|
+
type: "tool",
|
|
110
|
+
toolName: toolChoice.name
|
|
111
|
+
} : toolChoice;
|
|
112
|
+
}
|
|
113
|
+
/** `true` when the request should use AI SDK structured `Output.object`. */
|
|
114
|
+
function isStructuredOutputRequest(request) {
|
|
115
|
+
return require_decision.getAgentOutputMode(request.outputSchema) === "structured";
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* The canonical Vercel AI SDK adapter: builds the `{ generateText, streamText,
|
|
119
|
+
* decide }` executor set consumed by `runAgent`/`executeAgentRequest`. `ai`
|
|
120
|
+
* must not become a dependency of core `src/` files — this subpath is the one
|
|
121
|
+
* place it's imported, and callers must supply their own model resolver so no
|
|
122
|
+
* concrete provider package (e.g. `@ai-sdk/openai`) becomes a dependency here
|
|
123
|
+
* either.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```ts
|
|
127
|
+
* const executors = createAiSdkExecutors({ models: { quick: openai('gpt-5.4-mini') } });
|
|
128
|
+
* const result = await runAgent(machine, { input, executors });
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
function createAiSdkExecutors(options) {
|
|
132
|
+
const generateText = async (request, info) => {
|
|
133
|
+
const common = {
|
|
134
|
+
model: resolveAiSdkModel(options, request.model),
|
|
135
|
+
abortSignal: info?.signal,
|
|
136
|
+
...toAiSdkCallSettings(request),
|
|
137
|
+
...maxStepsSetting(request)
|
|
138
|
+
};
|
|
139
|
+
if (isStructuredOutputRequest(request)) {
|
|
140
|
+
const envelope = require_decision.buildEnvelopeSchema(request.outputSchema, { reasoning: request.reasoning });
|
|
141
|
+
const result = await (0, ai.generateText)({
|
|
142
|
+
...common,
|
|
143
|
+
output: ai.Output.object({ schema: envelope })
|
|
144
|
+
});
|
|
145
|
+
const { result: output, reasoning } = result.output;
|
|
146
|
+
return {
|
|
147
|
+
output,
|
|
148
|
+
...reasoning !== void 0 ? { reasoning } : {},
|
|
149
|
+
usage: result.usage,
|
|
150
|
+
finishReason: result.finishReason,
|
|
151
|
+
toolCalls: result.toolCalls,
|
|
152
|
+
toolResults: result.toolResults
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
const result = await (0, ai.generateText)(common);
|
|
156
|
+
return {
|
|
157
|
+
output: result.text,
|
|
158
|
+
usage: result.usage,
|
|
159
|
+
finishReason: result.finishReason,
|
|
160
|
+
toolCalls: result.toolCalls,
|
|
161
|
+
toolResults: result.toolResults
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
const streamText = async (request, info) => {
|
|
165
|
+
const result = (0, ai.streamText)({
|
|
166
|
+
model: resolveAiSdkModel(options, request.model),
|
|
167
|
+
abortSignal: info?.signal,
|
|
168
|
+
...toAiSdkCallSettings(request),
|
|
169
|
+
...maxStepsSetting(request)
|
|
170
|
+
});
|
|
171
|
+
for await (const chunk of result.textStream) info?.onChunk?.(chunk);
|
|
172
|
+
return {
|
|
173
|
+
output: await result.text,
|
|
174
|
+
usage: await result.usage,
|
|
175
|
+
finishReason: await result.finishReason
|
|
176
|
+
};
|
|
177
|
+
};
|
|
178
|
+
const decide = async (request) => {
|
|
179
|
+
const model = resolveAiSdkModel(options, request.model);
|
|
180
|
+
const tools = toAiSdkEventTools(request.events);
|
|
181
|
+
const messages = toDecisionMessages(request);
|
|
182
|
+
const result = await (0, ai.generateText)({
|
|
183
|
+
model,
|
|
184
|
+
abortSignal: request.signal,
|
|
185
|
+
system: request.system,
|
|
186
|
+
...messages ? { messages } : { prompt: request.prompt ?? "" },
|
|
187
|
+
tools,
|
|
188
|
+
toolChoice: "required",
|
|
189
|
+
stopWhen: (0, ai.stepCountIs)(1),
|
|
190
|
+
temperature: request.temperature,
|
|
191
|
+
maxOutputTokens: request.maxOutputTokens,
|
|
192
|
+
topP: request.topP,
|
|
193
|
+
topK: request.topK,
|
|
194
|
+
seed: request.seed,
|
|
195
|
+
stopSequences: request.stopSequences
|
|
196
|
+
});
|
|
197
|
+
const toolCall = result.toolCalls[0];
|
|
198
|
+
if (!toolCall) throw new Error("createAiSdkExecutors: decide — model did not call an event tool.");
|
|
199
|
+
const chosenEvent = request.events.find((event) => event.toolName === toolCall.toolName);
|
|
200
|
+
if (!chosenEvent) throw new Error(`createAiSdkExecutors: decide — model called unknown tool '${toolCall.toolName}'.`);
|
|
201
|
+
return {
|
|
202
|
+
event: {
|
|
203
|
+
...toolCall.input && typeof toolCall.input === "object" ? toolCall.input : {},
|
|
204
|
+
type: chosenEvent.type
|
|
205
|
+
},
|
|
206
|
+
usage: result.usage,
|
|
207
|
+
finishReason: result.finishReason
|
|
208
|
+
};
|
|
209
|
+
};
|
|
210
|
+
return {
|
|
211
|
+
generateText,
|
|
212
|
+
streamText,
|
|
213
|
+
decide
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
/** One AI SDK `tool()` per candidate event — the "tool-per-event +
|
|
217
|
+
* toolChoice: 'required'" recipe from docs/p0-design.md §2.6. */
|
|
218
|
+
function toAiSdkEventTools(events) {
|
|
219
|
+
return Object.fromEntries(events.map((event) => [event.toolName, (0, ai.tool)({
|
|
220
|
+
description: `Choose the '${event.type}' move.`,
|
|
221
|
+
inputSchema: event.inputSchema ? event.inputSchema : unknownSchema
|
|
222
|
+
})]));
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Messages for a decision request, with prior failed `attempts` (§2.6)
|
|
226
|
+
* rendered as appended user messages (via core's {@link renderDecisionAttempts})
|
|
227
|
+
* so retries converge. Core never rewrites prompts — this is adapter business.
|
|
228
|
+
*/
|
|
229
|
+
function toDecisionMessages(request) {
|
|
230
|
+
if (!request.messages && request.attempts.length === 0) return;
|
|
231
|
+
const messages = [...request.messages ?? (request.prompt !== void 0 ? [{
|
|
232
|
+
role: "user",
|
|
233
|
+
content: request.prompt
|
|
234
|
+
}] : [])];
|
|
235
|
+
for (const attempt of require_decision.renderDecisionAttempts(request)) messages.push({
|
|
236
|
+
role: "user",
|
|
237
|
+
content: attempt.content
|
|
238
|
+
});
|
|
239
|
+
return messages;
|
|
240
|
+
}
|
|
241
|
+
//#endregion
|
|
242
|
+
exports.createAiSdkExecutors = createAiSdkExecutors;
|
|
243
|
+
exports.defineModels = defineModels;
|
|
244
|
+
exports.isStructuredOutputRequest = isStructuredOutputRequest;
|
|
245
|
+
exports.toAiSdkCallSettings = toAiSdkCallSettings;
|
|
246
|
+
exports.toAiSdkEventTools = toAiSdkEventTools;
|
|
247
|
+
exports.toAiSdkToolChoice = toAiSdkToolChoice;
|
|
248
|
+
exports.toAiSdkTools = toAiSdkTools;
|
|
249
|
+
exports.toDecisionMessages = toDecisionMessages;
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { c as AgentTools, f as ChosenEvent } from "./types-BHjeDdch.cjs";
|
|
2
|
+
import { B as AgentEventDescriptor, D as AgentDecisionExecutor, i as AgentRequestExecutor, k as AgentDecisionRequest, l as AgentTextRequest, s as AgentRequestExecutors } from "./text-logic-1ZQkO3zr.cjs";
|
|
3
|
+
import { FinishReason, LanguageModel, LanguageModelUsage, ModelMessage, Tool, ToolSet, TypedToolCall, TypedToolResult } from "ai";
|
|
4
|
+
|
|
5
|
+
//#region src/ai-sdk/index.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Maps an {@link AgentTools} map onto AI SDK `tool()` definitions. A tool that
|
|
8
|
+
* already carries its own Standard Schema `inputSchema` — an AI SDK
|
|
9
|
+
* `tool({...})`, or any equivalent descriptor — is passed through **unchanged**,
|
|
10
|
+
* so the SDK applies its own validation, calls `execute(input, options)`, and
|
|
11
|
+
* every extra property (`providerOptions`, `toModelOutput`, …) survives. A bare
|
|
12
|
+
* `AgentToolExecute` function becomes a tool with an unconstrained input schema;
|
|
13
|
+
* a minimal descriptor with no readable schema is converted with a permissive
|
|
14
|
+
* fallback. Used internally by {@link toAiSdkCallSettings}; exported for callers
|
|
15
|
+
* building AI SDK calls by hand outside `createAiSdkExecutors`.
|
|
16
|
+
*/
|
|
17
|
+
declare function toAiSdkTools(tools: AgentTools): {
|
|
18
|
+
[k: string]: Tool<unknown, unknown> | Tool<unknown, never>;
|
|
19
|
+
};
|
|
20
|
+
/** AI SDK model registry: maps model refs (as used in `setupAgent({ models })`/`AgentTextRequest.model`) to AI SDK `LanguageModel` values. The optional `TKey` parameter pins the ref keys (see {@link defineModels}); it defaults to `string`, so bare `AiSdkModelMap` stays `Record<string, LanguageModel>`. */
|
|
21
|
+
type AiSdkModelMap<TKey extends string = string> = Record<TKey, LanguageModel>;
|
|
22
|
+
/**
|
|
23
|
+
* Identity helper for a `models` map whose value is exported. Returns the map
|
|
24
|
+
* unchanged, but types it as {@link AiSdkModelMap}`<keyof T & string>` — a
|
|
25
|
+
* portable, nameable type — so an exported `const models = defineModels({...})`
|
|
26
|
+
* needs no `Record<'a' | 'b', LanguageModel>` annotation and never triggers
|
|
27
|
+
* TS2742 ("inferred type cannot be named without a reference to …"). The exact
|
|
28
|
+
* ref keys survive, so `createAiSdkExecutors({ models })` and
|
|
29
|
+
* `setupAgent({ models })` still infer/autocomplete them.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* export const models = defineModels({
|
|
34
|
+
* quick: openai('gpt-5.4-mini'),
|
|
35
|
+
* deep: openai('gpt-5.4'),
|
|
36
|
+
* });
|
|
37
|
+
* // typeof models === AiSdkModelMap<'quick' | 'deep'>
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
declare function defineModels<T extends Record<string, LanguageModel>>(models: T): AiSdkModelMap<keyof T & string>;
|
|
41
|
+
/**
|
|
42
|
+
* Options for {@link createAiSdkExecutors}: either a static `models` map
|
|
43
|
+
* (model refs resolved by lookup), a `resolveModel` function (refs resolved
|
|
44
|
+
* dynamically, e.g. `"openai/gpt-5.4-mini"` → `openai('gpt-5.4-mini')`), or
|
|
45
|
+
* both — `resolveModel` takes precedence when both are supplied.
|
|
46
|
+
*/
|
|
47
|
+
type CreateAiSdkExecutorsOptions<TModels extends AiSdkModelMap = AiSdkModelMap> = {
|
|
48
|
+
models: TModels;
|
|
49
|
+
resolveModel?: (modelRef: keyof TModels & string) => LanguageModel;
|
|
50
|
+
} | {
|
|
51
|
+
models?: TModels;
|
|
52
|
+
resolveModel: (modelRef: string) => LanguageModel;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* AI SDK request-mapping settings shared by `generateText`/`streamText`.
|
|
56
|
+
* `AgentTextRequest.messages` (`AgentMessage[]`) and AI SDK's `ModelMessage[]`
|
|
57
|
+
* are structurally compatible by design (§1 of docs/p0-design.md) — the cast
|
|
58
|
+
* below is a typed identity mapping, not a semantic conversion.
|
|
59
|
+
*/
|
|
60
|
+
declare function toAiSdkCallSettings(request: AgentTextRequest & {
|
|
61
|
+
tools?: AgentTools;
|
|
62
|
+
}): {
|
|
63
|
+
temperature: number | undefined;
|
|
64
|
+
maxOutputTokens: number | undefined;
|
|
65
|
+
topP: number | undefined;
|
|
66
|
+
topK: number | undefined;
|
|
67
|
+
seed: number | undefined;
|
|
68
|
+
stopSequences: string[] | undefined;
|
|
69
|
+
tools: {
|
|
70
|
+
[k: string]: Tool<unknown, unknown> | Tool<unknown, never>;
|
|
71
|
+
} | undefined;
|
|
72
|
+
toolChoice: "auto" | "none" | "required" | {
|
|
73
|
+
type: "tool";
|
|
74
|
+
toolName: string;
|
|
75
|
+
} | undefined;
|
|
76
|
+
messages: ModelMessage[];
|
|
77
|
+
system: string | undefined;
|
|
78
|
+
} | {
|
|
79
|
+
temperature: number | undefined;
|
|
80
|
+
maxOutputTokens: number | undefined;
|
|
81
|
+
topP: number | undefined;
|
|
82
|
+
topK: number | undefined;
|
|
83
|
+
seed: number | undefined;
|
|
84
|
+
stopSequences: string[] | undefined;
|
|
85
|
+
tools: {
|
|
86
|
+
[k: string]: Tool<unknown, unknown> | Tool<unknown, never>;
|
|
87
|
+
} | undefined;
|
|
88
|
+
toolChoice: "auto" | "none" | "required" | {
|
|
89
|
+
type: "tool";
|
|
90
|
+
toolName: string;
|
|
91
|
+
} | undefined;
|
|
92
|
+
prompt: string;
|
|
93
|
+
system: string | undefined;
|
|
94
|
+
};
|
|
95
|
+
/** Maps an {@link AgentToolChoice} to AI SDK's tool-choice shape — `{ type: 'tool'; name }` becomes `{ type: 'tool'; toolName }`; `'auto'`/`'none'`/`'required'`/`undefined` pass through unchanged. */
|
|
96
|
+
declare function toAiSdkToolChoice(toolChoice: AgentTextRequest["toolChoice"]): "auto" | "none" | "required" | {
|
|
97
|
+
type: "tool";
|
|
98
|
+
toolName: string;
|
|
99
|
+
} | undefined;
|
|
100
|
+
/** `true` when the request should use AI SDK structured `Output.object`. */
|
|
101
|
+
declare function isStructuredOutputRequest(request: Pick<AgentTextRequest, "outputSchema">): boolean;
|
|
102
|
+
/**
|
|
103
|
+
* Raw result shape from {@link AiSdkExecutors.generateText} — the `{ output }`
|
|
104
|
+
* envelope (the validated structured object for structured-output requests,
|
|
105
|
+
* or the accumulated text string otherwise; unwrapped by
|
|
106
|
+
* `normalizeGeneratorResult`) plus the AI SDK call metadata. Core only reads
|
|
107
|
+
* `output`; everything else flows verbatim to `runAgent`'s
|
|
108
|
+
* `onResult(request, { raw })`, so `raw as AiSdkGenerateResult` is the
|
|
109
|
+
* supported cast for token accounting and tracing.
|
|
110
|
+
*/
|
|
111
|
+
type AiSdkGenerateResult = {
|
|
112
|
+
output: unknown;
|
|
113
|
+
/** The model's reasoning, present only when the request opted in via
|
|
114
|
+
* `reasoning: true` and the model produced it (see the structured-output
|
|
115
|
+
* envelope in {@link buildEnvelopeSchema}). Never enters machine context/output. */
|
|
116
|
+
reasoning?: string;
|
|
117
|
+
usage: LanguageModelUsage;
|
|
118
|
+
finishReason: FinishReason;
|
|
119
|
+
toolCalls: TypedToolCall<ToolSet>[];
|
|
120
|
+
toolResults: TypedToolResult<ToolSet>[];
|
|
121
|
+
};
|
|
122
|
+
/** Raw result shape from {@link AiSdkExecutors.streamText} — the `{ output }` envelope carrying the fully-accumulated text once the stream finishes (chunks are delivered separately via `onChunk`), plus the stream's final usage/finish metadata for `onResult`. */
|
|
123
|
+
type AiSdkStreamResult = {
|
|
124
|
+
output: string;
|
|
125
|
+
usage: LanguageModelUsage;
|
|
126
|
+
finishReason: FinishReason;
|
|
127
|
+
};
|
|
128
|
+
/** Raw result shape from {@link AiSdkExecutors.decide} — the chosen event plus the AI SDK call metadata, delivered per decision attempt to `onResult`. */
|
|
129
|
+
type AiSdkDecideResult = {
|
|
130
|
+
event: ChosenEvent;
|
|
131
|
+
usage: LanguageModelUsage;
|
|
132
|
+
finishReason: FinishReason;
|
|
133
|
+
};
|
|
134
|
+
/** `createAiSdkExecutors` always populates all three slots (unlike the
|
|
135
|
+
* general `AgentRequestExecutors`, where `streamText`/`decide` are optional),
|
|
136
|
+
* and its `generateText`/`streamText` results are concretely typed. */
|
|
137
|
+
interface AiSdkExecutors extends AgentRequestExecutors<AiSdkGenerateResult, AiSdkStreamResult> {
|
|
138
|
+
streamText: AgentRequestExecutor<AiSdkStreamResult>;
|
|
139
|
+
decide: AgentDecisionExecutor;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* The canonical Vercel AI SDK adapter: builds the `{ generateText, streamText,
|
|
143
|
+
* decide }` executor set consumed by `runAgent`/`executeAgentRequest`. `ai`
|
|
144
|
+
* must not become a dependency of core `src/` files — this subpath is the one
|
|
145
|
+
* place it's imported, and callers must supply their own model resolver so no
|
|
146
|
+
* concrete provider package (e.g. `@ai-sdk/openai`) becomes a dependency here
|
|
147
|
+
* either.
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```ts
|
|
151
|
+
* const executors = createAiSdkExecutors({ models: { quick: openai('gpt-5.4-mini') } });
|
|
152
|
+
* const result = await runAgent(machine, { input, executors });
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
declare function createAiSdkExecutors<TModels extends AiSdkModelMap>(options: CreateAiSdkExecutorsOptions<TModels>): AiSdkExecutors;
|
|
156
|
+
/** One AI SDK `tool()` per candidate event — the "tool-per-event +
|
|
157
|
+
* toolChoice: 'required'" recipe from docs/p0-design.md §2.6. */
|
|
158
|
+
declare function toAiSdkEventTools(events: AgentEventDescriptor[]): {
|
|
159
|
+
[k: string]: Tool<unknown, never>;
|
|
160
|
+
};
|
|
161
|
+
/**
|
|
162
|
+
* Messages for a decision request, with prior failed `attempts` (§2.6)
|
|
163
|
+
* rendered as appended user messages (via core's {@link renderDecisionAttempts})
|
|
164
|
+
* so retries converge. Core never rewrites prompts — this is adapter business.
|
|
165
|
+
*/
|
|
166
|
+
declare function toDecisionMessages(request: Pick<AgentDecisionRequest, "messages" | "prompt" | "events" | "attempts">): ModelMessage[] | undefined;
|
|
167
|
+
//#endregion
|
|
168
|
+
export { AiSdkDecideResult, AiSdkExecutors, AiSdkGenerateResult, AiSdkModelMap, AiSdkStreamResult, CreateAiSdkExecutorsOptions, createAiSdkExecutors, defineModels, isStructuredOutputRequest, toAiSdkCallSettings, toAiSdkEventTools, toAiSdkToolChoice, toAiSdkTools, toDecisionMessages };
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { c as AgentTools, f as ChosenEvent } from "./types-Cq1YlAQ6.mjs";
|
|
2
|
+
import { B as AgentEventDescriptor, D as AgentDecisionExecutor, i as AgentRequestExecutor, k as AgentDecisionRequest, l as AgentTextRequest, s as AgentRequestExecutors } from "./text-logic-2EMJIS-n.mjs";
|
|
3
|
+
import { FinishReason, LanguageModel, LanguageModelUsage, ModelMessage, Tool, ToolSet, TypedToolCall, TypedToolResult } from "ai";
|
|
4
|
+
|
|
5
|
+
//#region src/ai-sdk/index.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* Maps an {@link AgentTools} map onto AI SDK `tool()` definitions. A tool that
|
|
8
|
+
* already carries its own Standard Schema `inputSchema` — an AI SDK
|
|
9
|
+
* `tool({...})`, or any equivalent descriptor — is passed through **unchanged**,
|
|
10
|
+
* so the SDK applies its own validation, calls `execute(input, options)`, and
|
|
11
|
+
* every extra property (`providerOptions`, `toModelOutput`, …) survives. A bare
|
|
12
|
+
* `AgentToolExecute` function becomes a tool with an unconstrained input schema;
|
|
13
|
+
* a minimal descriptor with no readable schema is converted with a permissive
|
|
14
|
+
* fallback. Used internally by {@link toAiSdkCallSettings}; exported for callers
|
|
15
|
+
* building AI SDK calls by hand outside `createAiSdkExecutors`.
|
|
16
|
+
*/
|
|
17
|
+
declare function toAiSdkTools(tools: AgentTools): {
|
|
18
|
+
[k: string]: Tool<unknown, unknown> | Tool<unknown, never>;
|
|
19
|
+
};
|
|
20
|
+
/** AI SDK model registry: maps model refs (as used in `setupAgent({ models })`/`AgentTextRequest.model`) to AI SDK `LanguageModel` values. The optional `TKey` parameter pins the ref keys (see {@link defineModels}); it defaults to `string`, so bare `AiSdkModelMap` stays `Record<string, LanguageModel>`. */
|
|
21
|
+
type AiSdkModelMap<TKey extends string = string> = Record<TKey, LanguageModel>;
|
|
22
|
+
/**
|
|
23
|
+
* Identity helper for a `models` map whose value is exported. Returns the map
|
|
24
|
+
* unchanged, but types it as {@link AiSdkModelMap}`<keyof T & string>` — a
|
|
25
|
+
* portable, nameable type — so an exported `const models = defineModels({...})`
|
|
26
|
+
* needs no `Record<'a' | 'b', LanguageModel>` annotation and never triggers
|
|
27
|
+
* TS2742 ("inferred type cannot be named without a reference to …"). The exact
|
|
28
|
+
* ref keys survive, so `createAiSdkExecutors({ models })` and
|
|
29
|
+
* `setupAgent({ models })` still infer/autocomplete them.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* export const models = defineModels({
|
|
34
|
+
* quick: openai('gpt-5.4-mini'),
|
|
35
|
+
* deep: openai('gpt-5.4'),
|
|
36
|
+
* });
|
|
37
|
+
* // typeof models === AiSdkModelMap<'quick' | 'deep'>
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
declare function defineModels<T extends Record<string, LanguageModel>>(models: T): AiSdkModelMap<keyof T & string>;
|
|
41
|
+
/**
|
|
42
|
+
* Options for {@link createAiSdkExecutors}: either a static `models` map
|
|
43
|
+
* (model refs resolved by lookup), a `resolveModel` function (refs resolved
|
|
44
|
+
* dynamically, e.g. `"openai/gpt-5.4-mini"` → `openai('gpt-5.4-mini')`), or
|
|
45
|
+
* both — `resolveModel` takes precedence when both are supplied.
|
|
46
|
+
*/
|
|
47
|
+
type CreateAiSdkExecutorsOptions<TModels extends AiSdkModelMap = AiSdkModelMap> = {
|
|
48
|
+
models: TModels;
|
|
49
|
+
resolveModel?: (modelRef: keyof TModels & string) => LanguageModel;
|
|
50
|
+
} | {
|
|
51
|
+
models?: TModels;
|
|
52
|
+
resolveModel: (modelRef: string) => LanguageModel;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* AI SDK request-mapping settings shared by `generateText`/`streamText`.
|
|
56
|
+
* `AgentTextRequest.messages` (`AgentMessage[]`) and AI SDK's `ModelMessage[]`
|
|
57
|
+
* are structurally compatible by design (§1 of docs/p0-design.md) — the cast
|
|
58
|
+
* below is a typed identity mapping, not a semantic conversion.
|
|
59
|
+
*/
|
|
60
|
+
declare function toAiSdkCallSettings(request: AgentTextRequest & {
|
|
61
|
+
tools?: AgentTools;
|
|
62
|
+
}): {
|
|
63
|
+
temperature: number | undefined;
|
|
64
|
+
maxOutputTokens: number | undefined;
|
|
65
|
+
topP: number | undefined;
|
|
66
|
+
topK: number | undefined;
|
|
67
|
+
seed: number | undefined;
|
|
68
|
+
stopSequences: string[] | undefined;
|
|
69
|
+
tools: {
|
|
70
|
+
[k: string]: Tool<unknown, unknown> | Tool<unknown, never>;
|
|
71
|
+
} | undefined;
|
|
72
|
+
toolChoice: "auto" | "none" | "required" | {
|
|
73
|
+
type: "tool";
|
|
74
|
+
toolName: string;
|
|
75
|
+
} | undefined;
|
|
76
|
+
messages: ModelMessage[];
|
|
77
|
+
system: string | undefined;
|
|
78
|
+
} | {
|
|
79
|
+
temperature: number | undefined;
|
|
80
|
+
maxOutputTokens: number | undefined;
|
|
81
|
+
topP: number | undefined;
|
|
82
|
+
topK: number | undefined;
|
|
83
|
+
seed: number | undefined;
|
|
84
|
+
stopSequences: string[] | undefined;
|
|
85
|
+
tools: {
|
|
86
|
+
[k: string]: Tool<unknown, unknown> | Tool<unknown, never>;
|
|
87
|
+
} | undefined;
|
|
88
|
+
toolChoice: "auto" | "none" | "required" | {
|
|
89
|
+
type: "tool";
|
|
90
|
+
toolName: string;
|
|
91
|
+
} | undefined;
|
|
92
|
+
prompt: string;
|
|
93
|
+
system: string | undefined;
|
|
94
|
+
};
|
|
95
|
+
/** Maps an {@link AgentToolChoice} to AI SDK's tool-choice shape — `{ type: 'tool'; name }` becomes `{ type: 'tool'; toolName }`; `'auto'`/`'none'`/`'required'`/`undefined` pass through unchanged. */
|
|
96
|
+
declare function toAiSdkToolChoice(toolChoice: AgentTextRequest["toolChoice"]): "auto" | "none" | "required" | {
|
|
97
|
+
type: "tool";
|
|
98
|
+
toolName: string;
|
|
99
|
+
} | undefined;
|
|
100
|
+
/** `true` when the request should use AI SDK structured `Output.object`. */
|
|
101
|
+
declare function isStructuredOutputRequest(request: Pick<AgentTextRequest, "outputSchema">): boolean;
|
|
102
|
+
/**
|
|
103
|
+
* Raw result shape from {@link AiSdkExecutors.generateText} — the `{ output }`
|
|
104
|
+
* envelope (the validated structured object for structured-output requests,
|
|
105
|
+
* or the accumulated text string otherwise; unwrapped by
|
|
106
|
+
* `normalizeGeneratorResult`) plus the AI SDK call metadata. Core only reads
|
|
107
|
+
* `output`; everything else flows verbatim to `runAgent`'s
|
|
108
|
+
* `onResult(request, { raw })`, so `raw as AiSdkGenerateResult` is the
|
|
109
|
+
* supported cast for token accounting and tracing.
|
|
110
|
+
*/
|
|
111
|
+
type AiSdkGenerateResult = {
|
|
112
|
+
output: unknown;
|
|
113
|
+
/** The model's reasoning, present only when the request opted in via
|
|
114
|
+
* `reasoning: true` and the model produced it (see the structured-output
|
|
115
|
+
* envelope in {@link buildEnvelopeSchema}). Never enters machine context/output. */
|
|
116
|
+
reasoning?: string;
|
|
117
|
+
usage: LanguageModelUsage;
|
|
118
|
+
finishReason: FinishReason;
|
|
119
|
+
toolCalls: TypedToolCall<ToolSet>[];
|
|
120
|
+
toolResults: TypedToolResult<ToolSet>[];
|
|
121
|
+
};
|
|
122
|
+
/** Raw result shape from {@link AiSdkExecutors.streamText} — the `{ output }` envelope carrying the fully-accumulated text once the stream finishes (chunks are delivered separately via `onChunk`), plus the stream's final usage/finish metadata for `onResult`. */
|
|
123
|
+
type AiSdkStreamResult = {
|
|
124
|
+
output: string;
|
|
125
|
+
usage: LanguageModelUsage;
|
|
126
|
+
finishReason: FinishReason;
|
|
127
|
+
};
|
|
128
|
+
/** Raw result shape from {@link AiSdkExecutors.decide} — the chosen event plus the AI SDK call metadata, delivered per decision attempt to `onResult`. */
|
|
129
|
+
type AiSdkDecideResult = {
|
|
130
|
+
event: ChosenEvent;
|
|
131
|
+
usage: LanguageModelUsage;
|
|
132
|
+
finishReason: FinishReason;
|
|
133
|
+
};
|
|
134
|
+
/** `createAiSdkExecutors` always populates all three slots (unlike the
|
|
135
|
+
* general `AgentRequestExecutors`, where `streamText`/`decide` are optional),
|
|
136
|
+
* and its `generateText`/`streamText` results are concretely typed. */
|
|
137
|
+
interface AiSdkExecutors extends AgentRequestExecutors<AiSdkGenerateResult, AiSdkStreamResult> {
|
|
138
|
+
streamText: AgentRequestExecutor<AiSdkStreamResult>;
|
|
139
|
+
decide: AgentDecisionExecutor;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* The canonical Vercel AI SDK adapter: builds the `{ generateText, streamText,
|
|
143
|
+
* decide }` executor set consumed by `runAgent`/`executeAgentRequest`. `ai`
|
|
144
|
+
* must not become a dependency of core `src/` files — this subpath is the one
|
|
145
|
+
* place it's imported, and callers must supply their own model resolver so no
|
|
146
|
+
* concrete provider package (e.g. `@ai-sdk/openai`) becomes a dependency here
|
|
147
|
+
* either.
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```ts
|
|
151
|
+
* const executors = createAiSdkExecutors({ models: { quick: openai('gpt-5.4-mini') } });
|
|
152
|
+
* const result = await runAgent(machine, { input, executors });
|
|
153
|
+
* ```
|
|
154
|
+
*/
|
|
155
|
+
declare function createAiSdkExecutors<TModels extends AiSdkModelMap>(options: CreateAiSdkExecutorsOptions<TModels>): AiSdkExecutors;
|
|
156
|
+
/** One AI SDK `tool()` per candidate event — the "tool-per-event +
|
|
157
|
+
* toolChoice: 'required'" recipe from docs/p0-design.md §2.6. */
|
|
158
|
+
declare function toAiSdkEventTools(events: AgentEventDescriptor[]): {
|
|
159
|
+
[k: string]: Tool<unknown, never>;
|
|
160
|
+
};
|
|
161
|
+
/**
|
|
162
|
+
* Messages for a decision request, with prior failed `attempts` (§2.6)
|
|
163
|
+
* rendered as appended user messages (via core's {@link renderDecisionAttempts})
|
|
164
|
+
* so retries converge. Core never rewrites prompts — this is adapter business.
|
|
165
|
+
*/
|
|
166
|
+
declare function toDecisionMessages(request: Pick<AgentDecisionRequest, "messages" | "prompt" | "events" | "attempts">): ModelMessage[] | undefined;
|
|
167
|
+
//#endregion
|
|
168
|
+
export { AiSdkDecideResult, AiSdkExecutors, AiSdkGenerateResult, AiSdkModelMap, AiSdkStreamResult, CreateAiSdkExecutorsOptions, createAiSdkExecutors, defineModels, isStructuredOutputRequest, toAiSdkCallSettings, toAiSdkEventTools, toAiSdkToolChoice, toAiSdkTools, toDecisionMessages };
|