@dxos/assistant 0.8.4-main.bc674ce → 0.8.4-main.bcb3aa67d6
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-KVYJFFLF.mjs +1805 -0
- package/dist/lib/neutral/chunk-KVYJFFLF.mjs.map +7 -0
- package/dist/lib/{browser → neutral}/extraction/index.mjs +26 -32
- package/dist/lib/neutral/extraction/index.mjs.map +7 -0
- package/dist/lib/{browser → neutral}/index.mjs +72 -371
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/lib/neutral/testing/index.mjs +112 -0
- package/dist/lib/neutral/testing/index.mjs.map +7 -0
- package/dist/types/src/conversation/context.d.ts +24 -5
- package/dist/types/src/conversation/context.d.ts.map +1 -1
- package/dist/types/src/conversation/conversation.d.ts +29 -4
- package/dist/types/src/conversation/conversation.d.ts.map +1 -1
- package/dist/types/src/diff.d.ts +3 -1
- package/dist/types/src/diff.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction-ner-function/index.d.ts +0 -1
- package/dist/types/src/extraction/extraction-ner-function/index.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction.d.ts +729 -17
- package/dist/types/src/extraction/extraction.d.ts.map +1 -1
- package/dist/types/src/extraction/index.d.ts +0 -1
- package/dist/types/src/extraction/index.d.ts.map +1 -1
- package/dist/types/src/functions/services.d.ts +27 -8
- package/dist/types/src/functions/services.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +2 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/service/AgentService.d.ts +62 -0
- package/dist/types/src/service/AgentService.d.ts.map +1 -0
- package/dist/types/src/service/AgentService.test.d.ts +2 -0
- package/dist/types/src/service/AgentService.test.d.ts.map +1 -0
- package/dist/types/src/service/agent-process.d.ts +17 -0
- package/dist/types/src/service/agent-process.d.ts.map +1 -0
- package/dist/types/src/service/index.d.ts +3 -0
- package/dist/types/src/service/index.d.ts.map +1 -0
- package/dist/types/src/session/index.d.ts +1 -1
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/observer.d.ts.map +1 -1
- package/dist/types/src/session/session.d.ts +64 -8
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/session/toolkit.d.ts +6 -2
- package/dist/types/src/session/toolkit.d.ts.map +1 -1
- package/dist/types/src/templates/system.d.ts +0 -1
- package/dist/types/src/templates/system.d.ts.map +1 -1
- package/dist/types/src/testing/layer.d.ts +23 -9
- package/dist/types/src/testing/layer.d.ts.map +1 -1
- package/dist/types/src/tracing.d.ts +264 -0
- package/dist/types/src/tracing.d.ts.map +1 -0
- package/dist/types/src/types/artifact.d.ts +5 -5
- package/dist/types/src/types/artifact.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +33 -33
- package/src/conversation/context.test.ts +1 -1
- package/src/conversation/context.ts +84 -18
- package/src/conversation/conversation.ts +173 -32
- package/src/diff.test.ts +36 -5
- package/src/diff.ts +13 -4
- package/src/execution-graph.test.ts +2 -2
- package/src/execution-graph.ts +2 -2
- package/src/extraction/extraction-ner-function/index.ts +0 -1
- package/src/extraction/extraction.ts +38 -10
- package/src/extraction/index.ts +0 -1
- package/src/functions/services.ts +199 -50
- package/src/functions.conversations.json +1 -1
- package/src/functions.test.ts +28 -20
- package/src/index.ts +2 -0
- package/src/service/AgentService.conversations.json +1 -0
- package/src/service/AgentService.test.ts +169 -0
- package/src/service/AgentService.ts +171 -0
- package/src/service/agent-process.ts +360 -0
- package/src/service/index.ts +6 -0
- package/src/session/format.test.ts +23 -14
- package/src/session/index.ts +1 -2
- package/src/session/observer.ts +3 -2
- package/src/session/session.conversations.json +1 -1
- package/src/session/session.test.ts +52 -4
- package/src/session/session.ts +231 -117
- package/src/session/toolkit.ts +12 -7
- package/src/templates/instructions/format.tpl +0 -4
- package/src/templates/instructions/system.tpl +11 -2
- package/src/templates/system.ts +6 -5
- package/src/testing/layer.ts +209 -24
- package/src/tracing.ts +112 -0
- package/src/types/artifact.ts +17 -10
- package/dist/lib/browser/chunk-7CDDG6LL.mjs +0 -578
- package/dist/lib/browser/chunk-7CDDG6LL.mjs.map +0 -7
- 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/testing/index.mjs +0 -32
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-CC7ER6Q3.mjs +0 -579
- package/dist/lib/node-esm/chunk-CC7ER6Q3.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +0 -11
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +0 -7
- package/dist/lib/node-esm/extraction/index.mjs +0 -226
- package/dist/lib/node-esm/extraction/index.mjs.map +0 -7
- package/dist/lib/node-esm/index.mjs +0 -851
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/lib/node-esm/testing/index.mjs +0 -33
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +0 -4
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +0 -1
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts +0 -2
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts.map +0 -1
- package/dist/types/src/extraction/extraction-llm-function/index.d.ts +0 -2
- package/dist/types/src/extraction/extraction-llm-function/index.d.ts.map +0 -1
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +0 -347
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +0 -1
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts +0 -2
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts.map +0 -1
- package/dist/types/src/session/GenericToolkit.d.ts +0 -85
- package/dist/types/src/session/GenericToolkit.d.ts.map +0 -1
- package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +0 -84
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +0 -62
- package/src/extraction/extraction-llm-function/index.ts +0 -5
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +0 -116
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +0 -56
- package/src/session/GenericToolkit.ts +0 -131
- /package/dist/lib/{browser → neutral}/chunk-J5LGTIGS.mjs +0 -0
- /package/dist/lib/{browser → neutral}/chunk-J5LGTIGS.mjs.map +0 -0
|
@@ -71,7 +71,7 @@ describe('ExecutionGraph', () => {
|
|
|
71
71
|
blocks: [
|
|
72
72
|
{
|
|
73
73
|
_tag: 'text',
|
|
74
|
-
text: '{"id":"01K6AXZKK79H4E54RKP1YZDARK","@type":"dxn:type:dxos.
|
|
74
|
+
text: '{"id":"01K6AXZKK79H4E54RKP1YZDARK","@type":"dxn:type:org.dxos.type.organization:0.1.0","@meta":{"keys":[]},"name":"Blue Yard","website":"https://blueyard.com"}',
|
|
75
75
|
},
|
|
76
76
|
],
|
|
77
77
|
properties: {},
|
|
@@ -619,7 +619,7 @@ describe('ExecutionGraph', () => {
|
|
|
619
619
|
{ _tag: 'reasoning', reasoningText: 'I need to think about this' },
|
|
620
620
|
{ _tag: 'toolCall', toolCallId: 'tool1', name: 'analyze', input: '{}', providerExecuted: false },
|
|
621
621
|
{
|
|
622
|
-
_tag: '
|
|
622
|
+
_tag: 'stats',
|
|
623
623
|
message: 'OK',
|
|
624
624
|
duration: 1000,
|
|
625
625
|
toolCalls: 1,
|
package/src/execution-graph.ts
CHANGED
|
@@ -412,7 +412,7 @@ const createBlockCommit = (
|
|
|
412
412
|
message: block.error ? 'Tool error: ' + block.error : 'Tool call succeeded',
|
|
413
413
|
} satisfies Commit;
|
|
414
414
|
|
|
415
|
-
case '
|
|
415
|
+
case 'stats':
|
|
416
416
|
return {
|
|
417
417
|
id: getGenericBlockId(message.id, idx),
|
|
418
418
|
branch,
|
|
@@ -420,7 +420,7 @@ const createBlockCommit = (
|
|
|
420
420
|
timestamp,
|
|
421
421
|
icon: IconType.ROCKET,
|
|
422
422
|
level: LogLevel.INFO,
|
|
423
|
-
message: ContentBlock.
|
|
423
|
+
message: ContentBlock.createStatsMessage(block),
|
|
424
424
|
} satisfies Commit;
|
|
425
425
|
|
|
426
426
|
case 'status':
|
|
@@ -4,16 +4,14 @@
|
|
|
4
4
|
|
|
5
5
|
import * as Schema from 'effect/Schema';
|
|
6
6
|
|
|
7
|
-
import {
|
|
8
|
-
import { Type } from '@dxos/echo';
|
|
9
|
-
import { type FunctionDefinition } from '@dxos/functions';
|
|
10
|
-
import { type FunctionExecutor } from '@dxos/functions-runtime';
|
|
7
|
+
import { Obj } from '@dxos/echo';
|
|
11
8
|
import { log } from '@dxos/log';
|
|
9
|
+
import { Operation } from '@dxos/operation';
|
|
12
10
|
import { Message } from '@dxos/types';
|
|
13
11
|
|
|
14
12
|
export const ExtractionInput = Schema.Struct({
|
|
15
13
|
message: Message.Message,
|
|
16
|
-
objects: Schema.optional(Schema.Array(
|
|
14
|
+
objects: Schema.optional(Schema.Array(Obj.Unknown)),
|
|
17
15
|
options: Schema.optional(
|
|
18
16
|
Schema.Struct({
|
|
19
17
|
timeout: Schema.optional(Schema.Number),
|
|
@@ -31,8 +29,7 @@ export interface ExtractionOutput extends Schema.Schema.Type<typeof ExtractionOu
|
|
|
31
29
|
|
|
32
30
|
export type ProcessTranscriptMessageProps = {
|
|
33
31
|
input: ExtractionInput;
|
|
34
|
-
function:
|
|
35
|
-
executor: FunctionExecutor;
|
|
32
|
+
function: Operation.Definition<ExtractionInput, ExtractionOutput>;
|
|
36
33
|
|
|
37
34
|
options?: {
|
|
38
35
|
/**
|
|
@@ -49,7 +46,35 @@ export type ProcessTranscriptMessageProps = {
|
|
|
49
46
|
};
|
|
50
47
|
};
|
|
51
48
|
|
|
52
|
-
export type ExtractionFunction =
|
|
49
|
+
export type ExtractionFunction = Operation.Definition<ExtractionInput, ExtractionOutput>;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Placeholder for Anthropic-based entity extraction.
|
|
53
|
+
* Implementation is currently disabled pending restoration of extraction logic.
|
|
54
|
+
*/
|
|
55
|
+
export const extractionAnthropicFunction = Operation.make({
|
|
56
|
+
meta: {
|
|
57
|
+
key: 'org.dxos.function.extraction.anthropic',
|
|
58
|
+
name: 'Entity Extraction (Anthropic)',
|
|
59
|
+
description: 'Extract entities from transcript using Anthropic LLM.',
|
|
60
|
+
},
|
|
61
|
+
input: ExtractionInput,
|
|
62
|
+
output: ExtractionOutput,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Placeholder for NER-based entity extraction.
|
|
67
|
+
* Implementation is currently disabled pending restoration of extraction logic.
|
|
68
|
+
*/
|
|
69
|
+
export const extractionNerFunction = Operation.make({
|
|
70
|
+
meta: {
|
|
71
|
+
key: 'org.dxos.function.extraction.ner',
|
|
72
|
+
name: 'Entity Extraction (NER)',
|
|
73
|
+
description: 'Extract entities from transcript using Named Entity Recognition.',
|
|
74
|
+
},
|
|
75
|
+
input: ExtractionInput,
|
|
76
|
+
output: ExtractionOutput,
|
|
77
|
+
});
|
|
53
78
|
|
|
54
79
|
/**
|
|
55
80
|
* Extract entities from the transcript message and add them to the message.
|
|
@@ -58,9 +83,9 @@ export type ExtractionFunction = FunctionDefinition<ExtractionInput, ExtractionO
|
|
|
58
83
|
export const processTranscriptMessage = async (params: ProcessTranscriptMessageProps): Promise<ExtractionOutput> => {
|
|
59
84
|
try {
|
|
60
85
|
if (params.options?.timeout && params.options.timeout > 0) {
|
|
61
|
-
return await asyncTimeout(params.executor.invoke(params.function, params.input), params.options.timeout);
|
|
86
|
+
// return await asyncTimeout(params.executor.invoke(params.function, params.input), params.options.timeout);
|
|
62
87
|
} else {
|
|
63
|
-
return await params.executor.invoke(params.function, params.input);
|
|
88
|
+
// return await params.executor.invoke(params.function, params.input);
|
|
64
89
|
}
|
|
65
90
|
} catch (error) {
|
|
66
91
|
if (params.options?.fallbackToRaw) {
|
|
@@ -73,4 +98,7 @@ export const processTranscriptMessage = async (params: ProcessTranscriptMessageP
|
|
|
73
98
|
throw error;
|
|
74
99
|
}
|
|
75
100
|
}
|
|
101
|
+
|
|
102
|
+
// TODO(dmaretskyi): Restore extraction logic.
|
|
103
|
+
return { message: params.input.message, timeElapsed: 0 };
|
|
76
104
|
};
|
package/src/extraction/index.ts
CHANGED
|
@@ -9,71 +9,76 @@ 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
|
|
12
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
13
13
|
|
|
14
14
|
import { AiToolNotFoundError, ToolExecutionService, ToolResolverService } from '@dxos/ai';
|
|
15
|
+
import { GenericToolkit } from '@dxos/ai';
|
|
15
16
|
import { todo } from '@dxos/debug';
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
import { Function, FunctionDefinition, FunctionInvocationService } from '@dxos/functions';
|
|
17
|
+
import { Ref } from '@dxos/echo';
|
|
18
|
+
import { FunctionInvocationService, FunctionNotFoundError } from '@dxos/functions';
|
|
19
19
|
import { invariant } from '@dxos/invariant';
|
|
20
|
+
import { Operation, OperationRegistry } from '@dxos/operation';
|
|
21
|
+
import * as Option from 'effect/Option';
|
|
20
22
|
|
|
21
23
|
import { RefFromLLM } from '../types';
|
|
22
24
|
|
|
23
25
|
/**
|
|
24
26
|
* Constructs a `ToolResolverService` whose `resolve(id)` looks up tools in the following order:
|
|
25
27
|
* 1. Toolkit: return an existing tool from the provided `toolkit` if one is present under `id`.
|
|
26
|
-
* 2.
|
|
27
|
-
*
|
|
28
|
-
* 3. Functions passed in: fall back to a matching `FunctionDefinition` from the `functions` array.
|
|
28
|
+
* 2. Operations in DB: query by `key=id`; if found, deserialize and project to a tool.
|
|
29
|
+
* 3. Registered operations: fall back to a matching `Operation.Definition` from the handler set.
|
|
29
30
|
*
|
|
30
31
|
* If none of the above yield a match, the effect fails with `AiToolNotFoundError`.
|
|
31
32
|
*
|
|
32
33
|
* Requires `Database.Service` in the environment.
|
|
34
|
+
*
|
|
35
|
+
* @deprecated Moving to operation-based services.
|
|
33
36
|
*/
|
|
34
|
-
export const makeToolResolverFromFunctions = (
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
37
|
+
export const makeToolResolverFromFunctions = (): Layer.Layer<
|
|
38
|
+
ToolResolverService,
|
|
39
|
+
never,
|
|
40
|
+
GenericToolkit.GenericToolkitProvider | FunctionInvocationService
|
|
41
|
+
> => {
|
|
38
42
|
return Layer.effect(
|
|
39
43
|
ToolResolverService,
|
|
40
44
|
Effect.gen(function* () {
|
|
41
|
-
const
|
|
45
|
+
const toolkitProvider = yield* GenericToolkit.GenericToolkitProvider;
|
|
46
|
+
const functionInvocationService = yield* FunctionInvocationService;
|
|
42
47
|
return {
|
|
43
48
|
resolve: (id): Effect.Effect<Tool.Any, AiToolNotFoundError> =>
|
|
44
49
|
Effect.gen(function* () {
|
|
45
|
-
|
|
50
|
+
const toolkit = toolkitProvider.getToolkit();
|
|
46
51
|
|
|
47
|
-
const tool = toolkit.tools[id];
|
|
52
|
+
const tool = toolkit.toolkit.tools[id];
|
|
48
53
|
if (tool) {
|
|
49
54
|
return tool;
|
|
50
55
|
}
|
|
51
56
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if (!functionDef) {
|
|
59
|
-
return yield* Effect.fail(new AiToolNotFoundError(id));
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return projectFunctionToTool(functionDef);
|
|
63
|
-
}).pipe(Effect.provideService(Database.Service, dbService)),
|
|
57
|
+
return yield* functionInvocationService.resolveFunction(id).pipe(
|
|
58
|
+
Effect.map(projectFunctionToTool),
|
|
59
|
+
Effect.catchTag('FunctionNotFound', () => Effect.fail(new AiToolNotFoundError(id))),
|
|
60
|
+
);
|
|
61
|
+
}),
|
|
64
62
|
} satisfies Context.Tag.Service<ToolResolverService>;
|
|
65
63
|
}),
|
|
66
64
|
);
|
|
67
65
|
};
|
|
68
66
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
): Layer.Layer<
|
|
67
|
+
/**
|
|
68
|
+
* @deprecated Moving to operation-based services.
|
|
69
|
+
*/
|
|
70
|
+
export const makeToolExecutionServiceFromFunctions = (): Layer.Layer<
|
|
71
|
+
ToolExecutionService,
|
|
72
|
+
never,
|
|
73
|
+
FunctionInvocationService | GenericToolkit.GenericToolkitProvider
|
|
74
|
+
> => {
|
|
73
75
|
return Layer.effect(
|
|
74
76
|
ToolExecutionService,
|
|
75
77
|
Effect.gen(function* () {
|
|
76
|
-
const
|
|
78
|
+
const toolkitProvider = yield* GenericToolkit.GenericToolkitProvider;
|
|
79
|
+
const toolkit = toolkitProvider.getToolkit();
|
|
80
|
+
|
|
81
|
+
const toolkitHandler = yield* toolkit.toolkit.pipe(Effect.provide(toolkit.layer));
|
|
77
82
|
invariant(isHandlerLike(toolkitHandler));
|
|
78
83
|
const functionInvocationService = yield* FunctionInvocationService;
|
|
79
84
|
|
|
@@ -91,7 +96,7 @@ export const makeToolExecutionServiceFromFunctions = (
|
|
|
91
96
|
return result;
|
|
92
97
|
}
|
|
93
98
|
|
|
94
|
-
const
|
|
99
|
+
const functionDef = getOperationFromTool(tool).pipe(Option.getOrThrow);
|
|
95
100
|
|
|
96
101
|
return yield* functionInvocationService
|
|
97
102
|
.invokeFunction(functionDef, input as any)
|
|
@@ -108,33 +113,152 @@ export const makeToolExecutionServiceFromFunctions = (
|
|
|
108
113
|
);
|
|
109
114
|
};
|
|
110
115
|
|
|
116
|
+
export const makeToolResolverFromOperations = <R = never>({
|
|
117
|
+
toolkit: extraToolkit = GenericToolkit.empty,
|
|
118
|
+
}: { toolkit?: GenericToolkit.GenericToolkit<never, never, R> } = {}): Layer.Layer<
|
|
119
|
+
ToolResolverService,
|
|
120
|
+
never,
|
|
121
|
+
GenericToolkit.GenericToolkitProvider | OperationRegistry.Service | R
|
|
122
|
+
> => {
|
|
123
|
+
return Layer.effect(
|
|
124
|
+
ToolResolverService,
|
|
125
|
+
Effect.gen(function* () {
|
|
126
|
+
const toolkitProvider = yield* GenericToolkit.GenericToolkitProvider;
|
|
127
|
+
const operationRegistry = yield* OperationRegistry.Service;
|
|
128
|
+
return {
|
|
129
|
+
resolve: (id): Effect.Effect<Tool.Any, AiToolNotFoundError> =>
|
|
130
|
+
Effect.gen(function* () {
|
|
131
|
+
const toolkit = GenericToolkit.merge(extraToolkit, toolkitProvider.getToolkit());
|
|
132
|
+
|
|
133
|
+
const tool = toolkit.toolkit.tools[id];
|
|
134
|
+
if (tool) {
|
|
135
|
+
return tool;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return yield* operationRegistry.resolve(id).pipe(
|
|
139
|
+
Effect.flatMap(
|
|
140
|
+
Option.match({
|
|
141
|
+
onSome: (_) => Effect.succeed(projectFunctionToTool(_)),
|
|
142
|
+
onNone: () => Effect.fail(new AiToolNotFoundError(id)),
|
|
143
|
+
}),
|
|
144
|
+
),
|
|
145
|
+
);
|
|
146
|
+
}),
|
|
147
|
+
} satisfies Context.Tag.Service<ToolResolverService>;
|
|
148
|
+
}),
|
|
149
|
+
);
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
export const makeToolExecutionService = <E, R>(opts: {
|
|
153
|
+
invoke: (tool: Tool.Any, input: unknown) => Effect.Effect<unknown>;
|
|
154
|
+
}): Layer.Layer<ToolExecutionService, never, GenericToolkit.GenericToolkitProvider> =>
|
|
155
|
+
Layer.effect(
|
|
156
|
+
ToolExecutionService,
|
|
157
|
+
Effect.gen(function* () {
|
|
158
|
+
const toolkitProvider = yield* GenericToolkit.GenericToolkitProvider;
|
|
159
|
+
const toolkit = toolkitProvider.getToolkit();
|
|
160
|
+
|
|
161
|
+
const toolkitHandler = yield* toolkit.toolkit.pipe(Effect.provide(toolkit.layer));
|
|
162
|
+
invariant(isHandlerLike(toolkitHandler));
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
handlersFor: (toolkit) => {
|
|
166
|
+
const makeHandler = (tool: Tool.Any): ((params: unknown) => Effect.Effect<unknown, any, any>) => {
|
|
167
|
+
return Effect.fn(`toolFunctionHandler ${tool.name}`)(function* (input: any) {
|
|
168
|
+
if (toolkitHandler.tools[tool.name]) {
|
|
169
|
+
if (Tool.isProviderDefined(tool)) {
|
|
170
|
+
throw new Error('Attempted to call a provider-defined tool');
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// TODO(wittjosiah): Everything is `never` here.
|
|
174
|
+
const { result } = yield* (toolkitHandler.handle as any)(tool.name, input);
|
|
175
|
+
return result;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return yield* opts.invoke(tool, input).pipe(Effect.catchAllDefect((defect) => Effect.fail(defect)));
|
|
179
|
+
});
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
return toolkit.of(
|
|
183
|
+
Record.map(toolkit.tools, (tool, _name) => (Tool.isUserDefined(tool) ? makeHandler(tool) : null)) as any,
|
|
184
|
+
) as any;
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
}),
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
export const makeToolExecutionServiceFromOperationInvoker = (): Layer.Layer<
|
|
191
|
+
ToolExecutionService,
|
|
192
|
+
never,
|
|
193
|
+
Operation.Service | GenericToolkit.GenericToolkitProvider
|
|
194
|
+
> => {
|
|
195
|
+
return Layer.unwrapEffect(
|
|
196
|
+
Effect.gen(function* () {
|
|
197
|
+
const operationInvoker = yield* Operation.Service;
|
|
198
|
+
|
|
199
|
+
return makeToolExecutionService({
|
|
200
|
+
invoke: (tool, input) =>
|
|
201
|
+
Effect.gen(function* () {
|
|
202
|
+
const operationDef = getOperationFromTool(tool).pipe(Option.getOrThrow);
|
|
203
|
+
|
|
204
|
+
return yield* operationInvoker.invoke(operationDef, input).pipe(Effect.orDie);
|
|
205
|
+
}),
|
|
206
|
+
});
|
|
207
|
+
}),
|
|
208
|
+
);
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* @deprecated Bridge beetwen deprecated function invocataion service and operation services until we remove function invocation service.
|
|
213
|
+
*/
|
|
214
|
+
export const functionInvocationServiceFromOperations: Layer.Layer<
|
|
215
|
+
FunctionInvocationService,
|
|
216
|
+
never,
|
|
217
|
+
OperationRegistry.Service | Operation.Service
|
|
218
|
+
> = Layer.effect(
|
|
219
|
+
FunctionInvocationService,
|
|
220
|
+
Effect.gen(function* () {
|
|
221
|
+
const operationRegistry = yield* OperationRegistry.Service;
|
|
222
|
+
const operationInvoker = yield* Operation.Service;
|
|
223
|
+
return {
|
|
224
|
+
invokeFunction: (operationDef, input) => operationInvoker.invoke(operationDef, input).pipe(Effect.orDie),
|
|
225
|
+
resolveFunction: (key) =>
|
|
226
|
+
operationRegistry.resolve(key).pipe(
|
|
227
|
+
Effect.flatten,
|
|
228
|
+
Effect.catchTag('NoSuchElementException', () => Effect.fail(new FunctionNotFoundError(key))),
|
|
229
|
+
),
|
|
230
|
+
} satisfies Context.Tag.Service<FunctionInvocationService>;
|
|
231
|
+
}),
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
export const ToolExecutionServices = Layer.mergeAll(
|
|
235
|
+
makeToolResolverFromFunctions(),
|
|
236
|
+
makeToolExecutionServiceFromFunctions(),
|
|
237
|
+
);
|
|
238
|
+
|
|
111
239
|
class FunctionToolAnnotation extends Context.Tag('@dxos/assistant/FunctionToolAnnotation')<
|
|
112
240
|
FunctionToolAnnotation,
|
|
113
|
-
{ definition:
|
|
241
|
+
{ definition: Operation.Definition.Any }
|
|
114
242
|
>() {}
|
|
115
243
|
|
|
116
|
-
const
|
|
244
|
+
export const getOperationFromTool = (tool: Tool.Any): Option.Option<Operation.Definition.Any> => {
|
|
245
|
+
return Context.getOption(FunctionToolAnnotation)(tool.annotations).pipe(Option.map(({ definition }) => definition));
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
const toolCache = new WeakMap<Operation.Definition.Any, Tool.Any>();
|
|
117
249
|
|
|
118
250
|
/**
|
|
119
|
-
* Projects
|
|
120
|
-
*
|
|
121
|
-
* @param fn The function definition to project into a tool.
|
|
122
|
-
* @param meta Optional projection metadata.
|
|
123
|
-
* @param meta.deployedFunctionId Backend deployment ID used for remote invocation when present.
|
|
124
|
-
* This is the EDGE service's function deployment identifier (not the ECHO object ID/DXN and not `FunctionDefinition.key`).
|
|
251
|
+
* Projects an `Operation.Definition` into an `AiTool`.
|
|
125
252
|
*/
|
|
126
|
-
const projectFunctionToTool = (fn:
|
|
253
|
+
const projectFunctionToTool = (fn: Operation.Definition.Any): Tool.Any => {
|
|
127
254
|
if (toolCache.has(fn)) {
|
|
128
255
|
return toolCache.get(fn)!;
|
|
129
256
|
}
|
|
130
257
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
parameters: createStructFieldsFromSchema(fn.inputSchema),
|
|
136
|
-
// TODO(dmaretskyi): Include output schema.
|
|
137
|
-
failure: Schema.Any, // TODO(dmaretskyi): Better type for the failure?
|
|
258
|
+
const tool = Tool.make(makeToolName(fn.meta.name ?? fn.meta.key), {
|
|
259
|
+
description: fn.meta.description,
|
|
260
|
+
parameters: createStructFieldsFromSchema(fn.input),
|
|
261
|
+
failure: Schema.Any,
|
|
138
262
|
}).annotate(FunctionToolAnnotation, { definition: fn });
|
|
139
263
|
toolCache.set(fn, tool);
|
|
140
264
|
return tool;
|
|
@@ -168,8 +292,33 @@ const createStructFieldsFromSchema = (schema: Schema.Schema<any, any>): Record<s
|
|
|
168
292
|
* The picked schema type decodes to the original schema type.
|
|
169
293
|
*/
|
|
170
294
|
const mapSchemaTypeForLLM = (ast: SchemaAST.AST): SchemaAST.AST => {
|
|
171
|
-
if (
|
|
172
|
-
|
|
295
|
+
if (Ref.isRefType(ast)) {
|
|
296
|
+
const description = ast.annotations.description
|
|
297
|
+
? ast.annotations.description + '\n' + RefFromLLM.ast.annotations.description
|
|
298
|
+
: (RefFromLLM.ast.annotations.description as string);
|
|
299
|
+
return RefFromLLM.annotations({ description }).ast;
|
|
300
|
+
} else if (SchemaAST.isTupleType(ast)) {
|
|
301
|
+
return new SchemaAST.TupleType(
|
|
302
|
+
ast.elements.map((t) => new SchemaAST.OptionalType(mapSchemaTypeForLLM(t.type), t.isOptional, t.annotations)),
|
|
303
|
+
ast.rest.map((t) => new SchemaAST.Type(mapSchemaTypeForLLM(t.type), t.annotations)),
|
|
304
|
+
ast.isReadonly,
|
|
305
|
+
ast.annotations,
|
|
306
|
+
);
|
|
307
|
+
} else if (SchemaAST.isTypeLiteral(ast)) {
|
|
308
|
+
return new SchemaAST.TypeLiteral(
|
|
309
|
+
ast.propertySignatures.map(
|
|
310
|
+
(p) =>
|
|
311
|
+
new SchemaAST.PropertySignature(
|
|
312
|
+
p.name,
|
|
313
|
+
mapSchemaTypeForLLM(p.type),
|
|
314
|
+
p.isOptional,
|
|
315
|
+
p.isReadonly,
|
|
316
|
+
p.annotations,
|
|
317
|
+
),
|
|
318
|
+
),
|
|
319
|
+
ast.indexSignatures,
|
|
320
|
+
ast.annotations,
|
|
321
|
+
);
|
|
173
322
|
}
|
|
174
323
|
|
|
175
324
|
return ast;
|