fix(cli): default missing user type in user agent

This commit is contained in:
程序员阿江(Relakkes)
2026-08-03 04:44:33 +08:00
parent 90f4a0bc98
commit 94ebaf5bf4
2 changed files with 44 additions and 1 deletions
+42
View File
@@ -0,0 +1,42 @@
import { afterEach, describe, expect, test } from 'bun:test'
import { CLAUDE_CODE_COMPAT_VERSION } from '../constants/claudeCodeCompatibility.js'
import { getUserAgent } from './http.js'
const USER_AGENT_ENV_KEYS = [
'USER_TYPE',
'CLAUDE_CODE_ENTRYPOINT',
'CLAUDE_AGENT_SDK_VERSION',
'CLAUDE_AGENT_SDK_CLIENT_APP',
] as const
const originalEnv = Object.fromEntries(
USER_AGENT_ENV_KEYS.map(key => [key, process.env[key]]),
) as Record<(typeof USER_AGENT_ENV_KEYS)[number], string | undefined>
afterEach(() => {
for (const key of USER_AGENT_ENV_KEYS) {
const value = originalEnv[key]
if (value === undefined) delete process.env[key]
else process.env[key] = value
}
})
describe('getUserAgent', () => {
test('uses the external runtime label when USER_TYPE is absent', () => {
delete process.env.USER_TYPE
process.env.CLAUDE_CODE_ENTRYPOINT = 'sdk-cli'
delete process.env.CLAUDE_AGENT_SDK_VERSION
delete process.env.CLAUDE_AGENT_SDK_CLIENT_APP
expect(getUserAgent()).toBe(
`claude-cli/${CLAUDE_CODE_COMPAT_VERSION} (external, sdk-cli)`,
)
})
test('preserves an explicit runtime label', () => {
process.env.USER_TYPE = 'ant'
process.env.CLAUDE_CODE_ENTRYPOINT = 'cli'
expect(getUserAgent()).toContain('(ant, cli')
})
})
+2 -1
View File
@@ -17,6 +17,7 @@ import { getWorkload } from './workloadContext.js'
// WARNING: We rely on `claude-cli` in the user agent for log filtering.
// Please do NOT change this without making sure that logging also gets updated!
export function getUserAgent(): string {
const userType = process.env.USER_TYPE || 'external'
const agentSdkVersion = process.env.CLAUDE_AGENT_SDK_VERSION
? `, agent-sdk/${process.env.CLAUDE_AGENT_SDK_VERSION}`
: ''
@@ -32,7 +33,7 @@ export function getUserAgent(): string {
// so the read picks up the same setWorkload() value as getAttributionHeader.
const workload = getWorkload()
const workloadSuffix = workload ? `, workload/${workload}` : ''
return `claude-cli/${CLAUDE_CODE_COMPAT_VERSION} (${process.env.USER_TYPE}, ${process.env.CLAUDE_CODE_ENTRYPOINT ?? 'cli'}${agentSdkVersion}${clientApp}${workloadSuffix})`
return `claude-cli/${CLAUDE_CODE_COMPAT_VERSION} (${userType}, ${process.env.CLAUDE_CODE_ENTRYPOINT ?? 'cli'}${agentSdkVersion}${clientApp}${workloadSuffix})`
}
export function getMCPUserAgent(): string {