Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/usage-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,8 @@ teamai import --from-repo https://github.com/org/repo --incremental
teamai import --from-repo https://github.com/org/repo --skip-enrich
```

AI-backed steps (`--deep-enrich`, knowledge enrichment) shell out to an AI coding CLI already installed on the machine instead of calling a model API directly. teamai probes `claude` → `claude-internal` → `codex` → `codex-internal` → `codebuddy` → `workbuddy` → `openclaw` and uses the first one it finds. On macOS and Linux the probe runs through a login shell, so a CLI installed under `~/.nvm/` is found too. On Windows it uses the native `where`, which returns the npm shim (`%APPDATA%\npm\claude.cmd`) that Windows can actually launch — a Git Bash or WSL `bash` only reports MSYS paths such as `/c/Users/...`, which Windows cannot start.

For GitLab behind an API gateway, set `GITLAB_URL` and `GITLAB_API_PREFIX=api/gitlab` before running `teamai import --from-org https://gitlab.example.com/myorg`. Organization listing uses the configured prefix on every page; an unset or blank prefix defaults to `api/v4`.

The graph stores components, interfaces, configs, and cross-repo dependencies. `teamai recall` uses the graph for BM25 + graph-boosted ranking.
Expand Down
2 changes: 2 additions & 0 deletions docs/usage-guide.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,8 @@ teamai import --from-repo https://github.com/org/repo --incremental
teamai import --from-repo https://github.com/org/repo --skip-enrich
```

需要 AI 的步骤(`--deep-enrich`、知识增强)复用本机已安装的 AI 编码 CLI,而不是直接调用模型 API。teamai 按 `claude` → `claude-internal` → `codex` → `codex-internal` → `codebuddy` → `workbuddy` → `openclaw` 的顺序探测,取第一个可用者。macOS / Linux 上探测经由 login shell,因此装在 `~/.nvm/` 下的 CLI 也能找到;Windows 上改用原生命令 `where`,拿到的是 Windows 真正能启动的 npm shim(`%APPDATA%\npm\claude.cmd`)——Git Bash 或 WSL 的 `bash` 只会返回 `/c/Users/...` 这类 MSYS 路径,Windows 无法启动。

对于 API 网关后的 GitLab,先设置 `GITLAB_URL` 和 `GITLAB_API_PREFIX=api/gitlab`,再运行 `teamai import --from-org https://gitlab.example.com/myorg`。组织仓库列表的每一页请求都会使用配置的前缀;未设置或为空时默认使用 `api/v4`。

图谱存储组件、接口、配置和跨仓库依赖关系。`teamai recall` 利用图谱进行 BM25 + graph-boost 增强排名。
Expand Down
134 changes: 128 additions & 6 deletions src/__tests__/ai-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,38 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import type { ChildProcess } from 'node:child_process';
import type { EventEmitter } from 'node:events';

// ─── Mock child_process ────────────────────────────────────────────────────
// ─── Mock child_process / cross-spawn ──────────────────────────────────────
// vi.mock 会被 hoist 到文件顶部,factory 中不能引用外部 const/let 变量
// 改用 vi.fn() 内联,通过 vi.mocked(spawn) 在测试中动态设置行为

// callClaude 经 cross-spawn 启动 CLI:Windows 上 CLI 多为 npm 生成的 `.cmd` shim,
// Node 原生 spawn 无权直接执行(EINVAL),必须由 cross-spawn 经 cmd.exe 转义启动。
vi.mock('cross-spawn', () => ({
default: vi.fn(),
}));

vi.mock('node:child_process', () => ({
spawn: vi.fn(),
// detectClaudeCli 通过 execFileSync('bash', ['-lc', 'command -v <cmd>']) 获取绝对路径,
// detectClaudeCli 探测绝对路径:POSIX 走 bash -lc,Windows 走 where。
// 测试环境中返回伪路径,配合 existsSync mock 让探测成功并选中第一个候选 'claude'。
execFileSync: vi.fn(() => '/usr/local/bin/claude\n'),
// Windows 分支只接受可执行后缀(.exe/.cmd/.bat),因此伪路径需要按平台给出。
execFileSync: vi.fn(() =>
process.platform === 'win32' ? 'C:\\npm\\claude.cmd\n' : '/usr/local/bin/claude\n'
),
}));

// mock existsSync,使探测到的伪路径被视为存在
vi.mock('node:fs', () => ({
existsSync: vi.fn(() => true),
}));

import { spawn } from 'node:child_process';
import { callClaude, callClaudeParallel } from '../utils/ai-client.js';
import spawn from 'cross-spawn';
import { execFileSync } from 'node:child_process';
import {
callClaude,
callClaudeParallel,
pickWindowsCommand,
resolveCliPath,
} from '../utils/ai-client.js';

// ─── Helpers ───────────────────────────────────────────────────────────────

Expand Down Expand Up @@ -190,3 +204,111 @@ describe('callClaudeParallel', () => {
expect(maxRunning).toBeLessThanOrEqual(2);
});
});

// ─── pickWindowsCommand ────────────────────────────────────────────────────
// `where` 的输出解析是纯函数,因此在所有平台上都能回归(Windows 上的真实探测
// 由这些用例固定契约,不需要 Windows runner)。

describe('pickWindowsCommand', () => {
it('npm 的 shim 三件套:跳过无扩展名的 POSIX shim,选中 .cmd', () => {
// `where claude` 的真实输出顺序:无扩展名的 shim 排在最前
const output = [
'C:\\Users\\me\\AppData\\Roaming\\npm\\claude',
'C:\\Users\\me\\AppData\\Roaming\\npm\\claude.cmd',
'',
].join('\r\n');

expect(pickWindowsCommand(output)).toBe('C:\\Users\\me\\AppData\\Roaming\\npm\\claude.cmd');
});

it('原生安装的 .exe 优先于同名 .cmd(PATHEXT 优先级)', () => {
const output = [
'C:\\tools\\claude.cmd',
'C:\\Program Files\\Claude\\claude.exe',
].join('\r\n');

expect(pickWindowsCommand(output)).toBe('C:\\Program Files\\Claude\\claude.exe');
});

it('.bat 与 .exe 并存时选中 .exe', () => {
const output = ['C:\\tools\\claude.bat', 'C:\\tools\\claude.exe'].join('\n');

expect(pickWindowsCommand(output)).toBe('C:\\tools\\claude.exe');
});

it('忽略前后空白并把扩展名匹配视为大小写不敏感', () => {
expect(pickWindowsCommand(' C:\\npm\\CLAUDE.CMD \r\n')).toBe('C:\\npm\\CLAUDE.CMD');
});

it('只有无扩展名的 shim 时返回 null(CreateProcess 无法启动它)', () => {
expect(pickWindowsCommand('C:\\npm\\claude\r\n')).toBeNull();
});

it('where 未找到任何匹配时返回 null', () => {
expect(pickWindowsCommand('')).toBeNull();
expect(pickWindowsCommand('\r\n \r\n')).toBeNull();
});
});

// ─── resolveCliPath ────────────────────────────────────────────────────────
// platform 参数可注入,使 Windows 分支在 ubuntu / macos 的 CI 上也有覆盖。

/** execFileSync 是重载函数,vi.mocked 推不出返回值类型,这里取一个够用的视图。 */
const execFileSyncMock = vi.mocked(execFileSync) as unknown as {
mock: { calls: unknown[][] };
mockReturnValueOnce: (value: unknown) => void;
mockImplementationOnce: (impl: () => never) => void;
};

describe('resolveCliPath', () => {
beforeEach(() => {
vi.clearAllMocks();
});

it('win32:走 where,返回 where 输出里可启动的那条路径', () => {
execFileSyncMock.mockReturnValueOnce('C:\\npm\\claude\r\nC:\\npm\\claude.cmd\r\n');

expect(resolveCliPath('claude', 'win32')).toBe('C:\\npm\\claude.cmd');
expect(execFileSyncMock.mock.calls[0][0]).toBe('where');
expect(execFileSyncMock.mock.calls[0][1]).toEqual(['claude']);
});

it('win32:只调用 where,不碰 bash / zsh / which', () => {
execFileSyncMock.mockReturnValueOnce('C:\\npm\\claude.cmd\r\n');

resolveCliPath('claude', 'win32');

expect(execFileSyncMock.mock.calls.map((call) => call[0])).toEqual(['where']);
});

it('win32:where 未命中(退出码非 0)时返回 null', () => {
execFileSyncMock.mockImplementationOnce(() => {
throw new Error('Command failed: where claude');
});

expect(resolveCliPath('claude', 'win32')).toBeNull();
});

it('win32:where 只给出无扩展名的 shim 时返回 null,避免 spawn EINVAL', () => {
execFileSyncMock.mockReturnValueOnce('C:\\npm\\claude\r\n');

expect(resolveCliPath('claude', 'win32')).toBeNull();
});

it('posix:优先使用 bash login shell 的结果', () => {
execFileSyncMock.mockReturnValueOnce('/usr/local/bin/claude\n');

expect(resolveCliPath('claude', 'linux')).toBe('/usr/local/bin/claude');
expect(execFileSyncMock.mock.calls[0][0]).toBe('bash');
});

it('posix:bash 抛错时回退到 zsh', () => {
execFileSyncMock.mockImplementationOnce(() => {
throw new Error('spawn bash ENOENT');
});
execFileSyncMock.mockReturnValueOnce('/opt/homebrew/bin/claude\n');

expect(resolveCliPath('claude', 'darwin')).toBe('/opt/homebrew/bin/claude');
expect(execFileSyncMock.mock.calls.map((call) => call[0])).toEqual(['bash', 'zsh']);
});
});
Loading
Loading