copillm 0.4.2 → 0.4.5

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.
@@ -20,7 +20,8 @@ import { writeCommandOutput, writeHealthOutput } from "../shared/output.js";
20
20
  import { spawnDetachedDaemon } from "../daemon/spawnDetached.js";
21
21
  import { resolveRestartDecision } from "../daemon/restart.js";
22
22
  import { selfUpdateToLatest, describeSelfUpdate } from "../daemon/selfUpdate.js";
23
- import { getPackageInfo } from "../packageInfo.js";
23
+ import { computeVersionStatus } from "../daemon/versionStatus.js";
24
+ import { getPackageInfo } from "../../config/packageInfo.js";
24
25
  export function register(program) {
25
26
  program
26
27
  .command("start")
@@ -267,11 +268,13 @@ export function register(program) {
267
268
  .command("status")
268
269
  .description("Show daemon status")
269
270
  .option("--json", "JSON output")
271
+ .option("--no-registry-check", "Skip the npm registry lookup for latest-version comparison (also via NO_UPDATE_NOTIFIER / COPILLM_UPDATE_CHECK=0)")
270
272
  .action(async (opts) => {
271
273
  const config = loadConfig();
272
274
  const lockState = inspectLock();
273
275
  const checkedAtIso = new Date().toISOString();
274
276
  const uptimeSeconds = lockState.state === "running" ? computeUptimeSeconds(lockState.lock.started_at_iso) : null;
277
+ const cliPackageInfo = getPackageInfo();
275
278
  // inspectStoredCredential never returns the token itself, so it's safe to
276
279
  // include the result in the status payload.
277
280
  let authInfo;
@@ -303,6 +306,16 @@ export function register(program) {
303
306
  health_state: null,
304
307
  health_error: null,
305
308
  health_status: "unknown",
309
+ // Version reporting — populated below. cli_version is always known
310
+ // (read from this CLI binary). daemon_version is `null` until the
311
+ // /healthz probe completes; older daemons (predating the field) also
312
+ // report null. latest_version is best-effort and `null` if the
313
+ // registry lookup is skipped or fails.
314
+ daemon_version: null,
315
+ cli_version: cliPackageInfo.version,
316
+ latest_version: null,
317
+ update_available: false,
318
+ version_hint: null,
306
319
  checked_at_iso: checkedAtIso,
307
320
  stale_reason: lockState.state === "stale" ? lockState.reason : null
308
321
  };
@@ -313,7 +326,19 @@ export function register(program) {
313
326
  status.health_check_status_code = health.statusCode;
314
327
  status.health_state = health.status;
315
328
  status.health_error = health.error;
329
+ status.daemon_version = health.version;
316
330
  }
331
+ // Commander turns `--no-registry-check` into `opts.registryCheck === false`.
332
+ const versionStatus = await computeVersionStatus({
333
+ cliPackageInfo,
334
+ daemonVersion: status.daemon_version,
335
+ daemonRunning: status.running,
336
+ noRegistryCheck: opts.registryCheck === false
337
+ });
338
+ status.daemon_version = versionStatus.daemon_version;
339
+ status.latest_version = versionStatus.latest_version;
340
+ status.update_available = versionStatus.update_available;
341
+ status.version_hint = versionStatus.hint;
317
342
  if (opts.json) {
318
343
  process.stdout.write(JSON.stringify(status, null, 2) + "\n");
319
344
  return;
@@ -332,6 +357,7 @@ export function register(program) {
332
357
  process.stdout.write(` error=${status.health_error}`);
333
358
  }
334
359
  process.stdout.write("\n");
360
+ writeVersionLine(status);
335
361
  if (status.bearer_ttl_seconds !== null) {
336
362
  process.stdout.write(`bearer_ttl_seconds: ${status.bearer_ttl_seconds}\n`);
337
363
  }
@@ -344,10 +370,12 @@ export function register(program) {
344
370
  }
345
371
  if (lockState.state === "stale") {
346
372
  process.stdout.write(`stale lock (${lockState.reason})\n`);
373
+ writeVersionLine(status);
347
374
  writeAuthStatusLine(authInfo);
348
375
  return;
349
376
  }
350
377
  process.stdout.write("not running\n");
378
+ writeVersionLine(status);
351
379
  writeAuthStatusLine(authInfo);
352
380
  });
353
381
  program
@@ -453,6 +481,37 @@ function toDaemonLockState(lockState) {
453
481
  }
454
482
  return { state: "missing" };
455
483
  }
484
+ /**
485
+ * Render the `version: ...` line for `copillm status` human output.
486
+ *
487
+ * When the daemon is running and reports the same version as this CLI, the
488
+ * line is just `version: 0.4.3` — the common, boring case. Mismatches surface
489
+ * inline so users see immediately whether they need to restart (cli > daemon)
490
+ * or `npm install -g copillm` (latest > cli). When the daemon is stopped, the
491
+ * line only reports the CLI's version, since there is no live daemon version
492
+ * to compare against.
493
+ */
494
+ function writeVersionLine(status) {
495
+ let line = "version: ";
496
+ if (status.running && status.daemon_version !== null) {
497
+ line += status.daemon_version;
498
+ if (status.daemon_version !== status.cli_version) {
499
+ line += ` (cli ${status.cli_version})`;
500
+ }
501
+ }
502
+ else if (status.running && status.daemon_version === null) {
503
+ // Either an older daemon that predates this field, or a malformed
504
+ // /healthz response. The version_hint below will prompt a restart.
505
+ line += `? (cli ${status.cli_version})`;
506
+ }
507
+ else {
508
+ line += `cli ${status.cli_version}`;
509
+ }
510
+ if (status.version_hint) {
511
+ line += ` — ${status.version_hint}`;
512
+ }
513
+ process.stdout.write(line + "\n");
514
+ }
456
515
  /**
457
516
  * Load the shared credential/config/discovery context for `copillm start`'s
458
517
  * codex + pi init steps, ONLY when at least one of them is going to run.
@@ -94,7 +94,8 @@ export async function probeHealth(port, options) {
94
94
  statusCode: response.status,
95
95
  status: typeof payload.status === "string" ? payload.status : null,
96
96
  error: typeof payload.error === "string" ? payload.error : null,
97
- bearerTtlSeconds: response.ok && typeof payload.bearer_ttl_seconds === "number" ? payload.bearer_ttl_seconds : null
97
+ bearerTtlSeconds: response.ok && typeof payload.bearer_ttl_seconds === "number" ? payload.bearer_ttl_seconds : null,
98
+ version: typeof payload.version === "string" && payload.version.length > 0 ? payload.version : null
98
99
  },
99
100
  failed: false
100
101
  };
@@ -104,7 +105,7 @@ export async function probeHealth(port, options) {
104
105
  }
105
106
  }, options);
106
107
  return result.failed
107
- ? { ok: false, bearerTtlSeconds: null, statusCode: null, status: null, error: "health_probe_failed" }
108
+ ? { ok: false, bearerTtlSeconds: null, statusCode: null, status: null, error: "health_probe_failed", version: null }
108
109
  : result.ok;
109
110
  }
110
111
  export async function readLiveLock() {
@@ -0,0 +1,63 @@
1
+ import { fetchLatestNpmVersion, isNewerVersion, parseBooleanOverride } from "../updateNotifier.js";
2
+ const DEFAULT_REGISTRY_TIMEOUT_MS = 1_500;
3
+ /**
4
+ * Resolve the three version data points (daemon, cli, latest-on-npm) and
5
+ * compute the `copillm status` actionable hint from them. Pulled out of the
6
+ * status command so it stays unit-testable end-to-end without spinning up a
7
+ * real proxy.
8
+ *
9
+ * Opt-out precedence (any of these skips the npm lookup):
10
+ * - `noRegistryCheck` argument (driven by the `--no-registry-check` flag)
11
+ * - `NO_UPDATE_NOTIFIER` env (matches the update-notifier convention)
12
+ * - `COPILLM_UPDATE_CHECK=0|false|no|off`
13
+ *
14
+ * The registry lookup itself is best-effort: timeouts / network errors
15
+ * silently yield `latest_version: null`. We do *not* surface a "registry
16
+ * unreachable" message in status — that would be more noise than signal for
17
+ * a daemon-status command.
18
+ */
19
+ export async function computeVersionStatus(options) {
20
+ const env = options.env ?? process.env;
21
+ const cliVersion = options.cliPackageInfo.version;
22
+ const daemonVersion = options.daemonVersion;
23
+ const registryCheckDisabled = options.noRegistryCheck === true ||
24
+ "NO_UPDATE_NOTIFIER" in env ||
25
+ parseBooleanOverride(env.COPILLM_UPDATE_CHECK) === false;
26
+ let latestVersion = null;
27
+ if (!registryCheckDisabled) {
28
+ latestVersion = await fetchLatestNpmVersion(options.cliPackageInfo.name, {
29
+ fetchImpl: options.fetchImpl,
30
+ registryUrl: options.registryUrl ?? env.COPILLM_UPDATE_REGISTRY_URL,
31
+ timeoutMs: options.timeoutMs ?? DEFAULT_REGISTRY_TIMEOUT_MS
32
+ });
33
+ }
34
+ const daemonStale = options.daemonRunning && daemonVersion !== null && isNewerVersion(cliVersion, daemonVersion);
35
+ const cliStale = latestVersion !== null && isNewerVersion(latestVersion, cliVersion);
36
+ const daemonReportsNoVersion = options.daemonRunning && daemonVersion === null;
37
+ return {
38
+ daemon_version: daemonVersion,
39
+ cli_version: cliVersion,
40
+ latest_version: latestVersion,
41
+ update_available: daemonStale || cliStale,
42
+ hint: buildVersionHint({ daemonStale, cliStale, daemonReportsNoVersion, cliVersion, latestVersion })
43
+ };
44
+ }
45
+ /**
46
+ * Pure helper. Public for direct unit testing of the messaging matrix.
47
+ */
48
+ export function buildVersionHint(input) {
49
+ const parts = [];
50
+ if (input.cliStale && input.latestVersion !== null) {
51
+ parts.push(`newer version available: v${input.latestVersion} (npm install -g copillm)`);
52
+ }
53
+ if (input.daemonStale) {
54
+ parts.push(`restart to apply cli v${input.cliVersion}`);
55
+ }
56
+ if (parts.length === 0 && input.daemonReportsNoVersion) {
57
+ return "restart to start reporting version";
58
+ }
59
+ if (parts.length === 0) {
60
+ return null;
61
+ }
62
+ return parts.join("; ");
63
+ }
package/dist/cli/index.js CHANGED
@@ -11,7 +11,7 @@ import * as piCmd from "./commands/agents/pi.js";
11
11
  import * as copilotCmd from "./commands/agents/copilot.js";
12
12
  import { setRootLogger, setRootProgram } from "./shared/debug.js";
13
13
  import { applyDevModeEnv } from "./shared/devMode.js";
14
- import { getPackageInfo } from "./packageInfo.js";
14
+ import { getPackageInfo } from "../config/packageInfo.js";
15
15
  import { maybeNotifyAboutUpdate } from "./updateNotifier.js";
16
16
  const logger = createLogger();
17
17
  const program = new Command();
@@ -67,6 +67,24 @@ export function distTagsUrl(packageName, registryUrl = DEFAULT_REGISTRY_URL) {
67
67
  export function isNewerVersion(candidate, current) {
68
68
  return compareSemver(candidate, current) > 0;
69
69
  }
70
+ /**
71
+ * Parse `"1" | "true" | "yes" | "on"` → true, `"0" | "false" | "no" | "off"` → false,
72
+ * anything else → null. Exposed so the status command can mirror the
73
+ * `COPILLM_UPDATE_CHECK` opt-out semantics without re-implementing the parsing.
74
+ */
75
+ export function parseBooleanOverride(value) {
76
+ if (value === undefined) {
77
+ return null;
78
+ }
79
+ const normalized = value.trim().toLowerCase();
80
+ if (["1", "true", "yes", "on"].includes(normalized)) {
81
+ return true;
82
+ }
83
+ if (["0", "false", "no", "off"].includes(normalized)) {
84
+ return false;
85
+ }
86
+ return null;
87
+ }
70
88
  function shouldRunUpdateCheck(opts) {
71
89
  if (opts.stderr.isTTY !== true)
72
90
  return false;
@@ -168,19 +186,6 @@ function notifyIfNewer(stderr, packageInfo, latestVersion) {
168
186
  function hasArg(argv, arg) {
169
187
  return argv.slice(2).includes(arg);
170
188
  }
171
- function parseBooleanOverride(value) {
172
- if (value === undefined) {
173
- return null;
174
- }
175
- const normalized = value.trim().toLowerCase();
176
- if (["1", "true", "yes", "on"].includes(normalized)) {
177
- return true;
178
- }
179
- if (["0", "false", "no", "off"].includes(normalized)) {
180
- return false;
181
- }
182
- return null;
183
- }
184
189
  function isTruthyCi(value) {
185
190
  if (value === undefined) {
186
191
  return false;
@@ -1,7 +1,7 @@
1
1
  import { createRequire } from "node:module";
2
2
  const FALLBACK_PACKAGE_INFO = {
3
3
  name: "copillm",
4
- version: "0.4.2"
4
+ version: "0.4.5"
5
5
  };
6
6
  export function getPackageInfo() {
7
7
  const envName = cleanPackageValue(process.env.COPILLM_PACKAGE_NAME);
@@ -2,6 +2,7 @@ import fs from "node:fs";
2
2
  import { z } from "zod";
3
3
  import { accountModelsCacheReadPath, modelsCacheReadPath } from "../config/home.js";
4
4
  import { assertValidAccountId } from "../config/accountId.js";
5
+ import { toAnthropicSurfaceModelId } from "./claudeModelId.js";
5
6
  export const ANTHROPIC_FAMILIES = ["opus", "sonnet", "haiku"];
6
7
  const SUFFIX_BLOCKLIST = [
7
8
  "-high",
@@ -27,12 +28,20 @@ export function computeAnthropicDefaults(modelIds) {
27
28
  byFamily[family].push(id);
28
29
  }
29
30
  }
31
+ // The picked ids feed the ANTHROPIC_DEFAULT_{OPUS,SONNET,HAIKU}_MODEL env
32
+ // vars Claude Code reads, so they must be in Claude Code's dash-separated
33
+ // surface form (e.g. `claude-sonnet-4-6`). A dotted upstream id like
34
+ // `claude-sonnet-4.6` would canonicalise to the deprecated `claude-sonnet-4-0`
35
+ // inside Claude Code (see src/models/claudeModelId.ts).
30
36
  return {
31
- opus: pickPlainLatest(byFamily.opus),
32
- sonnet: pickPlainLatest(byFamily.sonnet),
33
- haiku: pickPlainLatest(byFamily.haiku)
37
+ opus: toSurfaceModelId(pickPlainLatest(byFamily.opus)),
38
+ sonnet: toSurfaceModelId(pickPlainLatest(byFamily.sonnet)),
39
+ haiku: toSurfaceModelId(pickPlainLatest(byFamily.haiku))
34
40
  };
35
41
  }
42
+ function toSurfaceModelId(modelId) {
43
+ return modelId === null ? null : toAnthropicSurfaceModelId(modelId);
44
+ }
36
45
  export function readModelIdsFromCache(accountId) {
37
46
  let file;
38
47
  if (accountId === undefined) {
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Bidirectional mapping between the **upstream Copilot** Claude model id form
3
+ * and the form Claude Code's model registry expects.
4
+ *
5
+ * GitHub Copilot's catalog names Claude models with a dotted minor version:
6
+ *
7
+ * claude-sonnet-4.6 claude-opus-4.8 claude-haiku-4.5
8
+ *
9
+ * Claude Code, however, keys its internal model registry on a dash-separated
10
+ * form (`claude-sonnet-4-6`) and carries a legacy canonicaliser that maps any
11
+ * unrecognised `claude-sonnet-4…` / `claude-opus-4…` string to the original,
12
+ * now-deprecated `claude-sonnet-4-0` / `claude-opus-4-0` (extracted from the
13
+ * Claude Code binary):
14
+ *
15
+ * if (e.includes("claude-sonnet-4-6")) return "claude-sonnet-4-6";
16
+ * if (e.includes("claude-sonnet-4-5")) return "claude-sonnet-4-5";
17
+ * if (/claude-sonnet-4(?!-\d(?!\d))/.test(e)) return "claude-sonnet-4-0"; // deprecated
18
+ * …same shape for opus…
19
+ *
20
+ * A dotted id like `claude-sonnet-4.6` slips past the specific `includes`
21
+ * checks (they look for a dash) and is caught by the loose regex, so Claude
22
+ * Code believes it is running the deprecated "Sonnet 4" — it injects
23
+ * `You are powered by the model named Sonnet 4` into the system prompt and
24
+ * shows a retirement warning. The dash-separated `claude-sonnet-4-6` is
25
+ * matched by the specific branch first and is not deprecated.
26
+ *
27
+ * So copillm advertises / exports the **dash** form to Claude Code, and
28
+ * rewrites it back to the **dotted** upstream form before forwarding a request
29
+ * to Copilot (which only accepts the dotted id — a dashed id returns upstream
30
+ * 400 `model_not_supported`).
31
+ *
32
+ * Both transforms are scoped to `claude-` ids and only touch the trailing
33
+ * `<major>.<minor>` / `<major>-<minor>` version segment, so they are exact
34
+ * inverses for every Copilot Claude id (each has a single trailing dotted
35
+ * version). Non-claude ids (gpt, gemini — the only ids with mid-string dots
36
+ * such as `gpt-5.3-codex`) are returned unchanged.
37
+ */
38
+ const CLAUDE_ID_PREFIX = "claude-";
39
+ function isClaudeId(modelId) {
40
+ return modelId.toLowerCase().startsWith(CLAUDE_ID_PREFIX);
41
+ }
42
+ /**
43
+ * Upstream (dotted) -> Claude Code surface (dashed).
44
+ * `claude-sonnet-4.6` -> `claude-sonnet-4-6`. No-op for non-claude ids and for
45
+ * ids without a trailing dotted version.
46
+ */
47
+ export function toAnthropicSurfaceModelId(modelId) {
48
+ if (!isClaudeId(modelId)) {
49
+ return modelId;
50
+ }
51
+ return modelId.replace(/(\d)\.(\d+)$/, "$1-$2");
52
+ }
53
+ /**
54
+ * Claude Code surface (dashed) -> upstream (dotted).
55
+ * `claude-sonnet-4-6` -> `claude-sonnet-4.6`. No-op for non-claude ids and for
56
+ * ids whose trailing segment is not a `<digit>-<digits>` version (e.g. an
57
+ * already-dotted id passes through unchanged).
58
+ */
59
+ export function toUpstreamModelId(modelId) {
60
+ if (!isClaudeId(modelId)) {
61
+ return modelId;
62
+ }
63
+ return modelId.replace(/(\d)-(\d+)$/, "$1.$2");
64
+ }
@@ -1,3 +1,4 @@
1
+ import { toAnthropicSurfaceModelId } from "../models/claudeModelId.js";
1
2
  /**
2
3
  * Claude Code's only client-side marker for a 1M-context-budget model is a
3
4
  * literal `[1m]` suffix on the model id (matched in its binary via
@@ -57,13 +58,18 @@ export function buildAnthropicModelsResponse(models) {
57
58
  * picker — a label that would imply 1M-class behaviour Claude Code would
58
59
  * never deliver for that vendor.
59
60
  *
61
+ * The base id is first mapped to Claude Code's dash-separated surface form
62
+ * (`claude-sonnet-4.6` -> `claude-sonnet-4-6`) via `toAnthropicSurfaceModelId`
63
+ * so the advertised id is not mistaken for the deprecated `claude-sonnet-4-0`
64
+ * (see src/models/claudeModelId.ts).
65
+ *
60
66
  * Models already carrying the suffix are left alone. Models below the 1M
61
67
  * threshold get no alias regardless of name — Claude Code has no
62
68
  * client-side marker for the 200K-1M intermediate range, and over-claiming
63
69
  * would set the wrong autocompact trigger.
64
70
  */
65
71
  export function applyOneMillionAlias(model) {
66
- const baseId = typeof model.id === "string" ? model.id : "";
72
+ const baseId = toAnthropicSurfaceModelId(typeof model.id === "string" ? model.id : "");
67
73
  if (baseId.endsWith(ONE_M_ALIAS_SUFFIX)) {
68
74
  return baseId;
69
75
  }
@@ -1,5 +1,6 @@
1
1
  import { createServer } from "node:http";
2
2
  import { randomUUID } from "node:crypto";
3
+ import { getPackageInfo } from "../config/packageInfo.js";
3
4
  import { singleAccountResolver } from "./accountResolver.js";
4
5
  import { attachRequestLifecycle, isBenignSocketError, safeEnd, safeSendJson } from "./requestLifecycle.js";
5
6
  import { InvalidRequestShapeError, JsonRequestParseError, RequestBodyTooLargeError } from "./errors.js";
@@ -11,6 +12,7 @@ import { handleProxyForward } from "./routes/proxyForward.js";
11
12
  import { isLocalRequest, resolveRoute, safePathname } from "./routes/shared.js";
12
13
  export async function startProxyServer(input) {
13
14
  const debugEnabled = input.debug === true;
15
+ const packageVersion = getPackageInfo().version;
14
16
  const resolver = input.accountResolver ??
15
17
  singleAccountResolver({
16
18
  tokenManager: input.tokenManager,
@@ -57,10 +59,10 @@ export async function startProxyServer(input) {
57
59
  }
58
60
  switch (route.kind) {
59
61
  case "livez":
60
- handleLivez(res);
62
+ handleLivez(res, { version: packageVersion });
61
63
  return;
62
64
  case "healthz":
63
- await handleHealthz(res, resolver.default.tokenManager);
65
+ await handleHealthz(res, resolver.default.tokenManager, { version: packageVersion });
64
66
  return;
65
67
  case "models":
66
68
  await handleModels(res, route.kind, resolver.default);
@@ -86,7 +88,8 @@ export async function startProxyServer(input) {
86
88
  tokenManager: resolver.default.tokenManager,
87
89
  githubToken: resolver.default.githubToken,
88
90
  port: input.port,
89
- accounts: resolver.describe()
91
+ accounts: resolver.describe(),
92
+ packageVersion
90
93
  });
91
94
  return;
92
95
  case "not_found":
@@ -35,6 +35,7 @@ export async function handleDebug(res, input) {
35
35
  port: input.port,
36
36
  pid: process.pid,
37
37
  node_version: process.version,
38
+ version: input.packageVersion ?? null,
38
39
  started_at_iso: DAEMON_STARTED_AT_ISO,
39
40
  uptime_seconds: uptimeSeconds,
40
41
  account_type: input.config.accountType,
@@ -1,17 +1,22 @@
1
1
  import { healthFailure } from "../errors.js";
2
2
  import { safeSendJson } from "../requestLifecycle.js";
3
3
  const HEALTH_REFRESH_THRESHOLD_SECONDS = 60;
4
- export function handleLivez(res) {
5
- safeSendJson(res, 200, { status: "ok", uptime_seconds: Math.floor(process.uptime()) });
4
+ export function handleLivez(res, meta) {
5
+ safeSendJson(res, 200, {
6
+ status: "ok",
7
+ uptime_seconds: Math.floor(process.uptime()),
8
+ version: meta.version
9
+ });
6
10
  }
7
- export async function handleHealthz(res, tokenManager) {
11
+ export async function handleHealthz(res, tokenManager, meta) {
8
12
  const ttl = tokenManager.expiresInSeconds();
9
13
  if (ttl !== null && ttl > HEALTH_REFRESH_THRESHOLD_SECONDS) {
10
14
  safeSendJson(res, 200, {
11
15
  status: "ok",
12
16
  token_state: "fresh",
13
17
  refresh_threshold_seconds: HEALTH_REFRESH_THRESHOLD_SECONDS,
14
- bearer_ttl_seconds: ttl
18
+ bearer_ttl_seconds: ttl,
19
+ version: meta.version
15
20
  });
16
21
  return;
17
22
  }
@@ -22,11 +27,12 @@ export async function handleHealthz(res, tokenManager) {
22
27
  status: "ok",
23
28
  token_state: "refreshed",
24
29
  refresh_threshold_seconds: HEALTH_REFRESH_THRESHOLD_SECONDS,
25
- bearer_ttl_seconds: refreshedTtl
30
+ bearer_ttl_seconds: refreshedTtl,
31
+ version: meta.version
26
32
  });
27
33
  }
28
34
  catch (error) {
29
35
  const failed = healthFailure(error);
30
- safeSendJson(res, failed.httpStatus, failed.payload);
36
+ safeSendJson(res, failed.httpStatus, { ...failed.payload, version: meta.version });
31
37
  }
32
38
  }
@@ -1,4 +1,5 @@
1
1
  import { resolveModelId } from "../../models/discovery.js";
2
+ import { toUpstreamModelId } from "../../models/claudeModelId.js";
2
3
  import { CopilotTokenManagerError } from "../../auth/copilotToken.js";
3
4
  import { anthropicToOpenAI, ProtocolTranslationError } from "../../translation/openaiAnthropic.js";
4
5
  import { writeAnthropicPrelude } from "../../translation/streamingOpenAIToAnthropic.js";
@@ -52,7 +53,14 @@ export async function handleProxyForward(input) {
52
53
  });
53
54
  return;
54
55
  }
55
- const upstreamBody = resolvedModel ? rewriteRequestedModel(translatedBody, resolvedModel.id) : translatedBody;
56
+ // Claude Code talks to copillm in its dash-separated surface form
57
+ // (`claude-sonnet-4-6`); upstream Copilot only accepts the dotted id
58
+ // (`claude-sonnet-4.6`). Rewrite back before forwarding. A local model
59
+ // selection already resolves to the dotted catalog id, so only the
60
+ // unfiltered anthropic path needs the explicit conversion.
61
+ const upstreamModelId = resolvedModel?.id ??
62
+ (requestedModel && route.kind === "anthropic" ? toUpstreamModelId(requestedModel) : null);
63
+ const upstreamBody = upstreamModelId ? rewriteRequestedModel(translatedBody, upstreamModelId) : translatedBody;
56
64
  const upstreamPath = route.kind === "codex_responses" ? "/responses" : "/chat/completions";
57
65
  const isAnthropicStreaming = route.anthroShape && isStreamingRequestBody(translatedBody);
58
66
  let prelude = null;
@@ -101,6 +101,13 @@ export async function forwardResponse(upstream, anthroShape, res, diagnostics) {
101
101
  }
102
102
  throw error;
103
103
  }
104
+ // Echo back the model id the client requested (Claude Code's dash-separated
105
+ // surface id) rather than the dotted id upstream returns, so Claude Code
106
+ // does not re-canonicalise the response model to the deprecated
107
+ // claude-…-4-0. Streaming already does this via the prelude fallbackModel.
108
+ if (diagnostics.requestedModel && payload && typeof payload === "object") {
109
+ payload.model = diagnostics.requestedModel;
110
+ }
104
111
  }
105
112
  safeSendJson(res, 200, payload);
106
113
  }
@@ -153,7 +153,7 @@ function translateSystemToText(system) {
153
153
  return joinTextBlocks(system, "Anthropic system prompt");
154
154
  }
155
155
  function translateAnthropicMessage(message) {
156
- if (message.role !== "assistant" && message.role !== "user") {
156
+ if (message.role !== "assistant" && message.role !== "user" && message.role !== "system") {
157
157
  throw new ProtocolTranslationError("unsupported_message_role", `Unsupported Anthropic message role: ${String(message.role)}.`);
158
158
  }
159
159
  if (typeof message.content === "string") {
@@ -162,6 +162,16 @@ function translateAnthropicMessage(message) {
162
162
  if (!Array.isArray(message.content)) {
163
163
  throw new ProtocolTranslationError("invalid_message_content", "Anthropic message content must be a string or array.");
164
164
  }
165
+ if (message.role === "system") {
166
+ // Claude Code's "mid-conversation system message" feature (a beta it turns on
167
+ // for correctly-recognised current models, e.g. Opus 4.8) places role:"system"
168
+ // entries inside `messages`, not just the top-level `system` field. Anthropic's
169
+ // own API only accepts system at the top level, but copillm translates to
170
+ // OpenAI chat/completions, which supports system-role messages natively — so
171
+ // map it through instead of 400ing. A 400 here surfaces to the user as a hard
172
+ // API error rather than triggering Claude Code's <system-reminder> fallback.
173
+ return [{ role: "system", content: joinTextBlocks(message.content, "Anthropic system message") }];
174
+ }
165
175
  return message.role === "assistant"
166
176
  ? translateAssistantMessageBlocks(message.content)
167
177
  : translateUserMessageBlocks(message.content);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "copillm",
3
- "version": "0.4.2",
3
+ "version": "0.4.5",
4
4
  "description": "Local Copilot proxy CLI (OpenAI/Anthropic-compatible)",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -29,6 +29,8 @@
29
29
  "dev:start": "npm run build && node dist/cli.js --dev start",
30
30
  "dev:stop": "npm run build && node dist/cli.js --dev stop",
31
31
  "dev:status": "node dist/cli.js --dev status",
32
+ "dev:link": "node scripts/dev-link.mjs",
33
+ "dev:unlink": "node scripts/dev-unlink.mjs",
32
34
  "lint": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.tests.json --noEmit && eslint src",
33
35
  "lint:boundaries": "eslint src",
34
36
  "test": "vitest run",