Files
OmniRoute/open-sse/handlers/chatCore/pluginOnResponse.ts
Diego Rodrigues de Sa e Souza 4e6f808b43 feat(plugins): add onStreamComplete built-in event exposing streaming usage and timing (#9571) (#9669)
* feat(plugins): add onStreamComplete built-in event exposing streaming usage and timing (#9571)

* fix(changelog): remove YAML frontmatter from 9571 fragment

The changelog fragment format requires the first non-empty line to be
a markdown bullet ("- "). YAML frontmatter was the first non-empty
line, causing the integrity check to fail.

---------

Co-authored-by: diegosouzapw <diegosouzapw@users.noreply.github.com>
Co-authored-by: backryun <bakryun0718@proton.me>
2026-08-12 02:15:57 -03:00

102 lines
3.2 KiB
TypeScript

/**
* chatCore plugin onResponse hook (Quality Gate v2 / Fase 9 — chatCore god-file decomposition,
* #3501).
*
* Extracted from handleChatCore's streaming finalization: runs the registered plugin `onResponse`
* hooks for a completed response. Fire-and-forget and fail-open — the inner run is not awaited
* and both the dynamic import and the run swallow their own errors, so a misbehaving plugin never
* affects the response. Non-streaming callers pass the translated JSON body as `data`; streaming
* callers pass `{ streamed: true }` because the SSE body is not materialized yet (#8711).
*/
/** Payload passed to plugin onResponse hooks from chatCore success paths. */
export type PluginOnResponsePayload = {
status: number;
/** Client-facing JSON body (non-streaming only). */
data?: unknown;
/** True when the handler returns an SSE stream whose body is not yet available. */
streamed?: boolean;
};
export async function runPluginOnResponseHook(args: {
requestId: string;
body: unknown;
model: string | null | undefined;
provider: string | null | undefined;
apiKeyInfo: unknown;
headers?: Record<string, string | string[] | undefined>;
response: PluginOnResponsePayload;
}): Promise<void> {
try {
const { runOnResponse } = await import("@/lib/plugins/hooks");
runOnResponse(
{
requestId: args.requestId,
body: args.body,
model: args.model,
provider: args.provider,
apiKeyInfo: args.apiKeyInfo,
headers: args.headers,
metadata: {},
},
args.response
).catch(() => {});
} catch (_) {
/* plugin onResponse optional */
}
}
/**
* Payload passed to plugin onStreamComplete hooks after a streaming response is consumed.
* Carries usage token counts, timing metrics (latency, TTFT), model, provider, and error code.
*/
export type PluginOnStreamCompletePayload = {
status: number;
usage?: {
prompt_tokens?: number;
completion_tokens?: number;
reasoning_tokens?: number;
cache_read_input_tokens?: number;
cache_creation_input_tokens?: number;
};
timing?: {
latencyMs: number;
ttft?: number;
};
model?: string;
provider?: string;
errorCode?: string;
};
/**
* Run plugin onStreamComplete hooks — fire-and-forget and fail-open.
* Called inside the onStreamComplete callback (chatCore.ts) where usage and timing data
* converge after an SSE stream is fully consumed.
*/
export async function runPluginOnStreamCompleteHook(args: {
status: number;
usage?: Record<string, unknown>;
ttft?: number;
model: string | null | undefined;
provider: string | null | undefined;
errorCode?: string | null | undefined;
startTime: number;
}): Promise<void> {
try {
const { runOnStreamComplete } = await import("@/lib/plugins/hooks");
runOnStreamComplete({
status: args.status,
usage: args.usage as PluginOnStreamCompletePayload["usage"],
timing: {
latencyMs: Date.now() - args.startTime,
ttft: args.ttft,
},
model: args.model ?? undefined,
provider: args.provider ?? undefined,
errorCode: args.errorCode ?? undefined,
}).catch(() => {});
} catch (_) {
/* plugin onStreamComplete optional */
}
}