@usepipa/pipa 0.2.2 → 0.3.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.
package/README.md CHANGED
@@ -3,11 +3,15 @@
3
3
  [![CI](https://github.com/lunchpaillola/pipa/actions/workflows/ci.yml/badge.svg)](https://github.com/lunchpaillola/pipa/actions/workflows/ci.yml)
4
4
  [![License](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](LICENSE)
5
5
 
6
- Pipa connects Slack to the OpenCode already installed on your computer. It runs locally: no hosted relay, database, public endpoint, or sandbox is involved.
6
+ Pipa runs OpenCode in one of two profile modes: local Slack Socket Mode or a Managed OpenCode server. Local mode uses no hosted relay, database, public endpoint, or sandbox.
7
7
 
8
8
  **Pipa is in early preview. Features may break unintentionally, and CLI commands, configuration, and Slack behavior may continue to change.**
9
9
 
10
- ## Requirements
10
+ ## Local Socket Mode
11
+
12
+ Profiles created by `pipa init` use Socket Mode. Profiles written before mode support continue to load as Socket Mode.
13
+
14
+ ### Requirements
11
15
 
12
16
  - Node.js 22 or newer
13
17
  - [OpenCode](https://opencode.ai) installed and authenticated
@@ -15,7 +19,7 @@ Pipa connects Slack to the OpenCode already installed on your computer. It runs
15
19
 
16
20
  Invite the Slack app only to channels whose members may use OpenCode in the configured working directory.
17
21
 
18
- ## Install
22
+ ### Install
19
23
 
20
24
  1. Install Pipa:
21
25
 
@@ -46,11 +50,27 @@ export PIPA_OPENCODE_ATTACH_URL=http://localhost:5555
46
50
  pipa start
47
51
  ```
48
52
 
53
+ ## Managed Mode
54
+
55
+ Managed mode starts exactly one persistent `opencode serve` child and does not connect to Slack or require Slack tokens. Create `~/.pipa/config.json` (or `$PIPA_HOME/.pipa/config.json`) with a private hostname and TCP port:
56
+
57
+ ```json
58
+ {
59
+ "botName": "Pipa",
60
+ "workingDirectory": "/workspace",
61
+ "slackMode": "managed",
62
+ "openCodeHostname": "127.0.0.1",
63
+ "openCodePort": 4096
64
+ }
65
+ ```
66
+
67
+ Then run `pipa start`. The OpenCode child inherits the working process environment, so runtime credentials and service configuration should remain in environment variables rather than the profile. `SIGINT` and `SIGTERM` are forwarded to the child.
68
+
49
69
  ## Commands
50
70
 
51
71
  ```text
52
72
  pipa init Configure Slack and the local working directory
53
- pipa start Connect Slack Socket Mode to local OpenCode
73
+ pipa start Start the configured Socket or Managed runtime
54
74
  pipa --version Print the installed version
55
75
  ```
56
76
 
package/bin/pipa.mjs CHANGED
@@ -5,8 +5,9 @@ import { readFile } from "node:fs/promises";
5
5
  import { createInterface } from "node:readline/promises";
6
6
  import { Writable } from "node:stream";
7
7
  import { stdin, stdout } from "node:process";
8
- import { acquireInstanceLock, createManifest, createManifestUrl } from "../src/state.mjs";
8
+ import { acquireInstanceLock, createManifest, createManifestUrl, loadConfig } from "../src/state.mjs";
9
9
  import { initializePipa, startPipa } from "../src/app.mjs";
10
+ import { startOpenCodeServer } from "../src/opencode.mjs";
10
11
 
11
12
  const packageJson = JSON.parse(await readFile(new URL("../package.json", import.meta.url), "utf8"));
12
13
 
@@ -98,7 +99,24 @@ function openUrl(url) {
98
99
  async function start(io) {
99
100
  const releaseLock = await acquireInstanceLock();
100
101
  try {
101
- const app = await startPipa();
102
+ const config = await loadConfig();
103
+ if (config.slackMode === "managed") {
104
+ const server = startOpenCodeServer(config);
105
+ io.output.write(`Pipa is starting OpenCode on ${config.openCodeHostname}:${config.openCodePort}.\n`);
106
+ const stopWithSigint = () => server.stop("SIGINT");
107
+ const stopWithSigterm = () => server.stop("SIGTERM");
108
+ process.on("SIGINT", stopWithSigint);
109
+ process.on("SIGTERM", stopWithSigterm);
110
+ try {
111
+ await server.wait();
112
+ } finally {
113
+ process.off("SIGINT", stopWithSigint);
114
+ process.off("SIGTERM", stopWithSigterm);
115
+ }
116
+ return;
117
+ }
118
+
119
+ const app = await startPipa({ config });
102
120
  io.output.write("Pipa is connected through Slack Socket Mode.\n");
103
121
  await new Promise((resolve, reject) => {
104
122
  let stopping = false;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@usepipa/pipa",
3
- "version": "0.2.2",
4
- "description": "Run Pipa locally through Slack and OpenCode",
3
+ "version": "0.3.0",
4
+ "description": "Run Pipa through Slack Socket Mode or as a managed OpenCode server",
5
5
  "keywords": ["slack", "opencode", "ai-agent", "cli"],
6
6
  "type": "module",
7
7
  "bin": {
package/src/opencode.mjs CHANGED
@@ -123,6 +123,35 @@ export function createOpenCodeExecutor(options = {}) {
123
123
  };
124
124
  }
125
125
 
126
+ export function startOpenCodeServer(config, options = {}) {
127
+ const spawn = options.spawn ?? crossSpawn;
128
+ const platform = options.platform ?? process.platform;
129
+ const child = spawn("opencode", [
130
+ "serve",
131
+ "--hostname", config.openCodeHostname,
132
+ "--port", String(config.openCodePort),
133
+ "--log-level", "ERROR",
134
+ ], {
135
+ cwd: config.workingDirectory,
136
+ env: cleanChildEnvironment(options.environment ?? process.env),
137
+ shell: false,
138
+ stdio: "inherit",
139
+ });
140
+ let stopping = false;
141
+ const exit = waitForExit(child).then((code) => {
142
+ if (!stopping) throw new Error(`OpenCode server exited unexpectedly with code ${code}.`);
143
+ });
144
+
145
+ return {
146
+ wait: () => exit,
147
+ stop(signal) {
148
+ if (stopping) return;
149
+ stopping = true;
150
+ terminateChild(child, platform, signal);
151
+ },
152
+ };
153
+ }
154
+
126
155
  function fetchAttachment(attachment, timeoutMs) {
127
156
  let timer;
128
157
  return Promise.race([
@@ -205,12 +234,13 @@ function waitForExit(child) {
205
234
  });
206
235
  }
207
236
 
208
- function terminateChild(child, platform) {
237
+ function terminateChild(child, platform, signal) {
209
238
  if (platform === "win32" && child.pid) {
210
239
  crossSpawn("taskkill", ["/pid", String(child.pid), "/t", "/f"], { stdio: "ignore" });
211
240
  return;
212
241
  }
213
- child.kill();
242
+ child.kill(signal);
214
243
  const force = setTimeout(() => child.kill("SIGKILL"), 5_000);
215
244
  force.unref?.();
245
+ child.once("close", () => clearTimeout(force));
216
246
  }
package/src/state.mjs CHANGED
@@ -24,6 +24,7 @@ const MANIFEST = {
24
24
  "files:read",
25
25
  "app_mentions:read",
26
26
  "channels:history",
27
+ "channels:read",
27
28
  "chat:write",
28
29
  "groups:history",
29
30
  "groups:read",
@@ -98,12 +99,22 @@ export async function canonicalWorkingDirectory(directory) {
98
99
 
99
100
  export async function loadConfig(file = pipaPaths().config) {
100
101
  const config = await readJson(file, "Run `pipa init` first.");
101
- for (const key of ["botName", "slackAppToken", "slackBotToken", "workingDirectory"]) {
102
+ const slackMode = config.slackMode === undefined ? "socket" : config.slackMode;
103
+ if (slackMode !== "socket" && slackMode !== "managed") {
104
+ throw new Error("Invalid Pipa config: slackMode is unsupported.");
105
+ }
106
+ const requiredStrings = slackMode === "socket"
107
+ ? ["botName", "workingDirectory", "slackAppToken", "slackBotToken"]
108
+ : ["botName", "workingDirectory", "openCodeHostname"];
109
+ for (const key of requiredStrings) {
102
110
  if (typeof config[key] !== "string" || !config[key].trim()) {
103
111
  throw new Error(`Invalid Pipa config: ${key} is missing.`);
104
112
  }
105
113
  }
106
- return config;
114
+ if (slackMode === "managed" && (!Number.isInteger(config.openCodePort) || config.openCodePort < 1 || config.openCodePort > 65535)) {
115
+ throw new Error("Invalid Pipa config: openCodePort must be an integer from 1 to 65535.");
116
+ }
117
+ return { ...config, slackMode };
107
118
  }
108
119
 
109
120
  export async function saveConfig(config, paths = pipaPaths(), manifest = createManifest(config.botName)) {