pi-durable-bot 0.6.0 → 0.7.0

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 (70) hide show
  1. package/CHANGELOG.md +33 -3
  2. package/dist/agent/resources.js +12 -1
  3. package/dist/agent/resources.js.map +1 -1
  4. package/dist/bot/bot.js +54 -5
  5. package/dist/bot/bot.js.map +1 -1
  6. package/dist/bot/prompt.js +13 -3
  7. package/dist/bot/prompt.js.map +1 -1
  8. package/dist/channels/discord/index.js +202 -20
  9. package/dist/channels/discord/index.js.map +1 -1
  10. package/dist/channels/slack/index.js +314 -16
  11. package/dist/channels/slack/index.js.map +1 -1
  12. package/dist/channels/telegram/index.js +21 -2
  13. package/dist/channels/telegram/index.js.map +1 -1
  14. package/dist/cli/agent.js +20 -1
  15. package/dist/cli/agent.js.map +1 -1
  16. package/dist/cli/commands.js +28 -4
  17. package/dist/cli/commands.js.map +1 -1
  18. package/dist/cli/mcp.js +27 -1
  19. package/dist/cli/mcp.js.map +1 -1
  20. package/dist/cli/pages.js +44 -0
  21. package/dist/cli/pages.js.map +1 -0
  22. package/dist/cli/service.js +107 -49
  23. package/dist/cli/service.js.map +1 -1
  24. package/dist/core/channel.js +4 -1
  25. package/dist/core/channel.js.map +1 -1
  26. package/dist/core/env-file.js +23 -6
  27. package/dist/core/env-file.js.map +1 -1
  28. package/dist/core/forms.js +16 -0
  29. package/dist/core/forms.js.map +1 -0
  30. package/dist/core/home.js +10 -2
  31. package/dist/core/home.js.map +1 -1
  32. package/dist/gateway/errors.js +3 -0
  33. package/dist/gateway/errors.js.map +1 -1
  34. package/dist/gateway/forms.js +99 -0
  35. package/dist/gateway/forms.js.map +1 -0
  36. package/dist/gateway/gateway.js +23 -1
  37. package/dist/gateway/gateway.js.map +1 -1
  38. package/dist/gateway/log.js +7 -5
  39. package/dist/gateway/log.js.map +1 -1
  40. package/dist/gateway/waker.js +23 -8
  41. package/dist/gateway/waker.js.map +1 -1
  42. package/dist/main.js +23 -4
  43. package/dist/main.js.map +1 -1
  44. package/dist/pages/cloudflared.js +149 -0
  45. package/dist/pages/cloudflared.js.map +1 -0
  46. package/dist/pages/files.js +244 -0
  47. package/dist/pages/files.js.map +1 -0
  48. package/dist/pages/host.js +268 -0
  49. package/dist/pages/host.js.map +1 -0
  50. package/dist/pages/secret-page.js +71 -0
  51. package/dist/pages/secret-page.js.map +1 -0
  52. package/dist/pages/server.js +305 -0
  53. package/dist/pages/server.js.map +1 -0
  54. package/dist/pages/settings.js +71 -0
  55. package/dist/pages/settings.js.map +1 -0
  56. package/dist/pages/store.js +492 -0
  57. package/dist/pages/store.js.map +1 -0
  58. package/dist/pages/tunnel.js +271 -0
  59. package/dist/pages/tunnel.js.map +1 -0
  60. package/docs/for-agents.md +10 -8
  61. package/kit/pots.css +473 -0
  62. package/kit/pots.js +627 -0
  63. package/package.json +4 -3
  64. package/{docs/channels/discord.md → skills/discord-channel/SKILL.md} +55 -2
  65. package/skills/pages/SKILL.md +242 -0
  66. package/skills/slack-channel/SKILL.md +126 -0
  67. package/skills/telegram-channel/SKILL.md +89 -0
  68. package/docs/channels/slack.md +0 -70
  69. package/docs/channels/telegram.md +0 -50
  70. /package/{docs/channels → skills/slack-channel}/slack-app-manifest.yaml +0 -0
@@ -0,0 +1,271 @@
1
+ import { execFileSync, spawn } from "node:child_process";
2
+ import { Resolver } from "node:dns/promises";
3
+ import { existsSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
4
+ import { createInterface } from "node:readline";
5
+ import { join } from "node:path";
6
+ // A Cloudflare Quick Tunnel to the page server: cloudflared runs while the gateway runs, starts again with a growing pause
7
+ // when it ends, and its https://<random>.trycloudflare.com address is read from its output, also when it changes.
8
+ const ADDRESS = /https:\/\/[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.trycloudflare\.com/;
9
+ /** cloudflared says this once a connection to Cloudflare works; the address is reachable from then on. */
10
+ const CONNECTED = /Registered tunnel connection/;
11
+ /** Lines of cloudflared's output kept, for a failure's message. */
12
+ const TAIL_LINES = 8;
13
+ const DEFAULT_BACKOFF = { firstMs: 1_000, maxMs: 60_000, resetAfterMs: 60_000 };
14
+ /** How long a new address may wait for public DNS before it is given out anyway. */
15
+ const DNS_WAIT_MS = 20_000;
16
+ /**
17
+ * A new Quick Tunnel's name comes into DNS about a second after cloudflared's first connection. A phone that looks it up
18
+ * before then gets "not found" and keeps that answer for a minute, so the address is given out once Cloudflare's and
19
+ * Google's resolvers have it: asked directly, not through this computer's cache, which would keep the "not found" too.
20
+ */
21
+ async function inPublicDns(address) {
22
+ const host = new URL(address).hostname;
23
+ const resolver = new Resolver({ timeout: 2_000, tries: 1 });
24
+ resolver.setServers(["1.1.1.1", "8.8.8.8"]);
25
+ const deadline = Date.now() + DNS_WAIT_MS;
26
+ while (Date.now() < deadline) {
27
+ try {
28
+ await resolver.resolve4(host);
29
+ return;
30
+ }
31
+ catch {
32
+ await new Promise((resolve) => setTimeout(resolve, 500));
33
+ }
34
+ }
35
+ }
36
+ /**
37
+ * cloudflared reads ~/.cloudflared/config.yml (and /etc/cloudflared/…) when it is given no config file, and that
38
+ * file's ingress rules take the place of --url: a Quick Tunnel would then show another service. So it gets a config
39
+ * file of its own, which says nothing but what the flags say.
40
+ */
41
+ const CONFIG = "# Written by Pots for its Cloudflare Quick Tunnel, so cloudflared reads no other config file.\nno-autoupdate: true\n";
42
+ export class QuickTunnel {
43
+ #options;
44
+ #backoff;
45
+ #configFile;
46
+ #pidFile;
47
+ #state = "off";
48
+ #address;
49
+ /** An address cloudflared gave before its first connection. */
50
+ #pending;
51
+ /** The pending address whose name is being looked for in DNS. */
52
+ #confirming;
53
+ #child;
54
+ #exited = Promise.resolve();
55
+ #supervising;
56
+ #stopped = false;
57
+ #error;
58
+ #tail = [];
59
+ #waiters = new Set();
60
+ #endPause;
61
+ constructor(options) {
62
+ this.#options = options;
63
+ this.#backoff = options.backoff ?? DEFAULT_BACKOFF;
64
+ this.#configFile = join(options.home, "cloudflared.yml");
65
+ this.#pidFile = join(options.home, "cloudflared.pid");
66
+ }
67
+ /** The address while a connection works. */
68
+ get address() {
69
+ return this.#address;
70
+ }
71
+ status() {
72
+ return { state: this.#state, ...(this.#address ? { address: this.#address } : {}), ...(this.#error ? { error: this.#error } : {}) };
73
+ }
74
+ /** Start the tunnel if it does not run, and wait until it has an address that works. */
75
+ ensure() {
76
+ if (this.#stopped)
77
+ return Promise.reject(new Error("the gateway is stopping"));
78
+ if (this.#address)
79
+ return Promise.resolve(this.#address);
80
+ const waiting = new Promise((resolve, reject) => {
81
+ const waiter = {
82
+ resolve: (address) => {
83
+ clearTimeout(timer);
84
+ resolve(address);
85
+ },
86
+ reject: (error) => {
87
+ clearTimeout(timer);
88
+ reject(error);
89
+ },
90
+ };
91
+ const timeout = this.#options.startTimeoutMs ?? 60_000;
92
+ const timer = setTimeout(() => {
93
+ this.#waiters.delete(waiter);
94
+ const output = this.#tail.length > 0 ? `; cloudflared said:\n${this.#tail.join("\n")}` : "";
95
+ reject(new Error(`the Cloudflare Quick Tunnel had no address after ${Math.round(timeout / 1000)} seconds${this.#error ? ` (${this.#error})` : ""}${output}`));
96
+ }, timeout);
97
+ this.#waiters.add(waiter);
98
+ });
99
+ this.#supervising ??= this.#supervise().finally(() => (this.#supervising = undefined));
100
+ return waiting;
101
+ }
102
+ /** Stop cloudflared, and do not start it again. */
103
+ async stop() {
104
+ this.#stopped = true;
105
+ this.#endPause?.();
106
+ for (const waiter of this.#waiters)
107
+ waiter.reject(new Error("the gateway is stopping"));
108
+ this.#waiters.clear();
109
+ const child = this.#child;
110
+ if (child && child.exitCode === null && child.signalCode === null) {
111
+ child.kill();
112
+ const killed = setTimeout(() => child.kill("SIGKILL"), 5_000);
113
+ await this.#exited;
114
+ clearTimeout(killed);
115
+ }
116
+ await this.#supervising;
117
+ this.#state = "off";
118
+ }
119
+ async #supervise() {
120
+ let pause = this.#backoff.firstMs;
121
+ while (!this.#stopped) {
122
+ let command;
123
+ try {
124
+ command = await this.#options.program();
125
+ }
126
+ catch (error) {
127
+ // No cloudflared, and no download: the next ensure() tries again.
128
+ this.#fail(error instanceof Error ? error.message : String(error));
129
+ this.#state = "off";
130
+ return;
131
+ }
132
+ if (this.#stopped)
133
+ return;
134
+ const started = Date.now();
135
+ const code = await this.#run(command);
136
+ this.#address = undefined;
137
+ this.#pending = undefined;
138
+ if (this.#stopped)
139
+ return;
140
+ if (Date.now() - started >= this.#backoff.resetAfterMs)
141
+ pause = this.#backoff.firstMs;
142
+ this.#error = `cloudflared ended (${code}); starting it again`;
143
+ this.#options.print("quick tunnel ended", { exit: code, again: pause });
144
+ this.#state = "waiting";
145
+ await new Promise((resolve) => {
146
+ const timer = setTimeout(resolve, pause);
147
+ this.#endPause = () => {
148
+ clearTimeout(timer);
149
+ resolve();
150
+ };
151
+ });
152
+ this.#endPause = undefined;
153
+ pause = Math.min(pause * 2, this.#backoff.maxMs);
154
+ }
155
+ }
156
+ /** Run cloudflared once, until it ends. Resolves with how it ended. */
157
+ #run(command) {
158
+ this.#state = "starting";
159
+ this.#tail = [];
160
+ stopLeftoverTunnel(this.#options.home, this.#options.print, this.#child?.pid);
161
+ writeFileSync(this.#configFile, CONFIG);
162
+ const args = [...command.slice(1), "tunnel", "--no-autoupdate", "--config", this.#configFile, "--url", this.#options.target, "--metrics", "127.0.0.1:0"];
163
+ // The person's own tunnel settings in the environment (TUNNEL_TOKEN, TUNNEL_URL, …) are not for this one.
164
+ const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.startsWith("TUNNEL_")));
165
+ let child;
166
+ try {
167
+ child = spawn(command[0], args, { stdio: ["ignore", "pipe", "pipe"], env, windowsHide: true });
168
+ }
169
+ catch (error) {
170
+ return Promise.resolve(`could not start: ${error instanceof Error ? error.message : String(error)}`);
171
+ }
172
+ this.#child = child;
173
+ const ended = new Promise((resolve) => {
174
+ child.once("error", (error) => resolve(`could not start: ${error.message}`));
175
+ child.once("close", (code, signal) => resolve(signal ? `signal ${signal}` : `exit code ${code}`));
176
+ });
177
+ this.#exited = ended.then(() => undefined);
178
+ if (child.pid !== undefined)
179
+ writeFileSync(this.#pidFile, `${child.pid}\n${this.#configFile}\n`);
180
+ for (const stream of [child.stdout, child.stderr])
181
+ createInterface({ input: stream }).on("line", (line) => this.#line(line));
182
+ return ended.then((how) => {
183
+ this.#child = undefined;
184
+ try {
185
+ if (readFileSync(this.#pidFile, "utf8").startsWith(`${child.pid}\n`))
186
+ unlinkSync(this.#pidFile);
187
+ }
188
+ catch {
189
+ // gone already
190
+ }
191
+ return how;
192
+ });
193
+ }
194
+ #line(line) {
195
+ this.#tail.push(line);
196
+ if (this.#tail.length > TAIL_LINES)
197
+ this.#tail.shift();
198
+ const address = ADDRESS.exec(line)?.[0];
199
+ if (address) {
200
+ if (this.#address && address !== this.#address) {
201
+ this.#address = address;
202
+ this.#options.print("quick tunnel address changed", { address });
203
+ }
204
+ else if (!this.#address)
205
+ this.#pending = address;
206
+ }
207
+ if (CONNECTED.test(line) && this.#pending && !this.#address && this.#confirming !== this.#pending) {
208
+ const pending = this.#pending;
209
+ this.#confirming = pending;
210
+ void (this.#options.ready ?? inPublicDns)(pending)
211
+ .catch(() => { })
212
+ .then(() => {
213
+ if (this.#confirming === pending)
214
+ this.#confirming = undefined;
215
+ // cloudflared ended, or another address came, while the name was looked for.
216
+ if (this.#stopped || this.#pending !== pending || this.#address)
217
+ return;
218
+ this.#address = pending;
219
+ this.#pending = undefined;
220
+ this.#state = "ready";
221
+ this.#error = undefined;
222
+ this.#options.print("quick tunnel ready", { address: pending });
223
+ for (const waiter of this.#waiters)
224
+ waiter.resolve(pending);
225
+ this.#waiters.clear();
226
+ });
227
+ }
228
+ }
229
+ #fail(error) {
230
+ this.#error = error;
231
+ this.#options.print("quick tunnel failed", { error });
232
+ for (const waiter of this.#waiters)
233
+ waiter.reject(new Error(error));
234
+ this.#waiters.clear();
235
+ }
236
+ }
237
+ /**
238
+ * A cloudflared that an earlier gateway of this home started and could not stop, as when it crashed, keeps a public
239
+ * entry to the page server open: the port stays the same, so its old address works again. Stop it, when the process of
240
+ * the ID file is still a cloudflared with this home's config file; the gateway does this at every start, also with a
241
+ * publicUrl, which needs no tunnel.
242
+ */
243
+ export function stopLeftoverTunnel(home, print, except) {
244
+ const pidFile = join(home, "cloudflared.pid");
245
+ if (!existsSync(pidFile))
246
+ return;
247
+ try {
248
+ const [pidText, config] = readFileSync(pidFile, "utf8").split("\n");
249
+ const pid = Number(pidText);
250
+ if (Number.isInteger(pid) && pid > 0 && config && pid !== except && pid !== process.pid) {
251
+ const command = process.platform === "win32"
252
+ ? execFileSync("tasklist", ["/FI", `PID eq ${pid}`, "/FO", "CSV", "/NH"], { encoding: "utf8", timeout: 5_000, windowsHide: true })
253
+ : execFileSync("ps", ["-o", "args=", "-p", String(pid)], { encoding: "utf8", timeout: 5_000 });
254
+ const ours = process.platform === "win32" ? /cloudflared/i.test(command) : command.includes(config) && command.includes("cloudflared");
255
+ if (ours) {
256
+ process.kill(pid);
257
+ print("stopped a quick tunnel left from before", { pid });
258
+ }
259
+ }
260
+ }
261
+ catch {
262
+ // not running, or not ours
263
+ }
264
+ try {
265
+ unlinkSync(pidFile);
266
+ }
267
+ catch {
268
+ // gone already
269
+ }
270
+ }
271
+ //# sourceMappingURL=tunnel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tunnel.js","sourceRoot":"","sources":["../../src/pages/tunnel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,YAAY,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC5E,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC9E,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAGjC,2HAA2H;AAC3H,kHAAkH;AAElH,MA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CAAC;QACP,eAAe;IACjB,CAAC;AACH,CAAC"}
@@ -23,6 +23,8 @@ Once the bot is made, **it connects its chat apps itself**. For each app, it cre
23
23
 
24
24
  So do not create the chat app (such as a Slack app), write a manifest, collect tokens or edit `~/.pots/` by hand for your person, and do not tell them to do any of that first. Your job is to install Pots and make the bot, as below. After that, hand the person over to their bot: they say "join our Slack" in `pots chat`, and the bot does the rest.
25
25
 
26
+ The same goes for its other tools: the bot adds its own routines, and skills for the command line tools it uses. Run `pots mcp add` only when your person asks for an MCP server; they sign in to it with `pots mcp login <server>` in their own terminal.
27
+
26
28
  On a computer without a desktop, such as a cloud server reached over SSH, the bot still does this. It explains the choices and lets the person pick:
27
29
 
28
30
  - a virtual desktop they can watch and sign in to over VNC;
@@ -42,19 +44,19 @@ When it needs tokens the person copied, it asks for them in `pots chat` with hid
42
44
  Do the setup yourself, in your own shell. Only one step is your person's: signing in to a model. A subscription sign-in opens their browser, and an API key must not pass through your conversation, so they do it in their own terminal with `pots login`.
43
45
 
44
46
  1. **Install:** `npm install -g pi-durable-bot`.
45
- 2. **Ask your person**, in your chat:
47
+ 2. **See what pi can use already:** `pots login --list`. It lists the models (for `--model`) and judges (for `--judge`) that have a login, a key or a local server on this computer, and never shows a secret.
48
+ 3. **Ask your person**, in your chat, only what `--list` cannot answer:
46
49
  - the bot's name;
47
50
  - in one sentence, what it is for (optional; the bot asks them later if they skip it);
48
- - how it should get its AI: a subscription they already pay for (ChatGPT, Claude, GitHub Copilot, Grok, Kimi and more), an API key from a provider, or a local model server (Ollama, LM Studio, llama.cpp, vLLM);
49
- - who decides whether a group message that does not mention the bot wakes it: the bot itself (the default: every such message wakes it), or Jev, a fast judge from TypeSafe that wakes it only for messages about its work, with their TypeSafe API key.
50
- 3. **See what pi can use already:** `pots login --list`. It lists the models (for `--model`) and judges (for `--judge`) that have a login, a key or a local server on this computer, and never shows a secret.
51
+ - how it should get its AI, when the list has nothing they want: a subscription they already pay for (ChatGPT, Claude, GitHub Copilot, Grok, Kimi and more), an API key from a provider, or a local model server (Ollama, LM Studio, llama.cpp, vLLM);
52
+ - who decides whether a group message that does not mention the bot wakes it: the bot itself (the default: every such message wakes it), or Jev, a fast judge from TypeSafe that wakes it only for messages about its work, with their TypeSafe API key. Do not ask when the bot will not be in group chats: use `--judge off`.
51
53
  4. **If what they chose is not listed, ask them to run `pots login` in their own terminal.** It asks how to get the AI, signs in or takes the key with hidden input, tries a model, and ends with a line such as `Ready: openai/gpt-6.1-sol`. On a computer without a browser, such as a server over SSH, it prints a link and asks for the code the page shows (choose "Copy code login (headless)" when it is offered). For Jev, they run it once more and choose "Use an API key", then TypeSafe. Then run `pots login --list` again.
52
54
  5. **Make the bot:**
53
55
  ```sh
54
56
  pots init --yes --name "Ada" --model openai/gpt-6.1-sol --about "our team's research and ops assistant" --no-chat
55
57
  ```
56
- Add `--judge typesafe/jev-latest` for Jev. `pots init` tries the model and the judge before it makes the bot, then starts the gateway as a background service (launchd on macOS, a systemd user service on Linux, Task Scheduler on Windows). On Linux it turns on lingering for the user (with `sudo` when the system asks for it), so the bot keeps running after your person logs out of SSH; `pots init --system` installs a system service instead (it needs sudo).
57
- 6. **Hand over:** tell your person to run `pots chat` to meet the bot. It says hello, checks the screen and asks which chat apps to join.
58
+ For `--model`, take the newest strong model of your person's provider from `pots login --list`, and tell them which one you chose. Use `--judge off` when the bot will not be in group chats, and `--judge typesafe/jev-latest` for Jev. `pots init` tries the model and the judge before it makes the bot, then starts the gateway as a background service (launchd on macOS, a systemd user service on Linux, Task Scheduler on Windows). On Linux it turns on lingering for the user (with `sudo` when the system asks for it), so the bot keeps running after your person logs out of SSH; `pots init --system` installs a system service instead (it needs sudo). Each Linux user has one Pots service: one gateway process that runs all of that user's bots (`pots bot new` adds more). On a computer that several people use, each user runs their own, with a user service of their own.
59
+ 6. **Check, then hand over:** `pots chat --bot <id> -m "hi" --timeout 60` must print the bot's answer; `<id>` is the folder name in the `Created …/bots/<id>` line of `pots init`. If no answer comes, see `pots status` and `pots service logs`. Then tell your person to run `pots chat` to meet the bot. It says hello, checks the screen and asks which chat apps to join.
58
60
 
59
61
  A person without an AI agent runs `pots init` alone instead: it asks the same things in the terminal, signs in, and opens the chat at the end.
60
62
 
@@ -69,7 +71,7 @@ Tell your person:
69
71
  | Command | What it does |
70
72
  |---|---|
71
73
  | `pots login` | Sign in to a model, or add an API key or a local server (in a terminal); `--list` shows what can be used |
72
- | `pots mcp add` / `list` / `remove` | MCP servers for every bot and pi on this computer, in pi's `~/.pi/agent/mcp.json`, with the options of `pi mcp` (`pots mcp --help`); the bot calls their tools from its code mode scripts |
74
+ | `pots mcp add` / `list` / `remove` | MCP servers for every bot and pi on this computer, in pi's `~/.pi/agent/mcp.json`, with the options of `pi mcp` (`pots mcp --help`); the bot calls their tools from code mode scripts: short JavaScript programs it writes to call its tools |
73
75
  | `pots mcp login <server>` | Sign in to an MCP server in the browser (in a terminal) |
74
76
  | `pots status` | The gateway, the service and each bot |
75
77
  | `pots service logs` | The gateway's output |
@@ -77,7 +79,7 @@ Tell your person:
77
79
  | `pots routine list` | The work a bot repeats on a schedule, with each routine's next time and last run |
78
80
  | `pots bot new` / `pots bot list` | More bots on the same computer |
79
81
  | `npm install -g pi-durable-bot@latest`, then `pots service restart` | Upgrade |
80
- | `pots service uninstall`, then `npm uninstall -g pi-durable-bot` | Remove it. The bots stay in `~/.pots/` until that folder is deleted |
82
+ | `pots service uninstall`, then `npm uninstall -g pi-durable-bot` | Remove it. The bots stay in `~/.pots/` until that folder is deleted. So does the screen lease file, `/tmp/pots-screens-<uid>.sqlite` (on Windows, `pots-screens.sqlite` in the temporary folder) |
81
83
 
82
84
  ## When something goes wrong
83
85