@dxos/assistant 0.8.4-main.c4373fc → 0.8.4-main.c85a9c8dae
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/README.md +1 -0
- package/dist/lib/neutral/chunk-BE6SRJ4N.mjs +193 -0
- package/dist/lib/neutral/chunk-BE6SRJ4N.mjs.map +7 -0
- package/dist/lib/neutral/extraction/index.mjs +223 -0
- package/dist/lib/neutral/extraction/index.mjs.map +7 -0
- package/dist/lib/neutral/index.mjs +1328 -0
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/lib/neutral/testing/index.mjs +38 -0
- package/dist/lib/neutral/testing/index.mjs.map +7 -0
- package/dist/types/src/conversation/context.d.ts +109 -60
- package/dist/types/src/conversation/context.d.ts.map +1 -1
- 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 +41 -16
- 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/diff.d.ts +3 -1
- package/dist/types/src/diff.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +22 -30
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/execution-graph.d.ts +5 -2
- package/dist/types/src/execution-graph.d.ts.map +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 +282 -268
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction.d.ts +348 -303
- package/dist/types/src/extraction/extraction.d.ts.map +1 -1
- package/dist/types/src/extraction/quotes.d.ts +2 -2
- package/dist/types/src/extraction/quotes.d.ts.map +1 -1
- package/dist/types/src/functions/services.d.ts +6 -6
- 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/session/artifact-diff.d.ts +1 -1
- package/dist/types/src/session/artifact-diff.d.ts.map +1 -1
- package/dist/types/src/session/format.d.ts +4 -176
- package/dist/types/src/session/format.d.ts.map +1 -1
- package/dist/types/src/session/index.d.ts +1 -0
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/observer.d.ts +6 -5
- package/dist/types/src/session/observer.d.ts.map +1 -1
- package/dist/types/src/session/session.d.ts +25 -15
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/session/toolkit.d.ts +9 -6
- package/dist/types/src/session/toolkit.d.ts.map +1 -1
- package/dist/types/src/session/version-pin.d.ts +3 -3
- package/dist/types/src/session/version-pin.d.ts.map +1 -1
- package/dist/types/src/testing/index.d.ts +2 -0
- package/dist/types/src/testing/index.d.ts.map +1 -0
- package/dist/types/src/testing/layer.d.ts +22 -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 +41 -31
- package/src/conversation/context.test.ts +46 -0
- package/src/conversation/context.ts +223 -49
- package/src/conversation/conversation.test.ts +65 -0
- package/src/conversation/conversation.ts +179 -53
- package/src/diff.test.ts +37 -6
- package/src/diff.ts +18 -4
- package/src/errors.ts +2 -2
- package/src/execution-graph.test.ts +46 -44
- package/src/execution-graph.ts +25 -16
- package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +2 -2
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +44 -54
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +15 -14
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +36 -44
- package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +10 -5
- package/src/extraction/extraction.ts +9 -8
- package/src/extraction/quotes.ts +4 -5
- package/src/functions/services.ts +88 -37
- package/src/functions.conversations.json +1 -0
- package/src/functions.test.ts +74 -0
- package/src/session/artifact-diff.ts +1 -1
- package/src/session/format.test.ts +103 -39
- package/src/session/format.ts +17 -16
- package/src/session/index.ts +1 -0
- package/src/session/observer.ts +6 -3
- package/src/session/session.conversations.json +1 -1
- package/src/session/session.test.ts +60 -18
- package/src/session/session.ts +99 -60
- package/src/session/toolkit.ts +21 -25
- package/src/session/version-pin.ts +6 -6
- package/src/templates/{format.tpl → instructions/format.tpl} +1 -4
- package/src/templates/system.ts +2 -2
- package/src/testing/index.ts +5 -0
- package/src/testing/layer.ts +85 -0
- package/src/{types.ts → types/artifact.ts} +43 -6
- package/src/types/index.ts +5 -0
- package/dist/lib/browser/chunk-CWGVG235.mjs +0 -380
- package/dist/lib/browser/chunk-CWGVG235.mjs.map +0 -7
- package/dist/lib/browser/chunk-UB26N5UF.mjs +0 -89
- package/dist/lib/browser/chunk-UB26N5UF.mjs.map +0 -7
- package/dist/lib/browser/extraction/index.mjs +0 -272
- package/dist/lib/browser/extraction/index.mjs.map +0 -7
- package/dist/lib/browser/index.mjs +0 -869
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs +0 -13
- package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-BCFXTNSK.mjs +0 -91
- package/dist/lib/node-esm/chunk-BCFXTNSK.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-OV5PMQPB.mjs +0 -382
- package/dist/lib/node-esm/chunk-OV5PMQPB.mjs.map +0 -7
- package/dist/lib/node-esm/extraction/index.mjs +0 -273
- package/dist/lib/node-esm/extraction/index.mjs.map +0 -7
- package/dist/lib/node-esm/index.mjs +0 -870
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs +0 -14
- package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs.map +0 -7
- 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/types.d.ts +0 -16
- package/dist/types/src/types.d.ts.map +0 -1
- package/src/summarization/summarization.test.ts +0 -202
- /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
- /package/src/templates/{system.tpl → instructions/system.tpl} +0 -0
|
@@ -2,18 +2,9 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
// @ts-nocheck
|
|
7
|
-
|
|
8
|
-
import { asyncTimeout } from '@dxos/async';
|
|
9
|
-
import { create } from '@dxos/echo/internal';
|
|
10
|
-
import { DatabaseService, defineFunction } from '@dxos/functions';
|
|
11
|
-
import { invariant } from '@dxos/invariant';
|
|
12
|
-
import { log } from '@dxos/log';
|
|
13
|
-
import { DataType } from '@dxos/schema';
|
|
5
|
+
import { defineFunction } from '@dxos/functions';
|
|
14
6
|
|
|
15
7
|
import { ExtractionInput, ExtractionOutput } from '../extraction';
|
|
16
|
-
import { findQuotes, insertReferences } from '../quotes';
|
|
17
8
|
|
|
18
9
|
/**
|
|
19
10
|
* Named Entity Recognition model
|
|
@@ -26,39 +17,40 @@ export const extractionNerFunction = defineFunction({
|
|
|
26
17
|
inputSchema: ExtractionInput,
|
|
27
18
|
outputSchema: ExtractionOutput,
|
|
28
19
|
handler: async ({ data: { message, options }, context }) => {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
const {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
)
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
20
|
+
throw new Error('not implemented');
|
|
21
|
+
// log.info('input', { message, options });
|
|
22
|
+
// const startTime = performance.now();
|
|
23
|
+
// const { db } = context.getService(Database.Service);
|
|
24
|
+
// const { extractFullEntities } = await import('./named-entity-recognition');
|
|
25
|
+
|
|
26
|
+
// const entitiesPromise = Promise.all(
|
|
27
|
+
// message.blocks.map(async (block) => {
|
|
28
|
+
// invariant(block._tag === 'transcript' || block._tag === 'text');
|
|
29
|
+
// return extractFullEntities(block.text);
|
|
30
|
+
// }),
|
|
31
|
+
// ).then((entities) => entities.flat());
|
|
32
|
+
// const entities = options?.timeout ? await asyncTimeout(entitiesPromise, options.timeout) : await entitiesPromise;
|
|
33
|
+
|
|
34
|
+
// log.info('entities', { entities });
|
|
35
|
+
// const quoteReferences = await findQuotes(
|
|
36
|
+
// entities.map((entity) => entity.word),
|
|
37
|
+
// db,
|
|
38
|
+
// );
|
|
39
|
+
|
|
40
|
+
// const blocksWithReferences = message.blocks.map((block) => {
|
|
41
|
+
// invariant(block._tag === 'transcript' || block._tag === 'text');
|
|
42
|
+
// return { ...block, text: insertReferences(block._tag, quoteReferences) };
|
|
43
|
+
// });
|
|
44
|
+
|
|
45
|
+
// const messageWithReferences = create(Message.Message, {
|
|
46
|
+
// ...message,
|
|
47
|
+
// blocks: blocksWithReferences,
|
|
48
|
+
// });
|
|
49
|
+
|
|
50
|
+
// log.info('output', { messageWithReferences });
|
|
51
|
+
// return {
|
|
52
|
+
// message: messageWithReferences,
|
|
53
|
+
// timeElapsed: performance.now() - startTime,
|
|
54
|
+
// };
|
|
63
55
|
},
|
|
64
56
|
});
|
|
@@ -6,7 +6,8 @@ import { type TokenClassificationOutput, type TokenClassificationPipelineType }
|
|
|
6
6
|
import { beforeAll, describe, expect, test } from 'vitest';
|
|
7
7
|
|
|
8
8
|
import { log } from '@dxos/log';
|
|
9
|
-
import {
|
|
9
|
+
import { type ContentBlock, type Message } from '@dxos/types';
|
|
10
|
+
import { createTestData } from '@dxos/types/testing';
|
|
10
11
|
|
|
11
12
|
import { combineNerTokens, createTokenGroups, extractFullEntities, getNer } from './named-entity-recognition';
|
|
12
13
|
|
|
@@ -57,8 +58,10 @@ describe.skip('NamedEntityRecognition', () => {
|
|
|
57
58
|
test('own data', async () => {
|
|
58
59
|
const { transcriptWoflram, transcriptJosiah } = await createTestData();
|
|
59
60
|
|
|
60
|
-
const blocks = [...transcriptWoflram, ...transcriptJosiah].flatMap((message) =>
|
|
61
|
-
message.blocks.flatMap((block) =>
|
|
61
|
+
const blocks = [...transcriptWoflram, ...transcriptJosiah].flatMap((message: Message.Message) =>
|
|
62
|
+
message.blocks.flatMap((block: ContentBlock.Any) =>
|
|
63
|
+
block._tag === 'transcript' ? [(block as ContentBlock.Transcript).text] : [],
|
|
64
|
+
),
|
|
62
65
|
);
|
|
63
66
|
|
|
64
67
|
for (const block of blocks) {
|
|
@@ -70,8 +73,10 @@ describe.skip('NamedEntityRecognition', () => {
|
|
|
70
73
|
test('more own data', async () => {
|
|
71
74
|
const { transcriptMessages } = await createTestData();
|
|
72
75
|
|
|
73
|
-
const blocks = transcriptMessages.flatMap((message) =>
|
|
74
|
-
message.blocks.flatMap((block) =>
|
|
76
|
+
const blocks = transcriptMessages.flatMap((message: Message.Message) =>
|
|
77
|
+
message.blocks.flatMap((block: ContentBlock.Any) =>
|
|
78
|
+
block._tag === 'transcript' ? [(block as ContentBlock.Transcript).text] : [],
|
|
79
|
+
),
|
|
75
80
|
);
|
|
76
81
|
|
|
77
82
|
for (const block of blocks) {
|
|
@@ -5,14 +5,15 @@
|
|
|
5
5
|
import * as Schema from 'effect/Schema';
|
|
6
6
|
|
|
7
7
|
import { asyncTimeout } from '@dxos/async';
|
|
8
|
-
import {
|
|
9
|
-
import { type FunctionDefinition
|
|
8
|
+
import { Obj } from '@dxos/echo';
|
|
9
|
+
import { type FunctionDefinition } from '@dxos/functions';
|
|
10
|
+
import { type FunctionExecutor } from '@dxos/functions-runtime';
|
|
10
11
|
import { log } from '@dxos/log';
|
|
11
|
-
import {
|
|
12
|
+
import { Message } from '@dxos/types';
|
|
12
13
|
|
|
13
14
|
export const ExtractionInput = Schema.Struct({
|
|
14
|
-
message:
|
|
15
|
-
objects: Schema.optional(Schema.Array(
|
|
15
|
+
message: Message.Message,
|
|
16
|
+
objects: Schema.optional(Schema.Array(Obj.Unknown)),
|
|
16
17
|
options: Schema.optional(
|
|
17
18
|
Schema.Struct({
|
|
18
19
|
timeout: Schema.optional(Schema.Number),
|
|
@@ -23,12 +24,12 @@ export const ExtractionInput = Schema.Struct({
|
|
|
23
24
|
export interface ExtractionInput extends Schema.Schema.Type<typeof ExtractionInput> {}
|
|
24
25
|
|
|
25
26
|
export const ExtractionOutput = Schema.Struct({
|
|
26
|
-
message:
|
|
27
|
+
message: Message.Message,
|
|
27
28
|
timeElapsed: Schema.Number,
|
|
28
29
|
});
|
|
29
30
|
export interface ExtractionOutput extends Schema.Schema.Type<typeof ExtractionOutput> {}
|
|
30
31
|
|
|
31
|
-
export type
|
|
32
|
+
export type ProcessTranscriptMessageProps = {
|
|
32
33
|
input: ExtractionInput;
|
|
33
34
|
function: FunctionDefinition<ExtractionInput, ExtractionOutput>;
|
|
34
35
|
executor: FunctionExecutor;
|
|
@@ -54,7 +55,7 @@ export type ExtractionFunction = FunctionDefinition<ExtractionInput, ExtractionO
|
|
|
54
55
|
* Extract entities from the transcript message and add them to the message.
|
|
55
56
|
*/
|
|
56
57
|
// TODO(dmaretskyi): Move context to a vector search index.
|
|
57
|
-
export const processTranscriptMessage = async (params:
|
|
58
|
+
export const processTranscriptMessage = async (params: ProcessTranscriptMessageProps): Promise<ExtractionOutput> => {
|
|
58
59
|
try {
|
|
59
60
|
if (params.options?.timeout && params.options.timeout > 0) {
|
|
60
61
|
return await asyncTimeout(params.executor.invoke(params.function, params.input), params.options.timeout);
|
package/src/extraction/quotes.ts
CHANGED
|
@@ -4,9 +4,8 @@
|
|
|
4
4
|
|
|
5
5
|
import * as Schema from 'effect/Schema';
|
|
6
6
|
|
|
7
|
-
import { Filter, Query } from '@dxos/echo';
|
|
8
|
-
import { ObjectId } from '@dxos/
|
|
9
|
-
import { type EchoDatabase } from '@dxos/echo-db';
|
|
7
|
+
import { type Database, Filter, Query } from '@dxos/echo';
|
|
8
|
+
import { ObjectId } from '@dxos/keys';
|
|
10
9
|
import { DXN } from '@dxos/keys';
|
|
11
10
|
import { log } from '@dxos/log';
|
|
12
11
|
|
|
@@ -28,10 +27,10 @@ export const ReferencedQuotes = Schema.Struct({
|
|
|
28
27
|
});
|
|
29
28
|
export type ReferencedQuotes = Schema.Schema.Type<typeof ReferencedQuotes>;
|
|
30
29
|
|
|
31
|
-
export const findQuotes = async (quotes: string[], db:
|
|
30
|
+
export const findQuotes = async (quotes: string[], db: Database.Database): Promise<ReferencedQuotes> => {
|
|
32
31
|
const references = await Promise.all(
|
|
33
32
|
quotes.map(async (quote) => {
|
|
34
|
-
const
|
|
33
|
+
const objects = await db.query(Query.select(Filter.text(quote, { type: 'vector' }))).run();
|
|
35
34
|
log.info('found quotes in vector index', { objects, quote });
|
|
36
35
|
return objects.length > 0 ? [{ id: objects[0].id.toString(), quote }] : [];
|
|
37
36
|
}),
|
|
@@ -9,13 +9,17 @@ import * as Effect from 'effect/Effect';
|
|
|
9
9
|
import * as Layer from 'effect/Layer';
|
|
10
10
|
import * as Record from 'effect/Record';
|
|
11
11
|
import * as Schema from 'effect/Schema';
|
|
12
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
12
13
|
|
|
13
14
|
import { AiToolNotFoundError, ToolExecutionService, ToolResolverService } from '@dxos/ai';
|
|
15
|
+
import { GenericToolkit } from '@dxos/ai';
|
|
14
16
|
import { todo } from '@dxos/debug';
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
17
|
+
import { Ref } from '@dxos/echo';
|
|
18
|
+
import { type FunctionDefinition, FunctionInvocationService } from '@dxos/functions';
|
|
17
19
|
import { invariant } from '@dxos/invariant';
|
|
18
20
|
|
|
21
|
+
import { RefFromLLM } from '../types';
|
|
22
|
+
|
|
19
23
|
/**
|
|
20
24
|
* Constructs a `ToolResolverService` whose `resolve(id)` looks up tools in the following order:
|
|
21
25
|
* 1. Toolkit: return an existing tool from the provided `toolkit` if one is present under `id`.
|
|
@@ -25,51 +29,50 @@ import { invariant } from '@dxos/invariant';
|
|
|
25
29
|
*
|
|
26
30
|
* If none of the above yield a match, the effect fails with `AiToolNotFoundError`.
|
|
27
31
|
*
|
|
28
|
-
* Requires `
|
|
32
|
+
* Requires `Database.Service` in the environment.
|
|
29
33
|
*/
|
|
30
|
-
export const makeToolResolverFromFunctions = (
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
+
export const makeToolResolverFromFunctions = (): Layer.Layer<
|
|
35
|
+
ToolResolverService,
|
|
36
|
+
never,
|
|
37
|
+
GenericToolkit.Provider | FunctionInvocationService
|
|
38
|
+
> => {
|
|
34
39
|
return Layer.effect(
|
|
35
40
|
ToolResolverService,
|
|
36
41
|
Effect.gen(function* () {
|
|
37
|
-
const
|
|
42
|
+
const toolkitProvider = yield* GenericToolkit.Provider;
|
|
43
|
+
const functionInvocationService = yield* FunctionInvocationService;
|
|
38
44
|
return {
|
|
39
45
|
resolve: (id): Effect.Effect<Tool.Any, AiToolNotFoundError> =>
|
|
40
46
|
Effect.gen(function* () {
|
|
41
|
-
const
|
|
47
|
+
const toolkit = toolkitProvider.getToolkit();
|
|
48
|
+
|
|
49
|
+
const tool = toolkit.toolkit.tools[id];
|
|
42
50
|
if (tool) {
|
|
43
51
|
return tool;
|
|
44
52
|
}
|
|
45
53
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
? FunctionDefinition.deserialize(dbFunction)
|
|
52
|
-
: functions.find((fn) => fn.key === id);
|
|
53
|
-
|
|
54
|
-
if (!functionDef) {
|
|
55
|
-
return yield* Effect.fail(new AiToolNotFoundError(id));
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return projectFunctionToTool(functionDef);
|
|
59
|
-
}).pipe(Effect.provideService(DatabaseService, dbService)),
|
|
54
|
+
return yield* functionInvocationService.resolveFunction(id).pipe(
|
|
55
|
+
Effect.map(projectFunctionToTool),
|
|
56
|
+
Effect.catchTag('FunctionNotFound', () => Effect.fail(new AiToolNotFoundError(id))),
|
|
57
|
+
);
|
|
58
|
+
}),
|
|
60
59
|
} satisfies Context.Tag.Service<ToolResolverService>;
|
|
61
60
|
}),
|
|
62
61
|
);
|
|
63
62
|
};
|
|
64
63
|
|
|
65
|
-
export const makeToolExecutionServiceFromFunctions = (
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
64
|
+
export const makeToolExecutionServiceFromFunctions = (): Layer.Layer<
|
|
65
|
+
ToolExecutionService,
|
|
66
|
+
never,
|
|
67
|
+
FunctionInvocationService | GenericToolkit.Provider
|
|
68
|
+
> => {
|
|
69
69
|
return Layer.effect(
|
|
70
70
|
ToolExecutionService,
|
|
71
71
|
Effect.gen(function* () {
|
|
72
|
-
const
|
|
72
|
+
const toolkitProvider = yield* GenericToolkit.Provider;
|
|
73
|
+
const toolkit = toolkitProvider.getToolkit();
|
|
74
|
+
|
|
75
|
+
const toolkitHandler = yield* toolkit.toolkit.pipe(Effect.provide(toolkit.layer));
|
|
73
76
|
invariant(isHandlerLike(toolkitHandler));
|
|
74
77
|
const functionInvocationService = yield* FunctionInvocationService;
|
|
75
78
|
|
|
@@ -81,8 +84,10 @@ export const makeToolExecutionServiceFromFunctions = (
|
|
|
81
84
|
if (Tool.isProviderDefined(tool)) {
|
|
82
85
|
throw new Error('Attempted to call a provider-defined tool');
|
|
83
86
|
}
|
|
87
|
+
|
|
84
88
|
// TODO(wittjosiah): Everything is `never` here.
|
|
85
|
-
|
|
89
|
+
const { result } = yield* (toolkitHandler.handle as any)(tool.name, input);
|
|
90
|
+
return result;
|
|
86
91
|
}
|
|
87
92
|
|
|
88
93
|
const { definition: functionDef } = Context.get(FunctionToolAnnotation)(tool.annotations as any);
|
|
@@ -102,26 +107,33 @@ export const makeToolExecutionServiceFromFunctions = (
|
|
|
102
107
|
);
|
|
103
108
|
};
|
|
104
109
|
|
|
110
|
+
export const ToolExecutionServices = Layer.mergeAll(
|
|
111
|
+
makeToolResolverFromFunctions(),
|
|
112
|
+
makeToolExecutionServiceFromFunctions(),
|
|
113
|
+
);
|
|
114
|
+
|
|
105
115
|
class FunctionToolAnnotation extends Context.Tag('@dxos/assistant/FunctionToolAnnotation')<
|
|
106
116
|
FunctionToolAnnotation,
|
|
107
|
-
{ definition: FunctionDefinition
|
|
117
|
+
{ definition: FunctionDefinition.Any }
|
|
108
118
|
>() {}
|
|
109
119
|
|
|
110
|
-
const toolCache = new WeakMap<FunctionDefinition
|
|
120
|
+
const toolCache = new WeakMap<FunctionDefinition.Any, Tool.Any>();
|
|
111
121
|
|
|
112
122
|
/**
|
|
113
123
|
* Projects a `FunctionDefinition` into an `AiTool`.
|
|
114
124
|
*
|
|
115
125
|
* @param fn The function definition to project into a tool.
|
|
116
126
|
* @param meta Optional projection metadata.
|
|
117
|
-
* @param meta.deployedFunctionId Backend deployment ID used for remote invocation when present.
|
|
118
|
-
*
|
|
127
|
+
* @param meta.deployedFunctionId Backend deployment ID used for remote invocation when present.
|
|
128
|
+
* This is the EDGE service's function deployment identifier (not the ECHO object ID/DXN and not `FunctionDefinition.key`).
|
|
119
129
|
*/
|
|
120
|
-
const projectFunctionToTool = (fn: FunctionDefinition
|
|
130
|
+
const projectFunctionToTool = (fn: FunctionDefinition.Any): Tool.Any => {
|
|
121
131
|
if (toolCache.has(fn)) {
|
|
122
132
|
return toolCache.get(fn)!;
|
|
123
133
|
}
|
|
124
134
|
|
|
135
|
+
// TODO(burdon): Use and map function.key?
|
|
136
|
+
// TODO(dmaretskyi): Use function key instead.
|
|
125
137
|
const tool = Tool.make(makeToolName(fn.name), {
|
|
126
138
|
description: fn.description,
|
|
127
139
|
parameters: createStructFieldsFromSchema(fn.inputSchema),
|
|
@@ -136,8 +148,8 @@ const projectFunctionToTool = (fn: FunctionDefinition<any, any>): Tool.Any => {
|
|
|
136
148
|
* @returns Tool name produced from function name by escaping invalid characters.
|
|
137
149
|
*/
|
|
138
150
|
const makeToolName = (name: string) => {
|
|
139
|
-
const toolName = name.replace(/[^a-zA-Z0-9]/g, '
|
|
140
|
-
invariant(toolName.match(/^[a-
|
|
151
|
+
const toolName = name.toLowerCase().replace(/[^a-zA-Z0-9]/g, '-');
|
|
152
|
+
invariant(toolName.match(/^[a-z_][a-z0-9-_]*$/));
|
|
141
153
|
return toolName;
|
|
142
154
|
};
|
|
143
155
|
|
|
@@ -145,7 +157,9 @@ const makeToolName = (name: string) => {
|
|
|
145
157
|
const createStructFieldsFromSchema = (schema: Schema.Schema<any, any>): Record<string, Schema.Schema<any, any>> => {
|
|
146
158
|
switch (schema.ast._tag) {
|
|
147
159
|
case 'TypeLiteral':
|
|
148
|
-
return Object.fromEntries(
|
|
160
|
+
return Object.fromEntries(
|
|
161
|
+
schema.ast.propertySignatures.map((prop) => [prop.name, Schema.make(mapSchemaTypeForLLM(prop.type))]),
|
|
162
|
+
);
|
|
149
163
|
case 'VoidKeyword':
|
|
150
164
|
return {};
|
|
151
165
|
default:
|
|
@@ -153,6 +167,43 @@ const createStructFieldsFromSchema = (schema: Schema.Schema<any, any>): Record<s
|
|
|
153
167
|
}
|
|
154
168
|
};
|
|
155
169
|
|
|
170
|
+
/**
|
|
171
|
+
* Picks an LLM-friendly schema type for the given schema AST.
|
|
172
|
+
* The picked schema type decodes to the original schema type.
|
|
173
|
+
*/
|
|
174
|
+
const mapSchemaTypeForLLM = (ast: SchemaAST.AST): SchemaAST.AST => {
|
|
175
|
+
if (Ref.isRefType(ast)) {
|
|
176
|
+
const description = ast.annotations.description
|
|
177
|
+
? ast.annotations.description + '\n' + RefFromLLM.ast.annotations.description
|
|
178
|
+
: (RefFromLLM.ast.annotations.description as string);
|
|
179
|
+
return RefFromLLM.annotations({ description }).ast;
|
|
180
|
+
} else if (SchemaAST.isTupleType(ast)) {
|
|
181
|
+
return new SchemaAST.TupleType(
|
|
182
|
+
ast.elements.map((t) => new SchemaAST.OptionalType(mapSchemaTypeForLLM(t.type), t.isOptional, t.annotations)),
|
|
183
|
+
ast.rest.map((t) => new SchemaAST.Type(mapSchemaTypeForLLM(t.type), t.annotations)),
|
|
184
|
+
ast.isReadonly,
|
|
185
|
+
ast.annotations,
|
|
186
|
+
);
|
|
187
|
+
} else if (SchemaAST.isTypeLiteral(ast)) {
|
|
188
|
+
return new SchemaAST.TypeLiteral(
|
|
189
|
+
ast.propertySignatures.map(
|
|
190
|
+
(p) =>
|
|
191
|
+
new SchemaAST.PropertySignature(
|
|
192
|
+
p.name,
|
|
193
|
+
mapSchemaTypeForLLM(p.type),
|
|
194
|
+
p.isOptional,
|
|
195
|
+
p.isReadonly,
|
|
196
|
+
p.annotations,
|
|
197
|
+
),
|
|
198
|
+
),
|
|
199
|
+
ast.indexSignatures,
|
|
200
|
+
ast.annotations,
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return ast;
|
|
205
|
+
};
|
|
206
|
+
|
|
156
207
|
const isHandlerLike = (value: unknown): value is Toolkit.WithHandler<Record<string, Tool.Any>> => {
|
|
157
208
|
return typeof (value as any).tools === 'object' && typeof (value as any).handle === 'function';
|
|
158
209
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"conversations":[{"parameters":{"model":"@anthropic/claude-opus-4-6","stream":true,"tools":[{"name":"read-name","description":"Reads the name of an organization.","inputSchema":{"type":"object","required":["org"],"properties":{"org":{"type":"string","description":"The ID of the referenced object. Formats accepted:\n- DXN (dxn:echo:@:01KG7R1ZXWFMWQ4DA1Q6TN1DG4). DXNs can be prepended with an @ symbol for compatibility with in-text references.\n- space ID, object ID tuple (spaceID:objectID)\n- Only object ID that is assumed to be in the current space (01KG7R1ZXWFMWQ4DA1Q6TN1DG4)","examples":["dxn:echo:@:01KG7R1ZXWFMWQ4DA1Q6TN1DG4","@dxn:echo:@:01KG7R1ZXWFMWQ4DA1Q6TN1DG4","BM3FSHFOMJCHCG5QW7JTVKGYABD2GAA7G:01KG7R1ZXWFMWQ4DA1Q6TN1DG4","01KG7R1ZXWFMWQ4DA1Q6TN1DG4"]}},"additionalProperties":false}}]},"prompt":{"content":[{"role":"user","content":"What is the name of the organization? 01JGFJJZ00G0WKQSJGMAKCNT89","options":{"anthropic":{"cacheControl":{"ttl":"5m","type":"ephemeral"}}}}]},"response":[{"type":"response-metadata","id":"msg_01FqHQGxFVdQKeMXjodHavsF","modelId":"claude-opus-4-20250514","timestamp":"1970-01-01T00:00:00.000Z","metadata":{}},{"type":"reasoning-start","id":"0","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"The user is asking for the name of","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" an organization an","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"d has provided what","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" appears to be an","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" object","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" ID: 01JGFJ","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"JZ00G0WK","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"QSJGMAKCNT89","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"\n\nI","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" need to use the read-name function","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" to get the name of this organization.","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" According","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" to the function description, I can","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" pass","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" just","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" the object","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" ID if","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" it's assume","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"d to be in the current space.","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"\n\nLet me call","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" the function with this","metadata":{}},{"type":"reasoning-delta","id":"0","delta":" ID.","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"","metadata":{}},{"type":"reasoning-delta","id":"0","delta":"","metadata":{"anthropic":{"type":"thinking","signature":"EokECkYICxgCKkDkOVtqWk3+YJ1nk31yy/hYUygSTbpQxoD7UKl2RqwyS+tJSV2rou35JEFIl30ogJZiohCBXqY0IZ3YO1SwhWq1EgztHHPI5rGjH4gehN8aDBh3BbEfM6OCraoAQiIwDSGtzFCbT7K+yTBtmtri6uuGvPwNGmRshKURPnFRCjdAf0gj1Jwge4pmIYnlGpRGKvACF4kjtsTHsn8f7ktuuzSEpVNaC6d5VYA6/Fm5ZFaiQPn0/F4fQXy1PNEyo4007gP21Ate8+98CCtlfkOnEE53hElq58doXrVOzpL+uH5b6PBPtM9f1+L9cKjhTPV6djIcewwHTTSJDTM6fr9c82f9u5EDvzNe1QODqlqCiVtNsjWCYOWNZUqILdl0TQ43Hzjf2T+WN6ARHfd3mGVsAtg8vcBM8D4mPvThsTnoswYIeB6Y5aDAA+Xu32jGRmJGclevuy25lJFZG8lAhOXJOdiP88zTG5XCN5ED3GLo2rjKBnQzwGpzg1M0ARKkrNzuLbNZ31uk36ayo+1T381Ld0yIQAX/ZUrLNZ8ovJ1sIHW18hmnyec69+4qWuy//NBWlAfyFOQnQfXM1f2XO5AFBrSILGtHxWM1+KINm1Xh68gHZpjjpyM5g8zl61nDLGxNq3OBugM7NSghygmGPmy2DlxU147cVzOHh7ZXh8fFxBfZlW0YAQ=="}}},{"type":"reasoning-end","id":"0","metadata":{}},{"type":"text-start","id":"1","metadata":{}},{"type":"text-delta","id":"1","delta":"I","metadata":{}},{"type":"text-delta","id":"1","delta":"'ll look","metadata":{}},{"type":"text-delta","id":"1","delta":" up the name of that","metadata":{}},{"type":"text-delta","id":"1","delta":" organization for you.","metadata":{}},{"type":"text-end","id":"1","metadata":{}},{"type":"tool-params-start","id":"toolu_01DdHbHNejZJC4CEjngmNLCf","name":"read-name","providerExecuted":false,"metadata":{}},{"type":"tool-params-delta","id":"toolu_01DdHbHNejZJC4CEjngmNLCf","delta":"","metadata":{}},{"type":"tool-params-delta","id":"toolu_01DdHbHNejZJC4CEjngmNLCf","delta":"{\"or","metadata":{}},{"type":"tool-params-delta","id":"toolu_01DdHbHNejZJC4CEjngmNLCf","delta":"g\": \"01JGFJJ","metadata":{}},{"type":"tool-params-delta","id":"toolu_01DdHbHNejZJC4CEjngmNLCf","delta":"Z00G0WKQ","metadata":{}},{"type":"tool-params-delta","id":"toolu_01DdHbHNejZJC4CEjngmNLCf","delta":"SJGM","metadata":{}},{"type":"tool-params-delta","id":"toolu_01DdHbHNejZJC4CEjngmNLCf","delta":"AKCNT89\"}","metadata":{}},{"type":"tool-params-end","id":"toolu_01DdHbHNejZJC4CEjngmNLCf","metadata":{}},{"type":"tool-call","id":"toolu_01DdHbHNejZJC4CEjngmNLCf","name":"read-name","providerExecuted":false,"metadata":{},"params":{"org":"dxn:echo:@:01JGFJJZ00G0WKQSJGMAKCNT89"}},{"type":"finish","reason":"tool-calls","usage":{"inputTokens":729,"outputTokens":187,"totalTokens":916},"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":729,"output_tokens":8,"server_tool_use":null,"service_tier":"standard"}}}}]},{"parameters":{"model":"@anthropic/claude-opus-4-6","stream":true,"tools":[{"name":"read-name","description":"Reads the name of an organization.","inputSchema":{"type":"object","required":["org"],"properties":{"org":{"type":"string","description":"The ID of the referenced object. Formats accepted:\n- DXN (dxn:echo:@:01KG7R1ZXWFMWQ4DA1Q6TN1DG4). DXNs can be prepended with an @ symbol for compatibility with in-text references.\n- space ID, object ID tuple (spaceID:objectID)\n- Only object ID that is assumed to be in the current space (01KG7R1ZXWFMWQ4DA1Q6TN1DG4)","examples":["dxn:echo:@:01KG7R1ZXWFMWQ4DA1Q6TN1DG4","@dxn:echo:@:01KG7R1ZXWFMWQ4DA1Q6TN1DG4","BM3FSHFOMJCHCG5QW7JTVKGYABD2GAA7G:01KG7R1ZXWFMWQ4DA1Q6TN1DG4","01KG7R1ZXWFMWQ4DA1Q6TN1DG4"]}},"additionalProperties":false}}]},"prompt":{"content":[{"role":"user","content":"What is the name of the organization? 01JGFJJZ00G0WKQSJGMAKCNT89","options":{}},{"role":"assistant","content":[{"type":"reasoning","text":"The user is asking for the name of an organization and has provided what appears to be an object ID: 01JGFJJZ00G0WKQSJGMAKCNT89\n\nI need to use the read-name function to get the name of this organization. According to the function description, I can pass just the object ID if it's assumed to be in the current space.\n\nLet me call the function with this ID.","options":{}},{"type":"text","text":"I'll look up the name of that organization for you.","options":{}},{"type":"tool-call","id":"toolu_01DdHbHNejZJC4CEjngmNLCf","name":"read-name","params":{"org":"01JGFJJZ00G0WKQSJGMAKCNT89"},"providerExecuted":false,"options":{}}],"options":{}},{"role":"tool","content":[{"type":"tool-result","id":"toolu_01DdHbHNejZJC4CEjngmNLCf","name":"read-name","isFailure":false,"result":"BlueYard","providerExecuted":false,"options":{}}],"options":{"anthropic":{"cacheControl":{"ttl":"5m","type":"ephemeral"}}}}]},"response":[{"type":"response-metadata","id":"msg_01X5xvtDymPG7tTUMLZz3eAD","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":"The name of the organization with","metadata":{}},{"type":"text-delta","id":"0","delta":" ID 01JGFJJ","metadata":{}},{"type":"text-delta","id":"0","delta":"Z00G0WKQ","metadata":{}},{"type":"text-delta","id":"0","delta":"SJGMAKCNT89 is","metadata":{}},{"type":"text-delta","id":"0","delta":" **BlueYard**.","metadata":{}},{"type":"text-end","id":"0","metadata":{}},{"type":"finish","reason":"stop","usage":{"inputTokens":801,"outputTokens":39,"totalTokens":840},"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":801,"output_tokens":6,"server_tool_use":null,"service_tier":"standard"}}}}]}]}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { describe, it } from '@effect/vitest';
|
|
6
|
+
import * as Effect from 'effect/Effect';
|
|
7
|
+
import * as Schema from 'effect/Schema';
|
|
8
|
+
|
|
9
|
+
import { ConsolePrinter } from '@dxos/ai';
|
|
10
|
+
import { MemoizedAiService } from '@dxos/ai/testing';
|
|
11
|
+
import { AiSession, GenerationObserver, createToolkit } from '@dxos/assistant';
|
|
12
|
+
import { Blueprint } from '@dxos/blueprints';
|
|
13
|
+
import { Database, Obj, Ref } from '@dxos/echo';
|
|
14
|
+
import { TestHelpers } from '@dxos/effect/testing';
|
|
15
|
+
import { defineFunction } from '@dxos/functions';
|
|
16
|
+
import { ObjectId } from '@dxos/keys';
|
|
17
|
+
import { Organization } from '@dxos/types';
|
|
18
|
+
|
|
19
|
+
import { AssistantTestLayer } from './testing';
|
|
20
|
+
|
|
21
|
+
ObjectId.dangerouslyDisableRandomness();
|
|
22
|
+
|
|
23
|
+
const readName = defineFunction({
|
|
24
|
+
key: 'dxos.org/function/read-name',
|
|
25
|
+
name: 'Read Name',
|
|
26
|
+
description: 'Reads the name of an organization.',
|
|
27
|
+
inputSchema: Schema.Struct({
|
|
28
|
+
org: Ref.Ref(Organization.Organization),
|
|
29
|
+
}),
|
|
30
|
+
outputSchema: Schema.String,
|
|
31
|
+
handler: Effect.fnUntraced(function* ({ data }) {
|
|
32
|
+
const org = yield* Database.load(data.org);
|
|
33
|
+
return org.name ?? '<no org>';
|
|
34
|
+
}),
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const blueprint = Blueprint.make({
|
|
38
|
+
key: 'dxos.org/blueprint/test',
|
|
39
|
+
name: 'Test blueprint',
|
|
40
|
+
tools: Blueprint.toolDefinitions({ functions: [readName] }),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const TestLayer = AssistantTestLayer({
|
|
44
|
+
aiServicePreset: 'edge-remote',
|
|
45
|
+
functions: [readName],
|
|
46
|
+
types: [Organization.Organization],
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
describe('Research', () => {
|
|
50
|
+
it.effect(
|
|
51
|
+
'call a function with a ref input',
|
|
52
|
+
Effect.fnUntraced(
|
|
53
|
+
function* (_) {
|
|
54
|
+
const org = yield* Database.add(
|
|
55
|
+
Obj.make(Organization.Organization, {
|
|
56
|
+
name: 'BlueYard',
|
|
57
|
+
website: 'https://blueyard.com',
|
|
58
|
+
}),
|
|
59
|
+
);
|
|
60
|
+
yield* Database.flush({ indexes: true });
|
|
61
|
+
yield* new AiSession().run({
|
|
62
|
+
prompt: `What is the name of the organization? ${org.id}`,
|
|
63
|
+
toolkit: yield* createToolkit({
|
|
64
|
+
blueprints: [blueprint],
|
|
65
|
+
}),
|
|
66
|
+
observer: GenerationObserver.fromPrinter(new ConsolePrinter()),
|
|
67
|
+
});
|
|
68
|
+
},
|
|
69
|
+
Effect.provide(TestLayer),
|
|
70
|
+
TestHelpers.provideTestContext,
|
|
71
|
+
),
|
|
72
|
+
MemoizedAiService.isGenerationEnabled() ? 240_000 : 30_000,
|
|
73
|
+
);
|
|
74
|
+
});
|