ai-integration
Skill0 starsChanged 15 days agoMITView on GitHub
Add LLM features to a Node.js backend with any OpenAI-compatible provider — AI config block, a shared AIClient (chat, json, stream, embed), bring-your-own-key storage with AES-256-GCM, key verification, SSRF guard on custom base URLs, model pool with cooldown and fallback, SSE streaming endpoints, tool-calling agent loops with human approval, usage logging without prompt content, friendly provider errors, per-user AI rate limits and evals. Use when building AI chat, assistants, agents, summarization, extraction, embeddings or BYOK settings in an Express backend.
- Reads credentials
6 other projects use a version of this.
Adopted by
Most recently taken up by
- darknecrocities/SkillBuildertheir own version, first seen Nov 29, 2026
- orchnesai/orchnes-360their own version, first seen Nov 29, 2026
- emanuelegreco29/codexpiatortheir own version, first seen Nov 10, 2026
- CrowPus/Zelyqtheir own version, first seen Aug 22, 2026
- Dokkabei97/forged-claude-codetheir own version, first seen Mar 1, 2026
- thesaifalitai/claude-setuptheir own version, first seen Feb 27, 2026
Other versions6 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 6
- darknecrocities/SkillBuilder★ 13same name
- CrowPus/Zelyq★ 11same name
- thesaifalitai/claude-setup★ 9same name
- orchnesai/orchnes-360★ 3same name
- Dokkabei97/forged-claude-code★ 2same name
- emanuelegreco29/codexpiator★ 1same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository14 other files it gives its agents.
- api-docsSkill
- auth-sessionsSkill
- backend-conventionsSkill
- backend-reviewSkill
- create-backendSkill
- deploy-dockerSkill
- mongoose-mongodbSkill
- new-moduleSkill
- nextjs-api-routesSkill
- prisma-postgresSkill
- redis-cacheSkill
- scheduled-jobsSkill
- security-hardeningSkill
- session-logSkill
The file
We split this skill into the rules it gives an agent
1 rule. 0 are common, 1 is distinctive, and 0 are very specific to this project.
0
Common
Most of the 4 edited versions we read keep these.
1
Distinctive
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 155
Never surface a raw provider body or stack.
“Common” is measured: kept by at least 6 in 10 of the 4 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
AI Integration
Tested templates live in the sibling create-backend skill in ../create-backend/templates/ai.md: each file sits under a ## \<path>`` heading with its full content in the code block below it. Write them at those paths:
src/shared/ai/ai.js AIClient: chat, json, stream, embed
src/shared/ai/model-pool.js createModelPool: cooldown + fallback across models
src/shared/ai/index.js barrel — modules import from shared/ai/index.js
src/shared/crypto/secret-box.js encryptSecret, decryptSecret, lastFour (AES-256-GCM)
src/shared/network/ssrf-guard.js assertPublicUrl for user-supplied base URLs
src/modules/assistant/ a complete AI module (see layout below)
api/assistant.md its API docThe ai config block and AI_* env vars are in ../create-backend/templates/common.md (src/config/index.js and .env.example). For an existing project, copy the overlay, merge that config block, add openai, mount assistantRoutes, and rename assistant to the feature.
AI module layout
src/modules/<name>/
controllers/<name>.controller.js SSE + JSON endpoints, maps provider errors to messages
services/<name>.service.js the turn: async generator, agent loop, usage log
services/model.service.js streams one model call, merges tool-call deltas
services/summarize.service.js one-shot JSON task validated by Zod
services/errors.service.js describeAiFailure: provider status → user message
routes/<name>.route.js authenticate → aiLimiter → validateRequest → controller
validations/<name>.schema.js request schemas + the schema model JSON must match
prompt/<purpose>.prompt.js system prompts and prompt builders, nothing else
tools/<verb>-<noun>.tool.js { definition, handler, requiresApproval }
tools/index.js TOOL_DEFINITIONS + TOOL_MAP
evals/cases.js inputs and expectations
evals/score.js pure scoring, unit-testable
evals/run-evals.js runs real turns, prints PASS/FAIL, exit code 1 on failure
index.js export { default as <name>Routes }Prompts never live inline in services. Tools never contain business logic — they call the module services the REST API uses.
Install only openai. It speaks to any OpenAI-compatible endpoint (OpenAI, OpenRouter, Groq, Together, DeepSeek, Mistral, Gemini's OpenAI endpoint, local Ollama) through baseURL.
Config
The generated app reads its AI settings from its own .env through src/config/index.js (both in ../create-backend/templates/common.md): provider base URL, default model, optional model pool, embedding model, the 32-byte key-encryption secret for stored user keys, and a switch that allows private hosts for self-hosted installs only. The plugin itself never reads or asks for any of these values.
Generate the key-encryption secret with openssl rand -hex 32, separately per environment. Rotating it makes stored keys unreadable, so rotation is a re-encrypt script, never a swap.
Using the client
import { ai, AIClient, createModelPool } from '../../../shared/ai/index.js';
const summary = await ai.chat({ system: SUMMARY_PROMPT, prompt: text, maxTokens: 300 });
const fields = await ai.json({ system: EXTRACT_PROMPT, prompt: text });
const vectors = await ai.embed(['first', 'second']);
const userClient = new AIClient({ apiKey, baseURL, model, maxRetries: 0 });
const pool = createModelPool(config.ai.modelPool.map((model) => new AIClient({ model, maxRetries: 0 })));- AI calls live in services, never controllers.
- Prompts live in
modules/<name>/prompt/<purpose>.prompt.jsas exported builders, not inline strings scattered through services. - Validate model JSON with Zod before using it. A model is untrusted input.
- Errors become
statusCode-carrying errors withprovider: 'ai'(toAIError), so the controller can map them.
Bring your own key (BYOK)
Store per user/org: baseUrl, model, keyCiphertext, keyLast4, lastVerifiedAt, isActive.
- On save:
assertPublicUrl(baseUrl)→ verify with a tiny call (chat({ prompt: 'hi', maxTokens: 5 }),timeout: 10000,maxRetries: 0) →encryptSecret(apiKey)→ store ciphertext +lastFour(apiKey). - Responses show
keyLast4andhasKeyonly. The key never appears in a response, log, error, analytics event or API doc example. - On use: decrypt in memory for one call, build an
AIClient, discard. Re-runassertPublicUrlon every call because DNS can change. - A partial update (model only) re-verifies with the stored key.
- Decrypt failure → log and fall back to the shared provider (or a clear 400 "re-enter your key"), never a 500 with the cause.
SSRF guard
A user-supplied baseUrl lets someone point your server at internal addresses. On hosted deployments require https and reject hosts resolving to private, loopback, link-local, CGNAT or cloud-metadata ranges (169.254.169.254). ALLOW_PRIVATE_AI_HOSTS=true disables the check for self-hosted installs only.
Streaming over SSE
const chat = async (req, res) => {
res.setHeader('Content-Type', 'text/event-stream');
res.setHeader('Cache-Control', 'no-cache, no-transform');
res.setHeader('Connection', 'keep-alive');
res.setHeader('X-Accel-Buffering', 'no');
res.flushHeaders();
const send = (event, data) => res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
try {
for await (const chunk of runTurnService(ctx, req.body)) {
send(chunk.type, chunk.data);
}
} catch (error) {
logger.error('AI chat failed', { error: error.message, stack: error.stack, requestId: req.requestId });
send('error', describeFailure(error));
} finally {
res.end();
}
};- The service is an
async function*that yields typed events (token,tool_call,tool_result,approval_required,waiting,done). The controller only translates them to SSE. - Headers are already sent, so errors become an
errorevent — neverhttpErrorafterflushHeaders. X-Accel-Buffering: nostops nginx from buffering the stream.- Stream with
stream_options: { include_usage: true }to get token counts.
Model pool and fallback
For shared keys on rate-limited providers, give the turn a pool of models. On 429 put the model in cooldown (use the provider's "try again in Xs" hint when present), on 429/5xx move to the next model that is not cooling. Only retry before the first token streamed; once output has started, surface the error. Optionally fall back to a second provider as a last resort. See createModelPool in src/shared/ai/model-pool.js.
Tool-calling agent loop
messages = [system, ...history, user]
for step in 0..MAX_TURNS:
result = model(messages, tools)
if no tool calls → return result.content
push assistant message with tool_calls
run all tool calls in Promise.all → push one `tool` message per call (JSON result)
return "ran out of steps" reply listing what did completeRules that keep agents safe:
- One file per tool:
tools/<verb>-<noun>.tool.jsexporting{ definition, handler, requiresApproval?, plan? };tools/index.jsbuildsTOOL_DEFINITIONSand aTOOL_MAPby name. - Tool definitions use JSON Schema with
additionalProperties: false; parse arguments in try/catch and validate before use. - Handlers call the same services the REST API uses, with the caller's identity and scope — the agent can never do more than the user could.
- Writes require approval.
requiresApprovaltools do not run;plan()produces a human summary, a pending-action record is saved (expires in 24 h), and the stream emitsapproval_required. A separatePOST .../actions/resolveapplies or rejects. If the model claims it "did" something still pending, append a correction. - Reject arguments the user never said (e.g. an invented assignee or due date) and tell the model to ask instead.
- Cap steps (
MAX_TURNS) andmaxTokensper call. Truncate tool results and history (last N messages). - Select a subset of tools per request when you have many; fewer tools → better tool choice on small models.
Usage, privacy, limits
- Log one usage row per turn:
userId, scope id, feature/route, provider, model, prompt/completion tokens, model calls, tool calls, duration, status. Never prompt or completion text unless the product explicitly stores conversations. - Usage logging failures are caught and logged; they never fail the user's request.
- Per-user rate limit on AI routes even with BYOK, so your server is not a free proxy. Shared-key plans get a daily cap counted from the usage table.
Friendly errors
Map provider failures to sentences the user can act on:
| Provider status | Message |
| 401 / 403 | Your API key was rejected. Check it in settings. |
| 404 | Model not found. Check the model name. |
| 413 | That conversation is too long. Start a new chat. |
| 429 (daily quota) | The provider's daily limit is used up. |
| 429 | Busy right now, try again in about N seconds. |
| 5xx / connection | The AI provider is not responding. Try again shortly. |
Never surface a raw provider body or stack.
Evals
For any agent or prompt that matters, keep modules/<name>/evals/: cases.js (input → expected tool calls / never-call / required fields), score.js (pure, unit-testable), and run-evals.js (a script that runs real turns against a fixture tenant, prints pass/fail, cleans up). Run with npm run eval:<name> before changing prompts or models.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

