@dxos/assistant 0.8.4-main.b97322e → 0.8.4-main.c4373fc
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-CWGVG235.mjs +380 -0
- package/dist/lib/browser/chunk-CWGVG235.mjs.map +7 -0
- package/dist/lib/browser/chunk-UB26N5UF.mjs +89 -0
- package/dist/lib/browser/chunk-UB26N5UF.mjs.map +7 -0
- package/dist/lib/browser/extraction/index.mjs +272 -0
- package/dist/lib/browser/extraction/index.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +687 -1649
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs +13 -0
- package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-BCFXTNSK.mjs +91 -0
- package/dist/lib/node-esm/chunk-BCFXTNSK.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-OV5PMQPB.mjs +382 -0
- package/dist/lib/node-esm/chunk-OV5PMQPB.mjs.map +7 -0
- package/dist/lib/node-esm/extraction/index.mjs +273 -0
- package/dist/lib/node-esm/extraction/index.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +686 -1649
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs +14 -0
- package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs.map +7 -0
- package/dist/types/src/conversation/context.d.ts +97 -0
- package/dist/types/src/conversation/context.d.ts.map +1 -0
- package/dist/types/src/conversation/conversation.d.ts +25 -22
- package/dist/types/src/conversation/conversation.d.ts.map +1 -1
- package/dist/types/src/conversation/index.d.ts +1 -0
- package/dist/types/src/conversation/index.d.ts.map +1 -1
- package/dist/types/src/diff.d.ts +15 -0
- package/dist/types/src/diff.d.ts.map +1 -0
- package/dist/types/src/diff.test.d.ts +2 -0
- package/dist/types/src/diff.test.d.ts.map +1 -0
- package/dist/types/src/errors.d.ts +46 -9
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/execution-graph.d.ts +82 -0
- package/dist/types/src/execution-graph.d.ts.map +1 -0
- package/dist/types/src/execution-graph.test.d.ts +2 -0
- package/dist/types/src/execution-graph.test.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +1 -1
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +103 -64
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction.d.ts +252 -218
- package/dist/types/src/extraction/extraction.d.ts.map +1 -1
- package/dist/types/src/extraction/quotes.d.ts +1 -1
- package/dist/types/src/extraction/quotes.d.ts.map +1 -1
- package/dist/types/src/functions/services.d.ts +17 -4
- package/dist/types/src/functions/services.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +3 -3
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/session/artifact-diff.d.ts +27 -0
- package/dist/types/src/session/artifact-diff.d.ts.map +1 -0
- package/dist/types/src/session/error-handling.d.ts +4 -0
- package/dist/types/src/session/error-handling.d.ts.map +1 -0
- package/dist/types/src/session/format.d.ts +184 -0
- package/dist/types/src/session/format.d.ts.map +1 -0
- package/dist/types/src/session/format.test.d.ts +2 -0
- package/dist/types/src/session/format.test.d.ts.map +1 -0
- package/dist/types/src/session/index.d.ts +3 -0
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/observer.d.ts +22 -0
- package/dist/types/src/session/observer.d.ts.map +1 -0
- package/dist/types/src/session/session.d.ts +27 -236
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/session/toolkit.d.ts +15 -0
- package/dist/types/src/session/toolkit.d.ts.map +1 -0
- package/dist/types/src/session/version-pin.d.ts +3 -3
- package/dist/types/src/session/version-pin.d.ts.map +1 -1
- package/dist/types/src/templates/index.d.ts +1 -1
- package/dist/types/src/templates/system.d.ts +18 -0
- package/dist/types/src/templates/system.d.ts.map +1 -0
- package/dist/types/src/types.d.ts +5 -2
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +44 -32
- package/src/conversation/context.ts +156 -0
- package/src/conversation/conversation.ts +78 -53
- package/src/conversation/index.ts +1 -0
- package/src/diff.test.ts +43 -0
- package/src/diff.ts +79 -0
- package/src/errors.ts +6 -1
- package/src/execution-graph.test.ts +738 -0
- package/src/execution-graph.ts +500 -0
- package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +8 -7
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +13 -11
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +9 -9
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +15 -8
- package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +2 -1
- package/src/extraction/extraction-ner-function/named-entity-recognition.ts +2 -0
- package/src/extraction/extraction.ts +2 -2
- package/src/extraction/quotes.test.ts +3 -3
- package/src/extraction/quotes.ts +4 -4
- package/src/functions/services.ts +114 -95
- package/src/index.ts +3 -3
- package/src/session/artifact-diff.ts +41 -0
- package/src/session/error-handling.ts +41 -0
- package/src/session/format.test.ts +60 -0
- package/src/session/format.ts +142 -0
- package/src/session/index.ts +3 -0
- package/src/session/observer.ts +59 -0
- package/src/session/session.conversations.json +1 -0
- package/src/session/session.test.ts +37 -198
- package/src/session/session.ts +125 -249
- package/src/session/toolkit.ts +48 -0
- package/src/session/version-pin.ts +5 -5
- package/src/summarization/summarization.test.ts +16 -22
- package/src/templates/format.tpl +60 -0
- package/src/templates/index.ts +1 -1
- package/src/templates/system.tpl +48 -0
- package/src/templates/system.ts +41 -0
- package/src/types.ts +12 -7
- package/src/typings.d.ts +5 -0
- package/dist/types/bin/chat.d.ts +0 -2
- package/dist/types/bin/chat.d.ts.map +0 -1
- package/dist/types/src/context/binder.d.ts +0 -34
- package/dist/types/src/context/binder.d.ts.map +0 -1
- package/dist/types/src/context/binding.d.ts +0 -56
- package/dist/types/src/context/binding.d.ts.map +0 -1
- package/dist/types/src/context/index.d.ts +0 -3
- package/dist/types/src/context/index.d.ts.map +0 -1
- package/dist/types/src/research/exa.d.ts +0 -12
- package/dist/types/src/research/exa.d.ts.map +0 -1
- package/dist/types/src/research/graph.d.ts +0 -51
- package/dist/types/src/research/graph.d.ts.map +0 -1
- package/dist/types/src/research/graph.test.d.ts +0 -2
- package/dist/types/src/research/graph.test.d.ts.map +0 -1
- package/dist/types/src/research/index.d.ts +0 -4
- package/dist/types/src/research/index.d.ts.map +0 -1
- package/dist/types/src/research/research.d.ts +0 -10
- package/dist/types/src/research/research.d.ts.map +0 -1
- package/dist/types/src/research/research.test.d.ts +0 -2
- package/dist/types/src/research/research.test.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/templates/prompt.d.ts +0 -30
- package/dist/types/src/templates/prompt.d.ts.map +0 -1
- package/dist/types/src/testing/exa-search-1748337321991.d.ts +0 -38
- package/dist/types/src/testing/exa-search-1748337321991.d.ts.map +0 -1
- package/dist/types/src/testing/exa-search-1748337331526.d.ts +0 -37
- package/dist/types/src/testing/exa-search-1748337331526.d.ts.map +0 -1
- package/dist/types/src/testing/exa-search-1748337344119.d.ts +0 -58
- package/dist/types/src/testing/exa-search-1748337344119.d.ts.map +0 -1
- package/dist/types/src/testing/index.d.ts +0 -3
- package/dist/types/src/testing/index.d.ts.map +0 -1
- package/src/context/binder.ts +0 -103
- package/src/context/binding.ts +0 -32
- package/src/context/index.ts +0 -6
- package/src/research/exa.ts +0 -97
- package/src/research/graph.test.ts +0 -64
- package/src/research/graph.ts +0 -369
- package/src/research/index.ts +0 -7
- package/src/research/instructions-research.tpl +0 -98
- package/src/research/research.test.ts +0 -190
- package/src/research/research.ts +0 -87
- package/src/session/__snapshots__/session.test.ts.snap +0 -32
- package/src/session/version-pin.test.ts +0 -7
- package/src/templates/assistant.tpl +0 -111
- package/src/templates/prompt.ts +0 -45
- package/src/testing/exa-search-1748337321991.ts +0 -131
- package/src/testing/exa-search-1748337331526.ts +0 -144
- package/src/testing/exa-search-1748337344119.ts +0 -133
- package/src/testing/index.ts +0 -11
- package/src/typedefs.d.ts +0 -8
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Array from 'effect/Array';
|
|
6
|
+
import * as Effect from 'effect/Effect';
|
|
7
|
+
import * as Function from 'effect/Function';
|
|
8
|
+
import * as Option from 'effect/Option';
|
|
9
|
+
|
|
10
|
+
import { Template } from '@dxos/blueprints';
|
|
11
|
+
import { Obj } from '@dxos/echo';
|
|
12
|
+
import { type ObjectId } from '@dxos/echo/internal';
|
|
13
|
+
import { ObjectVersion } from '@dxos/echo-db';
|
|
14
|
+
import { DatabaseService } from '@dxos/functions';
|
|
15
|
+
import { log } from '@dxos/log';
|
|
16
|
+
import { type ContentBlock, DataType } from '@dxos/schema';
|
|
17
|
+
import { trim } from '@dxos/util';
|
|
18
|
+
|
|
19
|
+
import { AiAssistantError } from '../errors';
|
|
20
|
+
|
|
21
|
+
import { ArtifactDiffResolver } from './artifact-diff';
|
|
22
|
+
import { type AiSessionRunParams } from './session';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Formats the system prompt.
|
|
26
|
+
*/
|
|
27
|
+
// TODO(burdon): Move to AiPreprocessor.
|
|
28
|
+
export const formatSystemPrompt = ({
|
|
29
|
+
system,
|
|
30
|
+
blueprints = [],
|
|
31
|
+
objects = [],
|
|
32
|
+
}: Pick<AiSessionRunParams<any>, 'system' | 'blueprints' | 'objects'>) =>
|
|
33
|
+
Effect.gen(function* () {
|
|
34
|
+
const blueprintDefs = yield* Function.pipe(
|
|
35
|
+
blueprints,
|
|
36
|
+
Effect.forEach((blueprint) => Effect.succeed(blueprint.instructions)),
|
|
37
|
+
Effect.flatMap(Effect.forEach((template) => DatabaseService.loadOption(template.source))),
|
|
38
|
+
Effect.map(Array.filter(Option.isSome)),
|
|
39
|
+
Effect.map(
|
|
40
|
+
Array.map(
|
|
41
|
+
(template) => trim`
|
|
42
|
+
<blueprint>
|
|
43
|
+
${Template.process(template.value.content)}
|
|
44
|
+
</blueprint>
|
|
45
|
+
`,
|
|
46
|
+
),
|
|
47
|
+
),
|
|
48
|
+
Effect.map((blueprints) =>
|
|
49
|
+
blueprints.length > 0 ? ['## Blueprints Definitions', ...blueprints].join('\n\n') : undefined,
|
|
50
|
+
),
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
const objectDefs = yield* Function.pipe(
|
|
54
|
+
objects,
|
|
55
|
+
Effect.forEach((object) =>
|
|
56
|
+
Effect.succeed(trim`
|
|
57
|
+
<object>
|
|
58
|
+
<dxn>${Obj.getDXN(object)}</dxn>
|
|
59
|
+
<typename>${Obj.getTypename(object)}</typename>
|
|
60
|
+
</object>
|
|
61
|
+
`),
|
|
62
|
+
),
|
|
63
|
+
Effect.map((objects) => (objects.length > 0 ? ['## Context Objects', ...objects].join('\n\n') : undefined)),
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
return yield* Function.pipe(
|
|
67
|
+
Effect.succeed([system, blueprintDefs, objectDefs].filter((def): def is string => def !== undefined)),
|
|
68
|
+
Effect.map((parts) => parts.join('\n\n')),
|
|
69
|
+
);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Formats the user prompt.
|
|
74
|
+
*/
|
|
75
|
+
// TODO(burdon): Move to AiPreprocessor.
|
|
76
|
+
// TODO(burdon): Convert util below to `Effect.fn` (to preserve stack info)
|
|
77
|
+
export const formatUserPrompt = ({ prompt, history = [] }: Pick<AiSessionRunParams<any>, 'prompt' | 'history'>) =>
|
|
78
|
+
Effect.gen(function* () {
|
|
79
|
+
const blocks: ContentBlock.Any[] = [];
|
|
80
|
+
|
|
81
|
+
// TODO(dmaretskyi): Evaluate other approaches as `serviceOption` isn't represented in the type system.
|
|
82
|
+
const artifactDiffResolver = yield* Effect.serviceOption(ArtifactDiffResolver);
|
|
83
|
+
if (Option.isSome(artifactDiffResolver)) {
|
|
84
|
+
const versions = gatherObjectVersions(history);
|
|
85
|
+
const artifactDiff = yield* Effect.tryPromise({
|
|
86
|
+
try: () =>
|
|
87
|
+
artifactDiffResolver.value.resolve(
|
|
88
|
+
[...versions.entries()].map(([id, version]) => ({ id, lastVersion: version })),
|
|
89
|
+
),
|
|
90
|
+
catch: AiAssistantError.wrap({ message: 'Artifact diff resolution error' }),
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
log('version', { artifactDiff, versions });
|
|
94
|
+
for (const [id, { version }] of [...artifactDiff.entries()]) {
|
|
95
|
+
if (ObjectVersion.equals(version, versions.get(id)!)) {
|
|
96
|
+
artifactDiff.delete(id);
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
blocks.push({ _tag: 'anchor', objectId: id, version });
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (artifactDiff.size > 0) {
|
|
104
|
+
blocks.push(createArtifactUpdateBlock(artifactDiff));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return Obj.make(DataType.Message, {
|
|
109
|
+
created: new Date().toISOString(),
|
|
110
|
+
sender: { role: 'user' },
|
|
111
|
+
blocks: [...blocks, { _tag: 'text', text: prompt }],
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
const gatherObjectVersions = (messages: DataType.Message[]): Map<ObjectId, ObjectVersion> => {
|
|
116
|
+
const artifactIds = new Map<ObjectId, ObjectVersion>();
|
|
117
|
+
for (const message of messages) {
|
|
118
|
+
for (const block of message.blocks) {
|
|
119
|
+
if (block._tag === 'anchor') {
|
|
120
|
+
artifactIds.set(block.objectId, block.version as ObjectVersion);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return artifactIds;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const createArtifactUpdateBlock = (
|
|
129
|
+
artifactDiff: Map<ObjectId, { version: ObjectVersion; diff?: string }>,
|
|
130
|
+
): ContentBlock.Any => {
|
|
131
|
+
return {
|
|
132
|
+
_tag: 'text',
|
|
133
|
+
// TODO(dmaretskyi): Does this need to be a special content-block?
|
|
134
|
+
disposition: 'artifact-update',
|
|
135
|
+
text: trim`
|
|
136
|
+
The following artifacts have been updated since the last message:
|
|
137
|
+
${[...artifactDiff.entries()]
|
|
138
|
+
.map(([id, { diff }]) => `<changed-artifact id="${id}">${diff ? `\n${diff}` : ''}</changed-artifact>`)
|
|
139
|
+
.join('\n')}
|
|
140
|
+
`,
|
|
141
|
+
};
|
|
142
|
+
};
|
package/src/session/index.ts
CHANGED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Effect from 'effect/Effect';
|
|
6
|
+
import * as Function from 'effect/Function';
|
|
7
|
+
|
|
8
|
+
import { type AiParser, type ConsolePrinter } from '@dxos/ai';
|
|
9
|
+
import { type DataType } from '@dxos/schema';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Live observer of the generation process.
|
|
13
|
+
*/
|
|
14
|
+
export interface GenerationObserver extends AiParser.ParseResponseCallbacks {
|
|
15
|
+
/**
|
|
16
|
+
* Complete messages fired during the session, both from the model and from the user.
|
|
17
|
+
* This message will contain all message blocks emitted through the `onBlock` callback.
|
|
18
|
+
*/
|
|
19
|
+
onMessage: (message: DataType.Message) => Effect.Effect<void>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const GenerationObserver = Object.freeze({
|
|
23
|
+
make: ({
|
|
24
|
+
onBegin = Function.constant(Effect.void),
|
|
25
|
+
onPart = Function.constant(Effect.void),
|
|
26
|
+
onBlock = Function.constant(Effect.void),
|
|
27
|
+
onEnd = Function.constant(Effect.void),
|
|
28
|
+
onMessage = Function.constant(Effect.void),
|
|
29
|
+
}: Partial<GenerationObserver> = {}): GenerationObserver => ({
|
|
30
|
+
onBegin,
|
|
31
|
+
onPart,
|
|
32
|
+
onBlock,
|
|
33
|
+
onEnd,
|
|
34
|
+
onMessage,
|
|
35
|
+
}),
|
|
36
|
+
|
|
37
|
+
noop: () => GenerationObserver.make(),
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Debug printer to be used in unit-tests and browser devtools.
|
|
41
|
+
*/
|
|
42
|
+
fromPrinter: (printer: ConsolePrinter) =>
|
|
43
|
+
GenerationObserver.make({
|
|
44
|
+
onBlock: (block) =>
|
|
45
|
+
Effect.sync(() => {
|
|
46
|
+
if (block.pending) {
|
|
47
|
+
return; // Only prints full blocks (better for unit-tests and browser devtools).
|
|
48
|
+
}
|
|
49
|
+
printer.printContentBlock(block);
|
|
50
|
+
}),
|
|
51
|
+
onMessage: (message) =>
|
|
52
|
+
Effect.sync(() => {
|
|
53
|
+
if (message.sender.role === 'assistant') {
|
|
54
|
+
return; // Skip assistant messages since they are printed in the `onBlock` callback.
|
|
55
|
+
}
|
|
56
|
+
printer.printMessage(message);
|
|
57
|
+
}),
|
|
58
|
+
}),
|
|
59
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"conversations":[{"parameters":{"model":"@anthropic/claude-3-5-sonnet-20241022","stream":true,"tools":[]},"prompt":{"content":[{"role":"user","content":"Hello world!","options":{}}]},"response":[{"type":"response-metadata","id":"msg_01Bm496JGgDMgC9YwdYTFFHo","modelId":"claude-3-5-sonnet-20241022","timestamp":"1970-01-01T00:00:00.000Z","metadata":{}},{"type":"text-start","id":"0","metadata":{}},{"type":"text-delta","id":"0","delta":"Hi","metadata":{}},{"type":"text-delta","id":"0","delta":"!","metadata":{}},{"type":"text-delta","id":"0","delta":" How can","metadata":{}},{"type":"text-delta","id":"0","delta":" I help you today?","metadata":{}},{"type":"text-end","id":"0","metadata":{}},{"type":"finish","reason":"stop","usage":{"inputTokens":10,"outputTokens":12,"totalTokens":22},"metadata":{"anthropic":{"usage":{"cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"input_tokens":10,"output_tokens":1,"server_tool_use":null,"service_tier":"standard"}}}}]},{"parameters":{"model":"@anthropic/claude-3-5-sonnet-20241022","stream":true,"tools":[{"name":"Calculator","description":"Basic calculator tool","inputSchema":{"type":"object","required":["input"],"properties":{"input":{"type":"string","description":"The calculation to perform."}},"additionalProperties":false}}]},"prompt":{"content":[{"role":"user","content":"What is 10 + 30?","options":{}}]},"response":[{"type":"response-metadata","id":"msg_01UNTbjtTHEpdFgGunu2k8Br","modelId":"claude-3-5-sonnet-20241022","timestamp":"1970-01-01T00:00:00.000Z","metadata":{}},{"type":"text-start","id":"0","metadata":{}},{"type":"text-delta","id":"0","delta":"I","metadata":{}},{"type":"text-delta","id":"0","delta":"'ll help","metadata":{}},{"type":"text-delta","id":"0","delta":" you calculate","metadata":{}},{"type":"text-delta","id":"0","delta":" ","metadata":{}},{"type":"text-delta","id":"0","delta":"10 + 30","metadata":{}},{"type":"text-delta","id":"0","delta":" using the","metadata":{}},{"type":"text-delta","id":"0","delta":" Calculator tool.","metadata":{}},{"type":"text-end","id":"0","metadata":{}},{"type":"tool-params-start","id":"toolu_01LTGUgfihCQtVchm1b37xHQ","name":"Calculator","providerExecuted":false,"metadata":{}},{"type":"tool-params-delta","id":"toolu_01LTGUgfihCQtVchm1b37xHQ","delta":"","metadata":{}},{"type":"tool-params-delta","id":"toolu_01LTGUgfihCQtVchm1b37xHQ","delta":"{\"input\": \"","metadata":{}},{"type":"tool-params-delta","id":"toolu_01LTGUgfihCQtVchm1b37xHQ","delta":"10 + 30","metadata":{}},{"type":"tool-params-delta","id":"toolu_01LTGUgfihCQtVchm1b37xHQ","delta":"\"}","metadata":{}},{"type":"tool-params-end","id":"toolu_01LTGUgfihCQtVchm1b37xHQ","metadata":{}},{"type":"tool-call","id":"toolu_01LTGUgfihCQtVchm1b37xHQ","name":"Calculator","params":{"input":"10 + 30"},"providerExecuted":false,"metadata":{}},{"type":"finish","reason":"tool-calls","usage":{"inputTokens":394,"outputTokens":73,"totalTokens":467},"metadata":{"anthropic":{"usage":{"cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"input_tokens":394,"output_tokens":1,"server_tool_use":null,"service_tier":"standard"}}}}]},{"parameters":{"model":"@anthropic/claude-3-5-sonnet-20241022","stream":true,"tools":[{"name":"Calculator","description":"Basic calculator tool","inputSchema":{"type":"object","required":["input"],"properties":{"input":{"type":"string","description":"The calculation to perform."}},"additionalProperties":false}}]},"prompt":{"content":[{"role":"user","content":"What is 10 + 30?","options":{}},{"role":"assistant","content":[{"type":"text","text":"I'll help you calculate 10 + 30 using the Calculator tool.","options":{}},{"type":"tool-call","id":"toolu_01LTGUgfihCQtVchm1b37xHQ","name":"Calculator","params":{"input":"10 + 30"},"providerExecuted":false,"options":{}}],"options":{}},{"role":"tool","content":[{"type":"tool-result","id":"toolu_01LTGUgfihCQtVchm1b37xHQ","name":"Calculator","result":{"result":40},"options":{}}],"options":{}}]},"response":[{"type":"response-metadata","id":"msg_01AKio3GheJGn38BhvAp33rs","modelId":"claude-3-5-sonnet-20241022","timestamp":"1970-01-01T00:00:00.000Z","metadata":{}},{"type":"text-start","id":"0","metadata":{}},{"type":"text-delta","id":"0","delta":"The result","metadata":{}},{"type":"text-delta","id":"0","delta":" of 10 +","metadata":{}},{"type":"text-delta","id":"0","delta":" 30 is ","metadata":{}},{"type":"text-delta","id":"0","delta":"40.","metadata":{}},{"type":"text-end","id":"0","metadata":{}},{"type":"finish","reason":"stop","usage":{"inputTokens":482,"outputTokens":18,"totalTokens":500},"metadata":{"anthropic":{"usage":{"cache_creation":{"ephemeral_1h_input_tokens":0,"ephemeral_5m_input_tokens":0},"cache_creation_input_tokens":0,"cache_read_input_tokens":0,"input_tokens":482,"output_tokens":3,"server_tool_use":null,"service_tier":"standard"}}}}]}]}
|
|
@@ -2,13 +2,18 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import * as Tool from '@effect/ai/Tool';
|
|
6
|
+
import * as Toolkit from '@effect/ai/Toolkit';
|
|
6
7
|
import { describe, it } from '@effect/vitest';
|
|
7
|
-
import
|
|
8
|
+
import * as Effect from 'effect/Effect';
|
|
9
|
+
import * as Layer from 'effect/Layer';
|
|
10
|
+
import * as Schema from 'effect/Schema';
|
|
8
11
|
|
|
9
|
-
import { AiService, ToolExecutionService, ToolResolverService } from '@dxos/ai';
|
|
12
|
+
import { AiService, MemoizedAiService, ToolExecutionService, ToolResolverService } from '@dxos/ai';
|
|
10
13
|
import { AiServiceTestingPreset } from '@dxos/ai/testing';
|
|
11
14
|
import { Obj, Type } from '@dxos/echo';
|
|
15
|
+
import { TestHelpers } from '@dxos/effect';
|
|
16
|
+
import { TracingService } from '@dxos/functions';
|
|
12
17
|
import { log } from '@dxos/log';
|
|
13
18
|
|
|
14
19
|
import { AiSession } from './session';
|
|
@@ -28,8 +33,8 @@ const CalendarEventSchema = Schema.Struct({
|
|
|
28
33
|
|
|
29
34
|
type CalendarEvent = Schema.Schema.Type<typeof CalendarEventSchema>;
|
|
30
35
|
|
|
31
|
-
class TestToolkit extends
|
|
32
|
-
|
|
36
|
+
class TestToolkit extends Toolkit.make(
|
|
37
|
+
Tool.make('Calculator', {
|
|
33
38
|
description: 'Basic calculator tool',
|
|
34
39
|
parameters: {
|
|
35
40
|
input: Schema.String.annotations({
|
|
@@ -52,7 +57,7 @@ const toolkitLayer = TestToolkit.toLayer({
|
|
|
52
57
|
const sanitizedInput = input.replace(/[^0-9+\-*/().\s]/g, '');
|
|
53
58
|
log.info('calculate', { sanitizedInput });
|
|
54
59
|
|
|
55
|
-
// eslint-disable-next-line
|
|
60
|
+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
56
61
|
return Function(`"use strict"; return (${sanitizedInput})`)();
|
|
57
62
|
})();
|
|
58
63
|
|
|
@@ -60,182 +65,56 @@ const toolkitLayer = TestToolkit.toLayer({
|
|
|
60
65
|
}),
|
|
61
66
|
});
|
|
62
67
|
|
|
63
|
-
|
|
68
|
+
const TestLayer = Layer.mergeAll(
|
|
69
|
+
toolkitLayer,
|
|
70
|
+
AiService.model('@anthropic/claude-3-5-sonnet-20241022'),
|
|
71
|
+
TracingService.layerNoop,
|
|
72
|
+
ToolResolverService.layerEmpty,
|
|
73
|
+
ToolExecutionService.layerEmpty,
|
|
74
|
+
).pipe(
|
|
75
|
+
//
|
|
76
|
+
Layer.provideMerge(MemoizedAiService.layerTest()),
|
|
77
|
+
Layer.provide(AiServiceTestingPreset('direct')),
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
describe('AiSession', () => {
|
|
64
81
|
it.effect(
|
|
65
82
|
'no tools',
|
|
66
|
-
Effect.
|
|
67
|
-
function* (
|
|
83
|
+
Effect.fnUntraced(
|
|
84
|
+
function* (_) {
|
|
68
85
|
const session = new AiSession({ operationModel: 'configured' });
|
|
69
|
-
|
|
70
86
|
const response = yield* session.run({
|
|
71
87
|
prompt: 'Hello world!',
|
|
72
88
|
history: [],
|
|
73
89
|
});
|
|
74
90
|
log.info('response', { response });
|
|
75
91
|
},
|
|
76
|
-
Effect.provide(
|
|
77
|
-
|
|
78
|
-
Layer.provideMerge(ToolResolverService.layerEmpty),
|
|
79
|
-
Layer.provideMerge(ToolExecutionService.layerEmpty),
|
|
80
|
-
Layer.provideMerge(AiServiceTestingPreset('direct')),
|
|
81
|
-
),
|
|
82
|
-
),
|
|
92
|
+
Effect.provide(TestLayer),
|
|
93
|
+
TestHelpers.provideTestContext,
|
|
83
94
|
),
|
|
84
95
|
);
|
|
85
96
|
|
|
86
|
-
it.effect
|
|
97
|
+
it.effect(
|
|
87
98
|
'calculator',
|
|
88
|
-
Effect.
|
|
89
|
-
function* (
|
|
99
|
+
Effect.fnUntraced(
|
|
100
|
+
function* (_) {
|
|
90
101
|
const session = new AiSession({ operationModel: 'configured' });
|
|
91
|
-
|
|
102
|
+
const toolkit = yield* TestToolkit;
|
|
92
103
|
const response = yield* session.run({
|
|
93
|
-
|
|
104
|
+
toolkit,
|
|
105
|
+
prompt: 'What is 10 + 30?',
|
|
94
106
|
history: [],
|
|
95
|
-
toolkit: TestToolkit,
|
|
96
107
|
});
|
|
97
108
|
log.info('response', { response });
|
|
98
109
|
},
|
|
99
|
-
Effect.provide(
|
|
100
|
-
|
|
101
|
-
toolkitLayer,
|
|
102
|
-
ToolResolverService.layerEmpty,
|
|
103
|
-
ToolExecutionService.layerEmpty,
|
|
104
|
-
AiService.model('@anthropic/claude-3-5-sonnet-20241022').pipe(
|
|
105
|
-
Layer.provideMerge(AiServiceTestingPreset('direct')),
|
|
106
|
-
),
|
|
107
|
-
),
|
|
108
|
-
),
|
|
110
|
+
Effect.provide(TestLayer),
|
|
111
|
+
TestHelpers.provideTestContext,
|
|
109
112
|
),
|
|
110
113
|
);
|
|
111
|
-
|
|
112
|
-
// TODO(dmaretskyi): Revive test.
|
|
113
|
-
/*
|
|
114
|
-
it.skip('create calendar itinerary', { timeout: 60_000 }, async () => {
|
|
115
|
-
|
|
116
|
-
// overrides: { model: 'llama3.1:8b' },
|
|
117
|
-
|
|
118
|
-
const session = new AiSession({ operationModel: 'configured' });
|
|
119
|
-
|
|
120
|
-
const objects = new Set<string>();
|
|
121
|
-
|
|
122
|
-
// Define calendar artifact.
|
|
123
|
-
const calendarArtifact = defineArtifact({
|
|
124
|
-
id: 'artifact:dxos.org/plugin/calendar',
|
|
125
|
-
name: 'Calendar',
|
|
126
|
-
instructions: 'Use this to create and query calendar events.',
|
|
127
|
-
schema: CalendarEventSchema,
|
|
128
|
-
tools: [
|
|
129
|
-
createTool('calendar', {
|
|
130
|
-
name: 'query',
|
|
131
|
-
description: 'Query the calendar for events',
|
|
132
|
-
schema: Schema.Struct({}),
|
|
133
|
-
execute: async () => {
|
|
134
|
-
return ToolResult.Success(CALENDAR_EVENTS);
|
|
135
|
-
},
|
|
136
|
-
}),
|
|
137
|
-
],
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
const tableArtifact = defineArtifact({
|
|
141
|
-
id: 'artifact:dxos.org/plugin/table',
|
|
142
|
-
name: 'Table',
|
|
143
|
-
instructions: 'Use this to create and manage tables. Each table has a unique id.',
|
|
144
|
-
schema: Schema.Struct({}),
|
|
145
|
-
tools: [
|
|
146
|
-
createTool('table', {
|
|
147
|
-
name: 'create',
|
|
148
|
-
description: 'Create a table',
|
|
149
|
-
schema: Schema.Struct({
|
|
150
|
-
data: Schema.Array(Schema.Any).annotations({ description: 'Array of data payloads to add as rows' }),
|
|
151
|
-
}),
|
|
152
|
-
execute: async ({ data }) => {
|
|
153
|
-
log('create table', { data });
|
|
154
|
-
const id = DXN.fromLocalObjectId(ObjectId.random()).toString();
|
|
155
|
-
objects.add(id);
|
|
156
|
-
// TODO(dmaretskyi): consider xml for refs instead of @dxn:echo:@:XXXXX
|
|
157
|
-
return ToolResult.Success(`table @${id}`);
|
|
158
|
-
},
|
|
159
|
-
}),
|
|
160
|
-
],
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
const mapArtifact = defineArtifact({
|
|
164
|
-
id: 'artifact:dxos.org/plugin/map',
|
|
165
|
-
name: 'Map',
|
|
166
|
-
instructions:
|
|
167
|
-
'Use this to create and manage maps. Maps source data from tables. Table id is required to create a map.',
|
|
168
|
-
schema: Schema.Struct({}),
|
|
169
|
-
tools: [
|
|
170
|
-
createTool('map', {
|
|
171
|
-
name: 'create',
|
|
172
|
-
description: 'Create a map',
|
|
173
|
-
schema: Schema.Struct({
|
|
174
|
-
source: ArtifactId.annotations({
|
|
175
|
-
description: 'The table that will be used as the source of the map',
|
|
176
|
-
}),
|
|
177
|
-
}),
|
|
178
|
-
execute: async ({ source }) => {
|
|
179
|
-
// TODO(dmaretskyi): Use effect-schema decode instead of manual parsing.
|
|
180
|
-
const sourceId = ArtifactId.toDXN(source);
|
|
181
|
-
if (!objects.has(sourceId.toString())) {
|
|
182
|
-
return ToolResult.Error(`table id=${source} not found`);
|
|
183
|
-
}
|
|
184
|
-
log('create map', { sourceId });
|
|
185
|
-
const id = DXN.fromLocalObjectId(ObjectId.random()).toString();
|
|
186
|
-
objects.add(id);
|
|
187
|
-
// TODO(dmaretskyi): consider xml for refs instead of @dxn:echo:@:XXXXX
|
|
188
|
-
return ToolResult.Success(`map @${id}`);
|
|
189
|
-
},
|
|
190
|
-
}),
|
|
191
|
-
],
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
const scriptArtifact = defineArtifact({
|
|
195
|
-
id: 'artifact:dxos.org/plugin/script',
|
|
196
|
-
name: 'Script',
|
|
197
|
-
instructions: 'Use this to create and manage scripts',
|
|
198
|
-
schema: Schema.Struct({}),
|
|
199
|
-
tools: [],
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
// session.streamEvent.on((event) => {
|
|
203
|
-
// printStreamEvent(event);
|
|
204
|
-
// });
|
|
205
|
-
|
|
206
|
-
const printer = new ConsolePrinter();
|
|
207
|
-
session.message.on((message) => printer.printMessage(message));
|
|
208
|
-
session.userMessage.on((message) => printer.printMessage(message));
|
|
209
|
-
session.block.on((block) => printer.printContentBlock(block));
|
|
210
|
-
|
|
211
|
-
// session.update.on((update) => {
|
|
212
|
-
// log('update', { update });
|
|
213
|
-
// });
|
|
214
|
-
|
|
215
|
-
// Test creating an itinerary
|
|
216
|
-
const response = await session.run({
|
|
217
|
-
client: aiClient,
|
|
218
|
-
tools: [],
|
|
219
|
-
artifacts: [calendarArtifact, tableArtifact, mapArtifact, scriptArtifact],
|
|
220
|
-
requiredArtifactIds: [calendarArtifact.id, tableArtifact.id, mapArtifact.id, scriptArtifact.id],
|
|
221
|
-
history: [],
|
|
222
|
-
generationOptions: {
|
|
223
|
-
model: '@anthropic/claude-3-5-haiku-20241022',
|
|
224
|
-
},
|
|
225
|
-
prompt: 'create a table and map for a travel itinerary based on events in my calendar',
|
|
226
|
-
toolResolver: new ToolRegistry([]),
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
log('result', {
|
|
230
|
-
objects,
|
|
231
|
-
finalMessage: response.at(-1),
|
|
232
|
-
});
|
|
233
|
-
});
|
|
234
|
-
*/
|
|
235
114
|
});
|
|
236
115
|
|
|
237
116
|
// Travel to rome, florence, livorno, siena, madrid for conferences
|
|
238
|
-
|
|
117
|
+
|
|
239
118
|
const _CALENDAR_EVENTS: CalendarEvent[] = [
|
|
240
119
|
Obj.make(CalendarEventSchema, {
|
|
241
120
|
title: 'Exploring Ancient Ruins in Rome',
|
|
@@ -268,43 +147,3 @@ const _CALENDAR_EVENTS: CalendarEvent[] = [
|
|
|
268
147
|
description: 'Travel to Madrid',
|
|
269
148
|
}),
|
|
270
149
|
];
|
|
271
|
-
|
|
272
|
-
// const printStreamEvent = (event: GenerationStreamEvent) => {
|
|
273
|
-
// switch (event.type) {
|
|
274
|
-
// case 'message_start': {
|
|
275
|
-
// process.stdout.write(`${event.message.role.toUpperCase()}\n\n`);
|
|
276
|
-
// for (const content of event.message.content) {
|
|
277
|
-
// printContentBlock(content);
|
|
278
|
-
// }
|
|
279
|
-
// break;
|
|
280
|
-
// }
|
|
281
|
-
// case 'content_block_start': {
|
|
282
|
-
// printContentBlock(event.content);
|
|
283
|
-
// break;
|
|
284
|
-
// }
|
|
285
|
-
// case 'content_block_delta': {
|
|
286
|
-
// switch (event.delta.type) {
|
|
287
|
-
// case 'text_delta': {
|
|
288
|
-
// process.stdout.write(event.delta.text);
|
|
289
|
-
// break;
|
|
290
|
-
// }
|
|
291
|
-
// case 'input_json_delta': {
|
|
292
|
-
// process.stdout.write(event.delta.partial_json);
|
|
293
|
-
// break;
|
|
294
|
-
// }
|
|
295
|
-
// }
|
|
296
|
-
// break;
|
|
297
|
-
// }
|
|
298
|
-
// case 'content_block_stop': {
|
|
299
|
-
// process.stdout.write('\n');
|
|
300
|
-
// break;
|
|
301
|
-
// }
|
|
302
|
-
// case 'message_delta': {
|
|
303
|
-
// break;
|
|
304
|
-
// }
|
|
305
|
-
// case 'message_stop': {
|
|
306
|
-
// process.stdout.write('\n\n');
|
|
307
|
-
// break;
|
|
308
|
-
// }
|
|
309
|
-
// }
|
|
310
|
-
// };
|