@dxos/assistant 0.8.4-main.dedc0f3 → 0.8.4-main.e8ec1fe
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 +119 -162
- package/dist/lib/browser/extraction/index.mjs.map +4 -4
- package/dist/lib/browser/index.mjs +978 -157
- 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 +119 -162
- package/dist/lib/node-esm/extraction/index.mjs.map +4 -4
- package/dist/lib/node-esm/index.mjs +978 -157
- 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 +14 -7
- package/dist/types/src/conversation/context.d.ts.map +1 -1
- package/dist/types/src/conversation/conversation.d.ts +11 -12
- 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 +38 -42
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/execution-graph.d.ts +85 -0
- package/dist/types/src/execution-graph.d.ts.map +1 -0
- package/dist/types/src/execution-graph.test.d.ts +2 -0
- package/dist/types/src/execution-graph.test.d.ts.map +1 -0
- 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 +60 -52
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction.d.ts +24 -13
- package/dist/types/src/extraction/extraction.d.ts.map +1 -1
- package/dist/types/src/extraction/quotes.d.ts +1 -1
- package/dist/types/src/extraction/quotes.d.ts.map +1 -1
- package/dist/types/src/functions/services.d.ts +18 -5
- package/dist/types/src/functions/services.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +2 -1
- package/dist/types/src/index.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 +2 -2
- package/dist/types/src/session/artifact-diff.d.ts.map +1 -1
- package/dist/types/src/session/error-handling.d.ts +2 -2
- package/dist/types/src/session/error-handling.d.ts.map +1 -1
- package/dist/types/src/session/format.d.ts +4 -174
- 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 +3 -3
- package/dist/types/src/session/observer.d.ts.map +1 -1
- package/dist/types/src/session/session.d.ts +15 -11
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/session/toolkit.d.ts +7 -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/types/artifact.d.ts +20 -0
- 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 +28 -31
- package/src/conversation/context.ts +49 -17
- package/src/conversation/conversation.ts +28 -23
- package/src/diff.test.ts +3 -3
- package/src/diff.ts +5 -0
- package/src/errors.ts +4 -4
- package/src/execution-graph.test.ts +740 -0
- package/src/execution-graph.ts +509 -0
- package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +2 -2
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +46 -54
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +10 -9
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +38 -44
- package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +12 -6
- package/src/extraction/extraction.ts +7 -6
- package/src/extraction/quotes.test.ts +2 -2
- package/src/extraction/quotes.ts +3 -3
- package/src/functions/services.ts +93 -50
- package/src/index.ts +2 -1
- package/src/session/GenericToolkit.ts +130 -0
- package/src/session/artifact-diff.ts +2 -2
- package/src/session/error-handling.ts +11 -6
- package/src/session/format.test.ts +5 -4
- package/src/session/format.ts +18 -13
- package/src/session/index.ts +2 -0
- package/src/session/observer.ts +4 -3
- package/src/session/session.conversations.json +1 -0
- package/src/session/session.test.ts +51 -210
- package/src/session/session.ts +40 -28
- package/src/session/toolkit.ts +19 -13
- package/src/session/version-pin.ts +4 -5
- package/src/templates/{format.tpl → instructions/format.tpl} +1 -0
- package/src/templates/{system.tpl → instructions/system.tpl} +10 -12
- package/src/templates/system.ts +2 -2
- package/src/{types.ts → types/artifact.ts} +22 -3
- package/src/types/index.ts +5 -0
- package/dist/lib/browser/chunk-5GJYG2ML.mjs +0 -396
- package/dist/lib/browser/chunk-5GJYG2ML.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-FLFCEB4P.mjs +0 -398
- package/dist/lib/node-esm/chunk-FLFCEB4P.mjs.map +0 -7
- package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs +0 -14
- package/dist/types/src/session/version-pin.test.d.ts +0 -2
- package/dist/types/src/session/version-pin.test.d.ts.map +0 -1
- 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 +0 -16
- package/dist/types/src/types.d.ts.map +0 -1
- package/src/session/__snapshots__/session.test.ts.snap +0 -32
- package/src/session/version-pin.test.ts +0 -7
- package/src/summarization/summarization.test.ts +0 -197
- /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
|
@@ -1,33 +1,19 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
AnthropicError,
|
|
5
|
-
ArtifactDiffResolver,
|
|
6
|
-
GenerationObserver,
|
|
7
|
-
VersionPin,
|
|
8
|
-
createToolkit
|
|
9
|
-
} from "./chunk-5GJYG2ML.mjs";
|
|
2
|
+
__export
|
|
3
|
+
} from "./chunk-J5LGTIGS.mjs";
|
|
10
4
|
|
|
11
5
|
// src/conversation/context.ts
|
|
12
6
|
import { computed } from "@preact/signals-core";
|
|
13
|
-
import
|
|
14
|
-
import
|
|
7
|
+
import * as Array2 from "effect/Array";
|
|
8
|
+
import * as Context from "effect/Context";
|
|
9
|
+
import * as Function from "effect/Function";
|
|
10
|
+
import * as Schema from "effect/Schema";
|
|
15
11
|
import { Blueprint } from "@dxos/blueprints";
|
|
16
|
-
import {
|
|
12
|
+
import { Resource } from "@dxos/context";
|
|
13
|
+
import { DXN, Filter, Obj, Query, Type } from "@dxos/echo";
|
|
14
|
+
import { invariant } from "@dxos/invariant";
|
|
17
15
|
import { ComplexSet } from "@dxos/util";
|
|
18
|
-
|
|
19
|
-
if (key in obj) {
|
|
20
|
-
Object.defineProperty(obj, key, {
|
|
21
|
-
value,
|
|
22
|
-
enumerable: true,
|
|
23
|
-
configurable: true,
|
|
24
|
-
writable: true
|
|
25
|
-
});
|
|
26
|
-
} else {
|
|
27
|
-
obj[key] = value;
|
|
28
|
-
}
|
|
29
|
-
return obj;
|
|
30
|
-
}
|
|
16
|
+
var __dxlog_file = "/__w/dxos/dxos/packages/core/assistant/src/conversation/context.ts";
|
|
31
17
|
var ContextBinding = Schema.Struct({
|
|
32
18
|
blueprints: Schema.Struct({
|
|
33
19
|
added: Schema.Array(Type.Ref(Blueprint.Blueprint)),
|
|
@@ -43,32 +29,96 @@ var ContextBinding = Schema.Struct({
|
|
|
43
29
|
version: "0.1.0"
|
|
44
30
|
}));
|
|
45
31
|
var Bindings = class {
|
|
32
|
+
blueprints = new ComplexSet((ref) => ref.dxn.toString());
|
|
33
|
+
// TODO(burdon): Some DXNs have the Space prefix so only compare the object ID.
|
|
34
|
+
objects = new ComplexSet((ref) => ref.dxn.asEchoDXN()?.echoId);
|
|
46
35
|
toJSON() {
|
|
47
36
|
return {
|
|
48
|
-
blueprints:
|
|
49
|
-
objects:
|
|
37
|
+
blueprints: Array2.fromIterable(this.blueprints).map((ref) => ref.dxn.toString()),
|
|
38
|
+
objects: Array2.fromIterable(this.objects).map((ref) => ref.dxn.toString())
|
|
50
39
|
};
|
|
51
40
|
}
|
|
52
|
-
constructor() {
|
|
53
|
-
_define_property(this, "blueprints", new ComplexSet((ref) => ref.dxn.toString()));
|
|
54
|
-
_define_property(this, "objects", new ComplexSet((ref) => ref.dxn.asEchoDXN()?.echoId));
|
|
55
|
-
}
|
|
56
41
|
};
|
|
57
|
-
var AiContextBinder = class {
|
|
42
|
+
var AiContextBinder = class extends Resource {
|
|
43
|
+
_queue;
|
|
44
|
+
/**
|
|
45
|
+
* Reactive query of all bindings.
|
|
46
|
+
*/
|
|
47
|
+
// TODO(burdon): Cache value?
|
|
48
|
+
_bindings;
|
|
49
|
+
_blueprints;
|
|
50
|
+
_objects;
|
|
51
|
+
constructor(_queue) {
|
|
52
|
+
super(), this._queue = _queue;
|
|
53
|
+
}
|
|
54
|
+
get bindings() {
|
|
55
|
+
invariant(this._bindings, "AiContextBinder not open", {
|
|
56
|
+
F: __dxlog_file,
|
|
57
|
+
L: 78,
|
|
58
|
+
S: this,
|
|
59
|
+
A: [
|
|
60
|
+
"this._bindings",
|
|
61
|
+
"'AiContextBinder not open'"
|
|
62
|
+
]
|
|
63
|
+
});
|
|
64
|
+
return this._bindings;
|
|
65
|
+
}
|
|
66
|
+
get blueprints() {
|
|
67
|
+
invariant(this._blueprints, "AiContextBinder not open", {
|
|
68
|
+
F: __dxlog_file,
|
|
69
|
+
L: 83,
|
|
70
|
+
S: this,
|
|
71
|
+
A: [
|
|
72
|
+
"this._blueprints",
|
|
73
|
+
"'AiContextBinder not open'"
|
|
74
|
+
]
|
|
75
|
+
});
|
|
76
|
+
return this._blueprints;
|
|
77
|
+
}
|
|
78
|
+
get objects() {
|
|
79
|
+
invariant(this._objects, "AiContextBinder not open", {
|
|
80
|
+
F: __dxlog_file,
|
|
81
|
+
L: 88,
|
|
82
|
+
S: this,
|
|
83
|
+
A: [
|
|
84
|
+
"this._objects",
|
|
85
|
+
"'AiContextBinder not open'"
|
|
86
|
+
]
|
|
87
|
+
});
|
|
88
|
+
return this._objects;
|
|
89
|
+
}
|
|
90
|
+
// TODO(wittjosiah): Use parent context?
|
|
91
|
+
async _open() {
|
|
92
|
+
const query = this._queue.query(Query.select(Filter.everything()));
|
|
93
|
+
this._ctx.onDispose(query.subscribe(() => {
|
|
94
|
+
}));
|
|
95
|
+
this._bindings = computed(() => this._reduce(query.objects));
|
|
96
|
+
this._blueprints = computed(() => [
|
|
97
|
+
...this.bindings.value.blueprints
|
|
98
|
+
]);
|
|
99
|
+
this._objects = computed(() => [
|
|
100
|
+
...this.bindings.value.objects
|
|
101
|
+
]);
|
|
102
|
+
}
|
|
103
|
+
async _close() {
|
|
104
|
+
this._bindings = void 0;
|
|
105
|
+
this._blueprints = void 0;
|
|
106
|
+
this._objects = void 0;
|
|
107
|
+
}
|
|
58
108
|
/**
|
|
59
109
|
* Asynchronous query of all bindings.
|
|
60
110
|
*/
|
|
61
111
|
async query() {
|
|
62
|
-
const
|
|
63
|
-
return this._reduce(
|
|
112
|
+
const { objects } = await this._queue.query(Query.select(Filter.everything())).run();
|
|
113
|
+
return this._reduce(objects);
|
|
64
114
|
}
|
|
65
115
|
// TODO(burdon): Pass in Blueprint obj (from registry?) and create reference.
|
|
66
116
|
async bind(props) {
|
|
67
|
-
if (!props.blueprints?.length && !props.objects?.length) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
117
|
const blueprints = props.blueprints?.filter((ref) => !this.blueprints.peek().find((b) => b.dxn.toString() === ref.dxn.toString())) ?? [];
|
|
71
118
|
const objects = props.objects?.filter((ref) => !this.objects.peek().find((o) => o.dxn.toString() === ref.dxn.toString())) ?? [];
|
|
119
|
+
if (!blueprints.length && !objects.length) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
72
122
|
await this._queue.append([
|
|
73
123
|
Obj.make(ContextBinding, {
|
|
74
124
|
blueprints: {
|
|
@@ -100,7 +150,7 @@ var AiContextBinder = class {
|
|
|
100
150
|
]);
|
|
101
151
|
}
|
|
102
152
|
_reduce(items) {
|
|
103
|
-
return pipe(items,
|
|
153
|
+
return Function.pipe(items, Array2.filter(Obj.instanceOf(ContextBinding)), Array2.reduce(new Bindings(), (context, item) => {
|
|
104
154
|
item.blueprints.removed.forEach((ref) => context.blueprints.delete(ref));
|
|
105
155
|
item.blueprints.added.forEach((ref) => context.blueprints.add(ref));
|
|
106
156
|
item.objects.removed.forEach((ref) => {
|
|
@@ -114,45 +164,418 @@ var AiContextBinder = class {
|
|
|
114
164
|
return context;
|
|
115
165
|
}));
|
|
116
166
|
}
|
|
117
|
-
constructor(_queue) {
|
|
118
|
-
_define_property(this, "_queue", void 0);
|
|
119
|
-
_define_property(this, "bindings", void 0);
|
|
120
|
-
_define_property(this, "blueprints", void 0);
|
|
121
|
-
_define_property(this, "objects", void 0);
|
|
122
|
-
this._queue = _queue;
|
|
123
|
-
this.bindings = computed(() => this._reduce(this._queue.objects));
|
|
124
|
-
this.blueprints = computed(() => [
|
|
125
|
-
...this.bindings.value.blueprints
|
|
126
|
-
]);
|
|
127
|
-
this.objects = computed(() => [
|
|
128
|
-
...this.bindings.value.objects
|
|
129
|
-
]);
|
|
130
|
-
}
|
|
131
167
|
};
|
|
132
168
|
var AiContextService = class extends Context.Tag("@dxos/assistant/AiContextService")() {
|
|
133
169
|
};
|
|
134
170
|
|
|
135
171
|
// src/conversation/conversation.ts
|
|
136
|
-
import
|
|
172
|
+
import * as Array5 from "effect/Array";
|
|
173
|
+
import * as Effect6 from "effect/Effect";
|
|
174
|
+
import * as Option2 from "effect/Option";
|
|
175
|
+
import { Resource as Resource2 } from "@dxos/context";
|
|
176
|
+
import { Obj as Obj4 } from "@dxos/echo";
|
|
177
|
+
import { DatabaseService as DatabaseService2 } from "@dxos/echo-db";
|
|
178
|
+
import { log as log3 } from "@dxos/log";
|
|
179
|
+
import { Message as Message3 } from "@dxos/types";
|
|
180
|
+
|
|
181
|
+
// src/session/artifact-diff.ts
|
|
182
|
+
import * as Context2 from "effect/Context";
|
|
183
|
+
var ArtifactDiffResolver = class extends Context2.Tag("@dxos/assistant/ArtifactDiffResolver")() {
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
// src/session/observer.ts
|
|
187
|
+
import * as Effect from "effect/Effect";
|
|
188
|
+
import * as Function2 from "effect/Function";
|
|
189
|
+
var GenerationObserver = Object.freeze({
|
|
190
|
+
make: ({ onBegin = Function2.constant(Effect.void), onPart = Function2.constant(Effect.void), onBlock = Function2.constant(Effect.void), onEnd = Function2.constant(Effect.void), onMessage = Function2.constant(Effect.void) } = {}) => ({
|
|
191
|
+
onBegin,
|
|
192
|
+
onPart,
|
|
193
|
+
onBlock,
|
|
194
|
+
onEnd,
|
|
195
|
+
onMessage
|
|
196
|
+
}),
|
|
197
|
+
noop: () => GenerationObserver.make(),
|
|
198
|
+
/**
|
|
199
|
+
* Debug printer to be used in unit-tests and browser devtools.
|
|
200
|
+
*/
|
|
201
|
+
fromPrinter: (printer) => GenerationObserver.make({
|
|
202
|
+
onBlock: (block) => Effect.sync(() => {
|
|
203
|
+
if (block.pending) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
printer.printContentBlock(block);
|
|
207
|
+
}),
|
|
208
|
+
onMessage: (message) => Effect.sync(() => {
|
|
209
|
+
if (message.sender.role === "assistant") {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
printer.printMessage(message);
|
|
213
|
+
})
|
|
214
|
+
})
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
// src/session/session.ts
|
|
218
|
+
import * as LanguageModel from "@effect/ai/LanguageModel";
|
|
219
|
+
import * as Chunk from "effect/Chunk";
|
|
220
|
+
import * as Effect3 from "effect/Effect";
|
|
221
|
+
import * as Stream from "effect/Stream";
|
|
222
|
+
import { AiParser, AiPreprocessor, callTool, getToolCalls } from "@dxos/ai";
|
|
223
|
+
import { todo } from "@dxos/debug";
|
|
224
|
+
import { Obj as Obj3 } from "@dxos/echo";
|
|
225
|
+
import { TracingService } from "@dxos/functions";
|
|
226
|
+
import { log as log2 } from "@dxos/log";
|
|
227
|
+
import { Message as Message2 } from "@dxos/types";
|
|
228
|
+
|
|
229
|
+
// src/session/format.ts
|
|
230
|
+
import * as Array4 from "effect/Array";
|
|
231
|
+
import * as Effect2 from "effect/Effect";
|
|
232
|
+
import * as Function3 from "effect/Function";
|
|
233
|
+
import * as Option from "effect/Option";
|
|
234
|
+
import { Template } from "@dxos/blueprints";
|
|
137
235
|
import { Obj as Obj2 } from "@dxos/echo";
|
|
138
|
-
import { DatabaseService } from "@dxos/
|
|
236
|
+
import { DatabaseService, ObjectVersion } from "@dxos/echo-db";
|
|
139
237
|
import { log } from "@dxos/log";
|
|
140
|
-
import {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
238
|
+
import { Message } from "@dxos/types";
|
|
239
|
+
import { trim } from "@dxos/util";
|
|
240
|
+
|
|
241
|
+
// src/errors.ts
|
|
242
|
+
import { BaseError } from "@dxos/errors";
|
|
243
|
+
var AiModelError = class extends BaseError.extend("AI_MODEL_ERROR", "AI model error") {
|
|
244
|
+
};
|
|
245
|
+
var AiAssistantError = class extends BaseError.extend("AI_ASSISTANT_ERROR", "AI assistant error") {
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
// src/session/format.ts
|
|
249
|
+
var __dxlog_file2 = "/__w/dxos/dxos/packages/core/assistant/src/session/format.ts";
|
|
250
|
+
var formatSystemPrompt = ({ system, blueprints = [], objects = [] }) => Effect2.gen(function* () {
|
|
251
|
+
const blueprintDefs = yield* Function3.pipe(blueprints, Effect2.forEach((blueprint) => Effect2.succeed(blueprint.instructions)), Effect2.flatMap(Effect2.forEach((template) => DatabaseService.loadOption(template.source))), Effect2.map(Array4.filter(Option.isSome)), Effect2.map(Array4.map((template) => trim`
|
|
252
|
+
<blueprint>
|
|
253
|
+
${Template.process(template.value.content)}
|
|
254
|
+
</blueprint>
|
|
255
|
+
`)), Effect2.map((blueprints2) => blueprints2.length > 0 ? [
|
|
256
|
+
"## Blueprints Definitions",
|
|
257
|
+
...blueprints2
|
|
258
|
+
].join("\n\n") : void 0));
|
|
259
|
+
const objectDefs = yield* Function3.pipe(objects, Effect2.forEach((object) => Effect2.succeed(trim`
|
|
260
|
+
<object>
|
|
261
|
+
<dxn>${Obj2.getDXN(object)}</dxn>
|
|
262
|
+
<typename>${Obj2.getTypename(object)}</typename>
|
|
263
|
+
</object>
|
|
264
|
+
`)), Effect2.map((objects2) => objects2.length > 0 ? [
|
|
265
|
+
"## Context Objects",
|
|
266
|
+
...objects2
|
|
267
|
+
].join("\n\n") : void 0));
|
|
268
|
+
return yield* Function3.pipe(Effect2.succeed([
|
|
269
|
+
system,
|
|
270
|
+
blueprintDefs,
|
|
271
|
+
objectDefs
|
|
272
|
+
].filter((def) => def !== void 0)), Effect2.map((parts) => parts.join("\n\n")));
|
|
273
|
+
});
|
|
274
|
+
var formatUserPrompt = ({ prompt, history = [] }) => Effect2.gen(function* () {
|
|
275
|
+
const blocks = [];
|
|
276
|
+
const artifactDiffResolver = yield* Effect2.serviceOption(ArtifactDiffResolver);
|
|
277
|
+
if (Option.isSome(artifactDiffResolver)) {
|
|
278
|
+
const versions = gatherObjectVersions(history);
|
|
279
|
+
const artifactDiff = yield* Effect2.tryPromise({
|
|
280
|
+
try: () => artifactDiffResolver.value.resolve([
|
|
281
|
+
...versions.entries()
|
|
282
|
+
].map(([id, version]) => ({
|
|
283
|
+
id,
|
|
284
|
+
lastVersion: version
|
|
285
|
+
}))),
|
|
286
|
+
catch: AiAssistantError.wrap({
|
|
287
|
+
message: "Artifact diff resolution error"
|
|
288
|
+
})
|
|
148
289
|
});
|
|
149
|
-
|
|
150
|
-
|
|
290
|
+
log("version", {
|
|
291
|
+
artifactDiff,
|
|
292
|
+
versions
|
|
293
|
+
}, {
|
|
294
|
+
F: __dxlog_file2,
|
|
295
|
+
L: 95,
|
|
296
|
+
S: this,
|
|
297
|
+
C: (f, a) => f(...a)
|
|
298
|
+
});
|
|
299
|
+
for (const [id, { version }] of [
|
|
300
|
+
...artifactDiff.entries()
|
|
301
|
+
]) {
|
|
302
|
+
if (ObjectVersion.equals(version, versions.get(id))) {
|
|
303
|
+
artifactDiff.delete(id);
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
blocks.push({
|
|
307
|
+
_tag: "anchor",
|
|
308
|
+
objectId: id,
|
|
309
|
+
version
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
if (artifactDiff.size > 0) {
|
|
313
|
+
blocks.push(createArtifactUpdateBlock(artifactDiff));
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return Obj2.make(Message.Message, {
|
|
317
|
+
created: (/* @__PURE__ */ new Date()).toISOString(),
|
|
318
|
+
sender: {
|
|
319
|
+
role: "user"
|
|
320
|
+
},
|
|
321
|
+
blocks: [
|
|
322
|
+
...blocks,
|
|
323
|
+
{
|
|
324
|
+
_tag: "text",
|
|
325
|
+
text: prompt
|
|
326
|
+
}
|
|
327
|
+
]
|
|
328
|
+
});
|
|
329
|
+
});
|
|
330
|
+
var gatherObjectVersions = (messages) => {
|
|
331
|
+
const artifactIds = /* @__PURE__ */ new Map();
|
|
332
|
+
for (const message of messages) {
|
|
333
|
+
for (const block of message.blocks) {
|
|
334
|
+
if (block._tag === "anchor") {
|
|
335
|
+
artifactIds.set(block.objectId, block.version);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
return artifactIds;
|
|
340
|
+
};
|
|
341
|
+
var createArtifactUpdateBlock = (artifactDiff) => {
|
|
342
|
+
return {
|
|
343
|
+
_tag: "text",
|
|
344
|
+
// TODO(dmaretskyi): Does this need to be a special content-block?
|
|
345
|
+
disposition: "artifact-update",
|
|
346
|
+
text: trim`
|
|
347
|
+
The following artifacts have been updated since the last message:
|
|
348
|
+
${[
|
|
349
|
+
...artifactDiff.entries()
|
|
350
|
+
].map(([id, { diff }]) => `<changed-artifact id="${id}">${diff ? `
|
|
351
|
+
${diff}` : ""}</changed-artifact>`).join("\n")}
|
|
352
|
+
`
|
|
353
|
+
};
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
// src/session/session.ts
|
|
357
|
+
var __dxlog_file3 = "/__w/dxos/dxos/packages/core/assistant/src/session/session.ts";
|
|
358
|
+
var AiSession = class {
|
|
359
|
+
_options;
|
|
360
|
+
/** Prevents concurrent execution of session. */
|
|
361
|
+
_semaphore = Effect3.runSync(Effect3.makeSemaphore(1));
|
|
362
|
+
/** Message history from queue. */
|
|
363
|
+
// TODO(burdon): Evolve into supporting a git-like graph of messages.
|
|
364
|
+
_history = [];
|
|
365
|
+
/** Pending messages for this session (incl. the current prompt). */
|
|
366
|
+
_pending = [];
|
|
367
|
+
constructor(_options = {}) {
|
|
368
|
+
this._options = _options;
|
|
369
|
+
}
|
|
370
|
+
run = ({ prompt, system: systemTemplate, history = [], objects = [], blueprints = [], toolkit, observer = GenerationObserver.noop() }) => Effect3.gen(this, function* () {
|
|
371
|
+
this._history = [
|
|
372
|
+
...history
|
|
373
|
+
];
|
|
374
|
+
this._pending = [];
|
|
375
|
+
const pending = this._pending;
|
|
376
|
+
const submitMessage = Effect3.fnUntraced(function* (message) {
|
|
377
|
+
pending.push(message);
|
|
378
|
+
yield* observer.onMessage(message);
|
|
379
|
+
yield* TracingService.emitConverationMessage(message);
|
|
380
|
+
return message;
|
|
381
|
+
});
|
|
382
|
+
const promptMessage = yield* submitMessage(yield* formatUserPrompt({
|
|
383
|
+
prompt,
|
|
384
|
+
history
|
|
385
|
+
}));
|
|
386
|
+
const system = yield* formatSystemPrompt({
|
|
387
|
+
system: systemTemplate,
|
|
388
|
+
blueprints,
|
|
389
|
+
objects
|
|
390
|
+
});
|
|
391
|
+
do {
|
|
392
|
+
log2("request", {
|
|
393
|
+
prompt: promptMessage,
|
|
394
|
+
system: {
|
|
395
|
+
snippet: createSnippet(system),
|
|
396
|
+
length: system.length
|
|
397
|
+
},
|
|
398
|
+
pending: this._pending.length,
|
|
399
|
+
history: this._history.length,
|
|
400
|
+
objects: objects?.length ?? 0
|
|
401
|
+
}, {
|
|
402
|
+
F: __dxlog_file3,
|
|
403
|
+
L: 110,
|
|
404
|
+
S: this,
|
|
405
|
+
C: (f, a) => f(...a)
|
|
406
|
+
});
|
|
407
|
+
const prompt2 = yield* AiPreprocessor.preprocessPrompt([
|
|
408
|
+
...this._history,
|
|
409
|
+
...this._pending
|
|
410
|
+
], {
|
|
411
|
+
system
|
|
412
|
+
});
|
|
413
|
+
const blocks = yield* LanguageModel.streamText({
|
|
414
|
+
prompt: prompt2,
|
|
415
|
+
toolkit,
|
|
416
|
+
disableToolCallResolution: true
|
|
417
|
+
}).pipe(
|
|
418
|
+
// TOOD(dmaretskyi): Error mapping.
|
|
419
|
+
AiParser.parseResponse({
|
|
420
|
+
onBegin: () => observer.onBegin(),
|
|
421
|
+
onBlock: (block) => observer.onBlock(block),
|
|
422
|
+
onPart: (part) => observer.onPart(part),
|
|
423
|
+
onEnd: (summary) => observer.onEnd(summary)
|
|
424
|
+
}),
|
|
425
|
+
Stream.runCollect,
|
|
426
|
+
Effect3.map(Chunk.toArray)
|
|
427
|
+
);
|
|
428
|
+
const response = yield* submitMessage(Obj3.make(Message2.Message, {
|
|
429
|
+
created: (/* @__PURE__ */ new Date()).toISOString(),
|
|
430
|
+
sender: {
|
|
431
|
+
role: "assistant"
|
|
432
|
+
},
|
|
433
|
+
blocks
|
|
434
|
+
}));
|
|
435
|
+
const toolCalls = getToolCalls(response);
|
|
436
|
+
if (toolCalls.length === 0) {
|
|
437
|
+
break;
|
|
438
|
+
} else if (!toolkit) {
|
|
439
|
+
throw new Error("No toolkit provided");
|
|
440
|
+
}
|
|
441
|
+
const toolResults = yield* Effect3.forEach(toolCalls, (toolCall) => {
|
|
442
|
+
return callTool(toolkit, toolCall).pipe(Effect3.provide(TracingService.layerSubframe((context) => ({
|
|
443
|
+
...context,
|
|
444
|
+
parentMessage: response.id,
|
|
445
|
+
toolCallId: toolCall.toolCallId
|
|
446
|
+
}))));
|
|
447
|
+
});
|
|
448
|
+
yield* submitMessage(Obj3.make(Message2.Message, {
|
|
449
|
+
created: (/* @__PURE__ */ new Date()).toISOString(),
|
|
450
|
+
sender: {
|
|
451
|
+
role: "tool"
|
|
452
|
+
},
|
|
453
|
+
blocks: toolResults
|
|
454
|
+
}));
|
|
455
|
+
} while (true);
|
|
456
|
+
log2("done", {
|
|
457
|
+
pending: this._pending.length
|
|
458
|
+
}, {
|
|
459
|
+
F: __dxlog_file3,
|
|
460
|
+
L: 181,
|
|
461
|
+
S: this,
|
|
462
|
+
C: (f, a) => f(...a)
|
|
463
|
+
});
|
|
464
|
+
return this._pending;
|
|
465
|
+
}).pipe(this._semaphore.withPermits(1), Effect3.withSpan("AiSession.run"));
|
|
466
|
+
/**
|
|
467
|
+
* @deprecated
|
|
468
|
+
*/
|
|
469
|
+
// TODO(burdon): Implement or remove.
|
|
470
|
+
async runStructured(_schema, _options) {
|
|
471
|
+
return todo();
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
var createSnippet = (text, len = 32) => text.length <= len * 2 ? text : [
|
|
475
|
+
text.slice(0, len),
|
|
476
|
+
"...",
|
|
477
|
+
text.slice(-len)
|
|
478
|
+
].join("");
|
|
479
|
+
|
|
480
|
+
// src/session/toolkit.ts
|
|
481
|
+
import * as Toolkit from "@effect/ai/Toolkit";
|
|
482
|
+
import * as Effect4 from "effect/Effect";
|
|
483
|
+
import { ToolExecutionService, ToolResolverService } from "@dxos/ai";
|
|
484
|
+
import { isTruthy } from "@dxos/util";
|
|
485
|
+
var createToolkit = ({ toolkit: toolkitParam, toolIds = [], blueprints = [] }) => Effect4.gen(function* () {
|
|
486
|
+
const blueprintToolkit = yield* ToolResolverService.resolveToolkit([
|
|
487
|
+
...blueprints.flatMap(({ tools }) => tools),
|
|
488
|
+
...toolIds
|
|
489
|
+
]);
|
|
490
|
+
const blueprintToolkitHandler = yield* blueprintToolkit.toContext(ToolExecutionService.handlersFor(blueprintToolkit));
|
|
491
|
+
const toolkit = Toolkit.merge(...[
|
|
492
|
+
toolkitParam,
|
|
493
|
+
blueprintToolkit
|
|
494
|
+
].filter(isTruthy));
|
|
495
|
+
return yield* toolkit.pipe(Effect4.provide(blueprintToolkitHandler));
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
// src/session/version-pin.ts
|
|
499
|
+
import * as Schema2 from "effect/Schema";
|
|
500
|
+
import { ObjectId } from "@dxos/echo/internal";
|
|
501
|
+
import { getVersion } from "@dxos/echo-db";
|
|
502
|
+
var ObjectVersionSchema = Schema2.Unknown;
|
|
503
|
+
var VersionPinSchema = Schema2.Struct({
|
|
504
|
+
// TODO(dmaretskyi): Use Ref when those support encoding.
|
|
505
|
+
objectId: ObjectId,
|
|
506
|
+
// TODO(dmaretskyi): Could be opaque.
|
|
507
|
+
version: ObjectVersionSchema
|
|
508
|
+
});
|
|
509
|
+
var VersionPin = class extends VersionPinSchema {
|
|
510
|
+
static DISPOSITION = "version-pin";
|
|
511
|
+
static fromObject(object) {
|
|
512
|
+
return VersionPin.make({
|
|
513
|
+
objectId: object.id,
|
|
514
|
+
version: getVersion(object)
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
static createBlock(pin) {
|
|
518
|
+
return {
|
|
519
|
+
_tag: "json",
|
|
520
|
+
disposition: VersionPin.DISPOSITION,
|
|
521
|
+
data: JSON.stringify(pin)
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
};
|
|
525
|
+
|
|
526
|
+
// src/session/GenericToolkit.ts
|
|
527
|
+
var GenericToolkit_exports = {};
|
|
528
|
+
__export(GenericToolkit_exports, {
|
|
529
|
+
TypeId: () => TypeId,
|
|
530
|
+
make: () => make,
|
|
531
|
+
merge: () => merge3
|
|
532
|
+
});
|
|
533
|
+
import * as Toolkit2 from "@effect/ai/Toolkit";
|
|
534
|
+
import * as Effect5 from "effect/Effect";
|
|
535
|
+
import * as Layer from "effect/Layer";
|
|
536
|
+
import * as Pipeable from "effect/Pipeable";
|
|
537
|
+
var TypeId = "~@dxos/assistant/GenericToolkit";
|
|
538
|
+
var make = (toolkit, layer) => {
|
|
539
|
+
return {
|
|
540
|
+
[TypeId]: TypeId,
|
|
541
|
+
toolkit,
|
|
542
|
+
layer,
|
|
543
|
+
handlers: toolkit.pipe(Effect5.provide(layer)),
|
|
544
|
+
pipe() {
|
|
545
|
+
return Pipeable.pipeArguments(this, arguments);
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
};
|
|
549
|
+
var merge3 = (...toolkits) => {
|
|
550
|
+
return make(Toolkit2.merge(...toolkits.map((t) => t.toolkit)), Layer.mergeAll(...toolkits.map((t) => t.layer)));
|
|
551
|
+
};
|
|
552
|
+
|
|
553
|
+
// src/conversation/conversation.ts
|
|
554
|
+
var __dxlog_file4 = "/__w/dxos/dxos/packages/core/assistant/src/conversation/conversation.ts";
|
|
555
|
+
var AiConversation = class extends Resource2 {
|
|
556
|
+
/**
|
|
557
|
+
* Message and binding queue.
|
|
558
|
+
*/
|
|
559
|
+
_queue;
|
|
560
|
+
/**
|
|
561
|
+
* Blueprints bound to the conversation.
|
|
562
|
+
*/
|
|
563
|
+
_context;
|
|
564
|
+
/**
|
|
565
|
+
* Toolkit from the current session request.
|
|
566
|
+
*/
|
|
567
|
+
_toolkit;
|
|
568
|
+
constructor(queue) {
|
|
569
|
+
super();
|
|
570
|
+
this._queue = queue;
|
|
571
|
+
this._context = new AiContextBinder(this._queue);
|
|
572
|
+
}
|
|
573
|
+
async _open() {
|
|
574
|
+
await this._context.open();
|
|
575
|
+
}
|
|
576
|
+
async _close() {
|
|
577
|
+
await this._context.close();
|
|
151
578
|
}
|
|
152
|
-
return obj;
|
|
153
|
-
}
|
|
154
|
-
var __dxlog_file = "/__w/dxos/dxos/packages/core/assistant/src/conversation/conversation.ts";
|
|
155
|
-
var AiConversation = class {
|
|
156
579
|
get context() {
|
|
157
580
|
return this._context;
|
|
158
581
|
}
|
|
@@ -161,31 +584,31 @@ var AiConversation = class {
|
|
|
161
584
|
}
|
|
162
585
|
async getHistory() {
|
|
163
586
|
const queueItems = await this._queue.queryObjects();
|
|
164
|
-
return queueItems.filter(
|
|
587
|
+
return queueItems.filter(Obj4.instanceOf(Message3.Message));
|
|
165
588
|
}
|
|
166
589
|
/**
|
|
167
590
|
* Creates a new cancelable request effect.
|
|
168
591
|
*/
|
|
169
592
|
createRequest(params) {
|
|
170
|
-
return
|
|
593
|
+
return Effect6.gen(this, function* () {
|
|
171
594
|
const session = new AiSession();
|
|
172
|
-
const history = yield*
|
|
173
|
-
const context = yield*
|
|
174
|
-
const blueprints = yield*
|
|
175
|
-
const objects = yield*
|
|
595
|
+
const history = yield* Effect6.promise(() => this.getHistory());
|
|
596
|
+
const context = yield* Effect6.promise(() => this.context.query());
|
|
597
|
+
const blueprints = yield* Effect6.forEach(context.blueprints.values(), DatabaseService2.loadOption).pipe(Effect6.map(Array5.filter(Option2.isSome)), Effect6.map(Array5.map((option) => option.value)));
|
|
598
|
+
const objects = yield* Effect6.forEach(context.objects.values(), DatabaseService2.loadOption).pipe(Effect6.map(Array5.filter(Option2.isSome)), Effect6.map(Array5.map((option) => option.value)));
|
|
176
599
|
const toolkit = yield* createToolkit({
|
|
177
600
|
blueprints
|
|
178
601
|
});
|
|
179
602
|
this._toolkit = toolkit;
|
|
180
603
|
const start = Date.now();
|
|
181
|
-
|
|
604
|
+
log3("run", {
|
|
182
605
|
history: history.length,
|
|
183
606
|
blueprints: blueprints.length,
|
|
184
607
|
objects: objects.length,
|
|
185
608
|
tools: this._toolkit?.tools.length ?? 0
|
|
186
609
|
}, {
|
|
187
|
-
F:
|
|
188
|
-
L:
|
|
610
|
+
F: __dxlog_file4,
|
|
611
|
+
L: 107,
|
|
189
612
|
S: this,
|
|
190
613
|
C: (f, a) => f(...a)
|
|
191
614
|
});
|
|
@@ -195,37 +618,30 @@ var AiConversation = class {
|
|
|
195
618
|
objects,
|
|
196
619
|
toolkit,
|
|
197
620
|
...params
|
|
198
|
-
}).pipe(
|
|
621
|
+
}).pipe(Effect6.provideService(AiContextService, {
|
|
199
622
|
binder: this.context
|
|
200
623
|
}));
|
|
201
|
-
|
|
624
|
+
log3("result", {
|
|
202
625
|
messages: messages.length,
|
|
203
626
|
duration: Date.now() - start
|
|
204
627
|
}, {
|
|
205
|
-
F:
|
|
206
|
-
L:
|
|
628
|
+
F: __dxlog_file4,
|
|
629
|
+
L: 121,
|
|
207
630
|
S: this,
|
|
208
631
|
C: (f, a) => f(...a)
|
|
209
632
|
});
|
|
210
|
-
yield*
|
|
633
|
+
yield* Effect6.promise(() => this._queue.append(messages));
|
|
211
634
|
return messages;
|
|
212
|
-
}).pipe(
|
|
213
|
-
}
|
|
214
|
-
constructor(options) {
|
|
215
|
-
_define_property2(this, "_queue", void 0);
|
|
216
|
-
_define_property2(this, "_context", void 0);
|
|
217
|
-
_define_property2(this, "_toolkit", void 0);
|
|
218
|
-
this._queue = options.queue;
|
|
219
|
-
this._context = new AiContextBinder(this._queue);
|
|
635
|
+
}).pipe(Effect6.withSpan("AiConversation.request"));
|
|
220
636
|
}
|
|
221
637
|
};
|
|
222
638
|
|
|
223
639
|
// src/diff.ts
|
|
224
640
|
import { next as A } from "@automerge/automerge";
|
|
225
641
|
import { DocAccessor, toCursorRange } from "@dxos/echo-db";
|
|
226
|
-
import { log as
|
|
642
|
+
import { log as log4 } from "@dxos/log";
|
|
227
643
|
import { isNonNullable } from "@dxos/util";
|
|
228
|
-
var
|
|
644
|
+
var __dxlog_file5 = "/__w/dxos/dxos/packages/core/assistant/src/diff.ts";
|
|
229
645
|
var reduceDiffs = (diffs) => {
|
|
230
646
|
return diffs.reduce((acc, diff) => {
|
|
231
647
|
const current = acc[acc.length - 1];
|
|
@@ -277,9 +693,9 @@ var applyDiffs = (accessor, diffs) => {
|
|
|
277
693
|
if (idx !== -1) {
|
|
278
694
|
A.splice(doc, accessor.path, idx, diff.match.length, diff.replace);
|
|
279
695
|
} else {
|
|
280
|
-
|
|
281
|
-
F:
|
|
282
|
-
L:
|
|
696
|
+
log4.warn("diff not found", diff, {
|
|
697
|
+
F: __dxlog_file5,
|
|
698
|
+
L: 78,
|
|
283
699
|
S: void 0,
|
|
284
700
|
C: (f, a) => f(...a)
|
|
285
701
|
});
|
|
@@ -289,79 +705,470 @@ var applyDiffs = (accessor, diffs) => {
|
|
|
289
705
|
return DocAccessor.getValue(accessor);
|
|
290
706
|
};
|
|
291
707
|
|
|
292
|
-
// src/
|
|
293
|
-
import {
|
|
294
|
-
import {
|
|
295
|
-
import {
|
|
296
|
-
import {
|
|
297
|
-
import {
|
|
298
|
-
import {
|
|
299
|
-
var
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
708
|
+
// src/execution-graph.ts
|
|
709
|
+
import { AgentStatus } from "@dxos/ai";
|
|
710
|
+
import { Obj as Obj5 } from "@dxos/echo";
|
|
711
|
+
import { MESSAGE_PROPERTY_TOOL_CALL_ID } from "@dxos/functions";
|
|
712
|
+
import { LogLevel } from "@dxos/log";
|
|
713
|
+
import { ContentBlock, Message as Message4 } from "@dxos/types";
|
|
714
|
+
import { isNonNullable as isNonNullable2 } from "@dxos/util";
|
|
715
|
+
var SKIP_BLOCKS = [
|
|
716
|
+
"text"
|
|
717
|
+
];
|
|
718
|
+
var ExecutionGraph = class {
|
|
719
|
+
_skipBlocks;
|
|
720
|
+
// TODO(dmaretskyi): Evolve the internal state to support chats, functions, circuit workflows, etc.
|
|
721
|
+
_commits = [];
|
|
722
|
+
_branchNames = /* @__PURE__ */ new Set();
|
|
723
|
+
_lastBlockId;
|
|
724
|
+
// Tool call tracking.
|
|
725
|
+
_toolCallCommitIds = /* @__PURE__ */ new Map();
|
|
726
|
+
_lastCommitByBranch = /* @__PURE__ */ new Map();
|
|
727
|
+
_pendingToolResults = /* @__PURE__ */ new Map();
|
|
728
|
+
constructor(_skipBlocks = SKIP_BLOCKS) {
|
|
729
|
+
this._skipBlocks = _skipBlocks;
|
|
730
|
+
}
|
|
731
|
+
/**
|
|
732
|
+
* Adds events to the graph.
|
|
733
|
+
*/
|
|
734
|
+
addEvents(events) {
|
|
735
|
+
const sortedEvents = this.sortObjectsByCreated(events);
|
|
736
|
+
for (const event of sortedEvents) {
|
|
737
|
+
if (Obj5.instanceOf(Message4.Message, event)) {
|
|
738
|
+
this._processMessage(event);
|
|
739
|
+
} else if (Obj5.instanceOf(AgentStatus, event)) {
|
|
740
|
+
this._processAgentStatus(event);
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
/**
|
|
745
|
+
* Processes a message event and creates commits for its blocks.
|
|
746
|
+
*/
|
|
747
|
+
_processMessage(message) {
|
|
748
|
+
const messageCommits = messageToCommits(message, this._lastBlockId, this._toolCallCommitIds, this._lastCommitByBranch, this._skipBlocks);
|
|
749
|
+
this._commits.push(...messageCommits);
|
|
750
|
+
messageCommits.forEach((commit) => this._branchNames.add(commit.branch));
|
|
751
|
+
this._trackToolCalls(messageCommits);
|
|
752
|
+
if (messageCommits.length > 0) {
|
|
753
|
+
this._lastBlockId = messageCommits[messageCommits.length - 1].id;
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
/**
|
|
757
|
+
* Processes an AgentStatus event.
|
|
758
|
+
*/
|
|
759
|
+
_processAgentStatus(event) {
|
|
760
|
+
const branch = this._getToolCallBranch(event.parentMessage, event.toolCallId);
|
|
761
|
+
this._branchNames.add(branch);
|
|
762
|
+
const agentStatusCommit = {
|
|
763
|
+
id: event.id,
|
|
764
|
+
branch,
|
|
765
|
+
message: event.message,
|
|
766
|
+
icon: "ph--flag--regular",
|
|
767
|
+
parents: this._getAgentStatusParents(event),
|
|
768
|
+
timestamp: new Date(event.created)
|
|
769
|
+
};
|
|
770
|
+
this._commits.push(agentStatusCommit);
|
|
771
|
+
this._lastBlockId = event.id;
|
|
772
|
+
if (event.toolCallId) {
|
|
773
|
+
this._lastCommitByBranch.set(branch, event.id);
|
|
774
|
+
this._updatePendingToolResults(event.toolCallId, event.id);
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
/**
|
|
778
|
+
* Stable sort: objects with 'created' field are sorted by it, others remain in place.
|
|
779
|
+
*/
|
|
780
|
+
sortObjectsByCreated(objects) {
|
|
781
|
+
const sortedObjects = objects.slice();
|
|
782
|
+
const createdObjects = [];
|
|
783
|
+
sortedObjects.forEach((obj, idx) => {
|
|
784
|
+
if (obj && typeof obj.created === "string") {
|
|
785
|
+
createdObjects.push({
|
|
786
|
+
idx,
|
|
787
|
+
obj
|
|
788
|
+
});
|
|
306
789
|
}
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
790
|
+
});
|
|
791
|
+
createdObjects.sort((a, b) => {
|
|
792
|
+
const aDate = new Date(a.obj.created).getTime();
|
|
793
|
+
const bDate = new Date(b.obj.created).getTime();
|
|
794
|
+
return aDate - bDate;
|
|
795
|
+
});
|
|
796
|
+
let createdIdx = 0;
|
|
797
|
+
return sortedObjects.map((obj, _idx) => {
|
|
798
|
+
if (obj && typeof obj.created === "string") {
|
|
799
|
+
return createdObjects[createdIdx++].obj;
|
|
310
800
|
}
|
|
311
|
-
return
|
|
312
|
-
})
|
|
801
|
+
return obj;
|
|
802
|
+
});
|
|
803
|
+
}
|
|
804
|
+
/**
|
|
805
|
+
* Returns the current state of the graph.
|
|
806
|
+
*/
|
|
807
|
+
getGraph(lastRequest = false) {
|
|
808
|
+
const idx = lastRequest ? this._commits.findLastIndex((c) => c.tags?.includes("user")) : -1;
|
|
809
|
+
const commits = idx === -1 ? this._commits : this._commits.slice(idx);
|
|
810
|
+
return {
|
|
811
|
+
branches: Array.from(this._branchNames),
|
|
812
|
+
commits
|
|
813
|
+
};
|
|
814
|
+
}
|
|
815
|
+
/**
|
|
816
|
+
* Tracks tool calls and handles pending tool result updates.
|
|
817
|
+
*/
|
|
818
|
+
_trackToolCalls(commits) {
|
|
819
|
+
commits.forEach((commit) => {
|
|
820
|
+
if (this._isToolCallCommit(commit)) {
|
|
821
|
+
const toolCallId = this._extractToolCallId(commit.id);
|
|
822
|
+
if (toolCallId) {
|
|
823
|
+
this._toolCallCommitIds.set(toolCallId, commit.id);
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
if (this._isToolResultCommit(commit)) {
|
|
827
|
+
const toolCallId = this._extractToolCallId(commit.id);
|
|
828
|
+
if (toolCallId) {
|
|
829
|
+
this._pendingToolResults.set(toolCallId, commit.id);
|
|
830
|
+
this._handleLateToolResultUpdate(toolCallId, commit.id);
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
this._lastCommitByBranch.set(commit.branch, commit.id);
|
|
834
|
+
});
|
|
835
|
+
}
|
|
836
|
+
/**
|
|
837
|
+
* Updates pending tool results to point to an AgentStatus event.
|
|
838
|
+
*/
|
|
839
|
+
_updatePendingToolResults(toolCallId, agentStatusId) {
|
|
840
|
+
const toolResultId = this._pendingToolResults.get(toolCallId);
|
|
841
|
+
if (!toolResultId) return;
|
|
842
|
+
const toolResultCommit = this._commits.find((c) => c.id === toolResultId);
|
|
843
|
+
if (!toolResultCommit) return;
|
|
844
|
+
const toolCallCommitId = this._toolCallCommitIds.get(toolCallId);
|
|
845
|
+
if (!toolCallCommitId) return;
|
|
846
|
+
const toolCallIndex = toolResultCommit.parents.indexOf(toolCallCommitId);
|
|
847
|
+
if (toolCallIndex !== -1) {
|
|
848
|
+
toolResultCommit.parents[toolCallIndex] = agentStatusId;
|
|
849
|
+
}
|
|
850
|
+
this._pendingToolResults.delete(toolCallId);
|
|
851
|
+
}
|
|
852
|
+
/**
|
|
853
|
+
* Handles late updates when AgentStatus is processed before tool result.
|
|
854
|
+
*/
|
|
855
|
+
_handleLateToolResultUpdate(toolCallId, toolResultId) {
|
|
856
|
+
const toolCallCommitId = this._toolCallCommitIds.get(toolCallId);
|
|
857
|
+
if (!toolCallCommitId) return;
|
|
858
|
+
const messageId = toolCallCommitId.split("_toolCall_")[0];
|
|
859
|
+
const toolCallBranch = `${messageId}_${toolCallId}`;
|
|
860
|
+
const agentStatusCommitId = this._lastCommitByBranch.get(toolCallBranch);
|
|
861
|
+
if (agentStatusCommitId && agentStatusCommitId !== toolCallCommitId) {
|
|
862
|
+
const toolResultCommit = this._commits.find((c) => c.id === toolResultId);
|
|
863
|
+
if (toolResultCommit) {
|
|
864
|
+
const toolCallIndex = toolResultCommit.parents.indexOf(toolCallCommitId);
|
|
865
|
+
if (toolCallIndex !== -1) {
|
|
866
|
+
toolResultCommit.parents[toolCallIndex] = agentStatusCommitId;
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
this._pendingToolResults.delete(toolCallId);
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
/**
|
|
873
|
+
* Gets the branch name for a tool call.
|
|
874
|
+
*/
|
|
875
|
+
_getToolCallBranch(parentMessage, toolCallId) {
|
|
876
|
+
if (toolCallId) {
|
|
877
|
+
return toolCallId;
|
|
878
|
+
}
|
|
879
|
+
return parentMessage || "main";
|
|
880
|
+
}
|
|
881
|
+
/**
|
|
882
|
+
* Gets the parents for an AgentStatus commit.
|
|
883
|
+
*/
|
|
884
|
+
_getAgentStatusParents(event) {
|
|
885
|
+
if (event.parentMessage && event.toolCallId) {
|
|
886
|
+
return [
|
|
887
|
+
getToolCallId(event.parentMessage, event.toolCallId)
|
|
888
|
+
];
|
|
889
|
+
}
|
|
890
|
+
return [];
|
|
891
|
+
}
|
|
892
|
+
/**
|
|
893
|
+
* Checks if a commit is a tool call commit.
|
|
894
|
+
*/
|
|
895
|
+
_isToolCallCommit(commit) {
|
|
896
|
+
return commit.message.includes("Calling tool");
|
|
897
|
+
}
|
|
898
|
+
/**
|
|
899
|
+
* Checks if a commit is a tool result commit.
|
|
900
|
+
*/
|
|
901
|
+
_isToolResultCommit(commit) {
|
|
902
|
+
return commit.message.includes("Tool call succeeded") || commit.message.includes("Tool error:");
|
|
903
|
+
}
|
|
904
|
+
/**
|
|
905
|
+
* Extracts tool call ID from a commit ID.
|
|
906
|
+
*/
|
|
907
|
+
_extractToolCallId(commitId) {
|
|
908
|
+
const toolCallMatch = commitId.match(/_toolCall_(.+)$/);
|
|
909
|
+
const toolResultMatch = commitId.match(/_toolResult_(.+)$/);
|
|
910
|
+
return toolCallMatch?.[1] || toolResultMatch?.[1];
|
|
911
|
+
}
|
|
912
|
+
};
|
|
913
|
+
var messageToCommits = (message, lastBlockId, toolCallIds, lastCommitByBranch, skipBlocks) => {
|
|
914
|
+
let previousBlockId = lastBlockId;
|
|
915
|
+
return message.blocks.map((block, idx) => {
|
|
916
|
+
const branch = getMessageBranch(message);
|
|
917
|
+
const parents = getBlockParents(block, previousBlockId, message, toolCallIds, lastCommitByBranch);
|
|
918
|
+
if (skipBlocks?.includes(block._tag)) {
|
|
919
|
+
return null;
|
|
920
|
+
}
|
|
921
|
+
const commit = createBlockCommit(block, message, branch, parents, idx);
|
|
922
|
+
if (commit) {
|
|
923
|
+
previousBlockId = commit.id;
|
|
924
|
+
}
|
|
925
|
+
return commit;
|
|
926
|
+
}).filter(isNonNullable2);
|
|
927
|
+
};
|
|
928
|
+
var getBlockParents = (block, previousBlockId, message, toolCallIds, lastCommitByBranch) => {
|
|
929
|
+
const parents = [];
|
|
930
|
+
if (block._tag === "toolResult") {
|
|
931
|
+
if (previousBlockId) {
|
|
932
|
+
parents.push(previousBlockId);
|
|
933
|
+
}
|
|
934
|
+
const toolCallCommitId = toolCallIds?.get(block.toolCallId);
|
|
935
|
+
if (toolCallCommitId) {
|
|
936
|
+
const toolCallBranch = getToolCallBranchFromCommitId(toolCallCommitId, block.toolCallId);
|
|
937
|
+
const lastBlockFromToolCallBranch = lastCommitByBranch?.get(toolCallBranch);
|
|
938
|
+
if (lastBlockFromToolCallBranch) {
|
|
939
|
+
parents.push(lastBlockFromToolCallBranch);
|
|
940
|
+
} else {
|
|
941
|
+
parents.push(toolCallCommitId);
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
} else {
|
|
945
|
+
if (previousBlockId) {
|
|
946
|
+
parents.push(previousBlockId);
|
|
947
|
+
} else {
|
|
948
|
+
const toolCallParent = getParentId(message);
|
|
949
|
+
if (toolCallParent) {
|
|
950
|
+
parents.push(toolCallParent);
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
return parents;
|
|
955
|
+
};
|
|
956
|
+
var createBlockCommit = (block, message, branch, parents, idx) => {
|
|
957
|
+
const timestamp = new Date(message.created);
|
|
958
|
+
switch (block._tag) {
|
|
959
|
+
case "text":
|
|
960
|
+
if (!block.text.trim().length) return null;
|
|
961
|
+
return {
|
|
962
|
+
id: getGenericBlockId(message.id, idx),
|
|
963
|
+
branch,
|
|
964
|
+
parents,
|
|
965
|
+
timestamp,
|
|
966
|
+
...message.sender.role === "user" ? {
|
|
967
|
+
icon: "ph--user--regular",
|
|
968
|
+
tags: [
|
|
969
|
+
"user"
|
|
970
|
+
],
|
|
971
|
+
message: "Processing request..."
|
|
972
|
+
} : {
|
|
973
|
+
icon: "ph--robot--regular",
|
|
974
|
+
message: `Response (${block.text.split(" ").length} words)`
|
|
975
|
+
}
|
|
976
|
+
};
|
|
977
|
+
case "toolCall":
|
|
978
|
+
return {
|
|
979
|
+
id: getToolCallId(message.id, block.toolCallId),
|
|
980
|
+
branch,
|
|
981
|
+
parents,
|
|
982
|
+
timestamp,
|
|
983
|
+
icon: "ph--wrench--regular",
|
|
984
|
+
level: LogLevel.INFO,
|
|
985
|
+
message: `Calling tool (${block.name})`
|
|
986
|
+
};
|
|
987
|
+
case "toolResult":
|
|
988
|
+
return {
|
|
989
|
+
id: getToolResultId(message.id, block.toolCallId),
|
|
990
|
+
branch,
|
|
991
|
+
parents,
|
|
992
|
+
timestamp,
|
|
993
|
+
icon: block.error ? "ph--x-circle--regular" : "ph--check-circle--regular",
|
|
994
|
+
level: block.error ? LogLevel.ERROR : LogLevel.INFO,
|
|
995
|
+
message: block.error ? "Tool error: " + block.error : "Tool call succeeded"
|
|
996
|
+
};
|
|
997
|
+
case "summary":
|
|
998
|
+
return {
|
|
999
|
+
id: getGenericBlockId(message.id, idx),
|
|
1000
|
+
branch,
|
|
1001
|
+
parents,
|
|
1002
|
+
timestamp,
|
|
1003
|
+
icon: "ph--rocket--regular",
|
|
1004
|
+
level: LogLevel.INFO,
|
|
1005
|
+
message: ContentBlock.createSummaryMessage(block)
|
|
1006
|
+
};
|
|
1007
|
+
case "status":
|
|
1008
|
+
return {
|
|
1009
|
+
id: getGenericBlockId(message.id, idx),
|
|
1010
|
+
branch,
|
|
1011
|
+
parents,
|
|
1012
|
+
timestamp,
|
|
1013
|
+
message: block.statusText,
|
|
1014
|
+
level: LogLevel.INFO,
|
|
1015
|
+
icon: "ph--flag--regular"
|
|
1016
|
+
};
|
|
1017
|
+
case "reasoning":
|
|
1018
|
+
return {
|
|
1019
|
+
id: getGenericBlockId(message.id, idx),
|
|
1020
|
+
branch,
|
|
1021
|
+
parents,
|
|
1022
|
+
timestamp,
|
|
1023
|
+
message: block.reasoningText ?? "Thinking...",
|
|
1024
|
+
icon: "ph--brain--regular"
|
|
1025
|
+
};
|
|
1026
|
+
case "reference":
|
|
1027
|
+
return {
|
|
1028
|
+
id: getGenericBlockId(message.id, idx),
|
|
1029
|
+
branch,
|
|
1030
|
+
parents,
|
|
1031
|
+
timestamp,
|
|
1032
|
+
icon: "ph--link--regular",
|
|
1033
|
+
message: stringifyRef(block.reference)
|
|
1034
|
+
};
|
|
1035
|
+
default:
|
|
1036
|
+
return null;
|
|
1037
|
+
}
|
|
1038
|
+
};
|
|
1039
|
+
var getToolCallBranchFromCommitId = (toolCallCommitId, toolCallId) => {
|
|
1040
|
+
const messageId = toolCallCommitId.split("_toolCall_")[0];
|
|
1041
|
+
return `${messageId}_${toolCallId}`;
|
|
1042
|
+
};
|
|
1043
|
+
var getToolCallId = (messageId, toolCallId) => `${messageId}_toolCall_${toolCallId}`;
|
|
1044
|
+
var getToolResultId = (messageId, toolCallId) => `${messageId}_toolResult_${toolCallId}`;
|
|
1045
|
+
var getGenericBlockId = (messageId, idx) => `${messageId}_block_${idx}`;
|
|
1046
|
+
var getBranchName = (options) => {
|
|
1047
|
+
if (options.toolCallId) {
|
|
1048
|
+
return options.toolCallId;
|
|
1049
|
+
} else if (options.parentMessage) {
|
|
1050
|
+
return options.parentMessage;
|
|
1051
|
+
} else {
|
|
1052
|
+
return "main";
|
|
1053
|
+
}
|
|
1054
|
+
};
|
|
1055
|
+
var getMessageBranch = (message) => {
|
|
1056
|
+
return getBranchName({
|
|
1057
|
+
parentMessage: message.parentMessage,
|
|
1058
|
+
toolCallId: message.properties?.[MESSAGE_PROPERTY_TOOL_CALL_ID]
|
|
313
1059
|
});
|
|
314
1060
|
};
|
|
315
|
-
var
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
1061
|
+
var getParentId = (message) => {
|
|
1062
|
+
if (message.parentMessage && message.properties?.[MESSAGE_PROPERTY_TOOL_CALL_ID]) {
|
|
1063
|
+
return getToolCallId(message.parentMessage, message.properties[MESSAGE_PROPERTY_TOOL_CALL_ID]);
|
|
1064
|
+
} else {
|
|
1065
|
+
return void 0;
|
|
1066
|
+
}
|
|
1067
|
+
};
|
|
1068
|
+
var stringifyRef = (ref) => {
|
|
1069
|
+
if (ref.target) {
|
|
1070
|
+
return stringifyObject(ref.target);
|
|
1071
|
+
}
|
|
1072
|
+
return ref.dxn.asEchoDXN()?.echoId ?? ref.dxn.asQueueDXN()?.objectId ?? "";
|
|
1073
|
+
};
|
|
1074
|
+
var stringifyObject = (obj) => {
|
|
1075
|
+
return Obj5.getLabel(obj) ?? Obj5.getTypename(obj) ?? obj.id;
|
|
1076
|
+
};
|
|
1077
|
+
|
|
1078
|
+
// src/functions/services.ts
|
|
1079
|
+
import * as Tool from "@effect/ai/Tool";
|
|
1080
|
+
import * as Context3 from "effect/Context";
|
|
1081
|
+
import * as Effect7 from "effect/Effect";
|
|
1082
|
+
import * as Layer2 from "effect/Layer";
|
|
1083
|
+
import * as Record from "effect/Record";
|
|
1084
|
+
import * as Schema3 from "effect/Schema";
|
|
1085
|
+
import { AiToolNotFoundError, ToolExecutionService as ToolExecutionService2, ToolResolverService as ToolResolverService2 } from "@dxos/ai";
|
|
1086
|
+
import { todo as todo2 } from "@dxos/debug";
|
|
1087
|
+
import { Query as Query2 } from "@dxos/echo";
|
|
1088
|
+
import { DatabaseService as DatabaseService3 } from "@dxos/echo-db";
|
|
1089
|
+
import { Function as Function4, FunctionDefinition, FunctionInvocationService } from "@dxos/functions";
|
|
1090
|
+
import { invariant as invariant2 } from "@dxos/invariant";
|
|
1091
|
+
var __dxlog_file6 = "/__w/dxos/dxos/packages/core/assistant/src/functions/services.ts";
|
|
1092
|
+
var makeToolResolverFromFunctions = (functions, toolkit) => {
|
|
1093
|
+
return Layer2.effect(ToolResolverService2, Effect7.gen(function* () {
|
|
1094
|
+
const dbService = yield* DatabaseService3;
|
|
1095
|
+
return {
|
|
1096
|
+
resolve: (id) => Effect7.gen(function* () {
|
|
1097
|
+
const tool = toolkit.tools[id];
|
|
1098
|
+
if (tool) {
|
|
1099
|
+
return tool;
|
|
1100
|
+
}
|
|
1101
|
+
const { objects: [dbFunction] } = yield* DatabaseService3.runQuery(Query2.type(Function4.Function, {
|
|
1102
|
+
key: id
|
|
1103
|
+
}));
|
|
1104
|
+
const functionDef = dbFunction ? FunctionDefinition.deserialize(dbFunction) : functions.find((fn2) => fn2.key === id);
|
|
1105
|
+
if (!functionDef) {
|
|
1106
|
+
return yield* Effect7.fail(new AiToolNotFoundError(id));
|
|
1107
|
+
}
|
|
1108
|
+
return projectFunctionToTool(functionDef);
|
|
1109
|
+
}).pipe(Effect7.provideService(DatabaseService3, dbService))
|
|
1110
|
+
};
|
|
1111
|
+
}));
|
|
1112
|
+
};
|
|
1113
|
+
var makeToolExecutionServiceFromFunctions = (toolkit, handlersLayer) => {
|
|
1114
|
+
return Layer2.effect(ToolExecutionService2, Effect7.gen(function* () {
|
|
1115
|
+
const toolkitHandler = yield* toolkit.pipe(Effect7.provide(handlersLayer));
|
|
1116
|
+
invariant2(isHandlerLike(toolkitHandler), void 0, {
|
|
1117
|
+
F: __dxlog_file6,
|
|
1118
|
+
L: 74,
|
|
1119
|
+
S: this,
|
|
1120
|
+
A: [
|
|
1121
|
+
"isHandlerLike(toolkitHandler)",
|
|
1122
|
+
""
|
|
1123
|
+
]
|
|
1124
|
+
});
|
|
1125
|
+
const functionInvocationService = yield* FunctionInvocationService;
|
|
319
1126
|
return {
|
|
320
1127
|
handlersFor: (toolkit2) => {
|
|
321
1128
|
const makeHandler = (tool) => {
|
|
322
|
-
return
|
|
323
|
-
if (toolkitHandler.tools
|
|
324
|
-
|
|
1129
|
+
return Effect7.fn(`toolFunctionHandler ${tool.name}`)(function* (input) {
|
|
1130
|
+
if (toolkitHandler.tools[tool.name]) {
|
|
1131
|
+
if (Tool.isProviderDefined(tool)) {
|
|
1132
|
+
throw new Error("Attempted to call a provider-defined tool");
|
|
1133
|
+
}
|
|
1134
|
+
const { result } = yield* toolkitHandler.handle(tool.name, input);
|
|
1135
|
+
return result;
|
|
325
1136
|
}
|
|
326
|
-
const {
|
|
327
|
-
|
|
328
|
-
if (!functionDef) {
|
|
329
|
-
return yield* Effect2.fail(new AiToolNotFoundError(tool.name));
|
|
330
|
-
}
|
|
331
|
-
return yield* localFunctionExecutionService.invokeFunction(functionDef, input).pipe(Effect2.catchAllDefect((defect) => Effect2.fail(defect)));
|
|
1137
|
+
const { definition: functionDef } = Context3.get(FunctionToolAnnotation)(tool.annotations);
|
|
1138
|
+
return yield* functionInvocationService.invokeFunction(functionDef, input).pipe(Effect7.catchAllDefect((defect) => Effect7.fail(defect)));
|
|
332
1139
|
});
|
|
333
1140
|
};
|
|
334
|
-
return toolkit2.of(Record.map(toolkit2.tools, (tool, _name) => makeHandler(tool)));
|
|
1141
|
+
return toolkit2.of(Record.map(toolkit2.tools, (tool, _name) => Tool.isUserDefined(tool) ? makeHandler(tool) : null));
|
|
335
1142
|
}
|
|
336
1143
|
};
|
|
337
1144
|
}));
|
|
338
1145
|
};
|
|
339
|
-
var FunctionToolAnnotation = class extends
|
|
1146
|
+
var FunctionToolAnnotation = class extends Context3.Tag("@dxos/assistant/FunctionToolAnnotation")() {
|
|
340
1147
|
};
|
|
341
1148
|
var toolCache = /* @__PURE__ */ new WeakMap();
|
|
342
|
-
var projectFunctionToTool = (
|
|
343
|
-
if (toolCache.has(
|
|
344
|
-
return toolCache.get(
|
|
1149
|
+
var projectFunctionToTool = (fn2) => {
|
|
1150
|
+
if (toolCache.has(fn2)) {
|
|
1151
|
+
return toolCache.get(fn2);
|
|
345
1152
|
}
|
|
346
|
-
const tool =
|
|
347
|
-
description:
|
|
348
|
-
parameters: createStructFieldsFromSchema(
|
|
1153
|
+
const tool = Tool.make(makeToolName(fn2.name), {
|
|
1154
|
+
description: fn2.description,
|
|
1155
|
+
parameters: createStructFieldsFromSchema(fn2.inputSchema),
|
|
349
1156
|
// TODO(dmaretskyi): Include output schema.
|
|
350
|
-
failure:
|
|
1157
|
+
failure: Schema3.Any
|
|
351
1158
|
}).annotate(FunctionToolAnnotation, {
|
|
352
|
-
|
|
1159
|
+
definition: fn2
|
|
353
1160
|
});
|
|
354
|
-
toolCache.set(
|
|
1161
|
+
toolCache.set(fn2, tool);
|
|
355
1162
|
return tool;
|
|
356
1163
|
};
|
|
357
1164
|
var makeToolName = (name) => {
|
|
358
|
-
const toolName = name.replace(/[^a-zA-Z0-9]/g, "
|
|
359
|
-
|
|
360
|
-
F:
|
|
361
|
-
L:
|
|
1165
|
+
const toolName = name.toLowerCase().replace(/[^a-zA-Z0-9]/g, "-");
|
|
1166
|
+
invariant2(toolName.match(/^[a-z_][a-z0-9-_]*$/), void 0, {
|
|
1167
|
+
F: __dxlog_file6,
|
|
1168
|
+
L: 144,
|
|
362
1169
|
S: void 0,
|
|
363
1170
|
A: [
|
|
364
|
-
"toolName.match(/^[a-
|
|
1171
|
+
"toolName.match(/^[a-z_][a-z0-9-_]*$/)",
|
|
365
1172
|
""
|
|
366
1173
|
]
|
|
367
1174
|
});
|
|
@@ -372,32 +1179,35 @@ var createStructFieldsFromSchema = (schema) => {
|
|
|
372
1179
|
case "TypeLiteral":
|
|
373
1180
|
return Object.fromEntries(schema.ast.propertySignatures.map((prop) => [
|
|
374
1181
|
prop.name,
|
|
375
|
-
|
|
1182
|
+
Schema3.make(prop.type)
|
|
376
1183
|
]));
|
|
377
1184
|
case "VoidKeyword":
|
|
378
1185
|
return {};
|
|
379
1186
|
default:
|
|
380
|
-
return
|
|
1187
|
+
return todo2(`Unsupported schema AST: ${schema.ast._tag}`);
|
|
381
1188
|
}
|
|
382
1189
|
};
|
|
1190
|
+
var isHandlerLike = (value) => {
|
|
1191
|
+
return typeof value.tools === "object" && typeof value.handle === "function";
|
|
1192
|
+
};
|
|
383
1193
|
|
|
384
1194
|
// src/templates/system.ts
|
|
385
1195
|
import defaultsDeep from "lodash.defaultsdeep";
|
|
386
|
-
import { Template } from "@dxos/blueprints";
|
|
1196
|
+
import { Template as Template2 } from "@dxos/blueprints";
|
|
387
1197
|
|
|
388
|
-
// raw-loader
|
|
389
|
-
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";
|
|
1198
|
+
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/instructions/format.tpl?raw
|
|
1199
|
+
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";
|
|
390
1200
|
|
|
391
|
-
// raw-loader
|
|
392
|
-
var system_default = "{{! System Prompt }}\n\
|
|
1201
|
+
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/instructions/system.tpl?raw
|
|
1202
|
+
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";
|
|
393
1203
|
|
|
394
1204
|
// src/templates/system.ts
|
|
395
|
-
var createSystemPrompt = (options = {}) =>
|
|
1205
|
+
var createSystemPrompt = (options = {}) => Template2.process(format_default, defaultsDeep(options, {
|
|
396
1206
|
DATETIME: (/* @__PURE__ */ new Date()).toISOString()
|
|
397
1207
|
}));
|
|
398
1208
|
var templates = {
|
|
399
1209
|
/** Editable system prompt.*/
|
|
400
|
-
system:
|
|
1210
|
+
system: Template2.make({
|
|
401
1211
|
/**
|
|
402
1212
|
* NOTE: We are forbidden from accessing randomness at model level in cloudflare, so we have to have pre-generated ids.
|
|
403
1213
|
*/
|
|
@@ -406,13 +1216,15 @@ var templates = {
|
|
|
406
1216
|
})
|
|
407
1217
|
};
|
|
408
1218
|
|
|
409
|
-
// src/types.ts
|
|
410
|
-
import
|
|
1219
|
+
// src/types/artifact.ts
|
|
1220
|
+
import * as Schema4 from "effect/Schema";
|
|
1221
|
+
import { DatabaseService as DatabaseService4 } from "@dxos/echo-db";
|
|
411
1222
|
import { DXN as DXN2, LOCAL_SPACE_TAG } from "@dxos/keys";
|
|
412
|
-
import { trim } from "@dxos/util";
|
|
1223
|
+
import { trim as trim2 } from "@dxos/util";
|
|
413
1224
|
var createArtifactElement = (id) => `<artifact id=${id} />`;
|
|
414
|
-
var ArtifactId = class extends
|
|
415
|
-
|
|
1225
|
+
var ArtifactId = class extends Schema4.String.annotations({
|
|
1226
|
+
// TODO(dmaretskyi): This section gets overriden.
|
|
1227
|
+
description: trim2`
|
|
416
1228
|
The ID of the referenced object. Formats accepted:
|
|
417
1229
|
- DXN (dxn:echo:@:XXXXX). DXNs can be prepended with an @ symbol for compatibility with in-text references.
|
|
418
1230
|
- space ID, object ID tuple (spaceID:objectID)
|
|
@@ -442,19 +1254,28 @@ var ArtifactId = class extends Schema3.String.annotations({
|
|
|
442
1254
|
throw new Error(`Unable to parse object reference: ${reference}`);
|
|
443
1255
|
}
|
|
444
1256
|
}
|
|
1257
|
+
/**
|
|
1258
|
+
* Resolves an artifact ID to an object.
|
|
1259
|
+
*/
|
|
1260
|
+
static resolve(schema, ref) {
|
|
1261
|
+
const dxn = ArtifactId.toDXN(ref);
|
|
1262
|
+
return DatabaseService4.resolve(dxn, schema);
|
|
1263
|
+
}
|
|
445
1264
|
};
|
|
446
1265
|
export {
|
|
447
1266
|
AiAssistantError,
|
|
448
1267
|
AiContextBinder,
|
|
449
1268
|
AiContextService,
|
|
450
1269
|
AiConversation,
|
|
1270
|
+
AiModelError,
|
|
451
1271
|
AiSession,
|
|
452
|
-
AnthropicError,
|
|
453
1272
|
ArtifactDiffResolver,
|
|
454
1273
|
ArtifactId,
|
|
455
1274
|
Bindings,
|
|
456
1275
|
ContextBinding,
|
|
1276
|
+
ExecutionGraph,
|
|
457
1277
|
GenerationObserver,
|
|
1278
|
+
GenericToolkit_exports as GenericToolkit,
|
|
458
1279
|
VersionPin,
|
|
459
1280
|
applyDiffs,
|
|
460
1281
|
computeDiffsWithCursors,
|