// summarizer-agent.ts
import { Agent } from '@samvad-protocol/sdk'
import { ChatOpenAI } from '@langchain/openai'
import { PromptTemplate } from '@langchain/core/prompts'
import { StringOutputParser } from '@langchain/core/output_parsers'
import { z } from 'zod'
const model = new ChatOpenAI({ model: 'gpt-4o-mini' })
const prompt = PromptTemplate.fromTemplate(
'Summarise the following text in 2–3 sentences:\n\n{text}'
)
const chain = prompt.pipe(model).pipe(new StringOutputParser())
const agent = new Agent({
name: 'Summarizer Agent',
version: '1.0.0',
description: 'Summarises text using GPT-4o-mini via SAMVAD',
url: process.env.AGENT_URL ?? 'http://localhost:3000',
})
agent.skill('summarize', {
name: 'Summarize',
description: 'Summarises up to 10,000 characters of text',
input: z.object({ text: z.string().max(10_000) }),
output: z.object({ summary: z.string() }),
modes: ['sync'],
trust: 'public',
handler: async (input, ctx) => {
const { text } = input as { text: string }
console.log(`Request from ${ctx.sender} (trace: ${ctx.traceId})`)
const summary = await chain.invoke({ text })
return { summary }
},
})
agent.serve({ port: 3000 })