Files
opencode-openai-codex-auth/lib/config.ts
T
Numman AliandHanzo Dev 3890872781 feat: Add CODEX_MODE with bridge prompt and reorganize lib/ structure
## CODEX_MODE Implementation

Added configurable CODEX_MODE that replaces OpenCode system prompts with
a Codex-OpenCode bridge prompt for better Codex CLI parity.

**Key Features:**
- Enabled by default for optimal Codex CLI compatibility
- Configurable via ~/.opencode/openai-codex-auth-config.json
- Environment variable override support (CODEX_MODE=0 or CODEX_MODE=1)
- Priority: env var > config file > default (true)

**Bridge Prompt:**
- ~450 tokens (~90% reduction vs full OpenCode prompt)
- Critical tool replacements (apply_patch → edit, update_plan → todowrite)
- Available tools list and verification checklist
- OpenCode working style guidelines

**Configuration:**
```json
{
  "codexMode": true  // default
}
```

## Library Reorganization

Restructured lib/ into semantic subfolders:

```
lib/
├── auth/              # OAuth authentication
│   ├── auth.ts
│   ├── browser.ts
│   └── server.ts
├── prompts/           # System prompts
│   ├── codex.ts
│   └── codex-opencode-bridge.ts
├── request/           # Request handling
│   ├── fetch-helpers.ts
│   ├── request-transformer.ts
│   └── response-handler.ts
├── config.ts          # Plugin configuration
├── constants.ts
├── logger.ts
├── types.ts
└── oauth-success.html
```

## Files Changed

**New Files:**
- lib/config.ts - Plugin configuration loading
- lib/prompts/codex-opencode-bridge.ts - Bridge prompt
- test/plugin-config.test.ts - 12 new tests

**Modified:**
- lib/request/request-transformer.ts - CODEX_MODE parameter
- lib/request/fetch-helpers.ts - Pass codexMode through pipeline
- index.ts - Load config and determine codexMode
- lib/types.ts - Add PluginConfig type
- README.md - Document CODEX_MODE configuration

**Reorganized:**
- lib/*.ts → lib/{auth,prompts,request}/*.ts
- Updated all imports across codebase and tests

## Testing

- 123 total tests (added 12 new tests for config loading)
- All tests passing
- Comprehensive CODEX_MODE coverage:
  - Config file loading (exists, missing, invalid)
  - Priority order testing
  - Environment variable override
  - Bridge prompt vs tool remap behavior

## Documentation

Updated README.md:
- Added "Plugin Configuration" section
- Documented CODEX_MODE setting and priority
- Updated features list (123 tests, CODEX_MODE)
- Updated "How It Works" section

Co-authored-by: Hanzo Dev <[email protected]>
2025-10-03 22:31:34 +01:00

61 lines
1.6 KiB
TypeScript

import { readFileSync, existsSync } from "node:fs";
import { join } from "node:path";
import { homedir } from "node:os";
import type { PluginConfig } from "./types.js";
const CONFIG_PATH = join(homedir(), ".opencode", "openai-codex-auth-config.json");
/**
* Default plugin configuration
* CODEX_MODE is enabled by default for better Codex CLI parity
*/
const DEFAULT_CONFIG: PluginConfig = {
codexMode: true,
};
/**
* Load plugin configuration from ~/.opencode/openai-codex-auth-config.json
* Falls back to defaults if file doesn't exist or is invalid
*
* @returns Plugin configuration
*/
export function loadPluginConfig(): PluginConfig {
try {
if (!existsSync(CONFIG_PATH)) {
return DEFAULT_CONFIG;
}
const fileContent = readFileSync(CONFIG_PATH, "utf-8");
const userConfig = JSON.parse(fileContent) as Partial<PluginConfig>;
// Merge with defaults
return {
...DEFAULT_CONFIG,
...userConfig,
};
} catch (error) {
console.warn(
`[openai-codex-plugin] Failed to load config from ${CONFIG_PATH}:`,
(error as Error).message
);
return DEFAULT_CONFIG;
}
}
/**
* Get the effective CODEX_MODE setting
* Priority: environment variable > config file > default (true)
*
* @param pluginConfig - Plugin configuration from file
* @returns True if CODEX_MODE should be enabled
*/
export function getCodexMode(pluginConfig: PluginConfig): boolean {
// Environment variable takes precedence
if (process.env.CODEX_MODE !== undefined) {
return process.env.CODEX_MODE === "1";
}
// Use config setting (defaults to true)
return pluginConfig.codexMode ?? true;
}