@xsai/shared-chat 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 +24 -2
- package/dist/index.js +33 -2
- package/package.json +1 -1
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;
|
|
@@ -139,6 +141,26 @@ interface ChatOptions extends CommonRequestOptions {
|
|
|
139
141
|
}
|
|
140
142
|
declare const chat: <T extends ChatOptions>(options: T) => Promise<Response>;
|
|
141
143
|
|
|
142
|
-
|
|
144
|
+
interface DetermineStepTypeOptions {
|
|
145
|
+
finishReason: FinishReason;
|
|
146
|
+
maxSteps: number;
|
|
147
|
+
stepsLength: number;
|
|
148
|
+
toolCallsLength: number;
|
|
149
|
+
}
|
|
150
|
+
/** @internal */
|
|
151
|
+
declare const determineStepType: ({ finishReason, maxSteps, stepsLength, toolCallsLength }: DetermineStepTypeOptions) => StepType;
|
|
152
|
+
|
|
153
|
+
interface ExecuteToolOptions {
|
|
154
|
+
abortSignal?: AbortSignal;
|
|
155
|
+
messages: Message[];
|
|
156
|
+
toolCall: ToolCall;
|
|
157
|
+
tools?: Tool[];
|
|
158
|
+
}
|
|
159
|
+
interface ExecuteToolResult {
|
|
160
|
+
parsedArgs: Record<string, unknown>;
|
|
161
|
+
result: string | ToolMessagePart[];
|
|
162
|
+
toolName: string;
|
|
163
|
+
}
|
|
164
|
+
declare const executeTool: ({ abortSignal, messages, toolCall, tools }: ExecuteToolOptions) => Promise<ExecuteToolResult>;
|
|
143
165
|
|
|
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,
|
|
166
|
+
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 ToolMessage, type ToolMessagePart, type Usage, type UserMessage, type UserMessagePart, chat, determineStepType, executeTool };
|
package/dist/index.js
CHANGED
|
@@ -16,7 +16,20 @@ var chat = async (options) => (options.fetch ?? globalThis.fetch)(requestURL("ch
|
|
|
16
16
|
signal: options.abortSignal
|
|
17
17
|
}).then(responseCatch);
|
|
18
18
|
|
|
19
|
-
// src/utils/
|
|
19
|
+
// src/utils/determine-step-type.ts
|
|
20
|
+
var determineStepType = ({ finishReason, maxSteps, stepsLength, toolCallsLength }) => {
|
|
21
|
+
if (stepsLength === 0) {
|
|
22
|
+
return "initial";
|
|
23
|
+
} else if (stepsLength < maxSteps) {
|
|
24
|
+
if (toolCallsLength > 0 && finishReason === "tool_calls")
|
|
25
|
+
return "tool-result";
|
|
26
|
+
else if (!["error", "length"].includes(finishReason))
|
|
27
|
+
return "continue";
|
|
28
|
+
}
|
|
29
|
+
return "done";
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// src/utils/internal/wrap-tool-result.ts
|
|
20
33
|
var wrapToolResult = (result) => {
|
|
21
34
|
if (typeof result === "string")
|
|
22
35
|
return result;
|
|
@@ -27,7 +40,25 @@ var wrapToolResult = (result) => {
|
|
|
27
40
|
}
|
|
28
41
|
return JSON.stringify(result);
|
|
29
42
|
};
|
|
43
|
+
|
|
44
|
+
// src/utils/execute-tool.ts
|
|
45
|
+
var executeTool = async ({ abortSignal, messages, toolCall, tools }) => {
|
|
46
|
+
const tool = tools?.find((tool2) => tool2.function.name === toolCall.function.name);
|
|
47
|
+
if (!tool) {
|
|
48
|
+
const availableTools = tools?.map((tool2) => tool2.function.name);
|
|
49
|
+
const availableToolsErrorMsg = availableTools == null || availableTools.length === 0 ? "No tools are available." : `Available tools: ${availableTools.join(", ")}.`;
|
|
50
|
+
throw new Error(`Model tried to call unavailable tool '${toolCall.function.name}. ${availableToolsErrorMsg}.`);
|
|
51
|
+
}
|
|
52
|
+
const parsedArgs = JSON.parse(toolCall.function.arguments);
|
|
53
|
+
const result = wrapToolResult(await tool.execute(parsedArgs, {
|
|
54
|
+
abortSignal,
|
|
55
|
+
messages,
|
|
56
|
+
toolCallId: toolCall.id
|
|
57
|
+
}));
|
|
58
|
+
return { parsedArgs, result, toolName: toolCall.function.name };
|
|
59
|
+
};
|
|
30
60
|
export {
|
|
31
61
|
chat,
|
|
32
|
-
|
|
62
|
+
determineStepType,
|
|
63
|
+
executeTool
|
|
33
64
|
};
|