|
| 1 | +# logic-grid-ai |
| 2 | + |
| 3 | +AI-powered themed category generation for [logic-grid](https://www.npmjs.com/package/logic-grid) puzzles. Uses the Anthropic API to turn a theme description into fully structured puzzle categories. |
| 4 | + |
| 5 | +## Install |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install logic-grid-ai logic-grid |
| 9 | +``` |
| 10 | + |
| 11 | +Requires `logic-grid` as a peer dependency and an [Anthropic API key](https://console.anthropic.com/settings/keys). |
| 12 | + |
| 13 | +## Quick Start |
| 14 | + |
| 15 | +```typescript |
| 16 | +import { generateTheme } from "logic-grid-ai"; |
| 17 | +import { generate } from "logic-grid"; |
| 18 | + |
| 19 | +const theme = await generateTheme({ |
| 20 | + theme: "pirate adventure", |
| 21 | + size: 4, |
| 22 | + categories: 4, |
| 23 | +}); |
| 24 | +// { |
| 25 | +// categories: [ |
| 26 | +// { name: "Pirate", values: ["Blackbeard", "Redbeard", ...], noun: "" }, |
| 27 | +// { name: "Ship", values: ["Revenge", "Kraken", ...], noun: "captain", verb: ["commands the", ...] }, |
| 28 | +// ... |
| 29 | +// ], |
| 30 | +// positionNoun: ["cove", "coves"], |
| 31 | +// positionPreposition: "at" |
| 32 | +// } |
| 33 | + |
| 34 | +const puzzle = generate({ |
| 35 | + size: 4, |
| 36 | + categories: 4, |
| 37 | + categoryNames: theme.categories, |
| 38 | + positionNoun: theme.positionNoun, |
| 39 | + positionPreposition: theme.positionPreposition, |
| 40 | +}); |
| 41 | +// Clues like: "Blackbeard commands the Revenge." |
| 42 | +// "The gold seeker is at the first cove." |
| 43 | +``` |
| 44 | + |
| 45 | +## API |
| 46 | + |
| 47 | +### `generateTheme(options)` |
| 48 | + |
| 49 | +Generate themed categories for a logic grid puzzle. |
| 50 | + |
| 51 | +```typescript |
| 52 | +const result = await generateTheme({ |
| 53 | + theme: "space exploration", // theme description (required) |
| 54 | + size: 5, // values per category, 3-8 (required) |
| 55 | + categories: 4, // number of categories, 3-8 (required) |
| 56 | + constraints: ["kid-friendly"], // optional hints for the AI |
| 57 | + client: myClient, // optional custom AIClient |
| 58 | +}); |
| 59 | +``` |
| 60 | + |
| 61 | +Returns a `ThemeResult`: |
| 62 | + |
| 63 | +```typescript |
| 64 | +interface ThemeResult { |
| 65 | + categories: Category[]; // from logic-grid |
| 66 | + positionNoun: [string, string]; // [singular, plural], e.g. ["planet", "planets"] |
| 67 | + positionPreposition: string; // e.g. "on" -> "lives on the first planet" |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +The result is validated against structural and semantic rules (value uniqueness, noun consistency, category count, etc.). If validation fails, the AI is retried with error feedback up to 3 times. |
| 72 | + |
| 73 | +### `createAnthropicClient(apiKey?)` |
| 74 | + |
| 75 | +Create the default AI client backed by the Anthropic SDK. If no key is provided, reads from `ANTHROPIC_API_KEY`. |
| 76 | + |
| 77 | +```typescript |
| 78 | +import { createAnthropicClient } from "logic-grid-ai"; |
| 79 | + |
| 80 | +const client = createAnthropicClient("sk-ant-..."); |
| 81 | +``` |
| 82 | + |
| 83 | +### Custom AI Client |
| 84 | + |
| 85 | +Implement the `AIClient` interface to use a different provider: |
| 86 | + |
| 87 | +```typescript |
| 88 | +import type { AIClient } from "logic-grid-ai"; |
| 89 | + |
| 90 | +const myClient: AIClient = { |
| 91 | + async completeJSON(prompt, schema) { |
| 92 | + // Call your preferred API, return JSON matching the schema |
| 93 | + }, |
| 94 | +}; |
| 95 | + |
| 96 | +const theme = await generateTheme({ |
| 97 | + theme: "cooking competition", |
| 98 | + size: 4, |
| 99 | + categories: 4, |
| 100 | + client: myClient, |
| 101 | +}); |
| 102 | +``` |
| 103 | + |
| 104 | +### `validateThemeResult(result, size, categories)` |
| 105 | + |
| 106 | +Validate AI output against structural and semantic rules. Returns an array of error messages (empty = valid). Used internally by `generateTheme`, but exported for custom pipelines. |
| 107 | + |
| 108 | +```typescript |
| 109 | +import { validateThemeResult } from "logic-grid-ai"; |
| 110 | + |
| 111 | +const errors = validateThemeResult(result, 4, 4); |
| 112 | +if (errors.length > 0) { |
| 113 | + console.error("Invalid theme:", errors); |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +## How It Works |
| 118 | + |
| 119 | +1. A detailed prompt describes the puzzle structure, category contract, and position noun semantics |
| 120 | +2. The AI responds via tool_use with structured JSON matching a strict schema |
| 121 | +3. The response is validated (category count, value uniqueness, noun consistency, etc.) |
| 122 | +4. If validation fails, errors are fed back to the AI for up to 3 retries |
| 123 | + |
| 124 | +## License |
| 125 | + |
| 126 | +MIT |
0 commit comments