@dxos/assistant 0.8.4-main.7ace549 → 0.8.4-main.8360d9e660

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 (97) hide show
  1. package/README.md +1 -0
  2. package/dist/lib/neutral/chunk-BE6SRJ4N.mjs +193 -0
  3. package/dist/lib/neutral/chunk-BE6SRJ4N.mjs.map +7 -0
  4. package/dist/lib/{browser → neutral}/extraction/index.mjs +6 -8
  5. package/dist/lib/neutral/extraction/index.mjs.map +7 -0
  6. package/dist/lib/{browser → neutral}/index.mjs +449 -410
  7. package/dist/lib/neutral/index.mjs.map +7 -0
  8. package/dist/lib/neutral/meta.json +1 -0
  9. package/dist/lib/neutral/testing/index.mjs +39 -0
  10. package/dist/lib/neutral/testing/index.mjs.map +7 -0
  11. package/dist/types/src/conversation/context.d.ts +99 -50
  12. package/dist/types/src/conversation/context.d.ts.map +1 -1
  13. package/dist/types/src/conversation/context.test.d.ts +2 -0
  14. package/dist/types/src/conversation/context.test.d.ts.map +1 -0
  15. package/dist/types/src/conversation/conversation.d.ts +40 -15
  16. package/dist/types/src/conversation/conversation.d.ts.map +1 -1
  17. package/dist/types/src/conversation/conversation.test.d.ts +2 -0
  18. package/dist/types/src/conversation/conversation.test.d.ts.map +1 -0
  19. package/dist/types/src/diff.d.ts +3 -1
  20. package/dist/types/src/diff.d.ts.map +1 -1
  21. package/dist/types/src/errors.d.ts +12 -16
  22. package/dist/types/src/errors.d.ts.map +1 -1
  23. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +265 -256
  24. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
  25. package/dist/types/src/extraction/extraction.d.ts +290 -242
  26. package/dist/types/src/extraction/extraction.d.ts.map +1 -1
  27. package/dist/types/src/extraction/quotes.d.ts +2 -2
  28. package/dist/types/src/extraction/quotes.d.ts.map +1 -1
  29. package/dist/types/src/functions/services.d.ts +6 -7
  30. package/dist/types/src/functions/services.d.ts.map +1 -1
  31. package/dist/types/src/functions.test.d.ts +2 -0
  32. package/dist/types/src/functions.test.d.ts.map +1 -0
  33. package/dist/types/src/session/format.d.ts +3 -3
  34. package/dist/types/src/session/format.d.ts.map +1 -1
  35. package/dist/types/src/session/index.d.ts +1 -1
  36. package/dist/types/src/session/index.d.ts.map +1 -1
  37. package/dist/types/src/session/observer.d.ts +4 -3
  38. package/dist/types/src/session/observer.d.ts.map +1 -1
  39. package/dist/types/src/session/session.d.ts +23 -13
  40. package/dist/types/src/session/session.d.ts.map +1 -1
  41. package/dist/types/src/session/toolkit.d.ts +9 -6
  42. package/dist/types/src/session/toolkit.d.ts.map +1 -1
  43. package/dist/types/src/session/version-pin.d.ts +1 -1
  44. package/dist/types/src/session/version-pin.d.ts.map +1 -1
  45. package/dist/types/src/testing/index.d.ts +2 -0
  46. package/dist/types/src/testing/index.d.ts.map +1 -0
  47. package/dist/types/src/testing/layer.d.ts +24 -0
  48. package/dist/types/src/testing/layer.d.ts.map +1 -0
  49. package/dist/types/src/types/artifact.d.ts +12 -3
  50. package/dist/types/src/types/artifact.d.ts.map +1 -1
  51. package/dist/types/tsconfig.tsbuildinfo +1 -1
  52. package/package.json +41 -33
  53. package/src/conversation/context.test.ts +46 -0
  54. package/src/conversation/context.ts +214 -69
  55. package/src/conversation/conversation.test.ts +65 -0
  56. package/src/conversation/conversation.ts +170 -51
  57. package/src/diff.test.ts +35 -4
  58. package/src/diff.ts +13 -4
  59. package/src/errors.ts +2 -2
  60. package/src/execution-graph.test.ts +2 -2
  61. package/src/execution-graph.ts +4 -4
  62. package/src/extraction/extraction-llm-function/extraction-llm-function.ts +1 -1
  63. package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +4 -4
  64. package/src/extraction/extraction-ner-function/extraction-ner-function.ts +2 -2
  65. package/src/extraction/extraction.ts +4 -4
  66. package/src/extraction/quotes.ts +3 -4
  67. package/src/functions/services.ts +77 -30
  68. package/src/functions.conversations.json +1 -0
  69. package/src/functions.test.ts +74 -0
  70. package/src/session/format.test.ts +101 -38
  71. package/src/session/format.ts +11 -12
  72. package/src/session/index.ts +1 -2
  73. package/src/session/observer.ts +4 -1
  74. package/src/session/session.conversations.json +1 -1
  75. package/src/session/session.test.ts +59 -17
  76. package/src/session/session.ts +94 -56
  77. package/src/session/toolkit.ts +21 -25
  78. package/src/session/version-pin.ts +2 -2
  79. package/src/templates/instructions/format.tpl +0 -4
  80. package/src/testing/index.ts +5 -0
  81. package/src/testing/layer.ts +92 -0
  82. package/src/types/artifact.ts +30 -11
  83. package/dist/lib/browser/chunk-J5LGTIGS.mjs +0 -10
  84. package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +0 -7
  85. package/dist/lib/browser/extraction/index.mjs.map +0 -7
  86. package/dist/lib/browser/index.mjs.map +0 -7
  87. package/dist/lib/browser/meta.json +0 -1
  88. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +0 -11
  89. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +0 -7
  90. package/dist/lib/node-esm/extraction/index.mjs +0 -226
  91. package/dist/lib/node-esm/extraction/index.mjs.map +0 -7
  92. package/dist/lib/node-esm/index.mjs +0 -1290
  93. package/dist/lib/node-esm/index.mjs.map +0 -7
  94. package/dist/lib/node-esm/meta.json +0 -1
  95. package/dist/types/src/session/GenericToolkit.d.ts +0 -84
  96. package/dist/types/src/session/GenericToolkit.d.ts.map +0 -1
  97. package/src/session/GenericToolkit.ts +0 -130
@@ -1,30 +1,37 @@
1
1
  import {
2
- __export
3
- } from "./chunk-J5LGTIGS.mjs";
2
+ ArtifactId,
3
+ RefFromLLM,
4
+ ToolExecutionServices,
5
+ createArtifactElement,
6
+ makeToolExecutionServiceFromFunctions,
7
+ makeToolResolverFromFunctions
8
+ } from "./chunk-BE6SRJ4N.mjs";
4
9
 
5
10
  // src/conversation/context.ts
6
- import { computed } from "@preact/signals-core";
7
- import * as Array2 from "effect/Array";
11
+ import { Atom, Registry } from "@effect-atom/atom-react";
12
+ import * as EArray from "effect/Array";
8
13
  import * as Context from "effect/Context";
14
+ import * as Effect from "effect/Effect";
9
15
  import * as Function from "effect/Function";
10
16
  import * as Schema from "effect/Schema";
11
17
  import { Blueprint } from "@dxos/blueprints";
12
18
  import { Resource } from "@dxos/context";
13
- import { DXN, Filter, Obj, Query, Type } from "@dxos/echo";
14
- import { invariant } from "@dxos/invariant";
15
- import { ComplexSet } from "@dxos/util";
19
+ import { DXN, Obj, Query, Ref, Type } from "@dxos/echo";
20
+ import { assertArgument } from "@dxos/invariant";
21
+ import { log } from "@dxos/log";
22
+ import { ComplexSet, isTruthy } from "@dxos/util";
16
23
  var __dxlog_file = "/__w/dxos/dxos/packages/core/assistant/src/conversation/context.ts";
17
24
  var ContextBinding = Schema.Struct({
18
25
  blueprints: Schema.Struct({
19
- added: Schema.Array(Type.Ref(Blueprint.Blueprint)),
20
- removed: Schema.Array(Type.Ref(Blueprint.Blueprint))
26
+ added: Schema.Array(Ref.Ref(Blueprint.Blueprint)),
27
+ removed: Schema.Array(Ref.Ref(Blueprint.Blueprint))
21
28
  }),
22
29
  objects: Schema.Struct({
23
- added: Schema.Array(Type.Ref(Obj.Any)),
24
- removed: Schema.Array(Type.Ref(Obj.Any))
30
+ added: Schema.Array(Ref.Ref(Obj.Unknown)),
31
+ removed: Schema.Array(Ref.Ref(Obj.Unknown))
25
32
  })
26
- }).pipe(Type.Obj({
27
- typename: "dxos.org/type/ContextBinding",
33
+ }).pipe(Type.object({
34
+ typename: "org.dxos.type.context-binding",
28
35
  version: "0.1.0"
29
36
  }));
30
37
  var Bindings = class {
@@ -33,148 +40,253 @@ var Bindings = class {
33
40
  objects = new ComplexSet((ref) => ref.dxn.asEchoDXN()?.echoId);
34
41
  toJSON() {
35
42
  return {
36
- blueprints: Array2.fromIterable(this.blueprints).map((ref) => ref.dxn.toString()),
37
- objects: Array2.fromIterable(this.objects).map((ref) => ref.dxn.toString())
43
+ blueprints: EArray.fromIterable(this.blueprints).map((ref) => ref.dxn.toString()),
44
+ objects: EArray.fromIterable(this.objects).map((ref) => ref.dxn.toString())
38
45
  };
39
46
  }
40
47
  };
41
48
  var AiContextBinder = class extends Resource {
49
+ _blueprints = Atom.make([]).pipe(Atom.keepAlive);
50
+ _objects = Atom.make([]).pipe(Atom.keepAlive);
51
+ _registry;
42
52
  _queue;
53
+ constructor(options) {
54
+ super();
55
+ assertArgument(options.queue, "options.queue", "Queue is required");
56
+ this._queue = options.queue;
57
+ this._registry = options.registry ?? Registry.make();
58
+ }
43
59
  /**
44
- * Reactive query of all bindings.
60
+ * Returns the blueprints atom for subscription.
45
61
  */
46
- // TODO(burdon): Cache value?
47
- _bindings;
48
- _blueprints;
49
- _objects;
50
- constructor(_queue) {
51
- super(), this._queue = _queue;
52
- }
53
- get bindings() {
54
- invariant(this._bindings, "AiContextBinder not open", {
55
- F: __dxlog_file,
56
- L: 77,
57
- S: this,
58
- A: [
59
- "this._bindings",
60
- "'AiContextBinder not open'"
61
- ]
62
- });
63
- return this._bindings;
64
- }
65
62
  get blueprints() {
66
- invariant(this._blueprints, "AiContextBinder not open", {
67
- F: __dxlog_file,
68
- L: 82,
69
- S: this,
70
- A: [
71
- "this._blueprints",
72
- "'AiContextBinder not open'"
73
- ]
74
- });
75
63
  return this._blueprints;
76
64
  }
65
+ /**
66
+ * Returns the objects atom for subscription.
67
+ */
77
68
  get objects() {
78
- invariant(this._objects, "AiContextBinder not open", {
79
- F: __dxlog_file,
80
- L: 87,
81
- S: this,
82
- A: [
83
- "this._objects",
84
- "'AiContextBinder not open'"
85
- ]
86
- });
87
69
  return this._objects;
88
70
  }
89
- // TODO(wittjosiah): Use parent context?
90
- async _open() {
91
- const query = this._queue.query(Query.select(Filter.everything()));
92
- this._ctx.onDispose(query.subscribe(() => {
93
- }));
94
- this._bindings = computed(() => this._reduce(query.objects));
95
- this._blueprints = computed(() => [
96
- ...this.bindings.value.blueprints
97
- ]);
98
- this._objects = computed(() => [
99
- ...this.bindings.value.objects
100
- ]);
71
+ /**
72
+ * Gets the current blueprints value.
73
+ */
74
+ getBlueprints() {
75
+ return this._registry.get(this._blueprints);
101
76
  }
102
- async _close() {
103
- this._bindings = void 0;
104
- this._blueprints = void 0;
105
- this._objects = void 0;
77
+ /**
78
+ * Gets the current objects value.
79
+ */
80
+ getObjects() {
81
+ return this._registry.get(this._objects);
82
+ }
83
+ /**
84
+ * Subscribe to changes in blueprints.
85
+ */
86
+ subscribeBlueprints(cb) {
87
+ return this._registry.subscribe(this._blueprints, () => cb(this._registry.get(this._blueprints)));
106
88
  }
107
89
  /**
108
- * Asynchronous query of all bindings.
90
+ * Subscribe to changes in objects.
109
91
  */
110
- async query() {
111
- const { objects } = await this._queue.query(Query.select(Filter.everything())).run();
112
- return this._reduce(objects);
92
+ subscribeObjects(cb) {
93
+ return this._registry.subscribe(this._objects, () => cb(this._registry.get(this._objects)));
94
+ }
95
+ async _open() {
96
+ const query = this._queue.query(Query.type(ContextBinding));
97
+ const initialResults = await query.run();
98
+ await this._updateBindings(initialResults);
99
+ this._ctx.onDispose(query.subscribe(async () => {
100
+ await this._updateBindings(query.results);
101
+ }));
113
102
  }
114
- // TODO(burdon): Pass in Blueprint obj (from registry?) and create reference.
115
- async bind(props) {
116
- const blueprints = props.blueprints?.filter((ref) => !this.blueprints.peek().find((b) => b.dxn.toString() === ref.dxn.toString())) ?? [];
117
- const objects = props.objects?.filter((ref) => !this.objects.peek().find((o) => o.dxn.toString() === ref.dxn.toString())) ?? [];
118
- if (!blueprints.length && !objects.length) {
103
+ async _updateBindings(items) {
104
+ if (items.length === 0) {
119
105
  return;
120
106
  }
107
+ const bindings = this._reduce(items);
108
+ const currentBlueprints = this._registry.get(this._blueprints);
109
+ const currentObjects = this._registry.get(this._objects);
110
+ const newBlueprints = await this._resolve(bindings.blueprints, currentBlueprints);
111
+ const newObjects = await this._resolve(bindings.objects, currentObjects);
112
+ this._registry.set(this._blueprints, newBlueprints);
113
+ this._registry.set(this._objects, newObjects);
114
+ log("updated bindings", {
115
+ blueprints: newBlueprints.length,
116
+ objects: newObjects.length
117
+ }, {
118
+ F: __dxlog_file,
119
+ L: 159,
120
+ S: this,
121
+ C: (f, a) => f(...a)
122
+ });
123
+ }
124
+ async _close() {
125
+ this._registry.set(this._blueprints, []);
126
+ this._registry.set(this._objects, []);
127
+ }
128
+ async bind({ blueprints, objects }) {
129
+ const currentBlueprints = this._registry.get(this._blueprints);
130
+ const currentObjects = this._registry.get(this._objects);
131
+ const { added: addedBlueprints, next: nextBlueprints } = this._processBindings(blueprints, currentBlueprints);
132
+ const { added: addedObjects, next: nextObjects } = this._processBindings(objects, currentObjects);
133
+ if (!addedBlueprints.length && !addedObjects.length) {
134
+ return;
135
+ }
136
+ this._registry.set(this._blueprints, nextBlueprints);
137
+ this._registry.set(this._objects, nextObjects);
138
+ log("bind", {
139
+ blueprints: addedBlueprints.length,
140
+ objects: addedObjects.length
141
+ }, {
142
+ F: __dxlog_file,
143
+ L: 185,
144
+ S: this,
145
+ C: (f, a) => f(...a)
146
+ });
121
147
  await this._queue.append([
122
148
  Obj.make(ContextBinding, {
123
149
  blueprints: {
124
- added: blueprints,
150
+ added: addedBlueprints,
125
151
  removed: []
126
152
  },
127
153
  objects: {
128
- added: objects,
154
+ added: addedObjects,
129
155
  removed: []
130
156
  }
131
157
  })
132
158
  ]);
133
159
  }
134
- async unbind(props) {
135
- if (!props.blueprints?.length && !props.objects?.length) {
160
+ async unbind({ blueprints, objects }) {
161
+ if (!blueprints?.length && !objects?.length) {
136
162
  return;
137
163
  }
164
+ log("unbind", {
165
+ blueprints: blueprints?.length,
166
+ objects: objects?.length
167
+ }, {
168
+ F: __dxlog_file,
169
+ L: 205,
170
+ S: this,
171
+ C: (f, a) => f(...a)
172
+ });
138
173
  await this._queue.append([
139
174
  Obj.make(ContextBinding, {
140
175
  blueprints: {
141
176
  added: [],
142
- removed: props.blueprints ?? []
177
+ removed: blueprints ?? []
143
178
  },
144
179
  objects: {
145
180
  added: [],
146
- removed: props.objects ?? []
181
+ removed: objects ?? []
147
182
  }
148
183
  })
149
184
  ]);
150
185
  }
186
+ /**
187
+ * Process bindings to filter duplicates and determine next state.
188
+ */
189
+ _processBindings(refs, current) {
190
+ const next = [
191
+ ...current
192
+ ];
193
+ const added = [];
194
+ if (!refs?.length) {
195
+ return {
196
+ added,
197
+ next
198
+ };
199
+ }
200
+ const seen = new Set(current.map((obj) => Obj.getDXN(obj).toString()));
201
+ for (const ref of refs) {
202
+ const dxn = ref.dxn.toString();
203
+ if (!seen.has(dxn)) {
204
+ seen.add(dxn);
205
+ added.push(ref);
206
+ if (ref.isAvailable) {
207
+ const target = ref.target;
208
+ if (target) {
209
+ next.push(target);
210
+ }
211
+ }
212
+ }
213
+ }
214
+ return {
215
+ added,
216
+ next
217
+ };
218
+ }
219
+ /**
220
+ * Reduce results into sets of blueprints and objects.
221
+ */
151
222
  _reduce(items) {
152
- return Function.pipe(items, Array2.filter(Obj.instanceOf(ContextBinding)), Array2.reduce(new Bindings(), (context, item) => {
153
- item.blueprints.removed.forEach((ref) => context.blueprints.delete(ref));
154
- item.blueprints.added.forEach((ref) => context.blueprints.add(ref));
155
- item.objects.removed.forEach((ref) => {
223
+ return Function.pipe(items, EArray.reduce(new Bindings(), (context, { blueprints, objects }) => {
224
+ blueprints.added.forEach((ref) => context.blueprints.add(ref));
225
+ blueprints.removed.forEach((ref) => context.blueprints.delete(ref));
226
+ objects.added.forEach((ref) => context.objects.add(ref));
227
+ objects.removed.forEach((ref) => {
156
228
  for (const obj of context.objects) {
157
229
  if (DXN.equalsEchoId(obj.dxn, ref.dxn)) {
158
230
  context.objects.delete(obj);
159
231
  }
160
232
  }
161
233
  });
162
- item.objects.added.forEach((ref) => context.objects.add(ref));
163
234
  return context;
164
235
  }));
165
236
  }
237
+ /**
238
+ * Resolve references to objects, loading them first if needed and falling back to existing objects.
239
+ */
240
+ async _resolve(refs, current) {
241
+ const refArray = [
242
+ ...refs
243
+ ];
244
+ await Promise.all(refArray.map((ref) => ref.tryLoad()));
245
+ return refArray.map((ref) => {
246
+ let target;
247
+ if (ref.isAvailable) {
248
+ target = ref.target;
249
+ }
250
+ return target ?? current.find((obj) => Obj.getDXN(obj).toString() === ref.dxn.toString());
251
+ }).filter(isTruthy);
252
+ }
166
253
  };
167
- var AiContextService = class extends Context.Tag("@dxos/assistant/AiContextService")() {
254
+ var AiContextService = class _AiContextService extends Context.Tag("@dxos/assistant/AiContextService")() {
255
+ static bindContext = ({ blueprints, objects }) => Effect.gen(function* () {
256
+ const { binder } = yield* _AiContextService;
257
+ yield* Effect.promise(() => binder.bind({
258
+ blueprints,
259
+ objects
260
+ }));
261
+ });
262
+ static findObjects = (type) => {
263
+ return Effect.gen(function* () {
264
+ const { binder } = yield* _AiContextService;
265
+ return binder.getObjects().filter(Obj.instanceOf(type));
266
+ });
267
+ };
268
+ /**
269
+ * Glorified type cast until we figure out function service typing.
270
+ * For context, the AiContextService is provided to functions called from tool calls, but it's not implemented in types yet.
271
+ * @deprecated
272
+ */
273
+ static fixFunctionHandlerType = (eff) => eff;
168
274
  };
169
275
 
170
276
  // src/conversation/conversation.ts
171
- import * as Array5 from "effect/Array";
277
+ import * as Array3 from "effect/Array";
278
+ import * as Context3 from "effect/Context";
172
279
  import * as Effect6 from "effect/Effect";
173
- import * as Option2 from "effect/Option";
280
+ import * as Either from "effect/Either";
281
+ import { pipe as pipe3 } from "effect/Function";
282
+ import * as Layer from "effect/Layer";
174
283
  import { Resource as Resource2 } from "@dxos/context";
175
284
  import { Obj as Obj4 } from "@dxos/echo";
176
- import { DatabaseService as DatabaseService2 } from "@dxos/echo-db";
177
- import { log as log3 } from "@dxos/log";
285
+ import { acquireReleaseResource } from "@dxos/effect";
286
+ import { QueueService } from "@dxos/functions";
287
+ import { invariant } from "@dxos/invariant";
288
+ import { log as log4 } from "@dxos/log";
289
+ import { McpToolkit } from "@dxos/mcp-client";
178
290
  import { Message as Message3 } from "@dxos/types";
179
291
 
180
292
  // src/session/artifact-diff.ts
@@ -183,10 +295,10 @@ var ArtifactDiffResolver = class extends Context2.Tag("@dxos/assistant/ArtifactD
183
295
  };
184
296
 
185
297
  // src/session/observer.ts
186
- import * as Effect from "effect/Effect";
298
+ import * as Effect2 from "effect/Effect";
187
299
  import * as Function2 from "effect/Function";
188
300
  var GenerationObserver = Object.freeze({
189
- make: ({ onBegin = Function2.constant(Effect.void), onPart = Function2.constant(Effect.void), onBlock = Function2.constant(Effect.void), onEnd = Function2.constant(Effect.void), onMessage = Function2.constant(Effect.void) } = {}) => ({
301
+ make: ({ onBegin = Function2.constant(Effect2.void), onPart = Function2.constant(Effect2.void), onBlock = Function2.constant(Effect2.void), onEnd = Function2.constant(Effect2.void), onMessage = Function2.constant(Effect2.void) } = {}) => ({
190
302
  onBegin,
191
303
  onPart,
192
304
  onBlock,
@@ -198,13 +310,13 @@ var GenerationObserver = Object.freeze({
198
310
  * Debug printer to be used in unit-tests and browser devtools.
199
311
  */
200
312
  fromPrinter: (printer) => GenerationObserver.make({
201
- onBlock: (block) => Effect.sync(() => {
313
+ onBlock: (block) => Effect2.sync(() => {
202
314
  if (block.pending) {
203
315
  return;
204
316
  }
205
317
  printer.printContentBlock(block);
206
318
  }),
207
- onMessage: (message) => Effect.sync(() => {
319
+ onMessage: (message) => Effect2.sync(() => {
208
320
  if (message.sender.role === "assistant") {
209
321
  return;
210
322
  }
@@ -216,66 +328,66 @@ var GenerationObserver = Object.freeze({
216
328
  // src/session/session.ts
217
329
  import * as LanguageModel from "@effect/ai/LanguageModel";
218
330
  import * as Chunk from "effect/Chunk";
219
- import * as Effect3 from "effect/Effect";
331
+ import * as Effect4 from "effect/Effect";
220
332
  import * as Stream from "effect/Stream";
221
- import { AiParser, AiPreprocessor, callTool, getToolCalls } from "@dxos/ai";
222
- import { todo } from "@dxos/debug";
333
+ import { AiParser, AiPreprocessor, AiSummarizer, callTool, withoutToolCallParising } from "@dxos/ai";
223
334
  import { Obj as Obj3 } from "@dxos/echo";
224
335
  import { TracingService } from "@dxos/functions";
225
- import { log as log2 } from "@dxos/log";
336
+ import { log as log3 } from "@dxos/log";
226
337
  import { Message as Message2 } from "@dxos/types";
227
338
 
228
339
  // src/session/format.ts
229
- import * as Array4 from "effect/Array";
230
- import * as Effect2 from "effect/Effect";
340
+ import * as Effect3 from "effect/Effect";
231
341
  import * as Function3 from "effect/Function";
232
342
  import * as Option from "effect/Option";
233
343
  import { Template } from "@dxos/blueprints";
234
344
  import { Obj as Obj2 } from "@dxos/echo";
235
- import { DatabaseService, ObjectVersion } from "@dxos/echo-db";
236
- import { log } from "@dxos/log";
345
+ import { ObjectVersion } from "@dxos/echo-db";
346
+ import { log as log2 } from "@dxos/log";
237
347
  import { Message } from "@dxos/types";
238
348
  import { trim } from "@dxos/util";
239
349
 
240
350
  // src/errors.ts
241
351
  import { BaseError } from "@dxos/errors";
242
- var AiModelError = class extends BaseError.extend("AI_MODEL_ERROR", "AI model error") {
352
+ var AiModelError = class extends BaseError.extend("AiModelError", "AI model error") {
243
353
  };
244
- var AiAssistantError = class extends BaseError.extend("AI_ASSISTANT_ERROR", "AI assistant error") {
354
+ var AiAssistantError = class extends BaseError.extend("AiAssistantError", "AI assistant error") {
245
355
  };
246
356
 
247
357
  // src/session/format.ts
248
358
  var __dxlog_file2 = "/__w/dxos/dxos/packages/core/assistant/src/session/format.ts";
249
- var formatSystemPrompt = ({ system, blueprints = [], objects = [] }) => Effect2.gen(function* () {
250
- const blueprintDefs = yield* Function3.pipe(blueprints, Effect2.forEach((blueprint) => Effect2.succeed(blueprint.instructions)), Effect2.flatMap(Effect2.forEach((template) => DatabaseService.loadOption(template.source))), Effect2.map(Array4.filter(Option.isSome)), Effect2.map(Array4.map((template) => trim`
359
+ var formatSystemPrompt = ({ system, blueprints = [], objects = [] }) => Effect3.gen(function* () {
360
+ const blueprintDefs = yield* Function3.pipe(blueprints, Effect3.forEach((blueprint) => Effect3.succeed(blueprint.instructions)), Effect3.flatMap(Effect3.forEach((template) => Effect3.gen(function* () {
361
+ return trim`
251
362
  <blueprint>
252
- ${Template.process(template.value.content)}
363
+ ${yield* Template.processTemplate(template)}
253
364
  </blueprint>
254
- `)), Effect2.map((blueprints2) => blueprints2.length > 0 ? [
365
+ `;
366
+ }))), Effect3.map((blueprints2) => blueprints2.length > 0 ? [
255
367
  "## Blueprints Definitions",
256
368
  ...blueprints2
257
369
  ].join("\n\n") : void 0));
258
- const objectDefs = yield* Function3.pipe(objects, Effect2.forEach((object) => Effect2.succeed(trim`
370
+ const objectDefs = yield* Function3.pipe(objects, Effect3.forEach((object) => Effect3.succeed(trim`
259
371
  <object>
260
372
  <dxn>${Obj2.getDXN(object)}</dxn>
261
373
  <typename>${Obj2.getTypename(object)}</typename>
262
374
  </object>
263
- `)), Effect2.map((objects2) => objects2.length > 0 ? [
375
+ `)), Effect3.map((objects2) => objects2.length > 0 ? [
264
376
  "## Context Objects",
265
377
  ...objects2
266
378
  ].join("\n\n") : void 0));
267
- return yield* Function3.pipe(Effect2.succeed([
379
+ return yield* Function3.pipe(Effect3.succeed([
268
380
  system,
269
381
  blueprintDefs,
270
382
  objectDefs
271
- ].filter((def) => def !== void 0)), Effect2.map((parts) => parts.join("\n\n")));
383
+ ].filter((def) => def !== void 0)), Effect3.map((parts) => parts.join("\n\n")));
272
384
  });
273
- var formatUserPrompt = ({ prompt, history = [] }) => Effect2.gen(function* () {
385
+ var formatUserPrompt = ({ prompt, history = [] }) => Effect3.gen(function* () {
274
386
  const blocks = [];
275
- const artifactDiffResolver = yield* Effect2.serviceOption(ArtifactDiffResolver);
387
+ const artifactDiffResolver = yield* Effect3.serviceOption(ArtifactDiffResolver);
276
388
  if (Option.isSome(artifactDiffResolver)) {
277
389
  const versions = gatherObjectVersions(history);
278
- const artifactDiff = yield* Effect2.tryPromise({
390
+ const artifactDiff = yield* Effect3.tryPromise({
279
391
  try: () => artifactDiffResolver.value.resolve([
280
392
  ...versions.entries()
281
393
  ].map(([id, version]) => ({
@@ -286,12 +398,12 @@ var formatUserPrompt = ({ prompt, history = [] }) => Effect2.gen(function* () {
286
398
  message: "Artifact diff resolution error"
287
399
  })
288
400
  });
289
- log("version", {
401
+ log2("version", {
290
402
  artifactDiff,
291
403
  versions
292
404
  }, {
293
405
  F: __dxlog_file2,
294
- L: 95,
406
+ L: 94,
295
407
  S: this,
296
408
  C: (f, a) => f(...a)
297
409
  });
@@ -357,38 +469,61 @@ var __dxlog_file3 = "/__w/dxos/dxos/packages/core/assistant/src/session/session.
357
469
  var AiSession = class {
358
470
  _options;
359
471
  /** Prevents concurrent execution of session. */
360
- _semaphore = Effect3.runSync(Effect3.makeSemaphore(1));
472
+ _semaphore = Effect4.runSync(Effect4.makeSemaphore(1));
361
473
  /** Message history from queue. */
362
474
  // TODO(burdon): Evolve into supporting a git-like graph of messages.
363
475
  _history = [];
364
476
  /** Pending messages for this session (incl. the current prompt). */
365
477
  _pending = [];
478
+ _started = 0;
479
+ _ended = 0;
480
+ _toolCalls = 0;
366
481
  constructor(_options = {}) {
367
482
  this._options = _options;
368
483
  }
369
- run = ({ prompt, system: systemTemplate, history = [], objects = [], blueprints = [], toolkit, observer = GenerationObserver.noop() }) => Effect3.gen(this, function* () {
484
+ get duration() {
485
+ return this._ended - this._started;
486
+ }
487
+ get toolCalls() {
488
+ return this._toolCalls;
489
+ }
490
+ run = ({ prompt, system: systemTemplate, history = [], objects = [], blueprints = [], toolkit, observer = GenerationObserver.noop(), onOutput = () => Effect4.void }) => Effect4.gen(this, function* () {
491
+ this._started = Date.now();
370
492
  this._history = [
371
493
  ...history
372
494
  ];
373
495
  this._pending = [];
374
- const pending = this._pending;
375
- const submitMessage = Effect3.fnUntraced(function* (message) {
376
- pending.push(message);
496
+ const submitMessage = (message) => Effect4.gen(this, function* () {
497
+ this._pending.push(message);
377
498
  yield* observer.onMessage(message);
378
499
  yield* TracingService.emitConverationMessage(message);
500
+ yield* onOutput(message);
379
501
  return message;
380
502
  });
381
- const promptMessage = yield* submitMessage(yield* formatUserPrompt({
382
- prompt,
383
- history
384
- }));
385
503
  const system = yield* formatSystemPrompt({
386
504
  system: systemTemplate,
387
505
  blueprints,
388
506
  objects
389
- });
507
+ }).pipe(Effect4.orDie);
508
+ if (this._options.summarizationThreshold !== void 0) {
509
+ const tokenCount = yield* AiPreprocessor.estimateTokens(yield* AiPreprocessor.preprocessPrompt([
510
+ ...this._history
511
+ ], {
512
+ system
513
+ }));
514
+ if (tokenCount > this._options.summarizationThreshold) {
515
+ const summary = yield* AiSummarizer.summarize([
516
+ ...this._history
517
+ ]);
518
+ yield* submitMessage(summary);
519
+ }
520
+ }
521
+ const promptMessage = yield* submitMessage(yield* formatUserPrompt({
522
+ prompt,
523
+ history
524
+ }));
390
525
  do {
391
- log2("request", {
526
+ log3("request", {
392
527
  prompt: promptMessage,
393
528
  system: {
394
529
  snippet: createSnippet(system),
@@ -399,7 +534,7 @@ var AiSession = class {
399
534
  objects: objects?.length ?? 0
400
535
  }, {
401
536
  F: __dxlog_file3,
402
- L: 110,
537
+ L: 149,
403
538
  S: this,
404
539
  C: (f, a) => f(...a)
405
540
  });
@@ -407,13 +542,16 @@ var AiSession = class {
407
542
  ...this._history,
408
543
  ...this._pending
409
544
  ], {
410
- system
545
+ system,
546
+ cacheControl: "ephemeral"
411
547
  });
412
- const blocks = yield* LanguageModel.streamText({
548
+ const messages = yield* LanguageModel.streamText({
413
549
  prompt: prompt2,
414
550
  toolkit,
415
551
  disableToolCallResolution: true
416
552
  }).pipe(
553
+ // Disable tool call parsing so that we can handle parameter schema errors on a per-tool call basis.
554
+ withoutToolCallParising,
417
555
  // TOOD(dmaretskyi): Error mapping.
418
556
  AiParser.parseResponse({
419
557
  onBegin: () => observer.onBegin(),
@@ -421,27 +559,34 @@ var AiSession = class {
421
559
  onPart: (part) => observer.onPart(part),
422
560
  onEnd: (summary) => observer.onEnd(summary)
423
561
  }),
562
+ Stream.mapEffect((block) => Effect4.gen(function* () {
563
+ return yield* submitMessage(Obj3.make(Message2.Message, {
564
+ created: (/* @__PURE__ */ new Date()).toISOString(),
565
+ sender: {
566
+ role: "assistant"
567
+ },
568
+ blocks: [
569
+ block
570
+ ]
571
+ }));
572
+ })),
424
573
  Stream.runCollect,
425
- Effect3.map(Chunk.toArray)
574
+ Effect4.map(Chunk.toArray)
426
575
  );
427
- const response = yield* submitMessage(Obj3.make(Message2.Message, {
428
- created: (/* @__PURE__ */ new Date()).toISOString(),
429
- sender: {
430
- role: "assistant"
431
- },
432
- blocks
433
- }));
434
- const toolCalls = getToolCalls(response);
576
+ const toolCalls = messages.flatMap((message) => message.blocks.filter((block) => block._tag === "toolCall" && block.providerExecuted === false).map((block) => ({
577
+ block,
578
+ message
579
+ })));
435
580
  if (toolCalls.length === 0) {
436
581
  break;
437
582
  } else if (!toolkit) {
438
583
  throw new Error("No toolkit provided");
439
584
  }
440
- const toolResults = yield* Effect3.forEach(toolCalls, (toolCall) => {
441
- return callTool(toolkit, toolCall).pipe(Effect3.provide(TracingService.layerSubframe((context) => ({
585
+ const toolResults = yield* Effect4.forEach(toolCalls, ({ block, message }) => {
586
+ return callTool(toolkit, block).pipe(Effect4.provide(TracingService.layerSubframe((context) => ({
442
587
  ...context,
443
- parentMessage: response.id,
444
- toolCallId: toolCall.toolCallId
588
+ parentMessage: message.id,
589
+ toolCallId: block.toolCallId
445
590
  }))));
446
591
  });
447
592
  yield* submitMessage(Obj3.make(Message2.Message, {
@@ -451,24 +596,21 @@ var AiSession = class {
451
596
  },
452
597
  blocks: toolResults
453
598
  }));
599
+ this._toolCalls++;
454
600
  } while (true);
455
- log2("done", {
456
- pending: this._pending.length
601
+ this._ended = Date.now();
602
+ log3("done", {
603
+ pending: this._pending.length,
604
+ duration: this.duration,
605
+ tools: this._toolCalls
457
606
  }, {
458
607
  F: __dxlog_file3,
459
- L: 181,
608
+ L: 236,
460
609
  S: this,
461
610
  C: (f, a) => f(...a)
462
611
  });
463
612
  return this._pending;
464
- }).pipe(this._semaphore.withPermits(1), Effect3.withSpan("AiSession.run"));
465
- /**
466
- * @deprecated
467
- */
468
- // TODO(burdon): Implement or remove.
469
- async runStructured(_schema, _options) {
470
- return todo();
471
- }
613
+ }).pipe(this._semaphore.withPermits(1), Effect4.withSpan("AiSession.run"));
472
614
  };
473
615
  var createSnippet = (text, len = 32) => text.length <= len * 2 ? text : [
474
616
  text.slice(0, len),
@@ -478,20 +620,19 @@ var createSnippet = (text, len = 32) => text.length <= len * 2 ? text : [
478
620
 
479
621
  // src/session/toolkit.ts
480
622
  import * as Toolkit from "@effect/ai/Toolkit";
481
- import * as Effect4 from "effect/Effect";
482
- import { ToolExecutionService, ToolResolverService } from "@dxos/ai";
483
- import { isTruthy } from "@dxos/util";
484
- var createToolkit = ({ toolkit: toolkitParam, toolIds = [], blueprints = [] }) => Effect4.gen(function* () {
485
- const blueprintToolkit = yield* ToolResolverService.resolveToolkit([
486
- ...blueprints.flatMap(({ tools }) => tools),
487
- ...toolIds
488
- ]);
489
- const blueprintToolkitHandler = yield* blueprintToolkit.toContext(ToolExecutionService.handlersFor(blueprintToolkit));
623
+ import * as Effect5 from "effect/Effect";
624
+ import { GenericToolkit, ToolExecutionService, ToolResolverService } from "@dxos/ai";
625
+ import { isTruthy as isTruthy2 } from "@dxos/util";
626
+ var createToolkit = ({ toolkit: toolkitProp, blueprints = [], genericToolkits = [] }) => Effect5.gen(function* () {
627
+ const blueprintToolkit = yield* ToolResolverService.resolveToolkit(blueprints.flatMap(({ tools }) => tools));
628
+ const blueprintToolHandler = yield* blueprintToolkit.toContext(ToolExecutionService.handlersFor(blueprintToolkit));
629
+ const genericToolkit = GenericToolkit.merge(...genericToolkits);
490
630
  const toolkit = Toolkit.merge(...[
491
- toolkitParam,
492
- blueprintToolkit
493
- ].filter(isTruthy));
494
- return yield* toolkit.pipe(Effect4.provide(blueprintToolkitHandler));
631
+ toolkitProp,
632
+ blueprintToolkit,
633
+ genericToolkit.toolkit
634
+ ].filter(isTruthy2));
635
+ return yield* toolkit.pipe(Effect5.provide(blueprintToolHandler), Effect5.provide(genericToolkit.layer));
495
636
  });
496
637
 
497
638
  // src/session/version-pin.ts
@@ -522,61 +663,42 @@ var VersionPin = class extends VersionPinSchema {
522
663
  }
523
664
  };
524
665
 
525
- // src/session/GenericToolkit.ts
526
- var GenericToolkit_exports = {};
527
- __export(GenericToolkit_exports, {
528
- TypeId: () => TypeId,
529
- make: () => make,
530
- merge: () => merge3
531
- });
532
- import * as Toolkit2 from "@effect/ai/Toolkit";
533
- import * as Effect5 from "effect/Effect";
534
- import * as Layer from "effect/Layer";
535
- import * as Pipeable from "effect/Pipeable";
536
- var TypeId = "~@dxos/assistant/GenericToolkit";
537
- var make = (toolkit, layer) => {
538
- return {
539
- [TypeId]: TypeId,
540
- toolkit,
541
- layer,
542
- handlers: toolkit.pipe(Effect5.provide(layer)),
543
- pipe() {
544
- return Pipeable.pipeArguments(this, arguments);
545
- }
546
- };
547
- };
548
- var merge3 = (...toolkits) => {
549
- return make(Toolkit2.merge(...toolkits.map((t) => t.toolkit)), Layer.mergeAll(...toolkits.map((t) => t.layer)));
550
- };
551
-
552
666
  // src/conversation/conversation.ts
553
667
  var __dxlog_file4 = "/__w/dxos/dxos/packages/core/assistant/src/conversation/conversation.ts";
668
+ var SUMMARY_THRESHOLD = 8e4;
554
669
  var AiConversation = class extends Resource2 {
555
670
  /**
556
- * Message and binding queue.
671
+ * Blueprints and objects bound to the conversation.
557
672
  */
673
+ _binder;
558
674
  _queue;
559
- /**
560
- * Blueprints bound to the conversation.
561
- */
562
- _context;
563
- /**
564
- * Toolkit from the current session request.
565
- */
566
675
  _toolkit;
567
- constructor(queue) {
676
+ constructor(options) {
568
677
  super();
569
- this._queue = queue;
570
- this._context = new AiContextBinder(this._queue);
678
+ this._queue = options.queue;
679
+ this._toolkit = options.toolkit;
680
+ invariant(this._queue, void 0, {
681
+ F: __dxlog_file4,
682
+ L: 72,
683
+ S: this,
684
+ A: [
685
+ "this._queue",
686
+ ""
687
+ ]
688
+ });
689
+ this._binder = new AiContextBinder({
690
+ queue: this._queue,
691
+ registry: options.registry
692
+ });
571
693
  }
572
694
  async _open() {
573
- await this._context.open();
695
+ await this._binder.open(this._ctx);
574
696
  }
575
- async _close() {
576
- await this._context.close();
697
+ get queue() {
698
+ return this._queue;
577
699
  }
578
700
  get context() {
579
- return this._context;
701
+ return this._binder;
580
702
  }
581
703
  get toolkit() {
582
704
  return this._toolkit;
@@ -585,60 +707,130 @@ var AiConversation = class extends Resource2 {
585
707
  const queueItems = await this._queue.queryObjects();
586
708
  return queueItems.filter(Obj4.instanceOf(Message3.Message));
587
709
  }
710
+ getTools() {
711
+ return Effect6.gen(this, function* () {
712
+ const blueprints = this.context.getBlueprints();
713
+ const tookit = yield* createToolkit({
714
+ toolkit: this._toolkit,
715
+ blueprints
716
+ });
717
+ return tookit.tools;
718
+ }).pipe(Effect6.orDie);
719
+ }
588
720
  /**
589
721
  * Creates a new cancelable request effect.
590
722
  */
591
723
  createRequest(params) {
592
724
  return Effect6.gen(this, function* () {
593
- const session = new AiSession();
594
725
  const history = yield* Effect6.promise(() => this.getHistory());
595
- const context = yield* Effect6.promise(() => this.context.query());
596
- const blueprints = yield* Effect6.forEach(context.blueprints.values(), DatabaseService2.loadOption).pipe(Effect6.map(Array5.filter(Option2.isSome)), Effect6.map(Array5.map((option) => option.value)));
597
- const objects = yield* Effect6.forEach(context.objects.values(), DatabaseService2.loadOption).pipe(Effect6.map(Array5.filter(Option2.isSome)), Effect6.map(Array5.map((option) => option.value)));
726
+ const blueprints = this.context.getBlueprints();
727
+ const mcps = yield* connectMcpServers(blueprints);
598
728
  const toolkit = yield* createToolkit({
599
- blueprints
729
+ toolkit: this._toolkit,
730
+ blueprints,
731
+ genericToolkits: mcps
600
732
  });
601
- this._toolkit = toolkit;
602
- const start = Date.now();
603
- log3("run", {
733
+ const objects = this.context.getObjects();
734
+ log4("run", {
604
735
  history: history.length,
605
736
  blueprints: blueprints.length,
606
- objects: objects.length,
607
- tools: this._toolkit?.tools.length ?? 0
737
+ tools: Object.keys(toolkit.tools).length,
738
+ objects: objects.length
608
739
  }, {
609
740
  F: __dxlog_file4,
610
- L: 107,
741
+ L: 128,
611
742
  S: this,
612
743
  C: (f, a) => f(...a)
613
744
  });
745
+ const session = new AiSession({
746
+ summarizationThreshold: SUMMARY_THRESHOLD
747
+ });
614
748
  const messages = yield* session.run({
615
749
  history,
616
750
  blueprints,
617
- objects,
618
751
  toolkit,
619
- ...params
620
- }).pipe(Effect6.provideService(AiContextService, {
752
+ objects,
753
+ ...params,
754
+ onOutput: (message) => Effect6.promise(() => this._queue.append([
755
+ message
756
+ ]))
757
+ }).pipe(Effect6.provide(Layer.mergeAll(Layer.succeed(AiContextService, {
621
758
  binder: this.context
622
- }));
623
- log3("result", {
759
+ }), Layer.succeed(AiConversationService, this))));
760
+ log4("result", {
624
761
  messages: messages.length,
625
- duration: Date.now() - start
762
+ duration: session.duration,
763
+ toolCalls: session.toolCalls
626
764
  }, {
627
765
  F: __dxlog_file4,
628
- L: 121,
766
+ L: 159,
629
767
  S: this,
630
768
  C: (f, a) => f(...a)
631
769
  });
632
- yield* Effect6.promise(() => this._queue.append(messages));
633
770
  return messages;
634
- }).pipe(Effect6.withSpan("AiConversation.request"));
771
+ });
635
772
  }
636
773
  };
774
+ var AiConversationService = class _AiConversationService extends Context3.Tag("@dxos/assistant/AiConversationService")() {
775
+ /**
776
+ * Create a new conversation layer from options.
777
+ */
778
+ static layer = (options) => aiContextFromConversation.pipe(Layer.provideMerge(Layer.scoped(_AiConversationService, Effect6.gen(function* () {
779
+ const conversation = yield* acquireReleaseResource(() => new AiConversation(options));
780
+ return conversation;
781
+ }))));
782
+ /**
783
+ * Create a new conversation with a new queue.
784
+ */
785
+ static layerNewQueue = (options) => Layer.unwrapScoped(Effect6.gen(function* () {
786
+ return _AiConversationService.layer({
787
+ ...options,
788
+ queue: yield* QueueService.createQueue()
789
+ });
790
+ }));
791
+ /**
792
+ * Run a prompt in the current conversation.
793
+ */
794
+ static run = (params) => Effect6.gen(function* () {
795
+ const conversation = yield* _AiConversationService;
796
+ return yield* conversation.createRequest(params);
797
+ });
798
+ };
799
+ var aiContextFromConversation = Layer.effect(AiContextService, Effect6.gen(function* () {
800
+ const conversation = yield* AiConversationService;
801
+ return {
802
+ binder: conversation.context
803
+ };
804
+ }));
805
+ var connectMcpServers = (blueprints) => pipe3(blueprints, Array3.flatMap((_) => _.mcpServers ?? []), Effect6.forEach(({ url, protocol }) => McpToolkit.make({
806
+ url,
807
+ kind: protocol
808
+ }).pipe(
809
+ // NOTE: Type-inference fails here without explicit void return.
810
+ Effect6.tap((toolkit) => log4.info("Connected to MCP server", {
811
+ url,
812
+ tools: Object.keys(toolkit.toolkit.tools).length
813
+ }, {
814
+ F: __dxlog_file4,
815
+ L: 240,
816
+ S: void 0,
817
+ C: (f, a) => f(...a)
818
+ })),
819
+ Effect6.tapDefect((error) => Effect6.sync(() => log4.warn("Failed to connect to MCP server", {
820
+ error
821
+ }, {
822
+ F: __dxlog_file4,
823
+ L: 242,
824
+ S: void 0,
825
+ C: (f, a) => f(...a)
826
+ }))),
827
+ Effect6.either
828
+ )), Effect6.map(Array3.filterMap((_) => Either.getRight(_))));
637
829
 
638
830
  // src/diff.ts
639
831
  import { next as A } from "@automerge/automerge";
640
832
  import { DocAccessor, toCursorRange } from "@dxos/echo-db";
641
- import { log as log4 } from "@dxos/log";
833
+ import { log as log5 } from "@dxos/log";
642
834
  import { isNonNullable } from "@dxos/util";
643
835
  var __dxlog_file5 = "/__w/dxos/dxos/packages/core/assistant/src/diff.ts";
644
836
  var reduceDiffs = (diffs) => {
@@ -668,8 +860,10 @@ var reduceDiffs = (diffs) => {
668
860
  }
669
861
  return acc;
670
862
  }
863
+ default: {
864
+ throw new Error(`Invalid diff format: each line must start with "- " or "+ ", got: ${JSON.stringify(diff)}`);
865
+ }
671
866
  }
672
- return acc;
673
867
  }, []);
674
868
  };
675
869
  var computeDiffsWithCursors = (accessor, diffs) => {
@@ -684,7 +878,7 @@ var computeDiffsWithCursors = (accessor, diffs) => {
684
878
  }
685
879
  }).filter(isNonNullable);
686
880
  };
687
- var applyDiffs = (accessor, diffs) => {
881
+ var applyDiffs = (accessor, diffs, { errorOnNotFound = false } = {}) => {
688
882
  for (const diff of reduceDiffs(diffs)) {
689
883
  accessor.handle.change((doc) => {
690
884
  const text = DocAccessor.getValue(accessor);
@@ -692,12 +886,16 @@ var applyDiffs = (accessor, diffs) => {
692
886
  if (idx !== -1) {
693
887
  A.splice(doc, accessor.path, idx, diff.match.length, diff.replace);
694
888
  } else {
695
- log4.warn("diff not found", diff, {
696
- F: __dxlog_file5,
697
- L: 78,
698
- S: void 0,
699
- C: (f, a) => f(...a)
700
- });
889
+ if (errorOnNotFound) {
890
+ throw new Error(`Diff not found: ${diff.match}`);
891
+ } else {
892
+ log5.warn("diff not found", diff, {
893
+ F: __dxlog_file5,
894
+ L: 86,
895
+ S: void 0,
896
+ C: (f, a) => f(...a)
897
+ });
898
+ }
701
899
  }
702
900
  });
703
901
  }
@@ -993,7 +1191,7 @@ var createBlockCommit = (block, message, branch, parents, idx) => {
993
1191
  level: block.error ? LogLevel.ERROR : LogLevel.INFO,
994
1192
  message: block.error ? "Tool error: " + block.error : "Tool call succeeded"
995
1193
  };
996
- case "summary":
1194
+ case "stats":
997
1195
  return {
998
1196
  id: getGenericBlockId(message.id, idx),
999
1197
  branch,
@@ -1001,7 +1199,7 @@ var createBlockCommit = (block, message, branch, parents, idx) => {
1001
1199
  timestamp,
1002
1200
  icon: "ph--rocket--regular",
1003
1201
  level: LogLevel.INFO,
1004
- message: ContentBlock.createSummaryMessage(block)
1202
+ message: ContentBlock.createStatsMessage(block)
1005
1203
  };
1006
1204
  case "status":
1007
1205
  return {
@@ -1074,128 +1272,12 @@ var stringifyObject = (obj) => {
1074
1272
  return Obj5.getLabel(obj) ?? Obj5.getTypename(obj) ?? obj.id;
1075
1273
  };
1076
1274
 
1077
- // src/functions/services.ts
1078
- import * as Tool from "@effect/ai/Tool";
1079
- import * as Context3 from "effect/Context";
1080
- import * as Effect7 from "effect/Effect";
1081
- import * as Layer2 from "effect/Layer";
1082
- import * as Record from "effect/Record";
1083
- import * as Schema3 from "effect/Schema";
1084
- import { AiToolNotFoundError, ToolExecutionService as ToolExecutionService2, ToolResolverService as ToolResolverService2 } from "@dxos/ai";
1085
- import { todo as todo2 } from "@dxos/debug";
1086
- import { Query as Query2 } from "@dxos/echo";
1087
- import { DatabaseService as DatabaseService3 } from "@dxos/echo-db";
1088
- import { Function as Function4, FunctionDefinition, FunctionInvocationService } from "@dxos/functions";
1089
- import { invariant as invariant2 } from "@dxos/invariant";
1090
- var __dxlog_file6 = "/__w/dxos/dxos/packages/core/assistant/src/functions/services.ts";
1091
- var makeToolResolverFromFunctions = (functions, toolkit) => {
1092
- return Layer2.effect(ToolResolverService2, Effect7.gen(function* () {
1093
- const dbService = yield* DatabaseService3;
1094
- return {
1095
- resolve: (id) => Effect7.gen(function* () {
1096
- const tool = toolkit.tools[id];
1097
- if (tool) {
1098
- return tool;
1099
- }
1100
- const { objects: [dbFunction] } = yield* DatabaseService3.runQuery(Query2.type(Function4.Function, {
1101
- key: id
1102
- }));
1103
- const functionDef = dbFunction ? FunctionDefinition.deserialize(dbFunction) : functions.find((fn2) => fn2.key === id);
1104
- if (!functionDef) {
1105
- return yield* Effect7.fail(new AiToolNotFoundError(id));
1106
- }
1107
- return projectFunctionToTool(functionDef);
1108
- }).pipe(Effect7.provideService(DatabaseService3, dbService))
1109
- };
1110
- }));
1111
- };
1112
- var makeToolExecutionServiceFromFunctions = (toolkit, handlersLayer) => {
1113
- return Layer2.effect(ToolExecutionService2, Effect7.gen(function* () {
1114
- const toolkitHandler = yield* toolkit.pipe(Effect7.provide(handlersLayer));
1115
- invariant2(isHandlerLike(toolkitHandler), void 0, {
1116
- F: __dxlog_file6,
1117
- L: 74,
1118
- S: this,
1119
- A: [
1120
- "isHandlerLike(toolkitHandler)",
1121
- ""
1122
- ]
1123
- });
1124
- const functionInvocationService = yield* FunctionInvocationService;
1125
- return {
1126
- handlersFor: (toolkit2) => {
1127
- const makeHandler = (tool) => {
1128
- return Effect7.fn(`toolFunctionHandler ${tool.name}`)(function* (input) {
1129
- if (toolkitHandler.tools[tool.name]) {
1130
- if (Tool.isProviderDefined(tool)) {
1131
- throw new Error("Attempted to call a provider-defined tool");
1132
- }
1133
- const { result } = yield* toolkitHandler.handle(tool.name, input);
1134
- return result;
1135
- }
1136
- const { definition: functionDef } = Context3.get(FunctionToolAnnotation)(tool.annotations);
1137
- return yield* functionInvocationService.invokeFunction(functionDef, input).pipe(Effect7.catchAllDefect((defect) => Effect7.fail(defect)));
1138
- });
1139
- };
1140
- return toolkit2.of(Record.map(toolkit2.tools, (tool, _name) => Tool.isUserDefined(tool) ? makeHandler(tool) : null));
1141
- }
1142
- };
1143
- }));
1144
- };
1145
- var FunctionToolAnnotation = class extends Context3.Tag("@dxos/assistant/FunctionToolAnnotation")() {
1146
- };
1147
- var toolCache = /* @__PURE__ */ new WeakMap();
1148
- var projectFunctionToTool = (fn2) => {
1149
- if (toolCache.has(fn2)) {
1150
- return toolCache.get(fn2);
1151
- }
1152
- const tool = Tool.make(makeToolName(fn2.name), {
1153
- description: fn2.description,
1154
- parameters: createStructFieldsFromSchema(fn2.inputSchema),
1155
- // TODO(dmaretskyi): Include output schema.
1156
- failure: Schema3.Any
1157
- }).annotate(FunctionToolAnnotation, {
1158
- definition: fn2
1159
- });
1160
- toolCache.set(fn2, tool);
1161
- return tool;
1162
- };
1163
- var makeToolName = (name) => {
1164
- const toolName = name.toLowerCase().replace(/[^a-zA-Z0-9]/g, "-");
1165
- invariant2(toolName.match(/^[a-z_][a-z0-9-_]*$/), void 0, {
1166
- F: __dxlog_file6,
1167
- L: 144,
1168
- S: void 0,
1169
- A: [
1170
- "toolName.match(/^[a-z_][a-z0-9-_]*$/)",
1171
- ""
1172
- ]
1173
- });
1174
- return toolName;
1175
- };
1176
- var createStructFieldsFromSchema = (schema) => {
1177
- switch (schema.ast._tag) {
1178
- case "TypeLiteral":
1179
- return Object.fromEntries(schema.ast.propertySignatures.map((prop) => [
1180
- prop.name,
1181
- Schema3.make(prop.type)
1182
- ]));
1183
- case "VoidKeyword":
1184
- return {};
1185
- default:
1186
- return todo2(`Unsupported schema AST: ${schema.ast._tag}`);
1187
- }
1188
- };
1189
- var isHandlerLike = (value) => {
1190
- return typeof value.tools === "object" && typeof value.handle === "function";
1191
- };
1192
-
1193
1275
  // src/templates/system.ts
1194
1276
  import defaultsDeep from "lodash.defaultsdeep";
1195
1277
  import { Template as Template2 } from "@dxos/blueprints";
1196
1278
 
1197
1279
  // raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/instructions/format.tpl?raw
1198
- var format_default = "{{! System Core }}\n\n## Input Formats\n\n### Blueprints\n\nYou may be provided with Blueprint definitions that include specific instructions and tools for a given set of tasks.\nBlue print instructions will be enclosed in a <blueprint> tag.\n\n### Context objects\n\nYou may be provided with references to objects that are relevant to the current prompt.\nObjects 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.\nThe <typename> element is the type of the object.\n\n<object>\n <dxn>object-id</dxn>\n <typename>example.com/type/TypeName</typename>\n</object>\n\n## Response Formats\n\nIt is very important to respond in the correct format.\nYou response must be valid Markdown.\nIn some cases you should respond with well-formed XML tags along with the content.\nCode snippets should use 2-space indents.\n\n{{#if cot}}\nBefore responding, explain your reasoning and include your detailed chain-of-thought enclosed in a <cot> tag.\nThe <cot> tag should be the first thing in your response.\n{{/if}}\n\n## Block references\n\nYou can reply with a block reference for objects if you have the DXN. For example:\n\n<object>\n <dxn>dxn:queue:data:B6INSIBY3CBEF4M5VZRYBCMAHQMPYK5AJ:01K24XMVHSZHS97SG1VTVQDM5Z:01K24XPK464FSCKVQJAB2H662M</dxn>\n</object>\n<object>\n <dxn>dxn:echo:@:01K24XPK464FSCKVQJAB2H662M</dxn>\n</object>\n\n### Suggestions\n\nEach suggestion must in the form of a proposed action enclosed in a <suggestion> tag.\nExample: <suggestion>Show on a map</suggestion>\nDo not use bullets when listing suggestions.\n\n### Multiple choice questions\n\nYou may ask multiple choice questions by wrapping consise options inside <option> tags inside a well formed <select> tag.\nExample: <select><option>Yes</option><option>No</option></select>\n\n## Toolkit\n\nIf the user asks for a list of tools, then just emit a single self-closing <toolkit/> tag instead of listing the tools.\n\n## Context\n\nThe current date and time is {{DATETIME}}.\n";
1280
+ var format_default = "{{! System Core }}\n\n## Input Formats\n\n### Blueprints\n\nYou may be provided with Blueprint definitions that include specific instructions and tools for a given set of tasks.\nBlue print instructions will be enclosed in a <blueprint> tag.\n\n### Context objects\n\nYou may be provided with references to objects that are relevant to the current prompt.\nObjects 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.\nThe <typename> element is the type of the object.\n\n<object>\n <dxn>object-id</dxn>\n <typename>example.com/type/TypeName</typename>\n</object>\n\n## Response Formats\n\nIt is very important to respond in the correct format.\nYou response must be valid Markdown.\nIn some cases you should respond with well-formed XML tags along with the content.\nCode snippets should use 2-space indents.\n\n{{#if cot}}\nBefore responding, explain your reasoning and include your detailed chain-of-thought enclosed in a <cot> tag.\nThe <cot> tag should be the first thing in your response.\n{{/if}}\n\n## Block references\n\nYou can reply with a block reference for objects if you have the DXN. For example:\n\n<object>\n <dxn>dxn:queue:data:B6INSIBY3CBEF4M5VZRYBCMAHQMPYK5AJ:01K24XMVHSZHS97SG1VTVQDM5Z:01K24XPK464FSCKVQJAB2H662M</dxn>\n</object>\n<object>\n <dxn>dxn:echo:@:01K24XPK464FSCKVQJAB2H662M</dxn>\n</object>\n\n### Suggestions\n\nEach suggestion must in the form of a proposed action enclosed in a <suggestion> tag.\nExample: <suggestion>Show on a map</suggestion>\nDo not use bullets when listing suggestions.\n\n### Multiple choice questions\n\nYou may ask multiple choice questions by wrapping consise options inside <option> tags inside a well formed <select> tag.\nExample: <select><option>Yes</option><option>No</option></select>\n\n## Context\n\nThe current date and time is {{DATETIME}}.\n";
1199
1281
 
1200
1282
  // raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/instructions/system.tpl?raw
1201
1283
  var system_default = "{{! System Prompt }}\n\n- You are a helpful assistant called Kai. \n- Your name is Kai and you are powered by different language models.\n- You were created by DXOS and operate inside of Composer and on the DXOS EDGE network.\n- In your initial greeting state your name and make some suggestions based on the current context objects.\n- You are an advanced AI assistant capable of creating and managing artifacts from provided data and tools.\n- Your task is to process user commands and questions and decide how best to respond.\n- In general be concise and direct.\n- Follow all instructions carefully.\n\n## Planning\n\n- Analyze the structure and type of the content in the user's message.\n- Determine if you complete the task using the available blueprint definitions?\n- If you can't complete the task using the available blueprint definitions, query the list of available blueprint definitions using the appropriate tool.\n- Identify which blueprint definitions are relevant to the user's request.\n- Evaluate the potential benefits of creating an artifact vs. normal processing for each identified element.\n- Make a final decision on whether to create an artifact and explain your reasoning.\n- Are the required blueprint definitions already available?\n- If not, select which blueprint definition(s) will be the most relevant and require them using the require_artifact_definitions tool.\n- The require'd artifact tools will be available for use after require.\n- If creating an artifact, outline how you will structure it within the response.\n- Decide if the artifact needs to be shown to the user.\n- Call the show tool to show the artifact to the user.\n- 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.\n\n## Blueprints and Artifacts\n\n- Determine if the interaction involves an artifact. Prefer artifacts for tables, lists, spreadsheets, kanbans, games, images, and other structured data.\n- Determine if the user is explicitly talking about creating a new artifact, or wants to use an existing artifact.\n- If it is ambiguous, query for existing artifacts first and then decide.\n- If you decide to create an artifact, call the associated tool to create the artifact.\n- Artifacts are stored in the database. Tools are used to create and query artifacts.\n- If you are unsure about creating an artifact ask the user for clarification.\n- Artifacts are mutable objects that can change over the course of the conversation.\n- Always re-query the artifact using the tool (like query or inspect) to get the latest state of the artifact before answering the user.\n- You must never generate the id of the artifact; only recall the ids that are already in the history.\n- Artifacts are created by requiring the specific artifact using the require_artifact tool and creating it by calling the associated tool.\n- You can add suggested actions at the end of your response.\n- 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.\n- Suggested actions must be in the form of a user instruction that you can follow.\n- Suggested actions could include actions that create artifacts.\n- After creating an artifact use a tool to add it to the chat context.\n\n## Context\n\nToday is {{DATETIME}}.\n";
@@ -1214,58 +1296,12 @@ var templates = {
1214
1296
  source: system_default
1215
1297
  })
1216
1298
  };
1217
-
1218
- // src/types/artifact.ts
1219
- import * as Schema4 from "effect/Schema";
1220
- import { DatabaseService as DatabaseService4 } from "@dxos/echo-db";
1221
- import { DXN as DXN2, LOCAL_SPACE_TAG } from "@dxos/keys";
1222
- import { trim as trim2 } from "@dxos/util";
1223
- var createArtifactElement = (id) => `<artifact id=${id} />`;
1224
- var ArtifactId = class extends Schema4.String.annotations({
1225
- // TODO(dmaretskyi): This section gets overriden.
1226
- description: trim2`
1227
- The ID of the referenced object. Formats accepted:
1228
- - DXN (dxn:echo:@:XXXXX). DXNs can be prepended with an @ symbol for compatibility with in-text references.
1229
- - space ID, object ID tuple (spaceID:objectID)
1230
- - Only object ID that is assumed to be in the current space (XXXXX)
1231
- `,
1232
- examples: [
1233
- "dxn:echo:@:XXXXX",
1234
- "@dxn:echo:@:XXXXX",
1235
- "spaceID:objectID",
1236
- "XXXXX"
1237
- ]
1238
- }) {
1239
- static toDXN(reference, owningSpaceId) {
1240
- if (reference.startsWith("@dxn:")) {
1241
- return DXN2.parse(reference.slice(1));
1242
- } else if (reference.startsWith("dxn:")) {
1243
- return DXN2.parse(reference);
1244
- } else if (/^[A-Z0-9]+:[A-Z0-9]+$/.test(reference)) {
1245
- const [spaceId, objectId] = reference.split(":");
1246
- return new DXN2(DXN2.kind.ECHO, [
1247
- spaceId === owningSpaceId ? LOCAL_SPACE_TAG : spaceId,
1248
- objectId
1249
- ]);
1250
- } else if (/^[A-Z0-9]+$/.test(reference)) {
1251
- return DXN2.fromLocalObjectId(reference);
1252
- } else {
1253
- throw new Error(`Unable to parse object reference: ${reference}`);
1254
- }
1255
- }
1256
- /**
1257
- * Resolves an artifact ID to an object.
1258
- */
1259
- static resolve(schema, ref) {
1260
- const dxn = ArtifactId.toDXN(ref);
1261
- return DatabaseService4.resolve(dxn, schema);
1262
- }
1263
- };
1264
1299
  export {
1265
1300
  AiAssistantError,
1266
1301
  AiContextBinder,
1267
1302
  AiContextService,
1268
1303
  AiConversation,
1304
+ AiConversationService,
1269
1305
  AiModelError,
1270
1306
  AiSession,
1271
1307
  ArtifactDiffResolver,
@@ -1274,13 +1310,16 @@ export {
1274
1310
  ContextBinding,
1275
1311
  ExecutionGraph,
1276
1312
  GenerationObserver,
1277
- GenericToolkit_exports as GenericToolkit,
1313
+ RefFromLLM,
1314
+ ToolExecutionServices,
1278
1315
  VersionPin,
1279
1316
  applyDiffs,
1280
1317
  computeDiffsWithCursors,
1281
1318
  createArtifactElement,
1282
1319
  createSystemPrompt,
1283
1320
  createToolkit,
1321
+ formatSystemPrompt,
1322
+ formatUserPrompt,
1284
1323
  makeToolExecutionServiceFromFunctions,
1285
1324
  makeToolResolverFromFunctions,
1286
1325
  reduceDiffs,