pushary 1.9.178 → 1.9.180

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 (145) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/{api-Y4XA3RLD.js → api-UDDZRXXB.js} +21 -11
  3. package/dist/bin/pushary-audit.js +3 -3
  4. package/dist/bin/pushary-bell-hook.js +28 -28
  5. package/dist/bin/pushary-bell.js +12 -12
  6. package/dist/bin/pushary-calls.js +15 -15
  7. package/dist/bin/pushary-claude.js +27 -27
  8. package/dist/bin/pushary-clean.js +42 -40
  9. package/dist/bin/pushary-codex-bridge.js +23 -23
  10. package/dist/bin/pushary-codex-hook.js +108 -54
  11. package/dist/bin/pushary-codex-session.js +8 -8
  12. package/dist/bin/pushary-codex.js +16 -16
  13. package/dist/bin/pushary-connect.js +35 -34
  14. package/dist/bin/pushary-cowork.js +5 -5
  15. package/dist/bin/pushary-daemon.js +46 -45
  16. package/dist/bin/pushary-disconnect.js +33 -31
  17. package/dist/bin/pushary-display-hook.js +36 -26
  18. package/dist/bin/pushary-doctor.js +47 -46
  19. package/dist/bin/pushary-elicitation-hook.js +28 -28
  20. package/dist/bin/pushary-gemini-bridge.js +18 -18
  21. package/dist/bin/pushary-gemini-hook.js +32 -32
  22. package/dist/bin/pushary-hook.js +36 -36
  23. package/dist/bin/pushary-login.js +22 -22
  24. package/dist/bin/pushary-logout.js +30 -29
  25. package/dist/bin/pushary-mcp.js +12 -12
  26. package/dist/bin/pushary-mode.js +16 -16
  27. package/dist/bin/pushary-notification-hook.js +27 -27
  28. package/dist/bin/pushary-opencode-bridge.js +19 -19
  29. package/dist/bin/pushary-opencode-hook.js +34 -33
  30. package/dist/bin/pushary-permission-denied-hook.js +36 -36
  31. package/dist/bin/pushary-permission-hook.js +36 -36
  32. package/dist/bin/pushary-post-hook.js +28 -28
  33. package/dist/bin/pushary-prompt-hook.js +27 -27
  34. package/dist/bin/pushary-session-end-hook.js +27 -27
  35. package/dist/bin/pushary-session-start-hook.js +39 -38
  36. package/dist/bin/pushary-sessions.js +14 -14
  37. package/dist/bin/pushary-setup.js +55 -54
  38. package/dist/bin/pushary-stats.js +5 -5
  39. package/dist/bin/pushary-status.js +36 -35
  40. package/dist/bin/pushary-stop-hook.js +27 -27
  41. package/dist/bin/pushary-stopfailure-hook.js +27 -27
  42. package/dist/bin/pushary-suggestions.js +16 -16
  43. package/dist/bin/pushary-transcript-register.js +17 -17
  44. package/dist/bin/pushary-transcripts.js +25 -25
  45. package/dist/bin/pushary-upgrade.js +44 -43
  46. package/dist/bin/pushary-wait.js +16 -16
  47. package/dist/bin/pushary.js +7 -7
  48. package/dist/{chunk-IK442FLD.js → chunk-255TJOUT.js} +4 -4
  49. package/dist/{chunk-5I7OXXV6.js → chunk-2C5DS3GU.js} +2 -2
  50. package/dist/{chunk-FIFFXOEQ.js → chunk-2EWBZQHF.js} +3 -3
  51. package/dist/{chunk-O2FHVZCR.js → chunk-2RG454N6.js} +4 -4
  52. package/dist/{chunk-5XUN2ATK.js → chunk-3GL34K67.js} +8 -8
  53. package/dist/{chunk-GSOMT5RJ.js → chunk-4XNQOTVR.js} +3 -3
  54. package/dist/{chunk-MTZOVCWL.js → chunk-4XQN44EO.js} +1 -1
  55. package/dist/{chunk-C3MRJDTL.js → chunk-5JR6LPS2.js} +1 -1
  56. package/dist/{chunk-IWOP6BX2.js → chunk-5Q3KXXT3.js} +6 -6
  57. package/dist/{chunk-WYNYVOJ2.js → chunk-67IYCZY2.js} +5 -5
  58. package/dist/{chunk-SUVMKXJM.js → chunk-76AS4CFU.js} +11 -11
  59. package/dist/{chunk-DGWLGPR3.js → chunk-A7MLOLAM.js} +5 -5
  60. package/dist/{chunk-WBSJ2GNP.js → chunk-AFH5LR76.js} +1 -1
  61. package/dist/{chunk-7PSU6TXK.js → chunk-AG3BUNOK.js} +3 -3
  62. package/dist/{chunk-2CKP2FXL.js → chunk-B4K5OVOE.js} +3 -3
  63. package/dist/{chunk-IMZ2O6I7.js → chunk-BRFHSICQ.js} +1 -1
  64. package/dist/{chunk-OI4JRC3E.js → chunk-CEHLYLLC.js} +2 -2
  65. package/dist/{chunk-247ADQBU.js → chunk-CGVQC5EF.js} +7 -7
  66. package/dist/{chunk-OB3KVRKZ.js → chunk-CHG3NBBJ.js} +2 -2
  67. package/dist/{chunk-PO6UUHBD.js → chunk-CVJ6H2VK.js} +1 -1
  68. package/dist/{chunk-JWZZO6T5.js → chunk-CXBOEYV6.js} +47 -34
  69. package/dist/{chunk-GWYSHO6R.js → chunk-D5WXTN2C.js} +3 -3
  70. package/dist/{chunk-DKZJDNTJ.js → chunk-DULV2NZK.js} +2 -2
  71. package/dist/{chunk-PDIPCZRU.js → chunk-E3TGXTDU.js} +2 -2
  72. package/dist/{chunk-ESCQRNNN.js → chunk-EOGOZ6XB.js} +4 -4
  73. package/dist/{chunk-HZ5N2WGD.js → chunk-EVJ53Y2E.js} +4 -4
  74. package/dist/{chunk-L7OCJAEF.js → chunk-FR6YVSPK.js} +5 -5
  75. package/dist/{chunk-T6PQ47XZ.js → chunk-FSIIKAGP.js} +3 -3
  76. package/dist/{chunk-WBPHMW55.js → chunk-HPSSY25E.js} +2 -2
  77. package/dist/{chunk-L2WALXZE.js → chunk-IEWZ4O74.js} +5 -5
  78. package/dist/{chunk-AIBJGDBE.js → chunk-IMGGR66U.js} +2 -2
  79. package/dist/{chunk-Q356XS2F.js → chunk-INTSTZH4.js} +1 -1
  80. package/dist/{chunk-E2JUP323.js → chunk-IQJL5HY5.js} +2 -2
  81. package/dist/{chunk-3VM7CX4Z.js → chunk-ITLAO546.js} +2 -2
  82. package/dist/{chunk-WP52TPG6.js → chunk-J45L73O6.js} +5 -5
  83. package/dist/{chunk-52U74DDW.js → chunk-JBAFH5Q2.js} +2 -2
  84. package/dist/{chunk-B4R55F4P.js → chunk-JJNBX3SP.js} +3 -3
  85. package/dist/{chunk-3CLTRY7V.js → chunk-JQCAB544.js} +3 -3
  86. package/dist/{chunk-4DMRIGY5.js → chunk-K2H2XC52.js} +4 -4
  87. package/dist/{chunk-4KSQRCH5.js → chunk-K4IMFKPP.js} +6 -6
  88. package/dist/{chunk-NNVIWYDZ.js → chunk-KFAYDRWP.js} +3 -3
  89. package/dist/{chunk-CN7AHMS3.js → chunk-KIR4VFWX.js} +5 -5
  90. package/dist/{chunk-JE42QYDU.js → chunk-LRA423VV.js} +1 -1
  91. package/dist/chunk-LSR6SAV7.js +890 -0
  92. package/dist/{chunk-W4LIY2RO.js → chunk-M7KHET4S.js} +1 -1
  93. package/dist/{chunk-7EH5V64U.js → chunk-MQKEYMX4.js} +5 -5
  94. package/dist/{chunk-2O5VF2QP.js → chunk-N7EBHNYA.js} +5 -5
  95. package/dist/{chunk-AQEJRCRK.js → chunk-NQXVL6LF.js} +1 -1
  96. package/dist/{chunk-KQRATFYV.js → chunk-O4IWURQP.js} +1 -1
  97. package/dist/{chunk-7C2YDR2P.js → chunk-O4IYGRNI.js} +2 -2
  98. package/dist/{chunk-FA4MQUX3.js → chunk-OJWADO7D.js} +4 -4
  99. package/dist/{chunk-IMVJ3H2C.js → chunk-OOA65NQG.js} +18 -18
  100. package/dist/{chunk-RKWA5PRV.js → chunk-OTY6O2LK.js} +1 -1
  101. package/dist/{chunk-3GE7GJMQ.js → chunk-PEIQT6KC.js} +2 -2
  102. package/dist/{chunk-6CIKM5GN.js → chunk-PTV6A5SZ.js} +1 -1
  103. package/dist/{chunk-DITKCHAY.js → chunk-Q7PIFWTG.js} +1 -1
  104. package/dist/{chunk-FEFIMTBN.js → chunk-QZNC5XOW.js} +4 -4
  105. package/dist/{chunk-NBMWRS3J.js → chunk-R2PK26FR.js} +2 -2
  106. package/dist/{chunk-NCXJE3LY.js → chunk-R4TTSINC.js} +1 -1
  107. package/dist/{chunk-25PD2NWC.js → chunk-RE3TYSP7.js} +3 -3
  108. package/dist/{chunk-ASMMRKHJ.js → chunk-RKYKCR22.js} +6 -6
  109. package/dist/{chunk-2VQNNQCL.js → chunk-RNVHH6AN.js} +1 -1
  110. package/dist/{chunk-3F6SGTZP.js → chunk-RY4HYXOC.js} +90 -21
  111. package/dist/{chunk-426PTJL4.js → chunk-SNKG7NMV.js} +38 -10
  112. package/dist/{chunk-3WK3ZOMA.js → chunk-ULYZX6VZ.js} +1 -1
  113. package/dist/{chunk-XXUNYVUE.js → chunk-UXPO3OEC.js} +2 -2
  114. package/dist/{chunk-CSY7A6UE.js → chunk-UZMMHZHU.js} +13 -11
  115. package/dist/{chunk-RCXQPIRI.js → chunk-VD3Q2A5J.js} +1 -1
  116. package/dist/{chunk-55LUC2XS.js → chunk-VKZPDU25.js} +64 -15
  117. package/dist/{chunk-A767YPE5.js → chunk-VOONPEA7.js} +5 -5
  118. package/dist/{chunk-OAK44Y4T.js → chunk-VRPVNOHT.js} +1 -1
  119. package/dist/chunk-VT4JY2AI.js +77 -0
  120. package/dist/{chunk-6KHVI5GM.js → chunk-VTWMIYBM.js} +4 -4
  121. package/dist/{chunk-S4RR2SW2.js → chunk-VWZCVHJG.js} +1 -1
  122. package/dist/{chunk-5ATP6NKJ.js → chunk-XDZ3O7RV.js} +9 -9
  123. package/dist/{chunk-OMHNCIKL.js → chunk-XID6PO4T.js} +4 -4
  124. package/dist/{chunk-NA3GEZG4.js → chunk-YH6QFABF.js} +6 -6
  125. package/dist/{chunk-LPUBT3LM.js → chunk-YQMNDBJD.js} +4 -4
  126. package/dist/{chunk-JLR4IGC2.js → chunk-Z7DDOLAZ.js} +1 -1
  127. package/dist/{chunk-I3RQS5F6.js → chunk-ZFF4UNXN.js} +10 -10
  128. package/dist/{chunk-2GFMA5F6.js → chunk-ZL2RR6WP.js} +1 -1
  129. package/dist/{codex-app-cards-switch-IRV5H4QX.js → codex-app-cards-switch-NWDQLOTK.js} +11 -11
  130. package/dist/{codex-card-routing-GLYARSKQ.js → codex-card-routing-WS72OC7Y.js} +2 -2
  131. package/dist/{codex-desktop-launch-3QB4LAYW.js → codex-desktop-launch-FH6RY46X.js} +1 -1
  132. package/dist/{codex-session-launch-ISLV5J36.js → codex-session-launch-SVUC7T26.js} +20 -20
  133. package/dist/{config-V7FP5LRI.js → config-4JVYJ27G.js} +3 -3
  134. package/dist/{global-upgrade-L3D27Z2N.js → global-upgrade-4FLQ53HE.js} +9 -9
  135. package/dist/{identity-IFUX72BS.js → identity-7H3PMMPS.js} +4 -4
  136. package/dist/{live-capabilities-XWR6FVYJ.js → live-capabilities-YENLFDYL.js} +4 -4
  137. package/dist/{paths-NQ4U74JJ.js → paths-PEFZYPII.js} +3 -3
  138. package/dist/reapply-DLMD74WK.js +70 -0
  139. package/dist/src/index.d.ts +2 -0
  140. package/dist/src/index.js +35 -35
  141. package/dist/{upgrade-lock-PSOXTS7H.js → upgrade-lock-L5QYJEGO.js} +4 -4
  142. package/dist/{usage-RFWDFYXH.js → usage-SKO6G5XJ.js} +3 -3
  143. package/package.json +2 -2
  144. package/dist/chunk-D4RE37P7.js +0 -984
  145. package/dist/reapply-PPKLBFB7.js +0 -69
@@ -0,0 +1,890 @@
1
+ import {
2
+ OPENCODE_APPROVAL_EVENTS_PLACEHOLDER,
3
+ OPENCODE_ASKED_EVENTS,
4
+ OPENCODE_CLI_BRIDGES_QUESTIONS,
5
+ OPENCODE_PLUGIN_MARKER,
6
+ OPENCODE_PLUGIN_PLACEHOLDER,
7
+ OPENCODE_PLUGIN_SIBLING,
8
+ OPENCODE_PLUGIN_SIBLING_CONTENTS
9
+ } from "./chunk-VT4JY2AI.js";
10
+
11
+ // src/opencode-runtime-bundle.ts
12
+ var openCodeRuntimeSource = `import { spawn } from 'child_process';
13
+ import { stat, readdir, readFile } from 'fs/promises';
14
+ import { isAbsolute, resolve, relative, sep, join } from 'path';
15
+ import { homedir } from 'os';
16
+
17
+ // src/opencode-runtime/hook-transport.ts
18
+
19
+ // ../contracts/src/hook-runtime.ts
20
+ var HOOK_BUDGETS = {
21
+ // OpenCode kills nothing and waits forever: \`Permission.ask\` blocks on a
22
+ // \`Deferred\` with no deadline of its own, so this budget is ours to choose
23
+ // rather than one we are told. Chosen to match Claude Code, because what it
24
+ // bounds is how long the person's tool call sits unanswered before Pushary
25
+ // stops asking and hands it back to the terminal.
26
+ opencode: { budgetSeconds: 120, guardSeconds: 10 }};
27
+ var OPENCODE_APPROVAL_EVENT = "permission.ask";
28
+ var KILL_REASON = "Stopped by user, this agent was halted from Pushary";
29
+ var DECISION_STOP_REASON = "Approval was cancelled or became unavailable. Retry only after resolving it.";
30
+
31
+ // src/opencode-protocol.ts
32
+ \`\${JSON.stringify({ type: "module" }, null, 2)}
33
+ \`;
34
+ var OPENCODE_REPLIED_EVENT = "permission.replied";
35
+ var OPENCODE_IDLE_EVENT = "session.idle";
36
+ var OPENCODE_FAILED_EVENT = "session.execution.failed";
37
+ var OPENCODE_SESSION_CREATED_EVENT = "session.created";
38
+ var OPENCODE_V2_FORWARDED_ACTIONS = ["shell", "read", "edit", "webfetch", "websearch", "skill", "subagent"];
39
+ var OPENCODE_STEP_ENDING_REASONS = [KILL_REASON, DECISION_STOP_REASON];
40
+ var OPENCODE_RESUBSCRIBE_DELAYS_MS = [1e3, 2e3, 5e3, 1e4];
41
+ var OPENCODE_SESSION_PARENT_TIMEOUT_MS = 2e3;
42
+ var OPENCODE_SUBAGENT_IDLE_EVENT = "session.subagent.idle";
43
+ var OPENCODE_QUESTION_EVENT = "form.created";
44
+ var OPENCODE_V1_QUESTION_EVENT = "question.asked";
45
+ var OPENCODE_QUESTION_SETTLED_EVENTS = ["form.replied", "form.cancelled", "question.replied", "question.rejected"];
46
+ var OPENCODE_INTERRUPTED_EVENT = "session.execution.interrupted";
47
+ var OPENCODE_GLOBAL_FORM_OWNER = "global";
48
+ var OPENCODE_QUESTION_ANSWERED = "answered";
49
+ var OPENCODE_QUESTION_CANCELLED = "cancelled";
50
+ var OPENCODE_QUESTION_FORM_KIND = "question";
51
+ var OPENCODE_QUESTION_UNDELIVERED_EVENT = "form.reply.failed";
52
+ var OPENCODE_QUESTION_RECHECK_MS = 3 * 6e4;
53
+ var OPENCODE_QUESTION_UNREACHABLE_EVENT = "form.unreachable";
54
+ var OPENCODE_SERVICE_REVERIFY_MS = 2e3;
55
+ var OPENCODE_SETTLED_ELSEWHERE_STATUSES = [404, 409];
56
+ var OPENCODE_SERVICE_REQUEST_TIMEOUT_MS = 5e3;
57
+ var OPENCODE_WITHDRAW_ON_STDIN_END = "withdraw_on_stdin_end";
58
+ var OPENCODE_CAPABILITIES_EVENT = "pushary.capabilities";
59
+ var OPENCODE_CAPABILITY_PROBE_TIMEOUT_MS = 5e3;
60
+ var OPENCODE_QUESTION_WITHDRAW_TIMEOUT_MS = 5e3;
61
+ var OPENCODE_QUESTION_ASK_IN_FLIGHT_GRACE_MS = 6e4;
62
+ var OPENCODE_HOOK_EXIT_MARGIN_MS = 2e3;
63
+ var OPENCODE_QUESTION_WITHDRAW_GRACE_MS = OPENCODE_QUESTION_ASK_IN_FLIGHT_GRACE_MS + OPENCODE_QUESTION_WITHDRAW_TIMEOUT_MS + 2 * OPENCODE_HOOK_EXIT_MARGIN_MS;
64
+ var OPENCODE_APPROVAL_TIMEOUT_MS = (HOOK_BUDGETS.opencode.budgetSeconds - HOOK_BUDGETS.opencode.guardSeconds) * 1e3;
65
+ var OPENCODE_STATE_DIRECTORY = "opencode";
66
+ var OPENCODE_SERVICE_REGISTRATION = /^service.*\\.json$/;
67
+ var OPENCODE_SERVICE_USER = "opencode";
68
+ var OPENCODE_SERVICE_PROTOCOL = "http:";
69
+ var OPENCODE_SERVICE_INFO_PATH = "/api/info";
70
+ var OPENCODE_LOOPBACK_HOSTS = ["127.0.0.1", "[::1]"];
71
+ var OPENCODE_LOOPBACK_ALIASES = {
72
+ "0.0.0.0": "127.0.0.1",
73
+ localhost: "127.0.0.1",
74
+ "[::]": "[::1]"
75
+ };
76
+
77
+ // src/opencode-runtime/constants.ts
78
+ var NOT_FOUND = 404;
79
+ var FORM_PENDING = "pending";
80
+ var REPLY_DELIVERED = "delivered";
81
+ var REPLY_SETTLED_ELSEWHERE = "settled_elsewhere";
82
+ var REPLY_REFUSED = "refused";
83
+ var TELEMETRY_TIMEOUT_MS = 1e4;
84
+ var MAX_REPLY_BYTES = 65536;
85
+ var HOME_PREFIX = "~";
86
+ var PARENT_SEGMENT = "..";
87
+ var MAX_TRACKED = 256;
88
+
89
+ // ../contracts/src/json-record.ts
90
+ var isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
91
+
92
+ // src/opencode-runtime/values.ts
93
+ var text = (value) => typeof value === "string" && value.length > 0 ? value : void 0;
94
+ var record = (value) => isRecord(value) ? value : {};
95
+ var remember = (map, key, value) => {
96
+ if (!key || value === void 0) return;
97
+ const oldest = map.keys().next().value;
98
+ if (map.size >= MAX_TRACKED && oldest !== void 0) map.delete(oldest);
99
+ map.set(key, value);
100
+ };
101
+
102
+ // src/opencode-runtime/hook-transport.ts
103
+ var createHookTransport = (command) => {
104
+ const withdrawsOverStdin = (payload) => payload[OPENCODE_WITHDRAW_ON_STDIN_END] === true;
105
+ const replyIn = (out) => {
106
+ const body = out.trim();
107
+ if (!body) return null;
108
+ try {
109
+ return JSON.parse(body);
110
+ } catch {
111
+ return null;
112
+ }
113
+ };
114
+ const exitedCleanly = (code, signal) => code === 0 && !signal;
115
+ const runHook = (payload, timeoutMs, onSpawn) => new Promise((resolve3) => {
116
+ let settled = false;
117
+ let timer;
118
+ const finish = (reply, cleanExit) => {
119
+ if (settled) return;
120
+ settled = true;
121
+ clearTimeout(timer);
122
+ resolve3({ reply, cleanExit });
123
+ };
124
+ let child;
125
+ try {
126
+ child = spawn(command[0], command.slice(1), { stdio: ["pipe", "pipe", "ignore"] });
127
+ } catch {
128
+ return finish(null, false);
129
+ }
130
+ if (onSpawn) onSpawn(child);
131
+ if (timeoutMs !== void 0) {
132
+ timer = setTimeout(() => {
133
+ try {
134
+ child.kill("SIGKILL");
135
+ } catch {
136
+ }
137
+ finish(null, false);
138
+ }, timeoutMs);
139
+ if (typeof timer.unref === "function") timer.unref();
140
+ }
141
+ let out = "";
142
+ child.stdout.on("data", (chunk) => {
143
+ if (out.length < MAX_REPLY_BYTES) out += chunk;
144
+ });
145
+ child.on("error", () => finish(null, false));
146
+ child.on("close", (code, signal) => finish(replyIn(out), exitedCleanly(code, signal)));
147
+ try {
148
+ child.stdin.on("error", () => {
149
+ });
150
+ if (withdrawsOverStdin(payload)) child.stdin.write(JSON.stringify(payload) + "\\n");
151
+ else child.stdin.end(JSON.stringify(payload));
152
+ } catch {
153
+ finish(null, false);
154
+ }
155
+ });
156
+ const run = (payload, timeoutMs, onSpawn) => runHook(payload, timeoutMs, onSpawn).then((ended) => ended.reply);
157
+ const commandSignature = async () => {
158
+ const parts = [];
159
+ for (const entry of command.filter((part) => isAbsolute(part))) {
160
+ try {
161
+ const info = await stat(entry);
162
+ parts.push([entry, info.ino, info.size, info.mtimeMs, info.ctimeMs].join(":"));
163
+ } catch {
164
+ }
165
+ }
166
+ return parts.join("|");
167
+ };
168
+ const reportsHeldStdin = (reply) => {
169
+ const capabilities = record(reply).capabilities;
170
+ return Array.isArray(capabilities) && capabilities.includes(OPENCODE_WITHDRAW_ON_STDIN_END);
171
+ };
172
+ const probeCapabilities = async () => {
173
+ const ended = await runHook({ hook_event_name: OPENCODE_CAPABILITIES_EVENT }, OPENCODE_CAPABILITY_PROBE_TIMEOUT_MS);
174
+ return { holds: ended.cleanExit && reportsHeldStdin(ended.reply), conclusive: ended.cleanExit };
175
+ };
176
+ let knownCapabilities;
177
+ let capabilityCheck;
178
+ const checkHookCapabilities = async () => {
179
+ const signature = await commandSignature();
180
+ if (signature && knownCapabilities && knownCapabilities.signature === signature) return knownCapabilities.holds;
181
+ const answer = await probeCapabilities();
182
+ knownCapabilities = signature && answer.conclusive ? { signature, holds: answer.holds } : void 0;
183
+ return answer.holds;
184
+ };
185
+ const hookWithdrawsOverStdin = () => {
186
+ if (!capabilityCheck)
187
+ capabilityCheck = checkHookCapabilities().finally(() => {
188
+ capabilityCheck = void 0;
189
+ });
190
+ return capabilityCheck;
191
+ };
192
+ const tell = (payload) => {
193
+ void run(payload, TELEMETRY_TIMEOUT_MS);
194
+ };
195
+ return { run, tell, hookWithdrawsOverStdin };
196
+ };
197
+ var inputFrom = (permission, metadata, resources) => {
198
+ const meta = record(metadata);
199
+ if (resources?.length === 1) {
200
+ if (permission === "read") return { file_path: resources[0] };
201
+ if (permission === "websearch") return { query: resources[0] };
202
+ if (permission === "skill") return { skill: resources[0] };
203
+ if (permission === "subagent") {
204
+ const description = text(meta.description);
205
+ return { subagent_type: resources[0], ...description ? { description } : {} };
206
+ }
207
+ }
208
+ const command = text(meta.command);
209
+ if (command) return { command };
210
+ const preview = record(Array.isArray(meta.files) && meta.files.length === 1 ? meta.files[0] : void 0);
211
+ const filepath = text(preview.file) ?? text(meta.filepath) ?? text(meta.file_path) ?? text(meta.path);
212
+ const diff = text(preview.patch) ?? text(meta.diff);
213
+ if (filepath) return { file_path: filepath, ...diff ? { diff } : {} };
214
+ const url = text(meta.url);
215
+ if (url) return { url };
216
+ return { permission, ...resources?.length ? { resources } : {} };
217
+ };
218
+ var askedFrom = (properties) => {
219
+ const value = record(properties);
220
+ const id = text(value.id);
221
+ const sessionID = text(value.sessionID);
222
+ if (!id || !sessionID) return null;
223
+ return {
224
+ id,
225
+ sessionID,
226
+ permission: text(value.action) ?? text(value.permission) ?? text(value.type) ?? "unknown",
227
+ metadata: record(value.metadata),
228
+ resources: Array.isArray(value.resources) ? value.resources.filter((item) => typeof item === "string") : void 0,
229
+ title: text(value.title) ?? text(value.message),
230
+ callID: text(record(value.source).id) ?? text(value.callID) ?? text(record(value.tool).callID)
231
+ };
232
+ };
233
+ var repliedID = (properties) => {
234
+ const value = record(properties);
235
+ return text(value.requestID) ?? text(value.permissionID) ?? text(value.id);
236
+ };
237
+ var feedbackFor = (reason) => {
238
+ const message = text(reason);
239
+ return message && !OPENCODE_STEP_ENDING_REASONS.some((reason2) => reason2 === message) ? message : void 0;
240
+ };
241
+ var failureReasonOf = (error) => typeof error === "string" ? text(error) : text(record(error).message) ?? text(record(error).type);
242
+ var isWorkdirInside = (root, workdir) => {
243
+ if (workdir.startsWith(HOME_PREFIX)) return false;
244
+ const path = relative(root, resolve(root, workdir));
245
+ return path === "" || path !== PARENT_SEGMENT && !path.startsWith(PARENT_SEGMENT + sep) && !isAbsolute(path);
246
+ };
247
+
248
+ // src/opencode-runtime/question-lifecycle.ts
249
+ var waitFor = (ms) => new Promise((resume) => {
250
+ setTimeout(resume, ms).unref();
251
+ });
252
+ var createQuestionLifecycle = (transport2, base, openQuestionChannel) => {
253
+ const { run, tell, hookWithdrawsOverStdin } = transport2;
254
+ const questions = /* @__PURE__ */ new Map();
255
+ const questionChannelFor = async () => {
256
+ if (!openQuestionChannel) return null;
257
+ try {
258
+ return await openQuestionChannel();
259
+ } catch {
260
+ return null;
261
+ }
262
+ };
263
+ const formPayload = (form) => ({
264
+ ...base(form.sessionID),
265
+ form_id: form.id,
266
+ form: { title: form.title, fields: form.fields }
267
+ });
268
+ const settleQuestion = async (channel, form, reply) => {
269
+ const outcome = record(reply);
270
+ if (outcome.status === OPENCODE_QUESTION_ANSWERED && isRecord(outcome.answer)) {
271
+ if (await channel.reply(form, outcome.answer) === REPLY_REFUSED)
272
+ tell({ ...formPayload(form), hook_event_name: OPENCODE_QUESTION_UNDELIVERED_EVENT });
273
+ return;
274
+ }
275
+ if (outcome.status === OPENCODE_QUESTION_CANCELLED) await channel.cancel(form, feedbackFor(outcome.reason));
276
+ };
277
+ let recheck;
278
+ const stopRechecking = () => {
279
+ clearInterval(recheck);
280
+ recheck = void 0;
281
+ };
282
+ const recheckQuestions = async () => {
283
+ for (const [id, pending] of Array.from(questions)) {
284
+ if (!pending.channel || !pending.child) continue;
285
+ let open = true;
286
+ try {
287
+ open = await pending.channel.isPending(pending.form);
288
+ } catch {
289
+ }
290
+ if (!open && questions.get(id) === pending) withdrawQuestion(id);
291
+ }
292
+ if (questions.size === 0) stopRechecking();
293
+ };
294
+ const keepRechecking = () => {
295
+ if (recheck) return;
296
+ recheck = setInterval(() => void recheckQuestions(), OPENCODE_QUESTION_RECHECK_MS);
297
+ if (typeof recheck.unref === "function") recheck.unref();
298
+ };
299
+ const trackQuestion = (id, pending) => {
300
+ if (questions.size >= MAX_TRACKED) withdrawQuestion(questions.keys().next().value);
301
+ questions.set(id, pending);
302
+ };
303
+ const forgetQuestion = (id, pending) => {
304
+ if (questions.get(id) === pending) questions.delete(id);
305
+ if (questions.size === 0) stopRechecking();
306
+ };
307
+ const unreachableTold = /* @__PURE__ */ new Map();
308
+ const tellUnreachable = (form) => {
309
+ if (unreachableTold.has(form.id)) return;
310
+ remember(unreachableTold, form.id, true);
311
+ tell({ ...formPayload(form), hook_event_name: OPENCODE_QUESTION_UNREACHABLE_EVENT });
312
+ };
313
+ const verifiedChannel = async (form, pending) => {
314
+ const channel = await questionChannelFor();
315
+ if (channel || !openQuestionChannel || questions.get(form.id) !== pending) return channel;
316
+ await waitFor(OPENCODE_SERVICE_REVERIFY_MS);
317
+ return questions.get(form.id) === pending ? questionChannelFor() : null;
318
+ };
319
+ const bridgeQuestion = async (form) => {
320
+ const pending = { child: void 0, channel: void 0, form, sessionID: form.sessionID, holdsStdin: false };
321
+ trackQuestion(form.id, pending);
322
+ const channel = await verifiedChannel(form, pending);
323
+ if (!channel || questions.get(form.id) !== pending) {
324
+ if (!channel && openQuestionChannel && questions.get(form.id) === pending) tellUnreachable(form);
325
+ forgetQuestion(form.id, pending);
326
+ return;
327
+ }
328
+ pending.channel = channel;
329
+ keepRechecking();
330
+ pending.holdsStdin = await hookWithdrawsOverStdin();
331
+ if (questions.get(form.id) !== pending) return;
332
+ const asked = {
333
+ ...formPayload(form),
334
+ hook_event_name: OPENCODE_QUESTION_EVENT,
335
+ ...pending.holdsStdin ? { [OPENCODE_WITHDRAW_ON_STDIN_END]: true } : {}
336
+ };
337
+ const reply = await run(asked, void 0, (child) => {
338
+ pending.child = child;
339
+ });
340
+ if (questions.get(form.id) !== pending) return;
341
+ try {
342
+ await settleQuestion(channel, form, reply);
343
+ } catch {
344
+ }
345
+ forgetQuestion(form.id, pending);
346
+ };
347
+ const signalQuestionHook = (child) => {
348
+ try {
349
+ child.kill("SIGTERM");
350
+ } catch {
351
+ }
352
+ };
353
+ const endQuestionHook = (child) => {
354
+ try {
355
+ child.stdin.end();
356
+ } catch {
357
+ }
358
+ const fallback = setTimeout(() => signalQuestionHook(child), OPENCODE_QUESTION_WITHDRAW_GRACE_MS);
359
+ if (typeof fallback.unref === "function") fallback.unref();
360
+ child.once("close", () => clearTimeout(fallback));
361
+ };
362
+ const withdrawQuestion = (id) => {
363
+ const pending = id ? questions.get(id) : void 0;
364
+ if (!pending || !id) return;
365
+ forgetQuestion(id, pending);
366
+ if (!pending.child) return;
367
+ if (pending.holdsStdin) endQuestionHook(pending.child);
368
+ else signalQuestionHook(pending.child);
369
+ };
370
+ const withdrawSessionQuestions = (sessionID) => {
371
+ for (const [id, pending] of Array.from(questions)) {
372
+ if (pending.sessionID === sessionID) withdrawQuestion(id);
373
+ }
374
+ };
375
+ return {
376
+ has: (id) => questions.has(id),
377
+ bridgeQuestion,
378
+ withdrawQuestion,
379
+ withdrawSessionQuestions,
380
+ reconcile: recheckQuestions,
381
+ dispose: () => {
382
+ for (const id of Array.from(questions.keys())) withdrawQuestion(id);
383
+ stopRechecking();
384
+ }
385
+ };
386
+ };
387
+
388
+ // src/opencode-runtime/sessions.ts
389
+ var createSessions = (tell, base, sessionParent) => {
390
+ const subagentSessions = /* @__PURE__ */ new Map();
391
+ const noteSession = (sessionID, parentID) => remember(subagentSessions, text(sessionID), Promise.resolve(text(parentID)));
392
+ const parentWithin = (sessionID) => new Promise((resolve3, reject) => {
393
+ const timer = setTimeout(() => reject(new Error("session lookup timed out")), OPENCODE_SESSION_PARENT_TIMEOUT_MS);
394
+ if (typeof timer.unref === "function") timer.unref();
395
+ Promise.resolve().then(() => sessionParent?.(sessionID)).then(resolve3, reject).finally(() => clearTimeout(timer));
396
+ });
397
+ const parentOf = (sessionID) => {
398
+ if (!sessionID) return Promise.resolve(void 0);
399
+ const known = subagentSessions.get(sessionID);
400
+ if (known) return known;
401
+ if (!sessionParent) return Promise.resolve(void 0);
402
+ const lookup = parentWithin(sessionID).then((parentID) => text(parentID)).catch(() => {
403
+ if (subagentSessions.get(sessionID) === lookup) subagentSessions.delete(sessionID);
404
+ return void 0;
405
+ });
406
+ remember(subagentSessions, sessionID, lookup);
407
+ return lookup;
408
+ };
409
+ const tellUnlessSubagent = (sessionID, payload) => {
410
+ void parentOf(sessionID).then((parentID) => {
411
+ if (!parentID) tell(payload);
412
+ });
413
+ };
414
+ const tellTurnEnded = (sessionID, payload) => {
415
+ void parentOf(sessionID).then((parentID) => {
416
+ tell(parentID ? { ...base(parentID), hook_event_name: OPENCODE_SUBAGENT_IDLE_EVENT, agent_id: sessionID } : payload);
417
+ });
418
+ };
419
+ return { noteSession, tellUnlessSubagent, tellTurnEnded };
420
+ };
421
+
422
+ // src/opencode-runtime/permission-replies.ts
423
+ var respondToPermission = async ({ client, replyPermission }, asked, allow, message) => {
424
+ const sdk = record(client);
425
+ const permission = record(sdk.permission);
426
+ const legacyMethod = sdk.postSessionIdPermissionsPermissionId;
427
+ const currentMethod = permission.reply;
428
+ const decision = allow ? "once" : "reject";
429
+ const feedback = !allow && message ? { message } : {};
430
+ if (replyPermission) {
431
+ try {
432
+ await replyPermission({ sessionID: asked.sessionID, requestID: asked.id, decision, ...feedback });
433
+ } catch {
434
+ }
435
+ return;
436
+ }
437
+ const legacy = typeof legacyMethod === "function" ? () => Reflect.apply(legacyMethod, client, [
438
+ {
439
+ path: { id: asked.sessionID, permissionID: asked.id },
440
+ body: { response: decision }
441
+ }
442
+ ]) : null;
443
+ const current = typeof currentMethod === "function" ? () => Reflect.apply(currentMethod, sdk.permission, [
444
+ {
445
+ requestID: asked.id,
446
+ reply: decision,
447
+ ...feedback,
448
+ path: { requestID: asked.id },
449
+ body: { reply: decision, ...feedback }
450
+ },
451
+ { throwOnError: true }
452
+ ]) : null;
453
+ for (const attempt of [legacy, current]) {
454
+ if (!attempt) continue;
455
+ try {
456
+ const result = await attempt();
457
+ if (record(result).error) continue;
458
+ return;
459
+ } catch {
460
+ }
461
+ }
462
+ };
463
+ var loopbackServiceUrl = (raw) => {
464
+ let url;
465
+ try {
466
+ if (!raw) return void 0;
467
+ url = new URL(raw);
468
+ } catch {
469
+ return void 0;
470
+ }
471
+ if (url.protocol !== OPENCODE_SERVICE_PROTOCOL) return void 0;
472
+ const literal = OPENCODE_LOOPBACK_ALIASES[url.hostname];
473
+ if (literal) url.hostname = literal;
474
+ return OPENCODE_LOOPBACK_HOSTS.some((host) => host === url.hostname) ? url.origin : void 0;
475
+ };
476
+ var serviceRequest = async (service, method, path, body) => {
477
+ const headers = {};
478
+ if (body) headers["content-type"] = "application/json";
479
+ if (service.password) headers.authorization = "Basic " + Buffer.from(OPENCODE_SERVICE_USER + ":" + service.password).toString("base64");
480
+ try {
481
+ return await fetch(new URL(path, service.url), {
482
+ method,
483
+ headers,
484
+ body: body ? JSON.stringify(body) : void 0,
485
+ redirect: "error",
486
+ signal: AbortSignal.timeout(OPENCODE_SERVICE_REQUEST_TIMEOUT_MS)
487
+ });
488
+ } catch {
489
+ return null;
490
+ }
491
+ };
492
+ var serviceAnswersAsThisProcess = async (service) => {
493
+ const response = await serviceRequest(service, "GET", OPENCODE_SERVICE_INFO_PATH);
494
+ if (!response || !response.ok) return false;
495
+ try {
496
+ const info = await response.json();
497
+ return record(info).pid === process.pid;
498
+ } catch {
499
+ return false;
500
+ }
501
+ };
502
+ var registeredServiceOf = async (directory, name) => {
503
+ try {
504
+ const info = record(JSON.parse(await readFile(join(directory, name), "utf8")));
505
+ const url = info && info.pid === process.pid ? loopbackServiceUrl(text(info.url)) : void 0;
506
+ return url ? { url, password: text(info.password) } : null;
507
+ } catch {
508
+ return null;
509
+ }
510
+ };
511
+ var ownService = async () => {
512
+ const directory = join(process.env.XDG_STATE_HOME || join(homedir(), ".local", "state"), OPENCODE_STATE_DIRECTORY);
513
+ let names;
514
+ try {
515
+ names = await readdir(directory);
516
+ } catch {
517
+ return null;
518
+ }
519
+ for (const name of names.filter((entry) => OPENCODE_SERVICE_REGISTRATION.test(entry))) {
520
+ const service = await registeredServiceOf(directory, name);
521
+ if (service && await serviceAnswersAsThisProcess(service)) return service;
522
+ }
523
+ return null;
524
+ };
525
+
526
+ // src/opencode-runtime/question-forms.ts
527
+ var isFormField = (value) => isRecord(value) && typeof value.key === "string";
528
+ var formOf = (properties) => {
529
+ const form = record(record(properties).form);
530
+ const id = text(form.id);
531
+ const sessionID = text(form.sessionID);
532
+ if (!id || !sessionID || sessionID === OPENCODE_GLOBAL_FORM_OWNER || record(form.metadata).kind !== OPENCODE_QUESTION_FORM_KIND || !Array.isArray(form.fields) || !form.fields.every(isFormField))
533
+ return null;
534
+ return { id, sessionID, title: text(form.title), fields: form.fields };
535
+ };
536
+ var v1OptionOf = (option) => {
537
+ const value = record(option);
538
+ const label = text(value.label);
539
+ return { value: label, label, description: text(value.description) };
540
+ };
541
+ var v1FieldOf = (question, index) => {
542
+ const asked = record(question);
543
+ return {
544
+ key: "q" + index,
545
+ title: text(asked.header),
546
+ description: text(asked.question),
547
+ type: asked.multiple === true ? "multiselect" : "string",
548
+ options: Array.isArray(asked.options) ? asked.options.map(v1OptionOf) : [],
549
+ custom: asked.custom !== false
550
+ };
551
+ };
552
+ var v1FormOf = (properties) => {
553
+ const value = record(properties);
554
+ const id = text(value.id);
555
+ const sessionID = text(value.sessionID);
556
+ return id && sessionID && Array.isArray(value.questions) ? { id, sessionID, fields: value.questions.map(v1FieldOf) } : null;
557
+ };
558
+ var settledQuestionID = (properties) => text(record(properties).requestID) ?? text(record(properties).id);
559
+ var v1AnswerOf = (value) => value === void 0 ? [] : Array.isArray(value) ? value : [String(value)];
560
+ var v1Answers = (form, answer) => form.fields.map((field) => v1AnswerOf(answer[field.key]));
561
+
562
+ // src/opencode-runtime/question-channels.ts
563
+ var succeeded = (response) => response?.ok === true;
564
+ var replyOutcomeOf = (status) => {
565
+ if (status !== void 0 && status >= 200 && status < 300) return REPLY_DELIVERED;
566
+ return OPENCODE_SETTLED_ELSEWHERE_STATUSES.some((settled) => settled === status) ? REPLY_SETTLED_ELSEWHERE : REPLY_REFUSED;
567
+ };
568
+ var formPath = (form) => "/api/session/" + encodeURIComponent(form.sessionID) + "/form/" + encodeURIComponent(form.id);
569
+ var formIsPending = async (service, form) => {
570
+ const response = await serviceRequest(service, "GET", formPath(form));
571
+ if (!response) return true;
572
+ if (response.status === NOT_FOUND) return false;
573
+ if (!response.ok) return true;
574
+ try {
575
+ const body = await response.json();
576
+ const state = record(record(body).data).state;
577
+ return !state || record(state).status === FORM_PENDING;
578
+ } catch {
579
+ return true;
580
+ }
581
+ };
582
+ var formChannel = (service) => ({
583
+ reply: async (form, answer) => {
584
+ const response = await serviceRequest(service, "POST", formPath(form) + "/reply", { answer });
585
+ return replyOutcomeOf(response ? response.status : void 0);
586
+ },
587
+ cancel: async (form, message) => {
588
+ const query = message ? "?message=" + encodeURIComponent(message) : "";
589
+ if (succeeded(await serviceRequest(service, "DELETE", formPath(form) + query))) return true;
590
+ return succeeded(await serviceRequest(service, "POST", formPath(form) + "/cancel"));
591
+ },
592
+ isPending: (form) => formIsPending(service, form)
593
+ });
594
+ var v2QuestionChannel = async () => {
595
+ const service = await ownService();
596
+ return service ? formChannel(service) : null;
597
+ };
598
+ var v1QuestionChannel = (client) => {
599
+ const raw = record(record(client)._client);
600
+ const rawPost = raw.post;
601
+ const rawGet = raw.get;
602
+ if (typeof rawPost !== "function" || typeof rawGet !== "function") return void 0;
603
+ const post = async (url, requestID, body) => {
604
+ try {
605
+ const result = await rawPost.call(raw, {
606
+ url,
607
+ path: { requestID },
608
+ ...body ? { body, headers: { "Content-Type": "application/json" } } : {}
609
+ });
610
+ if (!record(result).error) return REPLY_DELIVERED;
611
+ const status = record(record(result).response).status;
612
+ return replyOutcomeOf(typeof status === "number" ? status : void 0);
613
+ } catch {
614
+ return REPLY_REFUSED;
615
+ }
616
+ };
617
+ const channel = {
618
+ reply: (form, answer) => post("/question/{requestID}/reply", form.id, { answers: v1Answers(form, answer) }),
619
+ cancel: (form) => post("/question/{requestID}/reject", form.id),
620
+ isPending: async (form) => {
621
+ const result = await rawGet.call(raw, { url: "/question" });
622
+ const data = record(result).data;
623
+ return record(result).error || !Array.isArray(data) ? true : data.some((request) => record(request).id === form.id);
624
+ }
625
+ };
626
+ return async () => channel;
627
+ };
628
+
629
+ // src/opencode-runtime/plugin.ts
630
+ var createPluginHooks = async (options, config, transport2) => {
631
+ const { client, directory, worktree, replyPermission, sessionParent, questionChannel } = options;
632
+ const { run, tell } = transport2;
633
+ const { approvalEvents: APPROVAL_EVENTS2, bridgesQuestions: BRIDGES_QUESTIONS2 } = config;
634
+ const cwd = directory || worktree || process.cwd();
635
+ const tools = /* @__PURE__ */ new Map();
636
+ const waiting = /* @__PURE__ */ new Map();
637
+ const openQuestionChannel = questionChannel ?? v1QuestionChannel(client);
638
+ const base = (sessionID) => ({ session_id: sessionID ?? "", cwd });
639
+ const { noteSession, tellUnlessSubagent, tellTurnEnded } = createSessions(tell, base, sessionParent);
640
+ const questions = createQuestionLifecycle(transport2, base, openQuestionChannel);
641
+ const { withdrawQuestion, withdrawSessionQuestions, bridgeQuestion } = questions;
642
+ const gate = async (asked) => {
643
+ if (asked.permission === "external_directory") return;
644
+ if (replyPermission && !OPENCODE_V2_FORWARDED_ACTIONS.some((action) => action === asked.permission)) return;
645
+ if (Array.isArray(asked.metadata?.files) && asked.metadata.files.length > 1) return;
646
+ if (["read", "edit"].includes(asked.permission) && (asked.resources?.length ?? 0) > 1) return;
647
+ const tool = asked.callID ? tools.get(asked.callID) : void 0;
648
+ const workdir = ["shell", "bash"].includes(tool?.name ?? "") ? text(tool?.input.workdir) : void 0;
649
+ if (workdir && !isWorkdirInside(cwd, workdir)) return;
650
+ const reply = await run(
651
+ {
652
+ ...base(asked.sessionID),
653
+ cwd: workdir ? resolve(cwd, workdir) : cwd,
654
+ project_dir: cwd,
655
+ hook_event_name: OPENCODE_APPROVAL_EVENT,
656
+ tool_name: tool?.name ?? asked.permission,
657
+ tool_input: inputFrom(asked.permission, { ...tool?.input, ...asked.metadata }, asked.resources),
658
+ permission_id: asked.id,
659
+ permission_type: asked.permission,
660
+ title: asked.title
661
+ },
662
+ OPENCODE_APPROVAL_TIMEOUT_MS,
663
+ (child) => remember(waiting, asked.id, child)
664
+ );
665
+ if (!waiting.has(asked.id)) return;
666
+ const status = record(reply).status;
667
+ if (status === "allow" || status === "deny")
668
+ await respondToPermission(options, asked, status === "allow", feedbackFor(record(reply).reason));
669
+ waiting.delete(asked.id);
670
+ };
671
+ const withdraw = (id) => {
672
+ const child = id && waiting.get(id);
673
+ if (!child || !id) return;
674
+ waiting.delete(id);
675
+ try {
676
+ child.kill("SIGTERM");
677
+ } catch {
678
+ }
679
+ };
680
+ return {
681
+ "tool.execute.before": async (raw) => {
682
+ const input = record(raw);
683
+ remember(tools, text(input.callID), { name: text(input.tool), input: record(input.args) });
684
+ },
685
+ "tool.execute.after": async (rawInput, rawOutput) => {
686
+ const input = record(rawInput);
687
+ const output = record(rawOutput);
688
+ const call = text(input.callID);
689
+ if (call) tools.delete(call);
690
+ tell({
691
+ ...base(text(input.sessionID)),
692
+ hook_event_name: "tool.execute.after",
693
+ tool_name: text(input.tool) ?? "unknown",
694
+ tool_input: record(input.args),
695
+ tool_result: output?.error ? { error: output.error } : { title: text(output.title), output: text(output.output) }
696
+ });
697
+ },
698
+ "chat.message": async (rawInput, rawOutput) => {
699
+ const input = record(rawInput);
700
+ const output = record(rawOutput);
701
+ const parts = Array.isArray(output.parts) ? output.parts : [];
702
+ const prompt = parts.flatMap((part) => record(part).type === "text" && typeof record(part).text === "string" ? [record(part).text] : []).join("\\n").trim();
703
+ if (!prompt) return;
704
+ tellUnlessSubagent(text(input.sessionID), { ...base(text(input.sessionID)), hook_event_name: "chat.message", prompt });
705
+ },
706
+ event: async (input) => {
707
+ const event = record(record(input).event);
708
+ try {
709
+ const type = text(event.type) ?? "";
710
+ const properties = record(event.properties);
711
+ if (APPROVAL_EVENTS2.includes(type)) {
712
+ const asked = askedFrom(properties);
713
+ if (asked && !waiting.has(asked.id)) void gate(asked);
714
+ return;
715
+ }
716
+ if (type === OPENCODE_REPLIED_EVENT) {
717
+ withdraw(repliedID(properties));
718
+ return;
719
+ }
720
+ if (type === OPENCODE_QUESTION_EVENT || type === OPENCODE_V1_QUESTION_EVENT) {
721
+ const form = type === OPENCODE_QUESTION_EVENT ? formOf(properties) : v1FormOf(properties);
722
+ if (form && BRIDGES_QUESTIONS2 && APPROVAL_EVENTS2.length > 0 && !questions.has(form.id)) void bridgeQuestion(form);
723
+ return;
724
+ }
725
+ if (OPENCODE_QUESTION_SETTLED_EVENTS.some((settled) => settled === type)) {
726
+ withdrawQuestion(settledQuestionID(properties));
727
+ return;
728
+ }
729
+ if (type === OPENCODE_INTERRUPTED_EVENT) {
730
+ withdrawSessionQuestions(text(properties.sessionID));
731
+ return;
732
+ }
733
+ if (type === OPENCODE_SESSION_CREATED_EVENT) {
734
+ noteSession(properties.sessionID, properties.parentID);
735
+ return;
736
+ }
737
+ if (type === OPENCODE_IDLE_EVENT) {
738
+ withdrawSessionQuestions(text(properties.sessionID));
739
+ tellTurnEnded(text(properties.sessionID), {
740
+ ...base(text(properties.sessionID)),
741
+ hook_event_name: OPENCODE_IDLE_EVENT
742
+ });
743
+ return;
744
+ }
745
+ if (type === OPENCODE_FAILED_EVENT) {
746
+ withdrawSessionQuestions(text(properties.sessionID));
747
+ tellTurnEnded(text(properties.sessionID), {
748
+ ...base(text(properties.sessionID)),
749
+ hook_event_name: OPENCODE_FAILED_EVENT,
750
+ error: failureReasonOf(properties.error)
751
+ });
752
+ }
753
+ } catch {
754
+ }
755
+ },
756
+ reconcile: questions.reconcile,
757
+ dispose: async () => {
758
+ for (const id of Array.from(waiting.keys())) withdraw(id);
759
+ questions.dispose();
760
+ tools.clear();
761
+ }
762
+ };
763
+ };
764
+
765
+ // src/opencode-runtime/entry.ts
766
+ var transport = createHookTransport(COMMAND);
767
+ var server = (options) => createPluginHooks(options, { approvalEvents: APPROVAL_EVENTS, bridgesQuestions: BRIDGES_QUESTIONS }, transport);
768
+ var toolInput = (raw) => {
769
+ const event = record(raw);
770
+ return { callID: event.id, tool: event.tool, sessionID: event.sessionID, args: event.input };
771
+ };
772
+ var toolOutput = (raw) => {
773
+ const event = record(raw);
774
+ const content = record(event.result).content;
775
+ return {
776
+ output: event.status === "completed" && Array.isArray(content) ? content.flatMap((part) => record(part).type === "text" ? [text(record(part).text) ?? ""] : []).join("\\n") : void 0,
777
+ error: event.status === "error" ? record(event.error).message : void 0
778
+ };
779
+ };
780
+ var subscribe = (ctx, hooks) => {
781
+ const controller = new AbortController();
782
+ const pause = (ms) => new Promise((resume) => {
783
+ if (controller.signal.aborted) return resume();
784
+ const done = () => {
785
+ clearTimeout(timer);
786
+ controller.signal.removeEventListener("abort", done);
787
+ resume();
788
+ };
789
+ const timer = setTimeout(done, ms);
790
+ timer.unref();
791
+ controller.signal.addEventListener("abort", done, { once: true });
792
+ });
793
+ const consume = async () => {
794
+ let failures = 0;
795
+ let warned = false;
796
+ while (!controller.signal.aborted) {
797
+ try {
798
+ for await (const raw of ctx.event.subscribe({ signal: controller.signal })) {
799
+ if (controller.signal.aborted) break;
800
+ failures = 0;
801
+ const event = record(raw);
802
+ const location = record(event.location);
803
+ if (event.location && (location.directory !== ctx.location.directory || location.workspaceID !== ctx.location.workspaceID))
804
+ continue;
805
+ const type = event.type === "session.execution.succeeded" ? OPENCODE_IDLE_EVENT : event.type;
806
+ await hooks.event({ event: { type, properties: event.data } });
807
+ }
808
+ break;
809
+ } catch {
810
+ if (controller.signal.aborted) break;
811
+ if (!warned) console.error("Pushary event subscription stopped; native prompts remain available.");
812
+ warned = true;
813
+ }
814
+ await pause(OPENCODE_RESUBSCRIBE_DELAYS_MS[Math.min(failures, OPENCODE_RESUBSCRIBE_DELAYS_MS.length - 1)]);
815
+ failures += 1;
816
+ await hooks.reconcile();
817
+ }
818
+ await hooks.dispose();
819
+ };
820
+ const events = consume();
821
+ return async () => {
822
+ controller.abort();
823
+ await hooks.dispose();
824
+ await events;
825
+ };
826
+ };
827
+ var entry_default = {
828
+ id: "pushary",
829
+ server,
830
+ async setup(ctx) {
831
+ const hooks = await server({
832
+ directory: ctx.location.directory,
833
+ replyPermission: (request) => ctx.permission.reply(request),
834
+ sessionParent: typeof ctx.session.get === "function" ? async (sessionID) => record(await ctx.session.get?.({ sessionID })).parentID : void 0,
835
+ questionChannel: v2QuestionChannel
836
+ });
837
+ await ctx.tool.hook("execute.before", (event) => hooks["tool.execute.before"](toolInput(event)));
838
+ await ctx.tool.hook("execute.after", (event) => hooks["tool.execute.after"](toolInput(event), toolOutput(event)));
839
+ await ctx.session.hook("prompt", async (raw) => {
840
+ const event = record(raw);
841
+ await hooks["chat.message"]({ sessionID: event.sessionID }, { parts: [{ type: "text", text: record(event.prompt).text }] });
842
+ });
843
+ return subscribe(ctx, hooks);
844
+ }
845
+ };
846
+
847
+ export { entry_default as default };
848
+ `;
849
+
850
+ // src/opencode-plugin.ts
851
+ var openCodePluginSource = (bridgesQuestions) => `// ${OPENCODE_PLUGIN_MARKER}
852
+ const COMMAND = ${OPENCODE_PLUGIN_PLACEHOLDER}
853
+ const APPROVAL_EVENTS = ${OPENCODE_APPROVAL_EVENTS_PLACEHOLDER}
854
+ const BRIDGES_QUESTIONS = ${JSON.stringify(bridgesQuestions)}
855
+ ${openCodeRuntimeSource}`;
856
+ var openCodePluginModule = (bridgesQuestions = OPENCODE_CLI_BRIDGES_QUESTIONS) => ({
857
+ marker: OPENCODE_PLUGIN_MARKER,
858
+ source: openCodePluginSource(bridgesQuestions),
859
+ placeholder: OPENCODE_PLUGIN_PLACEHOLDER,
860
+ approvalEvents: {
861
+ placeholder: OPENCODE_APPROVAL_EVENTS_PLACEHOLDER,
862
+ events: OPENCODE_ASKED_EVENTS
863
+ },
864
+ sibling: {
865
+ path: OPENCODE_PLUGIN_SIBLING,
866
+ contents: OPENCODE_PLUGIN_SIBLING_CONTENTS
867
+ }
868
+ });
869
+ var renderOpenCodePlugin = (module, argv, holdsApprovals = true) => module.source.replace(module.placeholder, JSON.stringify(argv)).replace(module.approvalEvents.placeholder, JSON.stringify(holdsApprovals ? module.approvalEvents.events : []));
870
+ var COMMAND_LINE = /^const COMMAND = (.*)$/m;
871
+ var isArgv = (value) => Array.isArray(value) && value.every((item) => typeof item === "string");
872
+ var openCodePluginArgv = (contents) => {
873
+ if (!isOpenCodePluginOurs(contents)) return null;
874
+ const line = contents.match(COMMAND_LINE)?.[1];
875
+ if (line === void 0) return null;
876
+ try {
877
+ const parsed = JSON.parse(line);
878
+ return isArgv(parsed) ? parsed : null;
879
+ } catch {
880
+ return null;
881
+ }
882
+ };
883
+ var isOpenCodePluginOurs = (contents) => contents.includes(OPENCODE_PLUGIN_MARKER);
884
+
885
+ export {
886
+ openCodePluginModule,
887
+ renderOpenCodePlugin,
888
+ openCodePluginArgv,
889
+ isOpenCodePluginOurs
890
+ };