@palettelab/cli 0.3.62 → 0.3.63

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.
@@ -3,8 +3,9 @@
3
3
  const fs = require("fs")
4
4
  const path = require("path")
5
5
  const { spawn, spawnSync } = require("child_process")
6
+ const crypto = require("crypto")
6
7
  const { loadManifest } = require("../manifest")
7
- const { watchFrontend } = require("../bundler")
8
+ const { watchFrontend, watchFrontendBuffer } = require("../bundler")
8
9
  const { parseFlags, resolveEnvironment } = require("../environments")
9
10
  const { resolveDevPorts } = require("../ports")
10
11
  const { startSimulator } = require("../dev-simulator")
@@ -127,6 +128,70 @@ function writePlatformEnvFile(cwd, localEnv) {
127
128
  return envPath
128
129
  }
129
130
 
131
+ // `pltt dev --sandbox --watch`: keep rebuilding the frontend and PUT each
132
+ // bundle onto the preview publish. Frontend-only — backend changes still need
133
+ // a full re-run. Watch iterations skip `pltt test` by design (the initial
134
+ // publish already ran it).
135
+ async function startPreviewWatch({ cwd, flags, record, json }) {
136
+ const manifest = loadManifest(cwd)
137
+ if (!manifest.frontend?.entry) {
138
+ console.log("[pltt] --watch: manifest has no frontend entry; nothing to watch")
139
+ return
140
+ }
141
+ const envName = flags.env || process.env.PALETTE_ENV || "staging"
142
+ const env = resolveEnvironment({ cwd, flags: { ...flags, env: envName } })
143
+ let lastSha = null
144
+ let stopped = false
145
+ const pluginId = record.plugin_id || manifest.id
146
+
147
+ if (!json) {
148
+ console.log(`[pltt] watching ${manifest.frontend.entry} — frontend-only live updates (backend changes need a re-run)`)
149
+ }
150
+
151
+ await watchFrontendBuffer(
152
+ cwd,
153
+ manifest.frontend.entry,
154
+ { ...manifest.frontend, pluginId },
155
+ (bundle) => {
156
+ if (stopped) return
157
+ const sha = crypto.createHash("sha256").update(bundle).digest("hex")
158
+ if (lastSha === null) {
159
+ // Initial watch build — identical to the bundle just published.
160
+ lastSha = sha
161
+ return
162
+ }
163
+ if (sha === lastSha) return
164
+ fetch(`${env.url}/api/v1/appstore/previews/${record.id}/frontend`, {
165
+ method: "PUT",
166
+ headers: {
167
+ Authorization: `Bearer ${env.token}`,
168
+ "Content-Type": "application/javascript",
169
+ },
170
+ body: bundle,
171
+ })
172
+ .then(async (res) => {
173
+ if (res.ok) {
174
+ lastSha = sha
175
+ console.log(`[pltt] frontend pushed (${sha.slice(0, 12)}…) — preview reloads within ~4s`)
176
+ return
177
+ }
178
+ const body = await res.json().catch(() => ({}))
179
+ const detail = typeof body.detail === "string" ? body.detail : res.statusText
180
+ if (res.status === 410) {
181
+ console.error("[pltt] preview expired — re-run `pltt dev --sandbox --watch` to publish a fresh one")
182
+ stopped = true
183
+ return
184
+ }
185
+ console.error(`[pltt] frontend push failed (${res.status}): ${detail}`)
186
+ if (res.status === 403) stopped = true
187
+ })
188
+ .catch((err) => {
189
+ console.error(`[pltt] frontend push failed: ${err instanceof Error ? err.message : String(err)}`)
190
+ })
191
+ },
192
+ )
193
+ }
194
+
130
195
  async function run(args, { cwd }) {
131
196
  notifyIfUpdateAvailable(args).catch(() => {})
132
197
  const { flags, rest } = parseFlags(args)
@@ -154,6 +219,10 @@ async function run(args, { cwd }) {
154
219
  if (!json && record?.preview_url) {
155
220
  console.log(`[pltt] preview URL: ${record.preview_url}`)
156
221
  }
222
+ const watching = rest.includes("--watch") && record?.id
223
+ if (watching) {
224
+ await startPreviewWatch({ cwd, flags, record, json })
225
+ }
157
226
  if (!json && record?.id) {
158
227
  const envName = flags.env || process.env.PALETTE_ENV || "staging"
159
228
  console.log(`[pltt] status: pltt status ${record.id} --env ${envName}`)
@@ -172,6 +241,11 @@ async function run(args, { cwd }) {
172
241
  console.warn(`[pltt] run manually: pltt logs ${logPluginId} --follow --env ${envName}`)
173
242
  }
174
243
  }
244
+ if (watching) {
245
+ // Keep the process (and the frontend watcher) alive even when log
246
+ // streaming is unavailable or --json suppressed it. Ctrl+C to stop.
247
+ await new Promise(() => {})
248
+ }
175
249
  return
176
250
  }
177
251
 
@@ -28,6 +28,74 @@ function fail(message, fix) {
28
28
  return 1
29
29
  }
30
30
 
31
+ // What the platform lends this app, and what it looks like under `pltt dev`.
32
+ const PLATFORM_SERVICE_DEV_NOTES = {
33
+ llm: (cwd) =>
34
+ process.env.OPENAI_API_KEY || localEnvHas(cwd, "OPENAI_API_KEY")
35
+ ? "pltt dev: live via your OPENAI_API_KEY"
36
+ : "pltt dev: stubbed — set OPENAI_API_KEY for real calls (hosted uses the platform key)",
37
+ web: () => "pltt dev: keyless DuckDuckGo (hosted uses the platform search key)",
38
+ redis: () => "pltt dev: in-memory",
39
+ vector: () => "pltt dev: in-memory hash embeddings",
40
+ storage: () => "pltt dev: .palette/dev-storage",
41
+ }
42
+
43
+ function localEnvHas(cwd, name) {
44
+ try {
45
+ return Boolean(loadLocalEnvDetails(cwd, { apply: false }).values[name])
46
+ } catch {
47
+ return false
48
+ }
49
+ }
50
+
51
+ function reportPlatformServices(manifest, cwd) {
52
+ const declared = manifest.platform_services
53
+ const names = Array.isArray(declared) ? declared : Object.keys(declared || {})
54
+ if (!names.length) {
55
+ ok("no platform services declared")
56
+ } else {
57
+ ok(`manifest declares ${names.length} platform service(s): ${names.join(", ")}`)
58
+ for (const name of names) {
59
+ const note = PLATFORM_SERVICE_DEV_NOTES[name]
60
+ if (note) console.log(` ctx.${name} — ${note(cwd)}`)
61
+ }
62
+ }
63
+
64
+ const conns = manifest.connections || []
65
+ if (!conns.length) {
66
+ ok("no service connections declared")
67
+ return
68
+ }
69
+ const catalog = require("../connection-catalog.json")
70
+ const mockPath = path.join(cwd, ".palette", "connections.local.json")
71
+ let mocks = {}
72
+ try {
73
+ mocks = fs.existsSync(mockPath) ? JSON.parse(fs.readFileSync(mockPath, "utf8")) : {}
74
+ } catch {
75
+ mocks = {}
76
+ }
77
+ for (const spec of conns) {
78
+ const known = catalog.providers.find((p) => p.slug === spec.provider)
79
+ if (!known) {
80
+ warn(
81
+ `connection '${spec.id}' uses legacy provider '${spec.provider}'`,
82
+ "Legacy providers have no OS connection — the app must supply its own credentials.",
83
+ )
84
+ continue
85
+ }
86
+ const asked = (spec.tools || []).length
87
+ const label = asked ? `${asked} tool(s)` : `every read-only ${spec.provider} tool`
88
+ ok(`connection '${spec.id}' → ${known.label}, ${label}`)
89
+ const mocked = Object.keys(mocks[spec.id]?.tools || {}).length
90
+ if (!mocked) {
91
+ warn(
92
+ `no local responses mocked for ${spec.provider}`,
93
+ `pltt connections mock ${spec.provider}.<tool> — otherwise pltt dev raises on the call.`,
94
+ )
95
+ }
96
+ }
97
+ }
98
+
31
99
  function nodeMajor() {
32
100
  return Number(process.versions.node.split(".")[0])
33
101
  }
@@ -121,6 +189,8 @@ async function run(args, { cwd }) {
121
189
  failures += checkEntry(cwd, `tool[${tool.name}]`, tool.entry)
122
190
  }
123
191
 
192
+ reportPlatformServices(manifest, cwd)
193
+
124
194
  const secrets = declaredSecrets(manifest)
125
195
  const localEnv = loadLocalEnvDetails(cwd, { apply: false })
126
196
  const localSecrets = localEnv.values
@@ -0,0 +1,43 @@
1
+ "use strict"
2
+
3
+ const fs = require("fs")
4
+ const path = require("path")
5
+ const { spawn } = require("child_process")
6
+
7
+ function openInBrowser(url) {
8
+ const [cmd, args] =
9
+ process.platform === "darwin"
10
+ ? ["open", [url]]
11
+ : process.platform === "win32"
12
+ ? ["cmd", ["/c", "start", "", url]]
13
+ : ["xdg-open", [url]]
14
+ const child = spawn(cmd, args, { stdio: "ignore", detached: true })
15
+ child.unref()
16
+ }
17
+
18
+ async function run(_args, { cwd }) {
19
+ const recordPath = path.join(cwd, ".palette", "last-publish.json")
20
+ if (!fs.existsSync(recordPath)) {
21
+ console.error(
22
+ "[pltt] no publish record found. Run `pltt dev --sandbox` (or `pltt publish`) first.",
23
+ )
24
+ process.exit(1)
25
+ }
26
+ let record
27
+ try {
28
+ record = JSON.parse(fs.readFileSync(recordPath, "utf8"))
29
+ } catch (err) {
30
+ console.error(`[pltt] could not read ${recordPath}: ${err instanceof Error ? err.message : String(err)}`)
31
+ process.exit(1)
32
+ }
33
+ if (!record.preview_url) {
34
+ console.error(
35
+ "[pltt] last publish has no preview URL. Publish a preview with `pltt dev --sandbox`.",
36
+ )
37
+ process.exit(1)
38
+ }
39
+ console.log(`[pltt] opening ${record.preview_url}`)
40
+ openInBrowser(record.preview_url)
41
+ }
42
+
43
+ module.exports = run