@@ -15,17 +15,67 @@ function M.add_permission(permission)
1515 return
1616 end
1717
18+ if permission .tool then
19+ permission ._message_id = permission .tool .messageID
20+ permission ._call_id = permission .tool .callID
21+ end
22+
1823 -- Update if exists, otherwise add
1924 for i , existing in ipairs (M ._permission_queue ) do
2025 if existing .id == permission .id then
2126 M ._permission_queue [i ] = permission
22- M ._setup_dialog () -- Refresh dialog when permission is updated
27+ M ._setup_dialog ()
2328 return
2429 end
2530 end
2631
2732 table.insert (M ._permission_queue , permission )
28- M ._setup_dialog () -- Setup dialog when first permission is added
33+ M ._setup_dialog ()
34+ end
35+
36+ --- Update permission from message part data
37+ --- @param permission_id string
38+ --- @param part table
39+ --- @return boolean
40+ function M .update_permission_from_part (permission_id , part )
41+ if not permission_id or not part then
42+ return false
43+ end
44+
45+ local permission = nil
46+ for i , existing in ipairs (M ._permission_queue ) do
47+ if existing .id == permission_id then
48+ permission = existing
49+ break
50+ end
51+ end
52+
53+ if not permission then
54+ return false
55+ end
56+
57+ if part .state and part .state .input then
58+ local input = part .state .input
59+ local updated = false
60+
61+ if input .description and input .description ~= ' ' then
62+ permission ._description = input .description
63+ updated = true
64+ end
65+
66+ if input .command and input .command ~= ' ' then
67+ permission ._command = input .command
68+ updated = true
69+ end
70+
71+ if updated and M ._dialog then
72+ M ._setup_dialog ()
73+ end
74+
75+ return true
76+ end
77+
78+ return false
2979end
3080
3181--- Remove permission from queue
@@ -71,12 +121,33 @@ function M.format_display(output)
71121 progress = string.format (' (%d/%d)' , 1 , # M ._permission_queue )
72122 end
73123
74- local title = permission .title
75- or table.concat (permission .patterns or {}, ' `, `' ):gsub (' \r ' , ' \\ r' ):gsub (' \n ' , ' \\ n' )
76- or ' Unknown Permission'
77- local perm_type = permission .permission or permission .type or ' unknown'
124+ local content = {}
125+ local perm_type = permission .permission or permission .type or ' '
126+
127+ if permission ._description and permission ._description ~= ' ' then
128+ table.insert (content , (icons .get (perm_type )) .. ' *' .. perm_type .. ' * ' .. permission ._description )
129+ elseif permission .title then
130+ table.insert (content , (icons .get (perm_type )) .. ' *' .. perm_type .. ' * `' .. permission .title .. ' `' )
131+ else
132+ table.insert (content , (icons .get (perm_type )) .. ' *' .. perm_type .. ' *' )
133+ local lines = permission .patterns or {}
134+ table.insert (content , string.format (' ```%s' , perm_type ))
135+ for i , line in ipairs (lines ) do
136+ table.insert (content , line )
137+ end
138+ table.insert (content , ' ```' )
139+ end
78140
79- local content = { (icons .get (perm_type ) or ' ' ) .. ' *' .. (perm_type or ' ' ) .. ' *' .. ' `' .. title .. ' `' }
141+ table.insert (content , ' ' )
142+
143+ if permission ._command and permission ._command ~= ' ' then
144+ local lines = vim .split (permission ._command , ' \n ' )
145+ table.insert (content , string.format (' ```%s' , perm_type ))
146+ for _ , line in ipairs (lines ) do
147+ table.insert (content , line )
148+ end
149+ table.insert (content , ' ```' )
150+ end
80151
81152 local options = {
82153 { label = ' Allow once' },
@@ -88,6 +159,9 @@ function M.format_display(output)
88159 if perm_type == ' edit' and permission .metadata and permission .metadata .diff then
89160 render_content = function (out )
90161 out :add_line (content [1 ])
162+ if content [2 ] then
163+ out :add_line (content [2 ])
164+ end
91165 out :add_line (' ' )
92166
93167 local file_type = permission .metadata .filepath and vim .fn .fnamemodify (permission .metadata .filepath , ' :e' ) or ' '
@@ -112,6 +186,11 @@ function M._setup_dialog()
112186 return
113187 end
114188
189+ local saved_selection = nil
190+ if M ._dialog then
191+ saved_selection = M ._dialog :get_selection ()
192+ end
193+
115194 M ._clear_dialog ()
116195
117196 if not state .windows or not state .windows .output_buf then
@@ -176,6 +255,10 @@ function M._setup_dialog()
176255 })
177256
178257 M ._dialog :setup ()
258+
259+ if saved_selection then
260+ M ._dialog :set_selection (saved_selection )
261+ end
179262end
180263
181264function M ._clear_dialog ()
0 commit comments