@anyforge/control 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,6 @@
1
1
  import { promises as fs } from 'fs';
2
+ import { execFileSync } from 'child_process';
3
+ import { homedir } from 'os';
2
4
  import { join } from 'path';
3
5
  import open from 'open';
4
6
  import prompts from 'prompts';
@@ -13,8 +15,18 @@ export async function runInit(opts) {
13
15
  console.log(bold('AnyForge Control — device authorisation'));
14
16
  // eslint-disable-next-line no-console
15
17
  console.log(dim(` endpoint: ${endpoint}`));
16
- // 1. Issue a one-time code.
17
- const initRes = await fetch(`${endpoint}/api/cli/init`, { method: 'POST' });
18
+ // 1. Issue a one-time code. We detect the git remote up-front when
19
+ // running in a repo so it can be attached to the API key later.
20
+ // Detection happens before scope-pick so the server has it
21
+ // regardless of which scope the operator chooses.
22
+ const cwd0 = process.cwd();
23
+ const gitRoot0 = findGitRoot(cwd0);
24
+ const earlyRepo = gitRoot0 ? detectGitRemoteRepo(gitRoot0) : null;
25
+ const initRes = await fetch(`${endpoint}/api/cli/init`, {
26
+ method: 'POST',
27
+ headers: { 'content-type': 'application/json' },
28
+ body: JSON.stringify({ repo: earlyRepo }),
29
+ });
18
30
  const init = (await initRes.json());
19
31
  if (!init.ok || !init.data) {
20
32
  throw new Error(`Could not start the authorisation flow: ${init.error ?? 'unknown error'}`);
@@ -49,7 +61,7 @@ export async function runInit(opts) {
49
61
  // contract and avoids accidentally writing settings into a
50
62
  // subdirectory the operator didn't mean to target.
51
63
  const cwd = process.cwd();
52
- const repoRoot = (await isGitRepoRoot(cwd)) ? cwd : null;
64
+ const repoRoot = findGitRoot(cwd);
53
65
  const scope = await pickScope(repoRoot);
54
66
  // 6. Patch the chosen target. apiKey is inlined into the env map
55
67
  // so Claude Code can read it without a shell restart.
@@ -65,13 +77,32 @@ export async function runInit(opts) {
65
77
  // eslint-disable-next-line no-console
66
78
  console.log(dim(' Set ANTHROPIC_BASE_URL=' + endpoint + '/v1/anthropic and'));
67
79
  // eslint-disable-next-line no-console
68
- console.log(dim(' ANTHROPIC_API_KEY=<your AnyForge key> manually if needed.'));
80
+ console.log(dim(' ANTHROPIC_AUTH_TOKEN=<your AnyForge key> manually if needed.'));
69
81
  }
70
82
  else {
71
83
  summarisePatches(patches);
72
84
  }
73
85
  // eslint-disable-next-line no-console
74
86
  console.log();
87
+ if (await hasClaudeCodeLogin()) {
88
+ // Claude Code shows an "Auth conflict" warning when both a /login
89
+ // credential and an env-token are set — even though the token wins
90
+ // by precedence (#2 vs #6), the warning is cosmetic-but-loud. The
91
+ // only way to silence it is to stay logged out: /logout once, then
92
+ // skip the login prompt on the next launch.
93
+ // eslint-disable-next-line no-console
94
+ console.log(yellow(' ⚠ Existing Claude Code login detected (~/.claude/.credentials.json).'));
95
+ // eslint-disable-next-line no-console
96
+ console.log(dim(' Claude Code will work, but it will warn that both a token and a'));
97
+ // eslint-disable-next-line no-console
98
+ console.log(dim(' /login key are set. To silence the warning:'));
99
+ // eslint-disable-next-line no-console
100
+ console.log(dim(' 1) run `claude /logout`,'));
101
+ // eslint-disable-next-line no-console
102
+ console.log(dim(' 2) on the next launch, skip the login prompt (Esc / Ctrl+C).'));
103
+ // eslint-disable-next-line no-console
104
+ console.log(dim(' The AnyForge token in settings.local.json is enough on its own.'));
105
+ }
75
106
  if (scope === 'repo') {
76
107
  // eslint-disable-next-line no-console
77
108
  console.log(dim(' Re-run this command in another repo to patch it too,'));
@@ -112,17 +143,33 @@ async function pollForToken(args) {
112
143
  }
113
144
  throw new Error('Authorisation timed out (15 min). Re-run `anyforge init`.');
114
145
  }
115
- async function isGitRepoRoot(cwd) {
116
- // git worktrees use a `.git` file pointing at the real gitdir, so
117
- // accept either a directory or a regular file.
146
+ async function hasClaudeCodeLogin() {
147
+ // Claude Code's docs document the credentials file at
148
+ // ~/.claude/.credentials.json (Linux/Windows) or the macOS Keychain.
149
+ // We can only see the file on Linux/Windows. On macOS the keychain
150
+ // is invisible to us, so this check is best-effort — false negatives
151
+ // are fine since the warning is just a hint.
118
152
  try {
119
- const stat = await fs.stat(join(cwd, '.git'));
120
- return stat.isDirectory() || stat.isFile();
153
+ await fs.access(join(homedir(), '.claude', '.credentials.json'));
154
+ return true;
121
155
  }
122
156
  catch {
123
157
  return false;
124
158
  }
125
159
  }
160
+ function findGitRoot(cwd) {
161
+ // Works from any subdirectory — git walks up the tree to find the root.
162
+ try {
163
+ return execFileSync('git', ['rev-parse', '--show-toplevel'], {
164
+ cwd,
165
+ encoding: 'utf8',
166
+ stdio: ['pipe', 'pipe', 'pipe'],
167
+ }).trim();
168
+ }
169
+ catch {
170
+ return null;
171
+ }
172
+ }
126
173
  async function pickScope(repoRoot) {
127
174
  if (!repoRoot) {
128
175
  // eslint-disable-next-line no-console
@@ -152,6 +199,36 @@ function summarisePatches(patches) {
152
199
  console.log(` ${dim('•')} ${p.label} ${dim('(' + p.path + ')')}`);
153
200
  }
154
201
  }
202
+ /**
203
+ * Read the `origin` remote URL and normalise it to `owner/repo`.
204
+ * Handles both SSH (`git@github.com:owner/repo.git`) and HTTPS forms.
205
+ * Returns null when there is no remote, git is unavailable, or the
206
+ * URL can't be parsed into an owner/repo pair.
207
+ *
208
+ * Uses execFileSync (not exec/execSync) so no shell interpolation
209
+ * occurs — the args array is passed directly to the git binary.
210
+ */
211
+ function detectGitRemoteRepo(repoRoot) {
212
+ try {
213
+ const raw = execFileSync('git', ['remote', 'get-url', 'origin'], {
214
+ cwd: repoRoot,
215
+ encoding: 'utf8',
216
+ stdio: ['pipe', 'pipe', 'pipe'],
217
+ }).trim();
218
+ // SSH: git@github.com:owner/repo.git
219
+ const sshMatch = raw.match(/^git@[\w.-]+:([\w.-]+\/[\w.-]+?)(?:\.git)?$/);
220
+ if (sshMatch)
221
+ return sshMatch[1];
222
+ // HTTPS: https://github.com/owner/repo[.git]
223
+ const httpsMatch = raw.match(/^https?:\/\/[\w.-]+\/([\w.-]+\/[\w.-]+?)(?:\.git)?$/);
224
+ if (httpsMatch)
225
+ return httpsMatch[1];
226
+ return null;
227
+ }
228
+ catch {
229
+ return null;
230
+ }
231
+ }
155
232
  // Re-exported so future commands (e.g. status) can reuse the prompt
156
233
  // shape without duplicating the question text.
157
234
  export { prompts };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anyforge/control",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "AnyForge Control CLI — governed LLM proxy for Claude Code, Cursor, Cline, and any agent that talks to OpenAI- or Anthropic-shape APIs.",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://anyforge.ai",