@dxos/assistant 0.8.4-main.d05673bc65 → 0.8.4-main.dfabb4ec29
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 +770 -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 +32 -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 +73 -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 +18 -12
- package/src/conversation/context.ts +128 -55
- package/src/conversation/index.ts +1 -1
- package/src/conversation/session.ts +323 -0
- package/src/diff.test.ts +3 -3
- 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 +347 -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: "org.dxos.type.
|
|
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
|
+
protocol: 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,208 @@ 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
|
+
log3("write complete block", {
|
|
767
|
+
messageId: message.id,
|
|
768
|
+
role: message.sender.role,
|
|
769
|
+
block: JSON.stringify(block)
|
|
770
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 57, S: this });
|
|
771
|
+
yield* Trace2.write(CompleteBlock, {
|
|
772
|
+
messageId: message.id,
|
|
773
|
+
role: message.sender.role,
|
|
774
|
+
block
|
|
775
|
+
});
|
|
776
|
+
}
|
|
777
|
+
yield* this._onOutput(message);
|
|
778
|
+
return message;
|
|
779
|
+
});
|
|
780
|
+
getToolCalls = () => pipe3([
|
|
781
|
+
...this._history,
|
|
782
|
+
...this._pending
|
|
783
|
+
], Array3.reverse, Array3.takeWhile((_) => _.sender.role === "assistant"), Array3.flatMap((_) => _.blocks.filter(ContentBlock2.is("toolCall")).map((block) => ({
|
|
784
|
+
block,
|
|
785
|
+
message: _
|
|
786
|
+
}))), Array3.filter((_) => !_.block.providerExecuted), Array3.reverse);
|
|
787
|
+
/**
|
|
788
|
+
* Initialize a session: set up history, perform summarization if needed, and submit the user prompt.
|
|
789
|
+
* Must be called before `runTurn()`.
|
|
790
|
+
*/
|
|
791
|
+
begin = ({ prompt, system, history = [], blueprints = [], objects = [] }) => Effect5.gen(this, function* () {
|
|
491
792
|
this._started = Date.now();
|
|
492
793
|
this._history = [
|
|
493
794
|
...history
|
|
494
795
|
];
|
|
495
796
|
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,
|
|
797
|
+
const systemPrompt = yield* formatSystemPrompt({
|
|
798
|
+
system,
|
|
505
799
|
blueprints,
|
|
506
800
|
objects
|
|
507
|
-
}).pipe(
|
|
801
|
+
}).pipe(Effect5.orDie);
|
|
508
802
|
if (this._options.summarizationThreshold !== void 0) {
|
|
509
803
|
const tokenCount = yield* AiPreprocessor.estimateTokens(yield* AiPreprocessor.preprocessPrompt([
|
|
510
804
|
...this._history
|
|
511
805
|
], {
|
|
512
|
-
system
|
|
806
|
+
system: systemPrompt
|
|
513
807
|
}));
|
|
514
808
|
if (tokenCount > this._options.summarizationThreshold) {
|
|
515
809
|
const summary = yield* AiSummarizer.summarize([
|
|
516
810
|
...this._history
|
|
517
811
|
]);
|
|
518
|
-
yield*
|
|
812
|
+
yield* this._submitMessage(summary);
|
|
519
813
|
}
|
|
520
814
|
}
|
|
521
|
-
|
|
815
|
+
yield* this._submitMessage(yield* formatUserPrompt({
|
|
522
816
|
prompt,
|
|
523
817
|
history
|
|
524
818
|
}));
|
|
819
|
+
}).pipe(Effect5.withSpan("AiRequest.begin"));
|
|
820
|
+
/**
|
|
821
|
+
* Execute a single turn: one LLM generation followed by tool execution.
|
|
822
|
+
* The toolkit and system prompt can be updated between turns to reflect context changes (e.g. dynamically enabled blueprints).
|
|
823
|
+
*/
|
|
824
|
+
runAgentTurn = ({ system, toolkit: opaqueToolkit }) => Effect5.gen(this, function* () {
|
|
825
|
+
log3.info("request", {
|
|
826
|
+
system: {
|
|
827
|
+
snippet: createSnippet(system),
|
|
828
|
+
length: system.length
|
|
829
|
+
},
|
|
830
|
+
pending: this._pending.length,
|
|
831
|
+
history: this._history.length
|
|
832
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 114, S: this });
|
|
833
|
+
const prompt = yield* AiPreprocessor.preprocessPrompt([
|
|
834
|
+
...this._history,
|
|
835
|
+
...this._pending
|
|
836
|
+
], {
|
|
837
|
+
system,
|
|
838
|
+
cacheControl: "ephemeral"
|
|
839
|
+
});
|
|
840
|
+
const toolkit = opaqueToolkit ? yield* opaqueToolkit.handlers : void 0;
|
|
841
|
+
const observer = this._observer;
|
|
842
|
+
let currentMessageId = null;
|
|
843
|
+
const messages = yield* LanguageModel.streamText({
|
|
844
|
+
prompt,
|
|
845
|
+
toolkit,
|
|
846
|
+
disableToolCallResolution: true
|
|
847
|
+
}).pipe(withoutToolCallParising, AiParser.parseResponse({
|
|
848
|
+
emitPartial: true,
|
|
849
|
+
onBegin: () => observer.onBegin(),
|
|
850
|
+
onBlock: (block) => observer.onBlock(block),
|
|
851
|
+
onPart: (part) => observer.onPart(part),
|
|
852
|
+
onEnd: (summary) => observer.onEnd(summary)
|
|
853
|
+
}), Stream.mapEffect((block) => Effect5.gen(this, function* () {
|
|
854
|
+
if (block.pending) {
|
|
855
|
+
currentMessageId ??= Obj5.ID.random();
|
|
856
|
+
log3("emit ephemeral message", {
|
|
857
|
+
id: currentMessageId,
|
|
858
|
+
type: block._tag
|
|
859
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 145, S: this });
|
|
860
|
+
yield* Trace2.write(PartialBlock, {
|
|
861
|
+
messageId: currentMessageId,
|
|
862
|
+
role: "assistant",
|
|
863
|
+
block
|
|
864
|
+
});
|
|
865
|
+
return Option3.none();
|
|
866
|
+
} else {
|
|
867
|
+
currentMessageId ??= Obj5.ID.random();
|
|
868
|
+
const id = currentMessageId;
|
|
869
|
+
currentMessageId = null;
|
|
870
|
+
log3("emit complete message", {
|
|
871
|
+
id,
|
|
872
|
+
type: block._tag
|
|
873
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 159, S: this });
|
|
874
|
+
const message = Obj5.make(Message2.Message, {
|
|
875
|
+
id,
|
|
876
|
+
created: (/* @__PURE__ */ new Date()).toISOString(),
|
|
877
|
+
sender: {
|
|
878
|
+
role: "assistant"
|
|
879
|
+
},
|
|
880
|
+
blocks: [
|
|
881
|
+
block
|
|
882
|
+
]
|
|
883
|
+
});
|
|
884
|
+
return Option3.some(yield* this._submitMessage(message));
|
|
885
|
+
}
|
|
886
|
+
}), {
|
|
887
|
+
concurrency: 1,
|
|
888
|
+
unordered: false
|
|
889
|
+
}), Stream.filterMap((_) => _), Stream.runCollect, Effect5.map(Chunk.toArray));
|
|
890
|
+
log3.info("messages", {
|
|
891
|
+
messages
|
|
892
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 179, S: this });
|
|
893
|
+
const toolCalls = this.getToolCalls();
|
|
894
|
+
if (toolCalls.length === 0) {
|
|
895
|
+
this._ended = Date.now();
|
|
896
|
+
return {
|
|
897
|
+
messages,
|
|
898
|
+
done: true
|
|
899
|
+
};
|
|
900
|
+
} else if (!toolkit) {
|
|
901
|
+
throw new Error("No toolkit provided");
|
|
902
|
+
}
|
|
903
|
+
return {
|
|
904
|
+
messages,
|
|
905
|
+
done: false
|
|
906
|
+
};
|
|
907
|
+
}).pipe(Effect5.withSpan("AiRequest.runAgentTurn"));
|
|
908
|
+
runTools = ({ toolkit: opaqueToolkit }) => Effect5.gen(this, function* () {
|
|
909
|
+
const toolkit = opaqueToolkit ? yield* opaqueToolkit.handlers : void 0;
|
|
910
|
+
const toolCalls = this.getToolCalls();
|
|
911
|
+
const toolResults = yield* Effect5.forEach(toolCalls, ({ block, message }) => {
|
|
912
|
+
if (!toolkit) {
|
|
913
|
+
throw new Error("No toolkit provided");
|
|
914
|
+
}
|
|
915
|
+
return callTool(toolkit, block);
|
|
916
|
+
});
|
|
917
|
+
yield* this._submitMessage(Obj5.make(Message2.Message, {
|
|
918
|
+
created: (/* @__PURE__ */ new Date()).toISOString(),
|
|
919
|
+
sender: {
|
|
920
|
+
role: "tool"
|
|
921
|
+
},
|
|
922
|
+
blocks: toolResults
|
|
923
|
+
}));
|
|
924
|
+
this._toolCalls += toolResults.length;
|
|
925
|
+
}).pipe(Effect5.withSpan("AiRequest.runTools"));
|
|
926
|
+
/**
|
|
927
|
+
* Run a full conversation turn loop. Equivalent to calling `begin()` then `runTurn()` in a loop.
|
|
928
|
+
*/
|
|
929
|
+
run = ({ prompt, system: systemTemplate, history = [], objects = [], blueprints = [], toolkit }) => Effect5.gen(this, function* () {
|
|
930
|
+
yield* this.begin({
|
|
931
|
+
prompt,
|
|
932
|
+
system: systemTemplate,
|
|
933
|
+
history,
|
|
934
|
+
objects,
|
|
935
|
+
blueprints
|
|
936
|
+
});
|
|
937
|
+
const system = yield* formatSystemPrompt({
|
|
938
|
+
system: systemTemplate,
|
|
939
|
+
blueprints,
|
|
940
|
+
objects
|
|
941
|
+
}).pipe(Effect5.orDie);
|
|
525
942
|
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
|
-
], {
|
|
943
|
+
const { done } = yield* this.runAgentTurn({
|
|
545
944
|
system,
|
|
546
|
-
|
|
945
|
+
toolkit
|
|
547
946
|
});
|
|
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) {
|
|
947
|
+
if (done) {
|
|
581
948
|
break;
|
|
582
|
-
} else if (!toolkit) {
|
|
583
|
-
throw new Error("No toolkit provided");
|
|
584
949
|
}
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
...context,
|
|
588
|
-
parentMessage: message.id,
|
|
589
|
-
toolCallId: block.toolCallId
|
|
590
|
-
}))));
|
|
950
|
+
yield* this.runTools({
|
|
951
|
+
toolkit
|
|
591
952
|
});
|
|
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
953
|
} while (true);
|
|
601
|
-
this._ended = Date.now();
|
|
602
954
|
log3("done", {
|
|
603
955
|
pending: this._pending.length,
|
|
604
956
|
duration: this.duration,
|
|
605
957
|
tools: this._toolCalls
|
|
606
|
-
}, {
|
|
607
|
-
F: __dxlog_file3,
|
|
608
|
-
L: 236,
|
|
609
|
-
S: this,
|
|
610
|
-
C: (f, a) => f(...a)
|
|
611
|
-
});
|
|
958
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 242, S: this });
|
|
612
959
|
return this._pending;
|
|
613
|
-
}).pipe(this._semaphore.withPermits(1),
|
|
960
|
+
}).pipe(this._semaphore.withPermits(1), Effect5.withSpan("AiRequest.run"));
|
|
614
961
|
};
|
|
615
962
|
var createSnippet = (text, len = 32) => text.length <= len * 2 ? text : [
|
|
616
963
|
text.slice(0, len),
|
|
@@ -620,27 +967,30 @@ var createSnippet = (text, len = 32) => text.length <= len * 2 ? text : [
|
|
|
620
967
|
|
|
621
968
|
// src/session/toolkit.ts
|
|
622
969
|
import * as Toolkit from "@effect/ai/Toolkit";
|
|
623
|
-
import * as
|
|
624
|
-
import
|
|
625
|
-
import {
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
const
|
|
629
|
-
const
|
|
630
|
-
const
|
|
631
|
-
|
|
970
|
+
import * as Effect6 from "effect/Effect";
|
|
971
|
+
import * as Layer2 from "effect/Layer";
|
|
972
|
+
import { OpaqueToolkit as OpaqueToolkit2, ToolExecutionService as ToolExecutionService2, ToolResolverService as ToolResolverService2 } from "@dxos/ai";
|
|
973
|
+
import { isTruthy } from "@dxos/util";
|
|
974
|
+
var createToolkit = ({ toolkit: toolkitProp, blueprints = [], opaqueToolkits = [] }) => Effect6.gen(function* () {
|
|
975
|
+
const blueprintToolkit = yield* ToolResolverService2.resolveToolkit(blueprints.flatMap(({ tools }) => tools));
|
|
976
|
+
const blueprintToolHandler = yield* blueprintToolkit.toContext(ToolExecutionService2.handlersFor(blueprintToolkit));
|
|
977
|
+
const opaqueToolkit = OpaqueToolkit2.merge(...opaqueToolkits);
|
|
978
|
+
const toolkitDefs = [
|
|
979
|
+
toolkitProp?.toolkit,
|
|
632
980
|
blueprintToolkit,
|
|
633
|
-
|
|
634
|
-
].filter(
|
|
635
|
-
|
|
981
|
+
opaqueToolkit.toolkit
|
|
982
|
+
].filter(isTruthy);
|
|
983
|
+
const mergedToolkit = Toolkit.merge(...toolkitDefs);
|
|
984
|
+
const combinedHandlerLayer = Layer2.mergeAll(Layer2.succeedContext(blueprintToolHandler), toolkitProp?.layer ?? OpaqueToolkit2.empty.layer, opaqueToolkit.layer);
|
|
985
|
+
return OpaqueToolkit2.make(mergedToolkit, combinedHandlerLayer);
|
|
636
986
|
});
|
|
637
987
|
|
|
638
988
|
// src/session/version-pin.ts
|
|
639
|
-
import * as
|
|
989
|
+
import * as Schema5 from "effect/Schema";
|
|
640
990
|
import { getVersion } from "@dxos/echo-db";
|
|
641
991
|
import { ObjectId } from "@dxos/keys";
|
|
642
|
-
var ObjectVersionSchema =
|
|
643
|
-
var VersionPinSchema =
|
|
992
|
+
var ObjectVersionSchema = Schema5.Unknown;
|
|
993
|
+
var VersionPinSchema = Schema5.Struct({
|
|
644
994
|
// TODO(dmaretskyi): Use Ref when those support encoding.
|
|
645
995
|
objectId: ObjectId,
|
|
646
996
|
// TODO(dmaretskyi): Could be opaque.
|
|
@@ -663,176 +1013,225 @@ var VersionPin = class extends VersionPinSchema {
|
|
|
663
1013
|
}
|
|
664
1014
|
};
|
|
665
1015
|
|
|
666
|
-
// src/conversation/
|
|
667
|
-
var
|
|
1016
|
+
// src/conversation/session.ts
|
|
1017
|
+
var __dxlog_file5 = "/__w/dxos/dxos/packages/core/compute/assistant/src/conversation/session.ts";
|
|
668
1018
|
var SUMMARY_THRESHOLD = 8e4;
|
|
669
|
-
var
|
|
1019
|
+
var AiSession = class extends Resource2 {
|
|
670
1020
|
/**
|
|
671
|
-
* Blueprints and objects bound to the
|
|
1021
|
+
* Blueprints and objects bound to the session.
|
|
672
1022
|
*/
|
|
673
1023
|
_binder;
|
|
674
|
-
|
|
675
|
-
|
|
1024
|
+
_feed;
|
|
1025
|
+
_runtime;
|
|
676
1026
|
constructor(options) {
|
|
677
1027
|
super();
|
|
678
|
-
this.
|
|
679
|
-
this.
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
L: 72,
|
|
683
|
-
S: this,
|
|
684
|
-
A: [
|
|
685
|
-
"this._queue",
|
|
686
|
-
""
|
|
687
|
-
]
|
|
688
|
-
});
|
|
1028
|
+
this._feed = options.feed;
|
|
1029
|
+
this._runtime = options.runtime;
|
|
1030
|
+
invariant2(this._feed, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 41, S: this, A: ["this._feed", ""] });
|
|
1031
|
+
invariant2(this._runtime, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 42, S: this, A: ["this._runtime", ""] });
|
|
689
1032
|
this._binder = new AiContextBinder({
|
|
690
|
-
|
|
1033
|
+
feed: this._feed,
|
|
1034
|
+
runtime: this._runtime,
|
|
691
1035
|
registry: options.registry
|
|
692
1036
|
});
|
|
693
1037
|
}
|
|
694
1038
|
async _open() {
|
|
695
1039
|
await this._binder.open(this._ctx);
|
|
696
1040
|
}
|
|
697
|
-
get
|
|
698
|
-
return this.
|
|
1041
|
+
get feed() {
|
|
1042
|
+
return this._feed;
|
|
699
1043
|
}
|
|
700
1044
|
get context() {
|
|
701
1045
|
return this._binder;
|
|
702
1046
|
}
|
|
703
|
-
get toolkit() {
|
|
704
|
-
return this._toolkit;
|
|
705
|
-
}
|
|
706
1047
|
async getHistory() {
|
|
707
|
-
const
|
|
708
|
-
|
|
1048
|
+
const queryResult = await Runtime2.runPromise(this._runtime)(Feed2.query(this._feed, Filter.type(Message3.Message)));
|
|
1049
|
+
const items = await queryResult.run();
|
|
1050
|
+
return items.filter(Obj6.instanceOf(Message3.Message));
|
|
709
1051
|
}
|
|
710
1052
|
getTools() {
|
|
711
|
-
return
|
|
712
|
-
const
|
|
713
|
-
|
|
714
|
-
toolkit: this._toolkit,
|
|
715
|
-
blueprints
|
|
1053
|
+
return Effect7.gen(this, function* () {
|
|
1054
|
+
const toolkit = yield* createToolkit({
|
|
1055
|
+
blueprints: this.context.getBlueprints()
|
|
716
1056
|
});
|
|
717
|
-
return
|
|
718
|
-
}).pipe(
|
|
1057
|
+
return toolkit.toolkit.tools;
|
|
1058
|
+
}).pipe(Effect7.orDie);
|
|
1059
|
+
}
|
|
1060
|
+
/**
|
|
1061
|
+
* Not provided by default, since users might want to override them.
|
|
1062
|
+
*/
|
|
1063
|
+
makeToolExecutionServices() {
|
|
1064
|
+
return ToolExecutionServices.pipe(Layer3.provide(Operation2.withInvocationOptions({
|
|
1065
|
+
conversation: Obj6.getDXN(this._feed).toString()
|
|
1066
|
+
})));
|
|
719
1067
|
}
|
|
720
1068
|
/**
|
|
721
1069
|
* Creates a new cancelable request effect.
|
|
722
1070
|
*/
|
|
723
1071
|
createRequest(params) {
|
|
724
|
-
return
|
|
725
|
-
const history = yield*
|
|
1072
|
+
return Effect7.gen(this, function* () {
|
|
1073
|
+
const history = yield* Effect7.promise(() => this.getHistory());
|
|
726
1074
|
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
1075
|
const objects = this.context.getObjects();
|
|
734
1076
|
log4("run", {
|
|
735
1077
|
history: history.length,
|
|
736
1078
|
blueprints: blueprints.length,
|
|
737
|
-
tools: Object.keys(toolkit.tools).length,
|
|
738
1079
|
objects: objects.length
|
|
739
|
-
}, {
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
summarizationThreshold: SUMMARY_THRESHOLD
|
|
1080
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 85, S: this });
|
|
1081
|
+
const request = new AiRequest({
|
|
1082
|
+
summarizationThreshold: SUMMARY_THRESHOLD,
|
|
1083
|
+
observer: params.observer,
|
|
1084
|
+
onOutput: (message) => Effect7.promise(() => Runtime2.runPromise(this._runtime)(Feed2.append(this._feed, [
|
|
1085
|
+
message
|
|
1086
|
+
])))
|
|
747
1087
|
});
|
|
748
|
-
|
|
1088
|
+
yield* request.begin({
|
|
749
1089
|
history,
|
|
750
1090
|
blueprints,
|
|
751
|
-
toolkit,
|
|
752
1091
|
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)
|
|
1092
|
+
prompt: params.prompt,
|
|
1093
|
+
system: params.system
|
|
769
1094
|
});
|
|
770
|
-
|
|
771
|
-
|
|
1095
|
+
do {
|
|
1096
|
+
yield* Effect7.promise(() => this.context.sync());
|
|
1097
|
+
const currentBlueprints = this.context.getBlueprints();
|
|
1098
|
+
const mcps = yield* connectMcpServers(currentBlueprints, params.mcpServers);
|
|
1099
|
+
const toolkit = yield* createToolkit({
|
|
1100
|
+
toolkit: params.toolkit,
|
|
1101
|
+
blueprints: currentBlueprints,
|
|
1102
|
+
opaqueToolkits: mcps
|
|
1103
|
+
});
|
|
1104
|
+
log4("toolkit", {
|
|
1105
|
+
tools: Record2.keys(toolkit.toolkit.tools)
|
|
1106
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 114, S: this });
|
|
1107
|
+
const system = yield* formatSystemPrompt({
|
|
1108
|
+
system: params.system,
|
|
1109
|
+
blueprints: currentBlueprints,
|
|
1110
|
+
objects: this.context.getObjects()
|
|
1111
|
+
}).pipe(Effect7.orDie);
|
|
1112
|
+
const { done } = yield* request.runAgentTurn({
|
|
1113
|
+
system,
|
|
1114
|
+
toolkit
|
|
1115
|
+
});
|
|
1116
|
+
if (done) {
|
|
1117
|
+
break;
|
|
1118
|
+
}
|
|
1119
|
+
yield* request.runTools({
|
|
1120
|
+
toolkit
|
|
1121
|
+
});
|
|
1122
|
+
} while (true);
|
|
1123
|
+
log4("result", {
|
|
1124
|
+
messages: request.pending.length,
|
|
1125
|
+
duration: request.duration,
|
|
1126
|
+
toolCalls: request.toolCalls
|
|
1127
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 133, S: this });
|
|
1128
|
+
return [
|
|
1129
|
+
...request.pending
|
|
1130
|
+
];
|
|
1131
|
+
}).pipe(Effect7.provide(Layer3.mergeAll(Layer3.succeed(AiContextService, {
|
|
1132
|
+
binder: this.context
|
|
1133
|
+
}), Layer3.succeed(AiSessionService, this), Operation2.withInvocationOptions({
|
|
1134
|
+
conversation: Obj6.getDXN(this._feed).toString()
|
|
1135
|
+
}))), Effect7.withSpan("AiSession.createRequest"));
|
|
772
1136
|
}
|
|
773
1137
|
};
|
|
774
|
-
var
|
|
1138
|
+
var AiSessionService = class _AiSessionService extends Context4.Tag("@dxos/assistant/AiSessionService")() {
|
|
775
1139
|
/**
|
|
776
|
-
* Create a new
|
|
1140
|
+
* Create a new session layer from options.
|
|
777
1141
|
*/
|
|
778
|
-
static layer = (options) =>
|
|
779
|
-
const
|
|
780
|
-
return
|
|
1142
|
+
static layer = (options) => aiContextFromSession.pipe(Layer3.provideMerge(Layer3.scoped(_AiSessionService, Effect7.gen(function* () {
|
|
1143
|
+
const session = yield* acquireReleaseResource(() => new AiSession(options));
|
|
1144
|
+
return session;
|
|
781
1145
|
}))));
|
|
782
1146
|
/**
|
|
783
|
-
* Create a new
|
|
1147
|
+
* Create a new session with a new feed.
|
|
784
1148
|
*/
|
|
785
|
-
static
|
|
786
|
-
|
|
1149
|
+
static layerNewFeed = (options) => Layer3.unwrapScoped(Effect7.gen(function* () {
|
|
1150
|
+
const feed = yield* Database2.add(Feed2.make());
|
|
1151
|
+
const runtime2 = yield* Effect7.runtime();
|
|
1152
|
+
return _AiSessionService.layer({
|
|
787
1153
|
...options,
|
|
788
|
-
|
|
1154
|
+
feed,
|
|
1155
|
+
runtime: runtime2
|
|
789
1156
|
});
|
|
790
1157
|
}));
|
|
791
1158
|
/**
|
|
792
|
-
* Run a prompt in the current
|
|
1159
|
+
* Run a prompt in the current session.
|
|
793
1160
|
*/
|
|
794
|
-
static run = (params) =>
|
|
795
|
-
const
|
|
796
|
-
return yield*
|
|
1161
|
+
static run = (params) => Effect7.gen(function* () {
|
|
1162
|
+
const session = yield* _AiSessionService;
|
|
1163
|
+
return yield* session.createRequest(params);
|
|
797
1164
|
});
|
|
798
1165
|
};
|
|
799
|
-
var
|
|
800
|
-
const
|
|
1166
|
+
var aiContextFromSession = Layer3.effect(AiContextService, Effect7.gen(function* () {
|
|
1167
|
+
const session = yield* AiSessionService;
|
|
801
1168
|
return {
|
|
802
|
-
binder:
|
|
1169
|
+
binder: session.context
|
|
803
1170
|
};
|
|
804
1171
|
}));
|
|
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", {
|
|
1172
|
+
var connectMcpServers = (blueprints, spaceMcpServers = []) => {
|
|
1173
|
+
const blueprintServers = pipe4(blueprints, Array4.flatMap((_) => _.mcpServers ?? []), Array4.map(({ url, protocol, apiKey }) => ({
|
|
811
1174
|
url,
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
1175
|
+
protocol,
|
|
1176
|
+
apiKey
|
|
1177
|
+
})));
|
|
1178
|
+
const spaceServers = spaceMcpServers.map(({ url, protocol, apiKey }) => ({
|
|
1179
|
+
url,
|
|
1180
|
+
protocol,
|
|
1181
|
+
apiKey
|
|
1182
|
+
}));
|
|
1183
|
+
const allServers = [
|
|
1184
|
+
...blueprintServers,
|
|
1185
|
+
...spaceServers
|
|
1186
|
+
];
|
|
1187
|
+
return pipe4(allServers, Effect7.forEach((options) => McpToolkit.make(options).pipe(
|
|
1188
|
+
// NOTE: Type-inference fails here without explicit void return.
|
|
1189
|
+
Effect7.tap((toolkit) => log4.info("Connected to MCP server", {
|
|
1190
|
+
url: options.url,
|
|
1191
|
+
tools: Object.keys(toolkit.toolkit.tools).length
|
|
1192
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 197, S: void 0 })),
|
|
1193
|
+
// Surface typed connection failures via ephemeral trace + warn log, then drop the server.
|
|
1194
|
+
Effect7.tapError((error) => Effect7.gen(function* () {
|
|
1195
|
+
log4.warn("Failed to connect to MCP server", {
|
|
1196
|
+
url: error.url,
|
|
1197
|
+
protocol: error.protocol,
|
|
1198
|
+
message: error.message
|
|
1199
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 202, S: this });
|
|
1200
|
+
yield* Trace3.write(McpServerError, {
|
|
1201
|
+
url: error.url,
|
|
1202
|
+
protocol: error.protocol,
|
|
1203
|
+
message: error.message
|
|
1204
|
+
});
|
|
1205
|
+
})),
|
|
1206
|
+
// Catch unexpected defects too (e.g. malformed tool schemas) so a single broken
|
|
1207
|
+
// server can never abort the whole turn — surface them through the same channel.
|
|
1208
|
+
Effect7.catchAllDefect((defect) => Effect7.gen(function* () {
|
|
1209
|
+
const message = defect instanceof Error ? defect.message : String(defect);
|
|
1210
|
+
log4.warn("Unexpected MCP defect", {
|
|
1211
|
+
url: options.url,
|
|
1212
|
+
message
|
|
1213
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file5, L: 216, S: this });
|
|
1214
|
+
yield* Trace3.write(McpServerError, {
|
|
1215
|
+
url: options.url,
|
|
1216
|
+
protocol: options.protocol,
|
|
1217
|
+
message: `Unexpected MCP failure: ${message}`
|
|
1218
|
+
});
|
|
1219
|
+
return yield* Effect7.fail(new McpToolkit.McpConnectionError({
|
|
1220
|
+
url: options.url,
|
|
1221
|
+
protocol: options.protocol,
|
|
1222
|
+
message
|
|
1223
|
+
}));
|
|
1224
|
+
})),
|
|
1225
|
+
Effect7.either
|
|
1226
|
+
)), Effect7.map(Array4.filterMap((_) => Either.getRight(_))));
|
|
1227
|
+
};
|
|
829
1228
|
|
|
830
1229
|
// src/diff.ts
|
|
831
1230
|
import { next as A } from "@automerge/automerge";
|
|
832
1231
|
import { DocAccessor, toCursorRange } from "@dxos/echo-db";
|
|
833
1232
|
import { log as log5 } from "@dxos/log";
|
|
834
|
-
import { isNonNullable } from "@dxos/util";
|
|
835
|
-
var
|
|
1233
|
+
import { isNonNullable as isNonNullable2 } from "@dxos/util";
|
|
1234
|
+
var __dxlog_file6 = "/__w/dxos/dxos/packages/core/compute/assistant/src/diff.ts";
|
|
836
1235
|
var reduceDiffs = (diffs) => {
|
|
837
1236
|
return diffs.reduce((acc, diff) => {
|
|
838
1237
|
const current = acc[acc.length - 1];
|
|
@@ -876,7 +1275,7 @@ var computeDiffsWithCursors = (accessor, diffs) => {
|
|
|
876
1275
|
text: diff.replace
|
|
877
1276
|
};
|
|
878
1277
|
}
|
|
879
|
-
}).filter(
|
|
1278
|
+
}).filter(isNonNullable2);
|
|
880
1279
|
};
|
|
881
1280
|
var applyDiffs = (accessor, diffs, { errorOnNotFound = false } = {}) => {
|
|
882
1281
|
for (const diff of reduceDiffs(diffs)) {
|
|
@@ -889,12 +1288,7 @@ var applyDiffs = (accessor, diffs, { errorOnNotFound = false } = {}) => {
|
|
|
889
1288
|
if (errorOnNotFound) {
|
|
890
1289
|
throw new Error(`Diff not found: ${diff.match}`);
|
|
891
1290
|
} 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
|
-
});
|
|
1291
|
+
log5.warn("diff not found", diff, { "~LogMeta": "~LogMeta", F: __dxlog_file6, L: 71, S: void 0 });
|
|
898
1292
|
}
|
|
899
1293
|
}
|
|
900
1294
|
});
|
|
@@ -904,11 +1298,11 @@ var applyDiffs = (accessor, diffs, { errorOnNotFound = false } = {}) => {
|
|
|
904
1298
|
|
|
905
1299
|
// src/execution-graph.ts
|
|
906
1300
|
import { AgentStatus } from "@dxos/ai";
|
|
907
|
-
import { Obj as
|
|
1301
|
+
import { Obj as Obj7 } from "@dxos/echo";
|
|
908
1302
|
import { MESSAGE_PROPERTY_TOOL_CALL_ID } from "@dxos/functions";
|
|
909
1303
|
import { LogLevel } from "@dxos/log";
|
|
910
|
-
import { ContentBlock, Message as Message4 } from "@dxos/types";
|
|
911
|
-
import { isNonNullable as
|
|
1304
|
+
import { ContentBlock as ContentBlock3, Message as Message4 } from "@dxos/types";
|
|
1305
|
+
import { isNonNullable as isNonNullable3 } from "@dxos/util";
|
|
912
1306
|
var SKIP_BLOCKS = [
|
|
913
1307
|
"text"
|
|
914
1308
|
];
|
|
@@ -931,9 +1325,9 @@ var ExecutionGraph = class {
|
|
|
931
1325
|
addEvents(events) {
|
|
932
1326
|
const sortedEvents = this.sortObjectsByCreated(events);
|
|
933
1327
|
for (const event of sortedEvents) {
|
|
934
|
-
if (
|
|
1328
|
+
if (Obj7.instanceOf(Message4.Message, event)) {
|
|
935
1329
|
this._processMessage(event);
|
|
936
|
-
} else if (
|
|
1330
|
+
} else if (Obj7.instanceOf(AgentStatus, event)) {
|
|
937
1331
|
this._processAgentStatus(event);
|
|
938
1332
|
}
|
|
939
1333
|
}
|
|
@@ -1035,11 +1429,17 @@ var ExecutionGraph = class {
|
|
|
1035
1429
|
*/
|
|
1036
1430
|
_updatePendingToolResults(toolCallId, agentStatusId) {
|
|
1037
1431
|
const toolResultId = this._pendingToolResults.get(toolCallId);
|
|
1038
|
-
if (!toolResultId)
|
|
1432
|
+
if (!toolResultId) {
|
|
1433
|
+
return;
|
|
1434
|
+
}
|
|
1039
1435
|
const toolResultCommit = this._commits.find((c) => c.id === toolResultId);
|
|
1040
|
-
if (!toolResultCommit)
|
|
1436
|
+
if (!toolResultCommit) {
|
|
1437
|
+
return;
|
|
1438
|
+
}
|
|
1041
1439
|
const toolCallCommitId = this._toolCallCommitIds.get(toolCallId);
|
|
1042
|
-
if (!toolCallCommitId)
|
|
1440
|
+
if (!toolCallCommitId) {
|
|
1441
|
+
return;
|
|
1442
|
+
}
|
|
1043
1443
|
const toolCallIndex = toolResultCommit.parents.indexOf(toolCallCommitId);
|
|
1044
1444
|
if (toolCallIndex !== -1) {
|
|
1045
1445
|
toolResultCommit.parents[toolCallIndex] = agentStatusId;
|
|
@@ -1051,7 +1451,9 @@ var ExecutionGraph = class {
|
|
|
1051
1451
|
*/
|
|
1052
1452
|
_handleLateToolResultUpdate(toolCallId, toolResultId) {
|
|
1053
1453
|
const toolCallCommitId = this._toolCallCommitIds.get(toolCallId);
|
|
1054
|
-
if (!toolCallCommitId)
|
|
1454
|
+
if (!toolCallCommitId) {
|
|
1455
|
+
return;
|
|
1456
|
+
}
|
|
1055
1457
|
const messageId = toolCallCommitId.split("_toolCall_")[0];
|
|
1056
1458
|
const toolCallBranch = `${messageId}_${toolCallId}`;
|
|
1057
1459
|
const agentStatusCommitId = this._lastCommitByBranch.get(toolCallBranch);
|
|
@@ -1120,7 +1522,7 @@ var messageToCommits = (message, lastBlockId, toolCallIds, lastCommitByBranch, s
|
|
|
1120
1522
|
previousBlockId = commit.id;
|
|
1121
1523
|
}
|
|
1122
1524
|
return commit;
|
|
1123
|
-
}).filter(
|
|
1525
|
+
}).filter(isNonNullable3);
|
|
1124
1526
|
};
|
|
1125
1527
|
var getBlockParents = (block, previousBlockId, message, toolCallIds, lastCommitByBranch) => {
|
|
1126
1528
|
const parents = [];
|
|
@@ -1154,7 +1556,9 @@ var createBlockCommit = (block, message, branch, parents, idx) => {
|
|
|
1154
1556
|
const timestamp = new Date(message.created);
|
|
1155
1557
|
switch (block._tag) {
|
|
1156
1558
|
case "text":
|
|
1157
|
-
if (!block.text.trim().length)
|
|
1559
|
+
if (!block.text.trim().length) {
|
|
1560
|
+
return null;
|
|
1561
|
+
}
|
|
1158
1562
|
return {
|
|
1159
1563
|
id: getGenericBlockId(message.id, idx),
|
|
1160
1564
|
branch,
|
|
@@ -1167,7 +1571,7 @@ var createBlockCommit = (block, message, branch, parents, idx) => {
|
|
|
1167
1571
|
],
|
|
1168
1572
|
message: "Processing request..."
|
|
1169
1573
|
} : {
|
|
1170
|
-
icon: "ph--
|
|
1574
|
+
icon: "ph--drone--regular",
|
|
1171
1575
|
message: `Response (${block.text.split(" ").length} words)`
|
|
1172
1576
|
}
|
|
1173
1577
|
};
|
|
@@ -1199,7 +1603,7 @@ var createBlockCommit = (block, message, branch, parents, idx) => {
|
|
|
1199
1603
|
timestamp,
|
|
1200
1604
|
icon: "ph--rocket--regular",
|
|
1201
1605
|
level: LogLevel.INFO,
|
|
1202
|
-
message:
|
|
1606
|
+
message: ContentBlock3.createStatsMessage(block)
|
|
1203
1607
|
};
|
|
1204
1608
|
case "status":
|
|
1205
1609
|
return {
|
|
@@ -1269,47 +1673,49 @@ var stringifyRef = (ref) => {
|
|
|
1269
1673
|
return ref.dxn.asEchoDXN()?.echoId ?? ref.dxn.asQueueDXN()?.objectId ?? "";
|
|
1270
1674
|
};
|
|
1271
1675
|
var stringifyObject = (obj) => {
|
|
1272
|
-
return
|
|
1676
|
+
return Obj7.getLabel(obj) ?? Obj7.getTypename(obj) ?? obj.id;
|
|
1273
1677
|
};
|
|
1274
1678
|
|
|
1275
1679
|
// src/templates/system.ts
|
|
1276
1680
|
import defaultsDeep from "lodash.defaultsdeep";
|
|
1277
|
-
import { Template as Template2 } from "@dxos/
|
|
1681
|
+
import { Template as Template2 } from "@dxos/compute";
|
|
1278
1682
|
|
|
1279
|
-
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/instructions/format.tpl?raw
|
|
1683
|
+
// raw-loader:/__w/dxos/dxos/packages/core/compute/assistant/src/templates/instructions/format.tpl?raw
|
|
1280
1684
|
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
1685
|
|
|
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##
|
|
1686
|
+
// raw-loader:/__w/dxos/dxos/packages/core/compute/assistant/src/templates/instructions/system.tpl?raw
|
|
1687
|
+
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
1688
|
|
|
1285
1689
|
// src/templates/system.ts
|
|
1286
1690
|
var createSystemPrompt = (options = {}) => Template2.process(format_default, defaultsDeep(options, {
|
|
1287
1691
|
DATETIME: (/* @__PURE__ */ new Date()).toISOString()
|
|
1288
1692
|
}));
|
|
1693
|
+
var SYSTEM_ID = "01K2J2FZWH39F55CS7B26GP1Q3";
|
|
1289
1694
|
var templates = {
|
|
1290
|
-
/** Editable system prompt.*/
|
|
1291
1695
|
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",
|
|
1696
|
+
id: SYSTEM_ID,
|
|
1296
1697
|
source: system_default
|
|
1297
1698
|
})
|
|
1298
1699
|
};
|
|
1299
1700
|
export {
|
|
1701
|
+
AgentRequestBegin,
|
|
1702
|
+
AgentRequestEnd,
|
|
1300
1703
|
AiAssistantError,
|
|
1301
1704
|
AiContextBinder,
|
|
1302
1705
|
AiContextService,
|
|
1303
|
-
AiConversation,
|
|
1304
|
-
AiConversationService,
|
|
1305
1706
|
AiModelError,
|
|
1707
|
+
AiRequest,
|
|
1306
1708
|
AiSession,
|
|
1709
|
+
AiSessionService,
|
|
1307
1710
|
ArtifactDiffResolver,
|
|
1308
1711
|
ArtifactId,
|
|
1309
1712
|
Bindings,
|
|
1713
|
+
CompleteBlock,
|
|
1310
1714
|
ContextBinding,
|
|
1311
1715
|
ExecutionGraph,
|
|
1312
1716
|
GenerationObserver,
|
|
1717
|
+
McpServerError,
|
|
1718
|
+
PartialBlock,
|
|
1313
1719
|
RefFromLLM,
|
|
1314
1720
|
ToolExecutionServices,
|
|
1315
1721
|
VersionPin,
|
|
@@ -1320,8 +1726,10 @@ export {
|
|
|
1320
1726
|
createToolkit,
|
|
1321
1727
|
formatSystemPrompt,
|
|
1322
1728
|
formatUserPrompt,
|
|
1323
|
-
|
|
1324
|
-
|
|
1729
|
+
getOperationFromTool,
|
|
1730
|
+
makeToolExecutionService,
|
|
1731
|
+
makeToolExecutionServiceFromOperationInvoker,
|
|
1732
|
+
makeToolResolverFromOperations,
|
|
1325
1733
|
reduceDiffs,
|
|
1326
1734
|
templates
|
|
1327
1735
|
};
|