Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

8.6 KB
Raw Blame
import type { UIMessage } from '@ai-sdk/react'
import { generateText, type LanguageModel } from 'ai'
import { stripToolImageSentinels } from './tool-image-sentinel'

/**
 * Approximate token count using a character-based heuristic.
 *
 * On average, 1 token ≈ 4 characters for English text across most
 * tokenizers (GPT, Claude, etc.). This is intentionally conservative
 * so the trimmer leaves a safety margin.
 */
const CHARS_PER_TOKEN = 3.5

export function estimateTokens(text: string): number {
  if (!text) return 0
  return Math.ceil(text.length / CHARS_PER_TOKEN)
}

/**
 * Share of the model's context window a single tool result may occupy.
 *
 * One result should never crowd out the conversation that gives it meaning, so
 * this is deliberately a minority of the window: enough for a substantial page
 * or file, far short of the whole budget.
 */
const TOOL_OUTPUT_FRACTION_OF_CONTEXT = 0.25

/**
 * Per-tool-result character budget derived from the active model's context
 * window, so a bigger window earns a proportionally bigger allowance instead of
 * being held to one hardcoded number.
 *
 * Returns `undefined` when the window is unknown - remote providers often don't
 * report one - leaving the user's configured cap in sole charge.
 */
export function deriveToolOutputCap(
  contextWindowTokens: number | undefined
): number | undefined {
  if (!contextWindowTokens || contextWindowTokens <= 0) return undefined
  return Math.floor(
    contextWindowTokens * CHARS_PER_TOKEN * TOOL_OUTPUT_FRACTION_OF_CONTEXT
  )
}

/**
 * What one tool-result image costs the model. Vision encoders turn an image
 * into a roughly fixed number of tokens, not one per base64 character, so
 * counting the characters would put a 1 MB screenshot at hundreds of thousands
 * of tokens and evict the user's question from the window.
 */
const TOOL_IMAGE_TOKENS = 2_000
const TOOL_IMAGE_STAND_IN = 'x'.repeat(
  Math.ceil(TOOL_IMAGE_TOKENS * CHARS_PER_TOKEN)
)

function messageToText(message: UIMessage): string {
  const parts: string[] = []
  for (const part of message.parts) {
    if (part.type === 'text') {
      parts.push(part.text)
    } else if (part.type === 'dynamic-tool' || part.type.startsWith('tool-')) {
      // Swap each image's base64 for a stand-in of `TOOL_IMAGE_TOKENS`. An
      // image takes two forms in a tool output: an MCP `{ type: 'image',
      // data }` item and an inline tool image sentinel inside a string.
      parts.push(
        JSON.stringify(part, function (this: { type?: unknown }, key, value) {
          if (typeof value !== 'string') return value
          if (key === 'data' && this.type === 'image') return TOOL_IMAGE_STAND_IN
          return stripToolImageSentinels(value, TOOL_IMAGE_STAND_IN)
        })
      )
    }
  }

  const metadata = message.metadata as
    | { inline_file_contents?: Array<{ name?: string; content?: string }> }
    | undefined
  if (Array.isArray(metadata?.inline_file_contents)) {
    for (const file of metadata.inline_file_contents) {
      if (file?.content) {
        parts.push(`File: ${file.name || 'attachment'}\n${file.content}`)
      }
    }
  }

  return parts.join('\n')
}

export function estimateMessageTokens(message: UIMessage): number {
  const text = messageToText(message)
  // Add a small overhead per message for role/formatting tokens
  return estimateTokens(text) + 4
}

/** Tokens a whole conversation already occupies in the context window. */
export function estimateConversationTokens(
  messages: readonly UIMessage[] | undefined
): number {
  return (messages ?? []).reduce(
    (sum, message) => sum + estimateMessageTokens(message),
    0
  )
}

export interface ContextManagerConfig {
  maxContextTokens: number
  maxOutputTokens: number
  autoCompact: boolean
}

export interface TrimResult {
  messages: UIMessage[]
  trimmedCount: number
  compactedSummary?: string
}

/**
 * Trim messages to fit within the context budget.
 *
 * Strategy:
 * 1. Always keep the system prompt (counted separately) and the most recent message
 * 2. Walk backwards from the newest message, accumulating tokens
 * 3. Drop the oldest messages that don't fit
 * 4. Never drop the first user message if it would leave no context
 */
export function trimMessages(
  messages: UIMessage[],
  config: ContextManagerConfig,
  systemPromptTokens: number = 0
): TrimResult {
  const { maxContextTokens, maxOutputTokens } = config

  if (maxContextTokens <= 0) {
    return { messages, trimmedCount: 0 }
  }

  const inputBudget = maxContextTokens - maxOutputTokens - systemPromptTokens
  if (inputBudget <= 0) {
    return { messages: messages.slice(-1), trimmedCount: messages.length - 1 }
  }

  // Estimate tokens for each message
  const estimates = messages.map((msg) => ({
    message: msg,
    tokens: estimateMessageTokens(msg),
  }))

  // Walk backwards, accumulating tokens
  let totalTokens = 0
  const kept: UIMessage[] = []

  for (let i = estimates.length - 1; i >= 0; i--) {
    const { message, tokens } = estimates[i]
    if (totalTokens + tokens > inputBudget && kept.length > 0) {
      break
    }
    totalTokens += tokens
    kept.unshift(message)
  }

  // Ensure we always have at least the last message
  if (kept.length === 0 && messages.length > 0) {
    kept.push(messages[messages.length - 1])
  }

  return {
    messages: kept,
    trimmedCount: messages.length - kept.length,
  }
}

const COMPACT_SYSTEM_PROMPT =
  'You are a conversation summarizer. Produce a concise summary that preserves ' +
  'key facts, decisions, code snippets, and action items. Use bullet points. ' +
  'Keep the summary under 500 words.'

/**
 * Summarize older messages that would be trimmed, then prepend the summary
 * as a system-style user message so the model retains context.
 */
export async function compactMessages(
  messages: UIMessage[],
  config: ContextManagerConfig,
  model: LanguageModel,
  systemPromptTokens: number = 0
): Promise<TrimResult> {
  const { maxContextTokens, maxOutputTokens } = config

  if (maxContextTokens <= 0) {
    return { messages, trimmedCount: 0 }
  }

  const inputBudget = maxContextTokens - maxOutputTokens - systemPromptTokens
  if (inputBudget <= 0) {
    return { messages: messages.slice(-1), trimmedCount: messages.length - 1 }
  }

  // First figure out which messages would be kept/dropped
  const trimResult = trimMessages(messages, config, systemPromptTokens)

  if (trimResult.trimmedCount === 0) {
    return trimResult
  }

  const droppedMessages = messages.slice(0, trimResult.trimmedCount)

  // Build conversation text from dropped messages
  const conversationText = droppedMessages
    .map((m) => {
      const text = m.parts
        .filter((p): p is { type: 'text'; text: string } => p.type === 'text')
        .map((p) => p.text)
        .join('')
      return `${m.role}: ${text}`
    })
    .join('\n\n')

  if (!conversationText.trim()) {
    return trimResult
  }

  // The summarization call itself uses context: system prompt + conversation
  // excerpt + summary output. Cap the excerpt to ~70% of the context budget
  // (in characters, using the same heuristic) so the call doesn't exceed limits.
  const summaryOutputTokens = 512
  const summaryBudgetTokens = Math.max(
    1024,
    maxContextTokens - summaryOutputTokens - estimateTokens(COMPACT_SYSTEM_PROMPT)
  )
  const maxExcerptChars = Math.floor(summaryBudgetTokens * CHARS_PER_TOKEN)

  const truncated =
    conversationText.length > maxExcerptChars
      ? conversationText.slice(-maxExcerptChars)
      : conversationText

  try {
    const { text: summary } = await generateText({
      model,
      system: COMPACT_SYSTEM_PROMPT,
      prompt: `Summarize this conversation excerpt:\n\n${truncated}`,
      maxOutputTokens: summaryOutputTokens,
    })

    // Inject the summary as a system message so models treat it as context
    // rather than as a user turn (which could confuse turn-taking logic).
    const summaryMessage: UIMessage = {
      id: `compact-summary-${Date.now()}`,
      role: 'system',
      parts: [
        {
          type: 'text',
          text: `[Previous conversation summary]\n${summary}`,
        },
      ],
    }

    // Re-trim: the summary message itself consumes tokens, so the combined
    // set (summary + kept messages) may exceed the input budget. Run
    // trimMessages again on the merged list to guarantee we stay within
    // the context window.
    const merged = [summaryMessage, ...trimResult.messages]
    const refit = trimMessages(merged, config, systemPromptTokens)

    return {
      messages: refit.messages,
      trimmedCount: trimResult.trimmedCount + refit.trimmedCount,
      compactedSummary: summary,
    }
  } catch (error) {
    console.warn('Auto-compact summarization failed, falling back to trim:', error)
    return trimResult
  }
}