@dxos/assistant 0.8.2-main.f081794 → 0.8.2-main.fbd8ed0
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-B4HAWUFW.mjs} +710 -246
- package/dist/lib/browser/chunk-B4HAWUFW.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +5 -324
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +123 -86
- package/dist/lib/browser/testing/index.mjs.map +4 -4
- package/dist/lib/node/{chunk-7VH7NMIM.cjs → chunk-NOB7CIIC.cjs} +716 -259
- package/dist/lib/node/chunk-NOB7CIIC.cjs.map +7 -0
- package/dist/lib/node/index.cjs +35 -354
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/testing/index.cjs +112 -77
- package/dist/lib/node/testing/index.cjs.map +4 -4
- package/dist/lib/node-esm/{chunk-7JI4MDGL.mjs → chunk-SAWYU3V5.mjs} +710 -246
- package/dist/lib/node-esm/chunk-SAWYU3V5.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +5 -324
- 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 +123 -86
- package/dist/lib/node-esm/testing/index.mjs.map +4 -4
- package/dist/types/src/ai-service/parser.d.ts.map +1 -1
- package/dist/types/src/ai-service/stream.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 +133 -133
- 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/{conversation → experimental/conversation}/conversation.d.ts +2 -18
- package/dist/types/src/experimental/conversation/conversation.d.ts.map +1 -0
- package/dist/types/src/experimental/conversation/conversation.test.d.ts.map +1 -0
- package/dist/types/src/experimental/conversation/index.d.ts.map +1 -0
- package/dist/types/src/{conversation → experimental/conversation}/types.d.ts +9 -9
- package/dist/types/src/experimental/conversation/types.d.ts.map +1 -0
- package/dist/types/src/experimental/cypher/ast.d.ts.map +1 -0
- package/dist/types/src/experimental/cypher/combinators.d.ts.map +1 -0
- package/dist/types/src/experimental/cypher/combinators.test.d.ts.map +1 -0
- package/dist/types/src/experimental/cypher/echo-data-source.d.ts.map +1 -0
- package/dist/types/src/experimental/cypher/execution-plan.d.ts.map +1 -0
- package/dist/types/src/experimental/cypher/execution-plan.test.d.ts.map +1 -0
- package/dist/types/src/experimental/cypher/index.d.ts.map +1 -0
- package/dist/types/src/experimental/cypher/parjs.d.ts.map +1 -0
- package/dist/types/src/experimental/cypher/parser.d.ts.map +1 -0
- package/dist/types/src/experimental/cypher/query-executor.d.ts.map +1 -0
- package/dist/types/src/experimental/cypher/query-executor.test.d.ts.map +1 -0
- package/dist/types/src/experimental/cypher/schema.d.ts.map +1 -0
- package/dist/types/src/experimental/cypher/schema.test.d.ts.map +1 -0
- package/dist/types/src/experimental/index.d.ts +3 -0
- package/dist/types/src/experimental/index.d.ts.map +1 -0
- package/dist/types/src/experimental/ner/ner.test.d.ts +2 -0
- package/dist/types/src/experimental/ner/ner.test.d.ts.map +1 -0
- package/dist/types/src/experimental/research/exa.d.ts +6 -0
- package/dist/types/src/experimental/research/exa.d.ts.map +1 -0
- package/dist/types/src/experimental/research/research.test.d.ts +2 -0
- package/dist/types/src/experimental/research/research.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +1 -2
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/session/index.d.ts +2 -0
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/session.d.ts +33 -7
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/session/tools.d.ts +18 -0
- package/dist/types/src/session/tools.d.ts.map +1 -0
- package/dist/types/src/session/version-pin.d.ts +22 -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/index.d.ts +1 -0
- 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/printer.d.ts +6 -0
- package/dist/types/src/testing/printer.d.ts.map +1 -0
- 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 +25 -23
- 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 +20 -14
- package/src/ai-service/client.test.ts +5 -5
- package/src/ai-service/ollama-client.ts +8 -8
- package/src/ai-service/types.ts +14 -14
- package/src/bin/chat.ts +3 -3
- package/src/{conversation → experimental/conversation}/conversation.test.ts +5 -5
- package/src/{conversation → experimental/conversation}/conversation.ts +3 -112
- package/src/{conversation → experimental/conversation}/types.ts +10 -10
- package/src/{cypher → experimental/cypher}/combinators.test.ts +4 -4
- package/src/{cypher → experimental/cypher}/echo-data-source.ts +11 -10
- package/src/{cypher → experimental/cypher}/execution-plan.test.ts +1 -1
- package/src/{cypher → experimental/cypher}/query-executor.test.ts +2 -2
- package/src/{cypher → experimental/cypher}/schema.test.ts +3 -3
- package/src/{cypher → experimental/cypher}/schema.ts +1 -1
- package/src/experimental/index.ts +6 -0
- package/src/experimental/ner/ner.test.ts +163 -0
- package/src/experimental/research/exa.ts +38 -0
- package/src/experimental/research/instructions.tpl +64 -0
- package/src/experimental/research/research.test.ts +263 -0
- package/src/index.ts +3 -2
- package/src/session/index.ts +2 -0
- package/src/session/session.test.ts +30 -58
- package/src/session/session.ts +296 -150
- package/src/session/tools.ts +117 -0
- package/src/session/version-pin.test.ts +26 -0
- package/src/session/version-pin.ts +49 -0
- package/src/testing/artifacts.test.ts +22 -22
- package/src/testing/index.ts +1 -0
- package/src/testing/logger.ts +1 -1
- package/src/testing/printer.ts +41 -0
- package/src/testing/query-promts.ts +6 -6
- package/src/testing/query.test.ts +4 -5
- package/src/testing/test-data.ts +63 -56
- package/src/testing/test-schema.ts +28 -29
- package/src/typedefs.d.ts +8 -0
- 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/conversation/conversation.d.ts.map +0 -1
- package/dist/types/src/conversation/conversation.test.d.ts.map +0 -1
- package/dist/types/src/conversation/index.d.ts.map +0 -1
- package/dist/types/src/conversation/types.d.ts.map +0 -1
- package/dist/types/src/cypher/ast.d.ts.map +0 -1
- package/dist/types/src/cypher/combinators.d.ts.map +0 -1
- package/dist/types/src/cypher/combinators.test.d.ts.map +0 -1
- package/dist/types/src/cypher/echo-data-source.d.ts.map +0 -1
- package/dist/types/src/cypher/execution-plan.d.ts.map +0 -1
- package/dist/types/src/cypher/execution-plan.test.d.ts.map +0 -1
- package/dist/types/src/cypher/index.d.ts.map +0 -1
- package/dist/types/src/cypher/parjs.d.ts.map +0 -1
- package/dist/types/src/cypher/parser.d.ts.map +0 -1
- package/dist/types/src/cypher/query-executor.d.ts.map +0 -1
- package/dist/types/src/cypher/query-executor.test.d.ts.map +0 -1
- package/dist/types/src/cypher/schema.d.ts.map +0 -1
- package/dist/types/src/cypher/schema.test.d.ts.map +0 -1
- /package/dist/types/src/{conversation → experimental/conversation}/conversation.test.d.ts +0 -0
- /package/dist/types/src/{conversation → experimental/conversation}/index.d.ts +0 -0
- /package/dist/types/src/{cypher → experimental/cypher}/ast.d.ts +0 -0
- /package/dist/types/src/{cypher → experimental/cypher}/combinators.d.ts +0 -0
- /package/dist/types/src/{cypher → experimental/cypher}/combinators.test.d.ts +0 -0
- /package/dist/types/src/{cypher → experimental/cypher}/echo-data-source.d.ts +0 -0
- /package/dist/types/src/{cypher → experimental/cypher}/execution-plan.d.ts +0 -0
- /package/dist/types/src/{cypher → experimental/cypher}/execution-plan.test.d.ts +0 -0
- /package/dist/types/src/{cypher → experimental/cypher}/index.d.ts +0 -0
- /package/dist/types/src/{cypher → experimental/cypher}/parjs.d.ts +0 -0
- /package/dist/types/src/{cypher → experimental/cypher}/parser.d.ts +0 -0
- /package/dist/types/src/{cypher → experimental/cypher}/query-executor.d.ts +0 -0
- /package/dist/types/src/{cypher → experimental/cypher}/query-executor.test.d.ts +0 -0
- /package/dist/types/src/{cypher → experimental/cypher}/schema.d.ts +0 -0
- /package/dist/types/src/{cypher → experimental/cypher}/schema.test.d.ts +0 -0
- /package/src/{conversation → experimental/conversation}/index.ts +0 -0
- /package/src/{cypher → experimental/cypher}/ast.ts +0 -0
- /package/src/{cypher → experimental/cypher}/combinators.ts +0 -0
- /package/src/{cypher → experimental/cypher}/execution-plan.ts +0 -0
- /package/src/{cypher → experimental/cypher}/index.ts +0 -0
- /package/src/{cypher → experimental/cypher}/parjs-shims.d.ts +0 -0
- /package/src/{cypher → experimental/cypher}/parjs.ts +0 -0
- /package/src/{cypher → experimental/cypher}/parser.ts +0 -0
- /package/src/{cypher → experimental/cypher}/query-executor.ts +0 -0
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { type
|
|
6
|
-
import {
|
|
5
|
+
import { type Message, type Tool } from '@dxos/artifact';
|
|
6
|
+
import { ObjectId } from '@dxos/echo-schema';
|
|
7
7
|
import { invariant } from '@dxos/invariant';
|
|
8
8
|
import { type SpaceId } from '@dxos/keys';
|
|
9
9
|
import { log } from '@dxos/log';
|
|
10
10
|
|
|
11
|
-
import { type AIServiceClient, type GenerationStreamEvent, type LLMModel } from '
|
|
11
|
+
import { type AIServiceClient, type GenerationStreamEvent, type LLMModel } from '../../ai-service';
|
|
12
12
|
|
|
13
13
|
export type CreateLLMConversationParams = {
|
|
14
14
|
model: LLMModel;
|
|
@@ -140,112 +140,3 @@ export const runLLM = async (params: CreateLLMConversationParams) => {
|
|
|
140
140
|
history,
|
|
141
141
|
};
|
|
142
142
|
};
|
|
143
|
-
|
|
144
|
-
export const isToolUse = (message: Message, { onlyToolNames }: { onlyToolNames?: string[] } = {}) => {
|
|
145
|
-
const block = message.content.at(-1);
|
|
146
|
-
invariant(block);
|
|
147
|
-
return block.type === 'tool_use' && (!onlyToolNames || onlyToolNames.includes(block.name));
|
|
148
|
-
};
|
|
149
|
-
|
|
150
|
-
export type RunToolsOptions = {
|
|
151
|
-
message: Message;
|
|
152
|
-
tools: Tool[];
|
|
153
|
-
extensions?: ToolContextExtensions;
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
export type RunToolsResult =
|
|
157
|
-
| {
|
|
158
|
-
type: 'continue';
|
|
159
|
-
message: Message;
|
|
160
|
-
}
|
|
161
|
-
| {
|
|
162
|
-
type: 'break';
|
|
163
|
-
result: unknown;
|
|
164
|
-
};
|
|
165
|
-
|
|
166
|
-
export const runTools = async ({ message, tools, extensions }: RunToolsOptions): Promise<RunToolsResult> => {
|
|
167
|
-
const toolCalls = message.content.filter((block) => block.type === 'tool_use');
|
|
168
|
-
invariant(toolCalls.length === 1);
|
|
169
|
-
const toolCall = toolCalls[0];
|
|
170
|
-
const tool = tools.find((tool) => tool.name === toolCall.name);
|
|
171
|
-
if (!tool) {
|
|
172
|
-
const resultMessage = create(Message, {
|
|
173
|
-
role: 'user',
|
|
174
|
-
content: [
|
|
175
|
-
{
|
|
176
|
-
type: 'tool_result',
|
|
177
|
-
toolUseId: toolCall.id,
|
|
178
|
-
content: `Tool not found: ${toolCall.name}`,
|
|
179
|
-
isError: true,
|
|
180
|
-
},
|
|
181
|
-
],
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
// TODO(burdon): Retry count?
|
|
185
|
-
return {
|
|
186
|
-
type: 'continue',
|
|
187
|
-
message: resultMessage,
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
let toolResult: ToolResult;
|
|
192
|
-
try {
|
|
193
|
-
invariant(tool.execute);
|
|
194
|
-
toolResult = await tool.execute(toolCall.input, { extensions });
|
|
195
|
-
} catch (error: any) {
|
|
196
|
-
log('tool error', { error });
|
|
197
|
-
toolResult = ToolResult.Error(error.message);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
switch (toolResult.kind) {
|
|
201
|
-
case 'error': {
|
|
202
|
-
log('tool error', { message: toolResult.message });
|
|
203
|
-
const resultMessage = create(Message, {
|
|
204
|
-
role: 'user',
|
|
205
|
-
content: [
|
|
206
|
-
{
|
|
207
|
-
type: 'tool_result',
|
|
208
|
-
toolUseId: toolCall.id,
|
|
209
|
-
content: toolResult.message,
|
|
210
|
-
isError: true,
|
|
211
|
-
},
|
|
212
|
-
],
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
// TODO(burdon): Retry count?
|
|
216
|
-
return {
|
|
217
|
-
type: 'continue',
|
|
218
|
-
message: resultMessage,
|
|
219
|
-
};
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
case 'success': {
|
|
223
|
-
log('tool success', { result: toolResult.result });
|
|
224
|
-
const resultMessage = create(Message, {
|
|
225
|
-
role: 'user',
|
|
226
|
-
content: [
|
|
227
|
-
{
|
|
228
|
-
type: 'tool_result',
|
|
229
|
-
toolUseId: toolCall.id,
|
|
230
|
-
content:
|
|
231
|
-
typeof toolResult.result === 'string' ? toolResult.result : JSON.stringify(toolResult.result) ?? '',
|
|
232
|
-
},
|
|
233
|
-
...(toolResult.extractContentBlocks ?? []),
|
|
234
|
-
],
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
return {
|
|
238
|
-
type: 'continue',
|
|
239
|
-
message: resultMessage,
|
|
240
|
-
};
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
case 'break': {
|
|
244
|
-
log('tool break', { result: toolResult.result });
|
|
245
|
-
return {
|
|
246
|
-
type: 'break',
|
|
247
|
-
result: toolResult.result,
|
|
248
|
-
};
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
};
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { Schema
|
|
5
|
+
import { Schema } from 'effect';
|
|
6
6
|
|
|
7
7
|
import { type Message, type MessageContentBlock } from '@dxos/artifact';
|
|
8
8
|
import { toJsonSchema, JsonSchemaType, ObjectId } from '@dxos/echo-schema';
|
|
@@ -17,11 +17,11 @@ export const createUserMessage = (text: string): Message => ({
|
|
|
17
17
|
/**
|
|
18
18
|
* @deprecated Use {@link LLMTool} instead.
|
|
19
19
|
*/
|
|
20
|
-
export const ToolDefinition =
|
|
21
|
-
name:
|
|
22
|
-
description:
|
|
20
|
+
export const ToolDefinition = Schema.Struct({
|
|
21
|
+
name: Schema.String,
|
|
22
|
+
description: Schema.String,
|
|
23
23
|
parameters: JsonSchemaType,
|
|
24
|
-
execute:
|
|
24
|
+
execute: Schema.Any,
|
|
25
25
|
});
|
|
26
26
|
|
|
27
27
|
/**
|
|
@@ -83,14 +83,14 @@ export const ToolResult = Object.freeze({
|
|
|
83
83
|
Break: (result: unknown): ToolResult => ({ kind: 'break', result }),
|
|
84
84
|
});
|
|
85
85
|
|
|
86
|
-
export type DefineToolParams<Params extends
|
|
86
|
+
export type DefineToolParams<Params extends Schema.Schema.AnyNoContext> = {
|
|
87
87
|
name: string;
|
|
88
88
|
description: string;
|
|
89
89
|
schema: Params;
|
|
90
|
-
execute: (params:
|
|
90
|
+
execute: (params: Schema.Schema.Type<Params>, context: ToolExecutionContext) => Promise<ToolResult>;
|
|
91
91
|
};
|
|
92
92
|
|
|
93
|
-
export const defineTool = <Params extends
|
|
93
|
+
export const defineTool = <Params extends Schema.Schema.AnyNoContext>({
|
|
94
94
|
name,
|
|
95
95
|
description,
|
|
96
96
|
schema,
|
|
@@ -101,7 +101,7 @@ export const defineTool = <Params extends S.Schema.AnyNoContext>({
|
|
|
101
101
|
description,
|
|
102
102
|
parameters: toFunctionParameterSchema(schema),
|
|
103
103
|
execute: (params, context) => {
|
|
104
|
-
const sanitized =
|
|
104
|
+
const sanitized = Schema.decodeSync(schema)(params);
|
|
105
105
|
return execute(sanitized, context);
|
|
106
106
|
},
|
|
107
107
|
};
|
|
@@ -110,7 +110,7 @@ export const defineTool = <Params extends S.Schema.AnyNoContext>({
|
|
|
110
110
|
/**
|
|
111
111
|
* Adapts schems to be able to pass to AI providers.
|
|
112
112
|
*/
|
|
113
|
-
const toFunctionParameterSchema = (schema:
|
|
113
|
+
const toFunctionParameterSchema = (schema: Schema.Schema.All) => {
|
|
114
114
|
const jsonSchema = toJsonSchema(schema);
|
|
115
115
|
log('tool schema', { jsonSchema });
|
|
116
116
|
delete jsonSchema.anyOf;
|
|
@@ -77,7 +77,7 @@ describe('Cypher Combinators', () => {
|
|
|
77
77
|
test('match clause', ({ expect }) => {
|
|
78
78
|
testParser(matchClause, 'MATCH (n:Person)');
|
|
79
79
|
testParser(matchClause, 'MATCH (n:Person)-[r:KNOWS]->(m:Person)');
|
|
80
|
-
testParser(matchClause, 'MATCH (o:
|
|
80
|
+
testParser(matchClause, 'MATCH (o:Organization)-[:HAS_EMPLOYEE]->(e:Employee)');
|
|
81
81
|
});
|
|
82
82
|
|
|
83
83
|
test('where clause', ({ expect }) => {
|
|
@@ -116,15 +116,15 @@ describe('Cypher Combinators', () => {
|
|
|
116
116
|
);
|
|
117
117
|
testParser(
|
|
118
118
|
cypherQuery,
|
|
119
|
-
'MATCH (o:
|
|
119
|
+
'MATCH (o:Organization)-[:HAS_EMPLOYEE]->(e:Employee)-[:WORKS_ON]->(p:Project) WHERE o.name = "DXOS" AND p.name = "Composer" RETURN e.name, e.id, p.name, p.id',
|
|
120
120
|
);
|
|
121
121
|
testParser(
|
|
122
122
|
cypherQuery,
|
|
123
|
-
'MATCH (n:
|
|
123
|
+
'MATCH (n:Organization {name: "DXOS"})-[:HAS_EMPLOYEE]->(m:Employee)<-[:WORKS_ON]-(p:Project {name: "Composer"}) RETURN m.name AS employee',
|
|
124
124
|
);
|
|
125
125
|
testParser(
|
|
126
126
|
cypherQuery,
|
|
127
|
-
"MATCH (org:
|
|
127
|
+
"MATCH (org:Organization {name: 'DXOS'})-[:ORG_EMPLOYEES]->(c:Contact)<-[:TASK_ASSIGNEE]-(t:Task)-[:TASK_PROJECT]->(p:Project {name: 'Composer'}) RETURN c.name",
|
|
128
128
|
);
|
|
129
129
|
});
|
|
130
130
|
});
|
|
@@ -2,9 +2,11 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
+
import { type Schema } from 'effect';
|
|
6
|
+
|
|
5
7
|
import { raise } from '@dxos/debug';
|
|
6
|
-
import { type EchoDatabase, Filter, type
|
|
7
|
-
import { getSchema, getSchemaTypename,
|
|
8
|
+
import { type EchoDatabase, Filter, type AnyLiveObject } from '@dxos/echo-db';
|
|
9
|
+
import { getSchema, getSchemaTypename, StoredSchema, toJsonSchema } from '@dxos/echo-schema';
|
|
8
10
|
import { log } from '@dxos/log';
|
|
9
11
|
|
|
10
12
|
import type { DataSource, Node, Relationship } from './query-executor';
|
|
@@ -20,13 +22,12 @@ export class EchoDataSource implements DataSource {
|
|
|
20
22
|
return objects.map(this._objectToNode);
|
|
21
23
|
}
|
|
22
24
|
|
|
23
|
-
const schema = (await this._getAllSchema()).find((
|
|
24
|
-
|
|
25
|
+
const schema = (await this._getAllSchema()).find((schema) => getSchemaTypename(schema)?.endsWith(label));
|
|
25
26
|
if (!schema) {
|
|
26
27
|
return [];
|
|
27
28
|
}
|
|
28
29
|
|
|
29
|
-
const { objects } = await this._db.query(Filter.
|
|
30
|
+
const { objects } = await this._db.query(Filter.type(schema)).run();
|
|
30
31
|
return objects.map(this._objectToNode);
|
|
31
32
|
}
|
|
32
33
|
|
|
@@ -56,21 +57,21 @@ export class EchoDataSource implements DataSource {
|
|
|
56
57
|
return []; // TODO(dmaretskyi): Handle real relationships.
|
|
57
58
|
}
|
|
58
59
|
|
|
59
|
-
const { objects } = await this._db.query(Filter.
|
|
60
|
+
const { objects } = await this._db.query(Filter.type(relationship.schema)).run();
|
|
60
61
|
return (
|
|
61
62
|
await Promise.all(objects.map((object) => this._projectRefRelationship(object, relationship.property)))
|
|
62
63
|
).flat();
|
|
63
64
|
}
|
|
64
65
|
|
|
65
|
-
private async _getAllSchema(): Promise<
|
|
66
|
+
private async _getAllSchema(): Promise<Schema.Schema.AnyNoContext[]> {
|
|
66
67
|
return [
|
|
67
68
|
...(await this._db.schemaRegistry.query().run()),
|
|
68
69
|
// TODO(dmaretskyi): Remove once we can serialize recursive schema.
|
|
69
|
-
...this._db.graph.schemaRegistry.schemas.filter((
|
|
70
|
+
...this._db.graph.schemaRegistry.schemas.filter((schema) => getSchemaTypename(schema)?.startsWith('example.org')),
|
|
70
71
|
].filter((schema) => getSchemaTypename(schema) !== StoredSchema.typename);
|
|
71
72
|
}
|
|
72
73
|
|
|
73
|
-
private _objectToNode(object:
|
|
74
|
+
private _objectToNode(object: AnyLiveObject<any>): Node {
|
|
74
75
|
const { id, ...properties } = object;
|
|
75
76
|
return {
|
|
76
77
|
id,
|
|
@@ -80,7 +81,7 @@ export class EchoDataSource implements DataSource {
|
|
|
80
81
|
};
|
|
81
82
|
}
|
|
82
83
|
|
|
83
|
-
private async _projectRefRelationship(object:
|
|
84
|
+
private async _projectRefRelationship(object: AnyLiveObject<any>, prop: string): Promise<Relationship[]> {
|
|
84
85
|
if (!object[prop]) {
|
|
85
86
|
return [];
|
|
86
87
|
}
|
|
@@ -21,7 +21,7 @@ describe('Execution Plan', () => {
|
|
|
21
21
|
test('complex query', ({ expect }) => {
|
|
22
22
|
const plan = createExecutionPlan(
|
|
23
23
|
parseCypherQuery(
|
|
24
|
-
"MATCH (org:
|
|
24
|
+
"MATCH (org:Organization {name: 'DXOS'})-[:ORG_EMPLOYEES]->(c:Contact)<-[:TASK_ASSIGNEE]-(:Task)-[:TASK_PROJECT]->(p:Project {name: 'Composer'}) RETURN c.name",
|
|
25
25
|
),
|
|
26
26
|
);
|
|
27
27
|
log('', { plan });
|
|
@@ -7,7 +7,7 @@ import { test } from 'vitest';
|
|
|
7
7
|
import { log } from '@dxos/log';
|
|
8
8
|
|
|
9
9
|
import { executeQuery } from './query-executor';
|
|
10
|
-
import { createTestData } from '
|
|
10
|
+
import { createTestData } from '../../testing';
|
|
11
11
|
|
|
12
12
|
test.skip('run', async ({ expect }) => {
|
|
13
13
|
const dataSource = createTestData();
|
|
@@ -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:Organization {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, Organization, Project, Task } from '../../testing';
|
|
11
11
|
|
|
12
|
-
test('
|
|
13
|
-
const jsonSchema = [
|
|
12
|
+
test('Organization-Project-Task-Contact schema', async ({ expect }) => {
|
|
13
|
+
const jsonSchema = [Organization, Project, Task, Contact].map((schema) => toJsonSchema(schema));
|
|
14
14
|
const result = formatJsonSchemaForLLM(jsonSchema);
|
|
15
15
|
// log('result', { result });
|
|
16
16
|
expect(result).toHaveLength(8);
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { pipeline } from '@xenova/transformers';
|
|
6
|
+
import { beforeAll, describe, expect, test } from 'vitest';
|
|
7
|
+
|
|
8
|
+
import { log } from '@dxos/log';
|
|
9
|
+
import { createTestData } from '@dxos/schema/testing';
|
|
10
|
+
|
|
11
|
+
type NerToken = {
|
|
12
|
+
entity: string;
|
|
13
|
+
score: number;
|
|
14
|
+
index: number;
|
|
15
|
+
word: string;
|
|
16
|
+
start: number | null;
|
|
17
|
+
end: number | null;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const createTokenGroups = (tokens: NerToken[]): NerToken[][] => {
|
|
21
|
+
const tokenGroups: NerToken[][] = [];
|
|
22
|
+
|
|
23
|
+
const canBeCombined = (prevToken: NerToken, currentToken: NerToken) => {
|
|
24
|
+
const [currentState, currentEntity] = currentToken.entity.split('-'); // B, I, O
|
|
25
|
+
const [prevState, prevEntity] = prevToken.entity.split('-'); // B, I, O
|
|
26
|
+
|
|
27
|
+
if (prevEntity !== currentEntity) {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (prevState === 'O') {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (currentState === 'I') {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if ((prevState === 'B' || prevState === 'I') && currentState === 'B') {
|
|
40
|
+
return currentToken.word.startsWith('#');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return false;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
for (const token of tokens) {
|
|
47
|
+
if (tokenGroups.length === 0) {
|
|
48
|
+
tokenGroups.push([token]);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const lastTokenGroup = tokenGroups.at(-1)!;
|
|
53
|
+
if (canBeCombined(lastTokenGroup.at(-1)!, token)) {
|
|
54
|
+
lastTokenGroup.push(token);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
tokenGroups.push([token]);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return tokenGroups;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const combineNerTokens = (group: NerToken[]): NerToken => {
|
|
65
|
+
const combinedToken: NerToken = {
|
|
66
|
+
entity: group.at(0)!.entity.split('-')[1],
|
|
67
|
+
score: group.reduce((acc, token) => acc + token.score, 0) / group.length,
|
|
68
|
+
index: group.at(0)!.index,
|
|
69
|
+
word: group.at(0)!.word,
|
|
70
|
+
start: group.at(0)!.start,
|
|
71
|
+
end: group.at(-1)!.end,
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
for (const token of group.slice(1)) {
|
|
75
|
+
if (token.word.startsWith('#')) {
|
|
76
|
+
combinedToken.word += token.word.replace(/^#+/, '');
|
|
77
|
+
} else {
|
|
78
|
+
combinedToken.word += ' ' + token.word;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return combinedToken;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
describe.skip('Named Entity Recognition', () => {
|
|
86
|
+
let ner: any;
|
|
87
|
+
|
|
88
|
+
beforeAll(async () => {
|
|
89
|
+
// Initialize the NER pipeline
|
|
90
|
+
ner = await pipeline('ner', 'Xenova/bert-base-NER');
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test.skip('should identify named entities in text', async () => {
|
|
94
|
+
const text = 'Elon Musk is the CEO of SpaceX and Tesla.';
|
|
95
|
+
const result = await ner(text);
|
|
96
|
+
|
|
97
|
+
// Verify the structure of the result
|
|
98
|
+
expect(Array.isArray(result)).toBe(true);
|
|
99
|
+
expect(result.length).toBeGreaterThan(0);
|
|
100
|
+
|
|
101
|
+
log.info('ner', { result });
|
|
102
|
+
|
|
103
|
+
// Check for specific entities
|
|
104
|
+
const entities = result.map((item: any) => ({
|
|
105
|
+
text: item.word,
|
|
106
|
+
entity: item.entity,
|
|
107
|
+
score: item.score,
|
|
108
|
+
}));
|
|
109
|
+
|
|
110
|
+
// Verify Elon Musk is recognized as a person
|
|
111
|
+
const elonMusk = entities.find((e: any) => e.text === 'Elon' || e.text === 'Musk');
|
|
112
|
+
expect(elonMusk).toBeDefined();
|
|
113
|
+
expect(elonMusk?.entity).toMatch(/PER/);
|
|
114
|
+
|
|
115
|
+
// Verify SpaceX and Tesla are recognized as organizations
|
|
116
|
+
const companies = entities.filter((e: any) => e.text === 'SpaceX' || e.text === 'Tesla');
|
|
117
|
+
expect(companies.length).toBe(2);
|
|
118
|
+
companies.forEach((company: any) => {
|
|
119
|
+
expect(company.entity).toMatch(/ORG/);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test('own data', async () => {
|
|
124
|
+
const { transcriptWoflram, transcriptJosiah } = await createTestData();
|
|
125
|
+
|
|
126
|
+
const blocks = [...transcriptWoflram, ...transcriptJosiah].flatMap((message) =>
|
|
127
|
+
message.blocks.flatMap((block) => (block.type === 'transcription' ? [block.text] : [])),
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
for (const block of blocks) {
|
|
131
|
+
const result = await ner(block);
|
|
132
|
+
const combinedTokens = createTokenGroups(result).map(combineNerTokens);
|
|
133
|
+
log.info('ner', { text: block, result: combinedTokens });
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test('more own data', async () => {
|
|
138
|
+
const { transcriptMessages } = await createTestData();
|
|
139
|
+
|
|
140
|
+
const blocks = transcriptMessages.flatMap((message) =>
|
|
141
|
+
message.blocks.flatMap((block) => (block.type === 'transcription' ? [block.text] : [])),
|
|
142
|
+
);
|
|
143
|
+
|
|
144
|
+
for (const block of blocks) {
|
|
145
|
+
const result = await ner(block);
|
|
146
|
+
const combinedTokens = createTokenGroups(result).map(combineNerTokens);
|
|
147
|
+
log.info('ner', { text: block, result: combinedTokens });
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test('should handle empty text', async () => {
|
|
152
|
+
const result = await ner('');
|
|
153
|
+
expect(Array.isArray(result)).toBe(true);
|
|
154
|
+
expect(result.length).toBe(0);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
test('should handle text with no named entities', async () => {
|
|
158
|
+
const text = 'The weather is nice today.';
|
|
159
|
+
const result = await ner(text);
|
|
160
|
+
expect(Array.isArray(result)).toBe(true);
|
|
161
|
+
expect(result.length).toBe(0);
|
|
162
|
+
});
|
|
163
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Schema } from 'effect';
|
|
6
|
+
import Exa from 'exa-js';
|
|
7
|
+
|
|
8
|
+
import { defineTool, ToolResult } from '@dxos/artifact';
|
|
9
|
+
import { log } from '@dxos/log';
|
|
10
|
+
|
|
11
|
+
type CreateExaToolOptions = {
|
|
12
|
+
apiKey: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const createExaTool = ({ apiKey }: CreateExaToolOptions) => {
|
|
16
|
+
const exa = new Exa(apiKey);
|
|
17
|
+
|
|
18
|
+
return defineTool('search', {
|
|
19
|
+
name: 'web_search',
|
|
20
|
+
description: 'Search the web for information',
|
|
21
|
+
schema: Schema.Struct({
|
|
22
|
+
query: Schema.String.annotations({ description: 'The query to search for' }),
|
|
23
|
+
}),
|
|
24
|
+
execute: async ({ query }) => {
|
|
25
|
+
const context = await exa.searchAndContents(query, {
|
|
26
|
+
type: 'auto',
|
|
27
|
+
text: {
|
|
28
|
+
maxCharacters: 3_000,
|
|
29
|
+
},
|
|
30
|
+
livecrawl: 'always',
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
log.info('exa search', { query, costDollars: context.costDollars });
|
|
34
|
+
|
|
35
|
+
return ToolResult.Success(context);
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
You are an expert research agent. Your job is to assist a user by conducting in-depth research using real-time web search.
|
|
2
|
+
You output the results in a structured format matching the schema provided.
|
|
3
|
+
|
|
4
|
+
You are equipped with the ability to:
|
|
5
|
+
|
|
6
|
+
- Generate precise and effective search queries.
|
|
7
|
+
- Request web pages by query (through a `web_search` tool).
|
|
8
|
+
- Read the full content of retrieved pages.
|
|
9
|
+
- Synthesize accurate, clear, and structured answers using reliable information from the retrieved content.
|
|
10
|
+
|
|
11
|
+
Always follow these principles:
|
|
12
|
+
|
|
13
|
+
- Relevance First: Only return facts that are supported by content in the retrieved web pages. Never fabricate or guess information.
|
|
14
|
+
- Summarize, Don't Copy: Synthesize and rephrase content in your own words. Quote only when necessary.
|
|
15
|
+
- Multiple Sources: Cross-reference at least 2 sources before drawing conclusions, unless the information is directly stated and non-controversial.
|
|
16
|
+
- Transparency: Mention which sources were used and explain how you arrived at your conclusions.
|
|
17
|
+
- Accuracy Over Brevity: Prefer detailed, technically accurate explanations over shallow summaries.
|
|
18
|
+
- If uncertain, say so. It's better to admit uncertainty than to mislead.
|
|
19
|
+
- Pick the most concrete schema types for the extracted information.
|
|
20
|
+
- Fill the schema fields as completely as possible with the information you are confident about, do not fill the fields that you are not confident about.
|
|
21
|
+
- When outputting results, you can add extra data that isn't directly related to the user's question but fits the schema.
|
|
22
|
+
|
|
23
|
+
The user may ask for:
|
|
24
|
+
|
|
25
|
+
- Technical explanations
|
|
26
|
+
- Literature reviews
|
|
27
|
+
- Comparisons
|
|
28
|
+
- Emerging trends
|
|
29
|
+
- Implementation strategies
|
|
30
|
+
|
|
31
|
+
Begin by interpreting the user's request, then:
|
|
32
|
+
|
|
33
|
+
Break it into sub-questions (if applicable).
|
|
34
|
+
|
|
35
|
+
For each sub-question, generate a clear, concise web search query.
|
|
36
|
+
|
|
37
|
+
Use `web_search`(query) to retrieve information.
|
|
38
|
+
|
|
39
|
+
Extract and synthesize relevant answers.
|
|
40
|
+
|
|
41
|
+
Output should include:
|
|
42
|
+
|
|
43
|
+
- A clear, structured answer to the user’s question.
|
|
44
|
+
- A citation list or link list of the sources used.
|
|
45
|
+
|
|
46
|
+
Optionally, follow-up suggestions or questions for deeper inquiry.
|
|
47
|
+
|
|
48
|
+
Here's your operational instructions:
|
|
49
|
+
|
|
50
|
+
1. Analyze user's request and identify key topics to search for (3 or more). Print them out.
|
|
51
|
+
2. Perform a web search for each topic.
|
|
52
|
+
3. Read and analyze the results, cross reference information from multiple sources, in case of conflict, represent the information as a range of possible values.
|
|
53
|
+
4. Create a list of follow-up topics to research. Print them out.
|
|
54
|
+
5. Perform a web search for each follow-up topic.
|
|
55
|
+
6. Read and analyze the results, cross reference information from multiple sources, in case of conflict, represent the information as a range of possible values.
|
|
56
|
+
9. Create a clear, structured answer to the user's question.
|
|
57
|
+
10. Create a citation list or link list of the sources used.
|
|
58
|
+
11. Select the most concrete schema types for the extracted information. You can use multiple different schema types. Print your decision and reasoning.
|
|
59
|
+
12. Submit the results using the specific schema.
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|