EN

@core / ai

1.0.2 ▾
verifiedMIT
GitHub

Streaming AI chat with versioned prompts, tool calling and usage/cost logging; model set by env

Code11 filesContext~695 tokensscan passed

The exact tree that will be injected, after .genpmignore. Pinned to

src/lib/ai/adapters/hono.tsread-only · 0935ba9
// Adaptador Hono: `app.route('/ai', aiRoutes())` → POST /ai/chat (stream para `useChat`).
// Si usas @core/auth, monta antes `sessionMiddleware` y pasa `getUserId` para registrar el uso por usuario.
import type { Context } from 'hono';
import { Hono } from 'hono';
import type { UIMessage } from 'ai';
import { ChatInputError, sanitizeMessages, streamChat } from '../index.js';

export function aiRoutes(opts: { getUserId?: (c: Context) => string | null | undefined; requireUser?: boolean } = {}) {
  return new Hono().post('/chat', async (c) => {
    const userId = opts.getUserId?.(c) ?? null;
    if (opts.requireUser && !userId) return c.json({ error: 'unauthorized' }, 401);
    const body = await c.req.json<{ messages?: UIMessage[] }>().catch(() => ({ messages: undefined }));
    let messages: UIMessage[];
    try {
      messages = sanitizeMessages(body.messages);
    } catch (e) {
      if (e instanceof ChatInputError) return c.json({ error: e.message }, 400);
      throw e;
    }
    const result = await streamChat({ messages, userId, abortSignal: c.req.raw.signal });
    return result.toUIMessageStreamResponse();
  });
}

Report @core/ai

Sign in with GitHub to report a package.