@dxos/assistant 0.8.4-main.5acf9ea → 0.8.4-main.5ea62a8

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 (145) hide show
  1. package/dist/lib/browser/chunk-6WRGDEMH.mjs +89 -0
  2. package/dist/lib/browser/chunk-6WRGDEMH.mjs.map +7 -0
  3. package/dist/lib/browser/chunk-N4O3C3T5.mjs +396 -0
  4. package/dist/lib/browser/chunk-N4O3C3T5.mjs.map +7 -0
  5. package/dist/lib/browser/extraction/index.mjs +268 -0
  6. package/dist/lib/browser/extraction/index.mjs.map +7 -0
  7. package/dist/lib/browser/index.mjs +284 -1609
  8. package/dist/lib/browser/index.mjs.map +4 -4
  9. package/dist/lib/browser/meta.json +1 -1
  10. package/dist/lib/browser/named-entity-recognition-O46UTCLR.mjs +13 -0
  11. package/dist/lib/browser/named-entity-recognition-O46UTCLR.mjs.map +7 -0
  12. package/dist/lib/node-esm/chunk-3UU5V4FS.mjs +91 -0
  13. package/dist/lib/node-esm/chunk-3UU5V4FS.mjs.map +7 -0
  14. package/dist/lib/node-esm/chunk-QVJ7EAM2.mjs +398 -0
  15. package/dist/lib/node-esm/chunk-QVJ7EAM2.mjs.map +7 -0
  16. package/dist/lib/node-esm/extraction/index.mjs +269 -0
  17. package/dist/lib/node-esm/extraction/index.mjs.map +7 -0
  18. package/dist/lib/node-esm/index.mjs +283 -1609
  19. package/dist/lib/node-esm/index.mjs.map +4 -4
  20. package/dist/lib/node-esm/meta.json +1 -1
  21. package/dist/lib/node-esm/named-entity-recognition-W4VMRCH6.mjs +14 -0
  22. package/dist/lib/node-esm/named-entity-recognition-W4VMRCH6.mjs.map +7 -0
  23. package/dist/types/src/{context/binding.d.ts → conversation/context.d.ts} +45 -3
  24. package/dist/types/src/conversation/context.d.ts.map +1 -0
  25. package/dist/types/src/conversation/conversation.d.ts +25 -21
  26. package/dist/types/src/conversation/conversation.d.ts.map +1 -1
  27. package/dist/types/src/conversation/index.d.ts +1 -0
  28. package/dist/types/src/conversation/index.d.ts.map +1 -1
  29. package/dist/types/src/diff.d.ts +15 -0
  30. package/dist/types/src/diff.d.ts.map +1 -0
  31. package/dist/types/src/diff.test.d.ts +2 -0
  32. package/dist/types/src/diff.test.d.ts.map +1 -0
  33. package/dist/types/src/errors.d.ts +41 -4
  34. package/dist/types/src/errors.d.ts.map +1 -1
  35. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -1
  36. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +59 -27
  37. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
  38. package/dist/types/src/extraction/extraction.d.ts +244 -214
  39. package/dist/types/src/extraction/extraction.d.ts.map +1 -1
  40. package/dist/types/src/functions/services.d.ts +4 -3
  41. package/dist/types/src/functions/services.d.ts.map +1 -1
  42. package/dist/types/src/index.d.ts +2 -3
  43. package/dist/types/src/index.d.ts.map +1 -1
  44. package/dist/types/src/session/artifact-diff.d.ts +27 -0
  45. package/dist/types/src/session/artifact-diff.d.ts.map +1 -0
  46. package/dist/types/src/session/error-handling.d.ts +4 -0
  47. package/dist/types/src/session/error-handling.d.ts.map +1 -0
  48. package/dist/types/src/session/format.d.ts +182 -0
  49. package/dist/types/src/session/format.d.ts.map +1 -0
  50. package/dist/types/src/session/format.test.d.ts +2 -0
  51. package/dist/types/src/session/format.test.d.ts.map +1 -0
  52. package/dist/types/src/session/index.d.ts +3 -0
  53. package/dist/types/src/session/index.d.ts.map +1 -1
  54. package/dist/types/src/session/observer.d.ts +22 -0
  55. package/dist/types/src/session/observer.d.ts.map +1 -0
  56. package/dist/types/src/session/session.d.ts +23 -236
  57. package/dist/types/src/session/session.d.ts.map +1 -1
  58. package/dist/types/src/session/toolkit.d.ts +14 -0
  59. package/dist/types/src/session/toolkit.d.ts.map +1 -0
  60. package/dist/types/src/session/version-pin.d.ts +2 -2
  61. package/dist/types/src/session/version-pin.d.ts.map +1 -1
  62. package/dist/types/src/templates/index.d.ts +1 -1
  63. package/dist/types/src/templates/system.d.ts +18 -0
  64. package/dist/types/src/templates/system.d.ts.map +1 -0
  65. package/dist/types/src/types.d.ts +3 -0
  66. package/dist/types/src/types.d.ts.map +1 -1
  67. package/dist/types/tsconfig.tsbuildinfo +1 -1
  68. package/package.json +45 -29
  69. package/src/conversation/context.ts +154 -0
  70. package/src/conversation/conversation.ts +80 -53
  71. package/src/conversation/index.ts +1 -0
  72. package/src/diff.test.ts +43 -0
  73. package/src/diff.ts +77 -0
  74. package/src/errors.ts +5 -0
  75. package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +1 -0
  76. package/src/extraction/extraction-llm-function/extraction-llm-function.ts +7 -7
  77. package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +4 -3
  78. package/src/extraction/extraction-ner-function/extraction-ner-function.ts +2 -2
  79. package/src/extraction/quotes.test.ts +1 -1
  80. package/src/extraction/quotes.ts +1 -1
  81. package/src/functions/services.ts +37 -17
  82. package/src/index.ts +2 -3
  83. package/src/session/artifact-diff.ts +41 -0
  84. package/src/session/error-handling.ts +36 -0
  85. package/src/session/format.test.ts +60 -0
  86. package/src/session/format.ts +139 -0
  87. package/src/session/index.ts +3 -0
  88. package/src/session/observer.ts +58 -0
  89. package/src/session/session.test.ts +36 -38
  90. package/src/session/session.ts +114 -249
  91. package/src/session/toolkit.ts +42 -0
  92. package/src/session/version-pin.ts +4 -4
  93. package/src/summarization/summarization.test.ts +5 -7
  94. package/src/templates/format.tpl +59 -0
  95. package/src/templates/index.ts +1 -1
  96. package/src/templates/system.tpl +50 -0
  97. package/src/templates/system.ts +41 -0
  98. package/src/types.ts +10 -5
  99. package/src/typings.d.ts +5 -0
  100. package/dist/types/bin/chat.d.ts +0 -2
  101. package/dist/types/bin/chat.d.ts.map +0 -1
  102. package/dist/types/src/context/binder.d.ts +0 -34
  103. package/dist/types/src/context/binder.d.ts.map +0 -1
  104. package/dist/types/src/context/binding.d.ts.map +0 -1
  105. package/dist/types/src/context/index.d.ts +0 -3
  106. package/dist/types/src/context/index.d.ts.map +0 -1
  107. package/dist/types/src/research/exa.d.ts +0 -12
  108. package/dist/types/src/research/exa.d.ts.map +0 -1
  109. package/dist/types/src/research/graph.d.ts +0 -51
  110. package/dist/types/src/research/graph.d.ts.map +0 -1
  111. package/dist/types/src/research/graph.test.d.ts +0 -2
  112. package/dist/types/src/research/graph.test.d.ts.map +0 -1
  113. package/dist/types/src/research/index.d.ts +0 -4
  114. package/dist/types/src/research/index.d.ts.map +0 -1
  115. package/dist/types/src/research/research.d.ts +0 -10
  116. package/dist/types/src/research/research.d.ts.map +0 -1
  117. package/dist/types/src/research/research.test.d.ts +0 -2
  118. package/dist/types/src/research/research.test.d.ts.map +0 -1
  119. package/dist/types/src/templates/prompt.d.ts +0 -30
  120. package/dist/types/src/templates/prompt.d.ts.map +0 -1
  121. package/dist/types/src/testing/exa-search-1748337321991.d.ts +0 -38
  122. package/dist/types/src/testing/exa-search-1748337321991.d.ts.map +0 -1
  123. package/dist/types/src/testing/exa-search-1748337331526.d.ts +0 -37
  124. package/dist/types/src/testing/exa-search-1748337331526.d.ts.map +0 -1
  125. package/dist/types/src/testing/exa-search-1748337344119.d.ts +0 -58
  126. package/dist/types/src/testing/exa-search-1748337344119.d.ts.map +0 -1
  127. package/dist/types/src/testing/index.d.ts +0 -3
  128. package/dist/types/src/testing/index.d.ts.map +0 -1
  129. package/src/context/binder.ts +0 -103
  130. package/src/context/binding.ts +0 -32
  131. package/src/context/index.ts +0 -6
  132. package/src/research/exa.ts +0 -97
  133. package/src/research/graph.test.ts +0 -64
  134. package/src/research/graph.ts +0 -369
  135. package/src/research/index.ts +0 -7
  136. package/src/research/instructions-research.tpl +0 -98
  137. package/src/research/research.test.ts +0 -190
  138. package/src/research/research.ts +0 -87
  139. package/src/templates/assistant.tpl +0 -111
  140. package/src/templates/prompt.ts +0 -45
  141. package/src/testing/exa-search-1748337321991.ts +0 -131
  142. package/src/testing/exa-search-1748337331526.ts +0 -144
  143. package/src/testing/exa-search-1748337344119.ts +0 -133
  144. package/src/testing/index.ts +0 -11
  145. package/src/typedefs.d.ts +0 -8
@@ -2,44 +2,52 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { AiLanguageModel, AiToolkit, type AiError, type AiResponse, type AiTool } from '@effect/ai';
6
- import { Chunk, Context, Effect, Option, pipe, Stream, type Schema } from 'effect';
7
- import { Array, String } from 'effect';
5
+ import { type AiError, AiLanguageModel, type AiTool, type AiToolkit } from '@effect/ai';
6
+ import { Chunk, Effect, type Schema, Stream } from 'effect';
8
7
 
9
8
  import {
9
+ type AiInputPreprocessingError,
10
10
  AiParser,
11
11
  AiPreprocessor,
12
- getToolCalls,
13
- runTool,
14
- ToolExecutionService,
15
- ToolResolverService,
16
- type AgentStatus,
17
- type AiInputPreprocessingError,
18
12
  type AiToolNotFoundError,
19
- type GenerationStream,
13
+ type ToolExecutionService,
14
+ type ToolResolverService,
15
+ callTool,
16
+ getToolCalls,
20
17
  } from '@dxos/ai';
21
- import { Event } from '@dxos/async';
22
18
  import { type Blueprint } from '@dxos/blueprints';
23
19
  import { todo } from '@dxos/debug';
24
20
  import { Obj } from '@dxos/echo';
25
- import { ObjectVersion } from '@dxos/echo-db';
26
- import { type ObjectId } from '@dxos/echo-schema';
27
- import { DatabaseService } from '@dxos/functions';
21
+ import { TracingService } from '@dxos/functions';
28
22
  import { log } from '@dxos/log';
29
- import { DataType, type ContentBlock } from '@dxos/schema';
23
+ import { DataType } from '@dxos/schema';
30
24
 
31
- import { AiAssistantError } from '../errors';
25
+ import { type AiAssistantError } from '../errors';
32
26
 
33
- export type AiSessionOptions = {};
27
+ import { mapAiError } from './error-handling';
28
+ import { formatSystemPrompt, formatUserPrompt } from './format';
29
+ import { GenerationObserver } from './observer';
30
+
31
+ export type AiSessionRunError = AiError.AiError | AiInputPreprocessingError | AiToolNotFoundError | AiAssistantError;
34
32
 
35
- export type SessionRunOptions<Tools extends AiTool.Any> = {
33
+ export type AiSessionRunRequirements =
34
+ | AiLanguageModel.AiLanguageModel
35
+ | ToolExecutionService
36
+ | ToolResolverService
37
+ | TracingService;
38
+
39
+ export type AiSessionRunParams<Tools extends AiTool.Any> = {
36
40
  prompt: string;
37
- history: DataType.Message[];
38
- systemPrompt?: string;
39
- toolkit?: AiToolkit.AiToolkit<Tools>;
41
+ system?: string;
42
+ history?: DataType.Message[];
43
+ objects?: Obj.Any[];
40
44
  blueprints?: Blueprint.Blueprint[];
45
+ toolkit?: AiToolkit.ToHandler<Tools>;
46
+ observer?: GenerationObserver;
41
47
  };
42
48
 
49
+ export type AiSessionOptions = {};
50
+
43
51
  /**
44
52
  * Contains message history, tools, current context.
45
53
  * Current context means the state of the app, time of day, and other contextual information.
@@ -51,151 +59,114 @@ export type SessionRunOptions<Tools extends AiTool.Any> = {
51
59
  * Could be personal or shared.
52
60
  */
53
61
  export class AiSession {
54
- // TODO(burdon): Move to conversation.
62
+ /** Prevents concurrent execution of session. */
55
63
  private readonly _semaphore = Effect.runSync(Effect.makeSemaphore(1));
56
64
 
57
- /** Pending messages (incl. the current user request). */
58
- private _pending: DataType.Message[] = [];
59
-
60
- /** Current streaming response. */
61
- private _stream: GenerationStream | undefined;
62
-
63
- /** Prior history from queue. */
65
+ /** Message history from queue. */
66
+ // TODO(burdon): Evolve into supporting a git-like graph of messages.
64
67
  private _history: DataType.Message[] = [];
65
68
 
66
- /**
67
- * New message.
68
- */
69
- public readonly message = new Event<DataType.Message>();
70
-
71
- /**
72
- * Complete block added to Message.
73
- */
74
- public readonly block = new Event<ContentBlock.Any>();
75
-
76
- /**
77
- * Update partial block (while streaming).
78
- */
79
- public readonly update = new Event<ContentBlock.Any>();
80
-
81
- /**
82
- * Unparsed events from the underlying generation stream.
83
- */
84
- public readonly streamEvent = new Event<AiResponse.Part>();
85
-
86
- /**
87
- * User prompt or tool result.
88
- */
89
- public readonly userMessage = new Event<DataType.Message>();
90
-
91
- /**
92
- * Agent self-reporting its status.
93
- * Triggered by the model.
94
- */
95
- public readonly statusReport = new Event<AgentStatus>();
96
-
97
- /**
98
- * Emits when the session is done.
99
- */
100
- public readonly done = new Event<void>();
69
+ /** Pending messages for this session (incl. the current prompt). */
70
+ private _pending: DataType.Message[] = [];
101
71
 
102
72
  constructor(private readonly _options: AiSessionOptions = {}) {}
103
73
 
104
- /**
105
- * Runs the AI model loop interacting with tools and artifacts.
106
- * @param options - The session options.
107
- * @returns The messages generated by the session, including the user's prompt.
108
- */
109
- // TODO(dmaretskyi): Toolkit context doesn't get added to the effect type.
110
- run = <Tools extends AiTool.Any>(
111
- options: SessionRunOptions<Tools>,
112
- ): Effect.Effect<
113
- DataType.Message[],
114
- AiAssistantError | AiInputPreprocessingError | AiToolNotFoundError | AiError.AiError,
115
- AiLanguageModel.AiLanguageModel | ToolResolverService | ToolExecutionService | AiTool.ToHandler<Tools>
116
- > =>
74
+ run = <Tools extends AiTool.Any>({
75
+ prompt,
76
+ system: systemTemplate,
77
+ history = [],
78
+ objects = [],
79
+ blueprints = [],
80
+ toolkit,
81
+ observer = GenerationObserver.noop(),
82
+ }: AiSessionRunParams<Tools>): Effect.Effect<DataType.Message[], AiSessionRunError, AiSessionRunRequirements> =>
117
83
  Effect.gen(this, function* () {
118
- this._history = [...options.history];
84
+ this._history = [...history];
85
+ this._pending = [];
86
+ const pending = this._pending;
87
+
88
+ const submitMessage = Effect.fnUntraced(function* (message: DataType.Message) {
89
+ pending.push(message);
90
+ yield* observer.onMessage(message);
91
+ yield* TracingService.emitConverationMessage(message);
92
+ return message;
93
+ });
119
94
 
120
- const promptMessages = yield* this._formatUserPrompt(options.prompt, options.history);
121
- this._pending = [promptMessages];
122
- this.userMessage.emit(promptMessages);
95
+ // Submit the prompt.
96
+ // TODO(burdon): Remove if cancelled?
97
+ const promptMessage = yield* submitMessage(yield* formatUserPrompt({ prompt, history }));
123
98
 
124
- // Potential tool use loop.
99
+ // Generate system and prompt messages.
100
+ const system = yield* formatSystemPrompt({ system: systemTemplate, blueprints, objects });
101
+
102
+ // Tool call loop.
125
103
  do {
126
- log('request', {
104
+ log.info('request', {
105
+ prompt: promptMessage,
106
+ system: { snippet: createSnippet(system), length: system.length },
127
107
  pending: this._pending.length,
128
108
  history: this._history.length,
129
- tools: (Object.values(options.toolkit?.tools ?? {}) as AiTool.Any[]).map((tool: AiTool.Any) => tool.name),
109
+ objects: objects?.length ?? 0,
130
110
  });
131
111
 
132
112
  const prompt = yield* AiPreprocessor.preprocessAiInput([...this._history, ...this._pending]);
133
113
 
134
- const blueprints = options.blueprints ?? [];
135
-
136
- // Build system prompt from blueprint templates.
137
- // TODO(dmaretskyi): Loading BP from the Database should be done at the higher level. We need a type for the resolved blueprint.
138
- const systemPrompt = yield* pipe(
139
- blueprints,
140
- Effect.forEach((blueprint) => Effect.succeed(blueprint.instructions)),
141
- Effect.flatMap(Effect.forEach((template) => DatabaseService.loadRef(template.source))),
142
- Effect.map(Array.map((template) => `\n\n<blueprint>${template.content}</blueprint>`)),
143
- Effect.map(Array.reduce(options.systemPrompt ?? '', String.concat)),
144
- );
145
-
146
- // Build a combined toolkit from the blueprint tools and the provided toolkit.
147
- const blueprintToolkit = yield* ToolResolverService.resolveToolkit(blueprints.flatMap((bp) => bp.tools));
148
- const blueprintToolkitHandler: Context.Context<AiTool.ToHandler<AiTool.Any>> =
149
- yield* blueprintToolkit.toContext(yield* ToolExecutionService.handlersFor(blueprintToolkit));
150
- const toolkit = options.toolkit != null ? AiToolkit.merge(options.toolkit, blueprintToolkit) : blueprintToolkit;
151
- const toolkitWithBlueprintHandlers = yield* toolkit.pipe(
152
- Effect.provide(blueprintToolkitHandler) as any,
153
- ) as Effect.Effect<AiToolkit.ToHandler<any>, never, AiTool.ToHandler<Tools>>;
154
-
155
- log.info('run', {
156
- systemPrompt: [systemPrompt.slice(0, 32), '...', systemPrompt.slice(-32), systemPrompt.length].join(' '),
157
- tools: Object.keys(blueprintToolkit.tools),
158
- });
159
-
114
+ // Execute the stream request.
160
115
  const blocks = yield* AiLanguageModel.streamText({
161
116
  prompt,
162
- system: systemPrompt,
163
- toolkit: toolkitWithBlueprintHandlers,
117
+ system,
118
+ toolkit,
164
119
  disableToolCallResolution: true,
165
120
  }).pipe(
166
- AiParser.parseGptStream({
167
- onBlock: (block) =>
168
- Effect.gen(this, function* () {
169
- if (block.pending) {
170
- this.update.emit(block);
171
- } else {
172
- this.block.emit(block);
173
- }
174
- }),
175
- onPart: (part) =>
176
- Effect.gen(this, function* () {
177
- this.streamEvent.emit(part);
178
- }),
121
+ Stream.catchTag(
122
+ 'AiError',
123
+ Effect.fnUntraced(function* (err) {
124
+ return yield* Effect.fail(yield* mapAiError(err));
125
+ }),
126
+ ),
127
+ AiParser.parseResponse({
128
+ onBegin: () => observer.onBegin(),
129
+ onBlock: (block) => observer.onBlock(block),
130
+ onPart: (part) => observer.onPart(part),
131
+ onEnd: (summary) => observer.onEnd(summary),
179
132
  }),
180
133
  Stream.runCollect,
181
134
  Effect.map(Chunk.toArray),
182
135
  );
183
136
 
184
- const response = Obj.make(DataType.Message, {
185
- created: new Date().toISOString(),
186
- sender: { role: 'assistant' },
187
- blocks,
188
- });
189
- this._pending.push(response);
190
- this.message.emit(response);
137
+ // Create the response message.
138
+ const response = yield* submitMessage(
139
+ Obj.make(DataType.Message, {
140
+ created: new Date().toISOString(),
141
+ sender: { role: 'assistant' },
142
+ blocks,
143
+ }),
144
+ );
191
145
 
146
+ // Parse the response for tool calls.
192
147
  const toolCalls = getToolCalls(response);
193
148
  if (toolCalls.length === 0) {
194
149
  break;
150
+ } else if (!toolkit) {
151
+ throw new Error('No toolkit provided'); // TODO(burdon): Throw user error?
195
152
  }
196
153
 
197
- const toolResults = yield* runTools(toolCalls, toolkitWithBlueprintHandlers as any);
198
- this._pending.push(
154
+ // Execute the tool calls.
155
+ // TODO(burdon): Retry errors? Write result when each completes individually?
156
+ const toolResults = yield* Effect.forEach(toolCalls, (toolCall) => {
157
+ return callTool(toolkit, toolCall).pipe(
158
+ Effect.provide(
159
+ TracingService.layerSubframe((context) => ({
160
+ ...context,
161
+ parentMessage: response.id,
162
+ toolCallId: toolCall.toolCallId,
163
+ })),
164
+ ),
165
+ );
166
+ });
167
+
168
+ // Add to queue and continue loop.
169
+ yield* submitMessage(
199
170
  Obj.make(DataType.Message, {
200
171
  created: new Date().toISOString(),
201
172
  sender: { role: 'user' },
@@ -204,12 +175,17 @@ export class AiSession {
204
175
  );
205
176
  } while (true);
206
177
 
178
+ log('done', { pending: this._pending.length });
207
179
  return this._pending;
208
180
  }).pipe(this._semaphore.withPermits(1), Effect.withSpan('AiSession.run'));
209
181
 
182
+ /**
183
+ * @deprecated
184
+ */
185
+ // TODO(burdon): Implement or remove.
210
186
  async runStructured<S extends Schema.Schema.AnyNoContext>(
211
- schema: S,
212
- options: SessionRunOptions<AiTool.Any>,
187
+ _schema: S,
188
+ _options: AiSessionRunParams<AiTool.Any>,
213
189
  ): Promise<Schema.Schema.Type<S>> {
214
190
  return todo();
215
191
  // const parser = structuredOutputParser(schema);
@@ -219,118 +195,7 @@ export class AiSession {
219
195
  // });
220
196
  // return parser.getResult(result);
221
197
  }
222
-
223
- private _formatUserPrompt = (prompt: string, history: DataType.Message[]) =>
224
- Effect.gen(function* () {
225
- const prelude: ContentBlock.Any[] = [];
226
-
227
- // TODO(dmaretskyi): Evaluate other approaches as `serviceOption` isn't represented in the type system.
228
- const artifactDiffResolver = yield* Effect.serviceOption(ArtifactDiffResolver);
229
- if (Option.isSome(artifactDiffResolver)) {
230
- const versions = gatherObjectVersions(history);
231
-
232
- const artifactDiff = yield* Effect.tryPromise({
233
- try: () =>
234
- artifactDiffResolver.value.resolve(
235
- [...versions.entries()].map(([id, version]) => ({ id, lastVersion: version })),
236
- ),
237
- catch: AiAssistantError.wrap('Artifact diff resolution error'),
238
- });
239
-
240
- log.info('vision', {
241
- artifactDiff,
242
- versions,
243
- });
244
-
245
- for (const [id, { version }] of [...artifactDiff.entries()]) {
246
- if (ObjectVersion.equals(version, versions.get(id)!)) {
247
- artifactDiff.delete(id);
248
- continue;
249
- }
250
-
251
- prelude.push({ _tag: 'anchor', objectId: id, version });
252
- }
253
- if (artifactDiff.size > 0) {
254
- prelude.push(createArtifactUpdateBlock(artifactDiff));
255
- }
256
- }
257
-
258
- return Obj.make(DataType.Message, {
259
- created: new Date().toISOString(),
260
- sender: { role: 'user' },
261
- blocks: [...prelude, { _tag: 'text', text: prompt }],
262
- });
263
- });
264
-
265
- abort(): void {
266
- this._stream?.abort();
267
- }
268
- }
269
-
270
- const gatherObjectVersions = (messages: DataType.Message[]): Map<ObjectId, ObjectVersion> => {
271
- const artifactIds = new Map<ObjectId, ObjectVersion>();
272
- for (const message of messages) {
273
- for (const block of message.blocks) {
274
- if (block._tag === 'anchor') {
275
- artifactIds.set(block.objectId, block.version as ObjectVersion);
276
- }
277
- }
278
- }
279
-
280
- return artifactIds;
281
- };
282
-
283
- const createArtifactUpdateBlock = (
284
- artifactDiff: Map<ObjectId, { version: ObjectVersion; diff?: string }>,
285
- ): ContentBlock.Any => {
286
- return {
287
- _tag: 'text',
288
- // TODO(dmaretskyi): Does this need to be a special content-block?
289
- disposition: 'artifact-update',
290
- text: `
291
- The following artifacts have been updated since the last message:
292
- ${[...artifactDiff.entries()]
293
- .map(([id, { diff }]) => `<changed-artifact id="${id}">${diff ? `\n${diff}` : ''}</changed-artifact>`)
294
- .join('\n')}
295
- `,
296
- };
297
- };
298
-
299
- /**
300
- * Resolves artifact ids to their versions.
301
- * Used to give the model a sense of the changes to the artifacts made by users during the conversation.
302
- * The artifacts versions are pinned in the history, and whenever the artifact changes in-between assistant's steps,
303
- * a diff is inserted into the conversation.
304
- *
305
- * Can be optionally provided to the session run call.
306
- */
307
- // TODO(dmaretskyi): Convert to Context.Reference
308
- export class ArtifactDiffResolver extends Context.Tag('@dxos/assistant/ArtifactDiffResolver')<
309
- ArtifactDiffResolver,
310
- ArtifactDiffResolver.Service
311
- >() {}
312
-
313
- export namespace ArtifactDiffResolver {
314
- export type Service = {
315
- resolve: (artifacts: { id: ObjectId; lastVersion: ObjectVersion }[]) => Promise<
316
- Map<
317
- ObjectId,
318
- {
319
- version: ObjectVersion;
320
- diff?: string;
321
- }
322
- >
323
- >;
324
- };
325
198
  }
326
199
 
327
- const runTools: <Tools extends AiTool.Any>(
328
- toolCalls: ContentBlock.ToolCall[],
329
- toolkit: AiToolkit.AiToolkit<Tools>,
330
- ) => Effect.Effect<ContentBlock.ToolResult[], AiError.AiError, AiTool.ToHandler<Tools>> = Effect.fn('runTools')(
331
- function* (toolCalls, toolkit) {
332
- const toolkitWithHandlers = Effect.isEffect(toolkit) ? yield* toolkit : toolkit;
333
-
334
- return yield* Effect.forEach(toolCalls, (toolCall) => runTool(toolkitWithHandlers, toolCall));
335
- },
336
- );
200
+ const createSnippet = (text: string, len = 32) =>
201
+ text.length <= len * 2 ? text : [text.slice(0, len), '...', text.slice(-len)].join('');
@@ -0,0 +1,42 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { type AiTool, AiToolkit } from '@effect/ai';
6
+ import { type Context, Effect } from 'effect';
7
+
8
+ import { ToolExecutionService, type ToolId, ToolResolverService } from '@dxos/ai';
9
+ import { type Blueprint } from '@dxos/blueprints';
10
+ import { isNotFalsy } from '@dxos/util';
11
+
12
+ export type ToolkitParams<Tools extends AiTool.Any> = {
13
+ toolkit?: AiToolkit.AiToolkit<Tools>;
14
+ toolIds?: ToolId[];
15
+ blueprints?: Blueprint.Blueprint[];
16
+ };
17
+
18
+ /**
19
+ * Build a combined toolkit from the blueprint tools and the provided toolkit.
20
+ */
21
+ export const createToolkit = <Tools extends AiTool.Any>({
22
+ toolkit: toolkitParam,
23
+ toolIds = [],
24
+ blueprints = [],
25
+ }: ToolkitParams<Tools>) =>
26
+ Effect.gen(function* () {
27
+ const blueprintToolkit = yield* ToolResolverService.resolveToolkit([
28
+ ...blueprints.flatMap(({ tools }) => tools),
29
+ ...toolIds,
30
+ ]);
31
+
32
+ const blueprintToolkitHandler: Context.Context<AiTool.ToHandler<AiTool.Any>> = yield* blueprintToolkit.toContext(
33
+ ToolExecutionService.handlersFor(blueprintToolkit),
34
+ );
35
+
36
+ const toolkit = AiToolkit.merge(...[toolkitParam, blueprintToolkit].filter(isNotFalsy));
37
+ return yield* toolkit.pipe(Effect.provide(blueprintToolkitHandler)) as Effect.Effect<
38
+ AiToolkit.ToHandler<any>,
39
+ never,
40
+ AiTool.ToHandler<Tools>
41
+ >;
42
+ });
@@ -6,7 +6,7 @@ import { Schema } from 'effect';
6
6
 
7
7
  import { type ObjectVersion } from '@dxos/echo-db';
8
8
  import { getVersion } from '@dxos/echo-db';
9
- import { ObjectId, type BaseEchoObject } from '@dxos/echo-schema';
9
+ import { type AnyEchoObject, ObjectId } from '@dxos/echo-schema';
10
10
  import { type ContentBlock } from '@dxos/schema';
11
11
 
12
12
  // TODO(dmaretskyi): Extract.
@@ -15,7 +15,7 @@ const ObjectVersionSchema = Schema.Unknown as Schema.Schema<ObjectVersion>;
15
15
  const VersionPinSchema = Schema.Struct({
16
16
  // TODO(dmaretskyi): Use Ref when those support encoding.
17
17
  objectId: ObjectId,
18
- // TODO(dmaretskyi): Could be opaque
18
+ // TODO(dmaretskyi): Could be opaque.
19
19
  version: ObjectVersionSchema,
20
20
  });
21
21
 
@@ -27,11 +27,11 @@ export interface VersionPin extends Schema.Schema.Type<typeof VersionPinSchema>
27
27
 
28
28
  export const VersionPin: typeof VersionPinSchema & {
29
29
  DISPOSITION: 'version-pin';
30
- fromObject: (object: BaseEchoObject) => VersionPin;
30
+ fromObject: (object: AnyEchoObject) => VersionPin;
31
31
  createBlock: (pin: VersionPin) => ContentBlock.Any;
32
32
  } = class extends VersionPinSchema {
33
33
  static readonly DISPOSITION = 'version-pin';
34
- static fromObject(object: BaseEchoObject): VersionPin {
34
+ static fromObject(object: AnyEchoObject): VersionPin {
35
35
  return VersionPin.make({
36
36
  objectId: object.id,
37
37
  version: getVersion(object),
@@ -2,23 +2,21 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- // TODO(burdon): Fix.
5
+ // ISSUE(burdon): defineFunction
6
6
  // @ts-nocheck
7
7
 
8
- import { pipe, Schema } from 'effect';
8
+ import { Schema, pipe } from 'effect';
9
9
  import { beforeAll, describe, test } from 'vitest';
10
10
 
11
11
  import { AiService, MixedStreamParser } from '@dxos/ai';
12
12
  import { Obj } from '@dxos/echo';
13
13
  import { type EchoDatabase } from '@dxos/echo-db';
14
14
  import { EchoTestBuilder } from '@dxos/echo-db/testing';
15
- import { defineFunction, FunctionExecutor, ServiceContainer } from '@dxos/functions';
15
+ import { FunctionExecutor, ServiceContainer, defineFunction } from '@dxos/functions';
16
16
  import { type ContentBlock, DataType } from '@dxos/schema';
17
17
  import { createTestData } from '@dxos/schema/testing';
18
18
  import { trim } from '@dxos/util';
19
19
 
20
- const REMOTE_AI = true;
21
-
22
20
  const summarizationFn = defineFunction({
23
21
  description: 'Summarize a document',
24
22
  inputSchema: Schema.Struct({
@@ -27,7 +25,7 @@ const summarizationFn = defineFunction({
27
25
  }),
28
26
  outputSchema: DataType.Text,
29
27
  handler: async ({ data: { document, transcript }, context }) => {
30
- const ai = context.getService(AiService);
28
+ const ai = context.getService(AiService.AiService);
31
29
  const result = await new MixedStreamParser().parse(
32
30
  await ai.client.execStream({
33
31
  model: '@anthropic/claude-3-5-haiku-20241022',
@@ -92,7 +90,7 @@ const refinementFn = defineFunction({
92
90
  summary: DataType.Text,
93
91
  }),
94
92
  handler: async ({ data: { summaries }, context }) => {
95
- const ai = context.getService(AiService);
93
+ const ai = context.getService(AiService.AiService);
96
94
  const result = await new MixedStreamParser().parse(
97
95
  await ai.client.execStream({
98
96
  model: '@anthropic/claude-3-5-haiku-20241022',
@@ -0,0 +1,59 @@
1
+ {{! System Core }}
2
+
3
+ ## Input Formats
4
+
5
+ ### Blueprints
6
+
7
+ You may be provided with Blueprint definitions that include specific instructions and tools for a given set of tasks.
8
+ Blue print instructions will be enclosed in a <blueprint> tag.
9
+
10
+ ### Context objects
11
+
12
+ You may be provided with references to objects that are relevant to the current prompt.
13
+ Objects will be in the form of a <object> tag with a <dxn> element that is the unique key of the object required by many tools.
14
+ The <typename> element is the type of the object.
15
+
16
+ <object>
17
+ <dxn>object-id</dxn>
18
+ <typename>example.com/type/TypeName</typename>
19
+ </object>
20
+
21
+ ## Response Formats
22
+
23
+ It is very important to respond in the correct format.
24
+ You response must be valid Markdown.
25
+ In some cases you should respond with well-formed XML tags along with the content.
26
+
27
+ {{#if cot}}
28
+ Before responding, explain your reasoning and include your detailed chain-of-thought enclosed in a <cot> tag.
29
+ The <cot> tag should be the first thing in your response.
30
+ {{/if}}
31
+
32
+ ## Block references
33
+
34
+ You can reply with a block reference for objects if you have the DXN. For example:
35
+
36
+ <object>
37
+ <dxn>dxn:queue:data:B6INSIBY3CBEF4M5VZRYBCMAHQMPYK5AJ:01K24XMVHSZHS97SG1VTVQDM5Z:01K24XPK464FSCKVQJAB2H662M</dxn>
38
+ </object>
39
+ <object>
40
+ <dxn>dxn:echo:@:01K24XPK464FSCKVQJAB2H662M</dxn>
41
+ </object>
42
+
43
+ ### Suggestions
44
+
45
+ Each suggestion must in the form of a proposed action enclosed in a <suggest> tag.
46
+ Example: <suggest>Show on a map</suggest>
47
+
48
+ ### Multiple choice questions
49
+
50
+ You may ask multiple choice questions by wrapping consise options inside <option> tags inside a well formed <select> tag.
51
+ Example: <select><option>Yes</option><option>No</option></select>
52
+
53
+ ## Toolkit
54
+
55
+ If the user asks for a list of tools, then just emit a single self-closing <toolkit/> tag instead of listing the tools.
56
+
57
+ ## Context
58
+
59
+ The current date and time is {{DATETIME}}.
@@ -2,4 +2,4 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- export * from './prompt';
5
+ export * from './system';
@@ -0,0 +1,50 @@
1
+ {{! System Prompt }}
2
+
3
+ You are a helpful assistant called Kai.
4
+ Your name is Kai and you are powered by different language models.
5
+ You were created by DXOS and operate in Composer and on the DXOS EDGE network.
6
+ In your initial greeting state your name and make some suggestions based on the current context objects.
7
+
8
+ You are an advanced AI assistant capable of creating and managing artifacts from provided data and tools.
9
+ Your task is to process user commands and questions and decide how best to respond.
10
+ In general be concise and direct.
11
+
12
+ Follow these instructions carefully.
13
+
14
+ ## Include the following steps
15
+
16
+ - Analyze the structure and type of the content in the user's message.
17
+ - Determine if you complete the task using the available blueprint definitions?
18
+ - If you can't complete the task using the available blueprint definitions, query the list of available blueprint definitions using the appropriate tool.
19
+ - Identify which blueprint definitions are relevant to the user's request.
20
+ - Evaluate the potential benefits of creating an artifact vs. normal processing for each identified element.
21
+ - Make a final decision on whether to create an artifact and explain your reasoning.
22
+ - Are the required blueprint definitions already available?
23
+ - If not, select which blueprint definition(s) will be the most relevant and require them using the require_artifact_definitions tool.
24
+ - The require'd artifact tools will be available for use after require.
25
+ - If creating an artifact, outline how you will structure it within the response.
26
+ - Decide if the artifact needs to be shown to the user.
27
+ - Call the show tool to show the artifact to the user.
28
+ - Your reasoning must include: whether to use artifacts or not, to create one or query, whether to show the artifact to the user, and how to structure the response.
29
+
30
+ ## Blueprints
31
+
32
+ - Determine if the interaction involves an artifact. Prefer artifacts for tables, lists, spreadsheets, kanbans, games, images, and other structured data.
33
+ - Determine if the user is explicitly talking about creating a new artifact, or wants to use an existing artifact.
34
+ - If it is ambiguous, query for existing artifacts first and then decide.
35
+ - If you decide to create an artifact, call the associated tool to create the artifact.
36
+ - Artifacts are stored in the database. Tools are used to create and query artifacts.
37
+ - If you are unsure about creating an artifact ask the user for clarification.
38
+ - Artifacts are mutable objects that can change over the course of the conversation.
39
+ - Always re-query the artifact using the tool (like query or inspect) to get the latest state of the artifact before answering the user.
40
+ - You must never generate the id of the artifact; only recall the ids that are already in the history.
41
+ - Artifacts are created by requiring the specific artifact using the require_artifact tool and creating it by calling the associated tool.
42
+ - You can add suggested actions at the end of your response.
43
+ - Suggested actions should be very concise and start with a verb and be phrased as a command to an agent -- not a question to the user.
44
+ - Suggested actions must be in the form of a user instruction that you can follow.
45
+ - Suggested actions could include actions that create artifacts.
46
+ - After creating an artifact use a tool to add it to the chat context.
47
+
48
+ ## Context
49
+
50
+ Today is {{DATETIME}}.