KO
베타 번역

@core / ai

1.0.2 ▾
인증됨MIT
GitHub

스트리밍 AI 채팅: 버전 관리 프롬프트, 도구 호출, 사용량·비용 기록, 모델은 환경 변수로 지정

코드파일 11개컨텍스트약 695토큰검사 통과

.genpmignore 적용 후 주입될 정확한 트리입니다. 고정 대상:

src/lib/ai/adapters/hono.ts읽기 전용 · 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();
  });
}

@core/ai 신고

패키지를 신고하려면 GitHub로 로그인하세요.