The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { msg } from "gt-i18n"; |
| 2 | import { translatableDetail } from "../errors/translatable.ts"; |
| 3 | import { |
| 4 | CLIENT_CAPABILITIES_QUERY_PARAM, |
| 5 | ORCHESTRATION_PROTOCOL_QUERY_PARAM, |
| 6 | ORCHESTRATION_PROTOCOL_VERSION, |
| 7 | SETTINGS_SECRET_REFERENCES_CAPABILITY, |
| 8 | type ExecutionEnvironmentDescriptor, |
| 9 | } from "@t3tools/contracts"; |
| 10 | |
| 11 | import { ConnectionBlockedError } from "./model.ts"; |
| 12 | |
| 13 | export function orchestrationProtocolCompatibilityError( |
| 14 | descriptor: ExecutionEnvironmentDescriptor, |
| 15 | ): ConnectionBlockedError | null { |
| 16 | // Servers shipped before negotiation use the original wire protocol. |
| 17 | const serverProtocolVersion = descriptor.orchestrationProtocolVersion ?? 1; |
| 18 | if (serverProtocolVersion === ORCHESTRATION_PROTOCOL_VERSION) { |
| 19 | return null; |
| 20 | } |
| 21 | return serverProtocolVersion > ORCHESTRATION_PROTOCOL_VERSION |
| 22 | ? new ConnectionBlockedError({ |
| 23 | reason: "unsupported", |
| 24 | ...translatableDetail( |
| 25 | msg( |
| 26 | "This client is not supported by this server. Update your app or use a compatible release to connect to {host}.", |
| 27 | { host: descriptor.label }, |
| 28 | ), |
| 29 | ), |
| 30 | }) |
| 31 | : new ConnectionBlockedError({ |
| 32 | reason: "unsupported", |
| 33 | ...translatableDetail( |
| 34 | msg("This client requires a newer server. Update T3 Code on {host} to connect.", { |
| 35 | host: descriptor.label, |
| 36 | }), |
| 37 | ), |
| 38 | ...(canSelfUpdate(descriptor) ? { serverUpdateRequired: true } : {}), |
| 39 | }); |
| 40 | } |
| 41 | |
| 42 | function canSelfUpdate(descriptor: ExecutionEnvironmentDescriptor): boolean { |
| 43 | const { serverSelfUpdate, desktopAppUpdate } = descriptor.capabilities; |
| 44 | return ( |
| 45 | serverSelfUpdate !== undefined && |
| 46 | (serverSelfUpdate !== "desktop-managed" || desktopAppUpdate === true) |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | export function appendOrchestrationProtocol(socketUrl: string): string { |
| 51 | const url = new URL(socketUrl); |
| 52 | url.searchParams.set(ORCHESTRATION_PROTOCOL_QUERY_PARAM, String(ORCHESTRATION_PROTOCOL_VERSION)); |
| 53 | url.searchParams.set(CLIENT_CAPABILITIES_QUERY_PARAM, SETTINGS_SECRET_REFERENCES_CAPABILITY); |
| 54 | return url.toString(); |
| 55 | } |