Skip to content

Commit f671d7a

Browse files
committed
🤖 refactor: parseModelString returns provider + model tuple
Further eliminates duplication by having parseModelString return both the provider name and model ID as a tuple [providerName, modelId]. This removes the remaining duplicated logic: - modelString.slice(colonIndex + 1) in createModel() - modelString.indexOf(":") check logic Both call sites now use destructuring to get the parts they need. _Generated with `cmux`_
1 parent e90b881 commit f671d7a

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

src/services/aiService.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,21 @@ export async function preloadAISDKProviders(): Promise<void> {
109109
}
110110

111111
/**
112-
* Parse provider name from model string.
113-
* Handles model IDs with colons (e.g., "ollama:gpt-oss:20b" -> "ollama").
112+
* Parse provider and model ID from model string.
113+
* Handles model IDs with colons (e.g., "ollama:gpt-oss:20b").
114114
* Only splits on the first colon to support Ollama model naming convention.
115115
*
116116
* @param modelString - Model string in format "provider:model-id"
117-
* @returns Provider name (e.g., "anthropic", "openai", "ollama")
117+
* @returns Tuple of [providerName, modelId]
118+
* @example
119+
* parseModelString("anthropic:claude-opus-4") // ["anthropic", "claude-opus-4"]
120+
* parseModelString("ollama:gpt-oss:20b") // ["ollama", "gpt-oss:20b"]
118121
*/
119-
function parseProviderName(modelString: string): string {
122+
function parseModelString(modelString: string): [string, string] {
120123
const colonIndex = modelString.indexOf(":");
121-
return colonIndex !== -1 ? modelString.slice(0, colonIndex) : modelString;
124+
const providerName = colonIndex !== -1 ? modelString.slice(0, colonIndex) : modelString;
125+
const modelId = colonIndex !== -1 ? modelString.slice(colonIndex + 1) : "";
126+
return [providerName, modelId];
122127
}
123128

124129
export class AIService extends EventEmitter {
@@ -246,16 +251,7 @@ export class AIService extends EventEmitter {
246251
try {
247252
// Parse model string (format: "provider:model-id")
248253
// Parse provider and model ID from model string
249-
const colonIndex = modelString.indexOf(":");
250-
if (colonIndex === -1) {
251-
return Err({
252-
type: "invalid_model_string",
253-
message: `Invalid model string format: "${modelString}". Expected "provider:model-id"`,
254-
});
255-
}
256-
257-
const providerName = parseProviderName(modelString);
258-
const modelId = modelString.slice(colonIndex + 1);
254+
const [providerName, modelId] = parseModelString(modelString);
259255

260256
if (!providerName || !modelId) {
261257
return Err({
@@ -481,7 +477,7 @@ export class AIService extends EventEmitter {
481477
log.debug_obj(`${workspaceId}/1_original_messages.json`, messages);
482478

483479
// Extract provider name from modelString (e.g., "anthropic:claude-opus-4-1" -> "anthropic")
484-
const providerName = parseProviderName(modelString);
480+
const [providerName] = parseModelString(modelString);
485481

486482
// Get tool names early for mode transition sentinel (stub config, no workspace context needed)
487483
const earlyRuntime = createRuntime({ type: "local", srcBaseDir: process.cwd() });

0 commit comments

Comments
 (0)