feat(chat): refresh agent credentials during tool calls
This commit is contained in:
@@ -2,6 +2,7 @@ import {
|
||||
abortAgentChat,
|
||||
forkAgentChat,
|
||||
rejectAgentQuestion,
|
||||
replyAgentCredentialRefresh,
|
||||
replyAgentPermission,
|
||||
replyAgentQuestion,
|
||||
type StreamEvent,
|
||||
@@ -171,6 +172,32 @@ describe("streamAgentChat", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("parses credential refresh lifecycle events", async () => {
|
||||
mockNewSessionStream({
|
||||
ok: true,
|
||||
body: makeStream([
|
||||
'event: credential_refresh_required\ndata: {"session_id":"s1","request_id":"credential-1","reason":"access_token_rejected","timeout_ms":30000}\n\n',
|
||||
'event: credential_refreshed\ndata: {"session_id":"s1","request_id":"credential-1"}\n\n',
|
||||
]),
|
||||
});
|
||||
const events: StreamEvent[] = [];
|
||||
await streamAgentChat({ message: "hi", onEvent: (event) => events.push(event) });
|
||||
expect(events).toEqual([
|
||||
{
|
||||
type: "credential_refresh_required",
|
||||
sessionId: "s1",
|
||||
requestId: "credential-1",
|
||||
reason: "access_token_rejected",
|
||||
timeoutMs: 30000,
|
||||
},
|
||||
{
|
||||
type: "credential_refreshed",
|
||||
sessionId: "s1",
|
||||
requestId: "credential-1",
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it("parses tool_call arguments when params is empty", async () => {
|
||||
mockNewSessionStream({
|
||||
ok: true,
|
||||
@@ -388,6 +415,20 @@ describe("streamAgentChat", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("submits refreshed credentials through the authenticated context", async () => {
|
||||
apiFetch.mockResolvedValue({ ok: true, status: 202, text: async () => "" });
|
||||
await replyAgentCredentialRefresh("s1", "credential-1");
|
||||
expect(apiFetch).toHaveBeenCalledWith(
|
||||
expect.stringContaining("/api/v1/agent/sessions/s1/credential-refreshes"),
|
||||
expect.objectContaining({
|
||||
method: "POST",
|
||||
body: JSON.stringify({ request_id: "credential-1" }),
|
||||
projectHeaderMode: "include",
|
||||
skipAuthRedirect: true,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("calls question reply and reject endpoints", async () => {
|
||||
apiFetch.mockResolvedValue({
|
||||
ok: true,
|
||||
|
||||
@@ -90,6 +90,24 @@ export type StreamEvent =
|
||||
reason?: string;
|
||||
message: string;
|
||||
}
|
||||
| {
|
||||
type: "credential_refresh_required";
|
||||
sessionId: string;
|
||||
requestId: string;
|
||||
reason?: string;
|
||||
timeoutMs?: number;
|
||||
}
|
||||
| {
|
||||
type: "credential_refreshed";
|
||||
sessionId: string;
|
||||
requestId: string;
|
||||
}
|
||||
| {
|
||||
type: "credential_refresh_failed";
|
||||
sessionId: string;
|
||||
requestId: string;
|
||||
message: string;
|
||||
}
|
||||
| {
|
||||
type: "tool_call";
|
||||
sessionId: string;
|
||||
@@ -310,6 +328,7 @@ const emitParsedStreamEvent = (
|
||||
message_id?: string;
|
||||
todos?: unknown;
|
||||
reason?: string;
|
||||
timeout_ms?: number;
|
||||
};
|
||||
if (event === "state") {
|
||||
onEvent({
|
||||
@@ -366,6 +385,27 @@ const emitParsedStreamEvent = (
|
||||
reason: parsed.reason,
|
||||
message: parsed.message ?? "登录态已过期,请刷新登录后重试",
|
||||
});
|
||||
} else if (event === "credential_refresh_required") {
|
||||
onEvent({
|
||||
type: "credential_refresh_required",
|
||||
sessionId: parsed.session_id ?? "",
|
||||
requestId: parsed.request_id ?? "",
|
||||
reason: parsed.reason,
|
||||
timeoutMs: parsed.timeout_ms,
|
||||
});
|
||||
} else if (event === "credential_refreshed") {
|
||||
onEvent({
|
||||
type: "credential_refreshed",
|
||||
sessionId: parsed.session_id ?? "",
|
||||
requestId: parsed.request_id ?? "",
|
||||
});
|
||||
} else if (event === "credential_refresh_failed") {
|
||||
onEvent({
|
||||
type: "credential_refresh_failed",
|
||||
sessionId: parsed.session_id ?? "",
|
||||
requestId: parsed.request_id ?? "",
|
||||
message: parsed.message ?? "登录凭据续期失败",
|
||||
});
|
||||
} else if (event === "tool_call") {
|
||||
onEvent({
|
||||
type: "tool_call",
|
||||
@@ -637,6 +677,26 @@ export const replyAgentPermission = async (
|
||||
}
|
||||
};
|
||||
|
||||
export const replyAgentCredentialRefresh = async (
|
||||
sessionId: string,
|
||||
requestId: string,
|
||||
) => {
|
||||
const response = await apiFetch(
|
||||
`${config.AGENT_URL}/api/v1/agent/sessions/${encodeURIComponent(sessionId)}/credential-refreshes`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ request_id: requestId }),
|
||||
projectHeaderMode: "include",
|
||||
skipAuthRedirect: true,
|
||||
},
|
||||
);
|
||||
if (!response.ok) {
|
||||
const detail = await response.text();
|
||||
throw new Error(detail || `credential refresh reply failed: ${response.status}`);
|
||||
}
|
||||
};
|
||||
|
||||
export const replyAgentQuestion = async (
|
||||
sessionId: string,
|
||||
requestId: string,
|
||||
|
||||
Reference in New Issue
Block a user