Skip to content

Commit f14c7cb

Browse files
committed
feat(scout-agent): wire compaction logging into buildStreamTextParams
This commit: - Imports applyCompaction and createCompactionTool from ./compaction - Applies compaction to messages before processing in buildStreamTextParams - Logs when compaction is applied (showing message count reduction) - Adds the compaction tool to the tools object so the model can call it - Uses compacted messages for Slack metadata detection and model conversion - Fixes isolated declarations issue in createCompactionTool by adding return type - Fixes test assertion for execute method on Tool type
1 parent d062a49 commit f14c7cb

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

packages/scout-agent/lib/compaction.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ describe("compaction", () => {
198198
const tools = createCompactionTool();
199199
const compactionTool = tools[COMPACT_CONVERSATION_TOOL_NAME];
200200

201-
const result = (await compactionTool!.execute(
201+
const result = (await compactionTool.execute!(
202202
{ summary: "Test summary content" },
203203
{ abortSignal: new AbortController().signal } as any
204204
)) as { summary: string; compacted_at: string; message: string };

packages/scout-agent/lib/compaction.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { tool, type ModelMessage, APICallError } from "ai";
1+
import { tool, type Tool, type ModelMessage, APICallError } from "ai";
22
import { z } from "zod";
33
import type { Message } from "./types";
44

@@ -151,7 +151,10 @@ export function applyCompaction(messages: Message[]): Message[] {
151151
* Creates the compact_conversation tool.
152152
* This tool should be called by the model when the conversation is getting too long.
153153
*/
154-
export function createCompactionTool() {
154+
export function createCompactionTool(): Record<
155+
typeof COMPACT_CONVERSATION_TOOL_NAME,
156+
Tool
157+
> {
155158
return {
156159
[COMPACT_CONVERSATION_TOOL_NAME]: tool({
157160
description: `Compact the conversation history to save context space. Call this tool when instructed that the conversation is approaching context limits. Provide a detailed and thorough summary that captures:

packages/scout-agent/lib/core.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
githubAppContextFactory,
3030
handleGitHubWebhook,
3131
} from "./github";
32+
import { applyCompaction, createCompactionTool } from "./compaction";
3233
import { defaultSystemPrompt } from "./prompt";
3334
import { createSlackApp, createSlackTools, getSlackMetadata } from "./slack";
3435
import type { Message } from "./types";
@@ -346,7 +347,16 @@ export class Scout {
346347
)()
347348
: undefined;
348349

349-
const slackMetadata = getSlackMetadata(messages);
350+
// Apply compaction if a compaction summary exists in the message history
351+
const compactedMessages = applyCompaction(messages);
352+
const wasCompacted = compactedMessages.length !== messages.length;
353+
if (wasCompacted) {
354+
this.logger.info(
355+
`Applied conversation compaction: ${messages.length} messages -> ${compactedMessages.length} messages`
356+
);
357+
}
358+
359+
const slackMetadata = getSlackMetadata(compactedMessages);
350360
const respondingInSlack =
351361
this.slack.app !== undefined && slackMetadata !== undefined;
352362

@@ -447,6 +457,7 @@ export class Scout {
447457
}
448458

449459
const tools = {
460+
...createCompactionTool(),
450461
...(this.webSearch.config
451462
? createWebSearchTools({ exaApiKey: this.webSearch.config.exaApiKey })
452463
: {}),
@@ -473,7 +484,7 @@ ${slack.formattingRules}
473484
</formatting-rules>`;
474485
}
475486

476-
const converted = convertToModelMessages(messages, {
487+
const converted = convertToModelMessages(compactedMessages, {
477488
ignoreIncompleteToolCalls: true,
478489
tools,
479490
});

0 commit comments

Comments
 (0)