Skip to content

Commit 69e18a9

Browse files
committed
update check to send hook results in streaming
1 parent a627e55 commit 69e18a9

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

src/handlers/streamHandler.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ function readUInt32BE(buffer: Uint8Array, offset: number) {
2626
); // Ensure the result is an unsigned integer
2727
}
2828

29+
const shouldSendHookResultChunk = (
30+
strictOpenAiCompliance: boolean,
31+
hooksResult: HookSpan['hooksResult']
32+
) => {
33+
return (
34+
!strictOpenAiCompliance && hooksResult?.beforeRequestHooksResult?.length > 0
35+
);
36+
};
37+
2938
function getPayloadFromAWSChunk(chunk: Uint8Array): string {
3039
const decoder = new TextDecoder();
3140
const chunkLength = readUInt32BE(chunk, 0);
@@ -315,7 +324,7 @@ export function handleStreamingMode(
315324
if (proxyProvider === BEDROCK) {
316325
(async () => {
317326
try {
318-
if (!strictOpenAiCompliance) {
327+
if (shouldSendHookResultChunk(strictOpenAiCompliance, hooksResult)) {
319328
const hookResultChunk = constructHookResultChunk(hooksResult, fn);
320329
if (hookResultChunk) {
321330
await writer.write(encoder.encode(hookResultChunk));
@@ -347,7 +356,7 @@ export function handleStreamingMode(
347356
} else {
348357
(async () => {
349358
try {
350-
if (!strictOpenAiCompliance) {
359+
if (shouldSendHookResultChunk(strictOpenAiCompliance, hooksResult)) {
351360
const hookResultChunk = constructHookResultChunk(hooksResult, fn);
352361
if (hookResultChunk) {
353362
await writer.write(encoder.encode(hookResultChunk));
@@ -422,7 +431,7 @@ export async function handleJSONToStreamResponse(
422431
) {
423432
const generator = responseTransformerFunction(responseJSON, provider);
424433
(async () => {
425-
if (!strictOpenAiCompliance) {
434+
if (shouldSendHookResultChunk(strictOpenAiCompliance, hooksResult)) {
426435
const hookResultChunk = constructHookResultChunk(hooksResult, fn);
427436
if (hookResultChunk) {
428437
await writer.write(encoder.encode(hookResultChunk));
@@ -443,7 +452,7 @@ export async function handleJSONToStreamResponse(
443452
provider
444453
);
445454
(async () => {
446-
if (!strictOpenAiCompliance) {
455+
if (shouldSendHookResultChunk(strictOpenAiCompliance, hooksResult)) {
447456
const hookResultChunk = constructHookResultChunk(hooksResult, fn);
448457
if (hookResultChunk) {
449458
await writer.write(encoder.encode(hookResultChunk));
@@ -470,18 +479,16 @@ const constructHookResultChunk = (
470479
hooksResult: HookSpan['hooksResult'],
471480
fn: endpointStrings
472481
) => {
473-
if (fn === 'chatComplete' || fn === 'complete' || fn === 'embed') {
474-
return `data: ${JSON.stringify({
475-
hook_results: {
476-
before_request_hooks: hooksResult.beforeRequestHooksResult,
477-
},
478-
})}\n\n`;
479-
} else if (fn === 'messages') {
482+
if (fn === 'messages') {
480483
return `event: hook_results\ndata: ${JSON.stringify({
481484
hook_results: {
482485
before_request_hooks: hooksResult.beforeRequestHooksResult,
483486
},
484487
})}\n\n`;
485488
}
486-
return null;
489+
return `data: ${JSON.stringify({
490+
hook_results: {
491+
before_request_hooks: hooksResult.beforeRequestHooksResult,
492+
},
493+
})}\n\n`;
487494
};

0 commit comments

Comments
 (0)