@dxos/assistant 0.8.4-main.c4373fc → 0.8.4-main.c85a9c8dae
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-BE6SRJ4N.mjs +193 -0
- package/dist/lib/neutral/chunk-BE6SRJ4N.mjs.map +7 -0
- package/dist/lib/neutral/extraction/index.mjs +223 -0
- package/dist/lib/neutral/extraction/index.mjs.map +7 -0
- package/dist/lib/neutral/index.mjs +1328 -0
- package/dist/lib/neutral/index.mjs.map +7 -0
- package/dist/lib/neutral/meta.json +1 -0
- package/dist/lib/neutral/testing/index.mjs +38 -0
- package/dist/lib/neutral/testing/index.mjs.map +7 -0
- package/dist/types/src/conversation/context.d.ts +109 -60
- package/dist/types/src/conversation/context.d.ts.map +1 -1
- package/dist/types/src/conversation/context.test.d.ts +2 -0
- package/dist/types/src/conversation/context.test.d.ts.map +1 -0
- package/dist/types/src/conversation/conversation.d.ts +41 -16
- package/dist/types/src/conversation/conversation.d.ts.map +1 -1
- package/dist/types/src/conversation/conversation.test.d.ts +2 -0
- package/dist/types/src/conversation/conversation.test.d.ts.map +1 -0
- package/dist/types/src/diff.d.ts +3 -1
- package/dist/types/src/diff.d.ts.map +1 -1
- package/dist/types/src/errors.d.ts +22 -30
- package/dist/types/src/errors.d.ts.map +1 -1
- package/dist/types/src/execution-graph.d.ts +5 -2
- package/dist/types/src/execution-graph.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +282 -268
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
- package/dist/types/src/extraction/extraction.d.ts +348 -303
- package/dist/types/src/extraction/extraction.d.ts.map +1 -1
- package/dist/types/src/extraction/quotes.d.ts +2 -2
- package/dist/types/src/extraction/quotes.d.ts.map +1 -1
- package/dist/types/src/functions/services.d.ts +6 -6
- package/dist/types/src/functions/services.d.ts.map +1 -1
- package/dist/types/src/functions.test.d.ts +2 -0
- package/dist/types/src/functions.test.d.ts.map +1 -0
- package/dist/types/src/session/artifact-diff.d.ts +1 -1
- package/dist/types/src/session/artifact-diff.d.ts.map +1 -1
- package/dist/types/src/session/format.d.ts +4 -176
- package/dist/types/src/session/format.d.ts.map +1 -1
- package/dist/types/src/session/index.d.ts +1 -0
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/observer.d.ts +6 -5
- package/dist/types/src/session/observer.d.ts.map +1 -1
- package/dist/types/src/session/session.d.ts +25 -15
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/session/toolkit.d.ts +9 -6
- package/dist/types/src/session/toolkit.d.ts.map +1 -1
- package/dist/types/src/session/version-pin.d.ts +3 -3
- package/dist/types/src/session/version-pin.d.ts.map +1 -1
- package/dist/types/src/testing/index.d.ts +2 -0
- package/dist/types/src/testing/index.d.ts.map +1 -0
- package/dist/types/src/testing/layer.d.ts +22 -0
- package/dist/types/src/testing/layer.d.ts.map +1 -0
- package/dist/types/src/types/artifact.d.ts +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 +41 -31
- package/src/conversation/context.test.ts +46 -0
- package/src/conversation/context.ts +223 -49
- package/src/conversation/conversation.test.ts +65 -0
- package/src/conversation/conversation.ts +179 -53
- package/src/diff.test.ts +37 -6
- package/src/diff.ts +18 -4
- package/src/errors.ts +2 -2
- package/src/execution-graph.test.ts +46 -44
- package/src/execution-graph.ts +25 -16
- package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +2 -2
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +44 -54
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +15 -14
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +36 -44
- package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +10 -5
- package/src/extraction/extraction.ts +9 -8
- package/src/extraction/quotes.ts +4 -5
- package/src/functions/services.ts +88 -37
- package/src/functions.conversations.json +1 -0
- package/src/functions.test.ts +74 -0
- package/src/session/artifact-diff.ts +1 -1
- package/src/session/format.test.ts +103 -39
- package/src/session/format.ts +17 -16
- package/src/session/index.ts +1 -0
- package/src/session/observer.ts +6 -3
- package/src/session/session.conversations.json +1 -1
- package/src/session/session.test.ts +60 -18
- package/src/session/session.ts +99 -60
- package/src/session/toolkit.ts +21 -25
- package/src/session/version-pin.ts +6 -6
- package/src/templates/{format.tpl → instructions/format.tpl} +1 -4
- package/src/templates/system.ts +2 -2
- package/src/testing/index.ts +5 -0
- package/src/testing/layer.ts +85 -0
- package/src/{types.ts → types/artifact.ts} +43 -6
- package/src/types/index.ts +5 -0
- package/dist/lib/browser/chunk-CWGVG235.mjs +0 -380
- package/dist/lib/browser/chunk-CWGVG235.mjs.map +0 -7
- package/dist/lib/browser/chunk-UB26N5UF.mjs +0 -89
- package/dist/lib/browser/chunk-UB26N5UF.mjs.map +0 -7
- package/dist/lib/browser/extraction/index.mjs +0 -272
- package/dist/lib/browser/extraction/index.mjs.map +0 -7
- package/dist/lib/browser/index.mjs +0 -869
- package/dist/lib/browser/index.mjs.map +0 -7
- package/dist/lib/browser/meta.json +0 -1
- package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs +0 -13
- package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-BCFXTNSK.mjs +0 -91
- package/dist/lib/node-esm/chunk-BCFXTNSK.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-OV5PMQPB.mjs +0 -382
- package/dist/lib/node-esm/chunk-OV5PMQPB.mjs.map +0 -7
- package/dist/lib/node-esm/extraction/index.mjs +0 -273
- package/dist/lib/node-esm/extraction/index.mjs.map +0 -7
- package/dist/lib/node-esm/index.mjs +0 -870
- package/dist/lib/node-esm/index.mjs.map +0 -7
- package/dist/lib/node-esm/meta.json +0 -1
- package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs +0 -14
- package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs.map +0 -7
- package/dist/types/src/summarization/summarization.test.d.ts +0 -2
- package/dist/types/src/summarization/summarization.test.d.ts.map +0 -1
- package/dist/types/src/types.d.ts +0 -16
- package/dist/types/src/types.d.ts.map +0 -1
- package/src/summarization/summarization.test.ts +0 -202
- /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
- /package/src/templates/{system.tpl → instructions/system.tpl} +0 -0
package/package.json
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/assistant",
|
|
3
|
-
"version": "0.8.4-main.
|
|
3
|
+
"version": "0.8.4-main.c85a9c8dae",
|
|
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,
|
|
@@ -12,14 +16,17 @@
|
|
|
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
|
-
|
|
24
|
+
"default": "./dist/lib/neutral/extraction/index.mjs"
|
|
25
|
+
},
|
|
26
|
+
"./testing": {
|
|
27
|
+
"source": "./src/testing/index.ts",
|
|
28
|
+
"types": "./dist/types/src/testing/index.d.ts",
|
|
29
|
+
"default": "./dist/lib/neutral/testing/index.mjs"
|
|
23
30
|
}
|
|
24
31
|
},
|
|
25
32
|
"types": "dist/types/src/index.d.ts",
|
|
@@ -38,39 +45,42 @@
|
|
|
38
45
|
"src"
|
|
39
46
|
],
|
|
40
47
|
"dependencies": {
|
|
41
|
-
"@automerge/automerge": "3.
|
|
42
|
-
"@effect/
|
|
43
|
-
"@effect/
|
|
44
|
-
"@effect/
|
|
45
|
-
"@
|
|
48
|
+
"@automerge/automerge": "3.2.3",
|
|
49
|
+
"@effect-atom/atom": "^0.5.1",
|
|
50
|
+
"@effect-atom/atom-react": "^0.5.0",
|
|
51
|
+
"@effect/ai": "0.33.2",
|
|
52
|
+
"@effect/experimental": "0.58.0",
|
|
53
|
+
"@effect/platform": "0.94.4",
|
|
46
54
|
"@xenova/transformers": "^2.17.2",
|
|
47
55
|
"lodash.defaultsdeep": "^4.6.1",
|
|
48
|
-
"@dxos/
|
|
49
|
-
"@dxos/
|
|
50
|
-
"@dxos/
|
|
51
|
-
"@dxos/
|
|
52
|
-
"@dxos/
|
|
53
|
-
"@dxos/
|
|
54
|
-
"@dxos/echo-
|
|
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/node-std": "0.8.4-main.
|
|
66
|
-
"@dxos/
|
|
56
|
+
"@dxos/ai": "0.8.4-main.c85a9c8dae",
|
|
57
|
+
"@dxos/context": "0.8.4-main.c85a9c8dae",
|
|
58
|
+
"@dxos/debug": "0.8.4-main.c85a9c8dae",
|
|
59
|
+
"@dxos/echo-db": "0.8.4-main.c85a9c8dae",
|
|
60
|
+
"@dxos/echo": "0.8.4-main.c85a9c8dae",
|
|
61
|
+
"@dxos/async": "0.8.4-main.c85a9c8dae",
|
|
62
|
+
"@dxos/echo-protocol": "0.8.4-main.c85a9c8dae",
|
|
63
|
+
"@dxos/effect": "0.8.4-main.c85a9c8dae",
|
|
64
|
+
"@dxos/errors": "0.8.4-main.c85a9c8dae",
|
|
65
|
+
"@dxos/functions": "0.8.4-main.c85a9c8dae",
|
|
66
|
+
"@dxos/blueprints": "0.8.4-main.c85a9c8dae",
|
|
67
|
+
"@dxos/functions-runtime": "0.8.4-main.c85a9c8dae",
|
|
68
|
+
"@dxos/invariant": "0.8.4-main.c85a9c8dae",
|
|
69
|
+
"@dxos/keys": "0.8.4-main.c85a9c8dae",
|
|
70
|
+
"@dxos/log": "0.8.4-main.c85a9c8dae",
|
|
71
|
+
"@dxos/mcp-client": "0.8.4-main.c85a9c8dae",
|
|
72
|
+
"@dxos/schema": "0.8.4-main.c85a9c8dae",
|
|
73
|
+
"@dxos/node-std": "0.8.4-main.c85a9c8dae",
|
|
74
|
+
"@dxos/graph": "0.8.4-main.c85a9c8dae",
|
|
75
|
+
"@dxos/types": "0.8.4-main.c85a9c8dae",
|
|
76
|
+
"@dxos/util": "0.8.4-main.c85a9c8dae"
|
|
67
77
|
},
|
|
68
78
|
"devDependencies": {
|
|
69
79
|
"@types/lodash.defaultsdeep": "^4.6.6",
|
|
70
|
-
"effect": "3.
|
|
80
|
+
"effect": "3.19.16"
|
|
71
81
|
},
|
|
72
82
|
"peerDependencies": {
|
|
73
|
-
"effect": "
|
|
83
|
+
"effect": "3.19.16"
|
|
74
84
|
},
|
|
75
85
|
"publishConfig": {
|
|
76
86
|
"access": "public"
|
|
@@ -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
|
+
});
|
|
@@ -2,16 +2,20 @@
|
|
|
2
2
|
// Copyright 2025 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import * as
|
|
5
|
+
import { Atom, Registry } from '@effect-atom/atom-react';
|
|
6
|
+
import * as EArray from 'effect/Array';
|
|
7
7
|
import * as Context from 'effect/Context';
|
|
8
|
+
import * as Effect from 'effect/Effect';
|
|
8
9
|
import * as Function from 'effect/Function';
|
|
9
10
|
import * as Schema from 'effect/Schema';
|
|
10
11
|
|
|
11
12
|
import { Blueprint } from '@dxos/blueprints';
|
|
12
|
-
import {
|
|
13
|
+
import { Resource } from '@dxos/context';
|
|
14
|
+
import { DXN, Obj, Query, Ref, Type } from '@dxos/echo';
|
|
13
15
|
import { type Queue } from '@dxos/echo-db';
|
|
14
|
-
import {
|
|
16
|
+
import { assertArgument } from '@dxos/invariant';
|
|
17
|
+
import { log } from '@dxos/log';
|
|
18
|
+
import { ComplexSet, isTruthy } from '@dxos/util';
|
|
15
19
|
|
|
16
20
|
/**
|
|
17
21
|
* Thread message that binds or unbinds contextual objects to a conversation.
|
|
@@ -19,17 +23,16 @@ import { ComplexSet } from '@dxos/util';
|
|
|
19
23
|
// TODO(burdon): Move to @dxos/schema ContentBlock?
|
|
20
24
|
export const ContextBinding = Schema.Struct({
|
|
21
25
|
blueprints: Schema.Struct({
|
|
22
|
-
added: Schema.Array(
|
|
23
|
-
removed: Schema.Array(
|
|
26
|
+
added: Schema.Array(Ref.Ref(Blueprint.Blueprint)),
|
|
27
|
+
removed: Schema.Array(Ref.Ref(Blueprint.Blueprint)),
|
|
24
28
|
}),
|
|
25
29
|
|
|
26
|
-
// TODO(burdon): Type.Expando => Type.Obj (or Obj.Any?)
|
|
27
30
|
objects: Schema.Struct({
|
|
28
|
-
added: Schema.Array(
|
|
29
|
-
removed: Schema.Array(
|
|
31
|
+
added: Schema.Array(Ref.Ref(Obj.Unknown)),
|
|
32
|
+
removed: Schema.Array(Ref.Ref(Obj.Unknown)),
|
|
30
33
|
}),
|
|
31
34
|
}).pipe(
|
|
32
|
-
Type.
|
|
35
|
+
Type.object({
|
|
33
36
|
typename: 'dxos.org/type/ContextBinding',
|
|
34
37
|
version: '0.1.0',
|
|
35
38
|
}),
|
|
@@ -39,113 +42,260 @@ export interface ContextBinding extends Schema.Schema.Type<typeof ContextBinding
|
|
|
39
42
|
|
|
40
43
|
export type BindingProps = Partial<{
|
|
41
44
|
blueprints: Ref.Ref<Blueprint.Blueprint>[];
|
|
42
|
-
objects: Ref.Ref<
|
|
45
|
+
objects: Ref.Ref<Obj.Unknown>[];
|
|
43
46
|
}>;
|
|
44
47
|
|
|
45
48
|
export class Bindings {
|
|
46
49
|
readonly blueprints = new ComplexSet<Ref.Ref<Blueprint.Blueprint>>((ref) => ref.dxn.toString());
|
|
50
|
+
|
|
47
51
|
// TODO(burdon): Some DXNs have the Space prefix so only compare the object ID.
|
|
48
|
-
readonly objects = new ComplexSet<Ref.Ref<
|
|
52
|
+
readonly objects = new ComplexSet<Ref.Ref<Obj.Unknown>>((ref) => ref.dxn.asEchoDXN()?.echoId);
|
|
49
53
|
|
|
50
54
|
toJSON() {
|
|
51
55
|
return {
|
|
52
|
-
blueprints:
|
|
53
|
-
objects:
|
|
56
|
+
blueprints: EArray.fromIterable(this.blueprints).map((ref) => ref.dxn.toString()),
|
|
57
|
+
objects: EArray.fromIterable(this.objects).map((ref) => ref.dxn.toString()),
|
|
54
58
|
};
|
|
55
59
|
}
|
|
56
60
|
}
|
|
57
61
|
|
|
62
|
+
export type AiContextBinderOptions = {
|
|
63
|
+
queue: Queue;
|
|
64
|
+
registry?: Registry.Registry;
|
|
65
|
+
};
|
|
66
|
+
|
|
58
67
|
/**
|
|
59
68
|
* Manages bindings of blueprints and objects to a conversation.
|
|
60
69
|
*/
|
|
61
70
|
// TODO(burdon): Context should manage ephemeral state of bindings until prompt is issued?
|
|
62
|
-
export class AiContextBinder {
|
|
71
|
+
export class AiContextBinder extends Resource {
|
|
72
|
+
private readonly _blueprints = Atom.make<Blueprint.Blueprint[]>([]).pipe(Atom.keepAlive);
|
|
73
|
+
private readonly _objects = Atom.make<Obj.Unknown[]>([]).pipe(Atom.keepAlive);
|
|
74
|
+
private readonly _registry: Registry.Registry;
|
|
75
|
+
private readonly _queue: Queue;
|
|
76
|
+
|
|
77
|
+
constructor(options: AiContextBinderOptions) {
|
|
78
|
+
super();
|
|
79
|
+
assertArgument(options.queue, 'options.queue', 'Queue is required');
|
|
80
|
+
this._queue = options.queue;
|
|
81
|
+
this._registry = options.registry ?? Registry.make();
|
|
82
|
+
}
|
|
83
|
+
|
|
63
84
|
/**
|
|
64
|
-
*
|
|
85
|
+
* Returns the blueprints atom for subscription.
|
|
65
86
|
*/
|
|
66
|
-
|
|
67
|
-
|
|
87
|
+
get blueprints(): Atom.Atom<Blueprint.Blueprint[]> {
|
|
88
|
+
return this._blueprints;
|
|
89
|
+
}
|
|
68
90
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
91
|
+
/**
|
|
92
|
+
* Returns the objects atom for subscription.
|
|
93
|
+
*/
|
|
94
|
+
get objects(): Atom.Atom<Obj.Unknown[]> {
|
|
95
|
+
return this._objects;
|
|
96
|
+
}
|
|
72
97
|
|
|
73
|
-
|
|
98
|
+
/**
|
|
99
|
+
* Gets the current blueprints value.
|
|
100
|
+
*/
|
|
101
|
+
getBlueprints(): Blueprint.Blueprint[] {
|
|
102
|
+
return this._registry.get(this._blueprints);
|
|
103
|
+
}
|
|
74
104
|
|
|
75
|
-
|
|
105
|
+
/**
|
|
106
|
+
* Gets the current objects value.
|
|
107
|
+
*/
|
|
108
|
+
getObjects(): Obj.Unknown[] {
|
|
109
|
+
return this._registry.get(this._objects);
|
|
110
|
+
}
|
|
76
111
|
|
|
77
112
|
/**
|
|
78
|
-
*
|
|
113
|
+
* Subscribe to changes in blueprints.
|
|
79
114
|
*/
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
115
|
+
subscribeBlueprints(cb: (blueprints: Blueprint.Blueprint[]) => void): () => void {
|
|
116
|
+
return this._registry.subscribe(this._blueprints, () => cb(this._registry.get(this._blueprints)));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Subscribe to changes in objects.
|
|
121
|
+
*/
|
|
122
|
+
subscribeObjects(cb: (objects: Obj.Unknown[]) => void): () => void {
|
|
123
|
+
return this._registry.subscribe(this._objects, () => cb(this._registry.get(this._objects)));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
protected override async _open(): Promise<void> {
|
|
127
|
+
const query = this._queue.query(Query.type(ContextBinding));
|
|
128
|
+
|
|
129
|
+
// Process initial state before returning.
|
|
130
|
+
const initialResults = await query.run();
|
|
131
|
+
await this._updateBindings(initialResults);
|
|
132
|
+
|
|
133
|
+
// Subscribe to future changes.
|
|
134
|
+
this._ctx.onDispose(
|
|
135
|
+
query.subscribe(async () => {
|
|
136
|
+
await this._updateBindings(query.results);
|
|
137
|
+
}),
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
private async _updateBindings(items: ContextBinding[]): Promise<void> {
|
|
142
|
+
// Skip update if no items - preserve existing state set by bind().
|
|
143
|
+
if (items.length === 0) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const bindings = this._reduce(items);
|
|
148
|
+
|
|
149
|
+
// Resolve references (loading them first if needed).
|
|
150
|
+
const currentBlueprints = this._registry.get(this._blueprints);
|
|
151
|
+
const currentObjects = this._registry.get(this._objects);
|
|
152
|
+
const newBlueprints = await this._resolve(bindings.blueprints, currentBlueprints);
|
|
153
|
+
const newObjects = await this._resolve(bindings.objects, currentObjects);
|
|
154
|
+
|
|
155
|
+
// Atomic updates - subscribers notified automatically.
|
|
156
|
+
this._registry.set(this._blueprints, newBlueprints);
|
|
157
|
+
this._registry.set(this._objects, newObjects);
|
|
158
|
+
|
|
159
|
+
log('updated bindings', {
|
|
160
|
+
blueprints: newBlueprints.length,
|
|
161
|
+
objects: newObjects.length,
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
protected override async _close(): Promise<void> {
|
|
166
|
+
// Reset atoms to empty state.
|
|
167
|
+
this._registry.set(this._blueprints, []);
|
|
168
|
+
this._registry.set(this._objects, []);
|
|
83
169
|
}
|
|
84
170
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
171
|
+
async bind({ blueprints, objects }: BindingProps): Promise<void> {
|
|
172
|
+
const currentBlueprints = this._registry.get(this._blueprints);
|
|
173
|
+
const currentObjects = this._registry.get(this._objects);
|
|
174
|
+
|
|
175
|
+
const { added: addedBlueprints, next: nextBlueprints } = this._processBindings(blueprints, currentBlueprints);
|
|
176
|
+
const { added: addedObjects, next: nextObjects } = this._processBindings(objects, currentObjects);
|
|
177
|
+
if (!addedBlueprints.length && !addedObjects.length) {
|
|
88
178
|
return;
|
|
89
179
|
}
|
|
90
180
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
const objects =
|
|
95
|
-
props.objects?.filter((ref) => !this.objects.peek().find((o) => o.dxn.toString() === ref.dxn.toString())) ?? [];
|
|
181
|
+
// Atomic updates - subscribers notified automatically.
|
|
182
|
+
this._registry.set(this._blueprints, nextBlueprints);
|
|
183
|
+
this._registry.set(this._objects, nextObjects);
|
|
96
184
|
|
|
185
|
+
log('bind', { blueprints: addedBlueprints.length, objects: addedObjects.length });
|
|
97
186
|
await this._queue.append([
|
|
98
187
|
Obj.make(ContextBinding, {
|
|
99
188
|
blueprints: {
|
|
100
|
-
added:
|
|
189
|
+
added: addedBlueprints,
|
|
101
190
|
removed: [],
|
|
102
191
|
},
|
|
103
192
|
objects: {
|
|
104
|
-
added:
|
|
193
|
+
added: addedObjects,
|
|
105
194
|
removed: [],
|
|
106
195
|
},
|
|
107
196
|
}),
|
|
108
197
|
]);
|
|
109
198
|
}
|
|
110
199
|
|
|
111
|
-
async unbind(
|
|
112
|
-
if (!
|
|
200
|
+
async unbind({ blueprints, objects }: BindingProps): Promise<void> {
|
|
201
|
+
if (!blueprints?.length && !objects?.length) {
|
|
113
202
|
return;
|
|
114
203
|
}
|
|
115
204
|
|
|
205
|
+
log('unbind', { blueprints: blueprints?.length, objects: objects?.length });
|
|
116
206
|
await this._queue.append([
|
|
117
207
|
Obj.make(ContextBinding, {
|
|
118
208
|
blueprints: {
|
|
119
209
|
added: [],
|
|
120
|
-
removed:
|
|
210
|
+
removed: blueprints ?? [],
|
|
121
211
|
},
|
|
122
212
|
objects: {
|
|
123
213
|
added: [],
|
|
124
|
-
removed:
|
|
214
|
+
removed: objects ?? [],
|
|
125
215
|
},
|
|
126
216
|
}),
|
|
127
217
|
]);
|
|
128
218
|
}
|
|
129
219
|
|
|
130
|
-
|
|
220
|
+
/**
|
|
221
|
+
* Process bindings to filter duplicates and determine next state.
|
|
222
|
+
*/
|
|
223
|
+
private _processBindings<T extends Obj.Unknown>(
|
|
224
|
+
refs: Ref.Ref<T>[] | undefined,
|
|
225
|
+
current: T[],
|
|
226
|
+
): { added: Ref.Ref<T>[]; next: T[] } {
|
|
227
|
+
const next = [...current];
|
|
228
|
+
const added: Ref.Ref<T>[] = [];
|
|
229
|
+
if (!refs?.length) {
|
|
230
|
+
return { added, next };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const seen = new Set(current.map((obj) => Obj.getDXN(obj).toString()));
|
|
234
|
+
for (const ref of refs) {
|
|
235
|
+
const dxn = ref.dxn.toString();
|
|
236
|
+
if (!seen.has(dxn)) {
|
|
237
|
+
seen.add(dxn);
|
|
238
|
+
added.push(ref);
|
|
239
|
+
|
|
240
|
+
// Only resolve target if available (has target or resolver).
|
|
241
|
+
if (ref.isAvailable) {
|
|
242
|
+
const target = ref.target;
|
|
243
|
+
if (target) {
|
|
244
|
+
next.push(target);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
return { added, next };
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Reduce results into sets of blueprints and objects.
|
|
255
|
+
*/
|
|
256
|
+
private _reduce(items: ContextBinding[]): Bindings {
|
|
131
257
|
return Function.pipe(
|
|
132
258
|
items,
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
259
|
+
EArray.reduce(new Bindings(), (context, { blueprints, objects }) => {
|
|
260
|
+
blueprints.added.forEach((ref) => context.blueprints.add(ref));
|
|
261
|
+
blueprints.removed.forEach((ref) => context.blueprints.delete(ref));
|
|
262
|
+
|
|
263
|
+
objects.added.forEach((ref) => context.objects.add(ref));
|
|
264
|
+
objects.removed.forEach((ref) => {
|
|
138
265
|
for (const obj of context.objects) {
|
|
139
266
|
if (DXN.equalsEchoId(obj.dxn, ref.dxn)) {
|
|
140
267
|
context.objects.delete(obj);
|
|
141
268
|
}
|
|
142
269
|
}
|
|
143
270
|
});
|
|
144
|
-
|
|
271
|
+
|
|
145
272
|
return context;
|
|
146
273
|
}),
|
|
147
274
|
);
|
|
148
275
|
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Resolve references to objects, loading them first if needed and falling back to existing objects.
|
|
279
|
+
*/
|
|
280
|
+
private async _resolve<T>(refs: Iterable<Ref.Ref<T>>, current: T[]): Promise<T[]> {
|
|
281
|
+
const refArray = [...refs];
|
|
282
|
+
|
|
283
|
+
// Load all refs that need loading.
|
|
284
|
+
await Promise.all(refArray.map((ref) => ref.tryLoad()));
|
|
285
|
+
|
|
286
|
+
return refArray
|
|
287
|
+
.map((ref) => {
|
|
288
|
+
let target: T | undefined;
|
|
289
|
+
// Only resolve target if available (has target or resolver).
|
|
290
|
+
if (ref.isAvailable) {
|
|
291
|
+
target = ref.target;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// Fallback to existing object.
|
|
295
|
+
return target ?? current.find((obj) => Obj.getDXN(obj as any).toString() === ref.dxn.toString());
|
|
296
|
+
})
|
|
297
|
+
.filter(isTruthy);
|
|
298
|
+
}
|
|
149
299
|
}
|
|
150
300
|
|
|
151
301
|
export class AiContextService extends Context.Tag('@dxos/assistant/AiContextService')<
|
|
@@ -153,4 +303,28 @@ export class AiContextService extends Context.Tag('@dxos/assistant/AiContextServ
|
|
|
153
303
|
{
|
|
154
304
|
binder: AiContextBinder;
|
|
155
305
|
}
|
|
156
|
-
>() {
|
|
306
|
+
>() {
|
|
307
|
+
static bindContext = ({ blueprints, objects }: BindingProps): Effect.Effect<void, never, AiContextService> =>
|
|
308
|
+
Effect.gen(function* () {
|
|
309
|
+
const { binder } = yield* AiContextService;
|
|
310
|
+
yield* Effect.promise(() => binder.bind({ blueprints, objects }));
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
static findObjects = <T extends Type.AnyEntity>(
|
|
314
|
+
type: T,
|
|
315
|
+
): Effect.Effect<Schema.Schema.Type<T>[], never, AiContextService> => {
|
|
316
|
+
return Effect.gen(function* () {
|
|
317
|
+
const { binder } = yield* AiContextService;
|
|
318
|
+
return binder.getObjects().filter(Obj.instanceOf(type));
|
|
319
|
+
});
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Glorified type cast until we figure out function service typing.
|
|
324
|
+
* For context, the AiContextService is provided to functions called from tool calls, but it's not implemented in types yet.
|
|
325
|
+
* @deprecated
|
|
326
|
+
*/
|
|
327
|
+
static fixFunctionHandlerType = <A, E, R>(
|
|
328
|
+
eff: Effect.Effect<A, E, R>,
|
|
329
|
+
): Effect.Effect<A, E, Exclude<R, AiContextService>> => eff as any;
|
|
330
|
+
}
|
|
@@ -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
|
+
});
|