From 42d1559b8b1c53bd58447d4b9b3afa52859a866e Mon Sep 17 00:00:00 2001 From: ryoppippi <1560508+ryoppippi@users.noreply.github.com> Date: Mon, 12 May 2025 14:31:52 +0100 Subject: [PATCH] fix: follow best practice of Hono --- src/agent.ts | 10 +++++++--- src/authorize.ts | 10 +++++++--- src/index.ts | 8 ++++---- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/agent.ts b/src/agent.ts index cb80930..d3547ec 100644 --- a/src/agent.ts +++ b/src/agent.ts @@ -2,14 +2,16 @@ import { createGoogleGenerativeAI } from '@ai-sdk/google'; import { CoreMessage, streamText } from 'ai'; import { env } from 'cloudflare:workers'; import { verifySignature, streamResponse } from '@layercode/node-server-sdk'; -import { Context } from 'hono'; +import { Hono } from 'hono'; const sessionMessages = {} as Record; const SYSTEM_PROMPT = `You are a helpful conversation assistant. You should respond to the user's message in a conversational manner. Your output will be spoken by a TTS model. You should respond in a way that is easy for the TTS model to speak and sound natural.`; const WELCOME_MESSAGE = 'Welcome to Layercode. How can I help you today?'; -export const onRequestPost = async (c: Context) => { +const app = new Hono(); + +app.post('/', async (c) => { if (!env.GOOGLE_GENERATIVE_AI_API_KEY) { return c.json({ error: 'GOOGLE_GENERATIVE_AI_API_KEY is not set' }, 500); } @@ -68,4 +70,6 @@ export const onRequestPost = async (c: Context) => { // Here we return the textStream chunks as SSE messages to Layercode, to be spoken to the user await stream.ttsTextStream(textStream); }); -}; +}); + +export { app }; diff --git a/src/authorize.ts b/src/authorize.ts index 42cc1e8..83d3280 100644 --- a/src/authorize.ts +++ b/src/authorize.ts @@ -1,7 +1,9 @@ -import { Context } from 'hono'; +import { Hono } from 'hono'; import { env } from 'cloudflare:workers'; -export const onRequestPost = async (c: Context) => { +const app = new Hono(); + +app.post('/', async (c) => { try { const response = await fetch("https://api.layercode.com/v1/pipelines/authorize_session", { method: 'POST', @@ -20,4 +22,6 @@ export const onRequestPost = async (c: Context) => { } catch (error) { return c.json({ error: error }); } -}; +}); + +export { app } diff --git a/src/index.ts b/src/index.ts index 7fb1e94..6523b48 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,14 +1,14 @@ import { Hono } from 'hono'; -import { onRequestPost as onRequestPostAgent } from './agent'; -import { onRequestPost as onRequestPostAuthorize } from './authorize'; +import { app as agentApp } from './agent'; +import { app as authorizeApp } from './authorize'; import { cors } from 'hono/cors' const app = new Hono(); -app.post('/agent', onRequestPostAgent); +app.route('/agent', agentApp); app.use('/authorize', cors()) -app.post('/authorize', onRequestPostAuthorize); +app.route('/authorize', authorizeApp); export default app;