@dxos/assistant 0.8.4-main.c85a9c8dae → 0.8.4-main.cb12b3f963
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/neutral/extraction/index.mjs +29 -50
- package/dist/lib/neutral/extraction/index.mjs.map +4 -4
- package/dist/lib/neutral/index.mjs +765 -362
- package/dist/lib/neutral/index.mjs.map +4 -4
- package/dist/lib/neutral/meta.json +1 -1
- package/dist/types/src/conversation/context.d.ts +28 -22
- package/dist/types/src/conversation/context.d.ts.map +1 -1
- package/dist/types/src/conversation/index.d.ts +1 -1
- package/dist/types/src/conversation/index.d.ts.map +1 -1
- package/dist/types/src/conversation/session.d.ts +78 -0
- package/dist/types/src/conversation/session.d.ts.map +1 -0
- package/dist/types/src/diff.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +12 -12
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/execution-graph.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction-ner-function/index.d.ts +0 -1
- package/dist/types/src/extraction/extraction-ner-function/index.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction.d.ts +867 -153
- package/dist/types/src/extraction/extraction.d.ts.map +1 -1
- package/dist/types/src/extraction/index.d.ts +0 -1
- package/dist/types/src/extraction/index.d.ts.map +1 -1
- package/dist/types/src/extraction/quotes.d.ts.map +1 -1
- package/dist/types/src/functions/services.d.ts +14 -16
- package/dist/types/src/functions/services.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +1 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/session/artifact-diff.d.ts.map +1 -1
- package/dist/types/src/session/error-handling.d.ts.map +1 -1
- package/dist/types/src/session/format.d.ts +6 -3
- package/dist/types/src/session/format.d.ts.map +1 -1
- package/dist/types/src/session/index.d.ts +1 -1
- package/dist/types/src/session/request.d.ts +104 -0
- package/dist/types/src/session/request.d.ts.map +1 -0
- package/dist/types/src/session/toolkit.d.ts +5 -7
- package/dist/types/src/session/toolkit.d.ts.map +1 -1
- package/dist/types/src/templates/system.d.ts +1 -2
- package/dist/types/src/templates/system.d.ts.map +1 -1
- package/dist/types/src/tracing.d.ts +233 -0
- package/dist/types/src/tracing.d.ts.map +1 -0
- package/dist/types/src/types/artifact.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +24 -40
- package/src/conversation/context.test.ts +19 -13
- package/src/conversation/context.ts +128 -55
- package/src/conversation/index.ts +1 -1
- package/src/conversation/session.ts +334 -0
- package/src/diff.test.ts +3 -3
- package/src/execution-graph.test.ts +1 -1
- package/src/execution-graph.ts +16 -6
- package/src/extraction/extraction-ner-function/index.ts +0 -1
- package/src/extraction/extraction.ts +36 -8
- package/src/extraction/index.ts +0 -1
- package/src/functions/services.ts +57 -52
- package/src/index.ts +1 -0
- package/src/session/format.ts +14 -10
- package/src/session/index.ts +1 -1
- package/src/session/request.ts +342 -0
- package/src/session/toolkit.ts +18 -15
- package/src/templates/instructions/system.tpl +50 -2
- package/src/templates/system.ts +7 -6
- package/src/tracing.ts +57 -0
- package/dist/lib/neutral/chunk-BE6SRJ4N.mjs +0 -193
- package/dist/lib/neutral/chunk-BE6SRJ4N.mjs.map +0 -7
- package/dist/lib/neutral/testing/index.mjs +0 -38
- package/dist/lib/neutral/testing/index.mjs.map +0 -7
- package/dist/types/src/conversation/conversation.d.ts +0 -66
- package/dist/types/src/conversation/conversation.d.ts.map +0 -1
- package/dist/types/src/conversation/conversation.test.d.ts +0 -2
- package/dist/types/src/conversation/conversation.test.d.ts.map +0 -1
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +0 -4
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +0 -1
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts +0 -2
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts.map +0 -1
- package/dist/types/src/extraction/extraction-llm-function/index.d.ts +0 -2
- package/dist/types/src/extraction/extraction-llm-function/index.d.ts.map +0 -1
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +0 -355
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +0 -1
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts +0 -2
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts.map +0 -1
- package/dist/types/src/functions.test.d.ts +0 -2
- package/dist/types/src/functions.test.d.ts.map +0 -1
- package/dist/types/src/session/format.test.d.ts +0 -2
- package/dist/types/src/session/format.test.d.ts.map +0 -1
- package/dist/types/src/session/session.d.ts +0 -61
- package/dist/types/src/session/session.d.ts.map +0 -1
- package/dist/types/src/session/session.test.d.ts +0 -2
- package/dist/types/src/session/session.test.d.ts.map +0 -1
- package/dist/types/src/testing/index.d.ts +0 -2
- package/dist/types/src/testing/index.d.ts.map +0 -1
- package/dist/types/src/testing/layer.d.ts +0 -22
- package/dist/types/src/testing/layer.d.ts.map +0 -1
- package/src/conversation/conversation.test.ts +0 -65
- package/src/conversation/conversation.ts +0 -247
- package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +0 -84
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +0 -62
- package/src/extraction/extraction-llm-function/index.ts +0 -5
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +0 -116
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +0 -56
- package/src/functions.conversations.json +0 -1
- package/src/functions.test.ts +0 -74
- package/src/session/format.test.ts +0 -124
- package/src/session/session.conversations.json +0 -1
- package/src/session/session.test.ts +0 -191
- package/src/session/session.ts +0 -251
- package/src/testing/index.ts +0 -5
- package/src/testing/layer.ts +0 -85
|
@@ -1,26 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ArtifactId,
|
|
3
|
-
RefFromLLM,
|
|
4
|
-
ToolExecutionServices,
|
|
5
|
-
createArtifactElement,
|
|
6
|
-
makeToolExecutionServiceFromFunctions,
|
|
7
|
-
makeToolResolverFromFunctions
|
|
8
|
-
} from "./chunk-BE6SRJ4N.mjs";
|
|
9
|
-
|
|
10
1
|
// src/conversation/context.ts
|
|
11
2
|
import { Atom, Registry } from "@effect-atom/atom-react";
|
|
12
3
|
import * as EArray from "effect/Array";
|
|
13
4
|
import * as Context from "effect/Context";
|
|
14
5
|
import * as Effect from "effect/Effect";
|
|
15
6
|
import * as Function from "effect/Function";
|
|
7
|
+
import * as Runtime from "effect/Runtime";
|
|
16
8
|
import * as Schema from "effect/Schema";
|
|
17
|
-
import { Blueprint } from "@dxos/
|
|
9
|
+
import { Blueprint } from "@dxos/compute";
|
|
18
10
|
import { Resource } from "@dxos/context";
|
|
19
|
-
import { DXN, Obj, Query, Ref, Type } from "@dxos/echo";
|
|
11
|
+
import { DXN, Feed, Obj, Query, Ref, Type } from "@dxos/echo";
|
|
20
12
|
import { assertArgument } from "@dxos/invariant";
|
|
21
13
|
import { log } from "@dxos/log";
|
|
22
|
-
import { ComplexSet,
|
|
23
|
-
var __dxlog_file = "/__w/dxos/dxos/packages/core/assistant/src/conversation/context.ts";
|
|
14
|
+
import { ComplexSet, isNonNullable } from "@dxos/util";
|
|
15
|
+
var __dxlog_file = "/__w/dxos/dxos/packages/core/compute/assistant/src/conversation/context.ts";
|
|
24
16
|
var ContextBinding = Schema.Struct({
|
|
25
17
|
blueprints: Schema.Struct({
|
|
26
18
|
added: Schema.Array(Ref.Ref(Blueprint.Blueprint)),
|
|
@@ -31,7 +23,7 @@ var ContextBinding = Schema.Struct({
|
|
|
31
23
|
removed: Schema.Array(Ref.Ref(Obj.Unknown))
|
|
32
24
|
})
|
|
33
25
|
}).pipe(Type.object({
|
|
34
|
-
typename: "dxos.
|
|
26
|
+
typename: "org.dxos.type.contextBinding",
|
|
35
27
|
version: "0.1.0"
|
|
36
28
|
}));
|
|
37
29
|
var Bindings = class {
|
|
@@ -49,11 +41,15 @@ var AiContextBinder = class extends Resource {
|
|
|
49
41
|
_blueprints = Atom.make([]).pipe(Atom.keepAlive);
|
|
50
42
|
_objects = Atom.make([]).pipe(Atom.keepAlive);
|
|
51
43
|
_registry;
|
|
52
|
-
|
|
44
|
+
_feed;
|
|
45
|
+
_runtime;
|
|
46
|
+
#bindingsQuery;
|
|
53
47
|
constructor(options) {
|
|
54
48
|
super();
|
|
55
|
-
assertArgument(options.
|
|
56
|
-
|
|
49
|
+
assertArgument(options.feed, "options.feed", "Feed is required");
|
|
50
|
+
assertArgument(options.runtime, "options.runtime", "Feed runtime is required");
|
|
51
|
+
this._feed = options.feed;
|
|
52
|
+
this._runtime = options.runtime;
|
|
57
53
|
this._registry = options.registry ?? Registry.make();
|
|
58
54
|
}
|
|
59
55
|
/**
|
|
@@ -93,33 +89,67 @@ var AiContextBinder = class extends Resource {
|
|
|
93
89
|
return this._registry.subscribe(this._objects, () => cb(this._registry.get(this._objects)));
|
|
94
90
|
}
|
|
95
91
|
async _open() {
|
|
96
|
-
|
|
97
|
-
const initialResults = await
|
|
92
|
+
this.#bindingsQuery = await Runtime.runPromise(this._runtime)(Feed.query(this._feed, Query.type(ContextBinding)));
|
|
93
|
+
const initialResults = await this.#bindingsQuery.run();
|
|
98
94
|
await this._updateBindings(initialResults);
|
|
99
|
-
this._ctx.onDispose(
|
|
100
|
-
await this._updateBindings(
|
|
95
|
+
this._ctx.onDispose(this.#bindingsQuery.subscribe(async () => {
|
|
96
|
+
await this._updateBindings(this.#bindingsQuery.results);
|
|
101
97
|
}));
|
|
102
98
|
}
|
|
99
|
+
/**
|
|
100
|
+
* Re-reads bindings from the feed to pick up changes made by other processes.
|
|
101
|
+
*/
|
|
102
|
+
async sync() {
|
|
103
|
+
if (this.#bindingsQuery) {
|
|
104
|
+
const results = await this.#bindingsQuery.run();
|
|
105
|
+
log("sync", {
|
|
106
|
+
bindingItems: results.length
|
|
107
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 107, S: this });
|
|
108
|
+
await this._updateBindings(results);
|
|
109
|
+
log("sync complete", {
|
|
110
|
+
blueprints: this._registry.get(this._blueprints).length,
|
|
111
|
+
blueprintKeys: this._registry.get(this._blueprints).map((bp) => bp.key)
|
|
112
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 111, S: this });
|
|
113
|
+
}
|
|
114
|
+
}
|
|
103
115
|
async _updateBindings(items) {
|
|
104
116
|
if (items.length === 0) {
|
|
105
117
|
return;
|
|
106
118
|
}
|
|
107
119
|
const bindings = this._reduce(items);
|
|
120
|
+
log("_updateBindings", {
|
|
121
|
+
items: items.length,
|
|
122
|
+
blueprintRefs: [
|
|
123
|
+
...bindings.blueprints
|
|
124
|
+
].map((ref) => ({
|
|
125
|
+
dxn: ref.dxn.toString(),
|
|
126
|
+
available: ref.isAvailable
|
|
127
|
+
}))
|
|
128
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 123, S: this });
|
|
108
129
|
const currentBlueprints = this._registry.get(this._blueprints);
|
|
109
130
|
const currentObjects = this._registry.get(this._objects);
|
|
110
|
-
const
|
|
111
|
-
const
|
|
112
|
-
|
|
113
|
-
|
|
131
|
+
const resolvedBlueprints = await this._resolve(bindings.blueprints, currentBlueprints);
|
|
132
|
+
const resolvedObjects = await this._resolve(bindings.objects, currentObjects);
|
|
133
|
+
log("_updateBindings resolved", {
|
|
134
|
+
resolvedBlueprints: resolvedBlueprints.length,
|
|
135
|
+
resolvedBlueprintKeys: resolvedBlueprints.map((bp) => bp.key)
|
|
136
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 137, S: this });
|
|
137
|
+
const reducedBlueprintDxns = new ComplexSet(DXN.hash, [
|
|
138
|
+
...bindings.blueprints
|
|
139
|
+
].map((ref) => ref.dxn));
|
|
140
|
+
const reducedObjectDxns = new ComplexSet(DXN.hash, [
|
|
141
|
+
...bindings.objects
|
|
142
|
+
].map((ref) => ref.dxn));
|
|
143
|
+
const filteredBlueprints = currentBlueprints.filter((obj) => reducedBlueprintDxns.has(Obj.getDXN(obj)));
|
|
144
|
+
const filteredObjects = currentObjects.filter((obj) => reducedObjectDxns.has(Obj.getDXN(obj)));
|
|
145
|
+
const mergedBlueprints = this._mergeInto(filteredBlueprints, resolvedBlueprints);
|
|
146
|
+
const mergedObjects = this._mergeInto(filteredObjects, resolvedObjects);
|
|
147
|
+
this._registry.set(this._blueprints, mergedBlueprints);
|
|
148
|
+
this._registry.set(this._objects, mergedObjects);
|
|
114
149
|
log("updated bindings", {
|
|
115
|
-
blueprints:
|
|
116
|
-
objects:
|
|
117
|
-
}, {
|
|
118
|
-
F: __dxlog_file,
|
|
119
|
-
L: 159,
|
|
120
|
-
S: this,
|
|
121
|
-
C: (f, a) => f(...a)
|
|
122
|
-
});
|
|
150
|
+
blueprints: mergedBlueprints.length,
|
|
151
|
+
objects: mergedObjects.length
|
|
152
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 155, S: this });
|
|
123
153
|
}
|
|
124
154
|
async _close() {
|
|
125
155
|
this._registry.set(this._blueprints, []);
|
|
@@ -138,13 +168,8 @@ var AiContextBinder = class extends Resource {
|
|
|
138
168
|
log("bind", {
|
|
139
169
|
blueprints: addedBlueprints.length,
|
|
140
170
|
objects: addedObjects.length
|
|
141
|
-
}, {
|
|
142
|
-
|
|
143
|
-
L: 185,
|
|
144
|
-
S: this,
|
|
145
|
-
C: (f, a) => f(...a)
|
|
146
|
-
});
|
|
147
|
-
await this._queue.append([
|
|
171
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 176, S: this });
|
|
172
|
+
await Runtime.runPromise(this._runtime)(Feed.append(this._feed, [
|
|
148
173
|
Obj.make(ContextBinding, {
|
|
149
174
|
blueprints: {
|
|
150
175
|
added: addedBlueprints,
|
|
@@ -155,22 +180,27 @@ var AiContextBinder = class extends Resource {
|
|
|
155
180
|
removed: []
|
|
156
181
|
}
|
|
157
182
|
})
|
|
158
|
-
]);
|
|
183
|
+
]));
|
|
159
184
|
}
|
|
160
185
|
async unbind({ blueprints, objects }) {
|
|
161
186
|
if (!blueprints?.length && !objects?.length) {
|
|
162
187
|
return;
|
|
163
188
|
}
|
|
189
|
+
const removedBlueprintDxns = (blueprints ?? []).map((ref) => ref.dxn);
|
|
190
|
+
const removedObjectDxns = (objects ?? []).map((ref) => ref.dxn);
|
|
191
|
+
if (removedBlueprintDxns.length > 0) {
|
|
192
|
+
const current = this._registry.get(this._blueprints);
|
|
193
|
+
this._registry.set(this._blueprints, current.filter((obj) => !removedBlueprintDxns.some((dxn) => DXN.equalsEchoId(Obj.getDXN(obj), dxn))));
|
|
194
|
+
}
|
|
195
|
+
if (removedObjectDxns.length > 0) {
|
|
196
|
+
const current = this._registry.get(this._objects);
|
|
197
|
+
this._registry.set(this._objects, current.filter((obj) => !removedObjectDxns.some((dxn) => DXN.equalsEchoId(Obj.getDXN(obj), dxn))));
|
|
198
|
+
}
|
|
164
199
|
log("unbind", {
|
|
165
200
|
blueprints: blueprints?.length,
|
|
166
201
|
objects: objects?.length
|
|
167
|
-
}, {
|
|
168
|
-
|
|
169
|
-
L: 205,
|
|
170
|
-
S: this,
|
|
171
|
-
C: (f, a) => f(...a)
|
|
172
|
-
});
|
|
173
|
-
await this._queue.append([
|
|
202
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 208, S: this });
|
|
203
|
+
await Runtime.runPromise(this._runtime)(Feed.append(this._feed, [
|
|
174
204
|
Obj.make(ContextBinding, {
|
|
175
205
|
blueprints: {
|
|
176
206
|
added: [],
|
|
@@ -181,7 +211,7 @@ var AiContextBinder = class extends Resource {
|
|
|
181
211
|
removed: objects ?? []
|
|
182
212
|
}
|
|
183
213
|
})
|
|
184
|
-
]);
|
|
214
|
+
]));
|
|
185
215
|
}
|
|
186
216
|
/**
|
|
187
217
|
* Process bindings to filter duplicates and determine next state.
|
|
@@ -235,6 +265,23 @@ var AiContextBinder = class extends Resource {
|
|
|
235
265
|
}));
|
|
236
266
|
}
|
|
237
267
|
/**
|
|
268
|
+
* Merge resolved items into the current set, adding only items not already present (by DXN).
|
|
269
|
+
*/
|
|
270
|
+
_mergeInto(current, resolved) {
|
|
271
|
+
const seen = new Set(current.map((obj) => Obj.getDXN(obj).toString()));
|
|
272
|
+
const merged = [
|
|
273
|
+
...current
|
|
274
|
+
];
|
|
275
|
+
for (const obj of resolved) {
|
|
276
|
+
const dxn = Obj.getDXN(obj).toString();
|
|
277
|
+
if (!seen.has(dxn)) {
|
|
278
|
+
seen.add(dxn);
|
|
279
|
+
merged.push(obj);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return merged;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
238
285
|
* Resolve references to objects, loading them first if needed and falling back to existing objects.
|
|
239
286
|
*/
|
|
240
287
|
async _resolve(refs, current) {
|
|
@@ -248,7 +295,7 @@ var AiContextBinder = class extends Resource {
|
|
|
248
295
|
target = ref.target;
|
|
249
296
|
}
|
|
250
297
|
return target ?? current.find((obj) => Obj.getDXN(obj).toString() === ref.dxn.toString());
|
|
251
|
-
}).filter(
|
|
298
|
+
}).filter(isNonNullable);
|
|
252
299
|
}
|
|
253
300
|
};
|
|
254
301
|
var AiContextService = class _AiContextService extends Context.Tag("@dxos/assistant/AiContextService")() {
|
|
@@ -265,40 +312,219 @@ var AiContextService = class _AiContextService extends Context.Tag("@dxos/assist
|
|
|
265
312
|
return binder.getObjects().filter(Obj.instanceOf(type));
|
|
266
313
|
});
|
|
267
314
|
};
|
|
268
|
-
/**
|
|
269
|
-
* Glorified type cast until we figure out function service typing.
|
|
270
|
-
* For context, the AiContextService is provided to functions called from tool calls, but it's not implemented in types yet.
|
|
271
|
-
* @deprecated
|
|
272
|
-
*/
|
|
273
|
-
static fixFunctionHandlerType = (eff) => eff;
|
|
274
315
|
};
|
|
275
316
|
|
|
276
|
-
// src/conversation/
|
|
277
|
-
import * as
|
|
278
|
-
import * as
|
|
279
|
-
import * as
|
|
317
|
+
// src/conversation/session.ts
|
|
318
|
+
import * as Array4 from "effect/Array";
|
|
319
|
+
import * as Context4 from "effect/Context";
|
|
320
|
+
import * as Effect7 from "effect/Effect";
|
|
280
321
|
import * as Either from "effect/Either";
|
|
281
|
-
import { pipe as
|
|
282
|
-
import * as
|
|
322
|
+
import { pipe as pipe4 } from "effect/Function";
|
|
323
|
+
import * as Layer3 from "effect/Layer";
|
|
324
|
+
import * as Record2 from "effect/Record";
|
|
325
|
+
import * as Runtime2 from "effect/Runtime";
|
|
326
|
+
import { Operation as Operation2, Trace as Trace3 } from "@dxos/compute";
|
|
283
327
|
import { Resource as Resource2 } from "@dxos/context";
|
|
284
|
-
import { Obj as
|
|
328
|
+
import { Database as Database2, Feed as Feed2, Filter, Obj as Obj6 } from "@dxos/echo";
|
|
285
329
|
import { acquireReleaseResource } from "@dxos/effect";
|
|
286
|
-
import {
|
|
287
|
-
import { invariant } from "@dxos/invariant";
|
|
330
|
+
import { invariant as invariant2 } from "@dxos/invariant";
|
|
288
331
|
import { log as log4 } from "@dxos/log";
|
|
289
332
|
import { McpToolkit } from "@dxos/mcp-client";
|
|
290
333
|
import { Message as Message3 } from "@dxos/types";
|
|
291
334
|
|
|
292
|
-
// src/
|
|
335
|
+
// src/functions/services.ts
|
|
336
|
+
import * as Tool from "@effect/ai/Tool";
|
|
293
337
|
import * as Context2 from "effect/Context";
|
|
294
|
-
|
|
338
|
+
import * as Effect2 from "effect/Effect";
|
|
339
|
+
import * as Layer from "effect/Layer";
|
|
340
|
+
import * as Option from "effect/Option";
|
|
341
|
+
import * as Record from "effect/Record";
|
|
342
|
+
import * as Schema3 from "effect/Schema";
|
|
343
|
+
import * as SchemaAST from "effect/SchemaAST";
|
|
344
|
+
import { AiToolNotFoundError, ToolExecutionService, ToolResolverService } from "@dxos/ai";
|
|
345
|
+
import { OpaqueToolkit } from "@dxos/ai";
|
|
346
|
+
import { Operation, OperationRegistry } from "@dxos/compute";
|
|
347
|
+
import { todo } from "@dxos/debug";
|
|
348
|
+
import { Ref as Ref3 } from "@dxos/echo";
|
|
349
|
+
import { invariant } from "@dxos/invariant";
|
|
350
|
+
|
|
351
|
+
// src/types/artifact.ts
|
|
352
|
+
import * as Schema2 from "effect/Schema";
|
|
353
|
+
import { Obj as Obj2, Ref as Ref2 } from "@dxos/echo";
|
|
354
|
+
import { Database } from "@dxos/echo";
|
|
355
|
+
import { EncodedReference } from "@dxos/echo-protocol";
|
|
356
|
+
import { DXN as DXN2, LOCAL_SPACE_TAG } from "@dxos/keys";
|
|
357
|
+
import { trim } from "@dxos/util";
|
|
358
|
+
var createArtifactElement = (id) => `<artifact id=${id} />`;
|
|
359
|
+
var ArtifactId = class extends Schema2.String.annotations({
|
|
360
|
+
// TODO(dmaretskyi): This section gets overriden.
|
|
361
|
+
description: trim`
|
|
362
|
+
The ID of the referenced object. Formats accepted:
|
|
363
|
+
- DXN (dxn:echo:@:01KG7R1ZXWFMWQ4DA1Q6TN1DG4). DXNs can be prepended with an @ symbol for compatibility with in-text references.
|
|
364
|
+
- space ID, object ID tuple (spaceID:objectID)
|
|
365
|
+
- Only object ID that is assumed to be in the current space (01KG7R1ZXWFMWQ4DA1Q6TN1DG4)
|
|
366
|
+
`,
|
|
367
|
+
examples: [
|
|
368
|
+
"dxn:echo:@:01KG7R1ZXWFMWQ4DA1Q6TN1DG4",
|
|
369
|
+
"@dxn:echo:@:01KG7R1ZXWFMWQ4DA1Q6TN1DG4",
|
|
370
|
+
"BM3FSHFOMJCHCG5QW7JTVKGYABD2GAA7G:01KG7R1ZXWFMWQ4DA1Q6TN1DG4",
|
|
371
|
+
"01KG7R1ZXWFMWQ4DA1Q6TN1DG4"
|
|
372
|
+
]
|
|
373
|
+
}) {
|
|
374
|
+
static toDXN(reference, owningSpaceId) {
|
|
375
|
+
if (reference.startsWith("@dxn:")) {
|
|
376
|
+
return DXN2.parse(reference.slice(1));
|
|
377
|
+
} else if (reference.startsWith("dxn:")) {
|
|
378
|
+
return DXN2.parse(reference);
|
|
379
|
+
} else if (/^[A-Z0-9]+:[A-Z0-9]+$/.test(reference)) {
|
|
380
|
+
const [spaceId, objectId] = reference.split(":");
|
|
381
|
+
return new DXN2(DXN2.kind.ECHO, [
|
|
382
|
+
spaceId === owningSpaceId ? LOCAL_SPACE_TAG : spaceId,
|
|
383
|
+
objectId
|
|
384
|
+
]);
|
|
385
|
+
} else if (/^[A-Z0-9]+$/.test(reference)) {
|
|
386
|
+
return DXN2.fromLocalObjectId(reference);
|
|
387
|
+
} else {
|
|
388
|
+
throw new Error(`Unable to parse object reference: ${reference}`);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Resolves an artifact ID to an object.
|
|
393
|
+
*/
|
|
394
|
+
static resolve(schema, ref) {
|
|
395
|
+
const dxn = ArtifactId.toDXN(ref);
|
|
396
|
+
return Database.resolve(dxn, schema);
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
var RefFromLLM = Schema2.transform(ArtifactId, Ref2.Ref(Obj2.Unknown), {
|
|
400
|
+
decode: (fromA, fromI) => EncodedReference.fromDXN(ArtifactId.toDXN(fromA)),
|
|
401
|
+
encode: (toI, toA) => EncodedReference.toDXN(toI).toString(),
|
|
402
|
+
strict: false
|
|
403
|
+
}).annotations({
|
|
404
|
+
description: ArtifactId.ast.annotations.description
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
// src/functions/services.ts
|
|
408
|
+
var __dxlog_file2 = "/__w/dxos/dxos/packages/core/compute/assistant/src/functions/services.ts";
|
|
409
|
+
var makeToolResolverFromOperations = ({ toolkit: extraToolkit = OpaqueToolkit.empty } = {}) => {
|
|
410
|
+
return Layer.effect(ToolResolverService, Effect2.gen(function* () {
|
|
411
|
+
const toolkitProvider = yield* OpaqueToolkit.OpaqueToolkitProvider;
|
|
412
|
+
const operationRegistry = yield* OperationRegistry.Service;
|
|
413
|
+
return {
|
|
414
|
+
resolve: (id) => Effect2.gen(function* () {
|
|
415
|
+
const toolkit = OpaqueToolkit.merge(extraToolkit, toolkitProvider.getToolkit());
|
|
416
|
+
const tool = toolkit.toolkit.tools[id];
|
|
417
|
+
if (tool) {
|
|
418
|
+
return tool;
|
|
419
|
+
}
|
|
420
|
+
return yield* operationRegistry.resolve(id).pipe(Effect2.flatMap(Option.match({
|
|
421
|
+
onSome: (_) => Effect2.succeed(projectFunctionToTool(_)),
|
|
422
|
+
onNone: () => Effect2.fail(new AiToolNotFoundError(id))
|
|
423
|
+
})));
|
|
424
|
+
})
|
|
425
|
+
};
|
|
426
|
+
}));
|
|
427
|
+
};
|
|
428
|
+
var makeToolExecutionService = (opts) => Layer.effect(ToolExecutionService, Effect2.gen(function* () {
|
|
429
|
+
const toolkitProvider = yield* OpaqueToolkit.OpaqueToolkitProvider;
|
|
430
|
+
const toolkit = toolkitProvider.getToolkit();
|
|
431
|
+
const toolkitHandler = yield* toolkit.toolkit.pipe(Effect2.provide(toolkit.layer));
|
|
432
|
+
invariant(isHandlerLike(toolkitHandler), void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 42, S: this, A: ["isHandlerLike(toolkitHandler)", ""] });
|
|
433
|
+
return {
|
|
434
|
+
handlersFor: (toolkit2) => {
|
|
435
|
+
const makeHandler = (tool) => {
|
|
436
|
+
return Effect2.fn(`toolFunctionHandler ${tool.name}`)(function* (input) {
|
|
437
|
+
if (toolkitHandler.tools[tool.name]) {
|
|
438
|
+
if (Tool.isProviderDefined(tool)) {
|
|
439
|
+
throw new Error("Attempted to call a provider-defined tool");
|
|
440
|
+
}
|
|
441
|
+
const { result } = yield* toolkitHandler.handle(tool.name, input);
|
|
442
|
+
return result;
|
|
443
|
+
}
|
|
444
|
+
return yield* opts.invoke(tool, input).pipe(Effect2.catchAllDefect((defect) => Effect2.fail(defect)));
|
|
445
|
+
});
|
|
446
|
+
};
|
|
447
|
+
return toolkit2.of(Record.map(toolkit2.tools, (tool, _name) => Tool.isUserDefined(tool) ? makeHandler(tool) : null));
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
}));
|
|
451
|
+
var makeToolExecutionServiceFromOperationInvoker = () => {
|
|
452
|
+
return Layer.unwrapEffect(Effect2.gen(function* () {
|
|
453
|
+
const operationInvoker = yield* Operation.Service;
|
|
454
|
+
return makeToolExecutionService({
|
|
455
|
+
invoke: (tool, input) => Effect2.gen(function* () {
|
|
456
|
+
const operationDef = getOperationFromTool(tool).pipe(Option.getOrThrow);
|
|
457
|
+
return yield* operationInvoker.invoke(operationDef, input).pipe(Effect2.orDie);
|
|
458
|
+
})
|
|
459
|
+
});
|
|
460
|
+
}));
|
|
461
|
+
};
|
|
462
|
+
var ToolExecutionServices = Layer.mergeAll(makeToolResolverFromOperations(), makeToolExecutionServiceFromOperationInvoker());
|
|
463
|
+
var FunctionToolAnnotation = class extends Context2.Tag("@dxos/assistant/FunctionToolAnnotation")() {
|
|
464
|
+
};
|
|
465
|
+
var getOperationFromTool = (tool) => {
|
|
466
|
+
return Context2.getOption(FunctionToolAnnotation)(tool.annotations).pipe(Option.map(({ definition }) => definition));
|
|
467
|
+
};
|
|
468
|
+
var toolCache = /* @__PURE__ */ new WeakMap();
|
|
469
|
+
var projectFunctionToTool = (fn2) => {
|
|
470
|
+
if (toolCache.has(fn2)) {
|
|
471
|
+
return toolCache.get(fn2);
|
|
472
|
+
}
|
|
473
|
+
const tool = Tool.make(makeToolName(fn2.meta.name ?? fn2.meta.key), {
|
|
474
|
+
description: fn2.meta.description,
|
|
475
|
+
parameters: createStructFieldsFromSchema(fn2.input),
|
|
476
|
+
failure: Schema3.Any
|
|
477
|
+
}).annotate(FunctionToolAnnotation, {
|
|
478
|
+
definition: fn2
|
|
479
|
+
});
|
|
480
|
+
toolCache.set(fn2, tool);
|
|
481
|
+
return tool;
|
|
482
|
+
};
|
|
483
|
+
var makeToolName = (name) => {
|
|
484
|
+
const toolName = name.toLowerCase().replace(/[^a-zA-Z0-9]/g, "-");
|
|
485
|
+
invariant(toolName.match(/^[a-z_][a-z0-9-_]*$/), void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 100, S: void 0, A: ["toolName.match(/^[a-z_][a-z0-9-_]*$/)", ""] });
|
|
486
|
+
return toolName;
|
|
487
|
+
};
|
|
488
|
+
var createStructFieldsFromSchema = (schema) => {
|
|
489
|
+
switch (schema.ast._tag) {
|
|
490
|
+
case "TypeLiteral":
|
|
491
|
+
return Object.fromEntries(schema.ast.propertySignatures.map((prop) => [
|
|
492
|
+
prop.name,
|
|
493
|
+
Schema3.make(mapSchemaTypeForLLM(prop.type))
|
|
494
|
+
]));
|
|
495
|
+
case "VoidKeyword":
|
|
496
|
+
return {};
|
|
497
|
+
default:
|
|
498
|
+
return todo(`Unsupported schema AST: ${schema.ast._tag}`);
|
|
499
|
+
}
|
|
500
|
+
};
|
|
501
|
+
var mapSchemaTypeForLLM = (ast) => {
|
|
502
|
+
if (Ref3.isRefType(ast)) {
|
|
503
|
+
const description = ast.annotations.description ? ast.annotations.description + "\n" + RefFromLLM.ast.annotations.description : RefFromLLM.ast.annotations.description;
|
|
504
|
+
return RefFromLLM.annotations({
|
|
505
|
+
description
|
|
506
|
+
}).ast;
|
|
507
|
+
} else if (SchemaAST.isTupleType(ast)) {
|
|
508
|
+
return new SchemaAST.TupleType(ast.elements.map((t) => new SchemaAST.OptionalType(mapSchemaTypeForLLM(t.type), t.isOptional, t.annotations)), ast.rest.map((t) => new SchemaAST.Type(mapSchemaTypeForLLM(t.type), t.annotations)), ast.isReadonly, ast.annotations);
|
|
509
|
+
} else if (SchemaAST.isTypeLiteral(ast)) {
|
|
510
|
+
return new SchemaAST.TypeLiteral(ast.propertySignatures.map((p) => new SchemaAST.PropertySignature(p.name, mapSchemaTypeForLLM(p.type), p.isOptional, p.isReadonly, p.annotations)), ast.indexSignatures, ast.annotations);
|
|
511
|
+
}
|
|
512
|
+
return ast;
|
|
513
|
+
};
|
|
514
|
+
var isHandlerLike = (value) => {
|
|
515
|
+
return typeof value.tools === "object" && typeof value.handle === "function";
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
// src/session/artifact-diff.ts
|
|
519
|
+
import * as Context3 from "effect/Context";
|
|
520
|
+
var ArtifactDiffResolver = class extends Context3.Tag("@dxos/assistant/ArtifactDiffResolver")() {
|
|
295
521
|
};
|
|
296
522
|
|
|
297
523
|
// src/session/observer.ts
|
|
298
|
-
import * as
|
|
524
|
+
import * as Effect3 from "effect/Effect";
|
|
299
525
|
import * as Function2 from "effect/Function";
|
|
300
526
|
var GenerationObserver = Object.freeze({
|
|
301
|
-
make: ({ onBegin = Function2.constant(
|
|
527
|
+
make: ({ onBegin = Function2.constant(Effect3.void), onPart = Function2.constant(Effect3.void), onBlock = Function2.constant(Effect3.void), onEnd = Function2.constant(Effect3.void), onMessage = Function2.constant(Effect3.void) } = {}) => ({
|
|
302
528
|
onBegin,
|
|
303
529
|
onPart,
|
|
304
530
|
onBlock,
|
|
@@ -310,13 +536,13 @@ var GenerationObserver = Object.freeze({
|
|
|
310
536
|
* Debug printer to be used in unit-tests and browser devtools.
|
|
311
537
|
*/
|
|
312
538
|
fromPrinter: (printer) => GenerationObserver.make({
|
|
313
|
-
onBlock: (block) =>
|
|
539
|
+
onBlock: (block) => Effect3.sync(() => {
|
|
314
540
|
if (block.pending) {
|
|
315
541
|
return;
|
|
316
542
|
}
|
|
317
543
|
printer.printContentBlock(block);
|
|
318
544
|
}),
|
|
319
|
-
onMessage: (message) =>
|
|
545
|
+
onMessage: (message) => Effect3.sync(() => {
|
|
320
546
|
if (message.sender.role === "assistant") {
|
|
321
547
|
return;
|
|
322
548
|
}
|
|
@@ -325,27 +551,68 @@ var GenerationObserver = Object.freeze({
|
|
|
325
551
|
})
|
|
326
552
|
});
|
|
327
553
|
|
|
328
|
-
// src/session/
|
|
554
|
+
// src/session/request.ts
|
|
329
555
|
import * as LanguageModel from "@effect/ai/LanguageModel";
|
|
556
|
+
import * as Array3 from "effect/Array";
|
|
330
557
|
import * as Chunk from "effect/Chunk";
|
|
331
|
-
import * as
|
|
558
|
+
import * as Effect5 from "effect/Effect";
|
|
559
|
+
import { pipe as pipe3 } from "effect/Function";
|
|
560
|
+
import * as Option3 from "effect/Option";
|
|
332
561
|
import * as Stream from "effect/Stream";
|
|
333
562
|
import { AiParser, AiPreprocessor, AiSummarizer, callTool, withoutToolCallParising } from "@dxos/ai";
|
|
334
|
-
import {
|
|
335
|
-
import {
|
|
563
|
+
import { Trace as Trace2 } from "@dxos/compute";
|
|
564
|
+
import { Obj as Obj5 } from "@dxos/echo";
|
|
336
565
|
import { log as log3 } from "@dxos/log";
|
|
337
|
-
import { Message as Message2 } from "@dxos/types";
|
|
566
|
+
import { ContentBlock as ContentBlock2, Message as Message2 } from "@dxos/types";
|
|
567
|
+
|
|
568
|
+
// src/tracing.ts
|
|
569
|
+
import * as Schema4 from "effect/Schema";
|
|
570
|
+
import { Trace } from "@dxos/compute";
|
|
571
|
+
import { Obj as Obj3 } from "@dxos/echo";
|
|
572
|
+
import { ContentBlock, Actor } from "@dxos/types";
|
|
573
|
+
var PartialBlock = Trace.EventType("assistant.partialBlock", {
|
|
574
|
+
schema: Schema4.Struct({
|
|
575
|
+
messageId: Obj3.ID,
|
|
576
|
+
role: Actor.Role,
|
|
577
|
+
block: ContentBlock.Any
|
|
578
|
+
}),
|
|
579
|
+
isEphemeral: true
|
|
580
|
+
});
|
|
581
|
+
var CompleteBlock = Trace.EventType("assistant.completeBlock", {
|
|
582
|
+
schema: Schema4.Struct({
|
|
583
|
+
messageId: Obj3.ID,
|
|
584
|
+
role: Actor.Role,
|
|
585
|
+
block: ContentBlock.Any
|
|
586
|
+
}),
|
|
587
|
+
isEphemeral: false
|
|
588
|
+
});
|
|
589
|
+
var AgentRequestBegin = Trace.EventType("assistant.agentRequestBegin", {
|
|
590
|
+
schema: Schema4.Struct({}),
|
|
591
|
+
isEphemeral: false
|
|
592
|
+
});
|
|
593
|
+
var AgentRequestEnd = Trace.EventType("assistant.agentRequestEnd", {
|
|
594
|
+
schema: Schema4.Struct({}),
|
|
595
|
+
isEphemeral: false
|
|
596
|
+
});
|
|
597
|
+
var McpServerError = Trace.EventType("assistant.mcpServerError", {
|
|
598
|
+
schema: Schema4.Struct({
|
|
599
|
+
url: Schema4.String,
|
|
600
|
+
kind: Schema4.Literal("sse", "http"),
|
|
601
|
+
message: Schema4.String
|
|
602
|
+
}),
|
|
603
|
+
isEphemeral: true
|
|
604
|
+
});
|
|
338
605
|
|
|
339
606
|
// src/session/format.ts
|
|
340
|
-
import * as
|
|
607
|
+
import * as Effect4 from "effect/Effect";
|
|
341
608
|
import * as Function3 from "effect/Function";
|
|
342
|
-
import * as
|
|
343
|
-
import { Template } from "@dxos/
|
|
344
|
-
import { Obj as
|
|
609
|
+
import * as Option2 from "effect/Option";
|
|
610
|
+
import { Template } from "@dxos/compute";
|
|
611
|
+
import { Obj as Obj4 } from "@dxos/echo";
|
|
345
612
|
import { ObjectVersion } from "@dxos/echo-db";
|
|
346
613
|
import { log as log2 } from "@dxos/log";
|
|
347
614
|
import { Message } from "@dxos/types";
|
|
348
|
-
import { trim } from "@dxos/util";
|
|
615
|
+
import { trim as trim2 } from "@dxos/util";
|
|
349
616
|
|
|
350
617
|
// src/errors.ts
|
|
351
618
|
import { BaseError } from "@dxos/errors";
|
|
@@ -355,39 +622,39 @@ var AiAssistantError = class extends BaseError.extend("AiAssistantError", "AI as
|
|
|
355
622
|
};
|
|
356
623
|
|
|
357
624
|
// src/session/format.ts
|
|
358
|
-
var
|
|
359
|
-
var formatSystemPrompt = ({ system, blueprints = [], objects = [] }) =>
|
|
360
|
-
const blueprintDefs = yield* Function3.pipe(blueprints,
|
|
361
|
-
return
|
|
625
|
+
var __dxlog_file3 = "/__w/dxos/dxos/packages/core/compute/assistant/src/session/format.ts";
|
|
626
|
+
var formatSystemPrompt = ({ system, blueprints = [], objects = [] }) => Effect4.gen(function* () {
|
|
627
|
+
const blueprintDefs = yield* Function3.pipe(blueprints, Effect4.forEach((blueprint) => Effect4.succeed(blueprint.instructions)), Effect4.flatMap(Effect4.forEach((template) => Effect4.gen(function* () {
|
|
628
|
+
return trim2`
|
|
362
629
|
<blueprint>
|
|
363
630
|
${yield* Template.processTemplate(template)}
|
|
364
631
|
</blueprint>
|
|
365
632
|
`;
|
|
366
|
-
}))),
|
|
633
|
+
}))), Effect4.map((blueprints2) => blueprints2.length > 0 ? [
|
|
367
634
|
"## Blueprints Definitions",
|
|
368
635
|
...blueprints2
|
|
369
636
|
].join("\n\n") : void 0));
|
|
370
|
-
const objectDefs = yield* Function3.pipe(objects,
|
|
637
|
+
const objectDefs = yield* Function3.pipe(objects, Effect4.forEach((object) => Effect4.succeed(trim2`
|
|
371
638
|
<object>
|
|
372
|
-
<dxn>${
|
|
373
|
-
<typename>${
|
|
639
|
+
<dxn>${Obj4.getDXN(object)}</dxn>
|
|
640
|
+
<typename>${Obj4.getTypename(object)}</typename>
|
|
374
641
|
</object>
|
|
375
|
-
`)),
|
|
642
|
+
`)), Effect4.map((objects2) => objects2.length > 0 ? [
|
|
376
643
|
"## Context Objects",
|
|
377
644
|
...objects2
|
|
378
645
|
].join("\n\n") : void 0));
|
|
379
|
-
return yield* Function3.pipe(
|
|
646
|
+
return yield* Function3.pipe(Effect4.succeed([
|
|
380
647
|
system,
|
|
381
648
|
blueprintDefs,
|
|
382
649
|
objectDefs
|
|
383
|
-
].filter((def) => def !== void 0)),
|
|
384
|
-
});
|
|
385
|
-
var formatUserPrompt = ({ prompt, history = [] }) =>
|
|
650
|
+
].filter((def) => def !== void 0)), Effect4.map((parts) => parts.join("\n\n")));
|
|
651
|
+
}).pipe(Effect4.withSpan("formatSystemPrompt"));
|
|
652
|
+
var formatUserPrompt = ({ prompt, history = [] }) => Effect4.gen(function* () {
|
|
386
653
|
const blocks = [];
|
|
387
|
-
const artifactDiffResolver = yield*
|
|
388
|
-
if (
|
|
654
|
+
const artifactDiffResolver = yield* Effect4.serviceOption(ArtifactDiffResolver);
|
|
655
|
+
if (Option2.isSome(artifactDiffResolver)) {
|
|
389
656
|
const versions = gatherObjectVersions(history);
|
|
390
|
-
const artifactDiff = yield*
|
|
657
|
+
const artifactDiff = yield* Effect4.tryPromise({
|
|
391
658
|
try: () => artifactDiffResolver.value.resolve([
|
|
392
659
|
...versions.entries()
|
|
393
660
|
].map(([id, version]) => ({
|
|
@@ -401,12 +668,7 @@ var formatUserPrompt = ({ prompt, history = [] }) => Effect3.gen(function* () {
|
|
|
401
668
|
log2("version", {
|
|
402
669
|
artifactDiff,
|
|
403
670
|
versions
|
|
404
|
-
}, {
|
|
405
|
-
F: __dxlog_file2,
|
|
406
|
-
L: 94,
|
|
407
|
-
S: this,
|
|
408
|
-
C: (f, a) => f(...a)
|
|
409
|
-
});
|
|
671
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 65, S: this });
|
|
410
672
|
for (const [id, { version }] of [
|
|
411
673
|
...artifactDiff.entries()
|
|
412
674
|
]) {
|
|
@@ -424,20 +686,23 @@ var formatUserPrompt = ({ prompt, history = [] }) => Effect3.gen(function* () {
|
|
|
424
686
|
blocks.push(createArtifactUpdateBlock(artifactDiff));
|
|
425
687
|
}
|
|
426
688
|
}
|
|
427
|
-
return
|
|
689
|
+
return Obj4.make(Message.Message, {
|
|
428
690
|
created: (/* @__PURE__ */ new Date()).toISOString(),
|
|
429
691
|
sender: {
|
|
430
692
|
role: "user"
|
|
431
693
|
},
|
|
432
|
-
blocks: [
|
|
694
|
+
blocks: typeof prompt === "string" ? [
|
|
433
695
|
...blocks,
|
|
434
696
|
{
|
|
435
697
|
_tag: "text",
|
|
436
698
|
text: prompt
|
|
437
699
|
}
|
|
700
|
+
] : [
|
|
701
|
+
...blocks,
|
|
702
|
+
...prompt
|
|
438
703
|
]
|
|
439
704
|
});
|
|
440
|
-
});
|
|
705
|
+
}).pipe(Effect4.withSpan("formatUserPrompt"));
|
|
441
706
|
var gatherObjectVersions = (messages) => {
|
|
442
707
|
const artifactIds = /* @__PURE__ */ new Map();
|
|
443
708
|
for (const message of messages) {
|
|
@@ -453,8 +718,8 @@ var createArtifactUpdateBlock = (artifactDiff) => {
|
|
|
453
718
|
return {
|
|
454
719
|
_tag: "text",
|
|
455
720
|
// TODO(dmaretskyi): Does this need to be a special content-block?
|
|
456
|
-
disposition: "
|
|
457
|
-
text:
|
|
721
|
+
disposition: "synthetic",
|
|
722
|
+
text: trim2`
|
|
458
723
|
The following artifacts have been updated since the last message:
|
|
459
724
|
${[
|
|
460
725
|
...artifactDiff.entries()
|
|
@@ -464,12 +729,14 @@ ${diff}` : ""}</changed-artifact>`).join("\n")}
|
|
|
464
729
|
};
|
|
465
730
|
};
|
|
466
731
|
|
|
467
|
-
// src/session/
|
|
468
|
-
var
|
|
469
|
-
var
|
|
732
|
+
// src/session/request.ts
|
|
733
|
+
var __dxlog_file4 = "/__w/dxos/dxos/packages/core/compute/assistant/src/session/request.ts";
|
|
734
|
+
var AiRequest = class {
|
|
470
735
|
_options;
|
|
471
736
|
/** Prevents concurrent execution of session. */
|
|
472
|
-
_semaphore =
|
|
737
|
+
_semaphore = Effect5.runSync(Effect5.makeSemaphore(1));
|
|
738
|
+
_observer;
|
|
739
|
+
_onOutput;
|
|
473
740
|
/** Message history from queue. */
|
|
474
741
|
// TODO(burdon): Evolve into supporting a git-like graph of messages.
|
|
475
742
|
_history = [];
|
|
@@ -480,6 +747,8 @@ var AiSession = class {
|
|
|
480
747
|
_toolCalls = 0;
|
|
481
748
|
constructor(_options = {}) {
|
|
482
749
|
this._options = _options;
|
|
750
|
+
this._observer = _options.observer ?? GenerationObserver.noop();
|
|
751
|
+
this._onOutput = _options.onOutput ?? (() => Effect5.void);
|
|
483
752
|
}
|
|
484
753
|
get duration() {
|
|
485
754
|
return this._ended - this._started;
|
|
@@ -487,130 +756,203 @@ var AiSession = class {
|
|
|
487
756
|
get toolCalls() {
|
|
488
757
|
return this._toolCalls;
|
|
489
758
|
}
|
|
490
|
-
|
|
759
|
+
get pending() {
|
|
760
|
+
return this._pending;
|
|
761
|
+
}
|
|
762
|
+
_submitMessage = (message) => Effect5.gen(this, function* () {
|
|
763
|
+
this._pending.push(message);
|
|
764
|
+
yield* this._observer.onMessage(message);
|
|
765
|
+
for (const block of message.blocks) {
|
|
766
|
+
yield* Trace2.write(CompleteBlock, {
|
|
767
|
+
messageId: message.id,
|
|
768
|
+
role: message.sender.role,
|
|
769
|
+
block
|
|
770
|
+
});
|
|
771
|
+
}
|
|
772
|
+
yield* this._onOutput(message);
|
|
773
|
+
return message;
|
|
774
|
+
});
|
|
775
|
+
getToolCalls = () => pipe3([
|
|
776
|
+
...this._history,
|
|
777
|
+
...this._pending
|
|
778
|
+
], Array3.reverse, Array3.takeWhile((_) => _.sender.role === "assistant"), Array3.flatMap((_) => _.blocks.filter(ContentBlock2.is("toolCall")).map((block) => ({
|
|
779
|
+
block,
|
|
780
|
+
message: _
|
|
781
|
+
}))), Array3.filter((_) => !_.block.providerExecuted), Array3.reverse);
|
|
782
|
+
/**
|
|
783
|
+
* Initialize a session: set up history, perform summarization if needed, and submit the user prompt.
|
|
784
|
+
* Must be called before `runTurn()`.
|
|
785
|
+
*/
|
|
786
|
+
begin = ({ prompt, system, history = [], blueprints = [], objects = [] }) => Effect5.gen(this, function* () {
|
|
491
787
|
this._started = Date.now();
|
|
492
788
|
this._history = [
|
|
493
789
|
...history
|
|
494
790
|
];
|
|
495
791
|
this._pending = [];
|
|
496
|
-
const
|
|
497
|
-
|
|
498
|
-
yield* observer.onMessage(message);
|
|
499
|
-
yield* TracingService.emitConverationMessage(message);
|
|
500
|
-
yield* onOutput(message);
|
|
501
|
-
return message;
|
|
502
|
-
});
|
|
503
|
-
const system = yield* formatSystemPrompt({
|
|
504
|
-
system: systemTemplate,
|
|
792
|
+
const systemPrompt = yield* formatSystemPrompt({
|
|
793
|
+
system,
|
|
505
794
|
blueprints,
|
|
506
795
|
objects
|
|
507
|
-
}).pipe(
|
|
796
|
+
}).pipe(Effect5.orDie);
|
|
508
797
|
if (this._options.summarizationThreshold !== void 0) {
|
|
509
798
|
const tokenCount = yield* AiPreprocessor.estimateTokens(yield* AiPreprocessor.preprocessPrompt([
|
|
510
799
|
...this._history
|
|
511
800
|
], {
|
|
512
|
-
system
|
|
801
|
+
system: systemPrompt
|
|
513
802
|
}));
|
|
514
803
|
if (tokenCount > this._options.summarizationThreshold) {
|
|
515
804
|
const summary = yield* AiSummarizer.summarize([
|
|
516
805
|
...this._history
|
|
517
806
|
]);
|
|
518
|
-
yield*
|
|
807
|
+
yield* this._submitMessage(summary);
|
|
519
808
|
}
|
|
520
809
|
}
|
|
521
|
-
|
|
810
|
+
yield* this._submitMessage(yield* formatUserPrompt({
|
|
522
811
|
prompt,
|
|
523
812
|
history
|
|
524
813
|
}));
|
|
814
|
+
}).pipe(Effect5.withSpan("AiRequest.begin"));
|
|
815
|
+
/**
|
|
816
|
+
* Execute a single turn: one LLM generation followed by tool execution.
|
|
817
|
+
* The toolkit and system prompt can be updated between turns to reflect context changes (e.g. dynamically enabled blueprints).
|
|
818
|
+
*/
|
|
819
|
+
runAgentTurn = ({ system, toolkit: opaqueToolkit }) => Effect5.gen(this, function* () {
|
|
820
|
+
log3.info("request", {
|
|
821
|
+
system: {
|
|
822
|
+
snippet: createSnippet(system),
|
|
823
|
+
length: system.length
|
|
824
|
+
},
|
|
825
|
+
pending: this._pending.length,
|
|
826
|
+
history: this._history.length
|
|
827
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 109, S: this });
|
|
828
|
+
const prompt = yield* AiPreprocessor.preprocessPrompt([
|
|
829
|
+
...this._history,
|
|
830
|
+
...this._pending
|
|
831
|
+
], {
|
|
832
|
+
system,
|
|
833
|
+
cacheControl: "ephemeral"
|
|
834
|
+
});
|
|
835
|
+
const toolkit = opaqueToolkit ? yield* opaqueToolkit.handlers : void 0;
|
|
836
|
+
const observer = this._observer;
|
|
837
|
+
let currentMessageId = null;
|
|
838
|
+
const messages = yield* LanguageModel.streamText({
|
|
839
|
+
prompt,
|
|
840
|
+
toolkit,
|
|
841
|
+
disableToolCallResolution: true
|
|
842
|
+
}).pipe(withoutToolCallParising, AiParser.parseResponse({
|
|
843
|
+
emitPartial: true,
|
|
844
|
+
onBegin: () => observer.onBegin(),
|
|
845
|
+
onBlock: (block) => observer.onBlock(block),
|
|
846
|
+
onPart: (part) => observer.onPart(part),
|
|
847
|
+
onEnd: (summary) => observer.onEnd(summary)
|
|
848
|
+
}), Stream.mapEffect((block) => Effect5.gen(this, function* () {
|
|
849
|
+
if (block.pending) {
|
|
850
|
+
currentMessageId ??= Obj5.ID.random();
|
|
851
|
+
log3("emit ephemeral message", {
|
|
852
|
+
id: currentMessageId,
|
|
853
|
+
type: block._tag
|
|
854
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 140, S: this });
|
|
855
|
+
yield* Trace2.write(PartialBlock, {
|
|
856
|
+
messageId: currentMessageId,
|
|
857
|
+
role: "assistant",
|
|
858
|
+
block
|
|
859
|
+
});
|
|
860
|
+
return Option3.none();
|
|
861
|
+
} else {
|
|
862
|
+
currentMessageId ??= Obj5.ID.random();
|
|
863
|
+
const id = currentMessageId;
|
|
864
|
+
currentMessageId = null;
|
|
865
|
+
log3("emit complete message", {
|
|
866
|
+
id,
|
|
867
|
+
type: block._tag
|
|
868
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 154, S: this });
|
|
869
|
+
const message = Obj5.make(Message2.Message, {
|
|
870
|
+
id,
|
|
871
|
+
created: (/* @__PURE__ */ new Date()).toISOString(),
|
|
872
|
+
sender: {
|
|
873
|
+
role: "assistant"
|
|
874
|
+
},
|
|
875
|
+
blocks: [
|
|
876
|
+
block
|
|
877
|
+
]
|
|
878
|
+
});
|
|
879
|
+
return Option3.some(yield* this._submitMessage(message));
|
|
880
|
+
}
|
|
881
|
+
}), {
|
|
882
|
+
concurrency: 1,
|
|
883
|
+
unordered: false
|
|
884
|
+
}), Stream.filterMap((_) => _), Stream.runCollect, Effect5.map(Chunk.toArray));
|
|
885
|
+
log3.info("messages", {
|
|
886
|
+
messages
|
|
887
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 174, S: this });
|
|
888
|
+
const toolCalls = this.getToolCalls();
|
|
889
|
+
if (toolCalls.length === 0) {
|
|
890
|
+
this._ended = Date.now();
|
|
891
|
+
return {
|
|
892
|
+
messages,
|
|
893
|
+
done: true
|
|
894
|
+
};
|
|
895
|
+
} else if (!toolkit) {
|
|
896
|
+
throw new Error("No toolkit provided");
|
|
897
|
+
}
|
|
898
|
+
return {
|
|
899
|
+
messages,
|
|
900
|
+
done: false
|
|
901
|
+
};
|
|
902
|
+
}).pipe(Effect5.withSpan("AiRequest.runAgentTurn"));
|
|
903
|
+
runTools = ({ toolkit: opaqueToolkit }) => Effect5.gen(this, function* () {
|
|
904
|
+
const toolkit = opaqueToolkit ? yield* opaqueToolkit.handlers : void 0;
|
|
905
|
+
const toolCalls = this.getToolCalls();
|
|
906
|
+
const toolResults = yield* Effect5.forEach(toolCalls, ({ block, message }) => {
|
|
907
|
+
if (!toolkit) {
|
|
908
|
+
throw new Error("No toolkit provided");
|
|
909
|
+
}
|
|
910
|
+
return callTool(toolkit, block);
|
|
911
|
+
});
|
|
912
|
+
yield* this._submitMessage(Obj5.make(Message2.Message, {
|
|
913
|
+
created: (/* @__PURE__ */ new Date()).toISOString(),
|
|
914
|
+
sender: {
|
|
915
|
+
role: "tool"
|
|
916
|
+
},
|
|
917
|
+
blocks: toolResults
|
|
918
|
+
}));
|
|
919
|
+
this._toolCalls += toolResults.length;
|
|
920
|
+
}).pipe(Effect5.withSpan("AiRequest.runTools"));
|
|
921
|
+
/**
|
|
922
|
+
* Run a full conversation turn loop. Equivalent to calling `begin()` then `runTurn()` in a loop.
|
|
923
|
+
*/
|
|
924
|
+
run = ({ prompt, system: systemTemplate, history = [], objects = [], blueprints = [], toolkit }) => Effect5.gen(this, function* () {
|
|
925
|
+
yield* this.begin({
|
|
926
|
+
prompt,
|
|
927
|
+
system: systemTemplate,
|
|
928
|
+
history,
|
|
929
|
+
objects,
|
|
930
|
+
blueprints
|
|
931
|
+
});
|
|
932
|
+
const system = yield* formatSystemPrompt({
|
|
933
|
+
system: systemTemplate,
|
|
934
|
+
blueprints,
|
|
935
|
+
objects
|
|
936
|
+
}).pipe(Effect5.orDie);
|
|
525
937
|
do {
|
|
526
|
-
|
|
527
|
-
prompt: promptMessage,
|
|
528
|
-
system: {
|
|
529
|
-
snippet: createSnippet(system),
|
|
530
|
-
length: system.length
|
|
531
|
-
},
|
|
532
|
-
pending: this._pending.length,
|
|
533
|
-
history: this._history.length,
|
|
534
|
-
objects: objects?.length ?? 0
|
|
535
|
-
}, {
|
|
536
|
-
F: __dxlog_file3,
|
|
537
|
-
L: 149,
|
|
538
|
-
S: this,
|
|
539
|
-
C: (f, a) => f(...a)
|
|
540
|
-
});
|
|
541
|
-
const prompt2 = yield* AiPreprocessor.preprocessPrompt([
|
|
542
|
-
...this._history,
|
|
543
|
-
...this._pending
|
|
544
|
-
], {
|
|
938
|
+
const { done } = yield* this.runAgentTurn({
|
|
545
939
|
system,
|
|
546
|
-
|
|
940
|
+
toolkit
|
|
547
941
|
});
|
|
548
|
-
|
|
549
|
-
prompt: prompt2,
|
|
550
|
-
toolkit,
|
|
551
|
-
disableToolCallResolution: true
|
|
552
|
-
}).pipe(
|
|
553
|
-
// Disable tool call parsing so that we can handle parameter schema errors on a per-tool call basis.
|
|
554
|
-
withoutToolCallParising,
|
|
555
|
-
// TOOD(dmaretskyi): Error mapping.
|
|
556
|
-
AiParser.parseResponse({
|
|
557
|
-
onBegin: () => observer.onBegin(),
|
|
558
|
-
onBlock: (block) => observer.onBlock(block),
|
|
559
|
-
onPart: (part) => observer.onPart(part),
|
|
560
|
-
onEnd: (summary) => observer.onEnd(summary)
|
|
561
|
-
}),
|
|
562
|
-
Stream.mapEffect((block) => Effect4.gen(function* () {
|
|
563
|
-
return yield* submitMessage(Obj3.make(Message2.Message, {
|
|
564
|
-
created: (/* @__PURE__ */ new Date()).toISOString(),
|
|
565
|
-
sender: {
|
|
566
|
-
role: "assistant"
|
|
567
|
-
},
|
|
568
|
-
blocks: [
|
|
569
|
-
block
|
|
570
|
-
]
|
|
571
|
-
}));
|
|
572
|
-
})),
|
|
573
|
-
Stream.runCollect,
|
|
574
|
-
Effect4.map(Chunk.toArray)
|
|
575
|
-
);
|
|
576
|
-
const toolCalls = messages.flatMap((message) => message.blocks.filter((block) => block._tag === "toolCall" && block.providerExecuted === false).map((block) => ({
|
|
577
|
-
block,
|
|
578
|
-
message
|
|
579
|
-
})));
|
|
580
|
-
if (toolCalls.length === 0) {
|
|
942
|
+
if (done) {
|
|
581
943
|
break;
|
|
582
|
-
} else if (!toolkit) {
|
|
583
|
-
throw new Error("No toolkit provided");
|
|
584
944
|
}
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
...context,
|
|
588
|
-
parentMessage: message.id,
|
|
589
|
-
toolCallId: block.toolCallId
|
|
590
|
-
}))));
|
|
945
|
+
yield* this.runTools({
|
|
946
|
+
toolkit
|
|
591
947
|
});
|
|
592
|
-
yield* submitMessage(Obj3.make(Message2.Message, {
|
|
593
|
-
created: (/* @__PURE__ */ new Date()).toISOString(),
|
|
594
|
-
sender: {
|
|
595
|
-
role: "tool"
|
|
596
|
-
},
|
|
597
|
-
blocks: toolResults
|
|
598
|
-
}));
|
|
599
|
-
this._toolCalls++;
|
|
600
948
|
} while (true);
|
|
601
|
-
this._ended = Date.now();
|
|
602
949
|
log3("done", {
|
|
603
950
|
pending: this._pending.length,
|
|
604
951
|
duration: this.duration,
|
|
605
952
|
tools: this._toolCalls
|
|
606
|
-
}, {
|
|
607
|
-
F: __dxlog_file3,
|
|
608
|
-
L: 236,
|
|
609
|
-
S: this,
|
|
610
|
-
C: (f, a) => f(...a)
|
|
611
|
-
});
|
|
953
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 237, S: this });
|
|
612
954
|
return this._pending;
|
|
613
|
-
}).pipe(this._semaphore.withPermits(1),
|
|
955
|
+
}).pipe(this._semaphore.withPermits(1), Effect5.withSpan("AiRequest.run"));
|
|
614
956
|
};
|
|
615
957
|
var createSnippet = (text, len = 32) => text.length <= len * 2 ? text : [
|
|
616
958
|
text.slice(0, len),
|
|
@@ -620,27 +962,30 @@ var createSnippet = (text, len = 32) => text.length <= len * 2 ? text : [
|
|
|
620
962
|
|
|
621
963
|
// src/session/toolkit.ts
|
|
622
964
|
import * as Toolkit from "@effect/ai/Toolkit";
|
|
623
|
-
import * as
|
|
624
|
-
import
|
|
625
|
-
import {
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
const
|
|
629
|
-
const
|
|
630
|
-
const
|
|
631
|
-
|
|
965
|
+
import * as Effect6 from "effect/Effect";
|
|
966
|
+
import * as Layer2 from "effect/Layer";
|
|
967
|
+
import { OpaqueToolkit as OpaqueToolkit2, ToolExecutionService as ToolExecutionService2, ToolResolverService as ToolResolverService2 } from "@dxos/ai";
|
|
968
|
+
import { isTruthy } from "@dxos/util";
|
|
969
|
+
var createToolkit = ({ toolkit: toolkitProp, blueprints = [], opaqueToolkits = [] }) => Effect6.gen(function* () {
|
|
970
|
+
const blueprintToolkit = yield* ToolResolverService2.resolveToolkit(blueprints.flatMap(({ tools }) => tools));
|
|
971
|
+
const blueprintToolHandler = yield* blueprintToolkit.toContext(ToolExecutionService2.handlersFor(blueprintToolkit));
|
|
972
|
+
const opaqueToolkit = OpaqueToolkit2.merge(...opaqueToolkits);
|
|
973
|
+
const toolkitDefs = [
|
|
974
|
+
toolkitProp?.toolkit,
|
|
632
975
|
blueprintToolkit,
|
|
633
|
-
|
|
634
|
-
].filter(
|
|
635
|
-
|
|
976
|
+
opaqueToolkit.toolkit
|
|
977
|
+
].filter(isTruthy);
|
|
978
|
+
const mergedToolkit = Toolkit.merge(...toolkitDefs);
|
|
979
|
+
const combinedHandlerLayer = Layer2.mergeAll(Layer2.succeedContext(blueprintToolHandler), toolkitProp?.layer ?? OpaqueToolkit2.empty.layer, opaqueToolkit.layer);
|
|
980
|
+
return OpaqueToolkit2.make(mergedToolkit, combinedHandlerLayer);
|
|
636
981
|
});
|
|
637
982
|
|
|
638
983
|
// src/session/version-pin.ts
|
|
639
|
-
import * as
|
|
984
|
+
import * as Schema5 from "effect/Schema";
|
|
640
985
|
import { getVersion } from "@dxos/echo-db";
|
|
641
986
|
import { ObjectId } from "@dxos/keys";
|
|
642
|
-
var ObjectVersionSchema =
|
|
643
|
-
var VersionPinSchema =
|
|
987
|
+
var ObjectVersionSchema = Schema5.Unknown;
|
|
988
|
+
var VersionPinSchema = Schema5.Struct({
|
|
644
989
|
// TODO(dmaretskyi): Use Ref when those support encoding.
|
|
645
990
|
objectId: ObjectId,
|
|
646
991
|
// TODO(dmaretskyi): Could be opaque.
|
|
@@ -663,176 +1008,225 @@ var VersionPin = class extends VersionPinSchema {
|
|
|
663
1008
|
}
|
|
664
1009
|
};
|
|
665
1010
|
|
|
666
|
-
// src/conversation/
|
|
667
|
-
var
|
|
1011
|
+
// src/conversation/session.ts
|
|
1012
|
+
var __dxlog_file5 = "/__w/dxos/dxos/packages/core/compute/assistant/src/conversation/session.ts";
|
|
668
1013
|
var SUMMARY_THRESHOLD = 8e4;
|
|
669
|
-
var
|
|
1014
|
+
var AiSession = class extends Resource2 {
|
|
670
1015
|
/**
|
|
671
|
-
* Blueprints and objects bound to the
|
|
1016
|
+
* Blueprints and objects bound to the session.
|
|
672
1017
|
*/
|
|
673
1018
|
_binder;
|
|
674
|
-
|
|
675
|
-
|
|
1019
|
+
_feed;
|
|
1020
|
+
_runtime;
|
|
676
1021
|
constructor(options) {
|
|
677
1022
|
super();
|
|
678
|
-
this.
|
|
679
|
-
this.
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
L: 72,
|
|
683
|
-
S: this,
|
|
684
|
-
A: [
|
|
685
|
-
"this._queue",
|
|
686
|
-
""
|
|
687
|
-
]
|
|
688
|
-
});
|
|
1023
|
+
this._feed = options.feed;
|
|
1024
|
+
this._runtime = options.runtime;
|
|
1025
|
+
invariant2(this._feed, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 41, S: this, A: ["this._feed", ""] });
|
|
1026
|
+
invariant2(this._runtime, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 42, S: this, A: ["this._runtime", ""] });
|
|
689
1027
|
this._binder = new AiContextBinder({
|
|
690
|
-
|
|
1028
|
+
feed: this._feed,
|
|
1029
|
+
runtime: this._runtime,
|
|
691
1030
|
registry: options.registry
|
|
692
1031
|
});
|
|
693
1032
|
}
|
|
694
1033
|
async _open() {
|
|
695
1034
|
await this._binder.open(this._ctx);
|
|
696
1035
|
}
|
|
697
|
-
get
|
|
698
|
-
return this.
|
|
1036
|
+
get feed() {
|
|
1037
|
+
return this._feed;
|
|
699
1038
|
}
|
|
700
1039
|
get context() {
|
|
701
1040
|
return this._binder;
|
|
702
1041
|
}
|
|
703
|
-
get toolkit() {
|
|
704
|
-
return this._toolkit;
|
|
705
|
-
}
|
|
706
1042
|
async getHistory() {
|
|
707
|
-
const
|
|
708
|
-
|
|
1043
|
+
const queryResult = await Runtime2.runPromise(this._runtime)(Feed2.query(this._feed, Filter.type(Message3.Message)));
|
|
1044
|
+
const items = await queryResult.run();
|
|
1045
|
+
return items.filter(Obj6.instanceOf(Message3.Message));
|
|
709
1046
|
}
|
|
710
1047
|
getTools() {
|
|
711
|
-
return
|
|
712
|
-
const
|
|
713
|
-
|
|
714
|
-
toolkit: this._toolkit,
|
|
715
|
-
blueprints
|
|
1048
|
+
return Effect7.gen(this, function* () {
|
|
1049
|
+
const toolkit = yield* createToolkit({
|
|
1050
|
+
blueprints: this.context.getBlueprints()
|
|
716
1051
|
});
|
|
717
|
-
return
|
|
718
|
-
}).pipe(
|
|
1052
|
+
return toolkit.toolkit.tools;
|
|
1053
|
+
}).pipe(Effect7.orDie);
|
|
1054
|
+
}
|
|
1055
|
+
/**
|
|
1056
|
+
* Not provided by default, since users might want to override them.
|
|
1057
|
+
*/
|
|
1058
|
+
makeToolExecutionServices() {
|
|
1059
|
+
return ToolExecutionServices.pipe(Layer3.provide(Operation2.withInvocationOptions({
|
|
1060
|
+
conversation: Obj6.getDXN(this._feed).toString()
|
|
1061
|
+
})));
|
|
719
1062
|
}
|
|
720
1063
|
/**
|
|
721
1064
|
* Creates a new cancelable request effect.
|
|
722
1065
|
*/
|
|
723
1066
|
createRequest(params) {
|
|
724
|
-
return
|
|
725
|
-
const history = yield*
|
|
1067
|
+
return Effect7.gen(this, function* () {
|
|
1068
|
+
const history = yield* Effect7.promise(() => this.getHistory());
|
|
726
1069
|
const blueprints = this.context.getBlueprints();
|
|
727
|
-
const mcps = yield* connectMcpServers(blueprints);
|
|
728
|
-
const toolkit = yield* createToolkit({
|
|
729
|
-
toolkit: this._toolkit,
|
|
730
|
-
blueprints,
|
|
731
|
-
genericToolkits: mcps
|
|
732
|
-
});
|
|
733
1070
|
const objects = this.context.getObjects();
|
|
734
1071
|
log4("run", {
|
|
735
1072
|
history: history.length,
|
|
736
1073
|
blueprints: blueprints.length,
|
|
737
|
-
tools: Object.keys(toolkit.tools).length,
|
|
738
1074
|
objects: objects.length
|
|
739
|
-
}, {
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
summarizationThreshold: SUMMARY_THRESHOLD
|
|
1075
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 85, S: this });
|
|
1076
|
+
const request = new AiRequest({
|
|
1077
|
+
summarizationThreshold: SUMMARY_THRESHOLD,
|
|
1078
|
+
observer: params.observer,
|
|
1079
|
+
onOutput: (message) => Effect7.promise(() => Runtime2.runPromise(this._runtime)(Feed2.append(this._feed, [
|
|
1080
|
+
message
|
|
1081
|
+
])))
|
|
747
1082
|
});
|
|
748
|
-
|
|
1083
|
+
yield* request.begin({
|
|
749
1084
|
history,
|
|
750
1085
|
blueprints,
|
|
751
|
-
toolkit,
|
|
752
1086
|
objects,
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
message
|
|
756
|
-
]))
|
|
757
|
-
}).pipe(Effect6.provide(Layer.mergeAll(Layer.succeed(AiContextService, {
|
|
758
|
-
binder: this.context
|
|
759
|
-
}), Layer.succeed(AiConversationService, this))));
|
|
760
|
-
log4("result", {
|
|
761
|
-
messages: messages.length,
|
|
762
|
-
duration: session.duration,
|
|
763
|
-
toolCalls: session.toolCalls
|
|
764
|
-
}, {
|
|
765
|
-
F: __dxlog_file4,
|
|
766
|
-
L: 159,
|
|
767
|
-
S: this,
|
|
768
|
-
C: (f, a) => f(...a)
|
|
1087
|
+
prompt: params.prompt,
|
|
1088
|
+
system: params.system
|
|
769
1089
|
});
|
|
770
|
-
|
|
771
|
-
|
|
1090
|
+
do {
|
|
1091
|
+
yield* Effect7.promise(() => this.context.sync());
|
|
1092
|
+
const currentBlueprints = this.context.getBlueprints();
|
|
1093
|
+
const mcps = yield* connectMcpServers(currentBlueprints, params.mcpServers);
|
|
1094
|
+
const toolkit = yield* createToolkit({
|
|
1095
|
+
toolkit: params.toolkit,
|
|
1096
|
+
blueprints: currentBlueprints,
|
|
1097
|
+
opaqueToolkits: mcps
|
|
1098
|
+
});
|
|
1099
|
+
log4("toolkit", {
|
|
1100
|
+
tools: Record2.keys(toolkit.toolkit.tools)
|
|
1101
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 114, S: this });
|
|
1102
|
+
const system = yield* formatSystemPrompt({
|
|
1103
|
+
system: params.system,
|
|
1104
|
+
blueprints: currentBlueprints,
|
|
1105
|
+
objects: this.context.getObjects()
|
|
1106
|
+
}).pipe(Effect7.orDie);
|
|
1107
|
+
const { done } = yield* request.runAgentTurn({
|
|
1108
|
+
system,
|
|
1109
|
+
toolkit
|
|
1110
|
+
});
|
|
1111
|
+
if (done) {
|
|
1112
|
+
break;
|
|
1113
|
+
}
|
|
1114
|
+
yield* request.runTools({
|
|
1115
|
+
toolkit
|
|
1116
|
+
});
|
|
1117
|
+
} while (true);
|
|
1118
|
+
log4("result", {
|
|
1119
|
+
messages: request.pending.length,
|
|
1120
|
+
duration: request.duration,
|
|
1121
|
+
toolCalls: request.toolCalls
|
|
1122
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 133, S: this });
|
|
1123
|
+
return [
|
|
1124
|
+
...request.pending
|
|
1125
|
+
];
|
|
1126
|
+
}).pipe(Effect7.provide(Layer3.mergeAll(Layer3.succeed(AiContextService, {
|
|
1127
|
+
binder: this.context
|
|
1128
|
+
}), Layer3.succeed(AiSessionService, this), Operation2.withInvocationOptions({
|
|
1129
|
+
conversation: Obj6.getDXN(this._feed).toString()
|
|
1130
|
+
}))), Effect7.withSpan("AiSession.createRequest"));
|
|
772
1131
|
}
|
|
773
1132
|
};
|
|
774
|
-
var
|
|
1133
|
+
var AiSessionService = class _AiSessionService extends Context4.Tag("@dxos/assistant/AiSessionService")() {
|
|
775
1134
|
/**
|
|
776
|
-
* Create a new
|
|
1135
|
+
* Create a new session layer from options.
|
|
777
1136
|
*/
|
|
778
|
-
static layer = (options) =>
|
|
779
|
-
const
|
|
780
|
-
return
|
|
1137
|
+
static layer = (options) => aiContextFromSession.pipe(Layer3.provideMerge(Layer3.scoped(_AiSessionService, Effect7.gen(function* () {
|
|
1138
|
+
const session = yield* acquireReleaseResource(() => new AiSession(options));
|
|
1139
|
+
return session;
|
|
781
1140
|
}))));
|
|
782
1141
|
/**
|
|
783
|
-
* Create a new
|
|
1142
|
+
* Create a new session with a new feed.
|
|
784
1143
|
*/
|
|
785
|
-
static
|
|
786
|
-
|
|
1144
|
+
static layerNewFeed = (options) => Layer3.unwrapScoped(Effect7.gen(function* () {
|
|
1145
|
+
const feed = Feed2.make();
|
|
1146
|
+
yield* Database2.add(feed);
|
|
1147
|
+
const runtime2 = yield* Effect7.runtime();
|
|
1148
|
+
return _AiSessionService.layer({
|
|
787
1149
|
...options,
|
|
788
|
-
|
|
1150
|
+
feed,
|
|
1151
|
+
runtime: runtime2
|
|
789
1152
|
});
|
|
790
1153
|
}));
|
|
791
1154
|
/**
|
|
792
|
-
* Run a prompt in the current
|
|
1155
|
+
* Run a prompt in the current session.
|
|
793
1156
|
*/
|
|
794
|
-
static run = (params) =>
|
|
795
|
-
const
|
|
796
|
-
return yield*
|
|
1157
|
+
static run = (params) => Effect7.gen(function* () {
|
|
1158
|
+
const session = yield* _AiSessionService;
|
|
1159
|
+
return yield* session.createRequest(params);
|
|
797
1160
|
});
|
|
798
1161
|
};
|
|
799
|
-
var
|
|
800
|
-
const
|
|
1162
|
+
var aiContextFromSession = Layer3.effect(AiContextService, Effect7.gen(function* () {
|
|
1163
|
+
const session = yield* AiSessionService;
|
|
801
1164
|
return {
|
|
802
|
-
binder:
|
|
1165
|
+
binder: session.context
|
|
803
1166
|
};
|
|
804
1167
|
}));
|
|
805
|
-
var connectMcpServers = (blueprints
|
|
806
|
-
url,
|
|
807
|
-
kind: protocol
|
|
808
|
-
}).pipe(
|
|
809
|
-
// NOTE: Type-inference fails here without explicit void return.
|
|
810
|
-
Effect6.tap((toolkit) => log4.info("Connected to MCP server", {
|
|
1168
|
+
var connectMcpServers = (blueprints, spaceMcpServers = []) => {
|
|
1169
|
+
const blueprintServers = pipe4(blueprints, Array4.flatMap((_) => _.mcpServers ?? []), Array4.map(({ url, protocol }) => ({
|
|
811
1170
|
url,
|
|
812
|
-
|
|
813
|
-
}
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
}))
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
1171
|
+
kind: protocol
|
|
1172
|
+
})));
|
|
1173
|
+
const spaceServers = spaceMcpServers.map(({ url, protocol, apiKey }) => ({
|
|
1174
|
+
url,
|
|
1175
|
+
kind: protocol,
|
|
1176
|
+
apiKey
|
|
1177
|
+
}));
|
|
1178
|
+
const allServers = [
|
|
1179
|
+
...blueprintServers,
|
|
1180
|
+
...spaceServers
|
|
1181
|
+
];
|
|
1182
|
+
return pipe4(allServers, Effect7.forEach((options) => McpToolkit.make(options).pipe(
|
|
1183
|
+
// NOTE: Type-inference fails here without explicit void return.
|
|
1184
|
+
Effect7.tap((toolkit) => log4.info("Connected to MCP server", {
|
|
1185
|
+
url: options.url,
|
|
1186
|
+
tools: Object.keys(toolkit.toolkit.tools).length
|
|
1187
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 197, S: void 0 })),
|
|
1188
|
+
// Surface typed connection failures via ephemeral trace + warn log, then drop the server.
|
|
1189
|
+
Effect7.tapError((error) => Effect7.gen(function* () {
|
|
1190
|
+
log4.warn("Failed to connect to MCP server", {
|
|
1191
|
+
url: error.url,
|
|
1192
|
+
kind: error.kind,
|
|
1193
|
+
message: error.message
|
|
1194
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 202, S: this });
|
|
1195
|
+
yield* Trace3.write(McpServerError, {
|
|
1196
|
+
url: error.url,
|
|
1197
|
+
kind: error.kind,
|
|
1198
|
+
message: error.message
|
|
1199
|
+
});
|
|
1200
|
+
})),
|
|
1201
|
+
// Catch unexpected defects too (e.g. malformed tool schemas) so a single broken
|
|
1202
|
+
// server can never abort the whole turn — surface them through the same channel.
|
|
1203
|
+
Effect7.catchAllDefect((defect) => Effect7.gen(function* () {
|
|
1204
|
+
const message = defect instanceof Error ? defect.message : String(defect);
|
|
1205
|
+
log4.warn("Unexpected MCP defect", {
|
|
1206
|
+
url: options.url,
|
|
1207
|
+
message
|
|
1208
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 216, S: this });
|
|
1209
|
+
yield* Trace3.write(McpServerError, {
|
|
1210
|
+
url: options.url,
|
|
1211
|
+
kind: options.kind,
|
|
1212
|
+
message: `Unexpected MCP failure: ${message}`
|
|
1213
|
+
});
|
|
1214
|
+
return yield* Effect7.fail(new McpToolkit.McpConnectionError({
|
|
1215
|
+
url: options.url,
|
|
1216
|
+
kind: options.kind,
|
|
1217
|
+
message
|
|
1218
|
+
}));
|
|
1219
|
+
})),
|
|
1220
|
+
Effect7.either
|
|
1221
|
+
)), Effect7.map(Array4.filterMap((_) => Either.getRight(_))));
|
|
1222
|
+
};
|
|
829
1223
|
|
|
830
1224
|
// src/diff.ts
|
|
831
1225
|
import { next as A } from "@automerge/automerge";
|
|
832
1226
|
import { DocAccessor, toCursorRange } from "@dxos/echo-db";
|
|
833
1227
|
import { log as log5 } from "@dxos/log";
|
|
834
|
-
import { isNonNullable } from "@dxos/util";
|
|
835
|
-
var
|
|
1228
|
+
import { isNonNullable as isNonNullable2 } from "@dxos/util";
|
|
1229
|
+
var __dxlog_file6 = "/__w/dxos/dxos/packages/core/compute/assistant/src/diff.ts";
|
|
836
1230
|
var reduceDiffs = (diffs) => {
|
|
837
1231
|
return diffs.reduce((acc, diff) => {
|
|
838
1232
|
const current = acc[acc.length - 1];
|
|
@@ -876,7 +1270,7 @@ var computeDiffsWithCursors = (accessor, diffs) => {
|
|
|
876
1270
|
text: diff.replace
|
|
877
1271
|
};
|
|
878
1272
|
}
|
|
879
|
-
}).filter(
|
|
1273
|
+
}).filter(isNonNullable2);
|
|
880
1274
|
};
|
|
881
1275
|
var applyDiffs = (accessor, diffs, { errorOnNotFound = false } = {}) => {
|
|
882
1276
|
for (const diff of reduceDiffs(diffs)) {
|
|
@@ -889,12 +1283,7 @@ var applyDiffs = (accessor, diffs, { errorOnNotFound = false } = {}) => {
|
|
|
889
1283
|
if (errorOnNotFound) {
|
|
890
1284
|
throw new Error(`Diff not found: ${diff.match}`);
|
|
891
1285
|
} else {
|
|
892
|
-
log5.warn("diff not found", diff, {
|
|
893
|
-
F: __dxlog_file5,
|
|
894
|
-
L: 86,
|
|
895
|
-
S: void 0,
|
|
896
|
-
C: (f, a) => f(...a)
|
|
897
|
-
});
|
|
1286
|
+
log5.warn("diff not found", diff, { "~LogMeta": "~LogMeta", F: __dxlog_file6, L: 71, S: void 0 });
|
|
898
1287
|
}
|
|
899
1288
|
}
|
|
900
1289
|
});
|
|
@@ -904,11 +1293,11 @@ var applyDiffs = (accessor, diffs, { errorOnNotFound = false } = {}) => {
|
|
|
904
1293
|
|
|
905
1294
|
// src/execution-graph.ts
|
|
906
1295
|
import { AgentStatus } from "@dxos/ai";
|
|
907
|
-
import { Obj as
|
|
1296
|
+
import { Obj as Obj7 } from "@dxos/echo";
|
|
908
1297
|
import { MESSAGE_PROPERTY_TOOL_CALL_ID } from "@dxos/functions";
|
|
909
1298
|
import { LogLevel } from "@dxos/log";
|
|
910
|
-
import { ContentBlock, Message as Message4 } from "@dxos/types";
|
|
911
|
-
import { isNonNullable as
|
|
1299
|
+
import { ContentBlock as ContentBlock3, Message as Message4 } from "@dxos/types";
|
|
1300
|
+
import { isNonNullable as isNonNullable3 } from "@dxos/util";
|
|
912
1301
|
var SKIP_BLOCKS = [
|
|
913
1302
|
"text"
|
|
914
1303
|
];
|
|
@@ -931,9 +1320,9 @@ var ExecutionGraph = class {
|
|
|
931
1320
|
addEvents(events) {
|
|
932
1321
|
const sortedEvents = this.sortObjectsByCreated(events);
|
|
933
1322
|
for (const event of sortedEvents) {
|
|
934
|
-
if (
|
|
1323
|
+
if (Obj7.instanceOf(Message4.Message, event)) {
|
|
935
1324
|
this._processMessage(event);
|
|
936
|
-
} else if (
|
|
1325
|
+
} else if (Obj7.instanceOf(AgentStatus, event)) {
|
|
937
1326
|
this._processAgentStatus(event);
|
|
938
1327
|
}
|
|
939
1328
|
}
|
|
@@ -1035,11 +1424,17 @@ var ExecutionGraph = class {
|
|
|
1035
1424
|
*/
|
|
1036
1425
|
_updatePendingToolResults(toolCallId, agentStatusId) {
|
|
1037
1426
|
const toolResultId = this._pendingToolResults.get(toolCallId);
|
|
1038
|
-
if (!toolResultId)
|
|
1427
|
+
if (!toolResultId) {
|
|
1428
|
+
return;
|
|
1429
|
+
}
|
|
1039
1430
|
const toolResultCommit = this._commits.find((c) => c.id === toolResultId);
|
|
1040
|
-
if (!toolResultCommit)
|
|
1431
|
+
if (!toolResultCommit) {
|
|
1432
|
+
return;
|
|
1433
|
+
}
|
|
1041
1434
|
const toolCallCommitId = this._toolCallCommitIds.get(toolCallId);
|
|
1042
|
-
if (!toolCallCommitId)
|
|
1435
|
+
if (!toolCallCommitId) {
|
|
1436
|
+
return;
|
|
1437
|
+
}
|
|
1043
1438
|
const toolCallIndex = toolResultCommit.parents.indexOf(toolCallCommitId);
|
|
1044
1439
|
if (toolCallIndex !== -1) {
|
|
1045
1440
|
toolResultCommit.parents[toolCallIndex] = agentStatusId;
|
|
@@ -1051,7 +1446,9 @@ var ExecutionGraph = class {
|
|
|
1051
1446
|
*/
|
|
1052
1447
|
_handleLateToolResultUpdate(toolCallId, toolResultId) {
|
|
1053
1448
|
const toolCallCommitId = this._toolCallCommitIds.get(toolCallId);
|
|
1054
|
-
if (!toolCallCommitId)
|
|
1449
|
+
if (!toolCallCommitId) {
|
|
1450
|
+
return;
|
|
1451
|
+
}
|
|
1055
1452
|
const messageId = toolCallCommitId.split("_toolCall_")[0];
|
|
1056
1453
|
const toolCallBranch = `${messageId}_${toolCallId}`;
|
|
1057
1454
|
const agentStatusCommitId = this._lastCommitByBranch.get(toolCallBranch);
|
|
@@ -1120,7 +1517,7 @@ var messageToCommits = (message, lastBlockId, toolCallIds, lastCommitByBranch, s
|
|
|
1120
1517
|
previousBlockId = commit.id;
|
|
1121
1518
|
}
|
|
1122
1519
|
return commit;
|
|
1123
|
-
}).filter(
|
|
1520
|
+
}).filter(isNonNullable3);
|
|
1124
1521
|
};
|
|
1125
1522
|
var getBlockParents = (block, previousBlockId, message, toolCallIds, lastCommitByBranch) => {
|
|
1126
1523
|
const parents = [];
|
|
@@ -1154,7 +1551,9 @@ var createBlockCommit = (block, message, branch, parents, idx) => {
|
|
|
1154
1551
|
const timestamp = new Date(message.created);
|
|
1155
1552
|
switch (block._tag) {
|
|
1156
1553
|
case "text":
|
|
1157
|
-
if (!block.text.trim().length)
|
|
1554
|
+
if (!block.text.trim().length) {
|
|
1555
|
+
return null;
|
|
1556
|
+
}
|
|
1158
1557
|
return {
|
|
1159
1558
|
id: getGenericBlockId(message.id, idx),
|
|
1160
1559
|
branch,
|
|
@@ -1167,7 +1566,7 @@ var createBlockCommit = (block, message, branch, parents, idx) => {
|
|
|
1167
1566
|
],
|
|
1168
1567
|
message: "Processing request..."
|
|
1169
1568
|
} : {
|
|
1170
|
-
icon: "ph--
|
|
1569
|
+
icon: "ph--drone--regular",
|
|
1171
1570
|
message: `Response (${block.text.split(" ").length} words)`
|
|
1172
1571
|
}
|
|
1173
1572
|
};
|
|
@@ -1199,7 +1598,7 @@ var createBlockCommit = (block, message, branch, parents, idx) => {
|
|
|
1199
1598
|
timestamp,
|
|
1200
1599
|
icon: "ph--rocket--regular",
|
|
1201
1600
|
level: LogLevel.INFO,
|
|
1202
|
-
message:
|
|
1601
|
+
message: ContentBlock3.createStatsMessage(block)
|
|
1203
1602
|
};
|
|
1204
1603
|
case "status":
|
|
1205
1604
|
return {
|
|
@@ -1269,47 +1668,49 @@ var stringifyRef = (ref) => {
|
|
|
1269
1668
|
return ref.dxn.asEchoDXN()?.echoId ?? ref.dxn.asQueueDXN()?.objectId ?? "";
|
|
1270
1669
|
};
|
|
1271
1670
|
var stringifyObject = (obj) => {
|
|
1272
|
-
return
|
|
1671
|
+
return Obj7.getLabel(obj) ?? Obj7.getTypename(obj) ?? obj.id;
|
|
1273
1672
|
};
|
|
1274
1673
|
|
|
1275
1674
|
// src/templates/system.ts
|
|
1276
1675
|
import defaultsDeep from "lodash.defaultsdeep";
|
|
1277
|
-
import { Template as Template2 } from "@dxos/
|
|
1676
|
+
import { Template as Template2 } from "@dxos/compute";
|
|
1278
1677
|
|
|
1279
|
-
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/instructions/format.tpl?raw
|
|
1678
|
+
// raw-loader:/__w/dxos/dxos/packages/core/compute/assistant/src/templates/instructions/format.tpl?raw
|
|
1280
1679
|
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## Context\n\nThe current date and time is {{DATETIME}}.\n";
|
|
1281
1680
|
|
|
1282
|
-
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/instructions/system.tpl?raw
|
|
1283
|
-
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##
|
|
1681
|
+
// raw-loader:/__w/dxos/dxos/packages/core/compute/assistant/src/templates/instructions/system.tpl?raw
|
|
1682
|
+
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## Navigation\n\n- When the user asks to open, go to, navigate to, or show a specific document, page, or object, use the navigation tools instead of loading content inline.\n- First, call the Resolve navigation targets tool to find the navigation path for the requested item.\n - Without arguments, it returns available pages that can be navigated to.\n - With a DXN in the query, it resolves that specific object to a navigation path.\n- If you know the object's DXN (e.g. from a database query or context objects), pass it in the query to get the exact navigation path.\n- Then, call the Open tool with the resolved navigation path(s) in the subject array to navigate the user to that item.\n- Do not respond with the document's content when the user asks to open it \u2014 open it in the main content area instead.\n- If multiple targets match, present the options to the user and let them choose which to open.\n\n## Response format\n\n- NEVER output narration text outside `<status>` tags or the final user-facing answer. Any progress update, transition sentence, or \"let me\u2026\" lead-in MUST be wrapped in `<status>...</status>`. Violating this is an error.\n- When updating the user about the progress of the work you are doing, put the update in a <status> XML tag.\n- Only when you need to show the result to the user or ask a question use a text block without status tags.\n- After a `</status>` tag, the next output must be either another `<status>` tag, a tool call, or the final user-facing answer \u2014 never a transition sentence.\n\n<example>\n// reasoning...\n<status>Looking for emails from Depot</status>\n// tool call..\n<status>Found 2 emails from Depot.</status>\n<status>Loading email details</status>\n// tool call\n<status>Located mailbox</status>\nI found **2 emails from Depot**:\n</example>\n\nDo not do (no status tags):\n\n<incorrect_example>\n// reasoning...\n// tool call..\nI found 2 emails from Depot. Let me load their details:\n// tool call\nI found **2 emails from Depot**:\n</incorrect_example>\n\nDo not do (transition sentence after a status tag, before tool calls):\n\n<incorrect_example>\n<status>Doing X</status>\nLet me start by doing X:\n// tool call\n</incorrect_example>\n\n- NEVER write a lead-in sentence before a tool call. Use a `<status>` tag instead.\n- Avoid using plain text to tell the user what you are about to do; instead, use status blocks.\n- WRONG: Let me get...\n- CORRECT: <status>Getting...</status>";
|
|
1284
1683
|
|
|
1285
1684
|
// src/templates/system.ts
|
|
1286
1685
|
var createSystemPrompt = (options = {}) => Template2.process(format_default, defaultsDeep(options, {
|
|
1287
1686
|
DATETIME: (/* @__PURE__ */ new Date()).toISOString()
|
|
1288
1687
|
}));
|
|
1688
|
+
var SYSTEM_ID = "01K2J2FZWH39F55CS7B26GP1Q3";
|
|
1289
1689
|
var templates = {
|
|
1290
|
-
/** Editable system prompt.*/
|
|
1291
1690
|
system: Template2.make({
|
|
1292
|
-
|
|
1293
|
-
* NOTE: We are forbidden from accessing randomness at model level in cloudflare, so we have to have pre-generated ids.
|
|
1294
|
-
*/
|
|
1295
|
-
id: "01K2J2FZWH39F55CS7B26GP1Q3",
|
|
1691
|
+
id: SYSTEM_ID,
|
|
1296
1692
|
source: system_default
|
|
1297
1693
|
})
|
|
1298
1694
|
};
|
|
1299
1695
|
export {
|
|
1696
|
+
AgentRequestBegin,
|
|
1697
|
+
AgentRequestEnd,
|
|
1300
1698
|
AiAssistantError,
|
|
1301
1699
|
AiContextBinder,
|
|
1302
1700
|
AiContextService,
|
|
1303
|
-
AiConversation,
|
|
1304
|
-
AiConversationService,
|
|
1305
1701
|
AiModelError,
|
|
1702
|
+
AiRequest,
|
|
1306
1703
|
AiSession,
|
|
1704
|
+
AiSessionService,
|
|
1307
1705
|
ArtifactDiffResolver,
|
|
1308
1706
|
ArtifactId,
|
|
1309
1707
|
Bindings,
|
|
1708
|
+
CompleteBlock,
|
|
1310
1709
|
ContextBinding,
|
|
1311
1710
|
ExecutionGraph,
|
|
1312
1711
|
GenerationObserver,
|
|
1712
|
+
McpServerError,
|
|
1713
|
+
PartialBlock,
|
|
1313
1714
|
RefFromLLM,
|
|
1314
1715
|
ToolExecutionServices,
|
|
1315
1716
|
VersionPin,
|
|
@@ -1320,8 +1721,10 @@ export {
|
|
|
1320
1721
|
createToolkit,
|
|
1321
1722
|
formatSystemPrompt,
|
|
1322
1723
|
formatUserPrompt,
|
|
1323
|
-
|
|
1324
|
-
|
|
1724
|
+
getOperationFromTool,
|
|
1725
|
+
makeToolExecutionService,
|
|
1726
|
+
makeToolExecutionServiceFromOperationInvoker,
|
|
1727
|
+
makeToolResolverFromOperations,
|
|
1325
1728
|
reduceDiffs,
|
|
1326
1729
|
templates
|
|
1327
1730
|
};
|