Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 87 additions & 1 deletion tests/web/web-host.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,22 @@ test("serves workspaces through a runtime isolated from terminal sessions", asyn
};
},
switchSession: async () => ({ cancelled: false }),
listModels: () => [],
listModels: () => [
{
provider: "fixture",
id: "current-model",
name: "Current",
label: "Current",
current: true,
},
{
provider: "fixture",
id: "other-model",
name: "Other",
label: "Other",
current: false,
},
],
setModel: async () => {
throw new WebRuntimeRequestError(
"Model is not available",
Expand Down Expand Up @@ -243,6 +258,14 @@ test("serves workspaces through a runtime isolated from terminal sessions", asyn

const unauthorized = await fetch(`${launched.origin}/api/snapshot`);
assert.equal(unauthorized.status, 401);
const unauthorizedCapabilities = await fetch(
`${launched.origin}/api/capabilities`,
);
assert.equal(unauthorizedCapabilities.status, 401);
const unauthorizedDiagnostics = await fetch(
`${launched.origin}/api/diagnostics`,
);
assert.equal(unauthorizedDiagnostics.status, 401);

const response = await fetch(`${launched.origin}/api/snapshot`, {
headers: authorized,
Expand Down Expand Up @@ -294,6 +317,69 @@ test("serves workspaces through a runtime isolated from terminal sessions", asyn
omitted: 0,
truncated: false,
});
const capabilitiesResponse = await fetch(
`${launched.origin}/api/capabilities`,
{ headers: authorized },
);
assert.equal(capabilitiesResponse.status, 200);
const capabilitiesBody = (await capabilitiesResponse.json()) as {
sessionId: string;
capabilities: Record<string, unknown>;
};
assert.deepEqual(Object.keys(capabilitiesBody).sort(), [
"capabilities",
"sessionId",
]);
assert.equal(capabilitiesBody.sessionId, sessionManager.getSessionId());
assert.deepEqual(
capabilitiesBody.capabilities,
snapshot.runtime.capabilities,
);
assert.doesNotMatch(
JSON.stringify(capabilitiesBody),
/token|Authorization|Bearer|transcript|entries|messages|apiKey|secret/i,
);
const writeCapabilities = await fetch(
`${launched.origin}/api/capabilities`,
{
method: "POST",
headers: authorized,
body: "{}",
},
);
assert.equal(writeCapabilities.status, 405);

const diagnosticsResponse = await fetch(
`${launched.origin}/api/diagnostics`,
{ headers: authorized },
);
assert.equal(diagnosticsResponse.status, 200);
const diagnosticsBody = await diagnosticsResponse.json();
assert.deepEqual(diagnosticsBody, {
node: process.version,
cwd,
sessionId: sessionManager.getSessionId(),
workspaceSelected: true,
models: [
{
provider: "fixture",
id: "current-model",
name: "Current",
label: "Current",
current: true,
},
],
});
assert.doesNotMatch(
JSON.stringify(diagnosticsBody),
/token|Authorization|Bearer|transcript|entries|messages|apiKey|secret/i,
);
const writeDiagnostics = await fetch(`${launched.origin}/api/diagnostics`, {
method: "POST",
headers: authorized,
body: "{}",
});
assert.equal(writeDiagnostics.status, 405);

const sessionsResponse = await fetch(`${launched.origin}/api/sessions`, {
headers: authorized,
Expand Down
18 changes: 17 additions & 1 deletion web/host/web-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import {
} from "node:http";
import { URL } from "node:url";
import { promisify } from "node:util";
import { subscribeWebCapabilities } from "../../extensions/shared/web-observer-registry.ts";
import {
subscribeWebCapabilities,
webCapabilitySnapshot,
} from "../../extensions/shared/web-observer-registry.ts";
import { PiWebAdapter } from "../adapter/pi-adapter.ts";
import {
jsonByteLength,
Expand Down Expand Up @@ -557,6 +560,19 @@ export class WebHost {
}
if (url.pathname === "/api/models")
return this.json(response, 200, { models: this.runtime.listModels() });
if (url.pathname === "/api/capabilities")
return this.json(response, 200, {
sessionId: this.runtime.sessionManager.getSessionId(),
capabilities: webCapabilitySnapshot(this.runtime.sessionManager),
});
if (url.pathname === "/api/diagnostics")
return this.json(response, 200, {
node: process.version,
cwd: this.runtime.cwd,
sessionId: this.runtime.sessionManager.getSessionId(),
workspaceSelected: this.runtime.workspaceSelected,
models: this.runtime.listModels().filter((model) => model.current),
});
if (url.pathname === "/api/snapshot") {
const cursor = this.sequence;
const projection = await this.adapter.getSnapshot(
Expand Down
Loading