File size: 2,435 Bytes
d739b1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const { Notification } = require('electron');

/**
 * ๋ฐ์Šคํฌํ†ฑ ๋„ค์ดํ‹ฐ๋ธŒ ์•Œ๋ฆผ ๋ฐœ์†ก ํ—ฌํผ
 * @param {string} title ์•Œ๋ฆผ ์ œ๋ชฉ
 * @param {string} body ์•Œ๋ฆผ ๋‚ด์šฉ
 * @param {'normal'|'critical'} urgency ๊ธด๊ธ‰์„ฑ ๋ ˆ๋ฒจ (critical ์‹œ ๋ฌด์Œ ์•„๋‹˜, ์•Œ๋ฆผ์ฐฝ ์ƒ์‹œ ๋Œ€๊ธฐ)
 */
function sendNotification(title, body, urgency = 'normal') {
  if (!Notification?.isSupported?.()) {
    console.warn('[Notifier] Native notifications are not supported on this platform.');
    return { status: 'unsupported' };
  }

  try {
    const notification = new Notification({
      title: title,
      body: body,
      silent: urgency === 'normal',
      // macOS์˜ ๊ฒฝ์šฐ urgency ๊ฐ€ critical ์ด๋ฉด ๋ฐฐ๋„ˆ๊ฐ€ ๋‹ซํžˆ์ง€ ์•Š๊ณ  ๋จธ๋ฌผ๋„๋ก ์„ค์ •
      timeoutType: urgency === 'critical' ? 'never' : 'default'
    });
    
    // ์•Œ๋ฆผ ํด๋ฆญ ์‹œ ๋ฉ”์ธ ์œˆ๋„์šฐ ํฌ์ปค์‹ฑ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ
    notification.on('click', () => {
      const ctx = require('./context');
      if (ctx.mainWindow) {
        ctx.mainWindow.show();
        ctx.mainWindow.focus();
      }
    });

    notification.show();
    return { status: 'submitted' };
  } catch (err) {
    console.error('[Notifier] Failed to show native notification:', err);
    return { status: 'uncertain' };
  }
}

module.exports = {
  /**
   * ๋ฃจํ”„ ์ตœ์ข… ์™„๋ฃŒ ์‹œ ์•Œ๋ฆผ
   */
  notifyLoopComplete(runId, success, reason, cost) {
    const title = success ? 'โœ… Closed-Loop ์ตœ์ข… ์„ฑ๊ณต' : 'โŒ Closed-Loop ์ข…๋ฃŒ (์‹คํŒจ)';
    const body = success 
      ? `ํƒœ์Šคํฌ ${runId}๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์ตœ์ข… ๋ชฉํ‘œ(Goal)๋ฅผ ์™„์ˆ˜ํ–ˆ์Šต๋‹ˆ๋‹ค! (๋น„์šฉ: $${cost.toFixed(2)})`
      : `ํƒœ์Šคํฌ ${runId}๊ฐ€ ๋ชฉํ‘œ ๋‹ฌ์„ฑ์— ์‹คํŒจํ•˜๊ฑฐ๋‚˜ ์ค‘๋‹จ๋˜์—ˆ์Šต๋‹ˆ๋‹ค: ${reason}`;
    return sendNotification(title, body, success ? 'normal' : 'critical');
  },

  /**
   * ์˜ˆ์‚ฐ 80% ๊ฒฝ๊ณ  ๋“ฑ ๋ฃจํ”„ ๊ฒฝ๊ณ  ์•Œ๋ฆผ
   */
  notifyLoopWarning(runId, message) {
    const title = 'โš ๏ธ Closed-Loop ๊ฐ€๋“œ๋ ˆ์ผ ๊ฒฝ๊ณ ';
    const body = `ํƒœ์Šคํฌ ${runId}: ${message}`;
    return sendNotification(title, body, 'normal');
  },

  /**
   * HITL (์‚ฌ๋žŒ์˜ ํŒ๋‹จ) ๋Œ€๊ธฐ ์•Œ๋ฆผ
   */
  notifyHitlWait(runId, message) {
    const title = '๐Ÿ™‹ Closed-Loop ์‚ฌ๋žŒ์˜ ๊ฐœ์ž… ๋Œ€๊ธฐ';
    const body = `ํƒœ์Šคํฌ ${runId}๊ฐ€ ์‚ฌ์šฉ์ž์˜ ๋ถ„์„ ๋ฐ ์Šน์ธ ํ”ผ๋“œ๋ฐฑ์„ ๊ธฐ๋‹ค๋ฆฝ๋‹ˆ๋‹ค: ${message}`;
    return sendNotification(title, body, 'critical');
  }
};