@dxos/assistant 0.7.4-df37807 → 0.7.5-feature-compute.4d9d99a
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-AKP45LQH.mjs → chunk-LVLSJQB3.mjs} +506 -357
- package/dist/lib/browser/chunk-LVLSJQB3.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +17 -17
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +10 -3
- package/dist/lib/browser/testing/index.mjs.map +4 -4
- package/dist/lib/node/{chunk-G6CGWEMO.cjs → chunk-SHOSAIOD.cjs} +507 -362
- package/dist/lib/node/chunk-SHOSAIOD.cjs.map +7 -0
- package/dist/lib/node/index.cjs +42 -42
- package/dist/lib/node/index.cjs.map +1 -1
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +18 -12
- package/dist/lib/node/testing/index.cjs.map +4 -4
- package/dist/lib/node-esm/{chunk-WUPUPEFB.mjs → chunk-ZLBTRSUY.mjs} +506 -357
- package/dist/lib/node-esm/chunk-ZLBTRSUY.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +17 -17
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +10 -3
- package/dist/lib/node-esm/testing/index.mjs.map +4 -4
- package/dist/types/src/ai-service/client.d.ts +15 -32
- 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/stream.d.ts +34 -0
- package/dist/types/src/ai-service/stream.d.ts.map +1 -0
- package/dist/types/src/ai-service/stream.test.d.ts +2 -0
- package/dist/types/src/ai-service/stream.test.d.ts.map +1 -0
- package/dist/types/src/ai-service/types/index.d.ts +3 -0
- package/dist/types/src/ai-service/types/index.d.ts.map +1 -0
- package/dist/types/src/ai-service/types/message.d.ts +191 -0
- package/dist/types/src/ai-service/types/message.d.ts.map +1 -0
- package/dist/types/src/ai-service/types/schema.d.ts +175 -0
- package/dist/types/src/ai-service/types/schema.d.ts.map +1 -0
- package/dist/types/src/ai-service/util/index.d.ts +2 -0
- package/dist/types/src/ai-service/util/index.d.ts.map +1 -0
- package/dist/types/src/ai-service/{sse.d.ts → util/sse.d.ts} +5 -0
- package/dist/types/src/ai-service/util/sse.d.ts.map +1 -0
- package/dist/types/src/conversation/conversation.d.ts +54 -4
- package/dist/types/src/conversation/conversation.d.ts.map +1 -1
- package/dist/types/src/conversation/types.d.ts +35 -4
- package/dist/types/src/conversation/types.d.ts.map +1 -1
- package/dist/types/src/cypher/combinators.d.ts.map +1 -1
- package/dist/types/src/cypher/parjs.d.ts +1 -1
- package/dist/types/src/cypher/parjs.d.ts.map +1 -1
- package/dist/types/src/cypher/schema.d.ts.map +1 -1
- package/dist/types/src/testing/artifacts.test.d.ts +1 -0
- package/dist/types/src/testing/artifacts.test.d.ts.map +1 -0
- package/dist/types/src/testing/data.d.ts +2 -0
- package/dist/types/src/testing/data.d.ts.map +1 -0
- package/dist/types/src/testing/defs.d.ts +5 -0
- package/dist/types/src/testing/defs.d.ts.map +1 -0
- package/dist/types/src/testing/index.d.ts +1 -0
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/test-data.d.ts +1 -1
- package/dist/types/src/testing/test-data.d.ts.map +1 -1
- package/package.json +20 -18
- package/src/ai-service/client.test.ts +25 -26
- package/src/ai-service/client.ts +29 -177
- package/src/ai-service/index.ts +2 -1
- package/src/ai-service/stream.test.ts +186 -0
- package/src/ai-service/stream.ts +181 -0
- package/src/ai-service/types/index.ts +6 -0
- package/src/ai-service/types/message.ts +147 -0
- package/src/ai-service/types/schema.ts +128 -0
- package/src/ai-service/util/index.ts +5 -0
- package/src/ai-service/{sse.ts → util/sse.ts} +5 -0
- package/src/bin/chat.ts +6 -9
- package/src/conversation/conversation.test.ts +10 -14
- package/src/conversation/conversation.ts +109 -14
- package/src/conversation/types.ts +47 -6
- package/src/cypher/combinators.test.ts +3 -3
- package/src/cypher/combinators.ts +0 -1
- package/src/cypher/execution-plan.test.ts +5 -3
- package/src/cypher/parjs-shims.d.ts +4 -0
- package/src/cypher/parjs.ts +2 -2
- package/src/cypher/query-executor.test.ts +1 -1
- package/src/cypher/schema.ts +0 -1
- package/src/index.ts +1 -1
- package/src/testing/artifacts.test.ts +182 -0
- package/src/testing/data.ts +13 -0
- package/src/testing/defs.ts +8 -0
- package/src/testing/index.ts +1 -0
- package/src/testing/query.test.ts +9 -9
- package/src/testing/test-data.ts +1 -1
- package/vendor/parjs-browser.mjs +4 -0
- package/vendor/parjs-default.mjs +8 -0
- package/dist/lib/browser/chunk-AKP45LQH.mjs.map +0 -7
- package/dist/lib/node/chunk-G6CGWEMO.cjs.map +0 -7
- package/dist/lib/node-esm/chunk-WUPUPEFB.mjs.map +0 -7
- package/dist/types/src/ai-service/schema.d.ts +0 -296
- package/dist/types/src/ai-service/schema.d.ts.map +0 -1
- package/dist/types/src/ai-service/sse.d.ts.map +0 -1
- package/dist/types/src/sanity.test.d.ts +0 -2
- package/dist/types/src/sanity.test.d.ts.map +0 -1
- package/src/ai-service/schema.ts +0 -260
- package/src/sanity.test.ts +0 -9
|
@@ -1,131 +1,9 @@
|
|
|
1
|
-
// packages/experimental/assistant/src/ai-service/
|
|
2
|
-
import { Schema as S } from "@effect/schema";
|
|
3
|
-
import { ulid } from "ulidx";
|
|
4
|
-
import { JsonSchemaType } from "@dxos/echo-schema";
|
|
5
|
-
import { SpaceId } from "@dxos/keys";
|
|
6
|
-
var SpaceIdSchema = S.String.pipe(S.filter(SpaceId.isValid));
|
|
7
|
-
var ObjectIdBrand = Symbol("@dxos/echo/ObjectId");
|
|
8
|
-
var ObjectIdSchema = S.ULID.pipe(S.brand(ObjectIdBrand));
|
|
9
|
-
var ObjectId = class extends ObjectIdSchema {
|
|
10
|
-
static random() {
|
|
11
|
-
return ulid();
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
var Space = S.Struct({
|
|
15
|
-
id: SpaceIdSchema
|
|
16
|
-
});
|
|
17
|
-
var Thread = S.Struct({
|
|
18
|
-
id: ObjectId,
|
|
19
|
-
spaceId: SpaceIdSchema
|
|
20
|
-
});
|
|
21
|
-
var MessageRole = S.String.pipe(S.filter((role) => role === "user" || role === "assistant"));
|
|
22
|
-
var MessageTextContentBlock = S.Struct({
|
|
23
|
-
type: S.Literal("text"),
|
|
24
|
-
text: S.String
|
|
25
|
-
}).pipe(S.mutable);
|
|
26
|
-
var ImageSource = S.Struct({
|
|
27
|
-
type: S.Literal("base64"),
|
|
28
|
-
media_type: S.Literal("image/jpeg", "image/png", "image/gif", "image/webp"),
|
|
29
|
-
data: S.String
|
|
30
|
-
}).pipe(S.mutable);
|
|
31
|
-
var MessageImageContentBlock = S.Struct({
|
|
32
|
-
type: S.Literal("image"),
|
|
33
|
-
id: S.optional(S.String),
|
|
34
|
-
source: S.optional(ImageSource)
|
|
35
|
-
}).pipe(S.mutable);
|
|
36
|
-
var MessageToolUseContentBlock = S.Struct({
|
|
37
|
-
type: S.Literal("tool_use"),
|
|
38
|
-
/**
|
|
39
|
-
* Opaque service-defined ID of the tool invocation.
|
|
40
|
-
* Used to match the tool result block.
|
|
41
|
-
*/
|
|
42
|
-
id: S.String,
|
|
43
|
-
/**
|
|
44
|
-
* Tool name.
|
|
45
|
-
*/
|
|
46
|
-
name: S.String,
|
|
47
|
-
input: S.Unknown,
|
|
48
|
-
/**
|
|
49
|
-
* Used to accumulate the partial tool input JSON in streaming mode.
|
|
50
|
-
*/
|
|
51
|
-
inputJson: S.optional(S.String)
|
|
52
|
-
}).pipe(S.mutable);
|
|
53
|
-
var MessageToolResultContentBlock = S.Struct({
|
|
54
|
-
type: S.Literal("tool_result"),
|
|
55
|
-
toolUseId: S.String,
|
|
56
|
-
content: S.String,
|
|
57
|
-
isError: S.optional(S.Boolean)
|
|
58
|
-
});
|
|
59
|
-
var MessageContentBlock = S.Union(MessageTextContentBlock, MessageImageContentBlock, MessageToolUseContentBlock, MessageToolResultContentBlock);
|
|
60
|
-
var Message = S.Struct({
|
|
61
|
-
id: ObjectId,
|
|
62
|
-
threadId: S.optional(ObjectId),
|
|
63
|
-
spaceId: S.optional(SpaceIdSchema),
|
|
64
|
-
role: MessageRole,
|
|
65
|
-
content: S.Array(MessageContentBlock).pipe(S.mutable),
|
|
66
|
-
/**
|
|
67
|
-
* ID of the message from the foreign provider.
|
|
68
|
-
*/
|
|
69
|
-
// TODO(dmaretskyi): Should be in meta/keys.
|
|
70
|
-
foreignId: S.optional(S.String)
|
|
71
|
-
});
|
|
72
|
-
var MessageFromDb = S.Struct({
|
|
73
|
-
...Message.fields,
|
|
74
|
-
content: S.propertySignature(Message.fields.content.pipe((schema) => S.parseJson(schema))).pipe(S.fromKey("contentJson"))
|
|
75
|
-
});
|
|
76
|
-
var LLMModel = S.Literal("@hf/nousresearch/hermes-2-pro-mistral-7b", "@anthropic/claude-3-5-sonnet-20241022", "@anthropic/claude-3-5-haiku-20241022", "@ollama/llama-3-2-3b", "@ollama/llama-3-1-nemotron-70b-instruct", "@ollama/llama-3-1-nemotron-mini-4b-instruct");
|
|
77
|
-
var ToolTypes = Object.freeze({
|
|
78
|
-
// TODO(dmaretskyi): Not implemented yet.
|
|
79
|
-
// DatabaseQuery: 'database_query',
|
|
80
|
-
TextToImage: "text_to_image"
|
|
81
|
-
});
|
|
82
|
-
var LLMTool = S.Struct({
|
|
83
|
-
name: S.String,
|
|
84
|
-
/**
|
|
85
|
-
* If the tool is implemented by the service a type should be provided.
|
|
86
|
-
* For user-implemented tools, this field should be omitted.
|
|
87
|
-
* See {@link ToolTypes} for the list of supported types.
|
|
88
|
-
*/
|
|
89
|
-
type: S.optional(S.String),
|
|
90
|
-
/**
|
|
91
|
-
* Required for user-implemented tools.
|
|
92
|
-
*/
|
|
93
|
-
description: S.optional(S.String),
|
|
94
|
-
/**
|
|
95
|
-
* Input schema for the tool in the JSON Schema format.
|
|
96
|
-
* Required for user-implemented tools.
|
|
97
|
-
*/
|
|
98
|
-
parameters: S.optional(JsonSchemaType),
|
|
99
|
-
/**
|
|
100
|
-
* Tool-specific options.
|
|
101
|
-
*/
|
|
102
|
-
options: S.optional(S.Any),
|
|
103
|
-
/**
|
|
104
|
-
* Javascript function to execute the tool.
|
|
105
|
-
*/
|
|
106
|
-
execute: S.optional(S.Any)
|
|
107
|
-
});
|
|
108
|
-
var GenerateRequest = S.Struct({
|
|
109
|
-
model: LLMModel,
|
|
110
|
-
spaceId: SpaceIdSchema,
|
|
111
|
-
threadId: ObjectId,
|
|
112
|
-
/**
|
|
113
|
-
* System instructions to the LLM.
|
|
114
|
-
*/
|
|
115
|
-
// TODO(dmaretskyi): Should this be part of the thread?.
|
|
116
|
-
systemPrompt: S.optional(S.String),
|
|
117
|
-
/**
|
|
118
|
-
* Tools available for the LLM.
|
|
119
|
-
*/
|
|
120
|
-
tools: S.Array(LLMTool).pipe(S.mutable)
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
// packages/experimental/assistant/src/ai-service/client.ts
|
|
124
|
-
import { Schema as S2 } from "@effect/schema";
|
|
1
|
+
// packages/experimental/assistant/src/ai-service/stream.ts
|
|
125
2
|
import { Trigger } from "@dxos/async";
|
|
126
3
|
import { invariant } from "@dxos/invariant";
|
|
4
|
+
import { log } from "@dxos/log";
|
|
127
5
|
|
|
128
|
-
// packages/experimental/assistant/src/ai-service/sse.ts
|
|
6
|
+
// packages/experimental/assistant/src/ai-service/util/sse.ts
|
|
129
7
|
async function* iterSSEMessages(response, controller) {
|
|
130
8
|
if (!response.body) {
|
|
131
9
|
controller.abort();
|
|
@@ -362,84 +240,45 @@ var LineDecoder = class _LineDecoder {
|
|
|
362
240
|
}
|
|
363
241
|
};
|
|
364
242
|
|
|
365
|
-
// packages/experimental/assistant/src/ai-service/
|
|
366
|
-
var __dxlog_file = "/
|
|
367
|
-
var AIServiceClientImpl = class {
|
|
368
|
-
constructor({ endpoint }) {
|
|
369
|
-
this._endpoint = endpoint;
|
|
370
|
-
}
|
|
371
|
-
async getSpace(spaceId) {
|
|
372
|
-
throw new Error("Not implemented");
|
|
373
|
-
}
|
|
374
|
-
async getThread(spaceId, threadId) {
|
|
375
|
-
throw new Error("Not implemented");
|
|
376
|
-
}
|
|
377
|
-
async getMessagesInThread(spaceId, threadId) {
|
|
378
|
-
const res = await fetch(`${this._endpoint}/space/${spaceId}/thread/${threadId}/message`);
|
|
379
|
-
return S2.decodePromise(S2.Array(Message).pipe(S2.mutable))((await res.json()).data.results);
|
|
380
|
-
}
|
|
381
|
-
async insertMessages(messages) {
|
|
382
|
-
const res = await fetch(`${this._endpoint}/message`, {
|
|
383
|
-
method: "POST",
|
|
384
|
-
headers: {
|
|
385
|
-
"Content-Type": "application/json"
|
|
386
|
-
},
|
|
387
|
-
body: JSON.stringify({
|
|
388
|
-
messages: await S2.encodePromise(S2.Array(Message))(messages)
|
|
389
|
-
})
|
|
390
|
-
});
|
|
391
|
-
if (!res.ok) {
|
|
392
|
-
throw new Error(await res.text());
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
// TODO(dmaretskyi): Accept partial message schema.
|
|
396
|
-
async updateMessage(spaceId, threadId, messageId, message) {
|
|
397
|
-
throw new Error("Not implemented");
|
|
398
|
-
}
|
|
399
|
-
async generate(request) {
|
|
400
|
-
const response = await fetch(`${this._endpoint}/generate`, {
|
|
401
|
-
method: "POST",
|
|
402
|
-
headers: {
|
|
403
|
-
"Content-Type": "application/json"
|
|
404
|
-
},
|
|
405
|
-
body: JSON.stringify(request)
|
|
406
|
-
});
|
|
407
|
-
invariant(response.body instanceof ReadableStream, void 0, {
|
|
408
|
-
F: __dxlog_file,
|
|
409
|
-
L: 81,
|
|
410
|
-
S: this,
|
|
411
|
-
A: [
|
|
412
|
-
"response.body instanceof ReadableStream",
|
|
413
|
-
""
|
|
414
|
-
]
|
|
415
|
-
});
|
|
416
|
-
return GenerationStream.fromSSEResponse({
|
|
417
|
-
spaceId: request.spaceId,
|
|
418
|
-
threadId: request.threadId
|
|
419
|
-
}, response);
|
|
420
|
-
}
|
|
421
|
-
};
|
|
243
|
+
// packages/experimental/assistant/src/ai-service/stream.ts
|
|
244
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/ai-service/stream.ts";
|
|
422
245
|
var GenerationStream = class _GenerationStream {
|
|
423
|
-
|
|
424
|
-
|
|
246
|
+
/**
|
|
247
|
+
* Creates a stream from an SSE response.
|
|
248
|
+
*/
|
|
249
|
+
static fromSSEResponse(params, response, controller = new AbortController()) {
|
|
425
250
|
const iterator = async function* () {
|
|
426
251
|
for await (const sse of iterSSEMessages(response, controller)) {
|
|
427
252
|
if (sse.event === "completion") {
|
|
428
253
|
try {
|
|
429
254
|
yield JSON.parse(sse.data);
|
|
430
|
-
} catch (
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
255
|
+
} catch (err) {
|
|
256
|
+
log.error("could not parse message into JSON:", {
|
|
257
|
+
data: sse.data,
|
|
258
|
+
raw: sse.raw
|
|
259
|
+
}, {
|
|
260
|
+
F: __dxlog_file,
|
|
261
|
+
L: 30,
|
|
262
|
+
S: this,
|
|
263
|
+
C: (f, a) => f(...a)
|
|
264
|
+
});
|
|
265
|
+
throw err;
|
|
434
266
|
}
|
|
435
267
|
}
|
|
436
268
|
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") {
|
|
437
269
|
try {
|
|
438
270
|
yield JSON.parse(sse.data);
|
|
439
|
-
} catch (
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
271
|
+
} catch (err) {
|
|
272
|
+
log.error("could not parse message into JSON:", {
|
|
273
|
+
data: sse.data,
|
|
274
|
+
raw: sse.raw
|
|
275
|
+
}, {
|
|
276
|
+
F: __dxlog_file,
|
|
277
|
+
L: 46,
|
|
278
|
+
S: this,
|
|
279
|
+
C: (f, a) => f(...a)
|
|
280
|
+
});
|
|
281
|
+
throw err;
|
|
443
282
|
}
|
|
444
283
|
}
|
|
445
284
|
if (sse.event === "ping") {
|
|
@@ -450,22 +289,20 @@ var GenerationStream = class _GenerationStream {
|
|
|
450
289
|
}
|
|
451
290
|
}
|
|
452
291
|
};
|
|
453
|
-
return new _GenerationStream(
|
|
292
|
+
return new _GenerationStream(controller, iterator, params);
|
|
454
293
|
}
|
|
455
|
-
constructor(
|
|
456
|
-
this.
|
|
294
|
+
constructor(_controller, _getIterator, _params) {
|
|
295
|
+
this._controller = _controller;
|
|
457
296
|
this._getIterator = _getIterator;
|
|
297
|
+
this._params = _params;
|
|
298
|
+
this._done = new Trigger();
|
|
458
299
|
this._previousMessages = [];
|
|
459
300
|
this._accumulatedMessage = void 0;
|
|
460
|
-
this._done = new Trigger();
|
|
461
301
|
this._iterator = void 0;
|
|
462
302
|
}
|
|
463
303
|
[Symbol.asyncIterator]() {
|
|
464
304
|
return this._createIterator();
|
|
465
305
|
}
|
|
466
|
-
cancel() {
|
|
467
|
-
throw new Error("Not implemented");
|
|
468
|
-
}
|
|
469
306
|
get accumulatedMessages() {
|
|
470
307
|
return [
|
|
471
308
|
...this._previousMessages,
|
|
@@ -475,6 +312,12 @@ var GenerationStream = class _GenerationStream {
|
|
|
475
312
|
];
|
|
476
313
|
}
|
|
477
314
|
/**
|
|
315
|
+
* Aborts the stream.
|
|
316
|
+
*/
|
|
317
|
+
abort() {
|
|
318
|
+
this._controller.abort();
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
478
321
|
* Returns the complete message list generated by the AI service.
|
|
479
322
|
*/
|
|
480
323
|
async complete() {
|
|
@@ -483,12 +326,20 @@ var GenerationStream = class _GenerationStream {
|
|
|
483
326
|
}
|
|
484
327
|
_createIterator() {
|
|
485
328
|
const self = this;
|
|
486
|
-
return this._iterator ??=
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
329
|
+
return this._iterator ??= (() => {
|
|
330
|
+
const generator = async function* () {
|
|
331
|
+
try {
|
|
332
|
+
for await (const event of self._getIterator()) {
|
|
333
|
+
self._processEvent(event);
|
|
334
|
+
yield event;
|
|
335
|
+
}
|
|
336
|
+
this._done.wake();
|
|
337
|
+
} catch (err) {
|
|
338
|
+
this._done.throw(err);
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
return generator.call(this);
|
|
342
|
+
})();
|
|
492
343
|
}
|
|
493
344
|
_processEvent(event) {
|
|
494
345
|
switch (event.type) {
|
|
@@ -519,7 +370,7 @@ var GenerationStream = class _GenerationStream {
|
|
|
519
370
|
case "content_block_start": {
|
|
520
371
|
invariant(this._accumulatedMessage, void 0, {
|
|
521
372
|
F: __dxlog_file,
|
|
522
|
-
L:
|
|
373
|
+
L: 148,
|
|
523
374
|
S: this,
|
|
524
375
|
A: [
|
|
525
376
|
"this._accumulatedMessage",
|
|
@@ -532,7 +383,7 @@ var GenerationStream = class _GenerationStream {
|
|
|
532
383
|
case "content_block_delta": {
|
|
533
384
|
invariant(this._accumulatedMessage, void 0, {
|
|
534
385
|
F: __dxlog_file,
|
|
535
|
-
L:
|
|
386
|
+
L: 154,
|
|
536
387
|
S: this,
|
|
537
388
|
A: [
|
|
538
389
|
"this._accumulatedMessage",
|
|
@@ -551,7 +402,7 @@ var GenerationStream = class _GenerationStream {
|
|
|
551
402
|
case "content_block_stop": {
|
|
552
403
|
invariant(this._accumulatedMessage, void 0, {
|
|
553
404
|
F: __dxlog_file,
|
|
554
|
-
L:
|
|
405
|
+
L: 171,
|
|
555
406
|
S: this,
|
|
556
407
|
A: [
|
|
557
408
|
"this._accumulatedMessage",
|
|
@@ -560,12 +411,207 @@ var GenerationStream = class _GenerationStream {
|
|
|
560
411
|
});
|
|
561
412
|
const lastBlock = this._accumulatedMessage.content.at(-1);
|
|
562
413
|
if (lastBlock?.type === "tool_use") {
|
|
563
|
-
lastBlock.input = JSON.parse(lastBlock.inputJson ?? "");
|
|
414
|
+
lastBlock.input = lastBlock.inputJson == null || lastBlock.inputJson === "" ? {} : JSON.parse(lastBlock.inputJson ?? "");
|
|
564
415
|
}
|
|
565
416
|
break;
|
|
566
417
|
}
|
|
567
418
|
}
|
|
568
|
-
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
|
|
422
|
+
// packages/experimental/assistant/src/ai-service/types/message.ts
|
|
423
|
+
import { Schema as S } from "@effect/schema";
|
|
424
|
+
import { EchoObject, ObjectId } from "@dxos/echo-schema";
|
|
425
|
+
import { SpaceId } from "@dxos/keys";
|
|
426
|
+
var SpaceIdSchema = S.String.pipe(S.filter(SpaceId.isValid));
|
|
427
|
+
var Space = S.Struct({
|
|
428
|
+
id: SpaceIdSchema
|
|
429
|
+
});
|
|
430
|
+
var Thread = S.Struct({
|
|
431
|
+
id: ObjectId,
|
|
432
|
+
spaceId: SpaceIdSchema
|
|
433
|
+
});
|
|
434
|
+
var MessageRole = S.String.pipe(S.filter((role) => role === "user" || role === "assistant"));
|
|
435
|
+
var TagContentBlock = S.Struct({
|
|
436
|
+
type: S.Literal("tag"),
|
|
437
|
+
tag: S.String,
|
|
438
|
+
text: S.String
|
|
439
|
+
}).pipe(S.mutable);
|
|
440
|
+
var TextContentBlock = S.Struct({
|
|
441
|
+
type: S.Literal("text"),
|
|
442
|
+
text: S.String
|
|
443
|
+
}).pipe(S.mutable);
|
|
444
|
+
var ImageSource = S.Struct({
|
|
445
|
+
type: S.Literal("base64"),
|
|
446
|
+
media_type: S.Literal("image/jpeg", "image/png", "image/gif", "image/webp"),
|
|
447
|
+
data: S.String
|
|
448
|
+
}).pipe(S.mutable);
|
|
449
|
+
var ImageContentBlock = S.Struct({
|
|
450
|
+
type: S.Literal("image"),
|
|
451
|
+
id: S.optional(S.String),
|
|
452
|
+
source: S.optional(ImageSource)
|
|
453
|
+
}).pipe(S.mutable);
|
|
454
|
+
var ToolUseContentBlock = S.Struct({
|
|
455
|
+
type: S.Literal("tool_use"),
|
|
456
|
+
/**
|
|
457
|
+
* Opaque service-defined ID of the tool invocation.
|
|
458
|
+
* Used to match the tool result block.
|
|
459
|
+
*/
|
|
460
|
+
id: S.String,
|
|
461
|
+
/**
|
|
462
|
+
* Tool name.
|
|
463
|
+
*/
|
|
464
|
+
name: S.String,
|
|
465
|
+
input: S.Unknown,
|
|
466
|
+
/**
|
|
467
|
+
* Used to accumulate the partial tool input JSON in streaming mode.
|
|
468
|
+
*/
|
|
469
|
+
// TODO(burdon): Remove?
|
|
470
|
+
inputJson: S.optional(S.String)
|
|
471
|
+
}).pipe(S.mutable);
|
|
472
|
+
var ToolResultContentBlock = S.Struct({
|
|
473
|
+
type: S.Literal("tool_result"),
|
|
474
|
+
toolUseId: S.String,
|
|
475
|
+
content: S.String,
|
|
476
|
+
isError: S.optional(S.Boolean)
|
|
477
|
+
});
|
|
478
|
+
var MessageContentBlock = S.Union(TagContentBlock, TextContentBlock, ImageContentBlock, ToolUseContentBlock, ToolResultContentBlock);
|
|
479
|
+
var MessageSchema = S.Struct({
|
|
480
|
+
id: ObjectId,
|
|
481
|
+
threadId: S.optional(ObjectId),
|
|
482
|
+
spaceId: S.optional(SpaceIdSchema),
|
|
483
|
+
/**
|
|
484
|
+
* ID of the message from the foreign provider.
|
|
485
|
+
*/
|
|
486
|
+
// TODO(dmaretskyi): Should be in meta/keys.
|
|
487
|
+
foreignId: S.optional(S.String),
|
|
488
|
+
// TODO(dmaretskyi): Figure out how to deal with those.
|
|
489
|
+
// created: S.optional(S.DateFromString),
|
|
490
|
+
// updated: S.optional(S.DateFromString),
|
|
491
|
+
role: MessageRole,
|
|
492
|
+
// TODO(burdon): Rename blocks?
|
|
493
|
+
content: S.Array(MessageContentBlock).pipe(S.mutable)
|
|
494
|
+
});
|
|
495
|
+
var Message = MessageSchema.pipe(EchoObject("dxos.org/type/Message", "0.1.0"));
|
|
496
|
+
|
|
497
|
+
// packages/experimental/assistant/src/ai-service/types/schema.ts
|
|
498
|
+
import { Schema as S2 } from "@effect/schema";
|
|
499
|
+
import { JsonSchemaType, ObjectId as ObjectId2 } from "@dxos/echo-schema";
|
|
500
|
+
var LLMModel = S2.Literal("@hf/nousresearch/hermes-2-pro-mistral-7b", "@anthropic/claude-3-5-sonnet-20241022", "@anthropic/claude-3-5-haiku-20241022", "@ollama/llama-3-2-3b", "@ollama/llama-3-1-nemotron-70b-instruct", "@ollama/llama-3-1-nemotron-mini-4b-instruct", "@cf/deepseek-ai/deepseek-r1-distill-qwen-32b");
|
|
501
|
+
var ToolTypes = Object.freeze({
|
|
502
|
+
// TODO(dmaretskyi): Not implemented yet.
|
|
503
|
+
// DatabaseQuery: 'database_query',
|
|
504
|
+
TextToImage: "text_to_image"
|
|
505
|
+
});
|
|
506
|
+
var LLMTool = S2.Struct({
|
|
507
|
+
name: S2.String,
|
|
508
|
+
/**
|
|
509
|
+
* If the tool is implemented by the service a type should be provided.
|
|
510
|
+
* For user-implemented tools, this field should be omitted.
|
|
511
|
+
* See {@link ToolTypes} for the list of supported types.
|
|
512
|
+
*/
|
|
513
|
+
type: S2.optional(S2.String),
|
|
514
|
+
/**
|
|
515
|
+
* Required for user-implemented tools.
|
|
516
|
+
*/
|
|
517
|
+
description: S2.optional(S2.String),
|
|
518
|
+
/**
|
|
519
|
+
* Input schema for the tool in the JSON Schema format.
|
|
520
|
+
* Required for user-implemented tools.
|
|
521
|
+
*/
|
|
522
|
+
parameters: S2.optional(JsonSchemaType),
|
|
523
|
+
/**
|
|
524
|
+
* Tool-specific options.
|
|
525
|
+
*/
|
|
526
|
+
options: S2.optional(S2.Any),
|
|
527
|
+
/**
|
|
528
|
+
* Javascript function to execute the tool.
|
|
529
|
+
*/
|
|
530
|
+
execute: S2.optional(S2.Any)
|
|
531
|
+
});
|
|
532
|
+
var GenerateRequest = S2.Struct({
|
|
533
|
+
model: LLMModel,
|
|
534
|
+
spaceId: S2.optional(SpaceIdSchema),
|
|
535
|
+
threadId: S2.optional(ObjectId2),
|
|
536
|
+
/**
|
|
537
|
+
* History of messages to include in the context window.
|
|
538
|
+
*/
|
|
539
|
+
history: S2.optional(S2.Array(Message)),
|
|
540
|
+
/**
|
|
541
|
+
* System instructions to the LLM.
|
|
542
|
+
*/
|
|
543
|
+
// TODO(dmaretskyi): Should this be part of the thread?.
|
|
544
|
+
systemPrompt: S2.optional(S2.String),
|
|
545
|
+
/**
|
|
546
|
+
* Tools available for the LLM.
|
|
547
|
+
*/
|
|
548
|
+
tools: S2.optional(S2.Array(LLMTool).pipe(S2.mutable))
|
|
549
|
+
});
|
|
550
|
+
|
|
551
|
+
// packages/experimental/assistant/src/ai-service/client.ts
|
|
552
|
+
import { Schema as S3 } from "@effect/schema";
|
|
553
|
+
import { invariant as invariant2 } from "@dxos/invariant";
|
|
554
|
+
var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/ai-service/client.ts";
|
|
555
|
+
var AIServiceClientImpl = class {
|
|
556
|
+
constructor({ endpoint }) {
|
|
557
|
+
this._endpoint = endpoint;
|
|
558
|
+
}
|
|
559
|
+
async getSpace(spaceId) {
|
|
560
|
+
throw new Error("Not implemented");
|
|
561
|
+
}
|
|
562
|
+
async getThread(spaceId, threadId) {
|
|
563
|
+
throw new Error("Not implemented");
|
|
564
|
+
}
|
|
565
|
+
async getMessages(spaceId, threadId) {
|
|
566
|
+
const res = await fetch(`${this._endpoint}/space/${spaceId}/thread/${threadId}/message`);
|
|
567
|
+
return S3.decodePromise(S3.Array(Message).pipe(S3.mutable))((await res.json()).data.results);
|
|
568
|
+
}
|
|
569
|
+
async appendMessages(messages) {
|
|
570
|
+
const url = `${this._endpoint}/message`;
|
|
571
|
+
const res = await fetch(url, {
|
|
572
|
+
method: "POST",
|
|
573
|
+
headers: {
|
|
574
|
+
"Content-Type": "application/json"
|
|
575
|
+
},
|
|
576
|
+
body: JSON.stringify({
|
|
577
|
+
messages: await S3.encodePromise(S3.Array(Message))(messages)
|
|
578
|
+
})
|
|
579
|
+
});
|
|
580
|
+
if (!res.ok) {
|
|
581
|
+
throw new Error(`${await res.text()} [${url}]`);
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
// TODO(dmaretskyi): Accept partial message schema.
|
|
585
|
+
async updateMessage(spaceId, threadId, messageId, message) {
|
|
586
|
+
throw new Error("Not implemented");
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Open message stream.
|
|
590
|
+
*/
|
|
591
|
+
async generate(request) {
|
|
592
|
+
const controller = new AbortController();
|
|
593
|
+
const signal = controller.signal;
|
|
594
|
+
const response = await fetch(`${this._endpoint}/generate`, {
|
|
595
|
+
method: "POST",
|
|
596
|
+
headers: {
|
|
597
|
+
"Content-Type": "application/json"
|
|
598
|
+
},
|
|
599
|
+
body: JSON.stringify(request),
|
|
600
|
+
signal
|
|
601
|
+
});
|
|
602
|
+
invariant2(response.body instanceof ReadableStream, void 0, {
|
|
603
|
+
F: __dxlog_file2,
|
|
604
|
+
L: 85,
|
|
605
|
+
S: this,
|
|
606
|
+
A: [
|
|
607
|
+
"response.body instanceof ReadableStream",
|
|
608
|
+
""
|
|
609
|
+
]
|
|
610
|
+
});
|
|
611
|
+
return GenerationStream.fromSSEResponse({
|
|
612
|
+
spaceId: request.spaceId,
|
|
613
|
+
threadId: request.threadId
|
|
614
|
+
}, response, controller);
|
|
569
615
|
}
|
|
570
616
|
};
|
|
571
617
|
|
|
@@ -659,13 +705,13 @@ var parseCypherQuery = (query) => {
|
|
|
659
705
|
};
|
|
660
706
|
|
|
661
707
|
// packages/experimental/assistant/src/cypher/query-executor.ts
|
|
662
|
-
import { log as
|
|
708
|
+
import { log as log3 } from "@dxos/log";
|
|
663
709
|
import { mapValues } from "@dxos/util";
|
|
664
710
|
|
|
665
711
|
// packages/experimental/assistant/src/cypher/execution-plan.ts
|
|
666
|
-
import { invariant as
|
|
667
|
-
import { log } from "@dxos/log";
|
|
668
|
-
var
|
|
712
|
+
import { invariant as invariant3 } from "@dxos/invariant";
|
|
713
|
+
import { log as log2 } from "@dxos/log";
|
|
714
|
+
var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/cypher/execution-plan.ts";
|
|
669
715
|
var createExecutionPlan = (query) => {
|
|
670
716
|
return new ExecutionPlanBuilder().build(query);
|
|
671
717
|
};
|
|
@@ -677,10 +723,10 @@ var ExecutionPlanBuilder = class {
|
|
|
677
723
|
return `anon_${this.counter++}`;
|
|
678
724
|
}
|
|
679
725
|
build(query) {
|
|
680
|
-
|
|
726
|
+
log2("Creating execution plan for query", {
|
|
681
727
|
query
|
|
682
728
|
}, {
|
|
683
|
-
F:
|
|
729
|
+
F: __dxlog_file3,
|
|
684
730
|
L: 76,
|
|
685
731
|
S: this,
|
|
686
732
|
C: (f, a) => f(...a)
|
|
@@ -697,8 +743,8 @@ var ExecutionPlanBuilder = class {
|
|
|
697
743
|
};
|
|
698
744
|
}
|
|
699
745
|
createStepsForMatchClause(match) {
|
|
700
|
-
|
|
701
|
-
F:
|
|
746
|
+
invariant3(match.pattern.segments.length > 0, void 0, {
|
|
747
|
+
F: __dxlog_file3,
|
|
702
748
|
L: 90,
|
|
703
749
|
S: this,
|
|
704
750
|
A: [
|
|
@@ -707,8 +753,8 @@ var ExecutionPlanBuilder = class {
|
|
|
707
753
|
]
|
|
708
754
|
});
|
|
709
755
|
const firstSegment = match.pattern.segments[0];
|
|
710
|
-
|
|
711
|
-
F:
|
|
756
|
+
invariant3(firstSegment.astKind === "NodePattern", void 0, {
|
|
757
|
+
F: __dxlog_file3,
|
|
712
758
|
L: 92,
|
|
713
759
|
S: this,
|
|
714
760
|
A: [
|
|
@@ -735,8 +781,8 @@ var ExecutionPlanBuilder = class {
|
|
|
735
781
|
}
|
|
736
782
|
for (let i = 1; i < match.pattern.segments.length; i++) {
|
|
737
783
|
const relationshipSegment = match.pattern.segments[i++];
|
|
738
|
-
|
|
739
|
-
F:
|
|
784
|
+
invariant3(relationshipSegment.astKind === "RelationshipPattern", void 0, {
|
|
785
|
+
F: __dxlog_file3,
|
|
740
786
|
L: 115,
|
|
741
787
|
S: this,
|
|
742
788
|
A: [
|
|
@@ -745,8 +791,8 @@ var ExecutionPlanBuilder = class {
|
|
|
745
791
|
]
|
|
746
792
|
});
|
|
747
793
|
const nodeSegment = match.pattern.segments[i];
|
|
748
|
-
|
|
749
|
-
F:
|
|
794
|
+
invariant3(nodeSegment.astKind === "NodePattern", void 0, {
|
|
795
|
+
F: __dxlog_file3,
|
|
750
796
|
L: 117,
|
|
751
797
|
S: this,
|
|
752
798
|
A: [
|
|
@@ -829,15 +875,15 @@ var getDirection = (left, right) => {
|
|
|
829
875
|
};
|
|
830
876
|
|
|
831
877
|
// packages/experimental/assistant/src/cypher/query-executor.ts
|
|
832
|
-
var
|
|
878
|
+
var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/cypher/query-executor.ts";
|
|
833
879
|
var executeQueryPlan = async (dataSource, plan) => {
|
|
834
880
|
let buffer = [];
|
|
835
881
|
const results = [];
|
|
836
882
|
for (const step of plan.steps) {
|
|
837
|
-
|
|
883
|
+
log3("begin step", {
|
|
838
884
|
step
|
|
839
885
|
}, {
|
|
840
|
-
F:
|
|
886
|
+
F: __dxlog_file4,
|
|
841
887
|
L: 41,
|
|
842
888
|
S: void 0,
|
|
843
889
|
C: (f, a) => f(...a)
|
|
@@ -923,11 +969,11 @@ var executeQueryPlan = async (dataSource, plan) => {
|
|
|
923
969
|
break;
|
|
924
970
|
}
|
|
925
971
|
}
|
|
926
|
-
|
|
972
|
+
log3("finish step", {
|
|
927
973
|
buffer: buffer.map((tuple) => mapValues(tuple, (x) => x.id)),
|
|
928
974
|
results
|
|
929
975
|
}, {
|
|
930
|
-
F:
|
|
976
|
+
F: __dxlog_file4,
|
|
931
977
|
L: 118,
|
|
932
978
|
S: void 0,
|
|
933
979
|
C: (f, a) => f(...a)
|
|
@@ -1025,14 +1071,14 @@ import { raise as raise2 } from "@dxos/debug";
|
|
|
1025
1071
|
import { Filter } from "@dxos/echo-db";
|
|
1026
1072
|
import { getSchemaTypename, StoredSchema, toJsonSchema } from "@dxos/echo-schema";
|
|
1027
1073
|
import { getSchema } from "@dxos/live-object";
|
|
1028
|
-
import { log as
|
|
1074
|
+
import { log as log4 } from "@dxos/log";
|
|
1029
1075
|
function _ts_decorate(decorators, target, key, desc) {
|
|
1030
1076
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
1031
1077
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
1032
1078
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
1033
1079
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
1034
1080
|
}
|
|
1035
|
-
var
|
|
1081
|
+
var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/cypher/echo-data-source.ts";
|
|
1036
1082
|
var EchoDataSource = class {
|
|
1037
1083
|
constructor(_db) {
|
|
1038
1084
|
this._db = _db;
|
|
@@ -1106,62 +1152,143 @@ var EchoDataSource = class {
|
|
|
1106
1152
|
}
|
|
1107
1153
|
};
|
|
1108
1154
|
_ts_decorate([
|
|
1109
|
-
|
|
1110
|
-
F:
|
|
1155
|
+
log4.method(void 0, void 0, {
|
|
1156
|
+
F: __dxlog_file5,
|
|
1111
1157
|
L: 17,
|
|
1112
1158
|
S: void 0,
|
|
1113
1159
|
C: (f, a) => f(...a)
|
|
1114
1160
|
})
|
|
1115
1161
|
], EchoDataSource.prototype, "getNodes", null);
|
|
1116
1162
|
_ts_decorate([
|
|
1117
|
-
|
|
1118
|
-
F:
|
|
1163
|
+
log4.method(void 0, void 0, {
|
|
1164
|
+
F: __dxlog_file5,
|
|
1119
1165
|
L: 34,
|
|
1120
1166
|
S: void 0,
|
|
1121
1167
|
C: (f, a) => f(...a)
|
|
1122
1168
|
})
|
|
1123
1169
|
], EchoDataSource.prototype, "getRelationships", null);
|
|
1124
1170
|
|
|
1171
|
+
// packages/experimental/assistant/src/conversation/types.ts
|
|
1172
|
+
import { Schema as S4 } from "@effect/schema";
|
|
1173
|
+
import { toJsonSchema as toJsonSchema2, JsonSchemaType as JsonSchemaType2, ObjectId as ObjectId3 } from "@dxos/echo-schema";
|
|
1174
|
+
import { log as log5 } from "@dxos/log";
|
|
1175
|
+
var __dxlog_file6 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/conversation/types.ts";
|
|
1176
|
+
var createUserMessage = (spaceId, threadId, text) => ({
|
|
1177
|
+
id: ObjectId3.random(),
|
|
1178
|
+
spaceId,
|
|
1179
|
+
threadId,
|
|
1180
|
+
role: "user",
|
|
1181
|
+
content: [
|
|
1182
|
+
{
|
|
1183
|
+
type: "text",
|
|
1184
|
+
text
|
|
1185
|
+
}
|
|
1186
|
+
]
|
|
1187
|
+
});
|
|
1188
|
+
var LLMToolDefinition = S4.Struct({
|
|
1189
|
+
name: S4.String,
|
|
1190
|
+
description: S4.String,
|
|
1191
|
+
parameters: JsonSchemaType2,
|
|
1192
|
+
execute: S4.Any
|
|
1193
|
+
});
|
|
1194
|
+
var LLMToolResult = Object.freeze({
|
|
1195
|
+
/**
|
|
1196
|
+
* The tool execution was successful.
|
|
1197
|
+
* Gives the result back to the LLM.
|
|
1198
|
+
*/
|
|
1199
|
+
Success: (result, extractContentBlocks) => ({
|
|
1200
|
+
kind: "success",
|
|
1201
|
+
result,
|
|
1202
|
+
extractContentBlocks
|
|
1203
|
+
}),
|
|
1204
|
+
Error: (message) => ({
|
|
1205
|
+
kind: "error",
|
|
1206
|
+
message
|
|
1207
|
+
}),
|
|
1208
|
+
/**
|
|
1209
|
+
* Stop the conversation and return the result.
|
|
1210
|
+
*/
|
|
1211
|
+
Break: (result) => ({
|
|
1212
|
+
kind: "break",
|
|
1213
|
+
result
|
|
1214
|
+
})
|
|
1215
|
+
});
|
|
1216
|
+
var defineTool = ({ name, description, schema, execute }) => {
|
|
1217
|
+
return {
|
|
1218
|
+
name,
|
|
1219
|
+
description,
|
|
1220
|
+
parameters: toFunctionParameterSchema(schema),
|
|
1221
|
+
execute: (params, context) => {
|
|
1222
|
+
const sanitized = S4.decodeSync(schema)(params);
|
|
1223
|
+
return execute(sanitized, context);
|
|
1224
|
+
}
|
|
1225
|
+
};
|
|
1226
|
+
};
|
|
1227
|
+
var toFunctionParameterSchema = (schema) => {
|
|
1228
|
+
const jsonSchema = toJsonSchema2(schema);
|
|
1229
|
+
log5("tool schema", {
|
|
1230
|
+
jsonSchema
|
|
1231
|
+
}, {
|
|
1232
|
+
F: __dxlog_file6,
|
|
1233
|
+
L: 120,
|
|
1234
|
+
S: void 0,
|
|
1235
|
+
C: (f, a) => f(...a)
|
|
1236
|
+
});
|
|
1237
|
+
delete jsonSchema.anyOf;
|
|
1238
|
+
delete jsonSchema.$id;
|
|
1239
|
+
jsonSchema.type = "object";
|
|
1240
|
+
jsonSchema.properties ??= {};
|
|
1241
|
+
for (const key in jsonSchema.properties) {
|
|
1242
|
+
if (typeof jsonSchema.properties[key].description !== "string") {
|
|
1243
|
+
throw new Error(`Property missing description: ${key}`);
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
return jsonSchema;
|
|
1247
|
+
};
|
|
1248
|
+
|
|
1125
1249
|
// packages/experimental/assistant/src/conversation/conversation.ts
|
|
1126
|
-
import {
|
|
1127
|
-
import {
|
|
1128
|
-
|
|
1250
|
+
import { createStatic, ObjectId as ObjectId4 } from "@dxos/echo-schema";
|
|
1251
|
+
import { invariant as invariant4 } from "@dxos/invariant";
|
|
1252
|
+
import { log as log6 } from "@dxos/log";
|
|
1253
|
+
var __dxlog_file7 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/conversation/conversation.ts";
|
|
1129
1254
|
var runLLM = async (params) => {
|
|
1130
1255
|
let conversationResult = null;
|
|
1256
|
+
const history = params.history ?? [];
|
|
1131
1257
|
const generate = async () => {
|
|
1132
|
-
|
|
1258
|
+
log6("llm generate", {
|
|
1133
1259
|
tools: params.tools
|
|
1134
1260
|
}, {
|
|
1135
|
-
F:
|
|
1136
|
-
L:
|
|
1261
|
+
F: __dxlog_file7,
|
|
1262
|
+
L: 45,
|
|
1137
1263
|
S: void 0,
|
|
1138
1264
|
C: (f, a) => f(...a)
|
|
1139
1265
|
});
|
|
1140
1266
|
const beginTs = Date.now();
|
|
1141
|
-
const
|
|
1267
|
+
const stream = await params.client.generate({
|
|
1142
1268
|
model: params.model,
|
|
1143
1269
|
spaceId: params.spaceId,
|
|
1144
1270
|
threadId: params.threadId,
|
|
1271
|
+
history,
|
|
1145
1272
|
systemPrompt: params.system,
|
|
1146
1273
|
tools: params.tools
|
|
1147
1274
|
});
|
|
1148
|
-
for await (const event of
|
|
1275
|
+
for await (const event of stream) {
|
|
1149
1276
|
params.logger?.(event);
|
|
1150
1277
|
}
|
|
1151
|
-
const messages = await
|
|
1278
|
+
const messages = await stream.complete();
|
|
1152
1279
|
const message = messages.at(-1);
|
|
1153
|
-
|
|
1280
|
+
log6("llm result", {
|
|
1154
1281
|
time: Date.now() - beginTs,
|
|
1155
1282
|
message
|
|
1156
1283
|
}, {
|
|
1157
|
-
F:
|
|
1158
|
-
L:
|
|
1284
|
+
F: __dxlog_file7,
|
|
1285
|
+
L: 62,
|
|
1159
1286
|
S: void 0,
|
|
1160
1287
|
C: (f, a) => f(...a)
|
|
1161
1288
|
});
|
|
1162
|
-
|
|
1163
|
-
F:
|
|
1164
|
-
L:
|
|
1289
|
+
invariant4(message, void 0, {
|
|
1290
|
+
F: __dxlog_file7,
|
|
1291
|
+
L: 63,
|
|
1165
1292
|
S: void 0,
|
|
1166
1293
|
A: [
|
|
1167
1294
|
"message",
|
|
@@ -1172,16 +1299,16 @@ var runLLM = async (params) => {
|
|
|
1172
1299
|
type: "message",
|
|
1173
1300
|
message
|
|
1174
1301
|
});
|
|
1175
|
-
|
|
1176
|
-
...
|
|
1177
|
-
content:
|
|
1302
|
+
history.push(...messages.map((message2) => ({
|
|
1303
|
+
...message2,
|
|
1304
|
+
content: message2.content.filter((block) => block.type !== "image")
|
|
1178
1305
|
})));
|
|
1179
|
-
const
|
|
1180
|
-
if (
|
|
1306
|
+
const isToolUse2 = message.content.at(-1)?.type === "tool_use";
|
|
1307
|
+
if (isToolUse2) {
|
|
1181
1308
|
const toolCalls = message.content.filter((c) => c.type === "tool_use");
|
|
1182
|
-
|
|
1183
|
-
F:
|
|
1184
|
-
L:
|
|
1309
|
+
invariant4(toolCalls.length === 1, void 0, {
|
|
1310
|
+
F: __dxlog_file7,
|
|
1311
|
+
L: 74,
|
|
1185
1312
|
S: void 0,
|
|
1186
1313
|
A: [
|
|
1187
1314
|
"toolCalls.length === 1",
|
|
@@ -1189,23 +1316,23 @@ var runLLM = async (params) => {
|
|
|
1189
1316
|
]
|
|
1190
1317
|
});
|
|
1191
1318
|
const toolCall = toolCalls[0];
|
|
1192
|
-
const tool = params.tools.find((
|
|
1319
|
+
const tool = params.tools.find((tool2) => tool2.name === toolCall.name);
|
|
1193
1320
|
if (!tool) {
|
|
1194
1321
|
throw new Error(`Tool not found: ${toolCall.name}`);
|
|
1195
1322
|
}
|
|
1196
1323
|
const toolResult = await tool.execute(toolCall.input, {});
|
|
1197
1324
|
switch (toolResult.kind) {
|
|
1198
1325
|
case "error": {
|
|
1199
|
-
|
|
1326
|
+
log6.warn("tool error", {
|
|
1200
1327
|
message: toolResult.message
|
|
1201
1328
|
}, {
|
|
1202
|
-
F:
|
|
1203
|
-
L:
|
|
1329
|
+
F: __dxlog_file7,
|
|
1330
|
+
L: 84,
|
|
1204
1331
|
S: void 0,
|
|
1205
1332
|
C: (f, a) => f(...a)
|
|
1206
1333
|
});
|
|
1207
1334
|
const resultMessage = {
|
|
1208
|
-
id:
|
|
1335
|
+
id: ObjectId4.random(),
|
|
1209
1336
|
spaceId: params.spaceId,
|
|
1210
1337
|
threadId: params.threadId,
|
|
1211
1338
|
role: "user",
|
|
@@ -1222,22 +1349,20 @@ var runLLM = async (params) => {
|
|
|
1222
1349
|
type: "message",
|
|
1223
1350
|
message: resultMessage
|
|
1224
1351
|
});
|
|
1225
|
-
|
|
1226
|
-
resultMessage
|
|
1227
|
-
]);
|
|
1352
|
+
history.push(resultMessage);
|
|
1228
1353
|
return true;
|
|
1229
1354
|
}
|
|
1230
1355
|
case "success": {
|
|
1231
|
-
|
|
1356
|
+
log6("tool success", {
|
|
1232
1357
|
result: toolResult.result
|
|
1233
1358
|
}, {
|
|
1234
|
-
F:
|
|
1235
|
-
L:
|
|
1359
|
+
F: __dxlog_file7,
|
|
1360
|
+
L: 106,
|
|
1236
1361
|
S: void 0,
|
|
1237
1362
|
C: (f, a) => f(...a)
|
|
1238
1363
|
});
|
|
1239
1364
|
const resultMessage = {
|
|
1240
|
-
id:
|
|
1365
|
+
id: ObjectId4.random(),
|
|
1241
1366
|
spaceId: params.spaceId,
|
|
1242
1367
|
threadId: params.threadId,
|
|
1243
1368
|
role: "user",
|
|
@@ -1253,17 +1378,15 @@ var runLLM = async (params) => {
|
|
|
1253
1378
|
type: "message",
|
|
1254
1379
|
message: resultMessage
|
|
1255
1380
|
});
|
|
1256
|
-
|
|
1257
|
-
resultMessage
|
|
1258
|
-
]);
|
|
1381
|
+
history.push(resultMessage);
|
|
1259
1382
|
return true;
|
|
1260
1383
|
}
|
|
1261
1384
|
case "break": {
|
|
1262
|
-
|
|
1385
|
+
log6("tool break", {
|
|
1263
1386
|
result: toolResult.result
|
|
1264
1387
|
}, {
|
|
1265
|
-
F:
|
|
1266
|
-
L:
|
|
1388
|
+
F: __dxlog_file7,
|
|
1389
|
+
L: 127,
|
|
1267
1390
|
S: void 0,
|
|
1268
1391
|
C: (f, a) => f(...a)
|
|
1269
1392
|
});
|
|
@@ -1277,105 +1400,129 @@ var runLLM = async (params) => {
|
|
|
1277
1400
|
while (await generate()) {
|
|
1278
1401
|
}
|
|
1279
1402
|
return {
|
|
1280
|
-
result: conversationResult
|
|
1403
|
+
result: conversationResult,
|
|
1404
|
+
history
|
|
1281
1405
|
};
|
|
1282
1406
|
};
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
var __dxlog_file6 = "/Users/yarolegovich/Documents/Workspace/dxos/packages/experimental/assistant/src/conversation/types.ts";
|
|
1290
|
-
var createUserMessage = (spaceId, threadId, text) => ({
|
|
1291
|
-
id: ObjectId.random(),
|
|
1292
|
-
spaceId,
|
|
1293
|
-
threadId,
|
|
1294
|
-
role: "user",
|
|
1295
|
-
content: [
|
|
1296
|
-
{
|
|
1297
|
-
type: "text",
|
|
1298
|
-
text
|
|
1299
|
-
}
|
|
1300
|
-
]
|
|
1301
|
-
});
|
|
1302
|
-
var LLMToolDefinition = S3.Struct({
|
|
1303
|
-
name: S3.String,
|
|
1304
|
-
description: S3.String,
|
|
1305
|
-
parameters: JsonSchemaType2,
|
|
1306
|
-
execute: S3.Any
|
|
1307
|
-
});
|
|
1308
|
-
var LLMToolResult = Object.freeze({
|
|
1309
|
-
/**
|
|
1310
|
-
* The tool execution was successful.
|
|
1311
|
-
* Gives the result back to the LLM.
|
|
1312
|
-
*/
|
|
1313
|
-
Success: (result) => ({
|
|
1314
|
-
kind: "success",
|
|
1315
|
-
result
|
|
1316
|
-
}),
|
|
1317
|
-
Error: (message) => ({
|
|
1318
|
-
kind: "error",
|
|
1319
|
-
message
|
|
1320
|
-
}),
|
|
1321
|
-
/**
|
|
1322
|
-
* Stop the conversation and return the result.
|
|
1323
|
-
*/
|
|
1324
|
-
Break: (result) => ({
|
|
1325
|
-
kind: "break",
|
|
1326
|
-
result
|
|
1327
|
-
})
|
|
1328
|
-
});
|
|
1329
|
-
var defineTool = ({ name, description, schema, execute }) => {
|
|
1330
|
-
return {
|
|
1331
|
-
name,
|
|
1332
|
-
description,
|
|
1333
|
-
parameters: toFunctionParameterSchema(schema),
|
|
1334
|
-
execute: (params, context) => {
|
|
1335
|
-
const sanitized = S3.decodeSync(schema)(params);
|
|
1336
|
-
return execute(sanitized, context);
|
|
1337
|
-
}
|
|
1338
|
-
};
|
|
1339
|
-
};
|
|
1340
|
-
var toFunctionParameterSchema = (schema) => {
|
|
1341
|
-
const jsonSchema = toJsonSchema2(schema);
|
|
1342
|
-
log5("tool schema", {
|
|
1343
|
-
jsonSchema
|
|
1344
|
-
}, {
|
|
1345
|
-
F: __dxlog_file6,
|
|
1346
|
-
L: 83,
|
|
1407
|
+
var isToolUse = (message) => message.content.at(-1)?.type === "tool_use";
|
|
1408
|
+
var runTools = async ({ message, tools, extensions }) => {
|
|
1409
|
+
const toolCalls = message.content.filter((block) => block.type === "tool_use");
|
|
1410
|
+
invariant4(toolCalls.length === 1, void 0, {
|
|
1411
|
+
F: __dxlog_file7,
|
|
1412
|
+
L: 165,
|
|
1347
1413
|
S: void 0,
|
|
1348
|
-
|
|
1414
|
+
A: [
|
|
1415
|
+
"toolCalls.length === 1",
|
|
1416
|
+
""
|
|
1417
|
+
]
|
|
1349
1418
|
});
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1419
|
+
const toolCall = toolCalls[0];
|
|
1420
|
+
const tool = tools.find((tool2) => tool2.name === toolCall.name);
|
|
1421
|
+
if (!tool) {
|
|
1422
|
+
throw new Error(`Tool not found: ${toolCall.name}`);
|
|
1423
|
+
}
|
|
1424
|
+
let toolResult;
|
|
1425
|
+
try {
|
|
1426
|
+
toolResult = await tool.execute(toolCall.input, {
|
|
1427
|
+
extensions
|
|
1428
|
+
});
|
|
1429
|
+
} catch (error) {
|
|
1430
|
+
log6("tool error", {
|
|
1431
|
+
error
|
|
1432
|
+
}, {
|
|
1433
|
+
F: __dxlog_file7,
|
|
1434
|
+
L: 176,
|
|
1435
|
+
S: void 0,
|
|
1436
|
+
C: (f, a) => f(...a)
|
|
1437
|
+
});
|
|
1438
|
+
toolResult = LLMToolResult.Error(error.message);
|
|
1439
|
+
}
|
|
1440
|
+
switch (toolResult.kind) {
|
|
1441
|
+
case "error": {
|
|
1442
|
+
log6("tool error", {
|
|
1443
|
+
message: toolResult.message
|
|
1444
|
+
}, {
|
|
1445
|
+
F: __dxlog_file7,
|
|
1446
|
+
L: 182,
|
|
1447
|
+
S: void 0,
|
|
1448
|
+
C: (f, a) => f(...a)
|
|
1449
|
+
});
|
|
1450
|
+
const resultMessage = createStatic(Message, {
|
|
1451
|
+
role: "user",
|
|
1452
|
+
content: [
|
|
1453
|
+
{
|
|
1454
|
+
type: "tool_result",
|
|
1455
|
+
toolUseId: toolCall.id,
|
|
1456
|
+
content: toolResult.message,
|
|
1457
|
+
isError: true
|
|
1458
|
+
}
|
|
1459
|
+
]
|
|
1460
|
+
});
|
|
1461
|
+
return {
|
|
1462
|
+
type: "continue",
|
|
1463
|
+
message: resultMessage
|
|
1464
|
+
};
|
|
1465
|
+
}
|
|
1466
|
+
case "success": {
|
|
1467
|
+
log6("tool success", {
|
|
1468
|
+
result: toolResult.result
|
|
1469
|
+
}, {
|
|
1470
|
+
F: __dxlog_file7,
|
|
1471
|
+
L: 203,
|
|
1472
|
+
S: void 0,
|
|
1473
|
+
C: (f, a) => f(...a)
|
|
1474
|
+
});
|
|
1475
|
+
const resultMessage = createStatic(Message, {
|
|
1476
|
+
role: "user",
|
|
1477
|
+
content: [
|
|
1478
|
+
{
|
|
1479
|
+
type: "tool_result",
|
|
1480
|
+
toolUseId: toolCall.id,
|
|
1481
|
+
content: typeof toolResult.result === "string" ? toolResult.result : JSON.stringify(toolResult.result)
|
|
1482
|
+
}
|
|
1483
|
+
]
|
|
1484
|
+
});
|
|
1485
|
+
return {
|
|
1486
|
+
type: "continue",
|
|
1487
|
+
message: resultMessage
|
|
1488
|
+
};
|
|
1489
|
+
}
|
|
1490
|
+
case "break": {
|
|
1491
|
+
log6("tool break", {
|
|
1492
|
+
result: toolResult.result
|
|
1493
|
+
}, {
|
|
1494
|
+
F: __dxlog_file7,
|
|
1495
|
+
L: 222,
|
|
1496
|
+
S: void 0,
|
|
1497
|
+
C: (f, a) => f(...a)
|
|
1498
|
+
});
|
|
1499
|
+
return {
|
|
1500
|
+
type: "break",
|
|
1501
|
+
result: toolResult.result
|
|
1502
|
+
};
|
|
1353
1503
|
}
|
|
1354
1504
|
}
|
|
1355
|
-
return jsonSchema;
|
|
1356
1505
|
};
|
|
1357
1506
|
|
|
1358
1507
|
export {
|
|
1508
|
+
GenerationStream,
|
|
1359
1509
|
SpaceIdSchema,
|
|
1360
|
-
ObjectIdBrand,
|
|
1361
|
-
ObjectId,
|
|
1362
1510
|
Space,
|
|
1363
1511
|
Thread,
|
|
1364
1512
|
MessageRole,
|
|
1365
|
-
|
|
1513
|
+
TagContentBlock,
|
|
1514
|
+
TextContentBlock,
|
|
1366
1515
|
ImageSource,
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1516
|
+
ImageContentBlock,
|
|
1517
|
+
ToolUseContentBlock,
|
|
1518
|
+
ToolResultContentBlock,
|
|
1370
1519
|
MessageContentBlock,
|
|
1371
1520
|
Message,
|
|
1372
|
-
MessageFromDb,
|
|
1373
1521
|
LLMModel,
|
|
1374
1522
|
ToolTypes,
|
|
1375
1523
|
LLMTool,
|
|
1376
1524
|
GenerateRequest,
|
|
1377
1525
|
AIServiceClientImpl,
|
|
1378
|
-
GenerationStream,
|
|
1379
1526
|
parseCypherQuery,
|
|
1380
1527
|
executeQueryPlan,
|
|
1381
1528
|
executeQuery,
|
|
@@ -1385,10 +1532,12 @@ export {
|
|
|
1385
1532
|
formatInferredRelationshipLabel,
|
|
1386
1533
|
isReference,
|
|
1387
1534
|
EchoDataSource,
|
|
1388
|
-
runLLM,
|
|
1389
1535
|
createUserMessage,
|
|
1390
1536
|
LLMToolDefinition,
|
|
1391
1537
|
LLMToolResult,
|
|
1392
|
-
defineTool
|
|
1538
|
+
defineTool,
|
|
1539
|
+
runLLM,
|
|
1540
|
+
isToolUse,
|
|
1541
|
+
runTools
|
|
1393
1542
|
};
|
|
1394
|
-
//# sourceMappingURL=chunk-
|
|
1543
|
+
//# sourceMappingURL=chunk-LVLSJQB3.mjs.map
|