@noppu-labs/opencode-permission-reviewer 2.4.2 → 3.0.0

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.
Files changed (46) hide show
  1. package/CHANGELOG.md +44 -1
  2. package/LICENSE +21 -201
  3. package/LICENSE-APACHE +201 -0
  4. package/NOTICE +14 -6
  5. package/README.md +100 -787
  6. package/SECURITY.md +41 -22
  7. package/dist/explain.js +4932 -3522
  8. package/dist/index.d.ts +508 -547
  9. package/dist/index.js +12454 -9030
  10. package/dist/rpc.js +4 -1
  11. package/dist/tui/config/jsonc.ts +71 -63
  12. package/dist/tui/config/layer-read.ts +120 -0
  13. package/dist/tui/config/loader.ts +61 -513
  14. package/dist/tui/config/risk-policy-clamp.ts +80 -0
  15. package/dist/tui/config/trust-boundary-keys.ts +100 -0
  16. package/dist/tui/config/trust-boundary.ts +135 -0
  17. package/dist/tui/config/trusted-degradation.ts +97 -0
  18. package/dist/tui/config-fields.ts +55 -0
  19. package/dist/tui/config-knobs.ts +119 -0
  20. package/dist/tui/config-policy-rules.ts +86 -0
  21. package/dist/tui/config-risk-policy.ts +59 -0
  22. package/dist/tui/config-rule-condition.ts +150 -0
  23. package/dist/tui/config.ts +61 -430
  24. package/dist/tui/descriptor-io.ts +42 -0
  25. package/dist/tui/is-record.ts +13 -0
  26. package/dist/tui/opencode/event-normalizer.ts +41 -31
  27. package/dist/tui/reviewer-model.ts +87 -0
  28. package/dist/tui/tui.tsx +87 -99
  29. package/dist/tui/types.ts +170 -597
  30. package/dist/tui/ui/components.tsx +32 -136
  31. package/dist/tui/ui/review-connection.ts +32 -0
  32. package/dist/tui/ui/review-feed.ts +131 -0
  33. package/dist/tui/ui/review-overlay.tsx +83 -0
  34. package/dist/tui/ui/review-result.tsx +56 -0
  35. package/dist/tui/ui/review-store.ts +88 -0
  36. package/dist/tui/ui/review-theme.ts +63 -0
  37. package/dist/tui/ui/rpc.ts +5 -2
  38. package/dist/tui/ui/v1-host.ts +39 -0
  39. package/dist/tui/ui/v2.tsx +66 -220
  40. package/dist/tui/ui-protocol.ts +63 -118
  41. package/dist/tui/ui-state.ts +71 -64
  42. package/dist/tui/ui-status-parse.ts +74 -0
  43. package/package.json +16 -15
  44. package/rpc.js +2 -1
  45. package/server.js +2 -2
  46. package/tui.tsx +2 -1
@@ -0,0 +1,87 @@
1
+ import { nonEmptyStringOr } from "./config-fields.ts";
2
+ import { boundedInteger } from "./config-knobs.ts";
3
+ import type { EscalationReviewerConfig, ReviewerConfig } from "./types.ts";
4
+
5
+ export function splitModel(model: string): {
6
+ providerID: string;
7
+ modelID: string;
8
+ } {
9
+ const slash = model.indexOf("/");
10
+ if (slash <= 0 || slash === model.length - 1) {
11
+ throw new Error(
12
+ `Invalid reviewer model "${model}"; expected provider/model`,
13
+ );
14
+ }
15
+ return {
16
+ providerID: model.slice(0, slash),
17
+ modelID: model.slice(slash + 1),
18
+ };
19
+ }
20
+
21
+ export function isSystemOneReviewerModel(model: string): boolean {
22
+ let parsed: { providerID: string; modelID: string };
23
+ try {
24
+ parsed = splitModel(model);
25
+ } catch {
26
+ return false;
27
+ }
28
+ // Jev uses the typed System One API rather than a chat-session transport.
29
+ return (
30
+ ((parsed.providerID === "opencode" ||
31
+ parsed.providerID === "typesafe-ai") &&
32
+ /^jev(?:-|$)/.test(parsed.modelID)) ||
33
+ (parsed.providerID === "commandcode" && parsed.modelID === "typesafe/jev")
34
+ );
35
+ }
36
+
37
+ export function isProviderModelId(value: unknown): value is string {
38
+ return typeof value === "string" && value.includes("/");
39
+ }
40
+
41
+ /** The escalation reviewer object with a provider/model id that is not a
42
+ * System One model, or undefined when the value cannot be one. */
43
+ function escalationReviewerSource(
44
+ value: unknown,
45
+ ): (Record<string, unknown> & { model: string }) | undefined {
46
+ if (typeof value !== "object" || value === null || Array.isArray(value))
47
+ return;
48
+ const source = value as Record<string, unknown>;
49
+ if (
50
+ !isProviderModelId(source.model) ||
51
+ isSystemOneReviewerModel(source.model)
52
+ )
53
+ return;
54
+ return source as Record<string, unknown> & { model: string };
55
+ }
56
+
57
+ export function resolveEscalationReviewer(
58
+ value: unknown,
59
+ defaults: ReviewerConfig,
60
+ ): EscalationReviewerConfig | undefined {
61
+ const source = escalationReviewerSource(value);
62
+ if (source === undefined) return;
63
+ return {
64
+ model: source.model,
65
+ variant: nonEmptyStringOr(source.variant, defaults.variant),
66
+ outputFormat: source.outputFormat === "text" ? "text" : "json_schema",
67
+ timeoutMs: boundedInteger(
68
+ source.timeoutMs,
69
+ defaults.timeoutMs,
70
+ 5_000,
71
+ 600_000,
72
+ ),
73
+ };
74
+ }
75
+
76
+ export function isValidEscalationReviewer(value: unknown): boolean {
77
+ const source = escalationReviewerSource(value);
78
+ if (source === undefined) return false;
79
+ return (
80
+ (source.variant === undefined ||
81
+ (typeof source.variant === "string" && source.variant !== "")) &&
82
+ (source.outputFormat === undefined ||
83
+ source.outputFormat === "text" ||
84
+ source.outputFormat === "json_schema") &&
85
+ (source.timeoutMs === undefined || Number.isInteger(source.timeoutMs))
86
+ );
87
+ }
package/dist/tui/tui.tsx CHANGED
@@ -1,138 +1,125 @@
1
1
  /** @jsxImportSource @opentui/solid */
2
- import type { TuiPlugin, TuiPluginApi } from "@opencode-ai/plugin/tui"
3
- import { createSignal, Show } from "solid-js"
4
- import { DEFAULT_CONFIG, reviewBudgetMs } from "./config.ts"
5
- import { loadResolvedConfig } from "./config/loader.ts"
6
- // Import the normalizer directly. Going through ./runtime.ts would evaluate the
7
- // whole server engine (coordinator, git/ssh evidence, node:child_process) inside
8
- // the TUI process for a single unused re-export.
9
- import { extractPermissionRequest } from "./opencode/event-normalizer.ts"
10
- import { decodeUiStatus, type ReviewUiStatus } from "./ui-protocol.ts"
11
- import { ReviewUiState } from "./ui-state.ts"
12
- import { ReviewOverlay, ReviewResult, SPINNER } from "./ui/components.tsx"
13
- import { setupTuiV2 } from "./ui/v2.tsx"
2
+ import type { PluginOptions } from "@opencode-ai/plugin";
3
+ import type { TuiPlugin, TuiPluginApi } from "@opencode-ai/plugin/tui";
4
+ import type { JSX } from "solid-js";
5
+ import { createSignal, Show } from "solid-js";
6
+ import { loadResolvedConfig } from "./config/loader.ts";
7
+ import { reviewBudgetMs } from "./config.ts";
8
+ // Import the normalizer directly: `./index.ts` re-exports it, but importing that
9
+ // would evaluate the whole server engine (git/ssh evidence, `node:child_process`)
10
+ // inside the TUI process.
11
+ import { extractPermissionRequest } from "./opencode/event-normalizer.ts";
12
+ import { SPINNER } from "./ui/components.tsx";
13
+ import { ReviewOverlay } from "./ui/review-overlay.tsx";
14
+ import { ReviewResult } from "./ui/review-result.tsx";
15
+ import { activeStatus, notifyManual } from "./ui/v1-host.ts";
16
+ import { setupTuiV2 } from "./ui/v2.tsx";
17
+ import { decodeUiStatus, type ReviewUiStatus } from "./ui-protocol.ts";
18
+ import { ReviewUiState } from "./ui-state.ts";
14
19
 
15
- const REVIEW_MODE = "permission-reviewer"
20
+ const REVIEW_MODE = "permission-reviewer";
16
21
 
17
- function routeSessionID(api: TuiPluginApi): string | undefined {
18
- const route = api.route.current
19
- if (route.name !== "session") return
20
- const params = "params" in route ? route.params : undefined
21
- const sessionID = params?.sessionID
22
- return typeof sessionID === "string" ? sessionID : undefined
23
- }
24
-
25
- function parentSessionID(api: TuiPluginApi, sessionID: string): string | undefined {
26
- return api.state.session.get(sessionID)?.parentID
27
- }
28
-
29
- function notifyManual(api: TuiPluginApi, status: ReviewUiStatus): void {
30
- api.ui.toast({
31
- variant: "warning",
32
- title: "Manual review required",
33
- message:
34
- status.reason ?? "The reviewer could not decide. This permission now needs your approval.",
35
- duration: 8_000,
36
- })
37
- }
38
-
39
- export const tui: TuiPlugin = async (api, options) => {
40
- const config = loadResolvedConfig(options)
22
+ export const tui: TuiPlugin = async (
23
+ api: TuiPluginApi,
24
+ options: PluginOptions | undefined,
25
+ ) => {
26
+ const config = loadResolvedConfig(options);
41
27
  const state = new ReviewUiState({
42
28
  model: config.model,
43
29
  variant: config.variant,
44
30
  timeoutMs: reviewBudgetMs(config),
45
- })
31
+ });
46
32
 
47
33
  // Signal bumps whenever the UI state changes. Both slot factories read it
48
34
  // directly in their bodies, which makes the host re-render each panel:
49
35
  // a factory that only returns a component without reading any signal in its
50
36
  // own scope renders exactly once at boot and never updates again.
51
- const [revision, setRevision] = createSignal(0)
52
- const [frame, setFrame] = createSignal(0)
53
- const touch = () => {
54
- syncMode()
55
- setRevision((value) => value + 1)
56
- }
37
+ const [revision, setRevision] = createSignal(0);
38
+ const [frame, setFrame] = createSignal(0);
39
+ const touch = (): void => {
40
+ syncMode();
41
+ setRevision((value) => value + 1);
42
+ };
57
43
 
58
- const active = () => {
59
- const sessionID = routeSessionID(api)
60
- if (!sessionID) return
61
- return state.activeFor(sessionID, (id) => parentSessionID(api, id))
62
- }
44
+ const active = (): ReviewUiStatus | undefined => activeStatus(api, state);
63
45
 
64
- let popMode: (() => void) | undefined
65
- const syncMode = () => {
66
- const status = active()
67
- const wanted = status?.phase === "reviewing"
46
+ let popMode: (() => void) | undefined;
47
+ const syncMode = (): void => {
48
+ const status = active();
49
+ const wanted = status?.phase === "reviewing";
68
50
  if (wanted && popMode === undefined) {
69
51
  try {
70
- popMode = api.mode.push(REVIEW_MODE)
52
+ popMode = api.mode.push(REVIEW_MODE);
71
53
  } catch {
72
54
  // mode.push can throw if the host rejects unknown modes; keep the panel.
73
55
  }
74
56
  } else if (!wanted && popMode !== undefined) {
75
- popMode()
76
- popMode = undefined
57
+ popMode();
58
+ popMode = undefined;
77
59
  }
78
- }
60
+ };
79
61
 
80
62
  api.event.on("permission.asked", (event) => {
81
- const request = extractPermissionRequest(event)
82
- if (!request) return
83
- state.asked(request)
84
- touch()
85
- })
63
+ const request = extractPermissionRequest(event);
64
+ if (!request) return;
65
+ state.asked(request);
66
+ touch();
67
+ });
86
68
  api.event.on("permission.replied", (event) => {
87
- state.replied(event.properties.requestID)
88
- touch()
89
- })
69
+ state.replied(event.properties.requestID);
70
+ touch();
71
+ });
90
72
  api.event.on("tui.command.execute", (event) => {
91
- const status = decodeUiStatus(event.properties.command)
92
- if (!status) return
93
- if (!state.apply(status)) return
94
- if (status.phase === "manual") notifyManual(api, status)
95
- touch()
96
- })
73
+ const status = decodeUiStatus(event.properties.command);
74
+ if (!status) return;
75
+ if (!state.apply(status)) return;
76
+ if (status.phase === "manual") notifyManual(api, status);
77
+ touch();
78
+ });
97
79
 
98
80
  // Keep watchdog and result expiry running even when the UI is hidden.
99
81
  const ticker = setInterval(() => {
100
- if (active()?.phase === "reviewing") setFrame((value) => (value + 1) % SPINNER.length)
101
- const expired = state.expire()
102
- const dismissed = state.dismissResults()
103
- if (expired.length > 0 || dismissed.length > 0) touch()
104
- }, 250)
82
+ if (active()?.phase === "reviewing")
83
+ setFrame((value) => (value + 1) % SPINNER.length);
84
+ const expired = state.expire();
85
+ const dismissed = state.dismissResults();
86
+ if (expired.length > 0 || dismissed.length > 0) touch();
87
+ }, 250);
105
88
 
106
89
  api.lifecycle.onDispose(() => {
107
- clearInterval(ticker)
108
- popMode?.()
109
- popMode = undefined
110
- })
90
+ clearInterval(ticker);
91
+ popMode?.();
92
+ popMode = undefined;
93
+ });
111
94
 
112
95
  api.slots.register({
113
96
  order: 1_000,
114
97
  slots: {
115
- app() {
98
+ app(): JSX.Element {
116
99
  // Load-bearing direct read: it subscribes the slot's render pass to
117
100
  // every state transition (touch), so the factory re-runs and renders
118
101
  // the current status. The spinner frame is deliberately NOT read here
119
102
  // (only inside the panel's text children) so 250ms ticks update those
120
103
  // texts without re-running this factory.
121
- revision()
122
- const status = active()
123
- syncMode()
104
+ revision();
105
+ const status = active();
106
+ syncMode();
124
107
  return (
125
108
  <Show when={status?.phase === "reviewing" ? status : undefined} keyed>
126
- {(current) => (
127
- <ReviewOverlay theme={() => api.theme.current} status={current} frame={frame} />
109
+ {(current: ReviewUiStatus) => (
110
+ <ReviewOverlay
111
+ theme={() => api.theme.current}
112
+ status={current}
113
+ frame={frame}
114
+ />
128
115
  )}
129
116
  </Show>
130
- )
117
+ );
131
118
  },
132
- app_bottom() {
133
- revision()
134
- const status = active()
135
- syncMode()
119
+ app_bottom(): JSX.Element {
120
+ revision();
121
+ const status = active();
122
+ syncMode();
136
123
  return (
137
124
  <Show
138
125
  when={
@@ -144,19 +131,20 @@ export const tui: TuiPlugin = async (api, options) => {
144
131
  }
145
132
  keyed
146
133
  >
147
- {(current) => <ReviewResult theme={() => api.theme.current} status={current} />}
134
+ {(current: ReviewUiStatus) => (
135
+ <ReviewResult theme={() => api.theme.current} status={current} />
136
+ )}
148
137
  </Show>
149
- )
138
+ );
150
139
  },
151
140
  },
152
- })
153
- }
141
+ });
142
+ };
154
143
 
155
- const module = {
144
+ const module: { id: string; tui: TuiPlugin; setup: typeof setupTuiV2 } = {
156
145
  id: "opencode-permission-reviewer",
157
146
  tui,
158
147
  setup: setupTuiV2,
159
- }
148
+ };
160
149
 
161
- export default module
162
- export { ReviewUiState, decodeUiStatus, DEFAULT_CONFIG }
150
+ export default module;