@palettelab/cli 0.3.69 → 0.3.71
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 +105 -1
- package/backend-sdk/palette_sdk/__init__.py +3 -2
- package/backend-sdk/palette_sdk/data_rooms.py +12 -0
- package/backend-sdk/palette_sdk/jobs.py +18 -0
- package/backend-sdk/pyproject.toml +1 -1
- package/docs/python-backend-sdk.md +5 -0
- package/lib/acp.js +267 -0
- package/lib/agent-client.js +176 -0
- package/lib/cli.js +8 -1
- package/lib/commands/agent.js +763 -0
- package/lib/commands/check.js +106 -0
- package/package.json +1 -1
- package/template-fallback/package.json +1 -1
- package/template-fallback/templates/consumer-app/package.json +1 -1
- package/template-fallback/templates/dashboard/package.json +1 -1
- package/template-fallback/templates/database/package.json +1 -1
- package/template-fallback/templates/external-service/package.json +1 -1
- package/template-fallback/templates/frontend-only/package.json +1 -1
- package/template-fallback/templates/next/package.json +1 -1
- package/template-fallback/templates/palette-app/package.json +1 -1
- package/template-fallback/templates/provider-app/package.json +1 -1
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"use strict"
|
|
2
|
+
|
|
3
|
+
// The hosted Build Agent API (/api/v1/build-sessions on the selected environment, with its publish token): one
|
|
4
|
+
// method per endpoint, for `pltt agent` and its ACP server. Plus the two steps every change goes through: find the
|
|
5
|
+
// session that carries it out (locate) and follow that session until it is idle again (untilIdle).
|
|
6
|
+
|
|
7
|
+
const crypto = require("crypto")
|
|
8
|
+
const { setTimeout: sleep } = require("timers/promises")
|
|
9
|
+
const { resolveEnvironment } = require("./environments")
|
|
10
|
+
|
|
11
|
+
const q = encodeURIComponent
|
|
12
|
+
|
|
13
|
+
// A prompt's client-minted id: a retried request admits the message once.
|
|
14
|
+
function mintId() {
|
|
15
|
+
return `cli_${crypto.randomBytes(12).toString("hex")}`
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
class AgentClient {
|
|
19
|
+
constructor(env) {
|
|
20
|
+
this.env = env
|
|
21
|
+
this.base = `${env.url}/api/v1/build-sessions`
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// JSON over fetch. `retries` resends a request that failed in transit or with a 5xx: only for idempotent requests.
|
|
25
|
+
async request(method, route, body, { retries = 0 } = {}) {
|
|
26
|
+
const url = this.base + route
|
|
27
|
+
const init = {
|
|
28
|
+
method,
|
|
29
|
+
headers: { Authorization: `Bearer ${this.env.token}`, ...(body ? { "Content-Type": "application/json" } : {}) },
|
|
30
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
31
|
+
}
|
|
32
|
+
for (let attempt = 0; ; attempt++) {
|
|
33
|
+
const res = await fetch(url, init).catch((err) => {
|
|
34
|
+
if (attempt >= retries) throw err
|
|
35
|
+
return null
|
|
36
|
+
})
|
|
37
|
+
if (res && res.ok) return res.json()
|
|
38
|
+
if (res && (res.status < 500 || attempt >= retries)) throw new Error(`${method} ${url} → ${res.status}: ${await res.text()}`)
|
|
39
|
+
await sleep(300 * (attempt + 1))
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Server-sent events over fetch: data-only frames, comment lines are heartbeats.
|
|
44
|
+
async *events(route, { signal } = {}) {
|
|
45
|
+
const url = this.base + route
|
|
46
|
+
const res = await fetch(url, { headers: { Authorization: `Bearer ${this.env.token}`, Accept: "text/event-stream" }, signal })
|
|
47
|
+
if (!res.ok) throw new Error(`GET ${url} → ${res.status}: ${await res.text()}`)
|
|
48
|
+
const decoder = new TextDecoder()
|
|
49
|
+
let buffer = ""
|
|
50
|
+
for await (const chunk of res.body) {
|
|
51
|
+
buffer += decoder.decode(chunk, { stream: true })
|
|
52
|
+
let end
|
|
53
|
+
while ((end = buffer.indexOf("\n\n")) >= 0) {
|
|
54
|
+
const frame = buffer.slice(0, end)
|
|
55
|
+
buffer = buffer.slice(end + 2)
|
|
56
|
+
const data = frame.split("\n").filter((line) => line.startsWith("data:")).map((line) => line.slice(5).trim()).join("\n")
|
|
57
|
+
if (data) yield JSON.parse(data)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
sessions(app, { limit, archived } = {}) {
|
|
63
|
+
return this.request("GET", `?app_id=${q(app)}${limit ? `&limit=${limit}` : ""}${archived ? "&archived=true" : ""}`)
|
|
64
|
+
}
|
|
65
|
+
agents() { return this.request("GET", "/agents") }
|
|
66
|
+
commands() { return this.request("GET", "/commands") }
|
|
67
|
+
contract() { return this.request("GET", "/contract/agents-md") }
|
|
68
|
+
session(id) { return this.request("GET", `/${q(id)}`) }
|
|
69
|
+
// A retry of the same call carries the same id, so the server admits the message once.
|
|
70
|
+
prompt(id, text, { delivery = "steer", messageId = mintId() } = {}) {
|
|
71
|
+
return this.request("POST", `/${q(id)}/prompt`, { text, delivery, id: messageId }, { retries: 2 })
|
|
72
|
+
}
|
|
73
|
+
edit(app, text) { return this.request("POST", `/apps/${q(app)}/edit`, { text }) }
|
|
74
|
+
wait(id, timeout = 60) { return this.request("GET", `/${q(id)}/wait?timeout=${timeout}`) }
|
|
75
|
+
interrupt(id) { return this.request("POST", `/${q(id)}/interrupt`) }
|
|
76
|
+
compact(id) { return this.request("POST", `/${q(id)}/compact`) }
|
|
77
|
+
revert(id, step) { return this.request("POST", `/${q(id)}/revert`, { step }) }
|
|
78
|
+
unrevert(id) { return this.request("POST", `/${q(id)}/unrevert`) }
|
|
79
|
+
diff(id) { return this.request("GET", `/${q(id)}/diff`) }
|
|
80
|
+
context(id) { return this.request("GET", `/${q(id)}/context`) }
|
|
81
|
+
children(id) { return this.request("GET", `/${q(id)}/children`) }
|
|
82
|
+
inbox(id) { return this.request("GET", `/${q(id)}/inbox`) }
|
|
83
|
+
deliver(id, item, delivery) { return this.request("PATCH", `/${q(id)}/inbox/${q(item)}`, { delivery }) }
|
|
84
|
+
cancel(id, item) { return this.request("DELETE", `/${q(id)}/inbox/${q(item)}`) }
|
|
85
|
+
fork(id, { seq, title } = {}) { return this.request("POST", `/${q(id)}/fork`, { seq, title }) }
|
|
86
|
+
exportSession(id, { sanitize = false } = {}) { return this.request("GET", `/${q(id)}/export?sanitize=${sanitize}`) }
|
|
87
|
+
importSession(app, document) { return this.request("POST", "/import", { app_id: Number(app), document }) }
|
|
88
|
+
update(id, fields) { return this.request("PATCH", `/${q(id)}`, fields) }
|
|
89
|
+
remove(id) { return this.request("DELETE", `/${q(id)}`) }
|
|
90
|
+
attend(id) { return this.request("POST", `/${q(id)}/attend`) }
|
|
91
|
+
permissions(id) { return this.request("GET", `/${q(id)}/permissions`) }
|
|
92
|
+
permit(id, request, reply, message) {
|
|
93
|
+
return this.request("POST", `/${q(id)}/permissions/${q(request)}`, { reply, ...(message ? { message } : {}) })
|
|
94
|
+
}
|
|
95
|
+
questions(id) { return this.request("GET", `/${q(id)}/questions`) }
|
|
96
|
+
artifacts(id) { return this.request("GET", `/${q(id)}/artifacts`) }
|
|
97
|
+
artifact(id, artifact) { return this.request("GET", `/${q(id)}/artifacts/${q(artifact)}`) }
|
|
98
|
+
schedules(id) { return this.request("GET", `/${q(id)}/schedules`) }
|
|
99
|
+
schedule(id, schedule, action) {
|
|
100
|
+
return action === "delete"
|
|
101
|
+
? this.request("DELETE", `/${q(id)}/schedules/${q(schedule)}`)
|
|
102
|
+
: this.request("POST", `/${q(id)}/schedules/${q(schedule)}`, { action })
|
|
103
|
+
}
|
|
104
|
+
move(id, app) { return this.request("POST", `/${q(id)}/move`, { app_id: Number(app) }) }
|
|
105
|
+
skills() { return this.request("GET", "/skills") }
|
|
106
|
+
mcp() { return this.request("GET", "/mcp") }
|
|
107
|
+
models(query = "") { return this.request("GET", `/models${query ? `?q=${q(query)}` : ""}`) }
|
|
108
|
+
config() { return this.request("GET", "/config") }
|
|
109
|
+
plugins() { return this.request("GET", "/plugins") }
|
|
110
|
+
terminals(id) { return this.request("GET", `/${q(id)}/terminals`) }
|
|
111
|
+
openTerminal(id, { cols, rows, title } = {}) { return this.request("POST", `/${q(id)}/terminals`, { cols, rows, title }) }
|
|
112
|
+
readTerminal(id, lines = 100) { return this.request("GET", `/${q(id)}/terminals/read?lines=${lines}`) }
|
|
113
|
+
closeTerminal(id, terminal) { return this.request("DELETE", `/${q(id)}/terminals/${q(terminal)}`) }
|
|
114
|
+
terminalTicket(id, terminal) { return this.request("POST", `/${q(id)}/terminals/${q(terminal)}/ticket`) }
|
|
115
|
+
// The terminal's WebSocket, authorised by a one-use ticket (a WebSocket cannot carry the token header).
|
|
116
|
+
terminalUrl(id, terminal, ticket, after = "0") {
|
|
117
|
+
return `${this.base.replace(/^http/, "ws")}/${q(id)}/terminals/${q(terminal)}/connect?ticket=${q(ticket)}&after=${q(after)}`
|
|
118
|
+
}
|
|
119
|
+
// A raw call under /build-sessions, for scripting what has no subcommand.
|
|
120
|
+
raw(method, route, body) { return this.request(method.toUpperCase(), route.startsWith("/") || route.startsWith("?") ? route : `/${route}`, body) }
|
|
121
|
+
answer(id, question, answer) { return this.request("POST", `/${q(id)}/questions/${q(question)}`, { answer }) }
|
|
122
|
+
log(id, { after = 0, follow = true, signal } = {}) {
|
|
123
|
+
return this.events(`/${q(id)}/log?after=${after}&follow=${follow}`, { signal })
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function connect(cwd, flags) {
|
|
128
|
+
const env = resolveEnvironment({ cwd, flags })
|
|
129
|
+
if (!env.token) throw new Error(`no token for "${env.name}" — run pltt login or set $${env.token_env}`)
|
|
130
|
+
return new AgentClient(env)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// The root session that carries out a change just sent (`result` of an edit or a prompt) and the seq its new events
|
|
134
|
+
// start after. `before` lists the app's sessions from just before it was sent: a queued change, and an edit that
|
|
135
|
+
// started a build, run in a session that is not among them yet.
|
|
136
|
+
async function locate(client, app, result, before, signal) {
|
|
137
|
+
const seq = (id) => (before.find((s) => s.id === id) || {}).last_seq || 0
|
|
138
|
+
const target = result.status !== "queued" && result.session_id
|
|
139
|
+
if (target) {
|
|
140
|
+
const root = (before.find((s) => s.id === target) || {}).parent_id || target
|
|
141
|
+
return { session: root, after: seq(root) }
|
|
142
|
+
}
|
|
143
|
+
const seen = new Set(before.map((s) => s.id))
|
|
144
|
+
for (;;) {
|
|
145
|
+
const { sessions } = await client.sessions(app, { limit: 20 })
|
|
146
|
+
const found = sessions.find((s) => !s.parent_id && (result.status === "steered" || !seen.has(s.id)))
|
|
147
|
+
if (found) return { session: found.id, after: seq(found.id) }
|
|
148
|
+
await sleep(2000, undefined, { signal })
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Follow a session's log from `after` until a round of it ends and GET wait finds the session idle (an edit runs a
|
|
153
|
+
// plan round, then its build round, in the same session); a dropped stream reconnects. Resolves with wait's answer.
|
|
154
|
+
async function untilIdle(client, id, { after = 0, signal, onEvent }) {
|
|
155
|
+
for (;;) {
|
|
156
|
+
let ended = false
|
|
157
|
+
for await (const event of client.log(id, { after, follow: true, signal })) {
|
|
158
|
+
if (event.type === "log.synced") continue
|
|
159
|
+
after = event.seq ?? after
|
|
160
|
+
await onEvent(event)
|
|
161
|
+
if (event.type === "round.ended" && (event.data || {}).status !== "busy") {
|
|
162
|
+
ended = true
|
|
163
|
+
break
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
// ponytail: a round that starts after wait read the session idle (the gap between an edit's rounds) ends this
|
|
167
|
+
// early; an "edit finished" event from the server would close that gap.
|
|
168
|
+
if (!ended) await sleep(1000, undefined, { signal })
|
|
169
|
+
else {
|
|
170
|
+
const state = await client.wait(id, 1)
|
|
171
|
+
if (state.done) return state
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
module.exports = { AgentClient, connect, locate, untilIdle, mintId }
|
package/lib/cli.js
CHANGED
|
@@ -19,6 +19,8 @@ const capabilities = require("./commands/capabilities")
|
|
|
19
19
|
const connections = require("./commands/connections")
|
|
20
20
|
const platform = require("./commands/platform")
|
|
21
21
|
const models = require("./commands/models")
|
|
22
|
+
const agent = require("./commands/agent")
|
|
23
|
+
const check = require("./commands/check")
|
|
22
24
|
|
|
23
25
|
const COMMANDS = {
|
|
24
26
|
init: { run: init, help: "Scaffold a new plugin directory from the template" },
|
|
@@ -41,6 +43,7 @@ const COMMANDS = {
|
|
|
41
43
|
},
|
|
42
44
|
build: { run: build, help: "Validate palette-plugin.json and check entry points exist" },
|
|
43
45
|
test: { run: test, help: "Run local plugin contract checks before publishing" },
|
|
46
|
+
check: { run: check, help: "The checks of pltt test as diagnostics (id, file, line, fix) for agents and editors" },
|
|
44
47
|
package: { run: pkg, help: "Bundle the plugin into a tar.gz under dist/ (no upload)" },
|
|
45
48
|
publish: {
|
|
46
49
|
run: publish,
|
|
@@ -86,6 +89,10 @@ const COMMANDS = {
|
|
|
86
89
|
run: capabilities,
|
|
87
90
|
help: "Add, list, or inspect agent-callable app capabilities",
|
|
88
91
|
},
|
|
92
|
+
agent: {
|
|
93
|
+
run: agent,
|
|
94
|
+
help: "Drive the hosted Build Agent: run a change to its end, attach, steer, diff, fork, export, ACP server (pltt agent help)",
|
|
95
|
+
},
|
|
89
96
|
version: { run: version, help: "Show the installed pltt CLI version" },
|
|
90
97
|
update: { run: update, help: "Update the globally installed pltt CLI" },
|
|
91
98
|
}
|
|
@@ -98,7 +105,7 @@ function printHelp() {
|
|
|
98
105
|
console.log(` ${name.padEnd(12)} ${help}`)
|
|
99
106
|
}
|
|
100
107
|
console.log("\nGlobal flags:")
|
|
101
|
-
console.log(" --json Emit machine-readable JSON output (status, logs, package, publish, test, update, version, login, dev --sandbox, platform, models, services, capabilities)")
|
|
108
|
+
console.log(" --json Emit machine-readable JSON output (status, logs, package, publish, test, check, update, version, login, dev --sandbox, platform, models, services, capabilities)")
|
|
102
109
|
console.log(" -v, --version Show the installed pltt CLI version")
|
|
103
110
|
console.log("\nPublish flags:")
|
|
104
111
|
console.log(" --env <name> Target environment from ~/.palette/config.json (default: local)")
|