@xsai/shared-chat 0.1.2 → 0.2.0-beta.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/index.d.ts CHANGED
@@ -70,6 +70,8 @@ interface UserMessage extends CommonMessage<'user', UserMessagePart> {
70
70
  type UserMessagePart = AudioPart | ImagePart | TextPart;
71
71
  type Optional<T, K extends keyof T> = Omit<T, K> & Pick<Partial<T>, K>;
72
72
 
73
+ type StepType = 'continue' | 'done' | 'initial' | 'tool-result';
74
+
73
75
  interface CompletionToolCall {
74
76
  args: string;
75
77
  toolCallId: string;
@@ -83,11 +85,13 @@ interface CompletionToolResult {
83
85
  toolName: string;
84
86
  }
85
87
  interface Tool {
86
- execute: (input: unknown, options: ToolExecuteOptions) => (object | string | unknown[]) | Promise<object | string | unknown[]>;
88
+ execute: (input: unknown, options: ToolExecuteOptions) => Promise<ToolExecuteResult> | ToolExecuteResult;
87
89
  function: {
88
90
  description?: string;
89
91
  name: string;
90
92
  parameters: Record<string, unknown>;
93
+ /** @experimental */
94
+ returns?: Record<string, unknown>;
91
95
  strict?: boolean;
92
96
  };
93
97
  type: 'function';
@@ -97,6 +101,7 @@ interface ToolExecuteOptions {
97
101
  messages: Message[];
98
102
  toolCallId: string;
99
103
  }
104
+ type ToolExecuteResult = object | string | unknown[];
100
105
 
101
106
  type ToolChoice = 'auto' | 'none' | 'required' | {
102
107
  function: {
@@ -139,6 +144,26 @@ interface ChatOptions extends CommonRequestOptions {
139
144
  }
140
145
  declare const chat: <T extends ChatOptions>(options: T) => Promise<Response>;
141
146
 
142
- declare const wrapToolResult: (result: object | string | unknown[]) => string | ToolMessagePart[];
147
+ interface DetermineStepTypeOptions {
148
+ finishReason: FinishReason;
149
+ maxSteps: number;
150
+ stepsLength: number;
151
+ toolCallsLength: number;
152
+ }
153
+ /** @internal */
154
+ declare const determineStepType: ({ finishReason, maxSteps, stepsLength, toolCallsLength }: DetermineStepTypeOptions) => StepType;
155
+
156
+ interface ExecuteToolOptions {
157
+ abortSignal?: AbortSignal;
158
+ messages: Message[];
159
+ toolCall: ToolCall;
160
+ tools?: Tool[];
161
+ }
162
+ interface ExecuteToolResult {
163
+ parsedArgs: Record<string, unknown>;
164
+ result: string | ToolMessagePart[];
165
+ toolName: string;
166
+ }
167
+ declare const executeTool: ({ abortSignal, messages, toolCall, tools }: ExecuteToolOptions) => Promise<ExecuteToolResult>;
143
168
 
144
- export { type AssistantMessage, type AssistantMessagePart, type AssistantMessageResponse, type AudioBase64, type AudioPart, type ChatOptions, type CommonMessage, type CommonPart, type CompletionToolCall, type CompletionToolResult, type FinishReason, type ImagePart, type ImageURLorBase64, type Message, type Part, type RefusalPart, type SystemMessage, type SystemMessagePart, type TextPart, type Tool, type ToolCall, type ToolChoice, type ToolExecuteOptions, type ToolMessage, type ToolMessagePart, type Usage, type UserMessage, type UserMessagePart, chat, wrapToolResult };
169
+ export { type AssistantMessage, type AssistantMessagePart, type AssistantMessageResponse, type AudioBase64, type AudioPart, type ChatOptions, type CommonMessage, type CommonPart, type CompletionToolCall, type CompletionToolResult, type DetermineStepTypeOptions, type ExecuteToolOptions, type ExecuteToolResult, type FinishReason, type ImagePart, type ImageURLorBase64, type Message, type Part, type RefusalPart, type StepType, type SystemMessage, type SystemMessagePart, type TextPart, type Tool, type ToolCall, type ToolChoice, type ToolExecuteOptions, type ToolExecuteResult, type ToolMessage, type ToolMessagePart, type Usage, type UserMessage, type UserMessagePart, chat, determineStepType, executeTool };
package/dist/index.js CHANGED
@@ -1,10 +1,13 @@
1
1
  // src/utils/chat.ts
2
- import { requestBody, requestHeaders, requestURL, responseCatch } from "@xsai/shared";
2
+ import { clean, requestBody, requestHeaders, requestURL, responseCatch } from "@xsai/shared";
3
3
  var chat = async (options) => (options.fetch ?? globalThis.fetch)(requestURL("chat/completions", options.baseURL), {
4
4
  body: requestBody({
5
5
  ...options,
6
6
  tools: options.tools?.map((tool) => ({
7
- function: tool.function,
7
+ function: clean({
8
+ ...tool.function,
9
+ returns: void 0
10
+ }),
8
11
  type: "function"
9
12
  }))
10
13
  }),
@@ -16,7 +19,20 @@ var chat = async (options) => (options.fetch ?? globalThis.fetch)(requestURL("ch
16
19
  signal: options.abortSignal
17
20
  }).then(responseCatch);
18
21
 
19
- // src/utils/wrap-tool-result.ts
22
+ // src/utils/determine-step-type.ts
23
+ var determineStepType = ({ finishReason, maxSteps, stepsLength, toolCallsLength }) => {
24
+ if (stepsLength === 0) {
25
+ return "initial";
26
+ } else if (stepsLength < maxSteps) {
27
+ if (toolCallsLength > 0 && finishReason === "tool_calls")
28
+ return "tool-result";
29
+ else if (!["error", "length"].includes(finishReason))
30
+ return "continue";
31
+ }
32
+ return "done";
33
+ };
34
+
35
+ // src/utils/internal/wrap-tool-result.ts
20
36
  var wrapToolResult = (result) => {
21
37
  if (typeof result === "string")
22
38
  return result;
@@ -27,7 +43,25 @@ var wrapToolResult = (result) => {
27
43
  }
28
44
  return JSON.stringify(result);
29
45
  };
46
+
47
+ // src/utils/execute-tool.ts
48
+ var executeTool = async ({ abortSignal, messages, toolCall, tools }) => {
49
+ const tool = tools?.find((tool2) => tool2.function.name === toolCall.function.name);
50
+ if (!tool) {
51
+ const availableTools = tools?.map((tool2) => tool2.function.name);
52
+ const availableToolsErrorMsg = availableTools == null || availableTools.length === 0 ? "No tools are available." : `Available tools: ${availableTools.join(", ")}.`;
53
+ throw new Error(`Model tried to call unavailable tool '${toolCall.function.name}. ${availableToolsErrorMsg}.`);
54
+ }
55
+ const parsedArgs = JSON.parse(toolCall.function.arguments);
56
+ const result = wrapToolResult(await tool.execute(parsedArgs, {
57
+ abortSignal,
58
+ messages,
59
+ toolCallId: toolCall.id
60
+ }));
61
+ return { parsedArgs, result, toolName: toolCall.function.name };
62
+ };
30
63
  export {
31
64
  chat,
32
- wrapToolResult
65
+ determineStepType,
66
+ executeTool
33
67
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@xsai/shared-chat",
3
3
  "type": "module",
4
- "version": "0.1.2",
4
+ "version": "0.2.0-beta.1",
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",