@xsai/shared-chat 0.0.22 → 0.0.23

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,5 +1,10 @@
1
1
  import { CommonRequestOptions } from '@xsai/shared';
2
2
 
3
+ declare class ChatError extends Error {
4
+ response?: Response;
5
+ constructor(message: string, response?: Response);
6
+ }
7
+
3
8
  type Part = AudioPart | ImagePart | RefusalPart | TextPart;
4
9
  interface CommonPart<T extends string> {
5
10
  type: T;
@@ -75,7 +80,7 @@ type ToolChoice = 'auto' | 'none' | 'required' | {
75
80
  type: 'function';
76
81
  };
77
82
 
78
- interface ChatCompletionOptions extends CommonRequestOptions {
83
+ interface ChatOptions extends CommonRequestOptions {
79
84
  [key: string]: unknown;
80
85
  messages: Message[];
81
86
  toolChoice?: ToolChoice;
@@ -94,14 +99,25 @@ interface Tool {
94
99
  type: 'function';
95
100
  }
96
101
 
97
- declare const chatCompletion: <T extends ChatCompletionOptions>(options: T) => Promise<Response>;
102
+ declare const chat: <T extends ChatOptions>(options: T) => Promise<Response>;
98
103
 
99
- declare function messages(...messages: Message[]): Message[];
100
- declare function system<C extends string | SystemMessagePart[]>(content: C): SystemMessage;
101
- declare function user<C extends Array<UserMessagePart> | string>(content: C): UserMessage;
102
- declare function textPart(text: string): TextPart;
103
- declare function imagePart(url: string): ImagePart;
104
- declare function assistant<C extends AssistantMessagePart[] | string | ToolCall>(content: C): AssistantMessage;
105
- declare function tool<C extends string | ToolMessagePart[]>(content: C, toolCall: ToolCall): ToolMessage;
104
+ declare const messages: (...messages: Message[]) => Message[];
105
+ declare const system: <C extends string | SystemMessagePart[]>(content: C) => SystemMessage;
106
+ declare const user: <C extends Array<UserMessagePart> | string>(content: C) => UserMessage;
107
+ declare const textPart: (text: string) => TextPart;
108
+ declare const imagePart: (url: string) => ImagePart;
109
+ declare const assistant: <C extends AssistantMessagePart[] | string | ToolCall>(content: C) => AssistantMessage;
110
+ declare const tool: <C extends string | ToolMessagePart[]>(content: C, toolCall: ToolCall) => ToolMessage;
111
+
112
+ declare const message_assistant: typeof assistant;
113
+ declare const message_imagePart: typeof imagePart;
114
+ declare const message_messages: typeof messages;
115
+ declare const message_system: typeof system;
116
+ declare const message_textPart: typeof textPart;
117
+ declare const message_tool: typeof tool;
118
+ declare const message_user: typeof user;
119
+ declare namespace message {
120
+ 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 };
121
+ }
106
122
 
107
- export { type AssistantMessage, type AssistantMessagePart, type AssistantMessageResponse, type AudioBase64, type AudioPart, type ChatCompletionOptions, 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, assistant, chatCompletion, imagePart, messages, system, textPart, tool, user };
123
+ export { type AssistantMessage, type AssistantMessagePart, type AssistantMessageResponse, type AudioBase64, type AudioPart, ChatError, 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,15 @@
1
- import { requestBody, requestHeaders } from '@xsai/shared';
1
+ import { requestURL, requestBody, requestHeaders } from '@xsai/shared';
2
2
 
3
- const chatCompletion = async (options) => await (options.fetch ?? globalThis.fetch)(new URL("chat/completions", options.baseURL), {
3
+ class ChatError extends Error {
4
+ response;
5
+ constructor(message, response) {
6
+ super(message);
7
+ this.name = "ChatError";
8
+ this.response = response;
9
+ }
10
+ }
11
+
12
+ const chat = async (options) => await (options.fetch ?? globalThis.fetch)(requestURL("chat/completions", options.baseURL), {
4
13
  body: requestBody({
5
14
  ...options,
6
15
  tools: options.tools?.map((tool) => ({
@@ -16,34 +25,27 @@ const chatCompletion = async (options) => await (options.fetch ?? globalThis.fet
16
25
  signal: options.abortSignal
17
26
  });
18
27
 
19
- function messages(...messages2) {
20
- return messages2;
21
- }
22
- function system(content) {
23
- return { content, role: "system" };
24
- }
25
- function user(content) {
26
- return { content, role: "user" };
27
- }
28
- function textPart(text) {
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
- }
28
+ const messages = (...messages2) => messages2;
29
+ const system = (content) => ({ content, role: "system" });
30
+ const user = (content) => ({ content, role: "user" });
31
+ const textPart = (text) => ({ text, type: "text" });
32
+ const imagePart = (url) => ({ image_url: { url }, type: "image_url" });
33
+ const assistant = (content) => typeof content === "string" || Array.isArray(content) ? { content, role: "assistant" } : { role: "assistant", tool_calls: [content] };
34
+ const tool = (content, toolCall) => ({
35
+ content,
36
+ role: "tool",
37
+ tool_call_id: toolCall.id
38
+ });
39
+
40
+ var message = /*#__PURE__*/Object.freeze({
41
+ __proto__: null,
42
+ assistant: assistant,
43
+ imagePart: imagePart,
44
+ messages: messages,
45
+ system: system,
46
+ textPart: textPart,
47
+ tool: tool,
48
+ user: user
49
+ });
48
50
 
49
- export { assistant, chatCompletion, imagePart, messages, system, textPart, tool, user };
51
+ export { ChatError, chat, message };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xsai/shared-chat",
3
- "version": "0.0.22",
3
+ "version": "0.0.23",
4
4
  "type": "module",
5
5
  "author": "Moeru AI",
6
6
  "license": "MIT",