Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,9 @@ runs:
shell: bash
run: |
exec env -u NODE_OPTIONS NODE_OPTIONS=--disable-sigusr1 node --disable-sigusr1 "$ACTION_PATH/dist/main.js" run-codex-exec \
--prompt "${CODEX_PROMPT}" \
--prompt "" \
--prompt-file "${CODEX_PROMPT_FILE}" \
--prompt-environment-variable "${CODEX_PROMPT:+CODEX_PROMPT}" \
--output-file "$CODEX_OUTPUT_FILE" \
--codex-home "$CODEX_HOME" \
--cd "$CODEX_WORKING_DIRECTORY" \
Expand Down
35 changes: 27 additions & 8 deletions dist/main.js

Large diffs are not rendered by default.

35 changes: 28 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ export async function main() {
"--prompt-file <FILE>",
"File containing the prompt to pass to `codex exec`."
)
.option(
"--prompt-environment-variable <NAME>",
"Environment variable containing the prompt to pass to `codex exec`.",
""
)
.requiredOption(
"--codex-home <DIRECTORY>",
"Path to the Codex CLI home directory (where config files are stored)."
Expand Down Expand Up @@ -176,6 +181,7 @@ export async function main() {
async (options: {
prompt: string;
promptFile: string;
promptEnvironmentVariable: string;
codexHome: string;
cd: string;
extraArgs: Array<string>;
Expand All @@ -192,6 +198,7 @@ export async function main() {
const {
prompt,
promptFile,
promptEnvironmentVariable,
outputFile,
codexHome,
cd,
Expand All @@ -208,21 +215,35 @@ export async function main() {

const normalizedPrompt = emptyAsNull(prompt);
const normalizedPromptFile = emptyAsNull(promptFile);
if (normalizedPrompt != null && normalizedPromptFile != null) {
throw new Error(
"Only one of `prompt` or `prompt-file` may be specified."
);
const normalizedPromptEnvironmentVariable = emptyAsNull(
promptEnvironmentVariable
);
const promptSourceCount = [
normalizedPrompt,
normalizedPromptFile,
normalizedPromptEnvironmentVariable,
].filter((value) => value != null).length;
if (promptSourceCount > 1) {
throw new Error("Only one prompt source may be specified.");
}

let promptSource: PromptSource;
if (normalizedPrompt != null) {
promptSource = { type: "inline", content: normalizedPrompt };
} else if (normalizedPromptFile != null) {
promptSource = { type: "file", path: normalizedPromptFile };
} else {
throw new Error(
"Either `prompt` or `prompt-file` must be specified."
} else if (normalizedPromptEnvironmentVariable != null) {
const environmentPrompt = emptyAsNull(
process.env[normalizedPromptEnvironmentVariable] ?? ""
);
if (environmentPrompt == null) {
throw new Error(
`Prompt environment variable \`${normalizedPromptEnvironmentVariable}\` is not set or empty.`
);
}
promptSource = { type: "inline", content: environmentPrompt };
} else {
throw new Error("A prompt source must be specified.");
}

// Custom option processing to coerces to null does not work with
Expand Down
122 changes: 122 additions & 0 deletions test/promptSource.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import assert from "node:assert/strict";
import { spawnSync } from "node:child_process";
import {
chmodSync,
mkdtempSync,
readFileSync,
rmSync,
writeFileSync,
} from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { test } from "node:test";
import { fileURLToPath } from "node:url";

const actionPath = fileURLToPath(new URL("../action.yml", import.meta.url));
const mainPath = fileURLToPath(new URL("../dist/main.js", import.meta.url));

function runWithEnvironmentPrompt({ directPrompt = "" } = {}) {
const tempDir = mkdtempSync(path.join(tmpdir(), "codex-action-prompt-"));
const capturePath = path.join(tempDir, "prompt.txt");
const outputPath = path.join(tempDir, "output.txt");
const fakeCodexPath = path.join(tempDir, "codex.mjs");
writeFileSync(
fakeCodexPath,
`import { readFileSync, writeFileSync } from "node:fs";
const args = process.argv.slice(2);
writeFileSync(process.env.CODEX_CAPTURE_PROMPT, readFileSync(0, "utf8"));
const outputIndex = args.indexOf("--output-last-message");
writeFileSync(args[outputIndex + 1], "fake final message\\n");
`
);
const launcherPath = path.join(tempDir, "codex");
writeFileSync(
launcherPath,
`#!/bin/sh\nexec node "${fakeCodexPath}" "$@"\n`
);
chmodSync(launcherPath, 0o755);
writeFileSync(
path.join(tempDir, "codex.cmd"),
`@node "${fakeCodexPath}" %*\r\n`
);

const prompt = "first line\nsecond line with spaces";
const result = spawnSync(
process.execPath,
[
mainPath,
"run-codex-exec",
"--prompt",
directPrompt,
"--prompt-file",
"",
"--prompt-environment-variable",
"CODEX_PROMPT",
"--codex-home",
"",
"--cd",
tempDir,
"--extra-args",
"",
"--output-file",
outputPath,
"--output-schema-file",
"",
"--output-schema",
"",
"--sandbox",
"",
"--permission-profile",
"",
"--model",
"",
"--effort",
"",
"--safety-strategy",
"unsafe",
"--codex-user",
"",
],
{
encoding: "utf8",
env: {
...process.env,
PATH: `${tempDir}${path.delimiter}${process.env.PATH ?? ""}`,
CODEX_CAPTURE_PROMPT: capturePath,
CODEX_PROMPT: prompt,
},
}
);

let capturedPrompt = null;
try {
capturedPrompt = readFileSync(capturePath, "utf8");
} catch {
// Expected when prompt validation rejects the invocation.
}
rmSync(tempDir, { recursive: true, force: true });
return { result, capturedPrompt, prompt };
}

test("keeps the action inline prompt out of the helper argv", () => {
const action = readFileSync(actionPath, "utf8");
assert.match(
action,
/--prompt-environment-variable "\$\{CODEX_PROMPT:\+CODEX_PROMPT\}"/
);
assert.doesNotMatch(action, /--prompt "\$\{CODEX_PROMPT\}"/);

const { result, capturedPrompt, prompt } = runWithEnvironmentPrompt();
assert.equal(result.status, 0, result.stderr);
assert.equal(capturedPrompt, prompt);
});

test("rejects direct and environment-backed prompts together", () => {
const { result, capturedPrompt } = runWithEnvironmentPrompt({
directPrompt: "direct prompt",
});

assert.notEqual(result.status, 0);
assert.equal(capturedPrompt, null);
assert.match(result.stderr, /Only one prompt source may be specified/);
});
Loading