openpencil/server/utils/codex-client.ts
Kayshen Xu b05ebb944a
V0.4.4 (#59)
* feat(ai): enhance Windows CLI binary resolution and connection handling

- Introduced functions to handle both .cmd and .ps1 wrappers for Windows installations, improving compatibility for CLI tools like Codex and Copilot.
- Updated connection logic in `connect-agent.ts` to utilize environment variables for Codex home directory, enhancing flexibility in locating configuration files.
- Added warning messages in connection results to inform users when no models are found, guiding them to run the CLI tools for model population.
- Modified the agent settings dialog to accommodate the new warning field in connection responses, improving user feedback during the connection process.

This update significantly enhances the user experience for Windows users by ensuring better handling of CLI binaries and providing clearer connection status information.

* feat(ai): improve Windows binary resolution for CLI tools

- Added `resolveWinExtension` function to handle extensionless binaries returned by the `where` command on Windows, ensuring compatibility with `.cmd` and `.ps1` wrappers.
- Updated `connect-agent.ts` and `copilot-client.ts` to utilize the new resolution function, enhancing the reliability of binary path lookups for Codex and Copilot.
- Enhanced logging to provide clearer information on resolved paths and their existence status.

This update significantly improves the handling of CLI binaries on Windows, ensuring users have a smoother experience when connecting to AI tools.

* feat(ai): enhance binary path handling in connect-agent

- Added logic to ensure the directory of the resolved OpenCode binary is included in the PATH environment variable, improving the execution of CLI tools.
- Enhanced logging to provide feedback when the binary directory is prepended to the PATH, aiding in troubleshooting and user awareness.

This update improves the reliability of connecting to AI tools by ensuring the necessary binaries are accessible during execution.

* feat(patching): add patch for @opencode-ai/sdk and update package configurations

- Introduced a new patch for the @opencode-ai/sdk version 1.2.6 to address specific issues.
- Updated package.json and bun.lock to include the new patchedDependencies section, ensuring the patch is applied during installation.
- Removed redundant binary path handling code in connect-agent.ts to streamline the connection process.

This update enhances the SDK's functionality while simplifying the connection logic for improved performance.

* fix(ai): add orchestrator fallback and three-way intent routing

Orchestrator now falls back to a heuristic plan when the model returns
non-JSON instead of throwing an error.

Intent classification upgraded from binary (DESIGN/CHAT) to three-way
(DESIGN_NEW/DESIGN_MODIFY/CHAT). Modification requests without an
explicit selection auto-target the last top-level frame on the active
page, preventing them from being misrouted to the orchestrator.

* chore: bump version to 0.4.4 and remove deprecated patch for @opencode-ai/sdk

- Updated package version in package.json from 0.4.3 to 0.4.4.
- Removed the patchedDependencies section for @opencode-ai/sdk as the patch is no longer needed.
- Added new utility function `buildSpawnClaudeCodeProcess` to enhance agent SDK functionality across multiple files.

* chore: bump version to 0.4.4 in package.json

* chore(electron): optimize application icon assets

Reduce icon file sizes for faster builds and smaller distribution.

* chore: remove deprecated patchedDependencies for @opencode-ai/sdk in bun.lock

- Eliminated the patchedDependencies section for @opencode-ai/sdk as the patch is no longer necessary, streamlining the dependency management in the project.

* fix(ai): improve Codex CLI error extraction for auth and structured log errors

Parse Codex's structured log format (<timestamp> ERROR <module>: <message>)
to surface real errors like expired auth tokens instead of the unhelpful
"Warning: no last agent message" fallback.

* fix(ai): update error handling and remove deprecated reasoning field

- Updated error checks in chat and generate handlers to ensure proper validation of required fields.
- Removed the deprecated reasoning field from the OpenCode SDK integration, streamlining the prompt parameters.
- Enhanced logging for system prompt injection errors to improve debugging capabilities.

* fix(ai): enhance Windows compatibility for Codex CLI execution

- Updated the handling of prompts on Windows to avoid parsing errors caused by shell escaping in PowerShell and cmd.exe.
- Introduced a temporary PowerShell script to manage prompt input and command execution, ensuring special characters are processed correctly.
- Refactored the argument passing mechanism to accommodate the new script-based approach.

* fix(electron): improve process cleanup and Windows compatibility

- Simplified the application quit logic to always call app.quit() on window close.
- Enhanced the before-quit event to ensure proper cleanup of the Nitro process and port file.
- Updated the Nitro process termination logic to use SIGKILL for reliable cleanup on non-Windows platforms.
- Refactored argument handling in the Codex CLI execution to utilize PowerShell array splatting for safer argument passing.

* feat(ai): add fallback for model parsing from Codex's latest-model.md

- Implemented a new function to parse model IDs from the latest-model.md file when models_cache.json is unavailable, enhancing compatibility for fresh installations.
- Updated logging to reflect the loading of models from the fallback method and improved error handling for model loading scenarios.

* feat(ai): update prompt structure for design generation assistant

- Revised the prompt format to enhance clarity and organization for the design generation assistant.
- Introduced clear section headers for system instructions and user tasks, improving user experience and guidance.

* refactor(ai): streamline prompt handling for Codex CLI execution

- Simplified the prompt input mechanism for all platforms by utilizing stdin mode, eliminating the need for temporary scripts on Windows.
- Improved argument passing to avoid shell escaping issues and command-line length limits, enhancing compatibility and reliability across environments.
- Updated the execution logic to handle prompts more efficiently, ensuring a smoother user experience.

---------

Co-authored-by: Fini <fini.yang@gmail.com>
2026-03-20 22:00:22 +08:00

313 lines
8.8 KiB
TypeScript

import { spawn } from 'node:child_process'
import { mkdtemp, readFile, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
type ThinkingMode = 'adaptive' | 'disabled' | 'enabled'
type ThinkingEffort = 'low' | 'medium' | 'high' | 'max'
interface CodexExecOptions {
model?: string
systemPrompt?: string
thinkingMode?: ThinkingMode
thinkingBudgetTokens?: number
effort?: ThinkingEffort
timeoutMs?: number
/** Paths to temporary image files to reference in the prompt */
imageFiles?: string[]
}
interface CodexCliResult {
text?: string
error?: string
}
const DEFAULT_CODEX_TIMEOUT_MS = 15 * 60 * 1000
/**
* Allowlist-based env filter for Codex CLI subprocess.
* Only passes through safe system vars and provider-specific prefixes.
* Prevents leaking secrets like ANTHROPIC_API_KEY, AWS_SECRET_KEY, GITHUB_TOKEN, etc.
*/
const CODEX_ENV_ALLOWLIST = new Set([
'PATH', 'HOME', 'TERM', 'LANG', 'SHELL', 'TMPDIR',
// Windows-essential vars
'SYSTEMROOT', 'COMSPEC', 'USERPROFILE', 'APPDATA', 'LOCALAPPDATA',
'PATHEXT', 'SYSTEMDRIVE', 'TEMP', 'TMP', 'HOMEDRIVE', 'HOMEPATH',
])
export function filterCodexEnv(
env: Record<string, string | undefined>,
): Record<string, string | undefined> {
const result: Record<string, string | undefined> = {}
for (const [k, v] of Object.entries(env)) {
if (CODEX_ENV_ALLOWLIST.has(k) || k.startsWith('OPENAI_') || k.startsWith('CODEX_')) {
result[k] = v
}
}
return result
}
export async function runCodexExec(
userPrompt: string,
options: CodexExecOptions = {},
): Promise<CodexCliResult> {
const tempDir = await mkdtemp(join(tmpdir(), 'openpencil-codex-'))
const outputPath = join(tempDir, 'last-message.txt')
const prompt = buildPrompt(options.systemPrompt, userPrompt, options.imageFiles)
const codexEffort = resolveCodexEffort(options.thinkingMode, options.effort)
const args = [
'exec',
'--json',
'--skip-git-repo-check',
'--sandbox',
'read-only',
'--output-last-message',
outputPath,
]
if (options.model) {
args.push('--model', options.model)
}
if (codexEffort) {
args.push('--config', `model_reasoning_effort=${codexEffort}`)
}
// On Windows, passing long prompts as command-line arguments causes
// shell escaping issues (PowerShell MissingExpression, special chars).
// Use codex's stdin mode (`-` as prompt arg) on all platforms — simpler
// and avoids command-line length limits.
args.push('-')
try {
const runResult = await executeCodexCommand(
args,
options.timeoutMs ?? DEFAULT_CODEX_TIMEOUT_MS,
prompt,
)
const finalText = await readFile(outputPath, 'utf-8').catch(() => '')
const normalizedText = finalText.trim() || runResult.text.trim()
if (normalizedText) {
return { text: normalizedText }
}
if (runResult.errors.length > 0) {
return { error: runResult.errors.join('; ') }
}
return { error: 'Codex returned no output.' }
} catch (error) {
return { error: error instanceof Error ? error.message : 'Codex execution failed' }
} finally {
await rm(tempDir, { recursive: true, force: true }).catch(() => {})
}
}
function buildPrompt(systemPrompt: string | undefined, userPrompt: string, imageFiles?: string[]): string {
const userText = userPrompt.trim()
const imageSection = imageFiles && imageFiles.length > 0
? '\n' + imageFiles.map((f) => `[Attached image: ${f} — read this file to see the image]`).join('\n')
: ''
if (!systemPrompt?.trim()) {
return userText + imageSection
}
return [
'You are a design generation assistant. Follow the guidelines below to produce the requested output.',
'',
'--- GUIDELINES ---',
systemPrompt.trim(),
'',
'--- TASK ---',
userText + imageSection,
].join('\n')
}
function resolveCodexEffort(
thinkingMode: ThinkingMode | undefined,
effort: ThinkingEffort | undefined,
): 'low' | 'medium' | 'high' | undefined {
if (thinkingMode === 'disabled') {
return 'low'
}
if (effort === 'max') {
return 'high'
}
if (effort === 'low' || effort === 'medium' || effort === 'high') {
return effort
}
if (thinkingMode === 'enabled') {
return 'medium'
}
return undefined
}
async function executeCodexCommand(
args: string[],
timeoutMs: number,
stdinText?: string,
): Promise<{ text: string; errors: string[] }> {
return await new Promise((resolve, reject) => {
const child = spawn('codex', args, {
env: filterCodexEnv(process.env as Record<string, string | undefined>),
stdio: [stdinText ? 'pipe' : 'ignore', 'pipe', 'pipe'],
// On Windows, npm-installed CLIs are .cmd scripts — need shell to resolve.
...(process.platform === 'win32' && { shell: true }),
})
// Pipe prompt via stdin (codex reads from stdin when `-` is the prompt arg)
if (stdinText && child.stdin) {
child.stdin.write(stdinText)
child.stdin.end()
}
let stdoutBuffer = ''
let stderrBuffer = ''
let textAccumulator = ''
const errors: string[] = []
const flushStdoutLine = (line: string) => {
const event = parseCodexJsonLine(line)
if (!event) return
if (event.text) {
textAccumulator += event.text
}
if (event.error) {
errors.push(event.error)
}
}
const timer = setTimeout(() => {
child.kill('SIGTERM')
reject(new Error(`Codex request timed out after ${Math.round(timeoutMs / 1000)}s.`))
}, timeoutMs)
child.stdout!.on('data', (chunk: Buffer) => {
stdoutBuffer += chunk.toString('utf-8')
let idx = stdoutBuffer.indexOf('\n')
while (idx >= 0) {
const line = stdoutBuffer.slice(0, idx).trim()
stdoutBuffer = stdoutBuffer.slice(idx + 1)
if (line) flushStdoutLine(line)
idx = stdoutBuffer.indexOf('\n')
}
})
child.stderr!.on('data', (chunk: Buffer) => {
stderrBuffer += chunk.toString('utf-8')
})
child.on('error', (err) => {
clearTimeout(timer)
reject(err)
})
child.on('close', (code) => {
clearTimeout(timer)
const tail = stdoutBuffer.trim()
if (tail) {
flushStdoutLine(tail)
}
if (code === 0) {
resolve({ text: textAccumulator, errors })
return
}
const stderrError = extractCodexCliError(stderrBuffer)
const fallback = errors[errors.length - 1]
reject(
new Error(
stderrError
|| fallback
|| `Codex exited with code ${code ?? 'unknown'}.`,
),
)
})
})
}
function parseCodexJsonLine(
line: string,
): { text?: string; error?: string } | null {
let parsed: Record<string, unknown>
try {
parsed = JSON.parse(line) as Record<string, unknown>
} catch {
return null
}
const type = typeof parsed.type === 'string' ? parsed.type : ''
if (type === 'error') {
const message = getStringField(parsed, ['message'])
return { error: message || 'Codex returned an unknown error.' }
}
// Common Codex JSONL stream events include deltas in "delta" or "text".
const text =
getStringField(parsed, ['delta'])
|| getStringField(parsed, ['text'])
|| getStringField(parsed, ['content'])
if (!text) return null
return { text }
}
function getStringField(
obj: Record<string, unknown>,
keys: string[],
): string | null {
for (const key of keys) {
const val = obj[key]
if (typeof val === 'string' && val.length > 0) {
return val
}
}
return null
}
function extractCodexCliError(stderr: string): string | null {
const trimmed = stderr.trim()
if (!trimmed) return null
const lines = trimmed.split('\n').map((line) => line.trim()).filter(Boolean)
// 1. Look for "error: ..." lines (simple CLI errors)
for (let i = lines.length - 1; i >= 0; i--) {
const line = lines[i]
if (line.toLowerCase().startsWith('error:')) {
return line.replace(/^error:\s*/i, '').trim()
}
}
// 2. Look for Codex structured log errors: "<timestamp> ERROR <module>: <message>"
// These contain the real error (auth failures, API errors, etc.)
for (let i = lines.length - 1; i >= 0; i--) {
const match = lines[i].match(/\bERROR\s+\S+:\s*(.+)/)
if (match) {
const msg = match[1].trim()
// For auth errors, provide actionable guidance
if (/refresh token|sign in again|token.*expired|401 Unauthorized/i.test(msg)) {
return 'Codex authentication expired. Run "codex logout && codex login" to re-authenticate.'
}
return msg
}
}
// 3. Skip unhelpful "Warning: no last agent message" — surface it only as fallback
const lastLine = lines[lines.length - 1] ?? null
if (lastLine && /^warning:\s*no last agent message/i.test(lastLine)) {
return 'Codex returned no output. Check "codex login" status or try a different model.'
}
return lastLine
}