@dxos/assistant 0.8.4-main.ae835ea → 0.8.4-main.bc674ce

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 (116) hide show
  1. package/dist/lib/browser/{chunk-4VIJTKPM.mjs → chunk-7CDDG6LL.mjs} +252 -28
  2. package/dist/lib/browser/chunk-7CDDG6LL.mjs.map +7 -0
  3. package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
  4. package/dist/lib/browser/extraction/index.mjs +119 -166
  5. package/dist/lib/browser/extraction/index.mjs.map +4 -4
  6. package/dist/lib/browser/index.mjs +239 -289
  7. package/dist/lib/browser/index.mjs.map +4 -4
  8. package/dist/lib/browser/meta.json +1 -1
  9. package/dist/lib/browser/testing/index.mjs +32 -0
  10. package/dist/lib/browser/testing/index.mjs.map +7 -0
  11. package/dist/lib/node-esm/{chunk-AUUC7II4.mjs → chunk-CC7ER6Q3.mjs} +251 -28
  12. package/dist/lib/node-esm/chunk-CC7ER6Q3.mjs.map +7 -0
  13. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
  14. package/dist/lib/node-esm/extraction/index.mjs +119 -166
  15. package/dist/lib/node-esm/extraction/index.mjs.map +4 -4
  16. package/dist/lib/node-esm/index.mjs +239 -289
  17. package/dist/lib/node-esm/index.mjs.map +4 -4
  18. package/dist/lib/node-esm/meta.json +1 -1
  19. package/dist/lib/node-esm/testing/index.mjs +33 -0
  20. package/dist/lib/node-esm/testing/index.mjs.map +7 -0
  21. package/dist/types/src/conversation/context.d.ts +88 -62
  22. package/dist/types/src/conversation/context.d.ts.map +1 -1
  23. package/dist/types/src/conversation/context.test.d.ts +2 -0
  24. package/dist/types/src/conversation/context.test.d.ts.map +1 -0
  25. package/dist/types/src/conversation/conversation.d.ts +16 -16
  26. package/dist/types/src/conversation/conversation.d.ts.map +1 -1
  27. package/dist/types/src/conversation/conversation.test.d.ts +2 -0
  28. package/dist/types/src/conversation/conversation.test.d.ts.map +1 -0
  29. package/dist/types/src/errors.d.ts +22 -30
  30. package/dist/types/src/errors.d.ts.map +1 -1
  31. package/dist/types/src/execution-graph.d.ts +5 -2
  32. package/dist/types/src/execution-graph.d.ts.map +1 -1
  33. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -1
  34. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +254 -252
  35. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
  36. package/dist/types/src/extraction/extraction.d.ts +344 -309
  37. package/dist/types/src/extraction/extraction.d.ts.map +1 -1
  38. package/dist/types/src/extraction/quotes.d.ts +2 -2
  39. package/dist/types/src/extraction/quotes.d.ts.map +1 -1
  40. package/dist/types/src/functions/services.d.ts +4 -3
  41. package/dist/types/src/functions/services.d.ts.map +1 -1
  42. package/dist/types/src/functions.test.d.ts +2 -0
  43. package/dist/types/src/functions.test.d.ts.map +1 -0
  44. package/dist/types/src/session/GenericToolkit.d.ts +85 -0
  45. package/dist/types/src/session/GenericToolkit.d.ts.map +1 -0
  46. package/dist/types/src/session/artifact-diff.d.ts +1 -1
  47. package/dist/types/src/session/artifact-diff.d.ts.map +1 -1
  48. package/dist/types/src/session/format.d.ts +4 -178
  49. package/dist/types/src/session/format.d.ts.map +1 -1
  50. package/dist/types/src/session/index.d.ts +1 -0
  51. package/dist/types/src/session/index.d.ts.map +1 -1
  52. package/dist/types/src/session/observer.d.ts +6 -5
  53. package/dist/types/src/session/observer.d.ts.map +1 -1
  54. package/dist/types/src/session/session.d.ts +16 -11
  55. package/dist/types/src/session/session.d.ts.map +1 -1
  56. package/dist/types/src/session/toolkit.d.ts +5 -6
  57. package/dist/types/src/session/toolkit.d.ts.map +1 -1
  58. package/dist/types/src/session/version-pin.d.ts +3 -3
  59. package/dist/types/src/session/version-pin.d.ts.map +1 -1
  60. package/dist/types/src/testing/index.d.ts +2 -0
  61. package/dist/types/src/testing/index.d.ts.map +1 -0
  62. package/dist/types/src/testing/layer.d.ts +21 -0
  63. package/dist/types/src/testing/layer.d.ts.map +1 -0
  64. package/dist/types/src/types/artifact.d.ts +14 -2
  65. package/dist/types/src/types/artifact.d.ts.map +1 -1
  66. package/dist/types/tsconfig.tsbuildinfo +1 -1
  67. package/package.json +38 -27
  68. package/src/conversation/context.test.ts +46 -0
  69. package/src/conversation/context.ts +183 -66
  70. package/src/conversation/conversation.test.ts +65 -0
  71. package/src/conversation/conversation.ts +51 -54
  72. package/src/diff.test.ts +3 -3
  73. package/src/errors.ts +2 -2
  74. package/src/execution-graph.test.ts +45 -43
  75. package/src/execution-graph.ts +23 -14
  76. package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +2 -2
  77. package/src/extraction/extraction-llm-function/extraction-llm-function.ts +44 -54
  78. package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +14 -13
  79. package/src/extraction/extraction-ner-function/extraction-ner-function.ts +36 -44
  80. package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +10 -5
  81. package/src/extraction/extraction.ts +9 -8
  82. package/src/extraction/quotes.ts +4 -5
  83. package/src/functions/services.ts +41 -19
  84. package/src/functions.conversations.json +1 -0
  85. package/src/functions.test.ts +74 -0
  86. package/src/session/GenericToolkit.ts +131 -0
  87. package/src/session/artifact-diff.ts +1 -1
  88. package/src/session/format.test.ts +103 -39
  89. package/src/session/format.ts +17 -16
  90. package/src/session/index.ts +2 -0
  91. package/src/session/observer.ts +5 -3
  92. package/src/session/session.conversations.json +1 -1
  93. package/src/session/session.test.ts +7 -13
  94. package/src/session/session.ts +40 -19
  95. package/src/session/toolkit.ts +13 -22
  96. package/src/session/version-pin.ts +6 -6
  97. package/src/templates/{format.tpl → instructions/format.tpl} +1 -0
  98. package/src/templates/system.ts +2 -2
  99. package/src/testing/index.ts +5 -0
  100. package/src/testing/layer.ts +78 -0
  101. package/src/types/artifact.ts +33 -3
  102. package/dist/lib/browser/chunk-4VIJTKPM.mjs.map +0 -7
  103. package/dist/lib/browser/chunk-UB26N5UF.mjs +0 -89
  104. package/dist/lib/browser/chunk-UB26N5UF.mjs.map +0 -7
  105. package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs +0 -13
  106. package/dist/lib/node-esm/chunk-AUUC7II4.mjs.map +0 -7
  107. package/dist/lib/node-esm/chunk-BCFXTNSK.mjs +0 -91
  108. package/dist/lib/node-esm/chunk-BCFXTNSK.mjs.map +0 -7
  109. package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs +0 -14
  110. package/dist/types/src/summarization/summarization.test.d.ts +0 -2
  111. package/dist/types/src/summarization/summarization.test.d.ts.map +0 -1
  112. package/src/summarization/summarization.test.ts +0 -202
  113. /package/dist/lib/browser/{named-entity-recognition-QPYHML66.mjs.map → chunk-J5LGTIGS.mjs.map} +0 -0
  114. /package/dist/lib/node-esm/{named-entity-recognition-2HDF46YX.mjs.map → chunk-HSLMI22Q.mjs.map} +0 -0
  115. /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
  116. /package/src/templates/{system.tpl → instructions/system.tpl} +0 -0
@@ -3,34 +3,40 @@ import {
3
3
  AiModelError,
4
4
  AiSession,
5
5
  ArtifactDiffResolver,
6
+ ArtifactId,
6
7
  GenerationObserver,
8
+ GenericToolkit_exports,
9
+ RefFromLLM,
7
10
  VersionPin,
8
- createToolkit
9
- } from "./chunk-4VIJTKPM.mjs";
11
+ createArtifactElement,
12
+ createToolkit,
13
+ makeToolExecutionServiceFromFunctions,
14
+ makeToolResolverFromFunctions
15
+ } from "./chunk-7CDDG6LL.mjs";
16
+ import "./chunk-J5LGTIGS.mjs";
10
17
 
11
18
  // src/conversation/context.ts
12
- import { computed } from "@preact/signals-core";
13
- import * as Array2 from "effect/Array";
19
+ import { Atom, Registry } from "@effect-atom/atom-react";
20
+ import * as EArray from "effect/Array";
14
21
  import * as Context from "effect/Context";
15
22
  import * as Function from "effect/Function";
16
23
  import * as Schema from "effect/Schema";
17
24
  import { Blueprint } from "@dxos/blueprints";
18
25
  import { Resource } from "@dxos/context";
19
- import { DXN, Filter, Obj, Query, Type } from "@dxos/echo";
20
- import { invariant } from "@dxos/invariant";
21
- import { ComplexSet } from "@dxos/util";
26
+ import { DXN, Obj, Query, Type } from "@dxos/echo";
27
+ import { log } from "@dxos/log";
28
+ import { ComplexSet, isTruthy } from "@dxos/util";
22
29
  var __dxlog_file = "/__w/dxos/dxos/packages/core/assistant/src/conversation/context.ts";
23
30
  var ContextBinding = Schema.Struct({
24
31
  blueprints: Schema.Struct({
25
32
  added: Schema.Array(Type.Ref(Blueprint.Blueprint)),
26
33
  removed: Schema.Array(Type.Ref(Blueprint.Blueprint))
27
34
  }),
28
- // TODO(burdon): Type.Expando => Type.Obj (or Obj.Any?)
29
35
  objects: Schema.Struct({
30
- added: Schema.Array(Type.Ref(Type.Expando)),
31
- removed: Schema.Array(Type.Ref(Type.Expando))
36
+ added: Schema.Array(Type.Ref(Type.Obj)),
37
+ removed: Schema.Array(Type.Ref(Type.Obj))
32
38
  })
33
- }).pipe(Type.Obj({
39
+ }).pipe(Type.object({
34
40
  typename: "dxos.org/type/ContextBinding",
35
41
  version: "0.1.0"
36
42
  }));
@@ -40,238 +46,322 @@ var Bindings = class {
40
46
  objects = new ComplexSet((ref) => ref.dxn.asEchoDXN()?.echoId);
41
47
  toJSON() {
42
48
  return {
43
- blueprints: Array2.fromIterable(this.blueprints).map((ref) => ref.dxn.toString()),
44
- objects: Array2.fromIterable(this.objects).map((ref) => ref.dxn.toString())
49
+ blueprints: EArray.fromIterable(this.blueprints).map((ref) => ref.dxn.toString()),
50
+ objects: EArray.fromIterable(this.objects).map((ref) => ref.dxn.toString())
45
51
  };
46
52
  }
47
53
  };
48
54
  var AiContextBinder = class extends Resource {
55
+ _blueprints = Atom.make([]).pipe(Atom.keepAlive);
56
+ _objects = Atom.make([]).pipe(Atom.keepAlive);
57
+ _registry;
49
58
  _queue;
59
+ constructor(options) {
60
+ super();
61
+ this._queue = options.queue;
62
+ this._registry = options.registry ?? Registry.make();
63
+ }
50
64
  /**
51
- * Reactive query of all bindings.
65
+ * Returns the blueprints atom for subscription.
52
66
  */
53
- // TODO(burdon): Cache value?
54
- _bindings;
55
- _blueprints;
56
- _objects;
57
- constructor(_queue) {
58
- super(), this._queue = _queue;
59
- }
60
- get bindings() {
61
- invariant(this._bindings, "AiContextBinder not open", {
62
- F: __dxlog_file,
63
- L: 78,
64
- S: this,
65
- A: [
66
- "this._bindings",
67
- "'AiContextBinder not open'"
68
- ]
69
- });
70
- return this._bindings;
71
- }
72
67
  get blueprints() {
73
- invariant(this._blueprints, "AiContextBinder not open", {
74
- F: __dxlog_file,
75
- L: 83,
76
- S: this,
77
- A: [
78
- "this._blueprints",
79
- "'AiContextBinder not open'"
80
- ]
81
- });
82
68
  return this._blueprints;
83
69
  }
70
+ /**
71
+ * Returns the objects atom for subscription.
72
+ */
84
73
  get objects() {
85
- invariant(this._objects, "AiContextBinder not open", {
86
- F: __dxlog_file,
87
- L: 88,
88
- S: this,
89
- A: [
90
- "this._objects",
91
- "'AiContextBinder not open'"
92
- ]
93
- });
94
74
  return this._objects;
95
75
  }
96
- // TODO(wittjosiah): Use parent context?
97
- async _open() {
98
- const query = this._queue.query(Query.select(Filter.everything()));
99
- this._ctx.onDispose(query.subscribe(() => {
100
- }));
101
- this._bindings = computed(() => this._reduce(query.objects));
102
- this._blueprints = computed(() => [
103
- ...this.bindings.value.blueprints
104
- ]);
105
- this._objects = computed(() => [
106
- ...this.bindings.value.objects
107
- ]);
76
+ /**
77
+ * Gets the current blueprints value.
78
+ */
79
+ getBlueprints() {
80
+ return this._registry.get(this._blueprints);
108
81
  }
109
- async _close() {
110
- this._bindings = void 0;
111
- this._blueprints = void 0;
112
- this._objects = void 0;
82
+ /**
83
+ * Gets the current objects value.
84
+ */
85
+ getObjects() {
86
+ return this._registry.get(this._objects);
87
+ }
88
+ /**
89
+ * Subscribe to changes in blueprints.
90
+ */
91
+ subscribeBlueprints(cb) {
92
+ return this._registry.subscribe(this._blueprints, () => cb(this._registry.get(this._blueprints)));
113
93
  }
114
94
  /**
115
- * Asynchronous query of all bindings.
95
+ * Subscribe to changes in objects.
116
96
  */
117
- async query() {
118
- const { objects } = await this._queue.query(Query.select(Filter.everything())).run();
119
- return this._reduce(objects);
120
- }
121
- // TODO(burdon): Pass in Blueprint obj (from registry?) and create reference.
122
- async bind(props) {
123
- const blueprints = props.blueprints?.filter((ref) => !this.blueprints.peek().find((b) => b.dxn.toString() === ref.dxn.toString())) ?? [];
124
- const objects = props.objects?.filter((ref) => !this.objects.peek().find((o) => o.dxn.toString() === ref.dxn.toString())) ?? [];
125
- if (!blueprints.length && !objects.length) {
97
+ subscribeObjects(cb) {
98
+ return this._registry.subscribe(this._objects, () => cb(this._registry.get(this._objects)));
99
+ }
100
+ async _open() {
101
+ const query = this._queue.query(Query.type(ContextBinding));
102
+ const initialResults = await query.run();
103
+ await this._updateBindings(initialResults);
104
+ this._ctx.onDispose(query.subscribe(async () => {
105
+ await this._updateBindings(query.results);
106
+ }));
107
+ }
108
+ async _updateBindings(items) {
109
+ if (items.length === 0) {
126
110
  return;
127
111
  }
112
+ const bindings = this._reduce(items);
113
+ const currentBlueprints = this._registry.get(this._blueprints);
114
+ const currentObjects = this._registry.get(this._objects);
115
+ const newBlueprints = await this._resolve(bindings.blueprints, currentBlueprints);
116
+ const newObjects = await this._resolve(bindings.objects, currentObjects);
117
+ this._registry.set(this._blueprints, newBlueprints);
118
+ this._registry.set(this._objects, newObjects);
119
+ log("updated bindings", {
120
+ blueprints: newBlueprints.length,
121
+ objects: newObjects.length
122
+ }, {
123
+ F: __dxlog_file,
124
+ L: 156,
125
+ S: this,
126
+ C: (f, a) => f(...a)
127
+ });
128
+ }
129
+ async _close() {
130
+ this._registry.set(this._blueprints, []);
131
+ this._registry.set(this._objects, []);
132
+ }
133
+ async bind({ blueprints, objects }) {
134
+ const currentBlueprints = this._registry.get(this._blueprints);
135
+ const currentObjects = this._registry.get(this._objects);
136
+ const { added: addedBlueprints, next: nextBlueprints } = this._processBindings(blueprints, currentBlueprints);
137
+ const { added: addedObjects, next: nextObjects } = this._processBindings(objects, currentObjects);
138
+ if (!addedBlueprints.length && !addedObjects.length) {
139
+ return;
140
+ }
141
+ this._registry.set(this._blueprints, nextBlueprints);
142
+ this._registry.set(this._objects, nextObjects);
143
+ log("bind", {
144
+ blueprints: addedBlueprints.length,
145
+ objects: addedObjects.length
146
+ }, {
147
+ F: __dxlog_file,
148
+ L: 182,
149
+ S: this,
150
+ C: (f, a) => f(...a)
151
+ });
128
152
  await this._queue.append([
129
153
  Obj.make(ContextBinding, {
130
154
  blueprints: {
131
- added: blueprints,
155
+ added: addedBlueprints,
132
156
  removed: []
133
157
  },
134
158
  objects: {
135
- added: objects,
159
+ added: addedObjects,
136
160
  removed: []
137
161
  }
138
162
  })
139
163
  ]);
140
164
  }
141
- async unbind(props) {
142
- if (!props.blueprints?.length && !props.objects?.length) {
165
+ async unbind({ blueprints, objects }) {
166
+ if (!blueprints?.length && !objects?.length) {
143
167
  return;
144
168
  }
169
+ log("unbind", {
170
+ blueprints: blueprints?.length,
171
+ objects: objects?.length
172
+ }, {
173
+ F: __dxlog_file,
174
+ L: 202,
175
+ S: this,
176
+ C: (f, a) => f(...a)
177
+ });
145
178
  await this._queue.append([
146
179
  Obj.make(ContextBinding, {
147
180
  blueprints: {
148
181
  added: [],
149
- removed: props.blueprints ?? []
182
+ removed: blueprints ?? []
150
183
  },
151
184
  objects: {
152
185
  added: [],
153
- removed: props.objects ?? []
186
+ removed: objects ?? []
154
187
  }
155
188
  })
156
189
  ]);
157
190
  }
191
+ /**
192
+ * Process bindings to filter duplicates and determine next state.
193
+ */
194
+ _processBindings(refs, current) {
195
+ const next = [
196
+ ...current
197
+ ];
198
+ const added = [];
199
+ if (!refs?.length) {
200
+ return {
201
+ added,
202
+ next
203
+ };
204
+ }
205
+ const seen = new Set(current.map((obj) => Obj.getDXN(obj).toString()));
206
+ for (const ref of refs) {
207
+ const dxn = ref.dxn.toString();
208
+ if (!seen.has(dxn)) {
209
+ seen.add(dxn);
210
+ added.push(ref);
211
+ if (ref.isAvailable) {
212
+ const target = ref.target;
213
+ if (target) {
214
+ next.push(target);
215
+ }
216
+ }
217
+ }
218
+ }
219
+ return {
220
+ added,
221
+ next
222
+ };
223
+ }
224
+ /**
225
+ * Reduce results into sets of blueprints and objects.
226
+ */
158
227
  _reduce(items) {
159
- return Function.pipe(items, Array2.filter(Obj.instanceOf(ContextBinding)), Array2.reduce(new Bindings(), (context, item) => {
160
- item.blueprints.removed.forEach((ref) => context.blueprints.delete(ref));
161
- item.blueprints.added.forEach((ref) => context.blueprints.add(ref));
162
- item.objects.removed.forEach((ref) => {
228
+ return Function.pipe(items, EArray.reduce(new Bindings(), (context, { blueprints, objects }) => {
229
+ blueprints.added.forEach((ref) => context.blueprints.add(ref));
230
+ blueprints.removed.forEach((ref) => context.blueprints.delete(ref));
231
+ objects.added.forEach((ref) => context.objects.add(ref));
232
+ objects.removed.forEach((ref) => {
163
233
  for (const obj of context.objects) {
164
234
  if (DXN.equalsEchoId(obj.dxn, ref.dxn)) {
165
235
  context.objects.delete(obj);
166
236
  }
167
237
  }
168
238
  });
169
- item.objects.added.forEach((ref) => context.objects.add(ref));
170
239
  return context;
171
240
  }));
172
241
  }
242
+ /**
243
+ * Resolve references to objects, loading them first if needed and falling back to existing objects.
244
+ */
245
+ async _resolve(refs, current) {
246
+ const refArray = [
247
+ ...refs
248
+ ];
249
+ await Promise.all(refArray.map((ref) => ref.load()));
250
+ return refArray.map((ref) => {
251
+ let target;
252
+ if (ref.isAvailable) {
253
+ target = ref.target;
254
+ }
255
+ return target ?? current.find((obj) => Obj.getDXN(obj).toString() === ref.dxn.toString());
256
+ }).filter(isTruthy);
257
+ }
173
258
  };
174
259
  var AiContextService = class extends Context.Tag("@dxos/assistant/AiContextService")() {
175
260
  };
176
261
 
177
262
  // src/conversation/conversation.ts
178
- import * as Array4 from "effect/Array";
179
263
  import * as Effect from "effect/Effect";
180
- import * as Option from "effect/Option";
181
264
  import { Resource as Resource2 } from "@dxos/context";
182
265
  import { Obj as Obj2 } from "@dxos/echo";
183
- import { DatabaseService } from "@dxos/functions";
184
- import { log } from "@dxos/log";
185
- import { DataType } from "@dxos/schema";
266
+ import { invariant } from "@dxos/invariant";
267
+ import { log as log2 } from "@dxos/log";
268
+ import { Message } from "@dxos/types";
186
269
  var __dxlog_file2 = "/__w/dxos/dxos/packages/core/assistant/src/conversation/conversation.ts";
187
270
  var AiConversation = class extends Resource2 {
188
271
  /**
189
- * Message and binding queue.
272
+ * Blueprints and objects bound to the conversation.
190
273
  */
274
+ _binder;
191
275
  _queue;
192
- /**
193
- * Blueprints bound to the conversation.
194
- */
195
- _context;
196
- /**
197
- * Toolkit from the current session request.
198
- */
199
276
  _toolkit;
200
- constructor(queue) {
277
+ constructor(options) {
201
278
  super();
202
- this._queue = queue;
203
- this._context = new AiContextBinder(this._queue);
279
+ this._queue = options.queue;
280
+ this._toolkit = options.toolkit;
281
+ invariant(this._queue, void 0, {
282
+ F: __dxlog_file2,
283
+ L: 54,
284
+ S: this,
285
+ A: [
286
+ "this._queue",
287
+ ""
288
+ ]
289
+ });
290
+ this._binder = new AiContextBinder({
291
+ queue: this._queue,
292
+ registry: options.registry
293
+ });
204
294
  }
205
295
  async _open() {
206
- await this._context.open();
296
+ await this._binder.open(this._ctx);
207
297
  }
208
- async _close() {
209
- await this._context.close();
298
+ get queue() {
299
+ return this._queue;
210
300
  }
211
301
  get context() {
212
- return this._context;
302
+ return this._binder;
213
303
  }
214
304
  get toolkit() {
215
305
  return this._toolkit;
216
306
  }
217
307
  async getHistory() {
218
308
  const queueItems = await this._queue.queryObjects();
219
- return queueItems.filter(Obj2.instanceOf(DataType.Message));
309
+ return queueItems.filter(Obj2.instanceOf(Message.Message));
220
310
  }
221
311
  /**
222
312
  * Creates a new cancelable request effect.
223
313
  */
224
314
  createRequest(params) {
225
- return Effect.gen(this, function* () {
226
- const session = new AiSession();
227
- const history = yield* Effect.promise(() => this.getHistory());
228
- const context = yield* Effect.promise(() => this.context.query());
229
- const blueprints = yield* Effect.forEach(context.blueprints.values(), DatabaseService.loadOption).pipe(Effect.map(Array4.filter(Option.isSome)), Effect.map(Array4.map((option) => option.value)));
230
- const objects = yield* Effect.forEach(context.objects.values(), DatabaseService.loadOption).pipe(Effect.map(Array4.filter(Option.isSome)), Effect.map(Array4.map((option) => option.value)));
315
+ const self = this;
316
+ return Effect.gen(function* () {
317
+ const history = yield* Effect.promise(() => self.getHistory());
318
+ const blueprints = self.context.getBlueprints();
231
319
  const toolkit = yield* createToolkit({
320
+ toolkit: self._toolkit,
232
321
  blueprints
233
322
  });
234
- this._toolkit = toolkit;
235
- const start = Date.now();
236
- log("run", {
323
+ const objects = self.context.getObjects();
324
+ log2("run", {
237
325
  history: history.length,
238
326
  blueprints: blueprints.length,
239
- objects: objects.length,
240
- tools: this._toolkit?.tools.length ?? 0
327
+ tools: Object.keys(toolkit.tools).length,
328
+ objects: objects.length
241
329
  }, {
242
330
  F: __dxlog_file2,
243
- L: 107,
331
+ L: 99,
244
332
  S: this,
245
333
  C: (f, a) => f(...a)
246
334
  });
335
+ const session = new AiSession();
247
336
  const messages = yield* session.run({
248
337
  history,
249
338
  blueprints,
250
- objects,
251
339
  toolkit,
340
+ objects,
252
341
  ...params
253
342
  }).pipe(Effect.provideService(AiContextService, {
254
- binder: this.context
343
+ binder: self.context
255
344
  }));
256
- log("result", {
345
+ log2("result", {
257
346
  messages: messages.length,
258
- duration: Date.now() - start
347
+ duration: session.duration,
348
+ toolCalls: session.toolCalls
259
349
  }, {
260
350
  F: __dxlog_file2,
261
- L: 121,
351
+ L: 114,
262
352
  S: this,
263
353
  C: (f, a) => f(...a)
264
354
  });
265
- yield* Effect.promise(() => this._queue.append(messages));
355
+ yield* Effect.promise(() => self._queue.append(messages));
266
356
  return messages;
267
- }).pipe(Effect.withSpan("AiConversation.request"));
357
+ });
268
358
  }
269
359
  };
270
360
 
271
361
  // src/diff.ts
272
362
  import { next as A } from "@automerge/automerge";
273
363
  import { DocAccessor, toCursorRange } from "@dxos/echo-db";
274
- import { log as log2 } from "@dxos/log";
364
+ import { log as log3 } from "@dxos/log";
275
365
  import { isNonNullable } from "@dxos/util";
276
366
  var __dxlog_file3 = "/__w/dxos/dxos/packages/core/assistant/src/diff.ts";
277
367
  var reduceDiffs = (diffs) => {
@@ -325,7 +415,7 @@ var applyDiffs = (accessor, diffs) => {
325
415
  if (idx !== -1) {
326
416
  A.splice(doc, accessor.path, idx, diff.match.length, diff.replace);
327
417
  } else {
328
- log2.warn("diff not found", diff, {
418
+ log3.warn("diff not found", diff, {
329
419
  F: __dxlog_file3,
330
420
  L: 78,
331
421
  S: void 0,
@@ -342,9 +432,13 @@ import { AgentStatus } from "@dxos/ai";
342
432
  import { Obj as Obj3 } from "@dxos/echo";
343
433
  import { MESSAGE_PROPERTY_TOOL_CALL_ID } from "@dxos/functions";
344
434
  import { LogLevel } from "@dxos/log";
345
- import { ContentBlock, DataType as DataType2 } from "@dxos/schema";
435
+ import { ContentBlock, Message as Message2 } from "@dxos/types";
346
436
  import { isNonNullable as isNonNullable2 } from "@dxos/util";
437
+ var SKIP_BLOCKS = [
438
+ "text"
439
+ ];
347
440
  var ExecutionGraph = class {
441
+ _skipBlocks;
348
442
  // TODO(dmaretskyi): Evolve the internal state to support chats, functions, circuit workflows, etc.
349
443
  _commits = [];
350
444
  _branchNames = /* @__PURE__ */ new Set();
@@ -353,13 +447,16 @@ var ExecutionGraph = class {
353
447
  _toolCallCommitIds = /* @__PURE__ */ new Map();
354
448
  _lastCommitByBranch = /* @__PURE__ */ new Map();
355
449
  _pendingToolResults = /* @__PURE__ */ new Map();
450
+ constructor(_skipBlocks = SKIP_BLOCKS) {
451
+ this._skipBlocks = _skipBlocks;
452
+ }
356
453
  /**
357
454
  * Adds events to the graph.
358
455
  */
359
456
  addEvents(events) {
360
457
  const sortedEvents = this.sortObjectsByCreated(events);
361
458
  for (const event of sortedEvents) {
362
- if (Obj3.instanceOf(DataType2.Message, event)) {
459
+ if (Obj3.instanceOf(Message2.Message, event)) {
363
460
  this._processMessage(event);
364
461
  } else if (Obj3.instanceOf(AgentStatus, event)) {
365
462
  this._processAgentStatus(event);
@@ -370,7 +467,7 @@ var ExecutionGraph = class {
370
467
  * Processes a message event and creates commits for its blocks.
371
468
  */
372
469
  _processMessage(message) {
373
- const messageCommits = messageToCommits(message, this._lastBlockId, this._toolCallCommitIds, this._lastCommitByBranch);
470
+ const messageCommits = messageToCommits(message, this._lastBlockId, this._toolCallCommitIds, this._lastCommitByBranch, this._skipBlocks);
374
471
  this._commits.push(...messageCommits);
375
472
  messageCommits.forEach((commit) => this._branchNames.add(commit.branch));
376
473
  this._trackToolCalls(messageCommits);
@@ -535,11 +632,14 @@ var ExecutionGraph = class {
535
632
  return toolCallMatch?.[1] || toolResultMatch?.[1];
536
633
  }
537
634
  };
538
- var messageToCommits = (message, lastBlockId, toolCallIds, lastCommitByBranch) => {
635
+ var messageToCommits = (message, lastBlockId, toolCallIds, lastCommitByBranch, skipBlocks) => {
539
636
  let previousBlockId = lastBlockId;
540
637
  return message.blocks.map((block, idx) => {
541
638
  const branch = getMessageBranch(message);
542
639
  const parents = getBlockParents(block, previousBlockId, message, toolCallIds, lastCommitByBranch);
640
+ if (skipBlocks?.includes(block._tag)) {
641
+ return null;
642
+ }
543
643
  const commit = createBlockCommit(block, message, branch, parents, idx);
544
644
  if (commit) {
545
645
  previousBlockId = commit.id;
@@ -697,128 +797,14 @@ var stringifyObject = (obj) => {
697
797
  return Obj3.getLabel(obj) ?? Obj3.getTypename(obj) ?? obj.id;
698
798
  };
699
799
 
700
- // src/functions/services.ts
701
- import * as Tool from "@effect/ai/Tool";
702
- import * as Context2 from "effect/Context";
703
- import * as Effect2 from "effect/Effect";
704
- import * as Layer from "effect/Layer";
705
- import * as Record from "effect/Record";
706
- import * as Schema2 from "effect/Schema";
707
- import { AiToolNotFoundError, ToolExecutionService, ToolResolverService } from "@dxos/ai";
708
- import { todo } from "@dxos/debug";
709
- import { Query as Query2 } from "@dxos/echo";
710
- import { DatabaseService as DatabaseService2, Function as Function2, FunctionDefinition, FunctionInvocationService } from "@dxos/functions";
711
- import { invariant as invariant2 } from "@dxos/invariant";
712
- var __dxlog_file4 = "/__w/dxos/dxos/packages/core/assistant/src/functions/services.ts";
713
- var makeToolResolverFromFunctions = (functions, toolkit) => {
714
- return Layer.effect(ToolResolverService, Effect2.gen(function* () {
715
- const dbService = yield* DatabaseService2;
716
- return {
717
- resolve: (id) => Effect2.gen(function* () {
718
- const tool = toolkit.tools[id];
719
- if (tool) {
720
- return tool;
721
- }
722
- const { objects: [dbFunction] } = yield* DatabaseService2.runQuery(Query2.type(Function2.Function, {
723
- key: id
724
- }));
725
- const functionDef = dbFunction ? FunctionDefinition.deserialize(dbFunction) : functions.find((fn2) => fn2.key === id);
726
- if (!functionDef) {
727
- return yield* Effect2.fail(new AiToolNotFoundError(id));
728
- }
729
- return projectFunctionToTool(functionDef);
730
- }).pipe(Effect2.provideService(DatabaseService2, dbService))
731
- };
732
- }));
733
- };
734
- var makeToolExecutionServiceFromFunctions = (toolkit, handlersLayer) => {
735
- return Layer.effect(ToolExecutionService, Effect2.gen(function* () {
736
- const toolkitHandler = yield* toolkit.pipe(Effect2.provide(handlersLayer));
737
- invariant2(isHandlerLike(toolkitHandler), void 0, {
738
- F: __dxlog_file4,
739
- L: 73,
740
- S: this,
741
- A: [
742
- "isHandlerLike(toolkitHandler)",
743
- ""
744
- ]
745
- });
746
- const functionInvocationService = yield* FunctionInvocationService;
747
- return {
748
- handlersFor: (toolkit2) => {
749
- const makeHandler = (tool) => {
750
- return Effect2.fn(`toolFunctionHandler ${tool.name}`)(function* (input) {
751
- if (toolkitHandler.tools[tool.name]) {
752
- if (Tool.isProviderDefined(tool)) {
753
- throw new Error("Attempted to call a provider-defined tool");
754
- }
755
- return yield* toolkitHandler.handle(tool.name, input);
756
- }
757
- const { definition: functionDef } = Context2.get(FunctionToolAnnotation)(tool.annotations);
758
- return yield* functionInvocationService.invokeFunction(functionDef, input).pipe(Effect2.catchAllDefect((defect) => Effect2.fail(defect)));
759
- });
760
- };
761
- return toolkit2.of(Record.map(toolkit2.tools, (tool, _name) => Tool.isUserDefined(tool) ? makeHandler(tool) : null));
762
- }
763
- };
764
- }));
765
- };
766
- var FunctionToolAnnotation = class extends Context2.Tag("@dxos/assistant/FunctionToolAnnotation")() {
767
- };
768
- var toolCache = /* @__PURE__ */ new WeakMap();
769
- var projectFunctionToTool = (fn2) => {
770
- if (toolCache.has(fn2)) {
771
- return toolCache.get(fn2);
772
- }
773
- const tool = Tool.make(makeToolName(fn2.name), {
774
- description: fn2.description,
775
- parameters: createStructFieldsFromSchema(fn2.inputSchema),
776
- // TODO(dmaretskyi): Include output schema.
777
- failure: Schema2.Any
778
- }).annotate(FunctionToolAnnotation, {
779
- definition: fn2
780
- });
781
- toolCache.set(fn2, tool);
782
- return tool;
783
- };
784
- var makeToolName = (name) => {
785
- const toolName = name.replace(/[^a-zA-Z0-9]/g, "_");
786
- invariant2(toolName.match(/^[a-zA-Z_][a-zA-Z0-9-_]*$/), void 0, {
787
- F: __dxlog_file4,
788
- L: 140,
789
- S: void 0,
790
- A: [
791
- "toolName.match(/^[a-zA-Z_][a-zA-Z0-9-_]*$/)",
792
- ""
793
- ]
794
- });
795
- return toolName;
796
- };
797
- var createStructFieldsFromSchema = (schema) => {
798
- switch (schema.ast._tag) {
799
- case "TypeLiteral":
800
- return Object.fromEntries(schema.ast.propertySignatures.map((prop) => [
801
- prop.name,
802
- Schema2.make(prop.type)
803
- ]));
804
- case "VoidKeyword":
805
- return {};
806
- default:
807
- return todo(`Unsupported schema AST: ${schema.ast._tag}`);
808
- }
809
- };
810
- var isHandlerLike = (value) => {
811
- return typeof value.tools === "object" && typeof value.handle === "function";
812
- };
813
-
814
800
  // src/templates/system.ts
815
801
  import defaultsDeep from "lodash.defaultsdeep";
816
802
  import { Template } from "@dxos/blueprints";
817
803
 
818
- // raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/format.tpl?raw
819
- 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>\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";
804
+ // raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/instructions/format.tpl?raw
805
+ 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";
820
806
 
821
- // raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/system.tpl?raw
807
+ // raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/instructions/system.tpl?raw
822
808
  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";
823
809
 
824
810
  // src/templates/system.ts
@@ -835,44 +821,6 @@ var templates = {
835
821
  source: system_default
836
822
  })
837
823
  };
838
-
839
- // src/types/artifact.ts
840
- import * as Schema3 from "effect/Schema";
841
- import { DXN as DXN2, LOCAL_SPACE_TAG } from "@dxos/keys";
842
- import { trim } from "@dxos/util";
843
- var createArtifactElement = (id) => `<artifact id=${id} />`;
844
- var ArtifactId = class extends Schema3.String.annotations({
845
- description: trim`
846
- The ID of the referenced object. Formats accepted:
847
- - DXN (dxn:echo:@:XXXXX). DXNs can be prepended with an @ symbol for compatibility with in-text references.
848
- - space ID, object ID tuple (spaceID:objectID)
849
- - Only object ID that is assumed to be in the current space (XXXXX)
850
- `,
851
- examples: [
852
- "dxn:echo:@:XXXXX",
853
- "@dxn:echo:@:XXXXX",
854
- "spaceID:objectID",
855
- "XXXXX"
856
- ]
857
- }) {
858
- static toDXN(reference, owningSpaceId) {
859
- if (reference.startsWith("@dxn:")) {
860
- return DXN2.parse(reference.slice(1));
861
- } else if (reference.startsWith("dxn:")) {
862
- return DXN2.parse(reference);
863
- } else if (/^[A-Z0-9]+:[A-Z0-9]+$/.test(reference)) {
864
- const [spaceId, objectId] = reference.split(":");
865
- return new DXN2(DXN2.kind.ECHO, [
866
- spaceId === owningSpaceId ? LOCAL_SPACE_TAG : spaceId,
867
- objectId
868
- ]);
869
- } else if (/^[A-Z0-9]+$/.test(reference)) {
870
- return DXN2.fromLocalObjectId(reference);
871
- } else {
872
- throw new Error(`Unable to parse object reference: ${reference}`);
873
- }
874
- }
875
- };
876
824
  export {
877
825
  AiAssistantError,
878
826
  AiContextBinder,
@@ -886,6 +834,8 @@ export {
886
834
  ContextBinding,
887
835
  ExecutionGraph,
888
836
  GenerationObserver,
837
+ GenericToolkit_exports as GenericToolkit,
838
+ RefFromLLM,
889
839
  VersionPin,
890
840
  applyDiffs,
891
841
  computeDiffsWithCursors,