airband 0.0.3 → 0.0.5

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/airband-hook.js CHANGED
@@ -982,8 +982,149 @@ function toolNotice(toolName, toolInput, toolResponse, cwd) {
982
982
  return `\u21AA Airband${where}: ${what}${tail}`;
983
983
  }
984
984
 
985
- // src/core/writing.ts
985
+ // src/station/client.ts
986
986
  var fs8 = __toESM(require("node:fs"));
987
+ var http = __toESM(require("node:http"));
988
+ var stationFile = () => airbandFile("station.json");
989
+ var DECISION_MS = 11e4;
990
+ var CONNECT_MS = 400;
991
+ function readStationInfo() {
992
+ try {
993
+ const info = JSON.parse(fs8.readFileSync(stationFile(), "utf8"));
994
+ if (!info.port || !info.token) return void 0;
995
+ try {
996
+ process.kill(info.pid, 0);
997
+ } catch (e) {
998
+ if (e.code === "ESRCH") return void 0;
999
+ }
1000
+ return info;
1001
+ } catch {
1002
+ return void 0;
1003
+ }
1004
+ }
1005
+ function stationPost(info, route, body, budgetMs) {
1006
+ return new Promise((resolve6) => {
1007
+ const data = Buffer.from(JSON.stringify(body));
1008
+ let done = false;
1009
+ const finish = (v) => {
1010
+ if (done) return;
1011
+ done = true;
1012
+ clearTimeout(timer);
1013
+ resolve6(v);
1014
+ };
1015
+ const req = http.request(
1016
+ { host: "127.0.0.1", port: info.port, path: route, method: "POST", headers: { "content-type": "application/json", "content-length": data.length, "x-airband-token": info.token } },
1017
+ (res) => {
1018
+ let text = "";
1019
+ res.setEncoding("utf8");
1020
+ res.on("data", (c) => text += c);
1021
+ res.on("end", () => {
1022
+ try {
1023
+ finish(res.statusCode === 200 ? JSON.parse(text) : void 0);
1024
+ } catch {
1025
+ finish(void 0);
1026
+ }
1027
+ });
1028
+ res.on("error", () => finish(void 0));
1029
+ }
1030
+ );
1031
+ const timer = setTimeout(() => {
1032
+ req.destroy();
1033
+ finish(void 0);
1034
+ }, budgetMs);
1035
+ req.setTimeout(CONNECT_MS, () => {
1036
+ if (!req.socket || req.socket.connecting) {
1037
+ req.destroy();
1038
+ finish(void 0);
1039
+ }
1040
+ });
1041
+ req.on("socket", (s) => s.once("connect", () => req.setTimeout(0)));
1042
+ req.on("error", () => finish(void 0));
1043
+ req.end(data);
1044
+ });
1045
+ }
1046
+ async function askStation(ask, info = readStationInfo()) {
1047
+ if (!info || info.approvals === false || process.env.AIRBAND_NO_STATION) return void 0;
1048
+ const wait = Math.min(info.waitMs ?? DECISION_MS, DECISION_MS);
1049
+ const res = await stationPost(info, "/api/hook/permission", { ...ask, waitMs: wait }, wait + 2e3);
1050
+ return res?.answer;
1051
+ }
1052
+
1053
+ // src/station/reply.ts
1054
+ var TAKES_DECISIONS = ["claude", "codex"];
1055
+ function questionsOf(toolName, input) {
1056
+ if (toolName !== "AskUserQuestion" || !input || typeof input !== "object") return void 0;
1057
+ const qs = input.questions;
1058
+ if (!Array.isArray(qs) || !qs.length) return void 0;
1059
+ const out2 = [];
1060
+ for (const q of qs) {
1061
+ if (!q || typeof q !== "object") return void 0;
1062
+ const { question, header, options, multiSelect } = q;
1063
+ if (typeof question !== "string" || !Array.isArray(options)) return void 0;
1064
+ const opts = options.map((o) => o && typeof o === "object" ? o : {}).filter((o) => typeof o.label === "string").map((o) => ({ label: o.label, ...typeof o.description === "string" ? { description: o.description } : {} }));
1065
+ out2.push({ question, ...typeof header === "string" ? { header } : {}, options: opts, ...multiSelect === true ? { multiSelect: true } : {} });
1066
+ }
1067
+ return out2;
1068
+ }
1069
+ function answersFit(questions, answers) {
1070
+ if (!questions.length || Object.keys(answers).length !== questions.length) return false;
1071
+ return questions.every((q) => {
1072
+ const labels = q.options.map((o) => o.label);
1073
+ const pick = answers[q.question];
1074
+ if (q.multiSelect) {
1075
+ if (!Array.isArray(pick) || !pick.length || new Set(pick).size !== pick.length) return false;
1076
+ return pick.every((p) => labels.includes(p));
1077
+ }
1078
+ return typeof pick === "string" && labels.includes(pick);
1079
+ });
1080
+ }
1081
+ function permissionReply(agent, toolName, input, answer) {
1082
+ if (!answer || !TAKES_DECISIONS.includes(agent)) return void 0;
1083
+ const wrap = (decision) => JSON.stringify({ hookSpecificOutput: { hookEventName: "PermissionRequest", decision } });
1084
+ switch (answer.decision) {
1085
+ case "allow":
1086
+ case "always": {
1087
+ const questions = agent === "claude" ? questionsOf(toolName, input) : void 0;
1088
+ if (questions) {
1089
+ if (!answer.answers || !answersFit(questions, answer.answers)) return void 0;
1090
+ return wrap({ behavior: "allow", updatedInput: { ...input, answers: answer.answers } });
1091
+ }
1092
+ return wrap({ behavior: "allow" });
1093
+ }
1094
+ case "deny":
1095
+ return wrap({ behavior: "deny", message: (answer.message ?? "").trim() || "The person said no from Airband." });
1096
+ default:
1097
+ return void 0;
1098
+ }
1099
+ }
1100
+ function describeRequest(toolName, input) {
1101
+ const i = input && typeof input === "object" ? input : {};
1102
+ const str = (v) => typeof v === "string" ? v : void 0;
1103
+ const name = toolName ?? "a tool";
1104
+ if (toolName === "Bash" || toolName === "shell" || toolName === "exec_command" || toolName === "local_shell") {
1105
+ const cmd = str(i.command) ?? (Array.isArray(i.command) ? i.command.join(" ") : void 0) ?? str(i.cmd);
1106
+ return { title: "Wants to run a command", ...cmd ? { detail: cmd } : {} };
1107
+ }
1108
+ if (toolName === "Write" || toolName === "Edit" || toolName === "MultiEdit" || toolName === "NotebookEdit") {
1109
+ const file = str(i.file_path) ?? str(i.notebook_path);
1110
+ return { title: toolName === "Write" ? "Wants to write a file" : "Wants to edit a file", ...file ? { detail: file } : {} };
1111
+ }
1112
+ if (toolName === "WebFetch") return { title: "Wants to open a web page", ...str(i.url) ? { detail: str(i.url) } : {} };
1113
+ if (toolName === "WebSearch") return { title: "Wants to search the web", ...str(i.query) ? { detail: str(i.query) } : {} };
1114
+ if (toolName === "AskUserQuestion") {
1115
+ const first = questionsOf(toolName, input)?.[0]?.question;
1116
+ return { title: "Has a question for you", ...first ? { detail: first } : {} };
1117
+ }
1118
+ if (toolName === "ExitPlanMode") return { title: "Has a plan ready for you to approve", ...str(i.plan) ? { detail: str(i.plan).slice(0, 400) } : {} };
1119
+ if (toolName?.startsWith("mcp__")) {
1120
+ const [, server, tool] = toolName.split("__");
1121
+ return { title: `Wants to use ${tool ?? "a tool"} (${server ?? "MCP"})` };
1122
+ }
1123
+ return { title: `Wants to use ${name}` };
1124
+ }
1125
+
1126
+ // src/core/writing.ts
1127
+ var fs9 = __toESM(require("node:fs"));
987
1128
  var path7 = __toESM(require("node:path"));
988
1129
  var PROJECT_FILE = ".airband.json";
989
1130
  function projectFile(cwd) {
@@ -991,8 +1132,8 @@ function projectFile(cwd) {
991
1132
  let dir2 = path7.resolve(cwd);
992
1133
  for (let i = 0; i < 12; i++) {
993
1134
  const f = path7.join(dir2, PROJECT_FILE);
994
- if (fs8.existsSync(f)) return f;
995
- if (fs8.existsSync(path7.join(dir2, ".git"))) return void 0;
1135
+ if (fs9.existsSync(f)) return f;
1136
+ if (fs9.existsSync(path7.join(dir2, ".git"))) return void 0;
996
1137
  const up = path7.dirname(dir2);
997
1138
  if (up === dir2) return void 0;
998
1139
  dir2 = up;
@@ -1260,7 +1401,7 @@ function checkWrittenDoc(doc) {
1260
1401
  if (!found.length) return found;
1261
1402
  let offset = 0;
1262
1403
  try {
1263
- const whole = fs8.readFileSync(doc.file, "utf8");
1404
+ const whole = fs9.readFileSync(doc.file, "utf8");
1264
1405
  const at = whole.indexOf(doc.text.split("\n").find((l) => l.trim().length > 8) ?? doc.text);
1265
1406
  const firstLong = doc.text.split("\n").findIndex((l) => l.trim().length > 8);
1266
1407
  if (at >= 0) offset = whole.slice(0, at).split("\n").length - 1 - Math.max(0, firstLong);
@@ -1288,7 +1429,7 @@ function shellValues(command, flags, cwd) {
1288
1429
  const name = file && (file[2] ?? file[3] ?? file[4]);
1289
1430
  if (name && name !== "-") {
1290
1431
  try {
1291
- out2.push(fs8.readFileSync(path7.resolve(cwd ?? ".", name), "utf8").slice(0, 64e3));
1432
+ out2.push(fs9.readFileSync(path7.resolve(cwd ?? ".", name), "utf8").slice(0, 64e3));
1292
1433
  } catch {
1293
1434
  }
1294
1435
  }
@@ -1351,26 +1492,10 @@ function holdOnce(sessionId, text, now = Date.now()) {
1351
1492
  }
1352
1493
  return true;
1353
1494
  }
1354
- var logFile = () => airbandFile("writing-log.jsonl");
1355
- var LOG_KEEP = 200;
1356
- function logWriting(e) {
1357
- try {
1358
- const file = logFile();
1359
- fs8.mkdirSync(path7.dirname(file), { recursive: true });
1360
- fs8.appendFileSync(file, `${JSON.stringify({ ...e, findings: e.findings.slice(0, 12) })}
1361
- `);
1362
- if (fs8.statSync(file).size > 4e5) {
1363
- const lines = fs8.readFileSync(file, "utf8").split("\n").filter(Boolean);
1364
- writeFileAtomic(file, `${lines.slice(-LOG_KEEP).join("\n")}
1365
- `);
1366
- }
1367
- } catch {
1368
- }
1369
- }
1370
1495
 
1371
1496
  // src/hook/airband-hook.ts
1372
1497
  var EVENT_ALIASES = {
1373
- claude: {},
1498
+ claude: { PermissionRequest: "Notification" },
1374
1499
  codex: { PermissionRequest: "Notification" },
1375
1500
  gemini: { BeforeAgent: "UserPromptSubmit", AfterAgent: "Stop", AfterTool: "PostToolUse", BeforeTool: "PreToolUse" }
1376
1501
  };
@@ -1400,6 +1525,10 @@ function record(input, now = Date.now()) {
1400
1525
  if (!id || !status || !safeId(id)) return;
1401
1526
  const entry = { status, at: now, event: input.hook_event_name, cwd: input.cwd };
1402
1527
  if (status === "waiting" && input.message) entry.message = input.message.slice(0, 200);
1528
+ else if (status === "waiting" && input.tool_name) {
1529
+ const d = describeRequest(input.tool_name, input.tool_input);
1530
+ entry.message = (d.detail ? `${d.title}: ${d.detail}` : d.title).slice(0, 200);
1531
+ }
1403
1532
  writeFileAtomic(path8.join(statusDir(), `${id}.json`), JSON.stringify(entry));
1404
1533
  }
1405
1534
  function readSince(tool, file, cursor) {
@@ -1512,6 +1641,7 @@ function out(tool, event, context, notice) {
1512
1641
  return Object.keys(o).length ? JSON.stringify(o) : void 0;
1513
1642
  }
1514
1643
  function handle(raw, now = Date.now(), tool = "claude") {
1644
+ if (process.env.AIRBAND_COMPANION) return void 0;
1515
1645
  const input = normalise2(raw, tool);
1516
1646
  record(input, now);
1517
1647
  switch (input.hook_event_name) {
@@ -1587,7 +1717,6 @@ function docCheck(input, tool) {
1587
1717
  ].filter(Boolean).join("\n");
1588
1718
  const o = { hookSpecificOutput: { hookEventName: tool === "gemini" ? "AfterTool" : "PostToolUse", additionalContext: context } };
1589
1719
  if (tool === "claude" && noticesOn()) o.systemMessage = `\u21AA Airband checked ${name}: ${summarise(found)}`;
1590
- logWriting({ at: Date.now(), what: name, agent: tool, cwd: input.cwd, action: "fix", summary: summarise(found), findings: found });
1591
1720
  return JSON.stringify(o);
1592
1721
  } catch {
1593
1722
  return void 0;
@@ -1609,7 +1738,6 @@ function sendCheck(input, tool) {
1609
1738
  found.length > 12 ? `\u2026 and ${found.length - 12} more.` : "",
1610
1739
  `Rewrite it and send it again, keeping every fact, name, date and link. If the person asked for this exact wording, send it again unchanged: it will go through.`
1611
1740
  ].filter(Boolean).join("\n");
1612
- logWriting({ at: Date.now(), what: o.where, agent: tool, cwd: input.cwd, action: "held", summary: summarise(found), findings: found });
1613
1741
  if (tool === "gemini") return JSON.stringify({ decision: "deny", reason });
1614
1742
  const out2 = { hookSpecificOutput: { hookEventName: "PreToolUse", permissionDecision: "deny", permissionDecisionReason: reason } };
1615
1743
  if (noticesOn()) out2.systemMessage = `\u21AA Airband held a ${o.where} to fix first: ${summarise(found)}`;
@@ -1618,6 +1746,21 @@ function sendCheck(input, tool) {
1618
1746
  return void 0;
1619
1747
  }
1620
1748
  }
1749
+ async function permission(raw, tool, now = Date.now()) {
1750
+ try {
1751
+ handle(raw, now, tool);
1752
+ } catch {
1753
+ }
1754
+ if (process.env.AIRBAND_COMPANION || tool !== "claude" && tool !== "codex") return void 0;
1755
+ const answer = await askStation({ agent: tool, session_id: raw.session_id, cwd: raw.cwd, transcript_path: raw.transcript_path, tool_name: raw.tool_name, tool_input: raw.tool_input });
1756
+ if (answer && raw.session_id && safeId(raw.session_id)) {
1757
+ try {
1758
+ writeFileAtomic(path8.join(statusDir(), `${raw.session_id}.json`), JSON.stringify({ status: "working", at: Date.now(), event: "PermissionAnswered", cwd: raw.cwd }));
1759
+ } catch {
1760
+ }
1761
+ }
1762
+ return permissionReply(tool, raw.tool_name, raw.tool_input, answer);
1763
+ }
1621
1764
  function main() {
1622
1765
  const arg = (process.argv[2] ?? "claude").toLowerCase();
1623
1766
  const tool = arg === "codex" || arg === "gemini" ? arg : "claude";
@@ -1625,14 +1768,33 @@ function main() {
1625
1768
  process.stdin.setEncoding("utf8");
1626
1769
  process.stdin.on("data", (chunk) => raw += chunk);
1627
1770
  process.stdin.on("end", () => {
1771
+ let input;
1772
+ try {
1773
+ input = JSON.parse(raw);
1774
+ } catch {
1775
+ process.exit(0);
1776
+ }
1777
+ if (input.hook_event_name === "PermissionRequest") {
1778
+ clearTimeout(limit);
1779
+ setTimeout(() => process.exit(0), 115e3).unref();
1780
+ void permission(input, tool).then(
1781
+ (out2) => {
1782
+ if (out2) process.stdout.write(out2);
1783
+ process.exit(0);
1784
+ },
1785
+ () => process.exit(0)
1786
+ );
1787
+ return;
1788
+ }
1628
1789
  try {
1629
- const out2 = handle(JSON.parse(raw), Date.now(), tool);
1790
+ const out2 = handle(input, Date.now(), tool);
1630
1791
  if (out2) process.stdout.write(out2);
1631
1792
  } catch {
1632
1793
  }
1633
1794
  process.exit(0);
1634
1795
  });
1635
- setTimeout(() => process.exit(0), 4e3).unref();
1796
+ const limit = setTimeout(() => process.exit(0), 4e3);
1797
+ limit.unref();
1636
1798
  }
1637
1799
 
1638
1800
  // src/hook/bin.ts
package/airband-mcp.js CHANGED
@@ -44998,7 +44998,7 @@ var INSTRUCTIONS = [
44998
44998
  "- When the person refers to earlier work or another chat (what was done, which chat changed something, what happened while they were away, a ticket like ENG-142, a PR like #128), call airband_flightlog: with query to search every chat, or with chat to read one.",
44999
44999
  "- airband_notes holds notes chats left for this project; read them before working in an unfamiliar area, and save one when you learn something the next chat should know."
45000
45000
  ].join("\n");
45001
- var server = new McpServer({ name: "airband", version: "0.0.3" }, { instructions: INSTRUCTIONS });
45001
+ var server = new McpServer({ name: "airband", version: "0.0.5" }, { instructions: INSTRUCTIONS });
45002
45002
  var text = (t, isError = false) => ({ content: [{ type: "text", text: t }], ...isError ? { isError } : {} });
45003
45003
  var maps = /* @__PURE__ */ new Map();
45004
45004
  async function freshMap(folder = root) {