Skip to content

Instantly share code, notes, and snippets.

@mizchi
Last active June 20, 2025 11:46
Show Gist options
  • Save mizchi/cd19a6540300259d26a51fa8c6e03f1c to your computer and use it in GitHub Desktop.
Save mizchi/cd19a6540300259d26a51fa8c6e03f1c to your computer and use it in GitHub Desktop.
// 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