@dxos/assistant 0.8.4-main.3a94e84 → 0.8.4-main.406dc2a

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 (168) hide show
  1. package/dist/lib/browser/chunk-CWGVG235.mjs +380 -0
  2. package/dist/lib/browser/chunk-CWGVG235.mjs.map +7 -0
  3. package/dist/lib/browser/chunk-UB26N5UF.mjs +89 -0
  4. package/dist/lib/browser/chunk-UB26N5UF.mjs.map +7 -0
  5. package/dist/lib/browser/extraction/index.mjs +272 -0
  6. package/dist/lib/browser/extraction/index.mjs.map +7 -0
  7. package/dist/lib/browser/index.mjs +747 -1645
  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-QPYHML66.mjs +13 -0
  11. package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs.map +7 -0
  12. package/dist/lib/node-esm/chunk-BCFXTNSK.mjs +91 -0
  13. package/dist/lib/node-esm/chunk-BCFXTNSK.mjs.map +7 -0
  14. package/dist/lib/node-esm/chunk-OV5PMQPB.mjs +382 -0
  15. package/dist/lib/node-esm/chunk-OV5PMQPB.mjs.map +7 -0
  16. package/dist/lib/node-esm/extraction/index.mjs +273 -0
  17. package/dist/lib/node-esm/extraction/index.mjs.map +7 -0
  18. package/dist/lib/node-esm/index.mjs +746 -1645
  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-2HDF46YX.mjs +14 -0
  22. package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs.map +7 -0
  23. package/dist/types/src/conversation/context.d.ts +103 -0
  24. package/dist/types/src/conversation/context.d.ts.map +1 -0
  25. package/dist/types/src/conversation/conversation.d.ts +28 -22
  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 +46 -9
  34. package/dist/types/src/errors.d.ts.map +1 -1
  35. package/dist/types/src/execution-graph.d.ts +82 -0
  36. package/dist/types/src/execution-graph.d.ts.map +1 -0
  37. package/dist/types/src/execution-graph.test.d.ts +2 -0
  38. package/dist/types/src/execution-graph.test.d.ts.map +1 -0
  39. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +1 -1
  40. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -1
  41. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +103 -64
  42. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
  43. package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts.map +1 -1
  44. package/dist/types/src/extraction/extraction.d.ts +252 -218
  45. package/dist/types/src/extraction/extraction.d.ts.map +1 -1
  46. package/dist/types/src/extraction/quotes.d.ts +1 -1
  47. package/dist/types/src/extraction/quotes.d.ts.map +1 -1
  48. package/dist/types/src/functions/services.d.ts +17 -4
  49. package/dist/types/src/functions/services.d.ts.map +1 -1
  50. package/dist/types/src/index.d.ts +3 -3
  51. package/dist/types/src/index.d.ts.map +1 -1
  52. package/dist/types/src/session/artifact-diff.d.ts +27 -0
  53. package/dist/types/src/session/artifact-diff.d.ts.map +1 -0
  54. package/dist/types/src/session/error-handling.d.ts +4 -0
  55. package/dist/types/src/session/error-handling.d.ts.map +1 -0
  56. package/dist/types/src/session/format.d.ts +184 -0
  57. package/dist/types/src/session/format.d.ts.map +1 -0
  58. package/dist/types/src/session/format.test.d.ts +2 -0
  59. package/dist/types/src/session/format.test.d.ts.map +1 -0
  60. package/dist/types/src/session/index.d.ts +3 -0
  61. package/dist/types/src/session/index.d.ts.map +1 -1
  62. package/dist/types/src/session/observer.d.ts +22 -0
  63. package/dist/types/src/session/observer.d.ts.map +1 -0
  64. package/dist/types/src/session/session.d.ts +27 -236
  65. package/dist/types/src/session/session.d.ts.map +1 -1
  66. package/dist/types/src/session/toolkit.d.ts +15 -0
  67. package/dist/types/src/session/toolkit.d.ts.map +1 -0
  68. package/dist/types/src/session/version-pin.d.ts +3 -3
  69. package/dist/types/src/session/version-pin.d.ts.map +1 -1
  70. package/dist/types/src/templates/index.d.ts +1 -1
  71. package/dist/types/src/templates/system.d.ts +18 -0
  72. package/dist/types/src/templates/system.d.ts.map +1 -0
  73. package/dist/types/src/{types.d.ts → types/artifact.d.ts} +6 -3
  74. package/dist/types/src/types/artifact.d.ts.map +1 -0
  75. package/dist/types/src/types/index.d.ts +2 -0
  76. package/dist/types/src/types/index.d.ts.map +1 -0
  77. package/dist/types/tsconfig.tsbuildinfo +1 -1
  78. package/package.json +44 -32
  79. package/src/conversation/context.ts +186 -0
  80. package/src/conversation/conversation.ts +89 -53
  81. package/src/conversation/index.ts +1 -0
  82. package/src/diff.test.ts +43 -0
  83. package/src/diff.ts +79 -0
  84. package/src/errors.ts +6 -1
  85. package/src/execution-graph.test.ts +738 -0
  86. package/src/execution-graph.ts +500 -0
  87. package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +8 -7
  88. package/src/extraction/extraction-llm-function/extraction-llm-function.ts +13 -11
  89. package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +9 -9
  90. package/src/extraction/extraction-ner-function/extraction-ner-function.ts +15 -8
  91. package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +2 -1
  92. package/src/extraction/extraction-ner-function/named-entity-recognition.ts +2 -0
  93. package/src/extraction/extraction.ts +2 -2
  94. package/src/extraction/quotes.test.ts +3 -3
  95. package/src/extraction/quotes.ts +4 -4
  96. package/src/functions/services.ts +114 -95
  97. package/src/index.ts +3 -3
  98. package/src/session/artifact-diff.ts +41 -0
  99. package/src/session/error-handling.ts +41 -0
  100. package/src/session/format.test.ts +60 -0
  101. package/src/session/format.ts +142 -0
  102. package/src/session/index.ts +3 -0
  103. package/src/session/observer.ts +59 -0
  104. package/src/session/session.conversations.json +1 -0
  105. package/src/session/session.test.ts +37 -198
  106. package/src/session/session.ts +125 -249
  107. package/src/session/toolkit.ts +48 -0
  108. package/src/session/version-pin.ts +5 -5
  109. package/src/summarization/summarization.test.ts +16 -22
  110. package/src/templates/format.tpl +60 -0
  111. package/src/templates/index.ts +1 -1
  112. package/src/templates/system.tpl +48 -0
  113. package/src/templates/system.ts +41 -0
  114. package/src/{types.ts → types/artifact.ts} +12 -7
  115. package/src/types/index.ts +5 -0
  116. package/src/typings.d.ts +5 -0
  117. package/dist/types/bin/chat.d.ts +0 -2
  118. package/dist/types/bin/chat.d.ts.map +0 -1
  119. package/dist/types/src/context/binder.d.ts +0 -34
  120. package/dist/types/src/context/binder.d.ts.map +0 -1
  121. package/dist/types/src/context/binding.d.ts +0 -56
  122. package/dist/types/src/context/binding.d.ts.map +0 -1
  123. package/dist/types/src/context/index.d.ts +0 -3
  124. package/dist/types/src/context/index.d.ts.map +0 -1
  125. package/dist/types/src/research/exa.d.ts +0 -12
  126. package/dist/types/src/research/exa.d.ts.map +0 -1
  127. package/dist/types/src/research/graph.d.ts +0 -51
  128. package/dist/types/src/research/graph.d.ts.map +0 -1
  129. package/dist/types/src/research/graph.test.d.ts +0 -2
  130. package/dist/types/src/research/graph.test.d.ts.map +0 -1
  131. package/dist/types/src/research/index.d.ts +0 -4
  132. package/dist/types/src/research/index.d.ts.map +0 -1
  133. package/dist/types/src/research/research.d.ts +0 -10
  134. package/dist/types/src/research/research.d.ts.map +0 -1
  135. package/dist/types/src/research/research.test.d.ts +0 -2
  136. package/dist/types/src/research/research.test.d.ts.map +0 -1
  137. package/dist/types/src/session/version-pin.test.d.ts +0 -2
  138. package/dist/types/src/session/version-pin.test.d.ts.map +0 -1
  139. package/dist/types/src/templates/prompt.d.ts +0 -30
  140. package/dist/types/src/templates/prompt.d.ts.map +0 -1
  141. package/dist/types/src/testing/exa-search-1748337321991.d.ts +0 -38
  142. package/dist/types/src/testing/exa-search-1748337321991.d.ts.map +0 -1
  143. package/dist/types/src/testing/exa-search-1748337331526.d.ts +0 -37
  144. package/dist/types/src/testing/exa-search-1748337331526.d.ts.map +0 -1
  145. package/dist/types/src/testing/exa-search-1748337344119.d.ts +0 -58
  146. package/dist/types/src/testing/exa-search-1748337344119.d.ts.map +0 -1
  147. package/dist/types/src/testing/index.d.ts +0 -3
  148. package/dist/types/src/testing/index.d.ts.map +0 -1
  149. package/dist/types/src/types.d.ts.map +0 -1
  150. package/src/context/binder.ts +0 -103
  151. package/src/context/binding.ts +0 -32
  152. package/src/context/index.ts +0 -6
  153. package/src/research/exa.ts +0 -97
  154. package/src/research/graph.test.ts +0 -64
  155. package/src/research/graph.ts +0 -369
  156. package/src/research/index.ts +0 -7
  157. package/src/research/instructions-research.tpl +0 -98
  158. package/src/research/research.test.ts +0 -190
  159. package/src/research/research.ts +0 -87
  160. package/src/session/__snapshots__/session.test.ts.snap +0 -32
  161. package/src/session/version-pin.test.ts +0 -7
  162. package/src/templates/assistant.tpl +0 -111
  163. package/src/templates/prompt.ts +0 -45
  164. package/src/testing/exa-search-1748337321991.ts +0 -131
  165. package/src/testing/exa-search-1748337331526.ts +0 -144
  166. package/src/testing/exa-search-1748337344119.ts +0 -133
  167. package/src/testing/index.ts +0 -11
  168. package/src/typedefs.d.ts +0 -8
@@ -2,44 +2,57 @@
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 * as AiError from '@effect/ai/AiError';
6
+ import * as LanguageModel from '@effect/ai/LanguageModel';
7
+ import type * as Tool from '@effect/ai/Tool';
8
+ import type * as Toolkit from '@effect/ai/Toolkit';
9
+ import * as Chunk from 'effect/Chunk';
10
+ import * as Effect from 'effect/Effect';
11
+ import type * as Schema from 'effect/Schema';
12
+ import * as Stream from 'effect/Stream';
8
13
 
9
14
  import {
10
15
  AiParser,
11
16
  AiPreprocessor,
12
- getToolCalls,
13
- runTool,
14
- ToolExecutionService,
15
- ToolResolverService,
16
- type AgentStatus,
17
- type AiInputPreprocessingError,
18
17
  type AiToolNotFoundError,
19
- type GenerationStream,
18
+ type PromptPreprocessingError,
19
+ type ToolExecutionService,
20
+ type ToolResolverService,
21
+ callTool,
22
+ getToolCalls,
20
23
  } from '@dxos/ai';
21
- import { Event } from '@dxos/async';
22
24
  import { type Blueprint } from '@dxos/blueprints';
23
25
  import { todo } from '@dxos/debug';
24
26
  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';
27
+ import { TracingService } from '@dxos/functions';
28
28
  import { log } from '@dxos/log';
29
- import { DataType, type ContentBlock } from '@dxos/schema';
29
+ import { DataType } from '@dxos/schema';
30
30
 
31
- import { AiAssistantError } from '../errors';
31
+ import { type AiAssistantError } from '../errors';
32
32
 
33
- export type AiSessionOptions = {};
33
+ import { formatSystemPrompt, formatUserPrompt } from './format';
34
+ import { GenerationObserver } from './observer';
35
+
36
+ export type AiSessionRunError = AiError.AiError | PromptPreprocessingError | AiToolNotFoundError | AiAssistantError;
37
+
38
+ export type AiSessionRunRequirements =
39
+ | LanguageModel.LanguageModel
40
+ | ToolExecutionService
41
+ | ToolResolverService
42
+ | TracingService;
34
43
 
35
- export type SessionRunOptions<Tools extends AiTool.Any> = {
44
+ export type AiSessionRunParams<Tools extends Record<string, Tool.Any>> = {
36
45
  prompt: string;
37
- history: DataType.Message[];
38
- systemPrompt?: string;
39
- toolkit?: AiToolkit.AiToolkit<Tools>;
46
+ system?: string;
47
+ history?: DataType.Message[];
48
+ objects?: Obj.Any[];
40
49
  blueprints?: Blueprint.Blueprint[];
50
+ toolkit?: Toolkit.WithHandler<Tools>;
51
+ observer?: GenerationObserver;
41
52
  };
42
53
 
54
+ export type AiSessionOptions = {};
55
+
43
56
  /**
44
57
  * Contains message history, tools, current context.
45
58
  * Current context means the state of the app, time of day, and other contextual information.
@@ -51,165 +64,130 @@ export type SessionRunOptions<Tools extends AiTool.Any> = {
51
64
  * Could be personal or shared.
52
65
  */
53
66
  export class AiSession {
54
- // TODO(burdon): Move to conversation.
67
+ /** Prevents concurrent execution of session. */
55
68
  private readonly _semaphore = Effect.runSync(Effect.makeSemaphore(1));
56
69
 
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. */
70
+ /** Message history from queue. */
71
+ // TODO(burdon): Evolve into supporting a git-like graph of messages.
64
72
  private _history: DataType.Message[] = [];
65
73
 
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>();
74
+ /** Pending messages for this session (incl. the current prompt). */
75
+ private _pending: DataType.Message[] = [];
101
76
 
102
77
  constructor(private readonly _options: AiSessionOptions = {}) {}
103
78
 
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
- > =>
79
+ run = <Tools extends Record<string, Tool.Any>>({
80
+ prompt,
81
+ system: systemTemplate,
82
+ history = [],
83
+ objects = [],
84
+ blueprints = [],
85
+ toolkit,
86
+ observer = GenerationObserver.noop(),
87
+ }: AiSessionRunParams<Tools>): Effect.Effect<DataType.Message[], AiSessionRunError, AiSessionRunRequirements> =>
117
88
  Effect.gen(this, function* () {
118
- this._history = [...options.history];
89
+ this._history = [...history];
90
+ this._pending = [];
91
+ const pending = this._pending;
92
+
93
+ const submitMessage = Effect.fnUntraced(function* (message: DataType.Message) {
94
+ pending.push(message);
95
+ yield* observer.onMessage(message);
96
+ yield* TracingService.emitConverationMessage(message);
97
+ return message;
98
+ });
99
+
100
+ // Submit the prompt.
101
+ // TODO(burdon): Remove if cancelled?
102
+ const promptMessage = yield* submitMessage(yield* formatUserPrompt({ prompt, history }));
119
103
 
120
- const promptMessages = yield* this._formatUserPrompt(options.prompt, options.history);
121
- this._pending = [promptMessages];
122
- this.userMessage.emit(promptMessages);
104
+ // Generate system and prompt messages.
105
+ const system = yield* formatSystemPrompt({ system: systemTemplate, blueprints, objects });
123
106
 
124
- // Potential tool use loop.
107
+ // Tool call loop.
125
108
  do {
126
109
  log('request', {
110
+ prompt: promptMessage,
111
+ system: { snippet: createSnippet(system), length: system.length },
127
112
  pending: this._pending.length,
128
113
  history: this._history.length,
129
- tools: (Object.values(options.toolkit?.tools ?? {}) as AiTool.Any[]).map((tool: AiTool.Any) => tool.name),
114
+ objects: objects?.length ?? 0,
130
115
  });
131
116
 
132
- const prompt = yield* AiPreprocessor.preprocessAiInput([...this._history, ...this._pending]);
133
-
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) => DatabaseService.loadRef(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>>;
117
+ const prompt = yield* AiPreprocessor.preprocessPrompt([...this._history, ...this._pending], { system });
154
118
 
155
- log.info('run', {
156
- systemPrompt: [systemPrompt.slice(0, 32), '...', systemPrompt.slice(-32), systemPrompt.length].join(' '),
157
- tools: Object.keys(blueprintToolkit.tools),
158
- });
159
-
160
- const blocks = yield* AiLanguageModel.streamText({
119
+ // Execute the stream request.
120
+ // logDump('prompt', Prompt.Prompt.pipe(Schema.encodeSync)(prompt));
121
+ const blocks = yield* LanguageModel.streamText({
161
122
  prompt,
162
- system: systemPrompt,
163
- toolkit: toolkitWithBlueprintHandlers,
123
+ toolkit,
164
124
  disableToolCallResolution: true,
165
125
  }).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
- }),
126
+ // TOOD(dmaretskyi): Error mapping.
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
+ // TODO(wittjosiah): Sometimes tool error results are added to the queue before the tool agent statuses.
170
+ // This results in a broken execution graph.
171
+ yield* submitMessage(
199
172
  Obj.make(DataType.Message, {
200
173
  created: new Date().toISOString(),
201
- sender: { role: 'user' },
174
+ sender: { role: 'tool' },
202
175
  blocks: toolResults,
203
176
  }),
204
177
  );
205
178
  } while (true);
206
179
 
180
+ log('done', { pending: this._pending.length });
207
181
  return this._pending;
208
182
  }).pipe(this._semaphore.withPermits(1), Effect.withSpan('AiSession.run'));
209
183
 
184
+ /**
185
+ * @deprecated
186
+ */
187
+ // TODO(burdon): Implement or remove.
210
188
  async runStructured<S extends Schema.Schema.AnyNoContext>(
211
- schema: S,
212
- options: SessionRunOptions<AiTool.Any>,
189
+ _schema: S,
190
+ _options: AiSessionRunParams<any>,
213
191
  ): Promise<Schema.Schema.Type<S>> {
214
192
  return todo();
215
193
  // const parser = structuredOutputParser(schema);
@@ -219,118 +197,16 @@ export class AiSession {
219
197
  // });
220
198
  // return parser.getResult(result);
221
199
  }
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
200
  }
269
201
 
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
- }
202
+ const createSnippet = (text: string, len = 32) =>
203
+ text.length <= len * 2 ? text : [text.slice(0, len), '...', text.slice(-len)].join('');
279
204
 
280
- return artifactIds;
205
+ // TODO(dmaretskyi): Extract as a general util.
206
+ const logDump = (message: string, data: unknown) => {
207
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
208
+ const { writeFileSync } = require('node:fs');
209
+ const path = `/tmp/log-data-${Date.now()}.json`;
210
+ writeFileSync(path, JSON.stringify(data, null, 2));
211
+ log.info(message, { path });
281
212
  };
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('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
- }
326
-
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
- );
@@ -0,0 +1,48 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import type * as Tool from '@effect/ai/Tool';
6
+ import * as Toolkit from '@effect/ai/Toolkit';
7
+ import type * as Context from 'effect/Context';
8
+ import * as Effect from 'effect/Effect';
9
+
10
+ import { type AiToolNotFoundError, ToolExecutionService, type ToolId, ToolResolverService } from '@dxos/ai';
11
+ import { type Blueprint } from '@dxos/blueprints';
12
+ import { isTruthy } from '@dxos/util';
13
+
14
+ export type ToolkitParams<Tools extends Record<string, Tool.Any>> = {
15
+ toolkit?: Toolkit.Toolkit<Tools>;
16
+ toolIds?: ToolId[];
17
+ blueprints?: Blueprint.Blueprint[];
18
+ };
19
+
20
+ /**
21
+ * Build a combined toolkit from the blueprint tools and the provided toolkit.
22
+ */
23
+ export const createToolkit = <Tools extends Record<string, Tool.Any> = {}>({
24
+ toolkit: toolkitParam,
25
+ toolIds = [],
26
+ blueprints = [],
27
+ }: ToolkitParams<Tools>): Effect.Effect<
28
+ Toolkit.WithHandler<any>,
29
+ AiToolNotFoundError,
30
+ ToolResolverService | ToolExecutionService | Tool.HandlersFor<Tools>
31
+ > =>
32
+ Effect.gen(function* () {
33
+ const blueprintToolkit = yield* ToolResolverService.resolveToolkit([
34
+ ...blueprints.flatMap(({ tools }) => tools),
35
+ ...toolIds,
36
+ ]);
37
+
38
+ const blueprintToolkitHandler: Context.Context<Tool.HandlersFor<any>> = yield* blueprintToolkit.toContext(
39
+ ToolExecutionService.handlersFor(blueprintToolkit),
40
+ );
41
+
42
+ const toolkit = Toolkit.merge(...[toolkitParam, blueprintToolkit].filter(isTruthy));
43
+ return yield* toolkit.pipe(Effect.provide(blueprintToolkitHandler)) as any as Effect.Effect<
44
+ Toolkit.WithHandler<any>,
45
+ never,
46
+ Tool.HandlersFor<Tools>
47
+ >;
48
+ });
@@ -2,11 +2,11 @@
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 AnyEchoObject, ObjectId } from '@dxos/echo/internal';
7
8
  import { type ObjectVersion } from '@dxos/echo-db';
8
9
  import { getVersion } from '@dxos/echo-db';
9
- import { ObjectId, type BaseEchoObject } 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,24 +2,25 @@
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 * as Function from 'effect/Function';
9
+ import * as Schema from 'effect/Schema';
9
10
  import { beforeAll, describe, test } from 'vitest';
10
11
 
11
12
  import { AiService, MixedStreamParser } from '@dxos/ai';
12
13
  import { Obj } from '@dxos/echo';
13
14
  import { type EchoDatabase } from '@dxos/echo-db';
14
15
  import { EchoTestBuilder } from '@dxos/echo-db/testing';
15
- import { defineFunction, FunctionExecutor, ServiceContainer } from '@dxos/functions';
16
+ import { FunctionExecutor, ServiceContainer, defineFunction } from '@dxos/functions';
16
17
  import { type ContentBlock, DataType } from '@dxos/schema';
17
18
  import { createTestData } from '@dxos/schema/testing';
18
19
  import { trim } from '@dxos/util';
19
20
 
20
- const REMOTE_AI = true;
21
-
22
21
  const summarizationFn = defineFunction({
22
+ key: 'dxos.org/function/summarization/summarize',
23
+ name: 'Summarize transcript',
23
24
  description: 'Summarize a document',
24
25
  inputSchema: Schema.Struct({
25
26
  document: Schema.optional(DataType.Text),
@@ -27,7 +28,7 @@ const summarizationFn = defineFunction({
27
28
  }),
28
29
  outputSchema: DataType.Text,
29
30
  handler: async ({ data: { document, transcript }, context }) => {
30
- const ai = context.getService(AiService);
31
+ const ai = context.getService(AiService.AiService);
31
32
  const result = await new MixedStreamParser().parse(
32
33
  await ai.client.execStream({
33
34
  model: '@anthropic/claude-3-5-haiku-20241022',
@@ -78,13 +79,14 @@ const summarizationFn = defineFunction({
78
79
  ],
79
80
  }),
80
81
  );
81
- return Obj.make(DataType.Text, {
82
- content: pipe(result[0]?.blocks[0], (c) => (c?._tag === 'text' ? c.text : '')),
83
- });
82
+
83
+ return DataType.makeText(Function.pipe(result[0]?.blocks[0], (c) => (c?._tag === 'text' ? c.text : '')));
84
84
  },
85
85
  });
86
86
 
87
87
  const refinementFn = defineFunction({
88
+ key: 'dxos.org/function/summarization/refine',
89
+ name: 'Refine transcript summary',
88
90
  description: 'Refine a summary',
89
91
  inputSchema: Schema.Struct({
90
92
  summaries: Schema.Array(DataType.Text),
@@ -93,7 +95,7 @@ const refinementFn = defineFunction({
93
95
  summary: DataType.Text,
94
96
  }),
95
97
  handler: async ({ data: { summaries }, context }) => {
96
- const ai = context.getService(AiService);
98
+ const ai = context.getService(AiService.AiService);
97
99
  const result = await new MixedStreamParser().parse(
98
100
  await ai.client.execStream({
99
101
  model: '@anthropic/claude-3-5-haiku-20241022',
@@ -135,9 +137,7 @@ const refinementFn = defineFunction({
135
137
  }),
136
138
  );
137
139
  return {
138
- summary: Obj.make(DataType.Text, {
139
- content: pipe(result[0]?.blocks[0], (c) => (c?._tag === 'text' ? c.text : '')),
140
- }),
140
+ summary: DataType.makeText(Function.pipe(result[0]?.blocks[0], (c) => (c?._tag === 'text' ? c.text : ''))),
141
141
  };
142
142
  },
143
143
  });
@@ -166,9 +166,7 @@ describe.skip('Summarization', () => {
166
166
  test('keeps transcript outline', { timeout: 1000_000 }, async () => {
167
167
  const { transcriptMessages } = createTestData();
168
168
 
169
- const summary = Obj.make(DataType.Text, {
170
- content: '',
171
- });
169
+ const summary = DataType.makeText();
172
170
 
173
171
  const summaries = [];
174
172
 
@@ -179,12 +177,10 @@ describe.skip('Summarization', () => {
179
177
  transcript: blocks,
180
178
  });
181
179
  summary.content = result.content;
182
- summaries.push(Obj.make(DataType.Text, { content: result.content }));
180
+ summaries.push(DataType.makeText(result.content));
183
181
 
184
182
  console.log(blocks.at(-1));
185
183
  console.log();
186
- console.log('--------------------------------');
187
- console.log();
188
184
  console.log(summary.content);
189
185
  console.log();
190
186
 
@@ -194,12 +190,10 @@ describe.skip('Summarization', () => {
194
190
  summaries: history,
195
191
  });
196
192
  summary.content = result.summary.content;
197
- summaries.push(Obj.make(DataType.Text, { content: result.summary.content }));
193
+ summaries.push(DataType.makeText(result.summary.content));
198
194
 
199
195
  console.log('REFINED');
200
196
  console.log();
201
- console.log('--------------------------------');
202
- console.log();
203
197
  console.log(summary.content);
204
198
  console.log();
205
199
  }