Matthias Nott
2026-03-02 a0f39302919fbacf7a0d407f01b1a50413ea6f70
contexts/ConnectionContext.tsx
....@@ -7,7 +7,12 @@
77 useState,
88 } from "react";
99 import * as SecureStore from "expo-secure-store";
10
-import { ConnectionStatus, ServerConfig, WebSocketMessage } from "../types";
10
+import {
11
+ ConnectionStatus,
12
+ ServerConfig,
13
+ WsIncoming,
14
+ WsOutgoing,
15
+} from "../types";
1116 import { wsClient } from "../services/websocket";
1217
1318 const SECURE_STORE_KEY = "pailot_server_config";
....@@ -19,9 +24,10 @@
1924 disconnect: () => void;
2025 sendTextMessage: (text: string) => boolean;
2126 sendVoiceMessage: (audioBase64: string, transcript?: string) => boolean;
27
+ sendCommand: (command: string, args?: Record<string, unknown>) => boolean;
2228 saveServerConfig: (config: ServerConfig) => Promise<void>;
2329 onMessageReceived: React.MutableRefObject<
24
- ((data: WebSocketMessage) => void) | null
30
+ ((data: WsIncoming) => void) | null
2531 >;
2632 }
2733
....@@ -34,9 +40,7 @@
3440 }) {
3541 const [serverConfig, setServerConfig] = useState<ServerConfig | null>(null);
3642 const [status, setStatus] = useState<ConnectionStatus>("disconnected");
37
- const onMessageReceived = useRef<((data: WebSocketMessage) => void) | null>(
38
- null
39
- );
43
+ const onMessageReceived = useRef<((data: WsIncoming) => void) | null>(null);
4044
4145 useEffect(() => {
4246 loadConfig();
....@@ -48,7 +52,7 @@
4852 onClose: () => setStatus("disconnected"),
4953 onError: () => setStatus("disconnected"),
5054 onMessage: (data) => {
51
- onMessageReceived.current?.(data);
55
+ onMessageReceived.current?.(data as WsIncoming);
5256 },
5357 });
5458 }, []);
....@@ -92,18 +96,24 @@
9296 }, []);
9397
9498 const sendTextMessage = useCallback((text: string): boolean => {
95
- const msg: WebSocketMessage = { type: "text", content: text };
96
- return wsClient.send(msg);
99
+ return wsClient.send({ type: "text", content: text });
97100 }, []);
98101
99102 const sendVoiceMessage = useCallback(
100103 (audioBase64: string, transcript: string = ""): boolean => {
101
- const msg: WebSocketMessage = {
104
+ return wsClient.send({
102105 type: "voice",
103106 content: transcript,
104107 audioBase64,
105
- };
106
- return wsClient.send(msg);
108
+ });
109
+ },
110
+ []
111
+ );
112
+
113
+ const sendCommand = useCallback(
114
+ (command: string, args?: Record<string, unknown>): boolean => {
115
+ const msg: WsOutgoing = { type: "command", command, args };
116
+ return wsClient.send(msg as any);
107117 },
108118 []
109119 );
....@@ -117,6 +127,7 @@
117127 disconnect,
118128 sendTextMessage,
119129 sendVoiceMessage,
130
+ sendCommand,
120131 saveServerConfig,
121132 onMessageReceived,
122133 }}