@gonzih/cc-tg 0.9.2 → 0.9.3
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/README.md +36 -0
- package/dist/bot.d.ts +31 -4
- package/dist/bot.js +335 -312
- package/dist/formatter.d.ts +14 -12
- package/dist/formatter.js +72 -36
- package/dist/index.js +62 -3
- package/dist/notifier.d.ts +37 -0
- package/dist/notifier.js +124 -0
- package/dist/tokens.d.ts +22 -0
- package/dist/tokens.js +56 -0
- package/package.json +6 -5
- package/dist/cron.d.ts +0 -33
- package/dist/cron.js +0 -127
package/dist/cron.js
DELETED
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Cron job manager for cc-tg.
|
|
3
|
-
* Persists jobs to <cwd>/.cc-tg/crons.json.
|
|
4
|
-
* Fires prompts into Claude sessions on schedule.
|
|
5
|
-
*/
|
|
6
|
-
import { readFileSync, writeFileSync, mkdirSync, existsSync } from "fs";
|
|
7
|
-
import { join } from "path";
|
|
8
|
-
export class CronManager {
|
|
9
|
-
jobs = new Map();
|
|
10
|
-
storePath;
|
|
11
|
-
fire;
|
|
12
|
-
constructor(cwd, fire) {
|
|
13
|
-
this.storePath = join(cwd, ".cc-tg", "crons.json");
|
|
14
|
-
this.fire = fire;
|
|
15
|
-
this.load();
|
|
16
|
-
}
|
|
17
|
-
/** Parse "every 30m", "every 2h", "every 1d" → ms */
|
|
18
|
-
static parseSchedule(schedule) {
|
|
19
|
-
const m = schedule.trim().match(/^every\s+(\d+)(m|h|d)$/i);
|
|
20
|
-
if (!m)
|
|
21
|
-
return null;
|
|
22
|
-
const n = parseInt(m[1]);
|
|
23
|
-
const unit = m[2].toLowerCase();
|
|
24
|
-
if (unit === "m")
|
|
25
|
-
return n * 60_000;
|
|
26
|
-
if (unit === "h")
|
|
27
|
-
return n * 3_600_000;
|
|
28
|
-
if (unit === "d")
|
|
29
|
-
return n * 86_400_000;
|
|
30
|
-
return null;
|
|
31
|
-
}
|
|
32
|
-
add(chatId, schedule, prompt) {
|
|
33
|
-
const intervalMs = CronManager.parseSchedule(schedule);
|
|
34
|
-
if (!intervalMs)
|
|
35
|
-
return null;
|
|
36
|
-
const id = `${Date.now()}-${Math.random().toString(36).slice(2, 6)}`;
|
|
37
|
-
const job = { id, chatId, intervalMs, prompt, schedule, createdAt: new Date().toISOString() };
|
|
38
|
-
const timer = setInterval(() => {
|
|
39
|
-
console.log(`[cron:${id}] firing for chat=${chatId} prompt="${prompt}"`);
|
|
40
|
-
this.fire(chatId, prompt);
|
|
41
|
-
}, intervalMs);
|
|
42
|
-
this.jobs.set(id, { ...job, timer });
|
|
43
|
-
this.persist();
|
|
44
|
-
return job;
|
|
45
|
-
}
|
|
46
|
-
remove(chatId, id) {
|
|
47
|
-
const job = this.jobs.get(id);
|
|
48
|
-
if (!job || job.chatId !== chatId)
|
|
49
|
-
return false;
|
|
50
|
-
clearInterval(job.timer);
|
|
51
|
-
this.jobs.delete(id);
|
|
52
|
-
this.persist();
|
|
53
|
-
return true;
|
|
54
|
-
}
|
|
55
|
-
clearAll(chatId) {
|
|
56
|
-
let count = 0;
|
|
57
|
-
for (const [id, job] of this.jobs) {
|
|
58
|
-
if (job.chatId === chatId) {
|
|
59
|
-
clearInterval(job.timer);
|
|
60
|
-
this.jobs.delete(id);
|
|
61
|
-
count++;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
if (count)
|
|
65
|
-
this.persist();
|
|
66
|
-
return count;
|
|
67
|
-
}
|
|
68
|
-
list(chatId) {
|
|
69
|
-
return [...this.jobs.values()]
|
|
70
|
-
.filter((j) => j.chatId === chatId)
|
|
71
|
-
.map(({ timer: _t, ...j }) => j);
|
|
72
|
-
}
|
|
73
|
-
update(chatId, id, updates) {
|
|
74
|
-
const job = this.jobs.get(id);
|
|
75
|
-
if (!job || job.chatId !== chatId)
|
|
76
|
-
return false;
|
|
77
|
-
if (updates.schedule !== undefined) {
|
|
78
|
-
const intervalMs = CronManager.parseSchedule(updates.schedule);
|
|
79
|
-
if (!intervalMs)
|
|
80
|
-
return null;
|
|
81
|
-
job.intervalMs = intervalMs;
|
|
82
|
-
job.schedule = updates.schedule;
|
|
83
|
-
}
|
|
84
|
-
if (updates.prompt !== undefined) {
|
|
85
|
-
job.prompt = updates.prompt;
|
|
86
|
-
}
|
|
87
|
-
// Recreate timer so it uses updated intervalMs and always reads latest job.prompt
|
|
88
|
-
clearInterval(job.timer);
|
|
89
|
-
job.timer = setInterval(() => {
|
|
90
|
-
console.log(`[cron:${job.id}] firing for chat=${job.chatId} prompt="${job.prompt}"`);
|
|
91
|
-
this.fire(job.chatId, job.prompt);
|
|
92
|
-
}, job.intervalMs);
|
|
93
|
-
this.persist();
|
|
94
|
-
const { timer: _t, ...cronJob } = job;
|
|
95
|
-
return cronJob;
|
|
96
|
-
}
|
|
97
|
-
persist() {
|
|
98
|
-
try {
|
|
99
|
-
const dir = join(this.storePath, "..");
|
|
100
|
-
if (!existsSync(dir))
|
|
101
|
-
mkdirSync(dir, { recursive: true });
|
|
102
|
-
const data = [...this.jobs.values()].map(({ timer: _t, ...j }) => j);
|
|
103
|
-
writeFileSync(this.storePath, JSON.stringify(data, null, 2));
|
|
104
|
-
}
|
|
105
|
-
catch (err) {
|
|
106
|
-
console.error("[cron] persist error:", err.message);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
load() {
|
|
110
|
-
if (!existsSync(this.storePath))
|
|
111
|
-
return;
|
|
112
|
-
try {
|
|
113
|
-
const data = JSON.parse(readFileSync(this.storePath, "utf8"));
|
|
114
|
-
for (const job of data) {
|
|
115
|
-
const timer = setInterval(() => {
|
|
116
|
-
console.log(`[cron:${job.id}] firing for chat=${job.chatId} prompt="${job.prompt}"`);
|
|
117
|
-
this.fire(job.chatId, job.prompt);
|
|
118
|
-
}, job.intervalMs);
|
|
119
|
-
this.jobs.set(job.id, { ...job, timer });
|
|
120
|
-
}
|
|
121
|
-
console.log(`[cron] loaded ${data.length} jobs from disk`);
|
|
122
|
-
}
|
|
123
|
-
catch (err) {
|
|
124
|
-
console.error("[cron] load error:", err.message);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|