@sanqianx/claude-ai-workbench 0.13.2 → 0.13.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.
Files changed (120) hide show
  1. package/LICENSE +201 -201
  2. package/README.md +156 -156
  3. package/apps/agent-terminal/app.css +636 -613
  4. package/apps/agent-terminal/app.js +125 -65
  5. package/apps/agent-terminal/conversation-composer.js +44 -0
  6. package/apps/agent-terminal/index.html +171 -170
  7. package/apps/agent-terminal/panel-host-proof.html +152 -152
  8. package/apps/agent-terminal/panel-proof-content.html +41 -41
  9. package/apps/agent-terminal/right-workspace-dock.css +187 -159
  10. package/apps/agent-terminal/right-workspace-dock.js +623 -519
  11. package/apps/ai-profile-settings-host/index.html +25 -25
  12. package/apps/ai-profile-settings-host/page.css +1 -1
  13. package/apps/ai-profile-settings-host/page.js +113 -113
  14. package/apps/minimal-test-host/README.md +15 -15
  15. package/apps/minimal-test-host/host.css +2 -2
  16. package/apps/minimal-test-host/host.js +35 -35
  17. package/apps/minimal-test-host/index.html +58 -58
  18. package/apps/minimal-test-host/optional-mock/fault-injection.js +15 -15
  19. package/docs/sdk/FILE_LAYOUT.md +31 -31
  20. package/docs/sdk/INTEGRATION.md +126 -126
  21. package/docs/sdk/README.md +21 -21
  22. package/docs/sdk/SESSION_EVENTS.md +13 -13
  23. package/docs/sdk/openapi.yaml +8 -1
  24. package/package.json +1 -1
  25. package/packages/client/README.md +29 -29
  26. package/packages/client/index.d.ts +122 -122
  27. package/packages/client/index.js +260 -260
  28. package/packages/client/package.json +2 -2
  29. package/packages/contracts/README.md +9 -9
  30. package/packages/contracts/index.d.ts +519 -517
  31. package/packages/contracts/index.js +312 -312
  32. package/packages/contracts/package.json +1 -1
  33. package/packages/core/README.md +8 -8
  34. package/packages/core/index.d.ts +76 -76
  35. package/packages/core/index.js +222 -222
  36. package/packages/core/package.json +2 -2
  37. package/packages/server/README.md +17 -17
  38. package/packages/server/bin/agent-terminal-server.js +42 -42
  39. package/packages/server/bin/claude-workbench.js +225 -225
  40. package/packages/server/index.d.ts +171 -171
  41. package/packages/server/index.js +230 -230
  42. package/packages/server/lib/agent-discovery.js +133 -133
  43. package/packages/server/lib/agent-registry.js +143 -143
  44. package/packages/server/lib/agent-spawn-env.js +17 -17
  45. package/packages/server/lib/attachment-store.js +78 -78
  46. package/packages/server/lib/bridges/app-server-bridge.js +373 -373
  47. package/packages/server/lib/bridges/opencode-server-bridge.js +506 -506
  48. package/packages/server/lib/capture-settings-store.js +35 -35
  49. package/packages/server/lib/claude-history-recovery.js +87 -87
  50. package/packages/server/lib/cli-session-recovery.js +28 -28
  51. package/packages/server/lib/context-resolver.js +55 -55
  52. package/packages/server/lib/conversation-capture.js +51 -51
  53. package/packages/server/lib/delegation-phase-store.js +357 -357
  54. package/packages/server/lib/delegation-store.js +474 -468
  55. package/packages/server/lib/diagnostics.js +32 -32
  56. package/packages/server/lib/drivers/cli-json-runner.js +974 -974
  57. package/packages/server/lib/drivers/codex-driver.js +1442 -1442
  58. package/packages/server/lib/drivers/opencode-driver.js +1554 -1554
  59. package/packages/server/lib/drivers/zcode-driver.js +1250 -1250
  60. package/packages/server/lib/drivers/zcode-profile-runtime.js +71 -71
  61. package/packages/server/lib/event-stream.js +138 -138
  62. package/packages/server/lib/folder-picker.js +623 -623
  63. package/packages/server/lib/http-handler.js +339 -339
  64. package/packages/server/lib/json-file.js +26 -26
  65. package/packages/server/lib/knowledge-context.js +33 -33
  66. package/packages/server/lib/model-detection.js +295 -295
  67. package/packages/server/lib/parent-context.js +44 -0
  68. package/packages/server/lib/profile-store.js +283 -283
  69. package/packages/server/lib/project-store.js +102 -102
  70. package/packages/server/lib/runtime-loader.js +120 -120
  71. package/packages/server/lib/session-archive-store.js +53 -53
  72. package/packages/server/lib/session-history.js +612 -612
  73. package/packages/server/lib/system-doctor.js +125 -125
  74. package/packages/server/lib/ui-preferences-store.js +48 -48
  75. package/packages/server/lib/workbench-service.js +1696 -1675
  76. package/packages/server/package.json +2 -2
  77. package/packages/ui/README.md +30 -30
  78. package/packages/ui/ai-profile-settings.d.ts +45 -45
  79. package/packages/ui/ai-profile-settings.js +283 -283
  80. package/packages/ui/claude-terminal.d.ts +31 -31
  81. package/packages/ui/claude-terminal.js +343 -343
  82. package/packages/ui/index.d.ts +18 -18
  83. package/packages/ui/index.js +51 -51
  84. package/packages/ui/package.json +2 -2
  85. package/packages/ui/theme.css +13 -13
  86. package/src/assets/claude-prompts.json +21 -21
  87. package/src/backend/http/ai-profile-routes.js +61 -61
  88. package/src/backend/http/claude-routes.js +170 -170
  89. package/src/backend/http/node-helpers.js +45 -45
  90. package/src/backend/index.js +21 -21
  91. package/src/backend/lib/ai-adapter.js +144 -144
  92. package/src/backend/lib/ai-profile-store.js +259 -259
  93. package/src/backend/lib/ai-vendor-presets.js +83 -83
  94. package/src/backend/lib/ai-workspace.js +140 -140
  95. package/src/backend/lib/automation-config.js +253 -253
  96. package/src/backend/lib/claude-cli-runner.js +2392 -2392
  97. package/src/backend/lib/data-dir.js +169 -169
  98. package/src/backend/lib/llm-client.js +176 -176
  99. package/src/backend/lib/model-context-windows.js +82 -82
  100. package/src/backend/lib/native-task-control.js +217 -217
  101. package/src/backend/lib/native-task-history-index.js +475 -475
  102. package/src/backend/lib/native-task-history-reader.js +526 -526
  103. package/src/backend/lib/native-task-messages.js +457 -457
  104. package/src/backend/lib/native-task-registry.js +325 -325
  105. package/src/backend/lib/native-task-runtime.js +242 -242
  106. package/src/backend/lib/native-task-trace.js +440 -440
  107. package/src/backend/lib/native-task-transcript.js +393 -393
  108. package/src/backend/lib/prompt-registry.js +166 -166
  109. package/src/backend/lib/runtime-config.js +121 -121
  110. package/src/backend/lib/sdk-session-stream.js +143 -143
  111. package/src/backend/lib/sdk-turn-stream.js +16 -16
  112. package/src/backend/lib/store-scope.js +131 -131
  113. package/src/backend/lib/task-identity.js +104 -104
  114. package/src/backend/lib/task-state-projection.js +58 -58
  115. package/src/index.js +7 -7
  116. package/src/sdk-wave-a.ts +1 -1
  117. package/src/sdk-wave-b.ts +1 -1
  118. package/src/sdk-wave-c.ts +1 -1
  119. package/src/sdk-wave-d.ts +1 -1
  120. package/src/sdk-wave-e.ts +1 -1
@@ -1,176 +1,176 @@
1
- // _site/lib/llm-client.js
2
- //
3
- // Tiny Anthropic Messages API client. Zero npm deps; supports HTTPS and HTTP
4
- // Anthropic-compatible endpoints. Connection settings are resolved from an AI
5
- // profile first, then environment variables as fallback:
6
- //
7
- // profile.baseUrl or ANTHROPIC_BASE_URL
8
- // profile.apiKey or ANTHROPIC_AUTH_TOKEN
9
- // profile.mainModel or ANTHROPIC_MODEL
10
- // profile.timeoutMs or API_TIMEOUT_MS
11
- //
12
- // This lets the local UI persist model settings in ai-profiles.json while still
13
- // keeping env vars useful for scripts and temporary overrides.
14
-
15
- const fs = require('fs');
16
- const path = require('path');
17
- const http = require('http');
18
- const https = require('https');
19
- const { URL } = require('url');
20
-
21
- const DEFAULT_BASE = 'https://api.anthropic.com';
22
- const DEFAULT_VERSION = '2023-06-01';
23
- const DEFAULT_MODEL = 'claude-haiku-4-5';
24
- const { getDataDir } = require('./data-dir');
25
-
26
- function aiProfilesPath() {
27
- return path.join(getDataDir(), 'ai-profiles.json');
28
- }
29
-
30
- function readProfilesFile() {
31
- try {
32
- const cfg = JSON.parse(fs.readFileSync(aiProfilesPath(), 'utf-8'));
33
- return Array.isArray(cfg.profiles) ? cfg.profiles : [];
34
- } catch {
35
- return [];
36
- }
37
- }
38
-
39
- function findProfile(profileId) {
40
- if (!profileId) return null;
41
- return readProfilesFile().find(profile => profile && profile.id === profileId) || null;
42
- }
43
-
44
- function readConfig(options = {}) {
45
- const profileId = typeof options === 'string' ? options : options.profileId;
46
- const inlineProfile = options && typeof options === 'object' && options.profile && typeof options.profile === 'object'
47
- ? options.profile
48
- : null;
49
- const profile = inlineProfile || findProfile(profileId) || {};
50
- const timeoutMs = Number(profile.timeoutMs || process.env.API_TIMEOUT_MS || 60_000);
51
- return {
52
- profileId: profile.id || profileId || null,
53
- baseUrl: profile.baseUrl || profile.apiBaseUrl || profile.anthropicBaseUrl || process.env.ANTHROPIC_BASE_URL || DEFAULT_BASE,
54
- apiKey: profile.apiKey || profile.authToken || profile.anthropicAuthToken || process.env.ANTHROPIC_AUTH_TOKEN || '',
55
- model: profile.mainModel || profile.model || process.env.ANTHROPIC_MODEL || DEFAULT_MODEL,
56
- version: DEFAULT_VERSION,
57
- timeoutMs: Number.isFinite(timeoutMs) && timeoutMs > 0 ? timeoutMs : 60_000,
58
- };
59
- }
60
-
61
- function buildRequestPath(base, p) {
62
- if (!p.startsWith('/')) return p;
63
- const basePath = base.pathname.endsWith('/') ? base.pathname.slice(0, -1) : base.pathname;
64
- return basePath + p;
65
- }
66
-
67
- function postJson({ baseUrl, path: p, body, apiKey, version, timeoutMs = 60_000 }) {
68
- return new Promise((resolve, reject) => {
69
- try {
70
- const base = new URL(baseUrl);
71
- const transport = base.protocol === 'http:' ? http : https;
72
- const data = JSON.stringify(body);
73
- const req = transport.request({
74
- host: base.hostname,
75
- port: base.port || (base.protocol === 'http:' ? 80 : 443),
76
- path: buildRequestPath(base, p),
77
- method: 'POST',
78
- headers: {
79
- 'Content-Type': 'application/json',
80
- 'x-api-key': apiKey,
81
- 'anthropic-version': version,
82
- 'Content-Length': Buffer.byteLength(data),
83
- },
84
- timeout: timeoutMs,
85
- }, res => {
86
- const chunks = [];
87
- res.on('data', c => chunks.push(c));
88
- res.on('end', () => {
89
- const text = Buffer.concat(chunks).toString('utf-8');
90
- if (res.statusCode < 200 || res.statusCode >= 300) {
91
- return reject(new Error(`HTTP ${res.statusCode}: ${text.slice(0, 500)}`));
92
- }
93
- try { resolve(JSON.parse(text)); }
94
- catch (e) { reject(new Error(`bad JSON: ${e.message}; body head: ${text.slice(0, 200)}`)); }
95
- });
96
- });
97
- req.on('error', reject);
98
- req.on('timeout', () => req.destroy(new Error(`timeout after ${timeoutMs}ms`)));
99
- req.write(data);
100
- req.end();
101
- } catch (e) {
102
- reject(new Error(`bad baseUrl: ${baseUrl}: ${e.message}`));
103
- }
104
- });
105
- }
106
-
107
- async function completeText({ system, user, maxTokens = 512, model, profileId, profile }) {
108
- const cfg = readConfig({ profileId, profile });
109
- if (!cfg.apiKey) {
110
- const id = profileId ? ` for profile ${profileId}` : '';
111
- throw new Error(`API key not set${id}`);
112
- }
113
- const body = {
114
- model: model || cfg.model,
115
- max_tokens: maxTokens,
116
- messages: [{ role: 'user', content: user }],
117
- };
118
- if (system) body.system = system;
119
-
120
- const raw = await postJson({
121
- baseUrl: cfg.baseUrl,
122
- path: '/v1/messages',
123
- body,
124
- apiKey: cfg.apiKey,
125
- version: cfg.version,
126
- timeoutMs: cfg.timeoutMs,
127
- });
128
-
129
- const text = (raw.content || [])
130
- .filter(block => block && block.type === 'text' && typeof block.text === 'string')
131
- .map(block => block.text)
132
- .join('\n')
133
- .trim();
134
-
135
- return {
136
- text,
137
- raw,
138
- usage: raw.usage || null,
139
- model: raw.model || body.model,
140
- profileId: cfg.profileId,
141
- };
142
- }
143
-
144
- async function completeJson({ system, user, schema, maxTokens = 2048, model, profileId, profile }) {
145
- const schemaHint = schema ? `\n\nThe output MUST be a JSON object matching this schema:\n${schema}\n` : '';
146
- const finalUser = `${user}${schemaHint}\n\nRespond with ONLY the JSON object, no prose, no markdown fences.`;
147
- const result = await completeText({ system, user: finalUser, maxTokens, model, profileId, profile });
148
- const text = result.text;
149
-
150
- let parsed = null;
151
- let parseError = null;
152
- const candidates = [
153
- text,
154
- text.replace(/^```(?:json)?\s*/i, '').replace(/```\s*$/, ''),
155
- ];
156
- for (const candidate of candidates) {
157
- if (!candidate) continue;
158
- try {
159
- parsed = JSON.parse(candidate);
160
- parseError = null;
161
- break;
162
- } catch (e) {
163
- parseError = e;
164
- }
165
- }
166
-
167
- return { ...result, parsed, parseError };
168
- }
169
-
170
- module.exports = {
171
- completeJson,
172
- completeText,
173
- readConfig,
174
- findProfile,
175
- aiProfilesPath,
176
- };
1
+ // _site/lib/llm-client.js
2
+ //
3
+ // Tiny Anthropic Messages API client. Zero npm deps; supports HTTPS and HTTP
4
+ // Anthropic-compatible endpoints. Connection settings are resolved from an AI
5
+ // profile first, then environment variables as fallback:
6
+ //
7
+ // profile.baseUrl or ANTHROPIC_BASE_URL
8
+ // profile.apiKey or ANTHROPIC_AUTH_TOKEN
9
+ // profile.mainModel or ANTHROPIC_MODEL
10
+ // profile.timeoutMs or API_TIMEOUT_MS
11
+ //
12
+ // This lets the local UI persist model settings in ai-profiles.json while still
13
+ // keeping env vars useful for scripts and temporary overrides.
14
+
15
+ const fs = require('fs');
16
+ const path = require('path');
17
+ const http = require('http');
18
+ const https = require('https');
19
+ const { URL } = require('url');
20
+
21
+ const DEFAULT_BASE = 'https://api.anthropic.com';
22
+ const DEFAULT_VERSION = '2023-06-01';
23
+ const DEFAULT_MODEL = 'claude-haiku-4-5';
24
+ const { getDataDir } = require('./data-dir');
25
+
26
+ function aiProfilesPath() {
27
+ return path.join(getDataDir(), 'ai-profiles.json');
28
+ }
29
+
30
+ function readProfilesFile() {
31
+ try {
32
+ const cfg = JSON.parse(fs.readFileSync(aiProfilesPath(), 'utf-8'));
33
+ return Array.isArray(cfg.profiles) ? cfg.profiles : [];
34
+ } catch {
35
+ return [];
36
+ }
37
+ }
38
+
39
+ function findProfile(profileId) {
40
+ if (!profileId) return null;
41
+ return readProfilesFile().find(profile => profile && profile.id === profileId) || null;
42
+ }
43
+
44
+ function readConfig(options = {}) {
45
+ const profileId = typeof options === 'string' ? options : options.profileId;
46
+ const inlineProfile = options && typeof options === 'object' && options.profile && typeof options.profile === 'object'
47
+ ? options.profile
48
+ : null;
49
+ const profile = inlineProfile || findProfile(profileId) || {};
50
+ const timeoutMs = Number(profile.timeoutMs || process.env.API_TIMEOUT_MS || 60_000);
51
+ return {
52
+ profileId: profile.id || profileId || null,
53
+ baseUrl: profile.baseUrl || profile.apiBaseUrl || profile.anthropicBaseUrl || process.env.ANTHROPIC_BASE_URL || DEFAULT_BASE,
54
+ apiKey: profile.apiKey || profile.authToken || profile.anthropicAuthToken || process.env.ANTHROPIC_AUTH_TOKEN || '',
55
+ model: profile.mainModel || profile.model || process.env.ANTHROPIC_MODEL || DEFAULT_MODEL,
56
+ version: DEFAULT_VERSION,
57
+ timeoutMs: Number.isFinite(timeoutMs) && timeoutMs > 0 ? timeoutMs : 60_000,
58
+ };
59
+ }
60
+
61
+ function buildRequestPath(base, p) {
62
+ if (!p.startsWith('/')) return p;
63
+ const basePath = base.pathname.endsWith('/') ? base.pathname.slice(0, -1) : base.pathname;
64
+ return basePath + p;
65
+ }
66
+
67
+ function postJson({ baseUrl, path: p, body, apiKey, version, timeoutMs = 60_000 }) {
68
+ return new Promise((resolve, reject) => {
69
+ try {
70
+ const base = new URL(baseUrl);
71
+ const transport = base.protocol === 'http:' ? http : https;
72
+ const data = JSON.stringify(body);
73
+ const req = transport.request({
74
+ host: base.hostname,
75
+ port: base.port || (base.protocol === 'http:' ? 80 : 443),
76
+ path: buildRequestPath(base, p),
77
+ method: 'POST',
78
+ headers: {
79
+ 'Content-Type': 'application/json',
80
+ 'x-api-key': apiKey,
81
+ 'anthropic-version': version,
82
+ 'Content-Length': Buffer.byteLength(data),
83
+ },
84
+ timeout: timeoutMs,
85
+ }, res => {
86
+ const chunks = [];
87
+ res.on('data', c => chunks.push(c));
88
+ res.on('end', () => {
89
+ const text = Buffer.concat(chunks).toString('utf-8');
90
+ if (res.statusCode < 200 || res.statusCode >= 300) {
91
+ return reject(new Error(`HTTP ${res.statusCode}: ${text.slice(0, 500)}`));
92
+ }
93
+ try { resolve(JSON.parse(text)); }
94
+ catch (e) { reject(new Error(`bad JSON: ${e.message}; body head: ${text.slice(0, 200)}`)); }
95
+ });
96
+ });
97
+ req.on('error', reject);
98
+ req.on('timeout', () => req.destroy(new Error(`timeout after ${timeoutMs}ms`)));
99
+ req.write(data);
100
+ req.end();
101
+ } catch (e) {
102
+ reject(new Error(`bad baseUrl: ${baseUrl}: ${e.message}`));
103
+ }
104
+ });
105
+ }
106
+
107
+ async function completeText({ system, user, maxTokens = 512, model, profileId, profile }) {
108
+ const cfg = readConfig({ profileId, profile });
109
+ if (!cfg.apiKey) {
110
+ const id = profileId ? ` for profile ${profileId}` : '';
111
+ throw new Error(`API key not set${id}`);
112
+ }
113
+ const body = {
114
+ model: model || cfg.model,
115
+ max_tokens: maxTokens,
116
+ messages: [{ role: 'user', content: user }],
117
+ };
118
+ if (system) body.system = system;
119
+
120
+ const raw = await postJson({
121
+ baseUrl: cfg.baseUrl,
122
+ path: '/v1/messages',
123
+ body,
124
+ apiKey: cfg.apiKey,
125
+ version: cfg.version,
126
+ timeoutMs: cfg.timeoutMs,
127
+ });
128
+
129
+ const text = (raw.content || [])
130
+ .filter(block => block && block.type === 'text' && typeof block.text === 'string')
131
+ .map(block => block.text)
132
+ .join('\n')
133
+ .trim();
134
+
135
+ return {
136
+ text,
137
+ raw,
138
+ usage: raw.usage || null,
139
+ model: raw.model || body.model,
140
+ profileId: cfg.profileId,
141
+ };
142
+ }
143
+
144
+ async function completeJson({ system, user, schema, maxTokens = 2048, model, profileId, profile }) {
145
+ const schemaHint = schema ? `\n\nThe output MUST be a JSON object matching this schema:\n${schema}\n` : '';
146
+ const finalUser = `${user}${schemaHint}\n\nRespond with ONLY the JSON object, no prose, no markdown fences.`;
147
+ const result = await completeText({ system, user: finalUser, maxTokens, model, profileId, profile });
148
+ const text = result.text;
149
+
150
+ let parsed = null;
151
+ let parseError = null;
152
+ const candidates = [
153
+ text,
154
+ text.replace(/^```(?:json)?\s*/i, '').replace(/```\s*$/, ''),
155
+ ];
156
+ for (const candidate of candidates) {
157
+ if (!candidate) continue;
158
+ try {
159
+ parsed = JSON.parse(candidate);
160
+ parseError = null;
161
+ break;
162
+ } catch (e) {
163
+ parseError = e;
164
+ }
165
+ }
166
+
167
+ return { ...result, parsed, parseError };
168
+ }
169
+
170
+ module.exports = {
171
+ completeJson,
172
+ completeText,
173
+ readConfig,
174
+ findProfile,
175
+ aiProfilesPath,
176
+ };
@@ -1,82 +1,82 @@
1
- // Per-family context window sizes (in tokens) for common models.
2
- // Match strategy: lowercase the model name, then prefix-match — first hit wins.
3
- // Entry order matters: more specific prefixes (e.g. 'claude-opus-4') precede
4
- // broader ones (e.g. 'claude'). Keep specific entries above generic ones.
5
- const MODEL_CONTEXT_WINDOWS = [
6
- // Anthropic Claude — full family 200K
7
- ['claude-opus-4', 200000],
8
- ['claude-sonnet-4', 200000],
9
- ['claude-haiku-4', 200000],
10
- ['claude-3-7', 200000],
11
- ['claude-3-5', 200000],
12
- ['claude-3', 200000],
13
- ['claude', 200000],
14
-
15
- // MiniMax
16
- ['minimax-m3', 1048576],
17
- ['minimax-m2', 1048576],
18
- ['minimax', 1048576],
19
-
20
- // Zhipu GLM
21
- ['glm-5', 1048576],
22
- ['glm-4.6', 1048576],
23
- ['glm-4.5', 128000],
24
- ['glm-4-plus', 128000],
25
- ['glm-4', 128000],
26
- ['glm', 128000],
27
-
28
- // DeepSeek
29
- ['deepseek-v3', 128000],
30
- ['deepseek-r1', 128000],
31
- ['deepseek', 64000],
32
-
33
- // OpenAI
34
- ['gpt-5', 200000],
35
- ['gpt-4.1', 1047576],
36
- ['gpt-4o', 128000],
37
- ['gpt-4-turbo', 128000],
38
- ['gpt-4', 8192],
39
- ['o3', 200000],
40
- ['o4-mini', 200000],
41
- ['o1', 200000],
42
-
43
- // Qwen
44
- ['qwen3', 131072],
45
- ['qwen-max', 32768],
46
- ['qwen', 32768],
47
-
48
- // Google Gemini
49
- ['gemini-2.5', 1048576],
50
- ['gemini-1.5', 1048576],
51
- ['gemini', 32768],
52
- ];
53
-
54
- function normalizeModelName(model) {
55
- return String(model || '').trim().toLowerCase();
56
- }
57
-
58
- function lookupContextWindow(modelName) {
59
- const name = normalizeModelName(modelName);
60
- if (!name) return null;
61
- for (const [prefix, size] of MODEL_CONTEXT_WINDOWS) {
62
- if (name.startsWith(prefix)) return size;
63
- }
64
- return null;
65
- }
66
-
67
- // Resolution order: explicit profile override → model table → env var → fallback.
68
- function resolveContextWindow({ profileContextWindow, model, env = process.env, fallback = 200000 } = {}) {
69
- const explicit = Number(profileContextWindow);
70
- if (Number.isFinite(explicit) && explicit > 0) return Math.floor(explicit);
71
- const tableHit = lookupContextWindow(model);
72
- if (tableHit) return tableHit;
73
- const envValue = Number(env && env.CLAUDE_CONTEXT_WINDOW);
74
- if (Number.isFinite(envValue) && envValue > 0) return Math.floor(envValue);
75
- return fallback;
76
- }
77
-
78
- module.exports = {
79
- MODEL_CONTEXT_WINDOWS,
80
- lookupContextWindow,
81
- resolveContextWindow,
82
- };
1
+ // Per-family context window sizes (in tokens) for common models.
2
+ // Match strategy: lowercase the model name, then prefix-match — first hit wins.
3
+ // Entry order matters: more specific prefixes (e.g. 'claude-opus-4') precede
4
+ // broader ones (e.g. 'claude'). Keep specific entries above generic ones.
5
+ const MODEL_CONTEXT_WINDOWS = [
6
+ // Anthropic Claude — full family 200K
7
+ ['claude-opus-4', 200000],
8
+ ['claude-sonnet-4', 200000],
9
+ ['claude-haiku-4', 200000],
10
+ ['claude-3-7', 200000],
11
+ ['claude-3-5', 200000],
12
+ ['claude-3', 200000],
13
+ ['claude', 200000],
14
+
15
+ // MiniMax
16
+ ['minimax-m3', 1048576],
17
+ ['minimax-m2', 1048576],
18
+ ['minimax', 1048576],
19
+
20
+ // Zhipu GLM
21
+ ['glm-5', 1048576],
22
+ ['glm-4.6', 1048576],
23
+ ['glm-4.5', 128000],
24
+ ['glm-4-plus', 128000],
25
+ ['glm-4', 128000],
26
+ ['glm', 128000],
27
+
28
+ // DeepSeek
29
+ ['deepseek-v3', 128000],
30
+ ['deepseek-r1', 128000],
31
+ ['deepseek', 64000],
32
+
33
+ // OpenAI
34
+ ['gpt-5', 200000],
35
+ ['gpt-4.1', 1047576],
36
+ ['gpt-4o', 128000],
37
+ ['gpt-4-turbo', 128000],
38
+ ['gpt-4', 8192],
39
+ ['o3', 200000],
40
+ ['o4-mini', 200000],
41
+ ['o1', 200000],
42
+
43
+ // Qwen
44
+ ['qwen3', 131072],
45
+ ['qwen-max', 32768],
46
+ ['qwen', 32768],
47
+
48
+ // Google Gemini
49
+ ['gemini-2.5', 1048576],
50
+ ['gemini-1.5', 1048576],
51
+ ['gemini', 32768],
52
+ ];
53
+
54
+ function normalizeModelName(model) {
55
+ return String(model || '').trim().toLowerCase();
56
+ }
57
+
58
+ function lookupContextWindow(modelName) {
59
+ const name = normalizeModelName(modelName);
60
+ if (!name) return null;
61
+ for (const [prefix, size] of MODEL_CONTEXT_WINDOWS) {
62
+ if (name.startsWith(prefix)) return size;
63
+ }
64
+ return null;
65
+ }
66
+
67
+ // Resolution order: explicit profile override → model table → env var → fallback.
68
+ function resolveContextWindow({ profileContextWindow, model, env = process.env, fallback = 200000 } = {}) {
69
+ const explicit = Number(profileContextWindow);
70
+ if (Number.isFinite(explicit) && explicit > 0) return Math.floor(explicit);
71
+ const tableHit = lookupContextWindow(model);
72
+ if (tableHit) return tableHit;
73
+ const envValue = Number(env && env.CLAUDE_CONTEXT_WINDOW);
74
+ if (Number.isFinite(envValue) && envValue > 0) return Math.floor(envValue);
75
+ return fallback;
76
+ }
77
+
78
+ module.exports = {
79
+ MODEL_CONTEXT_WINDOWS,
80
+ lookupContextWindow,
81
+ resolveContextWindow,
82
+ };