@xsai/shared 0.5.0-beta.3 → 0.5.0-beta.5

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
@@ -17,7 +17,7 @@ interface InvalidToolCallErrorOptions extends ErrorOptions {
17
17
  toolCall: unknown;
18
18
  toolName?: string;
19
19
  }
20
- type InvalidToolCallReason = 'missing_arguments' | 'missing_name' | 'unknown_tool';
20
+ type InvalidToolCallReason = 'missing_arguments' | 'missing_name' | 'tool_call_id_mismatch' | 'unknown_tool';
21
21
  interface InvalidToolInputErrorOptions extends ErrorOptions {
22
22
  toolInput: unknown;
23
23
  toolName: string;
@@ -34,8 +34,8 @@ interface ToolExecutionErrorOptions extends InvalidToolInputErrorOptions {
34
34
  }
35
35
  type XSAIErrorCode = 'api_call_error' | 'invalid_response' | 'invalid_tool_call' | 'invalid_tool_input' | 'json_parse_error' | 'remote_api_error' | 'tool_execution_error';
36
36
  declare class XSAIError extends Error {
37
- code: XSAIErrorCode;
38
- constructor(message: string, code: XSAIErrorCode, options?: ErrorOptions);
37
+ code: (string & {}) | XSAIErrorCode;
38
+ constructor(message: string, code: (string & {}) | XSAIErrorCode, options?: ErrorOptions);
39
39
  static isInstance<T extends abstract new (...args: any[]) => XSAIError>(this: T, error: unknown): error is InstanceType<T>;
40
40
  }
41
41
  declare class APICallError extends XSAIError {
@@ -135,6 +135,8 @@ declare class DelayedPromise<T> {
135
135
  resolve(value: T): void;
136
136
  }
137
137
 
138
+ declare const postJSON: (path: string, options: WithUnknown<CommonRequestOptions>) => Promise<Response>;
139
+
138
140
  declare const requestBody: (body: Record<string, unknown>) => string;
139
141
 
140
142
  declare const requestHeaders: (headers?: CommonRequestOptions["headers"], apiKey?: CommonRequestOptions["apiKey"]) => Record<"Authorization", never>;
@@ -148,5 +150,5 @@ declare const responseJSON: <T>(res: Response) => Promise<T>;
148
150
  type TrampolineFn<T> = (() => Promise<TrampolineFn<T>> | TrampolineFn<T>) | Promise<T> | T;
149
151
  declare const trampoline: <T>(fn: () => Promise<TrampolineFn<T>> | TrampolineFn<T>) => Promise<T>;
150
152
 
151
- export { APICallError, DelayedPromise, InvalidResponseError, InvalidToolCallError, InvalidToolInputError, JSONParseError, RemoteAPIError, ToolExecutionError, XSAIError, clean, objCamelToSnake, requestBody, requestHeaders, requestURL, responseCatch, responseJSON, strCamelToSnake, trampoline };
153
+ export { APICallError, DelayedPromise, InvalidResponseError, InvalidToolCallError, InvalidToolInputError, JSONParseError, RemoteAPIError, ToolExecutionError, XSAIError, clean, objCamelToSnake, postJSON, requestBody, requestHeaders, requestURL, responseCatch, responseJSON, strCamelToSnake, trampoline };
152
154
  export type { APICallErrorOptions, CommonRequestOptions, Fetch, InvalidResponseErrorOptions, InvalidResponseReason, InvalidToolCallErrorOptions, InvalidToolCallReason, InvalidToolInputErrorOptions, JSONParseErrorOptions, RemoteAPIErrorOptions, ToolExecutionErrorOptions, TrampolineFn, WithUnknown, XSAIErrorCode };
package/dist/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  class XSAIError extends Error {
2
- code;
3
2
  constructor(message, code, options = {}) {
4
3
  super(message, { cause: options.cause });
5
4
  this.code = code;
@@ -10,13 +9,6 @@ class XSAIError extends Error {
10
9
  }
11
10
  }
12
11
  class APICallError extends XSAIError {
13
- requestBody;
14
- response;
15
- responseBody;
16
- responseHeaders;
17
- statusCode;
18
- statusText;
19
- url;
20
12
  constructor(message, options) {
21
13
  super(message, "api_call_error", options);
22
14
  this.requestBody = options.requestBody;
@@ -29,48 +21,36 @@ class APICallError extends XSAIError {
29
21
  }
30
22
  }
31
23
  class InvalidResponseError extends XSAIError {
32
- body;
33
- contentType;
34
- response;
35
- responseBody;
36
24
  constructor(message, options) {
37
25
  super(message, "invalid_response", options);
38
26
  Object.assign(this, options);
39
27
  }
40
28
  }
41
29
  class InvalidToolCallError extends XSAIError {
42
- availableTools;
43
- toolCall;
44
- toolName;
45
30
  constructor(message, options) {
46
31
  super(message, "invalid_tool_call", options);
47
32
  Object.assign(this, options);
48
33
  }
49
34
  }
50
35
  class InvalidToolInputError extends XSAIError {
51
- toolInput;
52
36
  constructor(message, options) {
53
37
  super(message, "invalid_tool_input", options);
54
38
  Object.assign(this, options);
55
39
  }
56
40
  }
57
41
  class JSONParseError extends XSAIError {
58
- text;
59
42
  constructor(message, options) {
60
43
  super(message, "json_parse_error", options);
61
44
  this.text = options.text;
62
45
  }
63
46
  }
64
47
  class RemoteAPIError extends XSAIError {
65
- response;
66
48
  constructor(message, options) {
67
49
  super(message, "remote_api_error", options);
68
50
  Object.assign(this, options);
69
51
  }
70
52
  }
71
53
  class ToolExecutionError extends XSAIError {
72
- toolCallId;
73
- toolInput;
74
54
  constructor(message, options) {
75
55
  super(message, "tool_execution_error", options);
76
56
  Object.assign(this, options);
@@ -97,9 +77,6 @@ class DelayedPromise {
97
77
  });
98
78
  return this._promise;
99
79
  }
100
- _promise;
101
- _reject;
102
- _resolve;
103
80
  status = { type: "pending" };
104
81
  reject(error) {
105
82
  this.status = { error, type: "rejected" };
@@ -160,6 +137,16 @@ const responseCatch = async (res) => {
160
137
  return res;
161
138
  };
162
139
 
140
+ const postJSON = async (path, options) => (options.fetch ?? globalThis.fetch)(requestURL(path, options.baseURL), {
141
+ body: requestBody(options),
142
+ headers: requestHeaders({
143
+ "Content-Type": "application/json",
144
+ ...options.headers
145
+ }, options.apiKey),
146
+ method: "POST",
147
+ signal: options.abortSignal
148
+ }).then(responseCatch);
149
+
163
150
  const responseJSON = async (res) => {
164
151
  const text = await res.text();
165
152
  try {
@@ -179,4 +166,4 @@ const trampoline = async (fn) => {
179
166
  return result;
180
167
  };
181
168
 
182
- export { APICallError, DelayedPromise, InvalidResponseError, InvalidToolCallError, InvalidToolInputError, JSONParseError, RemoteAPIError, ToolExecutionError, XSAIError, clean, objCamelToSnake, requestBody, requestHeaders, requestURL, responseCatch, responseJSON, strCamelToSnake, trampoline };
169
+ export { APICallError, DelayedPromise, InvalidResponseError, InvalidToolCallError, InvalidToolInputError, JSONParseError, RemoteAPIError, ToolExecutionError, XSAIError, clean, objCamelToSnake, postJSON, 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.5.0-beta.3",
4
+ "version": "0.5.0-beta.5",
5
5
  "description": "extra-small AI SDK.",
6
6
  "author": "Moeru AI",
7
7
  "license": "MIT",