@adminide-stack/yantra-mobile 12.0.28-alpha.67 → 12.0.28-alpha.69

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/lib/api/stt.js +54 -0
  2. package/lib/api/stt.js.map +1 -0
  3. package/lib/components/GatewayConnector/GatewayConnector.js +18 -0
  4. package/lib/components/GatewayConnector/GatewayConnector.js.map +1 -0
  5. package/lib/components/NavigationHeader/NavigationHeader.js +214 -0
  6. package/lib/components/NavigationHeader/NavigationHeader.js.map +1 -0
  7. package/lib/components/ThinkingIndicator.js +55 -0
  8. package/lib/components/ThinkingIndicator.js.map +1 -0
  9. package/lib/compute.js +108 -31
  10. package/lib/compute.js.map +1 -1
  11. package/lib/contexts/CdecliConnectionContext.js +47 -0
  12. package/lib/contexts/CdecliConnectionContext.js.map +1 -0
  13. package/lib/contexts/GatewayContext.js +1 -1
  14. package/lib/features/audio-input/AudioRecorderPanel.js +231 -0
  15. package/lib/features/audio-input/AudioRecorderPanel.js.map +1 -0
  16. package/lib/features/audio-input/MicErrorBoundary.js +34 -0
  17. package/lib/features/audio-input/MicErrorBoundary.js.map +1 -0
  18. package/lib/hooks/useCdecliAutoConnect.js +41 -18
  19. package/lib/hooks/useCdecliAutoConnect.js.map +1 -1
  20. package/lib/hooks/useChatApi.js +158 -41
  21. package/lib/hooks/useChatApi.js.map +1 -1
  22. package/lib/hooks/useChatStream.js +59 -16
  23. package/lib/hooks/useChatStream.js.map +1 -1
  24. package/lib/hooks/usePrerequisiteIds.js +8 -12
  25. package/lib/hooks/usePrerequisiteIds.js.map +1 -1
  26. package/lib/index.js +1 -1
  27. package/lib/index.js.map +1 -1
  28. package/lib/routes.json +112 -0
  29. package/lib/screens/Chat/index.js +423 -0
  30. package/lib/screens/Chat/index.js.map +1 -0
  31. package/lib/screens/ChatHistory/index.js +56 -0
  32. package/lib/screens/ChatHistory/index.js.map +1 -0
  33. package/lib/screens/Home/HomeScreen.js +136 -427
  34. package/lib/screens/Home/HomeScreen.js.map +1 -1
  35. package/lib/screens/Home/components/ChatHistoryLanding.js +436 -214
  36. package/lib/screens/Home/components/ChatHistoryLanding.js.map +1 -1
  37. package/package.json +4 -4
@@ -0,0 +1,47 @@
1
+ import {jsx}from'react/jsx-runtime';import {useContext,createContext,useState,useCallback,useMemo}from'react';import {useCdecliAutoConnect}from'../hooks/useCdecliAutoConnect.js';import {useGatewayContext}from'./GatewayContext.js';var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ const CdecliConnectionContext = createContext(null);
21
+ CdecliConnectionContext.displayName = "CdecliConnectionContext";
22
+ function CdecliConnectionProvider({
23
+ children
24
+ }) {
25
+ const {
26
+ selectedGateway
27
+ } = useGatewayContext();
28
+ const cdecliSelected = (selectedGateway == null ? void 0 : selectedGateway.channelType) === "cdecli-serve";
29
+ const [activeChannelId, setActiveChannelIdState] = useState(void 0);
30
+ const setActiveChannelId = useCallback((channelId) => {
31
+ setActiveChannelIdState((prev) => prev === channelId ? prev : channelId);
32
+ }, []);
33
+ const connection = useCdecliAutoConnect(cdecliSelected, activeChannelId);
34
+ const value = useMemo(() => __spreadProps(__spreadValues({}, connection), {
35
+ cdecliSelected,
36
+ activeChannelId,
37
+ setActiveChannelId
38
+ }), [connection, cdecliSelected, activeChannelId, setActiveChannelId]);
39
+ return /* @__PURE__ */ jsx(CdecliConnectionContext.Provider, { value, children });
40
+ }
41
+ function useCdecliConnection() {
42
+ const ctx = useContext(CdecliConnectionContext);
43
+ if (!ctx) {
44
+ throw new Error("useCdecliConnection must be used within CdecliConnectionProvider");
45
+ }
46
+ return ctx;
47
+ }export{CdecliConnectionProvider,useCdecliConnection};//# sourceMappingURL=CdecliConnectionContext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CdecliConnectionContext.js","sources":["../../src/contexts/CdecliConnectionContext.tsx"],"sourcesContent":["/**\n * Single, app-wide owner of the CDeCLI auto-connect lifecycle.\n *\n * Previously every consumer that needed CDeCLI status (the header\n * `GatewayConnector`, the Chat screen for streaming routing, etc.) called\n * `useCdecliAutoConnect` directly. Each instance kept its own state, so they\n * raced each other: the header would mount and connect with `chatId=undefined`\n * while the Chat screen mounted and connected with `chatId=<channelId>`. The\n * backend `CdecliServeProvider.connect()` reuses the per-account session, so\n * the LAST `gatewayConnect` mutation wins — when the header's \"no chatId\"\n * connect lost the chat binding, the agent received messages without a chat\n * context and replied with the generic \"agent did not return a response /\n * API key may be missing\" fallback.\n *\n * The fix: lift `useCdecliAutoConnect` into a context that runs ONCE per app\n * mount. Screens that need to bind a specific chat call `setActiveChannelId`;\n * everyone else (header pill, status banners) just reads the live status.\n *\n * Mount this provider INSIDE `GatewayContextProvider` — the hook depends on\n * `useGatewayContext` for the current selection.\n */\nimport type { ReactNode } from 'react';\nimport React, { createContext, useCallback, useContext, useMemo, useState } from 'react';\nimport { useCdecliAutoConnect, type UseCdecliAutoConnectResult } from '../hooks/useCdecliAutoConnect';\nimport { useGatewayContext } from './GatewayContext';\n\nexport interface CdecliConnectionContextValue extends UseCdecliAutoConnectResult {\n /** Whether the CDeCLI gateway is the currently selected gateway. */\n cdecliSelected: boolean;\n /** Current chat binding for the gateway session (undefined on Home / Chat history). */\n activeChannelId: string | undefined;\n /**\n * Register / clear the chat binding for the gateway session.\n * Screens that own a channel (Chat) call this on mount with their channelId\n * and on unmount with `undefined` (or simply leave the last binding in\n * place — the gateway tolerates stale chatIds, individual messages still\n * pass their own chatId via `gatewaySendMessage`).\n */\n setActiveChannelId: (channelId: string | undefined) => void;\n}\n\nconst CdecliConnectionContext = createContext<CdecliConnectionContextValue | null>(null);\nCdecliConnectionContext.displayName = 'CdecliConnectionContext';\n\nexport function CdecliConnectionProvider({ children }: { children: ReactNode }) {\n const { selectedGateway } = useGatewayContext();\n const cdecliSelected = selectedGateway?.channelType === 'cdecli-serve';\n\n const [activeChannelId, setActiveChannelIdState] = useState<string | undefined>(undefined);\n\n const setActiveChannelId = useCallback((channelId: string | undefined) => {\n setActiveChannelIdState((prev) => (prev === channelId ? prev : channelId));\n }, []);\n\n const connection = useCdecliAutoConnect(cdecliSelected, activeChannelId);\n\n const value = useMemo<CdecliConnectionContextValue>(\n () => ({\n ...connection,\n cdecliSelected,\n activeChannelId,\n setActiveChannelId,\n }),\n [connection, cdecliSelected, activeChannelId, setActiveChannelId],\n );\n\n return <CdecliConnectionContext.Provider value={value}>{children}</CdecliConnectionContext.Provider>;\n}\n\nexport function useCdecliConnection(): CdecliConnectionContextValue {\n const ctx = useContext(CdecliConnectionContext);\n if (!ctx) {\n throw new Error('useCdecliConnection must be used within CdecliConnectionProvider');\n }\n return ctx;\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;AAuCA,MAAM,uBAAA,GAA0B,cAAmD,IAAI,CAAA;AACvF,uBAAA,CAAwB,WAAc,GAAA,yBAAA;AAC/B,SAAS,wBAAyB,CAAA;AAAA,EACvC;AACF,CAEG,EAAA;AACD,EAAM,MAAA;AAAA,IACJ;AAAA,MACE,iBAAkB,EAAA;AACtB,EAAM,MAAA,cAAA,GAAA,CAAiB,mDAAiB,WAAgB,MAAA,cAAA;AACxD,EAAA,MAAM,CAAC,eAAA,EAAiB,uBAAuB,CAAA,GAAI,SAA6B,MAAS,CAAA;AACzF,EAAM,MAAA,kBAAA,GAAqB,WAAY,CAAA,CAAC,SAAkC,KAAA;AACxE,IAAA,uBAAA,CAAwB,CAAQ,IAAA,KAAA,IAAA,KAAS,SAAY,GAAA,IAAA,GAAO,SAAS,CAAA;AAAA,GACvE,EAAG,EAAE,CAAA;AACL,EAAM,MAAA,UAAA,GAAa,oBAAqB,CAAA,cAAA,EAAgB,eAAe,CAAA;AACvE,EAAA,MAAM,KAAQ,GAAA,OAAA,CAAsC,MAAO,aAAA,CAAA,cAAA,CAAA,EAAA,EACtD,UADsD,CAAA,EAAA;AAAA,IAEzD,cAAA;AAAA,IACA,eAAA;AAAA,IACA;AAAA,MACE,CAAC,UAAA,EAAY,cAAgB,EAAA,eAAA,EAAiB,kBAAkB,CAAC,CAAA;AACrE,EAAA,uBAAQ,GAAA,CAAA,uBAAA,CAAwB,QAAxB,EAAA,EAAiC,OAAe,QAAS,EAAA,CAAA;AACnE;AACO,SAAS,mBAAoD,GAAA;AAClE,EAAM,MAAA,GAAA,GAAM,WAAW,uBAAuB,CAAA;AAC9C,EAAA,IAAI,CAAC,GAAK,EAAA;AACR,IAAM,MAAA,IAAI,MAAM,kEAAkE,CAAA;AAAA;AAEpF,EAAO,OAAA,GAAA;AACT"}
@@ -1,4 +1,4 @@
1
- import {jsx}from'react/jsx-runtime';import {useContext,createContext,useState,useEffect,useCallback,useMemo}from'react';import {useGatewayRegistry}from'../hooks/useGatewayRegistry.js';import {GatewaySelectionStorage}from'../utils/gatewaySelectionStorage.js';const GatewayContext = createContext(null);
1
+ import {jsx}from'react/jsx-runtime';import {createContext,useState,useEffect,useCallback,useMemo,useContext}from'react';import {useGatewayRegistry}from'../hooks/useGatewayRegistry.js';import {GatewaySelectionStorage}from'../utils/gatewaySelectionStorage.js';const GatewayContext = createContext(null);
2
2
  GatewayContext.displayName = "GatewayContext";
3
3
  function GatewayContextProvider({
4
4
  children
@@ -0,0 +1,231 @@
1
+ import {jsxs,jsx}from'react/jsx-runtime';import {useState,useRef,useEffect,useCallback,useMemo}from'react';import {Animated,Easing,View,StyleSheet,ActivityIndicator,Pressable}from'react-native';import {Ionicons,Feather}from'@expo/vector-icons';import {useAudioRecorder,RecordingPresets,useAudioRecorderState,setAudioModeAsync,AudioModule}from'expo-audio';import {transcribeAudio}from'../../api/stt.js';const MAX_DURATION_MS = 3 * 60 * 1e3;
2
+ function AudioRecorderPanel({
3
+ isDark,
4
+ onTranscriptionComplete,
5
+ onCancel,
6
+ onError
7
+ }) {
8
+ const recorder = useAudioRecorder(RecordingPresets.HIGH_QUALITY);
9
+ const recorderState = useAudioRecorderState(recorder, 200);
10
+ const [isTranscribing, setIsTranscribing] = useState(false);
11
+ const hasStartedRef = useRef(false);
12
+ const isMountedRef = useRef(true);
13
+ const sendInFlightRef = useRef(false);
14
+ const pulse = useRef(new Animated.Value(1)).current;
15
+ useEffect(() => {
16
+ const loop = Animated.loop(Animated.sequence([Animated.timing(pulse, {
17
+ toValue: 0.3,
18
+ duration: 700,
19
+ easing: Easing.inOut(Easing.ease),
20
+ useNativeDriver: true
21
+ }), Animated.timing(pulse, {
22
+ toValue: 1,
23
+ duration: 700,
24
+ easing: Easing.inOut(Easing.ease),
25
+ useNativeDriver: true
26
+ })]));
27
+ loop.start();
28
+ return () => loop.stop();
29
+ }, [pulse]);
30
+ const stopAndCleanup = useCallback(async () => {
31
+ try {
32
+ if (recorderState.isRecording || recorder.isRecording) {
33
+ await recorder.stop().catch(() => void 0);
34
+ }
35
+ } catch (e) {
36
+ }
37
+ try {
38
+ await setAudioModeAsync({
39
+ allowsRecording: false,
40
+ playsInSilentMode: true
41
+ });
42
+ } catch (e) {
43
+ }
44
+ }, [recorder, recorderState.isRecording]);
45
+ const finalize = useCallback(async () => {
46
+ if (sendInFlightRef.current || isTranscribing) return;
47
+ sendInFlightRef.current = true;
48
+ if (isMountedRef.current) setIsTranscribing(true);
49
+ try {
50
+ await stopAndCleanup();
51
+ const uri = recorder.uri;
52
+ if (!uri) {
53
+ throw new Error("No recording produced");
54
+ }
55
+ const text = await transcribeAudio({
56
+ uri,
57
+ mimeType: "audio/m4a",
58
+ filename: "audio.m4a"
59
+ });
60
+ if (isMountedRef.current) {
61
+ onTranscriptionComplete(text);
62
+ }
63
+ } catch (err) {
64
+ const msg = err instanceof Error ? err.message : String(err);
65
+ onError == null ? void 0 : onError(msg);
66
+ onCancel();
67
+ } finally {
68
+ sendInFlightRef.current = false;
69
+ if (isMountedRef.current) setIsTranscribing(false);
70
+ }
71
+ }, [isTranscribing, recorder, stopAndCleanup, onTranscriptionComplete, onError, onCancel]);
72
+ useEffect(() => {
73
+ if (hasStartedRef.current) return;
74
+ hasStartedRef.current = true;
75
+ let cancelled = false;
76
+ const start = async () => {
77
+ try {
78
+ if (!AudioModule || typeof AudioModule.getRecordingPermissionsAsync !== "function") {
79
+ throw new Error("Audio module is not available");
80
+ }
81
+ const perm = await AudioModule.getRecordingPermissionsAsync();
82
+ if (!(perm == null ? void 0 : perm.granted)) {
83
+ throw new Error("Microphone permission denied");
84
+ }
85
+ await setAudioModeAsync({
86
+ allowsRecording: true,
87
+ playsInSilentMode: true
88
+ });
89
+ await recorder.prepareToRecordAsync();
90
+ if (cancelled || !isMountedRef.current) return;
91
+ recorder.record();
92
+ } catch (err) {
93
+ const msg = err instanceof Error ? err.message : String(err);
94
+ onError == null ? void 0 : onError(msg);
95
+ onCancel();
96
+ }
97
+ };
98
+ void start();
99
+ return () => {
100
+ cancelled = true;
101
+ };
102
+ }, []);
103
+ useEffect(() => {
104
+ return () => {
105
+ isMountedRef.current = false;
106
+ void stopAndCleanup();
107
+ };
108
+ }, []);
109
+ useEffect(() => {
110
+ var _a;
111
+ if (!recorderState.isRecording) return;
112
+ const elapsedMs = (_a = recorderState.durationMillis) != null ? _a : 0;
113
+ if (elapsedMs >= MAX_DURATION_MS && !sendInFlightRef.current) {
114
+ void finalize();
115
+ }
116
+ }, [recorderState.isRecording, recorderState.durationMillis, finalize]);
117
+ const handleCancelPress = useCallback(async () => {
118
+ if (isTranscribing) return;
119
+ await stopAndCleanup();
120
+ onCancel();
121
+ }, [isTranscribing, stopAndCleanup, onCancel]);
122
+ const handleSendPress = useCallback(() => {
123
+ void finalize();
124
+ }, [finalize]);
125
+ const formatted = useMemo(() => {
126
+ var _a;
127
+ const totalSeconds = Math.floor(((_a = recorderState.durationMillis) != null ? _a : 0) / 1e3);
128
+ const m = Math.floor(totalSeconds / 60);
129
+ const s = totalSeconds % 60;
130
+ return `${m}:${s.toString().padStart(2, "0")}`;
131
+ }, [recorderState.durationMillis]);
132
+ const bg = isDark ? "#0f172a" : "#ffffff";
133
+ const border = isDark ? "rgba(148,163,184,0.18)" : "#e4e4e7";
134
+ const fg = isDark ? "#e5e7eb" : "#18181b";
135
+ const muted = isDark ? "#94a3b8" : "#71717a";
136
+ const cancelBg = isDark ? "rgba(148,163,184,0.08)" : "#f4f4f5";
137
+ const showSpinner = isTranscribing;
138
+ const isRecording = recorderState.isRecording && !showSpinner;
139
+ const statusLabel = showSpinner ? "Transcribing\u2026" : isRecording ? formatted : "Initializing\u2026";
140
+ return /* @__PURE__ */ jsxs(View, { accessibilityRole: "summary", accessibilityLabel: "Voice recorder", style: [styles.container, {
141
+ backgroundColor: bg,
142
+ borderColor: border
143
+ }], children: [
144
+ /* @__PURE__ */ jsxs(View, { style: styles.statusRow, children: [
145
+ showSpinner ? /* @__PURE__ */ jsx(ActivityIndicator, { size: "small", color: fg }) : /* @__PURE__ */ jsx(Animated.View, { style: [styles.recDot, {
146
+ opacity: isRecording ? pulse : 0.35
147
+ }] }),
148
+ /* @__PURE__ */ jsx(Animated.Text, { style: [styles.timer, {
149
+ color: fg
150
+ }], accessibilityLiveRegion: "polite", children: statusLabel }),
151
+ isRecording && /* @__PURE__ */ jsx(Animated.Text, { style: [styles.maxLabel, {
152
+ color: muted
153
+ }], children: "/ 3:00" })
154
+ ] }),
155
+ /* @__PURE__ */ jsxs(View, { style: styles.actionsRow, children: [
156
+ /* @__PURE__ */ jsx(Pressable, { onPress: handleCancelPress, disabled: isTranscribing, accessibilityLabel: "Cancel recording", accessibilityRole: "button", style: ({
157
+ pressed
158
+ }) => [styles.iconBtn, {
159
+ backgroundColor: cancelBg,
160
+ borderColor: border,
161
+ opacity: isTranscribing ? 0.4 : pressed ? 0.7 : 1
162
+ }], children: /* @__PURE__ */ jsx(Ionicons, { name: "close", size: 16, color: fg }) }),
163
+ /* @__PURE__ */ jsx(Pressable, { onPress: handleSendPress, disabled: isTranscribing || !isRecording, accessibilityLabel: "Stop and transcribe", accessibilityRole: "button", style: ({
164
+ pressed
165
+ }) => [styles.sendBtn, {
166
+ opacity: isTranscribing ? 0.7 : !isRecording ? 0.5 : pressed ? 0.85 : 1
167
+ }], children: isTranscribing ? /* @__PURE__ */ jsx(ActivityIndicator, { size: "small", color: "#ffffff" }) : /* @__PURE__ */ jsx(Feather, { name: "arrow-up", size: 16, color: "#ffffff" }) })
168
+ ] })
169
+ ] });
170
+ }
171
+ const styles = StyleSheet.create({
172
+ container: {
173
+ flexDirection: "row",
174
+ alignItems: "center",
175
+ justifyContent: "space-between",
176
+ borderRadius: 24,
177
+ borderWidth: 1,
178
+ paddingVertical: 10,
179
+ paddingHorizontal: 14,
180
+ gap: 12,
181
+ shadowColor: "#0f172a",
182
+ shadowOpacity: 0.08,
183
+ shadowRadius: 12,
184
+ shadowOffset: {
185
+ width: 0,
186
+ height: 4
187
+ },
188
+ elevation: 4
189
+ },
190
+ statusRow: {
191
+ flexDirection: "row",
192
+ alignItems: "center",
193
+ gap: 8,
194
+ flexShrink: 1
195
+ },
196
+ recDot: {
197
+ width: 8,
198
+ height: 8,
199
+ borderRadius: 4,
200
+ backgroundColor: "#ef4444"
201
+ },
202
+ timer: {
203
+ fontSize: 13,
204
+ fontWeight: "600",
205
+ fontVariant: ["tabular-nums"]
206
+ },
207
+ maxLabel: {
208
+ fontSize: 11
209
+ },
210
+ actionsRow: {
211
+ flexDirection: "row",
212
+ alignItems: "center",
213
+ gap: 8
214
+ },
215
+ iconBtn: {
216
+ width: 32,
217
+ height: 32,
218
+ borderRadius: 16,
219
+ borderWidth: 1,
220
+ alignItems: "center",
221
+ justifyContent: "center"
222
+ },
223
+ sendBtn: {
224
+ width: 32,
225
+ height: 32,
226
+ borderRadius: 16,
227
+ alignItems: "center",
228
+ justifyContent: "center",
229
+ backgroundColor: "#18181b"
230
+ }
231
+ });export{AudioRecorderPanel,AudioRecorderPanel as default};//# sourceMappingURL=AudioRecorderPanel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AudioRecorderPanel.js","sources":["../../../src/features/audio-input/AudioRecorderPanel.tsx"],"sourcesContent":["import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';\nimport { ActivityIndicator, Animated, Easing, Pressable, StyleSheet, View } from 'react-native';\nimport { Feather, Ionicons } from '@expo/vector-icons';\nimport { AudioModule, RecordingPresets, setAudioModeAsync, useAudioRecorder, useAudioRecorderState } from 'expo-audio';\nimport { transcribeAudio } from '../../api/stt';\n\n/**\n * Mirrors the web ceiling in\n * `packages-modules/account/browser/src/features/audio-input/components/AudioRecorder.tsx`.\n * Three minutes is a soft Whisper ceiling — past that, accuracy and upload\n * time both degrade more than the marginal extra context is worth.\n */\nconst MAX_DURATION_MS = 3 * 60 * 1000;\n\nexport interface AudioRecorderPanelProps {\n isDark: boolean;\n /**\n * Called once with the final transcribed string after the user taps \"Send\".\n * The host screen typically appends this to its composer state and re-focuses\n * the text input — mirroring the web `handleTranscriptionComplete` flow.\n */\n onTranscriptionComplete: (text: string) => void;\n /** Called when the user discards the recording (X) or recording cannot start. */\n onCancel: () => void;\n /** Optional error sink — host can surface a toast/banner. */\n onError?: (error: string) => void;\n}\n\n/**\n * Inline voice-capture panel that replaces the composer while recording —\n * the React Native port of the web `AudioRecorder` component\n * (`packages-modules/account/browser/src/features/audio-input/components/AudioRecorder.tsx`).\n *\n * Flow (parity with web):\n * 1. Mount → request mic permission → set audio mode → prepare + start recording.\n * 2. While recording, display a live timer and pulsing record dot.\n * 3. User taps \"Send\" → stop recorder → upload `recorder.uri` to Groq Whisper\n * via `transcribeAudio` → return the transcribed text to the host via\n * `onTranscriptionComplete`.\n * 4. User taps \"Cancel\" (X) → stop + discard, no upload.\n * 5. If recording exceeds `MAX_DURATION_MS`, auto-finalize as if the user\n * tapped Send (matches web safety net).\n *\n * The panel is intentionally NOT rendered inside the `InputToolBar`'s `topContent`\n * slot — web fully swaps the toolbar for the recorder. Keeping the toolbar\n * visible during recording invites accidental sends with empty input and an\n * unfocused recorder state.\n */\nexport function AudioRecorderPanel({ isDark, onTranscriptionComplete, onCancel, onError }: AudioRecorderPanelProps) {\n const recorder = useAudioRecorder(RecordingPresets.HIGH_QUALITY);\n const recorderState = useAudioRecorderState(recorder, 200);\n const [isTranscribing, setIsTranscribing] = useState(false);\n\n /**\n * Guard against re-entry. `useEffect` runs once on mount but a fast double-\n * tap on the mic can produce two panel mounts in dev mode (StrictMode);\n * `hasStartedRef` ensures we don't start a second recorder against the\n * same shared object.\n */\n const hasStartedRef = useRef(false);\n const isMountedRef = useRef(true);\n /**\n * Caches the result of the in-flight transcription/cleanup pipeline so the\n * MAX_DURATION watcher and the explicit Send button can both call\n * `finalize()` without racing each other into a double-upload.\n */\n const sendInFlightRef = useRef(false);\n\n /**\n * Pulsing dot for the \"REC\" indicator. Pure UI sugar; avoids importing\n * reanimated for a 2-keyframe loop.\n */\n const pulse = useRef(new Animated.Value(1)).current;\n useEffect(() => {\n const loop = Animated.loop(\n Animated.sequence([\n Animated.timing(pulse, {\n toValue: 0.3,\n duration: 700,\n easing: Easing.inOut(Easing.ease),\n useNativeDriver: true,\n }),\n Animated.timing(pulse, {\n toValue: 1,\n duration: 700,\n easing: Easing.inOut(Easing.ease),\n useNativeDriver: true,\n }),\n ]),\n );\n loop.start();\n return () => loop.stop();\n }, [pulse]);\n\n const stopAndCleanup = useCallback(async () => {\n try {\n if (recorderState.isRecording || recorder.isRecording) {\n await recorder.stop().catch(() => undefined);\n }\n } catch {\n /* best-effort */\n }\n try {\n /*\n * Releasing the recording audio session matters more on iOS — leaving\n * `allowsRecording: true` will dim playback for the rest of the app\n * lifecycle. We tolerate failures because the AVAudioSession may\n * already be torn down by the time we get here.\n */\n await setAudioModeAsync({ allowsRecording: false, playsInSilentMode: true });\n } catch {\n /* best-effort */\n }\n }, [recorder, recorderState.isRecording]);\n\n /**\n * One-shot finalize: stop → upload → emit transcript. Swallowed errors get\n * surfaced via `onError` so the host can show a toast. We always clear\n * `sendInFlightRef` even on error so a retry path is possible if needed.\n */\n const finalize = useCallback(async () => {\n if (sendInFlightRef.current || isTranscribing) return;\n sendInFlightRef.current = true;\n if (isMountedRef.current) setIsTranscribing(true);\n try {\n await stopAndCleanup();\n const uri = recorder.uri;\n if (!uri) {\n throw new Error('No recording produced');\n }\n const text = await transcribeAudio({ uri, mimeType: 'audio/m4a', filename: 'audio.m4a' });\n if (isMountedRef.current) {\n onTranscriptionComplete(text);\n }\n } catch (err) {\n const msg = err instanceof Error ? err.message : String(err);\n onError?.(msg);\n onCancel();\n } finally {\n sendInFlightRef.current = false;\n if (isMountedRef.current) setIsTranscribing(false);\n }\n }, [isTranscribing, recorder, stopAndCleanup, onTranscriptionComplete, onError, onCancel]);\n\n useEffect(() => {\n if (hasStartedRef.current) return;\n hasStartedRef.current = true;\n let cancelled = false;\n const start = async () => {\n try {\n /*\n * Defensive: in a release build the native expo-audio module\n * can be missing or partially linked, and reaching for\n * `AudioModule.foo` would throw a TurboModule error before any\n * of our specific handling could run. Surface it as a normal\n * error path instead.\n */\n if (!AudioModule || typeof AudioModule.getRecordingPermissionsAsync !== 'function') {\n throw new Error('Audio module is not available');\n }\n /*\n * Permission is requested upstream by `handleMicPress` BEFORE\n * this panel mounts (see HomeScreen/Chat — without that gate,\n * `useAudioRecorder` would construct the native AVAudioRecorder\n * during render, which hard-crashes iOS TestFlight on the new\n * architecture). Here we only verify the grant via the\n * non-prompting getter — no second alert.\n */\n const perm = await AudioModule.getRecordingPermissionsAsync();\n if (!perm?.granted) {\n throw new Error('Microphone permission denied');\n }\n /*\n * iOS needs `allowsRecording: true` to route the mic through\n * AVAudioSession's PlayAndRecord category. `playsInSilentMode`\n * is required so the silent switch doesn't kill any TTS the\n * app may use later in the same session.\n */\n await setAudioModeAsync({ allowsRecording: true, playsInSilentMode: true });\n await recorder.prepareToRecordAsync();\n if (cancelled || !isMountedRef.current) return;\n recorder.record();\n } catch (err) {\n const msg = err instanceof Error ? err.message : String(err);\n onError?.(msg);\n onCancel();\n }\n };\n void start();\n return () => {\n cancelled = true;\n };\n // Mount-only effect: recorder/start refs are stable for the life of the panel.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n useEffect(() => {\n return () => {\n isMountedRef.current = false;\n /*\n * Unmount cleanup: fire-and-forget. We don't await anything here\n * because React doesn't allow async destructors and we can't\n * guarantee the recorder shared object outlives this tick. The\n * stopAndCleanup util is null-safe.\n */\n void stopAndCleanup();\n };\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n /**\n * Max-duration safety net. `RecorderState.durationMillis` already reports\n * elapsed time in milliseconds (see `expo-audio` `Audio.types.ts`), which\n * lines up directly with our `MAX_DURATION_MS` ceiling (web parity).\n */\n useEffect(() => {\n if (!recorderState.isRecording) return;\n const elapsedMs = recorderState.durationMillis ?? 0;\n if (elapsedMs >= MAX_DURATION_MS && !sendInFlightRef.current) {\n void finalize();\n }\n }, [recorderState.isRecording, recorderState.durationMillis, finalize]);\n\n const handleCancelPress = useCallback(async () => {\n if (isTranscribing) return;\n await stopAndCleanup();\n onCancel();\n }, [isTranscribing, stopAndCleanup, onCancel]);\n\n const handleSendPress = useCallback(() => {\n void finalize();\n }, [finalize]);\n\n const formatted = useMemo(() => {\n const totalSeconds = Math.floor((recorderState.durationMillis ?? 0) / 1000);\n const m = Math.floor(totalSeconds / 60);\n const s = totalSeconds % 60;\n return `${m}:${s.toString().padStart(2, '0')}`;\n }, [recorderState.durationMillis]);\n\n const bg = isDark ? '#0f172a' : '#ffffff';\n const border = isDark ? 'rgba(148,163,184,0.18)' : '#e4e4e7';\n const fg = isDark ? '#e5e7eb' : '#18181b';\n const muted = isDark ? '#94a3b8' : '#71717a';\n const cancelBg = isDark ? 'rgba(148,163,184,0.08)' : '#f4f4f5';\n\n const showSpinner = isTranscribing;\n const isRecording = recorderState.isRecording && !showSpinner;\n const statusLabel = showSpinner ? 'Transcribing…' : isRecording ? formatted : 'Initializing…';\n\n return (\n <View\n accessibilityRole=\"summary\"\n accessibilityLabel=\"Voice recorder\"\n style={[styles.container, { backgroundColor: bg, borderColor: border }]}\n >\n <View style={styles.statusRow}>\n {showSpinner ? (\n <ActivityIndicator size=\"small\" color={fg} />\n ) : (\n <Animated.View style={[styles.recDot, { opacity: isRecording ? pulse : 0.35 }]} />\n )}\n <Animated.Text style={[styles.timer, { color: fg }]} accessibilityLiveRegion=\"polite\">\n {statusLabel}\n </Animated.Text>\n {isRecording && <Animated.Text style={[styles.maxLabel, { color: muted }]}>/ 3:00</Animated.Text>}\n </View>\n <View style={styles.actionsRow}>\n <Pressable\n onPress={handleCancelPress}\n disabled={isTranscribing}\n accessibilityLabel=\"Cancel recording\"\n accessibilityRole=\"button\"\n style={({ pressed }) => [\n styles.iconBtn,\n {\n backgroundColor: cancelBg,\n borderColor: border,\n opacity: isTranscribing ? 0.4 : pressed ? 0.7 : 1,\n },\n ]}\n >\n <Ionicons name=\"close\" size={16} color={fg} />\n </Pressable>\n <Pressable\n onPress={handleSendPress}\n disabled={isTranscribing || !isRecording}\n accessibilityLabel=\"Stop and transcribe\"\n accessibilityRole=\"button\"\n style={({ pressed }) => [\n styles.sendBtn,\n {\n opacity: isTranscribing ? 0.7 : !isRecording ? 0.5 : pressed ? 0.85 : 1,\n },\n ]}\n >\n {isTranscribing ? (\n <ActivityIndicator size=\"small\" color=\"#ffffff\" />\n ) : (\n <Feather name=\"arrow-up\" size={16} color=\"#ffffff\" />\n )}\n </Pressable>\n </View>\n </View>\n );\n}\n\nconst styles = StyleSheet.create({\n container: {\n flexDirection: 'row',\n alignItems: 'center',\n justifyContent: 'space-between',\n borderRadius: 24,\n borderWidth: 1,\n paddingVertical: 10,\n paddingHorizontal: 14,\n gap: 12,\n shadowColor: '#0f172a',\n shadowOpacity: 0.08,\n shadowRadius: 12,\n shadowOffset: { width: 0, height: 4 },\n elevation: 4,\n },\n statusRow: {\n flexDirection: 'row',\n alignItems: 'center',\n gap: 8,\n flexShrink: 1,\n },\n recDot: {\n width: 8,\n height: 8,\n borderRadius: 4,\n backgroundColor: '#ef4444',\n },\n timer: {\n fontSize: 13,\n fontWeight: '600',\n fontVariant: ['tabular-nums'],\n },\n maxLabel: {\n fontSize: 11,\n },\n actionsRow: {\n flexDirection: 'row',\n alignItems: 'center',\n gap: 8,\n },\n iconBtn: {\n width: 32,\n height: 32,\n borderRadius: 16,\n borderWidth: 1,\n alignItems: 'center',\n justifyContent: 'center',\n },\n sendBtn: {\n width: 32,\n height: 32,\n borderRadius: 16,\n alignItems: 'center',\n justifyContent: 'center',\n backgroundColor: '#18181b',\n },\n});\n\nexport default AudioRecorderPanel;\n"],"names":[],"mappings":"kZAYA,MAAM,eAAA,GAAkB,IAAI,EAAK,GAAA,GAAA;AAmC1B,SAAS,kBAAmB,CAAA;AAAA,EACjC,MAAA;AAAA,EACA,uBAAA;AAAA,EACA,QAAA;AAAA,EACA;AACF,CAA4B,EAAA;AAC1B,EAAM,MAAA,QAAA,GAAW,gBAAiB,CAAA,gBAAA,CAAiB,YAAY,CAAA;AAC/D,EAAM,MAAA,aAAA,GAAgB,qBAAsB,CAAA,QAAA,EAAU,GAAG,CAAA;AACzD,EAAA,MAAM,CAAC,cAAA,EAAgB,iBAAiB,CAAA,GAAI,SAAS,KAAK,CAAA;AAQ1D,EAAM,MAAA,aAAA,GAAgB,OAAO,KAAK,CAAA;AAClC,EAAM,MA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AAc,EAAA,EAAA;AAAA,IACd,WAAa,EAAA,CAAA;AAAA,IACb,UAAY,EAAA,QAAA;AAAA,IACZ,cAAgB,EAAA;AAAA,GAClB;AAAA,EACA,OAAS,EAAA;AAAA,IACP,KAAO,EAAA,EAAA;AAAA,IACP,MAAQ,EAAA,EAAA;AAAA,IACR,YAAc,EAAA,EAAA;AAAA,IACd,UAAY,EAAA,QAAA;AAAA,IACZ,cAAgB,EAAA,QAAA;AAAA,IAChB,eAAiB,EAAA;AAAA;AAErB,CAAC,CAAA"}
@@ -0,0 +1,34 @@
1
+ import React from'react';var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, key + "" , value);
4
+ class MicErrorBoundary extends React.Component {
5
+ constructor() {
6
+ super(...arguments);
7
+ __publicField(this, "state", {
8
+ hasError: false
9
+ });
10
+ }
11
+ static getDerivedStateFromError() {
12
+ return {
13
+ hasError: true
14
+ };
15
+ }
16
+ componentDidCatch(error) {
17
+ var _a, _b;
18
+ const msg = (error == null ? void 0 : error.message) ? error.message : "Voice input failed to start.";
19
+ try {
20
+ (_b = (_a = this.props).onError) == null ? void 0 : _b.call(_a, msg);
21
+ } catch (e) {
22
+ }
23
+ try {
24
+ this.props.onCancel();
25
+ } catch (e) {
26
+ }
27
+ }
28
+ render() {
29
+ if (this.state.hasError) {
30
+ return null;
31
+ }
32
+ return this.props.children;
33
+ }
34
+ }export{MicErrorBoundary,MicErrorBoundary as default};//# sourceMappingURL=MicErrorBoundary.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MicErrorBoundary.js","sources":["../../../src/features/audio-input/MicErrorBoundary.tsx"],"sourcesContent":["import React from 'react';\n\ninterface MicErrorBoundaryProps {\n /**\n * Soft-cancel callback fired when a render-time error is caught.\n * Host screens use this to dismiss the panel so the composer reappears.\n */\n onCancel: () => void;\n /** Optional error sink so the host can surface a banner / toast. */\n onError?: (message: string) => void;\n children: React.ReactNode;\n}\n\ninterface MicErrorBoundaryState {\n hasError: boolean;\n}\n\n/**\n * Catches synchronous render-time errors thrown by the audio-input subtree.\n *\n * Why it exists: `expo-audio`'s `useAudioRecorder(...)` constructs a native\n * `AVAudioRecorder` (iOS) / `MediaRecorder` (Android) during the React render\n * phase. If the native module isn't linked, the new-architecture bridge can't\n * resolve it, or the recorder constructor throws (e.g. permissions revoked\n * mid-session), the throw propagates out of render and — in a release build\n * with Hermes — kills the JS thread and crashes the app.\n *\n * Wrapping the panel in this boundary turns that hard crash into a graceful\n * cancel: the boundary catches the throw, hides the children, fires\n * `onError` (so the host can surface \"Voice input failed to start\"), and\n * fires `onCancel` (so the host can dismiss the panel and restore the\n * composer). The composer remains usable, the user sees a clear message,\n * and the app stays alive.\n *\n * This does NOT catch async/native crashes (e.g. AVAudioSession explosions\n * inside Objective-C). For those, the pre-permission gate in `handleMicPress`\n * is the primary defense — this boundary is the belt-and-suspenders fallback\n * for the JS render path.\n */\nexport class MicErrorBoundary extends React.Component<MicErrorBoundaryProps, MicErrorBoundaryState> {\n state: MicErrorBoundaryState = { hasError: false };\n\n static getDerivedStateFromError(): MicErrorBoundaryState {\n return { hasError: true };\n }\n\n componentDidCatch(error: Error) {\n const msg = error?.message ? error.message : 'Voice input failed to start.';\n try {\n this.props.onError?.(msg);\n } catch {\n /* host error sink should never re-throw, but stay defensive */\n }\n try {\n this.props.onCancel();\n } catch {\n /* same — onCancel is \"soft\", never re-throws */\n }\n }\n\n render() {\n if (this.state.hasError) {\n return null;\n }\n return this.props.children;\n }\n}\n\nexport default MicErrorBoundary;\n"],"names":[],"mappings":";;;AAqCa,MAAA,gBAAA,SAAyB,MAAM,SAAwD,CAAA;AAAA,EAA7F,WAAA,GAAA;AAAA,IAAA,KAAA,CAAA,GAAA,SAAA,CAAA;AACL,IAA+B,aAAA,CAAA,IAAA,EAAA,OAAA,EAAA;AAAA,MAC7B,QAAU,EAAA;AAAA,KACZ,CAAA;AAAA;AAAA,EACA,OAAO,wBAAkD,GAAA;AACvD,IAAO,OAAA;AAAA,MACL,QAAU,EAAA;AAAA,KACZ;AAAA;AACF,EACA,kBAAkB,KAAc,EAAA;AA9ClC,IAAA,IAAA,EAAA,EAAA,EAAA;AA+CI,IAAA,MAAM,GAAM,GAAA,CAAA,KAAA,IAAA,IAAA,GAAA,MAAA,GAAA,KAAA,CAAO,OAAU,IAAA,KAAA,CAAM,OAAU,GAAA,8BAAA;AAC7C,IAAI,IAAA;AACF,MAAK,CAAA,EAAA,GAAA,CAAA,EAAA,GAAA,IAAA,CAAA,KAAA,EAAM,YAAX,IAAqB,GAAA,KAAA,CAAA,GAAA,EAAA,CAAA,IAAA,CAAA,EAAA,EAAA,GAAA,CAAA;AAAA,KACf,CAAA,OAAA,CAAA,EAAA;AAAA;AAGR,IAAI,IAAA;AACF,MAAA,IAAA,CAAK,MAAM,QAAS,EAAA;AAAA,KACd,CAAA,OAAA,CAAA,EAAA;AAAA;AAER;AACF,EACA,MAAS,GAAA;AACP,IAAI,IAAA,IAAA,CAAK,MAAM,QAAU,EAAA;AACvB,MAAO,OAAA,IAAA;AAAA;AAET,IAAA,OAAO,KAAK,KAAM,CAAA,QAAA;AAAA;AAEtB"}
@@ -40,33 +40,53 @@ function useCdecliAutoConnect(isSelected, channelId) {
40
40
  const [revealTokenMutation] = useRevealSecretApiTokenMutation();
41
41
  const {
42
42
  data: systemTokenData,
43
+ loading: systemTokenLoading,
43
44
  refetch: refetchSystemToken
44
45
  } = useGetUserSystemTokenQuery({
45
46
  skip: !isSelected,
46
47
  fetchPolicy: "network-only"
47
48
  });
49
+ const isAlreadyHasSystemTokenError = (err) => {
50
+ const message = err instanceof Error ? err.message : String(err != null ? err : "");
51
+ return /already have a system token/i.test(message);
52
+ };
48
53
  const obtainSystemToken = useCallback(async () => {
49
- var _a, _b, _c, _d;
54
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
50
55
  try {
51
56
  let tokenId = (_b = (_a = systemTokenData == null ? void 0 : systemTokenData.getUserSystemToken) == null ? void 0 : _a.id) != null ? _b : null;
52
57
  if (!tokenId) {
53
- if (!orgName || !projectId) return null;
54
- const {
55
- data: createData
56
- } = await createTokenMutation({
57
- variables: {
58
- input: {
59
- name: "System Token",
60
- expiryInSeconds: 31536e3,
61
- isSystemToken: true,
62
- environmentTagId: tagId,
63
- orgName: orgName || "default",
64
- projectId: projectId || "default"
58
+ if (!orgName || !projectId) {
59
+ const refetchResult = await refetchSystemToken().catch(() => null);
60
+ tokenId = (_e = (_d = (_c = refetchResult == null ? void 0 : refetchResult.data) == null ? void 0 : _c.getUserSystemToken) == null ? void 0 : _d.id) != null ? _e : null;
61
+ if (!tokenId) return null;
62
+ } else {
63
+ try {
64
+ const {
65
+ data: createData
66
+ } = await createTokenMutation({
67
+ variables: {
68
+ input: {
69
+ name: "System Token",
70
+ expiryInSeconds: 31536e3,
71
+ isSystemToken: true,
72
+ environmentTagId: tagId,
73
+ orgName: orgName || "default",
74
+ projectId: projectId || "default"
75
+ }
76
+ }
77
+ });
78
+ tokenId = (_g = (_f = createData == null ? void 0 : createData.createSecretApiToken) == null ? void 0 : _f.id) != null ? _g : null;
79
+ await refetchSystemToken();
80
+ } catch (createErr) {
81
+ if (!isAlreadyHasSystemTokenError(createErr)) throw createErr;
82
+ const refetchResult = await refetchSystemToken().catch(() => null);
83
+ tokenId = (_j = (_i = (_h = refetchResult == null ? void 0 : refetchResult.data) == null ? void 0 : _h.getUserSystemToken) == null ? void 0 : _i.id) != null ? _j : null;
84
+ if (!tokenId) {
85
+ console.warn("[useCdecliAutoConnect] System token already exists but refetch returned no id; retrying");
86
+ return null;
65
87
  }
66
88
  }
67
- });
68
- tokenId = (_d = (_c = createData == null ? void 0 : createData.createSecretApiToken) == null ? void 0 : _c.id) != null ? _d : null;
69
- await refetchSystemToken();
89
+ }
70
90
  }
71
91
  if (!tokenId) return null;
72
92
  const {
@@ -88,8 +108,11 @@ function useCdecliAutoConnect(isSelected, channelId) {
88
108
  return;
89
109
  }
90
110
  if (prerequisitesLoading || !accountUserId) return;
111
+ if (systemTokenLoading) return;
112
+ let isSilentReconnect = false;
91
113
  if (connectedRef.current || connectingRef.current) {
92
114
  if (channelId && prevChannelIdRef.current !== channelId) {
115
+ isSilentReconnect = connectedRef.current;
93
116
  prevChannelIdRef.current = channelId;
94
117
  connectedRef.current = false;
95
118
  } else {
@@ -99,7 +122,7 @@ function useCdecliAutoConnect(isSelected, channelId) {
99
122
  const doConnect = async () => {
100
123
  var _a;
101
124
  connectingRef.current = true;
102
- setStatus("connecting");
125
+ if (!isSilentReconnect) setStatus("connecting");
103
126
  setError(null);
104
127
  try {
105
128
  const endpoint = config.CDECLI_AGENT_ENDPOINT || "https://cdecli-agent.cdebase.dev";
@@ -151,7 +174,7 @@ function useCdecliAutoConnect(isSelected, channelId) {
151
174
  }
152
175
  };
153
176
  doConnect();
154
- }, [isSelected, obtainSystemToken, connectMutation, accountId, prerequisitesLoading, accountUserId, channelId]);
177
+ }, [isSelected, obtainSystemToken, connectMutation, accountId, prerequisitesLoading, accountUserId, channelId, systemTokenLoading]);
155
178
  const connectWithSkill = useCallback(async (skill, model, systemPrompt, skillId) => {
156
179
  var _a;
157
180
  console.log("[useCdecliAutoConnect] connectWithSkill called with skill=%s skillId=%s model=%s systemPrompt=%d chars", skill, skillId != null ? skillId : "", model, (_a = systemPrompt == null ? void 0 : systemPrompt.length) != null ? _a : 0);
@@ -1 +1 @@
1
- {"version":3,"file":"useCdecliAutoConnect.js","sources":["../../src/hooks/useCdecliAutoConnect.ts"],"sourcesContent":["/**\n * Auto-connects the cdecli-serve messenger-gateway channel when the CDeCLI\n * gateway is selected from the dropdown. Obtains/creates a system token and\n * calls the `gatewayConnect` mutation so the backend provider has a live\n * session with the cdecli-agent endpoint.\n */\n\nimport { useCallback, useEffect, useRef, useState } from 'react';\nimport {\n useGatewayConnectMutation,\n useGatewayDisconnectMutation,\n useGetUserSystemTokenQuery,\n useCreateSecretApiTokenMutation,\n useRevealSecretApiTokenMutation,\n} from 'common/graphql';\nimport { config } from '../config/env-config';\nimport { usePrerequisiteIds } from './usePrerequisiteIds';\n\n// ─── Types ───────────────────────────────────────────────────────\n\nexport type CdecliAutoConnectStatus = 'idle' | 'connecting' | 'connected' | 'error';\n\nexport interface UseCdecliAutoConnectResult {\n /** Whether the backend cdecli-serve channel is connected. */\n channelConnected: boolean;\n /** Current connection status. */\n status: CdecliAutoConnectStatus;\n /** Error message if status is 'error'. */\n error: string | null;\n /** Persistence mode reported by the backend provider ('backend' | 'frontend'). */\n persistenceMode: 'backend' | 'frontend' | undefined;\n /** Active skill name on the current CDeCLI session (null if none). */\n activeSkill: string | null;\n /** Per-user accountId used for this gateway connection. */\n accountId: string;\n /** Reconnect the CDeCLI session with a specific skill pre-activated. */\n connectWithSkill: (skill: string, model?: string, systemPrompt?: string, skillId?: string) => Promise<void>;\n}\n\n// ─── Constants ───────────────────────────────────────────────────\n\nconst CHANNEL_TYPE = 'cdecli-serve';\n\n// ─── Hook ────────────────────────────────────────────────────────\n\n/**\n * When `isSelected` is true, auto-connects the cdecli-serve channel on the\n * messenger-gateway backend using the hosted endpoint and a system token.\n */\nexport function useCdecliAutoConnect(isSelected: boolean, channelId?: string): UseCdecliAutoConnectResult {\n const [status, setStatus] = useState<CdecliAutoConnectStatus>('idle');\n const [error, setError] = useState<string | null>(null);\n const [persistenceMode, setPersistenceMode] = useState<'backend' | 'frontend' | undefined>(undefined);\n const [activeSkill, setActiveSkill] = useState<string | null>(null);\n const connectingRef = useRef(false);\n const connectedRef = useRef(false);\n\n const { orgName, projectId, tagId, accountUserId, loading: prerequisitesLoading } = usePrerequisiteIds();\n\n // Per-user accountId so each user gets their own cdecli-agent session.\n // Wait for the profile query to load before connecting — otherwise all\n // users fall back to 'default' and share a single cdecli-agent session.\n const accountId = accountUserId ?? 'default';\n\n // Track the previous channelId so we can reconnect when the user opens a new chat.\n const prevChannelIdRef = useRef<string | undefined>(channelId);\n\n const [connectMutation] = useGatewayConnectMutation();\n const [disconnectMutation] = useGatewayDisconnectMutation();\n const [createTokenMutation] = useCreateSecretApiTokenMutation();\n const [revealTokenMutation] = useRevealSecretApiTokenMutation();\n const { data: systemTokenData, refetch: refetchSystemToken } = useGetUserSystemTokenQuery({\n skip: !isSelected,\n fetchPolicy: 'network-only',\n });\n\n const obtainSystemToken = useCallback(async (): Promise<string | null> => {\n try {\n let tokenId: string | null = systemTokenData?.getUserSystemToken?.id ?? null;\n\n if (!tokenId) {\n if (!orgName || !projectId) return null;\n const { data: createData } = await createTokenMutation({\n variables: {\n input: {\n name: 'System Token',\n expiryInSeconds: 31536000,\n isSystemToken: true,\n environmentTagId: tagId,\n orgName: orgName || 'default',\n projectId: projectId || 'default',\n },\n },\n });\n tokenId = createData?.createSecretApiToken?.id ?? null;\n await refetchSystemToken();\n }\n\n if (!tokenId) return null;\n\n const { data: revealData } = await revealTokenMutation({\n variables: { id: tokenId },\n });\n return (revealData?.revealSecretApiToken as string) || null;\n } catch (err) {\n console.error('[useCdecliAutoConnect] obtainSystemToken error:', err);\n return null;\n }\n }, [systemTokenData, createTokenMutation, revealTokenMutation, refetchSystemToken, orgName, projectId, tagId]);\n\n // Auto-connect when CDeCLI gateway is selected\n useEffect(() => {\n if (!isSelected) {\n // Reset when deselected, but don't disconnect — keep the session alive\n // so switching back is instant.\n connectingRef.current = false;\n return;\n }\n\n // Wait until the user profile has loaded so accountId is the real\n // user id instead of the 'default' fallback.\n if (prerequisitesLoading || !accountUserId) return;\n\n // Already connected or currently connecting\n if (connectedRef.current || connectingRef.current) {\n // But if channelId changed, force a reconnect to get a fresh agent session\n if (channelId && prevChannelIdRef.current !== channelId) {\n prevChannelIdRef.current = channelId;\n connectedRef.current = false;\n // Fall through to doConnect\n } else {\n return;\n }\n }\n\n const doConnect = async () => {\n connectingRef.current = true;\n setStatus('connecting');\n setError(null);\n\n try {\n const endpoint = config.CDECLI_AGENT_ENDPOINT || 'https://cdecli-agent.cdebase.dev';\n const token = await obtainSystemToken();\n if (!token) {\n console.warn('[useCdecliAutoConnect] No system token, connecting without auth');\n }\n\n const { data } = await connectMutation({\n variables: {\n input: {\n channelType: CHANNEL_TYPE,\n accountId,\n options: {\n endpoint: endpoint.trim(),\n ...(token ? { token } : {}),\n ...(channelId ? { chatId: channelId } : {}),\n },\n },\n },\n });\n\n const result = data?.gatewayConnect as\n | ((typeof data)['gatewayConnect'] & { persistenceMode?: string })\n | undefined;\n console.log(\n '[useCdecliAutoConnect] connectMutation result (account=%s):',\n accountId,\n JSON.stringify(result),\n );\n if (result?.persistenceMode) {\n setPersistenceMode(result.persistenceMode as 'backend' | 'frontend');\n }\n if (result?.state === 'CONNECTED' || result?.connected) {\n setStatus('connected');\n connectedRef.current = true;\n } else if (result?.state === 'ERROR') {\n setStatus('error');\n setError(result.lastError ?? 'Failed to connect to CDeCLI agent');\n connectedRef.current = false;\n } else {\n // Assume connecting — the subscription will push the final state\n setStatus('connected');\n connectedRef.current = true;\n }\n } catch (err) {\n const msg = err instanceof Error ? err.message : String(err);\n console.error('[useCdecliAutoConnect] connect error:', msg);\n setStatus('error');\n setError(msg);\n connectedRef.current = false;\n } finally {\n connectingRef.current = false;\n }\n };\n\n doConnect();\n }, [isSelected, obtainSystemToken, connectMutation, accountId, prerequisitesLoading, accountUserId, channelId]);\n\n // Keep the backend session alive across page refreshes and SPA navigations.\n // The backend CdecliServeProvider.connect() will reuse the existing session\n // if the endpoint hasn't changed, preserving conversation context.\n // Only disconnect when the user explicitly switches to a different gateway.\n\n /** Reconnect the CDeCLI session with a specific skill pre-activated. */\n const connectWithSkill = useCallback(\n async (skill: string, model?: string, systemPrompt?: string, skillId?: string) => {\n console.log(\n '[useCdecliAutoConnect] connectWithSkill called with skill=%s skillId=%s model=%s systemPrompt=%d chars',\n skill,\n skillId ?? '',\n model,\n systemPrompt?.length ?? 0,\n );\n setStatus('connecting');\n setError(null);\n\n try {\n const endpoint = config.CDECLI_AGENT_ENDPOINT || 'https://cdecli-agent.cdebase.dev';\n const token = await obtainSystemToken();\n\n console.log('[useCdecliAutoConnect] connectWithSkill sending mutation with skill=%s', skill);\n const { data } = await connectMutation({\n variables: {\n input: {\n channelType: CHANNEL_TYPE,\n accountId,\n options: {\n endpoint: endpoint.trim(),\n ...(token ? { token } : {}),\n skill,\n ...(skillId ? { skillId } : {}),\n ...(model ? { model } : {}),\n ...(systemPrompt ? { systemPrompt } : {}),\n ...(channelId ? { chatId: channelId } : {}),\n },\n },\n },\n });\n\n console.log('[useCdecliAutoConnect] connectWithSkill result:', JSON.stringify(data?.gatewayConnect));\n const result = data?.gatewayConnect as\n | ((typeof data)['gatewayConnect'] & { persistenceMode?: string })\n | undefined;\n if (result?.persistenceMode) {\n setPersistenceMode(result.persistenceMode as 'backend' | 'frontend');\n }\n if (result?.state === 'CONNECTED' || result?.connected) {\n setStatus('connected');\n connectedRef.current = true;\n setActiveSkill(skill);\n } else {\n setStatus('connected');\n connectedRef.current = true;\n setActiveSkill(skill);\n }\n } catch (err) {\n const msg = err instanceof Error ? err.message : String(err);\n console.error('[useCdecliAutoConnect] connectWithSkill error:', msg);\n setStatus('error');\n setError(msg);\n }\n },\n [connectMutation, obtainSystemToken, accountId, channelId],\n );\n\n return {\n channelConnected: status === 'connected',\n status,\n error,\n persistenceMode,\n activeSkill,\n accountId,\n connectWithSkill,\n };\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;AAkCA,MAAM,YAAe,GAAA,cAAA;AAQL,SAAA,oBAAA,CAAqB,YAAqB,SAAgD,EAAA;AACxG,EAAA,MAAM,CAAC,MAAA,EAAQ,SAAS,CAAA,GAAI,SAAkC,MAAM,CAAA;AACpE,EAAA,MAAM,CAAC,KAAA,EAAO,QAAQ,CAAA,GAAI,SAAwB,IAAI,CAAA;AACtD,EAAA,MAAM,CAAC,eAAA,EAAiB,kBAAkB,CAAA,GAAI,SAA6C,MAAS,CAAA;AACpG,EAAA,MAAM,CAAC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AAA,IACA,KAAA;AAAA,IACA,eAAA;AAAA,IACA,WAAA;AAAA,IACA,SAAA;AAAA,IACA;AAAA,GACF;AACF"}
1
+ {"version":3,"file":"useCdecliAutoConnect.js","sources":["../../src/hooks/useCdecliAutoConnect.ts"],"sourcesContent":["/**\n * Auto-connects the cdecli-serve messenger-gateway channel when the CDeCLI\n * gateway is selected from the dropdown. Obtains/creates a system token and\n * calls the `gatewayConnect` mutation so the backend provider has a live\n * session with the cdecli-agent endpoint.\n */\n\nimport { useCallback, useEffect, useRef, useState } from 'react';\nimport {\n useGatewayConnectMutation,\n useGatewayDisconnectMutation,\n useGetUserSystemTokenQuery,\n useCreateSecretApiTokenMutation,\n useRevealSecretApiTokenMutation,\n} from 'common/graphql';\nimport { config } from '../config/env-config';\nimport { usePrerequisiteIds } from './usePrerequisiteIds';\n\n// ─── Types ───────────────────────────────────────────────────────\n\nexport type CdecliAutoConnectStatus = 'idle' | 'connecting' | 'connected' | 'error';\n\nexport interface UseCdecliAutoConnectResult {\n /** Whether the backend cdecli-serve channel is connected. */\n channelConnected: boolean;\n /** Current connection status. */\n status: CdecliAutoConnectStatus;\n /** Error message if status is 'error'. */\n error: string | null;\n /** Persistence mode reported by the backend provider ('backend' | 'frontend'). */\n persistenceMode: 'backend' | 'frontend' | undefined;\n /** Active skill name on the current CDeCLI session (null if none). */\n activeSkill: string | null;\n /** Per-user accountId used for this gateway connection. */\n accountId: string;\n /** Reconnect the CDeCLI session with a specific skill pre-activated. */\n connectWithSkill: (skill: string, model?: string, systemPrompt?: string, skillId?: string) => Promise<void>;\n}\n\n// ─── Constants ───────────────────────────────────────────────────\n\nconst CHANNEL_TYPE = 'cdecli-serve';\n\n// ─── Hook ────────────────────────────────────────────────────────\n\n/**\n * When `isSelected` is true, auto-connects the cdecli-serve channel on the\n * messenger-gateway backend using the hosted endpoint and a system token.\n */\nexport function useCdecliAutoConnect(isSelected: boolean, channelId?: string): UseCdecliAutoConnectResult {\n const [status, setStatus] = useState<CdecliAutoConnectStatus>('idle');\n const [error, setError] = useState<string | null>(null);\n const [persistenceMode, setPersistenceMode] = useState<'backend' | 'frontend' | undefined>(undefined);\n const [activeSkill, setActiveSkill] = useState<string | null>(null);\n const connectingRef = useRef(false);\n const connectedRef = useRef(false);\n\n const { orgName, projectId, tagId, accountUserId, loading: prerequisitesLoading } = usePrerequisiteIds();\n\n // Per-user accountId so each user gets their own cdecli-agent session.\n // Wait for the profile query to load before connecting — otherwise all\n // users fall back to 'default' and share a single cdecli-agent session.\n const accountId = accountUserId ?? 'default';\n\n // Track the previous channelId so we can reconnect when the user opens a new chat.\n const prevChannelIdRef = useRef<string | undefined>(channelId);\n\n const [connectMutation] = useGatewayConnectMutation();\n const [disconnectMutation] = useGatewayDisconnectMutation();\n const [createTokenMutation] = useCreateSecretApiTokenMutation();\n const [revealTokenMutation] = useRevealSecretApiTokenMutation();\n const {\n data: systemTokenData,\n loading: systemTokenLoading,\n refetch: refetchSystemToken,\n } = useGetUserSystemTokenQuery({\n skip: !isSelected,\n fetchPolicy: 'network-only',\n });\n\n /** True when the backend rejects createSecretApiToken because the user already has one. */\n const isAlreadyHasSystemTokenError = (err: unknown): boolean => {\n const message = err instanceof Error ? err.message : String(err ?? '');\n return /already have a system token/i.test(message);\n };\n\n const obtainSystemToken = useCallback(async (): Promise<string | null> => {\n try {\n let tokenId: string | null = systemTokenData?.getUserSystemToken?.id ?? null;\n\n /**\n * Race recovery: if the GetUserSystemToken query hasn't returned yet (or returned\n * stale data) we'll fall through to createSecretApiToken. The backend rejects that\n * with \"you already have a system token\" — catch it, refetch, and use the existing\n * token id instead of falling back to an unauthenticated connection.\n */\n if (!tokenId) {\n if (!orgName || !projectId) {\n const refetchResult = await refetchSystemToken().catch(() => null);\n tokenId = refetchResult?.data?.getUserSystemToken?.id ?? null;\n if (!tokenId) return null;\n } else {\n try {\n const { data: createData } = await createTokenMutation({\n variables: {\n input: {\n name: 'System Token',\n expiryInSeconds: 31536000,\n isSystemToken: true,\n environmentTagId: tagId,\n orgName: orgName || 'default',\n projectId: projectId || 'default',\n },\n },\n });\n tokenId = createData?.createSecretApiToken?.id ?? null;\n await refetchSystemToken();\n } catch (createErr) {\n if (!isAlreadyHasSystemTokenError(createErr)) throw createErr;\n const refetchResult = await refetchSystemToken().catch(() => null);\n tokenId = refetchResult?.data?.getUserSystemToken?.id ?? null;\n if (!tokenId) {\n console.warn(\n '[useCdecliAutoConnect] System token already exists but refetch returned no id; retrying',\n );\n return null;\n }\n }\n }\n }\n\n if (!tokenId) return null;\n\n const { data: revealData } = await revealTokenMutation({\n variables: { id: tokenId },\n });\n return (revealData?.revealSecretApiToken as string) || null;\n } catch (err) {\n console.error('[useCdecliAutoConnect] obtainSystemToken error:', err);\n return null;\n }\n }, [systemTokenData, createTokenMutation, revealTokenMutation, refetchSystemToken, orgName, projectId, tagId]);\n\n // Auto-connect when CDeCLI gateway is selected\n useEffect(() => {\n if (!isSelected) {\n // Reset when deselected, but don't disconnect — keep the session alive\n // so switching back is instant.\n connectingRef.current = false;\n return;\n }\n\n // Wait until the user profile has loaded so accountId is the real\n // user id instead of the 'default' fallback.\n if (prerequisitesLoading || !accountUserId) return;\n\n /**\n * Wait for the GetUserSystemToken query to resolve before we connect — otherwise we\n * race the network and try to create a duplicate token, which the backend rejects with\n * \"you already have a system token\" and we fall back to an unauthenticated session.\n */\n if (systemTokenLoading) return;\n\n /**\n * If we're already connected and just switching channels, the gateway needs a reconnect\n * (the session is bound server-side to chatId), but the user already had a working\n * connection. Skip flipping the visible status to 'connecting' so the toolbar pill\n * doesn't briefly show \"Checking...\" every time someone taps a history row.\n */\n let isSilentReconnect = false;\n if (connectedRef.current || connectingRef.current) {\n if (channelId && prevChannelIdRef.current !== channelId) {\n isSilentReconnect = connectedRef.current;\n prevChannelIdRef.current = channelId;\n connectedRef.current = false;\n } else {\n return;\n }\n }\n\n const doConnect = async () => {\n connectingRef.current = true;\n if (!isSilentReconnect) setStatus('connecting');\n setError(null);\n\n try {\n const endpoint = config.CDECLI_AGENT_ENDPOINT || 'https://cdecli-agent.cdebase.dev';\n const token = await obtainSystemToken();\n if (!token) {\n console.warn('[useCdecliAutoConnect] No system token, connecting without auth');\n }\n\n const { data } = await connectMutation({\n variables: {\n input: {\n channelType: CHANNEL_TYPE,\n accountId,\n options: {\n endpoint: endpoint.trim(),\n ...(token ? { token } : {}),\n ...(channelId ? { chatId: channelId } : {}),\n },\n },\n },\n });\n\n const result = data?.gatewayConnect as\n | ((typeof data)['gatewayConnect'] & { persistenceMode?: string })\n | undefined;\n console.log(\n '[useCdecliAutoConnect] connectMutation result (account=%s):',\n accountId,\n JSON.stringify(result),\n );\n if (result?.persistenceMode) {\n setPersistenceMode(result.persistenceMode as 'backend' | 'frontend');\n }\n if (result?.state === 'CONNECTED' || result?.connected) {\n setStatus('connected');\n connectedRef.current = true;\n } else if (result?.state === 'ERROR') {\n setStatus('error');\n setError(result.lastError ?? 'Failed to connect to CDeCLI agent');\n connectedRef.current = false;\n } else {\n // Assume connecting — the subscription will push the final state\n setStatus('connected');\n connectedRef.current = true;\n }\n } catch (err) {\n const msg = err instanceof Error ? err.message : String(err);\n console.error('[useCdecliAutoConnect] connect error:', msg);\n setStatus('error');\n setError(msg);\n connectedRef.current = false;\n } finally {\n connectingRef.current = false;\n }\n };\n\n doConnect();\n }, [\n isSelected,\n obtainSystemToken,\n connectMutation,\n accountId,\n prerequisitesLoading,\n accountUserId,\n channelId,\n systemTokenLoading,\n ]);\n\n // Keep the backend session alive across page refreshes and SPA navigations.\n // The backend CdecliServeProvider.connect() will reuse the existing session\n // if the endpoint hasn't changed, preserving conversation context.\n // Only disconnect when the user explicitly switches to a different gateway.\n\n /** Reconnect the CDeCLI session with a specific skill pre-activated. */\n const connectWithSkill = useCallback(\n async (skill: string, model?: string, systemPrompt?: string, skillId?: string) => {\n console.log(\n '[useCdecliAutoConnect] connectWithSkill called with skill=%s skillId=%s model=%s systemPrompt=%d chars',\n skill,\n skillId ?? '',\n model,\n systemPrompt?.length ?? 0,\n );\n setStatus('connecting');\n setError(null);\n\n try {\n const endpoint = config.CDECLI_AGENT_ENDPOINT || 'https://cdecli-agent.cdebase.dev';\n const token = await obtainSystemToken();\n\n console.log('[useCdecliAutoConnect] connectWithSkill sending mutation with skill=%s', skill);\n const { data } = await connectMutation({\n variables: {\n input: {\n channelType: CHANNEL_TYPE,\n accountId,\n options: {\n endpoint: endpoint.trim(),\n ...(token ? { token } : {}),\n skill,\n ...(skillId ? { skillId } : {}),\n ...(model ? { model } : {}),\n ...(systemPrompt ? { systemPrompt } : {}),\n ...(channelId ? { chatId: channelId } : {}),\n },\n },\n },\n });\n\n console.log('[useCdecliAutoConnect] connectWithSkill result:', JSON.stringify(data?.gatewayConnect));\n const result = data?.gatewayConnect as\n | ((typeof data)['gatewayConnect'] & { persistenceMode?: string })\n | undefined;\n if (result?.persistenceMode) {\n setPersistenceMode(result.persistenceMode as 'backend' | 'frontend');\n }\n if (result?.state === 'CONNECTED' || result?.connected) {\n setStatus('connected');\n connectedRef.current = true;\n setActiveSkill(skill);\n } else {\n setStatus('connected');\n connectedRef.current = true;\n setActiveSkill(skill);\n }\n } catch (err) {\n const msg = err instanceof Error ? err.message : String(err);\n console.error('[useCdecliAutoConnect] connectWithSkill error:', msg);\n setStatus('error');\n setError(msg);\n }\n },\n [connectMutation, obtainSystemToken, accountId, channelId],\n );\n\n return {\n channelConnected: status === 'connected',\n status,\n error,\n persistenceMode,\n activeSkill,\n accountId,\n connectWithSkill,\n };\n}\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;AAkCA,MAAM,YAAe,GAAA,cAAA;AAQL,SAAA,oBAAA,CAAqB,YAAqB,SAAgD,EAAA;AACxG,EAAA,MAAM,CAAC,MAAA,EAAQ,SAAS,CAAA,GAAI,SAAkC,MAAM,CAAA;AACpE,EAAA,MAAM,CAAC,KAAA,EAAO,QAAQ,CAAA,GAAI,SAAwB,IAAI,CAAA;AACtD,EAAA,MAAM,CAAC,eAAA,EAAiB,kBAAk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