Spaces:
Paused
Paused
File size: 4,435 Bytes
fb4d8fe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | import { randomUUID } from "node:crypto";
import type { GatewayRequestHandlers } from "./types.js";
import { defaultRuntime } from "../../runtime.js";
import { WizardSession } from "../../wizard/session.js";
import {
ErrorCodes,
errorShape,
formatValidationErrors,
validateWizardCancelParams,
validateWizardNextParams,
validateWizardStartParams,
validateWizardStatusParams,
} from "../protocol/index.js";
import { formatForLog } from "../ws-log.js";
export const wizardHandlers: GatewayRequestHandlers = {
"wizard.start": async ({ params, respond, context }) => {
if (!validateWizardStartParams(params)) {
respond(
false,
undefined,
errorShape(
ErrorCodes.INVALID_REQUEST,
`invalid wizard.start params: ${formatValidationErrors(validateWizardStartParams.errors)}`,
),
);
return;
}
const running = context.findRunningWizard();
if (running) {
respond(false, undefined, errorShape(ErrorCodes.UNAVAILABLE, "wizard already running"));
return;
}
const sessionId = randomUUID();
const opts = {
mode: params.mode,
workspace: typeof params.workspace === "string" ? params.workspace : undefined,
};
const session = new WizardSession((prompter) =>
context.wizardRunner(opts, defaultRuntime, prompter),
);
context.wizardSessions.set(sessionId, session);
const result = await session.next();
if (result.done) {
context.purgeWizardSession(sessionId);
}
respond(true, { sessionId, ...result }, undefined);
},
"wizard.next": async ({ params, respond, context }) => {
if (!validateWizardNextParams(params)) {
respond(
false,
undefined,
errorShape(
ErrorCodes.INVALID_REQUEST,
`invalid wizard.next params: ${formatValidationErrors(validateWizardNextParams.errors)}`,
),
);
return;
}
const sessionId = params.sessionId;
const session = context.wizardSessions.get(sessionId);
if (!session) {
respond(false, undefined, errorShape(ErrorCodes.INVALID_REQUEST, "wizard not found"));
return;
}
const answer = params.answer as { stepId?: string; value?: unknown } | undefined;
if (answer) {
if (session.getStatus() !== "running") {
respond(false, undefined, errorShape(ErrorCodes.INVALID_REQUEST, "wizard not running"));
return;
}
try {
await session.answer(String(answer.stepId ?? ""), answer.value);
} catch (err) {
respond(false, undefined, errorShape(ErrorCodes.INVALID_REQUEST, formatForLog(err)));
return;
}
}
const result = await session.next();
if (result.done) {
context.purgeWizardSession(sessionId);
}
respond(true, result, undefined);
},
"wizard.cancel": ({ params, respond, context }) => {
if (!validateWizardCancelParams(params)) {
respond(
false,
undefined,
errorShape(
ErrorCodes.INVALID_REQUEST,
`invalid wizard.cancel params: ${formatValidationErrors(validateWizardCancelParams.errors)}`,
),
);
return;
}
const sessionId = params.sessionId;
const session = context.wizardSessions.get(sessionId);
if (!session) {
respond(false, undefined, errorShape(ErrorCodes.INVALID_REQUEST, "wizard not found"));
return;
}
session.cancel();
const status = {
status: session.getStatus(),
error: session.getError(),
};
context.wizardSessions.delete(sessionId);
respond(true, status, undefined);
},
"wizard.status": ({ params, respond, context }) => {
if (!validateWizardStatusParams(params)) {
respond(
false,
undefined,
errorShape(
ErrorCodes.INVALID_REQUEST,
`invalid wizard.status params: ${formatValidationErrors(validateWizardStatusParams.errors)}`,
),
);
return;
}
const sessionId = params.sessionId;
const session = context.wizardSessions.get(sessionId);
if (!session) {
respond(false, undefined, errorShape(ErrorCodes.INVALID_REQUEST, "wizard not found"));
return;
}
const status = {
status: session.getStatus(),
error: session.getError(),
};
if (status.status !== "running") {
context.wizardSessions.delete(sessionId);
}
respond(true, status, undefined);
},
};
|