export default class ResearchSynthesizer extends AgenticSystem {
@field summaries: Array<{ source: string; content: StreamableValue<string> }> = [];
@field synthesis = stream<string>('');
@field status: 'idle' | 'summarizing' | 'synthesizing' = 'idle';
@action()
async synthesize(sources: string[]) {
this.status = 'summarizing';
// Create streams for each source
this.summaries = sources.map(source => ({
source,
content: stream<string>(''),
}));
// Stream all in parallel
await Promise.all(
sources.map(async (source, i) => {
for await (const chunk of ai.stream(`Summarize: ${source}`)) {
this.summaries[i].content.append(chunk);
}
this.summaries[i].content.complete();
})
);
// Then synthesize
this.status = 'synthesizing';
const allContent = this.summaries.map(s => s.content.current).join('\n\n');
for await (const chunk of ai.stream(`Synthesize:\n${allContent}`)) {
this.synthesis.append(chunk);
}
this.synthesis.complete();
this.status = 'idle';
}
}