@wyatex/xsai-shared-chat 0.4.3-fix-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 +217 -0
- package/dist/index.js +89 -0
- package/package.json +42 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { WithUnknown, CommonRequestOptions } from '@xsai/shared';
|
|
2
|
+
|
|
3
|
+
type FinishReason = 'content_filter' | 'error' | 'length' | 'other' | 'stop' | 'tool-calls' | (string & {});
|
|
4
|
+
|
|
5
|
+
interface AudioContentPart {
|
|
6
|
+
input_audio: {
|
|
7
|
+
data: string;
|
|
8
|
+
format: 'mp3' | 'wav';
|
|
9
|
+
};
|
|
10
|
+
type: 'input_audio';
|
|
11
|
+
}
|
|
12
|
+
/** @internal */
|
|
13
|
+
type CommonContentPart = AudioContentPart | FileContentPart | ImageContentPart | TextContentPart;
|
|
14
|
+
interface FileContentPart {
|
|
15
|
+
file: {
|
|
16
|
+
file_data?: string;
|
|
17
|
+
file_id?: string;
|
|
18
|
+
filename?: string;
|
|
19
|
+
};
|
|
20
|
+
type: 'file';
|
|
21
|
+
}
|
|
22
|
+
interface ImageContentPart {
|
|
23
|
+
image_url: {
|
|
24
|
+
detail?: 'auto' | 'high' | 'low';
|
|
25
|
+
url: string;
|
|
26
|
+
};
|
|
27
|
+
type: 'image_url';
|
|
28
|
+
}
|
|
29
|
+
interface RefusalContentPart {
|
|
30
|
+
refusal: string;
|
|
31
|
+
type: 'refusal';
|
|
32
|
+
}
|
|
33
|
+
interface TextContentPart {
|
|
34
|
+
text: string;
|
|
35
|
+
type: 'text';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface ToolCall {
|
|
39
|
+
function: ToolCallFunction | ToolCallFunctionWithoutArguments | ToolCallFunctionWithoutName;
|
|
40
|
+
id: string;
|
|
41
|
+
type: 'function';
|
|
42
|
+
}
|
|
43
|
+
interface ToolCallFunction {
|
|
44
|
+
arguments: string;
|
|
45
|
+
name: string;
|
|
46
|
+
}
|
|
47
|
+
interface ToolCallFunctionWithoutArguments {
|
|
48
|
+
arguments?: never;
|
|
49
|
+
name: string;
|
|
50
|
+
}
|
|
51
|
+
interface ToolCallFunctionWithoutName {
|
|
52
|
+
arguments: string;
|
|
53
|
+
name?: never;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface AssistantMessage {
|
|
57
|
+
content?: (RefusalContentPart | TextContentPart)[] | string;
|
|
58
|
+
name?: string;
|
|
59
|
+
/** @see {@link https://cookbook.openai.com/articles/gpt-oss/handle-raw-cot#chat-completions-api} */
|
|
60
|
+
reasoning?: string;
|
|
61
|
+
/** @remarks OpenAI doesn't support this, but some providers do. */
|
|
62
|
+
reasoning_content?: string;
|
|
63
|
+
refusal?: string;
|
|
64
|
+
role: 'assistant';
|
|
65
|
+
tool_calls?: ToolCall[];
|
|
66
|
+
}
|
|
67
|
+
interface DeveloperMessage {
|
|
68
|
+
content: string | TextContentPart[];
|
|
69
|
+
name?: string;
|
|
70
|
+
/** @remarks Before using, confirm that your model supports this. */
|
|
71
|
+
role: 'developer';
|
|
72
|
+
}
|
|
73
|
+
type Message = AssistantMessage | DeveloperMessage | SystemMessage | ToolMessage | UserMessage;
|
|
74
|
+
interface SystemMessage {
|
|
75
|
+
content: string | TextContentPart[];
|
|
76
|
+
name?: string;
|
|
77
|
+
role: 'system';
|
|
78
|
+
}
|
|
79
|
+
interface ToolMessage {
|
|
80
|
+
/** @remarks considering the support of ecosystems (such as MCP), we have relaxed this type. */
|
|
81
|
+
content: CommonContentPart[] | string;
|
|
82
|
+
role: 'tool';
|
|
83
|
+
tool_call_id: string;
|
|
84
|
+
}
|
|
85
|
+
interface UserMessage {
|
|
86
|
+
content: CommonContentPart[] | string;
|
|
87
|
+
name?: string;
|
|
88
|
+
role: 'user';
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
interface CompletionToolCall {
|
|
92
|
+
args: string;
|
|
93
|
+
toolCallId: string;
|
|
94
|
+
toolCallType: 'function';
|
|
95
|
+
toolName: string;
|
|
96
|
+
}
|
|
97
|
+
interface CompletionToolResult {
|
|
98
|
+
args: Record<string, unknown>;
|
|
99
|
+
result: ToolMessage['content'];
|
|
100
|
+
toolCallId: string;
|
|
101
|
+
toolName: string;
|
|
102
|
+
}
|
|
103
|
+
interface Tool {
|
|
104
|
+
execute: (input: unknown, options: ToolExecuteOptions) => Promise<ToolExecuteResult> | ToolExecuteResult;
|
|
105
|
+
function: {
|
|
106
|
+
description?: string;
|
|
107
|
+
name: string;
|
|
108
|
+
parameters: Record<string, unknown>;
|
|
109
|
+
strict?: boolean;
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
112
|
+
* Default support for 'function', extended to include custom types such as 'builtin_function'.
|
|
113
|
+
*
|
|
114
|
+
* @see Issue #269
|
|
115
|
+
*/
|
|
116
|
+
type: 'function' | 'builtin_function' | string;
|
|
117
|
+
}
|
|
118
|
+
interface ToolExecuteOptions {
|
|
119
|
+
abortSignal?: AbortSignal;
|
|
120
|
+
messages: Message[];
|
|
121
|
+
toolCallId: string;
|
|
122
|
+
}
|
|
123
|
+
type ToolExecuteResult = object | string | unknown[];
|
|
124
|
+
|
|
125
|
+
interface Usage {
|
|
126
|
+
completion_tokens: number;
|
|
127
|
+
prompt_tokens: number;
|
|
128
|
+
total_tokens: number;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
type CompletionStep<T extends boolean = false> = (T extends true ? {
|
|
132
|
+
usage: Usage;
|
|
133
|
+
} : {
|
|
134
|
+
usage?: Usage;
|
|
135
|
+
}) & {
|
|
136
|
+
finishReason: FinishReason;
|
|
137
|
+
stepType: CompletionStepType;
|
|
138
|
+
text?: string;
|
|
139
|
+
toolCalls: CompletionToolCall[];
|
|
140
|
+
toolResults: CompletionToolResult[];
|
|
141
|
+
};
|
|
142
|
+
type CompletionStepType = 'continue' | 'done' | 'initial' | 'tool-result';
|
|
143
|
+
|
|
144
|
+
type ToolChoice = AllowedTools | ToolChoiceMode | ToolChoiceTool;
|
|
145
|
+
interface AllowedTools {
|
|
146
|
+
mode: 'auto' | 'required';
|
|
147
|
+
tools: ToolChoiceTool[];
|
|
148
|
+
type: 'allowed_tools';
|
|
149
|
+
}
|
|
150
|
+
type ToolChoiceMode = 'auto' | 'none' | 'required';
|
|
151
|
+
interface ToolChoiceTool {
|
|
152
|
+
function: {
|
|
153
|
+
name: string;
|
|
154
|
+
};
|
|
155
|
+
type: 'function';
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** @see {@link https://platform.openai.com/docs/api-reference/chat/create} */
|
|
159
|
+
interface ChatOptions extends CommonRequestOptions {
|
|
160
|
+
/**
|
|
161
|
+
* number between -2.0 and 2.0.
|
|
162
|
+
* @default 0
|
|
163
|
+
*/
|
|
164
|
+
frequencyPenalty?: number;
|
|
165
|
+
messages: Message[];
|
|
166
|
+
/** Whether to enable parallel function calling during tool use. */
|
|
167
|
+
parallelToolCalls?: boolean;
|
|
168
|
+
/**
|
|
169
|
+
* number between -2.0 and 2.0.
|
|
170
|
+
* @default 0
|
|
171
|
+
*/
|
|
172
|
+
presencePenalty?: number;
|
|
173
|
+
/** Constrains effort on reasoning for reasoning models. */
|
|
174
|
+
reasoningEffort?: 'high' | 'medium' | 'minimal' | 'none' | 'xhigh';
|
|
175
|
+
seed?: number;
|
|
176
|
+
/** up to 4 sequences where the API will stop generating further tokens. */
|
|
177
|
+
stop?: [string, string, string, string] | [string, string, string] | [string, string] | [string] | string;
|
|
178
|
+
/**
|
|
179
|
+
* what sampling temperature to use, between 0 and 2.
|
|
180
|
+
* @default 1
|
|
181
|
+
*/
|
|
182
|
+
temperature?: number;
|
|
183
|
+
/** Controls which (if any) tool is called by the model. */
|
|
184
|
+
toolChoice?: ToolChoice;
|
|
185
|
+
/** A list of tools the model may call. */
|
|
186
|
+
tools?: Tool[];
|
|
187
|
+
/** @remarks OpenAI doesn't support this, but some providers do. */
|
|
188
|
+
topK?: number;
|
|
189
|
+
/** @default 1 */
|
|
190
|
+
topP?: number;
|
|
191
|
+
}
|
|
192
|
+
declare const chat: <T extends WithUnknown<ChatOptions>>(options: T) => Promise<Response>;
|
|
193
|
+
|
|
194
|
+
interface DetermineStepTypeOptions {
|
|
195
|
+
finishReason: FinishReason;
|
|
196
|
+
maxSteps: number;
|
|
197
|
+
stepsLength: number;
|
|
198
|
+
toolCallsLength: number;
|
|
199
|
+
}
|
|
200
|
+
/** @internal */
|
|
201
|
+
declare const determineStepType: ({ finishReason, maxSteps, stepsLength, toolCallsLength }: DetermineStepTypeOptions) => CompletionStepType;
|
|
202
|
+
|
|
203
|
+
interface ExecuteToolOptions {
|
|
204
|
+
abortSignal?: AbortSignal;
|
|
205
|
+
messages: Message[];
|
|
206
|
+
toolCall: ToolCall;
|
|
207
|
+
tools?: Tool[];
|
|
208
|
+
}
|
|
209
|
+
interface ExecuteToolResult {
|
|
210
|
+
completionToolCall: CompletionToolCall;
|
|
211
|
+
completionToolResult: CompletionToolResult;
|
|
212
|
+
message: ToolMessage;
|
|
213
|
+
}
|
|
214
|
+
declare const executeTool: ({ abortSignal, messages, toolCall, tools }: ExecuteToolOptions) => Promise<ExecuteToolResult>;
|
|
215
|
+
|
|
216
|
+
export { chat, determineStepType, executeTool };
|
|
217
|
+
export type { AssistantMessage, AudioContentPart, ChatOptions, CommonContentPart, CompletionStep, CompletionStepType, CompletionToolCall, CompletionToolResult, DetermineStepTypeOptions, DeveloperMessage, ExecuteToolOptions, ExecuteToolResult, FileContentPart, FinishReason, ImageContentPart, Message, RefusalContentPart, SystemMessage, TextContentPart, Tool, ToolCall, ToolChoice, ToolExecuteOptions, ToolExecuteResult, ToolMessage, Usage, UserMessage };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { requestURL, requestHeaders, requestBody, clean, responseCatch } from '@xsai/shared';
|
|
2
|
+
|
|
3
|
+
const chat = async (options) => (options.fetch ?? globalThis.fetch)(requestURL("chat/completions", options.baseURL), {
|
|
4
|
+
body: requestBody({
|
|
5
|
+
...options,
|
|
6
|
+
tools: options.tools?.map((tool) => ({
|
|
7
|
+
function: clean({
|
|
8
|
+
...tool.function,
|
|
9
|
+
returns: void 0
|
|
10
|
+
}),
|
|
11
|
+
/**
|
|
12
|
+
* Fix #269: Dynamically read the tool type instead of hardcoding it, to support extension types like 'builtin_function'.
|
|
13
|
+
*/
|
|
14
|
+
type: tool.type ?? "function"
|
|
15
|
+
}))
|
|
16
|
+
}),
|
|
17
|
+
headers: requestHeaders({
|
|
18
|
+
"Content-Type": "application/json",
|
|
19
|
+
...options.headers
|
|
20
|
+
}, options.apiKey),
|
|
21
|
+
method: "POST",
|
|
22
|
+
signal: options.abortSignal
|
|
23
|
+
}).then(responseCatch);
|
|
24
|
+
|
|
25
|
+
const determineStepType = ({ finishReason, maxSteps, stepsLength, toolCallsLength }) => {
|
|
26
|
+
if (stepsLength === 0) {
|
|
27
|
+
return "initial";
|
|
28
|
+
} else if (stepsLength < maxSteps) {
|
|
29
|
+
if (toolCallsLength > 0 && finishReason === "tool_calls")
|
|
30
|
+
return "tool-result";
|
|
31
|
+
else if (!["error", "length"].includes(finishReason))
|
|
32
|
+
return "continue";
|
|
33
|
+
}
|
|
34
|
+
return "done";
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const wrapToolResult = (result) => {
|
|
38
|
+
if (typeof result === "string")
|
|
39
|
+
return result;
|
|
40
|
+
if (Array.isArray(result)) {
|
|
41
|
+
if (result.every((item) => !!(typeof item === "object" && "type" in item && ["file", "image_url", "input_audio", "text"].includes(item.type)))) {
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return JSON.stringify(result);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const 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
|
+
if (toolCall.function.name == null)
|
|
56
|
+
throw new Error(`Missing toolCall.function.name: ${JSON.stringify(toolCall)}`);
|
|
57
|
+
if (toolCall.function.arguments == null)
|
|
58
|
+
throw new Error(`Missing toolCall.function.arguments: ${JSON.stringify(toolCall)}`);
|
|
59
|
+
const parsedArgs = JSON.parse(toolCall.function.arguments.trim() || "{}");
|
|
60
|
+
const result = wrapToolResult(await tool.execute(parsedArgs, {
|
|
61
|
+
abortSignal,
|
|
62
|
+
messages,
|
|
63
|
+
toolCallId: toolCall.id
|
|
64
|
+
}));
|
|
65
|
+
const completionToolCall = {
|
|
66
|
+
args: toolCall.function.arguments,
|
|
67
|
+
toolCallId: toolCall.id,
|
|
68
|
+
toolCallType: toolCall.type,
|
|
69
|
+
toolName: toolCall.function.name
|
|
70
|
+
};
|
|
71
|
+
const completionToolResult = {
|
|
72
|
+
args: parsedArgs,
|
|
73
|
+
result,
|
|
74
|
+
toolCallId: toolCall.id,
|
|
75
|
+
toolName: toolCall.function.name
|
|
76
|
+
};
|
|
77
|
+
const message = {
|
|
78
|
+
content: result,
|
|
79
|
+
role: "tool",
|
|
80
|
+
tool_call_id: toolCall.id
|
|
81
|
+
};
|
|
82
|
+
return {
|
|
83
|
+
completionToolCall,
|
|
84
|
+
completionToolResult,
|
|
85
|
+
message
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export { chat, determineStepType, executeTool };
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wyatex/xsai-shared-chat",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.4.3-fix-1",
|
|
5
|
+
"description": "extra-small AI SDK.",
|
|
6
|
+
"author": "Moeru AI",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"homepage": "https://xsai.js.org",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/moeru-ai/xsai.git",
|
|
12
|
+
"directory": "packages/shared-chat"
|
|
13
|
+
},
|
|
14
|
+
"bugs": "https://github.com/moeru-ai/xsai/issues",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"xsai",
|
|
17
|
+
"openai",
|
|
18
|
+
"ai"
|
|
19
|
+
],
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"exports": "./src/index.ts",
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"default": "./dist/index.js"
|
|
30
|
+
},
|
|
31
|
+
"./package.json": "./package.json"
|
|
32
|
+
},
|
|
33
|
+
"main": "./dist/index.js",
|
|
34
|
+
"types": "./dist/index.d.ts"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "pkgroll"
|
|
38
|
+
},
|
|
39
|
+
"dependencies": {
|
|
40
|
+
"@xsai/shared": "workspace:~"
|
|
41
|
+
}
|
|
42
|
+
}
|