@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.
Files changed (123) hide show
  1. package/README.md +1 -0
  2. package/dist/lib/neutral/chunk-BE6SRJ4N.mjs +193 -0
  3. package/dist/lib/neutral/chunk-BE6SRJ4N.mjs.map +7 -0
  4. package/dist/lib/neutral/extraction/index.mjs +223 -0
  5. package/dist/lib/neutral/extraction/index.mjs.map +7 -0
  6. package/dist/lib/neutral/index.mjs +1328 -0
  7. package/dist/lib/neutral/index.mjs.map +7 -0
  8. package/dist/lib/neutral/meta.json +1 -0
  9. package/dist/lib/neutral/testing/index.mjs +38 -0
  10. package/dist/lib/neutral/testing/index.mjs.map +7 -0
  11. package/dist/types/src/conversation/context.d.ts +109 -60
  12. package/dist/types/src/conversation/context.d.ts.map +1 -1
  13. package/dist/types/src/conversation/context.test.d.ts +2 -0
  14. package/dist/types/src/conversation/context.test.d.ts.map +1 -0
  15. package/dist/types/src/conversation/conversation.d.ts +41 -16
  16. package/dist/types/src/conversation/conversation.d.ts.map +1 -1
  17. package/dist/types/src/conversation/conversation.test.d.ts +2 -0
  18. package/dist/types/src/conversation/conversation.test.d.ts.map +1 -0
  19. package/dist/types/src/diff.d.ts +3 -1
  20. package/dist/types/src/diff.d.ts.map +1 -1
  21. package/dist/types/src/errors.d.ts +22 -30
  22. package/dist/types/src/errors.d.ts.map +1 -1
  23. package/dist/types/src/execution-graph.d.ts +5 -2
  24. package/dist/types/src/execution-graph.d.ts.map +1 -1
  25. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -1
  26. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +282 -268
  27. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
  28. package/dist/types/src/extraction/extraction.d.ts +348 -303
  29. package/dist/types/src/extraction/extraction.d.ts.map +1 -1
  30. package/dist/types/src/extraction/quotes.d.ts +2 -2
  31. package/dist/types/src/extraction/quotes.d.ts.map +1 -1
  32. package/dist/types/src/functions/services.d.ts +6 -6
  33. package/dist/types/src/functions/services.d.ts.map +1 -1
  34. package/dist/types/src/functions.test.d.ts +2 -0
  35. package/dist/types/src/functions.test.d.ts.map +1 -0
  36. package/dist/types/src/session/artifact-diff.d.ts +1 -1
  37. package/dist/types/src/session/artifact-diff.d.ts.map +1 -1
  38. package/dist/types/src/session/format.d.ts +4 -176
  39. package/dist/types/src/session/format.d.ts.map +1 -1
  40. package/dist/types/src/session/index.d.ts +1 -0
  41. package/dist/types/src/session/index.d.ts.map +1 -1
  42. package/dist/types/src/session/observer.d.ts +6 -5
  43. package/dist/types/src/session/observer.d.ts.map +1 -1
  44. package/dist/types/src/session/session.d.ts +25 -15
  45. package/dist/types/src/session/session.d.ts.map +1 -1
  46. package/dist/types/src/session/toolkit.d.ts +9 -6
  47. package/dist/types/src/session/toolkit.d.ts.map +1 -1
  48. package/dist/types/src/session/version-pin.d.ts +3 -3
  49. package/dist/types/src/session/version-pin.d.ts.map +1 -1
  50. package/dist/types/src/testing/index.d.ts +2 -0
  51. package/dist/types/src/testing/index.d.ts.map +1 -0
  52. package/dist/types/src/testing/layer.d.ts +22 -0
  53. package/dist/types/src/testing/layer.d.ts.map +1 -0
  54. package/dist/types/src/types/artifact.d.ts +28 -0
  55. package/dist/types/src/types/artifact.d.ts.map +1 -0
  56. package/dist/types/src/types/index.d.ts +2 -0
  57. package/dist/types/src/types/index.d.ts.map +1 -0
  58. package/dist/types/tsconfig.tsbuildinfo +1 -1
  59. package/package.json +41 -31
  60. package/src/conversation/context.test.ts +46 -0
  61. package/src/conversation/context.ts +223 -49
  62. package/src/conversation/conversation.test.ts +65 -0
  63. package/src/conversation/conversation.ts +179 -53
  64. package/src/diff.test.ts +37 -6
  65. package/src/diff.ts +18 -4
  66. package/src/errors.ts +2 -2
  67. package/src/execution-graph.test.ts +46 -44
  68. package/src/execution-graph.ts +25 -16
  69. package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +2 -2
  70. package/src/extraction/extraction-llm-function/extraction-llm-function.ts +44 -54
  71. package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +15 -14
  72. package/src/extraction/extraction-ner-function/extraction-ner-function.ts +36 -44
  73. package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +10 -5
  74. package/src/extraction/extraction.ts +9 -8
  75. package/src/extraction/quotes.ts +4 -5
  76. package/src/functions/services.ts +88 -37
  77. package/src/functions.conversations.json +1 -0
  78. package/src/functions.test.ts +74 -0
  79. package/src/session/artifact-diff.ts +1 -1
  80. package/src/session/format.test.ts +103 -39
  81. package/src/session/format.ts +17 -16
  82. package/src/session/index.ts +1 -0
  83. package/src/session/observer.ts +6 -3
  84. package/src/session/session.conversations.json +1 -1
  85. package/src/session/session.test.ts +60 -18
  86. package/src/session/session.ts +99 -60
  87. package/src/session/toolkit.ts +21 -25
  88. package/src/session/version-pin.ts +6 -6
  89. package/src/templates/{format.tpl → instructions/format.tpl} +1 -4
  90. package/src/templates/system.ts +2 -2
  91. package/src/testing/index.ts +5 -0
  92. package/src/testing/layer.ts +85 -0
  93. package/src/{types.ts → types/artifact.ts} +43 -6
  94. package/src/types/index.ts +5 -0
  95. package/dist/lib/browser/chunk-CWGVG235.mjs +0 -380
  96. package/dist/lib/browser/chunk-CWGVG235.mjs.map +0 -7
  97. package/dist/lib/browser/chunk-UB26N5UF.mjs +0 -89
  98. package/dist/lib/browser/chunk-UB26N5UF.mjs.map +0 -7
  99. package/dist/lib/browser/extraction/index.mjs +0 -272
  100. package/dist/lib/browser/extraction/index.mjs.map +0 -7
  101. package/dist/lib/browser/index.mjs +0 -869
  102. package/dist/lib/browser/index.mjs.map +0 -7
  103. package/dist/lib/browser/meta.json +0 -1
  104. package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs +0 -13
  105. package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs.map +0 -7
  106. package/dist/lib/node-esm/chunk-BCFXTNSK.mjs +0 -91
  107. package/dist/lib/node-esm/chunk-BCFXTNSK.mjs.map +0 -7
  108. package/dist/lib/node-esm/chunk-OV5PMQPB.mjs +0 -382
  109. package/dist/lib/node-esm/chunk-OV5PMQPB.mjs.map +0 -7
  110. package/dist/lib/node-esm/extraction/index.mjs +0 -273
  111. package/dist/lib/node-esm/extraction/index.mjs.map +0 -7
  112. package/dist/lib/node-esm/index.mjs +0 -870
  113. package/dist/lib/node-esm/index.mjs.map +0 -7
  114. package/dist/lib/node-esm/meta.json +0 -1
  115. package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs +0 -14
  116. package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs.map +0 -7
  117. package/dist/types/src/summarization/summarization.test.d.ts +0 -2
  118. package/dist/types/src/summarization/summarization.test.d.ts.map +0 -1
  119. package/dist/types/src/types.d.ts +0 -16
  120. package/dist/types/src/types.d.ts.map +0 -1
  121. package/src/summarization/summarization.test.ts +0 -202
  122. /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
  123. /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.c4373fc",
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
- "browser": "./dist/lib/browser/index.mjs",
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
- "browser": "./dist/lib/browser/extraction/index.mjs",
22
- "node": "./dist/lib/node-esm/extraction/index.mjs"
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.1.2",
42
- "@effect/ai": "0.29.1",
43
- "@effect/experimental": "0.56.0",
44
- "@effect/platform": "^0.92.1",
45
- "@preact/signals-core": "^1.12.1",
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/async": "0.8.4-main.c4373fc",
49
- "@dxos/blueprints": "0.8.4-main.c4373fc",
50
- "@dxos/ai": "0.8.4-main.c4373fc",
51
- "@dxos/context": "0.8.4-main.c4373fc",
52
- "@dxos/debug": "0.8.4-main.c4373fc",
53
- "@dxos/echo": "0.8.4-main.c4373fc",
54
- "@dxos/echo-db": "0.8.4-main.c4373fc",
55
- "@dxos/echo-protocol": "0.8.4-main.c4373fc",
56
- "@dxos/effect": "0.8.4-main.c4373fc",
57
- "@dxos/graph": "0.8.4-main.c4373fc",
58
- "@dxos/invariant": "0.8.4-main.c4373fc",
59
- "@dxos/errors": "0.8.4-main.c4373fc",
60
- "@dxos/keys": "0.8.4-main.c4373fc",
61
- "@dxos/live-object": "0.8.4-main.c4373fc",
62
- "@dxos/log": "0.8.4-main.c4373fc",
63
- "@dxos/schema": "0.8.4-main.c4373fc",
64
- "@dxos/util": "0.8.4-main.c4373fc",
65
- "@dxos/node-std": "0.8.4-main.c4373fc",
66
- "@dxos/functions": "0.8.4-main.c4373fc"
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.18.3"
80
+ "effect": "3.19.16"
71
81
  },
72
82
  "peerDependencies": {
73
- "effect": "^3.13.3"
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 { type ReadonlySignal, computed } from '@preact/signals-core';
6
- import * as Array from 'effect/Array';
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 { DXN, Obj, type Ref, type Relation, Type } from '@dxos/echo';
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 { ComplexSet } from '@dxos/util';
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(Type.Ref(Blueprint.Blueprint)),
23
- removed: Schema.Array(Type.Ref(Blueprint.Blueprint)),
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(Type.Ref(Type.Expando)),
29
- removed: Schema.Array(Type.Ref(Type.Expando)),
31
+ added: Schema.Array(Ref.Ref(Obj.Unknown)),
32
+ removed: Schema.Array(Ref.Ref(Obj.Unknown)),
30
33
  }),
31
34
  }).pipe(
32
- Type.Obj({
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<Type.Expando>[];
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<Type.Expando>>((ref) => ref.dxn.asEchoDXN()?.echoId);
52
+ readonly objects = new ComplexSet<Ref.Ref<Obj.Unknown>>((ref) => ref.dxn.asEchoDXN()?.echoId);
49
53
 
50
54
  toJSON() {
51
55
  return {
52
- blueprints: Array.fromIterable(this.blueprints).map((ref) => ref.dxn.toString()),
53
- objects: Array.fromIterable(this.objects).map((ref) => ref.dxn.toString()),
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
- * Reactive query of all bindings.
85
+ * Returns the blueprints atom for subscription.
65
86
  */
66
- // TODO(burdon): Cache value?
67
- readonly bindings: ReadonlySignal<Bindings> = computed(() => this._reduce(this._queue.objects));
87
+ get blueprints(): Atom.Atom<Blueprint.Blueprint[]> {
88
+ return this._blueprints;
89
+ }
68
90
 
69
- readonly blueprints: ReadonlySignal<Ref.Ref<Blueprint.Blueprint>[]> = computed(() => [
70
- ...this.bindings.value.blueprints,
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
- readonly objects: ReadonlySignal<Ref.Ref<Type.Expando>[]> = computed(() => [...this.bindings.value.objects]);
98
+ /**
99
+ * Gets the current blueprints value.
100
+ */
101
+ getBlueprints(): Blueprint.Blueprint[] {
102
+ return this._registry.get(this._blueprints);
103
+ }
74
104
 
75
- constructor(private readonly _queue: Queue) {}
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
- * Asynchronous query of all bindings.
113
+ * Subscribe to changes in blueprints.
79
114
  */
80
- async query(): Promise<Bindings> {
81
- const queueItems = await this._queue.queryObjects();
82
- return this._reduce(queueItems);
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
- // TODO(burdon): Pass in Blueprint obj (from registry?) and create reference.
86
- async bind(props: BindingProps): Promise<void> {
87
- if (!props.blueprints?.length && !props.objects?.length) {
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
- const blueprints =
92
- props.blueprints?.filter((ref) => !this.blueprints.peek().find((b) => b.dxn.toString() === ref.dxn.toString())) ??
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: blueprints,
189
+ added: addedBlueprints,
101
190
  removed: [],
102
191
  },
103
192
  objects: {
104
- added: objects,
193
+ added: addedObjects,
105
194
  removed: [],
106
195
  },
107
196
  }),
108
197
  ]);
109
198
  }
110
199
 
111
- async unbind(props: BindingProps): Promise<void> {
112
- if (!props.blueprints?.length && !props.objects?.length) {
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: props.blueprints ?? [],
210
+ removed: blueprints ?? [],
121
211
  },
122
212
  objects: {
123
213
  added: [],
124
- removed: props.objects ?? [],
214
+ removed: objects ?? [],
125
215
  },
126
216
  }),
127
217
  ]);
128
218
  }
129
219
 
130
- private _reduce(items: (Obj.Any | Relation.Any)[]): Bindings {
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
- Array.filter(Obj.instanceOf(ContextBinding)),
134
- Array.reduce(new Bindings(), (context, item) => {
135
- item.blueprints.removed.forEach((ref) => context.blueprints.delete(ref));
136
- item.blueprints.added.forEach((ref) => context.blueprints.add(ref));
137
- item.objects.removed.forEach((ref) => {
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
- item.objects.added.forEach((ref) => context.objects.add(ref));
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
+ });