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