@dxos/assistant 0.8.1-main.ba2dec9 → 0.8.1-staging.31c3ee1

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 (75) hide show
  1. package/dist/lib/browser/chunk-PND5VR6D.mjs +2315 -0
  2. package/dist/lib/browser/chunk-PND5VR6D.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +313 -1631
  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 +20 -6
  7. package/dist/lib/browser/testing/index.mjs.map +4 -4
  8. package/dist/lib/node/chunk-IBOBRBDF.cjs +2326 -0
  9. package/dist/lib/node/chunk-IBOBRBDF.cjs.map +7 -0
  10. package/dist/lib/node/index.cjs +321 -1629
  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 +22 -11
  14. package/dist/lib/node/testing/index.cjs.map +4 -4
  15. package/dist/lib/node-esm/chunk-DCQJOLSR.mjs +2317 -0
  16. package/dist/lib/node-esm/chunk-DCQJOLSR.mjs.map +7 -0
  17. package/dist/lib/node-esm/index.mjs +313 -1631
  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 +20 -6
  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.map +1 -1
  25. package/dist/types/src/ai-service/message-collector.d.ts +16 -16
  26. package/dist/types/src/ai-service/message-collector.d.ts.map +1 -1
  27. package/dist/types/src/ai-service/ollama-client.d.ts +1 -5
  28. package/dist/types/src/ai-service/ollama-client.d.ts.map +1 -1
  29. package/dist/types/src/ai-service/parser.d.ts +8 -8
  30. package/dist/types/src/ai-service/parser.d.ts.map +1 -1
  31. package/dist/types/src/ai-service/types.d.ts +90 -156
  32. package/dist/types/src/ai-service/types.d.ts.map +1 -1
  33. package/dist/types/src/conversation/conversation.d.ts +8 -8
  34. package/dist/types/src/conversation/conversation.d.ts.map +1 -1
  35. package/dist/types/src/index.d.ts +1 -0
  36. package/dist/types/src/index.d.ts.map +1 -1
  37. package/dist/types/src/session/index.d.ts +2 -0
  38. package/dist/types/src/session/index.d.ts.map +1 -0
  39. package/dist/types/src/session/session.d.ts +216 -0
  40. package/dist/types/src/session/session.d.ts.map +1 -0
  41. package/dist/types/src/session/session.test.d.ts +2 -0
  42. package/dist/types/src/session/session.test.d.ts.map +1 -0
  43. package/dist/types/src/testing/index.d.ts +1 -0
  44. package/dist/types/src/testing/index.d.ts.map +1 -1
  45. package/dist/types/src/testing/ollama.d.ts +6 -0
  46. package/dist/types/src/testing/ollama.d.ts.map +1 -0
  47. package/package.json +13 -13
  48. package/src/ai-service/client.test.ts +14 -14
  49. package/src/ai-service/defs.ts +18 -3
  50. package/src/ai-service/edge-client.ts +3 -2
  51. package/src/ai-service/ollama-client.ts +50 -24
  52. package/src/ai-service/parser.ts +4 -2
  53. package/src/ai-service/stream.test.ts +28 -4
  54. package/src/ai-service/types.ts +2 -2
  55. package/src/bin/chat.ts +3 -3
  56. package/src/conversation/conversation.test.ts +3 -3
  57. package/src/conversation/conversation.ts +17 -1
  58. package/src/cypher/query-executor.test.ts +3 -3
  59. package/src/cypher/schema.test.ts +1 -1
  60. package/src/index.ts +1 -0
  61. package/src/session/index.ts +5 -0
  62. package/src/session/session.test.ts +257 -0
  63. package/src/session/session.ts +375 -0
  64. package/src/testing/artifacts.test.ts +6 -6
  65. package/src/testing/index.ts +1 -0
  66. package/src/testing/object-extraction.test.ts +2 -2
  67. package/src/testing/ollama.ts +15 -0
  68. package/src/testing/query.test.ts +5 -5
  69. package/src/testing/test-schema.ts +5 -5
  70. package/dist/lib/browser/chunk-AXUUNA7O.mjs +0 -565
  71. package/dist/lib/browser/chunk-AXUUNA7O.mjs.map +0 -7
  72. package/dist/lib/node/chunk-QPHGQINW.cjs +0 -577
  73. package/dist/lib/node/chunk-QPHGQINW.cjs.map +0 -7
  74. package/dist/lib/node-esm/chunk-GOVFOBIB.mjs +0 -567
  75. package/dist/lib/node-esm/chunk-GOVFOBIB.mjs.map +0 -7
@@ -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,1680 +15,374 @@ 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
- AIServiceEdgeClient: () => AIServiceEdgeClient,
32
- DEFAULT_LLM_MODEL: () => DEFAULT_LLM_MODEL,
33
- DEFAULT_LLM_MODELS: () => DEFAULT_LLM_MODELS,
34
- EchoDataSource: () => import_chunk_QPHGQINW.EchoDataSource,
35
- GenerateRequest: () => GenerateRequest,
36
- GenerationStreamImpl: () => GenerationStreamImpl,
37
- LLMModel: () => LLMModel,
38
- MessageCollector: () => MessageCollector,
39
- MixedStreamParser: () => MixedStreamParser,
40
- OllamaClient: () => OllamaClient,
41
- StreamTransform: () => StreamTransform,
42
- ToolTypes: () => ToolTypes,
43
- createArtifactElement: () => createArtifactElement,
44
- createGenerationStream: () => createGenerationStream,
45
- emitMessageAsEvents: () => emitMessageAsEvents,
46
- executeQuery: () => import_chunk_QPHGQINW.executeQuery,
47
- executeQueryPlan: () => import_chunk_QPHGQINW.executeQueryPlan,
48
- formatInferredRelationshipLabel: () => import_chunk_QPHGQINW.formatInferredRelationshipLabel,
49
- formatJsonSchemaForLLM: () => import_chunk_QPHGQINW.formatJsonSchemaForLLM,
50
- formatNodeLabel: () => import_chunk_QPHGQINW.formatNodeLabel,
51
- isReference: () => import_chunk_QPHGQINW.isReference,
52
- isToolUse: () => isToolUse,
53
- mergeMessageBlock: () => mergeMessageBlock,
54
- parseCypherQuery: () => import_chunk_QPHGQINW.parseCypherQuery,
55
- runLLM: () => runLLM,
56
- runTools: () => runTools
21
+ AIServiceEdgeClient: () => import_chunk_IBOBRBDF.AIServiceEdgeClient,
22
+ AIServiecOverloadedError: () => AIServiecOverloadedError,
23
+ AISession: () => AISession,
24
+ DEFAULT_EDGE_MODEL: () => import_chunk_IBOBRBDF.DEFAULT_EDGE_MODEL,
25
+ DEFAULT_EDGE_MODELS: () => import_chunk_IBOBRBDF.DEFAULT_EDGE_MODELS,
26
+ DEFAULT_OLLAMA_ENDPOINT: () => import_chunk_IBOBRBDF.DEFAULT_OLLAMA_ENDPOINT,
27
+ DEFAULT_OLLAMA_MODEL: () => import_chunk_IBOBRBDF.DEFAULT_OLLAMA_MODEL,
28
+ DEFAULT_OLLAMA_MODELS: () => import_chunk_IBOBRBDF.DEFAULT_OLLAMA_MODELS,
29
+ EchoDataSource: () => import_chunk_IBOBRBDF.EchoDataSource,
30
+ GenerateRequest: () => import_chunk_IBOBRBDF.GenerateRequest,
31
+ GenerationStreamImpl: () => import_chunk_IBOBRBDF.GenerationStreamImpl,
32
+ LLMModel: () => import_chunk_IBOBRBDF.LLMModel,
33
+ MessageCollector: () => import_chunk_IBOBRBDF.MessageCollector,
34
+ MixedStreamParser: () => import_chunk_IBOBRBDF.MixedStreamParser,
35
+ OllamaClient: () => import_chunk_IBOBRBDF.OllamaClient,
36
+ StreamTransform: () => import_chunk_IBOBRBDF.StreamTransform,
37
+ ToolTypes: () => import_chunk_IBOBRBDF.ToolTypes,
38
+ createArtifactElement: () => import_chunk_IBOBRBDF.createArtifactElement,
39
+ createGenerationStream: () => import_chunk_IBOBRBDF.createGenerationStream,
40
+ emitMessageAsEvents: () => import_chunk_IBOBRBDF.emitMessageAsEvents,
41
+ executeQuery: () => import_chunk_IBOBRBDF.executeQuery,
42
+ executeQueryPlan: () => import_chunk_IBOBRBDF.executeQueryPlan,
43
+ formatInferredRelationshipLabel: () => import_chunk_IBOBRBDF.formatInferredRelationshipLabel,
44
+ formatJsonSchemaForLLM: () => import_chunk_IBOBRBDF.formatJsonSchemaForLLM,
45
+ formatNodeLabel: () => import_chunk_IBOBRBDF.formatNodeLabel,
46
+ isReference: () => import_chunk_IBOBRBDF.isReference,
47
+ isToolUse: () => import_chunk_IBOBRBDF.isToolUse,
48
+ mergeMessageBlock: () => import_chunk_IBOBRBDF.mergeMessageBlock,
49
+ parseCypherQuery: () => import_chunk_IBOBRBDF.parseCypherQuery,
50
+ runLLM: () => import_chunk_IBOBRBDF.runLLM,
51
+ runTools: () => import_chunk_IBOBRBDF.runTools
57
52
  });
58
53
  module.exports = __toCommonJS(node_exports);
59
- var import_chunk_QPHGQINW = require("./chunk-QPHGQINW.cjs");
54
+ var import_chunk_IBOBRBDF = require("./chunk-IBOBRBDF.cjs");
60
55
  var import_effect = require("effect");
61
56
  var import_artifact = require("@dxos/artifact");
62
- var import_invariant = require("@dxos/invariant");
63
- var import_log = require("@dxos/log");
64
57
  var import_async = require("@dxos/async");
65
- var import_log2 = require("@dxos/log");
66
- var import_artifact2 = require("@dxos/artifact");
67
58
  var import_echo_schema = require("@dxos/echo-schema");
68
- var import_invariant2 = require("@dxos/invariant");
69
- var import_log3 = require("@dxos/log");
70
- var import_effect2 = require("effect");
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
- var import_artifact5 = require("@dxos/artifact");
78
- var import_async2 = require("@dxos/async");
79
- var import_echo_schema4 = require("@dxos/echo-schema");
80
- var import_invariant4 = require("@dxos/invariant");
81
- var import_log5 = require("@dxos/log");
82
- var import_util = require("@dxos/util");
83
- var P = __toESM(require("parsimmon"));
84
- var DEFAULT_LLM_MODEL = "@anthropic/claude-3-5-sonnet-20241022";
85
- var DEFAULT_LLM_MODELS = [
86
- // AI Gateway.
87
- // https://developers.cloudflare.com/ai-gateway
88
- "@anthropic/claude-3-5-haiku-20241022",
89
- "@anthropic/claude-3-5-sonnet-20241022",
90
- "@anthropic/claude-3-7-sonnet-20250219",
91
- // Workers AI.
92
- // https://developers.cloudflare.com/workers-ai/models
93
- "@cf/deepseek-ai/deepseek-r1-distill-qwen-32b",
94
- "@hf/nousresearch/hermes-2-pro-mistral-7b",
95
- "@ollama/llama-3-1-nemotron-70b-instruct",
96
- "@ollama/llama-3-1-nemotron-mini-4b-instruct",
97
- "@ollama/llama-3-2-3b"
98
- ];
99
- async function* iterSSEMessages(response, controller) {
100
- if (!response.body) {
101
- controller.abort();
102
- throw new Error("Attempted to iterate over a response with no body");
103
- }
104
- const sseDecoder = new SSEDecoder();
105
- const lineDecoder = new LineDecoder();
106
- const iter = readableStreamAsyncIterable(response.body);
107
- for await (const sseChunk of iterSSEChunks(iter)) {
108
- for (const line of lineDecoder.decode(sseChunk)) {
109
- const sse = sseDecoder.decode(line);
110
- if (sse) {
111
- yield sse;
112
- }
113
- }
114
- }
115
- for (const line of lineDecoder.flush()) {
116
- const sse = sseDecoder.decode(line);
117
- if (sse) {
118
- yield sse;
119
- }
120
- }
121
- }
122
- async function* iterSSEChunks(iterator) {
123
- let data = new Uint8Array();
124
- for await (const chunk of iterator) {
125
- if (chunk == null) {
126
- continue;
127
- }
128
- const binaryChunk = chunk instanceof ArrayBuffer ? new Uint8Array(chunk) : typeof chunk === "string" ? new TextEncoder().encode(chunk) : chunk;
129
- const newData = new Uint8Array(data.length + binaryChunk.length);
130
- newData.set(data);
131
- newData.set(binaryChunk, data.length);
132
- data = newData;
133
- let patternIndex;
134
- while ((patternIndex = findDoubleNewlineIndex(data)) !== -1) {
135
- yield data.slice(0, patternIndex);
136
- data = data.slice(patternIndex);
137
- }
138
- }
139
- if (data.length > 0) {
140
- yield data;
141
- }
142
- }
143
- var findDoubleNewlineIndex = (buffer) => {
144
- const newline = 10;
145
- const carriage = 13;
146
- for (let i = 0; i < buffer.length - 2; i++) {
147
- if (buffer[i] === newline && buffer[i + 1] === newline) {
148
- return i + 2;
149
- }
150
- if (buffer[i] === carriage && buffer[i + 1] === carriage) {
151
- return i + 2;
152
- }
153
- if (buffer[i] === carriage && buffer[i + 1] === newline && i + 3 < buffer.length && buffer[i + 2] === carriage && buffer[i + 3] === newline) {
154
- return i + 4;
155
- }
156
- }
157
- return -1;
158
- };
159
- var SSEDecoder = class {
160
- constructor() {
161
- this.event = null;
162
- this.data = [];
163
- this.chunks = [];
164
- }
165
- decode(line) {
166
- if (line.endsWith("\r")) {
167
- line = line.substring(0, line.length - 1);
168
- }
169
- if (!line) {
170
- if (!this.event && !this.data.length) {
171
- return null;
172
- }
173
- const sse = {
174
- event: this.event,
175
- data: this.data.join("\n"),
176
- raw: this.chunks
177
- };
178
- this.event = null;
179
- this.data = [];
180
- this.chunks = [];
181
- return sse;
182
- }
183
- this.chunks.push(line);
184
- if (line.startsWith(":")) {
185
- return null;
186
- }
187
- let [fieldname, _, value] = partition(line, ":");
188
- if (value.startsWith(" ")) {
189
- value = value.substring(1);
190
- }
191
- if (fieldname === "event") {
192
- this.event = value;
193
- } else if (fieldname === "data") {
194
- this.data.push(value);
195
- }
196
- return null;
197
- }
198
- };
199
- var partition = (str, delimiter) => {
200
- const index = str.indexOf(delimiter);
201
- if (index !== -1) {
202
- return [
203
- str.substring(0, index),
204
- delimiter,
205
- str.substring(index + delimiter.length)
206
- ];
207
- }
208
- return [
209
- str,
210
- "",
211
- ""
212
- ];
213
- };
214
- function readableStreamAsyncIterable(stream) {
215
- if (stream[Symbol.asyncIterator]) {
216
- return stream;
217
- }
218
- const reader = stream.getReader();
219
- return {
220
- next: async () => {
221
- try {
222
- const result = await reader.read();
223
- if (result?.done) {
224
- reader.releaseLock();
225
- }
226
- return result;
227
- } catch (e) {
228
- reader.releaseLock();
229
- throw e;
230
- }
231
- },
232
- return: async () => {
233
- const cancelPromise = reader.cancel();
234
- reader.releaseLock();
235
- await cancelPromise;
236
- return {
237
- done: true,
238
- value: void 0
239
- };
240
- },
241
- [Symbol.asyncIterator]() {
242
- return this;
243
- }
244
- };
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;
245
66
  }
246
- var LineDecoder = class _LineDecoder {
247
- static {
248
- this.NEWLINE_CHARS = /* @__PURE__ */ new Set([
249
- "\n",
250
- "\r"
251
- ]);
252
- }
253
- static {
254
- this.NEWLINE_REGEXP = /\r\n|[\n\r]/g;
255
- }
256
- constructor() {
257
- this.buffer = [];
258
- this.trailingCR = false;
259
- }
260
- decode(chunk) {
261
- let text = this.decodeText(chunk);
262
- if (this.trailingCR) {
263
- text = "\r" + text;
264
- this.trailingCR = false;
265
- }
266
- if (text.endsWith("\r")) {
267
- this.trailingCR = true;
268
- text = text.slice(0, -1);
269
- }
270
- if (!text) {
271
- return [];
272
- }
273
- const trailingNewline = _LineDecoder.NEWLINE_CHARS.has(text[text.length - 1] || "");
274
- let lines = text.split(_LineDecoder.NEWLINE_REGEXP);
275
- if (trailingNewline) {
276
- lines.pop();
277
- }
278
- if (lines.length === 1 && !trailingNewline) {
279
- this.buffer.push(lines[0]);
280
- return [];
281
- }
282
- if (this.buffer.length > 0) {
283
- lines = [
284
- this.buffer.join("") + lines[0],
285
- ...lines.slice(1)
286
- ];
287
- this.buffer = [];
288
- }
289
- if (!trailingNewline) {
290
- this.buffer = [
291
- lines.pop() || ""
292
- ];
293
- }
294
- return lines;
295
- }
296
- decodeText(bytes) {
297
- if (bytes == null) {
298
- return "";
299
- }
300
- if (typeof bytes === "string") {
301
- return bytes;
302
- }
303
- if (typeof Buffer !== "undefined") {
304
- if (bytes instanceof Buffer) {
305
- return bytes.toString();
306
- }
307
- if (bytes instanceof Uint8Array) {
308
- return Buffer.from(bytes).toString();
309
- }
310
- 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.`);
311
- }
312
- if (typeof TextDecoder !== "undefined") {
313
- if (bytes instanceof Uint8Array || bytes instanceof ArrayBuffer) {
314
- this.textDecoder ??= new TextDecoder("utf8");
315
- return this.textDecoder.decode(bytes);
316
- }
317
- throw new Error(`Unexpected: received non-Uint8Array/ArrayBuffer (${bytes.constructor.name}) in a web platform. Please report this error.`);
318
- }
319
- throw new Error("Unexpected: neither Buffer nor TextDecoder are available as globals. Please report this error.");
320
- }
321
- flush() {
322
- if (!this.buffer.length && !this.trailingCR) {
323
- return [];
324
- }
325
- const lines = [
326
- this.buffer.join("")
327
- ];
328
- this.buffer = [];
329
- this.trailingCR = false;
330
- return lines;
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_IBOBRBDF.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);
81
+ });
331
82
  }
332
- };
333
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/ai-service/stream.ts";
334
- var createGenerationStream = (response, controller = new AbortController()) => {
335
- const iterator = async function* () {
336
- for await (const sse of iterSSEMessages(response, controller)) {
337
- if (sse.event === "completion") {
338
- try {
339
- yield JSON.parse(sse.data);
340
- } catch (err) {
341
- import_log2.log.error("could not parse message into JSON:", {
342
- data: sse.data,
343
- raw: sse.raw
344
- }, {
345
- F: __dxlog_file,
346
- L: 22,
347
- S: this,
348
- C: (f, a) => f(...a)
83
+ async run(options) {
84
+ const systemTools = [
85
+ (0, import_artifact.defineTool)("system", {
86
+ name: "query_artifact_definitions",
87
+ description: "Query the available artifact definitions",
88
+ schema: import_effect.Schema.Struct({}),
89
+ execute: async () => {
90
+ return import_artifact.ToolResult.Success({
91
+ artifactDefinitions: options.artifacts.map((artifact) => ({
92
+ id: artifact.id,
93
+ name: artifact.name,
94
+ description: artifact.description
95
+ }))
349
96
  });
350
- throw err;
351
97
  }
352
- }
353
- 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") {
354
- try {
355
- yield JSON.parse(sse.data);
356
- } catch (err) {
357
- import_log2.log.error("could not parse message into JSON:", {
358
- data: sse.data,
359
- raw: sse.raw
360
- }, {
361
- F: __dxlog_file,
362
- L: 38,
363
- S: this,
364
- C: (f, a) => f(...a)
365
- });
366
- throw err;
367
- }
368
- }
369
- if (sse.event === "ping") {
370
- continue;
371
- }
372
- if (sse.event === "error") {
373
- throw new Error(`Message generation error: ${sse.data}`);
374
- }
375
- }
376
- };
377
- return new GenerationStreamImpl(controller, iterator);
378
- };
379
- var GenerationStreamImpl = class {
380
- constructor(_controller, _getIterator) {
381
- this._controller = _controller;
382
- this._getIterator = _getIterator;
383
- this._done = new import_async.Trigger();
384
- this._iterator = void 0;
385
- }
386
- /**
387
- * Returns an iterator over the stream.
388
- */
389
- [Symbol.asyncIterator]() {
390
- return this._createIterator();
391
- }
392
- /**
393
- * Aborts the stream.
394
- */
395
- abort() {
396
- this._controller.abort();
397
- }
398
- /**
399
- * Returns the complete message list generated by the AI service.
400
- */
401
- async complete() {
402
- await this._done.wait();
403
- }
404
- /**
405
- * Creates an iterator over the stream.
406
- */
407
- _createIterator() {
408
- const self = this;
409
- return this._iterator ??= (() => {
410
- const generator = async function* () {
411
- try {
412
- for await (const event of self._getIterator()) {
413
- yield event;
414
- }
415
- this._done.wake();
416
- } catch (err) {
417
- this._done.throw(err);
418
- throw err;
419
- }
420
- };
421
- return generator.call(this);
422
- })();
423
- }
424
- };
425
- var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/ai-service/edge-client.ts";
426
- var AIServiceEdgeClient = class {
427
- constructor({ endpoint }) {
428
- (0, import_invariant.invariant)(endpoint, "endpoint is required", {
429
- F: __dxlog_file2,
430
- L: 23,
431
- S: this,
432
- A: [
433
- "endpoint",
434
- "'endpoint is required'"
435
- ]
436
- });
437
- this._endpoint = endpoint;
438
- }
439
- /**
440
- * @deprecated
441
- */
442
- // TODO(burdon): Remove.
443
- async appendMessages(messages) {
444
- const url = `${this._endpoint}/message`;
445
- const res = await fetch(url, {
446
- method: "POST",
447
- headers: {
448
- "Content-Type": "application/json"
449
- },
450
- body: JSON.stringify({
451
- messages: await import_effect.Schema.encodePromise(import_effect.Schema.Array(import_artifact.Message))(messages)
452
98
  })
453
- });
454
- if (!res.ok) {
455
- throw new Error(`${await res.text()} [${url}]`);
456
- }
457
- }
458
- /**
459
- * Process request and open message stream.
460
- */
461
- async exec(request) {
462
- import_log.log.info("requesting", {
463
- endpoint: this._endpoint
464
- }, {
465
- F: __dxlog_file2,
466
- L: 50,
467
- S: this,
468
- C: (f, a) => f(...a)
469
- });
470
- const controller = new AbortController();
471
- const response = await fetch(`${this._endpoint}/generate`, {
472
- signal: controller.signal,
473
- method: "POST",
474
- headers: {
475
- "Content-Type": "application/json"
476
- },
477
- body: JSON.stringify({
478
- // TODO(dmaretskyi): Errors if tools are not provided.
479
- tools: request.tools ?? [],
480
- ...request
481
- })
482
- });
483
- (0, import_invariant.invariant)(response.body instanceof ReadableStream, void 0, {
484
- F: __dxlog_file2,
485
- L: 63,
486
- S: this,
487
- A: [
488
- "response.body instanceof ReadableStream",
489
- ""
490
- ]
491
- });
492
- return createGenerationStream(response, controller);
493
- }
494
- };
495
- var MessageCollector = class {
496
- constructor() {
497
- this._messages = [];
498
- this._pendingMessage = void 0;
499
- }
500
- get messages() {
501
- return this._messages;
502
- }
503
- get pendingMessage() {
504
- return this._pendingMessage;
505
- }
506
- push(event) {
507
- switch (event.type) {
508
- case "message_start":
509
- this._pendingMessage = event.message;
510
- break;
511
- case "message_stop":
512
- if (this._pendingMessage) {
513
- this._messages.push(this._pendingMessage);
514
- this._pendingMessage = void 0;
515
- }
516
- break;
517
- case "content_block_start":
518
- if (this._pendingMessage) {
519
- this._pendingMessage.content = this._pendingMessage.content || [];
520
- this._pendingMessage.content[event.index] = event.content;
521
- }
522
- break;
523
- case "content_block_delta":
524
- if (this._pendingMessage && this._pendingMessage.content) {
525
- const block = this._pendingMessage.content[event.index];
526
- if (block && event.delta.type === "text_delta" && block.type === "text") {
527
- block.text = (block.text || "") + event.delta.text;
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
+ (0, import_log.log)("executing step", {
139
+ action: step.action
140
+ }, {
141
+ F: __dxlog_file,
142
+ L: 160,
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
186
+ });
528
187
  }
529
- }
188
+ }));
530
189
  break;
531
- case "content_block_stop":
532
- if (this._pendingMessage && this._pendingMessage.content) {
533
- const block = this._pendingMessage.content[event.index];
534
- if (block) {
535
- block.pending = false;
190
+ case "immediate":
191
+ systemTools.push((0, import_artifact.defineTool)("system", {
192
+ name: "require_artifact_definitions",
193
+ description: "Require the use of specific artifact definitions. This will allow the model to interact with artifact definitions and use their tools.",
194
+ schema: import_effect.Schema.Struct({
195
+ artifactDefinitionIds: import_effect.Schema.Array(import_effect.Schema.String).annotations({
196
+ description: "The ids of the artifact definitions to require",
197
+ examples: [
198
+ [
199
+ "artifact:dxos.org/example/Test"
200
+ ]
201
+ ]
202
+ })
203
+ }),
204
+ execute: async ({ artifactDefinitionIds }) => {
205
+ const missingArtifactDefinitionIds = artifactDefinitionIds.filter((artifactId) => !options.artifacts.some((artifact) => artifact.id === artifactId));
206
+ if (missingArtifactDefinitionIds.length > 0) {
207
+ return import_artifact.ToolResult.Error(`One or more artifact definition ids are invalid: ${missingArtifactDefinitionIds.join(", ")}`);
208
+ }
209
+ for (const artifactDefinitionId of artifactDefinitionIds) {
210
+ requiredArtifactIds.add(artifactDefinitionId);
211
+ }
212
+ return import_artifact.ToolResult.Success({});
536
213
  }
537
- }
538
- break;
539
- case "message_delta":
540
- if (this._pendingMessage && event.delta) {
541
- Object.assign(this._pendingMessage, event.delta);
542
- }
214
+ }));
543
215
  break;
544
216
  }
545
- }
546
- pushMessage(message) {
547
- this._messages.push(message);
548
- }
549
- };
550
- function* emitMessageAsEvents(message) {
551
- yield {
552
- type: "message_start",
553
- message
554
- };
555
- yield {
556
- type: "message_stop"
557
- };
558
- }
559
- var createArtifactElement = (id) => `<artifact id=${id} />`;
560
- var LLMModel = import_effect2.Schema.Literal(...DEFAULT_LLM_MODELS);
561
- var ToolTypes = Object.freeze({
562
- // TODO(dmaretskyi): Not implemented yet.
563
- // DatabaseQuery: 'database_query',
564
- TextToImage: "text_to_image"
565
- });
566
- var GenerateRequest = import_effect2.Schema.Struct({
567
- spaceId: import_effect2.Schema.optional(import_artifact3.SpaceIdSchema),
568
- threadId: import_effect2.Schema.optional(import_echo_schema2.ObjectId),
569
- /**
570
- * Preferred model or system default.
571
- */
572
- model: import_effect2.Schema.optional(LLMModel),
573
- /**
574
- * Tools available for the LLM.
575
- */
576
- tools: import_effect2.Schema.optional(import_effect2.Schema.Array(import_artifact3.Tool).pipe(import_effect2.Schema.mutable)),
577
- /**
578
- * System instructions to the LLM.
579
- */
580
- systemPrompt: import_effect2.Schema.optional(import_effect2.Schema.String),
581
- /**
582
- * History of messages to include in the context window.
583
- */
584
- // TODO(burdon): Rename messages.
585
- history: import_effect2.Schema.optional(import_effect2.Schema.Array(import_artifact3.Message)),
586
- /**
587
- * Current request.
588
- */
589
- prompt: import_effect2.Schema.optional(import_artifact3.Message)
590
- });
591
- var __dxlog_file3 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/conversation/conversation.ts";
592
- var runLLM = async (params) => {
593
- let conversationResult = null;
594
- const history = params.history ?? [];
595
- const generate = async () => {
596
- (0, import_log4.log)("llm generate", {
597
- tools: params.tools
598
- }, {
599
- F: __dxlog_file3,
600
- L: 44,
601
- S: void 0,
602
- C: (f, a) => f(...a)
603
- });
604
- const beginTs = Date.now();
605
- const stream = await params.client.exec({
606
- model: params.model,
607
- spaceId: params.spaceId,
608
- threadId: params.threadId,
609
- history,
610
- systemPrompt: params.system,
611
- tools: params.tools
612
- });
613
- for await (const event of stream) {
614
- params.logger?.(event);
615
- }
616
- await stream.complete();
617
- const messages = [];
618
- const message = messages.at(-1);
619
- (0, import_invariant3.invariant)(message, void 0, {
620
- F: __dxlog_file3,
621
- L: 63,
622
- S: void 0,
623
- A: [
624
- "message",
625
- ""
626
- ]
627
- });
628
- (0, import_log4.log)("llm result", {
629
- time: Date.now() - beginTs,
630
- message
631
- }, {
632
- F: __dxlog_file3,
633
- L: 65,
634
- S: void 0,
635
- C: (f, a) => f(...a)
636
- });
637
- (0, import_invariant3.invariant)(message, void 0, {
638
- F: __dxlog_file3,
639
- L: 66,
640
- S: void 0,
641
- A: [
642
- "message",
643
- ""
644
- ]
645
- });
646
- params.logger?.({
647
- type: "message",
648
- message
649
- });
650
- history.push(...messages.map((message2) => ({
651
- ...message2,
652
- content: message2.content.filter((block) => block.type !== "image")
653
- })));
654
- const isToolUse2 = message.content.at(-1)?.type === "tool_use";
655
- if (isToolUse2) {
656
- const toolCalls = message.content.filter((block) => block.type === "tool_use");
657
- (0, import_invariant3.invariant)(toolCalls.length === 1, void 0, {
658
- F: __dxlog_file3,
659
- L: 77,
660
- S: void 0,
661
- A: [
662
- "toolCalls.length === 1",
663
- ""
664
- ]
665
- });
666
- const toolCall = toolCalls[0];
667
- const tool = params.tools.find((tool2) => tool2.name === toolCall.name);
668
- if (!tool) {
669
- throw new Error(`Tool not found: ${toolCall.name}`);
670
- }
671
- (0, import_invariant3.invariant)(tool.execute, void 0, {
672
- F: __dxlog_file3,
673
- L: 84,
674
- S: void 0,
675
- A: [
676
- "tool.execute",
677
- ""
678
- ]
679
- });
680
- const toolResult = await tool.execute(toolCall.input);
681
- switch (toolResult.kind) {
682
- case "error": {
683
- import_log4.log.warn("tool error", {
684
- message: toolResult.message
685
- }, {
686
- F: __dxlog_file3,
687
- L: 88,
688
- S: void 0,
689
- C: (f, a) => f(...a)
690
- });
691
- const resultMessage = {
692
- id: import_echo_schema3.ObjectId.random(),
693
- spaceId: params.spaceId,
694
- threadId: params.threadId,
695
- role: "user",
696
- content: [
697
- {
698
- type: "tool_result",
699
- toolUseId: toolCall.id,
700
- content: toolResult.message,
701
- isError: true
702
- }
703
- ]
704
- };
705
- params.logger?.({
706
- type: "message",
707
- message: resultMessage
708
- });
709
- history.push(resultMessage);
710
- return true;
711
- }
712
- case "success": {
713
- (0, import_log4.log)("tool success", {
714
- result: toolResult.result
715
- }, {
716
- F: __dxlog_file3,
717
- L: 110,
718
- S: void 0,
719
- C: (f, a) => f(...a)
720
- });
721
- const resultMessage = {
722
- id: import_echo_schema3.ObjectId.random(),
723
- spaceId: params.spaceId,
724
- threadId: params.threadId,
725
- role: "user",
726
- content: [
727
- {
728
- type: "tool_result",
729
- toolUseId: toolCall.id,
730
- content: JSON.stringify(toolResult.result)
731
- }
732
- ]
733
- };
734
- params.logger?.({
735
- type: "message",
736
- message: resultMessage
737
- });
738
- history.push(resultMessage);
739
- return true;
740
- }
741
- case "break": {
742
- (0, import_log4.log)("tool break", {
743
- result: toolResult.result
744
- }, {
745
- F: __dxlog_file3,
746
- L: 131,
747
- S: void 0,
748
- C: (f, a) => f(...a)
749
- });
750
- conversationResult = toolResult.result;
751
- return false;
752
- }
753
- }
754
- }
755
- return false;
756
- };
757
- while (await generate()) {
758
- }
759
- return {
760
- result: conversationResult,
761
- history
762
- };
763
- };
764
- var isToolUse = (message, { onlyToolNames } = {}) => {
765
- const block = message.content.at(-1);
766
- (0, import_invariant3.invariant)(block, void 0, {
767
- F: __dxlog_file3,
768
- L: 152,
769
- S: void 0,
770
- A: [
771
- "block",
772
- ""
773
- ]
774
- });
775
- return block.type === "tool_use" && (!onlyToolNames || onlyToolNames.includes(block.name));
776
- };
777
- var runTools = async ({ message, tools, extensions }) => {
778
- const toolCalls = message.content.filter((block) => block.type === "tool_use");
779
- (0, import_invariant3.invariant)(toolCalls.length === 1, void 0, {
780
- F: __dxlog_file3,
781
- L: 174,
782
- S: void 0,
783
- A: [
784
- "toolCalls.length === 1",
785
- ""
786
- ]
787
- });
788
- const toolCall = toolCalls[0];
789
- const tool = tools.find((tool2) => tool2.name === toolCall.name);
790
- if (!tool) {
791
- throw new Error(`Tool not found: ${toolCall.name}`);
792
- }
793
- let toolResult;
794
- try {
795
- (0, import_invariant3.invariant)(tool.execute, void 0, {
796
- F: __dxlog_file3,
797
- L: 183,
798
- S: void 0,
799
- A: [
800
- "tool.execute",
801
- ""
802
- ]
803
- });
804
- toolResult = await tool.execute(toolCall.input, {
805
- extensions
806
- });
807
- } catch (error) {
808
- (0, import_log4.log)("tool error", {
809
- error
810
- }, {
811
- F: __dxlog_file3,
812
- L: 186,
813
- S: void 0,
814
- C: (f, a) => f(...a)
815
- });
816
- toolResult = import_artifact4.ToolResult.Error(error.message);
817
- }
818
- switch (toolResult.kind) {
819
- case "error": {
820
- (0, import_log4.log)("tool error", {
821
- message: toolResult.message
822
- }, {
823
- F: __dxlog_file3,
824
- L: 192,
825
- S: void 0,
826
- C: (f, a) => f(...a)
827
- });
828
- const resultMessage = (0, import_echo_schema3.createStatic)(import_artifact4.Message, {
217
+ this._history = [
218
+ ...options.history
219
+ ];
220
+ this._pending = [
221
+ (0, import_echo_schema.createStatic)(import_artifact.Message, {
829
222
  role: "user",
830
223
  content: [
831
224
  {
832
- type: "tool_result",
833
- toolUseId: toolCall.id,
834
- content: toolResult.message,
835
- isError: true
225
+ type: "text",
226
+ text: options.prompt
836
227
  }
837
228
  ]
838
- });
839
- return {
840
- type: "continue",
841
- message: resultMessage
842
- };
843
- }
844
- case "success": {
845
- (0, import_log4.log)("tool success", {
846
- result: toolResult.result
847
- }, {
848
- F: __dxlog_file3,
849
- L: 213,
850
- S: void 0,
851
- C: (f, a) => f(...a)
852
- });
853
- const resultMessage = (0, import_echo_schema3.createStatic)(import_artifact4.Message, {
854
- role: "user",
855
- content: [
856
- {
857
- type: "tool_result",
858
- toolUseId: toolCall.id,
859
- content: typeof toolResult.result === "string" ? toolResult.result : JSON.stringify(toolResult.result) ?? ""
860
- },
861
- ...toolResult.extractContentBlocks ?? []
862
- ]
863
- });
864
- return {
865
- type: "continue",
866
- message: resultMessage
867
- };
868
- }
869
- case "break": {
870
- (0, import_log4.log)("tool break", {
871
- result: toolResult.result
872
- }, {
873
- F: __dxlog_file3,
874
- L: 234,
875
- S: void 0,
876
- C: (f, a) => f(...a)
877
- });
878
- return {
879
- type: "break",
880
- result: toolResult.result
881
- };
882
- }
883
- }
884
- };
885
- var __dxlog_file4 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/ai-service/ollama-client.ts";
886
- var DEFAULT_OLLAMA_URL = "http://localhost:11434";
887
- var DEFAULT_OLLAMA_MODEL = "llama3.2:1b";
888
- var OllamaClient = class _OllamaClient {
889
- /**
890
- * Create a test client with small local model and no temperature for predictable results.
891
- */
892
- static createClient(options) {
893
- return new _OllamaClient({
894
- tools: options?.tools,
895
- overrides: {
896
- model: DEFAULT_OLLAMA_MODEL,
897
- temperature: 0
898
- }
899
- });
900
- }
901
- /**
902
- * Check if Ollama server is running and accessible.
903
- * @returns Promise that resolves to true if Ollama is running, false otherwise.
904
- */
905
- static async isRunning() {
906
- try {
907
- const response = await fetch(`${DEFAULT_OLLAMA_URL}/api/version`, {
908
- method: "GET",
909
- headers: {
910
- "Content-Type": "application/json"
911
- },
912
- signal: AbortSignal.timeout(100)
913
- });
914
- return response.ok;
915
- } catch (error) {
916
- return false;
917
- }
918
- }
919
- constructor({ endpoint, tools, overrides, maxToolInvocations }) {
920
- this._endpoint = endpoint ?? DEFAULT_OLLAMA_URL;
921
- this._tools = tools ?? [];
922
- this._modelOverride = overrides?.model;
923
- this._temperatureOverride = overrides?.temperature;
924
- this._maxToolInvocations = maxToolInvocations ?? 3;
925
- }
926
- _getEmbededTools(clientTools) {
927
- return [
928
- ...this._tools,
929
- ...clientTools.filter((tool) => tool.type !== void 0 && WELL_KNOWN_TOOLS[tool.type]).map((tool) => WELL_KNOWN_TOOLS[tool.type])
229
+ })
930
230
  ];
931
- }
932
- async *_generateStream(request, signal) {
933
- (0, import_invariant2.invariant)(request.prompt === void 0, "Prompt must be in history", {
934
- F: __dxlog_file4,
935
- L: 99,
936
- S: this,
937
- A: [
938
- "request.prompt === undefined",
939
- "'Prompt must be in history'"
940
- ]
941
- });
942
- const ollamaRequest = {
943
- model: this._modelOverride ?? request.model ?? "llama3",
944
- messages: [],
945
- stream: true,
946
- options: {
947
- temperature: this._temperatureOverride ?? 0.7
948
- }
949
- };
950
- if (request.systemPrompt) {
951
- ollamaRequest.messages.push({
952
- role: "system",
953
- content: request.systemPrompt
954
- });
955
- }
956
- if (request.history?.length) {
957
- for (const message of request.history) {
958
- const role = message.role === "assistant" ? "assistant" : "user";
959
- if (message.content.length === 0) {
960
- continue;
961
- }
962
- for (const content of message.content) {
963
- switch (content.type) {
964
- case "text": {
965
- ollamaRequest.messages.push({
966
- role,
967
- content: content.text
968
- });
969
- break;
970
- }
971
- case "tool_use": {
972
- ollamaRequest.messages.push({
973
- role: "assistant",
974
- content: "",
975
- tool_calls: [
976
- {
977
- function: {
978
- name: content.name,
979
- arguments: content.input
980
- }
981
- }
982
- ]
983
- });
984
- break;
985
- }
986
- case "tool_result": {
987
- ollamaRequest.messages.push({
988
- role: "tool",
989
- content: JSON.stringify({
990
- type: "tool_result",
991
- toolUseId: content.toolUseId,
992
- content: content.content
993
- })
994
- });
995
- break;
996
- }
997
- }
998
- }
999
- }
1000
- }
1001
- if (request.tools?.length || this._tools.length) {
1002
- const allTools = [
1003
- ...request.tools ?? [],
1004
- ...this._tools
1005
- ];
1006
- const withWellKnownTools = allTools.map((tool) => tool.type !== void 0 && WELL_KNOWN_TOOLS[tool.type] ? WELL_KNOWN_TOOLS[tool.type] : tool);
1007
- if (withWellKnownTools.length > 0) {
1008
- ollamaRequest.tools = withWellKnownTools.map((tool) => ({
1009
- type: "function",
1010
- function: {
1011
- name: tool.name,
1012
- description: tool.description,
1013
- parameters: {
1014
- type: "object",
1015
- properties: tool.parameters?.properties,
1016
- required: tool.parameters?.required
1017
- }
1018
- }
1019
- }));
1020
- }
1021
- }
1022
- import_log3.log.info("generate request", {
1023
- history: request.history,
1024
- prompt: request.prompt,
1025
- ollamaRequest
1026
- }, {
1027
- F: __dxlog_file4,
1028
- L: 193,
1029
- S: this,
1030
- C: (f, a) => f(...a)
1031
- });
1032
- const response = await fetch(`${this._endpoint}/api/chat`, {
1033
- method: "POST",
1034
- headers: {
1035
- "Content-Type": "application/json"
1036
- },
1037
- body: JSON.stringify(ollamaRequest),
1038
- signal
1039
- });
1040
- if (!response.ok) {
1041
- throw new Error(`Ollama API error: ${response.status} ${response.statusText}`);
1042
- }
1043
- if (!response.body) {
1044
- throw new Error("Ollama API returned no response body");
1045
- }
1046
- const reader = response.body.pipeThrough(new TextDecoderStream()).pipeThrough(new OllamaDecoderStream()).getReader();
231
+ this.userMessage.emit(this._pending.at(-1));
232
+ this._stream = void 0;
233
+ let error;
234
+ const requiredArtifactIds = new Set(options.requiredArtifactIds ?? []);
1047
235
  try {
1048
- const messageId = import_echo_schema.ObjectId.random();
1049
- yield {
1050
- type: "message_start",
1051
- message: {
1052
- id: messageId,
1053
- role: "assistant",
1054
- content: []
1055
- }
1056
- };
1057
- const textBlock = {
1058
- type: "text",
1059
- text: ""
1060
- };
1061
- let currentBlockIndex = 0;
1062
- yield {
1063
- type: "content_block_start",
1064
- index: currentBlockIndex,
1065
- content: textBlock
1066
- };
1067
- while (true) {
1068
- const { done, value: data } = await reader.read();
1069
- if (done) {
1070
- break;
1071
- }
1072
- import_log3.log.info("ollama data", {
1073
- data
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)
1074
247
  }, {
1075
- F: __dxlog_file4,
1076
- L: 256,
248
+ F: __dxlog_file,
249
+ L: 264,
1077
250
  S: this,
1078
251
  C: (f, a) => f(...a)
1079
252
  });
1080
- if (data.message?.content) {
1081
- yield {
1082
- type: "content_block_delta",
1083
- index: 0,
1084
- delta: {
1085
- type: "text_delta",
1086
- text: data.message.content
1087
- }
1088
- };
1089
- }
1090
- if (data.message?.tool_calls && data.message?.tool_calls?.length > 0) {
1091
- if (data.message.tool_calls.length > 1) {
1092
- import_log3.log.warn("Ollama returned multiple tool calls. Only the first one will be used.", void 0, {
1093
- F: __dxlog_file4,
1094
- L: 273,
1095
- S: this,
1096
- C: (f, a) => f(...a)
1097
- });
1098
- }
1099
- yield {
1100
- type: "content_block_stop",
1101
- index: currentBlockIndex++
1102
- };
1103
- yield {
1104
- type: "content_block_start",
1105
- index: currentBlockIndex,
1106
- content: {
1107
- type: "tool_use",
1108
- id: import_echo_schema.ObjectId.random(),
1109
- name: data.message.tool_calls[0].function.name,
1110
- input: sanitizeToolArguments(data.message.tool_calls[0].function.arguments)
1111
- }
1112
- };
1113
- yield {
1114
- type: "message_delta",
1115
- delta: {
1116
- stopReason: "tool_use"
1117
- }
1118
- };
1119
- }
1120
- }
1121
- yield {
1122
- type: "content_block_stop",
1123
- index: currentBlockIndex
1124
- };
1125
- yield {
1126
- type: "message_stop"
1127
- };
1128
- } finally {
1129
- reader.releaseLock();
1130
- }
1131
- }
1132
- async exec(request) {
1133
- const controller = new AbortController();
1134
- try {
1135
- return new GenerationStreamImpl(controller, async function* () {
1136
- const collector = new MessageCollector();
1137
- let toolInvocations = 0;
1138
- while (true) {
1139
- for await (const event of this._generateStream({
1140
- ...request,
1141
- history: [
1142
- ...request.history ?? [],
1143
- ...request.prompt ? [
1144
- request.prompt
1145
- ] : [],
1146
- ...collector.messages
1147
- ],
1148
- prompt: void 0
1149
- }, controller.signal)) {
1150
- collector.push(event);
1151
- yield event;
1152
- }
1153
- if (collector.messages.length > 0 && !isToolUse(collector.messages.at(-1), {
1154
- onlyToolNames: this._getEmbededTools(request.tools ?? []).map((tool) => tool.name)
1155
- })) {
1156
- break;
1157
- }
1158
- if (++toolInvocations > this._maxToolInvocations) {
1159
- throw new Error("Maximum number of tool invocations reached");
1160
- }
1161
- const result = await runTools({
1162
- message: collector.messages.at(-1),
1163
- tools: this._getEmbededTools(request.tools ?? [])
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: options.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: 289,
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: 294,
281
+ S: this,
282
+ A: [
283
+ "message",
284
+ ""
285
+ ]
286
+ });
287
+ if ((0, import_chunk_IBOBRBDF.isToolUse)(message)) {
288
+ (0, import_log.log)("tool request...", void 0, {
289
+ F: __dxlog_file,
290
+ L: 296,
291
+ S: this,
292
+ C: (f, a) => f(...a)
293
+ });
294
+ const response = await (0, import_chunk_IBOBRBDF.runTools)({
295
+ message: this._pending.at(-1),
296
+ tools,
297
+ extensions: options.extensions ?? {}
298
+ });
299
+ (0, import_log.log)("tool response", {
300
+ response
301
+ }, {
302
+ F: __dxlog_file,
303
+ L: 303,
304
+ S: this,
305
+ C: (f, a) => f(...a)
1164
306
  });
1165
- switch (result.type) {
307
+ switch (response.type) {
1166
308
  case "continue": {
1167
- collector.pushMessage(result.message);
1168
- yield* emitMessageAsEvents(result.message);
309
+ this._pending = [
310
+ ...this._pending,
311
+ response.message
312
+ ];
313
+ this.userMessage.emit(response.message);
314
+ more = true;
1169
315
  break;
1170
316
  }
1171
- case "break": {
1172
- throw new Error("Tool use breaks not allowed");
1173
- }
1174
317
  }
1175
318
  }
1176
- }.bind(this));
1177
- } catch (error) {
1178
- controller.abort();
1179
- throw error;
1180
- }
1181
- }
1182
- };
1183
- var sanitizeToolArguments = (args) => {
1184
- if (args.type === "object" && typeof args.properties === "object" && Array.isArray(args.required)) {
1185
- return args.properties;
1186
- }
1187
- return args;
1188
- };
1189
- var SAMPLE_IMAGE_URL = "https://images.nightcafe.studio/jobs/BNmcRhHCM1JRKoUtqSei/BNmcRhHCM1JRKoUtqSei--1--5b9rv.jpg";
1190
- var WELL_KNOWN_TOOLS = {
1191
- [ToolTypes.TextToImage]: (0, import_artifact2.defineTool)("system", {
1192
- name: "text-to-image",
1193
- description: "Generate an image from a text prompt",
1194
- schema: import_echo_schema.S.Struct({
1195
- prompt: import_echo_schema.S.String.annotations({
1196
- description: "The text prompt describing the image to generate"
1197
- })
1198
- }),
1199
- execute: async ({ prompt }) => {
1200
- const image = await fetch(SAMPLE_IMAGE_URL).then(async (res) => Buffer.from(await res.arrayBuffer()));
1201
- const imageBase64 = image.toString("base64");
1202
- const id = import_echo_schema.ObjectId.random();
1203
- return import_artifact2.ToolResult.Success(`The generation was successful. Image ID: ${id} Prompt: ${prompt}`, [
1204
- {
1205
- type: "image",
1206
- id,
1207
- source: {
1208
- type: "base64",
1209
- mediaType: "image/jpeg",
1210
- data: imageBase64
1211
- }
1212
- }
1213
- ]);
1214
- }
1215
- })
1216
- };
1217
- var OllamaDecoderStream = class extends TransformStream {
1218
- constructor() {
1219
- super({
1220
- transform: (chunk, controller) => {
1221
- this._buffer += chunk;
1222
- const lines = this._buffer.split("\n");
1223
- this._buffer = lines.pop() || "";
1224
- for (const line of lines) {
1225
- controller.enqueue(JSON.parse(line.trim()));
1226
- }
1227
- },
1228
- flush: (controller) => {
1229
- this._buffer = this._buffer.trim();
1230
- if (this._buffer.length > 0) {
1231
- controller.enqueue(JSON.parse(this._buffer));
1232
- }
1233
- }
1234
- });
1235
- this._buffer = "";
1236
- }
1237
- };
1238
- var textChunk = P.regexp(/[^<]+/).map((content) => ({
1239
- type: "text",
1240
- content
1241
- }));
1242
- 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]) => [
1243
- name,
1244
- value
1245
- ]);
1246
- var tagName = P.regexp(/[a-zA-Z_][a-zA-Z0-9_-]*/);
1247
- var selfClosingTag = P.seqMap(
1248
- //
1249
- P.string("<"),
1250
- tagName,
1251
- P.optWhitespace.then(attribute).many(),
1252
- P.optWhitespace.then(P.string("/>")),
1253
- P.optWhitespace,
1254
- (_, tag, attributes, __) => ({
1255
- type: "tag",
1256
- tag,
1257
- attributes: Object.fromEntries(attributes),
1258
- content: [],
1259
- selfClosing: true
1260
- })
1261
- );
1262
- var openTag = P.seqMap(
1263
- //
1264
- P.string("<"),
1265
- tagName,
1266
- P.optWhitespace.then(attribute).many(),
1267
- P.string(">"),
1268
- P.regexp(/[^<]*/),
1269
- (_, tag, attributes, __, content) => ({
1270
- type: "tag",
1271
- tag,
1272
- attributes: Object.fromEntries(attributes),
1273
- content: content.length ? [
1274
- {
1275
- type: "text",
1276
- content
1277
- }
1278
- ] : []
1279
- })
1280
- );
1281
- var closeTag = P.seqMap(
1282
- //
1283
- P.string("</"),
1284
- tagName,
1285
- P.string(">"),
1286
- P.optWhitespace,
1287
- (_, tag) => ({
1288
- type: "tag",
1289
- tag,
1290
- content: [],
1291
- closing: true
1292
- })
1293
- );
1294
- var mixedChunk = P.alt(selfClosingTag, openTag, closeTag, textChunk);
1295
- var StreamTransform = class {
1296
- constructor() {
1297
- this._buffer = "";
1298
- }
1299
- transform(chunk) {
1300
- this._buffer += chunk;
1301
- const results = [];
1302
- const parser = mixedChunk.many().skip(P.optWhitespace);
1303
- const result = parser.parse(this._buffer);
1304
- if (result.status) {
1305
- for (const chunk2 of result.value) {
1306
- results.push(chunk2);
1307
- }
1308
- this._buffer = "";
1309
- }
1310
- return results;
1311
- }
1312
- // TODO(burdon): Process remaining buffer?
1313
- flush() {
1314
- const remaining = this._buffer;
1315
- this._buffer = "";
1316
- return remaining.length > 0 ? [
1317
- {
1318
- type: "text",
1319
- content: remaining
1320
- }
1321
- ] : [];
1322
- }
1323
- };
1324
- var __dxlog_file5 = "/home/runner/work/dxos/dxos/packages/experimental/assistant/src/ai-service/parser.ts";
1325
- var MixedStreamParser = class {
1326
- constructor() {
1327
- this.message = new import_async2.Event();
1328
- this.block = new import_async2.Event();
1329
- this.update = new import_async2.Event();
1330
- this.streamEvent = new import_async2.Event();
1331
- }
1332
- _emitBlock(contentBlock, streamBlock) {
1333
- const messageBlock = streamBlock ? mergeMessageBlock(contentBlock, streamBlock) : contentBlock;
1334
- if (messageBlock) {
1335
- if (messageBlock.type === "text" && messageBlock.text.length === 0) {
1336
- return;
1337
- }
1338
- (0, import_invariant4.invariant)(this._message, void 0, {
1339
- F: __dxlog_file5,
1340
- L: 49,
319
+ } while (more);
320
+ } catch (err) {
321
+ import_log.log.catch(err, void 0, {
322
+ F: __dxlog_file,
323
+ L: 315,
1341
324
  S: this,
1342
- A: [
1343
- "this._message",
1344
- ""
1345
- ]
325
+ C: (f, a) => f(...a)
1346
326
  });
1347
- this._message.content.push(messageBlock);
1348
- this.block.emit(messageBlock);
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;
1349
338
  }
1350
- }
1351
- _emitUpdate(contentBlock, streamBlock) {
1352
- const messageBlock = mergeMessageBlock(contentBlock, streamBlock);
1353
- if (messageBlock) {
1354
- messageBlock.pending = true;
1355
- this.update.emit(messageBlock);
339
+ if (error) {
340
+ throw error;
1356
341
  }
342
+ return this._pending;
1357
343
  }
1358
- /**
1359
- * Parse stream until end.
1360
- */
1361
- async parse(stream) {
1362
- const transformer = new StreamTransform();
1363
- let contentBlock;
1364
- let streamBlock;
1365
- const stack = [];
1366
- const messagesCollected = [];
1367
- for await (const event of stream) {
1368
- this.streamEvent.emit(event);
1369
- switch (event.type) {
1370
- //
1371
- // Messages.
1372
- //
1373
- case "message_start": {
1374
- if (this._message) {
1375
- import_log5.log.warn("unexpected message_start", void 0, {
1376
- F: __dxlog_file5,
1377
- L: 93,
1378
- S: this,
1379
- C: (f, a) => f(...a)
1380
- });
1381
- }
1382
- this._message = (0, import_echo_schema4.createStatic)(import_artifact5.Message, {
1383
- role: event.message.role,
1384
- content: [
1385
- ...event.message.content
1386
- ]
1387
- });
1388
- this.message.emit(this._message);
1389
- break;
1390
- }
1391
- case "message_delta": {
1392
- if (!this._message) {
1393
- import_log5.log.warn("unexpected message_delta", void 0, {
1394
- F: __dxlog_file5,
1395
- L: 103,
1396
- S: this,
1397
- C: (f, a) => f(...a)
1398
- });
1399
- }
1400
- break;
1401
- }
1402
- case "message_stop": {
1403
- if (!this._message) {
1404
- import_log5.log.warn("unexpected message_stop", void 0, {
1405
- F: __dxlog_file5,
1406
- L: 111,
1407
- S: this,
1408
- C: (f, a) => f(...a)
1409
- });
1410
- } else {
1411
- messagesCollected.push(this._message);
1412
- this._message = void 0;
1413
- }
1414
- break;
1415
- }
1416
- //
1417
- // Content blocks.
1418
- //
1419
- case "content_block_start": {
1420
- if (contentBlock) {
1421
- import_log5.log.warn("unexpected content_block_start", {
1422
- content: contentBlock
1423
- }, {
1424
- F: __dxlog_file5,
1425
- L: 125,
1426
- S: this,
1427
- C: (f, a) => f(...a)
1428
- });
1429
- }
1430
- contentBlock = event.content;
1431
- break;
1432
- }
1433
- case "content_block_delta": {
1434
- if (!contentBlock) {
1435
- import_log5.log.warn("unexpected content_block_delta", void 0, {
1436
- F: __dxlog_file5,
1437
- L: 135,
1438
- S: this,
1439
- C: (f, a) => f(...a)
1440
- });
1441
- break;
1442
- }
1443
- switch (event.delta.type) {
1444
- //
1445
- // Text
1446
- //
1447
- case "text_delta": {
1448
- const chunks = transformer.transform(event.delta.text);
1449
- for (const chunk of chunks) {
1450
- switch (streamBlock?.type) {
1451
- //
1452
- // XML Fragment.
1453
- //
1454
- case "tag": {
1455
- if (chunk.type === "tag") {
1456
- if (chunk.selfClosing) {
1457
- streamBlock.content.push(chunk);
1458
- } else if (chunk.closing) {
1459
- if (stack.length > 0) {
1460
- const top = stack.pop();
1461
- (0, import_invariant4.invariant)(top && top.type === "tag", void 0, {
1462
- F: __dxlog_file5,
1463
- L: 159,
1464
- S: this,
1465
- A: [
1466
- "top && top.type === 'tag'",
1467
- ""
1468
- ]
1469
- });
1470
- top.content.push(streamBlock);
1471
- streamBlock = top;
1472
- } else {
1473
- this._emitBlock(contentBlock, streamBlock);
1474
- streamBlock = void 0;
1475
- }
1476
- } else {
1477
- stack.push(streamBlock);
1478
- streamBlock = chunk;
1479
- }
1480
- } else {
1481
- if (streamBlock.content.length === 0) {
1482
- streamBlock.content.push(chunk);
1483
- } else {
1484
- const last = streamBlock.content.at(-1);
1485
- (0, import_invariant4.invariant)(last, void 0, {
1486
- F: __dxlog_file5,
1487
- L: 176,
1488
- S: this,
1489
- A: [
1490
- "last",
1491
- ""
1492
- ]
1493
- });
1494
- if (last.type === "text") {
1495
- last.content += chunk.content;
1496
- } else {
1497
- streamBlock.content.push(chunk);
1498
- }
1499
- }
1500
- }
1501
- break;
1502
- }
1503
- //
1504
- // Text Fragment.
1505
- //
1506
- case "text": {
1507
- if (chunk.type === "tag") {
1508
- this._emitBlock(contentBlock, streamBlock);
1509
- if (chunk.selfClosing) {
1510
- this._emitBlock(contentBlock, chunk);
1511
- streamBlock = void 0;
1512
- } else {
1513
- streamBlock = chunk;
1514
- }
1515
- } else {
1516
- streamBlock.content += chunk.content;
1517
- }
1518
- break;
1519
- }
1520
- //
1521
- // No current chunk.
1522
- //
1523
- default: {
1524
- if (chunk.type === "tag" && chunk.selfClosing) {
1525
- this._emitBlock(contentBlock, chunk);
1526
- } else {
1527
- streamBlock = chunk;
1528
- }
1529
- }
1530
- }
1531
- }
1532
- break;
1533
- }
1534
- //
1535
- // JSON
1536
- //
1537
- case "input_json_delta": {
1538
- if (!contentBlock) {
1539
- import_log5.log.warn("unexpected input_json_delta", {
1540
- contentBlock
1541
- }, {
1542
- F: __dxlog_file5,
1543
- L: 226,
1544
- S: this,
1545
- C: (f, a) => f(...a)
1546
- });
1547
- break;
1548
- }
1549
- if (streamBlock?.type === "json") {
1550
- streamBlock.content += event.delta.partial_json;
1551
- } else {
1552
- streamBlock = {
1553
- type: "json",
1554
- content: event.delta.partial_json
1555
- };
1556
- }
1557
- break;
1558
- }
1559
- }
1560
- break;
1561
- }
1562
- case "content_block_stop": {
1563
- if (!contentBlock) {
1564
- import_log5.log.warn("unexpected content_block_stop", void 0, {
1565
- F: __dxlog_file5,
1566
- L: 243,
1567
- S: this,
1568
- C: (f, a) => f(...a)
1569
- });
1570
- break;
1571
- }
1572
- this._emitBlock(contentBlock, streamBlock);
1573
- contentBlock = void 0;
1574
- streamBlock = void 0;
1575
- break;
1576
- }
1577
- }
1578
- if (contentBlock && streamBlock) {
1579
- this._emitUpdate(contentBlock, streamBlock);
1580
- }
1581
- }
1582
- return messagesCollected;
344
+ abort() {
345
+ this._stream?.abort();
1583
346
  }
1584
347
  };
1585
- var mergeMessageBlock = (contentBlock, streamBlock) => {
1586
- (0, import_log5.log)("mergeMessageBlock", {
1587
- contentBlock,
1588
- streamBlock
1589
- }, {
1590
- F: __dxlog_file5,
1591
- L: 270,
1592
- S: void 0,
1593
- C: (f, a) => f(...a)
1594
- });
1595
- switch (streamBlock.type) {
1596
- //
1597
- // Text
1598
- //
1599
- case "text": {
1600
- return {
1601
- ...contentBlock,
1602
- type: "text",
1603
- text: streamBlock.content
1604
- };
1605
- }
1606
- //
1607
- // XML
1608
- //
1609
- case "tag": {
1610
- switch (streamBlock.tag) {
1611
- case "cot": {
1612
- const content = streamBlock.content.map((block) => {
1613
- switch (block.type) {
1614
- case "text":
1615
- return block.content;
1616
- default:
1617
- return null;
1618
- }
1619
- }).filter(import_util.isNotFalsy).join("\n");
1620
- return {
1621
- ...contentBlock,
1622
- type: "text",
1623
- disposition: "cot",
1624
- text: content
1625
- };
1626
- }
1627
- case "artifact": {
1628
- const { attributes } = streamBlock;
1629
- return {
1630
- type: "json",
1631
- disposition: "artifact",
1632
- json: JSON.stringify(attributes)
1633
- };
1634
- }
1635
- case "suggest": {
1636
- if (streamBlock.content.length === 1 && streamBlock.content[0].type === "text") {
1637
- return {
1638
- type: "json",
1639
- disposition: "suggest",
1640
- json: JSON.stringify({
1641
- text: streamBlock.content[0].content
1642
- })
1643
- };
1644
- }
1645
- break;
1646
- }
1647
- case "select": {
1648
- return {
1649
- type: "json",
1650
- disposition: "select",
1651
- json: JSON.stringify({
1652
- options: streamBlock.content.flatMap((content) => content.type === "tag" && content.content.length === 1 && content.content[0].type === "text" ? [
1653
- content.content[0].content
1654
- ] : [])
1655
- })
1656
- };
1657
- }
1658
- case "tool-list": {
1659
- return {
1660
- type: "json",
1661
- disposition: "tool_list",
1662
- json: JSON.stringify({})
1663
- };
1664
- }
1665
- }
1666
- break;
1667
- }
1668
- //
1669
- // JSON
1670
- //
1671
- case "json": {
1672
- switch (contentBlock.type) {
1673
- case "tool_use": {
1674
- return {
1675
- ...contentBlock,
1676
- input: (0, import_util.safeParseJson)(streamBlock.content) ?? {}
1677
- };
1678
- }
1679
- }
1680
- return {
1681
- ...contentBlock,
1682
- type: "json",
1683
- disposition: "artifact",
1684
- json: streamBlock.content
1685
- };
1686
- }
1687
- }
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
+ ${availableArtifacts.length > 0 ? `Artifacts already in context: ${availableArtifacts.join("\n")}` : ""}
373
+ `;
374
+ var AIServiecOverloadedError = class extends import_effect.Schema.TaggedError()("AIServiecOverloadedError", {}) {
1688
375
  };
1689
376
  // Annotate the CommonJS export names for ESM import in node:
1690
377
  0 && (module.exports = {
1691
378
  AIServiceEdgeClient,
1692
- DEFAULT_LLM_MODEL,
1693
- DEFAULT_LLM_MODELS,
379
+ AIServiecOverloadedError,
380
+ AISession,
381
+ DEFAULT_EDGE_MODEL,
382
+ DEFAULT_EDGE_MODELS,
383
+ DEFAULT_OLLAMA_ENDPOINT,
384
+ DEFAULT_OLLAMA_MODEL,
385
+ DEFAULT_OLLAMA_MODELS,
1694
386
  EchoDataSource,
1695
387
  GenerateRequest,
1696
388
  GenerationStreamImpl,