2025-12-14 22:34:55 +09:00
|
|
|
import type { PluginInput } from "@opencode-ai/plugin"
|
2025-12-23 10:45:24 +09:00
|
|
|
import { HOOK_NAME, NON_INTERACTIVE_ENV, SHELL_COMMAND_PATTERNS } from "./constants"
|
2026-01-15 16:04:06 +09:00
|
|
|
import { isNonInteractive } from "./detector"
|
|
|
|
|
import { log, detectShellType, buildEnvPrefix } from "../../shared"
|
2025-12-14 22:34:55 +09:00
|
|
|
|
|
|
|
|
export * from "./constants"
|
2025-12-25 15:27:34 +09:00
|
|
|
export * from "./detector"
|
2025-12-14 22:34:55 +09:00
|
|
|
export * from "./types"
|
|
|
|
|
|
2025-12-23 10:45:24 +09:00
|
|
|
const BANNED_COMMAND_PATTERNS = SHELL_COMMAND_PATTERNS.banned
|
|
|
|
|
.filter((cmd) => !cmd.includes("("))
|
|
|
|
|
.map((cmd) => new RegExp(`\\b${cmd}\\b`))
|
|
|
|
|
|
|
|
|
|
function detectBannedCommand(command: string): string | undefined {
|
|
|
|
|
for (let i = 0; i < BANNED_COMMAND_PATTERNS.length; i++) {
|
|
|
|
|
if (BANNED_COMMAND_PATTERNS[i].test(command)) {
|
|
|
|
|
return SHELL_COMMAND_PATTERNS.banned[i]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return undefined
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-14 22:34:55 +09:00
|
|
|
export function createNonInteractiveEnvHook(_ctx: PluginInput) {
|
|
|
|
|
return {
|
|
|
|
|
"tool.execute.before": async (
|
|
|
|
|
input: { tool: string; sessionID: string; callID: string },
|
2025-12-23 10:45:24 +09:00
|
|
|
output: { args: Record<string, unknown>; message?: string }
|
2025-12-14 22:34:55 +09:00
|
|
|
): Promise<void> => {
|
|
|
|
|
if (input.tool.toLowerCase() !== "bash") {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const command = output.args.command as string | undefined
|
|
|
|
|
if (!command) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-23 10:45:24 +09:00
|
|
|
const bannedCmd = detectBannedCommand(command)
|
|
|
|
|
if (bannedCmd) {
|
|
|
|
|
output.message = `⚠️ Warning: '${bannedCmd}' is an interactive command that may hang in non-interactive environments.`
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-02 22:19:46 +09:00
|
|
|
// Only prepend env vars for git commands (editor blocking, pager, etc.)
|
|
|
|
|
const isGitCommand = /\bgit\b/.test(command)
|
|
|
|
|
if (!isGitCommand) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-15 16:04:06 +09:00
|
|
|
if (!isNonInteractive()) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const shellType = detectShellType()
|
|
|
|
|
const envPrefix = buildEnvPrefix(NON_INTERACTIVE_ENV, shellType)
|
2026-01-03 15:36:01 +09:00
|
|
|
output.args.command = `${envPrefix} ${command}`
|
2026-01-02 22:19:46 +09:00
|
|
|
|
|
|
|
|
log(`[${HOOK_NAME}] Prepended non-interactive env vars to git command`, {
|
2025-12-14 22:34:55 +09:00
|
|
|
sessionID: input.sessionID,
|
2026-01-02 22:19:46 +09:00
|
|
|
envPrefix,
|
2025-12-14 22:34:55 +09:00
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|