natureco-cli 1.0.19 → 1.0.21
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/package.json +1 -1
- package/src/commands/dashboard.js +68 -0
package/package.json
CHANGED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
const http = require('http');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const path = require('path');
|
|
4
|
+
const os = require('os');
|
|
5
|
+
const chalk = require('chalk');
|
|
6
|
+
|
|
7
|
+
const CONFIG_DIR = path.join(os.homedir(), '.natureco');
|
|
8
|
+
const PID_FILE = path.join(CONFIG_DIR, 'dashboard.pid');
|
|
9
|
+
const PORT = 3848;
|
|
10
|
+
|
|
11
|
+
function getConfig() {
|
|
12
|
+
try { return JSON.parse(fs.readFileSync(path.join(CONFIG_DIR, 'config.json'), 'utf8')); }
|
|
13
|
+
catch { return {}; }
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function openBrowser(url) {
|
|
17
|
+
const { exec } = require('child_process');
|
|
18
|
+
const p = process.platform;
|
|
19
|
+
exec(p === 'darwin' ? 'open ' + url : p === 'win32' ? 'start ' + url : 'xdg-open ' + url);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const HTML = '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>NatureCo Dashboard</title><style>*{margin:0;padding:0;box-sizing:border-box}body{background:#050a0e;color:#eceff1;font-family:system-ui,sans-serif;display:flex;height:100vh}.sidebar{width:260px;background:rgba(255,255,255,0.03);border-right:1px solid rgba(0,230,118,0.1);padding:20px;display:flex;flex-direction:column}.logo{color:#00E676;font-size:20px;font-weight:700;margin-bottom:8px;letter-spacing:1px}.chat{flex:1;display:flex;flex-direction:column}.header{padding:16px 20px;border-bottom:1px solid rgba(0,230,118,0.1);display:flex;align-items:center;gap:8px}.messages{flex:1;padding:20px;overflow-y:auto}.input-area{padding:20px;border-top:1px solid rgba(0,230,118,0.1);display:flex;gap:10px}.input-area input{flex:1;background:rgba(255,255,255,0.05);border:1px solid rgba(0,230,118,0.2);border-radius:8px;padding:12px;color:#eceff1;outline:none;font-size:14px}.input-area button{background:linear-gradient(135deg,#00E676,#00BCD4);border:none;border-radius:8px;padding:12px 24px;color:#000;font-weight:700;cursor:pointer;font-size:14px}.msg{margin:8px 0;display:flex}.msg.user{justify-content:flex-end}.msg.user .bubble{background:linear-gradient(135deg,#00E676,#00BCD4);color:#000;border-radius:12px 12px 0 12px;padding:10px 14px;max-width:70%;font-size:14px}.msg.bot .bubble{background:rgba(255,255,255,0.05);border:1px solid rgba(0,230,118,0.1);border-radius:12px 12px 12px 0;padding:10px 14px;max-width:70%;font-size:14px}</style></head><body><div class="sidebar"><div class="logo">NatureCo</div><div id="botName" style="color:#546e7a;font-size:14px;margin-top:4px">Loading...</div><div style="margin-top:auto;color:#546e7a;font-size:12px">natureco.me</div></div><div class="chat"><div class="header"><span id="chatBot" style="color:#eceff1;font-weight:600">Dashboard</span><span style="width:8px;height:8px;background:#00E676;border-radius:50%;display:inline-block"></span><span style="color:#00E676;font-size:12px">Online</span></div><div class="messages" id="messages"></div><div class="input-area"><input id="input" placeholder="Mesaj yaz..." onkeydown="if(event.key===\')send()"><button onclick="send()">Gonder</button></div></div><script>var cfg={};fetch("/config").then(function(r){return r.json()}).then(function(d){cfg=d;document.getElementById("botName").textContent=d.defaultBot||"-";document.getElementById("chatBot").textContent=d.defaultBot||"-"});function send(){var i=document.getElementById("input");var m=i.value.trim();if(!m)return;i.value="";addMsg("user",m);fetch("https://api.natureco.me/api/agent/chat",{method:"POST",headers:{"Content-Type":"application/json","Authorization":"Bearer "+cfg.apiKey,"X-User-ID":"dashboard-user"},body:JSON.stringify({agent_id:cfg.defaultBotId,message:m,platform:"dashboard",user_id:"dashboard-user"})}).then(function(r){return r.json()}).then(function(d){addMsg("bot",d.reply||d.message||"Hata")}).catch(function(){addMsg("bot","Baglanti hatasi")})}function addMsg(t,m){var d=document.getElementById("messages");var el=document.createElement("div");el.className="msg "+t;el.innerHTML="<div class=bubble>"+m+"</div>";d.appendChild(el);d.scrollTop=d.scrollHeight}</script></body></html>';
|
|
23
|
+
|
|
24
|
+
function dashboard(action) {
|
|
25
|
+
action = action || 'start';
|
|
26
|
+
if (action === 'start') {
|
|
27
|
+
const cfg = getConfig();
|
|
28
|
+
const server = http.createServer((req, res) => {
|
|
29
|
+
if (req.url === '/config') {
|
|
30
|
+
res.writeHead(200, { 'Content-Type': 'application/json' });
|
|
31
|
+
res.end(JSON.stringify({ apiKey: cfg.apiKey, defaultBot: cfg.defaultBot, defaultBotId: cfg.defaultBotId }));
|
|
32
|
+
} else {
|
|
33
|
+
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
|
34
|
+
res.end(HTML);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
server.listen(PORT, () => {
|
|
38
|
+
fs.mkdirSync(CONFIG_DIR, { recursive: true });
|
|
39
|
+
fs.writeFileSync(PID_FILE, String(process.pid));
|
|
40
|
+
console.log(chalk.green('Dashboard started!'));
|
|
41
|
+
console.log(chalk.cyan('URL: ') + 'http://localhost:' + PORT);
|
|
42
|
+
openBrowser('http://localhost:' + PORT);
|
|
43
|
+
console.log(chalk.gray('Press Ctrl+C to stop'));
|
|
44
|
+
});
|
|
45
|
+
process.on('SIGINT', () => {
|
|
46
|
+
try { fs.unlinkSync(PID_FILE); } catch {}
|
|
47
|
+
server.close();
|
|
48
|
+
console.log(chalk.green('Dashboard stopped'));
|
|
49
|
+
process.exit(0);
|
|
50
|
+
});
|
|
51
|
+
} else if (action === 'stop') {
|
|
52
|
+
try {
|
|
53
|
+
const pid = parseInt(fs.readFileSync(PID_FILE, 'utf8'));
|
|
54
|
+
process.kill(pid);
|
|
55
|
+
fs.unlinkSync(PID_FILE);
|
|
56
|
+
console.log(chalk.green('Dashboard stopped'));
|
|
57
|
+
} catch { console.log(chalk.red('Dashboard not running')); }
|
|
58
|
+
} else if (action === 'status') {
|
|
59
|
+
try {
|
|
60
|
+
const pid = parseInt(fs.readFileSync(PID_FILE, 'utf8'));
|
|
61
|
+
console.log(chalk.green('Dashboard running'));
|
|
62
|
+
console.log('PID:', pid);
|
|
63
|
+
console.log('URL: http://localhost:' + PORT);
|
|
64
|
+
} catch { console.log(chalk.gray('Dashboard not running')); }
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
module.exports = dashboard;
|