@dxos/assistant 0.8.4-main.e098934 → 0.8.4-main.ead640a
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-3K5SRE5A.mjs → chunk-CWGVG235.mjs} +59 -83
- package/dist/lib/browser/chunk-CWGVG235.mjs.map +7 -0
- package/dist/lib/browser/extraction/index.mjs +17 -13
- package/dist/lib/browser/extraction/index.mjs.map +4 -4
- package/dist/lib/browser/index.mjs +550 -86
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/{chunk-W7AIB6DP.mjs → chunk-OV5PMQPB.mjs} +59 -83
- package/dist/lib/node-esm/chunk-OV5PMQPB.mjs.map +7 -0
- package/dist/lib/node-esm/extraction/index.mjs +17 -13
- package/dist/lib/node-esm/extraction/index.mjs.map +4 -4
- package/dist/lib/node-esm/index.mjs +550 -86
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/conversation/context.d.ts +14 -7
- package/dist/types/src/conversation/context.d.ts.map +1 -1
- package/dist/types/src/conversation/conversation.d.ts +9 -10
- package/dist/types/src/conversation/conversation.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +8 -8
- package/dist/types/src/execution-graph.d.ts +82 -0
- package/dist/types/src/execution-graph.d.ts.map +1 -0
- package/dist/types/src/execution-graph.test.d.ts +2 -0
- package/dist/types/src/execution-graph.test.d.ts.map +1 -0
- 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 +61 -57
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction.d.ts +10 -6
- package/dist/types/src/extraction/extraction.d.ts.map +1 -1
- package/dist/types/src/extraction/quotes.d.ts +1 -1
- package/dist/types/src/extraction/quotes.d.ts.map +1 -1
- package/dist/types/src/functions/services.d.ts +17 -5
- package/dist/types/src/functions/services.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +2 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/session/artifact-diff.d.ts +2 -2
- package/dist/types/src/session/artifact-diff.d.ts.map +1 -1
- package/dist/types/src/session/error-handling.d.ts +2 -2
- package/dist/types/src/session/error-handling.d.ts.map +1 -1
- package/dist/types/src/session/format.d.ts +33 -31
- package/dist/types/src/session/format.d.ts.map +1 -1
- package/dist/types/src/session/observer.d.ts +1 -1
- package/dist/types/src/session/observer.d.ts.map +1 -1
- package/dist/types/src/session/session.d.ts +13 -9
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/session/toolkit.d.ts +7 -6
- package/dist/types/src/session/toolkit.d.ts.map +1 -1
- package/dist/types/src/session/version-pin.d.ts +2 -2
- package/dist/types/src/session/version-pin.d.ts.map +1 -1
- package/dist/types/src/{types.d.ts → types/artifact.d.ts} +3 -3
- package/dist/types/src/types/artifact.d.ts.map +1 -0
- package/dist/types/src/types/index.d.ts +2 -0
- package/dist/types/src/types/index.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +26 -31
- package/src/conversation/context.ts +49 -17
- package/src/conversation/conversation.ts +23 -18
- package/src/execution-graph.test.ts +738 -0
- package/src/execution-graph.ts +500 -0
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +3 -1
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +3 -1
- package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +2 -1
- package/src/extraction/extraction.ts +2 -2
- package/src/extraction/quotes.ts +2 -2
- package/src/functions/services.ts +85 -46
- package/src/index.ts +2 -1
- package/src/session/artifact-diff.ts +2 -2
- package/src/session/error-handling.ts +6 -4
- package/src/session/format.test.ts +1 -1
- package/src/session/format.ts +8 -5
- package/src/session/observer.ts +2 -1
- package/src/session/session.conversations.json +1 -0
- package/src/session/session.test.ts +50 -209
- package/src/session/session.ts +31 -20
- package/src/session/toolkit.ts +19 -13
- package/src/session/version-pin.ts +2 -2
- package/src/summarization/summarization.test.ts +8 -3
- package/src/templates/system.tpl +10 -12
- package/src/{types.ts → types/artifact.ts} +2 -2
- package/src/types/index.ts +5 -0
- package/dist/lib/browser/chunk-3K5SRE5A.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-W7AIB6DP.mjs.map +0 -7
- package/dist/types/src/session/version-pin.test.d.ts +0 -2
- package/dist/types/src/session/version-pin.test.d.ts.map +0 -1
- package/dist/types/src/types.d.ts.map +0 -1
- package/src/session/__snapshots__/session.test.ts.snap +0 -32
- package/src/session/version-pin.test.ts +0 -7
|
@@ -6,14 +6,18 @@ import {
|
|
|
6
6
|
GenerationObserver,
|
|
7
7
|
VersionPin,
|
|
8
8
|
createToolkit
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-CWGVG235.mjs";
|
|
10
10
|
|
|
11
11
|
// src/conversation/context.ts
|
|
12
12
|
import { computed } from "@preact/signals-core";
|
|
13
|
-
import
|
|
14
|
-
import
|
|
13
|
+
import * as Array2 from "effect/Array";
|
|
14
|
+
import * as Context from "effect/Context";
|
|
15
|
+
import * as Function from "effect/Function";
|
|
16
|
+
import * as Schema from "effect/Schema";
|
|
15
17
|
import { Blueprint } from "@dxos/blueprints";
|
|
16
|
-
import {
|
|
18
|
+
import { Resource } from "@dxos/context";
|
|
19
|
+
import { DXN, Filter, Obj, Query, Type } from "@dxos/echo";
|
|
20
|
+
import { invariant } from "@dxos/invariant";
|
|
17
21
|
import { ComplexSet } from "@dxos/util";
|
|
18
22
|
function _define_property(obj, key, value) {
|
|
19
23
|
if (key in obj) {
|
|
@@ -28,6 +32,7 @@ function _define_property(obj, key, value) {
|
|
|
28
32
|
}
|
|
29
33
|
return obj;
|
|
30
34
|
}
|
|
35
|
+
var __dxlog_file = "/__w/dxos/dxos/packages/core/assistant/src/conversation/context.ts";
|
|
31
36
|
var ContextBinding = Schema.Struct({
|
|
32
37
|
blueprints: Schema.Struct({
|
|
33
38
|
added: Schema.Array(Type.Ref(Blueprint.Blueprint)),
|
|
@@ -45,8 +50,8 @@ var ContextBinding = Schema.Struct({
|
|
|
45
50
|
var Bindings = class {
|
|
46
51
|
toJSON() {
|
|
47
52
|
return {
|
|
48
|
-
blueprints:
|
|
49
|
-
objects:
|
|
53
|
+
blueprints: Array2.fromIterable(this.blueprints).map((ref) => ref.dxn.toString()),
|
|
54
|
+
objects: Array2.fromIterable(this.objects).map((ref) => ref.dxn.toString())
|
|
50
55
|
};
|
|
51
56
|
}
|
|
52
57
|
constructor() {
|
|
@@ -54,21 +59,75 @@ var Bindings = class {
|
|
|
54
59
|
_define_property(this, "objects", new ComplexSet((ref) => ref.dxn.asEchoDXN()?.echoId));
|
|
55
60
|
}
|
|
56
61
|
};
|
|
57
|
-
var AiContextBinder = class {
|
|
62
|
+
var AiContextBinder = class extends Resource {
|
|
63
|
+
get bindings() {
|
|
64
|
+
invariant(this._bindings, "AiContextBinder not open", {
|
|
65
|
+
F: __dxlog_file,
|
|
66
|
+
L: 78,
|
|
67
|
+
S: this,
|
|
68
|
+
A: [
|
|
69
|
+
"this._bindings",
|
|
70
|
+
"'AiContextBinder not open'"
|
|
71
|
+
]
|
|
72
|
+
});
|
|
73
|
+
return this._bindings;
|
|
74
|
+
}
|
|
75
|
+
get blueprints() {
|
|
76
|
+
invariant(this._blueprints, "AiContextBinder not open", {
|
|
77
|
+
F: __dxlog_file,
|
|
78
|
+
L: 83,
|
|
79
|
+
S: this,
|
|
80
|
+
A: [
|
|
81
|
+
"this._blueprints",
|
|
82
|
+
"'AiContextBinder not open'"
|
|
83
|
+
]
|
|
84
|
+
});
|
|
85
|
+
return this._blueprints;
|
|
86
|
+
}
|
|
87
|
+
get objects() {
|
|
88
|
+
invariant(this._objects, "AiContextBinder not open", {
|
|
89
|
+
F: __dxlog_file,
|
|
90
|
+
L: 88,
|
|
91
|
+
S: this,
|
|
92
|
+
A: [
|
|
93
|
+
"this._objects",
|
|
94
|
+
"'AiContextBinder not open'"
|
|
95
|
+
]
|
|
96
|
+
});
|
|
97
|
+
return this._objects;
|
|
98
|
+
}
|
|
99
|
+
// TODO(wittjosiah): Use parent context?
|
|
100
|
+
async _open() {
|
|
101
|
+
const query = this._queue.query(Query.select(Filter.everything()));
|
|
102
|
+
this._ctx.onDispose(query.subscribe(() => {
|
|
103
|
+
}));
|
|
104
|
+
this._bindings = computed(() => this._reduce(query.objects));
|
|
105
|
+
this._blueprints = computed(() => [
|
|
106
|
+
...this.bindings.value.blueprints
|
|
107
|
+
]);
|
|
108
|
+
this._objects = computed(() => [
|
|
109
|
+
...this.bindings.value.objects
|
|
110
|
+
]);
|
|
111
|
+
}
|
|
112
|
+
async _close() {
|
|
113
|
+
this._bindings = void 0;
|
|
114
|
+
this._blueprints = void 0;
|
|
115
|
+
this._objects = void 0;
|
|
116
|
+
}
|
|
58
117
|
/**
|
|
59
118
|
* Asynchronous query of all bindings.
|
|
60
119
|
*/
|
|
61
120
|
async query() {
|
|
62
|
-
const
|
|
63
|
-
return this._reduce(
|
|
121
|
+
const { objects } = await this._queue.query(Query.select(Filter.everything())).run();
|
|
122
|
+
return this._reduce(objects);
|
|
64
123
|
}
|
|
65
124
|
// TODO(burdon): Pass in Blueprint obj (from registry?) and create reference.
|
|
66
125
|
async bind(props) {
|
|
67
|
-
if (!props.blueprints?.length && !props.objects?.length) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
126
|
const blueprints = props.blueprints?.filter((ref) => !this.blueprints.peek().find((b) => b.dxn.toString() === ref.dxn.toString())) ?? [];
|
|
71
127
|
const objects = props.objects?.filter((ref) => !this.objects.peek().find((o) => o.dxn.toString() === ref.dxn.toString())) ?? [];
|
|
128
|
+
if (!blueprints.length && !objects.length) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
72
131
|
await this._queue.append([
|
|
73
132
|
Obj.make(ContextBinding, {
|
|
74
133
|
blueprints: {
|
|
@@ -100,7 +159,7 @@ var AiContextBinder = class {
|
|
|
100
159
|
]);
|
|
101
160
|
}
|
|
102
161
|
_reduce(items) {
|
|
103
|
-
return pipe(items,
|
|
162
|
+
return Function.pipe(items, Array2.filter(Obj.instanceOf(ContextBinding)), Array2.reduce(new Bindings(), (context, item) => {
|
|
104
163
|
item.blueprints.removed.forEach((ref) => context.blueprints.delete(ref));
|
|
105
164
|
item.blueprints.added.forEach((ref) => context.blueprints.add(ref));
|
|
106
165
|
item.objects.removed.forEach((ref) => {
|
|
@@ -115,25 +174,21 @@ var AiContextBinder = class {
|
|
|
115
174
|
}));
|
|
116
175
|
}
|
|
117
176
|
constructor(_queue) {
|
|
118
|
-
_define_property(this, "_queue", void 0)
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
this._queue = _queue;
|
|
123
|
-
this.bindings = computed(() => this._reduce(this._queue.objects));
|
|
124
|
-
this.blueprints = computed(() => [
|
|
125
|
-
...this.bindings.value.blueprints
|
|
126
|
-
]);
|
|
127
|
-
this.objects = computed(() => [
|
|
128
|
-
...this.bindings.value.objects
|
|
129
|
-
]);
|
|
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;
|
|
130
182
|
}
|
|
131
183
|
};
|
|
132
184
|
var AiContextService = class extends Context.Tag("@dxos/assistant/AiContextService")() {
|
|
133
185
|
};
|
|
134
186
|
|
|
135
187
|
// src/conversation/conversation.ts
|
|
136
|
-
import
|
|
188
|
+
import * as Array4 from "effect/Array";
|
|
189
|
+
import * as Effect from "effect/Effect";
|
|
190
|
+
import * as Option from "effect/Option";
|
|
191
|
+
import { Resource as Resource2 } from "@dxos/context";
|
|
137
192
|
import { Obj as Obj2 } from "@dxos/echo";
|
|
138
193
|
import { DatabaseService } from "@dxos/functions";
|
|
139
194
|
import { log } from "@dxos/log";
|
|
@@ -151,8 +206,14 @@ function _define_property2(obj, key, value) {
|
|
|
151
206
|
}
|
|
152
207
|
return obj;
|
|
153
208
|
}
|
|
154
|
-
var
|
|
155
|
-
var AiConversation = class {
|
|
209
|
+
var __dxlog_file2 = "/__w/dxos/dxos/packages/core/assistant/src/conversation/conversation.ts";
|
|
210
|
+
var AiConversation = class extends Resource2 {
|
|
211
|
+
async _open() {
|
|
212
|
+
await this._context.open();
|
|
213
|
+
}
|
|
214
|
+
async _close() {
|
|
215
|
+
await this._context.close();
|
|
216
|
+
}
|
|
156
217
|
get context() {
|
|
157
218
|
return this._context;
|
|
158
219
|
}
|
|
@@ -171,8 +232,8 @@ var AiConversation = class {
|
|
|
171
232
|
const session = new AiSession();
|
|
172
233
|
const history = yield* Effect.promise(() => this.getHistory());
|
|
173
234
|
const context = yield* Effect.promise(() => this.context.query());
|
|
174
|
-
const blueprints = yield* Effect.forEach(context.blueprints.values(), DatabaseService.loadOption).pipe(Effect.map(
|
|
175
|
-
const objects = yield* Effect.forEach(context.objects.values(), DatabaseService.loadOption).pipe(Effect.map(
|
|
235
|
+
const blueprints = yield* Effect.forEach(context.blueprints.values(), DatabaseService.loadOption).pipe(Effect.map(Array4.filter(Option.isSome)), Effect.map(Array4.map((option) => option.value)));
|
|
236
|
+
const objects = yield* Effect.forEach(context.objects.values(), DatabaseService.loadOption).pipe(Effect.map(Array4.filter(Option.isSome)), Effect.map(Array4.map((option) => option.value)));
|
|
176
237
|
const toolkit = yield* createToolkit({
|
|
177
238
|
blueprints
|
|
178
239
|
});
|
|
@@ -184,8 +245,8 @@ var AiConversation = class {
|
|
|
184
245
|
objects: objects.length,
|
|
185
246
|
tools: this._toolkit?.tools.length ?? 0
|
|
186
247
|
}, {
|
|
187
|
-
F:
|
|
188
|
-
L:
|
|
248
|
+
F: __dxlog_file2,
|
|
249
|
+
L: 107,
|
|
189
250
|
S: this,
|
|
190
251
|
C: (f, a) => f(...a)
|
|
191
252
|
});
|
|
@@ -202,8 +263,8 @@ var AiConversation = class {
|
|
|
202
263
|
messages: messages.length,
|
|
203
264
|
duration: Date.now() - start
|
|
204
265
|
}, {
|
|
205
|
-
F:
|
|
206
|
-
L:
|
|
266
|
+
F: __dxlog_file2,
|
|
267
|
+
L: 121,
|
|
207
268
|
S: this,
|
|
208
269
|
C: (f, a) => f(...a)
|
|
209
270
|
});
|
|
@@ -211,11 +272,18 @@ var AiConversation = class {
|
|
|
211
272
|
return messages;
|
|
212
273
|
}).pipe(Effect.withSpan("AiConversation.request"));
|
|
213
274
|
}
|
|
214
|
-
constructor(
|
|
215
|
-
|
|
216
|
-
|
|
275
|
+
constructor(queue) {
|
|
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
|
+
*/
|
|
217
285
|
_define_property2(this, "_toolkit", void 0);
|
|
218
|
-
this._queue =
|
|
286
|
+
this._queue = queue;
|
|
219
287
|
this._context = new AiContextBinder(this._queue);
|
|
220
288
|
}
|
|
221
289
|
};
|
|
@@ -225,7 +293,7 @@ import { next as A } from "@automerge/automerge";
|
|
|
225
293
|
import { DocAccessor, toCursorRange } from "@dxos/echo-db";
|
|
226
294
|
import { log as log2 } from "@dxos/log";
|
|
227
295
|
import { isNonNullable } from "@dxos/util";
|
|
228
|
-
var
|
|
296
|
+
var __dxlog_file3 = "/__w/dxos/dxos/packages/core/assistant/src/diff.ts";
|
|
229
297
|
var reduceDiffs = (diffs) => {
|
|
230
298
|
return diffs.reduce((acc, diff) => {
|
|
231
299
|
const current = acc[acc.length - 1];
|
|
@@ -278,7 +346,7 @@ var applyDiffs = (accessor, diffs) => {
|
|
|
278
346
|
A.splice(doc, accessor.path, idx, diff.match.length, diff.replace);
|
|
279
347
|
} else {
|
|
280
348
|
log2.warn("diff not found", diff, {
|
|
281
|
-
F:
|
|
349
|
+
F: __dxlog_file3,
|
|
282
350
|
L: 73,
|
|
283
351
|
S: void 0,
|
|
284
352
|
C: (f, a) => f(...a)
|
|
@@ -289,76 +357,468 @@ var applyDiffs = (accessor, diffs) => {
|
|
|
289
357
|
return DocAccessor.getValue(accessor);
|
|
290
358
|
};
|
|
291
359
|
|
|
360
|
+
// src/execution-graph.ts
|
|
361
|
+
import { AgentStatus } from "@dxos/ai";
|
|
362
|
+
import { Obj as Obj3 } from "@dxos/echo";
|
|
363
|
+
import { MESSAGE_PROPERTY_TOOL_CALL_ID } from "@dxos/functions";
|
|
364
|
+
import { LogLevel } from "@dxos/log";
|
|
365
|
+
import { ContentBlock, DataType as DataType2 } from "@dxos/schema";
|
|
366
|
+
import { isNonNullable as isNonNullable2 } from "@dxos/util";
|
|
367
|
+
function _define_property3(obj, key, value) {
|
|
368
|
+
if (key in obj) {
|
|
369
|
+
Object.defineProperty(obj, key, {
|
|
370
|
+
value,
|
|
371
|
+
enumerable: true,
|
|
372
|
+
configurable: true,
|
|
373
|
+
writable: true
|
|
374
|
+
});
|
|
375
|
+
} else {
|
|
376
|
+
obj[key] = value;
|
|
377
|
+
}
|
|
378
|
+
return obj;
|
|
379
|
+
}
|
|
380
|
+
var ExecutionGraph = class {
|
|
381
|
+
/**
|
|
382
|
+
* Adds events to the graph.
|
|
383
|
+
*/
|
|
384
|
+
addEvents(events) {
|
|
385
|
+
const sortedEvents = this.sortObjectsByCreated(events);
|
|
386
|
+
for (const event of sortedEvents) {
|
|
387
|
+
if (Obj3.instanceOf(DataType2.Message, event)) {
|
|
388
|
+
this._processMessage(event);
|
|
389
|
+
} else if (Obj3.instanceOf(AgentStatus, event)) {
|
|
390
|
+
this._processAgentStatus(event);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Processes a message event and creates commits for its blocks.
|
|
396
|
+
*/
|
|
397
|
+
_processMessage(message) {
|
|
398
|
+
const messageCommits = messageToCommits(message, this._lastBlockId, this._toolCallCommitIds, this._lastCommitByBranch);
|
|
399
|
+
this._commits.push(...messageCommits);
|
|
400
|
+
messageCommits.forEach((commit) => this._branchNames.add(commit.branch));
|
|
401
|
+
this._trackToolCalls(messageCommits);
|
|
402
|
+
if (messageCommits.length > 0) {
|
|
403
|
+
this._lastBlockId = messageCommits[messageCommits.length - 1].id;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Processes an AgentStatus event.
|
|
408
|
+
*/
|
|
409
|
+
_processAgentStatus(event) {
|
|
410
|
+
const branch = this._getToolCallBranch(event.parentMessage, event.toolCallId);
|
|
411
|
+
this._branchNames.add(branch);
|
|
412
|
+
const agentStatusCommit = {
|
|
413
|
+
id: event.id,
|
|
414
|
+
branch,
|
|
415
|
+
message: event.message,
|
|
416
|
+
icon: "ph--flag--regular",
|
|
417
|
+
parents: this._getAgentStatusParents(event),
|
|
418
|
+
timestamp: new Date(event.created)
|
|
419
|
+
};
|
|
420
|
+
this._commits.push(agentStatusCommit);
|
|
421
|
+
this._lastBlockId = event.id;
|
|
422
|
+
if (event.toolCallId) {
|
|
423
|
+
this._lastCommitByBranch.set(branch, event.id);
|
|
424
|
+
this._updatePendingToolResults(event.toolCallId, event.id);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Stable sort: objects with 'created' field are sorted by it, others remain in place.
|
|
429
|
+
*/
|
|
430
|
+
sortObjectsByCreated(objects) {
|
|
431
|
+
const sortedObjects = objects.slice();
|
|
432
|
+
const createdObjects = [];
|
|
433
|
+
sortedObjects.forEach((obj, idx) => {
|
|
434
|
+
if (obj && typeof obj.created === "string") {
|
|
435
|
+
createdObjects.push({
|
|
436
|
+
idx,
|
|
437
|
+
obj
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
});
|
|
441
|
+
createdObjects.sort((a, b) => {
|
|
442
|
+
const aDate = new Date(a.obj.created).getTime();
|
|
443
|
+
const bDate = new Date(b.obj.created).getTime();
|
|
444
|
+
return aDate - bDate;
|
|
445
|
+
});
|
|
446
|
+
let createdIdx = 0;
|
|
447
|
+
return sortedObjects.map((obj, _idx) => {
|
|
448
|
+
if (obj && typeof obj.created === "string") {
|
|
449
|
+
return createdObjects[createdIdx++].obj;
|
|
450
|
+
}
|
|
451
|
+
return obj;
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* Returns the current state of the graph.
|
|
456
|
+
*/
|
|
457
|
+
getGraph(lastRequest = false) {
|
|
458
|
+
const idx = lastRequest ? this._commits.findLastIndex((c) => c.tags?.includes("user")) : -1;
|
|
459
|
+
const commits = idx === -1 ? this._commits : this._commits.slice(idx);
|
|
460
|
+
return {
|
|
461
|
+
branches: Array.from(this._branchNames),
|
|
462
|
+
commits
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Tracks tool calls and handles pending tool result updates.
|
|
467
|
+
*/
|
|
468
|
+
_trackToolCalls(commits) {
|
|
469
|
+
commits.forEach((commit) => {
|
|
470
|
+
if (this._isToolCallCommit(commit)) {
|
|
471
|
+
const toolCallId = this._extractToolCallId(commit.id);
|
|
472
|
+
if (toolCallId) {
|
|
473
|
+
this._toolCallCommitIds.set(toolCallId, commit.id);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
if (this._isToolResultCommit(commit)) {
|
|
477
|
+
const toolCallId = this._extractToolCallId(commit.id);
|
|
478
|
+
if (toolCallId) {
|
|
479
|
+
this._pendingToolResults.set(toolCallId, commit.id);
|
|
480
|
+
this._handleLateToolResultUpdate(toolCallId, commit.id);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
this._lastCommitByBranch.set(commit.branch, commit.id);
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* Updates pending tool results to point to an AgentStatus event.
|
|
488
|
+
*/
|
|
489
|
+
_updatePendingToolResults(toolCallId, agentStatusId) {
|
|
490
|
+
const toolResultId = this._pendingToolResults.get(toolCallId);
|
|
491
|
+
if (!toolResultId) return;
|
|
492
|
+
const toolResultCommit = this._commits.find((c) => c.id === toolResultId);
|
|
493
|
+
if (!toolResultCommit) return;
|
|
494
|
+
const toolCallCommitId = this._toolCallCommitIds.get(toolCallId);
|
|
495
|
+
if (!toolCallCommitId) return;
|
|
496
|
+
const toolCallIndex = toolResultCommit.parents.indexOf(toolCallCommitId);
|
|
497
|
+
if (toolCallIndex !== -1) {
|
|
498
|
+
toolResultCommit.parents[toolCallIndex] = agentStatusId;
|
|
499
|
+
}
|
|
500
|
+
this._pendingToolResults.delete(toolCallId);
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Handles late updates when AgentStatus is processed before tool result.
|
|
504
|
+
*/
|
|
505
|
+
_handleLateToolResultUpdate(toolCallId, toolResultId) {
|
|
506
|
+
const toolCallCommitId = this._toolCallCommitIds.get(toolCallId);
|
|
507
|
+
if (!toolCallCommitId) return;
|
|
508
|
+
const messageId = toolCallCommitId.split("_toolCall_")[0];
|
|
509
|
+
const toolCallBranch = `${messageId}_${toolCallId}`;
|
|
510
|
+
const agentStatusCommitId = this._lastCommitByBranch.get(toolCallBranch);
|
|
511
|
+
if (agentStatusCommitId && agentStatusCommitId !== toolCallCommitId) {
|
|
512
|
+
const toolResultCommit = this._commits.find((c) => c.id === toolResultId);
|
|
513
|
+
if (toolResultCommit) {
|
|
514
|
+
const toolCallIndex = toolResultCommit.parents.indexOf(toolCallCommitId);
|
|
515
|
+
if (toolCallIndex !== -1) {
|
|
516
|
+
toolResultCommit.parents[toolCallIndex] = agentStatusCommitId;
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
this._pendingToolResults.delete(toolCallId);
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* Gets the branch name for a tool call.
|
|
524
|
+
*/
|
|
525
|
+
_getToolCallBranch(parentMessage, toolCallId) {
|
|
526
|
+
if (toolCallId) {
|
|
527
|
+
return toolCallId;
|
|
528
|
+
}
|
|
529
|
+
return parentMessage || "main";
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Gets the parents for an AgentStatus commit.
|
|
533
|
+
*/
|
|
534
|
+
_getAgentStatusParents(event) {
|
|
535
|
+
if (event.parentMessage && event.toolCallId) {
|
|
536
|
+
return [
|
|
537
|
+
getToolCallId(event.parentMessage, event.toolCallId)
|
|
538
|
+
];
|
|
539
|
+
}
|
|
540
|
+
return [];
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* Checks if a commit is a tool call commit.
|
|
544
|
+
*/
|
|
545
|
+
_isToolCallCommit(commit) {
|
|
546
|
+
return commit.message.includes("Calling tool");
|
|
547
|
+
}
|
|
548
|
+
/**
|
|
549
|
+
* Checks if a commit is a tool result commit.
|
|
550
|
+
*/
|
|
551
|
+
_isToolResultCommit(commit) {
|
|
552
|
+
return commit.message.includes("Tool call succeeded") || commit.message.includes("Tool error:");
|
|
553
|
+
}
|
|
554
|
+
/**
|
|
555
|
+
* Extracts tool call ID from a commit ID.
|
|
556
|
+
*/
|
|
557
|
+
_extractToolCallId(commitId) {
|
|
558
|
+
const toolCallMatch = commitId.match(/_toolCall_(.+)$/);
|
|
559
|
+
const toolResultMatch = commitId.match(/_toolResult_(.+)$/);
|
|
560
|
+
return toolCallMatch?.[1] || toolResultMatch?.[1];
|
|
561
|
+
}
|
|
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
|
+
};
|
|
571
|
+
var messageToCommits = (message, lastBlockId, toolCallIds, lastCommitByBranch) => {
|
|
572
|
+
let previousBlockId = lastBlockId;
|
|
573
|
+
return message.blocks.map((block, idx) => {
|
|
574
|
+
const branch = getMessageBranch(message);
|
|
575
|
+
const parents = getBlockParents(block, previousBlockId, message, toolCallIds, lastCommitByBranch);
|
|
576
|
+
const commit = createBlockCommit(block, message, branch, parents, idx);
|
|
577
|
+
if (commit) {
|
|
578
|
+
previousBlockId = commit.id;
|
|
579
|
+
}
|
|
580
|
+
return commit;
|
|
581
|
+
}).filter(isNonNullable2);
|
|
582
|
+
};
|
|
583
|
+
var getBlockParents = (block, previousBlockId, message, toolCallIds, lastCommitByBranch) => {
|
|
584
|
+
const parents = [];
|
|
585
|
+
if (block._tag === "toolResult") {
|
|
586
|
+
if (previousBlockId) {
|
|
587
|
+
parents.push(previousBlockId);
|
|
588
|
+
}
|
|
589
|
+
const toolCallCommitId = toolCallIds?.get(block.toolCallId);
|
|
590
|
+
if (toolCallCommitId) {
|
|
591
|
+
const toolCallBranch = getToolCallBranchFromCommitId(toolCallCommitId, block.toolCallId);
|
|
592
|
+
const lastBlockFromToolCallBranch = lastCommitByBranch?.get(toolCallBranch);
|
|
593
|
+
if (lastBlockFromToolCallBranch) {
|
|
594
|
+
parents.push(lastBlockFromToolCallBranch);
|
|
595
|
+
} else {
|
|
596
|
+
parents.push(toolCallCommitId);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
} else {
|
|
600
|
+
if (previousBlockId) {
|
|
601
|
+
parents.push(previousBlockId);
|
|
602
|
+
} else {
|
|
603
|
+
const toolCallParent = getParentId(message);
|
|
604
|
+
if (toolCallParent) {
|
|
605
|
+
parents.push(toolCallParent);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
return parents;
|
|
610
|
+
};
|
|
611
|
+
var createBlockCommit = (block, message, branch, parents, idx) => {
|
|
612
|
+
const timestamp = new Date(message.created);
|
|
613
|
+
switch (block._tag) {
|
|
614
|
+
case "text":
|
|
615
|
+
if (!block.text.trim().length) return null;
|
|
616
|
+
return {
|
|
617
|
+
id: getGenericBlockId(message.id, idx),
|
|
618
|
+
branch,
|
|
619
|
+
parents,
|
|
620
|
+
timestamp,
|
|
621
|
+
...message.sender.role === "user" ? {
|
|
622
|
+
icon: "ph--user--regular",
|
|
623
|
+
tags: [
|
|
624
|
+
"user"
|
|
625
|
+
],
|
|
626
|
+
message: "Processing request..."
|
|
627
|
+
} : {
|
|
628
|
+
icon: "ph--robot--regular",
|
|
629
|
+
message: `Response (${block.text.split(" ").length} words)`
|
|
630
|
+
}
|
|
631
|
+
};
|
|
632
|
+
case "toolCall":
|
|
633
|
+
return {
|
|
634
|
+
id: getToolCallId(message.id, block.toolCallId),
|
|
635
|
+
branch,
|
|
636
|
+
parents,
|
|
637
|
+
timestamp,
|
|
638
|
+
icon: "ph--wrench--regular",
|
|
639
|
+
level: LogLevel.INFO,
|
|
640
|
+
message: `Calling tool (${block.name})`
|
|
641
|
+
};
|
|
642
|
+
case "toolResult":
|
|
643
|
+
return {
|
|
644
|
+
id: getToolResultId(message.id, block.toolCallId),
|
|
645
|
+
branch,
|
|
646
|
+
parents,
|
|
647
|
+
timestamp,
|
|
648
|
+
icon: block.error ? "ph--x-circle--regular" : "ph--check-circle--regular",
|
|
649
|
+
level: block.error ? LogLevel.ERROR : LogLevel.INFO,
|
|
650
|
+
message: block.error ? "Tool error: " + block.error : "Tool call succeeded"
|
|
651
|
+
};
|
|
652
|
+
case "summary":
|
|
653
|
+
return {
|
|
654
|
+
id: getGenericBlockId(message.id, idx),
|
|
655
|
+
branch,
|
|
656
|
+
parents,
|
|
657
|
+
timestamp,
|
|
658
|
+
icon: "ph--rocket--regular",
|
|
659
|
+
level: LogLevel.INFO,
|
|
660
|
+
message: ContentBlock.createSummaryMessage(block)
|
|
661
|
+
};
|
|
662
|
+
case "status":
|
|
663
|
+
return {
|
|
664
|
+
id: getGenericBlockId(message.id, idx),
|
|
665
|
+
branch,
|
|
666
|
+
parents,
|
|
667
|
+
timestamp,
|
|
668
|
+
message: block.statusText,
|
|
669
|
+
level: LogLevel.INFO,
|
|
670
|
+
icon: "ph--flag--regular"
|
|
671
|
+
};
|
|
672
|
+
case "reasoning":
|
|
673
|
+
return {
|
|
674
|
+
id: getGenericBlockId(message.id, idx),
|
|
675
|
+
branch,
|
|
676
|
+
parents,
|
|
677
|
+
timestamp,
|
|
678
|
+
message: block.reasoningText ?? "Thinking...",
|
|
679
|
+
icon: "ph--brain--regular"
|
|
680
|
+
};
|
|
681
|
+
case "reference":
|
|
682
|
+
return {
|
|
683
|
+
id: getGenericBlockId(message.id, idx),
|
|
684
|
+
branch,
|
|
685
|
+
parents,
|
|
686
|
+
timestamp,
|
|
687
|
+
icon: "ph--link--regular",
|
|
688
|
+
message: stringifyRef(block.reference)
|
|
689
|
+
};
|
|
690
|
+
default:
|
|
691
|
+
return null;
|
|
692
|
+
}
|
|
693
|
+
};
|
|
694
|
+
var getToolCallBranchFromCommitId = (toolCallCommitId, toolCallId) => {
|
|
695
|
+
const messageId = toolCallCommitId.split("_toolCall_")[0];
|
|
696
|
+
return `${messageId}_${toolCallId}`;
|
|
697
|
+
};
|
|
698
|
+
var getToolCallId = (messageId, toolCallId) => `${messageId}_toolCall_${toolCallId}`;
|
|
699
|
+
var getToolResultId = (messageId, toolCallId) => `${messageId}_toolResult_${toolCallId}`;
|
|
700
|
+
var getGenericBlockId = (messageId, idx) => `${messageId}_block_${idx}`;
|
|
701
|
+
var getBranchName = (options) => {
|
|
702
|
+
if (options.toolCallId) {
|
|
703
|
+
return options.toolCallId;
|
|
704
|
+
} else if (options.parentMessage) {
|
|
705
|
+
return options.parentMessage;
|
|
706
|
+
} else {
|
|
707
|
+
return "main";
|
|
708
|
+
}
|
|
709
|
+
};
|
|
710
|
+
var getMessageBranch = (message) => {
|
|
711
|
+
return getBranchName({
|
|
712
|
+
parentMessage: message.parentMessage,
|
|
713
|
+
toolCallId: message.properties?.[MESSAGE_PROPERTY_TOOL_CALL_ID]
|
|
714
|
+
});
|
|
715
|
+
};
|
|
716
|
+
var getParentId = (message) => {
|
|
717
|
+
if (message.parentMessage && message.properties?.[MESSAGE_PROPERTY_TOOL_CALL_ID]) {
|
|
718
|
+
return getToolCallId(message.parentMessage, message.properties[MESSAGE_PROPERTY_TOOL_CALL_ID]);
|
|
719
|
+
} else {
|
|
720
|
+
return void 0;
|
|
721
|
+
}
|
|
722
|
+
};
|
|
723
|
+
var stringifyRef = (ref) => {
|
|
724
|
+
if (ref.target) {
|
|
725
|
+
return stringifyObject(ref.target);
|
|
726
|
+
}
|
|
727
|
+
return ref.dxn.asEchoDXN()?.echoId ?? ref.dxn.asQueueDXN()?.objectId ?? "";
|
|
728
|
+
};
|
|
729
|
+
var stringifyObject = (obj) => {
|
|
730
|
+
return Obj3.getLabel(obj) ?? Obj3.getTypename(obj) ?? obj.id;
|
|
731
|
+
};
|
|
732
|
+
|
|
292
733
|
// src/functions/services.ts
|
|
293
|
-
import
|
|
294
|
-
import
|
|
734
|
+
import * as Tool from "@effect/ai/Tool";
|
|
735
|
+
import * as Context2 from "effect/Context";
|
|
736
|
+
import * as Effect2 from "effect/Effect";
|
|
737
|
+
import * as Layer from "effect/Layer";
|
|
738
|
+
import * as Record from "effect/Record";
|
|
739
|
+
import * as Schema2 from "effect/Schema";
|
|
295
740
|
import { AiToolNotFoundError, ToolExecutionService, ToolResolverService } from "@dxos/ai";
|
|
296
741
|
import { todo } from "@dxos/debug";
|
|
297
|
-
import {
|
|
298
|
-
import {
|
|
299
|
-
|
|
742
|
+
import { Query as Query2 } from "@dxos/echo";
|
|
743
|
+
import { DatabaseService as DatabaseService2, FunctionDefinition, FunctionInvocationService, FunctionType } from "@dxos/functions";
|
|
744
|
+
import { invariant as invariant2 } from "@dxos/invariant";
|
|
745
|
+
var __dxlog_file4 = "/__w/dxos/dxos/packages/core/assistant/src/functions/services.ts";
|
|
300
746
|
var makeToolResolverFromFunctions = (functions, toolkit) => {
|
|
301
|
-
return Layer.
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
747
|
+
return Layer.effect(ToolResolverService, Effect2.gen(function* () {
|
|
748
|
+
const dbService = yield* DatabaseService2;
|
|
749
|
+
return {
|
|
750
|
+
resolve: (id) => Effect2.gen(function* () {
|
|
751
|
+
const tool = toolkit.tools[id];
|
|
752
|
+
if (tool) {
|
|
753
|
+
return tool;
|
|
754
|
+
}
|
|
755
|
+
const { objects: [dbFunction] } = yield* DatabaseService2.runQuery(Query2.type(FunctionType, {
|
|
756
|
+
key: id
|
|
757
|
+
}));
|
|
758
|
+
const functionDef = dbFunction ? FunctionDefinition.deserialize(dbFunction) : functions.find((fn2) => fn2.key === id);
|
|
759
|
+
if (!functionDef) {
|
|
760
|
+
return yield* Effect2.fail(new AiToolNotFoundError(id));
|
|
761
|
+
}
|
|
762
|
+
return projectFunctionToTool(functionDef);
|
|
763
|
+
}).pipe(Effect2.provideService(DatabaseService2, dbService))
|
|
764
|
+
};
|
|
765
|
+
}));
|
|
314
766
|
};
|
|
315
|
-
var makeToolExecutionServiceFromFunctions = (
|
|
767
|
+
var makeToolExecutionServiceFromFunctions = (toolkit, handlersLayer) => {
|
|
316
768
|
return Layer.effect(ToolExecutionService, Effect2.gen(function* () {
|
|
317
769
|
const toolkitHandler = yield* toolkit.pipe(Effect2.provide(handlersLayer));
|
|
318
|
-
|
|
770
|
+
invariant2(isHandlerLike(toolkitHandler), void 0, {
|
|
771
|
+
F: __dxlog_file4,
|
|
772
|
+
L: 73,
|
|
773
|
+
S: this,
|
|
774
|
+
A: [
|
|
775
|
+
"isHandlerLike(toolkitHandler)",
|
|
776
|
+
""
|
|
777
|
+
]
|
|
778
|
+
});
|
|
779
|
+
const functionInvocationService = yield* FunctionInvocationService;
|
|
319
780
|
return {
|
|
320
781
|
handlersFor: (toolkit2) => {
|
|
321
782
|
const makeHandler = (tool) => {
|
|
322
|
-
return Effect2.fn(
|
|
323
|
-
if (toolkitHandler.tools
|
|
783
|
+
return Effect2.fn(`toolFunctionHandler ${tool.name}`)(function* (input) {
|
|
784
|
+
if (toolkitHandler.tools[tool.name]) {
|
|
785
|
+
if (Tool.isProviderDefined(tool)) {
|
|
786
|
+
throw new Error("Attempted to call a provider-defined tool");
|
|
787
|
+
}
|
|
324
788
|
return yield* toolkitHandler.handle(tool.name, input);
|
|
325
789
|
}
|
|
326
|
-
const {
|
|
327
|
-
|
|
328
|
-
if (!functionDef) {
|
|
329
|
-
return yield* Effect2.fail(new AiToolNotFoundError(tool.name));
|
|
330
|
-
}
|
|
331
|
-
return yield* localFunctionExecutionService.invokeFunction(functionDef, input).pipe(Effect2.catchAllDefect((defect) => Effect2.fail(defect)));
|
|
790
|
+
const { definition: functionDef } = Context2.get(FunctionToolAnnotation)(tool.annotations);
|
|
791
|
+
return yield* functionInvocationService.invokeFunction(functionDef, input).pipe(Effect2.catchAllDefect((defect) => Effect2.fail(defect)));
|
|
332
792
|
});
|
|
333
793
|
};
|
|
334
|
-
return toolkit2.of(Record.map(toolkit2.tools, (tool, _name) => makeHandler(tool)));
|
|
794
|
+
return toolkit2.of(Record.map(toolkit2.tools, (tool, _name) => Tool.isUserDefined(tool) ? makeHandler(tool) : null));
|
|
335
795
|
}
|
|
336
796
|
};
|
|
337
797
|
}));
|
|
338
798
|
};
|
|
339
|
-
var FunctionToolAnnotation = class extends Context2.Tag("@dxos/
|
|
799
|
+
var FunctionToolAnnotation = class extends Context2.Tag("@dxos/assistant/FunctionToolAnnotation")() {
|
|
340
800
|
};
|
|
341
801
|
var toolCache = /* @__PURE__ */ new WeakMap();
|
|
342
|
-
var projectFunctionToTool = (
|
|
343
|
-
if (toolCache.has(
|
|
344
|
-
return toolCache.get(
|
|
802
|
+
var projectFunctionToTool = (fn2) => {
|
|
803
|
+
if (toolCache.has(fn2)) {
|
|
804
|
+
return toolCache.get(fn2);
|
|
345
805
|
}
|
|
346
|
-
const tool =
|
|
347
|
-
description:
|
|
348
|
-
parameters: createStructFieldsFromSchema(
|
|
806
|
+
const tool = Tool.make(makeToolName(fn2.name), {
|
|
807
|
+
description: fn2.description,
|
|
808
|
+
parameters: createStructFieldsFromSchema(fn2.inputSchema),
|
|
349
809
|
// TODO(dmaretskyi): Include output schema.
|
|
350
810
|
failure: Schema2.Any
|
|
351
811
|
}).annotate(FunctionToolAnnotation, {
|
|
352
|
-
|
|
812
|
+
definition: fn2
|
|
353
813
|
});
|
|
354
|
-
toolCache.set(
|
|
814
|
+
toolCache.set(fn2, tool);
|
|
355
815
|
return tool;
|
|
356
816
|
};
|
|
357
817
|
var makeToolName = (name) => {
|
|
358
818
|
const toolName = name.replace(/[^a-zA-Z0-9]/g, "_");
|
|
359
|
-
|
|
360
|
-
F:
|
|
361
|
-
L:
|
|
819
|
+
invariant2(toolName.match(/^[a-zA-Z_][a-zA-Z0-9-_]*$/), void 0, {
|
|
820
|
+
F: __dxlog_file4,
|
|
821
|
+
L: 140,
|
|
362
822
|
S: void 0,
|
|
363
823
|
A: [
|
|
364
824
|
"toolName.match(/^[a-zA-Z_][a-zA-Z0-9-_]*$/)",
|
|
@@ -380,16 +840,19 @@ var createStructFieldsFromSchema = (schema) => {
|
|
|
380
840
|
return todo(`Unsupported schema AST: ${schema.ast._tag}`);
|
|
381
841
|
}
|
|
382
842
|
};
|
|
843
|
+
var isHandlerLike = (value) => {
|
|
844
|
+
return typeof value.tools === "object" && typeof value.handle === "function";
|
|
845
|
+
};
|
|
383
846
|
|
|
384
847
|
// src/templates/system.ts
|
|
385
848
|
import defaultsDeep from "lodash.defaultsdeep";
|
|
386
849
|
import { Template } from "@dxos/blueprints";
|
|
387
850
|
|
|
388
|
-
// raw-loader
|
|
851
|
+
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/format.tpl?raw
|
|
389
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";
|
|
390
853
|
|
|
391
|
-
// raw-loader
|
|
392
|
-
var system_default = "{{! System Prompt }}\n\
|
|
854
|
+
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/system.tpl?raw
|
|
855
|
+
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";
|
|
393
856
|
|
|
394
857
|
// src/templates/system.ts
|
|
395
858
|
var createSystemPrompt = (options = {}) => Template.process(format_default, defaultsDeep(options, {
|
|
@@ -406,8 +869,8 @@ var templates = {
|
|
|
406
869
|
})
|
|
407
870
|
};
|
|
408
871
|
|
|
409
|
-
// src/types.ts
|
|
410
|
-
import
|
|
872
|
+
// src/types/artifact.ts
|
|
873
|
+
import * as Schema3 from "effect/Schema";
|
|
411
874
|
import { DXN as DXN2, LOCAL_SPACE_TAG } from "@dxos/keys";
|
|
412
875
|
import { trim } from "@dxos/util";
|
|
413
876
|
var createArtifactElement = (id) => `<artifact id=${id} />`;
|
|
@@ -454,6 +917,7 @@ export {
|
|
|
454
917
|
ArtifactId,
|
|
455
918
|
Bindings,
|
|
456
919
|
ContextBinding,
|
|
920
|
+
ExecutionGraph,
|
|
457
921
|
GenerationObserver,
|
|
458
922
|
VersionPin,
|
|
459
923
|
applyDiffs,
|