@dxos/assistant 0.8.0 → 0.8.1-main.013e445

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 (99) hide show
  1. package/dist/lib/browser/chunk-BDMTBYIM.mjs +2285 -0
  2. package/dist/lib/browser/chunk-BDMTBYIM.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +328 -1206
  4. package/dist/lib/browser/index.mjs.map +4 -4
  5. package/dist/lib/browser/meta.json +1 -1
  6. package/dist/lib/browser/testing/index.mjs +17 -3
  7. package/dist/lib/browser/testing/index.mjs.map +4 -4
  8. package/dist/lib/node/chunk-A44NGSCM.cjs +2296 -0
  9. package/dist/lib/node/chunk-A44NGSCM.cjs.map +7 -0
  10. package/dist/lib/node/index.cjs +335 -1206
  11. package/dist/lib/node/index.cjs.map +4 -4
  12. package/dist/lib/node/meta.json +1 -1
  13. package/dist/lib/node/testing/index.cjs +40 -29
  14. package/dist/lib/node/testing/index.cjs.map +4 -4
  15. package/dist/lib/node-esm/chunk-J6DMSFSZ.mjs +2287 -0
  16. package/dist/lib/node-esm/chunk-J6DMSFSZ.mjs.map +7 -0
  17. package/dist/lib/node-esm/index.mjs +328 -1206
  18. package/dist/lib/node-esm/index.mjs.map +4 -4
  19. package/dist/lib/node-esm/meta.json +1 -1
  20. package/dist/lib/node-esm/testing/index.mjs +17 -3
  21. package/dist/lib/node-esm/testing/index.mjs.map +4 -4
  22. package/dist/types/src/ai-service/defs.d.ts +8 -2
  23. package/dist/types/src/ai-service/defs.d.ts.map +1 -1
  24. package/dist/types/src/ai-service/edge-client.d.ts +19 -0
  25. package/dist/types/src/ai-service/edge-client.d.ts.map +1 -0
  26. package/dist/types/src/ai-service/index.d.ts +4 -1
  27. package/dist/types/src/ai-service/index.d.ts.map +1 -1
  28. package/dist/types/src/ai-service/message-collector.d.ts +88 -0
  29. package/dist/types/src/ai-service/message-collector.d.ts.map +1 -0
  30. package/dist/types/src/ai-service/ollama-client.d.ts +39 -0
  31. package/dist/types/src/ai-service/ollama-client.d.ts.map +1 -0
  32. package/dist/types/src/ai-service/parser.d.ts +11 -9
  33. package/dist/types/src/ai-service/parser.d.ts.map +1 -1
  34. package/dist/types/src/ai-service/service.d.ts +9 -0
  35. package/dist/types/src/ai-service/service.d.ts.map +1 -0
  36. package/dist/types/src/ai-service/stream.d.ts +2 -1
  37. package/dist/types/src/ai-service/stream.d.ts.map +1 -1
  38. package/dist/types/src/ai-service/testing/mock.d.ts +2 -2
  39. package/dist/types/src/ai-service/testing/mock.d.ts.map +1 -1
  40. package/dist/types/src/ai-service/types.d.ts +91 -157
  41. package/dist/types/src/ai-service/types.d.ts.map +1 -1
  42. package/dist/types/src/conversation/conversation.d.ts +11 -9
  43. package/dist/types/src/conversation/conversation.d.ts.map +1 -1
  44. package/dist/types/src/conversation/types.d.ts +1 -1
  45. package/dist/types/src/conversation/types.d.ts.map +1 -1
  46. package/dist/types/src/index.d.ts +1 -0
  47. package/dist/types/src/index.d.ts.map +1 -1
  48. package/dist/types/src/session/index.d.ts +2 -0
  49. package/dist/types/src/session/index.d.ts.map +1 -0
  50. package/dist/types/src/session/session.d.ts +215 -0
  51. package/dist/types/src/session/session.d.ts.map +1 -0
  52. package/dist/types/src/session/session.test.d.ts +2 -0
  53. package/dist/types/src/session/session.test.d.ts.map +1 -0
  54. package/dist/types/src/testing/index.d.ts +1 -0
  55. package/dist/types/src/testing/index.d.ts.map +1 -1
  56. package/dist/types/src/testing/ollama.d.ts +6 -0
  57. package/dist/types/src/testing/ollama.d.ts.map +1 -0
  58. package/dist/types/src/testing/query-promts.d.ts +1 -1
  59. package/dist/types/src/testing/query-promts.d.ts.map +1 -1
  60. package/dist/types/src/testing/test-data.d.ts +1 -1
  61. package/dist/types/src/testing/test-data.d.ts.map +1 -1
  62. package/dist/types/src/testing/test-schema.d.ts +1 -1
  63. package/dist/types/src/testing/test-schema.d.ts.map +1 -1
  64. package/package.json +17 -14
  65. package/src/ai-service/client.test.ts +100 -13
  66. package/src/ai-service/defs.ts +18 -3
  67. package/src/ai-service/{client.ts → edge-client.ts} +18 -26
  68. package/src/ai-service/index.ts +4 -1
  69. package/src/ai-service/message-collector.ts +84 -0
  70. package/src/ai-service/ollama-client.ts +499 -0
  71. package/src/ai-service/parser.ts +8 -2
  72. package/src/ai-service/service.ts +15 -0
  73. package/src/ai-service/stream.ts +5 -3
  74. package/src/ai-service/testing/mock.ts +7 -2
  75. package/src/ai-service/types.ts +3 -3
  76. package/src/bin/chat.ts +3 -3
  77. package/src/conversation/conversation.test.ts +5 -5
  78. package/src/conversation/conversation.ts +3 -2
  79. package/src/conversation/types.ts +1 -1
  80. package/src/index.ts +1 -0
  81. package/src/session/index.ts +5 -0
  82. package/src/session/session.test.ts +256 -0
  83. package/src/session/session.ts +369 -0
  84. package/src/testing/artifacts.test.ts +4 -4
  85. package/src/testing/index.ts +1 -0
  86. package/src/testing/object-extraction.test.ts +1 -1
  87. package/src/testing/ollama.ts +15 -0
  88. package/src/testing/query-promts.ts +1 -1
  89. package/src/testing/query.test.ts +4 -4
  90. package/src/testing/test-data.ts +1 -1
  91. package/src/testing/test-schema.ts +1 -1
  92. package/dist/lib/browser/chunk-AXUUNA7O.mjs +0 -565
  93. package/dist/lib/browser/chunk-AXUUNA7O.mjs.map +0 -7
  94. package/dist/lib/node/chunk-QPHGQINW.cjs +0 -577
  95. package/dist/lib/node/chunk-QPHGQINW.cjs.map +0 -7
  96. package/dist/lib/node-esm/chunk-GOVFOBIB.mjs +0 -567
  97. package/dist/lib/node-esm/chunk-GOVFOBIB.mjs.map +0 -7
  98. package/dist/types/src/ai-service/client.d.ts +0 -25
  99. package/dist/types/src/ai-service/client.d.ts.map +0 -1
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,1255 +15,386 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
  var node_exports = {};
30
20
  __export(node_exports, {
31
- AIServiceClientImpl: () => AIServiceClientImpl,
32
- DEFAULT_LLM_MODEL: () => DEFAULT_LLM_MODEL,
33
- DEFAULT_LLM_MODELS: () => DEFAULT_LLM_MODELS,
34
- EchoDataSource: () => import_chunk_QPHGQINW.EchoDataSource,
35
- GenerateRequest: () => GenerateRequest,
36
- GenerationStream: () => GenerationStream,
37
- LLMModel: () => LLMModel,
38
- MixedStreamParser: () => MixedStreamParser,
39
- StreamTransform: () => StreamTransform,
40
- ToolTypes: () => ToolTypes,
41
- createArtifactElement: () => createArtifactElement,
42
- createGenerationStream: () => createGenerationStream,
43
- executeQuery: () => import_chunk_QPHGQINW.executeQuery,
44
- executeQueryPlan: () => import_chunk_QPHGQINW.executeQueryPlan,
45
- formatInferredRelationshipLabel: () => import_chunk_QPHGQINW.formatInferredRelationshipLabel,
46
- formatJsonSchemaForLLM: () => import_chunk_QPHGQINW.formatJsonSchemaForLLM,
47
- formatNodeLabel: () => import_chunk_QPHGQINW.formatNodeLabel,
48
- isReference: () => import_chunk_QPHGQINW.isReference,
49
- isToolUse: () => isToolUse,
50
- mergeMessageBlock: () => mergeMessageBlock,
51
- parseCypherQuery: () => import_chunk_QPHGQINW.parseCypherQuery,
52
- runLLM: () => runLLM,
53
- runTools: () => runTools
21
+ AIServiceEdgeClient: () => import_chunk_A44NGSCM.AIServiceEdgeClient,
22
+ AIServiecOverloadedError: () => AIServiecOverloadedError,
23
+ AISession: () => AISession,
24
+ DEFAULT_EDGE_MODEL: () => import_chunk_A44NGSCM.DEFAULT_EDGE_MODEL,
25
+ DEFAULT_EDGE_MODELS: () => import_chunk_A44NGSCM.DEFAULT_EDGE_MODELS,
26
+ DEFAULT_OLLAMA_ENDPOINT: () => import_chunk_A44NGSCM.DEFAULT_OLLAMA_ENDPOINT,
27
+ DEFAULT_OLLAMA_MODEL: () => import_chunk_A44NGSCM.DEFAULT_OLLAMA_MODEL,
28
+ DEFAULT_OLLAMA_MODELS: () => import_chunk_A44NGSCM.DEFAULT_OLLAMA_MODELS,
29
+ EchoDataSource: () => import_chunk_A44NGSCM.EchoDataSource,
30
+ GenerateRequest: () => import_chunk_A44NGSCM.GenerateRequest,
31
+ GenerationStreamImpl: () => import_chunk_A44NGSCM.GenerationStreamImpl,
32
+ LLMModel: () => import_chunk_A44NGSCM.LLMModel,
33
+ MessageCollector: () => import_chunk_A44NGSCM.MessageCollector,
34
+ MixedStreamParser: () => import_chunk_A44NGSCM.MixedStreamParser,
35
+ OllamaClient: () => import_chunk_A44NGSCM.OllamaClient,
36
+ StreamTransform: () => import_chunk_A44NGSCM.StreamTransform,
37
+ ToolTypes: () => import_chunk_A44NGSCM.ToolTypes,
38
+ createArtifactElement: () => import_chunk_A44NGSCM.createArtifactElement,
39
+ createGenerationStream: () => import_chunk_A44NGSCM.createGenerationStream,
40
+ emitMessageAsEvents: () => import_chunk_A44NGSCM.emitMessageAsEvents,
41
+ executeQuery: () => import_chunk_A44NGSCM.executeQuery,
42
+ executeQueryPlan: () => import_chunk_A44NGSCM.executeQueryPlan,
43
+ formatInferredRelationshipLabel: () => import_chunk_A44NGSCM.formatInferredRelationshipLabel,
44
+ formatJsonSchemaForLLM: () => import_chunk_A44NGSCM.formatJsonSchemaForLLM,
45
+ formatNodeLabel: () => import_chunk_A44NGSCM.formatNodeLabel,
46
+ isReference: () => import_chunk_A44NGSCM.isReference,
47
+ isToolUse: () => import_chunk_A44NGSCM.isToolUse,
48
+ mergeMessageBlock: () => import_chunk_A44NGSCM.mergeMessageBlock,
49
+ parseCypherQuery: () => import_chunk_A44NGSCM.parseCypherQuery,
50
+ runLLM: () => import_chunk_A44NGSCM.runLLM,
51
+ runTools: () => import_chunk_A44NGSCM.runTools
54
52
  });
55
53
  module.exports = __toCommonJS(node_exports);
56
- var import_chunk_QPHGQINW = require("./chunk-QPHGQINW.cjs");
57
- var import_schema = require("@effect/schema");
54
+ var import_chunk_A44NGSCM = require("./chunk-A44NGSCM.cjs");
55
+ var import_effect = require("effect");
58
56
  var import_artifact = require("@dxos/artifact");
59
- var import_invariant = require("@dxos/invariant");
60
- var import_log = require("@dxos/log");
61
57
  var import_async = require("@dxos/async");
62
- var import_log2 = require("@dxos/log");
63
- var import_artifact2 = require("@dxos/artifact");
64
- var import_async2 = require("@dxos/async");
65
58
  var import_echo_schema = require("@dxos/echo-schema");
66
- var import_invariant2 = require("@dxos/invariant");
67
- var import_log3 = require("@dxos/log");
68
- var import_util = require("@dxos/util");
69
- var P = __toESM(require("parsimmon"));
70
- var import_schema2 = require("@effect/schema");
71
- var import_artifact3 = require("@dxos/artifact");
72
- var import_echo_schema2 = require("@dxos/echo-schema");
73
- var import_artifact4 = require("@dxos/artifact");
74
- var import_echo_schema3 = require("@dxos/echo-schema");
75
- var import_invariant3 = require("@dxos/invariant");
76
- var import_log4 = require("@dxos/log");
77
- async function* iterSSEMessages(response, controller) {
78
- if (!response.body) {
79
- controller.abort();
80
- throw new Error("Attempted to iterate over a response with no body");
81
- }
82
- const sseDecoder = new SSEDecoder();
83
- const lineDecoder = new LineDecoder();
84
- const iter = readableStreamAsyncIterable(response.body);
85
- for await (const sseChunk of iterSSEChunks(iter)) {
86
- for (const line of lineDecoder.decode(sseChunk)) {
87
- const sse = sseDecoder.decode(line);
88
- if (sse) {
89
- yield sse;
90
- }
91
- }
92
- }
93
- for (const line of lineDecoder.flush()) {
94
- const sse = sseDecoder.decode(line);
95
- if (sse) {
96
- yield sse;
97
- }
98
- }
99
- }
100
- async function* iterSSEChunks(iterator) {
101
- let data = new Uint8Array();
102
- for await (const chunk of iterator) {
103
- if (chunk == null) {
104
- continue;
105
- }
106
- const binaryChunk = chunk instanceof ArrayBuffer ? new Uint8Array(chunk) : typeof chunk === "string" ? new TextEncoder().encode(chunk) : chunk;
107
- const newData = new Uint8Array(data.length + binaryChunk.length);
108
- newData.set(data);
109
- newData.set(binaryChunk, data.length);
110
- data = newData;
111
- let patternIndex;
112
- while ((patternIndex = findDoubleNewlineIndex(data)) !== -1) {
113
- yield data.slice(0, patternIndex);
114
- data = data.slice(patternIndex);
115
- }
116
- }
117
- if (data.length > 0) {
118
- yield data;
119
- }
120
- }
121
- var findDoubleNewlineIndex = (buffer) => {
122
- const newline = 10;
123
- const carriage = 13;
124
- for (let i = 0; i < buffer.length - 2; i++) {
125
- if (buffer[i] === newline && buffer[i + 1] === newline) {
126
- return i + 2;
127
- }
128
- if (buffer[i] === carriage && buffer[i + 1] === carriage) {
129
- return i + 2;
130
- }
131
- if (buffer[i] === carriage && buffer[i + 1] === newline && i + 3 < buffer.length && buffer[i + 2] === carriage && buffer[i + 3] === newline) {
132
- return i + 4;
133
- }
134
- }
135
- return -1;
136
- };
137
- var SSEDecoder = class {
138
- constructor() {
139
- this.event = null;
140
- this.data = [];
141
- this.chunks = [];
142
- }
143
- decode(line) {
144
- if (line.endsWith("\r")) {
145
- line = line.substring(0, line.length - 1);
146
- }
147
- if (!line) {
148
- if (!this.event && !this.data.length) {
149
- return null;
150
- }
151
- const sse = {
152
- event: this.event,
153
- data: this.data.join("\n"),
154
- raw: this.chunks
155
- };
156
- this.event = null;
157
- this.data = [];
158
- this.chunks = [];
159
- return sse;
160
- }
161
- this.chunks.push(line);
162
- if (line.startsWith(":")) {
163
- return null;
164
- }
165
- let [fieldname, _, value] = partition(line, ":");
166
- if (value.startsWith(" ")) {
167
- value = value.substring(1);
168
- }
169
- if (fieldname === "event") {
170
- this.event = value;
171
- } else if (fieldname === "data") {
172
- this.data.push(value);
173
- }
174
- return null;
175
- }
176
- };
177
- var partition = (str, delimiter) => {
178
- const index = str.indexOf(delimiter);
179
- if (index !== -1) {
180
- return [
181
- str.substring(0, index),
182
- delimiter,
183
- str.substring(index + delimiter.length)
184
- ];
185
- }
186
- return [
187
- str,
188
- "",
189
- ""
190
- ];
191
- };
192
- function readableStreamAsyncIterable(stream) {
193
- if (stream[Symbol.asyncIterator]) {
194
- return stream;
195
- }
196
- const reader = stream.getReader();
197
- return {
198
- next: async () => {
199
- try {
200
- const result = await reader.read();
201
- if (result?.done) {
202
- reader.releaseLock();
203
- }
204
- return result;
205
- } catch (e) {
206
- reader.releaseLock();
207
- throw e;
208
- }
209
- },
210
- return: async () => {
211
- const cancelPromise = reader.cancel();
212
- reader.releaseLock();
213
- await cancelPromise;
214
- return {
215
- done: true,
216
- value: void 0
217
- };
218
- },
219
- [Symbol.asyncIterator]() {
220
- return this;
221
- }
222
- };
59
+ var import_invariant = require("@dxos/invariant");
60
+ var import_log = require("@dxos/log");
61
+ function _ts_decorate(decorators, target, key, desc) {
62
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
63
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
64
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
65
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
223
66
  }
224
- var LineDecoder = class _LineDecoder {
225
- static {
226
- this.NEWLINE_CHARS = /* @__PURE__ */ new Set([
227
- "\n",
228
- "\r"
229
- ]);
230
- }
231
- static {
232
- this.NEWLINE_REGEXP = /\r\n|[\n\r]/g;
233
- }
234
- constructor() {
235
- this.buffer = [];
236
- this.trailingCR = false;
237
- }
238
- decode(chunk) {
239
- let text = this.decodeText(chunk);
240
- if (this.trailingCR) {
241
- text = "\r" + text;
242
- this.trailingCR = false;
243
- }
244
- if (text.endsWith("\r")) {
245
- this.trailingCR = true;
246
- text = text.slice(0, -1);
247
- }
248
- if (!text) {
249
- return [];
250
- }
251
- const trailingNewline = _LineDecoder.NEWLINE_CHARS.has(text[text.length - 1] || "");
252
- let lines = text.split(_LineDecoder.NEWLINE_REGEXP);
253
- if (trailingNewline) {
254
- lines.pop();
255
- }
256
- if (lines.length === 1 && !trailingNewline) {
257
- this.buffer.push(lines[0]);
258
- return [];
259
- }
260
- if (this.buffer.length > 0) {
261
- lines = [
262
- this.buffer.join("") + lines[0],
263
- ...lines.slice(1)
264
- ];
265
- this.buffer = [];
266
- }
267
- if (!trailingNewline) {
268
- this.buffer = [
269
- lines.pop() || ""
270
- ];
271
- }
272
- return lines;
273
- }
274
- decodeText(bytes) {
275
- if (bytes == null) {
276
- return "";
277
- }
278
- if (typeof bytes === "string") {
279
- return bytes;
280
- }
281
- if (typeof Buffer !== "undefined") {
282
- if (bytes instanceof Buffer) {
283
- return bytes.toString();
284
- }
285
- if (bytes instanceof Uint8Array) {
286
- return Buffer.from(bytes).toString();
287
- }
288
- throw new Error(`Unexpected: received non-Uint8Array (${bytes.constructor.name}) stream chunk in an environment with a global "Buffer" defined, which this library assumes to be Node. Please report this error.`);
289
- }
290
- if (typeof TextDecoder !== "undefined") {
291
- if (bytes instanceof Uint8Array || bytes instanceof ArrayBuffer) {
292
- this.textDecoder ??= new TextDecoder("utf8");
293
- return this.textDecoder.decode(bytes);
294
- }
295
- throw new Error(`Unexpected: received non-Uint8Array/ArrayBuffer (${bytes.constructor.name}) in a web platform. Please report this error.`);
296
- }
297
- throw new Error("Unexpected: neither Buffer nor TextDecoder are available as globals. Please report this error.");
298
- }
299
- flush() {
300
- if (!this.buffer.length && !this.trailingCR) {
301
- return [];
302
- }
303
- const lines = [
304
- this.buffer.join("")
305
- ];
306
- this.buffer = [];
307
- this.trailingCR = false;
308
- return lines;
309
- }
310
- };
311
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/ai-service/stream.ts";
312
- var createGenerationStream = (response, controller = new AbortController()) => {
313
- const iterator = async function* () {
314
- for await (const sse of iterSSEMessages(response, controller)) {
315
- if (sse.event === "completion") {
316
- try {
317
- yield JSON.parse(sse.data);
318
- } catch (err) {
319
- import_log2.log.error("could not parse message into JSON:", {
320
- data: sse.data,
321
- raw: sse.raw
322
- }, {
323
- F: __dxlog_file,
324
- L: 21,
325
- S: this,
326
- C: (f, a) => f(...a)
327
- });
328
- throw err;
329
- }
330
- }
331
- if (sse.event === "message_start" || sse.event === "message_delta" || sse.event === "message_stop" || sse.event === "content_block_start" || sse.event === "content_block_delta" || sse.event === "content_block_stop") {
332
- try {
333
- yield JSON.parse(sse.data);
334
- } catch (err) {
335
- import_log2.log.error("could not parse message into JSON:", {
336
- data: sse.data,
337
- raw: sse.raw
338
- }, {
339
- F: __dxlog_file,
340
- L: 37,
341
- S: this,
342
- C: (f, a) => f(...a)
343
- });
344
- throw err;
345
- }
346
- }
347
- if (sse.event === "ping") {
348
- continue;
349
- }
350
- if (sse.event === "error") {
351
- throw new Error(`Message generation error: ${sse.data}`);
352
- }
353
- }
354
- };
355
- return new GenerationStream(controller, iterator);
356
- };
357
- var GenerationStream = class {
358
- constructor(_controller, _getIterator) {
359
- this._controller = _controller;
360
- this._getIterator = _getIterator;
361
- this._done = new import_async.Trigger();
362
- this._iterator = void 0;
363
- }
364
- /**
365
- * Returns an iterator over the stream.
366
- */
367
- [Symbol.asyncIterator]() {
368
- return this._createIterator();
369
- }
370
- /**
371
- * Aborts the stream.
372
- */
373
- abort() {
374
- this._controller.abort();
375
- }
376
- /**
377
- * Returns the complete message list generated by the AI service.
378
- */
379
- async complete() {
380
- await this._done.wait();
381
- }
382
- /**
383
- * Creates an iterator over the stream.
384
- */
385
- _createIterator() {
386
- const self = this;
387
- return this._iterator ??= (() => {
388
- const generator = async function* () {
389
- try {
390
- for await (const event of self._getIterator()) {
391
- yield event;
392
- }
393
- this._done.wake();
394
- } catch (err) {
395
- this._done.throw(err);
396
- }
397
- };
398
- return generator.call(this);
399
- })();
400
- }
401
- };
402
- var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/ai-service/client.ts";
403
- var AIServiceClientImpl = class {
404
- constructor({ endpoint }) {
405
- (0, import_invariant.invariant)(endpoint, "endpoint is required", {
406
- F: __dxlog_file2,
407
- L: 34,
408
- S: this,
409
- A: [
410
- "endpoint",
411
- "'endpoint is required'"
412
- ]
413
- });
414
- this._endpoint = endpoint;
415
- }
416
- async appendMessages(messages) {
417
- const url = `${this._endpoint}/message`;
418
- const res = await fetch(url, {
419
- method: "POST",
420
- headers: {
421
- "Content-Type": "application/json"
422
- },
423
- body: JSON.stringify({
424
- messages: await import_schema.Schema.encodePromise(import_schema.Schema.Array(import_artifact.Message))(messages)
425
- })
426
- });
427
- if (!res.ok) {
428
- throw new Error(`${await res.text()} [${url}]`);
429
- }
430
- }
431
- /**
432
- * Process request and open message stream.
433
- */
434
- async exec(request) {
435
- request = {
436
- tools: request.tools ?? [],
437
- ...request
438
- };
439
- import_log.log.info("requesting", {
440
- endpoint: this._endpoint
441
- }, {
442
- F: __dxlog_file2,
443
- L: 63,
444
- S: this,
445
- C: (f, a) => f(...a)
446
- });
447
- const controller = new AbortController();
448
- const response = await fetch(`${this._endpoint}/generate`, {
449
- signal: controller.signal,
450
- method: "POST",
451
- headers: {
452
- "Content-Type": "application/json"
453
- },
454
- body: JSON.stringify(request)
455
- });
456
- (0, import_invariant.invariant)(response.body instanceof ReadableStream, void 0, {
457
- F: __dxlog_file2,
458
- L: 72,
459
- S: this,
460
- A: [
461
- "response.body instanceof ReadableStream",
462
- ""
463
- ]
67
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/session/session.ts";
68
+ var AISession = class _AISession {
69
+ constructor(_options) {
70
+ this._options = _options;
71
+ this._parser = new import_chunk_A44NGSCM.MixedStreamParser();
72
+ this._pending = [];
73
+ this._history = [];
74
+ this.message = this._parser.message;
75
+ this.block = this._parser.block;
76
+ this.update = this._parser.update;
77
+ this.streamEvent = this._parser.streamEvent;
78
+ this.userMessage = new import_async.Event();
79
+ this._parser.message.on((message) => {
80
+ this._pending.push(message);
464
81
  });
465
- return createGenerationStream(response, controller);
466
- }
467
- };
468
- var DEFAULT_LLM_MODEL = "@anthropic/claude-3-5-sonnet-20241022";
469
- var DEFAULT_LLM_MODELS = [
470
- // AI Gateway.
471
- // https://developers.cloudflare.com/ai-gateway
472
- "@anthropic/claude-3-5-haiku-20241022",
473
- "@anthropic/claude-3-5-sonnet-20241022",
474
- "@anthropic/claude-3-7-sonnet-20250219",
475
- // Workers AI.
476
- // https://developers.cloudflare.com/workers-ai/models
477
- "@cf/deepseek-ai/deepseek-r1-distill-qwen-32b",
478
- "@hf/nousresearch/hermes-2-pro-mistral-7b",
479
- "@ollama/llama-3-1-nemotron-70b-instruct",
480
- "@ollama/llama-3-1-nemotron-mini-4b-instruct",
481
- "@ollama/llama-3-2-3b"
482
- ];
483
- var textChunk = P.regexp(/[^<]+/).map((content) => ({
484
- type: "text",
485
- content
486
- }));
487
- var attribute = P.seq(P.regexp(/[a-zA-Z_][a-zA-Z0-9_-]*/), P.string("=").trim(P.optWhitespace), P.alt(P.regexp(/"[^"]*"/).map((s) => s.slice(1, -1)), P.regexp(/'[^']*'/).map((s) => s.slice(1, -1)))).map(([name, _, value]) => [
488
- name,
489
- value
490
- ]);
491
- var tagName = P.regexp(/[a-zA-Z_][a-zA-Z0-9_-]*/);
492
- var selfClosingTag = P.seqMap(
493
- //
494
- P.string("<"),
495
- tagName,
496
- P.optWhitespace.then(attribute).many(),
497
- P.optWhitespace.then(P.string("/>")),
498
- P.optWhitespace,
499
- (_, tag, attributes, __) => ({
500
- type: "tag",
501
- tag,
502
- attributes: Object.fromEntries(attributes),
503
- content: [],
504
- selfClosing: true
505
- })
506
- );
507
- var openTag = P.seqMap(
508
- //
509
- P.string("<"),
510
- tagName,
511
- P.optWhitespace.then(attribute).many(),
512
- P.string(">"),
513
- P.regexp(/[^<]*/),
514
- (_, tag, attributes, __, content) => ({
515
- type: "tag",
516
- tag,
517
- attributes: Object.fromEntries(attributes),
518
- content: content.length ? [
519
- {
520
- type: "text",
521
- content
522
- }
523
- ] : []
524
- })
525
- );
526
- var closeTag = P.seqMap(
527
- //
528
- P.string("</"),
529
- tagName,
530
- P.string(">"),
531
- P.optWhitespace,
532
- (_, tag) => ({
533
- type: "tag",
534
- tag,
535
- content: [],
536
- closing: true
537
- })
538
- );
539
- var mixedChunk = P.alt(selfClosingTag, openTag, closeTag, textChunk);
540
- var StreamTransform = class {
541
- constructor() {
542
- this._buffer = "";
543
- }
544
- transform(chunk) {
545
- this._buffer += chunk;
546
- const results = [];
547
- const parser = mixedChunk.many().skip(P.optWhitespace);
548
- const result = parser.parse(this._buffer);
549
- if (result.status) {
550
- for (const chunk2 of result.value) {
551
- results.push(chunk2);
552
- }
553
- this._buffer = "";
554
- }
555
- return results;
556
- }
557
- // TODO(burdon): Process remaining buffer?
558
- flush() {
559
- const remaining = this._buffer;
560
- this._buffer = "";
561
- return remaining.length > 0 ? [
562
- {
563
- type: "text",
564
- content: remaining
565
- }
566
- ] : [];
567
- }
568
- };
569
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/ai-service/parser.ts";
570
- var MixedStreamParser = class {
571
- constructor() {
572
- this.message = new import_async2.Event();
573
- this.block = new import_async2.Event();
574
- this.update = new import_async2.Event();
575
- }
576
- _emitBlock(contentBlock, streamBlock) {
577
- const messageBlock = streamBlock ? mergeMessageBlock(contentBlock, streamBlock) : contentBlock;
578
- if (messageBlock) {
579
- if (messageBlock.type === "text" && messageBlock.text.length === 0) {
580
- return;
581
- }
582
- (0, import_invariant2.invariant)(this._message, void 0, {
583
- F: __dxlog_file3,
584
- L: 46,
585
- S: this,
586
- A: [
587
- "this._message",
588
- ""
589
- ]
590
- });
591
- this._message.content.push(messageBlock);
592
- this.block.emit(messageBlock);
593
- }
594
82
  }
595
- _emitUpdate(contentBlock, streamBlock) {
596
- const messageBlock = mergeMessageBlock(contentBlock, streamBlock);
597
- if (messageBlock) {
598
- messageBlock.pending = true;
599
- this.update.emit(messageBlock);
600
- }
601
- }
602
- /**
603
- * Parse stream until end.
604
- */
605
- async parse(stream) {
606
- const transformer = new StreamTransform();
607
- let contentBlock;
608
- let streamBlock;
609
- const stack = [];
610
- const messagesCollected = [];
611
- for await (const event of stream) {
612
- switch (event.type) {
613
- //
614
- // Messages.
615
- //
616
- case "message_start": {
617
- if (this._message) {
618
- import_log3.log.warn("unexpected message_start", void 0, {
619
- F: __dxlog_file3,
620
- L: 88,
621
- S: this,
622
- C: (f, a) => f(...a)
623
- });
624
- }
625
- this._message = (0, import_echo_schema.createStatic)(import_artifact2.Message, {
626
- role: "assistant",
627
- content: []
83
+ async run(options) {
84
+ const systemTools = [
85
+ (0, import_artifact.defineTool)("system", {
86
+ name: "query_artifacts",
87
+ description: "Query the available artifacts",
88
+ schema: import_effect.Schema.Struct({}),
89
+ execute: async () => {
90
+ return import_artifact.ToolResult.Success({
91
+ artifacts: options.artifacts.map((artifact) => ({
92
+ id: artifact.id,
93
+ name: artifact.name,
94
+ description: artifact.description
95
+ }))
628
96
  });
629
- this.message.emit(this._message);
630
- break;
631
- }
632
- case "message_delta": {
633
- if (!this._message) {
634
- import_log3.log.warn("unexpected message_delta", void 0, {
635
- F: __dxlog_file3,
636
- L: 98,
637
- S: this,
638
- C: (f, a) => f(...a)
639
- });
640
- }
641
- break;
642
97
  }
643
- case "message_stop": {
644
- if (!this._message) {
645
- import_log3.log.warn("unexpected message_stop", void 0, {
646
- F: __dxlog_file3,
647
- L: 106,
648
- S: this,
649
- C: (f, a) => f(...a)
650
- });
651
- } else {
652
- messagesCollected.push(this._message);
653
- this._message = void 0;
654
- }
655
- break;
656
- }
657
- //
658
- // Content blocks.
659
- //
660
- case "content_block_start": {
661
- if (contentBlock) {
662
- import_log3.log.warn("unexpected content_block_start", {
663
- content: contentBlock
664
- }, {
665
- F: __dxlog_file3,
666
- L: 120,
667
- S: this,
668
- C: (f, a) => f(...a)
669
- });
670
- }
671
- contentBlock = event.content;
672
- break;
673
- }
674
- case "content_block_delta": {
675
- if (!contentBlock) {
676
- import_log3.log.warn("unexpected content_block_delta", void 0, {
677
- F: __dxlog_file3,
678
- L: 130,
679
- S: this,
680
- C: (f, a) => f(...a)
98
+ })
99
+ ];
100
+ switch (this._options.operationModel) {
101
+ case "planning":
102
+ systemTools.push((0, import_artifact.defineTool)("system", {
103
+ name: "create_plan",
104
+ description: "Create a plan. Make sure that each step is independent and can be executed solely on the data returned by the previous step. The steps only share the data that is specified in the plan.",
105
+ schema: import_effect.Schema.Struct({
106
+ goal: import_effect.Schema.String.annotations({
107
+ description: "The goal that the plan will achieve."
108
+ }),
109
+ steps: import_effect.Schema.Array(import_effect.Schema.Struct({
110
+ action: import_effect.Schema.String.annotations({
111
+ description: "Complete, detailed action to perform."
112
+ }),
113
+ input: import_effect.Schema.String.annotations({
114
+ description: "The required input data for this step. First step must not require any data."
115
+ }),
116
+ output: import_effect.Schema.String.annotations({
117
+ description: "The output data from this step. This is passed to the next step as input."
118
+ }),
119
+ requiredArtifactIds: import_effect.Schema.Array(import_effect.Schema.String).annotations({
120
+ description: "The ids of the artifacts required to perform this step.",
121
+ examples: [
122
+ [
123
+ "artifact:dxos.org/example/Test"
124
+ ]
125
+ ]
126
+ })
127
+ })).annotations({
128
+ description: "Steps"
129
+ })
130
+ }),
131
+ execute: async ({ goal, steps }) => {
132
+ const missingArtifactIds = steps.flatMap((step) => step.requiredArtifactIds).filter((artifactId) => !options.artifacts.some((artifact) => artifact.id === artifactId));
133
+ if (missingArtifactIds.length > 0) {
134
+ return import_artifact.ToolResult.Error(`One or more artifact ids are invalid: ${missingArtifactIds.join(", ")}`);
135
+ }
136
+ const stepResults = [];
137
+ for (const step of steps) {
138
+ import_log.log.info("executing step", {
139
+ action: step.action
140
+ }, {
141
+ F: __dxlog_file,
142
+ L: 158,
143
+ S: this,
144
+ C: (f, a) => f(...a)
145
+ });
146
+ const session = new _AISession({
147
+ operationModel: "immediate"
148
+ });
149
+ session.message.on((ev) => this.message.emit(ev));
150
+ session.block.on((ev) => this.block.emit(ev));
151
+ session.update.on((ev) => this.update.emit(ev));
152
+ session.streamEvent.on((ev) => this.streamEvent.emit(ev));
153
+ session.userMessage.on((ev) => this.userMessage.emit(ev));
154
+ const messages = await session.run({
155
+ client: options.client,
156
+ artifacts: options.artifacts,
157
+ tools: options.tools,
158
+ history: options.history,
159
+ extensions: options.extensions,
160
+ generationOptions: options.generationOptions,
161
+ requiredArtifactIds: step.requiredArtifactIds.slice(),
162
+ prompt: `
163
+ You are an agent that executes the subtask in a plan.
164
+
165
+ While you know the plan's goal only focus on your subtask
166
+ Use the data from the previous step to complete your task.
167
+ Return all of the data from "output" in your last message.
168
+
169
+ Current step (that you need to complete):
170
+ ${JSON.stringify({
171
+ goal,
172
+ input: step.input,
173
+ output: step.output,
174
+ yourTask: step.action
175
+ }, null, 2)}
176
+
177
+ Previous step results:
178
+ ${JSON.stringify(stepResults.at(-1))}.
179
+ `
180
+ });
181
+ const result = messages.at(-1);
182
+ stepResults.push(result);
183
+ }
184
+ return import_artifact.ToolResult.Success({
185
+ stepResults
681
186
  });
682
- break;
683
187
  }
684
- switch (event.delta.type) {
685
- //
686
- // Text
687
- //
688
- case "text_delta": {
689
- const chunks = transformer.transform(event.delta.text);
690
- for (const chunk of chunks) {
691
- switch (streamBlock?.type) {
692
- //
693
- // XML Fragment.
694
- //
695
- case "tag": {
696
- if (chunk.type === "tag") {
697
- if (chunk.selfClosing) {
698
- streamBlock.content.push(chunk);
699
- } else if (chunk.closing) {
700
- if (stack.length > 0) {
701
- const top = stack.pop();
702
- (0, import_invariant2.invariant)(top && top.type === "tag", void 0, {
703
- F: __dxlog_file3,
704
- L: 154,
705
- S: this,
706
- A: [
707
- "top && top.type === 'tag'",
708
- ""
709
- ]
710
- });
711
- top.content.push(streamBlock);
712
- streamBlock = top;
713
- } else {
714
- this._emitBlock(contentBlock, streamBlock);
715
- streamBlock = void 0;
716
- }
717
- } else {
718
- stack.push(streamBlock);
719
- streamBlock = chunk;
720
- }
721
- } else {
722
- if (streamBlock.content.length === 0) {
723
- streamBlock.content.push(chunk);
724
- } else {
725
- const last = streamBlock.content.at(-1);
726
- (0, import_invariant2.invariant)(last, void 0, {
727
- F: __dxlog_file3,
728
- L: 171,
729
- S: this,
730
- A: [
731
- "last",
732
- ""
733
- ]
734
- });
735
- if (last.type === "text") {
736
- last.content += chunk.content;
737
- } else {
738
- streamBlock.content.push(chunk);
739
- }
740
- }
741
- }
742
- break;
743
- }
744
- //
745
- // Text Fragment.
746
- //
747
- case "text": {
748
- if (chunk.type === "tag") {
749
- this._emitBlock(contentBlock, streamBlock);
750
- if (chunk.selfClosing) {
751
- this._emitBlock(contentBlock, chunk);
752
- streamBlock = void 0;
753
- } else {
754
- streamBlock = chunk;
755
- }
756
- } else {
757
- streamBlock.content += chunk.content;
758
- }
759
- break;
760
- }
761
- //
762
- // No current chunk.
763
- //
764
- default: {
765
- if (chunk.type === "tag" && chunk.selfClosing) {
766
- this._emitBlock(contentBlock, chunk);
767
- } else {
768
- streamBlock = chunk;
769
- }
770
- }
771
- }
772
- }
773
- break;
188
+ }));
189
+ break;
190
+ case "immediate":
191
+ systemTools.push((0, import_artifact.defineTool)("system", {
192
+ name: "require_artifacts",
193
+ description: "Require the use of specific artifacts. This will allow the model to interact with artifacts and use their tools.",
194
+ schema: import_effect.Schema.Struct({
195
+ artifactIds: import_effect.Schema.Array(import_effect.Schema.String).annotations({
196
+ description: "The ids of the artifacts to require",
197
+ examples: [
198
+ [
199
+ "artifact:dxos.org/example/Test"
200
+ ]
201
+ ]
202
+ })
203
+ }),
204
+ execute: async ({ artifactIds }) => {
205
+ const missingArtifactIds = artifactIds.filter((artifactId) => !options.artifacts.some((artifact) => artifact.id === artifactId));
206
+ if (missingArtifactIds.length > 0) {
207
+ return import_artifact.ToolResult.Error(`One or more artifact ids are invalid: ${missingArtifactIds.join(", ")}`);
774
208
  }
775
- //
776
- // JSON
777
- //
778
- case "input_json_delta": {
779
- if (!contentBlock) {
780
- import_log3.log.warn("unexpected input_json_delta", {
781
- contentBlock
782
- }, {
783
- F: __dxlog_file3,
784
- L: 221,
785
- S: this,
786
- C: (f, a) => f(...a)
787
- });
788
- break;
789
- }
790
- if (streamBlock?.type === "json") {
791
- streamBlock.content += event.delta.partial_json;
792
- } else {
793
- streamBlock = {
794
- type: "json",
795
- content: event.delta.partial_json
796
- };
797
- }
798
- break;
209
+ for (const artifactId of artifactIds) {
210
+ requiredArtifactIds.add(artifactId);
799
211
  }
212
+ return import_artifact.ToolResult.Success({});
800
213
  }
801
- break;
802
- }
803
- case "content_block_stop": {
804
- if (!contentBlock) {
805
- import_log3.log.warn("unexpected content_block_stop", void 0, {
806
- F: __dxlog_file3,
807
- L: 238,
808
- S: this,
809
- C: (f, a) => f(...a)
810
- });
811
- break;
812
- }
813
- this._emitBlock(contentBlock, streamBlock);
814
- contentBlock = void 0;
815
- streamBlock = void 0;
816
- break;
817
- }
818
- }
819
- if (contentBlock && streamBlock) {
820
- this._emitUpdate(contentBlock, streamBlock);
821
- }
214
+ }));
215
+ break;
822
216
  }
823
- return messagesCollected;
824
- }
825
- };
826
- var mergeMessageBlock = (contentBlock, streamBlock) => {
827
- (0, import_log3.log)("mergeMessageBlock", {
828
- contentBlock,
829
- streamBlock
830
- }, {
831
- F: __dxlog_file3,
832
- L: 265,
833
- S: void 0,
834
- C: (f, a) => f(...a)
835
- });
836
- switch (streamBlock.type) {
837
- //
838
- // Text
839
- //
840
- case "text": {
841
- return {
842
- ...contentBlock,
843
- type: "text",
844
- text: streamBlock.content
845
- };
846
- }
847
- //
848
- // XML
849
- //
850
- case "tag": {
851
- switch (streamBlock.tag) {
852
- case "cot": {
853
- const content = streamBlock.content.map((block) => {
854
- switch (block.type) {
855
- case "text":
856
- return block.content;
857
- default:
858
- return null;
859
- }
860
- }).filter(import_util.isNotFalsy).join("\n");
861
- return {
862
- ...contentBlock,
217
+ this._history = [
218
+ ...options.history
219
+ ];
220
+ this._pending = [
221
+ (0, import_echo_schema.createStatic)(import_artifact.Message, {
222
+ role: "user",
223
+ content: [
224
+ {
863
225
  type: "text",
864
- disposition: "cot",
865
- text: content
866
- };
867
- }
868
- case "artifact": {
869
- const { attributes } = streamBlock;
870
- return {
871
- type: "json",
872
- disposition: "artifact",
873
- json: JSON.stringify(attributes)
874
- };
875
- }
876
- case "suggest": {
877
- if (streamBlock.content.length === 1 && streamBlock.content[0].type === "text") {
878
- return {
879
- type: "json",
880
- disposition: "suggest",
881
- json: JSON.stringify({
882
- text: streamBlock.content[0].content
883
- })
884
- };
226
+ text: options.prompt
885
227
  }
886
- break;
887
- }
888
- case "select": {
889
- return {
890
- type: "json",
891
- disposition: "select",
892
- json: JSON.stringify({
893
- options: streamBlock.content.flatMap((content) => content.type === "tag" && content.content.length === 1 && content.content[0].type === "text" ? [
894
- content.content[0].content
895
- ] : [])
896
- })
897
- };
898
- }
899
- case "tool-list": {
900
- return {
901
- type: "json",
902
- disposition: "tool_list",
903
- json: JSON.stringify({})
904
- };
905
- }
906
- }
907
- break;
908
- }
909
- //
910
- // JSON
911
- //
912
- case "json": {
913
- switch (contentBlock.type) {
914
- case "tool_use": {
915
- return {
916
- ...contentBlock,
917
- input: (0, import_util.safeParseJson)(streamBlock.content) ?? {}
918
- };
919
- }
920
- }
921
- return {
922
- ...contentBlock,
923
- type: "json",
924
- disposition: "artifact",
925
- json: streamBlock.content
926
- };
927
- }
928
- }
929
- };
930
- var createArtifactElement = (id) => `<artifact id=${id} />`;
931
- var LLMModel = import_schema2.Schema.Literal(...DEFAULT_LLM_MODELS);
932
- var ToolTypes = Object.freeze({
933
- // TODO(dmaretskyi): Not implemented yet.
934
- // DatabaseQuery: 'database_query',
935
- TextToImage: "text_to_image"
936
- });
937
- var GenerateRequest = import_schema2.Schema.Struct({
938
- spaceId: import_schema2.Schema.optional(import_artifact3.SpaceIdSchema),
939
- threadId: import_schema2.Schema.optional(import_echo_schema2.ObjectId),
940
- /**
941
- * Preferred model or system default.
942
- */
943
- model: import_schema2.Schema.optional(LLMModel),
944
- /**
945
- * Tools available for the LLM.
946
- */
947
- tools: import_schema2.Schema.optional(import_schema2.Schema.Array(import_artifact3.Tool).pipe(import_schema2.Schema.mutable)),
948
- /**
949
- * System instructions to the LLM.
950
- */
951
- systemPrompt: import_schema2.Schema.optional(import_schema2.Schema.String),
952
- /**
953
- * History of messages to include in the context window.
954
- */
955
- // TODO(burdon): Rename messages.
956
- history: import_schema2.Schema.optional(import_schema2.Schema.Array(import_artifact3.Message)),
957
- /**
958
- * Current request.
959
- */
960
- prompt: import_schema2.Schema.optional(import_artifact3.Message)
961
- });
962
- var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/conversation/conversation.ts";
963
- var runLLM = async (params) => {
964
- let conversationResult = null;
965
- const history = params.history ?? [];
966
- const generate = async () => {
967
- (0, import_log4.log)("llm generate", {
968
- tools: params.tools
969
- }, {
970
- F: __dxlog_file4,
971
- L: 44,
972
- S: void 0,
973
- C: (f, a) => f(...a)
974
- });
975
- const beginTs = Date.now();
976
- const stream = await params.client.exec({
977
- model: params.model,
978
- spaceId: params.spaceId,
979
- threadId: params.threadId,
980
- history,
981
- systemPrompt: params.system,
982
- tools: params.tools
983
- });
984
- for await (const event of stream) {
985
- params.logger?.(event);
986
- }
987
- await stream.complete();
988
- const messages = [];
989
- const message = messages.at(-1);
990
- (0, import_invariant3.invariant)(message, void 0, {
991
- F: __dxlog_file4,
992
- L: 63,
993
- S: void 0,
994
- A: [
995
- "message",
996
- ""
997
- ]
998
- });
999
- (0, import_log4.log)("llm result", {
1000
- time: Date.now() - beginTs,
1001
- message
1002
- }, {
1003
- F: __dxlog_file4,
1004
- L: 65,
1005
- S: void 0,
1006
- C: (f, a) => f(...a)
1007
- });
1008
- (0, import_invariant3.invariant)(message, void 0, {
1009
- F: __dxlog_file4,
1010
- L: 66,
1011
- S: void 0,
1012
- A: [
1013
- "message",
1014
- ""
1015
- ]
1016
- });
1017
- params.logger?.({
1018
- type: "message",
1019
- message
1020
- });
1021
- history.push(...messages.map((message2) => ({
1022
- ...message2,
1023
- content: message2.content.filter((block) => block.type !== "image")
1024
- })));
1025
- const isToolUse2 = message.content.at(-1)?.type === "tool_use";
1026
- if (isToolUse2) {
1027
- const toolCalls = message.content.filter((block) => block.type === "tool_use");
1028
- (0, import_invariant3.invariant)(toolCalls.length === 1, void 0, {
1029
- F: __dxlog_file4,
1030
- L: 77,
1031
- S: void 0,
1032
- A: [
1033
- "toolCalls.length === 1",
1034
- ""
1035
228
  ]
1036
- });
1037
- const toolCall = toolCalls[0];
1038
- const tool = params.tools.find((tool2) => tool2.name === toolCall.name);
1039
- if (!tool) {
1040
- throw new Error(`Tool not found: ${toolCall.name}`);
1041
- }
1042
- (0, import_invariant3.invariant)(tool.execute, void 0, {
1043
- F: __dxlog_file4,
1044
- L: 84,
1045
- S: void 0,
1046
- A: [
1047
- "tool.execute",
1048
- ""
1049
- ]
1050
- });
1051
- const toolResult = await tool.execute(toolCall.input);
1052
- switch (toolResult.kind) {
1053
- case "error": {
1054
- import_log4.log.warn("tool error", {
1055
- message: toolResult.message
1056
- }, {
1057
- F: __dxlog_file4,
1058
- L: 88,
1059
- S: void 0,
1060
- C: (f, a) => f(...a)
1061
- });
1062
- const resultMessage = {
1063
- id: import_echo_schema3.ObjectId.random(),
1064
- spaceId: params.spaceId,
1065
- threadId: params.threadId,
1066
- role: "user",
1067
- content: [
1068
- {
1069
- type: "tool_result",
1070
- toolUseId: toolCall.id,
1071
- content: toolResult.message,
1072
- isError: true
1073
- }
1074
- ]
1075
- };
1076
- params.logger?.({
1077
- type: "message",
1078
- message: resultMessage
1079
- });
1080
- history.push(resultMessage);
1081
- return true;
1082
- }
1083
- case "success": {
1084
- (0, import_log4.log)("tool success", {
1085
- result: toolResult.result
1086
- }, {
1087
- F: __dxlog_file4,
1088
- L: 110,
1089
- S: void 0,
229
+ })
230
+ ];
231
+ this.userMessage.emit(this._pending.at(-1));
232
+ this._stream = void 0;
233
+ let error;
234
+ const requiredArtifactIds = new Set(options.requiredArtifactIds ?? []);
235
+ try {
236
+ let more = false;
237
+ do {
238
+ const tools = [
239
+ ...systemTools,
240
+ ...options.tools,
241
+ ...options.artifacts.filter((artifact) => requiredArtifactIds.has(artifact.id)).flatMap((artifact) => artifact.tools)
242
+ ];
243
+ (0, import_log.log)("request", {
244
+ pending: this._pending.length,
245
+ history: this._history.length,
246
+ tools: tools.map((tool) => tool.name)
247
+ }, {
248
+ F: __dxlog_file,
249
+ L: 260,
250
+ S: this,
251
+ C: (f, a) => f(...a)
252
+ });
253
+ this._stream = await options.client.exec({
254
+ ...options.generationOptions ?? {},
255
+ // TODO(burdon): Rename messages or separate history/message.
256
+ history: [
257
+ ...this._history,
258
+ ...this._pending
259
+ ],
260
+ tools,
261
+ systemPrompt: createBaseInstructions({
262
+ availableArtifacts: Array.from(requiredArtifactIds),
263
+ operationModel: this._options.operationModel
264
+ })
265
+ });
266
+ await this._parser.parse(this._stream);
267
+ await this._stream.complete();
268
+ (0, import_log.log)("response", {
269
+ pending: this._pending
270
+ }, {
271
+ F: __dxlog_file,
272
+ L: 283,
273
+ S: this,
274
+ C: (f, a) => f(...a)
275
+ });
276
+ more = false;
277
+ const message = this._pending.at(-1);
278
+ (0, import_invariant.invariant)(message, void 0, {
279
+ F: __dxlog_file,
280
+ L: 288,
281
+ S: this,
282
+ A: [
283
+ "message",
284
+ ""
285
+ ]
286
+ });
287
+ if ((0, import_chunk_A44NGSCM.isToolUse)(message)) {
288
+ (0, import_log.log)("tool request...", void 0, {
289
+ F: __dxlog_file,
290
+ L: 290,
291
+ S: this,
1090
292
  C: (f, a) => f(...a)
1091
293
  });
1092
- const resultMessage = {
1093
- id: import_echo_schema3.ObjectId.random(),
1094
- spaceId: params.spaceId,
1095
- threadId: params.threadId,
1096
- role: "user",
1097
- content: [
1098
- {
1099
- type: "tool_result",
1100
- toolUseId: toolCall.id,
1101
- content: JSON.stringify(toolResult.result)
1102
- }
1103
- ]
1104
- };
1105
- params.logger?.({
1106
- type: "message",
1107
- message: resultMessage
294
+ const response = await (0, import_chunk_A44NGSCM.runTools)({
295
+ message: this._pending.at(-1),
296
+ tools,
297
+ extensions: options.extensions ?? {}
1108
298
  });
1109
- history.push(resultMessage);
1110
- return true;
1111
- }
1112
- case "break": {
1113
- (0, import_log4.log)("tool break", {
1114
- result: toolResult.result
299
+ (0, import_log.log)("tool response", {
300
+ response
1115
301
  }, {
1116
- F: __dxlog_file4,
1117
- L: 131,
1118
- S: void 0,
302
+ F: __dxlog_file,
303
+ L: 297,
304
+ S: this,
1119
305
  C: (f, a) => f(...a)
1120
306
  });
1121
- conversationResult = toolResult.result;
1122
- return false;
1123
- }
1124
- }
1125
- }
1126
- return false;
1127
- };
1128
- while (await generate()) {
1129
- }
1130
- return {
1131
- result: conversationResult,
1132
- history
1133
- };
1134
- };
1135
- var isToolUse = (message) => {
1136
- const block = message.content.at(-1);
1137
- (0, import_invariant3.invariant)(block, void 0, {
1138
- F: __dxlog_file4,
1139
- L: 152,
1140
- S: void 0,
1141
- A: [
1142
- "block",
1143
- ""
1144
- ]
1145
- });
1146
- return block.type === "tool_use";
1147
- };
1148
- var runTools = async ({ message, tools, extensions }) => {
1149
- const toolCalls = message.content.filter((block) => block.type === "tool_use");
1150
- (0, import_invariant3.invariant)(toolCalls.length === 1, void 0, {
1151
- F: __dxlog_file4,
1152
- L: 174,
1153
- S: void 0,
1154
- A: [
1155
- "toolCalls.length === 1",
1156
- ""
1157
- ]
1158
- });
1159
- const toolCall = toolCalls[0];
1160
- const tool = tools.find((tool2) => tool2.name === toolCall.name);
1161
- if (!tool) {
1162
- throw new Error(`Tool not found: ${toolCall.name}`);
1163
- }
1164
- let toolResult;
1165
- try {
1166
- (0, import_invariant3.invariant)(tool.execute, void 0, {
1167
- F: __dxlog_file4,
1168
- L: 183,
1169
- S: void 0,
1170
- A: [
1171
- "tool.execute",
1172
- ""
1173
- ]
1174
- });
1175
- toolResult = await tool.execute(toolCall.input, {
1176
- extensions
1177
- });
1178
- } catch (error) {
1179
- (0, import_log4.log)("tool error", {
1180
- error
1181
- }, {
1182
- F: __dxlog_file4,
1183
- L: 186,
1184
- S: void 0,
1185
- C: (f, a) => f(...a)
1186
- });
1187
- toolResult = import_artifact4.ToolResult.Error(error.message);
1188
- }
1189
- switch (toolResult.kind) {
1190
- case "error": {
1191
- (0, import_log4.log)("tool error", {
1192
- message: toolResult.message
1193
- }, {
1194
- F: __dxlog_file4,
1195
- L: 192,
1196
- S: void 0,
1197
- C: (f, a) => f(...a)
1198
- });
1199
- const resultMessage = (0, import_echo_schema3.createStatic)(import_artifact4.Message, {
1200
- role: "user",
1201
- content: [
1202
- {
1203
- type: "tool_result",
1204
- toolUseId: toolCall.id,
1205
- content: toolResult.message,
1206
- isError: true
307
+ switch (response.type) {
308
+ case "continue": {
309
+ this._pending = [
310
+ ...this._pending,
311
+ response.message
312
+ ];
313
+ this.userMessage.emit(response.message);
314
+ more = true;
315
+ break;
316
+ }
1207
317
  }
1208
- ]
1209
- });
1210
- return {
1211
- type: "continue",
1212
- message: resultMessage
1213
- };
1214
- }
1215
- case "success": {
1216
- (0, import_log4.log)("tool success", {
1217
- result: toolResult.result
1218
- }, {
1219
- F: __dxlog_file4,
1220
- L: 213,
1221
- S: void 0,
318
+ }
319
+ } while (more);
320
+ } catch (err) {
321
+ import_log.log.catch(err, void 0, {
322
+ F: __dxlog_file,
323
+ L: 309,
324
+ S: this,
1222
325
  C: (f, a) => f(...a)
1223
326
  });
1224
- const resultMessage = (0, import_echo_schema3.createStatic)(import_artifact4.Message, {
1225
- role: "user",
1226
- content: [
1227
- {
1228
- type: "tool_result",
1229
- toolUseId: toolCall.id,
1230
- content: typeof toolResult.result === "string" ? toolResult.result : JSON.stringify(toolResult.result) ?? ""
1231
- }
1232
- ]
1233
- });
1234
- return {
1235
- type: "continue",
1236
- message: resultMessage
1237
- };
327
+ if (err instanceof Error && err.message.includes("Overloaded")) {
328
+ error = new AIServiecOverloadedError("AI service overloaded", {
329
+ cause: err
330
+ });
331
+ } else {
332
+ error = new Error("AI service error", {
333
+ cause: err
334
+ });
335
+ }
336
+ } finally {
337
+ this._stream = void 0;
1238
338
  }
1239
- case "break": {
1240
- (0, import_log4.log)("tool break", {
1241
- result: toolResult.result
1242
- }, {
1243
- F: __dxlog_file4,
1244
- L: 233,
1245
- S: void 0,
1246
- C: (f, a) => f(...a)
1247
- });
1248
- return {
1249
- type: "break",
1250
- result: toolResult.result
1251
- };
339
+ if (error) {
340
+ throw error;
1252
341
  }
342
+ return this._pending;
1253
343
  }
344
+ abort() {
345
+ this._stream?.abort();
346
+ }
347
+ };
348
+ _ts_decorate([
349
+ import_async.synchronized
350
+ ], AISession.prototype, "run", null);
351
+ var createBaseInstructions = ({ availableArtifacts, operationModel }) => `
352
+ You are a friendly, advanced AI assistant capable of creating and managing artifacts from available data and tools.
353
+ Your task is to process user commands and questions and decide how best to respond.
354
+ In some cases, you will need to create or reference data objects called artifacts.
355
+
356
+ Follow these guidelines carefully:
357
+
358
+ Decision-making:
359
+
360
+ - Analyze the structure and type of the content in the user's message.
361
+ - Can you complete the task using the available artifacts?
362
+ - If you can't complete the task using the available artifacts, query the list of available artifacts using the appropriate tool.
363
+ - Identify which artifacts are relevant to the user's request.
364
+ ${operationModel === "planning" ? `
365
+ - Break down the user's request into a step-by-step plan that you will use to execute the user's request, the plan items should include artifacts which will be used to perform the actions.
366
+ ` : `
367
+ - Are the required artifacts already available?
368
+ - If not, select which artifact(s) will be the most relevant and require them using the require_artifacts tool.
369
+ - The require'd artifact tools will be available for use after require.
370
+ `}
371
+
372
+ Artifacts already required: ${availableArtifacts.join("\n")}
373
+ `;
374
+ var AIServiecOverloadedError = class extends import_effect.Schema.TaggedError()("AIServiecOverloadedError", {}) {
1254
375
  };
1255
376
  // Annotate the CommonJS export names for ESM import in node:
1256
377
  0 && (module.exports = {
1257
- AIServiceClientImpl,
1258
- DEFAULT_LLM_MODEL,
1259
- DEFAULT_LLM_MODELS,
378
+ AIServiceEdgeClient,
379
+ AIServiecOverloadedError,
380
+ AISession,
381
+ DEFAULT_EDGE_MODEL,
382
+ DEFAULT_EDGE_MODELS,
383
+ DEFAULT_OLLAMA_ENDPOINT,
384
+ DEFAULT_OLLAMA_MODEL,
385
+ DEFAULT_OLLAMA_MODELS,
1260
386
  EchoDataSource,
1261
387
  GenerateRequest,
1262
- GenerationStream,
388
+ GenerationStreamImpl,
1263
389
  LLMModel,
390
+ MessageCollector,
1264
391
  MixedStreamParser,
392
+ OllamaClient,
1265
393
  StreamTransform,
1266
394
  ToolTypes,
1267
395
  createArtifactElement,
1268
396
  createGenerationStream,
397
+ emitMessageAsEvents,
1269
398
  executeQuery,
1270
399
  executeQueryPlan,
1271
400
  formatInferredRelationshipLabel,