| import { HttpError } from "@openhands/typescript-client"; |
| import { getActiveBackend } from "../backend-registry/active-store"; |
| import type { Backend } from "../backend-registry/types"; |
| import { callCloudProxy } from "./proxy"; |
| import type { |
| CloudApiKeyMetadata, |
| CloudOrganization, |
| CloudOrganizationMember, |
| CloudOrganizationsResponse, |
| } from "./types"; |
|
|
| interface OrganizationsResult { |
| items: CloudOrganization[]; |
| currentOrgId: string | null; |
| } |
|
|
| function normalizeResult( |
| data: CloudOrganizationsResponse | undefined | null, |
| ): OrganizationsResult { |
| return { |
| items: data?.items ?? [], |
| currentOrgId: data?.current_org_id ?? null, |
| }; |
| } |
|
|
| function resolveBackend(backend?: Backend): Backend { |
| if (backend) return backend; |
| const active = getActiveBackend().backend; |
| if (active.kind !== "cloud") { |
| throw new Error( |
| "Cloud organization calls require a cloud backend. Active backend is local.", |
| ); |
| } |
| return active; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export async function getCloudOrganizations( |
| backend?: Backend, |
| ): Promise<OrganizationsResult> { |
| const target = resolveBackend(backend); |
| const data = await callCloudProxy<CloudOrganizationsResponse>({ |
| backend: target, |
| method: "GET", |
| path: "/api/organizations", |
| }); |
| return normalizeResult(data); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export async function getCurrentCloudApiKey( |
| backend?: Backend, |
| ): Promise<{ orgId: string | null; isLegacyKey: boolean }> { |
| const target = resolveBackend(backend); |
| try { |
| const data = await callCloudProxy<CloudApiKeyMetadata>({ |
| backend: target, |
| method: "GET", |
| path: "/api/keys/current", |
| }); |
| return { orgId: data?.org_id ?? null, isLegacyKey: false }; |
| } catch (e) { |
| if (e instanceof HttpError && e.status === 400) { |
| return { orgId: null, isLegacyKey: true }; |
| } |
| throw e; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export async function getCloudOrganizationMe( |
| orgId: string, |
| backend?: Backend, |
| ): Promise<{ |
| orgId: string; |
| userId: string; |
| role: string | null; |
| permissions?: string[] | null; |
| }> { |
| const target = resolveBackend(backend); |
| const data = await callCloudProxy<{ |
| org_id: string; |
| user_id: string; |
| role?: string; |
| permissions?: string[]; |
| }>({ |
| backend: target, |
| method: "GET", |
| path: `/api/organizations/${encodeURIComponent(orgId)}/me`, |
| }); |
| return { |
| orgId: data?.org_id ?? orgId, |
| userId: data?.user_id ?? "", |
| role: data?.role ?? null, |
| |
| |
| permissions: Array.isArray(data?.permissions) ? data.permissions : null, |
| }; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export async function getCloudOrganizationMember( |
| orgId: string, |
| userId: string, |
| backend?: Backend, |
| ): Promise<CloudOrganizationMember> { |
| const target = resolveBackend(backend); |
| return callCloudProxy<CloudOrganizationMember>({ |
| backend: target, |
| method: "GET", |
| path: `/api/organizations/${encodeURIComponent(orgId)}/members/${encodeURIComponent(userId)}`, |
| }); |
| } |
|
|