fix(context): normalize attachments in status analysis (#1022)

This commit is contained in:
程序员阿江(Relakkes) 2026-07-17 18:33:19 +08:00
parent 2d58d11fb1
commit 0bbfc62907
2 changed files with 63 additions and 1 deletions

View File

@ -0,0 +1,59 @@
import { afterAll, beforeAll, describe, expect, test } from 'bun:test'
import { mkdtemp, rm } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
let configDir: string
const originalConfigDir = process.env.CLAUDE_CONFIG_DIR
const originalSimpleMode = process.env.CLAUDE_CODE_SIMPLE
describe('analyzeContextUsage', () => {
beforeAll(async () => {
configDir = await mkdtemp(join(tmpdir(), 'cc-haha-analyze-context-'))
process.env.CLAUDE_CONFIG_DIR = configDir
process.env.CLAUDE_CODE_SIMPLE = '1'
})
afterAll(async () => {
if (originalConfigDir === undefined) {
delete process.env.CLAUDE_CONFIG_DIR
} else {
process.env.CLAUDE_CONFIG_DIR = originalConfigDir
}
if (originalSimpleMode === undefined) {
delete process.env.CLAUDE_CODE_SIMPLE
} else {
process.env.CLAUDE_CODE_SIMPLE = originalSimpleMode
}
await rm(configDir, { recursive: true, force: true })
})
test('analyzes attachment messages for the context status view', async () => {
const { analyzeContextUsage } = await import('./analyzeContext.js')
const result = await analyzeContextUsage(
[
{
type: 'attachment',
attachment: { type: 'directory', path: configDir },
uuid: 'issue-1022',
timestamp: '2026-07-17T00:00:00.000Z',
},
],
'claude-sonnet-4-20250514',
async () => ({ mode: 'default' }),
[],
{ activeAgents: [], allAgents: [] },
undefined,
undefined,
undefined,
undefined,
{ estimateOnly: true },
)
expect(result.messageBreakdown?.attachmentTokens).toBeGreaterThan(0)
expect(result.messageBreakdown?.attachmentsByType).toEqual([
{ name: 'directory', tokens: expect.any(Number) },
])
})
})

View File

@ -60,7 +60,10 @@ import { logForDebugging } from './debug.js'
import { isEnvTruthy } from './envUtils.js'
import { errorMessage, toError } from './errors.js'
import { logError } from './log.js'
import { normalizeMessagesForAPI } from './messages.js'
import {
normalizeAttachmentForAPI,
normalizeMessagesForAPI,
} from './messages.js'
import { getRuntimeMainLoopModel } from './model/model.js'
import { isFirstPartyAnthropicBaseUrl } from './model/providers.js'
import type { SettingSource } from './settings/constants.js'