feat(task-10): SQL routes + /workspaces + /models + fix sql preview path bug
Harness:
- preview_cmd FILE positional arg made optional; when omitted with --session,
path is derived via _session_sql_file (mirrors export_cmd) — fixes the
deferred Task-5 bug where the backend passed sessions/<id>/sql_final.sql
relative to harnessDir, which broke for workspace-dependent paths.
- New pytest: test_preview_session_no_file_resolves_sql_final
Backend:
- ThtRunner.sqlPreview: drop positional file arg; use --session only
- New routes/sql.ts: POST /sessions/:id/sql/preview + /export
- New routes/meta.ts: GET /workspaces (yaml scan) + GET /models (injectable
seam + graceful fallback to {models:[]})
- app.ts: register sqlRoutes + metaRoutes; add listModels to BuildAppDeps
- tht-runner.test.ts: add sqlPreview argv assertion (no file path)
- test/routes-sql-meta.test.ts: 9 tests (sql preview/export + meta routes)
Tests: harness 233 passed; backend 29 passed; build clean.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -5,10 +5,13 @@ import { PiProcessManager } from "./pi/pi-process-manager.js";
|
||||
import { SseHub } from "./sse/sse-hub.js";
|
||||
import { authPreHandler } from "./auth/auth.js";
|
||||
import { sessionRoutes } from "./routes/sessions.js";
|
||||
import { sqlRoutes } from "./routes/sql.js";
|
||||
import { metaRoutes, type ListModelsFn } from "./routes/meta.js";
|
||||
|
||||
export interface BuildAppDeps {
|
||||
thtRunner?: ThtRunner;
|
||||
spawnFn?: () => any;
|
||||
listModels?: ListModelsFn;
|
||||
}
|
||||
|
||||
export function buildApp(config: AppConfig, deps?: BuildAppDeps): FastifyInstance {
|
||||
@@ -25,6 +28,8 @@ export function buildApp(config: AppConfig, deps?: BuildAppDeps): FastifyInstanc
|
||||
app.addHook("preHandler", authPreHandler(config.authMode));
|
||||
app.get("/health", async () => ({ status: "ok" }));
|
||||
sessionRoutes(app, { mgr, tht: tht as ThtRunner, hub });
|
||||
sqlRoutes(app, { tht: tht as ThtRunner });
|
||||
metaRoutes(app, { harnessDir: config.harnessDir, listModels: deps?.listModels });
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
import { readdirSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
|
||||
export type ListModelsFn = () => Promise<string[]>;
|
||||
|
||||
/**
|
||||
* List YAML workspace configs found in <harnessDir>/workspaces/*.yaml.
|
||||
* Returns [{name, file}] — no database credentials or secrets.
|
||||
*/
|
||||
function listWorkspaces(harnessDir: string): { name: string; file: string }[] {
|
||||
const dir = join(harnessDir, "workspaces");
|
||||
let entries: string[];
|
||||
try {
|
||||
entries = readdirSync(dir);
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
return entries
|
||||
.filter((f) => f.endsWith(".yaml") || f.endsWith(".yml"))
|
||||
.map((f) => ({
|
||||
name: f.replace(/\.ya?ml$/, ""),
|
||||
file: f,
|
||||
}));
|
||||
}
|
||||
|
||||
export function metaRoutes(
|
||||
app: FastifyInstance,
|
||||
deps: { harnessDir: string; listModels?: ListModelsFn },
|
||||
): void {
|
||||
app.get("/workspaces", async () => {
|
||||
return listWorkspaces(deps.harnessDir);
|
||||
});
|
||||
|
||||
app.get("/models", async (_req, reply) => {
|
||||
const fn = deps.listModels ?? defaultListModels;
|
||||
try {
|
||||
const models = await fn();
|
||||
return { models };
|
||||
} catch {
|
||||
// Graceful fallback: Pi may not be running; don't crash the server.
|
||||
return { models: [] };
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Default implementation: spawns a short-lived `tht` invocation that asks a Pi
|
||||
* process for available models via get_available_models. This is intentionally
|
||||
* behind the injectable seam so tests can stub it without spawning real processes.
|
||||
*
|
||||
* In the MVP we return an empty list — the real spawn path can be wired in later
|
||||
* once a Pi-side "list models" RPC stabilises.
|
||||
*/
|
||||
async function defaultListModels(): Promise<string[]> {
|
||||
// Real ephemeral Pi spawn left for a follow-up task.
|
||||
// Returning [] here triggers the graceful fallback seen by clients.
|
||||
return [];
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import type { ThtRunner } from "../tht/tht-runner.js";
|
||||
|
||||
export function sqlRoutes(app: FastifyInstance, deps: { tht: ThtRunner }): void {
|
||||
app.post("/sessions/:id/sql/preview", async (req, reply) => {
|
||||
const id = (req.params as any).id as string;
|
||||
const { limit, offset } = (req.body as any) ?? {};
|
||||
try {
|
||||
const result = await deps.tht.sqlPreview(id, { limit, offset });
|
||||
return result;
|
||||
} catch (err: any) {
|
||||
return reply.code(500).send({ error: err.message ?? String(err) });
|
||||
}
|
||||
});
|
||||
|
||||
app.post("/sessions/:id/sql/export", async (req, reply) => {
|
||||
const id = (req.params as any).id as string;
|
||||
try {
|
||||
const result = await deps.tht.sqlExport(id);
|
||||
return result;
|
||||
} catch (err: any) {
|
||||
return reply.code(500).send({ error: err.message ?? String(err) });
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -82,7 +82,9 @@ export class ThtRunner {
|
||||
}
|
||||
|
||||
sqlPreview(id: string, p: { limit?: number; offset?: number }) {
|
||||
const a = ["sql", "preview", `sessions/${id}/sql_final.sql`, "--session", id, "--json"];
|
||||
// No positional FILE: the harness resolves sql_final.sql from the session
|
||||
// via _session_sql_file(cfg, session_id), which respects the workspace path.
|
||||
const a = ["sql", "preview", "--session", id, "--json"];
|
||||
if (p.limit != null) a.push("--limit", String(p.limit));
|
||||
if (p.offset != null) a.push("--offset", String(p.offset));
|
||||
return this.json<{
|
||||
|
||||
Reference in New Issue
Block a user