@dxos/assistant 0.8.4-main.e8ec1fe → 0.8.4-main.ef1bc66f44

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 (100) hide show
  1. package/dist/lib/neutral/chunk-ZPC54VRM.mjs +596 -0
  2. package/dist/lib/neutral/chunk-ZPC54VRM.mjs.map +7 -0
  3. package/dist/lib/{browser → neutral}/extraction/index.mjs +5 -5
  4. package/dist/lib/neutral/extraction/index.mjs.map +7 -0
  5. package/dist/lib/{browser → neutral}/index.mjs +320 -681
  6. package/dist/lib/neutral/index.mjs.map +7 -0
  7. package/dist/lib/neutral/meta.json +1 -0
  8. package/dist/lib/neutral/testing/index.mjs +38 -0
  9. package/dist/lib/neutral/testing/index.mjs.map +7 -0
  10. package/dist/types/src/conversation/context.d.ts +97 -62
  11. package/dist/types/src/conversation/context.d.ts.map +1 -1
  12. package/dist/types/src/conversation/context.test.d.ts +2 -0
  13. package/dist/types/src/conversation/context.test.d.ts.map +1 -0
  14. package/dist/types/src/conversation/conversation.d.ts +40 -15
  15. package/dist/types/src/conversation/conversation.d.ts.map +1 -1
  16. package/dist/types/src/conversation/conversation.test.d.ts +2 -0
  17. package/dist/types/src/conversation/conversation.test.d.ts.map +1 -0
  18. package/dist/types/src/diff.d.ts +3 -1
  19. package/dist/types/src/diff.d.ts.map +1 -1
  20. package/dist/types/src/errors.d.ts +12 -16
  21. package/dist/types/src/errors.d.ts.map +1 -1
  22. package/dist/types/src/execution-graph.d.ts +2 -2
  23. package/dist/types/src/execution-graph.d.ts.map +1 -1
  24. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +247 -245
  25. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
  26. package/dist/types/src/extraction/extraction.d.ts +342 -308
  27. package/dist/types/src/extraction/extraction.d.ts.map +1 -1
  28. package/dist/types/src/extraction/quotes.d.ts +2 -2
  29. package/dist/types/src/extraction/quotes.d.ts.map +1 -1
  30. package/dist/types/src/functions/services.d.ts +6 -7
  31. package/dist/types/src/functions/services.d.ts.map +1 -1
  32. package/dist/types/src/functions.test.d.ts +2 -0
  33. package/dist/types/src/functions.test.d.ts.map +1 -0
  34. package/dist/types/src/session/GenericToolkit.d.ts +23 -0
  35. package/dist/types/src/session/GenericToolkit.d.ts.map +1 -1
  36. package/dist/types/src/session/artifact-diff.d.ts +1 -1
  37. package/dist/types/src/session/artifact-diff.d.ts.map +1 -1
  38. package/dist/types/src/session/format.d.ts +3 -3
  39. package/dist/types/src/session/format.d.ts.map +1 -1
  40. package/dist/types/src/session/index.d.ts +1 -0
  41. package/dist/types/src/session/index.d.ts.map +1 -1
  42. package/dist/types/src/session/observer.d.ts +4 -3
  43. package/dist/types/src/session/observer.d.ts.map +1 -1
  44. package/dist/types/src/session/session.d.ts +14 -9
  45. package/dist/types/src/session/session.d.ts.map +1 -1
  46. package/dist/types/src/session/toolkit.d.ts +5 -6
  47. package/dist/types/src/session/toolkit.d.ts.map +1 -1
  48. package/dist/types/src/session/version-pin.d.ts +2 -2
  49. package/dist/types/src/session/version-pin.d.ts.map +1 -1
  50. package/dist/types/src/testing/index.d.ts +2 -0
  51. package/dist/types/src/testing/index.d.ts.map +1 -0
  52. package/dist/types/src/testing/layer.d.ts +22 -0
  53. package/dist/types/src/testing/layer.d.ts.map +1 -0
  54. package/dist/types/src/types/artifact.d.ts +13 -5
  55. package/dist/types/src/types/artifact.d.ts.map +1 -1
  56. package/dist/types/tsconfig.tsbuildinfo +1 -1
  57. package/package.json +40 -33
  58. package/src/conversation/context.test.ts +46 -0
  59. package/src/conversation/context.ts +211 -67
  60. package/src/conversation/conversation.test.ts +65 -0
  61. package/src/conversation/conversation.ts +123 -48
  62. package/src/diff.test.ts +35 -4
  63. package/src/diff.ts +13 -4
  64. package/src/errors.ts +2 -2
  65. package/src/execution-graph.test.ts +1 -1
  66. package/src/execution-graph.ts +4 -4
  67. package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +7 -7
  68. package/src/extraction/extraction-ner-function/extraction-ner-function.ts +1 -1
  69. package/src/extraction/extraction.ts +4 -4
  70. package/src/extraction/quotes.ts +4 -5
  71. package/src/functions/services.ts +55 -30
  72. package/src/functions.conversations.json +1 -0
  73. package/src/functions.test.ts +74 -0
  74. package/src/session/GenericToolkit.ts +30 -0
  75. package/src/session/artifact-diff.ts +1 -1
  76. package/src/session/format.test.ts +101 -38
  77. package/src/session/format.ts +12 -13
  78. package/src/session/index.ts +1 -0
  79. package/src/session/observer.ts +3 -1
  80. package/src/session/session.conversations.json +1 -1
  81. package/src/session/session.test.ts +26 -13
  82. package/src/session/session.ts +36 -14
  83. package/src/session/toolkit.ts +13 -22
  84. package/src/session/version-pin.ts +4 -3
  85. package/src/templates/instructions/format.tpl +0 -4
  86. package/src/testing/index.ts +5 -0
  87. package/src/testing/layer.ts +84 -0
  88. package/src/types/artifact.ts +31 -13
  89. package/dist/lib/browser/extraction/index.mjs.map +0 -7
  90. package/dist/lib/browser/index.mjs.map +0 -7
  91. package/dist/lib/browser/meta.json +0 -1
  92. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +0 -11
  93. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +0 -7
  94. package/dist/lib/node-esm/extraction/index.mjs +0 -226
  95. package/dist/lib/node-esm/extraction/index.mjs.map +0 -7
  96. package/dist/lib/node-esm/index.mjs +0 -1291
  97. package/dist/lib/node-esm/index.mjs.map +0 -7
  98. package/dist/lib/node-esm/meta.json +0 -1
  99. /package/dist/lib/{browser → neutral}/chunk-J5LGTIGS.mjs +0 -0
  100. /package/dist/lib/{browser → neutral}/chunk-J5LGTIGS.mjs.map +0 -0
@@ -1,30 +1,47 @@
1
1
  import {
2
- __export
3
- } from "./chunk-J5LGTIGS.mjs";
2
+ AiAssistantError,
3
+ AiModelError,
4
+ AiSession,
5
+ ArtifactDiffResolver,
6
+ ArtifactId,
7
+ GenerationObserver,
8
+ GenericToolkit_exports,
9
+ RefFromLLM,
10
+ ToolExecutionServices,
11
+ VersionPin,
12
+ createArtifactElement,
13
+ createToolkit,
14
+ formatSystemPrompt,
15
+ formatUserPrompt,
16
+ makeToolExecutionServiceFromFunctions,
17
+ makeToolResolverFromFunctions
18
+ } from "./chunk-ZPC54VRM.mjs";
19
+ import "./chunk-J5LGTIGS.mjs";
4
20
 
5
21
  // src/conversation/context.ts
6
- import { computed } from "@preact/signals-core";
7
- import * as Array2 from "effect/Array";
22
+ import { Atom, Registry } from "@effect-atom/atom-react";
23
+ import * as EArray from "effect/Array";
8
24
  import * as Context from "effect/Context";
25
+ import * as Effect from "effect/Effect";
9
26
  import * as Function from "effect/Function";
10
27
  import * as Schema from "effect/Schema";
11
28
  import { Blueprint } from "@dxos/blueprints";
12
29
  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";
30
+ import { DXN, Obj, Query, Type } from "@dxos/echo";
31
+ import { assertArgument } from "@dxos/invariant";
32
+ import { log } from "@dxos/log";
33
+ import { ComplexSet, isTruthy } from "@dxos/util";
16
34
  var __dxlog_file = "/__w/dxos/dxos/packages/core/assistant/src/conversation/context.ts";
17
35
  var ContextBinding = Schema.Struct({
18
36
  blueprints: Schema.Struct({
19
37
  added: Schema.Array(Type.Ref(Blueprint.Blueprint)),
20
38
  removed: Schema.Array(Type.Ref(Blueprint.Blueprint))
21
39
  }),
22
- // TODO(burdon): Type.Expando => Type.Obj (or Obj.Any?)
23
40
  objects: Schema.Struct({
24
- added: Schema.Array(Type.Ref(Type.Expando)),
25
- removed: Schema.Array(Type.Ref(Type.Expando))
41
+ added: Schema.Array(Type.Ref(Type.Obj)),
42
+ removed: Schema.Array(Type.Ref(Type.Obj))
26
43
  })
27
- }).pipe(Type.Obj({
44
+ }).pipe(Type.object({
28
45
  typename: "dxos.org/type/ContextBinding",
29
46
  version: "0.1.0"
30
47
  }));
@@ -34,614 +51,388 @@ var Bindings = class {
34
51
  objects = new ComplexSet((ref) => ref.dxn.asEchoDXN()?.echoId);
35
52
  toJSON() {
36
53
  return {
37
- blueprints: Array2.fromIterable(this.blueprints).map((ref) => ref.dxn.toString()),
38
- objects: Array2.fromIterable(this.objects).map((ref) => ref.dxn.toString())
54
+ blueprints: EArray.fromIterable(this.blueprints).map((ref) => ref.dxn.toString()),
55
+ objects: EArray.fromIterable(this.objects).map((ref) => ref.dxn.toString())
39
56
  };
40
57
  }
41
58
  };
42
59
  var AiContextBinder = class extends Resource {
60
+ _blueprints = Atom.make([]).pipe(Atom.keepAlive);
61
+ _objects = Atom.make([]).pipe(Atom.keepAlive);
62
+ _registry;
43
63
  _queue;
64
+ constructor(options) {
65
+ super();
66
+ assertArgument(options.queue, "options.queue", "Queue is required");
67
+ this._queue = options.queue;
68
+ this._registry = options.registry ?? Registry.make();
69
+ }
44
70
  /**
45
- * Reactive query of all bindings.
71
+ * Returns the blueprints atom for subscription.
46
72
  */
47
- // TODO(burdon): Cache value?
48
- _bindings;
49
- _blueprints;
50
- _objects;
51
- constructor(_queue) {
52
- super(), this._queue = _queue;
53
- }
54
- get bindings() {
55
- invariant(this._bindings, "AiContextBinder not open", {
56
- F: __dxlog_file,
57
- L: 78,
58
- S: this,
59
- A: [
60
- "this._bindings",
61
- "'AiContextBinder not open'"
62
- ]
63
- });
64
- return this._bindings;
65
- }
66
73
  get blueprints() {
67
- invariant(this._blueprints, "AiContextBinder not open", {
68
- F: __dxlog_file,
69
- L: 83,
70
- S: this,
71
- A: [
72
- "this._blueprints",
73
- "'AiContextBinder not open'"
74
- ]
75
- });
76
74
  return this._blueprints;
77
75
  }
76
+ /**
77
+ * Returns the objects atom for subscription.
78
+ */
78
79
  get objects() {
79
- invariant(this._objects, "AiContextBinder not open", {
80
- F: __dxlog_file,
81
- L: 88,
82
- S: this,
83
- A: [
84
- "this._objects",
85
- "'AiContextBinder not open'"
86
- ]
87
- });
88
80
  return this._objects;
89
81
  }
90
- // TODO(wittjosiah): Use parent context?
91
- async _open() {
92
- const query = this._queue.query(Query.select(Filter.everything()));
93
- this._ctx.onDispose(query.subscribe(() => {
94
- }));
95
- this._bindings = computed(() => this._reduce(query.objects));
96
- this._blueprints = computed(() => [
97
- ...this.bindings.value.blueprints
98
- ]);
99
- this._objects = computed(() => [
100
- ...this.bindings.value.objects
101
- ]);
82
+ /**
83
+ * Gets the current blueprints value.
84
+ */
85
+ getBlueprints() {
86
+ return this._registry.get(this._blueprints);
102
87
  }
103
- async _close() {
104
- this._bindings = void 0;
105
- this._blueprints = void 0;
106
- this._objects = void 0;
88
+ /**
89
+ * Gets the current objects value.
90
+ */
91
+ getObjects() {
92
+ return this._registry.get(this._objects);
93
+ }
94
+ /**
95
+ * Subscribe to changes in blueprints.
96
+ */
97
+ subscribeBlueprints(cb) {
98
+ return this._registry.subscribe(this._blueprints, () => cb(this._registry.get(this._blueprints)));
107
99
  }
108
100
  /**
109
- * Asynchronous query of all bindings.
101
+ * Subscribe to changes in objects.
110
102
  */
111
- async query() {
112
- const { objects } = await this._queue.query(Query.select(Filter.everything())).run();
113
- return this._reduce(objects);
103
+ subscribeObjects(cb) {
104
+ return this._registry.subscribe(this._objects, () => cb(this._registry.get(this._objects)));
105
+ }
106
+ async _open() {
107
+ const query = this._queue.query(Query.type(ContextBinding));
108
+ const initialResults = await query.run();
109
+ await this._updateBindings(initialResults);
110
+ this._ctx.onDispose(query.subscribe(async () => {
111
+ await this._updateBindings(query.results);
112
+ }));
113
+ }
114
+ async _updateBindings(items) {
115
+ if (items.length === 0) {
116
+ return;
117
+ }
118
+ const bindings = this._reduce(items);
119
+ const currentBlueprints = this._registry.get(this._blueprints);
120
+ const currentObjects = this._registry.get(this._objects);
121
+ const newBlueprints = await this._resolve(bindings.blueprints, currentBlueprints);
122
+ const newObjects = await this._resolve(bindings.objects, currentObjects);
123
+ this._registry.set(this._blueprints, newBlueprints);
124
+ this._registry.set(this._objects, newObjects);
125
+ log("updated bindings", {
126
+ blueprints: newBlueprints.length,
127
+ objects: newObjects.length
128
+ }, {
129
+ F: __dxlog_file,
130
+ L: 159,
131
+ S: this,
132
+ C: (f, a) => f(...a)
133
+ });
114
134
  }
115
- // TODO(burdon): Pass in Blueprint obj (from registry?) and create reference.
116
- async bind(props) {
117
- const blueprints = props.blueprints?.filter((ref) => !this.blueprints.peek().find((b) => b.dxn.toString() === ref.dxn.toString())) ?? [];
118
- const objects = props.objects?.filter((ref) => !this.objects.peek().find((o) => o.dxn.toString() === ref.dxn.toString())) ?? [];
119
- if (!blueprints.length && !objects.length) {
135
+ async _close() {
136
+ this._registry.set(this._blueprints, []);
137
+ this._registry.set(this._objects, []);
138
+ }
139
+ async bind({ blueprints, objects }) {
140
+ const currentBlueprints = this._registry.get(this._blueprints);
141
+ const currentObjects = this._registry.get(this._objects);
142
+ const { added: addedBlueprints, next: nextBlueprints } = this._processBindings(blueprints, currentBlueprints);
143
+ const { added: addedObjects, next: nextObjects } = this._processBindings(objects, currentObjects);
144
+ if (!addedBlueprints.length && !addedObjects.length) {
120
145
  return;
121
146
  }
147
+ this._registry.set(this._blueprints, nextBlueprints);
148
+ this._registry.set(this._objects, nextObjects);
149
+ log("bind", {
150
+ blueprints: addedBlueprints.length,
151
+ objects: addedObjects.length
152
+ }, {
153
+ F: __dxlog_file,
154
+ L: 185,
155
+ S: this,
156
+ C: (f, a) => f(...a)
157
+ });
122
158
  await this._queue.append([
123
159
  Obj.make(ContextBinding, {
124
160
  blueprints: {
125
- added: blueprints,
161
+ added: addedBlueprints,
126
162
  removed: []
127
163
  },
128
164
  objects: {
129
- added: objects,
165
+ added: addedObjects,
130
166
  removed: []
131
167
  }
132
168
  })
133
169
  ]);
134
170
  }
135
- async unbind(props) {
136
- if (!props.blueprints?.length && !props.objects?.length) {
171
+ async unbind({ blueprints, objects }) {
172
+ if (!blueprints?.length && !objects?.length) {
137
173
  return;
138
174
  }
175
+ log("unbind", {
176
+ blueprints: blueprints?.length,
177
+ objects: objects?.length
178
+ }, {
179
+ F: __dxlog_file,
180
+ L: 205,
181
+ S: this,
182
+ C: (f, a) => f(...a)
183
+ });
139
184
  await this._queue.append([
140
185
  Obj.make(ContextBinding, {
141
186
  blueprints: {
142
187
  added: [],
143
- removed: props.blueprints ?? []
188
+ removed: blueprints ?? []
144
189
  },
145
190
  objects: {
146
191
  added: [],
147
- removed: props.objects ?? []
192
+ removed: objects ?? []
148
193
  }
149
194
  })
150
195
  ]);
151
196
  }
197
+ /**
198
+ * Process bindings to filter duplicates and determine next state.
199
+ */
200
+ _processBindings(refs, current) {
201
+ const next = [
202
+ ...current
203
+ ];
204
+ const added = [];
205
+ if (!refs?.length) {
206
+ return {
207
+ added,
208
+ next
209
+ };
210
+ }
211
+ const seen = new Set(current.map((obj) => Obj.getDXN(obj).toString()));
212
+ for (const ref of refs) {
213
+ const dxn = ref.dxn.toString();
214
+ if (!seen.has(dxn)) {
215
+ seen.add(dxn);
216
+ added.push(ref);
217
+ if (ref.isAvailable) {
218
+ const target = ref.target;
219
+ if (target) {
220
+ next.push(target);
221
+ }
222
+ }
223
+ }
224
+ }
225
+ return {
226
+ added,
227
+ next
228
+ };
229
+ }
230
+ /**
231
+ * Reduce results into sets of blueprints and objects.
232
+ */
152
233
  _reduce(items) {
153
- return Function.pipe(items, Array2.filter(Obj.instanceOf(ContextBinding)), Array2.reduce(new Bindings(), (context, item) => {
154
- item.blueprints.removed.forEach((ref) => context.blueprints.delete(ref));
155
- item.blueprints.added.forEach((ref) => context.blueprints.add(ref));
156
- item.objects.removed.forEach((ref) => {
234
+ return Function.pipe(items, EArray.reduce(new Bindings(), (context, { blueprints, objects }) => {
235
+ blueprints.added.forEach((ref) => context.blueprints.add(ref));
236
+ blueprints.removed.forEach((ref) => context.blueprints.delete(ref));
237
+ objects.added.forEach((ref) => context.objects.add(ref));
238
+ objects.removed.forEach((ref) => {
157
239
  for (const obj of context.objects) {
158
240
  if (DXN.equalsEchoId(obj.dxn, ref.dxn)) {
159
241
  context.objects.delete(obj);
160
242
  }
161
243
  }
162
244
  });
163
- item.objects.added.forEach((ref) => context.objects.add(ref));
164
245
  return context;
165
246
  }));
166
247
  }
167
- };
168
- var AiContextService = class extends Context.Tag("@dxos/assistant/AiContextService")() {
169
- };
170
-
171
- // src/conversation/conversation.ts
172
- import * as Array5 from "effect/Array";
173
- import * as Effect6 from "effect/Effect";
174
- import * as Option2 from "effect/Option";
175
- import { Resource as Resource2 } from "@dxos/context";
176
- import { Obj as Obj4 } from "@dxos/echo";
177
- import { DatabaseService as DatabaseService2 } from "@dxos/echo-db";
178
- import { log as log3 } from "@dxos/log";
179
- import { Message as Message3 } from "@dxos/types";
180
-
181
- // src/session/artifact-diff.ts
182
- import * as Context2 from "effect/Context";
183
- var ArtifactDiffResolver = class extends Context2.Tag("@dxos/assistant/ArtifactDiffResolver")() {
184
- };
185
-
186
- // src/session/observer.ts
187
- import * as Effect from "effect/Effect";
188
- import * as Function2 from "effect/Function";
189
- var GenerationObserver = Object.freeze({
190
- 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) } = {}) => ({
191
- onBegin,
192
- onPart,
193
- onBlock,
194
- onEnd,
195
- onMessage
196
- }),
197
- noop: () => GenerationObserver.make(),
198
248
  /**
199
- * Debug printer to be used in unit-tests and browser devtools.
249
+ * Resolve references to objects, loading them first if needed and falling back to existing objects.
200
250
  */
201
- fromPrinter: (printer) => GenerationObserver.make({
202
- onBlock: (block) => Effect.sync(() => {
203
- if (block.pending) {
204
- return;
205
- }
206
- printer.printContentBlock(block);
207
- }),
208
- onMessage: (message) => Effect.sync(() => {
209
- if (message.sender.role === "assistant") {
210
- return;
211
- }
212
- printer.printMessage(message);
213
- })
214
- })
215
- });
216
-
217
- // src/session/session.ts
218
- import * as LanguageModel from "@effect/ai/LanguageModel";
219
- import * as Chunk from "effect/Chunk";
220
- import * as Effect3 from "effect/Effect";
221
- import * as Stream from "effect/Stream";
222
- import { AiParser, AiPreprocessor, callTool, getToolCalls } from "@dxos/ai";
223
- import { todo } from "@dxos/debug";
224
- import { Obj as Obj3 } from "@dxos/echo";
225
- import { TracingService } from "@dxos/functions";
226
- import { log as log2 } from "@dxos/log";
227
- import { Message as Message2 } from "@dxos/types";
228
-
229
- // src/session/format.ts
230
- import * as Array4 from "effect/Array";
231
- import * as Effect2 from "effect/Effect";
232
- import * as Function3 from "effect/Function";
233
- import * as Option from "effect/Option";
234
- import { Template } from "@dxos/blueprints";
235
- import { Obj as Obj2 } from "@dxos/echo";
236
- import { DatabaseService, ObjectVersion } from "@dxos/echo-db";
237
- import { log } from "@dxos/log";
238
- import { Message } from "@dxos/types";
239
- import { trim } from "@dxos/util";
240
-
241
- // src/errors.ts
242
- import { BaseError } from "@dxos/errors";
243
- var AiModelError = class extends BaseError.extend("AI_MODEL_ERROR", "AI model error") {
244
- };
245
- var AiAssistantError = class extends BaseError.extend("AI_ASSISTANT_ERROR", "AI assistant error") {
246
- };
247
-
248
- // src/session/format.ts
249
- var __dxlog_file2 = "/__w/dxos/dxos/packages/core/assistant/src/session/format.ts";
250
- var formatSystemPrompt = ({ system, blueprints = [], objects = [] }) => Effect2.gen(function* () {
251
- 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`
252
- <blueprint>
253
- ${Template.process(template.value.content)}
254
- </blueprint>
255
- `)), Effect2.map((blueprints2) => blueprints2.length > 0 ? [
256
- "## Blueprints Definitions",
257
- ...blueprints2
258
- ].join("\n\n") : void 0));
259
- const objectDefs = yield* Function3.pipe(objects, Effect2.forEach((object) => Effect2.succeed(trim`
260
- <object>
261
- <dxn>${Obj2.getDXN(object)}</dxn>
262
- <typename>${Obj2.getTypename(object)}</typename>
263
- </object>
264
- `)), Effect2.map((objects2) => objects2.length > 0 ? [
265
- "## Context Objects",
266
- ...objects2
267
- ].join("\n\n") : void 0));
268
- return yield* Function3.pipe(Effect2.succeed([
269
- system,
270
- blueprintDefs,
271
- objectDefs
272
- ].filter((def) => def !== void 0)), Effect2.map((parts) => parts.join("\n\n")));
273
- });
274
- var formatUserPrompt = ({ prompt, history = [] }) => Effect2.gen(function* () {
275
- const blocks = [];
276
- const artifactDiffResolver = yield* Effect2.serviceOption(ArtifactDiffResolver);
277
- if (Option.isSome(artifactDiffResolver)) {
278
- const versions = gatherObjectVersions(history);
279
- const artifactDiff = yield* Effect2.tryPromise({
280
- try: () => artifactDiffResolver.value.resolve([
281
- ...versions.entries()
282
- ].map(([id, version]) => ({
283
- id,
284
- lastVersion: version
285
- }))),
286
- catch: AiAssistantError.wrap({
287
- message: "Artifact diff resolution error"
288
- })
289
- });
290
- log("version", {
291
- artifactDiff,
292
- versions
293
- }, {
294
- F: __dxlog_file2,
295
- L: 95,
296
- S: this,
297
- C: (f, a) => f(...a)
298
- });
299
- for (const [id, { version }] of [
300
- ...artifactDiff.entries()
301
- ]) {
302
- if (ObjectVersion.equals(version, versions.get(id))) {
303
- artifactDiff.delete(id);
304
- continue;
305
- }
306
- blocks.push({
307
- _tag: "anchor",
308
- objectId: id,
309
- version
310
- });
311
- }
312
- if (artifactDiff.size > 0) {
313
- blocks.push(createArtifactUpdateBlock(artifactDiff));
314
- }
315
- }
316
- return Obj2.make(Message.Message, {
317
- created: (/* @__PURE__ */ new Date()).toISOString(),
318
- sender: {
319
- role: "user"
320
- },
321
- blocks: [
322
- ...blocks,
323
- {
324
- _tag: "text",
325
- text: prompt
326
- }
327
- ]
328
- });
329
- });
330
- var gatherObjectVersions = (messages) => {
331
- const artifactIds = /* @__PURE__ */ new Map();
332
- for (const message of messages) {
333
- for (const block of message.blocks) {
334
- if (block._tag === "anchor") {
335
- artifactIds.set(block.objectId, block.version);
251
+ async _resolve(refs, current) {
252
+ const refArray = [
253
+ ...refs
254
+ ];
255
+ await Promise.all(refArray.map((ref) => ref.load()));
256
+ return refArray.map((ref) => {
257
+ let target;
258
+ if (ref.isAvailable) {
259
+ target = ref.target;
336
260
  }
337
- }
261
+ return target ?? current.find((obj) => Obj.getDXN(obj).toString() === ref.dxn.toString());
262
+ }).filter(isTruthy);
338
263
  }
339
- return artifactIds;
340
264
  };
341
- var createArtifactUpdateBlock = (artifactDiff) => {
342
- return {
343
- _tag: "text",
344
- // TODO(dmaretskyi): Does this need to be a special content-block?
345
- disposition: "artifact-update",
346
- text: trim`
347
- The following artifacts have been updated since the last message:
348
- ${[
349
- ...artifactDiff.entries()
350
- ].map(([id, { diff }]) => `<changed-artifact id="${id}">${diff ? `
351
- ${diff}` : ""}</changed-artifact>`).join("\n")}
352
- `
353
- };
354
- };
355
-
356
- // src/session/session.ts
357
- var __dxlog_file3 = "/__w/dxos/dxos/packages/core/assistant/src/session/session.ts";
358
- var AiSession = class {
359
- _options;
360
- /** Prevents concurrent execution of session. */
361
- _semaphore = Effect3.runSync(Effect3.makeSemaphore(1));
362
- /** Message history from queue. */
363
- // TODO(burdon): Evolve into supporting a git-like graph of messages.
364
- _history = [];
365
- /** Pending messages for this session (incl. the current prompt). */
366
- _pending = [];
367
- constructor(_options = {}) {
368
- this._options = _options;
369
- }
370
- run = ({ prompt, system: systemTemplate, history = [], objects = [], blueprints = [], toolkit, observer = GenerationObserver.noop() }) => Effect3.gen(this, function* () {
371
- this._history = [
372
- ...history
373
- ];
374
- this._pending = [];
375
- const pending = this._pending;
376
- const submitMessage = Effect3.fnUntraced(function* (message) {
377
- pending.push(message);
378
- yield* observer.onMessage(message);
379
- yield* TracingService.emitConverationMessage(message);
380
- return message;
381
- });
382
- const promptMessage = yield* submitMessage(yield* formatUserPrompt({
383
- prompt,
384
- history
385
- }));
386
- const system = yield* formatSystemPrompt({
387
- system: systemTemplate,
265
+ var AiContextService = class _AiContextService extends Context.Tag("@dxos/assistant/AiContextService")() {
266
+ static bindContext = ({ blueprints, objects }) => Effect.gen(function* () {
267
+ const { binder } = yield* _AiContextService;
268
+ yield* Effect.promise(() => binder.bind({
388
269
  blueprints,
389
270
  objects
271
+ }));
272
+ });
273
+ static findObjects = (type) => {
274
+ return Effect.gen(function* () {
275
+ const { binder } = yield* _AiContextService;
276
+ return binder.getObjects().filter(Obj.instanceOf(type));
390
277
  });
391
- do {
392
- log2("request", {
393
- prompt: promptMessage,
394
- system: {
395
- snippet: createSnippet(system),
396
- length: system.length
397
- },
398
- pending: this._pending.length,
399
- history: this._history.length,
400
- objects: objects?.length ?? 0
401
- }, {
402
- F: __dxlog_file3,
403
- L: 110,
404
- S: this,
405
- C: (f, a) => f(...a)
406
- });
407
- const prompt2 = yield* AiPreprocessor.preprocessPrompt([
408
- ...this._history,
409
- ...this._pending
410
- ], {
411
- system
412
- });
413
- const blocks = yield* LanguageModel.streamText({
414
- prompt: prompt2,
415
- toolkit,
416
- disableToolCallResolution: true
417
- }).pipe(
418
- // TOOD(dmaretskyi): Error mapping.
419
- AiParser.parseResponse({
420
- onBegin: () => observer.onBegin(),
421
- onBlock: (block) => observer.onBlock(block),
422
- onPart: (part) => observer.onPart(part),
423
- onEnd: (summary) => observer.onEnd(summary)
424
- }),
425
- Stream.runCollect,
426
- Effect3.map(Chunk.toArray)
427
- );
428
- const response = yield* submitMessage(Obj3.make(Message2.Message, {
429
- created: (/* @__PURE__ */ new Date()).toISOString(),
430
- sender: {
431
- role: "assistant"
432
- },
433
- blocks
434
- }));
435
- const toolCalls = getToolCalls(response);
436
- if (toolCalls.length === 0) {
437
- break;
438
- } else if (!toolkit) {
439
- throw new Error("No toolkit provided");
440
- }
441
- const toolResults = yield* Effect3.forEach(toolCalls, (toolCall) => {
442
- return callTool(toolkit, toolCall).pipe(Effect3.provide(TracingService.layerSubframe((context) => ({
443
- ...context,
444
- parentMessage: response.id,
445
- toolCallId: toolCall.toolCallId
446
- }))));
447
- });
448
- yield* submitMessage(Obj3.make(Message2.Message, {
449
- created: (/* @__PURE__ */ new Date()).toISOString(),
450
- sender: {
451
- role: "tool"
452
- },
453
- blocks: toolResults
454
- }));
455
- } while (true);
456
- log2("done", {
457
- pending: this._pending.length
458
- }, {
459
- F: __dxlog_file3,
460
- L: 181,
461
- S: this,
462
- C: (f, a) => f(...a)
463
- });
464
- return this._pending;
465
- }).pipe(this._semaphore.withPermits(1), Effect3.withSpan("AiSession.run"));
278
+ };
466
279
  /**
280
+ * Glorified type cast until we figure out function service typing.
281
+ * For context, the AiContextService is provided to functions called from tool calls, but it's not implemented in types yet.
467
282
  * @deprecated
468
283
  */
469
- // TODO(burdon): Implement or remove.
470
- async runStructured(_schema, _options) {
471
- return todo();
472
- }
473
- };
474
- var createSnippet = (text, len = 32) => text.length <= len * 2 ? text : [
475
- text.slice(0, len),
476
- "...",
477
- text.slice(-len)
478
- ].join("");
479
-
480
- // src/session/toolkit.ts
481
- import * as Toolkit from "@effect/ai/Toolkit";
482
- import * as Effect4 from "effect/Effect";
483
- import { ToolExecutionService, ToolResolverService } from "@dxos/ai";
484
- import { isTruthy } from "@dxos/util";
485
- var createToolkit = ({ toolkit: toolkitParam, toolIds = [], blueprints = [] }) => Effect4.gen(function* () {
486
- const blueprintToolkit = yield* ToolResolverService.resolveToolkit([
487
- ...blueprints.flatMap(({ tools }) => tools),
488
- ...toolIds
489
- ]);
490
- const blueprintToolkitHandler = yield* blueprintToolkit.toContext(ToolExecutionService.handlersFor(blueprintToolkit));
491
- const toolkit = Toolkit.merge(...[
492
- toolkitParam,
493
- blueprintToolkit
494
- ].filter(isTruthy));
495
- return yield* toolkit.pipe(Effect4.provide(blueprintToolkitHandler));
496
- });
497
-
498
- // src/session/version-pin.ts
499
- import * as Schema2 from "effect/Schema";
500
- import { ObjectId } from "@dxos/echo/internal";
501
- import { getVersion } from "@dxos/echo-db";
502
- var ObjectVersionSchema = Schema2.Unknown;
503
- var VersionPinSchema = Schema2.Struct({
504
- // TODO(dmaretskyi): Use Ref when those support encoding.
505
- objectId: ObjectId,
506
- // TODO(dmaretskyi): Could be opaque.
507
- version: ObjectVersionSchema
508
- });
509
- var VersionPin = class extends VersionPinSchema {
510
- static DISPOSITION = "version-pin";
511
- static fromObject(object) {
512
- return VersionPin.make({
513
- objectId: object.id,
514
- version: getVersion(object)
515
- });
516
- }
517
- static createBlock(pin) {
518
- return {
519
- _tag: "json",
520
- disposition: VersionPin.DISPOSITION,
521
- data: JSON.stringify(pin)
522
- };
523
- }
524
- };
525
-
526
- // src/session/GenericToolkit.ts
527
- var GenericToolkit_exports = {};
528
- __export(GenericToolkit_exports, {
529
- TypeId: () => TypeId,
530
- make: () => make,
531
- merge: () => merge3
532
- });
533
- import * as Toolkit2 from "@effect/ai/Toolkit";
534
- import * as Effect5 from "effect/Effect";
535
- import * as Layer from "effect/Layer";
536
- import * as Pipeable from "effect/Pipeable";
537
- var TypeId = "~@dxos/assistant/GenericToolkit";
538
- var make = (toolkit, layer) => {
539
- return {
540
- [TypeId]: TypeId,
541
- toolkit,
542
- layer,
543
- handlers: toolkit.pipe(Effect5.provide(layer)),
544
- pipe() {
545
- return Pipeable.pipeArguments(this, arguments);
546
- }
547
- };
548
- };
549
- var merge3 = (...toolkits) => {
550
- return make(Toolkit2.merge(...toolkits.map((t) => t.toolkit)), Layer.mergeAll(...toolkits.map((t) => t.layer)));
284
+ static fixFunctionHandlerType = (eff) => eff;
551
285
  };
552
286
 
553
287
  // src/conversation/conversation.ts
554
- var __dxlog_file4 = "/__w/dxos/dxos/packages/core/assistant/src/conversation/conversation.ts";
288
+ import * as Context2 from "effect/Context";
289
+ import * as Effect2 from "effect/Effect";
290
+ import * as Layer from "effect/Layer";
291
+ import { Resource as Resource2 } from "@dxos/context";
292
+ import { Obj as Obj2 } from "@dxos/echo";
293
+ import { acquireReleaseResource } from "@dxos/effect";
294
+ import { QueueService } from "@dxos/functions";
295
+ import { invariant } from "@dxos/invariant";
296
+ import { log as log2 } from "@dxos/log";
297
+ import { Message } from "@dxos/types";
298
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/core/assistant/src/conversation/conversation.ts";
555
299
  var AiConversation = class extends Resource2 {
556
300
  /**
557
- * Message and binding queue.
301
+ * Blueprints and objects bound to the conversation.
558
302
  */
303
+ _binder;
559
304
  _queue;
560
- /**
561
- * Blueprints bound to the conversation.
562
- */
563
- _context;
564
- /**
565
- * Toolkit from the current session request.
566
- */
567
305
  _toolkit;
568
- constructor(queue) {
306
+ constructor(options) {
569
307
  super();
570
- this._queue = queue;
571
- this._context = new AiContextBinder(this._queue);
308
+ this._queue = options.queue;
309
+ this._toolkit = options.toolkit;
310
+ invariant(this._queue, void 0, {
311
+ F: __dxlog_file2,
312
+ L: 60,
313
+ S: this,
314
+ A: [
315
+ "this._queue",
316
+ ""
317
+ ]
318
+ });
319
+ this._binder = new AiContextBinder({
320
+ queue: this._queue,
321
+ registry: options.registry
322
+ });
572
323
  }
573
324
  async _open() {
574
- await this._context.open();
325
+ await this._binder.open(this._ctx);
575
326
  }
576
- async _close() {
577
- await this._context.close();
327
+ get queue() {
328
+ return this._queue;
578
329
  }
579
330
  get context() {
580
- return this._context;
331
+ return this._binder;
581
332
  }
582
333
  get toolkit() {
583
334
  return this._toolkit;
584
335
  }
585
336
  async getHistory() {
586
337
  const queueItems = await this._queue.queryObjects();
587
- return queueItems.filter(Obj4.instanceOf(Message3.Message));
338
+ return queueItems.filter(Obj2.instanceOf(Message.Message));
339
+ }
340
+ getTools() {
341
+ return Effect2.gen(this, function* () {
342
+ const blueprints = this.context.getBlueprints();
343
+ const tookit = yield* createToolkit({
344
+ toolkit: this._toolkit,
345
+ blueprints
346
+ });
347
+ return tookit.tools;
348
+ }).pipe(Effect2.orDie);
588
349
  }
589
350
  /**
590
351
  * Creates a new cancelable request effect.
591
352
  */
592
353
  createRequest(params) {
593
- return Effect6.gen(this, function* () {
594
- const session = new AiSession();
595
- const history = yield* Effect6.promise(() => this.getHistory());
596
- const context = yield* Effect6.promise(() => this.context.query());
597
- const blueprints = yield* Effect6.forEach(context.blueprints.values(), DatabaseService2.loadOption).pipe(Effect6.map(Array5.filter(Option2.isSome)), Effect6.map(Array5.map((option) => option.value)));
598
- const objects = yield* Effect6.forEach(context.objects.values(), DatabaseService2.loadOption).pipe(Effect6.map(Array5.filter(Option2.isSome)), Effect6.map(Array5.map((option) => option.value)));
354
+ return Effect2.gen(this, function* () {
355
+ const history = yield* Effect2.promise(() => this.getHistory());
356
+ const blueprints = this.context.getBlueprints();
599
357
  const toolkit = yield* createToolkit({
358
+ toolkit: this._toolkit,
600
359
  blueprints
601
360
  });
602
- this._toolkit = toolkit;
603
- const start = Date.now();
604
- log3("run", {
361
+ const objects = this.context.getObjects();
362
+ log2("run", {
605
363
  history: history.length,
606
364
  blueprints: blueprints.length,
607
- objects: objects.length,
608
- tools: this._toolkit?.tools.length ?? 0
365
+ tools: Object.keys(toolkit.tools).length,
366
+ objects: objects.length
609
367
  }, {
610
- F: __dxlog_file4,
611
- L: 107,
368
+ F: __dxlog_file2,
369
+ L: 112,
612
370
  S: this,
613
371
  C: (f, a) => f(...a)
614
372
  });
373
+ const session = new AiSession();
615
374
  const messages = yield* session.run({
616
375
  history,
617
376
  blueprints,
618
- objects,
619
377
  toolkit,
378
+ objects,
620
379
  ...params
621
- }).pipe(Effect6.provideService(AiContextService, {
380
+ }).pipe(Effect2.provide(Layer.mergeAll(Layer.succeed(AiContextService, {
622
381
  binder: this.context
623
- }));
624
- log3("result", {
382
+ }), Layer.succeed(AiConversationService, this))));
383
+ log2("result", {
625
384
  messages: messages.length,
626
- duration: Date.now() - start
385
+ duration: session.duration,
386
+ toolCalls: session.toolCalls
627
387
  }, {
628
- F: __dxlog_file4,
629
- L: 121,
388
+ F: __dxlog_file2,
389
+ L: 132,
630
390
  S: this,
631
391
  C: (f, a) => f(...a)
632
392
  });
633
- yield* Effect6.promise(() => this._queue.append(messages));
393
+ yield* Effect2.promise(() => this._queue.append(messages));
634
394
  return messages;
635
- }).pipe(Effect6.withSpan("AiConversation.request"));
395
+ });
636
396
  }
637
397
  };
398
+ var AiConversationService = class _AiConversationService extends Context2.Tag("@dxos/assistant/AiConversationService")() {
399
+ /**
400
+ * Create a new conversation layer from options.
401
+ */
402
+ static layer = (options) => aiContextFromConversation.pipe(Layer.provideMerge(Layer.scoped(_AiConversationService, Effect2.gen(function* () {
403
+ const conversation = yield* acquireReleaseResource(() => new AiConversation(options));
404
+ return conversation;
405
+ }))));
406
+ /**
407
+ * Create a new conversation with a new queue.
408
+ */
409
+ static layerNewQueue = (options) => Layer.unwrapScoped(Effect2.gen(function* () {
410
+ return _AiConversationService.layer({
411
+ ...options,
412
+ queue: yield* QueueService.createQueue()
413
+ });
414
+ }));
415
+ /**
416
+ * Run a prompt in the current conversation.
417
+ */
418
+ static run = (params) => Effect2.gen(function* () {
419
+ const conversation = yield* _AiConversationService;
420
+ return yield* conversation.createRequest(params);
421
+ });
422
+ };
423
+ var aiContextFromConversation = Layer.effect(AiContextService, Effect2.gen(function* () {
424
+ const conversation = yield* AiConversationService;
425
+ return {
426
+ binder: conversation.context
427
+ };
428
+ }));
638
429
 
639
430
  // src/diff.ts
640
431
  import { next as A } from "@automerge/automerge";
641
432
  import { DocAccessor, toCursorRange } from "@dxos/echo-db";
642
- import { log as log4 } from "@dxos/log";
433
+ import { log as log3 } from "@dxos/log";
643
434
  import { isNonNullable } from "@dxos/util";
644
- var __dxlog_file5 = "/__w/dxos/dxos/packages/core/assistant/src/diff.ts";
435
+ var __dxlog_file3 = "/__w/dxos/dxos/packages/core/assistant/src/diff.ts";
645
436
  var reduceDiffs = (diffs) => {
646
437
  return diffs.reduce((acc, diff) => {
647
438
  const current = acc[acc.length - 1];
@@ -669,8 +460,10 @@ var reduceDiffs = (diffs) => {
669
460
  }
670
461
  return acc;
671
462
  }
463
+ default: {
464
+ throw new Error(`Invalid diff format: each line must start with "- " or "+ ", got: ${JSON.stringify(diff)}`);
465
+ }
672
466
  }
673
- return acc;
674
467
  }, []);
675
468
  };
676
469
  var computeDiffsWithCursors = (accessor, diffs) => {
@@ -685,7 +478,7 @@ var computeDiffsWithCursors = (accessor, diffs) => {
685
478
  }
686
479
  }).filter(isNonNullable);
687
480
  };
688
- var applyDiffs = (accessor, diffs) => {
481
+ var applyDiffs = (accessor, diffs, { errorOnNotFound = false } = {}) => {
689
482
  for (const diff of reduceDiffs(diffs)) {
690
483
  accessor.handle.change((doc) => {
691
484
  const text = DocAccessor.getValue(accessor);
@@ -693,12 +486,16 @@ var applyDiffs = (accessor, diffs) => {
693
486
  if (idx !== -1) {
694
487
  A.splice(doc, accessor.path, idx, diff.match.length, diff.replace);
695
488
  } else {
696
- log4.warn("diff not found", diff, {
697
- F: __dxlog_file5,
698
- L: 78,
699
- S: void 0,
700
- C: (f, a) => f(...a)
701
- });
489
+ if (errorOnNotFound) {
490
+ throw new Error(`Diff not found: ${diff.match}`);
491
+ } else {
492
+ log3.warn("diff not found", diff, {
493
+ F: __dxlog_file3,
494
+ L: 86,
495
+ S: void 0,
496
+ C: (f, a) => f(...a)
497
+ });
498
+ }
702
499
  }
703
500
  });
704
501
  }
@@ -707,10 +504,10 @@ var applyDiffs = (accessor, diffs) => {
707
504
 
708
505
  // src/execution-graph.ts
709
506
  import { AgentStatus } from "@dxos/ai";
710
- import { Obj as Obj5 } from "@dxos/echo";
507
+ import { Obj as Obj3 } from "@dxos/echo";
711
508
  import { MESSAGE_PROPERTY_TOOL_CALL_ID } from "@dxos/functions";
712
509
  import { LogLevel } from "@dxos/log";
713
- import { ContentBlock, Message as Message4 } from "@dxos/types";
510
+ import { ContentBlock, Message as Message2 } from "@dxos/types";
714
511
  import { isNonNullable as isNonNullable2 } from "@dxos/util";
715
512
  var SKIP_BLOCKS = [
716
513
  "text"
@@ -734,9 +531,9 @@ var ExecutionGraph = class {
734
531
  addEvents(events) {
735
532
  const sortedEvents = this.sortObjectsByCreated(events);
736
533
  for (const event of sortedEvents) {
737
- if (Obj5.instanceOf(Message4.Message, event)) {
534
+ if (Obj3.instanceOf(Message2.Message, event)) {
738
535
  this._processMessage(event);
739
- } else if (Obj5.instanceOf(AgentStatus, event)) {
536
+ } else if (Obj3.instanceOf(AgentStatus, event)) {
740
537
  this._processAgentStatus(event);
741
538
  }
742
539
  }
@@ -1072,142 +869,26 @@ var stringifyRef = (ref) => {
1072
869
  return ref.dxn.asEchoDXN()?.echoId ?? ref.dxn.asQueueDXN()?.objectId ?? "";
1073
870
  };
1074
871
  var stringifyObject = (obj) => {
1075
- return Obj5.getLabel(obj) ?? Obj5.getTypename(obj) ?? obj.id;
1076
- };
1077
-
1078
- // src/functions/services.ts
1079
- import * as Tool from "@effect/ai/Tool";
1080
- import * as Context3 from "effect/Context";
1081
- import * as Effect7 from "effect/Effect";
1082
- import * as Layer2 from "effect/Layer";
1083
- import * as Record from "effect/Record";
1084
- import * as Schema3 from "effect/Schema";
1085
- import { AiToolNotFoundError, ToolExecutionService as ToolExecutionService2, ToolResolverService as ToolResolverService2 } from "@dxos/ai";
1086
- import { todo as todo2 } from "@dxos/debug";
1087
- import { Query as Query2 } from "@dxos/echo";
1088
- import { DatabaseService as DatabaseService3 } from "@dxos/echo-db";
1089
- import { Function as Function4, FunctionDefinition, FunctionInvocationService } from "@dxos/functions";
1090
- import { invariant as invariant2 } from "@dxos/invariant";
1091
- var __dxlog_file6 = "/__w/dxos/dxos/packages/core/assistant/src/functions/services.ts";
1092
- var makeToolResolverFromFunctions = (functions, toolkit) => {
1093
- return Layer2.effect(ToolResolverService2, Effect7.gen(function* () {
1094
- const dbService = yield* DatabaseService3;
1095
- return {
1096
- resolve: (id) => Effect7.gen(function* () {
1097
- const tool = toolkit.tools[id];
1098
- if (tool) {
1099
- return tool;
1100
- }
1101
- const { objects: [dbFunction] } = yield* DatabaseService3.runQuery(Query2.type(Function4.Function, {
1102
- key: id
1103
- }));
1104
- const functionDef = dbFunction ? FunctionDefinition.deserialize(dbFunction) : functions.find((fn2) => fn2.key === id);
1105
- if (!functionDef) {
1106
- return yield* Effect7.fail(new AiToolNotFoundError(id));
1107
- }
1108
- return projectFunctionToTool(functionDef);
1109
- }).pipe(Effect7.provideService(DatabaseService3, dbService))
1110
- };
1111
- }));
1112
- };
1113
- var makeToolExecutionServiceFromFunctions = (toolkit, handlersLayer) => {
1114
- return Layer2.effect(ToolExecutionService2, Effect7.gen(function* () {
1115
- const toolkitHandler = yield* toolkit.pipe(Effect7.provide(handlersLayer));
1116
- invariant2(isHandlerLike(toolkitHandler), void 0, {
1117
- F: __dxlog_file6,
1118
- L: 74,
1119
- S: this,
1120
- A: [
1121
- "isHandlerLike(toolkitHandler)",
1122
- ""
1123
- ]
1124
- });
1125
- const functionInvocationService = yield* FunctionInvocationService;
1126
- return {
1127
- handlersFor: (toolkit2) => {
1128
- const makeHandler = (tool) => {
1129
- return Effect7.fn(`toolFunctionHandler ${tool.name}`)(function* (input) {
1130
- if (toolkitHandler.tools[tool.name]) {
1131
- if (Tool.isProviderDefined(tool)) {
1132
- throw new Error("Attempted to call a provider-defined tool");
1133
- }
1134
- const { result } = yield* toolkitHandler.handle(tool.name, input);
1135
- return result;
1136
- }
1137
- const { definition: functionDef } = Context3.get(FunctionToolAnnotation)(tool.annotations);
1138
- return yield* functionInvocationService.invokeFunction(functionDef, input).pipe(Effect7.catchAllDefect((defect) => Effect7.fail(defect)));
1139
- });
1140
- };
1141
- return toolkit2.of(Record.map(toolkit2.tools, (tool, _name) => Tool.isUserDefined(tool) ? makeHandler(tool) : null));
1142
- }
1143
- };
1144
- }));
1145
- };
1146
- var FunctionToolAnnotation = class extends Context3.Tag("@dxos/assistant/FunctionToolAnnotation")() {
1147
- };
1148
- var toolCache = /* @__PURE__ */ new WeakMap();
1149
- var projectFunctionToTool = (fn2) => {
1150
- if (toolCache.has(fn2)) {
1151
- return toolCache.get(fn2);
1152
- }
1153
- const tool = Tool.make(makeToolName(fn2.name), {
1154
- description: fn2.description,
1155
- parameters: createStructFieldsFromSchema(fn2.inputSchema),
1156
- // TODO(dmaretskyi): Include output schema.
1157
- failure: Schema3.Any
1158
- }).annotate(FunctionToolAnnotation, {
1159
- definition: fn2
1160
- });
1161
- toolCache.set(fn2, tool);
1162
- return tool;
1163
- };
1164
- var makeToolName = (name) => {
1165
- const toolName = name.toLowerCase().replace(/[^a-zA-Z0-9]/g, "-");
1166
- invariant2(toolName.match(/^[a-z_][a-z0-9-_]*$/), void 0, {
1167
- F: __dxlog_file6,
1168
- L: 144,
1169
- S: void 0,
1170
- A: [
1171
- "toolName.match(/^[a-z_][a-z0-9-_]*$/)",
1172
- ""
1173
- ]
1174
- });
1175
- return toolName;
1176
- };
1177
- var createStructFieldsFromSchema = (schema) => {
1178
- switch (schema.ast._tag) {
1179
- case "TypeLiteral":
1180
- return Object.fromEntries(schema.ast.propertySignatures.map((prop) => [
1181
- prop.name,
1182
- Schema3.make(prop.type)
1183
- ]));
1184
- case "VoidKeyword":
1185
- return {};
1186
- default:
1187
- return todo2(`Unsupported schema AST: ${schema.ast._tag}`);
1188
- }
1189
- };
1190
- var isHandlerLike = (value) => {
1191
- return typeof value.tools === "object" && typeof value.handle === "function";
872
+ return Obj3.getLabel(obj) ?? Obj3.getTypename(obj) ?? obj.id;
1192
873
  };
1193
874
 
1194
875
  // src/templates/system.ts
1195
876
  import defaultsDeep from "lodash.defaultsdeep";
1196
- import { Template as Template2 } from "@dxos/blueprints";
877
+ import { Template } from "@dxos/blueprints";
1197
878
 
1198
879
  // raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/instructions/format.tpl?raw
1199
- 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";
880
+ 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";
1200
881
 
1201
882
  // raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/instructions/system.tpl?raw
1202
883
  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";
1203
884
 
1204
885
  // src/templates/system.ts
1205
- var createSystemPrompt = (options = {}) => Template2.process(format_default, defaultsDeep(options, {
886
+ var createSystemPrompt = (options = {}) => Template.process(format_default, defaultsDeep(options, {
1206
887
  DATETIME: (/* @__PURE__ */ new Date()).toISOString()
1207
888
  }));
1208
889
  var templates = {
1209
890
  /** Editable system prompt.*/
1210
- system: Template2.make({
891
+ system: Template.make({
1211
892
  /**
1212
893
  * NOTE: We are forbidden from accessing randomness at model level in cloudflare, so we have to have pre-generated ids.
1213
894
  */
@@ -1215,58 +896,12 @@ var templates = {
1215
896
  source: system_default
1216
897
  })
1217
898
  };
1218
-
1219
- // src/types/artifact.ts
1220
- import * as Schema4 from "effect/Schema";
1221
- import { DatabaseService as DatabaseService4 } from "@dxos/echo-db";
1222
- import { DXN as DXN2, LOCAL_SPACE_TAG } from "@dxos/keys";
1223
- import { trim as trim2 } from "@dxos/util";
1224
- var createArtifactElement = (id) => `<artifact id=${id} />`;
1225
- var ArtifactId = class extends Schema4.String.annotations({
1226
- // TODO(dmaretskyi): This section gets overriden.
1227
- description: trim2`
1228
- The ID of the referenced object. Formats accepted:
1229
- - DXN (dxn:echo:@:XXXXX). DXNs can be prepended with an @ symbol for compatibility with in-text references.
1230
- - space ID, object ID tuple (spaceID:objectID)
1231
- - Only object ID that is assumed to be in the current space (XXXXX)
1232
- `,
1233
- examples: [
1234
- "dxn:echo:@:XXXXX",
1235
- "@dxn:echo:@:XXXXX",
1236
- "spaceID:objectID",
1237
- "XXXXX"
1238
- ]
1239
- }) {
1240
- static toDXN(reference, owningSpaceId) {
1241
- if (reference.startsWith("@dxn:")) {
1242
- return DXN2.parse(reference.slice(1));
1243
- } else if (reference.startsWith("dxn:")) {
1244
- return DXN2.parse(reference);
1245
- } else if (/^[A-Z0-9]+:[A-Z0-9]+$/.test(reference)) {
1246
- const [spaceId, objectId] = reference.split(":");
1247
- return new DXN2(DXN2.kind.ECHO, [
1248
- spaceId === owningSpaceId ? LOCAL_SPACE_TAG : spaceId,
1249
- objectId
1250
- ]);
1251
- } else if (/^[A-Z0-9]+$/.test(reference)) {
1252
- return DXN2.fromLocalObjectId(reference);
1253
- } else {
1254
- throw new Error(`Unable to parse object reference: ${reference}`);
1255
- }
1256
- }
1257
- /**
1258
- * Resolves an artifact ID to an object.
1259
- */
1260
- static resolve(schema, ref) {
1261
- const dxn = ArtifactId.toDXN(ref);
1262
- return DatabaseService4.resolve(dxn, schema);
1263
- }
1264
- };
1265
899
  export {
1266
900
  AiAssistantError,
1267
901
  AiContextBinder,
1268
902
  AiContextService,
1269
903
  AiConversation,
904
+ AiConversationService,
1270
905
  AiModelError,
1271
906
  AiSession,
1272
907
  ArtifactDiffResolver,
@@ -1276,12 +911,16 @@ export {
1276
911
  ExecutionGraph,
1277
912
  GenerationObserver,
1278
913
  GenericToolkit_exports as GenericToolkit,
914
+ RefFromLLM,
915
+ ToolExecutionServices,
1279
916
  VersionPin,
1280
917
  applyDiffs,
1281
918
  computeDiffsWithCursors,
1282
919
  createArtifactElement,
1283
920
  createSystemPrompt,
1284
921
  createToolkit,
922
+ formatSystemPrompt,
923
+ formatUserPrompt,
1285
924
  makeToolExecutionServiceFromFunctions,
1286
925
  makeToolResolverFromFunctions,
1287
926
  reduceDiffs,