@jxsuite/ai 0.1.0 → 0.33.0
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/package.json +4 -2
- package/dist/index.js +0 -1531
- package/dist/index.js.map +0 -14
package/dist/index.js
DELETED
|
@@ -1,1531 +0,0 @@
|
|
|
1
|
-
// src/tools.ts
|
|
2
|
-
function toolError(error) {
|
|
3
|
-
return { success: false, error };
|
|
4
|
-
}
|
|
5
|
-
function createToolDefinition({
|
|
6
|
-
name,
|
|
7
|
-
description,
|
|
8
|
-
parameters,
|
|
9
|
-
strict = true,
|
|
10
|
-
llmStrict = false,
|
|
11
|
-
execute
|
|
12
|
-
}) {
|
|
13
|
-
return { name, description, parameters, strict, llmStrict, execute };
|
|
14
|
-
}
|
|
15
|
-
function createToolRegistry() {
|
|
16
|
-
const _tools = new Map;
|
|
17
|
-
return {
|
|
18
|
-
register(tool) {
|
|
19
|
-
if (_tools.has(tool.name)) {
|
|
20
|
-
console.warn(`Tool "${tool.name}" is being re-registered.`);
|
|
21
|
-
}
|
|
22
|
-
_tools.set(tool.name, tool);
|
|
23
|
-
},
|
|
24
|
-
list() {
|
|
25
|
-
return [..._tools.values()];
|
|
26
|
-
},
|
|
27
|
-
listForLLM() {
|
|
28
|
-
return [..._tools.values()].map((t) => ({
|
|
29
|
-
type: "function",
|
|
30
|
-
function: {
|
|
31
|
-
name: t.name,
|
|
32
|
-
description: t.description,
|
|
33
|
-
parameters: t.parameters,
|
|
34
|
-
...t.llmStrict === true ? { strict: true } : {}
|
|
35
|
-
}
|
|
36
|
-
}));
|
|
37
|
-
},
|
|
38
|
-
getDefinition(toolName) {
|
|
39
|
-
return _tools.get(toolName);
|
|
40
|
-
},
|
|
41
|
-
validate(toolName, args) {
|
|
42
|
-
const tool = _tools.get(toolName);
|
|
43
|
-
if (!tool) {
|
|
44
|
-
return { valid: false, errors: [`Unknown tool: "${toolName}"`] };
|
|
45
|
-
}
|
|
46
|
-
if (!tool.strict) {
|
|
47
|
-
return { valid: true };
|
|
48
|
-
}
|
|
49
|
-
const schema = tool.parameters;
|
|
50
|
-
if (!schema || !schema.properties) {
|
|
51
|
-
return { valid: true };
|
|
52
|
-
}
|
|
53
|
-
const errors = [];
|
|
54
|
-
const props = schema.properties;
|
|
55
|
-
const required = schema.required || [];
|
|
56
|
-
const argRecord = args;
|
|
57
|
-
for (const key of required) {
|
|
58
|
-
if (!(key in args) || argRecord[key] === undefined || argRecord[key] === null) {
|
|
59
|
-
errors.push(`Missing required argument: "${key}"`);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
for (const [key, value] of Object.entries(argRecord)) {
|
|
63
|
-
const propSchema = props[key];
|
|
64
|
-
if (!propSchema) {
|
|
65
|
-
continue;
|
|
66
|
-
}
|
|
67
|
-
const expectedType = propSchema.type;
|
|
68
|
-
if (!expectedType) {
|
|
69
|
-
continue;
|
|
70
|
-
}
|
|
71
|
-
const actualType = Array.isArray(value) ? "array" : typeof value;
|
|
72
|
-
if (expectedType === "number" || expectedType === "integer") {
|
|
73
|
-
if (actualType === "number") {
|
|
74
|
-
continue;
|
|
75
|
-
}
|
|
76
|
-
if (actualType === "string") {
|
|
77
|
-
if (!Number.isNaN(Number(value))) {
|
|
78
|
-
continue;
|
|
79
|
-
}
|
|
80
|
-
errors.push(`Argument "${key}" should be a number, got non-numeric string "${value}"`);
|
|
81
|
-
continue;
|
|
82
|
-
}
|
|
83
|
-
errors.push(`Argument "${key}" should be a number, got ${actualType}`);
|
|
84
|
-
continue;
|
|
85
|
-
}
|
|
86
|
-
if (expectedType === "string" && actualType !== "string") {
|
|
87
|
-
errors.push(`Argument "${key}" should be a string, got ${actualType}`);
|
|
88
|
-
} else if (expectedType === "boolean" && actualType !== "boolean") {
|
|
89
|
-
errors.push(`Argument "${key}" should be a boolean, got ${actualType}`);
|
|
90
|
-
} else if (expectedType === "array" && actualType !== "array") {
|
|
91
|
-
errors.push(`Argument "${key}" should be an array, got ${actualType}`);
|
|
92
|
-
} else if (expectedType === "object" && actualType !== "object") {
|
|
93
|
-
errors.push(`Argument "${key}" should be an object, got ${actualType}`);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return errors.length > 0 ? { valid: false, errors } : { valid: true };
|
|
97
|
-
},
|
|
98
|
-
async execute(toolName, args) {
|
|
99
|
-
const tool = _tools.get(toolName);
|
|
100
|
-
if (!tool) {
|
|
101
|
-
return toolError(`Unknown tool: "${toolName}"`);
|
|
102
|
-
}
|
|
103
|
-
const validation = this.validate(toolName, args);
|
|
104
|
-
if (!validation.valid) {
|
|
105
|
-
return toolError(`Validation failed: ${(validation.errors || []).join("; ")}`);
|
|
106
|
-
}
|
|
107
|
-
try {
|
|
108
|
-
return await tool.execute(args);
|
|
109
|
-
} catch (error) {
|
|
110
|
-
return toolError(`Tool "${toolName}" execution error: ${error.message}`);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
// ../../node_modules/@vue/shared/dist/shared.esm-bundler.js
|
|
116
|
-
function makeMap(str) {
|
|
117
|
-
const map = /* @__PURE__ */ Object.create(null);
|
|
118
|
-
for (const key of str.split(","))
|
|
119
|
-
map[key] = 1;
|
|
120
|
-
return (val) => (val in map);
|
|
121
|
-
}
|
|
122
|
-
var EMPTY_OBJ = Object.freeze({});
|
|
123
|
-
var EMPTY_ARR = Object.freeze([]);
|
|
124
|
-
var extend = Object.assign;
|
|
125
|
-
var hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
126
|
-
var hasOwn = (val, key) => hasOwnProperty.call(val, key);
|
|
127
|
-
var isArray = Array.isArray;
|
|
128
|
-
var isMap = (val) => toTypeString(val) === "[object Map]";
|
|
129
|
-
var isString = (val) => typeof val === "string";
|
|
130
|
-
var isSymbol = (val) => typeof val === "symbol";
|
|
131
|
-
var isObject = (val) => val !== null && typeof val === "object";
|
|
132
|
-
var objectToString = Object.prototype.toString;
|
|
133
|
-
var toTypeString = (value) => objectToString.call(value);
|
|
134
|
-
var toRawType = (value) => {
|
|
135
|
-
return toTypeString(value).slice(8, -1);
|
|
136
|
-
};
|
|
137
|
-
var isIntegerKey = (key) => isString(key) && key !== "NaN" && key[0] !== "-" && "" + parseInt(key, 10) === key;
|
|
138
|
-
var cacheStringFunction = (fn) => {
|
|
139
|
-
const cache = /* @__PURE__ */ Object.create(null);
|
|
140
|
-
return (str) => {
|
|
141
|
-
const hit = cache[str];
|
|
142
|
-
return hit || (cache[str] = fn(str));
|
|
143
|
-
};
|
|
144
|
-
};
|
|
145
|
-
var camelizeRE = /-\w/g;
|
|
146
|
-
var camelize = cacheStringFunction((str) => {
|
|
147
|
-
return str.replace(camelizeRE, (c) => c.slice(1).toUpperCase());
|
|
148
|
-
});
|
|
149
|
-
var hyphenateRE = /\B([A-Z])/g;
|
|
150
|
-
var hyphenate = cacheStringFunction((str) => str.replace(hyphenateRE, "-$1").toLowerCase());
|
|
151
|
-
var capitalize = cacheStringFunction((str) => {
|
|
152
|
-
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
153
|
-
});
|
|
154
|
-
var toHandlerKey = cacheStringFunction((str) => {
|
|
155
|
-
const s = str ? `on${capitalize(str)}` : ``;
|
|
156
|
-
return s;
|
|
157
|
-
});
|
|
158
|
-
var hasChanged = (value, oldValue) => !Object.is(value, oldValue);
|
|
159
|
-
var specialBooleanAttrs = `itemscope,allowfullscreen,formnovalidate,ismap,nomodule,novalidate,readonly`;
|
|
160
|
-
var isBooleanAttr = /* @__PURE__ */ makeMap(specialBooleanAttrs + `,async,autofocus,autoplay,controls,default,defer,disabled,hidden,inert,loop,open,required,reversed,scoped,seamless,checked,muted,multiple,selected`);
|
|
161
|
-
|
|
162
|
-
// ../../node_modules/@vue/reactivity/dist/reactivity.esm-bundler.js
|
|
163
|
-
function warn(msg, ...args) {
|
|
164
|
-
console.warn(`[Vue warn] ${msg}`, ...args);
|
|
165
|
-
}
|
|
166
|
-
var activeSub;
|
|
167
|
-
var batchDepth = 0;
|
|
168
|
-
var batchedSub;
|
|
169
|
-
var batchedComputed;
|
|
170
|
-
function startBatch() {
|
|
171
|
-
batchDepth++;
|
|
172
|
-
}
|
|
173
|
-
function endBatch() {
|
|
174
|
-
if (--batchDepth > 0) {
|
|
175
|
-
return;
|
|
176
|
-
}
|
|
177
|
-
if (batchedComputed) {
|
|
178
|
-
let e = batchedComputed;
|
|
179
|
-
batchedComputed = undefined;
|
|
180
|
-
while (e) {
|
|
181
|
-
const next = e.next;
|
|
182
|
-
e.next = undefined;
|
|
183
|
-
e.flags &= -9;
|
|
184
|
-
e = next;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
let error;
|
|
188
|
-
while (batchedSub) {
|
|
189
|
-
let e = batchedSub;
|
|
190
|
-
batchedSub = undefined;
|
|
191
|
-
while (e) {
|
|
192
|
-
const next = e.next;
|
|
193
|
-
e.next = undefined;
|
|
194
|
-
e.flags &= -9;
|
|
195
|
-
if (e.flags & 1) {
|
|
196
|
-
try {
|
|
197
|
-
e.trigger();
|
|
198
|
-
} catch (err) {
|
|
199
|
-
if (!error)
|
|
200
|
-
error = err;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
e = next;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
if (error)
|
|
207
|
-
throw error;
|
|
208
|
-
}
|
|
209
|
-
var shouldTrack = true;
|
|
210
|
-
var trackStack = [];
|
|
211
|
-
function pauseTracking() {
|
|
212
|
-
trackStack.push(shouldTrack);
|
|
213
|
-
shouldTrack = false;
|
|
214
|
-
}
|
|
215
|
-
function resetTracking() {
|
|
216
|
-
const last = trackStack.pop();
|
|
217
|
-
shouldTrack = last === undefined ? true : last;
|
|
218
|
-
}
|
|
219
|
-
var globalVersion = 0;
|
|
220
|
-
|
|
221
|
-
class Link {
|
|
222
|
-
constructor(sub, dep) {
|
|
223
|
-
this.sub = sub;
|
|
224
|
-
this.dep = dep;
|
|
225
|
-
this.version = dep.version;
|
|
226
|
-
this.nextDep = this.prevDep = this.nextSub = this.prevSub = this.prevActiveLink = undefined;
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
class Dep {
|
|
231
|
-
constructor(computed) {
|
|
232
|
-
this.computed = computed;
|
|
233
|
-
this.version = 0;
|
|
234
|
-
this.activeLink = undefined;
|
|
235
|
-
this.subs = undefined;
|
|
236
|
-
this.map = undefined;
|
|
237
|
-
this.key = undefined;
|
|
238
|
-
this.sc = 0;
|
|
239
|
-
this.__v_skip = true;
|
|
240
|
-
if (true) {
|
|
241
|
-
this.subsHead = undefined;
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
track(debugInfo) {
|
|
245
|
-
if (!activeSub || !shouldTrack || activeSub === this.computed) {
|
|
246
|
-
return;
|
|
247
|
-
}
|
|
248
|
-
let link = this.activeLink;
|
|
249
|
-
if (link === undefined || link.sub !== activeSub) {
|
|
250
|
-
link = this.activeLink = new Link(activeSub, this);
|
|
251
|
-
if (!activeSub.deps) {
|
|
252
|
-
activeSub.deps = activeSub.depsTail = link;
|
|
253
|
-
} else {
|
|
254
|
-
link.prevDep = activeSub.depsTail;
|
|
255
|
-
activeSub.depsTail.nextDep = link;
|
|
256
|
-
activeSub.depsTail = link;
|
|
257
|
-
}
|
|
258
|
-
addSub(link);
|
|
259
|
-
} else if (link.version === -1) {
|
|
260
|
-
link.version = this.version;
|
|
261
|
-
if (link.nextDep) {
|
|
262
|
-
const next = link.nextDep;
|
|
263
|
-
next.prevDep = link.prevDep;
|
|
264
|
-
if (link.prevDep) {
|
|
265
|
-
link.prevDep.nextDep = next;
|
|
266
|
-
}
|
|
267
|
-
link.prevDep = activeSub.depsTail;
|
|
268
|
-
link.nextDep = undefined;
|
|
269
|
-
activeSub.depsTail.nextDep = link;
|
|
270
|
-
activeSub.depsTail = link;
|
|
271
|
-
if (activeSub.deps === link) {
|
|
272
|
-
activeSub.deps = next;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
if (activeSub.onTrack) {
|
|
277
|
-
activeSub.onTrack(extend({
|
|
278
|
-
effect: activeSub
|
|
279
|
-
}, debugInfo));
|
|
280
|
-
}
|
|
281
|
-
return link;
|
|
282
|
-
}
|
|
283
|
-
trigger(debugInfo) {
|
|
284
|
-
this.version++;
|
|
285
|
-
globalVersion++;
|
|
286
|
-
this.notify(debugInfo);
|
|
287
|
-
}
|
|
288
|
-
notify(debugInfo) {
|
|
289
|
-
startBatch();
|
|
290
|
-
try {
|
|
291
|
-
if (true) {
|
|
292
|
-
for (let head = this.subsHead;head; head = head.nextSub) {
|
|
293
|
-
if (head.sub.onTrigger && !(head.sub.flags & 8)) {
|
|
294
|
-
head.sub.onTrigger(extend({
|
|
295
|
-
effect: head.sub
|
|
296
|
-
}, debugInfo));
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
for (let link = this.subs;link; link = link.prevSub) {
|
|
301
|
-
if (link.sub.notify()) {
|
|
302
|
-
link.sub.dep.notify();
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
} finally {
|
|
306
|
-
endBatch();
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
function addSub(link) {
|
|
311
|
-
link.dep.sc++;
|
|
312
|
-
if (link.sub.flags & 4) {
|
|
313
|
-
const computed = link.dep.computed;
|
|
314
|
-
if (computed && !link.dep.subs) {
|
|
315
|
-
computed.flags |= 4 | 16;
|
|
316
|
-
for (let l = computed.deps;l; l = l.nextDep) {
|
|
317
|
-
addSub(l);
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
const currentTail = link.dep.subs;
|
|
321
|
-
if (currentTail !== link) {
|
|
322
|
-
link.prevSub = currentTail;
|
|
323
|
-
if (currentTail)
|
|
324
|
-
currentTail.nextSub = link;
|
|
325
|
-
}
|
|
326
|
-
if (link.dep.subsHead === undefined) {
|
|
327
|
-
link.dep.subsHead = link;
|
|
328
|
-
}
|
|
329
|
-
link.dep.subs = link;
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
var targetMap = /* @__PURE__ */ new WeakMap;
|
|
333
|
-
var ITERATE_KEY = /* @__PURE__ */ Symbol("Object iterate");
|
|
334
|
-
var MAP_KEY_ITERATE_KEY = /* @__PURE__ */ Symbol("Map keys iterate");
|
|
335
|
-
var ARRAY_ITERATE_KEY = /* @__PURE__ */ Symbol("Array iterate");
|
|
336
|
-
function track(target, type, key) {
|
|
337
|
-
if (shouldTrack && activeSub) {
|
|
338
|
-
let depsMap = targetMap.get(target);
|
|
339
|
-
if (!depsMap) {
|
|
340
|
-
targetMap.set(target, depsMap = /* @__PURE__ */ new Map);
|
|
341
|
-
}
|
|
342
|
-
let dep = depsMap.get(key);
|
|
343
|
-
if (!dep) {
|
|
344
|
-
depsMap.set(key, dep = new Dep);
|
|
345
|
-
dep.map = depsMap;
|
|
346
|
-
dep.key = key;
|
|
347
|
-
}
|
|
348
|
-
if (true) {
|
|
349
|
-
dep.track({
|
|
350
|
-
target,
|
|
351
|
-
type,
|
|
352
|
-
key
|
|
353
|
-
});
|
|
354
|
-
} else {}
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
function trigger(target, type, key, newValue, oldValue, oldTarget) {
|
|
358
|
-
const depsMap = targetMap.get(target);
|
|
359
|
-
if (!depsMap) {
|
|
360
|
-
globalVersion++;
|
|
361
|
-
return;
|
|
362
|
-
}
|
|
363
|
-
const run = (dep) => {
|
|
364
|
-
if (dep) {
|
|
365
|
-
if (true) {
|
|
366
|
-
dep.trigger({
|
|
367
|
-
target,
|
|
368
|
-
type,
|
|
369
|
-
key,
|
|
370
|
-
newValue,
|
|
371
|
-
oldValue,
|
|
372
|
-
oldTarget
|
|
373
|
-
});
|
|
374
|
-
} else {}
|
|
375
|
-
}
|
|
376
|
-
};
|
|
377
|
-
startBatch();
|
|
378
|
-
if (type === "clear") {
|
|
379
|
-
depsMap.forEach(run);
|
|
380
|
-
} else {
|
|
381
|
-
const targetIsArray = isArray(target);
|
|
382
|
-
const isArrayIndex = targetIsArray && isIntegerKey(key);
|
|
383
|
-
if (targetIsArray && key === "length") {
|
|
384
|
-
const newLength = Number(newValue);
|
|
385
|
-
depsMap.forEach((dep, key2) => {
|
|
386
|
-
if (key2 === "length" || key2 === ARRAY_ITERATE_KEY || !isSymbol(key2) && key2 >= newLength) {
|
|
387
|
-
run(dep);
|
|
388
|
-
}
|
|
389
|
-
});
|
|
390
|
-
} else {
|
|
391
|
-
if (key !== undefined || depsMap.has(undefined)) {
|
|
392
|
-
run(depsMap.get(key));
|
|
393
|
-
}
|
|
394
|
-
if (isArrayIndex) {
|
|
395
|
-
run(depsMap.get(ARRAY_ITERATE_KEY));
|
|
396
|
-
}
|
|
397
|
-
switch (type) {
|
|
398
|
-
case "add":
|
|
399
|
-
if (!targetIsArray) {
|
|
400
|
-
run(depsMap.get(ITERATE_KEY));
|
|
401
|
-
if (isMap(target)) {
|
|
402
|
-
run(depsMap.get(MAP_KEY_ITERATE_KEY));
|
|
403
|
-
}
|
|
404
|
-
} else if (isArrayIndex) {
|
|
405
|
-
run(depsMap.get("length"));
|
|
406
|
-
}
|
|
407
|
-
break;
|
|
408
|
-
case "delete":
|
|
409
|
-
if (!targetIsArray) {
|
|
410
|
-
run(depsMap.get(ITERATE_KEY));
|
|
411
|
-
if (isMap(target)) {
|
|
412
|
-
run(depsMap.get(MAP_KEY_ITERATE_KEY));
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
break;
|
|
416
|
-
case "set":
|
|
417
|
-
if (isMap(target)) {
|
|
418
|
-
run(depsMap.get(ITERATE_KEY));
|
|
419
|
-
}
|
|
420
|
-
break;
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
endBatch();
|
|
425
|
-
}
|
|
426
|
-
function reactiveReadArray(array) {
|
|
427
|
-
const raw = toRaw(array);
|
|
428
|
-
if (raw === array)
|
|
429
|
-
return raw;
|
|
430
|
-
track(raw, "iterate", ARRAY_ITERATE_KEY);
|
|
431
|
-
return isShallow(array) ? raw : raw.map(toReactive);
|
|
432
|
-
}
|
|
433
|
-
function shallowReadArray(arr) {
|
|
434
|
-
track(arr = toRaw(arr), "iterate", ARRAY_ITERATE_KEY);
|
|
435
|
-
return arr;
|
|
436
|
-
}
|
|
437
|
-
function toWrapped(target, item) {
|
|
438
|
-
if (isReadonly(target)) {
|
|
439
|
-
return isReactive(target) ? toReadonly(toReactive(item)) : toReadonly(item);
|
|
440
|
-
}
|
|
441
|
-
return toReactive(item);
|
|
442
|
-
}
|
|
443
|
-
var arrayInstrumentations = {
|
|
444
|
-
__proto__: null,
|
|
445
|
-
[Symbol.iterator]() {
|
|
446
|
-
return iterator(this, Symbol.iterator, (item) => toWrapped(this, item));
|
|
447
|
-
},
|
|
448
|
-
concat(...args) {
|
|
449
|
-
return reactiveReadArray(this).concat(...args.map((x) => isArray(x) ? reactiveReadArray(x) : x));
|
|
450
|
-
},
|
|
451
|
-
entries() {
|
|
452
|
-
return iterator(this, "entries", (value) => {
|
|
453
|
-
value[1] = toWrapped(this, value[1]);
|
|
454
|
-
return value;
|
|
455
|
-
});
|
|
456
|
-
},
|
|
457
|
-
every(fn, thisArg) {
|
|
458
|
-
return apply(this, "every", fn, thisArg, undefined, arguments);
|
|
459
|
-
},
|
|
460
|
-
filter(fn, thisArg) {
|
|
461
|
-
return apply(this, "filter", fn, thisArg, (v) => v.map((item) => toWrapped(this, item)), arguments);
|
|
462
|
-
},
|
|
463
|
-
find(fn, thisArg) {
|
|
464
|
-
return apply(this, "find", fn, thisArg, (item) => toWrapped(this, item), arguments);
|
|
465
|
-
},
|
|
466
|
-
findIndex(fn, thisArg) {
|
|
467
|
-
return apply(this, "findIndex", fn, thisArg, undefined, arguments);
|
|
468
|
-
},
|
|
469
|
-
findLast(fn, thisArg) {
|
|
470
|
-
return apply(this, "findLast", fn, thisArg, (item) => toWrapped(this, item), arguments);
|
|
471
|
-
},
|
|
472
|
-
findLastIndex(fn, thisArg) {
|
|
473
|
-
return apply(this, "findLastIndex", fn, thisArg, undefined, arguments);
|
|
474
|
-
},
|
|
475
|
-
forEach(fn, thisArg) {
|
|
476
|
-
return apply(this, "forEach", fn, thisArg, undefined, arguments);
|
|
477
|
-
},
|
|
478
|
-
includes(...args) {
|
|
479
|
-
return searchProxy(this, "includes", args);
|
|
480
|
-
},
|
|
481
|
-
indexOf(...args) {
|
|
482
|
-
return searchProxy(this, "indexOf", args);
|
|
483
|
-
},
|
|
484
|
-
join(separator) {
|
|
485
|
-
return reactiveReadArray(this).join(separator);
|
|
486
|
-
},
|
|
487
|
-
lastIndexOf(...args) {
|
|
488
|
-
return searchProxy(this, "lastIndexOf", args);
|
|
489
|
-
},
|
|
490
|
-
map(fn, thisArg) {
|
|
491
|
-
return apply(this, "map", fn, thisArg, undefined, arguments);
|
|
492
|
-
},
|
|
493
|
-
pop() {
|
|
494
|
-
return noTracking(this, "pop");
|
|
495
|
-
},
|
|
496
|
-
push(...args) {
|
|
497
|
-
return noTracking(this, "push", args);
|
|
498
|
-
},
|
|
499
|
-
reduce(fn, ...args) {
|
|
500
|
-
return reduce(this, "reduce", fn, args);
|
|
501
|
-
},
|
|
502
|
-
reduceRight(fn, ...args) {
|
|
503
|
-
return reduce(this, "reduceRight", fn, args);
|
|
504
|
-
},
|
|
505
|
-
shift() {
|
|
506
|
-
return noTracking(this, "shift");
|
|
507
|
-
},
|
|
508
|
-
some(fn, thisArg) {
|
|
509
|
-
return apply(this, "some", fn, thisArg, undefined, arguments);
|
|
510
|
-
},
|
|
511
|
-
splice(...args) {
|
|
512
|
-
return noTracking(this, "splice", args);
|
|
513
|
-
},
|
|
514
|
-
toReversed() {
|
|
515
|
-
return reactiveReadArray(this).toReversed();
|
|
516
|
-
},
|
|
517
|
-
toSorted(comparer) {
|
|
518
|
-
return reactiveReadArray(this).toSorted(comparer);
|
|
519
|
-
},
|
|
520
|
-
toSpliced(...args) {
|
|
521
|
-
return reactiveReadArray(this).toSpliced(...args);
|
|
522
|
-
},
|
|
523
|
-
unshift(...args) {
|
|
524
|
-
return noTracking(this, "unshift", args);
|
|
525
|
-
},
|
|
526
|
-
values() {
|
|
527
|
-
return iterator(this, "values", (item) => toWrapped(this, item));
|
|
528
|
-
}
|
|
529
|
-
};
|
|
530
|
-
function iterator(self2, method, wrapValue) {
|
|
531
|
-
const arr = shallowReadArray(self2);
|
|
532
|
-
const iter = arr[method]();
|
|
533
|
-
if (arr !== self2 && !isShallow(self2)) {
|
|
534
|
-
iter._next = iter.next;
|
|
535
|
-
iter.next = () => {
|
|
536
|
-
const result = iter._next();
|
|
537
|
-
if (!result.done) {
|
|
538
|
-
result.value = wrapValue(result.value);
|
|
539
|
-
}
|
|
540
|
-
return result;
|
|
541
|
-
};
|
|
542
|
-
}
|
|
543
|
-
return iter;
|
|
544
|
-
}
|
|
545
|
-
var arrayProto = Array.prototype;
|
|
546
|
-
function apply(self2, method, fn, thisArg, wrappedRetFn, args) {
|
|
547
|
-
const arr = shallowReadArray(self2);
|
|
548
|
-
const needsWrap = arr !== self2 && !isShallow(self2);
|
|
549
|
-
const methodFn = arr[method];
|
|
550
|
-
if (methodFn !== arrayProto[method]) {
|
|
551
|
-
const result2 = methodFn.apply(self2, args);
|
|
552
|
-
return needsWrap ? toReactive(result2) : result2;
|
|
553
|
-
}
|
|
554
|
-
let wrappedFn = fn;
|
|
555
|
-
if (arr !== self2) {
|
|
556
|
-
if (needsWrap) {
|
|
557
|
-
wrappedFn = function(item, index) {
|
|
558
|
-
return fn.call(this, toWrapped(self2, item), index, self2);
|
|
559
|
-
};
|
|
560
|
-
} else if (fn.length > 2) {
|
|
561
|
-
wrappedFn = function(item, index) {
|
|
562
|
-
return fn.call(this, item, index, self2);
|
|
563
|
-
};
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
const result = methodFn.call(arr, wrappedFn, thisArg);
|
|
567
|
-
return needsWrap && wrappedRetFn ? wrappedRetFn(result) : result;
|
|
568
|
-
}
|
|
569
|
-
function reduce(self2, method, fn, args) {
|
|
570
|
-
const arr = shallowReadArray(self2);
|
|
571
|
-
const needsWrap = arr !== self2 && !isShallow(self2);
|
|
572
|
-
let wrappedFn = fn;
|
|
573
|
-
let wrapInitialAccumulator = false;
|
|
574
|
-
if (arr !== self2) {
|
|
575
|
-
if (needsWrap) {
|
|
576
|
-
wrapInitialAccumulator = args.length === 0;
|
|
577
|
-
wrappedFn = function(acc, item, index) {
|
|
578
|
-
if (wrapInitialAccumulator) {
|
|
579
|
-
wrapInitialAccumulator = false;
|
|
580
|
-
acc = toWrapped(self2, acc);
|
|
581
|
-
}
|
|
582
|
-
return fn.call(this, acc, toWrapped(self2, item), index, self2);
|
|
583
|
-
};
|
|
584
|
-
} else if (fn.length > 3) {
|
|
585
|
-
wrappedFn = function(acc, item, index) {
|
|
586
|
-
return fn.call(this, acc, item, index, self2);
|
|
587
|
-
};
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
const result = arr[method](wrappedFn, ...args);
|
|
591
|
-
return wrapInitialAccumulator ? toWrapped(self2, result) : result;
|
|
592
|
-
}
|
|
593
|
-
function searchProxy(self2, method, args) {
|
|
594
|
-
const arr = toRaw(self2);
|
|
595
|
-
track(arr, "iterate", ARRAY_ITERATE_KEY);
|
|
596
|
-
const res = arr[method](...args);
|
|
597
|
-
if ((res === -1 || res === false) && isProxy(args[0])) {
|
|
598
|
-
args[0] = toRaw(args[0]);
|
|
599
|
-
return arr[method](...args);
|
|
600
|
-
}
|
|
601
|
-
return res;
|
|
602
|
-
}
|
|
603
|
-
function noTracking(self2, method, args = []) {
|
|
604
|
-
pauseTracking();
|
|
605
|
-
startBatch();
|
|
606
|
-
const res = toRaw(self2)[method].apply(self2, args);
|
|
607
|
-
endBatch();
|
|
608
|
-
resetTracking();
|
|
609
|
-
return res;
|
|
610
|
-
}
|
|
611
|
-
var isNonTrackableKeys = /* @__PURE__ */ makeMap(`__proto__,__v_isRef,__isVue`);
|
|
612
|
-
var builtInSymbols = new Set(/* @__PURE__ */ Object.getOwnPropertyNames(Symbol).filter((key) => key !== "arguments" && key !== "caller").map((key) => Symbol[key]).filter(isSymbol));
|
|
613
|
-
function hasOwnProperty2(key) {
|
|
614
|
-
if (!isSymbol(key))
|
|
615
|
-
key = String(key);
|
|
616
|
-
const obj = toRaw(this);
|
|
617
|
-
track(obj, "has", key);
|
|
618
|
-
return obj.hasOwnProperty(key);
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
class BaseReactiveHandler {
|
|
622
|
-
constructor(_isReadonly = false, _isShallow = false) {
|
|
623
|
-
this._isReadonly = _isReadonly;
|
|
624
|
-
this._isShallow = _isShallow;
|
|
625
|
-
}
|
|
626
|
-
get(target, key, receiver) {
|
|
627
|
-
if (key === "__v_skip")
|
|
628
|
-
return target["__v_skip"];
|
|
629
|
-
const isReadonly2 = this._isReadonly, isShallow2 = this._isShallow;
|
|
630
|
-
if (key === "__v_isReactive") {
|
|
631
|
-
return !isReadonly2;
|
|
632
|
-
} else if (key === "__v_isReadonly") {
|
|
633
|
-
return isReadonly2;
|
|
634
|
-
} else if (key === "__v_isShallow") {
|
|
635
|
-
return isShallow2;
|
|
636
|
-
} else if (key === "__v_raw") {
|
|
637
|
-
if (receiver === (isReadonly2 ? isShallow2 ? shallowReadonlyMap : readonlyMap : isShallow2 ? shallowReactiveMap : reactiveMap).get(target) || Object.getPrototypeOf(target) === Object.getPrototypeOf(receiver)) {
|
|
638
|
-
return target;
|
|
639
|
-
}
|
|
640
|
-
return;
|
|
641
|
-
}
|
|
642
|
-
const targetIsArray = isArray(target);
|
|
643
|
-
if (!isReadonly2) {
|
|
644
|
-
let fn;
|
|
645
|
-
if (targetIsArray && (fn = arrayInstrumentations[key])) {
|
|
646
|
-
return fn;
|
|
647
|
-
}
|
|
648
|
-
if (key === "hasOwnProperty") {
|
|
649
|
-
return hasOwnProperty2;
|
|
650
|
-
}
|
|
651
|
-
}
|
|
652
|
-
const res = Reflect.get(target, key, isRef(target) ? target : receiver);
|
|
653
|
-
if (isSymbol(key) ? builtInSymbols.has(key) : isNonTrackableKeys(key)) {
|
|
654
|
-
return res;
|
|
655
|
-
}
|
|
656
|
-
if (!isReadonly2) {
|
|
657
|
-
track(target, "get", key);
|
|
658
|
-
}
|
|
659
|
-
if (isShallow2) {
|
|
660
|
-
return res;
|
|
661
|
-
}
|
|
662
|
-
if (isRef(res)) {
|
|
663
|
-
const value = targetIsArray && isIntegerKey(key) ? res : res.value;
|
|
664
|
-
return isReadonly2 && isObject(value) ? readonly(value) : value;
|
|
665
|
-
}
|
|
666
|
-
if (isObject(res)) {
|
|
667
|
-
return isReadonly2 ? readonly(res) : reactive(res);
|
|
668
|
-
}
|
|
669
|
-
return res;
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
|
|
673
|
-
class MutableReactiveHandler extends BaseReactiveHandler {
|
|
674
|
-
constructor(isShallow2 = false) {
|
|
675
|
-
super(false, isShallow2);
|
|
676
|
-
}
|
|
677
|
-
set(target, key, value, receiver) {
|
|
678
|
-
let oldValue = target[key];
|
|
679
|
-
const isArrayWithIntegerKey = isArray(target) && isIntegerKey(key);
|
|
680
|
-
if (!this._isShallow) {
|
|
681
|
-
const isOldValueReadonly = isReadonly(oldValue);
|
|
682
|
-
if (!isShallow(value) && !isReadonly(value)) {
|
|
683
|
-
oldValue = toRaw(oldValue);
|
|
684
|
-
value = toRaw(value);
|
|
685
|
-
}
|
|
686
|
-
if (!isArrayWithIntegerKey && isRef(oldValue) && !isRef(value)) {
|
|
687
|
-
if (isOldValueReadonly) {
|
|
688
|
-
if (true) {
|
|
689
|
-
warn(`Set operation on key "${String(key)}" failed: target is readonly.`, target[key]);
|
|
690
|
-
}
|
|
691
|
-
return true;
|
|
692
|
-
} else {
|
|
693
|
-
oldValue.value = value;
|
|
694
|
-
return true;
|
|
695
|
-
}
|
|
696
|
-
}
|
|
697
|
-
}
|
|
698
|
-
const hadKey = isArrayWithIntegerKey ? Number(key) < target.length : hasOwn(target, key);
|
|
699
|
-
const result = Reflect.set(target, key, value, isRef(target) ? target : receiver);
|
|
700
|
-
if (target === toRaw(receiver) && result) {
|
|
701
|
-
if (!hadKey) {
|
|
702
|
-
trigger(target, "add", key, value);
|
|
703
|
-
} else if (hasChanged(value, oldValue)) {
|
|
704
|
-
trigger(target, "set", key, value, oldValue);
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
return result;
|
|
708
|
-
}
|
|
709
|
-
deleteProperty(target, key) {
|
|
710
|
-
const hadKey = hasOwn(target, key);
|
|
711
|
-
const oldValue = target[key];
|
|
712
|
-
const result = Reflect.deleteProperty(target, key);
|
|
713
|
-
if (result && hadKey) {
|
|
714
|
-
trigger(target, "delete", key, undefined, oldValue);
|
|
715
|
-
}
|
|
716
|
-
return result;
|
|
717
|
-
}
|
|
718
|
-
has(target, key) {
|
|
719
|
-
const result = Reflect.has(target, key);
|
|
720
|
-
if (!isSymbol(key) || !builtInSymbols.has(key)) {
|
|
721
|
-
track(target, "has", key);
|
|
722
|
-
}
|
|
723
|
-
return result;
|
|
724
|
-
}
|
|
725
|
-
ownKeys(target) {
|
|
726
|
-
track(target, "iterate", isArray(target) ? "length" : ITERATE_KEY);
|
|
727
|
-
return Reflect.ownKeys(target);
|
|
728
|
-
}
|
|
729
|
-
}
|
|
730
|
-
|
|
731
|
-
class ReadonlyReactiveHandler extends BaseReactiveHandler {
|
|
732
|
-
constructor(isShallow2 = false) {
|
|
733
|
-
super(true, isShallow2);
|
|
734
|
-
}
|
|
735
|
-
set(target, key) {
|
|
736
|
-
if (true) {
|
|
737
|
-
warn(`Set operation on key "${String(key)}" failed: target is readonly.`, target);
|
|
738
|
-
}
|
|
739
|
-
return true;
|
|
740
|
-
}
|
|
741
|
-
deleteProperty(target, key) {
|
|
742
|
-
if (true) {
|
|
743
|
-
warn(`Delete operation on key "${String(key)}" failed: target is readonly.`, target);
|
|
744
|
-
}
|
|
745
|
-
return true;
|
|
746
|
-
}
|
|
747
|
-
}
|
|
748
|
-
var mutableHandlers = /* @__PURE__ */ new MutableReactiveHandler;
|
|
749
|
-
var readonlyHandlers = /* @__PURE__ */ new ReadonlyReactiveHandler;
|
|
750
|
-
var toShallow = (value) => value;
|
|
751
|
-
var getProto = (v) => Reflect.getPrototypeOf(v);
|
|
752
|
-
function createIterableMethod(method, isReadonly2, isShallow2) {
|
|
753
|
-
return function(...args) {
|
|
754
|
-
const target = this["__v_raw"];
|
|
755
|
-
const rawTarget = toRaw(target);
|
|
756
|
-
const targetIsMap = isMap(rawTarget);
|
|
757
|
-
const isPair = method === "entries" || method === Symbol.iterator && targetIsMap;
|
|
758
|
-
const isKeyOnly = method === "keys" && targetIsMap;
|
|
759
|
-
const innerIterator = target[method](...args);
|
|
760
|
-
const wrap = isShallow2 ? toShallow : isReadonly2 ? toReadonly : toReactive;
|
|
761
|
-
!isReadonly2 && track(rawTarget, "iterate", isKeyOnly ? MAP_KEY_ITERATE_KEY : ITERATE_KEY);
|
|
762
|
-
return extend(Object.create(innerIterator), {
|
|
763
|
-
next() {
|
|
764
|
-
const { value, done } = innerIterator.next();
|
|
765
|
-
return done ? { value, done } : {
|
|
766
|
-
value: isPair ? [wrap(value[0]), wrap(value[1])] : wrap(value),
|
|
767
|
-
done
|
|
768
|
-
};
|
|
769
|
-
}
|
|
770
|
-
});
|
|
771
|
-
};
|
|
772
|
-
}
|
|
773
|
-
function createReadonlyMethod(type) {
|
|
774
|
-
return function(...args) {
|
|
775
|
-
if (true) {
|
|
776
|
-
const key = args[0] ? `on key "${args[0]}" ` : ``;
|
|
777
|
-
warn(`${capitalize(type)} operation ${key}failed: target is readonly.`, toRaw(this));
|
|
778
|
-
}
|
|
779
|
-
return type === "delete" ? false : type === "clear" ? undefined : this;
|
|
780
|
-
};
|
|
781
|
-
}
|
|
782
|
-
function createInstrumentations(readonly, shallow) {
|
|
783
|
-
const instrumentations = {
|
|
784
|
-
get(key) {
|
|
785
|
-
const target = this["__v_raw"];
|
|
786
|
-
const rawTarget = toRaw(target);
|
|
787
|
-
const rawKey = toRaw(key);
|
|
788
|
-
if (!readonly) {
|
|
789
|
-
if (hasChanged(key, rawKey)) {
|
|
790
|
-
track(rawTarget, "get", key);
|
|
791
|
-
}
|
|
792
|
-
track(rawTarget, "get", rawKey);
|
|
793
|
-
}
|
|
794
|
-
const { has } = getProto(rawTarget);
|
|
795
|
-
const wrap = shallow ? toShallow : readonly ? toReadonly : toReactive;
|
|
796
|
-
if (has.call(rawTarget, key)) {
|
|
797
|
-
return wrap(target.get(key));
|
|
798
|
-
} else if (has.call(rawTarget, rawKey)) {
|
|
799
|
-
return wrap(target.get(rawKey));
|
|
800
|
-
} else if (target !== rawTarget) {
|
|
801
|
-
target.get(key);
|
|
802
|
-
}
|
|
803
|
-
},
|
|
804
|
-
get size() {
|
|
805
|
-
const target = this["__v_raw"];
|
|
806
|
-
!readonly && track(toRaw(target), "iterate", ITERATE_KEY);
|
|
807
|
-
return target.size;
|
|
808
|
-
},
|
|
809
|
-
has(key) {
|
|
810
|
-
const target = this["__v_raw"];
|
|
811
|
-
const rawTarget = toRaw(target);
|
|
812
|
-
const rawKey = toRaw(key);
|
|
813
|
-
if (!readonly) {
|
|
814
|
-
if (hasChanged(key, rawKey)) {
|
|
815
|
-
track(rawTarget, "has", key);
|
|
816
|
-
}
|
|
817
|
-
track(rawTarget, "has", rawKey);
|
|
818
|
-
}
|
|
819
|
-
return key === rawKey ? target.has(key) : target.has(key) || target.has(rawKey);
|
|
820
|
-
},
|
|
821
|
-
forEach(callback, thisArg) {
|
|
822
|
-
const observed = this;
|
|
823
|
-
const target = observed["__v_raw"];
|
|
824
|
-
const rawTarget = toRaw(target);
|
|
825
|
-
const wrap = shallow ? toShallow : readonly ? toReadonly : toReactive;
|
|
826
|
-
!readonly && track(rawTarget, "iterate", ITERATE_KEY);
|
|
827
|
-
return target.forEach((value, key) => {
|
|
828
|
-
return callback.call(thisArg, wrap(value), wrap(key), observed);
|
|
829
|
-
});
|
|
830
|
-
}
|
|
831
|
-
};
|
|
832
|
-
extend(instrumentations, readonly ? {
|
|
833
|
-
add: createReadonlyMethod("add"),
|
|
834
|
-
set: createReadonlyMethod("set"),
|
|
835
|
-
delete: createReadonlyMethod("delete"),
|
|
836
|
-
clear: createReadonlyMethod("clear")
|
|
837
|
-
} : {
|
|
838
|
-
add(value) {
|
|
839
|
-
const target = toRaw(this);
|
|
840
|
-
const proto = getProto(target);
|
|
841
|
-
const rawValue = toRaw(value);
|
|
842
|
-
const valueToAdd = !shallow && !isShallow(value) && !isReadonly(value) ? rawValue : value;
|
|
843
|
-
const hadKey = proto.has.call(target, valueToAdd) || hasChanged(value, valueToAdd) && proto.has.call(target, value) || hasChanged(rawValue, valueToAdd) && proto.has.call(target, rawValue);
|
|
844
|
-
if (!hadKey) {
|
|
845
|
-
target.add(valueToAdd);
|
|
846
|
-
trigger(target, "add", valueToAdd, valueToAdd);
|
|
847
|
-
}
|
|
848
|
-
return this;
|
|
849
|
-
},
|
|
850
|
-
set(key, value) {
|
|
851
|
-
if (!shallow && !isShallow(value) && !isReadonly(value)) {
|
|
852
|
-
value = toRaw(value);
|
|
853
|
-
}
|
|
854
|
-
const target = toRaw(this);
|
|
855
|
-
const { has, get } = getProto(target);
|
|
856
|
-
let hadKey = has.call(target, key);
|
|
857
|
-
if (!hadKey) {
|
|
858
|
-
key = toRaw(key);
|
|
859
|
-
hadKey = has.call(target, key);
|
|
860
|
-
} else if (true) {
|
|
861
|
-
checkIdentityKeys(target, has, key);
|
|
862
|
-
}
|
|
863
|
-
const oldValue = get.call(target, key);
|
|
864
|
-
target.set(key, value);
|
|
865
|
-
if (!hadKey) {
|
|
866
|
-
trigger(target, "add", key, value);
|
|
867
|
-
} else if (hasChanged(value, oldValue)) {
|
|
868
|
-
trigger(target, "set", key, value, oldValue);
|
|
869
|
-
}
|
|
870
|
-
return this;
|
|
871
|
-
},
|
|
872
|
-
delete(key) {
|
|
873
|
-
const target = toRaw(this);
|
|
874
|
-
const { has, get } = getProto(target);
|
|
875
|
-
let hadKey = has.call(target, key);
|
|
876
|
-
if (!hadKey) {
|
|
877
|
-
key = toRaw(key);
|
|
878
|
-
hadKey = has.call(target, key);
|
|
879
|
-
} else if (true) {
|
|
880
|
-
checkIdentityKeys(target, has, key);
|
|
881
|
-
}
|
|
882
|
-
const oldValue = get ? get.call(target, key) : undefined;
|
|
883
|
-
const result = target.delete(key);
|
|
884
|
-
if (hadKey) {
|
|
885
|
-
trigger(target, "delete", key, undefined, oldValue);
|
|
886
|
-
}
|
|
887
|
-
return result;
|
|
888
|
-
},
|
|
889
|
-
clear() {
|
|
890
|
-
const target = toRaw(this);
|
|
891
|
-
const hadItems = target.size !== 0;
|
|
892
|
-
const oldTarget = isMap(target) ? new Map(target) : new Set(target);
|
|
893
|
-
const result = target.clear();
|
|
894
|
-
if (hadItems) {
|
|
895
|
-
trigger(target, "clear", undefined, undefined, oldTarget);
|
|
896
|
-
}
|
|
897
|
-
return result;
|
|
898
|
-
}
|
|
899
|
-
});
|
|
900
|
-
const iteratorMethods = [
|
|
901
|
-
"keys",
|
|
902
|
-
"values",
|
|
903
|
-
"entries",
|
|
904
|
-
Symbol.iterator
|
|
905
|
-
];
|
|
906
|
-
iteratorMethods.forEach((method) => {
|
|
907
|
-
instrumentations[method] = createIterableMethod(method, readonly, shallow);
|
|
908
|
-
});
|
|
909
|
-
return instrumentations;
|
|
910
|
-
}
|
|
911
|
-
function createInstrumentationGetter(isReadonly2, shallow) {
|
|
912
|
-
const instrumentations = createInstrumentations(isReadonly2, shallow);
|
|
913
|
-
return (target, key, receiver) => {
|
|
914
|
-
if (key === "__v_isReactive") {
|
|
915
|
-
return !isReadonly2;
|
|
916
|
-
} else if (key === "__v_isReadonly") {
|
|
917
|
-
return isReadonly2;
|
|
918
|
-
} else if (key === "__v_raw") {
|
|
919
|
-
return target;
|
|
920
|
-
}
|
|
921
|
-
return Reflect.get(hasOwn(instrumentations, key) && key in target ? instrumentations : target, key, receiver);
|
|
922
|
-
};
|
|
923
|
-
}
|
|
924
|
-
var mutableCollectionHandlers = {
|
|
925
|
-
get: /* @__PURE__ */ createInstrumentationGetter(false, false)
|
|
926
|
-
};
|
|
927
|
-
var readonlyCollectionHandlers = {
|
|
928
|
-
get: /* @__PURE__ */ createInstrumentationGetter(true, false)
|
|
929
|
-
};
|
|
930
|
-
function checkIdentityKeys(target, has, key) {
|
|
931
|
-
const rawKey = toRaw(key);
|
|
932
|
-
if (rawKey !== key && has.call(target, rawKey)) {
|
|
933
|
-
const type = toRawType(target);
|
|
934
|
-
warn(`Reactive ${type} contains both the raw and reactive versions of the same object${type === `Map` ? ` as keys` : ``}, which can lead to inconsistencies. Avoid differentiating between the raw and reactive versions of an object and only use the reactive version if possible.`);
|
|
935
|
-
}
|
|
936
|
-
}
|
|
937
|
-
var reactiveMap = /* @__PURE__ */ new WeakMap;
|
|
938
|
-
var shallowReactiveMap = /* @__PURE__ */ new WeakMap;
|
|
939
|
-
var readonlyMap = /* @__PURE__ */ new WeakMap;
|
|
940
|
-
var shallowReadonlyMap = /* @__PURE__ */ new WeakMap;
|
|
941
|
-
function targetTypeMap(rawType) {
|
|
942
|
-
switch (rawType) {
|
|
943
|
-
case "Object":
|
|
944
|
-
case "Array":
|
|
945
|
-
return 1;
|
|
946
|
-
case "Map":
|
|
947
|
-
case "Set":
|
|
948
|
-
case "WeakMap":
|
|
949
|
-
case "WeakSet":
|
|
950
|
-
return 2;
|
|
951
|
-
default:
|
|
952
|
-
return 0;
|
|
953
|
-
}
|
|
954
|
-
}
|
|
955
|
-
function reactive(target) {
|
|
956
|
-
if (/* @__PURE__ */ isReadonly(target)) {
|
|
957
|
-
return target;
|
|
958
|
-
}
|
|
959
|
-
return createReactiveObject(target, false, mutableHandlers, mutableCollectionHandlers, reactiveMap);
|
|
960
|
-
}
|
|
961
|
-
function readonly(target) {
|
|
962
|
-
return createReactiveObject(target, true, readonlyHandlers, readonlyCollectionHandlers, readonlyMap);
|
|
963
|
-
}
|
|
964
|
-
function createReactiveObject(target, isReadonly2, baseHandlers, collectionHandlers, proxyMap) {
|
|
965
|
-
if (!isObject(target)) {
|
|
966
|
-
if (true) {
|
|
967
|
-
warn(`value cannot be made ${isReadonly2 ? "readonly" : "reactive"}: ${String(target)}`);
|
|
968
|
-
}
|
|
969
|
-
return target;
|
|
970
|
-
}
|
|
971
|
-
if (target["__v_raw"] && !(isReadonly2 && target["__v_isReactive"])) {
|
|
972
|
-
return target;
|
|
973
|
-
}
|
|
974
|
-
if (target["__v_skip"] || !Object.isExtensible(target)) {
|
|
975
|
-
return target;
|
|
976
|
-
}
|
|
977
|
-
const existingProxy = proxyMap.get(target);
|
|
978
|
-
if (existingProxy) {
|
|
979
|
-
return existingProxy;
|
|
980
|
-
}
|
|
981
|
-
const targetType = targetTypeMap(toRawType(target));
|
|
982
|
-
if (targetType === 0) {
|
|
983
|
-
return target;
|
|
984
|
-
}
|
|
985
|
-
const proxy = new Proxy(target, targetType === 2 ? collectionHandlers : baseHandlers);
|
|
986
|
-
proxyMap.set(target, proxy);
|
|
987
|
-
return proxy;
|
|
988
|
-
}
|
|
989
|
-
function isReactive(value) {
|
|
990
|
-
if (/* @__PURE__ */ isReadonly(value)) {
|
|
991
|
-
return /* @__PURE__ */ isReactive(value["__v_raw"]);
|
|
992
|
-
}
|
|
993
|
-
return !!(value && value["__v_isReactive"]);
|
|
994
|
-
}
|
|
995
|
-
function isReadonly(value) {
|
|
996
|
-
return !!(value && value["__v_isReadonly"]);
|
|
997
|
-
}
|
|
998
|
-
function isShallow(value) {
|
|
999
|
-
return !!(value && value["__v_isShallow"]);
|
|
1000
|
-
}
|
|
1001
|
-
function isProxy(value) {
|
|
1002
|
-
return value ? !!value["__v_raw"] : false;
|
|
1003
|
-
}
|
|
1004
|
-
function toRaw(observed) {
|
|
1005
|
-
const raw = observed && observed["__v_raw"];
|
|
1006
|
-
return raw ? /* @__PURE__ */ toRaw(raw) : observed;
|
|
1007
|
-
}
|
|
1008
|
-
var toReactive = (value) => isObject(value) ? /* @__PURE__ */ reactive(value) : value;
|
|
1009
|
-
var toReadonly = (value) => isObject(value) ? /* @__PURE__ */ readonly(value) : value;
|
|
1010
|
-
function isRef(r) {
|
|
1011
|
-
return r ? r["__v_isRef"] === true : false;
|
|
1012
|
-
}
|
|
1013
|
-
|
|
1014
|
-
// src/chat-state.ts
|
|
1015
|
-
var _idCounter = 0;
|
|
1016
|
-
function uid() {
|
|
1017
|
-
_idCounter += 1;
|
|
1018
|
-
return `msg_${Date.now()}_${_idCounter}`;
|
|
1019
|
-
}
|
|
1020
|
-
function createChatState(opts = {}) {
|
|
1021
|
-
const model = opts.model || "gpt-4o";
|
|
1022
|
-
const store = reactive({
|
|
1023
|
-
messages: [],
|
|
1024
|
-
status: "idle",
|
|
1025
|
-
streamingContent: "",
|
|
1026
|
-
pendingToolCalls: [],
|
|
1027
|
-
error: null,
|
|
1028
|
-
model,
|
|
1029
|
-
tokenCount: 0,
|
|
1030
|
-
contextWarning: false
|
|
1031
|
-
});
|
|
1032
|
-
let _streamingMessage = null;
|
|
1033
|
-
function sendMessage(text) {
|
|
1034
|
-
if (store.status === "streaming") {
|
|
1035
|
-
return;
|
|
1036
|
-
}
|
|
1037
|
-
const userMsg = {
|
|
1038
|
-
id: uid(),
|
|
1039
|
-
role: "user",
|
|
1040
|
-
content: text,
|
|
1041
|
-
timestamp: Date.now()
|
|
1042
|
-
};
|
|
1043
|
-
store.messages.push(userMsg);
|
|
1044
|
-
beginAssistantTurn();
|
|
1045
|
-
}
|
|
1046
|
-
function beginAssistantTurn() {
|
|
1047
|
-
if (store.status === "streaming") {
|
|
1048
|
-
return;
|
|
1049
|
-
}
|
|
1050
|
-
store.error = null;
|
|
1051
|
-
store.streamingContent = "";
|
|
1052
|
-
store.pendingToolCalls = [];
|
|
1053
|
-
store.status = "streaming";
|
|
1054
|
-
_streamingMessage = {
|
|
1055
|
-
id: uid(),
|
|
1056
|
-
role: "assistant",
|
|
1057
|
-
content: "",
|
|
1058
|
-
timestamp: Date.now()
|
|
1059
|
-
};
|
|
1060
|
-
store.messages.push(_streamingMessage);
|
|
1061
|
-
_streamingMessage = store.messages.at(-1) ?? null;
|
|
1062
|
-
}
|
|
1063
|
-
function pushToolResultMessage(toolCallId, content) {
|
|
1064
|
-
store.messages.push({
|
|
1065
|
-
id: uid(),
|
|
1066
|
-
role: "tool",
|
|
1067
|
-
toolCallId,
|
|
1068
|
-
content,
|
|
1069
|
-
timestamp: Date.now()
|
|
1070
|
-
});
|
|
1071
|
-
}
|
|
1072
|
-
function appendDelta(content) {
|
|
1073
|
-
if (store.status !== "streaming" || !_streamingMessage) {
|
|
1074
|
-
return;
|
|
1075
|
-
}
|
|
1076
|
-
store.streamingContent += content;
|
|
1077
|
-
_streamingMessage.content = store.streamingContent;
|
|
1078
|
-
}
|
|
1079
|
-
function appendToolCallStart(id, name) {
|
|
1080
|
-
if (store.status !== "streaming") {
|
|
1081
|
-
return;
|
|
1082
|
-
}
|
|
1083
|
-
const tc = {
|
|
1084
|
-
id,
|
|
1085
|
-
name,
|
|
1086
|
-
arguments: "",
|
|
1087
|
-
result: null
|
|
1088
|
-
};
|
|
1089
|
-
store.pendingToolCalls.push(tc);
|
|
1090
|
-
if (_streamingMessage) {
|
|
1091
|
-
if (!_streamingMessage.toolCalls) {
|
|
1092
|
-
_streamingMessage.toolCalls = [];
|
|
1093
|
-
}
|
|
1094
|
-
_streamingMessage.toolCalls.push(tc);
|
|
1095
|
-
}
|
|
1096
|
-
}
|
|
1097
|
-
function appendToolCallDelta(id, args) {
|
|
1098
|
-
const tc = store.pendingToolCalls.find((t) => t.id === id);
|
|
1099
|
-
if (tc) {
|
|
1100
|
-
tc.arguments += args;
|
|
1101
|
-
}
|
|
1102
|
-
}
|
|
1103
|
-
function appendToolCallEnd(_id) {}
|
|
1104
|
-
function appendToolResult(id, result) {
|
|
1105
|
-
const tc = store.pendingToolCalls.find((t) => t.id === id);
|
|
1106
|
-
if (tc) {
|
|
1107
|
-
tc.result = result;
|
|
1108
|
-
}
|
|
1109
|
-
if (_streamingMessage?.toolCalls) {
|
|
1110
|
-
const mtc = _streamingMessage.toolCalls.find((t) => t.id === id);
|
|
1111
|
-
if (mtc) {
|
|
1112
|
-
mtc.result = result;
|
|
1113
|
-
}
|
|
1114
|
-
}
|
|
1115
|
-
}
|
|
1116
|
-
function finishStream(_stopReason) {
|
|
1117
|
-
store.status = "idle";
|
|
1118
|
-
store.streamingContent = "";
|
|
1119
|
-
store.pendingToolCalls = [];
|
|
1120
|
-
_streamingMessage = null;
|
|
1121
|
-
}
|
|
1122
|
-
function setError(message) {
|
|
1123
|
-
store.status = "error";
|
|
1124
|
-
store.error = message;
|
|
1125
|
-
store.streamingContent = "";
|
|
1126
|
-
if (_streamingMessage) {
|
|
1127
|
-
const idx = store.messages.lastIndexOf(_streamingMessage);
|
|
1128
|
-
if (idx !== -1) {
|
|
1129
|
-
store.messages.splice(idx, 1);
|
|
1130
|
-
}
|
|
1131
|
-
}
|
|
1132
|
-
_streamingMessage = null;
|
|
1133
|
-
}
|
|
1134
|
-
function cancelStream() {
|
|
1135
|
-
if (_streamingMessage) {
|
|
1136
|
-
const idx = store.messages.lastIndexOf(_streamingMessage);
|
|
1137
|
-
if (idx !== -1) {
|
|
1138
|
-
store.messages.splice(idx, 1);
|
|
1139
|
-
}
|
|
1140
|
-
}
|
|
1141
|
-
store.status = "idle";
|
|
1142
|
-
store.streamingContent = "";
|
|
1143
|
-
store.pendingToolCalls = [];
|
|
1144
|
-
_streamingMessage = null;
|
|
1145
|
-
store.error = null;
|
|
1146
|
-
}
|
|
1147
|
-
function clearChat() {
|
|
1148
|
-
store.messages.length = 0;
|
|
1149
|
-
store.status = "idle";
|
|
1150
|
-
store.streamingContent = "";
|
|
1151
|
-
store.pendingToolCalls = [];
|
|
1152
|
-
store.error = null;
|
|
1153
|
-
store.contextWarning = false;
|
|
1154
|
-
_streamingMessage = null;
|
|
1155
|
-
}
|
|
1156
|
-
function retryLast() {
|
|
1157
|
-
while (store.messages.length > 0 && store.messages.at(-1).role !== "user") {
|
|
1158
|
-
store.messages.pop();
|
|
1159
|
-
}
|
|
1160
|
-
if (store.messages.length > 0 && store.messages.at(-1).role === "user") {
|
|
1161
|
-
store.messages.pop();
|
|
1162
|
-
}
|
|
1163
|
-
store.status = "idle";
|
|
1164
|
-
store.error = null;
|
|
1165
|
-
}
|
|
1166
|
-
function setModel(m) {
|
|
1167
|
-
store.model = m;
|
|
1168
|
-
}
|
|
1169
|
-
function setTokenCount(count) {
|
|
1170
|
-
store.tokenCount = count;
|
|
1171
|
-
}
|
|
1172
|
-
function setContextWarning(warning) {
|
|
1173
|
-
store.contextWarning = warning;
|
|
1174
|
-
}
|
|
1175
|
-
function toMessagesArray() {
|
|
1176
|
-
const out = [];
|
|
1177
|
-
for (const msg of store.messages) {
|
|
1178
|
-
if (msg.role === "assistant" && msg.toolCalls && msg.toolCalls.length > 0) {
|
|
1179
|
-
out.push({
|
|
1180
|
-
role: "assistant",
|
|
1181
|
-
content: msg.content || null,
|
|
1182
|
-
tool_calls: msg.toolCalls.map((tc) => ({
|
|
1183
|
-
id: tc.id,
|
|
1184
|
-
type: "function",
|
|
1185
|
-
function: {
|
|
1186
|
-
name: tc.name,
|
|
1187
|
-
arguments: tc.arguments
|
|
1188
|
-
}
|
|
1189
|
-
}))
|
|
1190
|
-
});
|
|
1191
|
-
} else if (msg.role === "tool") {
|
|
1192
|
-
out.push({
|
|
1193
|
-
role: "tool",
|
|
1194
|
-
tool_call_id: msg.toolCallId,
|
|
1195
|
-
content: msg.content
|
|
1196
|
-
});
|
|
1197
|
-
} else {
|
|
1198
|
-
out.push({
|
|
1199
|
-
role: msg.role,
|
|
1200
|
-
content: msg.content
|
|
1201
|
-
});
|
|
1202
|
-
}
|
|
1203
|
-
}
|
|
1204
|
-
return out;
|
|
1205
|
-
}
|
|
1206
|
-
return Object.assign(store, {
|
|
1207
|
-
sendMessage,
|
|
1208
|
-
beginAssistantTurn,
|
|
1209
|
-
appendDelta,
|
|
1210
|
-
appendToolCallStart,
|
|
1211
|
-
appendToolCallDelta,
|
|
1212
|
-
appendToolCallEnd,
|
|
1213
|
-
appendToolResult,
|
|
1214
|
-
pushToolResultMessage,
|
|
1215
|
-
finishStream,
|
|
1216
|
-
setError,
|
|
1217
|
-
cancelStream,
|
|
1218
|
-
clearChat,
|
|
1219
|
-
retryLast,
|
|
1220
|
-
setModel,
|
|
1221
|
-
setTokenCount,
|
|
1222
|
-
setContextWarning,
|
|
1223
|
-
toMessagesArray
|
|
1224
|
-
});
|
|
1225
|
-
}
|
|
1226
|
-
// src/streaming-client.ts
|
|
1227
|
-
var STREAM_EVENT_TYPES = {
|
|
1228
|
-
DELTA: "delta",
|
|
1229
|
-
TOOL_CALL_START: "tool_call_start",
|
|
1230
|
-
TOOL_CALL_DELTA: "tool_call_delta",
|
|
1231
|
-
TOOL_CALL_END: "tool_call_end",
|
|
1232
|
-
DONE: "done",
|
|
1233
|
-
ERROR: "error"
|
|
1234
|
-
};
|
|
1235
|
-
function createOpenAIStreamingClient({
|
|
1236
|
-
baseUrl,
|
|
1237
|
-
apiKey,
|
|
1238
|
-
model = "gpt-4o",
|
|
1239
|
-
temperature
|
|
1240
|
-
}) {
|
|
1241
|
-
async function* streamChat(messages, tools, systemPrompt, signal) {
|
|
1242
|
-
const url = `${baseUrl}/chat/completions`;
|
|
1243
|
-
const body = {
|
|
1244
|
-
model,
|
|
1245
|
-
messages: [{ role: "system", content: systemPrompt }, ...messages],
|
|
1246
|
-
stream: true,
|
|
1247
|
-
stream_options: { include_usage: true }
|
|
1248
|
-
};
|
|
1249
|
-
if (temperature !== undefined) {
|
|
1250
|
-
body.temperature = temperature;
|
|
1251
|
-
}
|
|
1252
|
-
if (tools && tools.length > 0) {
|
|
1253
|
-
body.tools = tools;
|
|
1254
|
-
body.tool_choice = "auto";
|
|
1255
|
-
body.parallel_tool_calls = true;
|
|
1256
|
-
}
|
|
1257
|
-
let response;
|
|
1258
|
-
try {
|
|
1259
|
-
response = await fetch(url, {
|
|
1260
|
-
method: "POST",
|
|
1261
|
-
headers: {
|
|
1262
|
-
"Content-Type": "application/json",
|
|
1263
|
-
Authorization: `Bearer ${apiKey}`
|
|
1264
|
-
},
|
|
1265
|
-
body: JSON.stringify(body),
|
|
1266
|
-
signal
|
|
1267
|
-
});
|
|
1268
|
-
} catch (error) {
|
|
1269
|
-
if (error.name === "AbortError") {
|
|
1270
|
-
yield { type: "done", stopReason: "cancelled" };
|
|
1271
|
-
return;
|
|
1272
|
-
}
|
|
1273
|
-
yield {
|
|
1274
|
-
type: "error",
|
|
1275
|
-
message: `Network error: ${error.message}`
|
|
1276
|
-
};
|
|
1277
|
-
return;
|
|
1278
|
-
}
|
|
1279
|
-
if (!response.ok) {
|
|
1280
|
-
let errorBody = "";
|
|
1281
|
-
try {
|
|
1282
|
-
errorBody = await response.text();
|
|
1283
|
-
} catch {}
|
|
1284
|
-
yield {
|
|
1285
|
-
type: "error",
|
|
1286
|
-
message: `API error ${response.status}: ${errorBody || response.statusText}`,
|
|
1287
|
-
code: String(response.status)
|
|
1288
|
-
};
|
|
1289
|
-
return;
|
|
1290
|
-
}
|
|
1291
|
-
const reader = response.body?.getReader();
|
|
1292
|
-
if (!reader) {
|
|
1293
|
-
yield { type: "error", message: "No response body" };
|
|
1294
|
-
return;
|
|
1295
|
-
}
|
|
1296
|
-
const decoder = new TextDecoder;
|
|
1297
|
-
let buffer = "";
|
|
1298
|
-
const pendingToolCalls = new Map;
|
|
1299
|
-
try {
|
|
1300
|
-
while (true) {
|
|
1301
|
-
const { done, value } = await reader.read();
|
|
1302
|
-
if (done) {
|
|
1303
|
-
break;
|
|
1304
|
-
}
|
|
1305
|
-
buffer += decoder.decode(value, { stream: true });
|
|
1306
|
-
const lines = buffer.split(`
|
|
1307
|
-
`);
|
|
1308
|
-
buffer = lines.pop() || "";
|
|
1309
|
-
for (const line of lines) {
|
|
1310
|
-
const trimmed = line.trim();
|
|
1311
|
-
if (!trimmed || !trimmed.startsWith("data: ")) {
|
|
1312
|
-
continue;
|
|
1313
|
-
}
|
|
1314
|
-
const dataStr = trimmed.slice(6);
|
|
1315
|
-
if (dataStr === "[DONE]") {
|
|
1316
|
-
for (const tc of pendingToolCalls.values()) {
|
|
1317
|
-
yield { type: "tool_call_end", id: tc.id };
|
|
1318
|
-
}
|
|
1319
|
-
pendingToolCalls.clear();
|
|
1320
|
-
yield { type: "done", stopReason: "stop" };
|
|
1321
|
-
return;
|
|
1322
|
-
}
|
|
1323
|
-
let parsed;
|
|
1324
|
-
try {
|
|
1325
|
-
parsed = JSON.parse(dataStr);
|
|
1326
|
-
} catch {
|
|
1327
|
-
continue;
|
|
1328
|
-
}
|
|
1329
|
-
const choice = parsed.choices?.[0];
|
|
1330
|
-
if (!choice) {
|
|
1331
|
-
continue;
|
|
1332
|
-
}
|
|
1333
|
-
const { delta } = choice;
|
|
1334
|
-
if (!delta) {
|
|
1335
|
-
continue;
|
|
1336
|
-
}
|
|
1337
|
-
if (delta.content) {
|
|
1338
|
-
yield { type: "delta", content: delta.content };
|
|
1339
|
-
}
|
|
1340
|
-
if (delta.tool_calls) {
|
|
1341
|
-
for (const tc of delta.tool_calls) {
|
|
1342
|
-
const existing = pendingToolCalls.get(tc.index);
|
|
1343
|
-
if (tc.id) {
|
|
1344
|
-
const entry = {
|
|
1345
|
-
id: tc.id,
|
|
1346
|
-
name: tc.function?.name || "",
|
|
1347
|
-
args: tc.function?.arguments || ""
|
|
1348
|
-
};
|
|
1349
|
-
pendingToolCalls.set(tc.index, entry);
|
|
1350
|
-
yield { type: "tool_call_start", id: tc.id, name: entry.name };
|
|
1351
|
-
if (entry.args) {
|
|
1352
|
-
yield { type: "tool_call_delta", id: tc.id, args: entry.args };
|
|
1353
|
-
}
|
|
1354
|
-
} else if (existing && tc.function?.arguments) {
|
|
1355
|
-
existing.args += tc.function.arguments;
|
|
1356
|
-
yield {
|
|
1357
|
-
type: "tool_call_delta",
|
|
1358
|
-
id: existing.id,
|
|
1359
|
-
args: tc.function.arguments
|
|
1360
|
-
};
|
|
1361
|
-
}
|
|
1362
|
-
}
|
|
1363
|
-
}
|
|
1364
|
-
if (choice.finish_reason === "tool_calls") {
|
|
1365
|
-
for (const tc of pendingToolCalls.values()) {
|
|
1366
|
-
yield { type: "tool_call_end", id: tc.id };
|
|
1367
|
-
}
|
|
1368
|
-
pendingToolCalls.clear();
|
|
1369
|
-
yield { type: "done", stopReason: "tool_calls" };
|
|
1370
|
-
return;
|
|
1371
|
-
}
|
|
1372
|
-
if (choice.finish_reason === "stop" || choice.finish_reason === "length") {
|
|
1373
|
-
for (const tc of pendingToolCalls.values()) {
|
|
1374
|
-
yield { type: "tool_call_end", id: tc.id };
|
|
1375
|
-
}
|
|
1376
|
-
pendingToolCalls.clear();
|
|
1377
|
-
yield {
|
|
1378
|
-
type: "done",
|
|
1379
|
-
stopReason: choice.finish_reason === "length" ? "length" : "stop"
|
|
1380
|
-
};
|
|
1381
|
-
return;
|
|
1382
|
-
}
|
|
1383
|
-
}
|
|
1384
|
-
}
|
|
1385
|
-
for (const tc of pendingToolCalls.values()) {
|
|
1386
|
-
yield { type: "tool_call_end", id: tc.id };
|
|
1387
|
-
}
|
|
1388
|
-
pendingToolCalls.clear();
|
|
1389
|
-
yield { type: "done", stopReason: "stop" };
|
|
1390
|
-
} catch (error) {
|
|
1391
|
-
reader.cancel();
|
|
1392
|
-
if (error.name === "AbortError") {
|
|
1393
|
-
yield { type: "done", stopReason: "cancelled" };
|
|
1394
|
-
return;
|
|
1395
|
-
}
|
|
1396
|
-
yield {
|
|
1397
|
-
type: "error",
|
|
1398
|
-
message: `Stream error: ${error.message}`
|
|
1399
|
-
};
|
|
1400
|
-
}
|
|
1401
|
-
}
|
|
1402
|
-
return { streamChat };
|
|
1403
|
-
}
|
|
1404
|
-
function createAnthropicStreamingClient({
|
|
1405
|
-
baseUrl: _,
|
|
1406
|
-
apiKey: __
|
|
1407
|
-
}) {
|
|
1408
|
-
async function* streamChat() {
|
|
1409
|
-
yield {
|
|
1410
|
-
type: "error",
|
|
1411
|
-
message: "Anthropic provider is not yet implemented (planned for v2). Use OpenAI.",
|
|
1412
|
-
code: "NOT_IMPLEMENTED"
|
|
1413
|
-
};
|
|
1414
|
-
}
|
|
1415
|
-
return { streamChat };
|
|
1416
|
-
}
|
|
1417
|
-
function createProxyStreamingClient({
|
|
1418
|
-
chatUrl,
|
|
1419
|
-
model = "gpt-4o",
|
|
1420
|
-
apiKey,
|
|
1421
|
-
baseUrl
|
|
1422
|
-
}) {
|
|
1423
|
-
async function* streamChat(messages, tools, systemPrompt, signal) {
|
|
1424
|
-
const headers = { "Content-Type": "application/json" };
|
|
1425
|
-
if (apiKey) {
|
|
1426
|
-
headers["X-Api-Key"] = apiKey;
|
|
1427
|
-
}
|
|
1428
|
-
if (baseUrl) {
|
|
1429
|
-
headers["X-Api-Base-URL"] = baseUrl;
|
|
1430
|
-
}
|
|
1431
|
-
let response;
|
|
1432
|
-
try {
|
|
1433
|
-
response = await fetch(chatUrl, {
|
|
1434
|
-
method: "POST",
|
|
1435
|
-
headers,
|
|
1436
|
-
body: JSON.stringify({ messages, tools, systemPrompt, model }),
|
|
1437
|
-
signal
|
|
1438
|
-
});
|
|
1439
|
-
} catch (error) {
|
|
1440
|
-
if (error.name === "AbortError") {
|
|
1441
|
-
yield { type: "done", stopReason: "cancelled" };
|
|
1442
|
-
return;
|
|
1443
|
-
}
|
|
1444
|
-
yield {
|
|
1445
|
-
type: "error",
|
|
1446
|
-
message: `Network error: ${error.message}`
|
|
1447
|
-
};
|
|
1448
|
-
return;
|
|
1449
|
-
}
|
|
1450
|
-
if (!response.ok) {
|
|
1451
|
-
let errorBody = "";
|
|
1452
|
-
try {
|
|
1453
|
-
errorBody = await response.text();
|
|
1454
|
-
} catch {}
|
|
1455
|
-
let cleanMessage = errorBody || response.statusText;
|
|
1456
|
-
try {
|
|
1457
|
-
const parsed = JSON.parse(errorBody);
|
|
1458
|
-
if (typeof parsed.error === "string") {
|
|
1459
|
-
cleanMessage = parsed.error;
|
|
1460
|
-
} else if (parsed.error?.message) {
|
|
1461
|
-
cleanMessage = parsed.error.message;
|
|
1462
|
-
}
|
|
1463
|
-
} catch {}
|
|
1464
|
-
yield {
|
|
1465
|
-
type: "error",
|
|
1466
|
-
message: cleanMessage,
|
|
1467
|
-
code: String(response.status)
|
|
1468
|
-
};
|
|
1469
|
-
return;
|
|
1470
|
-
}
|
|
1471
|
-
const reader = response.body?.getReader();
|
|
1472
|
-
if (!reader) {
|
|
1473
|
-
yield { type: "error", message: "No response body" };
|
|
1474
|
-
return;
|
|
1475
|
-
}
|
|
1476
|
-
const decoder = new TextDecoder;
|
|
1477
|
-
let buffer = "";
|
|
1478
|
-
try {
|
|
1479
|
-
while (true) {
|
|
1480
|
-
const { done, value } = await reader.read();
|
|
1481
|
-
if (done) {
|
|
1482
|
-
break;
|
|
1483
|
-
}
|
|
1484
|
-
buffer += decoder.decode(value, { stream: true });
|
|
1485
|
-
const lines = buffer.split(`
|
|
1486
|
-
`);
|
|
1487
|
-
buffer = lines.pop() || "";
|
|
1488
|
-
for (const line of lines) {
|
|
1489
|
-
const trimmed = line.trim();
|
|
1490
|
-
if (!trimmed || !trimmed.startsWith("data: ")) {
|
|
1491
|
-
continue;
|
|
1492
|
-
}
|
|
1493
|
-
const dataStr = trimmed.slice(6);
|
|
1494
|
-
let event;
|
|
1495
|
-
try {
|
|
1496
|
-
event = JSON.parse(dataStr);
|
|
1497
|
-
} catch {
|
|
1498
|
-
continue;
|
|
1499
|
-
}
|
|
1500
|
-
yield event;
|
|
1501
|
-
if (event.type === "done" || event.type === "error") {
|
|
1502
|
-
return;
|
|
1503
|
-
}
|
|
1504
|
-
}
|
|
1505
|
-
}
|
|
1506
|
-
} catch (error) {
|
|
1507
|
-
reader.cancel();
|
|
1508
|
-
if (error.name === "AbortError") {
|
|
1509
|
-
yield { type: "done", stopReason: "cancelled" };
|
|
1510
|
-
return;
|
|
1511
|
-
}
|
|
1512
|
-
yield {
|
|
1513
|
-
type: "error",
|
|
1514
|
-
message: `Stream error: ${error.message}`
|
|
1515
|
-
};
|
|
1516
|
-
}
|
|
1517
|
-
}
|
|
1518
|
-
return { streamChat };
|
|
1519
|
-
}
|
|
1520
|
-
export {
|
|
1521
|
-
createToolRegistry,
|
|
1522
|
-
createToolDefinition,
|
|
1523
|
-
createProxyStreamingClient,
|
|
1524
|
-
createOpenAIStreamingClient,
|
|
1525
|
-
createChatState,
|
|
1526
|
-
createAnthropicStreamingClient,
|
|
1527
|
-
STREAM_EVENT_TYPES
|
|
1528
|
-
};
|
|
1529
|
-
|
|
1530
|
-
//# debugId=BF651F4D1B20097764756E2164756E21
|
|
1531
|
-
//# sourceMappingURL=index.js.map
|