Hono
Edge-ready middleware for Hono. Works with Cloudflare Workers, Deno, Bun, and Node.js.
Installation
Terminal
npm install @tryhamster/gerbil honoQuick Start
index.ts
01import { Hono } from "hono";02import { gerbil } from "@tryhamster/gerbil/hono";03
04const app = new Hono();05
06// Create and mount AI routes07const aiRoutes = await gerbil({ model: "qwen3-0.6b" })();08app.route("/ai", aiRoutes);09
10export default app;Available Endpoints
| Method | Endpoint | Description |
|---|---|---|
| POST | /generate | Generate text |
| POST | /stream | Stream text (SSE) |
| POST | /json | Structured output |
| POST | /embed | Embeddings |
| GET | /info | Model info |
Configuration
config.ts
01import { gerbil } from "@tryhamster/gerbil/hono";02
03const aiRoutes = await gerbil({04 // Model05 model: "qwen3-0.6b",06 device: "auto",07 dtype: "q4",08
09 // Defaults10 maxTokens: 500,11 temperature: 0.7,12 system: "You are a helpful assistant.",13
14 // Callbacks15 onRequest: async (c) => {16 console.log("Request from:", c.req.header("user-agent"));17 },18 onResponse: async (result, c) => {19 console.log("Generated:", result.tokensGenerated, "tokens");20 },21})();Streaming Responses
streaming.ts
01import { Hono } from "hono";02import { streamSSE } from "hono/streaming";03import gerbil from "@tryhamster/gerbil";04
05const app = new Hono();06
07app.post("/stream", async (c) => {08 const { prompt } = await c.req.json();09
10 return streamSSE(c, async (stream) => {11 for await (const token of gerbil.stream(prompt)) {12 await stream.writeSSE({13 data: JSON.stringify({ token }),14 });15 }16 await stream.writeSSE({17 data: JSON.stringify({ done: true }),18 });19 });20});Custom Routes
custom.ts
01import { Hono } from "hono";02import gerbil from "@tryhamster/gerbil";03
04const app = new Hono();05
06// Load model07await gerbil.loadModel("qwen3-0.6b");08
09// Custom summarize endpoint10app.post("/summarize", async (c) => {11 const { content } = await c.req.json();12
13 const result = await gerbil.generate(14 `Summarize: ${content}`,15 { maxTokens: 150 }16 );17
18 return c.json({ summary: result.text });19});20
21// Custom translate endpoint22app.post("/translate", async (c) => {23 const { text, to } = await c.req.json();24
25 const result = await gerbil.generate(26 `Translate to ${to}: ${text}`,27 { maxTokens: 200 }28 );29
30 return c.json({ translation: result.text });31});32
33export default app;Middleware
middleware.ts
01import { Hono } from "hono";02import { cors } from "hono/cors";03import { logger } from "hono/logger";04import { bearerAuth } from "hono/bearer-auth";05import { gerbil } from "@tryhamster/gerbil/hono";06
07const app = new Hono();08
09// Apply middleware10app.use("*", logger());11app.use("/ai/*", cors());12app.use("/ai/*", bearerAuth({ token: process.env.API_TOKEN }));13
14// Mount AI routes15const aiRoutes = await gerbil({ model: "qwen3-0.6b" })();16app.route("/ai", aiRoutes);17
18export default app;Bun
bun.ts
01// index.ts02import { Hono } from "hono";03import { gerbil } from "@tryhamster/gerbil/hono";04
05const app = new Hono();06
07const aiRoutes = await gerbil({ model: "qwen3-0.6b" })();08app.route("/ai", aiRoutes);09
10export default {11 port: 3000,12 fetch: app.fetch,13};14
15// Run with: bun run index.tsDeno
deno.ts
01// main.ts02import { Hono } from "npm:hono";03import { gerbil } from "npm:@tryhamster/gerbil/hono";04
05const app = new Hono();06
07const aiRoutes = await gerbil({ model: "qwen3-0.6b" })();08app.route("/ai", aiRoutes);09
10Deno.serve({ port: 3000 }, app.fetch);11
12// Run with: deno run --allow-net --allow-read main.tsError Handling
error-handling.ts
01import { Hono } from "hono";02import { HTTPException } from "hono/http-exception";03import { gerbil } from "@tryhamster/gerbil/hono";04
05const app = new Hono();06
07// Global error handler08app.onError((err, c) => {09 console.error(err);10 11 if (err instanceof HTTPException) {12 return c.json({ error: err.message }, err.status);13 }14 15 return c.json({ error: "Internal server error" }, 500);16});17
18// AI routes with error handling19const aiRoutes = await gerbil({20 model: "qwen3-0.6b",21 onError: async (error, c) => {22 console.error("AI Error:", error);23 throw new HTTPException(400, { message: error.message });24 },25})();26
27app.route("/ai", aiRoutes);28
29export default app;