适配新的 opencode Agent 框架

This commit is contained in:
2026-04-29 15:33:08 +08:00
parent 49fd4f5eb1
commit 3b5a493cda
10 changed files with 53 additions and 53 deletions
+14 -14
View File
@@ -1,4 +1,4 @@
import { streamCopilotChat } from "./chatStream";
import { streamAgentChat } from "./chatStream";
import { ReadableStream } from "stream/web";
import { TextEncoder, TextDecoder } from "util";
@@ -32,7 +32,7 @@ const makeStream = (chunks: string[]) =>
},
});
describe("streamCopilotChat", () => {
describe("streamAgentChat", () => {
beforeEach(() => {
apiFetch.mockReset();
});
@@ -41,21 +41,21 @@ describe("streamCopilotChat", () => {
apiFetch.mockResolvedValue({
ok: true,
body: makeStream([
'event: token\ndata: {"conversationId":"c1","content":"he"}\n\n',
'event: token\ndata: {"conversationId":"c1","content":"llo"}\n\n',
'event: done\ndata: {"conversationId":"c1"}\n\n',
'event: token\ndata: {"session_id":"s1","content":"he"}\n\n',
'event: token\ndata: {"session_id":"s1","content":"llo"}\n\n',
'event: done\ndata: {"session_id":"s1"}\n\n',
]),
});
const events: Array<{ type: string; content?: string; conversationId?: string }> = [];
const events: Array<{ type: string; content?: string; sessionId?: string }> = [];
await streamCopilotChat({
await streamAgentChat({
message: "hi",
onEvent: (event) => events.push(event),
});
expect(apiFetch).toHaveBeenCalledWith(
expect.stringContaining("/api/v1/copilot/chat/stream"),
expect.stringContaining("/api/v1/agent/chat/stream"),
expect.objectContaining({
method: "POST",
projectHeaderMode: "include",
@@ -64,9 +64,9 @@ describe("streamCopilotChat", () => {
);
expect(events).toEqual([
{ type: "token", conversationId: "c1", content: "he" },
{ type: "token", conversationId: "c1", content: "llo" },
{ type: "done", conversationId: "c1" },
{ type: "token", sessionId: "s1", content: "he" },
{ type: "token", sessionId: "s1", content: "llo" },
{ type: "done", sessionId: "s1" },
]);
});
@@ -78,7 +78,7 @@ describe("streamCopilotChat", () => {
});
const events: Array<{ type: string; message?: string; detail?: string }> = [];
await streamCopilotChat({
await streamAgentChat({
message: "hi",
onEvent: (event) => events.push(event),
});
@@ -97,7 +97,7 @@ describe("streamCopilotChat", () => {
});
const events: Array<{ type: string; message?: string; detail?: string }> = [];
await streamCopilotChat({
await streamAgentChat({
message: "hi",
onEvent: (event) => events.push(event),
});
@@ -111,7 +111,7 @@ describe("streamCopilotChat", () => {
apiFetch.mockRejectedValue(new TypeError("Failed to fetch"));
const events: Array<{ type: string; message?: string; detail?: string }> = [];
await streamCopilotChat({
await streamAgentChat({
message: "hi",
onEvent: (event) => events.push(event),
});
+14 -14
View File
@@ -2,24 +2,24 @@ import { apiFetch } from "@/lib/apiFetch";
import { config } from "@config/config";
export type StreamEvent =
| { type: "token"; conversationId: string; content: string }
| { type: "done"; conversationId: string }
| { type: "token"; sessionId: string; content: string }
| { type: "done"; sessionId: string }
| {
type: "error";
conversationId?: string;
sessionId?: string;
message: string;
detail?: string;
}
| {
type: "tool_call";
conversationId: string;
sessionId: string;
tool: string;
params: Record<string, unknown>;
};
type StreamOptions = {
message: string;
conversationId?: string;
sessionId?: string;
signal?: AbortSignal;
onEvent: (event: StreamEvent) => void;
};
@@ -43,16 +43,16 @@ const parseEventBlock = (block: string): { event?: string; data?: string } => {
};
};
export const streamCopilotChat = async ({
export const streamAgentChat = async ({
message,
conversationId,
sessionId,
signal,
onEvent,
}: StreamOptions) => {
let response: Response;
try {
response = await apiFetch(
`${config.COPILOT_URL}/api/v1/copilot/chat/stream`,
`${config.AGENT_URL}/api/v1/agent/chat/stream`,
{
method: "POST",
signal,
@@ -62,7 +62,7 @@ export const streamCopilotChat = async ({
},
body: JSON.stringify({
message,
conversation_id: conversationId,
session_id: sessionId,
}),
projectHeaderMode: "include",
skipAuthRedirect: true,
@@ -115,7 +115,7 @@ export const streamCopilotChat = async ({
try {
const parsed = JSON.parse(data) as {
conversationId?: string;
session_id?: string;
content?: string;
message?: string;
detail?: string;
@@ -125,25 +125,25 @@ export const streamCopilotChat = async ({
if (event === "token") {
onEvent({
type: "token",
conversationId: parsed.conversationId ?? "",
sessionId: parsed.session_id ?? "",
content: parsed.content ?? "",
});
} else if (event === "done") {
onEvent({
type: "done",
conversationId: parsed.conversationId ?? "",
sessionId: parsed.session_id ?? "",
});
} else if (event === "error") {
onEvent({
type: "error",
conversationId: parsed.conversationId,
sessionId: parsed.session_id,
message: parsed.message ?? "unknown error",
detail: parsed.detail,
});
} else if (event === "tool_call") {
onEvent({
type: "tool_call",
conversationId: parsed.conversationId ?? "",
sessionId: parsed.session_id ?? "",
tool: parsed.tool ?? "",
params: parsed.params ?? {},
});