@antdv-next/x-sdk 0.0.1 → 0.0.2

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.
Files changed (54) hide show
  1. package/dist/_util/resolveMaybeRef.d.ts +2 -0
  2. package/dist/_util/resolveMaybeRef.js +7 -0
  3. package/dist/_util/types.js +0 -0
  4. package/dist/chat-providers/AbstractChatProvider.d.ts +2 -2
  5. package/dist/chat-providers/AbstractChatProvider.js +42 -0
  6. package/dist/chat-providers/DeepSeekChatProvider.d.ts +2 -2
  7. package/dist/chat-providers/DeepSeekChatProvider.js +59 -0
  8. package/dist/chat-providers/DefaultChatProvider.d.ts +2 -2
  9. package/dist/chat-providers/DefaultChatProvider.js +26 -0
  10. package/dist/chat-providers/OpenAIChatProvider.d.ts +2 -2
  11. package/dist/chat-providers/OpenAIChatProvider.js +49 -0
  12. package/dist/chat-providers/index.js +4 -1
  13. package/dist/index.d.ts +1 -1
  14. package/dist/index.js +7 -3
  15. package/dist/node_modules/vitest/dist/@vitest/expect/index.js +1456 -0
  16. package/dist/node_modules/vitest/dist/@vitest/pretty-format/index.js +884 -0
  17. package/dist/node_modules/vitest/dist/@vitest/runner/chunk-artifact.js +1543 -0
  18. package/dist/node_modules/vitest/dist/@vitest/snapshot/index.js +660 -0
  19. package/dist/node_modules/vitest/dist/@vitest/spy/index.js +384 -0
  20. package/dist/node_modules/vitest/dist/@vitest/utils/chunk-pathe.M-eThtNZ.js +80 -0
  21. package/dist/node_modules/vitest/dist/@vitest/utils/diff.js +1304 -0
  22. package/dist/node_modules/vitest/dist/@vitest/utils/display.js +556 -0
  23. package/dist/node_modules/vitest/dist/@vitest/utils/error.js +27 -0
  24. package/dist/node_modules/vitest/dist/@vitest/utils/helpers.js +179 -0
  25. package/dist/node_modules/vitest/dist/@vitest/utils/offset.js +25 -0
  26. package/dist/node_modules/vitest/dist/@vitest/utils/serialize.js +75 -0
  27. package/dist/node_modules/vitest/dist/@vitest/utils/source-map.js +371 -0
  28. package/dist/node_modules/vitest/dist/@vitest/utils/timers.js +35 -0
  29. package/dist/node_modules/vitest/dist/chunks/_commonjsHelpers.D26ty3Ew.js +4 -0
  30. package/dist/node_modules/vitest/dist/chunks/rpc.MzXet3jl.js +50 -0
  31. package/dist/node_modules/vitest/dist/chunks/test.CBQUpOM3.js +2640 -0
  32. package/dist/node_modules/vitest/dist/chunks/utils.BX5Fg8C4.js +42 -0
  33. package/dist/node_modules/vitest/dist/vendor/chai.js +2872 -0
  34. package/dist/node_modules/vitest/dist/vendor/magic-string.js +1009 -0
  35. package/dist/node_modules/vitest/dist/vendor/tinyrainbow.js +84 -0
  36. package/dist/x-chat/__tests__/index.test.d.ts +1 -0
  37. package/dist/x-chat/__tests__/index.test.js +257 -0
  38. package/dist/x-chat/index.d.ts +14 -23
  39. package/dist/x-chat/index.js +83 -66
  40. package/dist/x-chat/store.d.ts +2 -0
  41. package/dist/{store-C1diHqNH.js → x-chat/store.js} +15 -9
  42. package/dist/x-conversations/__tests__/index.test.d.ts +1 -0
  43. package/dist/x-conversations/__tests__/index.test.js +36 -0
  44. package/dist/x-conversations/index.d.ts +4 -4
  45. package/dist/x-conversations/index.js +37 -1
  46. package/dist/{x-conversations-BrKWhj5r.js → x-conversations/store.js} +2 -31
  47. package/dist/x-request/__tests__/index.test.d.ts +1 -0
  48. package/dist/x-request/__tests__/index.test.js +90 -0
  49. package/dist/x-request/index.d.ts +36 -11
  50. package/dist/x-request/index.js +246 -1
  51. package/dist/x-request/x-fetch.js +18 -0
  52. package/package.json +1 -1
  53. package/dist/chat-providers-5x9Va7p5.js +0 -167
  54. package/dist/x-request-BSFGaKND.js +0 -234
@@ -1,167 +0,0 @@
1
- //#region src/chat-providers/AbstractChatProvider.ts
2
- var AbstractChatProvider = class {
3
- _request;
4
- _getMessagesFn;
5
- _originalCallbacks;
6
- get request() {
7
- return this._request;
8
- }
9
- constructor(config) {
10
- const request = typeof config.request === "function" ? config.request() : config.request;
11
- if (!request.manual) throw new Error("request must be manual");
12
- this._request = request;
13
- this._originalCallbacks = this._request.options?.callbacks;
14
- }
15
- getMessages() {
16
- return this?._getMessagesFn();
17
- }
18
- injectGetMessages(getMessages) {
19
- this._getMessagesFn = getMessages;
20
- }
21
- injectRequest({ onUpdate, onSuccess, onError }) {
22
- const originalOnUpdate = this._originalCallbacks?.onUpdate;
23
- const originalOnSuccess = this._originalCallbacks?.onSuccess;
24
- const originalOnError = this._originalCallbacks?.onError;
25
- this._request.options.callbacks = {
26
- onUpdate: (data, responseHeaders) => {
27
- const msg = onUpdate(data, responseHeaders);
28
- if (originalOnUpdate) originalOnUpdate(data, responseHeaders, msg);
29
- },
30
- onSuccess: (data, responseHeaders) => {
31
- const msg = onSuccess(data, responseHeaders);
32
- if (originalOnSuccess) originalOnSuccess(data, responseHeaders, msg);
33
- },
34
- onError: (error, errorInfo, responseHeaders) => {
35
- const fallbackMsg = onError(error, errorInfo);
36
- if (originalOnError) originalOnError(error, errorInfo, responseHeaders, fallbackMsg);
37
- }
38
- };
39
- }
40
- };
41
- //#endregion
42
- //#region src/chat-providers/DeepSeekChatProvider.ts
43
- /**
44
- * DeepSeek Chat Provider
45
- * @template ChatMessage 消息类型
46
- * @template Input 请求参数类型
47
- * @template Output 响应数据类型
48
- */
49
- var DeepSeekChatProvider = class extends AbstractChatProvider {
50
- transformParams(requestParams, options) {
51
- return {
52
- ...options?.params,
53
- ...requestParams,
54
- messages: this.getMessages()
55
- };
56
- }
57
- transformLocalMessage(requestParams) {
58
- return requestParams?.messages || [];
59
- }
60
- transformMessage(info) {
61
- const { originMessage, chunk, responseHeaders } = info;
62
- let currentContent = "";
63
- let currentThink = "";
64
- let role = "assistant";
65
- try {
66
- let message;
67
- if (responseHeaders.get("content-type")?.includes("text/event-stream")) {
68
- if (chunk && chunk.data?.trim() !== "[DONE]") message = JSON.parse(chunk.data);
69
- } else message = chunk;
70
- if (message) message?.choices?.forEach((choice) => {
71
- if (choice?.delta) {
72
- currentThink = choice.delta.reasoning_content || "";
73
- currentContent += choice.delta.content || "";
74
- role = choice.delta.role;
75
- } else if (choice?.message) {
76
- currentThink = choice.message.reasoning_content || "";
77
- currentContent += choice.message.content || "";
78
- role = choice.message.role;
79
- }
80
- });
81
- } catch (error) {
82
- console.error("transformMessage error", error);
83
- }
84
- let content = "";
85
- let originMessageContent = typeof originMessage?.content === "string" ? originMessage?.content : originMessage?.content.text || "";
86
- if (!originMessageContent && currentThink) content = `\n\n<think>\n\n${currentThink?.replace?.(/^\n{0,2}/, "")}`;
87
- else if (originMessageContent.includes("<think>") && !originMessageContent.includes("</think>") && currentContent) {
88
- originMessageContent = originMessageContent.replace("<think>", "<think status=\"done\">");
89
- content = `${originMessageContent?.replace?.(/[\s\n]{0,2}$/, "")}\n\n</think>\n\n${currentContent}`;
90
- } else content = `${originMessageContent || ""}${currentThink}${currentContent}`;
91
- return {
92
- content,
93
- role: role || "assistant"
94
- };
95
- }
96
- };
97
- //#endregion
98
- //#region src/chat-providers/DefaultChatProvider.ts
99
- var DefaultChatProvider = class extends AbstractChatProvider {
100
- transformParams(requestParams, options) {
101
- if (typeof requestParams !== "object") throw new Error("requestParams must be an object");
102
- return {
103
- ...options?.params,
104
- ...requestParams
105
- };
106
- }
107
- transformLocalMessage(requestParams) {
108
- return requestParams;
109
- }
110
- transformMessage(info) {
111
- const { chunk, chunks, originMessage } = info;
112
- if (chunk) return chunk;
113
- if (Array.isArray(chunks)) {
114
- const chunk = chunks?.length > 0 ? chunks?.[chunks?.length - 1] : void 0;
115
- return originMessage ? originMessage : chunk;
116
- }
117
- return chunks;
118
- }
119
- };
120
- //#endregion
121
- //#region src/chat-providers/OpenAIChatProvider.ts
122
- /**
123
- * LLM OpenAI Compatible Chat Provider
124
- * @template ChatMessage 消息类型
125
- * @template Input 请求参数类型
126
- * @template Output 响应数据类型
127
- */
128
- var OpenAIChatProvider = class extends AbstractChatProvider {
129
- transformParams(requestParams, options) {
130
- return {
131
- ...options?.params,
132
- ...requestParams,
133
- messages: this.getMessages()
134
- };
135
- }
136
- transformLocalMessage(requestParams) {
137
- return requestParams?.messages || [];
138
- }
139
- transformMessage(info) {
140
- const { originMessage, chunk, responseHeaders } = info;
141
- let currentContent = "";
142
- let role = "assistant";
143
- try {
144
- let message;
145
- if (responseHeaders.get("content-type")?.includes("text/event-stream")) {
146
- if (chunk && chunk.data?.trim() !== "[DONE]") message = JSON.parse(chunk.data);
147
- } else message = chunk;
148
- if (message) message?.choices?.forEach((choice) => {
149
- if (choice?.delta) {
150
- currentContent += choice.delta.content || "";
151
- role = choice.delta.role || "assistant";
152
- } else if (choice?.message) {
153
- currentContent += choice.message.content || "";
154
- role = choice.message.role || "assistant";
155
- }
156
- });
157
- } catch (error) {
158
- console.error("transformMessage error", error);
159
- }
160
- return {
161
- content: `${originMessage?.content || ""}${currentContent || ""}`,
162
- role
163
- };
164
- }
165
- };
166
- //#endregion
167
- export { AbstractChatProvider as i, DefaultChatProvider as n, DeepSeekChatProvider as r, OpenAIChatProvider as t };
@@ -1,234 +0,0 @@
1
- import XStream from "./x-stream/index.js";
2
- //#region src/x-request/x-fetch.ts
3
- var XFetch = async (baseURL, options) => {
4
- const { fetch: fetchFn = globalThis.fetch, middlewares = {}, ...requestInit } = options;
5
- if (typeof fetchFn !== "function") throw new Error("The options.fetch must be a typeof fetch function!");
6
- let fetchArgs = [baseURL, requestInit];
7
- if (typeof middlewares.onRequest === "function") fetchArgs = await middlewares.onRequest(...fetchArgs);
8
- let response = await fetchFn(...fetchArgs);
9
- if (typeof middlewares.onResponse === "function") {
10
- const modifiedResponse = await middlewares.onResponse(response);
11
- if (!(modifiedResponse instanceof Response)) throw new Error("The options.onResponse must return a Response instance!");
12
- response = modifiedResponse;
13
- }
14
- if (!response.ok) throw new Error(`Fetch failed with status ${response.status}`);
15
- if (!response.body) throw new Error("The response body is empty.");
16
- return response;
17
- };
18
- //#endregion
19
- //#region src/x-request/index.ts
20
- var globalOptions = {
21
- manual: false,
22
- headers: { "Content-Type": "application/json" }
23
- };
24
- function setXRequestGlobalOptions(options) {
25
- Object.assign(globalOptions, options);
26
- }
27
- var LastEventId = "Last-Event-ID";
28
- var AbstractXRequestClass = class {
29
- baseURL;
30
- options;
31
- constructor(baseURL, options) {
32
- if (!baseURL || typeof baseURL !== "string") throw new Error("The baseURL is not valid!");
33
- this.baseURL = baseURL;
34
- this.options = options || {};
35
- }
36
- };
37
- var XRequestClass = class extends AbstractXRequestClass {
38
- _asyncHandler;
39
- timeoutHandler;
40
- _isTimeout = false;
41
- streamTimeoutHandler;
42
- _isStreamTimeout = false;
43
- abortController;
44
- _isRequesting = false;
45
- _manual = false;
46
- lastManualParams;
47
- retryTimes = 0;
48
- retryTimer;
49
- lastEventId = void 0;
50
- get asyncHandler() {
51
- return this._asyncHandler;
52
- }
53
- get isTimeout() {
54
- return this._isTimeout;
55
- }
56
- set isTimeout(value) {
57
- this._isTimeout = value;
58
- }
59
- get isStreamTimeout() {
60
- return this._isStreamTimeout;
61
- }
62
- set isStreamTimeout(value) {
63
- this._isStreamTimeout = value;
64
- }
65
- get isRequesting() {
66
- return this._isRequesting;
67
- }
68
- get manual() {
69
- return this._manual;
70
- }
71
- constructor(baseURL, options) {
72
- super(baseURL, options);
73
- this._manual = options?.manual || false;
74
- if (!this.manual) this.init();
75
- }
76
- run(params) {
77
- if (this.manual) {
78
- this.resetRetry();
79
- this.lastManualParams = params;
80
- this.init(params);
81
- return true;
82
- }
83
- console.warn("The request is not manual, so it cannot be run!");
84
- return false;
85
- }
86
- abort() {
87
- clearTimeout(this.timeoutHandler);
88
- clearTimeout(this.streamTimeoutHandler);
89
- this.abortController.abort();
90
- }
91
- init(extraParams, extraHeaders) {
92
- this.abortController = new AbortController();
93
- const { callbacks, params, headers = {}, transformStream, fetch, timeout, streamTimeout, middlewares, streamSeparator, partSeparator, kvSeparator, ...otherOptions } = this.options;
94
- const margeHeaders = Object.assign({}, globalOptions.headers || {}, headers, extraHeaders || {});
95
- const requestInit = {
96
- ...otherOptions,
97
- method: "POST",
98
- body: JSON.stringify({
99
- ...params,
100
- ...extraParams
101
- }),
102
- params: {
103
- ...params,
104
- ...extraParams
105
- },
106
- headers: margeHeaders,
107
- signal: this.abortController.signal,
108
- middlewares
109
- };
110
- if (timeout && timeout > 0) this.timeoutHandler = window.setTimeout(() => {
111
- this.isTimeout = true;
112
- this.finishRequest();
113
- callbacks?.onError?.(/* @__PURE__ */ new Error("TimeoutError"));
114
- }, timeout);
115
- this.startRequest();
116
- this._asyncHandler = XFetch(this.baseURL, {
117
- fetch,
118
- ...requestInit
119
- }).then(async (response) => {
120
- clearTimeout(this.timeoutHandler);
121
- if (this.isTimeout) return;
122
- if (transformStream) {
123
- let transformer = transformStream;
124
- if (typeof transformStream === "function") transformer = transformStream(this.baseURL, response.headers);
125
- await this.customResponseHandler(response, callbacks, transformer, streamTimeout, streamSeparator, partSeparator, kvSeparator);
126
- return;
127
- }
128
- const contentType = response.headers.get("content-type") || "";
129
- switch (contentType.split(";")[0].trim()) {
130
- case "text/event-stream":
131
- await this.sseResponseHandler(response, callbacks, streamTimeout, streamSeparator, partSeparator, kvSeparator);
132
- break;
133
- case "application/json":
134
- await this.jsonResponseHandler(response, callbacks);
135
- break;
136
- default: throw new Error(`The response content-type: ${contentType} is not support!`);
137
- }
138
- }).catch((error) => {
139
- clearTimeout(this.timeoutHandler);
140
- this.finishRequest();
141
- const err = error instanceof Error || error instanceof DOMException ? error : /* @__PURE__ */ new Error("Unknown error!");
142
- const returnOfOnError = callbacks?.onError?.(err);
143
- if (err.name !== "AbortError") {
144
- const retryInterval = typeof returnOfOnError === "number" ? returnOfOnError : this.options.retryInterval;
145
- if (retryInterval && retryInterval > 0) {
146
- if (typeof this.options.retryTimes === "number" && this.retryTimes >= this.options.retryTimes) return;
147
- clearTimeout(this.retryTimer);
148
- this.retryTimer = setTimeout(() => {
149
- const extraHeaders = {};
150
- if (typeof this.lastEventId !== "undefined") extraHeaders[LastEventId] = this.lastEventId;
151
- this.init(this.lastManualParams, extraHeaders);
152
- }, retryInterval);
153
- this.retryTimes = this.retryTimes + 1;
154
- }
155
- }
156
- });
157
- }
158
- startRequest() {
159
- this._isRequesting = true;
160
- }
161
- finishRequest() {
162
- this._isRequesting = false;
163
- }
164
- customResponseHandler = async (response, callbacks, transformStream, streamTimeout, streamSeparator, partSeparator, kvSeparator) => {
165
- const stream = XStream({
166
- readableStream: response.body,
167
- transformStream,
168
- streamSeparator,
169
- partSeparator,
170
- kvSeparator
171
- });
172
- await this.processStream(stream, response, callbacks, streamTimeout);
173
- };
174
- sseResponseHandler = async (response, callbacks, streamTimeout, streamSeparator, partSeparator, kvSeparator) => {
175
- const stream = XStream({
176
- readableStream: response.body,
177
- streamSeparator,
178
- partSeparator,
179
- kvSeparator
180
- });
181
- await this.processStream(stream, response, callbacks, streamTimeout);
182
- };
183
- async processStream(stream, response, callbacks, streamTimeout) {
184
- const chunks = [];
185
- const iterator = stream[Symbol.asyncIterator]();
186
- let result;
187
- do {
188
- if (streamTimeout) this.streamTimeoutHandler = window.setTimeout(() => {
189
- this.isStreamTimeout = true;
190
- this.finishRequest();
191
- callbacks?.onError?.(/* @__PURE__ */ new Error("StreamTimeoutError"), void 0, response.headers);
192
- }, streamTimeout);
193
- result = await iterator.next();
194
- clearTimeout(this.streamTimeoutHandler);
195
- if (this.isStreamTimeout) break;
196
- if (result.value) {
197
- chunks.push(result.value);
198
- callbacks?.onUpdate?.(result.value, response.headers);
199
- if (typeof result?.value?.id !== "undefined") this.lastEventId = result.value.id;
200
- }
201
- } while (!result.done);
202
- if (streamTimeout) {
203
- clearTimeout(this.streamTimeoutHandler);
204
- if (this.isStreamTimeout) {
205
- this.finishRequest();
206
- return;
207
- }
208
- }
209
- this.finishRequest();
210
- callbacks?.onSuccess?.(chunks, response.headers);
211
- }
212
- jsonResponseHandler = async (response, callbacks) => {
213
- const chunk = await response.json();
214
- if (chunk?.success === false) {
215
- const error = new Error(chunk.message || "System error");
216
- error.name = chunk.name || "SystemError";
217
- callbacks?.onError?.(error, chunk, response.headers);
218
- } else {
219
- callbacks?.onUpdate?.(chunk, response.headers);
220
- this.finishRequest();
221
- callbacks?.onSuccess?.([chunk], response.headers);
222
- }
223
- };
224
- resetRetry() {
225
- clearTimeout(this.retryTimer);
226
- this.retryTimes = 0;
227
- this.lastEventId = void 0;
228
- }
229
- };
230
- function XRequest(baseURL, options) {
231
- return new XRequestClass(baseURL, options);
232
- }
233
- //#endregion
234
- export { setXRequestGlobalOptions as i, XRequest as n, XRequestClass as r, AbstractXRequestClass as t };