@@ -44,11 +44,15 @@ func (p *jsHandlerPlugin) allScriptPaths() []string {
4444 return paths
4545}
4646
47- func (p * jsHandlerPlugin ) InterceptRequest (ctx context.Context , req pluginapi.RequestInterceptRequest ) (pluginapi.RequestInterceptResponse , error ) {
48- return p .interceptRequest (ctx , req , "" )
47+ func (p * jsHandlerPlugin ) InterceptRequestBeforeAuth (ctx context.Context , req pluginapi.RequestInterceptRequest ) (pluginapi.RequestInterceptResponse , error ) {
48+ return p .interceptRequest (ctx , req , "on_before_request" , " " )
4949}
5050
51- func (p * jsHandlerPlugin ) interceptRequest (ctx context.Context , req pluginapi.RequestInterceptRequest , hostCallbackID string ) (pluginapi.RequestInterceptResponse , error ) {
51+ func (p * jsHandlerPlugin ) InterceptRequestAfterAuth (ctx context.Context , req pluginapi.RequestInterceptRequest ) (pluginapi.RequestInterceptResponse , error ) {
52+ return p .interceptRequest (ctx , req , "on_after_auth_request" , "" )
53+ }
54+
55+ func (p * jsHandlerPlugin ) interceptRequest (ctx context.Context , req pluginapi.RequestInterceptRequest , hookName , hostCallbackID string ) (pluginapi.RequestInterceptResponse , error ) {
5256 resp := pluginapi.RequestInterceptResponse {}
5357 scriptPaths := p .allScriptPaths ()
5458 if len (scriptPaths ) == 0 {
@@ -64,7 +68,7 @@ func (p *jsHandlerPlugin) interceptRequest(ctx context.Context, req pluginapi.Re
6468 if scriptPath == "" {
6569 continue
6670 }
67- processed , cleared , errJS := p .applyJSBeforeRequest (scriptPath , []byte (body ), req .Model , req .SourceFormat , headers , hostCallbackID )
71+ processed , cleared , errJS := p .applyJSRequestHook (scriptPath , hookName , []byte (body ), req .Model , req .SourceFormat , req . ToFormat , headers , hostCallbackID )
6872 if errJS != nil {
6973 log .Warnf ("failed to execute JS request interceptor [%s]: %v" , scriptPath , errJS )
7074 continue
@@ -197,7 +201,7 @@ func (p *jsHandlerPlugin) interceptStreamChunk(ctx context.Context, req pluginap
197201 return resp , nil
198202}
199203
200- func (p * jsHandlerPlugin ) applyJSBeforeRequest (scriptPath string , payloadBytes []byte , model , protocol string , headers http.Header , hostCallbackID string ) ([]byte , []string , error ) {
204+ func (p * jsHandlerPlugin ) applyJSRequestHook (scriptPath , hookName string , payloadBytes []byte , model , sourceFormat , toFormat string , headers http.Header , hostCallbackID string ) ([]byte , []string , error ) {
201205 program , err := getJSProgram (scriptPath )
202206 if err != nil {
203207 return nil , nil , err
@@ -211,20 +215,24 @@ func (p *jsHandlerPlugin) applyJSBeforeRequest(scriptPath string, payloadBytes [
211215 headersMap := headerToAnyMap (headers )
212216
213217 jsCtx := map [string ]any {
214- "id" : generateRequestID (),
215- "body" : string (payloadBytes ),
216- "headers" : headersMap ,
217- "url" : "" ,
218- "model" : model ,
219- "protocol" : protocol ,
218+ "id" : generateRequestID (),
219+ "body" : string (payloadBytes ),
220+ "headers" : headersMap ,
221+ "url" : "" ,
222+ "model" : model ,
223+ "protocol" : sourceFormat ,
224+ "source_format" : sourceFormat ,
225+ "to_format" : toFormat ,
226+ "sourceFormat" : sourceFormat ,
227+ "toFormat" : toFormat ,
220228 }
221229
222- jsVal , errCall := engine .callFunction ("on_before_request" , p .cfg .Timeout , jsCtx )
230+ jsVal , errCall := engine .callFunction (hookName , p .cfg .Timeout , jsCtx )
223231 if errCall != nil {
224232 if errors .Is (errCall , ErrFunctionNotFound ) {
225233 return payloadBytes , nil , nil
226234 }
227- return nil , nil , fmt .Errorf ("on_before_request failed for %s: %w" , scriptPath , errCall )
235+ return nil , nil , fmt .Errorf ("%s failed for %s: %w" , hookName , scriptPath , errCall )
228236 }
229237
230238 if jsVal == nil || goja .IsUndefined (jsVal ) || goja .IsNull (jsVal ) {
0 commit comments