@palettelab/cli 0.3.62 → 0.3.65
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.
- package/README.md +185 -9
- package/backend-sdk/palette_sdk/__init__.py +13 -1
- package/backend-sdk/palette_sdk/connections.py +49 -11
- package/backend-sdk/palette_sdk/connections_generated.py +246 -0
- package/backend-sdk/palette_sdk/data_rooms.py +2 -2
- package/backend-sdk/palette_sdk/db/base.py +4 -1
- package/backend-sdk/palette_sdk/db/rls.py +7 -1
- package/backend-sdk/palette_sdk/manifest.py +16 -2
- package/backend-sdk/palette_sdk/platform_services.py +400 -0
- package/backend-sdk/palette_sdk/plugin_context.py +41 -2
- package/backend-sdk/palette_sdk/plugin_router.py +64 -8
- package/backend-sdk/palette_sdk/repository.py +12 -3
- package/backend-sdk/palette_sdk/talk.py +65 -0
- package/backend-sdk/palette_sdk/tasks.py +117 -0
- package/backend-sdk/pyproject.toml +1 -1
- package/docs/python-backend-sdk.md +196 -9
- package/lib/bundler.js +64 -0
- package/lib/cli.js +25 -3
- package/lib/commands/capabilities.js +1 -1
- package/lib/commands/connections.js +234 -0
- package/lib/commands/dev.js +75 -1
- package/lib/commands/doctor.js +85 -0
- package/lib/commands/open.js +43 -0
- package/lib/commands/platform.js +137 -0
- package/lib/commands/test.js +47 -4
- package/lib/connection-catalog.json +1185 -0
- package/lib/dev-simulator.js +232 -3
- package/lib/manifest.js +115 -5
- package/package.json +1 -1
- package/template-fallback/README.md +9 -0
- package/template-fallback/frontend/src/translations.ts +2 -2
- package/template-fallback/package.json +1 -1
- package/template-fallback/palette-plugin.json +1 -0
- package/template-fallback/templates/agent-tool/palette-plugin.json +1 -0
- package/template-fallback/templates/consumer-app/package.json +1 -1
- package/template-fallback/templates/consumer-app/palette-plugin.json +1 -0
- package/template-fallback/templates/dashboard/package.json +1 -1
- package/template-fallback/templates/dashboard/palette-plugin.json +1 -0
- package/template-fallback/templates/database/package.json +1 -1
- package/template-fallback/templates/database/palette-plugin.json +1 -0
- package/template-fallback/templates/external-service/package.json +1 -1
- package/template-fallback/templates/external-service/palette-plugin.json +1 -0
- package/template-fallback/templates/frontend-only/package.json +1 -1
- package/template-fallback/templates/frontend-only/palette-plugin.json +1 -0
- package/template-fallback/templates/next/package.json +1 -1
- package/template-fallback/templates/next/palette-plugin.json +1 -0
- package/template-fallback/templates/palette-app/package.json +1 -1
- package/template-fallback/templates/palette-app/palette-plugin.json +1 -0
- package/template-fallback/templates/provider-app/package.json +1 -1
- package/template-fallback/templates/provider-app/palette-plugin.json +1 -0
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* pltt connections — use the OS service connections from your app.
|
|
5
|
+
*
|
|
6
|
+
* Your app never does its own OAuth and never holds a provider token. The user
|
|
7
|
+
* connects Google/Slack/… once in Palette Connect; your app declares the tools
|
|
8
|
+
* it needs, the user grants them, and the platform proxies every call (keeping
|
|
9
|
+
* the scope check, write approval and audit trail).
|
|
10
|
+
*
|
|
11
|
+
* pltt connections list — providers + tools, and what you declare
|
|
12
|
+
* pltt connections add <provider> [--tools a,b]
|
|
13
|
+
* — write the connections block
|
|
14
|
+
* pltt connections pull — generate typed stubs for your app
|
|
15
|
+
* pltt connections mock <provider>.<tool> [--result '{...}']
|
|
16
|
+
* — seed a pltt dev response
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const fs = require("fs")
|
|
20
|
+
const path = require("path")
|
|
21
|
+
|
|
22
|
+
const CATALOG = require("../connection-catalog.json")
|
|
23
|
+
|
|
24
|
+
function readManifest(cwd) {
|
|
25
|
+
const p = path.join(cwd, "palette-plugin.json")
|
|
26
|
+
if (!fs.existsSync(p)) throw new Error("no palette-plugin.json found in current directory")
|
|
27
|
+
return JSON.parse(fs.readFileSync(p, "utf8"))
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function writeManifest(cwd, manifest) {
|
|
31
|
+
fs.writeFileSync(path.join(cwd, "palette-plugin.json"), JSON.stringify(manifest, null, 2) + "\n")
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function provider(slug) {
|
|
35
|
+
return CATALOG.providers.find((p) => p.slug === slug) || null
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function declaredTools(spec) {
|
|
39
|
+
const known = provider(spec.provider)
|
|
40
|
+
if (!known) return []
|
|
41
|
+
const asked = (spec.tools || []).filter((t) => known.tools.some((k) => k.name === t))
|
|
42
|
+
if (asked.length) return asked
|
|
43
|
+
return known.tools.filter((t) => !t.mutating).map((t) => t.name)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function valueAfter(argv, flag) {
|
|
47
|
+
const i = argv.indexOf(flag)
|
|
48
|
+
return i >= 0 ? argv[i + 1] : undefined
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// ---- list --------------------------------------------------------------------
|
|
52
|
+
|
|
53
|
+
async function runList(argv, { cwd }) {
|
|
54
|
+
let declared = new Map()
|
|
55
|
+
try {
|
|
56
|
+
for (const spec of readManifest(cwd).connections || []) {
|
|
57
|
+
declared.set(spec.provider, new Set(declaredTools(spec)))
|
|
58
|
+
}
|
|
59
|
+
} catch {
|
|
60
|
+
// Outside an app directory — just print the catalog.
|
|
61
|
+
}
|
|
62
|
+
const verbose = argv.includes("--tools") || argv.includes("-v")
|
|
63
|
+
for (const p of CATALOG.providers) {
|
|
64
|
+
const mine = declared.get(p.slug)
|
|
65
|
+
const mark = mine ? `declared (${mine.size} tool${mine.size === 1 ? "" : "s"})` : "not declared"
|
|
66
|
+
console.log(`\n${p.slug.padEnd(10)} ${p.label.padEnd(22)} ${p.auth_type.padEnd(10)} ${mark}`)
|
|
67
|
+
if (!verbose && !mine) {
|
|
68
|
+
console.log(` ${p.tools.length} tools — pltt connections list --tools to see them`)
|
|
69
|
+
continue
|
|
70
|
+
}
|
|
71
|
+
for (const tool of p.tools) {
|
|
72
|
+
const flag = mine?.has(tool.name) ? "✓" : " "
|
|
73
|
+
const write = tool.mutating ? " [write]" : ""
|
|
74
|
+
console.log(` ${flag} ${tool.name}${write}`)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
console.log("\nWrite tools stage for approval unless an org admin allows them for your app.")
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// ---- add ---------------------------------------------------------------------
|
|
81
|
+
|
|
82
|
+
async function runAdd(argv, { cwd }) {
|
|
83
|
+
const slug = argv.find((a) => !a.startsWith("-"))
|
|
84
|
+
const known = provider(slug)
|
|
85
|
+
if (!known) {
|
|
86
|
+
console.error(
|
|
87
|
+
`[pltt] usage: pltt connections add <${CATALOG.providers.map((p) => p.slug).join("|")}> [--tools a,b]`,
|
|
88
|
+
)
|
|
89
|
+
process.exit(1)
|
|
90
|
+
}
|
|
91
|
+
const requested = (valueAfter(argv, "--tools") || "")
|
|
92
|
+
.split(",")
|
|
93
|
+
.map((t) => t.trim())
|
|
94
|
+
.filter(Boolean)
|
|
95
|
+
const unknown = requested.filter((t) => !known.tools.some((k) => k.name === t))
|
|
96
|
+
if (unknown.length) {
|
|
97
|
+
console.error(`[pltt] unknown ${slug} tool(s): ${unknown.join(", ")}`)
|
|
98
|
+
console.error(`[pltt] see: pltt connections list --tools`)
|
|
99
|
+
process.exit(1)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const manifest = readManifest(cwd)
|
|
103
|
+
manifest.connections = manifest.connections || []
|
|
104
|
+
// The platform resolves a grant by provider, so one entry per provider.
|
|
105
|
+
let spec = manifest.connections.find((c) => c && c.provider === slug)
|
|
106
|
+
if (!spec) {
|
|
107
|
+
spec = { id: slug, provider: slug, label: known.label, auth: known.auth_type === "bot_token" ? "api_key" : "oauth2" }
|
|
108
|
+
manifest.connections.push(spec)
|
|
109
|
+
}
|
|
110
|
+
if (requested.length) {
|
|
111
|
+
spec.tools = Array.from(new Set([...(spec.tools || []), ...requested]))
|
|
112
|
+
}
|
|
113
|
+
writeManifest(cwd, manifest)
|
|
114
|
+
const effective = declaredTools(spec)
|
|
115
|
+
console.log(`[pltt] ${slug}: ${effective.length} tool(s) declared`)
|
|
116
|
+
if (!spec.tools) console.log(`[pltt] no --tools given — defaults to every read-only ${slug} tool`)
|
|
117
|
+
console.log(`[pltt] next: pltt connections pull`)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// ---- pull --------------------------------------------------------------------
|
|
121
|
+
|
|
122
|
+
function tsType(schemaType) {
|
|
123
|
+
return { integer: "number", number: "number", boolean: "boolean", array: "unknown[]", object: "Record<string, unknown>" }[schemaType] || "string"
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function camel(value) {
|
|
127
|
+
const [head, ...rest] = value.split("_")
|
|
128
|
+
return head + rest.map((p) => p[0].toUpperCase() + p.slice(1)).join("")
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function renderStubs(specs) {
|
|
132
|
+
const lines = [
|
|
133
|
+
"// GENERATED by `pltt connections pull` — do not edit by hand.",
|
|
134
|
+
"//",
|
|
135
|
+
"// Only the tools your palette-plugin.json declares. The platform holds the",
|
|
136
|
+
"// OAuth token and enforces scopes, write approval and audit on every call.",
|
|
137
|
+
"",
|
|
138
|
+
'import { palette } from "@palettelab/sdk"',
|
|
139
|
+
"",
|
|
140
|
+
]
|
|
141
|
+
for (const spec of specs) {
|
|
142
|
+
const known = provider(spec.provider)
|
|
143
|
+
const allowed = new Set(declaredTools(spec))
|
|
144
|
+
for (const tool of known.tools.filter((t) => allowed.has(t.name))) {
|
|
145
|
+
const props = Object.entries(tool.input_schema?.properties || {})
|
|
146
|
+
const required = new Set(tool.input_schema?.required || [])
|
|
147
|
+
const fields = props
|
|
148
|
+
.sort(([a], [b]) => (required.has(b) ? 1 : 0) - (required.has(a) ? 1 : 0) || a.localeCompare(b))
|
|
149
|
+
.map(([name, s]) => `${camel(name)}${required.has(name) ? "" : "?"}: ${tsType(s.type)}`)
|
|
150
|
+
const argMap = props.map(([name]) => `"${name}": args.${camel(name)}`).join(", ")
|
|
151
|
+
const fn = camel(`${tool.namespace}_${tool.method}`)
|
|
152
|
+
lines.push(
|
|
153
|
+
`/** ${tool.description.replace(/\*\//g, "*\\/")}${tool.mutating ? " [write — stages for approval]" : ""} */`,
|
|
154
|
+
`export function ${fn}(${fields.length ? `args: { ${fields.join("; ")} }` : ""}): Promise<unknown> {`,
|
|
155
|
+
` return palette.connections.call("${spec.provider}", "${tool.name}", ${fields.length ? `{ ${argMap} }` : "{}"})`,
|
|
156
|
+
"}",
|
|
157
|
+
"",
|
|
158
|
+
)
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return lines.join("\n")
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async function runPull(_argv, { cwd }) {
|
|
165
|
+
const specs = (readManifest(cwd).connections || []).filter((c) => provider(c.provider))
|
|
166
|
+
if (!specs.length) {
|
|
167
|
+
console.log("[pltt] no live connections declared — pltt connections add <provider>")
|
|
168
|
+
return
|
|
169
|
+
}
|
|
170
|
+
const outDir = path.join(cwd, ".palette", "types")
|
|
171
|
+
fs.mkdirSync(outDir, { recursive: true })
|
|
172
|
+
const outFile = path.join(outDir, "connections.ts")
|
|
173
|
+
fs.writeFileSync(outFile, renderStubs(specs))
|
|
174
|
+
const count = specs.reduce((n, s) => n + declaredTools(s).length, 0)
|
|
175
|
+
console.log(`[pltt] wrote ${count} typed helper(s) -> ${path.relative(cwd, outFile)}`)
|
|
176
|
+
console.log("[pltt] python: ctx.connections.<namespace>.<tool>(...) is already typed by palette-sdk")
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// ---- mock --------------------------------------------------------------------
|
|
180
|
+
|
|
181
|
+
async function runMock(argv, { cwd }) {
|
|
182
|
+
const target = argv.find((a) => !a.startsWith("-"))
|
|
183
|
+
const [slug, ...rest] = (target || "").split(".")
|
|
184
|
+
const toolName = rest.join(".")
|
|
185
|
+
const known = provider(slug)
|
|
186
|
+
const tool = known?.tools.find((t) => t.name === toolName || t.method === toolName)
|
|
187
|
+
if (!tool) {
|
|
188
|
+
console.error("[pltt] usage: pltt connections mock <provider>.<tool> e.g. google.gmail.search")
|
|
189
|
+
process.exit(1)
|
|
190
|
+
}
|
|
191
|
+
let result = { ok: true, mocked: tool.name }
|
|
192
|
+
const raw = valueAfter(argv, "--result")
|
|
193
|
+
if (raw) {
|
|
194
|
+
try {
|
|
195
|
+
result = JSON.parse(raw)
|
|
196
|
+
} catch (err) {
|
|
197
|
+
console.error(`[pltt] --result must be JSON: ${err.message}`)
|
|
198
|
+
process.exit(1)
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const mockPath = path.join(cwd, ".palette", "connections.local.json")
|
|
203
|
+
fs.mkdirSync(path.dirname(mockPath), { recursive: true })
|
|
204
|
+
const current = fs.existsSync(mockPath) ? JSON.parse(fs.readFileSync(mockPath, "utf8")) : {}
|
|
205
|
+
// Keyed by the manifest connection id so it merges with the status overrides
|
|
206
|
+
// the dev simulator already reads.
|
|
207
|
+
const spec = (readManifest(cwd).connections || []).find((c) => c.provider === slug)
|
|
208
|
+
const key = spec?.id || slug
|
|
209
|
+
current[key] = { ...(current[key] || {}), provider: slug, status: "connected" }
|
|
210
|
+
current[key].tools = { ...(current[key].tools || {}), [tool.name]: result }
|
|
211
|
+
fs.writeFileSync(mockPath, JSON.stringify(current, null, 2) + "\n")
|
|
212
|
+
console.log(`[pltt] mocked ${slug}.${tool.name} -> ${path.relative(cwd, mockPath)}`)
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// ---- entry -------------------------------------------------------------------
|
|
216
|
+
|
|
217
|
+
module.exports = async function connections(argv, ctx) {
|
|
218
|
+
const [sub, ...rest] = argv
|
|
219
|
+
if (sub === "list") return runList(rest, ctx)
|
|
220
|
+
if (sub === "add") return runAdd(rest, ctx)
|
|
221
|
+
if (sub === "pull") return runPull(rest, ctx)
|
|
222
|
+
if (sub === "mock") return runMock(rest, ctx)
|
|
223
|
+
console.log("pltt connections <command>\n")
|
|
224
|
+
console.log(" list Show OS providers + tools, and which your app declares.")
|
|
225
|
+
console.log(" add Declare a provider (and tools) in palette-plugin.json.")
|
|
226
|
+
console.log(" pull Generate typed helpers for the tools you declared.")
|
|
227
|
+
console.log(" mock Seed a local response for pltt dev.")
|
|
228
|
+
console.log("\nYour app never does its own OAuth: the user connects once in Palette")
|
|
229
|
+
console.log("Connect, grants your app, and the platform proxies every call.")
|
|
230
|
+
if (sub && !["help", "--help", "-h"].includes(sub)) {
|
|
231
|
+
console.error(`\n[pltt] unknown subcommand: ${sub}`)
|
|
232
|
+
process.exit(1)
|
|
233
|
+
}
|
|
234
|
+
}
|
package/lib/commands/dev.js
CHANGED
|
@@ -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
|
|
package/lib/commands/doctor.js
CHANGED
|
@@ -28,6 +28,89 @@ 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
|
+
media: (cwd) =>
|
|
38
|
+
process.env.FAL_KEY || localEnvHas(cwd, "FAL_KEY")
|
|
39
|
+
? "pltt dev: live via your FAL_KEY"
|
|
40
|
+
: "pltt dev: stubbed — set FAL_KEY for real renders (hosted uses the platform key)",
|
|
41
|
+
web: () => "pltt dev: keyless DuckDuckGo (hosted uses the platform search key)",
|
|
42
|
+
redis: () => "pltt dev: in-memory",
|
|
43
|
+
vector: () => "pltt dev: in-memory hash embeddings",
|
|
44
|
+
storage: () => "pltt dev: .palette/dev-storage",
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function localEnvHas(cwd, name) {
|
|
48
|
+
try {
|
|
49
|
+
return Boolean(loadLocalEnvDetails(cwd, { apply: false }).values[name])
|
|
50
|
+
} catch {
|
|
51
|
+
return false
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function reportPlatformServices(manifest, cwd) {
|
|
56
|
+
const declared = manifest.platform_services
|
|
57
|
+
const names = Array.isArray(declared) ? declared : Object.keys(declared || {})
|
|
58
|
+
const { PLATFORM_SERVICES } = require("../manifest")
|
|
59
|
+
if (!names.length) {
|
|
60
|
+
ok("no platform services declared")
|
|
61
|
+
console.log(` available: ${PLATFORM_SERVICES.map((s) => s.name).join(", ")} — pltt platform list`)
|
|
62
|
+
} else {
|
|
63
|
+
ok(`manifest declares ${names.length} platform service(s): ${names.join(", ")}`)
|
|
64
|
+
for (const name of names) {
|
|
65
|
+
const note = PLATFORM_SERVICE_DEV_NOTES[name]
|
|
66
|
+
if (note) console.log(` ctx.${name} — ${note(cwd)}`)
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const { INERT_PERMISSIONS } = require("../manifest")
|
|
71
|
+
const inert = (manifest.permissions || []).filter((permission) => INERT_PERMISSIONS.has(permission))
|
|
72
|
+
if (inert.length) {
|
|
73
|
+
warn(
|
|
74
|
+
`permissions grant nothing today: ${inert.join(", ")}`,
|
|
75
|
+
"No ctx.* client is behind them, so an app cannot act on them and a reviewer should not read them as capability. Drop them from the manifest.",
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const conns = manifest.connections || []
|
|
80
|
+
if (!conns.length) {
|
|
81
|
+
ok("no service connections declared")
|
|
82
|
+
return
|
|
83
|
+
}
|
|
84
|
+
const catalog = require("../connection-catalog.json")
|
|
85
|
+
const mockPath = path.join(cwd, ".palette", "connections.local.json")
|
|
86
|
+
let mocks = {}
|
|
87
|
+
try {
|
|
88
|
+
mocks = fs.existsSync(mockPath) ? JSON.parse(fs.readFileSync(mockPath, "utf8")) : {}
|
|
89
|
+
} catch {
|
|
90
|
+
mocks = {}
|
|
91
|
+
}
|
|
92
|
+
for (const spec of conns) {
|
|
93
|
+
const known = catalog.providers.find((p) => p.slug === spec.provider)
|
|
94
|
+
if (!known) {
|
|
95
|
+
warn(
|
|
96
|
+
`connection '${spec.id}' uses legacy provider '${spec.provider}'`,
|
|
97
|
+
"Legacy providers have no OS connection — the app must supply its own credentials.",
|
|
98
|
+
)
|
|
99
|
+
continue
|
|
100
|
+
}
|
|
101
|
+
const asked = (spec.tools || []).length
|
|
102
|
+
const label = asked ? `${asked} tool(s)` : `every read-only ${spec.provider} tool`
|
|
103
|
+
ok(`connection '${spec.id}' → ${known.label}, ${label}`)
|
|
104
|
+
const mocked = Object.keys(mocks[spec.id]?.tools || {}).length
|
|
105
|
+
if (!mocked) {
|
|
106
|
+
warn(
|
|
107
|
+
`no local responses mocked for ${spec.provider}`,
|
|
108
|
+
`pltt connections mock ${spec.provider}.<tool> — otherwise pltt dev raises on the call.`,
|
|
109
|
+
)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
31
114
|
function nodeMajor() {
|
|
32
115
|
return Number(process.versions.node.split(".")[0])
|
|
33
116
|
}
|
|
@@ -121,6 +204,8 @@ async function run(args, { cwd }) {
|
|
|
121
204
|
failures += checkEntry(cwd, `tool[${tool.name}]`, tool.entry)
|
|
122
205
|
}
|
|
123
206
|
|
|
207
|
+
reportPlatformServices(manifest, cwd)
|
|
208
|
+
|
|
124
209
|
const secrets = declaredSecrets(manifest)
|
|
125
210
|
const localEnv = loadLocalEnvDetails(cwd, { apply: false })
|
|
126
211
|
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
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
|
|
3
|
+
const fs = require("fs")
|
|
4
|
+
const path = require("path")
|
|
5
|
+
|
|
6
|
+
const { CTX_PERMISSIONS, PLATFORM_SERVICES, loadManifest } = require("../manifest")
|
|
7
|
+
const { loadLocalEnvDetails } = require("../secrets")
|
|
8
|
+
|
|
9
|
+
// Local key that makes a service call for real under `pltt dev` instead of
|
|
10
|
+
// returning a stub. Absent = the app still runs, it just gets placeholders.
|
|
11
|
+
const DEV_KEYS = { llm: "OPENAI_API_KEY", media: "FAL_KEY" }
|
|
12
|
+
|
|
13
|
+
function declaredNames(manifest) {
|
|
14
|
+
const declared = manifest.platform_services
|
|
15
|
+
return Array.isArray(declared) ? declared.slice() : Object.keys(declared || {})
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function devNote(name, cwd) {
|
|
19
|
+
const key = DEV_KEYS[name]
|
|
20
|
+
if (!key) return "pltt dev: emulated locally"
|
|
21
|
+
let present = Boolean(process.env[key])
|
|
22
|
+
if (!present) {
|
|
23
|
+
try {
|
|
24
|
+
present = Boolean(loadLocalEnvDetails(cwd, { apply: false }).values[key])
|
|
25
|
+
} catch {
|
|
26
|
+
present = false
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return present
|
|
30
|
+
? `pltt dev: live via your ${key}`
|
|
31
|
+
: `pltt dev: stubbed — set ${key} in .env.local for real calls`
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function list(manifest, cwd, json) {
|
|
35
|
+
const on = new Set(declaredNames(manifest))
|
|
36
|
+
const granted = new Set(manifest.permissions || [])
|
|
37
|
+
const rows = PLATFORM_SERVICES.map((service) => ({
|
|
38
|
+
...service,
|
|
39
|
+
declared: on.has(service.name),
|
|
40
|
+
dev: devNote(service.name, cwd),
|
|
41
|
+
}))
|
|
42
|
+
// The other half of what `ctx` can do: clients switched on by a manifest
|
|
43
|
+
// permission rather than by platform_services. Printing them together is the
|
|
44
|
+
// only way "what can ctx do, and what do I declare to get it" is one answer.
|
|
45
|
+
const gated = CTX_PERMISSIONS.map((entry) => ({
|
|
46
|
+
...entry,
|
|
47
|
+
declared: entry.permissions.some((permission) => granted.has(permission)),
|
|
48
|
+
missing: entry.permissions.filter((permission) => !granted.has(permission)),
|
|
49
|
+
}))
|
|
50
|
+
if (json) {
|
|
51
|
+
console.log(JSON.stringify({ app_id: manifest.id, services: rows, permissions: gated }, null, 2))
|
|
52
|
+
return
|
|
53
|
+
}
|
|
54
|
+
console.log(`Platform services for ${manifest.id}@${manifest.version}`)
|
|
55
|
+
console.log("Declare one and the platform's own credential is used — your app ships no key.\n")
|
|
56
|
+
for (const row of rows) {
|
|
57
|
+
console.log(` ${row.declared ? "[on] " : "[ ] "}${row.name.padEnd(8)} ${row.accessor}`)
|
|
58
|
+
console.log(` ${row.summary}`)
|
|
59
|
+
console.log(` instead of: ${row.instead_of}`)
|
|
60
|
+
if (row.python) console.log(` ${row.python}`)
|
|
61
|
+
console.log(` ${row.dev}`)
|
|
62
|
+
}
|
|
63
|
+
console.log("\nPermission-gated clients — add the permission to palette-plugin.json:\n")
|
|
64
|
+
for (const entry of gated) {
|
|
65
|
+
console.log(` ${entry.declared ? "[on] " : "[ ] "}${entry.accessor}`)
|
|
66
|
+
console.log(` ${entry.summary}`)
|
|
67
|
+
console.log(` permissions: ${entry.permissions.join(", ")}`)
|
|
68
|
+
if (entry.python) console.log(` ${entry.python}`)
|
|
69
|
+
}
|
|
70
|
+
const off = rows.filter((row) => !row.declared).map((row) => row.name)
|
|
71
|
+
if (off.length) console.log(`\n pltt platform enable ${off[0]}`)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function writeManifest(cwd, mutate) {
|
|
75
|
+
const file = path.join(cwd, "palette-plugin.json")
|
|
76
|
+
const raw = JSON.parse(fs.readFileSync(file, "utf8"))
|
|
77
|
+
mutate(raw)
|
|
78
|
+
fs.writeFileSync(file, JSON.stringify(raw, null, 2) + "\n")
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function assertKnown(names) {
|
|
82
|
+
const known = new Set(PLATFORM_SERVICES.map((service) => service.name))
|
|
83
|
+
const bad = names.filter((name) => !known.has(name))
|
|
84
|
+
if (!names.length || bad.length) {
|
|
85
|
+
throw new Error(
|
|
86
|
+
`usage: pltt platform <enable|disable> <${Array.from(known).join("|")}>` +
|
|
87
|
+
(bad.length ? ` (unknown: ${bad.join(", ")})` : ""),
|
|
88
|
+
)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function enable(cwd, names) {
|
|
93
|
+
assertKnown(names)
|
|
94
|
+
writeManifest(cwd, (manifest) => {
|
|
95
|
+
const current = manifest.platform_services
|
|
96
|
+
if (current && !Array.isArray(current)) {
|
|
97
|
+
// Object form carries per-service billing/required settings — keep it.
|
|
98
|
+
for (const name of names) if (!current[name]) current[name] = {}
|
|
99
|
+
return
|
|
100
|
+
}
|
|
101
|
+
const next = new Set([...(current || []), ...names])
|
|
102
|
+
manifest.platform_services = Array.from(next).sort()
|
|
103
|
+
})
|
|
104
|
+
for (const name of names) {
|
|
105
|
+
const service = PLATFORM_SERVICES.find((entry) => entry.name === name)
|
|
106
|
+
console.log(`[pltt] ${service.accessor} enabled — ${service.summary}`)
|
|
107
|
+
if (service.python) console.log(` ${service.python}`)
|
|
108
|
+
const key = DEV_KEYS[name]
|
|
109
|
+
if (key) console.log(` pltt dev stubs this unless ${key} is in .env.local`)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function disable(cwd, names) {
|
|
114
|
+
assertKnown(names)
|
|
115
|
+
writeManifest(cwd, (manifest) => {
|
|
116
|
+
const current = manifest.platform_services
|
|
117
|
+
if (current && !Array.isArray(current)) {
|
|
118
|
+
for (const name of names) delete current[name]
|
|
119
|
+
return
|
|
120
|
+
}
|
|
121
|
+
manifest.platform_services = (current || []).filter((name) => !names.includes(name))
|
|
122
|
+
})
|
|
123
|
+
console.log(`[pltt] removed: ${names.join(", ")}`)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
async function run(args, { cwd }) {
|
|
127
|
+
const manifest = loadManifest(cwd)
|
|
128
|
+
const positional = args.filter((arg) => !arg.startsWith("-"))
|
|
129
|
+
const command = positional[0] || "list"
|
|
130
|
+
const names = positional.slice(1)
|
|
131
|
+
if (command === "list") return list(manifest, cwd, args.includes("--json"))
|
|
132
|
+
if (command === "enable" || command === "add") return enable(cwd, names)
|
|
133
|
+
if (command === "disable" || command === "remove") return disable(cwd, names)
|
|
134
|
+
throw new Error("usage: pltt platform [list | enable <service...> | disable <service...>] [--json]")
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
module.exports = run
|
package/lib/commands/test.js
CHANGED
|
@@ -3,11 +3,51 @@
|
|
|
3
3
|
const fs = require("fs")
|
|
4
4
|
const path = require("path")
|
|
5
5
|
const { spawnSync } = require("child_process")
|
|
6
|
-
const { loadManifest, validateManifest, KNOWN_PERMISSIONS } = require("../manifest")
|
|
6
|
+
const { CTX_PERMISSIONS, loadManifest, validateManifest, KNOWN_PERMISSIONS } = require("../manifest")
|
|
7
7
|
const { bundleFrontend, bundleBackend } = require("../bundler")
|
|
8
8
|
const { declaredSecrets, loadLocalEnv, loadLocalEnvDetails } = require("../secrets")
|
|
9
9
|
const buildCommand = require("./build")
|
|
10
10
|
|
|
11
|
+
// Permissions consumed by a `ctx.*` client in a handler body rather than by a
|
|
12
|
+
// route's require_permission() dependency. The route probe cannot see those —
|
|
13
|
+
// reporting them as unused pushed authors to delete permissions their app
|
|
14
|
+
// genuinely needs, which then fails at runtime with a PermissionError.
|
|
15
|
+
function permissionsUsedByCtxClients(entryPath) {
|
|
16
|
+
const root = path.dirname(path.resolve(entryPath))
|
|
17
|
+
const sources = []
|
|
18
|
+
const walk = (dir, depth = 0) => {
|
|
19
|
+
if (depth > 6) return
|
|
20
|
+
let entries = []
|
|
21
|
+
try {
|
|
22
|
+
entries = fs.readdirSync(dir, { withFileTypes: true })
|
|
23
|
+
} catch {
|
|
24
|
+
return
|
|
25
|
+
}
|
|
26
|
+
for (const entry of entries) {
|
|
27
|
+
if (["__pycache__", ".venv", "node_modules", ".palette"].includes(entry.name)) continue
|
|
28
|
+
const abs = path.join(dir, entry.name)
|
|
29
|
+
if (entry.isDirectory()) walk(abs, depth + 1)
|
|
30
|
+
else if (entry.isFile() && entry.name.endsWith(".py")) {
|
|
31
|
+
try {
|
|
32
|
+
sources.push(fs.readFileSync(abs, "utf8"))
|
|
33
|
+
} catch {
|
|
34
|
+
/* unreadable file: fall through, the warning is advisory */
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
walk(root)
|
|
40
|
+
const blob = sources.join("\n")
|
|
41
|
+
const used = new Set()
|
|
42
|
+
for (const entry of CTX_PERMISSIONS) {
|
|
43
|
+
const accessor = entry.accessor.replace(/^ctx\./, "")
|
|
44
|
+
if (new RegExp(`\\bctx\\.${accessor}\\b`).test(blob)) {
|
|
45
|
+
for (const permission of entry.permissions) used.add(permission)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return used
|
|
49
|
+
}
|
|
50
|
+
|
|
11
51
|
const DEFAULT_FRONTEND_BUNDLE_LIMIT = 50 * 1024 * 1024
|
|
12
52
|
const DEFAULT_BACKEND_BUNDLE_LIMIT = 50 * 1024 * 1024
|
|
13
53
|
|
|
@@ -241,11 +281,13 @@ function checkBackendContract(cwd, manifest, out, pythonInfo) {
|
|
|
241
281
|
out.ok("route permissions are declared in manifest")
|
|
242
282
|
}
|
|
243
283
|
|
|
244
|
-
|
|
284
|
+
const ctxUsed = permissionsUsedByCtxClients(abs)
|
|
285
|
+
const trulyUnused = (report.unused_declared_permissions || []).filter((p) => !ctxUsed.has(p))
|
|
286
|
+
if (trulyUnused.length) {
|
|
245
287
|
out.warn(
|
|
246
|
-
`manifest declares unused backend permissions: ${
|
|
288
|
+
`manifest declares unused backend permissions: ${trulyUnused.join(", ")}`,
|
|
247
289
|
"Remove unused permissions unless the frontend uses them directly through the SDK.",
|
|
248
|
-
{ permissions:
|
|
290
|
+
{ permissions: trulyUnused },
|
|
249
291
|
)
|
|
250
292
|
}
|
|
251
293
|
const manifestCapabilities = []
|
|
@@ -936,3 +978,4 @@ async function run(args, { cwd }) {
|
|
|
936
978
|
}
|
|
937
979
|
|
|
938
980
|
module.exports = run
|
|
981
|
+
module.exports.permissionsUsedByCtxClients = permissionsUsedByCtxClients
|