Files
magpie/internal/catalog/live.go
T
Junjie Zhouandmultica-agent 12d823211e provider: a multi-protocol provider's model catalog keeps every endpoint's models (#1006)
* 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>
2026-10-13 13:42:56 +08:00

824 lines
27 KiB
Go

package catalog
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"math"
"net/http"
"net/url"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"time"
"github.com/yetone/magpie/internal/filememo"
)
// A vendor's own /models endpoint is the truth about what it serves today;
// models.dev lags and keeps legacy names around. magpie asks the vendor when
// it has a key, remembers the answer next to the models.dev cache, and lets
// the catalog fill in display names and reasoning levels.
// LivePath is where the fetched model list of one provider is kept.
func LivePath(provider string) string {
return filepath.Join(filepath.Dir(CachePath()), "models", provider+".json")
}
type liveFile struct {
Fetched time.Time `json:"fetched"`
Base string `json:"base"`
Models []Model `json:"models"`
// Sides, for a provider whose protocols are served at bases of their
// own, is what each of those bases listed, before the lists were
// merged into Models. A base that can't be asked keeps its own part
// from the list saved before (#904).
Sides map[string][]Model `json:"sides,omitempty"`
}
func readLive(provider string) (liveFile, error) {
return filememo.Read("live models", LivePath(provider), func(b []byte) (liveFile, error) {
var f liveFile
err := json.Unmarshal(b, &f)
return f, err
})
}
// Live returns the model list last fetched from the provider, if any: the
// models to talk to, without the ones that draw (LiveDrawers).
func Live(provider string) (models []Model, fetched time.Time, ok bool) {
f, err := readLive(provider)
if err != nil {
return nil, time.Time{}, false
}
ms := Chat(f.Models)
if len(ms) == 0 {
return nil, time.Time{}, false
}
return ms, f.Fetched, true
}
// LiveAll is the whole model list last fetched from the provider, the
// models that draw or make videos among them, with each one's keys: what
// tells which of the provider's keys serves an image model (#1657), which
// Live, the models to talk to, leaves out.
func LiveAll(provider string) (models []Model, ok bool) {
f, err := readLive(provider)
if err != nil || len(f.Models) == 0 {
return nil, false
}
return f.Models, true
}
// LiveSplit returns the provider's fetched list as every endpoint's own part
// of it: sides[base] is what that endpoint listed, and all is the whole list
// those parts were merged into. ok is false where there is no list at all;
// sides is nil for a list saved without its parts, where every endpoint
// answered alike.
func LiveSplit(provider string) (sides map[string][]Model, all []Model, ok bool) {
f, err := readLive(provider)
if err != nil || len(f.Models) == 0 {
return nil, nil, false
}
return f.Sides, f.Models, true
}
// LiveDrawers are the models in the provider's fetched list that make
// images, in the list's order.
func LiveDrawers(provider string) []Model {
f, err := readLive(provider)
if err != nil {
return nil
}
var out []Model
for _, m := range f.Models {
if m.Draws {
out = append(out, m)
}
}
return out
}
// LiveVideomakers are the models in the provider's fetched list that make
// videos, in the list's order.
func LiveVideomakers(provider string) []Model {
f, err := readLive(provider)
if err != nil {
return nil
}
var out []Model
for _, m := range f.Models {
if m.Films {
out = append(out, m)
}
}
return out
}
// Chat is ms without the models that draw or make videos.
func Chat(ms []Model) []Model {
out := make([]Model, 0, len(ms))
for _, m := range ms {
if !m.Draws && !m.Films {
out = append(out, m)
}
}
return out
}
// SaveLive stores a fetched list; an empty list forgets it.
func SaveLive(provider, base string, models []Model) error {
return SaveLiveSides(provider, base, models, nil)
}
// SaveLiveSides is SaveLive, keeping what each endpoint answered with beside
// the merged list (LiveSplit): a provider whose protocols sit at bases of
// their own asks every base, and a base that can't be asked next time has to
// know which of the models were its own to keep (#904).
func SaveLiveSides(provider, base string, models []Model, sides map[string][]Model) error {
p := LivePath(provider)
if len(models) == 0 {
err := os.Remove(p)
if errors.Is(err, os.ErrNotExist) {
return nil
}
if err == nil {
Touched()
}
return err
}
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
return err
}
b, _ := json.MarshalIndent(liveFile{Fetched: time.Now(), Base: base, Models: models, Sides: sides}, "", " ")
if err := os.WriteFile(p, b, 0o644); err != nil {
return err
}
Touched()
return nil
}
// Changed, when set, is told that the models magpie offers may be others
// now: a provider added, edited or removed, a vendor's list fetched anew.
// The agent package sets it, to bring the model lists agents keep in files
// of their own up to date.
var Changed func()
// Forget, when set, is told so first: the provider package drops the
// catalog a request holds (provider.Hold).
var Forget func()
// Touched tells Forget and Changed, if set.
func Touched() {
if Forget != nil {
Forget()
}
if Changed != nil {
Changed()
}
}
// Fetch asks an endpoint for its models. base is an API base URL of any
// flavour (…/v1, …/anthropic, …/api); the usual list paths are tried
// around it. The result keeps the server's order. anthropic sends the
// Anthropic version header, which the official API requires but which a
// dual-protocol relay like OpenRouter reads as a request for its
// Anthropic-flavoured catalog — namespaced, differently named ids.
func Fetch(ctx context.Context, base, key string, anthropic bool, headers map[string]string) ([]Model, error) {
ms, _, err := FetchAt(ctx, base, key, anthropic, headers)
return ms, err
}
// FetchAt is Fetch, and says which URL answered. An OpenAI-style base
// with a version in its path (…/v1, …/api/plan/v3, …/api/paas/v4,
// …/v1beta/openai) names the vendor's API as it is and is asked only as
// written, at base/models: a /v1 is looked for only around a base without
// one (a bare host, a relay's …/api), where it may have been left out. An
// Anthropic base is the root /v1/messages is asked at, so /v1/models
// under it is its own list. When no URL answers, the error names each one
// asked and what it said.
func FetchAt(ctx context.Context, base, key string, anthropic bool, headers map[string]string) ([]Model, string, error) {
return FetchAtListed(ctx, base, key, anthropic, headers, nil)
}
// ErrAsked is what FetchAtListed says when every list URL of the base was
// already asked for this key and none of them answered: there is nothing
// left to ask there, which is not a refusal of the base and is not
// reported as one to the user. A caller asking several bases of one
// provider reads it as "this base said nothing new" and moves on.
var ErrAsked = errors.New("every list of this base was already asked for this key")
// FetchAtListed is FetchAt over a provider's several bases, and over
// what one of them has already asked. listed is the record of this key's
// requests so far (NewListed), and the key of a request is the list URL
// it is made at and the authentication it is made with — Bearer, or the
// Anthropic version header with it.
//
// Two rules follow, and both are what make a multi-protocol vendor's
// models findable without asking its list twice (#1006). A list URL this
// key has already listed is not asked again, by any protocol: the answer
// is the answer, so a …/v1 base and the bare host beside it that speaks
// Anthropic are one /v1/models, fetched once. 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 list of a base whose Chat request
// was answered 401 is asked, with x-goog-api-key, instead of being taken
// for a list already had.
func FetchAtListed(ctx context.Context, base, key string, anthropic bool, headers map[string]string, listed *Listed) ([]Model, string, error) {
base = strings.TrimRight(strings.TrimSpace(base), "/")
if base == "" {
return nil, "", errors.New("no base URL")
}
all := listURLs(base, anthropic)
for _, u := range all {
if ms, ok := listed.listed(u); ok {
return ms, u, nil
}
}
auth := listAuthFor(anthropic)
var urls []string
for _, u := range all {
if !listed.asked(auth, u) {
urls = append(urls, u)
}
}
if len(urls) == 0 {
return nil, "", fmt.Errorf("%s: no model list: %w", base, ErrAsked)
}
var errs []string
for _, u := range urls {
ms, err := fetchOne(ctx, u, key, anthropic, headers)
listed.record(auth, u, ms)
if err == nil && len(ms) > 0 {
return ms, u, nil
}
if err == nil {
err = errors.New(u + ": no models listed")
}
errs = append(errs, err.Error())
if ctx.Err() != nil {
break
}
}
return nil, "", errors.New("no model list: " + strings.Join(errs, "; "))
}
// listAuthFor names how a list request is authenticated, which is half of
// what makes it the request it is: Chat and Responses both send Bearer,
// an Anthropic base sends the Anthropic version header as well.
func listAuthFor(anthropic bool) string {
if anthropic {
return "anthropic"
}
return "bearer"
}
// Listed is what one fetch of a provider has asked for one key: every
// list URL it asked at, under the authentication it asked it with, and
// the models of the ones that answered. A caller asking several bases of
// one provider passes the same Listed to each of them (FetchAtListed), so
// no list is fetched twice and no request is made twice.
type Listed struct {
askedAt map[string]map[string]bool // authentication -> list URL -> asked
lists map[string][]Model // list URL -> the models it listed
}
// NewListed is an empty record of one fetch's model list requests.
func NewListed() *Listed {
return &Listed{askedAt: map[string]map[string]bool{}, lists: map[string][]Model{}}
}
// asked reports whether the list at u has already been asked with this
// authentication for this key. A nil Listed asks nothing and records
// nothing, so a caller that keeps no record pays for every URL as before.
func (l *Listed) asked(auth, u string) bool {
if l == nil {
return false
}
return l.askedAt[auth][u]
}
// record notes a request made at u with this authentication, and the
// models it answered with, if it answered with any.
func (l *Listed) record(auth, u string, ms []Model) {
if l == nil {
return
}
if l.askedAt[auth] == nil {
l.askedAt[auth] = map[string]bool{}
}
l.askedAt[auth][u] = true
if len(ms) > 0 {
l.lists[u] = ms
}
}
// listed is what the list at u answered with, if it answered.
func (l *Listed) listed(u string) ([]Model, bool) {
if l == nil {
return nil, false
}
ms, ok := l.lists[u]
return ms, ok
}
// listURLs is the list URLs a base is asked at, in the order they are
// asked (FetchAtListed).
func listURLs(base string, anthropic bool) []string {
var urls []string
add := func(u string) {
for _, x := range urls {
if x == u {
return
}
}
urls = append(urls, u)
}
add(base + "/models")
if anthropic || !Versioned(base) {
add(base + "/v1/models")
root := base
for _, suffix := range []string{"/anthropic", "/apps/anthropic", "/api/anthropic", "/v1", "/api", "/api/v1"} {
if strings.HasSuffix(root, suffix) {
root = strings.TrimSuffix(root, suffix)
}
}
add(root + "/v1/models")
add(root + "/models")
}
return urls
}
// Versioned reports whether an API base URL has a version in its path — a
// segment like v1, v3, v4 or v1beta — and so names the vendor's API as it
// is, with no /v1 left out of it.
func Versioned(base string) bool {
rest := base
if _, after, ok := strings.Cut(base, "://"); ok {
rest = after
}
_, path, _ := strings.Cut(rest, "/")
if i := strings.IndexAny(path, "?#"); i >= 0 {
path = path[:i]
}
for _, seg := range strings.Split(path, "/") {
if len(seg) >= 2 && (seg[0] == 'v' || seg[0] == 'V') && seg[1] >= '0' && seg[1] <= '9' {
return true
}
}
return false
}
// FetchURL asks for the model list at exactly url.
func FetchURL(ctx context.Context, url, key string, anthropic bool, headers map[string]string) ([]Model, error) {
ms, err := fetchOne(ctx, strings.TrimSpace(url), key, anthropic, headers)
if err == nil && len(ms) == 0 {
err = errors.New(url + ": no models listed")
}
return ms, err
}
// modelPages caps how many pages of one model list are followed before
// the list is taken to be unreadable: a real catalog is one or two pages
// at PageLimit, and a relay that pages for ever stops here rather than
// being asked until it says so.
const modelPages = 20
// PageLimit is the page size asked of a list that pages, which Anthropic
// accepts: a real catalog of its own fits in one or two pages of it.
const PageLimit = 1000
// fetchOne asks the model list at url to its end: the first page as the
// URL stands, and every page after it with the id it ended at (Paged), so
// a list that pages is the whole list and not the twenty of its first
// page — a base whose only list is a paged one (Anthropic's own) has no
// other answer to fall back on, and was left with none (#1006).
func fetchOne(ctx context.Context, url, key string, anthropic bool, headers map[string]string) ([]Model, error) {
ctx, cancel := context.WithTimeout(ctx, 8*time.Second)
defer cancel()
var out []Model
err := Paged(url, func(at string) (bool, string, error) {
ms, more, last, err := fetchPage(ctx, at, key, anthropic, headers)
if err != nil {
return false, "", err
}
out = append(out, ms...)
return more, last, nil
})
if err != nil {
return nil, err
}
return out, nil
}
// Paged asks a model list that pages for every page of itself: page asks
// for one page, at the URL given, and says whether another page follows
// and the id it starts after. The first page is asked at list as it
// stands, so a list that does not page is asked exactly as it always was,
// and each page after it at the same list with limit=PageLimit and
// after_id set to the id the page before ended at. It stops when a page
// says none follows.
//
// What does not stop it leaves the list unread, and an unread list is no
// answer at all, as a page of it was (#904): 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 that same id, and one still paging after
// modelPages pages. Half a list is not a list — it is missing every model
// of the pages after, which the caller would take for models the vendor
// dropped, and would then drop from the user's picks.
//
// A caller that has to ask the list itself, for the headers it takes or
// the shape it reads, pages it here rather than with a rule of its own
// (provider.kiloModels).
func Paged(list string, page func(at string) (more bool, last string, err error)) error {
after := ""
for n := 0; ; n++ {
at := list
if after != "" {
u, err := url.Parse(list)
if err != nil {
return fmt.Errorf("%s: not a URL to page: %w", list, err)
}
q := u.Query()
q.Set("after_id", after)
q.Set("limit", strconv.Itoa(PageLimit))
u.RawQuery = q.Encode()
at = u.String()
}
more, last, err := page(at)
if err != nil {
return err
}
if !more {
return nil
}
if last == "" || last == after {
return fmt.Errorf("%s: only a page of the model list (has_more, last id %q), not the whole list", list, last)
}
if n+1 >= modelPages {
return fmt.Errorf("%s: still paging after %d pages of the model list (last id %q)", list, modelPages, last)
}
after = last
}
}
// fetchPage asks one page of the model list at url, and says whether
// another page follows, and the id it starts after. The caller bounds the
// time a whole list may take (fetchOne), and which key header to ask with
// is learned once and kept for the list that learned it (listAuth).
func fetchPage(ctx context.Context, url, key string, anthropic bool, headers map[string]string) ([]Model, bool, string, error) {
auth := listAuthOf(ctx)
ask := func(only string) (*http.Response, []byte, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, nil, err
}
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", "magpie")
if key != "" {
req.Header.Set("Authorization", "Bearer "+key)
req.Header.Set("x-api-key", key)
// a vendor that takes one of the two alone
OneAuth(req.Header, only)
}
if anthropic {
req.Header.Set("anthropic-version", "2023-06-01")
}
// The user's own headers, after the defaults so a private auth scheme
// wins. Written directly so the name keeps the exact case the user typed.
for k, v := range headers {
PutUserHeader(req.Header, k, v)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, nil, err
}
defer res.Body.Close()
b, _ := io.ReadAll(io.LimitReader(res.Body, 8<<20))
return res, b, nil
}
res, b, err := ask(auth.only)
if err != nil {
return nil, false, "", err
}
if auth.learned != nil && auth.only == "" && key != "" && BothAuthRefused(res.StatusCode, b) {
// refused for sending both: each alone, the one taken kept
for _, only := range OneAuthOrder {
if res, b, err = ask(only); err != nil {
return nil, false, "", err
}
if res.StatusCode != http.StatusUnauthorized && res.StatusCode != http.StatusForbidden {
auth.learned(only)
break
}
}
}
if res.StatusCode != http.StatusOK {
if msg := errorMessage(b); msg != "" {
return nil, false, "", fmt.Errorf("%s: %s (%s)", url, res.Status, msg)
}
return nil, false, "", fmt.Errorf("%s: %s", url, res.Status)
}
var v struct {
Data []liveModel `json:"data"`
Models []liveModel `json:"models"`
HasMore bool `json:"has_more"`
LastID string `json:"last_id"`
}
if err := json.Unmarshal(b, &v); err != nil {
return nil, false, "", fmt.Errorf("%s: not a model list", url)
}
rows := v.Data
if len(rows) == 0 {
rows = v.Models
}
// A list that says it has more is a page of the vendor's catalog, not
// the catalog: a model the user picked that lives on a later page is
// not in this reply, and a base that answers without a model is read
// as one that dropped it (#904). The pick would go for good, on a list
// that never said the model was gone. So the pages are followed to the
// end of the list (Paged), and only a list that cannot be read to its
// end says nothing about what this base serves today, which keeps what
// it listed last time. The shape is a convention, not a vendor's —
// Anthropic's and OpenAI's /v1/models both page with has_more and
// last_id.
var out []Model
for _, r := range rows {
id := r.ID
if id == "" {
id = r.Name
}
if id == "" {
continue
}
// Another magpie's list is already what its user exposed. Keep
// its retrieval models too; a vendor's general list still leaves
// out models agents can't chat with.
decision := r.Kind == "decision"
films := r.Kind == "video"
drawer := !films && !decision && (DrawsID(id) && !strings.Contains(strings.ToLower(id), "deep-research") || r.Kind == "image")
if !drawer && !films && !decision && headers["X-Magpie-Drawers"] == "" && !textModel(mdModel{ID: id}) {
continue
}
name, plain := r.DisplayName, ""
// OpenRouter's "Anthropic: Claude …" is a vendor-prefixed name;
// leave it to the catalog rather than changing every display name.
if name == "" && !strings.Contains(r.Name, ": ") {
name = r.Name
}
if r.Label != "" {
name = r.Label
if r.DisplayName != "" && r.DisplayName != r.Label {
plain = r.DisplayName
}
}
if name == "" {
name = id
}
input := imageInput(r.Modalities.Input)
apis := EndpointAPIs(r.Endpoints)
if native := EndpointAPIs(r.Native); len(native) > 0 {
apis = native
}
if len(apis) == 0 {
apis = targetAPIs(r.TypeTarget)
}
m := Model{ID: id, Name: name, Plain: plain, ImageInput: input, APIs: apis, Draws: drawer, Films: films, Decides: decision,
InputModalities: r.Modalities.Input, OutputModalities: r.Modalities.Output}
if n, ok := r.ContextLength.(float64); ok && n > 0 {
m.Context = int(n)
}
if n, ok := r.Output.(float64); ok && n > 0 {
m.Output = int(n)
}
m.Efforts = levelsOf(r.Levels)
if r.WebSearch == "native" || r.WebSearch == "magpie" {
m.WebSearch = r.WebSearch
}
if input != nil {
m.Images = *input
}
m.Tiers = tiersOf(r.Tiers)
var pr *Price
if len(r.Price) > 0 && json.Unmarshal(r.Price, &pr) == nil && pr != nil && pr.sane() {
m.Price = pr
}
out = append(out, m)
}
return out, v.HasMore, v.LastID, nil
}
// errorMessage is the message of a vendor's JSON error reply
// ({"error":{"message":…}}, {"error":"…"}, {"message":…}), cut short.
func errorMessage(b []byte) string {
var v struct {
Error json.RawMessage `json:"error"`
Message string `json:"message"`
}
if json.Unmarshal(b, &v) != nil {
return ""
}
msg := v.Message
var e struct {
Message string `json:"message"`
}
var s string
if json.Unmarshal(v.Error, &e) == nil && e.Message != "" {
msg = e.Message
} else if json.Unmarshal(v.Error, &s) == nil && s != "" {
msg = s
}
msg = strings.TrimSpace(msg)
if r := []rune(msg); len(r) > 160 {
msg = string(r[:160]) + "…"
}
return msg
}
type liveModel struct {
ID string `json:"id"`
Name string `json:"name"`
DisplayName string `json:"display_name"`
Modalities struct {
Input []string `json:"input"`
Output []string `json:"output"`
} `json:"modalities"`
// the paths the model is served on, where the vendor says: Command
// Code's Claude models on /messages alone, its open ones on
// /chat/completions and /responses
Endpoints []string `json:"supported_endpoints"`
// the context window, where the list tells it (OpenRouter, Command
// Code); any, as a vendor's odd value mustn't lose the whole list
ContextLength any `json:"context_length"`
// what another magpie's list tells of each model: the APIs its own
// provider serves it on, where a request goes on as it is rather
// than translated (native_endpoints), its longest reply and its
// reasoning levels. any, as a vendor's odd value mustn't lose the
// whole list
Native []string `json:"native_endpoints"`
Output any `json:"max_output_tokens"`
Levels any `json:"supported_reasoning_levels"`
// another magpie's name for the model with its provider there after
// it, and "image", "video" or "decision" for its other models
Label string `json:"magpie_label"`
Kind string `json:"kind"`
// how another magpie searches the web for the model: "native" or
// "magpie" (Model.WebSearch)
WebSearch string `json:"web_search"`
// the service tiers another magpie offers its Codex on the model
// (Fast's "priority"), as Codex's catalog writes them ({"id":…}) or
// as plain ids: kept as Model.Tiers (#1234). any, as an odd value
// mustn't lose the whole list
Tiers any `json:"service_tiers"`
// what another magpie counts a call to the model at, asked for with
// its X-Magpie-Prices header: the price its user set, else its list
// price. Raw, as an odd value mustn't lose the whole list
Price json.RawMessage `json:"magpie_price"`
// the protocol family PipeLLM routes the model by: openai, anthropic
// or gemini
TypeTarget string `json:"type_target"`
}
// tiersOf are the ids of a list's service_tiers, as Codex's catalog
// writes them ([{"id":"priority","name":"Fast"}]) or as plain ids.
func tiersOf(v any) []string {
xs, _ := v.([]any)
var out []string
for _, x := range xs {
id, _ := x.(string)
if o, ok := x.(map[string]any); ok {
id, _ = o["id"].(string)
}
if id != "" && !slices.Contains(out, id) {
out = append(out, id)
}
}
return out
}
// levelsOf are the efforts of a list's supported_reasoning_levels, as
// magpie and Codex write them ([{"effort":"high"}]) or as plain names.
func levelsOf(v any) []string {
xs, _ := v.([]any)
var out []string
for _, x := range xs {
e, _ := x.(string)
if o, ok := x.(map[string]any); ok {
e, _ = o["effort"].(string)
}
if e != "" && !slices.Contains(out, e) {
out = append(out, e)
}
}
return out
}
// EndpointAPIs names the APIs of a model list's supported_endpoints —
// "chat", "responses", "anthropic" — leaving out any magpie doesn't speak
// (Copilot's websocket one); nil when it names none of them.
func EndpointAPIs(endpoints []string) []string {
var out []string
for _, e := range endpoints {
var api string
switch strings.TrimPrefix(strings.TrimSuffix(e, "/"), "/v1") {
case "/chat/completions":
api = "chat"
case "/responses":
api = "responses"
case "/messages":
api = "anthropic"
}
if api != "" && !slices.Contains(out, api) {
out = append(out, api)
}
}
return out
}
// targetAPIs are the APIs a model is served on by its type_target, as
// PipeLLM's list says it: its own /v1/chat/completions and /v1/responses
// take the openai family alone, /v1/messages the anthropic one (asked
// there rather than through a converter), and its converter for Chat
// (/openai/v1) every family, Gemini's too.
func targetAPIs(target string) []string {
switch target {
case "openai":
return []string{"chat", "responses"}
case "anthropic":
return []string{"anthropic"}
case "gemini":
return []string{"chat"}
}
return nil
}
// Decorate fills in names and reasoning levels for live models from the
// catalog's entry for the same id, keeping the live order.
func Decorate(live []Model, known []Model) []Model {
byID := make(map[string]Model, len(known))
for _, m := range known {
byID[m.ID] = m
}
out := make([]Model, 0, len(live))
fallback := make([]string, len(live))
for i, m := range live {
k, ok := byID[m.ID]
if i := strings.LastIndexByte(m.ID, '/'); !ok && i >= 0 {
k, ok = byID[m.ID[i+1:]] // a gateway's "deepseek/deepseek-chat"
}
if ok {
if m.Name == "" || m.Name == m.ID {
fallback[i] = k.Name
}
m.Efforts, m.Released, m.Provider = k.Efforts, k.Released, k.Provider
m.Reasoning = m.Reasoning || k.Reasoning
if m.ImageInput == nil {
m.ImageInput = k.ImageInput
m.Images = m.Images || k.Images
m.InputModalities = k.InputModalities
}
if m.OutputModalities == nil {
m.OutputModalities = k.OutputModalities
}
if m.Context == 0 {
m.Context = k.Context
}
if m.Output == 0 {
m.Output = k.Output
}
if k.Temperature != nil {
m.Temperature = k.Temperature
}
}
out = append(out, m)
}
fillNames(out, fallback)
return out
}
// sane reports whether a price another magpie sent is one a vendor could
// charge: no part below zero or out of range, and tiers of a size.
func (p Price) sane() bool {
ok := func(vs ...float64) bool {
for _, v := range vs {
if math.IsNaN(v) || v < 0 || v > 1e6 {
return false
}
}
return true
}
if !ok(p.Input, p.Output, p.CacheRead, p.CacheWrite, p.CacheWrite1h) {
return false
}
for _, t := range p.Tiers {
if t.Above <= 0 || !ok(t.Input, t.Output, t.CacheRead, t.CacheWrite, t.CacheWrite1h) {
return false
}
}
return true
}