@@ -229,6 +229,85 @@ export const setTerminalServerConnections = async (token: string, connections: o
229229 return res ;
230230} ;
231231
232+ /**
233+ * Detect whether a terminal server URL points to an Orchestrator or a direct
234+ * Open Terminal instance.
235+ *
236+ * - GET {url}/api/v1/policies → 200 → "orchestrator"
237+ * - GET {url}/api/config → 200 → "terminal"
238+ * - Neither → null
239+ */
240+ export const detectTerminalServerType = async (
241+ url : string ,
242+ key : string
243+ ) : Promise < 'orchestrator' | 'terminal' | null > => {
244+ const baseUrl = url . replace ( / \/ $ / , '' ) ;
245+ const headers : Record < string , string > = { } ;
246+ if ( key ) {
247+ headers [ 'Authorization' ] = `Bearer ${ key } ` ;
248+ }
249+
250+ // Orchestrators expose a policies API; plain terminals don't.
251+ try {
252+ const res = await fetch ( `${ baseUrl } /api/v1/policies` , { headers } ) ;
253+ if ( res . ok ) return 'orchestrator' ;
254+ } catch {
255+ // ignore
256+ }
257+
258+ // Fall back to open-terminal config endpoint.
259+ try {
260+ const res = await fetch ( `${ baseUrl } /api/config` , { headers } ) ;
261+ if ( res . ok ) return 'terminal' ;
262+ } catch {
263+ // ignore
264+ }
265+
266+ return null ;
267+ } ;
268+
269+ /**
270+ * Create or update a policy on the orchestrator.
271+ * PUT {url}/api/v1/policies/{policyId}
272+ */
273+ export const putOrchestratorPolicy = async (
274+ url : string ,
275+ key : string ,
276+ policyId : string ,
277+ policyData : object
278+ ) : Promise < object | null > => {
279+ let error = null ;
280+
281+ const baseUrl = url . replace ( / \/ $ / , '' ) ;
282+ const headers : Record < string , string > = {
283+ 'Content-Type' : 'application/json'
284+ } ;
285+ if ( key ) {
286+ headers [ 'Authorization' ] = `Bearer ${ key } ` ;
287+ }
288+
289+ const res = await fetch ( `${ baseUrl } /api/v1/policies/${ encodeURIComponent ( policyId ) } ` , {
290+ method : 'PUT' ,
291+ headers,
292+ body : JSON . stringify ( policyData )
293+ } )
294+ . then ( async ( res ) => {
295+ if ( ! res . ok ) throw await res . json ( ) ;
296+ return res . json ( ) ;
297+ } )
298+ . catch ( ( err ) => {
299+ console . error ( err ) ;
300+ error = err . detail ;
301+ return null ;
302+ } ) ;
303+
304+ if ( error ) {
305+ throw error ;
306+ }
307+
308+ return res ;
309+ } ;
310+
232311export const verifyToolServerConnection = async ( token : string , connection : object ) => {
233312 let error = null ;
234313
0 commit comments