File size: 13,176 Bytes
391c43e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// lib/llm/core/context-manager.ts
// Portable context manager β€” no browser imports, no VFS, no Next.js, no configManager.

import type {
  Message,
  ParsedResponse,
  ToolResult,
  ProviderAdapter,
  CompactionConfig,
  ContextManager,
  ContentBlock,
  UsageInfo,
} from './types';

export class ContextManagerImpl implements ContextManager {
  private messages: Message[] = [];
  private compactionCount = 0;
  onMessageAdded?: (message: Message) => void;
  onMessagesReplaced?: (newMessages: Message[]) => void;

  constructor(private config: CompactionConfig) {}

  getMessages(): Message[] {
    return this.messages;
  }

  getCompactionCount(): number {
    return this.compactionCount;
  }

  setSystemPrompt(prompt: string): void {
    if (this.messages.length > 0 && this.messages[0].role === 'system') {
      this.messages[0] = { role: 'system', content: prompt };
    } else {
      this.messages.unshift({ role: 'system', content: prompt });
    }
  }

  addUserMessage(content: string | ContentBlock[]): void {
    const msg: Message = { role: 'user', content };
    this.messages.push(msg);
    this.onMessageAdded?.(msg);
  }

  addAssistantTurn(response: ParsedResponse): void {
    // Sanitize tool call arguments: providers reject invalid JSON in history
    if (response.toolCalls?.length) {
      for (const tc of response.toolCalls) {
        if (tc.function?.arguments) {
          try { JSON.parse(tc.function.arguments); } catch {
            tc.function.arguments = '{}';
          }
        }
      }
    }
    const msg: Message = {
      role: 'assistant',
      content: response.content || '',
      ...(response.toolCalls?.length ? { tool_calls: response.toolCalls } : {}),
      ...(response.reasoningDetails?.length ? { reasoning_details: response.reasoningDetails } : {}),
    };
    this.messages.push(msg);
    this.onMessageAdded?.(msg);
  }

  addToolResults(results: ToolResult[]): void {
    for (const r of results) {
      const msg: Message = { role: 'tool', content: r.content, tool_call_id: r.tool_call_id };
      this.messages.push(msg);
      this.onMessageAdded?.(msg);
    }
  }

  importMessages(messages: Message[]): void {
    this.messages = [...messages];
  }

  getTokenEstimate(): number {
    return this.messages.reduce((sum, m) => sum + ContextManagerImpl.estimateMessageTokens(m), 0);
  }

  needsCompaction(tokenCount: number): boolean {
    return tokenCount >= this.config.threshold;
  }

  /**
   * Returns messages with orphan tool calls repaired.
   * Operates on a copy; never mutates the persistent conversation history.
   */
  getSanitizedMessages(): Message[] {
    return this.repairOrphanToolCalls(this.messages);
  }

  /**
   * Compact the conversation by summarizing older messages and keeping recent ones.
   *
   * Strategy:
   *  - System prompt: replaced with opts.freshSystemPrompt
   *  - Older ~80% of non-system messages: sent for summarization via provider
   *  - Recent ~20% of non-system messages: kept verbatim
   *  - Summary output capped at ~10% of contextLength
   */
  async compact(
    provider: ProviderAdapter,
    opts?: { freshSystemPrompt?: string; projectContext?: string; signal?: AbortSignal }
  ): Promise<UsageInfo | undefined> {
    // 1. Separate system messages from conversation messages
    const systemMessages = this.messages.filter(m => m.role === 'system');
    const nonSystemMessages = this.messages.filter(m => m.role !== 'system');

    if (nonSystemMessages.length < 3) {
      return undefined;
    }

    // 2. Group non-system messages into "turns" (assistant + its tool results).
    // A turn is: one assistant message (possibly with tool_calls) + all following
    // tool-role messages that belong to it. User messages are standalone turns.
    // This ensures we never orphan a tool result from its assistant message.
    interface Turn { messages: Message[]; tokens: number }
    const turns: Turn[] = [];
    let currentTurn: Turn | null = null;

    for (const msg of nonSystemMessages) {
      if (msg.role === 'tool') {
        // Tool results attach to the current turn (started by assistant)
        if (currentTurn) {
          const t = ContextManagerImpl.estimateMessageTokens(msg);
          currentTurn.messages.push(msg);
          currentTurn.tokens += t;
        }
      } else {
        // assistant or user β€” starts a new turn
        if (currentTurn) turns.push(currentTurn);
        const t = ContextManagerImpl.estimateMessageTokens(msg);
        currentTurn = { messages: [msg], tokens: t };
      }
    }
    if (currentTurn) turns.push(currentTurn);

    if (turns.length < 2) {
      return undefined;
    }

    // Walk backwards from the end, keeping whole turns within the recent budget.
    // Always keep at least 1 turn.
    const recentTokenBudget = Math.round(this.config.contextLength * this.config.recentKeepRatio);
    let recentTokens = 0;
    let recentTurnCount = 0;

    for (let i = turns.length - 1; i >= 0; i--) {
      if (recentTurnCount >= 1 && recentTokens + turns[i].tokens > recentTokenBudget) {
        break;
      }
      recentTokens += turns[i].tokens;
      recentTurnCount++;
    }

    const splitTurnIndex = turns.length - recentTurnCount;

    if (splitTurnIndex <= 0) {
      return undefined;
    }

    const olderMessages = turns.slice(0, splitTurnIndex).flatMap(t => t.messages);
    const recentMessages = turns.slice(splitTurnIndex).flatMap(t => t.messages);

    // 3. Convert older messages to plain text for summarization.
    // Models hallucinate tool calls when they see tool_calls/tool messages in history,
    // even without tool definitions. Flatten everything to user/assistant text.
    const flattenedMessages: Message[] = [];
    for (const msg of olderMessages) {
      if (msg.role === 'assistant') {
        // Merge tool call info into text content
        let text = typeof msg.content === 'string' ? msg.content : '';
        if (msg.tool_calls) {
          for (const tc of msg.tool_calls) {
            const args = tc.function?.arguments || '';
            // Truncate very large tool args (file contents) to save tokens
            const truncatedArgs = args.length > 500 ? args.slice(0, 500) + '...[truncated]' : args;
            text += `\n[Called ${tc.function?.name}(${truncatedArgs})]`;
          }
        }
        if (text.trim()) {
          flattenedMessages.push({ role: 'assistant', content: text.trim() });
        }
      } else if (msg.role === 'tool') {
        // Convert tool result to user message (tools role confuses models without tool defs)
        const content = typeof msg.content === 'string' ? msg.content : JSON.stringify(msg.content);
        const truncated = content.length > 500 ? content.slice(0, 500) + '...[truncated]' : content;
        if (truncated.trim()) {
          flattenedMessages.push({ role: 'user', content: `[Tool result: ${truncated.trim()}]` });
        }
      } else {
        flattenedMessages.push(msg);
      }
    }

    // Merge consecutive same-role messages (some APIs reject adjacent same-role)
    const mergedMessages: Message[] = [];
    for (const msg of flattenedMessages) {
      const last = mergedMessages[mergedMessages.length - 1];
      if (last && last.role === msg.role && typeof last.content === 'string' && typeof msg.content === 'string') {
        last.content += '\n' + msg.content;
      } else {
        mergedMessages.push({ ...msg });
      }
    }

    // Extract previous compaction summary for iterative awareness
    let previousSummary: string | undefined;
    for (const msg of olderMessages) {
      if (msg.role === 'assistant' && msg.metadata?.isCompactSummary) {
        previousSummary = typeof msg.content === 'string'
          ? msg.content.replace(/^Here is a summary of the conversation so far:\n\n/, '')
          : undefined;
      }
    }

    // Build the compaction request messages
    const compactionMessages: Message[] = [
      ...systemMessages,
      ...mergedMessages,
      { role: 'user', content: this.config.buildCompactionPrompt(previousSummary) },
    ];

    const summaryMaxTokens = Math.min(
      16384,
      Math.max(256, Math.round(this.config.contextLength * this.config.summaryTokenRatio))
    );

    // Call provider for summarization (silent β€” no progress events in main chat)
    const result = await provider.call({
      messages: compactionMessages,
      maxTokens: summaryMaxTokens,
      signal: opts?.signal,
      silent: true,
    });

    const summary = result.content || '';
    if (!summary) {
      return undefined;
    }

    // 4. Rebuild conversation:
    //    [fresh system prompt] + [project context as user msg] + [summary as assistant] + [recent messages]
    let freshFromConfig: { systemPrompt?: string; projectContext?: string } = {};
    if ((!opts?.freshSystemPrompt || !opts?.projectContext) && this.config.getFreshContext) {
      try {
        freshFromConfig = await this.config.getFreshContext();
      } catch { /* fall back to stale system prompt */ }
    }
    const freshSystemPrompt = opts?.freshSystemPrompt
      || freshFromConfig.systemPrompt
      || (systemMessages[0] && typeof systemMessages[0].content === 'string' ? systemMessages[0].content : '');
    const summaryContent = `Here is a summary of the conversation so far:\n\n${summary}`;

    const projectContext = opts?.projectContext ?? freshFromConfig.projectContext;
    const contextUserContent = projectContext
      ? `${projectContext}\n\nThe earlier conversation was compacted into the summary below.`
      : 'The earlier conversation was compacted into the summary below.';

    this.messages = [
      { role: 'system', content: freshSystemPrompt },
      { role: 'user', content: contextUserContent },
      { role: 'assistant', content: summaryContent, metadata: { isCompactSummary: true } },
      ...recentMessages,
    ];

    this.compactionCount++;
    this.onMessagesReplaced?.(this.messages);

    return result.usage;
  }

  /**
   * Repair orphan tool calls in a message array.
   *
   * Two common issues fixed:
   *  (1) tool_calls with empty arguments β€” provider sent a malformed call.
   *      Drop the offending entry. If the message ends up empty, drop it.
   *  (2) tool_calls with no matching tool result β€” call was cancelled mid-flight.
   *      Synthesize a role:'tool' placeholder so the sequence is well-formed.
   *
   * Operates on a copy; never mutates the persistent conversation history.
   */
  private repairOrphanToolCalls(messages: Message[]): Message[] {
    const out: Message[] = [];

    for (let i = 0; i < messages.length; i++) {
      const msg = messages[i];

      if (msg.role !== 'assistant' || !msg.tool_calls || msg.tool_calls.length === 0) {
        out.push(msg);
        continue;
      }

      // Filter out tool calls with empty arguments and repair invalid JSON args
      const validCalls = msg.tool_calls.filter(tc => {
        const args = tc.function?.arguments;
        if (typeof args !== 'string' || args.trim() === '') return false;
        try {
          JSON.parse(args);
        } catch {
          // Truncated/malformed args β€” replace with empty object so the
          // conversation history stays valid JSON for every provider.
          tc.function.arguments = '{}';
        }
        return true;
      });

      const contentEmpty = typeof msg.content === 'string'
        ? msg.content.trim() === ''
        : !msg.content || (Array.isArray(msg.content) && msg.content.length === 0);

      if (validCalls.length === 0) {
        // No valid tool calls remain
        if (contentEmpty) {
          // Drop the entire message
          continue;
        }
        // Keep message without tool_calls
        const { tool_calls: _, ...rest } = msg;
        out.push(rest);
        continue;
      }

      // Push assistant with only valid calls
      out.push({ ...msg, tool_calls: validCalls });

      // Check which tool calls have matching results in subsequent messages
      const matchedIds = new Set<string>();
      for (let j = i + 1; j < messages.length; j++) {
        const next = messages[j];
        if (next.role === 'assistant') break;
        if (next.role === 'tool' && next.tool_call_id) {
          matchedIds.add(next.tool_call_id);
        }
      }

      // Inject synthetic results for unmatched calls
      for (const tc of validCalls) {
        if (!matchedIds.has(tc.id)) {
          out.push({
            role: 'tool',
            tool_call_id: tc.id,
            content: 'No result β€” call was cancelled or aborted before completion.',
          });
        }
      }
    }

    return out;
  }

  /**
   * Estimate token count of a message (content + tool call arguments).
   * Uses char/3.5 heuristic β€” fast and reasonable for context budgeting.
   */
  static estimateMessageTokens(msg: Message): number {
    const contentLen = typeof msg.content === 'string' ? msg.content.length : JSON.stringify(msg.content).length;
    const argsLen = msg.tool_calls?.reduce((s, tc) => s + (tc.function?.arguments?.length ?? 0), 0) ?? 0;
    return Math.round((contentLen + argsLen) / 3.5);
  }
}