Skip to content

Commit 841db99

Browse files
committed
Adding support for messages endpoint while fetching content parts
1 parent d5b26a0 commit 841db99

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

plugins/utils.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,19 @@ export class TimeoutError extends Error {
3636
}
3737
}
3838

39+
/**
40+
* Helper function to get the text from the current content part of a request/response context
41+
* @param context - The plugin context containing request/response data
42+
* @param eventType - The type of hook event (beforeRequestHook or afterRequestHook)
43+
* @returns The text from the current content part of the request/response context
44+
*/
3945
export const getText = (
4046
context: PluginContext,
4147
eventType: HookEventType
4248
): string => {
43-
switch (eventType) {
44-
case 'beforeRequestHook':
45-
return context.request?.text;
46-
case 'afterRequestHook':
47-
return context.response?.text;
48-
default:
49-
throw new Error('Invalid hook type');
50-
}
49+
return getCurrentContentPart(context, eventType)
50+
.textArray.filter((text) => text)
51+
.join('\n');
5152
};
5253

5354
/**
@@ -87,6 +88,9 @@ const getRequestContentPart = (json: any, requestType: string) => {
8788
textArray = Array.isArray(content)
8889
? content.map((item: any) => item)
8990
: [content];
91+
} else if (requestType === 'embed') {
92+
content = json.input;
93+
textArray = Array.isArray(content) ? content : [content];
9094
}
9195
return { content, textArray };
9296
};
@@ -95,6 +99,11 @@ const getResponseContentPart = (json: any, requestType: string) => {
9599
let content: Array<any> | string | Record<string, any> | null = null;
96100
let textArray: Array<string> = [];
97101

102+
// This can happen for streaming mode.
103+
if (!json) {
104+
return { content: null, textArray: [] };
105+
}
106+
98107
if (requestType === 'chatComplete') {
99108
content = json.choices[0].message.content as string;
100109
textArray = [content];

0 commit comments

Comments
 (0)