@aexol/spectral 0.7.1 → 0.7.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.
Files changed (219) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/dist/agent/agents.js +1 -1
  3. package/dist/agent/index.js +199 -184
  4. package/dist/commands/serve.js +0 -3
  5. package/dist/designer/data/systems/renault/DESIGN.md +1 -1
  6. package/dist/designer/philosophies.js +668 -0
  7. package/dist/mcp/sampling-handler.js +1 -1
  8. package/dist/memory/commands/status.js +1 -1
  9. package/dist/memory/compaction.js +2 -2
  10. package/dist/memory/config.js +1 -1
  11. package/dist/memory/debug-log.js +1 -1
  12. package/dist/memory/hooks/compaction-hook.js +29 -0
  13. package/dist/memory/index.js +2 -0
  14. package/dist/memory/observer.js +2 -2
  15. package/dist/memory/project-observations-store.js +14 -0
  16. package/dist/memory/tokens.js +1 -1
  17. package/dist/memory/tools/read-project-observations.js +82 -0
  18. package/dist/memory/tools/recall-observation.js +2 -2
  19. package/dist/pi/agent-core/agent-loop.js +501 -0
  20. package/dist/pi/agent-core/agent.js +401 -0
  21. package/dist/pi/agent-core/harness/agent-harness.js +899 -0
  22. package/dist/pi/agent-core/harness/compaction/branch-summarization.js +173 -0
  23. package/dist/pi/agent-core/harness/compaction/compaction.js +532 -0
  24. package/dist/pi/agent-core/harness/compaction/utils.js +130 -0
  25. package/dist/pi/agent-core/harness/env/nodejs.js +485 -0
  26. package/dist/pi/agent-core/harness/messages.js +101 -0
  27. package/dist/pi/agent-core/harness/prompt-templates.js +229 -0
  28. package/dist/pi/agent-core/harness/session/jsonl-repo.js +100 -0
  29. package/dist/pi/agent-core/harness/session/jsonl-storage.js +230 -0
  30. package/dist/pi/agent-core/harness/session/memory-repo.js +41 -0
  31. package/dist/pi/agent-core/harness/session/memory-storage.js +113 -0
  32. package/dist/pi/agent-core/harness/session/repo-utils.js +38 -0
  33. package/dist/pi/agent-core/harness/session/session.js +196 -0
  34. package/dist/pi/agent-core/harness/session/uuid.js +49 -0
  35. package/dist/pi/agent-core/harness/skills.js +310 -0
  36. package/dist/pi/agent-core/harness/system-prompt.js +29 -0
  37. package/dist/pi/agent-core/harness/types.js +93 -0
  38. package/dist/pi/agent-core/harness/utils/shell-output.js +125 -0
  39. package/dist/pi/agent-core/harness/utils/truncate.js +289 -0
  40. package/dist/pi/agent-core/index.js +24 -0
  41. package/dist/pi/agent-core/node.js +2 -0
  42. package/dist/pi/agent-core/proxy.js +277 -0
  43. package/dist/pi/agent-core/types.js +1 -0
  44. package/dist/pi/ai/api-registry.js +43 -0
  45. package/dist/pi/ai/cli.js +120 -0
  46. package/dist/pi/ai/env-api-keys.js +169 -0
  47. package/dist/pi/ai/image-models.generated.js +441 -0
  48. package/dist/pi/ai/image-models.js +22 -0
  49. package/dist/pi/ai/images-api-registry.js +21 -0
  50. package/dist/pi/ai/images.js +13 -0
  51. package/dist/pi/ai/index.js +18 -0
  52. package/dist/pi/ai/models.generated.js +16220 -0
  53. package/dist/pi/ai/models.js +70 -0
  54. package/dist/pi/ai/oauth.js +1 -0
  55. package/dist/pi/ai/providers/anthropic.js +945 -0
  56. package/dist/pi/ai/providers/faux.js +367 -0
  57. package/dist/pi/ai/providers/github-copilot-headers.js +28 -0
  58. package/dist/pi/ai/providers/openai-completions.js +945 -0
  59. package/dist/pi/ai/providers/openai-prompt-cache.js +9 -0
  60. package/dist/pi/ai/providers/register-builtins.js +97 -0
  61. package/dist/pi/ai/providers/simple-options.js +40 -0
  62. package/dist/pi/ai/providers/transform-messages.js +183 -0
  63. package/dist/pi/ai/session-resources.js +21 -0
  64. package/dist/pi/ai/stream.js +26 -0
  65. package/dist/pi/ai/types.js +1 -0
  66. package/dist/pi/ai/utils/diagnostics.js +24 -0
  67. package/dist/pi/ai/utils/event-stream.js +80 -0
  68. package/dist/pi/ai/utils/hash.js +13 -0
  69. package/dist/pi/ai/utils/headers.js +7 -0
  70. package/dist/pi/ai/utils/json-parse.js +112 -0
  71. package/dist/pi/ai/utils/node-http-proxy.js +96 -0
  72. package/dist/pi/ai/utils/oauth/anthropic.js +334 -0
  73. package/dist/pi/ai/utils/oauth/device-code.js +54 -0
  74. package/dist/pi/ai/utils/oauth/github-copilot.js +270 -0
  75. package/dist/pi/ai/utils/oauth/index.js +121 -0
  76. package/dist/pi/ai/utils/oauth/oauth-page.js +104 -0
  77. package/dist/pi/ai/utils/oauth/openai-codex.js +384 -0
  78. package/dist/pi/ai/utils/oauth/pkce.js +30 -0
  79. package/dist/pi/ai/utils/oauth/types.js +1 -0
  80. package/dist/pi/ai/utils/overflow.js +150 -0
  81. package/dist/pi/ai/utils/sanitize-unicode.js +25 -0
  82. package/dist/pi/ai/utils/typebox-helpers.js +20 -0
  83. package/dist/pi/ai/utils/validation.js +280 -0
  84. package/dist/pi/coding-agent/bun/cli.js +7 -0
  85. package/dist/pi/coding-agent/bun/restore-sandbox-env.js +31 -0
  86. package/dist/pi/coding-agent/cli/args.js +340 -0
  87. package/dist/pi/coding-agent/cli/file-processor.js +82 -0
  88. package/dist/pi/coding-agent/cli/initial-message.js +21 -0
  89. package/dist/pi/coding-agent/cli.js +17 -0
  90. package/dist/pi/coding-agent/config.js +414 -0
  91. package/dist/pi/coding-agent/core/agent-session-runtime.js +299 -0
  92. package/dist/pi/coding-agent/core/agent-session-services.js +117 -0
  93. package/dist/pi/coding-agent/core/agent-session.js +2498 -0
  94. package/dist/pi/coding-agent/core/auth-guidance.js +20 -0
  95. package/dist/pi/coding-agent/core/auth-storage.js +441 -0
  96. package/dist/pi/coding-agent/core/bash-executor.js +110 -0
  97. package/dist/pi/coding-agent/core/compaction/branch-summarization.js +242 -0
  98. package/dist/pi/coding-agent/core/compaction/compaction.js +624 -0
  99. package/dist/pi/coding-agent/core/compaction/index.js +6 -0
  100. package/dist/pi/coding-agent/core/compaction/utils.js +152 -0
  101. package/dist/pi/coding-agent/core/defaults.js +1 -0
  102. package/dist/pi/coding-agent/core/diagnostics.js +1 -0
  103. package/dist/pi/coding-agent/core/event-bus.js +24 -0
  104. package/dist/pi/coding-agent/core/exec.js +74 -0
  105. package/dist/pi/coding-agent/core/export-html/ansi-to-html.js +248 -0
  106. package/dist/pi/coding-agent/core/export-html/index.js +225 -0
  107. package/dist/pi/coding-agent/core/export-html/tool-renderer.js +107 -0
  108. package/dist/pi/coding-agent/core/extensions/index.js +8 -0
  109. package/dist/pi/coding-agent/core/extensions/loader.js +485 -0
  110. package/dist/pi/coding-agent/core/extensions/runner.js +824 -0
  111. package/dist/pi/coding-agent/core/extensions/types.js +44 -0
  112. package/dist/pi/coding-agent/core/extensions/wrapper.js +21 -0
  113. package/dist/pi/coding-agent/core/footer-data-provider.js +309 -0
  114. package/dist/pi/coding-agent/core/http-dispatcher.js +47 -0
  115. package/dist/pi/coding-agent/core/index.js +11 -0
  116. package/dist/pi/coding-agent/core/keybindings.js +294 -0
  117. package/dist/pi/coding-agent/core/messages.js +122 -0
  118. package/dist/pi/coding-agent/core/model-registry.js +728 -0
  119. package/dist/pi/coding-agent/core/model-resolver.js +494 -0
  120. package/dist/pi/coding-agent/core/output-guard.js +58 -0
  121. package/dist/pi/coding-agent/core/package-manager.js +2020 -0
  122. package/dist/pi/coding-agent/core/prompt-templates.js +237 -0
  123. package/dist/pi/coding-agent/core/provider-display-names.js +32 -0
  124. package/dist/pi/coding-agent/core/resolve-config-value.js +125 -0
  125. package/dist/pi/coding-agent/core/resource-loader.js +733 -0
  126. package/dist/pi/coding-agent/core/sdk.js +282 -0
  127. package/dist/pi/coding-agent/core/session-cwd.js +37 -0
  128. package/dist/pi/coding-agent/core/session-manager.js +1146 -0
  129. package/dist/pi/coding-agent/core/settings-manager.js +794 -0
  130. package/dist/pi/coding-agent/core/skills.js +386 -0
  131. package/dist/pi/coding-agent/core/slash-commands.js +24 -0
  132. package/dist/pi/coding-agent/core/source-info.js +18 -0
  133. package/dist/pi/coding-agent/core/system-prompt.js +122 -0
  134. package/dist/pi/coding-agent/core/telemetry.js +8 -0
  135. package/dist/pi/coding-agent/core/timings.js +30 -0
  136. package/dist/pi/coding-agent/core/tools/bash.js +341 -0
  137. package/dist/pi/coding-agent/core/tools/edit-diff.js +344 -0
  138. package/dist/pi/coding-agent/core/tools/edit.js +324 -0
  139. package/dist/pi/coding-agent/core/tools/file-mutation-queue.js +36 -0
  140. package/dist/pi/coding-agent/core/tools/find.js +297 -0
  141. package/dist/pi/coding-agent/core/tools/grep.js +303 -0
  142. package/dist/pi/coding-agent/core/tools/index.js +111 -0
  143. package/dist/pi/coding-agent/core/tools/ls.js +168 -0
  144. package/dist/pi/coding-agent/core/tools/output-accumulator.js +183 -0
  145. package/dist/pi/coding-agent/core/tools/path-utils.js +61 -0
  146. package/dist/pi/coding-agent/core/tools/read.js +288 -0
  147. package/dist/pi/coding-agent/core/tools/render-utils.js +48 -0
  148. package/dist/pi/coding-agent/core/tools/tool-definition-wrapper.js +33 -0
  149. package/dist/pi/coding-agent/core/tools/truncate.js +214 -0
  150. package/dist/pi/coding-agent/core/tools/write.js +212 -0
  151. package/dist/pi/coding-agent/index.js +41 -0
  152. package/dist/pi/coding-agent/main.js +5 -0
  153. package/dist/pi/coding-agent/migrations.js +280 -0
  154. package/dist/pi/coding-agent/modes/index.js +7 -0
  155. package/dist/pi/coding-agent/modes/interactive/components/diff.js +132 -0
  156. package/dist/pi/coding-agent/modes/interactive/components/keybinding-hints.js +35 -0
  157. package/dist/pi/coding-agent/modes/interactive/components/visual-truncate.js +32 -0
  158. package/dist/pi/coding-agent/modes/interactive/interactive-mode.js +3 -0
  159. package/dist/pi/coding-agent/modes/interactive/theme/theme.js +1023 -0
  160. package/dist/pi/coding-agent/modes/print-mode.js +130 -0
  161. package/dist/pi/coding-agent/modes/rpc/jsonl.js +48 -0
  162. package/dist/pi/coding-agent/modes/rpc/rpc-client.js +409 -0
  163. package/dist/pi/coding-agent/modes/rpc/rpc-mode.js +600 -0
  164. package/dist/pi/coding-agent/modes/rpc/rpc-types.js +7 -0
  165. package/dist/pi/coding-agent/utils/ansi.js +51 -0
  166. package/dist/pi/coding-agent/utils/changelog.js +86 -0
  167. package/dist/pi/coding-agent/utils/child-process.js +87 -0
  168. package/dist/pi/coding-agent/utils/clipboard-image.js +244 -0
  169. package/dist/pi/coding-agent/utils/clipboard-native.js +13 -0
  170. package/dist/pi/coding-agent/utils/clipboard.js +116 -0
  171. package/dist/pi/coding-agent/utils/exif-orientation.js +157 -0
  172. package/dist/pi/coding-agent/utils/frontmatter.js +25 -0
  173. package/dist/pi/coding-agent/utils/fs-watch.js +24 -0
  174. package/dist/pi/coding-agent/utils/git.js +162 -0
  175. package/dist/pi/coding-agent/utils/html.js +39 -0
  176. package/dist/pi/coding-agent/utils/image-convert.js +38 -0
  177. package/dist/pi/coding-agent/utils/image-resize.js +136 -0
  178. package/dist/pi/coding-agent/utils/mime.js +68 -0
  179. package/dist/pi/coding-agent/utils/paths.js +91 -0
  180. package/dist/pi/coding-agent/utils/photon.js +120 -0
  181. package/dist/pi/coding-agent/utils/pi-user-agent.js +4 -0
  182. package/dist/pi/coding-agent/utils/shell.js +194 -0
  183. package/dist/pi/coding-agent/utils/sleep.js +16 -0
  184. package/dist/pi/coding-agent/utils/syntax-highlight.js +117 -0
  185. package/dist/pi/coding-agent/utils/tools-manager.js +327 -0
  186. package/dist/pi/coding-agent/utils/version-check.js +81 -0
  187. package/dist/pi/coding-agent/utils/windows-self-update.js +76 -0
  188. package/dist/pi/tui/autocomplete.js +631 -0
  189. package/dist/pi/tui/components/box.js +103 -0
  190. package/dist/pi/tui/components/cancellable-loader.js +34 -0
  191. package/dist/pi/tui/components/editor.js +1915 -0
  192. package/dist/pi/tui/components/image.js +88 -0
  193. package/dist/pi/tui/components/input.js +425 -0
  194. package/dist/pi/tui/components/loader.js +68 -0
  195. package/dist/pi/tui/components/markdown.js +633 -0
  196. package/dist/pi/tui/components/select-list.js +158 -0
  197. package/dist/pi/tui/components/settings-list.js +184 -0
  198. package/dist/pi/tui/components/spacer.js +22 -0
  199. package/dist/pi/tui/components/text.js +88 -0
  200. package/dist/pi/tui/components/truncated-text.js +50 -0
  201. package/dist/pi/tui/editor-component.js +1 -0
  202. package/dist/pi/tui/fuzzy.js +109 -0
  203. package/dist/pi/tui/index.js +31 -0
  204. package/dist/pi/tui/keybindings.js +173 -0
  205. package/dist/pi/tui/keys.js +1172 -0
  206. package/dist/pi/tui/kill-ring.js +43 -0
  207. package/dist/pi/tui/stdin-buffer.js +360 -0
  208. package/dist/pi/tui/terminal-image.js +335 -0
  209. package/dist/pi/tui/terminal.js +324 -0
  210. package/dist/pi/tui/tui.js +1076 -0
  211. package/dist/pi/tui/undo-stack.js +24 -0
  212. package/dist/pi/tui/utils.js +1016 -0
  213. package/dist/relay/dispatcher.js +30 -0
  214. package/dist/server/handlers/queue.js +52 -0
  215. package/dist/server/pi-bridge.js +9 -1
  216. package/dist/server/session-stream.js +76 -111
  217. package/dist/server/storage.js +154 -2
  218. package/dist/server/title-generator.js +14 -153
  219. package/package.json +24 -6
@@ -0,0 +1,494 @@
1
+ /**
2
+ * Model resolution, scoping, and initial selection
3
+ */
4
+ import { modelsAreEqual } from "../../ai/index.js";
5
+ import chalk from "chalk";
6
+ import { minimatch } from "minimatch";
7
+ import { isValidThinkingLevel } from "../cli/args.js";
8
+ import { DEFAULT_THINKING_LEVEL } from "./defaults.js";
9
+ /** Default model IDs for each known provider */
10
+ export const defaultModelPerProvider = {
11
+ "amazon-bedrock": "us.anthropic.claude-opus-4-6-v1",
12
+ anthropic: "claude-opus-4-7",
13
+ openai: "gpt-5.4",
14
+ "azure-openai-responses": "gpt-5.4",
15
+ "openai-codex": "gpt-5.5",
16
+ deepseek: "deepseek-v4-pro",
17
+ google: "gemini-3.1-pro-preview",
18
+ "google-vertex": "gemini-3.1-pro-preview",
19
+ "github-copilot": "gpt-5.4",
20
+ openrouter: "moonshotai/kimi-k2.6",
21
+ "vercel-ai-gateway": "zai/glm-5.1",
22
+ xai: "grok-4.20-0309-reasoning",
23
+ groq: "openai/gpt-oss-120b",
24
+ cerebras: "zai-glm-4.7",
25
+ zai: "glm-5.1",
26
+ mistral: "devstral-medium-latest",
27
+ minimax: "MiniMax-M2.7",
28
+ "minimax-cn": "MiniMax-M2.7",
29
+ moonshotai: "kimi-k2.6",
30
+ "moonshotai-cn": "kimi-k2.6",
31
+ huggingface: "moonshotai/Kimi-K2.6",
32
+ fireworks: "accounts/fireworks/models/kimi-k2p6",
33
+ together: "moonshotai/Kimi-K2.6",
34
+ opencode: "kimi-k2.6",
35
+ "opencode-go": "kimi-k2.6",
36
+ "kimi-coding": "kimi-for-coding",
37
+ "cloudflare-workers-ai": "@cf/moonshotai/kimi-k2.6",
38
+ "cloudflare-ai-gateway": "workers-ai/@cf/moonshotai/kimi-k2.6",
39
+ xiaomi: "mimo-v2.5-pro",
40
+ "xiaomi-token-plan-cn": "mimo-v2.5-pro",
41
+ "xiaomi-token-plan-ams": "mimo-v2.5-pro",
42
+ "xiaomi-token-plan-sgp": "mimo-v2.5-pro",
43
+ };
44
+ /**
45
+ * Helper to check if a model ID looks like an alias (no date suffix)
46
+ * Dates are typically in format: -20241022 or -20250929
47
+ */
48
+ function isAlias(id) {
49
+ // Check if ID ends with -latest
50
+ if (id.endsWith("-latest"))
51
+ return true;
52
+ // Check if ID ends with a date pattern (-YYYYMMDD)
53
+ const datePattern = /-\d{8}$/;
54
+ return !datePattern.test(id);
55
+ }
56
+ /**
57
+ * Find an exact model reference match.
58
+ * Supports either a bare model id or a canonical provider/modelId reference.
59
+ * When matching by bare id, ambiguous matches across providers are rejected.
60
+ */
61
+ export function findExactModelReferenceMatch(modelReference, availableModels) {
62
+ const trimmedReference = modelReference.trim();
63
+ if (!trimmedReference) {
64
+ return undefined;
65
+ }
66
+ const normalizedReference = trimmedReference.toLowerCase();
67
+ const canonicalMatches = availableModels.filter((model) => `${model.provider}/${model.id}`.toLowerCase() === normalizedReference);
68
+ if (canonicalMatches.length === 1) {
69
+ return canonicalMatches[0];
70
+ }
71
+ if (canonicalMatches.length > 1) {
72
+ return undefined;
73
+ }
74
+ const slashIndex = trimmedReference.indexOf("/");
75
+ if (slashIndex !== -1) {
76
+ const provider = trimmedReference.substring(0, slashIndex).trim();
77
+ const modelId = trimmedReference.substring(slashIndex + 1).trim();
78
+ if (provider && modelId) {
79
+ const providerMatches = availableModels.filter((model) => model.provider.toLowerCase() === provider.toLowerCase() &&
80
+ model.id.toLowerCase() === modelId.toLowerCase());
81
+ if (providerMatches.length === 1) {
82
+ return providerMatches[0];
83
+ }
84
+ if (providerMatches.length > 1) {
85
+ return undefined;
86
+ }
87
+ }
88
+ }
89
+ const idMatches = availableModels.filter((model) => model.id.toLowerCase() === normalizedReference);
90
+ return idMatches.length === 1 ? idMatches[0] : undefined;
91
+ }
92
+ /**
93
+ * Try to match a pattern to a model from the available models list.
94
+ * Returns the matched model or undefined if no match found.
95
+ */
96
+ function tryMatchModel(modelPattern, availableModels) {
97
+ const exactMatch = findExactModelReferenceMatch(modelPattern, availableModels);
98
+ if (exactMatch) {
99
+ return exactMatch;
100
+ }
101
+ // No exact match - fall back to partial matching
102
+ const matches = availableModels.filter((m) => m.id.toLowerCase().includes(modelPattern.toLowerCase()) ||
103
+ m.name?.toLowerCase().includes(modelPattern.toLowerCase()));
104
+ if (matches.length === 0) {
105
+ return undefined;
106
+ }
107
+ // Separate into aliases and dated versions
108
+ const aliases = matches.filter((m) => isAlias(m.id));
109
+ const datedVersions = matches.filter((m) => !isAlias(m.id));
110
+ if (aliases.length > 0) {
111
+ // Prefer alias - if multiple aliases, pick the one that sorts highest
112
+ aliases.sort((a, b) => b.id.localeCompare(a.id));
113
+ return aliases[0];
114
+ }
115
+ else {
116
+ // No alias found, pick latest dated version
117
+ datedVersions.sort((a, b) => b.id.localeCompare(a.id));
118
+ return datedVersions[0];
119
+ }
120
+ }
121
+ function buildFallbackModel(provider, modelId, availableModels) {
122
+ const providerModels = availableModels.filter((m) => m.provider === provider);
123
+ if (providerModels.length === 0)
124
+ return undefined;
125
+ const defaultId = defaultModelPerProvider[provider];
126
+ const baseModel = defaultId
127
+ ? (providerModels.find((m) => m.id === defaultId) ?? providerModels[0])
128
+ : providerModels[0];
129
+ return {
130
+ ...baseModel,
131
+ id: modelId,
132
+ name: modelId,
133
+ };
134
+ }
135
+ /**
136
+ * Parse a pattern to extract model and thinking level.
137
+ * Handles models with colons in their IDs (e.g., OpenRouter's :exacto suffix).
138
+ *
139
+ * Algorithm:
140
+ * 1. Try to match full pattern as a model
141
+ * 2. If found, return it with "off" thinking level
142
+ * 3. If not found and has colons, split on last colon:
143
+ * - If suffix is valid thinking level, use it and recurse on prefix
144
+ * - If suffix is invalid, warn and recurse on prefix with "off"
145
+ *
146
+ * @internal Exported for testing
147
+ */
148
+ export function parseModelPattern(pattern, availableModels, options) {
149
+ // Try exact match first
150
+ const exactMatch = tryMatchModel(pattern, availableModels);
151
+ if (exactMatch) {
152
+ return { model: exactMatch, thinkingLevel: undefined, warning: undefined };
153
+ }
154
+ // No match - try splitting on last colon if present
155
+ const lastColonIndex = pattern.lastIndexOf(":");
156
+ if (lastColonIndex === -1) {
157
+ // No colons, pattern simply doesn't match any model
158
+ return { model: undefined, thinkingLevel: undefined, warning: undefined };
159
+ }
160
+ const prefix = pattern.substring(0, lastColonIndex);
161
+ const suffix = pattern.substring(lastColonIndex + 1);
162
+ if (isValidThinkingLevel(suffix)) {
163
+ // Valid thinking level - recurse on prefix and use this level
164
+ const result = parseModelPattern(prefix, availableModels, options);
165
+ if (result.model) {
166
+ // Only use this thinking level if no warning from inner recursion
167
+ return {
168
+ model: result.model,
169
+ thinkingLevel: result.warning ? undefined : suffix,
170
+ warning: result.warning,
171
+ };
172
+ }
173
+ return result;
174
+ }
175
+ else {
176
+ // Invalid suffix
177
+ const allowFallback = options?.allowInvalidThinkingLevelFallback ?? true;
178
+ if (!allowFallback) {
179
+ // In strict mode (CLI --model parsing), treat it as part of the model id and fail.
180
+ // This avoids accidentally resolving to a different model.
181
+ return { model: undefined, thinkingLevel: undefined, warning: undefined };
182
+ }
183
+ // Scope mode: recurse on prefix and warn
184
+ const result = parseModelPattern(prefix, availableModels, options);
185
+ if (result.model) {
186
+ return {
187
+ model: result.model,
188
+ thinkingLevel: undefined,
189
+ warning: `Invalid thinking level "${suffix}" in pattern "${pattern}". Using default instead.`,
190
+ };
191
+ }
192
+ return result;
193
+ }
194
+ }
195
+ /**
196
+ * Resolve model patterns to actual Model objects with optional thinking levels
197
+ * Format: "pattern:level" where :level is optional
198
+ * For each pattern, finds all matching models and picks the best version:
199
+ * 1. Prefer alias (e.g., claude-sonnet-4-5) over dated versions (claude-sonnet-4-5-20250929)
200
+ * 2. If no alias, pick the latest dated version
201
+ *
202
+ * Supports models with colons in their IDs (e.g., OpenRouter's model:exacto).
203
+ * The algorithm tries to match the full pattern first, then progressively
204
+ * strips colon-suffixes to find a match.
205
+ */
206
+ export async function resolveModelScope(patterns, modelRegistry) {
207
+ const availableModels = await modelRegistry.getAvailable();
208
+ const scopedModels = [];
209
+ for (const pattern of patterns) {
210
+ // Check if pattern contains glob characters
211
+ if (pattern.includes("*") || pattern.includes("?") || pattern.includes("[")) {
212
+ // Extract optional thinking level suffix (e.g., "provider/*:high")
213
+ const colonIdx = pattern.lastIndexOf(":");
214
+ let globPattern = pattern;
215
+ let thinkingLevel;
216
+ if (colonIdx !== -1) {
217
+ const suffix = pattern.substring(colonIdx + 1);
218
+ if (isValidThinkingLevel(suffix)) {
219
+ thinkingLevel = suffix;
220
+ globPattern = pattern.substring(0, colonIdx);
221
+ }
222
+ }
223
+ // Match against "provider/modelId" format OR just model ID
224
+ // This allows "*sonnet*" to match without requiring "anthropic/*sonnet*"
225
+ const matchingModels = availableModels.filter((m) => {
226
+ const fullId = `${m.provider}/${m.id}`;
227
+ return minimatch(fullId, globPattern, { nocase: true }) || minimatch(m.id, globPattern, { nocase: true });
228
+ });
229
+ if (matchingModels.length === 0) {
230
+ console.warn(chalk.yellow(`Warning: No models match pattern "${pattern}"`));
231
+ continue;
232
+ }
233
+ for (const model of matchingModels) {
234
+ if (!scopedModels.find((sm) => modelsAreEqual(sm.model, model))) {
235
+ scopedModels.push({ model, thinkingLevel });
236
+ }
237
+ }
238
+ continue;
239
+ }
240
+ const { model, thinkingLevel, warning } = parseModelPattern(pattern, availableModels);
241
+ if (warning) {
242
+ console.warn(chalk.yellow(`Warning: ${warning}`));
243
+ }
244
+ if (!model) {
245
+ console.warn(chalk.yellow(`Warning: No models match pattern "${pattern}"`));
246
+ continue;
247
+ }
248
+ // Avoid duplicates
249
+ if (!scopedModels.find((sm) => modelsAreEqual(sm.model, model))) {
250
+ scopedModels.push({ model, thinkingLevel });
251
+ }
252
+ }
253
+ return scopedModels;
254
+ }
255
+ /**
256
+ * Resolve a single model from CLI flags.
257
+ *
258
+ * Supports:
259
+ * - --provider <provider> --model <pattern>
260
+ * - --model <provider>/<pattern>
261
+ * - Fuzzy matching (same rules as model scoping: exact id, then partial id/name)
262
+ *
263
+ * Note: This does not apply the thinking level by itself, but it may *parse* and
264
+ * return a thinking level from "<pattern>:<thinking>" so the caller can apply it.
265
+ */
266
+ export function resolveCliModel(options) {
267
+ const { cliProvider, cliModel, modelRegistry } = options;
268
+ if (!cliModel) {
269
+ return { model: undefined, warning: undefined, error: undefined };
270
+ }
271
+ // Important: use *all* models here, not just models with pre-configured auth.
272
+ // This allows "--api-key" to be used for first-time setup.
273
+ const availableModels = modelRegistry.getAll();
274
+ if (availableModels.length === 0) {
275
+ return {
276
+ model: undefined,
277
+ warning: undefined,
278
+ error: "No models available. Check your installation or add models to models.json.",
279
+ };
280
+ }
281
+ // Build canonical provider lookup (case-insensitive)
282
+ const providerMap = new Map();
283
+ for (const m of availableModels) {
284
+ providerMap.set(m.provider.toLowerCase(), m.provider);
285
+ }
286
+ let provider = cliProvider ? providerMap.get(cliProvider.toLowerCase()) : undefined;
287
+ if (cliProvider && !provider) {
288
+ return {
289
+ model: undefined,
290
+ warning: undefined,
291
+ error: `Unknown provider "${cliProvider}". Use --list-models to see available providers/models.`,
292
+ };
293
+ }
294
+ // If no explicit --provider, try to interpret "provider/model" format first.
295
+ // When the prefix before the first slash matches a known provider, prefer that
296
+ // interpretation over matching models whose IDs literally contain slashes
297
+ // (e.g. "zai/glm-5" should resolve to provider=zai, model=glm-5, not to a
298
+ // vercel-ai-gateway model with id "zai/glm-5").
299
+ let pattern = cliModel;
300
+ let inferredProvider = false;
301
+ if (!provider) {
302
+ const slashIndex = cliModel.indexOf("/");
303
+ if (slashIndex !== -1) {
304
+ const maybeProvider = cliModel.substring(0, slashIndex);
305
+ const canonical = providerMap.get(maybeProvider.toLowerCase());
306
+ if (canonical) {
307
+ provider = canonical;
308
+ pattern = cliModel.substring(slashIndex + 1);
309
+ inferredProvider = true;
310
+ }
311
+ }
312
+ }
313
+ // If no provider was inferred from the slash, try exact matches without provider inference.
314
+ // This handles models whose IDs naturally contain slashes (e.g. OpenRouter-style IDs).
315
+ if (!provider) {
316
+ const lower = cliModel.toLowerCase();
317
+ const exact = availableModels.find((m) => m.id.toLowerCase() === lower || `${m.provider}/${m.id}`.toLowerCase() === lower);
318
+ if (exact) {
319
+ return { model: exact, warning: undefined, thinkingLevel: undefined, error: undefined };
320
+ }
321
+ }
322
+ if (cliProvider && provider) {
323
+ // If both were provided, tolerate --model <provider>/<pattern> by stripping the provider prefix
324
+ const prefix = `${provider}/`;
325
+ if (cliModel.toLowerCase().startsWith(prefix.toLowerCase())) {
326
+ pattern = cliModel.substring(prefix.length);
327
+ }
328
+ }
329
+ const candidates = provider ? availableModels.filter((m) => m.provider === provider) : availableModels;
330
+ const { model, thinkingLevel, warning } = parseModelPattern(pattern, candidates, {
331
+ allowInvalidThinkingLevelFallback: false,
332
+ });
333
+ if (model) {
334
+ return { model, thinkingLevel, warning, error: undefined };
335
+ }
336
+ // If we inferred a provider from the slash but found no match within that provider,
337
+ // fall back to matching the full input as a raw model id across all models.
338
+ // This handles OpenRouter-style IDs like "openai/gpt-4o:extended" where "openai"
339
+ // looks like a provider but the full string is actually a model id on openrouter.
340
+ if (inferredProvider) {
341
+ const lower = cliModel.toLowerCase();
342
+ const exact = availableModels.find((m) => m.id.toLowerCase() === lower || `${m.provider}/${m.id}`.toLowerCase() === lower);
343
+ if (exact) {
344
+ return { model: exact, warning: undefined, thinkingLevel: undefined, error: undefined };
345
+ }
346
+ // Also try parseModelPattern on the full input against all models
347
+ const fallback = parseModelPattern(cliModel, availableModels, {
348
+ allowInvalidThinkingLevelFallback: false,
349
+ });
350
+ if (fallback.model) {
351
+ return {
352
+ model: fallback.model,
353
+ thinkingLevel: fallback.thinkingLevel,
354
+ warning: fallback.warning,
355
+ error: undefined,
356
+ };
357
+ }
358
+ }
359
+ if (provider) {
360
+ const fallbackModel = buildFallbackModel(provider, pattern, availableModels);
361
+ if (fallbackModel) {
362
+ const fallbackWarning = warning
363
+ ? `${warning} Model "${pattern}" not found for provider "${provider}". Using custom model id.`
364
+ : `Model "${pattern}" not found for provider "${provider}". Using custom model id.`;
365
+ return { model: fallbackModel, thinkingLevel: undefined, warning: fallbackWarning, error: undefined };
366
+ }
367
+ }
368
+ const display = provider ? `${provider}/${pattern}` : cliModel;
369
+ return {
370
+ model: undefined,
371
+ thinkingLevel: undefined,
372
+ warning,
373
+ error: `Model "${display}" not found. Use --list-models to see available models.`,
374
+ };
375
+ }
376
+ /**
377
+ * Find the initial model to use based on priority:
378
+ * 1. CLI args (provider + model)
379
+ * 2. First model from scoped models (if not continuing/resuming)
380
+ * 3. Restored from session (if continuing/resuming)
381
+ * 4. Saved default from settings
382
+ * 5. First available model with valid API key
383
+ */
384
+ export async function findInitialModel(options) {
385
+ const { cliProvider, cliModel, scopedModels, isContinuing, defaultProvider, defaultModelId, defaultThinkingLevel, modelRegistry, } = options;
386
+ let model;
387
+ let thinkingLevel = DEFAULT_THINKING_LEVEL;
388
+ // 1. CLI args take priority
389
+ if (cliProvider && cliModel) {
390
+ const resolved = resolveCliModel({
391
+ cliProvider,
392
+ cliModel,
393
+ modelRegistry,
394
+ });
395
+ if (resolved.error) {
396
+ console.error(chalk.red(resolved.error));
397
+ process.exit(1);
398
+ }
399
+ if (resolved.model) {
400
+ return { model: resolved.model, thinkingLevel: DEFAULT_THINKING_LEVEL, fallbackMessage: undefined };
401
+ }
402
+ }
403
+ // 2. Use first model from scoped models (skip if continuing/resuming)
404
+ if (scopedModels.length > 0 && !isContinuing) {
405
+ return {
406
+ model: scopedModels[0].model,
407
+ thinkingLevel: scopedModels[0].thinkingLevel ?? defaultThinkingLevel ?? DEFAULT_THINKING_LEVEL,
408
+ fallbackMessage: undefined,
409
+ };
410
+ }
411
+ // 3. Try saved default from settings
412
+ if (defaultProvider && defaultModelId) {
413
+ const found = modelRegistry.find(defaultProvider, defaultModelId);
414
+ if (found) {
415
+ model = found;
416
+ if (defaultThinkingLevel) {
417
+ thinkingLevel = defaultThinkingLevel;
418
+ }
419
+ return { model, thinkingLevel, fallbackMessage: undefined };
420
+ }
421
+ }
422
+ // 4. Try first available model with valid API key
423
+ const availableModels = await modelRegistry.getAvailable();
424
+ if (availableModels.length > 0) {
425
+ // Try to find a default model from known providers
426
+ for (const provider of Object.keys(defaultModelPerProvider)) {
427
+ const defaultId = defaultModelPerProvider[provider];
428
+ const match = availableModels.find((m) => m.provider === provider && m.id === defaultId);
429
+ if (match) {
430
+ return { model: match, thinkingLevel: DEFAULT_THINKING_LEVEL, fallbackMessage: undefined };
431
+ }
432
+ }
433
+ // If no default found, use first available
434
+ return { model: availableModels[0], thinkingLevel: DEFAULT_THINKING_LEVEL, fallbackMessage: undefined };
435
+ }
436
+ // 5. No model found
437
+ return { model: undefined, thinkingLevel: DEFAULT_THINKING_LEVEL, fallbackMessage: undefined };
438
+ }
439
+ /**
440
+ * Restore model from session, with fallback to available models
441
+ */
442
+ export async function restoreModelFromSession(savedProvider, savedModelId, currentModel, shouldPrintMessages, modelRegistry) {
443
+ const restoredModel = modelRegistry.find(savedProvider, savedModelId);
444
+ // Check if restored model exists and still has auth configured
445
+ const hasConfiguredAuth = restoredModel ? modelRegistry.hasConfiguredAuth(restoredModel) : false;
446
+ if (restoredModel && hasConfiguredAuth) {
447
+ if (shouldPrintMessages) {
448
+ console.log(chalk.dim(`Restored model: ${savedProvider}/${savedModelId}`));
449
+ }
450
+ return { model: restoredModel, fallbackMessage: undefined };
451
+ }
452
+ // Model not found or no API key - fall back
453
+ const reason = !restoredModel ? "model no longer exists" : "no auth configured";
454
+ if (shouldPrintMessages) {
455
+ console.error(chalk.yellow(`Warning: Could not restore model ${savedProvider}/${savedModelId} (${reason}).`));
456
+ }
457
+ // If we already have a model, use it as fallback
458
+ if (currentModel) {
459
+ if (shouldPrintMessages) {
460
+ console.log(chalk.dim(`Falling back to: ${currentModel.provider}/${currentModel.id}`));
461
+ }
462
+ return {
463
+ model: currentModel,
464
+ fallbackMessage: `Could not restore model ${savedProvider}/${savedModelId} (${reason}). Using ${currentModel.provider}/${currentModel.id}.`,
465
+ };
466
+ }
467
+ // Try to find any available model
468
+ const availableModels = await modelRegistry.getAvailable();
469
+ if (availableModels.length > 0) {
470
+ // Try to find a default model from known providers
471
+ let fallbackModel;
472
+ for (const provider of Object.keys(defaultModelPerProvider)) {
473
+ const defaultId = defaultModelPerProvider[provider];
474
+ const match = availableModels.find((m) => m.provider === provider && m.id === defaultId);
475
+ if (match) {
476
+ fallbackModel = match;
477
+ break;
478
+ }
479
+ }
480
+ // If no default found, use first available
481
+ if (!fallbackModel) {
482
+ fallbackModel = availableModels[0];
483
+ }
484
+ if (shouldPrintMessages) {
485
+ console.log(chalk.dim(`Falling back to: ${fallbackModel.provider}/${fallbackModel.id}`));
486
+ }
487
+ return {
488
+ model: fallbackModel,
489
+ fallbackMessage: `Could not restore model ${savedProvider}/${savedModelId} (${reason}). Using ${fallbackModel.provider}/${fallbackModel.id}.`,
490
+ };
491
+ }
492
+ // No models available
493
+ return { model: undefined, fallbackMessage: undefined };
494
+ }
@@ -0,0 +1,58 @@
1
+ let stdoutTakeoverState;
2
+ export function takeOverStdout() {
3
+ if (stdoutTakeoverState) {
4
+ return;
5
+ }
6
+ const rawStdoutWrite = process.stdout.write.bind(process.stdout);
7
+ const rawStderrWrite = process.stderr.write.bind(process.stderr);
8
+ const originalStdoutWrite = process.stdout.write;
9
+ process.stdout.write = ((chunk, encodingOrCallback, callback) => {
10
+ if (typeof encodingOrCallback === "function") {
11
+ return rawStderrWrite(String(chunk), encodingOrCallback);
12
+ }
13
+ return rawStderrWrite(String(chunk), callback);
14
+ });
15
+ stdoutTakeoverState = {
16
+ rawStdoutWrite,
17
+ rawStderrWrite,
18
+ originalStdoutWrite,
19
+ };
20
+ }
21
+ export function restoreStdout() {
22
+ if (!stdoutTakeoverState) {
23
+ return;
24
+ }
25
+ process.stdout.write = stdoutTakeoverState.originalStdoutWrite;
26
+ stdoutTakeoverState = undefined;
27
+ }
28
+ export function isStdoutTakenOver() {
29
+ return stdoutTakeoverState !== undefined;
30
+ }
31
+ export function writeRawStdout(text) {
32
+ if (stdoutTakeoverState) {
33
+ stdoutTakeoverState.rawStdoutWrite(text);
34
+ return;
35
+ }
36
+ process.stdout.write(text);
37
+ }
38
+ export async function flushRawStdout() {
39
+ if (stdoutTakeoverState) {
40
+ await new Promise((resolve, reject) => {
41
+ stdoutTakeoverState?.rawStdoutWrite("", (err) => {
42
+ if (err)
43
+ reject(err);
44
+ else
45
+ resolve();
46
+ });
47
+ });
48
+ return;
49
+ }
50
+ await new Promise((resolve, reject) => {
51
+ process.stdout.write("", (err) => {
52
+ if (err)
53
+ reject(err);
54
+ else
55
+ resolve();
56
+ });
57
+ });
58
+ }