awaySummary.ts
services/awaySummary.ts
75
Lines
2671
Bytes
1
Exports
9
Imports
10
Keywords
What this is
This page documents one file from the repository and includes its full source so you can read it without leaving the docs site.
Beginner explanation
This file is one piece of the larger system. Its name, directory, imports, and exports show where it fits. Start by reading the exports and related files first.
How it is used
Start from the exports list and related files. Those are the easiest clues for where this file fits into the system.
Expert explanation
Architecturally, this file intersects with memory-layers, compaction, integrations. It contains 75 lines, 9 detected imports, and 1 detected exports.
Important relationships
Detected exports
generateAwaySummary
Keywords
messagesmemoryutilsrecentresponsemessagelogfordebugginggetassistantmessagetextmodelsignal
Detected imports
@anthropic-ai/sdk../Tool.js../types/message.js../utils/debug.js../utils/messages.js../utils/model/model.js../utils/systemPromptType.js./api/claude.js./SessionMemory/sessionMemoryUtils.js
Source notes
This page embeds the full file contents. Small or leaf files are still indexed honestly instead of being over-explained.
Full source
import { APIUserAbortError } from '@anthropic-ai/sdk'
import { getEmptyToolPermissionContext } from '../Tool.js'
import type { Message } from '../types/message.js'
import { logForDebugging } from '../utils/debug.js'
import {
createUserMessage,
getAssistantMessageText,
} from '../utils/messages.js'
import { getSmallFastModel } from '../utils/model/model.js'
import { asSystemPrompt } from '../utils/systemPromptType.js'
import { queryModelWithoutStreaming } from './api/claude.js'
import { getSessionMemoryContent } from './SessionMemory/sessionMemoryUtils.js'
// Recap only needs recent context — truncate to avoid "prompt too long" on
// large sessions. 30 messages ≈ ~15 exchanges, plenty for "where we left off."
const RECENT_MESSAGE_WINDOW = 30
function buildAwaySummaryPrompt(memory: string | null): string {
const memoryBlock = memory
? `Session memory (broader context):\n${memory}\n\n`
: ''
return `${memoryBlock}The user stepped away and is coming back. Write exactly 1-3 short sentences. Start by stating the high-level task — what they are building or debugging, not implementation details. Next: the concrete next step. Skip status reports and commit recaps.`
}
/**
* Generates a short session recap for the "while you were away" card.
* Returns null on abort, empty transcript, or error.
*/
export async function generateAwaySummary(
messages: readonly Message[],
signal: AbortSignal,
): Promise<string | null> {
if (messages.length === 0) {
return null
}
try {
const memory = await getSessionMemoryContent()
const recent = messages.slice(-RECENT_MESSAGE_WINDOW)
recent.push(createUserMessage({ content: buildAwaySummaryPrompt(memory) }))
const response = await queryModelWithoutStreaming({
messages: recent,
systemPrompt: asSystemPrompt([]),
thinkingConfig: { type: 'disabled' },
tools: [],
signal,
options: {
getToolPermissionContext: async () => getEmptyToolPermissionContext(),
model: getSmallFastModel(),
toolChoice: undefined,
isNonInteractiveSession: false,
hasAppendSystemPrompt: false,
agents: [],
querySource: 'away_summary',
mcpTools: [],
skipCacheWrite: true,
},
})
if (response.isApiErrorMessage) {
logForDebugging(
`[awaySummary] API error: ${getAssistantMessageText(response)}`,
)
return null
}
return getAssistantMessageText(response)
} catch (err) {
if (err instanceof APIUserAbortError || signal.aborted) {
return null
}
logForDebugging(`[awaySummary] generation failed: ${err}`)
return null
}
}