@dxos/assistant 0.8.4-main.f9ba587 → 0.8.4-main.fffef41

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 (177) hide show
  1. package/dist/lib/browser/chunk-J5LGTIGS.mjs +10 -0
  2. package/dist/lib/browser/chunk-J5LGTIGS.mjs.map +7 -0
  3. package/dist/lib/browser/extraction/index.mjs +225 -0
  4. package/dist/lib/browser/extraction/index.mjs.map +7 -0
  5. package/dist/lib/browser/index.mjs +1114 -1843
  6. package/dist/lib/browser/index.mjs.map +4 -4
  7. package/dist/lib/browser/meta.json +1 -1
  8. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs +11 -0
  9. package/dist/lib/node-esm/chunk-HSLMI22Q.mjs.map +7 -0
  10. package/dist/lib/node-esm/extraction/index.mjs +226 -0
  11. package/dist/lib/node-esm/extraction/index.mjs.map +7 -0
  12. package/dist/lib/node-esm/index.mjs +1114 -1844
  13. package/dist/lib/node-esm/index.mjs.map +4 -4
  14. package/dist/lib/node-esm/meta.json +1 -1
  15. package/dist/types/src/conversation/context.d.ts +103 -0
  16. package/dist/types/src/conversation/context.d.ts.map +1 -0
  17. package/dist/types/src/conversation/conversation.d.ts +29 -32
  18. package/dist/types/src/conversation/conversation.d.ts.map +1 -1
  19. package/dist/types/src/conversation/index.d.ts +1 -0
  20. package/dist/types/src/conversation/index.d.ts.map +1 -1
  21. package/dist/types/src/diff.d.ts +15 -0
  22. package/dist/types/src/diff.d.ts.map +1 -0
  23. package/dist/types/src/diff.test.d.ts +2 -0
  24. package/dist/types/src/diff.test.d.ts.map +1 -0
  25. package/dist/types/src/errors.d.ts +71 -0
  26. package/dist/types/src/errors.d.ts.map +1 -0
  27. package/dist/types/src/execution-graph.d.ts +85 -0
  28. package/dist/types/src/execution-graph.d.ts.map +1 -0
  29. package/dist/types/src/execution-graph.test.d.ts +2 -0
  30. package/dist/types/src/execution-graph.test.d.ts.map +1 -0
  31. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +1 -1
  32. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -1
  33. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +195 -58
  34. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
  35. package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts +1 -1
  36. package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts.map +1 -1
  37. package/dist/types/src/extraction/extraction.d.ts +264 -165
  38. package/dist/types/src/extraction/extraction.d.ts.map +1 -1
  39. package/dist/types/src/extraction/quotes.d.ts +1 -1
  40. package/dist/types/src/extraction/quotes.d.ts.map +1 -1
  41. package/dist/types/src/functions/index.d.ts +2 -0
  42. package/dist/types/src/functions/index.d.ts.map +1 -0
  43. package/dist/types/src/functions/services.d.ts +20 -0
  44. package/dist/types/src/functions/services.d.ts.map +1 -0
  45. package/dist/types/src/index.d.ts +5 -5
  46. package/dist/types/src/index.d.ts.map +1 -1
  47. package/dist/types/src/session/GenericToolkit.d.ts +84 -0
  48. package/dist/types/src/session/GenericToolkit.d.ts.map +1 -0
  49. package/dist/types/src/session/artifact-diff.d.ts +27 -0
  50. package/dist/types/src/session/artifact-diff.d.ts.map +1 -0
  51. package/dist/types/src/session/error-handling.d.ts +4 -0
  52. package/dist/types/src/session/error-handling.d.ts.map +1 -0
  53. package/dist/types/src/session/format.d.ts +12 -0
  54. package/dist/types/src/session/format.d.ts.map +1 -0
  55. package/dist/types/src/session/format.test.d.ts +2 -0
  56. package/dist/types/src/session/format.test.d.ts.map +1 -0
  57. package/dist/types/src/session/index.d.ts +4 -0
  58. package/dist/types/src/session/index.d.ts.map +1 -1
  59. package/dist/types/src/session/observer.d.ts +22 -0
  60. package/dist/types/src/session/observer.d.ts.map +1 -0
  61. package/dist/types/src/session/session.d.ts +36 -268
  62. package/dist/types/src/session/session.d.ts.map +1 -1
  63. package/dist/types/src/session/toolkit.d.ts +15 -0
  64. package/dist/types/src/session/toolkit.d.ts.map +1 -0
  65. package/dist/types/src/session/version-pin.d.ts +5 -5
  66. package/dist/types/src/session/version-pin.d.ts.map +1 -1
  67. package/dist/types/src/templates/index.d.ts +2 -0
  68. package/dist/types/src/templates/index.d.ts.map +1 -0
  69. package/dist/types/src/templates/system.d.ts +18 -0
  70. package/dist/types/src/templates/system.d.ts.map +1 -0
  71. package/dist/types/src/types/artifact.d.ts +20 -0
  72. package/dist/types/src/types/artifact.d.ts.map +1 -0
  73. package/dist/types/src/types/index.d.ts +2 -0
  74. package/dist/types/src/types/index.d.ts.map +1 -0
  75. package/dist/types/tsconfig.tsbuildinfo +1 -1
  76. package/package.json +48 -26
  77. package/src/conversation/context.ts +186 -0
  78. package/src/conversation/conversation.ts +101 -76
  79. package/src/conversation/index.ts +1 -0
  80. package/src/diff.test.ts +43 -0
  81. package/src/diff.ts +84 -0
  82. package/src/errors.ts +15 -0
  83. package/src/execution-graph.test.ts +740 -0
  84. package/src/execution-graph.ts +509 -0
  85. package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +14 -25
  86. package/src/extraction/extraction-llm-function/extraction-llm-function.ts +48 -52
  87. package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +18 -17
  88. package/src/extraction/extraction-ner-function/extraction-ner-function.ts +43 -41
  89. package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +12 -6
  90. package/src/extraction/extraction-ner-function/named-entity-recognition.ts +3 -1
  91. package/src/extraction/extraction.ts +7 -6
  92. package/src/extraction/quotes.test.ts +3 -3
  93. package/src/extraction/quotes.ts +4 -4
  94. package/src/functions/index.ts +5 -0
  95. package/src/functions/services.ts +162 -0
  96. package/src/index.ts +5 -5
  97. package/src/session/GenericToolkit.ts +130 -0
  98. package/src/session/artifact-diff.ts +41 -0
  99. package/src/session/error-handling.ts +41 -0
  100. package/src/session/format.test.ts +61 -0
  101. package/src/session/format.ts +144 -0
  102. package/src/session/index.ts +5 -0
  103. package/src/session/observer.ts +59 -0
  104. package/src/session/session.conversations.json +1 -0
  105. package/src/session/session.test.ts +88 -214
  106. package/src/session/session.ts +170 -539
  107. package/src/session/toolkit.ts +48 -0
  108. package/src/session/version-pin.ts +11 -12
  109. package/src/templates/index.ts +5 -0
  110. package/src/templates/instructions/format.tpl +61 -0
  111. package/src/templates/instructions/system.tpl +48 -0
  112. package/src/templates/system.ts +41 -0
  113. package/src/types/artifact.ts +71 -0
  114. package/src/types/index.ts +5 -0
  115. package/src/typings.d.ts +5 -0
  116. package/dist/types/bin/chat.d.ts +0 -2
  117. package/dist/types/bin/chat.d.ts.map +0 -1
  118. package/dist/types/src/blueprint/blueprint.d.ts +0 -41
  119. package/dist/types/src/blueprint/blueprint.d.ts.map +0 -1
  120. package/dist/types/src/blueprint/index.d.ts +0 -3
  121. package/dist/types/src/blueprint/index.d.ts.map +0 -1
  122. package/dist/types/src/blueprint/registry.d.ts +0 -7
  123. package/dist/types/src/blueprint/registry.d.ts.map +0 -1
  124. package/dist/types/src/context/binder.d.ts +0 -34
  125. package/dist/types/src/context/binder.d.ts.map +0 -1
  126. package/dist/types/src/context/binding.d.ts +0 -38
  127. package/dist/types/src/context/binding.d.ts.map +0 -1
  128. package/dist/types/src/context/index.d.ts +0 -3
  129. package/dist/types/src/context/index.d.ts.map +0 -1
  130. package/dist/types/src/research/exa.d.ts +0 -7
  131. package/dist/types/src/research/exa.d.ts.map +0 -1
  132. package/dist/types/src/research/graph.d.ts +0 -41
  133. package/dist/types/src/research/graph.d.ts.map +0 -1
  134. package/dist/types/src/research/graph.test.d.ts +0 -2
  135. package/dist/types/src/research/graph.test.d.ts.map +0 -1
  136. package/dist/types/src/research/index.d.ts +0 -4
  137. package/dist/types/src/research/index.d.ts.map +0 -1
  138. package/dist/types/src/research/research.d.ts +0 -10
  139. package/dist/types/src/research/research.d.ts.map +0 -1
  140. package/dist/types/src/research/research.test.d.ts +0 -2
  141. package/dist/types/src/research/research.test.d.ts.map +0 -1
  142. package/dist/types/src/session/version-pin.test.d.ts +0 -2
  143. package/dist/types/src/session/version-pin.test.d.ts.map +0 -1
  144. package/dist/types/src/summarization/summarization.test.d.ts +0 -2
  145. package/dist/types/src/summarization/summarization.test.d.ts.map +0 -1
  146. package/dist/types/src/testing/exa-search-1748337321991.d.ts +0 -38
  147. package/dist/types/src/testing/exa-search-1748337321991.d.ts.map +0 -1
  148. package/dist/types/src/testing/exa-search-1748337331526.d.ts +0 -37
  149. package/dist/types/src/testing/exa-search-1748337331526.d.ts.map +0 -1
  150. package/dist/types/src/testing/exa-search-1748337344119.d.ts +0 -58
  151. package/dist/types/src/testing/exa-search-1748337344119.d.ts.map +0 -1
  152. package/dist/types/src/testing/index.d.ts +0 -3
  153. package/dist/types/src/testing/index.d.ts.map +0 -1
  154. package/dist/types/src/types.d.ts +0 -3
  155. package/dist/types/src/types.d.ts.map +0 -1
  156. package/src/blueprint/blueprint.ts +0 -75
  157. package/src/blueprint/index.ts +0 -6
  158. package/src/blueprint/registry.ts +0 -15
  159. package/src/context/binder.ts +0 -101
  160. package/src/context/binding.ts +0 -31
  161. package/src/context/index.ts +0 -6
  162. package/src/research/exa.ts +0 -100
  163. package/src/research/graph.test.ts +0 -15
  164. package/src/research/graph.ts +0 -337
  165. package/src/research/index.ts +0 -7
  166. package/src/research/instructions-research.tpl +0 -97
  167. package/src/research/research.test.ts +0 -190
  168. package/src/research/research.ts +0 -90
  169. package/src/session/version-pin.test.ts +0 -26
  170. package/src/summarization/summarization.test.ts +0 -221
  171. package/src/testing/exa-search-1748337321991.ts +0 -131
  172. package/src/testing/exa-search-1748337331526.ts +0 -144
  173. package/src/testing/exa-search-1748337344119.ts +0 -133
  174. package/src/testing/index.ts +0 -11
  175. package/src/typedefs.d.ts +0 -8
  176. package/src/types.ts +0 -8
  177. /package/src/extraction/extraction-llm-function/{instructions.tpl → transcription-instructions.tpl} +0 -0
@@ -2,65 +2,61 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Message, ToolRegistry } from '@dxos/ai';
6
- import { Obj } from '@dxos/echo';
7
- import { create } from '@dxos/echo-schema';
8
- import { AiService, defineFunction, type FunctionDefinition } from '@dxos/functions';
9
- import { DataType } from '@dxos/schema';
5
+ import { type FunctionDefinition, defineFunction } from '@dxos/functions';
10
6
 
11
- import PROMPT from './instructions.tpl?raw';
12
- import { AISession } from '../../session';
13
7
  import { ExtractionInput, ExtractionOutput } from '../extraction';
14
- import { insertReferences, ReferencedQuotes } from '../quotes';
15
8
 
16
- export const extractionAnthropicFn: FunctionDefinition<ExtractionInput, ExtractionOutput> = defineFunction({
9
+ export const extractionAnthropicFunction: FunctionDefinition<ExtractionInput, ExtractionOutput> = defineFunction({
10
+ key: 'dxos.org/function/extraction/extract-entities',
11
+ name: 'Extract Entities',
17
12
  description: 'Extract entities from the transcript message and add them to the message.',
18
13
  inputSchema: ExtractionInput,
19
14
  outputSchema: ExtractionOutput,
20
15
  handler: async ({ data: { message, objects }, context }) => {
21
- const startTime = performance.now();
22
- const ai = context.getService(AiService);
23
- const session = new AISession({ operationModel: 'configured' });
24
- const result = await session.runStructured(ReferencedQuotes, {
25
- generationOptions: {
26
- model: '@anthropic/claude-3-5-haiku-20241022',
27
- },
28
- client: ai.client,
29
- systemPrompt: PROMPT,
30
- history: [
31
- Obj.make(Message, {
32
- role: 'user',
33
- content: [
34
- {
35
- type: 'text',
36
- text: `<context>${JSON.stringify(objects)}</context>`,
37
- },
38
- {
39
- type: 'text',
40
- text: `<transcript>${JSON.stringify(message.blocks)}</transcript>`,
41
- },
42
- ],
43
- }),
44
- ],
45
- artifacts: [],
46
- prompt: '',
47
- tools: [],
48
- toolResolver: new ToolRegistry([]),
49
- });
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.
50
46
 
51
- return {
52
- message: create(DataType.Message, {
53
- ...message,
54
- blocks: message.blocks.map((block, i) =>
55
- block.type !== 'transcription'
56
- ? block
57
- : {
58
- ...block,
59
- text: insertReferences(block.text, result),
60
- },
61
- ),
62
- }),
63
- timeElapsed: performance.now() - startTime,
64
- };
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
+ // };
65
61
  },
66
62
  });
@@ -2,32 +2,35 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { describe, test, beforeAll } from 'vitest';
5
+ import { beforeAll, describe, test } from 'vitest';
6
6
 
7
7
  import { type EchoDatabase } from '@dxos/echo-db';
8
8
  import { EchoTestBuilder } from '@dxos/echo-db/testing';
9
- import { FunctionExecutor, ServiceContainer } from '@dxos/functions';
9
+ import { FunctionExecutor, ServiceContainer } from '@dxos/functions-runtime';
10
10
  import { log } from '@dxos/log';
11
- import { DataType } from '@dxos/schema';
12
- import { createTestData, Testing } from '@dxos/schema/testing';
11
+ import { Testing } from '@dxos/schema/testing';
12
+ import { type Message, Organization, Person } from '@dxos/types';
13
+ import { createTestData } from '@dxos/types/testing';
13
14
  import { range } from '@dxos/util';
14
15
 
15
- import { extractionNerFn } from './extraction-ner-function';
16
16
  import { processTranscriptMessage } from '../extraction';
17
17
 
18
+ import { extractionNerFunction } from './extraction-ner-function';
19
+
18
20
  describe.skip('NER EntityExtraction', () => {
19
21
  let builder: EchoTestBuilder;
20
22
  let db: EchoDatabase;
21
23
  let executor: FunctionExecutor;
22
24
  let testData: {
23
- transcriptJosiah: DataType.Message[];
24
- transcriptWoflram: DataType.Message[];
25
- transcriptMessages: DataType.Message[];
25
+ transcriptJosiah: Message.Message[];
26
+ transcriptWoflram: Message.Message[];
27
+ transcriptMessages: Message.Message[];
26
28
  documents: Testing.DocumentType[];
27
- contacts: Record<string, DataType.Person>;
28
- organizations: Record<string, DataType.Organization>;
29
+ contacts: Record<string, Person.Person>;
30
+ organizations: Record<string, Organization.Organization>;
29
31
  };
30
- const TYPES = [DataType.Organization, DataType.Person, Testing.Contact, Testing.DocumentType];
32
+
33
+ const TYPES = [Organization.Organization, Person.Person, Testing.Person, Testing.DocumentType];
31
34
 
32
35
  beforeAll(async () => {
33
36
  // TODO(dmaretskyi): Helper to scaffold this from a config.
@@ -44,8 +47,8 @@ describe.skip('NER EntityExtraction', () => {
44
47
  contacts: Object.fromEntries(Object.entries(data.contacts).map(([key, value]) => [key, db.add(value)])),
45
48
  organizations: Object.fromEntries(Object.entries(data.organizations).map(([key, value]) => [key, db.add(value)])),
46
49
  };
47
- await db.flush();
48
50
 
51
+ await db.flush();
49
52
  executor = new FunctionExecutor(
50
53
  new ServiceContainer().setServices({
51
54
  database: { db },
@@ -64,7 +67,7 @@ describe.skip('NER EntityExtraction', () => {
64
67
  message,
65
68
  objects: [...documents, ...Object.values(contacts)],
66
69
  },
67
- function: extractionNerFn,
70
+ function: extractionNerFunction,
68
71
  executor,
69
72
  });
70
73
  log.info('output', enhancedMessage);
@@ -73,7 +76,6 @@ describe.skip('NER EntityExtraction', () => {
73
76
 
74
77
  test('computational irreducibility', async () => {
75
78
  const { transcriptWoflram, documents, contacts } = testData;
76
-
77
79
  log.info('context', { documents, contacts });
78
80
  const message = transcriptWoflram[0];
79
81
  log.info('input', message);
@@ -85,7 +87,7 @@ describe.skip('NER EntityExtraction', () => {
85
87
  message,
86
88
  objects: [...documents, ...Object.values(contacts)],
87
89
  },
88
- function: extractionNerFn,
90
+ function: extractionNerFunction,
89
91
  executor,
90
92
  });
91
93
  log.info('output', { message: enhancedMessage.blocks[0], timeElapsed });
@@ -95,7 +97,6 @@ describe.skip('NER EntityExtraction', () => {
95
97
 
96
98
  test('org and document linking', async () => {
97
99
  const { transcriptJosiah, documents, contacts, organizations } = testData;
98
-
99
100
  log.info('context', { contacts, organizations, documents });
100
101
 
101
102
  for (const message of transcriptJosiah) {
@@ -106,7 +107,7 @@ describe.skip('NER EntityExtraction', () => {
106
107
  message,
107
108
  objects: [...documents, ...Object.values(contacts), ...Object.values(organizations)],
108
109
  },
109
- function: extractionNerFn,
110
+ function: extractionNerFunction,
110
111
  executor,
111
112
  });
112
113
  log.info('output', enhancedMessage);
@@ -2,53 +2,55 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { asyncTimeout } from '@dxos/async';
6
- import { create } from '@dxos/echo-schema';
7
- import { DatabaseService, defineFunction } from '@dxos/functions';
8
- import { invariant } from '@dxos/invariant';
9
- import { log } from '@dxos/log';
10
- import { DataType } from '@dxos/schema';
11
-
12
- import { extractFullEntities } from './named-entity-recognition';
5
+ import { defineFunction } from '@dxos/functions';
6
+
13
7
  import { ExtractionInput, ExtractionOutput } from '../extraction';
14
- import { findQuotes, insertReferences } from '../quotes';
15
8
 
9
+ /**
10
+ * Named Entity Recognition model
11
+ */
16
12
  // TODO(mykola): Make it use vector index to query objects and not pass objects as input.
17
- export const extractionNerFn = defineFunction({
13
+ export const extractionNerFunction = defineFunction({
14
+ key: 'dxos.org/function/extraction/extract-entities',
15
+ name: 'Extract Entities',
18
16
  description: 'Extracts named entities from a text',
19
17
  inputSchema: ExtractionInput,
20
18
  outputSchema: ExtractionOutput,
21
19
  handler: async ({ data: { message, options }, context }) => {
22
- log.info('input', { message, options });
23
- const startTime = performance.now();
24
- const { db } = context.getService(DatabaseService);
25
-
26
- const entitiesPromise = Promise.all(
27
- message.blocks.map(async (block) => {
28
- invariant(block.type === 'transcription' || block.type === 'text', 'Block must have text');
29
- return extractFullEntities(block.text);
30
- }),
31
- ).then((entities) => entities.flat());
32
- const entities = options?.timeout ? await asyncTimeout(entitiesPromise, options.timeout) : await entitiesPromise;
33
- log.info('entities', { entities });
34
- const quoteReferences = await findQuotes(
35
- entities.map((entity) => entity.word),
36
- db,
37
- );
38
- const blocksWithReferences = message.blocks.map((block, i) => {
39
- invariant(block.type === 'transcription' || block.type === 'text', 'Block must have text');
40
- return { ...block, text: insertReferences(block.text, quoteReferences) };
41
- });
42
- const messageWithReferences = create(DataType.Message, {
43
- ...message,
44
- blocks: blocksWithReferences,
45
- });
46
-
47
- log.info('output', { messageWithReferences });
48
-
49
- return {
50
- message: messageWithReferences,
51
- timeElapsed: performance.now() - startTime,
52
- };
20
+ throw new Error('not implemented');
21
+ // log.info('input', { message, options });
22
+ // const startTime = performance.now();
23
+ // const { db } = context.getService(DatabaseService);
24
+ // const { extractFullEntities } = await import('./named-entity-recognition');
25
+
26
+ // const entitiesPromise = Promise.all(
27
+ // message.blocks.map(async (block) => {
28
+ // invariant(block._tag === 'transcript' || block._tag === 'text');
29
+ // return extractFullEntities(block.text);
30
+ // }),
31
+ // ).then((entities) => entities.flat());
32
+ // const entities = options?.timeout ? await asyncTimeout(entitiesPromise, options.timeout) : await entitiesPromise;
33
+
34
+ // log.info('entities', { entities });
35
+ // const quoteReferences = await findQuotes(
36
+ // entities.map((entity) => entity.word),
37
+ // db,
38
+ // );
39
+
40
+ // const blocksWithReferences = message.blocks.map((block) => {
41
+ // invariant(block._tag === 'transcript' || block._tag === 'text');
42
+ // return { ...block, text: insertReferences(block._tag, quoteReferences) };
43
+ // });
44
+
45
+ // const messageWithReferences = create(Message.Message, {
46
+ // ...message,
47
+ // blocks: blocksWithReferences,
48
+ // });
49
+
50
+ // log.info('output', { messageWithReferences });
51
+ // return {
52
+ // message: messageWithReferences,
53
+ // timeElapsed: performance.now() - startTime,
54
+ // };
53
55
  },
54
56
  });
@@ -6,11 +6,13 @@ import { type TokenClassificationOutput, type TokenClassificationPipelineType }
6
6
  import { beforeAll, describe, expect, test } from 'vitest';
7
7
 
8
8
  import { log } from '@dxos/log';
9
- import { createTestData } from '@dxos/schema/testing';
9
+ import { type ContentBlock, type Message } from '@dxos/types';
10
+ import { createTestData } from '@dxos/types/testing';
10
11
 
11
12
  import { combineNerTokens, createTokenGroups, extractFullEntities, getNer } from './named-entity-recognition';
12
13
 
13
- describe('NamedEntityRecognition', () => {
14
+ // TODO(mykola): This should not run on CI.
15
+ describe.skip('NamedEntityRecognition', () => {
14
16
  let ner: TokenClassificationPipelineType;
15
17
 
16
18
  beforeAll(async () => {
@@ -56,8 +58,10 @@ describe('NamedEntityRecognition', () => {
56
58
  test('own data', async () => {
57
59
  const { transcriptWoflram, transcriptJosiah } = await createTestData();
58
60
 
59
- const blocks = [...transcriptWoflram, ...transcriptJosiah].flatMap((message) =>
60
- message.blocks.flatMap((block) => (block.type === 'transcription' ? [block.text] : [])),
61
+ const blocks = [...transcriptWoflram, ...transcriptJosiah].flatMap((message: Message.Message) =>
62
+ message.blocks.flatMap((block: ContentBlock.Any) =>
63
+ block._tag === 'transcript' ? [(block as ContentBlock.Transcript).text] : [],
64
+ ),
61
65
  );
62
66
 
63
67
  for (const block of blocks) {
@@ -69,8 +73,10 @@ describe('NamedEntityRecognition', () => {
69
73
  test('more own data', async () => {
70
74
  const { transcriptMessages } = await createTestData();
71
75
 
72
- const blocks = transcriptMessages.flatMap((message) =>
73
- message.blocks.flatMap((block) => (block.type === 'transcription' ? [block.text] : [])),
76
+ const blocks = transcriptMessages.flatMap((message: Message.Message) =>
77
+ message.blocks.flatMap((block: ContentBlock.Any) =>
78
+ block._tag === 'transcript' ? [(block as ContentBlock.Transcript).text] : [],
79
+ ),
74
80
  );
75
81
 
76
82
  for (const block of blocks) {
@@ -4,14 +4,15 @@
4
4
 
5
5
  import {
6
6
  type TokenClassificationOutput,
7
+ type TokenClassificationPipelineType,
7
8
  type TokenClassificationSingle,
8
9
  pipeline,
9
- type TokenClassificationPipelineType,
10
10
  } from '@xenova/transformers';
11
11
 
12
12
  import { log } from '@dxos/log';
13
13
 
14
14
  let _ner: Promise<TokenClassificationPipelineType>;
15
+
15
16
  /**
16
17
  * Named Entity Recognition pipeline.
17
18
  * Initializes the pipeline on first call.
@@ -24,6 +25,7 @@ export const getNer = () => {
24
25
  return ner;
25
26
  });
26
27
  }
28
+
27
29
  return _ner;
28
30
  };
29
31
 
@@ -2,16 +2,17 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Schema } from 'effect';
5
+ import * as Schema from 'effect/Schema';
6
6
 
7
7
  import { asyncTimeout } from '@dxos/async';
8
- import { Expando } from '@dxos/echo-schema';
9
- import { type FunctionDefinition, type FunctionExecutor } from '@dxos/functions';
8
+ import { Expando } from '@dxos/echo/internal';
9
+ import { type FunctionDefinition } from '@dxos/functions';
10
+ import { type FunctionExecutor } from '@dxos/functions-runtime';
10
11
  import { log } from '@dxos/log';
11
- import { DataType } from '@dxos/schema';
12
+ import { Message } from '@dxos/types';
12
13
 
13
14
  export const ExtractionInput = Schema.Struct({
14
- message: DataType.Message,
15
+ message: Message.Message,
15
16
  objects: Schema.optional(Schema.Array(Expando)),
16
17
  options: Schema.optional(
17
18
  Schema.Struct({
@@ -23,7 +24,7 @@ export const ExtractionInput = Schema.Struct({
23
24
  export interface ExtractionInput extends Schema.Schema.Type<typeof ExtractionInput> {}
24
25
 
25
26
  export const ExtractionOutput = Schema.Struct({
26
- message: DataType.Message,
27
+ message: Message.Message,
27
28
  timeElapsed: Schema.Number,
28
29
  });
29
30
  export interface ExtractionOutput extends Schema.Schema.Type<typeof ExtractionOutput> {}
@@ -2,7 +2,7 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { describe, test, expect } from 'vitest';
5
+ import { describe, expect, test } from 'vitest';
6
6
 
7
7
  import { insertReferences } from './quotes';
8
8
 
@@ -13,7 +13,7 @@ describe('insertReferences', () => {
13
13
  };
14
14
 
15
15
  expect(insertReferences('This is a computational irreducibility test.', quotes)).toBe(
16
- 'This is a [computational irreducibility][dxn:echo:@:01JTG9JW11XGWJZ32AW8ET93D1] test.',
16
+ 'This is a [computational irreducibility](dxn:echo:@:01JTG9JW11XGWJZ32AW8ET93D1) test.',
17
17
  );
18
18
  expect(
19
19
  insertReferences(
@@ -21,7 +21,7 @@ describe('insertReferences', () => {
21
21
  quotes,
22
22
  ),
23
23
  ).toBe(
24
- "And what I'd like to talk today about is Steven Wolfram's concept of a [computational irreducibility][dxn:echo:@:01JTG9JW11XGWJZ32AW8ET93D1].",
24
+ "And what I'd like to talk today about is Steven Wolfram's concept of a [computational irreducibility](dxn:echo:@:01JTG9JW11XGWJZ32AW8ET93D1).",
25
25
  );
26
26
  });
27
27
  });
@@ -2,11 +2,11 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- import { Schema } from 'effect';
5
+ import * as Schema from 'effect/Schema';
6
6
 
7
- import { Query, Filter } from '@dxos/echo';
7
+ import { Filter, Query } from '@dxos/echo';
8
+ import { ObjectId } from '@dxos/echo/internal';
8
9
  import { type EchoDatabase } from '@dxos/echo-db';
9
- import { ObjectId } from '@dxos/echo-schema';
10
10
  import { DXN } from '@dxos/keys';
11
11
  import { log } from '@dxos/log';
12
12
 
@@ -51,7 +51,7 @@ export const insertReferences = (text: string, quotes: ReferencedQuotes) => {
51
51
 
52
52
  // Use a case-insensitive regular expression to replace the quote.
53
53
  const regex = new RegExp(quote.quote.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi');
54
- text = text.replace(regex, `[${quote.quote}][${DXN.fromLocalObjectId(quote.id).toString()}]`);
54
+ text = text.replace(regex, `[${quote.quote}](${DXN.fromLocalObjectId(quote.id).toString()})`);
55
55
  }
56
56
 
57
57
  return text;
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ export * from './services';
@@ -0,0 +1,162 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import * as Tool from '@effect/ai/Tool';
6
+ import type * as Toolkit from '@effect/ai/Toolkit';
7
+ import * as Context from 'effect/Context';
8
+ import * as Effect from 'effect/Effect';
9
+ import * as Layer from 'effect/Layer';
10
+ import * as Record from 'effect/Record';
11
+ import * as Schema from 'effect/Schema';
12
+
13
+ import { AiToolNotFoundError, ToolExecutionService, ToolResolverService } from '@dxos/ai';
14
+ import { todo } from '@dxos/debug';
15
+ import { Query } from '@dxos/echo';
16
+ import { DatabaseService } from '@dxos/echo-db';
17
+ import { Function, FunctionDefinition, FunctionInvocationService } from '@dxos/functions';
18
+ import { invariant } from '@dxos/invariant';
19
+
20
+ /**
21
+ * Constructs a `ToolResolverService` whose `resolve(id)` looks up tools in the following order:
22
+ * 1. Toolkit: return an existing tool from the provided `toolkit` if one is present under `id`.
23
+ * 2. Functions in DB: query `FunctionType` by `key=id`; if found, deserialize and project to a tool
24
+ * (propagating any `deployedFunctionId` discovered in the object's metadata).
25
+ * 3. Functions passed in: fall back to a matching `FunctionDefinition` from the `functions` array.
26
+ *
27
+ * If none of the above yield a match, the effect fails with `AiToolNotFoundError`.
28
+ *
29
+ * Requires `DatabaseService` in the environment.
30
+ */
31
+ export const makeToolResolverFromFunctions = (
32
+ functions: FunctionDefinition.Any[],
33
+ toolkit: Toolkit.Toolkit<any>,
34
+ ): Layer.Layer<ToolResolverService, never, DatabaseService> => {
35
+ return Layer.effect(
36
+ ToolResolverService,
37
+ Effect.gen(function* () {
38
+ const dbService = yield* DatabaseService;
39
+ return {
40
+ resolve: (id): Effect.Effect<Tool.Any, AiToolNotFoundError> =>
41
+ Effect.gen(function* () {
42
+ const tool = toolkit.tools[id];
43
+ if (tool) {
44
+ return tool;
45
+ }
46
+
47
+ const {
48
+ objects: [dbFunction],
49
+ } = yield* DatabaseService.runQuery(Query.type(Function.Function, { key: id }));
50
+
51
+ const functionDef = dbFunction
52
+ ? FunctionDefinition.deserialize(dbFunction)
53
+ : functions.find((fn) => fn.key === id);
54
+
55
+ if (!functionDef) {
56
+ return yield* Effect.fail(new AiToolNotFoundError(id));
57
+ }
58
+
59
+ return projectFunctionToTool(functionDef);
60
+ }).pipe(Effect.provideService(DatabaseService, dbService)),
61
+ } satisfies Context.Tag.Service<ToolResolverService>;
62
+ }),
63
+ );
64
+ };
65
+
66
+ export const makeToolExecutionServiceFromFunctions = (
67
+ toolkit: Toolkit.Toolkit<any>,
68
+ handlersLayer: Layer.Layer<Tool.Handler<any>, never, never>,
69
+ ): Layer.Layer<ToolExecutionService, never, FunctionInvocationService> => {
70
+ return Layer.effect(
71
+ ToolExecutionService,
72
+ Effect.gen(function* () {
73
+ const toolkitHandler = yield* toolkit.pipe(Effect.provide(handlersLayer));
74
+ invariant(isHandlerLike(toolkitHandler));
75
+ const functionInvocationService = yield* FunctionInvocationService;
76
+
77
+ return {
78
+ handlersFor: (toolkit) => {
79
+ const makeHandler = (tool: Tool.Any): ((params: unknown) => Effect.Effect<unknown, any, any>) => {
80
+ return Effect.fn(`toolFunctionHandler ${tool.name}`)(function* (input: any) {
81
+ if (toolkitHandler.tools[tool.name]) {
82
+ if (Tool.isProviderDefined(tool)) {
83
+ throw new Error('Attempted to call a provider-defined tool');
84
+ }
85
+ // TODO(wittjosiah): Everything is `never` here.
86
+ const { result } = yield* (toolkitHandler.handle as any)(tool.name, input);
87
+ return result;
88
+ }
89
+
90
+ const { definition: functionDef } = Context.get(FunctionToolAnnotation)(tool.annotations as any);
91
+
92
+ return yield* functionInvocationService
93
+ .invokeFunction(functionDef, input as any)
94
+ .pipe(Effect.catchAllDefect((defect) => Effect.fail(defect)));
95
+ });
96
+ };
97
+
98
+ return toolkit.of(
99
+ Record.map(toolkit.tools, (tool, _name) => (Tool.isUserDefined(tool) ? makeHandler(tool) : null)) as any,
100
+ ) as any;
101
+ },
102
+ };
103
+ }),
104
+ );
105
+ };
106
+
107
+ class FunctionToolAnnotation extends Context.Tag('@dxos/assistant/FunctionToolAnnotation')<
108
+ FunctionToolAnnotation,
109
+ { definition: FunctionDefinition.Any }
110
+ >() {}
111
+
112
+ const toolCache = new WeakMap<FunctionDefinition.Any, Tool.Any>();
113
+
114
+ /**
115
+ * Projects a `FunctionDefinition` into an `AiTool`.
116
+ *
117
+ * @param fn The function definition to project into a tool.
118
+ * @param meta Optional projection metadata.
119
+ * @param meta.deployedFunctionId Backend deployment ID used for remote invocation when present.
120
+ * This is the EDGE service's function deployment identifier (not the ECHO object ID/DXN and not `FunctionDefinition.key`).
121
+ */
122
+ const projectFunctionToTool = (fn: FunctionDefinition.Any): Tool.Any => {
123
+ if (toolCache.has(fn)) {
124
+ return toolCache.get(fn)!;
125
+ }
126
+
127
+ // TODO(burdon): Use and map function.key?
128
+ // TODO(dmaretskyi): Use function key instead.
129
+ const tool = Tool.make(makeToolName(fn.name), {
130
+ description: fn.description,
131
+ parameters: createStructFieldsFromSchema(fn.inputSchema),
132
+ // TODO(dmaretskyi): Include output schema.
133
+ failure: Schema.Any, // TODO(dmaretskyi): Better type for the failure?
134
+ }).annotate(FunctionToolAnnotation, { definition: fn });
135
+ toolCache.set(fn, tool);
136
+ return tool;
137
+ };
138
+
139
+ /**
140
+ * @returns Tool name produced from function name by escaping invalid characters.
141
+ */
142
+ const makeToolName = (name: string) => {
143
+ const toolName = name.toLowerCase().replace(/[^a-zA-Z0-9]/g, '-');
144
+ invariant(toolName.match(/^[a-z_][a-z0-9-_]*$/));
145
+ return toolName;
146
+ };
147
+
148
+ // TODO(dmaretskyi): Factor out.
149
+ const createStructFieldsFromSchema = (schema: Schema.Schema<any, any>): Record<string, Schema.Schema<any, any>> => {
150
+ switch (schema.ast._tag) {
151
+ case 'TypeLiteral':
152
+ return Object.fromEntries(schema.ast.propertySignatures.map((prop) => [prop.name, Schema.make(prop.type)]));
153
+ case 'VoidKeyword':
154
+ return {};
155
+ default:
156
+ return todo(`Unsupported schema AST: ${schema.ast._tag}`);
157
+ }
158
+ };
159
+
160
+ const isHandlerLike = (value: unknown): value is Toolkit.WithHandler<Record<string, Tool.Any>> => {
161
+ return typeof (value as any).tools === 'object' && typeof (value as any).handle === 'function';
162
+ };
package/src/index.ts CHANGED
@@ -2,11 +2,11 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- export * from './blueprint';
6
- export * from './context';
7
5
  export * from './conversation';
8
- export * from './extraction';
9
- export * from './research';
6
+ export * from './diff';
7
+ export * from './errors';
8
+ export * from './execution-graph';
9
+ export * from './functions';
10
10
  export * from './session';
11
+ export * from './templates';
11
12
  export * from './types';
12
- export * from './conversation';