-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathcodex.ts
More file actions
209 lines (185 loc) · 6.47 KB
/
codex.ts
File metadata and controls
209 lines (185 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { existsSync, readFileSync, readdirSync } from 'node:fs';
import { join, resolve } from 'node:path';
import yaml from 'js-yaml';
import { loadAgentManifest, loadFileIfExists } from '../utils/loader.js';
import { loadAllSkills, getAllowedTools } from '../utils/skill-loader.js';
import { buildComplianceSection, buildMcpServersConfig } from './shared.js';
/**
* Export a gitagent to OpenAI Codex CLI format.
*
* Codex CLI (openai/codex) uses:
* - AGENTS.md (custom agent instructions, project root)
* - codex.json (model and provider configuration)
*
* Reference: https://github.com/openai/codex
*/
export interface CodexExport {
/** Content for AGENTS.md */
instructions: string;
/** Content for codex.json */
config: Record<string, unknown>;
}
/**
* Export a gitagent directory to Codex CLI format.
*/
export function exportToCodex(dir: string): CodexExport {
const agentDir = resolve(dir);
const manifest = loadAgentManifest(agentDir);
const instructions = buildInstructions(agentDir, manifest);
const config = buildConfig(manifest);
return { instructions, config };
}
/**
* Export as a single string (for `gitagent export -f codex`).
*/
export function exportToCodexString(dir: string): string {
const exp = exportToCodex(dir);
const parts: string[] = [];
parts.push('# === AGENTS.md ===');
parts.push(exp.instructions);
parts.push('\n# === codex.json ===');
parts.push(JSON.stringify(exp.config, null, 2));
return parts.join('\n');
}
function buildInstructions(
agentDir: string,
manifest: ReturnType<typeof loadAgentManifest>,
): string {
const parts: string[] = [];
// Agent identity
parts.push(`# ${manifest.name}`);
parts.push(`${manifest.description}`);
parts.push('');
// SOUL.md — persona / identity
const soul = loadFileIfExists(join(agentDir, 'SOUL.md'));
if (soul) {
parts.push(soul);
parts.push('');
}
// RULES.md — constraints and operational rules
const rules = loadFileIfExists(join(agentDir, 'RULES.md'));
if (rules) {
parts.push(rules);
parts.push('');
}
// DUTIES.md — segregation of duties policy
const duty = loadFileIfExists(join(agentDir, 'DUTIES.md'));
if (duty) {
parts.push(duty);
parts.push('');
}
// Skills — include full instructions (Codex reads AGENTS.md as a single file)
const skillsDir = join(agentDir, 'skills');
const skills = loadAllSkills(skillsDir);
if (skills.length > 0) {
parts.push('## Skills');
parts.push('');
for (const skill of skills) {
const toolsList = getAllowedTools(skill.frontmatter);
const toolsNote = toolsList.length > 0 ? `\nAllowed tools: ${toolsList.join(', ')}` : '';
parts.push(`### ${skill.frontmatter.name}`);
parts.push(`${skill.frontmatter.description}${toolsNote}`);
parts.push('');
parts.push(skill.instructions);
parts.push('');
}
}
// Tools
const toolsDir = join(agentDir, 'tools');
if (existsSync(toolsDir)) {
const toolFiles = readdirSync(toolsDir).filter(f => f.endsWith('.yaml'));
if (toolFiles.length > 0) {
parts.push('## Tools');
parts.push('');
for (const file of toolFiles) {
try {
const content = readFileSync(join(toolsDir, file), 'utf-8');
const toolConfig = yaml.load(content) as {
name?: string;
description?: string;
input_schema?: Record<string, unknown>;
};
if (toolConfig?.name) {
parts.push(`### ${toolConfig.name}`);
if (toolConfig.description) {
parts.push(toolConfig.description);
}
if (toolConfig.input_schema) {
parts.push('');
parts.push('```yaml');
parts.push(yaml.dump(toolConfig.input_schema).trimEnd());
parts.push('```');
}
parts.push('');
}
} catch { /* skip malformed tools */ }
}
}
}
// Knowledge (always_load documents)
const knowledgeDir = join(agentDir, 'knowledge');
const indexPath = join(knowledgeDir, 'index.yaml');
if (existsSync(indexPath)) {
const index = yaml.load(readFileSync(indexPath, 'utf-8')) as {
documents?: Array<{ path: string; always_load?: boolean }>;
};
if (index.documents) {
const alwaysLoad = index.documents.filter(d => d.always_load);
if (alwaysLoad.length > 0) {
parts.push('## Knowledge');
parts.push('');
for (const doc of alwaysLoad) {
const content = loadFileIfExists(join(knowledgeDir, doc.path));
if (content) {
parts.push(`### ${doc.path}`);
parts.push(content);
parts.push('');
}
}
}
}
}
// Compliance constraints
if (manifest.compliance) {
const constraints = buildComplianceSection(manifest.compliance);
if (constraints) {
parts.push(constraints);
parts.push('');
}
}
return parts.join('\n').trimEnd() + '\n';
}
function buildConfig(manifest: ReturnType<typeof loadAgentManifest>): Record<string, unknown> {
const config: Record<string, unknown> = {};
// Map model preference to Codex CLI model format
// Codex CLI config.json accepts: { model: "string", provider?: "openai|azure|..." }
if (manifest.model?.preferred) {
const model = manifest.model.preferred;
config.model = model;
// Add provider hint when it can be inferred from the model name
const provider = inferProvider(model);
if (provider !== 'openai') {
// Only emit provider when non-default — Codex defaults to openai
config.provider = provider;
}
}
// MCP servers
const mcpServers = buildMcpServersConfig(manifest.mcp_servers);
if (mcpServers) {
config.mcpServers = mcpServers;
}
return config;
}
/**
* Infer the Codex CLI provider name from a model identifier.
* Codex CLI providers: openai (default), azure, ollama, openai-compatible
*/
function inferProvider(model: string): string {
if (model.startsWith('claude') || model.includes('anthropic')) return 'openai-compatible';
if (model.startsWith('gemini') || model.includes('google')) return 'openai-compatible';
if (model.startsWith('deepseek')) return 'openai-compatible';
if (model.startsWith('llama') || model.startsWith('mistral') || model.startsWith('qwen')) return 'ollama';
if (model.startsWith('o1') || model.startsWith('o3') || model.startsWith('o4') || model.startsWith('gpt')) return 'openai';
if (model.startsWith('codex')) return 'openai';
return 'openai';
}