( connection: ModelRuntimeConnection, modelId: string, )
| 41 | } |
| 42 | |
| 43 | export function resolveModelRuntime( |
| 44 | connection: ModelRuntimeConnection, |
| 45 | modelId: string, |
| 46 | ): ResolvedModelRuntime { |
| 47 | const override = lookupModelProviderOverride(connection.providerType, modelId); |
| 48 | const defaults = PROVIDER_DEFAULTS[connection.providerType]; |
| 49 | // Unknown providerType with no per-model override → can't resolve an adapter. |
| 50 | // Throw a clear error rather than crashing on `.runtimeAdapter`. Mirrors |
| 51 | // `isFakeBackend` in @maka/core/connection-readiness.ts. |
| 52 | if (!override && !defaults) { |
| 53 | throw new Error( |
| 54 | `Unknown provider type "${connection.providerType}"; cannot resolve model runtime.`, |
| 55 | ); |
| 56 | } |
| 57 | const apiProtocol = connection.models?.find((model) => model.id === modelId)?.apiProtocol; |
| 58 | if ( |
| 59 | connection.providerType === 'kimi-coding-plan' && |
| 60 | apiProtocol !== undefined && |
| 61 | apiProtocol !== 'anthropic-messages' && |
| 62 | apiProtocol !== 'openai-chat' |
| 63 | ) { |
| 64 | throw new Error( |
| 65 | `Kimi Coding Plan protocol must be openai-chat or anthropic-messages, received ${apiProtocol}`, |
| 66 | ); |
| 67 | } |
| 68 | const adapter: ProviderRuntimeAdapter = |
| 69 | connection.providerType === 'kimi-coding-plan' && apiProtocol === 'openai-chat' |
| 70 | ? ({ |
| 71 | kind: 'openai-compatible', |
| 72 | name: 'provider', |
| 73 | includeUsage: true, |
| 74 | } as const) |
| 75 | : override |
| 76 | ? runtimeAdapterOverride(override.npm) |
| 77 | : defaults.runtimeAdapter; |
| 78 | const configuredBaseUrl = connection.baseUrl?.trim(); |
| 79 | const resolvedBaseUrl = configuredBaseUrl |
| 80 | ? effectiveBaseUrl(connection) |
| 81 | : (override?.api ?? effectiveBaseUrl(connection)); |
| 82 | const wire = resolveModelRuntimeWire(connection.providerType, modelId, adapter, apiProtocol); |
| 83 | return { |
| 84 | adapter, |
| 85 | baseUrl: |
| 86 | connection.providerType === 'kimi-coding-plan' && apiProtocol === 'openai-chat' |
| 87 | ? kimiOpenAiBaseUrl(resolvedBaseUrl) |
| 88 | : resolvedBaseUrl, |
| 89 | ...(apiProtocol ? { apiProtocol } : {}), |
| 90 | wire, |
| 91 | reasoningReplay: reasoningReplayContract(adapter, wire), |
| 92 | applyPatchProfile: resolveApplyPatchProfile( |
| 93 | { wire, applyPatchProtocol: adapter.applyPatchProtocol }, |
| 94 | modelId, |
| 95 | ), |
| 96 | }; |
| 97 | } |
| 98 | |
| 99 | export function modelUsesAnthropicMessages( |
| 100 | connection: ModelRuntimeConnection, |
no test coverage detected