@dxos/assistant 0.8.2-main.fbd8ed0 → 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-B4HAWUFW.mjs → chunk-PND5VR6D.mjs} +286 -757
- package/dist/lib/browser/chunk-PND5VR6D.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +324 -7
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +102 -139
- package/dist/lib/browser/testing/index.mjs.map +4 -4
- package/dist/lib/node/{chunk-NOB7CIIC.cjs → chunk-IBOBRBDF.cjs} +303 -768
- package/dist/lib/node/chunk-IBOBRBDF.cjs.map +7 -0
- package/dist/lib/node/index.cjs +353 -36
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +93 -128
- package/dist/lib/node/testing/index.cjs.map +4 -4
- package/dist/lib/node-esm/{chunk-SAWYU3V5.mjs → chunk-DCQJOLSR.mjs} +286 -757
- package/dist/lib/node-esm/chunk-DCQJOLSR.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +324 -7
- 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 +102 -139
- package/dist/lib/node-esm/testing/index.mjs.map +4 -4
- 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/stream.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/testing/test-stream.d.ts.map +1 -1
- package/dist/types/src/ai-service/types.d.ts +101 -144
- package/dist/types/src/ai-service/types.d.ts.map +1 -1
- package/dist/types/src/ai-service/util/sse.d.ts.map +1 -1
- package/dist/types/src/{experimental/conversation → conversation}/conversation.d.ts +21 -2
- package/dist/types/src/conversation/conversation.d.ts.map +1 -0
- package/dist/types/src/conversation/conversation.test.d.ts.map +1 -0
- package/dist/types/src/conversation/index.d.ts.map +1 -0
- package/dist/types/src/{experimental/conversation → conversation}/types.d.ts +12 -11
- package/dist/types/src/conversation/types.d.ts.map +1 -0
- package/dist/types/src/cypher/ast.d.ts.map +1 -0
- package/dist/types/src/cypher/combinators.d.ts.map +1 -0
- package/dist/types/src/cypher/combinators.test.d.ts.map +1 -0
- package/dist/types/src/cypher/echo-data-source.d.ts.map +1 -0
- package/dist/types/src/cypher/execution-plan.d.ts.map +1 -0
- package/dist/types/src/cypher/execution-plan.test.d.ts.map +1 -0
- package/dist/types/src/cypher/index.d.ts.map +1 -0
- package/dist/types/src/cypher/parjs.d.ts.map +1 -0
- package/dist/types/src/cypher/parser.d.ts.map +1 -0
- package/dist/types/src/cypher/query-executor.d.ts.map +1 -0
- package/dist/types/src/cypher/query-executor.test.d.ts.map +1 -0
- package/dist/types/src/cypher/schema.d.ts.map +1 -0
- package/dist/types/src/cypher/schema.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +2 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/session/index.d.ts +0 -2
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/session.d.ts +13 -33
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/testing/index.d.ts +0 -1
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/logger.d.ts +1 -1
- package/dist/types/src/testing/logger.d.ts.map +1 -1
- package/dist/types/src/testing/ollama.d.ts.map +1 -1
- package/dist/types/src/testing/query-promts.d.ts +4 -4
- package/dist/types/src/testing/query-promts.d.ts.map +1 -1
- package/dist/types/src/testing/test-data.d.ts +5 -5
- package/dist/types/src/testing/test-data.d.ts.map +1 -1
- package/dist/types/src/testing/test-schema.d.ts +23 -25
- package/dist/types/src/testing/test-schema.d.ts.map +1 -1
- package/dist/types/src/util/trim.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +14 -20
- package/src/ai-service/client.test.ts +73 -55
- package/src/ai-service/edge-client.ts +20 -13
- package/src/ai-service/ollama-client.ts +10 -17
- 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 +15 -24
- package/src/bin/chat.ts +4 -4
- package/src/{experimental/conversation → conversation}/conversation.test.ts +5 -5
- package/src/{experimental/conversation → conversation}/conversation.ts +119 -4
- package/src/{experimental/conversation → conversation}/types.ts +14 -11
- package/src/{experimental/cypher → cypher}/combinators.test.ts +4 -4
- package/src/{experimental/cypher → cypher}/echo-data-source.ts +11 -11
- package/src/{experimental/cypher → cypher}/execution-plan.test.ts +1 -1
- package/src/{experimental/cypher → cypher}/query-executor.test.ts +2 -2
- package/src/{experimental/cypher → cypher}/schema.test.ts +3 -3
- package/src/{experimental/cypher → cypher}/schema.ts +1 -1
- package/src/index.ts +2 -3
- package/src/session/index.ts +0 -2
- package/src/session/session.test.ts +63 -35
- package/src/session/session.ts +151 -297
- package/src/testing/artifacts.test.ts +22 -22
- package/src/testing/index.ts +0 -1
- package/src/testing/logger.ts +1 -1
- package/src/testing/query-promts.ts +6 -6
- package/src/testing/query.test.ts +21 -18
- package/src/testing/test-data.ts +72 -79
- package/src/testing/test-schema.ts +29 -28
- package/dist/lib/browser/chunk-B4HAWUFW.mjs.map +0 -7
- package/dist/lib/node/chunk-NOB7CIIC.cjs.map +0 -7
- package/dist/lib/node-esm/chunk-SAWYU3V5.mjs.map +0 -7
- package/dist/types/src/experimental/conversation/conversation.d.ts.map +0 -1
- package/dist/types/src/experimental/conversation/conversation.test.d.ts.map +0 -1
- package/dist/types/src/experimental/conversation/index.d.ts.map +0 -1
- package/dist/types/src/experimental/conversation/types.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/ast.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/combinators.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/combinators.test.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/echo-data-source.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/execution-plan.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/execution-plan.test.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/index.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/parjs.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/parser.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/query-executor.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/query-executor.test.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/schema.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/schema.test.d.ts.map +0 -1
- package/dist/types/src/experimental/index.d.ts +0 -3
- package/dist/types/src/experimental/index.d.ts.map +0 -1
- package/dist/types/src/experimental/ner/ner.test.d.ts +0 -2
- package/dist/types/src/experimental/ner/ner.test.d.ts.map +0 -1
- package/dist/types/src/experimental/research/exa.d.ts +0 -6
- package/dist/types/src/experimental/research/exa.d.ts.map +0 -1
- package/dist/types/src/experimental/research/research.test.d.ts +0 -2
- package/dist/types/src/experimental/research/research.test.d.ts.map +0 -1
- package/dist/types/src/session/tools.d.ts +0 -18
- package/dist/types/src/session/tools.d.ts.map +0 -1
- package/dist/types/src/session/version-pin.d.ts +0 -22
- 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/dist/types/src/testing/printer.d.ts +0 -6
- package/dist/types/src/testing/printer.d.ts.map +0 -1
- package/src/experimental/index.ts +0 -6
- package/src/experimental/ner/ner.test.ts +0 -163
- package/src/experimental/research/exa.ts +0 -38
- package/src/experimental/research/instructions.tpl +0 -64
- package/src/experimental/research/research.test.ts +0 -263
- package/src/session/tools.ts +0 -117
- package/src/session/version-pin.test.ts +0 -26
- package/src/session/version-pin.ts +0 -49
- package/src/testing/printer.ts +0 -41
- package/src/typedefs.d.ts +0 -8
- /package/dist/types/src/{experimental/conversation → conversation}/conversation.test.d.ts +0 -0
- /package/dist/types/src/{experimental/conversation → conversation}/index.d.ts +0 -0
- /package/dist/types/src/{experimental/cypher → cypher}/ast.d.ts +0 -0
- /package/dist/types/src/{experimental/cypher → cypher}/combinators.d.ts +0 -0
- /package/dist/types/src/{experimental/cypher → cypher}/combinators.test.d.ts +0 -0
- /package/dist/types/src/{experimental/cypher → cypher}/echo-data-source.d.ts +0 -0
- /package/dist/types/src/{experimental/cypher → cypher}/execution-plan.d.ts +0 -0
- /package/dist/types/src/{experimental/cypher → cypher}/execution-plan.test.d.ts +0 -0
- /package/dist/types/src/{experimental/cypher → cypher}/index.d.ts +0 -0
- /package/dist/types/src/{experimental/cypher → cypher}/parjs.d.ts +0 -0
- /package/dist/types/src/{experimental/cypher → cypher}/parser.d.ts +0 -0
- /package/dist/types/src/{experimental/cypher → cypher}/query-executor.d.ts +0 -0
- /package/dist/types/src/{experimental/cypher → cypher}/query-executor.test.d.ts +0 -0
- /package/dist/types/src/{experimental/cypher → cypher}/schema.d.ts +0 -0
- /package/dist/types/src/{experimental/cypher → cypher}/schema.test.d.ts +0 -0
- /package/src/{experimental/conversation → conversation}/index.ts +0 -0
- /package/src/{experimental/cypher → cypher}/ast.ts +0 -0
- /package/src/{experimental/cypher → cypher}/combinators.ts +0 -0
- /package/src/{experimental/cypher → cypher}/execution-plan.ts +0 -0
- /package/src/{experimental/cypher → cypher}/index.ts +0 -0
- /package/src/{experimental/cypher → cypher}/parjs-shims.d.ts +0 -0
- /package/src/{experimental/cypher → cypher}/parjs.ts +0 -0
- /package/src/{experimental/cypher → cypher}/parser.ts +0 -0
- /package/src/{experimental/cypher → cypher}/query-executor.ts +0 -0
package/src/session/session.ts
CHANGED
|
@@ -2,27 +2,16 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
defineTool,
|
|
9
|
-
Message,
|
|
10
|
-
structuredOutputParser,
|
|
11
|
-
ToolResult,
|
|
12
|
-
type ArtifactDefinition,
|
|
13
|
-
type MessageContentBlock,
|
|
14
|
-
type Tool,
|
|
15
|
-
} from '@dxos/artifact';
|
|
5
|
+
import { Schema as S } from 'effect';
|
|
6
|
+
|
|
7
|
+
import { defineTool, Message, ToolResult, type ArtifactDefinition, type Tool } from '@dxos/artifact';
|
|
16
8
|
import { Event, synchronized } from '@dxos/async';
|
|
17
|
-
import {
|
|
18
|
-
import { create, type ObjectId } from '@dxos/echo-schema';
|
|
9
|
+
import { createStatic } from '@dxos/echo-schema';
|
|
19
10
|
import { invariant } from '@dxos/invariant';
|
|
20
11
|
import { log } from '@dxos/log';
|
|
21
12
|
|
|
22
|
-
import { isToolUse, runTools } from './tools';
|
|
23
|
-
import { VersionPin } from './version-pin';
|
|
24
13
|
import { MixedStreamParser, type AIServiceClient, type GenerateRequest, type GenerationStream } from '../ai-service';
|
|
25
|
-
|
|
14
|
+
import { isToolUse, runTools } from '../conversation';
|
|
26
15
|
/**
|
|
27
16
|
* Contains message history, tools, current context.
|
|
28
17
|
* Current context means the state of the app, time of day, and other contextual information.
|
|
@@ -34,22 +23,6 @@ import { MixedStreamParser, type AIServiceClient, type GenerateRequest, type Gen
|
|
|
34
23
|
* Could be personal or shared.
|
|
35
24
|
*/
|
|
36
25
|
|
|
37
|
-
/**
|
|
38
|
-
* Resolves artifact ids to their versions.
|
|
39
|
-
* Used to give the model a sense of the changes to the artifacts made by users during the conversation.
|
|
40
|
-
* The artifacts versions are pinned in the history, and whenever the artifact changes in-between assistant's steps,
|
|
41
|
-
* a diff is inserted into the conversation.
|
|
42
|
-
*/
|
|
43
|
-
export type ArtifactDiffResolver = (artifacts: { id: ObjectId; lastVersion: ObjectVersion }[]) => Promise<
|
|
44
|
-
Map<
|
|
45
|
-
ObjectId,
|
|
46
|
-
{
|
|
47
|
-
version: ObjectVersion;
|
|
48
|
-
diff?: string;
|
|
49
|
-
}
|
|
50
|
-
>
|
|
51
|
-
>;
|
|
52
|
-
|
|
53
26
|
export type SessionRunOptions = {
|
|
54
27
|
client: AIServiceClient;
|
|
55
28
|
|
|
@@ -74,22 +47,15 @@ export type SessionRunOptions = {
|
|
|
74
47
|
* Pre-require artifacts.
|
|
75
48
|
*/
|
|
76
49
|
requiredArtifactIds?: string[];
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* @see ArtifactDiffResolver
|
|
80
|
-
*/
|
|
81
|
-
artifactDiffResolver?: ArtifactDiffResolver;
|
|
82
50
|
};
|
|
83
51
|
|
|
84
|
-
type OperationModel = 'planning' | '
|
|
52
|
+
type OperationModel = 'planning' | 'immediate';
|
|
85
53
|
|
|
86
54
|
export type AiSessionOptions = {
|
|
87
55
|
/**
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
* - `importing`: The model can query registry and only pull in the artifacts that are required to complete the task.
|
|
92
|
-
* - `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.
|
|
93
59
|
*/
|
|
94
60
|
operationModel: OperationModel;
|
|
95
61
|
};
|
|
@@ -138,30 +104,147 @@ export class AISession {
|
|
|
138
104
|
|
|
139
105
|
@synchronized
|
|
140
106
|
async run(options: SessionRunOptions): Promise<Message[]> {
|
|
141
|
-
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
|
+
];
|
|
142
123
|
switch (this._options.operationModel) {
|
|
143
124
|
case 'planning':
|
|
144
|
-
systemTools.push(this._createQueryArtifactsTool(options.artifacts), this._createPlanningTool(options));
|
|
145
|
-
break;
|
|
146
|
-
case 'importing':
|
|
147
125
|
systemTools.push(
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
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
|
+
},
|
|
153
205
|
}),
|
|
154
206
|
);
|
|
155
207
|
break;
|
|
156
|
-
case '
|
|
157
|
-
|
|
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
|
+
);
|
|
158
238
|
break;
|
|
159
|
-
default:
|
|
160
|
-
throw new TypeError(`Invalid operation model: ${this._options.operationModel}`);
|
|
161
239
|
}
|
|
162
240
|
|
|
163
241
|
this._history = [...options.history];
|
|
164
|
-
this._pending = [
|
|
242
|
+
this._pending = [
|
|
243
|
+
createStatic(Message, {
|
|
244
|
+
role: 'user',
|
|
245
|
+
content: [{ type: 'text', text: options.prompt }],
|
|
246
|
+
}),
|
|
247
|
+
];
|
|
165
248
|
this.userMessage.emit(this._pending.at(-1)!);
|
|
166
249
|
this._stream = undefined;
|
|
167
250
|
let error: Error | undefined;
|
|
@@ -185,7 +268,7 @@ export class AISession {
|
|
|
185
268
|
});
|
|
186
269
|
|
|
187
270
|
// Open request stream.
|
|
188
|
-
this._stream = await options.client.
|
|
271
|
+
this._stream = await options.client.exec({
|
|
189
272
|
...(options.generationOptions ?? {}),
|
|
190
273
|
// TODO(burdon): Rename messages or separate history/message.
|
|
191
274
|
history: [...this._history, ...this._pending],
|
|
@@ -231,7 +314,7 @@ export class AISession {
|
|
|
231
314
|
} catch (err) {
|
|
232
315
|
log.catch(err);
|
|
233
316
|
if (err instanceof Error && err.message.includes('Overloaded')) {
|
|
234
|
-
error = new AIServiecOverloadedError('AI service overloaded', { cause: err }
|
|
317
|
+
error = new AIServiecOverloadedError('AI service overloaded', { cause: err });
|
|
235
318
|
} else {
|
|
236
319
|
error = new Error('AI service error', { cause: err });
|
|
237
320
|
}
|
|
@@ -246,185 +329,6 @@ export class AISession {
|
|
|
246
329
|
return this._pending;
|
|
247
330
|
}
|
|
248
331
|
|
|
249
|
-
async runStructured<S extends Schema.Schema.AnyNoContext>(schema: S, options: SessionRunOptions) {
|
|
250
|
-
const parser = structuredOutputParser(schema);
|
|
251
|
-
const result = await this.run({
|
|
252
|
-
...options,
|
|
253
|
-
tools: [...options.tools, parser.tool],
|
|
254
|
-
});
|
|
255
|
-
return parser.getResult(result);
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
private async _formatUserPrompt(
|
|
259
|
-
artifactDiffResolver: ArtifactDiffResolver | undefined,
|
|
260
|
-
prompt: string,
|
|
261
|
-
history: Message[],
|
|
262
|
-
) {
|
|
263
|
-
const prelude: MessageContentBlock[] = [];
|
|
264
|
-
|
|
265
|
-
if (artifactDiffResolver) {
|
|
266
|
-
const versions = gatherObjectVersions(history);
|
|
267
|
-
|
|
268
|
-
const artifactDiff = await artifactDiffResolver(
|
|
269
|
-
Array.from(versions.entries()).map(([id, version]) => ({ id, lastVersion: version })),
|
|
270
|
-
);
|
|
271
|
-
|
|
272
|
-
log.info('vision', {
|
|
273
|
-
artifactDiff,
|
|
274
|
-
versions,
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
for (const [id, { version }] of [...artifactDiff.entries()]) {
|
|
278
|
-
if (ObjectVersion.equals(version, versions.get(id)!)) {
|
|
279
|
-
artifactDiff.delete(id);
|
|
280
|
-
continue;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
prelude.push(VersionPin.createBlock(VersionPin.make({ objectId: id, version })));
|
|
284
|
-
}
|
|
285
|
-
if (artifactDiff.size > 0) {
|
|
286
|
-
prelude.push(createArtifactUpdateBlock(artifactDiff));
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
return create(Message, {
|
|
291
|
-
role: 'user',
|
|
292
|
-
content: [...prelude, { type: 'text', text: prompt }],
|
|
293
|
-
});
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
private _createQueryArtifactsTool(artifacts: ArtifactDefinition[]) {
|
|
297
|
-
return defineTool('system', {
|
|
298
|
-
name: 'query_artifact_definitions',
|
|
299
|
-
description: 'Query the available artifact definitions',
|
|
300
|
-
schema: Schema.Struct({}),
|
|
301
|
-
execute: async () => {
|
|
302
|
-
return ToolResult.Success({
|
|
303
|
-
artifactDefinitions: artifacts.map((artifact) => ({
|
|
304
|
-
id: artifact.id,
|
|
305
|
-
name: artifact.name,
|
|
306
|
-
description: artifact.description,
|
|
307
|
-
})),
|
|
308
|
-
});
|
|
309
|
-
},
|
|
310
|
-
});
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
private _createRequireTool(
|
|
314
|
-
artifacts: ArtifactDefinition[],
|
|
315
|
-
onRequire: (artifactDefinitionIds: readonly string[]) => void,
|
|
316
|
-
) {
|
|
317
|
-
return defineTool('system', {
|
|
318
|
-
name: 'require_artifact_definitions',
|
|
319
|
-
description:
|
|
320
|
-
'Require the use of specific artifact definitions. This will allow the model to interact with artifact definitions and use their tools.',
|
|
321
|
-
schema: Schema.Struct({
|
|
322
|
-
artifactDefinitionIds: Schema.Array(Schema.String).annotations({
|
|
323
|
-
description: 'The ids of the artifact definitions to require',
|
|
324
|
-
examples: [['artifact:dxos.org/example/Test']],
|
|
325
|
-
}),
|
|
326
|
-
}),
|
|
327
|
-
execute: async ({ artifactDefinitionIds }) => {
|
|
328
|
-
const missingArtifactDefinitionIds = artifactDefinitionIds.filter(
|
|
329
|
-
(artifactId) => !artifacts.some((artifact) => artifact.id === artifactId),
|
|
330
|
-
);
|
|
331
|
-
if (missingArtifactDefinitionIds.length > 0) {
|
|
332
|
-
return ToolResult.Error(
|
|
333
|
-
`One or more artifact definition ids are invalid: ${missingArtifactDefinitionIds.join(', ')}`,
|
|
334
|
-
);
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
onRequire(artifactDefinitionIds);
|
|
338
|
-
|
|
339
|
-
return ToolResult.Success({});
|
|
340
|
-
},
|
|
341
|
-
});
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
private _createPlanningTool(options: SessionRunOptions) {
|
|
345
|
-
return defineTool('system', {
|
|
346
|
-
name: 'create_plan',
|
|
347
|
-
description:
|
|
348
|
-
'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.',
|
|
349
|
-
schema: Schema.Struct({
|
|
350
|
-
goal: Schema.String.annotations({ description: 'The goal that the plan will achieve.' }),
|
|
351
|
-
steps: Schema.Array(
|
|
352
|
-
Schema.Struct({
|
|
353
|
-
action: Schema.String.annotations({
|
|
354
|
-
description: 'Complete, detailed action to perform.',
|
|
355
|
-
}),
|
|
356
|
-
input: Schema.String.annotations({
|
|
357
|
-
description: 'The required input data for this step. First step must not require any data.',
|
|
358
|
-
}),
|
|
359
|
-
output: Schema.String.annotations({
|
|
360
|
-
description: 'The output data from this step. This is passed to the next step as input.',
|
|
361
|
-
}),
|
|
362
|
-
requiredArtifactIds: Schema.Array(Schema.String).annotations({
|
|
363
|
-
description: 'The ids of the artifacts required to perform this step.',
|
|
364
|
-
examples: [['artifact:dxos.org/example/Test']],
|
|
365
|
-
}),
|
|
366
|
-
}),
|
|
367
|
-
).annotations({ description: 'Steps' }),
|
|
368
|
-
}),
|
|
369
|
-
execute: async ({ goal, steps }) => {
|
|
370
|
-
const missingArtifactIds = steps
|
|
371
|
-
.flatMap((step) => step.requiredArtifactIds)
|
|
372
|
-
.filter((artifactId) => !options.artifacts.some((artifact) => artifact.id === artifactId));
|
|
373
|
-
if (missingArtifactIds.length > 0) {
|
|
374
|
-
return ToolResult.Error(`One or more artifact ids are invalid: ${missingArtifactIds.join(', ')}`);
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
const stepResults: any[] = [];
|
|
378
|
-
for (const step of steps) {
|
|
379
|
-
log('executing step', { action: step.action });
|
|
380
|
-
const session = new AISession({ operationModel: 'importing' });
|
|
381
|
-
session.message.on((ev) => this.message.emit(ev));
|
|
382
|
-
session.block.on((ev) => this.block.emit(ev));
|
|
383
|
-
session.update.on((ev) => this.update.emit(ev));
|
|
384
|
-
session.streamEvent.on((ev) => this.streamEvent.emit(ev));
|
|
385
|
-
session.userMessage.on((ev) => this.userMessage.emit(ev));
|
|
386
|
-
|
|
387
|
-
const messages = await session.run({
|
|
388
|
-
client: options.client,
|
|
389
|
-
artifacts: options.artifacts,
|
|
390
|
-
tools: options.tools,
|
|
391
|
-
history: options.history,
|
|
392
|
-
extensions: options.extensions,
|
|
393
|
-
generationOptions: options.generationOptions,
|
|
394
|
-
requiredArtifactIds: step.requiredArtifactIds.slice(),
|
|
395
|
-
artifactDiffResolver: options.artifactDiffResolver,
|
|
396
|
-
prompt: `
|
|
397
|
-
You are an agent that executes the subtask in a plan.
|
|
398
|
-
|
|
399
|
-
While you know the plan's goal only focus on your subtask
|
|
400
|
-
Use the data from the previous step to complete your task.
|
|
401
|
-
Return all of the data from "output" in your last message.
|
|
402
|
-
|
|
403
|
-
Current step (that you need to complete):
|
|
404
|
-
${JSON.stringify(
|
|
405
|
-
{
|
|
406
|
-
goal,
|
|
407
|
-
input: step.input,
|
|
408
|
-
output: step.output,
|
|
409
|
-
yourTask: step.action,
|
|
410
|
-
},
|
|
411
|
-
null,
|
|
412
|
-
2,
|
|
413
|
-
)}
|
|
414
|
-
|
|
415
|
-
Previous step results:
|
|
416
|
-
${JSON.stringify(stepResults.at(-1))}.
|
|
417
|
-
`,
|
|
418
|
-
});
|
|
419
|
-
const result = messages.at(-1);
|
|
420
|
-
stepResults.push(result);
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
return ToolResult.Success({ stepResults });
|
|
424
|
-
},
|
|
425
|
-
});
|
|
426
|
-
}
|
|
427
|
-
|
|
428
332
|
abort() {
|
|
429
333
|
this._stream?.abort();
|
|
430
334
|
}
|
|
@@ -447,75 +351,25 @@ const createBaseInstructions = ({
|
|
|
447
351
|
Decision-making:
|
|
448
352
|
|
|
449
353
|
- Analyze the structure and type of the content in the user's message.
|
|
450
|
-
|
|
451
|
-
|
|
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.
|
|
452
357
|
${
|
|
453
358
|
operationModel === 'planning'
|
|
454
359
|
? `
|
|
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
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.
|
|
459
361
|
`
|
|
460
|
-
:
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
- Can you complete the task using the available artifacts?
|
|
466
|
-
- If you can't complete the task using the available artifacts, query the list of available artifacts using the appropriate tool.
|
|
467
|
-
- Identify which artifacts are relevant to the user's request.
|
|
468
|
-
- Are the required artifacts already available?
|
|
469
|
-
- If not, select which artifact(s) will be the most relevant and require them using the require_artifacts tool.
|
|
470
|
-
- The require'd artifact tools will be available for use after require.
|
|
471
|
-
`
|
|
472
|
-
: ''
|
|
473
|
-
}
|
|
474
|
-
${
|
|
475
|
-
operationModel === 'configured'
|
|
476
|
-
? `
|
|
477
|
-
- Select the most relevant artifact(s) to complete the task.
|
|
478
|
-
- Call the appropriate tool to use the artifact(s).
|
|
479
|
-
`
|
|
480
|
-
: ''
|
|
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
|
+
`
|
|
481
367
|
}
|
|
482
368
|
|
|
483
369
|
${availableArtifacts.length > 0 ? `Artifacts already in context: ${availableArtifacts.join('\n')}` : ''}
|
|
484
370
|
`;
|
|
485
371
|
|
|
486
|
-
export class AIServiecOverloadedError extends
|
|
372
|
+
export class AIServiecOverloadedError extends S.TaggedError<AIServiecOverloadedError>()(
|
|
487
373
|
'AIServiecOverloadedError',
|
|
488
374
|
{},
|
|
489
375
|
) {}
|
|
490
|
-
|
|
491
|
-
const gatherObjectVersions = (messages: Message[]): Map<ObjectId, ObjectVersion> => {
|
|
492
|
-
const artifactIds = new Map<ObjectId, ObjectVersion>();
|
|
493
|
-
for (const message of messages) {
|
|
494
|
-
for (const block of message.content) {
|
|
495
|
-
if (block.type === 'json' && block.disposition === VersionPin.DISPOSITION) {
|
|
496
|
-
const pin = VersionPin.pipe(Schema.decodeOption)(JSON.parse(block.json)).pipe(Option.getOrUndefined);
|
|
497
|
-
if (!pin) {
|
|
498
|
-
continue;
|
|
499
|
-
}
|
|
500
|
-
artifactIds.set(pin.objectId, pin.version);
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
return artifactIds;
|
|
506
|
-
};
|
|
507
|
-
|
|
508
|
-
const createArtifactUpdateBlock = (
|
|
509
|
-
artifactDiff: Map<ObjectId, { version: ObjectVersion; diff?: string }>,
|
|
510
|
-
): MessageContentBlock => {
|
|
511
|
-
return {
|
|
512
|
-
type: 'text',
|
|
513
|
-
disposition: 'artifact-update',
|
|
514
|
-
text: `
|
|
515
|
-
The following artifacts have been updated since the last message:
|
|
516
|
-
${Array.from(artifactDiff.entries())
|
|
517
|
-
.map(([id, { diff }]) => `<changed-artifact id="${id}">${diff ? `\n${diff}` : ''}</changed-artifact>`)
|
|
518
|
-
.join('\n')}
|
|
519
|
-
`,
|
|
520
|
-
};
|
|
521
|
-
};
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
/*
|
|
6
|
-
import { Schema } from '@effect/schema';
|
|
6
|
+
import { Schema as S } from '@effect/schema';
|
|
7
7
|
import { describe, test } from 'vitest';
|
|
8
8
|
|
|
9
9
|
import { ObjectId, toJsonSchema } from '@dxos/echo-schema';
|
|
@@ -15,26 +15,26 @@ import { AIServiceEdgeClient, Tool } from '../ai-service';
|
|
|
15
15
|
import { defineTool, ToolResult } from '../conversation';
|
|
16
16
|
|
|
17
17
|
// TODO(dmaretskyi): Effect schema.
|
|
18
|
-
const ArtifactDef =
|
|
19
|
-
typename:
|
|
20
|
-
description:
|
|
21
|
-
schema:
|
|
22
|
-
actions:
|
|
23
|
-
}).pipe(
|
|
18
|
+
const ArtifactDef = S.Struct({
|
|
19
|
+
typename: S.String,
|
|
20
|
+
description: S.String,
|
|
21
|
+
schema: S.Any,
|
|
22
|
+
actions: S.Array(Tool),
|
|
23
|
+
}).pipe(S.mutable);
|
|
24
24
|
|
|
25
|
-
type ArtifactDef =
|
|
25
|
+
type ArtifactDef = S.Schema.Type<typeof ArtifactDef>;
|
|
26
26
|
|
|
27
|
-
const ListItem =
|
|
28
|
-
name:
|
|
27
|
+
const ListItem = S.Struct({
|
|
28
|
+
name: S.String,
|
|
29
29
|
});
|
|
30
30
|
|
|
31
|
-
const ListSchema =
|
|
31
|
+
const ListSchema = S.Struct({
|
|
32
32
|
id: ObjectId,
|
|
33
|
-
title:
|
|
34
|
-
items:
|
|
35
|
-
}).pipe(
|
|
33
|
+
title: S.String,
|
|
34
|
+
items: S.Array(ListItem).pipe(S.mutable),
|
|
35
|
+
}).pipe(S.mutable);
|
|
36
36
|
|
|
37
|
-
type ListSchema =
|
|
37
|
+
type ListSchema = S.Schema.Type<typeof ListSchema>;
|
|
38
38
|
|
|
39
39
|
const list: ArtifactDef = {
|
|
40
40
|
typename: 'example.com/type/List',
|
|
@@ -44,7 +44,7 @@ const list: ArtifactDef = {
|
|
|
44
44
|
defineTool('list', {
|
|
45
45
|
name: 'query',
|
|
46
46
|
description: 'Query all lists',
|
|
47
|
-
schema:
|
|
47
|
+
schema: S.Struct({}),
|
|
48
48
|
execute: async ({}, context) => {
|
|
49
49
|
return ToolResult.Success(LISTS[id]);
|
|
50
50
|
},
|
|
@@ -52,7 +52,7 @@ const list: ArtifactDef = {
|
|
|
52
52
|
defineTool('list', {
|
|
53
53
|
name: 'inspect',
|
|
54
54
|
description: 'Get contents of the list',
|
|
55
|
-
schema:
|
|
55
|
+
schema: S.Struct({
|
|
56
56
|
id: ObjectId.annotations({ description: 'The list to inspect' }),
|
|
57
57
|
}),
|
|
58
58
|
execute: async ({ id }, context) => {
|
|
@@ -62,9 +62,9 @@ const list: ArtifactDef = {
|
|
|
62
62
|
defineTool('list', {
|
|
63
63
|
name: 'create',
|
|
64
64
|
description: 'Add one or more items to an existing list',
|
|
65
|
-
schema:
|
|
65
|
+
schema: S.Struct({
|
|
66
66
|
id: ObjectId.annotations({ description: 'The list to add items to' }),
|
|
67
|
-
items:
|
|
67
|
+
items: S.Array(ListItem).annotations({ description: 'The items to add to the list' }),
|
|
68
68
|
}),
|
|
69
69
|
execute: async ({ id, items }, context) => {
|
|
70
70
|
LISTS[id].items.push(...items);
|
|
@@ -81,7 +81,7 @@ const artifacts = [list];
|
|
|
81
81
|
const getArtifactDefinitions = defineTool('system', {
|
|
82
82
|
name: 'getArtifactDefinitions',
|
|
83
83
|
description: 'Queries the definitions and metadata of artifacts defined in the system',
|
|
84
|
-
schema:
|
|
84
|
+
schema: S.Any,
|
|
85
85
|
execute: async (_, context) => {
|
|
86
86
|
return ToolResult.Success(
|
|
87
87
|
artifacts.map((artifact) => ({
|
|
@@ -116,8 +116,8 @@ describe('Artifacts', () => {
|
|
|
116
116
|
name: 'custodian',
|
|
117
117
|
description: 'Custodian can tell you the password if you say the magic word',
|
|
118
118
|
parameters: toJsonSchema(
|
|
119
|
-
|
|
120
|
-
magicWord:
|
|
119
|
+
S.Struct({
|
|
120
|
+
magicWord: S.String.annotations({ description: 'The magic word. Should be exactly "pretty please"' }),
|
|
121
121
|
}),
|
|
122
122
|
),
|
|
123
123
|
};
|
package/src/testing/index.ts
CHANGED
package/src/testing/logger.ts
CHANGED
|
@@ -2,23 +2,23 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { Schema } from 'effect';
|
|
5
|
+
import { Schema as S } from 'effect';
|
|
6
6
|
|
|
7
7
|
import { defineTool, ToolResult } from '@dxos/artifact';
|
|
8
8
|
import { toJsonSchema } from '@dxos/echo-schema';
|
|
9
9
|
import { log } from '@dxos/log';
|
|
10
10
|
|
|
11
|
-
import { executeQuery, formatJsonSchemaForLLM, type DataSource } from '../
|
|
11
|
+
import { executeQuery, formatJsonSchemaForLLM, type DataSource } from '../cypher';
|
|
12
12
|
import { trim } from '../util';
|
|
13
13
|
|
|
14
|
-
export const createCypherTool = (dataSource: DataSource, schemaTypes:
|
|
14
|
+
export const createCypherTool = (dataSource: DataSource, schemaTypes: S.Schema.Any[] = []) =>
|
|
15
15
|
defineTool('dxos.org/echo', {
|
|
16
16
|
name: 'query',
|
|
17
17
|
description:
|
|
18
18
|
'Query the ECHO graph database in cypher query language. Returns data from the database.' +
|
|
19
19
|
(schemaTypes.length > 0 ? `\n\n${createSystemPrompt(schemaTypes)}` : ''),
|
|
20
|
-
schema:
|
|
21
|
-
query:
|
|
20
|
+
schema: S.Struct({
|
|
21
|
+
query: S.String.annotations({
|
|
22
22
|
description: `
|
|
23
23
|
A valid cypher query string to execute.
|
|
24
24
|
Query must have one MATCH clause.
|
|
@@ -55,7 +55,7 @@ export const createCypherTool = (dataSource: DataSource, schemaTypes: Schema.Sch
|
|
|
55
55
|
},
|
|
56
56
|
});
|
|
57
57
|
|
|
58
|
-
export const createSystemPrompt = (schemaTypes:
|
|
58
|
+
export const createSystemPrompt = (schemaTypes: S.Schema.Any[]) => trim`
|
|
59
59
|
You have access to ECHO graph database. You can query the database to get data to satisfy user prompts.
|
|
60
60
|
|
|
61
61
|
Database schema is defined in pseud-cypher syntax. The schema is:
|