



















@@ -31,23 +31,43 @@ const SHARED_CODEX_APP_SERVER_CLIENT_STATE = Symbol.for("openclaw.codexAppServer
31313232function getSharedCodexAppServerClientState(): SharedCodexAppServerClientState {
3333const globalState = globalThis as typeof globalThis & {
34-[SHARED_CODEX_APP_SERVER_CLIENT_STATE]?:
35-| SharedCodexAppServerClientState
36-| LegacySharedCodexAppServerClientState;
34+[SHARED_CODEX_APP_SERVER_CLIENT_STATE]?: unknown;
3735};
3836const state = globalState[SHARED_CODEX_APP_SERVER_CLIENT_STATE];
39-if (state?.clients instanceof Map) {
40-return state as SharedCodexAppServerClientState;
37+if (isSharedCodexAppServerClientState(state)) {
38+return state;
4139}
40+const legacyState = readLegacySharedCodexAppServerClientState(state);
4241const clients = new Map<string, SharedCodexAppServerClientEntry>();
43-if (state?.key && (state.client || state.promise)) {
44-clients.set(state.key, { client: state.client, promise: state.promise });
45-state.client?.addCloseHandler((closedClient) =>
46-clearSharedClientEntryIfCurrent(state.key!, closedClient),
42+if (legacyState?.key && (legacyState.client || legacyState.promise)) {
43+const legacyKey = legacyState.key;
44+clients.set(legacyKey, { client: legacyState.client, promise: legacyState.promise });
45+legacyState.client?.addCloseHandler((closedClient) =>
46+clearSharedClientEntryIfCurrent(legacyKey, closedClient),
4747);
4848}
49-globalState[SHARED_CODEX_APP_SERVER_CLIENT_STATE] = { clients };
50-return globalState[SHARED_CODEX_APP_SERVER_CLIENT_STATE];
49+const nextState: SharedCodexAppServerClientState = { clients };
50+globalState[SHARED_CODEX_APP_SERVER_CLIENT_STATE] = nextState;
51+return nextState;
52+}
53+54+function isSharedCodexAppServerClientState(
55+value: unknown,
56+): value is SharedCodexAppServerClientState {
57+return (
58+value !== null &&
59+typeof value === "object" &&
60+(value as { clients?: unknown }).clients instanceof Map
61+);
62+}
63+64+function readLegacySharedCodexAppServerClientState(
65+value: unknown,
66+): LegacySharedCodexAppServerClientState | undefined {
67+if (value === null || typeof value !== "object") {
68+return undefined;
69+}
70+return value as LegacySharedCodexAppServerClientState;
5171}
52725373export async function getSharedCodexAppServerClient(options?: {
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。