setup-agent 0.0.1

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.
@@ -0,0 +1,479 @@
1
+ export namespace Checkout {
2
+ export type StepStatus =
3
+ | "pending"
4
+ | "active"
5
+ | "done"
6
+ | "skipped"
7
+ | "blocked";
8
+
9
+ /** A unit of install work the agent declared; the catalog seeds none. */
10
+ export type Step = {
11
+ id: string;
12
+ title: string;
13
+ detail?: string;
14
+ status: StepStatus;
15
+ note?: string;
16
+ /** The product this step belongs to, when the agent said. */
17
+ product?: string;
18
+ createdAt: number;
19
+ };
20
+
21
+ export type Product = {
22
+ slug: string;
23
+ name: string;
24
+ /** The install reference the agent fetches while planning. */
25
+ guide?: string;
26
+ };
27
+
28
+ export type InputStatus = "open" | "answered" | "dismissed";
29
+
30
+ /**
31
+ * product: the agent proposes adding a product to this checkout, such as one
32
+ * another product depends on. The browser owns the catalog, so it answers
33
+ * with `checkout/add-product`; dismissing the input declines.
34
+ */
35
+ export type InputKind = "text" | "choice" | "model" | "product";
36
+
37
+ export type ChoiceVariant = { id: string; label: string };
38
+
39
+ export type ChoiceOption = {
40
+ id: string;
41
+ label: string;
42
+ description?: string;
43
+ /** A name from OPTION_ICONS, or the key of a brand mark on a preset option; unknown keys render without one. */
44
+ icon?: string;
45
+ /** A second pick under this option, such as the language of a framework. */
46
+ variants?: ChoiceVariant[];
47
+ };
48
+
49
+ /** A short nudge for users who need help choosing, with a longer guide to link to. */
50
+ export type InputHelp = { summary: string; href?: string };
51
+
52
+ /**
53
+ * The answer to a model input, stored as JSON text. The API key itself is
54
+ * never part of it: the browser deposits it with the checkout and the
55
+ * agent's `env` command writes it straight to a file.
56
+ */
57
+ export type ModelAnswer = {
58
+ provider: string;
59
+ model: string;
60
+ reasoningEffort?: "low" | "medium" | "high";
61
+ };
62
+
63
+ /**
64
+ * A choice answer is an option id, `<option>:<variant>` when the option has
65
+ * variants, or the user's own text when none of the options fit; a multiple
66
+ * choice answers a JSON array of those. A model answer is a JSON-encoded
67
+ * ModelAnswer whose provider is one of the options.
68
+ */
69
+ export type Input = {
70
+ phase: Status;
71
+ id: string;
72
+ kind: InputKind;
73
+ /** The standard input this was built from, when it was. */
74
+ preset?: string;
75
+ prompt: string;
76
+ placeholder?: string;
77
+ options?: ChoiceOption[];
78
+ /** The user may pick any number of options; the answer is a JSON array. */
79
+ multiple?: true;
80
+ /** The slug a product input proposes. */
81
+ product?: string;
82
+ default?: string;
83
+ help?: InputHelp;
84
+ optional: boolean;
85
+ status: InputStatus;
86
+ answer?: string;
87
+ /** A remark the user attached to the answer. */
88
+ note?: string;
89
+ stepId?: string;
90
+ createdAt: number;
91
+ answeredAt?: number;
92
+ };
93
+
94
+ export type LogEntry = {
95
+ phase: Status;
96
+ id: string;
97
+ role: "agent" | "user";
98
+ acknowledgedAt?: number;
99
+ at: number;
100
+ text: string;
101
+ /** The step that was active when the line was posted; none while planning. */
102
+ stepId?: string;
103
+ };
104
+
105
+ export type PlanStatus = "proposed" | "approved" | "changes-requested";
106
+
107
+ /** One revision of the agent's plan. Markdown the browser renders. */
108
+ export type Plan = {
109
+ revision: number;
110
+ markdown: string;
111
+ status: PlanStatus;
112
+ submittedAt: number;
113
+ decidedAt?: number;
114
+ /** What the user asked to change, when they did. */
115
+ feedback?: string;
116
+ };
117
+
118
+ /**
119
+ * waiting: no agent has joined. planning: the agent investigates and
120
+ * proposes a plan. installing: the plan is approved and steps run.
121
+ */
122
+ export type Status =
123
+ | "waiting"
124
+ | "planning"
125
+ | "installing"
126
+ | "done"
127
+ | "cancelled";
128
+
129
+ /**
130
+ * The agent's proposal to close the checkout. Only the user closes it, and
131
+ * they may keep messaging the agent instead; a later `done` renews it.
132
+ * `preview` is the loopback URL of a dev server the agent left running for
133
+ * the user to try before they close.
134
+ */
135
+ export type Completion = { proposedAt: number; preview?: string };
136
+
137
+ export type State = {
138
+ version: 2;
139
+ /** The id the browser gave the current checkout; a create with another id replaces it on the same link. */
140
+ id: string | null;
141
+ status: Status;
142
+ completion?: Completion;
143
+ createdAt: number | null;
144
+ products: Product[];
145
+ instructions: string;
146
+ agent: {
147
+ lastSeenAt: number | null;
148
+ /** False once the agent's stream said goodbye; heartbeats set it again. */
149
+ connected: boolean;
150
+ cwd: string | null;
151
+ /** The coding agent that ran the command, as the CLI detected it from its environment. */
152
+ kind: string | null;
153
+ /** When the command was first run against this checkout, before the agent joined the stream. */
154
+ introducedAt: number | null;
155
+ };
156
+ /** Every revision in order; the last one is current. */
157
+ plans: Plan[];
158
+ steps: Step[];
159
+ inputs: Input[];
160
+ log: LogEntry[];
161
+ };
162
+
163
+ export type ProductSeed = { slug: string; name: string; guide?: string };
164
+
165
+ export type InputSeed = {
166
+ kind?: InputKind;
167
+ preset?: string;
168
+ prompt: string;
169
+ placeholder?: string;
170
+ options?: ChoiceOption[];
171
+ multiple?: true;
172
+ product?: string;
173
+ default?: string;
174
+ help?: InputHelp;
175
+ optional?: boolean;
176
+ };
177
+
178
+ export type PlanDecision =
179
+ | { decision: "approve" }
180
+ | { decision: "revise"; feedback: string };
181
+
182
+ export type Commands = {
183
+ "checkout/create": (params: {
184
+ id: string;
185
+ products: ProductSeed[];
186
+ instructions?: string;
187
+ }) => void;
188
+ "checkout/begin-plan": () => void;
189
+ "checkout/answer": (params: {
190
+ inputId: string;
191
+ answer: string;
192
+ note?: string;
193
+ }) => void;
194
+ "checkout/add-product": (params: {
195
+ inputId: string;
196
+ product: ProductSeed;
197
+ }) => void;
198
+ "checkout/message": (params: { text: string }) => void;
199
+ "agent/ack": (params: { messageId: string }) => void;
200
+ "checkout/dismiss": (params: { inputId: string }) => void;
201
+ "checkout/plan": (params: PlanDecision) => void;
202
+ "checkout/cancel": () => void;
203
+ "checkout/finish": () => void;
204
+ "agent/intro": (params: { kind?: string }) => void;
205
+ "agent/hello": (params: { cwd?: string; kind?: string }) => void;
206
+ "agent/heartbeat": () => void;
207
+ "agent/bye": () => void;
208
+ "agent/plan": (params: { markdown: string }) => { revision: number };
209
+ "agent/add-step": (params: {
210
+ title: string;
211
+ detail?: string;
212
+ product?: string;
213
+ /** Start the step in the same call. */
214
+ active?: boolean;
215
+ }) => { stepId: string };
216
+ "agent/step": (params: {
217
+ stepId: string;
218
+ status: StepStatus;
219
+ note?: string;
220
+ }) => void;
221
+ "agent/ask": (params: InputSeed & { stepId?: string }) => {
222
+ inputId: string;
223
+ };
224
+ "agent/log": (params: { text: string }) => void;
225
+ "agent/done": (params?: { summary?: string; preview?: string }) => void;
226
+ };
227
+
228
+ export type RejectionReason =
229
+ | "already-created"
230
+ | "not-created"
231
+ | "closed"
232
+ | "planning-not-started"
233
+ | "plan-required"
234
+ | "no-plan"
235
+ | "plan-decided"
236
+ | "finish-not-proposed"
237
+ | "feedback-required"
238
+ | "empty-plan"
239
+ | "unknown-step"
240
+ | "unknown-product"
241
+ | "unknown-input"
242
+ | "input-closed"
243
+ | "invalid-input"
244
+ | "invalid-answer"
245
+ | "invalid-preview"
246
+ | "empty-message"
247
+ | "unknown-message";
248
+ }
249
+
250
+ export const AGENT_PRESENCE_MS = 15_000;
251
+ export const AGENT_HEARTBEAT_MS = 5_000;
252
+
253
+ export const initialCheckoutState = (): Checkout.State => ({
254
+ version: 2,
255
+ id: null,
256
+ status: "waiting",
257
+ createdAt: null,
258
+ products: [],
259
+ instructions: "",
260
+ agent: {
261
+ lastSeenAt: null,
262
+ connected: false,
263
+ cwd: null,
264
+ kind: null,
265
+ introducedAt: null,
266
+ },
267
+ plans: [],
268
+ steps: [],
269
+ inputs: [],
270
+ log: [],
271
+ });
272
+
273
+ export const isAgentPresent = (state: Checkout.State, now = Date.now()) =>
274
+ state.agent.connected &&
275
+ state.agent.lastSeenAt !== null &&
276
+ now - state.agent.lastSeenAt < AGENT_PRESENCE_MS;
277
+
278
+ export const isClosed = (state: Checkout.State) =>
279
+ state.status === "done" || state.status === "cancelled";
280
+
281
+ /** True while the agent's proposal to close waits for the user. */
282
+ export const finishProposed = (state: Checkout.State) =>
283
+ !isClosed(state) && state.completion !== undefined;
284
+
285
+ /** True when the user messaged the agent after it last proposed to close. */
286
+ export const followedUpSinceProposal = (state: Checkout.State) => {
287
+ const proposedAt = state.completion?.proposedAt;
288
+ return (
289
+ proposedAt !== undefined &&
290
+ state.log.some((entry) => entry.role === "user" && entry.at > proposedAt)
291
+ );
292
+ };
293
+
294
+ const LOOPBACK_HOSTS = new Set(["localhost", "127.0.0.1", "[::1]"]);
295
+
296
+ /**
297
+ * The URL a preview names, or `undefined` unless it is an http(s) URL on this
298
+ * machine's loopback interface. The browser opens it, so nothing else passes.
299
+ */
300
+ export const parsePreviewUrl = (value: string | undefined) => {
301
+ if (!value) return undefined;
302
+ let url: URL;
303
+ try {
304
+ url = new URL(value);
305
+ } catch {
306
+ return undefined;
307
+ }
308
+ if (url.protocol !== "http:" && url.protocol !== "https:") return undefined;
309
+ if (url.username !== "" || url.password !== "") return undefined;
310
+ const loopback =
311
+ LOOPBACK_HOSTS.has(url.hostname) || url.hostname.endsWith(".localhost");
312
+ return loopback ? url : undefined;
313
+ };
314
+
315
+ export const openInputs = (state: Checkout.State) =>
316
+ state.inputs.filter((input) => input.status === "open");
317
+
318
+ /** The current plan revision, or `undefined` before the agent proposed one. */
319
+ export const currentPlan = (state: Checkout.State): Checkout.Plan | undefined =>
320
+ state.plans.at(-1);
321
+
322
+ /** True while a proposed plan waits for the user's decision. */
323
+ export const planNeedsReview = (state: Checkout.State) =>
324
+ !isClosed(state) && currentPlan(state)?.status === "proposed";
325
+
326
+ /** The icons an agent can put on its own options, by name. */
327
+ export const OPTION_ICONS = [
328
+ "code",
329
+ "terminal",
330
+ "braces",
331
+ "git-branch",
332
+ "package",
333
+ "database",
334
+ "server",
335
+ "cloud",
336
+ "globe",
337
+ "monitor",
338
+ "smartphone",
339
+ "key",
340
+ "lock",
341
+ "shield",
342
+ "brain",
343
+ "sparkles",
344
+ "bot",
345
+ "workflow",
346
+ "file",
347
+ "folder",
348
+ "book",
349
+ "table",
350
+ "image",
351
+ "video",
352
+ "mic",
353
+ "speech",
354
+ "paperclip",
355
+ "message",
356
+ "mail",
357
+ "bell",
358
+ "calendar",
359
+ "clock",
360
+ "user",
361
+ "users",
362
+ "settings",
363
+ "wrench",
364
+ "plug",
365
+ "link",
366
+ "palette",
367
+ "search",
368
+ "zap",
369
+ "check",
370
+ "question",
371
+ ] as const;
372
+
373
+ export type OptionIcon = (typeof OPTION_ICONS)[number];
374
+
375
+ export const isOptionIcon = (value: string): value is OptionIcon =>
376
+ (OPTION_ICONS as readonly string[]).includes(value);
377
+
378
+ export const parseChoiceAnswer = (answer: string) => {
379
+ const [option = "", variant] = answer.split(":", 2);
380
+ return { option, ...(variant !== undefined && { variant }) };
381
+ };
382
+
383
+ /** The entries of a multiple choice answer, or `undefined` when the text is not a JSON array of strings. */
384
+ export const parseMultipleAnswer = (answer: string): string[] | undefined => {
385
+ let value: unknown;
386
+ try {
387
+ value = JSON.parse(answer);
388
+ } catch {
389
+ return undefined;
390
+ }
391
+ return Array.isArray(value) &&
392
+ value.every((entry) => typeof entry === "string")
393
+ ? value
394
+ : undefined;
395
+ };
396
+
397
+ type ChoiceAnswerKind = "option" | "custom" | "invalid";
398
+
399
+ const classifyChoiceEntry = (
400
+ input: Checkout.Input,
401
+ answer: string,
402
+ ): ChoiceAnswerKind => {
403
+ const { option, variant } = parseChoiceAnswer(answer);
404
+ const match = input.options?.find((candidate) => candidate.id === option);
405
+ if (!match) return answer.trim() === "" ? "invalid" : "custom";
406
+ if (!match.variants || match.variants.length === 0) {
407
+ return variant === undefined ? "option" : "invalid";
408
+ }
409
+ return match.variants.some((candidate) => candidate.id === variant)
410
+ ? "option"
411
+ : "invalid";
412
+ };
413
+
414
+ /**
415
+ * "option" when `answer` names one of the input's options (with a variant
416
+ * when the option has them), "custom" for the user's own text, "invalid"
417
+ * for an option named without its variant or with an unknown one. A
418
+ * multiple choice is classified entry by entry: "invalid" when any entry is
419
+ * or the array is empty, "custom" when any entry is the user's own text.
420
+ */
421
+ export const classifyChoiceAnswer = (
422
+ input: Checkout.Input,
423
+ answer: string,
424
+ ): ChoiceAnswerKind => {
425
+ if (!input.multiple) return classifyChoiceEntry(input, answer);
426
+ const entries = parseMultipleAnswer(answer);
427
+ if (!entries || entries.length === 0) return "invalid";
428
+ const kinds = entries.map((entry) => classifyChoiceEntry(input, entry));
429
+ if (kinds.includes("invalid")) return "invalid";
430
+ return kinds.includes("custom") ? "custom" : "option";
431
+ };
432
+
433
+ const REASONING_EFFORTS = new Set(["low", "medium", "high"]);
434
+
435
+ const isReasoningEffort = (
436
+ value: unknown,
437
+ ): value is NonNullable<Checkout.ModelAnswer["reasoningEffort"]> =>
438
+ typeof value === "string" && REASONING_EFFORTS.has(value);
439
+
440
+ /** The model answer an input holds, or `undefined` when the text is not one. */
441
+ export const parseModelAnswer = (
442
+ answer: string,
443
+ ): Checkout.ModelAnswer | undefined => {
444
+ let value: unknown;
445
+ try {
446
+ value = JSON.parse(answer);
447
+ } catch {
448
+ return undefined;
449
+ }
450
+ if (typeof value !== "object" || value === null) return undefined;
451
+ const { provider, model, reasoningEffort } = value as Record<string, unknown>;
452
+ if (
453
+ typeof provider !== "string" ||
454
+ typeof model !== "string" ||
455
+ provider === "" ||
456
+ model === ""
457
+ ) {
458
+ return undefined;
459
+ }
460
+ if (reasoningEffort === undefined) return { provider, model };
461
+ if (!isReasoningEffort(reasoningEffort)) return undefined;
462
+ return { provider, model, reasoningEffort };
463
+ };
464
+
465
+ /** Whether `answer` is a model answer naming one of the input's providers. */
466
+ export const isValidModelAnswer = (input: Checkout.Input, answer: string) => {
467
+ const parsed = parseModelAnswer(answer);
468
+ return (
469
+ parsed !== undefined &&
470
+ (input.options?.some((option) => option.id === parsed.provider) ?? false)
471
+ );
472
+ };
473
+
474
+ export const stepProgress = (state: Checkout.State) => ({
475
+ total: state.steps.length,
476
+ done: state.steps.filter(
477
+ (step) => step.status === "done" || step.status === "skipped",
478
+ ).length,
479
+ });