mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-08 10:02:56 +08:00
* test(codex): shorten placeholder auth fixture strings * feat(plugin-state): add reject-new overflow policy for keyed namespaces * feat(agents): thread agent identity and session generation through reset and hooks * refactor(codex): add SQLite-backed app-server thread binding store * refactor(codex): move app-server runtime callers onto the binding store * refactor(codex): move conversation, entry, and thread-tool flows onto the binding store * refactor(codex): move command handlers onto the binding store * feat(codex): import legacy binding sidecars via doctor state migration * fix(codex): keep doctor sidecar scan inside stateDir * fix(codex): make binding migration landable * chore(changelog): defer release note * test(plugin-state): stabilize durable-capacity ordering
224 lines
9.6 KiB
TypeScript
224 lines
9.6 KiB
TypeScript
/**
|
|
* Bundled Codex plugin entry: app-server harness, model provider, media
|
|
* understanding, migration provider, CLI-session commands, and binding hooks.
|
|
*/
|
|
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
|
|
import { mutateConfigFile } from "openclaw/plugin-sdk/config-mutation";
|
|
import { resolveLivePluginConfigObject } from "openclaw/plugin-sdk/plugin-config-runtime";
|
|
import { definePluginEntry } from "openclaw/plugin-sdk/plugin-entry";
|
|
import { createCodexAppServerAgentHarness } from "./harness.js";
|
|
import { buildCodexMediaUnderstandingProvider } from "./media-understanding-provider.js";
|
|
import { buildCodexProvider } from "./provider.js";
|
|
import {
|
|
CODEX_APP_SERVER_BINDING_MAX_ENTRIES,
|
|
CODEX_APP_SERVER_BINDING_NAMESPACE,
|
|
createLazyCodexAppServerBindingStore,
|
|
type StoredCodexAppServerBinding,
|
|
} from "./src/app-server/session-binding-store.js";
|
|
import type { CodexPluginsConfigBlock } from "./src/command-plugins-management.js";
|
|
import { createCodexCommand } from "./src/commands.js";
|
|
import {
|
|
handleCodexConversationBindingResolved,
|
|
handleCodexConversationInboundClaim,
|
|
} from "./src/conversation-binding.js";
|
|
import { buildCodexMigrationProvider } from "./src/migration/provider.js";
|
|
import { createCodexThreadsTool } from "./src/native-thread-tool.js";
|
|
import {
|
|
createCodexCliSessionNodeHostCommands,
|
|
createCodexCliSessionNodeInvokePolicies,
|
|
listCodexCliSessionsOnNode,
|
|
resumeCodexCliSessionOnNode,
|
|
resolveCodexCliSessionForBindingOnNode,
|
|
} from "./src/node-cli-sessions.js";
|
|
import { createCodexWebSearchProvider } from "./src/web-search-provider.js";
|
|
|
|
const ENDED_SESSION_REASONS: ReadonlySet<string> = new Set([
|
|
"new",
|
|
"reset",
|
|
"idle",
|
|
"daily",
|
|
"deleted",
|
|
]);
|
|
|
|
export default definePluginEntry({
|
|
id: "codex",
|
|
name: "Codex",
|
|
description: "Codex app-server harness and Codex-managed GPT model catalog.",
|
|
register(api) {
|
|
const resolveCurrentConfig = () =>
|
|
api.runtime.config?.current ? (api.runtime.config.current() as OpenClawConfig) : undefined;
|
|
const resolveCurrentPluginConfig = () =>
|
|
// Codex plugin config can change at runtime; resolve from live config for
|
|
// harness attempts and binding claims instead of keeping startup values.
|
|
resolveLivePluginConfigObject(
|
|
resolveCurrentConfig,
|
|
"codex",
|
|
api.pluginConfig as Record<string, unknown>,
|
|
) ?? api.pluginConfig;
|
|
const bindingStore = createLazyCodexAppServerBindingStore(
|
|
api.runtime.state.openSyncKeyedStore<StoredCodexAppServerBinding>({
|
|
namespace: CODEX_APP_SERVER_BINDING_NAMESPACE,
|
|
maxEntries: CODEX_APP_SERVER_BINDING_MAX_ENTRIES,
|
|
overflowPolicy: "reject-new",
|
|
}),
|
|
);
|
|
api.registerAgentHarness(
|
|
createCodexAppServerAgentHarness({
|
|
bindingStore,
|
|
resolveConfig: resolveCurrentConfig,
|
|
resolvePluginConfig: resolveCurrentPluginConfig,
|
|
}),
|
|
);
|
|
api.registerProvider(buildCodexProvider({ pluginConfig: api.pluginConfig }));
|
|
api.registerMediaUnderstandingProvider(
|
|
buildCodexMediaUnderstandingProvider({ pluginConfig: api.pluginConfig }),
|
|
);
|
|
api.registerWebSearchProvider(
|
|
createCodexWebSearchProvider({ resolvePluginConfig: resolveCurrentPluginConfig }),
|
|
);
|
|
api.registerMigrationProvider(buildCodexMigrationProvider({ runtime: api.runtime }));
|
|
api.registerTool(
|
|
(context) =>
|
|
createCodexThreadsTool({
|
|
bindingStore,
|
|
context,
|
|
runtime: api.runtime,
|
|
getPluginConfig: resolveCurrentPluginConfig,
|
|
}),
|
|
{ name: "codex_threads" },
|
|
);
|
|
api.registerToolMetadata({
|
|
toolName: "codex_threads",
|
|
displayName: "Codex Threads",
|
|
description: "Manage native Codex threads in the shared user Codex home.",
|
|
risk: "high",
|
|
tags: ["codex", "sessions"],
|
|
});
|
|
for (const command of createCodexCliSessionNodeHostCommands()) {
|
|
api.registerNodeHostCommand(command);
|
|
}
|
|
for (const policy of createCodexCliSessionNodeInvokePolicies()) {
|
|
api.registerNodeInvokePolicy(policy);
|
|
}
|
|
api.registerCommand(
|
|
createCodexCommand({
|
|
pluginConfig: api.pluginConfig,
|
|
resolvePluginConfig: resolveCurrentPluginConfig,
|
|
deps: {
|
|
bindingStore,
|
|
listCodexCliSessionsOnNode: (params) =>
|
|
listCodexCliSessionsOnNode({ runtime: api.runtime, ...params }),
|
|
resolveCodexCliSessionForBindingOnNode: (params) =>
|
|
resolveCodexCliSessionForBindingOnNode({ runtime: api.runtime, ...params }),
|
|
codexPluginsManagementIo: {
|
|
readConfig: () => {
|
|
const current = (api.runtime.config?.current?.() ?? {}) as OpenClawConfig;
|
|
const plugins = (current as Record<string, unknown>).plugins;
|
|
if (!plugins || typeof plugins !== "object") {
|
|
return Promise.resolve({});
|
|
}
|
|
const entries = (plugins as Record<string, unknown>).entries;
|
|
if (!entries || typeof entries !== "object") {
|
|
return Promise.resolve({});
|
|
}
|
|
const codexEntry = (entries as Record<string, unknown>).codex;
|
|
if (!codexEntry || typeof codexEntry !== "object") {
|
|
return Promise.resolve({});
|
|
}
|
|
const config = (codexEntry as Record<string, unknown>).config;
|
|
if (!config || typeof config !== "object") {
|
|
return Promise.resolve({});
|
|
}
|
|
const codexPlugins = (config as Record<string, unknown>).codexPlugins;
|
|
if (!codexPlugins || typeof codexPlugins !== "object") {
|
|
return Promise.resolve({});
|
|
}
|
|
const declared = (codexPlugins as Record<string, unknown>).plugins;
|
|
if (!declared || typeof declared !== "object") {
|
|
return Promise.resolve({
|
|
enabled: (codexPlugins as Record<string, unknown>).enabled === true,
|
|
});
|
|
}
|
|
return Promise.resolve({
|
|
enabled: (codexPlugins as Record<string, unknown>).enabled === true,
|
|
plugins: declared as Record<string, never>,
|
|
});
|
|
},
|
|
mutate: async (update) => {
|
|
await mutateConfigFile({
|
|
mutate: (draft) => {
|
|
// Create the nested plugin config path on demand so codex
|
|
// plugin commands can enable/update Codex-managed plugins.
|
|
const root = draft as Record<string, unknown>;
|
|
root.plugins = (root.plugins ?? {}) as Record<string, unknown>;
|
|
const pluginsBlock = root.plugins as Record<string, unknown>;
|
|
pluginsBlock.entries = (pluginsBlock.entries ?? {}) as Record<string, unknown>;
|
|
const entries = pluginsBlock.entries as Record<string, unknown>;
|
|
entries.codex = (entries.codex ?? {}) as Record<string, unknown>;
|
|
const codexEntry = entries.codex as Record<string, unknown>;
|
|
codexEntry.config = (codexEntry.config ?? {}) as Record<string, unknown>;
|
|
const config = codexEntry.config as Record<string, unknown>;
|
|
config.codexPlugins = (config.codexPlugins ?? {}) as Record<string, unknown>;
|
|
const codexPlugins = config.codexPlugins as Record<string, unknown>;
|
|
codexPlugins.plugins = (codexPlugins.plugins ?? {}) as Record<string, unknown>;
|
|
update(codexPlugins as CodexPluginsConfigBlock);
|
|
},
|
|
});
|
|
},
|
|
},
|
|
},
|
|
}),
|
|
);
|
|
api.on("inbound_claim", (event, ctx) =>
|
|
handleCodexConversationInboundClaim(event, ctx, {
|
|
bindingStore,
|
|
pluginConfig: resolveCurrentPluginConfig(),
|
|
config: resolveCurrentConfig(),
|
|
resumeCodexCliSessionOnNode: (params) =>
|
|
resumeCodexCliSessionOnNode({ runtime: api.runtime, ...params }),
|
|
}),
|
|
);
|
|
api.onConversationBindingResolved?.((event) =>
|
|
handleCodexConversationBindingResolved(event, { bindingStore }),
|
|
);
|
|
api.on("after_compaction", async (event, ctx) => {
|
|
const previousSessionId = event.previousSessionId?.trim();
|
|
const sessionId = ctx.sessionId?.trim();
|
|
if (!previousSessionId || !sessionId || previousSessionId === sessionId) {
|
|
return;
|
|
}
|
|
const config = resolveCurrentConfig();
|
|
const sessionKey = ctx.sessionKey?.trim();
|
|
const { sessionBindingIdentity } = await import("./src/app-server/session-binding.js");
|
|
const identity = sessionBindingIdentity({
|
|
sessionId,
|
|
...(sessionKey ? { sessionKey } : {}),
|
|
...(ctx.agentId ? { agentId: ctx.agentId } : {}),
|
|
...(config ? { config } : {}),
|
|
});
|
|
const adopted = await bindingStore.adoptSessionGeneration(identity, previousSessionId);
|
|
if (adopted === "conflict") {
|
|
api.logger.warn?.(
|
|
`codex: could not adopt compacted session generation ${sessionId} (${adopted}); secondary native compaction will skip`,
|
|
);
|
|
}
|
|
});
|
|
api.on("session_end", async (event, ctx) => {
|
|
if (!event.reason || !ENDED_SESSION_REASONS.has(event.reason)) {
|
|
return;
|
|
}
|
|
const sessionKey = event.sessionKey ?? ctx.sessionKey;
|
|
const config = resolveCurrentConfig();
|
|
const { sessionBindingIdentity } = await import("./src/app-server/session-binding.js");
|
|
await bindingStore.retireSessionGeneration(
|
|
sessionBindingIdentity({
|
|
sessionId: event.sessionId,
|
|
...(sessionKey ? { sessionKey } : {}),
|
|
...(ctx.agentId ? { agentId: ctx.agentId } : {}),
|
|
...(config ? { config } : {}),
|
|
}),
|
|
);
|
|
});
|
|
},
|
|
});
|