@@ -3,6 +3,7 @@ import type { Context } from "hono"
33import { streamSSE } from "hono/streaming"
44
55import { awaitApproval } from "~/lib/approval"
6+ import { getConfig } from "~/lib/config"
67import { createHandlerLogger } from "~/lib/logger"
78import { checkRateLimit } from "~/lib/rate-limit"
89import { state } from "~/lib/state"
@@ -24,6 +25,8 @@ export const handleResponses = async (c: Context) => {
2425 const payload = await c . req . json < ResponsesPayload > ( )
2526 logger . debug ( "Responses request payload:" , JSON . stringify ( payload ) )
2627
28+ useFunctionApplyPatch ( payload )
29+
2730 const selectedModel = state . models ?. data . find (
2831 ( model ) => model . id === payload . model ,
2932 )
@@ -78,3 +81,35 @@ const isAsyncIterable = <T>(value: unknown): value is AsyncIterable<T> =>
7881
7982const isStreamingRequested = ( payload : ResponsesPayload ) : boolean =>
8083 Boolean ( payload . stream )
84+
85+ const useFunctionApplyPatch = ( payload : ResponsesPayload ) : void => {
86+ const config = getConfig ( )
87+ const useFunctionApplyPatch = config . useFunctionApplyPatch ?? true
88+ if ( useFunctionApplyPatch ) {
89+ logger . debug ( "Using function tool apply_patch for responses" )
90+ if ( Array . isArray ( payload . tools ) ) {
91+ const toolsArr = payload . tools
92+ for ( let i = 0 ; i < toolsArr . length ; i ++ ) {
93+ const t = toolsArr [ i ]
94+ if ( t . type === "custom" && t . name === "apply_patch" ) {
95+ toolsArr [ i ] = {
96+ type : "function" ,
97+ name : t . name ,
98+ description : "Use the `apply_patch` tool to edit files" ,
99+ parameters : {
100+ type : "object" ,
101+ properties : {
102+ input : {
103+ type : "string" ,
104+ description : "The entire contents of the apply_patch command" ,
105+ } ,
106+ } ,
107+ required : [ "input" ] ,
108+ } ,
109+ strict : false ,
110+ }
111+ }
112+ }
113+ }
114+ }
115+ }
0 commit comments