The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { |
| 2 | AtomCommand, |
| 3 | AtomCommandOptions, |
| 4 | AtomCommandResult, |
| 5 | } from "@t3tools/client-runtime/state/runtime"; |
| 6 | import { |
| 7 | AuthOrchestrationOperateScope, |
| 8 | EnvironmentAuthorizationError, |
| 9 | type EnvironmentId, |
| 10 | } from "@t3tools/contracts"; |
| 11 | import * as Cause from "effect/Cause"; |
| 12 | import { AsyncResult } from "effect/reactivity"; |
| 13 | import { msg } from "gt-react"; |
| 14 | |
| 15 | import { m } from "../i18n/messages"; |
| 16 | |
| 17 | import { readEnvironmentScope } from "./session"; |
| 18 | import { useAtomCommand } from "./use-atom-command"; |
| 19 | |
| 20 | const ORCHESTRATION_OPERATE_DENIED_MESSAGE = msg( |
| 21 | "This connection cannot change threads or projects.", |
| 22 | ); |
| 23 | |
| 24 | export function useOrchestrationCommand<W extends { readonly environmentId: EnvironmentId }, A, E>( |
| 25 | command: AtomCommand<W, A, E>, |
| 26 | options?: string | AtomCommandOptions, |
| 27 | ): (value: W) => Promise<AtomCommandResult<A, E | EnvironmentAuthorizationError>> { |
| 28 | const run = useAtomCommand(command, options); |
| 29 | return async (value: W): Promise<AtomCommandResult<A, E | EnvironmentAuthorizationError>> => { |
| 30 | if (!readEnvironmentScope(value.environmentId, AuthOrchestrationOperateScope)) { |
| 31 | return AsyncResult.failure( |
| 32 | Cause.fail( |
| 33 | new EnvironmentAuthorizationError({ |
| 34 | requiredScope: AuthOrchestrationOperateScope, |
| 35 | message: m(ORCHESTRATION_OPERATE_DENIED_MESSAGE), |
| 36 | }), |
| 37 | ), |
| 38 | ); |
| 39 | } |
| 40 | return run(value); |
| 41 | }; |
| 42 | } |