@dxos/assistant 0.8.4-main.a4bbb77 → 0.8.4-main.abd8ff62ef
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/README.md +1 -0
- package/dist/lib/neutral/extraction/index.mjs +202 -0
- package/dist/lib/neutral/extraction/index.mjs.map +7 -0
- package/dist/lib/neutral/index.mjs +1731 -0
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/types/src/conversation/context.d.ts +120 -64
- package/dist/types/src/conversation/context.d.ts.map +1 -1
- package/dist/types/src/conversation/context.test.d.ts +2 -0
- package/dist/types/src/conversation/context.test.d.ts.map +1 -0
- package/dist/types/src/conversation/index.d.ts +1 -1
- package/dist/types/src/conversation/index.d.ts.map +1 -1
- package/dist/types/src/conversation/session.d.ts +78 -0
- package/dist/types/src/conversation/session.d.ts.map +1 -0
- package/dist/types/src/diff.d.ts +3 -1
- package/dist/types/src/diff.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +32 -40
- 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-ner-function/index.d.ts +0 -1
- package/dist/types/src/extraction/extraction-ner-function/index.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction.d.ts +1066 -307
- package/dist/types/src/extraction/extraction.d.ts.map +1 -1
- package/dist/types/src/extraction/index.d.ts +0 -1
- package/dist/types/src/extraction/index.d.ts.map +1 -1
- package/dist/types/src/extraction/quotes.d.ts +3 -3
- package/dist/types/src/extraction/quotes.d.ts.map +1 -1
- package/dist/types/src/functions/services.d.ts +15 -16
- package/dist/types/src/functions/services.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +1 -0
- package/dist/types/src/index.d.ts.map +1 -1
- 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 +8 -177
- package/dist/types/src/session/format.d.ts.map +1 -1
- package/dist/types/src/session/index.d.ts +2 -1
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/observer.d.ts +7 -6
- package/dist/types/src/session/observer.d.ts.map +1 -1
- package/dist/types/src/session/request.d.ts +104 -0
- package/dist/types/src/session/request.d.ts.map +1 -0
- package/dist/types/src/session/toolkit.d.ts +11 -9
- package/dist/types/src/session/toolkit.d.ts.map +1 -1
- package/dist/types/src/session/version-pin.d.ts +4 -4
- package/dist/types/src/session/version-pin.d.ts.map +1 -1
- package/dist/types/src/templates/system.d.ts +1 -2
- package/dist/types/src/templates/system.d.ts.map +1 -1
- package/dist/types/src/tracing.d.ts +233 -0
- package/dist/types/src/tracing.d.ts.map +1 -0
- package/dist/types/src/types/artifact.d.ts +28 -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 +35 -47
- package/src/conversation/context.test.ts +52 -0
- package/src/conversation/context.ts +324 -75
- package/src/conversation/index.ts +1 -1
- package/src/conversation/session.ts +334 -0
- package/src/diff.test.ts +37 -6
- package/src/diff.ts +18 -4
- package/src/errors.ts +2 -2
- package/src/execution-graph.test.ts +47 -45
- package/src/execution-graph.ts +41 -22
- package/src/extraction/extraction-ner-function/index.ts +0 -1
- package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +10 -5
- package/src/extraction/extraction.ts +44 -15
- package/src/extraction/index.ts +0 -1
- package/src/extraction/quotes.ts +5 -6
- package/src/functions/services.ts +126 -65
- package/src/functions.conversations.json +1 -0
- package/src/index.ts +1 -0
- package/src/session/artifact-diff.ts +2 -2
- package/src/session/error-handling.ts +4 -3
- package/src/session/format.ts +34 -26
- package/src/session/index.ts +2 -1
- package/src/session/observer.ts +8 -4
- package/src/session/request.conversations.json +1 -0
- package/src/session/request.ts +342 -0
- package/src/session/toolkit.ts +29 -28
- package/src/session/version-pin.ts +7 -7
- package/src/templates/{format.tpl → instructions/format.tpl} +1 -4
- package/src/templates/{system.tpl → instructions/system.tpl} +50 -2
- package/src/templates/system.ts +9 -8
- package/src/tracing.ts +57 -0
- package/src/{types.ts → types/artifact.ts} +44 -7
- package/src/types/index.ts +5 -0
- package/dist/lib/browser/chunk-ILL2CN3G.mjs +0 -374
- package/dist/lib/browser/chunk-ILL2CN3G.mjs.map +0 -7
- package/dist/lib/browser/chunk-UB26N5UF.mjs +0 -89
- package/dist/lib/browser/chunk-UB26N5UF.mjs.map +0 -7
- package/dist/lib/browser/extraction/index.mjs +0 -272
- package/dist/lib/browser/extraction/index.mjs.map +0 -7
- package/dist/lib/browser/index.mjs +0 -861
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs +0 -13
- package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs.map +0 -7
- 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-GVKA7VL3.mjs +0 -376
- package/dist/lib/node-esm/chunk-GVKA7VL3.mjs.map +0 -7
- package/dist/lib/node-esm/extraction/index.mjs +0 -273
- package/dist/lib/node-esm/extraction/index.mjs.map +0 -7
- package/dist/lib/node-esm/index.mjs +0 -862
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs +0 -14
- package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs.map +0 -7
- package/dist/types/src/conversation/conversation.d.ts +0 -41
- package/dist/types/src/conversation/conversation.d.ts.map +0 -1
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +0 -4
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +0 -1
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts +0 -2
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts.map +0 -1
- package/dist/types/src/extraction/extraction-llm-function/index.d.ts +0 -2
- package/dist/types/src/extraction/extraction-llm-function/index.d.ts.map +0 -1
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +0 -341
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +0 -1
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts +0 -2
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts.map +0 -1
- package/dist/types/src/session/format.test.d.ts +0 -2
- package/dist/types/src/session/format.test.d.ts.map +0 -1
- package/dist/types/src/session/session.d.ts +0 -47
- package/dist/types/src/session/session.d.ts.map +0 -1
- package/dist/types/src/session/session.test.d.ts +0 -2
- package/dist/types/src/session/session.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/conversation/conversation.ts +0 -119
- package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +0 -84
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +0 -72
- package/src/extraction/extraction-llm-function/index.ts +0 -5
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +0 -115
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +0 -64
- package/src/session/format.test.ts +0 -60
- package/src/session/session.conversations.json +0 -1
- package/src/session/session.test.ts +0 -146
- package/src/session/session.ts +0 -206
- package/src/summarization/summarization.test.ts +0 -201
- /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
|
@@ -1,862 +0,0 @@
|
|
|
1
|
-
import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
|
|
2
|
-
import {
|
|
3
|
-
AiAssistantError,
|
|
4
|
-
AiModelError,
|
|
5
|
-
AiSession,
|
|
6
|
-
ArtifactDiffResolver,
|
|
7
|
-
GenerationObserver,
|
|
8
|
-
VersionPin,
|
|
9
|
-
createToolkit
|
|
10
|
-
} from "./chunk-GVKA7VL3.mjs";
|
|
11
|
-
|
|
12
|
-
// src/conversation/context.ts
|
|
13
|
-
import { computed } from "@preact/signals-core";
|
|
14
|
-
import { Context, Schema } from "effect";
|
|
15
|
-
import { Array as Array2, pipe } from "effect";
|
|
16
|
-
import { Blueprint } from "@dxos/blueprints";
|
|
17
|
-
import { DXN, Obj, Type } from "@dxos/echo";
|
|
18
|
-
import { ComplexSet } from "@dxos/util";
|
|
19
|
-
function _define_property(obj, key, value) {
|
|
20
|
-
if (key in obj) {
|
|
21
|
-
Object.defineProperty(obj, key, {
|
|
22
|
-
value,
|
|
23
|
-
enumerable: true,
|
|
24
|
-
configurable: true,
|
|
25
|
-
writable: true
|
|
26
|
-
});
|
|
27
|
-
} else {
|
|
28
|
-
obj[key] = value;
|
|
29
|
-
}
|
|
30
|
-
return obj;
|
|
31
|
-
}
|
|
32
|
-
var ContextBinding = Schema.Struct({
|
|
33
|
-
blueprints: Schema.Struct({
|
|
34
|
-
added: Schema.Array(Type.Ref(Blueprint.Blueprint)),
|
|
35
|
-
removed: Schema.Array(Type.Ref(Blueprint.Blueprint))
|
|
36
|
-
}),
|
|
37
|
-
// TODO(burdon): Type.Expando => Type.Obj (or Obj.Any?)
|
|
38
|
-
objects: Schema.Struct({
|
|
39
|
-
added: Schema.Array(Type.Ref(Type.Expando)),
|
|
40
|
-
removed: Schema.Array(Type.Ref(Type.Expando))
|
|
41
|
-
})
|
|
42
|
-
}).pipe(Type.Obj({
|
|
43
|
-
typename: "dxos.org/type/ContextBinding",
|
|
44
|
-
version: "0.1.0"
|
|
45
|
-
}));
|
|
46
|
-
var Bindings = class {
|
|
47
|
-
toJSON() {
|
|
48
|
-
return {
|
|
49
|
-
blueprints: Array2.fromIterable(this.blueprints).map((ref) => ref.dxn.toString()),
|
|
50
|
-
objects: Array2.fromIterable(this.objects).map((ref) => ref.dxn.toString())
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
constructor() {
|
|
54
|
-
_define_property(this, "blueprints", new ComplexSet((ref) => ref.dxn.toString()));
|
|
55
|
-
_define_property(this, "objects", new ComplexSet((ref) => ref.dxn.asEchoDXN()?.echoId));
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
var AiContextBinder = class {
|
|
59
|
-
/**
|
|
60
|
-
* Asynchronous query of all bindings.
|
|
61
|
-
*/
|
|
62
|
-
async query() {
|
|
63
|
-
const queueItems = await this._queue.queryObjects();
|
|
64
|
-
return this._reduce(queueItems);
|
|
65
|
-
}
|
|
66
|
-
// TODO(burdon): Pass in Blueprint obj (from registry?) and create reference.
|
|
67
|
-
async bind(props) {
|
|
68
|
-
if (!props.blueprints?.length && !props.objects?.length) {
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
const blueprints = props.blueprints?.filter((ref) => !this.blueprints.peek().find((b) => b.dxn.toString() === ref.dxn.toString())) ?? [];
|
|
72
|
-
const objects = props.objects?.filter((ref) => !this.objects.peek().find((o) => o.dxn.toString() === ref.dxn.toString())) ?? [];
|
|
73
|
-
await this._queue.append([
|
|
74
|
-
Obj.make(ContextBinding, {
|
|
75
|
-
blueprints: {
|
|
76
|
-
added: blueprints,
|
|
77
|
-
removed: []
|
|
78
|
-
},
|
|
79
|
-
objects: {
|
|
80
|
-
added: objects,
|
|
81
|
-
removed: []
|
|
82
|
-
}
|
|
83
|
-
})
|
|
84
|
-
]);
|
|
85
|
-
}
|
|
86
|
-
async unbind(props) {
|
|
87
|
-
if (!props.blueprints?.length && !props.objects?.length) {
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
await this._queue.append([
|
|
91
|
-
Obj.make(ContextBinding, {
|
|
92
|
-
blueprints: {
|
|
93
|
-
added: [],
|
|
94
|
-
removed: props.blueprints ?? []
|
|
95
|
-
},
|
|
96
|
-
objects: {
|
|
97
|
-
added: [],
|
|
98
|
-
removed: props.objects ?? []
|
|
99
|
-
}
|
|
100
|
-
})
|
|
101
|
-
]);
|
|
102
|
-
}
|
|
103
|
-
_reduce(items) {
|
|
104
|
-
return pipe(items, Array2.filter(Obj.instanceOf(ContextBinding)), Array2.reduce(new Bindings(), (context, item) => {
|
|
105
|
-
item.blueprints.removed.forEach((ref) => context.blueprints.delete(ref));
|
|
106
|
-
item.blueprints.added.forEach((ref) => context.blueprints.add(ref));
|
|
107
|
-
item.objects.removed.forEach((ref) => {
|
|
108
|
-
for (const obj of context.objects) {
|
|
109
|
-
if (DXN.equalsEchoId(obj.dxn, ref.dxn)) {
|
|
110
|
-
context.objects.delete(obj);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
item.objects.added.forEach((ref) => context.objects.add(ref));
|
|
115
|
-
return context;
|
|
116
|
-
}));
|
|
117
|
-
}
|
|
118
|
-
constructor(_queue) {
|
|
119
|
-
_define_property(this, "_queue", void 0);
|
|
120
|
-
_define_property(this, "bindings", void 0);
|
|
121
|
-
_define_property(this, "blueprints", void 0);
|
|
122
|
-
_define_property(this, "objects", void 0);
|
|
123
|
-
this._queue = _queue;
|
|
124
|
-
this.bindings = computed(() => this._reduce(this._queue.objects));
|
|
125
|
-
this.blueprints = computed(() => [
|
|
126
|
-
...this.bindings.value.blueprints
|
|
127
|
-
]);
|
|
128
|
-
this.objects = computed(() => [
|
|
129
|
-
...this.bindings.value.objects
|
|
130
|
-
]);
|
|
131
|
-
}
|
|
132
|
-
};
|
|
133
|
-
var AiContextService = class extends Context.Tag("@dxos/assistant/AiContextService")() {
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
// src/conversation/conversation.ts
|
|
137
|
-
import { Array as Array3, Effect, Option } from "effect";
|
|
138
|
-
import { Obj as Obj2 } from "@dxos/echo";
|
|
139
|
-
import { DatabaseService } from "@dxos/functions";
|
|
140
|
-
import { log } from "@dxos/log";
|
|
141
|
-
import { DataType } from "@dxos/schema";
|
|
142
|
-
function _define_property2(obj, key, value) {
|
|
143
|
-
if (key in obj) {
|
|
144
|
-
Object.defineProperty(obj, key, {
|
|
145
|
-
value,
|
|
146
|
-
enumerable: true,
|
|
147
|
-
configurable: true,
|
|
148
|
-
writable: true
|
|
149
|
-
});
|
|
150
|
-
} else {
|
|
151
|
-
obj[key] = value;
|
|
152
|
-
}
|
|
153
|
-
return obj;
|
|
154
|
-
}
|
|
155
|
-
var __dxlog_file = "/__w/dxos/dxos/packages/core/assistant/src/conversation/conversation.ts";
|
|
156
|
-
var AiConversation = class {
|
|
157
|
-
get context() {
|
|
158
|
-
return this._context;
|
|
159
|
-
}
|
|
160
|
-
get toolkit() {
|
|
161
|
-
return this._toolkit;
|
|
162
|
-
}
|
|
163
|
-
async getHistory() {
|
|
164
|
-
const queueItems = await this._queue.queryObjects();
|
|
165
|
-
return queueItems.filter(Obj2.instanceOf(DataType.Message));
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* Creates a new cancelable request effect.
|
|
169
|
-
*/
|
|
170
|
-
createRequest(params) {
|
|
171
|
-
return Effect.gen(this, function* () {
|
|
172
|
-
const session = new AiSession();
|
|
173
|
-
const history = yield* Effect.promise(() => this.getHistory());
|
|
174
|
-
const context = yield* Effect.promise(() => this.context.query());
|
|
175
|
-
const blueprints = yield* Effect.forEach(context.blueprints.values(), DatabaseService.loadOption).pipe(Effect.map(Array3.filter(Option.isSome)), Effect.map(Array3.map((option) => option.value)));
|
|
176
|
-
const objects = yield* Effect.forEach(context.objects.values(), DatabaseService.loadOption).pipe(Effect.map(Array3.filter(Option.isSome)), Effect.map(Array3.map((option) => option.value)));
|
|
177
|
-
const toolkit = yield* createToolkit({
|
|
178
|
-
blueprints
|
|
179
|
-
});
|
|
180
|
-
this._toolkit = toolkit;
|
|
181
|
-
const start = Date.now();
|
|
182
|
-
log("run", {
|
|
183
|
-
history: history.length,
|
|
184
|
-
blueprints: blueprints.length,
|
|
185
|
-
objects: objects.length,
|
|
186
|
-
tools: this._toolkit?.tools.length ?? 0
|
|
187
|
-
}, {
|
|
188
|
-
F: __dxlog_file,
|
|
189
|
-
L: 98,
|
|
190
|
-
S: this,
|
|
191
|
-
C: (f, a) => f(...a)
|
|
192
|
-
});
|
|
193
|
-
const messages = yield* session.run({
|
|
194
|
-
history,
|
|
195
|
-
blueprints,
|
|
196
|
-
objects,
|
|
197
|
-
toolkit,
|
|
198
|
-
...params
|
|
199
|
-
}).pipe(Effect.provideService(AiContextService, {
|
|
200
|
-
binder: this.context
|
|
201
|
-
}));
|
|
202
|
-
log("result", {
|
|
203
|
-
messages: messages.length,
|
|
204
|
-
duration: Date.now() - start
|
|
205
|
-
}, {
|
|
206
|
-
F: __dxlog_file,
|
|
207
|
-
L: 112,
|
|
208
|
-
S: this,
|
|
209
|
-
C: (f, a) => f(...a)
|
|
210
|
-
});
|
|
211
|
-
yield* Effect.promise(() => this._queue.append(messages));
|
|
212
|
-
return messages;
|
|
213
|
-
}).pipe(Effect.withSpan("AiConversation.request"));
|
|
214
|
-
}
|
|
215
|
-
constructor(options) {
|
|
216
|
-
_define_property2(this, "_queue", void 0);
|
|
217
|
-
_define_property2(this, "_context", void 0);
|
|
218
|
-
_define_property2(this, "_toolkit", void 0);
|
|
219
|
-
this._queue = options.queue;
|
|
220
|
-
this._context = new AiContextBinder(this._queue);
|
|
221
|
-
}
|
|
222
|
-
};
|
|
223
|
-
|
|
224
|
-
// src/diff.ts
|
|
225
|
-
import { next as A } from "@automerge/automerge";
|
|
226
|
-
import { DocAccessor, toCursorRange } from "@dxos/echo-db";
|
|
227
|
-
import { log as log2 } from "@dxos/log";
|
|
228
|
-
import { isNonNullable } from "@dxos/util";
|
|
229
|
-
var __dxlog_file2 = "/__w/dxos/dxos/packages/core/assistant/src/diff.ts";
|
|
230
|
-
var reduceDiffs = (diffs) => {
|
|
231
|
-
return diffs.reduce((acc, diff) => {
|
|
232
|
-
const current = acc[acc.length - 1];
|
|
233
|
-
const [op, ...rest] = diff.split(" ");
|
|
234
|
-
switch (op) {
|
|
235
|
-
case "-": {
|
|
236
|
-
if (current && !current.match) {
|
|
237
|
-
current.match += rest.join(" ");
|
|
238
|
-
} else {
|
|
239
|
-
acc.push({
|
|
240
|
-
match: rest.join(" "),
|
|
241
|
-
replace: ""
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
return acc;
|
|
245
|
-
}
|
|
246
|
-
case "+": {
|
|
247
|
-
if (current && !current.replace) {
|
|
248
|
-
current.replace += rest.join(" ");
|
|
249
|
-
} else {
|
|
250
|
-
acc.push({
|
|
251
|
-
match: "",
|
|
252
|
-
replace: rest.join(" ")
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
return acc;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
return acc;
|
|
259
|
-
}, []);
|
|
260
|
-
};
|
|
261
|
-
var computeDiffsWithCursors = (accessor, diffs) => {
|
|
262
|
-
return reduceDiffs(diffs).map((diff) => {
|
|
263
|
-
const text = DocAccessor.getValue(accessor);
|
|
264
|
-
const idx = text.indexOf(diff.match);
|
|
265
|
-
if (idx !== -1) {
|
|
266
|
-
return {
|
|
267
|
-
cursor: toCursorRange(accessor, idx, idx + diff.match.length),
|
|
268
|
-
text: diff.replace
|
|
269
|
-
};
|
|
270
|
-
}
|
|
271
|
-
}).filter(isNonNullable);
|
|
272
|
-
};
|
|
273
|
-
var applyDiffs = (accessor, diffs) => {
|
|
274
|
-
for (const diff of reduceDiffs(diffs)) {
|
|
275
|
-
accessor.handle.change((doc) => {
|
|
276
|
-
const text = DocAccessor.getValue(accessor);
|
|
277
|
-
const idx = text.indexOf(diff.match);
|
|
278
|
-
if (idx !== -1) {
|
|
279
|
-
A.splice(doc, accessor.path, idx, diff.match.length, diff.replace);
|
|
280
|
-
} else {
|
|
281
|
-
log2.warn("diff not found", diff, {
|
|
282
|
-
F: __dxlog_file2,
|
|
283
|
-
L: 73,
|
|
284
|
-
S: void 0,
|
|
285
|
-
C: (f, a) => f(...a)
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
|
-
});
|
|
289
|
-
}
|
|
290
|
-
return DocAccessor.getValue(accessor);
|
|
291
|
-
};
|
|
292
|
-
|
|
293
|
-
// src/execution-graph.ts
|
|
294
|
-
import { AgentStatus } from "@dxos/ai";
|
|
295
|
-
import { Obj as Obj3 } from "@dxos/echo";
|
|
296
|
-
import { MESSAGE_PROPERTY_TOOL_CALL_ID } from "@dxos/functions";
|
|
297
|
-
import { LogLevel } from "@dxos/log";
|
|
298
|
-
import { ContentBlock, DataType as DataType2 } from "@dxos/schema";
|
|
299
|
-
import { isNonNullable as isNonNullable2 } from "@dxos/util";
|
|
300
|
-
function _define_property3(obj, key, value) {
|
|
301
|
-
if (key in obj) {
|
|
302
|
-
Object.defineProperty(obj, key, {
|
|
303
|
-
value,
|
|
304
|
-
enumerable: true,
|
|
305
|
-
configurable: true,
|
|
306
|
-
writable: true
|
|
307
|
-
});
|
|
308
|
-
} else {
|
|
309
|
-
obj[key] = value;
|
|
310
|
-
}
|
|
311
|
-
return obj;
|
|
312
|
-
}
|
|
313
|
-
var ExecutionGraph = class {
|
|
314
|
-
/**
|
|
315
|
-
* Adds events to the graph.
|
|
316
|
-
*/
|
|
317
|
-
addEvents(events) {
|
|
318
|
-
const sortedEvents = this.sortObjectsByCreated(events);
|
|
319
|
-
for (const event of sortedEvents) {
|
|
320
|
-
if (Obj3.instanceOf(DataType2.Message, event)) {
|
|
321
|
-
this._processMessage(event);
|
|
322
|
-
} else if (Obj3.instanceOf(AgentStatus, event)) {
|
|
323
|
-
this._processAgentStatus(event);
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
/**
|
|
328
|
-
* Processes a message event and creates commits for its blocks.
|
|
329
|
-
*/
|
|
330
|
-
_processMessage(message) {
|
|
331
|
-
const messageCommits = messageToCommits(message, this._lastBlockId, this._toolCallCommitIds, this._lastCommitByBranch);
|
|
332
|
-
this._commits.push(...messageCommits);
|
|
333
|
-
messageCommits.forEach((commit) => this._branchNames.add(commit.branch));
|
|
334
|
-
this._trackToolCalls(messageCommits);
|
|
335
|
-
if (messageCommits.length > 0) {
|
|
336
|
-
this._lastBlockId = messageCommits[messageCommits.length - 1].id;
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
/**
|
|
340
|
-
* Processes an AgentStatus event.
|
|
341
|
-
*/
|
|
342
|
-
_processAgentStatus(event) {
|
|
343
|
-
const branch = this._getToolCallBranch(event.parentMessage, event.toolCallId);
|
|
344
|
-
this._branchNames.add(branch);
|
|
345
|
-
const agentStatusCommit = {
|
|
346
|
-
id: event.id,
|
|
347
|
-
branch,
|
|
348
|
-
message: event.message,
|
|
349
|
-
icon: "ph--flag--regular",
|
|
350
|
-
parents: this._getAgentStatusParents(event),
|
|
351
|
-
timestamp: new Date(event.created)
|
|
352
|
-
};
|
|
353
|
-
this._commits.push(agentStatusCommit);
|
|
354
|
-
this._lastBlockId = event.id;
|
|
355
|
-
if (event.toolCallId) {
|
|
356
|
-
this._lastCommitByBranch.set(branch, event.id);
|
|
357
|
-
this._updatePendingToolResults(event.toolCallId, event.id);
|
|
358
|
-
}
|
|
359
|
-
}
|
|
360
|
-
/**
|
|
361
|
-
* Stable sort: objects with 'created' field are sorted by it, others remain in place.
|
|
362
|
-
*/
|
|
363
|
-
sortObjectsByCreated(objects) {
|
|
364
|
-
const sortedObjects = objects.slice();
|
|
365
|
-
const createdObjects = [];
|
|
366
|
-
sortedObjects.forEach((obj, idx) => {
|
|
367
|
-
if (obj && typeof obj.created === "string") {
|
|
368
|
-
createdObjects.push({
|
|
369
|
-
idx,
|
|
370
|
-
obj
|
|
371
|
-
});
|
|
372
|
-
}
|
|
373
|
-
});
|
|
374
|
-
createdObjects.sort((a, b) => {
|
|
375
|
-
const aDate = new Date(a.obj.created).getTime();
|
|
376
|
-
const bDate = new Date(b.obj.created).getTime();
|
|
377
|
-
return aDate - bDate;
|
|
378
|
-
});
|
|
379
|
-
let createdIdx = 0;
|
|
380
|
-
return sortedObjects.map((obj, _idx) => {
|
|
381
|
-
if (obj && typeof obj.created === "string") {
|
|
382
|
-
return createdObjects[createdIdx++].obj;
|
|
383
|
-
}
|
|
384
|
-
return obj;
|
|
385
|
-
});
|
|
386
|
-
}
|
|
387
|
-
/**
|
|
388
|
-
* Returns the current state of the graph.
|
|
389
|
-
*/
|
|
390
|
-
getGraph(lastRequest = false) {
|
|
391
|
-
const idx = lastRequest ? this._commits.findLastIndex((c) => c.tags?.includes("user")) : -1;
|
|
392
|
-
const commits = idx === -1 ? this._commits : this._commits.slice(idx);
|
|
393
|
-
return {
|
|
394
|
-
branches: Array.from(this._branchNames),
|
|
395
|
-
commits
|
|
396
|
-
};
|
|
397
|
-
}
|
|
398
|
-
/**
|
|
399
|
-
* Tracks tool calls and handles pending tool result updates.
|
|
400
|
-
*/
|
|
401
|
-
_trackToolCalls(commits) {
|
|
402
|
-
commits.forEach((commit) => {
|
|
403
|
-
if (this._isToolCallCommit(commit)) {
|
|
404
|
-
const toolCallId = this._extractToolCallId(commit.id);
|
|
405
|
-
if (toolCallId) {
|
|
406
|
-
this._toolCallCommitIds.set(toolCallId, commit.id);
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
if (this._isToolResultCommit(commit)) {
|
|
410
|
-
const toolCallId = this._extractToolCallId(commit.id);
|
|
411
|
-
if (toolCallId) {
|
|
412
|
-
this._pendingToolResults.set(toolCallId, commit.id);
|
|
413
|
-
this._handleLateToolResultUpdate(toolCallId, commit.id);
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
this._lastCommitByBranch.set(commit.branch, commit.id);
|
|
417
|
-
});
|
|
418
|
-
}
|
|
419
|
-
/**
|
|
420
|
-
* Updates pending tool results to point to an AgentStatus event.
|
|
421
|
-
*/
|
|
422
|
-
_updatePendingToolResults(toolCallId, agentStatusId) {
|
|
423
|
-
const toolResultId = this._pendingToolResults.get(toolCallId);
|
|
424
|
-
if (!toolResultId) return;
|
|
425
|
-
const toolResultCommit = this._commits.find((c) => c.id === toolResultId);
|
|
426
|
-
if (!toolResultCommit) return;
|
|
427
|
-
const toolCallCommitId = this._toolCallCommitIds.get(toolCallId);
|
|
428
|
-
if (!toolCallCommitId) return;
|
|
429
|
-
const toolCallIndex = toolResultCommit.parents.indexOf(toolCallCommitId);
|
|
430
|
-
if (toolCallIndex !== -1) {
|
|
431
|
-
toolResultCommit.parents[toolCallIndex] = agentStatusId;
|
|
432
|
-
}
|
|
433
|
-
this._pendingToolResults.delete(toolCallId);
|
|
434
|
-
}
|
|
435
|
-
/**
|
|
436
|
-
* Handles late updates when AgentStatus is processed before tool result.
|
|
437
|
-
*/
|
|
438
|
-
_handleLateToolResultUpdate(toolCallId, toolResultId) {
|
|
439
|
-
const toolCallCommitId = this._toolCallCommitIds.get(toolCallId);
|
|
440
|
-
if (!toolCallCommitId) return;
|
|
441
|
-
const messageId = toolCallCommitId.split("_toolCall_")[0];
|
|
442
|
-
const toolCallBranch = `${messageId}_${toolCallId}`;
|
|
443
|
-
const agentStatusCommitId = this._lastCommitByBranch.get(toolCallBranch);
|
|
444
|
-
if (agentStatusCommitId && agentStatusCommitId !== toolCallCommitId) {
|
|
445
|
-
const toolResultCommit = this._commits.find((c) => c.id === toolResultId);
|
|
446
|
-
if (toolResultCommit) {
|
|
447
|
-
const toolCallIndex = toolResultCommit.parents.indexOf(toolCallCommitId);
|
|
448
|
-
if (toolCallIndex !== -1) {
|
|
449
|
-
toolResultCommit.parents[toolCallIndex] = agentStatusCommitId;
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
this._pendingToolResults.delete(toolCallId);
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
/**
|
|
456
|
-
* Gets the branch name for a tool call.
|
|
457
|
-
*/
|
|
458
|
-
_getToolCallBranch(parentMessage, toolCallId) {
|
|
459
|
-
if (toolCallId) {
|
|
460
|
-
return toolCallId;
|
|
461
|
-
}
|
|
462
|
-
return parentMessage || "main";
|
|
463
|
-
}
|
|
464
|
-
/**
|
|
465
|
-
* Gets the parents for an AgentStatus commit.
|
|
466
|
-
*/
|
|
467
|
-
_getAgentStatusParents(event) {
|
|
468
|
-
if (event.parentMessage && event.toolCallId) {
|
|
469
|
-
return [
|
|
470
|
-
getToolCallId(event.parentMessage, event.toolCallId)
|
|
471
|
-
];
|
|
472
|
-
}
|
|
473
|
-
return [];
|
|
474
|
-
}
|
|
475
|
-
/**
|
|
476
|
-
* Checks if a commit is a tool call commit.
|
|
477
|
-
*/
|
|
478
|
-
_isToolCallCommit(commit) {
|
|
479
|
-
return commit.message.includes("Calling tool");
|
|
480
|
-
}
|
|
481
|
-
/**
|
|
482
|
-
* Checks if a commit is a tool result commit.
|
|
483
|
-
*/
|
|
484
|
-
_isToolResultCommit(commit) {
|
|
485
|
-
return commit.message.includes("Tool call succeeded") || commit.message.includes("Tool error:");
|
|
486
|
-
}
|
|
487
|
-
/**
|
|
488
|
-
* Extracts tool call ID from a commit ID.
|
|
489
|
-
*/
|
|
490
|
-
_extractToolCallId(commitId) {
|
|
491
|
-
const toolCallMatch = commitId.match(/_toolCall_(.+)$/);
|
|
492
|
-
const toolResultMatch = commitId.match(/_toolResult_(.+)$/);
|
|
493
|
-
return toolCallMatch?.[1] || toolResultMatch?.[1];
|
|
494
|
-
}
|
|
495
|
-
constructor() {
|
|
496
|
-
_define_property3(this, "_commits", []);
|
|
497
|
-
_define_property3(this, "_branchNames", /* @__PURE__ */ new Set());
|
|
498
|
-
_define_property3(this, "_lastBlockId", void 0);
|
|
499
|
-
_define_property3(this, "_toolCallCommitIds", /* @__PURE__ */ new Map());
|
|
500
|
-
_define_property3(this, "_lastCommitByBranch", /* @__PURE__ */ new Map());
|
|
501
|
-
_define_property3(this, "_pendingToolResults", /* @__PURE__ */ new Map());
|
|
502
|
-
}
|
|
503
|
-
};
|
|
504
|
-
var messageToCommits = (message, lastBlockId, toolCallIds, lastCommitByBranch) => {
|
|
505
|
-
let previousBlockId = lastBlockId;
|
|
506
|
-
return message.blocks.map((block, idx) => {
|
|
507
|
-
const branch = getMessageBranch(message);
|
|
508
|
-
const parents = getBlockParents(block, previousBlockId, message, toolCallIds, lastCommitByBranch);
|
|
509
|
-
const commit = createBlockCommit(block, message, branch, parents, idx);
|
|
510
|
-
if (commit) {
|
|
511
|
-
previousBlockId = commit.id;
|
|
512
|
-
}
|
|
513
|
-
return commit;
|
|
514
|
-
}).filter(isNonNullable2);
|
|
515
|
-
};
|
|
516
|
-
var getBlockParents = (block, previousBlockId, message, toolCallIds, lastCommitByBranch) => {
|
|
517
|
-
const parents = [];
|
|
518
|
-
if (block._tag === "toolResult") {
|
|
519
|
-
if (previousBlockId) {
|
|
520
|
-
parents.push(previousBlockId);
|
|
521
|
-
}
|
|
522
|
-
const toolCallCommitId = toolCallIds?.get(block.toolCallId);
|
|
523
|
-
if (toolCallCommitId) {
|
|
524
|
-
const toolCallBranch = getToolCallBranchFromCommitId(toolCallCommitId, block.toolCallId);
|
|
525
|
-
const lastBlockFromToolCallBranch = lastCommitByBranch?.get(toolCallBranch);
|
|
526
|
-
if (lastBlockFromToolCallBranch) {
|
|
527
|
-
parents.push(lastBlockFromToolCallBranch);
|
|
528
|
-
} else {
|
|
529
|
-
parents.push(toolCallCommitId);
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
} else {
|
|
533
|
-
if (previousBlockId) {
|
|
534
|
-
parents.push(previousBlockId);
|
|
535
|
-
} else {
|
|
536
|
-
const toolCallParent = getParentId(message);
|
|
537
|
-
if (toolCallParent) {
|
|
538
|
-
parents.push(toolCallParent);
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
}
|
|
542
|
-
return parents;
|
|
543
|
-
};
|
|
544
|
-
var createBlockCommit = (block, message, branch, parents, idx) => {
|
|
545
|
-
const timestamp = new Date(message.created);
|
|
546
|
-
switch (block._tag) {
|
|
547
|
-
case "text":
|
|
548
|
-
if (!block.text.trim().length) return null;
|
|
549
|
-
return {
|
|
550
|
-
id: getGenericBlockId(message.id, idx),
|
|
551
|
-
branch,
|
|
552
|
-
parents,
|
|
553
|
-
timestamp,
|
|
554
|
-
...message.sender.role === "user" ? {
|
|
555
|
-
icon: "ph--user--regular",
|
|
556
|
-
tags: [
|
|
557
|
-
"user"
|
|
558
|
-
],
|
|
559
|
-
message: "Processing request..."
|
|
560
|
-
} : {
|
|
561
|
-
icon: "ph--robot--regular",
|
|
562
|
-
message: `Response (${block.text.split(" ").length} words)`
|
|
563
|
-
}
|
|
564
|
-
};
|
|
565
|
-
case "toolCall":
|
|
566
|
-
return {
|
|
567
|
-
id: getToolCallId(message.id, block.toolCallId),
|
|
568
|
-
branch,
|
|
569
|
-
parents,
|
|
570
|
-
timestamp,
|
|
571
|
-
icon: "ph--wrench--regular",
|
|
572
|
-
level: LogLevel.INFO,
|
|
573
|
-
message: `Calling tool (${block.name})`
|
|
574
|
-
};
|
|
575
|
-
case "toolResult":
|
|
576
|
-
return {
|
|
577
|
-
id: getToolResultId(message.id, block.toolCallId),
|
|
578
|
-
branch,
|
|
579
|
-
parents,
|
|
580
|
-
timestamp,
|
|
581
|
-
icon: block.error ? "ph--x-circle--regular" : "ph--check-circle--regular",
|
|
582
|
-
level: block.error ? LogLevel.ERROR : LogLevel.INFO,
|
|
583
|
-
message: block.error ? "Tool error: " + block.error : "Tool call succeeded"
|
|
584
|
-
};
|
|
585
|
-
case "summary":
|
|
586
|
-
return {
|
|
587
|
-
id: getGenericBlockId(message.id, idx),
|
|
588
|
-
branch,
|
|
589
|
-
parents,
|
|
590
|
-
timestamp,
|
|
591
|
-
icon: "ph--rocket--regular",
|
|
592
|
-
level: LogLevel.INFO,
|
|
593
|
-
message: ContentBlock.createSummaryMessage(block)
|
|
594
|
-
};
|
|
595
|
-
case "status":
|
|
596
|
-
return {
|
|
597
|
-
id: getGenericBlockId(message.id, idx),
|
|
598
|
-
branch,
|
|
599
|
-
parents,
|
|
600
|
-
timestamp,
|
|
601
|
-
message: block.statusText,
|
|
602
|
-
level: LogLevel.INFO,
|
|
603
|
-
icon: "ph--flag--regular"
|
|
604
|
-
};
|
|
605
|
-
case "reasoning":
|
|
606
|
-
return {
|
|
607
|
-
id: getGenericBlockId(message.id, idx),
|
|
608
|
-
branch,
|
|
609
|
-
parents,
|
|
610
|
-
timestamp,
|
|
611
|
-
message: block.reasoningText ?? "Thinking...",
|
|
612
|
-
icon: "ph--brain--regular"
|
|
613
|
-
};
|
|
614
|
-
case "reference":
|
|
615
|
-
return {
|
|
616
|
-
id: getGenericBlockId(message.id, idx),
|
|
617
|
-
branch,
|
|
618
|
-
parents,
|
|
619
|
-
timestamp,
|
|
620
|
-
icon: "ph--link--regular",
|
|
621
|
-
message: stringifyRef(block.reference)
|
|
622
|
-
};
|
|
623
|
-
default:
|
|
624
|
-
return null;
|
|
625
|
-
}
|
|
626
|
-
};
|
|
627
|
-
var getToolCallBranchFromCommitId = (toolCallCommitId, toolCallId) => {
|
|
628
|
-
const messageId = toolCallCommitId.split("_toolCall_")[0];
|
|
629
|
-
return `${messageId}_${toolCallId}`;
|
|
630
|
-
};
|
|
631
|
-
var getToolCallId = (messageId, toolCallId) => `${messageId}_toolCall_${toolCallId}`;
|
|
632
|
-
var getToolResultId = (messageId, toolCallId) => `${messageId}_toolResult_${toolCallId}`;
|
|
633
|
-
var getGenericBlockId = (messageId, idx) => `${messageId}_block_${idx}`;
|
|
634
|
-
var getBranchName = (options) => {
|
|
635
|
-
if (options.toolCallId) {
|
|
636
|
-
return options.toolCallId;
|
|
637
|
-
} else if (options.parentMessage) {
|
|
638
|
-
return options.parentMessage;
|
|
639
|
-
} else {
|
|
640
|
-
return "main";
|
|
641
|
-
}
|
|
642
|
-
};
|
|
643
|
-
var getMessageBranch = (message) => {
|
|
644
|
-
return getBranchName({
|
|
645
|
-
parentMessage: message.parentMessage,
|
|
646
|
-
toolCallId: message.properties?.[MESSAGE_PROPERTY_TOOL_CALL_ID]
|
|
647
|
-
});
|
|
648
|
-
};
|
|
649
|
-
var getParentId = (message) => {
|
|
650
|
-
if (message.parentMessage && message.properties?.[MESSAGE_PROPERTY_TOOL_CALL_ID]) {
|
|
651
|
-
return getToolCallId(message.parentMessage, message.properties[MESSAGE_PROPERTY_TOOL_CALL_ID]);
|
|
652
|
-
} else {
|
|
653
|
-
return void 0;
|
|
654
|
-
}
|
|
655
|
-
};
|
|
656
|
-
var stringifyRef = (ref) => {
|
|
657
|
-
if (ref.target) {
|
|
658
|
-
return stringifyObject(ref.target);
|
|
659
|
-
}
|
|
660
|
-
return ref.dxn.asEchoDXN()?.echoId ?? ref.dxn.asQueueDXN()?.objectId ?? "";
|
|
661
|
-
};
|
|
662
|
-
var stringifyObject = (obj) => {
|
|
663
|
-
return Obj3.getLabel(obj) ?? Obj3.getTypename(obj) ?? obj.id;
|
|
664
|
-
};
|
|
665
|
-
|
|
666
|
-
// src/functions/services.ts
|
|
667
|
-
import { Tool } from "@effect/ai";
|
|
668
|
-
import { Context as Context2, Effect as Effect2, Layer, Record, Schema as Schema2 } from "effect";
|
|
669
|
-
import { AiToolNotFoundError, ToolExecutionService, ToolResolverService } from "@dxos/ai";
|
|
670
|
-
import { todo } from "@dxos/debug";
|
|
671
|
-
import { Query } from "@dxos/echo";
|
|
672
|
-
import { DatabaseService as DatabaseService2, FunctionDefinition, FunctionInvocationService, FunctionType } from "@dxos/functions";
|
|
673
|
-
import { invariant } from "@dxos/invariant";
|
|
674
|
-
var __dxlog_file3 = "/__w/dxos/dxos/packages/core/assistant/src/functions/services.ts";
|
|
675
|
-
var makeToolResolverFromFunctions = (functions, toolkit) => {
|
|
676
|
-
return Layer.effect(ToolResolverService, Effect2.gen(function* () {
|
|
677
|
-
const dbService = yield* DatabaseService2;
|
|
678
|
-
return {
|
|
679
|
-
resolve: (id) => Effect2.gen(function* () {
|
|
680
|
-
const tool = toolkit.tools[id];
|
|
681
|
-
if (tool) {
|
|
682
|
-
return tool;
|
|
683
|
-
}
|
|
684
|
-
const { objects: [dbFunction] } = yield* DatabaseService2.runQuery(Query.type(FunctionType, {
|
|
685
|
-
key: id
|
|
686
|
-
}));
|
|
687
|
-
const functionDef = dbFunction ? FunctionDefinition.deserialize(dbFunction) : functions.find((fn) => fn.key === id);
|
|
688
|
-
if (!functionDef) {
|
|
689
|
-
return yield* Effect2.fail(new AiToolNotFoundError(id));
|
|
690
|
-
}
|
|
691
|
-
return projectFunctionToTool(functionDef);
|
|
692
|
-
}).pipe(Effect2.provideService(DatabaseService2, dbService))
|
|
693
|
-
};
|
|
694
|
-
}));
|
|
695
|
-
};
|
|
696
|
-
var makeToolExecutionServiceFromFunctions = (toolkit, handlersLayer) => {
|
|
697
|
-
return Layer.effect(ToolExecutionService, Effect2.gen(function* () {
|
|
698
|
-
const toolkitHandler = yield* toolkit.pipe(Effect2.provide(handlersLayer));
|
|
699
|
-
invariant(isHandlerLike(toolkitHandler), void 0, {
|
|
700
|
-
F: __dxlog_file3,
|
|
701
|
-
L: 68,
|
|
702
|
-
S: this,
|
|
703
|
-
A: [
|
|
704
|
-
"isHandlerLike(toolkitHandler)",
|
|
705
|
-
""
|
|
706
|
-
]
|
|
707
|
-
});
|
|
708
|
-
const functionInvocationService = yield* FunctionInvocationService;
|
|
709
|
-
return {
|
|
710
|
-
handlersFor: (toolkit2) => {
|
|
711
|
-
const makeHandler = (tool) => {
|
|
712
|
-
return Effect2.fn(`toolFunctionHandler ${tool.name}`)(function* (input) {
|
|
713
|
-
if (toolkitHandler.tools[tool.name]) {
|
|
714
|
-
if (Tool.isProviderDefined(tool)) {
|
|
715
|
-
throw new Error("Attempted to call a provider-defined tool");
|
|
716
|
-
}
|
|
717
|
-
return yield* toolkitHandler.handle(tool.name, input);
|
|
718
|
-
}
|
|
719
|
-
const { definition: functionDef } = Context2.get(FunctionToolAnnotation)(tool.annotations);
|
|
720
|
-
return yield* functionInvocationService.invokeFunction(functionDef, input).pipe(Effect2.catchAllDefect((defect) => Effect2.fail(defect)));
|
|
721
|
-
});
|
|
722
|
-
};
|
|
723
|
-
return toolkit2.of(Record.map(toolkit2.tools, (tool, _name) => Tool.isUserDefined(tool) ? makeHandler(tool) : null));
|
|
724
|
-
}
|
|
725
|
-
};
|
|
726
|
-
}));
|
|
727
|
-
};
|
|
728
|
-
var FunctionToolAnnotation = class extends Context2.Tag("@dxos/assistant/FunctionToolAnnotation")() {
|
|
729
|
-
};
|
|
730
|
-
var toolCache = /* @__PURE__ */ new WeakMap();
|
|
731
|
-
var projectFunctionToTool = (fn) => {
|
|
732
|
-
if (toolCache.has(fn)) {
|
|
733
|
-
return toolCache.get(fn);
|
|
734
|
-
}
|
|
735
|
-
const tool = Tool.make(makeToolName(fn.name), {
|
|
736
|
-
description: fn.description,
|
|
737
|
-
parameters: createStructFieldsFromSchema(fn.inputSchema),
|
|
738
|
-
// TODO(dmaretskyi): Include output schema.
|
|
739
|
-
failure: Schema2.Any
|
|
740
|
-
}).annotate(FunctionToolAnnotation, {
|
|
741
|
-
definition: fn
|
|
742
|
-
});
|
|
743
|
-
toolCache.set(fn, tool);
|
|
744
|
-
return tool;
|
|
745
|
-
};
|
|
746
|
-
var makeToolName = (name) => {
|
|
747
|
-
const toolName = name.replace(/[^a-zA-Z0-9]/g, "_");
|
|
748
|
-
invariant(toolName.match(/^[a-zA-Z_][a-zA-Z0-9-_]*$/), void 0, {
|
|
749
|
-
F: __dxlog_file3,
|
|
750
|
-
L: 135,
|
|
751
|
-
S: void 0,
|
|
752
|
-
A: [
|
|
753
|
-
"toolName.match(/^[a-zA-Z_][a-zA-Z0-9-_]*$/)",
|
|
754
|
-
""
|
|
755
|
-
]
|
|
756
|
-
});
|
|
757
|
-
return toolName;
|
|
758
|
-
};
|
|
759
|
-
var createStructFieldsFromSchema = (schema) => {
|
|
760
|
-
switch (schema.ast._tag) {
|
|
761
|
-
case "TypeLiteral":
|
|
762
|
-
return Object.fromEntries(schema.ast.propertySignatures.map((prop) => [
|
|
763
|
-
prop.name,
|
|
764
|
-
Schema2.make(prop.type)
|
|
765
|
-
]));
|
|
766
|
-
case "VoidKeyword":
|
|
767
|
-
return {};
|
|
768
|
-
default:
|
|
769
|
-
return todo(`Unsupported schema AST: ${schema.ast._tag}`);
|
|
770
|
-
}
|
|
771
|
-
};
|
|
772
|
-
var isHandlerLike = (value) => {
|
|
773
|
-
return typeof value.tools === "object" && typeof value.handle === "function";
|
|
774
|
-
};
|
|
775
|
-
|
|
776
|
-
// src/templates/system.ts
|
|
777
|
-
import defaultsDeep from "lodash.defaultsdeep";
|
|
778
|
-
import { Template } from "@dxos/blueprints";
|
|
779
|
-
|
|
780
|
-
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/format.tpl?raw
|
|
781
|
-
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";
|
|
782
|
-
|
|
783
|
-
// raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/system.tpl?raw
|
|
784
|
-
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";
|
|
785
|
-
|
|
786
|
-
// src/templates/system.ts
|
|
787
|
-
var createSystemPrompt = (options = {}) => Template.process(format_default, defaultsDeep(options, {
|
|
788
|
-
DATETIME: (/* @__PURE__ */ new Date()).toISOString()
|
|
789
|
-
}));
|
|
790
|
-
var templates = {
|
|
791
|
-
/** Editable system prompt.*/
|
|
792
|
-
system: Template.make({
|
|
793
|
-
/**
|
|
794
|
-
* NOTE: We are forbidden from accessing randomness at model level in cloudflare, so we have to have pre-generated ids.
|
|
795
|
-
*/
|
|
796
|
-
id: "01K2J2FZWH39F55CS7B26GP1Q3",
|
|
797
|
-
source: system_default
|
|
798
|
-
})
|
|
799
|
-
};
|
|
800
|
-
|
|
801
|
-
// src/types.ts
|
|
802
|
-
import { Schema as Schema3 } from "effect";
|
|
803
|
-
import { DXN as DXN2, LOCAL_SPACE_TAG } from "@dxos/keys";
|
|
804
|
-
import { trim } from "@dxos/util";
|
|
805
|
-
var createArtifactElement = (id) => `<artifact id=${id} />`;
|
|
806
|
-
var ArtifactId = class extends Schema3.String.annotations({
|
|
807
|
-
description: trim`
|
|
808
|
-
The ID of the referenced object. Formats accepted:
|
|
809
|
-
- DXN (dxn:echo:@:XXXXX). DXNs can be prepended with an @ symbol for compatibility with in-text references.
|
|
810
|
-
- space ID, object ID tuple (spaceID:objectID)
|
|
811
|
-
- Only object ID that is assumed to be in the current space (XXXXX)
|
|
812
|
-
`,
|
|
813
|
-
examples: [
|
|
814
|
-
"dxn:echo:@:XXXXX",
|
|
815
|
-
"@dxn:echo:@:XXXXX",
|
|
816
|
-
"spaceID:objectID",
|
|
817
|
-
"XXXXX"
|
|
818
|
-
]
|
|
819
|
-
}) {
|
|
820
|
-
static toDXN(reference, owningSpaceId) {
|
|
821
|
-
if (reference.startsWith("@dxn:")) {
|
|
822
|
-
return DXN2.parse(reference.slice(1));
|
|
823
|
-
} else if (reference.startsWith("dxn:")) {
|
|
824
|
-
return DXN2.parse(reference);
|
|
825
|
-
} else if (/^[A-Z0-9]+:[A-Z0-9]+$/.test(reference)) {
|
|
826
|
-
const [spaceId, objectId] = reference.split(":");
|
|
827
|
-
return new DXN2(DXN2.kind.ECHO, [
|
|
828
|
-
spaceId === owningSpaceId ? LOCAL_SPACE_TAG : spaceId,
|
|
829
|
-
objectId
|
|
830
|
-
]);
|
|
831
|
-
} else if (/^[A-Z0-9]+$/.test(reference)) {
|
|
832
|
-
return DXN2.fromLocalObjectId(reference);
|
|
833
|
-
} else {
|
|
834
|
-
throw new Error(`Unable to parse object reference: ${reference}`);
|
|
835
|
-
}
|
|
836
|
-
}
|
|
837
|
-
};
|
|
838
|
-
export {
|
|
839
|
-
AiAssistantError,
|
|
840
|
-
AiContextBinder,
|
|
841
|
-
AiContextService,
|
|
842
|
-
AiConversation,
|
|
843
|
-
AiModelError,
|
|
844
|
-
AiSession,
|
|
845
|
-
ArtifactDiffResolver,
|
|
846
|
-
ArtifactId,
|
|
847
|
-
Bindings,
|
|
848
|
-
ContextBinding,
|
|
849
|
-
ExecutionGraph,
|
|
850
|
-
GenerationObserver,
|
|
851
|
-
VersionPin,
|
|
852
|
-
applyDiffs,
|
|
853
|
-
computeDiffsWithCursors,
|
|
854
|
-
createArtifactElement,
|
|
855
|
-
createSystemPrompt,
|
|
856
|
-
createToolkit,
|
|
857
|
-
makeToolExecutionServiceFromFunctions,
|
|
858
|
-
makeToolResolverFromFunctions,
|
|
859
|
-
reduceDiffs,
|
|
860
|
-
templates
|
|
861
|
-
};
|
|
862
|
-
//# sourceMappingURL=index.mjs.map
|