@dxos/assistant 0.8.4-main.67995b8 → 0.8.4-main.6fa680abb7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (196) hide show
  1. package/README.md +1 -0
  2. package/dist/lib/neutral/chunk-BE6SRJ4N.mjs +193 -0
  3. package/dist/lib/neutral/chunk-BE6SRJ4N.mjs.map +7 -0
  4. package/dist/lib/neutral/extraction/index.mjs +223 -0
  5. package/dist/lib/neutral/extraction/index.mjs.map +7 -0
  6. package/dist/lib/neutral/index.mjs +1328 -0
  7. package/dist/lib/neutral/index.mjs.map +7 -0
  8. package/dist/lib/neutral/meta.json +1 -0
  9. package/dist/lib/neutral/testing/index.mjs +38 -0
  10. package/dist/lib/neutral/testing/index.mjs.map +7 -0
  11. package/dist/types/src/conversation/context.d.ts +146 -0
  12. package/dist/types/src/conversation/context.d.ts.map +1 -0
  13. package/dist/types/src/conversation/context.test.d.ts +2 -0
  14. package/dist/types/src/conversation/context.test.d.ts.map +1 -0
  15. package/dist/types/src/conversation/conversation.d.ts +53 -25
  16. package/dist/types/src/conversation/conversation.d.ts.map +1 -1
  17. package/dist/types/src/conversation/conversation.test.d.ts +2 -0
  18. package/dist/types/src/conversation/conversation.test.d.ts.map +1 -0
  19. package/dist/types/src/conversation/index.d.ts +1 -0
  20. package/dist/types/src/conversation/index.d.ts.map +1 -1
  21. package/dist/types/src/diff.d.ts +17 -0
  22. package/dist/types/src/diff.d.ts.map +1 -0
  23. package/dist/types/src/diff.test.d.ts +2 -0
  24. package/dist/types/src/diff.test.d.ts.map +1 -0
  25. package/dist/types/src/errors.d.ts +47 -18
  26. package/dist/types/src/errors.d.ts.map +1 -1
  27. package/dist/types/src/execution-graph.d.ts +85 -0
  28. package/dist/types/src/execution-graph.d.ts.map +1 -0
  29. package/dist/types/src/execution-graph.test.d.ts +2 -0
  30. package/dist/types/src/execution-graph.test.d.ts.map +1 -0
  31. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +1 -1
  32. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -1
  33. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +290 -237
  34. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
  35. package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts.map +1 -1
  36. package/dist/types/src/extraction/extraction.d.ts +349 -270
  37. package/dist/types/src/extraction/extraction.d.ts.map +1 -1
  38. package/dist/types/src/extraction/quotes.d.ts +3 -3
  39. package/dist/types/src/extraction/quotes.d.ts.map +1 -1
  40. package/dist/types/src/functions/services.d.ts +17 -4
  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/index.d.ts +3 -2
  45. package/dist/types/src/index.d.ts.map +1 -1
  46. package/dist/types/src/session/artifact-diff.d.ts +27 -0
  47. package/dist/types/src/session/artifact-diff.d.ts.map +1 -0
  48. package/dist/types/src/session/error-handling.d.ts +4 -0
  49. package/dist/types/src/session/error-handling.d.ts.map +1 -0
  50. package/dist/types/src/session/format.d.ts +12 -0
  51. package/dist/types/src/session/format.d.ts.map +1 -0
  52. package/dist/types/src/session/format.test.d.ts +2 -0
  53. package/dist/types/src/session/format.test.d.ts.map +1 -0
  54. package/dist/types/src/session/index.d.ts +4 -0
  55. package/dist/types/src/session/index.d.ts.map +1 -1
  56. package/dist/types/src/session/observer.d.ts +23 -0
  57. package/dist/types/src/session/observer.d.ts.map +1 -0
  58. package/dist/types/src/session/session.d.ts +41 -240
  59. package/dist/types/src/session/session.d.ts.map +1 -1
  60. package/dist/types/src/session/toolkit.d.ts +18 -0
  61. package/dist/types/src/session/toolkit.d.ts.map +1 -0
  62. package/dist/types/src/session/version-pin.d.ts +4 -4
  63. package/dist/types/src/session/version-pin.d.ts.map +1 -1
  64. package/dist/types/src/templates/index.d.ts +1 -1
  65. package/dist/types/src/templates/system.d.ts +18 -0
  66. package/dist/types/src/templates/system.d.ts.map +1 -0
  67. package/dist/types/src/testing/index.d.ts +1 -2
  68. package/dist/types/src/testing/index.d.ts.map +1 -1
  69. package/dist/types/src/testing/layer.d.ts +22 -0
  70. package/dist/types/src/testing/layer.d.ts.map +1 -0
  71. package/dist/types/src/types/artifact.d.ts +28 -0
  72. package/dist/types/src/types/artifact.d.ts.map +1 -0
  73. package/dist/types/src/types/index.d.ts +2 -0
  74. package/dist/types/src/types/index.d.ts.map +1 -0
  75. package/dist/types/tsconfig.tsbuildinfo +1 -1
  76. package/package.json +44 -39
  77. package/src/conversation/context.test.ts +46 -0
  78. package/src/conversation/context.ts +330 -0
  79. package/src/conversation/conversation.test.ts +65 -0
  80. package/src/conversation/conversation.ts +214 -63
  81. package/src/conversation/index.ts +1 -0
  82. package/src/diff.test.ts +74 -0
  83. package/src/diff.ts +93 -0
  84. package/src/errors.ts +6 -1
  85. package/src/execution-graph.test.ts +740 -0
  86. package/src/execution-graph.ts +509 -0
  87. package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +10 -9
  88. package/src/extraction/extraction-llm-function/extraction-llm-function.ts +48 -56
  89. package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +23 -22
  90. package/src/extraction/extraction-ner-function/extraction-ner-function.ts +42 -43
  91. package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +12 -6
  92. package/src/extraction/extraction-ner-function/named-entity-recognition.ts +2 -0
  93. package/src/extraction/extraction.ts +10 -9
  94. package/src/extraction/quotes.test.ts +3 -3
  95. package/src/extraction/quotes.ts +6 -7
  96. package/src/functions/services.ts +157 -47
  97. package/src/functions.conversations.json +1 -0
  98. package/src/functions.test.ts +74 -0
  99. package/src/index.ts +3 -2
  100. package/src/session/artifact-diff.ts +41 -0
  101. package/src/session/error-handling.ts +41 -0
  102. package/src/session/format.test.ts +124 -0
  103. package/src/session/format.ts +143 -0
  104. package/src/session/index.ts +4 -0
  105. package/src/session/observer.ts +62 -0
  106. package/src/session/session.conversations.json +1 -0
  107. package/src/session/session.test.ts +82 -201
  108. package/src/session/session.ts +176 -261
  109. package/src/session/toolkit.ts +44 -0
  110. package/src/session/version-pin.ts +8 -8
  111. package/src/templates/index.ts +1 -1
  112. package/src/templates/instructions/format.tpl +57 -0
  113. package/src/templates/instructions/system.tpl +48 -0
  114. package/src/templates/system.ts +41 -0
  115. package/src/testing/index.ts +2 -8
  116. package/src/testing/layer.ts +85 -0
  117. package/src/types/artifact.ts +89 -0
  118. package/src/types/index.ts +5 -0
  119. package/src/typings.d.ts +5 -0
  120. package/dist/lib/browser/chunk-6WRGDEMH.mjs +0 -89
  121. package/dist/lib/browser/chunk-6WRGDEMH.mjs.map +0 -7
  122. package/dist/lib/browser/chunk-SIXTNSGV.mjs +0 -250
  123. package/dist/lib/browser/chunk-SIXTNSGV.mjs.map +0 -7
  124. package/dist/lib/browser/extraction/index.mjs +0 -269
  125. package/dist/lib/browser/extraction/index.mjs.map +0 -7
  126. package/dist/lib/browser/index.mjs +0 -1214
  127. package/dist/lib/browser/index.mjs.map +0 -7
  128. package/dist/lib/browser/meta.json +0 -1
  129. package/dist/lib/browser/named-entity-recognition-O46UTCLR.mjs +0 -13
  130. package/dist/lib/browser/named-entity-recognition-O46UTCLR.mjs.map +0 -7
  131. package/dist/lib/node-esm/chunk-3UU5V4FS.mjs +0 -91
  132. package/dist/lib/node-esm/chunk-3UU5V4FS.mjs.map +0 -7
  133. package/dist/lib/node-esm/chunk-FD5NXKDP.mjs +0 -252
  134. package/dist/lib/node-esm/chunk-FD5NXKDP.mjs.map +0 -7
  135. package/dist/lib/node-esm/extraction/index.mjs +0 -270
  136. package/dist/lib/node-esm/extraction/index.mjs.map +0 -7
  137. package/dist/lib/node-esm/index.mjs +0 -1215
  138. package/dist/lib/node-esm/index.mjs.map +0 -7
  139. package/dist/lib/node-esm/meta.json +0 -1
  140. package/dist/lib/node-esm/named-entity-recognition-W4VMRCH6.mjs +0 -14
  141. package/dist/lib/node-esm/named-entity-recognition-W4VMRCH6.mjs.map +0 -7
  142. package/dist/types/bin/chat.d.ts +0 -2
  143. package/dist/types/bin/chat.d.ts.map +0 -1
  144. package/dist/types/src/context/binder.d.ts +0 -34
  145. package/dist/types/src/context/binder.d.ts.map +0 -1
  146. package/dist/types/src/context/binding.d.ts +0 -54
  147. package/dist/types/src/context/binding.d.ts.map +0 -1
  148. package/dist/types/src/context/index.d.ts +0 -3
  149. package/dist/types/src/context/index.d.ts.map +0 -1
  150. package/dist/types/src/research/exa.d.ts +0 -12
  151. package/dist/types/src/research/exa.d.ts.map +0 -1
  152. package/dist/types/src/research/graph.d.ts +0 -51
  153. package/dist/types/src/research/graph.d.ts.map +0 -1
  154. package/dist/types/src/research/graph.test.d.ts +0 -2
  155. package/dist/types/src/research/graph.test.d.ts.map +0 -1
  156. package/dist/types/src/research/index.d.ts +0 -4
  157. package/dist/types/src/research/index.d.ts.map +0 -1
  158. package/dist/types/src/research/research.d.ts +0 -10
  159. package/dist/types/src/research/research.d.ts.map +0 -1
  160. package/dist/types/src/research/research.test.d.ts +0 -2
  161. package/dist/types/src/research/research.test.d.ts.map +0 -1
  162. package/dist/types/src/session/version-pin.test.d.ts +0 -2
  163. package/dist/types/src/session/version-pin.test.d.ts.map +0 -1
  164. package/dist/types/src/summarization/summarization.test.d.ts +0 -2
  165. package/dist/types/src/summarization/summarization.test.d.ts.map +0 -1
  166. package/dist/types/src/templates/prompt.d.ts +0 -30
  167. package/dist/types/src/templates/prompt.d.ts.map +0 -1
  168. package/dist/types/src/testing/exa-search-1748337321991.d.ts +0 -38
  169. package/dist/types/src/testing/exa-search-1748337321991.d.ts.map +0 -1
  170. package/dist/types/src/testing/exa-search-1748337331526.d.ts +0 -37
  171. package/dist/types/src/testing/exa-search-1748337331526.d.ts.map +0 -1
  172. package/dist/types/src/testing/exa-search-1748337344119.d.ts +0 -58
  173. package/dist/types/src/testing/exa-search-1748337344119.d.ts.map +0 -1
  174. package/dist/types/src/types.d.ts +0 -13
  175. package/dist/types/src/types.d.ts.map +0 -1
  176. package/src/context/binder.ts +0 -103
  177. package/src/context/binding.ts +0 -32
  178. package/src/context/index.ts +0 -6
  179. package/src/research/exa.ts +0 -97
  180. package/src/research/graph.test.ts +0 -64
  181. package/src/research/graph.ts +0 -369
  182. package/src/research/index.ts +0 -7
  183. package/src/research/instructions-research.tpl +0 -98
  184. package/src/research/research.test.ts +0 -190
  185. package/src/research/research.ts +0 -87
  186. package/src/session/__snapshots__/session.test.ts.snap +0 -32
  187. package/src/session/version-pin.test.ts +0 -7
  188. package/src/summarization/summarization.test.ts +0 -199
  189. package/src/templates/assistant.tpl +0 -111
  190. package/src/templates/prompt.ts +0 -45
  191. package/src/testing/exa-search-1748337321991.ts +0 -131
  192. package/src/testing/exa-search-1748337331526.ts +0 -144
  193. package/src/testing/exa-search-1748337344119.ts +0 -133
  194. package/src/typedefs.d.ts +0 -8
  195. package/src/types.ts +0 -47
  196. /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
@@ -0,0 +1,1328 @@
1
+ import {
2
+ ArtifactId,
3
+ RefFromLLM,
4
+ ToolExecutionServices,
5
+ createArtifactElement,
6
+ makeToolExecutionServiceFromFunctions,
7
+ makeToolResolverFromFunctions
8
+ } from "./chunk-BE6SRJ4N.mjs";
9
+
10
+ // src/conversation/context.ts
11
+ import { Atom, Registry } from "@effect-atom/atom-react";
12
+ import * as EArray from "effect/Array";
13
+ import * as Context from "effect/Context";
14
+ import * as Effect from "effect/Effect";
15
+ import * as Function from "effect/Function";
16
+ import * as Schema from "effect/Schema";
17
+ import { Blueprint } from "@dxos/blueprints";
18
+ import { Resource } from "@dxos/context";
19
+ import { DXN, Obj, Query, Ref, Type } from "@dxos/echo";
20
+ import { assertArgument } from "@dxos/invariant";
21
+ import { log } from "@dxos/log";
22
+ import { ComplexSet, isTruthy } from "@dxos/util";
23
+ var __dxlog_file = "/__w/dxos/dxos/packages/core/assistant/src/conversation/context.ts";
24
+ var ContextBinding = Schema.Struct({
25
+ blueprints: Schema.Struct({
26
+ added: Schema.Array(Ref.Ref(Blueprint.Blueprint)),
27
+ removed: Schema.Array(Ref.Ref(Blueprint.Blueprint))
28
+ }),
29
+ objects: Schema.Struct({
30
+ added: Schema.Array(Ref.Ref(Obj.Unknown)),
31
+ removed: Schema.Array(Ref.Ref(Obj.Unknown))
32
+ })
33
+ }).pipe(Type.object({
34
+ typename: "org.dxos.type.context-binding",
35
+ version: "0.1.0"
36
+ }));
37
+ var Bindings = class {
38
+ blueprints = new ComplexSet((ref) => ref.dxn.toString());
39
+ // TODO(burdon): Some DXNs have the Space prefix so only compare the object ID.
40
+ objects = new ComplexSet((ref) => ref.dxn.asEchoDXN()?.echoId);
41
+ toJSON() {
42
+ return {
43
+ blueprints: EArray.fromIterable(this.blueprints).map((ref) => ref.dxn.toString()),
44
+ objects: EArray.fromIterable(this.objects).map((ref) => ref.dxn.toString())
45
+ };
46
+ }
47
+ };
48
+ var AiContextBinder = class extends Resource {
49
+ _blueprints = Atom.make([]).pipe(Atom.keepAlive);
50
+ _objects = Atom.make([]).pipe(Atom.keepAlive);
51
+ _registry;
52
+ _queue;
53
+ constructor(options) {
54
+ super();
55
+ assertArgument(options.queue, "options.queue", "Queue is required");
56
+ this._queue = options.queue;
57
+ this._registry = options.registry ?? Registry.make();
58
+ }
59
+ /**
60
+ * Returns the blueprints atom for subscription.
61
+ */
62
+ get blueprints() {
63
+ return this._blueprints;
64
+ }
65
+ /**
66
+ * Returns the objects atom for subscription.
67
+ */
68
+ get objects() {
69
+ return this._objects;
70
+ }
71
+ /**
72
+ * Gets the current blueprints value.
73
+ */
74
+ getBlueprints() {
75
+ return this._registry.get(this._blueprints);
76
+ }
77
+ /**
78
+ * Gets the current objects value.
79
+ */
80
+ getObjects() {
81
+ return this._registry.get(this._objects);
82
+ }
83
+ /**
84
+ * Subscribe to changes in blueprints.
85
+ */
86
+ subscribeBlueprints(cb) {
87
+ return this._registry.subscribe(this._blueprints, () => cb(this._registry.get(this._blueprints)));
88
+ }
89
+ /**
90
+ * Subscribe to changes in objects.
91
+ */
92
+ subscribeObjects(cb) {
93
+ return this._registry.subscribe(this._objects, () => cb(this._registry.get(this._objects)));
94
+ }
95
+ async _open() {
96
+ const query = this._queue.query(Query.type(ContextBinding));
97
+ const initialResults = await query.run();
98
+ await this._updateBindings(initialResults);
99
+ this._ctx.onDispose(query.subscribe(async () => {
100
+ await this._updateBindings(query.results);
101
+ }));
102
+ }
103
+ async _updateBindings(items) {
104
+ if (items.length === 0) {
105
+ return;
106
+ }
107
+ const bindings = this._reduce(items);
108
+ const currentBlueprints = this._registry.get(this._blueprints);
109
+ const currentObjects = this._registry.get(this._objects);
110
+ const newBlueprints = await this._resolve(bindings.blueprints, currentBlueprints);
111
+ const newObjects = await this._resolve(bindings.objects, currentObjects);
112
+ this._registry.set(this._blueprints, newBlueprints);
113
+ this._registry.set(this._objects, newObjects);
114
+ log("updated bindings", {
115
+ blueprints: newBlueprints.length,
116
+ objects: newObjects.length
117
+ }, {
118
+ F: __dxlog_file,
119
+ L: 159,
120
+ S: this,
121
+ C: (f, a) => f(...a)
122
+ });
123
+ }
124
+ async _close() {
125
+ this._registry.set(this._blueprints, []);
126
+ this._registry.set(this._objects, []);
127
+ }
128
+ async bind({ blueprints, objects }) {
129
+ const currentBlueprints = this._registry.get(this._blueprints);
130
+ const currentObjects = this._registry.get(this._objects);
131
+ const { added: addedBlueprints, next: nextBlueprints } = this._processBindings(blueprints, currentBlueprints);
132
+ const { added: addedObjects, next: nextObjects } = this._processBindings(objects, currentObjects);
133
+ if (!addedBlueprints.length && !addedObjects.length) {
134
+ return;
135
+ }
136
+ this._registry.set(this._blueprints, nextBlueprints);
137
+ this._registry.set(this._objects, nextObjects);
138
+ log("bind", {
139
+ blueprints: addedBlueprints.length,
140
+ objects: addedObjects.length
141
+ }, {
142
+ F: __dxlog_file,
143
+ L: 185,
144
+ S: this,
145
+ C: (f, a) => f(...a)
146
+ });
147
+ await this._queue.append([
148
+ Obj.make(ContextBinding, {
149
+ blueprints: {
150
+ added: addedBlueprints,
151
+ removed: []
152
+ },
153
+ objects: {
154
+ added: addedObjects,
155
+ removed: []
156
+ }
157
+ })
158
+ ]);
159
+ }
160
+ async unbind({ blueprints, objects }) {
161
+ if (!blueprints?.length && !objects?.length) {
162
+ return;
163
+ }
164
+ log("unbind", {
165
+ blueprints: blueprints?.length,
166
+ objects: objects?.length
167
+ }, {
168
+ F: __dxlog_file,
169
+ L: 205,
170
+ S: this,
171
+ C: (f, a) => f(...a)
172
+ });
173
+ await this._queue.append([
174
+ Obj.make(ContextBinding, {
175
+ blueprints: {
176
+ added: [],
177
+ removed: blueprints ?? []
178
+ },
179
+ objects: {
180
+ added: [],
181
+ removed: objects ?? []
182
+ }
183
+ })
184
+ ]);
185
+ }
186
+ /**
187
+ * Process bindings to filter duplicates and determine next state.
188
+ */
189
+ _processBindings(refs, current) {
190
+ const next = [
191
+ ...current
192
+ ];
193
+ const added = [];
194
+ if (!refs?.length) {
195
+ return {
196
+ added,
197
+ next
198
+ };
199
+ }
200
+ const seen = new Set(current.map((obj) => Obj.getDXN(obj).toString()));
201
+ for (const ref of refs) {
202
+ const dxn = ref.dxn.toString();
203
+ if (!seen.has(dxn)) {
204
+ seen.add(dxn);
205
+ added.push(ref);
206
+ if (ref.isAvailable) {
207
+ const target = ref.target;
208
+ if (target) {
209
+ next.push(target);
210
+ }
211
+ }
212
+ }
213
+ }
214
+ return {
215
+ added,
216
+ next
217
+ };
218
+ }
219
+ /**
220
+ * Reduce results into sets of blueprints and objects.
221
+ */
222
+ _reduce(items) {
223
+ return Function.pipe(items, EArray.reduce(new Bindings(), (context, { blueprints, objects }) => {
224
+ blueprints.added.forEach((ref) => context.blueprints.add(ref));
225
+ blueprints.removed.forEach((ref) => context.blueprints.delete(ref));
226
+ objects.added.forEach((ref) => context.objects.add(ref));
227
+ objects.removed.forEach((ref) => {
228
+ for (const obj of context.objects) {
229
+ if (DXN.equalsEchoId(obj.dxn, ref.dxn)) {
230
+ context.objects.delete(obj);
231
+ }
232
+ }
233
+ });
234
+ return context;
235
+ }));
236
+ }
237
+ /**
238
+ * Resolve references to objects, loading them first if needed and falling back to existing objects.
239
+ */
240
+ async _resolve(refs, current) {
241
+ const refArray = [
242
+ ...refs
243
+ ];
244
+ await Promise.all(refArray.map((ref) => ref.tryLoad()));
245
+ return refArray.map((ref) => {
246
+ let target;
247
+ if (ref.isAvailable) {
248
+ target = ref.target;
249
+ }
250
+ return target ?? current.find((obj) => Obj.getDXN(obj).toString() === ref.dxn.toString());
251
+ }).filter(isTruthy);
252
+ }
253
+ };
254
+ var AiContextService = class _AiContextService extends Context.Tag("@dxos/assistant/AiContextService")() {
255
+ static bindContext = ({ blueprints, objects }) => Effect.gen(function* () {
256
+ const { binder } = yield* _AiContextService;
257
+ yield* Effect.promise(() => binder.bind({
258
+ blueprints,
259
+ objects
260
+ }));
261
+ });
262
+ static findObjects = (type) => {
263
+ return Effect.gen(function* () {
264
+ const { binder } = yield* _AiContextService;
265
+ return binder.getObjects().filter(Obj.instanceOf(type));
266
+ });
267
+ };
268
+ /**
269
+ * Glorified type cast until we figure out function service typing.
270
+ * For context, the AiContextService is provided to functions called from tool calls, but it's not implemented in types yet.
271
+ * @deprecated
272
+ */
273
+ static fixFunctionHandlerType = (eff) => eff;
274
+ };
275
+
276
+ // src/conversation/conversation.ts
277
+ import * as Array3 from "effect/Array";
278
+ import * as Context3 from "effect/Context";
279
+ import * as Effect6 from "effect/Effect";
280
+ import * as Either from "effect/Either";
281
+ import { pipe as pipe3 } from "effect/Function";
282
+ import * as Layer from "effect/Layer";
283
+ import { Resource as Resource2 } from "@dxos/context";
284
+ import { Obj as Obj4 } from "@dxos/echo";
285
+ import { acquireReleaseResource } from "@dxos/effect";
286
+ import { QueueService } from "@dxos/functions";
287
+ import { invariant } from "@dxos/invariant";
288
+ import { log as log4 } from "@dxos/log";
289
+ import { McpToolkit } from "@dxos/mcp-client";
290
+ import { Message as Message3 } from "@dxos/types";
291
+
292
+ // src/session/artifact-diff.ts
293
+ import * as Context2 from "effect/Context";
294
+ var ArtifactDiffResolver = class extends Context2.Tag("@dxos/assistant/ArtifactDiffResolver")() {
295
+ };
296
+
297
+ // src/session/observer.ts
298
+ import * as Effect2 from "effect/Effect";
299
+ import * as Function2 from "effect/Function";
300
+ var GenerationObserver = Object.freeze({
301
+ make: ({ onBegin = Function2.constant(Effect2.void), onPart = Function2.constant(Effect2.void), onBlock = Function2.constant(Effect2.void), onEnd = Function2.constant(Effect2.void), onMessage = Function2.constant(Effect2.void) } = {}) => ({
302
+ onBegin,
303
+ onPart,
304
+ onBlock,
305
+ onEnd,
306
+ onMessage
307
+ }),
308
+ noop: () => GenerationObserver.make(),
309
+ /**
310
+ * Debug printer to be used in unit-tests and browser devtools.
311
+ */
312
+ fromPrinter: (printer) => GenerationObserver.make({
313
+ onBlock: (block) => Effect2.sync(() => {
314
+ if (block.pending) {
315
+ return;
316
+ }
317
+ printer.printContentBlock(block);
318
+ }),
319
+ onMessage: (message) => Effect2.sync(() => {
320
+ if (message.sender.role === "assistant") {
321
+ return;
322
+ }
323
+ printer.printMessage(message);
324
+ })
325
+ })
326
+ });
327
+
328
+ // src/session/session.ts
329
+ import * as LanguageModel from "@effect/ai/LanguageModel";
330
+ import * as Chunk from "effect/Chunk";
331
+ import * as Effect4 from "effect/Effect";
332
+ import * as Stream from "effect/Stream";
333
+ import { AiParser, AiPreprocessor, AiSummarizer, callTool, withoutToolCallParising } from "@dxos/ai";
334
+ import { Obj as Obj3 } from "@dxos/echo";
335
+ import { TracingService } from "@dxos/functions";
336
+ import { log as log3 } from "@dxos/log";
337
+ import { Message as Message2 } from "@dxos/types";
338
+
339
+ // src/session/format.ts
340
+ import * as Effect3 from "effect/Effect";
341
+ import * as Function3 from "effect/Function";
342
+ import * as Option from "effect/Option";
343
+ import { Template } from "@dxos/blueprints";
344
+ import { Obj as Obj2 } from "@dxos/echo";
345
+ import { ObjectVersion } from "@dxos/echo-db";
346
+ import { log as log2 } from "@dxos/log";
347
+ import { Message } from "@dxos/types";
348
+ import { trim } from "@dxos/util";
349
+
350
+ // src/errors.ts
351
+ import { BaseError } from "@dxos/errors";
352
+ var AiModelError = class extends BaseError.extend("AiModelError", "AI model error") {
353
+ };
354
+ var AiAssistantError = class extends BaseError.extend("AiAssistantError", "AI assistant error") {
355
+ };
356
+
357
+ // src/session/format.ts
358
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/core/assistant/src/session/format.ts";
359
+ var formatSystemPrompt = ({ system, blueprints = [], objects = [] }) => Effect3.gen(function* () {
360
+ const blueprintDefs = yield* Function3.pipe(blueprints, Effect3.forEach((blueprint) => Effect3.succeed(blueprint.instructions)), Effect3.flatMap(Effect3.forEach((template) => Effect3.gen(function* () {
361
+ return trim`
362
+ <blueprint>
363
+ ${yield* Template.processTemplate(template)}
364
+ </blueprint>
365
+ `;
366
+ }))), Effect3.map((blueprints2) => blueprints2.length > 0 ? [
367
+ "## Blueprints Definitions",
368
+ ...blueprints2
369
+ ].join("\n\n") : void 0));
370
+ const objectDefs = yield* Function3.pipe(objects, Effect3.forEach((object) => Effect3.succeed(trim`
371
+ <object>
372
+ <dxn>${Obj2.getDXN(object)}</dxn>
373
+ <typename>${Obj2.getTypename(object)}</typename>
374
+ </object>
375
+ `)), Effect3.map((objects2) => objects2.length > 0 ? [
376
+ "## Context Objects",
377
+ ...objects2
378
+ ].join("\n\n") : void 0));
379
+ return yield* Function3.pipe(Effect3.succeed([
380
+ system,
381
+ blueprintDefs,
382
+ objectDefs
383
+ ].filter((def) => def !== void 0)), Effect3.map((parts) => parts.join("\n\n")));
384
+ });
385
+ var formatUserPrompt = ({ prompt, history = [] }) => Effect3.gen(function* () {
386
+ const blocks = [];
387
+ const artifactDiffResolver = yield* Effect3.serviceOption(ArtifactDiffResolver);
388
+ if (Option.isSome(artifactDiffResolver)) {
389
+ const versions = gatherObjectVersions(history);
390
+ const artifactDiff = yield* Effect3.tryPromise({
391
+ try: () => artifactDiffResolver.value.resolve([
392
+ ...versions.entries()
393
+ ].map(([id, version]) => ({
394
+ id,
395
+ lastVersion: version
396
+ }))),
397
+ catch: AiAssistantError.wrap({
398
+ message: "Artifact diff resolution error"
399
+ })
400
+ });
401
+ log2("version", {
402
+ artifactDiff,
403
+ versions
404
+ }, {
405
+ F: __dxlog_file2,
406
+ L: 94,
407
+ S: this,
408
+ C: (f, a) => f(...a)
409
+ });
410
+ for (const [id, { version }] of [
411
+ ...artifactDiff.entries()
412
+ ]) {
413
+ if (ObjectVersion.equals(version, versions.get(id))) {
414
+ artifactDiff.delete(id);
415
+ continue;
416
+ }
417
+ blocks.push({
418
+ _tag: "anchor",
419
+ objectId: id,
420
+ version
421
+ });
422
+ }
423
+ if (artifactDiff.size > 0) {
424
+ blocks.push(createArtifactUpdateBlock(artifactDiff));
425
+ }
426
+ }
427
+ return Obj2.make(Message.Message, {
428
+ created: (/* @__PURE__ */ new Date()).toISOString(),
429
+ sender: {
430
+ role: "user"
431
+ },
432
+ blocks: [
433
+ ...blocks,
434
+ {
435
+ _tag: "text",
436
+ text: prompt
437
+ }
438
+ ]
439
+ });
440
+ });
441
+ var gatherObjectVersions = (messages) => {
442
+ const artifactIds = /* @__PURE__ */ new Map();
443
+ for (const message of messages) {
444
+ for (const block of message.blocks) {
445
+ if (block._tag === "anchor") {
446
+ artifactIds.set(block.objectId, block.version);
447
+ }
448
+ }
449
+ }
450
+ return artifactIds;
451
+ };
452
+ var createArtifactUpdateBlock = (artifactDiff) => {
453
+ return {
454
+ _tag: "text",
455
+ // TODO(dmaretskyi): Does this need to be a special content-block?
456
+ disposition: "artifact-update",
457
+ text: trim`
458
+ The following artifacts have been updated since the last message:
459
+ ${[
460
+ ...artifactDiff.entries()
461
+ ].map(([id, { diff }]) => `<changed-artifact id="${id}">${diff ? `
462
+ ${diff}` : ""}</changed-artifact>`).join("\n")}
463
+ `
464
+ };
465
+ };
466
+
467
+ // src/session/session.ts
468
+ var __dxlog_file3 = "/__w/dxos/dxos/packages/core/assistant/src/session/session.ts";
469
+ var AiSession = class {
470
+ _options;
471
+ /** Prevents concurrent execution of session. */
472
+ _semaphore = Effect4.runSync(Effect4.makeSemaphore(1));
473
+ /** Message history from queue. */
474
+ // TODO(burdon): Evolve into supporting a git-like graph of messages.
475
+ _history = [];
476
+ /** Pending messages for this session (incl. the current prompt). */
477
+ _pending = [];
478
+ _started = 0;
479
+ _ended = 0;
480
+ _toolCalls = 0;
481
+ constructor(_options = {}) {
482
+ this._options = _options;
483
+ }
484
+ get duration() {
485
+ return this._ended - this._started;
486
+ }
487
+ get toolCalls() {
488
+ return this._toolCalls;
489
+ }
490
+ run = ({ prompt, system: systemTemplate, history = [], objects = [], blueprints = [], toolkit, observer = GenerationObserver.noop(), onOutput = () => Effect4.void }) => Effect4.gen(this, function* () {
491
+ this._started = Date.now();
492
+ this._history = [
493
+ ...history
494
+ ];
495
+ this._pending = [];
496
+ const submitMessage = (message) => Effect4.gen(this, function* () {
497
+ this._pending.push(message);
498
+ yield* observer.onMessage(message);
499
+ yield* TracingService.emitConverationMessage(message);
500
+ yield* onOutput(message);
501
+ return message;
502
+ });
503
+ const system = yield* formatSystemPrompt({
504
+ system: systemTemplate,
505
+ blueprints,
506
+ objects
507
+ }).pipe(Effect4.orDie);
508
+ if (this._options.summarizationThreshold !== void 0) {
509
+ const tokenCount = yield* AiPreprocessor.estimateTokens(yield* AiPreprocessor.preprocessPrompt([
510
+ ...this._history
511
+ ], {
512
+ system
513
+ }));
514
+ if (tokenCount > this._options.summarizationThreshold) {
515
+ const summary = yield* AiSummarizer.summarize([
516
+ ...this._history
517
+ ]);
518
+ yield* submitMessage(summary);
519
+ }
520
+ }
521
+ const promptMessage = yield* submitMessage(yield* formatUserPrompt({
522
+ prompt,
523
+ history
524
+ }));
525
+ do {
526
+ log3("request", {
527
+ prompt: promptMessage,
528
+ system: {
529
+ snippet: createSnippet(system),
530
+ length: system.length
531
+ },
532
+ pending: this._pending.length,
533
+ history: this._history.length,
534
+ objects: objects?.length ?? 0
535
+ }, {
536
+ F: __dxlog_file3,
537
+ L: 149,
538
+ S: this,
539
+ C: (f, a) => f(...a)
540
+ });
541
+ const prompt2 = yield* AiPreprocessor.preprocessPrompt([
542
+ ...this._history,
543
+ ...this._pending
544
+ ], {
545
+ system,
546
+ cacheControl: "ephemeral"
547
+ });
548
+ const messages = yield* LanguageModel.streamText({
549
+ prompt: prompt2,
550
+ toolkit,
551
+ disableToolCallResolution: true
552
+ }).pipe(
553
+ // Disable tool call parsing so that we can handle parameter schema errors on a per-tool call basis.
554
+ withoutToolCallParising,
555
+ // TOOD(dmaretskyi): Error mapping.
556
+ AiParser.parseResponse({
557
+ onBegin: () => observer.onBegin(),
558
+ onBlock: (block) => observer.onBlock(block),
559
+ onPart: (part) => observer.onPart(part),
560
+ onEnd: (summary) => observer.onEnd(summary)
561
+ }),
562
+ Stream.mapEffect((block) => Effect4.gen(function* () {
563
+ return yield* submitMessage(Obj3.make(Message2.Message, {
564
+ created: (/* @__PURE__ */ new Date()).toISOString(),
565
+ sender: {
566
+ role: "assistant"
567
+ },
568
+ blocks: [
569
+ block
570
+ ]
571
+ }));
572
+ })),
573
+ Stream.runCollect,
574
+ Effect4.map(Chunk.toArray)
575
+ );
576
+ const toolCalls = messages.flatMap((message) => message.blocks.filter((block) => block._tag === "toolCall" && block.providerExecuted === false).map((block) => ({
577
+ block,
578
+ message
579
+ })));
580
+ if (toolCalls.length === 0) {
581
+ break;
582
+ } else if (!toolkit) {
583
+ throw new Error("No toolkit provided");
584
+ }
585
+ const toolResults = yield* Effect4.forEach(toolCalls, ({ block, message }) => {
586
+ return callTool(toolkit, block).pipe(Effect4.provide(TracingService.layerSubframe((context) => ({
587
+ ...context,
588
+ parentMessage: message.id,
589
+ toolCallId: block.toolCallId
590
+ }))));
591
+ });
592
+ yield* submitMessage(Obj3.make(Message2.Message, {
593
+ created: (/* @__PURE__ */ new Date()).toISOString(),
594
+ sender: {
595
+ role: "tool"
596
+ },
597
+ blocks: toolResults
598
+ }));
599
+ this._toolCalls++;
600
+ } while (true);
601
+ this._ended = Date.now();
602
+ log3("done", {
603
+ pending: this._pending.length,
604
+ duration: this.duration,
605
+ tools: this._toolCalls
606
+ }, {
607
+ F: __dxlog_file3,
608
+ L: 236,
609
+ S: this,
610
+ C: (f, a) => f(...a)
611
+ });
612
+ return this._pending;
613
+ }).pipe(this._semaphore.withPermits(1), Effect4.withSpan("AiSession.run"));
614
+ };
615
+ var createSnippet = (text, len = 32) => text.length <= len * 2 ? text : [
616
+ text.slice(0, len),
617
+ "...",
618
+ text.slice(-len)
619
+ ].join("");
620
+
621
+ // src/session/toolkit.ts
622
+ import * as Toolkit from "@effect/ai/Toolkit";
623
+ import * as Effect5 from "effect/Effect";
624
+ import { GenericToolkit, ToolExecutionService, ToolResolverService } from "@dxos/ai";
625
+ import { isTruthy as isTruthy2 } from "@dxos/util";
626
+ var createToolkit = ({ toolkit: toolkitProp, blueprints = [], genericToolkits = [] }) => Effect5.gen(function* () {
627
+ const blueprintToolkit = yield* ToolResolverService.resolveToolkit(blueprints.flatMap(({ tools }) => tools));
628
+ const blueprintToolHandler = yield* blueprintToolkit.toContext(ToolExecutionService.handlersFor(blueprintToolkit));
629
+ const genericToolkit = GenericToolkit.merge(...genericToolkits);
630
+ const toolkit = Toolkit.merge(...[
631
+ toolkitProp,
632
+ blueprintToolkit,
633
+ genericToolkit.toolkit
634
+ ].filter(isTruthy2));
635
+ return yield* toolkit.pipe(Effect5.provide(blueprintToolHandler), Effect5.provide(genericToolkit.layer));
636
+ });
637
+
638
+ // src/session/version-pin.ts
639
+ import * as Schema2 from "effect/Schema";
640
+ import { getVersion } from "@dxos/echo-db";
641
+ import { ObjectId } from "@dxos/keys";
642
+ var ObjectVersionSchema = Schema2.Unknown;
643
+ var VersionPinSchema = Schema2.Struct({
644
+ // TODO(dmaretskyi): Use Ref when those support encoding.
645
+ objectId: ObjectId,
646
+ // TODO(dmaretskyi): Could be opaque.
647
+ version: ObjectVersionSchema
648
+ });
649
+ var VersionPin = class extends VersionPinSchema {
650
+ static DISPOSITION = "version-pin";
651
+ static fromObject(object) {
652
+ return VersionPin.make({
653
+ objectId: object.id,
654
+ version: getVersion(object)
655
+ });
656
+ }
657
+ static createBlock(pin) {
658
+ return {
659
+ _tag: "json",
660
+ disposition: VersionPin.DISPOSITION,
661
+ data: JSON.stringify(pin)
662
+ };
663
+ }
664
+ };
665
+
666
+ // src/conversation/conversation.ts
667
+ var __dxlog_file4 = "/__w/dxos/dxos/packages/core/assistant/src/conversation/conversation.ts";
668
+ var SUMMARY_THRESHOLD = 8e4;
669
+ var AiConversation = class extends Resource2 {
670
+ /**
671
+ * Blueprints and objects bound to the conversation.
672
+ */
673
+ _binder;
674
+ _queue;
675
+ _toolkit;
676
+ constructor(options) {
677
+ super();
678
+ this._queue = options.queue;
679
+ this._toolkit = options.toolkit;
680
+ invariant(this._queue, void 0, {
681
+ F: __dxlog_file4,
682
+ L: 72,
683
+ S: this,
684
+ A: [
685
+ "this._queue",
686
+ ""
687
+ ]
688
+ });
689
+ this._binder = new AiContextBinder({
690
+ queue: this._queue,
691
+ registry: options.registry
692
+ });
693
+ }
694
+ async _open() {
695
+ await this._binder.open(this._ctx);
696
+ }
697
+ get queue() {
698
+ return this._queue;
699
+ }
700
+ get context() {
701
+ return this._binder;
702
+ }
703
+ get toolkit() {
704
+ return this._toolkit;
705
+ }
706
+ async getHistory() {
707
+ const queueItems = await this._queue.queryObjects();
708
+ return queueItems.filter(Obj4.instanceOf(Message3.Message));
709
+ }
710
+ getTools() {
711
+ return Effect6.gen(this, function* () {
712
+ const blueprints = this.context.getBlueprints();
713
+ const tookit = yield* createToolkit({
714
+ toolkit: this._toolkit,
715
+ blueprints
716
+ });
717
+ return tookit.tools;
718
+ }).pipe(Effect6.orDie);
719
+ }
720
+ /**
721
+ * Creates a new cancelable request effect.
722
+ */
723
+ createRequest(params) {
724
+ return Effect6.gen(this, function* () {
725
+ const history = yield* Effect6.promise(() => this.getHistory());
726
+ const blueprints = this.context.getBlueprints();
727
+ const mcps = yield* connectMcpServers(blueprints);
728
+ const toolkit = yield* createToolkit({
729
+ toolkit: this._toolkit,
730
+ blueprints,
731
+ genericToolkits: mcps
732
+ });
733
+ const objects = this.context.getObjects();
734
+ log4("run", {
735
+ history: history.length,
736
+ blueprints: blueprints.length,
737
+ tools: Object.keys(toolkit.tools).length,
738
+ objects: objects.length
739
+ }, {
740
+ F: __dxlog_file4,
741
+ L: 128,
742
+ S: this,
743
+ C: (f, a) => f(...a)
744
+ });
745
+ const session = new AiSession({
746
+ summarizationThreshold: SUMMARY_THRESHOLD
747
+ });
748
+ const messages = yield* session.run({
749
+ history,
750
+ blueprints,
751
+ toolkit,
752
+ objects,
753
+ ...params,
754
+ onOutput: (message) => Effect6.promise(() => this._queue.append([
755
+ message
756
+ ]))
757
+ }).pipe(Effect6.provide(Layer.mergeAll(Layer.succeed(AiContextService, {
758
+ binder: this.context
759
+ }), Layer.succeed(AiConversationService, this))));
760
+ log4("result", {
761
+ messages: messages.length,
762
+ duration: session.duration,
763
+ toolCalls: session.toolCalls
764
+ }, {
765
+ F: __dxlog_file4,
766
+ L: 159,
767
+ S: this,
768
+ C: (f, a) => f(...a)
769
+ });
770
+ return messages;
771
+ });
772
+ }
773
+ };
774
+ var AiConversationService = class _AiConversationService extends Context3.Tag("@dxos/assistant/AiConversationService")() {
775
+ /**
776
+ * Create a new conversation layer from options.
777
+ */
778
+ static layer = (options) => aiContextFromConversation.pipe(Layer.provideMerge(Layer.scoped(_AiConversationService, Effect6.gen(function* () {
779
+ const conversation = yield* acquireReleaseResource(() => new AiConversation(options));
780
+ return conversation;
781
+ }))));
782
+ /**
783
+ * Create a new conversation with a new queue.
784
+ */
785
+ static layerNewQueue = (options) => Layer.unwrapScoped(Effect6.gen(function* () {
786
+ return _AiConversationService.layer({
787
+ ...options,
788
+ queue: yield* QueueService.createQueue()
789
+ });
790
+ }));
791
+ /**
792
+ * Run a prompt in the current conversation.
793
+ */
794
+ static run = (params) => Effect6.gen(function* () {
795
+ const conversation = yield* _AiConversationService;
796
+ return yield* conversation.createRequest(params);
797
+ });
798
+ };
799
+ var aiContextFromConversation = Layer.effect(AiContextService, Effect6.gen(function* () {
800
+ const conversation = yield* AiConversationService;
801
+ return {
802
+ binder: conversation.context
803
+ };
804
+ }));
805
+ var connectMcpServers = (blueprints) => pipe3(blueprints, Array3.flatMap((_) => _.mcpServers ?? []), Effect6.forEach(({ url, protocol }) => McpToolkit.make({
806
+ url,
807
+ kind: protocol
808
+ }).pipe(
809
+ // NOTE: Type-inference fails here without explicit void return.
810
+ Effect6.tap((toolkit) => log4.info("Connected to MCP server", {
811
+ url,
812
+ tools: Object.keys(toolkit.toolkit.tools).length
813
+ }, {
814
+ F: __dxlog_file4,
815
+ L: 240,
816
+ S: void 0,
817
+ C: (f, a) => f(...a)
818
+ })),
819
+ Effect6.tapDefect((error) => Effect6.sync(() => log4.warn("Failed to connect to MCP server", {
820
+ error
821
+ }, {
822
+ F: __dxlog_file4,
823
+ L: 242,
824
+ S: void 0,
825
+ C: (f, a) => f(...a)
826
+ }))),
827
+ Effect6.either
828
+ )), Effect6.map(Array3.filterMap((_) => Either.getRight(_))));
829
+
830
+ // src/diff.ts
831
+ import { next as A } from "@automerge/automerge";
832
+ import { DocAccessor, toCursorRange } from "@dxos/echo-db";
833
+ import { log as log5 } from "@dxos/log";
834
+ import { isNonNullable } from "@dxos/util";
835
+ var __dxlog_file5 = "/__w/dxos/dxos/packages/core/assistant/src/diff.ts";
836
+ var reduceDiffs = (diffs) => {
837
+ return diffs.reduce((acc, diff) => {
838
+ const current = acc[acc.length - 1];
839
+ const [op, ...rest] = diff.split(" ");
840
+ switch (op) {
841
+ case "-": {
842
+ if (current && !current.match) {
843
+ current.match += rest.join(" ");
844
+ } else {
845
+ acc.push({
846
+ match: rest.join(" "),
847
+ replace: ""
848
+ });
849
+ }
850
+ return acc;
851
+ }
852
+ case "+": {
853
+ if (current && !current.replace) {
854
+ current.replace += rest.join(" ");
855
+ } else {
856
+ acc.push({
857
+ match: "",
858
+ replace: rest.join(" ")
859
+ });
860
+ }
861
+ return acc;
862
+ }
863
+ default: {
864
+ throw new Error(`Invalid diff format: each line must start with "- " or "+ ", got: ${JSON.stringify(diff)}`);
865
+ }
866
+ }
867
+ }, []);
868
+ };
869
+ var computeDiffsWithCursors = (accessor, diffs) => {
870
+ return reduceDiffs(diffs).map((diff) => {
871
+ const text = DocAccessor.getValue(accessor);
872
+ const idx = text.indexOf(diff.match);
873
+ if (idx !== -1) {
874
+ return {
875
+ cursor: toCursorRange(accessor, idx, idx + diff.match.length),
876
+ text: diff.replace
877
+ };
878
+ }
879
+ }).filter(isNonNullable);
880
+ };
881
+ var applyDiffs = (accessor, diffs, { errorOnNotFound = false } = {}) => {
882
+ for (const diff of reduceDiffs(diffs)) {
883
+ accessor.handle.change((doc) => {
884
+ const text = DocAccessor.getValue(accessor);
885
+ const idx = text.indexOf(diff.match);
886
+ if (idx !== -1) {
887
+ A.splice(doc, accessor.path, idx, diff.match.length, diff.replace);
888
+ } else {
889
+ if (errorOnNotFound) {
890
+ throw new Error(`Diff not found: ${diff.match}`);
891
+ } else {
892
+ log5.warn("diff not found", diff, {
893
+ F: __dxlog_file5,
894
+ L: 86,
895
+ S: void 0,
896
+ C: (f, a) => f(...a)
897
+ });
898
+ }
899
+ }
900
+ });
901
+ }
902
+ return DocAccessor.getValue(accessor);
903
+ };
904
+
905
+ // src/execution-graph.ts
906
+ import { AgentStatus } from "@dxos/ai";
907
+ import { Obj as Obj5 } from "@dxos/echo";
908
+ import { MESSAGE_PROPERTY_TOOL_CALL_ID } from "@dxos/functions";
909
+ import { LogLevel } from "@dxos/log";
910
+ import { ContentBlock, Message as Message4 } from "@dxos/types";
911
+ import { isNonNullable as isNonNullable2 } from "@dxos/util";
912
+ var SKIP_BLOCKS = [
913
+ "text"
914
+ ];
915
+ var ExecutionGraph = class {
916
+ _skipBlocks;
917
+ // TODO(dmaretskyi): Evolve the internal state to support chats, functions, circuit workflows, etc.
918
+ _commits = [];
919
+ _branchNames = /* @__PURE__ */ new Set();
920
+ _lastBlockId;
921
+ // Tool call tracking.
922
+ _toolCallCommitIds = /* @__PURE__ */ new Map();
923
+ _lastCommitByBranch = /* @__PURE__ */ new Map();
924
+ _pendingToolResults = /* @__PURE__ */ new Map();
925
+ constructor(_skipBlocks = SKIP_BLOCKS) {
926
+ this._skipBlocks = _skipBlocks;
927
+ }
928
+ /**
929
+ * Adds events to the graph.
930
+ */
931
+ addEvents(events) {
932
+ const sortedEvents = this.sortObjectsByCreated(events);
933
+ for (const event of sortedEvents) {
934
+ if (Obj5.instanceOf(Message4.Message, event)) {
935
+ this._processMessage(event);
936
+ } else if (Obj5.instanceOf(AgentStatus, event)) {
937
+ this._processAgentStatus(event);
938
+ }
939
+ }
940
+ }
941
+ /**
942
+ * Processes a message event and creates commits for its blocks.
943
+ */
944
+ _processMessage(message) {
945
+ const messageCommits = messageToCommits(message, this._lastBlockId, this._toolCallCommitIds, this._lastCommitByBranch, this._skipBlocks);
946
+ this._commits.push(...messageCommits);
947
+ messageCommits.forEach((commit) => this._branchNames.add(commit.branch));
948
+ this._trackToolCalls(messageCommits);
949
+ if (messageCommits.length > 0) {
950
+ this._lastBlockId = messageCommits[messageCommits.length - 1].id;
951
+ }
952
+ }
953
+ /**
954
+ * Processes an AgentStatus event.
955
+ */
956
+ _processAgentStatus(event) {
957
+ const branch = this._getToolCallBranch(event.parentMessage, event.toolCallId);
958
+ this._branchNames.add(branch);
959
+ const agentStatusCommit = {
960
+ id: event.id,
961
+ branch,
962
+ message: event.message,
963
+ icon: "ph--flag--regular",
964
+ parents: this._getAgentStatusParents(event),
965
+ timestamp: new Date(event.created)
966
+ };
967
+ this._commits.push(agentStatusCommit);
968
+ this._lastBlockId = event.id;
969
+ if (event.toolCallId) {
970
+ this._lastCommitByBranch.set(branch, event.id);
971
+ this._updatePendingToolResults(event.toolCallId, event.id);
972
+ }
973
+ }
974
+ /**
975
+ * Stable sort: objects with 'created' field are sorted by it, others remain in place.
976
+ */
977
+ sortObjectsByCreated(objects) {
978
+ const sortedObjects = objects.slice();
979
+ const createdObjects = [];
980
+ sortedObjects.forEach((obj, idx) => {
981
+ if (obj && typeof obj.created === "string") {
982
+ createdObjects.push({
983
+ idx,
984
+ obj
985
+ });
986
+ }
987
+ });
988
+ createdObjects.sort((a, b) => {
989
+ const aDate = new Date(a.obj.created).getTime();
990
+ const bDate = new Date(b.obj.created).getTime();
991
+ return aDate - bDate;
992
+ });
993
+ let createdIdx = 0;
994
+ return sortedObjects.map((obj, _idx) => {
995
+ if (obj && typeof obj.created === "string") {
996
+ return createdObjects[createdIdx++].obj;
997
+ }
998
+ return obj;
999
+ });
1000
+ }
1001
+ /**
1002
+ * Returns the current state of the graph.
1003
+ */
1004
+ getGraph(lastRequest = false) {
1005
+ const idx = lastRequest ? this._commits.findLastIndex((c) => c.tags?.includes("user")) : -1;
1006
+ const commits = idx === -1 ? this._commits : this._commits.slice(idx);
1007
+ return {
1008
+ branches: Array.from(this._branchNames),
1009
+ commits
1010
+ };
1011
+ }
1012
+ /**
1013
+ * Tracks tool calls and handles pending tool result updates.
1014
+ */
1015
+ _trackToolCalls(commits) {
1016
+ commits.forEach((commit) => {
1017
+ if (this._isToolCallCommit(commit)) {
1018
+ const toolCallId = this._extractToolCallId(commit.id);
1019
+ if (toolCallId) {
1020
+ this._toolCallCommitIds.set(toolCallId, commit.id);
1021
+ }
1022
+ }
1023
+ if (this._isToolResultCommit(commit)) {
1024
+ const toolCallId = this._extractToolCallId(commit.id);
1025
+ if (toolCallId) {
1026
+ this._pendingToolResults.set(toolCallId, commit.id);
1027
+ this._handleLateToolResultUpdate(toolCallId, commit.id);
1028
+ }
1029
+ }
1030
+ this._lastCommitByBranch.set(commit.branch, commit.id);
1031
+ });
1032
+ }
1033
+ /**
1034
+ * Updates pending tool results to point to an AgentStatus event.
1035
+ */
1036
+ _updatePendingToolResults(toolCallId, agentStatusId) {
1037
+ const toolResultId = this._pendingToolResults.get(toolCallId);
1038
+ if (!toolResultId) return;
1039
+ const toolResultCommit = this._commits.find((c) => c.id === toolResultId);
1040
+ if (!toolResultCommit) return;
1041
+ const toolCallCommitId = this._toolCallCommitIds.get(toolCallId);
1042
+ if (!toolCallCommitId) return;
1043
+ const toolCallIndex = toolResultCommit.parents.indexOf(toolCallCommitId);
1044
+ if (toolCallIndex !== -1) {
1045
+ toolResultCommit.parents[toolCallIndex] = agentStatusId;
1046
+ }
1047
+ this._pendingToolResults.delete(toolCallId);
1048
+ }
1049
+ /**
1050
+ * Handles late updates when AgentStatus is processed before tool result.
1051
+ */
1052
+ _handleLateToolResultUpdate(toolCallId, toolResultId) {
1053
+ const toolCallCommitId = this._toolCallCommitIds.get(toolCallId);
1054
+ if (!toolCallCommitId) return;
1055
+ const messageId = toolCallCommitId.split("_toolCall_")[0];
1056
+ const toolCallBranch = `${messageId}_${toolCallId}`;
1057
+ const agentStatusCommitId = this._lastCommitByBranch.get(toolCallBranch);
1058
+ if (agentStatusCommitId && agentStatusCommitId !== toolCallCommitId) {
1059
+ const toolResultCommit = this._commits.find((c) => c.id === toolResultId);
1060
+ if (toolResultCommit) {
1061
+ const toolCallIndex = toolResultCommit.parents.indexOf(toolCallCommitId);
1062
+ if (toolCallIndex !== -1) {
1063
+ toolResultCommit.parents[toolCallIndex] = agentStatusCommitId;
1064
+ }
1065
+ }
1066
+ this._pendingToolResults.delete(toolCallId);
1067
+ }
1068
+ }
1069
+ /**
1070
+ * Gets the branch name for a tool call.
1071
+ */
1072
+ _getToolCallBranch(parentMessage, toolCallId) {
1073
+ if (toolCallId) {
1074
+ return toolCallId;
1075
+ }
1076
+ return parentMessage || "main";
1077
+ }
1078
+ /**
1079
+ * Gets the parents for an AgentStatus commit.
1080
+ */
1081
+ _getAgentStatusParents(event) {
1082
+ if (event.parentMessage && event.toolCallId) {
1083
+ return [
1084
+ getToolCallId(event.parentMessage, event.toolCallId)
1085
+ ];
1086
+ }
1087
+ return [];
1088
+ }
1089
+ /**
1090
+ * Checks if a commit is a tool call commit.
1091
+ */
1092
+ _isToolCallCommit(commit) {
1093
+ return commit.message.includes("Calling tool");
1094
+ }
1095
+ /**
1096
+ * Checks if a commit is a tool result commit.
1097
+ */
1098
+ _isToolResultCommit(commit) {
1099
+ return commit.message.includes("Tool call succeeded") || commit.message.includes("Tool error:");
1100
+ }
1101
+ /**
1102
+ * Extracts tool call ID from a commit ID.
1103
+ */
1104
+ _extractToolCallId(commitId) {
1105
+ const toolCallMatch = commitId.match(/_toolCall_(.+)$/);
1106
+ const toolResultMatch = commitId.match(/_toolResult_(.+)$/);
1107
+ return toolCallMatch?.[1] || toolResultMatch?.[1];
1108
+ }
1109
+ };
1110
+ var messageToCommits = (message, lastBlockId, toolCallIds, lastCommitByBranch, skipBlocks) => {
1111
+ let previousBlockId = lastBlockId;
1112
+ return message.blocks.map((block, idx) => {
1113
+ const branch = getMessageBranch(message);
1114
+ const parents = getBlockParents(block, previousBlockId, message, toolCallIds, lastCommitByBranch);
1115
+ if (skipBlocks?.includes(block._tag)) {
1116
+ return null;
1117
+ }
1118
+ const commit = createBlockCommit(block, message, branch, parents, idx);
1119
+ if (commit) {
1120
+ previousBlockId = commit.id;
1121
+ }
1122
+ return commit;
1123
+ }).filter(isNonNullable2);
1124
+ };
1125
+ var getBlockParents = (block, previousBlockId, message, toolCallIds, lastCommitByBranch) => {
1126
+ const parents = [];
1127
+ if (block._tag === "toolResult") {
1128
+ if (previousBlockId) {
1129
+ parents.push(previousBlockId);
1130
+ }
1131
+ const toolCallCommitId = toolCallIds?.get(block.toolCallId);
1132
+ if (toolCallCommitId) {
1133
+ const toolCallBranch = getToolCallBranchFromCommitId(toolCallCommitId, block.toolCallId);
1134
+ const lastBlockFromToolCallBranch = lastCommitByBranch?.get(toolCallBranch);
1135
+ if (lastBlockFromToolCallBranch) {
1136
+ parents.push(lastBlockFromToolCallBranch);
1137
+ } else {
1138
+ parents.push(toolCallCommitId);
1139
+ }
1140
+ }
1141
+ } else {
1142
+ if (previousBlockId) {
1143
+ parents.push(previousBlockId);
1144
+ } else {
1145
+ const toolCallParent = getParentId(message);
1146
+ if (toolCallParent) {
1147
+ parents.push(toolCallParent);
1148
+ }
1149
+ }
1150
+ }
1151
+ return parents;
1152
+ };
1153
+ var createBlockCommit = (block, message, branch, parents, idx) => {
1154
+ const timestamp = new Date(message.created);
1155
+ switch (block._tag) {
1156
+ case "text":
1157
+ if (!block.text.trim().length) return null;
1158
+ return {
1159
+ id: getGenericBlockId(message.id, idx),
1160
+ branch,
1161
+ parents,
1162
+ timestamp,
1163
+ ...message.sender.role === "user" ? {
1164
+ icon: "ph--user--regular",
1165
+ tags: [
1166
+ "user"
1167
+ ],
1168
+ message: "Processing request..."
1169
+ } : {
1170
+ icon: "ph--robot--regular",
1171
+ message: `Response (${block.text.split(" ").length} words)`
1172
+ }
1173
+ };
1174
+ case "toolCall":
1175
+ return {
1176
+ id: getToolCallId(message.id, block.toolCallId),
1177
+ branch,
1178
+ parents,
1179
+ timestamp,
1180
+ icon: "ph--wrench--regular",
1181
+ level: LogLevel.INFO,
1182
+ message: `Calling tool (${block.name})`
1183
+ };
1184
+ case "toolResult":
1185
+ return {
1186
+ id: getToolResultId(message.id, block.toolCallId),
1187
+ branch,
1188
+ parents,
1189
+ timestamp,
1190
+ icon: block.error ? "ph--x-circle--regular" : "ph--check-circle--regular",
1191
+ level: block.error ? LogLevel.ERROR : LogLevel.INFO,
1192
+ message: block.error ? "Tool error: " + block.error : "Tool call succeeded"
1193
+ };
1194
+ case "stats":
1195
+ return {
1196
+ id: getGenericBlockId(message.id, idx),
1197
+ branch,
1198
+ parents,
1199
+ timestamp,
1200
+ icon: "ph--rocket--regular",
1201
+ level: LogLevel.INFO,
1202
+ message: ContentBlock.createStatsMessage(block)
1203
+ };
1204
+ case "status":
1205
+ return {
1206
+ id: getGenericBlockId(message.id, idx),
1207
+ branch,
1208
+ parents,
1209
+ timestamp,
1210
+ message: block.statusText,
1211
+ level: LogLevel.INFO,
1212
+ icon: "ph--flag--regular"
1213
+ };
1214
+ case "reasoning":
1215
+ return {
1216
+ id: getGenericBlockId(message.id, idx),
1217
+ branch,
1218
+ parents,
1219
+ timestamp,
1220
+ message: block.reasoningText ?? "Thinking...",
1221
+ icon: "ph--brain--regular"
1222
+ };
1223
+ case "reference":
1224
+ return {
1225
+ id: getGenericBlockId(message.id, idx),
1226
+ branch,
1227
+ parents,
1228
+ timestamp,
1229
+ icon: "ph--link--regular",
1230
+ message: stringifyRef(block.reference)
1231
+ };
1232
+ default:
1233
+ return null;
1234
+ }
1235
+ };
1236
+ var getToolCallBranchFromCommitId = (toolCallCommitId, toolCallId) => {
1237
+ const messageId = toolCallCommitId.split("_toolCall_")[0];
1238
+ return `${messageId}_${toolCallId}`;
1239
+ };
1240
+ var getToolCallId = (messageId, toolCallId) => `${messageId}_toolCall_${toolCallId}`;
1241
+ var getToolResultId = (messageId, toolCallId) => `${messageId}_toolResult_${toolCallId}`;
1242
+ var getGenericBlockId = (messageId, idx) => `${messageId}_block_${idx}`;
1243
+ var getBranchName = (options) => {
1244
+ if (options.toolCallId) {
1245
+ return options.toolCallId;
1246
+ } else if (options.parentMessage) {
1247
+ return options.parentMessage;
1248
+ } else {
1249
+ return "main";
1250
+ }
1251
+ };
1252
+ var getMessageBranch = (message) => {
1253
+ return getBranchName({
1254
+ parentMessage: message.parentMessage,
1255
+ toolCallId: message.properties?.[MESSAGE_PROPERTY_TOOL_CALL_ID]
1256
+ });
1257
+ };
1258
+ var getParentId = (message) => {
1259
+ if (message.parentMessage && message.properties?.[MESSAGE_PROPERTY_TOOL_CALL_ID]) {
1260
+ return getToolCallId(message.parentMessage, message.properties[MESSAGE_PROPERTY_TOOL_CALL_ID]);
1261
+ } else {
1262
+ return void 0;
1263
+ }
1264
+ };
1265
+ var stringifyRef = (ref) => {
1266
+ if (ref.target) {
1267
+ return stringifyObject(ref.target);
1268
+ }
1269
+ return ref.dxn.asEchoDXN()?.echoId ?? ref.dxn.asQueueDXN()?.objectId ?? "";
1270
+ };
1271
+ var stringifyObject = (obj) => {
1272
+ return Obj5.getLabel(obj) ?? Obj5.getTypename(obj) ?? obj.id;
1273
+ };
1274
+
1275
+ // src/templates/system.ts
1276
+ import defaultsDeep from "lodash.defaultsdeep";
1277
+ import { Template as Template2 } from "@dxos/blueprints";
1278
+
1279
+ // raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/instructions/format.tpl?raw
1280
+ var format_default = "{{! System Core }}\n\n## Input Formats\n\n### Blueprints\n\nYou may be provided with Blueprint definitions that include specific instructions and tools for a given set of tasks.\nBlue print instructions will be enclosed in a <blueprint> tag.\n\n### Context objects\n\nYou may be provided with references to objects that are relevant to the current prompt.\nObjects will be in the form of a <object> tag with a <dxn> element that is the unique key of the object required by many tools.\nThe <typename> element is the type of the object.\n\n<object>\n <dxn>object-id</dxn>\n <typename>example.com/type/TypeName</typename>\n</object>\n\n## Response Formats\n\nIt is very important to respond in the correct format.\nYou response must be valid Markdown.\nIn some cases you should respond with well-formed XML tags along with the content.\nCode snippets should use 2-space indents.\n\n{{#if cot}}\nBefore responding, explain your reasoning and include your detailed chain-of-thought enclosed in a <cot> tag.\nThe <cot> tag should be the first thing in your response.\n{{/if}}\n\n## Block references\n\nYou can reply with a block reference for objects if you have the DXN. For example:\n\n<object>\n <dxn>dxn:queue:data:B6INSIBY3CBEF4M5VZRYBCMAHQMPYK5AJ:01K24XMVHSZHS97SG1VTVQDM5Z:01K24XPK464FSCKVQJAB2H662M</dxn>\n</object>\n<object>\n <dxn>dxn:echo:@:01K24XPK464FSCKVQJAB2H662M</dxn>\n</object>\n\n### Suggestions\n\nEach suggestion must in the form of a proposed action enclosed in a <suggestion> tag.\nExample: <suggestion>Show on a map</suggestion>\nDo not use bullets when listing suggestions.\n\n### Multiple choice questions\n\nYou may ask multiple choice questions by wrapping consise options inside <option> tags inside a well formed <select> tag.\nExample: <select><option>Yes</option><option>No</option></select>\n\n## Context\n\nThe current date and time is {{DATETIME}}.\n";
1281
+
1282
+ // raw-loader:/__w/dxos/dxos/packages/core/assistant/src/templates/instructions/system.tpl?raw
1283
+ var system_default = "{{! System Prompt }}\n\n- You are a helpful assistant called Kai. \n- Your name is Kai and you are powered by different language models.\n- You were created by DXOS and operate inside of Composer and on the DXOS EDGE network.\n- In your initial greeting state your name and make some suggestions based on the current context objects.\n- You are an advanced AI assistant capable of creating and managing artifacts from provided data and tools.\n- Your task is to process user commands and questions and decide how best to respond.\n- In general be concise and direct.\n- Follow all instructions carefully.\n\n## Planning\n\n- Analyze the structure and type of the content in the user's message.\n- Determine if you complete the task using the available blueprint definitions?\n- If you can't complete the task using the available blueprint definitions, query the list of available blueprint definitions using the appropriate tool.\n- Identify which blueprint definitions are relevant to the user's request.\n- Evaluate the potential benefits of creating an artifact vs. normal processing for each identified element.\n- Make a final decision on whether to create an artifact and explain your reasoning.\n- Are the required blueprint definitions already available?\n- If not, select which blueprint definition(s) will be the most relevant and require them using the require_artifact_definitions tool.\n- The require'd artifact tools will be available for use after require.\n- If creating an artifact, outline how you will structure it within the response.\n- Decide if the artifact needs to be shown to the user.\n- Call the show tool to show the artifact to the user.\n- Your reasoning must include: whether to use artifacts or not, to create one or query, whether to show the artifact to the user, and how to structure the response.\n\n## Blueprints and Artifacts\n\n- Determine if the interaction involves an artifact. Prefer artifacts for tables, lists, spreadsheets, kanbans, games, images, and other structured data.\n- Determine if the user is explicitly talking about creating a new artifact, or wants to use an existing artifact.\n- If it is ambiguous, query for existing artifacts first and then decide.\n- If you decide to create an artifact, call the associated tool to create the artifact.\n- Artifacts are stored in the database. Tools are used to create and query artifacts.\n- If you are unsure about creating an artifact ask the user for clarification.\n- Artifacts are mutable objects that can change over the course of the conversation.\n- Always re-query the artifact using the tool (like query or inspect) to get the latest state of the artifact before answering the user.\n- You must never generate the id of the artifact; only recall the ids that are already in the history.\n- Artifacts are created by requiring the specific artifact using the require_artifact tool and creating it by calling the associated tool.\n- You can add suggested actions at the end of your response.\n- Suggested actions should be very concise and start with a verb and be phrased as a command to an agent -- not a question to the user.\n- Suggested actions must be in the form of a user instruction that you can follow.\n- Suggested actions could include actions that create artifacts.\n- After creating an artifact use a tool to add it to the chat context.\n\n## Context\n\nToday is {{DATETIME}}.\n";
1284
+
1285
+ // src/templates/system.ts
1286
+ var createSystemPrompt = (options = {}) => Template2.process(format_default, defaultsDeep(options, {
1287
+ DATETIME: (/* @__PURE__ */ new Date()).toISOString()
1288
+ }));
1289
+ var templates = {
1290
+ /** Editable system prompt.*/
1291
+ system: Template2.make({
1292
+ /**
1293
+ * NOTE: We are forbidden from accessing randomness at model level in cloudflare, so we have to have pre-generated ids.
1294
+ */
1295
+ id: "01K2J2FZWH39F55CS7B26GP1Q3",
1296
+ source: system_default
1297
+ })
1298
+ };
1299
+ export {
1300
+ AiAssistantError,
1301
+ AiContextBinder,
1302
+ AiContextService,
1303
+ AiConversation,
1304
+ AiConversationService,
1305
+ AiModelError,
1306
+ AiSession,
1307
+ ArtifactDiffResolver,
1308
+ ArtifactId,
1309
+ Bindings,
1310
+ ContextBinding,
1311
+ ExecutionGraph,
1312
+ GenerationObserver,
1313
+ RefFromLLM,
1314
+ ToolExecutionServices,
1315
+ VersionPin,
1316
+ applyDiffs,
1317
+ computeDiffsWithCursors,
1318
+ createArtifactElement,
1319
+ createSystemPrompt,
1320
+ createToolkit,
1321
+ formatSystemPrompt,
1322
+ formatUserPrompt,
1323
+ makeToolExecutionServiceFromFunctions,
1324
+ makeToolResolverFromFunctions,
1325
+ reduceDiffs,
1326
+ templates
1327
+ };
1328
+ //# sourceMappingURL=index.mjs.map