-
Notifications
You must be signed in to change notification settings - Fork 15.9k
Expand file tree
/
Copy pathPermissionMode.ts
More file actions
137 lines (123 loc) · 3.29 KB
/
PermissionMode.ts
File metadata and controls
137 lines (123 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { feature } from 'bun:bundle'
import z from 'zod/v4'
import { PAUSE_ICON } from '../../constants/figures.js'
// Types extracted to src/types/permissions.ts to break import cycles
import {
EXTERNAL_PERMISSION_MODES,
type ExternalPermissionMode,
PERMISSION_MODES,
type PermissionMode,
} from '../../types/permissions.js'
import { lazySchema } from '../lazySchema.js'
// Re-export for backwards compatibility
export {
EXTERNAL_PERMISSION_MODES,
PERMISSION_MODES,
type ExternalPermissionMode,
type PermissionMode,
}
export const permissionModeSchema = lazySchema(() => z.enum(PERMISSION_MODES))
export const externalPermissionModeSchema = lazySchema(() =>
z.enum(EXTERNAL_PERMISSION_MODES),
)
type ModeColorKey =
| 'text'
| 'planMode'
| 'permission'
| 'autoAccept'
| 'error'
| 'warning'
type PermissionModeConfig = {
title: string
shortTitle: string
symbol: string
color: ModeColorKey
external: ExternalPermissionMode
}
const PERMISSION_MODE_CONFIG: Partial<
Record<PermissionMode, PermissionModeConfig>
> = {
default: {
title: 'Default',
shortTitle: 'Default',
symbol: '',
color: 'text',
external: 'default',
},
plan: {
title: 'Plan Mode',
shortTitle: 'Plan',
symbol: PAUSE_ICON,
color: 'planMode',
external: 'plan',
},
acceptEdits: {
title: 'Accept edits',
shortTitle: 'Accept',
symbol: '⏵⏵',
color: 'autoAccept',
external: 'acceptEdits',
},
bypassPermissions: {
title: 'Bypass',
shortTitle: 'Bypass',
symbol: '⏵⏵',
color: 'error',
external: 'bypassPermissions',
},
dontAsk: {
title: "Don't Ask",
shortTitle: 'DontAsk',
symbol: '⏵⏵',
color: 'error',
external: 'dontAsk',
},
auto: {
title: 'Auto',
shortTitle: 'Auto',
symbol: '⏵⏵',
color: 'warning' as ModeColorKey,
external: 'default' as ExternalPermissionMode,
},
}
/**
* Type guard to check if a PermissionMode is an ExternalPermissionMode.
* auto is ant-only and excluded from external modes.
*/
export function isExternalPermissionMode(
mode: PermissionMode,
): mode is ExternalPermissionMode {
// External users can't have auto, so always true for them
if (process.env.USER_TYPE !== 'ant') {
return true
}
return mode !== 'auto' && mode !== 'bubble'
}
function getModeConfig(mode: PermissionMode): PermissionModeConfig {
return PERMISSION_MODE_CONFIG[mode] ?? PERMISSION_MODE_CONFIG.default!
}
export function toExternalPermissionMode(
mode: PermissionMode,
): ExternalPermissionMode {
return getModeConfig(mode).external
}
export function permissionModeFromString(str: string): PermissionMode {
return (PERMISSION_MODES as readonly string[]).includes(str)
? (str as PermissionMode)
: 'default'
}
export function permissionModeTitle(mode: PermissionMode): string {
return getModeConfig(mode).title
}
export function isDefaultMode(mode: PermissionMode | undefined): boolean {
return mode === 'default' || mode === undefined
}
export function permissionModeShortTitle(mode: PermissionMode): string {
return getModeConfig(mode).shortTitle
}
export function permissionModeSymbol(mode: PermissionMode): string {
return getModeConfig(mode).symbol
}
export function getModeColor(mode: PermissionMode): ModeColorKey {
return getModeConfig(mode).color
}