@dxos/assistant 0.8.4-main.b97322e → 0.8.4-main.bc2380dfbc

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 (196) 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 +1755 -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.map +1 -1
  13. package/dist/types/src/extraction/extraction.d.ts +1066 -273
  14. package/dist/types/src/extraction/extraction.d.ts.map +1 -1
  15. package/dist/types/src/extraction/index.d.ts +0 -1
  16. package/dist/types/src/extraction/index.d.ts.map +1 -1
  17. package/dist/types/src/extraction/quotes.d.ts +3 -3
  18. package/dist/types/src/extraction/quotes.d.ts.map +1 -1
  19. package/dist/types/src/functions/services.d.ts +15 -4
  20. package/dist/types/src/functions/services.d.ts.map +1 -1
  21. package/dist/types/src/index.d.ts +2 -5
  22. package/dist/types/src/index.d.ts.map +1 -1
  23. package/dist/types/src/request/AiRequest.d.ts +104 -0
  24. package/dist/types/src/request/AiRequest.d.ts.map +1 -0
  25. package/dist/types/src/request/artifact-diff.d.ts +27 -0
  26. package/dist/types/src/request/artifact-diff.d.ts.map +1 -0
  27. package/dist/types/src/request/format.d.ts +15 -0
  28. package/dist/types/src/request/format.d.ts.map +1 -0
  29. package/dist/types/src/request/index.d.ts +4 -0
  30. package/dist/types/src/request/index.d.ts.map +1 -0
  31. package/dist/types/src/request/observer.d.ts +23 -0
  32. package/dist/types/src/request/observer.d.ts.map +1 -0
  33. package/dist/types/src/{session → request}/version-pin.d.ts +4 -4
  34. package/dist/types/src/request/version-pin.d.ts.map +1 -0
  35. package/dist/types/src/session/AiContext.d.ts +154 -0
  36. package/dist/types/src/session/AiContext.d.ts.map +1 -0
  37. package/dist/types/src/session/AiContext.test.d.ts +2 -0
  38. package/dist/types/src/session/AiContext.test.d.ts.map +1 -0
  39. package/dist/types/src/session/AiSession.d.ts +73 -0
  40. package/dist/types/src/session/AiSession.d.ts.map +1 -0
  41. package/dist/types/src/session/index.d.ts +3 -2
  42. package/dist/types/src/session/index.d.ts.map +1 -1
  43. package/dist/types/src/session/toolkit.d.ts +16 -0
  44. package/dist/types/src/session/toolkit.d.ts.map +1 -0
  45. package/dist/types/src/templates/index.d.ts +1 -1
  46. package/dist/types/src/templates/system.d.ts +17 -0
  47. package/dist/types/src/templates/system.d.ts.map +1 -0
  48. package/dist/types/src/util/artifact.d.ts +27 -0
  49. package/dist/types/src/util/artifact.d.ts.map +1 -0
  50. package/dist/types/src/util/diff.d.ts +17 -0
  51. package/dist/types/src/util/diff.d.ts.map +1 -0
  52. package/dist/types/src/util/diff.test.d.ts +2 -0
  53. package/dist/types/src/util/diff.test.d.ts.map +1 -0
  54. package/dist/types/src/util/error-handling.d.ts +4 -0
  55. package/dist/types/src/util/error-handling.d.ts.map +1 -0
  56. package/dist/types/src/util/errors.d.ts +67 -0
  57. package/dist/types/src/util/errors.d.ts.map +1 -0
  58. package/dist/types/src/util/execution-graph.d.ts +85 -0
  59. package/dist/types/src/util/execution-graph.d.ts.map +1 -0
  60. package/dist/types/src/util/execution-graph.test.d.ts +2 -0
  61. package/dist/types/src/util/execution-graph.test.d.ts.map +1 -0
  62. package/dist/types/src/util/index.d.ts +7 -0
  63. package/dist/types/src/util/index.d.ts.map +1 -0
  64. package/dist/types/src/util/tracing.d.ts +233 -0
  65. package/dist/types/src/util/tracing.d.ts.map +1 -0
  66. package/dist/types/tsconfig.tsbuildinfo +1 -1
  67. package/package.json +42 -36
  68. package/src/extraction/extraction-ner-function/index.ts +0 -1
  69. package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +12 -6
  70. package/src/extraction/extraction-ner-function/named-entity-recognition.ts +2 -0
  71. package/src/extraction/extraction.ts +44 -15
  72. package/src/extraction/index.ts +0 -1
  73. package/src/extraction/quotes.test.ts +3 -3
  74. package/src/extraction/quotes.ts +6 -7
  75. package/src/functions/services.ts +173 -98
  76. package/src/index.ts +2 -5
  77. package/src/request/AiRequest.ts +348 -0
  78. package/src/request/artifact-diff.ts +41 -0
  79. package/src/request/format.ts +147 -0
  80. package/src/request/index.ts +8 -0
  81. package/src/request/observer.ts +62 -0
  82. package/src/{session → request}/version-pin.ts +8 -8
  83. package/src/session/AiContext.test.ts +53 -0
  84. package/src/session/AiContext.ts +403 -0
  85. package/src/session/AiSession.ts +324 -0
  86. package/src/session/index.ts +4 -2
  87. package/src/session/toolkit.ts +47 -0
  88. package/src/templates/index.ts +1 -1
  89. package/src/templates/instructions/format.tpl +57 -0
  90. package/src/templates/instructions/system.tpl +96 -0
  91. package/src/templates/system.ts +42 -0
  92. package/src/typings.d.ts +5 -0
  93. package/src/util/artifact.ts +88 -0
  94. package/src/util/diff.test.ts +74 -0
  95. package/src/util/diff.ts +93 -0
  96. package/src/util/error-handling.ts +42 -0
  97. package/src/util/errors.ts +15 -0
  98. package/src/util/execution-graph.test.ts +740 -0
  99. package/src/util/execution-graph.ts +519 -0
  100. package/src/util/index.ts +10 -0
  101. package/src/util/tracing.ts +57 -0
  102. package/dist/lib/browser/index.mjs +0 -1831
  103. package/dist/lib/browser/index.mjs.map +0 -7
  104. package/dist/lib/browser/meta.json +0 -1
  105. package/dist/lib/node-esm/index.mjs +0 -1833
  106. package/dist/lib/node-esm/index.mjs.map +0 -7
  107. package/dist/lib/node-esm/meta.json +0 -1
  108. package/dist/types/bin/chat.d.ts +0 -2
  109. package/dist/types/bin/chat.d.ts.map +0 -1
  110. package/dist/types/src/context/binder.d.ts +0 -34
  111. package/dist/types/src/context/binder.d.ts.map +0 -1
  112. package/dist/types/src/context/binding.d.ts +0 -56
  113. package/dist/types/src/context/binding.d.ts.map +0 -1
  114. package/dist/types/src/context/index.d.ts +0 -3
  115. package/dist/types/src/context/index.d.ts.map +0 -1
  116. package/dist/types/src/conversation/conversation.d.ts +0 -38
  117. package/dist/types/src/conversation/conversation.d.ts.map +0 -1
  118. package/dist/types/src/conversation/index.d.ts +0 -2
  119. package/dist/types/src/conversation/index.d.ts.map +0 -1
  120. package/dist/types/src/errors.d.ts +0 -38
  121. package/dist/types/src/errors.d.ts.map +0 -1
  122. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +0 -4
  123. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +0 -1
  124. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts +0 -2
  125. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts.map +0 -1
  126. package/dist/types/src/extraction/extraction-llm-function/index.d.ts +0 -2
  127. package/dist/types/src/extraction/extraction-llm-function/index.d.ts.map +0 -1
  128. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +0 -302
  129. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +0 -1
  130. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts +0 -2
  131. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts.map +0 -1
  132. package/dist/types/src/research/exa.d.ts +0 -12
  133. package/dist/types/src/research/exa.d.ts.map +0 -1
  134. package/dist/types/src/research/graph.d.ts +0 -51
  135. package/dist/types/src/research/graph.d.ts.map +0 -1
  136. package/dist/types/src/research/graph.test.d.ts +0 -2
  137. package/dist/types/src/research/graph.test.d.ts.map +0 -1
  138. package/dist/types/src/research/index.d.ts +0 -4
  139. package/dist/types/src/research/index.d.ts.map +0 -1
  140. package/dist/types/src/research/research.d.ts +0 -10
  141. package/dist/types/src/research/research.d.ts.map +0 -1
  142. package/dist/types/src/research/research.test.d.ts +0 -2
  143. package/dist/types/src/research/research.test.d.ts.map +0 -1
  144. package/dist/types/src/session/session.d.ts +0 -260
  145. package/dist/types/src/session/session.d.ts.map +0 -1
  146. package/dist/types/src/session/session.test.d.ts +0 -2
  147. package/dist/types/src/session/session.test.d.ts.map +0 -1
  148. package/dist/types/src/session/version-pin.d.ts.map +0 -1
  149. package/dist/types/src/session/version-pin.test.d.ts +0 -2
  150. package/dist/types/src/session/version-pin.test.d.ts.map +0 -1
  151. package/dist/types/src/summarization/summarization.test.d.ts +0 -2
  152. package/dist/types/src/summarization/summarization.test.d.ts.map +0 -1
  153. package/dist/types/src/templates/prompt.d.ts +0 -30
  154. package/dist/types/src/templates/prompt.d.ts.map +0 -1
  155. package/dist/types/src/testing/exa-search-1748337321991.d.ts +0 -38
  156. package/dist/types/src/testing/exa-search-1748337321991.d.ts.map +0 -1
  157. package/dist/types/src/testing/exa-search-1748337331526.d.ts +0 -37
  158. package/dist/types/src/testing/exa-search-1748337331526.d.ts.map +0 -1
  159. package/dist/types/src/testing/exa-search-1748337344119.d.ts +0 -58
  160. package/dist/types/src/testing/exa-search-1748337344119.d.ts.map +0 -1
  161. package/dist/types/src/testing/index.d.ts +0 -3
  162. package/dist/types/src/testing/index.d.ts.map +0 -1
  163. package/dist/types/src/types.d.ts +0 -13
  164. package/dist/types/src/types.d.ts.map +0 -1
  165. package/src/context/binder.ts +0 -103
  166. package/src/context/binding.ts +0 -32
  167. package/src/context/index.ts +0 -6
  168. package/src/conversation/conversation.ts +0 -96
  169. package/src/conversation/index.ts +0 -5
  170. package/src/errors.ts +0 -10
  171. package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +0 -83
  172. package/src/extraction/extraction-llm-function/extraction-llm-function.ts +0 -70
  173. package/src/extraction/extraction-llm-function/index.ts +0 -5
  174. package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +0 -115
  175. package/src/extraction/extraction-ner-function/extraction-ner-function.ts +0 -57
  176. package/src/research/exa.ts +0 -97
  177. package/src/research/graph.test.ts +0 -64
  178. package/src/research/graph.ts +0 -369
  179. package/src/research/index.ts +0 -7
  180. package/src/research/instructions-research.tpl +0 -98
  181. package/src/research/research.test.ts +0 -190
  182. package/src/research/research.ts +0 -87
  183. package/src/session/__snapshots__/session.test.ts.snap +0 -32
  184. package/src/session/session.test.ts +0 -310
  185. package/src/session/session.ts +0 -336
  186. package/src/session/version-pin.test.ts +0 -7
  187. package/src/summarization/summarization.test.ts +0 -208
  188. package/src/templates/assistant.tpl +0 -111
  189. package/src/templates/prompt.ts +0 -45
  190. package/src/testing/exa-search-1748337321991.ts +0 -131
  191. package/src/testing/exa-search-1748337331526.ts +0 -144
  192. package/src/testing/exa-search-1748337344119.ts +0 -133
  193. package/src/testing/index.ts +0 -11
  194. package/src/typedefs.d.ts +0 -8
  195. package/src/types.ts +0 -47
  196. /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
@@ -0,0 +1,324 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ // @import-as-namespace
6
+
7
+ import { type Registry } from '@effect-atom/atom-react';
8
+ import * as Array from 'effect/Array';
9
+ import * as Context from 'effect/Context';
10
+ import * as Effect from 'effect/Effect';
11
+ import * as Either from 'effect/Either';
12
+ import { pipe } from 'effect/Function';
13
+ import * as Layer from 'effect/Layer';
14
+ import * as Record from 'effect/Record';
15
+ import * as Runtime from 'effect/Runtime';
16
+
17
+ import { type OpaqueToolkit, type ToolExecutionService, type ToolResolverService } from '@dxos/ai';
18
+ import { type Blueprint, type OperationRegistry, McpServer, Operation, Trace } from '@dxos/compute';
19
+ import { Resource } from '@dxos/context';
20
+ import { Database, Feed, Filter, Obj } from '@dxos/echo';
21
+ import { acquireReleaseResource } from '@dxos/effect';
22
+ import { invariant } from '@dxos/invariant';
23
+ import { log } from '@dxos/log';
24
+ import { McpToolkit } from '@dxos/mcp-client';
25
+ import { Message, type ContentBlock } from '@dxos/types';
26
+
27
+ import { ToolExecutionServices } from '../functions';
28
+ import { AiRequest, type GenerationObserver, formatSystemPrompt } from '../request';
29
+ import { McpServerError } from '../util';
30
+ import * as AiContext from './AiContext';
31
+ import { createToolkit } from './toolkit';
32
+
33
+ export type RunProps<R = never> = {
34
+ prompt: string | ContentBlock.Any[];
35
+ system?: string;
36
+ observer?: GenerationObserver;
37
+ toolkit?: OpaqueToolkit.OpaqueToolkit<R>;
38
+
39
+ /**
40
+ * Space-level MCP servers to connect alongside blueprint-defined ones.
41
+ */
42
+ mcpServers?: readonly McpServer.McpServer[];
43
+ };
44
+
45
+ export type Options = {
46
+ feed: Feed.Feed;
47
+ runtime: Runtime.Runtime<Feed.FeedService>;
48
+ registry?: Registry.Registry;
49
+ };
50
+
51
+ /**
52
+ * Summarization threshold in tokens.
53
+ */
54
+ // TODO(dmaretskyi): Take from model characteristics. opus has 200k max tokens.
55
+ const SUMMARY_THRESHOLD = 80_000;
56
+
57
+ /**
58
+ * Durable conversation state (initiated by users and agents) backed by a Feed.
59
+ * Executes tools based on AI responses and supports cancellation of in-progress requests.
60
+ */
61
+ export class Session extends Resource {
62
+ /**
63
+ * Blueprints and objects bound to the session.
64
+ */
65
+ private readonly _binder: AiContext.Binder;
66
+ private readonly _feed: Feed.Feed;
67
+ private readonly _runtime: Runtime.Runtime<Feed.FeedService>;
68
+
69
+ public constructor(options: Options) {
70
+ super();
71
+ this._feed = options.feed;
72
+ this._runtime = options.runtime;
73
+ invariant(this._feed);
74
+ invariant(this._runtime);
75
+ this._binder = new AiContext.Binder({
76
+ feed: this._feed,
77
+ runtime: this._runtime,
78
+ registry: options.registry,
79
+ });
80
+ }
81
+
82
+ protected override async _open(): Promise<void> {
83
+ await this._binder.open(this._ctx);
84
+ }
85
+
86
+ public get feed() {
87
+ return this._feed;
88
+ }
89
+
90
+ public get context() {
91
+ return this._binder;
92
+ }
93
+
94
+ public async getHistory(): Promise<Message.Message[]> {
95
+ const queryResult = await Runtime.runPromise(this._runtime)(Feed.query(this._feed, Filter.type(Message.Message)));
96
+ const items = await queryResult.run();
97
+ return items.filter(Obj.instanceOf(Message.Message));
98
+ }
99
+
100
+ getTools(): Effect.Effect<
101
+ Record<string, import('@effect/ai/Tool').Any>,
102
+ never,
103
+ ToolExecutionService | ToolResolverService
104
+ > {
105
+ return Effect.gen(this, function* () {
106
+ const toolkit = yield* createToolkit({ blueprints: this.context.getBlueprints() });
107
+ return toolkit.toolkit.tools;
108
+ }).pipe(Effect.orDie);
109
+ }
110
+
111
+ /**
112
+ * Not provided by default, since users might want to override them.
113
+ */
114
+ makeToolExecutionServices(): Layer.Layer<
115
+ ToolExecutionService | ToolResolverService,
116
+ never,
117
+ OpaqueToolkit.OpaqueToolkitProvider | Operation.Service | OperationRegistry.Service
118
+ > {
119
+ return ToolExecutionServices.pipe(
120
+ Layer.provide(Operation.withInvocationOptions({ conversation: Obj.getDXN(this._feed).toString() })),
121
+ );
122
+ }
123
+
124
+ /**
125
+ * Creates a new cancelable request effect.
126
+ */
127
+ public createRequest<R = never>(
128
+ params: RunProps<R>,
129
+ ): Effect.Effect<Message.Message[], AiRequest.RunError, AiRequest.RunRequirements | R> {
130
+ return Effect.gen(this, function* () {
131
+ const history = yield* Effect.promise(() => this.getHistory());
132
+ const blueprints = this.context.getBlueprints();
133
+ const objects = this.context.getObjects();
134
+
135
+ log('run', {
136
+ history: history.length,
137
+ blueprints: blueprints.length,
138
+ objects: objects.length,
139
+ });
140
+
141
+ const request = new AiRequest.Request({
142
+ summarizationThreshold: SUMMARY_THRESHOLD,
143
+ observer: params.observer,
144
+ onOutput: (message) =>
145
+ Effect.promise(() => Runtime.runPromise(this._runtime)(Feed.append(this._feed, [message]))),
146
+ });
147
+
148
+ yield* request.begin({
149
+ history,
150
+ blueprints,
151
+ objects,
152
+ prompt: params.prompt,
153
+ system: params.system,
154
+ });
155
+
156
+ // Turn loop: recompute toolkit and system prompt between turns to pick up dynamically enabled blueprints.
157
+ do {
158
+ yield* Effect.promise(() => this.context.sync());
159
+ const currentBlueprints = this.context.getBlueprints();
160
+ const mcps = yield* connectMcpServers(currentBlueprints, params.mcpServers);
161
+ const toolkit = yield* createToolkit({
162
+ toolkit: params.toolkit,
163
+ blueprints: currentBlueprints,
164
+ opaqueToolkits: mcps,
165
+ });
166
+
167
+ log('toolkit', { tools: Record.keys(toolkit.toolkit.tools) });
168
+ const system = yield* formatSystemPrompt({
169
+ system: params.system,
170
+ blueprints: currentBlueprints,
171
+ objects: this.context.getObjects(),
172
+ }).pipe(Effect.orDie);
173
+
174
+ const { done } = yield* request.runAgentTurn({ system, toolkit });
175
+ if (done) {
176
+ break;
177
+ }
178
+
179
+ yield* request.runTools({ toolkit });
180
+ } while (true);
181
+
182
+ log('result', {
183
+ messages: request.pending.length,
184
+ duration: request.duration,
185
+ toolCalls: request.toolCalls,
186
+ });
187
+
188
+ return [...request.pending];
189
+ }).pipe(
190
+ Effect.provide(
191
+ Layer.mergeAll(
192
+ Layer.succeed(AiContext.Service, {
193
+ binder: this.context,
194
+ }),
195
+ Layer.succeed(Service, this),
196
+ Operation.withInvocationOptions({ conversation: Obj.getDXN(this._feed).toString() }),
197
+ ),
198
+ ),
199
+ Effect.withSpan('AiSession.createRequest'),
200
+ );
201
+ }
202
+ }
203
+
204
+ /**
205
+ * Gives access to the ai session.
206
+ */
207
+ export class Service extends Context.Tag('@dxos/assistant/AiSessionService')<Service, Session>() {
208
+ /**
209
+ * Create a new session layer from options.
210
+ */
211
+ static layer = (options: Options): Layer.Layer<Service | AiContext.Service> =>
212
+ aiContextFromSession.pipe(
213
+ Layer.provideMerge(
214
+ Layer.scoped(
215
+ Service,
216
+ Effect.gen(function* () {
217
+ const session = yield* acquireReleaseResource(() => new Session(options));
218
+ return session;
219
+ }),
220
+ ),
221
+ ),
222
+ );
223
+
224
+ /**
225
+ * Create a new session with a new feed.
226
+ */
227
+ static layerNewFeed = (
228
+ options?: Omit<Options, 'feed' | 'runtime'>,
229
+ ): Layer.Layer<Service | AiContext.Service, never, Database.Service | Feed.FeedService> =>
230
+ Layer.unwrapScoped(
231
+ Effect.gen(function* () {
232
+ const feed = yield* Database.add(Feed.make());
233
+ const runtime = yield* Effect.runtime<Feed.FeedService>();
234
+ return Service.layer({ ...options, feed, runtime });
235
+ }),
236
+ );
237
+
238
+ /**
239
+ * Run a prompt in the current session.
240
+ */
241
+ static run = <R = never>(
242
+ params: RunProps<R>,
243
+ ): Effect.Effect<Message.Message[], AiRequest.RunError, AiRequest.RunRequirements | Service | R> =>
244
+ Effect.gen(function* () {
245
+ const session = yield* Service;
246
+ return yield* session.createRequest(params);
247
+ });
248
+ }
249
+
250
+ const aiContextFromSession = Layer.effect(
251
+ AiContext.Service,
252
+ Effect.gen(function* () {
253
+ const session = yield* Service;
254
+ return {
255
+ binder: session.context,
256
+ };
257
+ }),
258
+ );
259
+
260
+ const connectMcpServers = (
261
+ blueprints: readonly Blueprint.Blueprint[],
262
+ spaceMcpServers: readonly McpServer.McpServer[] = [],
263
+ ): Effect.Effect<OpaqueToolkit.OpaqueToolkit[], never, Trace.TraceService> => {
264
+ const blueprintServers: McpToolkit.McpToolkitOptions[] = pipe(
265
+ blueprints,
266
+ Array.flatMap((_) => _.mcpServers ?? []),
267
+ Array.map(({ url, protocol, apiKey }) => ({ url, protocol, apiKey })),
268
+ );
269
+ const spaceServers: McpToolkit.McpToolkitOptions[] = spaceMcpServers.map(({ url, protocol, apiKey }) => ({
270
+ url,
271
+ protocol,
272
+ apiKey,
273
+ }));
274
+ const allServers = [...blueprintServers, ...spaceServers];
275
+
276
+ return pipe(
277
+ allServers,
278
+ Effect.forEach((options) =>
279
+ McpToolkit.make(options).pipe(
280
+ // NOTE: Type-inference fails here without explicit void return.
281
+ Effect.tap((toolkit): void =>
282
+ log.info('Connected to MCP server', { url: options.url, tools: Object.keys(toolkit.toolkit.tools).length }),
283
+ ),
284
+ // Surface typed connection failures via ephemeral trace + warn log, then drop the server.
285
+ Effect.tapError((error) =>
286
+ Effect.gen(function* () {
287
+ log.warn('Failed to connect to MCP server', {
288
+ url: error.url,
289
+ protocol: error.protocol,
290
+ message: error.message,
291
+ });
292
+ yield* Trace.write(McpServerError, {
293
+ url: error.url,
294
+ protocol: error.protocol,
295
+ message: error.message,
296
+ });
297
+ }),
298
+ ),
299
+ // Catch unexpected defects too (e.g. malformed tool schemas) so a single broken
300
+ // server can never abort the whole turn — surface them through the same channel.
301
+ Effect.catchAllDefect((defect) =>
302
+ Effect.gen(function* () {
303
+ const message = defect instanceof Error ? defect.message : String(defect);
304
+ log.warn('Unexpected MCP defect', { url: options.url, message });
305
+ yield* Trace.write(McpServerError, {
306
+ url: options.url,
307
+ protocol: options.protocol,
308
+ message: `Unexpected MCP failure: ${message}`,
309
+ });
310
+ return yield* Effect.fail(
311
+ new McpToolkit.McpConnectionError({
312
+ url: options.url,
313
+ protocol: options.protocol,
314
+ message,
315
+ }),
316
+ );
317
+ }),
318
+ ),
319
+ Effect.either,
320
+ ),
321
+ ),
322
+ Effect.map(Array.filterMap((_) => Either.getRight(_))),
323
+ );
324
+ };
@@ -2,5 +2,7 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- export * from './session';
6
- export * from './version-pin';
5
+ export * from './toolkit';
6
+
7
+ export * as AiContext from './AiContext';
8
+ export * as AiSession from './AiSession';
@@ -0,0 +1,47 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as Toolkit from '@effect/ai/Toolkit';
6
+ import * as Effect from 'effect/Effect';
7
+ import * as Layer from 'effect/Layer';
8
+
9
+ import { type AiToolNotFoundError, OpaqueToolkit, ToolExecutionService, ToolResolverService } from '@dxos/ai';
10
+ import { type Blueprint } from '@dxos/compute';
11
+ import { isTruthy } from '@dxos/util';
12
+
13
+ export type CreateToolkitProps = {
14
+ toolkit?: OpaqueToolkit.Any;
15
+ blueprints?: readonly Blueprint.Blueprint[];
16
+ /**
17
+ * Self-contained with handlers toolkits.
18
+ */
19
+ opaqueToolkits?: readonly OpaqueToolkit.Any[];
20
+ };
21
+
22
+ /**
23
+ * Build a combined toolkit from the blueprint tools and the provided toolkit.
24
+ */
25
+ export const createToolkit = ({
26
+ toolkit: toolkitProp,
27
+ blueprints = [],
28
+ opaqueToolkits = [],
29
+ }: CreateToolkitProps): Effect.Effect<
30
+ OpaqueToolkit.OpaqueToolkit,
31
+ AiToolNotFoundError,
32
+ ToolResolverService | ToolExecutionService
33
+ > =>
34
+ Effect.gen(function* () {
35
+ const blueprintToolkit = yield* ToolResolverService.resolveToolkit(blueprints.flatMap(({ tools }) => tools));
36
+ const blueprintToolHandler = yield* blueprintToolkit.toContext(ToolExecutionService.handlersFor(blueprintToolkit));
37
+ const opaqueToolkit = OpaqueToolkit.merge(...opaqueToolkits);
38
+
39
+ const toolkitDefs = [toolkitProp?.toolkit, blueprintToolkit, opaqueToolkit.toolkit].filter(isTruthy);
40
+ const mergedToolkit = Toolkit.merge(...toolkitDefs);
41
+ const combinedHandlerLayer = Layer.mergeAll(
42
+ Layer.succeedContext(blueprintToolHandler),
43
+ toolkitProp?.layer ?? OpaqueToolkit.empty.layer,
44
+ opaqueToolkit.layer,
45
+ );
46
+ return OpaqueToolkit.make(mergedToolkit, combinedHandlerLayer as any) as OpaqueToolkit.OpaqueToolkit;
47
+ }) as Effect.Effect<OpaqueToolkit.OpaqueToolkit, AiToolNotFoundError, ToolResolverService | ToolExecutionService>;
@@ -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,57 @@
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
+ Code snippets should use 2-space indents.
27
+
28
+ {{#if cot}}
29
+ Before responding, explain your reasoning and include your detailed chain-of-thought enclosed in a <cot> tag.
30
+ The <cot> tag should be the first thing in your response.
31
+ {{/if}}
32
+
33
+ ## Block references
34
+
35
+ You can reply with a block reference for objects if you have the DXN. For example:
36
+
37
+ <object>
38
+ <dxn>dxn:queue:data:B6INSIBY3CBEF4M5VZRYBCMAHQMPYK5AJ:01K24XMVHSZHS97SG1VTVQDM5Z:01K24XPK464FSCKVQJAB2H662M</dxn>
39
+ </object>
40
+ <object>
41
+ <dxn>dxn:echo:@:01K24XPK464FSCKVQJAB2H662M</dxn>
42
+ </object>
43
+
44
+ ### Suggestions
45
+
46
+ Each suggestion must in the form of a proposed action enclosed in a <suggestion> tag.
47
+ Example: <suggestion>Show on a map</suggestion>
48
+ Do not use bullets when listing suggestions.
49
+
50
+ ### Multiple choice questions
51
+
52
+ You may ask multiple choice questions by wrapping consise options inside <option> tags inside a well formed <select> tag.
53
+ Example: <select><option>Yes</option><option>No</option></select>
54
+
55
+ ## Context
56
+
57
+ The current date and time is {{DATETIME}}.
@@ -0,0 +1,96 @@
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 inside of 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
+ - You are an advanced AI assistant capable of creating and managing artifacts from provided data and tools.
8
+ - Your task is to process user commands and questions and decide how best to respond.
9
+ - In general be concise and direct.
10
+ - Follow all instructions carefully.
11
+
12
+ ## Planning
13
+
14
+ - Analyze the structure and type of the content in the user's message.
15
+ - Determine if you complete the task using the available blueprint definitions?
16
+ - If you can't complete the task using the available blueprint definitions, query the list of available blueprint definitions using the appropriate tool.
17
+ - Identify which blueprint definitions are relevant to the user's request.
18
+ - Evaluate the potential benefits of creating an artifact vs. normal processing for each identified element.
19
+ - Make a final decision on whether to create an artifact and explain your reasoning.
20
+ - Are the required blueprint definitions already available?
21
+ - If not, select which blueprint definition(s) will be the most relevant and require them using the require_artifact_definitions tool.
22
+ - The require'd artifact tools will be available for use after require.
23
+ - If creating an artifact, outline how you will structure it within the response.
24
+ - Decide if the artifact needs to be shown to the user.
25
+ - Call the show tool to show the artifact to the user.
26
+ - 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.
27
+
28
+ ## Blueprints and Artifacts
29
+
30
+ - Determine if the interaction involves an artifact. Prefer artifacts for tables, lists, spreadsheets, kanbans, games, images, and other structured data.
31
+ - Determine if the user is explicitly talking about creating a new artifact, or wants to use an existing artifact.
32
+ - If it is ambiguous, query for existing artifacts first and then decide.
33
+ - If you decide to create an artifact, call the associated tool to create the artifact.
34
+ - Artifacts are stored in the database. Tools are used to create and query artifacts.
35
+ - If you are unsure about creating an artifact ask the user for clarification.
36
+ - Artifacts are mutable objects that can change over the course of the conversation.
37
+ - Always re-query the artifact using the tool (like query or inspect) to get the latest state of the artifact before answering the user.
38
+ - You must never generate the id of the artifact; only recall the ids that are already in the history.
39
+ - Artifacts are created by requiring the specific artifact using the require_artifact tool and creating it by calling the associated tool.
40
+ - You can add suggested actions at the end of your response.
41
+ - 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.
42
+ - Suggested actions must be in the form of a user instruction that you can follow.
43
+ - Suggested actions could include actions that create artifacts.
44
+ - After creating an artifact use a tool to add it to the chat context.
45
+
46
+ ## Navigation
47
+
48
+ - When the user asks to open, go to, navigate to, or show a specific document, page, or object, use the navigation tools instead of loading content inline.
49
+ - First, call the Resolve navigation targets tool to find the navigation path for the requested item.
50
+ - Without arguments, it returns available pages that can be navigated to.
51
+ - With a DXN in the query, it resolves that specific object to a navigation path.
52
+ - If you know the object's DXN (e.g. from a database query or context objects), pass it in the query to get the exact navigation path.
53
+ - Then, call the Open tool with the resolved navigation path(s) in the subject array to navigate the user to that item.
54
+ - Do not respond with the document's content when the user asks to open it — open it in the main content area instead.
55
+ - If multiple targets match, present the options to the user and let them choose which to open.
56
+
57
+ ## Response format
58
+
59
+ - NEVER output narration text outside `<status>` tags or the final user-facing answer. Any progress update, transition sentence, or "let me…" lead-in MUST be wrapped in `<status>...</status>`. Violating this is an error.
60
+ - When updating the user about the progress of the work you are doing, put the update in a <status> XML tag.
61
+ - Only when you need to show the result to the user or ask a question use a text block without status tags.
62
+ - After a `</status>` tag, the next output must be either another `<status>` tag, a tool call, or the final user-facing answer — never a transition sentence.
63
+
64
+ <example>
65
+ // reasoning...
66
+ <status>Looking for emails from Depot</status>
67
+ // tool call..
68
+ <status>Found 2 emails from Depot.</status>
69
+ <status>Loading email details</status>
70
+ // tool call
71
+ <status>Located mailbox</status>
72
+ I found **2 emails from Depot**:
73
+ </example>
74
+
75
+ Do not do (no status tags):
76
+
77
+ <incorrect_example>
78
+ // reasoning...
79
+ // tool call..
80
+ I found 2 emails from Depot. Let me load their details:
81
+ // tool call
82
+ I found **2 emails from Depot**:
83
+ </incorrect_example>
84
+
85
+ Do not do (transition sentence after a status tag, before tool calls):
86
+
87
+ <incorrect_example>
88
+ <status>Doing X</status>
89
+ Let me start by doing X:
90
+ // tool call
91
+ </incorrect_example>
92
+
93
+ - NEVER write a lead-in sentence before a tool call. Use a `<status>` tag instead.
94
+ - Avoid using plain text to tell the user what you are about to do; instead, use status blocks.
95
+ - WRONG: Let me get...
96
+ - CORRECT: <status>Getting...</status>
@@ -0,0 +1,42 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import defaultsDeep from 'lodash.defaultsdeep';
6
+
7
+ import { type ModelCapabilities } from '@dxos/ai';
8
+ import { Template } from '@dxos/compute';
9
+
10
+ import FORMAT from './instructions/format.tpl?raw';
11
+ import SYSTEM from './instructions/system.tpl?raw';
12
+
13
+ /**
14
+ * System template variables.
15
+ */
16
+ export type SystemPromptOptions = ModelCapabilities & {
17
+ DATETIME: string;
18
+ };
19
+
20
+ /**
21
+ * Base system prompt.
22
+ * NOTE: This contains protocol instructions that can not be dynamically edited.
23
+ */
24
+ export const createSystemPrompt = (options: Partial<SystemPromptOptions> = {}) =>
25
+ Template.process<SystemPromptOptions>(
26
+ FORMAT,
27
+ defaultsDeep(options, {
28
+ DATETIME: new Date().toISOString(),
29
+ }),
30
+ );
31
+
32
+ /**
33
+ * NOTE: We are forbidden from accessing randomness at model level in cloudflare, so we have to have pre-generated ids.
34
+ */
35
+ const SYSTEM_ID = '01K2J2FZWH39F55CS7B26GP1Q3';
36
+
37
+ export const templates = {
38
+ system: Template.make({
39
+ id: SYSTEM_ID,
40
+ source: SYSTEM,
41
+ }),
42
+ };
package/src/typings.d.ts CHANGED
@@ -2,6 +2,11 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
+ declare module '*?raw' {
6
+ const content: string;
7
+ export default content;
8
+ }
9
+
5
10
  declare module '*.tpl?raw' {
6
11
  const content: string;
7
12
  export default content;
@@ -0,0 +1,88 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import type * as Effect from 'effect/Effect';
6
+ import * as Schema from 'effect/Schema';
7
+
8
+ import { Database, type Err, Obj, Ref, type Type } from '@dxos/echo';
9
+ import { EncodedReference } from '@dxos/echo-protocol';
10
+ import { DXN, LOCAL_SPACE_TAG, type ObjectId, type SpaceId } from '@dxos/keys';
11
+ import { trim } from '@dxos/util';
12
+
13
+ /**
14
+ * @deprecated
15
+ */
16
+ export const createArtifactElement = (id: ObjectId) => `<artifact id=${id} />`;
17
+
18
+ /**
19
+ * A model-friendly way to reference an object.
20
+ * Supports vairous formats that will be normalized to a DXN.
21
+ *
22
+ * @deprecated Use `Ref.Ref(XXX)` instead.
23
+ */
24
+ // TODO(burdon): Rename RefFromLLM?
25
+ export const ArtifactId: Schema.Schema<string> & {
26
+ toDXN: (reference: ArtifactId, owningSpaceId?: SpaceId) => DXN;
27
+ resolve: <S extends Type.AnyEntity>(
28
+ schema: S,
29
+ ref: ArtifactId,
30
+ ) => Effect.Effect<Schema.Schema.Type<S>, Err.ObjectNotFoundError, Database.Service>;
31
+ } = class extends Schema.String.annotations({
32
+ // TODO(dmaretskyi): This section gets overriden.
33
+ description: trim`
34
+ The ID of the referenced object. Formats accepted:
35
+ - DXN (dxn:echo:@:01KG7R1ZXWFMWQ4DA1Q6TN1DG4). DXNs can be prepended with an @ symbol for compatibility with in-text references.
36
+ - space ID, object ID tuple (spaceID:objectID)
37
+ - Only object ID that is assumed to be in the current space (01KG7R1ZXWFMWQ4DA1Q6TN1DG4)
38
+ `,
39
+ examples: [
40
+ 'dxn:echo:@:01KG7R1ZXWFMWQ4DA1Q6TN1DG4',
41
+ '@dxn:echo:@:01KG7R1ZXWFMWQ4DA1Q6TN1DG4',
42
+ 'BM3FSHFOMJCHCG5QW7JTVKGYABD2GAA7G:01KG7R1ZXWFMWQ4DA1Q6TN1DG4',
43
+ '01KG7R1ZXWFMWQ4DA1Q6TN1DG4',
44
+ ],
45
+ }) {
46
+ static toDXN(reference: ArtifactId, owningSpaceId?: SpaceId): DXN {
47
+ // Allow @dxn: prefix for compatibility with in-text references.
48
+ if (reference.startsWith('@dxn:')) {
49
+ return DXN.parse(reference.slice(1));
50
+ } else if (reference.startsWith('dxn:')) {
51
+ return DXN.parse(reference);
52
+ } else if (/^[A-Z0-9]+:[A-Z0-9]+$/.test(reference)) {
53
+ const [spaceId, objectId] = reference.split(':');
54
+ // This is a workaround because the current Filter API doesn't work with fully qualified Echo DXNs.
55
+ // We check if the space ID is the same as the owning space and then use LOCAL_SPACE_TAG for local references.
56
+ // TODO(dmaretskyi): Fix this in the Echo and Filter API to properly handle fully qualified DXNs.
57
+ return new DXN(DXN.kind.ECHO, [spaceId === owningSpaceId ? LOCAL_SPACE_TAG : spaceId, objectId]);
58
+ } else if (/^[A-Z0-9]+$/.test(reference)) {
59
+ return DXN.fromLocalObjectId(reference);
60
+ } else {
61
+ throw new Error(`Unable to parse object reference: ${reference}`);
62
+ }
63
+ }
64
+
65
+ /**
66
+ * Resolves an artifact ID to an object.
67
+ */
68
+ static resolve<S extends Type.AnyEntity>(
69
+ schema: S,
70
+ ref: ArtifactId,
71
+ ): Effect.Effect<Schema.Schema.Type<S>, Err.ObjectNotFoundError, Database.Service> {
72
+ const dxn = ArtifactId.toDXN(ref);
73
+ return Database.resolve(dxn, schema);
74
+ }
75
+ };
76
+
77
+ export type ArtifactId = Schema.Schema.Type<typeof ArtifactId>;
78
+
79
+ /**
80
+ * Schema that decodes ECHO reference object from an LLM-friendly input.
81
+ */
82
+ export const RefFromLLM = Schema.transform(ArtifactId, Ref.Ref(Obj.Unknown), {
83
+ decode: (fromA, fromI) => EncodedReference.fromDXN(ArtifactId.toDXN(fromA)),
84
+ encode: (toI, toA) => EncodedReference.toDXN(toI).toString(),
85
+ strict: false,
86
+ }).annotations({
87
+ description: ArtifactId.ast.annotations.description as string,
88
+ });