Spaces:
Running
Running
Download web/src/lib/decide.ts from Aynkhn/WorkPilot-AI: direct link, hf CLI and curl.
- Browser
- Download file 1.28 kB
-
https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/web/src/lib/decide.ts
- Command line
-
hf download hf://spaces/Aynkhn/WorkPilot-AI/web/src/lib/decide.ts
-
curl -L -o decide.ts https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/web/src/lib/decide.ts
1.28 kB
| import { api, ApiError } from "@/lib/api"; | |
| import type { ApprovalItem, Decision, DecisionResult } from "@/lib/types"; | |
| export interface DecideInput { | |
| decision: Decision; | |
| note?: string; | |
| edited_args?: Record<string, unknown>; | |
| } | |
| export function needsStepUp(item: Pick<ApprovalItem, "requires_stepup">, d: Decision): boolean { | |
| return item.requires_stepup && (d === "approve" || d === "edit"); | |
| } | |
| /** POST a decision. Step-up is collected first when the item says it needs it, and once more | |
| * if the server still asks for it (e.g. the 5-minute token expired in between). */ | |
| export async function decide( | |
| item: Pick<ApprovalItem, "id" | "requires_stepup">, | |
| input: DecideInput, | |
| requestStepUp: () => Promise<boolean>, | |
| ): Promise<DecisionResult | null> { | |
| const stepup = needsStepUp(item, input.decision); | |
| if (stepup && !(await requestStepUp())) return null; | |
| const send = () => | |
| api<DecisionResult>(`/approvals/${item.id}/decision`, { | |
| method: "POST", | |
| body: input, | |
| stepup, | |
| }); | |
| try { | |
| return await send(); | |
| } catch (e) { | |
| if (e instanceof ApiError && e.status === 409 && /step-up/i.test(e.detail)) { | |
| if (!(await requestStepUp())) return null; | |
| return send(); | |
| } | |
| throw e; | |
| } | |
| } | |