9898--- @param str string The string to encode
9999--- @return string encoded_string The URL-encoded string
100100function M .url_encode (str )
101- if not str then return ' ' end
101+ if not str then
102+ return ' '
103+ end
102104 str = tostring (str )
103105 str = string.gsub (str , ' \n ' , ' \r\n ' )
104106 str = string.gsub (str , ' ([^%w%-%.%_%~])' , function (c )
@@ -114,10 +116,10 @@ function M.apply_path_map(path)
114116 if not path then
115117 return path
116118 end
117-
119+
118120 local config = require (' opencode.config' )
119121 local path_map = config .server .path_map
120-
122+
121123 if type (path_map ) == ' function' then
122124 local ok , result = pcall (path_map , path )
123125 if ok and result then
@@ -134,44 +136,46 @@ function M.apply_path_map(path)
134136 return path_map .. relative_path
135137 end
136138 end
137-
139+
138140 return path
139141end
140142
141143function M .apply_reverse_path_map (path )
142144 if not path then
143145 return path
144146 end
145-
147+
146148 local config = require (' opencode.config' )
147149 local reverse_path_map = config .server .reverse_path_map
148-
150+
149151 if type (reverse_path_map ) == ' function' then
150152 local ok , result = pcall (reverse_path_map , path )
151153 if ok and result then
152154 return result
153155 end
154156 return path
155157 end
156-
158+
157159 return path
158160end
159161
160162function M .reverse_transform_paths_recursive (data )
163+ local config = require (' opencode.config' )
164+ local reverse_path_map = config .server .reverse_path_map
165+ if not reverse_path_map or type (reverse_path_map ) ~= ' function' then
166+ return data
167+ end
168+
161169 if type (data ) ~= ' table' then
162170 return data
163171 end
164172
165173 local result = {}
166174 for key , value in pairs (data ) do
167- if type (value ) == ' string' and (
168- key == ' filePath' or
169- key == ' path' or
170- key == ' file' or
171- key == ' directory' or
172- key == ' cwd' or
173- key == ' root'
174- ) then
175+ if
176+ type (value ) == ' string'
177+ and (key == ' filePath' or key == ' path' or key == ' file' or key == ' directory' or key == ' cwd' or key == ' root' )
178+ then
175179 result [key ] = M .apply_reverse_path_map (value )
176180 elseif type (value ) == ' table' and (key == ' files' or key == ' deleted_files' ) then
177181 result [key ] = vim .tbl_map (M .apply_reverse_path_map , value )
@@ -184,6 +188,33 @@ function M.reverse_transform_paths_recursive(data)
184188 return result
185189end
186190
191+ --- Transform file paths in API payloads using configured path_map
192+ --- @param data any The data to transform (table , string , or other )
193+ --- @return any transformed_data The data with paths transformed
194+ function M .transform_paths_recursive (data )
195+ local config = require (' opencode.config' )
196+ local path_map = config .server .path_map
197+ if not path_map or type (path_map ) ~= ' function' then
198+ return data
199+ end
200+
201+ if type (data ) ~= ' table' then
202+ return data
203+ end
204+
205+ local result = {}
206+ for key , value in pairs (data ) do
207+ if type (value ) == ' string' and (key == ' filePath' or key == ' path' or key == ' directory' ) then
208+ result [key ] = M .apply_path_map (value )
209+ elseif type (value ) == ' table' then
210+ result [key ] = M .transform_paths_recursive (value )
211+ else
212+ result [key ] = value
213+ end
214+ end
215+ return result
216+ end
217+
187218--- Format a timestamp as time (e.g., "10:23 AM", "13 Oct 03:32 PM" "13 Oct 2025 03:32 PM")
188219--- @param timestamp number
189220--- @return string : Formatted time string
0 commit comments