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