@dxos/assistant 0.7.5-labs.5f04cf6 → 0.7.5-labs.8a82073
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/lib/browser/{chunk-37SDA65J.mjs → chunk-AXUUNA7O.mjs} +2 -2
- package/dist/lib/browser/{chunk-37SDA65J.mjs.map → chunk-AXUUNA7O.mjs.map} +3 -3
- package/dist/lib/browser/index.mjs +600 -324
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +2 -2
- package/dist/lib/browser/testing/index.mjs.map +2 -2
- package/dist/lib/node/{chunk-UYNIAO5N.cjs → chunk-QPHGQINW.cjs} +5 -5
- package/dist/lib/node/{chunk-UYNIAO5N.cjs.map → chunk-QPHGQINW.cjs.map} +3 -3
- package/dist/lib/node/index.cjs +607 -331
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +8 -8
- package/dist/lib/node/testing/index.cjs.map +2 -2
- package/dist/lib/node-esm/{chunk-AR24K7TY.mjs → chunk-GOVFOBIB.mjs} +2 -2
- package/dist/lib/node-esm/{chunk-AR24K7TY.mjs.map → chunk-GOVFOBIB.mjs.map} +3 -3
- package/dist/lib/node-esm/index.mjs +600 -324
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +2 -2
- package/dist/lib/node-esm/testing/index.mjs.map +2 -2
- package/dist/types/src/ai-service/client.d.ts +2 -2
- package/dist/types/src/ai-service/client.d.ts.map +1 -1
- package/dist/types/src/ai-service/index.d.ts +2 -1
- package/dist/types/src/ai-service/index.d.ts.map +1 -1
- package/dist/types/src/ai-service/parser.d.ts +128 -32
- package/dist/types/src/ai-service/parser.d.ts.map +1 -1
- package/dist/types/src/ai-service/stream.d.ts +23 -19
- package/dist/types/src/ai-service/stream.d.ts.map +1 -1
- package/dist/types/src/ai-service/stream.test.d.ts +1 -1
- package/dist/types/src/ai-service/stream.test.d.ts.map +1 -1
- package/dist/types/src/ai-service/testing.d.ts +7 -0
- package/dist/types/src/ai-service/testing.d.ts.map +1 -0
- package/dist/types/src/ai-service/transform.d.ts +27 -0
- package/dist/types/src/ai-service/transform.d.ts.map +1 -0
- package/dist/types/src/ai-service/{defs.d.ts → types.d.ts} +40 -24
- package/dist/types/src/ai-service/types.d.ts.map +1 -0
- package/dist/types/src/conversation/conversation.d.ts +14 -10
- package/dist/types/src/conversation/conversation.d.ts.map +1 -1
- package/dist/types/src/cypher/schema.d.ts.map +1 -1
- package/dist/types/src/testing/test-data.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +15 -13
- package/src/ai-service/client.test.ts +49 -34
- package/src/ai-service/client.ts +4 -13
- package/src/ai-service/index.ts +2 -1
- package/src/ai-service/parser.ts +295 -140
- package/src/ai-service/stream.test.ts +84 -89
- package/src/ai-service/stream.ts +61 -127
- package/src/ai-service/testing.ts +93 -0
- package/src/ai-service/transform.ts +121 -0
- package/src/ai-service/{defs.ts → types.ts} +21 -12
- package/src/bin/chat.ts +2 -1
- package/src/conversation/conversation.test.ts +5 -2
- package/src/conversation/conversation.ts +19 -8
- package/src/cypher/schema.ts +6 -1
- package/src/testing/test-data.ts +7 -1
- package/dist/types/src/ai-service/defs.d.ts.map +0 -1
- package/src/ai-service/stream.test.txt +0 -33
|
@@ -7,16 +7,15 @@ import {
|
|
|
7
7
|
formatNodeLabel,
|
|
8
8
|
isReference,
|
|
9
9
|
parseCypherQuery
|
|
10
|
-
} from "./chunk-
|
|
10
|
+
} from "./chunk-AXUUNA7O.mjs";
|
|
11
11
|
|
|
12
12
|
// packages/experimental/assistant/src/ai-service/client.ts
|
|
13
13
|
import { Schema as S } from "@effect/schema";
|
|
14
14
|
import { Message } from "@dxos/artifact";
|
|
15
|
-
import { invariant
|
|
15
|
+
import { invariant } from "@dxos/invariant";
|
|
16
16
|
|
|
17
17
|
// packages/experimental/assistant/src/ai-service/stream.ts
|
|
18
18
|
import { Trigger } from "@dxos/async";
|
|
19
|
-
import { invariant } from "@dxos/invariant";
|
|
20
19
|
import { log } from "@dxos/log";
|
|
21
20
|
|
|
22
21
|
// packages/experimental/assistant/src/ai-service/util/sse.ts
|
|
@@ -258,75 +257,64 @@ var LineDecoder = class _LineDecoder {
|
|
|
258
257
|
|
|
259
258
|
// packages/experimental/assistant/src/ai-service/stream.ts
|
|
260
259
|
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/ai-service/stream.ts";
|
|
261
|
-
var
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
S: this,
|
|
279
|
-
C: (f, a) => f(...a)
|
|
280
|
-
});
|
|
281
|
-
throw err;
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
if (sse.event === "message_start" || sse.event === "message_delta" || sse.event === "message_stop" || sse.event === "content_block_start" || sse.event === "content_block_delta" || sse.event === "content_block_stop") {
|
|
285
|
-
try {
|
|
286
|
-
yield JSON.parse(sse.data);
|
|
287
|
-
} catch (err) {
|
|
288
|
-
log.error("could not parse message into JSON:", {
|
|
289
|
-
data: sse.data,
|
|
290
|
-
raw: sse.raw
|
|
291
|
-
}, {
|
|
292
|
-
F: __dxlog_file,
|
|
293
|
-
L: 47,
|
|
294
|
-
S: this,
|
|
295
|
-
C: (f, a) => f(...a)
|
|
296
|
-
});
|
|
297
|
-
throw err;
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
if (sse.event === "ping") {
|
|
301
|
-
continue;
|
|
260
|
+
var createGenerationStream = (response, controller = new AbortController()) => {
|
|
261
|
+
const iterator = async function* () {
|
|
262
|
+
for await (const sse of iterSSEMessages(response, controller)) {
|
|
263
|
+
if (sse.event === "completion") {
|
|
264
|
+
try {
|
|
265
|
+
yield JSON.parse(sse.data);
|
|
266
|
+
} catch (err) {
|
|
267
|
+
log.error("could not parse message into JSON:", {
|
|
268
|
+
data: sse.data,
|
|
269
|
+
raw: sse.raw
|
|
270
|
+
}, {
|
|
271
|
+
F: __dxlog_file,
|
|
272
|
+
L: 21,
|
|
273
|
+
S: this,
|
|
274
|
+
C: (f, a) => f(...a)
|
|
275
|
+
});
|
|
276
|
+
throw err;
|
|
302
277
|
}
|
|
303
|
-
|
|
304
|
-
|
|
278
|
+
}
|
|
279
|
+
if (sse.event === "message_start" || sse.event === "message_delta" || sse.event === "message_stop" || sse.event === "content_block_start" || sse.event === "content_block_delta" || sse.event === "content_block_stop") {
|
|
280
|
+
try {
|
|
281
|
+
yield JSON.parse(sse.data);
|
|
282
|
+
} catch (err) {
|
|
283
|
+
log.error("could not parse message into JSON:", {
|
|
284
|
+
data: sse.data,
|
|
285
|
+
raw: sse.raw
|
|
286
|
+
}, {
|
|
287
|
+
F: __dxlog_file,
|
|
288
|
+
L: 37,
|
|
289
|
+
S: this,
|
|
290
|
+
C: (f, a) => f(...a)
|
|
291
|
+
});
|
|
292
|
+
throw err;
|
|
305
293
|
}
|
|
306
294
|
}
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
295
|
+
if (sse.event === "ping") {
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
if (sse.event === "error") {
|
|
299
|
+
throw new Error(`Message generation error: ${sse.data}`);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
return new GenerationStream(controller, iterator);
|
|
304
|
+
};
|
|
305
|
+
var GenerationStream = class {
|
|
306
|
+
constructor(_controller, _getIterator) {
|
|
311
307
|
this._controller = _controller;
|
|
312
308
|
this._getIterator = _getIterator;
|
|
313
|
-
this._params = _params;
|
|
314
309
|
this._done = new Trigger();
|
|
315
|
-
this._previousMessages = [];
|
|
316
|
-
this._accumulatedMessage = void 0;
|
|
317
310
|
this._iterator = void 0;
|
|
318
311
|
}
|
|
312
|
+
/**
|
|
313
|
+
* Returns an iterator over the stream.
|
|
314
|
+
*/
|
|
319
315
|
[Symbol.asyncIterator]() {
|
|
320
316
|
return this._createIterator();
|
|
321
317
|
}
|
|
322
|
-
get accumulatedMessages() {
|
|
323
|
-
return [
|
|
324
|
-
...this._previousMessages,
|
|
325
|
-
...this._accumulatedMessage == null ? [] : [
|
|
326
|
-
this._accumulatedMessage
|
|
327
|
-
]
|
|
328
|
-
];
|
|
329
|
-
}
|
|
330
318
|
/**
|
|
331
319
|
* Aborts the stream.
|
|
332
320
|
*/
|
|
@@ -338,15 +326,16 @@ var GenerationStream = class _GenerationStream {
|
|
|
338
326
|
*/
|
|
339
327
|
async complete() {
|
|
340
328
|
await this._done.wait();
|
|
341
|
-
return this.accumulatedMessages;
|
|
342
329
|
}
|
|
330
|
+
/**
|
|
331
|
+
* Creates an iterator over the stream.
|
|
332
|
+
*/
|
|
343
333
|
_createIterator() {
|
|
344
334
|
const self = this;
|
|
345
335
|
return this._iterator ??= (() => {
|
|
346
336
|
const generator = async function* () {
|
|
347
337
|
try {
|
|
348
338
|
for await (const event of self._getIterator()) {
|
|
349
|
-
self._processEvent(event);
|
|
350
339
|
yield event;
|
|
351
340
|
}
|
|
352
341
|
this._done.wake();
|
|
@@ -357,89 +346,13 @@ var GenerationStream = class _GenerationStream {
|
|
|
357
346
|
return generator.call(this);
|
|
358
347
|
})();
|
|
359
348
|
}
|
|
360
|
-
_processEvent(event) {
|
|
361
|
-
switch (event.type) {
|
|
362
|
-
case "message_start": {
|
|
363
|
-
if (this._accumulatedMessage) {
|
|
364
|
-
this._previousMessages.push(this._accumulatedMessage);
|
|
365
|
-
this._accumulatedMessage = void 0;
|
|
366
|
-
}
|
|
367
|
-
this._accumulatedMessage = {
|
|
368
|
-
...event.message,
|
|
369
|
-
threadId: this._params.threadId,
|
|
370
|
-
spaceId: this._params.spaceId
|
|
371
|
-
};
|
|
372
|
-
break;
|
|
373
|
-
}
|
|
374
|
-
case "message_delta": {
|
|
375
|
-
if (this._accumulatedMessage === void 0) {
|
|
376
|
-
throw new Error("Received message delta without a message start");
|
|
377
|
-
}
|
|
378
|
-
break;
|
|
379
|
-
}
|
|
380
|
-
case "message_stop": {
|
|
381
|
-
if (this._accumulatedMessage === void 0) {
|
|
382
|
-
throw new Error("Received message stop without a message start");
|
|
383
|
-
}
|
|
384
|
-
break;
|
|
385
|
-
}
|
|
386
|
-
case "content_block_start": {
|
|
387
|
-
invariant(this._accumulatedMessage, void 0, {
|
|
388
|
-
F: __dxlog_file,
|
|
389
|
-
L: 149,
|
|
390
|
-
S: this,
|
|
391
|
-
A: [
|
|
392
|
-
"this._accumulatedMessage",
|
|
393
|
-
""
|
|
394
|
-
]
|
|
395
|
-
});
|
|
396
|
-
this._accumulatedMessage.content.push(event.content);
|
|
397
|
-
break;
|
|
398
|
-
}
|
|
399
|
-
case "content_block_delta": {
|
|
400
|
-
invariant(this._accumulatedMessage, void 0, {
|
|
401
|
-
F: __dxlog_file,
|
|
402
|
-
L: 155,
|
|
403
|
-
S: this,
|
|
404
|
-
A: [
|
|
405
|
-
"this._accumulatedMessage",
|
|
406
|
-
""
|
|
407
|
-
]
|
|
408
|
-
});
|
|
409
|
-
const snapshotContent = this._accumulatedMessage.content.at(event.index);
|
|
410
|
-
if (snapshotContent?.type === "text" && event.delta.type === "text_delta") {
|
|
411
|
-
snapshotContent.text += event.delta.text;
|
|
412
|
-
} else if (snapshotContent?.type === "tool_use" && event.delta.type === "input_json_delta") {
|
|
413
|
-
snapshotContent.inputJson ??= "";
|
|
414
|
-
snapshotContent.inputJson += event.delta.partial_json;
|
|
415
|
-
}
|
|
416
|
-
break;
|
|
417
|
-
}
|
|
418
|
-
case "content_block_stop": {
|
|
419
|
-
invariant(this._accumulatedMessage, void 0, {
|
|
420
|
-
F: __dxlog_file,
|
|
421
|
-
L: 172,
|
|
422
|
-
S: this,
|
|
423
|
-
A: [
|
|
424
|
-
"this._accumulatedMessage",
|
|
425
|
-
""
|
|
426
|
-
]
|
|
427
|
-
});
|
|
428
|
-
const lastBlock = this._accumulatedMessage.content.at(-1);
|
|
429
|
-
if (lastBlock?.type === "tool_use") {
|
|
430
|
-
lastBlock.input = lastBlock.inputJson == null || lastBlock.inputJson === "" ? {} : JSON.parse(lastBlock.inputJson ?? "");
|
|
431
|
-
}
|
|
432
|
-
break;
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
349
|
};
|
|
437
350
|
|
|
438
351
|
// packages/experimental/assistant/src/ai-service/client.ts
|
|
439
352
|
var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/ai-service/client.ts";
|
|
440
353
|
var AIServiceClientImpl = class {
|
|
441
354
|
constructor({ endpoint }) {
|
|
442
|
-
|
|
355
|
+
invariant(endpoint, "endpoint is required", {
|
|
443
356
|
F: __dxlog_file2,
|
|
444
357
|
L: 36,
|
|
445
358
|
S: this,
|
|
@@ -484,37 +397,492 @@ var AIServiceClientImpl = class {
|
|
|
484
397
|
*/
|
|
485
398
|
async generate(request) {
|
|
486
399
|
const controller = new AbortController();
|
|
487
|
-
const signal = controller.signal;
|
|
488
400
|
const response = await fetch(`${this._endpoint}/generate`, {
|
|
401
|
+
signal: controller.signal,
|
|
489
402
|
method: "POST",
|
|
490
403
|
headers: {
|
|
491
404
|
"Content-Type": "application/json"
|
|
492
405
|
},
|
|
493
|
-
body: JSON.stringify(request)
|
|
494
|
-
signal
|
|
406
|
+
body: JSON.stringify(request)
|
|
495
407
|
});
|
|
496
|
-
|
|
408
|
+
invariant(response.body instanceof ReadableStream, void 0, {
|
|
497
409
|
F: __dxlog_file2,
|
|
498
|
-
L:
|
|
410
|
+
L: 85,
|
|
499
411
|
S: this,
|
|
500
412
|
A: [
|
|
501
413
|
"response.body instanceof ReadableStream",
|
|
502
414
|
""
|
|
503
415
|
]
|
|
504
416
|
});
|
|
505
|
-
return
|
|
506
|
-
spaceId: request.spaceId,
|
|
507
|
-
threadId: request.threadId
|
|
508
|
-
}, response, controller);
|
|
417
|
+
return createGenerationStream(response, controller);
|
|
509
418
|
}
|
|
510
419
|
};
|
|
511
420
|
|
|
512
|
-
// packages/experimental/assistant/src/ai-service/
|
|
421
|
+
// packages/experimental/assistant/src/ai-service/parser.ts
|
|
422
|
+
import { Message as Message2 } from "@dxos/artifact";
|
|
423
|
+
import { Event } from "@dxos/async";
|
|
424
|
+
import { createStatic } from "@dxos/echo-schema";
|
|
425
|
+
import { invariant as invariant2 } from "@dxos/invariant";
|
|
426
|
+
import { log as log2 } from "@dxos/log";
|
|
427
|
+
import { isNotFalsy, safeParseJson } from "@dxos/util";
|
|
428
|
+
|
|
429
|
+
// packages/experimental/assistant/src/ai-service/transform.ts
|
|
430
|
+
import * as P from "parsimmon";
|
|
431
|
+
var textChunk = P.regexp(/[^<]+/).map((content) => ({
|
|
432
|
+
type: "text",
|
|
433
|
+
content
|
|
434
|
+
}));
|
|
435
|
+
var attribute = P.seq(P.regexp(/[a-zA-Z_][a-zA-Z0-9_-]*/), P.string("=").trim(P.optWhitespace), P.alt(P.regexp(/"[^"]*"/).map((s) => s.slice(1, -1)), P.regexp(/'[^']*'/).map((s) => s.slice(1, -1)))).map(([name, _, value]) => [
|
|
436
|
+
name,
|
|
437
|
+
value
|
|
438
|
+
]);
|
|
439
|
+
var tagName = P.regexp(/[a-zA-Z_][a-zA-Z0-9_-]*/);
|
|
440
|
+
var selfClosingTag = P.seqMap(
|
|
441
|
+
//
|
|
442
|
+
P.string("<"),
|
|
443
|
+
tagName,
|
|
444
|
+
P.optWhitespace.then(attribute).many(),
|
|
445
|
+
P.optWhitespace.then(P.string("/>")),
|
|
446
|
+
P.optWhitespace,
|
|
447
|
+
(_, tag, attributes, __) => ({
|
|
448
|
+
type: "tag",
|
|
449
|
+
tag,
|
|
450
|
+
attributes: Object.fromEntries(attributes),
|
|
451
|
+
content: [],
|
|
452
|
+
selfClosing: true
|
|
453
|
+
})
|
|
454
|
+
);
|
|
455
|
+
var openTag = P.seqMap(
|
|
456
|
+
//
|
|
457
|
+
P.string("<"),
|
|
458
|
+
tagName,
|
|
459
|
+
P.optWhitespace.then(attribute).many(),
|
|
460
|
+
P.string(">"),
|
|
461
|
+
P.regexp(/[^<]*/),
|
|
462
|
+
(_, tag, attributes, __, content) => ({
|
|
463
|
+
type: "tag",
|
|
464
|
+
tag,
|
|
465
|
+
attributes: Object.fromEntries(attributes),
|
|
466
|
+
content: content.length ? [
|
|
467
|
+
{
|
|
468
|
+
type: "text",
|
|
469
|
+
content
|
|
470
|
+
}
|
|
471
|
+
] : []
|
|
472
|
+
})
|
|
473
|
+
);
|
|
474
|
+
var closeTag = P.seqMap(
|
|
475
|
+
//
|
|
476
|
+
P.string("</"),
|
|
477
|
+
tagName,
|
|
478
|
+
P.string(">"),
|
|
479
|
+
P.optWhitespace,
|
|
480
|
+
(_, tag) => ({
|
|
481
|
+
type: "tag",
|
|
482
|
+
tag,
|
|
483
|
+
content: [],
|
|
484
|
+
closing: true
|
|
485
|
+
})
|
|
486
|
+
);
|
|
487
|
+
var mixedChunk = P.alt(selfClosingTag, openTag, closeTag, textChunk);
|
|
488
|
+
var StreamTransform = class {
|
|
489
|
+
constructor() {
|
|
490
|
+
this._buffer = "";
|
|
491
|
+
}
|
|
492
|
+
transform(chunk) {
|
|
493
|
+
this._buffer += chunk;
|
|
494
|
+
const results = [];
|
|
495
|
+
const parser = mixedChunk.many().skip(P.optWhitespace);
|
|
496
|
+
const result = parser.parse(this._buffer);
|
|
497
|
+
if (result.status) {
|
|
498
|
+
for (const chunk2 of result.value) {
|
|
499
|
+
results.push(chunk2);
|
|
500
|
+
}
|
|
501
|
+
this._buffer = "";
|
|
502
|
+
}
|
|
503
|
+
return results;
|
|
504
|
+
}
|
|
505
|
+
// TODO(burdon): Process remaining buffer?
|
|
506
|
+
flush() {
|
|
507
|
+
const remaining = this._buffer;
|
|
508
|
+
this._buffer = "";
|
|
509
|
+
return remaining.length > 0 ? [
|
|
510
|
+
{
|
|
511
|
+
type: "text",
|
|
512
|
+
content: remaining
|
|
513
|
+
}
|
|
514
|
+
] : [];
|
|
515
|
+
}
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
// packages/experimental/assistant/src/ai-service/parser.ts
|
|
519
|
+
var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/ai-service/parser.ts";
|
|
520
|
+
var MixedStreamParser = class {
|
|
521
|
+
constructor() {
|
|
522
|
+
/**
|
|
523
|
+
* New message.
|
|
524
|
+
*/
|
|
525
|
+
this.message = new Event();
|
|
526
|
+
/**
|
|
527
|
+
* Complete block added to Message.
|
|
528
|
+
*/
|
|
529
|
+
this.block = new Event();
|
|
530
|
+
/**
|
|
531
|
+
* Update partial block (while streaming).
|
|
532
|
+
*/
|
|
533
|
+
this.update = new Event();
|
|
534
|
+
}
|
|
535
|
+
_emitBlock(contentBlock, streamBlock) {
|
|
536
|
+
const messageBlock = streamBlock ? mergeMessageBlock(contentBlock, streamBlock) : contentBlock;
|
|
537
|
+
if (messageBlock) {
|
|
538
|
+
if (messageBlock.type === "text" && messageBlock.text.length === 0) {
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
invariant2(this._message, void 0, {
|
|
542
|
+
F: __dxlog_file3,
|
|
543
|
+
L: 46,
|
|
544
|
+
S: this,
|
|
545
|
+
A: [
|
|
546
|
+
"this._message",
|
|
547
|
+
""
|
|
548
|
+
]
|
|
549
|
+
});
|
|
550
|
+
this._message.content.push(messageBlock);
|
|
551
|
+
this.block.emit(messageBlock);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
_emitUpdate(contentBlock, streamBlock) {
|
|
555
|
+
const messageBlock = mergeMessageBlock(contentBlock, streamBlock);
|
|
556
|
+
if (messageBlock) {
|
|
557
|
+
messageBlock.pending = true;
|
|
558
|
+
this.update.emit(messageBlock);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* Parse stream until end.
|
|
563
|
+
*/
|
|
564
|
+
async parse(stream) {
|
|
565
|
+
const transformer = new StreamTransform();
|
|
566
|
+
let contentBlock;
|
|
567
|
+
let streamBlock;
|
|
568
|
+
const stack = [];
|
|
569
|
+
for await (const event of stream) {
|
|
570
|
+
switch (event.type) {
|
|
571
|
+
//
|
|
572
|
+
// Messages.
|
|
573
|
+
//
|
|
574
|
+
case "message_start": {
|
|
575
|
+
if (this._message) {
|
|
576
|
+
log2.warn("unexpected message_start", void 0, {
|
|
577
|
+
F: __dxlog_file3,
|
|
578
|
+
L: 86,
|
|
579
|
+
S: this,
|
|
580
|
+
C: (f, a) => f(...a)
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
this._message = createStatic(Message2, {
|
|
584
|
+
role: "assistant",
|
|
585
|
+
content: []
|
|
586
|
+
});
|
|
587
|
+
this.message.emit(this._message);
|
|
588
|
+
break;
|
|
589
|
+
}
|
|
590
|
+
case "message_delta": {
|
|
591
|
+
if (!this._message) {
|
|
592
|
+
log2.warn("unexpected message_delta", void 0, {
|
|
593
|
+
F: __dxlog_file3,
|
|
594
|
+
L: 96,
|
|
595
|
+
S: this,
|
|
596
|
+
C: (f, a) => f(...a)
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
break;
|
|
600
|
+
}
|
|
601
|
+
case "message_stop": {
|
|
602
|
+
if (!this._message) {
|
|
603
|
+
log2.warn("unexpected message_stop", void 0, {
|
|
604
|
+
F: __dxlog_file3,
|
|
605
|
+
L: 104,
|
|
606
|
+
S: this,
|
|
607
|
+
C: (f, a) => f(...a)
|
|
608
|
+
});
|
|
609
|
+
} else {
|
|
610
|
+
this._message = void 0;
|
|
611
|
+
}
|
|
612
|
+
break;
|
|
613
|
+
}
|
|
614
|
+
//
|
|
615
|
+
// Content blocks.
|
|
616
|
+
//
|
|
617
|
+
case "content_block_start": {
|
|
618
|
+
if (contentBlock) {
|
|
619
|
+
log2.warn("unexpected content_block_start", {
|
|
620
|
+
content: contentBlock
|
|
621
|
+
}, {
|
|
622
|
+
F: __dxlog_file3,
|
|
623
|
+
L: 117,
|
|
624
|
+
S: this,
|
|
625
|
+
C: (f, a) => f(...a)
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
contentBlock = event.content;
|
|
629
|
+
break;
|
|
630
|
+
}
|
|
631
|
+
case "content_block_delta": {
|
|
632
|
+
if (!contentBlock) {
|
|
633
|
+
log2.warn("unexpected content_block_delta", void 0, {
|
|
634
|
+
F: __dxlog_file3,
|
|
635
|
+
L: 127,
|
|
636
|
+
S: this,
|
|
637
|
+
C: (f, a) => f(...a)
|
|
638
|
+
});
|
|
639
|
+
break;
|
|
640
|
+
}
|
|
641
|
+
switch (event.delta.type) {
|
|
642
|
+
//
|
|
643
|
+
// Text
|
|
644
|
+
//
|
|
645
|
+
case "text_delta": {
|
|
646
|
+
const chunks = transformer.transform(event.delta.text);
|
|
647
|
+
for (const chunk of chunks) {
|
|
648
|
+
switch (streamBlock?.type) {
|
|
649
|
+
//
|
|
650
|
+
// XML Fragment.
|
|
651
|
+
//
|
|
652
|
+
case "tag": {
|
|
653
|
+
if (chunk.type === "tag") {
|
|
654
|
+
if (chunk.selfClosing) {
|
|
655
|
+
streamBlock.content.push(chunk);
|
|
656
|
+
} else if (chunk.closing) {
|
|
657
|
+
if (stack.length > 0) {
|
|
658
|
+
const top = stack.pop();
|
|
659
|
+
invariant2(top && top.type === "tag", void 0, {
|
|
660
|
+
F: __dxlog_file3,
|
|
661
|
+
L: 151,
|
|
662
|
+
S: this,
|
|
663
|
+
A: [
|
|
664
|
+
"top && top.type === 'tag'",
|
|
665
|
+
""
|
|
666
|
+
]
|
|
667
|
+
});
|
|
668
|
+
top.content.push(streamBlock);
|
|
669
|
+
streamBlock = top;
|
|
670
|
+
} else {
|
|
671
|
+
this._emitBlock(contentBlock, streamBlock);
|
|
672
|
+
streamBlock = void 0;
|
|
673
|
+
}
|
|
674
|
+
} else {
|
|
675
|
+
stack.push(streamBlock);
|
|
676
|
+
streamBlock = chunk;
|
|
677
|
+
}
|
|
678
|
+
} else {
|
|
679
|
+
if (streamBlock.content.length === 0) {
|
|
680
|
+
streamBlock.content.push(chunk);
|
|
681
|
+
} else {
|
|
682
|
+
const last = streamBlock.content.at(-1);
|
|
683
|
+
invariant2(last, void 0, {
|
|
684
|
+
F: __dxlog_file3,
|
|
685
|
+
L: 168,
|
|
686
|
+
S: this,
|
|
687
|
+
A: [
|
|
688
|
+
"last",
|
|
689
|
+
""
|
|
690
|
+
]
|
|
691
|
+
});
|
|
692
|
+
if (last.type === "text") {
|
|
693
|
+
last.content += chunk.content;
|
|
694
|
+
} else {
|
|
695
|
+
streamBlock.content.push(chunk);
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
break;
|
|
700
|
+
}
|
|
701
|
+
//
|
|
702
|
+
// Text Fragment.
|
|
703
|
+
//
|
|
704
|
+
case "text": {
|
|
705
|
+
if (chunk.type === "tag") {
|
|
706
|
+
this._emitBlock(contentBlock, streamBlock);
|
|
707
|
+
if (chunk.selfClosing) {
|
|
708
|
+
this._emitBlock(contentBlock, chunk);
|
|
709
|
+
streamBlock = void 0;
|
|
710
|
+
} else {
|
|
711
|
+
streamBlock = chunk;
|
|
712
|
+
}
|
|
713
|
+
} else {
|
|
714
|
+
streamBlock.content += chunk.content;
|
|
715
|
+
}
|
|
716
|
+
break;
|
|
717
|
+
}
|
|
718
|
+
//
|
|
719
|
+
// No current chunk.
|
|
720
|
+
//
|
|
721
|
+
default: {
|
|
722
|
+
if (chunk.type === "tag" && chunk.selfClosing) {
|
|
723
|
+
this._emitBlock(contentBlock, chunk);
|
|
724
|
+
} else {
|
|
725
|
+
streamBlock = chunk;
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
break;
|
|
731
|
+
}
|
|
732
|
+
//
|
|
733
|
+
// JSON
|
|
734
|
+
//
|
|
735
|
+
case "input_json_delta": {
|
|
736
|
+
if (!contentBlock) {
|
|
737
|
+
log2.warn("unexpected input_json_delta", {
|
|
738
|
+
contentBlock
|
|
739
|
+
}, {
|
|
740
|
+
F: __dxlog_file3,
|
|
741
|
+
L: 218,
|
|
742
|
+
S: this,
|
|
743
|
+
C: (f, a) => f(...a)
|
|
744
|
+
});
|
|
745
|
+
break;
|
|
746
|
+
}
|
|
747
|
+
if (streamBlock?.type === "json") {
|
|
748
|
+
streamBlock.content += event.delta.partial_json;
|
|
749
|
+
} else {
|
|
750
|
+
streamBlock = {
|
|
751
|
+
type: "json",
|
|
752
|
+
content: event.delta.partial_json
|
|
753
|
+
};
|
|
754
|
+
}
|
|
755
|
+
break;
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
break;
|
|
759
|
+
}
|
|
760
|
+
case "content_block_stop": {
|
|
761
|
+
if (!contentBlock) {
|
|
762
|
+
log2.warn("unexpected content_block_stop", void 0, {
|
|
763
|
+
F: __dxlog_file3,
|
|
764
|
+
L: 235,
|
|
765
|
+
S: this,
|
|
766
|
+
C: (f, a) => f(...a)
|
|
767
|
+
});
|
|
768
|
+
break;
|
|
769
|
+
}
|
|
770
|
+
this._emitBlock(contentBlock, streamBlock);
|
|
771
|
+
contentBlock = void 0;
|
|
772
|
+
streamBlock = void 0;
|
|
773
|
+
break;
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
if (contentBlock && streamBlock) {
|
|
777
|
+
this._emitUpdate(contentBlock, streamBlock);
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
};
|
|
782
|
+
var mergeMessageBlock = (contentBlock, streamBlock) => {
|
|
783
|
+
log2("mergeMessageBlock", {
|
|
784
|
+
contentBlock,
|
|
785
|
+
streamBlock
|
|
786
|
+
}, {
|
|
787
|
+
F: __dxlog_file3,
|
|
788
|
+
L: 260,
|
|
789
|
+
S: void 0,
|
|
790
|
+
C: (f, a) => f(...a)
|
|
791
|
+
});
|
|
792
|
+
switch (streamBlock.type) {
|
|
793
|
+
//
|
|
794
|
+
// Text
|
|
795
|
+
//
|
|
796
|
+
case "text": {
|
|
797
|
+
return {
|
|
798
|
+
...contentBlock,
|
|
799
|
+
type: "text",
|
|
800
|
+
text: streamBlock.content
|
|
801
|
+
};
|
|
802
|
+
}
|
|
803
|
+
//
|
|
804
|
+
// XML
|
|
805
|
+
//
|
|
806
|
+
case "tag": {
|
|
807
|
+
switch (streamBlock.tag) {
|
|
808
|
+
case "cot": {
|
|
809
|
+
const content = streamBlock.content.map((block) => {
|
|
810
|
+
switch (block.type) {
|
|
811
|
+
case "text":
|
|
812
|
+
return block.content;
|
|
813
|
+
default:
|
|
814
|
+
return null;
|
|
815
|
+
}
|
|
816
|
+
}).filter(isNotFalsy).join("\n");
|
|
817
|
+
return {
|
|
818
|
+
...contentBlock,
|
|
819
|
+
type: "text",
|
|
820
|
+
disposition: "cot",
|
|
821
|
+
text: content
|
|
822
|
+
};
|
|
823
|
+
}
|
|
824
|
+
case "artifact": {
|
|
825
|
+
const { attributes } = streamBlock;
|
|
826
|
+
return {
|
|
827
|
+
type: "json",
|
|
828
|
+
disposition: "artifact",
|
|
829
|
+
json: JSON.stringify(attributes)
|
|
830
|
+
};
|
|
831
|
+
}
|
|
832
|
+
case "suggest": {
|
|
833
|
+
if (streamBlock.content.length === 1 && streamBlock.content[0].type === "text") {
|
|
834
|
+
return {
|
|
835
|
+
type: "json",
|
|
836
|
+
disposition: "suggest",
|
|
837
|
+
json: JSON.stringify({
|
|
838
|
+
text: streamBlock.content[0].content
|
|
839
|
+
})
|
|
840
|
+
};
|
|
841
|
+
}
|
|
842
|
+
break;
|
|
843
|
+
}
|
|
844
|
+
case "select": {
|
|
845
|
+
return {
|
|
846
|
+
type: "json",
|
|
847
|
+
disposition: "select",
|
|
848
|
+
json: JSON.stringify({
|
|
849
|
+
options: streamBlock.content.flatMap((content) => content.type === "tag" && content.content.length === 1 && content.content[0].type === "text" ? [
|
|
850
|
+
content.content[0].content
|
|
851
|
+
] : [])
|
|
852
|
+
})
|
|
853
|
+
};
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
break;
|
|
857
|
+
}
|
|
858
|
+
//
|
|
859
|
+
// JSON
|
|
860
|
+
//
|
|
861
|
+
case "json": {
|
|
862
|
+
switch (contentBlock.type) {
|
|
863
|
+
case "tool_use": {
|
|
864
|
+
return {
|
|
865
|
+
...contentBlock,
|
|
866
|
+
input: safeParseJson(streamBlock.content) ?? {}
|
|
867
|
+
};
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
return {
|
|
871
|
+
...contentBlock,
|
|
872
|
+
type: "json",
|
|
873
|
+
disposition: "artifact",
|
|
874
|
+
json: streamBlock.content
|
|
875
|
+
};
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
};
|
|
879
|
+
|
|
880
|
+
// packages/experimental/assistant/src/ai-service/types.ts
|
|
513
881
|
import { Schema as S2 } from "@effect/schema";
|
|
514
|
-
import { Tool, Message as
|
|
882
|
+
import { Tool, Message as Message3, SpaceIdSchema } from "@dxos/artifact";
|
|
515
883
|
import { ObjectId } from "@dxos/echo-schema";
|
|
516
884
|
var createArtifactElement = (id) => `<artifact id=${id} />`;
|
|
517
|
-
var LLMModel = S2.Literal("@anthropic/claude-3-5-haiku-20241022", "@anthropic/claude-3-5-sonnet-20241022", "@cf/deepseek-ai/deepseek-r1-distill-qwen-32b", "@hf/nousresearch/hermes-2-pro-mistral-7b", "@ollama/llama-3-2-3b", "@ollama/llama-3-1-nemotron-70b-instruct", "@ollama/llama-3-1-nemotron-mini-4b-instruct"
|
|
885
|
+
var LLMModel = S2.Literal("@anthropic/claude-3-5-haiku-20241022", "@anthropic/claude-3-5-sonnet-20241022", "@cf/deepseek-ai/deepseek-r1-distill-qwen-32b", "@hf/nousresearch/hermes-2-pro-mistral-7b", "@ollama/llama-3-2-3b", "@ollama/llama-3-1-nemotron-70b-instruct", "@ollama/llama-3-1-nemotron-mini-4b-instruct");
|
|
518
886
|
var ToolTypes = Object.freeze({
|
|
519
887
|
// TODO(dmaretskyi): Not implemented yet.
|
|
520
888
|
// DatabaseQuery: 'database_query',
|
|
@@ -523,159 +891,42 @@ var ToolTypes = Object.freeze({
|
|
|
523
891
|
var GenerateRequest = S2.Struct({
|
|
524
892
|
spaceId: S2.optional(SpaceIdSchema),
|
|
525
893
|
threadId: S2.optional(ObjectId),
|
|
894
|
+
// TODO(burdon): Make optional with default.
|
|
526
895
|
model: LLMModel,
|
|
527
896
|
/**
|
|
528
|
-
*
|
|
897
|
+
* Current request.
|
|
529
898
|
*/
|
|
530
|
-
|
|
899
|
+
// TODO(burdon): Remove from history.
|
|
900
|
+
// prompt: Message,
|
|
531
901
|
/**
|
|
532
902
|
* System instructions to the LLM.
|
|
533
903
|
*/
|
|
534
|
-
// TODO(dmaretskyi): Should this be part of the thread?.
|
|
535
904
|
systemPrompt: S2.optional(S2.String),
|
|
536
905
|
/**
|
|
906
|
+
* History of messages to include in the context window.
|
|
907
|
+
*/
|
|
908
|
+
history: S2.optional(S2.Array(Message3)),
|
|
909
|
+
/**
|
|
537
910
|
* Tools available for the LLM.
|
|
538
911
|
*/
|
|
539
912
|
tools: S2.optional(S2.Array(Tool).pipe(S2.mutable))
|
|
540
913
|
});
|
|
541
914
|
|
|
542
|
-
// packages/experimental/assistant/src/ai-service/parser.ts
|
|
543
|
-
var StreamingParser = class {
|
|
544
|
-
constructor(onBlock) {
|
|
545
|
-
this.onBlock = onBlock;
|
|
546
|
-
this.buffer = "";
|
|
547
|
-
}
|
|
548
|
-
/**
|
|
549
|
-
* Write a chunk of text to the parser.
|
|
550
|
-
*/
|
|
551
|
-
write(chunk) {
|
|
552
|
-
this.buffer += chunk;
|
|
553
|
-
this.processBuffer();
|
|
554
|
-
}
|
|
555
|
-
/**
|
|
556
|
-
* Signal that no more input will be written.
|
|
557
|
-
*/
|
|
558
|
-
end() {
|
|
559
|
-
if (this.buffer.length > 0) {
|
|
560
|
-
this.emitTextBlock(this.buffer);
|
|
561
|
-
this.buffer = "";
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
processBuffer() {
|
|
565
|
-
while (this.buffer.length > 0) {
|
|
566
|
-
if (this.tryParseCodeBlock()) {
|
|
567
|
-
continue;
|
|
568
|
-
}
|
|
569
|
-
const tagStartIndex = this.buffer.indexOf("<");
|
|
570
|
-
if (tagStartIndex === -1) {
|
|
571
|
-
return;
|
|
572
|
-
}
|
|
573
|
-
if (tagStartIndex > 0) {
|
|
574
|
-
this.emitTextBlock(this.buffer.slice(0, tagStartIndex));
|
|
575
|
-
this.buffer = this.buffer.slice(tagStartIndex);
|
|
576
|
-
continue;
|
|
577
|
-
}
|
|
578
|
-
const tagEndIndex = this.buffer.indexOf(">", tagStartIndex);
|
|
579
|
-
if (tagEndIndex === -1) {
|
|
580
|
-
return;
|
|
581
|
-
}
|
|
582
|
-
const isSelfClosing = this.buffer[tagEndIndex - 1] === "/";
|
|
583
|
-
const actualTagEndIndex = isSelfClosing ? tagEndIndex - 1 : tagEndIndex;
|
|
584
|
-
const tagContent = this.buffer.slice(tagStartIndex + 1, actualTagEndIndex).trim();
|
|
585
|
-
const [tagName, ...attrParts] = tagContent.split(/\s+/);
|
|
586
|
-
const attributes = this.parseAttributes(attrParts.join(" "));
|
|
587
|
-
if (isSelfClosing) {
|
|
588
|
-
this.emitTagBlock(tagName, "", attributes, true);
|
|
589
|
-
this.buffer = this.buffer.slice(tagEndIndex + 1);
|
|
590
|
-
continue;
|
|
591
|
-
}
|
|
592
|
-
const closingTagIndex = this.buffer.indexOf("</" + tagName + ">", tagEndIndex);
|
|
593
|
-
if (closingTagIndex === -1) {
|
|
594
|
-
return;
|
|
595
|
-
}
|
|
596
|
-
const content = this.buffer.slice(tagEndIndex + 1, closingTagIndex);
|
|
597
|
-
this.emitTagBlock(tagName, content, attributes, false);
|
|
598
|
-
this.buffer = this.buffer.slice(closingTagIndex + tagName.length + 3);
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
tryParseCodeBlock() {
|
|
602
|
-
const codeBlockStart = this.buffer.indexOf("```");
|
|
603
|
-
if (codeBlockStart === -1) {
|
|
604
|
-
return false;
|
|
605
|
-
}
|
|
606
|
-
if (codeBlockStart > 0) {
|
|
607
|
-
this.emitTextBlock(this.buffer.slice(0, codeBlockStart));
|
|
608
|
-
this.buffer = this.buffer.slice(codeBlockStart);
|
|
609
|
-
return true;
|
|
610
|
-
}
|
|
611
|
-
const nextNewline = this.buffer.indexOf("\n", codeBlockStart);
|
|
612
|
-
if (nextNewline === -1) {
|
|
613
|
-
return false;
|
|
614
|
-
}
|
|
615
|
-
const codeBlockEnd = this.buffer.indexOf("\n```", nextNewline);
|
|
616
|
-
if (codeBlockEnd === -1) {
|
|
617
|
-
return false;
|
|
618
|
-
}
|
|
619
|
-
const languageLine = this.buffer.slice(3, nextNewline).trim();
|
|
620
|
-
const content = this.buffer.slice(nextNewline + 1, codeBlockEnd);
|
|
621
|
-
this.emitCodeBlock(languageLine, content);
|
|
622
|
-
this.buffer = this.buffer.slice(codeBlockEnd + 4);
|
|
623
|
-
return true;
|
|
624
|
-
}
|
|
625
|
-
parseAttributes(attrString) {
|
|
626
|
-
const attributes = {};
|
|
627
|
-
if (!attrString) {
|
|
628
|
-
return attributes;
|
|
629
|
-
}
|
|
630
|
-
const attrPattern = /(\w+)\s*=\s*(?:"([^"]*)"|'([^']*)'|(\S+))/g;
|
|
631
|
-
let match;
|
|
632
|
-
while ((match = attrPattern.exec(attrString)) !== null) {
|
|
633
|
-
const [, name, doubleQuoted, singleQuoted, unquoted] = match;
|
|
634
|
-
attributes[name] = doubleQuoted ?? singleQuoted ?? unquoted;
|
|
635
|
-
}
|
|
636
|
-
return attributes;
|
|
637
|
-
}
|
|
638
|
-
emitTextBlock(text) {
|
|
639
|
-
if (text.trim().length > 0) {
|
|
640
|
-
this.onBlock({
|
|
641
|
-
type: "text",
|
|
642
|
-
content: text
|
|
643
|
-
});
|
|
644
|
-
}
|
|
645
|
-
}
|
|
646
|
-
emitTagBlock(name, content, attributes, selfClosing) {
|
|
647
|
-
this.onBlock({
|
|
648
|
-
type: "tag",
|
|
649
|
-
name,
|
|
650
|
-
content: content.trim(),
|
|
651
|
-
attributes,
|
|
652
|
-
selfClosing
|
|
653
|
-
});
|
|
654
|
-
}
|
|
655
|
-
emitCodeBlock(language, content) {
|
|
656
|
-
this.onBlock({
|
|
657
|
-
type: "code",
|
|
658
|
-
language,
|
|
659
|
-
content: content.trim()
|
|
660
|
-
});
|
|
661
|
-
}
|
|
662
|
-
};
|
|
663
|
-
|
|
664
915
|
// packages/experimental/assistant/src/conversation/conversation.ts
|
|
665
|
-
import { ToolResult, Message as
|
|
666
|
-
import { createStatic, ObjectId as ObjectId2 } from "@dxos/echo-schema";
|
|
916
|
+
import { ToolResult, Message as Message4 } from "@dxos/artifact";
|
|
917
|
+
import { createStatic as createStatic2, ObjectId as ObjectId2 } from "@dxos/echo-schema";
|
|
667
918
|
import { invariant as invariant3 } from "@dxos/invariant";
|
|
668
|
-
import { log as
|
|
669
|
-
var
|
|
919
|
+
import { log as log3 } from "@dxos/log";
|
|
920
|
+
var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/conversation/conversation.ts";
|
|
670
921
|
var runLLM = async (params) => {
|
|
671
922
|
let conversationResult = null;
|
|
672
923
|
const history = params.history ?? [];
|
|
673
924
|
const generate = async () => {
|
|
674
|
-
|
|
925
|
+
log3("llm generate", {
|
|
675
926
|
tools: params.tools
|
|
676
927
|
}, {
|
|
677
|
-
F:
|
|
678
|
-
L:
|
|
928
|
+
F: __dxlog_file4,
|
|
929
|
+
L: 44,
|
|
679
930
|
S: void 0,
|
|
680
931
|
C: (f, a) => f(...a)
|
|
681
932
|
});
|
|
@@ -691,20 +942,30 @@ var runLLM = async (params) => {
|
|
|
691
942
|
for await (const event of stream) {
|
|
692
943
|
params.logger?.(event);
|
|
693
944
|
}
|
|
694
|
-
|
|
945
|
+
await stream.complete();
|
|
946
|
+
const messages = [];
|
|
695
947
|
const message = messages.at(-1);
|
|
696
|
-
|
|
948
|
+
invariant3(message, void 0, {
|
|
949
|
+
F: __dxlog_file4,
|
|
950
|
+
L: 63,
|
|
951
|
+
S: void 0,
|
|
952
|
+
A: [
|
|
953
|
+
"message",
|
|
954
|
+
""
|
|
955
|
+
]
|
|
956
|
+
});
|
|
957
|
+
log3("llm result", {
|
|
697
958
|
time: Date.now() - beginTs,
|
|
698
959
|
message
|
|
699
960
|
}, {
|
|
700
|
-
F:
|
|
701
|
-
L:
|
|
961
|
+
F: __dxlog_file4,
|
|
962
|
+
L: 65,
|
|
702
963
|
S: void 0,
|
|
703
964
|
C: (f, a) => f(...a)
|
|
704
965
|
});
|
|
705
966
|
invariant3(message, void 0, {
|
|
706
|
-
F:
|
|
707
|
-
L:
|
|
967
|
+
F: __dxlog_file4,
|
|
968
|
+
L: 66,
|
|
708
969
|
S: void 0,
|
|
709
970
|
A: [
|
|
710
971
|
"message",
|
|
@@ -723,8 +984,8 @@ var runLLM = async (params) => {
|
|
|
723
984
|
if (isToolUse2) {
|
|
724
985
|
const toolCalls = message.content.filter((block) => block.type === "tool_use");
|
|
725
986
|
invariant3(toolCalls.length === 1, void 0, {
|
|
726
|
-
F:
|
|
727
|
-
L:
|
|
987
|
+
F: __dxlog_file4,
|
|
988
|
+
L: 77,
|
|
728
989
|
S: void 0,
|
|
729
990
|
A: [
|
|
730
991
|
"toolCalls.length === 1",
|
|
@@ -737,8 +998,8 @@ var runLLM = async (params) => {
|
|
|
737
998
|
throw new Error(`Tool not found: ${toolCall.name}`);
|
|
738
999
|
}
|
|
739
1000
|
invariant3(tool.execute, void 0, {
|
|
740
|
-
F:
|
|
741
|
-
L:
|
|
1001
|
+
F: __dxlog_file4,
|
|
1002
|
+
L: 84,
|
|
742
1003
|
S: void 0,
|
|
743
1004
|
A: [
|
|
744
1005
|
"tool.execute",
|
|
@@ -748,11 +1009,11 @@ var runLLM = async (params) => {
|
|
|
748
1009
|
const toolResult = await tool.execute(toolCall.input);
|
|
749
1010
|
switch (toolResult.kind) {
|
|
750
1011
|
case "error": {
|
|
751
|
-
|
|
1012
|
+
log3.warn("tool error", {
|
|
752
1013
|
message: toolResult.message
|
|
753
1014
|
}, {
|
|
754
|
-
F:
|
|
755
|
-
L:
|
|
1015
|
+
F: __dxlog_file4,
|
|
1016
|
+
L: 88,
|
|
756
1017
|
S: void 0,
|
|
757
1018
|
C: (f, a) => f(...a)
|
|
758
1019
|
});
|
|
@@ -778,11 +1039,11 @@ var runLLM = async (params) => {
|
|
|
778
1039
|
return true;
|
|
779
1040
|
}
|
|
780
1041
|
case "success": {
|
|
781
|
-
|
|
1042
|
+
log3("tool success", {
|
|
782
1043
|
result: toolResult.result
|
|
783
1044
|
}, {
|
|
784
|
-
F:
|
|
785
|
-
L:
|
|
1045
|
+
F: __dxlog_file4,
|
|
1046
|
+
L: 110,
|
|
786
1047
|
S: void 0,
|
|
787
1048
|
C: (f, a) => f(...a)
|
|
788
1049
|
});
|
|
@@ -807,11 +1068,11 @@ var runLLM = async (params) => {
|
|
|
807
1068
|
return true;
|
|
808
1069
|
}
|
|
809
1070
|
case "break": {
|
|
810
|
-
|
|
1071
|
+
log3("tool break", {
|
|
811
1072
|
result: toolResult.result
|
|
812
1073
|
}, {
|
|
813
|
-
F:
|
|
814
|
-
L:
|
|
1074
|
+
F: __dxlog_file4,
|
|
1075
|
+
L: 131,
|
|
815
1076
|
S: void 0,
|
|
816
1077
|
C: (f, a) => f(...a)
|
|
817
1078
|
});
|
|
@@ -829,12 +1090,24 @@ var runLLM = async (params) => {
|
|
|
829
1090
|
history
|
|
830
1091
|
};
|
|
831
1092
|
};
|
|
832
|
-
var isToolUse = (message) =>
|
|
1093
|
+
var isToolUse = (message) => {
|
|
1094
|
+
const block = message.content.at(-1);
|
|
1095
|
+
invariant3(block, void 0, {
|
|
1096
|
+
F: __dxlog_file4,
|
|
1097
|
+
L: 152,
|
|
1098
|
+
S: void 0,
|
|
1099
|
+
A: [
|
|
1100
|
+
"block",
|
|
1101
|
+
""
|
|
1102
|
+
]
|
|
1103
|
+
});
|
|
1104
|
+
return block.type === "tool_use";
|
|
1105
|
+
};
|
|
833
1106
|
var runTools = async ({ message, tools, extensions }) => {
|
|
834
1107
|
const toolCalls = message.content.filter((block) => block.type === "tool_use");
|
|
835
1108
|
invariant3(toolCalls.length === 1, void 0, {
|
|
836
|
-
F:
|
|
837
|
-
L:
|
|
1109
|
+
F: __dxlog_file4,
|
|
1110
|
+
L: 174,
|
|
838
1111
|
S: void 0,
|
|
839
1112
|
A: [
|
|
840
1113
|
"toolCalls.length === 1",
|
|
@@ -849,8 +1122,8 @@ var runTools = async ({ message, tools, extensions }) => {
|
|
|
849
1122
|
let toolResult;
|
|
850
1123
|
try {
|
|
851
1124
|
invariant3(tool.execute, void 0, {
|
|
852
|
-
F:
|
|
853
|
-
L:
|
|
1125
|
+
F: __dxlog_file4,
|
|
1126
|
+
L: 183,
|
|
854
1127
|
S: void 0,
|
|
855
1128
|
A: [
|
|
856
1129
|
"tool.execute",
|
|
@@ -861,11 +1134,11 @@ var runTools = async ({ message, tools, extensions }) => {
|
|
|
861
1134
|
extensions
|
|
862
1135
|
});
|
|
863
1136
|
} catch (error) {
|
|
864
|
-
|
|
1137
|
+
log3("tool error", {
|
|
865
1138
|
error
|
|
866
1139
|
}, {
|
|
867
|
-
F:
|
|
868
|
-
L:
|
|
1140
|
+
F: __dxlog_file4,
|
|
1141
|
+
L: 186,
|
|
869
1142
|
S: void 0,
|
|
870
1143
|
C: (f, a) => f(...a)
|
|
871
1144
|
});
|
|
@@ -873,15 +1146,15 @@ var runTools = async ({ message, tools, extensions }) => {
|
|
|
873
1146
|
}
|
|
874
1147
|
switch (toolResult.kind) {
|
|
875
1148
|
case "error": {
|
|
876
|
-
|
|
1149
|
+
log3("tool error", {
|
|
877
1150
|
message: toolResult.message
|
|
878
1151
|
}, {
|
|
879
|
-
F:
|
|
880
|
-
L:
|
|
1152
|
+
F: __dxlog_file4,
|
|
1153
|
+
L: 192,
|
|
881
1154
|
S: void 0,
|
|
882
1155
|
C: (f, a) => f(...a)
|
|
883
1156
|
});
|
|
884
|
-
const resultMessage =
|
|
1157
|
+
const resultMessage = createStatic2(Message4, {
|
|
885
1158
|
role: "user",
|
|
886
1159
|
content: [
|
|
887
1160
|
{
|
|
@@ -898,21 +1171,21 @@ var runTools = async ({ message, tools, extensions }) => {
|
|
|
898
1171
|
};
|
|
899
1172
|
}
|
|
900
1173
|
case "success": {
|
|
901
|
-
|
|
1174
|
+
log3("tool success", {
|
|
902
1175
|
result: toolResult.result
|
|
903
1176
|
}, {
|
|
904
|
-
F:
|
|
905
|
-
L:
|
|
1177
|
+
F: __dxlog_file4,
|
|
1178
|
+
L: 213,
|
|
906
1179
|
S: void 0,
|
|
907
1180
|
C: (f, a) => f(...a)
|
|
908
1181
|
});
|
|
909
|
-
const resultMessage =
|
|
1182
|
+
const resultMessage = createStatic2(Message4, {
|
|
910
1183
|
role: "user",
|
|
911
1184
|
content: [
|
|
912
1185
|
{
|
|
913
1186
|
type: "tool_result",
|
|
914
1187
|
toolUseId: toolCall.id,
|
|
915
|
-
content: typeof toolResult.result === "string" ? toolResult.result : JSON.stringify(toolResult.result)
|
|
1188
|
+
content: typeof toolResult.result === "string" ? toolResult.result : JSON.stringify(toolResult.result) ?? ""
|
|
916
1189
|
}
|
|
917
1190
|
]
|
|
918
1191
|
});
|
|
@@ -922,11 +1195,11 @@ var runTools = async ({ message, tools, extensions }) => {
|
|
|
922
1195
|
};
|
|
923
1196
|
}
|
|
924
1197
|
case "break": {
|
|
925
|
-
|
|
1198
|
+
log3("tool break", {
|
|
926
1199
|
result: toolResult.result
|
|
927
1200
|
}, {
|
|
928
|
-
F:
|
|
929
|
-
L:
|
|
1201
|
+
F: __dxlog_file4,
|
|
1202
|
+
L: 233,
|
|
930
1203
|
S: void 0,
|
|
931
1204
|
C: (f, a) => f(...a)
|
|
932
1205
|
});
|
|
@@ -943,9 +1216,11 @@ export {
|
|
|
943
1216
|
GenerateRequest,
|
|
944
1217
|
GenerationStream,
|
|
945
1218
|
LLMModel,
|
|
946
|
-
|
|
1219
|
+
MixedStreamParser,
|
|
1220
|
+
StreamTransform,
|
|
947
1221
|
ToolTypes,
|
|
948
1222
|
createArtifactElement,
|
|
1223
|
+
createGenerationStream,
|
|
949
1224
|
executeQuery,
|
|
950
1225
|
executeQueryPlan,
|
|
951
1226
|
formatInferredRelationshipLabel,
|
|
@@ -953,6 +1228,7 @@ export {
|
|
|
953
1228
|
formatNodeLabel,
|
|
954
1229
|
isReference,
|
|
955
1230
|
isToolUse,
|
|
1231
|
+
mergeMessageBlock,
|
|
956
1232
|
parseCypherQuery,
|
|
957
1233
|
runLLM,
|
|
958
1234
|
runTools
|