|
| 1 | +import { Operation } from '../api-types'; |
| 2 | +import { InterceptorStore } from '../../../model/interception/interceptor-store'; |
| 3 | +import { Interceptor } from '../../../model/interception/interceptors'; |
| 4 | + |
| 5 | +function isNonInteractive(interceptor: Interceptor): boolean { |
| 6 | + return !interceptor.uiConfig && !interceptor.customActivation && !interceptor.clientOnly; |
| 7 | +} |
| 8 | + |
| 9 | +export function registerInterceptorOperations( |
| 10 | + registry: { register(op: Operation): void }, |
| 11 | + interceptorStore: InterceptorStore |
| 12 | +): void { |
| 13 | + registry.register(interceptorsListOperation(interceptorStore)); |
| 14 | + registry.register(interceptorsActivateOperation(interceptorStore)); |
| 15 | +} |
| 16 | + |
| 17 | +function serializeInterceptor(interceptor: Interceptor) { |
| 18 | + return { |
| 19 | + id: interceptor.id, |
| 20 | + name: interceptor.name, |
| 21 | + description: interceptor.description.join(' '), |
| 22 | + isActive: interceptor.isActive, |
| 23 | + isActivable: interceptor.isActivable, |
| 24 | + isSupported: interceptor.isSupported, |
| 25 | + inProgress: !!interceptor.inProgress |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +function interceptorsListOperation(interceptorStore: InterceptorStore): Operation { |
| 30 | + return { |
| 31 | + definition: { |
| 32 | + name: 'interceptors.list', |
| 33 | + description: 'List available interceptors and their current status. ' + |
| 34 | + 'Shows which interceptors are supported, activable, and currently active.', |
| 35 | + category: 'interceptors', |
| 36 | + inputSchema: { |
| 37 | + type: 'object', |
| 38 | + properties: {} |
| 39 | + }, |
| 40 | + outputSchema: { |
| 41 | + type: 'object', |
| 42 | + properties: { |
| 43 | + interceptors: { |
| 44 | + type: 'array', |
| 45 | + items: { |
| 46 | + type: 'object', |
| 47 | + properties: { |
| 48 | + id: { type: 'string' }, |
| 49 | + name: { type: 'string' }, |
| 50 | + description: { type: 'string' }, |
| 51 | + isActive: { type: 'boolean' }, |
| 52 | + isActivable: { type: 'boolean' }, |
| 53 | + isSupported: { type: 'boolean' }, |
| 54 | + inProgress: { type: 'boolean' } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + }, |
| 61 | + handler: async () => { |
| 62 | + const interceptors = Object.values(interceptorStore.interceptors) |
| 63 | + .filter((i): i is Interceptor => !!i) |
| 64 | + .map(serializeInterceptor); |
| 65 | + |
| 66 | + return { |
| 67 | + success: true, |
| 68 | + data: { interceptors } |
| 69 | + }; |
| 70 | + } |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +function interceptorsActivateOperation(interceptorStore: InterceptorStore): Operation { |
| 75 | + return { |
| 76 | + definition: { |
| 77 | + name: 'interceptors.activate', |
| 78 | + description: 'Activate a non-interactive interceptor. Only simple interceptors that ' + |
| 79 | + 'require no UI interaction or confirmation are supported (e.g. fresh browser windows, ' + |
| 80 | + 'fresh-terminal, system-proxy). Interactive interceptors like docker-attach, ' + |
| 81 | + 'android-adb, electron etc. require the full UI.', |
| 82 | + category: 'interceptors', |
| 83 | + inputSchema: { |
| 84 | + type: 'object', |
| 85 | + properties: { |
| 86 | + id: { |
| 87 | + type: 'string', |
| 88 | + description: 'The interceptor ID to activate (e.g. "fresh-chrome", "fresh-terminal", "system-proxy")' |
| 89 | + } |
| 90 | + }, |
| 91 | + required: ['id'] |
| 92 | + }, |
| 93 | + outputSchema: { |
| 94 | + type: 'object', |
| 95 | + properties: { |
| 96 | + success: { type: 'boolean' } |
| 97 | + } |
| 98 | + } |
| 99 | + }, |
| 100 | + handler: async (params) => { |
| 101 | + const id = params.id as string; |
| 102 | + |
| 103 | + if (!id) { |
| 104 | + return { |
| 105 | + success: false, |
| 106 | + error: { code: 'INVALID_PARAMS', message: 'Missing required parameter: id' } |
| 107 | + }; |
| 108 | + } |
| 109 | + |
| 110 | + const interceptor = interceptorStore.interceptors[id]; |
| 111 | + if (!interceptor) { |
| 112 | + return { |
| 113 | + success: false, |
| 114 | + error: { code: 'NOT_FOUND', message: `Unknown interceptor: ${id}` } |
| 115 | + }; |
| 116 | + } |
| 117 | + |
| 118 | + if (!isNonInteractive(interceptor)) { |
| 119 | + return { |
| 120 | + success: false, |
| 121 | + error: { |
| 122 | + code: 'INTERACTIVE_REQUIRED', |
| 123 | + message: `Interceptor '${id}' (${interceptor.name}) requires interactive setup ` + |
| 124 | + `through the UI. Only non-interactive interceptors can be activated via this API.` |
| 125 | + } |
| 126 | + }; |
| 127 | + } |
| 128 | + |
| 129 | + if (!interceptor.isActivable) { |
| 130 | + return { |
| 131 | + success: false, |
| 132 | + error: { |
| 133 | + code: 'NOT_ACTIVABLE', |
| 134 | + message: `Interceptor '${id}' (${interceptor.name}) is not currently activable. ` + |
| 135 | + `It may not be installed or supported on this system.` |
| 136 | + } |
| 137 | + }; |
| 138 | + } |
| 139 | + |
| 140 | + try { |
| 141 | + await interceptorStore.activateInterceptor( |
| 142 | + id, |
| 143 | + interceptor.activationOptions |
| 144 | + ); |
| 145 | + return { success: true, data: {} }; |
| 146 | + } catch (e) { |
| 147 | + return { |
| 148 | + success: false, |
| 149 | + error: { |
| 150 | + code: 'ACTIVATION_FAILED', |
| 151 | + message: `Failed to activate interceptor '${id}': ${ |
| 152 | + e instanceof Error ? e.message : String(e) |
| 153 | + }` |
| 154 | + } |
| 155 | + }; |
| 156 | + } |
| 157 | + } |
| 158 | + }; |
| 159 | +} |
0 commit comments