Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/core/condense/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,17 @@ export type SummarizeResponse = {
* @param {ApiHandler} apiHandler - The API handler to use for token counting.
* @param {string} systemPrompt - The system prompt for API requests, which should be considered in the context token count
* @param {string} taskId - The task ID for the conversation, used for telemetry
* @param {boolean} isAutomaticTrigger - Whether the summarization is triggered automatically
* @returns {SummarizeResponse} - The result of the summarization operation (see above)
*/
export async function summarizeConversation(
messages: ApiMessage[],
apiHandler: ApiHandler,
systemPrompt: string,
taskId: string,
isAutomaticTrigger?: boolean,
): Promise<SummarizeResponse> {
telemetryService.captureContextCondensed(taskId)
telemetryService.captureContextCondensed(taskId, isAutomaticTrigger ?? false)
const response: SummarizeResponse = { messages, cost: 0, summary: "" }
const messagesToSummarize = getMessagesSinceLastSummary(messages.slice(0, -N_MESSAGES_TO_KEEP))
if (messagesToSummarize.length <= 1) {
Expand Down
16 changes: 14 additions & 2 deletions src/core/sliding-window/__tests__/sliding-window.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,13 @@ describe("truncateConversationIfNeeded", () => {
})

// Verify summarizeConversation was called with the right parameters
expect(summarizeSpy).toHaveBeenCalledWith(messagesWithSmallContent, mockApiHandler, "System prompt", taskId)
expect(summarizeSpy).toHaveBeenCalledWith(
messagesWithSmallContent,
mockApiHandler,
"System prompt",
taskId,
true,
)

// Verify the result contains the summary information
expect(result).toMatchObject({
Expand Down Expand Up @@ -663,7 +669,13 @@ describe("truncateConversationIfNeeded", () => {
})

// Verify summarizeConversation was called with the right parameters
expect(summarizeSpy).toHaveBeenCalledWith(messagesWithSmallContent, mockApiHandler, "System prompt", taskId)
expect(summarizeSpy).toHaveBeenCalledWith(
messagesWithSmallContent,
mockApiHandler,
"System prompt",
taskId,
true,
)

// Verify the result contains the summary information
expect(result).toMatchObject({
Expand Down
2 changes: 1 addition & 1 deletion src/core/sliding-window/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export async function truncateConversationIfNeeded({
const contextPercent = (100 * prevContextTokens) / contextWindow
if (contextPercent >= autoCondenseContextPercent || prevContextTokens > allowedTokens) {
// Attempt to intelligently condense the context
const result = await summarizeConversation(messages, apiHandler, systemPrompt, taskId)
const result = await summarizeConversation(messages, apiHandler, systemPrompt, taskId, true)
if (result.summary) {
return { ...result, prevContextTokens }
}
Expand Down
4 changes: 2 additions & 2 deletions src/services/telemetry/TelemetryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ class TelemetryService {
this.captureEvent(PostHogClient.EVENTS.TASK.CHECKPOINT_RESTORED, { taskId })
}

public captureContextCondensed(taskId: string): void {
this.captureEvent(PostHogClient.EVENTS.TASK.CONTEXT_CONDENSED, { taskId })
public captureContextCondensed(taskId: string, isAutomaticTrigger: boolean): void {
this.captureEvent(PostHogClient.EVENTS.TASK.CONTEXT_CONDENSED, { taskId, isAutomaticTrigger })
}

public captureSlidingWindowTruncation(taskId: string): void {
Expand Down
Loading