@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
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SSE wire-protocol encoders — the emit side of the framing contract in
|
|
3
|
+
* `./frames.ts`. Server-safe (TextEncoder only). The hub's stream route
|
|
4
|
+
* consumes these so emitter and decoder can never drift.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
FRAME_TERMINATOR,
|
|
9
|
+
END_OF_LEADING,
|
|
10
|
+
TRAILER_SENTINEL,
|
|
11
|
+
type SseLeadingFrame,
|
|
12
|
+
type SseTrailingUsageFrame,
|
|
13
|
+
} from './frames'
|
|
14
|
+
|
|
15
|
+
const encoder = new TextEncoder()
|
|
16
|
+
|
|
17
|
+
/** Matches every framing sentinel byte: `\0`, `\x1E`, `\x1F`. THE one
|
|
18
|
+
* regex — `stripSentinelBytes` is its only consumer. */
|
|
19
|
+
const SENTINEL_BYTES_RE = /[\u0000\u001E\u001F]/g
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Remove the SSE framing sentinel bytes (`\0`, `\x1E`, `\x1F`) from model
|
|
23
|
+
* text. Exported as a STANDALONE helper — not just folded into
|
|
24
|
+
* `encodeTextDelta` — because producers must strip at the point where they
|
|
25
|
+
* first take custody of the text, BEFORE any local accumulation.
|
|
26
|
+
*
|
|
27
|
+
* Concretely: the hub's `claude-util.ts` accumulates `state.answerText` for
|
|
28
|
+
* persistence + telemetry. If stripping only happened at encode time, the
|
|
29
|
+
* persisted/telemetered answer would diverge, character for character, from
|
|
30
|
+
* the bytes the user actually received whenever the model echoes a control
|
|
31
|
+
* byte. Both sides call this, so there is exactly ONE definition of "which
|
|
32
|
+
* bytes are framing" in the whole system — do not re-declare the regex.
|
|
33
|
+
*
|
|
34
|
+
* Idempotent and per-character, so it distributes over concatenation:
|
|
35
|
+
* `strip(a + b) === strip(a) + strip(b)` — safe to apply per-delta or on the
|
|
36
|
+
* accumulated string interchangeably.
|
|
37
|
+
*/
|
|
38
|
+
export function stripSentinelBytes(text: string): string {
|
|
39
|
+
return text.replace(SENTINEL_BYTES_RE, '')
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Encode one leading frame: JSON + `\0` terminator. */
|
|
43
|
+
export function encodeLeadingFrame(frame: SseLeadingFrame): Uint8Array {
|
|
44
|
+
return encoder.encode(JSON.stringify(frame) + FRAME_TERMINATOR)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Encode the single `\x1E` end-of-leading-frames sentinel. */
|
|
48
|
+
export function encodeEndOfLeading(): Uint8Array {
|
|
49
|
+
return encoder.encode(END_OF_LEADING)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Encode a raw answer-text delta. STRIPS the framing sentinel bytes via
|
|
54
|
+
* `stripSentinelBytes` so model-echoed control bytes can no longer
|
|
55
|
+
* mis-frame the stream (a literal `\x1F` in the answer used to flip the
|
|
56
|
+
* client into trailer mode and swallow the rest of the answer PLUS the real
|
|
57
|
+
* usage trailer — characterized by golden fixture (d)). Forward-looking
|
|
58
|
+
* encoder fix only; the decoder's behavior is unchanged.
|
|
59
|
+
*
|
|
60
|
+
* Producers that ALSO accumulate the text locally (persistence, telemetry)
|
|
61
|
+
* must call `stripSentinelBytes` on their own copy — see its docblock.
|
|
62
|
+
*/
|
|
63
|
+
export function encodeTextDelta(text: string): Uint8Array {
|
|
64
|
+
return encoder.encode(stripSentinelBytes(text))
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Encode the trailing usage frame: `\x1F` + JSON, no terminator
|
|
68
|
+
* (runs to stream end). */
|
|
69
|
+
export function encodeTrailingUsageFrame(frame: SseTrailingUsageFrame): Uint8Array {
|
|
70
|
+
return encoder.encode(TRAILER_SENTINEL + JSON.stringify(frame))
|
|
71
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `envFlagEnabled` — THE cross-repo predicate for reading a boolean env flag.
|
|
3
|
+
*
|
|
4
|
+
* WHY IT LIVES HERE: the same flag is read on BOTH sides of the guide-chat
|
|
5
|
+
* seam and each side had grown its own reading of it. The hub parsed the
|
|
6
|
+
* value (`'0'` / `'false'` / `'off'` mean OFF); the producing app used bare
|
|
7
|
+
* `Boolean(process.env.X)`, under which every non-empty string — including
|
|
8
|
+
* `'0'` and `'false'` — is TRUE.
|
|
9
|
+
*
|
|
10
|
+
* That divergence failed OPEN on the one flag that gates a security boundary.
|
|
11
|
+
* `TRUSTED_INGRESS_SETS_REAL_IP` is an operator's assertion that a reverse
|
|
12
|
+
* proxy OVERWRITES the client-IP headers; without it those headers are
|
|
13
|
+
* attacker-writable and must not be read. An operator writing `=0` to mean
|
|
14
|
+
* "off" got the hub honoring that while the producer read `x-real-ip` /
|
|
15
|
+
* `x-forwarded-for` anyway and forwarded the value as `x-chat-ip`, which the
|
|
16
|
+
* hub then trusts verbatim under the service token. A rate-limit-bucket
|
|
17
|
+
* spoof, reachable through a plausible operator typo.
|
|
18
|
+
*
|
|
19
|
+
* Both repos already depend on this package and `./chat-protocol` is its
|
|
20
|
+
* server-safe subpath (no React, no browser APIs), so this is the one place
|
|
21
|
+
* both sides can share — same reasoning that moved `normalizeIpForBucketKey`
|
|
22
|
+
* here. Keep it as ONE definition: a second copy is what caused the drift.
|
|
23
|
+
*
|
|
24
|
+
* Contract:
|
|
25
|
+
* - unset, empty, or whitespace-only → `false`;
|
|
26
|
+
* - `'0'`, `'false'`, `'off'` → `false` (case- and
|
|
27
|
+
* surrounding-whitespace-insensitive);
|
|
28
|
+
* - any other non-empty value → `true`.
|
|
29
|
+
*
|
|
30
|
+
* `process.env` is read PER CALL, never module-hoisted, so tests, preview and
|
|
31
|
+
* prod all observe the live value rather than whatever was present at
|
|
32
|
+
* module-eval time.
|
|
33
|
+
*/
|
|
34
|
+
export function envFlagEnabled(name: string): boolean {
|
|
35
|
+
const raw = process.env[name]
|
|
36
|
+
if (!raw) return false
|
|
37
|
+
const v = raw.trim().toLowerCase()
|
|
38
|
+
return v !== '' && v !== '0' && v !== 'false' && v !== 'off'
|
|
39
|
+
}
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalized chat stream events — the transport-agnostic event union BOTH
|
|
3
|
+
* decoders (SSE byte framing in `./decode.ts`, NATS chunks in
|
|
4
|
+
* `./nats-decoder.ts`) emit. Phase 3 of the chat unification makes the
|
|
5
|
+
* reducer consume these directly; today the SSE adapter maps them back to
|
|
6
|
+
* legacy `MessageSegment` yields.
|
|
7
|
+
*
|
|
8
|
+
* Server-safe: no React, no browser APIs.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
// The wire-frame shapes these events carry through are defined ONCE in
|
|
12
|
+
// `./frames.ts` — reuse them here rather than restating their fields.
|
|
13
|
+
import type {
|
|
14
|
+
ApprovalRequestField,
|
|
15
|
+
DecisionResolvedFrame,
|
|
16
|
+
UsageTelemetry,
|
|
17
|
+
} from './frames'
|
|
18
|
+
|
|
19
|
+
/** Optional envelope on every event. `seq` carries the transport's
|
|
20
|
+
* stream sequence (JetStream `streamSeq` on NATS; unused on SSE). */
|
|
21
|
+
interface ChatStreamEventBase {
|
|
22
|
+
seq?: number
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Stream entered answer-text mode (SSE: the `\x1E` sentinel or the
|
|
26
|
+
* JSON-parse-failure fallback; NATS: MESSAGE_START).
|
|
27
|
+
* `implicit` is true ONLY for the SSE fallback path where a
|
|
28
|
+
* non-JSON leading buffer flips straight into text mode — consumers
|
|
29
|
+
* that flush coalesced thinking on turn-start must SKIP the flush for
|
|
30
|
+
* implicit starts (legacy adapter parity: the fallback path never
|
|
31
|
+
* flushed pending thinking before the text). */
|
|
32
|
+
export interface TurnStartEvent extends ChatStreamEventBase {
|
|
33
|
+
type: 'turn-start'
|
|
34
|
+
implicit?: boolean
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface TurnEndEvent extends ChatStreamEventBase {
|
|
38
|
+
type: 'turn-end'
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Answer text delta. `leading: true` marks SSE `text-leading` frames
|
|
42
|
+
* (model preamble prose emitted BEFORE the tool approval card). */
|
|
43
|
+
export interface TextDeltaEvent extends ChatStreamEventBase {
|
|
44
|
+
type: 'text-delta'
|
|
45
|
+
text: string
|
|
46
|
+
leading?: boolean
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Thinking delta — APPEND-ONLY contract: each event carries the next
|
|
50
|
+
* verbatim slice; consumers accumulate. Decoders never diff. */
|
|
51
|
+
export interface ThinkingDeltaEvent extends ChatStreamEventBase {
|
|
52
|
+
type: 'thinking-delta'
|
|
53
|
+
text: string
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Guide-body delta (NATS `GUIDE` chunk) — the assistant's how-to /
|
|
57
|
+
* documentation answer, rendered as a titled "OpenFrame Guide" card rather
|
|
58
|
+
* than bare prose. Same APPEND-ONLY contract as `text-delta` /
|
|
59
|
+
* `thinking-delta`: each event carries the next verbatim slice and consumers
|
|
60
|
+
* coalesce into the trailing `guide` segment. NATS-only today — the SSE
|
|
61
|
+
* frame grammar has no guide frame — but it lives in the shared union
|
|
62
|
+
* because the reducer is transport-agnostic. */
|
|
63
|
+
export interface GuideDeltaEvent extends ChatStreamEventBase {
|
|
64
|
+
type: 'guide-delta'
|
|
65
|
+
text: string
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface StatusEvent extends ChatStreamEventBase {
|
|
69
|
+
type: 'status'
|
|
70
|
+
phase: 'thinking'
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** NATS tool execution progress (EXECUTING_TOOL / EXECUTED_TOOL). */
|
|
74
|
+
export interface ToolExecutionEvent extends ChatStreamEventBase {
|
|
75
|
+
type: 'tool-execution'
|
|
76
|
+
data: {
|
|
77
|
+
type: 'EXECUTING_TOOL' | 'EXECUTED_TOOL'
|
|
78
|
+
integratedToolType: string
|
|
79
|
+
toolFunction: string
|
|
80
|
+
toolTitle?: string
|
|
81
|
+
/**
|
|
82
|
+
* Human-readable "why this tool is running" line, rendered as the body of
|
|
83
|
+
* the tool card. Carried by the EXECUTING chunk ONLY — the EXECUTED one
|
|
84
|
+
* never repeats it, and the accumulator's merge keeps whatever the
|
|
85
|
+
* EXECUTING segment held (`toolData.toolExplanation ?? existing`). Omitting
|
|
86
|
+
* it from this contract blanks the card for the whole run, live and on
|
|
87
|
+
* replay, which is exactly what happened while it was missing here.
|
|
88
|
+
*/
|
|
89
|
+
toolExplanation?: string
|
|
90
|
+
parameters?: Record<string, unknown>
|
|
91
|
+
result?: string
|
|
92
|
+
success?: boolean
|
|
93
|
+
toolExecutionRequestId?: string
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** Single tool call inside a batch approval request (NATS). */
|
|
98
|
+
export interface ApprovalToolCall {
|
|
99
|
+
toolExecutionRequestId: string
|
|
100
|
+
toolName: string
|
|
101
|
+
toolTitle?: string
|
|
102
|
+
toolExplanation?: string
|
|
103
|
+
toolType?: string
|
|
104
|
+
requiresApproval: boolean
|
|
105
|
+
approvalType?: string | null
|
|
106
|
+
toolCallArguments?: Record<string, unknown> | null
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** A tool call awaits user approval. SSE fills `command`/`fields`
|
|
110
|
+
* (card-ready payload); NATS fills `command`/`explanation` or
|
|
111
|
+
* `toolCalls` (batch form). */
|
|
112
|
+
export interface ApprovalRequestEvent extends ChatStreamEventBase {
|
|
113
|
+
type: 'approval-request'
|
|
114
|
+
requestId: string
|
|
115
|
+
/** SSE: the write tool's name. NATS: approval tier (USER/ADMIN/…). */
|
|
116
|
+
approvalType?: string
|
|
117
|
+
command?: string
|
|
118
|
+
explanation?: string
|
|
119
|
+
fields?: ApprovalRequestField[]
|
|
120
|
+
toolCalls?: ApprovalToolCall[]
|
|
121
|
+
status?: 'pending'
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** An approval request was resolved (SSE `decision_resolved` frame /
|
|
125
|
+
* NATS APPROVAL_RESULT chunk). */
|
|
126
|
+
export interface ApprovalResolvedEvent extends ChatStreamEventBase {
|
|
127
|
+
type: 'approval-resolved'
|
|
128
|
+
requestId?: string
|
|
129
|
+
status: 'approved' | 'rejected'
|
|
130
|
+
ok?: boolean
|
|
131
|
+
toolName?: string
|
|
132
|
+
approvalType?: string
|
|
133
|
+
resolvedByName?: string | null
|
|
134
|
+
receiptText?: string
|
|
135
|
+
/** Inline post-approve card: the ref payload + its documentType +
|
|
136
|
+
* the `[card://…]` marker (SSE only). */
|
|
137
|
+
cardRef?: unknown
|
|
138
|
+
cardType?: string
|
|
139
|
+
marker?: string
|
|
140
|
+
result?: DecisionResolvedFrame['result']
|
|
141
|
+
willAutoContinue?: boolean
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Per-turn metadata. Raw wire values pass through UNVALIDATED — the
|
|
145
|
+
* consumer replicates the legacy truthiness/typeof gates (so a
|
|
146
|
+
* malformed frame degrades identically to the pre-SSOT parser). */
|
|
147
|
+
export interface ChatMetadataEvent extends ChatStreamEventBase {
|
|
148
|
+
type: 'metadata'
|
|
149
|
+
provider?: string | null
|
|
150
|
+
modelLabel?: string | null
|
|
151
|
+
/** Raw model id (SSE `meta.model`; NATS `modelName`). */
|
|
152
|
+
modelName?: string | null
|
|
153
|
+
contextWindowMaxTokens?: number | null
|
|
154
|
+
sources?: unknown
|
|
155
|
+
refs?: unknown
|
|
156
|
+
scrollAnchor?: unknown
|
|
157
|
+
/** Server-minted conversation id (`ChatMetadataFrame.conversationId`),
|
|
158
|
+
* passed through raw like every other catch-all field — the consumer
|
|
159
|
+
* applies the `typeof === 'string' && truthy` gate. */
|
|
160
|
+
conversationId?: string | null
|
|
161
|
+
routing?: {
|
|
162
|
+
routedComplexity: string
|
|
163
|
+
routedModel?: string
|
|
164
|
+
routedThinkingBudget: number | null
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** SSE usage frames — raw wire keys (snake_case) preserved. */
|
|
169
|
+
export interface UsageEvent extends ChatStreamEventBase {
|
|
170
|
+
type: 'usage'
|
|
171
|
+
stage: 'start' | 'end'
|
|
172
|
+
input_tokens?: number
|
|
173
|
+
output_tokens?: number
|
|
174
|
+
cache_read_input_tokens?: number
|
|
175
|
+
cache_creation_input_tokens?: number
|
|
176
|
+
hit_rate_pct?: number
|
|
177
|
+
telemetry?: UsageTelemetry
|
|
178
|
+
/** Left OPAQUE on purpose: the consumer (`chat-stream-reducer`'s
|
|
179
|
+
* `applySseUsage`) re-validates every nested field with the legacy
|
|
180
|
+
* truthiness/typeof gates, so a malformed frame degrades identically
|
|
181
|
+
* to the pre-SSOT parser. Typing it would imply guarantees the wire
|
|
182
|
+
* does not make. */
|
|
183
|
+
breakdown?: unknown
|
|
184
|
+
debug?: unknown
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** NATS TOKEN_USAGE chunk (camelCase backend shape). */
|
|
188
|
+
export interface TokenUsageEvent extends ChatStreamEventBase {
|
|
189
|
+
type: 'token-usage'
|
|
190
|
+
inputTokensSize: number
|
|
191
|
+
outputTokensSize: number
|
|
192
|
+
totalTokensSize: number
|
|
193
|
+
contextSize: number
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export interface CompactionEvent extends ChatStreamEventBase {
|
|
197
|
+
type: 'compaction'
|
|
198
|
+
phase: 'start' | 'end'
|
|
199
|
+
summary?: string
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export interface ErrorEvent extends ChatStreamEventBase {
|
|
203
|
+
type: 'error'
|
|
204
|
+
title: string
|
|
205
|
+
details?: string
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Non-assistant message on the stream (NATS): the user's own message
|
|
209
|
+
* echo (`message-request`), an operator direct message, or a system
|
|
210
|
+
* line. */
|
|
211
|
+
/**
|
|
212
|
+
* Dialog participant owner type. The two members below are the ones clients
|
|
213
|
+
* BRANCH ON (admin-vs-not decides echo dedup and author styling); the union
|
|
214
|
+
* stays open (`string & {}`) because the wire may carry other roles that the
|
|
215
|
+
* client treats as "not admin". Always compare against `CHAT_OWNER_ADMIN`,
|
|
216
|
+
* never a bare `'ADMIN'` literal — a typo in a literal silently disables
|
|
217
|
+
* dedup instead of failing to compile.
|
|
218
|
+
*/
|
|
219
|
+
export type ChatOwnerType = 'ADMIN' | 'CLIENT' | (string & {})
|
|
220
|
+
|
|
221
|
+
/** Canonical ADMIN owner-type token (see `ChatOwnerType`). */
|
|
222
|
+
export const CHAT_OWNER_ADMIN = 'ADMIN'
|
|
223
|
+
|
|
224
|
+
export interface ParticipantEvent extends ChatStreamEventBase {
|
|
225
|
+
type: 'participant'
|
|
226
|
+
kind: 'message-request' | 'direct-message' | 'system'
|
|
227
|
+
text: string
|
|
228
|
+
ownerType?: ChatOwnerType
|
|
229
|
+
displayName?: string
|
|
230
|
+
userId?: string
|
|
231
|
+
contextItems?: Array<{ type: string; id: string; label: string }>
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export interface DialogClosedEvent extends ChatStreamEventBase {
|
|
235
|
+
type: 'dialog-closed'
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export type ChatStreamEvent =
|
|
239
|
+
| TurnStartEvent
|
|
240
|
+
| TurnEndEvent
|
|
241
|
+
| TextDeltaEvent
|
|
242
|
+
| ThinkingDeltaEvent
|
|
243
|
+
| GuideDeltaEvent
|
|
244
|
+
| StatusEvent
|
|
245
|
+
| ToolExecutionEvent
|
|
246
|
+
| ApprovalRequestEvent
|
|
247
|
+
| ApprovalResolvedEvent
|
|
248
|
+
| ChatMetadataEvent
|
|
249
|
+
| UsageEvent
|
|
250
|
+
| TokenUsageEvent
|
|
251
|
+
| CompactionEvent
|
|
252
|
+
| ErrorEvent
|
|
253
|
+
| ParticipantEvent
|
|
254
|
+
| DialogClosedEvent
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SSE wire-protocol frame shapes — the byte-level contract between the
|
|
3
|
+
* hub's chat stream emitter (`multi-platform-hub/lib/utils/claude-util.ts`)
|
|
4
|
+
* and the lib's SSE decoder (`./decode.ts`).
|
|
5
|
+
*
|
|
6
|
+
* Wire format (custom byte framing, NOT standard SSE `data:` lines):
|
|
7
|
+
*
|
|
8
|
+
* [JSON frame]\0 [JSON frame]\0 … \x1E <raw UTF-8 answer deltas> \x1F [JSON usage trailer]
|
|
9
|
+
*
|
|
10
|
+
* 1. LEADING frames — each a JSON object terminated by `\0`
|
|
11
|
+
* (FRAME_TERMINATOR). Status, metadata, routing, live thinking
|
|
12
|
+
* deltas, usage:start, tool approval frames.
|
|
13
|
+
* 2. ONE `\x1E` byte (END_OF_LEADING) — flips the stream into raw
|
|
14
|
+
* UTF-8 answer-text mode.
|
|
15
|
+
* 3. `\x1F` (TRAILER_SENTINEL) + one JSON usage frame running to
|
|
16
|
+
* stream end (no terminator).
|
|
17
|
+
*
|
|
18
|
+
* Server-safe: no React, no browser APIs beyond TextEncoder/TextDecoder.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
// =============================================================================
|
|
22
|
+
// Sentinel constants
|
|
23
|
+
// =============================================================================
|
|
24
|
+
|
|
25
|
+
/** Terminates every leading JSON frame. */
|
|
26
|
+
export const FRAME_TERMINATOR = '\0'
|
|
27
|
+
/** Record Separator — end of leading frames, start of raw answer text. */
|
|
28
|
+
export const END_OF_LEADING = '\x1E'
|
|
29
|
+
/** Unit Separator — start of the trailing usage JSON frame. */
|
|
30
|
+
export const TRAILER_SENTINEL = '\x1F'
|
|
31
|
+
|
|
32
|
+
// =============================================================================
|
|
33
|
+
// Routing types (moved verbatim from the hub's `lib/chat/wire-frames.ts`;
|
|
34
|
+
// `RouteComplexity` is lib-local so the lib never imports hub code)
|
|
35
|
+
// =============================================================================
|
|
36
|
+
|
|
37
|
+
/** Server-side routing complexity tiers. Mirror of the hub's
|
|
38
|
+
* `lib/constants/ai-models.ts` union — kept as a lib-local literal type
|
|
39
|
+
* so the wire SSOT has zero hub imports. The DECODER accepts any string
|
|
40
|
+
* (older/newer servers may ship values outside this union). */
|
|
41
|
+
export type RouteComplexity = 'trivial' | 'default' | 'complex' | 'deep'
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Routing decision frame. Emitted once per turn as a LEADING frame
|
|
45
|
+
* (between metadata and the `\x1E` end-of-leading-frames sentinel) when
|
|
46
|
+
* the server's `decideRoute()` produces a `Route`.
|
|
47
|
+
*
|
|
48
|
+
* Field names use the `routed*` prefix on PURPOSE — the client's
|
|
49
|
+
* catch-all leading-frame branch inspects `meta.model` / `meta.modelLabel`.
|
|
50
|
+
* A bare `model` field on this frame would OVERWRITE the metadata frame's
|
|
51
|
+
* modelLabel and break the <ModelDisplay> badge. The `routed` prefix
|
|
52
|
+
* sidesteps that collision.
|
|
53
|
+
*
|
|
54
|
+
* Older clients that don't recognize `kind: 'routing'` silently drop
|
|
55
|
+
* the frame (the catch-all only matches frames carrying known fields
|
|
56
|
+
* like `sources` / `refs` / `modelLabel`).
|
|
57
|
+
*/
|
|
58
|
+
export interface RoutingFrame {
|
|
59
|
+
kind: 'routing'
|
|
60
|
+
routedComplexity: RouteComplexity
|
|
61
|
+
routedModel: string
|
|
62
|
+
routedThinkingBudget: number
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Optional field on the trailing usage frame's `breakdown`. Surfaces
|
|
67
|
+
* per-route attribution to the admin test console so an operator can
|
|
68
|
+
* see "this turn ran on Haiku/trivial/$0.0005" without re-parsing the
|
|
69
|
+
* stream. Additive: clients that ignore unknown keys keep working.
|
|
70
|
+
*/
|
|
71
|
+
export interface RoutedAnswerBreakdown {
|
|
72
|
+
model: string
|
|
73
|
+
complexity: RouteComplexity
|
|
74
|
+
thinkingBudget: number
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// =============================================================================
|
|
78
|
+
// Leading frames
|
|
79
|
+
// =============================================================================
|
|
80
|
+
|
|
81
|
+
/** First frame on every stream — lets the shell render "Thinking…"
|
|
82
|
+
* within ~200ms, before any upstream byte arrives. */
|
|
83
|
+
export interface StatusThinkingFrame {
|
|
84
|
+
status: 'thinking'
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Per-turn metadata frame. The emitter sends `options.metadata`
|
|
88
|
+
* verbatim; a separate refs-only frame may follow with the same shape.
|
|
89
|
+
* All fields optional — the decoder's catch-all treats ANY frame that
|
|
90
|
+
* didn't match a `kind` discriminant as metadata-ish. */
|
|
91
|
+
export interface ChatMetadataFrame {
|
|
92
|
+
/** Retrieval sources for the source-chip strip. Host-defined row
|
|
93
|
+
* shape (`ChatSource` on the adapter side) — opaque at wire level. */
|
|
94
|
+
sources?: unknown[]
|
|
95
|
+
/** Per-row refs for inline object cards, keyed `<type>:<id>`. */
|
|
96
|
+
refs?: Record<string, unknown>
|
|
97
|
+
/** Raw model id (e.g. 'claude-sonnet-x'). Presence alone triggers the
|
|
98
|
+
* client's meta merge; the value itself is not stored. */
|
|
99
|
+
model?: string
|
|
100
|
+
modelLabel?: string
|
|
101
|
+
provider?: string
|
|
102
|
+
contextWindowMaxTokens?: number
|
|
103
|
+
/** Per-message viewport-positioning hint ('top' | 'bottom'). */
|
|
104
|
+
scrollAnchor?: string
|
|
105
|
+
/** SERVER-minted conversation trace id (`chat_conversations.id`),
|
|
106
|
+
* echoed on every turn's leading metadata frame. The client never
|
|
107
|
+
* generates ids — the first turn of a session goes out id-less, the
|
|
108
|
+
* server mints one and echoes it here, and the adapter persists the
|
|
109
|
+
* echoed id as its ONLY local state (see
|
|
110
|
+
* `chat-conversation-storage.ts` on the consumer side). */
|
|
111
|
+
conversationId?: string
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Live adaptive-thinking delta. WIRE IS ALREADY DELTA (Anthropic
|
|
115
|
+
* thinking_delta slices) — decoders must NOT diff or re-accumulate. */
|
|
116
|
+
export interface ThinkingDeltaFrame {
|
|
117
|
+
kind: 'thinking-delta'
|
|
118
|
+
text: string
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Usage frame emitted as a LEADING frame right after Anthropic's
|
|
122
|
+
* message_start (stage='start'). */
|
|
123
|
+
export interface UsageStartFrame {
|
|
124
|
+
kind: 'usage'
|
|
125
|
+
stage: 'start'
|
|
126
|
+
input_tokens: number
|
|
127
|
+
cache_read_input_tokens?: number
|
|
128
|
+
cache_creation_input_tokens?: number
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Model preamble prose written BEFORE a tool_use block — emitted as a
|
|
132
|
+
* standalone leading frame so it isn't stranded behind the approval
|
|
133
|
+
* card (the `\x1E` sentinel is suppressed on tool turns). */
|
|
134
|
+
export interface TextLeadingFrame {
|
|
135
|
+
kind: 'text-leading'
|
|
136
|
+
text: string
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Tool invocation failed server-side (proposal persist error,
|
|
140
|
+
* validation, ownership denial …). */
|
|
141
|
+
export interface ToolErrorFrame {
|
|
142
|
+
kind: 'tool_error'
|
|
143
|
+
toolName?: string
|
|
144
|
+
toolUseId?: string
|
|
145
|
+
message: string
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export interface ApprovalRequestField {
|
|
149
|
+
label: string
|
|
150
|
+
value: string
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** The model called a write tool; the server persisted a proposal and
|
|
154
|
+
* ships a card-ready payload. Shape mirrors the hub's `onToolUse`
|
|
155
|
+
* return in `lib/data/doc-chat-utils.ts`. */
|
|
156
|
+
export interface ApprovalRequestFrame {
|
|
157
|
+
kind: 'approval_request'
|
|
158
|
+
proposalId: string
|
|
159
|
+
toolName: string
|
|
160
|
+
title?: string
|
|
161
|
+
fields?: ApprovalRequestField[]
|
|
162
|
+
preamble?: string | null
|
|
163
|
+
args?: Record<string, unknown>
|
|
164
|
+
expiresAt?: string
|
|
165
|
+
ttlSeconds?: number
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Server-driven post-approve / post-reject frame (confirm-tool route).
|
|
169
|
+
* Mirror of the hub's `DecisionResolvedFrame` in
|
|
170
|
+
* `lib/data/chat-agent-utils.ts` (tool_name widened to string — the
|
|
171
|
+
* known-write-tool whitelist is host-side knowledge). */
|
|
172
|
+
export interface DecisionResolvedFrame {
|
|
173
|
+
kind: 'decision_resolved'
|
|
174
|
+
/** The proposal this decision resolved — locates the SOURCE approval
|
|
175
|
+
* card so the shell can flip its status + append the receipt. */
|
|
176
|
+
proposalId: string
|
|
177
|
+
ok: boolean
|
|
178
|
+
action: 'approved' | 'rejected'
|
|
179
|
+
tool_name?: string
|
|
180
|
+
result?: {
|
|
181
|
+
ticket_id?: string
|
|
182
|
+
status?: string | null
|
|
183
|
+
mirror_synced?: boolean
|
|
184
|
+
}
|
|
185
|
+
card?: {
|
|
186
|
+
type: string
|
|
187
|
+
marker: string
|
|
188
|
+
ref: unknown
|
|
189
|
+
}
|
|
190
|
+
/** True when the server WILL pipe an auto-continuation turn after
|
|
191
|
+
* this frame. */
|
|
192
|
+
willAutoContinue: boolean
|
|
193
|
+
/** Pre-rendered receipt copy from the source strategy's
|
|
194
|
+
* `receiptRenderer` — client renders verbatim. */
|
|
195
|
+
receiptText?: string
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Every leading frame the emitter can produce. */
|
|
199
|
+
export type SseLeadingFrame =
|
|
200
|
+
| StatusThinkingFrame
|
|
201
|
+
| ChatMetadataFrame
|
|
202
|
+
| RoutingFrame
|
|
203
|
+
| ThinkingDeltaFrame
|
|
204
|
+
| UsageStartFrame
|
|
205
|
+
| TextLeadingFrame
|
|
206
|
+
| ToolErrorFrame
|
|
207
|
+
| ApprovalRequestFrame
|
|
208
|
+
| DecisionResolvedFrame
|
|
209
|
+
|
|
210
|
+
// =============================================================================
|
|
211
|
+
// Trailing usage frame
|
|
212
|
+
// =============================================================================
|
|
213
|
+
|
|
214
|
+
/** Card/chip emission counts for admin adoption tooling. */
|
|
215
|
+
export interface UsageTelemetry {
|
|
216
|
+
cards: number
|
|
217
|
+
chips: number
|
|
218
|
+
sentences: number
|
|
219
|
+
answerLen: number
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Trailing usage frame — `\x1F` + JSON, runs to stream end (no
|
|
223
|
+
* terminator). `stage: 'display'` is a legacy alias the decoder treats
|
|
224
|
+
* identically to `'end'`. */
|
|
225
|
+
export interface SseTrailingUsageFrame {
|
|
226
|
+
kind: 'usage'
|
|
227
|
+
stage: 'end' | 'display'
|
|
228
|
+
input_tokens?: number
|
|
229
|
+
output_tokens?: number
|
|
230
|
+
cache_read_input_tokens?: number
|
|
231
|
+
cache_creation_input_tokens?: number
|
|
232
|
+
hit_rate_pct?: number
|
|
233
|
+
telemetry?: UsageTelemetry
|
|
234
|
+
/** Cross-call usage breakdown (Haiku rewriter / classifier /
|
|
235
|
+
* summarizer + routed-answer attribution). */
|
|
236
|
+
breakdown?: {
|
|
237
|
+
haikuRewriter?: { input: number; output: number }
|
|
238
|
+
haikuClassifier?: { input: number; output: number }
|
|
239
|
+
haikuSummarizer?: { input: number; output: number }
|
|
240
|
+
routedAnswer?: RoutedAnswerBreakdown
|
|
241
|
+
}
|
|
242
|
+
/** Gated deep-debug payload (only present when the request carried
|
|
243
|
+
* the privileged debug secret). */
|
|
244
|
+
debug?: Record<string, unknown>
|
|
245
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chat wire-protocol SSOT.
|
|
3
|
+
*
|
|
4
|
+
* The byte-level SSE framing contract (frames + encode + decode) and the
|
|
5
|
+
* transport-agnostic `ChatStreamEvent` union both the SSE and NATS
|
|
6
|
+
* decoders emit. Published as the server-safe `./chat-protocol` subpath —
|
|
7
|
+
* no React, no browser APIs beyond TextEncoder/TextDecoder — so the hub's
|
|
8
|
+
* stream route (emit side) and the lib's chat hooks (decode side) share
|
|
9
|
+
* one protocol module.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export * from './frames'
|
|
13
|
+
export * from './events'
|
|
14
|
+
export * from './encode'
|
|
15
|
+
export * from './decode'
|
|
16
|
+
export * from './nats-decoder'
|
|
17
|
+
// Cross-repo IP bucket-key normalizer (producer app + consumer hub share it).
|
|
18
|
+
export * from './ip-normalize'
|
|
19
|
+
// Cross-repo env-flag predicate — the trust assertion that gates the IP
|
|
20
|
+
// forwarding above must be parsed IDENTICALLY on both sides of the seam.
|
|
21
|
+
export * from './env-flag'
|