@flamingo-stack/openframe-frontend-core 0.0.508 → 0.0.509
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/chat-protocol/decode.d.ts +58 -0
- package/dist/chat-protocol/decode.d.ts.map +1 -0
- package/dist/chat-protocol/encode.d.ts +44 -0
- package/dist/chat-protocol/encode.d.ts.map +1 -0
- package/dist/chat-protocol/env-flag.d.ts +35 -0
- package/dist/chat-protocol/env-flag.d.ts.map +1 -0
- package/dist/chat-protocol/events.d.ts +216 -0
- package/dist/chat-protocol/events.d.ts.map +1 -0
- package/dist/chat-protocol/frames.d.ts +213 -0
- package/dist/chat-protocol/frames.d.ts.map +1 -0
- package/dist/chat-protocol/index.cjs +506 -0
- package/dist/chat-protocol/index.cjs.map +1 -0
- package/dist/chat-protocol/index.d.ts +18 -0
- package/dist/chat-protocol/index.d.ts.map +1 -0
- package/dist/chat-protocol/index.js +490 -0
- package/dist/chat-protocol/index.js.map +1 -0
- package/dist/chat-protocol/ip-normalize.d.ts +44 -0
- package/dist/chat-protocol/ip-normalize.d.ts.map +1 -0
- package/dist/chat-protocol/nats-decoder.d.ts +26 -0
- package/dist/chat-protocol/nats-decoder.d.ts.map +1 -0
- package/dist/{chunk-3SQ5KXHQ.cjs → chunk-2T4GTV25.cjs} +9 -9
- package/dist/{chunk-3SQ5KXHQ.cjs.map → chunk-2T4GTV25.cjs.map} +1 -1
- package/dist/{chunk-CWAOV2FK.cjs → chunk-3CGZPAGR.cjs} +3 -3
- package/dist/{chunk-CWAOV2FK.cjs.map → chunk-3CGZPAGR.cjs.map} +1 -1
- package/dist/{chunk-TLMJHMXJ.js → chunk-3QSR22IC.js} +12 -12
- package/dist/chunk-3QSR22IC.js.map +1 -0
- package/dist/{chunk-TWYNR4TZ.cjs → chunk-4G5TLHJD.cjs} +7141 -4799
- package/dist/chunk-4G5TLHJD.cjs.map +1 -0
- package/dist/{chunk-ADPMHWOE.js → chunk-72XAID7Y.js} +4 -4
- package/dist/{chunk-PAGKRNWK.js → chunk-7WZHBQ4J.js} +2 -2
- package/dist/{chunk-OCEKO5CW.js → chunk-AEELJC4N.js} +12106 -9764
- package/dist/chunk-AEELJC4N.js.map +1 -0
- package/dist/{chunk-Z42BGM6Q.cjs → chunk-BT2WWDFP.cjs} +5 -5
- package/dist/{chunk-Z42BGM6Q.cjs.map → chunk-BT2WWDFP.cjs.map} +1 -1
- package/dist/{chunk-7LEL3RIX.cjs → chunk-C7OY3PQP.cjs} +11 -11
- package/dist/{chunk-7LEL3RIX.cjs.map → chunk-C7OY3PQP.cjs.map} +1 -1
- package/dist/{chunk-RHGGEKPQ.cjs → chunk-CIOLOQKQ.cjs} +4 -4
- package/dist/{chunk-RHGGEKPQ.cjs.map → chunk-CIOLOQKQ.cjs.map} +1 -1
- package/dist/{chunk-TSZHM74B.js → chunk-DC2TKS7C.js} +2 -2
- package/dist/{chunk-M2GOR3XQ.cjs → chunk-DYPYR7FR.cjs} +87 -87
- package/dist/{chunk-M2GOR3XQ.cjs.map → chunk-DYPYR7FR.cjs.map} +1 -1
- package/dist/{chunk-I64ABCDX.js → chunk-I2B6X77L.js} +2 -2
- package/dist/{chunk-NIZDKTGL.cjs → chunk-K5MSCMDK.cjs} +37 -37
- package/dist/{chunk-NIZDKTGL.cjs.map → chunk-K5MSCMDK.cjs.map} +1 -1
- package/dist/{chunk-DTYRYB2N.js → chunk-K6QTZ7CZ.js} +2 -2
- package/dist/{chunk-4LTMXDUS.js → chunk-KBN7PMFT.js} +2 -2
- package/dist/{chunk-TBMV7I5N.js → chunk-KXZO2WZX.js} +2 -2
- package/dist/{chunk-QTYZMP6D.cjs → chunk-LBAKLT6T.cjs} +62 -62
- package/dist/chunk-LBAKLT6T.cjs.map +1 -0
- package/dist/{chunk-KEBYLU3U.js → chunk-LFUPI7VX.js} +2 -2
- package/dist/{chunk-LDTR4IVZ.cjs → chunk-M37PBG4N.cjs} +31 -31
- package/dist/{chunk-LDTR4IVZ.cjs.map → chunk-M37PBG4N.cjs.map} +1 -1
- package/dist/{chunk-WA7RR64F.js → chunk-PO5O654H.js} +6 -5
- package/dist/chunk-PO5O654H.js.map +1 -0
- package/dist/{chunk-WK4N5VBX.cjs → chunk-QJJISLPA.cjs} +28 -27
- package/dist/chunk-QJJISLPA.cjs.map +1 -0
- package/dist/{chunk-2F44PLTV.cjs → chunk-T7MCSA55.cjs} +26 -26
- package/dist/{chunk-2F44PLTV.cjs.map → chunk-T7MCSA55.cjs.map} +1 -1
- package/dist/{chunk-WJQHJD7J.js → chunk-TWSKTJHW.js} +2 -2
- package/dist/{chunk-ZSQHZYCO.cjs → chunk-UQDMJ6DH.cjs} +13 -13
- package/dist/{chunk-ZSQHZYCO.cjs.map → chunk-UQDMJ6DH.cjs.map} +1 -1
- package/dist/{chunk-TZRUCD56.js → chunk-V3ZAAFE3.js} +5 -5
- package/dist/{chunk-LRGHJPET.js → chunk-XGIFOHKE.js} +2 -2
- package/dist/{chunk-POOMO3PA.cjs → chunk-YQEPRYT5.cjs} +7 -7
- package/dist/{chunk-POOMO3PA.cjs.map → chunk-YQEPRYT5.cjs.map} +1 -1
- package/dist/components/case-studies/index.cjs +8 -8
- package/dist/components/case-studies/index.js +2 -2
- package/dist/components/chat/chat-message-enhanced.d.ts.map +1 -1
- package/dist/components/chat/chat-message-list.d.ts.map +1 -1
- package/dist/components/chat/hooks/index.d.ts +0 -1
- package/dist/components/chat/hooks/index.d.ts.map +1 -1
- package/dist/components/chat/hooks/use-chat.d.ts.map +1 -1
- package/dist/components/chat/hooks/use-chunk-catchup.d.ts.map +1 -1
- package/dist/components/chat/hooks/use-nats-chat-adapter.d.ts +15 -48
- package/dist/components/chat/hooks/use-nats-chat-adapter.d.ts.map +1 -1
- package/dist/components/chat/hooks/use-sse-chat-adapter.d.ts +10 -57
- package/dist/components/chat/hooks/use-sse-chat-adapter.d.ts.map +1 -1
- package/dist/components/chat/index.cjs +10 -2
- package/dist/components/chat/index.cjs.map +1 -1
- package/dist/components/chat/index.d.ts +1 -0
- package/dist/components/chat/index.d.ts.map +1 -1
- package/dist/components/chat/index.js +21 -13
- package/dist/components/chat/stream/chat-dialog-store.d.ts +184 -0
- package/dist/components/chat/stream/chat-dialog-store.d.ts.map +1 -0
- package/dist/components/chat/stream/chat-stream-reducer.d.ts +380 -0
- package/dist/components/chat/stream/chat-stream-reducer.d.ts.map +1 -0
- package/dist/components/chat/stream/delta-batcher.d.ts +51 -0
- package/dist/components/chat/stream/delta-batcher.d.ts.map +1 -0
- package/dist/components/chat/stream/index.d.ts +17 -0
- package/dist/components/chat/stream/index.d.ts.map +1 -0
- package/dist/components/chat/stream/message-mutations.d.ts +77 -0
- package/dist/components/chat/stream/message-mutations.d.ts.map +1 -0
- package/dist/components/chat/stream/use-chat-stream-reducer.d.ts +22 -0
- package/dist/components/chat/stream/use-chat-stream-reducer.d.ts.map +1 -0
- package/dist/components/chat/types/api.types.d.ts +1 -81
- package/dist/components/chat/types/api.types.d.ts.map +1 -1
- package/dist/components/chat/types/processing.types.d.ts +2 -93
- package/dist/components/chat/types/processing.types.d.ts.map +1 -1
- package/dist/components/chat/types/unified-chat-state.types.d.ts +15 -0
- package/dist/components/chat/types/unified-chat-state.types.d.ts.map +1 -1
- package/dist/components/chat/utils/extract-incomplete-message-state.d.ts +34 -6
- package/dist/components/chat/utils/extract-incomplete-message-state.d.ts.map +1 -1
- package/dist/components/chat/utils/history-merge.d.ts.map +1 -1
- package/dist/components/chat/utils/index.d.ts +2 -3
- package/dist/components/chat/utils/index.d.ts.map +1 -1
- package/dist/components/chat/utils/process-historical-messages.d.ts +59 -11
- package/dist/components/chat/utils/process-historical-messages.d.ts.map +1 -1
- package/dist/components/contact/index.cjs +3 -3
- package/dist/components/contact/index.js +2 -2
- package/dist/components/docs/index.cjs +5 -5
- package/dist/components/docs/index.js +4 -4
- package/dist/components/embeds/index.cjs +3 -3
- package/dist/components/embeds/index.js +2 -2
- package/dist/components/faq/index.cjs +3 -3
- package/dist/components/faq/index.js +2 -2
- package/dist/components/features/index.cjs +2 -2
- package/dist/components/features/index.js +1 -1
- package/dist/components/help-center-pages/index.cjs +22 -22
- package/dist/components/help-center-pages/index.js +13 -13
- package/dist/components/index.cjs +174 -132
- package/dist/components/index.cjs.map +1 -1
- package/dist/components/index.js +65 -23
- package/dist/components/index.js.map +1 -1
- package/dist/components/meeting-scheduler/index.cjs +34 -34
- package/dist/components/meeting-scheduler/index.js +3 -3
- package/dist/components/navigation/index.cjs +2 -2
- package/dist/components/navigation/index.js +1 -1
- package/dist/components/onboarding-guides/index.cjs +5 -5
- package/dist/components/onboarding-guides/index.js +4 -4
- package/dist/components/onboarding-guides/onboarding-guide-detail-view.d.ts.map +1 -1
- package/dist/components/related-content/index.cjs +3 -3
- package/dist/components/related-content/index.js +2 -2
- package/dist/components/shared/product-release/release-detail-page.d.ts.map +1 -1
- package/dist/components/tickets/index.cjs +6 -6
- package/dist/components/tickets/index.js +5 -5
- package/dist/components/ui/index.cjs +44 -2
- package/dist/components/ui/index.cjs.map +1 -1
- package/dist/components/ui/index.d.ts +1 -2
- package/dist/components/ui/index.d.ts.map +1 -1
- package/dist/components/ui/index.js +55 -13
- package/dist/components/ui/markdown/base-components.d.ts +51 -0
- package/dist/components/ui/markdown/base-components.d.ts.map +1 -0
- package/dist/components/ui/markdown/engine.d.ts +99 -0
- package/dist/components/ui/markdown/engine.d.ts.map +1 -0
- package/dist/components/ui/markdown/heading-ids.d.ts +65 -0
- package/dist/components/ui/markdown/heading-ids.d.ts.map +1 -0
- package/dist/components/ui/markdown/index.d.ts +18 -0
- package/dist/components/ui/markdown/index.d.ts.map +1 -0
- package/dist/components/ui/markdown/mermaid-diagram.d.ts +79 -0
- package/dist/components/ui/markdown/mermaid-diagram.d.ts.map +1 -0
- package/dist/components/ui/markdown/rich/embed-overrides.d.ts +14 -0
- package/dist/components/ui/markdown/rich/embed-overrides.d.ts.map +1 -0
- package/dist/components/ui/markdown/rich/rich-markdown-renderer.d.ts +40 -0
- package/dist/components/ui/markdown/rich/rich-markdown-renderer.d.ts.map +1 -0
- package/dist/components/ui/markdown/rich/shortcodes.d.ts +8 -0
- package/dist/components/ui/markdown/rich/shortcodes.d.ts.map +1 -0
- package/dist/components/ui/markdown/sanitize.d.ts +153 -0
- package/dist/components/ui/markdown/sanitize.d.ts.map +1 -0
- package/dist/components/ui/markdown/simple-markdown-renderer.d.ts +23 -0
- package/dist/components/ui/markdown/simple-markdown-renderer.d.ts.map +1 -0
- package/dist/components/ui/markdown/streaming.d.ts +78 -0
- package/dist/components/ui/markdown/streaming.d.ts.map +1 -0
- package/dist/components/ui/markdown/text-size.d.ts +34 -0
- package/dist/components/ui/markdown/text-size.d.ts.map +1 -0
- package/dist/index.cjs +60 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +71 -13
- package/dist/utils/index.cjs +169 -40
- package/dist/utils/index.cjs.map +1 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +162 -41
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/markdown-fences.d.ts +42 -0
- package/dist/utils/markdown-fences.d.ts.map +1 -0
- package/dist/utils/markdown-heading-id.d.ts +85 -0
- package/dist/utils/markdown-heading-id.d.ts.map +1 -0
- package/dist/utils/markdown-section-extractor.d.ts.map +1 -1
- package/package.json +7 -1
- package/src/chat-protocol/__tests__/__snapshots__/nats-decoder-golden.test.ts.snap +319 -0
- package/src/chat-protocol/__tests__/chat-protocol.test.ts +516 -0
- package/src/chat-protocol/__tests__/env-flag.test.ts +52 -0
- package/src/chat-protocol/__tests__/ip-normalize.test.ts +136 -0
- package/src/chat-protocol/__tests__/nats-decoder-golden.test.ts +237 -0
- package/src/chat-protocol/decode.ts +278 -0
- package/src/chat-protocol/encode.ts +71 -0
- package/src/chat-protocol/env-flag.ts +39 -0
- package/src/chat-protocol/events.ts +254 -0
- package/src/chat-protocol/frames.ts +245 -0
- package/src/chat-protocol/index.ts +21 -0
- package/src/chat-protocol/ip-normalize.ts +146 -0
- package/src/chat-protocol/nats-decoder.ts +264 -0
- package/src/components/chat/.chat-message-list.md +10 -6
- package/src/components/chat/__tests__/chat-message-list.test.tsx +450 -9
- package/src/components/chat/__tests__/chat-message-streaming-memo.test.tsx +207 -0
- package/src/components/chat/__tests__/chat-pending-turn.test.tsx +250 -0
- package/src/components/chat/chat-message-enhanced.tsx +122 -20
- package/src/components/chat/chat-message-list.tsx +515 -133
- package/src/components/chat/embeddable-chat.tsx +6 -0
- package/src/components/chat/hooks/.index.md +30 -33
- package/src/components/chat/hooks/.use-nats-chat-adapter.md +36 -55
- package/src/components/chat/hooks/__tests__/__snapshots__/conversation-id-persistence-golden.test.ts.snap +37 -0
- package/src/components/chat/hooks/__tests__/__snapshots__/sse-stream-golden.test.ts.snap +0 -0
- package/src/components/chat/hooks/__tests__/chunk-catchup-dialog-staleness.test.ts +92 -0
- package/src/components/chat/hooks/__tests__/conversation-id-persistence-golden.test.ts +438 -0
- package/src/components/chat/hooks/__tests__/sse-stream-golden.test.ts +318 -0
- package/src/components/chat/hooks/index.ts +0 -1
- package/src/components/chat/hooks/use-chat.ts +8 -0
- package/src/components/chat/hooks/use-chunk-catchup.ts +26 -1
- package/src/components/chat/hooks/use-nats-chat-adapter.ts +254 -825
- package/src/components/chat/hooks/use-sse-chat-adapter.ts +380 -684
- package/src/components/chat/index.ts +5 -0
- package/src/components/chat/stream/__tests__/__snapshots__/chat-stream-reducer-golden.test.ts.snap +944 -0
- package/src/components/chat/stream/__tests__/chat-dialog-store.test.ts +806 -0
- package/src/components/chat/stream/__tests__/chat-stream-reducer-golden.test.ts +467 -0
- package/src/components/chat/stream/__tests__/chat-stream-reducer.test.ts +1330 -0
- package/src/components/chat/stream/__tests__/delta-batcher.test.ts +255 -0
- package/src/components/chat/stream/__tests__/use-chat-stream-reducer.test.ts +105 -0
- package/src/components/chat/stream/chat-dialog-store.ts +536 -0
- package/src/components/chat/stream/chat-stream-reducer.ts +1928 -0
- package/src/components/chat/stream/delta-batcher.ts +168 -0
- package/src/components/chat/stream/index.ts +55 -0
- package/src/components/chat/stream/message-mutations.ts +340 -0
- package/src/components/chat/stream/use-chat-stream-reducer.ts +126 -0
- package/src/components/chat/thinking-display.tsx +1 -1
- package/src/components/chat/types/.api.types.md +39 -49
- package/src/components/chat/types/.processing.types.md +29 -58
- package/src/components/chat/types/.unified-chat-state.types.md +2 -2
- package/src/components/chat/types/api.types.ts +6 -73
- package/src/components/chat/types/processing.types.ts +11 -53
- package/src/components/chat/types/unified-chat-state.types.ts +16 -0
- package/src/components/chat/utils/.extract-incomplete-message-state.md +21 -3
- package/src/components/chat/utils/.history-merge.md +44 -3
- package/src/components/chat/utils/.index.md +31 -40
- package/src/components/chat/utils/__tests__/__snapshots__/process-historical-messages-golden.test.ts.snap +420 -0
- package/src/components/chat/utils/__tests__/__snapshots__/segment-accumulator-golden.test.ts.snap +605 -0
- package/src/components/chat/utils/__tests__/extract-incomplete-message-state.test.ts +106 -0
- package/src/components/chat/utils/__tests__/history-merge.test.ts +160 -0
- package/src/components/chat/utils/__tests__/process-historical-messages-approvals.test.ts +61 -0
- package/src/components/chat/utils/__tests__/process-historical-messages-golden.test.ts +394 -0
- package/src/components/chat/utils/__tests__/segment-accumulator-golden.test.ts +270 -0
- package/src/components/chat/utils/extract-incomplete-message-state.ts +93 -6
- package/src/components/chat/utils/history-merge.ts +85 -5
- package/src/components/chat/utils/index.ts +5 -8
- package/src/components/chat/utils/process-historical-messages.ts +361 -385
- package/src/components/onboarding-guides/onboarding-guide-detail-view.tsx +22 -4
- package/src/components/shared/legal-document/legal-document-page.tsx +1 -1
- package/src/components/shared/product-release/release-detail-page.tsx +19 -4
- package/src/components/ui/__tests__/__snapshots__/markdown-parity.test.tsx.snap +2420 -0
- package/src/components/ui/__tests__/markdown-parity.test.tsx +2540 -0
- package/src/components/ui/index.ts +1 -2
- package/src/components/ui/markdown/__tests__/mermaid-security.test.ts +176 -0
- package/src/components/ui/markdown/__tests__/mermaid-stale-render.test.tsx +151 -0
- package/src/components/ui/markdown/__tests__/sanitize-invariant.test.ts +354 -0
- package/src/components/ui/markdown/__tests__/sanitize-render.test.tsx +118 -0
- package/src/components/ui/markdown/__tests__/streaming.test.tsx +436 -0
- package/src/components/ui/markdown/base-components.tsx +360 -0
- package/src/components/ui/markdown/engine.tsx +315 -0
- package/src/components/ui/markdown/heading-ids.ts +239 -0
- package/src/components/ui/markdown/index.ts +49 -0
- package/src/components/ui/markdown/mermaid-diagram.tsx +323 -0
- package/src/components/ui/markdown/rich/embed-overrides.tsx +199 -0
- package/src/components/ui/markdown/rich/rich-markdown-renderer.tsx +184 -0
- package/src/components/ui/markdown/rich/shortcodes.ts +170 -0
- package/src/components/ui/markdown/sanitize.ts +3584 -0
- package/src/components/ui/markdown/simple-markdown-renderer.tsx +28 -0
- package/src/components/ui/markdown/streaming.ts +390 -0
- package/src/components/ui/markdown/text-size.ts +106 -0
- package/src/components/ui/release-changelog-section.tsx +1 -1
- package/src/components/ui/ticket-info-section.tsx +1 -1
- package/src/utils/index.ts +1 -0
- package/src/utils/markdown-fences.ts +127 -0
- package/src/utils/markdown-heading-id.ts +348 -0
- package/src/utils/markdown-section-extractor.ts +41 -56
- package/dist/chunk-OCEKO5CW.js.map +0 -1
- package/dist/chunk-QTYZMP6D.cjs.map +0 -1
- package/dist/chunk-TLMJHMXJ.js.map +0 -1
- package/dist/chunk-TWYNR4TZ.cjs.map +0 -1
- package/dist/chunk-WA7RR64F.js.map +0 -1
- package/dist/chunk-WK4N5VBX.cjs.map +0 -1
- package/dist/components/chat/hooks/use-realtime-chunk-processor.d.ts +0 -6
- package/dist/components/chat/hooks/use-realtime-chunk-processor.d.ts.map +0 -1
- package/dist/components/chat/utils/chunk-parser.d.ts +0 -25
- package/dist/components/chat/utils/chunk-parser.d.ts.map +0 -1
- package/dist/components/ui/rich-markdown-renderer.d.ts +0 -34
- package/dist/components/ui/rich-markdown-renderer.d.ts.map +0 -1
- package/dist/components/ui/simple-markdown-renderer.d.ts +0 -73
- package/dist/components/ui/simple-markdown-renderer.d.ts.map +0 -1
- package/src/components/chat/hooks/.use-realtime-chunk-processor.md +0 -77
- package/src/components/chat/hooks/use-realtime-chunk-processor.ts +0 -489
- package/src/components/chat/utils/.chunk-parser.md +0 -62
- package/src/components/chat/utils/chunk-parser.ts +0 -262
- package/src/components/ui/.simple-markdown-renderer.md +0 -52
- package/src/components/ui/rich-markdown-renderer.tsx +0 -1223
- package/src/components/ui/simple-markdown-renderer.tsx +0 -964
- /package/dist/{chunk-ADPMHWOE.js.map → chunk-72XAID7Y.js.map} +0 -0
- /package/dist/{chunk-PAGKRNWK.js.map → chunk-7WZHBQ4J.js.map} +0 -0
- /package/dist/{chunk-TSZHM74B.js.map → chunk-DC2TKS7C.js.map} +0 -0
- /package/dist/{chunk-I64ABCDX.js.map → chunk-I2B6X77L.js.map} +0 -0
- /package/dist/{chunk-DTYRYB2N.js.map → chunk-K6QTZ7CZ.js.map} +0 -0
- /package/dist/{chunk-4LTMXDUS.js.map → chunk-KBN7PMFT.js.map} +0 -0
- /package/dist/{chunk-TBMV7I5N.js.map → chunk-KXZO2WZX.js.map} +0 -0
- /package/dist/{chunk-KEBYLU3U.js.map → chunk-LFUPI7VX.js.map} +0 -0
- /package/dist/{chunk-WJQHJD7J.js.map → chunk-TWSKTJHW.js.map} +0 -0
- /package/dist/{chunk-TZRUCD56.js.map → chunk-V3ZAAFE3.js.map} +0 -0
- /package/dist/{chunk-LRGHJPET.js.map → chunk-XGIFOHKE.js.map} +0 -0
|
@@ -1,6 +1,28 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Utility for processing historical messages from GraphQL/API
|
|
3
|
-
*
|
|
2
|
+
* Utility for processing historical messages from GraphQL/API into
|
|
3
|
+
* display-ready format.
|
|
4
|
+
*
|
|
5
|
+
* Phase 3 of the chat unification reduced this file to DECODE + ENVELOPE:
|
|
6
|
+
*
|
|
7
|
+
* - `decodeHistoricalMessageData` maps one persisted `MessageData` item
|
|
8
|
+
* into the shared `ChatStreamEvent` vocabulary (historical items share
|
|
9
|
+
* the MESSAGE_TYPE vocabulary with NATS chunks but are NOT full chunk
|
|
10
|
+
* envelopes — e.g. compaction summaries ride in `summary`, not `text`,
|
|
11
|
+
* and APPROVAL_RESULT rows carry `resolvedByName` directly);
|
|
12
|
+
* - `applyHistoryEvent` replays each decoded event into the shared
|
|
13
|
+
* per-turn kernel (`MessageSegmentAccumulator` — the same kernel the
|
|
14
|
+
* master `createChatStreamReducer` instantiates) with the
|
|
15
|
+
* history-specific approval semantics (display-all default, pending
|
|
16
|
+
* tracking + `flushPendingApprovals`, `approvalStatuses` overrides);
|
|
17
|
+
* - the ENVELOPE (user/assistant flush-grouping, OWNER_TYPE author /
|
|
18
|
+
* display-name / avatar resolution, standalone SYSTEM handling,
|
|
19
|
+
* per-message contextItems mapping, per-turn streamSeq MAX) lives ONCE
|
|
20
|
+
* in `processHistory` — the previous near-duplicate second copy was
|
|
21
|
+
* deleted (`processHistoricalMessagesWithErrors` is an alias; the two
|
|
22
|
+
* snapshots were byte-identical).
|
|
23
|
+
*
|
|
24
|
+
* Adapters then feed the processed rows into the reducer via
|
|
25
|
+
* `initializeWithState` (see `useNatsChatAdapter.loadDialogHistory`).
|
|
4
26
|
*/
|
|
5
27
|
|
|
6
28
|
import {
|
|
@@ -15,6 +37,7 @@ import {
|
|
|
15
37
|
type MessageData,
|
|
16
38
|
type MessageOwner,
|
|
17
39
|
} from '../types'
|
|
40
|
+
import type { ChatStreamEvent } from '../../../chat-protocol/events'
|
|
18
41
|
import { MessageSegmentAccumulator, createMessageSegmentAccumulator } from './message-segment-accumulator'
|
|
19
42
|
import { getCommandText } from './tool-call-helpers'
|
|
20
43
|
|
|
@@ -53,255 +76,71 @@ function pushStandaloneMessages(
|
|
|
53
76
|
})
|
|
54
77
|
}
|
|
55
78
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
export interface ProcessHistoricalMessagesResult {
|
|
60
|
-
messages: ProcessedMessage[]
|
|
61
|
-
escalatedApprovals: Map<
|
|
62
|
-
string,
|
|
63
|
-
{ command: string; explanation?: string; approvalType: string; toolCalls?: PendingToolCallData[] }
|
|
64
|
-
>
|
|
65
|
-
}
|
|
79
|
+
// =============================================================================
|
|
80
|
+
// Decode — persisted MessageData item → shared ChatStreamEvent vocabulary
|
|
81
|
+
// =============================================================================
|
|
66
82
|
|
|
67
83
|
/**
|
|
68
|
-
*
|
|
84
|
+
* Map one persisted `MessageData` item to a normalized `ChatStreamEvent`.
|
|
85
|
+
* Thin shim over the NATS chunk decoder's core mapping — the differences
|
|
86
|
+
* are exactly the persisted-row divergences from the realtime envelope:
|
|
87
|
+
*
|
|
88
|
+
* - compaction summaries arrive in `summary` (realtime: `text`);
|
|
89
|
+
* - APPROVAL_RESULT rows carry `resolvedByName` (realtime chunks carry
|
|
90
|
+
* the resolver's name as `displayName`);
|
|
91
|
+
* - `approvalType` defaults to 'CLIENT' (legacy history parity;
|
|
92
|
+
* realtime defaults APPROVAL_REQUEST to 'USER');
|
|
93
|
+
* - batch `toolCalls` pass through verbatim (already the persisted
|
|
94
|
+
* `PendingToolCallData` shape — no normalization pass).
|
|
95
|
+
*
|
|
96
|
+
* Returns `null` for rows the assistant-turn path doesn't decode (SYSTEM
|
|
97
|
+
* is handled by the envelope's standalone path; user TEXT rows are handled
|
|
98
|
+
* by the envelope's user path).
|
|
69
99
|
*/
|
|
70
|
-
export function
|
|
71
|
-
messages: HistoricalMessage[],
|
|
72
|
-
options: MessageProcessingOptions = {}
|
|
73
|
-
): ProcessHistoricalMessagesResult {
|
|
74
|
-
const {
|
|
75
|
-
assistantName = 'Fae',
|
|
76
|
-
assistantType = 'fae',
|
|
77
|
-
assistantAvatar,
|
|
78
|
-
onApprove,
|
|
79
|
-
onReject,
|
|
80
|
-
chatTypeFilter,
|
|
81
|
-
approvalStatuses = {},
|
|
82
|
-
// An omitted option means "display every approval type" — the original
|
|
83
|
-
// history semantics. Deliberately NOT defaulted to the realtime
|
|
84
|
-
// processor's ['CLIENT']: consumers that pass a wider list to their
|
|
85
|
-
// realtime processor but omit it on the history path would silently lose
|
|
86
|
-
// pending non-CLIENT approval cards on every reload/reconnect refetch
|
|
87
|
-
// (they also ignore `escalatedApprovals`). Realtime/history parity is
|
|
88
|
-
// opt-in: pass the same explicit list to both.
|
|
89
|
-
displayApprovalTypes,
|
|
90
|
-
batchApprovalsEnabled,
|
|
91
|
-
} = options
|
|
92
|
-
|
|
93
|
-
const processedMessages: ProcessedMessage[] = []
|
|
94
|
-
const accumulator = createMessageSegmentAccumulator({ onApprove, onReject })
|
|
95
|
-
const escalatedApprovals = new Map<
|
|
96
|
-
string,
|
|
97
|
-
{ command: string; explanation?: string; approvalType: string; toolCalls?: PendingToolCallData[] }
|
|
98
|
-
>()
|
|
99
|
-
|
|
100
|
-
let currentAssistantId: string | null = null
|
|
101
|
-
let currentAssistantTimestamp: Date | null = null
|
|
102
|
-
let lastAssistantId: string | null = null
|
|
103
|
-
// MAX persisted seq across the rows grouped into the current assistant turn
|
|
104
|
-
// — carried onto the flushed message's streamSeq for per-role merge coverage.
|
|
105
|
-
let currentAssistantStreamSeq: number | undefined
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Flush the current assistant message to processedMessages.
|
|
109
|
-
* Uses the LAST message ID in the group for stable React keys across page boundaries.
|
|
110
|
-
*/
|
|
111
|
-
const flushAssistantMessage = () => {
|
|
112
|
-
const idToUse = lastAssistantId || currentAssistantId
|
|
113
|
-
if (idToUse && accumulator.hasContent()) {
|
|
114
|
-
processedMessages.push({
|
|
115
|
-
id: idToUse,
|
|
116
|
-
role: 'assistant',
|
|
117
|
-
content: accumulator.getSegments(),
|
|
118
|
-
name: assistantName,
|
|
119
|
-
assistantType,
|
|
120
|
-
authorType: assistantType as AuthorType,
|
|
121
|
-
timestamp: currentAssistantTimestamp || new Date(),
|
|
122
|
-
avatar: assistantAvatar,
|
|
123
|
-
...(currentAssistantStreamSeq !== undefined ? { streamSeq: currentAssistantStreamSeq } : {}),
|
|
124
|
-
})
|
|
125
|
-
accumulator.resetSegments()
|
|
126
|
-
}
|
|
127
|
-
// Reset grouping identity + seq UNCONDITIONALLY — even on an EMPTY flush (an
|
|
128
|
-
// assistant turn whose only data was a filtered/escalated approval renders
|
|
129
|
-
// nothing, so `hasContent()` is false and nothing is pushed). Left inside
|
|
130
|
-
// the push-block, a stale id/timestamp and (worse) a stale
|
|
131
|
-
// `currentAssistantStreamSeq` bleed into the NEXT assistant turn: `!currentAssistantId`
|
|
132
|
-
// stays false so it keeps the old id/timestamp, and `Math.max` inflates its
|
|
133
|
-
// streamSeq — which then over-covers synthetics in the history merge.
|
|
134
|
-
currentAssistantId = null
|
|
135
|
-
currentAssistantTimestamp = null
|
|
136
|
-
lastAssistantId = null
|
|
137
|
-
currentAssistantStreamSeq = undefined
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
messages.forEach((msg, index) => {
|
|
141
|
-
// Filter by chat type if specified
|
|
142
|
-
if (chatTypeFilter && msg.chatType !== chatTypeFilter) return
|
|
143
|
-
|
|
144
|
-
const messageDataArray = Array.isArray(msg.messageData)
|
|
145
|
-
? msg.messageData
|
|
146
|
-
: msg.messageData
|
|
147
|
-
? [msg.messageData]
|
|
148
|
-
: []
|
|
149
|
-
|
|
150
|
-
const hasStandaloneData = messageDataArray.some((data) =>
|
|
151
|
-
data.type === MESSAGE_TYPE.SYSTEM
|
|
152
|
-
)
|
|
153
|
-
if (hasStandaloneData) {
|
|
154
|
-
flushAssistantMessage()
|
|
155
|
-
pushStandaloneMessages(processedMessages, msg, messageDataArray)
|
|
156
|
-
return
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
const isUserMessage =
|
|
160
|
-
msg.owner?.type === OWNER_TYPE.CLIENT || msg.owner?.type === OWNER_TYPE.ADMIN
|
|
161
|
-
|
|
162
|
-
if (isUserMessage) {
|
|
163
|
-
flushAssistantMessage()
|
|
164
|
-
|
|
165
|
-
const userAuthorType: AuthorType = msg.owner?.type === OWNER_TYPE.ADMIN ? 'admin' : 'user'
|
|
166
|
-
messageDataArray.forEach((data) => {
|
|
167
|
-
if (data.type === MESSAGE_TYPE.TEXT && 'text' in data && data.text) {
|
|
168
|
-
// `TextData.contextItems` (server: `[{ type, id }]`) — the entity
|
|
169
|
-
// context the user attached to this message. Surface it so the bubble
|
|
170
|
-
// renders its chip strip from history (no label on the wire → fall
|
|
171
|
-
// back to the id, matching the realtime path).
|
|
172
|
-
const rawContext = (data as { contextItems?: Array<{ type?: unknown; id?: unknown }> }).contextItems
|
|
173
|
-
const contextItems = Array.isArray(rawContext)
|
|
174
|
-
? rawContext
|
|
175
|
-
.filter((c) => typeof c?.type === 'string' && typeof c?.id === 'string')
|
|
176
|
-
.map((c) => ({ type: c.type as string, id: c.id as string, label: c.id as string }))
|
|
177
|
-
: undefined
|
|
178
|
-
processedMessages.push({
|
|
179
|
-
id: msg.id,
|
|
180
|
-
role: 'user',
|
|
181
|
-
content: data.text,
|
|
182
|
-
name: getOwnerDisplayName(msg.owner),
|
|
183
|
-
avatar: getOwnerAvatar(msg.owner),
|
|
184
|
-
authorType: userAuthorType,
|
|
185
|
-
timestamp: new Date(msg.createdAt),
|
|
186
|
-
...(contextItems && contextItems.length > 0 ? { contextItems } : {}),
|
|
187
|
-
...(typeof msg.lastChunkStreamSeq === 'number' ? { streamSeq: msg.lastChunkStreamSeq } : {}),
|
|
188
|
-
})
|
|
189
|
-
}
|
|
190
|
-
})
|
|
191
|
-
} else {
|
|
192
|
-
if (!currentAssistantId) {
|
|
193
|
-
currentAssistantId = msg.id
|
|
194
|
-
currentAssistantTimestamp = new Date(msg.createdAt)
|
|
195
|
-
}
|
|
196
|
-
lastAssistantId = msg.id
|
|
197
|
-
if (typeof msg.lastChunkStreamSeq === 'number') {
|
|
198
|
-
currentAssistantStreamSeq =
|
|
199
|
-
currentAssistantStreamSeq === undefined
|
|
200
|
-
? msg.lastChunkStreamSeq
|
|
201
|
-
: Math.max(currentAssistantStreamSeq, msg.lastChunkStreamSeq)
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
messageDataArray.forEach((data) => {
|
|
205
|
-
processMessageData(
|
|
206
|
-
data,
|
|
207
|
-
accumulator,
|
|
208
|
-
approvalStatuses,
|
|
209
|
-
{ displayApprovalTypes, batchApprovalsEnabled },
|
|
210
|
-
escalatedApprovals,
|
|
211
|
-
)
|
|
212
|
-
})
|
|
213
|
-
|
|
214
|
-
// Check if we should flush (next message is from user or last message)
|
|
215
|
-
const nextMsg = messages[index + 1]
|
|
216
|
-
const isLastMessage = index === messages.length - 1
|
|
217
|
-
const nextIsFromUser =
|
|
218
|
-
nextMsg &&
|
|
219
|
-
(nextMsg.owner?.type === OWNER_TYPE.CLIENT || nextMsg.owner?.type === OWNER_TYPE.ADMIN)
|
|
220
|
-
|
|
221
|
-
if (isLastMessage || nextIsFromUser) {
|
|
222
|
-
flushAssistantMessage()
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
})
|
|
226
|
-
|
|
227
|
-
flushAssistantMessage()
|
|
228
|
-
|
|
229
|
-
const pendingApprovalSegments = accumulator.flushPendingApprovals()
|
|
230
|
-
if (pendingApprovalSegments.length > 0) {
|
|
231
|
-
processedMessages.push({
|
|
232
|
-
id: `pending-approvals-${Date.now()}`,
|
|
233
|
-
role: 'assistant',
|
|
234
|
-
content: pendingApprovalSegments,
|
|
235
|
-
name: assistantName,
|
|
236
|
-
assistantType,
|
|
237
|
-
timestamp: new Date(),
|
|
238
|
-
avatar: assistantAvatar,
|
|
239
|
-
})
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
return {
|
|
243
|
-
messages: processedMessages,
|
|
244
|
-
escalatedApprovals: escalatedApprovals
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* Process a single message data item into segments
|
|
250
|
-
*/
|
|
251
|
-
function processMessageData(
|
|
252
|
-
data: MessageData,
|
|
253
|
-
accumulator: MessageSegmentAccumulator,
|
|
254
|
-
approvalStatuses: Record<string, string>,
|
|
255
|
-
options: MessageProcessingOptions = {},
|
|
256
|
-
escalatedApprovals?: Map<
|
|
257
|
-
string,
|
|
258
|
-
{ command: string; explanation?: string; approvalType: string; toolCalls?: PendingToolCallData[] }
|
|
259
|
-
>
|
|
260
|
-
): void {
|
|
261
|
-
// batchApprovalsEnabled is owned by the consumer (oss-tenant chat client /
|
|
262
|
-
// openframe-frontend tickets). Defaults to ON so consumers that haven't
|
|
263
|
-
// wired the flag yet get the batch UI; pass `false` to force legacy.
|
|
264
|
-
const { displayApprovalTypes, batchApprovalsEnabled = true } = options
|
|
100
|
+
export function decodeHistoricalMessageData(data: MessageData): ChatStreamEvent | null {
|
|
265
101
|
switch (data.type) {
|
|
266
102
|
case MESSAGE_TYPE.TEXT:
|
|
267
103
|
if ('text' in data && data.text) {
|
|
268
|
-
|
|
104
|
+
return { type: 'text-delta', text: data.text }
|
|
269
105
|
}
|
|
270
|
-
|
|
106
|
+
return null
|
|
271
107
|
|
|
272
108
|
case MESSAGE_TYPE.THINKING:
|
|
273
109
|
if ('text' in data && data.text) {
|
|
274
|
-
|
|
110
|
+
return { type: 'thinking-delta', text: data.text }
|
|
275
111
|
}
|
|
276
|
-
|
|
112
|
+
return null
|
|
277
113
|
|
|
278
114
|
case MESSAGE_TYPE.GUIDE:
|
|
279
115
|
if ('text' in data && data.text) {
|
|
280
|
-
|
|
116
|
+
return { type: 'guide-delta', text: data.text }
|
|
281
117
|
}
|
|
282
|
-
|
|
118
|
+
return null
|
|
283
119
|
|
|
284
120
|
case MESSAGE_TYPE.EXECUTING_TOOL:
|
|
285
121
|
if ('integratedToolType' in data) {
|
|
286
|
-
|
|
287
|
-
type: '
|
|
122
|
+
return {
|
|
123
|
+
type: 'tool-execution',
|
|
288
124
|
data: {
|
|
289
125
|
type: 'EXECUTING_TOOL',
|
|
290
126
|
integratedToolType: data.integratedToolType || '',
|
|
291
127
|
toolFunction: data.toolFunction || '',
|
|
292
128
|
toolTitle: typeof data.title === 'string' ? data.title : undefined,
|
|
129
|
+
// Same contract as the NATS decoder: the explanation line rides the
|
|
130
|
+
// EXECUTING chunk only, so a history replay that drops it renders
|
|
131
|
+
// an empty tool card even though the live stream showed one.
|
|
293
132
|
toolExplanation: typeof data.toolExplanation === 'string' ? data.toolExplanation : undefined,
|
|
294
133
|
parameters: data.parameters,
|
|
295
134
|
toolExecutionRequestId: data.toolExecutionRequestId,
|
|
296
135
|
},
|
|
297
|
-
}
|
|
136
|
+
}
|
|
298
137
|
}
|
|
299
|
-
|
|
138
|
+
return null
|
|
300
139
|
|
|
301
140
|
case MESSAGE_TYPE.EXECUTED_TOOL:
|
|
302
141
|
if ('integratedToolType' in data) {
|
|
303
|
-
|
|
304
|
-
type: '
|
|
142
|
+
return {
|
|
143
|
+
type: 'tool-execution',
|
|
305
144
|
data: {
|
|
306
145
|
type: 'EXECUTED_TOOL',
|
|
307
146
|
integratedToolType: data.integratedToolType || '',
|
|
@@ -312,218 +151,298 @@ function processMessageData(
|
|
|
312
151
|
success: data.success,
|
|
313
152
|
toolExecutionRequestId: data.toolExecutionRequestId,
|
|
314
153
|
},
|
|
315
|
-
}
|
|
154
|
+
}
|
|
316
155
|
}
|
|
317
|
-
|
|
156
|
+
return null
|
|
318
157
|
|
|
319
158
|
case MESSAGE_TYPE.APPROVAL_REQUEST:
|
|
320
159
|
if ('approvalRequestId' in data && data.approvalRequestId) {
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
:
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
if (isBatch) {
|
|
329
|
-
const status = (approvalStatuses[data.approvalRequestId] as ChatApprovalStatus) || 'pending'
|
|
330
|
-
if (batchApprovalsEnabled) {
|
|
331
|
-
accumulator.addApprovalBatch(data.approvalRequestId, approvalType, toolCalls!, status)
|
|
332
|
-
} else {
|
|
333
|
-
// Flag OFF — unfold batch into N legacy approval cards (same id).
|
|
334
|
-
for (const call of toolCalls!) {
|
|
335
|
-
if (!call.requiresApproval) continue
|
|
336
|
-
accumulator.addApprovalRequest(
|
|
337
|
-
data.approvalRequestId,
|
|
338
|
-
getCommandText(call),
|
|
339
|
-
call.toolExplanation,
|
|
340
|
-
approvalType,
|
|
341
|
-
status,
|
|
342
|
-
)
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
} else {
|
|
346
|
-
// The resolution may already be known to the consumer (realtime
|
|
347
|
-
// flipped it and fed it back via `approvalStatuses`) while the
|
|
348
|
-
// matching APPROVAL_RESULT row is absent from the fetched history
|
|
349
|
-
// pages. Without this, the single-approval path tracks it as
|
|
350
|
-
// pending and `flushPendingApprovals()` resurrects it as a stale
|
|
351
|
-
// sticky card on every history re-process. Mirror the batch path
|
|
352
|
-
// and honor `approvalStatuses`.
|
|
353
|
-
const resolvedStatus = approvalStatuses[data.approvalRequestId] as ChatApprovalStatus | undefined
|
|
354
|
-
if (resolvedStatus === 'approved' || resolvedStatus === 'rejected') {
|
|
355
|
-
accumulator.addApprovalRequest(
|
|
356
|
-
data.approvalRequestId,
|
|
357
|
-
data.command || '',
|
|
358
|
-
data.explanation,
|
|
359
|
-
approvalType,
|
|
360
|
-
resolvedStatus,
|
|
361
|
-
)
|
|
362
|
-
} else {
|
|
363
|
-
accumulator.trackApprovalRequest(data.approvalRequestId, {
|
|
364
|
-
command: data.command || '',
|
|
365
|
-
explanation: data.explanation,
|
|
366
|
-
approvalType,
|
|
367
|
-
})
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
} else {
|
|
371
|
-
escalatedApprovals?.set(data.approvalRequestId, {
|
|
372
|
-
command: data.command || '',
|
|
373
|
-
explanation: data.explanation,
|
|
374
|
-
approvalType,
|
|
375
|
-
...(isBatch ? { toolCalls } : {}),
|
|
376
|
-
})
|
|
160
|
+
return {
|
|
161
|
+
type: 'approval-request',
|
|
162
|
+
requestId: data.approvalRequestId,
|
|
163
|
+
approvalType: data.approvalType || 'CLIENT',
|
|
164
|
+
command: data.command || '',
|
|
165
|
+
explanation: data.explanation,
|
|
166
|
+
...(Array.isArray(data.toolCalls) ? { toolCalls: data.toolCalls } : {}),
|
|
377
167
|
}
|
|
378
168
|
}
|
|
379
|
-
|
|
169
|
+
return null
|
|
380
170
|
|
|
381
171
|
case MESSAGE_TYPE.APPROVAL_RESULT:
|
|
382
172
|
if ('approvalRequestId' in data && data.approvalRequestId) {
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
173
|
+
return {
|
|
174
|
+
type: 'approval-resolved',
|
|
175
|
+
requestId: data.approvalRequestId,
|
|
176
|
+
status: data.approved ? 'approved' : 'rejected',
|
|
177
|
+
approvalType: data.approvalType,
|
|
178
|
+
resolvedByName: 'resolvedByName' in data ? data.resolvedByName : undefined,
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return null
|
|
182
|
+
|
|
183
|
+
case MESSAGE_TYPE.ERROR:
|
|
184
|
+
if ('error' in data) {
|
|
185
|
+
return {
|
|
186
|
+
type: 'error',
|
|
187
|
+
title: data.error || 'An error occurred',
|
|
188
|
+
details: 'details' in data ? data.details : undefined,
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return null
|
|
192
|
+
|
|
193
|
+
case MESSAGE_TYPE.CONTEXT_COMPACTION_START:
|
|
194
|
+
return { type: 'compaction', phase: 'start' }
|
|
195
|
+
|
|
196
|
+
case MESSAGE_TYPE.CONTEXT_COMPACTION_END:
|
|
197
|
+
return {
|
|
198
|
+
type: 'compaction',
|
|
199
|
+
phase: 'end',
|
|
200
|
+
summary: 'summary' in data && typeof data.summary === 'string' ? data.summary : undefined,
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
case MESSAGE_TYPE.SYSTEM:
|
|
204
|
+
if ('text' in data && data.text) {
|
|
205
|
+
return { type: 'participant', kind: 'system', text: data.text }
|
|
206
|
+
}
|
|
207
|
+
return null
|
|
208
|
+
|
|
209
|
+
default:
|
|
210
|
+
// Unknown message type — ignore.
|
|
211
|
+
return null
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// =============================================================================
|
|
216
|
+
// Replay — decoded event → per-turn kernel (history approval semantics)
|
|
217
|
+
// =============================================================================
|
|
218
|
+
|
|
219
|
+
type EscalatedApprovals = Map<
|
|
220
|
+
string,
|
|
221
|
+
{ command: string; explanation?: string; approvalType: string; toolCalls?: PendingToolCallData[] }
|
|
222
|
+
>
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Replay one decoded event into the shared per-turn segment kernel with the
|
|
226
|
+
* HISTORY approval semantics (an omitted `displayApprovalTypes` means
|
|
227
|
+
* "display every approval type" — the original history behavior; the
|
|
228
|
+
* realtime reducer defaults to `['CLIENT']`).
|
|
229
|
+
*/
|
|
230
|
+
function applyHistoryEvent(
|
|
231
|
+
event: ChatStreamEvent,
|
|
232
|
+
accumulator: MessageSegmentAccumulator,
|
|
233
|
+
approvalStatuses: Record<string, string>,
|
|
234
|
+
options: MessageProcessingOptions,
|
|
235
|
+
escalatedApprovals?: EscalatedApprovals,
|
|
236
|
+
): void {
|
|
237
|
+
// batchApprovalsEnabled is owned by the consumer (oss-tenant chat client /
|
|
238
|
+
// openframe-frontend tickets). Defaults to ON so consumers that haven't
|
|
239
|
+
// wired the flag yet get the batch UI; pass `false` to force legacy.
|
|
240
|
+
const { displayApprovalTypes, batchApprovalsEnabled = true } = options
|
|
241
|
+
|
|
242
|
+
switch (event.type) {
|
|
243
|
+
case 'text-delta':
|
|
244
|
+
accumulator.appendText(event.text)
|
|
245
|
+
break
|
|
246
|
+
|
|
247
|
+
case 'thinking-delta':
|
|
248
|
+
accumulator.appendThinking(event.text)
|
|
249
|
+
break
|
|
250
|
+
|
|
251
|
+
case 'guide-delta':
|
|
252
|
+
accumulator.appendGuide(event.text)
|
|
253
|
+
break
|
|
254
|
+
|
|
255
|
+
case 'tool-execution':
|
|
256
|
+
accumulator.addToolExecution({ type: 'tool_execution', data: event.data })
|
|
257
|
+
break
|
|
387
258
|
|
|
388
|
-
|
|
259
|
+
case 'approval-request': {
|
|
260
|
+
const approvalType = event.approvalType || 'CLIENT'
|
|
261
|
+
const toolCalls = event.toolCalls as PendingToolCallData[] | undefined
|
|
262
|
+
const isBatch = !!toolCalls && toolCalls.length > 0
|
|
263
|
+
|
|
264
|
+
if (!displayApprovalTypes || displayApprovalTypes.includes(approvalType)) {
|
|
265
|
+
if (isBatch) {
|
|
266
|
+
const status = (approvalStatuses[event.requestId] as ChatApprovalStatus) || 'pending'
|
|
389
267
|
if (batchApprovalsEnabled) {
|
|
390
|
-
accumulator.addApprovalBatch(
|
|
391
|
-
data.approvalRequestId,
|
|
392
|
-
escalatedData.approvalType,
|
|
393
|
-
escalatedData.toolCalls,
|
|
394
|
-
status,
|
|
395
|
-
undefined,
|
|
396
|
-
resolvedByName,
|
|
397
|
-
)
|
|
268
|
+
accumulator.addApprovalBatch(event.requestId, approvalType, toolCalls!, status)
|
|
398
269
|
} else {
|
|
399
|
-
|
|
270
|
+
// Flag OFF — unfold batch into N legacy approval cards (same id).
|
|
271
|
+
for (const call of toolCalls!) {
|
|
400
272
|
if (!call.requiresApproval) continue
|
|
401
273
|
accumulator.addApprovalRequest(
|
|
402
|
-
|
|
274
|
+
event.requestId,
|
|
403
275
|
getCommandText(call),
|
|
404
276
|
call.toolExplanation,
|
|
405
|
-
|
|
277
|
+
approvalType,
|
|
406
278
|
status,
|
|
407
279
|
)
|
|
408
280
|
}
|
|
409
281
|
}
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
282
|
+
} else {
|
|
283
|
+
// The resolution may already be known to the consumer (realtime
|
|
284
|
+
// flipped it and fed it back via `approvalStatuses`) while the
|
|
285
|
+
// matching APPROVAL_RESULT row is absent from the fetched history
|
|
286
|
+
// pages. Without this, the single-approval path tracks it as
|
|
287
|
+
// pending and `flushPendingApprovals()` resurrects it as a stale
|
|
288
|
+
// sticky card on every history re-process. Mirror the batch path
|
|
289
|
+
// and honor `approvalStatuses`.
|
|
290
|
+
const resolvedStatus = approvalStatuses[event.requestId] as ChatApprovalStatus | undefined
|
|
291
|
+
if (resolvedStatus === 'approved' || resolvedStatus === 'rejected') {
|
|
292
|
+
accumulator.addApprovalRequest(
|
|
293
|
+
event.requestId,
|
|
294
|
+
event.command || '',
|
|
295
|
+
event.explanation,
|
|
296
|
+
approvalType,
|
|
297
|
+
resolvedStatus,
|
|
298
|
+
)
|
|
299
|
+
} else {
|
|
300
|
+
accumulator.trackApprovalRequest(event.requestId, {
|
|
301
|
+
command: event.command || '',
|
|
302
|
+
explanation: event.explanation,
|
|
303
|
+
approvalType,
|
|
304
|
+
})
|
|
305
|
+
}
|
|
421
306
|
}
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
accumulator.processApprovalResult(
|
|
431
|
-
data.approvalRequestId,
|
|
432
|
-
status === 'approved',
|
|
433
|
-
data.approvalType || 'USER'
|
|
434
|
-
)
|
|
307
|
+
} else {
|
|
308
|
+
escalatedApprovals?.set(event.requestId, {
|
|
309
|
+
command: event.command || '',
|
|
310
|
+
explanation: event.explanation,
|
|
311
|
+
approvalType,
|
|
312
|
+
...(isBatch ? { toolCalls } : {}),
|
|
313
|
+
})
|
|
435
314
|
}
|
|
436
315
|
break
|
|
316
|
+
}
|
|
437
317
|
|
|
438
|
-
case
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
318
|
+
case 'approval-resolved': {
|
|
319
|
+
const requestId = event.requestId
|
|
320
|
+
if (!requestId) break
|
|
321
|
+
const existingStatus = approvalStatuses[requestId] as ChatApprovalStatus | undefined
|
|
322
|
+
const status: ChatApprovalStatus = existingStatus || event.status
|
|
323
|
+
const resolvedByName = event.resolvedByName
|
|
324
|
+
const escalatedData = escalatedApprovals?.get(requestId)
|
|
325
|
+
|
|
326
|
+
if (escalatedData?.toolCalls && escalatedData.toolCalls.length > 0) {
|
|
327
|
+
if (batchApprovalsEnabled) {
|
|
328
|
+
accumulator.addApprovalBatch(
|
|
329
|
+
requestId,
|
|
330
|
+
escalatedData.approvalType,
|
|
331
|
+
escalatedData.toolCalls,
|
|
332
|
+
status,
|
|
333
|
+
undefined,
|
|
334
|
+
resolvedByName,
|
|
335
|
+
)
|
|
336
|
+
} else {
|
|
337
|
+
for (const call of escalatedData.toolCalls) {
|
|
338
|
+
if (!call.requiresApproval) continue
|
|
339
|
+
accumulator.addApprovalRequest(
|
|
340
|
+
requestId,
|
|
341
|
+
getCommandText(call),
|
|
342
|
+
call.toolExplanation,
|
|
343
|
+
escalatedData.approvalType,
|
|
344
|
+
status,
|
|
345
|
+
)
|
|
446
346
|
}
|
|
447
347
|
}
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
message
|
|
451
|
-
)
|
|
348
|
+
escalatedApprovals?.delete(requestId)
|
|
349
|
+
break
|
|
452
350
|
}
|
|
453
|
-
break
|
|
454
351
|
|
|
455
|
-
|
|
456
|
-
|
|
352
|
+
if (escalatedData) {
|
|
353
|
+
accumulator.trackApprovalRequest(requestId, {
|
|
354
|
+
command: escalatedData.command,
|
|
355
|
+
explanation: escalatedData.explanation,
|
|
356
|
+
approvalType: escalatedData.approvalType,
|
|
357
|
+
})
|
|
358
|
+
escalatedApprovals?.delete(requestId)
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// If a segment with this id is already present (batch or legacy), just
|
|
362
|
+
// flip its status. updateApprovalStatus matches both `approval_batch`
|
|
363
|
+
// and `approval_request` segments.
|
|
364
|
+
const before = accumulator.getSegments()
|
|
365
|
+
const after = accumulator.updateApprovalStatus(requestId, status, resolvedByName)
|
|
366
|
+
const updatedExisting = before.some((s, i) => after[i] !== s)
|
|
367
|
+
if (updatedExisting) break
|
|
368
|
+
|
|
369
|
+
accumulator.processApprovalResult(
|
|
370
|
+
requestId,
|
|
371
|
+
status === 'approved',
|
|
372
|
+
event.approvalType || 'USER',
|
|
373
|
+
)
|
|
457
374
|
break
|
|
375
|
+
}
|
|
458
376
|
|
|
459
|
-
case
|
|
460
|
-
|
|
461
|
-
|
|
377
|
+
case 'error': {
|
|
378
|
+
let message: string | undefined
|
|
379
|
+
if (event.details) {
|
|
380
|
+
try {
|
|
381
|
+
message = JSON.parse(event.details)?.error?.message
|
|
382
|
+
} catch {
|
|
383
|
+
message = event.details
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
accumulator.addError(event.title, message)
|
|
462
387
|
break
|
|
463
388
|
}
|
|
464
389
|
|
|
390
|
+
case 'compaction':
|
|
391
|
+
if (event.phase === 'start') {
|
|
392
|
+
accumulator.addContextCompaction()
|
|
393
|
+
} else {
|
|
394
|
+
accumulator.completeContextCompaction(event.summary)
|
|
395
|
+
}
|
|
396
|
+
break
|
|
397
|
+
|
|
465
398
|
default:
|
|
466
|
-
//
|
|
399
|
+
// Participant/system rows are envelope concerns; everything else is
|
|
400
|
+
// realtime-only vocabulary that never appears in persisted rows.
|
|
467
401
|
break
|
|
468
402
|
}
|
|
469
403
|
}
|
|
470
404
|
|
|
405
|
+
// =============================================================================
|
|
406
|
+
// Envelope — ONE implementation (previous duplicate deleted)
|
|
407
|
+
// =============================================================================
|
|
408
|
+
|
|
471
409
|
/**
|
|
472
|
-
*
|
|
473
|
-
* Returns a separate array of error messages that should be displayed
|
|
410
|
+
* Result type for historical message processing
|
|
474
411
|
*/
|
|
475
|
-
export
|
|
476
|
-
messages:
|
|
477
|
-
|
|
478
|
-
): ProcessedMessage[] {
|
|
479
|
-
const { assistantName = 'Fae', assistantType = 'fae', assistantAvatar, chatTypeFilter } = options
|
|
480
|
-
|
|
481
|
-
const errorMessages: ProcessedMessage[] = []
|
|
482
|
-
|
|
483
|
-
messages.forEach((msg) => {
|
|
484
|
-
if (chatTypeFilter && msg.chatType !== chatTypeFilter) return
|
|
485
|
-
|
|
486
|
-
const messageDataArray = Array.isArray(msg.messageData)
|
|
487
|
-
? msg.messageData
|
|
488
|
-
: msg.messageData
|
|
489
|
-
? [msg.messageData]
|
|
490
|
-
: []
|
|
491
|
-
|
|
492
|
-
messageDataArray.forEach((data) => {
|
|
493
|
-
if (data.type === MESSAGE_TYPE.ERROR) {
|
|
494
|
-
errorMessages.push({
|
|
495
|
-
id: `${msg.id}-error`,
|
|
496
|
-
role: 'error',
|
|
497
|
-
content: 'error' in data && data.error ? data.error : 'An error occurred',
|
|
498
|
-
name: assistantName,
|
|
499
|
-
assistantType,
|
|
500
|
-
timestamp: new Date(msg.createdAt),
|
|
501
|
-
avatar: assistantAvatar,
|
|
502
|
-
})
|
|
503
|
-
}
|
|
504
|
-
})
|
|
505
|
-
})
|
|
506
|
-
|
|
507
|
-
return errorMessages
|
|
412
|
+
export interface ProcessHistoricalMessagesResult {
|
|
413
|
+
messages: ProcessedMessage[]
|
|
414
|
+
escalatedApprovals: EscalatedApprovals
|
|
508
415
|
}
|
|
509
416
|
|
|
510
417
|
/**
|
|
511
|
-
* Process
|
|
418
|
+
* Process an array of historical messages into display-ready format
|
|
512
419
|
*/
|
|
513
|
-
export function
|
|
420
|
+
export function processHistoricalMessages(
|
|
514
421
|
messages: HistoricalMessage[],
|
|
515
|
-
options: MessageProcessingOptions = {}
|
|
422
|
+
options: MessageProcessingOptions = {},
|
|
516
423
|
): ProcessHistoricalMessagesResult {
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
424
|
+
const {
|
|
425
|
+
assistantName = 'Fae',
|
|
426
|
+
assistantType = 'fae',
|
|
427
|
+
assistantAvatar,
|
|
428
|
+
onApprove,
|
|
429
|
+
onReject,
|
|
430
|
+
chatTypeFilter,
|
|
431
|
+
approvalStatuses = {},
|
|
432
|
+
// An omitted option means "display every approval type" — the original
|
|
433
|
+
// history semantics. Deliberately NOT defaulted to the realtime
|
|
434
|
+
// reducer's ['CLIENT']: consumers that pass a wider list to their
|
|
435
|
+
// realtime path but omit it on the history path would silently lose
|
|
436
|
+
// pending non-CLIENT approval cards on every reload/reconnect refetch
|
|
437
|
+
// (they also ignore `escalatedApprovals`). Realtime/history parity is
|
|
438
|
+
// opt-in: pass the same explicit list to both.
|
|
439
|
+
displayApprovalTypes,
|
|
440
|
+
batchApprovalsEnabled,
|
|
441
|
+
} = options
|
|
520
442
|
|
|
521
443
|
const processedMessages: ProcessedMessage[] = []
|
|
522
444
|
const accumulator = createMessageSegmentAccumulator({ onApprove, onReject })
|
|
523
|
-
const escalatedApprovals = new Map
|
|
524
|
-
string,
|
|
525
|
-
{ command: string; explanation?: string; approvalType: string; toolCalls?: PendingToolCallData[] }
|
|
526
|
-
>()
|
|
445
|
+
const escalatedApprovals: EscalatedApprovals = new Map()
|
|
527
446
|
|
|
528
447
|
let currentAssistantId: string | null = null
|
|
529
448
|
let currentAssistantTimestamp: Date | null = null
|
|
@@ -532,6 +451,10 @@ export function processHistoricalMessagesWithErrors(
|
|
|
532
451
|
// — carried onto the flushed message's streamSeq for per-role merge coverage.
|
|
533
452
|
let currentAssistantStreamSeq: number | undefined
|
|
534
453
|
|
|
454
|
+
/**
|
|
455
|
+
* Flush the current assistant message to processedMessages.
|
|
456
|
+
* Uses the LAST message ID in the group for stable React keys across page boundaries.
|
|
457
|
+
*/
|
|
535
458
|
const flushAssistantMessage = () => {
|
|
536
459
|
const idToUse = lastAssistantId || currentAssistantId
|
|
537
460
|
if (idToUse && accumulator.hasContent()) {
|
|
@@ -562,6 +485,7 @@ export function processHistoricalMessagesWithErrors(
|
|
|
562
485
|
}
|
|
563
486
|
|
|
564
487
|
messages.forEach((msg, index) => {
|
|
488
|
+
// Filter by chat type if specified
|
|
565
489
|
if (chatTypeFilter && msg.chatType !== chatTypeFilter) return
|
|
566
490
|
|
|
567
491
|
const messageDataArray = Array.isArray(msg.messageData)
|
|
@@ -625,8 +549,10 @@ export function processHistoricalMessagesWithErrors(
|
|
|
625
549
|
}
|
|
626
550
|
|
|
627
551
|
messageDataArray.forEach((data) => {
|
|
628
|
-
|
|
629
|
-
|
|
552
|
+
const event = decodeHistoricalMessageData(data)
|
|
553
|
+
if (!event) return
|
|
554
|
+
applyHistoryEvent(
|
|
555
|
+
event,
|
|
630
556
|
accumulator,
|
|
631
557
|
approvalStatuses,
|
|
632
558
|
{ displayApprovalTypes, batchApprovalsEnabled },
|
|
@@ -634,6 +560,7 @@ export function processHistoricalMessagesWithErrors(
|
|
|
634
560
|
)
|
|
635
561
|
})
|
|
636
562
|
|
|
563
|
+
// Check if we should flush (next message is from user or last message)
|
|
637
564
|
const nextMsg = messages[index + 1]
|
|
638
565
|
const isLastMessage = index === messages.length - 1
|
|
639
566
|
const nextIsFromUser =
|
|
@@ -666,3 +593,52 @@ export function processHistoricalMessagesWithErrors(
|
|
|
666
593
|
escalatedApprovals: escalatedApprovals
|
|
667
594
|
}
|
|
668
595
|
}
|
|
596
|
+
|
|
597
|
+
/**
|
|
598
|
+
* Extract error messages from historical messages
|
|
599
|
+
* Returns a separate array of error messages that should be displayed
|
|
600
|
+
*/
|
|
601
|
+
export function extractErrorMessages(
|
|
602
|
+
messages: HistoricalMessage[],
|
|
603
|
+
options: MessageProcessingOptions = {}
|
|
604
|
+
): ProcessedMessage[] {
|
|
605
|
+
const { assistantName = 'Fae', assistantType = 'fae', assistantAvatar, chatTypeFilter } = options
|
|
606
|
+
|
|
607
|
+
const errorMessages: ProcessedMessage[] = []
|
|
608
|
+
|
|
609
|
+
messages.forEach((msg) => {
|
|
610
|
+
if (chatTypeFilter && msg.chatType !== chatTypeFilter) return
|
|
611
|
+
|
|
612
|
+
const messageDataArray = Array.isArray(msg.messageData)
|
|
613
|
+
? msg.messageData
|
|
614
|
+
: msg.messageData
|
|
615
|
+
? [msg.messageData]
|
|
616
|
+
: []
|
|
617
|
+
|
|
618
|
+
messageDataArray.forEach((data) => {
|
|
619
|
+
if (data.type === MESSAGE_TYPE.ERROR) {
|
|
620
|
+
errorMessages.push({
|
|
621
|
+
id: `${msg.id}-error`,
|
|
622
|
+
role: 'error',
|
|
623
|
+
content: 'error' in data && data.error ? data.error : 'An error occurred',
|
|
624
|
+
name: assistantName,
|
|
625
|
+
assistantType,
|
|
626
|
+
timestamp: new Date(msg.createdAt),
|
|
627
|
+
avatar: assistantAvatar,
|
|
628
|
+
})
|
|
629
|
+
}
|
|
630
|
+
})
|
|
631
|
+
})
|
|
632
|
+
|
|
633
|
+
return errorMessages
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
/**
|
|
637
|
+
* Process messages and include error messages in the correct order.
|
|
638
|
+
*
|
|
639
|
+
* HISTORICAL ALIAS: this was a byte-identical second copy of
|
|
640
|
+
* `processHistoricalMessages` (both goldens snapshot identical output on the
|
|
641
|
+
* same corpus). The duplicate envelope was deleted in Phase 3 — kept as an
|
|
642
|
+
* alias for the established import sites.
|
|
643
|
+
*/
|
|
644
|
+
export const processHistoricalMessagesWithErrors = processHistoricalMessages
|