refactor: share openai realtime close capture

This commit is contained in:
Peter Steinberger
2026-04-20 15:40:03 +01:00
parent b5a16e263d
commit 629b5b034a
3 changed files with 35 additions and 24 deletions

View File

@@ -1,3 +1,4 @@
import { captureWsEvent } from "openclaw/plugin-sdk/proxy-capture";
import { normalizeOptionalString } from "openclaw/plugin-sdk/text-runtime";
export const trimToUndefined = normalizeOptionalString;
@@ -31,3 +32,27 @@ export function resolveOpenAIProviderConfigRecord(
asObjectRecord(providers?.openai) ?? asObjectRecord(config.openai) ?? asObjectRecord(config)
);
}
export function captureOpenAIRealtimeWsClose(params: {
url: string;
flowId: string;
capability: "realtime-transcription" | "realtime-voice";
code: unknown;
reasonBuffer: unknown;
}): void {
captureWsEvent({
url: params.url,
direction: "local",
kind: "ws-close",
flowId: params.flowId,
closeCode: typeof params.code === "number" ? params.code : undefined,
meta: {
provider: "openai",
capability: params.capability,
reason:
Buffer.isBuffer(params.reasonBuffer) && params.reasonBuffer.length > 0
? params.reasonBuffer.toString("utf8")
: undefined,
},
});
}

View File

@@ -14,6 +14,7 @@ import { normalizeResolvedSecretInputString } from "openclaw/plugin-sdk/secret-i
import WebSocket from "ws";
import {
asFiniteNumber,
captureOpenAIRealtimeWsClose,
readRealtimeErrorDetail,
resolveOpenAIProviderConfigRecord,
trimToUndefined,
@@ -192,20 +193,12 @@ class OpenAIRealtimeTranscriptionSession implements RealtimeTranscriptionSession
});
this.ws.on("close", (code, reasonBuffer) => {
captureWsEvent({
captureOpenAIRealtimeWsClose({
url,
direction: "local",
kind: "ws-close",
flowId: this.flowId,
closeCode: typeof code === "number" ? code : undefined,
meta: {
provider: "openai",
capability: "realtime-transcription",
reason:
Buffer.isBuffer(reasonBuffer) && reasonBuffer.length > 0
? reasonBuffer.toString("utf8")
: undefined,
},
capability: "realtime-transcription",
code,
reasonBuffer,
});
this.connected = false;
if (this.closed) {

View File

@@ -15,6 +15,7 @@ import { normalizeResolvedSecretInputString } from "openclaw/plugin-sdk/secret-i
import WebSocket from "ws";
import {
asFiniteNumber,
captureOpenAIRealtimeWsClose,
readRealtimeErrorDetail,
resolveOpenAIProviderConfigRecord,
trimToUndefined,
@@ -293,20 +294,12 @@ class OpenAIRealtimeVoiceBridge implements RealtimeVoiceBridge {
});
this.ws.on("close", (code, reasonBuffer) => {
captureWsEvent({
captureOpenAIRealtimeWsClose({
url,
direction: "local",
kind: "ws-close",
flowId: this.flowId,
closeCode: typeof code === "number" ? code : undefined,
meta: {
provider: "openai",
capability: "realtime-voice",
reason:
Buffer.isBuffer(reasonBuffer) && reasonBuffer.length > 0
? reasonBuffer.toString("utf8")
: undefined,
},
capability: "realtime-voice",
code,
reasonBuffer,
});
this.connected = false;
if (this.intentionallyClosed) {