@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
@@ -0,0 +1,10 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, { get: all[name], enumerable: true });
5
+ };
6
+
7
+ export {
8
+ __export
9
+ };
10
+ //# sourceMappingURL=chunk-J5LGTIGS.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [],
5
+ "mappings": "",
6
+ "names": []
7
+ }
@@ -0,0 +1,225 @@
1
+ import "../chunk-J5LGTIGS.mjs";
2
+
3
+ // src/extraction/extraction.ts
4
+ import * as Schema from "effect/Schema";
5
+ import { asyncTimeout } from "@dxos/async";
6
+ import { Expando } from "@dxos/echo/internal";
7
+ import { log } from "@dxos/log";
8
+ import { Message } from "@dxos/types";
9
+ var __dxlog_file = "/__w/dxos/dxos/packages/core/assistant/src/extraction/extraction.ts";
10
+ var ExtractionInput = Schema.Struct({
11
+ message: Message.Message,
12
+ objects: Schema.optional(Schema.Array(Expando)),
13
+ options: Schema.optional(Schema.Struct({
14
+ timeout: Schema.optional(Schema.Number),
15
+ fallbackToRaw: Schema.optional(Schema.Boolean)
16
+ }))
17
+ });
18
+ var ExtractionOutput = Schema.Struct({
19
+ message: Message.Message,
20
+ timeElapsed: Schema.Number
21
+ });
22
+ var processTranscriptMessage = async (params) => {
23
+ try {
24
+ if (params.options?.timeout && params.options.timeout > 0) {
25
+ return await asyncTimeout(params.executor.invoke(params.function, params.input), params.options.timeout);
26
+ } else {
27
+ return await params.executor.invoke(params.function, params.input);
28
+ }
29
+ } catch (error) {
30
+ if (params.options?.fallbackToRaw) {
31
+ log.warn("failed to process message, falling back to raw text", {
32
+ error
33
+ }, {
34
+ F: __dxlog_file,
35
+ L: 67,
36
+ S: void 0,
37
+ C: (f, a) => f(...a)
38
+ });
39
+ return {
40
+ message: params.input.message,
41
+ timeElapsed: 0
42
+ };
43
+ } else {
44
+ throw error;
45
+ }
46
+ }
47
+ };
48
+
49
+ // src/extraction/extraction-llm-function/extraction-llm-function.ts
50
+ import { defineFunction } from "@dxos/functions";
51
+ var extractionAnthropicFunction = defineFunction({
52
+ key: "dxos.org/function/extraction/extract-entities",
53
+ name: "Extract Entities",
54
+ description: "Extract entities from the transcript message and add them to the message.",
55
+ inputSchema: ExtractionInput,
56
+ outputSchema: ExtractionOutput,
57
+ handler: async ({ data: { message, objects }, context }) => {
58
+ throw new Error("not implemented");
59
+ }
60
+ });
61
+
62
+ // src/extraction/extraction-ner-function/extraction-ner-function.ts
63
+ import { defineFunction as defineFunction2 } from "@dxos/functions";
64
+ var extractionNerFunction = defineFunction2({
65
+ key: "dxos.org/function/extraction/extract-entities",
66
+ name: "Extract Entities",
67
+ description: "Extracts named entities from a text",
68
+ inputSchema: ExtractionInput,
69
+ outputSchema: ExtractionOutput,
70
+ handler: async ({ data: { message, options }, context }) => {
71
+ throw new Error("not implemented");
72
+ }
73
+ });
74
+
75
+ // src/extraction/extraction-ner-function/named-entity-recognition.ts
76
+ import { pipeline } from "@xenova/transformers";
77
+ import { log as log2 } from "@dxos/log";
78
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/core/assistant/src/extraction/extraction-ner-function/named-entity-recognition.ts";
79
+ var _ner;
80
+ var getNer = () => {
81
+ if (!_ner) {
82
+ _ner = pipeline("ner", "Xenova/bert-base-NER").then((ner) => {
83
+ log2.info("NER model is ready", void 0, {
84
+ F: __dxlog_file2,
85
+ L: 24,
86
+ S: void 0,
87
+ C: (f, a) => f(...a)
88
+ });
89
+ return ner;
90
+ });
91
+ }
92
+ return _ner;
93
+ };
94
+ var extractFullEntities = async (text) => {
95
+ const ner = await getNer();
96
+ const entities = await ner(text);
97
+ const tokenGroups = createTokenGroups(entities);
98
+ const combinedEntities = tokenGroups.map(combineNerTokens);
99
+ return combinedEntities;
100
+ };
101
+ var createTokenGroups = (tokens) => {
102
+ const tokenGroups = [];
103
+ const canBeCombined = (prevToken, currentToken) => {
104
+ const [currentState, currentEntity] = currentToken.entity.split("-");
105
+ const [prevState, prevEntity] = prevToken.entity.split("-");
106
+ if (prevEntity !== currentEntity) {
107
+ return false;
108
+ }
109
+ if (prevState === "O") {
110
+ return false;
111
+ }
112
+ if (currentState === "I") {
113
+ return true;
114
+ }
115
+ if ((prevState === "B" || prevState === "I") && currentState === "B") {
116
+ return currentToken.word.startsWith("#");
117
+ }
118
+ return false;
119
+ };
120
+ for (const token of tokens) {
121
+ if (tokenGroups.length === 0) {
122
+ tokenGroups.push([
123
+ token
124
+ ]);
125
+ continue;
126
+ }
127
+ const lastTokenGroup = tokenGroups.at(-1);
128
+ if (canBeCombined(lastTokenGroup.at(-1), token)) {
129
+ lastTokenGroup.push(token);
130
+ continue;
131
+ }
132
+ tokenGroups.push([
133
+ token
134
+ ]);
135
+ }
136
+ return tokenGroups;
137
+ };
138
+ var combineNerTokens = (group) => {
139
+ const combinedToken = {
140
+ entity: group.at(0).entity.split("-")[1],
141
+ score: group.reduce((acc, token) => acc + token.score, 0) / group.length,
142
+ index: group.at(0).index,
143
+ word: group.at(0).word,
144
+ start: group.at(0).start,
145
+ end: group.at(-1).end
146
+ };
147
+ for (const token of group.slice(1)) {
148
+ if (token.word.startsWith("#")) {
149
+ combinedToken.word += token.word.replace(/^#+/, "");
150
+ } else {
151
+ combinedToken.word += " " + token.word;
152
+ }
153
+ }
154
+ return combinedToken;
155
+ };
156
+
157
+ // src/extraction/quotes.ts
158
+ import * as Schema2 from "effect/Schema";
159
+ import { Filter, Query } from "@dxos/echo";
160
+ import { ObjectId } from "@dxos/echo/internal";
161
+ import { DXN } from "@dxos/keys";
162
+ import { log as log3 } from "@dxos/log";
163
+ var __dxlog_file3 = "/__w/dxos/dxos/packages/core/assistant/src/extraction/quotes.ts";
164
+ var ReferencedQuotes = Schema2.Struct({
165
+ references: Schema2.Array(Schema2.Struct({
166
+ id: Schema2.String,
167
+ quote: Schema2.String
168
+ })).pipe(Schema2.mutable).annotations({
169
+ // TODO(burdon): Does this description make sense?
170
+ description: `
171
+ The references to the context objects that are mentioned in the transcript.
172
+ Quote should match the original transcript text exactly, while id is the id of the context object.
173
+ `
174
+ })
175
+ });
176
+ var findQuotes = async (quotes, db) => {
177
+ const references = await Promise.all(quotes.map(async (quote) => {
178
+ const { objects } = await db.query(Query.select(Filter.text(quote, {
179
+ type: "vector"
180
+ }))).run();
181
+ log3.info("found quotes in vector index", {
182
+ objects,
183
+ quote
184
+ }, {
185
+ F: __dxlog_file3,
186
+ L: 35,
187
+ S: void 0,
188
+ C: (f, a) => f(...a)
189
+ });
190
+ return objects.length > 0 ? [
191
+ {
192
+ id: objects[0].id.toString(),
193
+ quote
194
+ }
195
+ ] : [];
196
+ }));
197
+ return {
198
+ references: references.flat()
199
+ };
200
+ };
201
+ var insertReferences = (text, quotes) => {
202
+ for (const quote of quotes.references) {
203
+ if (!ObjectId.isValid(quote.id)) {
204
+ continue;
205
+ }
206
+ const regex = new RegExp(quote.quote.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "gi");
207
+ text = text.replace(regex, `[${quote.quote}](${DXN.fromLocalObjectId(quote.id).toString()})`);
208
+ }
209
+ return text;
210
+ };
211
+ export {
212
+ ExtractionInput,
213
+ ExtractionOutput,
214
+ ReferencedQuotes,
215
+ combineNerTokens,
216
+ createTokenGroups,
217
+ extractFullEntities,
218
+ extractionAnthropicFunction,
219
+ extractionNerFunction,
220
+ findQuotes,
221
+ getNer,
222
+ insertReferences,
223
+ processTranscriptMessage
224
+ };
225
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../../src/extraction/extraction.ts", "../../../../src/extraction/extraction-llm-function/extraction-llm-function.ts", "../../../../src/extraction/extraction-ner-function/extraction-ner-function.ts", "../../../../src/extraction/extraction-ner-function/named-entity-recognition.ts", "../../../../src/extraction/quotes.ts"],
4
+ "sourcesContent": ["//\n// Copyright 2025 DXOS.org\n//\n\nimport * as Schema from 'effect/Schema';\n\nimport { asyncTimeout } from '@dxos/async';\nimport { Expando } from '@dxos/echo/internal';\nimport { type FunctionDefinition } from '@dxos/functions';\nimport { type FunctionExecutor } from '@dxos/functions-runtime';\nimport { log } from '@dxos/log';\nimport { Message } from '@dxos/types';\n\nexport const ExtractionInput = Schema.Struct({\n message: Message.Message,\n objects: Schema.optional(Schema.Array(Expando)),\n options: Schema.optional(\n Schema.Struct({\n timeout: Schema.optional(Schema.Number),\n fallbackToRaw: Schema.optional(Schema.Boolean),\n }),\n ),\n});\nexport interface ExtractionInput extends Schema.Schema.Type<typeof ExtractionInput> {}\n\nexport const ExtractionOutput = Schema.Struct({\n message: Message.Message,\n timeElapsed: Schema.Number,\n});\nexport interface ExtractionOutput extends Schema.Schema.Type<typeof ExtractionOutput> {}\n\nexport type ProcessTranscriptMessageParams = {\n input: ExtractionInput;\n function: FunctionDefinition<ExtractionInput, ExtractionOutput>;\n executor: FunctionExecutor;\n\n options?: {\n /**\n * Timeout for the entity extraction.\n */\n timeout?: number;\n\n /**\n * Fallback to raw text if the entity extraction fails.\n * Otherwise the function will throw an error.\n * @default false\n */\n fallbackToRaw?: boolean;\n };\n};\n\nexport type ExtractionFunction = FunctionDefinition<ExtractionInput, ExtractionOutput>;\n\n/**\n * Extract entities from the transcript message and add them to the message.\n */\n// TODO(dmaretskyi): Move context to a vector search index.\nexport const processTranscriptMessage = async (params: ProcessTranscriptMessageParams): Promise<ExtractionOutput> => {\n try {\n if (params.options?.timeout && params.options.timeout > 0) {\n return await asyncTimeout(params.executor.invoke(params.function, params.input), params.options.timeout);\n } else {\n return await params.executor.invoke(params.function, params.input);\n }\n } catch (error) {\n if (params.options?.fallbackToRaw) {\n log.warn('failed to process message, falling back to raw text', { error });\n return {\n message: params.input.message,\n timeElapsed: 0,\n };\n } else {\n throw error;\n }\n }\n};\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { type FunctionDefinition, defineFunction } from '@dxos/functions';\n\nimport { ExtractionInput, ExtractionOutput } from '../extraction';\n\nexport const extractionAnthropicFunction: FunctionDefinition<ExtractionInput, ExtractionOutput> = defineFunction({\n key: 'dxos.org/function/extraction/extract-entities',\n name: 'Extract Entities',\n description: 'Extract entities from the transcript message and add them to the message.',\n inputSchema: ExtractionInput,\n outputSchema: ExtractionOutput,\n handler: async ({ data: { message, objects }, context }) => {\n throw new Error('not implemented');\n // const startTime = performance.now();\n // const ai = context.getService(AiService.AiService);\n // const session = new AiSession({ operationModel: 'configured' });\n // const result = await session.runStructured(ReferencedQuotes, {\n // generationOptions: {\n // model: '@anthropic/claude-3-5-haiku-20241022',\n // },\n // client: ai.client,\n // systemPrompt: PROMPT,\n // history: [\n // Obj.make(DataType.Message.Message, {\n // created: new Date().toISOString(),\n // sender: { role: 'user' },\n // blocks: [\n // {\n // _tag: 'text',\n // text: `<context>${JSON.stringify(objects)}</context>`,\n // } satisfies DataType.ContentBlock.Text,\n // {\n // _tag: 'text',\n // text: `<transcript>${JSON.stringify(message.blocks)}</transcript>`,\n // } satisfies DataType.ContentBlock.Text,\n // ],\n // }),\n // ],\n // artifacts: [],\n // prompt: '',\n // tools: [],\n // } as any); // TODO(burdon): Rewrite test.\n\n // return {\n // timeElapsed: performance.now() - startTime,\n // message: create(DataType.Message.Message, {\n // ...message,\n // blocks: message.blocks.map((block) =>\n // block._tag !== 'transcript'\n // ? block\n // : {\n // ...block,\n // text: insertReferences(block.text, result),\n // },\n // ),\n // }),\n // };\n },\n});\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport { defineFunction } from '@dxos/functions';\n\nimport { ExtractionInput, ExtractionOutput } from '../extraction';\n\n/**\n * Named Entity Recognition model\n */\n// TODO(mykola): Make it use vector index to query objects and not pass objects as input.\nexport const extractionNerFunction = defineFunction({\n key: 'dxos.org/function/extraction/extract-entities',\n name: 'Extract Entities',\n description: 'Extracts named entities from a text',\n inputSchema: ExtractionInput,\n outputSchema: ExtractionOutput,\n handler: async ({ data: { message, options }, context }) => {\n throw new Error('not implemented');\n // log.info('input', { message, options });\n // const startTime = performance.now();\n // const { db } = context.getService(DatabaseService);\n // const { extractFullEntities } = await import('./named-entity-recognition');\n\n // const entitiesPromise = Promise.all(\n // message.blocks.map(async (block) => {\n // invariant(block._tag === 'transcript' || block._tag === 'text');\n // return extractFullEntities(block.text);\n // }),\n // ).then((entities) => entities.flat());\n // const entities = options?.timeout ? await asyncTimeout(entitiesPromise, options.timeout) : await entitiesPromise;\n\n // log.info('entities', { entities });\n // const quoteReferences = await findQuotes(\n // entities.map((entity) => entity.word),\n // db,\n // );\n\n // const blocksWithReferences = message.blocks.map((block) => {\n // invariant(block._tag === 'transcript' || block._tag === 'text');\n // return { ...block, text: insertReferences(block._tag, quoteReferences) };\n // });\n\n // const messageWithReferences = create(Message.Message, {\n // ...message,\n // blocks: blocksWithReferences,\n // });\n\n // log.info('output', { messageWithReferences });\n // return {\n // message: messageWithReferences,\n // timeElapsed: performance.now() - startTime,\n // };\n },\n});\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport {\n type TokenClassificationOutput,\n type TokenClassificationPipelineType,\n type TokenClassificationSingle,\n pipeline,\n} from '@xenova/transformers';\n\nimport { log } from '@dxos/log';\n\nlet _ner: Promise<TokenClassificationPipelineType>;\n\n/**\n * Named Entity Recognition pipeline.\n * Initializes the pipeline on first call.\n * @returns The singleton promise that resolves to a token classification pipeline.\n */\nexport const getNer = () => {\n if (!_ner) {\n _ner = pipeline('ner', 'Xenova/bert-base-NER').then((ner) => {\n log.info('NER model is ready');\n return ner;\n });\n }\n\n return _ner;\n};\n\n/**\n * Extracts entities from text using the named entity recognition pipeline and does simple grouping of tokens.\n */\nexport const extractFullEntities = async (text: string): Promise<TokenClassificationSingle[]> => {\n const ner = await getNer();\n const entities = (await ner(text)) as TokenClassificationOutput;\n const tokenGroups = createTokenGroups(entities);\n const combinedEntities = tokenGroups.map(combineNerTokens);\n return combinedEntities;\n};\n\nexport const createTokenGroups = (tokens: TokenClassificationSingle[]): TokenClassificationSingle[][] => {\n const tokenGroups: TokenClassificationSingle[][] = [];\n\n const canBeCombined = (prevToken: TokenClassificationSingle, currentToken: TokenClassificationSingle) => {\n const [currentState, currentEntity] = currentToken.entity.split('-'); // B, I, O\n const [prevState, prevEntity] = prevToken.entity.split('-'); // B, I, O\n if (prevEntity !== currentEntity) {\n return false;\n }\n if (prevState === 'O') {\n return false;\n }\n if (currentState === 'I') {\n return true;\n }\n if ((prevState === 'B' || prevState === 'I') && currentState === 'B') {\n return currentToken.word.startsWith('#');\n }\n return false;\n };\n\n for (const token of tokens) {\n if (tokenGroups.length === 0) {\n tokenGroups.push([token]);\n continue;\n }\n const lastTokenGroup = tokenGroups.at(-1)!;\n if (canBeCombined(lastTokenGroup.at(-1)!, token)) {\n lastTokenGroup.push(token);\n continue;\n }\n tokenGroups.push([token]);\n }\n\n return tokenGroups;\n};\n\nexport const combineNerTokens = (group: TokenClassificationSingle[]): TokenClassificationSingle => {\n const combinedToken: TokenClassificationSingle = {\n entity: group.at(0)!.entity.split('-')[1],\n score: group.reduce((acc, token) => acc + token.score, 0) / group.length,\n index: group.at(0)!.index,\n word: group.at(0)!.word,\n start: group.at(0)!.start,\n end: group.at(-1)!.end,\n };\n\n for (const token of group.slice(1)) {\n if (token.word.startsWith('#')) {\n combinedToken.word += token.word.replace(/^#+/, '');\n } else {\n combinedToken.word += ' ' + token.word;\n }\n }\n\n return combinedToken;\n};\n", "//\n// Copyright 2025 DXOS.org\n//\n\nimport * as Schema from 'effect/Schema';\n\nimport { Filter, Query } from '@dxos/echo';\nimport { ObjectId } from '@dxos/echo/internal';\nimport { type EchoDatabase } from '@dxos/echo-db';\nimport { DXN } from '@dxos/keys';\nimport { log } from '@dxos/log';\n\nexport const ReferencedQuotes = Schema.Struct({\n references: Schema.Array(\n Schema.Struct({\n id: Schema.String,\n quote: Schema.String, // TODO(burdon): Quote?\n }),\n )\n .pipe(Schema.mutable)\n .annotations({\n // TODO(burdon): Does this description make sense?\n description: `\n The references to the context objects that are mentioned in the transcript. \n Quote should match the original transcript text exactly, while id is the id of the context object.\n `,\n }),\n});\nexport type ReferencedQuotes = Schema.Schema.Type<typeof ReferencedQuotes>;\n\nexport const findQuotes = async (quotes: string[], db: EchoDatabase): Promise<ReferencedQuotes> => {\n const references = await Promise.all(\n quotes.map(async (quote) => {\n const { objects } = await db.query(Query.select(Filter.text(quote, { type: 'vector' }))).run();\n log.info('found quotes in vector index', { objects, quote });\n return objects.length > 0 ? [{ id: objects[0].id.toString(), quote }] : [];\n }),\n );\n return { references: references.flat() };\n};\n\n/**\n * Finds and replaces all quotes with DXNs references.\n */\n// TODO(dmaretskyi): Lookup and verifiy ids from provided context.\nexport const insertReferences = (text: string, quotes: ReferencedQuotes) => {\n for (const quote of quotes.references) {\n if (!ObjectId.isValid(quote.id)) {\n continue;\n }\n\n // Use a case-insensitive regular expression to replace the quote.\n const regex = new RegExp(quote.quote.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\$&'), 'gi');\n text = text.replace(regex, `[${quote.quote}](${DXN.fromLocalObjectId(quote.id).toString()})`);\n }\n\n return text;\n};\n"],
5
+ "mappings": ";;;AAIA,YAAYA,YAAY;AAExB,SAASC,oBAAoB;AAC7B,SAASC,eAAe;AAGxB,SAASC,WAAW;AACpB,SAASC,eAAe;;AAEjB,IAAMC,kBAAyBC,cAAO;EAC3CC,SAASH,QAAQA;EACjBI,SAAgBC,gBAAgBC,aAAMR,OAAAA,CAAAA;EACtCS,SAAgBF,gBACPH,cAAO;IACZM,SAAgBH,gBAAgBI,aAAM;IACtCC,eAAsBL,gBAAgBM,cAAO;EAC/C,CAAA,CAAA;AAEJ,CAAA;AAGO,IAAMC,mBAA0BV,cAAO;EAC5CC,SAASH,QAAQA;EACjBa,aAAoBJ;AACtB,CAAA;AA6BO,IAAMK,2BAA2B,OAAOC,WAAAA;AAC7C,MAAI;AACF,QAAIA,OAAOR,SAASC,WAAWO,OAAOR,QAAQC,UAAU,GAAG;AACzD,aAAO,MAAMX,aAAakB,OAAOC,SAASC,OAAOF,OAAOG,UAAUH,OAAOI,KAAK,GAAGJ,OAAOR,QAAQC,OAAO;IACzG,OAAO;AACL,a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AAA;AAC7EX,WAAOA,KAAKW,QAAQF,OAAO,IAAIvB,MAAMA,KAAK,KAAKR,IAAIkC,kBAAkB1B,MAAMF,EAAE,EAAEqB,SAAQ,CAAA,GAAK;EAC9F;AAEA,SAAOL;AACT;",
6
+ "names": ["Schema", "asyncTimeout", "Expando", "log", "Message", "ExtractionInput", "Struct", "message", "objects", "optional", "Array", "options", "timeout", "Number", "fallbackToRaw", "Boolean", "ExtractionOutput", "timeElapsed", "processTranscriptMessage", "params", "executor", "invoke", "function", "input", "error", "warn", "defineFunction", "extractionAnthropicFunction", "defineFunction", "key", "name", "description", "inputSchema", "ExtractionInput", "outputSchema", "ExtractionOutput", "handler", "data", "message", "objects", "context", "Error", "defineFunction", "extractionNerFunction", "defineFunction", "key", "name", "description", "inputSchema", "ExtractionInput", "outputSchema", "ExtractionOutput", "handler", "data", "message", "options", "context", "Error", "pipeline", "log", "_ner", "getNer", "then", "ner", "info", "extractFullEntities", "text", "entities", "tokenGroups", "createTokenGroups", "combinedEntities", "map", "combineNerTokens", "tokens", "canBeCombined", "prevToken", "currentToken", "currentState", "currentEntity", "entity", "split", "prevState", "prevEntity", "word", "startsWith", "token", "length", "push", "lastTokenGroup", "at", "group", "combinedToken", "score", "reduce", "acc", "index", "start", "end", "slice", "replace", "Schema", "Filter", "Query", "ObjectId", "DXN", "log", "ReferencedQuotes", "Struct", "references", "Array", "id", "String", "quote", "pipe", "mutable", "annotations", "description", "findQuotes", "quotes", "db", "Promise", "all", "map", "objects", "query", "select", "text", "type", "run", "info", "length", "toString", "flat", "insertReferences", "isValid", "regex", "RegExp", "replace", "fromLocalObjectId"]
7
+ }