@dxos/assistant 0.8.2-main.12df754 → 0.8.2-main.36232bc
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/index.mjs +270 -150
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +7 -7
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node/index.cjs +266 -149
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +8 -8
- package/dist/lib/node/testing/index.cjs.map +3 -3
- package/dist/lib/node-esm/index.mjs +270 -150
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +7 -7
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- 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/session.d.ts +29 -4
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/session/version-pin.d.ts +17 -0
- package/dist/types/src/session/version-pin.d.ts.map +1 -0
- package/dist/types/src/session/version-pin.test.d.ts +2 -0
- package/dist/types/src/session/version-pin.test.d.ts.map +1 -0
- package/dist/types/src/testing/test-schema.d.ts +2 -2
- package/dist/types/src/testing/test-schema.d.ts.map +1 -1
- package/package.json +13 -13
- package/src/bin/chat.ts +2 -2
- package/src/cypher/combinators.test.ts +4 -4
- package/src/cypher/execution-plan.test.ts +1 -1
- package/src/cypher/query-executor.test.ts +1 -1
- package/src/cypher/schema.test.ts +3 -3
- package/src/session/index.ts +1 -0
- package/src/session/session.test.ts +2 -1
- package/src/session/session.ts +283 -147
- package/src/session/version-pin.test.ts +26 -0
- package/src/session/version-pin.ts +43 -0
- package/src/testing/query.test.ts +3 -3
- package/src/testing/test-data.ts +5 -5
- package/src/testing/test-schema.ts +3 -3
package/src/session/session.ts
CHANGED
|
@@ -2,16 +2,26 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { Schema as S } from 'effect';
|
|
6
|
-
|
|
7
|
-
import {
|
|
5
|
+
import { Option, Schema as S } from 'effect';
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
defineTool,
|
|
9
|
+
Message,
|
|
10
|
+
ToolResult,
|
|
11
|
+
type ArtifactDefinition,
|
|
12
|
+
type MessageContentBlock,
|
|
13
|
+
type Tool,
|
|
14
|
+
} from '@dxos/artifact';
|
|
8
15
|
import { Event, synchronized } from '@dxos/async';
|
|
9
|
-
import {
|
|
16
|
+
import { ObjectVersion } from '@dxos/echo-db';
|
|
17
|
+
import { create, type ObjectId } from '@dxos/echo-schema';
|
|
10
18
|
import { invariant } from '@dxos/invariant';
|
|
11
19
|
import { log } from '@dxos/log';
|
|
12
20
|
|
|
21
|
+
import { VersionPin } from './version-pin';
|
|
13
22
|
import { MixedStreamParser, type AIServiceClient, type GenerateRequest, type GenerationStream } from '../ai-service';
|
|
14
23
|
import { isToolUse, runTools } from '../conversation';
|
|
24
|
+
|
|
15
25
|
/**
|
|
16
26
|
* Contains message history, tools, current context.
|
|
17
27
|
* Current context means the state of the app, time of day, and other contextual information.
|
|
@@ -23,6 +33,22 @@ import { isToolUse, runTools } from '../conversation';
|
|
|
23
33
|
* Could be personal or shared.
|
|
24
34
|
*/
|
|
25
35
|
|
|
36
|
+
/**
|
|
37
|
+
* Resolves artifact ids to their versions.
|
|
38
|
+
* Used to give the model a sense of the changes to the artifacts made by users during the conversation.
|
|
39
|
+
* The artifacts versions are pinned in the history, and whenever the artifact changes in-between assistant's steps,
|
|
40
|
+
* a diff is inserted into the conversation.
|
|
41
|
+
*/
|
|
42
|
+
export type ArtifactDiffResolver = (artifacts: { id: ObjectId; lastVersion: ObjectVersion }[]) => Promise<
|
|
43
|
+
Map<
|
|
44
|
+
ObjectId,
|
|
45
|
+
{
|
|
46
|
+
version: ObjectVersion;
|
|
47
|
+
diff?: string;
|
|
48
|
+
}
|
|
49
|
+
>
|
|
50
|
+
>;
|
|
51
|
+
|
|
26
52
|
export type SessionRunOptions = {
|
|
27
53
|
client: AIServiceClient;
|
|
28
54
|
|
|
@@ -47,15 +73,22 @@ export type SessionRunOptions = {
|
|
|
47
73
|
* Pre-require artifacts.
|
|
48
74
|
*/
|
|
49
75
|
requiredArtifactIds?: string[];
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @see ArtifactDiffResolver
|
|
79
|
+
*/
|
|
80
|
+
artifactDiffResolver?: ArtifactDiffResolver;
|
|
50
81
|
};
|
|
51
82
|
|
|
52
|
-
type OperationModel = 'planning' | '
|
|
83
|
+
type OperationModel = 'planning' | 'importing' | 'configured';
|
|
53
84
|
|
|
54
85
|
export type AiSessionOptions = {
|
|
55
86
|
/**
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
87
|
+
* Determines the agent's handling of the artifacts definitions:
|
|
88
|
+
*
|
|
89
|
+
* - `planning`: The model will create a plan with required artifacts for each step.
|
|
90
|
+
* - `importing`: The model can query registry and only pull in the artifacts that are required to complete the task.
|
|
91
|
+
* - `configured`: The available artifacts are pre-selected and the model cannot select additional artifacts.
|
|
59
92
|
*/
|
|
60
93
|
operationModel: OperationModel;
|
|
61
94
|
};
|
|
@@ -104,147 +137,30 @@ export class AISession {
|
|
|
104
137
|
|
|
105
138
|
@synchronized
|
|
106
139
|
async run(options: SessionRunOptions): Promise<Message[]> {
|
|
107
|
-
const systemTools: Tool[] = [
|
|
108
|
-
defineTool('system', {
|
|
109
|
-
name: 'query_artifact_definitions',
|
|
110
|
-
description: 'Query the available artifact definitions',
|
|
111
|
-
schema: S.Struct({}),
|
|
112
|
-
execute: async () => {
|
|
113
|
-
return ToolResult.Success({
|
|
114
|
-
artifactDefinitions: options.artifacts.map((artifact) => ({
|
|
115
|
-
id: artifact.id,
|
|
116
|
-
name: artifact.name,
|
|
117
|
-
description: artifact.description,
|
|
118
|
-
})),
|
|
119
|
-
});
|
|
120
|
-
},
|
|
121
|
-
}),
|
|
122
|
-
];
|
|
140
|
+
const systemTools: Tool[] = [];
|
|
123
141
|
switch (this._options.operationModel) {
|
|
124
142
|
case 'planning':
|
|
125
|
-
systemTools.push(
|
|
126
|
-
defineTool('system', {
|
|
127
|
-
name: 'create_plan',
|
|
128
|
-
description:
|
|
129
|
-
'Create a plan. Make sure that each step is independent and can be executed solely on the data returned by the previous step. The steps only share the data that is specified in the plan.',
|
|
130
|
-
schema: S.Struct({
|
|
131
|
-
goal: S.String.annotations({ description: 'The goal that the plan will achieve.' }),
|
|
132
|
-
steps: S.Array(
|
|
133
|
-
S.Struct({
|
|
134
|
-
action: S.String.annotations({
|
|
135
|
-
description: 'Complete, detailed action to perform.',
|
|
136
|
-
}),
|
|
137
|
-
input: S.String.annotations({
|
|
138
|
-
description: 'The required input data for this step. First step must not require any data.',
|
|
139
|
-
}),
|
|
140
|
-
output: S.String.annotations({
|
|
141
|
-
description: 'The output data from this step. This is passed to the next step as input.',
|
|
142
|
-
}),
|
|
143
|
-
requiredArtifactIds: S.Array(S.String).annotations({
|
|
144
|
-
description: 'The ids of the artifacts required to perform this step.',
|
|
145
|
-
examples: [['artifact:dxos.org/example/Test']],
|
|
146
|
-
}),
|
|
147
|
-
}),
|
|
148
|
-
).annotations({ description: 'Steps' }),
|
|
149
|
-
}),
|
|
150
|
-
execute: async ({ goal, steps }) => {
|
|
151
|
-
const missingArtifactIds = steps
|
|
152
|
-
.flatMap((step) => step.requiredArtifactIds)
|
|
153
|
-
.filter((artifactId) => !options.artifacts.some((artifact) => artifact.id === artifactId));
|
|
154
|
-
if (missingArtifactIds.length > 0) {
|
|
155
|
-
return ToolResult.Error(`One or more artifact ids are invalid: ${missingArtifactIds.join(', ')}`);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
const stepResults: any[] = [];
|
|
159
|
-
for (const step of steps) {
|
|
160
|
-
log('executing step', { action: step.action });
|
|
161
|
-
const session = new AISession({ operationModel: 'immediate' });
|
|
162
|
-
session.message.on((ev) => this.message.emit(ev));
|
|
163
|
-
session.block.on((ev) => this.block.emit(ev));
|
|
164
|
-
session.update.on((ev) => this.update.emit(ev));
|
|
165
|
-
session.streamEvent.on((ev) => this.streamEvent.emit(ev));
|
|
166
|
-
session.userMessage.on((ev) => this.userMessage.emit(ev));
|
|
167
|
-
|
|
168
|
-
const messages = await session.run({
|
|
169
|
-
client: options.client,
|
|
170
|
-
artifacts: options.artifacts,
|
|
171
|
-
tools: options.tools,
|
|
172
|
-
history: options.history,
|
|
173
|
-
extensions: options.extensions,
|
|
174
|
-
generationOptions: options.generationOptions,
|
|
175
|
-
requiredArtifactIds: step.requiredArtifactIds.slice(),
|
|
176
|
-
prompt: `
|
|
177
|
-
You are an agent that executes the subtask in a plan.
|
|
178
|
-
|
|
179
|
-
While you know the plan's goal only focus on your subtask
|
|
180
|
-
Use the data from the previous step to complete your task.
|
|
181
|
-
Return all of the data from "output" in your last message.
|
|
182
|
-
|
|
183
|
-
Current step (that you need to complete):
|
|
184
|
-
${JSON.stringify(
|
|
185
|
-
{
|
|
186
|
-
goal,
|
|
187
|
-
input: step.input,
|
|
188
|
-
output: step.output,
|
|
189
|
-
yourTask: step.action,
|
|
190
|
-
},
|
|
191
|
-
null,
|
|
192
|
-
2,
|
|
193
|
-
)}
|
|
194
|
-
|
|
195
|
-
Previous step results:
|
|
196
|
-
${JSON.stringify(stepResults.at(-1))}.
|
|
197
|
-
`,
|
|
198
|
-
});
|
|
199
|
-
const result = messages.at(-1);
|
|
200
|
-
stepResults.push(result);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
return ToolResult.Success({ stepResults });
|
|
204
|
-
},
|
|
205
|
-
}),
|
|
206
|
-
);
|
|
143
|
+
systemTools.push(this._createQueryArtifactsTool(options.artifacts), this._createPlanningTool(options));
|
|
207
144
|
break;
|
|
208
|
-
case '
|
|
145
|
+
case 'importing':
|
|
209
146
|
systemTools.push(
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
artifactDefinitionIds: S.Array(S.String).annotations({
|
|
216
|
-
description: 'The ids of the artifact definitions to require',
|
|
217
|
-
examples: [['artifact:dxos.org/example/Test']],
|
|
218
|
-
}),
|
|
219
|
-
}),
|
|
220
|
-
execute: async ({ artifactDefinitionIds }) => {
|
|
221
|
-
const missingArtifactDefinitionIds = artifactDefinitionIds.filter(
|
|
222
|
-
(artifactId) => !options.artifacts.some((artifact) => artifact.id === artifactId),
|
|
223
|
-
);
|
|
224
|
-
if (missingArtifactDefinitionIds.length > 0) {
|
|
225
|
-
return ToolResult.Error(
|
|
226
|
-
`One or more artifact definition ids are invalid: ${missingArtifactDefinitionIds.join(', ')}`,
|
|
227
|
-
);
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
for (const artifactDefinitionId of artifactDefinitionIds) {
|
|
231
|
-
requiredArtifactIds.add(artifactDefinitionId);
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
return ToolResult.Success({});
|
|
235
|
-
},
|
|
147
|
+
this._createQueryArtifactsTool(options.artifacts),
|
|
148
|
+
this._createRequireTool(options.artifacts, (artifactDefinitionIds) => {
|
|
149
|
+
for (const artifactDefinitionId of artifactDefinitionIds) {
|
|
150
|
+
requiredArtifactIds.add(artifactDefinitionId);
|
|
151
|
+
}
|
|
236
152
|
}),
|
|
237
153
|
);
|
|
238
154
|
break;
|
|
155
|
+
case 'configured':
|
|
156
|
+
// no system tools
|
|
157
|
+
break;
|
|
158
|
+
default:
|
|
159
|
+
throw new TypeError(`Invalid operation model: ${this._options.operationModel}`);
|
|
239
160
|
}
|
|
240
161
|
|
|
241
162
|
this._history = [...options.history];
|
|
242
|
-
this._pending = [
|
|
243
|
-
create(Message, {
|
|
244
|
-
role: 'user',
|
|
245
|
-
content: [{ type: 'text', text: options.prompt }],
|
|
246
|
-
}),
|
|
247
|
-
];
|
|
163
|
+
this._pending = [await this._formatUserPrompt(options.artifactDiffResolver, options.prompt, options.history)];
|
|
248
164
|
this.userMessage.emit(this._pending.at(-1)!);
|
|
249
165
|
this._stream = undefined;
|
|
250
166
|
let error: Error | undefined;
|
|
@@ -329,6 +245,176 @@ export class AISession {
|
|
|
329
245
|
return this._pending;
|
|
330
246
|
}
|
|
331
247
|
|
|
248
|
+
private async _formatUserPrompt(
|
|
249
|
+
artifactDiffResolver: ArtifactDiffResolver | undefined,
|
|
250
|
+
prompt: string,
|
|
251
|
+
history: Message[],
|
|
252
|
+
) {
|
|
253
|
+
const prelude: MessageContentBlock[] = [];
|
|
254
|
+
|
|
255
|
+
if (artifactDiffResolver) {
|
|
256
|
+
const versions = gatherObjectVersions(history);
|
|
257
|
+
|
|
258
|
+
const artifactDiff = await artifactDiffResolver(
|
|
259
|
+
Array.from(versions.entries()).map(([id, version]) => ({ id, lastVersion: version })),
|
|
260
|
+
);
|
|
261
|
+
|
|
262
|
+
log.info('vision', {
|
|
263
|
+
artifactDiff,
|
|
264
|
+
versions,
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
for (const [id, { version }] of [...artifactDiff.entries()]) {
|
|
268
|
+
if (ObjectVersion.equals(version, versions.get(id)!)) {
|
|
269
|
+
artifactDiff.delete(id);
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
prelude.push(VersionPin.createBlock(VersionPin.make({ objectId: id, version })));
|
|
274
|
+
}
|
|
275
|
+
if (artifactDiff.size > 0) {
|
|
276
|
+
prelude.push(createArtifactUpdateBlock(artifactDiff));
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return create(Message, {
|
|
281
|
+
role: 'user',
|
|
282
|
+
content: [...prelude, { type: 'text', text: prompt }],
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
private _createQueryArtifactsTool(artifacts: ArtifactDefinition[]) {
|
|
287
|
+
return defineTool('system', {
|
|
288
|
+
name: 'query_artifact_definitions',
|
|
289
|
+
description: 'Query the available artifact definitions',
|
|
290
|
+
schema: S.Struct({}),
|
|
291
|
+
execute: async () => {
|
|
292
|
+
return ToolResult.Success({
|
|
293
|
+
artifactDefinitions: artifacts.map((artifact) => ({
|
|
294
|
+
id: artifact.id,
|
|
295
|
+
name: artifact.name,
|
|
296
|
+
description: artifact.description,
|
|
297
|
+
})),
|
|
298
|
+
});
|
|
299
|
+
},
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
private _createRequireTool(
|
|
304
|
+
artifacts: ArtifactDefinition[],
|
|
305
|
+
onRequire: (artifactDefinitionIds: readonly string[]) => void,
|
|
306
|
+
) {
|
|
307
|
+
return defineTool('system', {
|
|
308
|
+
name: 'require_artifact_definitions',
|
|
309
|
+
description:
|
|
310
|
+
'Require the use of specific artifact definitions. This will allow the model to interact with artifact definitions and use their tools.',
|
|
311
|
+
schema: S.Struct({
|
|
312
|
+
artifactDefinitionIds: S.Array(S.String).annotations({
|
|
313
|
+
description: 'The ids of the artifact definitions to require',
|
|
314
|
+
examples: [['artifact:dxos.org/example/Test']],
|
|
315
|
+
}),
|
|
316
|
+
}),
|
|
317
|
+
execute: async ({ artifactDefinitionIds }) => {
|
|
318
|
+
const missingArtifactDefinitionIds = artifactDefinitionIds.filter(
|
|
319
|
+
(artifactId) => !artifacts.some((artifact) => artifact.id === artifactId),
|
|
320
|
+
);
|
|
321
|
+
if (missingArtifactDefinitionIds.length > 0) {
|
|
322
|
+
return ToolResult.Error(
|
|
323
|
+
`One or more artifact definition ids are invalid: ${missingArtifactDefinitionIds.join(', ')}`,
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
onRequire(artifactDefinitionIds);
|
|
328
|
+
|
|
329
|
+
return ToolResult.Success({});
|
|
330
|
+
},
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
private _createPlanningTool(options: SessionRunOptions) {
|
|
335
|
+
return defineTool('system', {
|
|
336
|
+
name: 'create_plan',
|
|
337
|
+
description:
|
|
338
|
+
'Create a plan. Make sure that each step is independent and can be executed solely on the data returned by the previous step. The steps only share the data that is specified in the plan.',
|
|
339
|
+
schema: S.Struct({
|
|
340
|
+
goal: S.String.annotations({ description: 'The goal that the plan will achieve.' }),
|
|
341
|
+
steps: S.Array(
|
|
342
|
+
S.Struct({
|
|
343
|
+
action: S.String.annotations({
|
|
344
|
+
description: 'Complete, detailed action to perform.',
|
|
345
|
+
}),
|
|
346
|
+
input: S.String.annotations({
|
|
347
|
+
description: 'The required input data for this step. First step must not require any data.',
|
|
348
|
+
}),
|
|
349
|
+
output: S.String.annotations({
|
|
350
|
+
description: 'The output data from this step. This is passed to the next step as input.',
|
|
351
|
+
}),
|
|
352
|
+
requiredArtifactIds: S.Array(S.String).annotations({
|
|
353
|
+
description: 'The ids of the artifacts required to perform this step.',
|
|
354
|
+
examples: [['artifact:dxos.org/example/Test']],
|
|
355
|
+
}),
|
|
356
|
+
}),
|
|
357
|
+
).annotations({ description: 'Steps' }),
|
|
358
|
+
}),
|
|
359
|
+
execute: async ({ goal, steps }) => {
|
|
360
|
+
const missingArtifactIds = steps
|
|
361
|
+
.flatMap((step) => step.requiredArtifactIds)
|
|
362
|
+
.filter((artifactId) => !options.artifacts.some((artifact) => artifact.id === artifactId));
|
|
363
|
+
if (missingArtifactIds.length > 0) {
|
|
364
|
+
return ToolResult.Error(`One or more artifact ids are invalid: ${missingArtifactIds.join(', ')}`);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const stepResults: any[] = [];
|
|
368
|
+
for (const step of steps) {
|
|
369
|
+
log('executing step', { action: step.action });
|
|
370
|
+
const session = new AISession({ operationModel: 'importing' });
|
|
371
|
+
session.message.on((ev) => this.message.emit(ev));
|
|
372
|
+
session.block.on((ev) => this.block.emit(ev));
|
|
373
|
+
session.update.on((ev) => this.update.emit(ev));
|
|
374
|
+
session.streamEvent.on((ev) => this.streamEvent.emit(ev));
|
|
375
|
+
session.userMessage.on((ev) => this.userMessage.emit(ev));
|
|
376
|
+
|
|
377
|
+
const messages = await session.run({
|
|
378
|
+
client: options.client,
|
|
379
|
+
artifacts: options.artifacts,
|
|
380
|
+
tools: options.tools,
|
|
381
|
+
history: options.history,
|
|
382
|
+
extensions: options.extensions,
|
|
383
|
+
generationOptions: options.generationOptions,
|
|
384
|
+
requiredArtifactIds: step.requiredArtifactIds.slice(),
|
|
385
|
+
artifactDiffResolver: options.artifactDiffResolver,
|
|
386
|
+
prompt: `
|
|
387
|
+
You are an agent that executes the subtask in a plan.
|
|
388
|
+
|
|
389
|
+
While you know the plan's goal only focus on your subtask
|
|
390
|
+
Use the data from the previous step to complete your task.
|
|
391
|
+
Return all of the data from "output" in your last message.
|
|
392
|
+
|
|
393
|
+
Current step (that you need to complete):
|
|
394
|
+
${JSON.stringify(
|
|
395
|
+
{
|
|
396
|
+
goal,
|
|
397
|
+
input: step.input,
|
|
398
|
+
output: step.output,
|
|
399
|
+
yourTask: step.action,
|
|
400
|
+
},
|
|
401
|
+
null,
|
|
402
|
+
2,
|
|
403
|
+
)}
|
|
404
|
+
|
|
405
|
+
Previous step results:
|
|
406
|
+
${JSON.stringify(stepResults.at(-1))}.
|
|
407
|
+
`,
|
|
408
|
+
});
|
|
409
|
+
const result = messages.at(-1);
|
|
410
|
+
stepResults.push(result);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
return ToolResult.Success({ stepResults });
|
|
414
|
+
},
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
|
|
332
418
|
abort() {
|
|
333
419
|
this._stream?.abort();
|
|
334
420
|
}
|
|
@@ -351,19 +437,37 @@ const createBaseInstructions = ({
|
|
|
351
437
|
Decision-making:
|
|
352
438
|
|
|
353
439
|
- Analyze the structure and type of the content in the user's message.
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
- Identify which artifacts are relevant to the user's request.
|
|
440
|
+
|
|
441
|
+
|
|
357
442
|
${
|
|
358
443
|
operationModel === 'planning'
|
|
359
444
|
? `
|
|
445
|
+
- Can you complete the task using the available artifacts?
|
|
446
|
+
- If you can't complete the task using the available artifacts, query the list of available artifacts using the appropriate tool.
|
|
447
|
+
- Identify which artifacts are relevant to the user's request.
|
|
360
448
|
- Break down the user's request into a step-by-step plan that you will use to execute the user's request, the plan items should include artifacts which will be used to perform the actions.
|
|
361
449
|
`
|
|
362
|
-
:
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
450
|
+
: ''
|
|
451
|
+
}
|
|
452
|
+
${
|
|
453
|
+
operationModel === 'importing'
|
|
454
|
+
? `
|
|
455
|
+
- Can you complete the task using the available artifacts?
|
|
456
|
+
- If you can't complete the task using the available artifacts, query the list of available artifacts using the appropriate tool.
|
|
457
|
+
- Identify which artifacts are relevant to the user's request.
|
|
458
|
+
- Are the required artifacts already available?
|
|
459
|
+
- If not, select which artifact(s) will be the most relevant and require them using the require_artifacts tool.
|
|
460
|
+
- The require'd artifact tools will be available for use after require.
|
|
461
|
+
`
|
|
462
|
+
: ''
|
|
463
|
+
}
|
|
464
|
+
${
|
|
465
|
+
operationModel === 'configured'
|
|
466
|
+
? `
|
|
467
|
+
- Select the most relevant artifact(s) to complete the task.
|
|
468
|
+
- Call the appropriate tool to use the artifact(s).
|
|
469
|
+
`
|
|
470
|
+
: ''
|
|
367
471
|
}
|
|
368
472
|
|
|
369
473
|
${availableArtifacts.length > 0 ? `Artifacts already in context: ${availableArtifacts.join('\n')}` : ''}
|
|
@@ -373,3 +477,35 @@ export class AIServiecOverloadedError extends S.TaggedError<AIServiecOverloadedE
|
|
|
373
477
|
'AIServiecOverloadedError',
|
|
374
478
|
{},
|
|
375
479
|
) {}
|
|
480
|
+
|
|
481
|
+
const gatherObjectVersions = (messages: Message[]): Map<ObjectId, ObjectVersion> => {
|
|
482
|
+
const artifactIds = new Map<ObjectId, ObjectVersion>();
|
|
483
|
+
for (const message of messages) {
|
|
484
|
+
for (const block of message.content) {
|
|
485
|
+
if (block.type === 'json' && block.disposition === VersionPin.DISPOSITION) {
|
|
486
|
+
const pin = VersionPin.pipe(S.decodeOption)(JSON.parse(block.json)).pipe(Option.getOrUndefined);
|
|
487
|
+
if (!pin) {
|
|
488
|
+
continue;
|
|
489
|
+
}
|
|
490
|
+
artifactIds.set(pin.objectId, pin.version);
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
return artifactIds;
|
|
496
|
+
};
|
|
497
|
+
|
|
498
|
+
const createArtifactUpdateBlock = (
|
|
499
|
+
artifactDiff: Map<ObjectId, { version: ObjectVersion; diff?: string }>,
|
|
500
|
+
): MessageContentBlock => {
|
|
501
|
+
return {
|
|
502
|
+
type: 'text',
|
|
503
|
+
disposition: 'artifact-update',
|
|
504
|
+
text: `
|
|
505
|
+
The following artifacts have been updated since the last message:
|
|
506
|
+
${Array.from(artifactDiff.entries())
|
|
507
|
+
.map(([id, { diff }]) => `<changed-artifact id="${id}">${diff ? `\n${diff}` : ''}</changed-artifact>`)
|
|
508
|
+
.join('\n')}
|
|
509
|
+
`,
|
|
510
|
+
};
|
|
511
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { pipe, Schema } from 'effect';
|
|
6
|
+
import { describe, expect, test } from 'vitest';
|
|
7
|
+
|
|
8
|
+
import { Expando } from '@dxos/echo-schema';
|
|
9
|
+
import { invariant } from '@dxos/invariant';
|
|
10
|
+
import { live } from '@dxos/live-object';
|
|
11
|
+
|
|
12
|
+
import { VersionPin } from './version-pin';
|
|
13
|
+
|
|
14
|
+
describe('VersionPin', () => {
|
|
15
|
+
test('serialization', () => {
|
|
16
|
+
const obj = live(Expando, {
|
|
17
|
+
name: 'John Doe',
|
|
18
|
+
});
|
|
19
|
+
const block = VersionPin.createBlock(VersionPin.make({ objectId: obj.id, version: obj.version }));
|
|
20
|
+
invariant(block.type === 'json' && block.disposition === VersionPin.DISPOSITION);
|
|
21
|
+
const pin = pipe(JSON.parse(block.json), VersionPin.pipe(Schema.decodeSync));
|
|
22
|
+
expect(pin).toBeDefined();
|
|
23
|
+
expect(pin?.objectId).toBe(obj.id);
|
|
24
|
+
expect(pin?.version).toBe(obj.version);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Schema } from 'effect';
|
|
6
|
+
|
|
7
|
+
import type { MessageContentBlock } from '@dxos/artifact';
|
|
8
|
+
import type { ObjectVersion } from '@dxos/echo-db';
|
|
9
|
+
import { getVersion } from '@dxos/echo-db';
|
|
10
|
+
import { ObjectId, type BaseEchoObject } from '@dxos/echo-schema';
|
|
11
|
+
|
|
12
|
+
// TODO(dmaretskyi): Extract.
|
|
13
|
+
const ObjectVersionSchema = Schema.Unknown as Schema.Schema<ObjectVersion>;
|
|
14
|
+
|
|
15
|
+
const VersionPinSchema = Schema.Struct({
|
|
16
|
+
// TODO(dmaretskyi): Use Ref when those support encoding.
|
|
17
|
+
objectId: ObjectId,
|
|
18
|
+
// TODO(dmaretskyi): Could be opaque
|
|
19
|
+
version: ObjectVersionSchema,
|
|
20
|
+
});
|
|
21
|
+
export interface VersionPin extends Schema.Schema.Type<typeof VersionPinSchema> {}
|
|
22
|
+
|
|
23
|
+
export const VersionPin: typeof VersionPinSchema & {
|
|
24
|
+
DISPOSITION: 'version-pin';
|
|
25
|
+
fromObject: (object: BaseEchoObject) => VersionPin;
|
|
26
|
+
createBlock: (pin: VersionPin) => MessageContentBlock;
|
|
27
|
+
} = class extends VersionPinSchema {
|
|
28
|
+
static readonly DISPOSITION = 'version-pin';
|
|
29
|
+
static fromObject(object: BaseEchoObject): VersionPin {
|
|
30
|
+
return VersionPin.make({
|
|
31
|
+
objectId: object.id,
|
|
32
|
+
version: getVersion(object),
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
static createBlock(pin: VersionPin): MessageContentBlock {
|
|
37
|
+
return {
|
|
38
|
+
type: 'json',
|
|
39
|
+
disposition: VersionPin.DISPOSITION,
|
|
40
|
+
json: JSON.stringify(pin),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
};
|
|
@@ -14,7 +14,7 @@ import { AI_SERVICE_ENDPOINT } from './defs';
|
|
|
14
14
|
import { createLogger } from './logger';
|
|
15
15
|
import { createCypherTool, createSystemPrompt } from './query-promts';
|
|
16
16
|
import { createTestData, seedTestData } from './test-data';
|
|
17
|
-
import { Contact,
|
|
17
|
+
import { Contact, Organization, Project, Task } from './test-schema';
|
|
18
18
|
import { AIServiceEdgeClient, DEFAULT_EDGE_MODEL } from '../ai-service';
|
|
19
19
|
import { runLLM } from '../conversation';
|
|
20
20
|
import { EchoDataSource } from '../cypher';
|
|
@@ -25,7 +25,7 @@ const client = new AIServiceEdgeClient({
|
|
|
25
25
|
|
|
26
26
|
test.skip('cypher query', async () => {
|
|
27
27
|
const dataSource = createTestData();
|
|
28
|
-
const schemaTypes = [
|
|
28
|
+
const schemaTypes = [Organization, Project, Task, Contact];
|
|
29
29
|
|
|
30
30
|
const cypherTool = createCypherTool(dataSource);
|
|
31
31
|
|
|
@@ -56,7 +56,7 @@ test.skip('query ECHO', async () => {
|
|
|
56
56
|
await using builder = await new EchoTestBuilder().open();
|
|
57
57
|
const { db, graph } = await builder.createDatabase();
|
|
58
58
|
|
|
59
|
-
const schemaTypes = [
|
|
59
|
+
const schemaTypes = [Organization, Project, Task, Contact];
|
|
60
60
|
graph.schemaRegistry.addSchema(schemaTypes);
|
|
61
61
|
|
|
62
62
|
seedTestData(db);
|
package/src/testing/test-data.ts
CHANGED
|
@@ -17,7 +17,7 @@ import {
|
|
|
17
17
|
formatInferredRelationshipLabel,
|
|
18
18
|
formatNodeLabel,
|
|
19
19
|
} from '../cypher';
|
|
20
|
-
import { Contact,
|
|
20
|
+
import { Contact, Organization, Project, Task } from '../testing';
|
|
21
21
|
|
|
22
22
|
export const seedTestData = (db: EchoDatabase) => {
|
|
23
23
|
const contactRich = db.add(
|
|
@@ -122,14 +122,14 @@ export const seedTestData = (db: EchoDatabase) => {
|
|
|
122
122
|
);
|
|
123
123
|
|
|
124
124
|
const _orgDxos = db.add(
|
|
125
|
-
live(
|
|
125
|
+
live(Organization, {
|
|
126
126
|
name: 'DXOS',
|
|
127
127
|
employees: [makeRef(contactRich), makeRef(contactJosiah), makeRef(contactDima)],
|
|
128
128
|
projects: [makeRef(projectEcho)],
|
|
129
129
|
}),
|
|
130
130
|
);
|
|
131
131
|
const _orgBraneframe = db.add(
|
|
132
|
-
live(
|
|
132
|
+
live(Organization, {
|
|
133
133
|
name: 'Braneframe',
|
|
134
134
|
employees: [makeRef(contactJosiah), makeRef(contactRich)],
|
|
135
135
|
projects: [makeRef(projectComposer)],
|
|
@@ -226,13 +226,13 @@ export const createTestData = (): MockDataSource => {
|
|
|
226
226
|
assignee: makeRef(contactJosiah),
|
|
227
227
|
});
|
|
228
228
|
|
|
229
|
-
const _orgDxos = dataSource.add(
|
|
229
|
+
const _orgDxos = dataSource.add(Organization, {
|
|
230
230
|
id: ObjectId.random(),
|
|
231
231
|
name: 'DXOS',
|
|
232
232
|
employees: [makeRef(contactRich), makeRef(contactJosiah), makeRef(contactDima)],
|
|
233
233
|
projects: [makeRef(projectEcho)],
|
|
234
234
|
});
|
|
235
|
-
const _orgBraneframe = dataSource.add(
|
|
235
|
+
const _orgBraneframe = dataSource.add(Organization, {
|
|
236
236
|
id: ObjectId.random(),
|
|
237
237
|
name: 'Braneframe',
|
|
238
238
|
employees: [makeRef(contactJosiah), makeRef(contactRich)],
|
|
@@ -62,7 +62,7 @@ export const Task = S.Struct({
|
|
|
62
62
|
});
|
|
63
63
|
export interface Task extends S.Schema.Type<typeof Task> {}
|
|
64
64
|
|
|
65
|
-
export const
|
|
65
|
+
export const Organization = S.Struct({
|
|
66
66
|
id: ObjectId,
|
|
67
67
|
name: S.String.annotations({ description: 'The name of the organization.' }),
|
|
68
68
|
projects: S.Array(Ref(Project)),
|
|
@@ -72,9 +72,9 @@ export const Org = S.Struct({
|
|
|
72
72
|
.annotations({
|
|
73
73
|
[TypeAnnotationId]: {
|
|
74
74
|
kind: EntityKind.Object,
|
|
75
|
-
typename: 'example.com/type/
|
|
75
|
+
typename: 'example.com/type/Organization',
|
|
76
76
|
version: '0.1.0',
|
|
77
77
|
},
|
|
78
78
|
description: 'Contact information.',
|
|
79
79
|
});
|
|
80
|
-
export interface
|
|
80
|
+
export interface Organization extends S.Schema.Type<typeof Organization> {}
|