@dxos/assistant 0.8.4-main.84f28bd → 0.8.4-main.a4bbb77
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-ILL2CN3G.mjs +374 -0
- package/dist/lib/browser/chunk-ILL2CN3G.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 +712 -1870
- 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-GVKA7VL3.mjs +376 -0
- package/dist/lib/node-esm/chunk-GVKA7VL3.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 +712 -1871
- 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 +96 -0
- package/dist/types/src/conversation/context.d.ts.map +1 -0
- package/dist/types/src/conversation/conversation.d.ts +28 -31
- 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 +75 -0
- package/dist/types/src/errors.d.ts.map +1 -0
- 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 +198 -65
- 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 +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 +254 -162
- package/dist/types/src/extraction/extraction.d.ts.map +1 -1
- package/dist/types/src/functions/index.d.ts +2 -0
- package/dist/types/src/functions/index.d.ts.map +1 -0
- package/dist/types/src/functions/services.d.ts +18 -0
- package/dist/types/src/functions/services.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +5 -5
- 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 +32 -268
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/session/toolkit.d.ts +14 -0
- package/dist/types/src/session/toolkit.d.ts.map +1 -0
- package/dist/types/src/session/version-pin.d.ts +4 -4
- package/dist/types/src/session/version-pin.d.ts.map +1 -1
- package/dist/types/src/templates/index.d.ts +2 -0
- package/dist/types/src/templates/index.d.ts.map +1 -0
- 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 +13 -0
- package/dist/types/src/types.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +50 -24
- package/src/conversation/context.ts +154 -0
- package/src/conversation/conversation.ts +89 -73
- package/src/conversation/index.ts +1 -0
- package/src/diff.test.ts +43 -0
- package/src/diff.ts +79 -0
- package/src/errors.ts +15 -0
- 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 +13 -24
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +27 -21
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +9 -9
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +17 -7
- package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +4 -3
- package/src/extraction/extraction-ner-function/named-entity-recognition.ts +3 -1
- package/src/extraction/quotes.test.ts +3 -3
- package/src/extraction/quotes.ts +2 -2
- package/src/functions/index.ts +5 -0
- package/src/functions/services.ts +153 -0
- package/src/index.ts +5 -5
- package/src/session/artifact-diff.ts +41 -0
- package/src/session/error-handling.ts +40 -0
- package/src/session/format.test.ts +60 -0
- package/src/session/format.ts +139 -0
- package/src/session/index.ts +3 -0
- package/src/session/observer.ts +58 -0
- package/src/session/session.conversations.json +1 -0
- package/src/session/session.test.ts +86 -215
- package/src/session/session.ts +163 -539
- package/src/session/toolkit.ts +46 -0
- package/src/session/version-pin.ts +9 -9
- package/src/summarization/summarization.test.ts +86 -106
- package/src/templates/format.tpl +60 -0
- package/src/templates/index.ts +5 -0
- package/src/templates/system.tpl +48 -0
- package/src/templates/system.ts +41 -0
- package/src/types.ts +45 -1
- 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/blueprint/blueprint.d.ts +0 -41
- package/dist/types/src/blueprint/blueprint.d.ts.map +0 -1
- package/dist/types/src/blueprint/index.d.ts +0 -3
- package/dist/types/src/blueprint/index.d.ts.map +0 -1
- package/dist/types/src/blueprint/registry.d.ts +0 -7
- package/dist/types/src/blueprint/registry.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 -38
- 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 -7
- package/dist/types/src/research/exa.d.ts.map +0 -1
- package/dist/types/src/research/graph.d.ts +0 -41
- 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/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/blueprint/blueprint.ts +0 -75
- package/src/blueprint/index.ts +0 -6
- package/src/blueprint/registry.ts +0 -15
- package/src/context/binder.ts +0 -101
- package/src/context/binding.ts +0 -31
- package/src/context/index.ts +0 -6
- package/src/research/exa.ts +0 -100
- package/src/research/graph.test.ts +0 -15
- package/src/research/graph.ts +0 -337
- package/src/research/index.ts +0 -7
- package/src/research/instructions-research.tpl +0 -97
- package/src/research/research.test.ts +0 -190
- package/src/research/research.ts +0 -90
- package/src/session/version-pin.test.ts +0 -26
- 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,139 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Array, Effect, Option, pipe } from 'effect';
|
|
6
|
+
|
|
7
|
+
import { Template } from '@dxos/blueprints';
|
|
8
|
+
import { Obj } from '@dxos/echo';
|
|
9
|
+
import { ObjectVersion } from '@dxos/echo-db';
|
|
10
|
+
import { type ObjectId } from '@dxos/echo-schema';
|
|
11
|
+
import { DatabaseService } from '@dxos/functions';
|
|
12
|
+
import { log } from '@dxos/log';
|
|
13
|
+
import { type ContentBlock, DataType } from '@dxos/schema';
|
|
14
|
+
import { trim } from '@dxos/util';
|
|
15
|
+
|
|
16
|
+
import { AiAssistantError } from '../errors';
|
|
17
|
+
|
|
18
|
+
import { ArtifactDiffResolver } from './artifact-diff';
|
|
19
|
+
import { type AiSessionRunParams } from './session';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Formats the system prompt.
|
|
23
|
+
*/
|
|
24
|
+
// TODO(burdon): Move to AiPreprocessor.
|
|
25
|
+
export const formatSystemPrompt = ({
|
|
26
|
+
system,
|
|
27
|
+
blueprints = [],
|
|
28
|
+
objects = [],
|
|
29
|
+
}: Pick<AiSessionRunParams<any>, 'system' | 'blueprints' | 'objects'>) =>
|
|
30
|
+
Effect.gen(function* () {
|
|
31
|
+
const blueprintDefs = yield* pipe(
|
|
32
|
+
blueprints,
|
|
33
|
+
Effect.forEach((blueprint) => Effect.succeed(blueprint.instructions)),
|
|
34
|
+
Effect.flatMap(Effect.forEach((template) => DatabaseService.loadOption(template.source))),
|
|
35
|
+
Effect.map(Array.filter(Option.isSome)),
|
|
36
|
+
Effect.map(
|
|
37
|
+
Array.map(
|
|
38
|
+
(template) => trim`
|
|
39
|
+
<blueprint>
|
|
40
|
+
${Template.process(template.value.content)}
|
|
41
|
+
</blueprint>
|
|
42
|
+
`,
|
|
43
|
+
),
|
|
44
|
+
),
|
|
45
|
+
Effect.map((blueprints) =>
|
|
46
|
+
blueprints.length > 0 ? ['## Blueprints Definitions', ...blueprints].join('\n\n') : undefined,
|
|
47
|
+
),
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
const objectDefs = yield* pipe(
|
|
51
|
+
objects,
|
|
52
|
+
Effect.forEach((object) =>
|
|
53
|
+
Effect.succeed(trim`
|
|
54
|
+
<object>
|
|
55
|
+
<dxn>${Obj.getDXN(object)}</dxn>
|
|
56
|
+
<typename>${Obj.getTypename(object)}</typename>
|
|
57
|
+
</object>
|
|
58
|
+
`),
|
|
59
|
+
),
|
|
60
|
+
Effect.map((objects) => (objects.length > 0 ? ['## Context Objects', ...objects].join('\n\n') : undefined)),
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
return yield* pipe(
|
|
64
|
+
Effect.succeed([system, blueprintDefs, objectDefs].filter((def): def is string => def !== undefined)),
|
|
65
|
+
Effect.map((parts) => parts.join('\n\n')),
|
|
66
|
+
);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Formats the user prompt.
|
|
71
|
+
*/
|
|
72
|
+
// TODO(burdon): Move to AiPreprocessor.
|
|
73
|
+
// TODO(burdon): Convert util below to `Effect.fn` (to preserve stack info)
|
|
74
|
+
export const formatUserPrompt = ({ prompt, history = [] }: Pick<AiSessionRunParams<any>, 'prompt' | 'history'>) =>
|
|
75
|
+
Effect.gen(function* () {
|
|
76
|
+
const blocks: ContentBlock.Any[] = [];
|
|
77
|
+
|
|
78
|
+
// TODO(dmaretskyi): Evaluate other approaches as `serviceOption` isn't represented in the type system.
|
|
79
|
+
const artifactDiffResolver = yield* Effect.serviceOption(ArtifactDiffResolver);
|
|
80
|
+
if (Option.isSome(artifactDiffResolver)) {
|
|
81
|
+
const versions = gatherObjectVersions(history);
|
|
82
|
+
const artifactDiff = yield* Effect.tryPromise({
|
|
83
|
+
try: () =>
|
|
84
|
+
artifactDiffResolver.value.resolve(
|
|
85
|
+
[...versions.entries()].map(([id, version]) => ({ id, lastVersion: version })),
|
|
86
|
+
),
|
|
87
|
+
catch: AiAssistantError.wrap({ message: 'Artifact diff resolution error' }),
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
log('version', { artifactDiff, versions });
|
|
91
|
+
for (const [id, { version }] of [...artifactDiff.entries()]) {
|
|
92
|
+
if (ObjectVersion.equals(version, versions.get(id)!)) {
|
|
93
|
+
artifactDiff.delete(id);
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
blocks.push({ _tag: 'anchor', objectId: id, version });
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (artifactDiff.size > 0) {
|
|
101
|
+
blocks.push(createArtifactUpdateBlock(artifactDiff));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return Obj.make(DataType.Message, {
|
|
106
|
+
created: new Date().toISOString(),
|
|
107
|
+
sender: { role: 'user' },
|
|
108
|
+
blocks: [...blocks, { _tag: 'text', text: prompt }],
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
const gatherObjectVersions = (messages: DataType.Message[]): Map<ObjectId, ObjectVersion> => {
|
|
113
|
+
const artifactIds = new Map<ObjectId, ObjectVersion>();
|
|
114
|
+
for (const message of messages) {
|
|
115
|
+
for (const block of message.blocks) {
|
|
116
|
+
if (block._tag === 'anchor') {
|
|
117
|
+
artifactIds.set(block.objectId, block.version as ObjectVersion);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return artifactIds;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const createArtifactUpdateBlock = (
|
|
126
|
+
artifactDiff: Map<ObjectId, { version: ObjectVersion; diff?: string }>,
|
|
127
|
+
): ContentBlock.Any => {
|
|
128
|
+
return {
|
|
129
|
+
_tag: 'text',
|
|
130
|
+
// TODO(dmaretskyi): Does this need to be a special content-block?
|
|
131
|
+
disposition: 'artifact-update',
|
|
132
|
+
text: trim`
|
|
133
|
+
The following artifacts have been updated since the last message:
|
|
134
|
+
${[...artifactDiff.entries()]
|
|
135
|
+
.map(([id, { diff }]) => `<changed-artifact id="${id}">${diff ? `\n${diff}` : ''}</changed-artifact>`)
|
|
136
|
+
.join('\n')}
|
|
137
|
+
`,
|
|
138
|
+
};
|
|
139
|
+
};
|
package/src/session/index.ts
CHANGED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Effect, Function } from 'effect';
|
|
6
|
+
|
|
7
|
+
import { type AiParser, type ConsolePrinter } from '@dxos/ai';
|
|
8
|
+
import { type DataType } from '@dxos/schema';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Live observer of the generation process.
|
|
12
|
+
*/
|
|
13
|
+
export interface GenerationObserver extends AiParser.ParseResponseCallbacks {
|
|
14
|
+
/**
|
|
15
|
+
* Complete messages fired during the session, both from the model and from the user.
|
|
16
|
+
* This message will contain all message blocks emitted through the `onBlock` callback.
|
|
17
|
+
*/
|
|
18
|
+
onMessage: (message: DataType.Message) => Effect.Effect<void>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const GenerationObserver = Object.freeze({
|
|
22
|
+
make: ({
|
|
23
|
+
onBegin = Function.constant(Effect.void),
|
|
24
|
+
onPart = Function.constant(Effect.void),
|
|
25
|
+
onBlock = Function.constant(Effect.void),
|
|
26
|
+
onEnd = Function.constant(Effect.void),
|
|
27
|
+
onMessage = Function.constant(Effect.void),
|
|
28
|
+
}: Partial<GenerationObserver> = {}): GenerationObserver => ({
|
|
29
|
+
onBegin,
|
|
30
|
+
onPart,
|
|
31
|
+
onBlock,
|
|
32
|
+
onEnd,
|
|
33
|
+
onMessage,
|
|
34
|
+
}),
|
|
35
|
+
|
|
36
|
+
noop: () => GenerationObserver.make(),
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Debug printer to be used in unit-tests and browser devtools.
|
|
40
|
+
*/
|
|
41
|
+
fromPrinter: (printer: ConsolePrinter) =>
|
|
42
|
+
GenerationObserver.make({
|
|
43
|
+
onBlock: (block) =>
|
|
44
|
+
Effect.sync(() => {
|
|
45
|
+
if (block.pending) {
|
|
46
|
+
return; // Only prints full blocks (better for unit-tests and browser devtools).
|
|
47
|
+
}
|
|
48
|
+
printer.printContentBlock(block);
|
|
49
|
+
}),
|
|
50
|
+
onMessage: (message) =>
|
|
51
|
+
Effect.sync(() => {
|
|
52
|
+
if (message.sender.role === 'assistant') {
|
|
53
|
+
return; // Skip assistant messages since they are printed in the `onBlock` callback.
|
|
54
|
+
}
|
|
55
|
+
printer.printMessage(message);
|
|
56
|
+
}),
|
|
57
|
+
}),
|
|
58
|
+
});
|
|
@@ -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,18 +2,18 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import { describe,
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
5
|
+
import { Tool, Toolkit } from '@effect/ai';
|
|
6
|
+
import { describe, it } from '@effect/vitest';
|
|
7
|
+
import { Effect, Layer, Schema } from 'effect';
|
|
8
|
+
|
|
9
|
+
import { AiService, MemoizedAiService, ToolExecutionService, ToolResolverService } from '@dxos/ai';
|
|
10
|
+
import { AiServiceTestingPreset } from '@dxos/ai/testing';
|
|
11
|
+
import { Obj, Type } from '@dxos/echo';
|
|
12
|
+
import { TestHelpers } from '@dxos/effect';
|
|
13
|
+
import { TracingService } from '@dxos/functions';
|
|
14
14
|
import { log } from '@dxos/log';
|
|
15
15
|
|
|
16
|
-
import {
|
|
16
|
+
import { AiSession } from './session';
|
|
17
17
|
|
|
18
18
|
// Define a calendar event artifact schema.
|
|
19
19
|
const CalendarEventSchema = Schema.Struct({
|
|
@@ -30,178 +30,89 @@ const CalendarEventSchema = Schema.Struct({
|
|
|
30
30
|
|
|
31
31
|
type CalendarEvent = Schema.Schema.Type<typeof CalendarEventSchema>;
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
// });
|
|
40
|
-
const session = new AISession({ operationModel: 'configured' });
|
|
41
|
-
|
|
42
|
-
const printer = new ConsolePrinter();
|
|
43
|
-
session.message.on((message) => printer.printMessage(message));
|
|
44
|
-
session.userMessage.on((message) => printer.printMessage(message));
|
|
45
|
-
session.block.on((block) => printer.printContentBlock(block));
|
|
46
|
-
|
|
47
|
-
// session.update.on((update) => {
|
|
48
|
-
// log('update', { update });
|
|
49
|
-
// });
|
|
50
|
-
|
|
51
|
-
const calculatorTool = createTool('test', {
|
|
52
|
-
name: 'calculator',
|
|
53
|
-
description: 'Adds to numbers',
|
|
54
|
-
schema: Schema.Struct({
|
|
55
|
-
operand1: Schema.Number,
|
|
56
|
-
operand2: Schema.Number,
|
|
33
|
+
class TestToolkit extends Toolkit.make(
|
|
34
|
+
Tool.make('Calculator', {
|
|
35
|
+
description: 'Basic calculator tool',
|
|
36
|
+
parameters: {
|
|
37
|
+
input: Schema.String.annotations({
|
|
38
|
+
description: 'The calculation to perform.',
|
|
57
39
|
}),
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
})
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
// const aiClient = new OllamaAiServiceClient({
|
|
83
|
-
// overrides: { model: 'llama3.1:8b' },
|
|
84
|
-
// });
|
|
85
|
-
const session = new AISession({ operationModel: 'configured' });
|
|
86
|
-
|
|
87
|
-
const objects = new Set<string>();
|
|
88
|
-
|
|
89
|
-
// Define calendar artifact.
|
|
90
|
-
const calendarArtifact = defineArtifact({
|
|
91
|
-
id: 'artifact:dxos.org/plugin/calendar',
|
|
92
|
-
name: 'Calendar',
|
|
93
|
-
instructions: 'Use this to create and query calendar events.',
|
|
94
|
-
schema: CalendarEventSchema,
|
|
95
|
-
tools: [
|
|
96
|
-
createTool('calendar', {
|
|
97
|
-
name: 'query',
|
|
98
|
-
description: 'Query the calendar for events',
|
|
99
|
-
schema: Schema.Struct({}),
|
|
100
|
-
execute: async () => {
|
|
101
|
-
return ToolResult.Success(CALENDAR_EVENTS);
|
|
102
|
-
},
|
|
103
|
-
}),
|
|
104
|
-
],
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
const tableArtifact = defineArtifact({
|
|
108
|
-
id: 'artifact:dxos.org/plugin/table',
|
|
109
|
-
name: 'Table',
|
|
110
|
-
instructions: 'Use this to create and manage tables. Each table has a unique id.',
|
|
111
|
-
schema: Schema.Struct({}),
|
|
112
|
-
tools: [
|
|
113
|
-
createTool('table', {
|
|
114
|
-
name: 'create',
|
|
115
|
-
description: 'Create a table',
|
|
116
|
-
schema: Schema.Struct({
|
|
117
|
-
data: Schema.Array(Schema.Any).annotations({ description: 'Array of data payloads to add as rows' }),
|
|
118
|
-
}),
|
|
119
|
-
execute: async ({ data }) => {
|
|
120
|
-
log('create table', { data });
|
|
121
|
-
const id = DXN.fromLocalObjectId(ObjectId.random()).toString();
|
|
122
|
-
objects.add(id);
|
|
123
|
-
// TODO(dmaretskyi): consider xml for refs instead of @dxn:echo:@:XXXXX
|
|
124
|
-
return ToolResult.Success(`table @${id}`);
|
|
125
|
-
},
|
|
126
|
-
}),
|
|
127
|
-
],
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
const mapArtifact = defineArtifact({
|
|
131
|
-
id: 'artifact:dxos.org/plugin/map',
|
|
132
|
-
name: 'Map',
|
|
133
|
-
instructions:
|
|
134
|
-
'Use this to create and manage maps. Maps source data from tables. Table id is required to create a map.',
|
|
135
|
-
schema: Schema.Struct({}),
|
|
136
|
-
tools: [
|
|
137
|
-
createTool('map', {
|
|
138
|
-
name: 'create',
|
|
139
|
-
description: 'Create a map',
|
|
140
|
-
schema: Schema.Struct({
|
|
141
|
-
source: ArtifactId.annotations({
|
|
142
|
-
description: 'The table that will be used as the source of the map',
|
|
143
|
-
}),
|
|
144
|
-
}),
|
|
145
|
-
execute: async ({ source }) => {
|
|
146
|
-
// TODO(dmaretskyi): Use effect-schema decode instead of manual parsing.
|
|
147
|
-
const sourceId = ArtifactId.toDXN(source);
|
|
148
|
-
if (!objects.has(sourceId.toString())) {
|
|
149
|
-
return ToolResult.Error(`table id=${source} not found`);
|
|
150
|
-
}
|
|
151
|
-
log('create map', { sourceId });
|
|
152
|
-
const id = DXN.fromLocalObjectId(ObjectId.random()).toString();
|
|
153
|
-
objects.add(id);
|
|
154
|
-
// TODO(dmaretskyi): consider xml for refs instead of @dxn:echo:@:XXXXX
|
|
155
|
-
return ToolResult.Success(`map @${id}`);
|
|
156
|
-
},
|
|
157
|
-
}),
|
|
158
|
-
],
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
const scriptArtifact = defineArtifact({
|
|
162
|
-
id: 'artifact:dxos.org/plugin/script',
|
|
163
|
-
name: 'Script',
|
|
164
|
-
instructions: 'Use this to create and manage scripts',
|
|
165
|
-
schema: Schema.Struct({}),
|
|
166
|
-
tools: [],
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
// session.streamEvent.on((event) => {
|
|
170
|
-
// printStreamEvent(event);
|
|
171
|
-
// });
|
|
172
|
-
|
|
173
|
-
const printer = new ConsolePrinter();
|
|
174
|
-
session.message.on((message) => printer.printMessage(message));
|
|
175
|
-
session.userMessage.on((message) => printer.printMessage(message));
|
|
176
|
-
session.block.on((block) => printer.printContentBlock(block));
|
|
40
|
+
},
|
|
41
|
+
success: Schema.Struct({
|
|
42
|
+
result: Schema.Number,
|
|
43
|
+
}),
|
|
44
|
+
failure: Schema.Never,
|
|
45
|
+
}),
|
|
46
|
+
) {}
|
|
47
|
+
|
|
48
|
+
// Tool handlers.
|
|
49
|
+
const toolkitLayer = TestToolkit.toLayer({
|
|
50
|
+
Calculator: ({ input }) =>
|
|
51
|
+
Effect.gen(function* () {
|
|
52
|
+
const result = (() => {
|
|
53
|
+
// Restrict to basic arithmetic operations for safety.
|
|
54
|
+
const sanitizedInput = input.replace(/[^0-9+\-*/().\s]/g, '');
|
|
55
|
+
log.info('calculate', { sanitizedInput });
|
|
56
|
+
|
|
57
|
+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
58
|
+
return Function(`"use strict"; return (${sanitizedInput})`)();
|
|
59
|
+
})();
|
|
60
|
+
|
|
61
|
+
return { result };
|
|
62
|
+
}),
|
|
63
|
+
});
|
|
177
64
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
65
|
+
const TestLayer = Layer.mergeAll(
|
|
66
|
+
toolkitLayer,
|
|
67
|
+
AiService.model('@anthropic/claude-3-5-sonnet-20241022'),
|
|
68
|
+
TracingService.layerNoop,
|
|
69
|
+
ToolResolverService.layerEmpty,
|
|
70
|
+
ToolExecutionService.layerEmpty,
|
|
71
|
+
).pipe(
|
|
72
|
+
//
|
|
73
|
+
Layer.provideMerge(MemoizedAiService.layerTest()),
|
|
74
|
+
Layer.provide(AiServiceTestingPreset('direct')),
|
|
75
|
+
);
|
|
181
76
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
77
|
+
describe('AiSession', () => {
|
|
78
|
+
it.effect(
|
|
79
|
+
'no tools',
|
|
80
|
+
Effect.fnUntraced(
|
|
81
|
+
function* (_) {
|
|
82
|
+
const session = new AiSession({ operationModel: 'configured' });
|
|
83
|
+
const response = yield* session.run({
|
|
84
|
+
prompt: 'Hello world!',
|
|
85
|
+
history: [],
|
|
86
|
+
});
|
|
87
|
+
log.info('response', { response });
|
|
191
88
|
},
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
89
|
+
Effect.provide(TestLayer),
|
|
90
|
+
TestHelpers.provideTestContext,
|
|
91
|
+
),
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
it.effect(
|
|
95
|
+
'calculator',
|
|
96
|
+
Effect.fnUntraced(
|
|
97
|
+
function* (_) {
|
|
98
|
+
const session = new AiSession({ operationModel: 'configured' });
|
|
99
|
+
const toolkit = yield* TestToolkit;
|
|
100
|
+
const response = yield* session.run({
|
|
101
|
+
toolkit,
|
|
102
|
+
prompt: 'What is 10 + 30?',
|
|
103
|
+
history: [],
|
|
104
|
+
});
|
|
105
|
+
log.info('response', { response });
|
|
106
|
+
},
|
|
107
|
+
Effect.provide(TestLayer),
|
|
108
|
+
TestHelpers.provideTestContext,
|
|
109
|
+
),
|
|
110
|
+
);
|
|
201
111
|
});
|
|
202
112
|
|
|
203
113
|
// Travel to rome, florence, livorno, siena, madrid for conferences
|
|
204
|
-
|
|
114
|
+
|
|
115
|
+
const _CALENDAR_EVENTS: CalendarEvent[] = [
|
|
205
116
|
Obj.make(CalendarEventSchema, {
|
|
206
117
|
title: 'Exploring Ancient Ruins in Rome',
|
|
207
118
|
startTime: '2024-01-01T10:00:00Z',
|
|
@@ -233,43 +144,3 @@ const CALENDAR_EVENTS: CalendarEvent[] = [
|
|
|
233
144
|
description: 'Travel to Madrid',
|
|
234
145
|
}),
|
|
235
146
|
];
|
|
236
|
-
|
|
237
|
-
// const printStreamEvent = (event: GenerationStreamEvent) => {
|
|
238
|
-
// switch (event.type) {
|
|
239
|
-
// case 'message_start': {
|
|
240
|
-
// process.stdout.write(`${event.message.role.toUpperCase()}\n\n`);
|
|
241
|
-
// for (const content of event.message.content) {
|
|
242
|
-
// printContentBlock(content);
|
|
243
|
-
// }
|
|
244
|
-
// break;
|
|
245
|
-
// }
|
|
246
|
-
// case 'content_block_start': {
|
|
247
|
-
// printContentBlock(event.content);
|
|
248
|
-
// break;
|
|
249
|
-
// }
|
|
250
|
-
// case 'content_block_delta': {
|
|
251
|
-
// switch (event.delta.type) {
|
|
252
|
-
// case 'text_delta': {
|
|
253
|
-
// process.stdout.write(event.delta.text);
|
|
254
|
-
// break;
|
|
255
|
-
// }
|
|
256
|
-
// case 'input_json_delta': {
|
|
257
|
-
// process.stdout.write(event.delta.partial_json);
|
|
258
|
-
// break;
|
|
259
|
-
// }
|
|
260
|
-
// }
|
|
261
|
-
// break;
|
|
262
|
-
// }
|
|
263
|
-
// case 'content_block_stop': {
|
|
264
|
-
// process.stdout.write('\n');
|
|
265
|
-
// break;
|
|
266
|
-
// }
|
|
267
|
-
// case 'message_delta': {
|
|
268
|
-
// break;
|
|
269
|
-
// }
|
|
270
|
-
// case 'message_stop': {
|
|
271
|
-
// process.stdout.write('\n\n');
|
|
272
|
-
// break;
|
|
273
|
-
// }
|
|
274
|
-
// }
|
|
275
|
-
// };
|