Created
February 10, 2026 00:23
-
-
Save amiller/b6a38360a821ebb0f4dd08425c83847e to your computer and use it in GitHub Desktop.
OAuth3 Demo: Constrained Claude Query (template-based delegation)
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
| // @skill Constrained Claude Query | |
| // @description Template-based Anthropic API delegation - Agent provides word (β€20 chars) for character counting | |
| // @secrets ANTHROPIC_API_KEY | |
| // @network api.anthropic.com | |
| // @timeout 30 | |
| const WORD = Deno.env.get("WORD"); | |
| const ANTHROPIC_API_KEY = Deno.env.get("ANTHROPIC_API_KEY"); | |
| if (!WORD) { | |
| console.error("Error: WORD parameter required"); | |
| Deno.exit(1); | |
| } | |
| if (!ANTHROPIC_API_KEY) { | |
| console.error("Error: ANTHROPIC_API_KEY secret required"); | |
| Deno.exit(1); | |
| } | |
| // Sanitizer: Enforce 20 character limit | |
| if (WORD.length > 20) { | |
| console.error(`Error: WORD must be β€20 characters (got ${WORD.length})`); | |
| Deno.exit(1); | |
| } | |
| // Sanitizer: Only allow alphanumeric + basic punctuation (no newlines, control chars) | |
| if (!/^[a-zA-Z0-9\s\-_.,!?]+$/.test(WORD)) { | |
| console.error("Error: WORD contains invalid characters"); | |
| Deno.exit(1); | |
| } | |
| // Template: Agent can only fill in the suffix | |
| const TEMPLATE = "Count the characters in the following word: "; | |
| const fullPrompt = TEMPLATE + WORD; | |
| console.log(`π Prompt: "${fullPrompt}"`); | |
| console.log(`π Word length: ${WORD.length}/20 chars`); | |
| console.log(`\nπ€ Calling Anthropic API...\n`); | |
| try { | |
| const response = await fetch("https://api.anthropic.com/v1/messages", { | |
| method: "POST", | |
| headers: { | |
| "Content-Type": "application/json", | |
| "x-api-key": ANTHROPIC_API_KEY, | |
| "anthropic-version": "2023-06-01", | |
| }, | |
| body: JSON.stringify({ | |
| model: "claude-3-5-haiku-20241022", | |
| max_tokens: 100, | |
| messages: [ | |
| { | |
| role: "user", | |
| content: fullPrompt, | |
| }, | |
| ], | |
| }), | |
| }); | |
| if (!response.ok) { | |
| const error = await response.text(); | |
| console.error(`API Error ${response.status}: ${error}`); | |
| Deno.exit(1); | |
| } | |
| const data = await response.json(); | |
| const content = data.content[0]?.text || "(no response)"; | |
| console.log(`β Claude's response:\n${content}`); | |
| console.log(`\nπ Tokens used: ${data.usage.input_tokens} in, ${data.usage.output_tokens} out`); | |
| } catch (error) { | |
| console.error(`Network error: ${error.message}`); | |
| Deno.exit(1); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment