@dxos/assistant 0.8.4-main.e098934 → 0.8.4-main.e8ec1fe
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-J5LGTIGS.mjs +10 -0
- package/dist/lib/browser/extraction/index.mjs +118 -161
- package/dist/lib/browser/extraction/index.mjs.map +4 -4
- package/dist/lib/browser/index.mjs +977 -156
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
- package/dist/lib/node-esm/extraction/index.mjs +118 -161
- package/dist/lib/node-esm/extraction/index.mjs.map +4 -4
- package/dist/lib/node-esm/index.mjs +977 -156
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/conversation/context.d.ts +14 -7
- package/dist/types/src/conversation/context.d.ts.map +1 -1
- package/dist/types/src/conversation/conversation.d.ts +11 -12
- package/dist/types/src/conversation/conversation.d.ts.map +1 -1
- package/dist/types/src/diff.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +14 -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.map +1 -1
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +60 -52
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction.d.ts +24 -13
- package/dist/types/src/extraction/extraction.d.ts.map +1 -1
- package/dist/types/src/extraction/quotes.d.ts +1 -1
- package/dist/types/src/extraction/quotes.d.ts.map +1 -1
- package/dist/types/src/functions/services.d.ts +18 -5
- package/dist/types/src/functions/services.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +2 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/session/GenericToolkit.d.ts +84 -0
- package/dist/types/src/session/GenericToolkit.d.ts.map +1 -0
- package/dist/types/src/session/artifact-diff.d.ts +2 -2
- package/dist/types/src/session/artifact-diff.d.ts.map +1 -1
- package/dist/types/src/session/error-handling.d.ts +2 -2
- package/dist/types/src/session/error-handling.d.ts.map +1 -1
- package/dist/types/src/session/format.d.ts +4 -174
- 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 +3 -3
- package/dist/types/src/session/observer.d.ts.map +1 -1
- package/dist/types/src/session/session.d.ts +15 -11
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/session/toolkit.d.ts +7 -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/types/artifact.d.ts +20 -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 +28 -31
- package/src/conversation/context.ts +49 -17
- package/src/conversation/conversation.ts +28 -23
- package/src/diff.test.ts +3 -3
- package/src/diff.ts +5 -0
- 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 +2 -2
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +46 -54
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +10 -9
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +38 -44
- package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +12 -6
- package/src/extraction/extraction.ts +7 -6
- package/src/extraction/quotes.ts +2 -2
- package/src/functions/services.ts +93 -50
- package/src/index.ts +2 -1
- package/src/session/GenericToolkit.ts +130 -0
- package/src/session/artifact-diff.ts +2 -2
- package/src/session/error-handling.ts +6 -4
- package/src/session/format.test.ts +5 -4
- package/src/session/format.ts +17 -12
- package/src/session/index.ts +2 -0
- package/src/session/observer.ts +4 -3
- package/src/session/session.conversations.json +1 -0
- package/src/session/session.test.ts +51 -210
- package/src/session/session.ts +40 -28
- package/src/session/toolkit.ts +19 -13
- package/src/session/version-pin.ts +4 -5
- package/src/templates/{format.tpl → instructions/format.tpl} +1 -0
- package/src/templates/{system.tpl → instructions/system.tpl} +10 -12
- package/src/templates/system.ts +2 -2
- package/src/{types.ts → types/artifact.ts} +22 -3
- package/src/types/index.ts +5 -0
- package/dist/lib/browser/chunk-3K5SRE5A.mjs +0 -404
- package/dist/lib/browser/chunk-3K5SRE5A.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/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-W7AIB6DP.mjs +0 -406
- package/dist/lib/node-esm/chunk-W7AIB6DP.mjs.map +0 -7
- package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs +0 -14
- 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/types.d.ts +0 -16
- package/dist/types/src/types.d.ts.map +0 -1
- 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 -197
- /package/dist/lib/browser/{named-entity-recognition-QPYHML66.mjs.map → chunk-J5LGTIGS.mjs.map} +0 -0
- /package/dist/lib/node-esm/{named-entity-recognition-2HDF46YX.mjs.map → chunk-HSLMI22Q.mjs.map} +0 -0
- /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
|
@@ -2,61 +2,55 @@
|
|
|
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-schema';
|
|
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
|
|
20
11
|
*/
|
|
21
12
|
// TODO(mykola): Make it use vector index to query objects and not pass objects as input.
|
|
22
13
|
export const extractionNerFunction = defineFunction({
|
|
14
|
+
key: 'dxos.org/function/extraction/extract-entities',
|
|
15
|
+
name: 'Extract Entities',
|
|
23
16
|
description: 'Extracts named entities from a text',
|
|
24
17
|
inputSchema: ExtractionInput,
|
|
25
18
|
outputSchema: ExtractionOutput,
|
|
26
19
|
handler: async ({ data: { message, options }, context }) => {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const
|
|
30
|
-
const {
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
|
|
20
|
+
throw new Error('not implemented');
|
|
21
|
+
// log.info('input', { message, options });
|
|
22
|
+
// const startTime = performance.now();
|
|
23
|
+
// const { db } = context.getService(DatabaseService);
|
|
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
|
+
// };
|
|
61
55
|
},
|
|
62
56
|
});
|
|
@@ -6,11 +6,13 @@ 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
|
|
|
13
|
-
|
|
14
|
+
// TODO(mykola): This should not run on CI.
|
|
15
|
+
describe.skip('NamedEntityRecognition', () => {
|
|
14
16
|
let ner: TokenClassificationPipelineType;
|
|
15
17
|
|
|
16
18
|
beforeAll(async () => {
|
|
@@ -56,8 +58,10 @@ describe('NamedEntityRecognition', () => {
|
|
|
56
58
|
test('own data', async () => {
|
|
57
59
|
const { transcriptWoflram, transcriptJosiah } = await createTestData();
|
|
58
60
|
|
|
59
|
-
const blocks = [...transcriptWoflram, ...transcriptJosiah].flatMap((message) =>
|
|
60
|
-
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
|
+
),
|
|
61
65
|
);
|
|
62
66
|
|
|
63
67
|
for (const block of blocks) {
|
|
@@ -69,8 +73,10 @@ describe('NamedEntityRecognition', () => {
|
|
|
69
73
|
test('more own data', async () => {
|
|
70
74
|
const { transcriptMessages } = await createTestData();
|
|
71
75
|
|
|
72
|
-
const blocks = transcriptMessages.flatMap((message) =>
|
|
73
|
-
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
|
+
),
|
|
74
80
|
);
|
|
75
81
|
|
|
76
82
|
for (const block of blocks) {
|
|
@@ -2,16 +2,17 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import * as Schema from 'effect/Schema';
|
|
6
6
|
|
|
7
7
|
import { asyncTimeout } from '@dxos/async';
|
|
8
|
-
import { Expando } from '@dxos/echo
|
|
9
|
-
import { type FunctionDefinition
|
|
8
|
+
import { Expando } from '@dxos/echo/internal';
|
|
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
|
+
message: Message.Message,
|
|
15
16
|
objects: Schema.optional(Schema.Array(Expando)),
|
|
16
17
|
options: Schema.optional(
|
|
17
18
|
Schema.Struct({
|
|
@@ -23,7 +24,7 @@ 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> {}
|
package/src/extraction/quotes.ts
CHANGED
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import * as Schema from 'effect/Schema';
|
|
6
6
|
|
|
7
7
|
import { Filter, Query } from '@dxos/echo';
|
|
8
|
+
import { ObjectId } from '@dxos/echo/internal';
|
|
8
9
|
import { type EchoDatabase } from '@dxos/echo-db';
|
|
9
|
-
import { ObjectId } from '@dxos/echo-schema';
|
|
10
10
|
import { DXN } from '@dxos/keys';
|
|
11
11
|
import { log } from '@dxos/log';
|
|
12
12
|
|
|
@@ -2,97 +2,136 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
6
|
-
import
|
|
5
|
+
import * as Tool from '@effect/ai/Tool';
|
|
6
|
+
import type * 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 Record from 'effect/Record';
|
|
11
|
+
import * as Schema from 'effect/Schema';
|
|
7
12
|
|
|
8
13
|
import { AiToolNotFoundError, ToolExecutionService, ToolResolverService } from '@dxos/ai';
|
|
9
14
|
import { todo } from '@dxos/debug';
|
|
10
|
-
import {
|
|
15
|
+
import { Query } from '@dxos/echo';
|
|
16
|
+
import { DatabaseService } from '@dxos/echo-db';
|
|
17
|
+
import { Function, FunctionDefinition, FunctionInvocationService } from '@dxos/functions';
|
|
11
18
|
import { invariant } from '@dxos/invariant';
|
|
12
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Constructs a `ToolResolverService` whose `resolve(id)` looks up tools in the following order:
|
|
22
|
+
* 1. Toolkit: return an existing tool from the provided `toolkit` if one is present under `id`.
|
|
23
|
+
* 2. Functions in DB: query `FunctionType` by `key=id`; if found, deserialize and project to a tool
|
|
24
|
+
* (propagating any `deployedFunctionId` discovered in the object's metadata).
|
|
25
|
+
* 3. Functions passed in: fall back to a matching `FunctionDefinition` from the `functions` array.
|
|
26
|
+
*
|
|
27
|
+
* If none of the above yield a match, the effect fails with `AiToolNotFoundError`.
|
|
28
|
+
*
|
|
29
|
+
* Requires `DatabaseService` in the environment.
|
|
30
|
+
*/
|
|
13
31
|
export const makeToolResolverFromFunctions = (
|
|
14
|
-
functions: FunctionDefinition
|
|
15
|
-
toolkit:
|
|
16
|
-
): Layer.Layer<ToolResolverService> => {
|
|
17
|
-
return Layer.
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
32
|
+
functions: FunctionDefinition.Any[],
|
|
33
|
+
toolkit: Toolkit.Toolkit<any>,
|
|
34
|
+
): Layer.Layer<ToolResolverService, never, DatabaseService> => {
|
|
35
|
+
return Layer.effect(
|
|
36
|
+
ToolResolverService,
|
|
37
|
+
Effect.gen(function* () {
|
|
38
|
+
const dbService = yield* DatabaseService;
|
|
39
|
+
return {
|
|
40
|
+
resolve: (id): Effect.Effect<Tool.Any, AiToolNotFoundError> =>
|
|
41
|
+
Effect.gen(function* () {
|
|
42
|
+
const tool = toolkit.tools[id];
|
|
43
|
+
if (tool) {
|
|
44
|
+
return tool;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const {
|
|
48
|
+
objects: [dbFunction],
|
|
49
|
+
} = yield* DatabaseService.runQuery(Query.type(Function.Function, { key: id }));
|
|
50
|
+
|
|
51
|
+
const functionDef = dbFunction
|
|
52
|
+
? FunctionDefinition.deserialize(dbFunction)
|
|
53
|
+
: functions.find((fn) => fn.key === id);
|
|
54
|
+
|
|
55
|
+
if (!functionDef) {
|
|
56
|
+
return yield* Effect.fail(new AiToolNotFoundError(id));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return projectFunctionToTool(functionDef);
|
|
60
|
+
}).pipe(Effect.provideService(DatabaseService, dbService)),
|
|
61
|
+
} satisfies Context.Tag.Service<ToolResolverService>;
|
|
30
62
|
}),
|
|
31
|
-
|
|
63
|
+
);
|
|
32
64
|
};
|
|
33
65
|
|
|
34
66
|
export const makeToolExecutionServiceFromFunctions = (
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
): Layer.Layer<ToolExecutionService, never, LocalFunctionExecutionService> => {
|
|
67
|
+
toolkit: Toolkit.Toolkit<any>,
|
|
68
|
+
handlersLayer: Layer.Layer<Tool.Handler<any>, never, never>,
|
|
69
|
+
): Layer.Layer<ToolExecutionService, never, FunctionInvocationService> => {
|
|
39
70
|
return Layer.effect(
|
|
40
71
|
ToolExecutionService,
|
|
41
72
|
Effect.gen(function* () {
|
|
42
73
|
const toolkitHandler = yield* toolkit.pipe(Effect.provide(handlersLayer));
|
|
43
|
-
|
|
74
|
+
invariant(isHandlerLike(toolkitHandler));
|
|
75
|
+
const functionInvocationService = yield* FunctionInvocationService;
|
|
44
76
|
|
|
45
77
|
return {
|
|
46
78
|
handlersFor: (toolkit) => {
|
|
47
|
-
const makeHandler = (tool:
|
|
48
|
-
return Effect.fn(
|
|
49
|
-
if (toolkitHandler.tools
|
|
79
|
+
const makeHandler = (tool: Tool.Any): ((params: unknown) => Effect.Effect<unknown, any, any>) => {
|
|
80
|
+
return Effect.fn(`toolFunctionHandler ${tool.name}`)(function* (input: any) {
|
|
81
|
+
if (toolkitHandler.tools[tool.name]) {
|
|
82
|
+
if (Tool.isProviderDefined(tool)) {
|
|
83
|
+
throw new Error('Attempted to call a provider-defined tool');
|
|
84
|
+
}
|
|
50
85
|
// TODO(wittjosiah): Everything is `never` here.
|
|
51
|
-
|
|
86
|
+
const { result } = yield* (toolkitHandler.handle as any)(tool.name, input);
|
|
87
|
+
return result;
|
|
52
88
|
}
|
|
53
89
|
|
|
54
|
-
const {
|
|
55
|
-
const functionDef = functions.find((fn) => fn.name === functionName);
|
|
56
|
-
if (!functionDef) {
|
|
57
|
-
return yield* Effect.fail(new AiToolNotFoundError(tool.name));
|
|
58
|
-
}
|
|
90
|
+
const { definition: functionDef } = Context.get(FunctionToolAnnotation)(tool.annotations as any);
|
|
59
91
|
|
|
60
|
-
return yield*
|
|
61
|
-
.invokeFunction(functionDef, input)
|
|
92
|
+
return yield* functionInvocationService
|
|
93
|
+
.invokeFunction(functionDef, input as any)
|
|
62
94
|
.pipe(Effect.catchAllDefect((defect) => Effect.fail(defect)));
|
|
63
95
|
});
|
|
64
96
|
};
|
|
65
97
|
|
|
66
|
-
return toolkit.of(
|
|
98
|
+
return toolkit.of(
|
|
99
|
+
Record.map(toolkit.tools, (tool, _name) => (Tool.isUserDefined(tool) ? makeHandler(tool) : null)) as any,
|
|
100
|
+
) as any;
|
|
67
101
|
},
|
|
68
102
|
};
|
|
69
103
|
}),
|
|
70
104
|
);
|
|
71
105
|
};
|
|
72
106
|
|
|
73
|
-
|
|
74
|
-
* Tools that are projected from functions have this annotation.
|
|
75
|
-
*/
|
|
76
|
-
class FunctionToolAnnotation extends Context.Tag('@dxos/assisatnt/FunctionToolAnnotation')<
|
|
107
|
+
class FunctionToolAnnotation extends Context.Tag('@dxos/assistant/FunctionToolAnnotation')<
|
|
77
108
|
FunctionToolAnnotation,
|
|
78
|
-
{
|
|
79
|
-
functionName: string;
|
|
80
|
-
}
|
|
109
|
+
{ definition: FunctionDefinition.Any }
|
|
81
110
|
>() {}
|
|
82
111
|
|
|
83
|
-
const toolCache = new WeakMap<FunctionDefinition
|
|
112
|
+
const toolCache = new WeakMap<FunctionDefinition.Any, Tool.Any>();
|
|
84
113
|
|
|
85
|
-
|
|
114
|
+
/**
|
|
115
|
+
* Projects a `FunctionDefinition` into an `AiTool`.
|
|
116
|
+
*
|
|
117
|
+
* @param fn The function definition to project into a tool.
|
|
118
|
+
* @param meta Optional projection metadata.
|
|
119
|
+
* @param meta.deployedFunctionId Backend deployment ID used for remote invocation when present.
|
|
120
|
+
* This is the EDGE service's function deployment identifier (not the ECHO object ID/DXN and not `FunctionDefinition.key`).
|
|
121
|
+
*/
|
|
122
|
+
const projectFunctionToTool = (fn: FunctionDefinition.Any): Tool.Any => {
|
|
86
123
|
if (toolCache.has(fn)) {
|
|
87
124
|
return toolCache.get(fn)!;
|
|
88
125
|
}
|
|
89
126
|
|
|
90
|
-
|
|
127
|
+
// TODO(burdon): Use and map function.key?
|
|
128
|
+
// TODO(dmaretskyi): Use function key instead.
|
|
129
|
+
const tool = Tool.make(makeToolName(fn.name), {
|
|
91
130
|
description: fn.description,
|
|
92
131
|
parameters: createStructFieldsFromSchema(fn.inputSchema),
|
|
93
132
|
// TODO(dmaretskyi): Include output schema.
|
|
94
133
|
failure: Schema.Any, // TODO(dmaretskyi): Better type for the failure?
|
|
95
|
-
}).annotate(FunctionToolAnnotation, {
|
|
134
|
+
}).annotate(FunctionToolAnnotation, { definition: fn });
|
|
96
135
|
toolCache.set(fn, tool);
|
|
97
136
|
return tool;
|
|
98
137
|
};
|
|
@@ -101,8 +140,8 @@ const projectFunctionToTool = (fn: FunctionDefinition<any, any>): AiTool.Any =>
|
|
|
101
140
|
* @returns Tool name produced from function name by escaping invalid characters.
|
|
102
141
|
*/
|
|
103
142
|
const makeToolName = (name: string) => {
|
|
104
|
-
const toolName = name.replace(/[^a-zA-Z0-9]/g, '
|
|
105
|
-
invariant(toolName.match(/^[a-
|
|
143
|
+
const toolName = name.toLowerCase().replace(/[^a-zA-Z0-9]/g, '-');
|
|
144
|
+
invariant(toolName.match(/^[a-z_][a-z0-9-_]*$/));
|
|
106
145
|
return toolName;
|
|
107
146
|
};
|
|
108
147
|
|
|
@@ -117,3 +156,7 @@ const createStructFieldsFromSchema = (schema: Schema.Schema<any, any>): Record<s
|
|
|
117
156
|
return todo(`Unsupported schema AST: ${schema.ast._tag}`);
|
|
118
157
|
}
|
|
119
158
|
};
|
|
159
|
+
|
|
160
|
+
const isHandlerLike = (value: unknown): value is Toolkit.WithHandler<Record<string, Tool.Any>> => {
|
|
161
|
+
return typeof (value as any).tools === 'object' && typeof (value as any).handle === 'function';
|
|
162
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -4,8 +4,9 @@
|
|
|
4
4
|
|
|
5
5
|
export * from './conversation';
|
|
6
6
|
export * from './diff';
|
|
7
|
+
export * from './errors';
|
|
8
|
+
export * from './execution-graph';
|
|
7
9
|
export * from './functions';
|
|
8
10
|
export * from './session';
|
|
9
11
|
export * from './templates';
|
|
10
12
|
export * from './types';
|
|
11
|
-
export * from './errors';
|
|
@@ -0,0 +1,130 @@
|
|
|
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 Effect from 'effect/Effect';
|
|
8
|
+
import * as Layer from 'effect/Layer';
|
|
9
|
+
import * as Pipeable from 'effect/Pipeable';
|
|
10
|
+
import type * as Schema from 'effect/Schema';
|
|
11
|
+
import type * as SchemaAST from 'effect/SchemaAST';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Unique identifier for generic toolkit instances.
|
|
15
|
+
*/
|
|
16
|
+
export const TypeId = '~@dxos/assistant/GenericToolkit';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Type-level representation of the generic toolkit identifier.
|
|
20
|
+
*/
|
|
21
|
+
export type TypeId = typeof TypeId;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Type-safe way to define toolkits where we don't know specific types of tools,
|
|
25
|
+
* and we want to bundle definition and handlers together.
|
|
26
|
+
*
|
|
27
|
+
* Usefull for plugin-based systems, where tools are dynamically added.
|
|
28
|
+
*
|
|
29
|
+
* @param TR requirements type to the tool invocation.
|
|
30
|
+
* @param E failure type to the handlers layer.
|
|
31
|
+
* @param R requirements type to the handlers layer.
|
|
32
|
+
*/
|
|
33
|
+
export interface GenericToolkit<TR = never, E = never, R = never> extends Pipeable.Pipeable {
|
|
34
|
+
readonly [TypeId]: TypeId;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Toolkit definition.
|
|
38
|
+
*/
|
|
39
|
+
readonly toolkit: Toolkit.Toolkit<GenericTools<TR>>;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Handlers layer.
|
|
43
|
+
*/
|
|
44
|
+
readonly layer: Layer.Layer<unknown, E, R>;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Handlers effect.
|
|
48
|
+
*/
|
|
49
|
+
readonly handlers: Effect.Effect<Toolkit.WithHandler<GenericTools<TR>>, E, R>;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Generic constraint.
|
|
54
|
+
*
|
|
55
|
+
* NOTE: Only use in place of `T extends PortableToolkit.Any`. Not suitable for standalone use.
|
|
56
|
+
*/
|
|
57
|
+
export interface Any {
|
|
58
|
+
readonly [TypeId]: TypeId;
|
|
59
|
+
readonly toolkit: Toolkit.Toolkit<any>;
|
|
60
|
+
readonly layer: Layer.Layer<unknown, any, any>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export type InvocationRequirements<T extends Any> = T extends GenericToolkit<infer TR, infer _E, infer _R> ? TR : never;
|
|
64
|
+
export type Failure<T extends Any> = T extends GenericToolkit<infer _TR, infer E, infer _R> ? E : never;
|
|
65
|
+
export type Requirements<T extends Any> = T extends GenericToolkit<infer _TR, infer _E, infer R> ? R : never;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Creates a portable toolkit from toolkit definition and handlers layer.
|
|
69
|
+
*/
|
|
70
|
+
export const make = <Tools extends Record<string, Tool.Any>, E, R>(
|
|
71
|
+
toolkit: Toolkit.Toolkit<Tools>,
|
|
72
|
+
layer: Layer.Layer<Tool.HandlersFor<Tools>, E, R>,
|
|
73
|
+
): GenericToolkit<E, R> => {
|
|
74
|
+
return {
|
|
75
|
+
[TypeId]: TypeId,
|
|
76
|
+
toolkit: toolkit as any,
|
|
77
|
+
layer: layer as any,
|
|
78
|
+
handlers: toolkit.pipe(Effect.provide(layer)) as any,
|
|
79
|
+
pipe() {
|
|
80
|
+
// eslint-disable-next-line prefer-rest-params
|
|
81
|
+
return Pipeable.pipeArguments(this, arguments);
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Merges multiple portable toolkits into a single portable toolkit.
|
|
88
|
+
*/
|
|
89
|
+
export const merge = <const Toolkits extends ReadonlyArray<Any>>(
|
|
90
|
+
/**
|
|
91
|
+
* The toolkits to merge together.
|
|
92
|
+
*/
|
|
93
|
+
...toolkits: Toolkits
|
|
94
|
+
): GenericToolkit<
|
|
95
|
+
InvocationRequirements<Toolkits[number]>,
|
|
96
|
+
Failure<Toolkits[number]>,
|
|
97
|
+
Requirements<Toolkits[number]>
|
|
98
|
+
> => {
|
|
99
|
+
return make(
|
|
100
|
+
Toolkit.merge(...toolkits.map((t) => t.toolkit)),
|
|
101
|
+
Layer.mergeAll(...(toolkits.map((t) => t.layer) as any)),
|
|
102
|
+
) as any;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Generic tools type where we cannot statically enumerate individual tools.
|
|
107
|
+
*/
|
|
108
|
+
export type GenericTools<R = never> = Record<
|
|
109
|
+
string,
|
|
110
|
+
Tool.Tool<
|
|
111
|
+
string,
|
|
112
|
+
{
|
|
113
|
+
readonly parameters: AnyStructSchemaNoContext;
|
|
114
|
+
readonly success: Schema.Schema.AnyNoContext;
|
|
115
|
+
readonly failure: typeof Schema.Never;
|
|
116
|
+
},
|
|
117
|
+
R
|
|
118
|
+
>
|
|
119
|
+
>;
|
|
120
|
+
|
|
121
|
+
export interface AnyStructSchemaNoContext extends Pipeable.Pipeable {
|
|
122
|
+
readonly [Schema.TypeId]: any;
|
|
123
|
+
readonly make: any;
|
|
124
|
+
readonly Type: any;
|
|
125
|
+
readonly Encoded: any;
|
|
126
|
+
readonly Context: never;
|
|
127
|
+
readonly ast: SchemaAST.AST;
|
|
128
|
+
readonly fields: Schema.Struct.Fields;
|
|
129
|
+
readonly annotations: any;
|
|
130
|
+
}
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
5
|
+
import * as Context from 'effect/Context';
|
|
6
6
|
|
|
7
|
+
import { type ObjectId } from '@dxos/echo/internal';
|
|
7
8
|
import { type ObjectVersion } from '@dxos/echo-db';
|
|
8
|
-
import { type ObjectId } from '@dxos/echo-schema';
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Resolves artifact ids to their versions.
|
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import
|
|
6
|
-
import
|
|
7
|
-
import
|
|
5
|
+
import * as AiError from '@effect/ai/AiError';
|
|
6
|
+
import * as HttpClientError from '@effect/platform/HttpClientError';
|
|
7
|
+
import * as Effect from 'effect/Effect';
|
|
8
|
+
import * as Schema from 'effect/Schema';
|
|
8
9
|
|
|
9
10
|
import { AiModelError } from '../errors';
|
|
10
11
|
|
|
@@ -16,6 +17,7 @@ const AnthropicErrorResponse = Schema.Struct({
|
|
|
16
17
|
}),
|
|
17
18
|
});
|
|
18
19
|
|
|
20
|
+
// TODO(dmaretskyi): Needs rework
|
|
19
21
|
export const mapAiError = (err: AiError.AiError): Effect.Effect<AiError.AiError> =>
|
|
20
22
|
Effect.gen(function* () {
|
|
21
23
|
const cause = err.cause;
|
|
@@ -28,7 +30,7 @@ export const mapAiError = (err: AiError.AiError): Effect.Effect<AiError.AiError>
|
|
|
28
30
|
message: body.error.message,
|
|
29
31
|
context: { model: 'anthropic', type: body.error.type },
|
|
30
32
|
});
|
|
31
|
-
return
|
|
33
|
+
return AiError.UnknownError.make({
|
|
32
34
|
description: body.error.message,
|
|
33
35
|
module: err.module,
|
|
34
36
|
method: err.method,
|
|
@@ -3,13 +3,14 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { beforeAll, describe, it } from '@effect/vitest';
|
|
6
|
-
import
|
|
6
|
+
import * as Effect from 'effect/Effect';
|
|
7
7
|
|
|
8
8
|
import { Blueprint, Template } from '@dxos/blueprints';
|
|
9
9
|
import { Obj } from '@dxos/echo';
|
|
10
10
|
import { type EchoDatabase } from '@dxos/echo-db';
|
|
11
11
|
import { EchoTestBuilder } from '@dxos/echo-db/testing';
|
|
12
|
-
import {
|
|
12
|
+
import { Text } from '@dxos/schema';
|
|
13
|
+
import { Organization } from '@dxos/types';
|
|
13
14
|
import { trim } from '@dxos/util';
|
|
14
15
|
|
|
15
16
|
import { createSystemPrompt } from '../templates/system';
|
|
@@ -21,7 +22,7 @@ describe('format', () => {
|
|
|
21
22
|
beforeAll(async () => {
|
|
22
23
|
const builder = await new EchoTestBuilder().open();
|
|
23
24
|
({ db } = await builder.createDatabase({
|
|
24
|
-
types: [
|
|
25
|
+
types: [Text.Text, Organization.Organization, Blueprint.Blueprint],
|
|
25
26
|
}));
|
|
26
27
|
});
|
|
27
28
|
|
|
@@ -29,7 +30,7 @@ describe('format', () => {
|
|
|
29
30
|
'should format',
|
|
30
31
|
Effect.fn(function* () {
|
|
31
32
|
const object = db.add(
|
|
32
|
-
Obj.make(
|
|
33
|
+
Obj.make(Organization.Organization, {
|
|
33
34
|
name: 'Test',
|
|
34
35
|
website: 'https://www.test.com',
|
|
35
36
|
}),
|