@dxos/assistant 0.8.4-main.ead640a → 0.8.4-main.ef1bc66f44
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/lib/neutral/chunk-J5LGTIGS.mjs +10 -0
- package/dist/lib/neutral/chunk-ZPC54VRM.mjs +596 -0
- package/dist/lib/neutral/chunk-ZPC54VRM.mjs.map +7 -0
- package/dist/lib/neutral/extraction/index.mjs +225 -0
- package/dist/lib/neutral/extraction/index.mjs.map +7 -0
- package/dist/lib/{browser → neutral}/index.mjs +346 -350
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/lib/neutral/testing/index.mjs +38 -0
- package/dist/lib/neutral/testing/index.mjs.map +7 -0
- package/dist/types/src/conversation/context.d.ts +97 -62
- 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 +41 -16
- 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/diff.d.ts +3 -1
- package/dist/types/src/diff.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +22 -30
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/execution-graph.d.ts +5 -2
- package/dist/types/src/execution-graph.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +254 -248
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction.d.ts +344 -303
- 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 +6 -6
- 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 +107 -0
- package/dist/types/src/session/GenericToolkit.d.ts.map +1 -0
- package/dist/types/src/session/artifact-diff.d.ts +1 -1
- package/dist/types/src/session/artifact-diff.d.ts.map +1 -1
- package/dist/types/src/session/format.d.ts +4 -176
- package/dist/types/src/session/format.d.ts.map +1 -1
- package/dist/types/src/session/index.d.ts +2 -0
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/observer.d.ts +6 -5
- package/dist/types/src/session/observer.d.ts.map +1 -1
- package/dist/types/src/session/session.d.ts +16 -11
- 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 +3 -3
- 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 +22 -0
- package/dist/types/src/testing/layer.d.ts.map +1 -0
- package/dist/types/src/types/artifact.d.ts +14 -2
- package/dist/types/src/types/artifact.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +40 -31
- package/src/conversation/context.test.ts +46 -0
- package/src/conversation/context.ts +211 -67
- package/src/conversation/conversation.test.ts +65 -0
- package/src/conversation/conversation.ts +128 -53
- package/src/diff.test.ts +37 -6
- package/src/diff.ts +18 -4
- package/src/errors.ts +2 -2
- package/src/execution-graph.test.ts +45 -43
- package/src/execution-graph.ts +23 -14
- package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +2 -2
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +44 -54
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +15 -14
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +36 -44
- package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +10 -5
- package/src/extraction/extraction.ts +9 -8
- package/src/extraction/quotes.ts +4 -5
- package/src/functions/services.ts +66 -37
- package/src/functions.conversations.json +1 -0
- package/src/functions.test.ts +74 -0
- package/src/session/GenericToolkit.ts +160 -0
- package/src/session/artifact-diff.ts +1 -1
- package/src/session/format.test.ts +103 -39
- package/src/session/format.ts +17 -16
- package/src/session/index.ts +3 -0
- package/src/session/observer.ts +5 -3
- package/src/session/session.conversations.json +1 -1
- package/src/session/session.test.ts +28 -15
- package/src/session/session.ts +44 -21
- package/src/session/toolkit.ts +13 -22
- package/src/session/version-pin.ts +6 -6
- package/src/templates/{format.tpl → instructions/format.tpl} +1 -4
- package/src/templates/system.ts +2 -2
- package/src/testing/index.ts +5 -0
- package/src/testing/layer.ts +84 -0
- package/src/types/artifact.ts +43 -6
- package/dist/lib/browser/chunk-CWGVG235.mjs +0 -380
- package/dist/lib/browser/chunk-CWGVG235.mjs.map +0 -7
- package/dist/lib/browser/chunk-UB26N5UF.mjs +0 -89
- package/dist/lib/browser/chunk-UB26N5UF.mjs.map +0 -7
- package/dist/lib/browser/extraction/index.mjs +0 -272
- package/dist/lib/browser/extraction/index.mjs.map +0 -7
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs +0 -13
- package/dist/lib/node-esm/chunk-BCFXTNSK.mjs +0 -91
- package/dist/lib/node-esm/chunk-BCFXTNSK.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-OV5PMQPB.mjs +0 -382
- package/dist/lib/node-esm/chunk-OV5PMQPB.mjs.map +0 -7
- package/dist/lib/node-esm/extraction/index.mjs +0 -273
- package/dist/lib/node-esm/extraction/index.mjs.map +0 -7
- package/dist/lib/node-esm/index.mjs +0 -934
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs +0 -14
- package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs.map +0 -7
- package/dist/types/src/summarization/summarization.test.d.ts +0 -2
- package/dist/types/src/summarization/summarization.test.d.ts.map +0 -1
- package/src/summarization/summarization.test.ts +0 -202
- /package/dist/lib/{browser/named-entity-recognition-QPYHML66.mjs.map → neutral/chunk-J5LGTIGS.mjs.map} +0 -0
- /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
- /package/src/templates/{system.tpl → instructions/system.tpl} +0 -0
|
@@ -3,295 +3,434 @@ import {
|
|
|
3
3
|
AiModelError,
|
|
4
4
|
AiSession,
|
|
5
5
|
ArtifactDiffResolver,
|
|
6
|
+
ArtifactId,
|
|
6
7
|
GenerationObserver,
|
|
8
|
+
GenericToolkit_exports,
|
|
9
|
+
RefFromLLM,
|
|
10
|
+
ToolExecutionServices,
|
|
7
11
|
VersionPin,
|
|
8
|
-
|
|
9
|
-
|
|
12
|
+
createArtifactElement,
|
|
13
|
+
createToolkit,
|
|
14
|
+
formatSystemPrompt,
|
|
15
|
+
formatUserPrompt,
|
|
16
|
+
makeToolExecutionServiceFromFunctions,
|
|
17
|
+
makeToolResolverFromFunctions
|
|
18
|
+
} from "./chunk-ZPC54VRM.mjs";
|
|
19
|
+
import "./chunk-J5LGTIGS.mjs";
|
|
10
20
|
|
|
11
21
|
// src/conversation/context.ts
|
|
12
|
-
import {
|
|
13
|
-
import * as
|
|
22
|
+
import { Atom, Registry } from "@effect-atom/atom-react";
|
|
23
|
+
import * as EArray from "effect/Array";
|
|
14
24
|
import * as Context from "effect/Context";
|
|
25
|
+
import * as Effect from "effect/Effect";
|
|
15
26
|
import * as Function from "effect/Function";
|
|
16
27
|
import * as Schema from "effect/Schema";
|
|
17
28
|
import { Blueprint } from "@dxos/blueprints";
|
|
18
29
|
import { Resource } from "@dxos/context";
|
|
19
|
-
import { DXN,
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
22
|
-
|
|
23
|
-
if (key in obj) {
|
|
24
|
-
Object.defineProperty(obj, key, {
|
|
25
|
-
value,
|
|
26
|
-
enumerable: true,
|
|
27
|
-
configurable: true,
|
|
28
|
-
writable: true
|
|
29
|
-
});
|
|
30
|
-
} else {
|
|
31
|
-
obj[key] = value;
|
|
32
|
-
}
|
|
33
|
-
return obj;
|
|
34
|
-
}
|
|
30
|
+
import { DXN, Obj, Query, Type } from "@dxos/echo";
|
|
31
|
+
import { assertArgument } from "@dxos/invariant";
|
|
32
|
+
import { log } from "@dxos/log";
|
|
33
|
+
import { ComplexSet, isTruthy } from "@dxos/util";
|
|
35
34
|
var __dxlog_file = "/__w/dxos/dxos/packages/core/assistant/src/conversation/context.ts";
|
|
36
35
|
var ContextBinding = Schema.Struct({
|
|
37
36
|
blueprints: Schema.Struct({
|
|
38
37
|
added: Schema.Array(Type.Ref(Blueprint.Blueprint)),
|
|
39
38
|
removed: Schema.Array(Type.Ref(Blueprint.Blueprint))
|
|
40
39
|
}),
|
|
41
|
-
// TODO(burdon): Type.Expando => Type.Obj (or Obj.Any?)
|
|
42
40
|
objects: Schema.Struct({
|
|
43
|
-
added: Schema.Array(Type.Ref(Type.
|
|
44
|
-
removed: Schema.Array(Type.Ref(Type.
|
|
41
|
+
added: Schema.Array(Type.Ref(Type.Obj)),
|
|
42
|
+
removed: Schema.Array(Type.Ref(Type.Obj))
|
|
45
43
|
})
|
|
46
|
-
}).pipe(Type.
|
|
44
|
+
}).pipe(Type.object({
|
|
47
45
|
typename: "dxos.org/type/ContextBinding",
|
|
48
46
|
version: "0.1.0"
|
|
49
47
|
}));
|
|
50
48
|
var Bindings = class {
|
|
49
|
+
blueprints = new ComplexSet((ref) => ref.dxn.toString());
|
|
50
|
+
// TODO(burdon): Some DXNs have the Space prefix so only compare the object ID.
|
|
51
|
+
objects = new ComplexSet((ref) => ref.dxn.asEchoDXN()?.echoId);
|
|
51
52
|
toJSON() {
|
|
52
53
|
return {
|
|
53
|
-
blueprints:
|
|
54
|
-
objects:
|
|
54
|
+
blueprints: EArray.fromIterable(this.blueprints).map((ref) => ref.dxn.toString()),
|
|
55
|
+
objects: EArray.fromIterable(this.objects).map((ref) => ref.dxn.toString())
|
|
55
56
|
};
|
|
56
57
|
}
|
|
57
|
-
constructor() {
|
|
58
|
-
_define_property(this, "blueprints", new ComplexSet((ref) => ref.dxn.toString()));
|
|
59
|
-
_define_property(this, "objects", new ComplexSet((ref) => ref.dxn.asEchoDXN()?.echoId));
|
|
60
|
-
}
|
|
61
58
|
};
|
|
62
59
|
var AiContextBinder = class extends Resource {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
});
|
|
73
|
-
return this._bindings;
|
|
60
|
+
_blueprints = Atom.make([]).pipe(Atom.keepAlive);
|
|
61
|
+
_objects = Atom.make([]).pipe(Atom.keepAlive);
|
|
62
|
+
_registry;
|
|
63
|
+
_queue;
|
|
64
|
+
constructor(options) {
|
|
65
|
+
super();
|
|
66
|
+
assertArgument(options.queue, "options.queue", "Queue is required");
|
|
67
|
+
this._queue = options.queue;
|
|
68
|
+
this._registry = options.registry ?? Registry.make();
|
|
74
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Returns the blueprints atom for subscription.
|
|
72
|
+
*/
|
|
75
73
|
get blueprints() {
|
|
76
|
-
invariant(this._blueprints, "AiContextBinder not open", {
|
|
77
|
-
F: __dxlog_file,
|
|
78
|
-
L: 83,
|
|
79
|
-
S: this,
|
|
80
|
-
A: [
|
|
81
|
-
"this._blueprints",
|
|
82
|
-
"'AiContextBinder not open'"
|
|
83
|
-
]
|
|
84
|
-
});
|
|
85
74
|
return this._blueprints;
|
|
86
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* Returns the objects atom for subscription.
|
|
78
|
+
*/
|
|
87
79
|
get objects() {
|
|
88
|
-
invariant(this._objects, "AiContextBinder not open", {
|
|
89
|
-
F: __dxlog_file,
|
|
90
|
-
L: 88,
|
|
91
|
-
S: this,
|
|
92
|
-
A: [
|
|
93
|
-
"this._objects",
|
|
94
|
-
"'AiContextBinder not open'"
|
|
95
|
-
]
|
|
96
|
-
});
|
|
97
80
|
return this._objects;
|
|
98
81
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
this._bindings = computed(() => this._reduce(query.objects));
|
|
105
|
-
this._blueprints = computed(() => [
|
|
106
|
-
...this.bindings.value.blueprints
|
|
107
|
-
]);
|
|
108
|
-
this._objects = computed(() => [
|
|
109
|
-
...this.bindings.value.objects
|
|
110
|
-
]);
|
|
82
|
+
/**
|
|
83
|
+
* Gets the current blueprints value.
|
|
84
|
+
*/
|
|
85
|
+
getBlueprints() {
|
|
86
|
+
return this._registry.get(this._blueprints);
|
|
111
87
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
88
|
+
/**
|
|
89
|
+
* Gets the current objects value.
|
|
90
|
+
*/
|
|
91
|
+
getObjects() {
|
|
92
|
+
return this._registry.get(this._objects);
|
|
116
93
|
}
|
|
117
94
|
/**
|
|
118
|
-
*
|
|
95
|
+
* Subscribe to changes in blueprints.
|
|
119
96
|
*/
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
97
|
+
subscribeBlueprints(cb) {
|
|
98
|
+
return this._registry.subscribe(this._blueprints, () => cb(this._registry.get(this._blueprints)));
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Subscribe to changes in objects.
|
|
102
|
+
*/
|
|
103
|
+
subscribeObjects(cb) {
|
|
104
|
+
return this._registry.subscribe(this._objects, () => cb(this._registry.get(this._objects)));
|
|
105
|
+
}
|
|
106
|
+
async _open() {
|
|
107
|
+
const query = this._queue.query(Query.type(ContextBinding));
|
|
108
|
+
const initialResults = await query.run();
|
|
109
|
+
await this._updateBindings(initialResults);
|
|
110
|
+
this._ctx.onDispose(query.subscribe(async () => {
|
|
111
|
+
await this._updateBindings(query.results);
|
|
112
|
+
}));
|
|
113
|
+
}
|
|
114
|
+
async _updateBindings(items) {
|
|
115
|
+
if (items.length === 0) {
|
|
129
116
|
return;
|
|
130
117
|
}
|
|
118
|
+
const bindings = this._reduce(items);
|
|
119
|
+
const currentBlueprints = this._registry.get(this._blueprints);
|
|
120
|
+
const currentObjects = this._registry.get(this._objects);
|
|
121
|
+
const newBlueprints = await this._resolve(bindings.blueprints, currentBlueprints);
|
|
122
|
+
const newObjects = await this._resolve(bindings.objects, currentObjects);
|
|
123
|
+
this._registry.set(this._blueprints, newBlueprints);
|
|
124
|
+
this._registry.set(this._objects, newObjects);
|
|
125
|
+
log("updated bindings", {
|
|
126
|
+
blueprints: newBlueprints.length,
|
|
127
|
+
objects: newObjects.length
|
|
128
|
+
}, {
|
|
129
|
+
F: __dxlog_file,
|
|
130
|
+
L: 159,
|
|
131
|
+
S: this,
|
|
132
|
+
C: (f, a) => f(...a)
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
async _close() {
|
|
136
|
+
this._registry.set(this._blueprints, []);
|
|
137
|
+
this._registry.set(this._objects, []);
|
|
138
|
+
}
|
|
139
|
+
async bind({ blueprints, objects }) {
|
|
140
|
+
const currentBlueprints = this._registry.get(this._blueprints);
|
|
141
|
+
const currentObjects = this._registry.get(this._objects);
|
|
142
|
+
const { added: addedBlueprints, next: nextBlueprints } = this._processBindings(blueprints, currentBlueprints);
|
|
143
|
+
const { added: addedObjects, next: nextObjects } = this._processBindings(objects, currentObjects);
|
|
144
|
+
if (!addedBlueprints.length && !addedObjects.length) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
this._registry.set(this._blueprints, nextBlueprints);
|
|
148
|
+
this._registry.set(this._objects, nextObjects);
|
|
149
|
+
log("bind", {
|
|
150
|
+
blueprints: addedBlueprints.length,
|
|
151
|
+
objects: addedObjects.length
|
|
152
|
+
}, {
|
|
153
|
+
F: __dxlog_file,
|
|
154
|
+
L: 185,
|
|
155
|
+
S: this,
|
|
156
|
+
C: (f, a) => f(...a)
|
|
157
|
+
});
|
|
131
158
|
await this._queue.append([
|
|
132
159
|
Obj.make(ContextBinding, {
|
|
133
160
|
blueprints: {
|
|
134
|
-
added:
|
|
161
|
+
added: addedBlueprints,
|
|
135
162
|
removed: []
|
|
136
163
|
},
|
|
137
164
|
objects: {
|
|
138
|
-
added:
|
|
165
|
+
added: addedObjects,
|
|
139
166
|
removed: []
|
|
140
167
|
}
|
|
141
168
|
})
|
|
142
169
|
]);
|
|
143
170
|
}
|
|
144
|
-
async unbind(
|
|
145
|
-
if (!
|
|
171
|
+
async unbind({ blueprints, objects }) {
|
|
172
|
+
if (!blueprints?.length && !objects?.length) {
|
|
146
173
|
return;
|
|
147
174
|
}
|
|
175
|
+
log("unbind", {
|
|
176
|
+
blueprints: blueprints?.length,
|
|
177
|
+
objects: objects?.length
|
|
178
|
+
}, {
|
|
179
|
+
F: __dxlog_file,
|
|
180
|
+
L: 205,
|
|
181
|
+
S: this,
|
|
182
|
+
C: (f, a) => f(...a)
|
|
183
|
+
});
|
|
148
184
|
await this._queue.append([
|
|
149
185
|
Obj.make(ContextBinding, {
|
|
150
186
|
blueprints: {
|
|
151
187
|
added: [],
|
|
152
|
-
removed:
|
|
188
|
+
removed: blueprints ?? []
|
|
153
189
|
},
|
|
154
190
|
objects: {
|
|
155
191
|
added: [],
|
|
156
|
-
removed:
|
|
192
|
+
removed: objects ?? []
|
|
157
193
|
}
|
|
158
194
|
})
|
|
159
195
|
]);
|
|
160
196
|
}
|
|
197
|
+
/**
|
|
198
|
+
* Process bindings to filter duplicates and determine next state.
|
|
199
|
+
*/
|
|
200
|
+
_processBindings(refs, current) {
|
|
201
|
+
const next = [
|
|
202
|
+
...current
|
|
203
|
+
];
|
|
204
|
+
const added = [];
|
|
205
|
+
if (!refs?.length) {
|
|
206
|
+
return {
|
|
207
|
+
added,
|
|
208
|
+
next
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
const seen = new Set(current.map((obj) => Obj.getDXN(obj).toString()));
|
|
212
|
+
for (const ref of refs) {
|
|
213
|
+
const dxn = ref.dxn.toString();
|
|
214
|
+
if (!seen.has(dxn)) {
|
|
215
|
+
seen.add(dxn);
|
|
216
|
+
added.push(ref);
|
|
217
|
+
if (ref.isAvailable) {
|
|
218
|
+
const target = ref.target;
|
|
219
|
+
if (target) {
|
|
220
|
+
next.push(target);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
return {
|
|
226
|
+
added,
|
|
227
|
+
next
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Reduce results into sets of blueprints and objects.
|
|
232
|
+
*/
|
|
161
233
|
_reduce(items) {
|
|
162
|
-
return Function.pipe(items,
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
234
|
+
return Function.pipe(items, EArray.reduce(new Bindings(), (context, { blueprints, objects }) => {
|
|
235
|
+
blueprints.added.forEach((ref) => context.blueprints.add(ref));
|
|
236
|
+
blueprints.removed.forEach((ref) => context.blueprints.delete(ref));
|
|
237
|
+
objects.added.forEach((ref) => context.objects.add(ref));
|
|
238
|
+
objects.removed.forEach((ref) => {
|
|
166
239
|
for (const obj of context.objects) {
|
|
167
240
|
if (DXN.equalsEchoId(obj.dxn, ref.dxn)) {
|
|
168
241
|
context.objects.delete(obj);
|
|
169
242
|
}
|
|
170
243
|
}
|
|
171
244
|
});
|
|
172
|
-
item.objects.added.forEach((ref) => context.objects.add(ref));
|
|
173
245
|
return context;
|
|
174
246
|
}));
|
|
175
247
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
248
|
+
/**
|
|
249
|
+
* Resolve references to objects, loading them first if needed and falling back to existing objects.
|
|
250
|
+
*/
|
|
251
|
+
async _resolve(refs, current) {
|
|
252
|
+
const refArray = [
|
|
253
|
+
...refs
|
|
254
|
+
];
|
|
255
|
+
await Promise.all(refArray.map((ref) => ref.load()));
|
|
256
|
+
return refArray.map((ref) => {
|
|
257
|
+
let target;
|
|
258
|
+
if (ref.isAvailable) {
|
|
259
|
+
target = ref.target;
|
|
260
|
+
}
|
|
261
|
+
return target ?? current.find((obj) => Obj.getDXN(obj).toString() === ref.dxn.toString());
|
|
262
|
+
}).filter(isTruthy);
|
|
182
263
|
}
|
|
183
264
|
};
|
|
184
|
-
var AiContextService = class extends Context.Tag("@dxos/assistant/AiContextService")() {
|
|
265
|
+
var AiContextService = class _AiContextService extends Context.Tag("@dxos/assistant/AiContextService")() {
|
|
266
|
+
static bindContext = ({ blueprints, objects }) => Effect.gen(function* () {
|
|
267
|
+
const { binder } = yield* _AiContextService;
|
|
268
|
+
yield* Effect.promise(() => binder.bind({
|
|
269
|
+
blueprints,
|
|
270
|
+
objects
|
|
271
|
+
}));
|
|
272
|
+
});
|
|
273
|
+
static findObjects = (type) => {
|
|
274
|
+
return Effect.gen(function* () {
|
|
275
|
+
const { binder } = yield* _AiContextService;
|
|
276
|
+
return binder.getObjects().filter(Obj.instanceOf(type));
|
|
277
|
+
});
|
|
278
|
+
};
|
|
279
|
+
/**
|
|
280
|
+
* Glorified type cast until we figure out function service typing.
|
|
281
|
+
* For context, the AiContextService is provided to functions called from tool calls, but it's not implemented in types yet.
|
|
282
|
+
* @deprecated
|
|
283
|
+
*/
|
|
284
|
+
static fixFunctionHandlerType = (eff) => eff;
|
|
185
285
|
};
|
|
186
286
|
|
|
187
287
|
// src/conversation/conversation.ts
|
|
188
|
-
import * as
|
|
189
|
-
import * as
|
|
190
|
-
import * as
|
|
288
|
+
import * as Context2 from "effect/Context";
|
|
289
|
+
import * as Effect2 from "effect/Effect";
|
|
290
|
+
import * as Layer from "effect/Layer";
|
|
191
291
|
import { Resource as Resource2 } from "@dxos/context";
|
|
192
292
|
import { Obj as Obj2 } from "@dxos/echo";
|
|
193
|
-
import {
|
|
194
|
-
import {
|
|
195
|
-
import {
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
Object.defineProperty(obj, key, {
|
|
199
|
-
value,
|
|
200
|
-
enumerable: true,
|
|
201
|
-
configurable: true,
|
|
202
|
-
writable: true
|
|
203
|
-
});
|
|
204
|
-
} else {
|
|
205
|
-
obj[key] = value;
|
|
206
|
-
}
|
|
207
|
-
return obj;
|
|
208
|
-
}
|
|
293
|
+
import { acquireReleaseResource } from "@dxos/effect";
|
|
294
|
+
import { QueueService } from "@dxos/functions";
|
|
295
|
+
import { invariant } from "@dxos/invariant";
|
|
296
|
+
import { log as log2 } from "@dxos/log";
|
|
297
|
+
import { Message } from "@dxos/types";
|
|
209
298
|
var __dxlog_file2 = "/__w/dxos/dxos/packages/core/assistant/src/conversation/conversation.ts";
|
|
210
299
|
var AiConversation = class extends Resource2 {
|
|
300
|
+
/**
|
|
301
|
+
* Blueprints and objects bound to the conversation.
|
|
302
|
+
*/
|
|
303
|
+
_binder;
|
|
304
|
+
_queue;
|
|
305
|
+
_toolkit;
|
|
306
|
+
constructor(options) {
|
|
307
|
+
super();
|
|
308
|
+
this._queue = options.queue;
|
|
309
|
+
this._toolkit = options.toolkit;
|
|
310
|
+
invariant(this._queue, void 0, {
|
|
311
|
+
F: __dxlog_file2,
|
|
312
|
+
L: 60,
|
|
313
|
+
S: this,
|
|
314
|
+
A: [
|
|
315
|
+
"this._queue",
|
|
316
|
+
""
|
|
317
|
+
]
|
|
318
|
+
});
|
|
319
|
+
this._binder = new AiContextBinder({
|
|
320
|
+
queue: this._queue,
|
|
321
|
+
registry: options.registry
|
|
322
|
+
});
|
|
323
|
+
}
|
|
211
324
|
async _open() {
|
|
212
|
-
await this.
|
|
325
|
+
await this._binder.open(this._ctx);
|
|
213
326
|
}
|
|
214
|
-
|
|
215
|
-
|
|
327
|
+
get queue() {
|
|
328
|
+
return this._queue;
|
|
216
329
|
}
|
|
217
330
|
get context() {
|
|
218
|
-
return this.
|
|
331
|
+
return this._binder;
|
|
219
332
|
}
|
|
220
333
|
get toolkit() {
|
|
221
334
|
return this._toolkit;
|
|
222
335
|
}
|
|
223
336
|
async getHistory() {
|
|
224
337
|
const queueItems = await this._queue.queryObjects();
|
|
225
|
-
return queueItems.filter(Obj2.instanceOf(
|
|
338
|
+
return queueItems.filter(Obj2.instanceOf(Message.Message));
|
|
339
|
+
}
|
|
340
|
+
getTools() {
|
|
341
|
+
return Effect2.gen(this, function* () {
|
|
342
|
+
const blueprints = this.context.getBlueprints();
|
|
343
|
+
const tookit = yield* createToolkit({
|
|
344
|
+
toolkit: this._toolkit,
|
|
345
|
+
blueprints
|
|
346
|
+
});
|
|
347
|
+
return tookit.tools;
|
|
348
|
+
}).pipe(Effect2.orDie);
|
|
226
349
|
}
|
|
227
350
|
/**
|
|
228
351
|
* Creates a new cancelable request effect.
|
|
229
352
|
*/
|
|
230
353
|
createRequest(params) {
|
|
231
|
-
return
|
|
232
|
-
const
|
|
233
|
-
const
|
|
234
|
-
const context = yield* Effect.promise(() => this.context.query());
|
|
235
|
-
const blueprints = yield* Effect.forEach(context.blueprints.values(), DatabaseService.loadOption).pipe(Effect.map(Array4.filter(Option.isSome)), Effect.map(Array4.map((option) => option.value)));
|
|
236
|
-
const objects = yield* Effect.forEach(context.objects.values(), DatabaseService.loadOption).pipe(Effect.map(Array4.filter(Option.isSome)), Effect.map(Array4.map((option) => option.value)));
|
|
354
|
+
return Effect2.gen(this, function* () {
|
|
355
|
+
const history = yield* Effect2.promise(() => this.getHistory());
|
|
356
|
+
const blueprints = this.context.getBlueprints();
|
|
237
357
|
const toolkit = yield* createToolkit({
|
|
358
|
+
toolkit: this._toolkit,
|
|
238
359
|
blueprints
|
|
239
360
|
});
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
log("run", {
|
|
361
|
+
const objects = this.context.getObjects();
|
|
362
|
+
log2("run", {
|
|
243
363
|
history: history.length,
|
|
244
364
|
blueprints: blueprints.length,
|
|
245
|
-
|
|
246
|
-
|
|
365
|
+
tools: Object.keys(toolkit.tools).length,
|
|
366
|
+
objects: objects.length
|
|
247
367
|
}, {
|
|
248
368
|
F: __dxlog_file2,
|
|
249
|
-
L:
|
|
369
|
+
L: 112,
|
|
250
370
|
S: this,
|
|
251
371
|
C: (f, a) => f(...a)
|
|
252
372
|
});
|
|
373
|
+
const session = new AiSession();
|
|
253
374
|
const messages = yield* session.run({
|
|
254
375
|
history,
|
|
255
376
|
blueprints,
|
|
256
|
-
objects,
|
|
257
377
|
toolkit,
|
|
378
|
+
objects,
|
|
258
379
|
...params
|
|
259
|
-
}).pipe(
|
|
380
|
+
}).pipe(Effect2.provide(Layer.mergeAll(Layer.succeed(AiContextService, {
|
|
260
381
|
binder: this.context
|
|
261
|
-
}));
|
|
262
|
-
|
|
382
|
+
}), Layer.succeed(AiConversationService, this))));
|
|
383
|
+
log2("result", {
|
|
263
384
|
messages: messages.length,
|
|
264
|
-
duration:
|
|
385
|
+
duration: session.duration,
|
|
386
|
+
toolCalls: session.toolCalls
|
|
265
387
|
}, {
|
|
266
388
|
F: __dxlog_file2,
|
|
267
|
-
L:
|
|
389
|
+
L: 132,
|
|
268
390
|
S: this,
|
|
269
391
|
C: (f, a) => f(...a)
|
|
270
392
|
});
|
|
271
|
-
yield*
|
|
393
|
+
yield* Effect2.promise(() => this._queue.append(messages));
|
|
272
394
|
return messages;
|
|
273
|
-
})
|
|
274
|
-
}
|
|
275
|
-
constructor(queue) {
|
|
276
|
-
super(), /**
|
|
277
|
-
* Message and binding queue.
|
|
278
|
-
*/
|
|
279
|
-
_define_property2(this, "_queue", void 0), /**
|
|
280
|
-
* Blueprints bound to the conversation.
|
|
281
|
-
*/
|
|
282
|
-
_define_property2(this, "_context", void 0), /**
|
|
283
|
-
* Toolkit from the current session request.
|
|
284
|
-
*/
|
|
285
|
-
_define_property2(this, "_toolkit", void 0);
|
|
286
|
-
this._queue = queue;
|
|
287
|
-
this._context = new AiContextBinder(this._queue);
|
|
395
|
+
});
|
|
288
396
|
}
|
|
289
397
|
};
|
|
398
|
+
var AiConversationService = class _AiConversationService extends Context2.Tag("@dxos/assistant/AiConversationService")() {
|
|
399
|
+
/**
|
|
400
|
+
* Create a new conversation layer from options.
|
|
401
|
+
*/
|
|
402
|
+
static layer = (options) => aiContextFromConversation.pipe(Layer.provideMerge(Layer.scoped(_AiConversationService, Effect2.gen(function* () {
|
|
403
|
+
const conversation = yield* acquireReleaseResource(() => new AiConversation(options));
|
|
404
|
+
return conversation;
|
|
405
|
+
}))));
|
|
406
|
+
/**
|
|
407
|
+
* Create a new conversation with a new queue.
|
|
408
|
+
*/
|
|
409
|
+
static layerNewQueue = (options) => Layer.unwrapScoped(Effect2.gen(function* () {
|
|
410
|
+
return _AiConversationService.layer({
|
|
411
|
+
...options,
|
|
412
|
+
queue: yield* QueueService.createQueue()
|
|
413
|
+
});
|
|
414
|
+
}));
|
|
415
|
+
/**
|
|
416
|
+
* Run a prompt in the current conversation.
|
|
417
|
+
*/
|
|
418
|
+
static run = (params) => Effect2.gen(function* () {
|
|
419
|
+
const conversation = yield* _AiConversationService;
|
|
420
|
+
return yield* conversation.createRequest(params);
|
|
421
|
+
});
|
|
422
|
+
};
|
|
423
|
+
var aiContextFromConversation = Layer.effect(AiContextService, Effect2.gen(function* () {
|
|
424
|
+
const conversation = yield* AiConversationService;
|
|
425
|
+
return {
|
|
426
|
+
binder: conversation.context
|
|
427
|
+
};
|
|
428
|
+
}));
|
|
290
429
|
|
|
291
430
|
// src/diff.ts
|
|
292
431
|
import { next as A } from "@automerge/automerge";
|
|
293
432
|
import { DocAccessor, toCursorRange } from "@dxos/echo-db";
|
|
294
|
-
import { log as
|
|
433
|
+
import { log as log3 } from "@dxos/log";
|
|
295
434
|
import { isNonNullable } from "@dxos/util";
|
|
296
435
|
var __dxlog_file3 = "/__w/dxos/dxos/packages/core/assistant/src/diff.ts";
|
|
297
436
|
var reduceDiffs = (diffs) => {
|
|
@@ -321,8 +460,10 @@ var reduceDiffs = (diffs) => {
|
|
|
321
460
|
}
|
|
322
461
|
return acc;
|
|
323
462
|
}
|
|
463
|
+
default: {
|
|
464
|
+
throw new Error(`Invalid diff format: each line must start with "- " or "+ ", got: ${JSON.stringify(diff)}`);
|
|
465
|
+
}
|
|
324
466
|
}
|
|
325
|
-
return acc;
|
|
326
467
|
}, []);
|
|
327
468
|
};
|
|
328
469
|
var computeDiffsWithCursors = (accessor, diffs) => {
|
|
@@ -337,7 +478,7 @@ var computeDiffsWithCursors = (accessor, diffs) => {
|
|
|
337
478
|
}
|
|
338
479
|
}).filter(isNonNullable);
|
|
339
480
|
};
|
|
340
|
-
var applyDiffs = (accessor, diffs) => {
|
|
481
|
+
var applyDiffs = (accessor, diffs, { errorOnNotFound = false } = {}) => {
|
|
341
482
|
for (const diff of reduceDiffs(diffs)) {
|
|
342
483
|
accessor.handle.change((doc) => {
|
|
343
484
|
const text = DocAccessor.getValue(accessor);
|
|
@@ -345,12 +486,16 @@ var applyDiffs = (accessor, diffs) => {
|
|
|
345
486
|
if (idx !== -1) {
|
|
346
487
|
A.splice(doc, accessor.path, idx, diff.match.length, diff.replace);
|
|
347
488
|
} else {
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
489
|
+
if (errorOnNotFound) {
|
|
490
|
+
throw new Error(`Diff not found: ${diff.match}`);
|
|
491
|
+
} else {
|
|
492
|
+
log3.warn("diff not found", diff, {
|
|
493
|
+
F: __dxlog_file3,
|
|
494
|
+
L: 86,
|
|
495
|
+
S: void 0,
|
|
496
|
+
C: (f, a) => f(...a)
|
|
497
|
+
});
|
|
498
|
+
}
|
|
354
499
|
}
|
|
355
500
|
});
|
|
356
501
|
}
|
|
@@ -362,29 +507,31 @@ import { AgentStatus } from "@dxos/ai";
|
|
|
362
507
|
import { Obj as Obj3 } from "@dxos/echo";
|
|
363
508
|
import { MESSAGE_PROPERTY_TOOL_CALL_ID } from "@dxos/functions";
|
|
364
509
|
import { LogLevel } from "@dxos/log";
|
|
365
|
-
import { ContentBlock,
|
|
510
|
+
import { ContentBlock, Message as Message2 } from "@dxos/types";
|
|
366
511
|
import { isNonNullable as isNonNullable2 } from "@dxos/util";
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
value,
|
|
371
|
-
enumerable: true,
|
|
372
|
-
configurable: true,
|
|
373
|
-
writable: true
|
|
374
|
-
});
|
|
375
|
-
} else {
|
|
376
|
-
obj[key] = value;
|
|
377
|
-
}
|
|
378
|
-
return obj;
|
|
379
|
-
}
|
|
512
|
+
var SKIP_BLOCKS = [
|
|
513
|
+
"text"
|
|
514
|
+
];
|
|
380
515
|
var ExecutionGraph = class {
|
|
516
|
+
_skipBlocks;
|
|
517
|
+
// TODO(dmaretskyi): Evolve the internal state to support chats, functions, circuit workflows, etc.
|
|
518
|
+
_commits = [];
|
|
519
|
+
_branchNames = /* @__PURE__ */ new Set();
|
|
520
|
+
_lastBlockId;
|
|
521
|
+
// Tool call tracking.
|
|
522
|
+
_toolCallCommitIds = /* @__PURE__ */ new Map();
|
|
523
|
+
_lastCommitByBranch = /* @__PURE__ */ new Map();
|
|
524
|
+
_pendingToolResults = /* @__PURE__ */ new Map();
|
|
525
|
+
constructor(_skipBlocks = SKIP_BLOCKS) {
|
|
526
|
+
this._skipBlocks = _skipBlocks;
|
|
527
|
+
}
|
|
381
528
|
/**
|
|
382
529
|
* Adds events to the graph.
|
|
383
530
|
*/
|
|
384
531
|
addEvents(events) {
|
|
385
532
|
const sortedEvents = this.sortObjectsByCreated(events);
|
|
386
533
|
for (const event of sortedEvents) {
|
|
387
|
-
if (Obj3.instanceOf(
|
|
534
|
+
if (Obj3.instanceOf(Message2.Message, event)) {
|
|
388
535
|
this._processMessage(event);
|
|
389
536
|
} else if (Obj3.instanceOf(AgentStatus, event)) {
|
|
390
537
|
this._processAgentStatus(event);
|
|
@@ -395,7 +542,7 @@ var ExecutionGraph = class {
|
|
|
395
542
|
* Processes a message event and creates commits for its blocks.
|
|
396
543
|
*/
|
|
397
544
|
_processMessage(message) {
|
|
398
|
-
const messageCommits = messageToCommits(message, this._lastBlockId, this._toolCallCommitIds, this._lastCommitByBranch);
|
|
545
|
+
const messageCommits = messageToCommits(message, this._lastBlockId, this._toolCallCommitIds, this._lastCommitByBranch, this._skipBlocks);
|
|
399
546
|
this._commits.push(...messageCommits);
|
|
400
547
|
messageCommits.forEach((commit) => this._branchNames.add(commit.branch));
|
|
401
548
|
this._trackToolCalls(messageCommits);
|
|
@@ -559,20 +706,15 @@ var ExecutionGraph = class {
|
|
|
559
706
|
const toolResultMatch = commitId.match(/_toolResult_(.+)$/);
|
|
560
707
|
return toolCallMatch?.[1] || toolResultMatch?.[1];
|
|
561
708
|
}
|
|
562
|
-
constructor() {
|
|
563
|
-
_define_property3(this, "_commits", []);
|
|
564
|
-
_define_property3(this, "_branchNames", /* @__PURE__ */ new Set());
|
|
565
|
-
_define_property3(this, "_lastBlockId", void 0);
|
|
566
|
-
_define_property3(this, "_toolCallCommitIds", /* @__PURE__ */ new Map());
|
|
567
|
-
_define_property3(this, "_lastCommitByBranch", /* @__PURE__ */ new Map());
|
|
568
|
-
_define_property3(this, "_pendingToolResults", /* @__PURE__ */ new Map());
|
|
569
|
-
}
|
|
570
709
|
};
|
|
571
|
-
var messageToCommits = (message, lastBlockId, toolCallIds, lastCommitByBranch) => {
|
|
710
|
+
var messageToCommits = (message, lastBlockId, toolCallIds, lastCommitByBranch, skipBlocks) => {
|
|
572
711
|
let previousBlockId = lastBlockId;
|
|
573
712
|
return message.blocks.map((block, idx) => {
|
|
574
713
|
const branch = getMessageBranch(message);
|
|
575
714
|
const parents = getBlockParents(block, previousBlockId, message, toolCallIds, lastCommitByBranch);
|
|
715
|
+
if (skipBlocks?.includes(block._tag)) {
|
|
716
|
+
return null;
|
|
717
|
+
}
|
|
576
718
|
const commit = createBlockCommit(block, message, branch, parents, idx);
|
|
577
719
|
if (commit) {
|
|
578
720
|
previousBlockId = commit.id;
|
|
@@ -730,128 +872,14 @@ var stringifyObject = (obj) => {
|
|
|
730
872
|
return Obj3.getLabel(obj) ?? Obj3.getTypename(obj) ?? obj.id;
|
|
731
873
|
};
|
|
732
874
|
|
|
733
|
-
// src/functions/services.ts
|
|
734
|
-
import * as Tool from "@effect/ai/Tool";
|
|
735
|
-
import * as Context2 from "effect/Context";
|
|
736
|
-
import * as Effect2 from "effect/Effect";
|
|
737
|
-
import * as Layer from "effect/Layer";
|
|
738
|
-
import * as Record from "effect/Record";
|
|
739
|
-
import * as Schema2 from "effect/Schema";
|
|
740
|
-
import { AiToolNotFoundError, ToolExecutionService, ToolResolverService } from "@dxos/ai";
|
|
741
|
-
import { todo } from "@dxos/debug";
|
|
742
|
-
import { Query as Query2 } from "@dxos/echo";
|
|
743
|
-
import { DatabaseService as DatabaseService2, FunctionDefinition, FunctionInvocationService, FunctionType } from "@dxos/functions";
|
|
744
|
-
import { invariant as invariant2 } from "@dxos/invariant";
|
|
745
|
-
var __dxlog_file4 = "/__w/dxos/dxos/packages/core/assistant/src/functions/services.ts";
|
|
746
|
-
var makeToolResolverFromFunctions = (functions, toolkit) => {
|
|
747
|
-
return Layer.effect(ToolResolverService, Effect2.gen(function* () {
|
|
748
|
-
const dbService = yield* DatabaseService2;
|
|
749
|
-
return {
|
|
750
|
-
resolve: (id) => Effect2.gen(function* () {
|
|
751
|
-
const tool = toolkit.tools[id];
|
|
752
|
-
if (tool) {
|
|
753
|
-
return tool;
|
|
754
|
-
}
|
|
755
|
-
const { objects: [dbFunction] } = yield* DatabaseService2.runQuery(Query2.type(FunctionType, {
|
|
756
|
-
key: id
|
|
757
|
-
}));
|
|
758
|
-
const functionDef = dbFunction ? FunctionDefinition.deserialize(dbFunction) : functions.find((fn2) => fn2.key === id);
|
|
759
|
-
if (!functionDef) {
|
|
760
|
-
return yield* Effect2.fail(new AiToolNotFoundError(id));
|
|
761
|
-
}
|
|
762
|
-
return projectFunctionToTool(functionDef);
|
|
763
|
-
}).pipe(Effect2.provideService(DatabaseService2, dbService))
|
|
764
|
-
};
|
|
765
|
-
}));
|
|
766
|
-
};
|
|
767
|
-
var makeToolExecutionServiceFromFunctions = (toolkit, handlersLayer) => {
|
|
768
|
-
return Layer.effect(ToolExecutionService, Effect2.gen(function* () {
|
|
769
|
-
const toolkitHandler = yield* toolkit.pipe(Effect2.provide(handlersLayer));
|
|
770
|
-
invariant2(isHandlerLike(toolkitHandler), void 0, {
|
|
771
|
-
F: __dxlog_file4,
|
|
772
|
-
L: 73,
|
|
773
|
-
S: this,
|
|
774
|
-
A: [
|
|
775
|
-
"isHandlerLike(toolkitHandler)",
|
|
776
|
-
""
|
|
777
|
-
]
|
|
778
|
-
});
|
|
779
|
-
const functionInvocationService = yield* FunctionInvocationService;
|
|
780
|
-
return {
|
|
781
|
-
handlersFor: (toolkit2) => {
|
|
782
|
-
const makeHandler = (tool) => {
|
|
783
|
-
return Effect2.fn(`toolFunctionHandler ${tool.name}`)(function* (input) {
|
|
784
|
-
if (toolkitHandler.tools[tool.name]) {
|
|
785
|
-
if (Tool.isProviderDefined(tool)) {
|
|
786
|
-
throw new Error("Attempted to call a provider-defined tool");
|
|
787
|
-
}
|
|
788
|
-
return yield* toolkitHandler.handle(tool.name, input);
|
|
789
|
-
}
|
|
790
|
-
const { definition: functionDef } = Context2.get(FunctionToolAnnotation)(tool.annotations);
|
|
791
|
-
return yield* functionInvocationService.invokeFunction(functionDef, input).pipe(Effect2.catchAllDefect((defect) => Effect2.fail(defect)));
|
|
792
|
-
});
|
|
793
|
-
};
|
|
794
|
-
return toolkit2.of(Record.map(toolkit2.tools, (tool, _name) => Tool.isUserDefined(tool) ? makeHandler(tool) : null));
|
|
795
|
-
}
|
|
796
|
-
};
|
|
797
|
-
}));
|
|
798
|
-
};
|
|
799
|
-
var FunctionToolAnnotation = class extends Context2.Tag("@dxos/assistant/FunctionToolAnnotation")() {
|
|
800
|
-
};
|
|
801
|
-
var toolCache = /* @__PURE__ */ new WeakMap();
|
|
802
|
-
var projectFunctionToTool = (fn2) => {
|
|
803
|
-
if (toolCache.has(fn2)) {
|
|
804
|
-
return toolCache.get(fn2);
|
|
805
|
-
}
|
|
806
|
-
const tool = Tool.make(makeToolName(fn2.name), {
|
|
807
|
-
description: fn2.description,
|
|
808
|
-
parameters: createStructFieldsFromSchema(fn2.inputSchema),
|
|
809
|
-
// TODO(dmaretskyi): Include output schema.
|
|
810
|
-
failure: Schema2.Any
|
|
811
|
-
}).annotate(FunctionToolAnnotation, {
|
|
812
|
-
definition: fn2
|
|
813
|
-
});
|
|
814
|
-
toolCache.set(fn2, tool);
|
|
815
|
-
return tool;
|
|
816
|
-
};
|
|
817
|
-
var makeToolName = (name) => {
|
|
818
|
-
const toolName = name.replace(/[^a-zA-Z0-9]/g, "_");
|
|
819
|
-
invariant2(toolName.match(/^[a-zA-Z_][a-zA-Z0-9-_]*$/), void 0, {
|
|
820
|
-
F: __dxlog_file4,
|
|
821
|
-
L: 140,
|
|
822
|
-
S: void 0,
|
|
823
|
-
A: [
|
|
824
|
-
"toolName.match(/^[a-zA-Z_][a-zA-Z0-9-_]*$/)",
|
|
825
|
-
""
|
|
826
|
-
]
|
|
827
|
-
});
|
|
828
|
-
return toolName;
|
|
829
|
-
};
|
|
830
|
-
var createStructFieldsFromSchema = (schema) => {
|
|
831
|
-
switch (schema.ast._tag) {
|
|
832
|
-
case "TypeLiteral":
|
|
833
|
-
return Object.fromEntries(schema.ast.propertySignatures.map((prop) => [
|
|
834
|
-
prop.name,
|
|
835
|
-
Schema2.make(prop.type)
|
|
836
|
-
]));
|
|
837
|
-
case "VoidKeyword":
|
|
838
|
-
return {};
|
|
839
|
-
default:
|
|
840
|
-
return todo(`Unsupported schema AST: ${schema.ast._tag}`);
|
|
841
|
-
}
|
|
842
|
-
};
|
|
843
|
-
var isHandlerLike = (value) => {
|
|
844
|
-
return typeof value.tools === "object" && typeof value.handle === "function";
|
|
845
|
-
};
|
|
846
|
-
|
|
847
875
|
// src/templates/system.ts
|
|
848
876
|
import defaultsDeep from "lodash.defaultsdeep";
|
|
849
877
|
import { Template } from "@dxos/blueprints";
|
|
850
878
|
|
|
851
|
-
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/format.tpl?raw
|
|
852
|
-
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>\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##
|
|
879
|
+
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/instructions/format.tpl?raw
|
|
880
|
+
var format_default = "{{! System Core }}\n\n## Input Formats\n\n### Blueprints\n\nYou may be provided with Blueprint definitions that include specific instructions and tools for a given set of tasks.\nBlue print instructions will be enclosed in a <blueprint> tag.\n\n### Context objects\n\nYou may be provided with references to objects that are relevant to the current prompt.\nObjects will be in the form of a <object> tag with a <dxn> element that is the unique key of the object required by many tools.\nThe <typename> element is the type of the object.\n\n<object>\n <dxn>object-id</dxn>\n <typename>example.com/type/TypeName</typename>\n</object>\n\n## Response Formats\n\nIt is very important to respond in the correct format.\nYou response must be valid Markdown.\nIn some cases you should respond with well-formed XML tags along with the content.\nCode snippets should use 2-space indents.\n\n{{#if cot}}\nBefore responding, explain your reasoning and include your detailed chain-of-thought enclosed in a <cot> tag.\nThe <cot> tag should be the first thing in your response.\n{{/if}}\n\n## Block references\n\nYou can reply with a block reference for objects if you have the DXN. For example:\n\n<object>\n <dxn>dxn:queue:data:B6INSIBY3CBEF4M5VZRYBCMAHQMPYK5AJ:01K24XMVHSZHS97SG1VTVQDM5Z:01K24XPK464FSCKVQJAB2H662M</dxn>\n</object>\n<object>\n <dxn>dxn:echo:@:01K24XPK464FSCKVQJAB2H662M</dxn>\n</object>\n\n### Suggestions\n\nEach suggestion must in the form of a proposed action enclosed in a <suggestion> tag.\nExample: <suggestion>Show on a map</suggestion>\nDo not use bullets when listing suggestions.\n\n### Multiple choice questions\n\nYou may ask multiple choice questions by wrapping consise options inside <option> tags inside a well formed <select> tag.\nExample: <select><option>Yes</option><option>No</option></select>\n\n## Context\n\nThe current date and time is {{DATETIME}}.\n";
|
|
853
881
|
|
|
854
|
-
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/system.tpl?raw
|
|
882
|
+
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/instructions/system.tpl?raw
|
|
855
883
|
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";
|
|
856
884
|
|
|
857
885
|
// src/templates/system.ts
|
|
@@ -868,49 +896,12 @@ var templates = {
|
|
|
868
896
|
source: system_default
|
|
869
897
|
})
|
|
870
898
|
};
|
|
871
|
-
|
|
872
|
-
// src/types/artifact.ts
|
|
873
|
-
import * as Schema3 from "effect/Schema";
|
|
874
|
-
import { DXN as DXN2, LOCAL_SPACE_TAG } from "@dxos/keys";
|
|
875
|
-
import { trim } from "@dxos/util";
|
|
876
|
-
var createArtifactElement = (id) => `<artifact id=${id} />`;
|
|
877
|
-
var ArtifactId = class extends Schema3.String.annotations({
|
|
878
|
-
description: trim`
|
|
879
|
-
The ID of the referenced object. Formats accepted:
|
|
880
|
-
- DXN (dxn:echo:@:XXXXX). DXNs can be prepended with an @ symbol for compatibility with in-text references.
|
|
881
|
-
- space ID, object ID tuple (spaceID:objectID)
|
|
882
|
-
- Only object ID that is assumed to be in the current space (XXXXX)
|
|
883
|
-
`,
|
|
884
|
-
examples: [
|
|
885
|
-
"dxn:echo:@:XXXXX",
|
|
886
|
-
"@dxn:echo:@:XXXXX",
|
|
887
|
-
"spaceID:objectID",
|
|
888
|
-
"XXXXX"
|
|
889
|
-
]
|
|
890
|
-
}) {
|
|
891
|
-
static toDXN(reference, owningSpaceId) {
|
|
892
|
-
if (reference.startsWith("@dxn:")) {
|
|
893
|
-
return DXN2.parse(reference.slice(1));
|
|
894
|
-
} else if (reference.startsWith("dxn:")) {
|
|
895
|
-
return DXN2.parse(reference);
|
|
896
|
-
} else if (/^[A-Z0-9]+:[A-Z0-9]+$/.test(reference)) {
|
|
897
|
-
const [spaceId, objectId] = reference.split(":");
|
|
898
|
-
return new DXN2(DXN2.kind.ECHO, [
|
|
899
|
-
spaceId === owningSpaceId ? LOCAL_SPACE_TAG : spaceId,
|
|
900
|
-
objectId
|
|
901
|
-
]);
|
|
902
|
-
} else if (/^[A-Z0-9]+$/.test(reference)) {
|
|
903
|
-
return DXN2.fromLocalObjectId(reference);
|
|
904
|
-
} else {
|
|
905
|
-
throw new Error(`Unable to parse object reference: ${reference}`);
|
|
906
|
-
}
|
|
907
|
-
}
|
|
908
|
-
};
|
|
909
899
|
export {
|
|
910
900
|
AiAssistantError,
|
|
911
901
|
AiContextBinder,
|
|
912
902
|
AiContextService,
|
|
913
903
|
AiConversation,
|
|
904
|
+
AiConversationService,
|
|
914
905
|
AiModelError,
|
|
915
906
|
AiSession,
|
|
916
907
|
ArtifactDiffResolver,
|
|
@@ -919,12 +910,17 @@ export {
|
|
|
919
910
|
ContextBinding,
|
|
920
911
|
ExecutionGraph,
|
|
921
912
|
GenerationObserver,
|
|
913
|
+
GenericToolkit_exports as GenericToolkit,
|
|
914
|
+
RefFromLLM,
|
|
915
|
+
ToolExecutionServices,
|
|
922
916
|
VersionPin,
|
|
923
917
|
applyDiffs,
|
|
924
918
|
computeDiffsWithCursors,
|
|
925
919
|
createArtifactElement,
|
|
926
920
|
createSystemPrompt,
|
|
927
921
|
createToolkit,
|
|
922
|
+
formatSystemPrompt,
|
|
923
|
+
formatUserPrompt,
|
|
928
924
|
makeToolExecutionServiceFromFunctions,
|
|
929
925
|
makeToolResolverFromFunctions,
|
|
930
926
|
reduceDiffs,
|