@dxos/assistant 0.8.2-main.f11618f → 0.8.2-staging.7ac8446
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-DEIGOD4U.mjs → chunk-PND5VR6D.mjs} +84 -91
- package/dist/lib/browser/chunk-PND5VR6D.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +153 -275
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +25 -25
- package/dist/lib/browser/testing/index.mjs.map +3 -3
- package/dist/lib/node/{chunk-7VH7NMIM.cjs → chunk-IBOBRBDF.cjs} +98 -106
- package/dist/lib/node/chunk-IBOBRBDF.cjs.map +7 -0
- package/dist/lib/node/index.cjs +183 -302
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +33 -33
- package/dist/lib/node/testing/index.cjs.map +3 -3
- package/dist/lib/node-esm/{chunk-7JI4MDGL.mjs → chunk-DCQJOLSR.mjs} +84 -91
- package/dist/lib/node-esm/chunk-DCQJOLSR.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +153 -275
- 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 +25 -25
- package/dist/lib/node-esm/testing/index.mjs.map +3 -3
- package/dist/types/src/ai-service/edge-client.d.ts +5 -4
- package/dist/types/src/ai-service/edge-client.d.ts.map +1 -1
- package/dist/types/src/ai-service/message-collector.d.ts +6 -0
- package/dist/types/src/ai-service/message-collector.d.ts.map +1 -1
- package/dist/types/src/ai-service/ollama-client.d.ts +2 -6
- package/dist/types/src/ai-service/ollama-client.d.ts.map +1 -1
- package/dist/types/src/ai-service/parser.d.ts +3 -0
- package/dist/types/src/ai-service/parser.d.ts.map +1 -1
- package/dist/types/src/ai-service/service.d.ts +2 -18
- package/dist/types/src/ai-service/service.d.ts.map +1 -1
- package/dist/types/src/ai-service/testing/mock.d.ts +2 -6
- package/dist/types/src/ai-service/testing/mock.d.ts.map +1 -1
- package/dist/types/src/ai-service/types.d.ts +9 -52
- package/dist/types/src/ai-service/types.d.ts.map +1 -1
- package/dist/types/src/conversation/conversation.d.ts +3 -0
- package/dist/types/src/conversation/conversation.d.ts.map +1 -1
- package/dist/types/src/conversation/types.d.ts +3 -2
- 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 +0 -1
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/session.d.ts +10 -29
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/testing/test-schema.d.ts +2 -2
- package/dist/types/src/testing/test-schema.d.ts.map +1 -1
- package/package.json +13 -13
- package/src/ai-service/client.test.ts +68 -50
- package/src/ai-service/edge-client.ts +20 -13
- package/src/ai-service/ollama-client.ts +2 -9
- package/src/ai-service/parser.ts +2 -13
- package/src/ai-service/service.ts +3 -19
- package/src/ai-service/testing/mock.ts +1 -9
- package/src/ai-service/types.ts +6 -15
- package/src/bin/chat.ts +3 -3
- package/src/conversation/conversation.ts +11 -5
- package/src/conversation/types.ts +4 -1
- package/src/cypher/combinators.test.ts +4 -4
- package/src/cypher/echo-data-source.ts +2 -1
- 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 +0 -1
- package/src/session/session.test.ts +7 -8
- package/src/session/session.ts +148 -284
- package/src/testing/query.test.ts +19 -17
- package/src/testing/test-data.ts +22 -22
- package/src/testing/test-schema.ts +3 -3
- package/dist/lib/browser/chunk-DEIGOD4U.mjs.map +0 -7
- package/dist/lib/node/chunk-7VH7NMIM.cjs.map +0 -7
- package/dist/lib/node-esm/chunk-7JI4MDGL.mjs.map +0 -7
- package/dist/types/src/session/version-pin.d.ts +0 -17
- package/dist/types/src/session/version-pin.d.ts.map +0 -1
- package/dist/types/src/session/version-pin.test.d.ts +0 -2
- package/dist/types/src/session/version-pin.test.d.ts.map +0 -1
- package/src/session/version-pin.test.ts +0 -26
- package/src/session/version-pin.ts +0 -43
|
@@ -25,7 +25,7 @@ test.skip('run', async ({ expect }) => {
|
|
|
25
25
|
const result = await executeQuery(
|
|
26
26
|
dataSource,
|
|
27
27
|
`
|
|
28
|
-
MATCH (org:
|
|
28
|
+
MATCH (org:Org {name: 'DXOS'})-[:ORG_EMPLOYEES]->(c:Contact)<-[:TASK_ASSIGNEE]-(t:Task)-[:TASK_PROJECT]->(p:Project {name: 'Composer'})
|
|
29
29
|
RETURN c.name, t.name
|
|
30
30
|
`,
|
|
31
31
|
);
|
|
@@ -7,10 +7,10 @@ import { test } from 'vitest';
|
|
|
7
7
|
import { toJsonSchema } from '@dxos/echo-schema';
|
|
8
8
|
|
|
9
9
|
import { formatJsonSchemaForLLM } from './schema';
|
|
10
|
-
import { Contact,
|
|
10
|
+
import { Contact, Org, Project, Task } from '../testing';
|
|
11
11
|
|
|
12
|
-
test('
|
|
13
|
-
const jsonSchema = [
|
|
12
|
+
test('Org-Project-Task-Contact schema', async ({ expect }) => {
|
|
13
|
+
const jsonSchema = [Org, Project, Task, Contact].map((schema) => toJsonSchema(schema));
|
|
14
14
|
const result = formatJsonSchemaForLLM(jsonSchema);
|
|
15
15
|
// log('result', { result });
|
|
16
16
|
expect(result).toHaveLength(8);
|
package/src/session/index.ts
CHANGED
|
@@ -15,7 +15,7 @@ import {
|
|
|
15
15
|
type MessageContentBlock,
|
|
16
16
|
} from '@dxos/artifact';
|
|
17
17
|
import { AIServiceEdgeClient } from '@dxos/assistant';
|
|
18
|
-
import {
|
|
18
|
+
import { createStatic, EchoObject, ObjectId } from '@dxos/echo-schema';
|
|
19
19
|
import { DXN } from '@dxos/keys';
|
|
20
20
|
import { log } from '@dxos/log';
|
|
21
21
|
|
|
@@ -39,7 +39,7 @@ describe.skip('AISession with Ollama', () => {
|
|
|
39
39
|
// const aiClient = new OllamaClient({
|
|
40
40
|
// overrides: { model: 'llama3.1:8b' },
|
|
41
41
|
// });
|
|
42
|
-
const session = new AISession({ operationModel: '
|
|
42
|
+
const session = new AISession({ operationModel: 'immediate' });
|
|
43
43
|
|
|
44
44
|
const objects = new Set<string>();
|
|
45
45
|
|
|
@@ -140,7 +140,6 @@ describe.skip('AISession with Ollama', () => {
|
|
|
140
140
|
client: aiClient,
|
|
141
141
|
tools: [],
|
|
142
142
|
artifacts: [calendarArtifact, tableArtifact, mapArtifact, scriptArtifact],
|
|
143
|
-
requiredArtifactIds: [calendarArtifact.id, tableArtifact.id, mapArtifact.id, scriptArtifact.id],
|
|
144
143
|
history: [],
|
|
145
144
|
generationOptions: {
|
|
146
145
|
model: '@anthropic/claude-3-5-haiku-20241022',
|
|
@@ -157,31 +156,31 @@ describe.skip('AISession with Ollama', () => {
|
|
|
157
156
|
|
|
158
157
|
// Travel to rome, florence, livorno, siena, madrid for conferences
|
|
159
158
|
const CALENDAR_EVENTS: CalendarEvent[] = [
|
|
160
|
-
|
|
159
|
+
createStatic(CalendarEventSchema, {
|
|
161
160
|
title: 'Exploring Ancient Ruins in Rome',
|
|
162
161
|
startTime: '2024-01-01T10:00:00Z',
|
|
163
162
|
endTime: '2024-01-01T11:00:00Z',
|
|
164
163
|
description: 'Tech conference at the historic Colosseum with networking opportunities',
|
|
165
164
|
}),
|
|
166
|
-
|
|
165
|
+
createStatic(CalendarEventSchema, {
|
|
167
166
|
title: 'Renaissance Tech Summit in Florence',
|
|
168
167
|
startTime: '2024-01-01T11:00:00Z',
|
|
169
168
|
endTime: '2024-01-01T12:00:00Z',
|
|
170
169
|
description: 'Discussing AI innovations surrounded by Renaissance art',
|
|
171
170
|
}),
|
|
172
|
-
|
|
171
|
+
createStatic(CalendarEventSchema, {
|
|
173
172
|
title: 'Travel to Livorno',
|
|
174
173
|
startTime: '2024-01-01T12:00:00Z',
|
|
175
174
|
endTime: '2024-01-01T13:00:00Z',
|
|
176
175
|
description: 'Travel to Livorno',
|
|
177
176
|
}),
|
|
178
|
-
|
|
177
|
+
createStatic(CalendarEventSchema, {
|
|
179
178
|
title: 'Travel to Siena',
|
|
180
179
|
startTime: '2024-01-01T13:00:00Z',
|
|
181
180
|
endTime: '2024-01-01T14:00:00Z',
|
|
182
181
|
description: 'Travel to Siena',
|
|
183
182
|
}),
|
|
184
|
-
|
|
183
|
+
createStatic(CalendarEventSchema, {
|
|
185
184
|
title: 'Travel to Madrid',
|
|
186
185
|
startTime: '2024-01-01T14:00:00Z',
|
|
187
186
|
endTime: '2024-01-01T15:00:00Z',
|
package/src/session/session.ts
CHANGED
|
@@ -2,26 +2,16 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
defineTool,
|
|
9
|
-
Message,
|
|
10
|
-
ToolResult,
|
|
11
|
-
type ArtifactDefinition,
|
|
12
|
-
type MessageContentBlock,
|
|
13
|
-
type Tool,
|
|
14
|
-
} from '@dxos/artifact';
|
|
5
|
+
import { Schema as S } from 'effect';
|
|
6
|
+
|
|
7
|
+
import { defineTool, Message, ToolResult, type ArtifactDefinition, type Tool } from '@dxos/artifact';
|
|
15
8
|
import { Event, synchronized } from '@dxos/async';
|
|
16
|
-
import {
|
|
17
|
-
import { create, type ObjectId } from '@dxos/echo-schema';
|
|
9
|
+
import { createStatic } from '@dxos/echo-schema';
|
|
18
10
|
import { invariant } from '@dxos/invariant';
|
|
19
11
|
import { log } from '@dxos/log';
|
|
20
12
|
|
|
21
|
-
import { VersionPin } from './version-pin';
|
|
22
13
|
import { MixedStreamParser, type AIServiceClient, type GenerateRequest, type GenerationStream } from '../ai-service';
|
|
23
14
|
import { isToolUse, runTools } from '../conversation';
|
|
24
|
-
|
|
25
15
|
/**
|
|
26
16
|
* Contains message history, tools, current context.
|
|
27
17
|
* Current context means the state of the app, time of day, and other contextual information.
|
|
@@ -33,22 +23,6 @@ import { isToolUse, runTools } from '../conversation';
|
|
|
33
23
|
* Could be personal or shared.
|
|
34
24
|
*/
|
|
35
25
|
|
|
36
|
-
/**
|
|
37
|
-
* Resolves artifact ids to their versions.
|
|
38
|
-
* Used to give the model a sense of the changes to the artifacts made by users during the conversation.
|
|
39
|
-
* The artifacts versions are pinned in the history, and whenever the artifact changes in-between assistant's steps,
|
|
40
|
-
* a diff is inserted into the conversation.
|
|
41
|
-
*/
|
|
42
|
-
export type ArtifactDiffResolver = (artifacts: { id: ObjectId; lastVersion: ObjectVersion }[]) => Promise<
|
|
43
|
-
Map<
|
|
44
|
-
ObjectId,
|
|
45
|
-
{
|
|
46
|
-
version: ObjectVersion;
|
|
47
|
-
diff?: string;
|
|
48
|
-
}
|
|
49
|
-
>
|
|
50
|
-
>;
|
|
51
|
-
|
|
52
26
|
export type SessionRunOptions = {
|
|
53
27
|
client: AIServiceClient;
|
|
54
28
|
|
|
@@ -73,22 +47,15 @@ export type SessionRunOptions = {
|
|
|
73
47
|
* Pre-require artifacts.
|
|
74
48
|
*/
|
|
75
49
|
requiredArtifactIds?: string[];
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* @see ArtifactDiffResolver
|
|
79
|
-
*/
|
|
80
|
-
artifactDiffResolver?: ArtifactDiffResolver;
|
|
81
50
|
};
|
|
82
51
|
|
|
83
|
-
type OperationModel = 'planning' | '
|
|
52
|
+
type OperationModel = 'planning' | 'immediate';
|
|
84
53
|
|
|
85
54
|
export type AiSessionOptions = {
|
|
86
55
|
/**
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
* - `importing`: The model can query registry and only pull in the artifacts that are required to complete the task.
|
|
91
|
-
* - `configured`: The available artifacts are pre-selected and the model cannot select additional artifacts.
|
|
56
|
+
* In planning mode, the model will create a plan before executing the user's request.
|
|
57
|
+
* The plan will determine the artifacts that will be used to perform the actions.
|
|
58
|
+
* In require mode, the model will select the artifacts that will be used to perform the actions.
|
|
92
59
|
*/
|
|
93
60
|
operationModel: OperationModel;
|
|
94
61
|
};
|
|
@@ -137,30 +104,147 @@ export class AISession {
|
|
|
137
104
|
|
|
138
105
|
@synchronized
|
|
139
106
|
async run(options: SessionRunOptions): Promise<Message[]> {
|
|
140
|
-
const systemTools: Tool[] = [
|
|
107
|
+
const systemTools: Tool[] = [
|
|
108
|
+
defineTool('system', {
|
|
109
|
+
name: 'query_artifact_definitions',
|
|
110
|
+
description: 'Query the available artifact definitions',
|
|
111
|
+
schema: S.Struct({}),
|
|
112
|
+
execute: async () => {
|
|
113
|
+
return ToolResult.Success({
|
|
114
|
+
artifactDefinitions: options.artifacts.map((artifact) => ({
|
|
115
|
+
id: artifact.id,
|
|
116
|
+
name: artifact.name,
|
|
117
|
+
description: artifact.description,
|
|
118
|
+
})),
|
|
119
|
+
});
|
|
120
|
+
},
|
|
121
|
+
}),
|
|
122
|
+
];
|
|
141
123
|
switch (this._options.operationModel) {
|
|
142
124
|
case 'planning':
|
|
143
|
-
systemTools.push(this._createQueryArtifactsTool(options.artifacts), this._createPlanningTool(options));
|
|
144
|
-
break;
|
|
145
|
-
case 'importing':
|
|
146
125
|
systemTools.push(
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
126
|
+
defineTool('system', {
|
|
127
|
+
name: 'create_plan',
|
|
128
|
+
description:
|
|
129
|
+
'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.',
|
|
130
|
+
schema: S.Struct({
|
|
131
|
+
goal: S.String.annotations({ description: 'The goal that the plan will achieve.' }),
|
|
132
|
+
steps: S.Array(
|
|
133
|
+
S.Struct({
|
|
134
|
+
action: S.String.annotations({
|
|
135
|
+
description: 'Complete, detailed action to perform.',
|
|
136
|
+
}),
|
|
137
|
+
input: S.String.annotations({
|
|
138
|
+
description: 'The required input data for this step. First step must not require any data.',
|
|
139
|
+
}),
|
|
140
|
+
output: S.String.annotations({
|
|
141
|
+
description: 'The output data from this step. This is passed to the next step as input.',
|
|
142
|
+
}),
|
|
143
|
+
requiredArtifactIds: S.Array(S.String).annotations({
|
|
144
|
+
description: 'The ids of the artifacts required to perform this step.',
|
|
145
|
+
examples: [['artifact:dxos.org/example/Test']],
|
|
146
|
+
}),
|
|
147
|
+
}),
|
|
148
|
+
).annotations({ description: 'Steps' }),
|
|
149
|
+
}),
|
|
150
|
+
execute: async ({ goal, steps }) => {
|
|
151
|
+
const missingArtifactIds = steps
|
|
152
|
+
.flatMap((step) => step.requiredArtifactIds)
|
|
153
|
+
.filter((artifactId) => !options.artifacts.some((artifact) => artifact.id === artifactId));
|
|
154
|
+
if (missingArtifactIds.length > 0) {
|
|
155
|
+
return ToolResult.Error(`One or more artifact ids are invalid: ${missingArtifactIds.join(', ')}`);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const stepResults: any[] = [];
|
|
159
|
+
for (const step of steps) {
|
|
160
|
+
log('executing step', { action: step.action });
|
|
161
|
+
const session = new AISession({ operationModel: 'immediate' });
|
|
162
|
+
session.message.on((ev) => this.message.emit(ev));
|
|
163
|
+
session.block.on((ev) => this.block.emit(ev));
|
|
164
|
+
session.update.on((ev) => this.update.emit(ev));
|
|
165
|
+
session.streamEvent.on((ev) => this.streamEvent.emit(ev));
|
|
166
|
+
session.userMessage.on((ev) => this.userMessage.emit(ev));
|
|
167
|
+
|
|
168
|
+
const messages = await session.run({
|
|
169
|
+
client: options.client,
|
|
170
|
+
artifacts: options.artifacts,
|
|
171
|
+
tools: options.tools,
|
|
172
|
+
history: options.history,
|
|
173
|
+
extensions: options.extensions,
|
|
174
|
+
generationOptions: options.generationOptions,
|
|
175
|
+
requiredArtifactIds: step.requiredArtifactIds.slice(),
|
|
176
|
+
prompt: `
|
|
177
|
+
You are an agent that executes the subtask in a plan.
|
|
178
|
+
|
|
179
|
+
While you know the plan's goal only focus on your subtask
|
|
180
|
+
Use the data from the previous step to complete your task.
|
|
181
|
+
Return all of the data from "output" in your last message.
|
|
182
|
+
|
|
183
|
+
Current step (that you need to complete):
|
|
184
|
+
${JSON.stringify(
|
|
185
|
+
{
|
|
186
|
+
goal,
|
|
187
|
+
input: step.input,
|
|
188
|
+
output: step.output,
|
|
189
|
+
yourTask: step.action,
|
|
190
|
+
},
|
|
191
|
+
null,
|
|
192
|
+
2,
|
|
193
|
+
)}
|
|
194
|
+
|
|
195
|
+
Previous step results:
|
|
196
|
+
${JSON.stringify(stepResults.at(-1))}.
|
|
197
|
+
`,
|
|
198
|
+
});
|
|
199
|
+
const result = messages.at(-1);
|
|
200
|
+
stepResults.push(result);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return ToolResult.Success({ stepResults });
|
|
204
|
+
},
|
|
152
205
|
}),
|
|
153
206
|
);
|
|
154
207
|
break;
|
|
155
|
-
case '
|
|
156
|
-
|
|
208
|
+
case 'immediate':
|
|
209
|
+
systemTools.push(
|
|
210
|
+
defineTool('system', {
|
|
211
|
+
name: 'require_artifact_definitions',
|
|
212
|
+
description:
|
|
213
|
+
'Require the use of specific artifact definitions. This will allow the model to interact with artifact definitions and use their tools.',
|
|
214
|
+
schema: S.Struct({
|
|
215
|
+
artifactDefinitionIds: S.Array(S.String).annotations({
|
|
216
|
+
description: 'The ids of the artifact definitions to require',
|
|
217
|
+
examples: [['artifact:dxos.org/example/Test']],
|
|
218
|
+
}),
|
|
219
|
+
}),
|
|
220
|
+
execute: async ({ artifactDefinitionIds }) => {
|
|
221
|
+
const missingArtifactDefinitionIds = artifactDefinitionIds.filter(
|
|
222
|
+
(artifactId) => !options.artifacts.some((artifact) => artifact.id === artifactId),
|
|
223
|
+
);
|
|
224
|
+
if (missingArtifactDefinitionIds.length > 0) {
|
|
225
|
+
return ToolResult.Error(
|
|
226
|
+
`One or more artifact definition ids are invalid: ${missingArtifactDefinitionIds.join(', ')}`,
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
for (const artifactDefinitionId of artifactDefinitionIds) {
|
|
231
|
+
requiredArtifactIds.add(artifactDefinitionId);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return ToolResult.Success({});
|
|
235
|
+
},
|
|
236
|
+
}),
|
|
237
|
+
);
|
|
157
238
|
break;
|
|
158
|
-
default:
|
|
159
|
-
throw new TypeError(`Invalid operation model: ${this._options.operationModel}`);
|
|
160
239
|
}
|
|
161
240
|
|
|
162
241
|
this._history = [...options.history];
|
|
163
|
-
this._pending = [
|
|
242
|
+
this._pending = [
|
|
243
|
+
createStatic(Message, {
|
|
244
|
+
role: 'user',
|
|
245
|
+
content: [{ type: 'text', text: options.prompt }],
|
|
246
|
+
}),
|
|
247
|
+
];
|
|
164
248
|
this.userMessage.emit(this._pending.at(-1)!);
|
|
165
249
|
this._stream = undefined;
|
|
166
250
|
let error: Error | undefined;
|
|
@@ -184,7 +268,7 @@ export class AISession {
|
|
|
184
268
|
});
|
|
185
269
|
|
|
186
270
|
// Open request stream.
|
|
187
|
-
this._stream = await options.client.
|
|
271
|
+
this._stream = await options.client.exec({
|
|
188
272
|
...(options.generationOptions ?? {}),
|
|
189
273
|
// TODO(burdon): Rename messages or separate history/message.
|
|
190
274
|
history: [...this._history, ...this._pending],
|
|
@@ -245,176 +329,6 @@ export class AISession {
|
|
|
245
329
|
return this._pending;
|
|
246
330
|
}
|
|
247
331
|
|
|
248
|
-
private async _formatUserPrompt(
|
|
249
|
-
artifactDiffResolver: ArtifactDiffResolver | undefined,
|
|
250
|
-
prompt: string,
|
|
251
|
-
history: Message[],
|
|
252
|
-
) {
|
|
253
|
-
const prelude: MessageContentBlock[] = [];
|
|
254
|
-
|
|
255
|
-
if (artifactDiffResolver) {
|
|
256
|
-
const versions = gatherObjectVersions(history);
|
|
257
|
-
|
|
258
|
-
const artifactDiff = await artifactDiffResolver(
|
|
259
|
-
Array.from(versions.entries()).map(([id, version]) => ({ id, lastVersion: version })),
|
|
260
|
-
);
|
|
261
|
-
|
|
262
|
-
log.info('vision', {
|
|
263
|
-
artifactDiff,
|
|
264
|
-
versions,
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
for (const [id, { version }] of [...artifactDiff.entries()]) {
|
|
268
|
-
if (ObjectVersion.equals(version, versions.get(id)!)) {
|
|
269
|
-
artifactDiff.delete(id);
|
|
270
|
-
continue;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
prelude.push(VersionPin.createBlock(VersionPin.make({ objectId: id, version })));
|
|
274
|
-
}
|
|
275
|
-
if (artifactDiff.size > 0) {
|
|
276
|
-
prelude.push(createArtifactUpdateBlock(artifactDiff));
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
return create(Message, {
|
|
281
|
-
role: 'user',
|
|
282
|
-
content: [...prelude, { type: 'text', text: prompt }],
|
|
283
|
-
});
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
private _createQueryArtifactsTool(artifacts: ArtifactDefinition[]) {
|
|
287
|
-
return defineTool('system', {
|
|
288
|
-
name: 'query_artifact_definitions',
|
|
289
|
-
description: 'Query the available artifact definitions',
|
|
290
|
-
schema: S.Struct({}),
|
|
291
|
-
execute: async () => {
|
|
292
|
-
return ToolResult.Success({
|
|
293
|
-
artifactDefinitions: artifacts.map((artifact) => ({
|
|
294
|
-
id: artifact.id,
|
|
295
|
-
name: artifact.name,
|
|
296
|
-
description: artifact.description,
|
|
297
|
-
})),
|
|
298
|
-
});
|
|
299
|
-
},
|
|
300
|
-
});
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
private _createRequireTool(
|
|
304
|
-
artifacts: ArtifactDefinition[],
|
|
305
|
-
onRequire: (artifactDefinitionIds: readonly string[]) => void,
|
|
306
|
-
) {
|
|
307
|
-
return defineTool('system', {
|
|
308
|
-
name: 'require_artifact_definitions',
|
|
309
|
-
description:
|
|
310
|
-
'Require the use of specific artifact definitions. This will allow the model to interact with artifact definitions and use their tools.',
|
|
311
|
-
schema: S.Struct({
|
|
312
|
-
artifactDefinitionIds: S.Array(S.String).annotations({
|
|
313
|
-
description: 'The ids of the artifact definitions to require',
|
|
314
|
-
examples: [['artifact:dxos.org/example/Test']],
|
|
315
|
-
}),
|
|
316
|
-
}),
|
|
317
|
-
execute: async ({ artifactDefinitionIds }) => {
|
|
318
|
-
const missingArtifactDefinitionIds = artifactDefinitionIds.filter(
|
|
319
|
-
(artifactId) => !artifacts.some((artifact) => artifact.id === artifactId),
|
|
320
|
-
);
|
|
321
|
-
if (missingArtifactDefinitionIds.length > 0) {
|
|
322
|
-
return ToolResult.Error(
|
|
323
|
-
`One or more artifact definition ids are invalid: ${missingArtifactDefinitionIds.join(', ')}`,
|
|
324
|
-
);
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
onRequire(artifactDefinitionIds);
|
|
328
|
-
|
|
329
|
-
return ToolResult.Success({});
|
|
330
|
-
},
|
|
331
|
-
});
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
private _createPlanningTool(options: SessionRunOptions) {
|
|
335
|
-
return defineTool('system', {
|
|
336
|
-
name: 'create_plan',
|
|
337
|
-
description:
|
|
338
|
-
'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: S.Struct({
|
|
340
|
-
goal: S.String.annotations({ description: 'The goal that the plan will achieve.' }),
|
|
341
|
-
steps: S.Array(
|
|
342
|
-
S.Struct({
|
|
343
|
-
action: S.String.annotations({
|
|
344
|
-
description: 'Complete, detailed action to perform.',
|
|
345
|
-
}),
|
|
346
|
-
input: S.String.annotations({
|
|
347
|
-
description: 'The required input data for this step. First step must not require any data.',
|
|
348
|
-
}),
|
|
349
|
-
output: S.String.annotations({
|
|
350
|
-
description: 'The output data from this step. This is passed to the next step as input.',
|
|
351
|
-
}),
|
|
352
|
-
requiredArtifactIds: S.Array(S.String).annotations({
|
|
353
|
-
description: 'The ids of the artifacts required to perform this step.',
|
|
354
|
-
examples: [['artifact:dxos.org/example/Test']],
|
|
355
|
-
}),
|
|
356
|
-
}),
|
|
357
|
-
).annotations({ description: 'Steps' }),
|
|
358
|
-
}),
|
|
359
|
-
execute: async ({ goal, steps }) => {
|
|
360
|
-
const missingArtifactIds = steps
|
|
361
|
-
.flatMap((step) => step.requiredArtifactIds)
|
|
362
|
-
.filter((artifactId) => !options.artifacts.some((artifact) => artifact.id === artifactId));
|
|
363
|
-
if (missingArtifactIds.length > 0) {
|
|
364
|
-
return ToolResult.Error(`One or more artifact ids are invalid: ${missingArtifactIds.join(', ')}`);
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
const stepResults: any[] = [];
|
|
368
|
-
for (const step of steps) {
|
|
369
|
-
log('executing step', { action: step.action });
|
|
370
|
-
const session = new AISession({ operationModel: 'importing' });
|
|
371
|
-
session.message.on((ev) => this.message.emit(ev));
|
|
372
|
-
session.block.on((ev) => this.block.emit(ev));
|
|
373
|
-
session.update.on((ev) => this.update.emit(ev));
|
|
374
|
-
session.streamEvent.on((ev) => this.streamEvent.emit(ev));
|
|
375
|
-
session.userMessage.on((ev) => this.userMessage.emit(ev));
|
|
376
|
-
|
|
377
|
-
const messages = await session.run({
|
|
378
|
-
client: options.client,
|
|
379
|
-
artifacts: options.artifacts,
|
|
380
|
-
tools: options.tools,
|
|
381
|
-
history: options.history,
|
|
382
|
-
extensions: options.extensions,
|
|
383
|
-
generationOptions: options.generationOptions,
|
|
384
|
-
requiredArtifactIds: step.requiredArtifactIds.slice(),
|
|
385
|
-
artifactDiffResolver: options.artifactDiffResolver,
|
|
386
|
-
prompt: `
|
|
387
|
-
You are an agent that executes the subtask in a plan.
|
|
388
|
-
|
|
389
|
-
While you know the plan's goal only focus on your subtask
|
|
390
|
-
Use the data from the previous step to complete your task.
|
|
391
|
-
Return all of the data from "output" in your last message.
|
|
392
|
-
|
|
393
|
-
Current step (that you need to complete):
|
|
394
|
-
${JSON.stringify(
|
|
395
|
-
{
|
|
396
|
-
goal,
|
|
397
|
-
input: step.input,
|
|
398
|
-
output: step.output,
|
|
399
|
-
yourTask: step.action,
|
|
400
|
-
},
|
|
401
|
-
null,
|
|
402
|
-
2,
|
|
403
|
-
)}
|
|
404
|
-
|
|
405
|
-
Previous step results:
|
|
406
|
-
${JSON.stringify(stepResults.at(-1))}.
|
|
407
|
-
`,
|
|
408
|
-
});
|
|
409
|
-
const result = messages.at(-1);
|
|
410
|
-
stepResults.push(result);
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
return ToolResult.Success({ stepResults });
|
|
414
|
-
},
|
|
415
|
-
});
|
|
416
|
-
}
|
|
417
|
-
|
|
418
332
|
abort() {
|
|
419
333
|
this._stream?.abort();
|
|
420
334
|
}
|
|
@@ -437,37 +351,19 @@ const createBaseInstructions = ({
|
|
|
437
351
|
Decision-making:
|
|
438
352
|
|
|
439
353
|
- Analyze the structure and type of the content in the user's message.
|
|
440
|
-
|
|
441
|
-
|
|
354
|
+
- Can you complete the task using the available artifacts?
|
|
355
|
+
- If you can't complete the task using the available artifacts, query the list of available artifacts using the appropriate tool.
|
|
356
|
+
- Identify which artifacts are relevant to the user's request.
|
|
442
357
|
${
|
|
443
358
|
operationModel === 'planning'
|
|
444
359
|
? `
|
|
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.
|
|
448
360
|
- 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.
|
|
449
361
|
`
|
|
450
|
-
:
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
- Can you complete the task using the available artifacts?
|
|
456
|
-
- If you can't complete the task using the available artifacts, query the list of available artifacts using the appropriate tool.
|
|
457
|
-
- Identify which artifacts are relevant to the user's request.
|
|
458
|
-
- Are the required artifacts already available?
|
|
459
|
-
- If not, select which artifact(s) will be the most relevant and require them using the require_artifacts tool.
|
|
460
|
-
- The require'd artifact tools will be available for use after require.
|
|
461
|
-
`
|
|
462
|
-
: ''
|
|
463
|
-
}
|
|
464
|
-
${
|
|
465
|
-
operationModel === 'configured'
|
|
466
|
-
? `
|
|
467
|
-
- Select the most relevant artifact(s) to complete the task.
|
|
468
|
-
- Call the appropriate tool to use the artifact(s).
|
|
469
|
-
`
|
|
470
|
-
: ''
|
|
362
|
+
: `
|
|
363
|
+
- Are the required artifacts already available?
|
|
364
|
+
- If not, select which artifact(s) will be the most relevant and require them using the require_artifacts tool.
|
|
365
|
+
- The require'd artifact tools will be available for use after require.
|
|
366
|
+
`
|
|
471
367
|
}
|
|
472
368
|
|
|
473
369
|
${availableArtifacts.length > 0 ? `Artifacts already in context: ${availableArtifacts.join('\n')}` : ''}
|
|
@@ -477,35 +373,3 @@ export class AIServiecOverloadedError extends S.TaggedError<AIServiecOverloadedE
|
|
|
477
373
|
'AIServiecOverloadedError',
|
|
478
374
|
{},
|
|
479
375
|
) {}
|
|
480
|
-
|
|
481
|
-
const gatherObjectVersions = (messages: Message[]): Map<ObjectId, ObjectVersion> => {
|
|
482
|
-
const artifactIds = new Map<ObjectId, ObjectVersion>();
|
|
483
|
-
for (const message of messages) {
|
|
484
|
-
for (const block of message.content) {
|
|
485
|
-
if (block.type === 'json' && block.disposition === VersionPin.DISPOSITION) {
|
|
486
|
-
const pin = VersionPin.pipe(S.decodeOption)(JSON.parse(block.json)).pipe(Option.getOrUndefined);
|
|
487
|
-
if (!pin) {
|
|
488
|
-
continue;
|
|
489
|
-
}
|
|
490
|
-
artifactIds.set(pin.objectId, pin.version);
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
return artifactIds;
|
|
496
|
-
};
|
|
497
|
-
|
|
498
|
-
const createArtifactUpdateBlock = (
|
|
499
|
-
artifactDiff: Map<ObjectId, { version: ObjectVersion; diff?: string }>,
|
|
500
|
-
): MessageContentBlock => {
|
|
501
|
-
return {
|
|
502
|
-
type: 'text',
|
|
503
|
-
disposition: 'artifact-update',
|
|
504
|
-
text: `
|
|
505
|
-
The following artifacts have been updated since the last message:
|
|
506
|
-
${Array.from(artifactDiff.entries())
|
|
507
|
-
.map(([id, { diff }]) => `<changed-artifact id="${id}">${diff ? `\n${diff}` : ''}</changed-artifact>`)
|
|
508
|
-
.join('\n')}
|
|
509
|
-
`,
|
|
510
|
-
};
|
|
511
|
-
};
|
|
@@ -14,7 +14,7 @@ import { AI_SERVICE_ENDPOINT } from './defs';
|
|
|
14
14
|
import { createLogger } from './logger';
|
|
15
15
|
import { createCypherTool, createSystemPrompt } from './query-promts';
|
|
16
16
|
import { createTestData, seedTestData } from './test-data';
|
|
17
|
-
import { Contact,
|
|
17
|
+
import { Contact, Org, Project, Task } from './test-schema';
|
|
18
18
|
import { AIServiceEdgeClient, DEFAULT_EDGE_MODEL } from '../ai-service';
|
|
19
19
|
import { runLLM } from '../conversation';
|
|
20
20
|
import { EchoDataSource } from '../cypher';
|
|
@@ -25,26 +25,27 @@ const client = new AIServiceEdgeClient({
|
|
|
25
25
|
|
|
26
26
|
test.skip('cypher query', async () => {
|
|
27
27
|
const dataSource = createTestData();
|
|
28
|
-
const schemaTypes = [
|
|
28
|
+
const schemaTypes = [Org, Project, Task, Contact];
|
|
29
29
|
|
|
30
30
|
const cypherTool = createCypherTool(dataSource);
|
|
31
31
|
|
|
32
32
|
const spaceId = SpaceId.random();
|
|
33
33
|
const threadId = ObjectId.random();
|
|
34
34
|
|
|
35
|
+
await client.appendMessages([
|
|
36
|
+
createUserMessage(
|
|
37
|
+
spaceId,
|
|
38
|
+
threadId,
|
|
39
|
+
'Query the database and give me all employees from DXOS organization that work on Composer and what their tasks are.',
|
|
40
|
+
),
|
|
41
|
+
]);
|
|
42
|
+
|
|
35
43
|
const result = await runLLM({
|
|
36
44
|
model: DEFAULT_EDGE_MODEL,
|
|
37
45
|
tools: [cypherTool],
|
|
38
46
|
spaceId,
|
|
39
47
|
threadId,
|
|
40
48
|
system: createSystemPrompt(schemaTypes),
|
|
41
|
-
history: [
|
|
42
|
-
createUserMessage(
|
|
43
|
-
spaceId,
|
|
44
|
-
threadId,
|
|
45
|
-
'Query the database and give me all employees from DXOS organization that work on Composer and what their tasks are.',
|
|
46
|
-
),
|
|
47
|
-
],
|
|
48
49
|
client,
|
|
49
50
|
logger: createLogger({ stream: false }),
|
|
50
51
|
});
|
|
@@ -56,7 +57,7 @@ test.skip('query ECHO', async () => {
|
|
|
56
57
|
await using builder = await new EchoTestBuilder().open();
|
|
57
58
|
const { db, graph } = await builder.createDatabase();
|
|
58
59
|
|
|
59
|
-
const schemaTypes = [
|
|
60
|
+
const schemaTypes = [Org, Project, Task, Contact];
|
|
60
61
|
graph.schemaRegistry.addSchema(schemaTypes);
|
|
61
62
|
|
|
62
63
|
seedTestData(db);
|
|
@@ -68,6 +69,14 @@ test.skip('query ECHO', async () => {
|
|
|
68
69
|
const spaceId = SpaceId.random();
|
|
69
70
|
const threadId = ObjectId.random();
|
|
70
71
|
|
|
72
|
+
await client.appendMessages([
|
|
73
|
+
createUserMessage(
|
|
74
|
+
spaceId,
|
|
75
|
+
threadId,
|
|
76
|
+
'Query the database and give me all employees from DXOS organization that work on Composer and what their tasks are.',
|
|
77
|
+
),
|
|
78
|
+
]);
|
|
79
|
+
|
|
71
80
|
const result = await runLLM({
|
|
72
81
|
model: DEFAULT_EDGE_MODEL,
|
|
73
82
|
tools: [cypherTool],
|
|
@@ -75,13 +84,6 @@ test.skip('query ECHO', async () => {
|
|
|
75
84
|
threadId,
|
|
76
85
|
system: createSystemPrompt(schemaTypes),
|
|
77
86
|
client,
|
|
78
|
-
history: [
|
|
79
|
-
createUserMessage(
|
|
80
|
-
spaceId,
|
|
81
|
-
threadId,
|
|
82
|
-
'Query the database and give me all employees from DXOS organization that work on Composer and what their tasks are.',
|
|
83
|
-
),
|
|
84
|
-
],
|
|
85
87
|
logger: createLogger({ stream: false }),
|
|
86
88
|
});
|
|
87
89
|
|