@noppu-labs/opencode-permission-reviewer 0.0.0-stage → 2.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +632 -0
- package/LICENSE +201 -0
- package/MIGRATION.md +111 -0
- package/NOTICE +13 -0
- package/README.md +805 -2
- package/SECURITY.md +31 -0
- package/dist/explain.d.ts +4 -0
- package/dist/explain.js +4422 -0
- package/dist/index.d.ts +1133 -0
- package/dist/index.js +10714 -0
- package/dist/rpc.d.ts +42 -0
- package/dist/rpc.js +19 -0
- package/dist/tui/config/jsonc.ts +100 -0
- package/dist/tui/config/loader.ts +573 -0
- package/dist/tui/config.ts +479 -0
- package/dist/tui/opencode/event-normalizer.ts +52 -0
- package/dist/tui/tui.tsx +162 -0
- package/dist/tui/types.ts +687 -0
- package/dist/tui/ui/components.tsx +181 -0
- package/dist/tui/ui/rpc.ts +16 -0
- package/dist/tui/ui/v2.tsx +261 -0
- package/dist/tui/ui-protocol.ts +183 -0
- package/dist/tui/ui-state.ts +139 -0
- package/package.json +128 -4
- package/rpc.js +1 -0
- package/server.js +2 -0
- package/tui.tsx +1 -0
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/** @jsxImportSource @opentui/solid */
|
|
2
|
+
import { Show } from "solid-js"
|
|
3
|
+
import type { ColorInput } from "@opentui/core"
|
|
4
|
+
import type { ReviewUiStatus } from "../ui-protocol.ts"
|
|
5
|
+
|
|
6
|
+
export interface ReviewTheme {
|
|
7
|
+
backgroundPanel: ColorInput
|
|
8
|
+
text: ColorInput
|
|
9
|
+
textMuted: ColorInput
|
|
10
|
+
info: ColorInput
|
|
11
|
+
success: ColorInput
|
|
12
|
+
error: ColorInput
|
|
13
|
+
}
|
|
14
|
+
export const SPINNER = ["◐", "◓", "◑", "◒"] as const
|
|
15
|
+
|
|
16
|
+
/** Compact, non-modal progress strip for hosts with an editable composer. */
|
|
17
|
+
export function ReviewProgress(props: {
|
|
18
|
+
theme: () => ReviewTheme
|
|
19
|
+
status: ReviewUiStatus
|
|
20
|
+
frame: () => number
|
|
21
|
+
}) {
|
|
22
|
+
const singleLine = (value: string) => value.replace(/[\r\n\t]+/g, " ")
|
|
23
|
+
const elapsed = () => {
|
|
24
|
+
props.frame()
|
|
25
|
+
return `${(Math.max(0, Date.now() - props.status.emittedAt) / 1_000).toFixed(1)}s`
|
|
26
|
+
}
|
|
27
|
+
return (
|
|
28
|
+
<box
|
|
29
|
+
height={2}
|
|
30
|
+
flexShrink={0}
|
|
31
|
+
overflow="hidden"
|
|
32
|
+
backgroundColor={props.theme().backgroundPanel}
|
|
33
|
+
flexDirection="column"
|
|
34
|
+
paddingLeft={1}
|
|
35
|
+
paddingRight={1}
|
|
36
|
+
>
|
|
37
|
+
<box height={1} flexDirection="row" gap={1}>
|
|
38
|
+
<text
|
|
39
|
+
fg={props.theme().info}
|
|
40
|
+
flexGrow={1}
|
|
41
|
+
flexShrink={1}
|
|
42
|
+
minWidth={0}
|
|
43
|
+
wrapMode="none"
|
|
44
|
+
truncate
|
|
45
|
+
>
|
|
46
|
+
{SPINNER[props.frame() % SPINNER.length] ?? "◐"} Reviewing this permission
|
|
47
|
+
<span style={{ fg: props.theme().textMuted }}>
|
|
48
|
+
{" "}
|
|
49
|
+
· {singleLine(props.status.model)} · {singleLine(props.status.variant)}
|
|
50
|
+
</span>
|
|
51
|
+
</text>
|
|
52
|
+
<text fg={props.theme().textMuted} flexShrink={0} wrapMode="none">
|
|
53
|
+
{elapsed()}
|
|
54
|
+
</text>
|
|
55
|
+
</box>
|
|
56
|
+
<text fg={props.theme().text} wrapMode="none" truncate>
|
|
57
|
+
<span style={{ fg: props.theme().textMuted }}>{props.status.permission} · </span>
|
|
58
|
+
{singleLine(props.status.action)}
|
|
59
|
+
<Show when={props.status.actorName}>
|
|
60
|
+
<span style={{ fg: props.theme().textMuted }}>
|
|
61
|
+
{" "}
|
|
62
|
+
· actor {singleLine(props.status.actorName ?? "")}
|
|
63
|
+
</span>
|
|
64
|
+
</Show>
|
|
65
|
+
</text>
|
|
66
|
+
</box>
|
|
67
|
+
)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function ReviewOverlay(props: {
|
|
71
|
+
theme: () => ReviewTheme
|
|
72
|
+
status: ReviewUiStatus
|
|
73
|
+
/** Spinner signal, read inside text children so only those update per tick. */
|
|
74
|
+
frame: () => number
|
|
75
|
+
}) {
|
|
76
|
+
const theme = props.theme
|
|
77
|
+
const elapsed = () => {
|
|
78
|
+
// Reading the tick signal inside the text child is what re-renders it every
|
|
79
|
+
// 250ms: Date.now() and status.emittedAt are plain values, so a text that
|
|
80
|
+
// reads no signal is evaluated once at mount and freezes.
|
|
81
|
+
props.frame()
|
|
82
|
+
const elapsedMs = Math.max(0, Date.now() - props.status.emittedAt)
|
|
83
|
+
return `${(elapsedMs / 1_000).toFixed(1)}s`
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return (
|
|
87
|
+
<box
|
|
88
|
+
position="absolute"
|
|
89
|
+
left={0}
|
|
90
|
+
right={0}
|
|
91
|
+
bottom={0}
|
|
92
|
+
height="auto"
|
|
93
|
+
maxHeight={12}
|
|
94
|
+
overflow="hidden"
|
|
95
|
+
backgroundColor={theme().backgroundPanel}
|
|
96
|
+
border={["left"]}
|
|
97
|
+
borderColor={theme().info}
|
|
98
|
+
flexDirection="column"
|
|
99
|
+
onMouseDown={(event) => {
|
|
100
|
+
event.preventDefault()
|
|
101
|
+
event.stopPropagation()
|
|
102
|
+
}}
|
|
103
|
+
onMouseUp={(event) => {
|
|
104
|
+
event.preventDefault()
|
|
105
|
+
event.stopPropagation()
|
|
106
|
+
}}
|
|
107
|
+
>
|
|
108
|
+
<box
|
|
109
|
+
flexDirection="column"
|
|
110
|
+
gap={1}
|
|
111
|
+
paddingTop={1}
|
|
112
|
+
paddingBottom={1}
|
|
113
|
+
paddingLeft={2}
|
|
114
|
+
paddingRight={3}
|
|
115
|
+
>
|
|
116
|
+
<box flexDirection="row" gap={1}>
|
|
117
|
+
<text fg={theme().info}>{SPINNER[props.frame() % SPINNER.length] ?? "◐"}</text>
|
|
118
|
+
<text fg={theme().text}>Reviewing this permission</text>
|
|
119
|
+
<box flexGrow={1} />
|
|
120
|
+
<text fg={theme().textMuted}>{elapsed()}</text>
|
|
121
|
+
</box>
|
|
122
|
+
<box flexDirection="row" gap={1} paddingLeft={2}>
|
|
123
|
+
<text fg={theme().textMuted}>{props.status.permission}</text>
|
|
124
|
+
<text fg={theme().text} wrapMode="word">
|
|
125
|
+
{props.status.action}
|
|
126
|
+
</text>
|
|
127
|
+
<Show when={props.status.actorName}>
|
|
128
|
+
<text fg={theme().textMuted}>· actor {props.status.actorName}</text>
|
|
129
|
+
</Show>
|
|
130
|
+
</box>
|
|
131
|
+
<box paddingLeft={2} flexDirection="row" gap={1}>
|
|
132
|
+
<text fg={theme().textMuted}>
|
|
133
|
+
{props.status.model} · reasoning {props.status.variant}
|
|
134
|
+
</text>
|
|
135
|
+
<box flexGrow={1} />
|
|
136
|
+
<text fg={theme().textMuted}>No action needed</text>
|
|
137
|
+
</box>
|
|
138
|
+
</box>
|
|
139
|
+
</box>
|
|
140
|
+
)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function ReviewResult(props: { theme: () => ReviewTheme; status: ReviewUiStatus }) {
|
|
144
|
+
const theme = props.theme
|
|
145
|
+
const appearance = () => {
|
|
146
|
+
if (props.status.phase === "unknown") {
|
|
147
|
+
return { color: theme().textMuted, icon: "?", title: "Review status unavailable" }
|
|
148
|
+
}
|
|
149
|
+
if (props.status.phase === "approved") {
|
|
150
|
+
return { color: theme().success, icon: "✓", title: "Review approved" }
|
|
151
|
+
}
|
|
152
|
+
// Fail-closed escalate→deny is distinct from an explicit reviewer/policy deny.
|
|
153
|
+
if (props.status.escalationDisposition === "deny") {
|
|
154
|
+
return { color: theme().error, icon: "✕", title: "Review blocked (fail-closed)" }
|
|
155
|
+
}
|
|
156
|
+
return { color: theme().error, icon: "✕", title: "Review blocked" }
|
|
157
|
+
}
|
|
158
|
+
const singleLine = (value: string) => value.replace(/[\r\n\t]+/g, " ")
|
|
159
|
+
|
|
160
|
+
return (
|
|
161
|
+
<box
|
|
162
|
+
height={props.status.reason ? 2 : 1}
|
|
163
|
+
flexShrink={0}
|
|
164
|
+
overflow="hidden"
|
|
165
|
+
backgroundColor={theme().backgroundPanel}
|
|
166
|
+
flexDirection="column"
|
|
167
|
+
paddingLeft={1}
|
|
168
|
+
paddingRight={1}
|
|
169
|
+
>
|
|
170
|
+
<text fg={appearance().color} wrapMode="none" truncate>
|
|
171
|
+
{appearance().icon} {appearance().title} · {props.status.permission} ·{" "}
|
|
172
|
+
{singleLine(props.status.action)}
|
|
173
|
+
</text>
|
|
174
|
+
<Show when={props.status.reason}>
|
|
175
|
+
<text fg={theme().textMuted} wrapMode="none" truncate>
|
|
176
|
+
{singleLine(props.status.reason ?? "")}
|
|
177
|
+
</text>
|
|
178
|
+
</Show>
|
|
179
|
+
</box>
|
|
180
|
+
)
|
|
181
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Portable, read-only protocol. No UI message can approve a permission. */
|
|
2
|
+
export const ReviewerRpc = {
|
|
3
|
+
id: "opencode-permission-reviewer",
|
|
4
|
+
methods: {
|
|
5
|
+
identity: {
|
|
6
|
+
input: { type: "object", additionalProperties: false },
|
|
7
|
+
output: { type: "string" },
|
|
8
|
+
},
|
|
9
|
+
status: { input: { type: "object", additionalProperties: false }, output: { type: "object" } },
|
|
10
|
+
snapshot: {
|
|
11
|
+
input: { type: "object", additionalProperties: false },
|
|
12
|
+
output: { type: "object" },
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
events: { "review.updated": { schema: { type: "object" } } },
|
|
16
|
+
} as const
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
/** @jsxImportSource @opentui/solid */
|
|
2
|
+
import type { Plugin } from "@opencode/plugin/tui"
|
|
3
|
+
import type { ColorInput } from "@opentui/core"
|
|
4
|
+
import { createSignal, Show } from "solid-js"
|
|
5
|
+
import { ReviewerRpc } from "./rpc.ts"
|
|
6
|
+
import { ReviewProgress, ReviewResult, type ReviewTheme } from "./components.tsx"
|
|
7
|
+
import { ReviewUiState } from "../ui-state.ts"
|
|
8
|
+
import { decodeUiStatus, encodeUiStatus, type ReviewUiStatus } from "../ui-protocol.ts"
|
|
9
|
+
|
|
10
|
+
type Context = Parameters<Plugin.Definition["setup"]>[0]
|
|
11
|
+
const object = (value: unknown): Record<string, unknown> | undefined =>
|
|
12
|
+
typeof value === "object" && value !== null ? (value as Record<string, unknown>) : undefined
|
|
13
|
+
const color = (value: unknown): ColorInput | undefined => {
|
|
14
|
+
if (typeof value === "string") return value
|
|
15
|
+
const candidate = object(value)
|
|
16
|
+
return candidate && "buffer" in candidate ? (value as ColorInput) : undefined
|
|
17
|
+
}
|
|
18
|
+
const FALLBACK_THEME = {
|
|
19
|
+
backgroundPanel: "#111111",
|
|
20
|
+
text: "#eeeeee",
|
|
21
|
+
textMuted: "#999999",
|
|
22
|
+
info: "#4da3ff",
|
|
23
|
+
success: "#48c774",
|
|
24
|
+
error: "#ff5c5c",
|
|
25
|
+
} satisfies ReviewTheme
|
|
26
|
+
|
|
27
|
+
export function resolveReviewTheme(value: unknown): ReviewTheme {
|
|
28
|
+
const host = object(value)
|
|
29
|
+
if (!host) return FALLBACK_THEME
|
|
30
|
+
const background = object(host.background)
|
|
31
|
+
const surface = object(background?.surface)
|
|
32
|
+
const raised = object(background?.raised)
|
|
33
|
+
const text = object(host.text)
|
|
34
|
+
const status = object(text?.status)
|
|
35
|
+
const feedback = object(text?.feedback)
|
|
36
|
+
const infoFeedback = object(feedback?.info)
|
|
37
|
+
const successFeedback = object(feedback?.success)
|
|
38
|
+
const errorFeedback = object(feedback?.error)
|
|
39
|
+
return {
|
|
40
|
+
backgroundPanel:
|
|
41
|
+
color(surface?.overlay) ??
|
|
42
|
+
color(raised?.base) ??
|
|
43
|
+
color(host.backgroundPanel) ??
|
|
44
|
+
FALLBACK_THEME.backgroundPanel,
|
|
45
|
+
text: color(text?.default) ?? color(text?.base) ?? color(host.text) ?? FALLBACK_THEME.text,
|
|
46
|
+
textMuted:
|
|
47
|
+
color(text?.subdued) ??
|
|
48
|
+
color(text?.muted) ??
|
|
49
|
+
color(host.textMuted) ??
|
|
50
|
+
FALLBACK_THEME.textMuted,
|
|
51
|
+
info:
|
|
52
|
+
color(status?.running) ??
|
|
53
|
+
color(infoFeedback?.base) ??
|
|
54
|
+
color(host.info) ??
|
|
55
|
+
FALLBACK_THEME.info,
|
|
56
|
+
success:
|
|
57
|
+
color(successFeedback?.default) ??
|
|
58
|
+
color(successFeedback?.base) ??
|
|
59
|
+
color(host.success) ??
|
|
60
|
+
FALLBACK_THEME.success,
|
|
61
|
+
error:
|
|
62
|
+
color(errorFeedback?.default) ??
|
|
63
|
+
color(errorFeedback?.base) ??
|
|
64
|
+
color(host.error) ??
|
|
65
|
+
FALLBACK_THEME.error,
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function setupTuiV2(ctx: Context): Promise<() => void> {
|
|
70
|
+
let directory = ctx.location?.directory
|
|
71
|
+
const rpc = ctx.client.rpc(ReviewerRpc)
|
|
72
|
+
const [revision, touch] = createSignal(0)
|
|
73
|
+
const [frame, tick] = createSignal(0)
|
|
74
|
+
let state = new ReviewUiState({ model: "", variant: "", timeoutMs: 180_000 })
|
|
75
|
+
let generation: string | undefined
|
|
76
|
+
let sequence = -1
|
|
77
|
+
let stopped = false
|
|
78
|
+
let controller: AbortController | undefined
|
|
79
|
+
let reconnect: ReturnType<typeof setTimeout> | undefined
|
|
80
|
+
let popMode: (() => void) | undefined
|
|
81
|
+
let routeSession: string | undefined
|
|
82
|
+
const active = () => {
|
|
83
|
+
revision()
|
|
84
|
+
const route = ctx.ui.router.current()
|
|
85
|
+
return route.type === "session"
|
|
86
|
+
? state.activeFor(route.sessionID, (id) => ctx.data.session.get(id)?.parentID)
|
|
87
|
+
: undefined
|
|
88
|
+
}
|
|
89
|
+
const theme = (): ReviewTheme => resolveReviewTheme((ctx as unknown as { theme?: unknown }).theme)
|
|
90
|
+
const refresh = () => {
|
|
91
|
+
touch((value) => value + 1)
|
|
92
|
+
const reviewing = active()?.phase === "reviewing"
|
|
93
|
+
if (reviewing && !popMode) {
|
|
94
|
+
try {
|
|
95
|
+
popMode = ctx.keymap.mode.push("permission-reviewer")
|
|
96
|
+
} catch {
|
|
97
|
+
/* The panel remains useful without a host mode. */
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (!reviewing && popMode) {
|
|
101
|
+
popMode()
|
|
102
|
+
popMode = undefined
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const applyStatus = (value: unknown) => {
|
|
106
|
+
const status = decodeUiStatus(encodeUiStatus(value as ReviewUiStatus))
|
|
107
|
+
if (!status) return
|
|
108
|
+
state.apply(status)
|
|
109
|
+
if (status.phase === "manual")
|
|
110
|
+
ctx.ui.toast.show({
|
|
111
|
+
variant: "warning",
|
|
112
|
+
title: "Manual review required",
|
|
113
|
+
message: status.reason ?? "The host requires your decision.",
|
|
114
|
+
})
|
|
115
|
+
}
|
|
116
|
+
const update = (value: unknown, owner: AbortController) => {
|
|
117
|
+
if (owner !== controller || owner.signal.aborted) return
|
|
118
|
+
const event = object(value)
|
|
119
|
+
if (!event || event.directory !== directory) return
|
|
120
|
+
if (typeof event.generation !== "string") return
|
|
121
|
+
if (event.generation !== generation) {
|
|
122
|
+
owner.abort()
|
|
123
|
+
return
|
|
124
|
+
}
|
|
125
|
+
if (typeof event.revision !== "number" || event.revision <= sequence) return
|
|
126
|
+
sequence = event.revision
|
|
127
|
+
applyStatus(event.status)
|
|
128
|
+
refresh()
|
|
129
|
+
}
|
|
130
|
+
const markDisconnected = () => {
|
|
131
|
+
for (const status of state.all())
|
|
132
|
+
if (status.phase === "reviewing")
|
|
133
|
+
state.apply({
|
|
134
|
+
...status,
|
|
135
|
+
phase: "unknown",
|
|
136
|
+
emittedAt: Date.now(),
|
|
137
|
+
reason: "Reconnecting to reviewer status",
|
|
138
|
+
})
|
|
139
|
+
refresh()
|
|
140
|
+
}
|
|
141
|
+
const connect = async () => {
|
|
142
|
+
controller?.abort()
|
|
143
|
+
clearTimeout(reconnect)
|
|
144
|
+
if (!directory || stopped) return
|
|
145
|
+
const scopeDirectory = directory
|
|
146
|
+
const current = new AbortController()
|
|
147
|
+
controller = current
|
|
148
|
+
const buffered: unknown[] = []
|
|
149
|
+
let loading = true
|
|
150
|
+
const stream = (async () => {
|
|
151
|
+
for await (const event of rpc.events.subscribe("review.updated", {
|
|
152
|
+
signal: current.signal,
|
|
153
|
+
})) {
|
|
154
|
+
if (event.location.directory !== scopeDirectory) continue
|
|
155
|
+
if (loading) {
|
|
156
|
+
if (buffered.length >= 512) throw new Error("Reviewer event buffer overflow")
|
|
157
|
+
buffered.push(event.data)
|
|
158
|
+
} else update(event.data, current)
|
|
159
|
+
}
|
|
160
|
+
})()
|
|
161
|
+
void stream.catch(() => {})
|
|
162
|
+
try {
|
|
163
|
+
const snapshot = object(
|
|
164
|
+
await rpc.snapshot({}, { location: { directory: scopeDirectory }, signal: current.signal }),
|
|
165
|
+
)
|
|
166
|
+
if (
|
|
167
|
+
!snapshot ||
|
|
168
|
+
snapshot.directory !== scopeDirectory ||
|
|
169
|
+
typeof snapshot.generation !== "string" ||
|
|
170
|
+
typeof snapshot.revision !== "number" ||
|
|
171
|
+
!Array.isArray(snapshot.reviews)
|
|
172
|
+
)
|
|
173
|
+
throw new Error("Invalid reviewer snapshot")
|
|
174
|
+
if (current.signal.aborted) return
|
|
175
|
+
state = new ReviewUiState({ model: "", variant: "", timeoutMs: 180_000 })
|
|
176
|
+
generation = snapshot.generation
|
|
177
|
+
sequence = snapshot.revision
|
|
178
|
+
for (const status of snapshot.reviews) applyStatus(status)
|
|
179
|
+
loading = false
|
|
180
|
+
for (const event of buffered) update(event, current)
|
|
181
|
+
refresh()
|
|
182
|
+
await stream
|
|
183
|
+
if (controller === current && !stopped) markDisconnected()
|
|
184
|
+
} catch {
|
|
185
|
+
if (controller !== current || stopped) return
|
|
186
|
+
// Unknown means the UI lacks authoritative state, never human approval.
|
|
187
|
+
markDisconnected()
|
|
188
|
+
} finally {
|
|
189
|
+
current.abort()
|
|
190
|
+
await stream.catch(() => {})
|
|
191
|
+
if (!stopped && controller === current)
|
|
192
|
+
reconnect = setTimeout(() => {
|
|
193
|
+
void connect()
|
|
194
|
+
}, 1000)
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
void connect()
|
|
198
|
+
const slot = ctx.ui.slot({
|
|
199
|
+
append: "app",
|
|
200
|
+
render: () => {
|
|
201
|
+
const reviewing = () => {
|
|
202
|
+
const status = active()
|
|
203
|
+
return status?.phase === "reviewing" ? status : undefined
|
|
204
|
+
}
|
|
205
|
+
const terminal = () => {
|
|
206
|
+
const status = active()
|
|
207
|
+
return status && status.phase !== "reviewing" && status.phase !== "manual"
|
|
208
|
+
? status
|
|
209
|
+
: undefined
|
|
210
|
+
}
|
|
211
|
+
return (
|
|
212
|
+
<>
|
|
213
|
+
<Show when={reviewing()} keyed>
|
|
214
|
+
{(current) => (
|
|
215
|
+
<box position="absolute" bottom={0} left={0} right={0}>
|
|
216
|
+
<ReviewProgress theme={theme} status={current} frame={frame} />
|
|
217
|
+
</box>
|
|
218
|
+
)}
|
|
219
|
+
</Show>
|
|
220
|
+
<Show when={terminal()} keyed>
|
|
221
|
+
{(current) => (
|
|
222
|
+
<box position="absolute" bottom={0} left={0} right={0}>
|
|
223
|
+
<ReviewResult theme={theme} status={current} />
|
|
224
|
+
</box>
|
|
225
|
+
)}
|
|
226
|
+
</Show>
|
|
227
|
+
</>
|
|
228
|
+
)
|
|
229
|
+
},
|
|
230
|
+
})
|
|
231
|
+
const timer = setInterval(() => {
|
|
232
|
+
tick((value) => value + 1)
|
|
233
|
+
const route = ctx.ui.router.current()
|
|
234
|
+
const nextSession = route.type === "session" ? route.sessionID : undefined
|
|
235
|
+
if (nextSession !== routeSession) {
|
|
236
|
+
routeSession = nextSession
|
|
237
|
+
refresh()
|
|
238
|
+
}
|
|
239
|
+
const nextDirectory =
|
|
240
|
+
route.type === "session"
|
|
241
|
+
? (ctx.data.session.get(route.sessionID)?.location.directory ?? ctx.location?.directory)
|
|
242
|
+
: ctx.location?.directory
|
|
243
|
+
if (nextDirectory !== directory) {
|
|
244
|
+
directory = nextDirectory
|
|
245
|
+
state = new ReviewUiState({ model: "", variant: "", timeoutMs: 180_000 })
|
|
246
|
+
refresh()
|
|
247
|
+
void connect()
|
|
248
|
+
}
|
|
249
|
+
const expired = state.expire()
|
|
250
|
+
if (expired.length) controller?.abort()
|
|
251
|
+
if (expired.length || state.dismissResults().length) refresh()
|
|
252
|
+
}, 250)
|
|
253
|
+
return () => {
|
|
254
|
+
stopped = true
|
|
255
|
+
controller?.abort()
|
|
256
|
+
clearTimeout(reconnect)
|
|
257
|
+
clearInterval(timer)
|
|
258
|
+
popMode?.()
|
|
259
|
+
slot()
|
|
260
|
+
}
|
|
261
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ActorProfile,
|
|
3
|
+
EscalationDisposition,
|
|
4
|
+
PermissionRequest,
|
|
5
|
+
ReviewDecision,
|
|
6
|
+
} from "./types.ts"
|
|
7
|
+
|
|
8
|
+
export const UI_COMMAND_PREFIX = "opencode-permission-reviewer.status."
|
|
9
|
+
// Time to wait for the server's first "reviewing" ack after permission.asked.
|
|
10
|
+
// Short enough to surface a broken publish path, long enough to survive a busy
|
|
11
|
+
// event bus without flipping the panel to "manual" before the review starts.
|
|
12
|
+
export const UI_START_GRACE_MS = 15_000
|
|
13
|
+
export const UI_WATCHDOG_GRACE_MS = 5_000
|
|
14
|
+
|
|
15
|
+
export type ReviewUiPhase = "reviewing" | "approved" | "denied" | "manual" | "unknown"
|
|
16
|
+
|
|
17
|
+
export interface ReviewUiStatus {
|
|
18
|
+
version: 1
|
|
19
|
+
requestID: string
|
|
20
|
+
sessionID: string
|
|
21
|
+
phase: ReviewUiPhase
|
|
22
|
+
permission: string
|
|
23
|
+
action: string
|
|
24
|
+
model: string
|
|
25
|
+
variant: string
|
|
26
|
+
emittedAt: number
|
|
27
|
+
timeoutMs: number
|
|
28
|
+
reason?: string
|
|
29
|
+
decision?: ReviewDecision
|
|
30
|
+
/**
|
|
31
|
+
* When phase is `denied` and this is `"deny"`, the block came from fail-closed
|
|
32
|
+
* escalation disposition rather than an explicit reviewer/policy deny.
|
|
33
|
+
*/
|
|
34
|
+
escalationDisposition?: EscalationDisposition
|
|
35
|
+
/** Resolved actor (absent on the initial "reviewing" phase, before context
|
|
36
|
+
* collection completes). */
|
|
37
|
+
actorName?: string
|
|
38
|
+
actorProfile?: ActorProfile
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function boundedText(value: string, max: number): string {
|
|
42
|
+
const compact = value.replace(/\s+/g, " ").trim()
|
|
43
|
+
if (compact.length <= max) return compact
|
|
44
|
+
return `${compact.slice(0, Math.max(0, max - 1))}…`
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function permissionAction(request: PermissionRequest): string {
|
|
48
|
+
const metadata = request.metadata
|
|
49
|
+
const candidates = [
|
|
50
|
+
metadata.command,
|
|
51
|
+
metadata.filepath,
|
|
52
|
+
metadata.url,
|
|
53
|
+
metadata.query,
|
|
54
|
+
metadata.path,
|
|
55
|
+
request.patterns.join(", "),
|
|
56
|
+
]
|
|
57
|
+
const detail = candidates.find(
|
|
58
|
+
(value): value is string => typeof value === "string" && value.trim().length > 0,
|
|
59
|
+
)
|
|
60
|
+
const prefix = request.permission === "bash" && detail ? "$ " : ""
|
|
61
|
+
return boundedText(`${prefix}${detail ?? request.permission}`, 500)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function createUiStatus(
|
|
65
|
+
request: PermissionRequest,
|
|
66
|
+
phase: ReviewUiPhase,
|
|
67
|
+
options: {
|
|
68
|
+
model: string
|
|
69
|
+
variant: string
|
|
70
|
+
timeoutMs: number
|
|
71
|
+
reason?: string
|
|
72
|
+
decision?: ReviewDecision
|
|
73
|
+
escalationDisposition?: EscalationDisposition
|
|
74
|
+
emittedAt?: number
|
|
75
|
+
actorName?: string
|
|
76
|
+
actorProfile?: ActorProfile
|
|
77
|
+
},
|
|
78
|
+
): ReviewUiStatus {
|
|
79
|
+
return {
|
|
80
|
+
version: 1,
|
|
81
|
+
requestID: request.id,
|
|
82
|
+
sessionID: request.sessionID,
|
|
83
|
+
phase,
|
|
84
|
+
permission: request.permission,
|
|
85
|
+
action: permissionAction(request),
|
|
86
|
+
model: boundedText(options.model, 200),
|
|
87
|
+
variant: boundedText(options.variant, 100),
|
|
88
|
+
emittedAt: options.emittedAt ?? Date.now(),
|
|
89
|
+
timeoutMs: options.timeoutMs,
|
|
90
|
+
...(options.reason === undefined ? {} : { reason: boundedText(options.reason, 2_000) }),
|
|
91
|
+
...(options.decision === undefined ? {} : { decision: options.decision }),
|
|
92
|
+
...(options.escalationDisposition === undefined
|
|
93
|
+
? {}
|
|
94
|
+
: { escalationDisposition: options.escalationDisposition }),
|
|
95
|
+
...(options.actorName === undefined ? {} : { actorName: boundedText(options.actorName, 100) }),
|
|
96
|
+
...(options.actorProfile === undefined ? {} : { actorProfile: options.actorProfile }),
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
101
|
+
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function isDecision(value: unknown): value is ReviewDecision {
|
|
105
|
+
if (!isRecord(value)) return false
|
|
106
|
+
return (
|
|
107
|
+
(value.outcome === "allow" || value.outcome === "deny" || value.outcome === "escalate") &&
|
|
108
|
+
(value.risk_level === "low" ||
|
|
109
|
+
value.risk_level === "medium" ||
|
|
110
|
+
value.risk_level === "high" ||
|
|
111
|
+
value.risk_level === "critical") &&
|
|
112
|
+
(value.user_authorization === "high" ||
|
|
113
|
+
value.user_authorization === "medium" ||
|
|
114
|
+
value.user_authorization === "low" ||
|
|
115
|
+
value.user_authorization === "unknown") &&
|
|
116
|
+
typeof value.rationale === "string" &&
|
|
117
|
+
typeof value.confidence === "number" &&
|
|
118
|
+
Number.isFinite(value.confidence) &&
|
|
119
|
+
value.confidence >= 0 &&
|
|
120
|
+
value.confidence <= 1
|
|
121
|
+
)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function parseUiStatus(value: unknown): ReviewUiStatus | undefined {
|
|
125
|
+
if (!isRecord(value) || value.version !== 1) return
|
|
126
|
+
if (typeof value.requestID !== "string" || typeof value.sessionID !== "string") return
|
|
127
|
+
if (
|
|
128
|
+
value.phase !== "reviewing" &&
|
|
129
|
+
value.phase !== "approved" &&
|
|
130
|
+
value.phase !== "denied" &&
|
|
131
|
+
value.phase !== "manual" &&
|
|
132
|
+
value.phase !== "unknown"
|
|
133
|
+
) {
|
|
134
|
+
return
|
|
135
|
+
}
|
|
136
|
+
if (typeof value.permission !== "string" || typeof value.action !== "string") return
|
|
137
|
+
if (typeof value.model !== "string" || typeof value.variant !== "string") return
|
|
138
|
+
if (typeof value.emittedAt !== "number" || !Number.isFinite(value.emittedAt)) return
|
|
139
|
+
if (
|
|
140
|
+
typeof value.timeoutMs !== "number" ||
|
|
141
|
+
!Number.isFinite(value.timeoutMs) ||
|
|
142
|
+
value.timeoutMs < 0
|
|
143
|
+
)
|
|
144
|
+
return
|
|
145
|
+
if (value.reason !== undefined && typeof value.reason !== "string") return
|
|
146
|
+
if (value.decision !== undefined && !isDecision(value.decision)) return
|
|
147
|
+
if (
|
|
148
|
+
value.escalationDisposition !== undefined &&
|
|
149
|
+
value.escalationDisposition !== "manual" &&
|
|
150
|
+
value.escalationDisposition !== "deny"
|
|
151
|
+
) {
|
|
152
|
+
return
|
|
153
|
+
}
|
|
154
|
+
if (value.actorName !== undefined && typeof value.actorName !== "string") return
|
|
155
|
+
if (
|
|
156
|
+
value.actorProfile !== undefined &&
|
|
157
|
+
value.actorProfile !== "read-only" &&
|
|
158
|
+
value.actorProfile !== "validation" &&
|
|
159
|
+
value.actorProfile !== "workspace" &&
|
|
160
|
+
value.actorProfile !== "operator" &&
|
|
161
|
+
value.actorProfile !== "reviewer" &&
|
|
162
|
+
value.actorProfile !== "unknown"
|
|
163
|
+
) {
|
|
164
|
+
return
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return value as unknown as ReviewUiStatus
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export function encodeUiStatus(status: ReviewUiStatus): string {
|
|
171
|
+
return `${UI_COMMAND_PREFIX}${Buffer.from(JSON.stringify(status), "utf8").toString("base64url")}`
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function decodeUiStatus(command: string): ReviewUiStatus | undefined {
|
|
175
|
+
if (!command.startsWith(UI_COMMAND_PREFIX)) return
|
|
176
|
+
const encoded = command.slice(UI_COMMAND_PREFIX.length)
|
|
177
|
+
if (!encoded || encoded.length > 16_000) return
|
|
178
|
+
try {
|
|
179
|
+
return parseUiStatus(JSON.parse(Buffer.from(encoded, "base64url").toString("utf8")))
|
|
180
|
+
} catch {
|
|
181
|
+
return
|
|
182
|
+
}
|
|
183
|
+
}
|