@dxos/assistant 0.8.2-main.12df754 → 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.
Files changed (65) hide show
  1. package/dist/lib/browser/{chunk-DEIGOD4U.mjs → chunk-OLO6SVOL.mjs} +21 -21
  2. package/dist/lib/browser/chunk-OLO6SVOL.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +272 -152
  4. package/dist/lib/browser/index.mjs.map +4 -4
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/testing/index.mjs +82 -86
  7. package/dist/lib/browser/testing/index.mjs.map +3 -3
  8. package/dist/lib/node/{chunk-7VH7NMIM.cjs → chunk-CI5Q7FBG.cjs} +6 -6
  9. package/dist/lib/node/chunk-CI5Q7FBG.cjs.map +7 -0
  10. package/dist/lib/node/index.cjs +300 -183
  11. package/dist/lib/node/index.cjs.map +4 -4
  12. package/dist/lib/node/meta.json +1 -1
  13. package/dist/lib/node/testing/index.cjs +72 -77
  14. package/dist/lib/node/testing/index.cjs.map +3 -3
  15. package/dist/lib/node-esm/{chunk-7JI4MDGL.mjs → chunk-Y3SYOFFJ.mjs} +21 -21
  16. package/dist/lib/node-esm/chunk-Y3SYOFFJ.mjs.map +7 -0
  17. package/dist/lib/node-esm/index.mjs +272 -152
  18. package/dist/lib/node-esm/index.mjs.map +4 -4
  19. package/dist/lib/node-esm/meta.json +1 -1
  20. package/dist/lib/node-esm/testing/index.mjs +82 -86
  21. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  22. package/dist/types/src/ai-service/types.d.ts +133 -133
  23. package/dist/types/src/ai-service/types.d.ts.map +1 -1
  24. package/dist/types/src/conversation/types.d.ts +9 -9
  25. package/dist/types/src/conversation/types.d.ts.map +1 -1
  26. package/dist/types/src/cypher/echo-data-source.d.ts.map +1 -1
  27. package/dist/types/src/session/index.d.ts +1 -0
  28. package/dist/types/src/session/index.d.ts.map +1 -1
  29. package/dist/types/src/session/session.d.ts +32 -7
  30. package/dist/types/src/session/session.d.ts.map +1 -1
  31. package/dist/types/src/session/version-pin.d.ts +17 -0
  32. package/dist/types/src/session/version-pin.d.ts.map +1 -0
  33. package/dist/types/src/session/version-pin.test.d.ts +2 -0
  34. package/dist/types/src/session/version-pin.test.d.ts.map +1 -0
  35. package/dist/types/src/testing/query-promts.d.ts +3 -3
  36. package/dist/types/src/testing/query-promts.d.ts.map +1 -1
  37. package/dist/types/src/testing/test-data.d.ts +4 -4
  38. package/dist/types/src/testing/test-data.d.ts.map +1 -1
  39. package/dist/types/src/testing/test-schema.d.ts +25 -23
  40. package/dist/types/src/testing/test-schema.d.ts.map +1 -1
  41. package/package.json +15 -14
  42. package/src/ai-service/client.test.ts +5 -5
  43. package/src/ai-service/ollama-client.ts +7 -7
  44. package/src/ai-service/types.ts +14 -14
  45. package/src/bin/chat.ts +2 -2
  46. package/src/conversation/conversation.test.ts +3 -3
  47. package/src/conversation/types.ts +10 -10
  48. package/src/cypher/combinators.test.ts +4 -4
  49. package/src/cypher/echo-data-source.ts +7 -5
  50. package/src/cypher/execution-plan.test.ts +1 -1
  51. package/src/cypher/query-executor.test.ts +1 -1
  52. package/src/cypher/schema.test.ts +3 -3
  53. package/src/session/index.ts +1 -0
  54. package/src/session/session.test.ts +24 -17
  55. package/src/session/session.ts +285 -149
  56. package/src/session/version-pin.test.ts +26 -0
  57. package/src/session/version-pin.ts +43 -0
  58. package/src/testing/artifacts.test.ts +22 -22
  59. package/src/testing/query-promts.ts +5 -5
  60. package/src/testing/query.test.ts +3 -3
  61. package/src/testing/test-data.ts +62 -55
  62. package/src/testing/test-schema.ts +28 -29
  63. package/dist/lib/browser/chunk-DEIGOD4U.mjs.map +0 -7
  64. package/dist/lib/node/chunk-7VH7NMIM.cjs.map +0 -7
  65. package/dist/lib/node-esm/chunk-7JI4MDGL.mjs.map +0 -7
@@ -2,16 +2,26 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Schema as S } from 'effect';
6
-
7
- import { defineTool, Message, ToolResult, type ArtifactDefinition, type Tool } from '@dxos/artifact';
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 { create } from '@dxos/echo-schema';
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' | 'immediate';
83
+ type OperationModel = 'planning' | 'importing' | 'configured';
53
84
 
54
85
  export type AiSessionOptions = {
55
86
  /**
56
- * In planning mode, the model will create a plan before executing the user's request.
57
- * The plan will determine the artifacts that will be used to perform the actions.
58
- * In require mode, the model will select the artifacts that will be used to perform the actions.
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 'immediate':
145
+ case 'importing':
209
146
  systemTools.push(
210
- defineTool('system', {
211
- name: 'require_artifact_definitions',
212
- description:
213
- 'Require the use of specific artifact definitions. This will allow the model to interact with artifact definitions and use their tools.',
214
- schema: S.Struct({
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;
@@ -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
- - Can you complete the task using the available artifacts?
355
- - If you can't complete the task using the available artifacts, query the list of available artifacts using the appropriate tool.
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
- - Are the required artifacts already available?
364
- - If not, select which artifact(s) will be the most relevant and require them using the require_artifacts tool.
365
- - The require'd artifact tools will be available for use after require.
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 S.TaggedError<AIServiecOverloadedError>()(
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 as S } from '@effect/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 = S.Struct({
19
- typename: S.String,
20
- description: S.String,
21
- schema: S.Any,
22
- actions: S.Array(Tool),
23
- }).pipe(S.mutable);
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 = S.Schema.Type<typeof ArtifactDef>;
25
+ type ArtifactDef = Schema.Schema.Type<typeof ArtifactDef>;
26
26
 
27
- const ListItem = S.Struct({
28
- name: S.String,
27
+ const ListItem = Schema.Struct({
28
+ name: Schema.String,
29
29
  });
30
30
 
31
- const ListSchema = S.Struct({
31
+ const ListSchema = Schema.Struct({
32
32
  id: ObjectId,
33
- title: S.String,
34
- items: S.Array(ListItem).pipe(S.mutable),
35
- }).pipe(S.mutable);
33
+ title: Schema.String,
34
+ items: Schema.Array(ListItem).pipe(Schema.mutable),
35
+ }).pipe(Schema.mutable);
36
36
 
37
- type ListSchema = S.Schema.Type<typeof 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: S.Struct({}),
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: S.Struct({
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: S.Struct({
65
+ schema: Schema.Struct({
66
66
  id: ObjectId.annotations({ description: 'The list to add items to' }),
67
- items: S.Array(ListItem).annotations({ description: 'The items to add to the list' }),
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: S.Any,
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
- S.Struct({
120
- magicWord: S.String.annotations({ description: 'The magic word. Should be exactly "pretty please"' }),
119
+ Schema.Struct({
120
+ magicWord: Schema.String.annotations({ description: 'The magic word. Should be exactly "pretty please"' }),
121
121
  }),
122
122
  ),
123
123
  };