Changeset 0.15.1 (#73)

This commit is contained in:
2026-02-27 00:12:46 +00:00
parent e663104575
commit 1370d701af
7 changed files with 693 additions and 16 deletions

View File

@@ -25,6 +25,25 @@ const Tokens = {
return total;
},
// Estimate tokens for a set of attachments (staged or sent).
// Images: ~765 tokens (base tile estimate, reasonable cross-provider).
// Documents with extracted text: text length / 4.
// Other/unknown: raw size / 4 (rough fallback).
estimateAttachments(attachments) {
if (!attachments?.length) return 0;
let total = 0;
for (const att of attachments) {
if (att.content_type?.startsWith('image/')) {
total += 765; // base tile estimate
} else if (att.extracted_text) {
total += this.estimate(att.extracted_text);
} else if (att.size_bytes) {
total += Math.ceil(att.size_bytes / 4);
}
}
return total;
},
// Get context budget for current model
getContextBudget() {
const caps = UI.getSelectedModelCaps();
@@ -65,9 +84,17 @@ function updateInputTokens() {
// Show relative to available context
const chat = App.chats.find(c => c.id === App.currentChatId);
const convTokens = Tokens.estimateConversation(chat?.messages || [], App.settings.systemPrompt);
const totalWithInput = convTokens + inputTokens;
// Include staged attachment estimates
const attTokens = Tokens.estimateAttachments(
(App.stagedAttachments || []).filter(a => a.status !== 'error').map(a => ({
content_type: a.contentType,
size_bytes: a.sizeBytes,
}))
);
const totalWithInput = convTokens + inputTokens + attTokens;
const pct = totalWithInput / budget.maxContext;
el.textContent = `~${Tokens.format(inputTokens)} tokens · ${Tokens.format(totalWithInput)} / ${Tokens.format(budget.maxContext)} context`;
const attLabel = attTokens > 0 ? ` +${Tokens.format(attTokens)} files` : '';
el.textContent = `~${Tokens.format(inputTokens)} tokens${attLabel} · ${Tokens.format(totalWithInput)} / ${Tokens.format(budget.maxContext)} context`;
el.className = 'input-token-count' + (pct > 0.9 ? ' danger' : pct > 0.75 ? ' warning' : '');
} else {
el.textContent = `~${Tokens.format(inputTokens)} tokens`;