@dxos/assistant 0.8.4-main.b97322e → 0.8.4-main.bc674ce
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-7CDDG6LL.mjs +578 -0
- package/dist/lib/browser/chunk-7CDDG6LL.mjs.map +7 -0
- package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
- package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +7 -0
- package/dist/lib/browser/extraction/index.mjs +225 -0
- package/dist/lib/browser/extraction/index.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +723 -1704
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/testing/index.mjs +32 -0
- package/dist/lib/browser/testing/index.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-CC7ER6Q3.mjs +579 -0
- package/dist/lib/node-esm/chunk-CC7ER6Q3.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
- package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
- package/dist/lib/node-esm/extraction/index.mjs +226 -0
- package/dist/lib/node-esm/extraction/index.mjs.map +7 -0
- package/dist/lib/node-esm/index.mjs +723 -1705
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/testing/index.mjs +33 -0
- package/dist/lib/node-esm/testing/index.mjs.map +7 -0
- package/dist/types/src/conversation/context.d.ts +129 -0
- package/dist/types/src/conversation/context.d.ts.map +1 -0
- 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 +29 -26
- 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/conversation/index.d.ts +1 -0
- package/dist/types/src/conversation/index.d.ts.map +1 -1
- package/dist/types/src/diff.d.ts +15 -0
- package/dist/types/src/diff.d.ts.map +1 -0
- package/dist/types/src/diff.test.d.ts +2 -0
- package/dist/types/src/diff.test.d.ts.map +1 -0
- package/dist/types/src/errors.d.ts +47 -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 +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 +258 -213
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.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 +345 -270
- package/dist/types/src/extraction/extraction.d.ts.map +1 -1
- package/dist/types/src/extraction/quotes.d.ts +3 -3
- package/dist/types/src/extraction/quotes.d.ts.map +1 -1
- package/dist/types/src/functions/services.d.ts +18 -4
- 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/index.d.ts +3 -3
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/session/GenericToolkit.d.ts +85 -0
- package/dist/types/src/session/GenericToolkit.d.ts.map +1 -0
- package/dist/types/src/session/artifact-diff.d.ts +27 -0
- package/dist/types/src/session/artifact-diff.d.ts.map +1 -0
- package/dist/types/src/session/error-handling.d.ts +4 -0
- package/dist/types/src/session/error-handling.d.ts.map +1 -0
- package/dist/types/src/session/format.d.ts +12 -0
- package/dist/types/src/session/format.d.ts.map +1 -0
- package/dist/types/src/session/format.test.d.ts +2 -0
- package/dist/types/src/session/format.test.d.ts.map +1 -0
- package/dist/types/src/session/index.d.ts +4 -0
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/observer.d.ts +23 -0
- package/dist/types/src/session/observer.d.ts.map +1 -0
- package/dist/types/src/session/session.d.ts +33 -237
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/session/toolkit.d.ts +14 -0
- package/dist/types/src/session/toolkit.d.ts.map +1 -0
- package/dist/types/src/session/version-pin.d.ts +4 -4
- package/dist/types/src/session/version-pin.d.ts.map +1 -1
- package/dist/types/src/templates/index.d.ts +1 -1
- package/dist/types/src/templates/system.d.ts +18 -0
- package/dist/types/src/templates/system.d.ts.map +1 -0
- package/dist/types/src/testing/index.d.ts +1 -2
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/testing/layer.d.ts +21 -0
- package/dist/types/src/testing/layer.d.ts.map +1 -0
- package/dist/types/src/types/artifact.d.ts +28 -0
- package/dist/types/src/types/artifact.d.ts.map +1 -0
- package/dist/types/src/types/index.d.ts +2 -0
- package/dist/types/src/types/index.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +56 -33
- package/src/conversation/context.test.ts +46 -0
- package/src/conversation/context.ts +303 -0
- package/src/conversation/conversation.test.ts +65 -0
- package/src/conversation/conversation.ts +93 -64
- package/src/conversation/index.ts +1 -0
- package/src/diff.test.ts +43 -0
- package/src/diff.ts +84 -0
- package/src/errors.ts +6 -1
- 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 +10 -9
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +48 -56
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +22 -21
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +42 -43
- package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +12 -6
- package/src/extraction/extraction-ner-function/named-entity-recognition.ts +2 -0
- package/src/extraction/extraction.ts +10 -9
- package/src/extraction/quotes.test.ts +3 -3
- package/src/extraction/quotes.ts +6 -7
- package/src/functions/services.ts +138 -97
- package/src/functions.conversations.json +1 -0
- package/src/functions.test.ts +74 -0
- package/src/index.ts +3 -3
- package/src/session/GenericToolkit.ts +131 -0
- package/src/session/artifact-diff.ts +41 -0
- package/src/session/error-handling.ts +41 -0
- package/src/session/format.test.ts +124 -0
- package/src/session/format.ts +143 -0
- package/src/session/index.ts +5 -0
- package/src/session/observer.ts +61 -0
- package/src/session/session.conversations.json +1 -0
- package/src/session/session.test.ts +34 -201
- package/src/session/session.ts +146 -248
- package/src/session/toolkit.ts +39 -0
- package/src/session/version-pin.ts +8 -8
- package/src/templates/index.ts +1 -1
- package/src/templates/instructions/format.tpl +61 -0
- package/src/templates/instructions/system.tpl +48 -0
- package/src/templates/system.ts +41 -0
- package/src/testing/index.ts +2 -8
- package/src/testing/layer.ts +78 -0
- package/src/types/artifact.ts +82 -0
- package/src/types/index.ts +5 -0
- package/src/typings.d.ts +5 -0
- package/dist/types/bin/chat.d.ts +0 -2
- package/dist/types/bin/chat.d.ts.map +0 -1
- package/dist/types/src/context/binder.d.ts +0 -34
- package/dist/types/src/context/binder.d.ts.map +0 -1
- package/dist/types/src/context/binding.d.ts +0 -56
- package/dist/types/src/context/binding.d.ts.map +0 -1
- package/dist/types/src/context/index.d.ts +0 -3
- package/dist/types/src/context/index.d.ts.map +0 -1
- package/dist/types/src/research/exa.d.ts +0 -12
- package/dist/types/src/research/exa.d.ts.map +0 -1
- package/dist/types/src/research/graph.d.ts +0 -51
- package/dist/types/src/research/graph.d.ts.map +0 -1
- package/dist/types/src/research/graph.test.d.ts +0 -2
- package/dist/types/src/research/graph.test.d.ts.map +0 -1
- package/dist/types/src/research/index.d.ts +0 -4
- package/dist/types/src/research/index.d.ts.map +0 -1
- package/dist/types/src/research/research.d.ts +0 -10
- package/dist/types/src/research/research.d.ts.map +0 -1
- package/dist/types/src/research/research.test.d.ts +0 -2
- package/dist/types/src/research/research.test.d.ts.map +0 -1
- 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/templates/prompt.d.ts +0 -30
- package/dist/types/src/templates/prompt.d.ts.map +0 -1
- package/dist/types/src/testing/exa-search-1748337321991.d.ts +0 -38
- package/dist/types/src/testing/exa-search-1748337321991.d.ts.map +0 -1
- package/dist/types/src/testing/exa-search-1748337331526.d.ts +0 -37
- package/dist/types/src/testing/exa-search-1748337331526.d.ts.map +0 -1
- package/dist/types/src/testing/exa-search-1748337344119.d.ts +0 -58
- package/dist/types/src/testing/exa-search-1748337344119.d.ts.map +0 -1
- package/dist/types/src/types.d.ts +0 -13
- package/dist/types/src/types.d.ts.map +0 -1
- package/src/context/binder.ts +0 -103
- package/src/context/binding.ts +0 -32
- package/src/context/index.ts +0 -6
- package/src/research/exa.ts +0 -97
- package/src/research/graph.test.ts +0 -64
- package/src/research/graph.ts +0 -369
- package/src/research/index.ts +0 -7
- package/src/research/instructions-research.tpl +0 -98
- package/src/research/research.test.ts +0 -190
- package/src/research/research.ts +0 -87
- 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 -208
- package/src/templates/assistant.tpl +0 -111
- package/src/templates/prompt.ts +0 -45
- package/src/testing/exa-search-1748337321991.ts +0 -131
- package/src/testing/exa-search-1748337331526.ts +0 -144
- package/src/testing/exa-search-1748337344119.ts +0 -133
- package/src/typedefs.d.ts +0 -8
- package/src/types.ts +0 -47
- /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
package/package.json
CHANGED
|
@@ -1,70 +1,93 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/assistant",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.bc674ce",
|
|
4
4
|
"description": "Natural language interface to DXOS",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/dxos/dxos"
|
|
10
|
+
},
|
|
7
11
|
"license": "MIT",
|
|
8
12
|
"author": "info@dxos.org",
|
|
9
13
|
"sideEffects": true,
|
|
10
14
|
"type": "module",
|
|
11
15
|
"exports": {
|
|
12
16
|
".": {
|
|
17
|
+
"source": "./src/index.ts",
|
|
13
18
|
"types": "./dist/types/src/index.d.ts",
|
|
14
19
|
"browser": "./dist/lib/browser/index.mjs",
|
|
15
20
|
"node": "./dist/lib/node-esm/index.mjs"
|
|
21
|
+
},
|
|
22
|
+
"./extraction": {
|
|
23
|
+
"source": "./src/extraction/index.ts",
|
|
24
|
+
"types": "./dist/types/src/extraction/index.d.ts",
|
|
25
|
+
"browser": "./dist/lib/browser/extraction/index.mjs",
|
|
26
|
+
"node": "./dist/lib/node-esm/extraction/index.mjs"
|
|
27
|
+
},
|
|
28
|
+
"./testing": {
|
|
29
|
+
"source": "./src/testing/index.ts",
|
|
30
|
+
"types": "./dist/types/src/testing/index.d.ts",
|
|
31
|
+
"browser": "./dist/lib/browser/testing/index.mjs",
|
|
32
|
+
"node": "./dist/lib/node-esm/testing/index.mjs"
|
|
16
33
|
}
|
|
17
34
|
},
|
|
18
35
|
"types": "dist/types/src/index.d.ts",
|
|
36
|
+
"typesVersions": {
|
|
37
|
+
"*": {
|
|
38
|
+
"types": [
|
|
39
|
+
"dist/types/src/types/index.d.ts"
|
|
40
|
+
],
|
|
41
|
+
"extraction": [
|
|
42
|
+
"dist/types/src/extraction/index.d.ts"
|
|
43
|
+
]
|
|
44
|
+
}
|
|
45
|
+
},
|
|
19
46
|
"files": [
|
|
20
47
|
"dist",
|
|
21
48
|
"src"
|
|
22
49
|
],
|
|
23
50
|
"dependencies": {
|
|
24
|
-
"@
|
|
25
|
-
"@effect/
|
|
26
|
-
"@effect/
|
|
27
|
-
"@effect/
|
|
28
|
-
"@
|
|
51
|
+
"@automerge/automerge": "3.2.3",
|
|
52
|
+
"@effect-atom/atom-react": "^0.4.6",
|
|
53
|
+
"@effect/ai": "0.32.1",
|
|
54
|
+
"@effect/experimental": "0.57.11",
|
|
55
|
+
"@effect/platform": "0.93.6",
|
|
29
56
|
"@xenova/transformers": "^2.17.2",
|
|
30
|
-
"chalk": "^4.1.0",
|
|
31
|
-
"exa-js": "^1.5.12",
|
|
32
|
-
"handlebars": "4.7.8",
|
|
33
57
|
"lodash.defaultsdeep": "^4.6.1",
|
|
34
|
-
"@dxos/ai": "0.8.4-main.
|
|
35
|
-
"@dxos/
|
|
36
|
-
"@dxos/context": "0.8.4-main.
|
|
37
|
-
"@dxos/
|
|
38
|
-
"@dxos/
|
|
39
|
-
"@dxos/
|
|
40
|
-
"@dxos/echo-db": "0.8.4-main.
|
|
41
|
-
"@dxos/
|
|
42
|
-
"@dxos/
|
|
43
|
-
"@dxos/
|
|
44
|
-
"@dxos/
|
|
45
|
-
"@dxos/
|
|
46
|
-
"@dxos/graph": "0.8.4-main.
|
|
47
|
-
"@dxos/
|
|
48
|
-
"@dxos/
|
|
49
|
-
"@dxos/
|
|
50
|
-
"@dxos/
|
|
51
|
-
"@dxos/
|
|
52
|
-
"@dxos/
|
|
53
|
-
"@dxos/
|
|
58
|
+
"@dxos/ai": "0.8.4-main.bc674ce",
|
|
59
|
+
"@dxos/blueprints": "0.8.4-main.bc674ce",
|
|
60
|
+
"@dxos/context": "0.8.4-main.bc674ce",
|
|
61
|
+
"@dxos/async": "0.8.4-main.bc674ce",
|
|
62
|
+
"@dxos/echo": "0.8.4-main.bc674ce",
|
|
63
|
+
"@dxos/debug": "0.8.4-main.bc674ce",
|
|
64
|
+
"@dxos/echo-db": "0.8.4-main.bc674ce",
|
|
65
|
+
"@dxos/echo-protocol": "0.8.4-main.bc674ce",
|
|
66
|
+
"@dxos/effect": "0.8.4-main.bc674ce",
|
|
67
|
+
"@dxos/functions": "0.8.4-main.bc674ce",
|
|
68
|
+
"@dxos/errors": "0.8.4-main.bc674ce",
|
|
69
|
+
"@dxos/functions-runtime": "0.8.4-main.bc674ce",
|
|
70
|
+
"@dxos/graph": "0.8.4-main.bc674ce",
|
|
71
|
+
"@dxos/invariant": "0.8.4-main.bc674ce",
|
|
72
|
+
"@dxos/keys": "0.8.4-main.bc674ce",
|
|
73
|
+
"@dxos/log": "0.8.4-main.bc674ce",
|
|
74
|
+
"@dxos/schema": "0.8.4-main.bc674ce",
|
|
75
|
+
"@dxos/node-std": "0.8.4-main.bc674ce",
|
|
76
|
+
"@dxos/types": "0.8.4-main.bc674ce",
|
|
77
|
+
"@dxos/util": "0.8.4-main.bc674ce"
|
|
54
78
|
},
|
|
55
79
|
"devDependencies": {
|
|
56
80
|
"@types/lodash.defaultsdeep": "^4.6.6",
|
|
57
|
-
"effect": "3.
|
|
58
|
-
"inquirer": "^12.0.0"
|
|
81
|
+
"effect": "3.19.11"
|
|
59
82
|
},
|
|
60
83
|
"peerDependencies": {
|
|
61
|
-
"effect": "
|
|
84
|
+
"effect": "3.19.11"
|
|
62
85
|
},
|
|
63
86
|
"publishConfig": {
|
|
64
87
|
"access": "public"
|
|
65
88
|
},
|
|
66
89
|
"beast": {},
|
|
67
90
|
"scripts": {
|
|
68
|
-
"run:chat": "
|
|
91
|
+
"run:chat": "vite-node ./bin/chat.ts"
|
|
69
92
|
}
|
|
70
93
|
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import * as Schema from 'effect/Schema';
|
|
6
|
+
import { describe, it } from 'vitest';
|
|
7
|
+
|
|
8
|
+
import { Obj, Ref, Type } from '@dxos/echo';
|
|
9
|
+
import { type Queue } from '@dxos/echo-db';
|
|
10
|
+
|
|
11
|
+
import { AiContextBinder } from './context';
|
|
12
|
+
|
|
13
|
+
// TODO(burdon): Create mock queue?
|
|
14
|
+
const createMockQueue = (): Queue =>
|
|
15
|
+
({
|
|
16
|
+
query: () =>
|
|
17
|
+
({
|
|
18
|
+
subscribe: () => ({ unsubscribe: () => {} }),
|
|
19
|
+
results: [],
|
|
20
|
+
}) as any,
|
|
21
|
+
append: async () => {
|
|
22
|
+
return [] as any;
|
|
23
|
+
},
|
|
24
|
+
}) as any;
|
|
25
|
+
|
|
26
|
+
describe('AiContextBinder', () => {
|
|
27
|
+
it('should handle bind with Ref', async () => {
|
|
28
|
+
const queue = createMockQueue();
|
|
29
|
+
const binder = new AiContextBinder({ queue });
|
|
30
|
+
|
|
31
|
+
const TestSchema = Schema.Struct({}).pipe(
|
|
32
|
+
Type.object({
|
|
33
|
+
typename: 'dxos.org/type/Example',
|
|
34
|
+
version: '0.1.0',
|
|
35
|
+
}),
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
const obj = Obj.make(TestSchema, {});
|
|
39
|
+
const ref = Ref.make(obj);
|
|
40
|
+
|
|
41
|
+
await binder.bind({
|
|
42
|
+
blueprints: [],
|
|
43
|
+
objects: [ref],
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
});
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Atom, Registry } from '@effect-atom/atom-react';
|
|
6
|
+
import * as EArray from 'effect/Array';
|
|
7
|
+
import * as Context from 'effect/Context';
|
|
8
|
+
import * as Function from 'effect/Function';
|
|
9
|
+
import * as Schema from 'effect/Schema';
|
|
10
|
+
|
|
11
|
+
import { Blueprint } from '@dxos/blueprints';
|
|
12
|
+
import { Resource } from '@dxos/context';
|
|
13
|
+
import { DXN, Obj, Query, type Ref, Type } from '@dxos/echo';
|
|
14
|
+
import { type Queue } from '@dxos/echo-db';
|
|
15
|
+
import { log } from '@dxos/log';
|
|
16
|
+
import { ComplexSet, isTruthy } from '@dxos/util';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Thread message that binds or unbinds contextual objects to a conversation.
|
|
20
|
+
*/
|
|
21
|
+
// TODO(burdon): Move to @dxos/schema ContentBlock?
|
|
22
|
+
export const ContextBinding = Schema.Struct({
|
|
23
|
+
blueprints: Schema.Struct({
|
|
24
|
+
added: Schema.Array(Type.Ref(Blueprint.Blueprint)),
|
|
25
|
+
removed: Schema.Array(Type.Ref(Blueprint.Blueprint)),
|
|
26
|
+
}),
|
|
27
|
+
|
|
28
|
+
objects: Schema.Struct({
|
|
29
|
+
added: Schema.Array(Type.Ref(Type.Obj)),
|
|
30
|
+
removed: Schema.Array(Type.Ref(Type.Obj)),
|
|
31
|
+
}),
|
|
32
|
+
}).pipe(
|
|
33
|
+
Type.object({
|
|
34
|
+
typename: 'dxos.org/type/ContextBinding',
|
|
35
|
+
version: '0.1.0',
|
|
36
|
+
}),
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
export interface ContextBinding extends Schema.Schema.Type<typeof ContextBinding> {}
|
|
40
|
+
|
|
41
|
+
export type BindingProps = Partial<{
|
|
42
|
+
blueprints: Ref.Ref<Blueprint.Blueprint>[];
|
|
43
|
+
objects: Ref.Ref<Obj.Unknown>[];
|
|
44
|
+
}>;
|
|
45
|
+
|
|
46
|
+
export class Bindings {
|
|
47
|
+
readonly blueprints = new ComplexSet<Ref.Ref<Blueprint.Blueprint>>((ref) => ref.dxn.toString());
|
|
48
|
+
|
|
49
|
+
// TODO(burdon): Some DXNs have the Space prefix so only compare the object ID.
|
|
50
|
+
readonly objects = new ComplexSet<Ref.Ref<Obj.Unknown>>((ref) => ref.dxn.asEchoDXN()?.echoId);
|
|
51
|
+
|
|
52
|
+
toJSON() {
|
|
53
|
+
return {
|
|
54
|
+
blueprints: EArray.fromIterable(this.blueprints).map((ref) => ref.dxn.toString()),
|
|
55
|
+
objects: EArray.fromIterable(this.objects).map((ref) => ref.dxn.toString()),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export type AiContextBinderOptions = {
|
|
61
|
+
queue: Queue;
|
|
62
|
+
registry?: Registry.Registry;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Manages bindings of blueprints and objects to a conversation.
|
|
67
|
+
*/
|
|
68
|
+
// TODO(burdon): Context should manage ephemeral state of bindings until prompt is issued?
|
|
69
|
+
export class AiContextBinder extends Resource {
|
|
70
|
+
private readonly _blueprints = Atom.make<Blueprint.Blueprint[]>([]).pipe(Atom.keepAlive);
|
|
71
|
+
private readonly _objects = Atom.make<Obj.Unknown[]>([]).pipe(Atom.keepAlive);
|
|
72
|
+
private readonly _registry: Registry.Registry;
|
|
73
|
+
private readonly _queue: Queue;
|
|
74
|
+
|
|
75
|
+
constructor(options: AiContextBinderOptions) {
|
|
76
|
+
super();
|
|
77
|
+
this._queue = options.queue;
|
|
78
|
+
this._registry = options.registry ?? Registry.make();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Returns the blueprints atom for subscription.
|
|
83
|
+
*/
|
|
84
|
+
get blueprints(): Atom.Atom<Blueprint.Blueprint[]> {
|
|
85
|
+
return this._blueprints;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Returns the objects atom for subscription.
|
|
90
|
+
*/
|
|
91
|
+
get objects(): Atom.Atom<Obj.Unknown[]> {
|
|
92
|
+
return this._objects;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Gets the current blueprints value.
|
|
97
|
+
*/
|
|
98
|
+
getBlueprints(): Blueprint.Blueprint[] {
|
|
99
|
+
return this._registry.get(this._blueprints);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Gets the current objects value.
|
|
104
|
+
*/
|
|
105
|
+
getObjects(): Obj.Unknown[] {
|
|
106
|
+
return this._registry.get(this._objects);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Subscribe to changes in blueprints.
|
|
111
|
+
*/
|
|
112
|
+
subscribeBlueprints(cb: (blueprints: Blueprint.Blueprint[]) => void): () => void {
|
|
113
|
+
return this._registry.subscribe(this._blueprints, () => cb(this._registry.get(this._blueprints)));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Subscribe to changes in objects.
|
|
118
|
+
*/
|
|
119
|
+
subscribeObjects(cb: (objects: Obj.Unknown[]) => void): () => void {
|
|
120
|
+
return this._registry.subscribe(this._objects, () => cb(this._registry.get(this._objects)));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
protected override async _open(): Promise<void> {
|
|
124
|
+
const query = this._queue.query(Query.type(ContextBinding));
|
|
125
|
+
|
|
126
|
+
// Process initial state before returning.
|
|
127
|
+
const initialResults = await query.run();
|
|
128
|
+
await this._updateBindings(initialResults);
|
|
129
|
+
|
|
130
|
+
// Subscribe to future changes.
|
|
131
|
+
this._ctx.onDispose(
|
|
132
|
+
query.subscribe(async () => {
|
|
133
|
+
await this._updateBindings(query.results);
|
|
134
|
+
}),
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
private async _updateBindings(items: ContextBinding[]): Promise<void> {
|
|
139
|
+
// Skip update if no items - preserve existing state set by bind().
|
|
140
|
+
if (items.length === 0) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const bindings = this._reduce(items);
|
|
145
|
+
|
|
146
|
+
// Resolve references (loading them first if needed).
|
|
147
|
+
const currentBlueprints = this._registry.get(this._blueprints);
|
|
148
|
+
const currentObjects = this._registry.get(this._objects);
|
|
149
|
+
const newBlueprints = await this._resolve(bindings.blueprints, currentBlueprints);
|
|
150
|
+
const newObjects = await this._resolve(bindings.objects, currentObjects);
|
|
151
|
+
|
|
152
|
+
// Atomic updates - subscribers notified automatically.
|
|
153
|
+
this._registry.set(this._blueprints, newBlueprints);
|
|
154
|
+
this._registry.set(this._objects, newObjects);
|
|
155
|
+
|
|
156
|
+
log('updated bindings', {
|
|
157
|
+
blueprints: newBlueprints.length,
|
|
158
|
+
objects: newObjects.length,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
protected override async _close(): Promise<void> {
|
|
163
|
+
// Reset atoms to empty state.
|
|
164
|
+
this._registry.set(this._blueprints, []);
|
|
165
|
+
this._registry.set(this._objects, []);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
async bind({ blueprints, objects }: BindingProps): Promise<void> {
|
|
169
|
+
const currentBlueprints = this._registry.get(this._blueprints);
|
|
170
|
+
const currentObjects = this._registry.get(this._objects);
|
|
171
|
+
|
|
172
|
+
const { added: addedBlueprints, next: nextBlueprints } = this._processBindings(blueprints, currentBlueprints);
|
|
173
|
+
const { added: addedObjects, next: nextObjects } = this._processBindings(objects, currentObjects);
|
|
174
|
+
if (!addedBlueprints.length && !addedObjects.length) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Atomic updates - subscribers notified automatically.
|
|
179
|
+
this._registry.set(this._blueprints, nextBlueprints);
|
|
180
|
+
this._registry.set(this._objects, nextObjects);
|
|
181
|
+
|
|
182
|
+
log('bind', { blueprints: addedBlueprints.length, objects: addedObjects.length });
|
|
183
|
+
await this._queue.append([
|
|
184
|
+
Obj.make(ContextBinding, {
|
|
185
|
+
blueprints: {
|
|
186
|
+
added: addedBlueprints,
|
|
187
|
+
removed: [],
|
|
188
|
+
},
|
|
189
|
+
objects: {
|
|
190
|
+
added: addedObjects,
|
|
191
|
+
removed: [],
|
|
192
|
+
},
|
|
193
|
+
}),
|
|
194
|
+
]);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async unbind({ blueprints, objects }: BindingProps): Promise<void> {
|
|
198
|
+
if (!blueprints?.length && !objects?.length) {
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
log('unbind', { blueprints: blueprints?.length, objects: objects?.length });
|
|
203
|
+
await this._queue.append([
|
|
204
|
+
Obj.make(ContextBinding, {
|
|
205
|
+
blueprints: {
|
|
206
|
+
added: [],
|
|
207
|
+
removed: blueprints ?? [],
|
|
208
|
+
},
|
|
209
|
+
objects: {
|
|
210
|
+
added: [],
|
|
211
|
+
removed: objects ?? [],
|
|
212
|
+
},
|
|
213
|
+
}),
|
|
214
|
+
]);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Process bindings to filter duplicates and determine next state.
|
|
219
|
+
*/
|
|
220
|
+
private _processBindings<T extends Obj.Unknown>(
|
|
221
|
+
refs: Ref.Ref<T>[] | undefined,
|
|
222
|
+
current: T[],
|
|
223
|
+
): { added: Ref.Ref<T>[]; next: T[] } {
|
|
224
|
+
const next = [...current];
|
|
225
|
+
const added: Ref.Ref<T>[] = [];
|
|
226
|
+
if (!refs?.length) {
|
|
227
|
+
return { added, next };
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
const seen = new Set(current.map((obj) => Obj.getDXN(obj).toString()));
|
|
231
|
+
for (const ref of refs) {
|
|
232
|
+
const dxn = ref.dxn.toString();
|
|
233
|
+
if (!seen.has(dxn)) {
|
|
234
|
+
seen.add(dxn);
|
|
235
|
+
added.push(ref);
|
|
236
|
+
|
|
237
|
+
// Only resolve target if available (has target or resolver).
|
|
238
|
+
if (ref.isAvailable) {
|
|
239
|
+
const target = ref.target;
|
|
240
|
+
if (target) {
|
|
241
|
+
next.push(target);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return { added, next };
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Reduce results into sets of blueprints and objects.
|
|
252
|
+
*/
|
|
253
|
+
private _reduce(items: ContextBinding[]): Bindings {
|
|
254
|
+
return Function.pipe(
|
|
255
|
+
items,
|
|
256
|
+
EArray.reduce(new Bindings(), (context, { blueprints, objects }) => {
|
|
257
|
+
blueprints.added.forEach((ref) => context.blueprints.add(ref));
|
|
258
|
+
blueprints.removed.forEach((ref) => context.blueprints.delete(ref));
|
|
259
|
+
|
|
260
|
+
objects.added.forEach((ref) => context.objects.add(ref));
|
|
261
|
+
objects.removed.forEach((ref) => {
|
|
262
|
+
for (const obj of context.objects) {
|
|
263
|
+
if (DXN.equalsEchoId(obj.dxn, ref.dxn)) {
|
|
264
|
+
context.objects.delete(obj);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
return context;
|
|
270
|
+
}),
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Resolve references to objects, loading them first if needed and falling back to existing objects.
|
|
276
|
+
*/
|
|
277
|
+
private async _resolve<T>(refs: Iterable<Ref.Ref<T>>, current: T[]): Promise<T[]> {
|
|
278
|
+
const refArray = [...refs];
|
|
279
|
+
|
|
280
|
+
// Load all refs that need loading.
|
|
281
|
+
await Promise.all(refArray.map((ref) => ref.load()));
|
|
282
|
+
|
|
283
|
+
return refArray
|
|
284
|
+
.map((ref) => {
|
|
285
|
+
let target: T | undefined;
|
|
286
|
+
// Only resolve target if available (has target or resolver).
|
|
287
|
+
if (ref.isAvailable) {
|
|
288
|
+
target = ref.target;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// Fallback to existing object.
|
|
292
|
+
return target ?? current.find((obj) => Obj.getDXN(obj as any).toString() === ref.dxn.toString());
|
|
293
|
+
})
|
|
294
|
+
.filter(isTruthy);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export class AiContextService extends Context.Tag('@dxos/assistant/AiContextService')<
|
|
299
|
+
AiContextService,
|
|
300
|
+
{
|
|
301
|
+
binder: AiContextBinder;
|
|
302
|
+
}
|
|
303
|
+
>() {}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { it } from '@effect/vitest';
|
|
6
|
+
import * as Effect from 'effect/Effect';
|
|
7
|
+
import { afterEach, beforeEach, describe, expect } from 'vitest';
|
|
8
|
+
|
|
9
|
+
import { Blueprint } from '@dxos/blueprints';
|
|
10
|
+
import { Obj, Ref } from '@dxos/echo';
|
|
11
|
+
import { EchoTestBuilder } from '@dxos/echo-db/testing';
|
|
12
|
+
|
|
13
|
+
import { ContextBinding } from './context';
|
|
14
|
+
import { AiConversation } from './conversation';
|
|
15
|
+
|
|
16
|
+
describe('AiConversation', () => {
|
|
17
|
+
let builder: EchoTestBuilder;
|
|
18
|
+
|
|
19
|
+
beforeEach(async () => {
|
|
20
|
+
builder = await new EchoTestBuilder().open();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
afterEach(async () => {
|
|
24
|
+
await builder.close();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it.effect('loads blueprints on open', () =>
|
|
28
|
+
Effect.gen(function* () {
|
|
29
|
+
const peer = yield* Effect.promise(() => builder.createPeer({ types: [Blueprint.Blueprint, ContextBinding] }));
|
|
30
|
+
const db = yield* Effect.promise(() => peer.createDatabase());
|
|
31
|
+
const queues = peer.client.constructQueueFactory(db.spaceId);
|
|
32
|
+
const queue = queues.create<ContextBinding>();
|
|
33
|
+
|
|
34
|
+
// Create blueprint.
|
|
35
|
+
const blueprint = db.add(
|
|
36
|
+
Blueprint.make({
|
|
37
|
+
key: 'example.com/blueprint/Test',
|
|
38
|
+
name: 'Test Blueprint',
|
|
39
|
+
}),
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
// Add blueprint to queue via binding.
|
|
43
|
+
yield* Effect.promise(() =>
|
|
44
|
+
queue.append([
|
|
45
|
+
Obj.make(ContextBinding, {
|
|
46
|
+
blueprints: {
|
|
47
|
+
added: [Ref.make(blueprint)],
|
|
48
|
+
removed: [],
|
|
49
|
+
},
|
|
50
|
+
objects: {
|
|
51
|
+
added: [],
|
|
52
|
+
removed: [],
|
|
53
|
+
},
|
|
54
|
+
}),
|
|
55
|
+
]),
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
const conversation = new AiConversation({ queue });
|
|
59
|
+
yield* Effect.promise(() => conversation.open());
|
|
60
|
+
|
|
61
|
+
expect(conversation.context.getBlueprints()).toHaveLength(1);
|
|
62
|
+
expect(conversation.context.getObjects()).toHaveLength(0);
|
|
63
|
+
}),
|
|
64
|
+
);
|
|
65
|
+
});
|