32 lines
1 KiB
TypeScript
32 lines
1 KiB
TypeScript
|
|
import { eq } from "../drizzle.js";
|
||
|
|
import { db } from "../db.js";
|
||
|
|
import { workspaces } from "../schema.js";
|
||
|
|
|
||
|
|
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Resolves a workspace handle (UUID or slug) to a `{ id, slug, name }` record.
|
||
|
|
* MCP tools accept either since agents may have hard-coded either form. Throws
|
||
|
|
* a friendly Error if no row matches so `toolCatch` can surface the message.
|
||
|
|
*/
|
||
|
|
export async function resolveWorkspaceHandle(
|
||
|
|
handle: string,
|
||
|
|
): Promise<{ id: string; slug: string; name: string }> {
|
||
|
|
const cleaned = handle.trim();
|
||
|
|
if (!cleaned) {
|
||
|
|
throw new Error("Workspace handle is required");
|
||
|
|
}
|
||
|
|
const cond = UUID_RE.test(cleaned)
|
||
|
|
? eq(workspaces.id, cleaned)
|
||
|
|
: eq(workspaces.slug, cleaned);
|
||
|
|
const [row] = await db
|
||
|
|
.select({ id: workspaces.id, slug: workspaces.slug, name: workspaces.name })
|
||
|
|
.from(workspaces)
|
||
|
|
.where(cond)
|
||
|
|
.limit(1);
|
||
|
|
if (!row) {
|
||
|
|
throw new Error(`Workspace not found for handle "${handle}"`);
|
||
|
|
}
|
||
|
|
return row;
|
||
|
|
}
|