@dxos/assistant 0.8.3 → 0.8.4-main.1da679c

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 (168) hide show
  1. package/dist/lib/browser/chunk-3K5SRE5A.mjs +404 -0
  2. package/dist/lib/browser/chunk-3K5SRE5A.mjs.map +7 -0
  3. package/dist/lib/browser/chunk-UB26N5UF.mjs +89 -0
  4. package/dist/lib/browser/chunk-UB26N5UF.mjs.map +7 -0
  5. package/dist/lib/browser/extraction/index.mjs +268 -0
  6. package/dist/lib/browser/extraction/index.mjs.map +7 -0
  7. package/dist/lib/browser/index.mjs +412 -2101
  8. package/dist/lib/browser/index.mjs.map +4 -4
  9. package/dist/lib/browser/meta.json +1 -1
  10. package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs +13 -0
  11. package/dist/lib/browser/named-entity-recognition-QPYHML66.mjs.map +7 -0
  12. package/dist/lib/node-esm/chunk-BCFXTNSK.mjs +91 -0
  13. package/dist/lib/node-esm/chunk-BCFXTNSK.mjs.map +7 -0
  14. package/dist/lib/node-esm/chunk-W7AIB6DP.mjs +406 -0
  15. package/dist/lib/node-esm/chunk-W7AIB6DP.mjs.map +7 -0
  16. package/dist/lib/node-esm/extraction/index.mjs +269 -0
  17. package/dist/lib/node-esm/extraction/index.mjs.map +7 -0
  18. package/dist/lib/node-esm/index.mjs +412 -2102
  19. package/dist/lib/node-esm/index.mjs.map +4 -4
  20. package/dist/lib/node-esm/meta.json +1 -1
  21. package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs +14 -0
  22. package/dist/lib/node-esm/named-entity-recognition-2HDF46YX.mjs.map +7 -0
  23. package/dist/types/src/conversation/context.d.ts +96 -0
  24. package/dist/types/src/conversation/context.d.ts.map +1 -0
  25. package/dist/types/src/conversation/conversation.d.ts +42 -0
  26. package/dist/types/src/conversation/conversation.d.ts.map +1 -0
  27. package/dist/types/src/conversation/index.d.ts +3 -0
  28. package/dist/types/src/conversation/index.d.ts.map +1 -0
  29. package/dist/types/src/diff.d.ts +15 -0
  30. package/dist/types/src/diff.d.ts.map +1 -0
  31. package/dist/types/src/diff.test.d.ts +2 -0
  32. package/dist/types/src/diff.test.d.ts.map +1 -0
  33. package/dist/types/src/errors.d.ts +75 -0
  34. package/dist/types/src/errors.d.ts.map +1 -0
  35. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +1 -1
  36. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -1
  37. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +177 -48
  38. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
  39. package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts +1 -1
  40. package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts.map +1 -1
  41. package/dist/types/src/extraction/extraction.d.ts +250 -162
  42. package/dist/types/src/extraction/extraction.d.ts.map +1 -1
  43. package/dist/types/src/functions/index.d.ts +2 -0
  44. package/dist/types/src/functions/index.d.ts.map +1 -0
  45. package/dist/types/src/functions/services.d.ts +7 -0
  46. package/dist/types/src/functions/services.d.ts.map +1 -0
  47. package/dist/types/src/index.d.ts +5 -3
  48. package/dist/types/src/index.d.ts.map +1 -1
  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 +182 -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 +3 -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 +32 -246
  62. package/dist/types/src/session/session.d.ts.map +1 -1
  63. package/dist/types/src/session/toolkit.d.ts +14 -0
  64. package/dist/types/src/session/toolkit.d.ts.map +1 -0
  65. package/dist/types/src/session/version-pin.d.ts +4 -4
  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.d.ts +13 -0
  72. package/dist/types/src/types.d.ts.map +1 -1
  73. package/dist/types/tsconfig.tsbuildinfo +1 -1
  74. package/package.json +48 -32
  75. package/src/conversation/context.ts +154 -0
  76. package/src/conversation/conversation.ts +123 -0
  77. package/src/conversation/index.ts +6 -0
  78. package/src/diff.test.ts +43 -0
  79. package/src/diff.ts +79 -0
  80. package/src/errors.ts +15 -0
  81. package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +13 -24
  82. package/src/extraction/extraction-llm-function/extraction-llm-function.ts +25 -20
  83. package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +9 -9
  84. package/src/extraction/extraction-ner-function/extraction-ner-function.ts +15 -7
  85. package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +2 -2
  86. package/src/extraction/extraction-ner-function/named-entity-recognition.ts +3 -1
  87. package/src/extraction/quotes.test.ts +3 -3
  88. package/src/extraction/quotes.ts +2 -2
  89. package/src/functions/index.ts +5 -0
  90. package/src/functions/services.ts +119 -0
  91. package/src/index.ts +5 -3
  92. package/src/session/__snapshots__/session.test.ts.snap +32 -0
  93. package/src/session/artifact-diff.ts +41 -0
  94. package/src/session/error-handling.ts +39 -0
  95. package/src/session/format.test.ts +60 -0
  96. package/src/session/format.ts +139 -0
  97. package/src/session/index.ts +3 -0
  98. package/src/session/observer.ts +58 -0
  99. package/src/session/session.test.ts +98 -19
  100. package/src/session/session.ts +159 -507
  101. package/src/session/toolkit.ts +42 -0
  102. package/src/session/version-pin.test.ts +2 -21
  103. package/src/session/version-pin.ts +9 -9
  104. package/src/summarization/summarization.test.ts +82 -106
  105. package/src/templates/format.tpl +60 -0
  106. package/src/templates/index.ts +5 -0
  107. package/src/templates/system.tpl +48 -0
  108. package/src/templates/system.ts +41 -0
  109. package/src/types.ts +45 -1
  110. package/src/typings.d.ts +5 -0
  111. package/dist/lib/node/index.cjs +0 -2178
  112. package/dist/lib/node/index.cjs.map +0 -7
  113. package/dist/lib/node/meta.json +0 -1
  114. package/dist/types/bin/chat.d.ts +0 -2
  115. package/dist/types/bin/chat.d.ts.map +0 -1
  116. package/dist/types/src/blueprint/ai.test.d.ts +0 -2
  117. package/dist/types/src/blueprint/ai.test.d.ts.map +0 -1
  118. package/dist/types/src/blueprint/blueprint.d.ts +0 -55
  119. package/dist/types/src/blueprint/blueprint.d.ts.map +0 -1
  120. package/dist/types/src/blueprint/blueprint.test.d.ts +0 -2
  121. package/dist/types/src/blueprint/blueprint.test.d.ts.map +0 -1
  122. package/dist/types/src/blueprint/index.d.ts +0 -4
  123. package/dist/types/src/blueprint/index.d.ts.map +0 -1
  124. package/dist/types/src/blueprint/logger.d.ts +0 -25
  125. package/dist/types/src/blueprint/logger.d.ts.map +0 -1
  126. package/dist/types/src/blueprint/machine.d.ts +0 -143
  127. package/dist/types/src/blueprint/machine.d.ts.map +0 -1
  128. package/dist/types/src/blueprint/test-data.d.ts +0 -3
  129. package/dist/types/src/blueprint/test-data.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/testing/exa-search-1748337321991.d.ts +0 -38
  143. package/dist/types/src/testing/exa-search-1748337321991.d.ts.map +0 -1
  144. package/dist/types/src/testing/exa-search-1748337331526.d.ts +0 -37
  145. package/dist/types/src/testing/exa-search-1748337331526.d.ts.map +0 -1
  146. package/dist/types/src/testing/exa-search-1748337344119.d.ts +0 -58
  147. package/dist/types/src/testing/exa-search-1748337344119.d.ts.map +0 -1
  148. package/dist/types/src/testing/index.d.ts +0 -3
  149. package/dist/types/src/testing/index.d.ts.map +0 -1
  150. package/src/blueprint/ai.test.ts +0 -156
  151. package/src/blueprint/blueprint.test.ts +0 -151
  152. package/src/blueprint/blueprint.ts +0 -75
  153. package/src/blueprint/index.ts +0 -7
  154. package/src/blueprint/logger.ts +0 -155
  155. package/src/blueprint/machine.ts +0 -251
  156. package/src/blueprint/test-data.ts +0 -55
  157. package/src/research/exa.ts +0 -100
  158. package/src/research/graph.test.ts +0 -15
  159. package/src/research/graph.ts +0 -337
  160. package/src/research/index.ts +0 -7
  161. package/src/research/instructions-research.tpl +0 -97
  162. package/src/research/research.test.ts +0 -190
  163. package/src/research/research.ts +0 -89
  164. package/src/testing/exa-search-1748337321991.ts +0 -131
  165. package/src/testing/exa-search-1748337331526.ts +0 -144
  166. package/src/testing/exa-search-1748337344119.ts +0 -133
  167. package/src/testing/index.ts +0 -11
  168. package/src/typedefs.d.ts +0 -8
@@ -0,0 +1,269 @@
1
+ import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
2
+ import {
3
+ AiSession
4
+ } from "../chunk-W7AIB6DP.mjs";
5
+ import {
6
+ combineNerTokens,
7
+ createTokenGroups,
8
+ extractFullEntities,
9
+ getNer
10
+ } from "../chunk-BCFXTNSK.mjs";
11
+
12
+ // src/extraction/extraction.ts
13
+ import { Schema } from "effect";
14
+ import { asyncTimeout } from "@dxos/async";
15
+ import { Expando } from "@dxos/echo-schema";
16
+ import { log } from "@dxos/log";
17
+ import { DataType } from "@dxos/schema";
18
+ var __dxlog_file = "/__w/dxos/dxos/packages/core/assistant/src/extraction/extraction.ts";
19
+ var ExtractionInput = Schema.Struct({
20
+ message: DataType.Message,
21
+ objects: Schema.optional(Schema.Array(Expando)),
22
+ options: Schema.optional(Schema.Struct({
23
+ timeout: Schema.optional(Schema.Number),
24
+ fallbackToRaw: Schema.optional(Schema.Boolean)
25
+ }))
26
+ });
27
+ var ExtractionOutput = Schema.Struct({
28
+ message: DataType.Message,
29
+ timeElapsed: Schema.Number
30
+ });
31
+ var processTranscriptMessage = async (params) => {
32
+ try {
33
+ if (params.options?.timeout && params.options.timeout > 0) {
34
+ return await asyncTimeout(params.executor.invoke(params.function, params.input), params.options.timeout);
35
+ } else {
36
+ return await params.executor.invoke(params.function, params.input);
37
+ }
38
+ } catch (error) {
39
+ if (params.options?.fallbackToRaw) {
40
+ log.warn("failed to process message, falling back to raw text", {
41
+ error
42
+ }, {
43
+ F: __dxlog_file,
44
+ L: 66,
45
+ S: void 0,
46
+ C: (f, a) => f(...a)
47
+ });
48
+ return {
49
+ message: params.input.message,
50
+ timeElapsed: 0
51
+ };
52
+ } else {
53
+ throw error;
54
+ }
55
+ }
56
+ };
57
+
58
+ // src/extraction/extraction-llm-function/extraction-llm-function.ts
59
+ import { AiService } from "@dxos/ai";
60
+ import { Obj } from "@dxos/echo";
61
+ import { create } from "@dxos/echo-schema";
62
+ import { defineFunction } from "@dxos/functions";
63
+ import { DataType as DataType2 } from "@dxos/schema";
64
+
65
+ // src/extraction/quotes.ts
66
+ import { Schema as Schema2 } from "effect";
67
+ import { Filter, Query } from "@dxos/echo";
68
+ import { ObjectId } from "@dxos/echo-schema";
69
+ import { DXN } from "@dxos/keys";
70
+ import { log as log2 } from "@dxos/log";
71
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/core/assistant/src/extraction/quotes.ts";
72
+ var ReferencedQuotes = Schema2.Struct({
73
+ references: Schema2.Array(Schema2.Struct({
74
+ id: Schema2.String,
75
+ quote: Schema2.String
76
+ })).pipe(Schema2.mutable).annotations({
77
+ // TODO(burdon): Does this description make sense?
78
+ description: `
79
+ The references to the context objects that are mentioned in the transcript.
80
+ Quote should match the original transcript text exactly, while id is the id of the context object.
81
+ `
82
+ })
83
+ });
84
+ var findQuotes = async (quotes, db) => {
85
+ const references = await Promise.all(quotes.map(async (quote) => {
86
+ const { objects } = await db.query(Query.select(Filter.text(quote, {
87
+ type: "vector"
88
+ }))).run();
89
+ log2.info("found quotes in vector index", {
90
+ objects,
91
+ quote
92
+ }, {
93
+ F: __dxlog_file2,
94
+ L: 35,
95
+ S: void 0,
96
+ C: (f, a) => f(...a)
97
+ });
98
+ return objects.length > 0 ? [
99
+ {
100
+ id: objects[0].id.toString(),
101
+ quote
102
+ }
103
+ ] : [];
104
+ }));
105
+ return {
106
+ references: references.flat()
107
+ };
108
+ };
109
+ var insertReferences = (text, quotes) => {
110
+ for (const quote of quotes.references) {
111
+ if (!ObjectId.isValid(quote.id)) {
112
+ continue;
113
+ }
114
+ const regex = new RegExp(quote.quote.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "gi");
115
+ text = text.replace(regex, `[${quote.quote}](${DXN.fromLocalObjectId(quote.id).toString()})`);
116
+ }
117
+ return text;
118
+ };
119
+
120
+ // raw-loader:/__w/dxos/dxos/packages/core/assistant/src/extraction/extraction-llm-function/instructions.tpl?raw
121
+ var instructions_default = 'You are an AI assistant specialized in processing and enhancing transcripts with contextual information.\nYour task is to analyze a given transcript and extract references to the context objects.\n\nThe context and transcript are provided in the user message in `context` and `transcript` XML tags.\n\nHere are your instructions:\n\n1. Read the transcript and context information provided above.\n\n2. Process each segment of the transcript separately.\n\n3. For each segment:\n a. Analyze the content and identify relevant information from the context.\n b. Extract a number of references to the context objects that are mentioned in the segment.\n c. Output the references in the format `{ "quote": "earnings report", "id": "01JT0JP9AX0XKGZX4MV4B69VT6" }`.\n d. If there are no references, just output an empty array.\n\n4. Maintain the exact order and structure of the original transcript segments.\n\nOutput Format:\nYou have been provided with a tool that specifies output format\n\nRemember:\n- Only quote the text that is in the transcript.\n- Analyze each segment carefully before processing it.\n- Only reference objects that appear in the context and exactly by their id, If the object is not in the context, do not insert a reference. \n\nNow, process the transcript and context provided below according to these instructions.';
122
+
123
+ // src/extraction/extraction-llm-function/extraction-llm-function.ts
124
+ var extractionAnthropicFunction = defineFunction({
125
+ description: "Extract entities from the transcript message and add them to the message.",
126
+ inputSchema: ExtractionInput,
127
+ outputSchema: ExtractionOutput,
128
+ handler: async ({ data: { message, objects }, context }) => {
129
+ const startTime = performance.now();
130
+ const ai = context.getService(AiService.AiService);
131
+ const session = new AiSession({
132
+ operationModel: "configured"
133
+ });
134
+ const result = await session.runStructured(ReferencedQuotes, {
135
+ generationOptions: {
136
+ model: "@anthropic/claude-3-5-haiku-20241022"
137
+ },
138
+ client: ai.client,
139
+ systemPrompt: instructions_default,
140
+ history: [
141
+ Obj.make(DataType2.Message, {
142
+ created: (/* @__PURE__ */ new Date()).toISOString(),
143
+ sender: {
144
+ role: "user"
145
+ },
146
+ blocks: [
147
+ {
148
+ _tag: "text",
149
+ text: `<context>${JSON.stringify(objects)}</context>`
150
+ },
151
+ {
152
+ _tag: "text",
153
+ text: `<transcript>${JSON.stringify(message.blocks)}</transcript>`
154
+ }
155
+ ]
156
+ })
157
+ ],
158
+ artifacts: [],
159
+ prompt: "",
160
+ tools: []
161
+ });
162
+ return {
163
+ timeElapsed: performance.now() - startTime,
164
+ message: create(DataType2.Message, {
165
+ ...message,
166
+ blocks: message.blocks.map((block) => block._tag !== "transcript" ? block : {
167
+ ...block,
168
+ text: insertReferences(block.text, result)
169
+ })
170
+ })
171
+ };
172
+ }
173
+ });
174
+
175
+ // src/extraction/extraction-ner-function/extraction-ner-function.ts
176
+ import { asyncTimeout as asyncTimeout2 } from "@dxos/async";
177
+ import { create as create2 } from "@dxos/echo-schema";
178
+ import { DatabaseService, defineFunction as defineFunction2 } from "@dxos/functions";
179
+ import { invariant } from "@dxos/invariant";
180
+ import { log as log3 } from "@dxos/log";
181
+ import { DataType as DataType3 } from "@dxos/schema";
182
+ var __dxlog_file3 = "/__w/dxos/dxos/packages/core/assistant/src/extraction/extraction-ner-function/extraction-ner-function.ts";
183
+ var extractionNerFunction = defineFunction2({
184
+ description: "Extracts named entities from a text",
185
+ inputSchema: ExtractionInput,
186
+ outputSchema: ExtractionOutput,
187
+ handler: async ({ data: { message, options }, context }) => {
188
+ log3.info("input", {
189
+ message,
190
+ options
191
+ }, {
192
+ F: __dxlog_file3,
193
+ L: 27,
194
+ S: void 0,
195
+ C: (f, a) => f(...a)
196
+ });
197
+ const startTime = performance.now();
198
+ const { db } = context.getService(DatabaseService);
199
+ const { extractFullEntities: extractFullEntities2 } = await import("../named-entity-recognition-2HDF46YX.mjs");
200
+ const entitiesPromise = Promise.all(message.blocks.map(async (block) => {
201
+ invariant(block._tag === "transcript" || block._tag === "text", void 0, {
202
+ F: __dxlog_file3,
203
+ L: 34,
204
+ S: void 0,
205
+ A: [
206
+ "block._tag === 'transcript' || block._tag === 'text'",
207
+ ""
208
+ ]
209
+ });
210
+ return extractFullEntities2(block.text);
211
+ })).then((entities2) => entities2.flat());
212
+ const entities = options?.timeout ? await asyncTimeout2(entitiesPromise, options.timeout) : await entitiesPromise;
213
+ log3.info("entities", {
214
+ entities
215
+ }, {
216
+ F: __dxlog_file3,
217
+ L: 40,
218
+ S: void 0,
219
+ C: (f, a) => f(...a)
220
+ });
221
+ const quoteReferences = await findQuotes(entities.map((entity) => entity.word), db);
222
+ const blocksWithReferences = message.blocks.map((block) => {
223
+ invariant(block._tag === "transcript" || block._tag === "text", void 0, {
224
+ F: __dxlog_file3,
225
+ L: 47,
226
+ S: void 0,
227
+ A: [
228
+ "block._tag === 'transcript' || block._tag === 'text'",
229
+ ""
230
+ ]
231
+ });
232
+ return {
233
+ ...block,
234
+ text: insertReferences(block._tag, quoteReferences)
235
+ };
236
+ });
237
+ const messageWithReferences = create2(DataType3.Message, {
238
+ ...message,
239
+ blocks: blocksWithReferences
240
+ });
241
+ log3.info("output", {
242
+ messageWithReferences
243
+ }, {
244
+ F: __dxlog_file3,
245
+ L: 56,
246
+ S: void 0,
247
+ C: (f, a) => f(...a)
248
+ });
249
+ return {
250
+ message: messageWithReferences,
251
+ timeElapsed: performance.now() - startTime
252
+ };
253
+ }
254
+ });
255
+ export {
256
+ ExtractionInput,
257
+ ExtractionOutput,
258
+ ReferencedQuotes,
259
+ combineNerTokens,
260
+ createTokenGroups,
261
+ extractFullEntities,
262
+ extractionAnthropicFunction,
263
+ extractionNerFunction,
264
+ findQuotes,
265
+ getNer,
266
+ insertReferences,
267
+ processTranscriptMessage
268
+ };
269
+ //# 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/quotes.ts", "raw-loader:/__w/dxos/dxos/packages/core/assistant/src/extraction/extraction-llm-function/instructions.tpl?raw", "../../../../src/extraction/extraction-ner-function/extraction-ner-function.ts"],
4
+ "sourcesContent": ["//\n// Copyright 2025 DXOS.org\n//\n\nimport { Schema } from 'effect';\n\nimport { asyncTimeout } from '@dxos/async';\nimport { Expando } from '@dxos/echo-schema';\nimport { type FunctionDefinition, type FunctionExecutor } from '@dxos/functions';\nimport { log } from '@dxos/log';\nimport { DataType } from '@dxos/schema';\n\nexport const ExtractionInput = Schema.Struct({\n message: DataType.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: DataType.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\n// ISSUE(burdon): defineFunction\n// @ts-nocheck\n\nimport { AiService } from '@dxos/ai';\nimport { Obj } from '@dxos/echo';\nimport { create } from '@dxos/echo-schema';\nimport { type FunctionDefinition, defineFunction } from '@dxos/functions';\nimport { type ContentBlock, DataType } from '@dxos/schema';\n\nimport { AiSession } from '../../session';\nimport { ExtractionInput, ExtractionOutput } from '../extraction';\nimport { ReferencedQuotes, insertReferences } from '../quotes';\n\nimport PROMPT from './instructions.tpl?raw';\n\nexport const extractionAnthropicFunction: FunctionDefinition<ExtractionInput, ExtractionOutput> = defineFunction({\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 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, {\n created: new Date().toISOString(),\n sender: { role: 'user' },\n blocks: [\n {\n _tag: 'text',\n text: `<context>${JSON.stringify(objects)}</context>`,\n } satisfies ContentBlock.Text,\n {\n _tag: 'text',\n text: `<transcript>${JSON.stringify(message.blocks)}</transcript>`,\n } satisfies 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, {\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 { Schema } from 'effect';\n\nimport { Filter, Query } from '@dxos/echo';\nimport { type EchoDatabase } from '@dxos/echo-db';\nimport { ObjectId } from '@dxos/echo-schema';\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", "You are an AI assistant specialized in processing and enhancing transcripts with contextual information.\nYour task is to analyze a given transcript and extract references to the context objects.\n\nThe context and transcript are provided in the user message in `context` and `transcript` XML tags.\n\nHere are your instructions:\n\n1. Read the transcript and context information provided above.\n\n2. Process each segment of the transcript separately.\n\n3. For each segment:\n a. Analyze the content and identify relevant information from the context.\n b. Extract a number of references to the context objects that are mentioned in the segment.\n c. Output the references in the format `{ \"quote\": \"earnings report\", \"id\": \"01JT0JP9AX0XKGZX4MV4B69VT6\" }`.\n d. If there are no references, just output an empty array.\n\n4. Maintain the exact order and structure of the original transcript segments.\n\nOutput Format:\nYou have been provided with a tool that specifies output format\n\nRemember:\n- Only quote the text that is in the transcript.\n- Analyze each segment carefully before processing it.\n- Only reference objects that appear in the context and exactly by their id, If the object is not in the context, do not insert a reference. \n\nNow, process the transcript and context provided below according to these instructions.", "//\n// Copyright 2025 DXOS.org\n//\n\n// ISSUE(burdon): defineFunction\n// @ts-nocheck\n\nimport { asyncTimeout } from '@dxos/async';\nimport { create } from '@dxos/echo-schema';\nimport { DatabaseService, defineFunction } from '@dxos/functions';\nimport { invariant } from '@dxos/invariant';\nimport { log } from '@dxos/log';\nimport { DataType } from '@dxos/schema';\n\nimport { ExtractionInput, ExtractionOutput } from '../extraction';\nimport { findQuotes, insertReferences } from '../quotes';\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 description: 'Extracts named entities from a text',\n inputSchema: ExtractionInput,\n outputSchema: ExtractionOutput,\n handler: async ({ data: { message, options }, context }) => {\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(DataType.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"],
5
+ "mappings": ";;;;;;;;;;;;AAIA,SAASA,cAAc;AAEvB,SAASC,oBAAoB;AAC7B,SAASC,eAAe;AAExB,SAASC,WAAW;AACpB,SAASC,gBAAgB;;AAElB,IAAMC,kBAAkBL,OAAOM,OAAO;EAC3CC,SAASH,SAASI;EAClBC,SAAST,OAAOU,SAASV,OAAOW,MAAMT,OAAAA,CAAAA;EACtCU,SAASZ,OAAOU,SACdV,OAAOM,OAAO;IACZO,SAASb,OAAOU,SAASV,OAAOc,MAAM;IACtCC,eAAef,OAAOU,SAASV,OAAOgB,OAAO;EAC/C,CAAA,CAAA;AAEJ,CAAA;AAGO,IAAMC,mBAAmBjB,OAAOM,OAAO;EAC5CC,SAASH,SAASI;EAClBU,aAAalB,OAAOc;AACtB,CAAA;AA6BO,IAAMK,2BAA2B,OAAOC,WAAAA;AAC7C,MA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AQuB,OAAO,IAAI,MAAMZ;AAEjGT,IAAAA,KAAIC,KAAK,YAAY;MAAEkB;IAAS,GAAA;;;;;;AAChC,UAAMI,kBAAkB,MAAMC,WAC5BL,SAASN,IAAI,CAACY,WAAWA,OAAOC,IAAI,GACpCrB,EAAAA;AAGF,UAAMsB,uBAAuB9B,QAAQe,OAAOC,IAAI,CAACC,UAAAA;AAC/CC,gBAAUD,MAAME,SAAS,gBAAgBF,MAAME,SAAS,QAAA,QAAA;;;;;;;;;AACxD,aAAO;QAAE,GAAGF;QAAOG,MAAMW,iBAAiBd,MAAME,MAAMO,eAAAA;MAAiB;IACzE,CAAA;AAEA,UAAMM,wBAAwBC,QAAOC,UAASC,SAAS;MACrD,GAAGnC;MACHe,QAAQe;IACV,CAAA;AAEA3B,IAAAA,KAAIC,KAAK,UAAU;MAAE4B;IAAsB,GAAA;;;;;;AAC3C,WAAO;MACLhC,SAASgC;MACTI,aAAa9B,YAAYC,IAAG,IAAKF;IACnC;EACF;AACF,CAAA;",
6
+ "names": ["Schema", "asyncTimeout", "Expando", "log", "DataType", "ExtractionInput", "Struct", "message", "Message", "objects", "optional", "Array", "options", "timeout", "Number", "fallbackToRaw", "Boolean", "ExtractionOutput", "timeElapsed", "processTranscriptMessage", "params", "executor", "invoke", "function", "input", "error", "warn", "AiService", "Obj", "create", "defineFunction", "DataType", "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", "extractionAnthropicFunction", "defineFunction", "description", "inputSchema", "ExtractionInput", "outputSchema", "ExtractionOutput", "handler", "data", "message", "objects", "context", "startTime", "performance", "now", "ai", "getService", "AiService", "session", "AiSession", "operationModel", "result", "runStructured", "ReferencedQuotes", "generationOptions", "model", "client", "systemPrompt", "PROMPT", "history", "Obj", "make", "DataType", "Message", "created", "Date", "toISOString", "sender", "role", "blocks", "_tag", "text", "JSON", "stringify", "artifacts", "prompt", "tools", "timeElapsed", "create", "map", "block", "insertReferences", "asyncTimeout", "create", "DatabaseService", "defineFunction", "invariant", "log", "DataType", "extractionNerFunction", "defineFunction", "description", "inputSchema", "ExtractionInput", "outputSchema", "ExtractionOutput", "handler", "data", "message", "options", "context", "log", "info", "startTime", "performance", "now", "db", "getService", "DatabaseService", "extractFullEntities", "entitiesPromise", "Promise", "all", "blocks", "map", "block", "invariant", "_tag", "text", "then", "entities", "flat", "timeout", "asyncTimeout", "quoteReferences", "findQuotes", "entity", "word", "blocksWithReferences", "insertReferences", "messageWithReferences", "create", "DataType", "Message", "timeElapsed"]
7
+ }