@dxos/assistant 0.8.4-main.ead640a → 0.8.4-main.ef1bc66f44
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/neutral/chunk-J5LGTIGS.mjs +10 -0
- package/dist/lib/neutral/chunk-ZPC54VRM.mjs +596 -0
- package/dist/lib/neutral/chunk-ZPC54VRM.mjs.map +7 -0
- package/dist/lib/neutral/extraction/index.mjs +225 -0
- package/dist/lib/neutral/extraction/index.mjs.map +7 -0
- package/dist/lib/{browser → neutral}/index.mjs +346 -350
- 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 +97 -62
- 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 +254 -248
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction.d.ts +344 -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/GenericToolkit.d.ts +107 -0
- package/dist/types/src/session/GenericToolkit.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 +2 -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 +16 -11
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/session/toolkit.d.ts +5 -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 +14 -2
- package/dist/types/src/types/artifact.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +40 -31
- package/src/conversation/context.test.ts +46 -0
- package/src/conversation/context.ts +211 -67
- package/src/conversation/conversation.test.ts +65 -0
- package/src/conversation/conversation.ts +128 -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 +45 -43
- package/src/execution-graph.ts +23 -14
- 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 +66 -37
- package/src/functions.conversations.json +1 -0
- package/src/functions.test.ts +74 -0
- package/src/session/GenericToolkit.ts +160 -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 +3 -0
- package/src/session/observer.ts +5 -3
- package/src/session/session.conversations.json +1 -1
- package/src/session/session.test.ts +28 -15
- package/src/session/session.ts +44 -21
- package/src/session/toolkit.ts +13 -22
- 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 +84 -0
- package/src/types/artifact.ts +43 -6
- 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.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/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 -934
- 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/src/summarization/summarization.test.ts +0 -202
- /package/dist/lib/{browser/named-entity-recognition-QPYHML66.mjs.map → neutral/chunk-J5LGTIGS.mjs.map} +0 -0
- /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 { Type } 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(Type.Obj)),
|
|
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 type * as SchemaAST from 'effect/SchemaAST';
|
|
12
13
|
|
|
13
14
|
import { AiToolNotFoundError, ToolExecutionService, ToolResolverService } from '@dxos/ai';
|
|
14
15
|
import { todo } from '@dxos/debug';
|
|
15
|
-
import {
|
|
16
|
-
import {
|
|
16
|
+
import { Type } from '@dxos/echo';
|
|
17
|
+
import { type FunctionDefinition, FunctionInvocationService } from '@dxos/functions';
|
|
17
18
|
import { invariant } from '@dxos/invariant';
|
|
18
19
|
|
|
20
|
+
import { GenericToolkit } from '../session';
|
|
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,21 @@ 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 (Type.Ref.isRefSchemaAST(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
|
+
}
|
|
181
|
+
|
|
182
|
+
return ast;
|
|
183
|
+
};
|
|
184
|
+
|
|
156
185
|
const isHandlerLike = (value: unknown): value is Toolkit.WithHandler<Record<string, Tool.Any>> => {
|
|
157
186
|
return typeof (value as any).tools === 'object' && typeof (value as any).handle === 'function';
|
|
158
187
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"conversations":[{"parameters":{"model":"@anthropic/claude-opus-4-0","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-0","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, Type } 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: Type.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
|
+
});
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import type * as Tool from '@effect/ai/Tool';
|
|
6
|
+
import * as Toolkit from '@effect/ai/Toolkit';
|
|
7
|
+
import * as Context from 'effect/Context';
|
|
8
|
+
import * as Effect from 'effect/Effect';
|
|
9
|
+
import * as Layer from 'effect/Layer';
|
|
10
|
+
import * as Pipeable from 'effect/Pipeable';
|
|
11
|
+
import type * as Schema from 'effect/Schema';
|
|
12
|
+
import type * as SchemaAST from 'effect/SchemaAST';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Unique identifier for generic toolkit instances.
|
|
16
|
+
*/
|
|
17
|
+
export const TypeId = '~@dxos/assistant/GenericToolkit';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Type-level representation of the generic toolkit identifier.
|
|
21
|
+
*/
|
|
22
|
+
export type TypeId = typeof TypeId;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Type-safe way to define toolkits where we don't know specific types of tools,
|
|
26
|
+
* and we want to bundle definition and handlers together.
|
|
27
|
+
*
|
|
28
|
+
* Usefull for plugin-based systems, where tools are dynamically added.
|
|
29
|
+
*
|
|
30
|
+
* @param TR requirements type to the tool invocation.
|
|
31
|
+
* @param E failure type to the handlers layer.
|
|
32
|
+
* @param R requirements type to the handlers layer.
|
|
33
|
+
*/
|
|
34
|
+
export interface GenericToolkit<TR = never, E = never, R = never> extends Pipeable.Pipeable {
|
|
35
|
+
readonly [TypeId]: TypeId;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Toolkit definition.
|
|
39
|
+
*/
|
|
40
|
+
readonly toolkit: Toolkit.Toolkit<GenericTools<TR>>;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Handlers layer.
|
|
44
|
+
*/
|
|
45
|
+
readonly layer: Layer.Layer<unknown, E, R>;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Handlers effect.
|
|
49
|
+
*/
|
|
50
|
+
readonly handlers: Effect.Effect<Toolkit.WithHandler<GenericTools<TR>>, E, R>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Generic constraint.
|
|
55
|
+
*
|
|
56
|
+
* NOTE: Only use in place of `T extends PortableToolkit.Any`. Not suitable for standalone use.
|
|
57
|
+
*/
|
|
58
|
+
export interface Any {
|
|
59
|
+
readonly [TypeId]: TypeId;
|
|
60
|
+
readonly toolkit: Toolkit.Toolkit<any>;
|
|
61
|
+
readonly layer: Layer.Layer<unknown, any, any>;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export type InvocationRequirements<T extends Any> = T extends GenericToolkit<infer TR, infer _E, infer _R> ? TR : never;
|
|
65
|
+
export type Failure<T extends Any> = T extends GenericToolkit<infer _TR, infer E, infer _R> ? E : never;
|
|
66
|
+
export type Requirements<T extends Any> = T extends GenericToolkit<infer _TR, infer _E, infer R> ? R : never;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Creates a portable toolkit from toolkit definition and handlers layer.
|
|
70
|
+
*/
|
|
71
|
+
export const make = <Tools extends Record<string, Tool.Any>, E, R>(
|
|
72
|
+
toolkit: Toolkit.Toolkit<Tools>,
|
|
73
|
+
layer: Layer.Layer<Tool.HandlersFor<Tools>, E, R>,
|
|
74
|
+
): GenericToolkit<E, R> => {
|
|
75
|
+
return {
|
|
76
|
+
[TypeId]: TypeId,
|
|
77
|
+
toolkit: toolkit as any,
|
|
78
|
+
layer: layer as any,
|
|
79
|
+
handlers: toolkit.pipe(Effect.provide(layer)) as any,
|
|
80
|
+
pipe() {
|
|
81
|
+
// eslint-disable-next-line prefer-rest-params
|
|
82
|
+
return Pipeable.pipeArguments(this, arguments);
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Empty generic toolkit.
|
|
89
|
+
*/
|
|
90
|
+
export const empty = make(Toolkit.empty, Layer.empty);
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Merges multiple portable toolkits into a single portable toolkit.
|
|
94
|
+
*/
|
|
95
|
+
export const merge = <const Toolkits extends ReadonlyArray<Any>>(
|
|
96
|
+
/**
|
|
97
|
+
* The toolkits to merge together.
|
|
98
|
+
*/
|
|
99
|
+
...toolkits: Toolkits
|
|
100
|
+
): GenericToolkit<
|
|
101
|
+
InvocationRequirements<Toolkits[number]>,
|
|
102
|
+
Failure<Toolkits[number]>,
|
|
103
|
+
Requirements<Toolkits[number]>
|
|
104
|
+
> => {
|
|
105
|
+
return make(
|
|
106
|
+
Toolkit.merge(...toolkits.map((t) => t.toolkit)),
|
|
107
|
+
Layer.mergeAll(...(toolkits.map((t) => t.layer) as any)),
|
|
108
|
+
) as any;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Generic tools type where we cannot statically enumerate individual tools.
|
|
113
|
+
*/
|
|
114
|
+
export type GenericTools<R = never> = Record<
|
|
115
|
+
string,
|
|
116
|
+
Tool.Tool<
|
|
117
|
+
string,
|
|
118
|
+
{
|
|
119
|
+
readonly parameters: AnyStructSchemaNoContext;
|
|
120
|
+
readonly success: Schema.Schema.AnyNoContext;
|
|
121
|
+
readonly failure: typeof Schema.Never;
|
|
122
|
+
readonly failureMode: Tool.FailureMode;
|
|
123
|
+
},
|
|
124
|
+
R
|
|
125
|
+
>
|
|
126
|
+
>;
|
|
127
|
+
|
|
128
|
+
export interface AnyStructSchemaNoContext extends Pipeable.Pipeable {
|
|
129
|
+
readonly [Schema.TypeId]: any;
|
|
130
|
+
readonly make: any;
|
|
131
|
+
readonly Type: any;
|
|
132
|
+
readonly Encoded: any;
|
|
133
|
+
readonly Context: never;
|
|
134
|
+
readonly ast: SchemaAST.AST;
|
|
135
|
+
readonly fields: Schema.Struct.Fields;
|
|
136
|
+
readonly annotations: any;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Provides a generic toolkit to the agent.
|
|
141
|
+
*/
|
|
142
|
+
export class Provider extends Context.Tag('@dxos/assistant/GenericToolkit.Provider')<
|
|
143
|
+
Provider,
|
|
144
|
+
{
|
|
145
|
+
readonly getToolkit: () => GenericToolkit;
|
|
146
|
+
}
|
|
147
|
+
>() {}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Layer for providing a generic toolkit to the agent.
|
|
151
|
+
*/
|
|
152
|
+
export const providerLayer = (toolkit: GenericToolkit) =>
|
|
153
|
+
Layer.succeed(Provider, {
|
|
154
|
+
getToolkit: () => toolkit,
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Layer for providing an empty generic toolkit to the agent.
|
|
159
|
+
*/
|
|
160
|
+
export const providerEmpty = providerLayer(empty);
|