@tsplc/agent 0.5.9 → 0.5.12

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.
Files changed (51) hide show
  1. package/dist/cli.js +2660 -525
  2. package/dist/cli.js.map +32 -18
  3. package/dist/client.js +262 -37
  4. package/dist/client.js.map +4 -4
  5. package/dist/index.js +2325 -415
  6. package/dist/index.js.map +27 -14
  7. package/package.json +6 -5
  8. package/src/access.ts +276 -0
  9. package/src/agent.ts +130 -29
  10. package/src/audit.ts +64 -0
  11. package/src/cli.ts +81 -5
  12. package/src/client.ts +204 -36
  13. package/src/controller.ts +60 -0
  14. package/src/dashboard/index.ts +118 -0
  15. package/src/dashboard/ui/access.ts +192 -0
  16. package/src/dashboard/ui/actions.ts +192 -0
  17. package/src/dashboard/ui/api.ts +158 -0
  18. package/src/dashboard/ui/app.ts +41 -0
  19. package/src/dashboard/ui/approval.ts +132 -0
  20. package/src/dashboard/ui/audit.ts +115 -0
  21. package/src/dashboard/ui/contract.ts +19 -0
  22. package/src/dashboard/ui/dashboard.css +1332 -0
  23. package/src/dashboard/ui/deployment.ts +432 -0
  24. package/src/dashboard/ui/deployments.ts +317 -0
  25. package/src/dashboard/ui/details.ts +240 -0
  26. package/src/dashboard/ui/dom.ts +91 -0
  27. package/src/dashboard/ui/environment.ts +326 -0
  28. package/src/dashboard/ui/format.ts +118 -0
  29. package/src/dashboard/ui/logs.ts +127 -0
  30. package/src/dashboard/ui/main.ts +86 -0
  31. package/src/dashboard/ui/overlay.ts +146 -0
  32. package/src/dashboard/ui/shell.ts +192 -0
  33. package/src/dashboard/ui/source.ts +144 -0
  34. package/src/deploy.ts +174 -144
  35. package/src/deployments/health.ts +80 -0
  36. package/src/deployments/retention.ts +45 -0
  37. package/src/deployments/routes.ts +273 -0
  38. package/src/deployments/runner.ts +877 -0
  39. package/src/deployments/store.ts +233 -0
  40. package/src/environment.ts +284 -0
  41. package/src/index.ts +4 -0
  42. package/src/login.ts +17 -0
  43. package/src/operations.ts +6 -1
  44. package/src/protocol.ts +259 -2
  45. package/src/releases.ts +73 -1
  46. package/src/routes.ts +52 -0
  47. package/src/secrets.ts +74 -4
  48. package/src/server-process.ts +33 -16
  49. package/src/sessions.ts +58 -0
  50. package/src/state.ts +2 -0
  51. package/src/tailscale.ts +112 -0
@@ -0,0 +1,432 @@
1
+ import { errorMessage } from "../../errors";
2
+ import type { DeploymentLogLine, DeploymentRecord } from "../../protocol";
3
+ import { cancel, deploymentMenu, redeploy, rollback } from "./actions";
4
+ import { api } from "./api";
5
+ import { type App, deploymentHref } from "./app";
6
+ import { approvalBanner } from "./approval";
7
+ import { gitSummary, origin, requester } from "./deployments";
8
+ import { activeWindow, changesTab, healthSummary, runtimeTab } from "./details";
9
+ import { badge, dot, empty, fill, h, type View } from "./dom";
10
+ import {
11
+ cancelable,
12
+ formatAgo,
13
+ formatDeploymentDuration,
14
+ formatSpan,
15
+ phaseDuration,
16
+ phaseLabels,
17
+ shortId,
18
+ states,
19
+ } from "./format";
20
+ import { logViewer } from "./logs";
21
+ import { menuButton } from "./overlay";
22
+ import { sourceView } from "./source";
23
+
24
+ const tabs = [
25
+ { name: "logs", label: "Logs" },
26
+ { name: "changes", label: "Changes" },
27
+ { name: "source", label: "Source" },
28
+ { name: "runtime", label: "Runtime" },
29
+ { name: "debug", label: "Debug" },
30
+ ] as const;
31
+ type Tab = (typeof tabs)[number]["name"];
32
+
33
+ /** A deployment page's path: `/deployments/<id>`, then a tab, then the tab's own path. */
34
+ export const deploymentLocation = (pathname: string) => {
35
+ const [, , id = "", tab = "", ...rest] = pathname.split("/");
36
+ return {
37
+ id: decodeURIComponent(id),
38
+ tab: tabs.find((option) => option.name === tab)?.name ?? "logs",
39
+ rest: rest.map(decodeURIComponent).join("/"),
40
+ };
41
+ };
42
+
43
+ /** How long a lost event stream waits before it reconnects. */
44
+ const reconnectAfter = 2000;
45
+
46
+ const phaseTimeline = (record: DeploymentRecord, now: number) =>
47
+ h(
48
+ "ol",
49
+ { class: "phases" },
50
+ ...record.phases.map((phase) =>
51
+ h(
52
+ "li",
53
+ { class: `phase o-${phase.outcome}` },
54
+ h("span", { class: "phase-mark" }),
55
+ h("span", { class: "phase-name" }, phaseLabels[phase.name]),
56
+ h(
57
+ "span",
58
+ { class: "muted mono" },
59
+ phase.outcome === "skipped"
60
+ ? "skipped"
61
+ : formatSpan(phaseDuration(phase, now)),
62
+ ),
63
+ ),
64
+ ),
65
+ );
66
+
67
+ const fact = (label: string, ...value: (Node | string | null)[]) =>
68
+ h(
69
+ "div",
70
+ { class: "fact" },
71
+ h("div", { class: "fact-label" }, label),
72
+ h("div", { class: "fact-value" }, ...value),
73
+ );
74
+
75
+ /** One deployment: its summary, what it waits on, and its logs, changes, source, runtime and debugger. */
76
+ export const deploymentView = (
77
+ app: App,
78
+ pathname: string,
79
+ ): View & { navigate(pathname: string): boolean } => {
80
+ const { id } = deploymentLocation(pathname);
81
+ let at = deploymentLocation(pathname);
82
+ let record: DeploymentRecord | null = null;
83
+ let current: string | null = null;
84
+ let lastGood: string | null = null;
85
+ const lines: DeploymentLogLine[] = [];
86
+ const logs = logViewer(lines);
87
+ let source: ReturnType<typeof sourceView> | null = null;
88
+ let debug: HTMLElement | null = null;
89
+ let events: EventSource | null = null;
90
+ let retry: ReturnType<typeof setTimeout> | undefined;
91
+ let ticker: ReturnType<typeof setInterval> | undefined;
92
+ let disposed = false;
93
+
94
+ const header = h(
95
+ "div",
96
+ { class: "deployment-header" },
97
+ empty("Loading deployment…"),
98
+ );
99
+ const banner = h("div");
100
+ const overview = h("div");
101
+ const tabBar = h("nav", { class: "tabs" });
102
+ const content = h("div", { class: "tab-content" });
103
+
104
+ const reload = async () => {
105
+ try {
106
+ const [loaded, list] = await Promise.all([
107
+ api.deployment(id),
108
+ api.deployments(),
109
+ ]);
110
+ current = list.current;
111
+ lastGood = list.lastGood;
112
+ show(loaded);
113
+ } catch (error) {
114
+ fill(header, h("p", { class: "error-text" }, errorMessage(error)));
115
+ }
116
+ };
117
+
118
+ const renderHeader = (shown: DeploymentRecord) => {
119
+ const state = states[shown.state];
120
+ const now = Date.now();
121
+ const isCurrent = current === shown.id;
122
+ const deployer = app.can("deployer");
123
+ fill(
124
+ header,
125
+ h(
126
+ "div",
127
+ { class: "crumbs" },
128
+ h(
129
+ "a",
130
+ {
131
+ href: "/deployments",
132
+ onclick: (event) => {
133
+ event.preventDefault();
134
+ app.navigate("/deployments");
135
+ },
136
+ },
137
+ "Deployments",
138
+ ),
139
+ " / ",
140
+ h("span", { class: "mono" }, shortId(shown.id)),
141
+ ),
142
+ h(
143
+ "div",
144
+ { class: "page-header" },
145
+ h(
146
+ "div",
147
+ {},
148
+ h("h1", { class: "mono" }, shown.id),
149
+ h(
150
+ "div",
151
+ { class: "title-state" },
152
+ dot(state.tone),
153
+ state.label,
154
+ isCurrent ? badge("Current", "ok") : null,
155
+ lastGood === shown.id && !isCurrent
156
+ ? badge("Last good", "neutral")
157
+ : null,
158
+ ),
159
+ ),
160
+ h(
161
+ "div",
162
+ { class: "actions" },
163
+ deployer && cancelable(shown.state)
164
+ ? h(
165
+ "button",
166
+ {
167
+ class: "button",
168
+ onclick: () => void cancel(shown).then(reload),
169
+ },
170
+ "Cancel",
171
+ )
172
+ : null,
173
+ deployer
174
+ ? h(
175
+ "button",
176
+ {
177
+ class: "button",
178
+ disabled: !state.finished,
179
+ onclick: () => void redeploy(app, shown),
180
+ },
181
+ "Redeploy",
182
+ )
183
+ : null,
184
+ deployer
185
+ ? h(
186
+ "button",
187
+ {
188
+ class: "button primary",
189
+ disabled: shown.state !== "ready" || isCurrent,
190
+ title: isCurrent ? "This deployment runs now" : "",
191
+ onclick: () => void rollback(app, shown),
192
+ },
193
+ "Rollback to this",
194
+ )
195
+ : null,
196
+ menuButton("More actions", () =>
197
+ deploymentMenu(app, shown, { current: isCurrent, open: false }),
198
+ ),
199
+ ),
200
+ ),
201
+ );
202
+ fill(
203
+ overview,
204
+ h(
205
+ "div",
206
+ { class: "summary" },
207
+ fact(
208
+ "Created",
209
+ formatAgo(shown.createdAt, now),
210
+ h("span", { class: "muted" }, " by "),
211
+ requester(shown),
212
+ ),
213
+ fact(
214
+ "Duration",
215
+ h("span", { class: "mono" }, formatDeploymentDuration(shown, now)),
216
+ ),
217
+ fact(
218
+ "Mode",
219
+ shown.mode === "review" ? "Review" : "Apply",
220
+ h(
221
+ "span",
222
+ { class: "muted" },
223
+ shown.operation === "download"
224
+ ? " · full download"
225
+ : " · online change",
226
+ ),
227
+ ),
228
+ fact("Origin", origin(shown, app) ?? "Upload from tsplc deploy"),
229
+ fact("Source", gitSummary(shown)),
230
+ fact(
231
+ "Release",
232
+ h(
233
+ "span",
234
+ { class: "mono", title: shown.release },
235
+ shown.release.slice(0, 16),
236
+ ),
237
+ shown.build
238
+ ? h(
239
+ "span",
240
+ { class: "muted mono" },
241
+ ` · build ${shown.build.slice(0, 12)}`,
242
+ )
243
+ : null,
244
+ ),
245
+ fact("Active", activeWindow(shown)),
246
+ fact("Health", healthSummary(shown.health)),
247
+ ),
248
+ shown.phases.length ? phaseTimeline(shown, now) : null,
249
+ );
250
+ };
251
+
252
+ const renderTabs = (shown: DeploymentRecord) => {
253
+ fill(
254
+ tabBar,
255
+ ...tabs.map((tab) =>
256
+ h(
257
+ "a",
258
+ {
259
+ class: `tab${tab.name === at.tab ? " on" : ""}`,
260
+ href: deploymentHref(id, tab.name === "logs" ? "" : tab.name),
261
+ onclick: (event) => {
262
+ event.preventDefault();
263
+ app.navigate(
264
+ deploymentHref(id, tab.name === "logs" ? "" : tab.name),
265
+ );
266
+ },
267
+ },
268
+ tab.label,
269
+ tab.name === "changes" && shown.findings.length
270
+ ? h("span", { class: "count-pill" }, String(shown.findings.length))
271
+ : null,
272
+ ),
273
+ ),
274
+ );
275
+ };
276
+
277
+ const debugTab = (shown: DeploymentRecord) => {
278
+ debug ??= h("iframe", {
279
+ class: "debugger",
280
+ src: "/debug/",
281
+ title: "Debugger",
282
+ });
283
+ return h(
284
+ "div",
285
+ {},
286
+ current === shown.id
287
+ ? null
288
+ : h(
289
+ "p",
290
+ { class: "callout" },
291
+ "The debugger shows the release running now, which isn't this deployment's.",
292
+ ),
293
+ h(
294
+ "p",
295
+ { class: "muted" },
296
+ h(
297
+ "a",
298
+ { href: "/debug/", target: "_blank", rel: "noopener" },
299
+ "Open the debugger in a new tab",
300
+ ),
301
+ ),
302
+ debug,
303
+ );
304
+ };
305
+
306
+ const tabContent: Record<Tab, (shown: DeploymentRecord) => HTMLElement> = {
307
+ logs: () => logs.element,
308
+ changes: changesTab,
309
+ source: () => {
310
+ source ??= sourceView(app, id);
311
+ void source.show(at.rest || null);
312
+ return source.element;
313
+ },
314
+ runtime: runtimeTab,
315
+ debug: debugTab,
316
+ };
317
+
318
+ const renderContent = (shown: DeploymentRecord) => {
319
+ fill(content, tabContent[at.tab](shown));
320
+ };
321
+
322
+ const show = (shown: DeploymentRecord) => {
323
+ const first = record === null;
324
+ record = shown;
325
+ renderHeader(shown);
326
+ fill(banner, approvalBanner(app, shown, reload));
327
+ renderTabs(shown);
328
+ // Logs, source and the debugger keep their own state; the other tabs follow the record.
329
+ if (first || at.tab === "changes" || at.tab === "runtime") {
330
+ renderContent(shown);
331
+ }
332
+ if (states[shown.state].finished) {
333
+ stopLive();
334
+ } else {
335
+ startLive();
336
+ }
337
+ };
338
+
339
+ const append = (added: DeploymentLogLine[]) => {
340
+ const last = lines.at(-1)?.seq ?? 0;
341
+ const fresh = added.filter((line) => line.seq > last);
342
+ lines.push(...fresh);
343
+ logs.added(fresh);
344
+ };
345
+
346
+ const startLive = () => {
347
+ ticker ??= setInterval(() => {
348
+ if (record) {
349
+ renderHeader(record);
350
+ }
351
+ }, 1000);
352
+ if (events || retry || disposed) {
353
+ return;
354
+ }
355
+ const stream = new EventSource(api.eventsUrl(id, lines.at(-1)?.seq ?? 0));
356
+ events = stream;
357
+ stream.addEventListener("record", (event) => {
358
+ if (event instanceof MessageEvent) {
359
+ show(JSON.parse(event.data));
360
+ }
361
+ });
362
+ stream.addEventListener("log", (event) => {
363
+ if (event instanceof MessageEvent) {
364
+ append([JSON.parse(event.data)]);
365
+ }
366
+ });
367
+ stream.addEventListener("error", () => {
368
+ stream.close();
369
+ events = null;
370
+ // The stream ends once the deployment finishes; otherwise it was cut, and resumes.
371
+ if (!disposed && record && !states[record.state].finished) {
372
+ retry = setTimeout(() => {
373
+ retry = undefined;
374
+ startLive();
375
+ }, reconnectAfter);
376
+ }
377
+ });
378
+ };
379
+
380
+ const stopLive = () => {
381
+ events?.close();
382
+ events = null;
383
+ clearTimeout(retry);
384
+ retry = undefined;
385
+ clearInterval(ticker);
386
+ ticker = undefined;
387
+ };
388
+
389
+ // The log so far is read before the live stream opens, which then resumes after its last line.
390
+ void Promise.all([
391
+ api.deployment(id),
392
+ api.deployments(),
393
+ api.logs(id, 0),
394
+ ]).then(
395
+ ([loaded, list, initial]) => {
396
+ current = list.current;
397
+ lastGood = list.lastGood;
398
+ append(initial);
399
+ show(loaded);
400
+ },
401
+ (error: unknown) =>
402
+ fill(header, h("p", { class: "error-text" }, errorMessage(error))),
403
+ );
404
+
405
+ return {
406
+ element: h(
407
+ "div",
408
+ { class: "page" },
409
+ header,
410
+ banner,
411
+ overview,
412
+ tabBar,
413
+ content,
414
+ ),
415
+ navigate: (next) => {
416
+ const location = deploymentLocation(next);
417
+ if (location.id !== id) {
418
+ return false;
419
+ }
420
+ at = location;
421
+ if (record) {
422
+ renderTabs(record);
423
+ renderContent(record);
424
+ }
425
+ return true;
426
+ },
427
+ dispose: () => {
428
+ disposed = true;
429
+ stopLive();
430
+ },
431
+ };
432
+ };