@xsai/generate-text 0.1.2 → 0.1.3

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 CHANGED
@@ -1,4 +1,4 @@
1
- import { ChatOptions, Tool, FinishReason, AssistantMessageResponse, Usage, Message, CompletionToolCall, CompletionToolResult } from '@xsai/shared-chat';
1
+ import { ChatOptions, Tool, FinishReason, AssistantMessageResponse, Usage, Message, CompletionToolCall, CompletionToolResult, StepType } from '@xsai/shared-chat';
2
2
 
3
3
  interface GenerateTextOptions extends ChatOptions {
4
4
  /** @default 1 */
@@ -34,7 +34,7 @@ interface GenerateTextResult {
34
34
  }
35
35
  interface GenerateTextStepResult {
36
36
  finishReason: FinishReason;
37
- stepType: 'continue' | 'initial' | 'tool-result';
37
+ stepType: StepType;
38
38
  text?: string;
39
39
  toolCalls: CompletionToolCall[];
40
40
  toolResults: CompletionToolResult[];
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/index.ts
2
- import { chat, wrapToolResult } from "@xsai/shared-chat";
2
+ import { chat, determineStepType, executeTool } from "@xsai/shared-chat";
3
3
  var rawGenerateText = async (options) => chat({
4
4
  ...options,
5
5
  maxSteps: void 0,
@@ -12,9 +12,15 @@ var rawGenerateText = async (options) => chat({
12
12
  const toolCalls = [];
13
13
  const toolResults = [];
14
14
  const { finish_reason: finishReason, message } = choices[0];
15
+ const msgToolCalls = message?.tool_calls ?? [];
16
+ const stepType = determineStepType({
17
+ finishReason,
18
+ maxSteps: options.maxSteps ?? 1,
19
+ stepsLength: steps.length,
20
+ toolCallsLength: msgToolCalls.length
21
+ });
15
22
  messages.push({ ...message, content: message.content });
16
- const stepType = steps.length === 0 ? "initial" : steps.at(-1)?.finishReason === "tool-calls" ? "tool-result" : "continue";
17
- if (message.content !== void 0 && message.content.length > 0 || !message.tool_calls || steps.length >= (options.maxSteps ?? 1)) {
23
+ if (finishReason === "stop" || stepType === "done") {
18
24
  const step2 = {
19
25
  finishReason,
20
26
  stepType,
@@ -36,35 +42,20 @@ var rawGenerateText = async (options) => chat({
36
42
  usage
37
43
  };
38
44
  }
39
- for (const {
40
- function: { arguments: toolArgs, name: toolName },
41
- id: toolCallId,
42
- type: toolCallType
43
- } of message.tool_calls) {
44
- const tool = options.tools?.find((tool2) => tool2.function.name === toolName);
45
- if (!tool) {
46
- const availableTools = options.tools?.map((tool2) => tool2.function.name);
47
- const availableToolsErrorMsg = availableTools === void 0 ? "No tools are available." : `Available tools: ${availableTools.join(", ")}.`;
48
- throw new Error(`Model tried to call unavailable tool '${toolName}. ${availableToolsErrorMsg}.`);
49
- }
50
- const parsedArgs = JSON.parse(toolArgs);
51
- const result = wrapToolResult(await tool.execute(parsedArgs, { abortSignal: options.abortSignal, messages, toolCallId }));
52
- toolCalls.push({
53
- args: toolArgs,
54
- toolCallId,
55
- toolCallType,
56
- toolName
57
- });
58
- toolResults.push({
59
- args: parsedArgs,
60
- result,
61
- toolCallId,
62
- toolName
45
+ for (const toolCall of msgToolCalls) {
46
+ const { parsedArgs, result, toolName } = await executeTool({
47
+ abortSignal: options.abortSignal,
48
+ messages,
49
+ toolCall,
50
+ tools: options.tools
63
51
  });
52
+ const toolInfo = { toolCallId: toolCall.id, toolName };
53
+ toolCalls.push({ ...toolInfo, args: toolCall.function.arguments, toolCallType: toolCall.type });
54
+ toolResults.push({ ...toolInfo, args: parsedArgs, result });
64
55
  messages.push({
65
56
  content: result,
66
57
  role: "tool",
67
- tool_call_id: toolCallId
58
+ tool_call_id: toolCall.id
68
59
  });
69
60
  }
70
61
  const step = {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@xsai/generate-text",
3
3
  "type": "module",
4
- "version": "0.1.2",
4
+ "version": "0.1.3",
5
5
  "description": "extra-small AI SDK for Browser, Node.js, Deno, Bun or Edge Runtime.",
6
6
  "author": "Moeru AI",
7
7
  "license": "MIT",