feat(daemon): resolve /api/v1/models without spawning an instance

`GET /api/v1/models?workspaceId=` could only answer for a named workspace, and
naming one is enough to spawn its Neovim — fine for the chat picker, wrong for
the status page, which has no session to offer and would sit in
E_INSTANCE_STARTING forever.

The parameter is now optional. With one, the read is relayed exactly as before.
Without one, src/daemon/models.ts scans only instances that are already `ready`,
prefers the one with live sessions and the longest uptime, and relays
`workspace.query{kind:"models"}` to it, falling through to the next candidate on
failure. No instance is ever created.

The plugin's answer is lazy: the first call returns
`{providers:[], pending:true}` while its cache fills. The response therefore
carries `pending`, the `workspaceId` that answered (or null) and a
`pendingReason` of `fetching`, `no-instance` or `no-live-session`, so a client
can poll with a bound and explain itself instead of showing an empty list. The
workspaceId-scoped answer now also echoes `workspaceId` back.
This commit is contained in:
yuez committed 2026-10-28 08:14:05 +08:00
1 parent 88354ec078
commit 22de7dcc9c
6 files changed
+535 -4

No files matched your search

+20
View File
@@ -275,6 +275,7 @@ describe("HTTP API", () => {
expect(attention.status).toBe(200);
const models = await fetch(`${h.url}/api/v1/models?workspaceId=ws-1`, authed());
expect(models.status).toBe(200);
expect(await models.json()).toEqual(expect.objectContaining({ workspaceId: "ws-1" }));
expect(conn.commands.some((call) => call.method === "workspace.query" && call.params?.kind === "models")).toBe(
true,
);
@@ -451,6 +452,25 @@ describe("HTTP API", () => {
}));
});
it("resolves bare models without spawning when no instance is ready", async () => {
const bare = await createHarness();
try {
const bareToken = bare.createToken();
const response = await fetch(`${bare.url}/api/v1/models`, {
headers: { Authorization: `Bearer ${bareToken}` },
});
expect(response.status).toBe(200);
const body = (await response.json()) as Record<string, unknown>;
expect(body).toEqual({
providers: [], pending: true, workspaceId: null, pendingReason: "no-instance",
});
expect(body).not.toHaveProperty("attempted");
expect(bare.instances.ensureCalls).toEqual([]);
} finally {
await bare.cleanup();
}
});
it("redirects a scanned /pair/<code> link to the zero-input connect page", async () => {
const res = await fetch(`${h.url}/pair/abcd2345`, { redirect: "manual" });
expect(res.status).toBe(302);