@xsai/text 1.0.0-beta.6 → 1.0.0-beta.7
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/index.d.ts +3 -2
- package/dist/index.js +27 -32
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { S as StepEndDoneEvent, T as ToolCallPart, a as ToolResultPart, U as Usage, b as TextEvent,
|
|
1
|
+
import { S as StepEndDoneEvent, T as ToolCallPart, a as ToolResultPart, U as Usage, b as TextEvent, L as LanguageModelOptions, M as Message, c as LanguageModel, d as ToolExecuteOptions } from './language-model-CAPBb-ti.js';
|
|
2
2
|
export { A as AssistantMessage, e as AssistantMessageContent, f as AudioPart, C as CombinedStandardSchema, g as Content, h as ContentEndEvent, i as ContentMap, j as ContentStartEvent, k as ContentType, D as DeveloperMessage, l as DeveloperMessageContent, E as ExecutableTool, F as FilePart, m as FinishReason, I as ImagePart, n as InferSchemaInput, o as InferSchemaOutput, J as JSONSchema7, p as JSONSchema7Definition, q as LanguageModelProviderOptions, r as MessageMap, s as MessageProviderMetadata, t as MessageType, P as PartProviderMetadata, u as ProviderPart, R as RawEvent, v as ReasoningDeltaEvent, w as ReasoningPart, x as ReasoningPartContent, y as RefusalDeltaEvent, z as RefusalPart, B as ResolvedSchema, G as StepEndEvent, H as StepEndFailEvent, K as StepStartEvent, N as StepStatus, O as SystemMessage, Q as SystemMessageContent, V as TextDeltaEvent, W as TextEventMap, X as TextEventType, Y as TextPart, Z as Tool, _ as ToolCallDeltaEvent, $ as ToolChoice, a0 as ToolOptions, a1 as ToolResultPartContent, a2 as UnresolvedSchema, a3 as UserMessage, a4 as UserMessageContent, a5 as formatIssues, a6 as resolveSchema, a7 as tool } from './language-model-CAPBb-ti.js';
|
|
3
3
|
import { Promisable } from '@xsai/shared';
|
|
4
4
|
export { HttpError, XSAIError, XSAIErrorCause, XSAIErrorCauseMap, XSAIErrorCode, XSAIErrorOptions } from '@xsai/shared';
|
|
@@ -16,8 +16,9 @@ interface CollectResult extends StepResult {
|
|
|
16
16
|
declare const collect: (stream: ReadableStream<TextEvent>) => Promise<CollectResult>;
|
|
17
17
|
|
|
18
18
|
type PrepareStep = (options: PrepareStepOptions) => Promisable<PrepareStepResult | undefined>;
|
|
19
|
-
interface PrepareStepOptions {
|
|
19
|
+
interface PrepareStepOptions extends Pick<LanguageModelOptions, 'instructions' | 'tools'> {
|
|
20
20
|
input: readonly Message[];
|
|
21
|
+
model: LanguageModel;
|
|
21
22
|
signal: AbortSignal;
|
|
22
23
|
stepNumber: number;
|
|
23
24
|
steps: readonly StepResult[];
|
package/dist/index.js
CHANGED
|
@@ -112,6 +112,13 @@ const runTool = async (call, tools, options) => {
|
|
|
112
112
|
}
|
|
113
113
|
return { callId: call.callId, output: await tool.execute(input, options), type: "tool-result" };
|
|
114
114
|
};
|
|
115
|
+
const finishTool = async (call, result, { postToolCall, signal }) => {
|
|
116
|
+
const posted = await attempt(call, signal, async () => postToolCall?.(result, { callId: call.callId, signal }));
|
|
117
|
+
if (posted == null)
|
|
118
|
+
return result;
|
|
119
|
+
assertSameCallId(result.callId, posted.callId);
|
|
120
|
+
return posted;
|
|
121
|
+
};
|
|
115
122
|
const executeTool = async (call, { postToolCall, preToolCall, signal, tools }) => {
|
|
116
123
|
const options = { callId: call.callId, signal };
|
|
117
124
|
const prepared = await attempt(call, signal, async () => preToolCall?.(call, options));
|
|
@@ -122,13 +129,8 @@ const executeTool = async (call, { postToolCall, preToolCall, signal, tools }) =
|
|
|
122
129
|
call = prepared;
|
|
123
130
|
}
|
|
124
131
|
const result = await attempt(call, signal, async () => runTool(call, tools, options));
|
|
125
|
-
|
|
126
|
-
if (posted == null)
|
|
127
|
-
return result;
|
|
128
|
-
assertSameCallId(result.callId, posted.callId);
|
|
129
|
-
return posted;
|
|
132
|
+
return finishTool(call, result, { postToolCall, signal });
|
|
130
133
|
};
|
|
131
|
-
const executeTools = async ({ reason, toolCalls, ...options }) => reason === "length" ? toolCalls.map((call) => toolError(call, "The response reached the output token limit before the tool call was complete.")) : Promise.all(toolCalls.map(async (call) => executeTool(call, options)));
|
|
132
134
|
|
|
133
135
|
const streamText = (model, { postToolCall, prepareStep, preToolCall, stopWhen: stopWhenOption, ...options }) => {
|
|
134
136
|
const stopWhen = stopWhenOption ?? maxSteps(10);
|
|
@@ -139,7 +141,7 @@ const streamText = (model, { postToolCall, prepareStep, preToolCall, stopWhen: s
|
|
|
139
141
|
const generate = async function* () {
|
|
140
142
|
while (true) {
|
|
141
143
|
signal.throwIfAborted();
|
|
142
|
-
const { model: preparedModel = model, ...preparedOptions } = await prepareStep?.({ input, signal, stepNumber: steps.length, steps }) ?? {};
|
|
144
|
+
const { model: preparedModel = model, ...preparedOptions } = await prepareStep?.({ input, instructions: options.instructions, model, signal, stepNumber: steps.length, steps, tools: options.tools }) ?? {};
|
|
143
145
|
signal.throwIfAborted();
|
|
144
146
|
const modelOptions = {
|
|
145
147
|
...options,
|
|
@@ -159,13 +161,8 @@ const streamText = (model, { postToolCall, prepareStep, preToolCall, stopWhen: s
|
|
|
159
161
|
signal.throwIfAborted();
|
|
160
162
|
if (step.toolCalls.length === 0)
|
|
161
163
|
continue;
|
|
162
|
-
const
|
|
163
|
-
|
|
164
|
-
postToolCall,
|
|
165
|
-
preToolCall,
|
|
166
|
-
reason: step.reason,
|
|
167
|
-
toolCalls: step.toolCalls
|
|
168
|
-
});
|
|
164
|
+
const toolOptions = { ...modelOptions, postToolCall, preToolCall };
|
|
165
|
+
const results = await Promise.all(step.toolCalls.map(async (call) => step.reason === "length" ? finishTool(call, toolError(call, "The response reached the output token limit before the tool call was complete."), toolOptions) : executeTool(call, toolOptions)));
|
|
169
166
|
signal.throwIfAborted();
|
|
170
167
|
step.toolResults.push(...results);
|
|
171
168
|
input.push({ content: results, role: "user" });
|
|
@@ -216,26 +213,24 @@ const tool = ((options) => {
|
|
|
216
213
|
name: options.name,
|
|
217
214
|
outputSchema
|
|
218
215
|
};
|
|
219
|
-
if (options.execute == null)
|
|
216
|
+
if (options.execute == null)
|
|
220
217
|
return tool2;
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
result = validation.value;
|
|
231
|
-
}
|
|
232
|
-
if (outputSchema)
|
|
233
|
-
return JSON.stringify(result);
|
|
234
|
-
else
|
|
235
|
-
return result;
|
|
218
|
+
return {
|
|
219
|
+
...tool2,
|
|
220
|
+
execute: async (input, executeOptions) => {
|
|
221
|
+
let result = await options.execute(input, executeOptions);
|
|
222
|
+
if (outputSchema?.validate != null) {
|
|
223
|
+
const validation = await outputSchema.validate(result);
|
|
224
|
+
if (validation.issues)
|
|
225
|
+
throw new Error(`Tool output validation failed for "${options.name}": ${formatIssues(validation.issues)}`);
|
|
226
|
+
result = validation.value;
|
|
236
227
|
}
|
|
237
|
-
|
|
238
|
-
|
|
228
|
+
if (outputSchema)
|
|
229
|
+
return JSON.stringify(result);
|
|
230
|
+
else
|
|
231
|
+
return result;
|
|
232
|
+
}
|
|
233
|
+
};
|
|
239
234
|
});
|
|
240
235
|
|
|
241
236
|
export { TextEventTarget, and, collect, executeTool, generateText, hasToolCall, maxSteps, not, or, stepCountAtLeast, streamText, toCustomEvent, tool, withEventTarget };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xsai/text",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0.0-beta.
|
|
4
|
+
"version": "1.0.0-beta.7",
|
|
5
5
|
"description": "extra-small AI SDK: provider-agnostic text generation, streaming and tool calling.",
|
|
6
6
|
"author": "Moeru AI",
|
|
7
7
|
"license": "MIT",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"node": ">=26"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@xsai/shared": "1.0.0-beta.
|
|
44
|
+
"@xsai/shared": "1.0.0-beta.7"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@standard-schema/spec": "1.1.0",
|