catence 0.2.0-beta.17 → 0.2.0-beta.18

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.
package/README.md CHANGED
@@ -7,9 +7,9 @@ Catence is a local-first fitness MCP server for Garmin, Intervals.icu, and targe
7
7
  ## What is included
8
8
 
9
9
  - Garmin, Intervals.icu, and Strava ingestion with source-aware normalization.
10
- - Read-only MCP tools, plus explicitly named, lock-guarded Strava hydration tools.
10
+ - Read-only MCP tools, plus explicitly named, lock-guarded write tools for Strava hydration and detached syncs.
11
11
  - One shared Catence agent can serve several isolated athlete stores. Every personal-data MCP call names an `athleteId`; Catence never silently combines athletes.
12
- - A password-protected Chainlit Console with an authenticated dashboard.
12
+ - A password-protected Chainlit Console with an authenticated dashboard, a data-sync button with live progress, and in-app model management.
13
13
  - A generated demo catalog for safe evaluation in Glama, desktop MCP clients, or local development.
14
14
 
15
15
  Catence helps explore recovery, training load, trends, activity detail, swimming/cycling/running progress, segments, gear, and data quality. It reports the evidence and coverage it can see; it does not diagnose or prescribe training.
@@ -3,7 +3,7 @@
3
3
  * Keep these values synchronized with release/manifest.json; release checks
4
4
  * deliberately reject drift before a package can be published.
5
5
  */
6
- export declare const CATENCE_RUNTIME_VERSION = "0.2.0-beta.17";
6
+ export declare const CATENCE_RUNTIME_VERSION = "0.2.0-beta.18";
7
7
  export declare const CATENCE_PROTOCOL_VERSION = 1;
8
8
  export type CatenceRuntimeCapabilities = {
9
9
  mcp: true;
@@ -3,7 +3,7 @@
3
3
  * Keep these values synchronized with release/manifest.json; release checks
4
4
  * deliberately reject drift before a package can be published.
5
5
  */
6
- export const CATENCE_RUNTIME_VERSION = '0.2.0-beta.17';
6
+ export const CATENCE_RUNTIME_VERSION = '0.2.0-beta.18';
7
7
  export const CATENCE_PROTOCOL_VERSION = 1;
8
8
  export function catenceRuntimeHealth() {
9
9
  return {
@@ -12,7 +12,7 @@ declare const rateLimitSchema: z.ZodObject<{
12
12
  requests: z.ZodNumber;
13
13
  windowSeconds: z.ZodNumber;
14
14
  }, z.core.$strict>;
15
- declare const catenceConfigSchema: z.ZodObject<{
15
+ export declare const catenceConfigSchema: z.ZodObject<{
16
16
  mcp: z.ZodOptional<z.ZodObject<{
17
17
  rateLimits: z.ZodOptional<z.ZodObject<{
18
18
  server: z.ZodOptional<z.ZodNullable<z.ZodObject<{
@@ -50,6 +50,8 @@ declare const catenceConfigSchema: z.ZodObject<{
50
50
  models: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodObject<{
51
51
  label: z.ZodOptional<z.ZodString>;
52
52
  model: z.ZodString;
53
+ reasoningEffort: z.ZodOptional<z.ZodString>;
54
+ variants: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
53
55
  }, z.core.$strict>>>;
54
56
  defaultModel: z.ZodOptional<z.ZodString>;
55
57
  defaultReasoningEffort: z.ZodOptional<z.ZodEnum<{
@@ -32,9 +32,13 @@ export function requireIntervalsConfig(environment = process.env) {
32
32
  const rateLimitSchema = z.object({ requests: z.number().int().positive(), windowSeconds: z.number().int().positive() }).strict();
33
33
  const nullableRateLimitSchema = rateLimitSchema.nullable();
34
34
  const environmentVariableSchema = z.string().regex(/^[A-Z][A-Z0-9_]*$/, 'must be an uppercase environment-variable name');
35
+ // Keep this aligned with the Console's Python validator, which accepts a
36
+ // per-model reasoningEffort and free-form reasoning-effort variants.
35
37
  const consoleModelSchema = z.object({
36
38
  label: z.string().min(1).optional(),
37
39
  model: z.string().min(1),
40
+ reasoningEffort: z.string().min(1).optional(),
41
+ variants: z.record(z.string(), z.string()).optional(),
38
42
  }).strict();
39
43
  const consoleProfileSchema = z.object({
40
44
  label: z.string().min(1).optional(),
@@ -60,7 +64,7 @@ const consoleLimitsSchema = z.object({
60
64
  toolRounds: z.number().int().min(1).max(32).optional(),
61
65
  toolResultCharacters: z.number().int().min(1_000).max(250_000).optional(),
62
66
  }).strict();
63
- const catenceConfigSchema = z.object({
67
+ export const catenceConfigSchema = z.object({
64
68
  mcp: z.object({
65
69
  rateLimits: z.object({
66
70
  server: nullableRateLimitSchema.optional(),
@@ -0,0 +1,52 @@
1
+ export declare const OPENCODE_GO_DEFAULT_BASE_URL = "https://opencode.ai/zen/go/v1";
2
+ export declare const OPENCODE_GO_API_KEY_ENV = "OPENCODE_GO_API_KEY";
3
+ export declare const OPENCODE_GO_API_BASE_ENV = "OPENCODE_GO_API_BASE";
4
+ export declare const OPENCODE_GO_MESSAGES_API_BASE_ENV = "OPENCODE_GO_MESSAGES_API_BASE";
5
+ export type OpenCodeGoRoute = 'chat' | 'responses' | 'messages';
6
+ /** Live model ids plus trusted routing metadata ("live discovery + trusted metadata"). */
7
+ export declare function classifyOpenCodeGoModel(id: string): {
8
+ route: OpenCodeGoRoute;
9
+ guessed: boolean;
10
+ };
11
+ export type OpenCodeGoConsoleProfiles = {
12
+ profiles: Record<string, Record<string, unknown>>;
13
+ defaultProfile: string;
14
+ counts: {
15
+ chat: number;
16
+ responses: number;
17
+ messages: number;
18
+ };
19
+ guessedRoutes: string[];
20
+ };
21
+ /**
22
+ * Build the two ready-made Console profiles from live model ids.
23
+ *
24
+ * The public `GET /models` endpoint returns only ids, not which API each model
25
+ * needs, so routing comes from the trusted table intersected with the live
26
+ * list; prefix-guessed routes are reported so callers can warn.
27
+ */
28
+ export declare function buildOpenCodeGoConsoleProfiles(modelIds: readonly string[], baseUrl?: string): OpenCodeGoConsoleProfiles;
29
+ export declare function fetchOpenCodeGoModelIds(baseUrl?: string): Promise<string[]>;
30
+ export type OpenCodeGoMergeResult = {
31
+ configPath: string;
32
+ mergedProfileIds: string[];
33
+ defaultProfile: string;
34
+ counts: {
35
+ chat: number;
36
+ responses: number;
37
+ messages: number;
38
+ };
39
+ guessedRoutes: string[];
40
+ };
41
+ /**
42
+ * Fetch the live OpenCode Go catalog and merge the two Console profiles into
43
+ * `configPath`, preserving every other section plus existing console fields
44
+ * (`limits`, other profiles) and the existing `defaultProfile` unless
45
+ * `setDefault` is passed. The written file is re-validated with the same
46
+ * strict schema the runtime applies on load, then replaced atomically.
47
+ */
48
+ export declare function mergeOpenCodeGoConsoleProfiles(options: {
49
+ configPath: string;
50
+ baseUrl?: string;
51
+ setDefault?: boolean;
52
+ }): Promise<OpenCodeGoMergeResult>;
@@ -0,0 +1,189 @@
1
+ import { mkdir, readFile, rename, writeFile } from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import { catenceConfigSchema } from './configuration.js';
4
+ export const OPENCODE_GO_DEFAULT_BASE_URL = 'https://opencode.ai/zen/go/v1';
5
+ export const OPENCODE_GO_API_KEY_ENV = 'OPENCODE_GO_API_KEY';
6
+ export const OPENCODE_GO_API_BASE_ENV = 'OPENCODE_GO_API_BASE';
7
+ export const OPENCODE_GO_MESSAGES_API_BASE_ENV = 'OPENCODE_GO_MESSAGES_API_BASE';
8
+ // LiteLLM appends a fixed suffix to the profile base URL per provider, so the
9
+ // three APIs need different prefixes and two base URLs:
10
+ // openai/<id> -> {base}/chat/completions
11
+ // openai/responses/<id> -> {base}/responses
12
+ // anthropic/<id> -> {base minus /v1}/v1/messages
13
+ const RESPONSES_MODELS = new Set(['grok-4.5', 'gpt-5.6-luna']);
14
+ const MESSAGES_MODELS = new Set([
15
+ 'minimax-m2.5',
16
+ 'minimax-m2.7',
17
+ 'minimax-m3',
18
+ 'qwen3.5-plus',
19
+ 'qwen3.6-plus',
20
+ 'qwen3.7-plus',
21
+ 'qwen3.7-max',
22
+ 'qwen3.8-max',
23
+ ]);
24
+ const BRAND_NAMES = new Map([
25
+ ['deepseek', 'DeepSeek'],
26
+ ['glm', 'GLM'],
27
+ ['gpt', 'GPT'],
28
+ ['grok', 'Grok'],
29
+ ['hy3', 'Hy3'],
30
+ ['kimi', 'Kimi'],
31
+ ['mimo', 'MiMo'],
32
+ ['minimax', 'MiniMax'],
33
+ ['qwen', 'Qwen'],
34
+ ]);
35
+ const CHAT_PROFILE_ID = 'opencode-go';
36
+ const MESSAGES_PROFILE_ID = 'opencode-go-messages';
37
+ /** Live model ids plus trusted routing metadata ("live discovery + trusted metadata"). */
38
+ export function classifyOpenCodeGoModel(id) {
39
+ if (RESPONSES_MODELS.has(id))
40
+ return { route: 'responses', guessed: false };
41
+ if (MESSAGES_MODELS.has(id))
42
+ return { route: 'messages', guessed: false };
43
+ if (/^(grok-|gpt-)/.test(id))
44
+ return { route: 'responses', guessed: true };
45
+ if (/^(minimax-|qwen)/.test(id))
46
+ return { route: 'messages', guessed: true };
47
+ return { route: 'chat', guessed: false };
48
+ }
49
+ function modelReference(id, route) {
50
+ if (route === 'responses')
51
+ return `openai/responses/${id}`;
52
+ if (route === 'messages')
53
+ return `anthropic/${id}`;
54
+ return `openai/${id}`;
55
+ }
56
+ function labelFor(id) {
57
+ return id
58
+ .split('-')
59
+ .map((segment) => BRAND_NAMES.get(segment) ?? segment.charAt(0).toUpperCase() + segment.slice(1))
60
+ .join(' ');
61
+ }
62
+ /**
63
+ * Build the two ready-made Console profiles from live model ids.
64
+ *
65
+ * The public `GET /models` endpoint returns only ids, not which API each model
66
+ * needs, so routing comes from the trusted table intersected with the live
67
+ * list; prefix-guessed routes are reported so callers can warn.
68
+ */
69
+ export function buildOpenCodeGoConsoleProfiles(modelIds, baseUrl = OPENCODE_GO_DEFAULT_BASE_URL) {
70
+ const groups = { chat: [], responses: [], messages: [] };
71
+ const guessedRoutes = [];
72
+ for (const id of modelIds) {
73
+ if (!id)
74
+ continue;
75
+ const { route, guessed } = classifyOpenCodeGoModel(id);
76
+ groups[route].push(id);
77
+ if (guessed)
78
+ guessedRoutes.push(`${id} -> ${route}`);
79
+ }
80
+ for (const route of Object.keys(groups))
81
+ groups[route].sort();
82
+ const chatModels = {};
83
+ for (const id of [...groups.chat, ...groups.responses]) {
84
+ chatModels[id] = { label: labelFor(id), model: modelReference(id, groups.chat.includes(id) ? 'chat' : 'responses') };
85
+ }
86
+ const messagesModels = {};
87
+ for (const id of groups.messages) {
88
+ messagesModels[id] = { label: labelFor(id), model: modelReference(id, 'messages') };
89
+ }
90
+ const chatDefault = groups.chat[0] ?? groups.responses[0];
91
+ const profiles = {
92
+ [CHAT_PROFILE_ID]: {
93
+ label: 'OpenCode Go',
94
+ ...(chatDefault ? { defaultModel: chatDefault } : {}),
95
+ models: chatModels,
96
+ apiKeyEnv: OPENCODE_GO_API_KEY_ENV,
97
+ apiBaseEnv: OPENCODE_GO_API_BASE_ENV,
98
+ },
99
+ };
100
+ if (groups.messages.length) {
101
+ profiles[MESSAGES_PROFILE_ID] = {
102
+ label: 'OpenCode Go (Anthropic)',
103
+ defaultModel: groups.messages[0],
104
+ models: messagesModels,
105
+ apiKeyEnv: OPENCODE_GO_API_KEY_ENV,
106
+ apiBaseEnv: OPENCODE_GO_MESSAGES_API_BASE_ENV,
107
+ };
108
+ }
109
+ return {
110
+ profiles,
111
+ defaultProfile: CHAT_PROFILE_ID,
112
+ counts: { chat: groups.chat.length, responses: groups.responses.length, messages: groups.messages.length },
113
+ guessedRoutes,
114
+ };
115
+ }
116
+ export async function fetchOpenCodeGoModelIds(baseUrl = OPENCODE_GO_DEFAULT_BASE_URL) {
117
+ const modelsUrl = `${baseUrl.replace(/\/$/, '')}/models`;
118
+ let response;
119
+ try {
120
+ response = await fetch(modelsUrl, { headers: { accept: 'application/json' } });
121
+ }
122
+ catch (error) {
123
+ throw new Error(`Could not reach ${modelsUrl}: ${error instanceof Error ? error.message : String(error)}`);
124
+ }
125
+ if (!response.ok)
126
+ throw new Error(`GET ${modelsUrl} returned HTTP ${response.status}.`);
127
+ const payload = (await response.json());
128
+ const ids = (payload.data ?? []).map((entry) => entry.id).filter((id) => typeof id === 'string' && Boolean(id));
129
+ if (!ids.length)
130
+ throw new Error(`GET ${modelsUrl} returned no models.`);
131
+ return ids;
132
+ }
133
+ async function readConfigRoot(configPath) {
134
+ try {
135
+ const root = JSON.parse(await readFile(configPath, 'utf8'));
136
+ if (typeof root !== 'object' || root === null || Array.isArray(root)) {
137
+ throw new Error(`${configPath} must contain a JSON object.`);
138
+ }
139
+ return root;
140
+ }
141
+ catch (error) {
142
+ if (error.code === 'ENOENT')
143
+ return {};
144
+ throw error;
145
+ }
146
+ }
147
+ /**
148
+ * Fetch the live OpenCode Go catalog and merge the two Console profiles into
149
+ * `configPath`, preserving every other section plus existing console fields
150
+ * (`limits`, other profiles) and the existing `defaultProfile` unless
151
+ * `setDefault` is passed. The written file is re-validated with the same
152
+ * strict schema the runtime applies on load, then replaced atomically.
153
+ */
154
+ export async function mergeOpenCodeGoConsoleProfiles(options) {
155
+ const baseUrl = options.baseUrl ?? OPENCODE_GO_DEFAULT_BASE_URL;
156
+ const modelIds = await fetchOpenCodeGoModelIds(baseUrl);
157
+ const built = buildOpenCodeGoConsoleProfiles(modelIds, baseUrl);
158
+ const root = await readConfigRoot(options.configPath);
159
+ const existingConsole = typeof root.console === 'object' && root.console !== null && !Array.isArray(root.console)
160
+ ? root.console
161
+ : {};
162
+ const existingProfiles = typeof existingConsole.profiles === 'object' && existingConsole.profiles !== null && !Array.isArray(existingConsole.profiles)
163
+ ? existingConsole.profiles
164
+ : {};
165
+ const defaultProfile = typeof existingConsole.defaultProfile === 'string' && existingConsole.defaultProfile && !options.setDefault
166
+ ? existingConsole.defaultProfile
167
+ : built.defaultProfile;
168
+ const updatedConsole = {
169
+ ...existingConsole,
170
+ defaultProfile,
171
+ profiles: { ...existingProfiles, ...built.profiles },
172
+ };
173
+ // Validate before writing anything: the runtime rejects invalid configs at
174
+ // load time, so a bad merge must never reach disk.
175
+ const candidate = { ...root, console: updatedConsole };
176
+ catenceConfigSchema.parse(candidate);
177
+ await mkdir(path.dirname(options.configPath), { recursive: true });
178
+ const temporaryPath = `${options.configPath}.tmp`;
179
+ await writeFile(temporaryPath, `${JSON.stringify(candidate, null, 2)}\n`, 'utf8');
180
+ await rename(temporaryPath, options.configPath);
181
+ return {
182
+ configPath: options.configPath,
183
+ mergedProfileIds: Object.keys(built.profiles),
184
+ defaultProfile,
185
+ counts: built.counts,
186
+ guessedRoutes: built.guessedRoutes,
187
+ };
188
+ }
189
+ //# sourceMappingURL=opencode-go.js.map
@@ -0,0 +1,40 @@
1
+ import { type SpawnOptions } from 'node:child_process';
2
+ import type { CatencePaths } from '../../contracts/runtime.js';
3
+ export declare const DETACHED_SYNC_PROVIDERS: readonly ["intervals", "garmin", "strava", "all"];
4
+ export type DetachedSyncProvider = (typeof DETACHED_SYNC_PROVIDERS)[number];
5
+ export type DetachedSyncRequest = {
6
+ paths: CatencePaths;
7
+ athleteId: string;
8
+ provider?: DetachedSyncProvider;
9
+ from?: string;
10
+ refresh?: boolean;
11
+ };
12
+ export type DetachedSyncHandle = {
13
+ started: true;
14
+ athleteId: string;
15
+ provider: DetachedSyncProvider;
16
+ logFile: string;
17
+ };
18
+ /** Raised when another sync run is already active for this store. */
19
+ export declare class DetachedSyncBusyError extends Error {
20
+ readonly runId: string;
21
+ constructor(runId: string);
22
+ }
23
+ /** Minimal structural spawner so tests can inject a fake child process. */
24
+ export type DetachedSyncSpawner = (command: string, arguments_: readonly string[], options: SpawnOptions) => {
25
+ unref(): void;
26
+ };
27
+ /**
28
+ * Spawn one detached `catence-data sync` process for an athlete store and
29
+ * return immediately.
30
+ *
31
+ * The child outlives this process (SSH deaths, Console restarts), logs to a
32
+ * file under the catalog home, and is guarded by the same live-progress
33
+ * heartbeat check the progress tools use. The guard and the spawn are not
34
+ * atomic; the single-writer DuckDB lock remains the authoritative guard, so a
35
+ * racing second child fails cleanly in its own log file.
36
+ */
37
+ export declare function startDetachedSync(request: DetachedSyncRequest, dependencies?: {
38
+ now?: () => Date;
39
+ spawnProcess?: DetachedSyncSpawner;
40
+ }): Promise<DetachedSyncHandle>;
@@ -0,0 +1,78 @@
1
+ import { spawn } from 'node:child_process';
2
+ import { open, mkdir, writeFile } from 'node:fs/promises';
3
+ import path from 'node:path';
4
+ import { fileURLToPath } from 'node:url';
5
+ import { syncProgress } from './management.js';
6
+ export const DETACHED_SYNC_PROVIDERS = ['intervals', 'garmin', 'strava', 'all'];
7
+ /** Raised when another sync run is already active for this store. */
8
+ export class DetachedSyncBusyError extends Error {
9
+ runId;
10
+ constructor(runId) {
11
+ super(`A Catence sync run is already active (${runId}). Watch it with the progress tools instead of starting another.`);
12
+ this.name = 'DetachedSyncBusyError';
13
+ this.runId = runId;
14
+ }
15
+ }
16
+ /**
17
+ * Resolve the packaged `catence-data` CLI entry next to this module. In a
18
+ * built install both live under dist/; source checkouts run through tsx and
19
+ * tests inject their own spawner.
20
+ */
21
+ function cliScriptPath() {
22
+ return path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../interfaces/cli/main.js');
23
+ }
24
+ function assertValidRequest(request) {
25
+ if (!request.athleteId || !/^[a-z][a-z0-9-]{0,62}$/.test(request.athleteId)) {
26
+ throw new Error('athleteId is required and must be a lowercase athlete identifier.');
27
+ }
28
+ if (request.provider !== undefined && !DETACHED_SYNC_PROVIDERS.includes(request.provider)) {
29
+ throw new Error('provider must be intervals, garmin, strava, or all.');
30
+ }
31
+ if (request.from !== undefined && !/^\d{4}-\d{2}-\d{2}$/.test(request.from)) {
32
+ throw new Error('from must use YYYY-MM-DD.');
33
+ }
34
+ }
35
+ /**
36
+ * Spawn one detached `catence-data sync` process for an athlete store and
37
+ * return immediately.
38
+ *
39
+ * The child outlives this process (SSH deaths, Console restarts), logs to a
40
+ * file under the catalog home, and is guarded by the same live-progress
41
+ * heartbeat check the progress tools use. The guard and the spawn are not
42
+ * atomic; the single-writer DuckDB lock remains the authoritative guard, so a
43
+ * racing second child fails cleanly in its own log file.
44
+ */
45
+ export async function startDetachedSync(request, dependencies = {}) {
46
+ assertValidRequest(request);
47
+ const provider = request.provider ?? 'all';
48
+ const snapshot = await syncProgress(request.paths);
49
+ const activeRun = snapshot.running[0];
50
+ if (activeRun)
51
+ throw new DetachedSyncBusyError(activeRun.runId);
52
+ const stamp = (dependencies.now ?? (() => new Date()))().toISOString().replace(/[:.]/g, '-');
53
+ const logFile = path.join(request.paths.root, 'logs', `sync-${request.athleteId}-${stamp}.log`);
54
+ await mkdir(path.dirname(logFile), { recursive: true });
55
+ await writeFile(logFile, `[${new Date().toISOString()}] catence-data --athlete ${request.athleteId} sync --provider ${provider}${request.from ? ` --from ${request.from}` : ''}${request.refresh ? ' --refresh' : ''}\n`, 'utf8');
56
+ const arguments_ = [cliScriptPath(), '--home', request.paths.root, '--athlete', request.athleteId, 'sync', '--provider', provider];
57
+ if (request.from)
58
+ arguments_.push('--from', request.from);
59
+ if (request.refresh)
60
+ arguments_.push('--refresh');
61
+ // The file handle stays open only for the spawn; the detached child holds
62
+ // its own duplicate of the descriptor afterwards.
63
+ const logHandle = await open(logFile, 'a');
64
+ try {
65
+ const spawnProcess = dependencies.spawnProcess ?? spawn;
66
+ const child = spawnProcess(process.execPath, arguments_, {
67
+ detached: true,
68
+ stdio: ['ignore', logHandle.fd, logHandle.fd],
69
+ env: process.env,
70
+ });
71
+ child.unref();
72
+ }
73
+ finally {
74
+ await logHandle.close();
75
+ }
76
+ return { started: true, athleteId: request.athleteId, provider, logFile };
77
+ }
78
+ //# sourceMappingURL=detached-sync.js.map
@@ -1,11 +1,17 @@
1
1
  import { type Server } from 'node:http';
2
- import { type CatalogPaths, type CatencePaths } from '../../runtime/index.js';
2
+ import { type DetachedSyncSpawner, mergeOpenCodeGoConsoleProfiles, startDetachedSync, type CatalogPaths, type CatencePaths } from '../../runtime/index.js';
3
3
  export type CatenceHttpServerOptions = {
4
4
  /** A shared catalog for public servers. All data APIs then require athleteId. */
5
5
  catalogPaths?: CatalogPaths;
6
6
  /** Internal compatibility hook for tests and embedded single-store callers. */
7
7
  paths?: CatencePaths;
8
8
  allowedOrigins?: readonly string[];
9
+ /** Internal compatibility hook for tests: override the detached sync spawner. */
10
+ startSync?: typeof startDetachedSync;
11
+ /** Internal compatibility hook for tests: passed into the default startDetachedSync. */
12
+ syncSpawnProcess?: DetachedSyncSpawner;
13
+ /** Internal compatibility hook for tests: override OpenCode Go model discovery. */
14
+ mergeModels?: typeof mergeOpenCodeGoConsoleProfiles;
9
15
  };
10
16
  /**
11
17
  * Create Catence's local Streamable HTTP server.
@@ -1,6 +1,6 @@
1
1
  import { createServer } from 'node:http';
2
2
  import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
3
- import { catenceRuntimeHealth, DashboardSnapshotService, jsonSafe, loadCatalog, openReadOnlyRepository, resolveAthlete, resolveCatalogPaths } from '../../runtime/index.js';
3
+ import { catenceRuntimeHealth, DashboardSnapshotService, dataStatus, DetachedSyncBusyError, DETACHED_SYNC_PROVIDERS, jsonSafe, loadCatalog, mergeOpenCodeGoConsoleProfiles, openReadOnlyRepository, ReadOnlyDatabaseError, resolveAthlete, resolveCatalogPaths, startDetachedSync, syncProgress, } from '../../runtime/index.js';
4
4
  import { createCatenceMcpServer } from '../mcp/server.js';
5
5
  const MAX_REQUEST_BYTES = 1_000_000;
6
6
  function json(response, statusCode, value) {
@@ -85,6 +85,65 @@ async function dashboardPaths(catalogPaths, staticPaths, url) {
85
85
  return staticPaths;
86
86
  throw new Error('No Catence data store is configured.');
87
87
  }
88
+ function parseSyncBody(body) {
89
+ if (body === undefined)
90
+ return {};
91
+ if (typeof body !== 'object' || body === null || Array.isArray(body))
92
+ throw new Error('Request body must be a JSON object.');
93
+ const raw = body;
94
+ for (const key of Object.keys(raw)) {
95
+ if (!['athleteId', 'provider', 'from', 'refresh', 'refreshModels'].includes(key)) {
96
+ throw new Error(`Unknown sync field: ${key}.`);
97
+ }
98
+ }
99
+ if (raw.athleteId !== undefined && typeof raw.athleteId !== 'string')
100
+ throw new Error('athleteId must be a string.');
101
+ if (raw.provider !== undefined && (typeof raw.provider !== 'string' || !DETACHED_SYNC_PROVIDERS.includes(raw.provider))) {
102
+ throw new Error('provider must be intervals, garmin, strava, or all.');
103
+ }
104
+ if (raw.from !== undefined && (typeof raw.from !== 'string' || !/^\d{4}-\d{2}-\d{2}$/.test(raw.from))) {
105
+ throw new Error('from must use YYYY-MM-DD.');
106
+ }
107
+ for (const flag of ['refresh', 'refreshModels']) {
108
+ if (raw[flag] !== undefined && typeof raw[flag] !== 'boolean')
109
+ throw new Error(`${flag} must be a boolean.`);
110
+ }
111
+ return raw;
112
+ }
113
+ async function resolveSyncPaths(catalogPaths, staticPaths, body) {
114
+ if (catalogPaths) {
115
+ if (!body.athleteId)
116
+ throw new Error('athleteId is required. Call /api/v1/athletes to inspect the configured catalog.');
117
+ return (await resolveAthlete(catalogPaths, body.athleteId)).paths;
118
+ }
119
+ if (staticPaths)
120
+ return staticPaths;
121
+ throw new Error('No Catence data store is configured.');
122
+ }
123
+ /** Best-effort last-success timestamps; null while a run holds the write lock. */
124
+ async function lastSyncSummary(paths) {
125
+ try {
126
+ const status = await dataStatus(paths);
127
+ const runs = Array.isArray(status.syncRuns) ? status.syncRuns : [];
128
+ const completed = runs.filter((run) => typeof run.completed_at === 'string' && String(run.status).startsWith('completed'));
129
+ const providers = {};
130
+ let lastCompletedAt = null;
131
+ for (const run of completed) {
132
+ const completedAt = String(run.completed_at);
133
+ const provider = String(run.provider ?? 'unknown');
134
+ if (!providers[provider] || providers[provider] < completedAt)
135
+ providers[provider] = completedAt;
136
+ if (!lastCompletedAt || lastCompletedAt < completedAt)
137
+ lastCompletedAt = completedAt;
138
+ }
139
+ return { lastCompletedAt, providers, recentRuns: runs.slice(0, 5) };
140
+ }
141
+ catch (error) {
142
+ if (error instanceof ReadOnlyDatabaseError)
143
+ return null;
144
+ throw error;
145
+ }
146
+ }
88
147
  async function handleMcpRequest(request, response, paths) {
89
148
  if (request.method !== 'POST') {
90
149
  methodNotAllowed(response);
@@ -143,6 +202,8 @@ export function createCatenceHttpServer(options = {}) {
143
202
  const catalogPaths = options.catalogPaths ?? (options.paths ? null : resolveCatalogPaths());
144
203
  const staticPaths = options.paths ?? null;
145
204
  const allowedOrigins = options.allowedOrigins ?? [];
205
+ const startSync = options.startSync ?? startDetachedSync;
206
+ const mergeModels = options.mergeModels ?? mergeOpenCodeGoConsoleProfiles;
146
207
  return createServer((request, response) => {
147
208
  void (async () => {
148
209
  if (!addCorsHeaders(request, response, allowedOrigins)) {
@@ -182,6 +243,48 @@ export function createCatenceHttpServer(options = {}) {
182
243
  }
183
244
  return;
184
245
  }
246
+ if (pathname === '/api/v1/sync' && request.method === 'POST') {
247
+ let body;
248
+ try {
249
+ body = parseSyncBody(await readJsonBody(request));
250
+ const paths = await resolveSyncPaths(catalogPaths, staticPaths, body);
251
+ // Optional model self-discovery runs before the data sync so new
252
+ // OpenCode Go models appear without a manual script run. Failure is
253
+ // non-fatal: it must never block or fail a data sync.
254
+ let discoveryWarning = null;
255
+ if (body.refreshModels) {
256
+ try {
257
+ await mergeModels({ configPath: paths.config, setDefault: false });
258
+ }
259
+ catch (error) {
260
+ discoveryWarning = `OpenCode Go model discovery failed; profiles were left unchanged (${error instanceof Error ? error.message : String(error)}).`;
261
+ }
262
+ }
263
+ const handle = await startSync({ paths, athleteId: body.athleteId ?? 'local', provider: body.provider, from: body.from, refresh: body.refresh }, { spawnProcess: options.syncSpawnProcess });
264
+ json(response, 202, { ...handle, ...(discoveryWarning ? { warning: discoveryWarning } : {}), progressEndpoint: '/api/v1/sync/status' });
265
+ }
266
+ catch (error) {
267
+ json(response, error instanceof DetachedSyncBusyError ? 409 : 400, {
268
+ error: {
269
+ code: error instanceof DetachedSyncBusyError ? 'sync_in_progress' : 'sync_start_failed',
270
+ message: error instanceof Error ? error.message : String(error),
271
+ },
272
+ });
273
+ }
274
+ return;
275
+ }
276
+ if (pathname === '/api/v1/sync/status' && request.method === 'GET') {
277
+ try {
278
+ const url = new URL(request.url ?? '/', 'http://localhost');
279
+ const paths = await dashboardPaths(catalogPaths, staticPaths, url);
280
+ const athleteId = catalogPaths ? (await resolveAthlete(catalogPaths, url.searchParams.get('athleteId') ?? '')).athlete.id : 'local';
281
+ json(response, 200, { athleteId, progress: await syncProgress(paths), lastSync: await lastSyncSummary(paths) });
282
+ }
283
+ catch (error) {
284
+ json(response, 400, { error: { code: 'sync_status_unavailable', message: error instanceof Error ? error.message : String(error) } });
285
+ }
286
+ return;
287
+ }
185
288
  if (pathname === '/mcp') {
186
289
  await handleMcpRequest(request, response, catalogPaths ?? staticPaths ?? resolveCatalogPaths());
187
290
  return;
@@ -1,8 +1,9 @@
1
1
  import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
2
- import { type CatalogPaths, hydrateStravaActivity, type CatencePaths } from '../../runtime/index.js';
2
+ import { type CatalogPaths, hydrateStravaActivity, startDetachedSync, type CatencePaths } from '../../runtime/index.js';
3
3
  type StravaActivityHydrator = typeof hydrateStravaActivity;
4
4
  type McpDependencies = {
5
5
  hydrateStravaActivity?: StravaActivityHydrator;
6
+ startDetachedSync?: typeof startDetachedSync;
6
7
  };
7
8
  export declare function createCatenceMcpServer(paths?: CatencePaths | CatalogPaths, dependencies?: McpDependencies): McpServer;
8
9
  export {};
@@ -1,7 +1,7 @@
1
1
  import { McpServer, ResourceTemplate } from '@modelcontextprotocol/sdk/server/mcp.js';
2
2
  import { AsyncLocalStorage } from 'node:async_hooks';
3
3
  import { z } from 'zod';
4
- import { ActivityDiscoveryService, AnalyticsService, loadCatalog, configuredMcpRateLimit, DataWriteBusyError, DATASET_CATALOG, demoStoreMetadata, FitnessService, getDataset, hydrateStravaActivity, hydrateStravaSegmentHistory, jsonSafe, loadCatenceConfig, openReadOnlyRepository, queryReadOnlyData, QueryValidationError, ReadOnlyDatabaseError, resolveAthlete, resolveCatalogPaths, searchContext, SlidingWindowLimiter, StravaEnrichmentError, StravaRateLimitError, SwimmingService, WELLNESS_METRICS, WellnessService, } from '../../runtime/index.js';
4
+ import { ActivityDiscoveryService, AnalyticsService, loadCatalog, configuredMcpRateLimit, DataWriteBusyError, DATASET_CATALOG, demoStoreMetadata, DetachedSyncBusyError, FitnessService, getDataset, hydrateStravaActivity, hydrateStravaSegmentHistory, jsonSafe, loadCatenceConfig, openReadOnlyRepository, queryReadOnlyData, QueryValidationError, ReadOnlyDatabaseError, resolveAthlete, resolveCatalogPaths, searchContext, SlidingWindowLimiter, startDetachedSync, StravaEnrichmentError, StravaRateLimitError, SwimmingService, WELLNESS_METRICS, WellnessService, } from '../../runtime/index.js';
5
5
  const filterSchema = z.object({
6
6
  column: z.string().min(1),
7
7
  op: z.enum(['eq', 'neq', 'gt', 'gte', 'lt', 'lte', 'in', 'between', 'contains']),
@@ -32,17 +32,19 @@ function textResult(value) {
32
32
  return { content: [{ type: 'text', text: JSON.stringify(jsonSafe(value), null, 2) }] };
33
33
  }
34
34
  function errorResult(error) {
35
- const classified = error instanceof DataWriteBusyError
36
- ? { code: 'data_sync_in_progress', message: error.message, retryable: true }
37
- : error instanceof StravaRateLimitError
38
- ? { code: 'rate_limited', message: error.message, retryable: true, retryAfterSeconds: error.retryAfterSeconds ?? null }
39
- : error instanceof StravaEnrichmentError
40
- ? { code: 'strava_connection_error', message: error.message }
41
- : error instanceof ReadOnlyDatabaseError
42
- ? { code: error.code, message: error.message }
43
- : error instanceof QueryValidationError
44
- ? { code: 'invalid_request', message: error.message }
45
- : { code: 'data_unavailable', message: error instanceof Error ? error.message : String(error) };
35
+ const classified = error instanceof DetachedSyncBusyError
36
+ ? { code: 'sync_in_progress', message: error.message, retryable: true, runId: error.runId }
37
+ : error instanceof DataWriteBusyError
38
+ ? { code: 'data_sync_in_progress', message: error.message, retryable: true }
39
+ : error instanceof StravaRateLimitError
40
+ ? { code: 'rate_limited', message: error.message, retryable: true, retryAfterSeconds: error.retryAfterSeconds ?? null }
41
+ : error instanceof StravaEnrichmentError
42
+ ? { code: 'strava_connection_error', message: error.message }
43
+ : error instanceof ReadOnlyDatabaseError
44
+ ? { code: error.code, message: error.message }
45
+ : error instanceof QueryValidationError
46
+ ? { code: 'invalid_request', message: error.message }
47
+ : { code: 'data_unavailable', message: error instanceof Error ? error.message : String(error) };
46
48
  return { ...textResult({ data: null, provenance: {}, query: {}, caveats: [classified.message], error: classified }), isError: true };
47
49
  }
48
50
  function hydrationFailure(error) {
@@ -123,6 +125,7 @@ async function activitySegments(repository, activityId, hydratedStravaActivityId
123
125
  export function createCatenceMcpServer(paths = resolveCatalogPaths(), dependencies = {}) {
124
126
  const server = new McpServer({ name: 'catence', version: '0.1.0' }, { instructions: MCP_INSTRUCTIONS });
125
127
  const hydrateActivity = dependencies.hydrateStravaActivity ?? hydrateStravaActivity;
128
+ const startSync = dependencies.startDetachedSync ?? startDetachedSync;
126
129
  const catalogPaths = isCatalogPaths(paths) ? paths : null;
127
130
  const staticPaths = isCatalogPaths(paths) ? null : paths;
128
131
  const scope = new AsyncLocalStorage();
@@ -176,7 +179,12 @@ export function createCatenceMcpServer(paths = resolveCatalogPaths(), dependenci
176
179
  ...(demoStore ? { demoStore: { generated: true, seed: demoStore.seed, days: demoStore.days, startDate: demoStore.startDate, endDate: demoStore.endDate }, caveats: [...caveats, 'This response uses Catence generated demo data, not personal measurements.'] } : {}),
177
180
  });
178
181
  };
179
- return selected ? scope.run(selected, run) : run();
182
+ // `return await` matters here: a bare `return run()` would let a
183
+ // rejected promise escape the try block, bypassing the classified
184
+ // errorResult payload entirely.
185
+ if (selected)
186
+ return await scope.run(selected, run);
187
+ return await run();
180
188
  }
181
189
  catch (error) {
182
190
  return errorResult(error);
@@ -238,6 +246,28 @@ export function createCatenceMcpServer(paths = resolveCatalogPaths(), dependenci
238
246
  data: await repository.progress(),
239
247
  provenance: { database: 'read-only DuckDB snapshot' }, query: {}, caveats: [],
240
248
  }))));
249
+ server.registerTool('start_detached_sync', {
250
+ title: 'Start a detached data sync',
251
+ description: 'Write-only sync trigger, named like the other explicit lock-guarded write tools. Spawns one detached catence-data sync process for this athlete and returns immediately with the run handle and log file; it refuses while another sync run is active. Track the run with catence_sync_progress; this tool never waits for completion.',
252
+ inputSchema: {
253
+ provider: z.enum(['intervals', 'garmin', 'strava', 'all']).optional(),
254
+ from: z.string().date().optional(),
255
+ refresh: z.boolean().optional(),
256
+ },
257
+ }, tool('start_detached_sync', async (input) => {
258
+ const paths = activePaths();
259
+ const athleteId = activeAthlete()?.id ?? 'local';
260
+ const handle = await startSync({ paths, athleteId, provider: input.provider, from: input.from, refresh: input.refresh });
261
+ return {
262
+ data: { ...handle, progressTool: 'catence_sync_progress' },
263
+ provenance: { operation: 'detached_sync_spawn' },
264
+ query: { ...input },
265
+ caveats: [
266
+ 'The sync runs in its own process and keeps running if this session ends; its log file records the full worker output.',
267
+ 'Only one sync run may be active per athlete store.',
268
+ ],
269
+ };
270
+ }));
241
271
  server.registerTool('review_daily_recovery_load', {
242
272
  title: 'Review daily recovery and training load',
243
273
  description: 'Return source-cited daily health, training, and nutrition facts for a recovery/load review. It does not prescribe a training plan.',
@@ -30,6 +30,10 @@ export { queryReadOnlyData } from '../core/query/sql-guard.js';
30
30
  export { createDemoStore, demoStoreMetadata } from '../elt/application/demo.js';
31
31
  export { connectStrava, connectStravaWithCallback, dataStatus, disconnectStravaAccount, initializeDataStore, importStagedFile, linkActivity, rebuildRetrievalIndex, reimportNutrition, resolveExtractionErrors, retryDataSync, syncData, syncProgress, unlinkActivity, } from '../elt/application/management.js';
32
32
  export type { ProviderChoice } from '../elt/application/management.js';
33
+ export { DETACHED_SYNC_PROVIDERS, DetachedSyncBusyError, startDetachedSync, } from '../elt/application/detached-sync.js';
34
+ export type { DetachedSyncHandle, DetachedSyncProvider, DetachedSyncRequest, DetachedSyncSpawner } from '../elt/application/detached-sync.js';
35
+ export { buildOpenCodeGoConsoleProfiles, classifyOpenCodeGoModel, fetchOpenCodeGoModelIds, mergeOpenCodeGoConsoleProfiles, OPENCODE_GO_API_BASE_ENV, OPENCODE_GO_API_KEY_ENV, OPENCODE_GO_DEFAULT_BASE_URL, OPENCODE_GO_MESSAGES_API_BASE_ENV, } from '../core/runtime/opencode-go.js';
36
+ export type { OpenCodeGoConsoleProfiles, OpenCodeGoMergeResult, OpenCodeGoRoute } from '../core/runtime/opencode-go.js';
33
37
  export type { InterruptedRuns, SyncProgressSnapshot, SyncProgressState } from '../contracts/progress.js';
34
38
  export { openReadOnlyRepository, ReadOnlyDatabaseError } from '../elt/storage/database.js';
35
39
  export { DataWriteBusyError } from '../elt/storage/write-lock.js';
@@ -16,6 +16,8 @@ export { jsonSafe, ReadOnlyRepository } from '../core/query/repository.js';
16
16
  export { queryReadOnlyData } from '../core/query/sql-guard.js';
17
17
  export { createDemoStore, demoStoreMetadata } from '../elt/application/demo.js';
18
18
  export { connectStrava, connectStravaWithCallback, dataStatus, disconnectStravaAccount, initializeDataStore, importStagedFile, linkActivity, rebuildRetrievalIndex, reimportNutrition, resolveExtractionErrors, retryDataSync, syncData, syncProgress, unlinkActivity, } from '../elt/application/management.js';
19
+ export { DETACHED_SYNC_PROVIDERS, DetachedSyncBusyError, startDetachedSync, } from '../elt/application/detached-sync.js';
20
+ export { buildOpenCodeGoConsoleProfiles, classifyOpenCodeGoModel, fetchOpenCodeGoModelIds, mergeOpenCodeGoConsoleProfiles, OPENCODE_GO_API_BASE_ENV, OPENCODE_GO_API_KEY_ENV, OPENCODE_GO_DEFAULT_BASE_URL, OPENCODE_GO_MESSAGES_API_BASE_ENV, } from '../core/runtime/opencode-go.js';
19
21
  export { openReadOnlyRepository, ReadOnlyDatabaseError } from '../elt/storage/database.js';
20
22
  export { DataWriteBusyError } from '../elt/storage/write-lock.js';
21
23
  export { hydrateStravaActivity, hydrateStravaSegmentHistory, StravaEnrichmentError, StravaRateLimitError, } from '../elt/ingestion/providers/strava/service.js';
@@ -160,6 +160,12 @@ Precedence for reasoning effort: per-chat dropdown selection > model
160
160
  `reasoningEffort` > profile `defaultReasoningEffort` > provider default. See
161
161
  [`llm-providers.md`](llm-providers.md#reasoning-effort) for the full behavior.
162
162
 
163
+ The Console's **Models** page edits this section in place (enable/disable,
164
+ add custom variants, remove, set defaults). Disabled choices are stored in the
165
+ Console database rather than here; every write preserves all non-console
166
+ sections and is re-validated with the same strict parser before the file is
167
+ replaced.
168
+
163
169
  ### The Console wizard
164
170
 
165
171
  On the first authenticated chat, the Console wizard (`write_provider_setup`)
@@ -119,6 +119,30 @@ Preflight with:
119
119
  catence-console doctor
120
120
  ```
121
121
 
122
+ ### Manage models in the Console
123
+
124
+ The **Models** page (header button) manages the model list without editing
125
+ `config.json` by hand:
126
+
127
+ - Enable/disable toggles hide models from the chat's Model dropdown. Disabled
128
+ choices are stored per machine in the Console database
129
+ (`console/chat-history.sqlite3`), never in `config.json`, and the Console
130
+ refuses to disable the only enabled model.
131
+ - **Add a custom model** appends one deployment to an existing profile — id,
132
+ display label, LiteLLM reference (`openai/…`, `anthropic/…`,
133
+ `openai/responses/…`), and optional fixed reasoning effort or a custom
134
+ variants map.
135
+ - **Remove / Make default** edit `config.json`'s console section directly;
136
+ every other section is preserved, the result is re-validated with the same
137
+ strict parser used at startup before it replaces the file, and secrets stay
138
+ forbidden (only environment-variable names are ever stored).
139
+
140
+ The dashboard header has a **Sync data** button that starts the same detached
141
+ sync as `catence-data sync --provider all` through the authenticated Console
142
+ origin, shows live progress while the run is active, and displays the last
143
+ completed sync afterwards. Each manual sync also refreshes OpenCode Go model
144
+ profiles first; a discovery failure never blocks the data sync.
145
+
122
146
  ## Console serve options
123
147
 
124
148
  `catence-console serve` accepts:
@@ -100,6 +100,11 @@ athletes never leak into each other.
100
100
  - Rate limits are tracked **per athlete**: each athlete has its own sliding
101
101
  window for server, tool, and resource limits.
102
102
  - MCP responses carry an `athlete` field identifying which store served them.
103
+ - `start_detached_sync` is the one explicitly named write tool besides the
104
+ Strava hydration tools: it spawns a detached `catence-data sync` child for
105
+ the selected athlete and returns immediately with the run handle and log
106
+ file. It refuses to start while another sync run is active (`sync_in_progress`);
107
+ follow the run with `catence_sync_progress`.
103
108
 
104
109
  ### Adding another athlete
105
110
 
@@ -298,6 +303,17 @@ override the host and port when the flags are omitted.
298
303
  - `GET /api/v1/dashboard` requires `athleteId`, for example
299
304
  `http://127.0.0.1:8787/api/v1/dashboard?athleteId=alex&days=28` (`endDate`
300
305
  is an optional `YYYY-MM-DD`; `days` ranges 1-90 and defaults to 28).
306
+ - `POST /api/v1/sync` starts a detached data sync for one athlete:
307
+ `{"athleteId": "alex", "provider": "all", "from": "2026-01-01", "refresh": false}`.
308
+ The spawned child outlives the request, logs to `<home>/logs/`, and the call
309
+ returns `202` with the run handle immediately; an already-active run is
310
+ refused with `409 sync_in_progress`. Pass `"refreshModels": true` to also
311
+ refresh OpenCode Go model profiles first (failure is a non-fatal warning in
312
+ the response).
313
+ - `GET /api/v1/sync/status?athleteId=alex` reports live progress heartbeats,
314
+ recent runs, and best-effort last-completion timestamps per provider. It
315
+ keeps working while a sync holds the write lock (`lastSync` becomes `null`
316
+ then).
301
317
  - Browser origins must be listed with `--allow-origin` (repeatable). The
302
318
  packaged Console instead proxies the dashboard through its authenticated
303
319
  same-origin route.
@@ -198,6 +198,12 @@ The script warns on stderr and skips any model that does not fit one of these
198
198
  families. Rerun it to refresh the model lists; existing profiles, limits, and
199
199
  `defaultProfile` are preserved unless `--set-default` is passed.
200
200
 
201
+ You do not have to run the script manually: the Console dashboard's **Sync
202
+ data** button (and `POST /api/v1/sync` with `"refreshModels": true`) refreshes
203
+ these profiles from the live catalog before starting a data sync. Discovery
204
+ failures — offline machine, catalog unreachable — are reported as a warning and
205
+ never block or fail the sync itself.
206
+
201
207
  ```sh
202
208
  export OPENCODE_GO_API_KEY='…' # any non-empty value satisfies the key check
203
209
  export OPENCODE_GO_API_BASE='https://opencode.ai/zen/go/v1'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "catence",
3
- "version": "0.2.0-beta.17",
3
+ "version": "0.2.0-beta.18",
4
4
  "description": "Local MCP access to normalized Garmin, Intervals.icu, and targeted Strava fitness data.",
5
5
  "license": "AGPL-3.0-or-later",
6
6
  "repository": {
@@ -1,12 +1,11 @@
1
+ #!/usr/bin/env node
1
2
  // Discover OpenCode Go models and emit Catence Console profiles for them.
2
3
  //
3
- // OpenCode Go serves its catalog through three APIs: most models speak the
4
- // OpenAI Chat Completions API, Grok and GPT-5.6 Luna speak the OpenAI
5
- // Responses API, and MiniMax and Qwen speak the Anthropic Messages API. The
4
+ // Thin CLI wrapper around the runtime's src/core/runtime/opencode-go.ts
5
+ // module, which owns the "live discovery + trusted metadata" logic: the
6
6
  // public `GET /models` endpoint returns only model ids, not which API each
7
- // model needs, so routing comes from the trusted table below and is intersected
8
- // with the live list (the same "live discovery + trusted metadata" approach
9
- // OpenClaw uses).
7
+ // model needs, so routing comes from a trusted table intersected with the
8
+ // live list (the same approach OpenClaw uses).
10
9
  //
11
10
  // Usage:
12
11
  // node scripts/discover-opencode-go.mjs [--base-url <url>] [--write <config.json>] [--set-default]
@@ -17,62 +16,19 @@
17
16
  // --set-default With --write, set console.defaultProfile to opencode-go even when one already exists.
18
17
 
19
18
  import process from 'node:process';
20
- import { readFile, writeFile } from 'node:fs/promises';
21
19
 
22
- const DEFAULT_BASE_URL = 'https://opencode.ai/zen/go/v1';
23
- const API_KEY_ENV = 'OPENCODE_GO_API_KEY';
24
- const API_BASE_ENV = 'OPENCODE_GO_API_BASE';
25
- const MESSAGES_BASE_ENV = 'OPENCODE_GO_MESSAGES_API_BASE';
26
-
27
- // LiteLLM appends a fixed suffix to the profile base URL per provider, so the
28
- // three APIs need different prefixes and two base URLs:
29
- // openai/<id> -> {base}/chat/completions
30
- // openai/responses/<id> -> {base}/responses
31
- // anthropic/<id> -> {base minus /v1}/v1/messages
32
- const RESPONSES_MODELS = new Set(['grok-4.5', 'gpt-5.6-luna']);
33
- const MESSAGES_MODELS = new Set([
34
- 'minimax-m2.5',
35
- 'minimax-m2.7',
36
- 'minimax-m3',
37
- 'qwen3.5-plus',
38
- 'qwen3.6-plus',
39
- 'qwen3.7-plus',
40
- 'qwen3.7-max',
41
- 'qwen3.8-max',
42
- ]);
43
-
44
- const BRAND_NAMES = new Map([
45
- ['deepseek', 'DeepSeek'],
46
- ['glm', 'GLM'],
47
- ['gpt', 'GPT'],
48
- ['grok', 'Grok'],
49
- ['hy3', 'Hy3'],
50
- ['kimi', 'Kimi'],
51
- ['mimo', 'MiMo'],
52
- ['minimax', 'MiniMax'],
53
- ['qwen', 'Qwen'],
54
- ]);
55
-
56
- function classify(id) {
57
- if (RESPONSES_MODELS.has(id)) return { route: 'responses', guessed: false };
58
- if (MESSAGES_MODELS.has(id)) return { route: 'messages', guessed: false };
59
- if (/^(grok-|gpt-)/.test(id)) return { route: 'responses', guessed: true };
60
- if (/^(minimax-|qwen)/.test(id)) return { route: 'messages', guessed: true };
61
- return { route: 'chat', guessed: false };
20
+ let runtime;
21
+ try {
22
+ runtime = await import(new URL('../dist/core/runtime/opencode-go.js', import.meta.url).href);
23
+ } catch (error) {
24
+ if (error?.code !== 'ERR_MODULE_NOT_FOUND') throw error;
25
+ process.stderr.write(
26
+ 'The discovery module is not built yet. Run `npm run build` in the catence checkout first, or use an installed `catence` package.\n',
27
+ );
28
+ process.exit(1);
62
29
  }
63
30
 
64
- function modelReference(id, route) {
65
- if (route === 'responses') return `openai/responses/${id}`;
66
- if (route === 'messages') return `anthropic/${id}`;
67
- return `openai/${id}`;
68
- }
69
-
70
- function labelFor(id) {
71
- return id
72
- .split('-')
73
- .map((segment) => BRAND_NAMES.get(segment) ?? segment.charAt(0).toUpperCase() + segment.slice(1))
74
- .join(' ');
75
- }
31
+ const { OPENCODE_GO_API_BASE_ENV, OPENCODE_GO_API_KEY_ENV, OPENCODE_GO_MESSAGES_API_BASE_ENV } = runtime;
76
32
 
77
33
  function usage() {
78
34
  process.stderr.write(
@@ -82,7 +38,7 @@ function usage() {
82
38
 
83
39
  async function main() {
84
40
  const args = process.argv.slice(2);
85
- let baseUrl = DEFAULT_BASE_URL;
41
+ let baseUrl = runtime.OPENCODE_GO_DEFAULT_BASE_URL;
86
42
  let writePath = null;
87
43
  let setDefault = false;
88
44
  for (let i = 0; i < args.length; i += 1) {
@@ -108,100 +64,36 @@ async function main() {
108
64
  if (!baseUrl.endsWith('/v1')) {
109
65
  process.stderr.write(`Warning: --base-url ${baseUrl} does not end in /v1; chat and responses URLs may be wrong.\n`);
110
66
  }
111
- const modelsUrl = `${baseUrl.replace(/\/$/, '')}/models`;
112
67
  const messagesBase = baseUrl.replace(/\/v1\/?$/, '');
113
68
 
114
- let response;
115
- try {
116
- response = await fetch(modelsUrl, { headers: { accept: 'application/json' } });
117
- } catch (error) {
118
- throw new Error(`Could not reach ${modelsUrl}: ${error.message}`);
119
- }
120
- if (!response.ok) {
121
- throw new Error(`GET ${modelsUrl} returned HTTP ${response.status}.`);
122
- }
123
- const payload = await response.json();
124
- const ids = (payload.data ?? []).map((entry) => entry.id).filter((id) => typeof id === 'string' && id);
125
-
126
- const groups = { chat: [], responses: [], messages: [] };
127
- const guessed = [];
128
- for (const id of ids) {
129
- const { route, guessed: wasGuessed } = classify(id);
130
- if (!(route in groups)) continue;
131
- groups[route].push(id);
132
- if (wasGuessed) guessed.push(`${id} -> ${route}`);
133
- }
134
- for (const route of Object.keys(groups)) groups[route].sort();
135
-
136
- const chatProfileModels = {};
137
- for (const id of groups.chat) {
138
- chatProfileModels[id] = { label: labelFor(id), model: modelReference(id, 'chat') };
139
- }
140
- for (const id of groups.responses) {
141
- chatProfileModels[id] = { label: labelFor(id), model: modelReference(id, 'responses') };
142
- }
143
- const messagesProfileModels = {};
144
- for (const id of groups.messages) {
145
- messagesProfileModels[id] = { label: labelFor(id), model: modelReference(id, 'messages') };
146
- }
147
-
148
- const chatDefault = groups.chat[0] ?? groups.responses[0];
149
- const profiles = {};
150
- profiles['opencode-go'] = {
151
- label: 'OpenCode Go',
152
- ...(chatDefault ? { defaultModel: chatDefault } : {}),
153
- models: chatProfileModels,
154
- apiKeyEnv: API_KEY_ENV,
155
- apiBaseEnv: API_BASE_ENV,
156
- };
157
- if (messagesProfileModels && groups.messages.length) {
158
- profiles['opencode-go-messages'] = {
159
- label: 'OpenCode Go (Anthropic)',
160
- defaultModel: groups.messages[0],
161
- models: messagesProfileModels,
162
- apiKeyEnv: API_KEY_ENV,
163
- apiBaseEnv: MESSAGES_BASE_ENV,
164
- };
165
- }
166
-
167
- const consoleSection = { defaultProfile: 'opencode-go', profiles };
168
-
169
- if (guessed.length) {
170
- process.stderr.write('Routed by prefix heuristic (verify before use):\n');
171
- for (const line of guessed) process.stderr.write(` ${line}\n`);
172
- }
173
- process.stderr.write(
174
- `Discovered ${ids.length} models (${groups.chat.length} chat, ${groups.responses.length} responses, ${groups.messages.length} messages).\n`,
175
- );
176
-
177
69
  if (writePath) {
178
- let root = {};
179
- try {
180
- root = JSON.parse(await readFile(writePath, 'utf8'));
181
- } catch (error) {
182
- if (error.code !== 'ENOENT') throw new Error(`Could not read ${writePath}: ${error.message}`);
70
+ const result = await runtime.mergeOpenCodeGoConsoleProfiles({ configPath: writePath, baseUrl, setDefault });
71
+ if (result.guessedRoutes.length) {
72
+ process.stderr.write('Routed by prefix heuristic (verify before use):\n');
73
+ for (const line of result.guessedRoutes) process.stderr.write(` ${line}\n`);
183
74
  }
184
- if (typeof root !== 'object' || root === null || Array.isArray(root)) {
185
- throw new Error(`${writePath} must contain a JSON object.`);
186
- }
187
- const existingConsole = typeof root.console === 'object' && root.console !== null ? root.console : {};
188
- const mergedProfiles = { ...(existingConsole.profiles ?? {}), ...profiles };
189
- const defaultProfile =
190
- existingConsole.defaultProfile && !setDefault ? existingConsole.defaultProfile : 'opencode-go';
191
- root.console = { ...existingConsole, defaultProfile, profiles: mergedProfiles };
192
- await writeFile(writePath, `${JSON.stringify(root, null, 2)}\n`);
193
- process.stderr.write(`Merged profiles into ${writePath} (defaultProfile: ${defaultProfile}).\n`);
75
+ process.stderr.write(
76
+ `Merged ${result.counts.chat} chat, ${result.counts.responses} responses, and ${result.counts.messages} messages models into ${result.configPath}` +
77
+ ` as ${result.mergedProfileIds.join(' + ')} (defaultProfile: ${result.defaultProfile}).\n`,
78
+ );
194
79
  return;
195
80
  }
196
81
 
82
+ const ids = await runtime.fetchOpenCodeGoModelIds(baseUrl);
83
+ const built = runtime.buildOpenCodeGoConsoleProfiles(ids, baseUrl);
84
+ if (built.guessedRoutes.length) {
85
+ process.stderr.write('Routed by prefix heuristic (verify before use):\n');
86
+ for (const line of built.guessedRoutes) process.stderr.write(` ${line}\n`);
87
+ }
197
88
  process.stderr.write(
198
- `Export the key and base URLs, then add the JSON below under "console" in config.json:\n` +
199
- ` export ${API_KEY_ENV}='sk-…'\n` +
200
- ` export ${API_BASE_ENV}='${baseUrl}'\n` +
201
- ` export ${MESSAGES_BASE_ENV}='${messagesBase}'\n` +
89
+ `Discovered ${ids.length} models (${built.counts.chat} chat, ${built.counts.responses} responses, ${built.counts.messages} messages).\n` +
90
+ `Export the key and base URLs, then add the JSON below under "console" in config.json:\n` +
91
+ ` export ${OPENCODE_GO_API_KEY_ENV}='sk-…'\n` +
92
+ ` export ${OPENCODE_GO_API_BASE_ENV}='${baseUrl}'\n` +
93
+ ` export ${OPENCODE_GO_MESSAGES_API_BASE_ENV}='${messagesBase}'\n` +
202
94
  `(or rerun with --write ~/.catence/config.json to merge into an existing config.)\n`,
203
95
  );
204
- process.stdout.write(`${JSON.stringify(consoleSection, null, 2)}\n`);
96
+ process.stdout.write(`${JSON.stringify({ defaultProfile: built.defaultProfile, profiles: built.profiles }, null, 2)}\n`);
205
97
  }
206
98
 
207
99
  await main().catch((error) => {