@dxos/assistant 0.8.4-main.b97322e → 0.8.4-main.bc674ce
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-7CDDG6LL.mjs +578 -0
- package/dist/lib/browser/chunk-7CDDG6LL.mjs.map +7 -0
- package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
- package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +7 -0
- package/dist/lib/browser/extraction/index.mjs +225 -0
- package/dist/lib/browser/extraction/index.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +723 -1704
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +32 -0
- package/dist/lib/browser/testing/index.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-CC7ER6Q3.mjs +579 -0
- package/dist/lib/node-esm/chunk-CC7ER6Q3.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
- package/dist/lib/node-esm/extraction/index.mjs +226 -0
- package/dist/lib/node-esm/extraction/index.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +723 -1705
- 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 +33 -0
- package/dist/lib/node-esm/testing/index.mjs.map +7 -0
- package/dist/types/src/conversation/context.d.ts +129 -0
- package/dist/types/src/conversation/context.d.ts.map +1 -0
- package/dist/types/src/conversation/context.test.d.ts +2 -0
- package/dist/types/src/conversation/context.test.d.ts.map +1 -0
- package/dist/types/src/conversation/conversation.d.ts +29 -26
- package/dist/types/src/conversation/conversation.d.ts.map +1 -1
- package/dist/types/src/conversation/conversation.test.d.ts +2 -0
- package/dist/types/src/conversation/conversation.test.d.ts.map +1 -0
- 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 +47 -18
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/execution-graph.d.ts +85 -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 +258 -213
- 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 +345 -270
- package/dist/types/src/extraction/extraction.d.ts.map +1 -1
- package/dist/types/src/extraction/quotes.d.ts +3 -3
- package/dist/types/src/extraction/quotes.d.ts.map +1 -1
- package/dist/types/src/functions/services.d.ts +18 -4
- package/dist/types/src/functions/services.d.ts.map +1 -1
- package/dist/types/src/functions.test.d.ts +2 -0
- package/dist/types/src/functions.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +3 -3
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/session/GenericToolkit.d.ts +85 -0
- package/dist/types/src/session/GenericToolkit.d.ts.map +1 -0
- 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 +12 -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 +4 -0
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/observer.d.ts +23 -0
- package/dist/types/src/session/observer.d.ts.map +1 -0
- package/dist/types/src/session/session.d.ts +33 -237
- 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 +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/testing/index.d.ts +1 -2
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/layer.d.ts +21 -0
- package/dist/types/src/testing/layer.d.ts.map +1 -0
- package/dist/types/src/types/artifact.d.ts +28 -0
- package/dist/types/src/types/artifact.d.ts.map +1 -0
- package/dist/types/src/types/index.d.ts +2 -0
- package/dist/types/src/types/index.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +56 -33
- package/src/conversation/context.test.ts +46 -0
- package/src/conversation/context.ts +303 -0
- package/src/conversation/conversation.test.ts +65 -0
- package/src/conversation/conversation.ts +93 -64
- package/src/conversation/index.ts +1 -0
- package/src/diff.test.ts +43 -0
- package/src/diff.ts +84 -0
- package/src/errors.ts +6 -1
- package/src/execution-graph.test.ts +740 -0
- package/src/execution-graph.ts +509 -0
- package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +10 -9
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +48 -56
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +22 -21
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +42 -43
- package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +12 -6
- package/src/extraction/extraction-ner-function/named-entity-recognition.ts +2 -0
- package/src/extraction/extraction.ts +10 -9
- package/src/extraction/quotes.test.ts +3 -3
- package/src/extraction/quotes.ts +6 -7
- package/src/functions/services.ts +138 -97
- package/src/functions.conversations.json +1 -0
- package/src/functions.test.ts +74 -0
- package/src/index.ts +3 -3
- package/src/session/GenericToolkit.ts +131 -0
- package/src/session/artifact-diff.ts +41 -0
- package/src/session/error-handling.ts +41 -0
- package/src/session/format.test.ts +124 -0
- package/src/session/format.ts +143 -0
- package/src/session/index.ts +5 -0
- package/src/session/observer.ts +61 -0
- package/src/session/session.conversations.json +1 -0
- package/src/session/session.test.ts +34 -201
- package/src/session/session.ts +146 -248
- package/src/session/toolkit.ts +39 -0
- package/src/session/version-pin.ts +8 -8
- package/src/templates/index.ts +1 -1
- package/src/templates/instructions/format.tpl +61 -0
- package/src/templates/instructions/system.tpl +48 -0
- package/src/templates/system.ts +41 -0
- package/src/testing/index.ts +2 -8
- package/src/testing/layer.ts +78 -0
- package/src/types/artifact.ts +82 -0
- package/src/types/index.ts +5 -0
- 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/summarization/summarization.test.d.ts +0 -2
- package/dist/types/src/summarization/summarization.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/types.d.ts +0 -13
- package/dist/types/src/types.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/summarization/summarization.test.ts +0 -208
- 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/typedefs.d.ts +0 -8
- package/src/types.ts +0 -47
- /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { describe, expect, it } from '@effect/vitest';
|
|
6
|
+
import * as Effect from 'effect/Effect';
|
|
7
|
+
import * as Schema from 'effect/Schema';
|
|
8
|
+
|
|
9
|
+
import { Blueprint, Template } from '@dxos/blueprints';
|
|
10
|
+
import { Database, Obj, Query } from '@dxos/echo';
|
|
11
|
+
import { TestHelpers } from '@dxos/effect/testing';
|
|
12
|
+
import { defineFunction } from '@dxos/functions';
|
|
13
|
+
import { Text } from '@dxos/schema';
|
|
14
|
+
import { Organization } from '@dxos/types';
|
|
15
|
+
import { trim } from '@dxos/util';
|
|
16
|
+
|
|
17
|
+
import { createSystemPrompt } from '../templates/system';
|
|
18
|
+
import { AssistantTestLayer } from '../testing';
|
|
19
|
+
|
|
20
|
+
import { formatSystemPrompt } from './format';
|
|
21
|
+
|
|
22
|
+
const organizationListFunction = defineFunction({
|
|
23
|
+
key: 'dxos.org/function/organization-list',
|
|
24
|
+
name: 'Organization List',
|
|
25
|
+
description: 'List organizations',
|
|
26
|
+
inputSchema: Schema.Struct({}),
|
|
27
|
+
outputSchema: Schema.Array(Schema.String),
|
|
28
|
+
handler: Effect.fnUntraced(function* () {
|
|
29
|
+
const organizations = yield* Database.Service.runQuery(Query.type(Organization.Organization));
|
|
30
|
+
return organizations.map((organization) => organization.name);
|
|
31
|
+
}),
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const TestLayer = AssistantTestLayer({
|
|
35
|
+
types: [Text.Text, Organization.Organization, Blueprint.Blueprint],
|
|
36
|
+
functions: [organizationListFunction],
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe('format', () => {
|
|
40
|
+
it.effect(
|
|
41
|
+
'should format',
|
|
42
|
+
Effect.fnUntraced(
|
|
43
|
+
function* (_) {
|
|
44
|
+
const { db } = yield* Database.Service;
|
|
45
|
+
const object = db.add(
|
|
46
|
+
Obj.make(Organization.Organization, {
|
|
47
|
+
name: 'Test',
|
|
48
|
+
website: 'https://www.test.com',
|
|
49
|
+
}),
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
const blueprint = db.add(
|
|
53
|
+
Blueprint.make({
|
|
54
|
+
key: 'example.com/blueprint/test',
|
|
55
|
+
name: 'Test',
|
|
56
|
+
instructions: Template.make({
|
|
57
|
+
source: trim`
|
|
58
|
+
Test
|
|
59
|
+
This is the test blueprint.
|
|
60
|
+
`,
|
|
61
|
+
}),
|
|
62
|
+
}),
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
const output = yield* formatSystemPrompt({
|
|
66
|
+
system: createSystemPrompt({}),
|
|
67
|
+
blueprints: [blueprint],
|
|
68
|
+
objects: [object],
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
console.log(output);
|
|
72
|
+
},
|
|
73
|
+
Effect.provide(TestLayer),
|
|
74
|
+
TestHelpers.provideTestContext,
|
|
75
|
+
),
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
it.effect(
|
|
79
|
+
'should format with function input',
|
|
80
|
+
Effect.fnUntraced(
|
|
81
|
+
function* (_) {
|
|
82
|
+
const { db } = yield* Database.Service;
|
|
83
|
+
const object = db.add(
|
|
84
|
+
Obj.make(Organization.Organization, {
|
|
85
|
+
name: 'Test Org',
|
|
86
|
+
website: 'https://www.test.com',
|
|
87
|
+
}),
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
const blueprint = db.add(
|
|
91
|
+
Blueprint.make({
|
|
92
|
+
key: 'example.com/blueprint/test',
|
|
93
|
+
name: 'Test',
|
|
94
|
+
instructions: Template.make({
|
|
95
|
+
source: trim`
|
|
96
|
+
Organization List:
|
|
97
|
+
{{#each organizations}}
|
|
98
|
+
- {{this}}
|
|
99
|
+
{{/each}}
|
|
100
|
+
`,
|
|
101
|
+
inputs: [
|
|
102
|
+
{
|
|
103
|
+
name: 'organizations',
|
|
104
|
+
kind: 'function',
|
|
105
|
+
function: organizationListFunction.key,
|
|
106
|
+
},
|
|
107
|
+
],
|
|
108
|
+
}),
|
|
109
|
+
}),
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
const output = yield* formatSystemPrompt({
|
|
113
|
+
system: createSystemPrompt({}),
|
|
114
|
+
blueprints: [blueprint],
|
|
115
|
+
objects: [object],
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
expect(output).to.include('Test Org');
|
|
119
|
+
},
|
|
120
|
+
Effect.provide(TestLayer),
|
|
121
|
+
TestHelpers.provideTestContext,
|
|
122
|
+
),
|
|
123
|
+
);
|
|
124
|
+
});
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Effect from 'effect/Effect';
|
|
6
|
+
import * as Function from 'effect/Function';
|
|
7
|
+
import * as Option from 'effect/Option';
|
|
8
|
+
|
|
9
|
+
import { Template } from '@dxos/blueprints';
|
|
10
|
+
import { Obj } from '@dxos/echo';
|
|
11
|
+
import { ObjectVersion } from '@dxos/echo-db';
|
|
12
|
+
import { type ObjectId } from '@dxos/keys';
|
|
13
|
+
import { log } from '@dxos/log';
|
|
14
|
+
import { type ContentBlock, Message } from '@dxos/types';
|
|
15
|
+
import { trim } from '@dxos/util';
|
|
16
|
+
|
|
17
|
+
import { AiAssistantError } from '../errors';
|
|
18
|
+
|
|
19
|
+
import { ArtifactDiffResolver } from './artifact-diff';
|
|
20
|
+
import { type AiSessionRunError, type AiSessionRunProps } from './session';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Formats the system prompt.
|
|
24
|
+
*/
|
|
25
|
+
// TODO(burdon): Move to AiPreprocessor.
|
|
26
|
+
export const formatSystemPrompt = ({
|
|
27
|
+
system,
|
|
28
|
+
blueprints = [],
|
|
29
|
+
objects = [],
|
|
30
|
+
}: Pick<AiSessionRunProps<any>, 'system' | 'blueprints' | 'objects'>) =>
|
|
31
|
+
Effect.gen(function* () {
|
|
32
|
+
const blueprintDefs = yield* Function.pipe(
|
|
33
|
+
blueprints,
|
|
34
|
+
Effect.forEach((blueprint) => Effect.succeed(blueprint.instructions)),
|
|
35
|
+
Effect.flatMap(
|
|
36
|
+
Effect.forEach((template) =>
|
|
37
|
+
Effect.gen(function* () {
|
|
38
|
+
return trim`
|
|
39
|
+
<blueprint>
|
|
40
|
+
${yield* Template.processTemplate(template)}
|
|
41
|
+
</blueprint>
|
|
42
|
+
`;
|
|
43
|
+
}),
|
|
44
|
+
),
|
|
45
|
+
),
|
|
46
|
+
Effect.map((blueprints) =>
|
|
47
|
+
blueprints.length > 0 ? ['## Blueprints Definitions', ...blueprints].join('\n\n') : undefined,
|
|
48
|
+
),
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
const objectDefs = yield* Function.pipe(
|
|
52
|
+
objects,
|
|
53
|
+
Effect.forEach((object) =>
|
|
54
|
+
Effect.succeed(trim`
|
|
55
|
+
<object>
|
|
56
|
+
<dxn>${Obj.getDXN(object)}</dxn>
|
|
57
|
+
<typename>${Obj.getTypename(object)}</typename>
|
|
58
|
+
</object>
|
|
59
|
+
`),
|
|
60
|
+
),
|
|
61
|
+
Effect.map((objects) => (objects.length > 0 ? ['## Context Objects', ...objects].join('\n\n') : undefined)),
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
return yield* Function.pipe(
|
|
65
|
+
Effect.succeed([system, blueprintDefs, objectDefs].filter((def): def is string => def !== undefined)),
|
|
66
|
+
Effect.map((parts) => parts.join('\n\n')),
|
|
67
|
+
);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Formats the user prompt.
|
|
72
|
+
*/
|
|
73
|
+
// TODO(burdon): Move to AiPreprocessor.
|
|
74
|
+
// TODO(burdon): Convert util below to `Effect.fn` (to preserve stack info)
|
|
75
|
+
export const formatUserPrompt = ({
|
|
76
|
+
prompt,
|
|
77
|
+
history = [],
|
|
78
|
+
}: Pick<AiSessionRunProps<any>, 'prompt' | 'history'>): Effect.Effect<Message.Message, AiSessionRunError> =>
|
|
79
|
+
Effect.gen(function* () {
|
|
80
|
+
const blocks: ContentBlock.Any[] = [];
|
|
81
|
+
|
|
82
|
+
// TODO(dmaretskyi): Evaluate other approaches as `serviceOption` isn't represented in the type system.
|
|
83
|
+
const artifactDiffResolver = yield* Effect.serviceOption(ArtifactDiffResolver);
|
|
84
|
+
if (Option.isSome(artifactDiffResolver)) {
|
|
85
|
+
const versions = gatherObjectVersions(history);
|
|
86
|
+
const artifactDiff = yield* Effect.tryPromise({
|
|
87
|
+
try: () =>
|
|
88
|
+
artifactDiffResolver.value.resolve(
|
|
89
|
+
[...versions.entries()].map(([id, version]) => ({ id, lastVersion: version })),
|
|
90
|
+
),
|
|
91
|
+
catch: AiAssistantError.wrap({ message: 'Artifact diff resolution error' }),
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
log('version', { artifactDiff, versions });
|
|
95
|
+
for (const [id, { version }] of [...artifactDiff.entries()]) {
|
|
96
|
+
if (ObjectVersion.equals(version, versions.get(id)!)) {
|
|
97
|
+
artifactDiff.delete(id);
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
blocks.push({ _tag: 'anchor', objectId: id, version });
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (artifactDiff.size > 0) {
|
|
105
|
+
blocks.push(createArtifactUpdateBlock(artifactDiff));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return Obj.make(Message.Message, {
|
|
110
|
+
created: new Date().toISOString(),
|
|
111
|
+
sender: { role: 'user' },
|
|
112
|
+
blocks: [...blocks, { _tag: 'text', text: prompt }],
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
const gatherObjectVersions = (messages: Message.Message[]): Map<ObjectId, ObjectVersion> => {
|
|
117
|
+
const artifactIds = new Map<ObjectId, ObjectVersion>();
|
|
118
|
+
for (const message of messages) {
|
|
119
|
+
for (const block of message.blocks) {
|
|
120
|
+
if (block._tag === 'anchor') {
|
|
121
|
+
artifactIds.set(block.objectId, block.version as ObjectVersion);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return artifactIds;
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const createArtifactUpdateBlock = (
|
|
130
|
+
artifactDiff: Map<ObjectId, { version: ObjectVersion; diff?: string }>,
|
|
131
|
+
): ContentBlock.Any => {
|
|
132
|
+
return {
|
|
133
|
+
_tag: 'text',
|
|
134
|
+
// TODO(dmaretskyi): Does this need to be a special content-block?
|
|
135
|
+
disposition: 'artifact-update',
|
|
136
|
+
text: trim`
|
|
137
|
+
The following artifacts have been updated since the last message:
|
|
138
|
+
${[...artifactDiff.entries()]
|
|
139
|
+
.map(([id, { diff }]) => `<changed-artifact id="${id}">${diff ? `\n${diff}` : ''}</changed-artifact>`)
|
|
140
|
+
.join('\n')}
|
|
141
|
+
`,
|
|
142
|
+
};
|
|
143
|
+
};
|
package/src/session/index.ts
CHANGED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type * as Tool from '@effect/ai/Tool';
|
|
6
|
+
import * as Effect from 'effect/Effect';
|
|
7
|
+
import * as Function from 'effect/Function';
|
|
8
|
+
|
|
9
|
+
import { type AiParser, type ConsolePrinter } from '@dxos/ai';
|
|
10
|
+
import { type Message } from '@dxos/types';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Live observer of the generation process.
|
|
14
|
+
*/
|
|
15
|
+
export interface GenerationObserver<Tools extends Record<string, Tool.Any> = any>
|
|
16
|
+
extends AiParser.ParseResponseCallbacks<Tools> {
|
|
17
|
+
/**
|
|
18
|
+
* Complete messages fired during the session, both from the model and from the user.
|
|
19
|
+
* This message will contain all message blocks emitted through the `onBlock` callback.
|
|
20
|
+
*/
|
|
21
|
+
onMessage: (message: Message.Message) => Effect.Effect<void>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const GenerationObserver = Object.freeze({
|
|
25
|
+
make: ({
|
|
26
|
+
onBegin = Function.constant(Effect.void),
|
|
27
|
+
onPart = Function.constant(Effect.void),
|
|
28
|
+
onBlock = Function.constant(Effect.void),
|
|
29
|
+
onEnd = Function.constant(Effect.void),
|
|
30
|
+
onMessage = Function.constant(Effect.void),
|
|
31
|
+
}: Partial<GenerationObserver> = {}): GenerationObserver => ({
|
|
32
|
+
onBegin,
|
|
33
|
+
onPart,
|
|
34
|
+
onBlock,
|
|
35
|
+
onEnd,
|
|
36
|
+
onMessage,
|
|
37
|
+
}),
|
|
38
|
+
|
|
39
|
+
noop: () => GenerationObserver.make(),
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Debug printer to be used in unit-tests and browser devtools.
|
|
43
|
+
*/
|
|
44
|
+
fromPrinter: (printer: ConsolePrinter) =>
|
|
45
|
+
GenerationObserver.make({
|
|
46
|
+
onBlock: (block) =>
|
|
47
|
+
Effect.sync(() => {
|
|
48
|
+
if (block.pending) {
|
|
49
|
+
return; // Only prints full blocks (better for unit-tests and browser devtools).
|
|
50
|
+
}
|
|
51
|
+
printer.printContentBlock(block);
|
|
52
|
+
}),
|
|
53
|
+
onMessage: (message) =>
|
|
54
|
+
Effect.sync(() => {
|
|
55
|
+
if (message.sender.role === 'assistant') {
|
|
56
|
+
return; // Skip assistant messages since they are printed in the `onBlock` callback.
|
|
57
|
+
}
|
|
58
|
+
printer.printMessage(message);
|
|
59
|
+
}),
|
|
60
|
+
}),
|
|
61
|
+
});
|
|
@@ -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","isFailure":false,"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"}}}}]},{"parameters":{"model":"@anthropic/claude-opus-4-0","stream":true,"tools":[]},"prompt":{"content":[{"role":"user","content":"Hello world!","options":{}}]},"response":[{"type":"response-metadata","id":"msg_01QPPeofAkjiYa83LPCeSYfS","modelId":"claude-opus-4-20250514","timestamp":"1970-01-01T00:00:00.000Z","metadata":{}},{"type":"text-start","id":"0","metadata":{}},{"type":"text-delta","id":"0","delta":"Hello!","metadata":{}},{"type":"text-delta","id":"0","delta":" Welcome","metadata":{}},{"type":"text-delta","id":"0","delta":" to the worl","metadata":{}},{"type":"text-delta","id":"0","delta":"d!","metadata":{}},{"type":"text-delta","id":"0","delta":" 🌍 ","metadata":{}},{"type":"text-delta","id":"0","delta":"How","metadata":{}},{"type":"text-delta","id":"0","delta":" are you doing today? Is there anything","metadata":{}},{"type":"text-delta","id":"0","delta":" specific you'd like to chat about or","metadata":{}},{"type":"text-delta","id":"0","delta":" any questions","metadata":{}},{"type":"text-delta","id":"0","delta":" I can help you with?","metadata":{}},{"type":"text-end","id":"0","metadata":{}},{"type":"finish","reason":"stop","usage":{"inputTokens":10,"outputTokens":40,"totalTokens":50},"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":2,"server_tool_use":null,"service_tier":"standard"}}}}]},{"parameters":{"model":"@anthropic/claude-opus-4-0","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_01EaHNEDYN3Hvp5toY8hQGe6","modelId":"claude-opus-4-20250514","timestamp":"1970-01-01T00:00:00.000Z","metadata":{}},{"type":"text-start","id":"0","metadata":{}},{"type":"text-delta","id":"0","delta":"I'll help","metadata":{}},{"type":"text-delta","id":"0","delta":" you calculate ","metadata":{}},{"type":"text-delta","id":"0","delta":"10 + 30.","metadata":{}},{"type":"text-end","id":"0","metadata":{}},{"type":"tool-params-start","id":"toolu_01X4qkrMwCisiUN8Cm4E4Voe","name":"Calculator","providerExecuted":false,"metadata":{}},{"type":"tool-params-delta","id":"toolu_01X4qkrMwCisiUN8Cm4E4Voe","delta":"","metadata":{}},{"type":"tool-params-delta","id":"toolu_01X4qkrMwCisiUN8Cm4E4Voe","delta":"{\"input\": ","metadata":{}},{"type":"tool-params-delta","id":"toolu_01X4qkrMwCisiUN8Cm4E4Voe","delta":"\"10 + 3","metadata":{}},{"type":"tool-params-delta","id":"toolu_01X4qkrMwCisiUN8Cm4E4Voe","delta":"0\"}","metadata":{}},{"type":"tool-params-end","id":"toolu_01X4qkrMwCisiUN8Cm4E4Voe","metadata":{}},{"type":"tool-call","id":"toolu_01X4qkrMwCisiUN8Cm4E4Voe","name":"Calculator","params":{"input":"10 + 30"},"providerExecuted":false,"metadata":{}},{"type":"finish","reason":"tool-calls","usage":{"inputTokens":394,"outputTokens":68,"totalTokens":462},"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":3,"server_tool_use":null,"service_tier":"standard"}}}}]},{"parameters":{"model":"@anthropic/claude-opus-4-0","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.","options":{}},{"type":"tool-call","id":"toolu_01X4qkrMwCisiUN8Cm4E4Voe","name":"Calculator","params":{"input":"10 + 30"},"providerExecuted":false,"options":{}}],"options":{}},{"role":"tool","content":[{"type":"tool-result","id":"toolu_01X4qkrMwCisiUN8Cm4E4Voe","name":"Calculator","isFailure":false,"result":{"result":40},"options":{}}],"options":{}}]},"response":[{"type":"response-metadata","id":"msg_01RjnEkFT3iEUFCpfC4B28aS","modelId":"claude-opus-4-20250514","timestamp":"1970-01-01T00:00:00.000Z","metadata":{}},{"type":"text-start","id":"0","metadata":{}},{"type":"text-delta","id":"0","delta":"10 + 30 = ","metadata":{}},{"type":"text-delta","id":"0","delta":"40","metadata":{}},{"type":"text-end","id":"0","metadata":{}},{"type":"finish","reason":"stop","usage":{"inputTokens":478,"outputTokens":12,"totalTokens":490},"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":478,"output_tokens":8,"server_tool_use":null,"service_tier":"standard"}}}}]}]}
|
|
@@ -2,15 +2,19 @@
|
|
|
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';
|
|
10
|
-
import { AiServiceTestingPreset } from '@dxos/ai/testing';
|
|
11
12
|
import { Obj, Type } from '@dxos/echo';
|
|
13
|
+
import { TestHelpers } from '@dxos/effect/testing';
|
|
12
14
|
import { log } from '@dxos/log';
|
|
13
15
|
|
|
16
|
+
import { AssistantTestLayer } from '../testing';
|
|
17
|
+
|
|
14
18
|
import { AiSession } from './session';
|
|
15
19
|
|
|
16
20
|
// Define a calendar event artifact schema.
|
|
@@ -20,7 +24,7 @@ const CalendarEventSchema = Schema.Struct({
|
|
|
20
24
|
endTime: Schema.String,
|
|
21
25
|
description: Schema.String,
|
|
22
26
|
}).pipe(
|
|
23
|
-
Type.
|
|
27
|
+
Type.object({
|
|
24
28
|
typename: 'example.com/type/CalendarEvent',
|
|
25
29
|
version: '0.1.0',
|
|
26
30
|
}),
|
|
@@ -28,8 +32,8 @@ const CalendarEventSchema = Schema.Struct({
|
|
|
28
32
|
|
|
29
33
|
type CalendarEvent = Schema.Schema.Type<typeof CalendarEventSchema>;
|
|
30
34
|
|
|
31
|
-
|
|
32
|
-
|
|
35
|
+
const TestToolkit = Toolkit.make(
|
|
36
|
+
Tool.make('Calculator', {
|
|
33
37
|
description: 'Basic calculator tool',
|
|
34
38
|
parameters: {
|
|
35
39
|
input: Schema.String.annotations({
|
|
@@ -41,7 +45,7 @@ class TestToolkit extends AiToolkit.make(
|
|
|
41
45
|
}),
|
|
42
46
|
failure: Schema.Never,
|
|
43
47
|
}),
|
|
44
|
-
)
|
|
48
|
+
);
|
|
45
49
|
|
|
46
50
|
// Tool handlers.
|
|
47
51
|
const toolkitLayer = TestToolkit.toLayer({
|
|
@@ -52,7 +56,7 @@ const toolkitLayer = TestToolkit.toLayer({
|
|
|
52
56
|
const sanitizedInput = input.replace(/[^0-9+\-*/().\s]/g, '');
|
|
53
57
|
log.info('calculate', { sanitizedInput });
|
|
54
58
|
|
|
55
|
-
// eslint-disable-next-line
|
|
59
|
+
// eslint-disable-next-line @typescript-eslint/no-implied-eval
|
|
56
60
|
return Function(`"use strict"; return (${sanitizedInput})`)();
|
|
57
61
|
})();
|
|
58
62
|
|
|
@@ -60,182 +64,51 @@ const toolkitLayer = TestToolkit.toLayer({
|
|
|
60
64
|
}),
|
|
61
65
|
});
|
|
62
66
|
|
|
63
|
-
|
|
67
|
+
const TestLayer = Layer.mergeAll(
|
|
68
|
+
AssistantTestLayer({
|
|
69
|
+
types: [CalendarEventSchema],
|
|
70
|
+
}),
|
|
71
|
+
toolkitLayer,
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
describe('AiSession', () => {
|
|
64
75
|
it.effect(
|
|
65
76
|
'no tools',
|
|
66
|
-
Effect.
|
|
67
|
-
function* (
|
|
77
|
+
Effect.fnUntraced(
|
|
78
|
+
function* (_) {
|
|
68
79
|
const session = new AiSession({ operationModel: 'configured' });
|
|
69
|
-
|
|
70
80
|
const response = yield* session.run({
|
|
71
81
|
prompt: 'Hello world!',
|
|
72
82
|
history: [],
|
|
73
83
|
});
|
|
74
84
|
log.info('response', { response });
|
|
75
85
|
},
|
|
76
|
-
Effect.provide(
|
|
77
|
-
|
|
78
|
-
Layer.provideMerge(ToolResolverService.layerEmpty),
|
|
79
|
-
Layer.provideMerge(ToolExecutionService.layerEmpty),
|
|
80
|
-
Layer.provideMerge(AiServiceTestingPreset('direct')),
|
|
81
|
-
),
|
|
82
|
-
),
|
|
86
|
+
Effect.provide(TestLayer),
|
|
87
|
+
TestHelpers.provideTestContext,
|
|
83
88
|
),
|
|
84
89
|
);
|
|
85
90
|
|
|
86
|
-
it.effect
|
|
91
|
+
it.effect(
|
|
87
92
|
'calculator',
|
|
88
|
-
Effect.
|
|
89
|
-
function* (
|
|
93
|
+
Effect.fnUntraced(
|
|
94
|
+
function* (_) {
|
|
90
95
|
const session = new AiSession({ operationModel: 'configured' });
|
|
91
|
-
|
|
96
|
+
const toolkit = yield* TestToolkit;
|
|
92
97
|
const response = yield* session.run({
|
|
93
|
-
|
|
98
|
+
toolkit,
|
|
99
|
+
prompt: 'What is 10 + 30?',
|
|
94
100
|
history: [],
|
|
95
|
-
toolkit: TestToolkit,
|
|
96
101
|
});
|
|
97
102
|
log.info('response', { response });
|
|
98
103
|
},
|
|
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
|
-
),
|
|
104
|
+
Effect.provide(TestLayer),
|
|
105
|
+
TestHelpers.provideTestContext,
|
|
109
106
|
),
|
|
110
107
|
);
|
|
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
108
|
});
|
|
236
109
|
|
|
237
110
|
// Travel to rome, florence, livorno, siena, madrid for conferences
|
|
238
|
-
|
|
111
|
+
|
|
239
112
|
const _CALENDAR_EVENTS: CalendarEvent[] = [
|
|
240
113
|
Obj.make(CalendarEventSchema, {
|
|
241
114
|
title: 'Exploring Ancient Ruins in Rome',
|
|
@@ -268,43 +141,3 @@ const _CALENDAR_EVENTS: CalendarEvent[] = [
|
|
|
268
141
|
description: 'Travel to Madrid',
|
|
269
142
|
}),
|
|
270
143
|
];
|
|
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
|
-
// };
|