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