@ra3orblade/swarm 0.4.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/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,171 @@
1
+ <p align="center"><img src="docs/art/swarm-icon.svg" alt="Swarm" width="96" height="96"></p>
2
+
3
+ <h1 align="center">Swarm</h1>
4
+
5
+ <p align="center"><b>See every agent. Stop the collisions.</b><br>
6
+ A local-first control plane for AI-agent development on any repository.</p>
7
+
8
+ <p align="center">
9
+ <a href="https://getswarm.vercel.app">Website</a> ·
10
+ <a href="https://getswarm.vercel.app/docs/">Docs</a> ·
11
+ <a href="https://getswarm.vercel.app/#downloads">Downloads</a> ·
12
+ <a href="https://getswarm.vercel.app/changelog">Changelog</a> ·
13
+ <a href="https://github.com/ra3orblade/swarm/issues/new?template=feedback.yml">Feedback</a>
14
+ </p>
15
+
16
+ <p align="center">
17
+ <a href="https://github.com/ra3orblade/swarm/releases/latest"><img alt="Release" src="https://img.shields.io/github/v/release/ra3orblade/swarm?display_name=tag&color=a3e635&labelColor=0e1013"></a>
18
+ <a href="https://www.npmjs.com/package/@ra3orblade/swarm"><img alt="npm" src="https://img.shields.io/npm/v/%40ra3orblade%2Fswarm?color=a3e635&labelColor=0e1013"></a>
19
+ <a href="https://github.com/ra3orblade/swarm/actions/workflows/ci.yml"><img alt="CI" src="https://img.shields.io/github/actions/workflow/status/ra3orblade/swarm/ci.yml?branch=main&label=ci&labelColor=0e1013"></a>
20
+ <a href="https://github.com/ra3orblade/swarm/releases"><img alt="Downloads" src="https://img.shields.io/github/downloads/ra3orblade/swarm/total?label=downloads&color=a3e635&labelColor=0e1013"></a>
21
+ <a href="https://www.npmjs.com/package/@ra3orblade/swarm"><img alt="npm downloads" src="https://img.shields.io/npm/dm/%40ra3orblade%2Fswarm?label=npm%2Fmonth&color=a3e635&labelColor=0e1013"></a>
22
+ <a href="LICENSE"><img alt="Apache-2.0" src="https://img.shields.io/badge/license-Apache--2.0-lightgrey?labelColor=0e1013"></a>
23
+ </p>
24
+
25
+ <p align="center"><a href="https://getswarm.vercel.app"><img src="docs/art/screens/fleet.jpg" alt="Swarm Fleet view — every agent session on the machine, live" width="100%"></a></p>
26
+
27
+ Run more than one [Claude Code](https://claude.com/claude-code) session at a time — or a [Codex CLI](https://github.com/openai/codex) or Grok run on the side — and you lose the thread fast: which session is on which branch, what it's costing, which worktree has uncommitted work nobody owns, why that edit got blocked. Swarm is one daemon that watches every session on your machine — live tool calls, reasoning, token spend, cost — keeps a ledger of who holds which task, worktree and runtime resource, turns the "never do X" prose in `CLAUDE.md` into real permission decisions, and streams all of it to one dashboard.
28
+
29
+ It runs entirely on your machine. No account, no telemetry, works offline. Nothing is added to your repositories.
30
+
31
+ ```sh
32
+ bunx @ra3orblade/swarm setup
33
+ ```
34
+
35
+ > Status: early, but real — observability, task claims, runtime resources, configurable rules, incidents and a cross-forge merge queue are built and dogfooded daily. Spawned agents and verification gates are on the [roadmap](ROADMAP.md).
36
+
37
+ ---
38
+
39
+ ## What you get
40
+
41
+ **Fleet** — every session across every project, live: agent, title, branch, what it's doing right now, model, a trend sparkline, output tokens, context size, cost, age. Filter by agent (Claude Code, Codex, Grok).
42
+
43
+ **Session** — an agent's reasoning and tool calls as a live stream, with cost per turn, cache hit rate, thinking share, tool histogram and the transcript path.
44
+
45
+ <p align="center"><img src="docs/art/screens/session.jpg" alt="Session view — live reasoning stream with a stats panel" width="100%"></p>
46
+
47
+ **Board** — the coordination ledger for a project: task **claims** (each in an isolated git worktree), **worktrees** with branch, dirty/unpushed state and which session is inside, **runtime resources** (ports, dev servers, databases held as named singletons), and **incidents** — every command the rules asked about or denied, with the rule and the command.
48
+
49
+ <p align="center"><img src="docs/art/screens/board.jpg" alt="Board view — worktrees and incidents" width="100%"></p>
50
+
51
+ **Rules** — guardrails on the Bash commands a Claude Code session runs: `shared_tree`, `destructive_git`, `pattern_kill`, `protected_ports`, plus `no_foreign_worktree` and the opt-in `claim_required_to_write` on file writes; each `ask | deny | off` per repo in `.swarm.toml`. A `deny` is returned to Claude Code as a real permission denial. Ports held as resources are protected automatically. Guardrails against accidents, not a sandbox — see [what rules are and aren't](https://getswarm.vercel.app/docs/03-rules-and-config#what-rules-are--and-arent).
52
+
53
+ **PRs** — one merge queue across GitHub and GitLab, read through your already-authenticated `gh` / `glab`. Merge from the dashboard when checks and review are clear. No tokens stored.
54
+
55
+ **Timeline** — session lanes per project, coloured by agent, 3–72 h.
56
+
57
+ <p align="center"><img src="docs/art/screens/timeline.jpg" alt="Timeline view — session lanes per project" width="100%"></p>
58
+
59
+ **Spend & Stats** — cost by project, model and agent, today and all-time; plus the fun numbers: tokens, turns, streaks, activity calendar, words written, what it adds up to in novels and coffee.
60
+
61
+ <p align="center"><img src="docs/art/screens/stats.jpg" alt="Stats view" width="100%"></p>
62
+
63
+ **Multi-agent** — Claude Code via its hooks and transcripts; Codex CLI and Grok by tailing the session logs they already write (`~/.codex`, ACP `updates.jsonl`). Every session is tagged with its agent; Spend breaks down per agent.
64
+
65
+ **Zero instrumentation** — it reads the hooks and transcripts the agents already write. Every table is a real data grid: sort, resize, reorder, filter, persisted layouts. Light and dark themes.
66
+
67
+ ## Install
68
+
69
+ Requires [Bun](https://bun.sh) ≥ 1.3, git, and at least one agent: [Claude Code](https://claude.com/claude-code) (`claude` on your PATH — hooks, rules and MCP need it), [Codex CLI](https://github.com/openai/codex) and/or Grok (observed by tailing their logs; no hooks, so no rules). Optional: `gh` and/or `glab` (authenticated) for the PRs view.
70
+
71
+ ```sh
72
+ bunx @ra3orblade/swarm setup # daemon + hooks + MCP, opens the dashboard
73
+ ```
74
+
75
+ Or install the CLI globally (`bun add -g @ra3orblade/swarm`, then `swarm setup`). Start `claude` in any folder and it appears at **http://127.0.0.1:7777**. The daemon runs in the background and auto-starts when needed; `swarm doctor` checks everything and prints the fix for each gap.
76
+
77
+ Prefer a native window with auto-updates? Get the **desktop app** from the [downloads page](https://getswarm.vercel.app/#downloads) — macOS (signed + notarized), Windows, Linux (`.deb` / `.rpm`). It bundles the daemon; run `bunx @ra3orblade/swarm install` once to add the Claude Code hooks. Building it yourself needs the Rust toolchain: `bun run desktop:dev` / `bun run desktop:build`.
78
+
79
+ Full guide: **[getswarm.vercel.app/docs](https://getswarm.vercel.app/docs/)** — [Getting started](https://getswarm.vercel.app/docs/01-getting-started) · [The dashboard](https://getswarm.vercel.app/docs/02-dashboard) · [Rules and configuration](https://getswarm.vercel.app/docs/03-rules-and-config) · [Claims and worktrees](https://getswarm.vercel.app/docs/04-claims-and-worktrees) · [Runtime resources](https://getswarm.vercel.app/docs/05-runtime-resources) · [Pull requests](https://getswarm.vercel.app/docs/06-pull-requests) · [CLI](https://getswarm.vercel.app/docs/07-cli) · [MCP](https://getswarm.vercel.app/docs/08-mcp) · [Desktop app](https://getswarm.vercel.app/docs/09-desktop-app) · [Privacy & FAQ](https://getswarm.vercel.app/docs/10-privacy-and-faq)
80
+
81
+ ## Rules in 30 seconds
82
+
83
+ Drop a `.swarm.toml` in a repo (commit it — it's how the repo declares its rules). Global defaults live in `~/.swarm/config.toml`. Bad config never takes the daemon down.
84
+
85
+ ```toml
86
+ [rules] # "ask" | "deny" | "off"
87
+ shared_tree = "deny" # broad `git add -A` / `commit -a` while another live session shares the checkout
88
+ destructive_git = "ask" # reset --hard, checkout ., clean -f …
89
+ pattern_kill = "ask" # pkill -f and friends
90
+ protected_ports = "ask" # freeing a port listed below (or held as a resource)
91
+ no_foreign_worktree = "ask" # file writes into a worktree another claim holds
92
+ claim_required_to_write = "off" # opt-in: writes to the shared checkout need a claim
93
+
94
+ [rules.protected]
95
+ ports = [5432]
96
+ ```
97
+
98
+ Every `ask` and `deny` is recorded as an incident on the Board. Reference: [Rules and configuration](https://getswarm.vercel.app/docs/03-rules-and-config).
99
+
100
+ ## CLI
101
+
102
+ ```sh
103
+ swarm setup # first-run: daemon + hooks + dashboard
104
+ swarm start | stop | restart # manage the background daemon
105
+ swarm status # live sessions in the terminal
106
+ swarm doctor # check setup, print the fix for each gap
107
+ swarm add <path> · ls · ui # pin a project · list projects · open the dashboard
108
+ swarm tail # follow the live event stream
109
+
110
+ swarm claim <task> [--owner n] # claim a task in a fresh isolated worktree (fail-closed)
111
+ swarm renew | release <task> # extend the lease · release + remove the worktree
112
+ swarm claims · reap # list claims · release abandoned ones (keeps those holding work)
113
+ swarm res ls | acquire <name> [--pid n] [--port n] | release <name> [--force]
114
+ swarm stats [--json] # all-time totals, streak, records
115
+
116
+ swarm install | uninstall # add/remove Swarm hooks in ~/.claude/settings.json
117
+ ```
118
+
119
+ ## For agents (MCP)
120
+
121
+ `swarm setup` registers an MCP server, so an agent can coordinate itself instead of you running the CLI:
122
+
123
+ - `swarm_status` — what's claimed, who's live, what's held in this project
124
+ - `swarm_claim` / `swarm_renew` / `swarm_release` / `swarm_reap` — task claims in isolated worktrees (fail-closed)
125
+ - `swarm_acquire_resource` / `swarm_release_resource` / `swarm_resources` — hold a port, dev server or database as a named singleton; held ports are protected from other agents automatically
126
+
127
+ ## How it works
128
+
129
+ ```
130
+ Claude Code sessions ──hooks──▶ swarm-hook ──┐
131
+ (any folder, any repo) │
132
+ transcripts (JSONL) ──────┼──▶ swarmd ──▶ SQLite (~/.swarm)
133
+ Codex CLI ─── ~/.codex rollout logs ────────────┤ │
134
+ Grok ─── ACP updates.jsonl ────────────────┘ └──▶ SSE ──▶ dashboard · CLI · MCP
135
+
136
+ rules engine (ask / deny → incidents)
137
+ ```
138
+
139
+ - **Identity** is the git common dir, so every worktree of a repo maps to one project.
140
+ - **State** lives in `~/.swarm/` — never in your repositories. Uninstalling leaves your repos untouched.
141
+ - **Hooks** are installed once at the Claude Code user level, so every session on the machine reports in. The hook fails open: if the daemon is down, nothing blocks your work.
142
+
143
+ Design docs (architecture, data model, protocol, interface, roadmap) are rendered at [getswarm.vercel.app/docs/design](https://getswarm.vercel.app/docs/design/) and live in [`docs/`](docs/00-index.md).
144
+
145
+ ## Privacy
146
+
147
+ Everything is local. Swarm reads the hook payloads and transcript files (Claude Code, Codex, Grok) that already exist on your disk, stores derived state in `~/.swarm/swarm.db`, and serves a dashboard on `127.0.0.1`. Optional outbound paths, all under your control: fetching model prices from the public LiteLLM list (`SWARM_OFFLINE=1` skips it); the PRs view shelling out to your already-authenticated `gh` / `glab` (Swarm stores no forge tokens); and the desktop app asking GitHub Releases for updates when you click *Check for Updates…*. No data about your sessions leaves your machine.
148
+
149
+ ## Configuration
150
+
151
+ | Env | Default | Meaning |
152
+ |-----|---------|---------|
153
+ | `SWARM_PORT` | `7777` | daemon port (`SWARM_PORT` > `[daemon].port` in config > `7777`) |
154
+ | `SWARM_HOME` | `~/.swarm` | state directory |
155
+ | `SWARM_URL` | derived | override the daemon URL clients use |
156
+ | `SWARM_OFFLINE` | – | `1` disables the pricing fetch |
157
+ | `SWARM_STRICT_PORT` | – | `1` fails instead of falling back to a free port |
158
+
159
+ Model prices can be overridden in `~/.swarm/pricing.json`. Full reference: [docs/13-config.md](docs/13-config.md).
160
+
161
+ ## Contributing
162
+
163
+ Issues and PRs welcome — see [CONTRIBUTING.md](CONTRIBUTING.md), the [roadmap](ROADMAP.md) and the [changelog](CHANGELOG.md). Found something confusing? Use the **Feedback** button in the dashboard or [open an issue](https://github.com/ra3orblade/swarm/issues/new/choose). By participating you agree to the [Code of Conduct](CODE_OF_CONDUCT.md).
164
+
165
+ ```sh
166
+ git clone https://github.com/ra3orblade/swarm.git && cd swarm && bun install
167
+ bun run dev # daemon with hot reload
168
+ bun run test # the suite CI runs
169
+ ```
170
+
171
+ Apache-2.0.
@@ -0,0 +1,52 @@
1
+ #!/usr/bin/env bun
2
+ // @bun
3
+
4
+ // packages/client/src/daemon.ts
5
+ import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "fs";
6
+ import { homedir } from "os";
7
+ import { join } from "path";
8
+ function swarmHome() {
9
+ return process.env.SWARM_HOME ?? join(homedir(), ".swarm");
10
+ }
11
+ var DEFAULT_PORT = Number(process.env.SWARM_PORT ?? 7777);
12
+ var infoFile = () => join(swarmHome(), "daemon.json");
13
+ function readDaemonInfo() {
14
+ const file = infoFile();
15
+ if (!existsSync(file))
16
+ return null;
17
+ try {
18
+ return JSON.parse(readFileSync(file, "utf8"));
19
+ } catch {
20
+ return null;
21
+ }
22
+ }
23
+ function resolveBaseUrl(explicit) {
24
+ const raw = explicit ?? process.env.SWARM_URL ?? readDaemonInfo()?.url ?? `http://127.0.0.1:${DEFAULT_PORT}`;
25
+ return raw.replace(/\/$/, "");
26
+ }
27
+ // packages/hook/src/bin.ts
28
+ var event = process.argv[2] ?? "Unknown";
29
+ var input = await Bun.stdin.text();
30
+ var timeout = Number(process.env.SWARM_HOOK_TIMEOUT_MS ?? 400);
31
+ var post = async (base) => {
32
+ const r = await fetch(`${base}/v1/hook/${event}`, {
33
+ method: "POST",
34
+ headers: { "content-type": "application/json" },
35
+ body: input || "{}",
36
+ signal: AbortSignal.timeout(timeout)
37
+ });
38
+ return r.ok ? await r.text() || "{}" : null;
39
+ };
40
+ var base = resolveBaseUrl();
41
+ var fallback = `http://127.0.0.1:${DEFAULT_PORT}`;
42
+ var out = null;
43
+ try {
44
+ out = await post(base);
45
+ } catch {}
46
+ if (out === null && base !== fallback) {
47
+ try {
48
+ out = await post(fallback);
49
+ } catch {}
50
+ }
51
+ process.stdout.write(`${out ?? "{}"}
52
+ `);