@xsai/shared 0.4.4 → 0.5.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 +84 -3
- package/dist/index.js +101 -12
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,87 @@
|
|
|
1
|
+
interface APICallErrorOptions extends ErrorOptions {
|
|
2
|
+
requestBody?: string;
|
|
3
|
+
response: Response;
|
|
4
|
+
responseBody?: string;
|
|
5
|
+
}
|
|
6
|
+
interface InvalidResponseErrorOptions extends ErrorOptions {
|
|
7
|
+
body?: unknown;
|
|
8
|
+
contentType?: null | string;
|
|
9
|
+
reason: InvalidResponseReason;
|
|
10
|
+
response?: Response;
|
|
11
|
+
responseBody?: string;
|
|
12
|
+
}
|
|
13
|
+
type InvalidResponseReason = 'empty_body' | 'invalid_body' | 'no_choices';
|
|
14
|
+
interface InvalidToolCallErrorOptions extends ErrorOptions {
|
|
15
|
+
availableTools?: string[];
|
|
16
|
+
reason: InvalidToolCallReason;
|
|
17
|
+
toolCall: unknown;
|
|
18
|
+
toolName?: string;
|
|
19
|
+
}
|
|
20
|
+
type InvalidToolCallReason = 'missing_arguments' | 'missing_name' | 'unknown_tool';
|
|
21
|
+
interface InvalidToolInputErrorOptions extends ErrorOptions {
|
|
22
|
+
toolInput: unknown;
|
|
23
|
+
toolName: string;
|
|
24
|
+
}
|
|
25
|
+
interface JSONParseErrorOptions extends ErrorOptions {
|
|
26
|
+
text: string;
|
|
27
|
+
}
|
|
28
|
+
interface RemoteAPIErrorOptions extends ErrorOptions {
|
|
29
|
+
response?: Response;
|
|
30
|
+
responseBody: string;
|
|
31
|
+
}
|
|
32
|
+
interface ToolExecutionErrorOptions extends InvalidToolInputErrorOptions {
|
|
33
|
+
toolCallId?: string;
|
|
34
|
+
}
|
|
35
|
+
type XSAIErrorCode = 'api_call_error' | 'invalid_response' | 'invalid_tool_call' | 'invalid_tool_input' | 'json_parse_error' | 'remote_api_error' | 'tool_execution_error';
|
|
1
36
|
declare class XSAIError extends Error {
|
|
37
|
+
code: XSAIErrorCode;
|
|
38
|
+
constructor(message: string, code: XSAIErrorCode, options?: ErrorOptions);
|
|
39
|
+
static isInstance<T extends abstract new (...args: any[]) => XSAIError>(this: T, error: unknown): error is InstanceType<T>;
|
|
40
|
+
}
|
|
41
|
+
declare class APICallError extends XSAIError {
|
|
42
|
+
requestBody?: string;
|
|
43
|
+
response: Response;
|
|
44
|
+
responseBody?: string;
|
|
45
|
+
responseHeaders: Record<string, string>;
|
|
46
|
+
statusCode: number;
|
|
47
|
+
statusText: string;
|
|
48
|
+
url: string;
|
|
49
|
+
constructor(message: string, options: APICallErrorOptions);
|
|
50
|
+
}
|
|
51
|
+
declare class InvalidResponseError extends XSAIError {
|
|
52
|
+
body?: unknown;
|
|
53
|
+
contentType?: null | string;
|
|
54
|
+
reason: InvalidResponseErrorOptions['reason'];
|
|
55
|
+
response?: Response;
|
|
56
|
+
responseBody?: string;
|
|
57
|
+
constructor(message: string, options: InvalidResponseErrorOptions);
|
|
58
|
+
}
|
|
59
|
+
declare class InvalidToolCallError extends XSAIError {
|
|
60
|
+
availableTools?: string[];
|
|
61
|
+
reason: InvalidToolCallErrorOptions['reason'];
|
|
62
|
+
toolCall: unknown;
|
|
63
|
+
toolName?: string;
|
|
64
|
+
constructor(message: string, options: InvalidToolCallErrorOptions);
|
|
65
|
+
}
|
|
66
|
+
declare class InvalidToolInputError extends XSAIError {
|
|
67
|
+
toolInput: unknown;
|
|
68
|
+
toolName: string;
|
|
69
|
+
constructor(message: string, options: InvalidToolInputErrorOptions);
|
|
70
|
+
}
|
|
71
|
+
declare class JSONParseError extends XSAIError {
|
|
72
|
+
text: string;
|
|
73
|
+
constructor(message: string, options: JSONParseErrorOptions);
|
|
74
|
+
}
|
|
75
|
+
declare class RemoteAPIError extends XSAIError {
|
|
2
76
|
response?: Response;
|
|
3
|
-
|
|
77
|
+
responseBody: string;
|
|
78
|
+
constructor(message: string, options: RemoteAPIErrorOptions);
|
|
79
|
+
}
|
|
80
|
+
declare class ToolExecutionError extends XSAIError {
|
|
81
|
+
toolCallId?: string;
|
|
82
|
+
toolInput: unknown;
|
|
83
|
+
toolName: string;
|
|
84
|
+
constructor(message: string, options: ToolExecutionErrorOptions);
|
|
4
85
|
}
|
|
5
86
|
|
|
6
87
|
type Fetch = (input: URL, init: RequestInit) => Promise<Response>;
|
|
@@ -67,5 +148,5 @@ declare const responseJSON: <T>(res: Response) => Promise<T>;
|
|
|
67
148
|
type TrampolineFn<T> = (() => Promise<TrampolineFn<T>> | TrampolineFn<T>) | Promise<T> | T;
|
|
68
149
|
declare const trampoline: <T>(fn: () => Promise<TrampolineFn<T>> | TrampolineFn<T>) => Promise<T>;
|
|
69
150
|
|
|
70
|
-
export { DelayedPromise, XSAIError, clean, objCamelToSnake, requestBody, requestHeaders, requestURL, responseCatch, responseJSON, strCamelToSnake, trampoline };
|
|
71
|
-
export type { CommonRequestOptions, Fetch, TrampolineFn, WithUnknown };
|
|
151
|
+
export { APICallError, DelayedPromise, InvalidResponseError, InvalidToolCallError, InvalidToolInputError, JSONParseError, RemoteAPIError, ToolExecutionError, XSAIError, clean, objCamelToSnake, requestBody, requestHeaders, requestURL, responseCatch, responseJSON, strCamelToSnake, trampoline };
|
|
152
|
+
export type { APICallErrorOptions, CommonRequestOptions, Fetch, InvalidResponseErrorOptions, InvalidResponseReason, InvalidToolCallErrorOptions, InvalidToolCallReason, InvalidToolInputErrorOptions, JSONParseErrorOptions, RemoteAPIErrorOptions, ToolExecutionErrorOptions, TrampolineFn, WithUnknown, XSAIErrorCode };
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,79 @@
|
|
|
1
1
|
class XSAIError extends Error {
|
|
2
|
+
code;
|
|
3
|
+
constructor(message, code, options = {}) {
|
|
4
|
+
super(message, { cause: options.cause });
|
|
5
|
+
this.code = code;
|
|
6
|
+
this.name = new.target.name;
|
|
7
|
+
}
|
|
8
|
+
static isInstance(error) {
|
|
9
|
+
return error instanceof this;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
class APICallError extends XSAIError {
|
|
13
|
+
requestBody;
|
|
14
|
+
response;
|
|
15
|
+
responseBody;
|
|
16
|
+
responseHeaders;
|
|
17
|
+
statusCode;
|
|
18
|
+
statusText;
|
|
19
|
+
url;
|
|
20
|
+
constructor(message, options) {
|
|
21
|
+
super(message, "api_call_error", options);
|
|
22
|
+
this.requestBody = options.requestBody;
|
|
23
|
+
this.response = options.response;
|
|
24
|
+
this.responseBody = options.responseBody;
|
|
25
|
+
this.responseHeaders = Object.fromEntries(options.response.headers.entries());
|
|
26
|
+
this.statusCode = options.response.status;
|
|
27
|
+
this.statusText = options.response.statusText;
|
|
28
|
+
this.url = options.response.url;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
class InvalidResponseError extends XSAIError {
|
|
32
|
+
body;
|
|
33
|
+
contentType;
|
|
34
|
+
response;
|
|
35
|
+
responseBody;
|
|
36
|
+
constructor(message, options) {
|
|
37
|
+
super(message, "invalid_response", options);
|
|
38
|
+
Object.assign(this, options);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
class InvalidToolCallError extends XSAIError {
|
|
42
|
+
availableTools;
|
|
43
|
+
toolCall;
|
|
44
|
+
toolName;
|
|
45
|
+
constructor(message, options) {
|
|
46
|
+
super(message, "invalid_tool_call", options);
|
|
47
|
+
Object.assign(this, options);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
class InvalidToolInputError extends XSAIError {
|
|
51
|
+
toolInput;
|
|
52
|
+
constructor(message, options) {
|
|
53
|
+
super(message, "invalid_tool_input", options);
|
|
54
|
+
Object.assign(this, options);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
class JSONParseError extends XSAIError {
|
|
58
|
+
text;
|
|
59
|
+
constructor(message, options) {
|
|
60
|
+
super(message, "json_parse_error", options);
|
|
61
|
+
this.text = options.text;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
class RemoteAPIError extends XSAIError {
|
|
2
65
|
response;
|
|
3
|
-
constructor(message,
|
|
4
|
-
super(message,
|
|
5
|
-
this
|
|
6
|
-
|
|
66
|
+
constructor(message, options) {
|
|
67
|
+
super(message, "remote_api_error", options);
|
|
68
|
+
Object.assign(this, options);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
class ToolExecutionError extends XSAIError {
|
|
72
|
+
toolCallId;
|
|
73
|
+
toolInput;
|
|
74
|
+
constructor(message, options) {
|
|
75
|
+
super(message, "tool_execution_error", options);
|
|
76
|
+
Object.assign(this, options);
|
|
7
77
|
}
|
|
8
78
|
}
|
|
9
79
|
|
|
@@ -67,12 +137,28 @@ const requestURL = (path, baseURL) => {
|
|
|
67
137
|
};
|
|
68
138
|
|
|
69
139
|
const responseCatch = async (res) => {
|
|
70
|
-
if (!res.ok)
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
140
|
+
if (!res.ok) {
|
|
141
|
+
const responseBody = await res.text();
|
|
142
|
+
throw new APICallError(`Remote sent ${res.status} response: ${responseBody}`, {
|
|
143
|
+
response: res,
|
|
144
|
+
responseBody
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
if (!res.body) {
|
|
148
|
+
throw new InvalidResponseError("Response body is empty from remote server", {
|
|
149
|
+
reason: "empty_body",
|
|
150
|
+
response: res
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
if (!(res.body instanceof ReadableStream)) {
|
|
154
|
+
const contentType = res.headers.get("Content-Type");
|
|
155
|
+
throw new InvalidResponseError(`Expected Response body to be a ReadableStream, but got ${String(res.body)}; Content Type is ${contentType}`, {
|
|
156
|
+
body: res.body,
|
|
157
|
+
contentType,
|
|
158
|
+
reason: "invalid_body",
|
|
159
|
+
response: res
|
|
160
|
+
});
|
|
161
|
+
}
|
|
76
162
|
return res;
|
|
77
163
|
};
|
|
78
164
|
|
|
@@ -81,7 +167,10 @@ const responseJSON = async (res) => {
|
|
|
81
167
|
try {
|
|
82
168
|
return JSON.parse(text);
|
|
83
169
|
} catch (cause) {
|
|
84
|
-
throw new
|
|
170
|
+
throw new JSONParseError(`Failed to parse response, response body: ${text}`, {
|
|
171
|
+
cause,
|
|
172
|
+
text
|
|
173
|
+
});
|
|
85
174
|
}
|
|
86
175
|
};
|
|
87
176
|
|
|
@@ -92,4 +181,4 @@ const trampoline = async (fn) => {
|
|
|
92
181
|
return result;
|
|
93
182
|
};
|
|
94
183
|
|
|
95
|
-
export { DelayedPromise, XSAIError, clean, objCamelToSnake, requestBody, requestHeaders, requestURL, responseCatch, responseJSON, strCamelToSnake, trampoline };
|
|
184
|
+
export { APICallError, DelayedPromise, InvalidResponseError, InvalidToolCallError, InvalidToolInputError, JSONParseError, RemoteAPIError, ToolExecutionError, XSAIError, clean, objCamelToSnake, requestBody, requestHeaders, requestURL, responseCatch, responseJSON, strCamelToSnake, trampoline };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xsai/shared",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.5.0-beta.1",
|
|
5
5
|
"description": "extra-small AI SDK.",
|
|
6
6
|
"author": "Moeru AI",
|
|
7
7
|
"license": "MIT",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"dist"
|
|
30
30
|
],
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@moeru/std": "^0.1.0-beta.
|
|
32
|
+
"@moeru/std": "^0.1.0-beta.17"
|
|
33
33
|
},
|
|
34
34
|
"scripts": {
|
|
35
35
|
"build": "pkgroll"
|