import trodo from 'trodo-node';
import Anthropic from '@anthropic-ai/sdk';
trodo.init({ siteId: process.env.TRODO_SITE_ID });
const anthropic = new Anthropic();
export async function summarise(userId, text) {
const { result } = await trodo.wrapAgent(
'summariser',
async (run) => {
run.setInput({ text });
const stream = anthropic.messages.stream({
model: 'claude-3-5-sonnet-latest',
max_tokens: 512,
messages: [{ role: 'user', content: `Summarise:\n\n${text}` }],
});
let full = '';
for await (const event of stream) {
if (event.type === 'content_block_delta' && event.delta.type === 'text_delta') {
full += event.delta.text;
process.stdout.write(event.delta.text); // stream to your UI
}
}
await stream.finalMessage(); // ensures usage is captured
run.setOutput({ summary: full });
return full;
},
{ distinctId: userId },
);
return result;
}