opencode-mobile 1.0.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.
Files changed (70) hide show
  1. package/dist/index.d.ts +4 -0
  2. package/dist/index.d.ts.map +1 -0
  3. package/dist/index.js +4 -0
  4. package/dist/index.js.map +1 -0
  5. package/dist/push-notifications.d.ts +4 -0
  6. package/dist/push-notifications.d.ts.map +1 -0
  7. package/dist/push-notifications.js +299 -0
  8. package/dist/push-notifications.js.map +1 -0
  9. package/dist/reverse-proxy.d.ts +9 -0
  10. package/dist/reverse-proxy.d.ts.map +1 -0
  11. package/dist/reverse-proxy.js +72 -0
  12. package/dist/reverse-proxy.js.map +1 -0
  13. package/dist/sdk-logger.d.ts +17 -0
  14. package/dist/sdk-logger.d.ts.map +1 -0
  15. package/dist/sdk-logger.js +94 -0
  16. package/dist/sdk-logger.js.map +1 -0
  17. package/dist/src/push/formatter.d.ts +25 -0
  18. package/dist/src/push/formatter.d.ts.map +1 -0
  19. package/dist/src/push/formatter.js +142 -0
  20. package/dist/src/push/formatter.js.map +1 -0
  21. package/dist/src/push/index.d.ts +8 -0
  22. package/dist/src/push/index.d.ts.map +1 -0
  23. package/dist/src/push/index.js +8 -0
  24. package/dist/src/push/index.js.map +1 -0
  25. package/dist/src/push/sender.d.ts +9 -0
  26. package/dist/src/push/sender.d.ts.map +1 -0
  27. package/dist/src/push/sender.js +75 -0
  28. package/dist/src/push/sender.js.map +1 -0
  29. package/dist/src/push/token-store.d.ts +17 -0
  30. package/dist/src/push/token-store.d.ts.map +1 -0
  31. package/dist/src/push/token-store.js +42 -0
  32. package/dist/src/push/token-store.js.map +1 -0
  33. package/dist/src/push/types.d.ts +51 -0
  34. package/dist/src/push/types.d.ts.map +1 -0
  35. package/dist/src/push/types.js +5 -0
  36. package/dist/src/push/types.js.map +1 -0
  37. package/dist/src/tunnel/cloudflare.d.ts +17 -0
  38. package/dist/src/tunnel/cloudflare.d.ts.map +1 -0
  39. package/dist/src/tunnel/cloudflare.js +74 -0
  40. package/dist/src/tunnel/cloudflare.js.map +1 -0
  41. package/dist/src/tunnel/index.d.ts +31 -0
  42. package/dist/src/tunnel/index.d.ts.map +1 -0
  43. package/dist/src/tunnel/index.js +83 -0
  44. package/dist/src/tunnel/index.js.map +1 -0
  45. package/dist/src/tunnel/localtunnel.d.ts +13 -0
  46. package/dist/src/tunnel/localtunnel.d.ts.map +1 -0
  47. package/dist/src/tunnel/localtunnel.js +31 -0
  48. package/dist/src/tunnel/localtunnel.js.map +1 -0
  49. package/dist/src/tunnel/ngrok.d.ts +21 -0
  50. package/dist/src/tunnel/ngrok.d.ts.map +1 -0
  51. package/dist/src/tunnel/ngrok.js +91 -0
  52. package/dist/src/tunnel/ngrok.js.map +1 -0
  53. package/dist/src/tunnel/qrcode.d.ts +12 -0
  54. package/dist/src/tunnel/qrcode.d.ts.map +1 -0
  55. package/dist/src/tunnel/qrcode.js +24 -0
  56. package/dist/src/tunnel/qrcode.js.map +1 -0
  57. package/dist/src/tunnel/types.d.ts +32 -0
  58. package/dist/src/tunnel/types.d.ts.map +1 -0
  59. package/dist/src/tunnel/types.js +5 -0
  60. package/dist/src/tunnel/types.js.map +1 -0
  61. package/dist/src/utils/port.d.ts +12 -0
  62. package/dist/src/utils/port.d.ts.map +1 -0
  63. package/dist/src/utils/port.js +41 -0
  64. package/dist/src/utils/port.js.map +1 -0
  65. package/dist/tunnel-manager.d.ts +30 -0
  66. package/dist/tunnel-manager.d.ts.map +1 -0
  67. package/dist/tunnel-manager.js +639 -0
  68. package/dist/tunnel-manager.js.map +1 -0
  69. package/package.json +60 -0
  70. package/push-notifications.ts +346 -0
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "opencode-mobile",
3
+ "version": "1.0.0",
4
+ "description": "Mobile push notification plugin for OpenCode - enables push notifications via Expo for mobile devices",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "type": "module",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "push-notifications.ts"
17
+ ],
18
+ "keywords": [
19
+ "opencode",
20
+ "plugin",
21
+ "push-notifications",
22
+ "mobile",
23
+ "expo"
24
+ ],
25
+ "author": "",
26
+ "license": "MIT",
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "https://github.com/doza62/opencode-mobile"
30
+ },
31
+ "homepage": "https://github.com/doza62/opencode-mobile#readme",
32
+ "bugs": {
33
+ "url": "https://github.com/doza62/opencode-mobile/issues"
34
+ },
35
+ "publishConfig": {
36
+ "access": "public"
37
+ },
38
+ "dependencies": {
39
+ "@ngrok/ngrok": "^1.7.0",
40
+ "@opencode-ai/plugin": "^1.1.12",
41
+ "cloudflared": "^0.1.1",
42
+ "cloudflared-tunnel": "^1.0.3",
43
+ "localtunnel": "^2.0.2",
44
+ "ngrok": "^5.0.0-beta.2",
45
+ "qrcode": "^1.5.4",
46
+ "qrcode-terminal": "^0.12.0"
47
+ },
48
+ "devDependencies": {
49
+ "@types/node": "^22.13.9",
50
+ "@types/qrcode": "^1.5.6",
51
+ "@types/qrcode-terminal": "^0.12.2",
52
+ "typescript": "^5.8.2"
53
+ },
54
+ "scripts": {
55
+ "build": "tsc",
56
+ "typecheck": "tsc --noEmit",
57
+ "prepublishOnly": "npm run build",
58
+ "test": "echo \"Error: no test specified\" && exit 1"
59
+ }
60
+ }
@@ -0,0 +1,346 @@
1
+ import * as fs from "fs";
2
+ import * as path from "path";
3
+ import * as net from "net";
4
+
5
+ declare const Bun: any;
6
+
7
+ import type { Plugin } from "@opencode-ai/plugin";
8
+ import { startTunnel, stopTunnel, displayQRCode as displayQR, getTunnelDetails } from "./src/tunnel";
9
+ import { getNextAvailablePort } from "./src/utils/port";
10
+ import type { PushToken, Notification, NotificationEvent, PluginContext } from "./src/push";
11
+ import { loadTokens, saveTokens, truncate } from "./src/push/token-store";
12
+ import { formatNotification } from "./src/push/formatter";
13
+ import { sendPush } from "./src/push/sender";
14
+
15
+ import { createLogger, configureLogger } from "./sdk-logger";
16
+
17
+ const CONFIG_DIR = path.join(process.env.HOME || "", ".config/opencode");
18
+
19
+ // Simple console logging (no client.log dependency)
20
+ const logger = createLogger("PushPlugin");
21
+ const TOKEN_FILE = path.join(CONFIG_DIR, "push-tokens.json");
22
+
23
+ let tokenServerStarted = false;
24
+ let pluginInitialized = false;
25
+ let bunServer: any = null;
26
+ let bunServerPort: number | null = null;
27
+
28
+ async function startTokenServer(
29
+ openCodeUrl: string,
30
+ port: number,
31
+ ): Promise<void> {
32
+ if (tokenServerStarted) return;
33
+
34
+ const cors = {
35
+ "Access-Control-Allow-Origin": "*",
36
+ "Access-Control-Allow-Methods": "GET, POST, DELETE, OPTIONS",
37
+ "Access-Control-Allow-Headers": "Content-Type, x-opencode-directory",
38
+ };
39
+
40
+ bunServer = Bun.serve({
41
+ port: port,
42
+ hostname: "0.0.0.0",
43
+ idleTimeout: 0, // Disable timeout for SSE connections
44
+ async fetch(req: Request) {
45
+ const url = new URL(req.url);
46
+ if (req.method === "OPTIONS")
47
+ return new Response(null, { status: 204, headers: cors });
48
+
49
+ if (url.pathname === "/push-token" && req.method === "POST") {
50
+ const body = (await req.json()) as {
51
+ token?: string;
52
+ platform?: string;
53
+ deviceId?: string;
54
+ };
55
+ const { token, platform, deviceId } = body;
56
+ if (!token || !deviceId)
57
+ return new Response(JSON.stringify({ error: "Missing fields" }), {
58
+ status: 400,
59
+ headers: cors,
60
+ });
61
+
62
+ // Validate platform field
63
+ const validPlatform = (platform && (platform === "ios" || platform === "android"))
64
+ ? platform
65
+ : "ios";
66
+
67
+ const tokens = loadTokens();
68
+ const idx = tokens.findIndex((t) => t.deviceId === deviceId);
69
+ const newToken: PushToken = {
70
+ token,
71
+ platform: validPlatform,
72
+ deviceId,
73
+ registeredAt: new Date().toISOString(),
74
+ };
75
+ if (idx >= 0) tokens[idx] = newToken;
76
+ else tokens.push(newToken);
77
+ saveTokens(tokens);
78
+ return new Response(JSON.stringify({ success: true }), {
79
+ status: 200,
80
+ headers: cors,
81
+ });
82
+ }
83
+
84
+ if (url.pathname === "/push-token" && req.method === "DELETE") {
85
+ const body = (await req.json()) as { deviceId?: string };
86
+ const { deviceId } = body;
87
+ saveTokens(loadTokens().filter((t) => t.deviceId !== deviceId));
88
+ return new Response(JSON.stringify({ success: true }), {
89
+ status: 200,
90
+ headers: cors,
91
+ });
92
+ }
93
+
94
+ if (url.pathname === "/push-token" && req.method === "GET") {
95
+ return new Response(JSON.stringify({ count: loadTokens().length }), {
96
+ status: 200,
97
+ headers: cors,
98
+ });
99
+ }
100
+
101
+ if (url.pathname === "/push-token/test" && req.method === "POST") {
102
+ await sendPush({
103
+ title: "Test",
104
+ body: "Push notifications working!",
105
+ data: { type: "test", serverUrl: openCodeUrl },
106
+ });
107
+ return new Response(JSON.stringify({ success: true }), {
108
+ status: 200,
109
+ headers: cors,
110
+ });
111
+ }
112
+
113
+ // Return tunnel information
114
+ if (url.pathname === "/tunnel" && req.method === "GET") {
115
+ const details = getTunnelDetails();
116
+ return new Response(JSON.stringify(details, null, 2), {
117
+ status: 200,
118
+ headers: { ...cors, "Content-Type": "application/json" },
119
+ });
120
+ }
121
+
122
+ // Proxy everything else directly to OpenCode server (transparent)
123
+ const pathname = url.pathname + url.search;
124
+ try {
125
+ const controller = new AbortController();
126
+ const timeoutId = setTimeout(() => controller.abort(), 10000); // 10 second timeout
127
+ const proxyReq = new Request(`${openCodeUrl}${pathname}`, {
128
+ method: req.method,
129
+ headers: req.headers,
130
+ body: req.body,
131
+ redirect: "follow",
132
+ signal: controller.signal,
133
+ });
134
+ try {
135
+ return await fetch(proxyReq);
136
+ } catch (e: any) {
137
+ if (e.name === 'AbortError') {
138
+ return new Response("Gateway timeout", { status: 504, headers: cors });
139
+ }
140
+ throw e;
141
+ } finally {
142
+ clearTimeout(timeoutId);
143
+ }
144
+ } catch (e) {
145
+ logger.error("Proxy error:", e);
146
+ return new Response("Proxy error", { status: 502, headers: cors });
147
+ }
148
+ },
149
+ });
150
+
151
+ tokenServerStarted = true;
152
+ }
153
+
154
+
155
+ function stopAll(): void {
156
+ logger.info("Shutting down...");
157
+
158
+ stopTunnel();
159
+ if (bunServer) {
160
+ logger.info("Stopping Bun server...");
161
+ bunServer.stop();
162
+ bunServer = null;
163
+ }
164
+ tokenServerStarted = false;
165
+ logger.info("Shutdown complete");
166
+ }
167
+
168
+ export const PushNotificationPlugin: Plugin = async (ctx) => {
169
+ if (pluginInitialized) {
170
+ return { event: async ({ event }) => {} };
171
+ }
172
+ pluginInitialized = true;
173
+
174
+ // Skip tunnel setup in server mode (no --serve flag)
175
+ const processArgs = process.argv.slice(2).join(' ');
176
+ const hasServeFlag = processArgs.includes('--serve') || processArgs.includes('serve');
177
+
178
+ if (!hasServeFlag) {
179
+ return {
180
+ event: async ({ event }) => {},
181
+ };
182
+ }
183
+
184
+ const serverPort = parseInt(String((ctx as any).serverUrl?.port || 4096), 10);
185
+ logger.info(`App started. Creating tunnel with ngrok to port ${serverPort}`);
186
+
187
+ // Test logging with different levels
188
+ logger.debug("Detailed development info - appears only with --log-level DEBUG");
189
+ logger.info("Standard operational message - appears with --log-level INFO, WARN, ERROR");
190
+ logger.warn("Non-critical issue warning - appears with --log-level WARN, ERROR");
191
+ logger.error("Critical failure message - appears only with --log-level ERROR");
192
+
193
+ // Get server port from ctx, find next available for push-token
194
+ const pushTokenPort = await getNextAvailablePort(serverPort + 1);
195
+ bunServerPort = pushTokenPort;
196
+ const openCodeUrl = `http://127.0.0.1:${serverPort}`;
197
+
198
+ logger.info(`Token API: http://127.0.0.1:${pushTokenPort} → ${openCodeUrl}`);
199
+
200
+ try {
201
+ await startTokenServer(openCodeUrl, pushTokenPort);
202
+
203
+ let tunnelInfo: any;
204
+ let ngrokFailed = false;
205
+
206
+ try {
207
+ // Create tunnel directly to OpenCode server (transparent proxy)
208
+ tunnelInfo = await startTunnel({ port: serverPort, provider: "ngrok" });
209
+ } catch (ngrokError: any) {
210
+ const errorMsg = ngrokError.message.toLowerCase();
211
+ const isAuthIssue = errorMsg.includes("invalid tunnel configuration") ||
212
+ errorMsg.includes("authtoken") ||
213
+ errorMsg.includes("authentication") ||
214
+ errorMsg.includes("auth token") ||
215
+ errorMsg.includes("session failed") ||
216
+ errorMsg.includes("connect to api.ngrok.com");
217
+
218
+ if (isAuthIssue) {
219
+ logger.info("Ngrok needs authtoken (ERR_NGROK_4018)");
220
+
221
+ const readline = await import("readline");
222
+ const rl = readline.createInterface({
223
+ input: process.stdin,
224
+ output: process.stdout,
225
+ });
226
+
227
+ const newToken = await new Promise<string | null>((resolve) => {
228
+ rl.question("[?] Enter ngrok authtoken (or Enter to skip): ", async (token) => {
229
+ rl.close();
230
+ resolve(token && token.trim().length > 25 ? token.trim() : null);
231
+ });
232
+ });
233
+
234
+ if (newToken) {
235
+ logger.info("Writing ngrok config...");
236
+
237
+ // Basic validation - ngrok authtokens are typically 50+ characters
238
+ if (!newToken || newToken.length < 25) {
239
+ logger.error("Invalid authtoken format");
240
+ ngrokFailed = true;
241
+ } else {
242
+ try {
243
+ const { writeFileSync, existsSync, mkdirSync } = fs;
244
+ const configPath = `${process.env.HOME}/Library/Application Support/ngrok/ngrok.yml`;
245
+ const configDir = `${process.env.HOME}/Library/Application Support/ngrok`;
246
+
247
+ if (!existsSync(configDir)) {
248
+ mkdirSync(configDir, { recursive: true });
249
+ }
250
+
251
+ // Escape any special characters that could break YAML
252
+ const escapedToken = newToken.replace(/[:\[\]{}|>&*?!@]/g, '\\$&');
253
+ const v3Config = `version: "3"
254
+
255
+ agent:
256
+ authtoken: ${escapedToken}
257
+ `;
258
+ writeFileSync(configPath, v3Config, { mode: 0o600 });
259
+ logger.info(`Config: ${configPath}`);
260
+
261
+ // Retry with new authtoken (pass it directly to ngrok.connect)
262
+ tunnelInfo = await startTunnel({
263
+ port: serverPort,
264
+ provider: "ngrok",
265
+ authToken: newToken
266
+ });
267
+ logger.info("Ngrok tunnel active!");
268
+ } catch (retryError: any) {
269
+ logger.error(`Retry failed: ${retryError.message}`);
270
+ ngrokFailed = true;
271
+ }
272
+ }
273
+ } else {
274
+ ngrokFailed = true;
275
+ }
276
+ } else {
277
+ ngrokFailed = true;
278
+ }
279
+
280
+ if (ngrokFailed) {
281
+ logger.info("Trying localtunnel...");
282
+ try {
283
+ tunnelInfo = await startTunnel({ port: serverPort, provider: "localtunnel" });
284
+ } catch (localtunnelError: any) {
285
+ logger.error("Localtunnel failed:", localtunnelError.message);
286
+ throw new Error("All tunnel providers failed");
287
+ }
288
+ }
289
+ }
290
+
291
+ if (!tunnelInfo) {
292
+ throw new Error("Failed to establish tunnel");
293
+ }
294
+
295
+ await displayQR(tunnelInfo.url);
296
+
297
+ return {
298
+ event: async ({ event }) => {
299
+ // Format notification from event
300
+ const notification = formatNotification(event, tunnelInfo.url, ctx);
301
+
302
+ if (!notification) {
303
+ // No notification needed for this event type
304
+ return;
305
+ }
306
+
307
+ // Log the raw event that triggered this push
308
+ const eventAny = event as any;
309
+ logger.info("Raw event received", {
310
+ type: event.type,
311
+ sessionID: eventAny.sessionID || eventAny.sessionId,
312
+ timestamp: eventAny.timestamp,
313
+ properties: event.properties
314
+ });
315
+
316
+ // Log the formatted notification
317
+ logger.info("Sending push notification", {
318
+ title: notification.title,
319
+ body: notification.body,
320
+ sessionId: notification.data?.sessionId,
321
+ type: notification.data?.type
322
+ });
323
+
324
+ // Send the push notification
325
+ await sendPush(notification);
326
+
327
+ logger.debug("Push notification sent successfully");
328
+ },
329
+ };
330
+ } catch (error: any) {
331
+ logger.error("Failed:", error.message);
332
+ return { event: async ({ event }) => {} };
333
+ }
334
+ };
335
+
336
+ process.on("SIGTERM", () => {
337
+ stopAll();
338
+ });
339
+ process.on("SIGINT", () => {
340
+ stopAll();
341
+ });
342
+ process.on("SIGHUP", () => {
343
+ stopAll();
344
+ });
345
+
346
+ export default PushNotificationPlugin;