refactor(omo-codex): port ultrawork hook to typescript
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
#!/usr/bin/env node
|
||||
import { stdin as processStdin, stdout as processStdout } from "node:process";
|
||||
|
||||
import { runUserPromptSubmitHook } from "./codex-hook.js";
|
||||
|
||||
const command = process.argv[2];
|
||||
const subcommand = process.argv[3];
|
||||
|
||||
if (command === "hook" && subcommand === "user-prompt-submit") {
|
||||
await runHookCli();
|
||||
} else {
|
||||
process.stderr.write("Usage: codex-ultrawork hook user-prompt-submit\n");
|
||||
process.exitCode = 1;
|
||||
}
|
||||
|
||||
async function runHookCli(): Promise<void> {
|
||||
const raw = await readStdin();
|
||||
if (raw.trim().length === 0) return;
|
||||
const parsed = parseHookInput(raw);
|
||||
const output = runUserPromptSubmitHook(parsed);
|
||||
if (output.length > 0) {
|
||||
processStdout.write(output);
|
||||
}
|
||||
}
|
||||
|
||||
function parseHookInput(raw: string): unknown | undefined {
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(raw);
|
||||
return parsed;
|
||||
} catch (error) {
|
||||
if (error instanceof SyntaxError) return undefined;
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
function readStdin(): Promise<string> {
|
||||
return new Promise((resolve) => {
|
||||
let data = "";
|
||||
processStdin.setEncoding("utf8");
|
||||
processStdin.on("data", (chunk: string) => {
|
||||
data += chunk;
|
||||
});
|
||||
processStdin.once("error", () => {
|
||||
resolve(data);
|
||||
});
|
||||
processStdin.once("end", () => {
|
||||
resolve(data);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { ULTRAWORK_DIRECTIVE } from "./directive.js";
|
||||
|
||||
const ULTRAWORK_PATTERN = /\b(?:ultrawork|ulw)\b/i;
|
||||
|
||||
export type CodexUserPromptSubmitInput = {
|
||||
readonly hook_event_name: "UserPromptSubmit";
|
||||
readonly prompt: string;
|
||||
};
|
||||
|
||||
export function runUserPromptSubmitHook(input: unknown): string {
|
||||
if (!isCodexUserPromptSubmitInput(input)) return "";
|
||||
return isUltraworkPrompt(input.prompt) ? ULTRAWORK_DIRECTIVE : "";
|
||||
}
|
||||
|
||||
export function isUltraworkPrompt(prompt: string): boolean {
|
||||
return ULTRAWORK_PATTERN.test(prompt);
|
||||
}
|
||||
|
||||
function isCodexUserPromptSubmitInput(value: unknown): value is CodexUserPromptSubmitInput {
|
||||
return isRecord(value) && value["hook_event_name"] === "UserPromptSubmit" && typeof value["prompt"] === "string";
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
|
||||
export const ULTRAWORK_DIRECTIVE: string = readFileSync(new URL("../directive.md", import.meta.url), "utf8");
|
||||
Reference in New Issue
Block a user