Last active
June 20, 2025 11:46
-
-
Save mizchi/cd19a6540300259d26a51fa8c6e03f1c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// node --test ask-claude.test.ts | |
import { test } from "node:test"; | |
import { query, type Options } from "@anthropic-ai/claude-code"; | |
async function assertAI(prompt: string, options: Options = {}): Promise<void> { | |
const MARKER = "**ASSERT_OK**"; | |
const abortController = new AbortController(); | |
const finalPrompt = `Assert user query. Return ${MARKER} if it is true. \n\n${prompt}`; | |
for await (const message of query({ | |
prompt: finalPrompt, | |
options: { | |
maxTurns: 7, | |
allowedTools: ["Read(*)"], | |
cwd: process.cwd(), | |
abortController, | |
...options, | |
}, | |
})) { | |
if (message.type === "assistant") { | |
const c = message.message.content; | |
const res: string = Array.isArray(c) ? c.map((c) => c.text).join("") : c; | |
if (res.includes(MARKER)) { | |
abortController.abort(); | |
return; | |
} | |
} | |
} | |
throw new Error("No response"); | |
} | |
test("assertAI", async () => { | |
await assertAI(`Exists README.md`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment