import type { CorpConfig } from "../../core/config/types.js"; import { assertHoseAssistTenantMatches, exchangeHoseAssistLink } from "../../core/auth/hose.js"; import { createSealClientForUrl } from "../../core/http/factory.js"; import { createDelegation, getMe } from "../../domains/seal/api.js"; import { runTool } from "../helpers.js"; import { stringOption } from "../parser.js"; import { outputOptions, printJson } from "../printer.js"; import type { ParsedArgs } from "../types.js"; import type { createSealClient } from "../../core/http/factory.js"; export async function handleDelegationFromHoseLink(link: string, note?: string) { const session = await exchangeHoseAssistLink(link); const client = createSealClientForUrl(session.sealUrl, session.token); const me = await getMe(client); assertHoseAssistTenantMatches(session.corpId, me.tenant as unknown as Record); const delegation = await createDelegation(client, { note }); printJson({ tenant: { id: me.tenant.id, name: me.tenant.name, tenantSlug: me.tenant.tenantSlug, corpId: session.corpId }, user: { id: me.user.id, name: me.user.name, providerUserId: me.user.providerUserId, isAdmin: me.user.isAdmin }, delegation, expiresIn: session.expiresIn }); } export async function handleDelegationCommand( action: string | undefined, maybeName: string | undefined, client: Awaited>, corp: CorpConfig, args: ParsedArgs ) { if (action === "list") { await runTool("seal_delegation_list", client, corp, {}, outputOptions(args.options)); return; } if (action === "create") { const note = stringOption(args.options.note) ?? (maybeName ? [maybeName, ...args.command.slice(2)].join(" ") : undefined); await runTool("seal_delegation_create", client, corp, { note }, outputOptions(args.options)); return; } if (action === "revoke") { const tokenId = maybeName ?? stringOption(args.options.tokenId); if (!tokenId) throw new Error("Usage: seal-use delegation revoke "); await runTool("seal_delegation_revoke", client, corp, { tokenId }, outputOptions(args.options)); return; } throw new Error(`Unknown delegation command: ${action ?? ""}. Run 'seal-use help' for available commands.`); }