@dxos/assistant 0.8.4-main.72ec0f3 → 0.8.4-main.765dc60934
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/LICENSE +102 -5
- package/README.md +2 -1
- package/dist/lib/{browser → neutral}/extraction/index.mjs +33 -54
- package/dist/lib/neutral/extraction/index.mjs.map +7 -0
- package/dist/lib/neutral/index.mjs +1755 -0
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- 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-ner-function/named-entity-recognition.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction.d.ts +1065 -313
- 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/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 +13 -16
- package/dist/types/src/functions/services.d.ts.map +1 -1
- package/dist/types/src/index.d.ts +2 -5
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/request/AiRequest.d.ts +104 -0
- package/dist/types/src/request/AiRequest.d.ts.map +1 -0
- package/dist/types/src/{session → request}/artifact-diff.d.ts +1 -1
- package/dist/types/src/request/artifact-diff.d.ts.map +1 -0
- package/dist/types/src/request/format.d.ts +15 -0
- package/dist/types/src/request/format.d.ts.map +1 -0
- package/dist/types/src/request/index.d.ts +4 -0
- package/dist/types/src/request/index.d.ts.map +1 -0
- package/dist/types/src/{session → request}/observer.d.ts +4 -3
- package/dist/types/src/request/observer.d.ts.map +1 -0
- package/dist/types/src/{session → request}/version-pin.d.ts +2 -2
- package/dist/types/src/request/version-pin.d.ts.map +1 -0
- package/dist/types/src/session/AiContext.d.ts +154 -0
- package/dist/types/src/session/AiContext.d.ts.map +1 -0
- package/dist/types/src/session/AiContext.test.d.ts +2 -0
- package/dist/types/src/session/AiContext.test.d.ts.map +1 -0
- package/dist/types/src/session/AiSession.d.ts +73 -0
- package/dist/types/src/session/AiSession.d.ts.map +1 -0
- package/dist/types/src/session/index.d.ts +2 -5
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/toolkit.d.ts +10 -9
- package/dist/types/src/session/toolkit.d.ts.map +1 -1
- package/dist/types/src/templates/system.d.ts +1 -2
- package/dist/types/src/templates/system.d.ts.map +1 -1
- package/dist/types/src/util/artifact.d.ts +27 -0
- package/dist/types/src/util/artifact.d.ts.map +1 -0
- package/dist/types/src/{diff.d.ts → util/diff.d.ts} +3 -1
- package/dist/types/src/util/diff.d.ts.map +1 -0
- package/dist/types/src/{diff.test.d.ts.map → util/diff.test.d.ts.map} +1 -1
- package/dist/types/src/util/error-handling.d.ts.map +1 -0
- package/dist/types/src/{errors.d.ts → util/errors.d.ts} +24 -28
- package/dist/types/src/util/errors.d.ts.map +1 -0
- package/dist/types/src/{execution-graph.d.ts → util/execution-graph.d.ts} +2 -2
- package/dist/types/src/util/execution-graph.d.ts.map +1 -0
- package/dist/types/src/util/execution-graph.test.d.ts.map +1 -0
- package/dist/types/src/util/index.d.ts +7 -0
- package/dist/types/src/util/index.d.ts.map +1 -0
- package/dist/types/src/util/tracing.d.ts +233 -0
- package/dist/types/src/util/tracing.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +36 -44
- package/src/extraction/extraction-ner-function/index.ts +0 -1
- package/src/extraction/extraction.ts +40 -12
- package/src/extraction/index.ts +0 -1
- package/src/extraction/quotes.ts +4 -5
- package/src/functions/services.ts +115 -63
- package/src/index.ts +2 -5
- package/src/request/AiRequest.ts +348 -0
- package/src/{session → request}/artifact-diff.ts +1 -1
- package/src/{session → request}/format.ts +24 -21
- package/src/request/index.ts +8 -0
- package/src/{session → request}/observer.ts +4 -1
- package/src/{session → request}/version-pin.ts +4 -3
- package/src/session/AiContext.test.ts +53 -0
- package/src/session/AiContext.ts +403 -0
- package/src/session/AiSession.ts +324 -0
- package/src/session/index.ts +2 -5
- package/src/session/toolkit.ts +27 -28
- package/src/templates/instructions/format.tpl +0 -4
- package/src/templates/instructions/system.tpl +50 -2
- package/src/templates/system.ts +7 -6
- package/src/{types → util}/artifact.ts +29 -12
- package/src/util/diff.test.ts +74 -0
- package/src/{diff.ts → util/diff.ts} +13 -4
- package/src/{session → util}/error-handling.ts +3 -2
- package/src/util/errors.ts +15 -0
- package/src/{execution-graph.test.ts → util/execution-graph.test.ts} +3 -3
- package/src/{execution-graph.ts → util/execution-graph.ts} +22 -12
- package/src/util/index.ts +10 -0
- package/src/util/tracing.ts +57 -0
- package/dist/lib/browser/extraction/index.mjs.map +0 -7
- package/dist/lib/browser/index.mjs +0 -1290
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- 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 -1291
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/types/src/conversation/context.d.ts +0 -103
- package/dist/types/src/conversation/context.d.ts.map +0 -1
- package/dist/types/src/conversation/conversation.d.ts +0 -41
- package/dist/types/src/conversation/conversation.d.ts.map +0 -1
- package/dist/types/src/conversation/index.d.ts +0 -3
- package/dist/types/src/conversation/index.d.ts.map +0 -1
- package/dist/types/src/diff.d.ts.map +0 -1
- package/dist/types/src/errors.d.ts.map +0 -1
- package/dist/types/src/execution-graph.d.ts.map +0 -1
- package/dist/types/src/execution-graph.test.d.ts.map +0 -1
- 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 -345
- 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 -84
- package/dist/types/src/session/GenericToolkit.d.ts.map +0 -1
- package/dist/types/src/session/artifact-diff.d.ts.map +0 -1
- package/dist/types/src/session/error-handling.d.ts.map +0 -1
- package/dist/types/src/session/format.d.ts +0 -12
- package/dist/types/src/session/format.d.ts.map +0 -1
- package/dist/types/src/session/format.test.d.ts +0 -2
- package/dist/types/src/session/format.test.d.ts.map +0 -1
- package/dist/types/src/session/observer.d.ts.map +0 -1
- package/dist/types/src/session/session.d.ts +0 -51
- package/dist/types/src/session/session.d.ts.map +0 -1
- package/dist/types/src/session/session.test.d.ts +0 -2
- package/dist/types/src/session/session.test.d.ts.map +0 -1
- package/dist/types/src/session/version-pin.d.ts.map +0 -1
- package/dist/types/src/types/artifact.d.ts +0 -20
- package/dist/types/src/types/artifact.d.ts.map +0 -1
- package/dist/types/src/types/index.d.ts +0 -2
- package/dist/types/src/types/index.d.ts.map +0 -1
- package/src/conversation/context.ts +0 -186
- package/src/conversation/conversation.ts +0 -128
- package/src/conversation/index.ts +0 -6
- package/src/diff.test.ts +0 -43
- package/src/errors.ts +0 -15
- 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 -130
- package/src/session/format.test.ts +0 -61
- package/src/session/session.conversations.json +0 -1
- package/src/session/session.test.ts +0 -149
- package/src/session/session.ts +0 -213
- package/src/types/index.ts +0 -5
- /package/dist/lib/{browser → neutral}/chunk-J5LGTIGS.mjs +0 -0
- /package/dist/lib/{browser → neutral}/chunk-J5LGTIGS.mjs.map +0 -0
- /package/dist/types/src/{diff.test.d.ts → util/diff.test.d.ts} +0 -0
- /package/dist/types/src/{session → util}/error-handling.d.ts +0 -0
- /package/dist/types/src/{execution-graph.test.d.ts → util/execution-graph.test.d.ts} +0 -0
package/package.json
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/assistant",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.765dc60934",
|
|
4
4
|
"description": "Natural language interface to DXOS",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
-
"
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
11
|
+
"license": "FSL-1.1-Apache-2.0",
|
|
8
12
|
"author": "info@dxos.org",
|
|
9
13
|
"sideEffects": true,
|
|
10
14
|
"type": "module",
|
|
@@ -12,67 +16,55 @@
|
|
|
12
16
|
".": {
|
|
13
17
|
"source": "./src/index.ts",
|
|
14
18
|
"types": "./dist/types/src/index.d.ts",
|
|
15
|
-
"
|
|
16
|
-
"node": "./dist/lib/node-esm/index.mjs"
|
|
19
|
+
"default": "./dist/lib/neutral/index.mjs"
|
|
17
20
|
},
|
|
18
21
|
"./extraction": {
|
|
19
22
|
"source": "./src/extraction/index.ts",
|
|
20
23
|
"types": "./dist/types/src/extraction/index.d.ts",
|
|
21
|
-
"
|
|
22
|
-
"node": "./dist/lib/node-esm/extraction/index.mjs"
|
|
24
|
+
"default": "./dist/lib/neutral/extraction/index.mjs"
|
|
23
25
|
}
|
|
24
26
|
},
|
|
25
27
|
"types": "dist/types/src/index.d.ts",
|
|
26
|
-
"typesVersions": {
|
|
27
|
-
"*": {
|
|
28
|
-
"types": [
|
|
29
|
-
"dist/types/src/types/index.d.ts"
|
|
30
|
-
],
|
|
31
|
-
"extraction": [
|
|
32
|
-
"dist/types/src/extraction/index.d.ts"
|
|
33
|
-
]
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
28
|
"files": [
|
|
37
29
|
"dist",
|
|
38
30
|
"src"
|
|
39
31
|
],
|
|
40
32
|
"dependencies": {
|
|
41
|
-
"@automerge/automerge": "3.
|
|
42
|
-
"@effect/
|
|
43
|
-
"@effect/
|
|
44
|
-
"@effect/
|
|
45
|
-
"@
|
|
33
|
+
"@automerge/automerge": "3.2.6",
|
|
34
|
+
"@effect-atom/atom": "^0.5.1",
|
|
35
|
+
"@effect-atom/atom-react": "^0.5.0",
|
|
36
|
+
"@effect/ai": "0.33.2",
|
|
37
|
+
"@effect/experimental": "0.58.0",
|
|
38
|
+
"@effect/platform": "0.94.4",
|
|
46
39
|
"@xenova/transformers": "^2.17.2",
|
|
47
40
|
"lodash.defaultsdeep": "^4.6.1",
|
|
48
|
-
"@dxos/ai": "0.8.4-main.
|
|
49
|
-
"@dxos/async": "0.8.4-main.
|
|
50
|
-
"@dxos/
|
|
51
|
-
"@dxos/debug": "0.8.4-main.
|
|
52
|
-
"@dxos/echo
|
|
53
|
-
"@dxos/echo": "0.8.4-main.
|
|
54
|
-
"@dxos/
|
|
55
|
-
"@dxos/
|
|
56
|
-
"@dxos/
|
|
57
|
-
"@dxos/
|
|
58
|
-
"@dxos/
|
|
59
|
-
"@dxos/
|
|
60
|
-
"@dxos/
|
|
61
|
-
"@dxos/
|
|
62
|
-
"@dxos/log": "0.8.4-main.
|
|
63
|
-
"@dxos/
|
|
64
|
-
"@dxos/
|
|
65
|
-
"@dxos/schema": "0.8.4-main.
|
|
66
|
-
"@dxos/
|
|
67
|
-
"@dxos/types": "0.8.4-main.
|
|
68
|
-
"@dxos/util": "0.8.4-main.72ec0f3"
|
|
41
|
+
"@dxos/ai": "0.8.4-main.765dc60934",
|
|
42
|
+
"@dxos/async": "0.8.4-main.765dc60934",
|
|
43
|
+
"@dxos/context": "0.8.4-main.765dc60934",
|
|
44
|
+
"@dxos/debug": "0.8.4-main.765dc60934",
|
|
45
|
+
"@dxos/echo": "0.8.4-main.765dc60934",
|
|
46
|
+
"@dxos/echo-protocol": "0.8.4-main.765dc60934",
|
|
47
|
+
"@dxos/echo-db": "0.8.4-main.765dc60934",
|
|
48
|
+
"@dxos/effect": "0.8.4-main.765dc60934",
|
|
49
|
+
"@dxos/errors": "0.8.4-main.765dc60934",
|
|
50
|
+
"@dxos/functions": "0.8.4-main.765dc60934",
|
|
51
|
+
"@dxos/compute": "0.8.4-main.765dc60934",
|
|
52
|
+
"@dxos/graph": "0.8.4-main.765dc60934",
|
|
53
|
+
"@dxos/invariant": "0.8.4-main.765dc60934",
|
|
54
|
+
"@dxos/keys": "0.8.4-main.765dc60934",
|
|
55
|
+
"@dxos/log": "0.8.4-main.765dc60934",
|
|
56
|
+
"@dxos/mcp-client": "0.8.4-main.765dc60934",
|
|
57
|
+
"@dxos/node-std": "0.8.4-main.765dc60934",
|
|
58
|
+
"@dxos/schema": "0.8.4-main.765dc60934",
|
|
59
|
+
"@dxos/util": "0.8.4-main.765dc60934",
|
|
60
|
+
"@dxos/types": "0.8.4-main.765dc60934"
|
|
69
61
|
},
|
|
70
62
|
"devDependencies": {
|
|
71
63
|
"@types/lodash.defaultsdeep": "^4.6.6",
|
|
72
|
-
"effect": "3.
|
|
64
|
+
"effect": "3.20.0"
|
|
73
65
|
},
|
|
74
66
|
"peerDependencies": {
|
|
75
|
-
"effect": "
|
|
67
|
+
"effect": "3.20.0"
|
|
76
68
|
},
|
|
77
69
|
"publishConfig": {
|
|
78
70
|
"access": "public"
|
|
@@ -4,16 +4,14 @@
|
|
|
4
4
|
|
|
5
5
|
import * as Schema from 'effect/Schema';
|
|
6
6
|
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { type FunctionDefinition } from '@dxos/functions';
|
|
10
|
-
import { type FunctionExecutor } from '@dxos/functions-runtime';
|
|
7
|
+
import { Operation } from '@dxos/compute';
|
|
8
|
+
import { Obj } from '@dxos/echo';
|
|
11
9
|
import { log } from '@dxos/log';
|
|
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),
|
|
@@ -29,10 +27,9 @@ export const ExtractionOutput = Schema.Struct({
|
|
|
29
27
|
});
|
|
30
28
|
export interface ExtractionOutput extends Schema.Schema.Type<typeof ExtractionOutput> {}
|
|
31
29
|
|
|
32
|
-
export type
|
|
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,18 +46,46 @@ export type ProcessTranscriptMessageParams = {
|
|
|
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.
|
|
56
81
|
*/
|
|
57
82
|
// TODO(dmaretskyi): Move context to a vector search index.
|
|
58
|
-
export const processTranscriptMessage = async (params:
|
|
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
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
|
}),
|
|
@@ -7,72 +7,67 @@ import type * as Toolkit from '@effect/ai/Toolkit';
|
|
|
7
7
|
import * as Context from 'effect/Context';
|
|
8
8
|
import * as Effect from 'effect/Effect';
|
|
9
9
|
import * as Layer from 'effect/Layer';
|
|
10
|
+
import * as Option from 'effect/Option';
|
|
10
11
|
import * as Record from 'effect/Record';
|
|
11
12
|
import * as Schema from 'effect/Schema';
|
|
13
|
+
import * as SchemaAST from 'effect/SchemaAST';
|
|
12
14
|
|
|
13
15
|
import { AiToolNotFoundError, ToolExecutionService, ToolResolverService } from '@dxos/ai';
|
|
16
|
+
import { OpaqueToolkit } from '@dxos/ai';
|
|
17
|
+
import { Operation, OperationRegistry } from '@dxos/compute';
|
|
14
18
|
import { todo } from '@dxos/debug';
|
|
15
|
-
import {
|
|
16
|
-
import { DatabaseService } from '@dxos/echo-db';
|
|
17
|
-
import { Function, FunctionDefinition, FunctionInvocationService } from '@dxos/functions';
|
|
19
|
+
import { Ref } from '@dxos/echo';
|
|
18
20
|
import { invariant } from '@dxos/invariant';
|
|
19
21
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
* Requires `DatabaseService` in the environment.
|
|
30
|
-
*/
|
|
31
|
-
export const makeToolResolverFromFunctions = (
|
|
32
|
-
functions: FunctionDefinition.Any[],
|
|
33
|
-
toolkit: Toolkit.Toolkit<any>,
|
|
34
|
-
): Layer.Layer<ToolResolverService, never, DatabaseService> => {
|
|
22
|
+
import { RefFromLLM } from '../util';
|
|
23
|
+
|
|
24
|
+
export const makeToolResolverFromOperations = <R = never>({
|
|
25
|
+
toolkit: extraToolkit = OpaqueToolkit.empty,
|
|
26
|
+
}: { toolkit?: OpaqueToolkit.OpaqueToolkit<never, never, R> } = {}): Layer.Layer<
|
|
27
|
+
ToolResolverService,
|
|
28
|
+
never,
|
|
29
|
+
OpaqueToolkit.OpaqueToolkitProvider | OperationRegistry.Service | R
|
|
30
|
+
> => {
|
|
35
31
|
return Layer.effect(
|
|
36
32
|
ToolResolverService,
|
|
37
33
|
Effect.gen(function* () {
|
|
38
|
-
const
|
|
34
|
+
const toolkitProvider = yield* OpaqueToolkit.OpaqueToolkitProvider;
|
|
35
|
+
const operationRegistry = yield* OperationRegistry.Service;
|
|
39
36
|
return {
|
|
40
37
|
resolve: (id): Effect.Effect<Tool.Any, AiToolNotFoundError> =>
|
|
41
38
|
Effect.gen(function* () {
|
|
42
|
-
const
|
|
39
|
+
const toolkit = OpaqueToolkit.merge(extraToolkit, toolkitProvider.getToolkit());
|
|
40
|
+
|
|
41
|
+
const tool = toolkit.toolkit.tools[id];
|
|
43
42
|
if (tool) {
|
|
44
43
|
return tool;
|
|
45
44
|
}
|
|
46
45
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
return yield* Effect.fail(new AiToolNotFoundError(id));
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return projectFunctionToTool(functionDef);
|
|
60
|
-
}).pipe(Effect.provideService(DatabaseService, dbService)),
|
|
46
|
+
return yield* operationRegistry.resolve(id).pipe(
|
|
47
|
+
Effect.flatMap(
|
|
48
|
+
Option.match({
|
|
49
|
+
onSome: (_) => Effect.succeed(projectFunctionToTool(_)),
|
|
50
|
+
onNone: () => Effect.fail(new AiToolNotFoundError(id)),
|
|
51
|
+
}),
|
|
52
|
+
),
|
|
53
|
+
);
|
|
54
|
+
}),
|
|
61
55
|
} satisfies Context.Tag.Service<ToolResolverService>;
|
|
62
56
|
}),
|
|
63
57
|
);
|
|
64
58
|
};
|
|
65
59
|
|
|
66
|
-
export const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
return Layer.effect(
|
|
60
|
+
export const makeToolExecutionService = <E, R>(opts: {
|
|
61
|
+
invoke: (tool: Tool.Any, input: unknown) => Effect.Effect<unknown>;
|
|
62
|
+
}): Layer.Layer<ToolExecutionService, never, OpaqueToolkit.OpaqueToolkitProvider> =>
|
|
63
|
+
Layer.effect(
|
|
71
64
|
ToolExecutionService,
|
|
72
65
|
Effect.gen(function* () {
|
|
73
|
-
const
|
|
66
|
+
const toolkitProvider = yield* OpaqueToolkit.OpaqueToolkitProvider;
|
|
67
|
+
const toolkit = toolkitProvider.getToolkit();
|
|
68
|
+
|
|
69
|
+
const toolkitHandler = yield* toolkit.toolkit.pipe(Effect.provide(toolkit.layer));
|
|
74
70
|
invariant(isHandlerLike(toolkitHandler));
|
|
75
|
-
const functionInvocationService = yield* FunctionInvocationService;
|
|
76
71
|
|
|
77
72
|
return {
|
|
78
73
|
handlersFor: (toolkit) => {
|
|
@@ -82,16 +77,13 @@ export const makeToolExecutionServiceFromFunctions = (
|
|
|
82
77
|
if (Tool.isProviderDefined(tool)) {
|
|
83
78
|
throw new Error('Attempted to call a provider-defined tool');
|
|
84
79
|
}
|
|
80
|
+
|
|
85
81
|
// TODO(wittjosiah): Everything is `never` here.
|
|
86
82
|
const { result } = yield* (toolkitHandler.handle as any)(tool.name, input);
|
|
87
83
|
return result;
|
|
88
84
|
}
|
|
89
85
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
return yield* functionInvocationService
|
|
93
|
-
.invokeFunction(functionDef, input as any)
|
|
94
|
-
.pipe(Effect.catchAllDefect((defect) => Effect.fail(defect)));
|
|
86
|
+
return yield* opts.invoke(tool, input).pipe(Effect.catchAllDefect((defect) => Effect.fail(defect)));
|
|
95
87
|
});
|
|
96
88
|
};
|
|
97
89
|
|
|
@@ -102,35 +94,56 @@ export const makeToolExecutionServiceFromFunctions = (
|
|
|
102
94
|
};
|
|
103
95
|
}),
|
|
104
96
|
);
|
|
97
|
+
|
|
98
|
+
export const makeToolExecutionServiceFromOperationInvoker = (): Layer.Layer<
|
|
99
|
+
ToolExecutionService,
|
|
100
|
+
never,
|
|
101
|
+
Operation.Service | OpaqueToolkit.OpaqueToolkitProvider
|
|
102
|
+
> => {
|
|
103
|
+
return Layer.unwrapEffect(
|
|
104
|
+
Effect.gen(function* () {
|
|
105
|
+
const operationInvoker = yield* Operation.Service;
|
|
106
|
+
|
|
107
|
+
return makeToolExecutionService({
|
|
108
|
+
invoke: (tool, input) =>
|
|
109
|
+
Effect.gen(function* () {
|
|
110
|
+
const operationDef = getOperationFromTool(tool).pipe(Option.getOrThrow);
|
|
111
|
+
|
|
112
|
+
return yield* operationInvoker.invoke(operationDef, input).pipe(Effect.orDie);
|
|
113
|
+
}),
|
|
114
|
+
});
|
|
115
|
+
}),
|
|
116
|
+
);
|
|
105
117
|
};
|
|
106
118
|
|
|
119
|
+
export const ToolExecutionServices = Layer.mergeAll(
|
|
120
|
+
makeToolResolverFromOperations(),
|
|
121
|
+
makeToolExecutionServiceFromOperationInvoker(),
|
|
122
|
+
);
|
|
123
|
+
|
|
107
124
|
class FunctionToolAnnotation extends Context.Tag('@dxos/assistant/FunctionToolAnnotation')<
|
|
108
125
|
FunctionToolAnnotation,
|
|
109
|
-
{ definition:
|
|
126
|
+
{ definition: Operation.Definition.Any }
|
|
110
127
|
>() {}
|
|
111
128
|
|
|
112
|
-
const
|
|
129
|
+
export const getOperationFromTool = (tool: Tool.Any): Option.Option<Operation.Definition.Any> => {
|
|
130
|
+
return Context.getOption(FunctionToolAnnotation)(tool.annotations).pipe(Option.map(({ definition }) => definition));
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const toolCache = new WeakMap<Operation.Definition.Any, Tool.Any>();
|
|
113
134
|
|
|
114
135
|
/**
|
|
115
|
-
* Projects
|
|
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`).
|
|
136
|
+
* Projects an `Operation.Definition` into an `AiTool`.
|
|
121
137
|
*/
|
|
122
|
-
const projectFunctionToTool = (fn:
|
|
138
|
+
const projectFunctionToTool = (fn: Operation.Definition.Any): Tool.Any => {
|
|
123
139
|
if (toolCache.has(fn)) {
|
|
124
140
|
return toolCache.get(fn)!;
|
|
125
141
|
}
|
|
126
142
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
parameters: createStructFieldsFromSchema(fn.inputSchema),
|
|
132
|
-
// TODO(dmaretskyi): Include output schema.
|
|
133
|
-
failure: Schema.Any, // TODO(dmaretskyi): Better type for the failure?
|
|
143
|
+
const tool = Tool.make(makeToolName(fn.meta.name ?? fn.meta.key), {
|
|
144
|
+
description: fn.meta.description,
|
|
145
|
+
parameters: createStructFieldsFromSchema(fn.input),
|
|
146
|
+
failure: Schema.Any,
|
|
134
147
|
}).annotate(FunctionToolAnnotation, { definition: fn });
|
|
135
148
|
toolCache.set(fn, tool);
|
|
136
149
|
return tool;
|
|
@@ -149,7 +162,9 @@ const makeToolName = (name: string) => {
|
|
|
149
162
|
const createStructFieldsFromSchema = (schema: Schema.Schema<any, any>): Record<string, Schema.Schema<any, any>> => {
|
|
150
163
|
switch (schema.ast._tag) {
|
|
151
164
|
case 'TypeLiteral':
|
|
152
|
-
return Object.fromEntries(
|
|
165
|
+
return Object.fromEntries(
|
|
166
|
+
schema.ast.propertySignatures.map((prop) => [prop.name, Schema.make(mapSchemaTypeForLLM(prop.type))]),
|
|
167
|
+
);
|
|
153
168
|
case 'VoidKeyword':
|
|
154
169
|
return {};
|
|
155
170
|
default:
|
|
@@ -157,6 +172,43 @@ const createStructFieldsFromSchema = (schema: Schema.Schema<any, any>): Record<s
|
|
|
157
172
|
}
|
|
158
173
|
};
|
|
159
174
|
|
|
175
|
+
/**
|
|
176
|
+
* Picks an LLM-friendly schema type for the given schema AST.
|
|
177
|
+
* The picked schema type decodes to the original schema type.
|
|
178
|
+
*/
|
|
179
|
+
const mapSchemaTypeForLLM = (ast: SchemaAST.AST): SchemaAST.AST => {
|
|
180
|
+
if (Ref.isRefType(ast)) {
|
|
181
|
+
const description = ast.annotations.description
|
|
182
|
+
? ast.annotations.description + '\n' + RefFromLLM.ast.annotations.description
|
|
183
|
+
: (RefFromLLM.ast.annotations.description as string);
|
|
184
|
+
return RefFromLLM.annotations({ description }).ast;
|
|
185
|
+
} else if (SchemaAST.isTupleType(ast)) {
|
|
186
|
+
return new SchemaAST.TupleType(
|
|
187
|
+
ast.elements.map((t) => new SchemaAST.OptionalType(mapSchemaTypeForLLM(t.type), t.isOptional, t.annotations)),
|
|
188
|
+
ast.rest.map((t) => new SchemaAST.Type(mapSchemaTypeForLLM(t.type), t.annotations)),
|
|
189
|
+
ast.isReadonly,
|
|
190
|
+
ast.annotations,
|
|
191
|
+
);
|
|
192
|
+
} else if (SchemaAST.isTypeLiteral(ast)) {
|
|
193
|
+
return new SchemaAST.TypeLiteral(
|
|
194
|
+
ast.propertySignatures.map(
|
|
195
|
+
(p) =>
|
|
196
|
+
new SchemaAST.PropertySignature(
|
|
197
|
+
p.name,
|
|
198
|
+
mapSchemaTypeForLLM(p.type),
|
|
199
|
+
p.isOptional,
|
|
200
|
+
p.isReadonly,
|
|
201
|
+
p.annotations,
|
|
202
|
+
),
|
|
203
|
+
),
|
|
204
|
+
ast.indexSignatures,
|
|
205
|
+
ast.annotations,
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return ast;
|
|
210
|
+
};
|
|
211
|
+
|
|
160
212
|
const isHandlerLike = (value: unknown): value is Toolkit.WithHandler<Record<string, Tool.Any>> => {
|
|
161
213
|
return typeof (value as any).tools === 'object' && typeof (value as any).handle === 'function';
|
|
162
214
|
};
|
package/src/index.ts
CHANGED
|
@@ -2,11 +2,8 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
export * from './conversation';
|
|
6
|
-
export * from './diff';
|
|
7
|
-
export * from './errors';
|
|
8
|
-
export * from './execution-graph';
|
|
9
5
|
export * from './functions';
|
|
6
|
+
export * from './request';
|
|
10
7
|
export * from './session';
|
|
11
8
|
export * from './templates';
|
|
12
|
-
export * from './
|
|
9
|
+
export * from './util';
|