fix(api): align frontend with REST contracts

This commit is contained in:
2026-07-30 20:38:52 +08:00
parent b57e58ff87
commit 782363cfb6
65 changed files with 99362 additions and 318 deletions
+36 -19
View File
@@ -466,6 +466,28 @@ const readStreamEvents = async (
}
};
const ensureAgentSession = async (sessionId?: string) => {
if (sessionId) return sessionId;
const response = await apiFetch(`${config.AGENT_URL}/api/v1/agent/sessions`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({}),
projectHeaderMode: "include",
skipAuthRedirect: true,
});
if (!response.ok) {
throw new Error((await response.text()) || `session creation failed: ${response.status}`);
}
const payload = (await response.json()) as { session_id?: string };
if (!payload.session_id) {
throw new Error("session creation returned no session_id");
}
return payload.session_id;
};
export const streamAgentChat = async ({
message,
sessionId,
@@ -476,8 +498,9 @@ export const streamAgentChat = async ({
}: StreamOptions) => {
let response: Response;
try {
const effectiveSessionId = await ensureAgentSession(sessionId);
response = await apiFetch(
`${config.AGENT_URL}/api/v1/agent/chat/stream`,
`${config.AGENT_URL}/api/v1/agent/sessions/${encodeURIComponent(effectiveSessionId)}/runs`,
{
method: "POST",
signal,
@@ -487,7 +510,6 @@ export const streamAgentChat = async ({
},
body: JSON.stringify({
message,
session_id: sessionId,
model,
approval_mode: approvalMode,
}),
@@ -535,7 +557,7 @@ export const resumeAgentChatStream = async ({
let response: Response;
try {
response = await apiFetch(
`${config.AGENT_URL}/api/v1/agent/chat/session/${encodeURIComponent(sessionId)}/stream`,
`${config.AGENT_URL}/api/v1/agent/sessions/${encodeURIComponent(sessionId)}/runs/current/events`,
{
method: "GET",
signal,
@@ -576,14 +598,8 @@ export const abortAgentChat = async (sessionId?: string) => {
return;
}
const response = await apiFetch(`${config.AGENT_URL}/api/v1/agent/chat/abort`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
session_id: sessionId,
}),
const response = await apiFetch(`${config.AGENT_URL}/api/v1/agent/sessions/${encodeURIComponent(sessionId)}/runs/current`, {
method: "DELETE",
projectHeaderMode: "include",
skipAuthRedirect: true,
});
@@ -600,14 +616,14 @@ export const replyAgentPermission = async (
reply: PermissionReply,
) => {
const response = await apiFetch(
`${config.AGENT_URL}/api/v1/agent/chat/permission/${encodeURIComponent(requestId)}/reply`,
`${config.AGENT_URL}/api/v1/agent/sessions/${encodeURIComponent(sessionId)}/permission-responses`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
session_id: sessionId,
request_id: requestId,
reply,
}),
projectHeaderMode: "include",
@@ -627,14 +643,15 @@ export const replyAgentQuestion = async (
answers: string[][],
) => {
const response = await apiFetch(
`${config.AGENT_URL}/api/v1/agent/chat/question/${encodeURIComponent(requestId)}/reply`,
`${config.AGENT_URL}/api/v1/agent/sessions/${encodeURIComponent(sessionId)}/question-responses`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
session_id: sessionId,
request_id: requestId,
action: "reply",
answers,
}),
projectHeaderMode: "include",
@@ -653,14 +670,15 @@ export const rejectAgentQuestion = async (
requestId: string,
) => {
const response = await apiFetch(
`${config.AGENT_URL}/api/v1/agent/chat/question/${encodeURIComponent(requestId)}/reject`,
`${config.AGENT_URL}/api/v1/agent/sessions/${encodeURIComponent(sessionId)}/question-responses`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
session_id: sessionId,
request_id: requestId,
action: "reject",
}),
projectHeaderMode: "include",
skipAuthRedirect: true,
@@ -674,13 +692,12 @@ export const rejectAgentQuestion = async (
};
export const forkAgentChat = async (sessionId: string | undefined, keepMessageCount: number) => {
const response = await apiFetch(`${config.AGENT_URL}/api/v1/agent/chat/fork`, {
const response = await apiFetch(`${config.AGENT_URL}/api/v1/agent/sessions/${encodeURIComponent(sessionId ?? "")}/forks`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
session_id: sessionId,
keep_message_count: keepMessageCount,
}),
projectHeaderMode: "include",