resolveLlmRelayTarget function
- LlmRelayRequest request, {
- required Iterable<
CustomProviderEntry> providers, - String? resolveKey(
- CustomProviderEntry entry
The SEC-01 resolution over the saved-provider table (pure; hosts inject
the key lookup): a named provider resolves endpoint AND key from the
SAME record — a client baseUrl that is not byte-equal to the record's
is a rejection checked BEFORE any network call; an unknown name is a
named rejection; a record whose catalog wire dialect is not
openai-completions is rejected by dialect (the saved apiType is a
catalog NAME — kimi/zai/openrouter/… forward keyed — while anthropic/
google/chatgpt-responses reject); no name at all → null (anonymous,
keyless) — unless the unnamed baseUrl byte-matches a KEYED saved
record, which answers a migration hint instead (legacy clients named
no provider; the old URL-keyed branch served them, and a raw endpoint
401 is not actionable).
Implementation
LlmRelayTarget? resolveLlmRelayTarget(
LlmRelayRequest request, {
required Iterable<CustomProviderEntry> providers,
String? Function(CustomProviderEntry entry)? resolveKey,
}) {
final name = request.provider;
if (name == null) {
// Narrow path: the key lookup only runs when the baseUrl byte-matches
// a saved record (never per unnamed frame).
final keyedRecord = providers.any(
(e) => e.baseUrl == request.baseUrl && (resolveKey?.call(e) != null),
);
if (keyedRecord) {
return LlmRelayTarget.reject(
'keyless relay to a saved provider - update the extension / '
're-pair (/browser connect) so llmReq names its provider',
);
}
return null;
}
final entry = providers.where((e) => e.name == name).firstOrNull;
if (entry == null) {
return LlmRelayTarget.reject(
'unknown provider "$name" - re-pair (/browser connect) to refresh '
'the provider list',
);
}
// The saved apiType selects the catalog spec; the spec's `api` field is
// the wire dialect — kimi/zai/openrouter/minimax/aiin/dial/copilot/
// codemie are all openai-completions and the relay forwards them keyed
// (they are exactly what `/provider kimi` et al. save); only genuinely
// foreign dialects (anthropic, google, chatgpt responses, unknown
// names) reject.
final dialect = providerCatalog[entry.apiType]?.api;
if (dialect != 'openai-completions') {
return LlmRelayTarget.reject(
'provider "$name" uses the "${entry.apiType}" dialect - the relay '
'forwards openai-completions endpoints only',
);
}
// Byte-equality BEFORE any network call: a client-chosen baseUrl can
// never steer a stored key to another address.
if (request.baseUrl != entry.baseUrl) {
return LlmRelayTarget.reject(
'provider "$name" is served at ${entry.baseUrl} - the relay sends '
'stored keys only to the saved record\'s own endpoint '
'(got ${Uri.tryParse(request.baseUrl)?.host ?? request.baseUrl})',
);
}
return LlmRelayTarget(baseUrl: entry.baseUrl, key: resolveKey?.call(entry));
}