@xsai/shared-chat 0.0.22 → 0.0.24
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 +21 -10
- package/dist/index.js +25 -32
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -75,7 +75,7 @@ type ToolChoice = 'auto' | 'none' | 'required' | {
|
|
|
75
75
|
type: 'function';
|
|
76
76
|
};
|
|
77
77
|
|
|
78
|
-
interface
|
|
78
|
+
interface ChatOptions extends CommonRequestOptions {
|
|
79
79
|
[key: string]: unknown;
|
|
80
80
|
messages: Message[];
|
|
81
81
|
toolChoice?: ToolChoice;
|
|
@@ -94,14 +94,25 @@ interface Tool {
|
|
|
94
94
|
type: 'function';
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
-
declare const
|
|
97
|
+
declare const chat: <T extends ChatOptions>(options: T) => Promise<Response>;
|
|
98
98
|
|
|
99
|
-
declare
|
|
100
|
-
declare
|
|
101
|
-
declare
|
|
102
|
-
declare
|
|
103
|
-
declare
|
|
104
|
-
declare
|
|
105
|
-
declare
|
|
99
|
+
declare const messages: (...messages: Message[]) => Message[];
|
|
100
|
+
declare const system: <C extends string | SystemMessagePart[]>(content: C) => SystemMessage;
|
|
101
|
+
declare const user: <C extends Array<UserMessagePart> | string>(content: C) => UserMessage;
|
|
102
|
+
declare const textPart: (text: string) => TextPart;
|
|
103
|
+
declare const imagePart: (url: string) => ImagePart;
|
|
104
|
+
declare const assistant: <C extends AssistantMessagePart[] | string | ToolCall>(content: C) => AssistantMessage;
|
|
105
|
+
declare const tool: <C extends string | ToolMessagePart[]>(content: C, toolCall: ToolCall) => ToolMessage;
|
|
106
106
|
|
|
107
|
-
|
|
107
|
+
declare const message_assistant: typeof assistant;
|
|
108
|
+
declare const message_imagePart: typeof imagePart;
|
|
109
|
+
declare const message_messages: typeof messages;
|
|
110
|
+
declare const message_system: typeof system;
|
|
111
|
+
declare const message_textPart: typeof textPart;
|
|
112
|
+
declare const message_tool: typeof tool;
|
|
113
|
+
declare const message_user: typeof user;
|
|
114
|
+
declare namespace message {
|
|
115
|
+
export { message_assistant as assistant, message_imagePart as imagePart, message_messages as messages, message_system as system, message_textPart as textPart, message_tool as tool, message_user as user };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export { type AssistantMessage, type AssistantMessagePart, type AssistantMessageResponse, type AudioBase64, type AudioPart, type ChatOptions, type CommonMessage, type CommonPart, 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 ToolMessage, type ToolMessagePart, type UserMessage, type UserMessagePart, chat, message };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { requestBody, requestHeaders } from '@xsai/shared';
|
|
1
|
+
import { requestURL, requestBody, requestHeaders, responseCatch } from '@xsai/shared';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const chat = async (options) => await (options.fetch ?? globalThis.fetch)(requestURL("chat/completions", options.baseURL), {
|
|
4
4
|
body: requestBody({
|
|
5
5
|
...options,
|
|
6
6
|
tools: options.tools?.map((tool) => ({
|
|
@@ -14,36 +14,29 @@ const chatCompletion = async (options) => await (options.fetch ?? globalThis.fet
|
|
|
14
14
|
}, options.apiKey),
|
|
15
15
|
method: "POST",
|
|
16
16
|
signal: options.abortSignal
|
|
17
|
+
}).then(responseCatch);
|
|
18
|
+
|
|
19
|
+
const messages = (...messages2) => messages2;
|
|
20
|
+
const system = (content) => ({ content, role: "system" });
|
|
21
|
+
const user = (content) => ({ content, role: "user" });
|
|
22
|
+
const textPart = (text) => ({ text, type: "text" });
|
|
23
|
+
const imagePart = (url) => ({ image_url: { url }, type: "image_url" });
|
|
24
|
+
const assistant = (content) => typeof content === "string" || Array.isArray(content) ? { content, role: "assistant" } : { role: "assistant", tool_calls: [content] };
|
|
25
|
+
const tool = (content, toolCall) => ({
|
|
26
|
+
content,
|
|
27
|
+
role: "tool",
|
|
28
|
+
tool_call_id: toolCall.id
|
|
17
29
|
});
|
|
18
30
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return { text, type: "text" };
|
|
30
|
-
}
|
|
31
|
-
function imagePart(url) {
|
|
32
|
-
return { image_url: { url }, type: "image_url" };
|
|
33
|
-
}
|
|
34
|
-
function assistant(content) {
|
|
35
|
-
if (typeof content === "string")
|
|
36
|
-
return { content, role: "assistant" };
|
|
37
|
-
if (Array.isArray(content))
|
|
38
|
-
return { content, role: "assistant" };
|
|
39
|
-
return { role: "assistant", tool_calls: [content] };
|
|
40
|
-
}
|
|
41
|
-
function tool(content, toolCall) {
|
|
42
|
-
return {
|
|
43
|
-
content,
|
|
44
|
-
role: "tool",
|
|
45
|
-
tool_call_id: toolCall.id
|
|
46
|
-
};
|
|
47
|
-
}
|
|
31
|
+
var message = /*#__PURE__*/Object.freeze({
|
|
32
|
+
__proto__: null,
|
|
33
|
+
assistant: assistant,
|
|
34
|
+
imagePart: imagePart,
|
|
35
|
+
messages: messages,
|
|
36
|
+
system: system,
|
|
37
|
+
textPart: textPart,
|
|
38
|
+
tool: tool,
|
|
39
|
+
user: user
|
|
40
|
+
});
|
|
48
41
|
|
|
49
|
-
export {
|
|
42
|
+
export { chat, message };
|