@dxos/assistant 0.8.3 → 0.8.4-main.1da679c
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-3K5SRE5A.mjs +404 -0
- package/dist/lib/browser/chunk-3K5SRE5A.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 +268 -0
- package/dist/lib/browser/extraction/index.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +412 -2101
- 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-W7AIB6DP.mjs +406 -0
- package/dist/lib/node-esm/chunk-W7AIB6DP.mjs.map +7 -0
- package/dist/lib/node-esm/extraction/index.mjs +269 -0
- package/dist/lib/node-esm/extraction/index.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +412 -2102
- 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 +42 -0
- package/dist/types/src/conversation/conversation.d.ts.map +1 -0
- package/dist/types/src/conversation/index.d.ts +3 -0
- package/dist/types/src/conversation/index.d.ts.map +1 -0
- 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/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 +177 -48
- 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 +250 -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 +7 -0
- package/dist/types/src/functions/services.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +5 -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 +182 -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 -246
- 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 +48 -32
- package/src/conversation/context.ts +154 -0
- package/src/conversation/conversation.ts +123 -0
- package/src/conversation/index.ts +6 -0
- package/src/diff.test.ts +43 -0
- package/src/diff.ts +79 -0
- package/src/errors.ts +15 -0
- package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +13 -24
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +25 -20
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +9 -9
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +15 -7
- package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +2 -2
- 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 +119 -0
- package/src/index.ts +5 -3
- package/src/session/__snapshots__/session.test.ts.snap +32 -0
- package/src/session/artifact-diff.ts +41 -0
- package/src/session/error-handling.ts +39 -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.test.ts +98 -19
- package/src/session/session.ts +159 -507
- package/src/session/toolkit.ts +42 -0
- package/src/session/version-pin.test.ts +2 -21
- package/src/session/version-pin.ts +9 -9
- package/src/summarization/summarization.test.ts +82 -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/lib/node/index.cjs +0 -2178
- package/dist/lib/node/index.cjs.map +0 -7
- package/dist/lib/node/meta.json +0 -1
- package/dist/types/bin/chat.d.ts +0 -2
- package/dist/types/bin/chat.d.ts.map +0 -1
- package/dist/types/src/blueprint/ai.test.d.ts +0 -2
- package/dist/types/src/blueprint/ai.test.d.ts.map +0 -1
- package/dist/types/src/blueprint/blueprint.d.ts +0 -55
- package/dist/types/src/blueprint/blueprint.d.ts.map +0 -1
- package/dist/types/src/blueprint/blueprint.test.d.ts +0 -2
- package/dist/types/src/blueprint/blueprint.test.d.ts.map +0 -1
- package/dist/types/src/blueprint/index.d.ts +0 -4
- package/dist/types/src/blueprint/index.d.ts.map +0 -1
- package/dist/types/src/blueprint/logger.d.ts +0 -25
- package/dist/types/src/blueprint/logger.d.ts.map +0 -1
- package/dist/types/src/blueprint/machine.d.ts +0 -143
- package/dist/types/src/blueprint/machine.d.ts.map +0 -1
- package/dist/types/src/blueprint/test-data.d.ts +0 -3
- package/dist/types/src/blueprint/test-data.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/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/ai.test.ts +0 -156
- package/src/blueprint/blueprint.test.ts +0 -151
- package/src/blueprint/blueprint.ts +0 -75
- package/src/blueprint/index.ts +0 -7
- package/src/blueprint/logger.ts +0 -155
- package/src/blueprint/machine.ts +0 -251
- package/src/blueprint/test-data.ts +0 -55
- 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 -89
- 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
|
+
});
|
|
@@ -2,18 +2,17 @@
|
|
|
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 { DXN } from '@dxos/keys';
|
|
5
|
+
import { AiTool, AiToolkit } from '@effect/ai';
|
|
6
|
+
import { describe, it } from '@effect/vitest';
|
|
7
|
+
import { Effect, Layer, Schema } from 'effect';
|
|
8
|
+
|
|
9
|
+
import { AiService, ToolExecutionService, ToolResolverService } from '@dxos/ai';
|
|
10
|
+
import { AiServiceTestingPreset } from '@dxos/ai/testing';
|
|
11
|
+
import { Obj, Type } from '@dxos/echo';
|
|
12
|
+
import { TracingService } from '@dxos/functions';
|
|
14
13
|
import { log } from '@dxos/log';
|
|
15
14
|
|
|
16
|
-
import {
|
|
15
|
+
import { AiSession } from './session';
|
|
17
16
|
|
|
18
17
|
// Define a calendar event artifact schema.
|
|
19
18
|
const CalendarEventSchema = Schema.Struct({
|
|
@@ -30,15 +29,92 @@ const CalendarEventSchema = Schema.Struct({
|
|
|
30
29
|
|
|
31
30
|
type CalendarEvent = Schema.Schema.Type<typeof CalendarEventSchema>;
|
|
32
31
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
32
|
+
class TestToolkit extends AiToolkit.make(
|
|
33
|
+
AiTool.make('Calculator', {
|
|
34
|
+
description: 'Basic calculator tool',
|
|
35
|
+
parameters: {
|
|
36
|
+
input: Schema.String.annotations({
|
|
37
|
+
description: 'The calculation to perform.',
|
|
38
|
+
}),
|
|
39
|
+
},
|
|
40
|
+
success: Schema.Struct({
|
|
41
|
+
result: Schema.Number,
|
|
42
|
+
}),
|
|
43
|
+
failure: Schema.Never,
|
|
44
|
+
}),
|
|
45
|
+
) {}
|
|
46
|
+
|
|
47
|
+
// Tool handlers.
|
|
48
|
+
const toolkitLayer = TestToolkit.toLayer({
|
|
49
|
+
Calculator: ({ input }) =>
|
|
50
|
+
Effect.gen(function* () {
|
|
51
|
+
const result = (() => {
|
|
52
|
+
// Restrict to basic arithmetic operations for safety.
|
|
53
|
+
const sanitizedInput = input.replace(/[^0-9+\-*/().\s]/g, '');
|
|
54
|
+
log.info('calculate', { sanitizedInput });
|
|
55
|
+
|
|
56
|
+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
57
|
+
return Function(`"use strict"; return (${sanitizedInput})`)();
|
|
58
|
+
})();
|
|
41
59
|
|
|
60
|
+
return { result };
|
|
61
|
+
}),
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe.runIf(process.env.DX_RUN_SLOW_TESTS)('AiSession', () => {
|
|
65
|
+
it.effect('no tools', () =>
|
|
66
|
+
Effect.gen(function* () {
|
|
67
|
+
const session = new AiSession({ operationModel: 'configured' });
|
|
68
|
+
const response = yield* session.run({
|
|
69
|
+
prompt: 'Hello world!',
|
|
70
|
+
history: [],
|
|
71
|
+
});
|
|
72
|
+
log.info('response', { response });
|
|
73
|
+
}).pipe(
|
|
74
|
+
Effect.provide(
|
|
75
|
+
Layer.mergeAll(
|
|
76
|
+
toolkitLayer,
|
|
77
|
+
AiService.model('@anthropic/claude-3-5-sonnet-20241022').pipe(
|
|
78
|
+
Layer.provideMerge(ToolResolverService.layerEmpty),
|
|
79
|
+
Layer.provideMerge(ToolExecutionService.layerEmpty),
|
|
80
|
+
Layer.provideMerge(AiServiceTestingPreset('direct')),
|
|
81
|
+
Layer.provideMerge(TracingService.layerNoop),
|
|
82
|
+
),
|
|
83
|
+
),
|
|
84
|
+
),
|
|
85
|
+
),
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
it.effect('calculator', () =>
|
|
89
|
+
Effect.gen(function* () {
|
|
90
|
+
const session = new AiSession({ operationModel: 'configured' });
|
|
91
|
+
const toolkit = yield* TestToolkit;
|
|
92
|
+
const response = yield* session.run({
|
|
93
|
+
toolkit,
|
|
94
|
+
prompt: 'What is 10 + 20?',
|
|
95
|
+
history: [],
|
|
96
|
+
});
|
|
97
|
+
log.info('response', { response });
|
|
98
|
+
}).pipe(
|
|
99
|
+
Effect.provide(
|
|
100
|
+
Layer.mergeAll(
|
|
101
|
+
TracingService.layerNoop,
|
|
102
|
+
AiService.model('@anthropic/claude-3-5-sonnet-20241022').pipe(
|
|
103
|
+
Layer.provideMerge(AiServiceTestingPreset('direct')),
|
|
104
|
+
),
|
|
105
|
+
ToolResolverService.layerEmpty,
|
|
106
|
+
ToolExecutionService.layerEmpty,
|
|
107
|
+
toolkitLayer,
|
|
108
|
+
),
|
|
109
|
+
),
|
|
110
|
+
),
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
// TODO(dmaretskyi): Revive test.
|
|
114
|
+
/*
|
|
115
|
+
it.skip('create calendar itinerary', { timeout: 60_000 }, async () => {
|
|
116
|
+
// overrides: { model: 'llama3.1:8b' },
|
|
117
|
+
const session = new AiSession({ operationModel: 'configured' });
|
|
42
118
|
const objects = new Set<string>();
|
|
43
119
|
|
|
44
120
|
// Define calendar artifact.
|
|
@@ -145,6 +221,7 @@ describe.skip('AISession with Ollama', () => {
|
|
|
145
221
|
model: '@anthropic/claude-3-5-haiku-20241022',
|
|
146
222
|
},
|
|
147
223
|
prompt: 'create a table and map for a travel itinerary based on events in my calendar',
|
|
224
|
+
toolResolver: new ToolRegistry([]),
|
|
148
225
|
});
|
|
149
226
|
|
|
150
227
|
log('result', {
|
|
@@ -152,10 +229,12 @@ describe.skip('AISession with Ollama', () => {
|
|
|
152
229
|
finalMessage: response.at(-1),
|
|
153
230
|
});
|
|
154
231
|
});
|
|
232
|
+
*/
|
|
155
233
|
});
|
|
156
234
|
|
|
157
235
|
// Travel to rome, florence, livorno, siena, madrid for conferences
|
|
158
|
-
|
|
236
|
+
|
|
237
|
+
const _CALENDAR_EVENTS: CalendarEvent[] = [
|
|
159
238
|
Obj.make(CalendarEventSchema, {
|
|
160
239
|
title: 'Exploring Ancient Ruins in Rome',
|
|
161
240
|
startTime: '2024-01-01T10:00:00Z',
|