@dxos/assistant 0.8.4-main.5ad4a44 → 0.8.4-main.66e292d
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-J5LGTIGS.mjs +10 -0
- package/dist/lib/browser/extraction/index.mjs +117 -164
- package/dist/lib/browser/extraction/index.mjs.map +4 -4
- package/dist/lib/browser/index.mjs +585 -165
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
- package/dist/lib/node-esm/extraction/index.mjs +117 -164
- package/dist/lib/node-esm/extraction/index.mjs.map +4 -4
- package/dist/lib/node-esm/index.mjs +585 -165
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/conversation/context.d.ts +45 -45
- package/dist/types/src/conversation/context.d.ts.map +1 -1
- package/dist/types/src/conversation/conversation.d.ts +8 -8
- package/dist/types/src/conversation/conversation.d.ts.map +1 -1
- package/dist/types/src/diff.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +14 -18
- 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 +47 -42
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction.d.ts +104 -107
- package/dist/types/src/extraction/extraction.d.ts.map +1 -1
- package/dist/types/src/extraction/quotes.d.ts.map +1 -1
- package/dist/types/src/functions/services.d.ts +3 -2
- package/dist/types/src/functions/services.d.ts.map +1 -1
- package/dist/types/src/session/GenericToolkit.d.ts +84 -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 +3 -175
- package/dist/types/src/session/format.d.ts.map +1 -1
- package/dist/types/src/session/index.d.ts +1 -0
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/observer.d.ts +2 -2
- package/dist/types/src/session/observer.d.ts.map +1 -1
- package/dist/types/src/session/session.d.ts +3 -3
- package/dist/types/src/session/session.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/{types.d.ts → types/artifact.d.ts} +6 -3
- package/dist/types/src/types/artifact.d.ts.map +1 -0
- package/dist/types/src/types/index.d.ts +2 -0
- package/dist/types/src/types/index.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +22 -20
- package/src/conversation/context.ts +49 -20
- package/src/conversation/conversation.ts +20 -13
- package/src/diff.test.ts +3 -3
- package/src/diff.ts +5 -0
- package/src/execution-graph.test.ts +45 -43
- package/src/execution-graph.ts +21 -12
- 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 +11 -10
- 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 +7 -6
- package/src/extraction/quotes.ts +1 -1
- package/src/functions/services.ts +15 -11
- package/src/session/GenericToolkit.ts +130 -0
- package/src/session/artifact-diff.ts +1 -1
- package/src/session/format.test.ts +4 -3
- package/src/session/format.ts +10 -8
- package/src/session/index.ts +2 -0
- package/src/session/observer.ts +2 -2
- package/src/session/session.test.ts +2 -2
- package/src/session/session.ts +9 -8
- package/src/session/version-pin.ts +6 -6
- package/src/templates/{format.tpl → instructions/format.tpl} +1 -0
- package/src/templates/system.ts +2 -2
- package/src/{types.ts → types/artifact.ts} +21 -3
- package/src/types/index.ts +5 -0
- 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/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/named-entity-recognition-2HDF46YX.mjs +0 -14
- package/dist/types/src/summarization/summarization.test.d.ts +0 -2
- package/dist/types/src/summarization/summarization.test.d.ts.map +0 -1
- package/dist/types/src/types.d.ts.map +0 -1
- package/src/summarization/summarization.test.ts +0 -202
- /package/dist/lib/browser/{named-entity-recognition-QPYHML66.mjs.map → chunk-J5LGTIGS.mjs.map} +0 -0
- /package/dist/lib/node-esm/{named-entity-recognition-2HDF46YX.mjs.map → chunk-HSLMI22Q.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
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
AiSession,
|
|
5
|
-
ArtifactDiffResolver,
|
|
6
|
-
GenerationObserver,
|
|
7
|
-
VersionPin,
|
|
8
|
-
createToolkit
|
|
9
|
-
} from "./chunk-CWGVG235.mjs";
|
|
2
|
+
__export
|
|
3
|
+
} from "./chunk-J5LGTIGS.mjs";
|
|
10
4
|
|
|
11
5
|
// src/conversation/context.ts
|
|
12
6
|
import { computed } from "@preact/signals-core";
|
|
@@ -15,62 +9,115 @@ import * as Context from "effect/Context";
|
|
|
15
9
|
import * as Function from "effect/Function";
|
|
16
10
|
import * as Schema from "effect/Schema";
|
|
17
11
|
import { Blueprint } from "@dxos/blueprints";
|
|
18
|
-
import {
|
|
12
|
+
import { Resource } from "@dxos/context";
|
|
13
|
+
import { DXN, Filter, Obj, Query, Type } from "@dxos/echo";
|
|
14
|
+
import { invariant } from "@dxos/invariant";
|
|
19
15
|
import { ComplexSet } from "@dxos/util";
|
|
20
|
-
|
|
21
|
-
if (key in obj) {
|
|
22
|
-
Object.defineProperty(obj, key, {
|
|
23
|
-
value,
|
|
24
|
-
enumerable: true,
|
|
25
|
-
configurable: true,
|
|
26
|
-
writable: true
|
|
27
|
-
});
|
|
28
|
-
} else {
|
|
29
|
-
obj[key] = value;
|
|
30
|
-
}
|
|
31
|
-
return obj;
|
|
32
|
-
}
|
|
16
|
+
var __dxlog_file = "/__w/dxos/dxos/packages/core/assistant/src/conversation/context.ts";
|
|
33
17
|
var ContextBinding = Schema.Struct({
|
|
34
18
|
blueprints: Schema.Struct({
|
|
35
19
|
added: Schema.Array(Type.Ref(Blueprint.Blueprint)),
|
|
36
20
|
removed: Schema.Array(Type.Ref(Blueprint.Blueprint))
|
|
37
21
|
}),
|
|
38
|
-
// TODO(burdon): Type.Expando => Type.Obj (or Obj.Any?)
|
|
39
22
|
objects: Schema.Struct({
|
|
40
|
-
added: Schema.Array(Type.Ref(
|
|
41
|
-
removed: Schema.Array(Type.Ref(
|
|
23
|
+
added: Schema.Array(Type.Ref(Obj.Any)),
|
|
24
|
+
removed: Schema.Array(Type.Ref(Obj.Any))
|
|
42
25
|
})
|
|
43
26
|
}).pipe(Type.Obj({
|
|
44
27
|
typename: "dxos.org/type/ContextBinding",
|
|
45
28
|
version: "0.1.0"
|
|
46
29
|
}));
|
|
47
30
|
var Bindings = class {
|
|
31
|
+
blueprints = new ComplexSet((ref) => ref.dxn.toString());
|
|
32
|
+
// TODO(burdon): Some DXNs have the Space prefix so only compare the object ID.
|
|
33
|
+
objects = new ComplexSet((ref) => ref.dxn.asEchoDXN()?.echoId);
|
|
48
34
|
toJSON() {
|
|
49
35
|
return {
|
|
50
36
|
blueprints: Array2.fromIterable(this.blueprints).map((ref) => ref.dxn.toString()),
|
|
51
37
|
objects: Array2.fromIterable(this.objects).map((ref) => ref.dxn.toString())
|
|
52
38
|
};
|
|
53
39
|
}
|
|
54
|
-
constructor() {
|
|
55
|
-
_define_property(this, "blueprints", new ComplexSet((ref) => ref.dxn.toString()));
|
|
56
|
-
_define_property(this, "objects", new ComplexSet((ref) => ref.dxn.asEchoDXN()?.echoId));
|
|
57
|
-
}
|
|
58
40
|
};
|
|
59
|
-
var AiContextBinder = class {
|
|
41
|
+
var AiContextBinder = class extends Resource {
|
|
42
|
+
_queue;
|
|
43
|
+
/**
|
|
44
|
+
* Reactive query of all bindings.
|
|
45
|
+
*/
|
|
46
|
+
// TODO(burdon): Cache value?
|
|
47
|
+
_bindings;
|
|
48
|
+
_blueprints;
|
|
49
|
+
_objects;
|
|
50
|
+
constructor(_queue) {
|
|
51
|
+
super(), this._queue = _queue;
|
|
52
|
+
}
|
|
53
|
+
get bindings() {
|
|
54
|
+
invariant(this._bindings, "AiContextBinder not open", {
|
|
55
|
+
F: __dxlog_file,
|
|
56
|
+
L: 77,
|
|
57
|
+
S: this,
|
|
58
|
+
A: [
|
|
59
|
+
"this._bindings",
|
|
60
|
+
"'AiContextBinder not open'"
|
|
61
|
+
]
|
|
62
|
+
});
|
|
63
|
+
return this._bindings;
|
|
64
|
+
}
|
|
65
|
+
get blueprints() {
|
|
66
|
+
invariant(this._blueprints, "AiContextBinder not open", {
|
|
67
|
+
F: __dxlog_file,
|
|
68
|
+
L: 82,
|
|
69
|
+
S: this,
|
|
70
|
+
A: [
|
|
71
|
+
"this._blueprints",
|
|
72
|
+
"'AiContextBinder not open'"
|
|
73
|
+
]
|
|
74
|
+
});
|
|
75
|
+
return this._blueprints;
|
|
76
|
+
}
|
|
77
|
+
get objects() {
|
|
78
|
+
invariant(this._objects, "AiContextBinder not open", {
|
|
79
|
+
F: __dxlog_file,
|
|
80
|
+
L: 87,
|
|
81
|
+
S: this,
|
|
82
|
+
A: [
|
|
83
|
+
"this._objects",
|
|
84
|
+
"'AiContextBinder not open'"
|
|
85
|
+
]
|
|
86
|
+
});
|
|
87
|
+
return this._objects;
|
|
88
|
+
}
|
|
89
|
+
// TODO(wittjosiah): Use parent context?
|
|
90
|
+
async _open() {
|
|
91
|
+
const query = this._queue.query(Query.select(Filter.everything()));
|
|
92
|
+
this._ctx.onDispose(query.subscribe(() => {
|
|
93
|
+
}));
|
|
94
|
+
this._bindings = computed(() => this._reduce(query.objects));
|
|
95
|
+
this._blueprints = computed(() => [
|
|
96
|
+
...this.bindings.value.blueprints
|
|
97
|
+
]);
|
|
98
|
+
this._objects = computed(() => [
|
|
99
|
+
...this.bindings.value.objects
|
|
100
|
+
]);
|
|
101
|
+
}
|
|
102
|
+
async _close() {
|
|
103
|
+
this._bindings = void 0;
|
|
104
|
+
this._blueprints = void 0;
|
|
105
|
+
this._objects = void 0;
|
|
106
|
+
}
|
|
60
107
|
/**
|
|
61
108
|
* Asynchronous query of all bindings.
|
|
62
109
|
*/
|
|
63
110
|
async query() {
|
|
64
|
-
const
|
|
65
|
-
return this._reduce(
|
|
111
|
+
const { objects } = await this._queue.query(Query.select(Filter.everything())).run();
|
|
112
|
+
return this._reduce(objects);
|
|
66
113
|
}
|
|
67
114
|
// TODO(burdon): Pass in Blueprint obj (from registry?) and create reference.
|
|
68
115
|
async bind(props) {
|
|
69
|
-
if (!props.blueprints?.length && !props.objects?.length) {
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
116
|
const blueprints = props.blueprints?.filter((ref) => !this.blueprints.peek().find((b) => b.dxn.toString() === ref.dxn.toString())) ?? [];
|
|
73
117
|
const objects = props.objects?.filter((ref) => !this.objects.peek().find((o) => o.dxn.toString() === ref.dxn.toString())) ?? [];
|
|
118
|
+
if (!blueprints.length && !objects.length) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
74
121
|
await this._queue.append([
|
|
75
122
|
Obj.make(ContextBinding, {
|
|
76
123
|
blueprints: {
|
|
@@ -116,47 +163,418 @@ var AiContextBinder = class {
|
|
|
116
163
|
return context;
|
|
117
164
|
}));
|
|
118
165
|
}
|
|
119
|
-
constructor(_queue) {
|
|
120
|
-
_define_property(this, "_queue", void 0);
|
|
121
|
-
_define_property(this, "bindings", void 0);
|
|
122
|
-
_define_property(this, "blueprints", void 0);
|
|
123
|
-
_define_property(this, "objects", void 0);
|
|
124
|
-
this._queue = _queue;
|
|
125
|
-
this.bindings = computed(() => this._reduce(this._queue.objects));
|
|
126
|
-
this.blueprints = computed(() => [
|
|
127
|
-
...this.bindings.value.blueprints
|
|
128
|
-
]);
|
|
129
|
-
this.objects = computed(() => [
|
|
130
|
-
...this.bindings.value.objects
|
|
131
|
-
]);
|
|
132
|
-
}
|
|
133
166
|
};
|
|
134
167
|
var AiContextService = class extends Context.Tag("@dxos/assistant/AiContextService")() {
|
|
135
168
|
};
|
|
136
169
|
|
|
137
170
|
// src/conversation/conversation.ts
|
|
138
|
-
import * as
|
|
171
|
+
import * as Array5 from "effect/Array";
|
|
172
|
+
import * as Effect6 from "effect/Effect";
|
|
173
|
+
import * as Option2 from "effect/Option";
|
|
174
|
+
import { Resource as Resource2 } from "@dxos/context";
|
|
175
|
+
import { Obj as Obj4 } from "@dxos/echo";
|
|
176
|
+
import { DatabaseService as DatabaseService2 } from "@dxos/echo-db";
|
|
177
|
+
import { log as log3 } from "@dxos/log";
|
|
178
|
+
import { Message as Message3 } from "@dxos/types";
|
|
179
|
+
|
|
180
|
+
// src/session/artifact-diff.ts
|
|
181
|
+
import * as Context2 from "effect/Context";
|
|
182
|
+
var ArtifactDiffResolver = class extends Context2.Tag("@dxos/assistant/ArtifactDiffResolver")() {
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
// src/session/observer.ts
|
|
139
186
|
import * as Effect from "effect/Effect";
|
|
187
|
+
import * as Function2 from "effect/Function";
|
|
188
|
+
var GenerationObserver = Object.freeze({
|
|
189
|
+
make: ({ onBegin = Function2.constant(Effect.void), onPart = Function2.constant(Effect.void), onBlock = Function2.constant(Effect.void), onEnd = Function2.constant(Effect.void), onMessage = Function2.constant(Effect.void) } = {}) => ({
|
|
190
|
+
onBegin,
|
|
191
|
+
onPart,
|
|
192
|
+
onBlock,
|
|
193
|
+
onEnd,
|
|
194
|
+
onMessage
|
|
195
|
+
}),
|
|
196
|
+
noop: () => GenerationObserver.make(),
|
|
197
|
+
/**
|
|
198
|
+
* Debug printer to be used in unit-tests and browser devtools.
|
|
199
|
+
*/
|
|
200
|
+
fromPrinter: (printer) => GenerationObserver.make({
|
|
201
|
+
onBlock: (block) => Effect.sync(() => {
|
|
202
|
+
if (block.pending) {
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
printer.printContentBlock(block);
|
|
206
|
+
}),
|
|
207
|
+
onMessage: (message) => Effect.sync(() => {
|
|
208
|
+
if (message.sender.role === "assistant") {
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
printer.printMessage(message);
|
|
212
|
+
})
|
|
213
|
+
})
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
// src/session/session.ts
|
|
217
|
+
import * as LanguageModel from "@effect/ai/LanguageModel";
|
|
218
|
+
import * as Chunk from "effect/Chunk";
|
|
219
|
+
import * as Effect3 from "effect/Effect";
|
|
220
|
+
import * as Stream from "effect/Stream";
|
|
221
|
+
import { AiParser, AiPreprocessor, callTool, getToolCalls } from "@dxos/ai";
|
|
222
|
+
import { todo } from "@dxos/debug";
|
|
223
|
+
import { Obj as Obj3 } from "@dxos/echo";
|
|
224
|
+
import { TracingService } from "@dxos/functions";
|
|
225
|
+
import { log as log2 } from "@dxos/log";
|
|
226
|
+
import { Message as Message2 } from "@dxos/types";
|
|
227
|
+
|
|
228
|
+
// src/session/format.ts
|
|
229
|
+
import * as Array4 from "effect/Array";
|
|
230
|
+
import * as Effect2 from "effect/Effect";
|
|
231
|
+
import * as Function3 from "effect/Function";
|
|
140
232
|
import * as Option from "effect/Option";
|
|
233
|
+
import { Template } from "@dxos/blueprints";
|
|
141
234
|
import { Obj as Obj2 } from "@dxos/echo";
|
|
142
|
-
import { DatabaseService } from "@dxos/
|
|
235
|
+
import { DatabaseService, ObjectVersion } from "@dxos/echo-db";
|
|
143
236
|
import { log } from "@dxos/log";
|
|
144
|
-
import {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
237
|
+
import { Message } from "@dxos/types";
|
|
238
|
+
import { trim } from "@dxos/util";
|
|
239
|
+
|
|
240
|
+
// src/errors.ts
|
|
241
|
+
import { BaseError } from "@dxos/errors";
|
|
242
|
+
var AiModelError = class extends BaseError.extend("AI_MODEL_ERROR", "AI model error") {
|
|
243
|
+
};
|
|
244
|
+
var AiAssistantError = class extends BaseError.extend("AI_ASSISTANT_ERROR", "AI assistant error") {
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
// src/session/format.ts
|
|
248
|
+
var __dxlog_file2 = "/__w/dxos/dxos/packages/core/assistant/src/session/format.ts";
|
|
249
|
+
var formatSystemPrompt = ({ system, blueprints = [], objects = [] }) => Effect2.gen(function* () {
|
|
250
|
+
const blueprintDefs = yield* Function3.pipe(blueprints, Effect2.forEach((blueprint) => Effect2.succeed(blueprint.instructions)), Effect2.flatMap(Effect2.forEach((template) => DatabaseService.loadOption(template.source))), Effect2.map(Array4.filter(Option.isSome)), Effect2.map(Array4.map((template) => trim`
|
|
251
|
+
<blueprint>
|
|
252
|
+
${Template.process(template.value.content)}
|
|
253
|
+
</blueprint>
|
|
254
|
+
`)), Effect2.map((blueprints2) => blueprints2.length > 0 ? [
|
|
255
|
+
"## Blueprints Definitions",
|
|
256
|
+
...blueprints2
|
|
257
|
+
].join("\n\n") : void 0));
|
|
258
|
+
const objectDefs = yield* Function3.pipe(objects, Effect2.forEach((object) => Effect2.succeed(trim`
|
|
259
|
+
<object>
|
|
260
|
+
<dxn>${Obj2.getDXN(object)}</dxn>
|
|
261
|
+
<typename>${Obj2.getTypename(object)}</typename>
|
|
262
|
+
</object>
|
|
263
|
+
`)), Effect2.map((objects2) => objects2.length > 0 ? [
|
|
264
|
+
"## Context Objects",
|
|
265
|
+
...objects2
|
|
266
|
+
].join("\n\n") : void 0));
|
|
267
|
+
return yield* Function3.pipe(Effect2.succeed([
|
|
268
|
+
system,
|
|
269
|
+
blueprintDefs,
|
|
270
|
+
objectDefs
|
|
271
|
+
].filter((def) => def !== void 0)), Effect2.map((parts) => parts.join("\n\n")));
|
|
272
|
+
});
|
|
273
|
+
var formatUserPrompt = ({ prompt, history = [] }) => Effect2.gen(function* () {
|
|
274
|
+
const blocks = [];
|
|
275
|
+
const artifactDiffResolver = yield* Effect2.serviceOption(ArtifactDiffResolver);
|
|
276
|
+
if (Option.isSome(artifactDiffResolver)) {
|
|
277
|
+
const versions = gatherObjectVersions(history);
|
|
278
|
+
const artifactDiff = yield* Effect2.tryPromise({
|
|
279
|
+
try: () => artifactDiffResolver.value.resolve([
|
|
280
|
+
...versions.entries()
|
|
281
|
+
].map(([id, version]) => ({
|
|
282
|
+
id,
|
|
283
|
+
lastVersion: version
|
|
284
|
+
}))),
|
|
285
|
+
catch: AiAssistantError.wrap({
|
|
286
|
+
message: "Artifact diff resolution error"
|
|
287
|
+
})
|
|
152
288
|
});
|
|
153
|
-
|
|
154
|
-
|
|
289
|
+
log("version", {
|
|
290
|
+
artifactDiff,
|
|
291
|
+
versions
|
|
292
|
+
}, {
|
|
293
|
+
F: __dxlog_file2,
|
|
294
|
+
L: 95,
|
|
295
|
+
S: this,
|
|
296
|
+
C: (f, a) => f(...a)
|
|
297
|
+
});
|
|
298
|
+
for (const [id, { version }] of [
|
|
299
|
+
...artifactDiff.entries()
|
|
300
|
+
]) {
|
|
301
|
+
if (ObjectVersion.equals(version, versions.get(id))) {
|
|
302
|
+
artifactDiff.delete(id);
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
blocks.push({
|
|
306
|
+
_tag: "anchor",
|
|
307
|
+
objectId: id,
|
|
308
|
+
version
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
if (artifactDiff.size > 0) {
|
|
312
|
+
blocks.push(createArtifactUpdateBlock(artifactDiff));
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return Obj2.make(Message.Message, {
|
|
316
|
+
created: (/* @__PURE__ */ new Date()).toISOString(),
|
|
317
|
+
sender: {
|
|
318
|
+
role: "user"
|
|
319
|
+
},
|
|
320
|
+
blocks: [
|
|
321
|
+
...blocks,
|
|
322
|
+
{
|
|
323
|
+
_tag: "text",
|
|
324
|
+
text: prompt
|
|
325
|
+
}
|
|
326
|
+
]
|
|
327
|
+
});
|
|
328
|
+
});
|
|
329
|
+
var gatherObjectVersions = (messages) => {
|
|
330
|
+
const artifactIds = /* @__PURE__ */ new Map();
|
|
331
|
+
for (const message of messages) {
|
|
332
|
+
for (const block of message.blocks) {
|
|
333
|
+
if (block._tag === "anchor") {
|
|
334
|
+
artifactIds.set(block.objectId, block.version);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
return artifactIds;
|
|
339
|
+
};
|
|
340
|
+
var createArtifactUpdateBlock = (artifactDiff) => {
|
|
341
|
+
return {
|
|
342
|
+
_tag: "text",
|
|
343
|
+
// TODO(dmaretskyi): Does this need to be a special content-block?
|
|
344
|
+
disposition: "artifact-update",
|
|
345
|
+
text: trim`
|
|
346
|
+
The following artifacts have been updated since the last message:
|
|
347
|
+
${[
|
|
348
|
+
...artifactDiff.entries()
|
|
349
|
+
].map(([id, { diff }]) => `<changed-artifact id="${id}">${diff ? `
|
|
350
|
+
${diff}` : ""}</changed-artifact>`).join("\n")}
|
|
351
|
+
`
|
|
352
|
+
};
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
// src/session/session.ts
|
|
356
|
+
var __dxlog_file3 = "/__w/dxos/dxos/packages/core/assistant/src/session/session.ts";
|
|
357
|
+
var AiSession = class {
|
|
358
|
+
_options;
|
|
359
|
+
/** Prevents concurrent execution of session. */
|
|
360
|
+
_semaphore = Effect3.runSync(Effect3.makeSemaphore(1));
|
|
361
|
+
/** Message history from queue. */
|
|
362
|
+
// TODO(burdon): Evolve into supporting a git-like graph of messages.
|
|
363
|
+
_history = [];
|
|
364
|
+
/** Pending messages for this session (incl. the current prompt). */
|
|
365
|
+
_pending = [];
|
|
366
|
+
constructor(_options = {}) {
|
|
367
|
+
this._options = _options;
|
|
368
|
+
}
|
|
369
|
+
run = ({ prompt, system: systemTemplate, history = [], objects = [], blueprints = [], toolkit, observer = GenerationObserver.noop() }) => Effect3.gen(this, function* () {
|
|
370
|
+
this._history = [
|
|
371
|
+
...history
|
|
372
|
+
];
|
|
373
|
+
this._pending = [];
|
|
374
|
+
const pending = this._pending;
|
|
375
|
+
const submitMessage = Effect3.fnUntraced(function* (message) {
|
|
376
|
+
pending.push(message);
|
|
377
|
+
yield* observer.onMessage(message);
|
|
378
|
+
yield* TracingService.emitConverationMessage(message);
|
|
379
|
+
return message;
|
|
380
|
+
});
|
|
381
|
+
const promptMessage = yield* submitMessage(yield* formatUserPrompt({
|
|
382
|
+
prompt,
|
|
383
|
+
history
|
|
384
|
+
}));
|
|
385
|
+
const system = yield* formatSystemPrompt({
|
|
386
|
+
system: systemTemplate,
|
|
387
|
+
blueprints,
|
|
388
|
+
objects
|
|
389
|
+
});
|
|
390
|
+
do {
|
|
391
|
+
log2("request", {
|
|
392
|
+
prompt: promptMessage,
|
|
393
|
+
system: {
|
|
394
|
+
snippet: createSnippet(system),
|
|
395
|
+
length: system.length
|
|
396
|
+
},
|
|
397
|
+
pending: this._pending.length,
|
|
398
|
+
history: this._history.length,
|
|
399
|
+
objects: objects?.length ?? 0
|
|
400
|
+
}, {
|
|
401
|
+
F: __dxlog_file3,
|
|
402
|
+
L: 110,
|
|
403
|
+
S: this,
|
|
404
|
+
C: (f, a) => f(...a)
|
|
405
|
+
});
|
|
406
|
+
const prompt2 = yield* AiPreprocessor.preprocessPrompt([
|
|
407
|
+
...this._history,
|
|
408
|
+
...this._pending
|
|
409
|
+
], {
|
|
410
|
+
system
|
|
411
|
+
});
|
|
412
|
+
const blocks = yield* LanguageModel.streamText({
|
|
413
|
+
prompt: prompt2,
|
|
414
|
+
toolkit,
|
|
415
|
+
disableToolCallResolution: true
|
|
416
|
+
}).pipe(
|
|
417
|
+
// TOOD(dmaretskyi): Error mapping.
|
|
418
|
+
AiParser.parseResponse({
|
|
419
|
+
onBegin: () => observer.onBegin(),
|
|
420
|
+
onBlock: (block) => observer.onBlock(block),
|
|
421
|
+
onPart: (part) => observer.onPart(part),
|
|
422
|
+
onEnd: (summary) => observer.onEnd(summary)
|
|
423
|
+
}),
|
|
424
|
+
Stream.runCollect,
|
|
425
|
+
Effect3.map(Chunk.toArray)
|
|
426
|
+
);
|
|
427
|
+
const response = yield* submitMessage(Obj3.make(Message2.Message, {
|
|
428
|
+
created: (/* @__PURE__ */ new Date()).toISOString(),
|
|
429
|
+
sender: {
|
|
430
|
+
role: "assistant"
|
|
431
|
+
},
|
|
432
|
+
blocks
|
|
433
|
+
}));
|
|
434
|
+
const toolCalls = getToolCalls(response);
|
|
435
|
+
if (toolCalls.length === 0) {
|
|
436
|
+
break;
|
|
437
|
+
} else if (!toolkit) {
|
|
438
|
+
throw new Error("No toolkit provided");
|
|
439
|
+
}
|
|
440
|
+
const toolResults = yield* Effect3.forEach(toolCalls, (toolCall) => {
|
|
441
|
+
return callTool(toolkit, toolCall).pipe(Effect3.provide(TracingService.layerSubframe((context) => ({
|
|
442
|
+
...context,
|
|
443
|
+
parentMessage: response.id,
|
|
444
|
+
toolCallId: toolCall.toolCallId
|
|
445
|
+
}))));
|
|
446
|
+
});
|
|
447
|
+
yield* submitMessage(Obj3.make(Message2.Message, {
|
|
448
|
+
created: (/* @__PURE__ */ new Date()).toISOString(),
|
|
449
|
+
sender: {
|
|
450
|
+
role: "tool"
|
|
451
|
+
},
|
|
452
|
+
blocks: toolResults
|
|
453
|
+
}));
|
|
454
|
+
} while (true);
|
|
455
|
+
log2("done", {
|
|
456
|
+
pending: this._pending.length
|
|
457
|
+
}, {
|
|
458
|
+
F: __dxlog_file3,
|
|
459
|
+
L: 181,
|
|
460
|
+
S: this,
|
|
461
|
+
C: (f, a) => f(...a)
|
|
462
|
+
});
|
|
463
|
+
return this._pending;
|
|
464
|
+
}).pipe(this._semaphore.withPermits(1), Effect3.withSpan("AiSession.run"));
|
|
465
|
+
/**
|
|
466
|
+
* @deprecated
|
|
467
|
+
*/
|
|
468
|
+
// TODO(burdon): Implement or remove.
|
|
469
|
+
async runStructured(_schema, _options) {
|
|
470
|
+
return todo();
|
|
471
|
+
}
|
|
472
|
+
};
|
|
473
|
+
var createSnippet = (text, len = 32) => text.length <= len * 2 ? text : [
|
|
474
|
+
text.slice(0, len),
|
|
475
|
+
"...",
|
|
476
|
+
text.slice(-len)
|
|
477
|
+
].join("");
|
|
478
|
+
|
|
479
|
+
// src/session/toolkit.ts
|
|
480
|
+
import * as Toolkit from "@effect/ai/Toolkit";
|
|
481
|
+
import * as Effect4 from "effect/Effect";
|
|
482
|
+
import { ToolExecutionService, ToolResolverService } from "@dxos/ai";
|
|
483
|
+
import { isTruthy } from "@dxos/util";
|
|
484
|
+
var createToolkit = ({ toolkit: toolkitParam, toolIds = [], blueprints = [] }) => Effect4.gen(function* () {
|
|
485
|
+
const blueprintToolkit = yield* ToolResolverService.resolveToolkit([
|
|
486
|
+
...blueprints.flatMap(({ tools }) => tools),
|
|
487
|
+
...toolIds
|
|
488
|
+
]);
|
|
489
|
+
const blueprintToolkitHandler = yield* blueprintToolkit.toContext(ToolExecutionService.handlersFor(blueprintToolkit));
|
|
490
|
+
const toolkit = Toolkit.merge(...[
|
|
491
|
+
toolkitParam,
|
|
492
|
+
blueprintToolkit
|
|
493
|
+
].filter(isTruthy));
|
|
494
|
+
return yield* toolkit.pipe(Effect4.provide(blueprintToolkitHandler));
|
|
495
|
+
});
|
|
496
|
+
|
|
497
|
+
// src/session/version-pin.ts
|
|
498
|
+
import * as Schema2 from "effect/Schema";
|
|
499
|
+
import { getVersion } from "@dxos/echo-db";
|
|
500
|
+
import { ObjectId } from "@dxos/keys";
|
|
501
|
+
var ObjectVersionSchema = Schema2.Unknown;
|
|
502
|
+
var VersionPinSchema = Schema2.Struct({
|
|
503
|
+
// TODO(dmaretskyi): Use Ref when those support encoding.
|
|
504
|
+
objectId: ObjectId,
|
|
505
|
+
// TODO(dmaretskyi): Could be opaque.
|
|
506
|
+
version: ObjectVersionSchema
|
|
507
|
+
});
|
|
508
|
+
var VersionPin = class extends VersionPinSchema {
|
|
509
|
+
static DISPOSITION = "version-pin";
|
|
510
|
+
static fromObject(object) {
|
|
511
|
+
return VersionPin.make({
|
|
512
|
+
objectId: object.id,
|
|
513
|
+
version: getVersion(object)
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
static createBlock(pin) {
|
|
517
|
+
return {
|
|
518
|
+
_tag: "json",
|
|
519
|
+
disposition: VersionPin.DISPOSITION,
|
|
520
|
+
data: JSON.stringify(pin)
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
};
|
|
524
|
+
|
|
525
|
+
// src/session/GenericToolkit.ts
|
|
526
|
+
var GenericToolkit_exports = {};
|
|
527
|
+
__export(GenericToolkit_exports, {
|
|
528
|
+
TypeId: () => TypeId,
|
|
529
|
+
make: () => make,
|
|
530
|
+
merge: () => merge3
|
|
531
|
+
});
|
|
532
|
+
import * as Toolkit2 from "@effect/ai/Toolkit";
|
|
533
|
+
import * as Effect5 from "effect/Effect";
|
|
534
|
+
import * as Layer from "effect/Layer";
|
|
535
|
+
import * as Pipeable from "effect/Pipeable";
|
|
536
|
+
var TypeId = "~@dxos/assistant/GenericToolkit";
|
|
537
|
+
var make = (toolkit, layer) => {
|
|
538
|
+
return {
|
|
539
|
+
[TypeId]: TypeId,
|
|
540
|
+
toolkit,
|
|
541
|
+
layer,
|
|
542
|
+
handlers: toolkit.pipe(Effect5.provide(layer)),
|
|
543
|
+
pipe() {
|
|
544
|
+
return Pipeable.pipeArguments(this, arguments);
|
|
545
|
+
}
|
|
546
|
+
};
|
|
547
|
+
};
|
|
548
|
+
var merge3 = (...toolkits) => {
|
|
549
|
+
return make(Toolkit2.merge(...toolkits.map((t) => t.toolkit)), Layer.mergeAll(...toolkits.map((t) => t.layer)));
|
|
550
|
+
};
|
|
551
|
+
|
|
552
|
+
// src/conversation/conversation.ts
|
|
553
|
+
var __dxlog_file4 = "/__w/dxos/dxos/packages/core/assistant/src/conversation/conversation.ts";
|
|
554
|
+
var AiConversation = class extends Resource2 {
|
|
555
|
+
/**
|
|
556
|
+
* Message and binding queue.
|
|
557
|
+
*/
|
|
558
|
+
_queue;
|
|
559
|
+
/**
|
|
560
|
+
* Blueprints bound to the conversation.
|
|
561
|
+
*/
|
|
562
|
+
_context;
|
|
563
|
+
/**
|
|
564
|
+
* Toolkit from the current session request.
|
|
565
|
+
*/
|
|
566
|
+
_toolkit;
|
|
567
|
+
constructor(queue) {
|
|
568
|
+
super();
|
|
569
|
+
this._queue = queue;
|
|
570
|
+
this._context = new AiContextBinder(this._queue);
|
|
571
|
+
}
|
|
572
|
+
async _open() {
|
|
573
|
+
await this._context.open();
|
|
574
|
+
}
|
|
575
|
+
async _close() {
|
|
576
|
+
await this._context.close();
|
|
155
577
|
}
|
|
156
|
-
return obj;
|
|
157
|
-
}
|
|
158
|
-
var __dxlog_file = "/__w/dxos/dxos/packages/core/assistant/src/conversation/conversation.ts";
|
|
159
|
-
var AiConversation = class {
|
|
160
578
|
get context() {
|
|
161
579
|
return this._context;
|
|
162
580
|
}
|
|
@@ -165,31 +583,31 @@ var AiConversation = class {
|
|
|
165
583
|
}
|
|
166
584
|
async getHistory() {
|
|
167
585
|
const queueItems = await this._queue.queryObjects();
|
|
168
|
-
return queueItems.filter(
|
|
586
|
+
return queueItems.filter(Obj4.instanceOf(Message3.Message));
|
|
169
587
|
}
|
|
170
588
|
/**
|
|
171
589
|
* Creates a new cancelable request effect.
|
|
172
590
|
*/
|
|
173
591
|
createRequest(params) {
|
|
174
|
-
return
|
|
592
|
+
return Effect6.gen(this, function* () {
|
|
175
593
|
const session = new AiSession();
|
|
176
|
-
const history = yield*
|
|
177
|
-
const context = yield*
|
|
178
|
-
const blueprints = yield*
|
|
179
|
-
const objects = yield*
|
|
594
|
+
const history = yield* Effect6.promise(() => this.getHistory());
|
|
595
|
+
const context = yield* Effect6.promise(() => this.context.query());
|
|
596
|
+
const blueprints = yield* Effect6.forEach(context.blueprints.values(), DatabaseService2.loadOption).pipe(Effect6.map(Array5.filter(Option2.isSome)), Effect6.map(Array5.map((option) => option.value)));
|
|
597
|
+
const objects = yield* Effect6.forEach(context.objects.values(), DatabaseService2.loadOption).pipe(Effect6.map(Array5.filter(Option2.isSome)), Effect6.map(Array5.map((option) => option.value)));
|
|
180
598
|
const toolkit = yield* createToolkit({
|
|
181
599
|
blueprints
|
|
182
600
|
});
|
|
183
601
|
this._toolkit = toolkit;
|
|
184
602
|
const start = Date.now();
|
|
185
|
-
|
|
603
|
+
log3("run", {
|
|
186
604
|
history: history.length,
|
|
187
605
|
blueprints: blueprints.length,
|
|
188
606
|
objects: objects.length,
|
|
189
607
|
tools: this._toolkit?.tools.length ?? 0
|
|
190
608
|
}, {
|
|
191
|
-
F:
|
|
192
|
-
L:
|
|
609
|
+
F: __dxlog_file4,
|
|
610
|
+
L: 107,
|
|
193
611
|
S: this,
|
|
194
612
|
C: (f, a) => f(...a)
|
|
195
613
|
});
|
|
@@ -199,37 +617,30 @@ var AiConversation = class {
|
|
|
199
617
|
objects,
|
|
200
618
|
toolkit,
|
|
201
619
|
...params
|
|
202
|
-
}).pipe(
|
|
620
|
+
}).pipe(Effect6.provideService(AiContextService, {
|
|
203
621
|
binder: this.context
|
|
204
622
|
}));
|
|
205
|
-
|
|
623
|
+
log3("result", {
|
|
206
624
|
messages: messages.length,
|
|
207
625
|
duration: Date.now() - start
|
|
208
626
|
}, {
|
|
209
|
-
F:
|
|
210
|
-
L:
|
|
627
|
+
F: __dxlog_file4,
|
|
628
|
+
L: 121,
|
|
211
629
|
S: this,
|
|
212
630
|
C: (f, a) => f(...a)
|
|
213
631
|
});
|
|
214
|
-
yield*
|
|
632
|
+
yield* Effect6.promise(() => this._queue.append(messages));
|
|
215
633
|
return messages;
|
|
216
|
-
}).pipe(
|
|
217
|
-
}
|
|
218
|
-
constructor(options) {
|
|
219
|
-
_define_property2(this, "_queue", void 0);
|
|
220
|
-
_define_property2(this, "_context", void 0);
|
|
221
|
-
_define_property2(this, "_toolkit", void 0);
|
|
222
|
-
this._queue = options.queue;
|
|
223
|
-
this._context = new AiContextBinder(this._queue);
|
|
634
|
+
}).pipe(Effect6.withSpan("AiConversation.request"));
|
|
224
635
|
}
|
|
225
636
|
};
|
|
226
637
|
|
|
227
638
|
// src/diff.ts
|
|
228
639
|
import { next as A } from "@automerge/automerge";
|
|
229
640
|
import { DocAccessor, toCursorRange } from "@dxos/echo-db";
|
|
230
|
-
import { log as
|
|
641
|
+
import { log as log4 } from "@dxos/log";
|
|
231
642
|
import { isNonNullable } from "@dxos/util";
|
|
232
|
-
var
|
|
643
|
+
var __dxlog_file5 = "/__w/dxos/dxos/packages/core/assistant/src/diff.ts";
|
|
233
644
|
var reduceDiffs = (diffs) => {
|
|
234
645
|
return diffs.reduce((acc, diff) => {
|
|
235
646
|
const current = acc[acc.length - 1];
|
|
@@ -281,9 +692,9 @@ var applyDiffs = (accessor, diffs) => {
|
|
|
281
692
|
if (idx !== -1) {
|
|
282
693
|
A.splice(doc, accessor.path, idx, diff.match.length, diff.replace);
|
|
283
694
|
} else {
|
|
284
|
-
|
|
285
|
-
F:
|
|
286
|
-
L:
|
|
695
|
+
log4.warn("diff not found", diff, {
|
|
696
|
+
F: __dxlog_file5,
|
|
697
|
+
L: 78,
|
|
287
698
|
S: void 0,
|
|
288
699
|
C: (f, a) => f(...a)
|
|
289
700
|
});
|
|
@@ -295,34 +706,36 @@ var applyDiffs = (accessor, diffs) => {
|
|
|
295
706
|
|
|
296
707
|
// src/execution-graph.ts
|
|
297
708
|
import { AgentStatus } from "@dxos/ai";
|
|
298
|
-
import { Obj as
|
|
709
|
+
import { Obj as Obj5 } from "@dxos/echo";
|
|
299
710
|
import { MESSAGE_PROPERTY_TOOL_CALL_ID } from "@dxos/functions";
|
|
300
711
|
import { LogLevel } from "@dxos/log";
|
|
301
|
-
import { ContentBlock,
|
|
712
|
+
import { ContentBlock, Message as Message4 } from "@dxos/types";
|
|
302
713
|
import { isNonNullable as isNonNullable2 } from "@dxos/util";
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
value,
|
|
307
|
-
enumerable: true,
|
|
308
|
-
configurable: true,
|
|
309
|
-
writable: true
|
|
310
|
-
});
|
|
311
|
-
} else {
|
|
312
|
-
obj[key] = value;
|
|
313
|
-
}
|
|
314
|
-
return obj;
|
|
315
|
-
}
|
|
714
|
+
var SKIP_BLOCKS = [
|
|
715
|
+
"text"
|
|
716
|
+
];
|
|
316
717
|
var ExecutionGraph = class {
|
|
718
|
+
_skipBlocks;
|
|
719
|
+
// TODO(dmaretskyi): Evolve the internal state to support chats, functions, circuit workflows, etc.
|
|
720
|
+
_commits = [];
|
|
721
|
+
_branchNames = /* @__PURE__ */ new Set();
|
|
722
|
+
_lastBlockId;
|
|
723
|
+
// Tool call tracking.
|
|
724
|
+
_toolCallCommitIds = /* @__PURE__ */ new Map();
|
|
725
|
+
_lastCommitByBranch = /* @__PURE__ */ new Map();
|
|
726
|
+
_pendingToolResults = /* @__PURE__ */ new Map();
|
|
727
|
+
constructor(_skipBlocks = SKIP_BLOCKS) {
|
|
728
|
+
this._skipBlocks = _skipBlocks;
|
|
729
|
+
}
|
|
317
730
|
/**
|
|
318
731
|
* Adds events to the graph.
|
|
319
732
|
*/
|
|
320
733
|
addEvents(events) {
|
|
321
734
|
const sortedEvents = this.sortObjectsByCreated(events);
|
|
322
735
|
for (const event of sortedEvents) {
|
|
323
|
-
if (
|
|
736
|
+
if (Obj5.instanceOf(Message4.Message, event)) {
|
|
324
737
|
this._processMessage(event);
|
|
325
|
-
} else if (
|
|
738
|
+
} else if (Obj5.instanceOf(AgentStatus, event)) {
|
|
326
739
|
this._processAgentStatus(event);
|
|
327
740
|
}
|
|
328
741
|
}
|
|
@@ -331,7 +744,7 @@ var ExecutionGraph = class {
|
|
|
331
744
|
* Processes a message event and creates commits for its blocks.
|
|
332
745
|
*/
|
|
333
746
|
_processMessage(message) {
|
|
334
|
-
const messageCommits = messageToCommits(message, this._lastBlockId, this._toolCallCommitIds, this._lastCommitByBranch);
|
|
747
|
+
const messageCommits = messageToCommits(message, this._lastBlockId, this._toolCallCommitIds, this._lastCommitByBranch, this._skipBlocks);
|
|
335
748
|
this._commits.push(...messageCommits);
|
|
336
749
|
messageCommits.forEach((commit) => this._branchNames.add(commit.branch));
|
|
337
750
|
this._trackToolCalls(messageCommits);
|
|
@@ -495,20 +908,15 @@ var ExecutionGraph = class {
|
|
|
495
908
|
const toolResultMatch = commitId.match(/_toolResult_(.+)$/);
|
|
496
909
|
return toolCallMatch?.[1] || toolResultMatch?.[1];
|
|
497
910
|
}
|
|
498
|
-
constructor() {
|
|
499
|
-
_define_property3(this, "_commits", []);
|
|
500
|
-
_define_property3(this, "_branchNames", /* @__PURE__ */ new Set());
|
|
501
|
-
_define_property3(this, "_lastBlockId", void 0);
|
|
502
|
-
_define_property3(this, "_toolCallCommitIds", /* @__PURE__ */ new Map());
|
|
503
|
-
_define_property3(this, "_lastCommitByBranch", /* @__PURE__ */ new Map());
|
|
504
|
-
_define_property3(this, "_pendingToolResults", /* @__PURE__ */ new Map());
|
|
505
|
-
}
|
|
506
911
|
};
|
|
507
|
-
var messageToCommits = (message, lastBlockId, toolCallIds, lastCommitByBranch) => {
|
|
912
|
+
var messageToCommits = (message, lastBlockId, toolCallIds, lastCommitByBranch, skipBlocks) => {
|
|
508
913
|
let previousBlockId = lastBlockId;
|
|
509
914
|
return message.blocks.map((block, idx) => {
|
|
510
915
|
const branch = getMessageBranch(message);
|
|
511
916
|
const parents = getBlockParents(block, previousBlockId, message, toolCallIds, lastCommitByBranch);
|
|
917
|
+
if (skipBlocks?.includes(block._tag)) {
|
|
918
|
+
return null;
|
|
919
|
+
}
|
|
512
920
|
const commit = createBlockCommit(block, message, branch, parents, idx);
|
|
513
921
|
if (commit) {
|
|
514
922
|
previousBlockId = commit.id;
|
|
@@ -663,49 +1071,50 @@ var stringifyRef = (ref) => {
|
|
|
663
1071
|
return ref.dxn.asEchoDXN()?.echoId ?? ref.dxn.asQueueDXN()?.objectId ?? "";
|
|
664
1072
|
};
|
|
665
1073
|
var stringifyObject = (obj) => {
|
|
666
|
-
return
|
|
1074
|
+
return Obj5.getLabel(obj) ?? Obj5.getTypename(obj) ?? obj.id;
|
|
667
1075
|
};
|
|
668
1076
|
|
|
669
1077
|
// src/functions/services.ts
|
|
670
1078
|
import * as Tool from "@effect/ai/Tool";
|
|
671
|
-
import * as
|
|
672
|
-
import * as
|
|
673
|
-
import * as
|
|
1079
|
+
import * as Context3 from "effect/Context";
|
|
1080
|
+
import * as Effect7 from "effect/Effect";
|
|
1081
|
+
import * as Layer2 from "effect/Layer";
|
|
674
1082
|
import * as Record from "effect/Record";
|
|
675
|
-
import * as
|
|
676
|
-
import { AiToolNotFoundError, ToolExecutionService, ToolResolverService } from "@dxos/ai";
|
|
677
|
-
import { todo } from "@dxos/debug";
|
|
678
|
-
import { Query } from "@dxos/echo";
|
|
679
|
-
import { DatabaseService as
|
|
680
|
-
import {
|
|
681
|
-
|
|
1083
|
+
import * as Schema3 from "effect/Schema";
|
|
1084
|
+
import { AiToolNotFoundError, ToolExecutionService as ToolExecutionService2, ToolResolverService as ToolResolverService2 } from "@dxos/ai";
|
|
1085
|
+
import { todo as todo2 } from "@dxos/debug";
|
|
1086
|
+
import { Query as Query2 } from "@dxos/echo";
|
|
1087
|
+
import { DatabaseService as DatabaseService3 } from "@dxos/echo-db";
|
|
1088
|
+
import { Function as Function4, FunctionDefinition, FunctionInvocationService } from "@dxos/functions";
|
|
1089
|
+
import { invariant as invariant2 } from "@dxos/invariant";
|
|
1090
|
+
var __dxlog_file6 = "/__w/dxos/dxos/packages/core/assistant/src/functions/services.ts";
|
|
682
1091
|
var makeToolResolverFromFunctions = (functions, toolkit) => {
|
|
683
|
-
return
|
|
684
|
-
const dbService = yield*
|
|
1092
|
+
return Layer2.effect(ToolResolverService2, Effect7.gen(function* () {
|
|
1093
|
+
const dbService = yield* DatabaseService3;
|
|
685
1094
|
return {
|
|
686
|
-
resolve: (id) =>
|
|
1095
|
+
resolve: (id) => Effect7.gen(function* () {
|
|
687
1096
|
const tool = toolkit.tools[id];
|
|
688
1097
|
if (tool) {
|
|
689
1098
|
return tool;
|
|
690
1099
|
}
|
|
691
|
-
const { objects: [dbFunction] } = yield*
|
|
1100
|
+
const { objects: [dbFunction] } = yield* DatabaseService3.runQuery(Query2.type(Function4.Function, {
|
|
692
1101
|
key: id
|
|
693
1102
|
}));
|
|
694
1103
|
const functionDef = dbFunction ? FunctionDefinition.deserialize(dbFunction) : functions.find((fn2) => fn2.key === id);
|
|
695
1104
|
if (!functionDef) {
|
|
696
|
-
return yield*
|
|
1105
|
+
return yield* Effect7.fail(new AiToolNotFoundError(id));
|
|
697
1106
|
}
|
|
698
1107
|
return projectFunctionToTool(functionDef);
|
|
699
|
-
}).pipe(
|
|
1108
|
+
}).pipe(Effect7.provideService(DatabaseService3, dbService))
|
|
700
1109
|
};
|
|
701
1110
|
}));
|
|
702
1111
|
};
|
|
703
1112
|
var makeToolExecutionServiceFromFunctions = (toolkit, handlersLayer) => {
|
|
704
|
-
return
|
|
705
|
-
const toolkitHandler = yield* toolkit.pipe(
|
|
706
|
-
|
|
707
|
-
F:
|
|
708
|
-
L:
|
|
1113
|
+
return Layer2.effect(ToolExecutionService2, Effect7.gen(function* () {
|
|
1114
|
+
const toolkitHandler = yield* toolkit.pipe(Effect7.provide(handlersLayer));
|
|
1115
|
+
invariant2(isHandlerLike(toolkitHandler), void 0, {
|
|
1116
|
+
F: __dxlog_file6,
|
|
1117
|
+
L: 74,
|
|
709
1118
|
S: this,
|
|
710
1119
|
A: [
|
|
711
1120
|
"isHandlerLike(toolkitHandler)",
|
|
@@ -716,15 +1125,16 @@ var makeToolExecutionServiceFromFunctions = (toolkit, handlersLayer) => {
|
|
|
716
1125
|
return {
|
|
717
1126
|
handlersFor: (toolkit2) => {
|
|
718
1127
|
const makeHandler = (tool) => {
|
|
719
|
-
return
|
|
1128
|
+
return Effect7.fn(`toolFunctionHandler ${tool.name}`)(function* (input) {
|
|
720
1129
|
if (toolkitHandler.tools[tool.name]) {
|
|
721
1130
|
if (Tool.isProviderDefined(tool)) {
|
|
722
1131
|
throw new Error("Attempted to call a provider-defined tool");
|
|
723
1132
|
}
|
|
724
|
-
|
|
1133
|
+
const { result } = yield* toolkitHandler.handle(tool.name, input);
|
|
1134
|
+
return result;
|
|
725
1135
|
}
|
|
726
|
-
const { definition: functionDef } =
|
|
727
|
-
return yield* functionInvocationService.invokeFunction(functionDef, input).pipe(
|
|
1136
|
+
const { definition: functionDef } = Context3.get(FunctionToolAnnotation)(tool.annotations);
|
|
1137
|
+
return yield* functionInvocationService.invokeFunction(functionDef, input).pipe(Effect7.catchAllDefect((defect) => Effect7.fail(defect)));
|
|
728
1138
|
});
|
|
729
1139
|
};
|
|
730
1140
|
return toolkit2.of(Record.map(toolkit2.tools, (tool, _name) => Tool.isUserDefined(tool) ? makeHandler(tool) : null));
|
|
@@ -732,7 +1142,7 @@ var makeToolExecutionServiceFromFunctions = (toolkit, handlersLayer) => {
|
|
|
732
1142
|
};
|
|
733
1143
|
}));
|
|
734
1144
|
};
|
|
735
|
-
var FunctionToolAnnotation = class extends
|
|
1145
|
+
var FunctionToolAnnotation = class extends Context3.Tag("@dxos/assistant/FunctionToolAnnotation")() {
|
|
736
1146
|
};
|
|
737
1147
|
var toolCache = /* @__PURE__ */ new WeakMap();
|
|
738
1148
|
var projectFunctionToTool = (fn2) => {
|
|
@@ -743,7 +1153,7 @@ var projectFunctionToTool = (fn2) => {
|
|
|
743
1153
|
description: fn2.description,
|
|
744
1154
|
parameters: createStructFieldsFromSchema(fn2.inputSchema),
|
|
745
1155
|
// TODO(dmaretskyi): Include output schema.
|
|
746
|
-
failure:
|
|
1156
|
+
failure: Schema3.Any
|
|
747
1157
|
}).annotate(FunctionToolAnnotation, {
|
|
748
1158
|
definition: fn2
|
|
749
1159
|
});
|
|
@@ -751,13 +1161,13 @@ var projectFunctionToTool = (fn2) => {
|
|
|
751
1161
|
return tool;
|
|
752
1162
|
};
|
|
753
1163
|
var makeToolName = (name) => {
|
|
754
|
-
const toolName = name.replace(/[^a-zA-Z0-9]/g, "
|
|
755
|
-
|
|
756
|
-
F:
|
|
757
|
-
L:
|
|
1164
|
+
const toolName = name.toLowerCase().replace(/[^a-zA-Z0-9]/g, "-");
|
|
1165
|
+
invariant2(toolName.match(/^[a-z_][a-z0-9-_]*$/), void 0, {
|
|
1166
|
+
F: __dxlog_file6,
|
|
1167
|
+
L: 144,
|
|
758
1168
|
S: void 0,
|
|
759
1169
|
A: [
|
|
760
|
-
"toolName.match(/^[a-
|
|
1170
|
+
"toolName.match(/^[a-z_][a-z0-9-_]*$/)",
|
|
761
1171
|
""
|
|
762
1172
|
]
|
|
763
1173
|
});
|
|
@@ -768,12 +1178,12 @@ var createStructFieldsFromSchema = (schema) => {
|
|
|
768
1178
|
case "TypeLiteral":
|
|
769
1179
|
return Object.fromEntries(schema.ast.propertySignatures.map((prop) => [
|
|
770
1180
|
prop.name,
|
|
771
|
-
|
|
1181
|
+
Schema3.make(prop.type)
|
|
772
1182
|
]));
|
|
773
1183
|
case "VoidKeyword":
|
|
774
1184
|
return {};
|
|
775
1185
|
default:
|
|
776
|
-
return
|
|
1186
|
+
return todo2(`Unsupported schema AST: ${schema.ast._tag}`);
|
|
777
1187
|
}
|
|
778
1188
|
};
|
|
779
1189
|
var isHandlerLike = (value) => {
|
|
@@ -782,21 +1192,21 @@ var isHandlerLike = (value) => {
|
|
|
782
1192
|
|
|
783
1193
|
// src/templates/system.ts
|
|
784
1194
|
import defaultsDeep from "lodash.defaultsdeep";
|
|
785
|
-
import { Template } from "@dxos/blueprints";
|
|
1195
|
+
import { Template as Template2 } from "@dxos/blueprints";
|
|
786
1196
|
|
|
787
|
-
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/format.tpl?raw
|
|
788
|
-
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## 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";
|
|
1197
|
+
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/instructions/format.tpl?raw
|
|
1198
|
+
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";
|
|
789
1199
|
|
|
790
|
-
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/system.tpl?raw
|
|
1200
|
+
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/instructions/system.tpl?raw
|
|
791
1201
|
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";
|
|
792
1202
|
|
|
793
1203
|
// src/templates/system.ts
|
|
794
|
-
var createSystemPrompt = (options = {}) =>
|
|
1204
|
+
var createSystemPrompt = (options = {}) => Template2.process(format_default, defaultsDeep(options, {
|
|
795
1205
|
DATETIME: (/* @__PURE__ */ new Date()).toISOString()
|
|
796
1206
|
}));
|
|
797
1207
|
var templates = {
|
|
798
1208
|
/** Editable system prompt.*/
|
|
799
|
-
system:
|
|
1209
|
+
system: Template2.make({
|
|
800
1210
|
/**
|
|
801
1211
|
* NOTE: We are forbidden from accessing randomness at model level in cloudflare, so we have to have pre-generated ids.
|
|
802
1212
|
*/
|
|
@@ -805,13 +1215,15 @@ var templates = {
|
|
|
805
1215
|
})
|
|
806
1216
|
};
|
|
807
1217
|
|
|
808
|
-
// src/types.ts
|
|
809
|
-
import * as
|
|
1218
|
+
// src/types/artifact.ts
|
|
1219
|
+
import * as Schema4 from "effect/Schema";
|
|
1220
|
+
import { DatabaseService as DatabaseService4 } from "@dxos/echo-db";
|
|
810
1221
|
import { DXN as DXN2, LOCAL_SPACE_TAG } from "@dxos/keys";
|
|
811
|
-
import { trim } from "@dxos/util";
|
|
1222
|
+
import { trim as trim2 } from "@dxos/util";
|
|
812
1223
|
var createArtifactElement = (id) => `<artifact id=${id} />`;
|
|
813
|
-
var ArtifactId = class extends
|
|
814
|
-
|
|
1224
|
+
var ArtifactId = class extends Schema4.String.annotations({
|
|
1225
|
+
// TODO(dmaretskyi): This section gets overriden.
|
|
1226
|
+
description: trim2`
|
|
815
1227
|
The ID of the referenced object. Formats accepted:
|
|
816
1228
|
- DXN (dxn:echo:@:XXXXX). DXNs can be prepended with an @ symbol for compatibility with in-text references.
|
|
817
1229
|
- space ID, object ID tuple (spaceID:objectID)
|
|
@@ -841,6 +1253,13 @@ var ArtifactId = class extends Schema3.String.annotations({
|
|
|
841
1253
|
throw new Error(`Unable to parse object reference: ${reference}`);
|
|
842
1254
|
}
|
|
843
1255
|
}
|
|
1256
|
+
/**
|
|
1257
|
+
* Resolves an artifact ID to an object.
|
|
1258
|
+
*/
|
|
1259
|
+
static resolve(schema, ref) {
|
|
1260
|
+
const dxn = ArtifactId.toDXN(ref);
|
|
1261
|
+
return DatabaseService4.resolve(dxn, schema);
|
|
1262
|
+
}
|
|
844
1263
|
};
|
|
845
1264
|
export {
|
|
846
1265
|
AiAssistantError,
|
|
@@ -855,6 +1274,7 @@ export {
|
|
|
855
1274
|
ContextBinding,
|
|
856
1275
|
ExecutionGraph,
|
|
857
1276
|
GenerationObserver,
|
|
1277
|
+
GenericToolkit_exports as GenericToolkit,
|
|
858
1278
|
VersionPin,
|
|
859
1279
|
applyDiffs,
|
|
860
1280
|
computeDiffsWithCursors,
|