@@ -36,18 +36,19 @@ export class TimeoutError extends Error {
36
36
}
37
37
}
38
38
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
+ */
39
45
export const getText = (
40
46
context : PluginContext ,
41
47
eventType : HookEventType
42
48
) : 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' ) ;
51
52
} ;
52
53
53
54
/**
@@ -87,6 +88,9 @@ const getRequestContentPart = (json: any, requestType: string) => {
87
88
textArray = Array . isArray ( content )
88
89
? content . map ( ( item : any ) => item )
89
90
: [ content ] ;
91
+ } else if ( requestType === 'embed' ) {
92
+ content = json . input ;
93
+ textArray = Array . isArray ( content ) ? content : [ content ] ;
90
94
}
91
95
return { content, textArray } ;
92
96
} ;
@@ -95,6 +99,11 @@ const getResponseContentPart = (json: any, requestType: string) => {
95
99
let content : Array < any > | string | Record < string , any > | null = null ;
96
100
let textArray : Array < string > = [ ] ;
97
101
102
+ // This can happen for streaming mode.
103
+ if ( ! json ) {
104
+ return { content : null , textArray : [ ] } ;
105
+ }
106
+
98
107
if ( requestType === 'chatComplete' ) {
99
108
content = json . choices [ 0 ] . message . content as string ;
100
109
textArray = [ content ] ;
0 commit comments