Open History to see the last commit
1import { approvalId } from '@tanstack/ai-sandbox'
2
3/**
4 * Permission modes for the OpenCode adapter, mirroring the Claude Code and
5 * Gemini CLI adapters' semantics:
6 *
7 * - `'default'`: bridged TanStack tools run; anything else that asks for
8 * permission is rejected with no prompt (a headless server must never hang
9 * on an interactive question).
10 * - `'acceptEdits'`: additionally auto-approves file-mutation requests
11 * (edit / write / patch).
12 * - `'bypassPermissions'`: approves everything.
13 */
14export type OpencodePermissionMode =
15 | 'default'
16 | 'acceptEdits'
17 | 'bypassPermissions'
18
19/** Structural subset of an OpenCode `permission.updated` payload. */
20export interface OpencodePermissionRequest {
21 id: string
22 sessionID: string
23 /** Permission category, e.g. `'edit'`, `'bash'`, `'webfetch'`, a tool id. */
24 type: string
25 title: string
26 /** Tool call id this permission gates, when it gates a tool. */
27 callID?: string
28}
29
30/** OpenCode permission reply: allow once, allow always, or reject. */
31export type OpencodePermissionResponse = 'once' | 'always' | 'reject'
32
33/** Custom permission handler; replaces the adapter's default policy. */
34export type PermissionHandler = (
35 request: OpencodePermissionRequest,
36) => Promise<OpencodePermissionResponse> | OpencodePermissionResponse
37
38/** Permission categories treated as file mutations for `'acceptEdits'`. */
39const EDIT_TYPES = new Set(['edit', 'write', 'patch'])
40
41/**
42 * Decide whether an OpenCode permission request targets one of the bridged
43 * TanStack tools. OpenCode names MCP tools `<server>_<tool>` (e.g.
44 * `tanstack_lookup_user`), so a request is bridged when its type or title is
45 * a registered tool name, or carries the `tanstack` server prefix.
46 */
47export function matchBridgedToolName(
48 request: OpencodePermissionRequest,
49 bridgedToolNames: ReadonlySet<string> | undefined,
50): boolean {
51 if (!bridgedToolNames || bridgedToolNames.size === 0) return false
52 for (const field of [request.type, request.title]) {
53 if (typeof field !== 'string' || field === '') continue
54 if (bridgedToolNames.has(field)) return true
55 if (field.startsWith('tanstack_') && bridgedToolNames.has(field.slice(9))) {
56 return true
57 }
58 if (field.startsWith('tanstack.') && bridgedToolNames.has(field.slice(9))) {
59 return true
60 }
61 }
62 return false
63}
64
65/**
66 * The adapter's default permission policy. Always answers immediately — never
67 * hangs a headless server on a question only an interactive user could
68 * answer.
69 */
70export function resolvePermission(
71 request: OpencodePermissionRequest,
72 mode: OpencodePermissionMode,
73 bridgedToolNames: ReadonlySet<string> | undefined,
74): OpencodePermissionResponse {
75 if (matchBridgedToolName(request, bridgedToolNames)) {
76 return 'once'
77 }
78 if (mode === 'bypassPermissions') {
79 return 'once'
80 }
81 if (mode === 'acceptEdits' && EDIT_TYPES.has(request.type)) {
82 return 'once'
83 }
84 return 'reject'
85}
86
87/**
88 * Interactive variant: when the mode/bridge policy would reject, consult the
89 * client's approval decisions. Returns the OpenCode response plus, when the
90 * action still needs a client decision, the `approvalId`/`title` the adapter
91 * should surface via an `approval-requested` event.
92 */
93export function resolveInteractivePermission(
94 request: OpencodePermissionRequest,
95 mode: OpencodePermissionMode,
96 bridgedToolNames: ReadonlySet<string> | undefined,
97 approvals: ReadonlyMap<string, boolean> | undefined,
98): {
99 response: OpencodePermissionResponse
100 approvalId?: string
101 title?: string
102} {
103 if (matchBridgedToolName(request, bridgedToolNames))
104 return { response: 'once' }
105 if (mode === 'bypassPermissions') return { response: 'once' }
106 if (mode === 'acceptEdits' && EDIT_TYPES.has(request.type)) {
107 return { response: 'once' }
108 }
109
110 const id = approvalId({
111 provider: 'opencode',
112 kind: 'tool',
113 target: request.type || request.title,
114 })
115 const granted = approvals?.get(id)
116 if (granted === true) return { response: 'once' }
117 if (granted === false) return { response: 'reject' }
118 return { response: 'reject', approvalId: id, title: request.title }
119}