libretto 0.5.3-experimental.5 → 0.5.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 (126) hide show
  1. package/README.md +114 -37
  2. package/README.template.md +160 -0
  3. package/dist/cli/cli.js +22 -97
  4. package/dist/cli/commands/browser.js +86 -59
  5. package/dist/cli/commands/deploy.js +148 -0
  6. package/dist/cli/commands/execution.js +218 -96
  7. package/dist/cli/commands/init.js +34 -29
  8. package/dist/cli/commands/logs.js +4 -5
  9. package/dist/cli/commands/shared.js +30 -29
  10. package/dist/cli/commands/snapshot.js +26 -39
  11. package/dist/cli/core/ai-config.js +21 -4
  12. package/dist/cli/core/api-snapshot-analyzer.js +15 -5
  13. package/dist/cli/core/browser.js +207 -37
  14. package/dist/cli/core/context.js +4 -1
  15. package/dist/cli/core/deploy-artifact.js +687 -0
  16. package/dist/cli/core/session-telemetry.js +434 -174
  17. package/dist/cli/core/session.js +21 -8
  18. package/dist/cli/core/snapshot-analyzer.js +14 -31
  19. package/dist/cli/core/snapshot-api-config.js +2 -6
  20. package/dist/cli/core/telemetry.js +20 -4
  21. package/dist/cli/framework/simple-cli.js +144 -43
  22. package/dist/cli/router.js +16 -21
  23. package/dist/cli/workers/run-integration-runtime.js +25 -45
  24. package/dist/cli/workers/run-integration-worker-protocol.js +3 -2
  25. package/dist/cli/workers/run-integration-worker.js +1 -4
  26. package/dist/index.d.ts +1 -2
  27. package/dist/index.js +13 -10
  28. package/dist/runtime/download/download.js +5 -1
  29. package/dist/runtime/extract/extract.js +11 -2
  30. package/dist/runtime/network/network.js +8 -1
  31. package/dist/runtime/recovery/agent.js +6 -2
  32. package/dist/runtime/recovery/errors.js +3 -1
  33. package/dist/runtime/recovery/recovery.js +3 -1
  34. package/dist/shared/condense-dom/condense-dom.js +17 -69
  35. package/dist/shared/config/config.d.ts +1 -9
  36. package/dist/shared/config/config.js +0 -18
  37. package/dist/shared/config/index.d.ts +2 -1
  38. package/dist/shared/config/index.js +0 -10
  39. package/dist/shared/debug/pause.js +9 -3
  40. package/dist/shared/dom-semantics.d.ts +8 -0
  41. package/dist/shared/dom-semantics.js +69 -0
  42. package/dist/shared/instrumentation/instrument.js +101 -5
  43. package/dist/shared/llm/ai-sdk-adapter.js +3 -1
  44. package/dist/shared/llm/client.js +3 -1
  45. package/dist/shared/logger/index.js +4 -1
  46. package/dist/shared/run/api.js +3 -1
  47. package/dist/shared/run/browser.js +47 -3
  48. package/dist/shared/state/session-state.d.ts +2 -1
  49. package/dist/shared/state/session-state.js +5 -2
  50. package/dist/shared/visualization/ghost-cursor.js +36 -14
  51. package/dist/shared/visualization/highlight.js +9 -6
  52. package/dist/shared/workflow/workflow.d.ts +18 -10
  53. package/dist/shared/workflow/workflow.js +50 -5
  54. package/package.json +14 -6
  55. package/scripts/generate-changelog.ts +132 -0
  56. package/scripts/postinstall.mjs +4 -3
  57. package/scripts/skills-libretto.mjs +2 -88
  58. package/scripts/summarize-evals.mjs +32 -10
  59. package/skills/libretto/SKILL.md +132 -62
  60. package/skills/libretto/references/action-logs.md +101 -0
  61. package/skills/libretto/references/auth-profiles.md +1 -2
  62. package/skills/libretto/references/code-generation-rules.md +176 -0
  63. package/skills/libretto/references/configuration-file-reference.md +53 -0
  64. package/skills/libretto/references/pages-and-page-targeting.md +1 -1
  65. package/skills/libretto/references/site-security-review.md +143 -0
  66. package/src/cli/cli.ts +23 -110
  67. package/src/cli/commands/browser.ts +94 -70
  68. package/src/cli/commands/deploy.ts +198 -0
  69. package/src/cli/commands/execution.ts +251 -111
  70. package/src/cli/commands/init.ts +37 -33
  71. package/src/cli/commands/logs.ts +7 -7
  72. package/src/cli/commands/shared.ts +36 -37
  73. package/src/cli/commands/snapshot.ts +44 -59
  74. package/src/cli/core/ai-config.ts +24 -4
  75. package/src/cli/core/api-snapshot-analyzer.ts +17 -6
  76. package/src/cli/core/browser.ts +260 -49
  77. package/src/cli/core/context.ts +7 -2
  78. package/src/cli/core/deploy-artifact.ts +938 -0
  79. package/src/cli/core/session-telemetry.ts +449 -197
  80. package/src/cli/core/session.ts +21 -7
  81. package/src/cli/core/snapshot-analyzer.ts +26 -46
  82. package/src/cli/core/snapshot-api-config.ts +170 -175
  83. package/src/cli/core/telemetry.ts +39 -4
  84. package/src/cli/framework/simple-cli.ts +281 -98
  85. package/src/cli/router.ts +15 -21
  86. package/src/cli/workers/run-integration-runtime.ts +35 -57
  87. package/src/cli/workers/run-integration-worker-protocol.ts +2 -1
  88. package/src/cli/workers/run-integration-worker.ts +1 -4
  89. package/src/index.ts +77 -67
  90. package/src/runtime/download/download.ts +62 -58
  91. package/src/runtime/download/index.ts +5 -5
  92. package/src/runtime/extract/extract.ts +71 -61
  93. package/src/runtime/network/index.ts +3 -3
  94. package/src/runtime/network/network.ts +99 -93
  95. package/src/runtime/recovery/agent.ts +217 -212
  96. package/src/runtime/recovery/errors.ts +107 -104
  97. package/src/runtime/recovery/index.ts +3 -3
  98. package/src/runtime/recovery/recovery.ts +38 -35
  99. package/src/shared/condense-dom/condense-dom.ts +27 -82
  100. package/src/shared/config/config.ts +0 -19
  101. package/src/shared/config/index.ts +0 -5
  102. package/src/shared/debug/pause.ts +57 -51
  103. package/src/shared/dom-semantics.ts +68 -0
  104. package/src/shared/instrumentation/errors.ts +64 -62
  105. package/src/shared/instrumentation/index.ts +5 -5
  106. package/src/shared/instrumentation/instrument.ts +339 -209
  107. package/src/shared/llm/ai-sdk-adapter.ts +58 -55
  108. package/src/shared/llm/client.ts +181 -174
  109. package/src/shared/llm/types.ts +39 -39
  110. package/src/shared/logger/index.ts +11 -4
  111. package/src/shared/logger/logger.ts +312 -306
  112. package/src/shared/logger/sinks.ts +118 -114
  113. package/src/shared/paths/paths.ts +50 -49
  114. package/src/shared/paths/repo-root.ts +17 -17
  115. package/src/shared/run/api.ts +5 -1
  116. package/src/shared/run/browser.ts +65 -3
  117. package/src/shared/state/index.ts +9 -9
  118. package/src/shared/state/session-state.ts +46 -43
  119. package/src/shared/visualization/ghost-cursor.ts +180 -149
  120. package/src/shared/visualization/highlight.ts +89 -86
  121. package/src/shared/visualization/index.ts +13 -13
  122. package/src/shared/workflow/workflow.ts +107 -30
  123. package/scripts/check-skills-sync.mjs +0 -23
  124. package/scripts/prepare-release.sh +0 -97
  125. package/skills/libretto/references/reverse-engineering-network-requests.md +0 -75
  126. package/skills/libretto/references/user-action-log.md +0 -31
@@ -0,0 +1,198 @@
1
+ import { randomBytes } from "node:crypto";
2
+ import { z } from "zod";
3
+ import { buildHostedDeployTarball } from "../core/deploy-artifact.js";
4
+ import { SimpleCLI } from "../framework/simple-cli.js";
5
+
6
+ type DeploymentStatus = "building" | "ready" | "failed";
7
+
8
+ type DeploymentResponse = {
9
+ json: {
10
+ deployment_id: string;
11
+ status: DeploymentStatus;
12
+ workflows?: string[] | null;
13
+ build_error?: string | null;
14
+ };
15
+ };
16
+
17
+ function generateDeploymentName(): string {
18
+ return `deploy-${Date.now().toString(36)}-${randomBytes(4).toString("hex")}`;
19
+ }
20
+
21
+ function getConfig() {
22
+ const apiUrl = process.env.LIBRETTO_API_URL;
23
+ const apiKey = process.env.LIBRETTO_API_KEY;
24
+
25
+ if (!apiUrl) {
26
+ throw new Error(
27
+ "LIBRETTO_API_URL environment variable is required.",
28
+ );
29
+ }
30
+ if (!apiKey) {
31
+ throw new Error(
32
+ "LIBRETTO_API_KEY environment variable is required.",
33
+ );
34
+ }
35
+
36
+ return { apiUrl: apiUrl.replace(/\/$/, ""), apiKey };
37
+ }
38
+
39
+ async function postJson(
40
+ apiUrl: string,
41
+ apiKey: string,
42
+ path: string,
43
+ input: Record<string, unknown> = {},
44
+ ): Promise<Response> {
45
+ return fetch(`${apiUrl}${path}`, {
46
+ method: "POST",
47
+ headers: {
48
+ "x-api-key": apiKey,
49
+ "Content-Type": "application/json",
50
+ },
51
+ body: JSON.stringify({ json: input }),
52
+ });
53
+ }
54
+
55
+ async function pollDeployment(
56
+ apiUrl: string,
57
+ apiKey: string,
58
+ deploymentId: string,
59
+ pollIntervalMs: number,
60
+ maxWaitMs: number,
61
+ ): Promise<DeploymentResponse["json"]> {
62
+ const start = Date.now();
63
+ let status: DeploymentStatus = "building";
64
+ let deployment: DeploymentResponse["json"] | undefined;
65
+
66
+ while (status === "building" && Date.now() - start < maxWaitMs) {
67
+ await new Promise((r) => setTimeout(r, pollIntervalMs));
68
+
69
+ const res = await postJson(apiUrl, apiKey, "/v1/deployments/get", {
70
+ id: deploymentId,
71
+ });
72
+ const body = (await res.json()) as DeploymentResponse;
73
+ if (res.status !== 200) {
74
+ throw new Error(
75
+ `Failed to get deployment status (${res.status}): ${JSON.stringify(body)}`,
76
+ );
77
+ }
78
+ status = body.json.status;
79
+ deployment = body.json;
80
+ process.stdout.write(".");
81
+ }
82
+ console.log();
83
+
84
+ if (!deployment) {
85
+ throw new Error("Deployment timed out before receiving a status update.");
86
+ }
87
+
88
+ return deployment;
89
+ }
90
+
91
+ export const deployInput = SimpleCLI.input({
92
+ positionals: [
93
+ SimpleCLI.positional("sourceDir", z.string().default("."), {
94
+ help: "Path to source directory (default: current directory)",
95
+ }),
96
+ ],
97
+ named: {
98
+ description: SimpleCLI.option(z.string().optional(), {
99
+ help: "Deployment description",
100
+ }),
101
+ entryPoint: SimpleCLI.option(z.string().optional(), {
102
+ name: "entry-point",
103
+ help: "Entry point file (default: index.ts)",
104
+ }),
105
+ external: SimpleCLI.option(
106
+ z
107
+ .string()
108
+ .optional()
109
+ .transform((value) =>
110
+ value
111
+ ?.split(",")
112
+ .map((entry) => entry.trim())
113
+ .filter((entry) => entry.length > 0) ?? [],
114
+ ),
115
+ {
116
+ help:
117
+ "Comma-separated packages to keep out of the bundle and install into the deployed package",
118
+ },
119
+ ),
120
+ },
121
+ });
122
+
123
+ export const deployCommand = SimpleCLI.command({
124
+ description: "Deploy workflows to the hosted platform",
125
+ experimental: true,
126
+ })
127
+ .input(deployInput)
128
+ .handle(async ({ input }) => {
129
+ const { apiUrl, apiKey } = getConfig();
130
+ const deploymentName = generateDeploymentName();
131
+
132
+ // Hosted deploy uploads a generated artifact with a deploy entrypoint and
133
+ // a minimal manifest. Bundled code is embedded in the generated files;
134
+ // external packages are listed in the manifest for installation.
135
+ console.log("Bundling hosted deployment artifact...");
136
+ const { entryPoint, source } = await buildHostedDeployTarball({
137
+ additionalExternals: input.external,
138
+ deploymentName,
139
+ entryPoint: input.entryPoint,
140
+ sourceDir: input.sourceDir,
141
+ });
142
+
143
+ const createPayload: Record<string, unknown> = {
144
+ source,
145
+ entry_point: entryPoint,
146
+ };
147
+ if (input.description) createPayload.description = input.description;
148
+
149
+ console.log("Uploading deployment...");
150
+ const res = await postJson(
151
+ apiUrl,
152
+ apiKey,
153
+ "/v1/deployments/create",
154
+ createPayload,
155
+ );
156
+ const body = (await res.json()) as DeploymentResponse;
157
+ if (res.status !== 200) {
158
+ throw new Error(
159
+ `Failed to create deployment (${res.status}): ${JSON.stringify(body)}`,
160
+ );
161
+ }
162
+
163
+ const { deployment_id, status } = body.json;
164
+ console.log(`Deployment created: ${deployment_id}`);
165
+ console.log(`Status: ${status}`);
166
+
167
+ if (status === "building") {
168
+ process.stdout.write("Waiting for build");
169
+ const deployment = await pollDeployment(
170
+ apiUrl,
171
+ apiKey,
172
+ deployment_id,
173
+ 10_000,
174
+ 5 * 60 * 1000,
175
+ );
176
+
177
+ if (deployment.status === "failed") {
178
+ throw new Error(
179
+ `Build failed: ${deployment.build_error ?? "unknown error"}`,
180
+ );
181
+ }
182
+
183
+ if (deployment.status === "ready") {
184
+ console.log(`Build complete.`);
185
+ if (deployment.workflows?.length) {
186
+ console.log(
187
+ `Workflows: ${deployment.workflows.join(", ")}`,
188
+ );
189
+ }
190
+ } else {
191
+ console.log(
192
+ `Build still in progress (timed out waiting). Check status with deployment ID: ${deployment_id}`,
193
+ );
194
+ }
195
+ }
196
+
197
+ return deployment_id;
198
+ });