@dxos/assistant 0.8.2-main.12df754 → 0.8.2-main.2f9c567

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 (65) hide show
  1. package/dist/lib/browser/{chunk-DEIGOD4U.mjs → chunk-OLO6SVOL.mjs} +21 -21
  2. package/dist/lib/browser/chunk-OLO6SVOL.mjs.map +7 -0
  3. package/dist/lib/browser/index.mjs +272 -152
  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 +82 -86
  7. package/dist/lib/browser/testing/index.mjs.map +3 -3
  8. package/dist/lib/node/{chunk-7VH7NMIM.cjs → chunk-CI5Q7FBG.cjs} +6 -6
  9. package/dist/lib/node/chunk-CI5Q7FBG.cjs.map +7 -0
  10. package/dist/lib/node/index.cjs +300 -183
  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 +72 -77
  14. package/dist/lib/node/testing/index.cjs.map +3 -3
  15. package/dist/lib/node-esm/{chunk-7JI4MDGL.mjs → chunk-Y3SYOFFJ.mjs} +21 -21
  16. package/dist/lib/node-esm/chunk-Y3SYOFFJ.mjs.map +7 -0
  17. package/dist/lib/node-esm/index.mjs +272 -152
  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 +82 -86
  21. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  22. package/dist/types/src/ai-service/types.d.ts +133 -133
  23. package/dist/types/src/ai-service/types.d.ts.map +1 -1
  24. package/dist/types/src/conversation/types.d.ts +9 -9
  25. package/dist/types/src/conversation/types.d.ts.map +1 -1
  26. package/dist/types/src/cypher/echo-data-source.d.ts.map +1 -1
  27. package/dist/types/src/session/index.d.ts +1 -0
  28. package/dist/types/src/session/index.d.ts.map +1 -1
  29. package/dist/types/src/session/session.d.ts +32 -7
  30. package/dist/types/src/session/session.d.ts.map +1 -1
  31. package/dist/types/src/session/version-pin.d.ts +17 -0
  32. package/dist/types/src/session/version-pin.d.ts.map +1 -0
  33. package/dist/types/src/session/version-pin.test.d.ts +2 -0
  34. package/dist/types/src/session/version-pin.test.d.ts.map +1 -0
  35. package/dist/types/src/testing/query-promts.d.ts +3 -3
  36. package/dist/types/src/testing/query-promts.d.ts.map +1 -1
  37. package/dist/types/src/testing/test-data.d.ts +4 -4
  38. package/dist/types/src/testing/test-data.d.ts.map +1 -1
  39. package/dist/types/src/testing/test-schema.d.ts +25 -23
  40. package/dist/types/src/testing/test-schema.d.ts.map +1 -1
  41. package/package.json +15 -14
  42. package/src/ai-service/client.test.ts +5 -5
  43. package/src/ai-service/ollama-client.ts +7 -7
  44. package/src/ai-service/types.ts +14 -14
  45. package/src/bin/chat.ts +2 -2
  46. package/src/conversation/conversation.test.ts +3 -3
  47. package/src/conversation/types.ts +10 -10
  48. package/src/cypher/combinators.test.ts +4 -4
  49. package/src/cypher/echo-data-source.ts +7 -5
  50. package/src/cypher/execution-plan.test.ts +1 -1
  51. package/src/cypher/query-executor.test.ts +1 -1
  52. package/src/cypher/schema.test.ts +3 -3
  53. package/src/session/index.ts +1 -0
  54. package/src/session/session.test.ts +24 -17
  55. package/src/session/session.ts +285 -149
  56. package/src/session/version-pin.test.ts +26 -0
  57. package/src/session/version-pin.ts +43 -0
  58. package/src/testing/artifacts.test.ts +22 -22
  59. package/src/testing/query-promts.ts +5 -5
  60. package/src/testing/query.test.ts +3 -3
  61. package/src/testing/test-data.ts +62 -55
  62. package/src/testing/test-schema.ts +28 -29
  63. package/dist/lib/browser/chunk-DEIGOD4U.mjs.map +0 -7
  64. package/dist/lib/node/chunk-7VH7NMIM.cjs.map +0 -7
  65. package/dist/lib/node-esm/chunk-7JI4MDGL.mjs.map +0 -7
@@ -18,47 +18,77 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var node_exports = {};
20
20
  __export(node_exports, {
21
- AIServiceEdgeClient: () => import_chunk_7VH7NMIM.AIServiceEdgeClient,
21
+ AIServiceEdgeClient: () => import_chunk_CI5Q7FBG.AIServiceEdgeClient,
22
22
  AIServiecOverloadedError: () => AIServiecOverloadedError,
23
23
  AISession: () => AISession,
24
- DEFAULT_EDGE_MODEL: () => import_chunk_7VH7NMIM.DEFAULT_EDGE_MODEL,
25
- DEFAULT_EDGE_MODELS: () => import_chunk_7VH7NMIM.DEFAULT_EDGE_MODELS,
26
- DEFAULT_OLLAMA_ENDPOINT: () => import_chunk_7VH7NMIM.DEFAULT_OLLAMA_ENDPOINT,
27
- DEFAULT_OLLAMA_MODEL: () => import_chunk_7VH7NMIM.DEFAULT_OLLAMA_MODEL,
28
- DEFAULT_OLLAMA_MODELS: () => import_chunk_7VH7NMIM.DEFAULT_OLLAMA_MODELS,
29
- EchoDataSource: () => import_chunk_7VH7NMIM.EchoDataSource,
30
- GenerateRequest: () => import_chunk_7VH7NMIM.GenerateRequest,
31
- GenerateResponse: () => import_chunk_7VH7NMIM.GenerateResponse,
32
- GenerationStreamImpl: () => import_chunk_7VH7NMIM.GenerationStreamImpl,
33
- LLMModel: () => import_chunk_7VH7NMIM.LLMModel,
34
- MessageCollector: () => import_chunk_7VH7NMIM.MessageCollector,
35
- MixedStreamParser: () => import_chunk_7VH7NMIM.MixedStreamParser,
36
- OllamaClient: () => import_chunk_7VH7NMIM.OllamaClient,
37
- StreamTransform: () => import_chunk_7VH7NMIM.StreamTransform,
38
- ToolTypes: () => import_chunk_7VH7NMIM.ToolTypes,
39
- createArtifactElement: () => import_chunk_7VH7NMIM.createArtifactElement,
40
- createGenerationStream: () => import_chunk_7VH7NMIM.createGenerationStream,
41
- emitMessageAsEvents: () => import_chunk_7VH7NMIM.emitMessageAsEvents,
42
- executeQuery: () => import_chunk_7VH7NMIM.executeQuery,
43
- executeQueryPlan: () => import_chunk_7VH7NMIM.executeQueryPlan,
44
- formatInferredRelationshipLabel: () => import_chunk_7VH7NMIM.formatInferredRelationshipLabel,
45
- formatJsonSchemaForLLM: () => import_chunk_7VH7NMIM.formatJsonSchemaForLLM,
46
- formatNodeLabel: () => import_chunk_7VH7NMIM.formatNodeLabel,
47
- isReference: () => import_chunk_7VH7NMIM.isReference,
48
- isToolUse: () => import_chunk_7VH7NMIM.isToolUse,
49
- mergeMessageBlock: () => import_chunk_7VH7NMIM.mergeMessageBlock,
50
- parseCypherQuery: () => import_chunk_7VH7NMIM.parseCypherQuery,
51
- runLLM: () => import_chunk_7VH7NMIM.runLLM,
52
- runTools: () => import_chunk_7VH7NMIM.runTools
24
+ DEFAULT_EDGE_MODEL: () => import_chunk_CI5Q7FBG.DEFAULT_EDGE_MODEL,
25
+ DEFAULT_EDGE_MODELS: () => import_chunk_CI5Q7FBG.DEFAULT_EDGE_MODELS,
26
+ DEFAULT_OLLAMA_ENDPOINT: () => import_chunk_CI5Q7FBG.DEFAULT_OLLAMA_ENDPOINT,
27
+ DEFAULT_OLLAMA_MODEL: () => import_chunk_CI5Q7FBG.DEFAULT_OLLAMA_MODEL,
28
+ DEFAULT_OLLAMA_MODELS: () => import_chunk_CI5Q7FBG.DEFAULT_OLLAMA_MODELS,
29
+ EchoDataSource: () => import_chunk_CI5Q7FBG.EchoDataSource,
30
+ GenerateRequest: () => import_chunk_CI5Q7FBG.GenerateRequest,
31
+ GenerateResponse: () => import_chunk_CI5Q7FBG.GenerateResponse,
32
+ GenerationStreamImpl: () => import_chunk_CI5Q7FBG.GenerationStreamImpl,
33
+ LLMModel: () => import_chunk_CI5Q7FBG.LLMModel,
34
+ MessageCollector: () => import_chunk_CI5Q7FBG.MessageCollector,
35
+ MixedStreamParser: () => import_chunk_CI5Q7FBG.MixedStreamParser,
36
+ OllamaClient: () => import_chunk_CI5Q7FBG.OllamaClient,
37
+ StreamTransform: () => import_chunk_CI5Q7FBG.StreamTransform,
38
+ ToolTypes: () => import_chunk_CI5Q7FBG.ToolTypes,
39
+ VersionPin: () => VersionPin,
40
+ createArtifactElement: () => import_chunk_CI5Q7FBG.createArtifactElement,
41
+ createGenerationStream: () => import_chunk_CI5Q7FBG.createGenerationStream,
42
+ emitMessageAsEvents: () => import_chunk_CI5Q7FBG.emitMessageAsEvents,
43
+ executeQuery: () => import_chunk_CI5Q7FBG.executeQuery,
44
+ executeQueryPlan: () => import_chunk_CI5Q7FBG.executeQueryPlan,
45
+ formatInferredRelationshipLabel: () => import_chunk_CI5Q7FBG.formatInferredRelationshipLabel,
46
+ formatJsonSchemaForLLM: () => import_chunk_CI5Q7FBG.formatJsonSchemaForLLM,
47
+ formatNodeLabel: () => import_chunk_CI5Q7FBG.formatNodeLabel,
48
+ isReference: () => import_chunk_CI5Q7FBG.isReference,
49
+ isToolUse: () => import_chunk_CI5Q7FBG.isToolUse,
50
+ mergeMessageBlock: () => import_chunk_CI5Q7FBG.mergeMessageBlock,
51
+ parseCypherQuery: () => import_chunk_CI5Q7FBG.parseCypherQuery,
52
+ runLLM: () => import_chunk_CI5Q7FBG.runLLM,
53
+ runTools: () => import_chunk_CI5Q7FBG.runTools
53
54
  });
54
55
  module.exports = __toCommonJS(node_exports);
55
- var import_chunk_7VH7NMIM = require("./chunk-7VH7NMIM.cjs");
56
+ var import_chunk_CI5Q7FBG = require("./chunk-CI5Q7FBG.cjs");
56
57
  var import_effect = require("effect");
57
58
  var import_artifact = require("@dxos/artifact");
58
59
  var import_async = require("@dxos/async");
60
+ var import_echo_db = require("@dxos/echo-db");
59
61
  var import_echo_schema = require("@dxos/echo-schema");
60
62
  var import_invariant = require("@dxos/invariant");
61
63
  var import_log = require("@dxos/log");
64
+ var import_effect2 = require("effect");
65
+ var import_echo_db2 = require("@dxos/echo-db");
66
+ var import_echo_schema2 = require("@dxos/echo-schema");
67
+ var ObjectVersionSchema = import_effect2.Schema.Unknown;
68
+ var VersionPinSchema = import_effect2.Schema.Struct({
69
+ // TODO(dmaretskyi): Use Ref when those support encoding.
70
+ objectId: import_echo_schema2.ObjectId,
71
+ // TODO(dmaretskyi): Could be opaque
72
+ version: ObjectVersionSchema
73
+ });
74
+ var VersionPin = class extends VersionPinSchema {
75
+ static {
76
+ this.DISPOSITION = "version-pin";
77
+ }
78
+ static fromObject(object) {
79
+ return VersionPin.make({
80
+ objectId: object.id,
81
+ version: (0, import_echo_db2.getVersion)(object)
82
+ });
83
+ }
84
+ static createBlock(pin) {
85
+ return {
86
+ type: "json",
87
+ disposition: VersionPin.DISPOSITION,
88
+ json: JSON.stringify(pin)
89
+ };
90
+ }
91
+ };
62
92
  function _ts_decorate(decorators, target, key, desc) {
63
93
  var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
64
94
  if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
@@ -69,7 +99,7 @@ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/experimental/assistant/
69
99
  var AISession = class _AISession {
70
100
  constructor(_options) {
71
101
  this._options = _options;
72
- this._parser = new import_chunk_7VH7NMIM.MixedStreamParser();
102
+ this._parser = new import_chunk_CI5Q7FBG.MixedStreamParser();
73
103
  this._pending = [];
74
104
  this._history = [];
75
105
  this.message = this._parser.message;
@@ -82,152 +112,28 @@ var AISession = class _AISession {
82
112
  });
83
113
  }
84
114
  async run(options) {
85
- const systemTools = [
86
- (0, import_artifact.defineTool)("system", {
87
- name: "query_artifact_definitions",
88
- description: "Query the available artifact definitions",
89
- schema: import_effect.Schema.Struct({}),
90
- execute: async () => {
91
- return import_artifact.ToolResult.Success({
92
- artifactDefinitions: options.artifacts.map((artifact) => ({
93
- id: artifact.id,
94
- name: artifact.name,
95
- description: artifact.description
96
- }))
97
- });
98
- }
99
- })
100
- ];
115
+ const systemTools = [];
101
116
  switch (this._options.operationModel) {
102
117
  case "planning":
103
- systemTools.push((0, import_artifact.defineTool)("system", {
104
- name: "create_plan",
105
- 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.",
106
- schema: import_effect.Schema.Struct({
107
- goal: import_effect.Schema.String.annotations({
108
- description: "The goal that the plan will achieve."
109
- }),
110
- steps: import_effect.Schema.Array(import_effect.Schema.Struct({
111
- action: import_effect.Schema.String.annotations({
112
- description: "Complete, detailed action to perform."
113
- }),
114
- input: import_effect.Schema.String.annotations({
115
- description: "The required input data for this step. First step must not require any data."
116
- }),
117
- output: import_effect.Schema.String.annotations({
118
- description: "The output data from this step. This is passed to the next step as input."
119
- }),
120
- requiredArtifactIds: import_effect.Schema.Array(import_effect.Schema.String).annotations({
121
- description: "The ids of the artifacts required to perform this step.",
122
- examples: [
123
- [
124
- "artifact:dxos.org/example/Test"
125
- ]
126
- ]
127
- })
128
- })).annotations({
129
- description: "Steps"
130
- })
131
- }),
132
- execute: async ({ goal, steps }) => {
133
- const missingArtifactIds = steps.flatMap((step) => step.requiredArtifactIds).filter((artifactId) => !options.artifacts.some((artifact) => artifact.id === artifactId));
134
- if (missingArtifactIds.length > 0) {
135
- return import_artifact.ToolResult.Error(`One or more artifact ids are invalid: ${missingArtifactIds.join(", ")}`);
136
- }
137
- const stepResults = [];
138
- for (const step of steps) {
139
- (0, import_log.log)("executing step", {
140
- action: step.action
141
- }, {
142
- F: __dxlog_file,
143
- L: 160,
144
- S: this,
145
- C: (f, a) => f(...a)
146
- });
147
- const session = new _AISession({
148
- operationModel: "immediate"
149
- });
150
- session.message.on((ev) => this.message.emit(ev));
151
- session.block.on((ev) => this.block.emit(ev));
152
- session.update.on((ev) => this.update.emit(ev));
153
- session.streamEvent.on((ev) => this.streamEvent.emit(ev));
154
- session.userMessage.on((ev) => this.userMessage.emit(ev));
155
- const messages = await session.run({
156
- client: options.client,
157
- artifacts: options.artifacts,
158
- tools: options.tools,
159
- history: options.history,
160
- extensions: options.extensions,
161
- generationOptions: options.generationOptions,
162
- requiredArtifactIds: step.requiredArtifactIds.slice(),
163
- prompt: `
164
- You are an agent that executes the subtask in a plan.
165
-
166
- While you know the plan's goal only focus on your subtask
167
- Use the data from the previous step to complete your task.
168
- Return all of the data from "output" in your last message.
169
-
170
- Current step (that you need to complete):
171
- ${JSON.stringify({
172
- goal,
173
- input: step.input,
174
- output: step.output,
175
- yourTask: step.action
176
- }, null, 2)}
177
-
178
- Previous step results:
179
- ${JSON.stringify(stepResults.at(-1))}.
180
- `
181
- });
182
- const result = messages.at(-1);
183
- stepResults.push(result);
184
- }
185
- return import_artifact.ToolResult.Success({
186
- stepResults
187
- });
188
- }
189
- }));
118
+ systemTools.push(this._createQueryArtifactsTool(options.artifacts), this._createPlanningTool(options));
190
119
  break;
191
- case "immediate":
192
- systemTools.push((0, import_artifact.defineTool)("system", {
193
- name: "require_artifact_definitions",
194
- description: "Require the use of specific artifact definitions. This will allow the model to interact with artifact definitions and use their tools.",
195
- schema: import_effect.Schema.Struct({
196
- artifactDefinitionIds: import_effect.Schema.Array(import_effect.Schema.String).annotations({
197
- description: "The ids of the artifact definitions to require",
198
- examples: [
199
- [
200
- "artifact:dxos.org/example/Test"
201
- ]
202
- ]
203
- })
204
- }),
205
- execute: async ({ artifactDefinitionIds }) => {
206
- const missingArtifactDefinitionIds = artifactDefinitionIds.filter((artifactId) => !options.artifacts.some((artifact) => artifact.id === artifactId));
207
- if (missingArtifactDefinitionIds.length > 0) {
208
- return import_artifact.ToolResult.Error(`One or more artifact definition ids are invalid: ${missingArtifactDefinitionIds.join(", ")}`);
209
- }
210
- for (const artifactDefinitionId of artifactDefinitionIds) {
211
- requiredArtifactIds.add(artifactDefinitionId);
212
- }
213
- return import_artifact.ToolResult.Success({});
120
+ case "importing":
121
+ systemTools.push(this._createQueryArtifactsTool(options.artifacts), this._createRequireTool(options.artifacts, (artifactDefinitionIds) => {
122
+ for (const artifactDefinitionId of artifactDefinitionIds) {
123
+ requiredArtifactIds.add(artifactDefinitionId);
214
124
  }
215
125
  }));
216
126
  break;
127
+ case "configured":
128
+ break;
129
+ default:
130
+ throw new TypeError(`Invalid operation model: ${this._options.operationModel}`);
217
131
  }
218
132
  this._history = [
219
133
  ...options.history
220
134
  ];
221
135
  this._pending = [
222
- (0, import_echo_schema.create)(import_artifact.Message, {
223
- role: "user",
224
- content: [
225
- {
226
- type: "text",
227
- text: options.prompt
228
- }
229
- ]
230
- })
136
+ await this._formatUserPrompt(options.artifactDiffResolver, options.prompt, options.history)
231
137
  ];
232
138
  this.userMessage.emit(this._pending.at(-1));
233
139
  this._stream = void 0;
@@ -247,7 +153,7 @@ var AISession = class _AISession {
247
153
  tools: tools.map((tool) => tool.name)
248
154
  }, {
249
155
  F: __dxlog_file,
250
- L: 264,
156
+ L: 180,
251
157
  S: this,
252
158
  C: (f, a) => f(...a)
253
159
  });
@@ -270,7 +176,7 @@ var AISession = class _AISession {
270
176
  pending: this._pending
271
177
  }, {
272
178
  F: __dxlog_file,
273
- L: 289,
179
+ L: 205,
274
180
  S: this,
275
181
  C: (f, a) => f(...a)
276
182
  });
@@ -278,21 +184,21 @@ var AISession = class _AISession {
278
184
  const message = this._pending.at(-1);
279
185
  (0, import_invariant.invariant)(message, void 0, {
280
186
  F: __dxlog_file,
281
- L: 294,
187
+ L: 210,
282
188
  S: this,
283
189
  A: [
284
190
  "message",
285
191
  ""
286
192
  ]
287
193
  });
288
- if ((0, import_chunk_7VH7NMIM.isToolUse)(message)) {
194
+ if ((0, import_chunk_CI5Q7FBG.isToolUse)(message)) {
289
195
  (0, import_log.log)("tool request...", void 0, {
290
196
  F: __dxlog_file,
291
- L: 296,
197
+ L: 212,
292
198
  S: this,
293
199
  C: (f, a) => f(...a)
294
200
  });
295
- const response = await (0, import_chunk_7VH7NMIM.runTools)({
201
+ const response = await (0, import_chunk_CI5Q7FBG.runTools)({
296
202
  message: this._pending.at(-1),
297
203
  tools,
298
204
  extensions: options.extensions ?? {}
@@ -301,7 +207,7 @@ var AISession = class _AISession {
301
207
  response
302
208
  }, {
303
209
  F: __dxlog_file,
304
- L: 303,
210
+ L: 219,
305
211
  S: this,
306
212
  C: (f, a) => f(...a)
307
213
  });
@@ -321,7 +227,7 @@ var AISession = class _AISession {
321
227
  } catch (err) {
322
228
  import_log.log.catch(err, void 0, {
323
229
  F: __dxlog_file,
324
- L: 315,
230
+ L: 231,
325
231
  S: this,
326
232
  C: (f, a) => f(...a)
327
233
  });
@@ -342,6 +248,180 @@ var AISession = class _AISession {
342
248
  }
343
249
  return this._pending;
344
250
  }
251
+ async _formatUserPrompt(artifactDiffResolver, prompt, history) {
252
+ const prelude = [];
253
+ if (artifactDiffResolver) {
254
+ const versions = gatherObjectVersions(history);
255
+ const artifactDiff = await artifactDiffResolver(Array.from(versions.entries()).map(([id, version]) => ({
256
+ id,
257
+ lastVersion: version
258
+ })));
259
+ import_log.log.info("vision", {
260
+ artifactDiff,
261
+ versions
262
+ }, {
263
+ F: __dxlog_file,
264
+ L: 262,
265
+ S: this,
266
+ C: (f, a) => f(...a)
267
+ });
268
+ for (const [id, { version }] of [
269
+ ...artifactDiff.entries()
270
+ ]) {
271
+ if (import_echo_db.ObjectVersion.equals(version, versions.get(id))) {
272
+ artifactDiff.delete(id);
273
+ continue;
274
+ }
275
+ prelude.push(VersionPin.createBlock(VersionPin.make({
276
+ objectId: id,
277
+ version
278
+ })));
279
+ }
280
+ if (artifactDiff.size > 0) {
281
+ prelude.push(createArtifactUpdateBlock(artifactDiff));
282
+ }
283
+ }
284
+ return (0, import_echo_schema.create)(import_artifact.Message, {
285
+ role: "user",
286
+ content: [
287
+ ...prelude,
288
+ {
289
+ type: "text",
290
+ text: prompt
291
+ }
292
+ ]
293
+ });
294
+ }
295
+ _createQueryArtifactsTool(artifacts) {
296
+ return (0, import_artifact.defineTool)("system", {
297
+ name: "query_artifact_definitions",
298
+ description: "Query the available artifact definitions",
299
+ schema: import_effect.Schema.Struct({}),
300
+ execute: async () => {
301
+ return import_artifact.ToolResult.Success({
302
+ artifactDefinitions: artifacts.map((artifact) => ({
303
+ id: artifact.id,
304
+ name: artifact.name,
305
+ description: artifact.description
306
+ }))
307
+ });
308
+ }
309
+ });
310
+ }
311
+ _createRequireTool(artifacts, onRequire) {
312
+ return (0, import_artifact.defineTool)("system", {
313
+ name: "require_artifact_definitions",
314
+ description: "Require the use of specific artifact definitions. This will allow the model to interact with artifact definitions and use their tools.",
315
+ schema: import_effect.Schema.Struct({
316
+ artifactDefinitionIds: import_effect.Schema.Array(import_effect.Schema.String).annotations({
317
+ description: "The ids of the artifact definitions to require",
318
+ examples: [
319
+ [
320
+ "artifact:dxos.org/example/Test"
321
+ ]
322
+ ]
323
+ })
324
+ }),
325
+ execute: async ({ artifactDefinitionIds }) => {
326
+ const missingArtifactDefinitionIds = artifactDefinitionIds.filter((artifactId) => !artifacts.some((artifact) => artifact.id === artifactId));
327
+ if (missingArtifactDefinitionIds.length > 0) {
328
+ return import_artifact.ToolResult.Error(`One or more artifact definition ids are invalid: ${missingArtifactDefinitionIds.join(", ")}`);
329
+ }
330
+ onRequire(artifactDefinitionIds);
331
+ return import_artifact.ToolResult.Success({});
332
+ }
333
+ });
334
+ }
335
+ _createPlanningTool(options) {
336
+ return (0, import_artifact.defineTool)("system", {
337
+ name: "create_plan",
338
+ 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.",
339
+ schema: import_effect.Schema.Struct({
340
+ goal: import_effect.Schema.String.annotations({
341
+ description: "The goal that the plan will achieve."
342
+ }),
343
+ steps: import_effect.Schema.Array(import_effect.Schema.Struct({
344
+ action: import_effect.Schema.String.annotations({
345
+ description: "Complete, detailed action to perform."
346
+ }),
347
+ input: import_effect.Schema.String.annotations({
348
+ description: "The required input data for this step. First step must not require any data."
349
+ }),
350
+ output: import_effect.Schema.String.annotations({
351
+ description: "The output data from this step. This is passed to the next step as input."
352
+ }),
353
+ requiredArtifactIds: import_effect.Schema.Array(import_effect.Schema.String).annotations({
354
+ description: "The ids of the artifacts required to perform this step.",
355
+ examples: [
356
+ [
357
+ "artifact:dxos.org/example/Test"
358
+ ]
359
+ ]
360
+ })
361
+ })).annotations({
362
+ description: "Steps"
363
+ })
364
+ }),
365
+ execute: async ({ goal, steps }) => {
366
+ const missingArtifactIds = steps.flatMap((step) => step.requiredArtifactIds).filter((artifactId) => !options.artifacts.some((artifact) => artifact.id === artifactId));
367
+ if (missingArtifactIds.length > 0) {
368
+ return import_artifact.ToolResult.Error(`One or more artifact ids are invalid: ${missingArtifactIds.join(", ")}`);
369
+ }
370
+ const stepResults = [];
371
+ for (const step of steps) {
372
+ (0, import_log.log)("executing step", {
373
+ action: step.action
374
+ }, {
375
+ F: __dxlog_file,
376
+ L: 369,
377
+ S: this,
378
+ C: (f, a) => f(...a)
379
+ });
380
+ const session = new _AISession({
381
+ operationModel: "importing"
382
+ });
383
+ session.message.on((ev) => this.message.emit(ev));
384
+ session.block.on((ev) => this.block.emit(ev));
385
+ session.update.on((ev) => this.update.emit(ev));
386
+ session.streamEvent.on((ev) => this.streamEvent.emit(ev));
387
+ session.userMessage.on((ev) => this.userMessage.emit(ev));
388
+ const messages = await session.run({
389
+ client: options.client,
390
+ artifacts: options.artifacts,
391
+ tools: options.tools,
392
+ history: options.history,
393
+ extensions: options.extensions,
394
+ generationOptions: options.generationOptions,
395
+ requiredArtifactIds: step.requiredArtifactIds.slice(),
396
+ artifactDiffResolver: options.artifactDiffResolver,
397
+ prompt: `
398
+ You are an agent that executes the subtask in a plan.
399
+
400
+ While you know the plan's goal only focus on your subtask
401
+ Use the data from the previous step to complete your task.
402
+ Return all of the data from "output" in your last message.
403
+
404
+ Current step (that you need to complete):
405
+ ${JSON.stringify({
406
+ goal,
407
+ input: step.input,
408
+ output: step.output,
409
+ yourTask: step.action
410
+ }, null, 2)}
411
+
412
+ Previous step results:
413
+ ${JSON.stringify(stepResults.at(-1))}.
414
+ `
415
+ });
416
+ const result = messages.at(-1);
417
+ stepResults.push(result);
418
+ }
419
+ return import_artifact.ToolResult.Success({
420
+ stepResults
421
+ });
422
+ }
423
+ });
424
+ }
345
425
  abort() {
346
426
  this._stream?.abort();
347
427
  }
@@ -359,21 +439,57 @@ var createBaseInstructions = ({ availableArtifacts, operationModel }) => `
359
439
  Decision-making:
360
440
 
361
441
  - Analyze the structure and type of the content in the user's message.
362
- - Can you complete the task using the available artifacts?
363
- - If you can't complete the task using the available artifacts, query the list of available artifacts using the appropriate tool.
364
- - Identify which artifacts are relevant to the user's request.
442
+
443
+
365
444
  ${operationModel === "planning" ? `
445
+ - Can you complete the task using the available artifacts?
446
+ - If you can't complete the task using the available artifacts, query the list of available artifacts using the appropriate tool.
447
+ - Identify which artifacts are relevant to the user's request.
366
448
  - 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.
367
- ` : `
368
- - Are the required artifacts already available?
369
- - If not, select which artifact(s) will be the most relevant and require them using the require_artifacts tool.
370
- - The require'd artifact tools will be available for use after require.
371
- `}
449
+ ` : ""}
450
+ ${operationModel === "importing" ? `
451
+ - Can you complete the task using the available artifacts?
452
+ - If you can't complete the task using the available artifacts, query the list of available artifacts using the appropriate tool.
453
+ - Identify which artifacts are relevant to the user's request.
454
+ - Are the required artifacts already available?
455
+ - If not, select which artifact(s) will be the most relevant and require them using the require_artifacts tool.
456
+ - The require'd artifact tools will be available for use after require.
457
+ ` : ""}
458
+ ${operationModel === "configured" ? `
459
+ - Select the most relevant artifact(s) to complete the task.
460
+ - Call the appropriate tool to use the artifact(s).
461
+ ` : ""}
372
462
 
373
463
  ${availableArtifacts.length > 0 ? `Artifacts already in context: ${availableArtifacts.join("\n")}` : ""}
374
464
  `;
375
465
  var AIServiecOverloadedError = class extends import_effect.Schema.TaggedError()("AIServiecOverloadedError", {}) {
376
466
  };
467
+ var gatherObjectVersions = (messages) => {
468
+ const artifactIds = /* @__PURE__ */ new Map();
469
+ for (const message of messages) {
470
+ for (const block of message.content) {
471
+ if (block.type === "json" && block.disposition === VersionPin.DISPOSITION) {
472
+ const pin = VersionPin.pipe(import_effect.Schema.decodeOption)(JSON.parse(block.json)).pipe(import_effect.Option.getOrUndefined);
473
+ if (!pin) {
474
+ continue;
475
+ }
476
+ artifactIds.set(pin.objectId, pin.version);
477
+ }
478
+ }
479
+ }
480
+ return artifactIds;
481
+ };
482
+ var createArtifactUpdateBlock = (artifactDiff) => {
483
+ return {
484
+ type: "text",
485
+ disposition: "artifact-update",
486
+ text: `
487
+ The following artifacts have been updated since the last message:
488
+ ${Array.from(artifactDiff.entries()).map(([id, { diff }]) => `<changed-artifact id="${id}">${diff ? `
489
+ ${diff}` : ""}</changed-artifact>`).join("\n")}
490
+ `
491
+ };
492
+ };
377
493
  // Annotate the CommonJS export names for ESM import in node:
378
494
  0 && (module.exports = {
379
495
  AIServiceEdgeClient,
@@ -394,6 +510,7 @@ var AIServiecOverloadedError = class extends import_effect.Schema.TaggedError()(
394
510
  OllamaClient,
395
511
  StreamTransform,
396
512
  ToolTypes,
513
+ VersionPin,
397
514
  createArtifactElement,
398
515
  createGenerationStream,
399
516
  emitMessageAsEvents,