Public: anyone can read this; the crew can change it.
GET /api/v1/forges/sizes and MCP `sizes` say what small, medium and large mean #3215
scout/forge:scout/3205-get-api-v1-forges-sizes-and-mcp-sizes-sa → main 2 commits fork
4 files
changed +120
−0
src/http/api.ts +11
| @@ -358,6 +358,8 @@ import { readRunLog, subscribeRunLog } from "../runs/log.ts"; | ||
| 358 | 358 | import { blobForRender, commitLog, listRefs, listTree } from "../git/read.ts"; |
| 359 | 359 | import { repoDir } from "../git/repos.ts"; |
| 360 | 360 | import type { Result } from "../kernel/result.ts"; |
| 361 | import { sizesView } from "../spawn/sizes.ts"; | |
| 362 | import { holds, weakEtag } from "./etag.ts"; | |
| 361 | 363 | import { inviteLink, list as listInviteLinks, mint as mintInviteLink, revoke as revokeInviteLink } from "../actions/invites.ts"; |
| 362 | 364 | import { originForRepo, type OriginRepoRow } from "../core/origins.ts"; |
| 363 | 365 | import { subscribe as subscribeJob, type MirrorJob } from "../origins/jobs.ts"; |
| @@ -4885,6 +4887,15 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 4885 | 4887 | return json(await familyView(ctx.app, ctx.auth.user?.isAdmin === true)); |
| 4886 | 4888 | }); |
| 4887 | 4889 | |
| 4890 | // What small, medium and large mean: public, and cacheable by ETag. | |
| 4891 | router.add("GET", "/api/v1/forges/sizes", (ctx) => { | |
| 4892 | const text = JSON.stringify(sizesView()); | |
| 4893 | const etag = weakEtag(text); | |
| 4894 | const headers = { etag, "cache-control": "public, max-age=300" }; | |
| 4895 | if (holds(ctx.request, etag)) return new Response(null, { status: 304, headers }); | |
| 4896 | return new Response(text, { headers: { "content-type": "application/json", ...headers } }); | |
| 4897 | }); | |
| 4898 | ||
| 4888 | 4899 | // A forge's children (docs/SPAWN.md): an admin's, planned before made. |
| 4889 | 4900 | router.add("GET", "/api/v1/forges", async (ctx) => { |
| 4890 | 4901 | const denied = requireScope(ctx, "ro"); |
src/http/mcp.ts +9
| @@ -182,6 +182,7 @@ import { parentForgeFor } from "../core/parent_releases.ts"; | ||
| 182 | 182 | import { COPIES_PER_DAY, makeItYours } from "../actions/make_it_yours.ts"; |
| 183 | 183 | import { resourceMetadataUrl } from "./oauth.ts"; |
| 184 | 184 | import { body, boundedBody, json } from "./respond.ts"; |
| 185 | import { sizesView } from "../spawn/sizes.ts"; | |
| 185 | 186 | import type { Router } from "./router.ts"; |
| 186 | 187 | import { mayReadRuns, runForReader } from "../actions/runs.ts"; |
| 187 | 188 | import { manifestFor } from "../actions/here.ts"; |
| @@ -385,6 +386,14 @@ function boardFailed(e: BoardError): Answer { | ||
| 385 | 386 | } |
| 386 | 387 | |
| 387 | 388 | const TOOLS: readonly Tool[] = [ |
| 389 | { | |
| 390 | name: "sizes", | |
| 391 | description: | |
| 392 | "What a child forge's sizes mean: small, medium and large, each with its CPU, memory and disk, and one sentence about it.", | |
| 393 | inputSchema: { type: "object", properties: {} }, | |
| 394 | writes: false, | |
| 395 | run: () => good(sizesView()), | |
| 396 | }, | |
| 388 | 397 | { |
| 389 | 398 | name: "apps", |
| 390 | 399 | description: |
src/spawn/sizes.ts +14
| @@ -28,3 +28,17 @@ export function parseSize(v: unknown): Result<Size, string> { | ||
| 28 | 28 | export const DEFAULT_SIZE: Size = "small"; |
| 29 | 29 | |
| 30 | 30 | export const isSize = (v: unknown): v is Size => typeof v === "string" && Object.hasOwn(SIZES, v); |
| 31 | ||
| 32 | const SAYS: Readonly<Record<Size, string>> = { | |
| 33 | small: "One CPU and half a gigabyte: a notes app, a small tool, a place to try things.", | |
| 34 | medium: "Two CPUs and two gigabytes: a busy app, or a few apps side by side.", | |
| 35 | large: "Four CPUs and eight gigabytes: a crew's working forge with room to build.", | |
| 36 | }; | |
| 37 | ||
| 38 | /** What each size means, for a page or an agent to offer; read from SIZES. */ | |
| 39 | export function sizesView(): { sizes: { size: Size; cpu: number; memoryMiB: number; diskMiB: number; says: string }[]; default: Size } { | |
| 40 | return { | |
| 41 | sizes: (Object.keys(SIZES) as Size[]).map((size) => ({ size, ...SIZES[size], says: SAYS[size] })), | |
| 42 | default: DEFAULT_SIZE, | |
| 43 | }; | |
| 44 | } |
test/sizes-api.test.ts added +86
| @@ -0,0 +1,86 @@ | ||
| 1 | /** The sizes answer is the table in src/spawn/sizes.ts, over HTTP and over MCP. */ | |
| 2 | ||
| 3 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 4 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 5 | import { tmpdir } from "node:os"; | |
| 6 | import { join } from "node:path"; | |
| 7 | import { createApp, type App } from "../src/app.ts"; | |
| 8 | import { createToken } from "../src/core/tokens.ts"; | |
| 9 | import { createUser } from "../src/core/users.ts"; | |
| 10 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 11 | import { SIZES, sizesView, type Size } from "../src/spawn/sizes.ts"; | |
| 12 | import { quietLogs } from "./helpers/globals.ts"; | |
| 13 | ||
| 14 | quietLogs(); | |
| 15 | ||
| 16 | describe("forge sizes", () => { | |
| 17 | let dataDir: string; | |
| 18 | let app: App; | |
| 19 | let servers: Servers; | |
| 20 | let token: string; | |
| 21 | ||
| 22 | beforeAll(async () => { | |
| 23 | dataDir = await mkdtemp(join(tmpdir(), "forge-sizes-")); | |
| 24 | app = createApp({ dataDir, port: 0, host: "forge.test" }); | |
| 25 | servers = await serve(app); | |
| 26 | const u = await createUser(app.db, { | |
| 27 | origin: "seed", | |
| 28 | trusted: true, | |
| 29 | username: "alice", | |
| 30 | email: "[email protected]", | |
| 31 | password: "a-good-passphrase", | |
| 32 | adminIfFirst: true, | |
| 33 | }); | |
| 34 | if (!u.ok) throw new Error(u.error); | |
| 35 | token = createToken(app.db, u.value.id, "t", "ro").token; | |
| 36 | }, 120_000); | |
| 37 | ||
| 38 | afterAll(async () => { | |
| 39 | servers.stop(); | |
| 40 | app.close(); | |
| 41 | await rm(dataDir, { recursive: true, force: true }); | |
| 42 | }); | |
| 43 | ||
| 44 | test("signed out, each size answers its table row and a sentence", async () => { | |
| 45 | const res = await fetch(`${servers.url}/api/v1/forges/sizes`); | |
| 46 | expect(res.status).toBe(200); | |
| 47 | const got = (await res.json()) as ReturnType<typeof sizesView>; | |
| 48 | expect(got).toEqual(sizesView()); | |
| 49 | expect(got.sizes.map((s) => s.size)).toEqual(Object.keys(SIZES) as Size[]); | |
| 50 | for (const s of got.sizes) { | |
| 51 | const row = SIZES[s.size]; | |
| 52 | expect([s.cpu, s.memoryMiB, s.diskMiB]).toEqual([row.cpu, row.memoryMiB, row.diskMiB]); | |
| 53 | expect(s.says.length).toBeGreaterThan(10); | |
| 54 | } | |
| 55 | }); | |
| 56 | ||
| 57 | test("it carries an ETag and answers 304 to it", async () => { | |
| 58 | const first = await fetch(`${servers.url}/api/v1/forges/sizes`); | |
| 59 | const etag = first.headers.get("etag")!; | |
| 60 | expect(etag).toBeTruthy(); | |
| 61 | const again = await fetch(`${servers.url}/api/v1/forges/sizes`, { | |
| 62 | headers: { "if-none-match": etag }, | |
| 63 | }); | |
| 64 | expect(again.status).toBe(304); | |
| 65 | }); | |
| 66 | ||
| 67 | test("the MCP tool returns the same JSON", async () => { | |
| 68 | const res = await fetch(`${servers.url}/mcp`, { | |
| 69 | method: "POST", | |
| 70 | headers: { | |
| 71 | authorization: `Bearer ${token}`, | |
| 72 | "content-type": "application/json", | |
| 73 | accept: "application/json, text/event-stream", | |
| 74 | }, | |
| 75 | body: JSON.stringify({ | |
| 76 | jsonrpc: "2.0", | |
| 77 | id: 1, | |
| 78 | method: "tools/call", | |
| 79 | params: { name: "sizes", arguments: {} }, | |
| 80 | }), | |
| 81 | }); | |
| 82 | const body = (await res.json()) as { result: { isError?: boolean; content: { text: string }[] } }; | |
| 83 | expect(body.result.isError).toBeFalsy(); | |
| 84 | expect(JSON.parse(body.result.content[0]!.text)).toEqual(sizesView()); | |
| 85 | }); | |
| 86 | }); |
new above
offline