fix(backend): forward workspace to tht config selection (MVP backend-side)
POST /sessions no longer silently drops `workspace`. ThtRunner.run/json take an optional workspace; configArg() selects workspaces/<ws>.yaml when it exists under harnessDir, else falls back to default configPath. sessionNew threads workspace through. Route forwards b.workspace with an MVP note (gate/Pi side still single-workspace via symlinked config/tht.yaml). 19/19 tests pass, tsc clean. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -7,7 +7,11 @@ import { getUser } from "../auth/auth.js";
|
||||
export function sessionRoutes(app: FastifyInstance, d: { mgr: PiProcessManager; tht: ThtRunner; hub: SseHub }) {
|
||||
app.post("/sessions", async (req, reply) => {
|
||||
const b = req.body as any;
|
||||
const { id } = await d.tht.sessionNew({ question: b.question, provider: b.provider, model: b.model, thinking: b.thinking, name: b.name });
|
||||
// NOTE (MVP): `workspace` selects the tht `-c <config>` for the backend's own
|
||||
// tht calls (session new/list/show). The gate/Pi side still uses the symlinked
|
||||
// `config/tht.yaml` (single-workspace). Full multi-workspace on the Pi side is
|
||||
// future work — the gate would need a THT_CONFIG/workspace env passed at spawn.
|
||||
const { id } = await d.tht.sessionNew({ question: b.question, provider: b.provider, model: b.model, thinking: b.thinking, name: b.name, workspace: b.workspace });
|
||||
const rt = await d.mgr.spawnFor(id, { provider: b.provider, model: b.model, thinking: b.thinking, author: getUser(req).id });
|
||||
rt.bridge.onClientEvent((e) => d.hub.publish(id, e.type, e));
|
||||
return { id };
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { spawn } from "node:child_process";
|
||||
import { existsSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
|
||||
export interface ThtConfig {
|
||||
thtBin: string;
|
||||
@@ -19,9 +21,21 @@ export interface SessionRow {
|
||||
export class ThtRunner {
|
||||
constructor(private cfg: ThtConfig) {}
|
||||
|
||||
run(args: string[]): Promise<{ code: number; stdout: string; stderr: string }> {
|
||||
/**
|
||||
* Resolve the `-c <config>` args. If `workspace` is given AND a matching
|
||||
* `workspaces/<workspace>.yaml` exists under harnessDir, select it; otherwise
|
||||
* fall back to the default configPath.
|
||||
*/
|
||||
private configArg(workspace?: string): string[] {
|
||||
if (workspace && existsSync(join(this.cfg.harnessDir, "workspaces", `${workspace}.yaml`))) {
|
||||
return ["-c", `workspaces/${workspace}.yaml`];
|
||||
}
|
||||
return ["-c", this.cfg.configPath];
|
||||
}
|
||||
|
||||
run(args: string[], workspace?: string): Promise<{ code: number; stdout: string; stderr: string }> {
|
||||
return new Promise((resolve) => {
|
||||
const ch = spawn(this.cfg.thtBin, ["-c", this.cfg.configPath, ...args], {
|
||||
const ch = spawn(this.cfg.thtBin, [...this.configArg(workspace), ...args], {
|
||||
cwd: this.cfg.harnessDir,
|
||||
});
|
||||
let stdout = "";
|
||||
@@ -32,8 +46,8 @@ export class ThtRunner {
|
||||
});
|
||||
}
|
||||
|
||||
private async json<T>(args: string[]): Promise<T> {
|
||||
const { code, stdout, stderr } = await this.run(args);
|
||||
private async json<T>(args: string[], workspace?: string): Promise<T> {
|
||||
const { code, stdout, stderr } = await this.run(args, workspace);
|
||||
if (code !== 0) throw new Error(`tht ${args.join(" ")} exit ${code}: ${stderr.trim()}`);
|
||||
return JSON.parse(stdout) as T;
|
||||
}
|
||||
@@ -44,6 +58,7 @@ export class ThtRunner {
|
||||
model?: string;
|
||||
thinking?: string;
|
||||
name?: string;
|
||||
workspace?: string;
|
||||
}) {
|
||||
const a = ["session", "new", o.question];
|
||||
for (const [f, v] of [
|
||||
@@ -55,7 +70,7 @@ export class ThtRunner {
|
||||
if (v) a.push(f, v);
|
||||
}
|
||||
a.push("--json");
|
||||
return this.json<{ id: string }>(a);
|
||||
return this.json<{ id: string }>(a, o.workspace);
|
||||
}
|
||||
|
||||
sessionList() {
|
||||
|
||||
Reference in New Issue
Block a user