@dxos/assistant 0.8.3 → 0.8.4-main.84f28bd

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 (62) hide show
  1. package/dist/lib/browser/index.mjs +456 -595
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node-esm/index.mjs +456 -595
  5. package/dist/lib/node-esm/index.mjs.map +4 -4
  6. package/dist/lib/node-esm/meta.json +1 -1
  7. package/dist/types/src/blueprint/blueprint.d.ts +33 -47
  8. package/dist/types/src/blueprint/blueprint.d.ts.map +1 -1
  9. package/dist/types/src/blueprint/index.d.ts +1 -2
  10. package/dist/types/src/blueprint/index.d.ts.map +1 -1
  11. package/dist/types/src/blueprint/registry.d.ts +7 -0
  12. package/dist/types/src/blueprint/registry.d.ts.map +1 -0
  13. package/dist/types/src/context/binder.d.ts +34 -0
  14. package/dist/types/src/context/binder.d.ts.map +1 -0
  15. package/dist/types/src/context/binding.d.ts +38 -0
  16. package/dist/types/src/context/binding.d.ts.map +1 -0
  17. package/dist/types/src/context/index.d.ts +3 -0
  18. package/dist/types/src/context/index.d.ts.map +1 -0
  19. package/dist/types/src/conversation/conversation.d.ts +44 -0
  20. package/dist/types/src/conversation/conversation.d.ts.map +1 -0
  21. package/dist/types/src/conversation/index.d.ts +2 -0
  22. package/dist/types/src/conversation/index.d.ts.map +1 -0
  23. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -1
  24. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +10 -10
  25. package/dist/types/src/index.d.ts +4 -1
  26. package/dist/types/src/index.d.ts.map +1 -1
  27. package/dist/types/src/research/research.d.ts.map +1 -1
  28. package/dist/types/src/session/session.d.ts +25 -3
  29. package/dist/types/src/session/session.d.ts.map +1 -1
  30. package/dist/types/tsconfig.tsbuildinfo +1 -1
  31. package/package.json +21 -30
  32. package/src/blueprint/blueprint.ts +57 -57
  33. package/src/blueprint/index.ts +1 -2
  34. package/src/blueprint/registry.ts +15 -0
  35. package/src/context/binder.ts +101 -0
  36. package/src/context/binding.ts +31 -0
  37. package/src/context/index.ts +6 -0
  38. package/src/conversation/conversation.ts +103 -0
  39. package/src/conversation/index.ts +5 -0
  40. package/src/extraction/extraction-llm-function/extraction-llm-function.ts +2 -1
  41. package/src/index.ts +4 -1
  42. package/src/research/research.ts +3 -2
  43. package/src/session/session.test.ts +48 -2
  44. package/src/session/session.ts +51 -18
  45. package/dist/lib/node/index.cjs +0 -2178
  46. package/dist/lib/node/index.cjs.map +0 -7
  47. package/dist/lib/node/meta.json +0 -1
  48. package/dist/types/src/blueprint/ai.test.d.ts +0 -2
  49. package/dist/types/src/blueprint/ai.test.d.ts.map +0 -1
  50. package/dist/types/src/blueprint/blueprint.test.d.ts +0 -2
  51. package/dist/types/src/blueprint/blueprint.test.d.ts.map +0 -1
  52. package/dist/types/src/blueprint/logger.d.ts +0 -25
  53. package/dist/types/src/blueprint/logger.d.ts.map +0 -1
  54. package/dist/types/src/blueprint/machine.d.ts +0 -143
  55. package/dist/types/src/blueprint/machine.d.ts.map +0 -1
  56. package/dist/types/src/blueprint/test-data.d.ts +0 -3
  57. package/dist/types/src/blueprint/test-data.d.ts.map +0 -1
  58. package/src/blueprint/ai.test.ts +0 -156
  59. package/src/blueprint/blueprint.test.ts +0 -151
  60. package/src/blueprint/logger.ts +0 -155
  61. package/src/blueprint/machine.ts +0 -251
  62. package/src/blueprint/test-data.ts +0 -55
@@ -2,7 +2,7 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Option, Schema } from 'effect';
5
+ import { Array, Option, pipe, Schema } from 'effect';
6
6
 
7
7
  import {
8
8
  AgentStatusReport,
@@ -19,6 +19,8 @@ import {
19
19
  ToolResult,
20
20
  createTool,
21
21
  type ExecutableTool,
22
+ type ToolId,
23
+ type ToolResolver,
22
24
  } from '@dxos/ai';
23
25
  import { type ArtifactDefinition } from '@dxos/artifact';
24
26
  import { Event, synchronized } from '@dxos/async';
@@ -58,14 +60,12 @@ export type ArtifactDiffResolver = (artifacts: { id: ObjectId; lastVersion: Obje
58
60
  >;
59
61
 
60
62
  export type SessionRunOptions = {
61
- client: AiServiceClient;
62
-
63
63
  artifacts: ArtifactDefinition[];
64
64
 
65
65
  /**
66
66
  * Non-artifact specific tools.
67
67
  */
68
- tools: ExecutableTool[];
68
+ tools: ToolId[];
69
69
 
70
70
  history: Message[];
71
71
 
@@ -82,6 +82,14 @@ export type SessionRunOptions = {
82
82
  */
83
83
  requiredArtifactIds?: string[];
84
84
 
85
+ client: AiServiceClient;
86
+ toolResolver: ToolResolver;
87
+
88
+ /**
89
+ * Executable tools that do not go through the tool resolver.
90
+ */
91
+ executableTools?: ExecutableTool[];
92
+
85
93
  /**
86
94
  * @see ArtifactDiffResolver
87
95
  */
@@ -129,6 +137,9 @@ export class AISession {
129
137
  */
130
138
  public readonly update = this._parser.update;
131
139
 
140
+ /**
141
+ * Unparsed events from the underlying generation stream.
142
+ */
132
143
  public readonly streamEvent = this._parser.streamEvent;
133
144
 
134
145
  /**
@@ -138,9 +149,19 @@ export class AISession {
138
149
 
139
150
  /**
140
151
  * Agent self-reporting its status.
152
+ * Triggered by the model.
141
153
  */
142
154
  public readonly statusReport = new Event<AgentStatus>();
143
155
 
156
+ /**
157
+ * Emits when the session is done.
158
+ */
159
+ public readonly done = new Event<void>();
160
+
161
+ /**
162
+ * Emits when the tool reports its status.
163
+ * Triggered by the tool execution function.
164
+ */
144
165
  public readonly toolStatusReport = new Event<{ message: Message; status: AgentStatus }>();
145
166
 
146
167
  constructor(private readonly _options: AiSessionOptions) {
@@ -155,6 +176,11 @@ export class AISession {
155
176
  });
156
177
  }
157
178
 
179
+ /**
180
+ * Runs the AI model loop interacting with tools and artifacts.
181
+ * @param options - The session options.
182
+ * @returns The messages generated by the session, including the user's prompt.
183
+ */
158
184
  @synchronized
159
185
  async run(options: SessionRunOptions): Promise<Message[]> {
160
186
  const systemTools: ExecutableTool[] = [];
@@ -190,13 +216,17 @@ export class AISession {
190
216
  try {
191
217
  let more = false;
192
218
  do {
193
- const tools: ExecutableTool[] = [
194
- ...systemTools,
195
- ...options.tools,
196
- ...options.artifacts
197
- .filter((artifact) => requiredArtifactIds.has(artifact.id))
198
- .flatMap((artifact) => artifact.tools),
199
- ];
219
+ const tools: ExecutableTool[] = pipe(
220
+ [
221
+ ...systemTools,
222
+ ...(await Promise.all(options.tools.map((toolId) => options.toolResolver.resolve(toolId)))),
223
+ ...(options.executableTools ?? []),
224
+ ...options.artifacts
225
+ .filter((artifact) => requiredArtifactIds.has(artifact.id))
226
+ .flatMap((artifact) => artifact.tools),
227
+ ],
228
+ Array.dedupeWith((a, b) => a.id === b.id),
229
+ );
200
230
 
201
231
  log('request', {
202
232
  pending: this._pending.length,
@@ -213,7 +243,7 @@ export class AISession {
213
243
  systemPrompt:
214
244
  options.systemPrompt ??
215
245
  createBaseInstructions({
216
- availableArtifacts: Array.from(requiredArtifactIds),
246
+ availableArtifacts: [...requiredArtifactIds],
217
247
  operationModel: this._options.operationModel,
218
248
  }),
219
249
  });
@@ -258,6 +288,7 @@ export class AISession {
258
288
  }
259
289
  } finally {
260
290
  this._stream = undefined;
291
+ this.done.emit();
261
292
  }
262
293
 
263
294
  if (error) {
@@ -274,7 +305,7 @@ export class AISession {
274
305
  const parser = structuredOutputParser(schema);
275
306
  const result = await this.run({
276
307
  ...options,
277
- tools: [...options.tools, parser.tool],
308
+ executableTools: [...(options.executableTools ?? []), parser.tool],
278
309
  });
279
310
  return parser.getResult(result);
280
311
  }
@@ -290,7 +321,7 @@ export class AISession {
290
321
  const versions = gatherObjectVersions(history);
291
322
 
292
323
  const artifactDiff = await artifactDiffResolver(
293
- Array.from(versions.entries()).map(([id, version]) => ({ id, lastVersion: version })),
324
+ [...versions.entries()].map(([id, version]) => ({ id, lastVersion: version })),
294
325
  );
295
326
 
296
327
  log.info('vision', {
@@ -409,14 +440,11 @@ export class AISession {
409
440
  session.userMessage.on((ev) => this.userMessage.emit(ev));
410
441
 
411
442
  const messages = await session.run({
412
- client: options.client,
413
443
  artifacts: options.artifacts,
414
444
  tools: options.tools,
415
445
  history: options.history,
416
446
  extensions: options.extensions,
417
- generationOptions: options.generationOptions,
418
447
  requiredArtifactIds: step.requiredArtifactIds.slice(),
419
- artifactDiffResolver: options.artifactDiffResolver,
420
448
  prompt: `
421
449
  You are an agent that executes the subtask in a plan.
422
450
 
@@ -439,6 +467,11 @@ export class AISession {
439
467
  Previous step results:
440
468
  ${JSON.stringify(stepResults.at(-1))}.
441
469
  `,
470
+
471
+ generationOptions: options.generationOptions,
472
+ client: options.client,
473
+ toolResolver: options.toolResolver,
474
+ artifactDiffResolver: options.artifactDiffResolver,
442
475
  });
443
476
  const result = messages.at(-1);
444
477
  stepResults.push(result);
@@ -541,7 +574,7 @@ const createArtifactUpdateBlock = (
541
574
  disposition: 'artifact-update',
542
575
  text: `
543
576
  The following artifacts have been updated since the last message:
544
- ${Array.from(artifactDiff.entries())
577
+ ${[...artifactDiff.entries()]
545
578
  .map(([id, { diff }]) => `<changed-artifact id="${id}">${diff ? `\n${diff}` : ''}</changed-artifact>`)
546
579
  .join('\n')}
547
580
  `,