Skip to content

工具函数库

src/utils/ 包含 300+ 个工具函数文件,按功能分类如下。

文件系统

注意: 以下文件名部分为示意,实际文件名可能不同。

文件功能
file.ts文件读写、权限检查
path.ts路径规范化、跨平台处理
glob.tsGlob 模式匹配
tempfile.ts临时文件/目录管理(非 tempDir.ts)

Git 操作

文件功能
git.tsGit 状态、diff、log、blame
gitDiff.tsDiff 解析与展示
worktree.tsWorktree 管理(非 gitWorktree.ts)

Shell 与进程

文件功能
shell/shellProvider.tsShell 提供者(非 Shell.ts)
process.ts子进程管理
execFileNoThrow.ts命令执行封装(非 exec.ts)
signal.ts信号处理
env.ts环境变量管理

认证与安全

文件功能
auth.ts认证工具 (~800 行)
crypto.ts加密工具
sanitization.ts输入消毒(非 sanitize.ts)
permissions/权限检查(目录,非 permissions.ts)

字符串与格式

文件功能
stringUtils.ts字符串处理(非 string.ts)
format.ts格式化工具
markdown.tsMarkdown 渲染
truncate.ts文本截断
cliHighlight.ts语法高亮(非 highlight.ts)
diff.tsDiff 显示
json.tsJSON 安全解析
xml.tsXML 解析/生成

网络

文件功能
http.tsHTTP 请求封装(非 fetch.ts)
proxy.ts代理配置

终端与 UI

文件功能
terminal.ts终端检测、尺寸、能力
ansiToPng.tsANSI 转 PNG
ansiToSvg.tsANSI 转 SVG

数据结构

文件功能
CircularBuffer.ts环形缓冲区(非 ring.ts)
lockfile.ts文件锁/互斥锁(非 mutex.ts)
queueProcessor.ts队列处理(非 queue.ts)

Token 与模型

文件功能
tokens.tsToken 计数(非 tokenizer.ts)
model/模型信息查询(目录,非 modelInfo.ts)
modelCost.ts价格计算(非 pricing.ts)

平台

文件功能
platform.ts平台检测 (Windows/macOS/Linux)
browser.ts浏览器打开

关键工具函数详解

auth.ts (~800 行)

typescript
// src/utils/auth.ts — 真实导出函数
export function isAnthropicAuthEnabled(): boolean
export function getAuthTokenSource(): AuthTokenSource
export function getAnthropicApiKey(): string | null
export function hasAnthropicApiKeyAuth(): boolean
export function getConfiguredApiKeyHelper(): KeyHelper | null
export function calculateApiKeyHelperTTL(): number
export function isAwsAuthRefreshFromProjectSettings(): boolean

awsAuthRefresh 不是函数,而是一个设置项(字符串,指定用于刷新 AWS 凭证的外部命令)。通过内部的 runAwsAuthRefresh() 私有函数执行。

shell/shellProvider.ts

typescript
// src/utils/shell/shellProvider.ts — 真实代码
export type ShellProvider = {
  type: ShellType  // 'bash' | 'powershell'
  shellPath: string
  detached: boolean

  buildExecCommand(
    command: string,
    opts: { id: number | string; sandboxTmpDir?: string; useSandbox: boolean },
  ): Promise<{ commandString: string; cwdFilePath: string }>

  getSpawnArgs(commandString: string): string[]
  // ... 更多方法
}

Shell 检测优先级:CLAUDE_CODE_SHELL 环境变量 → SHELL 环境变量 → which('bash')which('sh') → PowerShell (Windows)

file.ts

src/utils/file.ts 包含文件读写相关工具函数。详细 API 请直接参阅源码。