mirror of
https://github.com/luckyyzh/pi-agent-integrated.git
synced 2026-10-03 11:09:34 +00:00
feat: integrate Pi backend and Pi Web
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
// Client-side helper for POST /api/agent/[id].
|
||||
//
|
||||
// Every /api/agent/[id] route returns one of:
|
||||
// { success: true, data: <result> }
|
||||
// { error: string } (non-2xx)
|
||||
//
|
||||
// Call sites previously repeated the same 5-line fetch block 13× in
|
||||
// hooks/useAgentSession.ts. This helper collapses that down to one line.
|
||||
|
||||
export async function sendAgentCommand<T = unknown>(
|
||||
sessionId: string,
|
||||
command: Record<string, unknown>,
|
||||
): Promise<T> {
|
||||
const res = await fetch(`/api/agent/${encodeURIComponent(sessionId)}`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(command),
|
||||
});
|
||||
const body = (await res.json().catch(() => ({}))) as {
|
||||
success?: boolean;
|
||||
data?: T;
|
||||
error?: string;
|
||||
};
|
||||
if (!res.ok || body.error) {
|
||||
throw new Error(body.error ?? `HTTP ${res.status}`);
|
||||
}
|
||||
return body.data as T;
|
||||
}
|
||||
Reference in New Issue
Block a user