@dxos/assistant 0.8.4-main.9be5663bfe → 0.8.4-main.abd8ff62ef
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 +4 -21
- package/dist/lib/neutral/extraction/index.mjs.map +3 -3
- package/dist/lib/neutral/index.mjs +1259 -79
- 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 +9 -12
- 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/named-entity-recognition.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction.d.ts +461 -455
- package/dist/types/src/extraction/extraction.d.ts.map +1 -1
- package/dist/types/src/extraction/quotes.d.ts.map +1 -1
- package/dist/types/src/functions/services.d.ts +7 -29
- package/dist/types/src/functions/services.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +0 -1
- 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/{session.d.ts → request.d.ts} +25 -33
- 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 -1
- package/dist/types/src/templates/system.d.ts.map +1 -1
- package/dist/types/src/tracing.d.ts +64 -95
- package/dist/types/src/tracing.d.ts.map +1 -1
- package/dist/types/src/types/artifact.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +21 -38
- package/src/conversation/context.ts +1 -1
- package/src/conversation/index.ts +1 -1
- package/src/conversation/session.ts +334 -0
- package/src/execution-graph.ts +15 -5
- package/src/extraction/extraction.ts +1 -1
- package/src/functions/services.ts +12 -127
- package/src/functions.conversations.json +1 -1
- package/src/index.ts +0 -1
- package/src/session/format.ts +14 -9
- package/src/session/index.ts +1 -1
- package/src/session/request.conversations.json +1 -0
- package/src/session/{session.ts → request.ts} +44 -51
- package/src/session/toolkit.ts +18 -15
- package/src/templates/instructions/system.tpl +11 -0
- package/src/templates/system.ts +1 -1
- package/src/tracing.ts +13 -69
- package/dist/lib/neutral/chunk-J5LGTIGS.mjs +0 -10
- package/dist/lib/neutral/chunk-J5LGTIGS.mjs.map +0 -7
- package/dist/lib/neutral/chunk-UAWIXKF6.mjs +0 -1875
- package/dist/lib/neutral/chunk-UAWIXKF6.mjs.map +0 -7
- package/dist/lib/neutral/testing/index.mjs +0 -114
- package/dist/lib/neutral/testing/index.mjs.map +0 -7
- package/dist/types/src/conversation/conversation.d.ts +0 -68
- 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/functions.test.d.ts +0 -2
- package/dist/types/src/functions.test.d.ts.map +0 -1
- package/dist/types/src/service/AgentService.d.ts +0 -61
- package/dist/types/src/service/AgentService.d.ts.map +0 -1
- package/dist/types/src/service/AgentService.test.d.ts +0 -2
- package/dist/types/src/service/AgentService.test.d.ts.map +0 -1
- package/dist/types/src/service/agent-process.d.ts +0 -17
- package/dist/types/src/service/agent-process.d.ts.map +0 -1
- package/dist/types/src/service/index.d.ts +0 -3
- package/dist/types/src/service/index.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.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 -35
- package/dist/types/src/testing/layer.d.ts.map +0 -1
- package/src/conversation/conversation.test.ts +0 -58
- package/src/conversation/conversation.ts +0 -275
- package/src/functions.test.ts +0 -82
- package/src/service/AgentService.conversations.json +0 -1
- package/src/service/AgentService.test.ts +0 -169
- package/src/service/AgentService.ts +0 -165
- package/src/service/agent-process.ts +0 -359
- package/src/service/index.ts +0 -6
- package/src/session/format.test.ts +0 -132
- package/src/session/session.conversations.json +0 -1
- package/src/session/session.test.ts +0 -190
- package/src/testing/index.ts +0 -5
- package/src/testing/layer.ts +0 -259
|
@@ -1,49 +1,1232 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
1
|
+
// src/conversation/context.ts
|
|
2
|
+
import { Atom, Registry } from "@effect-atom/atom-react";
|
|
3
|
+
import * as EArray from "effect/Array";
|
|
4
|
+
import * as Context from "effect/Context";
|
|
5
|
+
import * as Effect from "effect/Effect";
|
|
6
|
+
import * as Function from "effect/Function";
|
|
7
|
+
import * as Runtime from "effect/Runtime";
|
|
8
|
+
import * as Schema from "effect/Schema";
|
|
9
|
+
import { Blueprint } from "@dxos/compute";
|
|
10
|
+
import { Resource } from "@dxos/context";
|
|
11
|
+
import { DXN, Feed, Obj, Query, Ref, Type } from "@dxos/echo";
|
|
12
|
+
import { assertArgument } from "@dxos/invariant";
|
|
13
|
+
import { log } from "@dxos/log";
|
|
14
|
+
import { ComplexSet, isNonNullable } from "@dxos/util";
|
|
15
|
+
var __dxlog_file = "/__w/dxos/dxos/packages/core/assistant/src/conversation/context.ts";
|
|
16
|
+
var ContextBinding = Schema.Struct({
|
|
17
|
+
blueprints: Schema.Struct({
|
|
18
|
+
added: Schema.Array(Ref.Ref(Blueprint.Blueprint)),
|
|
19
|
+
removed: Schema.Array(Ref.Ref(Blueprint.Blueprint))
|
|
20
|
+
}),
|
|
21
|
+
objects: Schema.Struct({
|
|
22
|
+
added: Schema.Array(Ref.Ref(Obj.Unknown)),
|
|
23
|
+
removed: Schema.Array(Ref.Ref(Obj.Unknown))
|
|
24
|
+
})
|
|
25
|
+
}).pipe(Type.object({
|
|
26
|
+
typename: "org.dxos.type.contextBinding",
|
|
27
|
+
version: "0.1.0"
|
|
28
|
+
}));
|
|
29
|
+
var Bindings = class {
|
|
30
|
+
blueprints = new ComplexSet((ref) => ref.dxn.toString());
|
|
31
|
+
// TODO(burdon): Some DXNs have the Space prefix so only compare the object ID.
|
|
32
|
+
objects = new ComplexSet((ref) => ref.dxn.asEchoDXN()?.echoId);
|
|
33
|
+
toJSON() {
|
|
34
|
+
return {
|
|
35
|
+
blueprints: EArray.fromIterable(this.blueprints).map((ref) => ref.dxn.toString()),
|
|
36
|
+
objects: EArray.fromIterable(this.objects).map((ref) => ref.dxn.toString())
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var AiContextBinder = class extends Resource {
|
|
41
|
+
_blueprints = Atom.make([]).pipe(Atom.keepAlive);
|
|
42
|
+
_objects = Atom.make([]).pipe(Atom.keepAlive);
|
|
43
|
+
_registry;
|
|
44
|
+
_feed;
|
|
45
|
+
_runtime;
|
|
46
|
+
#bindingsQuery;
|
|
47
|
+
constructor(options) {
|
|
48
|
+
super();
|
|
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;
|
|
53
|
+
this._registry = options.registry ?? Registry.make();
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Returns the blueprints atom for subscription.
|
|
57
|
+
*/
|
|
58
|
+
get blueprints() {
|
|
59
|
+
return this._blueprints;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Returns the objects atom for subscription.
|
|
63
|
+
*/
|
|
64
|
+
get objects() {
|
|
65
|
+
return this._objects;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Gets the current blueprints value.
|
|
69
|
+
*/
|
|
70
|
+
getBlueprints() {
|
|
71
|
+
return this._registry.get(this._blueprints);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Gets the current objects value.
|
|
75
|
+
*/
|
|
76
|
+
getObjects() {
|
|
77
|
+
return this._registry.get(this._objects);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Subscribe to changes in blueprints.
|
|
81
|
+
*/
|
|
82
|
+
subscribeBlueprints(cb) {
|
|
83
|
+
return this._registry.subscribe(this._blueprints, () => cb(this._registry.get(this._blueprints)));
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Subscribe to changes in objects.
|
|
87
|
+
*/
|
|
88
|
+
subscribeObjects(cb) {
|
|
89
|
+
return this._registry.subscribe(this._objects, () => cb(this._registry.get(this._objects)));
|
|
90
|
+
}
|
|
91
|
+
async _open() {
|
|
92
|
+
this.#bindingsQuery = await Runtime.runPromise(this._runtime)(Feed.query(this._feed, Query.type(ContextBinding)));
|
|
93
|
+
const initialResults = await this.#bindingsQuery.run();
|
|
94
|
+
await this._updateBindings(initialResults);
|
|
95
|
+
this._ctx.onDispose(this.#bindingsQuery.subscribe(async () => {
|
|
96
|
+
await this._updateBindings(this.#bindingsQuery.results);
|
|
97
|
+
}));
|
|
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
|
+
}
|
|
115
|
+
async _updateBindings(items) {
|
|
116
|
+
if (items.length === 0) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
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 });
|
|
129
|
+
const currentBlueprints = this._registry.get(this._blueprints);
|
|
130
|
+
const currentObjects = this._registry.get(this._objects);
|
|
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);
|
|
149
|
+
log("updated bindings", {
|
|
150
|
+
blueprints: mergedBlueprints.length,
|
|
151
|
+
objects: mergedObjects.length
|
|
152
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 155, S: this });
|
|
153
|
+
}
|
|
154
|
+
async _close() {
|
|
155
|
+
this._registry.set(this._blueprints, []);
|
|
156
|
+
this._registry.set(this._objects, []);
|
|
157
|
+
}
|
|
158
|
+
async bind({ blueprints, objects }) {
|
|
159
|
+
const currentBlueprints = this._registry.get(this._blueprints);
|
|
160
|
+
const currentObjects = this._registry.get(this._objects);
|
|
161
|
+
const { added: addedBlueprints, next: nextBlueprints } = this._processBindings(blueprints, currentBlueprints);
|
|
162
|
+
const { added: addedObjects, next: nextObjects } = this._processBindings(objects, currentObjects);
|
|
163
|
+
if (!addedBlueprints.length && !addedObjects.length) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
this._registry.set(this._blueprints, nextBlueprints);
|
|
167
|
+
this._registry.set(this._objects, nextObjects);
|
|
168
|
+
log("bind", {
|
|
169
|
+
blueprints: addedBlueprints.length,
|
|
170
|
+
objects: addedObjects.length
|
|
171
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 176, S: this });
|
|
172
|
+
await Runtime.runPromise(this._runtime)(Feed.append(this._feed, [
|
|
173
|
+
Obj.make(ContextBinding, {
|
|
174
|
+
blueprints: {
|
|
175
|
+
added: addedBlueprints,
|
|
176
|
+
removed: []
|
|
177
|
+
},
|
|
178
|
+
objects: {
|
|
179
|
+
added: addedObjects,
|
|
180
|
+
removed: []
|
|
181
|
+
}
|
|
182
|
+
})
|
|
183
|
+
]));
|
|
184
|
+
}
|
|
185
|
+
async unbind({ blueprints, objects }) {
|
|
186
|
+
if (!blueprints?.length && !objects?.length) {
|
|
187
|
+
return;
|
|
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
|
+
}
|
|
199
|
+
log("unbind", {
|
|
200
|
+
blueprints: blueprints?.length,
|
|
201
|
+
objects: objects?.length
|
|
202
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 208, S: this });
|
|
203
|
+
await Runtime.runPromise(this._runtime)(Feed.append(this._feed, [
|
|
204
|
+
Obj.make(ContextBinding, {
|
|
205
|
+
blueprints: {
|
|
206
|
+
added: [],
|
|
207
|
+
removed: blueprints ?? []
|
|
208
|
+
},
|
|
209
|
+
objects: {
|
|
210
|
+
added: [],
|
|
211
|
+
removed: objects ?? []
|
|
212
|
+
}
|
|
213
|
+
})
|
|
214
|
+
]));
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Process bindings to filter duplicates and determine next state.
|
|
218
|
+
*/
|
|
219
|
+
_processBindings(refs, current) {
|
|
220
|
+
const next = [
|
|
221
|
+
...current
|
|
222
|
+
];
|
|
223
|
+
const added = [];
|
|
224
|
+
if (!refs?.length) {
|
|
225
|
+
return {
|
|
226
|
+
added,
|
|
227
|
+
next
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
const seen = new Set(current.map((obj) => Obj.getDXN(obj).toString()));
|
|
231
|
+
for (const ref of refs) {
|
|
232
|
+
const dxn = ref.dxn.toString();
|
|
233
|
+
if (!seen.has(dxn)) {
|
|
234
|
+
seen.add(dxn);
|
|
235
|
+
added.push(ref);
|
|
236
|
+
if (ref.isAvailable) {
|
|
237
|
+
const target = ref.target;
|
|
238
|
+
if (target) {
|
|
239
|
+
next.push(target);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return {
|
|
245
|
+
added,
|
|
246
|
+
next
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Reduce results into sets of blueprints and objects.
|
|
251
|
+
*/
|
|
252
|
+
_reduce(items) {
|
|
253
|
+
return Function.pipe(items, EArray.reduce(new Bindings(), (context, { blueprints, objects }) => {
|
|
254
|
+
blueprints.added.forEach((ref) => context.blueprints.add(ref));
|
|
255
|
+
blueprints.removed.forEach((ref) => context.blueprints.delete(ref));
|
|
256
|
+
objects.added.forEach((ref) => context.objects.add(ref));
|
|
257
|
+
objects.removed.forEach((ref) => {
|
|
258
|
+
for (const obj of context.objects) {
|
|
259
|
+
if (DXN.equalsEchoId(obj.dxn, ref.dxn)) {
|
|
260
|
+
context.objects.delete(obj);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
return context;
|
|
265
|
+
}));
|
|
266
|
+
}
|
|
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
|
+
/**
|
|
285
|
+
* Resolve references to objects, loading them first if needed and falling back to existing objects.
|
|
286
|
+
*/
|
|
287
|
+
async _resolve(refs, current) {
|
|
288
|
+
const refArray = [
|
|
289
|
+
...refs
|
|
290
|
+
];
|
|
291
|
+
await Promise.all(refArray.map((ref) => ref.tryLoad()));
|
|
292
|
+
return refArray.map((ref) => {
|
|
293
|
+
let target;
|
|
294
|
+
if (ref.isAvailable) {
|
|
295
|
+
target = ref.target;
|
|
296
|
+
}
|
|
297
|
+
return target ?? current.find((obj) => Obj.getDXN(obj).toString() === ref.dxn.toString());
|
|
298
|
+
}).filter(isNonNullable);
|
|
299
|
+
}
|
|
300
|
+
};
|
|
301
|
+
var AiContextService = class _AiContextService extends Context.Tag("@dxos/assistant/AiContextService")() {
|
|
302
|
+
static bindContext = ({ blueprints, objects }) => Effect.gen(function* () {
|
|
303
|
+
const { binder } = yield* _AiContextService;
|
|
304
|
+
yield* Effect.promise(() => binder.bind({
|
|
305
|
+
blueprints,
|
|
306
|
+
objects
|
|
307
|
+
}));
|
|
308
|
+
});
|
|
309
|
+
static findObjects = (type) => {
|
|
310
|
+
return Effect.gen(function* () {
|
|
311
|
+
const { binder } = yield* _AiContextService;
|
|
312
|
+
return binder.getObjects().filter(Obj.instanceOf(type));
|
|
313
|
+
});
|
|
314
|
+
};
|
|
315
|
+
};
|
|
316
|
+
|
|
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";
|
|
321
|
+
import * as Either from "effect/Either";
|
|
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";
|
|
327
|
+
import { Resource as Resource2 } from "@dxos/context";
|
|
328
|
+
import { Database as Database2, Feed as Feed2, Filter, Obj as Obj6 } from "@dxos/echo";
|
|
329
|
+
import { acquireReleaseResource } from "@dxos/effect";
|
|
330
|
+
import { invariant as invariant2 } from "@dxos/invariant";
|
|
331
|
+
import { log as log4 } from "@dxos/log";
|
|
332
|
+
import { McpToolkit } from "@dxos/mcp-client";
|
|
333
|
+
import { Message as Message3 } from "@dxos/types";
|
|
334
|
+
|
|
335
|
+
// src/functions/services.ts
|
|
336
|
+
import * as Tool from "@effect/ai/Tool";
|
|
337
|
+
import * as Context2 from "effect/Context";
|
|
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/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")() {
|
|
521
|
+
};
|
|
522
|
+
|
|
523
|
+
// src/session/observer.ts
|
|
524
|
+
import * as Effect3 from "effect/Effect";
|
|
525
|
+
import * as Function2 from "effect/Function";
|
|
526
|
+
var GenerationObserver = Object.freeze({
|
|
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) } = {}) => ({
|
|
528
|
+
onBegin,
|
|
529
|
+
onPart,
|
|
530
|
+
onBlock,
|
|
531
|
+
onEnd,
|
|
532
|
+
onMessage
|
|
533
|
+
}),
|
|
534
|
+
noop: () => GenerationObserver.make(),
|
|
535
|
+
/**
|
|
536
|
+
* Debug printer to be used in unit-tests and browser devtools.
|
|
537
|
+
*/
|
|
538
|
+
fromPrinter: (printer) => GenerationObserver.make({
|
|
539
|
+
onBlock: (block) => Effect3.sync(() => {
|
|
540
|
+
if (block.pending) {
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
printer.printContentBlock(block);
|
|
544
|
+
}),
|
|
545
|
+
onMessage: (message) => Effect3.sync(() => {
|
|
546
|
+
if (message.sender.role === "assistant") {
|
|
547
|
+
return;
|
|
548
|
+
}
|
|
549
|
+
printer.printMessage(message);
|
|
550
|
+
})
|
|
551
|
+
})
|
|
552
|
+
});
|
|
553
|
+
|
|
554
|
+
// src/session/request.ts
|
|
555
|
+
import * as LanguageModel from "@effect/ai/LanguageModel";
|
|
556
|
+
import * as Array3 from "effect/Array";
|
|
557
|
+
import * as Chunk from "effect/Chunk";
|
|
558
|
+
import * as Effect5 from "effect/Effect";
|
|
559
|
+
import { pipe as pipe3 } from "effect/Function";
|
|
560
|
+
import * as Option3 from "effect/Option";
|
|
561
|
+
import * as Stream from "effect/Stream";
|
|
562
|
+
import { AiParser, AiPreprocessor, AiSummarizer, callTool, withoutToolCallParising } from "@dxos/ai";
|
|
563
|
+
import { Trace as Trace2 } from "@dxos/compute";
|
|
564
|
+
import { Obj as Obj5 } from "@dxos/echo";
|
|
565
|
+
import { log as log3 } from "@dxos/log";
|
|
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
|
+
});
|
|
605
|
+
|
|
606
|
+
// src/session/format.ts
|
|
607
|
+
import * as Effect4 from "effect/Effect";
|
|
608
|
+
import * as Function3 from "effect/Function";
|
|
609
|
+
import * as Option2 from "effect/Option";
|
|
610
|
+
import { Template } from "@dxos/compute";
|
|
611
|
+
import { Obj as Obj4 } from "@dxos/echo";
|
|
612
|
+
import { ObjectVersion } from "@dxos/echo-db";
|
|
613
|
+
import { log as log2 } from "@dxos/log";
|
|
614
|
+
import { Message } from "@dxos/types";
|
|
615
|
+
import { trim as trim2 } from "@dxos/util";
|
|
616
|
+
|
|
617
|
+
// src/errors.ts
|
|
618
|
+
import { BaseError } from "@dxos/errors";
|
|
619
|
+
var AiModelError = class extends BaseError.extend("AiModelError", "AI model error") {
|
|
620
|
+
};
|
|
621
|
+
var AiAssistantError = class extends BaseError.extend("AiAssistantError", "AI assistant error") {
|
|
622
|
+
};
|
|
623
|
+
|
|
624
|
+
// src/session/format.ts
|
|
625
|
+
var __dxlog_file3 = "/__w/dxos/dxos/packages/core/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`
|
|
629
|
+
<blueprint>
|
|
630
|
+
${yield* Template.processTemplate(template)}
|
|
631
|
+
</blueprint>
|
|
632
|
+
`;
|
|
633
|
+
}))), Effect4.map((blueprints2) => blueprints2.length > 0 ? [
|
|
634
|
+
"## Blueprints Definitions",
|
|
635
|
+
...blueprints2
|
|
636
|
+
].join("\n\n") : void 0));
|
|
637
|
+
const objectDefs = yield* Function3.pipe(objects, Effect4.forEach((object) => Effect4.succeed(trim2`
|
|
638
|
+
<object>
|
|
639
|
+
<dxn>${Obj4.getDXN(object)}</dxn>
|
|
640
|
+
<typename>${Obj4.getTypename(object)}</typename>
|
|
641
|
+
</object>
|
|
642
|
+
`)), Effect4.map((objects2) => objects2.length > 0 ? [
|
|
643
|
+
"## Context Objects",
|
|
644
|
+
...objects2
|
|
645
|
+
].join("\n\n") : void 0));
|
|
646
|
+
return yield* Function3.pipe(Effect4.succeed([
|
|
647
|
+
system,
|
|
648
|
+
blueprintDefs,
|
|
649
|
+
objectDefs
|
|
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* () {
|
|
653
|
+
const blocks = [];
|
|
654
|
+
const artifactDiffResolver = yield* Effect4.serviceOption(ArtifactDiffResolver);
|
|
655
|
+
if (Option2.isSome(artifactDiffResolver)) {
|
|
656
|
+
const versions = gatherObjectVersions(history);
|
|
657
|
+
const artifactDiff = yield* Effect4.tryPromise({
|
|
658
|
+
try: () => artifactDiffResolver.value.resolve([
|
|
659
|
+
...versions.entries()
|
|
660
|
+
].map(([id, version]) => ({
|
|
661
|
+
id,
|
|
662
|
+
lastVersion: version
|
|
663
|
+
}))),
|
|
664
|
+
catch: AiAssistantError.wrap({
|
|
665
|
+
message: "Artifact diff resolution error"
|
|
666
|
+
})
|
|
667
|
+
});
|
|
668
|
+
log2("version", {
|
|
669
|
+
artifactDiff,
|
|
670
|
+
versions
|
|
671
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 65, S: this });
|
|
672
|
+
for (const [id, { version }] of [
|
|
673
|
+
...artifactDiff.entries()
|
|
674
|
+
]) {
|
|
675
|
+
if (ObjectVersion.equals(version, versions.get(id))) {
|
|
676
|
+
artifactDiff.delete(id);
|
|
677
|
+
continue;
|
|
678
|
+
}
|
|
679
|
+
blocks.push({
|
|
680
|
+
_tag: "anchor",
|
|
681
|
+
objectId: id,
|
|
682
|
+
version
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
if (artifactDiff.size > 0) {
|
|
686
|
+
blocks.push(createArtifactUpdateBlock(artifactDiff));
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
return Obj4.make(Message.Message, {
|
|
690
|
+
created: (/* @__PURE__ */ new Date()).toISOString(),
|
|
691
|
+
sender: {
|
|
692
|
+
role: "user"
|
|
693
|
+
},
|
|
694
|
+
blocks: typeof prompt === "string" ? [
|
|
695
|
+
...blocks,
|
|
696
|
+
{
|
|
697
|
+
_tag: "text",
|
|
698
|
+
text: prompt
|
|
699
|
+
}
|
|
700
|
+
] : [
|
|
701
|
+
...blocks,
|
|
702
|
+
...prompt
|
|
703
|
+
]
|
|
704
|
+
});
|
|
705
|
+
}).pipe(Effect4.withSpan("formatUserPrompt"));
|
|
706
|
+
var gatherObjectVersions = (messages) => {
|
|
707
|
+
const artifactIds = /* @__PURE__ */ new Map();
|
|
708
|
+
for (const message of messages) {
|
|
709
|
+
for (const block of message.blocks) {
|
|
710
|
+
if (block._tag === "anchor") {
|
|
711
|
+
artifactIds.set(block.objectId, block.version);
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
return artifactIds;
|
|
716
|
+
};
|
|
717
|
+
var createArtifactUpdateBlock = (artifactDiff) => {
|
|
718
|
+
return {
|
|
719
|
+
_tag: "text",
|
|
720
|
+
// TODO(dmaretskyi): Does this need to be a special content-block?
|
|
721
|
+
disposition: "synthetic",
|
|
722
|
+
text: trim2`
|
|
723
|
+
The following artifacts have been updated since the last message:
|
|
724
|
+
${[
|
|
725
|
+
...artifactDiff.entries()
|
|
726
|
+
].map(([id, { diff }]) => `<changed-artifact id="${id}">${diff ? `
|
|
727
|
+
${diff}` : ""}</changed-artifact>`).join("\n")}
|
|
728
|
+
`
|
|
729
|
+
};
|
|
730
|
+
};
|
|
731
|
+
|
|
732
|
+
// src/session/request.ts
|
|
733
|
+
var __dxlog_file4 = "/__w/dxos/dxos/packages/core/assistant/src/session/request.ts";
|
|
734
|
+
var AiRequest = class {
|
|
735
|
+
_options;
|
|
736
|
+
/** Prevents concurrent execution of session. */
|
|
737
|
+
_semaphore = Effect5.runSync(Effect5.makeSemaphore(1));
|
|
738
|
+
_observer;
|
|
739
|
+
_onOutput;
|
|
740
|
+
/** Message history from queue. */
|
|
741
|
+
// TODO(burdon): Evolve into supporting a git-like graph of messages.
|
|
742
|
+
_history = [];
|
|
743
|
+
/** Pending messages for this session (incl. the current prompt). */
|
|
744
|
+
_pending = [];
|
|
745
|
+
_started = 0;
|
|
746
|
+
_ended = 0;
|
|
747
|
+
_toolCalls = 0;
|
|
748
|
+
constructor(_options = {}) {
|
|
749
|
+
this._options = _options;
|
|
750
|
+
this._observer = _options.observer ?? GenerationObserver.noop();
|
|
751
|
+
this._onOutput = _options.onOutput ?? (() => Effect5.void);
|
|
752
|
+
}
|
|
753
|
+
get duration() {
|
|
754
|
+
return this._ended - this._started;
|
|
755
|
+
}
|
|
756
|
+
get toolCalls() {
|
|
757
|
+
return this._toolCalls;
|
|
758
|
+
}
|
|
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* () {
|
|
787
|
+
this._started = Date.now();
|
|
788
|
+
this._history = [
|
|
789
|
+
...history
|
|
790
|
+
];
|
|
791
|
+
this._pending = [];
|
|
792
|
+
const systemPrompt = yield* formatSystemPrompt({
|
|
793
|
+
system,
|
|
794
|
+
blueprints,
|
|
795
|
+
objects
|
|
796
|
+
}).pipe(Effect5.orDie);
|
|
797
|
+
if (this._options.summarizationThreshold !== void 0) {
|
|
798
|
+
const tokenCount = yield* AiPreprocessor.estimateTokens(yield* AiPreprocessor.preprocessPrompt([
|
|
799
|
+
...this._history
|
|
800
|
+
], {
|
|
801
|
+
system: systemPrompt
|
|
802
|
+
}));
|
|
803
|
+
if (tokenCount > this._options.summarizationThreshold) {
|
|
804
|
+
const summary = yield* AiSummarizer.summarize([
|
|
805
|
+
...this._history
|
|
806
|
+
]);
|
|
807
|
+
yield* this._submitMessage(summary);
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
yield* this._submitMessage(yield* formatUserPrompt({
|
|
811
|
+
prompt,
|
|
812
|
+
history
|
|
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);
|
|
937
|
+
do {
|
|
938
|
+
const { done } = yield* this.runAgentTurn({
|
|
939
|
+
system,
|
|
940
|
+
toolkit
|
|
941
|
+
});
|
|
942
|
+
if (done) {
|
|
943
|
+
break;
|
|
944
|
+
}
|
|
945
|
+
yield* this.runTools({
|
|
946
|
+
toolkit
|
|
947
|
+
});
|
|
948
|
+
} while (true);
|
|
949
|
+
log3("done", {
|
|
950
|
+
pending: this._pending.length,
|
|
951
|
+
duration: this.duration,
|
|
952
|
+
tools: this._toolCalls
|
|
953
|
+
}, { "~LogMeta": "~LogMeta", F: __dxlog_file4, L: 237, S: this });
|
|
954
|
+
return this._pending;
|
|
955
|
+
}).pipe(this._semaphore.withPermits(1), Effect5.withSpan("AiRequest.run"));
|
|
956
|
+
};
|
|
957
|
+
var createSnippet = (text, len = 32) => text.length <= len * 2 ? text : [
|
|
958
|
+
text.slice(0, len),
|
|
959
|
+
"...",
|
|
960
|
+
text.slice(-len)
|
|
961
|
+
].join("");
|
|
962
|
+
|
|
963
|
+
// src/session/toolkit.ts
|
|
964
|
+
import * as Toolkit from "@effect/ai/Toolkit";
|
|
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,
|
|
975
|
+
blueprintToolkit,
|
|
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);
|
|
981
|
+
});
|
|
982
|
+
|
|
983
|
+
// src/session/version-pin.ts
|
|
984
|
+
import * as Schema5 from "effect/Schema";
|
|
985
|
+
import { getVersion } from "@dxos/echo-db";
|
|
986
|
+
import { ObjectId } from "@dxos/keys";
|
|
987
|
+
var ObjectVersionSchema = Schema5.Unknown;
|
|
988
|
+
var VersionPinSchema = Schema5.Struct({
|
|
989
|
+
// TODO(dmaretskyi): Use Ref when those support encoding.
|
|
990
|
+
objectId: ObjectId,
|
|
991
|
+
// TODO(dmaretskyi): Could be opaque.
|
|
992
|
+
version: ObjectVersionSchema
|
|
993
|
+
});
|
|
994
|
+
var VersionPin = class extends VersionPinSchema {
|
|
995
|
+
static DISPOSITION = "version-pin";
|
|
996
|
+
static fromObject(object) {
|
|
997
|
+
return VersionPin.make({
|
|
998
|
+
objectId: object.id,
|
|
999
|
+
version: getVersion(object)
|
|
1000
|
+
});
|
|
1001
|
+
}
|
|
1002
|
+
static createBlock(pin) {
|
|
1003
|
+
return {
|
|
1004
|
+
_tag: "json",
|
|
1005
|
+
disposition: VersionPin.DISPOSITION,
|
|
1006
|
+
data: JSON.stringify(pin)
|
|
1007
|
+
};
|
|
1008
|
+
}
|
|
1009
|
+
};
|
|
1010
|
+
|
|
1011
|
+
// src/conversation/session.ts
|
|
1012
|
+
var __dxlog_file5 = "/__w/dxos/dxos/packages/core/assistant/src/conversation/session.ts";
|
|
1013
|
+
var SUMMARY_THRESHOLD = 8e4;
|
|
1014
|
+
var AiSession = class extends Resource2 {
|
|
1015
|
+
/**
|
|
1016
|
+
* Blueprints and objects bound to the session.
|
|
1017
|
+
*/
|
|
1018
|
+
_binder;
|
|
1019
|
+
_feed;
|
|
1020
|
+
_runtime;
|
|
1021
|
+
constructor(options) {
|
|
1022
|
+
super();
|
|
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", ""] });
|
|
1027
|
+
this._binder = new AiContextBinder({
|
|
1028
|
+
feed: this._feed,
|
|
1029
|
+
runtime: this._runtime,
|
|
1030
|
+
registry: options.registry
|
|
1031
|
+
});
|
|
1032
|
+
}
|
|
1033
|
+
async _open() {
|
|
1034
|
+
await this._binder.open(this._ctx);
|
|
1035
|
+
}
|
|
1036
|
+
get feed() {
|
|
1037
|
+
return this._feed;
|
|
1038
|
+
}
|
|
1039
|
+
get context() {
|
|
1040
|
+
return this._binder;
|
|
1041
|
+
}
|
|
1042
|
+
async getHistory() {
|
|
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));
|
|
1046
|
+
}
|
|
1047
|
+
getTools() {
|
|
1048
|
+
return Effect7.gen(this, function* () {
|
|
1049
|
+
const toolkit = yield* createToolkit({
|
|
1050
|
+
blueprints: this.context.getBlueprints()
|
|
1051
|
+
});
|
|
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
|
+
})));
|
|
1062
|
+
}
|
|
1063
|
+
/**
|
|
1064
|
+
* Creates a new cancelable request effect.
|
|
1065
|
+
*/
|
|
1066
|
+
createRequest(params) {
|
|
1067
|
+
return Effect7.gen(this, function* () {
|
|
1068
|
+
const history = yield* Effect7.promise(() => this.getHistory());
|
|
1069
|
+
const blueprints = this.context.getBlueprints();
|
|
1070
|
+
const objects = this.context.getObjects();
|
|
1071
|
+
log4("run", {
|
|
1072
|
+
history: history.length,
|
|
1073
|
+
blueprints: blueprints.length,
|
|
1074
|
+
objects: objects.length
|
|
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
|
+
])))
|
|
1082
|
+
});
|
|
1083
|
+
yield* request.begin({
|
|
1084
|
+
history,
|
|
1085
|
+
blueprints,
|
|
1086
|
+
objects,
|
|
1087
|
+
prompt: params.prompt,
|
|
1088
|
+
system: params.system
|
|
1089
|
+
});
|
|
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"));
|
|
1131
|
+
}
|
|
1132
|
+
};
|
|
1133
|
+
var AiSessionService = class _AiSessionService extends Context4.Tag("@dxos/assistant/AiSessionService")() {
|
|
1134
|
+
/**
|
|
1135
|
+
* Create a new session layer from options.
|
|
1136
|
+
*/
|
|
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;
|
|
1140
|
+
}))));
|
|
1141
|
+
/**
|
|
1142
|
+
* Create a new session with a new feed.
|
|
1143
|
+
*/
|
|
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({
|
|
1149
|
+
...options,
|
|
1150
|
+
feed,
|
|
1151
|
+
runtime: runtime2
|
|
1152
|
+
});
|
|
1153
|
+
}));
|
|
1154
|
+
/**
|
|
1155
|
+
* Run a prompt in the current session.
|
|
1156
|
+
*/
|
|
1157
|
+
static run = (params) => Effect7.gen(function* () {
|
|
1158
|
+
const session = yield* _AiSessionService;
|
|
1159
|
+
return yield* session.createRequest(params);
|
|
1160
|
+
});
|
|
1161
|
+
};
|
|
1162
|
+
var aiContextFromSession = Layer3.effect(AiContextService, Effect7.gen(function* () {
|
|
1163
|
+
const session = yield* AiSessionService;
|
|
1164
|
+
return {
|
|
1165
|
+
binder: session.context
|
|
1166
|
+
};
|
|
1167
|
+
}));
|
|
1168
|
+
var connectMcpServers = (blueprints, spaceMcpServers = []) => {
|
|
1169
|
+
const blueprintServers = pipe4(blueprints, Array4.flatMap((_) => _.mcpServers ?? []), Array4.map(({ url, protocol }) => ({
|
|
1170
|
+
url,
|
|
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
|
+
};
|
|
40
1223
|
|
|
41
1224
|
// src/diff.ts
|
|
42
1225
|
import { next as A } from "@automerge/automerge";
|
|
43
1226
|
import { DocAccessor, toCursorRange } from "@dxos/echo-db";
|
|
44
|
-
import { log } from "@dxos/log";
|
|
45
|
-
import { isNonNullable } from "@dxos/util";
|
|
46
|
-
var
|
|
1227
|
+
import { log as log5 } from "@dxos/log";
|
|
1228
|
+
import { isNonNullable as isNonNullable2 } from "@dxos/util";
|
|
1229
|
+
var __dxlog_file6 = "/__w/dxos/dxos/packages/core/assistant/src/diff.ts";
|
|
47
1230
|
var reduceDiffs = (diffs) => {
|
|
48
1231
|
return diffs.reduce((acc, diff) => {
|
|
49
1232
|
const current = acc[acc.length - 1];
|
|
@@ -87,7 +1270,7 @@ var computeDiffsWithCursors = (accessor, diffs) => {
|
|
|
87
1270
|
text: diff.replace
|
|
88
1271
|
};
|
|
89
1272
|
}
|
|
90
|
-
}).filter(
|
|
1273
|
+
}).filter(isNonNullable2);
|
|
91
1274
|
};
|
|
92
1275
|
var applyDiffs = (accessor, diffs, { errorOnNotFound = false } = {}) => {
|
|
93
1276
|
for (const diff of reduceDiffs(diffs)) {
|
|
@@ -100,12 +1283,7 @@ var applyDiffs = (accessor, diffs, { errorOnNotFound = false } = {}) => {
|
|
|
100
1283
|
if (errorOnNotFound) {
|
|
101
1284
|
throw new Error(`Diff not found: ${diff.match}`);
|
|
102
1285
|
} else {
|
|
103
|
-
|
|
104
|
-
F: __dxlog_file,
|
|
105
|
-
L: 86,
|
|
106
|
-
S: void 0,
|
|
107
|
-
C: (f, a) => f(...a)
|
|
108
|
-
});
|
|
1286
|
+
log5.warn("diff not found", diff, { "~LogMeta": "~LogMeta", F: __dxlog_file6, L: 71, S: void 0 });
|
|
109
1287
|
}
|
|
110
1288
|
}
|
|
111
1289
|
});
|
|
@@ -115,11 +1293,11 @@ var applyDiffs = (accessor, diffs, { errorOnNotFound = false } = {}) => {
|
|
|
115
1293
|
|
|
116
1294
|
// src/execution-graph.ts
|
|
117
1295
|
import { AgentStatus } from "@dxos/ai";
|
|
118
|
-
import { Obj } from "@dxos/echo";
|
|
1296
|
+
import { Obj as Obj7 } from "@dxos/echo";
|
|
119
1297
|
import { MESSAGE_PROPERTY_TOOL_CALL_ID } from "@dxos/functions";
|
|
120
1298
|
import { LogLevel } from "@dxos/log";
|
|
121
|
-
import { ContentBlock, Message } from "@dxos/types";
|
|
122
|
-
import { isNonNullable as
|
|
1299
|
+
import { ContentBlock as ContentBlock3, Message as Message4 } from "@dxos/types";
|
|
1300
|
+
import { isNonNullable as isNonNullable3 } from "@dxos/util";
|
|
123
1301
|
var SKIP_BLOCKS = [
|
|
124
1302
|
"text"
|
|
125
1303
|
];
|
|
@@ -142,9 +1320,9 @@ var ExecutionGraph = class {
|
|
|
142
1320
|
addEvents(events) {
|
|
143
1321
|
const sortedEvents = this.sortObjectsByCreated(events);
|
|
144
1322
|
for (const event of sortedEvents) {
|
|
145
|
-
if (
|
|
1323
|
+
if (Obj7.instanceOf(Message4.Message, event)) {
|
|
146
1324
|
this._processMessage(event);
|
|
147
|
-
} else if (
|
|
1325
|
+
} else if (Obj7.instanceOf(AgentStatus, event)) {
|
|
148
1326
|
this._processAgentStatus(event);
|
|
149
1327
|
}
|
|
150
1328
|
}
|
|
@@ -246,11 +1424,17 @@ var ExecutionGraph = class {
|
|
|
246
1424
|
*/
|
|
247
1425
|
_updatePendingToolResults(toolCallId, agentStatusId) {
|
|
248
1426
|
const toolResultId = this._pendingToolResults.get(toolCallId);
|
|
249
|
-
if (!toolResultId)
|
|
1427
|
+
if (!toolResultId) {
|
|
1428
|
+
return;
|
|
1429
|
+
}
|
|
250
1430
|
const toolResultCommit = this._commits.find((c) => c.id === toolResultId);
|
|
251
|
-
if (!toolResultCommit)
|
|
1431
|
+
if (!toolResultCommit) {
|
|
1432
|
+
return;
|
|
1433
|
+
}
|
|
252
1434
|
const toolCallCommitId = this._toolCallCommitIds.get(toolCallId);
|
|
253
|
-
if (!toolCallCommitId)
|
|
1435
|
+
if (!toolCallCommitId) {
|
|
1436
|
+
return;
|
|
1437
|
+
}
|
|
254
1438
|
const toolCallIndex = toolResultCommit.parents.indexOf(toolCallCommitId);
|
|
255
1439
|
if (toolCallIndex !== -1) {
|
|
256
1440
|
toolResultCommit.parents[toolCallIndex] = agentStatusId;
|
|
@@ -262,7 +1446,9 @@ var ExecutionGraph = class {
|
|
|
262
1446
|
*/
|
|
263
1447
|
_handleLateToolResultUpdate(toolCallId, toolResultId) {
|
|
264
1448
|
const toolCallCommitId = this._toolCallCommitIds.get(toolCallId);
|
|
265
|
-
if (!toolCallCommitId)
|
|
1449
|
+
if (!toolCallCommitId) {
|
|
1450
|
+
return;
|
|
1451
|
+
}
|
|
266
1452
|
const messageId = toolCallCommitId.split("_toolCall_")[0];
|
|
267
1453
|
const toolCallBranch = `${messageId}_${toolCallId}`;
|
|
268
1454
|
const agentStatusCommitId = this._lastCommitByBranch.get(toolCallBranch);
|
|
@@ -331,7 +1517,7 @@ var messageToCommits = (message, lastBlockId, toolCallIds, lastCommitByBranch, s
|
|
|
331
1517
|
previousBlockId = commit.id;
|
|
332
1518
|
}
|
|
333
1519
|
return commit;
|
|
334
|
-
}).filter(
|
|
1520
|
+
}).filter(isNonNullable3);
|
|
335
1521
|
};
|
|
336
1522
|
var getBlockParents = (block, previousBlockId, message, toolCallIds, lastCommitByBranch) => {
|
|
337
1523
|
const parents = [];
|
|
@@ -365,7 +1551,9 @@ var createBlockCommit = (block, message, branch, parents, idx) => {
|
|
|
365
1551
|
const timestamp = new Date(message.created);
|
|
366
1552
|
switch (block._tag) {
|
|
367
1553
|
case "text":
|
|
368
|
-
if (!block.text.trim().length)
|
|
1554
|
+
if (!block.text.trim().length) {
|
|
1555
|
+
return null;
|
|
1556
|
+
}
|
|
369
1557
|
return {
|
|
370
1558
|
id: getGenericBlockId(message.id, idx),
|
|
371
1559
|
branch,
|
|
@@ -410,7 +1598,7 @@ var createBlockCommit = (block, message, branch, parents, idx) => {
|
|
|
410
1598
|
timestamp,
|
|
411
1599
|
icon: "ph--rocket--regular",
|
|
412
1600
|
level: LogLevel.INFO,
|
|
413
|
-
message:
|
|
1601
|
+
message: ContentBlock3.createStatsMessage(block)
|
|
414
1602
|
};
|
|
415
1603
|
case "status":
|
|
416
1604
|
return {
|
|
@@ -480,44 +1668,40 @@ var stringifyRef = (ref) => {
|
|
|
480
1668
|
return ref.dxn.asEchoDXN()?.echoId ?? ref.dxn.asQueueDXN()?.objectId ?? "";
|
|
481
1669
|
};
|
|
482
1670
|
var stringifyObject = (obj) => {
|
|
483
|
-
return
|
|
1671
|
+
return Obj7.getLabel(obj) ?? Obj7.getTypename(obj) ?? obj.id;
|
|
484
1672
|
};
|
|
485
1673
|
|
|
486
1674
|
// src/templates/system.ts
|
|
487
1675
|
import defaultsDeep from "lodash.defaultsdeep";
|
|
488
|
-
import { Template } from "@dxos/
|
|
1676
|
+
import { Template as Template2 } from "@dxos/compute";
|
|
489
1677
|
|
|
490
1678
|
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/instructions/format.tpl?raw
|
|
491
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";
|
|
492
1680
|
|
|
493
1681
|
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/instructions/system.tpl?raw
|
|
494
|
-
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- 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\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\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>";
|
|
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>";
|
|
495
1683
|
|
|
496
1684
|
// src/templates/system.ts
|
|
497
|
-
var createSystemPrompt = (options = {}) =>
|
|
1685
|
+
var createSystemPrompt = (options = {}) => Template2.process(format_default, defaultsDeep(options, {
|
|
498
1686
|
DATETIME: (/* @__PURE__ */ new Date()).toISOString()
|
|
499
1687
|
}));
|
|
500
1688
|
var SYSTEM_ID = "01K2J2FZWH39F55CS7B26GP1Q3";
|
|
501
1689
|
var templates = {
|
|
502
|
-
system:
|
|
1690
|
+
system: Template2.make({
|
|
503
1691
|
id: SYSTEM_ID,
|
|
504
1692
|
source: system_default
|
|
505
1693
|
})
|
|
506
1694
|
};
|
|
507
1695
|
export {
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
AgentRequestCompleted,
|
|
511
|
-
AgentService_exports as AgentService,
|
|
512
|
-
AgentTurnCompleted,
|
|
513
|
-
AgentTurnStarted,
|
|
1696
|
+
AgentRequestBegin,
|
|
1697
|
+
AgentRequestEnd,
|
|
514
1698
|
AiAssistantError,
|
|
515
1699
|
AiContextBinder,
|
|
516
1700
|
AiContextService,
|
|
517
|
-
AiConversation,
|
|
518
|
-
AiConversationService,
|
|
519
1701
|
AiModelError,
|
|
1702
|
+
AiRequest,
|
|
520
1703
|
AiSession,
|
|
1704
|
+
AiSessionService,
|
|
521
1705
|
ArtifactDiffResolver,
|
|
522
1706
|
ArtifactId,
|
|
523
1707
|
Bindings,
|
|
@@ -525,10 +1709,9 @@ export {
|
|
|
525
1709
|
ContextBinding,
|
|
526
1710
|
ExecutionGraph,
|
|
527
1711
|
GenerationObserver,
|
|
1712
|
+
McpServerError,
|
|
528
1713
|
PartialBlock,
|
|
529
1714
|
RefFromLLM,
|
|
530
|
-
ToolCallCompleted,
|
|
531
|
-
ToolCallStarted,
|
|
532
1715
|
ToolExecutionServices,
|
|
533
1716
|
VersionPin,
|
|
534
1717
|
applyDiffs,
|
|
@@ -538,12 +1721,9 @@ export {
|
|
|
538
1721
|
createToolkit,
|
|
539
1722
|
formatSystemPrompt,
|
|
540
1723
|
formatUserPrompt,
|
|
541
|
-
functionInvocationServiceFromOperations,
|
|
542
1724
|
getOperationFromTool,
|
|
543
1725
|
makeToolExecutionService,
|
|
544
|
-
makeToolExecutionServiceFromFunctions,
|
|
545
1726
|
makeToolExecutionServiceFromOperationInvoker,
|
|
546
|
-
makeToolResolverFromFunctions,
|
|
547
1727
|
makeToolResolverFromOperations,
|
|
548
1728
|
reduceDiffs,
|
|
549
1729
|
templates
|