@niwa-labs/workflow 0.0.0-stage → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +13 -2
- package/package.json +55 -4
- package/src/actions.ts +408 -0
- package/src/agent.ts +211 -0
- package/src/ask.ts +84 -0
- package/src/bridge.ts +710 -0
- package/src/catalogue.ts +74 -0
- package/src/check.ts +880 -0
- package/src/child.ts +1378 -0
- package/src/cockpit/actions.ts +128 -0
- package/src/cockpit/catalogue-actions.ts +97 -0
- package/src/cockpit/dashboard.tsx +134 -0
- package/src/cockpit/groups.ts +45 -0
- package/src/cockpit/index.tsx +50 -0
- package/src/cockpit/launch.tsx +136 -0
- package/src/cockpit/plan.ts +101 -0
- package/src/cockpit/run-commands.tsx +102 -0
- package/src/cockpit/run-page.tsx +140 -0
- package/src/cockpit/run-summary.ts +16 -0
- package/src/cockpit/runs.tsx +73 -0
- package/src/cockpit/statistics.tsx +176 -0
- package/src/cockpit/track.tsx +159 -0
- package/src/cockpit/workflows.tsx +383 -0
- package/src/describe-cache.ts +122 -0
- package/src/describe.ts +174 -0
- package/src/guide.ts +213 -0
- package/src/index.ts +2813 -0
- package/src/inspect.ts +235 -0
- package/src/installation.ts +90 -0
- package/src/language.ts +217 -0
- package/src/records-entry.ts +63 -0
- package/src/records.ts +574 -0
- package/src/registry.ts +250 -0
- package/src/revise.ts +89 -0
- package/src/runner.ts +339 -0
- package/src/script-types.ts +549 -0
- package/src/stats.ts +460 -0
- package/src/status.ts +512 -0
- package/src/store.ts +259 -0
- package/src/subscriptions.ts +102 -0
- package/src/waiting.ts +61 -0
- package/test/cockpit-actions.test.ts +20 -0
- package/test/cockpit-groups.test.ts +42 -0
- package/test/cockpit-plan.test.ts +118 -0
- package/test/fixtures/branches-host.ts +58 -0
- package/test/fixtures/branches-script.js +23 -0
- package/test/fixtures/bridge-host.ts +23 -0
- package/test/fixtures/bridge-keys-script.js +15 -0
- package/test/fixtures/bridge-wait-script.js +8 -0
- package/test/fixtures/cancel-host.ts +58 -0
- package/test/fixtures/cancel-script.js +12 -0
- package/test/fixtures/crash-host.ts +61 -0
- package/test/fixtures/crash-script.js +14 -0
- package/test/fixtures/ping-records.ts +20 -0
- package/test/fixtures/revision-host.ts +57 -0
- package/test/fixtures/revision-script.js +11 -0
- package/test/fixtures/running-host.ts +61 -0
- package/test/fixtures/running-script.js +12 -0
- package/test/fixtures/with-data/flow.ts +2 -0
- package/test/fixtures/with-data/table.json +1 -0
- package/test/git-repo.ts +45 -0
- package/test/git-worktree.test.ts +413 -0
- package/test/question-format.test.ts +279 -0
- package/test/workflow-agent-caps.test.ts +91 -0
- package/test/workflow-agent-lifetime.test.ts +93 -0
- package/test/workflow-agent-options.test.ts +216 -0
- package/test/workflow-agent-schema.test.ts +386 -0
- package/test/workflow-agent-typings.test.ts +241 -0
- package/test/workflow-ask-resume.test.ts +145 -0
- package/test/workflow-branch-keys.test.ts +133 -0
- package/test/workflow-bridge-inproc.test.ts +230 -0
- package/test/workflow-bridge.test.ts +141 -0
- package/test/workflow-builtin-candidate.test.ts +230 -0
- package/test/workflow-builtin.test.ts +197 -0
- package/test/workflow-cancel-agent.test.ts +144 -0
- package/test/workflow-cancel.test.ts +166 -0
- package/test/workflow-chain.test.ts +84 -0
- package/test/workflow-check-options.test.ts +144 -0
- package/test/workflow-check-uses.test.ts +148 -0
- package/test/workflow-check.test.ts +282 -0
- package/test/workflow-contract.test.ts +295 -0
- package/test/workflow-crash-resume.test.ts +141 -0
- package/test/workflow-deadline.test.ts +102 -0
- package/test/workflow-describe-cache.test.ts +240 -0
- package/test/workflow-directory.test.ts +73 -0
- package/test/workflow-emits.test.ts +261 -0
- package/test/workflow-group-keys.test.ts +72 -0
- package/test/workflow-harnesses.test.ts +262 -0
- package/test/workflow-inspect.test.ts +152 -0
- package/test/workflow-language-types.test.ts +148 -0
- package/test/workflow-manifest.test.ts +93 -0
- package/test/workflow-manual-pause.test.ts +116 -0
- package/test/workflow-owners.test.ts +154 -0
- package/test/workflow-pause.test.ts +200 -0
- package/test/workflow-phases.test.ts +445 -0
- package/test/workflow-plugin-entries.test.ts +59 -0
- package/test/workflow-record-refs.test.ts +310 -0
- package/test/workflow-replay.test.ts +206 -0
- package/test/workflow-resume-once.test.ts +188 -0
- package/test/workflow-revision.test.ts +216 -0
- package/test/workflow-scope.test.ts +505 -0
- package/test/workflow-script-defaults.test.ts +119 -0
- package/test/workflow-script-error.test.ts +228 -0
- package/test/workflow-script-libraries.test.ts +88 -0
- package/test/workflow-script-packages.test.ts +119 -0
- package/test/workflow-stdout.test.ts +89 -0
- package/test/workflow-stop.test.ts +299 -0
- package/test/workflow-store.test.ts +220 -0
- package/test/workflow-visible.test.ts +136 -0
- package/test/workflow-wait-version.test.ts +54 -0
- package/test/workflow-wait.test.ts +224 -0
- package/test/workflow-waiting-agent.test.ts +64 -0
- package/tsconfig.json +15 -0
package/src/agent.ts
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The agent action of a harness: what an `ask()` call of a script goes to when it names that
|
|
3
|
+
* harness.
|
|
4
|
+
*
|
|
5
|
+
* It starts a session and asks it for the answer: one call is one session and one answer. The
|
|
6
|
+
* sessions module is reached through the typed references of its actions, the way the tasks module
|
|
7
|
+
* reaches it, so the workflow module names no harness kind and its calls are checked against the
|
|
8
|
+
* schemas the sessions module really serves. The one thing it takes from the sessions module is
|
|
9
|
+
* the name of the action that answers a question: the session of an agent call must see it, or a
|
|
10
|
+
* harness that cannot answer by itself never answers at all.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type { Action, ActionContext } from "@niwa-labs/core";
|
|
14
|
+
import { DESCRIPTION_MAX, DESCRIPTION_MIN, EntityDescription, JsonSchemaValue, action } from "@niwa-labs/core";
|
|
15
|
+
import { WorktreeOption, gitWorktreeGet, gitWorktreeRemove } from "@niwa-labs/git/records";
|
|
16
|
+
import type { WorktreeOptionGiven } from "@niwa-labs/git/records";
|
|
17
|
+
import { PromptSchema, SessionModeSchema, ToolsModeSchema, blocksOf, withBlocks } from "@niwa-labs/harness";
|
|
18
|
+
import type { OptionBlocks } from "@niwa-labs/harness";
|
|
19
|
+
import { BoxAccessSchema } from "@niwa-labs/sandbox";
|
|
20
|
+
import { SUBMIT_ANSWER_ACTION, harnessGet, sessionAsk, sessionEnd, sessionStart } from "@niwa-labs/sessions/records";
|
|
21
|
+
import { z } from "zod";
|
|
22
|
+
|
|
23
|
+
import { agentActionName } from "./language.ts";
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The niwa actions the session of an agent call sees.
|
|
27
|
+
*
|
|
28
|
+
* The action that answers a question is among them: a harness that cannot answer by itself
|
|
29
|
+
* answers through it, and a session that does not see it can never answer at all.
|
|
30
|
+
*/
|
|
31
|
+
const VISIBLE_ACTIONS: readonly string[] = [SUBMIT_ANSWER_ACTION];
|
|
32
|
+
|
|
33
|
+
/** What the adapter of a harness knows of what a session may be started with: the models and the efforts it lists. */
|
|
34
|
+
export interface HarnessLaunch {
|
|
35
|
+
readonly models?: readonly string[];
|
|
36
|
+
readonly efforts?: readonly string[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** A text that is one of the listed, or any non-empty text when the adapter lists none. */
|
|
40
|
+
function oneOf(listed: readonly string[] | undefined): z.ZodType<string, string> {
|
|
41
|
+
const [first, ...rest] = listed ?? [];
|
|
42
|
+
return first === undefined ? z.string().min(1) : z.enum([first, ...rest]);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The fields of an agent call. The model and the effort are the listed ones where the adapter lists them. Which block
|
|
47
|
+
* the harness takes is the sessions module's to say, so it is checked when the call runs (`requireOwnBlock`), and what
|
|
48
|
+
* is in it is checked by the schema of the harness's kind when `session.start` takes it.
|
|
49
|
+
*/
|
|
50
|
+
const agentFields = (launch: HarnessLaunch) =>
|
|
51
|
+
z.object({
|
|
52
|
+
prompt: z.union([z.string(), PromptSchema]).optional(),
|
|
53
|
+
harness: z.string().optional(),
|
|
54
|
+
label: z.string().optional(),
|
|
55
|
+
/** The actions (names or masks) the session sees besides the one that answers. */
|
|
56
|
+
visible: z.array(z.string()).optional(),
|
|
57
|
+
tools: ToolsModeSchema.optional(),
|
|
58
|
+
/** The model of the session, for its whole life. */
|
|
59
|
+
model: oneOf(launch.models).optional(),
|
|
60
|
+
/** The effort of the session, for its whole life. */
|
|
61
|
+
effort: oneOf(launch.efforts).optional(),
|
|
62
|
+
/** The working mode of the session, for its whole life. */
|
|
63
|
+
mode: SessionModeSchema.optional(),
|
|
64
|
+
/** The directory the session starts in. */
|
|
65
|
+
cwd: z.string().min(1).optional(),
|
|
66
|
+
/** Run the session in a new git worktree; removed when the call ends with it clean and no new commit. */
|
|
67
|
+
worktree: WorktreeOption.optional(),
|
|
68
|
+
/** The JSON Schema the answer must pass. Absent: the answer is text. */
|
|
69
|
+
schema: JsonSchemaValue.optional(),
|
|
70
|
+
/** How long, in milliseconds, a turn of the session may go with no progress before it counts as stuck. */
|
|
71
|
+
stuckAfter: z.number().int().positive().optional(),
|
|
72
|
+
/** What the session's box has besides the host's default (`allow`) or instead of it (`only`). */
|
|
73
|
+
box: BoxAccessSchema.optional(),
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
/** Every key of an agent call that is not one of its fields is an options block, an object named after the kind. */
|
|
77
|
+
const agentInput = (harness: string, launch: HarnessLaunch) =>
|
|
78
|
+
withBlocks(agentFields(launch), (key) => `the agent call of the harness "${harness}" has no field "${key}"`);
|
|
79
|
+
|
|
80
|
+
/** The names of the fields of an agent call. */
|
|
81
|
+
const AGENT_FIELDS: readonly string[] = Object.keys(agentFields({}).shape);
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Refuse, before a session is started, a block that is not the one the sessions module binds the harness to
|
|
85
|
+
* (`harness.get`): the kind of a harness is the sessions module's to know.
|
|
86
|
+
*/
|
|
87
|
+
async function requireOwnBlock(harness: string, given: object, ctx: ActionContext): Promise<void> {
|
|
88
|
+
const blocks = Object.keys(blocksOfCall(harness, given));
|
|
89
|
+
if (blocks.length === 0) return;
|
|
90
|
+
const { block: own } = await ctx.step(harnessGet, { name: harness });
|
|
91
|
+
const foreign = blocks.filter((block) => block !== own);
|
|
92
|
+
if (foreign.length === 0) return;
|
|
93
|
+
throw new Error(
|
|
94
|
+
own === undefined
|
|
95
|
+
? `the harness "${harness}" takes no options (asked for "${foreign.join('", "')}")`
|
|
96
|
+
: `the harness "${harness}" takes the "${own}" options block only (asked for "${foreign.join('", "')}")`,
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
/** The blocks of an agent call; a key that is neither a field nor a block is refused, naming the harness. */
|
|
100
|
+
function blocksOfCall(harness: string, input: object): OptionBlocks {
|
|
101
|
+
try {
|
|
102
|
+
return blocksOf(input, AGENT_FIELDS);
|
|
103
|
+
} catch (error) {
|
|
104
|
+
throw new Error(`the agent call of the harness "${harness}": ${error instanceof Error ? error.message : String(error)}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
type AgentInput = z.output<ReturnType<typeof agentInput>>;
|
|
109
|
+
type AgentInputGiven = z.input<ReturnType<typeof agentInput>>;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* What an agent call answers: `{ answer }` with the text the session gave, or, when the call
|
|
113
|
+
* carries a schema, the value that passed it.
|
|
114
|
+
*/
|
|
115
|
+
const AgentOutput = z.unknown();
|
|
116
|
+
|
|
117
|
+
/** The worktree an agent call leaves behind: where the work is, and whether it changed anything. */
|
|
118
|
+
export const AgentWorktree = z.object({
|
|
119
|
+
id: z.string().min(1),
|
|
120
|
+
path: z.string().min(1),
|
|
121
|
+
branch: z.string().min(1),
|
|
122
|
+
/** Uncommitted changes, or commits its base does not have. */
|
|
123
|
+
changed: z.boolean(),
|
|
124
|
+
});
|
|
125
|
+
export type AgentWorktree = z.infer<typeof AgentWorktree>;
|
|
126
|
+
|
|
127
|
+
/** Whether a JSON Schema describes an object, so a field can ride along with the answer. */
|
|
128
|
+
function isObjectSchema(schema: JsonSchemaValue): boolean {
|
|
129
|
+
return typeof schema === "object" && schema !== null && (schema as { type?: unknown }).type === "object";
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** The worktree an agent call asks session.start for: the description is the call's label, else the first line of its prompt. */
|
|
133
|
+
function worktreeAsked(option: WorktreeOption, label: string | undefined, prompt: string): WorktreeOptionGiven {
|
|
134
|
+
const asked = option === true ? {} : option;
|
|
135
|
+
if (asked.description !== undefined) return asked;
|
|
136
|
+
const line = (label ?? prompt).split("\n").map((part) => part.trim()).find((part) => part.length > 0) ?? "";
|
|
137
|
+
const text = line.length >= DESCRIPTION_MIN ? line.slice(0, DESCRIPTION_MAX).trim() : "an agent call";
|
|
138
|
+
return { ...asked, description: EntityDescription.parse(text.length >= DESCRIPTION_MIN ? text : "an agent call") };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** The schema the answer of a session must pass when the call names none. */
|
|
142
|
+
const ANSWER_SCHEMA: JsonSchemaValue = {
|
|
143
|
+
type: "object",
|
|
144
|
+
properties: { answer: { type: "string" } },
|
|
145
|
+
required: ["answer"],
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* The action `session.start.<harness>`: it starts a session of the harness and asks it.
|
|
150
|
+
*
|
|
151
|
+
* The session is started without a prompt: the question carries the work, so the session answers
|
|
152
|
+
* once, and the answer is a value that passed the schema: the schema of the call, else `{ answer }`.
|
|
153
|
+
* An answer that does not pass it is refused by `session.ask` (see there), so none enters the journal.
|
|
154
|
+
* An options block other than the one the sessions module binds the harness to fails the call before a session starts.
|
|
155
|
+
*/
|
|
156
|
+
export function sessionAgent(harness: string, launch: HarnessLaunch = {}): Action<AgentInput, unknown, AgentInputGiven> {
|
|
157
|
+
return action(String(agentActionName(harness)), {
|
|
158
|
+
description: `Starts a session of the harness "${harness}" and, when given a prompt, asks it and closes it.`,
|
|
159
|
+
input: agentInput(harness, launch),
|
|
160
|
+
output: AgentOutput,
|
|
161
|
+
// A session is started and asked: a call that was cut off may have done both.
|
|
162
|
+
recovery: "do-not-repeat",
|
|
163
|
+
effect: "write",
|
|
164
|
+
world: "local",
|
|
165
|
+
run: async (input, ctx) => {
|
|
166
|
+
await requireOwnBlock(harness, input, ctx);
|
|
167
|
+
const answerSchema = input.schema ?? ANSWER_SCHEMA;
|
|
168
|
+
if (input.worktree !== undefined && !isObjectSchema(answerSchema)) {
|
|
169
|
+
throw new Error("an agent call with a worktree answers an object, so the worktree can ride along: give it a schema of an object");
|
|
170
|
+
}
|
|
171
|
+
// The session is the run's: started as the run, it is held to the run's grant (its box, its tools),
|
|
172
|
+
// as every call of the script is.
|
|
173
|
+
const run = ctx.caller.kind === "client" ? ctx.caller.principal : undefined;
|
|
174
|
+
const started = await ctx.step(sessionStart, {
|
|
175
|
+
harness,
|
|
176
|
+
prompt: "",
|
|
177
|
+
...(input.model === undefined ? {} : { model: input.model }),
|
|
178
|
+
...(input.effort === undefined ? {} : { effort: input.effort }),
|
|
179
|
+
...(input.tools === undefined ? {} : { tools: input.tools }),
|
|
180
|
+
...(input.mode === undefined ? {} : { mode: input.mode }),
|
|
181
|
+
...(input.cwd === undefined ? {} : { cwd: input.cwd }),
|
|
182
|
+
...(input.worktree === undefined ? {} : { worktree: worktreeAsked(input.worktree, input.label, typeof input.prompt === "string" ? input.prompt : input.prompt?.sections[0]?.text ?? "workflow session") }),
|
|
183
|
+
...(input.stuckAfter === undefined ? {} : { stuckAfter: input.stuckAfter }),
|
|
184
|
+
...(input.box === undefined ? {} : { box: input.box }),
|
|
185
|
+
...blocksOfCall(harness, input),
|
|
186
|
+
visible: { actions: [...new Set([...VISIBLE_ACTIONS, ...(input.visible ?? [])])], disabledTools: [] },
|
|
187
|
+
}, run === undefined ? {} : { as: run });
|
|
188
|
+
if (input.prompt === undefined) return started;
|
|
189
|
+
const { session, worktree } = started;
|
|
190
|
+
const as = run === undefined ? {} : { as: run };
|
|
191
|
+
// A parked turn will re-enter this step. Keep its session until it has an answer.
|
|
192
|
+
const { value } = await ctx.step(sessionAsk, {
|
|
193
|
+
session,
|
|
194
|
+
prompt: input.prompt,
|
|
195
|
+
schema: answerSchema,
|
|
196
|
+
}, as);
|
|
197
|
+
await ctx.step(sessionEnd, { session, reason: "workflow question finished" }, as);
|
|
198
|
+
if (worktree === undefined) return value;
|
|
199
|
+
// The worktree goes when the work left nothing in it; otherwise the answer says where the work is.
|
|
200
|
+
const now = await ctx.step(gitWorktreeGet, { id: worktree.id }, as);
|
|
201
|
+
const changed = now.dirty || now.aheadOfBase > 0;
|
|
202
|
+
if (!changed && now.sessions.length === 0) {
|
|
203
|
+
// Recheck at removal: concurrent work may have made this worktree dirty.
|
|
204
|
+
await ctx.step(gitWorktreeRemove, { id: worktree.id }, as);
|
|
205
|
+
return value;
|
|
206
|
+
}
|
|
207
|
+
const kept: AgentWorktree = { ...worktree, changed };
|
|
208
|
+
return { ...(value as object), worktree: kept };
|
|
209
|
+
},
|
|
210
|
+
});
|
|
211
|
+
}
|
package/src/ask.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `ask.<kind>` of a script: the question a script puts to the owner, built from
|
|
3
|
+
* what the call says, and the typed value the owner's answer comes back as.
|
|
4
|
+
*
|
|
5
|
+
* The child process builds the question, the host asks it as a request and turns the answer into
|
|
6
|
+
* the value; both sides use this one file, so they cannot disagree about a kind.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { OptionIdSchema, QuestionSchema, confirmOptions, isYes } from "@niwa-labs/core";
|
|
10
|
+
import type { Answer, Question, QuestionOption } from "@niwa-labs/core";
|
|
11
|
+
|
|
12
|
+
/** An option of `ask.choice` and `ask.multi`: the id alone, or the id with how it is shown. */
|
|
13
|
+
export type AskOption = string | { readonly id: string; readonly label?: string; readonly description?: string };
|
|
14
|
+
|
|
15
|
+
/** The options as a question holds them. Their ids are what the script gets back, so they are told apart. */
|
|
16
|
+
function optionsOf(options: readonly AskOption[]): QuestionOption[] {
|
|
17
|
+
if (options.length === 0) throw new Error("a question with no options cannot be answered");
|
|
18
|
+
const seen = new Set<string>();
|
|
19
|
+
return options.map((option) => {
|
|
20
|
+
const { id, label, description } = typeof option === "string" ? { id: option } : option;
|
|
21
|
+
if (seen.has(id)) throw new Error(`the option "${id}" is given twice`);
|
|
22
|
+
seen.add(id);
|
|
23
|
+
return {
|
|
24
|
+
id: OptionIdSchema.parse(id),
|
|
25
|
+
label: label ?? id,
|
|
26
|
+
...(description === undefined ? {} : { description }),
|
|
27
|
+
flavor: "plain" as const,
|
|
28
|
+
};
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** `ask.choice(text, options)`: one of the options. */
|
|
33
|
+
export const choiceQuestion = (text: string, options: readonly AskOption[]): Question =>
|
|
34
|
+
QuestionSchema.parse({ kind: "choice", title: text, options: optionsOf(options) });
|
|
35
|
+
|
|
36
|
+
/** `ask.multi(text, options)`: any number of the options. */
|
|
37
|
+
export const multiQuestion = (text: string, options: readonly AskOption[]): Question =>
|
|
38
|
+
QuestionSchema.parse({ kind: "multi", title: text, options: optionsOf(options) });
|
|
39
|
+
|
|
40
|
+
/** `ask.confirm(text)`: yes or no. */
|
|
41
|
+
export const confirmQuestion = (text: string): Question =>
|
|
42
|
+
QuestionSchema.parse({ kind: "confirm", title: text, options: confirmOptions() });
|
|
43
|
+
|
|
44
|
+
/** `ask.text(text)`: a free text. */
|
|
45
|
+
export const textQuestion = (text: string): Question => QuestionSchema.parse({ kind: "text", title: text });
|
|
46
|
+
|
|
47
|
+
/** `ask.form(text, schema)`: a value that passes a JSON Schema. */
|
|
48
|
+
export const formQuestion = (text: string, schema: Readonly<Record<string, unknown>>): Question =>
|
|
49
|
+
QuestionSchema.parse({ kind: "form", title: text, schema });
|
|
50
|
+
|
|
51
|
+
/** The value a script gets for the answer of the owner to a question of its own: the id, the ids, a boolean, a text, the form. */
|
|
52
|
+
export function valueOf(question: Question, answer: Answer): unknown {
|
|
53
|
+
switch (question.kind) {
|
|
54
|
+
case "confirm":
|
|
55
|
+
return isYes(answer);
|
|
56
|
+
case "choice":
|
|
57
|
+
return "ids" in answer ? answer.ids[0] : undefined;
|
|
58
|
+
case "multi":
|
|
59
|
+
return "ids" in answer ? answer.ids : [];
|
|
60
|
+
case "text":
|
|
61
|
+
return "text" in answer ? answer.text : undefined;
|
|
62
|
+
case "form":
|
|
63
|
+
return "form" in answer ? answer.form : undefined;
|
|
64
|
+
case "permission":
|
|
65
|
+
throw new Error("a script does not ask for a permission");
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** What a question comes to when nobody answers it, for a run that only plans: the neutral value of its kind. */
|
|
70
|
+
export function neutralValue(question: Question): unknown {
|
|
71
|
+
switch (question.kind) {
|
|
72
|
+
case "confirm":
|
|
73
|
+
return false;
|
|
74
|
+
case "choice":
|
|
75
|
+
return question.options[0]?.id;
|
|
76
|
+
case "multi":
|
|
77
|
+
return [];
|
|
78
|
+
case "text":
|
|
79
|
+
return "";
|
|
80
|
+
case "form":
|
|
81
|
+
case "permission":
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
}
|