27 lines
2.7 KiB
TypeScript
27 lines
2.7 KiB
TypeScript
// SPEC: PJ2026-010403 API契约 draft-2026-06-17-r0; PJ2026-010401 Web工作台 draft-2026-06-17-r0.
|
|
// Responsibility: Code Agent HTTP client for send, steer, turn, trace, session and cancel APIs.
|
|
|
|
import { fetchJson, type ApiRequestOptions } from "./client";
|
|
import type { AgentChatResponse, AgentChatResultResponse, ApiResult } from "@/types";
|
|
|
|
export interface AgentTraceRequestOptions {
|
|
sinceSeq?: number | null;
|
|
limit?: number | null;
|
|
}
|
|
|
|
function agentTracePath(traceId: string, projectId: string, options: AgentTraceRequestOptions = {}): string {
|
|
const params = new URLSearchParams({ projectId });
|
|
if (Number.isFinite(options.sinceSeq)) params.set("sinceSeq", String(Math.max(0, Math.trunc(Number(options.sinceSeq)))));
|
|
if (Number.isFinite(options.limit)) params.set("limit", String(Math.max(1, Math.trunc(Number(options.limit)))));
|
|
return `/v1/agent/traces/${encodeURIComponent(traceId)}?${params.toString()}`;
|
|
}
|
|
|
|
export const agentAPI = {
|
|
sendAgentMessage: (payload: Record<string, unknown>, timeoutMs: number, activityRef?: ApiRequestOptions["activityRef"]): Promise<ApiResult<AgentChatResponse>> => fetchJson("/v1/agent/chat", { method: "POST", body: JSON.stringify(payload), timeoutMs, timeoutName: "Code Agent", activityRef }),
|
|
steerAgentMessage: (payload: Record<string, unknown>, timeoutMs: number, activityRef?: ApiRequestOptions["activityRef"]): Promise<ApiResult<AgentChatResponse>> => fetchJson("/v1/agent/chat/steer", { method: "POST", body: JSON.stringify(payload), timeoutMs, timeoutName: "Code Agent steer", activityRef }),
|
|
getAgentTurn: (traceId: string, projectId: string, timeoutMs = 8000, activityRef?: ApiRequestOptions["activityRef"]): Promise<ApiResult<AgentChatResultResponse>> => fetchJson(`/v1/agent/turns/${encodeURIComponent(traceId)}?projectId=${encodeURIComponent(projectId)}`, { timeoutMs, timeoutName: "Code Agent turn", activityRef }),
|
|
getAgentTrace: (traceId: string, projectId: string, timeoutMs = 8000, activityRef?: ApiRequestOptions["activityRef"], options?: AgentTraceRequestOptions): Promise<ApiResult<AgentChatResultResponse>> => fetchJson(agentTracePath(traceId, projectId, options), { timeoutMs, timeoutName: "Code Agent trace", activityRef }),
|
|
getAgentSession: (sessionId: string): Promise<ApiResult<{ session?: Record<string, unknown> }>> => fetchJson(`/v1/agent/sessions/${encodeURIComponent(sessionId)}`, { timeoutMs: 8000, timeoutName: "Code Agent session" }),
|
|
cancelAgentMessage: (payload: Record<string, unknown>): Promise<ApiResult<{ ok?: boolean; canceled?: boolean; error?: { code?: string; message?: string; userMessage?: string } }>> => fetchJson("/v1/agent/chat/cancel", { method: "POST", body: JSON.stringify(payload), timeoutMs: 30000, timeoutName: "Code Agent cancel" })
|
|
};
|