Skip to content

Commit dd69a45

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 3ac5a9f commit dd69a45

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
@@ -52,7 +52,9 @@ function M.open(opts)
5252

5353
if are_windows_closed then
5454
-- Check if whether prompting will be allowed
55-
local allowed, err_msg = util.check_prompt_allowed(config.prompt_guard)
55+
local context_module = require('opencode.context')
56+
local mentioned_files = context_module.context.mentioned_files or {}
57+
local allowed, err_msg = util.check_prompt_allowed(config.prompt_guard, mentioned_files)
5658
if not allowed then
5759
vim.notify(err_msg or 'Prompts will be denied by prompt_guard', vim.log.levels.WARN)
5860
end
@@ -91,7 +93,9 @@ end
9193
--- @param opts? SendMessageOpts
9294
function M.send_message(prompt, opts)
9395
-- Check if prompt is allowed
94-
local allowed, err_msg = util.check_prompt_allowed(config.prompt_guard)
96+
local context_module = require('opencode.context')
97+
local mentioned_files = context_module.context.mentioned_files or {}
98+
local allowed, err_msg = util.check_prompt_allowed(config.prompt_guard, mentioned_files)
9599

96100
if not allowed then
97101
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
@@ -127,7 +127,7 @@
127127
---@field ui OpencodeUIConfig
128128
---@field context OpencodeContextConfig
129129
---@field debug OpencodeDebugConfig
130-
---@field prompt_guard? fun(): boolean
130+
---@field prompt_guard? fun(mentioned_files: string[]): boolean
131131

132132
---@class MessagePartState
133133
---@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
@@ -349,9 +349,10 @@ end
349349

350350
--- Check if prompt is allowed via guard callback
351351
--- @param guard_callback? function
352+
--- @param mentioned_files? string[] List of mentioned files in the context
352353
--- @return boolean allowed
353354
--- @return string|nil error_message
354-
function M.check_prompt_allowed(guard_callback)
355+
function M.check_prompt_allowed(guard_callback, mentioned_files)
355356
if not guard_callback then
356357
return true, nil -- No guard = always allowed
357358
end
@@ -360,7 +361,8 @@ function M.check_prompt_allowed(guard_callback)
360361
return false, 'prompt_guard must be a function'
361362
end
362363

363-
local success, result = pcall(guard_callback)
364+
mentioned_files = mentioned_files or {}
365+
local success, result = pcall(guard_callback, mentioned_files)
364366

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

0 commit comments

Comments
 (0)