@dxos/assistant 0.8.4-main.406dc2a → 0.8.4-main.548089c
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-J5LGTIGS.mjs +10 -0
- package/dist/lib/browser/extraction/index.mjs +114 -161
- package/dist/lib/browser/extraction/index.mjs.map +4 -4
- package/dist/lib/browser/index.mjs +508 -151
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
- package/dist/lib/node-esm/extraction/index.mjs +114 -161
- package/dist/lib/node-esm/extraction/index.mjs.map +4 -4
- package/dist/lib/node-esm/index.mjs +508 -151
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/conversation/conversation.d.ts +4 -7
- package/dist/types/src/conversation/conversation.d.ts.map +1 -1
- package/dist/types/src/diff.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +8 -16
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/execution-graph.d.ts +3 -0
- package/dist/types/src/execution-graph.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +28 -24
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction.d.ts +14 -7
- package/dist/types/src/extraction/extraction.d.ts.map +1 -1
- package/dist/types/src/functions/services.d.ts +2 -1
- package/dist/types/src/functions/services.d.ts.map +1 -1
- package/dist/types/src/session/GenericToolkit.d.ts +84 -0
- package/dist/types/src/session/GenericToolkit.d.ts.map +1 -0
- package/dist/types/src/session/format.d.ts +16 -16
- package/dist/types/src/session/format.d.ts.map +1 -1
- package/dist/types/src/session/index.d.ts +1 -0
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/observer.d.ts +2 -2
- package/dist/types/src/session/observer.d.ts.map +1 -1
- package/dist/types/src/session/session.d.ts +3 -3
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/session/version-pin.d.ts +1 -1
- package/dist/types/src/session/version-pin.d.ts.map +1 -1
- package/dist/types/src/types/artifact.d.ts +4 -0
- package/dist/types/src/types/artifact.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +22 -20
- package/src/conversation/conversation.ts +8 -12
- package/src/diff.test.ts +3 -3
- package/src/diff.ts +5 -0
- package/src/execution-graph.test.ts +44 -42
- package/src/execution-graph.ts +19 -10
- package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +2 -2
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +44 -54
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +10 -9
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +36 -44
- package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +10 -5
- package/src/extraction/extraction.ts +5 -4
- package/src/functions/services.ts +11 -7
- package/src/session/GenericToolkit.ts +130 -0
- package/src/session/format.test.ts +4 -3
- package/src/session/format.ts +4 -5
- package/src/session/index.ts +2 -0
- package/src/session/observer.ts +2 -2
- package/src/session/session.test.ts +2 -2
- package/src/session/session.ts +9 -8
- package/src/session/version-pin.ts +2 -3
- package/src/templates/{format.tpl → instructions/format.tpl} +1 -0
- package/src/templates/system.ts +2 -2
- package/src/types/artifact.ts +20 -1
- package/dist/lib/browser/chunk-CWGVG235.mjs +0 -380
- package/dist/lib/browser/chunk-CWGVG235.mjs.map +0 -7
- package/dist/lib/browser/chunk-UB26N5UF.mjs +0 -89
- package/dist/lib/browser/chunk-UB26N5UF.mjs.map +0 -7
- package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs +0 -13
- package/dist/lib/node-esm/chunk-BCFXTNSK.mjs +0 -91
- package/dist/lib/node-esm/chunk-BCFXTNSK.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-OV5PMQPB.mjs +0 -382
- package/dist/lib/node-esm/chunk-OV5PMQPB.mjs.map +0 -7
- package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs +0 -14
- package/dist/types/src/summarization/summarization.test.d.ts +0 -2
- package/dist/types/src/summarization/summarization.test.d.ts.map +0 -1
- package/src/summarization/summarization.test.ts +0 -202
- /package/dist/lib/browser/{named-entity-recognition-QPYHML66.mjs.map → chunk-J5LGTIGS.mjs.map} +0 -0
- /package/dist/lib/node-esm/{named-entity-recognition-2HDF46YX.mjs.map → chunk-HSLMI22Q.mjs.map} +0 -0
- /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
- /package/src/templates/{system.tpl → instructions/system.tpl} +0 -0
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
AiSession,
|
|
5
|
-
ArtifactDiffResolver,
|
|
6
|
-
GenerationObserver,
|
|
7
|
-
VersionPin,
|
|
8
|
-
createToolkit
|
|
9
|
-
} from "./chunk-CWGVG235.mjs";
|
|
2
|
+
__export
|
|
3
|
+
} from "./chunk-J5LGTIGS.mjs";
|
|
10
4
|
|
|
11
5
|
// src/conversation/context.ts
|
|
12
6
|
import { computed } from "@preact/signals-core";
|
|
@@ -19,19 +13,6 @@ import { Resource } from "@dxos/context";
|
|
|
19
13
|
import { DXN, Filter, Obj, Query, Type } from "@dxos/echo";
|
|
20
14
|
import { invariant } from "@dxos/invariant";
|
|
21
15
|
import { ComplexSet } from "@dxos/util";
|
|
22
|
-
function _define_property(obj, key, value) {
|
|
23
|
-
if (key in obj) {
|
|
24
|
-
Object.defineProperty(obj, key, {
|
|
25
|
-
value,
|
|
26
|
-
enumerable: true,
|
|
27
|
-
configurable: true,
|
|
28
|
-
writable: true
|
|
29
|
-
});
|
|
30
|
-
} else {
|
|
31
|
-
obj[key] = value;
|
|
32
|
-
}
|
|
33
|
-
return obj;
|
|
34
|
-
}
|
|
35
16
|
var __dxlog_file = "/__w/dxos/dxos/packages/core/assistant/src/conversation/context.ts";
|
|
36
17
|
var ContextBinding = Schema.Struct({
|
|
37
18
|
blueprints: Schema.Struct({
|
|
@@ -48,18 +29,28 @@ var ContextBinding = Schema.Struct({
|
|
|
48
29
|
version: "0.1.0"
|
|
49
30
|
}));
|
|
50
31
|
var Bindings = class {
|
|
32
|
+
blueprints = new ComplexSet((ref) => ref.dxn.toString());
|
|
33
|
+
// TODO(burdon): Some DXNs have the Space prefix so only compare the object ID.
|
|
34
|
+
objects = new ComplexSet((ref) => ref.dxn.asEchoDXN()?.echoId);
|
|
51
35
|
toJSON() {
|
|
52
36
|
return {
|
|
53
37
|
blueprints: Array2.fromIterable(this.blueprints).map((ref) => ref.dxn.toString()),
|
|
54
38
|
objects: Array2.fromIterable(this.objects).map((ref) => ref.dxn.toString())
|
|
55
39
|
};
|
|
56
40
|
}
|
|
57
|
-
constructor() {
|
|
58
|
-
_define_property(this, "blueprints", new ComplexSet((ref) => ref.dxn.toString()));
|
|
59
|
-
_define_property(this, "objects", new ComplexSet((ref) => ref.dxn.asEchoDXN()?.echoId));
|
|
60
|
-
}
|
|
61
41
|
};
|
|
62
42
|
var AiContextBinder = class extends Resource {
|
|
43
|
+
_queue;
|
|
44
|
+
/**
|
|
45
|
+
* Reactive query of all bindings.
|
|
46
|
+
*/
|
|
47
|
+
// TODO(burdon): Cache value?
|
|
48
|
+
_bindings;
|
|
49
|
+
_blueprints;
|
|
50
|
+
_objects;
|
|
51
|
+
constructor(_queue) {
|
|
52
|
+
super(), this._queue = _queue;
|
|
53
|
+
}
|
|
63
54
|
get bindings() {
|
|
64
55
|
invariant(this._bindings, "AiContextBinder not open", {
|
|
65
56
|
F: __dxlog_file,
|
|
@@ -173,41 +164,412 @@ var AiContextBinder = class extends Resource {
|
|
|
173
164
|
return context;
|
|
174
165
|
}));
|
|
175
166
|
}
|
|
176
|
-
constructor(_queue) {
|
|
177
|
-
super(), _define_property(this, "_queue", void 0), /**
|
|
178
|
-
* Reactive query of all bindings.
|
|
179
|
-
*/
|
|
180
|
-
// TODO(burdon): Cache value?
|
|
181
|
-
_define_property(this, "_bindings", void 0), _define_property(this, "_blueprints", void 0), _define_property(this, "_objects", void 0), this._queue = _queue;
|
|
182
|
-
}
|
|
183
167
|
};
|
|
184
168
|
var AiContextService = class extends Context.Tag("@dxos/assistant/AiContextService")() {
|
|
185
169
|
};
|
|
186
170
|
|
|
187
171
|
// src/conversation/conversation.ts
|
|
188
|
-
import * as
|
|
172
|
+
import * as Array5 from "effect/Array";
|
|
173
|
+
import * as Effect6 from "effect/Effect";
|
|
174
|
+
import * as Option2 from "effect/Option";
|
|
175
|
+
import { Resource as Resource2 } from "@dxos/context";
|
|
176
|
+
import { Obj as Obj4 } from "@dxos/echo";
|
|
177
|
+
import { DatabaseService as DatabaseService2 } from "@dxos/echo-db";
|
|
178
|
+
import { log as log3 } from "@dxos/log";
|
|
179
|
+
import { Message as Message3 } from "@dxos/types";
|
|
180
|
+
|
|
181
|
+
// src/session/artifact-diff.ts
|
|
182
|
+
import * as Context2 from "effect/Context";
|
|
183
|
+
var ArtifactDiffResolver = class extends Context2.Tag("@dxos/assistant/ArtifactDiffResolver")() {
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
// src/session/observer.ts
|
|
189
187
|
import * as Effect from "effect/Effect";
|
|
188
|
+
import * as Function2 from "effect/Function";
|
|
189
|
+
var GenerationObserver = Object.freeze({
|
|
190
|
+
make: ({ onBegin = Function2.constant(Effect.void), onPart = Function2.constant(Effect.void), onBlock = Function2.constant(Effect.void), onEnd = Function2.constant(Effect.void), onMessage = Function2.constant(Effect.void) } = {}) => ({
|
|
191
|
+
onBegin,
|
|
192
|
+
onPart,
|
|
193
|
+
onBlock,
|
|
194
|
+
onEnd,
|
|
195
|
+
onMessage
|
|
196
|
+
}),
|
|
197
|
+
noop: () => GenerationObserver.make(),
|
|
198
|
+
/**
|
|
199
|
+
* Debug printer to be used in unit-tests and browser devtools.
|
|
200
|
+
*/
|
|
201
|
+
fromPrinter: (printer) => GenerationObserver.make({
|
|
202
|
+
onBlock: (block) => Effect.sync(() => {
|
|
203
|
+
if (block.pending) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
printer.printContentBlock(block);
|
|
207
|
+
}),
|
|
208
|
+
onMessage: (message) => Effect.sync(() => {
|
|
209
|
+
if (message.sender.role === "assistant") {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
printer.printMessage(message);
|
|
213
|
+
})
|
|
214
|
+
})
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
// src/session/session.ts
|
|
218
|
+
import * as LanguageModel from "@effect/ai/LanguageModel";
|
|
219
|
+
import * as Chunk from "effect/Chunk";
|
|
220
|
+
import * as Effect3 from "effect/Effect";
|
|
221
|
+
import * as Stream from "effect/Stream";
|
|
222
|
+
import { AiParser, AiPreprocessor, callTool, getToolCalls } from "@dxos/ai";
|
|
223
|
+
import { todo } from "@dxos/debug";
|
|
224
|
+
import { Obj as Obj3 } from "@dxos/echo";
|
|
225
|
+
import { TracingService } from "@dxos/functions";
|
|
226
|
+
import { log as log2 } from "@dxos/log";
|
|
227
|
+
import { Message as Message2 } from "@dxos/types";
|
|
228
|
+
|
|
229
|
+
// src/session/format.ts
|
|
230
|
+
import * as Array4 from "effect/Array";
|
|
231
|
+
import * as Effect2 from "effect/Effect";
|
|
232
|
+
import * as Function3 from "effect/Function";
|
|
190
233
|
import * as Option from "effect/Option";
|
|
191
|
-
import {
|
|
234
|
+
import { Template } from "@dxos/blueprints";
|
|
192
235
|
import { Obj as Obj2 } from "@dxos/echo";
|
|
193
|
-
import { DatabaseService } from "@dxos/
|
|
236
|
+
import { DatabaseService, ObjectVersion } from "@dxos/echo-db";
|
|
194
237
|
import { log } from "@dxos/log";
|
|
195
|
-
import {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
238
|
+
import { Message } from "@dxos/types";
|
|
239
|
+
import { trim } from "@dxos/util";
|
|
240
|
+
|
|
241
|
+
// src/errors.ts
|
|
242
|
+
import { BaseError } from "@dxos/errors";
|
|
243
|
+
var AiModelError = class extends BaseError.extend("AI_MODEL_ERROR", "AI model error") {
|
|
244
|
+
};
|
|
245
|
+
var AiAssistantError = class extends BaseError.extend("AI_ASSISTANT_ERROR", "AI assistant error") {
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
// src/session/format.ts
|
|
249
|
+
var __dxlog_file2 = "/__w/dxos/dxos/packages/core/assistant/src/session/format.ts";
|
|
250
|
+
var formatSystemPrompt = ({ system, blueprints = [], objects = [] }) => Effect2.gen(function* () {
|
|
251
|
+
const blueprintDefs = yield* Function3.pipe(blueprints, Effect2.forEach((blueprint) => Effect2.succeed(blueprint.instructions)), Effect2.flatMap(Effect2.forEach((template) => DatabaseService.loadOption(template.source))), Effect2.map(Array4.filter(Option.isSome)), Effect2.map(Array4.map((template) => trim`
|
|
252
|
+
<blueprint>
|
|
253
|
+
${Template.process(template.value.content)}
|
|
254
|
+
</blueprint>
|
|
255
|
+
`)), Effect2.map((blueprints2) => blueprints2.length > 0 ? [
|
|
256
|
+
"## Blueprints Definitions",
|
|
257
|
+
...blueprints2
|
|
258
|
+
].join("\n\n") : void 0));
|
|
259
|
+
const objectDefs = yield* Function3.pipe(objects, Effect2.forEach((object) => Effect2.succeed(trim`
|
|
260
|
+
<object>
|
|
261
|
+
<dxn>${Obj2.getDXN(object)}</dxn>
|
|
262
|
+
<typename>${Obj2.getTypename(object)}</typename>
|
|
263
|
+
</object>
|
|
264
|
+
`)), Effect2.map((objects2) => objects2.length > 0 ? [
|
|
265
|
+
"## Context Objects",
|
|
266
|
+
...objects2
|
|
267
|
+
].join("\n\n") : void 0));
|
|
268
|
+
return yield* Function3.pipe(Effect2.succeed([
|
|
269
|
+
system,
|
|
270
|
+
blueprintDefs,
|
|
271
|
+
objectDefs
|
|
272
|
+
].filter((def) => def !== void 0)), Effect2.map((parts) => parts.join("\n\n")));
|
|
273
|
+
});
|
|
274
|
+
var formatUserPrompt = ({ prompt, history = [] }) => Effect2.gen(function* () {
|
|
275
|
+
const blocks = [];
|
|
276
|
+
const artifactDiffResolver = yield* Effect2.serviceOption(ArtifactDiffResolver);
|
|
277
|
+
if (Option.isSome(artifactDiffResolver)) {
|
|
278
|
+
const versions = gatherObjectVersions(history);
|
|
279
|
+
const artifactDiff = yield* Effect2.tryPromise({
|
|
280
|
+
try: () => artifactDiffResolver.value.resolve([
|
|
281
|
+
...versions.entries()
|
|
282
|
+
].map(([id, version]) => ({
|
|
283
|
+
id,
|
|
284
|
+
lastVersion: version
|
|
285
|
+
}))),
|
|
286
|
+
catch: AiAssistantError.wrap({
|
|
287
|
+
message: "Artifact diff resolution error"
|
|
288
|
+
})
|
|
203
289
|
});
|
|
204
|
-
|
|
205
|
-
|
|
290
|
+
log("version", {
|
|
291
|
+
artifactDiff,
|
|
292
|
+
versions
|
|
293
|
+
}, {
|
|
294
|
+
F: __dxlog_file2,
|
|
295
|
+
L: 92,
|
|
296
|
+
S: this,
|
|
297
|
+
C: (f, a) => f(...a)
|
|
298
|
+
});
|
|
299
|
+
for (const [id, { version }] of [
|
|
300
|
+
...artifactDiff.entries()
|
|
301
|
+
]) {
|
|
302
|
+
if (ObjectVersion.equals(version, versions.get(id))) {
|
|
303
|
+
artifactDiff.delete(id);
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
blocks.push({
|
|
307
|
+
_tag: "anchor",
|
|
308
|
+
objectId: id,
|
|
309
|
+
version
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
if (artifactDiff.size > 0) {
|
|
313
|
+
blocks.push(createArtifactUpdateBlock(artifactDiff));
|
|
314
|
+
}
|
|
206
315
|
}
|
|
207
|
-
return
|
|
208
|
-
|
|
209
|
-
|
|
316
|
+
return Obj2.make(Message.Message, {
|
|
317
|
+
created: (/* @__PURE__ */ new Date()).toISOString(),
|
|
318
|
+
sender: {
|
|
319
|
+
role: "user"
|
|
320
|
+
},
|
|
321
|
+
blocks: [
|
|
322
|
+
...blocks,
|
|
323
|
+
{
|
|
324
|
+
_tag: "text",
|
|
325
|
+
text: prompt
|
|
326
|
+
}
|
|
327
|
+
]
|
|
328
|
+
});
|
|
329
|
+
});
|
|
330
|
+
var gatherObjectVersions = (messages) => {
|
|
331
|
+
const artifactIds = /* @__PURE__ */ new Map();
|
|
332
|
+
for (const message of messages) {
|
|
333
|
+
for (const block of message.blocks) {
|
|
334
|
+
if (block._tag === "anchor") {
|
|
335
|
+
artifactIds.set(block.objectId, block.version);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return artifactIds;
|
|
340
|
+
};
|
|
341
|
+
var createArtifactUpdateBlock = (artifactDiff) => {
|
|
342
|
+
return {
|
|
343
|
+
_tag: "text",
|
|
344
|
+
// TODO(dmaretskyi): Does this need to be a special content-block?
|
|
345
|
+
disposition: "artifact-update",
|
|
346
|
+
text: trim`
|
|
347
|
+
The following artifacts have been updated since the last message:
|
|
348
|
+
${[
|
|
349
|
+
...artifactDiff.entries()
|
|
350
|
+
].map(([id, { diff }]) => `<changed-artifact id="${id}">${diff ? `
|
|
351
|
+
${diff}` : ""}</changed-artifact>`).join("\n")}
|
|
352
|
+
`
|
|
353
|
+
};
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
// src/session/session.ts
|
|
357
|
+
var __dxlog_file3 = "/__w/dxos/dxos/packages/core/assistant/src/session/session.ts";
|
|
358
|
+
var AiSession = class {
|
|
359
|
+
_options;
|
|
360
|
+
/** Prevents concurrent execution of session. */
|
|
361
|
+
_semaphore = Effect3.runSync(Effect3.makeSemaphore(1));
|
|
362
|
+
/** Message history from queue. */
|
|
363
|
+
// TODO(burdon): Evolve into supporting a git-like graph of messages.
|
|
364
|
+
_history = [];
|
|
365
|
+
/** Pending messages for this session (incl. the current prompt). */
|
|
366
|
+
_pending = [];
|
|
367
|
+
constructor(_options = {}) {
|
|
368
|
+
this._options = _options;
|
|
369
|
+
}
|
|
370
|
+
run = ({ prompt, system: systemTemplate, history = [], objects = [], blueprints = [], toolkit, observer = GenerationObserver.noop() }) => Effect3.gen(this, function* () {
|
|
371
|
+
this._history = [
|
|
372
|
+
...history
|
|
373
|
+
];
|
|
374
|
+
this._pending = [];
|
|
375
|
+
const pending = this._pending;
|
|
376
|
+
const submitMessage = Effect3.fnUntraced(function* (message) {
|
|
377
|
+
pending.push(message);
|
|
378
|
+
yield* observer.onMessage(message);
|
|
379
|
+
yield* TracingService.emitConverationMessage(message);
|
|
380
|
+
return message;
|
|
381
|
+
});
|
|
382
|
+
const promptMessage = yield* submitMessage(yield* formatUserPrompt({
|
|
383
|
+
prompt,
|
|
384
|
+
history
|
|
385
|
+
}));
|
|
386
|
+
const system = yield* formatSystemPrompt({
|
|
387
|
+
system: systemTemplate,
|
|
388
|
+
blueprints,
|
|
389
|
+
objects
|
|
390
|
+
});
|
|
391
|
+
do {
|
|
392
|
+
log2("request", {
|
|
393
|
+
prompt: promptMessage,
|
|
394
|
+
system: {
|
|
395
|
+
snippet: createSnippet(system),
|
|
396
|
+
length: system.length
|
|
397
|
+
},
|
|
398
|
+
pending: this._pending.length,
|
|
399
|
+
history: this._history.length,
|
|
400
|
+
objects: objects?.length ?? 0
|
|
401
|
+
}, {
|
|
402
|
+
F: __dxlog_file3,
|
|
403
|
+
L: 110,
|
|
404
|
+
S: this,
|
|
405
|
+
C: (f, a) => f(...a)
|
|
406
|
+
});
|
|
407
|
+
const prompt2 = yield* AiPreprocessor.preprocessPrompt([
|
|
408
|
+
...this._history,
|
|
409
|
+
...this._pending
|
|
410
|
+
], {
|
|
411
|
+
system
|
|
412
|
+
});
|
|
413
|
+
const blocks = yield* LanguageModel.streamText({
|
|
414
|
+
prompt: prompt2,
|
|
415
|
+
toolkit,
|
|
416
|
+
disableToolCallResolution: true
|
|
417
|
+
}).pipe(
|
|
418
|
+
// TOOD(dmaretskyi): Error mapping.
|
|
419
|
+
AiParser.parseResponse({
|
|
420
|
+
onBegin: () => observer.onBegin(),
|
|
421
|
+
onBlock: (block) => observer.onBlock(block),
|
|
422
|
+
onPart: (part) => observer.onPart(part),
|
|
423
|
+
onEnd: (summary) => observer.onEnd(summary)
|
|
424
|
+
}),
|
|
425
|
+
Stream.runCollect,
|
|
426
|
+
Effect3.map(Chunk.toArray)
|
|
427
|
+
);
|
|
428
|
+
const response = yield* submitMessage(Obj3.make(Message2.Message, {
|
|
429
|
+
created: (/* @__PURE__ */ new Date()).toISOString(),
|
|
430
|
+
sender: {
|
|
431
|
+
role: "assistant"
|
|
432
|
+
},
|
|
433
|
+
blocks
|
|
434
|
+
}));
|
|
435
|
+
const toolCalls = getToolCalls(response);
|
|
436
|
+
if (toolCalls.length === 0) {
|
|
437
|
+
break;
|
|
438
|
+
} else if (!toolkit) {
|
|
439
|
+
throw new Error("No toolkit provided");
|
|
440
|
+
}
|
|
441
|
+
const toolResults = yield* Effect3.forEach(toolCalls, (toolCall) => {
|
|
442
|
+
return callTool(toolkit, toolCall).pipe(Effect3.provide(TracingService.layerSubframe((context) => ({
|
|
443
|
+
...context,
|
|
444
|
+
parentMessage: response.id,
|
|
445
|
+
toolCallId: toolCall.toolCallId
|
|
446
|
+
}))));
|
|
447
|
+
});
|
|
448
|
+
yield* submitMessage(Obj3.make(Message2.Message, {
|
|
449
|
+
created: (/* @__PURE__ */ new Date()).toISOString(),
|
|
450
|
+
sender: {
|
|
451
|
+
role: "tool"
|
|
452
|
+
},
|
|
453
|
+
blocks: toolResults
|
|
454
|
+
}));
|
|
455
|
+
} while (true);
|
|
456
|
+
log2("done", {
|
|
457
|
+
pending: this._pending.length
|
|
458
|
+
}, {
|
|
459
|
+
F: __dxlog_file3,
|
|
460
|
+
L: 181,
|
|
461
|
+
S: this,
|
|
462
|
+
C: (f, a) => f(...a)
|
|
463
|
+
});
|
|
464
|
+
return this._pending;
|
|
465
|
+
}).pipe(this._semaphore.withPermits(1), Effect3.withSpan("AiSession.run"));
|
|
466
|
+
/**
|
|
467
|
+
* @deprecated
|
|
468
|
+
*/
|
|
469
|
+
// TODO(burdon): Implement or remove.
|
|
470
|
+
async runStructured(_schema, _options) {
|
|
471
|
+
return todo();
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
var createSnippet = (text, len = 32) => text.length <= len * 2 ? text : [
|
|
475
|
+
text.slice(0, len),
|
|
476
|
+
"...",
|
|
477
|
+
text.slice(-len)
|
|
478
|
+
].join("");
|
|
479
|
+
|
|
480
|
+
// src/session/toolkit.ts
|
|
481
|
+
import * as Toolkit from "@effect/ai/Toolkit";
|
|
482
|
+
import * as Effect4 from "effect/Effect";
|
|
483
|
+
import { ToolExecutionService, ToolResolverService } from "@dxos/ai";
|
|
484
|
+
import { isTruthy } from "@dxos/util";
|
|
485
|
+
var createToolkit = ({ toolkit: toolkitParam, toolIds = [], blueprints = [] }) => Effect4.gen(function* () {
|
|
486
|
+
const blueprintToolkit = yield* ToolResolverService.resolveToolkit([
|
|
487
|
+
...blueprints.flatMap(({ tools }) => tools),
|
|
488
|
+
...toolIds
|
|
489
|
+
]);
|
|
490
|
+
const blueprintToolkitHandler = yield* blueprintToolkit.toContext(ToolExecutionService.handlersFor(blueprintToolkit));
|
|
491
|
+
const toolkit = Toolkit.merge(...[
|
|
492
|
+
toolkitParam,
|
|
493
|
+
blueprintToolkit
|
|
494
|
+
].filter(isTruthy));
|
|
495
|
+
return yield* toolkit.pipe(Effect4.provide(blueprintToolkitHandler));
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
// src/session/version-pin.ts
|
|
499
|
+
import * as Schema2 from "effect/Schema";
|
|
500
|
+
import { ObjectId } from "@dxos/echo/internal";
|
|
501
|
+
import { getVersion } from "@dxos/echo-db";
|
|
502
|
+
var ObjectVersionSchema = Schema2.Unknown;
|
|
503
|
+
var VersionPinSchema = Schema2.Struct({
|
|
504
|
+
// TODO(dmaretskyi): Use Ref when those support encoding.
|
|
505
|
+
objectId: ObjectId,
|
|
506
|
+
// TODO(dmaretskyi): Could be opaque.
|
|
507
|
+
version: ObjectVersionSchema
|
|
508
|
+
});
|
|
509
|
+
var VersionPin = class extends VersionPinSchema {
|
|
510
|
+
static DISPOSITION = "version-pin";
|
|
511
|
+
static fromObject(object) {
|
|
512
|
+
return VersionPin.make({
|
|
513
|
+
objectId: object.id,
|
|
514
|
+
version: getVersion(object)
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
static createBlock(pin) {
|
|
518
|
+
return {
|
|
519
|
+
_tag: "json",
|
|
520
|
+
disposition: VersionPin.DISPOSITION,
|
|
521
|
+
data: JSON.stringify(pin)
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
};
|
|
525
|
+
|
|
526
|
+
// src/session/GenericToolkit.ts
|
|
527
|
+
var GenericToolkit_exports = {};
|
|
528
|
+
__export(GenericToolkit_exports, {
|
|
529
|
+
TypeId: () => TypeId,
|
|
530
|
+
make: () => make,
|
|
531
|
+
merge: () => merge3
|
|
532
|
+
});
|
|
533
|
+
import * as Toolkit2 from "@effect/ai/Toolkit";
|
|
534
|
+
import * as Effect5 from "effect/Effect";
|
|
535
|
+
import * as Layer from "effect/Layer";
|
|
536
|
+
import * as Pipeable from "effect/Pipeable";
|
|
537
|
+
var TypeId = "~@dxos/assistant/GenericToolkit";
|
|
538
|
+
var make = (toolkit, layer) => {
|
|
539
|
+
return {
|
|
540
|
+
[TypeId]: TypeId,
|
|
541
|
+
toolkit,
|
|
542
|
+
layer,
|
|
543
|
+
handlers: toolkit.pipe(Effect5.provide(layer)),
|
|
544
|
+
pipe() {
|
|
545
|
+
return Pipeable.pipeArguments(this, arguments);
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
};
|
|
549
|
+
var merge3 = (...toolkits) => {
|
|
550
|
+
return make(Toolkit2.merge(...toolkits.map((t) => t.toolkit)), Layer.mergeAll(...toolkits.map((t) => t.layer)));
|
|
551
|
+
};
|
|
552
|
+
|
|
553
|
+
// src/conversation/conversation.ts
|
|
554
|
+
var __dxlog_file4 = "/__w/dxos/dxos/packages/core/assistant/src/conversation/conversation.ts";
|
|
210
555
|
var AiConversation = class extends Resource2 {
|
|
556
|
+
/**
|
|
557
|
+
* Message and binding queue.
|
|
558
|
+
*/
|
|
559
|
+
_queue;
|
|
560
|
+
/**
|
|
561
|
+
* Blueprints bound to the conversation.
|
|
562
|
+
*/
|
|
563
|
+
_context;
|
|
564
|
+
/**
|
|
565
|
+
* Toolkit from the current session request.
|
|
566
|
+
*/
|
|
567
|
+
_toolkit;
|
|
568
|
+
constructor(queue) {
|
|
569
|
+
super();
|
|
570
|
+
this._queue = queue;
|
|
571
|
+
this._context = new AiContextBinder(this._queue);
|
|
572
|
+
}
|
|
211
573
|
async _open() {
|
|
212
574
|
await this._context.open();
|
|
213
575
|
}
|
|
@@ -222,31 +584,31 @@ var AiConversation = class extends Resource2 {
|
|
|
222
584
|
}
|
|
223
585
|
async getHistory() {
|
|
224
586
|
const queueItems = await this._queue.queryObjects();
|
|
225
|
-
return queueItems.filter(
|
|
587
|
+
return queueItems.filter(Obj4.instanceOf(Message3.Message));
|
|
226
588
|
}
|
|
227
589
|
/**
|
|
228
590
|
* Creates a new cancelable request effect.
|
|
229
591
|
*/
|
|
230
592
|
createRequest(params) {
|
|
231
|
-
return
|
|
593
|
+
return Effect6.gen(this, function* () {
|
|
232
594
|
const session = new AiSession();
|
|
233
|
-
const history = yield*
|
|
234
|
-
const context = yield*
|
|
235
|
-
const blueprints = yield*
|
|
236
|
-
const objects = yield*
|
|
595
|
+
const history = yield* Effect6.promise(() => this.getHistory());
|
|
596
|
+
const context = yield* Effect6.promise(() => this.context.query());
|
|
597
|
+
const blueprints = yield* Effect6.forEach(context.blueprints.values(), DatabaseService2.loadOption).pipe(Effect6.map(Array5.filter(Option2.isSome)), Effect6.map(Array5.map((option) => option.value)));
|
|
598
|
+
const objects = yield* Effect6.forEach(context.objects.values(), DatabaseService2.loadOption).pipe(Effect6.map(Array5.filter(Option2.isSome)), Effect6.map(Array5.map((option) => option.value)));
|
|
237
599
|
const toolkit = yield* createToolkit({
|
|
238
600
|
blueprints
|
|
239
601
|
});
|
|
240
602
|
this._toolkit = toolkit;
|
|
241
603
|
const start = Date.now();
|
|
242
|
-
|
|
604
|
+
log3("run", {
|
|
243
605
|
history: history.length,
|
|
244
606
|
blueprints: blueprints.length,
|
|
245
607
|
objects: objects.length,
|
|
246
608
|
tools: this._toolkit?.tools.length ?? 0
|
|
247
609
|
}, {
|
|
248
|
-
F:
|
|
249
|
-
L:
|
|
610
|
+
F: __dxlog_file4,
|
|
611
|
+
L: 107,
|
|
250
612
|
S: this,
|
|
251
613
|
C: (f, a) => f(...a)
|
|
252
614
|
});
|
|
@@ -256,44 +618,30 @@ var AiConversation = class extends Resource2 {
|
|
|
256
618
|
objects,
|
|
257
619
|
toolkit,
|
|
258
620
|
...params
|
|
259
|
-
}).pipe(
|
|
621
|
+
}).pipe(Effect6.provideService(AiContextService, {
|
|
260
622
|
binder: this.context
|
|
261
623
|
}));
|
|
262
|
-
|
|
624
|
+
log3("result", {
|
|
263
625
|
messages: messages.length,
|
|
264
626
|
duration: Date.now() - start
|
|
265
627
|
}, {
|
|
266
|
-
F:
|
|
267
|
-
L:
|
|
628
|
+
F: __dxlog_file4,
|
|
629
|
+
L: 121,
|
|
268
630
|
S: this,
|
|
269
631
|
C: (f, a) => f(...a)
|
|
270
632
|
});
|
|
271
|
-
yield*
|
|
633
|
+
yield* Effect6.promise(() => this._queue.append(messages));
|
|
272
634
|
return messages;
|
|
273
|
-
}).pipe(
|
|
274
|
-
}
|
|
275
|
-
constructor(options) {
|
|
276
|
-
super(), /**
|
|
277
|
-
* Message and binding queue.
|
|
278
|
-
*/
|
|
279
|
-
_define_property2(this, "_queue", void 0), /**
|
|
280
|
-
* Blueprints bound to the conversation.
|
|
281
|
-
*/
|
|
282
|
-
_define_property2(this, "_context", void 0), /**
|
|
283
|
-
* Toolkit from the current session request.
|
|
284
|
-
*/
|
|
285
|
-
_define_property2(this, "_toolkit", void 0);
|
|
286
|
-
this._queue = options.queue;
|
|
287
|
-
this._context = new AiContextBinder(this._queue);
|
|
635
|
+
}).pipe(Effect6.withSpan("AiConversation.request"));
|
|
288
636
|
}
|
|
289
637
|
};
|
|
290
638
|
|
|
291
639
|
// src/diff.ts
|
|
292
640
|
import { next as A } from "@automerge/automerge";
|
|
293
641
|
import { DocAccessor, toCursorRange } from "@dxos/echo-db";
|
|
294
|
-
import { log as
|
|
642
|
+
import { log as log4 } from "@dxos/log";
|
|
295
643
|
import { isNonNullable } from "@dxos/util";
|
|
296
|
-
var
|
|
644
|
+
var __dxlog_file5 = "/__w/dxos/dxos/packages/core/assistant/src/diff.ts";
|
|
297
645
|
var reduceDiffs = (diffs) => {
|
|
298
646
|
return diffs.reduce((acc, diff) => {
|
|
299
647
|
const current = acc[acc.length - 1];
|
|
@@ -345,9 +693,9 @@ var applyDiffs = (accessor, diffs) => {
|
|
|
345
693
|
if (idx !== -1) {
|
|
346
694
|
A.splice(doc, accessor.path, idx, diff.match.length, diff.replace);
|
|
347
695
|
} else {
|
|
348
|
-
|
|
349
|
-
F:
|
|
350
|
-
L:
|
|
696
|
+
log4.warn("diff not found", diff, {
|
|
697
|
+
F: __dxlog_file5,
|
|
698
|
+
L: 78,
|
|
351
699
|
S: void 0,
|
|
352
700
|
C: (f, a) => f(...a)
|
|
353
701
|
});
|
|
@@ -359,34 +707,36 @@ var applyDiffs = (accessor, diffs) => {
|
|
|
359
707
|
|
|
360
708
|
// src/execution-graph.ts
|
|
361
709
|
import { AgentStatus } from "@dxos/ai";
|
|
362
|
-
import { Obj as
|
|
710
|
+
import { Obj as Obj5 } from "@dxos/echo";
|
|
363
711
|
import { MESSAGE_PROPERTY_TOOL_CALL_ID } from "@dxos/functions";
|
|
364
712
|
import { LogLevel } from "@dxos/log";
|
|
365
|
-
import { ContentBlock,
|
|
713
|
+
import { ContentBlock, Message as Message4 } from "@dxos/types";
|
|
366
714
|
import { isNonNullable as isNonNullable2 } from "@dxos/util";
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
value,
|
|
371
|
-
enumerable: true,
|
|
372
|
-
configurable: true,
|
|
373
|
-
writable: true
|
|
374
|
-
});
|
|
375
|
-
} else {
|
|
376
|
-
obj[key] = value;
|
|
377
|
-
}
|
|
378
|
-
return obj;
|
|
379
|
-
}
|
|
715
|
+
var SKIP_BLOCKS = [
|
|
716
|
+
"text"
|
|
717
|
+
];
|
|
380
718
|
var ExecutionGraph = class {
|
|
719
|
+
_skipBlocks;
|
|
720
|
+
// TODO(dmaretskyi): Evolve the internal state to support chats, functions, circuit workflows, etc.
|
|
721
|
+
_commits = [];
|
|
722
|
+
_branchNames = /* @__PURE__ */ new Set();
|
|
723
|
+
_lastBlockId;
|
|
724
|
+
// Tool call tracking.
|
|
725
|
+
_toolCallCommitIds = /* @__PURE__ */ new Map();
|
|
726
|
+
_lastCommitByBranch = /* @__PURE__ */ new Map();
|
|
727
|
+
_pendingToolResults = /* @__PURE__ */ new Map();
|
|
728
|
+
constructor(_skipBlocks = SKIP_BLOCKS) {
|
|
729
|
+
this._skipBlocks = _skipBlocks;
|
|
730
|
+
}
|
|
381
731
|
/**
|
|
382
732
|
* Adds events to the graph.
|
|
383
733
|
*/
|
|
384
734
|
addEvents(events) {
|
|
385
735
|
const sortedEvents = this.sortObjectsByCreated(events);
|
|
386
736
|
for (const event of sortedEvents) {
|
|
387
|
-
if (
|
|
737
|
+
if (Obj5.instanceOf(Message4.Message, event)) {
|
|
388
738
|
this._processMessage(event);
|
|
389
|
-
} else if (
|
|
739
|
+
} else if (Obj5.instanceOf(AgentStatus, event)) {
|
|
390
740
|
this._processAgentStatus(event);
|
|
391
741
|
}
|
|
392
742
|
}
|
|
@@ -395,7 +745,7 @@ var ExecutionGraph = class {
|
|
|
395
745
|
* Processes a message event and creates commits for its blocks.
|
|
396
746
|
*/
|
|
397
747
|
_processMessage(message) {
|
|
398
|
-
const messageCommits = messageToCommits(message, this._lastBlockId, this._toolCallCommitIds, this._lastCommitByBranch);
|
|
748
|
+
const messageCommits = messageToCommits(message, this._lastBlockId, this._toolCallCommitIds, this._lastCommitByBranch, this._skipBlocks);
|
|
399
749
|
this._commits.push(...messageCommits);
|
|
400
750
|
messageCommits.forEach((commit) => this._branchNames.add(commit.branch));
|
|
401
751
|
this._trackToolCalls(messageCommits);
|
|
@@ -559,20 +909,15 @@ var ExecutionGraph = class {
|
|
|
559
909
|
const toolResultMatch = commitId.match(/_toolResult_(.+)$/);
|
|
560
910
|
return toolCallMatch?.[1] || toolResultMatch?.[1];
|
|
561
911
|
}
|
|
562
|
-
constructor() {
|
|
563
|
-
_define_property3(this, "_commits", []);
|
|
564
|
-
_define_property3(this, "_branchNames", /* @__PURE__ */ new Set());
|
|
565
|
-
_define_property3(this, "_lastBlockId", void 0);
|
|
566
|
-
_define_property3(this, "_toolCallCommitIds", /* @__PURE__ */ new Map());
|
|
567
|
-
_define_property3(this, "_lastCommitByBranch", /* @__PURE__ */ new Map());
|
|
568
|
-
_define_property3(this, "_pendingToolResults", /* @__PURE__ */ new Map());
|
|
569
|
-
}
|
|
570
912
|
};
|
|
571
|
-
var messageToCommits = (message, lastBlockId, toolCallIds, lastCommitByBranch) => {
|
|
913
|
+
var messageToCommits = (message, lastBlockId, toolCallIds, lastCommitByBranch, skipBlocks) => {
|
|
572
914
|
let previousBlockId = lastBlockId;
|
|
573
915
|
return message.blocks.map((block, idx) => {
|
|
574
916
|
const branch = getMessageBranch(message);
|
|
575
917
|
const parents = getBlockParents(block, previousBlockId, message, toolCallIds, lastCommitByBranch);
|
|
918
|
+
if (skipBlocks?.includes(block._tag)) {
|
|
919
|
+
return null;
|
|
920
|
+
}
|
|
576
921
|
const commit = createBlockCommit(block, message, branch, parents, idx);
|
|
577
922
|
if (commit) {
|
|
578
923
|
previousBlockId = commit.id;
|
|
@@ -727,49 +1072,50 @@ var stringifyRef = (ref) => {
|
|
|
727
1072
|
return ref.dxn.asEchoDXN()?.echoId ?? ref.dxn.asQueueDXN()?.objectId ?? "";
|
|
728
1073
|
};
|
|
729
1074
|
var stringifyObject = (obj) => {
|
|
730
|
-
return
|
|
1075
|
+
return Obj5.getLabel(obj) ?? Obj5.getTypename(obj) ?? obj.id;
|
|
731
1076
|
};
|
|
732
1077
|
|
|
733
1078
|
// src/functions/services.ts
|
|
734
1079
|
import * as Tool from "@effect/ai/Tool";
|
|
735
|
-
import * as
|
|
736
|
-
import * as
|
|
737
|
-
import * as
|
|
1080
|
+
import * as Context3 from "effect/Context";
|
|
1081
|
+
import * as Effect7 from "effect/Effect";
|
|
1082
|
+
import * as Layer2 from "effect/Layer";
|
|
738
1083
|
import * as Record from "effect/Record";
|
|
739
|
-
import * as
|
|
740
|
-
import { AiToolNotFoundError, ToolExecutionService, ToolResolverService } from "@dxos/ai";
|
|
741
|
-
import { todo } from "@dxos/debug";
|
|
1084
|
+
import * as Schema3 from "effect/Schema";
|
|
1085
|
+
import { AiToolNotFoundError, ToolExecutionService as ToolExecutionService2, ToolResolverService as ToolResolverService2 } from "@dxos/ai";
|
|
1086
|
+
import { todo as todo2 } from "@dxos/debug";
|
|
742
1087
|
import { Query as Query2 } from "@dxos/echo";
|
|
743
|
-
import { DatabaseService as
|
|
1088
|
+
import { DatabaseService as DatabaseService3 } from "@dxos/echo-db";
|
|
1089
|
+
import { Function as Function4, FunctionDefinition, FunctionInvocationService } from "@dxos/functions";
|
|
744
1090
|
import { invariant as invariant2 } from "@dxos/invariant";
|
|
745
|
-
var
|
|
1091
|
+
var __dxlog_file6 = "/__w/dxos/dxos/packages/core/assistant/src/functions/services.ts";
|
|
746
1092
|
var makeToolResolverFromFunctions = (functions, toolkit) => {
|
|
747
|
-
return
|
|
748
|
-
const dbService = yield*
|
|
1093
|
+
return Layer2.effect(ToolResolverService2, Effect7.gen(function* () {
|
|
1094
|
+
const dbService = yield* DatabaseService3;
|
|
749
1095
|
return {
|
|
750
|
-
resolve: (id) =>
|
|
1096
|
+
resolve: (id) => Effect7.gen(function* () {
|
|
751
1097
|
const tool = toolkit.tools[id];
|
|
752
1098
|
if (tool) {
|
|
753
1099
|
return tool;
|
|
754
1100
|
}
|
|
755
|
-
const { objects: [dbFunction] } = yield*
|
|
1101
|
+
const { objects: [dbFunction] } = yield* DatabaseService3.runQuery(Query2.type(Function4.Function, {
|
|
756
1102
|
key: id
|
|
757
1103
|
}));
|
|
758
1104
|
const functionDef = dbFunction ? FunctionDefinition.deserialize(dbFunction) : functions.find((fn2) => fn2.key === id);
|
|
759
1105
|
if (!functionDef) {
|
|
760
|
-
return yield*
|
|
1106
|
+
return yield* Effect7.fail(new AiToolNotFoundError(id));
|
|
761
1107
|
}
|
|
762
1108
|
return projectFunctionToTool(functionDef);
|
|
763
|
-
}).pipe(
|
|
1109
|
+
}).pipe(Effect7.provideService(DatabaseService3, dbService))
|
|
764
1110
|
};
|
|
765
1111
|
}));
|
|
766
1112
|
};
|
|
767
1113
|
var makeToolExecutionServiceFromFunctions = (toolkit, handlersLayer) => {
|
|
768
|
-
return
|
|
769
|
-
const toolkitHandler = yield* toolkit.pipe(
|
|
1114
|
+
return Layer2.effect(ToolExecutionService2, Effect7.gen(function* () {
|
|
1115
|
+
const toolkitHandler = yield* toolkit.pipe(Effect7.provide(handlersLayer));
|
|
770
1116
|
invariant2(isHandlerLike(toolkitHandler), void 0, {
|
|
771
|
-
F:
|
|
772
|
-
L:
|
|
1117
|
+
F: __dxlog_file6,
|
|
1118
|
+
L: 74,
|
|
773
1119
|
S: this,
|
|
774
1120
|
A: [
|
|
775
1121
|
"isHandlerLike(toolkitHandler)",
|
|
@@ -780,15 +1126,16 @@ var makeToolExecutionServiceFromFunctions = (toolkit, handlersLayer) => {
|
|
|
780
1126
|
return {
|
|
781
1127
|
handlersFor: (toolkit2) => {
|
|
782
1128
|
const makeHandler = (tool) => {
|
|
783
|
-
return
|
|
1129
|
+
return Effect7.fn(`toolFunctionHandler ${tool.name}`)(function* (input) {
|
|
784
1130
|
if (toolkitHandler.tools[tool.name]) {
|
|
785
1131
|
if (Tool.isProviderDefined(tool)) {
|
|
786
1132
|
throw new Error("Attempted to call a provider-defined tool");
|
|
787
1133
|
}
|
|
788
|
-
|
|
1134
|
+
const { result } = yield* toolkitHandler.handle(tool.name, input);
|
|
1135
|
+
return result;
|
|
789
1136
|
}
|
|
790
|
-
const { definition: functionDef } =
|
|
791
|
-
return yield* functionInvocationService.invokeFunction(functionDef, input).pipe(
|
|
1137
|
+
const { definition: functionDef } = Context3.get(FunctionToolAnnotation)(tool.annotations);
|
|
1138
|
+
return yield* functionInvocationService.invokeFunction(functionDef, input).pipe(Effect7.catchAllDefect((defect) => Effect7.fail(defect)));
|
|
792
1139
|
});
|
|
793
1140
|
};
|
|
794
1141
|
return toolkit2.of(Record.map(toolkit2.tools, (tool, _name) => Tool.isUserDefined(tool) ? makeHandler(tool) : null));
|
|
@@ -796,7 +1143,7 @@ var makeToolExecutionServiceFromFunctions = (toolkit, handlersLayer) => {
|
|
|
796
1143
|
};
|
|
797
1144
|
}));
|
|
798
1145
|
};
|
|
799
|
-
var FunctionToolAnnotation = class extends
|
|
1146
|
+
var FunctionToolAnnotation = class extends Context3.Tag("@dxos/assistant/FunctionToolAnnotation")() {
|
|
800
1147
|
};
|
|
801
1148
|
var toolCache = /* @__PURE__ */ new WeakMap();
|
|
802
1149
|
var projectFunctionToTool = (fn2) => {
|
|
@@ -807,7 +1154,7 @@ var projectFunctionToTool = (fn2) => {
|
|
|
807
1154
|
description: fn2.description,
|
|
808
1155
|
parameters: createStructFieldsFromSchema(fn2.inputSchema),
|
|
809
1156
|
// TODO(dmaretskyi): Include output schema.
|
|
810
|
-
failure:
|
|
1157
|
+
failure: Schema3.Any
|
|
811
1158
|
}).annotate(FunctionToolAnnotation, {
|
|
812
1159
|
definition: fn2
|
|
813
1160
|
});
|
|
@@ -815,13 +1162,13 @@ var projectFunctionToTool = (fn2) => {
|
|
|
815
1162
|
return tool;
|
|
816
1163
|
};
|
|
817
1164
|
var makeToolName = (name) => {
|
|
818
|
-
const toolName = name.replace(/[^a-zA-Z0-9]/g, "
|
|
819
|
-
invariant2(toolName.match(/^[a-
|
|
820
|
-
F:
|
|
821
|
-
L:
|
|
1165
|
+
const toolName = name.toLowerCase().replace(/[^a-zA-Z0-9]/g, "-");
|
|
1166
|
+
invariant2(toolName.match(/^[a-z_][a-z0-9-_]*$/), void 0, {
|
|
1167
|
+
F: __dxlog_file6,
|
|
1168
|
+
L: 144,
|
|
822
1169
|
S: void 0,
|
|
823
1170
|
A: [
|
|
824
|
-
"toolName.match(/^[a-
|
|
1171
|
+
"toolName.match(/^[a-z_][a-z0-9-_]*$/)",
|
|
825
1172
|
""
|
|
826
1173
|
]
|
|
827
1174
|
});
|
|
@@ -832,12 +1179,12 @@ var createStructFieldsFromSchema = (schema) => {
|
|
|
832
1179
|
case "TypeLiteral":
|
|
833
1180
|
return Object.fromEntries(schema.ast.propertySignatures.map((prop) => [
|
|
834
1181
|
prop.name,
|
|
835
|
-
|
|
1182
|
+
Schema3.make(prop.type)
|
|
836
1183
|
]));
|
|
837
1184
|
case "VoidKeyword":
|
|
838
1185
|
return {};
|
|
839
1186
|
default:
|
|
840
|
-
return
|
|
1187
|
+
return todo2(`Unsupported schema AST: ${schema.ast._tag}`);
|
|
841
1188
|
}
|
|
842
1189
|
};
|
|
843
1190
|
var isHandlerLike = (value) => {
|
|
@@ -846,21 +1193,21 @@ var isHandlerLike = (value) => {
|
|
|
846
1193
|
|
|
847
1194
|
// src/templates/system.ts
|
|
848
1195
|
import defaultsDeep from "lodash.defaultsdeep";
|
|
849
|
-
import { Template } from "@dxos/blueprints";
|
|
1196
|
+
import { Template as Template2 } from "@dxos/blueprints";
|
|
850
1197
|
|
|
851
|
-
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/format.tpl?raw
|
|
852
|
-
var format_default = "{{! System Core }}\n\n## Input Formats\n\n### Blueprints\n\nYou may be provided with Blueprint definitions that include specific instructions and tools for a given set of tasks.\nBlue print instructions will be enclosed in a <blueprint> tag.\n\n### Context objects\n\nYou may be provided with references to objects that are relevant to the current prompt.\nObjects will be in the form of a <object> tag with a <dxn> element that is the unique key of the object required by many tools.\nThe <typename> element is the type of the object.\n\n<object>\n <dxn>object-id</dxn>\n <typename>example.com/type/TypeName</typename>\n</object>\n\n## Response Formats\n\nIt is very important to respond in the correct format.\nYou response must be valid Markdown.\nIn some cases you should respond with well-formed XML tags along with the content.\nCode snippets should use 2-space indents.\n\n{{#if cot}}\nBefore responding, explain your reasoning and include your detailed chain-of-thought enclosed in a <cot> tag.\nThe <cot> tag should be the first thing in your response.\n{{/if}}\n\n## Block references\n\nYou can reply with a block reference for objects if you have the DXN. For example:\n\n<object>\n <dxn>dxn:queue:data:B6INSIBY3CBEF4M5VZRYBCMAHQMPYK5AJ:01K24XMVHSZHS97SG1VTVQDM5Z:01K24XPK464FSCKVQJAB2H662M</dxn>\n</object>\n<object>\n <dxn>dxn:echo:@:01K24XPK464FSCKVQJAB2H662M</dxn>\n</object>\n\n### Suggestions\n\nEach suggestion must in the form of a proposed action enclosed in a <suggestion> tag.\nExample: <suggestion>Show on a map</suggestion>\n\n### Multiple choice questions\n\nYou may ask multiple choice questions by wrapping consise options inside <option> tags inside a well formed <select> tag.\nExample: <select><option>Yes</option><option>No</option></select>\n\n## Toolkit\n\nIf the user asks for a list of tools, then just emit a single self-closing <toolkit/> tag instead of listing the tools.\n\n## Context\n\nThe current date and time is {{DATETIME}}.\n";
|
|
1198
|
+
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/instructions/format.tpl?raw
|
|
1199
|
+
var format_default = "{{! System Core }}\n\n## Input Formats\n\n### Blueprints\n\nYou may be provided with Blueprint definitions that include specific instructions and tools for a given set of tasks.\nBlue print instructions will be enclosed in a <blueprint> tag.\n\n### Context objects\n\nYou may be provided with references to objects that are relevant to the current prompt.\nObjects will be in the form of a <object> tag with a <dxn> element that is the unique key of the object required by many tools.\nThe <typename> element is the type of the object.\n\n<object>\n <dxn>object-id</dxn>\n <typename>example.com/type/TypeName</typename>\n</object>\n\n## Response Formats\n\nIt is very important to respond in the correct format.\nYou response must be valid Markdown.\nIn some cases you should respond with well-formed XML tags along with the content.\nCode snippets should use 2-space indents.\n\n{{#if cot}}\nBefore responding, explain your reasoning and include your detailed chain-of-thought enclosed in a <cot> tag.\nThe <cot> tag should be the first thing in your response.\n{{/if}}\n\n## Block references\n\nYou can reply with a block reference for objects if you have the DXN. For example:\n\n<object>\n <dxn>dxn:queue:data:B6INSIBY3CBEF4M5VZRYBCMAHQMPYK5AJ:01K24XMVHSZHS97SG1VTVQDM5Z:01K24XPK464FSCKVQJAB2H662M</dxn>\n</object>\n<object>\n <dxn>dxn:echo:@:01K24XPK464FSCKVQJAB2H662M</dxn>\n</object>\n\n### Suggestions\n\nEach suggestion must in the form of a proposed action enclosed in a <suggestion> tag.\nExample: <suggestion>Show on a map</suggestion>\nDo not use bullets when listing suggestions.\n\n### Multiple choice questions\n\nYou may ask multiple choice questions by wrapping consise options inside <option> tags inside a well formed <select> tag.\nExample: <select><option>Yes</option><option>No</option></select>\n\n## Toolkit\n\nIf the user asks for a list of tools, then just emit a single self-closing <toolkit/> tag instead of listing the tools.\n\n## Context\n\nThe current date and time is {{DATETIME}}.\n";
|
|
853
1200
|
|
|
854
|
-
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/system.tpl?raw
|
|
1201
|
+
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/instructions/system.tpl?raw
|
|
855
1202
|
var system_default = "{{! System Prompt }}\n\n- You are a helpful assistant called Kai. \n- Your name is Kai and you are powered by different language models.\n- You were created by DXOS and operate inside of Composer and on the DXOS EDGE network.\n- In your initial greeting state your name and make some suggestions based on the current context objects.\n- You are an advanced AI assistant capable of creating and managing artifacts from provided data and tools.\n- Your task is to process user commands and questions and decide how best to respond.\n- In general be concise and direct.\n- Follow all instructions carefully.\n\n## Planning\n\n- Analyze the structure and type of the content in the user's message.\n- Determine if you complete the task using the available blueprint definitions?\n- If you can't complete the task using the available blueprint definitions, query the list of available blueprint definitions using the appropriate tool.\n- Identify which blueprint definitions are relevant to the user's request.\n- Evaluate the potential benefits of creating an artifact vs. normal processing for each identified element.\n- Make a final decision on whether to create an artifact and explain your reasoning.\n- Are the required blueprint definitions already available?\n- If not, select which blueprint definition(s) will be the most relevant and require them using the require_artifact_definitions tool.\n- The require'd artifact tools will be available for use after require.\n- If creating an artifact, outline how you will structure it within the response.\n- Decide if the artifact needs to be shown to the user.\n- Call the show tool to show the artifact to the user.\n- Your reasoning must include: whether to use artifacts or not, to create one or query, whether to show the artifact to the user, and how to structure the response.\n\n## Blueprints and Artifacts\n\n- Determine if the interaction involves an artifact. Prefer artifacts for tables, lists, spreadsheets, kanbans, games, images, and other structured data.\n- Determine if the user is explicitly talking about creating a new artifact, or wants to use an existing artifact.\n- If it is ambiguous, query for existing artifacts first and then decide.\n- If you decide to create an artifact, call the associated tool to create the artifact.\n- Artifacts are stored in the database. Tools are used to create and query artifacts.\n- If you are unsure about creating an artifact ask the user for clarification.\n- Artifacts are mutable objects that can change over the course of the conversation.\n- Always re-query the artifact using the tool (like query or inspect) to get the latest state of the artifact before answering the user.\n- You must never generate the id of the artifact; only recall the ids that are already in the history.\n- Artifacts are created by requiring the specific artifact using the require_artifact tool and creating it by calling the associated tool.\n- You can add suggested actions at the end of your response.\n- Suggested actions should be very concise and start with a verb and be phrased as a command to an agent -- not a question to the user.\n- Suggested actions must be in the form of a user instruction that you can follow.\n- Suggested actions could include actions that create artifacts.\n- After creating an artifact use a tool to add it to the chat context.\n\n## Context\n\nToday is {{DATETIME}}.\n";
|
|
856
1203
|
|
|
857
1204
|
// src/templates/system.ts
|
|
858
|
-
var createSystemPrompt = (options = {}) =>
|
|
1205
|
+
var createSystemPrompt = (options = {}) => Template2.process(format_default, defaultsDeep(options, {
|
|
859
1206
|
DATETIME: (/* @__PURE__ */ new Date()).toISOString()
|
|
860
1207
|
}));
|
|
861
1208
|
var templates = {
|
|
862
1209
|
/** Editable system prompt.*/
|
|
863
|
-
system:
|
|
1210
|
+
system: Template2.make({
|
|
864
1211
|
/**
|
|
865
1212
|
* NOTE: We are forbidden from accessing randomness at model level in cloudflare, so we have to have pre-generated ids.
|
|
866
1213
|
*/
|
|
@@ -870,12 +1217,14 @@ var templates = {
|
|
|
870
1217
|
};
|
|
871
1218
|
|
|
872
1219
|
// src/types/artifact.ts
|
|
873
|
-
import * as
|
|
1220
|
+
import * as Schema4 from "effect/Schema";
|
|
1221
|
+
import { DatabaseService as DatabaseService4 } from "@dxos/echo-db";
|
|
874
1222
|
import { DXN as DXN2, LOCAL_SPACE_TAG } from "@dxos/keys";
|
|
875
|
-
import { trim } from "@dxos/util";
|
|
1223
|
+
import { trim as trim2 } from "@dxos/util";
|
|
876
1224
|
var createArtifactElement = (id) => `<artifact id=${id} />`;
|
|
877
|
-
var ArtifactId = class extends
|
|
878
|
-
|
|
1225
|
+
var ArtifactId = class extends Schema4.String.annotations({
|
|
1226
|
+
// TODO(dmaretskyi): This section gets overriden.
|
|
1227
|
+
description: trim2`
|
|
879
1228
|
The ID of the referenced object. Formats accepted:
|
|
880
1229
|
- DXN (dxn:echo:@:XXXXX). DXNs can be prepended with an @ symbol for compatibility with in-text references.
|
|
881
1230
|
- space ID, object ID tuple (spaceID:objectID)
|
|
@@ -905,6 +1254,13 @@ var ArtifactId = class extends Schema3.String.annotations({
|
|
|
905
1254
|
throw new Error(`Unable to parse object reference: ${reference}`);
|
|
906
1255
|
}
|
|
907
1256
|
}
|
|
1257
|
+
/**
|
|
1258
|
+
* Resolves an artifact ID to an object.
|
|
1259
|
+
*/
|
|
1260
|
+
static resolve(schema, ref) {
|
|
1261
|
+
const dxn = ArtifactId.toDXN(ref);
|
|
1262
|
+
return DatabaseService4.resolve(dxn, schema);
|
|
1263
|
+
}
|
|
908
1264
|
};
|
|
909
1265
|
export {
|
|
910
1266
|
AiAssistantError,
|
|
@@ -919,6 +1275,7 @@ export {
|
|
|
919
1275
|
ContextBinding,
|
|
920
1276
|
ExecutionGraph,
|
|
921
1277
|
GenerationObserver,
|
|
1278
|
+
GenericToolkit_exports as GenericToolkit,
|
|
922
1279
|
VersionPin,
|
|
923
1280
|
applyDiffs,
|
|
924
1281
|
computeDiffsWithCursors,
|