Skip to main content
In your Agent Endpoint you’ll make a good bunch of calls to AgentView calls: creating or fetching users, creating or fetching sessions, adding and updating runs, etc. Each of those operations can fail, which protects your app state from being corrupted. For example, user might not have access to the requested session or you can’t start a new run because other one hasn’t finished yet. AgentView makes it easy for your endpoit to easily handle errors and return them as correct HTTP responses with the correct status code and body. Any time an error occurs, AgentView will throw an exception which is an instance of AgentViewError with body and statusCode:
import 'dotenv/config'
import { Hono } from 'hono'
import { serve } from '@hono/node-server'
import { AgentView, AgentViewError } from "agentview";
import { OpenAI } from 'openai';

const app = new Hono();
const client = new OpenAI()

const av = new AgentView({
    agent: "my_simple_agent",
    version: "0.2.1"
})

app.post('/chat', async (c) => {
  // your chat endpoint...
})

// Error handling
app.onError((error, c) => {
  if (error instanceof AgentViewError) {
    return c.json(error.body, error.status);
  }
});

serve({
  fetch: app.fetch,
  port: 3000
}, (info) => {
  console.log(`Agent API server is running on http://localhost:${info.port}`)
})