@treestructuredplanning/gateway 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +198 -2
- package/dist/cli.js +347 -0
- package/dist/gateway-schema.json +889 -0
- package/dist/shared-3OFEPCJI.js +3832 -0
- package/dist/vendor.js +4451 -0
- package/package.json +31 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Webify Services
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,199 @@
|
|
|
1
|
-
#
|
|
1
|
+
# TSP gateway
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The TSP gateway lets the [Tree-Structured Planning](https://treestructuredplanning.com)
|
|
4
|
+
(TSP) canvas start, stop and resume Claude Code and Codex sessions on your computer, in the
|
|
5
|
+
repository folders you register. It runs in the background under your own user account and
|
|
6
|
+
only connects outward to TSP.
|
|
7
|
+
|
|
8
|
+
## Requirements
|
|
9
|
+
|
|
10
|
+
- Node.js **22 or newer**.
|
|
11
|
+
- Claude Code, or Codex **0.162.1 or newer**, installed separately and signed in with its
|
|
12
|
+
own login (`codex login` for Codex). The gateway never signs in for you.
|
|
13
|
+
- Each folder you register must be a local clone with a GitHub remote, and the harness must
|
|
14
|
+
trust it: run `claude` or `codex` in the folder once and accept its trust prompt. Codex
|
|
15
|
+
sessions never start in a folder Codex has not trusted, and trusting a parent folder does not
|
|
16
|
+
count for a repository inside it.
|
|
17
|
+
- On Linux, a running Secret Service such as GNOME Keyring or KWallet. The gateway will not
|
|
18
|
+
run without an OS credential store.
|
|
19
|
+
- Build must be available for your TSP account.
|
|
20
|
+
|
|
21
|
+
`connect` and `tsp-gateway status` print the steps left to you for each folder, such as
|
|
22
|
+
installing or signing in to a harness or accepting its trust prompt.
|
|
23
|
+
|
|
24
|
+
## Connect this computer
|
|
25
|
+
|
|
26
|
+
Run this inside the local clone of the repository you want TSP to build in:
|
|
27
|
+
|
|
28
|
+
```sh
|
|
29
|
+
npx -y @treestructuredplanning/gateway@latest connect
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
`connect [folder]` (the folder defaults to the current one) does the whole setup, and running
|
|
33
|
+
it again is safe:
|
|
34
|
+
|
|
35
|
+
1. It checks that the folder is a clone with a GitHub remote, so a wrong folder fails before
|
|
36
|
+
anything changes.
|
|
37
|
+
2. It pairs this computer with your TSP account, only after you approve in the browser window
|
|
38
|
+
it opens. It also prints the approval link in case no browser opens. A working pairing is
|
|
39
|
+
kept.
|
|
40
|
+
3. It registers the folder.
|
|
41
|
+
4. It installs a login item that starts the gateway when you log in.
|
|
42
|
+
5. It prints the gateway's status and any steps only you can take in each folder: install a
|
|
43
|
+
harness, sign in, or run the harness there once to accept its trust prompt.
|
|
44
|
+
|
|
45
|
+
Run outside any clone, `connect` keeps the folders already registered and repairs the rest,
|
|
46
|
+
so it also fixes a stopped, outdated or revoked gateway. If TSP cannot be reached, nothing
|
|
47
|
+
changes; try again later. The login item runs the copy of the gateway that `connect` ran
|
|
48
|
+
from. With `npx` that is npm's cache, so if you clear that cache, run `connect` again.
|
|
49
|
+
|
|
50
|
+
## Disconnect and uninstall
|
|
51
|
+
|
|
52
|
+
```sh
|
|
53
|
+
npx -y @treestructuredplanning/gateway@latest disconnect
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
`disconnect` removes the login item, then revokes this computer's pairing in TSP and deletes
|
|
57
|
+
its stored credential. Registered folders stay in the config file. If TSP cannot be reached,
|
|
58
|
+
the pairing is kept; run `disconnect` again. If the credential was already missing, the
|
|
59
|
+
computer may still be listed in TSP; remove it from Settings there.
|
|
60
|
+
|
|
61
|
+
To leave nothing behind, also delete the config and state folders listed under
|
|
62
|
+
[What it stores](#what-it-stores), and if you installed globally:
|
|
63
|
+
|
|
64
|
+
```sh
|
|
65
|
+
npm uninstall --global @treestructuredplanning/gateway
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Commands
|
|
69
|
+
|
|
70
|
+
With a global install (`npm install --global @treestructuredplanning/gateway@latest`) the
|
|
71
|
+
command is `tsp-gateway`; with `npx`, use `npx -y @treestructuredplanning/gateway@latest`
|
|
72
|
+
in its place.
|
|
73
|
+
|
|
74
|
+
| Command | What it does |
|
|
75
|
+
| --------------------------- | ---------------------------------------------------------------------------- |
|
|
76
|
+
| `connect [folder]` | Pair if needed, register the folder, install the login item, print status. |
|
|
77
|
+
| `disconnect` | Remove the login item, then unpair. |
|
|
78
|
+
| `pair` | Pair this computer with your TSP account (replaces any existing pairing). |
|
|
79
|
+
| `add <folder>` | Register a repository folder. |
|
|
80
|
+
| `remove <owner/name>` | Unregister a repository. |
|
|
81
|
+
| `configure-launch '<json>'` | Set how sessions get your shell environment (see below). |
|
|
82
|
+
| `status` | Show pairing, connection, harness sign-in and trust, and registered folders. |
|
|
83
|
+
| `start` | Run the gateway in the foreground. Ctrl-C stops it, not your sessions. |
|
|
84
|
+
| `install` | Install or refresh the login item. Run it again after upgrading the gateway. |
|
|
85
|
+
| `uninstall` | Remove the login item; keep the pairing and folders. |
|
|
86
|
+
| `unpair` | Revoke this computer's pairing in TSP and delete the stored credential. |
|
|
87
|
+
|
|
88
|
+
Registering or removing a folder takes effect in the running gateway without a restart.
|
|
89
|
+
|
|
90
|
+
## What TSP can ask it to do
|
|
91
|
+
|
|
92
|
+
TSP can ask the gateway for exactly four things, and only in folders you registered:
|
|
93
|
+
|
|
94
|
+
- **Launch** a Claude Code or Codex session, with a prompt describing the TSP work you
|
|
95
|
+
started from the canvas.
|
|
96
|
+
- **Stop** that session.
|
|
97
|
+
- **Resume** that session, with the text you typed. With no text, a stopped session gets the
|
|
98
|
+
fixed prompt `Continue where you left off.`; a done or failed session reopens without a
|
|
99
|
+
prompt and waits for you.
|
|
100
|
+
- **Report status**: which harnesses are installed, signed in and trust each registered
|
|
101
|
+
folder, and how each launched session is doing.
|
|
102
|
+
|
|
103
|
+
Only your own clicks in TSP send a prompt. Sessions keep running if the gateway stops. A
|
|
104
|
+
session that finishes does not mean TSP has reviewed or accepted its work. Keep the
|
|
105
|
+
[TSP plugin](https://github.com/WebifyServices/tsp-plugin) installed so launched sessions
|
|
106
|
+
have TSP's tools.
|
|
107
|
+
|
|
108
|
+
The gateway never:
|
|
109
|
+
|
|
110
|
+
- changes your Claude Code or Codex settings, permissions or configuration, or passes any
|
|
111
|
+
flag that bypasses them. Your own settings govern every session.
|
|
112
|
+
- answers an approval prompt or question inside a session. When a session needs you, TSP
|
|
113
|
+
shows the command to open it yourself (`claude attach <id>` or `codex resume <id>`).
|
|
114
|
+
- signs in, accepts a trust prompt, or approves pairing for you.
|
|
115
|
+
- accepts a shell command, program, folder, file path or environment variable from TSP.
|
|
116
|
+
- runs anything in a folder you have not registered, or starts Codex in a folder Codex does
|
|
117
|
+
not trust.
|
|
118
|
+
|
|
119
|
+
For Codex, sessions run in Codex's own background service, which the gateway starts when
|
|
120
|
+
needed and never stops.
|
|
121
|
+
|
|
122
|
+
## What leaves your computer
|
|
123
|
+
|
|
124
|
+
The gateway sends TSP only repository names (`owner/name`), which harnesses are available,
|
|
125
|
+
signed in and trusted per folder, and session status, including any waiting message with
|
|
126
|
+
folder paths removed. Folder paths, environment variables, command lines, transcripts and raw
|
|
127
|
+
error output stay on your computer.
|
|
128
|
+
|
|
129
|
+
The gateway only connects outward, to TSP, and accepts no inbound connections. The only
|
|
130
|
+
exception is pairing: while you approve in the browser, it listens briefly on `127.0.0.1`
|
|
131
|
+
(your computer only) for the browser's reply, then stops.
|
|
132
|
+
|
|
133
|
+
## How sessions get your environment
|
|
134
|
+
|
|
135
|
+
By default, Claude Code sessions start through your login shell, so variables your shell
|
|
136
|
+
startup files export reach the session: for example `CLAUDE_CONFIG_DIR`, `ANTHROPIC_*`,
|
|
137
|
+
`AWS_*`, proxy settings and version-manager `PATH` entries. Aliases and unexported shell
|
|
138
|
+
variables do not. Bash reads its login profile; source `.bashrc` from there if your terminal
|
|
139
|
+
relies on it. Codex's background service gets the same environment when the gateway starts
|
|
140
|
+
it, and keeps that environment until it restarts.
|
|
141
|
+
|
|
142
|
+
Supported shells are bash, zsh, ksh, fish, csh, tcsh and sh. With any other shell, or none,
|
|
143
|
+
the gateway falls back to `/bin/sh`, then to starting the harness directly. On Windows the
|
|
144
|
+
harness always starts directly and PowerShell or cmd profiles are not loaded. Your shell
|
|
145
|
+
startup must finish quickly, must not wait for input, and must not redirect its own output to
|
|
146
|
+
a file or terminal.
|
|
147
|
+
|
|
148
|
+
Change this with `configure-launch`, which sets the `launch` section of the gateway's config
|
|
149
|
+
file. Omitted settings use defaults:
|
|
150
|
+
|
|
151
|
+
```sh
|
|
152
|
+
# Restore the default login-shell behaviour.
|
|
153
|
+
tsp-gateway configure-launch '{}'
|
|
154
|
+
|
|
155
|
+
# Use the gateway's own environment directly.
|
|
156
|
+
tsp-gateway configure-launch '{"mode":"direct"}'
|
|
157
|
+
|
|
158
|
+
# Choose a shell, run through direnv in the session's folder, and add a nonsecret variable.
|
|
159
|
+
# Allow the repository's .envrc first; direnv exec . must also succeed from your home folder.
|
|
160
|
+
tsp-gateway configure-launch '{"shell":"/bin/zsh","wrapper":["direnv","exec","."],"env":{"AWS_PROFILE":"development"}}'
|
|
161
|
+
|
|
162
|
+
# Any program of yours can wrap the harness; give it as an argument list, not a command string.
|
|
163
|
+
tsp-gateway configure-launch '{"wrapper":["/absolute/path/to/company-launcher","--profile","development"]}'
|
|
164
|
+
tsp-gateway status
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
- `mode`: `"login-shell"` (default) or `"direct"`.
|
|
168
|
+
- `shell`: an absolute path.
|
|
169
|
+
- `wrapper`: an argument list whose program is an absolute path or a command name. The harness
|
|
170
|
+
command is appended to it. It also runs for the gateway's status checks, sometimes from your
|
|
171
|
+
home folder, so keep it fast and noninteractive.
|
|
172
|
+
- `env`: variable names and plain string values, applied after your shell startup.
|
|
173
|
+
|
|
174
|
+
Unknown fields and command strings are rejected. **Keep secrets in your shell setup or
|
|
175
|
+
keychain, not in this file.** `status` shows the shell in use and why, the wrapper program and
|
|
176
|
+
the names of `env` variables, never their values.
|
|
177
|
+
|
|
178
|
+
If a session's environment differs from your terminal's, check the shell and reason in
|
|
179
|
+
`tsp-gateway status` and the gateway log, then check that your shell startup exports the
|
|
180
|
+
variables you need. Your terminal and the login item can have different `$SHELL` values. For a
|
|
181
|
+
slow or interactive startup, make its interactive parts depend on a terminal, use a wrapper,
|
|
182
|
+
or choose `direct`. Changes apply to sessions started afterwards, not to running ones.
|
|
183
|
+
|
|
184
|
+
## What it stores
|
|
185
|
+
|
|
186
|
+
| What | Where |
|
|
187
|
+
| -------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
188
|
+
| Credential | One OS credential-store entry (Keychain, Credential Manager or Secret Service) named `com.treestructuredplanning.gateway`, holding the pairing's refresh token. |
|
|
189
|
+
| Config | `${XDG_CONFIG_HOME:-~/.config}/tsp-gateway/config.json` (mode 0600, folder 0700), or `%APPDATA%\tsp-gateway` on Windows: pairing identity, registered folders and their paths, captured `PATH`, launch settings. |
|
|
190
|
+
| State and logs | `${XDG_STATE_HOME:-~/.local/state}/tsp-gateway`, or `%LOCALAPPDATA%\tsp-gateway` on Windows: status, logs, and a private record of the commands it received, which can include launch prompts and your resume text. Never uploaded. |
|
|
191
|
+
| Login item | `~/Library/LaunchAgents/com.treestructuredplanning.tsp-gateway.plist` (macOS), `~/.config/systemd/user/tsp-gateway.service` (Linux), or the `com.treestructuredplanning.tsp-gateway` logon task (Windows). |
|
|
192
|
+
|
|
193
|
+
On Windows, access to these folders follows your user profile's permissions. Delete the state
|
|
194
|
+
folder only after `disconnect`, once no gateway is running.
|
|
195
|
+
|
|
196
|
+
## Help
|
|
197
|
+
|
|
198
|
+
See the [TSP documentation](https://treestructuredplanning.com/docs) or email
|
|
199
|
+
`support@treestructuredplanning.com`.
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createRequire } from "node:module"; const require = createRequire(import.meta.url);
|
|
3
|
+
import {
|
|
4
|
+
Commands,
|
|
5
|
+
GATEWAY_CLOSE_DISPOSITION,
|
|
6
|
+
GatewayStatus,
|
|
7
|
+
HarnessExecutor,
|
|
8
|
+
Journal,
|
|
9
|
+
OAuthTokens,
|
|
10
|
+
addRepository,
|
|
11
|
+
awaitDaemonConnection,
|
|
12
|
+
builtinHarnesses,
|
|
13
|
+
connect,
|
|
14
|
+
daemonConnection,
|
|
15
|
+
daemonLock,
|
|
16
|
+
decodeLaunchConfig,
|
|
17
|
+
disconnect,
|
|
18
|
+
execute,
|
|
19
|
+
field,
|
|
20
|
+
installLoginItem,
|
|
21
|
+
isObject,
|
|
22
|
+
keychain,
|
|
23
|
+
locations,
|
|
24
|
+
pairMachine,
|
|
25
|
+
parseJson,
|
|
26
|
+
privateDirectory,
|
|
27
|
+
readConfig,
|
|
28
|
+
readLaunchObservation,
|
|
29
|
+
remainingSteps,
|
|
30
|
+
removeLoginItem,
|
|
31
|
+
runDaemon,
|
|
32
|
+
saveConfig,
|
|
33
|
+
unpairMachine,
|
|
34
|
+
validSlug,
|
|
35
|
+
validateOrigin,
|
|
36
|
+
watchConfig
|
|
37
|
+
} from "./shared-3OFEPCJI.js";
|
|
38
|
+
|
|
39
|
+
// src/cli.ts
|
|
40
|
+
import { readFile } from "node:fs/promises";
|
|
41
|
+
import { homedir } from "node:os";
|
|
42
|
+
import { isAbsolute, join } from "node:path";
|
|
43
|
+
import { fileURLToPath } from "node:url";
|
|
44
|
+
import { parseArgs } from "node:util";
|
|
45
|
+
|
|
46
|
+
// package.json
|
|
47
|
+
var package_default = {
|
|
48
|
+
name: "@treestructuredplanning/gateway",
|
|
49
|
+
version: "0.1.1",
|
|
50
|
+
description: "Outbound local harness gateway for Tree-Structured Planning",
|
|
51
|
+
type: "module",
|
|
52
|
+
engines: {
|
|
53
|
+
node: ">=22"
|
|
54
|
+
},
|
|
55
|
+
bin: {
|
|
56
|
+
"tsp-gateway": "dist/cli.js"
|
|
57
|
+
},
|
|
58
|
+
files: [
|
|
59
|
+
"dist/cli.js",
|
|
60
|
+
"dist/shared-*.js",
|
|
61
|
+
"dist/vendor.js",
|
|
62
|
+
"dist/gateway-schema.json",
|
|
63
|
+
"README.md"
|
|
64
|
+
],
|
|
65
|
+
scripts: {
|
|
66
|
+
build: "node scripts/build.mjs",
|
|
67
|
+
prepack: "pnpm build",
|
|
68
|
+
test: "pnpm build && node --test --test-concurrency=3 test/*.test.mjs",
|
|
69
|
+
"measure:launch": "pnpm build && node scripts/measure-launch.mjs",
|
|
70
|
+
typecheck: "tsc --noEmit",
|
|
71
|
+
lint: "eslint src --report-unused-disable-directives --max-warnings 0",
|
|
72
|
+
check: "pnpm typecheck && pnpm lint && pnpm format:check && pnpm test",
|
|
73
|
+
"format:check": 'prettier --check "src/**/*.ts" "test/*.mjs" "scripts/*.mjs" README.md package.json tsconfig.json',
|
|
74
|
+
"test:coverage": 'pnpm build && node --test-concurrency=3 --experimental-test-coverage --test-coverage-lines=80 --test --test-coverage-include="dist/*.js" --test-coverage-exclude="dist/vendor.js" test/*.test.mjs'
|
|
75
|
+
},
|
|
76
|
+
devDependencies: {
|
|
77
|
+
"@tsp/shared": "workspace:*",
|
|
78
|
+
"@types/node": "^24.10.9",
|
|
79
|
+
esbuild: "0.25.12",
|
|
80
|
+
typescript: "^5.7.2"
|
|
81
|
+
},
|
|
82
|
+
license: "MIT",
|
|
83
|
+
dependencies: {
|
|
84
|
+
"@napi-rs/keyring": "2.1.2",
|
|
85
|
+
"smol-toml": "1.6.0",
|
|
86
|
+
ws: "8.21.0"
|
|
87
|
+
},
|
|
88
|
+
publishConfig: {
|
|
89
|
+
access: "public"
|
|
90
|
+
},
|
|
91
|
+
repository: {
|
|
92
|
+
type: "git",
|
|
93
|
+
url: "git+https://github.com/WebifyServices/tsp.git",
|
|
94
|
+
directory: "packages/tsp-gateway"
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// src/cli.ts
|
|
99
|
+
async function runLocal(loginPath, paths, config, version) {
|
|
100
|
+
const pairing = config.pairing;
|
|
101
|
+
if (!pairing) throw new Error("Run tsp-gateway pair first.");
|
|
102
|
+
await privateDirectory(paths.state);
|
|
103
|
+
const release = await daemonLock(paths.state);
|
|
104
|
+
const abort = new AbortController();
|
|
105
|
+
const stop = () => abort.abort();
|
|
106
|
+
process.once("SIGINT", stop);
|
|
107
|
+
process.once("SIGTERM", stop);
|
|
108
|
+
try {
|
|
109
|
+
const journal = new Journal(join(paths.state, `${pairing.machine_id}-receipts.jsonl`));
|
|
110
|
+
await journal.load();
|
|
111
|
+
const repositories = async () => (await readConfig(paths.config)).repositories;
|
|
112
|
+
const environment = {
|
|
113
|
+
...process.env,
|
|
114
|
+
PATH: loginPath ?? config.loginPath ?? process.env["PATH"]
|
|
115
|
+
};
|
|
116
|
+
const executor = new HarnessExecutor(environment, {
|
|
117
|
+
config: async () => (await readConfig(paths.config)).launch,
|
|
118
|
+
changed: () => {
|
|
119
|
+
void status.refresh().catch(() => console.error("Could not update gateway launch status"));
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
const status = new GatewayStatus(join(paths.state, "status.json"), executor);
|
|
123
|
+
const drivers = builtinHarnesses(environment, executor);
|
|
124
|
+
const commands = new Commands(journal, drivers, repositories);
|
|
125
|
+
await runDaemon({
|
|
126
|
+
pairing,
|
|
127
|
+
version,
|
|
128
|
+
drivers,
|
|
129
|
+
commands,
|
|
130
|
+
tokens: new OAuthTokens(pairing, keychain(pairing.origin)),
|
|
131
|
+
repositories: async () => drivers.repositories(await repositories()),
|
|
132
|
+
watchFacts: (changed) => watchConfig(paths.config, () => {
|
|
133
|
+
executor.invalidate();
|
|
134
|
+
changed();
|
|
135
|
+
void status.refresh().catch(() => console.error("Could not update gateway launch status"));
|
|
136
|
+
}),
|
|
137
|
+
signal: abort.signal,
|
|
138
|
+
status: (current) => status.update(current)
|
|
139
|
+
});
|
|
140
|
+
} finally {
|
|
141
|
+
process.removeListener("SIGINT", stop);
|
|
142
|
+
process.removeListener("SIGTERM", stop);
|
|
143
|
+
await release();
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
async function printStatus(paths, config, drivers, executor, idle = "Not running") {
|
|
147
|
+
const connection = await daemonConnection(join(paths.state, "status.json")) ?? idle;
|
|
148
|
+
let daemonLaunch = null;
|
|
149
|
+
let lastMessage = null;
|
|
150
|
+
let closeCode = null;
|
|
151
|
+
let closeDisposition = null;
|
|
152
|
+
try {
|
|
153
|
+
const status = parseJson(await readFile(join(paths.state, "status.json"), "utf8"));
|
|
154
|
+
if (isObject(status)) {
|
|
155
|
+
daemonLaunch = readLaunchObservation(field(status, "launch"));
|
|
156
|
+
const code = field(status, "close_code"), recovery = field(status, "close_disposition");
|
|
157
|
+
if (typeof code === "number" && Number.isInteger(code)) closeCode = code;
|
|
158
|
+
closeDisposition = Object.values(GATEWAY_CLOSE_DISPOSITION).find((value) => value === recovery) ?? null;
|
|
159
|
+
}
|
|
160
|
+
if (isObject(status) && typeof field(status, "message") === "string")
|
|
161
|
+
lastMessage = field(status, "message");
|
|
162
|
+
} catch {
|
|
163
|
+
}
|
|
164
|
+
console.log(
|
|
165
|
+
JSON.stringify(
|
|
166
|
+
{
|
|
167
|
+
paired: config.pairing !== null,
|
|
168
|
+
machine_id: config.pairing?.machine_id ?? null,
|
|
169
|
+
connection,
|
|
170
|
+
last_message: lastMessage,
|
|
171
|
+
close_code: closeCode,
|
|
172
|
+
close_disposition: closeDisposition,
|
|
173
|
+
harnesses: await drivers.detect(),
|
|
174
|
+
launch: await executor.status(),
|
|
175
|
+
launch_source: "status-probe",
|
|
176
|
+
last_daemon_launch: daemonLaunch,
|
|
177
|
+
repositories: Object.keys(config.repositories)
|
|
178
|
+
},
|
|
179
|
+
null,
|
|
180
|
+
2
|
|
181
|
+
)
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
function startupOptions(args, version) {
|
|
185
|
+
const options = args[0] === "start" ? parseArgs({
|
|
186
|
+
args: args.slice(1),
|
|
187
|
+
options: {
|
|
188
|
+
"expected-version": { type: "string" },
|
|
189
|
+
"config-file": { type: "string" },
|
|
190
|
+
"state-dir": { type: "string" },
|
|
191
|
+
"login-path": { type: "string" }
|
|
192
|
+
}
|
|
193
|
+
}).values : void 0;
|
|
194
|
+
if (options?.["expected-version"] !== void 0 && options["expected-version"] !== version)
|
|
195
|
+
throw new Error("Installed gateway version changed. Run tsp-gateway install again.");
|
|
196
|
+
const defaults = locations();
|
|
197
|
+
const paths = {
|
|
198
|
+
config: options?.["config-file"] ?? defaults.config,
|
|
199
|
+
state: options?.["state-dir"] ?? defaults.state
|
|
200
|
+
};
|
|
201
|
+
if (!isAbsolute(paths.config) || !isAbsolute(paths.state))
|
|
202
|
+
throw new Error("Gateway storage paths must be absolute");
|
|
203
|
+
return { paths, loginPath: options?.["login-path"] };
|
|
204
|
+
}
|
|
205
|
+
async function openAuthorization(url) {
|
|
206
|
+
const platform = process.platform;
|
|
207
|
+
const result = platform === "darwin" ? await execute("open", [url], process.env) : platform === "win32" ? await execute("rundll32.exe", ["url.dll,FileProtocolHandler", url], process.env) : await execute("xdg-open", [url], process.env);
|
|
208
|
+
if (result.code !== 0) throw new Error("Could not open the authorization browser");
|
|
209
|
+
}
|
|
210
|
+
function gatewayOrigin(config) {
|
|
211
|
+
return validateOrigin(
|
|
212
|
+
process.env["TSP_GATEWAY_ORIGIN"] ?? config.pairing?.origin ?? "https://treestructuredplanning.com"
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
async function configureLaunch(args, config, path) {
|
|
216
|
+
const json = args[1];
|
|
217
|
+
if (!json || args.length !== 2)
|
|
218
|
+
throw new Error("Usage: tsp-gateway configure-launch '<JSON object>'");
|
|
219
|
+
config.launch = decodeLaunchConfig(parseJson(json));
|
|
220
|
+
await saveConfig(path, config);
|
|
221
|
+
console.log("Launch settings saved. The running gateway picks up the change automatically.");
|
|
222
|
+
}
|
|
223
|
+
function connectSummary(outcome, config, version) {
|
|
224
|
+
const machine = config.pairing?.machine_id ?? "this computer";
|
|
225
|
+
const repository = outcome.repository;
|
|
226
|
+
const registeredCount = Object.keys(config.repositories).length;
|
|
227
|
+
return [
|
|
228
|
+
outcome.pairing === "kept" ? `Already paired as ${machine}.` : `Paired as ${machine}.`,
|
|
229
|
+
repository === null ? `No GitHub repository here; kept ${registeredCount} registered ${registeredCount === 1 ? "folder" : "folders"}.` : repository.change === "unchanged" ? `${repository.slug} is already registered.` : repository.change === "moved" ? `Registered ${repository.slug} at this folder in place of its previous folder.` : `Registered ${repository.slug}.`,
|
|
230
|
+
outcome.loginItem === "current" ? `The login item already runs tsp-gateway ${version}.` : `Installed tsp-gateway ${version} as a login item.`
|
|
231
|
+
];
|
|
232
|
+
}
|
|
233
|
+
var VERBS = {
|
|
234
|
+
connect: async ({ args, argument, config, version, host, paths, drivers, executor }) => {
|
|
235
|
+
if (args.length > 2) throw new Error("Usage: tsp-gateway connect [folder]");
|
|
236
|
+
const since = Date.now();
|
|
237
|
+
const outcome = await connect(host, config, argument);
|
|
238
|
+
for (const line of connectSummary(outcome, config, version)) console.log(line);
|
|
239
|
+
const reported = outcome.loginItem === "installed" ? await awaitDaemonConnection(join(paths.state, "status.json"), since) : null;
|
|
240
|
+
await printStatus(
|
|
241
|
+
paths,
|
|
242
|
+
config,
|
|
243
|
+
drivers,
|
|
244
|
+
executor,
|
|
245
|
+
outcome.loginItem === "installed" && reported === null ? "Starting" : void 0
|
|
246
|
+
);
|
|
247
|
+
const remaining = await remainingSteps(drivers, config, outcome.repository);
|
|
248
|
+
console.log(
|
|
249
|
+
remaining.length > 0 ? [
|
|
250
|
+
remaining.length === 1 ? "Remaining step:" : "Remaining steps:",
|
|
251
|
+
...remaining
|
|
252
|
+
].join("\n") : "Connected. Nothing left to do on this computer."
|
|
253
|
+
);
|
|
254
|
+
},
|
|
255
|
+
disconnect: async ({ args, host, config }) => {
|
|
256
|
+
if (args.length !== 1) throw new Error("Usage: tsp-gateway disconnect");
|
|
257
|
+
const outcome = await disconnect(host, config);
|
|
258
|
+
console.log(
|
|
259
|
+
`${outcome.loginItem === "removed" ? "Login item removed." : "No login item was installed."} ${outcome.pairing} Registered folders are kept.`
|
|
260
|
+
);
|
|
261
|
+
},
|
|
262
|
+
pair: async ({ host, config }) => {
|
|
263
|
+
await pairMachine(host, config);
|
|
264
|
+
console.log("Paired. Use tsp-gateway add <folder>, then tsp-gateway install.");
|
|
265
|
+
},
|
|
266
|
+
add: async ({ args, argument, host, config, drivers }) => {
|
|
267
|
+
if (!argument || args.length !== 2) throw new Error("Usage: tsp-gateway add <folder>");
|
|
268
|
+
const registered = await addRepository(host, config, argument);
|
|
269
|
+
console.log(
|
|
270
|
+
`Registered ${registered.slug}. ${drivers.setupInstructions().join(" ")} The running gateway picks up the change automatically.`
|
|
271
|
+
);
|
|
272
|
+
},
|
|
273
|
+
remove: async ({ argument, config, paths }) => {
|
|
274
|
+
if (!argument || !validSlug(argument.toLowerCase()))
|
|
275
|
+
throw new Error("Usage: tsp-gateway remove <owner/name>");
|
|
276
|
+
delete config.repositories[argument.toLowerCase()];
|
|
277
|
+
await saveConfig(paths.config, config);
|
|
278
|
+
console.log("Repository removed. The running gateway picks up the change automatically.");
|
|
279
|
+
},
|
|
280
|
+
"configure-launch": ({ args, config, paths }) => configureLaunch(args, config, paths.config),
|
|
281
|
+
status: ({ paths, config, drivers, executor }) => printStatus(paths, config, drivers, executor),
|
|
282
|
+
install: async ({ host, config, version }) => {
|
|
283
|
+
await installLoginItem(host, config);
|
|
284
|
+
console.log(`Installed tsp-gateway ${version} as a login item.`);
|
|
285
|
+
},
|
|
286
|
+
uninstall: async ({ host }) => {
|
|
287
|
+
console.log(
|
|
288
|
+
`${await removeLoginItem(host) === "removed" ? "Login item removed." : "No login item was installed."} Pairing and folders are retained.`
|
|
289
|
+
);
|
|
290
|
+
},
|
|
291
|
+
unpair: async ({ host, config }) => {
|
|
292
|
+
console.log(await unpairMachine(host, config));
|
|
293
|
+
},
|
|
294
|
+
start: ({ loginPath, paths, config, version }) => runLocal(loginPath, paths, config, version)
|
|
295
|
+
};
|
|
296
|
+
function isVerbName(name) {
|
|
297
|
+
return Object.hasOwn(VERBS, name);
|
|
298
|
+
}
|
|
299
|
+
function verbNamed(name) {
|
|
300
|
+
return name !== void 0 && isVerbName(name) ? VERBS[name] : null;
|
|
301
|
+
}
|
|
302
|
+
async function main(args) {
|
|
303
|
+
const [command, argument] = args;
|
|
304
|
+
const verb = verbNamed(command);
|
|
305
|
+
if (!verb) {
|
|
306
|
+
console.log(
|
|
307
|
+
"Usage: tsp-gateway connect [folder] | disconnect | pair | add <folder> | remove <slug> | configure-launch <json> | status | start | install | uninstall | unpair"
|
|
308
|
+
);
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
const version = package_default.version;
|
|
312
|
+
const { paths, loginPath } = startupOptions(args, version);
|
|
313
|
+
const config = await readConfig(paths.config);
|
|
314
|
+
const environment = { ...process.env, PATH: config.loginPath ?? process.env["PATH"] };
|
|
315
|
+
const executor = new HarnessExecutor(environment, { config: async () => config.launch });
|
|
316
|
+
await verb({
|
|
317
|
+
args,
|
|
318
|
+
argument,
|
|
319
|
+
version,
|
|
320
|
+
paths,
|
|
321
|
+
loginPath,
|
|
322
|
+
config,
|
|
323
|
+
drivers: builtinHarnesses(environment, executor),
|
|
324
|
+
executor,
|
|
325
|
+
host: {
|
|
326
|
+
paths,
|
|
327
|
+
origin: gatewayOrigin(config),
|
|
328
|
+
version,
|
|
329
|
+
node: process.execPath,
|
|
330
|
+
entry: fileURLToPath(import.meta.url),
|
|
331
|
+
env: process.env,
|
|
332
|
+
platform: process.platform,
|
|
333
|
+
home: homedir(),
|
|
334
|
+
cwd: process.cwd(),
|
|
335
|
+
tokens: keychain,
|
|
336
|
+
openBrowser: openAuthorization,
|
|
337
|
+
report: (line) => console.log(line)
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
void main(process.argv.slice(2)).catch((error) => {
|
|
342
|
+
console.error(
|
|
343
|
+
error instanceof Error ? error.message : "Gateway could not complete the command"
|
|
344
|
+
);
|
|
345
|
+
process.exitCode = 1;
|
|
346
|
+
});
|
|
347
|
+
//# sourceMappingURL=cli.js.map
|