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