@tsplc/tmp-agent 0.0.0-stage → 0.6.3

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