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