Skip to content

Commit 8f16faf

Browse files
feat: pass mentioned files to prompt_guard callback
The prompt_guard callback now receives the list of mentioned files from the opencode context as a parameter. This allows users to make decisions based on which files are included in the context. Changes: - Updated prompt_guard type signature from `fun(): boolean` to `fun(mentioned_files: string[]): boolean` - Modified util.check_prompt_allowed to accept and pass mentioned_files parameter - Updated prompt_guard_indicator to retrieve mentioned files from context - Updated core.lua toggle and send_message to pass mentioned files to guard callback This enables use cases like denying prompts when certain sensitive files are mentioned, or allowing prompts only when specific files are included.
1 parent 8cb171f commit 8f16faf

4 files changed

Lines changed: 14 additions & 6 deletions

File tree

lua/opencode/core.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ function M.open(opts)
4949

5050
if are_windows_closed then
5151
-- Check if whether prompting will be allowed
52-
local allowed, err_msg = util.check_prompt_allowed(config.prompt_guard)
52+
local context_module = require('opencode.context')
53+
local mentioned_files = context_module.context.mentioned_files or {}
54+
local allowed, err_msg = util.check_prompt_allowed(config.prompt_guard, mentioned_files)
5355
if not allowed then
5456
vim.notify(err_msg or 'Prompts will be denied by prompt_guard', vim.log.levels.WARN)
5557
end
@@ -88,7 +90,9 @@ end
8890
--- @param opts? SendMessageOpts
8991
function M.send_message(prompt, opts)
9092
-- Check if prompt is allowed
91-
local allowed, err_msg = util.check_prompt_allowed(config.prompt_guard)
93+
local context_module = require('opencode.context')
94+
local mentioned_files = context_module.context.mentioned_files or {}
95+
local allowed, err_msg = util.check_prompt_allowed(config.prompt_guard, mentioned_files)
9296

9397
if not allowed then
9498
vim.notify(err_msg or 'Prompt denied by prompt_guard', vim.log.levels.ERROR)

lua/opencode/types.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
---@field ui OpencodeUIConfig
142142
---@field context OpencodeContextConfig
143143
---@field debug OpencodeDebugConfig
144-
---@field prompt_guard? fun(): boolean
144+
---@field prompt_guard? fun(mentioned_files: string[]): boolean
145145

146146
---@class MessagePartState
147147
---@field input TaskToolInput|BashToolInput|FileToolInput|TodoToolInput|GlobToolInput|GrepToolInput|WebFetchToolInput|ListToolInput Input data for the tool

lua/opencode/ui/prompt_guard_indicator.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ local state = require('opencode.state')
44
local config = require('opencode.config')
55
local util = require('opencode.util')
66
local icons = require('opencode.ui.icons')
7+
local context = require('opencode.context')
78

89
---Get the current prompt guard status
910
---@return boolean allowed
1011
---@return string|nil error_message
1112
function M.get_status()
12-
return util.check_prompt_allowed(config.prompt_guard)
13+
local mentioned_files = context.context.mentioned_files or {}
14+
return util.check_prompt_allowed(config.prompt_guard, mentioned_files)
1315
end
1416

1517
---Check if guard will deny prompts

lua/opencode/util.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,10 @@ end
362362

363363
--- Check if prompt is allowed via guard callback
364364
--- @param guard_callback? function
365+
--- @param mentioned_files? string[] List of mentioned files in the context
365366
--- @return boolean allowed
366367
--- @return string|nil error_message
367-
function M.check_prompt_allowed(guard_callback)
368+
function M.check_prompt_allowed(guard_callback, mentioned_files)
368369
if not guard_callback then
369370
return true, nil -- No guard = always allowed
370371
end
@@ -373,7 +374,8 @@ function M.check_prompt_allowed(guard_callback)
373374
return false, 'prompt_guard must be a function'
374375
end
375376

376-
local success, result = pcall(guard_callback)
377+
mentioned_files = mentioned_files or {}
378+
local success, result = pcall(guard_callback, mentioned_files)
377379

378380
if not success then
379381
return false, 'prompt_guard error: ' .. tostring(result)

0 commit comments

Comments
 (0)