@dxos/assistant 0.8.4-main.7ace549 → 0.8.4-main.937b3ca
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/browser/chunk-7CDDG6LL.mjs +578 -0
- package/dist/lib/browser/chunk-7CDDG6LL.mjs.map +7 -0
- package/dist/lib/browser/extraction/index.mjs +3 -3
- package/dist/lib/browser/extraction/index.mjs.map +3 -3
- package/dist/lib/browser/index.mjs +239 -678
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +32 -0
- package/dist/lib/browser/testing/index.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-CC7ER6Q3.mjs +579 -0
- package/dist/lib/node-esm/chunk-CC7ER6Q3.mjs.map +7 -0
- package/dist/lib/node-esm/extraction/index.mjs +3 -3
- package/dist/lib/node-esm/extraction/index.mjs.map +3 -3
- package/dist/lib/node-esm/index.mjs +239 -678
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +33 -0
- package/dist/lib/node-esm/testing/index.mjs.map +7 -0
- package/dist/types/src/conversation/context.d.ts +74 -42
- package/dist/types/src/conversation/context.d.ts.map +1 -1
- package/dist/types/src/conversation/context.test.d.ts +2 -0
- package/dist/types/src/conversation/context.test.d.ts.map +1 -0
- package/dist/types/src/conversation/conversation.d.ts +14 -14
- package/dist/types/src/conversation/conversation.d.ts.map +1 -1
- package/dist/types/src/conversation/conversation.test.d.ts +2 -0
- package/dist/types/src/conversation/conversation.test.d.ts.map +1 -0
- package/dist/types/src/errors.d.ts +12 -16
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +179 -178
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction.d.ts +285 -241
- package/dist/types/src/extraction/extraction.d.ts.map +1 -1
- package/dist/types/src/extraction/quotes.d.ts +2 -2
- package/dist/types/src/extraction/quotes.d.ts.map +1 -1
- package/dist/types/src/functions/services.d.ts +3 -3
- package/dist/types/src/functions/services.d.ts.map +1 -1
- package/dist/types/src/functions.test.d.ts +2 -0
- package/dist/types/src/functions.test.d.ts.map +1 -0
- package/dist/types/src/session/GenericToolkit.d.ts +1 -0
- package/dist/types/src/session/GenericToolkit.d.ts.map +1 -1
- package/dist/types/src/session/format.d.ts +3 -3
- package/dist/types/src/session/format.d.ts.map +1 -1
- package/dist/types/src/session/observer.d.ts +4 -3
- package/dist/types/src/session/observer.d.ts.map +1 -1
- package/dist/types/src/session/session.d.ts +14 -9
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/session/toolkit.d.ts +5 -6
- package/dist/types/src/session/toolkit.d.ts.map +1 -1
- package/dist/types/src/session/version-pin.d.ts +1 -1
- package/dist/types/src/session/version-pin.d.ts.map +1 -1
- package/dist/types/src/testing/index.d.ts +2 -0
- package/dist/types/src/testing/index.d.ts.map +1 -0
- package/dist/types/src/testing/layer.d.ts +21 -0
- package/dist/types/src/testing/layer.d.ts.map +1 -0
- package/dist/types/src/types/artifact.d.ts +12 -3
- package/dist/types/src/types/artifact.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +38 -29
- package/src/conversation/context.test.ts +46 -0
- package/src/conversation/context.ts +183 -65
- package/src/conversation/conversation.test.ts +65 -0
- package/src/conversation/conversation.ts +46 -49
- package/src/diff.test.ts +1 -1
- package/src/errors.ts +2 -2
- package/src/execution-graph.ts +2 -2
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +3 -3
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +1 -1
- package/src/extraction/extraction.ts +3 -3
- package/src/extraction/quotes.ts +3 -4
- package/src/functions/services.ts +28 -10
- package/src/functions.conversations.json +1 -0
- package/src/functions.test.ts +74 -0
- package/src/session/GenericToolkit.ts +1 -0
- package/src/session/format.test.ts +101 -38
- package/src/session/format.ts +11 -12
- package/src/session/observer.ts +3 -1
- package/src/session/session.conversations.json +1 -1
- package/src/session/session.test.ts +7 -13
- package/src/session/session.ts +33 -12
- package/src/session/toolkit.ts +13 -22
- package/src/session/version-pin.ts +2 -2
- package/src/testing/index.ts +5 -0
- package/src/testing/layer.ts +78 -0
- package/src/types/artifact.ts +18 -6
|
@@ -1,18 +1,31 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
AiAssistantError,
|
|
3
|
+
AiModelError,
|
|
4
|
+
AiSession,
|
|
5
|
+
ArtifactDiffResolver,
|
|
6
|
+
ArtifactId,
|
|
7
|
+
GenerationObserver,
|
|
8
|
+
GenericToolkit_exports,
|
|
9
|
+
RefFromLLM,
|
|
10
|
+
VersionPin,
|
|
11
|
+
createArtifactElement,
|
|
12
|
+
createToolkit,
|
|
13
|
+
makeToolExecutionServiceFromFunctions,
|
|
14
|
+
makeToolResolverFromFunctions
|
|
15
|
+
} from "./chunk-7CDDG6LL.mjs";
|
|
16
|
+
import "./chunk-J5LGTIGS.mjs";
|
|
4
17
|
|
|
5
18
|
// src/conversation/context.ts
|
|
6
|
-
import {
|
|
7
|
-
import * as
|
|
19
|
+
import { Atom, Registry } from "@effect-atom/atom-react";
|
|
20
|
+
import * as EArray from "effect/Array";
|
|
8
21
|
import * as Context from "effect/Context";
|
|
9
22
|
import * as Function from "effect/Function";
|
|
10
23
|
import * as Schema from "effect/Schema";
|
|
11
24
|
import { Blueprint } from "@dxos/blueprints";
|
|
12
25
|
import { Resource } from "@dxos/context";
|
|
13
|
-
import { DXN,
|
|
14
|
-
import {
|
|
15
|
-
import { ComplexSet } from "@dxos/util";
|
|
26
|
+
import { DXN, Obj, Query, Type } from "@dxos/echo";
|
|
27
|
+
import { log } from "@dxos/log";
|
|
28
|
+
import { ComplexSet, isTruthy } from "@dxos/util";
|
|
16
29
|
var __dxlog_file = "/__w/dxos/dxos/packages/core/assistant/src/conversation/context.ts";
|
|
17
30
|
var ContextBinding = Schema.Struct({
|
|
18
31
|
blueprints: Schema.Struct({
|
|
@@ -20,10 +33,10 @@ var ContextBinding = Schema.Struct({
|
|
|
20
33
|
removed: Schema.Array(Type.Ref(Blueprint.Blueprint))
|
|
21
34
|
}),
|
|
22
35
|
objects: Schema.Struct({
|
|
23
|
-
added: Schema.Array(Type.Ref(Obj
|
|
24
|
-
removed: Schema.Array(Type.Ref(Obj
|
|
36
|
+
added: Schema.Array(Type.Ref(Type.Obj)),
|
|
37
|
+
removed: Schema.Array(Type.Ref(Type.Obj))
|
|
25
38
|
})
|
|
26
|
-
}).pipe(Type.
|
|
39
|
+
}).pipe(Type.object({
|
|
27
40
|
typename: "dxos.org/type/ContextBinding",
|
|
28
41
|
version: "0.1.0"
|
|
29
42
|
}));
|
|
@@ -33,614 +46,324 @@ var Bindings = class {
|
|
|
33
46
|
objects = new ComplexSet((ref) => ref.dxn.asEchoDXN()?.echoId);
|
|
34
47
|
toJSON() {
|
|
35
48
|
return {
|
|
36
|
-
blueprints:
|
|
37
|
-
objects:
|
|
49
|
+
blueprints: EArray.fromIterable(this.blueprints).map((ref) => ref.dxn.toString()),
|
|
50
|
+
objects: EArray.fromIterable(this.objects).map((ref) => ref.dxn.toString())
|
|
38
51
|
};
|
|
39
52
|
}
|
|
40
53
|
};
|
|
41
54
|
var AiContextBinder = class extends Resource {
|
|
55
|
+
_blueprints = Atom.make([]).pipe(Atom.keepAlive);
|
|
56
|
+
_objects = Atom.make([]).pipe(Atom.keepAlive);
|
|
57
|
+
_registry;
|
|
42
58
|
_queue;
|
|
59
|
+
constructor(options) {
|
|
60
|
+
super();
|
|
61
|
+
this._queue = options.queue;
|
|
62
|
+
this._registry = options.registry ?? Registry.make();
|
|
63
|
+
}
|
|
43
64
|
/**
|
|
44
|
-
*
|
|
65
|
+
* Returns the blueprints atom for subscription.
|
|
45
66
|
*/
|
|
46
|
-
// TODO(burdon): Cache value?
|
|
47
|
-
_bindings;
|
|
48
|
-
_blueprints;
|
|
49
|
-
_objects;
|
|
50
|
-
constructor(_queue) {
|
|
51
|
-
super(), this._queue = _queue;
|
|
52
|
-
}
|
|
53
|
-
get bindings() {
|
|
54
|
-
invariant(this._bindings, "AiContextBinder not open", {
|
|
55
|
-
F: __dxlog_file,
|
|
56
|
-
L: 77,
|
|
57
|
-
S: this,
|
|
58
|
-
A: [
|
|
59
|
-
"this._bindings",
|
|
60
|
-
"'AiContextBinder not open'"
|
|
61
|
-
]
|
|
62
|
-
});
|
|
63
|
-
return this._bindings;
|
|
64
|
-
}
|
|
65
67
|
get blueprints() {
|
|
66
|
-
invariant(this._blueprints, "AiContextBinder not open", {
|
|
67
|
-
F: __dxlog_file,
|
|
68
|
-
L: 82,
|
|
69
|
-
S: this,
|
|
70
|
-
A: [
|
|
71
|
-
"this._blueprints",
|
|
72
|
-
"'AiContextBinder not open'"
|
|
73
|
-
]
|
|
74
|
-
});
|
|
75
68
|
return this._blueprints;
|
|
76
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Returns the objects atom for subscription.
|
|
72
|
+
*/
|
|
77
73
|
get objects() {
|
|
78
|
-
invariant(this._objects, "AiContextBinder not open", {
|
|
79
|
-
F: __dxlog_file,
|
|
80
|
-
L: 87,
|
|
81
|
-
S: this,
|
|
82
|
-
A: [
|
|
83
|
-
"this._objects",
|
|
84
|
-
"'AiContextBinder not open'"
|
|
85
|
-
]
|
|
86
|
-
});
|
|
87
74
|
return this._objects;
|
|
88
75
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
this._bindings = computed(() => this._reduce(query.objects));
|
|
95
|
-
this._blueprints = computed(() => [
|
|
96
|
-
...this.bindings.value.blueprints
|
|
97
|
-
]);
|
|
98
|
-
this._objects = computed(() => [
|
|
99
|
-
...this.bindings.value.objects
|
|
100
|
-
]);
|
|
76
|
+
/**
|
|
77
|
+
* Gets the current blueprints value.
|
|
78
|
+
*/
|
|
79
|
+
getBlueprints() {
|
|
80
|
+
return this._registry.get(this._blueprints);
|
|
101
81
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Gets the current objects value.
|
|
84
|
+
*/
|
|
85
|
+
getObjects() {
|
|
86
|
+
return this._registry.get(this._objects);
|
|
106
87
|
}
|
|
107
88
|
/**
|
|
108
|
-
*
|
|
89
|
+
* Subscribe to changes in blueprints.
|
|
109
90
|
*/
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
91
|
+
subscribeBlueprints(cb) {
|
|
92
|
+
return this._registry.subscribe(this._blueprints, () => cb(this._registry.get(this._blueprints)));
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Subscribe to changes in objects.
|
|
96
|
+
*/
|
|
97
|
+
subscribeObjects(cb) {
|
|
98
|
+
return this._registry.subscribe(this._objects, () => cb(this._registry.get(this._objects)));
|
|
99
|
+
}
|
|
100
|
+
async _open() {
|
|
101
|
+
const query = this._queue.query(Query.type(ContextBinding));
|
|
102
|
+
const initialResults = await query.run();
|
|
103
|
+
await this._updateBindings(initialResults);
|
|
104
|
+
this._ctx.onDispose(query.subscribe(async () => {
|
|
105
|
+
await this._updateBindings(query.results);
|
|
106
|
+
}));
|
|
107
|
+
}
|
|
108
|
+
async _updateBindings(items) {
|
|
109
|
+
if (items.length === 0) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const bindings = this._reduce(items);
|
|
113
|
+
const currentBlueprints = this._registry.get(this._blueprints);
|
|
114
|
+
const currentObjects = this._registry.get(this._objects);
|
|
115
|
+
const newBlueprints = await this._resolve(bindings.blueprints, currentBlueprints);
|
|
116
|
+
const newObjects = await this._resolve(bindings.objects, currentObjects);
|
|
117
|
+
this._registry.set(this._blueprints, newBlueprints);
|
|
118
|
+
this._registry.set(this._objects, newObjects);
|
|
119
|
+
log("updated bindings", {
|
|
120
|
+
blueprints: newBlueprints.length,
|
|
121
|
+
objects: newObjects.length
|
|
122
|
+
}, {
|
|
123
|
+
F: __dxlog_file,
|
|
124
|
+
L: 156,
|
|
125
|
+
S: this,
|
|
126
|
+
C: (f, a) => f(...a)
|
|
127
|
+
});
|
|
113
128
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
129
|
+
async _close() {
|
|
130
|
+
this._registry.set(this._blueprints, []);
|
|
131
|
+
this._registry.set(this._objects, []);
|
|
132
|
+
}
|
|
133
|
+
async bind({ blueprints, objects }) {
|
|
134
|
+
const currentBlueprints = this._registry.get(this._blueprints);
|
|
135
|
+
const currentObjects = this._registry.get(this._objects);
|
|
136
|
+
const { added: addedBlueprints, next: nextBlueprints } = this._processBindings(blueprints, currentBlueprints);
|
|
137
|
+
const { added: addedObjects, next: nextObjects } = this._processBindings(objects, currentObjects);
|
|
138
|
+
if (!addedBlueprints.length && !addedObjects.length) {
|
|
119
139
|
return;
|
|
120
140
|
}
|
|
141
|
+
this._registry.set(this._blueprints, nextBlueprints);
|
|
142
|
+
this._registry.set(this._objects, nextObjects);
|
|
143
|
+
log("bind", {
|
|
144
|
+
blueprints: addedBlueprints.length,
|
|
145
|
+
objects: addedObjects.length
|
|
146
|
+
}, {
|
|
147
|
+
F: __dxlog_file,
|
|
148
|
+
L: 182,
|
|
149
|
+
S: this,
|
|
150
|
+
C: (f, a) => f(...a)
|
|
151
|
+
});
|
|
121
152
|
await this._queue.append([
|
|
122
153
|
Obj.make(ContextBinding, {
|
|
123
154
|
blueprints: {
|
|
124
|
-
added:
|
|
155
|
+
added: addedBlueprints,
|
|
125
156
|
removed: []
|
|
126
157
|
},
|
|
127
158
|
objects: {
|
|
128
|
-
added:
|
|
159
|
+
added: addedObjects,
|
|
129
160
|
removed: []
|
|
130
161
|
}
|
|
131
162
|
})
|
|
132
163
|
]);
|
|
133
164
|
}
|
|
134
|
-
async unbind(
|
|
135
|
-
if (!
|
|
165
|
+
async unbind({ blueprints, objects }) {
|
|
166
|
+
if (!blueprints?.length && !objects?.length) {
|
|
136
167
|
return;
|
|
137
168
|
}
|
|
169
|
+
log("unbind", {
|
|
170
|
+
blueprints: blueprints?.length,
|
|
171
|
+
objects: objects?.length
|
|
172
|
+
}, {
|
|
173
|
+
F: __dxlog_file,
|
|
174
|
+
L: 202,
|
|
175
|
+
S: this,
|
|
176
|
+
C: (f, a) => f(...a)
|
|
177
|
+
});
|
|
138
178
|
await this._queue.append([
|
|
139
179
|
Obj.make(ContextBinding, {
|
|
140
180
|
blueprints: {
|
|
141
181
|
added: [],
|
|
142
|
-
removed:
|
|
182
|
+
removed: blueprints ?? []
|
|
143
183
|
},
|
|
144
184
|
objects: {
|
|
145
185
|
added: [],
|
|
146
|
-
removed:
|
|
186
|
+
removed: objects ?? []
|
|
147
187
|
}
|
|
148
188
|
})
|
|
149
189
|
]);
|
|
150
190
|
}
|
|
191
|
+
/**
|
|
192
|
+
* Process bindings to filter duplicates and determine next state.
|
|
193
|
+
*/
|
|
194
|
+
_processBindings(refs, current) {
|
|
195
|
+
const next = [
|
|
196
|
+
...current
|
|
197
|
+
];
|
|
198
|
+
const added = [];
|
|
199
|
+
if (!refs?.length) {
|
|
200
|
+
return {
|
|
201
|
+
added,
|
|
202
|
+
next
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
const seen = new Set(current.map((obj) => Obj.getDXN(obj).toString()));
|
|
206
|
+
for (const ref of refs) {
|
|
207
|
+
const dxn = ref.dxn.toString();
|
|
208
|
+
if (!seen.has(dxn)) {
|
|
209
|
+
seen.add(dxn);
|
|
210
|
+
added.push(ref);
|
|
211
|
+
if (ref.isAvailable) {
|
|
212
|
+
const target = ref.target;
|
|
213
|
+
if (target) {
|
|
214
|
+
next.push(target);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return {
|
|
220
|
+
added,
|
|
221
|
+
next
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Reduce results into sets of blueprints and objects.
|
|
226
|
+
*/
|
|
151
227
|
_reduce(items) {
|
|
152
|
-
return Function.pipe(items,
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
228
|
+
return Function.pipe(items, EArray.reduce(new Bindings(), (context, { blueprints, objects }) => {
|
|
229
|
+
blueprints.added.forEach((ref) => context.blueprints.add(ref));
|
|
230
|
+
blueprints.removed.forEach((ref) => context.blueprints.delete(ref));
|
|
231
|
+
objects.added.forEach((ref) => context.objects.add(ref));
|
|
232
|
+
objects.removed.forEach((ref) => {
|
|
156
233
|
for (const obj of context.objects) {
|
|
157
234
|
if (DXN.equalsEchoId(obj.dxn, ref.dxn)) {
|
|
158
235
|
context.objects.delete(obj);
|
|
159
236
|
}
|
|
160
237
|
}
|
|
161
238
|
});
|
|
162
|
-
item.objects.added.forEach((ref) => context.objects.add(ref));
|
|
163
239
|
return context;
|
|
164
240
|
}));
|
|
165
241
|
}
|
|
166
|
-
};
|
|
167
|
-
var AiContextService = class extends Context.Tag("@dxos/assistant/AiContextService")() {
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
// src/conversation/conversation.ts
|
|
171
|
-
import * as Array5 from "effect/Array";
|
|
172
|
-
import * as Effect6 from "effect/Effect";
|
|
173
|
-
import * as Option2 from "effect/Option";
|
|
174
|
-
import { Resource as Resource2 } from "@dxos/context";
|
|
175
|
-
import { Obj as Obj4 } from "@dxos/echo";
|
|
176
|
-
import { DatabaseService as DatabaseService2 } from "@dxos/echo-db";
|
|
177
|
-
import { log as log3 } from "@dxos/log";
|
|
178
|
-
import { Message as Message3 } from "@dxos/types";
|
|
179
|
-
|
|
180
|
-
// src/session/artifact-diff.ts
|
|
181
|
-
import * as Context2 from "effect/Context";
|
|
182
|
-
var ArtifactDiffResolver = class extends Context2.Tag("@dxos/assistant/ArtifactDiffResolver")() {
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
// src/session/observer.ts
|
|
186
|
-
import * as Effect from "effect/Effect";
|
|
187
|
-
import * as Function2 from "effect/Function";
|
|
188
|
-
var GenerationObserver = Object.freeze({
|
|
189
|
-
make: ({ onBegin = Function2.constant(Effect.void), onPart = Function2.constant(Effect.void), onBlock = Function2.constant(Effect.void), onEnd = Function2.constant(Effect.void), onMessage = Function2.constant(Effect.void) } = {}) => ({
|
|
190
|
-
onBegin,
|
|
191
|
-
onPart,
|
|
192
|
-
onBlock,
|
|
193
|
-
onEnd,
|
|
194
|
-
onMessage
|
|
195
|
-
}),
|
|
196
|
-
noop: () => GenerationObserver.make(),
|
|
197
242
|
/**
|
|
198
|
-
*
|
|
243
|
+
* Resolve references to objects, loading them first if needed and falling back to existing objects.
|
|
199
244
|
*/
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
return;
|
|
204
|
-
}
|
|
205
|
-
printer.printContentBlock(block);
|
|
206
|
-
}),
|
|
207
|
-
onMessage: (message) => Effect.sync(() => {
|
|
208
|
-
if (message.sender.role === "assistant") {
|
|
209
|
-
return;
|
|
210
|
-
}
|
|
211
|
-
printer.printMessage(message);
|
|
212
|
-
})
|
|
213
|
-
})
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
// src/session/session.ts
|
|
217
|
-
import * as LanguageModel from "@effect/ai/LanguageModel";
|
|
218
|
-
import * as Chunk from "effect/Chunk";
|
|
219
|
-
import * as Effect3 from "effect/Effect";
|
|
220
|
-
import * as Stream from "effect/Stream";
|
|
221
|
-
import { AiParser, AiPreprocessor, callTool, getToolCalls } from "@dxos/ai";
|
|
222
|
-
import { todo } from "@dxos/debug";
|
|
223
|
-
import { Obj as Obj3 } from "@dxos/echo";
|
|
224
|
-
import { TracingService } from "@dxos/functions";
|
|
225
|
-
import { log as log2 } from "@dxos/log";
|
|
226
|
-
import { Message as Message2 } from "@dxos/types";
|
|
227
|
-
|
|
228
|
-
// src/session/format.ts
|
|
229
|
-
import * as Array4 from "effect/Array";
|
|
230
|
-
import * as Effect2 from "effect/Effect";
|
|
231
|
-
import * as Function3 from "effect/Function";
|
|
232
|
-
import * as Option from "effect/Option";
|
|
233
|
-
import { Template } from "@dxos/blueprints";
|
|
234
|
-
import { Obj as Obj2 } from "@dxos/echo";
|
|
235
|
-
import { DatabaseService, ObjectVersion } from "@dxos/echo-db";
|
|
236
|
-
import { log } from "@dxos/log";
|
|
237
|
-
import { Message } from "@dxos/types";
|
|
238
|
-
import { trim } from "@dxos/util";
|
|
239
|
-
|
|
240
|
-
// src/errors.ts
|
|
241
|
-
import { BaseError } from "@dxos/errors";
|
|
242
|
-
var AiModelError = class extends BaseError.extend("AI_MODEL_ERROR", "AI model error") {
|
|
243
|
-
};
|
|
244
|
-
var AiAssistantError = class extends BaseError.extend("AI_ASSISTANT_ERROR", "AI assistant error") {
|
|
245
|
-
};
|
|
246
|
-
|
|
247
|
-
// src/session/format.ts
|
|
248
|
-
var __dxlog_file2 = "/__w/dxos/dxos/packages/core/assistant/src/session/format.ts";
|
|
249
|
-
var formatSystemPrompt = ({ system, blueprints = [], objects = [] }) => Effect2.gen(function* () {
|
|
250
|
-
const blueprintDefs = yield* Function3.pipe(blueprints, Effect2.forEach((blueprint) => Effect2.succeed(blueprint.instructions)), Effect2.flatMap(Effect2.forEach((template) => DatabaseService.loadOption(template.source))), Effect2.map(Array4.filter(Option.isSome)), Effect2.map(Array4.map((template) => trim`
|
|
251
|
-
<blueprint>
|
|
252
|
-
${Template.process(template.value.content)}
|
|
253
|
-
</blueprint>
|
|
254
|
-
`)), Effect2.map((blueprints2) => blueprints2.length > 0 ? [
|
|
255
|
-
"## Blueprints Definitions",
|
|
256
|
-
...blueprints2
|
|
257
|
-
].join("\n\n") : void 0));
|
|
258
|
-
const objectDefs = yield* Function3.pipe(objects, Effect2.forEach((object) => Effect2.succeed(trim`
|
|
259
|
-
<object>
|
|
260
|
-
<dxn>${Obj2.getDXN(object)}</dxn>
|
|
261
|
-
<typename>${Obj2.getTypename(object)}</typename>
|
|
262
|
-
</object>
|
|
263
|
-
`)), Effect2.map((objects2) => objects2.length > 0 ? [
|
|
264
|
-
"## Context Objects",
|
|
265
|
-
...objects2
|
|
266
|
-
].join("\n\n") : void 0));
|
|
267
|
-
return yield* Function3.pipe(Effect2.succeed([
|
|
268
|
-
system,
|
|
269
|
-
blueprintDefs,
|
|
270
|
-
objectDefs
|
|
271
|
-
].filter((def) => def !== void 0)), Effect2.map((parts) => parts.join("\n\n")));
|
|
272
|
-
});
|
|
273
|
-
var formatUserPrompt = ({ prompt, history = [] }) => Effect2.gen(function* () {
|
|
274
|
-
const blocks = [];
|
|
275
|
-
const artifactDiffResolver = yield* Effect2.serviceOption(ArtifactDiffResolver);
|
|
276
|
-
if (Option.isSome(artifactDiffResolver)) {
|
|
277
|
-
const versions = gatherObjectVersions(history);
|
|
278
|
-
const artifactDiff = yield* Effect2.tryPromise({
|
|
279
|
-
try: () => artifactDiffResolver.value.resolve([
|
|
280
|
-
...versions.entries()
|
|
281
|
-
].map(([id, version]) => ({
|
|
282
|
-
id,
|
|
283
|
-
lastVersion: version
|
|
284
|
-
}))),
|
|
285
|
-
catch: AiAssistantError.wrap({
|
|
286
|
-
message: "Artifact diff resolution error"
|
|
287
|
-
})
|
|
288
|
-
});
|
|
289
|
-
log("version", {
|
|
290
|
-
artifactDiff,
|
|
291
|
-
versions
|
|
292
|
-
}, {
|
|
293
|
-
F: __dxlog_file2,
|
|
294
|
-
L: 95,
|
|
295
|
-
S: this,
|
|
296
|
-
C: (f, a) => f(...a)
|
|
297
|
-
});
|
|
298
|
-
for (const [id, { version }] of [
|
|
299
|
-
...artifactDiff.entries()
|
|
300
|
-
]) {
|
|
301
|
-
if (ObjectVersion.equals(version, versions.get(id))) {
|
|
302
|
-
artifactDiff.delete(id);
|
|
303
|
-
continue;
|
|
304
|
-
}
|
|
305
|
-
blocks.push({
|
|
306
|
-
_tag: "anchor",
|
|
307
|
-
objectId: id,
|
|
308
|
-
version
|
|
309
|
-
});
|
|
310
|
-
}
|
|
311
|
-
if (artifactDiff.size > 0) {
|
|
312
|
-
blocks.push(createArtifactUpdateBlock(artifactDiff));
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
return Obj2.make(Message.Message, {
|
|
316
|
-
created: (/* @__PURE__ */ new Date()).toISOString(),
|
|
317
|
-
sender: {
|
|
318
|
-
role: "user"
|
|
319
|
-
},
|
|
320
|
-
blocks: [
|
|
321
|
-
...blocks,
|
|
322
|
-
{
|
|
323
|
-
_tag: "text",
|
|
324
|
-
text: prompt
|
|
325
|
-
}
|
|
326
|
-
]
|
|
327
|
-
});
|
|
328
|
-
});
|
|
329
|
-
var gatherObjectVersions = (messages) => {
|
|
330
|
-
const artifactIds = /* @__PURE__ */ new Map();
|
|
331
|
-
for (const message of messages) {
|
|
332
|
-
for (const block of message.blocks) {
|
|
333
|
-
if (block._tag === "anchor") {
|
|
334
|
-
artifactIds.set(block.objectId, block.version);
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
return artifactIds;
|
|
339
|
-
};
|
|
340
|
-
var createArtifactUpdateBlock = (artifactDiff) => {
|
|
341
|
-
return {
|
|
342
|
-
_tag: "text",
|
|
343
|
-
// TODO(dmaretskyi): Does this need to be a special content-block?
|
|
344
|
-
disposition: "artifact-update",
|
|
345
|
-
text: trim`
|
|
346
|
-
The following artifacts have been updated since the last message:
|
|
347
|
-
${[
|
|
348
|
-
...artifactDiff.entries()
|
|
349
|
-
].map(([id, { diff }]) => `<changed-artifact id="${id}">${diff ? `
|
|
350
|
-
${diff}` : ""}</changed-artifact>`).join("\n")}
|
|
351
|
-
`
|
|
352
|
-
};
|
|
353
|
-
};
|
|
354
|
-
|
|
355
|
-
// src/session/session.ts
|
|
356
|
-
var __dxlog_file3 = "/__w/dxos/dxos/packages/core/assistant/src/session/session.ts";
|
|
357
|
-
var AiSession = class {
|
|
358
|
-
_options;
|
|
359
|
-
/** Prevents concurrent execution of session. */
|
|
360
|
-
_semaphore = Effect3.runSync(Effect3.makeSemaphore(1));
|
|
361
|
-
/** Message history from queue. */
|
|
362
|
-
// TODO(burdon): Evolve into supporting a git-like graph of messages.
|
|
363
|
-
_history = [];
|
|
364
|
-
/** Pending messages for this session (incl. the current prompt). */
|
|
365
|
-
_pending = [];
|
|
366
|
-
constructor(_options = {}) {
|
|
367
|
-
this._options = _options;
|
|
368
|
-
}
|
|
369
|
-
run = ({ prompt, system: systemTemplate, history = [], objects = [], blueprints = [], toolkit, observer = GenerationObserver.noop() }) => Effect3.gen(this, function* () {
|
|
370
|
-
this._history = [
|
|
371
|
-
...history
|
|
245
|
+
async _resolve(refs, current) {
|
|
246
|
+
const refArray = [
|
|
247
|
+
...refs
|
|
372
248
|
];
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
yield* TracingService.emitConverationMessage(message);
|
|
379
|
-
return message;
|
|
380
|
-
});
|
|
381
|
-
const promptMessage = yield* submitMessage(yield* formatUserPrompt({
|
|
382
|
-
prompt,
|
|
383
|
-
history
|
|
384
|
-
}));
|
|
385
|
-
const system = yield* formatSystemPrompt({
|
|
386
|
-
system: systemTemplate,
|
|
387
|
-
blueprints,
|
|
388
|
-
objects
|
|
389
|
-
});
|
|
390
|
-
do {
|
|
391
|
-
log2("request", {
|
|
392
|
-
prompt: promptMessage,
|
|
393
|
-
system: {
|
|
394
|
-
snippet: createSnippet(system),
|
|
395
|
-
length: system.length
|
|
396
|
-
},
|
|
397
|
-
pending: this._pending.length,
|
|
398
|
-
history: this._history.length,
|
|
399
|
-
objects: objects?.length ?? 0
|
|
400
|
-
}, {
|
|
401
|
-
F: __dxlog_file3,
|
|
402
|
-
L: 110,
|
|
403
|
-
S: this,
|
|
404
|
-
C: (f, a) => f(...a)
|
|
405
|
-
});
|
|
406
|
-
const prompt2 = yield* AiPreprocessor.preprocessPrompt([
|
|
407
|
-
...this._history,
|
|
408
|
-
...this._pending
|
|
409
|
-
], {
|
|
410
|
-
system
|
|
411
|
-
});
|
|
412
|
-
const blocks = yield* LanguageModel.streamText({
|
|
413
|
-
prompt: prompt2,
|
|
414
|
-
toolkit,
|
|
415
|
-
disableToolCallResolution: true
|
|
416
|
-
}).pipe(
|
|
417
|
-
// TOOD(dmaretskyi): Error mapping.
|
|
418
|
-
AiParser.parseResponse({
|
|
419
|
-
onBegin: () => observer.onBegin(),
|
|
420
|
-
onBlock: (block) => observer.onBlock(block),
|
|
421
|
-
onPart: (part) => observer.onPart(part),
|
|
422
|
-
onEnd: (summary) => observer.onEnd(summary)
|
|
423
|
-
}),
|
|
424
|
-
Stream.runCollect,
|
|
425
|
-
Effect3.map(Chunk.toArray)
|
|
426
|
-
);
|
|
427
|
-
const response = yield* submitMessage(Obj3.make(Message2.Message, {
|
|
428
|
-
created: (/* @__PURE__ */ new Date()).toISOString(),
|
|
429
|
-
sender: {
|
|
430
|
-
role: "assistant"
|
|
431
|
-
},
|
|
432
|
-
blocks
|
|
433
|
-
}));
|
|
434
|
-
const toolCalls = getToolCalls(response);
|
|
435
|
-
if (toolCalls.length === 0) {
|
|
436
|
-
break;
|
|
437
|
-
} else if (!toolkit) {
|
|
438
|
-
throw new Error("No toolkit provided");
|
|
249
|
+
await Promise.all(refArray.map((ref) => ref.load()));
|
|
250
|
+
return refArray.map((ref) => {
|
|
251
|
+
let target;
|
|
252
|
+
if (ref.isAvailable) {
|
|
253
|
+
target = ref.target;
|
|
439
254
|
}
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
...context,
|
|
443
|
-
parentMessage: response.id,
|
|
444
|
-
toolCallId: toolCall.toolCallId
|
|
445
|
-
}))));
|
|
446
|
-
});
|
|
447
|
-
yield* submitMessage(Obj3.make(Message2.Message, {
|
|
448
|
-
created: (/* @__PURE__ */ new Date()).toISOString(),
|
|
449
|
-
sender: {
|
|
450
|
-
role: "tool"
|
|
451
|
-
},
|
|
452
|
-
blocks: toolResults
|
|
453
|
-
}));
|
|
454
|
-
} while (true);
|
|
455
|
-
log2("done", {
|
|
456
|
-
pending: this._pending.length
|
|
457
|
-
}, {
|
|
458
|
-
F: __dxlog_file3,
|
|
459
|
-
L: 181,
|
|
460
|
-
S: this,
|
|
461
|
-
C: (f, a) => f(...a)
|
|
462
|
-
});
|
|
463
|
-
return this._pending;
|
|
464
|
-
}).pipe(this._semaphore.withPermits(1), Effect3.withSpan("AiSession.run"));
|
|
465
|
-
/**
|
|
466
|
-
* @deprecated
|
|
467
|
-
*/
|
|
468
|
-
// TODO(burdon): Implement or remove.
|
|
469
|
-
async runStructured(_schema, _options) {
|
|
470
|
-
return todo();
|
|
471
|
-
}
|
|
472
|
-
};
|
|
473
|
-
var createSnippet = (text, len = 32) => text.length <= len * 2 ? text : [
|
|
474
|
-
text.slice(0, len),
|
|
475
|
-
"...",
|
|
476
|
-
text.slice(-len)
|
|
477
|
-
].join("");
|
|
478
|
-
|
|
479
|
-
// src/session/toolkit.ts
|
|
480
|
-
import * as Toolkit from "@effect/ai/Toolkit";
|
|
481
|
-
import * as Effect4 from "effect/Effect";
|
|
482
|
-
import { ToolExecutionService, ToolResolverService } from "@dxos/ai";
|
|
483
|
-
import { isTruthy } from "@dxos/util";
|
|
484
|
-
var createToolkit = ({ toolkit: toolkitParam, toolIds = [], blueprints = [] }) => Effect4.gen(function* () {
|
|
485
|
-
const blueprintToolkit = yield* ToolResolverService.resolveToolkit([
|
|
486
|
-
...blueprints.flatMap(({ tools }) => tools),
|
|
487
|
-
...toolIds
|
|
488
|
-
]);
|
|
489
|
-
const blueprintToolkitHandler = yield* blueprintToolkit.toContext(ToolExecutionService.handlersFor(blueprintToolkit));
|
|
490
|
-
const toolkit = Toolkit.merge(...[
|
|
491
|
-
toolkitParam,
|
|
492
|
-
blueprintToolkit
|
|
493
|
-
].filter(isTruthy));
|
|
494
|
-
return yield* toolkit.pipe(Effect4.provide(blueprintToolkitHandler));
|
|
495
|
-
});
|
|
496
|
-
|
|
497
|
-
// src/session/version-pin.ts
|
|
498
|
-
import * as Schema2 from "effect/Schema";
|
|
499
|
-
import { getVersion } from "@dxos/echo-db";
|
|
500
|
-
import { ObjectId } from "@dxos/keys";
|
|
501
|
-
var ObjectVersionSchema = Schema2.Unknown;
|
|
502
|
-
var VersionPinSchema = Schema2.Struct({
|
|
503
|
-
// TODO(dmaretskyi): Use Ref when those support encoding.
|
|
504
|
-
objectId: ObjectId,
|
|
505
|
-
// TODO(dmaretskyi): Could be opaque.
|
|
506
|
-
version: ObjectVersionSchema
|
|
507
|
-
});
|
|
508
|
-
var VersionPin = class extends VersionPinSchema {
|
|
509
|
-
static DISPOSITION = "version-pin";
|
|
510
|
-
static fromObject(object) {
|
|
511
|
-
return VersionPin.make({
|
|
512
|
-
objectId: object.id,
|
|
513
|
-
version: getVersion(object)
|
|
514
|
-
});
|
|
255
|
+
return target ?? current.find((obj) => Obj.getDXN(obj).toString() === ref.dxn.toString());
|
|
256
|
+
}).filter(isTruthy);
|
|
515
257
|
}
|
|
516
|
-
static createBlock(pin) {
|
|
517
|
-
return {
|
|
518
|
-
_tag: "json",
|
|
519
|
-
disposition: VersionPin.DISPOSITION,
|
|
520
|
-
data: JSON.stringify(pin)
|
|
521
|
-
};
|
|
522
|
-
}
|
|
523
|
-
};
|
|
524
|
-
|
|
525
|
-
// src/session/GenericToolkit.ts
|
|
526
|
-
var GenericToolkit_exports = {};
|
|
527
|
-
__export(GenericToolkit_exports, {
|
|
528
|
-
TypeId: () => TypeId,
|
|
529
|
-
make: () => make,
|
|
530
|
-
merge: () => merge3
|
|
531
|
-
});
|
|
532
|
-
import * as Toolkit2 from "@effect/ai/Toolkit";
|
|
533
|
-
import * as Effect5 from "effect/Effect";
|
|
534
|
-
import * as Layer from "effect/Layer";
|
|
535
|
-
import * as Pipeable from "effect/Pipeable";
|
|
536
|
-
var TypeId = "~@dxos/assistant/GenericToolkit";
|
|
537
|
-
var make = (toolkit, layer) => {
|
|
538
|
-
return {
|
|
539
|
-
[TypeId]: TypeId,
|
|
540
|
-
toolkit,
|
|
541
|
-
layer,
|
|
542
|
-
handlers: toolkit.pipe(Effect5.provide(layer)),
|
|
543
|
-
pipe() {
|
|
544
|
-
return Pipeable.pipeArguments(this, arguments);
|
|
545
|
-
}
|
|
546
|
-
};
|
|
547
258
|
};
|
|
548
|
-
var
|
|
549
|
-
return make(Toolkit2.merge(...toolkits.map((t) => t.toolkit)), Layer.mergeAll(...toolkits.map((t) => t.layer)));
|
|
259
|
+
var AiContextService = class extends Context.Tag("@dxos/assistant/AiContextService")() {
|
|
550
260
|
};
|
|
551
261
|
|
|
552
262
|
// src/conversation/conversation.ts
|
|
553
|
-
|
|
263
|
+
import * as Effect from "effect/Effect";
|
|
264
|
+
import { Resource as Resource2 } from "@dxos/context";
|
|
265
|
+
import { Obj as Obj2 } from "@dxos/echo";
|
|
266
|
+
import { invariant } from "@dxos/invariant";
|
|
267
|
+
import { log as log2 } from "@dxos/log";
|
|
268
|
+
import { Message } from "@dxos/types";
|
|
269
|
+
var __dxlog_file2 = "/__w/dxos/dxos/packages/core/assistant/src/conversation/conversation.ts";
|
|
554
270
|
var AiConversation = class extends Resource2 {
|
|
555
271
|
/**
|
|
556
|
-
*
|
|
272
|
+
* Blueprints and objects bound to the conversation.
|
|
557
273
|
*/
|
|
274
|
+
_binder;
|
|
558
275
|
_queue;
|
|
559
|
-
/**
|
|
560
|
-
* Blueprints bound to the conversation.
|
|
561
|
-
*/
|
|
562
|
-
_context;
|
|
563
|
-
/**
|
|
564
|
-
* Toolkit from the current session request.
|
|
565
|
-
*/
|
|
566
276
|
_toolkit;
|
|
567
|
-
constructor(
|
|
277
|
+
constructor(options) {
|
|
568
278
|
super();
|
|
569
|
-
this._queue = queue;
|
|
570
|
-
this.
|
|
279
|
+
this._queue = options.queue;
|
|
280
|
+
this._toolkit = options.toolkit;
|
|
281
|
+
invariant(this._queue, void 0, {
|
|
282
|
+
F: __dxlog_file2,
|
|
283
|
+
L: 54,
|
|
284
|
+
S: this,
|
|
285
|
+
A: [
|
|
286
|
+
"this._queue",
|
|
287
|
+
""
|
|
288
|
+
]
|
|
289
|
+
});
|
|
290
|
+
this._binder = new AiContextBinder({
|
|
291
|
+
queue: this._queue,
|
|
292
|
+
registry: options.registry
|
|
293
|
+
});
|
|
571
294
|
}
|
|
572
295
|
async _open() {
|
|
573
|
-
await this.
|
|
296
|
+
await this._binder.open(this._ctx);
|
|
574
297
|
}
|
|
575
|
-
|
|
576
|
-
|
|
298
|
+
get queue() {
|
|
299
|
+
return this._queue;
|
|
577
300
|
}
|
|
578
301
|
get context() {
|
|
579
|
-
return this.
|
|
302
|
+
return this._binder;
|
|
580
303
|
}
|
|
581
304
|
get toolkit() {
|
|
582
305
|
return this._toolkit;
|
|
583
306
|
}
|
|
584
307
|
async getHistory() {
|
|
585
308
|
const queueItems = await this._queue.queryObjects();
|
|
586
|
-
return queueItems.filter(
|
|
309
|
+
return queueItems.filter(Obj2.instanceOf(Message.Message));
|
|
587
310
|
}
|
|
588
311
|
/**
|
|
589
312
|
* Creates a new cancelable request effect.
|
|
590
313
|
*/
|
|
591
314
|
createRequest(params) {
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
const history = yield*
|
|
595
|
-
const
|
|
596
|
-
const blueprints = yield* Effect6.forEach(context.blueprints.values(), DatabaseService2.loadOption).pipe(Effect6.map(Array5.filter(Option2.isSome)), Effect6.map(Array5.map((option) => option.value)));
|
|
597
|
-
const objects = yield* Effect6.forEach(context.objects.values(), DatabaseService2.loadOption).pipe(Effect6.map(Array5.filter(Option2.isSome)), Effect6.map(Array5.map((option) => option.value)));
|
|
315
|
+
const self = this;
|
|
316
|
+
return Effect.gen(function* () {
|
|
317
|
+
const history = yield* Effect.promise(() => self.getHistory());
|
|
318
|
+
const blueprints = self.context.getBlueprints();
|
|
598
319
|
const toolkit = yield* createToolkit({
|
|
320
|
+
toolkit: self._toolkit,
|
|
599
321
|
blueprints
|
|
600
322
|
});
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
log3("run", {
|
|
323
|
+
const objects = self.context.getObjects();
|
|
324
|
+
log2("run", {
|
|
604
325
|
history: history.length,
|
|
605
326
|
blueprints: blueprints.length,
|
|
606
|
-
|
|
607
|
-
|
|
327
|
+
tools: Object.keys(toolkit.tools).length,
|
|
328
|
+
objects: objects.length
|
|
608
329
|
}, {
|
|
609
|
-
F:
|
|
610
|
-
L:
|
|
330
|
+
F: __dxlog_file2,
|
|
331
|
+
L: 99,
|
|
611
332
|
S: this,
|
|
612
333
|
C: (f, a) => f(...a)
|
|
613
334
|
});
|
|
335
|
+
const session = new AiSession();
|
|
614
336
|
const messages = yield* session.run({
|
|
615
337
|
history,
|
|
616
338
|
blueprints,
|
|
617
|
-
objects,
|
|
618
339
|
toolkit,
|
|
340
|
+
objects,
|
|
619
341
|
...params
|
|
620
|
-
}).pipe(
|
|
621
|
-
binder:
|
|
342
|
+
}).pipe(Effect.provideService(AiContextService, {
|
|
343
|
+
binder: self.context
|
|
622
344
|
}));
|
|
623
|
-
|
|
345
|
+
log2("result", {
|
|
624
346
|
messages: messages.length,
|
|
625
|
-
duration:
|
|
347
|
+
duration: session.duration,
|
|
348
|
+
toolCalls: session.toolCalls
|
|
626
349
|
}, {
|
|
627
|
-
F:
|
|
628
|
-
L:
|
|
350
|
+
F: __dxlog_file2,
|
|
351
|
+
L: 114,
|
|
629
352
|
S: this,
|
|
630
353
|
C: (f, a) => f(...a)
|
|
631
354
|
});
|
|
632
|
-
yield*
|
|
355
|
+
yield* Effect.promise(() => self._queue.append(messages));
|
|
633
356
|
return messages;
|
|
634
|
-
})
|
|
357
|
+
});
|
|
635
358
|
}
|
|
636
359
|
};
|
|
637
360
|
|
|
638
361
|
// src/diff.ts
|
|
639
362
|
import { next as A } from "@automerge/automerge";
|
|
640
363
|
import { DocAccessor, toCursorRange } from "@dxos/echo-db";
|
|
641
|
-
import { log as
|
|
364
|
+
import { log as log3 } from "@dxos/log";
|
|
642
365
|
import { isNonNullable } from "@dxos/util";
|
|
643
|
-
var
|
|
366
|
+
var __dxlog_file3 = "/__w/dxos/dxos/packages/core/assistant/src/diff.ts";
|
|
644
367
|
var reduceDiffs = (diffs) => {
|
|
645
368
|
return diffs.reduce((acc, diff) => {
|
|
646
369
|
const current = acc[acc.length - 1];
|
|
@@ -692,8 +415,8 @@ var applyDiffs = (accessor, diffs) => {
|
|
|
692
415
|
if (idx !== -1) {
|
|
693
416
|
A.splice(doc, accessor.path, idx, diff.match.length, diff.replace);
|
|
694
417
|
} else {
|
|
695
|
-
|
|
696
|
-
F:
|
|
418
|
+
log3.warn("diff not found", diff, {
|
|
419
|
+
F: __dxlog_file3,
|
|
697
420
|
L: 78,
|
|
698
421
|
S: void 0,
|
|
699
422
|
C: (f, a) => f(...a)
|
|
@@ -706,10 +429,10 @@ var applyDiffs = (accessor, diffs) => {
|
|
|
706
429
|
|
|
707
430
|
// src/execution-graph.ts
|
|
708
431
|
import { AgentStatus } from "@dxos/ai";
|
|
709
|
-
import { Obj as
|
|
432
|
+
import { Obj as Obj3 } from "@dxos/echo";
|
|
710
433
|
import { MESSAGE_PROPERTY_TOOL_CALL_ID } from "@dxos/functions";
|
|
711
434
|
import { LogLevel } from "@dxos/log";
|
|
712
|
-
import { ContentBlock, Message as
|
|
435
|
+
import { ContentBlock, Message as Message2 } from "@dxos/types";
|
|
713
436
|
import { isNonNullable as isNonNullable2 } from "@dxos/util";
|
|
714
437
|
var SKIP_BLOCKS = [
|
|
715
438
|
"text"
|
|
@@ -733,9 +456,9 @@ var ExecutionGraph = class {
|
|
|
733
456
|
addEvents(events) {
|
|
734
457
|
const sortedEvents = this.sortObjectsByCreated(events);
|
|
735
458
|
for (const event of sortedEvents) {
|
|
736
|
-
if (
|
|
459
|
+
if (Obj3.instanceOf(Message2.Message, event)) {
|
|
737
460
|
this._processMessage(event);
|
|
738
|
-
} else if (
|
|
461
|
+
} else if (Obj3.instanceOf(AgentStatus, event)) {
|
|
739
462
|
this._processAgentStatus(event);
|
|
740
463
|
}
|
|
741
464
|
}
|
|
@@ -1071,128 +794,12 @@ var stringifyRef = (ref) => {
|
|
|
1071
794
|
return ref.dxn.asEchoDXN()?.echoId ?? ref.dxn.asQueueDXN()?.objectId ?? "";
|
|
1072
795
|
};
|
|
1073
796
|
var stringifyObject = (obj) => {
|
|
1074
|
-
return
|
|
1075
|
-
};
|
|
1076
|
-
|
|
1077
|
-
// src/functions/services.ts
|
|
1078
|
-
import * as Tool from "@effect/ai/Tool";
|
|
1079
|
-
import * as Context3 from "effect/Context";
|
|
1080
|
-
import * as Effect7 from "effect/Effect";
|
|
1081
|
-
import * as Layer2 from "effect/Layer";
|
|
1082
|
-
import * as Record from "effect/Record";
|
|
1083
|
-
import * as Schema3 from "effect/Schema";
|
|
1084
|
-
import { AiToolNotFoundError, ToolExecutionService as ToolExecutionService2, ToolResolverService as ToolResolverService2 } from "@dxos/ai";
|
|
1085
|
-
import { todo as todo2 } from "@dxos/debug";
|
|
1086
|
-
import { Query as Query2 } from "@dxos/echo";
|
|
1087
|
-
import { DatabaseService as DatabaseService3 } from "@dxos/echo-db";
|
|
1088
|
-
import { Function as Function4, FunctionDefinition, FunctionInvocationService } from "@dxos/functions";
|
|
1089
|
-
import { invariant as invariant2 } from "@dxos/invariant";
|
|
1090
|
-
var __dxlog_file6 = "/__w/dxos/dxos/packages/core/assistant/src/functions/services.ts";
|
|
1091
|
-
var makeToolResolverFromFunctions = (functions, toolkit) => {
|
|
1092
|
-
return Layer2.effect(ToolResolverService2, Effect7.gen(function* () {
|
|
1093
|
-
const dbService = yield* DatabaseService3;
|
|
1094
|
-
return {
|
|
1095
|
-
resolve: (id) => Effect7.gen(function* () {
|
|
1096
|
-
const tool = toolkit.tools[id];
|
|
1097
|
-
if (tool) {
|
|
1098
|
-
return tool;
|
|
1099
|
-
}
|
|
1100
|
-
const { objects: [dbFunction] } = yield* DatabaseService3.runQuery(Query2.type(Function4.Function, {
|
|
1101
|
-
key: id
|
|
1102
|
-
}));
|
|
1103
|
-
const functionDef = dbFunction ? FunctionDefinition.deserialize(dbFunction) : functions.find((fn2) => fn2.key === id);
|
|
1104
|
-
if (!functionDef) {
|
|
1105
|
-
return yield* Effect7.fail(new AiToolNotFoundError(id));
|
|
1106
|
-
}
|
|
1107
|
-
return projectFunctionToTool(functionDef);
|
|
1108
|
-
}).pipe(Effect7.provideService(DatabaseService3, dbService))
|
|
1109
|
-
};
|
|
1110
|
-
}));
|
|
1111
|
-
};
|
|
1112
|
-
var makeToolExecutionServiceFromFunctions = (toolkit, handlersLayer) => {
|
|
1113
|
-
return Layer2.effect(ToolExecutionService2, Effect7.gen(function* () {
|
|
1114
|
-
const toolkitHandler = yield* toolkit.pipe(Effect7.provide(handlersLayer));
|
|
1115
|
-
invariant2(isHandlerLike(toolkitHandler), void 0, {
|
|
1116
|
-
F: __dxlog_file6,
|
|
1117
|
-
L: 74,
|
|
1118
|
-
S: this,
|
|
1119
|
-
A: [
|
|
1120
|
-
"isHandlerLike(toolkitHandler)",
|
|
1121
|
-
""
|
|
1122
|
-
]
|
|
1123
|
-
});
|
|
1124
|
-
const functionInvocationService = yield* FunctionInvocationService;
|
|
1125
|
-
return {
|
|
1126
|
-
handlersFor: (toolkit2) => {
|
|
1127
|
-
const makeHandler = (tool) => {
|
|
1128
|
-
return Effect7.fn(`toolFunctionHandler ${tool.name}`)(function* (input) {
|
|
1129
|
-
if (toolkitHandler.tools[tool.name]) {
|
|
1130
|
-
if (Tool.isProviderDefined(tool)) {
|
|
1131
|
-
throw new Error("Attempted to call a provider-defined tool");
|
|
1132
|
-
}
|
|
1133
|
-
const { result } = yield* toolkitHandler.handle(tool.name, input);
|
|
1134
|
-
return result;
|
|
1135
|
-
}
|
|
1136
|
-
const { definition: functionDef } = Context3.get(FunctionToolAnnotation)(tool.annotations);
|
|
1137
|
-
return yield* functionInvocationService.invokeFunction(functionDef, input).pipe(Effect7.catchAllDefect((defect) => Effect7.fail(defect)));
|
|
1138
|
-
});
|
|
1139
|
-
};
|
|
1140
|
-
return toolkit2.of(Record.map(toolkit2.tools, (tool, _name) => Tool.isUserDefined(tool) ? makeHandler(tool) : null));
|
|
1141
|
-
}
|
|
1142
|
-
};
|
|
1143
|
-
}));
|
|
1144
|
-
};
|
|
1145
|
-
var FunctionToolAnnotation = class extends Context3.Tag("@dxos/assistant/FunctionToolAnnotation")() {
|
|
1146
|
-
};
|
|
1147
|
-
var toolCache = /* @__PURE__ */ new WeakMap();
|
|
1148
|
-
var projectFunctionToTool = (fn2) => {
|
|
1149
|
-
if (toolCache.has(fn2)) {
|
|
1150
|
-
return toolCache.get(fn2);
|
|
1151
|
-
}
|
|
1152
|
-
const tool = Tool.make(makeToolName(fn2.name), {
|
|
1153
|
-
description: fn2.description,
|
|
1154
|
-
parameters: createStructFieldsFromSchema(fn2.inputSchema),
|
|
1155
|
-
// TODO(dmaretskyi): Include output schema.
|
|
1156
|
-
failure: Schema3.Any
|
|
1157
|
-
}).annotate(FunctionToolAnnotation, {
|
|
1158
|
-
definition: fn2
|
|
1159
|
-
});
|
|
1160
|
-
toolCache.set(fn2, tool);
|
|
1161
|
-
return tool;
|
|
1162
|
-
};
|
|
1163
|
-
var makeToolName = (name) => {
|
|
1164
|
-
const toolName = name.toLowerCase().replace(/[^a-zA-Z0-9]/g, "-");
|
|
1165
|
-
invariant2(toolName.match(/^[a-z_][a-z0-9-_]*$/), void 0, {
|
|
1166
|
-
F: __dxlog_file6,
|
|
1167
|
-
L: 144,
|
|
1168
|
-
S: void 0,
|
|
1169
|
-
A: [
|
|
1170
|
-
"toolName.match(/^[a-z_][a-z0-9-_]*$/)",
|
|
1171
|
-
""
|
|
1172
|
-
]
|
|
1173
|
-
});
|
|
1174
|
-
return toolName;
|
|
1175
|
-
};
|
|
1176
|
-
var createStructFieldsFromSchema = (schema) => {
|
|
1177
|
-
switch (schema.ast._tag) {
|
|
1178
|
-
case "TypeLiteral":
|
|
1179
|
-
return Object.fromEntries(schema.ast.propertySignatures.map((prop) => [
|
|
1180
|
-
prop.name,
|
|
1181
|
-
Schema3.make(prop.type)
|
|
1182
|
-
]));
|
|
1183
|
-
case "VoidKeyword":
|
|
1184
|
-
return {};
|
|
1185
|
-
default:
|
|
1186
|
-
return todo2(`Unsupported schema AST: ${schema.ast._tag}`);
|
|
1187
|
-
}
|
|
1188
|
-
};
|
|
1189
|
-
var isHandlerLike = (value) => {
|
|
1190
|
-
return typeof value.tools === "object" && typeof value.handle === "function";
|
|
797
|
+
return Obj3.getLabel(obj) ?? Obj3.getTypename(obj) ?? obj.id;
|
|
1191
798
|
};
|
|
1192
799
|
|
|
1193
800
|
// src/templates/system.ts
|
|
1194
801
|
import defaultsDeep from "lodash.defaultsdeep";
|
|
1195
|
-
import { Template
|
|
802
|
+
import { Template } from "@dxos/blueprints";
|
|
1196
803
|
|
|
1197
804
|
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/instructions/format.tpl?raw
|
|
1198
805
|
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";
|
|
@@ -1201,12 +808,12 @@ var format_default = "{{! System Core }}\n\n## Input Formats\n\n### Blueprints\n
|
|
|
1201
808
|
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";
|
|
1202
809
|
|
|
1203
810
|
// src/templates/system.ts
|
|
1204
|
-
var createSystemPrompt = (options = {}) =>
|
|
811
|
+
var createSystemPrompt = (options = {}) => Template.process(format_default, defaultsDeep(options, {
|
|
1205
812
|
DATETIME: (/* @__PURE__ */ new Date()).toISOString()
|
|
1206
813
|
}));
|
|
1207
814
|
var templates = {
|
|
1208
815
|
/** Editable system prompt.*/
|
|
1209
|
-
system:
|
|
816
|
+
system: Template.make({
|
|
1210
817
|
/**
|
|
1211
818
|
* NOTE: We are forbidden from accessing randomness at model level in cloudflare, so we have to have pre-generated ids.
|
|
1212
819
|
*/
|
|
@@ -1214,53 +821,6 @@ var templates = {
|
|
|
1214
821
|
source: system_default
|
|
1215
822
|
})
|
|
1216
823
|
};
|
|
1217
|
-
|
|
1218
|
-
// src/types/artifact.ts
|
|
1219
|
-
import * as Schema4 from "effect/Schema";
|
|
1220
|
-
import { DatabaseService as DatabaseService4 } from "@dxos/echo-db";
|
|
1221
|
-
import { DXN as DXN2, LOCAL_SPACE_TAG } from "@dxos/keys";
|
|
1222
|
-
import { trim as trim2 } from "@dxos/util";
|
|
1223
|
-
var createArtifactElement = (id) => `<artifact id=${id} />`;
|
|
1224
|
-
var ArtifactId = class extends Schema4.String.annotations({
|
|
1225
|
-
// TODO(dmaretskyi): This section gets overriden.
|
|
1226
|
-
description: trim2`
|
|
1227
|
-
The ID of the referenced object. Formats accepted:
|
|
1228
|
-
- DXN (dxn:echo:@:XXXXX). DXNs can be prepended with an @ symbol for compatibility with in-text references.
|
|
1229
|
-
- space ID, object ID tuple (spaceID:objectID)
|
|
1230
|
-
- Only object ID that is assumed to be in the current space (XXXXX)
|
|
1231
|
-
`,
|
|
1232
|
-
examples: [
|
|
1233
|
-
"dxn:echo:@:XXXXX",
|
|
1234
|
-
"@dxn:echo:@:XXXXX",
|
|
1235
|
-
"spaceID:objectID",
|
|
1236
|
-
"XXXXX"
|
|
1237
|
-
]
|
|
1238
|
-
}) {
|
|
1239
|
-
static toDXN(reference, owningSpaceId) {
|
|
1240
|
-
if (reference.startsWith("@dxn:")) {
|
|
1241
|
-
return DXN2.parse(reference.slice(1));
|
|
1242
|
-
} else if (reference.startsWith("dxn:")) {
|
|
1243
|
-
return DXN2.parse(reference);
|
|
1244
|
-
} else if (/^[A-Z0-9]+:[A-Z0-9]+$/.test(reference)) {
|
|
1245
|
-
const [spaceId, objectId] = reference.split(":");
|
|
1246
|
-
return new DXN2(DXN2.kind.ECHO, [
|
|
1247
|
-
spaceId === owningSpaceId ? LOCAL_SPACE_TAG : spaceId,
|
|
1248
|
-
objectId
|
|
1249
|
-
]);
|
|
1250
|
-
} else if (/^[A-Z0-9]+$/.test(reference)) {
|
|
1251
|
-
return DXN2.fromLocalObjectId(reference);
|
|
1252
|
-
} else {
|
|
1253
|
-
throw new Error(`Unable to parse object reference: ${reference}`);
|
|
1254
|
-
}
|
|
1255
|
-
}
|
|
1256
|
-
/**
|
|
1257
|
-
* Resolves an artifact ID to an object.
|
|
1258
|
-
*/
|
|
1259
|
-
static resolve(schema, ref) {
|
|
1260
|
-
const dxn = ArtifactId.toDXN(ref);
|
|
1261
|
-
return DatabaseService4.resolve(dxn, schema);
|
|
1262
|
-
}
|
|
1263
|
-
};
|
|
1264
824
|
export {
|
|
1265
825
|
AiAssistantError,
|
|
1266
826
|
AiContextBinder,
|
|
@@ -1275,6 +835,7 @@ export {
|
|
|
1275
835
|
ExecutionGraph,
|
|
1276
836
|
GenerationObserver,
|
|
1277
837
|
GenericToolkit_exports as GenericToolkit,
|
|
838
|
+
RefFromLLM,
|
|
1278
839
|
VersionPin,
|
|
1279
840
|
applyDiffs,
|
|
1280
841
|
computeDiffsWithCursors,
|