@dxos/assistant 0.8.4-main.bc674ce → 0.8.4-main.bcb3aa67d6
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/README.md +1 -0
- package/dist/lib/neutral/chunk-KVYJFFLF.mjs +1805 -0
- package/dist/lib/neutral/chunk-KVYJFFLF.mjs.map +7 -0
- package/dist/lib/{browser → neutral}/extraction/index.mjs +26 -32
- package/dist/lib/neutral/extraction/index.mjs.map +7 -0
- package/dist/lib/{browser → neutral}/index.mjs +72 -371
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/lib/neutral/testing/index.mjs +112 -0
- package/dist/lib/neutral/testing/index.mjs.map +7 -0
- package/dist/types/src/conversation/context.d.ts +24 -5
- package/dist/types/src/conversation/context.d.ts.map +1 -1
- package/dist/types/src/conversation/conversation.d.ts +29 -4
- package/dist/types/src/conversation/conversation.d.ts.map +1 -1
- package/dist/types/src/diff.d.ts +3 -1
- package/dist/types/src/diff.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.d.ts +729 -17
- 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/functions/services.d.ts +27 -8
- package/dist/types/src/functions/services.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +2 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/service/AgentService.d.ts +62 -0
- package/dist/types/src/service/AgentService.d.ts.map +1 -0
- package/dist/types/src/service/AgentService.test.d.ts +2 -0
- package/dist/types/src/service/AgentService.test.d.ts.map +1 -0
- package/dist/types/src/service/agent-process.d.ts +17 -0
- package/dist/types/src/service/agent-process.d.ts.map +1 -0
- package/dist/types/src/service/index.d.ts +3 -0
- package/dist/types/src/service/index.d.ts.map +1 -0
- package/dist/types/src/session/index.d.ts +1 -1
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/observer.d.ts.map +1 -1
- package/dist/types/src/session/session.d.ts +64 -8
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/session/toolkit.d.ts +6 -2
- package/dist/types/src/session/toolkit.d.ts.map +1 -1
- package/dist/types/src/templates/system.d.ts +0 -1
- package/dist/types/src/templates/system.d.ts.map +1 -1
- package/dist/types/src/testing/layer.d.ts +23 -9
- package/dist/types/src/testing/layer.d.ts.map +1 -1
- package/dist/types/src/tracing.d.ts +264 -0
- package/dist/types/src/tracing.d.ts.map +1 -0
- package/dist/types/src/types/artifact.d.ts +5 -5
- package/dist/types/src/types/artifact.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +33 -33
- package/src/conversation/context.test.ts +1 -1
- package/src/conversation/context.ts +84 -18
- package/src/conversation/conversation.ts +173 -32
- package/src/diff.test.ts +36 -5
- package/src/diff.ts +13 -4
- package/src/execution-graph.test.ts +2 -2
- package/src/execution-graph.ts +2 -2
- package/src/extraction/extraction-ner-function/index.ts +0 -1
- package/src/extraction/extraction.ts +38 -10
- package/src/extraction/index.ts +0 -1
- package/src/functions/services.ts +199 -50
- package/src/functions.conversations.json +1 -1
- package/src/functions.test.ts +28 -20
- package/src/index.ts +2 -0
- package/src/service/AgentService.conversations.json +1 -0
- package/src/service/AgentService.test.ts +169 -0
- package/src/service/AgentService.ts +171 -0
- package/src/service/agent-process.ts +360 -0
- package/src/service/index.ts +6 -0
- package/src/session/format.test.ts +23 -14
- package/src/session/index.ts +1 -2
- package/src/session/observer.ts +3 -2
- package/src/session/session.conversations.json +1 -1
- package/src/session/session.test.ts +52 -4
- package/src/session/session.ts +231 -117
- package/src/session/toolkit.ts +12 -7
- package/src/templates/instructions/format.tpl +0 -4
- package/src/templates/instructions/system.tpl +11 -2
- package/src/templates/system.ts +6 -5
- package/src/testing/layer.ts +209 -24
- package/src/tracing.ts +112 -0
- package/src/types/artifact.ts +17 -10
- package/dist/lib/browser/chunk-7CDDG6LL.mjs +0 -578
- package/dist/lib/browser/chunk-7CDDG6LL.mjs.map +0 -7
- package/dist/lib/browser/extraction/index.mjs.map +0 -7
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/browser/testing/index.mjs +0 -32
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-CC7ER6Q3.mjs +0 -579
- package/dist/lib/node-esm/chunk-CC7ER6Q3.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +0 -11
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +0 -7
- package/dist/lib/node-esm/extraction/index.mjs +0 -226
- package/dist/lib/node-esm/extraction/index.mjs.map +0 -7
- package/dist/lib/node-esm/index.mjs +0 -851
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/lib/node-esm/testing/index.mjs +0 -33
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
- 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 -347
- 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/session/GenericToolkit.d.ts +0 -85
- package/dist/types/src/session/GenericToolkit.d.ts.map +0 -1
- 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/session/GenericToolkit.ts +0 -131
- /package/dist/lib/{browser → neutral}/chunk-J5LGTIGS.mjs +0 -0
- /package/dist/lib/{browser → neutral}/chunk-J5LGTIGS.mjs.map +0 -0
|
@@ -1,851 +0,0 @@
|
|
|
1
|
-
import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
|
|
2
|
-
import {
|
|
3
|
-
AiAssistantError,
|
|
4
|
-
AiModelError,
|
|
5
|
-
AiSession,
|
|
6
|
-
ArtifactDiffResolver,
|
|
7
|
-
ArtifactId,
|
|
8
|
-
GenerationObserver,
|
|
9
|
-
GenericToolkit_exports,
|
|
10
|
-
RefFromLLM,
|
|
11
|
-
VersionPin,
|
|
12
|
-
createArtifactElement,
|
|
13
|
-
createToolkit,
|
|
14
|
-
makeToolExecutionServiceFromFunctions,
|
|
15
|
-
makeToolResolverFromFunctions
|
|
16
|
-
} from "./chunk-CC7ER6Q3.mjs";
|
|
17
|
-
import "./chunk-HSLMI22Q.mjs";
|
|
18
|
-
|
|
19
|
-
// src/conversation/context.ts
|
|
20
|
-
import { Atom, Registry } from "@effect-atom/atom-react";
|
|
21
|
-
import * as EArray from "effect/Array";
|
|
22
|
-
import * as Context from "effect/Context";
|
|
23
|
-
import * as Function from "effect/Function";
|
|
24
|
-
import * as Schema from "effect/Schema";
|
|
25
|
-
import { Blueprint } from "@dxos/blueprints";
|
|
26
|
-
import { Resource } from "@dxos/context";
|
|
27
|
-
import { DXN, Obj, Query, Type } from "@dxos/echo";
|
|
28
|
-
import { log } from "@dxos/log";
|
|
29
|
-
import { ComplexSet, isTruthy } from "@dxos/util";
|
|
30
|
-
var __dxlog_file = "/__w/dxos/dxos/packages/core/assistant/src/conversation/context.ts";
|
|
31
|
-
var ContextBinding = Schema.Struct({
|
|
32
|
-
blueprints: Schema.Struct({
|
|
33
|
-
added: Schema.Array(Type.Ref(Blueprint.Blueprint)),
|
|
34
|
-
removed: Schema.Array(Type.Ref(Blueprint.Blueprint))
|
|
35
|
-
}),
|
|
36
|
-
objects: Schema.Struct({
|
|
37
|
-
added: Schema.Array(Type.Ref(Type.Obj)),
|
|
38
|
-
removed: Schema.Array(Type.Ref(Type.Obj))
|
|
39
|
-
})
|
|
40
|
-
}).pipe(Type.object({
|
|
41
|
-
typename: "dxos.org/type/ContextBinding",
|
|
42
|
-
version: "0.1.0"
|
|
43
|
-
}));
|
|
44
|
-
var Bindings = class {
|
|
45
|
-
blueprints = new ComplexSet((ref) => ref.dxn.toString());
|
|
46
|
-
// TODO(burdon): Some DXNs have the Space prefix so only compare the object ID.
|
|
47
|
-
objects = new ComplexSet((ref) => ref.dxn.asEchoDXN()?.echoId);
|
|
48
|
-
toJSON() {
|
|
49
|
-
return {
|
|
50
|
-
blueprints: EArray.fromIterable(this.blueprints).map((ref) => ref.dxn.toString()),
|
|
51
|
-
objects: EArray.fromIterable(this.objects).map((ref) => ref.dxn.toString())
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
var AiContextBinder = class extends Resource {
|
|
56
|
-
_blueprints = Atom.make([]).pipe(Atom.keepAlive);
|
|
57
|
-
_objects = Atom.make([]).pipe(Atom.keepAlive);
|
|
58
|
-
_registry;
|
|
59
|
-
_queue;
|
|
60
|
-
constructor(options) {
|
|
61
|
-
super();
|
|
62
|
-
this._queue = options.queue;
|
|
63
|
-
this._registry = options.registry ?? Registry.make();
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Returns the blueprints atom for subscription.
|
|
67
|
-
*/
|
|
68
|
-
get blueprints() {
|
|
69
|
-
return this._blueprints;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Returns the objects atom for subscription.
|
|
73
|
-
*/
|
|
74
|
-
get objects() {
|
|
75
|
-
return this._objects;
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Gets the current blueprints value.
|
|
79
|
-
*/
|
|
80
|
-
getBlueprints() {
|
|
81
|
-
return this._registry.get(this._blueprints);
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Gets the current objects value.
|
|
85
|
-
*/
|
|
86
|
-
getObjects() {
|
|
87
|
-
return this._registry.get(this._objects);
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Subscribe to changes in blueprints.
|
|
91
|
-
*/
|
|
92
|
-
subscribeBlueprints(cb) {
|
|
93
|
-
return this._registry.subscribe(this._blueprints, () => cb(this._registry.get(this._blueprints)));
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Subscribe to changes in objects.
|
|
97
|
-
*/
|
|
98
|
-
subscribeObjects(cb) {
|
|
99
|
-
return this._registry.subscribe(this._objects, () => cb(this._registry.get(this._objects)));
|
|
100
|
-
}
|
|
101
|
-
async _open() {
|
|
102
|
-
const query = this._queue.query(Query.type(ContextBinding));
|
|
103
|
-
const initialResults = await query.run();
|
|
104
|
-
await this._updateBindings(initialResults);
|
|
105
|
-
this._ctx.onDispose(query.subscribe(async () => {
|
|
106
|
-
await this._updateBindings(query.results);
|
|
107
|
-
}));
|
|
108
|
-
}
|
|
109
|
-
async _updateBindings(items) {
|
|
110
|
-
if (items.length === 0) {
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
const bindings = this._reduce(items);
|
|
114
|
-
const currentBlueprints = this._registry.get(this._blueprints);
|
|
115
|
-
const currentObjects = this._registry.get(this._objects);
|
|
116
|
-
const newBlueprints = await this._resolve(bindings.blueprints, currentBlueprints);
|
|
117
|
-
const newObjects = await this._resolve(bindings.objects, currentObjects);
|
|
118
|
-
this._registry.set(this._blueprints, newBlueprints);
|
|
119
|
-
this._registry.set(this._objects, newObjects);
|
|
120
|
-
log("updated bindings", {
|
|
121
|
-
blueprints: newBlueprints.length,
|
|
122
|
-
objects: newObjects.length
|
|
123
|
-
}, {
|
|
124
|
-
F: __dxlog_file,
|
|
125
|
-
L: 156,
|
|
126
|
-
S: this,
|
|
127
|
-
C: (f, a) => f(...a)
|
|
128
|
-
});
|
|
129
|
-
}
|
|
130
|
-
async _close() {
|
|
131
|
-
this._registry.set(this._blueprints, []);
|
|
132
|
-
this._registry.set(this._objects, []);
|
|
133
|
-
}
|
|
134
|
-
async bind({ blueprints, objects }) {
|
|
135
|
-
const currentBlueprints = this._registry.get(this._blueprints);
|
|
136
|
-
const currentObjects = this._registry.get(this._objects);
|
|
137
|
-
const { added: addedBlueprints, next: nextBlueprints } = this._processBindings(blueprints, currentBlueprints);
|
|
138
|
-
const { added: addedObjects, next: nextObjects } = this._processBindings(objects, currentObjects);
|
|
139
|
-
if (!addedBlueprints.length && !addedObjects.length) {
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
this._registry.set(this._blueprints, nextBlueprints);
|
|
143
|
-
this._registry.set(this._objects, nextObjects);
|
|
144
|
-
log("bind", {
|
|
145
|
-
blueprints: addedBlueprints.length,
|
|
146
|
-
objects: addedObjects.length
|
|
147
|
-
}, {
|
|
148
|
-
F: __dxlog_file,
|
|
149
|
-
L: 182,
|
|
150
|
-
S: this,
|
|
151
|
-
C: (f, a) => f(...a)
|
|
152
|
-
});
|
|
153
|
-
await this._queue.append([
|
|
154
|
-
Obj.make(ContextBinding, {
|
|
155
|
-
blueprints: {
|
|
156
|
-
added: addedBlueprints,
|
|
157
|
-
removed: []
|
|
158
|
-
},
|
|
159
|
-
objects: {
|
|
160
|
-
added: addedObjects,
|
|
161
|
-
removed: []
|
|
162
|
-
}
|
|
163
|
-
})
|
|
164
|
-
]);
|
|
165
|
-
}
|
|
166
|
-
async unbind({ blueprints, objects }) {
|
|
167
|
-
if (!blueprints?.length && !objects?.length) {
|
|
168
|
-
return;
|
|
169
|
-
}
|
|
170
|
-
log("unbind", {
|
|
171
|
-
blueprints: blueprints?.length,
|
|
172
|
-
objects: objects?.length
|
|
173
|
-
}, {
|
|
174
|
-
F: __dxlog_file,
|
|
175
|
-
L: 202,
|
|
176
|
-
S: this,
|
|
177
|
-
C: (f, a) => f(...a)
|
|
178
|
-
});
|
|
179
|
-
await this._queue.append([
|
|
180
|
-
Obj.make(ContextBinding, {
|
|
181
|
-
blueprints: {
|
|
182
|
-
added: [],
|
|
183
|
-
removed: blueprints ?? []
|
|
184
|
-
},
|
|
185
|
-
objects: {
|
|
186
|
-
added: [],
|
|
187
|
-
removed: objects ?? []
|
|
188
|
-
}
|
|
189
|
-
})
|
|
190
|
-
]);
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Process bindings to filter duplicates and determine next state.
|
|
194
|
-
*/
|
|
195
|
-
_processBindings(refs, current) {
|
|
196
|
-
const next = [
|
|
197
|
-
...current
|
|
198
|
-
];
|
|
199
|
-
const added = [];
|
|
200
|
-
if (!refs?.length) {
|
|
201
|
-
return {
|
|
202
|
-
added,
|
|
203
|
-
next
|
|
204
|
-
};
|
|
205
|
-
}
|
|
206
|
-
const seen = new Set(current.map((obj) => Obj.getDXN(obj).toString()));
|
|
207
|
-
for (const ref of refs) {
|
|
208
|
-
const dxn = ref.dxn.toString();
|
|
209
|
-
if (!seen.has(dxn)) {
|
|
210
|
-
seen.add(dxn);
|
|
211
|
-
added.push(ref);
|
|
212
|
-
if (ref.isAvailable) {
|
|
213
|
-
const target = ref.target;
|
|
214
|
-
if (target) {
|
|
215
|
-
next.push(target);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
return {
|
|
221
|
-
added,
|
|
222
|
-
next
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* Reduce results into sets of blueprints and objects.
|
|
227
|
-
*/
|
|
228
|
-
_reduce(items) {
|
|
229
|
-
return Function.pipe(items, EArray.reduce(new Bindings(), (context, { blueprints, objects }) => {
|
|
230
|
-
blueprints.added.forEach((ref) => context.blueprints.add(ref));
|
|
231
|
-
blueprints.removed.forEach((ref) => context.blueprints.delete(ref));
|
|
232
|
-
objects.added.forEach((ref) => context.objects.add(ref));
|
|
233
|
-
objects.removed.forEach((ref) => {
|
|
234
|
-
for (const obj of context.objects) {
|
|
235
|
-
if (DXN.equalsEchoId(obj.dxn, ref.dxn)) {
|
|
236
|
-
context.objects.delete(obj);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
});
|
|
240
|
-
return context;
|
|
241
|
-
}));
|
|
242
|
-
}
|
|
243
|
-
/**
|
|
244
|
-
* Resolve references to objects, loading them first if needed and falling back to existing objects.
|
|
245
|
-
*/
|
|
246
|
-
async _resolve(refs, current) {
|
|
247
|
-
const refArray = [
|
|
248
|
-
...refs
|
|
249
|
-
];
|
|
250
|
-
await Promise.all(refArray.map((ref) => ref.load()));
|
|
251
|
-
return refArray.map((ref) => {
|
|
252
|
-
let target;
|
|
253
|
-
if (ref.isAvailable) {
|
|
254
|
-
target = ref.target;
|
|
255
|
-
}
|
|
256
|
-
return target ?? current.find((obj) => Obj.getDXN(obj).toString() === ref.dxn.toString());
|
|
257
|
-
}).filter(isTruthy);
|
|
258
|
-
}
|
|
259
|
-
};
|
|
260
|
-
var AiContextService = class extends Context.Tag("@dxos/assistant/AiContextService")() {
|
|
261
|
-
};
|
|
262
|
-
|
|
263
|
-
// src/conversation/conversation.ts
|
|
264
|
-
import * as Effect from "effect/Effect";
|
|
265
|
-
import { Resource as Resource2 } from "@dxos/context";
|
|
266
|
-
import { Obj as Obj2 } from "@dxos/echo";
|
|
267
|
-
import { invariant } from "@dxos/invariant";
|
|
268
|
-
import { log as log2 } from "@dxos/log";
|
|
269
|
-
import { Message } from "@dxos/types";
|
|
270
|
-
var __dxlog_file2 = "/__w/dxos/dxos/packages/core/assistant/src/conversation/conversation.ts";
|
|
271
|
-
var AiConversation = class extends Resource2 {
|
|
272
|
-
/**
|
|
273
|
-
* Blueprints and objects bound to the conversation.
|
|
274
|
-
*/
|
|
275
|
-
_binder;
|
|
276
|
-
_queue;
|
|
277
|
-
_toolkit;
|
|
278
|
-
constructor(options) {
|
|
279
|
-
super();
|
|
280
|
-
this._queue = options.queue;
|
|
281
|
-
this._toolkit = options.toolkit;
|
|
282
|
-
invariant(this._queue, void 0, {
|
|
283
|
-
F: __dxlog_file2,
|
|
284
|
-
L: 54,
|
|
285
|
-
S: this,
|
|
286
|
-
A: [
|
|
287
|
-
"this._queue",
|
|
288
|
-
""
|
|
289
|
-
]
|
|
290
|
-
});
|
|
291
|
-
this._binder = new AiContextBinder({
|
|
292
|
-
queue: this._queue,
|
|
293
|
-
registry: options.registry
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
async _open() {
|
|
297
|
-
await this._binder.open(this._ctx);
|
|
298
|
-
}
|
|
299
|
-
get queue() {
|
|
300
|
-
return this._queue;
|
|
301
|
-
}
|
|
302
|
-
get context() {
|
|
303
|
-
return this._binder;
|
|
304
|
-
}
|
|
305
|
-
get toolkit() {
|
|
306
|
-
return this._toolkit;
|
|
307
|
-
}
|
|
308
|
-
async getHistory() {
|
|
309
|
-
const queueItems = await this._queue.queryObjects();
|
|
310
|
-
return queueItems.filter(Obj2.instanceOf(Message.Message));
|
|
311
|
-
}
|
|
312
|
-
/**
|
|
313
|
-
* Creates a new cancelable request effect.
|
|
314
|
-
*/
|
|
315
|
-
createRequest(params) {
|
|
316
|
-
const self = this;
|
|
317
|
-
return Effect.gen(function* () {
|
|
318
|
-
const history = yield* Effect.promise(() => self.getHistory());
|
|
319
|
-
const blueprints = self.context.getBlueprints();
|
|
320
|
-
const toolkit = yield* createToolkit({
|
|
321
|
-
toolkit: self._toolkit,
|
|
322
|
-
blueprints
|
|
323
|
-
});
|
|
324
|
-
const objects = self.context.getObjects();
|
|
325
|
-
log2("run", {
|
|
326
|
-
history: history.length,
|
|
327
|
-
blueprints: blueprints.length,
|
|
328
|
-
tools: Object.keys(toolkit.tools).length,
|
|
329
|
-
objects: objects.length
|
|
330
|
-
}, {
|
|
331
|
-
F: __dxlog_file2,
|
|
332
|
-
L: 99,
|
|
333
|
-
S: this,
|
|
334
|
-
C: (f, a) => f(...a)
|
|
335
|
-
});
|
|
336
|
-
const session = new AiSession();
|
|
337
|
-
const messages = yield* session.run({
|
|
338
|
-
history,
|
|
339
|
-
blueprints,
|
|
340
|
-
toolkit,
|
|
341
|
-
objects,
|
|
342
|
-
...params
|
|
343
|
-
}).pipe(Effect.provideService(AiContextService, {
|
|
344
|
-
binder: self.context
|
|
345
|
-
}));
|
|
346
|
-
log2("result", {
|
|
347
|
-
messages: messages.length,
|
|
348
|
-
duration: session.duration,
|
|
349
|
-
toolCalls: session.toolCalls
|
|
350
|
-
}, {
|
|
351
|
-
F: __dxlog_file2,
|
|
352
|
-
L: 114,
|
|
353
|
-
S: this,
|
|
354
|
-
C: (f, a) => f(...a)
|
|
355
|
-
});
|
|
356
|
-
yield* Effect.promise(() => self._queue.append(messages));
|
|
357
|
-
return messages;
|
|
358
|
-
});
|
|
359
|
-
}
|
|
360
|
-
};
|
|
361
|
-
|
|
362
|
-
// src/diff.ts
|
|
363
|
-
import { next as A } from "@automerge/automerge";
|
|
364
|
-
import { DocAccessor, toCursorRange } from "@dxos/echo-db";
|
|
365
|
-
import { log as log3 } from "@dxos/log";
|
|
366
|
-
import { isNonNullable } from "@dxos/util";
|
|
367
|
-
var __dxlog_file3 = "/__w/dxos/dxos/packages/core/assistant/src/diff.ts";
|
|
368
|
-
var reduceDiffs = (diffs) => {
|
|
369
|
-
return diffs.reduce((acc, diff) => {
|
|
370
|
-
const current = acc[acc.length - 1];
|
|
371
|
-
const [op, ...rest] = diff.split(" ");
|
|
372
|
-
switch (op) {
|
|
373
|
-
case "-": {
|
|
374
|
-
if (current && !current.match) {
|
|
375
|
-
current.match += rest.join(" ");
|
|
376
|
-
} else {
|
|
377
|
-
acc.push({
|
|
378
|
-
match: rest.join(" "),
|
|
379
|
-
replace: ""
|
|
380
|
-
});
|
|
381
|
-
}
|
|
382
|
-
return acc;
|
|
383
|
-
}
|
|
384
|
-
case "+": {
|
|
385
|
-
if (current && !current.replace) {
|
|
386
|
-
current.replace += rest.join(" ");
|
|
387
|
-
} else {
|
|
388
|
-
acc.push({
|
|
389
|
-
match: "",
|
|
390
|
-
replace: rest.join(" ")
|
|
391
|
-
});
|
|
392
|
-
}
|
|
393
|
-
return acc;
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
return acc;
|
|
397
|
-
}, []);
|
|
398
|
-
};
|
|
399
|
-
var computeDiffsWithCursors = (accessor, diffs) => {
|
|
400
|
-
return reduceDiffs(diffs).map((diff) => {
|
|
401
|
-
const text = DocAccessor.getValue(accessor);
|
|
402
|
-
const idx = text.indexOf(diff.match);
|
|
403
|
-
if (idx !== -1) {
|
|
404
|
-
return {
|
|
405
|
-
cursor: toCursorRange(accessor, idx, idx + diff.match.length),
|
|
406
|
-
text: diff.replace
|
|
407
|
-
};
|
|
408
|
-
}
|
|
409
|
-
}).filter(isNonNullable);
|
|
410
|
-
};
|
|
411
|
-
var applyDiffs = (accessor, diffs) => {
|
|
412
|
-
for (const diff of reduceDiffs(diffs)) {
|
|
413
|
-
accessor.handle.change((doc) => {
|
|
414
|
-
const text = DocAccessor.getValue(accessor);
|
|
415
|
-
const idx = text.indexOf(diff.match);
|
|
416
|
-
if (idx !== -1) {
|
|
417
|
-
A.splice(doc, accessor.path, idx, diff.match.length, diff.replace);
|
|
418
|
-
} else {
|
|
419
|
-
log3.warn("diff not found", diff, {
|
|
420
|
-
F: __dxlog_file3,
|
|
421
|
-
L: 78,
|
|
422
|
-
S: void 0,
|
|
423
|
-
C: (f, a) => f(...a)
|
|
424
|
-
});
|
|
425
|
-
}
|
|
426
|
-
});
|
|
427
|
-
}
|
|
428
|
-
return DocAccessor.getValue(accessor);
|
|
429
|
-
};
|
|
430
|
-
|
|
431
|
-
// src/execution-graph.ts
|
|
432
|
-
import { AgentStatus } from "@dxos/ai";
|
|
433
|
-
import { Obj as Obj3 } from "@dxos/echo";
|
|
434
|
-
import { MESSAGE_PROPERTY_TOOL_CALL_ID } from "@dxos/functions";
|
|
435
|
-
import { LogLevel } from "@dxos/log";
|
|
436
|
-
import { ContentBlock, Message as Message2 } from "@dxos/types";
|
|
437
|
-
import { isNonNullable as isNonNullable2 } from "@dxos/util";
|
|
438
|
-
var SKIP_BLOCKS = [
|
|
439
|
-
"text"
|
|
440
|
-
];
|
|
441
|
-
var ExecutionGraph = class {
|
|
442
|
-
_skipBlocks;
|
|
443
|
-
// TODO(dmaretskyi): Evolve the internal state to support chats, functions, circuit workflows, etc.
|
|
444
|
-
_commits = [];
|
|
445
|
-
_branchNames = /* @__PURE__ */ new Set();
|
|
446
|
-
_lastBlockId;
|
|
447
|
-
// Tool call tracking.
|
|
448
|
-
_toolCallCommitIds = /* @__PURE__ */ new Map();
|
|
449
|
-
_lastCommitByBranch = /* @__PURE__ */ new Map();
|
|
450
|
-
_pendingToolResults = /* @__PURE__ */ new Map();
|
|
451
|
-
constructor(_skipBlocks = SKIP_BLOCKS) {
|
|
452
|
-
this._skipBlocks = _skipBlocks;
|
|
453
|
-
}
|
|
454
|
-
/**
|
|
455
|
-
* Adds events to the graph.
|
|
456
|
-
*/
|
|
457
|
-
addEvents(events) {
|
|
458
|
-
const sortedEvents = this.sortObjectsByCreated(events);
|
|
459
|
-
for (const event of sortedEvents) {
|
|
460
|
-
if (Obj3.instanceOf(Message2.Message, event)) {
|
|
461
|
-
this._processMessage(event);
|
|
462
|
-
} else if (Obj3.instanceOf(AgentStatus, event)) {
|
|
463
|
-
this._processAgentStatus(event);
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
/**
|
|
468
|
-
* Processes a message event and creates commits for its blocks.
|
|
469
|
-
*/
|
|
470
|
-
_processMessage(message) {
|
|
471
|
-
const messageCommits = messageToCommits(message, this._lastBlockId, this._toolCallCommitIds, this._lastCommitByBranch, this._skipBlocks);
|
|
472
|
-
this._commits.push(...messageCommits);
|
|
473
|
-
messageCommits.forEach((commit) => this._branchNames.add(commit.branch));
|
|
474
|
-
this._trackToolCalls(messageCommits);
|
|
475
|
-
if (messageCommits.length > 0) {
|
|
476
|
-
this._lastBlockId = messageCommits[messageCommits.length - 1].id;
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
/**
|
|
480
|
-
* Processes an AgentStatus event.
|
|
481
|
-
*/
|
|
482
|
-
_processAgentStatus(event) {
|
|
483
|
-
const branch = this._getToolCallBranch(event.parentMessage, event.toolCallId);
|
|
484
|
-
this._branchNames.add(branch);
|
|
485
|
-
const agentStatusCommit = {
|
|
486
|
-
id: event.id,
|
|
487
|
-
branch,
|
|
488
|
-
message: event.message,
|
|
489
|
-
icon: "ph--flag--regular",
|
|
490
|
-
parents: this._getAgentStatusParents(event),
|
|
491
|
-
timestamp: new Date(event.created)
|
|
492
|
-
};
|
|
493
|
-
this._commits.push(agentStatusCommit);
|
|
494
|
-
this._lastBlockId = event.id;
|
|
495
|
-
if (event.toolCallId) {
|
|
496
|
-
this._lastCommitByBranch.set(branch, event.id);
|
|
497
|
-
this._updatePendingToolResults(event.toolCallId, event.id);
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
/**
|
|
501
|
-
* Stable sort: objects with 'created' field are sorted by it, others remain in place.
|
|
502
|
-
*/
|
|
503
|
-
sortObjectsByCreated(objects) {
|
|
504
|
-
const sortedObjects = objects.slice();
|
|
505
|
-
const createdObjects = [];
|
|
506
|
-
sortedObjects.forEach((obj, idx) => {
|
|
507
|
-
if (obj && typeof obj.created === "string") {
|
|
508
|
-
createdObjects.push({
|
|
509
|
-
idx,
|
|
510
|
-
obj
|
|
511
|
-
});
|
|
512
|
-
}
|
|
513
|
-
});
|
|
514
|
-
createdObjects.sort((a, b) => {
|
|
515
|
-
const aDate = new Date(a.obj.created).getTime();
|
|
516
|
-
const bDate = new Date(b.obj.created).getTime();
|
|
517
|
-
return aDate - bDate;
|
|
518
|
-
});
|
|
519
|
-
let createdIdx = 0;
|
|
520
|
-
return sortedObjects.map((obj, _idx) => {
|
|
521
|
-
if (obj && typeof obj.created === "string") {
|
|
522
|
-
return createdObjects[createdIdx++].obj;
|
|
523
|
-
}
|
|
524
|
-
return obj;
|
|
525
|
-
});
|
|
526
|
-
}
|
|
527
|
-
/**
|
|
528
|
-
* Returns the current state of the graph.
|
|
529
|
-
*/
|
|
530
|
-
getGraph(lastRequest = false) {
|
|
531
|
-
const idx = lastRequest ? this._commits.findLastIndex((c) => c.tags?.includes("user")) : -1;
|
|
532
|
-
const commits = idx === -1 ? this._commits : this._commits.slice(idx);
|
|
533
|
-
return {
|
|
534
|
-
branches: Array.from(this._branchNames),
|
|
535
|
-
commits
|
|
536
|
-
};
|
|
537
|
-
}
|
|
538
|
-
/**
|
|
539
|
-
* Tracks tool calls and handles pending tool result updates.
|
|
540
|
-
*/
|
|
541
|
-
_trackToolCalls(commits) {
|
|
542
|
-
commits.forEach((commit) => {
|
|
543
|
-
if (this._isToolCallCommit(commit)) {
|
|
544
|
-
const toolCallId = this._extractToolCallId(commit.id);
|
|
545
|
-
if (toolCallId) {
|
|
546
|
-
this._toolCallCommitIds.set(toolCallId, commit.id);
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
if (this._isToolResultCommit(commit)) {
|
|
550
|
-
const toolCallId = this._extractToolCallId(commit.id);
|
|
551
|
-
if (toolCallId) {
|
|
552
|
-
this._pendingToolResults.set(toolCallId, commit.id);
|
|
553
|
-
this._handleLateToolResultUpdate(toolCallId, commit.id);
|
|
554
|
-
}
|
|
555
|
-
}
|
|
556
|
-
this._lastCommitByBranch.set(commit.branch, commit.id);
|
|
557
|
-
});
|
|
558
|
-
}
|
|
559
|
-
/**
|
|
560
|
-
* Updates pending tool results to point to an AgentStatus event.
|
|
561
|
-
*/
|
|
562
|
-
_updatePendingToolResults(toolCallId, agentStatusId) {
|
|
563
|
-
const toolResultId = this._pendingToolResults.get(toolCallId);
|
|
564
|
-
if (!toolResultId) return;
|
|
565
|
-
const toolResultCommit = this._commits.find((c) => c.id === toolResultId);
|
|
566
|
-
if (!toolResultCommit) return;
|
|
567
|
-
const toolCallCommitId = this._toolCallCommitIds.get(toolCallId);
|
|
568
|
-
if (!toolCallCommitId) return;
|
|
569
|
-
const toolCallIndex = toolResultCommit.parents.indexOf(toolCallCommitId);
|
|
570
|
-
if (toolCallIndex !== -1) {
|
|
571
|
-
toolResultCommit.parents[toolCallIndex] = agentStatusId;
|
|
572
|
-
}
|
|
573
|
-
this._pendingToolResults.delete(toolCallId);
|
|
574
|
-
}
|
|
575
|
-
/**
|
|
576
|
-
* Handles late updates when AgentStatus is processed before tool result.
|
|
577
|
-
*/
|
|
578
|
-
_handleLateToolResultUpdate(toolCallId, toolResultId) {
|
|
579
|
-
const toolCallCommitId = this._toolCallCommitIds.get(toolCallId);
|
|
580
|
-
if (!toolCallCommitId) return;
|
|
581
|
-
const messageId = toolCallCommitId.split("_toolCall_")[0];
|
|
582
|
-
const toolCallBranch = `${messageId}_${toolCallId}`;
|
|
583
|
-
const agentStatusCommitId = this._lastCommitByBranch.get(toolCallBranch);
|
|
584
|
-
if (agentStatusCommitId && agentStatusCommitId !== toolCallCommitId) {
|
|
585
|
-
const toolResultCommit = this._commits.find((c) => c.id === toolResultId);
|
|
586
|
-
if (toolResultCommit) {
|
|
587
|
-
const toolCallIndex = toolResultCommit.parents.indexOf(toolCallCommitId);
|
|
588
|
-
if (toolCallIndex !== -1) {
|
|
589
|
-
toolResultCommit.parents[toolCallIndex] = agentStatusCommitId;
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
this._pendingToolResults.delete(toolCallId);
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
/**
|
|
596
|
-
* Gets the branch name for a tool call.
|
|
597
|
-
*/
|
|
598
|
-
_getToolCallBranch(parentMessage, toolCallId) {
|
|
599
|
-
if (toolCallId) {
|
|
600
|
-
return toolCallId;
|
|
601
|
-
}
|
|
602
|
-
return parentMessage || "main";
|
|
603
|
-
}
|
|
604
|
-
/**
|
|
605
|
-
* Gets the parents for an AgentStatus commit.
|
|
606
|
-
*/
|
|
607
|
-
_getAgentStatusParents(event) {
|
|
608
|
-
if (event.parentMessage && event.toolCallId) {
|
|
609
|
-
return [
|
|
610
|
-
getToolCallId(event.parentMessage, event.toolCallId)
|
|
611
|
-
];
|
|
612
|
-
}
|
|
613
|
-
return [];
|
|
614
|
-
}
|
|
615
|
-
/**
|
|
616
|
-
* Checks if a commit is a tool call commit.
|
|
617
|
-
*/
|
|
618
|
-
_isToolCallCommit(commit) {
|
|
619
|
-
return commit.message.includes("Calling tool");
|
|
620
|
-
}
|
|
621
|
-
/**
|
|
622
|
-
* Checks if a commit is a tool result commit.
|
|
623
|
-
*/
|
|
624
|
-
_isToolResultCommit(commit) {
|
|
625
|
-
return commit.message.includes("Tool call succeeded") || commit.message.includes("Tool error:");
|
|
626
|
-
}
|
|
627
|
-
/**
|
|
628
|
-
* Extracts tool call ID from a commit ID.
|
|
629
|
-
*/
|
|
630
|
-
_extractToolCallId(commitId) {
|
|
631
|
-
const toolCallMatch = commitId.match(/_toolCall_(.+)$/);
|
|
632
|
-
const toolResultMatch = commitId.match(/_toolResult_(.+)$/);
|
|
633
|
-
return toolCallMatch?.[1] || toolResultMatch?.[1];
|
|
634
|
-
}
|
|
635
|
-
};
|
|
636
|
-
var messageToCommits = (message, lastBlockId, toolCallIds, lastCommitByBranch, skipBlocks) => {
|
|
637
|
-
let previousBlockId = lastBlockId;
|
|
638
|
-
return message.blocks.map((block, idx) => {
|
|
639
|
-
const branch = getMessageBranch(message);
|
|
640
|
-
const parents = getBlockParents(block, previousBlockId, message, toolCallIds, lastCommitByBranch);
|
|
641
|
-
if (skipBlocks?.includes(block._tag)) {
|
|
642
|
-
return null;
|
|
643
|
-
}
|
|
644
|
-
const commit = createBlockCommit(block, message, branch, parents, idx);
|
|
645
|
-
if (commit) {
|
|
646
|
-
previousBlockId = commit.id;
|
|
647
|
-
}
|
|
648
|
-
return commit;
|
|
649
|
-
}).filter(isNonNullable2);
|
|
650
|
-
};
|
|
651
|
-
var getBlockParents = (block, previousBlockId, message, toolCallIds, lastCommitByBranch) => {
|
|
652
|
-
const parents = [];
|
|
653
|
-
if (block._tag === "toolResult") {
|
|
654
|
-
if (previousBlockId) {
|
|
655
|
-
parents.push(previousBlockId);
|
|
656
|
-
}
|
|
657
|
-
const toolCallCommitId = toolCallIds?.get(block.toolCallId);
|
|
658
|
-
if (toolCallCommitId) {
|
|
659
|
-
const toolCallBranch = getToolCallBranchFromCommitId(toolCallCommitId, block.toolCallId);
|
|
660
|
-
const lastBlockFromToolCallBranch = lastCommitByBranch?.get(toolCallBranch);
|
|
661
|
-
if (lastBlockFromToolCallBranch) {
|
|
662
|
-
parents.push(lastBlockFromToolCallBranch);
|
|
663
|
-
} else {
|
|
664
|
-
parents.push(toolCallCommitId);
|
|
665
|
-
}
|
|
666
|
-
}
|
|
667
|
-
} else {
|
|
668
|
-
if (previousBlockId) {
|
|
669
|
-
parents.push(previousBlockId);
|
|
670
|
-
} else {
|
|
671
|
-
const toolCallParent = getParentId(message);
|
|
672
|
-
if (toolCallParent) {
|
|
673
|
-
parents.push(toolCallParent);
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
|
-
}
|
|
677
|
-
return parents;
|
|
678
|
-
};
|
|
679
|
-
var createBlockCommit = (block, message, branch, parents, idx) => {
|
|
680
|
-
const timestamp = new Date(message.created);
|
|
681
|
-
switch (block._tag) {
|
|
682
|
-
case "text":
|
|
683
|
-
if (!block.text.trim().length) return null;
|
|
684
|
-
return {
|
|
685
|
-
id: getGenericBlockId(message.id, idx),
|
|
686
|
-
branch,
|
|
687
|
-
parents,
|
|
688
|
-
timestamp,
|
|
689
|
-
...message.sender.role === "user" ? {
|
|
690
|
-
icon: "ph--user--regular",
|
|
691
|
-
tags: [
|
|
692
|
-
"user"
|
|
693
|
-
],
|
|
694
|
-
message: "Processing request..."
|
|
695
|
-
} : {
|
|
696
|
-
icon: "ph--robot--regular",
|
|
697
|
-
message: `Response (${block.text.split(" ").length} words)`
|
|
698
|
-
}
|
|
699
|
-
};
|
|
700
|
-
case "toolCall":
|
|
701
|
-
return {
|
|
702
|
-
id: getToolCallId(message.id, block.toolCallId),
|
|
703
|
-
branch,
|
|
704
|
-
parents,
|
|
705
|
-
timestamp,
|
|
706
|
-
icon: "ph--wrench--regular",
|
|
707
|
-
level: LogLevel.INFO,
|
|
708
|
-
message: `Calling tool (${block.name})`
|
|
709
|
-
};
|
|
710
|
-
case "toolResult":
|
|
711
|
-
return {
|
|
712
|
-
id: getToolResultId(message.id, block.toolCallId),
|
|
713
|
-
branch,
|
|
714
|
-
parents,
|
|
715
|
-
timestamp,
|
|
716
|
-
icon: block.error ? "ph--x-circle--regular" : "ph--check-circle--regular",
|
|
717
|
-
level: block.error ? LogLevel.ERROR : LogLevel.INFO,
|
|
718
|
-
message: block.error ? "Tool error: " + block.error : "Tool call succeeded"
|
|
719
|
-
};
|
|
720
|
-
case "summary":
|
|
721
|
-
return {
|
|
722
|
-
id: getGenericBlockId(message.id, idx),
|
|
723
|
-
branch,
|
|
724
|
-
parents,
|
|
725
|
-
timestamp,
|
|
726
|
-
icon: "ph--rocket--regular",
|
|
727
|
-
level: LogLevel.INFO,
|
|
728
|
-
message: ContentBlock.createSummaryMessage(block)
|
|
729
|
-
};
|
|
730
|
-
case "status":
|
|
731
|
-
return {
|
|
732
|
-
id: getGenericBlockId(message.id, idx),
|
|
733
|
-
branch,
|
|
734
|
-
parents,
|
|
735
|
-
timestamp,
|
|
736
|
-
message: block.statusText,
|
|
737
|
-
level: LogLevel.INFO,
|
|
738
|
-
icon: "ph--flag--regular"
|
|
739
|
-
};
|
|
740
|
-
case "reasoning":
|
|
741
|
-
return {
|
|
742
|
-
id: getGenericBlockId(message.id, idx),
|
|
743
|
-
branch,
|
|
744
|
-
parents,
|
|
745
|
-
timestamp,
|
|
746
|
-
message: block.reasoningText ?? "Thinking...",
|
|
747
|
-
icon: "ph--brain--regular"
|
|
748
|
-
};
|
|
749
|
-
case "reference":
|
|
750
|
-
return {
|
|
751
|
-
id: getGenericBlockId(message.id, idx),
|
|
752
|
-
branch,
|
|
753
|
-
parents,
|
|
754
|
-
timestamp,
|
|
755
|
-
icon: "ph--link--regular",
|
|
756
|
-
message: stringifyRef(block.reference)
|
|
757
|
-
};
|
|
758
|
-
default:
|
|
759
|
-
return null;
|
|
760
|
-
}
|
|
761
|
-
};
|
|
762
|
-
var getToolCallBranchFromCommitId = (toolCallCommitId, toolCallId) => {
|
|
763
|
-
const messageId = toolCallCommitId.split("_toolCall_")[0];
|
|
764
|
-
return `${messageId}_${toolCallId}`;
|
|
765
|
-
};
|
|
766
|
-
var getToolCallId = (messageId, toolCallId) => `${messageId}_toolCall_${toolCallId}`;
|
|
767
|
-
var getToolResultId = (messageId, toolCallId) => `${messageId}_toolResult_${toolCallId}`;
|
|
768
|
-
var getGenericBlockId = (messageId, idx) => `${messageId}_block_${idx}`;
|
|
769
|
-
var getBranchName = (options) => {
|
|
770
|
-
if (options.toolCallId) {
|
|
771
|
-
return options.toolCallId;
|
|
772
|
-
} else if (options.parentMessage) {
|
|
773
|
-
return options.parentMessage;
|
|
774
|
-
} else {
|
|
775
|
-
return "main";
|
|
776
|
-
}
|
|
777
|
-
};
|
|
778
|
-
var getMessageBranch = (message) => {
|
|
779
|
-
return getBranchName({
|
|
780
|
-
parentMessage: message.parentMessage,
|
|
781
|
-
toolCallId: message.properties?.[MESSAGE_PROPERTY_TOOL_CALL_ID]
|
|
782
|
-
});
|
|
783
|
-
};
|
|
784
|
-
var getParentId = (message) => {
|
|
785
|
-
if (message.parentMessage && message.properties?.[MESSAGE_PROPERTY_TOOL_CALL_ID]) {
|
|
786
|
-
return getToolCallId(message.parentMessage, message.properties[MESSAGE_PROPERTY_TOOL_CALL_ID]);
|
|
787
|
-
} else {
|
|
788
|
-
return void 0;
|
|
789
|
-
}
|
|
790
|
-
};
|
|
791
|
-
var stringifyRef = (ref) => {
|
|
792
|
-
if (ref.target) {
|
|
793
|
-
return stringifyObject(ref.target);
|
|
794
|
-
}
|
|
795
|
-
return ref.dxn.asEchoDXN()?.echoId ?? ref.dxn.asQueueDXN()?.objectId ?? "";
|
|
796
|
-
};
|
|
797
|
-
var stringifyObject = (obj) => {
|
|
798
|
-
return Obj3.getLabel(obj) ?? Obj3.getTypename(obj) ?? obj.id;
|
|
799
|
-
};
|
|
800
|
-
|
|
801
|
-
// src/templates/system.ts
|
|
802
|
-
import defaultsDeep from "lodash.defaultsdeep";
|
|
803
|
-
import { Template } from "@dxos/blueprints";
|
|
804
|
-
|
|
805
|
-
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/instructions/format.tpl?raw
|
|
806
|
-
var format_default = "{{! System Core }}\n\n## Input Formats\n\n### Blueprints\n\nYou may be provided with Blueprint definitions that include specific instructions and tools for a given set of tasks.\nBlue print instructions will be enclosed in a <blueprint> tag.\n\n### Context objects\n\nYou may be provided with references to objects that are relevant to the current prompt.\nObjects will be in the form of a <object> tag with a <dxn> element that is the unique key of the object required by many tools.\nThe <typename> element is the type of the object.\n\n<object>\n <dxn>object-id</dxn>\n <typename>example.com/type/TypeName</typename>\n</object>\n\n## Response Formats\n\nIt is very important to respond in the correct format.\nYou response must be valid Markdown.\nIn some cases you should respond with well-formed XML tags along with the content.\nCode snippets should use 2-space indents.\n\n{{#if cot}}\nBefore responding, explain your reasoning and include your detailed chain-of-thought enclosed in a <cot> tag.\nThe <cot> tag should be the first thing in your response.\n{{/if}}\n\n## Block references\n\nYou can reply with a block reference for objects if you have the DXN. For example:\n\n<object>\n <dxn>dxn:queue:data:B6INSIBY3CBEF4M5VZRYBCMAHQMPYK5AJ:01K24XMVHSZHS97SG1VTVQDM5Z:01K24XPK464FSCKVQJAB2H662M</dxn>\n</object>\n<object>\n <dxn>dxn:echo:@:01K24XPK464FSCKVQJAB2H662M</dxn>\n</object>\n\n### Suggestions\n\nEach suggestion must in the form of a proposed action enclosed in a <suggestion> tag.\nExample: <suggestion>Show on a map</suggestion>\nDo not use bullets when listing suggestions.\n\n### Multiple choice questions\n\nYou may ask multiple choice questions by wrapping consise options inside <option> tags inside a well formed <select> tag.\nExample: <select><option>Yes</option><option>No</option></select>\n\n## Toolkit\n\nIf the user asks for a list of tools, then just emit a single self-closing <toolkit/> tag instead of listing the tools.\n\n## Context\n\nThe current date and time is {{DATETIME}}.\n";
|
|
807
|
-
|
|
808
|
-
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/instructions/system.tpl?raw
|
|
809
|
-
var system_default = "{{! System Prompt }}\n\n- You are a helpful assistant called Kai. \n- Your name is Kai and you are powered by different language models.\n- You were created by DXOS and operate inside of Composer and on the DXOS EDGE network.\n- In your initial greeting state your name and make some suggestions based on the current context objects.\n- You are an advanced AI assistant capable of creating and managing artifacts from provided data and tools.\n- Your task is to process user commands and questions and decide how best to respond.\n- In general be concise and direct.\n- Follow all instructions carefully.\n\n## Planning\n\n- Analyze the structure and type of the content in the user's message.\n- Determine if you complete the task using the available blueprint definitions?\n- If you can't complete the task using the available blueprint definitions, query the list of available blueprint definitions using the appropriate tool.\n- Identify which blueprint definitions are relevant to the user's request.\n- Evaluate the potential benefits of creating an artifact vs. normal processing for each identified element.\n- Make a final decision on whether to create an artifact and explain your reasoning.\n- Are the required blueprint definitions already available?\n- If not, select which blueprint definition(s) will be the most relevant and require them using the require_artifact_definitions tool.\n- The require'd artifact tools will be available for use after require.\n- If creating an artifact, outline how you will structure it within the response.\n- Decide if the artifact needs to be shown to the user.\n- Call the show tool to show the artifact to the user.\n- Your reasoning must include: whether to use artifacts or not, to create one or query, whether to show the artifact to the user, and how to structure the response.\n\n## Blueprints and Artifacts\n\n- Determine if the interaction involves an artifact. Prefer artifacts for tables, lists, spreadsheets, kanbans, games, images, and other structured data.\n- Determine if the user is explicitly talking about creating a new artifact, or wants to use an existing artifact.\n- If it is ambiguous, query for existing artifacts first and then decide.\n- If you decide to create an artifact, call the associated tool to create the artifact.\n- Artifacts are stored in the database. Tools are used to create and query artifacts.\n- If you are unsure about creating an artifact ask the user for clarification.\n- Artifacts are mutable objects that can change over the course of the conversation.\n- Always re-query the artifact using the tool (like query or inspect) to get the latest state of the artifact before answering the user.\n- You must never generate the id of the artifact; only recall the ids that are already in the history.\n- Artifacts are created by requiring the specific artifact using the require_artifact tool and creating it by calling the associated tool.\n- You can add suggested actions at the end of your response.\n- Suggested actions should be very concise and start with a verb and be phrased as a command to an agent -- not a question to the user.\n- Suggested actions must be in the form of a user instruction that you can follow.\n- Suggested actions could include actions that create artifacts.\n- After creating an artifact use a tool to add it to the chat context.\n\n## Context\n\nToday is {{DATETIME}}.\n";
|
|
810
|
-
|
|
811
|
-
// src/templates/system.ts
|
|
812
|
-
var createSystemPrompt = (options = {}) => Template.process(format_default, defaultsDeep(options, {
|
|
813
|
-
DATETIME: (/* @__PURE__ */ new Date()).toISOString()
|
|
814
|
-
}));
|
|
815
|
-
var templates = {
|
|
816
|
-
/** Editable system prompt.*/
|
|
817
|
-
system: Template.make({
|
|
818
|
-
/**
|
|
819
|
-
* NOTE: We are forbidden from accessing randomness at model level in cloudflare, so we have to have pre-generated ids.
|
|
820
|
-
*/
|
|
821
|
-
id: "01K2J2FZWH39F55CS7B26GP1Q3",
|
|
822
|
-
source: system_default
|
|
823
|
-
})
|
|
824
|
-
};
|
|
825
|
-
export {
|
|
826
|
-
AiAssistantError,
|
|
827
|
-
AiContextBinder,
|
|
828
|
-
AiContextService,
|
|
829
|
-
AiConversation,
|
|
830
|
-
AiModelError,
|
|
831
|
-
AiSession,
|
|
832
|
-
ArtifactDiffResolver,
|
|
833
|
-
ArtifactId,
|
|
834
|
-
Bindings,
|
|
835
|
-
ContextBinding,
|
|
836
|
-
ExecutionGraph,
|
|
837
|
-
GenerationObserver,
|
|
838
|
-
GenericToolkit_exports as GenericToolkit,
|
|
839
|
-
RefFromLLM,
|
|
840
|
-
VersionPin,
|
|
841
|
-
applyDiffs,
|
|
842
|
-
computeDiffsWithCursors,
|
|
843
|
-
createArtifactElement,
|
|
844
|
-
createSystemPrompt,
|
|
845
|
-
createToolkit,
|
|
846
|
-
makeToolExecutionServiceFromFunctions,
|
|
847
|
-
makeToolResolverFromFunctions,
|
|
848
|
-
reduceDiffs,
|
|
849
|
-
templates
|
|
850
|
-
};
|
|
851
|
-
//# sourceMappingURL=index.mjs.map
|