* provider: a multi-protocol provider's catalog is the union of every endpoint's models A provider whose protocols sit at bases of their own kept only the first list that answered, so the models of every other endpoint were nowhere in the product: not in the agents' model lists, not in the catalog the routing and the per-model preferences read, and not fixable by pinning a model's protocol, which setModelAPI refuses for a model the provider does not list. nyownm reported it as #904 and worked around it with one provider per protocol. fetchOne now asks every endpoint the provider speaks and merges the lists by model id, the way fetchPerKey merges the lists of a vendor's keys: the first list's fields stand, and an image capability both lists answer is merged. The base kept is still the first protocol's, the one the drawer list is fetched beside and the models are saved with. A models URL is still asked alone, one base two protocols share is still asked once, and a provider with no endpoint answering still fails with every URL it asked named in the error. Verified with go test -tags nogui ./internal/provider ./internal/catalog and ./internal/gateway ./internal/agent, all green. TestFetchUnionsProtocolLists and TestFetchUnionsProtocolListsWithoutChat fail on the base commit with the single list the first protocol answered. * provider: an endpoint that can't be asked keeps the models it listed A provider whose protocols sit at bases of their own is asked every base and the lists are merged, so one read that failed saved the union of the rest as the whole list: the models of the failed base read as gone, dropGonePicks took them out of the user's picks and stored that, and nothing brought them back when the endpoint recovered (yetone's review of #1006, on nyownm's #904). A read that failed says nothing about what the vendor serves today, the rule fetchPerKey's keys already follow. fetchOne keeps what each base listed and saves it beside the merged list, in a new "sides" part of the catalog's live file, and a base it could not ask takes its own part back for that fetch. What is kept is that base's own models alone: a model the bases that answered have dropped is dropped here too, however many of them dropped it and however many bases are down. Keeping the whole list saved before would bring such a model back, which is the same fault the other way round. A list saved before this kept no parts, so a base whose part is missing keeps what no answering base lists, until the next fetch with every base answering says which they are. The parts are kept only where more than one base was asked: a single one that fails is the fetch that fails, and its list is kept as it is, so a provider of one protocol's cache is the file it was. fetchOne answers one fetched list now — the models, the base kept for routing, the parts — rather than three values. fetchPerKey marks the parts with the keys that saw them, and keeps a plan's own models out of them. Verified with go test ./internal/provider ./internal/catalog, all green, and ./internal/gateway ./internal/agent. TestFetchKeepsTheModelsAFailedProtocolListed, TestFetchDropsAGoneModelWhileAnotherProtocolFails and TestFetchKeepsTheModelsAFailedProtocolListedPerKey fail on 952d0b00 with the model of the failed protocol taken out of the picks. Co-authored-by: multica-agent <github@multica.ai> * provider: a base that never answered keeps nothing, and is not saved as one that listed nothing keepDeadSides read a part missing from the saved sides as the whole list saved before, and kept what no answering base lists from it. Two things were wrong with that, and both were reported on #1006 (nyownm's #904): A base in none of the saved parts is one that has never answered. The whole list is not its own, so a model its vendor had already dropped was kept for it, and the keepDeadSides result was saved as sides[base] — from then on that base had a part, and the model could never be dropped again. An Anthropic base answering 404 from the start of a provider's life was enough: one round after the Chat base dropped a model, that model stayed in the catalog, and dropGonePicks could not take it out of the user's picks. The saving is the other half of the same fault. A part is what a base listed, so one that never answered has none, and writing an empty one fixes the reading of "never answered" as "listed nothing" for good. Only a part read out of the file is written back; the list saved without any parts at all — before this kept them — is the one case where a base may stand in for a missing part, and what it stands in for is kept for that fetch alone, never saved as its own. So keepDeadSides answers two lists now: the models this fetch's list is made of, and the parts to write beside it. fetchPerKey keeps a model a key that could not be asked at all owns to the bases that listed it, read out of the parts saved beside the list: kept as a whole list with no part saying where it came from it belonged to no base, and was read as gone the first time that key answered half-way — the same permanent deletion of a picked model, one round later and needing no new base. Verified with go test ./internal/provider ./internal/catalog ./internal/gateway ./internal/agent, all green, and -race -count=3 over ./internal/provider with the four failures it already had. TestFetchDropsAModelWhileAnEndpointHasNeverAnswered and TestFetchKeepsAModelsBaseAfterItsKeyFailedWhole fail on 6621f73e. Co-authored-by: multica-agent <github@multica.ai> * provider: a base that answered without a model does not keep owning it fetchPerKey gave every model kept for a key that could not be asked at all to the bases that listed it last time, and a part there said what the bases listed when one of them had answered this round without it (yetone's review of #1006, nyownm's #904). With two keys where both were served a Claude model, and the Anthropic base stopping both of them in the round the second key was down, sides[anthropic] kept a model the base had plainly dropped, and the fetch after that read it as a base that couldn't be asked and held the model one round longer than it should have. It is self-healing — the next round with the base answering settles it — and it is the same fault c8fabd70 was written to undo, read from the other side. sides may only say what the bases listed. A model kept for a failed key belongs to a base that was not asked this round for any of the model's own keys: one that was, and answered without the model, has said what it serves today and no longer owns it. Which is per key, not per provider: fetchOne answers which of its bases replied, which a model of the second key alone never saw an answer from, so the base's saying nothing about it for the first key leaves it that base's. The keys a model is kept under are read from the list saved before, since add clears the ones of the model it is adding. TestFetchDropsAModelABaseStoppedServingWhileAKeyIsDown fails on c8fabd70 with the part holding claude-b, and TestFetchKeepsAModelsBaseAfterItsKeyFailedWhole goes on passing: the same two keys, a model only one of them sees. Co-authored-by: multica-agent <github@multica.ai> * provider, catalog: a page of a model list is not the list (#904) A /v1/models that answers with has_more is one page of the vendor's catalog. Read as the whole list, every model of a later page reads as one the vendor has dropped, and the user's pick of it goes for good, on a list that never said so. Such a reply is no answer: the base is treated as one that could not be asked, which keeps what it listed last time (#904's review of #1006). OpenRouter's Anthropic base is not asked for the list at all. With the Anthropic version header its /models answers with the catalog again, twenty newest first, every id namespaced under `anthropic/`, and the cursors of the pages after. None of those twenty is an id in OpenRouter's own catalog, so merged they are entries of the model picker that name no model: the long, cluttered list #904's reporter wrote of. Its Chat base is that catalog, whole, and the fetch is one request where it was two. The rule is OpenRouter's, by host, as the ones after it are scoped to their vendors. Co-authored-by: multica-agent <github@multica.ai> * provider: the OpenRouter rule reads the base being asked (0fb16990) It read the Chat base's host, so a provider with a Chat base of OpenRouter's and an Anthropic base of another vendor's had that other base skipped: its models were in neither the union nor the sides nor the bases that could not be asked, so no keepDeadSides path found them and the user's picks of them went for good. `magpie provider set <id> anthropic=<url>` reaches that shape from the CLI. The rule is about the vendor whose base is being asked, so that is what it now reads, together with whether the provider asks at a base of its own besides it: an OpenRouter Anthropic base alone is still asked, its reply being the only catalog there would otherwise be, and catalog.fetchOne is what keeps a page of it from being read as the whole. Five combinations of the two bases and openrouter.ai, one assertion each; three of them fail on 0fb16990. Co-authored-by: multica-agent <github@multica.ai> * catalog, provider: a model list is followed to its last page, not read as a page (#1006) A reply with has_more was refused as no answer at all, which is right for a provider with several bases and wrong for one with a single base: Anthropic's own preset has no other base to fall back on, so once its list passes twenty models — about sixteen today — the fetch answered an error and no models, where main listed the twenty of the first page, and a pick of a model on a later page went with them. catalog.fetchOne now reads a list to its end: the first page as the URL stands, and every page after it with limit=1000 and the after_id the page before ended at, up to twenty pages. Half a list is still no answer, and that is what the following ends on: a page that fails, a list that pages without naming the id to carry on from, one that answers the page just asked again under the same id, and one still paging at the cap all leave the base as one that could not be asked, which keeps what it listed last time. The rule lives in catalog.Paged, and the Kilo gateway's list — OpenRouter's shape, which pages the same way — is paged by it instead of refused by a guard of its own; the comment on that guard claimed OpenRouter's /models pages, which neither api.kilo.ai nor openrouter.ai was answering with on 2026-10-06, and now says so. One assertion of TestFetchOpenRouterRuleOverTheFourBaseCombinations follows the rule rather than the old count: OpenRouter's Anthropic base alone is asked for its first page and the page after it, which is the same page again, where before it was asked once and refused. Tests: TestFetchFollowsThePagesOfAList, TestFetchFollowsAPageAListAnswersItsOwnWay, TestFetchTakesAPagedListAsNoAnswerWhenAPageFails, TestFetchTakesAPagedListWithoutAnIdAsNoAnswer, TestFetchTakesAListThatPagesWithoutEndAsNoAnswer (catalog); TestFetchFollowsThePagesOfASingleAnthropicBase (provider, the Anthropic preset's one base, 25 models 20 to a page) and TestKiloModelsFollowThePagesOfTheList. Co-authored-by: multica-agent <github@multica.ai> * provider: OpenRouter's Anthropic base alone is asked, and refused (#1006) The fake that stands for openrouter.ai answered every cursor with the same twenty ids under the same last_id, so following the pages of a list stopped where the rule of catalog.Paged says a list stops being readable, and the single-base case looked covered: the twenty were refused and the fetch failed. The endpoint does not answer that way. Asked with the Anthropic version header on 2026-10-06, /api/v1/models gave those twenty with has_more and a cursor, and under the cursor 413 more ids with has_more false — a different set, and the end of the list. The two pages share nothing, and following them lands deeper in the `anthropic/` namespace rather than back at the catalog it was namespaced from: of the 433 ids, 4 are in the 466 OpenRouter serves without that header. So the twenty were not a page to be refused but the first half of a list that reads to the end, and the end is still not a catalog of models to serve. The base is still asked — it is the only base there is, and refusing to ask would leave the provider with nothing at all — and the list it answers with is now refused as a whole, which is what the old rule did with one page of it. It fails the fetch like any other base that could not be asked, so the provider keeps the models it listed last time (#904). The two replies are kept as they came, under testdata, and a test reads them back: twenty, then 413, the cursor the first ends at, has_more true and then false, and not one id of either page in the catalog OpenRouter serves. The fake pages with after_id, as the endpoint does. Tests: TestOpenRouterNamespacedPagesAreTheEndpointShape (new), TestFetchOpenRouterRuleOverTheFourBaseCombinations (the single-base case now asks exactly the two pages it answers with, keeps no list, and fails), TestFetchKeepsPicksWhenAModelListIsPaginated, TestFetchDoesNotMergeOpenRouter's NamespacedList, TestFetchOpenRouterAddsNoNamespacedModelAndKeepsPicks, TestFetchKeepsAnotherVendorsAnthropicList. Co-authored-by: multica-agent <github@multica.ai> * provider: the message for a namespaced list counts nothing (#1006) The reason a provider whose only base is OpenRouter's Anthropic one is refused is still the one it was, and the base is still asked and refused exactly as before: what changed is the sentence the user is left with, and the comments around it. That sentence counted the list it refused — "433 of them, of which 4 are ids OpenRouter serves" — and those counts are of whatever OpenRouter's catalog was that day, so the number a user reads is wrong tomorrow without anyone touching it. It now says why the answer is no good and what to do instead: give this provider OpenRouter's Chat base, which lists those models under the ids OpenRouter serves, or type the model ids in by hand, which is what the caller already appends to every base it could not ask. The comments said the same counts as fact, and cited a probe file that is not in the repository. The counts stay, marked as what was observed on 2026-10-06, and what does not move is now the point: following the pages of the list lands deeper in the namespace, not back at the catalog it was namespaced from. The dead file references are gone, and one test checks that the message counts nothing at all, so a count cannot creep back in. Tests: TestFetchOpenRouterRuleOverTheFourBaseCombinations (the message names the Chat base and the ids to type, and holds no number but the version in that base's path). Co-authored-by: multica-agent <github@multica.ai> * provider: a multi-protocol provider's catalog is the union of every endpoint's models A provider whose protocols sit at bases of their own kept only the first list that answered, so the models of every other endpoint were nowhere in the product: not in the agents' model lists, not in the catalog the routing and the per-model preferences read, and not fixable by pinning a model's protocol, which setModelAPI refuses for a model the provider does not list. nyownm reported it as #904 and worked around it with one provider per protocol. fetchOne now asks every endpoint the provider speaks and merges the lists by model id, the way fetchPerKey merges the lists of a vendor's keys: the first list's fields stand, and an image capability both lists answer is merged. The base kept is still the first protocol's, the one the drawer list is fetched beside and the models are saved with. A models URL is still asked alone, one base two protocols share is still asked once, and a provider with no endpoint answering still fails with every URL it asked named in the error. Verified with go test -tags nogui ./internal/provider ./internal/catalog and ./internal/gateway ./internal/agent, all green. TestFetchUnionsProtocolLists and TestFetchUnionsProtocolListsWithoutChat fail on the base commit with the single list the first protocol answered. Co-authored-by: multica-agent <github@multica.ai> * provider: an endpoint that can't be asked keeps the models it listed A provider whose protocols sit at bases of their own is asked every base and the lists are merged, so one read that failed saved the union of the rest as the whole list: the models of the failed base read as gone, dropGonePicks took them out of the user's picks and stored that, and nothing brought them back when the endpoint recovered (yetone's review of vendor serves today, the rule fetchPerKey's keys already follow. fetchOne keeps what each base listed and saves it beside the merged list, in a new "sides" part of the catalog's live file, and a base it could not ask takes its own part back for that fetch. What is kept is that base's own models alone: a model the bases that answered have dropped is dropped here too, however many of them dropped it and however many bases are down. Keeping the whole list saved before would bring such a model back, which is the same fault the other way round. A list saved before this kept no parts, so a base whose part is missing keeps what no answering base lists, until the next fetch with every base answering says which they are. The parts are kept only where more than one base was asked: a single one that fails is the fetch that fails, and its list is kept as it is, so a provider of one protocol's cache is the file it was. fetchOne answers one fetched list now — the models, the base kept for routing, the parts — rather than three values. fetchPerKey marks the parts with the keys that saw them, and keeps a plan's own models out of them. Verified with go test ./internal/provider ./internal/catalog, all green, and ./internal/gateway ./internal/agent. TestFetchKeepsTheModelsAFailedProtocolListed, TestFetchDropsAGoneModelWhileAnotherProtocolFails and TestFetchKeepsTheModelsAFailedProtocolListedPerKey fail on 952d0b00 with the model of the failed protocol taken out of the picks. Co-authored-by: multica-agent <github@multica.ai> * provider: a base that never answered keeps nothing, and is not saved as one that listed nothing keepDeadSides read a part missing from the saved sides as the whole list saved before, and kept what no answering base lists from it. Two things were wrong with that, and both were reported on #1006 (nyownm's #904): A base in none of the saved parts is one that has never answered. The whole list is not its own, so a model its vendor had already dropped was kept for it, and the keepDeadSides result was saved as sides[base] — from then on that base had a part, and the model could never be dropped again. An Anthropic base answering 404 from the start of a provider's life was enough: one round after the Chat base dropped a model, that model stayed in the catalog, and dropGonePicks could not take it out of the user's picks. The saving is the other half of the same fault. A part is what a base listed, so one that never answered has none, and writing an empty one fixes the reading of "never answered" as "listed nothing" for good. Only a part read out of the file is written back; the list saved without any parts at all — before this kept them — is the one case where a base may stand in for a missing part, and what it stands in for is kept for that fetch alone, never saved as its own. So keepDeadSides answers two lists now: the models this fetch's list is made of, and the parts to write beside it. fetchPerKey keeps a model a key that could not be asked at all owns to the bases that listed it, read out of the parts saved beside the list: kept as a whole list with no part saying where it came from it belonged to no base, and was read as gone the first time that key answered half-way — the same permanent deletion of a picked model, one round later and needing no new base. Verified with go test ./internal/provider ./internal/catalog ./internal/gateway ./internal/agent, all green, and -race -count=3 over ./internal/provider with the four failures it already had. TestFetchDropsAModelWhileAnEndpointHasNeverAnswered and TestFetchKeepsAModelsBaseAfterItsKeyFailedWhole fail on 6621f73e. Co-authored-by: multica-agent <github@multica.ai> * provider: a base that answered without a model does not keep owning it fetchPerKey gave every model kept for a key that could not be asked at all to the bases that listed it last time, and a part there said what the bases listed when one of them had answered this round without it (yetone's review of #1006, nyownm's #904). With two keys where both were served a Claude model, and the Anthropic base stopping both of them in the round the second key was down, sides[anthropic] kept a model the base had plainly dropped, and the fetch after that read it as a base that couldn't be asked and held the model one round longer than it should have. It is self-healing — the next round with the base answering settles it — and it is the same fault c8fabd70 was written to undo, read from the other side. sides may only say what the bases listed. A model kept for a failed key belongs to a base that was not asked this round for any of the model's own keys: one that was, and answered without the model, has said what it serves today and no longer owns it. Which is per key, not per provider: fetchOne answers which of its bases replied, which a model of the second key alone never saw an answer from, so the base's saying nothing about it for the first key leaves it that base's. The keys a model is kept under are read from the list saved before, since add clears the ones of the model it is adding. TestFetchDropsAModelABaseStoppedServingWhileAKeyIsDown fails on c8fabd70 with the part holding claude-b, and TestFetchKeepsAModelsBaseAfterItsKeyFailedWhole goes on passing: the same two keys, a model only one of them sees. Co-authored-by: multica-agent <github@multica.ai> * provider, catalog: a page of a model list is not the list (#904) A /v1/models that answers with has_more is one page of the vendor's catalog. Read as the whole list, every model of a later page reads as one the vendor has dropped, and the user's pick of it goes for good, on a list that never said so. Such a reply is no answer: the base is treated as one that could not be asked, which keeps what it listed last time (#904's review of #1006). OpenRouter's Anthropic base is not asked for the list at all. With the Anthropic version header its /models answers with the catalog again, twenty newest first, every id namespaced under `anthropic/`, and the cursors of the pages after. None of those twenty is an id in OpenRouter's own catalog, so merged they are entries of the model picker that name no model: the long, cluttered list #904's reporter wrote of. Its Chat base is that catalog, whole, and the fetch is one request where it was two. The rule is OpenRouter's, by host, as the ones after it are scoped to their vendors. Co-authored-by: multica-agent <github@multica.ai> * provider: the OpenRouter rule reads the base being asked (0fb16990) It read the Chat base's host, so a provider with a Chat base of OpenRouter's and an Anthropic base of another vendor's had that other base skipped: its models were in neither the union nor the sides nor the bases that could not be asked, so no keepDeadSides path found them and the user's picks of them went for good. `magpie provider set <id> anthropic=<url>` reaches that shape from the CLI. The rule is about the vendor whose base is being asked, so that is what it now reads, together with whether the provider asks at a base of its own besides it: an OpenRouter Anthropic base alone is still asked, its reply being the only catalog there would otherwise be, and catalog.fetchOne is what keeps a page of it from being read as the whole. Five combinations of the two bases and openrouter.ai, one assertion each; three of them fail on 0fb16990. Co-authored-by: multica-agent <github@multica.ai> * catalog, provider: a model list is followed to its last page, not read as a page (#1006) A reply with has_more was refused as no answer at all, which is right for a provider with several bases and wrong for one with a single base: Anthropic's own preset has no other base to fall back on, so once its list passes twenty models — about sixteen today — the fetch answered an error and no models, where main listed the twenty of the first page, and a pick of a model on a later page went with them. catalog.fetchOne now reads a list to its end: the first page as the URL stands, and every page after it with limit=1000 and the after_id the page before ended at, up to twenty pages. Half a list is still no answer, and that is what the following ends on: a page that fails, a list that pages without naming the id to carry on from, one that answers the page just asked again under the same id, and one still paging at the cap all leave the base as one that could not be asked, which keeps what it listed last time. The rule lives in catalog.Paged, and the Kilo gateway's list — OpenRouter's shape, which pages the same way — is paged by it instead of refused by a guard of its own; the comment on that guard claimed OpenRouter's /models pages, which neither api.kilo.ai nor openrouter.ai was answering with on 2026-10-06, and now says so. One assertion of TestFetchOpenRouterRuleOverTheFourBaseCombinations follows the rule rather than the old count: OpenRouter's Anthropic base alone is asked for its first page and the page after it, which is the same page again, where before it was asked once and refused. Tests: TestFetchFollowsThePagesOfAList, TestFetchFollowsAPageAListAnswersItsOwnWay, TestFetchTakesAPagedListAsNoAnswerWhenAPageFails, TestFetchTakesAPagedListWithoutAnIdAsNoAnswer, TestFetchTakesAListThatPagesWithoutEndAsNoAnswer (catalog); TestFetchFollowsThePagesOfASingleAnthropicBase (provider, the Anthropic preset's one base, 25 models 20 to a page) and TestKiloModelsFollowThePagesOfTheList. Co-authored-by: multica-agent <github@multica.ai> * provider: OpenRouter's Anthropic base alone is asked, and refused (#1006) The fake that stands for openrouter.ai answered every cursor with the same twenty ids under the same last_id, so following the pages of a list stopped where the rule of catalog.Paged says a list stops being readable, and the single-base case looked covered: the twenty were refused and the fetch failed. The endpoint does not answer that way. Asked with the Anthropic version header on 2026-10-06, /api/v1/models gave those twenty with has_more and a cursor, and under the cursor 413 more ids with has_more false — a different set, and the end of the list. The two pages share nothing, and following them lands deeper in the `anthropic/` namespace rather than back at the catalog it was namespaced from: of the 433 ids, 4 are in the 466 OpenRouter serves without that header. So the twenty were not a page to be refused but the first half of a list that reads to the end, and the end is still not a catalog of models to serve. The base is still asked — it is the only base there is, and refusing to ask would leave the provider with nothing at all — and the list it answers with is now refused as a whole, which is what the old rule did with one page of it. It fails the fetch like any other base that could not be asked, so the provider keeps the models it listed last time (#904). The two replies are kept as they came, under testdata, and a test reads them back: twenty, then 413, the cursor the first ends at, has_more true and then false, and not one id of either page in the catalog OpenRouter serves. The fake pages with after_id, as the endpoint does. Tests: TestOpenRouterNamespacedPagesAreTheEndpointShape (new), TestFetchOpenRouterRuleOverTheFourBaseCombinations (the single-base case now asks exactly the two pages it answers with, keeps no list, and fails), TestFetchKeepsPicksWhenAModelListIsPaginated, TestFetchDoesNotMergeOpenRouter's NamespacedList, TestFetchOpenRouterAddsNoNamespacedModelAndKeepsPicks, TestFetchKeepsAnotherVendorsAnthropicList. Co-authored-by: multica-agent <github@multica.ai> * provider: the message for a namespaced list counts nothing (#1006) The reason a provider whose only base is OpenRouter's Anthropic one is refused is still the one it was, and the base is still asked and refused exactly as before: what changed is the sentence the user is left with, and the comments around it. That sentence counted the list it refused — "433 of them, of which 4 are ids OpenRouter serves" — and those counts are of whatever OpenRouter's catalog was that day, so the number a user reads is wrong tomorrow without anyone touching it. It now says why the answer is no good and what to do instead: give this provider OpenRouter's Chat base, which lists those models under the ids OpenRouter serves, or type the model ids in by hand, which is what the caller already appends to every base it could not ask. The comments said the same counts as fact, and cited a probe file that is not in the repository. The counts stay, marked as what was observed on 2026-10-06, and what does not move is now the point: following the pages of the list lands deeper in the namespace, not back at the catalog it was namespaced from. The dead file references are gone, and one test checks that the message counts nothing at all, so a count cannot creep back in. Tests: TestFetchOpenRouterRuleOverTheFourBaseCombinations (the message names the Chat base and the ids to type, and holds no number but the version in that base's path). Co-authored-by: multica-agent <github@multica.ai> * provider: the list of a multi-protocol vendor is asked once (#1006) The dedup added in 952d0b00 is keyed by the base URL as it is configured, which is neither of the two things that decide whether a list is asked again: which list URL the request goes to, and what it authenticates with. Both of a vendor's ends were lost to it. A Chat base and the Anthropic base of the same remote magpie are two strings, one list — GET /v1/models — and it was fetched twice, which #1282 says a remote's test does not do. And one base string serving Chat, Anthropic and Gemini is three requests: the Chat request refused a 401 took the other two for lists already had, so a vendor whose models were all there was reported as serving none. The key is now the list URL and the authentication, in catalog.Listed, and it gives two rules. A list URL this key has listed is not asked again by any protocol — the answer is the answer, so …/v1 and the bare host beside it are one request. And a request already made is not made again, but a URL refused under one authentication is not refused under the next, so the Gemini and Anthropic lists of a base whose Chat request was refused are asked, each with its own. The base string is left out of the key on purpose: Chat and Responses at one base are one request because both send Bearer at the same URL, not because the strings match. A base whose every list was asked already and none answered says so with catalog.ErrAsked, which the caller reads as "nothing new here" rather than as a second refusal to put in the message the user reads. Tests: TestFetchAsksEachAuthenticationAtOneBase, TestFetchAsksOneListOnce ForTwoBasesOfIt, TestFetchReportsOneRefusalForOneBaseAskedTwice. Co-authored-by: multica-agent <github@multica.ai> * provider: a Gemini base that fails once no longer deletes the picked model it listed The union fetch merges a Gemini base's list like any other protocol's, but it kept none of the fetch's bookkeeping for it: what it listed was in no `side`, so the parts saved beside the catalog said no base listed it, and a base that could not be asked was in no `dead` either, so `keepDeadSides` never saw it. A picked Gemini model was therefore dropped from the user's provider for good the first time the Gemini endpoint answered 502 or timed out — the fault the Anthropic base of the same fetch was fixed for in #904. A Gemini base also went uncounted in `asked`, so a two-base provider's parts were saved on the strength of one base asked. A Gemini base now joins `asked` before it is asked, joins `sides` and `answered` with what it listed, and joins `dead` when it cannot be asked. What a failed Gemini base keeps is its own models alone, so a model its list has really dropped is still dropped from the picks. Verified at 20ee273aaedc10ab0baf06cffbd826ddc367be3c + this commit, Go 1.26.8, darwin/arm64, under a temporary HOME with GOPATH, GOMODCACHE and GOCACHE pinned. Without the change (internal/provider/models.go put back): go test ./internal/provider -run 'TestFetchKeepsTheModelsAFailedGeminiBaseListed|TestFetchDropsAGoneGeminiModelWhileAnotherProtocolFails' multiproto_models_test.go:713: picks after the Gemini base failed: [gpt-a], want [gpt-a claude-b] multiproto_models_test.go:824: picks after the Gemini list dropped a model: [gpt-a gpt-b claude-b claude-c], want [gpt-a gpt-b claude-b] FAIL (exit 1) With it: go test ./internal/provider -run 'TestFetchKeepsTheModelsAFailedGeminiBaseListed|TestFetchDropsAGoneGeminiModelWhileAnotherProtocolFails' ok (exit 0) go test -tags nogui ./internal/provider -count=1 ok github.com/yetone/magpie/internal/provider 109.187s (exit 0) gofmt -l internal/provider/models.go internal/provider/multiproto_models_test.go (prints nothing) go vet ./internal/provider (exit 0) GOOS=linux go vet -tags nogui ./... (exit 0) GOOS=windows go vet ./... (exit 0) go build ./... (exit 0) GOOS=linux go build -tags nogui ./... (exit 0) GOOS=windows go build ./... (exit 0) The maintainer's own reproduction of the first failure, run on 20ee273a before anything was changed, is the same one line and exits 1: --- FAIL: TestZZGeminiSideKept (0.04s) zz_gem_test.go:81: picks after a protocol failed: [gpt-a], want [gpt-a claude-b] The whole-repository `go test -tags nogui ./...` was not run: the change touches only internal/provider, and that package's full suite is green. Co-authored-by: multica-agent <github@multica.ai> --------- Co-authored-by: multica-agent <github@multica.ai>
348 lines
10 KiB
Go
348 lines
10 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/url"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
)
|
|
|
|
// Which APIs a vendor or relay serves behind the one URL the user typed is
|
|
// found by asking (01huadalang on Discord: 一键检测支持什么协议,发一个最小
|
|
// 请求看是否返回200): each of the three magpie speaks upstream — Chat
|
|
// Completions, Responses and Anthropic Messages — is sent the smallest
|
|
// request Test sends, at that URL as the API takes it, and what answered
|
|
// is what the provider can be given. Since a custom provider can keep a
|
|
// Gemini API's URL (#1346), Gemini's generateContent is asked too, at the
|
|
// …/v1beta a relay serves it under (NagaseMinato: 「检测」不会探测 Gemini
|
|
// 端点,为什么呢?).
|
|
|
|
// detectProtocols are the APIs a detection asks, in the order it says
|
|
// what they answered: magpie's three, then Gemini's.
|
|
var detectProtocols = []Protocol{Chat, Responses, Anthropic, Gemini}
|
|
|
|
// Detection is what one API answered at the URL it was asked at.
|
|
type Detection struct {
|
|
Result
|
|
// Base is the URL the provider keeps for it, as the editor's fields
|
|
// take it: …/v1 for OpenAI's two, the root for Anthropic's, …/v1beta
|
|
// for Gemini's.
|
|
Base string `json:"base"`
|
|
}
|
|
|
|
// DetectBase is base as proto takes it: an endpoint pasted whole (…/v1/chat/
|
|
// completions, …/models/gemini-2.5-pro:generateContent) cut to its base, then the root for Anthropic, which adds
|
|
// /v1/messages itself, …/v1 for OpenAI's two when base is a bare host, and
|
|
// for Gemini's the …/v1beta beside a …/v1 or on a bare host (GeminiBase) —
|
|
// the editor's respellURL, in Go.
|
|
func DetectBase(base string, proto Protocol) string {
|
|
u := strings.TrimRight(strings.TrimSpace(base), "/")
|
|
for _, end := range []string{"/chat/completions", "/responses", "/v1/messages", "/messages"} {
|
|
u = strings.TrimSuffix(u, end)
|
|
}
|
|
// a Gemini endpoint or a model list pasted whole
|
|
u = strings.TrimRight(geminiMethod.ReplaceAllString(u, ""), "/")
|
|
if u == "" {
|
|
return ""
|
|
}
|
|
switch proto {
|
|
case Anthropic:
|
|
return strings.TrimSuffix(u, "/v1")
|
|
case Gemini:
|
|
if pu, err := url.Parse(u); err == nil && pu.Host != "" && strings.Trim(pu.Path, "/") == "v1" {
|
|
return u + "beta"
|
|
}
|
|
return GeminiBase(u)
|
|
}
|
|
if pu, err := url.Parse(u); err == nil && strings.Trim(pu.Path, "/") == "" {
|
|
return u + "/v1"
|
|
}
|
|
return u
|
|
}
|
|
|
|
// ErrNoURL is a detection with no URL to ask at.
|
|
var ErrNoURL = errors.New("type the base URL first")
|
|
|
|
// Detect asks each of Chat, Responses, Anthropic and Gemini at the URL p has for
|
|
// it, else at base as it takes it (DetectBase), the smallest request, and
|
|
// says what each answered, in that order. model is asked on all three;
|
|
// with none, one is picked for each — a Claude model on Anthropic's, an
|
|
// OpenAI one on Responses — from picked, the models the editor's form has
|
|
// and hasn't saved yet (#1545: a relay with no model list is asked for
|
|
// the models typed in the form, not only after a Save), else from p's
|
|
// list, and with no list the vendor is asked for one first (a GET of its
|
|
// models, which costs nothing). Only those URLs are asked, and only a
|
|
// key's provider: a sign-in's API is its agent's.
|
|
func (p Provider) Detect(ctx context.Context, base, model string, picked ...string) ([]Detection, error) {
|
|
q, err := p.detecting(base)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ctx = q.Via(ctx)
|
|
model = strings.TrimSpace(model)
|
|
var ids []string
|
|
if model == "" {
|
|
for _, m := range picked {
|
|
if m = strings.TrimSpace(m); m != "" && !slices.Contains(ids, m) {
|
|
ids = append(ids, m)
|
|
}
|
|
}
|
|
if len(ids) == 0 {
|
|
ids = q.detectModels(ctx)
|
|
}
|
|
}
|
|
out := make([]Detection, len(detectProtocols))
|
|
var wg sync.WaitGroup
|
|
for i, proto := range detectProtocols {
|
|
m := model
|
|
if m == "" {
|
|
m = detectModel(ids, proto)
|
|
}
|
|
var ask func(context.Context) Result
|
|
out[i], ask = q.detectOne(proto, m, testWait)
|
|
if ask == nil {
|
|
continue
|
|
}
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
out[i].Result = ask(ctx)
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
return out, nil
|
|
}
|
|
|
|
// detecting is p with the URL it would be asked at for each API: its own,
|
|
// else base as that API takes it.
|
|
func (p Provider) detecting(base string) (Provider, error) {
|
|
if p.Account != nil {
|
|
return p, errors.New("a sign-in is asked on its agent's own API; there is nothing to detect")
|
|
}
|
|
q := p
|
|
for _, proto := range detectProtocols {
|
|
u := strings.TrimSpace(p.Base(proto))
|
|
if u == "" {
|
|
u = DetectBase(base, proto)
|
|
}
|
|
if u != "" {
|
|
if pu, err := url.Parse(u); err != nil || (pu.Scheme != "http" && pu.Scheme != "https") || pu.Host == "" {
|
|
return p, errors.New("the base URL must start with http:// or https://, not " + u)
|
|
}
|
|
}
|
|
switch proto {
|
|
case Chat:
|
|
q.Chat = u
|
|
case Responses:
|
|
q.Responses = u
|
|
case Anthropic:
|
|
q.Anthropic = u
|
|
case Gemini:
|
|
q.Gemini = u
|
|
}
|
|
}
|
|
if len(q.Speaks()) == 0 {
|
|
return p, ErrNoURL
|
|
}
|
|
return q, nil
|
|
}
|
|
|
|
// detectOne is model's detection on proto as far as it can be told
|
|
// without asking, and, when it can be asked, the ask (nil when not).
|
|
func (q Provider) detectOne(proto Protocol, m string, wait time.Duration) (Detection, func(context.Context) Result) {
|
|
d := Detection{Result: Result{Protocol: proto, Model: m}, Base: q.Base(proto)}
|
|
if d.Base == "" {
|
|
d.Error = "no URL to ask"
|
|
return d, nil
|
|
}
|
|
if m == "" {
|
|
d.Error = "no model to try: type one the vendor serves"
|
|
return d, nil
|
|
}
|
|
k, ok := q.keyFor(proto)
|
|
if !ok {
|
|
d.Error = "no key is on for this endpoint"
|
|
return d, nil
|
|
}
|
|
return d, func(ctx context.Context) Result {
|
|
u, body := tiny(k, proto, UpstreamName(q, m))
|
|
return probe(ctx, k, proto, u, k.tested(body), m, wait)
|
|
}
|
|
}
|
|
|
|
// DetectDecide asks the System One API at p's decision URL (…/systemone
|
|
// pasted whole is cut to its root) the smallest question for model, or
|
|
// the one p is asked with when none is given, and says how it answered.
|
|
func (p Provider) DetectDecide(ctx context.Context, model string) Detection {
|
|
p.Decide = strings.TrimSuffix(strings.TrimRight(strings.TrimSpace(p.Decide), "/"), "/systemone")
|
|
if p.Decide != "" && !strings.Contains(p.Decide, "://") {
|
|
p.Decide = "https://" + p.Decide
|
|
}
|
|
if model = strings.TrimSpace(model); model == "" {
|
|
model = p.Jev()
|
|
}
|
|
d := Detection{Result: Result{Protocol: "decide", Model: model}, Base: p.Decide}
|
|
switch {
|
|
case p.Decide == "":
|
|
d.Error = "no URL to ask"
|
|
return d
|
|
case strings.Contains(p.Decide, WorkspaceID):
|
|
d.Error = "give the workspace ID first"
|
|
return d
|
|
}
|
|
t0 := time.Now()
|
|
err := p.AskSystemOne(p.Via(ctx), model)
|
|
d.Millis = time.Since(t0).Milliseconds()
|
|
if err != nil {
|
|
d.Error = err.Error()
|
|
return d
|
|
}
|
|
d.OK, d.Status = true, 200
|
|
return d
|
|
}
|
|
|
|
// ModelDetection is what each API answered for one model
|
|
// (01huadalang on Discord: 应该能看出来选择的模型支持情况…有的仅支持
|
|
// response 有的双协议).
|
|
type ModelDetection struct {
|
|
Model string `json:"model"`
|
|
Results []Detection `json:"results"`
|
|
}
|
|
|
|
// A detection of many models asks at most DetectMax of them, DetectWide
|
|
// requests at a time, each waiting detectWait for its answer.
|
|
const (
|
|
DetectMax = 30
|
|
DetectWide = 8
|
|
detectWait = 12 * time.Second
|
|
)
|
|
|
|
// DetectModels asks each of models (the first DetectMax, an image model
|
|
// left out: it is asked on the images API) on each of Chat, Responses,
|
|
// Anthropic and Gemini at the URL p has for it, else at base as it takes it, and
|
|
// says what each answered, model by model; and, by API, the first answer
|
|
// from a model it served, else the first model's — what Detect says for
|
|
// one. A probe not started before ctx ends says so, unasked.
|
|
func (p Provider) DetectModels(ctx context.Context, base string, models []string) ([]ModelDetection, []Detection, error) {
|
|
q, err := p.detecting(base)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
var ids []string
|
|
for _, m := range models {
|
|
if m = strings.TrimSpace(m); m != "" && !slices.Contains(ids, m) {
|
|
ids = append(ids, m)
|
|
}
|
|
}
|
|
if len(ids) == 0 {
|
|
return nil, nil, errors.New("no model to try: type one the vendor serves")
|
|
}
|
|
if len(ids) > DetectMax {
|
|
ids = ids[:DetectMax]
|
|
}
|
|
ctx = q.Via(ctx)
|
|
out := make([]ModelDetection, len(ids))
|
|
sem := make(chan struct{}, DetectWide)
|
|
var wg sync.WaitGroup
|
|
for i, m := range ids {
|
|
out[i] = ModelDetection{Model: m, Results: make([]Detection, len(detectProtocols))}
|
|
for j, proto := range detectProtocols {
|
|
d, ask := q.detectOne(proto, m, detectWait)
|
|
if ask != nil && catalog.ImagesAPI(m) {
|
|
d.Error, ask = "an image model: asked on the images API, not here", nil
|
|
}
|
|
out[i].Results[j] = d
|
|
if ask == nil {
|
|
continue
|
|
}
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
select {
|
|
case sem <- struct{}{}:
|
|
case <-ctx.Done():
|
|
out[i].Results[j].Error = "not asked: out of time"
|
|
return
|
|
}
|
|
defer func() { <-sem }()
|
|
if ctx.Err() != nil {
|
|
out[i].Results[j].Error = "not asked: out of time"
|
|
return
|
|
}
|
|
out[i].Results[j].Result = ask(ctx)
|
|
}()
|
|
}
|
|
}
|
|
wg.Wait()
|
|
sum := make([]Detection, len(detectProtocols))
|
|
for j := range detectProtocols {
|
|
sum[j] = out[0].Results[j]
|
|
for _, md := range out {
|
|
if md.Results[j].OK {
|
|
sum[j] = md.Results[j]
|
|
break
|
|
}
|
|
}
|
|
}
|
|
return out, sum, nil
|
|
}
|
|
|
|
// detectModels are the models a detection may ask for: those p exposes and
|
|
// lists, else the vendor's list, asked for now and not kept.
|
|
func (p Provider) detectModels(ctx context.Context) []string {
|
|
var ids []string
|
|
if p.ID != "" {
|
|
for _, ms := range [][]catalog.Model{p.Exposed(), p.Available()} {
|
|
for _, m := range ms {
|
|
if !slices.Contains(ids, m.ID) {
|
|
ids = append(ids, m.ID)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if len(ids) > 0 {
|
|
return ids
|
|
}
|
|
l, err := p.fetchOne(ctx)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
for _, m := range l.models {
|
|
ids = append(ids, m.ID)
|
|
}
|
|
return ids
|
|
}
|
|
|
|
// detectModel is the model of ids to ask proto for: one made for it — a
|
|
// Claude model on Anthropic's, an OpenAI one on Responses, a Gemini one on
|
|
// Gemini's, any other on Chat — else the first that chats.
|
|
func detectModel(ids []string, proto Protocol) string {
|
|
made := func(id string) bool {
|
|
switch proto {
|
|
case Anthropic:
|
|
return isClaude(id)
|
|
case Responses:
|
|
return openAIModel(id)
|
|
case Gemini:
|
|
return strings.Contains(strings.ToLower(id), "gemini")
|
|
}
|
|
return !isClaude(id)
|
|
}
|
|
for _, want := range []func(string) bool{made, func(string) bool { return true }} {
|
|
for _, id := range ids {
|
|
if want(id) && !catalog.ImagesAPI(id) {
|
|
return id
|
|
}
|
|
}
|
|
}
|
|
if len(ids) > 0 {
|
|
return ids[0]
|
|
}
|
|
return ""
|
|
}
|