@cliphijack/santaclaude 0.9.6 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/santaclaude.js +37 -18
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cliphijack/santaclaude",
3
- "version": "0.9.6",
3
+ "version": "1.0.1",
4
4
  "publishConfig": { "access": "public" },
5
5
  "description": "SantaClaude 커넥터 — 클라우드 예약을 내 로컬 Claude(tmux)에 발사",
6
6
  "bin": { "santaclaude": "./santaclaude.js" },
package/santaclaude.js CHANGED
@@ -226,31 +226,50 @@ async function run(conf) {
226
226
  execFileSync('tmux', ['send-keys', '-t', pane, '-l', '🏪 "' + name + '" 마켓에 클린 등록됐어!']);
227
227
  setTimeout(() => { try { execFileSync('tmux', ['send-keys', '-t', pane, 'Enter']); } catch (e) {} }, 300);
228
228
  } else { console.warn(` 스킬 등록 실패(${name}): ${j.error}`); try { fs.unlinkSync(path.join(d, 'READY')); } catch (e) {} }
229
- } catch (e) {}
229
+ } catch (e) { console.error('[scan fetch err]', e.message); }
230
230
  }
231
- } catch (e) {}
231
+ } catch (e) { console.error('[scan err]', e.message); }
232
+ }
233
+
234
+ // 예약 발사(job) → 해당 탭에 주입
235
+ function onJob(j) {
236
+ if (!paneExists(session)) { if (!warned) { console.warn(` ⚠️ 작업장 "${session}" 없음 — 보류`); warned = true; } return; }
237
+ warned = false;
238
+ const tname = j.target && String(j.target).trim();
239
+ const tgt = (tname && windowExists(session, tname)) ? (session + ':' + tname) : pane;
240
+ console.log(`[발사] ${new Date().toISOString()} → ${tgt}: ${String(j.message).slice(0, 60)}`);
241
+ try { inject(tgt, '[SantaClaude] ' + j.message); } catch (e) { console.warn(` 주입 실패(${tgt}): ${e.message}`); }
232
242
  }
243
+ function sendHb() { if (ws && ws.readyState === 1) { try { ws.send(JSON.stringify({ type: 'hb', pane, sessions: listWindows(session), screens: captureAll(session) })); } catch (e) {} } }
233
244
 
234
- async function tick() {
245
+ // ── WebSocket 상시 연결 (폴링 제거 — DO가 예약/명령을 push) ──
246
+ const wsUrl = api.replace(/^http/, 'ws') + '/ws?token=' + encodeURIComponent(token);
247
+ let ws = null, hbIv = null;
248
+ function connectWS() {
249
+ try { ws = new WebSocket(wsUrl); } catch (e) { console.error(' WebSocket 생성 실패:', e.message); setTimeout(connectWS, 5000); return; }
250
+ ws.addEventListener('open', () => { console.log(' 🔌 클라우드 연결됨 (WebSocket).'); sendHb(); clearInterval(hbIv); hbIv = setInterval(sendHb, every); });
251
+ ws.addEventListener('message', (e) => { let m; try { m = JSON.parse(typeof e.data === 'string' ? e.data : e.data.toString()); } catch { return; } if (m.type === 'job') onJob(m); else if (m.type === 'ctl') runControl(m.cmd); });
252
+ ws.addEventListener('close', () => { clearInterval(hbIv); console.log(' 🔌 연결 끊김 — 5초 후 재연결'); setTimeout(connectWS, 5000); });
253
+ ws.addEventListener('error', () => { try { ws.close(); } catch (e) {} });
254
+ }
255
+ // 폴링 폴백 (node<21 = WebSocket 미지원) — 워커가 DO 백엔드라 폴링이어도 KV write 0
256
+ async function pollTick() {
235
257
  try {
236
- post(api, '/api/heartbeat', { token, pane, sessions: listWindows(session), screens: captureAll(session) }).catch(() => {}); // 보고 = 윈도우 목록 + 각 탭 화면 미러
237
- scanPublish();
258
+ post(api, '/api/heartbeat', { token, pane, sessions: listWindows(session), screens: captureAll(session) }).catch(() => {});
238
259
  post(api, '/api/control/claim', { token }).then((c) => { for (const cmd of (c && c.commands) || []) runControl(cmd); }).catch(() => {});
239
260
  const d = await post(api, '/api/jobs/claim', { token });
240
- for (const j of (d.jobs || [])) {
241
- if (!paneExists(session)) { if (!warned) { console.warn(` ⚠️ 작업장 "${session}" 없음 — 보류`); warned = true; } continue; }
242
- warned = false;
243
- // 대상 윈도우 지정 → session:target, 없으면 기본 pane(활성창)
244
- const tname = j.target && String(j.target).trim();
245
- const tgt = (tname && windowExists(session, tname)) ? (session + ':' + tname) : pane;
246
- console.log(`[발사] ${new Date().toISOString()} ${tgt}: ${String(j.message).slice(0, 60)}`);
247
- try { inject(tgt, '[SantaClaude] ' + j.message); } catch (e) { console.warn(` 주입 실패(${tgt}): ${e.message}`); }
248
- }
249
- } catch (e) { console.error('[폴링 오류]', e.message); }
261
+ for (const j of (d.jobs || [])) onJob(j);
262
+ } catch (e) {}
263
+ }
264
+ const scanIv = setInterval(scanPublish, Math.max(every, 10000)); // 로컬 클린등록 폴더 감지(주기)
265
+ if (typeof WebSocket !== 'undefined') {
266
+ connectWS();
267
+ process.on('SIGINT', () => { clearInterval(hbIv); clearInterval(scanIv); try { ws && ws.close(); } catch (e) {} console.log('\n🛷 커넥터 종료. 너의 루돌프들은 자러 간다.'); process.exit(0); });
268
+ } else {
269
+ console.log(` ℹ️ 이 node는 WebSocket(21+) 미지원 — 폴링 모드로 동작 (DO 백엔드라 KV write 0 동일, 발사 ±${Math.round(every / 1000)}초). node 21+ 쓰면 자동 WebSocket.`);
270
+ await pollTick(); const pollIv = setInterval(pollTick, every);
271
+ process.on('SIGINT', () => { clearInterval(pollIv); clearInterval(scanIv); console.log('\n🛷 커넥터 종료. 너의 루돌프들은 자러 간다.'); process.exit(0); });
250
272
  }
251
- await tick();
252
- const iv = setInterval(tick, every);
253
- process.on('SIGINT', () => { clearInterval(iv); console.log('\n🛷 커넥터 종료. 너의 루돌프들은 자러 간다.'); process.exit(0); });
254
273
  }
255
274
 
256
275
  async function main() {