@dxos/plugin-transcription 0.8.2-staging.7ac8446 → 0.8.2
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/TranscriptContainer-PFKLKRPQ.mjs +11 -0
- package/dist/lib/browser/{chunk-YCZISX63.mjs → chunk-4KCUYW2F.mjs} +139 -95
- package/dist/lib/browser/chunk-4KCUYW2F.mjs.map +7 -0
- package/dist/lib/browser/{chunk-6ZR5MDK3.mjs → chunk-GOF7MJNV.mjs} +3 -3
- package/dist/lib/browser/{chunk-6ZR5MDK3.mjs.map → chunk-GOF7MJNV.mjs.map} +1 -1
- package/dist/lib/browser/{chunk-K7FOYAXO.mjs → chunk-IGEKO7ZU.mjs} +2 -2
- package/dist/lib/browser/chunk-NODIXNQP.mjs +54 -0
- package/dist/lib/browser/chunk-NODIXNQP.mjs.map +7 -0
- package/dist/lib/browser/chunk-TJPJBE45.mjs +764 -0
- package/dist/lib/browser/chunk-TJPJBE45.mjs.map +7 -0
- package/dist/lib/browser/{chunk-YEPT334A.mjs → chunk-W2KNYJD6.mjs} +4 -4
- package/dist/lib/browser/chunk-W2KNYJD6.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +30 -216
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/{intent-resolver-4R66ZCKT.mjs → intent-resolver-I2LJSQDR.mjs} +7 -11
- package/dist/lib/browser/intent-resolver-I2LJSQDR.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/{react-surface-FQUHU5G7.mjs → react-surface-ID2MLJHZ.mjs} +11 -9
- package/dist/lib/browser/react-surface-ID2MLJHZ.mjs.map +7 -0
- package/dist/lib/browser/transcriber-NA7CIFIT.mjs +56 -0
- package/dist/lib/browser/transcriber-NA7CIFIT.mjs.map +7 -0
- package/dist/lib/browser/types/index.mjs +4 -8
- package/dist/lib/node/{TranscriptContainer-QEYTTT2V.cjs → TranscriptContainer-LHMC7553.cjs} +9 -8
- package/dist/lib/node/TranscriptContainer-LHMC7553.cjs.map +7 -0
- package/dist/lib/node/chunk-3GS5UNO5.cjs +778 -0
- package/dist/lib/node/chunk-3GS5UNO5.cjs.map +7 -0
- package/dist/lib/node/{chunk-LP4FG423.cjs → chunk-5J3FBCON.cjs} +5 -5
- package/dist/lib/node/{chunk-NVZWGFJL.cjs → chunk-AFUZI63A.cjs} +6 -6
- package/dist/lib/node/{chunk-NVZWGFJL.cjs.map → chunk-AFUZI63A.cjs.map} +1 -1
- package/dist/lib/node/chunk-EIMUQYCZ.cjs +74 -0
- package/dist/lib/node/chunk-EIMUQYCZ.cjs.map +7 -0
- package/dist/lib/node/{chunk-CZMLKKWT.cjs → chunk-FKDJNJ57.cjs} +8 -8
- package/dist/lib/node/chunk-FKDJNJ57.cjs.map +7 -0
- package/dist/lib/node/{chunk-FCCJALOT.cjs → chunk-ZFTXJZXH.cjs} +138 -95
- package/dist/lib/node/chunk-ZFTXJZXH.cjs.map +7 -0
- package/dist/lib/node/index.cjs +59 -251
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/{intent-resolver-WMMCOUEH.cjs → intent-resolver-LLMGF5I5.cjs} +10 -11
- package/dist/lib/node/intent-resolver-LLMGF5I5.cjs.map +7 -0
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node/{react-surface-WI2MGWCC.cjs → react-surface-SEQLEL4K.cjs} +13 -11
- package/dist/lib/node/react-surface-SEQLEL4K.cjs.map +7 -0
- package/dist/lib/node/transcriber-RKKN7LDU.cjs +68 -0
- package/dist/lib/node/transcriber-RKKN7LDU.cjs.map +7 -0
- package/dist/lib/node/types/index.cjs +7 -11
- package/dist/lib/node/types/index.cjs.map +2 -2
- package/dist/lib/node-esm/{TranscriptContainer-ZXUCOWGY.mjs → TranscriptContainer-DLNRFVTA.mjs} +4 -3
- package/dist/lib/node-esm/chunk-BRPFVRAD.mjs +55 -0
- package/dist/lib/node-esm/chunk-BRPFVRAD.mjs.map +7 -0
- package/dist/lib/node-esm/{chunk-YQYBLFNI.mjs → chunk-EFIWBQDF.mjs} +139 -95
- package/dist/lib/node-esm/chunk-EFIWBQDF.mjs.map +7 -0
- package/dist/lib/node-esm/{chunk-J4D2RBBH.mjs → chunk-KUWWHMOA.mjs} +2 -2
- package/dist/lib/node-esm/{chunk-PTZHDZ36.mjs → chunk-MFJPEIVB.mjs} +4 -4
- package/dist/lib/node-esm/chunk-MFJPEIVB.mjs.map +7 -0
- package/dist/lib/node-esm/chunk-PR7EYODX.mjs +765 -0
- package/dist/lib/node-esm/chunk-PR7EYODX.mjs.map +7 -0
- package/dist/lib/node-esm/{chunk-X4DDJIVL.mjs → chunk-TXMDLLBR.mjs} +3 -3
- package/dist/lib/node-esm/{chunk-X4DDJIVL.mjs.map → chunk-TXMDLLBR.mjs.map} +1 -1
- package/dist/lib/node-esm/index.mjs +30 -216
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/{intent-resolver-QN2LO5TX.mjs → intent-resolver-WOZKOEBW.mjs} +7 -11
- package/dist/lib/node-esm/intent-resolver-WOZKOEBW.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/lib/node-esm/{react-surface-PCH6NTVB.mjs → react-surface-7MXUUTAL.mjs} +11 -9
- package/dist/lib/node-esm/react-surface-7MXUUTAL.mjs.map +7 -0
- package/dist/lib/node-esm/transcriber-5WSI6DYK.mjs +57 -0
- package/dist/lib/node-esm/transcriber-5WSI6DYK.mjs.map +7 -0
- package/dist/lib/node-esm/types/index.mjs +4 -8
- package/dist/types/src/TranscriptionPlugin.d.ts.map +1 -1
- package/dist/types/src/capabilities/capabilities.d.ts +8 -10
- package/dist/types/src/capabilities/capabilities.d.ts.map +1 -1
- package/dist/types/src/capabilities/index.d.ts +2 -183
- package/dist/types/src/capabilities/index.d.ts.map +1 -1
- package/dist/types/src/capabilities/intent-resolver.d.ts +2 -2
- package/dist/types/src/capabilities/intent-resolver.d.ts.map +1 -1
- package/dist/types/src/capabilities/react-surface.d.ts.map +1 -1
- package/dist/types/src/capabilities/transcriber.d.ts +2 -1
- package/dist/types/src/capabilities/transcriber.d.ts.map +1 -1
- package/dist/types/src/components/Transcript/Transcript.d.ts +14 -4
- package/dist/types/src/components/Transcript/Transcript.d.ts.map +1 -1
- package/dist/types/src/components/Transcript/Transcript.stories.d.ts +19 -4
- package/dist/types/src/components/Transcript/Transcript.stories.d.ts.map +1 -1
- package/dist/types/src/components/Transcript/deprecated/OldTranscript.d.ts +27 -0
- package/dist/types/src/components/Transcript/deprecated/OldTranscript.d.ts.map +1 -0
- package/dist/types/src/components/Transcript/transcript-extension.d.ts +19 -0
- package/dist/types/src/components/Transcript/transcript-extension.d.ts.map +1 -0
- package/dist/types/src/components/TranscriptContainer.d.ts.map +1 -1
- package/dist/types/src/components/stories/FileTranscription.stories.d.ts +17 -0
- package/dist/types/src/components/stories/FileTranscription.stories.d.ts.map +1 -0
- package/dist/types/src/components/stories/KeyWordRecognition.stories.d.ts +10 -0
- package/dist/types/src/components/stories/KeyWordRecognition.stories.d.ts.map +1 -0
- package/dist/types/src/components/stories/MicrophoneTranscription.stories.d.ts +19 -0
- package/dist/types/src/components/stories/MicrophoneTranscription.stories.d.ts.map +1 -0
- package/dist/types/src/components/stories/TranscriptionStory.d.ts +11 -0
- package/dist/types/src/components/stories/TranscriptionStory.d.ts.map +1 -0
- package/dist/types/src/components/stories/useIsSpeaking.d.ts +3 -0
- package/dist/types/src/components/stories/useIsSpeaking.d.ts.map +1 -0
- package/dist/types/src/hooks/index.d.ts +1 -0
- package/dist/types/src/hooks/index.d.ts.map +1 -1
- package/dist/types/src/hooks/useAudioFile.d.ts +1 -1
- package/dist/types/src/hooks/useAudioFile.d.ts.map +1 -1
- package/dist/types/src/hooks/useAudioTrack.d.ts +1 -1
- package/dist/types/src/hooks/useAudioTrack.d.ts.map +1 -1
- package/dist/types/src/hooks/useQueueModelAdapter.d.ts +7 -0
- package/dist/types/src/hooks/useQueueModelAdapter.d.ts.map +1 -0
- package/dist/types/src/hooks/useTranscriber.d.ts +1 -1
- package/dist/types/src/hooks/useTranscriber.d.ts.map +1 -1
- package/dist/types/src/hooks/useVoiceInput.d.ts.map +1 -1
- package/dist/types/src/model/index.d.ts +2 -0
- package/dist/types/src/model/index.d.ts.map +1 -0
- package/dist/types/src/model/model.d.ts +68 -0
- package/dist/types/src/model/model.d.ts.map +1 -0
- package/dist/types/src/model/model.test.d.ts +2 -0
- package/dist/types/src/model/model.test.d.ts.map +1 -0
- package/dist/types/src/segments-normalization/index.d.ts +4 -0
- package/dist/types/src/segments-normalization/index.d.ts.map +1 -0
- package/dist/types/src/segments-normalization/message-normalizer.d.ts +26 -0
- package/dist/types/src/segments-normalization/message-normalizer.d.ts.map +1 -0
- package/dist/types/src/segments-normalization/normalization.d.ts +520 -0
- package/dist/types/src/segments-normalization/normalization.d.ts.map +1 -0
- package/dist/types/src/segments-normalization/normalization.test.d.ts +2 -0
- package/dist/types/src/segments-normalization/normalization.test.d.ts.map +1 -0
- package/dist/types/src/segments-normalization/utils.d.ts +3 -0
- package/dist/types/src/segments-normalization/utils.d.ts.map +1 -0
- package/dist/types/src/testing/index.d.ts +2 -0
- package/dist/types/src/testing/index.d.ts.map +1 -0
- package/dist/types/src/testing/testing.d.ts +40 -0
- package/dist/types/src/testing/testing.d.ts.map +1 -0
- package/dist/types/src/transcriber/media-stream-recorder.d.ts +8 -5
- package/dist/types/src/transcriber/media-stream-recorder.d.ts.map +1 -1
- package/dist/types/src/transcriber/transcriber.browser.test.d.ts +2 -0
- package/dist/types/src/transcriber/transcriber.browser.test.d.ts.map +1 -0
- package/dist/types/src/transcriber/transcriber.d.ts +6 -2
- package/dist/types/src/transcriber/transcriber.d.ts.map +1 -1
- package/dist/types/src/transcriber/transcription-manager.d.ts +19 -15
- package/dist/types/src/transcriber/transcription-manager.d.ts.map +1 -1
- package/dist/types/src/translations.d.ts +3 -20
- package/dist/types/src/translations.d.ts.map +1 -1
- package/dist/types/src/types/schema.d.ts +23 -40
- package/dist/types/src/types/schema.d.ts.map +1 -1
- package/dist/types/src/types/types.d.ts +15 -8
- package/dist/types/src/types/types.d.ts.map +1 -1
- package/dist/types/src/util/array.d.ts.map +1 -1
- package/dist/types/src/util/get-time-str.d.ts.map +1 -1
- package/dist/types/src/util/index.d.ts +0 -1
- package/dist/types/src/util/index.d.ts.map +1 -1
- package/dist/types/src/util/monitor-audio-level.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +62 -41
- package/src/TranscriptionPlugin.tsx +13 -18
- package/src/capabilities/capabilities.ts +15 -8
- package/src/capabilities/index.ts +0 -2
- package/src/capabilities/intent-resolver.ts +7 -8
- package/src/capabilities/react-surface.tsx +5 -4
- package/src/capabilities/transcriber.ts +34 -6
- package/src/components/Transcript/Transcript.stories.tsx +201 -43
- package/src/components/Transcript/Transcript.tsx +83 -208
- package/src/components/Transcript/deprecated/OldTranscript.tsx +260 -0
- package/src/components/Transcript/transcript-extension.ts +236 -0
- package/src/components/TranscriptContainer.tsx +11 -6
- package/src/components/stories/FileTranscription.stories.tsx +240 -0
- package/src/components/stories/KeyWordRecognition.stories.tsx +128 -0
- package/src/components/stories/MicrophoneTranscription.stories.tsx +274 -0
- package/src/components/stories/TranscriptionStory.tsx +41 -0
- package/src/components/stories/useIsSpeaking.tsx +40 -0
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useAudioFile.ts +39 -9
- package/src/hooks/useAudioTrack.ts +2 -2
- package/src/hooks/useQueueModelAdapter.ts +65 -0
- package/src/hooks/useTranscriber.ts +4 -2
- package/src/meta.ts +2 -2
- package/src/model/index.ts +5 -0
- package/src/model/model.test.ts +169 -0
- package/src/model/model.ts +229 -0
- package/src/segments-normalization/index.ts +7 -0
- package/src/segments-normalization/message-normalizer.ts +108 -0
- package/src/segments-normalization/normalization.test.ts +142 -0
- package/src/segments-normalization/normalization.ts +103 -0
- package/src/segments-normalization/utils.ts +9 -0
- package/src/testing/index.ts +5 -0
- package/src/testing/testing.ts +216 -0
- package/src/transcriber/media-stream-recorder.ts +14 -7
- package/src/transcriber/{transcriber.test.ts → transcriber.browser.test.ts} +7 -7
- package/src/transcriber/transcriber.ts +33 -20
- package/src/transcriber/transcription-manager.ts +87 -71
- package/src/translations.ts +2 -3
- package/src/types/schema.ts +11 -33
- package/src/types/types.ts +8 -12
- package/src/util/index.ts +0 -1
- package/dist/lib/browser/TranscriptContainer-JQ3HFHLQ.mjs +0 -10
- package/dist/lib/browser/app-graph-builder-VDQOVX7P.mjs +0 -191
- package/dist/lib/browser/app-graph-builder-VDQOVX7P.mjs.map +0 -7
- package/dist/lib/browser/chunk-G4B6CPC2.mjs +0 -253
- package/dist/lib/browser/chunk-G4B6CPC2.mjs.map +0 -7
- package/dist/lib/browser/chunk-GU2BNYKZ.mjs +0 -35
- package/dist/lib/browser/chunk-GU2BNYKZ.mjs.map +0 -7
- package/dist/lib/browser/chunk-OGNXNGJB.mjs +0 -73
- package/dist/lib/browser/chunk-OGNXNGJB.mjs.map +0 -7
- package/dist/lib/browser/chunk-YCZISX63.mjs.map +0 -7
- package/dist/lib/browser/chunk-YEPT334A.mjs.map +0 -7
- package/dist/lib/browser/intent-resolver-4R66ZCKT.mjs.map +0 -7
- package/dist/lib/browser/meeting-transcription-state-DLFHQK72.mjs +0 -18
- package/dist/lib/browser/meeting-transcription-state-DLFHQK72.mjs.map +0 -7
- package/dist/lib/browser/react-surface-FQUHU5G7.mjs.map +0 -7
- package/dist/lib/browser/transcriber-LGMIWDYI.mjs +0 -36
- package/dist/lib/browser/transcriber-LGMIWDYI.mjs.map +0 -7
- package/dist/lib/node/TranscriptContainer-QEYTTT2V.cjs.map +0 -7
- package/dist/lib/node/app-graph-builder-IFSIVNUR.cjs +0 -200
- package/dist/lib/node/app-graph-builder-IFSIVNUR.cjs.map +0 -7
- package/dist/lib/node/chunk-3KHUSQNW.cjs +0 -55
- package/dist/lib/node/chunk-3KHUSQNW.cjs.map +0 -7
- package/dist/lib/node/chunk-CZMLKKWT.cjs.map +0 -7
- package/dist/lib/node/chunk-FCCJALOT.cjs.map +0 -7
- package/dist/lib/node/chunk-P4VVD6ZO.cjs +0 -95
- package/dist/lib/node/chunk-P4VVD6ZO.cjs.map +0 -7
- package/dist/lib/node/chunk-UOFHT4GZ.cjs +0 -280
- package/dist/lib/node/chunk-UOFHT4GZ.cjs.map +0 -7
- package/dist/lib/node/intent-resolver-WMMCOUEH.cjs.map +0 -7
- package/dist/lib/node/meeting-transcription-state-URIYILEQ.cjs +0 -34
- package/dist/lib/node/meeting-transcription-state-URIYILEQ.cjs.map +0 -7
- package/dist/lib/node/react-surface-WI2MGWCC.cjs.map +0 -7
- package/dist/lib/node/transcriber-QG5WPKX4.cjs +0 -49
- package/dist/lib/node/transcriber-QG5WPKX4.cjs.map +0 -7
- package/dist/lib/node-esm/app-graph-builder-UL6XLIZY.mjs +0 -192
- package/dist/lib/node-esm/app-graph-builder-UL6XLIZY.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-6X7X2UZF.mjs +0 -254
- package/dist/lib/node-esm/chunk-6X7X2UZF.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-GIO6J6FE.mjs +0 -74
- package/dist/lib/node-esm/chunk-GIO6J6FE.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-HKATMD34.mjs +0 -37
- package/dist/lib/node-esm/chunk-HKATMD34.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-PTZHDZ36.mjs.map +0 -7
- package/dist/lib/node-esm/chunk-YQYBLFNI.mjs.map +0 -7
- package/dist/lib/node-esm/intent-resolver-QN2LO5TX.mjs.map +0 -7
- package/dist/lib/node-esm/meeting-transcription-state-ZYVRAW6Y.mjs +0 -19
- package/dist/lib/node-esm/meeting-transcription-state-ZYVRAW6Y.mjs.map +0 -7
- package/dist/lib/node-esm/react-surface-PCH6NTVB.mjs.map +0 -7
- package/dist/lib/node-esm/transcriber-TMPYZ5L4.mjs +0 -37
- package/dist/lib/node-esm/transcriber-TMPYZ5L4.mjs.map +0 -7
- package/dist/types/src/capabilities/app-graph-builder.d.ts +0 -181
- package/dist/types/src/capabilities/app-graph-builder.d.ts.map +0 -1
- package/dist/types/src/capabilities/meeting-transcription-state.d.ts +0 -6
- package/dist/types/src/capabilities/meeting-transcription-state.d.ts.map +0 -1
- package/dist/types/src/components/transcription.stories.d.ts +0 -15
- package/dist/types/src/components/transcription.stories.d.ts.map +0 -1
- package/dist/types/src/transcriber/transcriber.test.d.ts +0 -2
- package/dist/types/src/transcriber/transcriber.test.d.ts.map +0 -1
- package/dist/types/src/util/random-queue-dxn.d.ts +0 -3
- package/dist/types/src/util/random-queue-dxn.d.ts.map +0 -1
- package/src/capabilities/app-graph-builder.ts +0 -152
- package/src/capabilities/meeting-transcription-state.ts +0 -16
- package/src/components/transcription.stories.tsx +0 -196
- package/src/util/random-queue-dxn.ts +0 -9
- /package/dist/lib/browser/{TranscriptContainer-JQ3HFHLQ.mjs.map → TranscriptContainer-PFKLKRPQ.mjs.map} +0 -0
- /package/dist/lib/browser/{chunk-K7FOYAXO.mjs.map → chunk-IGEKO7ZU.mjs.map} +0 -0
- /package/dist/lib/node/{chunk-LP4FG423.cjs.map → chunk-5J3FBCON.cjs.map} +0 -0
- /package/dist/lib/node-esm/{TranscriptContainer-ZXUCOWGY.mjs.map → TranscriptContainer-DLNRFVTA.mjs.map} +0 -0
- /package/dist/lib/node-esm/{chunk-J4D2RBBH.mjs.map → chunk-KUWWHMOA.mjs.map} +0 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { EditorView } from '@codemirror/view';
|
|
6
|
+
import { describe, test } from 'vitest';
|
|
7
|
+
|
|
8
|
+
import { type DataType } from '@dxos/schema';
|
|
9
|
+
|
|
10
|
+
import { SerializationModel, DocumentAdapter, type ChunkRenderer } from './model';
|
|
11
|
+
|
|
12
|
+
const blockToMarkdown: ChunkRenderer<DataType.Message> = (
|
|
13
|
+
message: DataType.Message,
|
|
14
|
+
index: number,
|
|
15
|
+
debug = true,
|
|
16
|
+
): string[] => {
|
|
17
|
+
return [
|
|
18
|
+
`###### ${message.sender.name}`,
|
|
19
|
+
...message.blocks.filter((block) => block.type === 'transcription').map((block) => block.text),
|
|
20
|
+
'',
|
|
21
|
+
];
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const createDate = () => new Date().toISOString();
|
|
25
|
+
|
|
26
|
+
describe('SerializationModel', () => {
|
|
27
|
+
test('basic', ({ expect }) => {
|
|
28
|
+
const model = new SerializationModel<DataType.Message>(blockToMarkdown);
|
|
29
|
+
expect(model.chunks.length).to.eq(0);
|
|
30
|
+
expect(model.doc.toString()).to.eq('');
|
|
31
|
+
|
|
32
|
+
// Create message.
|
|
33
|
+
const message: DataType.Message = {
|
|
34
|
+
id: '1',
|
|
35
|
+
created: createDate(),
|
|
36
|
+
sender: { name: 'Alice' },
|
|
37
|
+
blocks: [
|
|
38
|
+
{
|
|
39
|
+
type: 'transcription',
|
|
40
|
+
started: createDate(),
|
|
41
|
+
text: 'Hello world!',
|
|
42
|
+
},
|
|
43
|
+
],
|
|
44
|
+
};
|
|
45
|
+
model.appendChunk(message);
|
|
46
|
+
{
|
|
47
|
+
const text = model.doc.toString();
|
|
48
|
+
expect(text).to.eq('###### Alice\nHello world!\n\n');
|
|
49
|
+
expect(model.chunks.length).to.eq(1);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Update message.
|
|
53
|
+
message.blocks.push({ type: 'transcription', started: createDate(), text: 'Hello again!' });
|
|
54
|
+
model.updateChunk(message);
|
|
55
|
+
{
|
|
56
|
+
const text = model.doc.toString();
|
|
57
|
+
expect(text).to.eq('###### Alice\nHello world!\nHello again!\n\n');
|
|
58
|
+
expect(model.chunks.length).to.eq(1);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
test('sync - append', ({ expect }) => {
|
|
63
|
+
const view = new EditorView({ extensions: [], doc: '' });
|
|
64
|
+
const model = new SerializationModel<DataType.Message>(blockToMarkdown);
|
|
65
|
+
const adapter = new DocumentAdapter(view);
|
|
66
|
+
expect(adapter.lineCount()).to.eq(0);
|
|
67
|
+
expect(view.state.doc.toString()).to.eq('');
|
|
68
|
+
|
|
69
|
+
// Append message.
|
|
70
|
+
{
|
|
71
|
+
const message: DataType.Message = {
|
|
72
|
+
id: '1',
|
|
73
|
+
created: createDate(),
|
|
74
|
+
sender: { name: 'Alice' },
|
|
75
|
+
blocks: [
|
|
76
|
+
{
|
|
77
|
+
type: 'transcription',
|
|
78
|
+
started: createDate(),
|
|
79
|
+
text: 'Hello world!',
|
|
80
|
+
},
|
|
81
|
+
],
|
|
82
|
+
};
|
|
83
|
+
model.appendChunk(message);
|
|
84
|
+
model.sync(adapter);
|
|
85
|
+
expect(view.state.doc.toString()).to.eq('###### Alice\nHello world!\n\n');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Append message.
|
|
89
|
+
{
|
|
90
|
+
const message: DataType.Message = {
|
|
91
|
+
id: '2',
|
|
92
|
+
created: createDate(),
|
|
93
|
+
sender: { name: 'Bob' },
|
|
94
|
+
blocks: [
|
|
95
|
+
{
|
|
96
|
+
type: 'transcription',
|
|
97
|
+
started: createDate(),
|
|
98
|
+
text: 'Hello world!',
|
|
99
|
+
},
|
|
100
|
+
],
|
|
101
|
+
};
|
|
102
|
+
model.appendChunk(message);
|
|
103
|
+
model.sync(adapter);
|
|
104
|
+
expect(view.state.doc.toString()).to.eq('###### Alice\nHello world!\n\n###### Bob\nHello world!\n\n');
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test('sync - append, update, delete', ({ expect }) => {
|
|
109
|
+
const view = new EditorView({ extensions: [], doc: '' });
|
|
110
|
+
const model = new SerializationModel<DataType.Message>(blockToMarkdown);
|
|
111
|
+
const adapter = new DocumentAdapter(view);
|
|
112
|
+
expect(adapter.lineCount()).to.eq(0);
|
|
113
|
+
|
|
114
|
+
// Append message.
|
|
115
|
+
{
|
|
116
|
+
const message: DataType.Message = {
|
|
117
|
+
id: '1',
|
|
118
|
+
created: createDate(),
|
|
119
|
+
sender: { name: 'Alice' },
|
|
120
|
+
blocks: [
|
|
121
|
+
{
|
|
122
|
+
type: 'transcription',
|
|
123
|
+
started: createDate(),
|
|
124
|
+
text: 'Hello world!',
|
|
125
|
+
},
|
|
126
|
+
],
|
|
127
|
+
};
|
|
128
|
+
model.appendChunk(message);
|
|
129
|
+
model.sync(adapter);
|
|
130
|
+
expect(view.state.doc.toString()).to.eq('###### Alice\nHello world!\n\n');
|
|
131
|
+
|
|
132
|
+
// Update message (add block).
|
|
133
|
+
message.blocks.push({ type: 'transcription', started: createDate(), text: 'Hello again!' });
|
|
134
|
+
model.updateChunk(message);
|
|
135
|
+
model.sync(adapter);
|
|
136
|
+
expect(view.state.doc.toString()).to.eq('###### Alice\nHello world!\nHello again!\n\n');
|
|
137
|
+
|
|
138
|
+
// Update message (remove block).
|
|
139
|
+
message.blocks.splice(0, 1);
|
|
140
|
+
model.updateChunk(message);
|
|
141
|
+
model.sync(adapter);
|
|
142
|
+
expect(view.state.doc.toString()).to.eq('###### Alice\nHello again!\n\n');
|
|
143
|
+
|
|
144
|
+
expect(model.getChunkAtLine(1)).to.eq(message);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// Append message.
|
|
148
|
+
{
|
|
149
|
+
const message: DataType.Message = {
|
|
150
|
+
id: '2',
|
|
151
|
+
created: createDate(),
|
|
152
|
+
sender: { name: 'Bob' },
|
|
153
|
+
blocks: [{ type: 'transcription', started: createDate(), text: 'Hello again!' }],
|
|
154
|
+
};
|
|
155
|
+
model.appendChunk(message);
|
|
156
|
+
model.sync(adapter);
|
|
157
|
+
expect(view.state.doc.toString()).to.eq('###### Alice\nHello again!\n\n###### Bob\nHello again!\n\n');
|
|
158
|
+
|
|
159
|
+
expect(model.getChunkAtLine(4)).to.eq(message);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Delete block.
|
|
163
|
+
{
|
|
164
|
+
model.deleteBlock('1');
|
|
165
|
+
model.sync(adapter);
|
|
166
|
+
expect(view.state.doc.toString()).to.eq('###### Bob\nHello again!\n\n');
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
});
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { Text } from '@codemirror/state';
|
|
6
|
+
import { type EditorView } from '@codemirror/view';
|
|
7
|
+
|
|
8
|
+
import { Event } from '@dxos/async';
|
|
9
|
+
import { invariant } from '@dxos/invariant';
|
|
10
|
+
import { log } from '@dxos/log';
|
|
11
|
+
|
|
12
|
+
export type Chunk = { id: string };
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Covert block to a set of markdown lines.
|
|
16
|
+
*/
|
|
17
|
+
export type ChunkRenderer<T extends Chunk> = (chunk: T, index: number, debug?: boolean) => string[];
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Abstract representation of the document model.
|
|
21
|
+
*/
|
|
22
|
+
export interface ChunkDocument {
|
|
23
|
+
lineCount(): number;
|
|
24
|
+
replaceLines(from: number, remove: number, insert: string[]): void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Codemirror document adapter.
|
|
29
|
+
*/
|
|
30
|
+
export class DocumentAdapter implements ChunkDocument {
|
|
31
|
+
constructor(private readonly _view: EditorView) {}
|
|
32
|
+
|
|
33
|
+
/** The document must always have at least one line. */
|
|
34
|
+
lineCount(): number {
|
|
35
|
+
return this._view.state.doc.lines - 1;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
replaceLines(from: number, remove: number, insert: string[]): void {
|
|
39
|
+
log('replaceLines', { count: this._view.state.doc.lines, from, remove, insert });
|
|
40
|
+
|
|
41
|
+
const numLines = this._view.state.doc.lines;
|
|
42
|
+
const posFrom = this._view.state.doc.line(from).from;
|
|
43
|
+
const posTo = remove === 0 ? posFrom : this._view.state.doc.line(Math.min(numLines, from + remove)).from;
|
|
44
|
+
|
|
45
|
+
// Don't remove the last line.
|
|
46
|
+
let text = insert.join('\n');
|
|
47
|
+
if (posTo === this._view.state.doc.length) {
|
|
48
|
+
text += '\n';
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
this._view.dispatch({
|
|
52
|
+
changes: { from: posFrom, to: posTo, insert: text },
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
type ChunkChange<T extends Chunk> =
|
|
58
|
+
| {
|
|
59
|
+
type: 'append';
|
|
60
|
+
index: number;
|
|
61
|
+
chunk: T;
|
|
62
|
+
}
|
|
63
|
+
| {
|
|
64
|
+
type: 'update';
|
|
65
|
+
index: number;
|
|
66
|
+
chunk: T;
|
|
67
|
+
}
|
|
68
|
+
| {
|
|
69
|
+
type: 'delete';
|
|
70
|
+
index: number;
|
|
71
|
+
id: string;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Each Chunk can be converted into a set of lines.
|
|
76
|
+
* Chunks can be added, updated or deleted from the Queue.
|
|
77
|
+
* A Document is an immutable projection that represents a flat list of lines corresponding to the Queue.
|
|
78
|
+
*
|
|
79
|
+
* Ideally we would implement a custom virtual Text model for the View, but this currently isn't possible in Codemirror.
|
|
80
|
+
* Instead this model tracks changes and syncs them with the ChunkDocument.
|
|
81
|
+
*/
|
|
82
|
+
export class SerializationModel<T extends Chunk> {
|
|
83
|
+
/** Ordered set of chunks. */
|
|
84
|
+
private readonly _chunks: T[] = [];
|
|
85
|
+
|
|
86
|
+
/** Map of chunk IDs to their current line counts. */
|
|
87
|
+
private readonly _chunkLineCounts = new Map<string, number>();
|
|
88
|
+
|
|
89
|
+
/** Map of line numbers to chunk IDs. */
|
|
90
|
+
private readonly _lineToChunk = new Map<number, T>();
|
|
91
|
+
|
|
92
|
+
/** Track chunk changes since last sync. */
|
|
93
|
+
private readonly _changes: Array<ChunkChange<T>> = [];
|
|
94
|
+
|
|
95
|
+
/** Emits when the document is updated. */
|
|
96
|
+
public readonly update = new Event<void>();
|
|
97
|
+
|
|
98
|
+
constructor(
|
|
99
|
+
private readonly _renderer: ChunkRenderer<T>,
|
|
100
|
+
initialChunks: T[] = [],
|
|
101
|
+
) {
|
|
102
|
+
initialChunks.forEach((chunk) => {
|
|
103
|
+
this.appendChunk(chunk);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
toJSON(): { chunks: number; changes: number } {
|
|
108
|
+
return {
|
|
109
|
+
chunks: this._chunks.length,
|
|
110
|
+
changes: this._changes.length,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
get doc() {
|
|
115
|
+
return Text.of([...this._chunks.flatMap((chunk, index) => this._renderer(chunk, index)), '']);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
get chunks() {
|
|
119
|
+
return this._chunks;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Get the block ID at the given line number.
|
|
124
|
+
*/
|
|
125
|
+
getChunkAtLine(line: number): T | undefined {
|
|
126
|
+
return this._lineToChunk.get(line);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
reset(): this {
|
|
130
|
+
this._chunks.length = 0;
|
|
131
|
+
this._chunkLineCounts.clear();
|
|
132
|
+
this._lineToChunk.clear();
|
|
133
|
+
this.update.emit();
|
|
134
|
+
return this;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
appendChunk(chunk: T): this {
|
|
138
|
+
const index = this._chunks.length;
|
|
139
|
+
this._chunks.push(chunk);
|
|
140
|
+
this._changes.push({ type: 'append', index, chunk });
|
|
141
|
+
this.update.emit();
|
|
142
|
+
return this;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
updateChunk(chunk: T): this {
|
|
146
|
+
const index = this._chunks.findIndex((c) => c.id === chunk.id);
|
|
147
|
+
invariant(index !== -1);
|
|
148
|
+
this._chunks[index] = chunk;
|
|
149
|
+
this._changes.push({ type: 'update', index, chunk });
|
|
150
|
+
this.update.emit();
|
|
151
|
+
return this;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
deleteBlock(id: string): this {
|
|
155
|
+
const index = this._chunks.findIndex((c) => c.id === id);
|
|
156
|
+
invariant(index !== -1);
|
|
157
|
+
this._chunks.splice(index, 1);
|
|
158
|
+
this._changes.push({ type: 'delete', index, id });
|
|
159
|
+
this.update.emit();
|
|
160
|
+
return this;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Syncs the tracked changes with the ChunkDocument.
|
|
165
|
+
*/
|
|
166
|
+
sync(document: ChunkDocument): this {
|
|
167
|
+
log('sync', { changes: this._changes });
|
|
168
|
+
|
|
169
|
+
// Process each change in order.
|
|
170
|
+
for (const change of this._changes) {
|
|
171
|
+
switch (change.type) {
|
|
172
|
+
case 'append': {
|
|
173
|
+
// For appends, convert block to lines and add at the end.
|
|
174
|
+
const lines = this._renderer(change.chunk, change.index);
|
|
175
|
+
const lastLine = document.lineCount();
|
|
176
|
+
document.replaceLines(lastLine + 1, 0, lines);
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
case 'update': {
|
|
181
|
+
// For updates, find the block's line range and replace with new lines.
|
|
182
|
+
let lineStart = 1;
|
|
183
|
+
for (let i = 0; i < change.index; i++) {
|
|
184
|
+
lineStart += this._chunkLineCounts.get(this._chunks[i].id)!;
|
|
185
|
+
}
|
|
186
|
+
const previousLineCount = this._chunkLineCounts.get(change.chunk.id)!;
|
|
187
|
+
const newLines = this._renderer(change.chunk, change.index);
|
|
188
|
+
document.replaceLines(lineStart, previousLineCount, newLines);
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
case 'delete': {
|
|
193
|
+
// For deletes, find the block's line range and remove those lines.
|
|
194
|
+
let lineStart = 1;
|
|
195
|
+
for (let i = 0; i < change.index; i++) {
|
|
196
|
+
lineStart += this._chunkLineCounts.get(this._chunks[i].id)!;
|
|
197
|
+
}
|
|
198
|
+
const previousLineCount = this._chunkLineCounts.get(change.id)!;
|
|
199
|
+
document.replaceLines(lineStart, previousLineCount, []);
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Clear the line-to-block mapping.
|
|
206
|
+
this._lineToChunk.clear();
|
|
207
|
+
|
|
208
|
+
// Update all block line counts and line-to-block mapping after processing changes.
|
|
209
|
+
let currentLine = 1;
|
|
210
|
+
for (const [index, chunk] of this._chunks.entries()) {
|
|
211
|
+
const lineCount = this._renderer(chunk, index).length;
|
|
212
|
+
this._chunkLineCounts.set(chunk.id, lineCount);
|
|
213
|
+
this._lineToChunk.set(currentLine, chunk);
|
|
214
|
+
currentLine += lineCount;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// Clean up deleted blocks.
|
|
218
|
+
const chunkIds = new Set(this._chunks.map((chunk) => chunk.id));
|
|
219
|
+
for (const id of this._chunkLineCounts.keys()) {
|
|
220
|
+
if (!chunkIds.has(id)) {
|
|
221
|
+
this._chunkLineCounts.delete(id);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Clear changes after sync.
|
|
226
|
+
this._changes.length = 0;
|
|
227
|
+
return this;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
//
|
|
6
|
+
// Copyright 2025 DXOS.org
|
|
7
|
+
//
|
|
8
|
+
|
|
9
|
+
import { effect } from '@preact/signals-core';
|
|
10
|
+
|
|
11
|
+
import { asyncTimeout, DeferredTask } from '@dxos/async';
|
|
12
|
+
import { LifecycleState, Resource } from '@dxos/context';
|
|
13
|
+
import { type Queue } from '@dxos/echo-db';
|
|
14
|
+
import { type FunctionExecutor } from '@dxos/functions';
|
|
15
|
+
import { log } from '@dxos/log';
|
|
16
|
+
import { type DataType } from '@dxos/schema';
|
|
17
|
+
|
|
18
|
+
import { type MessageWithRangeId, sentenceNormalization } from './normalization';
|
|
19
|
+
import { getActorId } from './utils';
|
|
20
|
+
|
|
21
|
+
const PROCESSING_TIMEOUT = 20_000; // ms
|
|
22
|
+
const MAX_RANGE_ID_COUNT = 10;
|
|
23
|
+
|
|
24
|
+
export type SegmentsNormalizerParams = {
|
|
25
|
+
functionExecutor: FunctionExecutor;
|
|
26
|
+
queue: Queue<DataType.Message>;
|
|
27
|
+
startingCursor: QueueCursor;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
// TODO(mykola): Use generic queue cursor from @dxos/protocols.
|
|
31
|
+
export type QueueCursor = {
|
|
32
|
+
actorId: string;
|
|
33
|
+
timestamp: string;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// TODO(mykola): .
|
|
37
|
+
export class MessageNormalizer extends Resource {
|
|
38
|
+
private readonly _functionExecutor: FunctionExecutor;
|
|
39
|
+
private _queue: Queue<DataType.Message>;
|
|
40
|
+
private _cursor: QueueCursor;
|
|
41
|
+
private _messagesToProcess: MessageWithRangeId[] = [];
|
|
42
|
+
private _normalizationTask?: DeferredTask;
|
|
43
|
+
private _lastProcessedMessageIds?: string[];
|
|
44
|
+
|
|
45
|
+
constructor({ functionExecutor, queue, startingCursor }: SegmentsNormalizerParams) {
|
|
46
|
+
super();
|
|
47
|
+
this._functionExecutor = functionExecutor;
|
|
48
|
+
this._queue = queue;
|
|
49
|
+
this._cursor = startingCursor;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
protected override async _open(): Promise<void> {
|
|
53
|
+
this._normalizationTask = new DeferredTask(this._ctx, () => this._processMessages());
|
|
54
|
+
const unsubscribe = effect(() => {
|
|
55
|
+
if (this._lifecycleState !== LifecycleState.OPEN) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
this._messagesToProcess = this._queue.objects.filter((message) => {
|
|
60
|
+
const actorId = getActorId(message.sender);
|
|
61
|
+
return actorId === this._cursor.actorId && message.created >= this._cursor.timestamp;
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
this._normalizationTask!.schedule();
|
|
65
|
+
});
|
|
66
|
+
this._ctx.onDispose(() => unsubscribe());
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Need to unpack strings from blocks from messages run them through the function and then pack them back into blocks into messages.
|
|
70
|
+
private async _processMessages(): Promise<void> {
|
|
71
|
+
const messages = this._messagesToProcess;
|
|
72
|
+
this._messagesToProcess = [];
|
|
73
|
+
if (
|
|
74
|
+
this._lastProcessedMessageIds &&
|
|
75
|
+
messages.every((message) => this._lastProcessedMessageIds!.includes(message.id))
|
|
76
|
+
) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
this._lastProcessedMessageIds = messages.map((message) => message.id);
|
|
81
|
+
|
|
82
|
+
try {
|
|
83
|
+
// TODO(mykola): Executor should support timeout.
|
|
84
|
+
const response = await asyncTimeout(
|
|
85
|
+
this._functionExecutor.invoke(sentenceNormalization, { messages }),
|
|
86
|
+
PROCESSING_TIMEOUT,
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
this._writeMessages(response.sentences);
|
|
90
|
+
this._lastProcessedMessageIds.push(...response.sentences.map((sentence) => sentence.id));
|
|
91
|
+
if (response.sentences.length === 1 && response.sentences.at(-1)!.rangeId!.length > MAX_RANGE_ID_COUNT) {
|
|
92
|
+
log.warn('Sentence is too long', { messages });
|
|
93
|
+
this._cursor.timestamp = new Date(new Date(response.sentences[0].created).getTime() + 1).toISOString();
|
|
94
|
+
}
|
|
95
|
+
} catch {
|
|
96
|
+
log.error('Failed to normalize segments', { messages });
|
|
97
|
+
// If processing failed, emit the segments as is.
|
|
98
|
+
this._writeMessages(messages);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
private _writeMessages(messages: MessageWithRangeId[]): void {
|
|
103
|
+
log.info('writing messages', { messages });
|
|
104
|
+
const lastMessage = messages[messages.length - 1];
|
|
105
|
+
this._cursor.timestamp = lastMessage.created;
|
|
106
|
+
this._queue.append(messages);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { effect } from '@preact/signals-core';
|
|
6
|
+
import { describe, test } from 'vitest';
|
|
7
|
+
|
|
8
|
+
import { AIServiceEdgeClient, OllamaClient } from '@dxos/ai';
|
|
9
|
+
import { AI_SERVICE_ENDPOINT } from '@dxos/ai/testing';
|
|
10
|
+
import { scheduleTaskInterval } from '@dxos/async';
|
|
11
|
+
import { Context } from '@dxos/context';
|
|
12
|
+
import { MemoryQueue } from '@dxos/echo-db';
|
|
13
|
+
import { createQueueDxn, ObjectId } from '@dxos/echo-schema';
|
|
14
|
+
import { FunctionExecutor, ServiceContainer } from '@dxos/functions';
|
|
15
|
+
import { log } from '@dxos/log';
|
|
16
|
+
import { type DataType } from '@dxos/schema';
|
|
17
|
+
|
|
18
|
+
import { MessageNormalizer } from './message-normalizer';
|
|
19
|
+
import { type MessageWithRangeId, sentenceNormalization } from './normalization';
|
|
20
|
+
import { getActorId } from './utils';
|
|
21
|
+
|
|
22
|
+
const sender: DataType.Actor = {
|
|
23
|
+
identityDid: 'did:key:123',
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
// Generate bunch of complex messages.
|
|
27
|
+
const messages: MessageWithRangeId[] = [
|
|
28
|
+
// Control message.
|
|
29
|
+
'Hello, every body. We will talk about quantum entanglement today',
|
|
30
|
+
|
|
31
|
+
// Two sentences in one message.
|
|
32
|
+
'hello every body we will talk about quantum entanglement today',
|
|
33
|
+
|
|
34
|
+
// Sentence broken into pieces.
|
|
35
|
+
'quantum entanglement',
|
|
36
|
+
'is one of the most...',
|
|
37
|
+
'perplexing and fascinating phenomena',
|
|
38
|
+
'in modern physics',
|
|
39
|
+
|
|
40
|
+
// Two sentences without separators.
|
|
41
|
+
'it challenges our classical intuitions about how the universe works and forces us to reconsider our assumptions about space, time, and information',
|
|
42
|
+
'at its core, quantum entanglement refers to a peculiar connection between two or more particles in such a way that the state of one particle instantly...',
|
|
43
|
+
'determines the state of the other, no matter how far apart they may be — even if they are light-years away',
|
|
44
|
+
|
|
45
|
+
// 2 sentences are merged.
|
|
46
|
+
'this seemingly instantaneous correlation between distant particles has baffled physicists since the early 20th century to understand entanglement, we',
|
|
47
|
+
'first need to touch on the fundamentals of quantum mechanics',
|
|
48
|
+
|
|
49
|
+
// No punctuation.
|
|
50
|
+
'in classical physics objects have well-defined properties such as position speed and momentum',
|
|
51
|
+
].map((string, index) => ({
|
|
52
|
+
id: ObjectId.random(),
|
|
53
|
+
created: new Date(Date.now() + 1000 * index).toISOString(),
|
|
54
|
+
sender,
|
|
55
|
+
blocks: [{ type: 'transcription', started: new Date(Date.now() + 1000 * index).toISOString(), text: string }],
|
|
56
|
+
rangeId: [],
|
|
57
|
+
}));
|
|
58
|
+
|
|
59
|
+
const REMOTE_AI = true;
|
|
60
|
+
|
|
61
|
+
describe.skip('SentenceNormalization', () => {
|
|
62
|
+
const getExecutor = () => {
|
|
63
|
+
return new FunctionExecutor(
|
|
64
|
+
new ServiceContainer().setServices({
|
|
65
|
+
ai: {
|
|
66
|
+
client: REMOTE_AI
|
|
67
|
+
? new AIServiceEdgeClient({
|
|
68
|
+
endpoint: AI_SERVICE_ENDPOINT.REMOTE,
|
|
69
|
+
defaultGenerationOptions: {
|
|
70
|
+
model: '@anthropic/claude-3-5-sonnet-20241022',
|
|
71
|
+
},
|
|
72
|
+
})
|
|
73
|
+
: new OllamaClient({
|
|
74
|
+
overrides: {
|
|
75
|
+
model: 'llama3.1:8b',
|
|
76
|
+
},
|
|
77
|
+
}),
|
|
78
|
+
},
|
|
79
|
+
}),
|
|
80
|
+
);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
test('messages merging', { timeout: 120_000 }, async () => {
|
|
84
|
+
const executor = getExecutor();
|
|
85
|
+
const sentences: MessageWithRangeId[] = [];
|
|
86
|
+
let buffer: MessageWithRangeId[] = [];
|
|
87
|
+
let activeSentenceIndex = 0;
|
|
88
|
+
for (const message of messages) {
|
|
89
|
+
const result = await executor.invoke(sentenceNormalization, {
|
|
90
|
+
messages: [...buffer, message],
|
|
91
|
+
});
|
|
92
|
+
log.info('BEFORE sentence splicing', { activeSentenceIndex, sentences, inserting: result.sentences });
|
|
93
|
+
sentences.splice(activeSentenceIndex, sentences.length - activeSentenceIndex, ...result.sentences);
|
|
94
|
+
log.info('AFTER sentence splicing', { activeSentenceIndex, sentences });
|
|
95
|
+
|
|
96
|
+
if (sentences.length > 0) {
|
|
97
|
+
activeSentenceIndex = sentences.length - 1;
|
|
98
|
+
buffer = sentences.slice(activeSentenceIndex);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
log.info('sentences', {
|
|
102
|
+
originalMessages: JSON.stringify(messages, null, 2),
|
|
103
|
+
sentences: JSON.stringify(sentences, null, 2),
|
|
104
|
+
});
|
|
105
|
+
throw new Error('test');
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test.only('queue', { timeout: 120_000 }, async () => {
|
|
109
|
+
// Create queue.
|
|
110
|
+
const queue = new MemoryQueue<DataType.Message>(createQueueDxn());
|
|
111
|
+
const ctx = new Context();
|
|
112
|
+
let idx = 0;
|
|
113
|
+
scheduleTaskInterval(
|
|
114
|
+
ctx,
|
|
115
|
+
async () => {
|
|
116
|
+
if (idx >= messages.length) {
|
|
117
|
+
void ctx.dispose();
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
await queue.append([messages[idx]]);
|
|
121
|
+
idx++;
|
|
122
|
+
},
|
|
123
|
+
2_000,
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
// Create normalizer.
|
|
127
|
+
const normalizer = new MessageNormalizer({
|
|
128
|
+
functionExecutor: getExecutor(),
|
|
129
|
+
queue,
|
|
130
|
+
startingCursor: { actorId: getActorId(sender), timestamp: '0' },
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
// Start normalizer.
|
|
134
|
+
await normalizer.open();
|
|
135
|
+
effect(() => {
|
|
136
|
+
log.info('normalizer');
|
|
137
|
+
log.info(JSON.stringify(queue.objects, null, 2));
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
await new Promise(() => {});
|
|
141
|
+
});
|
|
142
|
+
});
|