@polka-codes/core 0.4.2 → 0.4.4
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.js +1572 -626
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -10,7 +10,7 @@ var __export = (target, all) => {
|
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
// ../../node_modules/@anthropic-ai/sdk/version.mjs
|
|
13
|
-
var VERSION = "0.
|
|
13
|
+
var VERSION = "0.36.2";
|
|
14
14
|
|
|
15
15
|
// ../../node_modules/@anthropic-ai/sdk/_shims/registry.mjs
|
|
16
16
|
var auto = false;
|
|
@@ -295,6 +295,35 @@ LineDecoder.NEWLINE_CHARS = new Set([`
|
|
|
295
295
|
`, "\r"]);
|
|
296
296
|
LineDecoder.NEWLINE_REGEXP = /\r\n|[\n\r]/g;
|
|
297
297
|
|
|
298
|
+
// ../../node_modules/@anthropic-ai/sdk/internal/stream-utils.mjs
|
|
299
|
+
function ReadableStreamToAsyncIterable(stream) {
|
|
300
|
+
if (stream[Symbol.asyncIterator])
|
|
301
|
+
return stream;
|
|
302
|
+
const reader = stream.getReader();
|
|
303
|
+
return {
|
|
304
|
+
async next() {
|
|
305
|
+
try {
|
|
306
|
+
const result = await reader.read();
|
|
307
|
+
if (result?.done)
|
|
308
|
+
reader.releaseLock();
|
|
309
|
+
return result;
|
|
310
|
+
} catch (e) {
|
|
311
|
+
reader.releaseLock();
|
|
312
|
+
throw e;
|
|
313
|
+
}
|
|
314
|
+
},
|
|
315
|
+
async return() {
|
|
316
|
+
const cancelPromise = reader.cancel();
|
|
317
|
+
reader.releaseLock();
|
|
318
|
+
await cancelPromise;
|
|
319
|
+
return { done: true, value: undefined };
|
|
320
|
+
},
|
|
321
|
+
[Symbol.asyncIterator]() {
|
|
322
|
+
return this;
|
|
323
|
+
}
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
298
327
|
// ../../node_modules/@anthropic-ai/sdk/streaming.mjs
|
|
299
328
|
class Stream {
|
|
300
329
|
constructor(iterator, controller) {
|
|
@@ -352,7 +381,7 @@ class Stream {
|
|
|
352
381
|
let consumed = false;
|
|
353
382
|
async function* iterLines() {
|
|
354
383
|
const lineDecoder = new LineDecoder;
|
|
355
|
-
const iter =
|
|
384
|
+
const iter = ReadableStreamToAsyncIterable(readableStream);
|
|
356
385
|
for await (const chunk of iter) {
|
|
357
386
|
for (const line of lineDecoder.decode(chunk)) {
|
|
358
387
|
yield line;
|
|
@@ -444,7 +473,7 @@ async function* _iterSSEMessages(response, controller) {
|
|
|
444
473
|
}
|
|
445
474
|
const sseDecoder = new SSEDecoder;
|
|
446
475
|
const lineDecoder = new LineDecoder;
|
|
447
|
-
const iter =
|
|
476
|
+
const iter = ReadableStreamToAsyncIterable(response.body);
|
|
448
477
|
for await (const sseChunk of iterSSEChunks(iter)) {
|
|
449
478
|
for (const line of lineDecoder.decode(sseChunk)) {
|
|
450
479
|
const sse = sseDecoder.decode(line);
|
|
@@ -543,33 +572,6 @@ function partition(str, delimiter) {
|
|
|
543
572
|
}
|
|
544
573
|
return [str, "", ""];
|
|
545
574
|
}
|
|
546
|
-
function readableStreamAsyncIterable(stream) {
|
|
547
|
-
if (stream[Symbol.asyncIterator])
|
|
548
|
-
return stream;
|
|
549
|
-
const reader = stream.getReader();
|
|
550
|
-
return {
|
|
551
|
-
async next() {
|
|
552
|
-
try {
|
|
553
|
-
const result = await reader.read();
|
|
554
|
-
if (result?.done)
|
|
555
|
-
reader.releaseLock();
|
|
556
|
-
return result;
|
|
557
|
-
} catch (e) {
|
|
558
|
-
reader.releaseLock();
|
|
559
|
-
throw e;
|
|
560
|
-
}
|
|
561
|
-
},
|
|
562
|
-
async return() {
|
|
563
|
-
const cancelPromise = reader.cancel();
|
|
564
|
-
reader.releaseLock();
|
|
565
|
-
await cancelPromise;
|
|
566
|
-
return { done: true, value: undefined };
|
|
567
|
-
},
|
|
568
|
-
[Symbol.asyncIterator]() {
|
|
569
|
-
return this;
|
|
570
|
-
}
|
|
571
|
-
};
|
|
572
|
-
}
|
|
573
575
|
|
|
574
576
|
// ../../node_modules/@anthropic-ai/sdk/uploads.mjs
|
|
575
577
|
var isResponseLike = (value) => value != null && typeof value === "object" && typeof value.url === "string" && typeof value.blob === "function";
|
|
@@ -920,7 +922,14 @@ class APIClient {
|
|
|
920
922
|
if (signal)
|
|
921
923
|
signal.addEventListener("abort", () => controller.abort());
|
|
922
924
|
const timeout = setTimeout(() => controller.abort(), ms);
|
|
923
|
-
|
|
925
|
+
const fetchOptions = {
|
|
926
|
+
signal: controller.signal,
|
|
927
|
+
...options
|
|
928
|
+
};
|
|
929
|
+
if (fetchOptions.method) {
|
|
930
|
+
fetchOptions.method = fetchOptions.method.toUpperCase();
|
|
931
|
+
}
|
|
932
|
+
return this.fetch.call(undefined, url, fetchOptions).finally(() => {
|
|
924
933
|
clearTimeout(timeout);
|
|
925
934
|
});
|
|
926
935
|
}
|
|
@@ -1380,7 +1389,7 @@ class JSONLDecoder {
|
|
|
1380
1389
|
controller.abort();
|
|
1381
1390
|
throw new AnthropicError(`Attempted to iterate over a response with no body`);
|
|
1382
1391
|
}
|
|
1383
|
-
return new JSONLDecoder(
|
|
1392
|
+
return new JSONLDecoder(ReadableStreamToAsyncIterable(response.body), controller);
|
|
1384
1393
|
}
|
|
1385
1394
|
}
|
|
1386
1395
|
|
|
@@ -1424,6 +1433,19 @@ class Batches extends APIResource {
|
|
|
1424
1433
|
}
|
|
1425
1434
|
});
|
|
1426
1435
|
}
|
|
1436
|
+
delete(messageBatchId, params = {}, options) {
|
|
1437
|
+
if (isRequestOptions(params)) {
|
|
1438
|
+
return this.delete(messageBatchId, {}, params);
|
|
1439
|
+
}
|
|
1440
|
+
const { betas } = params;
|
|
1441
|
+
return this._client.delete(`/v1/messages/batches/${messageBatchId}?beta=true`, {
|
|
1442
|
+
...options,
|
|
1443
|
+
headers: {
|
|
1444
|
+
"anthropic-beta": [...betas ?? [], "message-batches-2024-09-24"].toString(),
|
|
1445
|
+
...options?.headers
|
|
1446
|
+
}
|
|
1447
|
+
});
|
|
1448
|
+
}
|
|
1427
1449
|
cancel(messageBatchId, params = {}, options) {
|
|
1428
1450
|
if (isRequestOptions(params)) {
|
|
1429
1451
|
return this.cancel(messageBatchId, {}, params);
|
|
@@ -1450,6 +1472,7 @@ class Batches extends APIResource {
|
|
|
1450
1472
|
...options,
|
|
1451
1473
|
headers: {
|
|
1452
1474
|
"anthropic-beta": [...betas ?? [], "message-batches-2024-09-24"].toString(),
|
|
1475
|
+
Accept: "application/binary",
|
|
1453
1476
|
...options?.headers
|
|
1454
1477
|
},
|
|
1455
1478
|
__binaryResponse: true
|
|
@@ -1461,92 +1484,6 @@ class BetaMessageBatchesPage extends Page {
|
|
|
1461
1484
|
}
|
|
1462
1485
|
Batches.BetaMessageBatchesPage = BetaMessageBatchesPage;
|
|
1463
1486
|
|
|
1464
|
-
// ../../node_modules/@anthropic-ai/sdk/resources/beta/messages/messages.mjs
|
|
1465
|
-
class Messages extends APIResource {
|
|
1466
|
-
constructor() {
|
|
1467
|
-
super(...arguments);
|
|
1468
|
-
this.batches = new Batches(this._client);
|
|
1469
|
-
}
|
|
1470
|
-
create(params, options) {
|
|
1471
|
-
const { betas, ...body } = params;
|
|
1472
|
-
return this._client.post("/v1/messages?beta=true", {
|
|
1473
|
-
body,
|
|
1474
|
-
timeout: this._client._options.timeout ?? 600000,
|
|
1475
|
-
...options,
|
|
1476
|
-
headers: {
|
|
1477
|
-
...betas?.toString() != null ? { "anthropic-beta": betas?.toString() } : undefined,
|
|
1478
|
-
...options?.headers
|
|
1479
|
-
},
|
|
1480
|
-
stream: params.stream ?? false
|
|
1481
|
-
});
|
|
1482
|
-
}
|
|
1483
|
-
countTokens(params, options) {
|
|
1484
|
-
const { betas, ...body } = params;
|
|
1485
|
-
return this._client.post("/v1/messages/count_tokens?beta=true", {
|
|
1486
|
-
body,
|
|
1487
|
-
...options,
|
|
1488
|
-
headers: {
|
|
1489
|
-
"anthropic-beta": [...betas ?? [], "token-counting-2024-11-01"].toString(),
|
|
1490
|
-
...options?.headers
|
|
1491
|
-
}
|
|
1492
|
-
});
|
|
1493
|
-
}
|
|
1494
|
-
}
|
|
1495
|
-
Messages.Batches = Batches;
|
|
1496
|
-
Messages.BetaMessageBatchesPage = BetaMessageBatchesPage;
|
|
1497
|
-
|
|
1498
|
-
// ../../node_modules/@anthropic-ai/sdk/resources/beta/beta.mjs
|
|
1499
|
-
class Beta extends APIResource {
|
|
1500
|
-
constructor() {
|
|
1501
|
-
super(...arguments);
|
|
1502
|
-
this.models = new Models(this._client);
|
|
1503
|
-
this.messages = new Messages(this._client);
|
|
1504
|
-
}
|
|
1505
|
-
}
|
|
1506
|
-
Beta.Models = Models;
|
|
1507
|
-
Beta.BetaModelInfosPage = BetaModelInfosPage;
|
|
1508
|
-
Beta.Messages = Messages;
|
|
1509
|
-
// ../../node_modules/@anthropic-ai/sdk/resources/completions.mjs
|
|
1510
|
-
class Completions extends APIResource {
|
|
1511
|
-
create(body, options) {
|
|
1512
|
-
return this._client.post("/v1/complete", {
|
|
1513
|
-
body,
|
|
1514
|
-
timeout: this._client._options.timeout ?? 600000,
|
|
1515
|
-
...options,
|
|
1516
|
-
stream: body.stream ?? false
|
|
1517
|
-
});
|
|
1518
|
-
}
|
|
1519
|
-
}
|
|
1520
|
-
// ../../node_modules/@anthropic-ai/sdk/resources/messages/batches.mjs
|
|
1521
|
-
class Batches2 extends APIResource {
|
|
1522
|
-
create(body, options) {
|
|
1523
|
-
return this._client.post("/v1/messages/batches", { body, ...options });
|
|
1524
|
-
}
|
|
1525
|
-
retrieve(messageBatchId, options) {
|
|
1526
|
-
return this._client.get(`/v1/messages/batches/${messageBatchId}`, options);
|
|
1527
|
-
}
|
|
1528
|
-
list(query = {}, options) {
|
|
1529
|
-
if (isRequestOptions(query)) {
|
|
1530
|
-
return this.list({}, query);
|
|
1531
|
-
}
|
|
1532
|
-
return this._client.getAPIList("/v1/messages/batches", MessageBatchesPage, { query, ...options });
|
|
1533
|
-
}
|
|
1534
|
-
cancel(messageBatchId, options) {
|
|
1535
|
-
return this._client.post(`/v1/messages/batches/${messageBatchId}/cancel`, options);
|
|
1536
|
-
}
|
|
1537
|
-
async results(messageBatchId, options) {
|
|
1538
|
-
const batch = await this.retrieve(messageBatchId);
|
|
1539
|
-
if (!batch.results_url) {
|
|
1540
|
-
throw new AnthropicError(`No batch \`results_url\`; Has it finished processing? ${batch.processing_status} - ${batch.id}`);
|
|
1541
|
-
}
|
|
1542
|
-
return this._client.get(batch.results_url, { ...options, __binaryResponse: true })._thenUnwrap((_, props) => JSONLDecoder.fromResponse(props.response, props.controller));
|
|
1543
|
-
}
|
|
1544
|
-
}
|
|
1545
|
-
|
|
1546
|
-
class MessageBatchesPage extends Page {
|
|
1547
|
-
}
|
|
1548
|
-
Batches2.MessageBatchesPage = MessageBatchesPage;
|
|
1549
|
-
|
|
1550
1487
|
// ../../node_modules/@anthropic-ai/sdk/_vendor/partial-json-parser/parser.mjs
|
|
1551
1488
|
var tokenize = (input) => {
|
|
1552
1489
|
let current = 0;
|
|
@@ -1767,7 +1704,7 @@ var generate = (tokens) => {
|
|
|
1767
1704
|
};
|
|
1768
1705
|
var partialParse = (input) => JSON.parse(generate(unstrip(strip(tokenize(input)))));
|
|
1769
1706
|
|
|
1770
|
-
// ../../node_modules/@anthropic-ai/sdk/lib/
|
|
1707
|
+
// ../../node_modules/@anthropic-ai/sdk/lib/BetaMessageStream.mjs
|
|
1771
1708
|
var __classPrivateFieldSet2 = function(receiver, state, value, kind2, f) {
|
|
1772
1709
|
if (kind2 === "m")
|
|
1773
1710
|
throw new TypeError("Private method is not writable");
|
|
@@ -1784,57 +1721,61 @@ var __classPrivateFieldGet2 = function(receiver, state, kind2, f) {
|
|
|
1784
1721
|
throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
1785
1722
|
return kind2 === "m" ? f : kind2 === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
1786
1723
|
};
|
|
1787
|
-
var
|
|
1788
|
-
var
|
|
1789
|
-
var
|
|
1790
|
-
var
|
|
1791
|
-
var
|
|
1792
|
-
var
|
|
1793
|
-
var
|
|
1794
|
-
var
|
|
1795
|
-
var
|
|
1796
|
-
var
|
|
1797
|
-
var
|
|
1798
|
-
var
|
|
1799
|
-
var
|
|
1800
|
-
var
|
|
1801
|
-
var
|
|
1802
|
-
var
|
|
1803
|
-
var
|
|
1804
|
-
var
|
|
1805
|
-
var
|
|
1806
|
-
var
|
|
1724
|
+
var _BetaMessageStream_instances;
|
|
1725
|
+
var _BetaMessageStream_currentMessageSnapshot;
|
|
1726
|
+
var _BetaMessageStream_connectedPromise;
|
|
1727
|
+
var _BetaMessageStream_resolveConnectedPromise;
|
|
1728
|
+
var _BetaMessageStream_rejectConnectedPromise;
|
|
1729
|
+
var _BetaMessageStream_endPromise;
|
|
1730
|
+
var _BetaMessageStream_resolveEndPromise;
|
|
1731
|
+
var _BetaMessageStream_rejectEndPromise;
|
|
1732
|
+
var _BetaMessageStream_listeners;
|
|
1733
|
+
var _BetaMessageStream_ended;
|
|
1734
|
+
var _BetaMessageStream_errored;
|
|
1735
|
+
var _BetaMessageStream_aborted;
|
|
1736
|
+
var _BetaMessageStream_catchingPromiseCreated;
|
|
1737
|
+
var _BetaMessageStream_response;
|
|
1738
|
+
var _BetaMessageStream_request_id;
|
|
1739
|
+
var _BetaMessageStream_getFinalMessage;
|
|
1740
|
+
var _BetaMessageStream_getFinalText;
|
|
1741
|
+
var _BetaMessageStream_handleError;
|
|
1742
|
+
var _BetaMessageStream_beginRequest;
|
|
1743
|
+
var _BetaMessageStream_addStreamEvent;
|
|
1744
|
+
var _BetaMessageStream_endRequest;
|
|
1745
|
+
var _BetaMessageStream_accumulateMessage;
|
|
1807
1746
|
var JSON_BUF_PROPERTY = "__json_buf";
|
|
1808
1747
|
|
|
1809
|
-
class
|
|
1748
|
+
class BetaMessageStream {
|
|
1810
1749
|
constructor() {
|
|
1811
|
-
|
|
1750
|
+
_BetaMessageStream_instances.add(this);
|
|
1812
1751
|
this.messages = [];
|
|
1813
1752
|
this.receivedMessages = [];
|
|
1814
|
-
|
|
1753
|
+
_BetaMessageStream_currentMessageSnapshot.set(this, undefined);
|
|
1815
1754
|
this.controller = new AbortController;
|
|
1816
|
-
|
|
1817
|
-
|
|
1755
|
+
_BetaMessageStream_connectedPromise.set(this, undefined);
|
|
1756
|
+
_BetaMessageStream_resolveConnectedPromise.set(this, () => {
|
|
1818
1757
|
});
|
|
1819
|
-
|
|
1758
|
+
_BetaMessageStream_rejectConnectedPromise.set(this, () => {
|
|
1820
1759
|
});
|
|
1821
|
-
|
|
1822
|
-
|
|
1760
|
+
_BetaMessageStream_endPromise.set(this, undefined);
|
|
1761
|
+
_BetaMessageStream_resolveEndPromise.set(this, () => {
|
|
1823
1762
|
});
|
|
1824
|
-
|
|
1763
|
+
_BetaMessageStream_rejectEndPromise.set(this, () => {
|
|
1825
1764
|
});
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1765
|
+
_BetaMessageStream_listeners.set(this, {});
|
|
1766
|
+
_BetaMessageStream_ended.set(this, false);
|
|
1767
|
+
_BetaMessageStream_errored.set(this, false);
|
|
1768
|
+
_BetaMessageStream_aborted.set(this, false);
|
|
1769
|
+
_BetaMessageStream_catchingPromiseCreated.set(this, false);
|
|
1770
|
+
_BetaMessageStream_response.set(this, undefined);
|
|
1771
|
+
_BetaMessageStream_request_id.set(this, undefined);
|
|
1772
|
+
_BetaMessageStream_handleError.set(this, (error) => {
|
|
1773
|
+
__classPrivateFieldSet2(this, _BetaMessageStream_errored, true, "f");
|
|
1833
1774
|
if (error instanceof Error && error.name === "AbortError") {
|
|
1834
1775
|
error = new APIUserAbortError;
|
|
1835
1776
|
}
|
|
1836
1777
|
if (error instanceof APIUserAbortError) {
|
|
1837
|
-
__classPrivateFieldSet2(this,
|
|
1778
|
+
__classPrivateFieldSet2(this, _BetaMessageStream_aborted, true, "f");
|
|
1838
1779
|
return this._emit("abort", error);
|
|
1839
1780
|
}
|
|
1840
1781
|
if (error instanceof AnthropicError) {
|
|
@@ -1847,26 +1788,43 @@ class MessageStream {
|
|
|
1847
1788
|
}
|
|
1848
1789
|
return this._emit("error", new AnthropicError(String(error)));
|
|
1849
1790
|
});
|
|
1850
|
-
__classPrivateFieldSet2(this,
|
|
1851
|
-
__classPrivateFieldSet2(this,
|
|
1852
|
-
__classPrivateFieldSet2(this,
|
|
1791
|
+
__classPrivateFieldSet2(this, _BetaMessageStream_connectedPromise, new Promise((resolve, reject) => {
|
|
1792
|
+
__classPrivateFieldSet2(this, _BetaMessageStream_resolveConnectedPromise, resolve, "f");
|
|
1793
|
+
__classPrivateFieldSet2(this, _BetaMessageStream_rejectConnectedPromise, reject, "f");
|
|
1853
1794
|
}), "f");
|
|
1854
|
-
__classPrivateFieldSet2(this,
|
|
1855
|
-
__classPrivateFieldSet2(this,
|
|
1856
|
-
__classPrivateFieldSet2(this,
|
|
1795
|
+
__classPrivateFieldSet2(this, _BetaMessageStream_endPromise, new Promise((resolve, reject) => {
|
|
1796
|
+
__classPrivateFieldSet2(this, _BetaMessageStream_resolveEndPromise, resolve, "f");
|
|
1797
|
+
__classPrivateFieldSet2(this, _BetaMessageStream_rejectEndPromise, reject, "f");
|
|
1857
1798
|
}), "f");
|
|
1858
|
-
__classPrivateFieldGet2(this,
|
|
1799
|
+
__classPrivateFieldGet2(this, _BetaMessageStream_connectedPromise, "f").catch(() => {
|
|
1859
1800
|
});
|
|
1860
|
-
__classPrivateFieldGet2(this,
|
|
1801
|
+
__classPrivateFieldGet2(this, _BetaMessageStream_endPromise, "f").catch(() => {
|
|
1861
1802
|
});
|
|
1862
1803
|
}
|
|
1804
|
+
get response() {
|
|
1805
|
+
return __classPrivateFieldGet2(this, _BetaMessageStream_response, "f");
|
|
1806
|
+
}
|
|
1807
|
+
get request_id() {
|
|
1808
|
+
return __classPrivateFieldGet2(this, _BetaMessageStream_request_id, "f");
|
|
1809
|
+
}
|
|
1810
|
+
async withResponse() {
|
|
1811
|
+
const response = await __classPrivateFieldGet2(this, _BetaMessageStream_connectedPromise, "f");
|
|
1812
|
+
if (!response) {
|
|
1813
|
+
throw new Error("Could not resolve a `Response` object");
|
|
1814
|
+
}
|
|
1815
|
+
return {
|
|
1816
|
+
data: this,
|
|
1817
|
+
response,
|
|
1818
|
+
request_id: response.headers.get("request-id")
|
|
1819
|
+
};
|
|
1820
|
+
}
|
|
1863
1821
|
static fromReadableStream(stream) {
|
|
1864
|
-
const runner = new
|
|
1822
|
+
const runner = new BetaMessageStream;
|
|
1865
1823
|
runner._run(() => runner._fromReadableStream(stream));
|
|
1866
1824
|
return runner;
|
|
1867
1825
|
}
|
|
1868
1826
|
static createMessage(messages, params, options) {
|
|
1869
|
-
const runner = new
|
|
1827
|
+
const runner = new BetaMessageStream;
|
|
1870
1828
|
for (const message of params.messages) {
|
|
1871
1829
|
runner._addMessageParam(message);
|
|
1872
1830
|
}
|
|
@@ -1877,7 +1835,7 @@ class MessageStream {
|
|
|
1877
1835
|
executor().then(() => {
|
|
1878
1836
|
this._emitFinal();
|
|
1879
1837
|
this._emit("end");
|
|
1880
|
-
}, __classPrivateFieldGet2(this,
|
|
1838
|
+
}, __classPrivateFieldGet2(this, _BetaMessageStream_handleError, "f"));
|
|
1881
1839
|
}
|
|
1882
1840
|
_addMessageParam(message) {
|
|
1883
1841
|
this.messages.push(message);
|
|
@@ -1895,42 +1853,44 @@ class MessageStream {
|
|
|
1895
1853
|
this.controller.abort();
|
|
1896
1854
|
signal.addEventListener("abort", () => this.controller.abort());
|
|
1897
1855
|
}
|
|
1898
|
-
__classPrivateFieldGet2(this,
|
|
1899
|
-
const stream = await messages.create({ ...params, stream: true }, { ...options, signal: this.controller.signal });
|
|
1900
|
-
this._connected();
|
|
1856
|
+
__classPrivateFieldGet2(this, _BetaMessageStream_instances, "m", _BetaMessageStream_beginRequest).call(this);
|
|
1857
|
+
const { response, data: stream } = await messages.create({ ...params, stream: true }, { ...options, signal: this.controller.signal }).withResponse();
|
|
1858
|
+
this._connected(response);
|
|
1901
1859
|
for await (const event of stream) {
|
|
1902
|
-
__classPrivateFieldGet2(this,
|
|
1860
|
+
__classPrivateFieldGet2(this, _BetaMessageStream_instances, "m", _BetaMessageStream_addStreamEvent).call(this, event);
|
|
1903
1861
|
}
|
|
1904
1862
|
if (stream.controller.signal?.aborted) {
|
|
1905
1863
|
throw new APIUserAbortError;
|
|
1906
1864
|
}
|
|
1907
|
-
__classPrivateFieldGet2(this,
|
|
1865
|
+
__classPrivateFieldGet2(this, _BetaMessageStream_instances, "m", _BetaMessageStream_endRequest).call(this);
|
|
1908
1866
|
}
|
|
1909
|
-
_connected() {
|
|
1867
|
+
_connected(response) {
|
|
1910
1868
|
if (this.ended)
|
|
1911
1869
|
return;
|
|
1912
|
-
|
|
1870
|
+
__classPrivateFieldSet2(this, _BetaMessageStream_response, response, "f");
|
|
1871
|
+
__classPrivateFieldSet2(this, _BetaMessageStream_request_id, response?.headers.get("request-id"), "f");
|
|
1872
|
+
__classPrivateFieldGet2(this, _BetaMessageStream_resolveConnectedPromise, "f").call(this, response);
|
|
1913
1873
|
this._emit("connect");
|
|
1914
1874
|
}
|
|
1915
1875
|
get ended() {
|
|
1916
|
-
return __classPrivateFieldGet2(this,
|
|
1876
|
+
return __classPrivateFieldGet2(this, _BetaMessageStream_ended, "f");
|
|
1917
1877
|
}
|
|
1918
1878
|
get errored() {
|
|
1919
|
-
return __classPrivateFieldGet2(this,
|
|
1879
|
+
return __classPrivateFieldGet2(this, _BetaMessageStream_errored, "f");
|
|
1920
1880
|
}
|
|
1921
1881
|
get aborted() {
|
|
1922
|
-
return __classPrivateFieldGet2(this,
|
|
1882
|
+
return __classPrivateFieldGet2(this, _BetaMessageStream_aborted, "f");
|
|
1923
1883
|
}
|
|
1924
1884
|
abort() {
|
|
1925
1885
|
this.controller.abort();
|
|
1926
1886
|
}
|
|
1927
1887
|
on(event, listener) {
|
|
1928
|
-
const listeners = __classPrivateFieldGet2(this,
|
|
1888
|
+
const listeners = __classPrivateFieldGet2(this, _BetaMessageStream_listeners, "f")[event] || (__classPrivateFieldGet2(this, _BetaMessageStream_listeners, "f")[event] = []);
|
|
1929
1889
|
listeners.push({ listener });
|
|
1930
1890
|
return this;
|
|
1931
1891
|
}
|
|
1932
1892
|
off(event, listener) {
|
|
1933
|
-
const listeners = __classPrivateFieldGet2(this,
|
|
1893
|
+
const listeners = __classPrivateFieldGet2(this, _BetaMessageStream_listeners, "f")[event];
|
|
1934
1894
|
if (!listeners)
|
|
1935
1895
|
return this;
|
|
1936
1896
|
const index = listeners.findIndex((l) => l.listener === listener);
|
|
@@ -1939,69 +1899,69 @@ class MessageStream {
|
|
|
1939
1899
|
return this;
|
|
1940
1900
|
}
|
|
1941
1901
|
once(event, listener) {
|
|
1942
|
-
const listeners = __classPrivateFieldGet2(this,
|
|
1902
|
+
const listeners = __classPrivateFieldGet2(this, _BetaMessageStream_listeners, "f")[event] || (__classPrivateFieldGet2(this, _BetaMessageStream_listeners, "f")[event] = []);
|
|
1943
1903
|
listeners.push({ listener, once: true });
|
|
1944
1904
|
return this;
|
|
1945
1905
|
}
|
|
1946
1906
|
emitted(event) {
|
|
1947
1907
|
return new Promise((resolve, reject) => {
|
|
1948
|
-
__classPrivateFieldSet2(this,
|
|
1908
|
+
__classPrivateFieldSet2(this, _BetaMessageStream_catchingPromiseCreated, true, "f");
|
|
1949
1909
|
if (event !== "error")
|
|
1950
1910
|
this.once("error", reject);
|
|
1951
1911
|
this.once(event, resolve);
|
|
1952
1912
|
});
|
|
1953
1913
|
}
|
|
1954
1914
|
async done() {
|
|
1955
|
-
__classPrivateFieldSet2(this,
|
|
1956
|
-
await __classPrivateFieldGet2(this,
|
|
1915
|
+
__classPrivateFieldSet2(this, _BetaMessageStream_catchingPromiseCreated, true, "f");
|
|
1916
|
+
await __classPrivateFieldGet2(this, _BetaMessageStream_endPromise, "f");
|
|
1957
1917
|
}
|
|
1958
1918
|
get currentMessage() {
|
|
1959
|
-
return __classPrivateFieldGet2(this,
|
|
1919
|
+
return __classPrivateFieldGet2(this, _BetaMessageStream_currentMessageSnapshot, "f");
|
|
1960
1920
|
}
|
|
1961
1921
|
async finalMessage() {
|
|
1962
1922
|
await this.done();
|
|
1963
|
-
return __classPrivateFieldGet2(this,
|
|
1923
|
+
return __classPrivateFieldGet2(this, _BetaMessageStream_instances, "m", _BetaMessageStream_getFinalMessage).call(this);
|
|
1964
1924
|
}
|
|
1965
1925
|
async finalText() {
|
|
1966
1926
|
await this.done();
|
|
1967
|
-
return __classPrivateFieldGet2(this,
|
|
1927
|
+
return __classPrivateFieldGet2(this, _BetaMessageStream_instances, "m", _BetaMessageStream_getFinalText).call(this);
|
|
1968
1928
|
}
|
|
1969
1929
|
_emit(event, ...args) {
|
|
1970
|
-
if (__classPrivateFieldGet2(this,
|
|
1930
|
+
if (__classPrivateFieldGet2(this, _BetaMessageStream_ended, "f"))
|
|
1971
1931
|
return;
|
|
1972
1932
|
if (event === "end") {
|
|
1973
|
-
__classPrivateFieldSet2(this,
|
|
1974
|
-
__classPrivateFieldGet2(this,
|
|
1933
|
+
__classPrivateFieldSet2(this, _BetaMessageStream_ended, true, "f");
|
|
1934
|
+
__classPrivateFieldGet2(this, _BetaMessageStream_resolveEndPromise, "f").call(this);
|
|
1975
1935
|
}
|
|
1976
|
-
const listeners = __classPrivateFieldGet2(this,
|
|
1936
|
+
const listeners = __classPrivateFieldGet2(this, _BetaMessageStream_listeners, "f")[event];
|
|
1977
1937
|
if (listeners) {
|
|
1978
|
-
__classPrivateFieldGet2(this,
|
|
1938
|
+
__classPrivateFieldGet2(this, _BetaMessageStream_listeners, "f")[event] = listeners.filter((l) => !l.once);
|
|
1979
1939
|
listeners.forEach(({ listener }) => listener(...args));
|
|
1980
1940
|
}
|
|
1981
1941
|
if (event === "abort") {
|
|
1982
1942
|
const error = args[0];
|
|
1983
|
-
if (!__classPrivateFieldGet2(this,
|
|
1943
|
+
if (!__classPrivateFieldGet2(this, _BetaMessageStream_catchingPromiseCreated, "f") && !listeners?.length) {
|
|
1984
1944
|
Promise.reject(error);
|
|
1985
1945
|
}
|
|
1986
|
-
__classPrivateFieldGet2(this,
|
|
1987
|
-
__classPrivateFieldGet2(this,
|
|
1946
|
+
__classPrivateFieldGet2(this, _BetaMessageStream_rejectConnectedPromise, "f").call(this, error);
|
|
1947
|
+
__classPrivateFieldGet2(this, _BetaMessageStream_rejectEndPromise, "f").call(this, error);
|
|
1988
1948
|
this._emit("end");
|
|
1989
1949
|
return;
|
|
1990
1950
|
}
|
|
1991
1951
|
if (event === "error") {
|
|
1992
1952
|
const error = args[0];
|
|
1993
|
-
if (!__classPrivateFieldGet2(this,
|
|
1953
|
+
if (!__classPrivateFieldGet2(this, _BetaMessageStream_catchingPromiseCreated, "f") && !listeners?.length) {
|
|
1994
1954
|
Promise.reject(error);
|
|
1995
1955
|
}
|
|
1996
|
-
__classPrivateFieldGet2(this,
|
|
1997
|
-
__classPrivateFieldGet2(this,
|
|
1956
|
+
__classPrivateFieldGet2(this, _BetaMessageStream_rejectConnectedPromise, "f").call(this, error);
|
|
1957
|
+
__classPrivateFieldGet2(this, _BetaMessageStream_rejectEndPromise, "f").call(this, error);
|
|
1998
1958
|
this._emit("end");
|
|
1999
1959
|
}
|
|
2000
1960
|
}
|
|
2001
1961
|
_emitFinal() {
|
|
2002
1962
|
const finalMessage = this.receivedMessages.at(-1);
|
|
2003
1963
|
if (finalMessage) {
|
|
2004
|
-
this._emit("finalMessage", __classPrivateFieldGet2(this,
|
|
1964
|
+
this._emit("finalMessage", __classPrivateFieldGet2(this, _BetaMessageStream_instances, "m", _BetaMessageStream_getFinalMessage).call(this));
|
|
2005
1965
|
}
|
|
2006
1966
|
}
|
|
2007
1967
|
async _fromReadableStream(readableStream, options) {
|
|
@@ -2011,23 +1971,23 @@ class MessageStream {
|
|
|
2011
1971
|
this.controller.abort();
|
|
2012
1972
|
signal.addEventListener("abort", () => this.controller.abort());
|
|
2013
1973
|
}
|
|
2014
|
-
__classPrivateFieldGet2(this,
|
|
2015
|
-
this._connected();
|
|
1974
|
+
__classPrivateFieldGet2(this, _BetaMessageStream_instances, "m", _BetaMessageStream_beginRequest).call(this);
|
|
1975
|
+
this._connected(null);
|
|
2016
1976
|
const stream = Stream.fromReadableStream(readableStream, this.controller);
|
|
2017
1977
|
for await (const event of stream) {
|
|
2018
|
-
__classPrivateFieldGet2(this,
|
|
1978
|
+
__classPrivateFieldGet2(this, _BetaMessageStream_instances, "m", _BetaMessageStream_addStreamEvent).call(this, event);
|
|
2019
1979
|
}
|
|
2020
1980
|
if (stream.controller.signal?.aborted) {
|
|
2021
1981
|
throw new APIUserAbortError;
|
|
2022
1982
|
}
|
|
2023
|
-
__classPrivateFieldGet2(this,
|
|
1983
|
+
__classPrivateFieldGet2(this, _BetaMessageStream_instances, "m", _BetaMessageStream_endRequest).call(this);
|
|
2024
1984
|
}
|
|
2025
|
-
[(
|
|
1985
|
+
[(_BetaMessageStream_currentMessageSnapshot = new WeakMap, _BetaMessageStream_connectedPromise = new WeakMap, _BetaMessageStream_resolveConnectedPromise = new WeakMap, _BetaMessageStream_rejectConnectedPromise = new WeakMap, _BetaMessageStream_endPromise = new WeakMap, _BetaMessageStream_resolveEndPromise = new WeakMap, _BetaMessageStream_rejectEndPromise = new WeakMap, _BetaMessageStream_listeners = new WeakMap, _BetaMessageStream_ended = new WeakMap, _BetaMessageStream_errored = new WeakMap, _BetaMessageStream_aborted = new WeakMap, _BetaMessageStream_catchingPromiseCreated = new WeakMap, _BetaMessageStream_response = new WeakMap, _BetaMessageStream_request_id = new WeakMap, _BetaMessageStream_handleError = new WeakMap, _BetaMessageStream_instances = new WeakSet, _BetaMessageStream_getFinalMessage = function _BetaMessageStream_getFinalMessage() {
|
|
2026
1986
|
if (this.receivedMessages.length === 0) {
|
|
2027
1987
|
throw new AnthropicError("stream ended without producing a Message with role=assistant");
|
|
2028
1988
|
}
|
|
2029
1989
|
return this.receivedMessages.at(-1);
|
|
2030
|
-
},
|
|
1990
|
+
}, _BetaMessageStream_getFinalText = function _BetaMessageStream_getFinalText() {
|
|
2031
1991
|
if (this.receivedMessages.length === 0) {
|
|
2032
1992
|
throw new AnthropicError("stream ended without producing a Message with role=assistant");
|
|
2033
1993
|
}
|
|
@@ -2036,14 +1996,14 @@ class MessageStream {
|
|
|
2036
1996
|
throw new AnthropicError("stream ended without producing a content block with type=text");
|
|
2037
1997
|
}
|
|
2038
1998
|
return textBlocks.join(" ");
|
|
2039
|
-
},
|
|
1999
|
+
}, _BetaMessageStream_beginRequest = function _BetaMessageStream_beginRequest() {
|
|
2040
2000
|
if (this.ended)
|
|
2041
2001
|
return;
|
|
2042
|
-
__classPrivateFieldSet2(this,
|
|
2043
|
-
},
|
|
2002
|
+
__classPrivateFieldSet2(this, _BetaMessageStream_currentMessageSnapshot, undefined, "f");
|
|
2003
|
+
}, _BetaMessageStream_addStreamEvent = function _BetaMessageStream_addStreamEvent(event) {
|
|
2044
2004
|
if (this.ended)
|
|
2045
2005
|
return;
|
|
2046
|
-
const messageSnapshot = __classPrivateFieldGet2(this,
|
|
2006
|
+
const messageSnapshot = __classPrivateFieldGet2(this, _BetaMessageStream_instances, "m", _BetaMessageStream_accumulateMessage).call(this, event);
|
|
2047
2007
|
this._emit("streamEvent", event, messageSnapshot);
|
|
2048
2008
|
switch (event.type) {
|
|
2049
2009
|
case "content_block_delta": {
|
|
@@ -2067,25 +2027,25 @@ class MessageStream {
|
|
|
2067
2027
|
break;
|
|
2068
2028
|
}
|
|
2069
2029
|
case "message_start": {
|
|
2070
|
-
__classPrivateFieldSet2(this,
|
|
2030
|
+
__classPrivateFieldSet2(this, _BetaMessageStream_currentMessageSnapshot, messageSnapshot, "f");
|
|
2071
2031
|
break;
|
|
2072
2032
|
}
|
|
2073
2033
|
case "content_block_start":
|
|
2074
2034
|
case "message_delta":
|
|
2075
2035
|
break;
|
|
2076
2036
|
}
|
|
2077
|
-
},
|
|
2037
|
+
}, _BetaMessageStream_endRequest = function _BetaMessageStream_endRequest() {
|
|
2078
2038
|
if (this.ended) {
|
|
2079
2039
|
throw new AnthropicError(`stream has ended, this shouldn't happen`);
|
|
2080
2040
|
}
|
|
2081
|
-
const snapshot = __classPrivateFieldGet2(this,
|
|
2041
|
+
const snapshot = __classPrivateFieldGet2(this, _BetaMessageStream_currentMessageSnapshot, "f");
|
|
2082
2042
|
if (!snapshot) {
|
|
2083
2043
|
throw new AnthropicError(`request ended without sending any chunks`);
|
|
2084
2044
|
}
|
|
2085
|
-
__classPrivateFieldSet2(this,
|
|
2045
|
+
__classPrivateFieldSet2(this, _BetaMessageStream_currentMessageSnapshot, undefined, "f");
|
|
2086
2046
|
return snapshot;
|
|
2087
|
-
},
|
|
2088
|
-
let snapshot = __classPrivateFieldGet2(this,
|
|
2047
|
+
}, _BetaMessageStream_accumulateMessage = function _BetaMessageStream_accumulateMessage(event) {
|
|
2048
|
+
let snapshot = __classPrivateFieldGet2(this, _BetaMessageStream_currentMessageSnapshot, "f");
|
|
2089
2049
|
if (event.type === "message_start") {
|
|
2090
2050
|
if (snapshot) {
|
|
2091
2051
|
throw new AnthropicError(`Unexpected event order, got ${event.type} before receiving "message_stop"`);
|
|
@@ -2183,42 +2143,598 @@ class MessageStream {
|
|
|
2183
2143
|
}
|
|
2184
2144
|
}
|
|
2185
2145
|
|
|
2186
|
-
// ../../node_modules/@anthropic-ai/sdk/resources/messages/messages.mjs
|
|
2187
|
-
|
|
2146
|
+
// ../../node_modules/@anthropic-ai/sdk/resources/beta/messages/messages.mjs
|
|
2147
|
+
var DEPRECATED_MODELS = {
|
|
2148
|
+
"claude-1.3": "November 6th, 2024",
|
|
2149
|
+
"claude-1.3-100k": "November 6th, 2024",
|
|
2150
|
+
"claude-instant-1.1": "November 6th, 2024",
|
|
2151
|
+
"claude-instant-1.1-100k": "November 6th, 2024",
|
|
2152
|
+
"claude-instant-1.2": "November 6th, 2024",
|
|
2153
|
+
"claude-3-sonnet-20240229": "July 21st, 2025",
|
|
2154
|
+
"claude-2.1": "July 21st, 2025",
|
|
2155
|
+
"claude-2.0": "July 21st, 2025"
|
|
2156
|
+
};
|
|
2157
|
+
|
|
2158
|
+
class Messages extends APIResource {
|
|
2188
2159
|
constructor() {
|
|
2189
2160
|
super(...arguments);
|
|
2190
|
-
this.batches = new
|
|
2161
|
+
this.batches = new Batches(this._client);
|
|
2191
2162
|
}
|
|
2192
|
-
create(
|
|
2163
|
+
create(params, options) {
|
|
2164
|
+
const { betas, ...body } = params;
|
|
2193
2165
|
if (body.model in DEPRECATED_MODELS) {
|
|
2194
2166
|
console.warn(`The model '${body.model}' is deprecated and will reach end-of-life on ${DEPRECATED_MODELS[body.model]}
|
|
2195
2167
|
Please migrate to a newer model. Visit https://docs.anthropic.com/en/docs/resources/model-deprecations for more information.`);
|
|
2196
2168
|
}
|
|
2197
|
-
return this._client.post("/v1/messages", {
|
|
2169
|
+
return this._client.post("/v1/messages?beta=true", {
|
|
2198
2170
|
body,
|
|
2199
2171
|
timeout: this._client._options.timeout ?? 600000,
|
|
2200
2172
|
...options,
|
|
2201
|
-
|
|
2173
|
+
headers: {
|
|
2174
|
+
...betas?.toString() != null ? { "anthropic-beta": betas?.toString() } : undefined,
|
|
2175
|
+
...options?.headers
|
|
2176
|
+
},
|
|
2177
|
+
stream: params.stream ?? false
|
|
2202
2178
|
});
|
|
2203
2179
|
}
|
|
2204
2180
|
stream(body, options) {
|
|
2205
|
-
return
|
|
2181
|
+
return BetaMessageStream.createMessage(this, body, options);
|
|
2206
2182
|
}
|
|
2207
|
-
countTokens(
|
|
2208
|
-
|
|
2183
|
+
countTokens(params, options) {
|
|
2184
|
+
const { betas, ...body } = params;
|
|
2185
|
+
return this._client.post("/v1/messages/count_tokens?beta=true", {
|
|
2186
|
+
body,
|
|
2187
|
+
...options,
|
|
2188
|
+
headers: {
|
|
2189
|
+
"anthropic-beta": [...betas ?? [], "token-counting-2024-11-01"].toString(),
|
|
2190
|
+
...options?.headers
|
|
2191
|
+
}
|
|
2192
|
+
});
|
|
2209
2193
|
}
|
|
2210
2194
|
}
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2195
|
+
Messages.Batches = Batches;
|
|
2196
|
+
Messages.BetaMessageBatchesPage = BetaMessageBatchesPage;
|
|
2197
|
+
|
|
2198
|
+
// ../../node_modules/@anthropic-ai/sdk/resources/beta/beta.mjs
|
|
2199
|
+
class Beta extends APIResource {
|
|
2200
|
+
constructor() {
|
|
2201
|
+
super(...arguments);
|
|
2202
|
+
this.models = new Models(this._client);
|
|
2203
|
+
this.messages = new Messages(this._client);
|
|
2204
|
+
}
|
|
2205
|
+
}
|
|
2206
|
+
Beta.Models = Models;
|
|
2207
|
+
Beta.BetaModelInfosPage = BetaModelInfosPage;
|
|
2208
|
+
Beta.Messages = Messages;
|
|
2209
|
+
// ../../node_modules/@anthropic-ai/sdk/resources/completions.mjs
|
|
2210
|
+
class Completions extends APIResource {
|
|
2211
|
+
create(body, options) {
|
|
2212
|
+
return this._client.post("/v1/complete", {
|
|
2213
|
+
body,
|
|
2214
|
+
timeout: this._client._options.timeout ?? 600000,
|
|
2215
|
+
...options,
|
|
2216
|
+
stream: body.stream ?? false
|
|
2217
|
+
});
|
|
2218
|
+
}
|
|
2219
|
+
}
|
|
2220
|
+
// ../../node_modules/@anthropic-ai/sdk/resources/messages/batches.mjs
|
|
2221
|
+
class Batches2 extends APIResource {
|
|
2222
|
+
create(body, options) {
|
|
2223
|
+
return this._client.post("/v1/messages/batches", { body, ...options });
|
|
2224
|
+
}
|
|
2225
|
+
retrieve(messageBatchId, options) {
|
|
2226
|
+
return this._client.get(`/v1/messages/batches/${messageBatchId}`, options);
|
|
2227
|
+
}
|
|
2228
|
+
list(query = {}, options) {
|
|
2229
|
+
if (isRequestOptions(query)) {
|
|
2230
|
+
return this.list({}, query);
|
|
2231
|
+
}
|
|
2232
|
+
return this._client.getAPIList("/v1/messages/batches", MessageBatchesPage, { query, ...options });
|
|
2233
|
+
}
|
|
2234
|
+
delete(messageBatchId, options) {
|
|
2235
|
+
return this._client.delete(`/v1/messages/batches/${messageBatchId}`, options);
|
|
2236
|
+
}
|
|
2237
|
+
cancel(messageBatchId, options) {
|
|
2238
|
+
return this._client.post(`/v1/messages/batches/${messageBatchId}/cancel`, options);
|
|
2239
|
+
}
|
|
2240
|
+
async results(messageBatchId, options) {
|
|
2241
|
+
const batch = await this.retrieve(messageBatchId);
|
|
2242
|
+
if (!batch.results_url) {
|
|
2243
|
+
throw new AnthropicError(`No batch \`results_url\`; Has it finished processing? ${batch.processing_status} - ${batch.id}`);
|
|
2244
|
+
}
|
|
2245
|
+
return this._client.get(batch.results_url, {
|
|
2246
|
+
...options,
|
|
2247
|
+
headers: {
|
|
2248
|
+
Accept: "application/binary",
|
|
2249
|
+
...options?.headers
|
|
2250
|
+
},
|
|
2251
|
+
__binaryResponse: true
|
|
2252
|
+
})._thenUnwrap((_, props) => JSONLDecoder.fromResponse(props.response, props.controller));
|
|
2253
|
+
}
|
|
2254
|
+
}
|
|
2255
|
+
|
|
2256
|
+
class MessageBatchesPage extends Page {
|
|
2257
|
+
}
|
|
2258
|
+
Batches2.MessageBatchesPage = MessageBatchesPage;
|
|
2259
|
+
|
|
2260
|
+
// ../../node_modules/@anthropic-ai/sdk/lib/MessageStream.mjs
|
|
2261
|
+
var __classPrivateFieldSet3 = function(receiver, state, value, kind2, f) {
|
|
2262
|
+
if (kind2 === "m")
|
|
2263
|
+
throw new TypeError("Private method is not writable");
|
|
2264
|
+
if (kind2 === "a" && !f)
|
|
2265
|
+
throw new TypeError("Private accessor was defined without a setter");
|
|
2266
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
|
|
2267
|
+
throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
2268
|
+
return kind2 === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value), value;
|
|
2269
|
+
};
|
|
2270
|
+
var __classPrivateFieldGet3 = function(receiver, state, kind2, f) {
|
|
2271
|
+
if (kind2 === "a" && !f)
|
|
2272
|
+
throw new TypeError("Private accessor was defined without a getter");
|
|
2273
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
|
|
2274
|
+
throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
2275
|
+
return kind2 === "m" ? f : kind2 === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
2276
|
+
};
|
|
2277
|
+
var _MessageStream_instances;
|
|
2278
|
+
var _MessageStream_currentMessageSnapshot;
|
|
2279
|
+
var _MessageStream_connectedPromise;
|
|
2280
|
+
var _MessageStream_resolveConnectedPromise;
|
|
2281
|
+
var _MessageStream_rejectConnectedPromise;
|
|
2282
|
+
var _MessageStream_endPromise;
|
|
2283
|
+
var _MessageStream_resolveEndPromise;
|
|
2284
|
+
var _MessageStream_rejectEndPromise;
|
|
2285
|
+
var _MessageStream_listeners;
|
|
2286
|
+
var _MessageStream_ended;
|
|
2287
|
+
var _MessageStream_errored;
|
|
2288
|
+
var _MessageStream_aborted;
|
|
2289
|
+
var _MessageStream_catchingPromiseCreated;
|
|
2290
|
+
var _MessageStream_response;
|
|
2291
|
+
var _MessageStream_request_id;
|
|
2292
|
+
var _MessageStream_getFinalMessage;
|
|
2293
|
+
var _MessageStream_getFinalText;
|
|
2294
|
+
var _MessageStream_handleError;
|
|
2295
|
+
var _MessageStream_beginRequest;
|
|
2296
|
+
var _MessageStream_addStreamEvent;
|
|
2297
|
+
var _MessageStream_endRequest;
|
|
2298
|
+
var _MessageStream_accumulateMessage;
|
|
2299
|
+
var JSON_BUF_PROPERTY2 = "__json_buf";
|
|
2300
|
+
|
|
2301
|
+
class MessageStream {
|
|
2302
|
+
constructor() {
|
|
2303
|
+
_MessageStream_instances.add(this);
|
|
2304
|
+
this.messages = [];
|
|
2305
|
+
this.receivedMessages = [];
|
|
2306
|
+
_MessageStream_currentMessageSnapshot.set(this, undefined);
|
|
2307
|
+
this.controller = new AbortController;
|
|
2308
|
+
_MessageStream_connectedPromise.set(this, undefined);
|
|
2309
|
+
_MessageStream_resolveConnectedPromise.set(this, () => {
|
|
2310
|
+
});
|
|
2311
|
+
_MessageStream_rejectConnectedPromise.set(this, () => {
|
|
2312
|
+
});
|
|
2313
|
+
_MessageStream_endPromise.set(this, undefined);
|
|
2314
|
+
_MessageStream_resolveEndPromise.set(this, () => {
|
|
2315
|
+
});
|
|
2316
|
+
_MessageStream_rejectEndPromise.set(this, () => {
|
|
2317
|
+
});
|
|
2318
|
+
_MessageStream_listeners.set(this, {});
|
|
2319
|
+
_MessageStream_ended.set(this, false);
|
|
2320
|
+
_MessageStream_errored.set(this, false);
|
|
2321
|
+
_MessageStream_aborted.set(this, false);
|
|
2322
|
+
_MessageStream_catchingPromiseCreated.set(this, false);
|
|
2323
|
+
_MessageStream_response.set(this, undefined);
|
|
2324
|
+
_MessageStream_request_id.set(this, undefined);
|
|
2325
|
+
_MessageStream_handleError.set(this, (error) => {
|
|
2326
|
+
__classPrivateFieldSet3(this, _MessageStream_errored, true, "f");
|
|
2327
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
2328
|
+
error = new APIUserAbortError;
|
|
2329
|
+
}
|
|
2330
|
+
if (error instanceof APIUserAbortError) {
|
|
2331
|
+
__classPrivateFieldSet3(this, _MessageStream_aborted, true, "f");
|
|
2332
|
+
return this._emit("abort", error);
|
|
2333
|
+
}
|
|
2334
|
+
if (error instanceof AnthropicError) {
|
|
2335
|
+
return this._emit("error", error);
|
|
2336
|
+
}
|
|
2337
|
+
if (error instanceof Error) {
|
|
2338
|
+
const anthropicError = new AnthropicError(error.message);
|
|
2339
|
+
anthropicError.cause = error;
|
|
2340
|
+
return this._emit("error", anthropicError);
|
|
2341
|
+
}
|
|
2342
|
+
return this._emit("error", new AnthropicError(String(error)));
|
|
2343
|
+
});
|
|
2344
|
+
__classPrivateFieldSet3(this, _MessageStream_connectedPromise, new Promise((resolve, reject) => {
|
|
2345
|
+
__classPrivateFieldSet3(this, _MessageStream_resolveConnectedPromise, resolve, "f");
|
|
2346
|
+
__classPrivateFieldSet3(this, _MessageStream_rejectConnectedPromise, reject, "f");
|
|
2347
|
+
}), "f");
|
|
2348
|
+
__classPrivateFieldSet3(this, _MessageStream_endPromise, new Promise((resolve, reject) => {
|
|
2349
|
+
__classPrivateFieldSet3(this, _MessageStream_resolveEndPromise, resolve, "f");
|
|
2350
|
+
__classPrivateFieldSet3(this, _MessageStream_rejectEndPromise, reject, "f");
|
|
2351
|
+
}), "f");
|
|
2352
|
+
__classPrivateFieldGet3(this, _MessageStream_connectedPromise, "f").catch(() => {
|
|
2353
|
+
});
|
|
2354
|
+
__classPrivateFieldGet3(this, _MessageStream_endPromise, "f").catch(() => {
|
|
2355
|
+
});
|
|
2356
|
+
}
|
|
2357
|
+
get response() {
|
|
2358
|
+
return __classPrivateFieldGet3(this, _MessageStream_response, "f");
|
|
2359
|
+
}
|
|
2360
|
+
get request_id() {
|
|
2361
|
+
return __classPrivateFieldGet3(this, _MessageStream_request_id, "f");
|
|
2362
|
+
}
|
|
2363
|
+
async withResponse() {
|
|
2364
|
+
const response = await __classPrivateFieldGet3(this, _MessageStream_connectedPromise, "f");
|
|
2365
|
+
if (!response) {
|
|
2366
|
+
throw new Error("Could not resolve a `Response` object");
|
|
2367
|
+
}
|
|
2368
|
+
return {
|
|
2369
|
+
data: this,
|
|
2370
|
+
response,
|
|
2371
|
+
request_id: response.headers.get("request-id")
|
|
2372
|
+
};
|
|
2373
|
+
}
|
|
2374
|
+
static fromReadableStream(stream) {
|
|
2375
|
+
const runner = new MessageStream;
|
|
2376
|
+
runner._run(() => runner._fromReadableStream(stream));
|
|
2377
|
+
return runner;
|
|
2378
|
+
}
|
|
2379
|
+
static createMessage(messages, params, options) {
|
|
2380
|
+
const runner = new MessageStream;
|
|
2381
|
+
for (const message of params.messages) {
|
|
2382
|
+
runner._addMessageParam(message);
|
|
2383
|
+
}
|
|
2384
|
+
runner._run(() => runner._createMessage(messages, { ...params, stream: true }, { ...options, headers: { ...options?.headers, "X-Stainless-Helper-Method": "stream" } }));
|
|
2385
|
+
return runner;
|
|
2386
|
+
}
|
|
2387
|
+
_run(executor) {
|
|
2388
|
+
executor().then(() => {
|
|
2389
|
+
this._emitFinal();
|
|
2390
|
+
this._emit("end");
|
|
2391
|
+
}, __classPrivateFieldGet3(this, _MessageStream_handleError, "f"));
|
|
2392
|
+
}
|
|
2393
|
+
_addMessageParam(message) {
|
|
2394
|
+
this.messages.push(message);
|
|
2395
|
+
}
|
|
2396
|
+
_addMessage(message, emit = true) {
|
|
2397
|
+
this.receivedMessages.push(message);
|
|
2398
|
+
if (emit) {
|
|
2399
|
+
this._emit("message", message);
|
|
2400
|
+
}
|
|
2401
|
+
}
|
|
2402
|
+
async _createMessage(messages, params, options) {
|
|
2403
|
+
const signal = options?.signal;
|
|
2404
|
+
if (signal) {
|
|
2405
|
+
if (signal.aborted)
|
|
2406
|
+
this.controller.abort();
|
|
2407
|
+
signal.addEventListener("abort", () => this.controller.abort());
|
|
2408
|
+
}
|
|
2409
|
+
__classPrivateFieldGet3(this, _MessageStream_instances, "m", _MessageStream_beginRequest).call(this);
|
|
2410
|
+
const { response, data: stream } = await messages.create({ ...params, stream: true }, { ...options, signal: this.controller.signal }).withResponse();
|
|
2411
|
+
this._connected(response);
|
|
2412
|
+
for await (const event of stream) {
|
|
2413
|
+
__classPrivateFieldGet3(this, _MessageStream_instances, "m", _MessageStream_addStreamEvent).call(this, event);
|
|
2414
|
+
}
|
|
2415
|
+
if (stream.controller.signal?.aborted) {
|
|
2416
|
+
throw new APIUserAbortError;
|
|
2417
|
+
}
|
|
2418
|
+
__classPrivateFieldGet3(this, _MessageStream_instances, "m", _MessageStream_endRequest).call(this);
|
|
2419
|
+
}
|
|
2420
|
+
_connected(response) {
|
|
2421
|
+
if (this.ended)
|
|
2422
|
+
return;
|
|
2423
|
+
__classPrivateFieldSet3(this, _MessageStream_response, response, "f");
|
|
2424
|
+
__classPrivateFieldSet3(this, _MessageStream_request_id, response?.headers.get("request-id"), "f");
|
|
2425
|
+
__classPrivateFieldGet3(this, _MessageStream_resolveConnectedPromise, "f").call(this, response);
|
|
2426
|
+
this._emit("connect");
|
|
2427
|
+
}
|
|
2428
|
+
get ended() {
|
|
2429
|
+
return __classPrivateFieldGet3(this, _MessageStream_ended, "f");
|
|
2430
|
+
}
|
|
2431
|
+
get errored() {
|
|
2432
|
+
return __classPrivateFieldGet3(this, _MessageStream_errored, "f");
|
|
2433
|
+
}
|
|
2434
|
+
get aborted() {
|
|
2435
|
+
return __classPrivateFieldGet3(this, _MessageStream_aborted, "f");
|
|
2436
|
+
}
|
|
2437
|
+
abort() {
|
|
2438
|
+
this.controller.abort();
|
|
2439
|
+
}
|
|
2440
|
+
on(event, listener) {
|
|
2441
|
+
const listeners = __classPrivateFieldGet3(this, _MessageStream_listeners, "f")[event] || (__classPrivateFieldGet3(this, _MessageStream_listeners, "f")[event] = []);
|
|
2442
|
+
listeners.push({ listener });
|
|
2443
|
+
return this;
|
|
2444
|
+
}
|
|
2445
|
+
off(event, listener) {
|
|
2446
|
+
const listeners = __classPrivateFieldGet3(this, _MessageStream_listeners, "f")[event];
|
|
2447
|
+
if (!listeners)
|
|
2448
|
+
return this;
|
|
2449
|
+
const index = listeners.findIndex((l) => l.listener === listener);
|
|
2450
|
+
if (index >= 0)
|
|
2451
|
+
listeners.splice(index, 1);
|
|
2452
|
+
return this;
|
|
2453
|
+
}
|
|
2454
|
+
once(event, listener) {
|
|
2455
|
+
const listeners = __classPrivateFieldGet3(this, _MessageStream_listeners, "f")[event] || (__classPrivateFieldGet3(this, _MessageStream_listeners, "f")[event] = []);
|
|
2456
|
+
listeners.push({ listener, once: true });
|
|
2457
|
+
return this;
|
|
2458
|
+
}
|
|
2459
|
+
emitted(event) {
|
|
2460
|
+
return new Promise((resolve, reject) => {
|
|
2461
|
+
__classPrivateFieldSet3(this, _MessageStream_catchingPromiseCreated, true, "f");
|
|
2462
|
+
if (event !== "error")
|
|
2463
|
+
this.once("error", reject);
|
|
2464
|
+
this.once(event, resolve);
|
|
2465
|
+
});
|
|
2466
|
+
}
|
|
2467
|
+
async done() {
|
|
2468
|
+
__classPrivateFieldSet3(this, _MessageStream_catchingPromiseCreated, true, "f");
|
|
2469
|
+
await __classPrivateFieldGet3(this, _MessageStream_endPromise, "f");
|
|
2470
|
+
}
|
|
2471
|
+
get currentMessage() {
|
|
2472
|
+
return __classPrivateFieldGet3(this, _MessageStream_currentMessageSnapshot, "f");
|
|
2473
|
+
}
|
|
2474
|
+
async finalMessage() {
|
|
2475
|
+
await this.done();
|
|
2476
|
+
return __classPrivateFieldGet3(this, _MessageStream_instances, "m", _MessageStream_getFinalMessage).call(this);
|
|
2477
|
+
}
|
|
2478
|
+
async finalText() {
|
|
2479
|
+
await this.done();
|
|
2480
|
+
return __classPrivateFieldGet3(this, _MessageStream_instances, "m", _MessageStream_getFinalText).call(this);
|
|
2481
|
+
}
|
|
2482
|
+
_emit(event, ...args) {
|
|
2483
|
+
if (__classPrivateFieldGet3(this, _MessageStream_ended, "f"))
|
|
2484
|
+
return;
|
|
2485
|
+
if (event === "end") {
|
|
2486
|
+
__classPrivateFieldSet3(this, _MessageStream_ended, true, "f");
|
|
2487
|
+
__classPrivateFieldGet3(this, _MessageStream_resolveEndPromise, "f").call(this);
|
|
2488
|
+
}
|
|
2489
|
+
const listeners = __classPrivateFieldGet3(this, _MessageStream_listeners, "f")[event];
|
|
2490
|
+
if (listeners) {
|
|
2491
|
+
__classPrivateFieldGet3(this, _MessageStream_listeners, "f")[event] = listeners.filter((l) => !l.once);
|
|
2492
|
+
listeners.forEach(({ listener }) => listener(...args));
|
|
2493
|
+
}
|
|
2494
|
+
if (event === "abort") {
|
|
2495
|
+
const error = args[0];
|
|
2496
|
+
if (!__classPrivateFieldGet3(this, _MessageStream_catchingPromiseCreated, "f") && !listeners?.length) {
|
|
2497
|
+
Promise.reject(error);
|
|
2498
|
+
}
|
|
2499
|
+
__classPrivateFieldGet3(this, _MessageStream_rejectConnectedPromise, "f").call(this, error);
|
|
2500
|
+
__classPrivateFieldGet3(this, _MessageStream_rejectEndPromise, "f").call(this, error);
|
|
2501
|
+
this._emit("end");
|
|
2502
|
+
return;
|
|
2503
|
+
}
|
|
2504
|
+
if (event === "error") {
|
|
2505
|
+
const error = args[0];
|
|
2506
|
+
if (!__classPrivateFieldGet3(this, _MessageStream_catchingPromiseCreated, "f") && !listeners?.length) {
|
|
2507
|
+
Promise.reject(error);
|
|
2508
|
+
}
|
|
2509
|
+
__classPrivateFieldGet3(this, _MessageStream_rejectConnectedPromise, "f").call(this, error);
|
|
2510
|
+
__classPrivateFieldGet3(this, _MessageStream_rejectEndPromise, "f").call(this, error);
|
|
2511
|
+
this._emit("end");
|
|
2512
|
+
}
|
|
2513
|
+
}
|
|
2514
|
+
_emitFinal() {
|
|
2515
|
+
const finalMessage = this.receivedMessages.at(-1);
|
|
2516
|
+
if (finalMessage) {
|
|
2517
|
+
this._emit("finalMessage", __classPrivateFieldGet3(this, _MessageStream_instances, "m", _MessageStream_getFinalMessage).call(this));
|
|
2518
|
+
}
|
|
2519
|
+
}
|
|
2520
|
+
async _fromReadableStream(readableStream, options) {
|
|
2521
|
+
const signal = options?.signal;
|
|
2522
|
+
if (signal) {
|
|
2523
|
+
if (signal.aborted)
|
|
2524
|
+
this.controller.abort();
|
|
2525
|
+
signal.addEventListener("abort", () => this.controller.abort());
|
|
2526
|
+
}
|
|
2527
|
+
__classPrivateFieldGet3(this, _MessageStream_instances, "m", _MessageStream_beginRequest).call(this);
|
|
2528
|
+
this._connected(null);
|
|
2529
|
+
const stream = Stream.fromReadableStream(readableStream, this.controller);
|
|
2530
|
+
for await (const event of stream) {
|
|
2531
|
+
__classPrivateFieldGet3(this, _MessageStream_instances, "m", _MessageStream_addStreamEvent).call(this, event);
|
|
2532
|
+
}
|
|
2533
|
+
if (stream.controller.signal?.aborted) {
|
|
2534
|
+
throw new APIUserAbortError;
|
|
2535
|
+
}
|
|
2536
|
+
__classPrivateFieldGet3(this, _MessageStream_instances, "m", _MessageStream_endRequest).call(this);
|
|
2537
|
+
}
|
|
2538
|
+
[(_MessageStream_currentMessageSnapshot = new WeakMap, _MessageStream_connectedPromise = new WeakMap, _MessageStream_resolveConnectedPromise = new WeakMap, _MessageStream_rejectConnectedPromise = new WeakMap, _MessageStream_endPromise = new WeakMap, _MessageStream_resolveEndPromise = new WeakMap, _MessageStream_rejectEndPromise = new WeakMap, _MessageStream_listeners = new WeakMap, _MessageStream_ended = new WeakMap, _MessageStream_errored = new WeakMap, _MessageStream_aborted = new WeakMap, _MessageStream_catchingPromiseCreated = new WeakMap, _MessageStream_response = new WeakMap, _MessageStream_request_id = new WeakMap, _MessageStream_handleError = new WeakMap, _MessageStream_instances = new WeakSet, _MessageStream_getFinalMessage = function _MessageStream_getFinalMessage() {
|
|
2539
|
+
if (this.receivedMessages.length === 0) {
|
|
2540
|
+
throw new AnthropicError("stream ended without producing a Message with role=assistant");
|
|
2541
|
+
}
|
|
2542
|
+
return this.receivedMessages.at(-1);
|
|
2543
|
+
}, _MessageStream_getFinalText = function _MessageStream_getFinalText() {
|
|
2544
|
+
if (this.receivedMessages.length === 0) {
|
|
2545
|
+
throw new AnthropicError("stream ended without producing a Message with role=assistant");
|
|
2546
|
+
}
|
|
2547
|
+
const textBlocks = this.receivedMessages.at(-1).content.filter((block) => block.type === "text").map((block) => block.text);
|
|
2548
|
+
if (textBlocks.length === 0) {
|
|
2549
|
+
throw new AnthropicError("stream ended without producing a content block with type=text");
|
|
2550
|
+
}
|
|
2551
|
+
return textBlocks.join(" ");
|
|
2552
|
+
}, _MessageStream_beginRequest = function _MessageStream_beginRequest() {
|
|
2553
|
+
if (this.ended)
|
|
2554
|
+
return;
|
|
2555
|
+
__classPrivateFieldSet3(this, _MessageStream_currentMessageSnapshot, undefined, "f");
|
|
2556
|
+
}, _MessageStream_addStreamEvent = function _MessageStream_addStreamEvent(event) {
|
|
2557
|
+
if (this.ended)
|
|
2558
|
+
return;
|
|
2559
|
+
const messageSnapshot = __classPrivateFieldGet3(this, _MessageStream_instances, "m", _MessageStream_accumulateMessage).call(this, event);
|
|
2560
|
+
this._emit("streamEvent", event, messageSnapshot);
|
|
2561
|
+
switch (event.type) {
|
|
2562
|
+
case "content_block_delta": {
|
|
2563
|
+
const content = messageSnapshot.content.at(-1);
|
|
2564
|
+
if (event.delta.type === "text_delta" && content.type === "text") {
|
|
2565
|
+
this._emit("text", event.delta.text, content.text || "");
|
|
2566
|
+
} else if (event.delta.type === "input_json_delta" && content.type === "tool_use") {
|
|
2567
|
+
if (content.input) {
|
|
2568
|
+
this._emit("inputJson", event.delta.partial_json, content.input);
|
|
2569
|
+
}
|
|
2570
|
+
}
|
|
2571
|
+
break;
|
|
2572
|
+
}
|
|
2573
|
+
case "message_stop": {
|
|
2574
|
+
this._addMessageParam(messageSnapshot);
|
|
2575
|
+
this._addMessage(messageSnapshot, true);
|
|
2576
|
+
break;
|
|
2577
|
+
}
|
|
2578
|
+
case "content_block_stop": {
|
|
2579
|
+
this._emit("contentBlock", messageSnapshot.content.at(-1));
|
|
2580
|
+
break;
|
|
2581
|
+
}
|
|
2582
|
+
case "message_start": {
|
|
2583
|
+
__classPrivateFieldSet3(this, _MessageStream_currentMessageSnapshot, messageSnapshot, "f");
|
|
2584
|
+
break;
|
|
2585
|
+
}
|
|
2586
|
+
case "content_block_start":
|
|
2587
|
+
case "message_delta":
|
|
2588
|
+
break;
|
|
2589
|
+
}
|
|
2590
|
+
}, _MessageStream_endRequest = function _MessageStream_endRequest() {
|
|
2591
|
+
if (this.ended) {
|
|
2592
|
+
throw new AnthropicError(`stream has ended, this shouldn't happen`);
|
|
2593
|
+
}
|
|
2594
|
+
const snapshot = __classPrivateFieldGet3(this, _MessageStream_currentMessageSnapshot, "f");
|
|
2595
|
+
if (!snapshot) {
|
|
2596
|
+
throw new AnthropicError(`request ended without sending any chunks`);
|
|
2597
|
+
}
|
|
2598
|
+
__classPrivateFieldSet3(this, _MessageStream_currentMessageSnapshot, undefined, "f");
|
|
2599
|
+
return snapshot;
|
|
2600
|
+
}, _MessageStream_accumulateMessage = function _MessageStream_accumulateMessage(event) {
|
|
2601
|
+
let snapshot = __classPrivateFieldGet3(this, _MessageStream_currentMessageSnapshot, "f");
|
|
2602
|
+
if (event.type === "message_start") {
|
|
2603
|
+
if (snapshot) {
|
|
2604
|
+
throw new AnthropicError(`Unexpected event order, got ${event.type} before receiving "message_stop"`);
|
|
2605
|
+
}
|
|
2606
|
+
return event.message;
|
|
2607
|
+
}
|
|
2608
|
+
if (!snapshot) {
|
|
2609
|
+
throw new AnthropicError(`Unexpected event order, got ${event.type} before "message_start"`);
|
|
2610
|
+
}
|
|
2611
|
+
switch (event.type) {
|
|
2612
|
+
case "message_stop":
|
|
2613
|
+
return snapshot;
|
|
2614
|
+
case "message_delta":
|
|
2615
|
+
snapshot.stop_reason = event.delta.stop_reason;
|
|
2616
|
+
snapshot.stop_sequence = event.delta.stop_sequence;
|
|
2617
|
+
snapshot.usage.output_tokens = event.usage.output_tokens;
|
|
2618
|
+
return snapshot;
|
|
2619
|
+
case "content_block_start":
|
|
2620
|
+
snapshot.content.push(event.content_block);
|
|
2621
|
+
return snapshot;
|
|
2622
|
+
case "content_block_delta": {
|
|
2623
|
+
const snapshotContent = snapshot.content.at(event.index);
|
|
2624
|
+
if (snapshotContent?.type === "text" && event.delta.type === "text_delta") {
|
|
2625
|
+
snapshotContent.text += event.delta.text;
|
|
2626
|
+
} else if (snapshotContent?.type === "tool_use" && event.delta.type === "input_json_delta") {
|
|
2627
|
+
let jsonBuf = snapshotContent[JSON_BUF_PROPERTY2] || "";
|
|
2628
|
+
jsonBuf += event.delta.partial_json;
|
|
2629
|
+
Object.defineProperty(snapshotContent, JSON_BUF_PROPERTY2, {
|
|
2630
|
+
value: jsonBuf,
|
|
2631
|
+
enumerable: false,
|
|
2632
|
+
writable: true
|
|
2633
|
+
});
|
|
2634
|
+
if (jsonBuf) {
|
|
2635
|
+
snapshotContent.input = partialParse(jsonBuf);
|
|
2636
|
+
}
|
|
2637
|
+
}
|
|
2638
|
+
return snapshot;
|
|
2639
|
+
}
|
|
2640
|
+
case "content_block_stop":
|
|
2641
|
+
return snapshot;
|
|
2642
|
+
}
|
|
2643
|
+
}, Symbol.asyncIterator)]() {
|
|
2644
|
+
const pushQueue = [];
|
|
2645
|
+
const readQueue = [];
|
|
2646
|
+
let done = false;
|
|
2647
|
+
this.on("streamEvent", (event) => {
|
|
2648
|
+
const reader = readQueue.shift();
|
|
2649
|
+
if (reader) {
|
|
2650
|
+
reader.resolve(event);
|
|
2651
|
+
} else {
|
|
2652
|
+
pushQueue.push(event);
|
|
2653
|
+
}
|
|
2654
|
+
});
|
|
2655
|
+
this.on("end", () => {
|
|
2656
|
+
done = true;
|
|
2657
|
+
for (const reader of readQueue) {
|
|
2658
|
+
reader.resolve(undefined);
|
|
2659
|
+
}
|
|
2660
|
+
readQueue.length = 0;
|
|
2661
|
+
});
|
|
2662
|
+
this.on("abort", (err) => {
|
|
2663
|
+
done = true;
|
|
2664
|
+
for (const reader of readQueue) {
|
|
2665
|
+
reader.reject(err);
|
|
2666
|
+
}
|
|
2667
|
+
readQueue.length = 0;
|
|
2668
|
+
});
|
|
2669
|
+
this.on("error", (err) => {
|
|
2670
|
+
done = true;
|
|
2671
|
+
for (const reader of readQueue) {
|
|
2672
|
+
reader.reject(err);
|
|
2673
|
+
}
|
|
2674
|
+
readQueue.length = 0;
|
|
2675
|
+
});
|
|
2676
|
+
return {
|
|
2677
|
+
next: async () => {
|
|
2678
|
+
if (!pushQueue.length) {
|
|
2679
|
+
if (done) {
|
|
2680
|
+
return { value: undefined, done: true };
|
|
2681
|
+
}
|
|
2682
|
+
return new Promise((resolve, reject) => readQueue.push({ resolve, reject })).then((chunk2) => chunk2 ? { value: chunk2, done: false } : { value: undefined, done: true });
|
|
2683
|
+
}
|
|
2684
|
+
const chunk = pushQueue.shift();
|
|
2685
|
+
return { value: chunk, done: false };
|
|
2686
|
+
},
|
|
2687
|
+
return: async () => {
|
|
2688
|
+
this.abort();
|
|
2689
|
+
return { value: undefined, done: true };
|
|
2690
|
+
}
|
|
2691
|
+
};
|
|
2692
|
+
}
|
|
2693
|
+
toReadableStream() {
|
|
2694
|
+
const stream = new Stream(this[Symbol.asyncIterator].bind(this), this.controller);
|
|
2695
|
+
return stream.toReadableStream();
|
|
2696
|
+
}
|
|
2697
|
+
}
|
|
2698
|
+
|
|
2699
|
+
// ../../node_modules/@anthropic-ai/sdk/resources/messages/messages.mjs
|
|
2700
|
+
class Messages2 extends APIResource {
|
|
2701
|
+
constructor() {
|
|
2702
|
+
super(...arguments);
|
|
2703
|
+
this.batches = new Batches2(this._client);
|
|
2704
|
+
}
|
|
2705
|
+
create(body, options) {
|
|
2706
|
+
if (body.model in DEPRECATED_MODELS2) {
|
|
2707
|
+
console.warn(`The model '${body.model}' is deprecated and will reach end-of-life on ${DEPRECATED_MODELS2[body.model]}
|
|
2708
|
+
Please migrate to a newer model. Visit https://docs.anthropic.com/en/docs/resources/model-deprecations for more information.`);
|
|
2709
|
+
}
|
|
2710
|
+
return this._client.post("/v1/messages", {
|
|
2711
|
+
body,
|
|
2712
|
+
timeout: this._client._options.timeout ?? 600000,
|
|
2713
|
+
...options,
|
|
2714
|
+
stream: body.stream ?? false
|
|
2715
|
+
});
|
|
2716
|
+
}
|
|
2717
|
+
stream(body, options) {
|
|
2718
|
+
return MessageStream.createMessage(this, body, options);
|
|
2719
|
+
}
|
|
2720
|
+
countTokens(body, options) {
|
|
2721
|
+
return this._client.post("/v1/messages/count_tokens", { body, ...options });
|
|
2722
|
+
}
|
|
2723
|
+
}
|
|
2724
|
+
var DEPRECATED_MODELS2 = {
|
|
2725
|
+
"claude-1.3": "November 6th, 2024",
|
|
2726
|
+
"claude-1.3-100k": "November 6th, 2024",
|
|
2727
|
+
"claude-instant-1.1": "November 6th, 2024",
|
|
2728
|
+
"claude-instant-1.1-100k": "November 6th, 2024",
|
|
2729
|
+
"claude-instant-1.2": "November 6th, 2024",
|
|
2730
|
+
"claude-3-sonnet-20240229": "July 21st, 2025",
|
|
2731
|
+
"claude-2.1": "July 21st, 2025",
|
|
2732
|
+
"claude-2.0": "July 21st, 2025"
|
|
2733
|
+
};
|
|
2734
|
+
Messages2.Batches = Batches2;
|
|
2735
|
+
Messages2.MessageBatchesPage = MessageBatchesPage;
|
|
2736
|
+
// ../../node_modules/@anthropic-ai/sdk/resources/models.mjs
|
|
2737
|
+
class Models2 extends APIResource {
|
|
2222
2738
|
retrieve(modelId, options) {
|
|
2223
2739
|
return this._client.get(`/v1/models/${modelId}`, options);
|
|
2224
2740
|
}
|
|
@@ -2361,11 +2877,12 @@ class AiServiceBase {
|
|
|
2361
2877
|
totalCost: 0
|
|
2362
2878
|
};
|
|
2363
2879
|
let resp = "";
|
|
2880
|
+
let reasoning = "";
|
|
2364
2881
|
for await (const chunk of stream) {
|
|
2365
2882
|
switch (chunk.type) {
|
|
2366
2883
|
case "usage":
|
|
2367
|
-
usage.inputTokens = chunk.inputTokens;
|
|
2368
|
-
usage.outputTokens = chunk.outputTokens;
|
|
2884
|
+
usage.inputTokens = chunk.inputTokens ?? 0;
|
|
2885
|
+
usage.outputTokens = chunk.outputTokens ?? 0;
|
|
2369
2886
|
usage.cacheWriteTokens = chunk.cacheWriteTokens ?? 0;
|
|
2370
2887
|
usage.cacheReadTokens = chunk.cacheReadTokens ?? 0;
|
|
2371
2888
|
usage.totalCost = chunk.totalCost;
|
|
@@ -2373,10 +2890,13 @@ class AiServiceBase {
|
|
|
2373
2890
|
case "text":
|
|
2374
2891
|
resp += chunk.text;
|
|
2375
2892
|
break;
|
|
2893
|
+
case "reasoning":
|
|
2894
|
+
reasoning += chunk.text;
|
|
2376
2895
|
}
|
|
2377
2896
|
}
|
|
2378
2897
|
return {
|
|
2379
2898
|
response: resp,
|
|
2899
|
+
reasoning,
|
|
2380
2900
|
usage
|
|
2381
2901
|
};
|
|
2382
2902
|
}
|
|
@@ -2446,6 +2966,16 @@ var deepSeekModels = {
|
|
|
2446
2966
|
outputPrice: 0.28,
|
|
2447
2967
|
cacheWritesPrice: 0.14,
|
|
2448
2968
|
cacheReadsPrice: 0.014
|
|
2969
|
+
},
|
|
2970
|
+
"deepseek-reasoner": {
|
|
2971
|
+
maxTokens: 8000,
|
|
2972
|
+
contextWindow: 64000,
|
|
2973
|
+
supportsImages: false,
|
|
2974
|
+
supportsPromptCache: true,
|
|
2975
|
+
inputPrice: 0,
|
|
2976
|
+
outputPrice: 2.19,
|
|
2977
|
+
cacheWritesPrice: 0.55,
|
|
2978
|
+
cacheReadsPrice: 0.14
|
|
2449
2979
|
}
|
|
2450
2980
|
};
|
|
2451
2981
|
|
|
@@ -2461,7 +2991,7 @@ class AnthropicService extends AiServiceBase {
|
|
|
2461
2991
|
apiKey: options.apiKey,
|
|
2462
2992
|
baseURL: options.baseUrl || undefined
|
|
2463
2993
|
});
|
|
2464
|
-
const id = this.#options.
|
|
2994
|
+
const id = this.#options.model ?? anthropicDefaultModelId;
|
|
2465
2995
|
this.model = {
|
|
2466
2996
|
id,
|
|
2467
2997
|
info: anthropicModels[id] ?? anthropicModels[anthropicDefaultModelId]
|
|
@@ -2550,23 +3080,21 @@ class AnthropicService extends AiServiceBase {
|
|
|
2550
3080
|
switch (chunk.type) {
|
|
2551
3081
|
case "message_start": {
|
|
2552
3082
|
const usage = chunk.message.usage;
|
|
2553
|
-
|
|
3083
|
+
yield {
|
|
2554
3084
|
type: "usage",
|
|
2555
|
-
inputTokens: usage.input_tokens
|
|
2556
|
-
outputTokens: usage.output_tokens
|
|
2557
|
-
cacheWriteTokens: usage.cache_creation_input_tokens ||
|
|
2558
|
-
cacheReadTokens: usage.cache_read_input_tokens ||
|
|
3085
|
+
inputTokens: usage.input_tokens,
|
|
3086
|
+
outputTokens: usage.output_tokens,
|
|
3087
|
+
cacheWriteTokens: usage.cache_creation_input_tokens || 0,
|
|
3088
|
+
cacheReadTokens: usage.cache_read_input_tokens || 0
|
|
2559
3089
|
};
|
|
2560
|
-
yield usageInfo;
|
|
2561
3090
|
break;
|
|
2562
3091
|
}
|
|
2563
3092
|
case "message_delta": {
|
|
2564
|
-
|
|
3093
|
+
yield {
|
|
2565
3094
|
type: "usage",
|
|
2566
3095
|
inputTokens: 0,
|
|
2567
|
-
outputTokens: chunk.usage.output_tokens
|
|
3096
|
+
outputTokens: chunk.usage.output_tokens
|
|
2568
3097
|
};
|
|
2569
|
-
yield deltaUsage;
|
|
2570
3098
|
break;
|
|
2571
3099
|
}
|
|
2572
3100
|
case "message_stop":
|
|
@@ -2916,7 +3444,7 @@ function stringify(object, opts = {}) {
|
|
|
2916
3444
|
return joined.length > 0 ? prefix + joined : "";
|
|
2917
3445
|
}
|
|
2918
3446
|
// ../../node_modules/openai/version.mjs
|
|
2919
|
-
var VERSION2 = "4.
|
|
3447
|
+
var VERSION2 = "4.80.0";
|
|
2920
3448
|
|
|
2921
3449
|
// ../../node_modules/openai/_shims/registry.mjs
|
|
2922
3450
|
var auto2 = false;
|
|
@@ -3217,6 +3745,35 @@ LineDecoder2.NEWLINE_CHARS = new Set([`
|
|
|
3217
3745
|
`, "\r"]);
|
|
3218
3746
|
LineDecoder2.NEWLINE_REGEXP = /\r\n|[\n\r]/g;
|
|
3219
3747
|
|
|
3748
|
+
// ../../node_modules/openai/internal/stream-utils.mjs
|
|
3749
|
+
function ReadableStreamToAsyncIterable2(stream) {
|
|
3750
|
+
if (stream[Symbol.asyncIterator])
|
|
3751
|
+
return stream;
|
|
3752
|
+
const reader = stream.getReader();
|
|
3753
|
+
return {
|
|
3754
|
+
async next() {
|
|
3755
|
+
try {
|
|
3756
|
+
const result = await reader.read();
|
|
3757
|
+
if (result?.done)
|
|
3758
|
+
reader.releaseLock();
|
|
3759
|
+
return result;
|
|
3760
|
+
} catch (e) {
|
|
3761
|
+
reader.releaseLock();
|
|
3762
|
+
throw e;
|
|
3763
|
+
}
|
|
3764
|
+
},
|
|
3765
|
+
async return() {
|
|
3766
|
+
const cancelPromise = reader.cancel();
|
|
3767
|
+
reader.releaseLock();
|
|
3768
|
+
await cancelPromise;
|
|
3769
|
+
return { done: true, value: undefined };
|
|
3770
|
+
},
|
|
3771
|
+
[Symbol.asyncIterator]() {
|
|
3772
|
+
return this;
|
|
3773
|
+
}
|
|
3774
|
+
};
|
|
3775
|
+
}
|
|
3776
|
+
|
|
3220
3777
|
// ../../node_modules/openai/streaming.mjs
|
|
3221
3778
|
class Stream2 {
|
|
3222
3779
|
constructor(iterator, controller) {
|
|
@@ -3283,7 +3840,7 @@ class Stream2 {
|
|
|
3283
3840
|
let consumed = false;
|
|
3284
3841
|
async function* iterLines() {
|
|
3285
3842
|
const lineDecoder = new LineDecoder2;
|
|
3286
|
-
const iter =
|
|
3843
|
+
const iter = ReadableStreamToAsyncIterable2(readableStream);
|
|
3287
3844
|
for await (const chunk of iter) {
|
|
3288
3845
|
for (const line of lineDecoder.decode(chunk)) {
|
|
3289
3846
|
yield line;
|
|
@@ -3375,7 +3932,7 @@ async function* _iterSSEMessages2(response, controller) {
|
|
|
3375
3932
|
}
|
|
3376
3933
|
const sseDecoder = new SSEDecoder2;
|
|
3377
3934
|
const lineDecoder = new LineDecoder2;
|
|
3378
|
-
const iter =
|
|
3935
|
+
const iter = ReadableStreamToAsyncIterable2(response.body);
|
|
3379
3936
|
for await (const sseChunk of iterSSEChunks2(iter)) {
|
|
3380
3937
|
for (const line of lineDecoder.decode(sseChunk)) {
|
|
3381
3938
|
const sse = sseDecoder.decode(line);
|
|
@@ -3474,33 +4031,6 @@ function partition2(str, delimiter) {
|
|
|
3474
4031
|
}
|
|
3475
4032
|
return [str, "", ""];
|
|
3476
4033
|
}
|
|
3477
|
-
function readableStreamAsyncIterable2(stream) {
|
|
3478
|
-
if (stream[Symbol.asyncIterator])
|
|
3479
|
-
return stream;
|
|
3480
|
-
const reader = stream.getReader();
|
|
3481
|
-
return {
|
|
3482
|
-
async next() {
|
|
3483
|
-
try {
|
|
3484
|
-
const result = await reader.read();
|
|
3485
|
-
if (result?.done)
|
|
3486
|
-
reader.releaseLock();
|
|
3487
|
-
return result;
|
|
3488
|
-
} catch (e) {
|
|
3489
|
-
reader.releaseLock();
|
|
3490
|
-
throw e;
|
|
3491
|
-
}
|
|
3492
|
-
},
|
|
3493
|
-
async return() {
|
|
3494
|
-
const cancelPromise = reader.cancel();
|
|
3495
|
-
reader.releaseLock();
|
|
3496
|
-
await cancelPromise;
|
|
3497
|
-
return { done: true, value: undefined };
|
|
3498
|
-
},
|
|
3499
|
-
[Symbol.asyncIterator]() {
|
|
3500
|
-
return this;
|
|
3501
|
-
}
|
|
3502
|
-
};
|
|
3503
|
-
}
|
|
3504
4034
|
|
|
3505
4035
|
// ../../node_modules/openai/uploads.mjs
|
|
3506
4036
|
var isResponseLike2 = (value) => value != null && typeof value === "object" && typeof value.url === "string" && typeof value.blob === "function";
|
|
@@ -3591,7 +4121,7 @@ var addFormValue = async (form, key, value) => {
|
|
|
3591
4121
|
};
|
|
3592
4122
|
|
|
3593
4123
|
// ../../node_modules/openai/core.mjs
|
|
3594
|
-
var
|
|
4124
|
+
var __classPrivateFieldSet4 = function(receiver, state, value, kind3, f) {
|
|
3595
4125
|
if (kind3 === "m")
|
|
3596
4126
|
throw new TypeError("Private method is not writable");
|
|
3597
4127
|
if (kind3 === "a" && !f)
|
|
@@ -3600,7 +4130,7 @@ var __classPrivateFieldSet3 = function(receiver, state, value, kind3, f) {
|
|
|
3600
4130
|
throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
3601
4131
|
return kind3 === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value), value;
|
|
3602
4132
|
};
|
|
3603
|
-
var
|
|
4133
|
+
var __classPrivateFieldGet4 = function(receiver, state, kind3, f) {
|
|
3604
4134
|
if (kind3 === "a" && !f)
|
|
3605
4135
|
throw new TypeError("Private accessor was defined without a getter");
|
|
3606
4136
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
|
|
@@ -3950,7 +4480,7 @@ class APIClient2 {
|
|
|
3950
4480
|
class AbstractPage2 {
|
|
3951
4481
|
constructor(client, response, body, options) {
|
|
3952
4482
|
_AbstractPage_client2.set(this, undefined);
|
|
3953
|
-
|
|
4483
|
+
__classPrivateFieldSet4(this, _AbstractPage_client2, client, "f");
|
|
3954
4484
|
this.options = options;
|
|
3955
4485
|
this.response = response;
|
|
3956
4486
|
this.body = body;
|
|
@@ -3977,7 +4507,7 @@ class AbstractPage2 {
|
|
|
3977
4507
|
nextOptions.query = undefined;
|
|
3978
4508
|
nextOptions.path = nextInfo.url.toString();
|
|
3979
4509
|
}
|
|
3980
|
-
return await
|
|
4510
|
+
return await __classPrivateFieldGet4(this, _AbstractPage_client2, "f").requestAPIList(this.constructor, nextOptions);
|
|
3981
4511
|
}
|
|
3982
4512
|
async* iterPages() {
|
|
3983
4513
|
let page = this;
|
|
@@ -4211,9 +4741,32 @@ function applyHeadersMut2(targetHeaders, newHeaders) {
|
|
|
4211
4741
|
}
|
|
4212
4742
|
}
|
|
4213
4743
|
}
|
|
4744
|
+
var SENSITIVE_HEADERS = new Set(["authorization", "api-key"]);
|
|
4214
4745
|
function debug2(action, ...args) {
|
|
4215
4746
|
if (typeof process !== "undefined" && process?.env?.["DEBUG"] === "true") {
|
|
4216
|
-
|
|
4747
|
+
const modifiedArgs = args.map((arg) => {
|
|
4748
|
+
if (!arg) {
|
|
4749
|
+
return arg;
|
|
4750
|
+
}
|
|
4751
|
+
if (arg["headers"]) {
|
|
4752
|
+
const modifiedArg2 = { ...arg, headers: { ...arg["headers"] } };
|
|
4753
|
+
for (const header in arg["headers"]) {
|
|
4754
|
+
if (SENSITIVE_HEADERS.has(header.toLowerCase())) {
|
|
4755
|
+
modifiedArg2["headers"][header] = "REDACTED";
|
|
4756
|
+
}
|
|
4757
|
+
}
|
|
4758
|
+
return modifiedArg2;
|
|
4759
|
+
}
|
|
4760
|
+
let modifiedArg = null;
|
|
4761
|
+
for (const header in arg) {
|
|
4762
|
+
if (SENSITIVE_HEADERS.has(header.toLowerCase())) {
|
|
4763
|
+
modifiedArg ?? (modifiedArg = { ...arg });
|
|
4764
|
+
modifiedArg[header] = "REDACTED";
|
|
4765
|
+
}
|
|
4766
|
+
}
|
|
4767
|
+
return modifiedArg ?? arg;
|
|
4768
|
+
});
|
|
4769
|
+
console.log(`OpenAI:DEBUG:${action}`, ...modifiedArgs);
|
|
4217
4770
|
}
|
|
4218
4771
|
}
|
|
4219
4772
|
var uuid42 = () => {
|
|
@@ -4450,7 +5003,7 @@ var isToolMessage = (message) => {
|
|
|
4450
5003
|
};
|
|
4451
5004
|
|
|
4452
5005
|
// ../../node_modules/openai/lib/EventStream.mjs
|
|
4453
|
-
var
|
|
5006
|
+
var __classPrivateFieldSet5 = function(receiver, state, value, kind3, f) {
|
|
4454
5007
|
if (kind3 === "m")
|
|
4455
5008
|
throw new TypeError("Private method is not writable");
|
|
4456
5009
|
if (kind3 === "a" && !f)
|
|
@@ -4459,7 +5012,7 @@ var __classPrivateFieldSet4 = function(receiver, state, value, kind3, f) {
|
|
|
4459
5012
|
throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
4460
5013
|
return kind3 === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value), value;
|
|
4461
5014
|
};
|
|
4462
|
-
var
|
|
5015
|
+
var __classPrivateFieldGet5 = function(receiver, state, kind3, f) {
|
|
4463
5016
|
if (kind3 === "a" && !f)
|
|
4464
5017
|
throw new TypeError("Private accessor was defined without a getter");
|
|
4465
5018
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
|
|
@@ -4499,17 +5052,17 @@ class EventStream {
|
|
|
4499
5052
|
_EventStream_errored.set(this, false);
|
|
4500
5053
|
_EventStream_aborted.set(this, false);
|
|
4501
5054
|
_EventStream_catchingPromiseCreated.set(this, false);
|
|
4502
|
-
|
|
4503
|
-
|
|
4504
|
-
|
|
5055
|
+
__classPrivateFieldSet5(this, _EventStream_connectedPromise, new Promise((resolve, reject) => {
|
|
5056
|
+
__classPrivateFieldSet5(this, _EventStream_resolveConnectedPromise, resolve, "f");
|
|
5057
|
+
__classPrivateFieldSet5(this, _EventStream_rejectConnectedPromise, reject, "f");
|
|
4505
5058
|
}), "f");
|
|
4506
|
-
|
|
4507
|
-
|
|
4508
|
-
|
|
5059
|
+
__classPrivateFieldSet5(this, _EventStream_endPromise, new Promise((resolve, reject) => {
|
|
5060
|
+
__classPrivateFieldSet5(this, _EventStream_resolveEndPromise, resolve, "f");
|
|
5061
|
+
__classPrivateFieldSet5(this, _EventStream_rejectEndPromise, reject, "f");
|
|
4509
5062
|
}), "f");
|
|
4510
|
-
|
|
5063
|
+
__classPrivateFieldGet5(this, _EventStream_connectedPromise, "f").catch(() => {
|
|
4511
5064
|
});
|
|
4512
|
-
|
|
5065
|
+
__classPrivateFieldGet5(this, _EventStream_endPromise, "f").catch(() => {
|
|
4513
5066
|
});
|
|
4514
5067
|
}
|
|
4515
5068
|
_run(executor) {
|
|
@@ -4517,34 +5070,34 @@ class EventStream {
|
|
|
4517
5070
|
executor().then(() => {
|
|
4518
5071
|
this._emitFinal();
|
|
4519
5072
|
this._emit("end");
|
|
4520
|
-
},
|
|
5073
|
+
}, __classPrivateFieldGet5(this, _EventStream_instances, "m", _EventStream_handleError).bind(this));
|
|
4521
5074
|
}, 0);
|
|
4522
5075
|
}
|
|
4523
5076
|
_connected() {
|
|
4524
5077
|
if (this.ended)
|
|
4525
5078
|
return;
|
|
4526
|
-
|
|
5079
|
+
__classPrivateFieldGet5(this, _EventStream_resolveConnectedPromise, "f").call(this);
|
|
4527
5080
|
this._emit("connect");
|
|
4528
5081
|
}
|
|
4529
5082
|
get ended() {
|
|
4530
|
-
return
|
|
5083
|
+
return __classPrivateFieldGet5(this, _EventStream_ended, "f");
|
|
4531
5084
|
}
|
|
4532
5085
|
get errored() {
|
|
4533
|
-
return
|
|
5086
|
+
return __classPrivateFieldGet5(this, _EventStream_errored, "f");
|
|
4534
5087
|
}
|
|
4535
5088
|
get aborted() {
|
|
4536
|
-
return
|
|
5089
|
+
return __classPrivateFieldGet5(this, _EventStream_aborted, "f");
|
|
4537
5090
|
}
|
|
4538
5091
|
abort() {
|
|
4539
5092
|
this.controller.abort();
|
|
4540
5093
|
}
|
|
4541
5094
|
on(event, listener) {
|
|
4542
|
-
const listeners =
|
|
5095
|
+
const listeners = __classPrivateFieldGet5(this, _EventStream_listeners, "f")[event] || (__classPrivateFieldGet5(this, _EventStream_listeners, "f")[event] = []);
|
|
4543
5096
|
listeners.push({ listener });
|
|
4544
5097
|
return this;
|
|
4545
5098
|
}
|
|
4546
5099
|
off(event, listener) {
|
|
4547
|
-
const listeners =
|
|
5100
|
+
const listeners = __classPrivateFieldGet5(this, _EventStream_listeners, "f")[event];
|
|
4548
5101
|
if (!listeners)
|
|
4549
5102
|
return this;
|
|
4550
5103
|
const index = listeners.findIndex((l) => l.listener === listener);
|
|
@@ -4553,52 +5106,52 @@ class EventStream {
|
|
|
4553
5106
|
return this;
|
|
4554
5107
|
}
|
|
4555
5108
|
once(event, listener) {
|
|
4556
|
-
const listeners =
|
|
5109
|
+
const listeners = __classPrivateFieldGet5(this, _EventStream_listeners, "f")[event] || (__classPrivateFieldGet5(this, _EventStream_listeners, "f")[event] = []);
|
|
4557
5110
|
listeners.push({ listener, once: true });
|
|
4558
5111
|
return this;
|
|
4559
5112
|
}
|
|
4560
5113
|
emitted(event) {
|
|
4561
5114
|
return new Promise((resolve, reject) => {
|
|
4562
|
-
|
|
5115
|
+
__classPrivateFieldSet5(this, _EventStream_catchingPromiseCreated, true, "f");
|
|
4563
5116
|
if (event !== "error")
|
|
4564
5117
|
this.once("error", reject);
|
|
4565
5118
|
this.once(event, resolve);
|
|
4566
5119
|
});
|
|
4567
5120
|
}
|
|
4568
5121
|
async done() {
|
|
4569
|
-
|
|
4570
|
-
await
|
|
5122
|
+
__classPrivateFieldSet5(this, _EventStream_catchingPromiseCreated, true, "f");
|
|
5123
|
+
await __classPrivateFieldGet5(this, _EventStream_endPromise, "f");
|
|
4571
5124
|
}
|
|
4572
5125
|
_emit(event, ...args) {
|
|
4573
|
-
if (
|
|
5126
|
+
if (__classPrivateFieldGet5(this, _EventStream_ended, "f")) {
|
|
4574
5127
|
return;
|
|
4575
5128
|
}
|
|
4576
5129
|
if (event === "end") {
|
|
4577
|
-
|
|
4578
|
-
|
|
5130
|
+
__classPrivateFieldSet5(this, _EventStream_ended, true, "f");
|
|
5131
|
+
__classPrivateFieldGet5(this, _EventStream_resolveEndPromise, "f").call(this);
|
|
4579
5132
|
}
|
|
4580
|
-
const listeners =
|
|
5133
|
+
const listeners = __classPrivateFieldGet5(this, _EventStream_listeners, "f")[event];
|
|
4581
5134
|
if (listeners) {
|
|
4582
|
-
|
|
5135
|
+
__classPrivateFieldGet5(this, _EventStream_listeners, "f")[event] = listeners.filter((l) => !l.once);
|
|
4583
5136
|
listeners.forEach(({ listener }) => listener(...args));
|
|
4584
5137
|
}
|
|
4585
5138
|
if (event === "abort") {
|
|
4586
5139
|
const error = args[0];
|
|
4587
|
-
if (!
|
|
5140
|
+
if (!__classPrivateFieldGet5(this, _EventStream_catchingPromiseCreated, "f") && !listeners?.length) {
|
|
4588
5141
|
Promise.reject(error);
|
|
4589
5142
|
}
|
|
4590
|
-
|
|
4591
|
-
|
|
5143
|
+
__classPrivateFieldGet5(this, _EventStream_rejectConnectedPromise, "f").call(this, error);
|
|
5144
|
+
__classPrivateFieldGet5(this, _EventStream_rejectEndPromise, "f").call(this, error);
|
|
4592
5145
|
this._emit("end");
|
|
4593
5146
|
return;
|
|
4594
5147
|
}
|
|
4595
5148
|
if (event === "error") {
|
|
4596
5149
|
const error = args[0];
|
|
4597
|
-
if (!
|
|
5150
|
+
if (!__classPrivateFieldGet5(this, _EventStream_catchingPromiseCreated, "f") && !listeners?.length) {
|
|
4598
5151
|
Promise.reject(error);
|
|
4599
5152
|
}
|
|
4600
|
-
|
|
4601
|
-
|
|
5153
|
+
__classPrivateFieldGet5(this, _EventStream_rejectConnectedPromise, "f").call(this, error);
|
|
5154
|
+
__classPrivateFieldGet5(this, _EventStream_rejectEndPromise, "f").call(this, error);
|
|
4602
5155
|
this._emit("end");
|
|
4603
5156
|
}
|
|
4604
5157
|
}
|
|
@@ -4606,12 +5159,12 @@ class EventStream {
|
|
|
4606
5159
|
}
|
|
4607
5160
|
}
|
|
4608
5161
|
_EventStream_connectedPromise = new WeakMap, _EventStream_resolveConnectedPromise = new WeakMap, _EventStream_rejectConnectedPromise = new WeakMap, _EventStream_endPromise = new WeakMap, _EventStream_resolveEndPromise = new WeakMap, _EventStream_rejectEndPromise = new WeakMap, _EventStream_listeners = new WeakMap, _EventStream_ended = new WeakMap, _EventStream_errored = new WeakMap, _EventStream_aborted = new WeakMap, _EventStream_catchingPromiseCreated = new WeakMap, _EventStream_instances = new WeakSet, _EventStream_handleError = function _EventStream_handleError2(error) {
|
|
4609
|
-
|
|
5162
|
+
__classPrivateFieldSet5(this, _EventStream_errored, true, "f");
|
|
4610
5163
|
if (error instanceof Error && error.name === "AbortError") {
|
|
4611
5164
|
error = new APIUserAbortError2;
|
|
4612
5165
|
}
|
|
4613
5166
|
if (error instanceof APIUserAbortError2) {
|
|
4614
|
-
|
|
5167
|
+
__classPrivateFieldSet5(this, _EventStream_aborted, true, "f");
|
|
4615
5168
|
return this._emit("abort", error);
|
|
4616
5169
|
}
|
|
4617
5170
|
if (error instanceof OpenAIError) {
|
|
@@ -4711,7 +5264,7 @@ function validateInputTools(tools) {
|
|
|
4711
5264
|
}
|
|
4712
5265
|
|
|
4713
5266
|
// ../../node_modules/openai/lib/AbstractChatCompletionRunner.mjs
|
|
4714
|
-
var
|
|
5267
|
+
var __classPrivateFieldGet6 = function(receiver, state, kind3, f) {
|
|
4715
5268
|
if (kind3 === "a" && !f)
|
|
4716
5269
|
throw new TypeError("Private accessor was defined without a getter");
|
|
4717
5270
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
|
|
@@ -4771,23 +5324,23 @@ class AbstractChatCompletionRunner extends EventStream {
|
|
|
4771
5324
|
}
|
|
4772
5325
|
async finalContent() {
|
|
4773
5326
|
await this.done();
|
|
4774
|
-
return
|
|
5327
|
+
return __classPrivateFieldGet6(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalContent).call(this);
|
|
4775
5328
|
}
|
|
4776
5329
|
async finalMessage() {
|
|
4777
5330
|
await this.done();
|
|
4778
|
-
return
|
|
5331
|
+
return __classPrivateFieldGet6(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalMessage).call(this);
|
|
4779
5332
|
}
|
|
4780
5333
|
async finalFunctionCall() {
|
|
4781
5334
|
await this.done();
|
|
4782
|
-
return
|
|
5335
|
+
return __classPrivateFieldGet6(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalFunctionCall).call(this);
|
|
4783
5336
|
}
|
|
4784
5337
|
async finalFunctionCallResult() {
|
|
4785
5338
|
await this.done();
|
|
4786
|
-
return
|
|
5339
|
+
return __classPrivateFieldGet6(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalFunctionCallResult).call(this);
|
|
4787
5340
|
}
|
|
4788
5341
|
async totalUsage() {
|
|
4789
5342
|
await this.done();
|
|
4790
|
-
return
|
|
5343
|
+
return __classPrivateFieldGet6(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_calculateTotalUsage).call(this);
|
|
4791
5344
|
}
|
|
4792
5345
|
allChatCompletions() {
|
|
4793
5346
|
return [...this._chatCompletions];
|
|
@@ -4796,20 +5349,20 @@ class AbstractChatCompletionRunner extends EventStream {
|
|
|
4796
5349
|
const completion = this._chatCompletions[this._chatCompletions.length - 1];
|
|
4797
5350
|
if (completion)
|
|
4798
5351
|
this._emit("finalChatCompletion", completion);
|
|
4799
|
-
const finalMessage =
|
|
5352
|
+
const finalMessage = __classPrivateFieldGet6(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalMessage).call(this);
|
|
4800
5353
|
if (finalMessage)
|
|
4801
5354
|
this._emit("finalMessage", finalMessage);
|
|
4802
|
-
const finalContent =
|
|
5355
|
+
const finalContent = __classPrivateFieldGet6(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalContent).call(this);
|
|
4803
5356
|
if (finalContent)
|
|
4804
5357
|
this._emit("finalContent", finalContent);
|
|
4805
|
-
const finalFunctionCall =
|
|
5358
|
+
const finalFunctionCall = __classPrivateFieldGet6(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalFunctionCall).call(this);
|
|
4806
5359
|
if (finalFunctionCall)
|
|
4807
5360
|
this._emit("finalFunctionCall", finalFunctionCall);
|
|
4808
|
-
const finalFunctionCallResult =
|
|
5361
|
+
const finalFunctionCallResult = __classPrivateFieldGet6(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalFunctionCallResult).call(this);
|
|
4809
5362
|
if (finalFunctionCallResult != null)
|
|
4810
5363
|
this._emit("finalFunctionCallResult", finalFunctionCallResult);
|
|
4811
5364
|
if (this._chatCompletions.some((c) => c.usage)) {
|
|
4812
|
-
this._emit("totalUsage",
|
|
5365
|
+
this._emit("totalUsage", __classPrivateFieldGet6(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_calculateTotalUsage).call(this));
|
|
4813
5366
|
}
|
|
4814
5367
|
}
|
|
4815
5368
|
async _createChatCompletion(client, params, options) {
|
|
@@ -4819,7 +5372,7 @@ class AbstractChatCompletionRunner extends EventStream {
|
|
|
4819
5372
|
this.controller.abort();
|
|
4820
5373
|
signal.addEventListener("abort", () => this.controller.abort());
|
|
4821
5374
|
}
|
|
4822
|
-
|
|
5375
|
+
__classPrivateFieldGet6(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_validateParams).call(this, params);
|
|
4823
5376
|
const chatCompletion = await client.chat.completions.create({ ...params, stream: false }, { ...options, signal: this.controller.signal });
|
|
4824
5377
|
this._connected();
|
|
4825
5378
|
return this._addChatCompletion(parseChatCompletion(chatCompletion, params));
|
|
@@ -4883,7 +5436,7 @@ class AbstractChatCompletionRunner extends EventStream {
|
|
|
4883
5436
|
continue;
|
|
4884
5437
|
}
|
|
4885
5438
|
const rawContent = await fn.function(parsed, this);
|
|
4886
|
-
const content =
|
|
5439
|
+
const content = __classPrivateFieldGet6(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_stringifyFunctionCallResult).call(this, rawContent);
|
|
4887
5440
|
this._addMessage({ role, name, content });
|
|
4888
5441
|
if (singleFunctionToCall)
|
|
4889
5442
|
return;
|
|
@@ -4969,7 +5522,7 @@ class AbstractChatCompletionRunner extends EventStream {
|
|
|
4969
5522
|
continue;
|
|
4970
5523
|
}
|
|
4971
5524
|
const rawContent = await fn.function(parsed, this);
|
|
4972
|
-
const content =
|
|
5525
|
+
const content = __classPrivateFieldGet6(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_stringifyFunctionCallResult).call(this, rawContent);
|
|
4973
5526
|
this._addMessage({ role, tool_call_id, content });
|
|
4974
5527
|
if (singleFunctionToCall) {
|
|
4975
5528
|
return;
|
|
@@ -4980,7 +5533,7 @@ class AbstractChatCompletionRunner extends EventStream {
|
|
|
4980
5533
|
}
|
|
4981
5534
|
}
|
|
4982
5535
|
_AbstractChatCompletionRunner_instances = new WeakSet, _AbstractChatCompletionRunner_getFinalContent = function _AbstractChatCompletionRunner_getFinalContent2() {
|
|
4983
|
-
return
|
|
5536
|
+
return __classPrivateFieldGet6(this, _AbstractChatCompletionRunner_instances, "m", _AbstractChatCompletionRunner_getFinalMessage).call(this).content ?? null;
|
|
4984
5537
|
}, _AbstractChatCompletionRunner_getFinalMessage = function _AbstractChatCompletionRunner_getFinalMessage2() {
|
|
4985
5538
|
let i = this.messages.length;
|
|
4986
5539
|
while (i-- > 0) {
|
|
@@ -5287,7 +5840,7 @@ var _parseJSON = (jsonString, allow) => {
|
|
|
5287
5840
|
var partialParse2 = (input) => parseJSON(input, Allow.ALL ^ Allow.NUM);
|
|
5288
5841
|
|
|
5289
5842
|
// ../../node_modules/openai/lib/ChatCompletionStream.mjs
|
|
5290
|
-
var
|
|
5843
|
+
var __classPrivateFieldSet6 = function(receiver, state, value, kind3, f) {
|
|
5291
5844
|
if (kind3 === "m")
|
|
5292
5845
|
throw new TypeError("Private method is not writable");
|
|
5293
5846
|
if (kind3 === "a" && !f)
|
|
@@ -5296,7 +5849,7 @@ var __classPrivateFieldSet5 = function(receiver, state, value, kind3, f) {
|
|
|
5296
5849
|
throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5297
5850
|
return kind3 === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value), value;
|
|
5298
5851
|
};
|
|
5299
|
-
var
|
|
5852
|
+
var __classPrivateFieldGet7 = function(receiver, state, kind3, f) {
|
|
5300
5853
|
if (kind3 === "a" && !f)
|
|
5301
5854
|
throw new TypeError("Private accessor was defined without a getter");
|
|
5302
5855
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
|
|
@@ -5323,11 +5876,11 @@ class ChatCompletionStream extends AbstractChatCompletionRunner {
|
|
|
5323
5876
|
_ChatCompletionStream_params.set(this, undefined);
|
|
5324
5877
|
_ChatCompletionStream_choiceEventStates.set(this, undefined);
|
|
5325
5878
|
_ChatCompletionStream_currentChatCompletionSnapshot.set(this, undefined);
|
|
5326
|
-
|
|
5327
|
-
|
|
5879
|
+
__classPrivateFieldSet6(this, _ChatCompletionStream_params, params, "f");
|
|
5880
|
+
__classPrivateFieldSet6(this, _ChatCompletionStream_choiceEventStates, [], "f");
|
|
5328
5881
|
}
|
|
5329
5882
|
get currentChatCompletionSnapshot() {
|
|
5330
|
-
return
|
|
5883
|
+
return __classPrivateFieldGet7(this, _ChatCompletionStream_currentChatCompletionSnapshot, "f");
|
|
5331
5884
|
}
|
|
5332
5885
|
static fromReadableStream(stream) {
|
|
5333
5886
|
const runner = new ChatCompletionStream(null);
|
|
@@ -5347,16 +5900,16 @@ class ChatCompletionStream extends AbstractChatCompletionRunner {
|
|
|
5347
5900
|
this.controller.abort();
|
|
5348
5901
|
signal.addEventListener("abort", () => this.controller.abort());
|
|
5349
5902
|
}
|
|
5350
|
-
|
|
5903
|
+
__classPrivateFieldGet7(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_beginRequest).call(this);
|
|
5351
5904
|
const stream = await client.chat.completions.create({ ...params, stream: true }, { ...options, signal: this.controller.signal });
|
|
5352
5905
|
this._connected();
|
|
5353
5906
|
for await (const chunk of stream) {
|
|
5354
|
-
|
|
5907
|
+
__classPrivateFieldGet7(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_addChunk).call(this, chunk);
|
|
5355
5908
|
}
|
|
5356
5909
|
if (stream.controller.signal?.aborted) {
|
|
5357
5910
|
throw new APIUserAbortError2;
|
|
5358
5911
|
}
|
|
5359
|
-
return this._addChatCompletion(
|
|
5912
|
+
return this._addChatCompletion(__classPrivateFieldGet7(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_endRequest).call(this));
|
|
5360
5913
|
}
|
|
5361
5914
|
async _fromReadableStream(readableStream, options) {
|
|
5362
5915
|
const signal = options?.signal;
|
|
@@ -5365,28 +5918,28 @@ class ChatCompletionStream extends AbstractChatCompletionRunner {
|
|
|
5365
5918
|
this.controller.abort();
|
|
5366
5919
|
signal.addEventListener("abort", () => this.controller.abort());
|
|
5367
5920
|
}
|
|
5368
|
-
|
|
5921
|
+
__classPrivateFieldGet7(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_beginRequest).call(this);
|
|
5369
5922
|
this._connected();
|
|
5370
5923
|
const stream = Stream2.fromReadableStream(readableStream, this.controller);
|
|
5371
5924
|
let chatId;
|
|
5372
5925
|
for await (const chunk of stream) {
|
|
5373
5926
|
if (chatId && chatId !== chunk.id) {
|
|
5374
|
-
this._addChatCompletion(
|
|
5927
|
+
this._addChatCompletion(__classPrivateFieldGet7(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_endRequest).call(this));
|
|
5375
5928
|
}
|
|
5376
|
-
|
|
5929
|
+
__classPrivateFieldGet7(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_addChunk).call(this, chunk);
|
|
5377
5930
|
chatId = chunk.id;
|
|
5378
5931
|
}
|
|
5379
5932
|
if (stream.controller.signal?.aborted) {
|
|
5380
5933
|
throw new APIUserAbortError2;
|
|
5381
5934
|
}
|
|
5382
|
-
return this._addChatCompletion(
|
|
5935
|
+
return this._addChatCompletion(__classPrivateFieldGet7(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_endRequest).call(this));
|
|
5383
5936
|
}
|
|
5384
5937
|
[(_ChatCompletionStream_params = new WeakMap, _ChatCompletionStream_choiceEventStates = new WeakMap, _ChatCompletionStream_currentChatCompletionSnapshot = new WeakMap, _ChatCompletionStream_instances = new WeakSet, _ChatCompletionStream_beginRequest = function _ChatCompletionStream_beginRequest() {
|
|
5385
5938
|
if (this.ended)
|
|
5386
5939
|
return;
|
|
5387
|
-
|
|
5940
|
+
__classPrivateFieldSet6(this, _ChatCompletionStream_currentChatCompletionSnapshot, undefined, "f");
|
|
5388
5941
|
}, _ChatCompletionStream_getChoiceEventState = function _ChatCompletionStream_getChoiceEventState(choice) {
|
|
5389
|
-
let state =
|
|
5942
|
+
let state = __classPrivateFieldGet7(this, _ChatCompletionStream_choiceEventStates, "f")[choice.index];
|
|
5390
5943
|
if (state) {
|
|
5391
5944
|
return state;
|
|
5392
5945
|
}
|
|
@@ -5398,12 +5951,12 @@ class ChatCompletionStream extends AbstractChatCompletionRunner {
|
|
|
5398
5951
|
done_tool_calls: new Set,
|
|
5399
5952
|
current_tool_call_index: null
|
|
5400
5953
|
};
|
|
5401
|
-
|
|
5954
|
+
__classPrivateFieldGet7(this, _ChatCompletionStream_choiceEventStates, "f")[choice.index] = state;
|
|
5402
5955
|
return state;
|
|
5403
5956
|
}, _ChatCompletionStream_addChunk = function _ChatCompletionStream_addChunk(chunk) {
|
|
5404
5957
|
if (this.ended)
|
|
5405
5958
|
return;
|
|
5406
|
-
const completion =
|
|
5959
|
+
const completion = __classPrivateFieldGet7(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_accumulateChatCompletion).call(this, chunk);
|
|
5407
5960
|
this._emit("chunk", chunk, completion);
|
|
5408
5961
|
for (const choice of chunk.choices) {
|
|
5409
5962
|
const choiceSnapshot = completion.choices[choice.index];
|
|
@@ -5433,18 +5986,18 @@ class ChatCompletionStream extends AbstractChatCompletionRunner {
|
|
|
5433
5986
|
snapshot: choiceSnapshot.logprobs?.refusal ?? []
|
|
5434
5987
|
});
|
|
5435
5988
|
}
|
|
5436
|
-
const state =
|
|
5989
|
+
const state = __classPrivateFieldGet7(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getChoiceEventState).call(this, choiceSnapshot);
|
|
5437
5990
|
if (choiceSnapshot.finish_reason) {
|
|
5438
|
-
|
|
5991
|
+
__classPrivateFieldGet7(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_emitContentDoneEvents).call(this, choiceSnapshot);
|
|
5439
5992
|
if (state.current_tool_call_index != null) {
|
|
5440
|
-
|
|
5993
|
+
__classPrivateFieldGet7(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_emitToolCallDoneEvent).call(this, choiceSnapshot, state.current_tool_call_index);
|
|
5441
5994
|
}
|
|
5442
5995
|
}
|
|
5443
5996
|
for (const toolCall of choice.delta.tool_calls ?? []) {
|
|
5444
5997
|
if (state.current_tool_call_index !== toolCall.index) {
|
|
5445
|
-
|
|
5998
|
+
__classPrivateFieldGet7(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_emitContentDoneEvents).call(this, choiceSnapshot);
|
|
5446
5999
|
if (state.current_tool_call_index != null) {
|
|
5447
|
-
|
|
6000
|
+
__classPrivateFieldGet7(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_emitToolCallDoneEvent).call(this, choiceSnapshot, state.current_tool_call_index);
|
|
5448
6001
|
}
|
|
5449
6002
|
}
|
|
5450
6003
|
state.current_tool_call_index = toolCall.index;
|
|
@@ -5468,7 +6021,7 @@ class ChatCompletionStream extends AbstractChatCompletionRunner {
|
|
|
5468
6021
|
}
|
|
5469
6022
|
}
|
|
5470
6023
|
}, _ChatCompletionStream_emitToolCallDoneEvent = function _ChatCompletionStream_emitToolCallDoneEvent(choiceSnapshot, toolCallIndex) {
|
|
5471
|
-
const state =
|
|
6024
|
+
const state = __classPrivateFieldGet7(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getChoiceEventState).call(this, choiceSnapshot);
|
|
5472
6025
|
if (state.done_tool_calls.has(toolCallIndex)) {
|
|
5473
6026
|
return;
|
|
5474
6027
|
}
|
|
@@ -5480,7 +6033,7 @@ class ChatCompletionStream extends AbstractChatCompletionRunner {
|
|
|
5480
6033
|
throw new Error("tool call snapshot missing `type`");
|
|
5481
6034
|
}
|
|
5482
6035
|
if (toolCallSnapshot.type === "function") {
|
|
5483
|
-
const inputTool =
|
|
6036
|
+
const inputTool = __classPrivateFieldGet7(this, _ChatCompletionStream_params, "f")?.tools?.find((tool) => tool.type === "function" && tool.function.name === toolCallSnapshot.function.name);
|
|
5484
6037
|
this._emit("tool_calls.function.arguments.done", {
|
|
5485
6038
|
name: toolCallSnapshot.function.name,
|
|
5486
6039
|
index: toolCallIndex,
|
|
@@ -5491,10 +6044,10 @@ class ChatCompletionStream extends AbstractChatCompletionRunner {
|
|
|
5491
6044
|
assertNever(toolCallSnapshot.type);
|
|
5492
6045
|
}
|
|
5493
6046
|
}, _ChatCompletionStream_emitContentDoneEvents = function _ChatCompletionStream_emitContentDoneEvents(choiceSnapshot) {
|
|
5494
|
-
const state =
|
|
6047
|
+
const state = __classPrivateFieldGet7(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getChoiceEventState).call(this, choiceSnapshot);
|
|
5495
6048
|
if (choiceSnapshot.message.content && !state.content_done) {
|
|
5496
6049
|
state.content_done = true;
|
|
5497
|
-
const responseFormat =
|
|
6050
|
+
const responseFormat = __classPrivateFieldGet7(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getAutoParseableResponseFormat).call(this);
|
|
5498
6051
|
this._emit("content.done", {
|
|
5499
6052
|
content: choiceSnapshot.message.content,
|
|
5500
6053
|
parsed: responseFormat ? responseFormat.$parseRaw(choiceSnapshot.message.content) : null
|
|
@@ -5516,25 +6069,25 @@ class ChatCompletionStream extends AbstractChatCompletionRunner {
|
|
|
5516
6069
|
if (this.ended) {
|
|
5517
6070
|
throw new OpenAIError(`stream has ended, this shouldn't happen`);
|
|
5518
6071
|
}
|
|
5519
|
-
const snapshot =
|
|
6072
|
+
const snapshot = __classPrivateFieldGet7(this, _ChatCompletionStream_currentChatCompletionSnapshot, "f");
|
|
5520
6073
|
if (!snapshot) {
|
|
5521
6074
|
throw new OpenAIError(`request ended without sending any chunks`);
|
|
5522
6075
|
}
|
|
5523
|
-
|
|
5524
|
-
|
|
5525
|
-
return finalizeChatCompletion(snapshot,
|
|
6076
|
+
__classPrivateFieldSet6(this, _ChatCompletionStream_currentChatCompletionSnapshot, undefined, "f");
|
|
6077
|
+
__classPrivateFieldSet6(this, _ChatCompletionStream_choiceEventStates, [], "f");
|
|
6078
|
+
return finalizeChatCompletion(snapshot, __classPrivateFieldGet7(this, _ChatCompletionStream_params, "f"));
|
|
5526
6079
|
}, _ChatCompletionStream_getAutoParseableResponseFormat = function _ChatCompletionStream_getAutoParseableResponseFormat() {
|
|
5527
|
-
const responseFormat =
|
|
6080
|
+
const responseFormat = __classPrivateFieldGet7(this, _ChatCompletionStream_params, "f")?.response_format;
|
|
5528
6081
|
if (isAutoParsableResponseFormat(responseFormat)) {
|
|
5529
6082
|
return responseFormat;
|
|
5530
6083
|
}
|
|
5531
6084
|
return null;
|
|
5532
6085
|
}, _ChatCompletionStream_accumulateChatCompletion = function _ChatCompletionStream_accumulateChatCompletion(chunk) {
|
|
5533
6086
|
var _a2, _b, _c, _d;
|
|
5534
|
-
let snapshot =
|
|
6087
|
+
let snapshot = __classPrivateFieldGet7(this, _ChatCompletionStream_currentChatCompletionSnapshot, "f");
|
|
5535
6088
|
const { choices, ...rest } = chunk;
|
|
5536
6089
|
if (!snapshot) {
|
|
5537
|
-
snapshot =
|
|
6090
|
+
snapshot = __classPrivateFieldSet6(this, _ChatCompletionStream_currentChatCompletionSnapshot, {
|
|
5538
6091
|
...rest,
|
|
5539
6092
|
choices: []
|
|
5540
6093
|
}, "f");
|
|
@@ -5565,7 +6118,7 @@ class ChatCompletionStream extends AbstractChatCompletionRunner {
|
|
|
5565
6118
|
}
|
|
5566
6119
|
if (finish_reason) {
|
|
5567
6120
|
choice.finish_reason = finish_reason;
|
|
5568
|
-
if (
|
|
6121
|
+
if (__classPrivateFieldGet7(this, _ChatCompletionStream_params, "f") && hasAutoParseableInput(__classPrivateFieldGet7(this, _ChatCompletionStream_params, "f"))) {
|
|
5569
6122
|
if (finish_reason === "length") {
|
|
5570
6123
|
throw new LengthFinishReasonError;
|
|
5571
6124
|
}
|
|
@@ -5599,7 +6152,7 @@ class ChatCompletionStream extends AbstractChatCompletionRunner {
|
|
|
5599
6152
|
}
|
|
5600
6153
|
if (content) {
|
|
5601
6154
|
choice.message.content = (choice.message.content || "") + content;
|
|
5602
|
-
if (!choice.message.refusal &&
|
|
6155
|
+
if (!choice.message.refusal && __classPrivateFieldGet7(this, _ChatCompletionStream_instances, "m", _ChatCompletionStream_getAutoParseableResponseFormat).call(this)) {
|
|
5603
6156
|
choice.message.parsed = partialParse2(choice.message.content);
|
|
5604
6157
|
}
|
|
5605
6158
|
}
|
|
@@ -5619,7 +6172,7 @@ class ChatCompletionStream extends AbstractChatCompletionRunner {
|
|
|
5619
6172
|
tool_call.function.name = fn.name;
|
|
5620
6173
|
if (fn?.arguments) {
|
|
5621
6174
|
tool_call.function.arguments += fn.arguments;
|
|
5622
|
-
if (shouldParseToolCall(
|
|
6175
|
+
if (shouldParseToolCall(__classPrivateFieldGet7(this, _ChatCompletionStream_params, "f"), tool_call)) {
|
|
5623
6176
|
tool_call.function.parsed_arguments = partialParse2(tool_call.function.arguments);
|
|
5624
6177
|
}
|
|
5625
6178
|
}
|
|
@@ -5864,14 +6417,14 @@ class Realtime extends APIResource2 {
|
|
|
5864
6417
|
Realtime.Sessions = Sessions;
|
|
5865
6418
|
|
|
5866
6419
|
// ../../node_modules/openai/lib/AssistantStream.mjs
|
|
5867
|
-
var
|
|
6420
|
+
var __classPrivateFieldGet8 = function(receiver, state, kind3, f) {
|
|
5868
6421
|
if (kind3 === "a" && !f)
|
|
5869
6422
|
throw new TypeError("Private accessor was defined without a getter");
|
|
5870
6423
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
|
|
5871
6424
|
throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
5872
6425
|
return kind3 === "m" ? f : kind3 === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
5873
6426
|
};
|
|
5874
|
-
var
|
|
6427
|
+
var __classPrivateFieldSet7 = function(receiver, state, value, kind3, f) {
|
|
5875
6428
|
if (kind3 === "m")
|
|
5876
6429
|
throw new TypeError("Private method is not writable");
|
|
5877
6430
|
if (kind3 === "a" && !f)
|
|
@@ -5985,12 +6538,12 @@ class AssistantStream extends EventStream {
|
|
|
5985
6538
|
this._connected();
|
|
5986
6539
|
const stream = Stream2.fromReadableStream(readableStream, this.controller);
|
|
5987
6540
|
for await (const event of stream) {
|
|
5988
|
-
|
|
6541
|
+
__classPrivateFieldGet8(this, _AssistantStream_instances, "m", _AssistantStream_addEvent).call(this, event);
|
|
5989
6542
|
}
|
|
5990
6543
|
if (stream.controller.signal?.aborted) {
|
|
5991
6544
|
throw new APIUserAbortError2;
|
|
5992
6545
|
}
|
|
5993
|
-
return this._addRun(
|
|
6546
|
+
return this._addRun(__classPrivateFieldGet8(this, _AssistantStream_instances, "m", _AssistantStream_endRequest).call(this));
|
|
5994
6547
|
}
|
|
5995
6548
|
toReadableStream() {
|
|
5996
6549
|
const stream = new Stream2(this[Symbol.asyncIterator].bind(this), this.controller);
|
|
@@ -6018,12 +6571,12 @@ class AssistantStream extends EventStream {
|
|
|
6018
6571
|
});
|
|
6019
6572
|
this._connected();
|
|
6020
6573
|
for await (const event of stream) {
|
|
6021
|
-
|
|
6574
|
+
__classPrivateFieldGet8(this, _AssistantStream_instances, "m", _AssistantStream_addEvent).call(this, event);
|
|
6022
6575
|
}
|
|
6023
6576
|
if (stream.controller.signal?.aborted) {
|
|
6024
6577
|
throw new APIUserAbortError2;
|
|
6025
6578
|
}
|
|
6026
|
-
return this._addRun(
|
|
6579
|
+
return this._addRun(__classPrivateFieldGet8(this, _AssistantStream_instances, "m", _AssistantStream_endRequest).call(this));
|
|
6027
6580
|
}
|
|
6028
6581
|
static createThreadAssistantStream(params, thread, options) {
|
|
6029
6582
|
const runner = new AssistantStream;
|
|
@@ -6042,30 +6595,30 @@ class AssistantStream extends EventStream {
|
|
|
6042
6595
|
return runner;
|
|
6043
6596
|
}
|
|
6044
6597
|
currentEvent() {
|
|
6045
|
-
return
|
|
6598
|
+
return __classPrivateFieldGet8(this, _AssistantStream_currentEvent, "f");
|
|
6046
6599
|
}
|
|
6047
6600
|
currentRun() {
|
|
6048
|
-
return
|
|
6601
|
+
return __classPrivateFieldGet8(this, _AssistantStream_currentRunSnapshot, "f");
|
|
6049
6602
|
}
|
|
6050
6603
|
currentMessageSnapshot() {
|
|
6051
|
-
return
|
|
6604
|
+
return __classPrivateFieldGet8(this, _AssistantStream_messageSnapshot, "f");
|
|
6052
6605
|
}
|
|
6053
6606
|
currentRunStepSnapshot() {
|
|
6054
|
-
return
|
|
6607
|
+
return __classPrivateFieldGet8(this, _AssistantStream_currentRunStepSnapshot, "f");
|
|
6055
6608
|
}
|
|
6056
6609
|
async finalRunSteps() {
|
|
6057
6610
|
await this.done();
|
|
6058
|
-
return Object.values(
|
|
6611
|
+
return Object.values(__classPrivateFieldGet8(this, _AssistantStream_runStepSnapshots, "f"));
|
|
6059
6612
|
}
|
|
6060
6613
|
async finalMessages() {
|
|
6061
6614
|
await this.done();
|
|
6062
|
-
return Object.values(
|
|
6615
|
+
return Object.values(__classPrivateFieldGet8(this, _AssistantStream_messageSnapshots, "f"));
|
|
6063
6616
|
}
|
|
6064
6617
|
async finalRun() {
|
|
6065
6618
|
await this.done();
|
|
6066
|
-
if (!
|
|
6619
|
+
if (!__classPrivateFieldGet8(this, _AssistantStream_finalRun, "f"))
|
|
6067
6620
|
throw Error("Final run was not received.");
|
|
6068
|
-
return
|
|
6621
|
+
return __classPrivateFieldGet8(this, _AssistantStream_finalRun, "f");
|
|
6069
6622
|
}
|
|
6070
6623
|
async _createThreadAssistantStream(thread, params, options) {
|
|
6071
6624
|
const signal = options?.signal;
|
|
@@ -6078,12 +6631,12 @@ class AssistantStream extends EventStream {
|
|
|
6078
6631
|
const stream = await thread.createAndRun(body, { ...options, signal: this.controller.signal });
|
|
6079
6632
|
this._connected();
|
|
6080
6633
|
for await (const event of stream) {
|
|
6081
|
-
|
|
6634
|
+
__classPrivateFieldGet8(this, _AssistantStream_instances, "m", _AssistantStream_addEvent).call(this, event);
|
|
6082
6635
|
}
|
|
6083
6636
|
if (stream.controller.signal?.aborted) {
|
|
6084
6637
|
throw new APIUserAbortError2;
|
|
6085
6638
|
}
|
|
6086
|
-
return this._addRun(
|
|
6639
|
+
return this._addRun(__classPrivateFieldGet8(this, _AssistantStream_instances, "m", _AssistantStream_endRequest).call(this));
|
|
6087
6640
|
}
|
|
6088
6641
|
async _createAssistantStream(run, threadId, params, options) {
|
|
6089
6642
|
const signal = options?.signal;
|
|
@@ -6096,12 +6649,12 @@ class AssistantStream extends EventStream {
|
|
|
6096
6649
|
const stream = await run.create(threadId, body, { ...options, signal: this.controller.signal });
|
|
6097
6650
|
this._connected();
|
|
6098
6651
|
for await (const event of stream) {
|
|
6099
|
-
|
|
6652
|
+
__classPrivateFieldGet8(this, _AssistantStream_instances, "m", _AssistantStream_addEvent).call(this, event);
|
|
6100
6653
|
}
|
|
6101
6654
|
if (stream.controller.signal?.aborted) {
|
|
6102
6655
|
throw new APIUserAbortError2;
|
|
6103
6656
|
}
|
|
6104
|
-
return this._addRun(
|
|
6657
|
+
return this._addRun(__classPrivateFieldGet8(this, _AssistantStream_instances, "m", _AssistantStream_endRequest).call(this));
|
|
6105
6658
|
}
|
|
6106
6659
|
static accumulateDelta(acc, delta) {
|
|
6107
6660
|
for (const [key, deltaValue] of Object.entries(delta)) {
|
|
@@ -6172,8 +6725,8 @@ class AssistantStream extends EventStream {
|
|
|
6172
6725
|
_AssistantStream_addEvent = function _AssistantStream_addEvent2(event) {
|
|
6173
6726
|
if (this.ended)
|
|
6174
6727
|
return;
|
|
6175
|
-
|
|
6176
|
-
|
|
6728
|
+
__classPrivateFieldSet7(this, _AssistantStream_currentEvent, event, "f");
|
|
6729
|
+
__classPrivateFieldGet8(this, _AssistantStream_instances, "m", _AssistantStream_handleEvent).call(this, event);
|
|
6177
6730
|
switch (event.event) {
|
|
6178
6731
|
case "thread.created":
|
|
6179
6732
|
break;
|
|
@@ -6186,7 +6739,7 @@ _AssistantStream_addEvent = function _AssistantStream_addEvent2(event) {
|
|
|
6186
6739
|
case "thread.run.cancelling":
|
|
6187
6740
|
case "thread.run.cancelled":
|
|
6188
6741
|
case "thread.run.expired":
|
|
6189
|
-
|
|
6742
|
+
__classPrivateFieldGet8(this, _AssistantStream_instances, "m", _AssistantStream_handleRun).call(this, event);
|
|
6190
6743
|
break;
|
|
6191
6744
|
case "thread.run.step.created":
|
|
6192
6745
|
case "thread.run.step.in_progress":
|
|
@@ -6195,14 +6748,14 @@ _AssistantStream_addEvent = function _AssistantStream_addEvent2(event) {
|
|
|
6195
6748
|
case "thread.run.step.failed":
|
|
6196
6749
|
case "thread.run.step.cancelled":
|
|
6197
6750
|
case "thread.run.step.expired":
|
|
6198
|
-
|
|
6751
|
+
__classPrivateFieldGet8(this, _AssistantStream_instances, "m", _AssistantStream_handleRunStep).call(this, event);
|
|
6199
6752
|
break;
|
|
6200
6753
|
case "thread.message.created":
|
|
6201
6754
|
case "thread.message.in_progress":
|
|
6202
6755
|
case "thread.message.delta":
|
|
6203
6756
|
case "thread.message.completed":
|
|
6204
6757
|
case "thread.message.incomplete":
|
|
6205
|
-
|
|
6758
|
+
__classPrivateFieldGet8(this, _AssistantStream_instances, "m", _AssistantStream_handleMessage).call(this, event);
|
|
6206
6759
|
break;
|
|
6207
6760
|
case "error":
|
|
6208
6761
|
throw new Error("Encountered an error event in event processing - errors should be processed earlier");
|
|
@@ -6211,13 +6764,13 @@ _AssistantStream_addEvent = function _AssistantStream_addEvent2(event) {
|
|
|
6211
6764
|
if (this.ended) {
|
|
6212
6765
|
throw new OpenAIError(`stream has ended, this shouldn't happen`);
|
|
6213
6766
|
}
|
|
6214
|
-
if (!
|
|
6767
|
+
if (!__classPrivateFieldGet8(this, _AssistantStream_finalRun, "f"))
|
|
6215
6768
|
throw Error("Final run has not been received");
|
|
6216
|
-
return
|
|
6769
|
+
return __classPrivateFieldGet8(this, _AssistantStream_finalRun, "f");
|
|
6217
6770
|
}, _AssistantStream_handleMessage = function _AssistantStream_handleMessage2(event) {
|
|
6218
|
-
const [accumulatedMessage, newContent] =
|
|
6219
|
-
|
|
6220
|
-
|
|
6771
|
+
const [accumulatedMessage, newContent] = __classPrivateFieldGet8(this, _AssistantStream_instances, "m", _AssistantStream_accumulateMessage).call(this, event, __classPrivateFieldGet8(this, _AssistantStream_messageSnapshot, "f"));
|
|
6772
|
+
__classPrivateFieldSet7(this, _AssistantStream_messageSnapshot, accumulatedMessage, "f");
|
|
6773
|
+
__classPrivateFieldGet8(this, _AssistantStream_messageSnapshots, "f")[accumulatedMessage.id] = accumulatedMessage;
|
|
6221
6774
|
for (const content of newContent) {
|
|
6222
6775
|
const snapshotContent = accumulatedMessage.content[content.index];
|
|
6223
6776
|
if (snapshotContent?.type == "text") {
|
|
@@ -6243,46 +6796,46 @@ _AssistantStream_addEvent = function _AssistantStream_addEvent2(event) {
|
|
|
6243
6796
|
throw Error("The snapshot associated with this text delta is not text or missing");
|
|
6244
6797
|
}
|
|
6245
6798
|
}
|
|
6246
|
-
if (content.index !=
|
|
6247
|
-
if (
|
|
6248
|
-
switch (
|
|
6799
|
+
if (content.index != __classPrivateFieldGet8(this, _AssistantStream_currentContentIndex, "f")) {
|
|
6800
|
+
if (__classPrivateFieldGet8(this, _AssistantStream_currentContent, "f")) {
|
|
6801
|
+
switch (__classPrivateFieldGet8(this, _AssistantStream_currentContent, "f").type) {
|
|
6249
6802
|
case "text":
|
|
6250
|
-
this._emit("textDone",
|
|
6803
|
+
this._emit("textDone", __classPrivateFieldGet8(this, _AssistantStream_currentContent, "f").text, __classPrivateFieldGet8(this, _AssistantStream_messageSnapshot, "f"));
|
|
6251
6804
|
break;
|
|
6252
6805
|
case "image_file":
|
|
6253
|
-
this._emit("imageFileDone",
|
|
6806
|
+
this._emit("imageFileDone", __classPrivateFieldGet8(this, _AssistantStream_currentContent, "f").image_file, __classPrivateFieldGet8(this, _AssistantStream_messageSnapshot, "f"));
|
|
6254
6807
|
break;
|
|
6255
6808
|
}
|
|
6256
6809
|
}
|
|
6257
|
-
|
|
6810
|
+
__classPrivateFieldSet7(this, _AssistantStream_currentContentIndex, content.index, "f");
|
|
6258
6811
|
}
|
|
6259
|
-
|
|
6812
|
+
__classPrivateFieldSet7(this, _AssistantStream_currentContent, accumulatedMessage.content[content.index], "f");
|
|
6260
6813
|
}
|
|
6261
6814
|
}
|
|
6262
6815
|
break;
|
|
6263
6816
|
case "thread.message.completed":
|
|
6264
6817
|
case "thread.message.incomplete":
|
|
6265
|
-
if (
|
|
6266
|
-
const currentContent = event.data.content[
|
|
6818
|
+
if (__classPrivateFieldGet8(this, _AssistantStream_currentContentIndex, "f") !== undefined) {
|
|
6819
|
+
const currentContent = event.data.content[__classPrivateFieldGet8(this, _AssistantStream_currentContentIndex, "f")];
|
|
6267
6820
|
if (currentContent) {
|
|
6268
6821
|
switch (currentContent.type) {
|
|
6269
6822
|
case "image_file":
|
|
6270
|
-
this._emit("imageFileDone", currentContent.image_file,
|
|
6823
|
+
this._emit("imageFileDone", currentContent.image_file, __classPrivateFieldGet8(this, _AssistantStream_messageSnapshot, "f"));
|
|
6271
6824
|
break;
|
|
6272
6825
|
case "text":
|
|
6273
|
-
this._emit("textDone", currentContent.text,
|
|
6826
|
+
this._emit("textDone", currentContent.text, __classPrivateFieldGet8(this, _AssistantStream_messageSnapshot, "f"));
|
|
6274
6827
|
break;
|
|
6275
6828
|
}
|
|
6276
6829
|
}
|
|
6277
6830
|
}
|
|
6278
|
-
if (
|
|
6831
|
+
if (__classPrivateFieldGet8(this, _AssistantStream_messageSnapshot, "f")) {
|
|
6279
6832
|
this._emit("messageDone", event.data);
|
|
6280
6833
|
}
|
|
6281
|
-
|
|
6834
|
+
__classPrivateFieldSet7(this, _AssistantStream_messageSnapshot, undefined, "f");
|
|
6282
6835
|
}
|
|
6283
6836
|
}, _AssistantStream_handleRunStep = function _AssistantStream_handleRunStep2(event) {
|
|
6284
|
-
const accumulatedRunStep =
|
|
6285
|
-
|
|
6837
|
+
const accumulatedRunStep = __classPrivateFieldGet8(this, _AssistantStream_instances, "m", _AssistantStream_accumulateRunStep).call(this, event);
|
|
6838
|
+
__classPrivateFieldSet7(this, _AssistantStream_currentRunStepSnapshot, accumulatedRunStep, "f");
|
|
6286
6839
|
switch (event.event) {
|
|
6287
6840
|
case "thread.run.step.created":
|
|
6288
6841
|
this._emit("runStepCreated", event.data);
|
|
@@ -6291,16 +6844,16 @@ _AssistantStream_addEvent = function _AssistantStream_addEvent2(event) {
|
|
|
6291
6844
|
const delta = event.data.delta;
|
|
6292
6845
|
if (delta.step_details && delta.step_details.type == "tool_calls" && delta.step_details.tool_calls && accumulatedRunStep.step_details.type == "tool_calls") {
|
|
6293
6846
|
for (const toolCall of delta.step_details.tool_calls) {
|
|
6294
|
-
if (toolCall.index ==
|
|
6847
|
+
if (toolCall.index == __classPrivateFieldGet8(this, _AssistantStream_currentToolCallIndex, "f")) {
|
|
6295
6848
|
this._emit("toolCallDelta", toolCall, accumulatedRunStep.step_details.tool_calls[toolCall.index]);
|
|
6296
6849
|
} else {
|
|
6297
|
-
if (
|
|
6298
|
-
this._emit("toolCallDone",
|
|
6850
|
+
if (__classPrivateFieldGet8(this, _AssistantStream_currentToolCall, "f")) {
|
|
6851
|
+
this._emit("toolCallDone", __classPrivateFieldGet8(this, _AssistantStream_currentToolCall, "f"));
|
|
6299
6852
|
}
|
|
6300
|
-
|
|
6301
|
-
|
|
6302
|
-
if (
|
|
6303
|
-
this._emit("toolCallCreated",
|
|
6853
|
+
__classPrivateFieldSet7(this, _AssistantStream_currentToolCallIndex, toolCall.index, "f");
|
|
6854
|
+
__classPrivateFieldSet7(this, _AssistantStream_currentToolCall, accumulatedRunStep.step_details.tool_calls[toolCall.index], "f");
|
|
6855
|
+
if (__classPrivateFieldGet8(this, _AssistantStream_currentToolCall, "f"))
|
|
6856
|
+
this._emit("toolCallCreated", __classPrivateFieldGet8(this, _AssistantStream_currentToolCall, "f"));
|
|
6304
6857
|
}
|
|
6305
6858
|
}
|
|
6306
6859
|
}
|
|
@@ -6310,12 +6863,12 @@ _AssistantStream_addEvent = function _AssistantStream_addEvent2(event) {
|
|
|
6310
6863
|
case "thread.run.step.failed":
|
|
6311
6864
|
case "thread.run.step.cancelled":
|
|
6312
6865
|
case "thread.run.step.expired":
|
|
6313
|
-
|
|
6866
|
+
__classPrivateFieldSet7(this, _AssistantStream_currentRunStepSnapshot, undefined, "f");
|
|
6314
6867
|
const details = event.data.step_details;
|
|
6315
6868
|
if (details.type == "tool_calls") {
|
|
6316
|
-
if (
|
|
6317
|
-
this._emit("toolCallDone",
|
|
6318
|
-
|
|
6869
|
+
if (__classPrivateFieldGet8(this, _AssistantStream_currentToolCall, "f")) {
|
|
6870
|
+
this._emit("toolCallDone", __classPrivateFieldGet8(this, _AssistantStream_currentToolCall, "f"));
|
|
6871
|
+
__classPrivateFieldSet7(this, _AssistantStream_currentToolCall, undefined, "f");
|
|
6319
6872
|
}
|
|
6320
6873
|
}
|
|
6321
6874
|
this._emit("runStepDone", event.data, accumulatedRunStep);
|
|
@@ -6324,34 +6877,34 @@ _AssistantStream_addEvent = function _AssistantStream_addEvent2(event) {
|
|
|
6324
6877
|
break;
|
|
6325
6878
|
}
|
|
6326
6879
|
}, _AssistantStream_handleEvent = function _AssistantStream_handleEvent2(event) {
|
|
6327
|
-
|
|
6880
|
+
__classPrivateFieldGet8(this, _AssistantStream_events, "f").push(event);
|
|
6328
6881
|
this._emit("event", event);
|
|
6329
6882
|
}, _AssistantStream_accumulateRunStep = function _AssistantStream_accumulateRunStep2(event) {
|
|
6330
6883
|
switch (event.event) {
|
|
6331
6884
|
case "thread.run.step.created":
|
|
6332
|
-
|
|
6885
|
+
__classPrivateFieldGet8(this, _AssistantStream_runStepSnapshots, "f")[event.data.id] = event.data;
|
|
6333
6886
|
return event.data;
|
|
6334
6887
|
case "thread.run.step.delta":
|
|
6335
|
-
let snapshot =
|
|
6888
|
+
let snapshot = __classPrivateFieldGet8(this, _AssistantStream_runStepSnapshots, "f")[event.data.id];
|
|
6336
6889
|
if (!snapshot) {
|
|
6337
6890
|
throw Error("Received a RunStepDelta before creation of a snapshot");
|
|
6338
6891
|
}
|
|
6339
6892
|
let data = event.data;
|
|
6340
6893
|
if (data.delta) {
|
|
6341
6894
|
const accumulated = AssistantStream.accumulateDelta(snapshot, data.delta);
|
|
6342
|
-
|
|
6895
|
+
__classPrivateFieldGet8(this, _AssistantStream_runStepSnapshots, "f")[event.data.id] = accumulated;
|
|
6343
6896
|
}
|
|
6344
|
-
return
|
|
6897
|
+
return __classPrivateFieldGet8(this, _AssistantStream_runStepSnapshots, "f")[event.data.id];
|
|
6345
6898
|
case "thread.run.step.completed":
|
|
6346
6899
|
case "thread.run.step.failed":
|
|
6347
6900
|
case "thread.run.step.cancelled":
|
|
6348
6901
|
case "thread.run.step.expired":
|
|
6349
6902
|
case "thread.run.step.in_progress":
|
|
6350
|
-
|
|
6903
|
+
__classPrivateFieldGet8(this, _AssistantStream_runStepSnapshots, "f")[event.data.id] = event.data;
|
|
6351
6904
|
break;
|
|
6352
6905
|
}
|
|
6353
|
-
if (
|
|
6354
|
-
return
|
|
6906
|
+
if (__classPrivateFieldGet8(this, _AssistantStream_runStepSnapshots, "f")[event.data.id])
|
|
6907
|
+
return __classPrivateFieldGet8(this, _AssistantStream_runStepSnapshots, "f")[event.data.id];
|
|
6355
6908
|
throw new Error("No snapshot available");
|
|
6356
6909
|
}, _AssistantStream_accumulateMessage = function _AssistantStream_accumulateMessage2(event, snapshot) {
|
|
6357
6910
|
let newContent = [];
|
|
@@ -6367,7 +6920,7 @@ _AssistantStream_addEvent = function _AssistantStream_addEvent2(event) {
|
|
|
6367
6920
|
for (const contentElement of data.delta.content) {
|
|
6368
6921
|
if (contentElement.index in snapshot.content) {
|
|
6369
6922
|
let currentContent = snapshot.content[contentElement.index];
|
|
6370
|
-
snapshot.content[contentElement.index] =
|
|
6923
|
+
snapshot.content[contentElement.index] = __classPrivateFieldGet8(this, _AssistantStream_instances, "m", _AssistantStream_accumulateContent).call(this, contentElement, currentContent);
|
|
6371
6924
|
} else {
|
|
6372
6925
|
snapshot.content[contentElement.index] = contentElement;
|
|
6373
6926
|
newContent.push(contentElement);
|
|
@@ -6388,7 +6941,7 @@ _AssistantStream_addEvent = function _AssistantStream_addEvent2(event) {
|
|
|
6388
6941
|
}, _AssistantStream_accumulateContent = function _AssistantStream_accumulateContent2(contentElement, currentContent) {
|
|
6389
6942
|
return AssistantStream.accumulateDelta(currentContent, contentElement);
|
|
6390
6943
|
}, _AssistantStream_handleRun = function _AssistantStream_handleRun2(event) {
|
|
6391
|
-
|
|
6944
|
+
__classPrivateFieldSet7(this, _AssistantStream_currentRunSnapshot, event.data, "f");
|
|
6392
6945
|
switch (event.event) {
|
|
6393
6946
|
case "thread.run.created":
|
|
6394
6947
|
break;
|
|
@@ -6401,10 +6954,10 @@ _AssistantStream_addEvent = function _AssistantStream_addEvent2(event) {
|
|
|
6401
6954
|
case "thread.run.failed":
|
|
6402
6955
|
case "thread.run.completed":
|
|
6403
6956
|
case "thread.run.expired":
|
|
6404
|
-
|
|
6405
|
-
if (
|
|
6406
|
-
this._emit("toolCallDone",
|
|
6407
|
-
|
|
6957
|
+
__classPrivateFieldSet7(this, _AssistantStream_finalRun, event.data, "f");
|
|
6958
|
+
if (__classPrivateFieldGet8(this, _AssistantStream_currentToolCall, "f")) {
|
|
6959
|
+
this._emit("toolCallDone", __classPrivateFieldGet8(this, _AssistantStream_currentToolCall, "f"));
|
|
6960
|
+
__classPrivateFieldSet7(this, _AssistantStream_currentToolCall, undefined, "f");
|
|
6408
6961
|
}
|
|
6409
6962
|
break;
|
|
6410
6963
|
case "thread.run.cancelling":
|
|
@@ -7314,7 +7867,7 @@ class DeepSeekService extends AiServiceBase {
|
|
|
7314
7867
|
baseURL: "https://api.deepseek.com/v1",
|
|
7315
7868
|
apiKey: options.apiKey
|
|
7316
7869
|
});
|
|
7317
|
-
const id = options.
|
|
7870
|
+
const id = options.model || deepSeekDefaultModelId;
|
|
7318
7871
|
this.model = {
|
|
7319
7872
|
id,
|
|
7320
7873
|
info: deepSeekModels[id] ?? deepSeekModels[deepSeekDefaultModelId]
|
|
@@ -7335,6 +7888,12 @@ class DeepSeekService extends AiServiceBase {
|
|
|
7335
7888
|
});
|
|
7336
7889
|
for await (const chunk of stream) {
|
|
7337
7890
|
const delta = chunk.choices[0]?.delta;
|
|
7891
|
+
if (delta?.reasoning_content) {
|
|
7892
|
+
yield {
|
|
7893
|
+
type: "reasoning",
|
|
7894
|
+
text: delta.reasoning_content
|
|
7895
|
+
};
|
|
7896
|
+
}
|
|
7338
7897
|
if (delta?.content) {
|
|
7339
7898
|
yield {
|
|
7340
7899
|
type: "text",
|
|
@@ -7365,7 +7924,7 @@ class OllamaService extends AiServiceBase {
|
|
|
7365
7924
|
apiKey: "ollama"
|
|
7366
7925
|
});
|
|
7367
7926
|
this.model = {
|
|
7368
|
-
id: options.
|
|
7927
|
+
id: options.model || "maryasov/qwen2.5-coder-cline:7b",
|
|
7369
7928
|
info: openAiModelInfoSaneDefaults
|
|
7370
7929
|
};
|
|
7371
7930
|
}
|
|
@@ -7415,6 +7974,15 @@ var createService = (provider, options) => {
|
|
|
7415
7974
|
}
|
|
7416
7975
|
};
|
|
7417
7976
|
// src/tool.ts
|
|
7977
|
+
var ToolResponseType;
|
|
7978
|
+
((ToolResponseType2) => {
|
|
7979
|
+
ToolResponseType2["Reply"] = "Reply";
|
|
7980
|
+
ToolResponseType2["Exit"] = "Exit";
|
|
7981
|
+
ToolResponseType2["Invalid"] = "Invalid";
|
|
7982
|
+
ToolResponseType2["Error"] = "Error";
|
|
7983
|
+
ToolResponseType2["Interrupted"] = "Interrupted";
|
|
7984
|
+
ToolResponseType2["HandOver"] = "HandOver";
|
|
7985
|
+
})(ToolResponseType ||= {});
|
|
7418
7986
|
var getAvailableTools = (provider, allTools) => {
|
|
7419
7987
|
return allTools.filter((tool) => tool.isAvailable(provider));
|
|
7420
7988
|
};
|
|
@@ -7425,11 +7993,14 @@ function parseAssistantMessage(assistantMessage, tools, toolNamePrefix) {
|
|
|
7425
7993
|
const results = [];
|
|
7426
7994
|
const toolTags = tools.map((tool) => `${toolNamePrefix}${tool.name}`);
|
|
7427
7995
|
const toolPattern = toolTags.join("|");
|
|
7428
|
-
|
|
7429
|
-
|
|
7430
|
-
|
|
7431
|
-
|
|
7432
|
-
|
|
7996
|
+
let remainingMessage = assistantMessage;
|
|
7997
|
+
let match;
|
|
7998
|
+
const tagRegex = new RegExp(`<(${toolPattern})>([\\s\\S]*?)<\\/\\1>`, "s");
|
|
7999
|
+
while (true) {
|
|
8000
|
+
match = tagRegex.exec(remainingMessage);
|
|
8001
|
+
if (match === null)
|
|
8002
|
+
break;
|
|
8003
|
+
const beforeTag = remainingMessage.slice(0, match.index).trim();
|
|
7433
8004
|
if (beforeTag) {
|
|
7434
8005
|
results.push({
|
|
7435
8006
|
type: "text",
|
|
@@ -7439,6 +8010,7 @@ function parseAssistantMessage(assistantMessage, tools, toolNamePrefix) {
|
|
|
7439
8010
|
const tagName = match[1];
|
|
7440
8011
|
const toolName = tagName.replace(toolNamePrefix, "");
|
|
7441
8012
|
const tool = tools.find((t) => t.name === toolName);
|
|
8013
|
+
const fullTagContent = match[0];
|
|
7442
8014
|
if (tool) {
|
|
7443
8015
|
const params = {};
|
|
7444
8016
|
for (const param of tool.parameters) {
|
|
@@ -7461,14 +8033,15 @@ function parseAssistantMessage(assistantMessage, tools, toolNamePrefix) {
|
|
|
7461
8033
|
content: fullTagContent
|
|
7462
8034
|
});
|
|
7463
8035
|
}
|
|
7464
|
-
|
|
7465
|
-
|
|
7466
|
-
|
|
7467
|
-
|
|
7468
|
-
|
|
7469
|
-
|
|
7470
|
-
}
|
|
7471
|
-
}
|
|
8036
|
+
remainingMessage = remainingMessage.slice(match.index + fullTagContent.length);
|
|
8037
|
+
}
|
|
8038
|
+
if (remainingMessage.trim()) {
|
|
8039
|
+
results.push({
|
|
8040
|
+
type: "text",
|
|
8041
|
+
content: remainingMessage.trim()
|
|
8042
|
+
});
|
|
8043
|
+
}
|
|
8044
|
+
if (results.length === 0) {
|
|
7472
8045
|
results.push({
|
|
7473
8046
|
type: "text",
|
|
7474
8047
|
content: assistantMessage
|
|
@@ -7504,63 +8077,152 @@ var toolUsePrompt = (tools, toolNamePrefix) => {
|
|
|
7504
8077
|
if (tools.length === 0) {
|
|
7505
8078
|
return "";
|
|
7506
8079
|
}
|
|
7507
|
-
const parameterPrefix = `${toolNamePrefix}parameter_`;
|
|
7508
|
-
let exampleIndex = 0;
|
|
8080
|
+
const parameterPrefix = `${toolNamePrefix}parameter_`;
|
|
8081
|
+
let exampleIndex = 0;
|
|
8082
|
+
return `
|
|
8083
|
+
====
|
|
8084
|
+
|
|
8085
|
+
TOOL USE
|
|
8086
|
+
|
|
8087
|
+
You have access to a set of tools that are executed upon the user's approval. You can use one tool per message, and will receive the result of that tool use in the user's response. You use tools step-by-step to accomplish a given task, with each tool use informed by the result of the previous tool use.
|
|
8088
|
+
|
|
8089
|
+
# Tool Use Formatting
|
|
8090
|
+
|
|
8091
|
+
Tool use is formatted using XML-style tags. The tool name is enclosed in opening and closing tags, and each parameter is similarly enclosed within its own set of tags. Here's the structure:
|
|
8092
|
+
|
|
8093
|
+
<${toolNamePrefix}tool_name>
|
|
8094
|
+
<${parameterPrefix}name1>value1</${parameterPrefix}name1>
|
|
8095
|
+
<${parameterPrefix}name2>value2</${parameterPrefix}name2>
|
|
8096
|
+
...
|
|
8097
|
+
</${toolNamePrefix}tool_name>
|
|
8098
|
+
|
|
8099
|
+
For example:
|
|
8100
|
+
|
|
8101
|
+
<${toolNamePrefix}read_file>
|
|
8102
|
+
<${parameterPrefix}path>src/main.js</${parameterPrefix}path>
|
|
8103
|
+
</${toolNamePrefix}read_file>
|
|
8104
|
+
|
|
8105
|
+
Always adhere to this format for the tool use to ensure proper parsing and execution.
|
|
8106
|
+
|
|
8107
|
+
# Tools
|
|
8108
|
+
${tools.map((tool) => toolInfoPrompt(tool, toolNamePrefix, parameterPrefix)).join(`
|
|
8109
|
+
`)}
|
|
8110
|
+
|
|
8111
|
+
# Tool Use Examples
|
|
8112
|
+
${tools.map((tool) => {
|
|
8113
|
+
let promp = "";
|
|
8114
|
+
for (const example of tool.examples ?? []) {
|
|
8115
|
+
promp += toolInfoExamplesPrompt(exampleIndex++, tool, example, toolNamePrefix, parameterPrefix);
|
|
8116
|
+
}
|
|
8117
|
+
return promp;
|
|
8118
|
+
}).join("")}
|
|
8119
|
+
# Tool Use Guidelines
|
|
8120
|
+
|
|
8121
|
+
1. **In \`<thinking>\` tags**, assess what information you have and what you need to proceed.
|
|
8122
|
+
2. **Choose one tool at a time per message** based on the task and its description. Do not assume a tool’s outcome without explicit confirmation.
|
|
8123
|
+
3. **Formulate tool use only in the specified XML format** for each tool.
|
|
8124
|
+
4. **Wait for the user’s response** after each tool use. Do not proceed until you have their confirmation.
|
|
8125
|
+
5. The user’s response may include:
|
|
8126
|
+
- Tool success or failure details
|
|
8127
|
+
- Linter errors
|
|
8128
|
+
- Terminal output or other relevant feedback
|
|
8129
|
+
6. **Never repeat or quote the entire tool command** in your final user-facing message. Summarize outcomes clearly and avoid echoing commands verbatim.
|
|
8130
|
+
7. **Respond concisely** and move the conversation forward. Do not re-issue the same command or re-trigger tool use without necessity.
|
|
8131
|
+
8. Follow these steps **iteratively**, confirming success and addressing issues as you go.
|
|
8132
|
+
|
|
8133
|
+
By adhering to these guidelines:
|
|
8134
|
+
- You maintain clarity without accidentally re-invoking tools.
|
|
8135
|
+
- You confirm each step’s results before proceeding.
|
|
8136
|
+
- You provide only the necessary information in user-facing replies to prevent re-interpretation as new commands.`;
|
|
8137
|
+
};
|
|
8138
|
+
var agentsPrompt = (agents, name) => `
|
|
8139
|
+
====
|
|
8140
|
+
|
|
8141
|
+
AVAILABLE AGENTS
|
|
8142
|
+
|
|
8143
|
+
The following agents are available for task handover:
|
|
8144
|
+
${agents.map((agent) => `
|
|
8145
|
+
- **${agent.name}**
|
|
8146
|
+
- Responsibilities:
|
|
8147
|
+
${agent.responsibilities.map((resp) => ` - ${resp}`).join(`
|
|
8148
|
+
`)}`).join(`
|
|
8149
|
+
`)}
|
|
8150
|
+
|
|
8151
|
+
- **Current Agent Role**
|
|
8152
|
+
You are currently acting as **${name}**. If you identify the task is beyond your current scope, use the handover tool to transition to the other agent. Include sufficient context so the new agent can seamlessly continue the work.
|
|
8153
|
+
`;
|
|
8154
|
+
var capabilities = (toolNamePrefix) => `
|
|
8155
|
+
====
|
|
8156
|
+
|
|
8157
|
+
CAPABILITIES
|
|
8158
|
+
|
|
8159
|
+
- You have access to a range of tools to aid you in your work. These tools help you effectively accomplish a wide range of tasks, such as writing code, making edits or improvements to existing files, understanding the current state of a project, performing system operations, and much more.
|
|
8160
|
+
- When the user initially gives you a task, a recursive list of all filepaths in the current working directory will be included in environment_details. This provides an overview of the project's file structure, offering key insights into the project from directory/file names (how developers conceptualize and organize their code) and file extensions (the language used). This can also guide decision-making on which files to explore further.
|
|
8161
|
+
- You can use ${toolNamePrefix}search_files to perform regex searches across files in a specified directory, outputting context-rich results that include surrounding lines. This is particularly useful for understanding code patterns, finding specific implementations, or identifying areas that need refactoring.
|
|
8162
|
+
- You can use the ${toolNamePrefix}list_code_definition_names tool to get an overview of source code definitions for all files at the top level of a specified directory. This can be particularly useful when you need to understand the broader context and relationships between certain parts of the code. You may need to call this tool multiple times to understand various parts of the codebase related to the task.
|
|
8163
|
+
\t- For example, when asked to make edits or improvements you might analyze the file structure in the initial environment_details to get an overview of the project, then use ${toolNamePrefix}list_code_definition_names to get further insight using source code definitions for files located in relevant directories, then ${toolNamePrefix}read_file to examine the contents of relevant files, analyze the code and suggest improvements or make necessary edits, then use the ${toolNamePrefix}replace_in_file tool to implement changes. If you refactored code that could affect other parts of the codebase, you could use ${toolNamePrefix}search_files to ensure you update other files as needed.
|
|
8164
|
+
- You can use the ${toolNamePrefix}execute_command tool to run commands on the user's computer whenever you feel it can help accomplish the user's task. When you need to execute a CLI command, you must provide a clear explanation of what the command does. Prefer to execute complex CLI commands over creating executable scripts, since they are more flexible and easier to run. Interactive and long-running commands are allowed, since the commands are run in the user's VSCode terminal. The user may keep commands running in the background and you will be kept updated on their status along the way. Each command you execute is run in a new terminal instance.`;
|
|
8165
|
+
var systemInformation = (info) => `
|
|
8166
|
+
====
|
|
8167
|
+
|
|
8168
|
+
SYSTEM INFORMATION
|
|
8169
|
+
|
|
8170
|
+
Operating System: ${info.os}`;
|
|
8171
|
+
var interactiveMode = (interactive) => {
|
|
8172
|
+
if (interactive) {
|
|
8173
|
+
return `
|
|
8174
|
+
====
|
|
8175
|
+
|
|
8176
|
+
INTERACTIVE MODE
|
|
8177
|
+
|
|
8178
|
+
You are in interactive mode. This means you may ask user questions to gather additional information to complete the task.
|
|
8179
|
+
`;
|
|
8180
|
+
}
|
|
8181
|
+
return `
|
|
8182
|
+
====
|
|
8183
|
+
|
|
8184
|
+
NON-INTERACTIVE MODE
|
|
8185
|
+
|
|
8186
|
+
You are in non-interactive mode. This means you will not be able to ask user questions to gather additional information to complete the task. You should try to use available tools to accomplish the task. If unable to precede further, you may try to end the task and provide a reason.
|
|
8187
|
+
`;
|
|
8188
|
+
};
|
|
8189
|
+
var customInstructions = (customInstructions2) => {
|
|
8190
|
+
const joined = customInstructions2.join(`
|
|
8191
|
+
`);
|
|
8192
|
+
if (joined.trim() === "") {
|
|
8193
|
+
return "";
|
|
8194
|
+
}
|
|
7509
8195
|
return `
|
|
7510
8196
|
====
|
|
7511
8197
|
|
|
7512
|
-
|
|
7513
|
-
|
|
7514
|
-
You have access to a set of tools that are executed upon the user's approval. You can use one tool per message, and will receive the result of that tool use in the user's response. You use tools step-by-step to accomplish a given task, with each tool use informed by the result of the previous tool use.
|
|
7515
|
-
|
|
7516
|
-
# Tool Use Formatting
|
|
7517
|
-
|
|
7518
|
-
Tool use is formatted using XML-style tags. The tool name is enclosed in opening and closing tags, and each parameter is similarly enclosed within its own set of tags. Here's the structure:
|
|
7519
|
-
|
|
7520
|
-
<${toolNamePrefix}tool_name>
|
|
7521
|
-
<${parameterPrefix}name1>value1</${parameterPrefix}name1>
|
|
7522
|
-
<${parameterPrefix}name2>value2</${parameterPrefix}name2>
|
|
7523
|
-
...
|
|
7524
|
-
</${toolNamePrefix}tool_name>
|
|
7525
|
-
|
|
7526
|
-
For example:
|
|
7527
|
-
|
|
7528
|
-
<${toolNamePrefix}read_file>
|
|
7529
|
-
<${parameterPrefix}path>src/main.js</${parameterPrefix}path>
|
|
7530
|
-
</${toolNamePrefix}read_file>
|
|
7531
|
-
|
|
7532
|
-
Always adhere to this format for the tool use to ensure proper parsing and execution.
|
|
8198
|
+
USER'S CUSTOM INSTRUCTIONS
|
|
7533
8199
|
|
|
7534
|
-
|
|
7535
|
-
${tools.map((tool) => toolInfoPrompt(tool, toolNamePrefix, parameterPrefix)).join(`
|
|
7536
|
-
`)}
|
|
8200
|
+
The following additional instructions are provided by the user, and should be followed to the best of your ability without interfering with the TOOL USE guidelines.
|
|
7537
8201
|
|
|
7538
|
-
|
|
7539
|
-
|
|
7540
|
-
|
|
7541
|
-
|
|
7542
|
-
|
|
8202
|
+
${joined}`;
|
|
8203
|
+
};
|
|
8204
|
+
var customScripts = (commands) => {
|
|
8205
|
+
const joined = Object.entries(commands).map(([name, command]) => {
|
|
8206
|
+
if (typeof command === "string") {
|
|
8207
|
+
return `- ${name}
|
|
8208
|
+
- Command: \`${command}\``;
|
|
7543
8209
|
}
|
|
7544
|
-
return
|
|
7545
|
-
|
|
7546
|
-
|
|
8210
|
+
return `- ${name}
|
|
8211
|
+
- Command: \`${command.command}\`
|
|
8212
|
+
- Description: ${command.description}`;
|
|
8213
|
+
}).join(`
|
|
8214
|
+
`);
|
|
8215
|
+
if (joined.trim() === "") {
|
|
8216
|
+
return "";
|
|
8217
|
+
}
|
|
8218
|
+
return `
|
|
8219
|
+
====
|
|
7547
8220
|
|
|
7548
|
-
|
|
7549
|
-
2. **Choose one tool at a time per message** based on the task and its description. Do not assume a tool’s outcome without explicit confirmation.
|
|
7550
|
-
3. **Formulate tool use only in the specified XML format** for each tool.
|
|
7551
|
-
4. **Wait for the user’s response** after each tool use. Do not proceed until you have their confirmation.
|
|
7552
|
-
5. The user’s response may include:
|
|
7553
|
-
- Tool success or failure details
|
|
7554
|
-
- Linter errors
|
|
7555
|
-
- Terminal output or other relevant feedback
|
|
7556
|
-
6. **Never repeat or quote the entire tool command** in your final user-facing message. Summarize outcomes clearly and avoid echoing commands verbatim.
|
|
7557
|
-
7. **Respond concisely** and move the conversation forward. Do not re-issue the same command or re-trigger tool use without necessity.
|
|
7558
|
-
8. Follow these steps **iteratively**, confirming success and addressing issues as you go.
|
|
8221
|
+
USER'S CUSTOM COMMANDS
|
|
7559
8222
|
|
|
7560
|
-
|
|
7561
|
-
|
|
7562
|
-
|
|
7563
|
-
- You provide only the necessary information in user-facing replies to prevent re-interpretation as new commands.`;
|
|
8223
|
+
The following additional commands are provided by the user, and should be followed to the best of your ability without interfering with the TOOL USE guidelines.
|
|
8224
|
+
|
|
8225
|
+
${joined}`;
|
|
7564
8226
|
};
|
|
7565
8227
|
var responsePrompts = {
|
|
7566
8228
|
errorInvokeTool: (tool, error) => `An error occurred while invoking the tool "${tool}": ${error}`,
|
|
@@ -7574,20 +8236,34 @@ ${result}
|
|
|
7574
8236
|
};
|
|
7575
8237
|
|
|
7576
8238
|
// src/Agent/AgentBase.ts
|
|
7577
|
-
var
|
|
7578
|
-
((
|
|
7579
|
-
|
|
7580
|
-
|
|
7581
|
-
|
|
7582
|
-
|
|
7583
|
-
|
|
8239
|
+
var TaskEventKind;
|
|
8240
|
+
((TaskEventKind2) => {
|
|
8241
|
+
TaskEventKind2["StartRequest"] = "StartRequest";
|
|
8242
|
+
TaskEventKind2["EndRequest"] = "EndRequest";
|
|
8243
|
+
TaskEventKind2["Usage"] = "Usage";
|
|
8244
|
+
TaskEventKind2["Text"] = "Text";
|
|
8245
|
+
TaskEventKind2["Reasoning"] = "Reasoning";
|
|
8246
|
+
TaskEventKind2["ToolUse"] = "ToolUse";
|
|
8247
|
+
TaskEventKind2["ToolReply"] = "ToolReply";
|
|
8248
|
+
TaskEventKind2["ToolInvalid"] = "ToolInvalid";
|
|
8249
|
+
TaskEventKind2["ToolError"] = "ToolError";
|
|
8250
|
+
TaskEventKind2["ToolInterrupted"] = "ToolInterrupted";
|
|
8251
|
+
TaskEventKind2["ToolHandOver"] = "ToolHandOver";
|
|
8252
|
+
TaskEventKind2["MaxIterationsReached"] = "MaxIterationsReached";
|
|
8253
|
+
TaskEventKind2["EndTask"] = "EndTask";
|
|
8254
|
+
})(TaskEventKind ||= {});
|
|
7584
8255
|
|
|
7585
8256
|
class AgentBase {
|
|
7586
8257
|
ai;
|
|
7587
8258
|
config;
|
|
7588
8259
|
handlers;
|
|
7589
|
-
constructor(ai, config) {
|
|
8260
|
+
constructor(name, ai, config) {
|
|
7590
8261
|
this.ai = ai;
|
|
8262
|
+
if (config.agents && Object.keys(config.agents).length > 0) {
|
|
8263
|
+
const agents = agentsPrompt(config.agents, name);
|
|
8264
|
+
config.systemPrompt += `
|
|
8265
|
+
${agents}`;
|
|
8266
|
+
}
|
|
7591
8267
|
this.config = config;
|
|
7592
8268
|
const handlers = {};
|
|
7593
8269
|
for (const tool of config.tools) {
|
|
@@ -7624,24 +8300,26 @@ class AgentBase {
|
|
|
7624
8300
|
let nextRequest = userMessage;
|
|
7625
8301
|
while (nextRequest) {
|
|
7626
8302
|
if (taskInfo.messages.length > taskInfo.options.maxIterations * 2) {
|
|
7627
|
-
callback({ kind: "
|
|
7628
|
-
return ["MaxIterations"
|
|
8303
|
+
callback({ kind: "MaxIterationsReached" /* MaxIterationsReached */, info: taskInfo });
|
|
8304
|
+
return ["MaxIterations", taskInfo];
|
|
7629
8305
|
}
|
|
7630
8306
|
const response = await this.#request(taskInfo, nextRequest, callback);
|
|
7631
8307
|
const [newMessage, exitReason] = await this.#handleResponse(taskInfo, response, callback);
|
|
7632
8308
|
if (exitReason) {
|
|
8309
|
+
callback({ kind: "EndTask" /* EndTask */, info: taskInfo });
|
|
7633
8310
|
return [exitReason, taskInfo];
|
|
7634
8311
|
}
|
|
7635
8312
|
nextRequest = newMessage;
|
|
7636
8313
|
}
|
|
7637
|
-
callback({ kind: "
|
|
7638
|
-
return ["
|
|
8314
|
+
callback({ kind: "EndTask" /* EndTask */, info: taskInfo });
|
|
8315
|
+
return [{ type: "Exit" /* Exit */, message: "Task completed successfully" }, taskInfo];
|
|
7639
8316
|
}
|
|
7640
|
-
async continueTask(userMessage, taskInfo, callback) {
|
|
8317
|
+
async continueTask(userMessage, taskInfo, callback = () => {
|
|
8318
|
+
}) {
|
|
7641
8319
|
return await this.#processLoop(userMessage, taskInfo, callback);
|
|
7642
8320
|
}
|
|
7643
8321
|
async#request(info, userMessage, callback) {
|
|
7644
|
-
await callback({ kind: "
|
|
8322
|
+
await callback({ kind: "StartRequest" /* StartRequest */, info, userMessage });
|
|
7645
8323
|
info.messages.push({
|
|
7646
8324
|
role: "user",
|
|
7647
8325
|
content: userMessage
|
|
@@ -7651,16 +8329,19 @@ class AgentBase {
|
|
|
7651
8329
|
for await (const chunk of stream) {
|
|
7652
8330
|
switch (chunk.type) {
|
|
7653
8331
|
case "usage":
|
|
7654
|
-
info.inputTokens = chunk.inputTokens;
|
|
7655
|
-
info.outputTokens = chunk.outputTokens;
|
|
8332
|
+
info.inputTokens = chunk.inputTokens ?? 0;
|
|
8333
|
+
info.outputTokens = chunk.outputTokens ?? 0;
|
|
7656
8334
|
info.cacheWriteTokens = chunk.cacheWriteTokens ?? 0;
|
|
7657
8335
|
info.cacheReadTokens = chunk.cacheReadTokens ?? 0;
|
|
7658
8336
|
info.totalCost = chunk.totalCost;
|
|
7659
|
-
await callback({ kind: "
|
|
8337
|
+
await callback({ kind: "Usage" /* Usage */, info });
|
|
7660
8338
|
break;
|
|
7661
8339
|
case "text":
|
|
7662
8340
|
currentAssistantMessage += chunk.text;
|
|
7663
|
-
await callback({ kind: "
|
|
8341
|
+
await callback({ kind: "Text" /* Text */, info, newText: chunk.text });
|
|
8342
|
+
break;
|
|
8343
|
+
case "reasoning":
|
|
8344
|
+
await callback({ kind: "Reasoning" /* Reasoning */, info, newText: chunk.text });
|
|
7664
8345
|
break;
|
|
7665
8346
|
}
|
|
7666
8347
|
}
|
|
@@ -7672,41 +8353,53 @@ class AgentBase {
|
|
|
7672
8353
|
content: currentAssistantMessage
|
|
7673
8354
|
});
|
|
7674
8355
|
const ret = parseAssistantMessage(currentAssistantMessage, this.config.tools, this.config.toolNamePrefix);
|
|
7675
|
-
await callback({ kind: "
|
|
8356
|
+
await callback({ kind: "EndRequest" /* EndRequest */, info });
|
|
7676
8357
|
return ret;
|
|
7677
8358
|
}
|
|
7678
8359
|
async#handleResponse(info, response, callback) {
|
|
7679
8360
|
const toolReponses = [];
|
|
7680
|
-
|
|
7681
|
-
|
|
7682
|
-
|
|
7683
|
-
|
|
7684
|
-
|
|
7685
|
-
|
|
7686
|
-
|
|
7687
|
-
|
|
7688
|
-
|
|
7689
|
-
|
|
7690
|
-
|
|
7691
|
-
|
|
7692
|
-
|
|
7693
|
-
|
|
7694
|
-
|
|
7695
|
-
|
|
7696
|
-
|
|
7697
|
-
|
|
7698
|
-
|
|
7699
|
-
|
|
7700
|
-
|
|
7701
|
-
|
|
7702
|
-
|
|
7703
|
-
|
|
7704
|
-
|
|
8361
|
+
outer:
|
|
8362
|
+
for (const content of response) {
|
|
8363
|
+
switch (content.type) {
|
|
8364
|
+
case "text":
|
|
8365
|
+
break;
|
|
8366
|
+
case "tool_use": {
|
|
8367
|
+
await callback({ kind: "ToolUse" /* ToolUse */, info, tool: content.name });
|
|
8368
|
+
const toolResp = await this.#invokeTool(content.name, content.params);
|
|
8369
|
+
switch (toolResp.type) {
|
|
8370
|
+
case "Reply" /* Reply */:
|
|
8371
|
+
await callback({ kind: "ToolReply" /* ToolReply */, info, tool: content.name });
|
|
8372
|
+
toolReponses.push({ tool: content.name, response: toolResp.message });
|
|
8373
|
+
break;
|
|
8374
|
+
case "Exit" /* Exit */:
|
|
8375
|
+
return [undefined, toolResp];
|
|
8376
|
+
case "Invalid" /* Invalid */:
|
|
8377
|
+
await callback({ kind: "ToolInvalid" /* ToolInvalid */, info, tool: content.name });
|
|
8378
|
+
toolReponses.push({ tool: content.name, response: toolResp.message });
|
|
8379
|
+
break outer;
|
|
8380
|
+
case "Error" /* Error */:
|
|
8381
|
+
await callback({ kind: "ToolError" /* ToolError */, info, tool: content.name });
|
|
8382
|
+
toolReponses.push({ tool: content.name, response: toolResp.message });
|
|
8383
|
+
break outer;
|
|
8384
|
+
case "Interrupted" /* Interrupted */:
|
|
8385
|
+
await callback({ kind: "ToolInterrupted" /* ToolInterrupted */, info, tool: content.name });
|
|
8386
|
+
return [undefined, toolResp];
|
|
8387
|
+
case "HandOver" /* HandOver */:
|
|
8388
|
+
await callback({
|
|
8389
|
+
kind: "ToolHandOver" /* ToolHandOver */,
|
|
8390
|
+
info,
|
|
8391
|
+
tool: content.name,
|
|
8392
|
+
agentName: toolResp.agentName,
|
|
8393
|
+
task: toolResp.task,
|
|
8394
|
+
context: toolResp.context,
|
|
8395
|
+
files: toolResp.files
|
|
8396
|
+
});
|
|
8397
|
+
return [undefined, toolResp];
|
|
8398
|
+
}
|
|
8399
|
+
break;
|
|
7705
8400
|
}
|
|
7706
|
-
break;
|
|
7707
8401
|
}
|
|
7708
8402
|
}
|
|
7709
|
-
}
|
|
7710
8403
|
if (toolReponses.length === 0 && !this.config.interactive) {
|
|
7711
8404
|
return [responsePrompts.requireUseTool, undefined];
|
|
7712
8405
|
}
|
|
@@ -7774,6 +8467,7 @@ __export(exports_allTools, {
|
|
|
7774
8467
|
readFile: () => readFile_default,
|
|
7775
8468
|
listFiles: () => listFiles_default,
|
|
7776
8469
|
listCodeDefinitionNames: () => listCodeDefinitionNames_default,
|
|
8470
|
+
handOver: () => handOver_default,
|
|
7777
8471
|
executeCommand: () => executeCommand_default,
|
|
7778
8472
|
attemptCompletion: () => attemptCompletion_default,
|
|
7779
8473
|
askFollowupQuestion: () => askFollowupQuestion_default
|
|
@@ -7837,14 +8531,14 @@ ${search}`);
|
|
|
7837
8531
|
// src/tools/utils/getArg.ts
|
|
7838
8532
|
var getString = (args, name, defaultValue) => {
|
|
7839
8533
|
const ret = args[name] ?? defaultValue;
|
|
7840
|
-
if (
|
|
8534
|
+
if (ret === undefined) {
|
|
7841
8535
|
throw new Error(`Missing required argument: ${name}`);
|
|
7842
8536
|
}
|
|
7843
8537
|
return ret;
|
|
7844
8538
|
};
|
|
7845
8539
|
var getStringArray = (args, name, defaultValue) => {
|
|
7846
8540
|
const ret = args[name];
|
|
7847
|
-
if (
|
|
8541
|
+
if (ret === undefined) {
|
|
7848
8542
|
if (defaultValue === undefined) {
|
|
7849
8543
|
throw new Error(`Missing required argument: ${name}`);
|
|
7850
8544
|
}
|
|
@@ -7854,7 +8548,7 @@ var getStringArray = (args, name, defaultValue) => {
|
|
|
7854
8548
|
};
|
|
7855
8549
|
var getBoolean = (args, name, defaultValue) => {
|
|
7856
8550
|
const ret = args[name];
|
|
7857
|
-
if (
|
|
8551
|
+
if (ret === undefined) {
|
|
7858
8552
|
if (defaultValue === undefined) {
|
|
7859
8553
|
throw new Error(`Missing required argument: ${name}`);
|
|
7860
8554
|
}
|
|
@@ -7871,7 +8565,7 @@ var getBoolean = (args, name, defaultValue) => {
|
|
|
7871
8565
|
};
|
|
7872
8566
|
var getInt = (args, name, defaultValue) => {
|
|
7873
8567
|
const ret = args[name];
|
|
7874
|
-
if (
|
|
8568
|
+
if (ret === undefined) {
|
|
7875
8569
|
if (defaultValue === undefined) {
|
|
7876
8570
|
throw new Error(`Missing required argument: ${name}`);
|
|
7877
8571
|
}
|
|
@@ -8493,6 +9187,81 @@ var writeToFile_default = {
|
|
|
8493
9187
|
handler: handler9,
|
|
8494
9188
|
isAvailable: isAvailable9
|
|
8495
9189
|
};
|
|
9190
|
+
// src/tools/handOver.ts
|
|
9191
|
+
var toolInfo10 = {
|
|
9192
|
+
name: "hand_over",
|
|
9193
|
+
description: "Hand over the current task to another agent to complete",
|
|
9194
|
+
parameters: [
|
|
9195
|
+
{
|
|
9196
|
+
name: "agent_name",
|
|
9197
|
+
description: "The name of the agent to hand over the task to",
|
|
9198
|
+
required: true,
|
|
9199
|
+
usageValue: "Name of the target agent"
|
|
9200
|
+
},
|
|
9201
|
+
{
|
|
9202
|
+
name: "task",
|
|
9203
|
+
description: "The task to be completed by the target agent",
|
|
9204
|
+
required: true,
|
|
9205
|
+
usageValue: "Task description"
|
|
9206
|
+
},
|
|
9207
|
+
{
|
|
9208
|
+
name: "context",
|
|
9209
|
+
description: "The context information for the task",
|
|
9210
|
+
required: true,
|
|
9211
|
+
usageValue: "Context information"
|
|
9212
|
+
},
|
|
9213
|
+
{
|
|
9214
|
+
name: "files",
|
|
9215
|
+
description: "The files relevant to the task",
|
|
9216
|
+
required: true,
|
|
9217
|
+
usageValue: "Relevant files"
|
|
9218
|
+
}
|
|
9219
|
+
],
|
|
9220
|
+
examples: [
|
|
9221
|
+
{
|
|
9222
|
+
description: "Hand over a coding task to the coder agent",
|
|
9223
|
+
parameters: [
|
|
9224
|
+
{
|
|
9225
|
+
name: "agent_name",
|
|
9226
|
+
value: "Coder"
|
|
9227
|
+
},
|
|
9228
|
+
{
|
|
9229
|
+
name: "task",
|
|
9230
|
+
value: "Implement the login feature"
|
|
9231
|
+
},
|
|
9232
|
+
{
|
|
9233
|
+
name: "context",
|
|
9234
|
+
value: "We need a secure login system with email and password"
|
|
9235
|
+
},
|
|
9236
|
+
{
|
|
9237
|
+
name: "files",
|
|
9238
|
+
value: "src/auth/login.ts,src/auth/types.ts"
|
|
9239
|
+
}
|
|
9240
|
+
]
|
|
9241
|
+
}
|
|
9242
|
+
]
|
|
9243
|
+
};
|
|
9244
|
+
var handler10 = async (_provider, args) => {
|
|
9245
|
+
const agentName = getString(args, "agent_name");
|
|
9246
|
+
const task = getString(args, "task");
|
|
9247
|
+
const context = getString(args, "context", undefined);
|
|
9248
|
+
const files = getStringArray(args, "files", []);
|
|
9249
|
+
return {
|
|
9250
|
+
type: "HandOver" /* HandOver */,
|
|
9251
|
+
agentName,
|
|
9252
|
+
task,
|
|
9253
|
+
context,
|
|
9254
|
+
files
|
|
9255
|
+
};
|
|
9256
|
+
};
|
|
9257
|
+
var isAvailable10 = (_provider) => {
|
|
9258
|
+
return true;
|
|
9259
|
+
};
|
|
9260
|
+
var handOver_default = {
|
|
9261
|
+
...toolInfo10,
|
|
9262
|
+
handler: handler10,
|
|
9263
|
+
isAvailable: isAvailable10
|
|
9264
|
+
};
|
|
8496
9265
|
// src/Agent/CoderAgent/prompts.ts
|
|
8497
9266
|
var basePrompt = "You are a highly skilled software engineer with extensive knowledge in many programming languages, frameworks, design patterns, and best practices.";
|
|
8498
9267
|
var editingFilesPrompt = (toolNamePrefix) => `
|
|
@@ -8556,17 +9325,6 @@ You have access to two tools for working with files: **${toolNamePrefix}write_to
|
|
|
8556
9325
|
4. Once the file has been edited with either ${toolNamePrefix}write_to_file or ${toolNamePrefix}replace_in_file, the system will provide you with the final state of the modified file. Use this updated content as the reference point for any subsequent SEARCH/REPLACE operations, since it reflects any auto-formatting or user-applied changes.
|
|
8557
9326
|
|
|
8558
9327
|
By thoughtfully selecting between ${toolNamePrefix}write_to_file and ${toolNamePrefix}replace_in_file, you can make your file editing process smoother, safer, and more efficient.`;
|
|
8559
|
-
var capabilities = (toolNamePrefix) => `
|
|
8560
|
-
====
|
|
8561
|
-
|
|
8562
|
-
CAPABILITIES
|
|
8563
|
-
|
|
8564
|
-
- You have access to a range of tools to aid you in your work. These tools help you effectively accomplish a wide range of tasks, such as writing code, making edits or improvements to existing files, understanding the current state of a project, performing system operations, and much more.
|
|
8565
|
-
- When the user initially gives you a task, a recursive list of all filepaths in the current working directory will be included in environment_details. This provides an overview of the project's file structure, offering key insights into the project from directory/file names (how developers conceptualize and organize their code) and file extensions (the language used). This can also guide decision-making on which files to explore further.
|
|
8566
|
-
- You can use ${toolNamePrefix}search_files to perform regex searches across files in a specified directory, outputting context-rich results that include surrounding lines. This is particularly useful for understanding code patterns, finding specific implementations, or identifying areas that need refactoring.
|
|
8567
|
-
- You can use the ${toolNamePrefix}list_code_definition_names tool to get an overview of source code definitions for all files at the top level of a specified directory. This can be particularly useful when you need to understand the broader context and relationships between certain parts of the code. You may need to call this tool multiple times to understand various parts of the codebase related to the task.
|
|
8568
|
-
\t- For example, when asked to make edits or improvements you might analyze the file structure in the initial environment_details to get an overview of the project, then use ${toolNamePrefix}list_code_definition_names to get further insight using source code definitions for files located in relevant directories, then ${toolNamePrefix}read_file to examine the contents of relevant files, analyze the code and suggest improvements or make necessary edits, then use the ${toolNamePrefix}replace_in_file tool to implement changes. If you refactored code that could affect other parts of the codebase, you could use ${toolNamePrefix}search_files to ensure you update other files as needed.
|
|
8569
|
-
- You can use the ${toolNamePrefix}execute_command tool to run commands on the user's computer whenever you feel it can help accomplish the user's task. When you need to execute a CLI command, you must provide a clear explanation of what the command does. Prefer to execute complex CLI commands over creating executable scripts, since they are more flexible and easier to run. Interactive and long-running commands are allowed, since the commands are run in the user's VSCode terminal. The user may keep commands running in the background and you will be kept updated on their status along the way. Each command you execute is run in a new terminal instance.`;
|
|
8570
9328
|
var rules = (toolNamePrefix) => `
|
|
8571
9329
|
====
|
|
8572
9330
|
|
|
@@ -8581,7 +9339,9 @@ RULES
|
|
|
8581
9339
|
- When creating a new project (such as an app, website, or any software project), organize all new files within a dedicated project directory unless the user specifies otherwise. Use appropriate file paths when creating files, as the ${toolNamePrefix}write_to_file tool will automatically create any necessary directories. Structure the project logically, adhering to best practices for the specific type of project being created. Unless otherwise specified, new projects should be easily run without additional setup, for example most projects can be built in HTML, CSS, and JavaScript - which you can open in a browser.
|
|
8582
9340
|
- Be sure to consider the type of project (e.g. Python, JavaScript, web application) when determining the appropriate structure and files to include. Also consider what files may be most relevant to accomplishing the task, for example looking at a project's manifest file would help you understand the project's dependencies, which you could incorporate into any code you write.
|
|
8583
9341
|
- When making changes to code, always consider the context in which the code is being used. Ensure that your changes are compatible with the existing codebase and that they follow the project's coding standards and best practices.
|
|
9342
|
+
- **Adhere to any established coding style, linting rules, or naming conventions if they are known or can be inferred from the existing codebase, to maintain consistency.**
|
|
8584
9343
|
- When you want to modify a file, use the ${toolNamePrefix}replace_in_file or ${toolNamePrefix}write_to_file tool directly with the desired changes. You do not need to display the changes before using the tool.
|
|
9344
|
+
- **Do not guess or hallucinate file content that has not been explicitly provided or read. Always read an existing file with \`${toolNamePrefix}read_file\` (or rely on content the user directly provided) before modifying it, unless you are creating a brand-new file.**
|
|
8585
9345
|
- Do not ask for more information than necessary. Use the tools provided to accomplish the user's request efficiently and effectively. When you've completed your task, you must use the ${toolNamePrefix}attempt_completion tool to present the result to the user.
|
|
8586
9346
|
- The user may provide a file's contents directly in their message, in which case you shouldn't use the ${toolNamePrefix}read_file tool to get the file contents again since you already have it.
|
|
8587
9347
|
- Your goal is to try to accomplish the user's task, NOT engage in a back and forth conversation.
|
|
@@ -8603,69 +9363,7 @@ You accomplish a given task iteratively, breaking it down into clear steps and w
|
|
|
8603
9363
|
3. Remember, you have extensive capabilities with access to a wide range of tools that can be used in powerful and clever ways as necessary to accomplish each goal. Before calling a tool, do some analysis within <thinking></thinking> tags. First, analyze the file structure provided in environment_details to gain context and insights for proceeding effectively. Then, think about which of the provided tools is the most relevant tool to accomplish the user's task. Next, go through each of the required parameters of the relevant tool and determine if the user has directly provided or given enough information to infer a value. When deciding if the parameter can be inferred, carefully consider all the context to see if it supports a specific value. If all of the required parameters are present or can be reasonably inferred, close the thinking tag and proceed with the tool use.
|
|
8604
9364
|
4. Once you've completed the user's task, you must use the ${toolNamePrefix}attempt_completion tool to present the result of the task to the user.
|
|
8605
9365
|
5. The user may provide feedback, which you can use to make improvements and try again. But DO NOT continue in pointless back and forth conversations, i.e. don't end your responses with questions or offers for further assistance.`;
|
|
8606
|
-
var
|
|
8607
|
-
====
|
|
8608
|
-
|
|
8609
|
-
SYSTEM INFORMATION
|
|
8610
|
-
|
|
8611
|
-
Operating System: ${info.os}`;
|
|
8612
|
-
var customInstructions = (customInstructions2) => {
|
|
8613
|
-
const joined = customInstructions2.join(`
|
|
8614
|
-
`);
|
|
8615
|
-
if (joined.trim() === "") {
|
|
8616
|
-
return "";
|
|
8617
|
-
}
|
|
8618
|
-
return `
|
|
8619
|
-
====
|
|
8620
|
-
|
|
8621
|
-
USER'S CUSTOM INSTRUCTIONS
|
|
8622
|
-
|
|
8623
|
-
The following additional instructions are provided by the user, and should be followed to the best of your ability without interfering with the TOOL USE guidelines.
|
|
8624
|
-
|
|
8625
|
-
${joined}`;
|
|
8626
|
-
};
|
|
8627
|
-
var customCommands = (commands) => {
|
|
8628
|
-
const joined = Object.entries(commands).map(([name, command]) => {
|
|
8629
|
-
if (typeof command === "string") {
|
|
8630
|
-
return `- ${name}
|
|
8631
|
-
- Command: \`${command}\``;
|
|
8632
|
-
}
|
|
8633
|
-
return `- ${name}
|
|
8634
|
-
- Command: \`${command.command}\`
|
|
8635
|
-
- Description: ${command.description}`;
|
|
8636
|
-
}).join(`
|
|
8637
|
-
`);
|
|
8638
|
-
if (joined.trim() === "") {
|
|
8639
|
-
return "";
|
|
8640
|
-
}
|
|
8641
|
-
return `
|
|
8642
|
-
====
|
|
8643
|
-
|
|
8644
|
-
USER'S CUSTOM COMMANDS
|
|
8645
|
-
|
|
8646
|
-
The following additional commands are provided by the user, and should be followed to the best of your ability without interfering with the TOOL USE guidelines.
|
|
8647
|
-
|
|
8648
|
-
${joined}`;
|
|
8649
|
-
};
|
|
8650
|
-
var interactiveMode = (interactive) => {
|
|
8651
|
-
if (interactive) {
|
|
8652
|
-
return `
|
|
8653
|
-
====
|
|
8654
|
-
|
|
8655
|
-
INTERACTIVE MODE
|
|
8656
|
-
|
|
8657
|
-
You are in interactive mode. This means you may ask user questions to gather additional information to complete the task.
|
|
8658
|
-
`;
|
|
8659
|
-
}
|
|
8660
|
-
return `
|
|
8661
|
-
====
|
|
8662
|
-
|
|
8663
|
-
NON-INTERACTIVE MODE
|
|
8664
|
-
|
|
8665
|
-
You are in non-interactive mode. This means you will not be able to ask user questions to gather additional information to complete the task. You should try to use available tools to accomplish the task. If unable to precede further, you may try to end the task and provide a reason.
|
|
8666
|
-
`;
|
|
8667
|
-
};
|
|
8668
|
-
var fullSystemPrompt = (info, tools, toolNamePrefix, instructions, commands, interactive) => `
|
|
9366
|
+
var fullSystemPrompt = (info, tools, toolNamePrefix, instructions, scripts, interactive) => `
|
|
8669
9367
|
${basePrompt}
|
|
8670
9368
|
${toolUsePrompt(tools, toolNamePrefix)}
|
|
8671
9369
|
${editingFilesPrompt(toolNamePrefix)}
|
|
@@ -8674,7 +9372,7 @@ ${rules(toolNamePrefix)}
|
|
|
8674
9372
|
${objectives(toolNamePrefix)}
|
|
8675
9373
|
${systemInformation(info)}
|
|
8676
9374
|
${customInstructions(instructions)}
|
|
8677
|
-
${
|
|
9375
|
+
${customScripts(scripts)}
|
|
8678
9376
|
${interactiveMode(interactive)}
|
|
8679
9377
|
`;
|
|
8680
9378
|
|
|
@@ -8686,14 +9384,162 @@ class CoderAgent extends AgentBase {
|
|
|
8686
9384
|
const toolNamePrefix = "tool_";
|
|
8687
9385
|
const systemPrompt = fullSystemPrompt({
|
|
8688
9386
|
os: options.os
|
|
8689
|
-
}, tools, toolNamePrefix, options.customInstructions ?? [], options.
|
|
8690
|
-
super(options.ai, {
|
|
9387
|
+
}, tools, toolNamePrefix, options.customInstructions ?? [], options.scripts ?? {}, options.interactive);
|
|
9388
|
+
super(coderAgentInfo.name, options.ai, {
|
|
9389
|
+
systemPrompt,
|
|
9390
|
+
tools,
|
|
9391
|
+
toolNamePrefix,
|
|
9392
|
+
provider: options.provider,
|
|
9393
|
+
interactive: options.interactive,
|
|
9394
|
+
agents: options.agents
|
|
9395
|
+
});
|
|
9396
|
+
}
|
|
9397
|
+
}
|
|
9398
|
+
var coderAgentInfo = {
|
|
9399
|
+
name: "Coder",
|
|
9400
|
+
responsibilities: [
|
|
9401
|
+
"Editing and refactoring existing code.",
|
|
9402
|
+
"Creating new features or modules.",
|
|
9403
|
+
"Running tests and analyzing test results.",
|
|
9404
|
+
"Maintaining coding standards, lint rules, and general code quality."
|
|
9405
|
+
]
|
|
9406
|
+
};
|
|
9407
|
+
// src/Agent/ArchitectAgent/prompts.ts
|
|
9408
|
+
var fullSystemPrompt2 = (info, tools, toolNamePrefix, instructions, scripts, interactive) => `
|
|
9409
|
+
# Architect Agent
|
|
9410
|
+
|
|
9411
|
+
## Role
|
|
9412
|
+
You are the **Architect** agent, responsible for:
|
|
9413
|
+
1. **Task Analysis** – Understand requirements.
|
|
9414
|
+
2. **File Identification** – Find and select relevant files.
|
|
9415
|
+
3. **File Reading** – Use the provided tools to gather information from these files.
|
|
9416
|
+
4. **Implementation Plan** – Draft a concise plan detailing steps, resources, and dependencies.
|
|
9417
|
+
5. **Review & Improve** – Evaluate and refine the plan.
|
|
9418
|
+
6. **Handover** – Provide the final plan, context, and files to the **Coder** agent.
|
|
9419
|
+
|
|
9420
|
+
> **Note**: The **Architect** agent must not make any direct modifications. Your role is limited to creating the implementation plan and handing it over to the **Coder** agent, who will perform any actual changes.
|
|
9421
|
+
|
|
9422
|
+
## Rules
|
|
9423
|
+
1. **Consistency**: Maintain alignment with the user’s instructions and the system’s objectives at all times.
|
|
9424
|
+
2. **Relevance**: Only read and use files directly related to the task. Avoid unnecessary or tangential information.
|
|
9425
|
+
3. **Conciseness**: Keep all communications and plans succinct, avoiding superfluous or repetitive details.
|
|
9426
|
+
4. **Accuracy**: Ensure the information you gather and any conclusions you draw are correct and verifiable.
|
|
9427
|
+
5. **Clarity**: Present the final plan and any supporting details in a structured and easily understandable format.
|
|
9428
|
+
6. **Minimal Queries**: Ask clarifying questions only when essential, and avoid repeated questioning that does not add value.
|
|
9429
|
+
|
|
9430
|
+
## Steps
|
|
9431
|
+
1. **Analyze Task**
|
|
9432
|
+
- Gather and understand the user’s requirements.
|
|
9433
|
+
- Note any potential constraints or objectives that may influence the plan.
|
|
9434
|
+
|
|
9435
|
+
2. **Identify Relevant Files**
|
|
9436
|
+
- Determine which files or documents are necessary.
|
|
9437
|
+
- Justify why these files are relevant.
|
|
9438
|
+
|
|
9439
|
+
3. **Read Files via Tools**
|
|
9440
|
+
- Utilize the provided tools to access and extract information from the identified files.
|
|
9441
|
+
- Summarize key insights or data for the solution.
|
|
9442
|
+
|
|
9443
|
+
4. **Create Implementation Plan**
|
|
9444
|
+
- Outline tasks, define milestones, and detail resources or dependencies.
|
|
9445
|
+
- Provide clear, concise instructions for each step.
|
|
9446
|
+
|
|
9447
|
+
5. **Review & Improve**
|
|
9448
|
+
- Check the plan for consistency, clarity, and feasibility.
|
|
9449
|
+
- Make adjustments or refinements to ensure accuracy and efficiency.
|
|
9450
|
+
|
|
9451
|
+
6. **Handover**
|
|
9452
|
+
- Deliver the final implementation plan, context, and relevant files to the **Coder** agent.
|
|
9453
|
+
- Provide any additional instructions or clarifications needed for successful implementation.
|
|
9454
|
+
${toolUsePrompt(tools, toolNamePrefix)}
|
|
9455
|
+
${capabilities(toolNamePrefix)}
|
|
9456
|
+
${systemInformation(info)}
|
|
9457
|
+
${customInstructions(instructions)}
|
|
9458
|
+
${customScripts(scripts)}
|
|
9459
|
+
${interactiveMode(interactive)}
|
|
9460
|
+
`;
|
|
9461
|
+
|
|
9462
|
+
// src/Agent/ArchitectAgent/index.ts
|
|
9463
|
+
class ArchitectAgent extends AgentBase {
|
|
9464
|
+
constructor(options) {
|
|
9465
|
+
const agentTools = [
|
|
9466
|
+
...options.additionalTools ?? [],
|
|
9467
|
+
askFollowupQuestion_default,
|
|
9468
|
+
attemptCompletion_default,
|
|
9469
|
+
handOver_default,
|
|
9470
|
+
listCodeDefinitionNames_default,
|
|
9471
|
+
listFiles_default,
|
|
9472
|
+
readFile_default,
|
|
9473
|
+
searchFiles_default
|
|
9474
|
+
];
|
|
9475
|
+
const tools = getAvailableTools(options.provider, agentTools);
|
|
9476
|
+
const toolNamePrefix = "tool_";
|
|
9477
|
+
const systemPrompt = fullSystemPrompt2({
|
|
9478
|
+
os: options.os
|
|
9479
|
+
}, tools, toolNamePrefix, options.customInstructions ?? [], options.scripts ?? {}, options.interactive);
|
|
9480
|
+
super(architectAgentInfo.name, options.ai, {
|
|
8691
9481
|
systemPrompt,
|
|
8692
9482
|
tools,
|
|
8693
9483
|
toolNamePrefix,
|
|
8694
9484
|
provider: options.provider,
|
|
8695
|
-
interactive: options.interactive
|
|
9485
|
+
interactive: options.interactive,
|
|
9486
|
+
agents: options.agents
|
|
9487
|
+
});
|
|
9488
|
+
}
|
|
9489
|
+
}
|
|
9490
|
+
var architectAgentInfo = {
|
|
9491
|
+
name: "Architect",
|
|
9492
|
+
responsibilities: [
|
|
9493
|
+
"Analyzing the user’s overall task and requirements.",
|
|
9494
|
+
"Creating plans and making higher-level decisions about system structure and design.",
|
|
9495
|
+
"Reviewing and analyzing existing code or components for maintainability and scalability.",
|
|
9496
|
+
"Laying out the roadmap for implementation."
|
|
9497
|
+
]
|
|
9498
|
+
};
|
|
9499
|
+
// src/Agent/MultiAgent.ts
|
|
9500
|
+
class MultiAgent {
|
|
9501
|
+
#config;
|
|
9502
|
+
#activeAgent = null;
|
|
9503
|
+
constructor(config) {
|
|
9504
|
+
this.#config = config;
|
|
9505
|
+
}
|
|
9506
|
+
get model() {
|
|
9507
|
+
return this.#activeAgent?.model;
|
|
9508
|
+
}
|
|
9509
|
+
async#startTask(agentName, task, context, callback) {
|
|
9510
|
+
this.#activeAgent = await this.#config.createAgent(agentName);
|
|
9511
|
+
const [exitReason, info] = await this.#activeAgent.startTask({
|
|
9512
|
+
task,
|
|
9513
|
+
context,
|
|
9514
|
+
callback
|
|
8696
9515
|
});
|
|
9516
|
+
if (typeof exitReason === "string") {
|
|
9517
|
+
return [exitReason, info];
|
|
9518
|
+
}
|
|
9519
|
+
if (exitReason.type === "HandOver") {
|
|
9520
|
+
const context2 = await this.#config.getContext(agentName, exitReason.context, exitReason.files);
|
|
9521
|
+
const [exitReason2, info2] = await this.#startTask(exitReason.agentName, exitReason.task, context2, callback);
|
|
9522
|
+
info2.inputTokens += info.inputTokens;
|
|
9523
|
+
info2.outputTokens += info.outputTokens;
|
|
9524
|
+
info2.cacheWriteTokens += info.cacheWriteTokens;
|
|
9525
|
+
info2.cacheReadTokens += info.cacheReadTokens;
|
|
9526
|
+
info2.totalCost = (info.totalCost ?? 0) + (info2.totalCost ?? 0);
|
|
9527
|
+
return [exitReason2, info2];
|
|
9528
|
+
}
|
|
9529
|
+
return [exitReason, info];
|
|
9530
|
+
}
|
|
9531
|
+
async startTask(options) {
|
|
9532
|
+
if (this.#activeAgent) {
|
|
9533
|
+
throw new Error("An active agent already exists");
|
|
9534
|
+
}
|
|
9535
|
+
return this.#startTask(options.agentName, options.task, options.context, options.callback);
|
|
9536
|
+
}
|
|
9537
|
+
async continueTask(userMessage, taskInfo, callback = () => {
|
|
9538
|
+
}) {
|
|
9539
|
+
if (!this.#activeAgent) {
|
|
9540
|
+
throw new Error("No active agent");
|
|
9541
|
+
}
|
|
9542
|
+
return this.#activeAgent.continueTask(userMessage, taskInfo, callback);
|
|
8697
9543
|
}
|
|
8698
9544
|
}
|
|
8699
9545
|
// src/AiTool/generateGitCommitMessage.ts
|
|
@@ -8756,6 +9602,97 @@ ${output}`);
|
|
|
8756
9602
|
}
|
|
8757
9603
|
};
|
|
8758
9604
|
|
|
9605
|
+
// src/AiTool/generateGithubPullRequestDetails.ts
|
|
9606
|
+
var prompt2 = `
|
|
9607
|
+
You are given:
|
|
9608
|
+
- A branch name in <tool_input_branch_name>.
|
|
9609
|
+
- An optional context message in <tool_input_context> (which may or may not be present).
|
|
9610
|
+
- All commit messages combined in <tool_input_commit_messages>.
|
|
9611
|
+
- All diffs combined in <tool_input_commit_diff>.
|
|
9612
|
+
|
|
9613
|
+
Your task:
|
|
9614
|
+
1. Consider the optional context (if provided).
|
|
9615
|
+
2. Analyze the combined commit messages and diffs.
|
|
9616
|
+
3. Produce a single GitHub Pull Request title.
|
|
9617
|
+
4. Produce a Pull Request description that explains the changes.
|
|
9618
|
+
|
|
9619
|
+
Output format:
|
|
9620
|
+
<tool_output>
|
|
9621
|
+
<tool_output_pr_title>YOUR PR TITLE HERE</tool_output_pr_title>
|
|
9622
|
+
<tool_output_pr_description>YOUR PR DESCRIPTION HERE</tool_output_pr_description>
|
|
9623
|
+
</tool_output>
|
|
9624
|
+
|
|
9625
|
+
Below is an **example** of the input and output:
|
|
9626
|
+
|
|
9627
|
+
Example Input:
|
|
9628
|
+
<tool_input>
|
|
9629
|
+
<tool_input_branch_name>feature/refactor-logging</tool_input_branch_name>
|
|
9630
|
+
<tool_input_context>Focus on clean code and maintainability</tool_input_context>
|
|
9631
|
+
<tool_input_commit_messages>
|
|
9632
|
+
Remove debug logs
|
|
9633
|
+
Refactor order validation logic
|
|
9634
|
+
</tool_input_commit_messages>
|
|
9635
|
+
<tool_input_commit_diff>
|
|
9636
|
+
diff --git a/user_service.py b/user_service.py
|
|
9637
|
+
- print("Debug info")
|
|
9638
|
+
+ # Removed debug print statements
|
|
9639
|
+
|
|
9640
|
+
diff --git a/order_service.py b/order_service.py
|
|
9641
|
+
- if is_valid_order(order):
|
|
9642
|
+
- process_order(order)
|
|
9643
|
+
+ validate_and_process(order)
|
|
9644
|
+
</tool_input_commit_diff>
|
|
9645
|
+
</tool_input>
|
|
9646
|
+
|
|
9647
|
+
Example Output:
|
|
9648
|
+
<tool_output>
|
|
9649
|
+
<tool_output_pr_title>Refactor Order Validation and Remove Debug Logs</tool_output_pr_title>
|
|
9650
|
+
<tool_output_pr_description>
|
|
9651
|
+
This PR removes unnecessary debug print statements and updates order validation
|
|
9652
|
+
to use the new validate_and_process method for improved maintainability.
|
|
9653
|
+
</tool_output_pr_description>
|
|
9654
|
+
</tool_output>
|
|
9655
|
+
|
|
9656
|
+
---
|
|
9657
|
+
|
|
9658
|
+
Use the above format whenever you receive \`<tool_input>\` that may include a branch name, an optional context, aggregated commit messages in a single tag, and a combined diff in a single tag. Provide your final output strictly in \`<tool_output>\` with \`<tool_output_pr_title>\` and \`<tool_output_pr_description>\`.
|
|
9659
|
+
Only highlight the changed code and avoid including the context around the changes in the description.
|
|
9660
|
+
`;
|
|
9661
|
+
var generateGithubPullRequestDetails_default = {
|
|
9662
|
+
name: "generateGithubPullRequestDetails",
|
|
9663
|
+
description: "Generates a GitHub pull request title and description from git commits",
|
|
9664
|
+
prompt: prompt2,
|
|
9665
|
+
formatInput: (params) => {
|
|
9666
|
+
return `<tool_input>
|
|
9667
|
+
<tool_input_branch_name>${params.branchName}</tool_input_branch_name>${params.context ? `
|
|
9668
|
+
<tool_input_context>${params.context}</tool_input_context>` : ""}
|
|
9669
|
+
<tool_input_commit_messages>${params.commitMessages}</tool_input_commit_messages>
|
|
9670
|
+
<tool_input_commit_diff>${params.commitDiff}</tool_input_commit_diff>
|
|
9671
|
+
</tool_input>`;
|
|
9672
|
+
},
|
|
9673
|
+
parseOutput: (output) => {
|
|
9674
|
+
const regex = /<tool_output>([\s\S]*)<\/tool_output>/gm;
|
|
9675
|
+
const match = regex.exec(output);
|
|
9676
|
+
if (!match) {
|
|
9677
|
+
throw new Error(`Could not parse output:
|
|
9678
|
+
${output}`);
|
|
9679
|
+
}
|
|
9680
|
+
const [, outputContent] = match;
|
|
9681
|
+
const titleRegex = /<tool_output_pr_title>([\s\S]*)<\/tool_output_pr_title>/gm;
|
|
9682
|
+
const titleMatch = titleRegex.exec(outputContent);
|
|
9683
|
+
const descriptionRegex = /<tool_output_pr_description>([\s\S]*)<\/tool_output_pr_description>/gm;
|
|
9684
|
+
const descriptionMatch = descriptionRegex.exec(outputContent);
|
|
9685
|
+
if (titleMatch && descriptionMatch) {
|
|
9686
|
+
return {
|
|
9687
|
+
title: titleMatch[1],
|
|
9688
|
+
description: descriptionMatch[1]
|
|
9689
|
+
};
|
|
9690
|
+
}
|
|
9691
|
+
throw new Error(`Could not parse output:
|
|
9692
|
+
${output}`);
|
|
9693
|
+
}
|
|
9694
|
+
};
|
|
9695
|
+
|
|
8759
9696
|
// src/AiTool/index.ts
|
|
8760
9697
|
var executeTool = async (definition, ai, params) => {
|
|
8761
9698
|
const { response, usage } = await ai.request(definition.prompt, [{ role: "user", content: definition.formatInput(params) }]);
|
|
@@ -8770,6 +9707,7 @@ var makeTool = (definition) => {
|
|
|
8770
9707
|
};
|
|
8771
9708
|
};
|
|
8772
9709
|
var generateGitCommitMessage = makeTool(generateGitCommitMessage_default);
|
|
9710
|
+
var generateGithubPullRequestDetails = makeTool(generateGithubPullRequestDetails_default);
|
|
8773
9711
|
export {
|
|
8774
9712
|
writeToFile_default as writeToFile,
|
|
8775
9713
|
searchFiles_default as searchFiles,
|
|
@@ -8779,6 +9717,9 @@ export {
|
|
|
8779
9717
|
makeTool,
|
|
8780
9718
|
listFiles_default as listFiles,
|
|
8781
9719
|
listCodeDefinitionNames_default as listCodeDefinitionNames,
|
|
9720
|
+
handOver_default as handOver,
|
|
9721
|
+
getAvailableTools,
|
|
9722
|
+
generateGithubPullRequestDetails,
|
|
8782
9723
|
generateGitCommitMessage,
|
|
8783
9724
|
executeTool,
|
|
8784
9725
|
executeCommand_default as executeCommand,
|
|
@@ -8786,14 +9727,19 @@ export {
|
|
|
8786
9727
|
deepSeekModels,
|
|
8787
9728
|
deepSeekDefaultModelId,
|
|
8788
9729
|
createService,
|
|
9730
|
+
coderAgentInfo,
|
|
8789
9731
|
attemptCompletion_default as attemptCompletion,
|
|
8790
9732
|
askFollowupQuestion_default as askFollowupQuestion,
|
|
9733
|
+
architectAgentInfo,
|
|
8791
9734
|
anthropicModels,
|
|
8792
9735
|
anthropicDefaultModelId,
|
|
8793
9736
|
exports_allTools as allTools,
|
|
9737
|
+
ToolResponseType,
|
|
9738
|
+
TaskEventKind,
|
|
9739
|
+
MultiAgent,
|
|
8794
9740
|
MockProvider,
|
|
8795
|
-
ExitReason,
|
|
8796
9741
|
CoderAgent,
|
|
9742
|
+
ArchitectAgent,
|
|
8797
9743
|
AiServiceProvider,
|
|
8798
9744
|
AgentBase
|
|
8799
9745
|
};
|