refactor: 统一实时协议类型并扩展会话状态字段
This commit is contained in:
35
web/src/protocol.ts
Normal file
35
web/src/protocol.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
export type ClientMsgType = "hello" | "start" | "stop" | "paste" | "ping";
|
||||
|
||||
export type ServerMsgType =
|
||||
| "ready"
|
||||
| "state"
|
||||
| "start_ack"
|
||||
| "stop_ack"
|
||||
| "partial"
|
||||
| "final"
|
||||
| "pasted"
|
||||
| "error"
|
||||
| "pong";
|
||||
|
||||
export type SessionState = "idle" | "recording" | "stopping";
|
||||
|
||||
export interface ClientMsg {
|
||||
type: ClientMsgType;
|
||||
sessionId?: string;
|
||||
seq?: number;
|
||||
text?: string;
|
||||
version?: number;
|
||||
ts?: number;
|
||||
}
|
||||
|
||||
export interface ServerMsg {
|
||||
type: ServerMsgType;
|
||||
state?: SessionState;
|
||||
sessionId?: string;
|
||||
seq?: number;
|
||||
text?: string;
|
||||
message?: string;
|
||||
code?: string;
|
||||
retryable?: boolean;
|
||||
ts?: number;
|
||||
}
|
||||
@@ -13,9 +13,13 @@ type ConnectionStatus = "connected" | "disconnected" | "connecting";
|
||||
interface AppState {
|
||||
// Connection
|
||||
connectionStatus: ConnectionStatus;
|
||||
weakNetwork: boolean;
|
||||
// Recording
|
||||
recording: boolean;
|
||||
pendingStart: boolean;
|
||||
stopping: boolean;
|
||||
micReady: boolean;
|
||||
activeSessionId: string | null;
|
||||
// Preview
|
||||
previewText: string;
|
||||
previewActive: boolean;
|
||||
@@ -24,8 +28,12 @@ interface AppState {
|
||||
|
||||
// Actions
|
||||
setConnectionStatus: (status: ConnectionStatus) => void;
|
||||
setWeakNetwork: (weak: boolean) => void;
|
||||
setRecording: (recording: boolean) => void;
|
||||
setPendingStart: (pending: boolean) => void;
|
||||
setStopping: (stopping: boolean) => void;
|
||||
setMicReady: (ready: boolean) => void;
|
||||
setActiveSessionId: (sessionId: string | null) => void;
|
||||
setPreview: (text: string, isFinal: boolean) => void;
|
||||
clearPreview: () => void;
|
||||
addHistory: (text: string) => void;
|
||||
@@ -36,15 +44,23 @@ export const useAppStore = create<AppState>()(
|
||||
persist(
|
||||
(set, get) => ({
|
||||
connectionStatus: "connecting",
|
||||
weakNetwork: false,
|
||||
recording: false,
|
||||
pendingStart: false,
|
||||
stopping: false,
|
||||
micReady: false,
|
||||
activeSessionId: null,
|
||||
previewText: "",
|
||||
previewActive: false,
|
||||
history: [],
|
||||
|
||||
setConnectionStatus: (connectionStatus) => set({ connectionStatus }),
|
||||
setWeakNetwork: (weakNetwork) => set({ weakNetwork }),
|
||||
setRecording: (recording) => set({ recording }),
|
||||
setPendingStart: (pendingStart) => set({ pendingStart }),
|
||||
setStopping: (stopping) => set({ stopping }),
|
||||
setMicReady: (micReady) => set({ micReady }),
|
||||
setActiveSessionId: (activeSessionId) => set({ activeSessionId }),
|
||||
|
||||
setPreview: (text, isFinal) =>
|
||||
set({
|
||||
|
||||
Reference in New Issue
Block a user