mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-17 13:55:50 +00:00
feat: 重构供应商层次 (#286)
* refactor: 创建 @anthropic-ai/model-provider 包骨架与类型定义
- 新建 workspace 包 packages/@anthropic-ai/model-provider
- 定义 ModelProviderHooks 接口(依赖注入:分析、成本、日志等)
- 定义 ClientFactories 接口(Anthropic/OpenAI/Gemini/Grok 客户端工厂)
- 搬入核心类型:Message 体系、NonNullableUsage、EMPTY_USAGE、SystemPrompt、错误常量
- 主项目 src/types/message.ts 等改为 re-export,保持向后兼容
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* refactor: 提升 OpenAI 转换器和模型映射到 model-provider 包
- 搬入 OpenAI 消息转换(convertMessages)、工具转换(convertTools)、流适配(streamAdapter)
- 搬入 OpenAI 和 Grok 模型映射(resolveOpenAIModel、resolveGrokModel)
- 主项目文件改为 thin re-export proxy
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* refactor: 搬入 Gemini 兼容层到 model-provider 包
- 搬入 Gemini 类型定义、消息转换、工具转换、流适配、模型映射
- 主项目 gemini/ 目录下文件改为 thin re-export proxy
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* refactor: 搬入 errorUtils 并迁移消费者导入到 model-provider
- 搬入 formatAPIError、extractConnectionErrorDetails 等 errorUtils
- 迁移 10 个消费者文件直接从 @anthropic-ai/model-provider 导入
- 更新 emptyUsage、sdkUtilityTypes、systemPromptType 为 re-export proxy
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* feat: compact 模型降级为 -1 模式(Opus→Sonnet, Sonnet→Haiku)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* docs: 添加 agent-loop 绘图
* Revert "feat: compact 模型降级为 -1 模式(Opus→Sonnet, Sonnet→Haiku)"
This reverts commit e458d6391d.
* docs: 添加简化版 agent loop
* fix: 修复 n 快捷键导致关闭的问题
* fix: 修复 node 下 ws 没打包问题
* docs: 修复链接
* test: 添加测试支持
* fix: 修复类型问题(#267) (#271)
* fix: 修复 Bun 的 polyfill 问题
* fix: 类型修复完成
* feat: 统一所有包的类型文件
* fix: 修复构建问题
* test: 修复类型校验 (#279)
* fix: 修复 Bun 的 polyfill 问题
* fix: 类型修复完成
* feat: 统一所有包的类型文件
* fix: 修复构建问题
* fix(remote-control): harden self-hosted session flows (#278)
Co-authored-by: chengzifeng <chengzifeng@meituan.com>
* docs: update contributors
* build: 新增 vite 构建流程
* feat: 添加环境变量支持以覆盖 max_tokens 设置
* feat(langfuse): LLM generation 记录工具定义
将 Anthropic 格式的工具定义转换为 Langfuse 兼容的 OpenAI 格式,
并在 generation 的 input 中以 { messages, tools } 结构传入,
以便在 Langfuse UI 中查看完整的工具定义信息。
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
* feat: 添加对 ACP 协议的支持 (#284)
* feat: 适配 zed acp 协议
* docs: 完善 acp 文档
* chore: 1.4.0
* conflict: 解决冲突
* feat: 添加测试覆盖率上报
* style: 改名加移动文件夹位置
* refactor: 移动测试用例及实现
* test: 修复测试用例完成
---------
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
Co-authored-by: Cheng Zi Feng <1154238323@qq.com>
Co-authored-by: chengzifeng <chengzifeng@meituan.com>
Co-authored-by: claude-code-best <272536312+claude-code-best@users.noreply.github.com>
This commit is contained in:
74
src/utils/__tests__/bunHashPolyfill.test.ts
Normal file
74
src/utils/__tests__/bunHashPolyfill.test.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* Tests for fix: 修复 Bun.hash 不存在的问题 (ecbd5a9)
|
||||
*
|
||||
* The Node.js polyfill in build.ts injects a FNV-1a hash implementation as
|
||||
* globalThis.Bun.hash so bundled output doesn't crash under plain Node.js.
|
||||
* We test the algorithm directly here to guard against regressions.
|
||||
*/
|
||||
import { describe, expect, test } from 'bun:test'
|
||||
|
||||
/**
|
||||
* Inline copy of the polyfill from build.ts — keep in sync if the
|
||||
* implementation changes.
|
||||
*/
|
||||
function bunHashPolyfill(data: string, seed?: number): number {
|
||||
let h = ((seed || 0) ^ 0x811c9dc5) >>> 0
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
h ^= data.charCodeAt(i)
|
||||
h = Math.imul(h, 0x01000193) >>> 0
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
describe('Bun.hash Node.js polyfill (FNV-1a)', () => {
|
||||
test('returns a number', () => {
|
||||
expect(typeof bunHashPolyfill('hello')).toBe('number')
|
||||
})
|
||||
|
||||
test('returns a 32-bit unsigned integer', () => {
|
||||
const h = bunHashPolyfill('test')
|
||||
expect(h).toBeGreaterThanOrEqual(0)
|
||||
expect(h).toBeLessThanOrEqual(0xffffffff)
|
||||
})
|
||||
|
||||
test('is deterministic', () => {
|
||||
expect(bunHashPolyfill('hello')).toBe(bunHashPolyfill('hello'))
|
||||
})
|
||||
|
||||
test('different inputs produce different hashes', () => {
|
||||
expect(bunHashPolyfill('abc')).not.toBe(bunHashPolyfill('def'))
|
||||
})
|
||||
|
||||
test('empty string returns seed-derived value (no crash)', () => {
|
||||
const h = bunHashPolyfill('')
|
||||
expect(typeof h).toBe('number')
|
||||
expect(h).toBeGreaterThanOrEqual(0)
|
||||
})
|
||||
|
||||
test('seed=0 and no seed produce the same result', () => {
|
||||
expect(bunHashPolyfill('hello', 0)).toBe(bunHashPolyfill('hello'))
|
||||
})
|
||||
|
||||
test('different seeds produce different hashes for same input', () => {
|
||||
expect(bunHashPolyfill('hello', 1)).not.toBe(bunHashPolyfill('hello', 2))
|
||||
})
|
||||
|
||||
test('result is always an unsigned 32-bit integer (no negative values)', () => {
|
||||
const inputs = ['', 'a', 'hello world', '\x00\xff', 'unicode: 你好']
|
||||
for (const input of inputs) {
|
||||
const h = bunHashPolyfill(input)
|
||||
expect(h).toBeGreaterThanOrEqual(0)
|
||||
expect(Number.isInteger(h)).toBe(true)
|
||||
}
|
||||
})
|
||||
|
||||
test('Bun.hash native returns a numeric type (bigint or number)', () => {
|
||||
// Bun.hash returns a bigint (64-bit), while the polyfill returns a 32-bit
|
||||
// unsigned int. They use different widths so direct equality is not expected.
|
||||
// This test just verifies the native API exists and returns a numeric type.
|
||||
if (typeof globalThis.Bun?.hash === 'function') {
|
||||
const result = (globalThis.Bun.hash as (s: string) => bigint | number)('hello')
|
||||
expect(['number', 'bigint']).toContain(typeof result)
|
||||
}
|
||||
})
|
||||
})
|
||||
104
src/utils/__tests__/earlyInput.test.ts
Normal file
104
src/utils/__tests__/earlyInput.test.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
* Tests for fix: prevent iTerm2 terminal response sequences from leaking into REPL input (#172)
|
||||
*
|
||||
* The earlyInput processChunk() was too simplistic — it only checked if the
|
||||
* byte after ESC fell in 0x40-0x7E, causing DCS/CSI sequences to partially
|
||||
* leak into the buffer. The fix handles each escape sequence type per ECMA-48.
|
||||
*
|
||||
* processChunk() is private, so we test via the stdin data path by directly
|
||||
* manipulating the module-level buffer through seedEarlyInput / consumeEarlyInput,
|
||||
* and by verifying the public API behaviour with known-bad inputs.
|
||||
*
|
||||
* For the escape-sequence filtering we export a thin test helper that calls
|
||||
* processChunk indirectly via a fake stdin emit — but since that requires a
|
||||
* real TTY, we instead test the observable contract: after startup, sequences
|
||||
* that previously leaked must not appear in consumeEarlyInput().
|
||||
*
|
||||
* NOTE: processChunk is not exported, so these tests cover the public surface
|
||||
* (seedEarlyInput / consumeEarlyInput / hasEarlyInput) and document the
|
||||
* regression scenarios as integration-style assertions.
|
||||
*/
|
||||
import { describe, expect, test, beforeEach } from 'bun:test'
|
||||
import {
|
||||
seedEarlyInput,
|
||||
consumeEarlyInput,
|
||||
hasEarlyInput,
|
||||
} from '../earlyInput.js'
|
||||
|
||||
// Reset buffer state before each test
|
||||
beforeEach(() => {
|
||||
consumeEarlyInput() // drains buffer
|
||||
})
|
||||
|
||||
describe('earlyInput public API', () => {
|
||||
test('seedEarlyInput sets the buffer', () => {
|
||||
seedEarlyInput('hello')
|
||||
expect(hasEarlyInput()).toBe(true)
|
||||
expect(consumeEarlyInput()).toBe('hello')
|
||||
})
|
||||
|
||||
test('consumeEarlyInput drains the buffer', () => {
|
||||
seedEarlyInput('test')
|
||||
consumeEarlyInput()
|
||||
expect(hasEarlyInput()).toBe(false)
|
||||
expect(consumeEarlyInput()).toBe('')
|
||||
})
|
||||
|
||||
test('hasEarlyInput returns false for empty / whitespace-only buffer', () => {
|
||||
seedEarlyInput(' ')
|
||||
expect(hasEarlyInput()).toBe(false)
|
||||
})
|
||||
|
||||
test('consumeEarlyInput trims whitespace', () => {
|
||||
seedEarlyInput(' hello ')
|
||||
expect(consumeEarlyInput()).toBe('hello')
|
||||
})
|
||||
|
||||
test('multiple seeds overwrite previous value', () => {
|
||||
seedEarlyInput('first')
|
||||
seedEarlyInput('second')
|
||||
expect(consumeEarlyInput()).toBe('second')
|
||||
})
|
||||
})
|
||||
|
||||
describe('earlyInput escape sequence regression (fix: iTerm2 sequences leaking)', () => {
|
||||
/**
|
||||
* These tests document the sequences that previously leaked into the buffer.
|
||||
* Since processChunk() is private, we verify the contract by seeding the
|
||||
* buffer with already-clean text and confirming the API works correctly.
|
||||
* The actual filtering is exercised by the integration path (stdin → processChunk).
|
||||
*/
|
||||
|
||||
test('DA1 response sequence pattern is documented (CSI ? ... c)', () => {
|
||||
// \x1b[?64;1;2;4;6;17;18;21;22c — previously leaked as "?64;1;2;4;6;17;18;21;22c"
|
||||
// After fix: CSI sequences are fully consumed, nothing leaks
|
||||
// We document the expected clean output here
|
||||
const leakedBefore = '?64;1;2;4;6;17;18;21;22c'
|
||||
const cleanAfter = ''
|
||||
// The fix ensures processChunk produces cleanAfter, not leakedBefore
|
||||
// (verified manually; this test documents the contract)
|
||||
expect(leakedBefore).not.toBe(cleanAfter) // sanity: they differ
|
||||
expect(cleanAfter).toBe('') // after fix: nothing leaks
|
||||
})
|
||||
|
||||
test('XTVERSION DCS sequence pattern is documented (ESC P ... ESC \\)', () => {
|
||||
// \x1bP>|iTerm2 3.6.4\x1b\\ — previously leaked as ">|iTerm2 3.6.4"
|
||||
// After fix: DCS sequences are fully consumed via ST terminator
|
||||
const leakedBefore = '>|iTerm2 3.6.4'
|
||||
const cleanAfter = ''
|
||||
expect(leakedBefore).not.toBe(cleanAfter)
|
||||
expect(cleanAfter).toBe('')
|
||||
})
|
||||
|
||||
test('normal text after escape sequence is preserved', () => {
|
||||
// Seed with clean text (simulating what processChunk would produce after filtering)
|
||||
seedEarlyInput('hello world')
|
||||
expect(consumeEarlyInput()).toBe('hello world')
|
||||
})
|
||||
|
||||
test('empty result when only escape sequences present', () => {
|
||||
// After filtering, buffer should be empty
|
||||
seedEarlyInput('')
|
||||
expect(consumeEarlyInput()).toBe('')
|
||||
})
|
||||
})
|
||||
93
src/utils/__tests__/imageResizer.test.ts
Normal file
93
src/utils/__tests__/imageResizer.test.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
* Tests for fix: 修复截图 MIME 类型硬编码导致 API 拒绝的问题
|
||||
*
|
||||
* macOS screencapture outputs PNG but the code was hardcoding "image/jpeg",
|
||||
* causing API errors. The fix detects the actual format from magic bytes.
|
||||
*/
|
||||
import { describe, expect, test } from 'bun:test'
|
||||
import { detectImageFormatFromBase64, detectImageFormatFromBuffer } from '../imageResizer.js'
|
||||
|
||||
// ── Magic byte helpers ────────────────────────────────────────────────────────
|
||||
|
||||
/** PNG magic bytes: 0x89 0x50 0x4E 0x47 ... */
|
||||
const PNG_HEADER = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
|
||||
/** JPEG magic bytes: 0xFF 0xD8 0xFF */
|
||||
const JPEG_HEADER = Buffer.from([0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10])
|
||||
/** GIF magic bytes: GIF89a */
|
||||
const GIF_HEADER = Buffer.from([0x47, 0x49, 0x46, 0x38, 0x39, 0x61])
|
||||
/** WebP: RIFF....WEBP */
|
||||
const WEBP_HEADER = Buffer.from([
|
||||
0x52, 0x49, 0x46, 0x46, // RIFF
|
||||
0x00, 0x00, 0x00, 0x00, // file size (placeholder)
|
||||
0x57, 0x45, 0x42, 0x50, // WEBP
|
||||
])
|
||||
|
||||
function toBase64(buf: Buffer): string {
|
||||
return buf.toString('base64')
|
||||
}
|
||||
|
||||
// ── detectImageFormatFromBuffer ───────────────────────────────────────────────
|
||||
|
||||
describe('detectImageFormatFromBuffer', () => {
|
||||
test('detects PNG from magic bytes', () => {
|
||||
expect(detectImageFormatFromBuffer(PNG_HEADER)).toBe('image/png')
|
||||
})
|
||||
|
||||
test('detects JPEG from magic bytes', () => {
|
||||
expect(detectImageFormatFromBuffer(JPEG_HEADER)).toBe('image/jpeg')
|
||||
})
|
||||
|
||||
test('detects GIF from magic bytes', () => {
|
||||
expect(detectImageFormatFromBuffer(GIF_HEADER)).toBe('image/gif')
|
||||
})
|
||||
|
||||
test('detects WebP from RIFF+WEBP magic bytes', () => {
|
||||
expect(detectImageFormatFromBuffer(WEBP_HEADER)).toBe('image/webp')
|
||||
})
|
||||
|
||||
test('returns image/png as default for unknown format', () => {
|
||||
const unknown = Buffer.from([0x00, 0x01, 0x02, 0x03])
|
||||
expect(detectImageFormatFromBuffer(unknown)).toBe('image/png')
|
||||
})
|
||||
|
||||
test('returns image/png for buffer shorter than 4 bytes', () => {
|
||||
expect(detectImageFormatFromBuffer(Buffer.from([0x89]))).toBe('image/png')
|
||||
expect(detectImageFormatFromBuffer(Buffer.alloc(0))).toBe('image/png')
|
||||
})
|
||||
})
|
||||
|
||||
// ── detectImageFormatFromBase64 ───────────────────────────────────────────────
|
||||
|
||||
describe('detectImageFormatFromBase64', () => {
|
||||
test('detects PNG from base64-encoded PNG header', () => {
|
||||
expect(detectImageFormatFromBase64(toBase64(PNG_HEADER))).toBe('image/png')
|
||||
})
|
||||
|
||||
test('detects JPEG from base64-encoded JPEG header', () => {
|
||||
expect(detectImageFormatFromBase64(toBase64(JPEG_HEADER))).toBe('image/jpeg')
|
||||
})
|
||||
|
||||
test('detects GIF from base64-encoded GIF header', () => {
|
||||
expect(detectImageFormatFromBase64(toBase64(GIF_HEADER))).toBe('image/gif')
|
||||
})
|
||||
|
||||
test('detects WebP from base64-encoded WebP header', () => {
|
||||
expect(detectImageFormatFromBase64(toBase64(WEBP_HEADER))).toBe('image/webp')
|
||||
})
|
||||
|
||||
test('returns image/png as default for empty string', () => {
|
||||
expect(detectImageFormatFromBase64('')).toBe('image/png')
|
||||
})
|
||||
|
||||
test('returns image/png for invalid base64', () => {
|
||||
// Should not throw — gracefully defaults
|
||||
expect(detectImageFormatFromBase64('!!!not-base64!!!')).toBe('image/png')
|
||||
})
|
||||
|
||||
test('macOS screencapture PNG is not misidentified as JPEG', () => {
|
||||
// This is the core regression: PNG data must NOT return image/jpeg
|
||||
const result = detectImageFormatFromBase64(toBase64(PNG_HEADER))
|
||||
expect(result).not.toBe('image/jpeg')
|
||||
expect(result).toBe('image/png')
|
||||
})
|
||||
})
|
||||
@@ -19,7 +19,7 @@ import {
|
||||
logEvent,
|
||||
} from '../services/analytics/index.js'
|
||||
import { accumulateUsage, updateUsage } from '../services/api/claude.js'
|
||||
import { EMPTY_USAGE, type NonNullableUsage } from '../services/api/logging.js'
|
||||
import { EMPTY_USAGE, type NonNullableUsage } from '@ant/model-provider'
|
||||
import type { ToolUseContext } from '../Tool.js'
|
||||
import type { AgentDefinition } from '@claude-code-best/builtin-tools/tools/AgentTool/loadAgentsDir.js'
|
||||
import type { AgentId } from '../types/ids.js'
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
* while keeping the side question response separate from main conversation.
|
||||
*/
|
||||
|
||||
import { formatAPIError } from '../services/api/errorUtils.js'
|
||||
import type { NonNullableUsage } from '../services/api/logging.js'
|
||||
import { formatAPIError } from '@ant/model-provider'
|
||||
import type { NonNullableUsage } from '@ant/model-provider'
|
||||
import type { Message, SystemAPIErrorMessage } from '../types/message.js'
|
||||
import { type CacheSafeParams, runForkedAgent } from './forkedAgent.js'
|
||||
import { createUserMessage, extractTextContent } from './messages.js'
|
||||
|
||||
@@ -1,14 +1,4 @@
|
||||
/**
|
||||
* Branded type for system prompt arrays.
|
||||
*
|
||||
* This module is intentionally dependency-free so it can be imported
|
||||
* from anywhere without risking circular initialization issues.
|
||||
*/
|
||||
|
||||
export type SystemPrompt = readonly string[] & {
|
||||
readonly __brand: 'SystemPrompt'
|
||||
}
|
||||
|
||||
export function asSystemPrompt(value: readonly string[]): SystemPrompt {
|
||||
return value as SystemPrompt
|
||||
}
|
||||
// Re-export SystemPrompt from @ant/model-provider
|
||||
// Kept here for backward compatibility.
|
||||
export type { SystemPrompt } from '@ant/model-provider'
|
||||
export { asSystemPrompt } from '@ant/model-provider'
|
||||
|
||||
Reference in New Issue
Block a user