mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-25 12:59:34 +00:00
* fix(codex): honor bound agent exec host policy * fix(codex): carry bound agent into app-server runtime * fix(codex): preserve main session exec overrides * fix(codex): narrow runtime session reads * fix(codex): honor default agent session aliases * fix(codex): isolate bound session exec overrides * fix(codex): honor unscoped default session policy
126 lines
3.8 KiB
TypeScript
126 lines
3.8 KiB
TypeScript
// Codex plugin module implements conversation binding data behavior.
|
|
import process from "node:process";
|
|
import type { PluginConversationBinding } from "openclaw/plugin-sdk/plugin-entry";
|
|
import { asOptionalRecord as readRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
|
|
|
|
const BINDING_DATA_VERSION = 1;
|
|
|
|
export type CodexAppServerConversationBindingData = {
|
|
kind: "codex-app-server-session";
|
|
version: 1;
|
|
sessionFile: string;
|
|
workspaceDir: string;
|
|
agentDir?: string;
|
|
agentId?: string;
|
|
};
|
|
|
|
export type CodexCliNodeConversationBindingData = {
|
|
kind: "codex-cli-node-session";
|
|
version: 1;
|
|
nodeId: string;
|
|
sessionId: string;
|
|
cwd?: string;
|
|
};
|
|
|
|
export type CodexConversationBindingData =
|
|
| CodexAppServerConversationBindingData
|
|
| CodexCliNodeConversationBindingData;
|
|
|
|
export function createCodexConversationBindingData(params: {
|
|
sessionFile: string;
|
|
workspaceDir: string;
|
|
agentDir?: string;
|
|
agentId?: string;
|
|
}): CodexAppServerConversationBindingData {
|
|
const agentDir = params.agentDir?.trim();
|
|
const agentId = params.agentId?.trim();
|
|
return {
|
|
kind: "codex-app-server-session",
|
|
version: BINDING_DATA_VERSION,
|
|
sessionFile: params.sessionFile,
|
|
workspaceDir: params.workspaceDir,
|
|
...(agentDir ? { agentDir } : {}),
|
|
...(agentId ? { agentId } : {}),
|
|
};
|
|
}
|
|
|
|
export function createCodexCliNodeConversationBindingData(params: {
|
|
nodeId: string;
|
|
sessionId: string;
|
|
cwd?: string;
|
|
}): CodexCliNodeConversationBindingData {
|
|
const cwd = params.cwd?.trim();
|
|
return {
|
|
kind: "codex-cli-node-session",
|
|
version: BINDING_DATA_VERSION,
|
|
nodeId: params.nodeId,
|
|
sessionId: params.sessionId,
|
|
...(cwd ? { cwd } : {}),
|
|
};
|
|
}
|
|
|
|
export function readCodexConversationBindingData(
|
|
binding: PluginConversationBinding | null | undefined,
|
|
): CodexConversationBindingData | undefined {
|
|
const data = binding?.data;
|
|
if (!data || typeof data !== "object" || Array.isArray(data)) {
|
|
return undefined;
|
|
}
|
|
return readCodexConversationBindingDataRecord(data);
|
|
}
|
|
|
|
export function readCodexConversationBindingDataRecord(
|
|
data: Record<string, unknown>,
|
|
): CodexConversationBindingData | undefined {
|
|
if (data.kind === "codex-cli-node-session") {
|
|
if (
|
|
data.version !== BINDING_DATA_VERSION ||
|
|
typeof data.nodeId !== "string" ||
|
|
!data.nodeId.trim() ||
|
|
typeof data.sessionId !== "string" ||
|
|
!data.sessionId.trim()
|
|
) {
|
|
return undefined;
|
|
}
|
|
return {
|
|
kind: "codex-cli-node-session",
|
|
version: BINDING_DATA_VERSION,
|
|
nodeId: data.nodeId.trim(),
|
|
sessionId: data.sessionId.trim(),
|
|
cwd: typeof data.cwd === "string" && data.cwd.trim() ? data.cwd.trim() : undefined,
|
|
};
|
|
}
|
|
if (data.kind !== "codex-app-server-session") {
|
|
return undefined;
|
|
}
|
|
if (
|
|
data.version !== BINDING_DATA_VERSION ||
|
|
typeof data.sessionFile !== "string" ||
|
|
!data.sessionFile.trim()
|
|
) {
|
|
return undefined;
|
|
}
|
|
return {
|
|
kind: "codex-app-server-session",
|
|
version: BINDING_DATA_VERSION,
|
|
sessionFile: data.sessionFile,
|
|
workspaceDir:
|
|
typeof data.workspaceDir === "string" && data.workspaceDir.trim()
|
|
? data.workspaceDir
|
|
: process.cwd(),
|
|
agentDir: typeof data.agentDir === "string" && data.agentDir.trim() ? data.agentDir : undefined,
|
|
agentId: typeof data.agentId === "string" && data.agentId.trim() ? data.agentId : undefined,
|
|
};
|
|
}
|
|
|
|
export function resolveCodexDefaultWorkspaceDir(pluginConfig: unknown): string {
|
|
const appServer = readRecord(readRecord(pluginConfig)?.appServer);
|
|
const configured = readString(appServer, "defaultWorkspaceDir");
|
|
return configured ?? process.cwd();
|
|
}
|
|
|
|
function readString(record: Record<string, unknown> | undefined, key: string) {
|
|
const value = record?.[key];
|
|
return typeof value === "string" && value.trim() ? value.trim() : undefined;
|
|
}
|