@dxos/assistant 0.8.1 → 0.8.2-main.2f9c567
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-PND5VR6D.mjs → chunk-OLO6SVOL.mjs} +91 -84
- package/dist/lib/browser/chunk-OLO6SVOL.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +276 -154
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +98 -102
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node/{chunk-IBOBRBDF.cjs → chunk-CI5Q7FBG.cjs} +106 -98
- package/dist/lib/node/chunk-CI5Q7FBG.cjs.map +7 -0
- package/dist/lib/node/index.cjs +302 -183
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +88 -93
- package/dist/lib/node/testing/index.cjs.map +3 -3
- package/dist/lib/node-esm/{chunk-DCQJOLSR.mjs → chunk-Y3SYOFFJ.mjs} +91 -84
- package/dist/lib/node-esm/chunk-Y3SYOFFJ.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +276 -154
- 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 +98 -102
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/types/src/ai-service/edge-client.d.ts +4 -5
- package/dist/types/src/ai-service/edge-client.d.ts.map +1 -1
- package/dist/types/src/ai-service/message-collector.d.ts +0 -6
- package/dist/types/src/ai-service/message-collector.d.ts.map +1 -1
- package/dist/types/src/ai-service/ollama-client.d.ts +6 -2
- package/dist/types/src/ai-service/ollama-client.d.ts.map +1 -1
- package/dist/types/src/ai-service/parser.d.ts +0 -3
- package/dist/types/src/ai-service/parser.d.ts.map +1 -1
- package/dist/types/src/ai-service/service.d.ts +18 -2
- package/dist/types/src/ai-service/service.d.ts.map +1 -1
- package/dist/types/src/ai-service/testing/mock.d.ts +6 -2
- package/dist/types/src/ai-service/testing/mock.d.ts.map +1 -1
- package/dist/types/src/ai-service/types.d.ts +144 -101
- package/dist/types/src/ai-service/types.d.ts.map +1 -1
- package/dist/types/src/conversation/conversation.d.ts +0 -3
- package/dist/types/src/conversation/conversation.d.ts.map +1 -1
- package/dist/types/src/conversation/types.d.ts +11 -12
- package/dist/types/src/conversation/types.d.ts.map +1 -1
- package/dist/types/src/cypher/echo-data-source.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/session.d.ts +32 -13
- 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/query-promts.d.ts +3 -3
- package/dist/types/src/testing/query-promts.d.ts.map +1 -1
- package/dist/types/src/testing/test-data.d.ts +4 -4
- package/dist/types/src/testing/test-data.d.ts.map +1 -1
- package/dist/types/src/testing/test-schema.d.ts +25 -23
- package/dist/types/src/testing/test-schema.d.ts.map +1 -1
- package/package.json +15 -14
- package/src/ai-service/client.test.ts +55 -73
- package/src/ai-service/edge-client.ts +13 -20
- package/src/ai-service/ollama-client.ts +16 -9
- package/src/ai-service/parser.ts +13 -2
- package/src/ai-service/service.ts +19 -3
- package/src/ai-service/testing/mock.ts +9 -1
- package/src/ai-service/types.ts +24 -15
- package/src/bin/chat.ts +3 -3
- package/src/conversation/conversation.test.ts +3 -3
- package/src/conversation/conversation.ts +5 -11
- package/src/conversation/types.ts +11 -14
- package/src/cypher/combinators.test.ts +4 -4
- package/src/cypher/echo-data-source.ts +7 -6
- 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 +29 -22
- package/src/session/session.ts +286 -150
- package/src/session/version-pin.test.ts +26 -0
- package/src/session/version-pin.ts +43 -0
- package/src/testing/artifacts.test.ts +22 -22
- package/src/testing/query-promts.ts +5 -5
- package/src/testing/query.test.ts +17 -19
- package/src/testing/test-data.ts +78 -71
- package/src/testing/test-schema.ts +28 -29
- package/dist/lib/browser/chunk-PND5VR6D.mjs.map +0 -7
- package/dist/lib/node/chunk-IBOBRBDF.cjs.map +0 -7
- package/dist/lib/node-esm/chunk-DCQJOLSR.mjs.map +0 -7
package/src/session/session.ts
CHANGED
|
@@ -2,16 +2,26 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { Schema
|
|
6
|
-
|
|
7
|
-
import {
|
|
5
|
+
import { Option, Schema } 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
|
-
createStatic(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;
|
|
@@ -268,7 +184,7 @@ export class AISession {
|
|
|
268
184
|
});
|
|
269
185
|
|
|
270
186
|
// Open request stream.
|
|
271
|
-
this._stream = await options.client.
|
|
187
|
+
this._stream = await options.client.execStream({
|
|
272
188
|
...(options.generationOptions ?? {}),
|
|
273
189
|
// TODO(burdon): Rename messages or separate history/message.
|
|
274
190
|
history: [...this._history, ...this._pending],
|
|
@@ -314,7 +230,7 @@ export class AISession {
|
|
|
314
230
|
} catch (err) {
|
|
315
231
|
log.catch(err);
|
|
316
232
|
if (err instanceof Error && err.message.includes('Overloaded')) {
|
|
317
|
-
error = new AIServiecOverloadedError('AI service overloaded', { cause: err });
|
|
233
|
+
error = new AIServiecOverloadedError('AI service overloaded', { cause: err } as any); // TODO(dmaretskyi): Schema errors cant have cause
|
|
318
234
|
} else {
|
|
319
235
|
error = new Error('AI service error', { cause: err });
|
|
320
236
|
}
|
|
@@ -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: Schema.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: Schema.Struct({
|
|
312
|
+
artifactDefinitionIds: Schema.Array(Schema.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: Schema.Struct({
|
|
340
|
+
goal: Schema.String.annotations({ description: 'The goal that the plan will achieve.' }),
|
|
341
|
+
steps: Schema.Array(
|
|
342
|
+
Schema.Struct({
|
|
343
|
+
action: Schema.String.annotations({
|
|
344
|
+
description: 'Complete, detailed action to perform.',
|
|
345
|
+
}),
|
|
346
|
+
input: Schema.String.annotations({
|
|
347
|
+
description: 'The required input data for this step. First step must not require any data.',
|
|
348
|
+
}),
|
|
349
|
+
output: Schema.String.annotations({
|
|
350
|
+
description: 'The output data from this step. This is passed to the next step as input.',
|
|
351
|
+
}),
|
|
352
|
+
requiredArtifactIds: Schema.Array(Schema.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,25 +437,75 @@ 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')}` : ''}
|
|
370
474
|
`;
|
|
371
475
|
|
|
372
|
-
export class AIServiecOverloadedError extends
|
|
476
|
+
export class AIServiecOverloadedError extends Schema.TaggedError<AIServiecOverloadedError>()(
|
|
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(Schema.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
|
+
};
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
/*
|
|
6
|
-
import { Schema
|
|
6
|
+
import { Schema } from '@effect/schema';
|
|
7
7
|
import { describe, test } from 'vitest';
|
|
8
8
|
|
|
9
9
|
import { ObjectId, toJsonSchema } from '@dxos/echo-schema';
|
|
@@ -15,26 +15,26 @@ import { AIServiceEdgeClient, Tool } from '../ai-service';
|
|
|
15
15
|
import { defineTool, ToolResult } from '../conversation';
|
|
16
16
|
|
|
17
17
|
// TODO(dmaretskyi): Effect schema.
|
|
18
|
-
const ArtifactDef =
|
|
19
|
-
typename:
|
|
20
|
-
description:
|
|
21
|
-
schema:
|
|
22
|
-
actions:
|
|
23
|
-
}).pipe(
|
|
18
|
+
const ArtifactDef = Schema.Struct({
|
|
19
|
+
typename: Schema.String,
|
|
20
|
+
description: Schema.String,
|
|
21
|
+
schema: Schema.Any,
|
|
22
|
+
actions: Schema.Array(Tool),
|
|
23
|
+
}).pipe(Schema.mutable);
|
|
24
24
|
|
|
25
|
-
type ArtifactDef =
|
|
25
|
+
type ArtifactDef = Schema.Schema.Type<typeof ArtifactDef>;
|
|
26
26
|
|
|
27
|
-
const ListItem =
|
|
28
|
-
name:
|
|
27
|
+
const ListItem = Schema.Struct({
|
|
28
|
+
name: Schema.String,
|
|
29
29
|
});
|
|
30
30
|
|
|
31
|
-
const ListSchema =
|
|
31
|
+
const ListSchema = Schema.Struct({
|
|
32
32
|
id: ObjectId,
|
|
33
|
-
title:
|
|
34
|
-
items:
|
|
35
|
-
}).pipe(
|
|
33
|
+
title: Schema.String,
|
|
34
|
+
items: Schema.Array(ListItem).pipe(Schema.mutable),
|
|
35
|
+
}).pipe(Schema.mutable);
|
|
36
36
|
|
|
37
|
-
type ListSchema =
|
|
37
|
+
type ListSchema = Schema.Schema.Type<typeof ListSchema>;
|
|
38
38
|
|
|
39
39
|
const list: ArtifactDef = {
|
|
40
40
|
typename: 'example.com/type/List',
|
|
@@ -44,7 +44,7 @@ const list: ArtifactDef = {
|
|
|
44
44
|
defineTool('list', {
|
|
45
45
|
name: 'query',
|
|
46
46
|
description: 'Query all lists',
|
|
47
|
-
schema:
|
|
47
|
+
schema: Schema.Struct({}),
|
|
48
48
|
execute: async ({}, context) => {
|
|
49
49
|
return ToolResult.Success(LISTS[id]);
|
|
50
50
|
},
|
|
@@ -52,7 +52,7 @@ const list: ArtifactDef = {
|
|
|
52
52
|
defineTool('list', {
|
|
53
53
|
name: 'inspect',
|
|
54
54
|
description: 'Get contents of the list',
|
|
55
|
-
schema:
|
|
55
|
+
schema: Schema.Struct({
|
|
56
56
|
id: ObjectId.annotations({ description: 'The list to inspect' }),
|
|
57
57
|
}),
|
|
58
58
|
execute: async ({ id }, context) => {
|
|
@@ -62,9 +62,9 @@ const list: ArtifactDef = {
|
|
|
62
62
|
defineTool('list', {
|
|
63
63
|
name: 'create',
|
|
64
64
|
description: 'Add one or more items to an existing list',
|
|
65
|
-
schema:
|
|
65
|
+
schema: Schema.Struct({
|
|
66
66
|
id: ObjectId.annotations({ description: 'The list to add items to' }),
|
|
67
|
-
items:
|
|
67
|
+
items: Schema.Array(ListItem).annotations({ description: 'The items to add to the list' }),
|
|
68
68
|
}),
|
|
69
69
|
execute: async ({ id, items }, context) => {
|
|
70
70
|
LISTS[id].items.push(...items);
|
|
@@ -81,7 +81,7 @@ const artifacts = [list];
|
|
|
81
81
|
const getArtifactDefinitions = defineTool('system', {
|
|
82
82
|
name: 'getArtifactDefinitions',
|
|
83
83
|
description: 'Queries the definitions and metadata of artifacts defined in the system',
|
|
84
|
-
schema:
|
|
84
|
+
schema: Schema.Any,
|
|
85
85
|
execute: async (_, context) => {
|
|
86
86
|
return ToolResult.Success(
|
|
87
87
|
artifacts.map((artifact) => ({
|
|
@@ -116,8 +116,8 @@ describe('Artifacts', () => {
|
|
|
116
116
|
name: 'custodian',
|
|
117
117
|
description: 'Custodian can tell you the password if you say the magic word',
|
|
118
118
|
parameters: toJsonSchema(
|
|
119
|
-
|
|
120
|
-
magicWord:
|
|
119
|
+
Schema.Struct({
|
|
120
|
+
magicWord: Schema.String.annotations({ description: 'The magic word. Should be exactly "pretty please"' }),
|
|
121
121
|
}),
|
|
122
122
|
),
|
|
123
123
|
};
|