业务链条

This commit is contained in:
fengxiangdebaozi committed 2026-06-02 21:15:46 +08:00
1 parent 30410b0a93
commit 9ae6184dc1
20 files changed
+2440 -24

No files matched your search

@@ -0,0 +1,129 @@
/**
* Backend-persisted business-chain storage for /page/roundtable/planning.
*
* A business chain is a named, reusable, *ordered* list of agents. Persisted
* server-side (MySQL) bound to the logged-in user, so it follows the account
* across devices/sessions. Backend: `/api/roundtable-chains` (see
* `app/gateway/routers/roundtable_chains.py`).
*
* snake_case ⇄ camelCase mapping is collapsed at this boundary (mirroring
* `api/drafts.ts`) so callers stay clean.
*/
import { apiFetch } from "@/core/api/fetch-client";
import { getBackendBaseURL } from "@/core/config";
import type { BusinessChain, ChainSeat } from "@/roundtable-planning/lib/business-chain";
/** Fields accepted when creating a chain. */
export interface ChainCreatePayload {
/** Optional stable id so the client can keep one id across create + updates. */
id?: string;
title: string;
description?: string;
seats: ChainSeat[];
}
/** Partial update — send only what changed. */
export interface ChainUpdatePayload {
title?: string;
description?: string;
seats?: ChainSeat[];
}
const BASE = () => `${getBackendBaseURL()}/api/roundtable-chains`;
async function readJson<T>(res: Response, ctx: string): Promise<T> {
if (!res.ok) {
const text = await res.text().catch(() => "");
throw new Error(`${ctx} failed (${res.status}): ${text}`);
}
return (await res.json()) as T;
}
// ── server ⇄ client field mapping ──────────────────────────────────────────
// Backend uses snake_case (created_at / updated_at); the rest of the frontend
// reads camelCase. `seats` is already camel-clean ({agent_id, name, description}).
interface ServerChain {
id: string;
title: string;
description: string | null;
seats: ChainSeat[];
created_at: string | null;
updated_at: string | null;
}
function toChain(s: ServerChain): BusinessChain {
return {
id: s.id,
title: s.title ?? "",
description: s.description ?? undefined,
seats: Array.isArray(s.seats) ? s.seats : [],
createdAt: s.created_at ?? undefined,
updatedAt: s.updated_at ?? undefined,
};
}
function fromCreate(p: ChainCreatePayload) {
return {
...(p.id ? { id: p.id } : {}),
title: p.title,
...(p.description !== undefined ? { description: p.description } : {}),
seats: p.seats,
};
}
function fromUpdate(p: ChainUpdatePayload) {
const out: Record<string, unknown> = {};
if (p.title !== undefined) out.title = p.title;
if (p.description !== undefined) out.description = p.description;
if (p.seats !== undefined) out.seats = p.seats;
return out;
}
// ── CRUD ────────────────────────────────────────────────────────────────────
/** List the current user's chains, newest first. */
export async function listChains(): Promise<BusinessChain[]> {
const res = await apiFetch(BASE(), { method: "GET" });
const data = await readJson<{ chains: ServerChain[] }>(res, "list chains");
return (data.chains ?? []).map(toChain);
}
/** Fetch one chain. Returns null on 404. */
export async function getChain(id: string): Promise<BusinessChain | null> {
const res = await apiFetch(`${BASE()}/${encodeURIComponent(id)}`, { method: "GET" });
if (res.status === 404) return null;
const data = await readJson<ServerChain>(res, "get chain");
return toChain(data);
}
export async function createChain(payload: ChainCreatePayload): Promise<BusinessChain> {
const res = await apiFetch(BASE(), {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(fromCreate(payload)),
});
const data = await readJson<ServerChain>(res, "create chain");
return toChain(data);
}
export async function updateChain(id: string, patch: ChainUpdatePayload): Promise<BusinessChain | null> {
const res = await apiFetch(`${BASE()}/${encodeURIComponent(id)}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(fromUpdate(patch)),
});
if (res.status === 404) return null;
const data = await readJson<ServerChain>(res, "update chain");
return toChain(data);
}
export async function deleteChain(id: string): Promise<void> {
const res = await apiFetch(`${BASE()}/${encodeURIComponent(id)}`, { method: "DELETE" });
if (!res.ok && res.status !== 404) {
const text = await res.text().catch(() => "");
throw new Error(`delete chain failed (${res.status}): ${text}`);
}
}
@@ -45,6 +45,15 @@ export interface DraftStep2Snapshot {
budgetLimit: number;
/** Per-seat stub AI messages for the sandbox `write-file:` virtual URL loader. */
seatStubMessages?: Record<string, unknown[]>;
/**
* Step 2 orchestration mode (2026-06 business chains). recommend = leader
* dispatches freely; chain = dispatch follows the saved business chain order.
* Optional — drafts saved before this won't carry it (hydrate defaults to
* "recommend").
*/
orchestrationMode?: "recommend" | "chain";
/** Source business chain (display only), when `orchestrationMode === "chain"`. */
chain?: { id: string; title: string } | null;
}
/** Lightweight metadata row returned by the list endpoint. */