|
|
|
@@ -1,5 +1,10 @@
|
|
|
|
|
// explain-permissions.js — opencode plugin
|
|
|
|
|
//
|
|
|
|
|
// Kanonische Quelle: git.az-gruppe.com/AZ-Intec-GmbH/opencode-plugins
|
|
|
|
|
// (Tag explain-permissions/v1.2.0). Fleet-Bezug: az-fleet vendort diese
|
|
|
|
|
// Datei nach config/managed/plugins/ (Pin mit sha256 dort in
|
|
|
|
|
// roles/managed_config — Byte-Gleichheit ist Vertragsgrundlage).
|
|
|
|
|
//
|
|
|
|
|
// Zweck: Jeden berechtigungspflichtigen Tool-Call laienverständlich erklären —
|
|
|
|
|
// WAS passiert, WELCHE FOLGEN, WIE RISKANT (Zielgruppe: nicht-technische Nutzer).
|
|
|
|
|
//
|
|
|
|
@@ -23,6 +28,16 @@
|
|
|
|
|
// Fehler, wenn das Modell einen berechtigungspflichtigen Call OHNE vorherige
|
|
|
|
|
// Text-Erklärung im selben Message-Turn absetzt. Default: AUS (Dialog-Schleifen).
|
|
|
|
|
//
|
|
|
|
|
// Notifications je Plattform (v1.2.0):
|
|
|
|
|
// Linux notify-send (libnotify)
|
|
|
|
|
// macOS osascript "display notification" (Boardmittel)
|
|
|
|
|
// Windows PowerShell-Toast via WinRT; AppUserModelID = PowerShell-AUMID
|
|
|
|
|
// (keine App-Registrierung nötig); Titel/Text über env-Variablen des
|
|
|
|
|
// Child-Prozesses (injektionssicher), XML-Escape in PowerShell
|
|
|
|
|
// Alle Zweige fail-soft: fehlendes Binary, headless Session oder Spawn-Fehler
|
|
|
|
|
// sind stille No-Ops (Node und Bun behandeln ENOENT verschieden — try/catch
|
|
|
|
|
// plus error-Listener decken beide Semantiken ab).
|
|
|
|
|
//
|
|
|
|
|
// Wichtig: Plugin-Änderungen greifen erst nach NEUSTART von opencode — eine neue
|
|
|
|
|
// Chat-Session im laufenden Prozess reicht nicht. Der Audit-Log-Eintrag
|
|
|
|
|
// "plugin-loaded" (mit Version) zeigt an, welche Version wann geladen wurde.
|
|
|
|
@@ -34,7 +49,10 @@
|
|
|
|
|
// OPENCODE_EXPLAIN_ENFORCE=1 Hartes Erzwingen aktivieren (Default: aus)
|
|
|
|
|
// OPENCODE_EXPLAIN_DEBUG=1 Permission-Events ins Log (Default: aus)
|
|
|
|
|
// OPENCODE_EXPLAIN_LOG_PATH=… Anderer Log-Pfad
|
|
|
|
|
// (Default: ~/.local/state/opencode/explain-permissions.jsonl)
|
|
|
|
|
// (Default Windows: %LOCALAPPDATA%\opencode\explain-permissions.jsonl —
|
|
|
|
|
// idiomatisch, roamt nicht; Fallback ~\AppData\Local, wenn LOCALAPPDATA
|
|
|
|
|
// nicht gesetzt. Entscheidung dokumentiert im README.)
|
|
|
|
|
// (Default sonst: ~/.local/state/opencode/explain-permissions.jsonl)
|
|
|
|
|
//
|
|
|
|
|
// Log ansehen:
|
|
|
|
|
// tail -f ~/.local/state/opencode/explain-permissions.jsonl | jq .
|
|
|
|
@@ -45,15 +63,23 @@ import os from "node:os";
|
|
|
|
|
import { spawn } from "node:child_process";
|
|
|
|
|
|
|
|
|
|
const ENV = process.env;
|
|
|
|
|
const PLUGIN_VERSION = "1.1.0";
|
|
|
|
|
const PLUGIN_VERSION = "1.2.0";
|
|
|
|
|
const NOTIFY = ENV.OPENCODE_EXPLAIN_NOTIFY !== "0";
|
|
|
|
|
const LOG = ENV.OPENCODE_EXPLAIN_LOG !== "0";
|
|
|
|
|
const INJECT = ENV.OPENCODE_EXPLAIN_INJECT !== "0";
|
|
|
|
|
const ENFORCE = ENV.OPENCODE_EXPLAIN_ENFORCE === "1";
|
|
|
|
|
const DEBUG = ENV.OPENCODE_EXPLAIN_DEBUG === "1";
|
|
|
|
|
const LOG_PATH =
|
|
|
|
|
ENV.OPENCODE_EXPLAIN_LOG_PATH ||
|
|
|
|
|
path.join(os.homedir(), ".local", "state", "opencode", "explain-permissions.jsonl");
|
|
|
|
|
|
|
|
|
|
// Windows: %LOCALAPPDATA% (idiomatisch, roamt nicht); sonst XDG-State im Profil.
|
|
|
|
|
function defaultLogPath() {
|
|
|
|
|
if (process.platform === "win32") {
|
|
|
|
|
const base = ENV.LOCALAPPDATA || (os.homedir() + "\\AppData\\Local");
|
|
|
|
|
return base + "\\opencode\\explain-permissions.jsonl";
|
|
|
|
|
}
|
|
|
|
|
return path.join(os.homedir(), ".local", "state", "opencode", "explain-permissions.jsonl");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const LOG_PATH = ENV.OPENCODE_EXPLAIN_LOG_PATH || defaultLogPath();
|
|
|
|
|
|
|
|
|
|
const SYSTEM_PROMPT_RULE = `## Tool-Call-Erklärungen (verpflichtend, laienverständlich)
|
|
|
|
|
Vor JEDEM Tool-Call, der eine Freigabe erfordert, schreibe unmittelbar davor eine Erklärung auf Deutsch, die ein Mensch ohne Computer-Kenntnisse versteht. Struktur:
|
|
|
|
@@ -118,20 +144,62 @@ function appendLog(entry) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function notify(title, body) {
|
|
|
|
|
if (!NOTIFY) return;
|
|
|
|
|
// Detached Spawn, fail-soft in Node UND Bun: ENOENT wirft/emittiert in den
|
|
|
|
|
// beiden Runtimes unterschiedlich (Node: asynchrones 'error'-Event, Bun kann
|
|
|
|
|
// synchron werfen) — try/catch plus error-Listener decken beide Semantiken ab.
|
|
|
|
|
// stdio 'ignore' + unref: der Agent-Prozess wartet nie auf das Notification-
|
|
|
|
|
// Binary; fehlendes Binary/headless = stiller No-Op.
|
|
|
|
|
function runDetached(cmd, args, env) {
|
|
|
|
|
try {
|
|
|
|
|
const child = spawn(
|
|
|
|
|
"notify-send",
|
|
|
|
|
["-a", "opencode", "-i", "utilities-terminal", "-u", "normal", title, body.slice(0, 400)],
|
|
|
|
|
{ detached: true, stdio: "ignore" },
|
|
|
|
|
);
|
|
|
|
|
const child = spawn(cmd, args, { detached: true, stdio: "ignore", env: { ...ENV, ...env } });
|
|
|
|
|
child.on("error", () => {});
|
|
|
|
|
child.unref();
|
|
|
|
|
} catch {
|
|
|
|
|
// keine Desktop-Session (headless) → egal
|
|
|
|
|
// fehlendes Binary oder Spawn-Verweigerung → stiller No-Op
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PowerShell-AppUserModelID (Shell-Folder-GUID von Windows PowerShell): Toasts
|
|
|
|
|
// erscheinen unter dem Namen "Windows PowerShell", ohne dass eine eigene App
|
|
|
|
|
// registriert werden muss (AUMID-Standardtrick, kein Shortcut, keine Registry).
|
|
|
|
|
const TOAST_AUMID = "{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\\WindowsPowerShell\\v1.0\\powershell.exe";
|
|
|
|
|
|
|
|
|
|
// Titel/Text erreichen PowerShell über env-Variablen des Child-Prozesses —
|
|
|
|
|
// kein String-Interpolieren in den Skript-Body (injektionssicher); XML-Escape
|
|
|
|
|
// macht [SecurityElement]::Escape auf der Windows-Seite.
|
|
|
|
|
const TOAST_PS = [
|
|
|
|
|
"$ErrorActionPreference = 'SilentlyContinue'",
|
|
|
|
|
"[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null",
|
|
|
|
|
"$xml = New-Object Windows.Data.Xml.Dom.XmlDocument",
|
|
|
|
|
"$t = [System.Security.SecurityElement]::Escape($env:OC_TOAST_TITLE)",
|
|
|
|
|
"$b = [System.Security.SecurityElement]::Escape($env:OC_TOAST_BODY)",
|
|
|
|
|
"$xml.LoadXml('<toast><visual><binding template=\"ToastGeneric\"><text>' + $t + '</text><text>' + $b + '</text></binding></visual></toast>')",
|
|
|
|
|
"$toast = New-Object Windows.UI.Notifications.ToastNotification $xml",
|
|
|
|
|
"[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier('" + TOAST_AUMID + "').Show($toast)",
|
|
|
|
|
].join("\n");
|
|
|
|
|
|
|
|
|
|
function windowsToast(title, body) {
|
|
|
|
|
const encoded = Buffer.from(TOAST_PS, "utf16le").toString("base64");
|
|
|
|
|
runDetached(
|
|
|
|
|
"powershell.exe",
|
|
|
|
|
["-NoProfile", "-NonInteractive", "-WindowStyle", "Hidden", "-EncodedCommand", encoded],
|
|
|
|
|
{ OC_TOAST_TITLE: title, OC_TOAST_BODY: body },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function macosNotify(title, body) {
|
|
|
|
|
const esc = (s) => s.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
|
|
|
|
|
runDetached("osascript", ["-e", `display notification "${esc(body)}" with title "${esc(title)}"`]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function notify(title, body) {
|
|
|
|
|
if (!NOTIFY) return;
|
|
|
|
|
const b = body.slice(0, 400);
|
|
|
|
|
if (process.platform === "win32") windowsToast(title, b);
|
|
|
|
|
else if (process.platform === "darwin") macosNotify(title, b);
|
|
|
|
|
else runDetached("notify-send", ["-a", "opencode", "-i", "utilities-terminal", "-u", "normal", title, b]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function debugLog(source, payload) {
|
|
|
|
|
if (!DEBUG) return;
|
|
|
|
|
let serialized;
|
|
|
|
|