@dxos/assistant 0.8.2-main.fbd8ed0 → 0.8.2-staging.4d6ad0f
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.
- package/dist/lib/browser/index.mjs +1983 -64
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +1976 -63
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +1984 -64
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/{src/bin → bin}/chat.d.ts.map +1 -1
- package/dist/types/src/blueprint/blueprint.d.ts +79 -0
- package/dist/types/src/blueprint/blueprint.d.ts.map +1 -0
- package/dist/types/src/blueprint/blueprint.test.d.ts +2 -0
- package/dist/types/src/blueprint/blueprint.test.d.ts.map +1 -0
- package/dist/types/src/blueprint/index.d.ts +4 -0
- package/dist/types/src/blueprint/index.d.ts.map +1 -0
- package/dist/types/src/blueprint/logger.d.ts +3 -0
- package/dist/types/src/blueprint/logger.d.ts.map +1 -0
- package/dist/types/src/{ai-service/parser.d.ts → blueprint/machine.d.ts} +46 -68
- package/dist/types/src/blueprint/machine.d.ts.map +1 -0
- package/dist/types/src/blueprint/test-data.d.ts +3 -0
- package/dist/types/src/blueprint/test-data.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts +4 -0
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts +2 -0
- package/dist/types/src/extraction/extraction-llm-function/extraction-llm-function.test.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-llm-function/index.d.ts +2 -0
- package/dist/types/src/extraction/extraction-llm-function/index.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts +210 -0
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts +2 -0
- package/dist/types/src/extraction/extraction-ner-function/extraction-ner-function.test.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-ner-function/index.d.ts +3 -0
- package/dist/types/src/extraction/extraction-ner-function/index.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts +14 -0
- package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.test.d.ts +2 -0
- package/dist/types/src/extraction/extraction-ner-function/named-entity-recognition.test.d.ts.map +1 -0
- package/dist/types/src/extraction/extraction.d.ts +242 -0
- package/dist/types/src/extraction/extraction.d.ts.map +1 -0
- package/dist/types/src/extraction/index.d.ts +5 -0
- package/dist/types/src/extraction/index.d.ts.map +1 -0
- package/dist/types/src/extraction/quotes.d.ts +15 -0
- package/dist/types/src/extraction/quotes.d.ts.map +1 -0
- package/dist/types/src/extraction/quotes.test.d.ts +2 -0
- package/dist/types/src/extraction/quotes.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +4 -2
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/research/exa.d.ts +7 -0
- package/dist/types/src/research/exa.d.ts.map +1 -0
- package/dist/types/src/research/graph.d.ts +39 -0
- package/dist/types/src/research/graph.d.ts.map +1 -0
- package/dist/types/src/research/graph.test.d.ts +2 -0
- package/dist/types/src/research/graph.test.d.ts.map +1 -0
- package/dist/types/src/research/index.d.ts +4 -0
- package/dist/types/src/research/index.d.ts.map +1 -0
- package/dist/types/src/research/research.d.ts +10 -0
- package/dist/types/src/research/research.d.ts.map +1 -0
- package/dist/types/src/research/research.test.d.ts.map +1 -0
- package/dist/types/src/session/index.d.ts +0 -1
- package/dist/types/src/session/index.d.ts.map +1 -1
- package/dist/types/src/session/session.d.ts +47 -22
- package/dist/types/src/session/session.d.ts.map +1 -1
- package/dist/types/src/session/version-pin.d.ts +3 -4
- package/dist/types/src/session/version-pin.d.ts.map +1 -1
- package/dist/types/src/summarization/summarization.test.d.ts +2 -0
- package/dist/types/src/summarization/summarization.test.d.ts.map +1 -0
- package/dist/types/src/testing/exa-search-1748337321991.d.ts +38 -0
- package/dist/types/src/testing/exa-search-1748337321991.d.ts.map +1 -0
- package/dist/types/src/testing/exa-search-1748337331526.d.ts +37 -0
- package/dist/types/src/testing/exa-search-1748337331526.d.ts.map +1 -0
- package/dist/types/src/testing/exa-search-1748337344119.d.ts +58 -0
- package/dist/types/src/testing/exa-search-1748337344119.d.ts.map +1 -0
- package/dist/types/src/testing/index.d.ts +2 -7
- package/dist/types/src/testing/index.d.ts.map +1 -1
- package/dist/types/src/types.d.ts +3 -0
- package/dist/types/src/types.d.ts.map +1 -0
- package/package.json +22 -38
- package/src/blueprint/blueprint.test.ts +142 -0
- package/src/blueprint/blueprint.ts +95 -0
- package/src/blueprint/index.ts +7 -0
- package/src/blueprint/logger.ts +66 -0
- package/src/blueprint/machine.ts +208 -0
- package/src/blueprint/test-data.ts +55 -0
- package/src/extraction/extraction-llm-function/extraction-llm-function.test.ts +95 -0
- package/src/extraction/extraction-llm-function/extraction-llm-function.ts +64 -0
- package/src/extraction/extraction-llm-function/index.ts +5 -0
- package/src/extraction/extraction-llm-function/instructions.tpl +28 -0
- package/src/extraction/extraction-ner-function/extraction-ner-function.test.ts +115 -0
- package/src/extraction/extraction-ner-function/extraction-ner-function.ts +54 -0
- package/src/extraction/extraction-ner-function/index.ts +6 -0
- package/src/extraction/extraction-ner-function/named-entity-recognition.test.ts +94 -0
- package/src/extraction/extraction-ner-function/named-entity-recognition.ts +97 -0
- package/src/extraction/extraction.ts +75 -0
- package/src/extraction/index.ts +8 -0
- package/src/extraction/quotes.test.ts +27 -0
- package/src/extraction/quotes.ts +57 -0
- package/src/index.ts +4 -4
- package/src/research/exa.ts +100 -0
- package/src/research/graph.test.ts +15 -0
- package/src/research/graph.ts +291 -0
- package/src/research/index.ts +7 -0
- package/src/research/instructions-research.tpl +97 -0
- package/src/research/research.test.ts +189 -0
- package/src/research/research.ts +89 -0
- package/src/session/index.ts +0 -1
- package/src/session/session.test.ts +7 -7
- package/src/session/session.ts +46 -19
- package/src/session/version-pin.ts +2 -3
- package/src/summarization/summarization.test.ts +216 -0
- package/src/testing/exa-search-1748337321991.ts +131 -0
- package/src/testing/exa-search-1748337331526.ts +144 -0
- package/src/testing/exa-search-1748337344119.ts +133 -0
- package/src/testing/index.ts +8 -8
- package/src/types.ts +8 -0
- package/src/typings.d.ts +8 -0
- package/dist/lib/browser/chunk-B4HAWUFW.mjs +0 -2786
- package/dist/lib/browser/chunk-B4HAWUFW.mjs.map +0 -7
- package/dist/lib/browser/testing/index.mjs +0 -658
- package/dist/lib/browser/testing/index.mjs.map +0 -7
- package/dist/lib/node/chunk-NOB7CIIC.cjs +0 -2791
- package/dist/lib/node/chunk-NOB7CIIC.cjs.map +0 -7
- package/dist/lib/node/testing/index.cjs +0 -672
- package/dist/lib/node/testing/index.cjs.map +0 -7
- package/dist/lib/node-esm/chunk-SAWYU3V5.mjs +0 -2788
- package/dist/lib/node-esm/chunk-SAWYU3V5.mjs.map +0 -7
- package/dist/lib/node-esm/testing/index.mjs +0 -659
- package/dist/lib/node-esm/testing/index.mjs.map +0 -7
- package/dist/types/src/ai-service/client.test.d.ts +0 -2
- package/dist/types/src/ai-service/client.test.d.ts.map +0 -1
- package/dist/types/src/ai-service/defs.d.ts +0 -9
- package/dist/types/src/ai-service/defs.d.ts.map +0 -1
- package/dist/types/src/ai-service/edge-client.d.ts +0 -18
- package/dist/types/src/ai-service/edge-client.d.ts.map +0 -1
- package/dist/types/src/ai-service/index.d.ts +0 -10
- package/dist/types/src/ai-service/index.d.ts.map +0 -1
- package/dist/types/src/ai-service/message-collector.d.ts +0 -82
- package/dist/types/src/ai-service/message-collector.d.ts.map +0 -1
- package/dist/types/src/ai-service/ollama-client.d.ts +0 -43
- package/dist/types/src/ai-service/ollama-client.d.ts.map +0 -1
- package/dist/types/src/ai-service/parser.d.ts.map +0 -1
- package/dist/types/src/ai-service/service.d.ts +0 -25
- package/dist/types/src/ai-service/service.d.ts.map +0 -1
- package/dist/types/src/ai-service/stream.d.ts +0 -40
- package/dist/types/src/ai-service/stream.d.ts.map +0 -1
- package/dist/types/src/ai-service/stream.test.d.ts +0 -2
- package/dist/types/src/ai-service/stream.test.d.ts.map +0 -1
- package/dist/types/src/ai-service/testing/index.d.ts +0 -3
- package/dist/types/src/ai-service/testing/index.d.ts.map +0 -1
- package/dist/types/src/ai-service/testing/mock.d.ts +0 -11
- package/dist/types/src/ai-service/testing/mock.d.ts.map +0 -1
- package/dist/types/src/ai-service/testing/test-stream.d.ts +0 -7
- package/dist/types/src/ai-service/testing/test-stream.d.ts.map +0 -1
- package/dist/types/src/ai-service/transform.d.ts +0 -27
- package/dist/types/src/ai-service/transform.d.ts.map +0 -1
- package/dist/types/src/ai-service/types.d.ts +0 -210
- package/dist/types/src/ai-service/types.d.ts.map +0 -1
- package/dist/types/src/ai-service/util/index.d.ts +0 -2
- package/dist/types/src/ai-service/util/index.d.ts.map +0 -1
- package/dist/types/src/ai-service/util/sse.d.ts +0 -39
- package/dist/types/src/ai-service/util/sse.d.ts.map +0 -1
- package/dist/types/src/experimental/conversation/conversation.d.ts +0 -61
- package/dist/types/src/experimental/conversation/conversation.d.ts.map +0 -1
- package/dist/types/src/experimental/conversation/conversation.test.d.ts +0 -2
- package/dist/types/src/experimental/conversation/conversation.test.d.ts.map +0 -1
- package/dist/types/src/experimental/conversation/index.d.ts +0 -2
- package/dist/types/src/experimental/conversation/index.d.ts.map +0 -1
- package/dist/types/src/experimental/conversation/types.d.ts +0 -78
- package/dist/types/src/experimental/conversation/types.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/ast.d.ts +0 -56
- package/dist/types/src/experimental/cypher/ast.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/combinators.d.ts +0 -14
- package/dist/types/src/experimental/cypher/combinators.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/combinators.test.d.ts +0 -2
- package/dist/types/src/experimental/cypher/combinators.test.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/echo-data-source.d.ts +0 -16
- package/dist/types/src/experimental/cypher/echo-data-source.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/execution-plan.d.ts +0 -49
- package/dist/types/src/experimental/cypher/execution-plan.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/execution-plan.test.d.ts +0 -2
- package/dist/types/src/experimental/cypher/execution-plan.test.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/index.d.ts +0 -5
- package/dist/types/src/experimental/cypher/index.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/parjs.d.ts +0 -4
- package/dist/types/src/experimental/cypher/parjs.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/parser.d.ts +0 -6
- package/dist/types/src/experimental/cypher/parser.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/query-executor.d.ts +0 -26
- package/dist/types/src/experimental/cypher/query-executor.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/query-executor.test.d.ts +0 -2
- package/dist/types/src/experimental/cypher/query-executor.test.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/schema.d.ts +0 -12
- package/dist/types/src/experimental/cypher/schema.d.ts.map +0 -1
- package/dist/types/src/experimental/cypher/schema.test.d.ts +0 -2
- package/dist/types/src/experimental/cypher/schema.test.d.ts.map +0 -1
- package/dist/types/src/experimental/index.d.ts +0 -3
- package/dist/types/src/experimental/index.d.ts.map +0 -1
- package/dist/types/src/experimental/ner/ner.test.d.ts +0 -2
- package/dist/types/src/experimental/ner/ner.test.d.ts.map +0 -1
- package/dist/types/src/experimental/research/exa.d.ts +0 -6
- package/dist/types/src/experimental/research/exa.d.ts.map +0 -1
- package/dist/types/src/experimental/research/research.test.d.ts.map +0 -1
- package/dist/types/src/session/tools.d.ts +0 -18
- package/dist/types/src/session/tools.d.ts.map +0 -1
- package/dist/types/src/testing/artifacts.test.d.ts +0 -1
- package/dist/types/src/testing/artifacts.test.d.ts.map +0 -1
- package/dist/types/src/testing/data.d.ts +0 -2
- package/dist/types/src/testing/data.d.ts.map +0 -1
- package/dist/types/src/testing/defs.d.ts +0 -5
- package/dist/types/src/testing/defs.d.ts.map +0 -1
- package/dist/types/src/testing/logger.d.ts +0 -12
- package/dist/types/src/testing/logger.d.ts.map +0 -1
- package/dist/types/src/testing/object-extraction.test.d.ts +0 -1
- package/dist/types/src/testing/object-extraction.test.d.ts.map +0 -1
- package/dist/types/src/testing/ollama.d.ts +0 -6
- package/dist/types/src/testing/ollama.d.ts.map +0 -1
- package/dist/types/src/testing/printer.d.ts +0 -6
- package/dist/types/src/testing/printer.d.ts.map +0 -1
- package/dist/types/src/testing/query-promts.d.ts +0 -5
- package/dist/types/src/testing/query-promts.d.ts.map +0 -1
- package/dist/types/src/testing/query.test.d.ts +0 -2
- package/dist/types/src/testing/query.test.d.ts.map +0 -1
- package/dist/types/src/testing/test-data.d.ts +0 -29
- package/dist/types/src/testing/test-data.d.ts.map +0 -1
- package/dist/types/src/testing/test-schema.d.ts +0 -52
- package/dist/types/src/testing/test-schema.d.ts.map +0 -1
- package/dist/types/src/util/index.d.ts +0 -2
- package/dist/types/src/util/index.d.ts.map +0 -1
- package/dist/types/src/util/trim.d.ts +0 -5
- package/dist/types/src/util/trim.d.ts.map +0 -1
- package/src/ai-service/client.test.ts +0 -247
- package/src/ai-service/defs.ts +0 -39
- package/src/ai-service/edge-client.ts +0 -60
- package/src/ai-service/index.ts +0 -13
- package/src/ai-service/message-collector.ts +0 -84
- package/src/ai-service/ollama-client.ts +0 -506
- package/src/ai-service/parser.ts +0 -369
- package/src/ai-service/service.ts +0 -31
- package/src/ai-service/stream.test.ts +0 -125
- package/src/ai-service/stream.ts +0 -118
- package/src/ai-service/testing/index.ts +0 -6
- package/src/ai-service/testing/mock.ts +0 -30
- package/src/ai-service/testing/test-stream.ts +0 -93
- package/src/ai-service/transform.ts +0 -121
- package/src/ai-service/types.ts +0 -112
- package/src/ai-service/util/index.ts +0 -5
- package/src/ai-service/util/sse.ts +0 -341
- package/src/bin/chat.ts +0 -79
- package/src/experimental/conversation/conversation.test.ts +0 -73
- package/src/experimental/conversation/conversation.ts +0 -142
- package/src/experimental/conversation/index.ts +0 -5
- package/src/experimental/conversation/types.ts +0 -126
- package/src/experimental/cypher/ast.ts +0 -75
- package/src/experimental/cypher/combinators.test.ts +0 -137
- package/src/experimental/cypher/combinators.ts +0 -148
- package/src/experimental/cypher/echo-data-source.ts +0 -105
- package/src/experimental/cypher/execution-plan.test.ts +0 -44
- package/src/experimental/cypher/execution-plan.ts +0 -208
- package/src/experimental/cypher/index.ts +0 -8
- package/src/experimental/cypher/parjs-shims.d.ts +0 -10
- package/src/experimental/cypher/parjs.ts +0 -10
- package/src/experimental/cypher/parser.ts +0 -19
- package/src/experimental/cypher/query-executor.test.ts +0 -47
- package/src/experimental/cypher/query-executor.ts +0 -145
- package/src/experimental/cypher/schema.test.ts +0 -17
- package/src/experimental/cypher/schema.ts +0 -86
- package/src/experimental/index.ts +0 -6
- package/src/experimental/ner/ner.test.ts +0 -163
- package/src/experimental/research/exa.ts +0 -38
- package/src/experimental/research/instructions.tpl +0 -64
- package/src/experimental/research/research.test.ts +0 -263
- package/src/session/tools.ts +0 -117
- package/src/testing/artifacts.test.ts +0 -182
- package/src/testing/data.ts +0 -13
- package/src/testing/defs.ts +0 -9
- package/src/testing/logger.ts +0 -129
- package/src/testing/object-extraction.test.ts +0 -71
- package/src/testing/ollama.ts +0 -15
- package/src/testing/printer.ts +0 -41
- package/src/testing/query-promts.ts +0 -75
- package/src/testing/query.test.ts +0 -88
- package/src/testing/test-data.ts +0 -379
- package/src/testing/test-schema.ts +0 -79
- package/src/util/index.ts +0 -5
- package/src/util/trim.ts +0 -15
- package/vendor/parjs-browser.mjs +0 -4
- package/vendor/parjs-default.mjs +0 -8
- /package/dist/types/{src/bin → bin}/chat.d.ts +0 -0
- /package/dist/types/src/{experimental/research → research}/research.test.d.ts +0 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Message } from '@dxos/ai';
|
|
6
|
+
import { create } from '@dxos/echo-schema';
|
|
7
|
+
import { AiService, defineFunction, type FunctionDefinition } from '@dxos/functions';
|
|
8
|
+
import { DataType } from '@dxos/schema';
|
|
9
|
+
|
|
10
|
+
import PROMPT from './instructions.tpl?raw';
|
|
11
|
+
import { AISession } from '../../session';
|
|
12
|
+
import { ExtractionInput, ExtractionOutput } from '../extraction';
|
|
13
|
+
import { insertReferences, ReferencedQuotes } from '../quotes';
|
|
14
|
+
|
|
15
|
+
export const extractionAnthropicFn: FunctionDefinition<ExtractionInput, ExtractionOutput> = defineFunction({
|
|
16
|
+
description: 'Extract entities from the transcript message and add them to the message.',
|
|
17
|
+
inputSchema: ExtractionInput,
|
|
18
|
+
outputSchema: ExtractionOutput,
|
|
19
|
+
handler: async ({ data: { message, objects }, context }) => {
|
|
20
|
+
const startTime = performance.now();
|
|
21
|
+
const ai = context.getService(AiService);
|
|
22
|
+
const session = new AISession({ operationModel: 'configured' });
|
|
23
|
+
const result = await session.runStructured(ReferencedQuotes, {
|
|
24
|
+
generationOptions: {
|
|
25
|
+
model: '@anthropic/claude-3-5-haiku-20241022',
|
|
26
|
+
},
|
|
27
|
+
client: ai.client,
|
|
28
|
+
systemPrompt: PROMPT,
|
|
29
|
+
history: [
|
|
30
|
+
create(Message, {
|
|
31
|
+
role: 'user',
|
|
32
|
+
content: [
|
|
33
|
+
{
|
|
34
|
+
type: 'text',
|
|
35
|
+
text: `<context>${JSON.stringify(objects)}</context>`,
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
type: 'text',
|
|
39
|
+
text: `<transcript>${JSON.stringify(message.blocks)}</transcript>`,
|
|
40
|
+
},
|
|
41
|
+
],
|
|
42
|
+
}),
|
|
43
|
+
],
|
|
44
|
+
artifacts: [],
|
|
45
|
+
prompt: '',
|
|
46
|
+
tools: [],
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
message: create(DataType.Message, {
|
|
51
|
+
...message,
|
|
52
|
+
blocks: message.blocks.map((block, i) =>
|
|
53
|
+
block.type !== 'transcription'
|
|
54
|
+
? block
|
|
55
|
+
: {
|
|
56
|
+
...block,
|
|
57
|
+
text: insertReferences(block.text, result),
|
|
58
|
+
},
|
|
59
|
+
),
|
|
60
|
+
}),
|
|
61
|
+
timeElapsed: performance.now() - startTime,
|
|
62
|
+
};
|
|
63
|
+
},
|
|
64
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
You are an AI assistant specialized in processing and enhancing transcripts with contextual information.
|
|
2
|
+
Your task is to analyze a given transcript and extract references to the context objects.
|
|
3
|
+
|
|
4
|
+
The context and transcript are provided in the user message in `context` and `transcript` XML tags.
|
|
5
|
+
|
|
6
|
+
Here are your instructions:
|
|
7
|
+
|
|
8
|
+
1. Read the transcript and context information provided above.
|
|
9
|
+
|
|
10
|
+
2. Process each segment of the transcript separately.
|
|
11
|
+
|
|
12
|
+
3. For each segment:
|
|
13
|
+
a. Analyze the content and identify relevant information from the context.
|
|
14
|
+
b. Extract a number of references to the context objects that are mentioned in the segment.
|
|
15
|
+
c. Output the references in the format `{ "quote": "earnings report", "id": "01JT0JP9AX0XKGZX4MV4B69VT6" }`.
|
|
16
|
+
d. If there are no references, just output an empty array.
|
|
17
|
+
|
|
18
|
+
4. Maintain the exact order and structure of the original transcript segments.
|
|
19
|
+
|
|
20
|
+
Output Format:
|
|
21
|
+
You have been provided with a tool that specifies output format
|
|
22
|
+
|
|
23
|
+
Remember:
|
|
24
|
+
- Only quote the text that is in the transcript.
|
|
25
|
+
- Analyze each segment carefully before processing it.
|
|
26
|
+
- 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.
|
|
27
|
+
|
|
28
|
+
Now, process the transcript and context provided below according to these instructions.
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { describe, test, beforeAll } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { type EchoDatabase } from '@dxos/echo-db';
|
|
8
|
+
import { EchoTestBuilder } from '@dxos/echo-db/testing';
|
|
9
|
+
import { FunctionExecutor, ServiceContainer } from '@dxos/functions';
|
|
10
|
+
import { log } from '@dxos/log';
|
|
11
|
+
import { DataType } from '@dxos/schema';
|
|
12
|
+
import { createTestData, Testing } from '@dxos/schema/testing';
|
|
13
|
+
import { range } from '@dxos/util';
|
|
14
|
+
|
|
15
|
+
import { extractionNerFn } from './extraction-ner-function';
|
|
16
|
+
import { processTranscriptMessage } from '../extraction';
|
|
17
|
+
|
|
18
|
+
describe.skip('NER EntityExtraction', () => {
|
|
19
|
+
let builder: EchoTestBuilder;
|
|
20
|
+
let db: EchoDatabase;
|
|
21
|
+
let executor: FunctionExecutor;
|
|
22
|
+
let testData: {
|
|
23
|
+
transcriptJosiah: DataType.Message[];
|
|
24
|
+
transcriptWoflram: DataType.Message[];
|
|
25
|
+
transcriptMessages: DataType.Message[];
|
|
26
|
+
documents: Testing.DocumentType[];
|
|
27
|
+
contacts: Record<string, DataType.Person>;
|
|
28
|
+
organizations: Record<string, DataType.Organization>;
|
|
29
|
+
};
|
|
30
|
+
const TYPES = [DataType.Organization, DataType.Person, Testing.Contact, Testing.DocumentType];
|
|
31
|
+
|
|
32
|
+
beforeAll(async () => {
|
|
33
|
+
// TODO(dmaretskyi): Helper to scaffold this from a config.
|
|
34
|
+
builder = await new EchoTestBuilder().open();
|
|
35
|
+
const { db: db1 } = await builder.createDatabase({ indexing: { vector: true } });
|
|
36
|
+
db = db1;
|
|
37
|
+
db.graph.schemaRegistry.addSchema(TYPES);
|
|
38
|
+
const data = createTestData();
|
|
39
|
+
testData = {
|
|
40
|
+
transcriptJosiah: data.transcriptJosiah,
|
|
41
|
+
transcriptWoflram: data.transcriptWoflram,
|
|
42
|
+
transcriptMessages: data.transcriptMessages,
|
|
43
|
+
documents: data.documents.map((document) => db.add(document)),
|
|
44
|
+
contacts: Object.fromEntries(Object.entries(data.contacts).map(([key, value]) => [key, db.add(value)])),
|
|
45
|
+
organizations: Object.fromEntries(Object.entries(data.organizations).map(([key, value]) => [key, db.add(value)])),
|
|
46
|
+
};
|
|
47
|
+
await db.flush();
|
|
48
|
+
|
|
49
|
+
executor = new FunctionExecutor(
|
|
50
|
+
new ServiceContainer().setServices({
|
|
51
|
+
database: { db },
|
|
52
|
+
}),
|
|
53
|
+
);
|
|
54
|
+
}, 30_000);
|
|
55
|
+
|
|
56
|
+
test('should process a transcript block', async () => {
|
|
57
|
+
const { transcriptMessages, documents, contacts } = testData;
|
|
58
|
+
log.info('context', { documents, contacts });
|
|
59
|
+
|
|
60
|
+
for (const message of transcriptMessages) {
|
|
61
|
+
log.info('input', message);
|
|
62
|
+
const { message: enhancedMessage } = await processTranscriptMessage({
|
|
63
|
+
input: {
|
|
64
|
+
message,
|
|
65
|
+
objects: [...documents, ...Object.values(contacts)],
|
|
66
|
+
},
|
|
67
|
+
function: extractionNerFn,
|
|
68
|
+
executor,
|
|
69
|
+
});
|
|
70
|
+
log.info('output', enhancedMessage);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('computational irreducibility', async () => {
|
|
75
|
+
const { transcriptWoflram, documents, contacts } = testData;
|
|
76
|
+
|
|
77
|
+
log.info('context', { documents, contacts });
|
|
78
|
+
const message = transcriptWoflram[0];
|
|
79
|
+
log.info('input', message);
|
|
80
|
+
|
|
81
|
+
await Promise.all(
|
|
82
|
+
range(10).map(async () => {
|
|
83
|
+
const { message: enhancedMessage, timeElapsed } = await processTranscriptMessage({
|
|
84
|
+
input: {
|
|
85
|
+
message,
|
|
86
|
+
objects: [...documents, ...Object.values(contacts)],
|
|
87
|
+
},
|
|
88
|
+
function: extractionNerFn,
|
|
89
|
+
executor,
|
|
90
|
+
});
|
|
91
|
+
log.info('output', { message: enhancedMessage.blocks[0], timeElapsed });
|
|
92
|
+
}),
|
|
93
|
+
);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
test('org and document linking', async () => {
|
|
97
|
+
const { transcriptJosiah, documents, contacts, organizations } = testData;
|
|
98
|
+
|
|
99
|
+
log.info('context', { contacts, organizations, documents });
|
|
100
|
+
|
|
101
|
+
for (const message of transcriptJosiah) {
|
|
102
|
+
log.info('input', message);
|
|
103
|
+
|
|
104
|
+
const { message: enhancedMessage } = await processTranscriptMessage({
|
|
105
|
+
input: {
|
|
106
|
+
message,
|
|
107
|
+
objects: [...documents, ...Object.values(contacts), ...Object.values(organizations)],
|
|
108
|
+
},
|
|
109
|
+
function: extractionNerFn,
|
|
110
|
+
executor,
|
|
111
|
+
});
|
|
112
|
+
log.info('output', enhancedMessage);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { asyncTimeout } from '@dxos/async';
|
|
6
|
+
import { create } from '@dxos/echo-schema';
|
|
7
|
+
import { DatabaseService, defineFunction } from '@dxos/functions';
|
|
8
|
+
import { invariant } from '@dxos/invariant';
|
|
9
|
+
import { log } from '@dxos/log';
|
|
10
|
+
import { DataType } from '@dxos/schema';
|
|
11
|
+
|
|
12
|
+
import { extractFullEntities } from './named-entity-recognition';
|
|
13
|
+
import { ExtractionInput, ExtractionOutput } from '../extraction';
|
|
14
|
+
import { findQuotes, insertReferences } from '../quotes';
|
|
15
|
+
|
|
16
|
+
// TODO(mykola): Make it use vector index to query objects and not pass objects as input.
|
|
17
|
+
export const extractionNerFn = defineFunction({
|
|
18
|
+
description: 'Extracts named entities from a text',
|
|
19
|
+
inputSchema: ExtractionInput,
|
|
20
|
+
outputSchema: ExtractionOutput,
|
|
21
|
+
handler: async ({ data: { message, options }, context }) => {
|
|
22
|
+
log.info('input', { message, options });
|
|
23
|
+
const startTime = performance.now();
|
|
24
|
+
const { db } = context.getService(DatabaseService);
|
|
25
|
+
|
|
26
|
+
const entitiesPromise = Promise.all(
|
|
27
|
+
message.blocks.map(async (block) => {
|
|
28
|
+
invariant(block.type === 'transcription' || block.type === 'text', 'Block must have text');
|
|
29
|
+
return extractFullEntities(block.text);
|
|
30
|
+
}),
|
|
31
|
+
).then((entities) => entities.flat());
|
|
32
|
+
const entities = options?.timeout ? await asyncTimeout(entitiesPromise, options.timeout) : await entitiesPromise;
|
|
33
|
+
log.info('entities', { entities });
|
|
34
|
+
const quoteReferences = await findQuotes(
|
|
35
|
+
entities.map((entity) => entity.word),
|
|
36
|
+
db,
|
|
37
|
+
);
|
|
38
|
+
const blocksWithReferences = message.blocks.map((block, i) => {
|
|
39
|
+
invariant(block.type === 'transcription' || block.type === 'text', 'Block must have text');
|
|
40
|
+
return { ...block, text: insertReferences(block.text, quoteReferences) };
|
|
41
|
+
});
|
|
42
|
+
const messageWithReferences = create(DataType.Message, {
|
|
43
|
+
...message,
|
|
44
|
+
blocks: blocksWithReferences,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
log.info('output', { messageWithReferences });
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
message: messageWithReferences,
|
|
51
|
+
timeElapsed: performance.now() - startTime,
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
});
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type TokenClassificationOutput, type TokenClassificationPipelineType } from '@xenova/transformers';
|
|
6
|
+
import { beforeAll, describe, expect, test } from 'vitest';
|
|
7
|
+
|
|
8
|
+
import { log } from '@dxos/log';
|
|
9
|
+
import { createTestData } from '@dxos/schema/testing';
|
|
10
|
+
|
|
11
|
+
import { combineNerTokens, createTokenGroups, extractFullEntities, getNer } from './named-entity-recognition';
|
|
12
|
+
|
|
13
|
+
describe('NamedEntityRecognition', () => {
|
|
14
|
+
let ner: TokenClassificationPipelineType;
|
|
15
|
+
|
|
16
|
+
beforeAll(async () => {
|
|
17
|
+
// Initialize the NER pipeline
|
|
18
|
+
ner = await getNer();
|
|
19
|
+
}, 30_000);
|
|
20
|
+
|
|
21
|
+
test('should identify named entities in text', async () => {
|
|
22
|
+
const text = 'Elon Musk is the CEO of SpaceX and Tesla.';
|
|
23
|
+
const result = (await ner(text)) as TokenClassificationOutput;
|
|
24
|
+
|
|
25
|
+
// Verify the structure of the result
|
|
26
|
+
expect(Array.isArray(result)).toBe(true);
|
|
27
|
+
expect(result.length).toBeGreaterThan(0);
|
|
28
|
+
|
|
29
|
+
log.info('ner', { result });
|
|
30
|
+
|
|
31
|
+
const combinedTokens = createTokenGroups(result).map(combineNerTokens);
|
|
32
|
+
log.info('combined', { text, result: combinedTokens });
|
|
33
|
+
|
|
34
|
+
// Check for specific entities
|
|
35
|
+
const entities = combinedTokens.map((item) => ({
|
|
36
|
+
text: item.word,
|
|
37
|
+
entity: item.entity,
|
|
38
|
+
score: item.score,
|
|
39
|
+
}));
|
|
40
|
+
|
|
41
|
+
log.info('entities', { entities });
|
|
42
|
+
|
|
43
|
+
// Verify Elon Musk is recognized as a person
|
|
44
|
+
const elonMusk = entities.find((e) => e.text.includes('Elon') || e.text.includes('Musk'));
|
|
45
|
+
expect(elonMusk).toBeDefined();
|
|
46
|
+
expect(elonMusk?.entity).toMatch(/PER/);
|
|
47
|
+
|
|
48
|
+
// Verify SpaceX and Tesla are recognized as organizations
|
|
49
|
+
const companies = entities.filter((e: any) => e.text === 'SpaceX' || e.text === 'Tesla');
|
|
50
|
+
expect(companies.length).toBe(2);
|
|
51
|
+
companies.forEach((company: any) => {
|
|
52
|
+
expect(company.entity).toMatch(/ORG/);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test('own data', async () => {
|
|
57
|
+
const { transcriptWoflram, transcriptJosiah } = await createTestData();
|
|
58
|
+
|
|
59
|
+
const blocks = [...transcriptWoflram, ...transcriptJosiah].flatMap((message) =>
|
|
60
|
+
message.blocks.flatMap((block) => (block.type === 'transcription' ? [block.text] : [])),
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
for (const block of blocks) {
|
|
64
|
+
const entities = await extractFullEntities(block);
|
|
65
|
+
log.info('ner', { text: block, result: entities });
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test('more own data', async () => {
|
|
70
|
+
const { transcriptMessages } = await createTestData();
|
|
71
|
+
|
|
72
|
+
const blocks = transcriptMessages.flatMap((message) =>
|
|
73
|
+
message.blocks.flatMap((block) => (block.type === 'transcription' ? [block.text] : [])),
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
for (const block of blocks) {
|
|
77
|
+
const entities = await extractFullEntities(block);
|
|
78
|
+
log.info('ner', { text: block, result: entities });
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
test('should handle empty text', async () => {
|
|
83
|
+
const result = await ner('');
|
|
84
|
+
expect(Array.isArray(result)).toBe(true);
|
|
85
|
+
expect(result.length).toBe(0);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test('should handle text with no named entities', async () => {
|
|
89
|
+
const text = 'The weather is nice today.';
|
|
90
|
+
const result = await ner(text);
|
|
91
|
+
expect(Array.isArray(result)).toBe(true);
|
|
92
|
+
expect(result.length).toBe(0);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
type TokenClassificationOutput,
|
|
7
|
+
type TokenClassificationSingle,
|
|
8
|
+
pipeline,
|
|
9
|
+
type TokenClassificationPipelineType,
|
|
10
|
+
} from '@xenova/transformers';
|
|
11
|
+
|
|
12
|
+
import { log } from '@dxos/log';
|
|
13
|
+
|
|
14
|
+
let _ner: Promise<TokenClassificationPipelineType>;
|
|
15
|
+
/**
|
|
16
|
+
* Named Entity Recognition pipeline.
|
|
17
|
+
* Initializes the pipeline on first call.
|
|
18
|
+
* @returns The singleton promise that resolves to a token classification pipeline.
|
|
19
|
+
*/
|
|
20
|
+
export const getNer = () => {
|
|
21
|
+
if (!_ner) {
|
|
22
|
+
_ner = pipeline('ner', 'Xenova/bert-base-NER').then((ner) => {
|
|
23
|
+
log.info('NER model is ready');
|
|
24
|
+
return ner;
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
return _ner;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Extracts entities from text using the named entity recognition pipeline and does simple grouping of tokens.
|
|
32
|
+
*/
|
|
33
|
+
export const extractFullEntities = async (text: string): Promise<TokenClassificationSingle[]> => {
|
|
34
|
+
const ner = await getNer();
|
|
35
|
+
const entities = (await ner(text)) as TokenClassificationOutput;
|
|
36
|
+
const tokenGroups = createTokenGroups(entities);
|
|
37
|
+
const combinedEntities = tokenGroups.map(combineNerTokens);
|
|
38
|
+
return combinedEntities;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const createTokenGroups = (tokens: TokenClassificationSingle[]): TokenClassificationSingle[][] => {
|
|
42
|
+
const tokenGroups: TokenClassificationSingle[][] = [];
|
|
43
|
+
|
|
44
|
+
const canBeCombined = (prevToken: TokenClassificationSingle, currentToken: TokenClassificationSingle) => {
|
|
45
|
+
const [currentState, currentEntity] = currentToken.entity.split('-'); // B, I, O
|
|
46
|
+
const [prevState, prevEntity] = prevToken.entity.split('-'); // B, I, O
|
|
47
|
+
if (prevEntity !== currentEntity) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
if (prevState === 'O') {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
if (currentState === 'I') {
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
if ((prevState === 'B' || prevState === 'I') && currentState === 'B') {
|
|
57
|
+
return currentToken.word.startsWith('#');
|
|
58
|
+
}
|
|
59
|
+
return false;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
for (const token of tokens) {
|
|
63
|
+
if (tokenGroups.length === 0) {
|
|
64
|
+
tokenGroups.push([token]);
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const lastTokenGroup = tokenGroups.at(-1)!;
|
|
68
|
+
if (canBeCombined(lastTokenGroup.at(-1)!, token)) {
|
|
69
|
+
lastTokenGroup.push(token);
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
tokenGroups.push([token]);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return tokenGroups;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export const combineNerTokens = (group: TokenClassificationSingle[]): TokenClassificationSingle => {
|
|
79
|
+
const combinedToken: TokenClassificationSingle = {
|
|
80
|
+
entity: group.at(0)!.entity.split('-')[1],
|
|
81
|
+
score: group.reduce((acc, token) => acc + token.score, 0) / group.length,
|
|
82
|
+
index: group.at(0)!.index,
|
|
83
|
+
word: group.at(0)!.word,
|
|
84
|
+
start: group.at(0)!.start,
|
|
85
|
+
end: group.at(-1)!.end,
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
for (const token of group.slice(1)) {
|
|
89
|
+
if (token.word.startsWith('#')) {
|
|
90
|
+
combinedToken.word += token.word.replace(/^#+/, '');
|
|
91
|
+
} else {
|
|
92
|
+
combinedToken.word += ' ' + token.word;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return combinedToken;
|
|
97
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Schema } from 'effect';
|
|
6
|
+
|
|
7
|
+
import { asyncTimeout } from '@dxos/async';
|
|
8
|
+
import { Expando } from '@dxos/echo-schema';
|
|
9
|
+
import { type FunctionDefinition, type FunctionExecutor } from '@dxos/functions';
|
|
10
|
+
import { log } from '@dxos/log';
|
|
11
|
+
import { DataType } from '@dxos/schema';
|
|
12
|
+
|
|
13
|
+
export const ExtractionInput = Schema.Struct({
|
|
14
|
+
message: DataType.Message,
|
|
15
|
+
objects: Schema.optional(Schema.Array(Expando)),
|
|
16
|
+
options: Schema.optional(
|
|
17
|
+
Schema.Struct({
|
|
18
|
+
timeout: Schema.optional(Schema.Number),
|
|
19
|
+
fallbackToRaw: Schema.optional(Schema.Boolean),
|
|
20
|
+
}),
|
|
21
|
+
),
|
|
22
|
+
});
|
|
23
|
+
export interface ExtractionInput extends Schema.Schema.Type<typeof ExtractionInput> {}
|
|
24
|
+
|
|
25
|
+
export const ExtractionOutput = Schema.Struct({
|
|
26
|
+
message: DataType.Message,
|
|
27
|
+
timeElapsed: Schema.Number,
|
|
28
|
+
});
|
|
29
|
+
export interface ExtractionOutput extends Schema.Schema.Type<typeof ExtractionOutput> {}
|
|
30
|
+
|
|
31
|
+
export type ProcessTranscriptMessageParams = {
|
|
32
|
+
input: ExtractionInput;
|
|
33
|
+
function: FunctionDefinition<ExtractionInput, ExtractionOutput>;
|
|
34
|
+
executor: FunctionExecutor;
|
|
35
|
+
|
|
36
|
+
options?: {
|
|
37
|
+
/**
|
|
38
|
+
* Timeout for the entity extraction.
|
|
39
|
+
*/
|
|
40
|
+
timeout?: number;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Fallback to raw text if the entity extraction fails.
|
|
44
|
+
* Otherwise the function will throw an error.
|
|
45
|
+
* @default false
|
|
46
|
+
*/
|
|
47
|
+
fallbackToRaw?: boolean;
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export type ExtractionFunction = FunctionDefinition<ExtractionInput, ExtractionOutput>;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Extract entities from the transcript message and add them to the message.
|
|
55
|
+
*/
|
|
56
|
+
// TODO(dmaretskyi): Move context to a vector search index.
|
|
57
|
+
export const processTranscriptMessage = async (params: ProcessTranscriptMessageParams): Promise<ExtractionOutput> => {
|
|
58
|
+
try {
|
|
59
|
+
if (params.options?.timeout && params.options.timeout > 0) {
|
|
60
|
+
return await asyncTimeout(params.executor.invoke(params.function, params.input), params.options.timeout);
|
|
61
|
+
} else {
|
|
62
|
+
return await params.executor.invoke(params.function, params.input);
|
|
63
|
+
}
|
|
64
|
+
} catch (error) {
|
|
65
|
+
if (params.options?.fallbackToRaw) {
|
|
66
|
+
log.warn('failed to process message, falling back to raw text', { error });
|
|
67
|
+
return {
|
|
68
|
+
message: params.input.message,
|
|
69
|
+
timeElapsed: 0,
|
|
70
|
+
};
|
|
71
|
+
} else {
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { describe, test, expect } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import { insertReferences } from './quotes';
|
|
8
|
+
|
|
9
|
+
describe('insertReferences', () => {
|
|
10
|
+
test('should replace quotes with DXN references', () => {
|
|
11
|
+
const quotes = {
|
|
12
|
+
references: [{ quote: 'computational irreducibility', id: '01JTG9JW11XGWJZ32AW8ET93D1' }],
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
expect(insertReferences('This is a computational irreducibility test.', quotes)).toBe(
|
|
16
|
+
'This is a [computational irreducibility][dxn:echo:@:01JTG9JW11XGWJZ32AW8ET93D1] test.',
|
|
17
|
+
);
|
|
18
|
+
expect(
|
|
19
|
+
insertReferences(
|
|
20
|
+
"And what I'd like to talk today about is Steven Wolfram's concept of a computational irreducibility.",
|
|
21
|
+
quotes,
|
|
22
|
+
),
|
|
23
|
+
).toBe(
|
|
24
|
+
"And what I'd like to talk today about is Steven Wolfram's concept of a [computational irreducibility][dxn:echo:@:01JTG9JW11XGWJZ32AW8ET93D1].",
|
|
25
|
+
);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Schema } from 'effect';
|
|
6
|
+
|
|
7
|
+
import { type EchoDatabase } from '@dxos/echo-db';
|
|
8
|
+
import { Filter, ObjectId, Query } from '@dxos/echo-schema';
|
|
9
|
+
import { DXN } from '@dxos/keys';
|
|
10
|
+
import { log } from '@dxos/log';
|
|
11
|
+
|
|
12
|
+
export const ReferencedQuotes = Schema.Struct({
|
|
13
|
+
references: Schema.Array(
|
|
14
|
+
Schema.Struct({
|
|
15
|
+
id: Schema.String,
|
|
16
|
+
quote: Schema.String, // TODO(burdon): Quote?
|
|
17
|
+
}),
|
|
18
|
+
)
|
|
19
|
+
.pipe(Schema.mutable)
|
|
20
|
+
.annotations({
|
|
21
|
+
// TODO(burdon): Does this description make sense?
|
|
22
|
+
description: `
|
|
23
|
+
The references to the context objects that are mentioned in the transcript.
|
|
24
|
+
Quote should match the original transcript text exactly, while id is the id of the context object.
|
|
25
|
+
`,
|
|
26
|
+
}),
|
|
27
|
+
});
|
|
28
|
+
export type ReferencedQuotes = Schema.Schema.Type<typeof ReferencedQuotes>;
|
|
29
|
+
|
|
30
|
+
export const findQuotes = async (quotes: string[], db: EchoDatabase): Promise<ReferencedQuotes> => {
|
|
31
|
+
const references = await Promise.all(
|
|
32
|
+
quotes.map(async (quote) => {
|
|
33
|
+
const { objects } = await db.query(Query.select(Filter.text(quote, { type: 'vector' }))).run();
|
|
34
|
+
log.info('found quotes in vector index', { objects, quote });
|
|
35
|
+
return objects.length > 0 ? [{ id: objects[0].id.toString(), quote }] : [];
|
|
36
|
+
}),
|
|
37
|
+
);
|
|
38
|
+
return { references: references.flat() };
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Finds and replaces all quotes with DXNs references.
|
|
43
|
+
*/
|
|
44
|
+
// TODO(dmaretskyi): Lookup and verifiy ids from provided context.
|
|
45
|
+
export const insertReferences = (text: string, quotes: ReferencedQuotes) => {
|
|
46
|
+
for (const quote of quotes.references) {
|
|
47
|
+
if (!ObjectId.isValid(quote.id)) {
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Use a case-insensitive regular expression to replace the quote.
|
|
52
|
+
const regex = new RegExp(quote.quote.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi');
|
|
53
|
+
text = text.replace(regex, `[${quote.quote}][${DXN.fromLocalObjectId(quote.id).toString()}]`);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return text;
|
|
57
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
// Copyright 2024 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
export * from './
|
|
5
|
+
export * from './extraction';
|
|
6
|
+
export * from './research';
|
|
6
7
|
export * from './session';
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export * from './experimental';
|
|
8
|
+
export * from './types';
|
|
9
|
+
export * from './blueprint';
|