@dxos/assistant 0.8.4-main.84f28bd → 0.8.4-main.8baae0fced

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 (201) hide show
  1. package/LICENSE +102 -5
  2. package/README.md +2 -1
  3. package/dist/lib/neutral/chunk-J5LGTIGS.mjs +10 -0
  4. package/dist/lib/neutral/chunk-J5LGTIGS.mjs.map +7 -0
  5. package/dist/lib/neutral/extraction/index.mjs +204 -0
  6. package/dist/lib/neutral/extraction/index.mjs.map +7 -0
  7. package/dist/lib/neutral/index.mjs +1779 -0
  8. package/dist/lib/neutral/index.mjs.map +7 -0
  9. package/dist/lib/neutral/meta.json +1 -0
  10. package/dist/types/src/extraction/extraction-ner-function/index.d.ts +0 -1
  11. package/dist/types/src/extraction/extraction-ner-function/index.d.ts.map +1 -1
  12. package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts +1 -1
  13. package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts.map +1 -1
  14. package/dist/types/src/extraction/extraction.d.ts +1078 -215
  15. package/dist/types/src/extraction/extraction.d.ts.map +1 -1
  16. package/dist/types/src/extraction/index.d.ts +0 -1
  17. package/dist/types/src/extraction/index.d.ts.map +1 -1
  18. package/dist/types/src/extraction/quotes.d.ts +3 -3
  19. package/dist/types/src/extraction/quotes.d.ts.map +1 -1
  20. package/dist/types/src/functions/index.d.ts +2 -0
  21. package/dist/types/src/functions/index.d.ts.map +1 -0
  22. package/dist/types/src/functions/services.d.ts +17 -0
  23. package/dist/types/src/functions/services.d.ts.map +1 -0
  24. package/dist/types/src/index.d.ts +4 -7
  25. package/dist/types/src/index.d.ts.map +1 -1
  26. package/dist/types/src/request/AiRequest.d.ts +106 -0
  27. package/dist/types/src/request/AiRequest.d.ts.map +1 -0
  28. package/dist/types/src/request/artifact-diff.d.ts +27 -0
  29. package/dist/types/src/request/artifact-diff.d.ts.map +1 -0
  30. package/dist/types/src/request/format.d.ts +15 -0
  31. package/dist/types/src/request/format.d.ts.map +1 -0
  32. package/dist/types/src/request/index.d.ts +4 -0
  33. package/dist/types/src/request/index.d.ts.map +1 -0
  34. package/dist/types/src/request/observer.d.ts +23 -0
  35. package/dist/types/src/request/observer.d.ts.map +1 -0
  36. package/dist/types/src/{session → request}/version-pin.d.ts +5 -5
  37. package/dist/types/src/request/version-pin.d.ts.map +1 -0
  38. package/dist/types/src/session/AiContext.d.ts +154 -0
  39. package/dist/types/src/session/AiContext.d.ts.map +1 -0
  40. package/dist/types/src/session/AiContext.test.d.ts +2 -0
  41. package/dist/types/src/session/AiContext.test.d.ts.map +1 -0
  42. package/dist/types/src/session/AiSession.d.ts +73 -0
  43. package/dist/types/src/session/AiSession.d.ts.map +1 -0
  44. package/dist/types/src/session/index.d.ts +3 -2
  45. package/dist/types/src/session/index.d.ts.map +1 -1
  46. package/dist/types/src/session/toolkit.d.ts +16 -0
  47. package/dist/types/src/session/toolkit.d.ts.map +1 -0
  48. package/dist/types/src/templates/index.d.ts +2 -0
  49. package/dist/types/src/templates/index.d.ts.map +1 -0
  50. package/dist/types/src/templates/system.d.ts +17 -0
  51. package/dist/types/src/templates/system.d.ts.map +1 -0
  52. package/dist/types/src/util/artifact.d.ts +27 -0
  53. package/dist/types/src/util/artifact.d.ts.map +1 -0
  54. package/dist/types/src/util/diff.d.ts +17 -0
  55. package/dist/types/src/util/diff.d.ts.map +1 -0
  56. package/dist/types/src/util/diff.test.d.ts +2 -0
  57. package/dist/types/src/util/diff.test.d.ts.map +1 -0
  58. package/dist/types/src/util/error-handling.d.ts +4 -0
  59. package/dist/types/src/util/error-handling.d.ts.map +1 -0
  60. package/dist/types/src/util/errors.d.ts +67 -0
  61. package/dist/types/src/util/errors.d.ts.map +1 -0
  62. package/dist/types/src/util/execution-graph.d.ts +85 -0
  63. package/dist/types/src/util/execution-graph.d.ts.map +1 -0
  64. package/dist/types/src/util/execution-graph.test.d.ts +2 -0
  65. package/dist/types/src/util/execution-graph.test.d.ts.map +1 -0
  66. package/dist/types/src/util/index.d.ts +7 -0
  67. package/dist/types/src/util/index.d.ts.map +1 -0
  68. package/dist/types/src/util/tracing.d.ts +237 -0
  69. package/dist/types/src/util/tracing.d.ts.map +1 -0
  70. package/dist/types/tsconfig.tsbuildinfo +1 -1
  71. package/package.json +44 -30
  72. package/src/extraction/extraction-ner-function/index.ts +0 -1
  73. package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +12 -6
  74. package/src/extraction/extraction-ner-function/named-entity-recognition.ts +3 -1
  75. package/src/extraction/extraction.ts +44 -15
  76. package/src/extraction/index.ts +0 -1
  77. package/src/extraction/quotes.test.ts +3 -3
  78. package/src/extraction/quotes.ts +6 -7
  79. package/src/{conversation → functions}/index.ts +1 -1
  80. package/src/functions/services.ts +214 -0
  81. package/src/index.ts +4 -7
  82. package/src/request/AiRequest.ts +386 -0
  83. package/src/request/artifact-diff.ts +41 -0
  84. package/src/request/format.ts +147 -0
  85. package/src/request/index.ts +8 -0
  86. package/src/request/observer.ts +62 -0
  87. package/src/{session → request}/version-pin.ts +12 -12
  88. package/src/session/AiContext.test.ts +53 -0
  89. package/src/session/AiContext.ts +403 -0
  90. package/src/session/AiSession.ts +324 -0
  91. package/src/session/index.ts +4 -2
  92. package/src/session/toolkit.ts +47 -0
  93. package/src/templates/index.ts +5 -0
  94. package/src/templates/instructions/format.tpl +57 -0
  95. package/src/templates/instructions/system.tpl +96 -0
  96. package/src/templates/system.ts +42 -0
  97. package/src/typings.d.ts +5 -0
  98. package/src/util/artifact.ts +88 -0
  99. package/src/util/diff.test.ts +74 -0
  100. package/src/util/diff.ts +93 -0
  101. package/src/util/error-handling.ts +42 -0
  102. package/src/util/errors.ts +15 -0
  103. package/src/util/execution-graph.test.ts +740 -0
  104. package/src/util/execution-graph.ts +519 -0
  105. package/src/util/index.ts +10 -0
  106. package/src/util/tracing.ts +57 -0
  107. package/dist/lib/browser/index.mjs +0 -2019
  108. package/dist/lib/browser/index.mjs.map +0 -7
  109. package/dist/lib/browser/meta.json +0 -1
  110. package/dist/lib/node-esm/index.mjs +0 -2021
  111. package/dist/lib/node-esm/index.mjs.map +0 -7
  112. package/dist/lib/node-esm/meta.json +0 -1
  113. package/dist/types/bin/chat.d.ts +0 -2
  114. package/dist/types/bin/chat.d.ts.map +0 -1
  115. package/dist/types/src/blueprint/blueprint.d.ts +0 -41
  116. package/dist/types/src/blueprint/blueprint.d.ts.map +0 -1
  117. package/dist/types/src/blueprint/index.d.ts +0 -3
  118. package/dist/types/src/blueprint/index.d.ts.map +0 -1
  119. package/dist/types/src/blueprint/registry.d.ts +0 -7
  120. package/dist/types/src/blueprint/registry.d.ts.map +0 -1
  121. package/dist/types/src/context/binder.d.ts +0 -34
  122. package/dist/types/src/context/binder.d.ts.map +0 -1
  123. package/dist/types/src/context/binding.d.ts +0 -38
  124. package/dist/types/src/context/binding.d.ts.map +0 -1
  125. package/dist/types/src/context/index.d.ts +0 -3
  126. package/dist/types/src/context/index.d.ts.map +0 -1
  127. package/dist/types/src/conversation/conversation.d.ts +0 -44
  128. package/dist/types/src/conversation/conversation.d.ts.map +0 -1
  129. package/dist/types/src/conversation/index.d.ts +0 -2
  130. package/dist/types/src/conversation/index.d.ts.map +0 -1
  131. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +0 -4
  132. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +0 -1
  133. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts +0 -2
  134. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts.map +0 -1
  135. package/dist/types/src/extraction/extraction-llm-function/index.d.ts +0 -2
  136. package/dist/types/src/extraction/extraction-llm-function/index.d.ts.map +0 -1
  137. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +0 -208
  138. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +0 -1
  139. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts +0 -2
  140. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts.map +0 -1
  141. package/dist/types/src/research/exa.d.ts +0 -7
  142. package/dist/types/src/research/exa.d.ts.map +0 -1
  143. package/dist/types/src/research/graph.d.ts +0 -41
  144. package/dist/types/src/research/graph.d.ts.map +0 -1
  145. package/dist/types/src/research/graph.test.d.ts +0 -2
  146. package/dist/types/src/research/graph.test.d.ts.map +0 -1
  147. package/dist/types/src/research/index.d.ts +0 -4
  148. package/dist/types/src/research/index.d.ts.map +0 -1
  149. package/dist/types/src/research/research.d.ts +0 -10
  150. package/dist/types/src/research/research.d.ts.map +0 -1
  151. package/dist/types/src/research/research.test.d.ts +0 -2
  152. package/dist/types/src/research/research.test.d.ts.map +0 -1
  153. package/dist/types/src/session/session.d.ts +0 -283
  154. package/dist/types/src/session/session.d.ts.map +0 -1
  155. package/dist/types/src/session/session.test.d.ts +0 -2
  156. package/dist/types/src/session/session.test.d.ts.map +0 -1
  157. package/dist/types/src/session/version-pin.d.ts.map +0 -1
  158. package/dist/types/src/session/version-pin.test.d.ts +0 -2
  159. package/dist/types/src/session/version-pin.test.d.ts.map +0 -1
  160. package/dist/types/src/summarization/summarization.test.d.ts +0 -2
  161. package/dist/types/src/summarization/summarization.test.d.ts.map +0 -1
  162. package/dist/types/src/testing/exa-search-1748337321991.d.ts +0 -38
  163. package/dist/types/src/testing/exa-search-1748337321991.d.ts.map +0 -1
  164. package/dist/types/src/testing/exa-search-1748337331526.d.ts +0 -37
  165. package/dist/types/src/testing/exa-search-1748337331526.d.ts.map +0 -1
  166. package/dist/types/src/testing/exa-search-1748337344119.d.ts +0 -58
  167. package/dist/types/src/testing/exa-search-1748337344119.d.ts.map +0 -1
  168. package/dist/types/src/testing/index.d.ts +0 -3
  169. package/dist/types/src/testing/index.d.ts.map +0 -1
  170. package/dist/types/src/types.d.ts +0 -3
  171. package/dist/types/src/types.d.ts.map +0 -1
  172. package/src/blueprint/blueprint.ts +0 -75
  173. package/src/blueprint/index.ts +0 -6
  174. package/src/blueprint/registry.ts +0 -15
  175. package/src/context/binder.ts +0 -101
  176. package/src/context/binding.ts +0 -31
  177. package/src/context/index.ts +0 -6
  178. package/src/conversation/conversation.ts +0 -103
  179. package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +0 -95
  180. package/src/extraction/extraction-llm-function/extraction-llm-function.ts +0 -66
  181. package/src/extraction/extraction-llm-function/index.ts +0 -5
  182. package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +0 -115
  183. package/src/extraction/extraction-ner-function/extraction-ner-function.ts +0 -54
  184. package/src/research/exa.ts +0 -100
  185. package/src/research/graph.test.ts +0 -15
  186. package/src/research/graph.ts +0 -337
  187. package/src/research/index.ts +0 -7
  188. package/src/research/instructions-research.tpl +0 -97
  189. package/src/research/research.test.ts +0 -190
  190. package/src/research/research.ts +0 -90
  191. package/src/session/session.test.ts +0 -275
  192. package/src/session/session.ts +0 -582
  193. package/src/session/version-pin.test.ts +0 -26
  194. package/src/summarization/summarization.test.ts +0 -221
  195. package/src/testing/exa-search-1748337321991.ts +0 -131
  196. package/src/testing/exa-search-1748337331526.ts +0 -144
  197. package/src/testing/exa-search-1748337344119.ts +0 -133
  198. package/src/testing/index.ts +0 -11
  199. package/src/typedefs.d.ts +0 -8
  200. package/src/types.ts +0 -8
  201. /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
@@ -0,0 +1,386 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ // @import-as-namespace
6
+
7
+ import type * as AiError from '@effect/ai/AiError';
8
+ import * as LanguageModel from '@effect/ai/LanguageModel';
9
+ import type * as Toolkit from '@effect/ai/Toolkit';
10
+ import * as Array from 'effect/Array';
11
+ import * as Chunk from 'effect/Chunk';
12
+ import * as Effect from 'effect/Effect';
13
+ import { pipe } from 'effect/Function';
14
+ import * as Option from 'effect/Option';
15
+ import * as Stream from 'effect/Stream';
16
+
17
+ import {
18
+ AiParser,
19
+ AiPreprocessor,
20
+ AiSummarizer,
21
+ type AiToolNotFoundError,
22
+ callTool,
23
+ type OpaqueToolkit,
24
+ type PromptPreprocessingError,
25
+ type ToolExecutionService,
26
+ type ToolResolverService,
27
+ withoutToolCallParising,
28
+ } from '@dxos/ai';
29
+ import { type Blueprint, Trace, Operation, OperationRegistry } from '@dxos/compute';
30
+ import { Database, Obj } from '@dxos/echo';
31
+ import { log } from '@dxos/log';
32
+ import { ContentBlock, Message } from '@dxos/types';
33
+
34
+ import { getOperationFromTool } from '../functions/services';
35
+ import { type AiAssistantError, CompleteBlock, PartialBlock } from '../util';
36
+ import { formatSystemPrompt, formatUserPrompt } from './format';
37
+ import { GenerationObserver } from './observer';
38
+
39
+ export type RunError = AiError.AiError | PromptPreprocessingError | AiToolNotFoundError | AiAssistantError;
40
+
41
+ export type RunRequirements =
42
+ | LanguageModel.LanguageModel
43
+ | ToolExecutionService
44
+ | ToolResolverService
45
+ | Database.Service
46
+ | Operation.Service
47
+ | OperationRegistry.Service
48
+ | Trace.TraceService;
49
+
50
+ export type Options = {
51
+ /**
52
+ * Summarize before executing the prompt if the existing history exceeds this threshold.
53
+ */
54
+ summarizationThreshold?: number;
55
+
56
+ // TODO(dmaretskyi): Plan to phase out in favor of TracingService and the return type being a stream.
57
+ observer?: GenerationObserver;
58
+ /**
59
+ * Callback for when a message is received from the user, model, or tool.
60
+ * This is useful for streaming the output to a queue.
61
+ */
62
+ onOutput?: (message: Message.Message) => Effect.Effect<void, never, never>;
63
+ };
64
+
65
+ export type RunProps<R = never> = {
66
+ prompt: string | ContentBlock.Any[];
67
+ // TODO(wittjosiah): Rename to systemPrompt.
68
+ system?: string;
69
+ history?: Message.Message[];
70
+ objects?: Obj.Unknown[];
71
+ blueprints?: readonly Blueprint.Blueprint[];
72
+ toolkit?: OpaqueToolkit.OpaqueToolkit<R>;
73
+ };
74
+
75
+ export type BeginProps = {
76
+ prompt: string | ContentBlock.Any[];
77
+ system?: string;
78
+ history?: Message.Message[];
79
+ objects?: Obj.Unknown[];
80
+ blueprints?: readonly Blueprint.Blueprint[];
81
+ };
82
+
83
+ export type TurnProps<R = never> = {
84
+ system: string;
85
+ toolkit?: OpaqueToolkit.OpaqueToolkit<R>;
86
+ };
87
+
88
+ export type TurnResult = {
89
+ messages: Message.Message[];
90
+ done: boolean;
91
+ };
92
+
93
+ /**
94
+ * Contains message history, tools, current context.
95
+ * Current context means the state of the app, time of day, and other contextual information.
96
+ * It makes requests to the model, its a state machine.
97
+ * It keeps track of the current goal.
98
+ * It manages the context window.
99
+ * Tracks the success criteria of reaching the goal, exposing metrics (stretch).
100
+ * Could be run locally in the app or remotely.
101
+ * Could be personal or shared.
102
+ */
103
+ export class Request {
104
+ /** Prevents concurrent execution of session. */
105
+ private readonly _semaphore = Effect.runSync(Effect.makeSemaphore(1));
106
+
107
+ private readonly _observer: GenerationObserver;
108
+ private readonly _onOutput: (message: Message.Message) => Effect.Effect<void, never, never>;
109
+
110
+ /** Message history from queue. */
111
+ // TODO(burdon): Evolve into supporting a git-like graph of messages.
112
+ private _history: Message.Message[] = [];
113
+
114
+ /** Pending messages for this session (incl. the current prompt). */
115
+ private _pending: Message.Message[] = [];
116
+
117
+ private _started = 0;
118
+ private _ended = 0;
119
+ private _toolCalls = 0;
120
+
121
+ constructor(private readonly _options: Options = {}) {
122
+ this._observer = _options.observer ?? GenerationObserver.noop();
123
+ this._onOutput = _options.onOutput ?? (() => Effect.void);
124
+ }
125
+
126
+ get duration(): number {
127
+ return this._ended - this._started;
128
+ }
129
+
130
+ get toolCalls(): number {
131
+ return this._toolCalls;
132
+ }
133
+
134
+ get pending(): readonly Message.Message[] {
135
+ return this._pending;
136
+ }
137
+
138
+ private _submitMessage = (message: Message.Message): Effect.Effect<Message.Message, never, Trace.TraceService> =>
139
+ Effect.gen(this, function* () {
140
+ this._pending.push(message);
141
+ yield* this._observer.onMessage(message);
142
+ for (const block of message.blocks) {
143
+ log('write complete block', {
144
+ messageId: message.id,
145
+ role: message.sender.role!,
146
+ block: JSON.stringify(block),
147
+ });
148
+ yield* Trace.write(CompleteBlock, {
149
+ messageId: message.id,
150
+ role: message.sender.role!,
151
+ block,
152
+ });
153
+ }
154
+ yield* this._onOutput(message);
155
+ return message;
156
+ });
157
+
158
+ getToolCalls = () =>
159
+ pipe(
160
+ [...this._history, ...this._pending],
161
+ Array.reverse,
162
+ Array.takeWhile((_) => _.sender.role === 'assistant'),
163
+ Array.flatMap((_) => _.blocks.filter(ContentBlock.is('toolCall')).map((block) => ({ block, message: _ }))),
164
+ Array.filter((_) => !_.block.providerExecuted),
165
+ Array.reverse,
166
+ );
167
+
168
+ /**
169
+ * Initialize a session: set up history, perform summarization if needed, and submit the user prompt.
170
+ * Must be called before `runTurn()`.
171
+ */
172
+ begin = ({
173
+ prompt,
174
+ system,
175
+ history = [],
176
+ blueprints = [],
177
+ objects = [],
178
+ }: BeginProps): Effect.Effect<void, RunError, RunRequirements> =>
179
+ Effect.gen(this, function* () {
180
+ this._started = Date.now();
181
+ this._history = [...history];
182
+ this._pending = [];
183
+
184
+ const systemPrompt = yield* formatSystemPrompt({ system, blueprints, objects }).pipe(Effect.orDie);
185
+
186
+ if (this._options.summarizationThreshold !== undefined) {
187
+ const tokenCount = yield* AiPreprocessor.estimateTokens(
188
+ yield* AiPreprocessor.preprocessPrompt([...this._history], {
189
+ system: systemPrompt,
190
+ }),
191
+ );
192
+ if (tokenCount > this._options.summarizationThreshold) {
193
+ const summary = yield* AiSummarizer.summarize([...this._history]);
194
+ yield* this._submitMessage(summary);
195
+ }
196
+ }
197
+
198
+ yield* this._submitMessage(yield* formatUserPrompt({ prompt, history }));
199
+ }).pipe(Effect.withSpan('AiRequest.begin'));
200
+
201
+ /**
202
+ * Execute a single turn: one LLM generation followed by tool execution.
203
+ * The toolkit and system prompt can be updated between turns to reflect context changes (e.g. dynamically enabled blueprints).
204
+ */
205
+ runAgentTurn = <const R = never>({
206
+ system,
207
+ toolkit: opaqueToolkit,
208
+ }: TurnProps<R>): Effect.Effect<TurnResult, RunError, RunRequirements | R> =>
209
+ Effect.gen(this, function* () {
210
+ log.info('request', {
211
+ system: { snippet: createSnippet(system), length: system.length },
212
+ pending: this._pending.length,
213
+ history: this._history.length,
214
+ });
215
+
216
+ const prompt = yield* AiPreprocessor.preprocessPrompt([...this._history, ...this._pending], {
217
+ system,
218
+ cacheControl: 'ephemeral',
219
+ });
220
+
221
+ const toolkit = opaqueToolkit ? yield* opaqueToolkit.handlers : undefined;
222
+
223
+ const observer = this._observer;
224
+ let currentMessageId: Obj.ID | null = null;
225
+
226
+ const messages = yield* LanguageModel.streamText({
227
+ prompt,
228
+ toolkit,
229
+ disableToolCallResolution: true,
230
+ }).pipe(
231
+ withoutToolCallParising,
232
+ AiParser.parseResponse({
233
+ emitPartial: true,
234
+ onBegin: () => observer.onBegin(),
235
+ onBlock: (block) => observer.onBlock(block),
236
+ onPart: (part) => observer.onPart(part as any),
237
+ onEnd: (summary) => observer.onEnd(summary),
238
+ }),
239
+ Stream.map((block) => enrichToolCallBlock(block, toolkit)),
240
+ Stream.mapEffect(
241
+ (block) =>
242
+ Effect.gen(this, function* () {
243
+ if (block.pending) {
244
+ currentMessageId ??= Obj.ID.random();
245
+ log('emit ephemeral message', { id: currentMessageId, type: block._tag });
246
+ yield* Trace.write(PartialBlock, {
247
+ messageId: currentMessageId,
248
+ role: 'assistant',
249
+ block,
250
+ });
251
+ return Option.none();
252
+ } else {
253
+ currentMessageId ??= Obj.ID.random();
254
+ const id = currentMessageId;
255
+ currentMessageId = null;
256
+ log('emit complete message', { id, type: block._tag });
257
+ const message = Obj.make(Message.Message, {
258
+ id,
259
+ created: new Date().toISOString(),
260
+ sender: { role: 'assistant' },
261
+ blocks: [block],
262
+ });
263
+ return Option.some(yield* this._submitMessage(message));
264
+ }
265
+ }),
266
+ { concurrency: 1, unordered: false },
267
+ ),
268
+ Stream.filterMap((_) => _),
269
+ Stream.runCollect,
270
+ Effect.map(Chunk.toArray),
271
+ );
272
+ log.info('messages', { messages });
273
+
274
+ const toolCalls = this.getToolCalls();
275
+
276
+ if (toolCalls.length === 0) {
277
+ this._ended = Date.now();
278
+ return { messages, done: true };
279
+ } else if (!toolkit) {
280
+ throw new Error('No toolkit provided');
281
+ }
282
+
283
+ return { messages, done: false };
284
+ }).pipe(Effect.withSpan('AiRequest.runAgentTurn'));
285
+
286
+ runTools = <const R = never>({
287
+ toolkit: opaqueToolkit,
288
+ }: {
289
+ toolkit?: OpaqueToolkit.OpaqueToolkit<R>;
290
+ }): Effect.Effect<void, RunError, RunRequirements | R> =>
291
+ Effect.gen(this, function* () {
292
+ const toolkit = opaqueToolkit ? yield* opaqueToolkit.handlers : undefined;
293
+ const toolCalls = this.getToolCalls();
294
+ const toolResults = yield* Effect.forEach(toolCalls, ({ block, message }) => {
295
+ if (!toolkit) {
296
+ throw new Error('No toolkit provided');
297
+ }
298
+ return callTool(toolkit, block);
299
+ });
300
+
301
+ yield* this._submitMessage(
302
+ Obj.make(Message.Message, {
303
+ created: new Date().toISOString(),
304
+ sender: { role: 'tool' },
305
+ blocks: toolResults,
306
+ }),
307
+ );
308
+
309
+ this._toolCalls += toolResults.length;
310
+ }).pipe(Effect.withSpan('AiRequest.runTools'));
311
+
312
+ /**
313
+ * Run a full conversation turn loop. Equivalent to calling `begin()` then `runTurn()` in a loop.
314
+ */
315
+ run = <const R = never>({
316
+ prompt,
317
+ system: systemTemplate,
318
+ history = [],
319
+ objects = [],
320
+ blueprints = [],
321
+ toolkit,
322
+ }: RunProps<R>): Effect.Effect<Message.Message[], RunError, RunRequirements | R> =>
323
+ Effect.gen(this, function* () {
324
+ yield* this.begin({ prompt, system: systemTemplate, history, objects, blueprints });
325
+
326
+ const system = yield* formatSystemPrompt({ system: systemTemplate, blueprints, objects }).pipe(Effect.orDie);
327
+
328
+ do {
329
+ const { done } = yield* this.runAgentTurn({ system, toolkit });
330
+ if (done) {
331
+ break;
332
+ }
333
+ yield* this.runTools({ toolkit });
334
+ } while (true);
335
+
336
+ log('done', { pending: this._pending.length, duration: this.duration, tools: this._toolCalls });
337
+ return this._pending;
338
+ }).pipe(this._semaphore.withPermits(1), Effect.withSpan('AiRequest.run'));
339
+ }
340
+
341
+ /**
342
+ * Annotates `toolCall` blocks with metadata about the backing Operation, when one exists.
343
+ * Tool calls that resolve to a toolkit handler (no Operation) are left unchanged so callers can
344
+ * distinguish operation invocations from inline tool calls.
345
+ */
346
+ const enrichToolCallBlock = (
347
+ block: ContentBlock.Any,
348
+ toolkit: Toolkit.WithHandler<any> | undefined,
349
+ ): ContentBlock.Any => {
350
+ if (block._tag !== 'toolCall' || !toolkit) {
351
+ return block;
352
+ }
353
+ const tool = toolkit.tools[block.name];
354
+ if (!tool) {
355
+ return block;
356
+ }
357
+ // Some tools (provider-defined, raw MCP) don't carry an Effect `Context` for annotations and
358
+ // `getOperationFromTool` throws. Be defensive: treat any failure as "no operation".
359
+ let operationOpt: Option.Option<Operation.Definition.Any>;
360
+ try {
361
+ operationOpt = getOperationFromTool(tool);
362
+ } catch {
363
+ return block;
364
+ }
365
+ if (Option.isNone(operationOpt)) {
366
+ return block;
367
+ }
368
+ const { meta } = operationOpt.value;
369
+ return {
370
+ ...block,
371
+ operationKey: meta.key,
372
+ operationName: meta.name,
373
+ } satisfies ContentBlock.ToolCall;
374
+ };
375
+
376
+ const createSnippet = (text: string, len = 32) =>
377
+ text.length <= len * 2 ? text : [text.slice(0, len), '...', text.slice(-len)].join('');
378
+
379
+ // TODO(dmaretskyi): Extract as a general util.
380
+ const logDump = (message: string, data: unknown) => {
381
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
382
+ const { writeFileSync } = require('node:fs');
383
+ const path = `/tmp/log-data-${Date.now()}.json`;
384
+ writeFileSync(path, JSON.stringify(data, null, 2));
385
+ log.info(message, { path });
386
+ };
@@ -0,0 +1,41 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as Context from 'effect/Context';
6
+
7
+ import { type ObjectVersion } from '@dxos/echo-db';
8
+ import { type ObjectId } from '@dxos/keys';
9
+
10
+ /**
11
+ * Resolves artifact ids to their versions.
12
+ * Used to give the model a sense of the changes to the artifacts made by users during the conversation.
13
+ * The artifacts versions are pinned in the history, and whenever the artifact changes in-between assistant's steps,
14
+ * a diff is inserted into the conversation.
15
+ *
16
+ * Can be optionally provided to the session run call.
17
+ */
18
+ // TODO(dmaretskyi): Convert to Context.Reference
19
+ export class ArtifactDiffResolver extends Context.Tag('@dxos/assistant/ArtifactDiffResolver')<
20
+ ArtifactDiffResolver,
21
+ ArtifactDiffResolver.Service
22
+ >() {}
23
+
24
+ export namespace ArtifactDiffResolver {
25
+ export type Service = {
26
+ resolve: (
27
+ artifacts: {
28
+ id: ObjectId;
29
+ lastVersion: ObjectVersion;
30
+ }[],
31
+ ) => Promise<
32
+ Map<
33
+ ObjectId,
34
+ {
35
+ version: ObjectVersion;
36
+ diff?: string;
37
+ }
38
+ >
39
+ >;
40
+ };
41
+ }
@@ -0,0 +1,147 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as Effect from 'effect/Effect';
6
+ import * as Function from 'effect/Function';
7
+ import * as Option from 'effect/Option';
8
+
9
+ import { type FunctionNotFoundError, type Operation, type OperationRegistry, Template } from '@dxos/compute';
10
+ import { type Database, Obj } from '@dxos/echo';
11
+ import { ObjectVersion } from '@dxos/echo-db';
12
+ import { type ObjectNotFoundError } from '@dxos/echo/Err';
13
+ import { type ObjectId } from '@dxos/keys';
14
+ import { log } from '@dxos/log';
15
+ import { type ContentBlock, Message } from '@dxos/types';
16
+ import { trim } from '@dxos/util';
17
+
18
+ import { AiAssistantError } from '../util';
19
+ import type * as AiRequest from './AiRequest';
20
+ import { ArtifactDiffResolver } from './artifact-diff';
21
+
22
+ /**
23
+ * Formats the system prompt.
24
+ */
25
+ // TODO(burdon): Move to AiPreprocessor.
26
+ export const formatSystemPrompt = ({
27
+ system,
28
+ blueprints = [],
29
+ objects = [],
30
+ }: Pick<AiRequest.RunProps, 'system' | 'blueprints' | 'objects'>): Effect.Effect<
31
+ string,
32
+ FunctionNotFoundError | ObjectNotFoundError,
33
+ Database.Service | OperationRegistry.Service | Operation.Service
34
+ > =>
35
+ Effect.gen(function* () {
36
+ const blueprintDefs = yield* Function.pipe(
37
+ blueprints,
38
+ Effect.forEach((blueprint) => Effect.succeed(blueprint.instructions)),
39
+ Effect.flatMap(
40
+ Effect.forEach((template) =>
41
+ Effect.gen(function* () {
42
+ return trim`
43
+ <blueprint>
44
+ ${yield* Template.processTemplate(template)}
45
+ </blueprint>
46
+ `;
47
+ }),
48
+ ),
49
+ ),
50
+ Effect.map((blueprints) =>
51
+ blueprints.length > 0 ? ['## Blueprints Definitions', ...blueprints].join('\n\n') : undefined,
52
+ ),
53
+ );
54
+
55
+ const objectDefs = yield* Function.pipe(
56
+ objects,
57
+ Effect.forEach((object) =>
58
+ Effect.succeed(trim`
59
+ <object>
60
+ <dxn>${Obj.getDXN(object)}</dxn>
61
+ <typename>${Obj.getTypename(object)}</typename>
62
+ </object>
63
+ `),
64
+ ),
65
+ Effect.map((objects) => (objects.length > 0 ? ['## Context Objects', ...objects].join('\n\n') : undefined)),
66
+ );
67
+
68
+ return yield* Function.pipe(
69
+ Effect.succeed([system, blueprintDefs, objectDefs].filter((def): def is string => def !== undefined)),
70
+ Effect.map((parts) => parts.join('\n\n')),
71
+ );
72
+ }).pipe(Effect.withSpan('formatSystemPrompt'));
73
+
74
+ /**
75
+ * Formats the user prompt.
76
+ */
77
+ // TODO(burdon): Move to AiPreprocessor.
78
+ // TODO(burdon): Convert util below to `Effect.fn` (to preserve stack info)
79
+ export const formatUserPrompt = ({
80
+ prompt,
81
+ history = [],
82
+ }: Pick<AiRequest.RunProps, 'prompt' | 'history'>): Effect.Effect<Message.Message, AiRequest.RunError> =>
83
+ Effect.gen(function* () {
84
+ const blocks: ContentBlock.Any[] = [];
85
+
86
+ // TODO(dmaretskyi): Evaluate other approaches as `serviceOption` isn't represented in the type system.
87
+ const artifactDiffResolver = yield* Effect.serviceOption(ArtifactDiffResolver);
88
+ if (Option.isSome(artifactDiffResolver)) {
89
+ const versions = gatherObjectVersions(history);
90
+ const artifactDiff = yield* Effect.tryPromise({
91
+ try: () =>
92
+ artifactDiffResolver.value.resolve(
93
+ [...versions.entries()].map(([id, version]) => ({ id, lastVersion: version })),
94
+ ),
95
+ catch: AiAssistantError.wrap({ message: 'Artifact diff resolution error' }),
96
+ });
97
+
98
+ log('version', { artifactDiff, versions });
99
+ for (const [id, { version }] of [...artifactDiff.entries()]) {
100
+ if (ObjectVersion.equals(version, versions.get(id)!)) {
101
+ artifactDiff.delete(id);
102
+ continue;
103
+ }
104
+
105
+ blocks.push({ _tag: 'anchor', objectId: id, version });
106
+ }
107
+
108
+ if (artifactDiff.size > 0) {
109
+ blocks.push(createArtifactUpdateBlock(artifactDiff));
110
+ }
111
+ }
112
+
113
+ return Obj.make(Message.Message, {
114
+ created: new Date().toISOString(),
115
+ sender: { role: 'user' },
116
+ blocks: typeof prompt === 'string' ? [...blocks, { _tag: 'text', text: prompt }] : [...blocks, ...prompt],
117
+ });
118
+ }).pipe(Effect.withSpan('formatUserPrompt'));
119
+
120
+ const gatherObjectVersions = (messages: Message.Message[]): Map<ObjectId, ObjectVersion> => {
121
+ const artifactIds = new Map<ObjectId, ObjectVersion>();
122
+ for (const message of messages) {
123
+ for (const block of message.blocks) {
124
+ if (block._tag === 'anchor') {
125
+ artifactIds.set(block.objectId, block.version as ObjectVersion);
126
+ }
127
+ }
128
+ }
129
+
130
+ return artifactIds;
131
+ };
132
+
133
+ const createArtifactUpdateBlock = (
134
+ artifactDiff: Map<ObjectId, { version: ObjectVersion; diff?: string }>,
135
+ ): ContentBlock.Any => {
136
+ return {
137
+ _tag: 'text',
138
+ // TODO(dmaretskyi): Does this need to be a special content-block?
139
+ disposition: 'synthetic',
140
+ text: trim`
141
+ The following artifacts have been updated since the last message:
142
+ ${[...artifactDiff.entries()]
143
+ .map(([id, { diff }]) => `<changed-artifact id="${id}">${diff ? `\n${diff}` : ''}</changed-artifact>`)
144
+ .join('\n')}
145
+ `,
146
+ };
147
+ };
@@ -0,0 +1,8 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ export * from './format';
6
+ export * from './observer';
7
+
8
+ export * as AiRequest from './AiRequest';
@@ -0,0 +1,62 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import type * as Tool from '@effect/ai/Tool';
6
+ import * as Effect from 'effect/Effect';
7
+ import * as Function from 'effect/Function';
8
+
9
+ import { type AiParser, type ConsolePrinter } from '@dxos/ai';
10
+ import { type Message } from '@dxos/types';
11
+
12
+ /**
13
+ * Live observer of the generation process.
14
+ */
15
+ export interface GenerationObserver<
16
+ Tools extends Record<string, Tool.Any> = any,
17
+ > extends AiParser.ParseResponseCallbacks<Tools> {
18
+ /**
19
+ * Complete messages fired during the session, both from the model and from the user.
20
+ * This message will contain all message blocks emitted through the `onBlock` callback.
21
+ */
22
+ onMessage: (message: Message.Message) => Effect.Effect<void>;
23
+ }
24
+
25
+ export const GenerationObserver = Object.freeze({
26
+ make: ({
27
+ onBegin = Function.constant(Effect.void),
28
+ onPart = Function.constant(Effect.void),
29
+ onBlock = Function.constant(Effect.void),
30
+ onEnd = Function.constant(Effect.void),
31
+ onMessage = Function.constant(Effect.void),
32
+ }: Partial<GenerationObserver> = {}): GenerationObserver => ({
33
+ onBegin,
34
+ onPart,
35
+ onBlock,
36
+ onEnd,
37
+ onMessage,
38
+ }),
39
+
40
+ noop: () => GenerationObserver.make(),
41
+
42
+ /**
43
+ * Debug printer to be used in unit-tests and browser devtools.
44
+ */
45
+ fromPrinter: (printer: ConsolePrinter) =>
46
+ GenerationObserver.make({
47
+ onBlock: (block) =>
48
+ Effect.sync(() => {
49
+ if (block.pending) {
50
+ return; // Only prints full blocks (better for unit-tests and browser devtools).
51
+ }
52
+ printer.printContentBlock(block);
53
+ }),
54
+ onMessage: (message) =>
55
+ Effect.sync(() => {
56
+ if (message.sender.role === 'assistant') {
57
+ return; // Skip assistant messages since they are printed in the `onBlock` callback.
58
+ }
59
+ printer.printMessage(message);
60
+ }),
61
+ }),
62
+ });
@@ -2,12 +2,12 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Schema } from 'effect';
5
+ import * as Schema from 'effect/Schema';
6
6
 
7
- import { type MessageContentBlock } from '@dxos/ai';
8
- import { type ObjectVersion } from '@dxos/echo-db';
9
- import { getVersion } from '@dxos/echo-db';
10
- import { ObjectId, type BaseEchoObject } from '@dxos/echo-schema';
7
+ import { type Obj } from '@dxos/echo';
8
+ import { type ObjectVersion, getVersion } from '@dxos/echo-db';
9
+ import { ObjectId } from '@dxos/keys';
10
+ import { type ContentBlock } from '@dxos/types';
11
11
 
12
12
  // TODO(dmaretskyi): Extract.
13
13
  const ObjectVersionSchema = Schema.Unknown as Schema.Schema<ObjectVersion>;
@@ -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,22 +27,22 @@ 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;
31
- createBlock: (pin: VersionPin) => MessageContentBlock;
30
+ fromObject: (object: Obj.Unknown) => VersionPin;
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: Obj.Unknown): VersionPin {
35
35
  return VersionPin.make({
36
36
  objectId: object.id,
37
37
  version: getVersion(object),
38
38
  });
39
39
  }
40
40
 
41
- static createBlock(pin: VersionPin): MessageContentBlock {
41
+ static createBlock(pin: VersionPin): ContentBlock.Any {
42
42
  return {
43
- type: 'json',
43
+ _tag: 'json',
44
44
  disposition: VersionPin.DISPOSITION,
45
- json: JSON.stringify(pin),
45
+ data: JSON.stringify(pin),
46
46
  };
47
47
  }
48
48
  };