Skip to content

Commit e90b881

Browse files
committed
🤖 refactor: extract parseProviderName to eliminate duplication
Consolidates two instances of provider name extraction logic into a single helper function. Both createModel() and streamMessage() were duplicating the logic to parse provider names from model strings. The helper properly handles Ollama model IDs with colons (e.g., "ollama:gpt-oss:20b" -> "ollama") by splitting only on the first colon. _Generated with `cmux`_
1 parent 82b51a2 commit e90b881

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/services/aiService.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,19 @@ export async function preloadAISDKProviders(): Promise<void> {
108108
]);
109109
}
110110

111+
/**
112+
* Parse provider name from model string.
113+
* Handles model IDs with colons (e.g., "ollama:gpt-oss:20b" -> "ollama").
114+
* Only splits on the first colon to support Ollama model naming convention.
115+
*
116+
* @param modelString - Model string in format "provider:model-id"
117+
* @returns Provider name (e.g., "anthropic", "openai", "ollama")
118+
*/
119+
function parseProviderName(modelString: string): string {
120+
const colonIndex = modelString.indexOf(":");
121+
return colonIndex !== -1 ? modelString.slice(0, colonIndex) : modelString;
122+
}
123+
111124
export class AIService extends EventEmitter {
112125
private readonly streamManager: StreamManager;
113126
private readonly historyService: HistoryService;
@@ -232,7 +245,7 @@ export class AIService extends EventEmitter {
232245
): Promise<Result<LanguageModel, SendMessageError>> {
233246
try {
234247
// Parse model string (format: "provider:model-id")
235-
// Only split on the first colon to support model IDs with colons (e.g., "ollama:gpt-oss:20b")
248+
// Parse provider and model ID from model string
236249
const colonIndex = modelString.indexOf(":");
237250
if (colonIndex === -1) {
238251
return Err({
@@ -241,7 +254,7 @@ export class AIService extends EventEmitter {
241254
});
242255
}
243256

244-
const providerName = modelString.slice(0, colonIndex);
257+
const providerName = parseProviderName(modelString);
245258
const modelId = modelString.slice(colonIndex + 1);
246259

247260
if (!providerName || !modelId) {
@@ -468,9 +481,7 @@ export class AIService extends EventEmitter {
468481
log.debug_obj(`${workspaceId}/1_original_messages.json`, messages);
469482

470483
// Extract provider name from modelString (e.g., "anthropic:claude-opus-4-1" -> "anthropic")
471-
// Use indexOf to handle model IDs with colons (e.g., "ollama:gpt-oss:20b")
472-
const colonIndex = modelString.indexOf(":");
473-
const providerName = colonIndex !== -1 ? modelString.slice(0, colonIndex) : modelString;
484+
const providerName = parseProviderName(modelString);
474485

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

0 commit comments

Comments
 (0)