feat(orchestrator): add OpenClaw SSE bridge streaming
All checks were successful
ci/woodpecker/push/ci Pipeline was successful
All checks were successful
ci/woodpecker/push/ci Pipeline was successful
This commit is contained in:
@@ -12,6 +12,7 @@ import type {
|
||||
import type { AgentProviderConfig } from "@prisma/client";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { EncryptionService } from "../../../security/encryption.service";
|
||||
import { OpenClawSseBridge } from "./openclaw-sse.bridge";
|
||||
|
||||
const DEFAULT_SESSION_LIMIT = 50;
|
||||
const DEFAULT_MESSAGE_LIMIT = 50;
|
||||
@@ -21,7 +22,6 @@ const API_TOKEN_KEYS = ["apiToken", "token", "bearerToken"] as const;
|
||||
const DISPLAY_NAME_KEYS = ["displayName", "label"] as const;
|
||||
|
||||
type JsonRecord = Record<string, unknown>;
|
||||
type AsyncChunkStream = AsyncIterable<string | Uint8Array | Buffer>;
|
||||
|
||||
interface HttpErrorWithResponse {
|
||||
response?: {
|
||||
@@ -38,7 +38,8 @@ export class OpenClawProvider implements IAgentProvider {
|
||||
constructor(
|
||||
private readonly config: AgentProviderConfig,
|
||||
private readonly encryptionService: EncryptionService,
|
||||
private readonly httpService: HttpService
|
||||
private readonly httpService: HttpService,
|
||||
private readonly sseBridge: OpenClawSseBridge
|
||||
) {
|
||||
this.providerId = this.config.name;
|
||||
this.displayName = this.resolveDisplayName();
|
||||
@@ -196,64 +197,7 @@ export class OpenClawProvider implements IAgentProvider {
|
||||
|
||||
async *streamMessages(sessionId: string): AsyncIterable<AgentMessage> {
|
||||
try {
|
||||
const response = await this.httpService.axiosRef.get(
|
||||
this.buildUrl(`/api/sessions/${encodeURIComponent(sessionId)}/stream`),
|
||||
{
|
||||
headers: this.authHeaders({
|
||||
Accept: "text/event-stream",
|
||||
}),
|
||||
responseType: "stream",
|
||||
}
|
||||
);
|
||||
|
||||
const stream = this.asAsyncChunkStream(response.data);
|
||||
if (!stream) {
|
||||
throw new Error("OpenClaw stream response is not readable");
|
||||
}
|
||||
|
||||
const decoder = new TextDecoder();
|
||||
let buffer = "";
|
||||
let streamDone = false;
|
||||
|
||||
for await (const chunk of stream) {
|
||||
const textChunk =
|
||||
typeof chunk === "string" ? chunk : decoder.decode(chunk, { stream: true });
|
||||
buffer += textChunk.replace(/\r\n/gu, "\n");
|
||||
|
||||
const events = buffer.split("\n\n");
|
||||
buffer = events.pop() ?? "";
|
||||
|
||||
for (const event of events) {
|
||||
const data = this.extractSseData(event);
|
||||
if (data === null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (data === "[DONE]") {
|
||||
streamDone = true;
|
||||
break;
|
||||
}
|
||||
|
||||
const message = this.toAgentMessage(this.tryParseJson(data) ?? data, sessionId);
|
||||
if (message !== null) {
|
||||
yield message;
|
||||
}
|
||||
}
|
||||
|
||||
if (streamDone) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!streamDone && buffer.trim().length > 0) {
|
||||
const data = this.extractSseData(buffer.trim());
|
||||
if (data !== null && data !== "[DONE]") {
|
||||
const message = this.toAgentMessage(this.tryParseJson(data) ?? data, sessionId);
|
||||
if (message !== null) {
|
||||
yield message;
|
||||
}
|
||||
}
|
||||
}
|
||||
yield* this.sseBridge.streamSession(this.resolveBaseUrl(), sessionId, this.authHeaders());
|
||||
} catch (error) {
|
||||
throw this.toServiceUnavailable(`stream messages for session ${sessionId}`, error);
|
||||
}
|
||||
@@ -631,40 +575,6 @@ export class OpenClawProvider implements IAgentProvider {
|
||||
return new URL(path, `${this.resolveBaseUrl()}/`).toString();
|
||||
}
|
||||
|
||||
private extractSseData(rawEvent: string): string | null {
|
||||
const lines = rawEvent.split("\n");
|
||||
const dataLines = lines
|
||||
.filter((line) => line.startsWith("data:"))
|
||||
.map((line) => line.slice(5).trimStart());
|
||||
|
||||
if (dataLines.length > 0) {
|
||||
return dataLines.join("\n").trim();
|
||||
}
|
||||
|
||||
const trimmed = rawEvent.trim();
|
||||
if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private tryParseJson(value: string): unknown {
|
||||
try {
|
||||
return JSON.parse(value) as unknown;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private asAsyncChunkStream(value: unknown): AsyncChunkStream | null {
|
||||
if (value !== null && typeof value === "object" && Symbol.asyncIterator in value) {
|
||||
return value as AsyncChunkStream;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private isRecord(value: unknown): value is JsonRecord {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user