EN

← @core/ai

1.0.1 → 1.0.2

3 files changed · +69 −1 · 44137bf…0935ba9

To apply these changes while keeping your edits, run in your project: genpm diff @core/ai --to 1.0.2

src/lib/ai/.genpmignore modified +2 −0
@@ -2,3 +2,5 @@
 *.test.ts
 *.test.tsx
 __fixtures__/
+# Documentación del repositorio (GitHub), no forma parte del módulo inyectado.
+README.md
src/lib/ai/README.md added +66 −0
@@ -0,0 +1,66 @@
+# @core/ai
+
+[![genpm](https://api.genpm.net/v1/badge/@core/ai.svg)](https://genpm.net/p/@core/ai) ![license](https://img.shields.io/badge/license-MIT-blue)
+
+> Streaming AI chat with versioned prompts, tool calling and usage/cost logging; model set by env
+
+A [GenPM](https://genpm.net) module: **source code + AI rules + MCP config**, injected into your project instead of hidden in `node_modules`. The code lands in `src/lib/ai`, pinned to a verified commit, and you own it.
+
+## Install
+
+```bash
+genpm add @core/ai
+```
+
+No genpm yet? `curl -fsSL https://genpm.net/install.sh | sh` (macOS/Linux), `irm https://genpm.net/install.ps1 | iex` (Windows), or one-off with `npx @genpm-net/cli add @core/ai`.
+
+Or let your AI agent do it: run `genpm mcp setup` once, then ask *"add @core/ai with genpm"* in Claude Code, Cursor, VS Code, Windsurf, Codex or Gemini CLI. It shows you a dry-run plan before writing anything.
+
+## What it does
+
+Streaming chat endpoint on the Vercel AI SDK, with versioned system prompts, a tool-calling scaffold and per-response
+usage logging (tokens and cost) in the database. Provider and model come from env: nothing is hardcoded. No RAG,
+embeddings or chat history storage (store `UIMessage[]` yourself if you need it).
+
+## Setup
+
+After `genpm add`, follow these steps (your AI agent finds the same steps in `src/lib/ai/AGENTS.md` and can do them for you):
+
+1. Requires Node 22+ (AI SDK 7). Env: `AI_MODEL` as `<provider>:<model-id>` plus `AI_PROVIDER_API_KEY`, and install
+   that provider package: `anthropic` → `@ai-sdk/anthropic`, `openai` → `@ai-sdk/openai`, also `google`, `mistral`,
+   `groq`, `xai`. Or `AI_MODEL=<provider>/<model-id>` with `AI_GATEWAY_API_KEY` (Vercel AI Gateway, no extra package).
+   Ask the user which provider and model to use; do not pick one for them.
+2. Generate and apply migrations (see `src/lib/db/AGENTS.md`).
+3. Hono: `app.route('/ai', aiRoutes({ getUserId: (c) => c.get('user')?.id, requireUser: true }))` after
+   `@core/auth`'s `sessionMiddleware`. Without auth the route is public and anyone can spend the provider credits:
+   put it behind your own auth or a rate limit before deploying.
+   Next.js: `app/api/ai/chat/route.ts` → `export const POST = chatRoute({ getUserId, requireUser: true })`.
+   Delete the adapter of the framework you don't use.
+4. Frontend: `useChat({ transport: new DefaultChatTransport({ api: '/ai/chat' }) })` from `@ai-sdk/react`.
+5. Verify: `curl -N -X POST localhost:3000/ai/chat -H 'content-type: application/json' -d '{"messages":[{"id":"1","role":"user","parts":[{"type":"text","text":"hi"}]}]}'`.
+
+## What's inside
+
+Paths are relative to `src/lib/ai`.
+
+- `index.ts` — public API: `streamChat`, `getModel`, `setModel`, `recordUsage`, `prompts`, `tools`, `PRICES`.
+- `prompts.ts` — **your config**: system prompts with `version`. Bump `version` whenever you change a prompt.
+- `tools.ts` — **your config**: tools the model can call (zod `inputSchema` + server-side `execute`).
+- `prices.ts` — **your config**: USD per million tokens per `AI_MODEL`, for cost logging.
+- `schema.ts` — `ai_usage` table. Depends on `../db` (@core/db).
+- `adapters/hono.ts`, `adapters/next.ts` — `POST /ai/chat` returning a UI message stream for `useChat`.
+
+## At a glance
+
+- **GenPM dependencies** (installed with it): [@core/db](https://genpm.net/p/@core/db)
+- **npm dependencies** (proposed, never installed without asking): `ai`, `zod`
+- **Environment variables** (added to `.env.example`): `AI_MODEL`, `AI_PROVIDER_API_KEY`
+- **AI rules**: [`AGENTS.md`](AGENTS.md), loaded by your agent only when it works in `src/lib/ai`
+
+## Security
+
+GenPM installs a 40-character commit SHA (never a branch), verifies an Ed25519-signed manifest, never runs install scripts and scans AI rules for prompt injection. Details: [genpm.net/docs/security](https://genpm.net/docs/security).
+
+## License
+
+MIT — see [LICENSE](LICENSE). Browse the full catalog at [genpm.net](https://genpm.net).
src/lib/ai/genpm.json modified +1 −1
@@ -1,7 +1,7 @@
 {
   "$": 1,
   "n": "@core/ai",
-  "v": "1.0.1",
+  "v": "1.0.2",
   "s": {
     "en": "Streaming AI chat with versioned prompts, tool calling and usage/cost logging; model set by env",
     "es": "Chat de IA con streaming, prompts versionados, tool calling y registro de uso y coste; modelo por env",