@dxos/assistant 0.8.4-main.fffef41 → 0.8.4-staging.60fe92afc8

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 (174) hide show
  1. package/LICENSE +102 -5
  2. package/README.md +2 -1
  3. package/dist/lib/{browser → neutral}/extraction/index.mjs +42 -59
  4. package/dist/lib/neutral/extraction/index.mjs.map +7 -0
  5. package/dist/lib/neutral/index.mjs +1886 -0
  6. package/dist/lib/neutral/index.mjs.map +7 -0
  7. package/dist/lib/neutral/meta.json +1 -0
  8. package/dist/types/src/extraction/extraction-ner-function/index.d.ts +0 -1
  9. package/dist/types/src/extraction/extraction-ner-function/index.d.ts.map +1 -1
  10. package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts.map +1 -1
  11. package/dist/types/src/extraction/extraction.d.ts +1649 -317
  12. package/dist/types/src/extraction/extraction.d.ts.map +1 -1
  13. package/dist/types/src/extraction/index.d.ts +0 -1
  14. package/dist/types/src/extraction/index.d.ts.map +1 -1
  15. package/dist/types/src/extraction/quotes.d.ts +2 -2
  16. package/dist/types/src/extraction/quotes.d.ts.map +1 -1
  17. package/dist/types/src/functions/services.d.ts +14 -16
  18. package/dist/types/src/functions/services.d.ts.map +1 -1
  19. package/dist/types/src/index.d.ts +2 -5
  20. package/dist/types/src/index.d.ts.map +1 -1
  21. package/dist/types/src/request/AiRequest.d.ts +308 -0
  22. package/dist/types/src/request/AiRequest.d.ts.map +1 -0
  23. package/dist/types/src/{session → request}/artifact-diff.d.ts +4 -4
  24. package/dist/types/src/request/artifact-diff.d.ts.map +1 -0
  25. package/dist/types/src/request/format.d.ts +15 -0
  26. package/dist/types/src/request/format.d.ts.map +1 -0
  27. package/dist/types/src/request/index.d.ts +4 -0
  28. package/dist/types/src/request/index.d.ts.map +1 -0
  29. package/dist/types/src/{session → request}/observer.d.ts +4 -3
  30. package/dist/types/src/request/observer.d.ts.map +1 -0
  31. package/dist/types/src/{session → request}/version-pin.d.ts +4 -4
  32. package/dist/types/src/request/version-pin.d.ts.map +1 -0
  33. package/dist/types/src/session/AiContext.d.ts +178 -0
  34. package/dist/types/src/session/AiContext.d.ts.map +1 -0
  35. package/dist/types/src/session/AiContext.test.d.ts +2 -0
  36. package/dist/types/src/session/AiContext.test.d.ts.map +1 -0
  37. package/dist/types/src/session/AiSession.d.ts +79 -0
  38. package/dist/types/src/session/AiSession.d.ts.map +1 -0
  39. package/dist/types/src/session/AiSession.test.d.ts +2 -0
  40. package/dist/types/src/session/AiSession.test.d.ts.map +1 -0
  41. package/dist/types/src/session/SessionLink.d.ts +17 -0
  42. package/dist/types/src/session/SessionLink.d.ts.map +1 -0
  43. package/dist/types/src/session/SessionLoader.d.ts +15 -0
  44. package/dist/types/src/session/SessionLoader.d.ts.map +1 -0
  45. package/dist/types/src/session/SessionLoader.test.d.ts +2 -0
  46. package/dist/types/src/session/SessionLoader.test.d.ts.map +1 -0
  47. package/dist/types/src/session/index.d.ts +4 -5
  48. package/dist/types/src/session/index.d.ts.map +1 -1
  49. package/dist/types/src/session/toolkit.d.ts +10 -9
  50. package/dist/types/src/session/toolkit.d.ts.map +1 -1
  51. package/dist/types/src/templates/system.d.ts +12 -3
  52. package/dist/types/src/templates/system.d.ts.map +1 -1
  53. package/dist/types/src/util/artifact.d.ts +18 -0
  54. package/dist/types/src/util/artifact.d.ts.map +1 -0
  55. package/dist/types/src/{diff.d.ts → util/diff.d.ts} +4 -2
  56. package/dist/types/src/util/diff.d.ts.map +1 -0
  57. package/dist/types/src/{diff.test.d.ts.map → util/diff.test.d.ts.map} +1 -1
  58. package/dist/types/src/util/error-handling.d.ts.map +1 -0
  59. package/dist/types/src/{errors.d.ts → util/errors.d.ts} +24 -28
  60. package/dist/types/src/util/errors.d.ts.map +1 -0
  61. package/dist/types/src/{execution-graph.d.ts → util/execution-graph.d.ts} +2 -2
  62. package/dist/types/src/util/execution-graph.d.ts.map +1 -0
  63. package/dist/types/src/util/execution-graph.test.d.ts.map +1 -0
  64. package/dist/types/src/util/index.d.ts +7 -0
  65. package/dist/types/src/util/index.d.ts.map +1 -0
  66. package/dist/types/src/util/tracing.d.ts +296 -0
  67. package/dist/types/src/util/tracing.d.ts.map +1 -0
  68. package/dist/types/tsconfig.tsbuildinfo +1 -1
  69. package/package.json +36 -44
  70. package/src/extraction/extraction-ner-function/index.ts +0 -1
  71. package/src/extraction/extraction.ts +47 -18
  72. package/src/extraction/index.ts +0 -1
  73. package/src/extraction/quotes.test.ts +2 -2
  74. package/src/extraction/quotes.ts +6 -8
  75. package/src/functions/services.ts +129 -63
  76. package/src/index.ts +2 -5
  77. package/src/request/AiRequest.ts +387 -0
  78. package/src/{session → request}/artifact-diff.ts +4 -4
  79. package/src/{session → request}/format.ts +28 -25
  80. package/src/request/index.ts +8 -0
  81. package/src/{session → request}/observer.ts +4 -1
  82. package/src/{session → request}/version-pin.ts +6 -5
  83. package/src/session/AiContext.test.ts +49 -0
  84. package/src/session/AiContext.ts +420 -0
  85. package/src/session/AiSession.test.ts +111 -0
  86. package/src/session/AiSession.ts +328 -0
  87. package/src/session/SessionLink.ts +23 -0
  88. package/src/session/SessionLoader.test.ts +115 -0
  89. package/src/session/SessionLoader.ts +55 -0
  90. package/src/session/index.ts +4 -5
  91. package/src/session/toolkit.ts +27 -28
  92. package/src/templates/instructions/format.tpl +0 -4
  93. package/src/templates/instructions/system.tpl +50 -2
  94. package/src/templates/system.ts +7 -6
  95. package/src/util/artifact.ts +96 -0
  96. package/src/util/diff.test.ts +74 -0
  97. package/src/{diff.ts → util/diff.ts} +14 -5
  98. package/src/{session → util}/error-handling.ts +3 -2
  99. package/src/util/errors.ts +15 -0
  100. package/src/{execution-graph.test.ts → util/execution-graph.test.ts} +4 -4
  101. package/src/{execution-graph.ts → util/execution-graph.ts} +30 -19
  102. package/src/util/index.ts +10 -0
  103. package/src/util/tracing.ts +60 -0
  104. package/dist/lib/browser/extraction/index.mjs.map +0 -7
  105. package/dist/lib/browser/index.mjs +0 -1290
  106. package/dist/lib/browser/index.mjs.map +0 -7
  107. package/dist/lib/browser/meta.json +0 -1
  108. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +0 -11
  109. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +0 -7
  110. package/dist/lib/node-esm/extraction/index.mjs +0 -226
  111. package/dist/lib/node-esm/extraction/index.mjs.map +0 -7
  112. package/dist/lib/node-esm/index.mjs +0 -1291
  113. package/dist/lib/node-esm/index.mjs.map +0 -7
  114. package/dist/lib/node-esm/meta.json +0 -1
  115. package/dist/types/src/conversation/context.d.ts +0 -103
  116. package/dist/types/src/conversation/context.d.ts.map +0 -1
  117. package/dist/types/src/conversation/conversation.d.ts +0 -41
  118. package/dist/types/src/conversation/conversation.d.ts.map +0 -1
  119. package/dist/types/src/conversation/index.d.ts +0 -3
  120. package/dist/types/src/conversation/index.d.ts.map +0 -1
  121. package/dist/types/src/diff.d.ts.map +0 -1
  122. package/dist/types/src/errors.d.ts.map +0 -1
  123. package/dist/types/src/execution-graph.d.ts.map +0 -1
  124. package/dist/types/src/execution-graph.test.d.ts.map +0 -1
  125. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +0 -4
  126. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +0 -1
  127. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts +0 -2
  128. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts.map +0 -1
  129. package/dist/types/src/extraction/extraction-llm-function/index.d.ts +0 -2
  130. package/dist/types/src/extraction/extraction-llm-function/index.d.ts.map +0 -1
  131. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +0 -345
  132. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +0 -1
  133. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts +0 -2
  134. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts.map +0 -1
  135. package/dist/types/src/session/GenericToolkit.d.ts +0 -84
  136. package/dist/types/src/session/GenericToolkit.d.ts.map +0 -1
  137. package/dist/types/src/session/artifact-diff.d.ts.map +0 -1
  138. package/dist/types/src/session/error-handling.d.ts.map +0 -1
  139. package/dist/types/src/session/format.d.ts +0 -12
  140. package/dist/types/src/session/format.d.ts.map +0 -1
  141. package/dist/types/src/session/format.test.d.ts +0 -2
  142. package/dist/types/src/session/format.test.d.ts.map +0 -1
  143. package/dist/types/src/session/observer.d.ts.map +0 -1
  144. package/dist/types/src/session/session.d.ts +0 -51
  145. package/dist/types/src/session/session.d.ts.map +0 -1
  146. package/dist/types/src/session/session.test.d.ts +0 -2
  147. package/dist/types/src/session/session.test.d.ts.map +0 -1
  148. package/dist/types/src/session/version-pin.d.ts.map +0 -1
  149. package/dist/types/src/types/artifact.d.ts +0 -20
  150. package/dist/types/src/types/artifact.d.ts.map +0 -1
  151. package/dist/types/src/types/index.d.ts +0 -2
  152. package/dist/types/src/types/index.d.ts.map +0 -1
  153. package/src/conversation/context.ts +0 -186
  154. package/src/conversation/conversation.ts +0 -128
  155. package/src/conversation/index.ts +0 -6
  156. package/src/diff.test.ts +0 -43
  157. package/src/errors.ts +0 -15
  158. package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +0 -84
  159. package/src/extraction/extraction-llm-function/extraction-llm-function.ts +0 -62
  160. package/src/extraction/extraction-llm-function/index.ts +0 -5
  161. package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +0 -116
  162. package/src/extraction/extraction-ner-function/extraction-ner-function.ts +0 -56
  163. package/src/session/GenericToolkit.ts +0 -130
  164. package/src/session/format.test.ts +0 -61
  165. package/src/session/session.conversations.json +0 -1
  166. package/src/session/session.test.ts +0 -149
  167. package/src/session/session.ts +0 -213
  168. package/src/types/artifact.ts +0 -71
  169. package/src/types/index.ts +0 -5
  170. /package/dist/lib/{browser → neutral}/chunk-J5LGTIGS.mjs +0 -0
  171. /package/dist/lib/{browser → neutral}/chunk-J5LGTIGS.mjs.map +0 -0
  172. /package/dist/types/src/{diff.test.d.ts → util/diff.test.d.ts} +0 -0
  173. /package/dist/types/src/{session → util}/error-handling.d.ts +0 -0
  174. /package/dist/types/src/{execution-graph.test.d.ts → util/execution-graph.test.d.ts} +0 -0
@@ -1,186 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { type ReadonlySignal, computed } from '@preact/signals-core';
6
- import * as Array 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, Filter, Obj, Query, type Ref, type Relation, Type } from '@dxos/echo';
14
- import { type Queue } from '@dxos/echo-db';
15
- import { invariant } from '@dxos/invariant';
16
- import { ComplexSet } 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
- // TODO(burdon): Type.Expando => Type.Obj (or Obj.Any?)
29
- objects: Schema.Struct({
30
- added: Schema.Array(Type.Ref(Type.Expando)),
31
- removed: Schema.Array(Type.Ref(Type.Expando)),
32
- }),
33
- }).pipe(
34
- Type.Obj({
35
- typename: 'dxos.org/type/ContextBinding',
36
- version: '0.1.0',
37
- }),
38
- );
39
-
40
- export interface ContextBinding extends Schema.Schema.Type<typeof ContextBinding> {}
41
-
42
- export type BindingProps = Partial<{
43
- blueprints: Ref.Ref<Blueprint.Blueprint>[];
44
- objects: Ref.Ref<Type.Expando>[];
45
- }>;
46
-
47
- export class Bindings {
48
- readonly blueprints = new ComplexSet<Ref.Ref<Blueprint.Blueprint>>((ref) => ref.dxn.toString());
49
- // TODO(burdon): Some DXNs have the Space prefix so only compare the object ID.
50
- readonly objects = new ComplexSet<Ref.Ref<Type.Expando>>((ref) => ref.dxn.asEchoDXN()?.echoId);
51
-
52
- toJSON() {
53
- return {
54
- blueprints: Array.fromIterable(this.blueprints).map((ref) => ref.dxn.toString()),
55
- objects: Array.fromIterable(this.objects).map((ref) => ref.dxn.toString()),
56
- };
57
- }
58
- }
59
-
60
- /**
61
- * Manages bindings of blueprints and objects to a conversation.
62
- */
63
- // TODO(burdon): Context should manage ephemeral state of bindings until prompt is issued?
64
- export class AiContextBinder extends Resource {
65
- /**
66
- * Reactive query of all bindings.
67
- */
68
- // TODO(burdon): Cache value?
69
- private _bindings?: ReadonlySignal<Bindings>;
70
- private _blueprints?: ReadonlySignal<Ref.Ref<Blueprint.Blueprint>[]>;
71
- private _objects?: ReadonlySignal<Ref.Ref<Type.Expando>[]>;
72
-
73
- constructor(private readonly _queue: Queue) {
74
- super();
75
- }
76
-
77
- get bindings() {
78
- invariant(this._bindings, 'AiContextBinder not open');
79
- return this._bindings;
80
- }
81
-
82
- get blueprints() {
83
- invariant(this._blueprints, 'AiContextBinder not open');
84
- return this._blueprints;
85
- }
86
-
87
- get objects() {
88
- invariant(this._objects, 'AiContextBinder not open');
89
- return this._objects;
90
- }
91
-
92
- // TODO(wittjosiah): Use parent context?
93
- protected override async _open(): Promise<void> {
94
- const query = this._queue.query(Query.select(Filter.everything()));
95
- this._ctx.onDispose(query.subscribe(() => {}));
96
- this._bindings = computed(() => this._reduce(query.objects));
97
- this._blueprints = computed(() => [...this.bindings.value.blueprints]);
98
- this._objects = computed(() => [...this.bindings.value.objects]);
99
- }
100
-
101
- protected override async _close(): Promise<void> {
102
- this._bindings = undefined;
103
- this._blueprints = undefined;
104
- this._objects = undefined;
105
- }
106
-
107
- /**
108
- * Asynchronous query of all bindings.
109
- */
110
- async query(): Promise<Bindings> {
111
- const { objects } = await this._queue.query(Query.select(Filter.everything())).run();
112
- return this._reduce(objects);
113
- }
114
-
115
- // TODO(burdon): Pass in Blueprint obj (from registry?) and create reference.
116
- async bind(props: BindingProps): Promise<void> {
117
- const blueprints =
118
- props.blueprints?.filter((ref) => !this.blueprints.peek().find((b) => b.dxn.toString() === ref.dxn.toString())) ??
119
- [];
120
- const objects =
121
- props.objects?.filter((ref) => !this.objects.peek().find((o) => o.dxn.toString() === ref.dxn.toString())) ?? [];
122
-
123
- if (!blueprints.length && !objects.length) {
124
- return;
125
- }
126
-
127
- await this._queue.append([
128
- Obj.make(ContextBinding, {
129
- blueprints: {
130
- added: blueprints,
131
- removed: [],
132
- },
133
- objects: {
134
- added: objects,
135
- removed: [],
136
- },
137
- }),
138
- ]);
139
- }
140
-
141
- async unbind(props: BindingProps): Promise<void> {
142
- if (!props.blueprints?.length && !props.objects?.length) {
143
- return;
144
- }
145
-
146
- await this._queue.append([
147
- Obj.make(ContextBinding, {
148
- blueprints: {
149
- added: [],
150
- removed: props.blueprints ?? [],
151
- },
152
- objects: {
153
- added: [],
154
- removed: props.objects ?? [],
155
- },
156
- }),
157
- ]);
158
- }
159
-
160
- private _reduce(items: (Obj.Any | Relation.Any)[]): Bindings {
161
- return Function.pipe(
162
- items,
163
- Array.filter(Obj.instanceOf(ContextBinding)),
164
- Array.reduce(new Bindings(), (context, item) => {
165
- item.blueprints.removed.forEach((ref) => context.blueprints.delete(ref));
166
- item.blueprints.added.forEach((ref) => context.blueprints.add(ref));
167
- item.objects.removed.forEach((ref) => {
168
- for (const obj of context.objects) {
169
- if (DXN.equalsEchoId(obj.dxn, ref.dxn)) {
170
- context.objects.delete(obj);
171
- }
172
- }
173
- });
174
- item.objects.added.forEach((ref) => context.objects.add(ref));
175
- return context;
176
- }),
177
- );
178
- }
179
- }
180
-
181
- export class AiContextService extends Context.Tag('@dxos/assistant/AiContextService')<
182
- AiContextService,
183
- {
184
- binder: AiContextBinder;
185
- }
186
- >() {}
@@ -1,128 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import type * as Toolkit from '@effect/ai/Toolkit';
6
- import * as Array from 'effect/Array';
7
- import * as Effect from 'effect/Effect';
8
- import * as Option from 'effect/Option';
9
-
10
- import { Resource } from '@dxos/context';
11
- import { Obj } from '@dxos/echo';
12
- import { type Queue } from '@dxos/echo-db';
13
- import { DatabaseService } from '@dxos/echo-db';
14
- import { log } from '@dxos/log';
15
- import { Message } from '@dxos/types';
16
-
17
- import {
18
- AiSession,
19
- type AiSessionRunError,
20
- type AiSessionRunRequirements,
21
- type GenerationObserver,
22
- createToolkit,
23
- } from '../session';
24
-
25
- import { AiContextBinder, AiContextService, type ContextBinding } from './context';
26
-
27
- export interface AiConversationRunParams {
28
- prompt: string;
29
- system?: string;
30
- observer?: GenerationObserver;
31
- }
32
-
33
- /**
34
- * Durable conversation state (initiated by users and agents) backed by a Queue.
35
- * Executes tools based on AI responses and supports cancellation of in-progress requests.
36
- */
37
- export class AiConversation extends Resource {
38
- /**
39
- * Message and binding queue.
40
- */
41
- private readonly _queue: Queue<Message.Message | ContextBinding>;
42
-
43
- /**
44
- * Blueprints bound to the conversation.
45
- */
46
- private readonly _context: AiContextBinder;
47
-
48
- /**
49
- * Toolkit from the current session request.
50
- */
51
- private _toolkit: Toolkit.WithHandler<any> | undefined;
52
-
53
- public constructor(queue: Queue<Message.Message | ContextBinding>) {
54
- super();
55
- this._queue = queue;
56
- this._context = new AiContextBinder(this._queue);
57
- }
58
-
59
- protected override async _open(): Promise<void> {
60
- // TODO(wittjosiah): Pass in parent context?
61
- await this._context.open();
62
- }
63
-
64
- protected override async _close(): Promise<void> {
65
- await this._context.close();
66
- }
67
-
68
- public get context() {
69
- return this._context;
70
- }
71
-
72
- public get toolkit() {
73
- return this._toolkit;
74
- }
75
-
76
- public async getHistory(): Promise<Message.Message[]> {
77
- const queueItems = await this._queue.queryObjects();
78
- return queueItems.filter(Obj.instanceOf(Message.Message));
79
- }
80
-
81
- /**
82
- * Creates a new cancelable request effect.
83
- */
84
- createRequest(
85
- params: AiConversationRunParams,
86
- ): Effect.Effect<Message.Message[], AiSessionRunError, AiSessionRunRequirements> {
87
- return Effect.gen(this, function* () {
88
- const session = new AiSession();
89
- const history = yield* Effect.promise(() => this.getHistory());
90
-
91
- // Get context objects.
92
- const context = yield* Effect.promise(() => this.context.query());
93
- const blueprints = yield* Effect.forEach(context.blueprints.values(), DatabaseService.loadOption).pipe(
94
- Effect.map(Array.filter(Option.isSome)),
95
- Effect.map(Array.map((option) => option.value)),
96
- );
97
- const objects = yield* Effect.forEach(context.objects.values(), DatabaseService.loadOption).pipe(
98
- Effect.map(Array.filter(Option.isSome)),
99
- Effect.map(Array.map((option) => option.value)),
100
- );
101
-
102
- // Create toolkit.
103
- const toolkit = yield* createToolkit({ blueprints });
104
- this._toolkit = toolkit;
105
-
106
- const start = Date.now();
107
- log('run', {
108
- history: history.length,
109
- blueprints: blueprints.length,
110
- objects: objects.length,
111
- tools: this._toolkit?.tools.length ?? 0,
112
- });
113
-
114
- // Process request.
115
- const messages = yield* session.run({ history, blueprints, objects, toolkit, ...params }).pipe(
116
- Effect.provideService(AiContextService, {
117
- binder: this.context,
118
- }),
119
- );
120
-
121
- log('result', { messages: messages.length, duration: Date.now() - start });
122
-
123
- // Append to queue.
124
- yield* Effect.promise(() => this._queue.append(messages));
125
- return messages;
126
- }).pipe(Effect.withSpan('AiConversation.request'));
127
- }
128
- }
@@ -1,6 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- export * from './context';
6
- export * from './conversation';
package/src/diff.test.ts DELETED
@@ -1,43 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { describe, expect, it } from 'vitest';
6
-
7
- import { createDocAccessor } from '@dxos/echo-db';
8
- import { EchoTestBuilder } from '@dxos/echo-db/testing';
9
- import { Text } from '@dxos/schema';
10
- import { trim } from '@dxos/util';
11
-
12
- import { applyDiffs } from './diff';
13
-
14
- describe('diff', () => {
15
- it('should apply diffs', async () => {
16
- const builder = new EchoTestBuilder();
17
- const { db, graph } = await builder.createDatabase();
18
- graph.schemaRegistry.addSchema([Text.Text]);
19
-
20
- const document = trim`
21
- # Hello World
22
- There'z a typo in this sentence.
23
- And another oon here.
24
- But not this one.
25
- `;
26
-
27
- const text = db.add(Text.make(document));
28
- const accessor = createDocAccessor(text, ['content']);
29
- const result = applyDiffs(accessor, [
30
- `- There'z a typo in this sentence.`,
31
- `+ There is a typo in this sentence.`,
32
- `- And another oon here.`,
33
- `+ And another one here.`,
34
- ]);
35
-
36
- expect(result).toBe(trim`
37
- # Hello World
38
- There is a typo in this sentence.
39
- And another one here.
40
- But not this one.
41
- `);
42
- });
43
- });
package/src/errors.ts DELETED
@@ -1,15 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { BaseError } from '@dxos/errors';
6
-
7
- /**
8
- * Generic error from AI model.
9
- */
10
- export class AiModelError extends BaseError.extend('AI_MODEL_ERROR', 'AI model error') {}
11
-
12
- /**
13
- * Generic error for AI agent execution.
14
- */
15
- export class AiAssistantError extends BaseError.extend('AI_ASSISTANT_ERROR', 'AI assistant error') {}
@@ -1,84 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { beforeAll, describe, test } from 'vitest';
6
-
7
- import { todo } from '@dxos/debug';
8
- import { FunctionExecutor } from '@dxos/functions-runtime';
9
- import { log } from '@dxos/log';
10
- import { createTestData } from '@dxos/types/testing';
11
- import { range } from '@dxos/util';
12
-
13
- import { processTranscriptMessage } from '../extraction';
14
-
15
- import { extractionAnthropicFunction } from './extraction-llm-function';
16
-
17
- // TODO(burdon): Rewrite test.
18
- describe.skip('LLM EntityExtraction', () => {
19
- let executor: FunctionExecutor;
20
-
21
- beforeAll(async () => {
22
- executor = new FunctionExecutor(todo());
23
- });
24
-
25
- test('should process a transcript block', async () => {
26
- const { transcriptMessages, documents, contacts } = createTestData();
27
- log.info('context', { documents, contacts });
28
-
29
- for (const message of transcriptMessages.slice(0, 1)) {
30
- log.info('input', message);
31
- const { message: enhancedMessage } = await processTranscriptMessage({
32
- input: {
33
- message,
34
- objects: [...documents, ...Object.values(contacts)],
35
- },
36
- function: extractionAnthropicFunction,
37
- executor,
38
- });
39
- log.info('output', enhancedMessage);
40
- }
41
- });
42
-
43
- test('computational irreducibility', async () => {
44
- const { transcriptWoflram, documents, contacts } = createTestData();
45
- log.info('context', { documents, contacts });
46
-
47
- const message = transcriptWoflram[0];
48
- log.info('input', message);
49
-
50
- await Promise.all(
51
- range(10).map(async () => {
52
- const { message: enhancedMessage, timeElapsed } = await processTranscriptMessage({
53
- input: {
54
- message,
55
- objects: [...documents, ...Object.values(contacts)],
56
- },
57
- function: extractionAnthropicFunction,
58
- executor,
59
- });
60
-
61
- log.info('output', { message: enhancedMessage.blocks[0], timeElapsed });
62
- }),
63
- );
64
- });
65
-
66
- test('org and document linking', async () => {
67
- const { transcriptJosiah, documents, contacts, organizations } = createTestData();
68
- log.info('context', { contacts, organizations, documents });
69
-
70
- for (const message of transcriptJosiah) {
71
- log.info('input', message);
72
- const { message: enhancedMessage } = await processTranscriptMessage({
73
- input: {
74
- message,
75
- objects: [...documents, ...Object.values(contacts), ...Object.values(organizations)],
76
- },
77
- function: extractionAnthropicFunction,
78
- executor,
79
- });
80
-
81
- log.info('output', enhancedMessage);
82
- }
83
- });
84
- });
@@ -1,62 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { type FunctionDefinition, defineFunction } from '@dxos/functions';
6
-
7
- import { ExtractionInput, ExtractionOutput } from '../extraction';
8
-
9
- export const extractionAnthropicFunction: FunctionDefinition<ExtractionInput, ExtractionOutput> = defineFunction({
10
- key: 'dxos.org/function/extraction/extract-entities',
11
- name: 'Extract Entities',
12
- description: 'Extract entities from the transcript message and add them to the message.',
13
- inputSchema: ExtractionInput,
14
- outputSchema: ExtractionOutput,
15
- handler: async ({ data: { message, objects }, context }) => {
16
- throw new Error('not implemented');
17
- // const startTime = performance.now();
18
- // const ai = context.getService(AiService.AiService);
19
- // const session = new AiSession({ operationModel: 'configured' });
20
- // const result = await session.runStructured(ReferencedQuotes, {
21
- // generationOptions: {
22
- // model: '@anthropic/claude-3-5-haiku-20241022',
23
- // },
24
- // client: ai.client,
25
- // systemPrompt: PROMPT,
26
- // history: [
27
- // Obj.make(DataType.Message.Message, {
28
- // created: new Date().toISOString(),
29
- // sender: { role: 'user' },
30
- // blocks: [
31
- // {
32
- // _tag: 'text',
33
- // text: `<context>${JSON.stringify(objects)}</context>`,
34
- // } satisfies DataType.ContentBlock.Text,
35
- // {
36
- // _tag: 'text',
37
- // text: `<transcript>${JSON.stringify(message.blocks)}</transcript>`,
38
- // } satisfies DataType.ContentBlock.Text,
39
- // ],
40
- // }),
41
- // ],
42
- // artifacts: [],
43
- // prompt: '',
44
- // tools: [],
45
- // } as any); // TODO(burdon): Rewrite test.
46
-
47
- // return {
48
- // timeElapsed: performance.now() - startTime,
49
- // message: create(DataType.Message.Message, {
50
- // ...message,
51
- // blocks: message.blocks.map((block) =>
52
- // block._tag !== 'transcript'
53
- // ? block
54
- // : {
55
- // ...block,
56
- // text: insertReferences(block.text, result),
57
- // },
58
- // ),
59
- // }),
60
- // };
61
- },
62
- });
@@ -1,5 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- export * from './extraction-llm-function';
@@ -1,116 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- import { beforeAll, describe, test } from 'vitest';
6
-
7
- import { type EchoDatabase } from '@dxos/echo-db';
8
- import { EchoTestBuilder } from '@dxos/echo-db/testing';
9
- import { FunctionExecutor, ServiceContainer } from '@dxos/functions-runtime';
10
- import { log } from '@dxos/log';
11
- import { Testing } from '@dxos/schema/testing';
12
- import { type Message, Organization, Person } from '@dxos/types';
13
- import { createTestData } from '@dxos/types/testing';
14
- import { range } from '@dxos/util';
15
-
16
- import { processTranscriptMessage } from '../extraction';
17
-
18
- import { extractionNerFunction } from './extraction-ner-function';
19
-
20
- describe.skip('NER EntityExtraction', () => {
21
- let builder: EchoTestBuilder;
22
- let db: EchoDatabase;
23
- let executor: FunctionExecutor;
24
- let testData: {
25
- transcriptJosiah: Message.Message[];
26
- transcriptWoflram: Message.Message[];
27
- transcriptMessages: Message.Message[];
28
- documents: Testing.DocumentType[];
29
- contacts: Record<string, Person.Person>;
30
- organizations: Record<string, Organization.Organization>;
31
- };
32
-
33
- const TYPES = [Organization.Organization, Person.Person, Testing.Person, Testing.DocumentType];
34
-
35
- beforeAll(async () => {
36
- // TODO(dmaretskyi): Helper to scaffold this from a config.
37
- builder = await new EchoTestBuilder().open();
38
- const { db: db1 } = await builder.createDatabase({ indexing: { vector: true } });
39
- db = db1;
40
- db.graph.schemaRegistry.addSchema(TYPES);
41
- const data = createTestData();
42
- testData = {
43
- transcriptJosiah: data.transcriptJosiah,
44
- transcriptWoflram: data.transcriptWoflram,
45
- transcriptMessages: data.transcriptMessages,
46
- documents: data.documents.map((document) => db.add(document)),
47
- contacts: Object.fromEntries(Object.entries(data.contacts).map(([key, value]) => [key, db.add(value)])),
48
- organizations: Object.fromEntries(Object.entries(data.organizations).map(([key, value]) => [key, db.add(value)])),
49
- };
50
-
51
- await db.flush();
52
- executor = new FunctionExecutor(
53
- new ServiceContainer().setServices({
54
- database: { db },
55
- }),
56
- );
57
- }, 30_000);
58
-
59
- test('should process a transcript block', async () => {
60
- const { transcriptMessages, documents, contacts } = testData;
61
- log.info('context', { documents, contacts });
62
-
63
- for (const message of transcriptMessages) {
64
- log.info('input', message);
65
- const { message: enhancedMessage } = await processTranscriptMessage({
66
- input: {
67
- message,
68
- objects: [...documents, ...Object.values(contacts)],
69
- },
70
- function: extractionNerFunction,
71
- executor,
72
- });
73
- log.info('output', enhancedMessage);
74
- }
75
- });
76
-
77
- test('computational irreducibility', async () => {
78
- const { transcriptWoflram, documents, contacts } = testData;
79
- log.info('context', { documents, contacts });
80
- const message = transcriptWoflram[0];
81
- log.info('input', message);
82
-
83
- await Promise.all(
84
- range(10).map(async () => {
85
- const { message: enhancedMessage, timeElapsed } = await processTranscriptMessage({
86
- input: {
87
- message,
88
- objects: [...documents, ...Object.values(contacts)],
89
- },
90
- function: extractionNerFunction,
91
- executor,
92
- });
93
- log.info('output', { message: enhancedMessage.blocks[0], timeElapsed });
94
- }),
95
- );
96
- });
97
-
98
- test('org and document linking', async () => {
99
- const { transcriptJosiah, documents, contacts, organizations } = testData;
100
- log.info('context', { contacts, organizations, documents });
101
-
102
- for (const message of transcriptJosiah) {
103
- log.info('input', message);
104
-
105
- const { message: enhancedMessage } = await processTranscriptMessage({
106
- input: {
107
- message,
108
- objects: [...documents, ...Object.values(contacts), ...Object.values(organizations)],
109
- },
110
- function: extractionNerFunction,
111
- executor,
112
- });
113
- log.info('output', enhancedMessage);
114
- }
115
- });
116
- });