52 lines
1.7 KiB
TypeScript
52 lines
1.7 KiB
TypeScript
import * as plugins from './plugins.js';
|
|
|
|
const COMPACTION_PROMPT = `Provide a detailed prompt for continuing our conversation above.
|
|
Focus on information that would be helpful for continuing the conversation, including what we did, what we're doing, which files we're working on, and what we're going to do next.
|
|
The summary that you construct will be used so that another agent can read it and continue the work.
|
|
|
|
When constructing the summary, try to stick to this template:
|
|
---
|
|
## Goal
|
|
[What goal(s) is the user trying to accomplish?]
|
|
|
|
## Instructions
|
|
- [What important instructions did the user give you that are relevant]
|
|
|
|
## Discoveries
|
|
[What notable things were learned during this conversation that would be useful for the next agent to know]
|
|
|
|
## Accomplished
|
|
[What work has been completed, what work is still in progress, and what work is left?]
|
|
|
|
## Relevant files / directories
|
|
[A structured list of relevant files that have been read, edited, or created]
|
|
---`;
|
|
|
|
/**
|
|
* Compacts a message history into a summary.
|
|
* Pass this as the onContextOverflow handler in IAgentRunOptions.
|
|
*
|
|
* @param model The same model used by runAgent, or a cheaper small model
|
|
* @param messages The full message history that overflowed
|
|
* @returns A minimal ModelMessage[] containing the summary as context
|
|
*/
|
|
export async function compactMessages(
|
|
model: plugins.LanguageModelV3,
|
|
messages: plugins.ModelMessage[],
|
|
): Promise<plugins.ModelMessage[]> {
|
|
const result = await plugins.generateText({
|
|
model,
|
|
messages: [
|
|
...messages,
|
|
{ role: 'user', content: COMPACTION_PROMPT },
|
|
],
|
|
});
|
|
|
|
return [
|
|
{
|
|
role: 'user',
|
|
content: `[Previous conversation summary]\n\n${result.text}\n\n[End of summary. Continue from here.]`,
|
|
},
|
|
];
|
|
}
|