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

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 (145) hide show
  1. package/dist/lib/browser/chunk-6WRGDEMH.mjs +89 -0
  2. package/dist/lib/browser/chunk-6WRGDEMH.mjs.map +7 -0
  3. package/dist/lib/browser/chunk-NZGGCEF3.mjs +341 -0
  4. package/dist/lib/browser/chunk-NZGGCEF3.mjs.map +7 -0
  5. package/dist/lib/browser/extraction/index.mjs +269 -0
  6. package/dist/lib/browser/extraction/index.mjs.map +7 -0
  7. package/dist/lib/browser/index.mjs +240 -1905
  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-O46UTCLR.mjs +13 -0
  11. package/dist/lib/browser/named-entity-recognition-O46UTCLR.mjs.map +7 -0
  12. package/dist/lib/node-esm/chunk-3UU5V4FS.mjs +91 -0
  13. package/dist/lib/node-esm/chunk-3UU5V4FS.mjs.map +7 -0
  14. package/dist/lib/node-esm/chunk-XOOPGSGW.mjs +343 -0
  15. package/dist/lib/node-esm/chunk-XOOPGSGW.mjs.map +7 -0
  16. package/dist/lib/node-esm/extraction/index.mjs +270 -0
  17. package/dist/lib/node-esm/extraction/index.mjs.map +7 -0
  18. package/dist/lib/node-esm/index.mjs +240 -1906
  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-W4VMRCH6.mjs +14 -0
  22. package/dist/lib/node-esm/named-entity-recognition-W4VMRCH6.mjs.map +7 -0
  23. package/dist/types/src/conversation/context.d.ts +90 -0
  24. package/dist/types/src/conversation/context.d.ts.map +1 -0
  25. package/dist/types/src/conversation/conversation.d.ts +31 -25
  26. package/dist/types/src/conversation/conversation.d.ts.map +1 -1
  27. package/dist/types/src/conversation/index.d.ts +1 -0
  28. package/dist/types/src/conversation/index.d.ts.map +1 -1
  29. package/dist/types/src/errors.d.ts +39 -0
  30. package/dist/types/src/errors.d.ts.map +1 -0
  31. package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -1
  32. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +143 -45
  33. package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -1
  34. package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts +1 -1
  35. package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts.map +1 -1
  36. package/dist/types/src/extraction/extraction.d.ts +106 -44
  37. package/dist/types/src/extraction/extraction.d.ts.map +1 -1
  38. package/dist/types/src/functions/index.d.ts +2 -0
  39. package/dist/types/src/functions/index.d.ts.map +1 -0
  40. package/dist/types/src/functions/services.d.ts +7 -0
  41. package/dist/types/src/functions/services.d.ts.map +1 -0
  42. package/dist/types/src/index.d.ts +3 -5
  43. package/dist/types/src/index.d.ts.map +1 -1
  44. package/dist/types/src/session/artifact-diff.d.ts +27 -0
  45. package/dist/types/src/session/artifact-diff.d.ts.map +1 -0
  46. package/dist/types/src/session/format.d.ts +168 -0
  47. package/dist/types/src/session/format.d.ts.map +1 -0
  48. package/dist/types/src/session/format.test.d.ts +2 -0
  49. package/dist/types/src/session/format.test.d.ts.map +1 -0
  50. package/dist/types/src/session/index.d.ts +1 -0
  51. package/dist/types/src/session/index.d.ts.map +1 -1
  52. package/dist/types/src/session/session.d.ts +123 -239
  53. package/dist/types/src/session/session.d.ts.map +1 -1
  54. package/dist/types/src/session/version-pin.d.ts +4 -4
  55. package/dist/types/src/session/version-pin.d.ts.map +1 -1
  56. package/dist/types/src/templates/index.d.ts +2 -0
  57. package/dist/types/src/templates/index.d.ts.map +1 -0
  58. package/dist/types/src/templates/system.d.ts +22 -0
  59. package/dist/types/src/templates/system.d.ts.map +1 -0
  60. package/dist/types/src/types.d.ts +13 -0
  61. package/dist/types/src/types.d.ts.map +1 -1
  62. package/dist/types/tsconfig.tsbuildinfo +1 -1
  63. package/package.json +46 -22
  64. package/src/{context/binder.ts → conversation/context.ts} +51 -19
  65. package/src/conversation/conversation.ts +90 -68
  66. package/src/conversation/index.ts +1 -0
  67. package/src/errors.ts +10 -0
  68. package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +7 -18
  69. package/src/extraction/extraction-llm-function/extraction-llm-function.ts +22 -17
  70. package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +4 -3
  71. package/src/extraction/extraction-ner-function/extraction-ner-function.ts +7 -4
  72. package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +2 -2
  73. package/src/extraction/extraction-ner-function/named-entity-recognition.ts +1 -1
  74. package/src/extraction/quotes.test.ts +1 -1
  75. package/src/extraction/quotes.ts +1 -1
  76. package/src/functions/index.ts +5 -0
  77. package/src/functions/services.ts +114 -0
  78. package/src/index.ts +3 -5
  79. package/src/session/__snapshots__/session.test.ts.snap +32 -0
  80. package/src/session/artifact-diff.ts +37 -0
  81. package/src/session/format.test.ts +60 -0
  82. package/src/session/format.ts +139 -0
  83. package/src/session/index.ts +1 -0
  84. package/src/session/session.test.ts +87 -57
  85. package/src/session/session.ts +220 -523
  86. package/src/session/version-pin.test.ts +2 -21
  87. package/src/session/version-pin.ts +9 -9
  88. package/src/summarization/summarization.test.ts +82 -106
  89. package/src/templates/format.tpl +55 -0
  90. package/src/templates/index.ts +5 -0
  91. package/src/templates/system.tpl +47 -0
  92. package/src/templates/system.ts +32 -0
  93. package/src/types.ts +45 -1
  94. package/src/typings.d.ts +5 -0
  95. package/dist/types/bin/chat.d.ts +0 -2
  96. package/dist/types/bin/chat.d.ts.map +0 -1
  97. package/dist/types/src/blueprint/blueprint.d.ts +0 -41
  98. package/dist/types/src/blueprint/blueprint.d.ts.map +0 -1
  99. package/dist/types/src/blueprint/index.d.ts +0 -3
  100. package/dist/types/src/blueprint/index.d.ts.map +0 -1
  101. package/dist/types/src/blueprint/registry.d.ts +0 -7
  102. package/dist/types/src/blueprint/registry.d.ts.map +0 -1
  103. package/dist/types/src/context/binder.d.ts +0 -34
  104. package/dist/types/src/context/binder.d.ts.map +0 -1
  105. package/dist/types/src/context/binding.d.ts +0 -38
  106. package/dist/types/src/context/binding.d.ts.map +0 -1
  107. package/dist/types/src/context/index.d.ts +0 -3
  108. package/dist/types/src/context/index.d.ts.map +0 -1
  109. package/dist/types/src/research/exa.d.ts +0 -7
  110. package/dist/types/src/research/exa.d.ts.map +0 -1
  111. package/dist/types/src/research/graph.d.ts +0 -41
  112. package/dist/types/src/research/graph.d.ts.map +0 -1
  113. package/dist/types/src/research/graph.test.d.ts +0 -2
  114. package/dist/types/src/research/graph.test.d.ts.map +0 -1
  115. package/dist/types/src/research/index.d.ts +0 -4
  116. package/dist/types/src/research/index.d.ts.map +0 -1
  117. package/dist/types/src/research/research.d.ts +0 -10
  118. package/dist/types/src/research/research.d.ts.map +0 -1
  119. package/dist/types/src/research/research.test.d.ts +0 -2
  120. package/dist/types/src/research/research.test.d.ts.map +0 -1
  121. package/dist/types/src/testing/exa-search-1748337321991.d.ts +0 -38
  122. package/dist/types/src/testing/exa-search-1748337321991.d.ts.map +0 -1
  123. package/dist/types/src/testing/exa-search-1748337331526.d.ts +0 -37
  124. package/dist/types/src/testing/exa-search-1748337331526.d.ts.map +0 -1
  125. package/dist/types/src/testing/exa-search-1748337344119.d.ts +0 -58
  126. package/dist/types/src/testing/exa-search-1748337344119.d.ts.map +0 -1
  127. package/dist/types/src/testing/index.d.ts +0 -3
  128. package/dist/types/src/testing/index.d.ts.map +0 -1
  129. package/src/blueprint/blueprint.ts +0 -75
  130. package/src/blueprint/index.ts +0 -6
  131. package/src/blueprint/registry.ts +0 -15
  132. package/src/context/binding.ts +0 -31
  133. package/src/context/index.ts +0 -6
  134. package/src/research/exa.ts +0 -100
  135. package/src/research/graph.test.ts +0 -15
  136. package/src/research/graph.ts +0 -337
  137. package/src/research/index.ts +0 -7
  138. package/src/research/instructions-research.tpl +0 -97
  139. package/src/research/research.test.ts +0 -190
  140. package/src/research/research.ts +0 -90
  141. package/src/testing/exa-search-1748337321991.ts +0 -131
  142. package/src/testing/exa-search-1748337331526.ts +0 -144
  143. package/src/testing/exa-search-1748337344119.ts +0 -133
  144. package/src/testing/index.ts +0 -11
  145. package/src/typedefs.d.ts +0 -8
@@ -0,0 +1,269 @@
1
+ import {
2
+ AiSession
3
+ } from "../chunk-NZGGCEF3.mjs";
4
+ import {
5
+ combineNerTokens,
6
+ createTokenGroups,
7
+ extractFullEntities,
8
+ getNer
9
+ } from "../chunk-6WRGDEMH.mjs";
10
+
11
+ // src/extraction/extraction.ts
12
+ import { Schema } from "effect";
13
+ import { asyncTimeout } from "@dxos/async";
14
+ import { Expando } from "@dxos/echo-schema";
15
+ import { log } from "@dxos/log";
16
+ import { DataType } from "@dxos/schema";
17
+ var __dxlog_file = "/__w/dxos/dxos/packages/core/assistant/src/extraction/extraction.ts";
18
+ var ExtractionInput = Schema.Struct({
19
+ message: DataType.Message,
20
+ objects: Schema.optional(Schema.Array(Expando)),
21
+ options: Schema.optional(Schema.Struct({
22
+ timeout: Schema.optional(Schema.Number),
23
+ fallbackToRaw: Schema.optional(Schema.Boolean)
24
+ }))
25
+ });
26
+ var ExtractionOutput = Schema.Struct({
27
+ message: DataType.Message,
28
+ timeElapsed: Schema.Number
29
+ });
30
+ var processTranscriptMessage = async (params) => {
31
+ try {
32
+ if (params.options?.timeout && params.options.timeout > 0) {
33
+ return await asyncTimeout(params.executor.invoke(params.function, params.input), params.options.timeout);
34
+ } else {
35
+ return await params.executor.invoke(params.function, params.input);
36
+ }
37
+ } catch (error) {
38
+ if (params.options?.fallbackToRaw) {
39
+ log.warn("failed to process message, falling back to raw text", {
40
+ error
41
+ }, {
42
+ F: __dxlog_file,
43
+ L: 66,
44
+ S: void 0,
45
+ C: (f, a) => f(...a)
46
+ });
47
+ return {
48
+ message: params.input.message,
49
+ timeElapsed: 0
50
+ };
51
+ } else {
52
+ throw error;
53
+ }
54
+ }
55
+ };
56
+
57
+ // src/extraction/extraction-llm-function/extraction-llm-function.ts
58
+ import { AiService, ToolRegistry } from "@dxos/ai";
59
+ import { Obj } from "@dxos/echo";
60
+ import { create } from "@dxos/echo-schema";
61
+ import { defineFunction } from "@dxos/functions";
62
+ import { DataType as DataType2 } from "@dxos/schema";
63
+
64
+ // src/extraction/quotes.ts
65
+ import { Schema as Schema2 } from "effect";
66
+ import { Filter, Query } from "@dxos/echo";
67
+ import { ObjectId } from "@dxos/echo-schema";
68
+ import { DXN } from "@dxos/keys";
69
+ import { log as log2 } from "@dxos/log";
70
+ var __dxlog_file2 = "/__w/dxos/dxos/packages/core/assistant/src/extraction/quotes.ts";
71
+ var ReferencedQuotes = Schema2.Struct({
72
+ references: Schema2.Array(Schema2.Struct({
73
+ id: Schema2.String,
74
+ quote: Schema2.String
75
+ })).pipe(Schema2.mutable).annotations({
76
+ // TODO(burdon): Does this description make sense?
77
+ description: `
78
+ The references to the context objects that are mentioned in the transcript.
79
+ Quote should match the original transcript text exactly, while id is the id of the context object.
80
+ `
81
+ })
82
+ });
83
+ var findQuotes = async (quotes, db) => {
84
+ const references = await Promise.all(quotes.map(async (quote) => {
85
+ const { objects } = await db.query(Query.select(Filter.text(quote, {
86
+ type: "vector"
87
+ }))).run();
88
+ log2.info("found quotes in vector index", {
89
+ objects,
90
+ quote
91
+ }, {
92
+ F: __dxlog_file2,
93
+ L: 35,
94
+ S: void 0,
95
+ C: (f, a) => f(...a)
96
+ });
97
+ return objects.length > 0 ? [
98
+ {
99
+ id: objects[0].id.toString(),
100
+ quote
101
+ }
102
+ ] : [];
103
+ }));
104
+ return {
105
+ references: references.flat()
106
+ };
107
+ };
108
+ var insertReferences = (text, quotes) => {
109
+ for (const quote of quotes.references) {
110
+ if (!ObjectId.isValid(quote.id)) {
111
+ continue;
112
+ }
113
+ const regex = new RegExp(quote.quote.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "gi");
114
+ text = text.replace(regex, `[${quote.quote}][${DXN.fromLocalObjectId(quote.id).toString()}]`);
115
+ }
116
+ return text;
117
+ };
118
+
119
+ // raw-loader:./instructions.tpl?raw
120
+ 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.';
121
+
122
+ // src/extraction/extraction-llm-function/extraction-llm-function.ts
123
+ var extractionAnthropicFn = defineFunction({
124
+ description: "Extract entities from the transcript message and add them to the message.",
125
+ inputSchema: ExtractionInput,
126
+ outputSchema: ExtractionOutput,
127
+ handler: async ({ data: { message, objects }, context }) => {
128
+ const startTime = performance.now();
129
+ const ai = context.getService(AiService.AiService);
130
+ const session = new AiSession({
131
+ operationModel: "configured"
132
+ });
133
+ const result = await session.runStructured(ReferencedQuotes, {
134
+ generationOptions: {
135
+ model: "@anthropic/claude-3-5-haiku-20241022"
136
+ },
137
+ client: ai.client,
138
+ systemPrompt: instructions_default,
139
+ history: [
140
+ Obj.make(DataType2.Message, {
141
+ created: (/* @__PURE__ */ new Date()).toISOString(),
142
+ sender: {
143
+ role: "user"
144
+ },
145
+ blocks: [
146
+ {
147
+ _tag: "text",
148
+ text: `<context>${JSON.stringify(objects)}</context>`
149
+ },
150
+ {
151
+ _tag: "text",
152
+ text: `<transcript>${JSON.stringify(message.blocks)}</transcript>`
153
+ }
154
+ ]
155
+ })
156
+ ],
157
+ artifacts: [],
158
+ prompt: "",
159
+ tools: [],
160
+ toolResolver: new ToolRegistry([])
161
+ });
162
+ return {
163
+ message: create(DataType2.Message, {
164
+ ...message,
165
+ blocks: message.blocks.map((block, i) => block._tag !== "transcript" ? block : {
166
+ ...block,
167
+ text: insertReferences(block.text, result)
168
+ })
169
+ }),
170
+ timeElapsed: performance.now() - startTime
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 extractionNerFn = 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: 24,
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-O46UTCLR.mjs");
200
+ const entitiesPromise = Promise.all(message.blocks.map(async (block) => {
201
+ invariant(block._tag === "transcript" || block._tag === "text", "Block must have text", {
202
+ F: __dxlog_file3,
203
+ L: 31,
204
+ S: void 0,
205
+ A: [
206
+ "block._tag === 'transcript' || block._tag === 'text'",
207
+ "'Block must have text'"
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: 36,
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, i) => {
223
+ invariant(block._tag === "transcript" || block._tag === "text", "Block must have text", {
224
+ F: __dxlog_file3,
225
+ L: 42,
226
+ S: void 0,
227
+ A: [
228
+ "block._tag === 'transcript' || block._tag === 'text'",
229
+ "'Block must have text'"
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: 50,
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
+ extractionAnthropicFn,
263
+ extractionNerFn,
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:./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, ToolRegistry } 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 extractionAnthropicFn: 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 toolResolver: new ToolRegistry([]),\n } as any); // TODO(burdon): Rewrite test.\n\n return {\n message: create(DataType.Message, {\n ...message,\n blocks: message.blocks.map((block, i) =>\n block._tag !== 'transcript'\n ? block\n : {\n ...block,\n text: insertReferences(block.text, result),\n },\n ),\n }),\n timeElapsed: performance.now() - startTime,\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// TODO(mykola): Make it use vector index to query objects and not pass objects as input.\nexport const extractionNerFn = 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', 'Block must have 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 log.info('entities', { entities });\n const quoteReferences = await findQuotes(\n entities.map((entity) => entity.word),\n db,\n );\n const blocksWithReferences = message.blocks.map((block, i) => {\n invariant(block._tag === 'transcript' || block._tag === 'text', 'Block must have text');\n return { ...block, text: insertReferences(block._tag, quoteReferences) };\n });\n const messageWithReferences = create(DataType.Message, {\n ...message,\n blocks: blocksWithReferences,\n });\n\n log.info('output', { messageWithReferences });\n\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,W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EJ;MAASC;IAAQ,GAAA;;;;;;AACrC,UAAMI,YAAYC,YAAYC,IAAG;AACjC,UAAM,EAAEC,GAAE,IAAKN,QAAQO,WAAWC,eAAAA;AAClC,UAAM,EAAEC,qBAAAA,qBAAmB,IAAK,MAAM,OAAO,0CAAA;AAE7C,UAAMC,kBAAkBC,QAAQC,IAC9Bd,QAAQe,OAAOC,IAAI,OAAOC,UAAAA;AACxBC,gBAAUD,MAAME,SAAS,gBAAgBF,MAAME,SAAS,QAAQ,wBAAA;;;;;;;;;AAChE,aAAOR,qBAAoBM,MAAMG,IAAI;IACvC,CAAA,CAAA,EACAC,KAAK,CAACC,cAAaA,UAASC,KAAI,CAAA;AAClC,UAAMD,WAAWrB,SAASuB,UAAU,MAAMC,cAAab,iBAAiBX,QAAQuB,OAAO,IAAI,MAAMZ;AACjGT,IAAAA,KAAIC,KAAK,YAAY;MAAEkB;IAAS,GAAA;;;;;;AAChC,UAAMI,kBAAkB,MAAMC,WAC5BL,SAASN,IAAI,CAACY,WAAWA,OAAOC,IAAI,GACpCrB,EAAAA;AAEF,UAAMsB,uBAAuB9B,QAAQe,OAAOC,IAAI,CAACC,OAAOc,MAAAA;AACtDb,gBAAUD,MAAME,SAAS,gBAAgBF,MAAME,SAAS,QAAQ,wBAAA;;;;;;;;;AAChE,aAAO;QAAE,GAAGF;QAAOG,MAAMY,iBAAiBf,MAAME,MAAMO,eAAAA;MAAiB;IACzE,CAAA;AACA,UAAMO,wBAAwBC,QAAOC,UAASC,SAAS;MACrD,GAAGpC;MACHe,QAAQe;IACV,CAAA;AAEA3B,IAAAA,KAAIC,KAAK,UAAU;MAAE6B;IAAsB,GAAA;;;;;;AAE3C,WAAO;MACLjC,SAASiC;MACTI,aAAa/B,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", "ToolRegistry", "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", "extractionAnthropicFn", "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", "toolResolver", "ToolRegistry", "create", "map", "block", "i", "insertReferences", "timeElapsed", "asyncTimeout", "create", "DatabaseService", "defineFunction", "invariant", "log", "DataType", "extractionNerFn", "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", "i", "insertReferences", "messageWithReferences", "create", "DataType", "Message", "timeElapsed"]
7
+ }