MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / summarizeConversation

Function summarizeConversation

src/core/condense/index.ts:256–510  ·  view source on GitHub ↗
(options: SummarizeConversationOptions)

Source from the content-addressed store, hash-verified

254 * getEnvironmentDetails() in recursivelyMakeClineRequests().
255 */
256export async function summarizeConversation(options: SummarizeConversationOptions): Promise<SummarizeResponse> {
257 const {
258 messages,
259 apiHandler,
260 systemPrompt,
261 taskId,
262 isAutomaticTrigger,
263 customCondensingPrompt,
264 metadata,
265 environmentDetails,
266 filesReadByRoo,
267 cwd,
268 rooIgnoreController,
269 } = options
270 TelemetryService.instance.captureContextCondensed(
271 taskId,
272 isAutomaticTrigger ?? false,
273 !!customCondensingPrompt?.trim(),
274 )
275
276 const response: SummarizeResponse = { messages, cost: 0, summary: "" }
277
278 // Get messages to summarize (all messages since the last summary, if any)
279 const messagesToSummarize = getMessagesSinceLastSummary(messages)
280
281 if (messagesToSummarize.length <= 1) {
282 const error =
283 messages.length <= 1
284 ? t("common:errors.condense_not_enough_messages")
285 : t("common:errors.condensed_recently")
286 return { ...response, error }
287 }
288
289 // Check if there's a recent summary in the messages (edge case)
290 const recentSummaryExists = messagesToSummarize.some((message: ApiMessage) => message.isSummary)
291
292 if (recentSummaryExists && messagesToSummarize.length <= 2) {
293 const error = t("common:errors.condensed_recently")
294 return { ...response, error }
295 }
296
297 // Use custom prompt if provided and non-empty, otherwise use the default CONDENSE prompt
298 // This respects user's custom condensing prompt setting
299 const condenseInstructions = customCondensingPrompt?.trim() || supportPrompt.default.CONDENSE
300
301 const finalRequestMessage: Anthropic.MessageParam = {
302 role: "user",
303 content: condenseInstructions,
304 }
305
306 // Inject synthetic tool_results for orphan tool_calls to prevent API rejections
307 // (e.g., when user triggers condense after receiving attempt_completion but before responding)
308 const messagesWithToolResults = injectSyntheticToolResults(messagesToSummarize)
309
310 // Transform tool_use and tool_result blocks to text representations.
311 // This is necessary because some providers (like Bedrock via LiteLLM) require the `tools` parameter
312 // when tool blocks are present. By converting them to text, we can send the conversation for
313 // summarization without needing to pass the tools parameter.

Callers 5

condenseContextMethod · 0.90
manageContextFunction · 0.90
condense.spec.tsFile · 0.90
index.spec.tsFile · 0.90

Calls 12

tFunction · 0.90
maybeRemoveImageBlocksFunction · 0.90
extractCommandBlocksFunction · 0.85
errorMethod · 0.65
createMessageMethod · 0.65
countTokensMethod · 0.65
StringInterface · 0.50

Tested by

no test coverage detected