@hasna/prompts 0.3.4 → 0.3.6
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/LICENSE +170 -0
- package/dist/cli/index.js +292 -0
- package/dist/db/database.d.ts.map +1 -1
- package/dist/db/schedules.d.ts +29 -0
- package/dist/db/schedules.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/lib/cron.d.ts +3 -0
- package/dist/lib/cron.d.ts.map +1 -0
- package/dist/mcp/index.js +279 -0
- package/package.json +2 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship made available under
|
|
36
|
+
the License, as indicated by a copyright notice that is included in
|
|
37
|
+
or attached to the work (an example is provided in the Appendix below).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
43
|
+
of this License, Derivative Works shall not include works that remain
|
|
44
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
45
|
+
the Work and Derivative Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean, as submitted to the Licensor for inclusion
|
|
48
|
+
in the Work by the copyright owner or by an individual or Legal Entity
|
|
49
|
+
authorized to submit on behalf of the copyright owner. For the purposes
|
|
50
|
+
of this definition, "submitted" means any form of electronic, verbal,
|
|
51
|
+
or written communication sent to the Licensor or its representatives,
|
|
52
|
+
including but not limited to communication on electronic mailing lists,
|
|
53
|
+
source code control systems, and issue tracking systems that are managed
|
|
54
|
+
by, or on behalf of, the Licensor for the purpose of recording and
|
|
55
|
+
discussing the Work, but excluding communication that is conspicuously
|
|
56
|
+
marked or designated in writing by the copyright owner as "Not a
|
|
57
|
+
Contribution."
|
|
58
|
+
|
|
59
|
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of
|
|
60
|
+
whom a Contribution has been received by the Licensor and included
|
|
61
|
+
within the Work.
|
|
62
|
+
|
|
63
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
64
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
65
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
66
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
67
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
68
|
+
Work and such Derivative Works in Source or Object form.
|
|
69
|
+
|
|
70
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
71
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
72
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
73
|
+
(except as stated in this section) patent license to make, have made,
|
|
74
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
75
|
+
where such license applies only to those patent contributions
|
|
76
|
+
Licensors can make available under this License.
|
|
77
|
+
|
|
78
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
79
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
80
|
+
modifications, and in Source or Object form, provided that You
|
|
81
|
+
meet the following conditions:
|
|
82
|
+
|
|
83
|
+
(a) You must give any other recipients of the Work or Derivative Works
|
|
84
|
+
a copy of this License; and
|
|
85
|
+
|
|
86
|
+
(b) You must cause any modified files to carry prominent notices
|
|
87
|
+
stating that You changed the files; and
|
|
88
|
+
|
|
89
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
90
|
+
that You distribute, all copyright, patent, trademark, and
|
|
91
|
+
attribution notices from the Source form of the Work,
|
|
92
|
+
excluding those notices that do not pertain to any part of
|
|
93
|
+
the Derivative Works; and
|
|
94
|
+
|
|
95
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
96
|
+
distribution, You must include a readable copy of the attribution
|
|
97
|
+
notices contained within such NOTICE file, in at least one
|
|
98
|
+
of the following places: within a NOTICE text file distributed
|
|
99
|
+
as part of the Derivative Works; within the Source form or
|
|
100
|
+
documentation, if provided along with the Derivative Works; or,
|
|
101
|
+
within a display generated by the Derivative Works, if and
|
|
102
|
+
wherever such third-party notices normally appear. The contents
|
|
103
|
+
of the NOTICE file are for informational purposes only and
|
|
104
|
+
do not modify the License. You may add Your own attribution
|
|
105
|
+
notices within Derivative Works that You distribute, alongside
|
|
106
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
107
|
+
that such additional attribution notices cannot be construed
|
|
108
|
+
as modifying the License.
|
|
109
|
+
|
|
110
|
+
You may add Your own license statement for Your modifications and
|
|
111
|
+
may provide additional grant of rights to use, copy, modify, merge,
|
|
112
|
+
publish, distribute, sublicense, and/or sell copies of the Software.
|
|
113
|
+
|
|
114
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
115
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
116
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
117
|
+
this License, without any additional terms or conditions.
|
|
118
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
119
|
+
the terms of any separate license agreement you may have executed
|
|
120
|
+
with Licensor regarding such Contributions.
|
|
121
|
+
|
|
122
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
123
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
124
|
+
except as required for reasonable and customary use in describing the
|
|
125
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
126
|
+
|
|
127
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
128
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
129
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
130
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
131
|
+
implied, including, without limitation, any warranties or conditions
|
|
132
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
133
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
134
|
+
appropriateness of using or redistributing the Work and assume any
|
|
135
|
+
risks associated with Your exercise of permissions under this License.
|
|
136
|
+
|
|
137
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
138
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
139
|
+
unless required by applicable law (such as deliberate and grossly
|
|
140
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
141
|
+
liable to You for damages, including any direct, indirect, special,
|
|
142
|
+
incidental, or exemplary damages of any character arising as a
|
|
143
|
+
result of this License or out of the use or inability to use the
|
|
144
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
145
|
+
work stoppage, computer failure or malfunction, or all other
|
|
146
|
+
commercial damages or losses), even if such Contributor has been
|
|
147
|
+
advised of the possibility of such damages.
|
|
148
|
+
|
|
149
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
150
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
151
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
152
|
+
or other liability obligations and/or rights consistent with this
|
|
153
|
+
License. However, in accepting such obligations, You may offer only
|
|
154
|
+
conditions consistent with this License.
|
|
155
|
+
|
|
156
|
+
END OF TERMS AND CONDITIONS
|
|
157
|
+
|
|
158
|
+
Copyright 2024 hasna.com
|
|
159
|
+
|
|
160
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
161
|
+
you may not use this file except in compliance with the License.
|
|
162
|
+
You may obtain a copy of the License at
|
|
163
|
+
|
|
164
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
165
|
+
|
|
166
|
+
Unless required by applicable law or agreed to in writing, software
|
|
167
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
168
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
169
|
+
See the License for the specific language governing permissions and
|
|
170
|
+
limitations under the License.
|
package/dist/cli/index.js
CHANGED
|
@@ -2229,6 +2229,25 @@ function runMigrations(db) {
|
|
|
2229
2229
|
CREATE INDEX IF NOT EXISTS idx_usage_log_used_at ON usage_log(used_at);
|
|
2230
2230
|
`
|
|
2231
2231
|
},
|
|
2232
|
+
{
|
|
2233
|
+
name: "008_schedules",
|
|
2234
|
+
sql: `
|
|
2235
|
+
CREATE TABLE IF NOT EXISTS prompt_schedules (
|
|
2236
|
+
id TEXT PRIMARY KEY,
|
|
2237
|
+
prompt_id TEXT NOT NULL REFERENCES prompts(id) ON DELETE CASCADE,
|
|
2238
|
+
prompt_slug TEXT NOT NULL,
|
|
2239
|
+
cron TEXT NOT NULL,
|
|
2240
|
+
vars TEXT NOT NULL DEFAULT '{}',
|
|
2241
|
+
agent_id TEXT,
|
|
2242
|
+
last_run_at TEXT,
|
|
2243
|
+
next_run_at TEXT NOT NULL,
|
|
2244
|
+
run_count INTEGER NOT NULL DEFAULT 0,
|
|
2245
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
2246
|
+
);
|
|
2247
|
+
CREATE INDEX IF NOT EXISTS idx_prompt_schedules_next_run ON prompt_schedules(next_run_at);
|
|
2248
|
+
CREATE INDEX IF NOT EXISTS idx_prompt_schedules_prompt_id ON prompt_schedules(prompt_id);
|
|
2249
|
+
`
|
|
2250
|
+
},
|
|
2232
2251
|
{
|
|
2233
2252
|
name: "002_fts5",
|
|
2234
2253
|
sql: `
|
|
@@ -3167,6 +3186,152 @@ function lintAll(prompts) {
|
|
|
3167
3186
|
return prompts.map((p) => ({ prompt: p, issues: lintPrompt(p) })).filter((r) => r.issues.length > 0);
|
|
3168
3187
|
}
|
|
3169
3188
|
|
|
3189
|
+
// src/db/schedules.ts
|
|
3190
|
+
init_database();
|
|
3191
|
+
|
|
3192
|
+
// src/lib/cron.ts
|
|
3193
|
+
function parseField(field, min, max) {
|
|
3194
|
+
if (field === "*") {
|
|
3195
|
+
return range(min, max);
|
|
3196
|
+
}
|
|
3197
|
+
const result = [];
|
|
3198
|
+
for (const part of field.split(",")) {
|
|
3199
|
+
if (part.includes("/")) {
|
|
3200
|
+
const [rangeOrStar, stepStr] = part.split("/");
|
|
3201
|
+
const step = parseInt(stepStr ?? "1", 10);
|
|
3202
|
+
const start = rangeOrStar === "*" ? min : parseInt(rangeOrStar ?? String(min), 10);
|
|
3203
|
+
for (let v = start;v <= max; v += step)
|
|
3204
|
+
result.push(v);
|
|
3205
|
+
} else if (part.includes("-")) {
|
|
3206
|
+
const [fromStr, toStr] = part.split("-");
|
|
3207
|
+
const from = parseInt(fromStr ?? String(min), 10);
|
|
3208
|
+
const to = parseInt(toStr ?? String(max), 10);
|
|
3209
|
+
for (let v = from;v <= to; v++)
|
|
3210
|
+
result.push(v);
|
|
3211
|
+
} else {
|
|
3212
|
+
result.push(parseInt(part, 10));
|
|
3213
|
+
}
|
|
3214
|
+
}
|
|
3215
|
+
return [...new Set(result)].sort((a, b) => a - b).filter((v) => v >= min && v <= max);
|
|
3216
|
+
}
|
|
3217
|
+
function range(min, max) {
|
|
3218
|
+
const r = [];
|
|
3219
|
+
for (let i = min;i <= max; i++)
|
|
3220
|
+
r.push(i);
|
|
3221
|
+
return r;
|
|
3222
|
+
}
|
|
3223
|
+
function getNextRunTime(cronExpr, from = new Date) {
|
|
3224
|
+
const parts = cronExpr.trim().split(/\s+/);
|
|
3225
|
+
if (parts.length !== 5)
|
|
3226
|
+
throw new Error(`Invalid cron expression (need 5 fields): ${cronExpr}`);
|
|
3227
|
+
const [minField, hourField, domField, monField, dowField] = parts;
|
|
3228
|
+
const minutes = parseField(minField, 0, 59);
|
|
3229
|
+
const hours = parseField(hourField, 0, 23);
|
|
3230
|
+
const doms = parseField(domField, 1, 31);
|
|
3231
|
+
const months = parseField(monField, 1, 12);
|
|
3232
|
+
const dows = parseField(dowField, 0, 6);
|
|
3233
|
+
const next = new Date(from);
|
|
3234
|
+
next.setSeconds(0, 0);
|
|
3235
|
+
next.setMinutes(next.getMinutes() + 1);
|
|
3236
|
+
const limit = new Date(from);
|
|
3237
|
+
limit.setFullYear(limit.getFullYear() + 4);
|
|
3238
|
+
while (next <= limit) {
|
|
3239
|
+
const mon = next.getMonth() + 1;
|
|
3240
|
+
if (!months.includes(mon)) {
|
|
3241
|
+
next.setDate(1);
|
|
3242
|
+
next.setHours(0, 0, 0, 0);
|
|
3243
|
+
next.setMonth(next.getMonth() + 1);
|
|
3244
|
+
continue;
|
|
3245
|
+
}
|
|
3246
|
+
const dom = next.getDate();
|
|
3247
|
+
const dow = next.getDay();
|
|
3248
|
+
const domMatch = domField === "*" || doms.includes(dom);
|
|
3249
|
+
const dowMatch = dowField === "*" || dows.includes(dow);
|
|
3250
|
+
const dayMatch = domField === "*" && dowField === "*" ? true : domField !== "*" && dowField !== "*" ? domMatch || dowMatch : domField !== "*" ? domMatch : dowMatch;
|
|
3251
|
+
if (!dayMatch) {
|
|
3252
|
+
next.setDate(next.getDate() + 1);
|
|
3253
|
+
next.setHours(0, 0, 0, 0);
|
|
3254
|
+
continue;
|
|
3255
|
+
}
|
|
3256
|
+
const h = next.getHours();
|
|
3257
|
+
const validHour = hours.find((v) => v >= h);
|
|
3258
|
+
if (validHour === undefined) {
|
|
3259
|
+
next.setDate(next.getDate() + 1);
|
|
3260
|
+
next.setHours(0, 0, 0, 0);
|
|
3261
|
+
continue;
|
|
3262
|
+
}
|
|
3263
|
+
if (validHour > h) {
|
|
3264
|
+
next.setHours(validHour, 0, 0, 0);
|
|
3265
|
+
}
|
|
3266
|
+
const m = next.getMinutes();
|
|
3267
|
+
const validMin = minutes.find((v) => v >= m);
|
|
3268
|
+
if (validMin === undefined) {
|
|
3269
|
+
next.setHours(next.getHours() + 1, 0, 0, 0);
|
|
3270
|
+
continue;
|
|
3271
|
+
}
|
|
3272
|
+
next.setMinutes(validMin);
|
|
3273
|
+
return next;
|
|
3274
|
+
}
|
|
3275
|
+
throw new Error(`Could not find next run time for cron: ${cronExpr}`);
|
|
3276
|
+
}
|
|
3277
|
+
function validateCron(expr) {
|
|
3278
|
+
try {
|
|
3279
|
+
getNextRunTime(expr, new Date);
|
|
3280
|
+
return null;
|
|
3281
|
+
} catch (e) {
|
|
3282
|
+
return e instanceof Error ? e.message : String(e);
|
|
3283
|
+
}
|
|
3284
|
+
}
|
|
3285
|
+
|
|
3286
|
+
// src/db/schedules.ts
|
|
3287
|
+
function rowToSchedule(row) {
|
|
3288
|
+
return {
|
|
3289
|
+
...row,
|
|
3290
|
+
vars: JSON.parse(row.vars)
|
|
3291
|
+
};
|
|
3292
|
+
}
|
|
3293
|
+
function createSchedule(input) {
|
|
3294
|
+
const db = getDatabase();
|
|
3295
|
+
const id = `SCH-${Math.random().toString(36).slice(2, 8).toUpperCase()}`;
|
|
3296
|
+
const next_run_at = getNextRunTime(input.cron).toISOString();
|
|
3297
|
+
const vars = JSON.stringify(input.vars ?? {});
|
|
3298
|
+
db.run(`INSERT INTO prompt_schedules (id, prompt_id, prompt_slug, cron, vars, agent_id, next_run_at)
|
|
3299
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`, [id, input.prompt_id, input.prompt_slug, input.cron, vars, input.agent_id ?? null, next_run_at]);
|
|
3300
|
+
const row = db.query("SELECT * FROM prompt_schedules WHERE id = ?").get(id);
|
|
3301
|
+
return rowToSchedule(row);
|
|
3302
|
+
}
|
|
3303
|
+
function listSchedules(promptId) {
|
|
3304
|
+
const db = getDatabase();
|
|
3305
|
+
const rows = promptId ? db.query("SELECT * FROM prompt_schedules WHERE prompt_id = ? ORDER BY next_run_at").all(promptId) : db.query("SELECT * FROM prompt_schedules ORDER BY next_run_at").all();
|
|
3306
|
+
return rows.map(rowToSchedule);
|
|
3307
|
+
}
|
|
3308
|
+
function deleteSchedule(id) {
|
|
3309
|
+
const db = getDatabase();
|
|
3310
|
+
db.run("DELETE FROM prompt_schedules WHERE id = ?", [id]);
|
|
3311
|
+
}
|
|
3312
|
+
function getDueSchedules() {
|
|
3313
|
+
const db = getDatabase();
|
|
3314
|
+
const now = new Date().toISOString();
|
|
3315
|
+
const rows = db.query(`SELECT ps.*, p.body as prompt_body
|
|
3316
|
+
FROM prompt_schedules ps
|
|
3317
|
+
JOIN prompts p ON p.id = ps.prompt_id
|
|
3318
|
+
WHERE ps.next_run_at <= ?
|
|
3319
|
+
ORDER BY ps.next_run_at`).all(now);
|
|
3320
|
+
const due = [];
|
|
3321
|
+
for (const row of rows) {
|
|
3322
|
+
const schedule = rowToSchedule(row);
|
|
3323
|
+
let rendered = row.prompt_body;
|
|
3324
|
+
for (const [k, v] of Object.entries(schedule.vars)) {
|
|
3325
|
+
rendered = rendered.replace(new RegExp(`\\{\\{\\s*${k}[^}]*\\}\\}`, "g"), v);
|
|
3326
|
+
}
|
|
3327
|
+
rendered = rendered.replace(/\{\{([^|}]+)\|([^}]*)\}\}/g, (_, _name, def) => def);
|
|
3328
|
+
const newNext = getNextRunTime(schedule.cron).toISOString();
|
|
3329
|
+
db.run(`UPDATE prompt_schedules SET last_run_at = ?, next_run_at = ?, run_count = run_count + 1 WHERE id = ?`, [now, newNext, schedule.id]);
|
|
3330
|
+
due.push({ ...schedule, rendered, prompt_body: row.prompt_body });
|
|
3331
|
+
}
|
|
3332
|
+
return due;
|
|
3333
|
+
}
|
|
3334
|
+
|
|
3170
3335
|
// src/lib/audit.ts
|
|
3171
3336
|
init_database();
|
|
3172
3337
|
function runAudit() {
|
|
@@ -4332,4 +4497,131 @@ ${chalk.green(`Created: ${imported.created}`)} ${chalk.yellow(`Updated: ${impor
|
|
|
4332
4497
|
handleError(e);
|
|
4333
4498
|
}
|
|
4334
4499
|
});
|
|
4500
|
+
program2.command("remove <id>").alias("rm").alias("uninstall").description("Remove a prompt (alias for delete)").option("-y, --yes", "Skip confirmation").action(async (id, opts) => {
|
|
4501
|
+
try {
|
|
4502
|
+
const prompt = getPrompt(id);
|
|
4503
|
+
if (!prompt)
|
|
4504
|
+
handleError(`Prompt not found: ${id}`);
|
|
4505
|
+
if (!opts.yes && !isJson()) {
|
|
4506
|
+
const { createInterface } = await import("readline");
|
|
4507
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
4508
|
+
await new Promise((resolve) => {
|
|
4509
|
+
rl.question(chalk.yellow(`Remove "${prompt.slug}"? [y/N] `), (ans) => {
|
|
4510
|
+
rl.close();
|
|
4511
|
+
if (ans.toLowerCase() !== "y") {
|
|
4512
|
+
console.log("Cancelled.");
|
|
4513
|
+
process.exit(0);
|
|
4514
|
+
}
|
|
4515
|
+
resolve();
|
|
4516
|
+
});
|
|
4517
|
+
});
|
|
4518
|
+
}
|
|
4519
|
+
deletePrompt(id);
|
|
4520
|
+
if (isJson())
|
|
4521
|
+
output({ deleted: true, id: prompt.id });
|
|
4522
|
+
else
|
|
4523
|
+
console.log(chalk.red(`Removed ${prompt.slug}`));
|
|
4524
|
+
} catch (e) {
|
|
4525
|
+
handleError(e);
|
|
4526
|
+
}
|
|
4527
|
+
});
|
|
4528
|
+
var scheduleCmd = program2.command("schedule").description("Manage prompt schedules");
|
|
4529
|
+
scheduleCmd.command("add <id> <cron>").description("Schedule a prompt to run on a cron (5-field: min hour dom mon dow)").option("--vars <json>", `JSON object of template variables, e.g. '{"name":"Alice"}'`).option("--agent <id>", "Agent ID to associate").action((id, cron, opts) => {
|
|
4530
|
+
try {
|
|
4531
|
+
const cronError = validateCron(cron);
|
|
4532
|
+
if (cronError)
|
|
4533
|
+
handleError(`Invalid cron: ${cronError}`);
|
|
4534
|
+
const prompt = getPrompt(id);
|
|
4535
|
+
if (!prompt)
|
|
4536
|
+
handleError(`Prompt not found: ${id}`);
|
|
4537
|
+
const vars = opts.vars ? JSON.parse(opts.vars) : undefined;
|
|
4538
|
+
const schedule = createSchedule({ prompt_id: prompt.id, prompt_slug: prompt.slug, cron, vars, agent_id: opts.agent });
|
|
4539
|
+
if (isJson()) {
|
|
4540
|
+
output(schedule);
|
|
4541
|
+
return;
|
|
4542
|
+
}
|
|
4543
|
+
console.log(chalk.green(`Scheduled "${prompt.slug}" [${schedule.id}]`));
|
|
4544
|
+
console.log(` Cron: ${cron}`);
|
|
4545
|
+
console.log(` Next run: ${schedule.next_run_at}`);
|
|
4546
|
+
} catch (e) {
|
|
4547
|
+
handleError(e);
|
|
4548
|
+
}
|
|
4549
|
+
});
|
|
4550
|
+
scheduleCmd.command("list [id]").description("List schedules, optionally filtered by prompt ID").action((id) => {
|
|
4551
|
+
try {
|
|
4552
|
+
const schedules = listSchedules(id);
|
|
4553
|
+
if (isJson()) {
|
|
4554
|
+
output(schedules);
|
|
4555
|
+
return;
|
|
4556
|
+
}
|
|
4557
|
+
if (!schedules.length) {
|
|
4558
|
+
console.log(chalk.gray("No schedules."));
|
|
4559
|
+
return;
|
|
4560
|
+
}
|
|
4561
|
+
for (const s of schedules) {
|
|
4562
|
+
console.log(`${chalk.bold(s.id)} ${chalk.cyan(s.prompt_slug)} cron:${s.cron} next:${s.next_run_at} runs:${s.run_count}`);
|
|
4563
|
+
}
|
|
4564
|
+
} catch (e) {
|
|
4565
|
+
handleError(e);
|
|
4566
|
+
}
|
|
4567
|
+
});
|
|
4568
|
+
scheduleCmd.command("remove <scheduleId>").alias("delete").description("Remove a prompt schedule").action((scheduleId) => {
|
|
4569
|
+
try {
|
|
4570
|
+
deleteSchedule(scheduleId);
|
|
4571
|
+
if (isJson())
|
|
4572
|
+
output({ deleted: true, id: scheduleId });
|
|
4573
|
+
else
|
|
4574
|
+
console.log(chalk.red(`Removed schedule ${scheduleId}`));
|
|
4575
|
+
} catch (e) {
|
|
4576
|
+
handleError(e);
|
|
4577
|
+
}
|
|
4578
|
+
});
|
|
4579
|
+
scheduleCmd.command("due").description("Show and execute all due schedules").option("--dry-run", "Show due prompts without marking them as ran").action((opts) => {
|
|
4580
|
+
try {
|
|
4581
|
+
const due = getDueSchedules();
|
|
4582
|
+
if (!due.length) {
|
|
4583
|
+
console.log(chalk.gray("No prompts due."));
|
|
4584
|
+
return;
|
|
4585
|
+
}
|
|
4586
|
+
if (isJson()) {
|
|
4587
|
+
output(due);
|
|
4588
|
+
return;
|
|
4589
|
+
}
|
|
4590
|
+
for (const d of due) {
|
|
4591
|
+
console.log(chalk.bold(`
|
|
4592
|
+
[${d.id}] ${d.prompt_slug}`));
|
|
4593
|
+
console.log(chalk.gray(`Next run: ${d.next_run_at} | Runs: ${d.run_count}`));
|
|
4594
|
+
console.log(chalk.white(d.rendered));
|
|
4595
|
+
}
|
|
4596
|
+
if (!opts.dryRun)
|
|
4597
|
+
console.log(chalk.green(`
|
|
4598
|
+
\u2713 Marked ${due.length} schedule(s) as ran.`));
|
|
4599
|
+
} catch (e) {
|
|
4600
|
+
handleError(e);
|
|
4601
|
+
}
|
|
4602
|
+
});
|
|
4603
|
+
scheduleCmd.command("next <cron>").description("Preview when a cron expression will fire").option("-n, --count <n>", "Number of runs to show", "5").action((cron, opts) => {
|
|
4604
|
+
try {
|
|
4605
|
+
const cronError = validateCron(cron);
|
|
4606
|
+
if (cronError)
|
|
4607
|
+
handleError(`Invalid cron: ${cronError}`);
|
|
4608
|
+
const count = parseInt(opts.count ?? "5", 10);
|
|
4609
|
+
const runs = [];
|
|
4610
|
+
let from = new Date;
|
|
4611
|
+
for (let i = 0;i < count; i++) {
|
|
4612
|
+
const next = getNextRunTime(cron, from);
|
|
4613
|
+
runs.push(next.toISOString());
|
|
4614
|
+
from = next;
|
|
4615
|
+
}
|
|
4616
|
+
if (isJson()) {
|
|
4617
|
+
output({ cron, next_runs: runs });
|
|
4618
|
+
return;
|
|
4619
|
+
}
|
|
4620
|
+
console.log(chalk.bold(`Next ${count} runs for "${cron}":`));
|
|
4621
|
+
for (const r of runs)
|
|
4622
|
+
console.log(` ${r}`);
|
|
4623
|
+
} catch (e) {
|
|
4624
|
+
handleError(e);
|
|
4625
|
+
}
|
|
4626
|
+
});
|
|
4335
4627
|
program2.parse();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"database.d.ts","sourceRoot":"","sources":["../../src/db/database.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAMrC,wBAAgB,SAAS,IAAI,MAAM,CAsBlC;AAED,wBAAgB,WAAW,IAAI,QAAQ,CAmBtC;AAED,wBAAgB,aAAa,IAAI,IAAI,CAKpC;AAGD,wBAAgB,aAAa,IAAI,IAAI,CAEpC;
|
|
1
|
+
{"version":3,"file":"database.d.ts","sourceRoot":"","sources":["../../src/db/database.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAMrC,wBAAgB,SAAS,IAAI,MAAM,CAsBlC;AAED,wBAAgB,WAAW,IAAI,QAAQ,CAmBtC;AAED,wBAAgB,aAAa,IAAI,IAAI,CAKpC;AAGD,wBAAgB,aAAa,IAAI,IAAI,CAEpC;AA4KD,wBAAgB,cAAc,CAAC,EAAE,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CA0B5E;AAED,wBAAgB,MAAM,CAAC,EAAE,EAAE,QAAQ,GAAG,OAAO,CAM5C;AAED,wBAAgB,aAAa,CAAC,EAAE,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAkC3E"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export interface PromptSchedule {
|
|
2
|
+
id: string;
|
|
3
|
+
prompt_id: string;
|
|
4
|
+
prompt_slug: string;
|
|
5
|
+
cron: string;
|
|
6
|
+
vars: Record<string, string>;
|
|
7
|
+
agent_id: string | null;
|
|
8
|
+
last_run_at: string | null;
|
|
9
|
+
next_run_at: string;
|
|
10
|
+
run_count: number;
|
|
11
|
+
created_at: string;
|
|
12
|
+
}
|
|
13
|
+
export declare function createSchedule(input: {
|
|
14
|
+
prompt_id: string;
|
|
15
|
+
prompt_slug: string;
|
|
16
|
+
cron: string;
|
|
17
|
+
vars?: Record<string, string>;
|
|
18
|
+
agent_id?: string;
|
|
19
|
+
}): PromptSchedule;
|
|
20
|
+
export declare function listSchedules(promptId?: string): PromptSchedule[];
|
|
21
|
+
export declare function getSchedule(id: string): PromptSchedule | null;
|
|
22
|
+
export declare function deleteSchedule(id: string): void;
|
|
23
|
+
export interface DueSchedule extends PromptSchedule {
|
|
24
|
+
rendered: string;
|
|
25
|
+
prompt_body: string;
|
|
26
|
+
}
|
|
27
|
+
export declare function getDueSchedules(): DueSchedule[];
|
|
28
|
+
export declare function markScheduleRan(id: string): PromptSchedule | null;
|
|
29
|
+
//# sourceMappingURL=schedules.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schedules.d.ts","sourceRoot":"","sources":["../../src/db/schedules.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,cAAc;IAC7B,EAAE,EAAE,MAAM,CAAA;IACV,SAAS,EAAE,MAAM,CAAA;IACjB,WAAW,EAAE,MAAM,CAAA;IACnB,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC5B,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAA;IACvB,WAAW,EAAE,MAAM,GAAG,IAAI,CAAA;IAC1B,WAAW,EAAE,MAAM,CAAA;IACnB,SAAS,EAAE,MAAM,CAAA;IACjB,UAAU,EAAE,MAAM,CAAA;CACnB;AAsBD,wBAAgB,cAAc,CAAC,KAAK,EAAE;IACpC,SAAS,EAAE,MAAM,CAAA;IACjB,WAAW,EAAE,MAAM,CAAA;IACnB,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC7B,QAAQ,CAAC,EAAE,MAAM,CAAA;CAClB,GAAG,cAAc,CAgBjB;AAED,wBAAgB,aAAa,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,cAAc,EAAE,CAMjE;AAED,wBAAgB,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,cAAc,GAAG,IAAI,CAI7D;AAED,wBAAgB,cAAc,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,CAG/C;AAED,MAAM,WAAW,WAAY,SAAQ,cAAc;IACjD,QAAQ,EAAE,MAAM,CAAA;IAChB,WAAW,EAAE,MAAM,CAAA;CACpB;AAED,wBAAgB,eAAe,IAAI,WAAW,EAAE,CAoC/C;AAED,wBAAgB,eAAe,CAAC,EAAE,EAAE,MAAM,GAAG,cAAc,GAAG,IAAI,CAWjE"}
|
package/dist/index.js
CHANGED
|
@@ -148,6 +148,25 @@ function runMigrations(db) {
|
|
|
148
148
|
CREATE INDEX IF NOT EXISTS idx_usage_log_used_at ON usage_log(used_at);
|
|
149
149
|
`
|
|
150
150
|
},
|
|
151
|
+
{
|
|
152
|
+
name: "008_schedules",
|
|
153
|
+
sql: `
|
|
154
|
+
CREATE TABLE IF NOT EXISTS prompt_schedules (
|
|
155
|
+
id TEXT PRIMARY KEY,
|
|
156
|
+
prompt_id TEXT NOT NULL REFERENCES prompts(id) ON DELETE CASCADE,
|
|
157
|
+
prompt_slug TEXT NOT NULL,
|
|
158
|
+
cron TEXT NOT NULL,
|
|
159
|
+
vars TEXT NOT NULL DEFAULT '{}',
|
|
160
|
+
agent_id TEXT,
|
|
161
|
+
last_run_at TEXT,
|
|
162
|
+
next_run_at TEXT NOT NULL,
|
|
163
|
+
run_count INTEGER NOT NULL DEFAULT 0,
|
|
164
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
165
|
+
);
|
|
166
|
+
CREATE INDEX IF NOT EXISTS idx_prompt_schedules_next_run ON prompt_schedules(next_run_at);
|
|
167
|
+
CREATE INDEX IF NOT EXISTS idx_prompt_schedules_prompt_id ON prompt_schedules(prompt_id);
|
|
168
|
+
`
|
|
169
|
+
},
|
|
151
170
|
{
|
|
152
171
|
name: "002_fts5",
|
|
153
172
|
sql: `
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cron.d.ts","sourceRoot":"","sources":["../../src/lib/cron.ts"],"names":[],"mappings":"AAiCA,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,GAAE,IAAiB,GAAG,IAAI,CA4E9E;AAED,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAOxD"}
|
package/dist/mcp/index.js
CHANGED
|
@@ -4142,6 +4142,25 @@ function runMigrations(db) {
|
|
|
4142
4142
|
CREATE INDEX IF NOT EXISTS idx_usage_log_used_at ON usage_log(used_at);
|
|
4143
4143
|
`
|
|
4144
4144
|
},
|
|
4145
|
+
{
|
|
4146
|
+
name: "008_schedules",
|
|
4147
|
+
sql: `
|
|
4148
|
+
CREATE TABLE IF NOT EXISTS prompt_schedules (
|
|
4149
|
+
id TEXT PRIMARY KEY,
|
|
4150
|
+
prompt_id TEXT NOT NULL REFERENCES prompts(id) ON DELETE CASCADE,
|
|
4151
|
+
prompt_slug TEXT NOT NULL,
|
|
4152
|
+
cron TEXT NOT NULL,
|
|
4153
|
+
vars TEXT NOT NULL DEFAULT '{}',
|
|
4154
|
+
agent_id TEXT,
|
|
4155
|
+
last_run_at TEXT,
|
|
4156
|
+
next_run_at TEXT NOT NULL,
|
|
4157
|
+
run_count INTEGER NOT NULL DEFAULT 0,
|
|
4158
|
+
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
4159
|
+
);
|
|
4160
|
+
CREATE INDEX IF NOT EXISTS idx_prompt_schedules_next_run ON prompt_schedules(next_run_at);
|
|
4161
|
+
CREATE INDEX IF NOT EXISTS idx_prompt_schedules_prompt_id ON prompt_schedules(prompt_id);
|
|
4162
|
+
`
|
|
4163
|
+
},
|
|
4145
4164
|
{
|
|
4146
4165
|
name: "002_fts5",
|
|
4147
4166
|
sql: `
|
|
@@ -5137,6 +5156,154 @@ async function maybeSaveMemento(opts) {
|
|
|
5137
5156
|
} catch {}
|
|
5138
5157
|
}
|
|
5139
5158
|
|
|
5159
|
+
// src/lib/cron.ts
|
|
5160
|
+
function parseField(field, min, max) {
|
|
5161
|
+
if (field === "*") {
|
|
5162
|
+
return range(min, max);
|
|
5163
|
+
}
|
|
5164
|
+
const result = [];
|
|
5165
|
+
for (const part of field.split(",")) {
|
|
5166
|
+
if (part.includes("/")) {
|
|
5167
|
+
const [rangeOrStar, stepStr] = part.split("/");
|
|
5168
|
+
const step = parseInt(stepStr ?? "1", 10);
|
|
5169
|
+
const start = rangeOrStar === "*" ? min : parseInt(rangeOrStar ?? String(min), 10);
|
|
5170
|
+
for (let v = start;v <= max; v += step)
|
|
5171
|
+
result.push(v);
|
|
5172
|
+
} else if (part.includes("-")) {
|
|
5173
|
+
const [fromStr, toStr] = part.split("-");
|
|
5174
|
+
const from = parseInt(fromStr ?? String(min), 10);
|
|
5175
|
+
const to = parseInt(toStr ?? String(max), 10);
|
|
5176
|
+
for (let v = from;v <= to; v++)
|
|
5177
|
+
result.push(v);
|
|
5178
|
+
} else {
|
|
5179
|
+
result.push(parseInt(part, 10));
|
|
5180
|
+
}
|
|
5181
|
+
}
|
|
5182
|
+
return [...new Set(result)].sort((a, b) => a - b).filter((v) => v >= min && v <= max);
|
|
5183
|
+
}
|
|
5184
|
+
function range(min, max) {
|
|
5185
|
+
const r = [];
|
|
5186
|
+
for (let i = min;i <= max; i++)
|
|
5187
|
+
r.push(i);
|
|
5188
|
+
return r;
|
|
5189
|
+
}
|
|
5190
|
+
function getNextRunTime(cronExpr, from = new Date) {
|
|
5191
|
+
const parts = cronExpr.trim().split(/\s+/);
|
|
5192
|
+
if (parts.length !== 5)
|
|
5193
|
+
throw new Error(`Invalid cron expression (need 5 fields): ${cronExpr}`);
|
|
5194
|
+
const [minField, hourField, domField, monField, dowField] = parts;
|
|
5195
|
+
const minutes = parseField(minField, 0, 59);
|
|
5196
|
+
const hours = parseField(hourField, 0, 23);
|
|
5197
|
+
const doms = parseField(domField, 1, 31);
|
|
5198
|
+
const months = parseField(monField, 1, 12);
|
|
5199
|
+
const dows = parseField(dowField, 0, 6);
|
|
5200
|
+
const next = new Date(from);
|
|
5201
|
+
next.setSeconds(0, 0);
|
|
5202
|
+
next.setMinutes(next.getMinutes() + 1);
|
|
5203
|
+
const limit = new Date(from);
|
|
5204
|
+
limit.setFullYear(limit.getFullYear() + 4);
|
|
5205
|
+
while (next <= limit) {
|
|
5206
|
+
const mon = next.getMonth() + 1;
|
|
5207
|
+
if (!months.includes(mon)) {
|
|
5208
|
+
next.setDate(1);
|
|
5209
|
+
next.setHours(0, 0, 0, 0);
|
|
5210
|
+
next.setMonth(next.getMonth() + 1);
|
|
5211
|
+
continue;
|
|
5212
|
+
}
|
|
5213
|
+
const dom = next.getDate();
|
|
5214
|
+
const dow = next.getDay();
|
|
5215
|
+
const domMatch = domField === "*" || doms.includes(dom);
|
|
5216
|
+
const dowMatch = dowField === "*" || dows.includes(dow);
|
|
5217
|
+
const dayMatch = domField === "*" && dowField === "*" ? true : domField !== "*" && dowField !== "*" ? domMatch || dowMatch : domField !== "*" ? domMatch : dowMatch;
|
|
5218
|
+
if (!dayMatch) {
|
|
5219
|
+
next.setDate(next.getDate() + 1);
|
|
5220
|
+
next.setHours(0, 0, 0, 0);
|
|
5221
|
+
continue;
|
|
5222
|
+
}
|
|
5223
|
+
const h = next.getHours();
|
|
5224
|
+
const validHour = hours.find((v) => v >= h);
|
|
5225
|
+
if (validHour === undefined) {
|
|
5226
|
+
next.setDate(next.getDate() + 1);
|
|
5227
|
+
next.setHours(0, 0, 0, 0);
|
|
5228
|
+
continue;
|
|
5229
|
+
}
|
|
5230
|
+
if (validHour > h) {
|
|
5231
|
+
next.setHours(validHour, 0, 0, 0);
|
|
5232
|
+
}
|
|
5233
|
+
const m = next.getMinutes();
|
|
5234
|
+
const validMin = minutes.find((v) => v >= m);
|
|
5235
|
+
if (validMin === undefined) {
|
|
5236
|
+
next.setHours(next.getHours() + 1, 0, 0, 0);
|
|
5237
|
+
continue;
|
|
5238
|
+
}
|
|
5239
|
+
next.setMinutes(validMin);
|
|
5240
|
+
return next;
|
|
5241
|
+
}
|
|
5242
|
+
throw new Error(`Could not find next run time for cron: ${cronExpr}`);
|
|
5243
|
+
}
|
|
5244
|
+
function validateCron(expr) {
|
|
5245
|
+
try {
|
|
5246
|
+
getNextRunTime(expr, new Date);
|
|
5247
|
+
return null;
|
|
5248
|
+
} catch (e) {
|
|
5249
|
+
return e instanceof Error ? e.message : String(e);
|
|
5250
|
+
}
|
|
5251
|
+
}
|
|
5252
|
+
|
|
5253
|
+
// src/db/schedules.ts
|
|
5254
|
+
function rowToSchedule(row) {
|
|
5255
|
+
return {
|
|
5256
|
+
...row,
|
|
5257
|
+
vars: JSON.parse(row.vars)
|
|
5258
|
+
};
|
|
5259
|
+
}
|
|
5260
|
+
function createSchedule(input) {
|
|
5261
|
+
const db = getDatabase();
|
|
5262
|
+
const id = `SCH-${Math.random().toString(36).slice(2, 8).toUpperCase()}`;
|
|
5263
|
+
const next_run_at = getNextRunTime(input.cron).toISOString();
|
|
5264
|
+
const vars = JSON.stringify(input.vars ?? {});
|
|
5265
|
+
db.run(`INSERT INTO prompt_schedules (id, prompt_id, prompt_slug, cron, vars, agent_id, next_run_at)
|
|
5266
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)`, [id, input.prompt_id, input.prompt_slug, input.cron, vars, input.agent_id ?? null, next_run_at]);
|
|
5267
|
+
const row = db.query("SELECT * FROM prompt_schedules WHERE id = ?").get(id);
|
|
5268
|
+
return rowToSchedule(row);
|
|
5269
|
+
}
|
|
5270
|
+
function listSchedules(promptId) {
|
|
5271
|
+
const db = getDatabase();
|
|
5272
|
+
const rows = promptId ? db.query("SELECT * FROM prompt_schedules WHERE prompt_id = ? ORDER BY next_run_at").all(promptId) : db.query("SELECT * FROM prompt_schedules ORDER BY next_run_at").all();
|
|
5273
|
+
return rows.map(rowToSchedule);
|
|
5274
|
+
}
|
|
5275
|
+
function getSchedule(id) {
|
|
5276
|
+
const db = getDatabase();
|
|
5277
|
+
const row = db.query("SELECT * FROM prompt_schedules WHERE id = ?").get(id);
|
|
5278
|
+
return row ? rowToSchedule(row) : null;
|
|
5279
|
+
}
|
|
5280
|
+
function deleteSchedule(id) {
|
|
5281
|
+
const db = getDatabase();
|
|
5282
|
+
db.run("DELETE FROM prompt_schedules WHERE id = ?", [id]);
|
|
5283
|
+
}
|
|
5284
|
+
function getDueSchedules() {
|
|
5285
|
+
const db = getDatabase();
|
|
5286
|
+
const now = new Date().toISOString();
|
|
5287
|
+
const rows = db.query(`SELECT ps.*, p.body as prompt_body
|
|
5288
|
+
FROM prompt_schedules ps
|
|
5289
|
+
JOIN prompts p ON p.id = ps.prompt_id
|
|
5290
|
+
WHERE ps.next_run_at <= ?
|
|
5291
|
+
ORDER BY ps.next_run_at`).all(now);
|
|
5292
|
+
const due = [];
|
|
5293
|
+
for (const row of rows) {
|
|
5294
|
+
const schedule = rowToSchedule(row);
|
|
5295
|
+
let rendered = row.prompt_body;
|
|
5296
|
+
for (const [k, v] of Object.entries(schedule.vars)) {
|
|
5297
|
+
rendered = rendered.replace(new RegExp(`\\{\\{\\s*${k}[^}]*\\}\\}`, "g"), v);
|
|
5298
|
+
}
|
|
5299
|
+
rendered = rendered.replace(/\{\{([^|}]+)\|([^}]*)\}\}/g, (_, _name, def) => def);
|
|
5300
|
+
const newNext = getNextRunTime(schedule.cron).toISOString();
|
|
5301
|
+
db.run(`UPDATE prompt_schedules SET last_run_at = ?, next_run_at = ?, run_count = run_count + 1 WHERE id = ?`, [now, newNext, schedule.id]);
|
|
5302
|
+
due.push({ ...schedule, rendered, prompt_body: row.prompt_body });
|
|
5303
|
+
}
|
|
5304
|
+
return due;
|
|
5305
|
+
}
|
|
5306
|
+
|
|
5140
5307
|
// src/lib/diff.ts
|
|
5141
5308
|
function diffTexts(a, b) {
|
|
5142
5309
|
const aLines = a.split(`
|
|
@@ -5923,6 +6090,118 @@ server.registerTool("prompts_project_delete", {
|
|
|
5923
6090
|
return err(e instanceof Error ? e.message : String(e));
|
|
5924
6091
|
}
|
|
5925
6092
|
});
|
|
6093
|
+
server.registerTool("prompts_schedule", {
|
|
6094
|
+
description: "Schedule a prompt to run on a cron. Stores the schedule in the DB. Call prompts_get_due periodically (e.g. via /loop) to retrieve and execute due prompts.",
|
|
6095
|
+
inputSchema: {
|
|
6096
|
+
id: exports_external.string().describe("Prompt ID or slug"),
|
|
6097
|
+
cron: exports_external.string().describe("Cron expression (5 fields: min hour dom mon dow). Example: '*/5 * * * *' for every 5 minutes"),
|
|
6098
|
+
vars: exports_external.record(exports_external.string()).optional().describe("Template variable overrides (key\u2192value)"),
|
|
6099
|
+
agent_id: exports_external.string().optional().describe("Agent ID to associate with this schedule")
|
|
6100
|
+
}
|
|
6101
|
+
}, async ({ id, cron, vars, agent_id }) => {
|
|
6102
|
+
try {
|
|
6103
|
+
const cronError = validateCron(cron);
|
|
6104
|
+
if (cronError)
|
|
6105
|
+
return err(`Invalid cron expression: ${cronError}`);
|
|
6106
|
+
const prompt = getPrompt(id);
|
|
6107
|
+
if (!prompt)
|
|
6108
|
+
return err(`Prompt not found: ${id}`);
|
|
6109
|
+
const schedule = createSchedule({
|
|
6110
|
+
prompt_id: prompt.id,
|
|
6111
|
+
prompt_slug: prompt.slug,
|
|
6112
|
+
cron,
|
|
6113
|
+
vars,
|
|
6114
|
+
agent_id
|
|
6115
|
+
});
|
|
6116
|
+
return ok({
|
|
6117
|
+
schedule,
|
|
6118
|
+
message: `Prompt "${prompt.title}" scheduled with cron "${cron}". Next run: ${schedule.next_run_at}. Call prompts_get_due to execute due prompts.`
|
|
6119
|
+
});
|
|
6120
|
+
} catch (e) {
|
|
6121
|
+
return err(e instanceof Error ? e.message : String(e));
|
|
6122
|
+
}
|
|
6123
|
+
});
|
|
6124
|
+
server.registerTool("prompts_list_schedules", {
|
|
6125
|
+
description: "List all prompt schedules, optionally filtered by prompt.",
|
|
6126
|
+
inputSchema: {
|
|
6127
|
+
prompt_id: exports_external.string().optional().describe("Filter by prompt ID or slug")
|
|
6128
|
+
}
|
|
6129
|
+
}, async ({ prompt_id }) => {
|
|
6130
|
+
try {
|
|
6131
|
+
let resolvedId;
|
|
6132
|
+
if (prompt_id) {
|
|
6133
|
+
const prompt = getPrompt(prompt_id);
|
|
6134
|
+
if (!prompt)
|
|
6135
|
+
return err(`Prompt not found: ${prompt_id}`);
|
|
6136
|
+
resolvedId = prompt.id;
|
|
6137
|
+
}
|
|
6138
|
+
const schedules = listSchedules(resolvedId);
|
|
6139
|
+
return ok({ schedules, count: schedules.length });
|
|
6140
|
+
} catch (e) {
|
|
6141
|
+
return err(e instanceof Error ? e.message : String(e));
|
|
6142
|
+
}
|
|
6143
|
+
});
|
|
6144
|
+
server.registerTool("prompts_unschedule", {
|
|
6145
|
+
description: "Delete a prompt schedule by ID.",
|
|
6146
|
+
inputSchema: { id: exports_external.string().describe("Schedule ID (e.g. SCH-ABC123)") }
|
|
6147
|
+
}, async ({ id }) => {
|
|
6148
|
+
try {
|
|
6149
|
+
const schedule = getSchedule(id);
|
|
6150
|
+
if (!schedule)
|
|
6151
|
+
return err(`Schedule not found: ${id}`);
|
|
6152
|
+
deleteSchedule(id);
|
|
6153
|
+
return ok({ deleted: true, id });
|
|
6154
|
+
} catch (e) {
|
|
6155
|
+
return err(e instanceof Error ? e.message : String(e));
|
|
6156
|
+
}
|
|
6157
|
+
});
|
|
6158
|
+
server.registerTool("prompts_get_due", {
|
|
6159
|
+
description: "Get all prompts that are due to run now. Returns the rendered prompt text for each. Automatically advances next_run_at after retrieval. Call this on a loop (e.g. every minute) to drive scheduled prompt execution.",
|
|
6160
|
+
inputSchema: {}
|
|
6161
|
+
}, async () => {
|
|
6162
|
+
try {
|
|
6163
|
+
const due = getDueSchedules();
|
|
6164
|
+
if (!due.length)
|
|
6165
|
+
return ok({ due: [], count: 0, message: "No prompts due right now." });
|
|
6166
|
+
return ok({
|
|
6167
|
+
due: due.map((d) => ({
|
|
6168
|
+
schedule_id: d.id,
|
|
6169
|
+
prompt_id: d.prompt_id,
|
|
6170
|
+
prompt_slug: d.prompt_slug,
|
|
6171
|
+
cron: d.cron,
|
|
6172
|
+
rendered: d.rendered,
|
|
6173
|
+
next_run_at: d.next_run_at,
|
|
6174
|
+
run_count: d.run_count
|
|
6175
|
+
})),
|
|
6176
|
+
count: due.length
|
|
6177
|
+
});
|
|
6178
|
+
} catch (e) {
|
|
6179
|
+
return err(e instanceof Error ? e.message : String(e));
|
|
6180
|
+
}
|
|
6181
|
+
});
|
|
6182
|
+
server.registerTool("prompts_next_run", {
|
|
6183
|
+
description: "Preview when a cron expression will next fire, without creating a schedule.",
|
|
6184
|
+
inputSchema: {
|
|
6185
|
+
cron: exports_external.string().describe("Cron expression (5 fields)"),
|
|
6186
|
+
count: exports_external.number().optional().describe("Number of next runs to preview (default: 5)")
|
|
6187
|
+
}
|
|
6188
|
+
}, async ({ cron, count = 5 }) => {
|
|
6189
|
+
try {
|
|
6190
|
+
const cronError = validateCron(cron);
|
|
6191
|
+
if (cronError)
|
|
6192
|
+
return err(`Invalid cron expression: ${cronError}`);
|
|
6193
|
+
const runs = [];
|
|
6194
|
+
let from = new Date;
|
|
6195
|
+
for (let i = 0;i < count; i++) {
|
|
6196
|
+
const next = getNextRunTime(cron, from);
|
|
6197
|
+
runs.push(next.toISOString());
|
|
6198
|
+
from = next;
|
|
6199
|
+
}
|
|
6200
|
+
return ok({ cron, next_runs: runs });
|
|
6201
|
+
} catch (e) {
|
|
6202
|
+
return err(e instanceof Error ? e.message : String(e));
|
|
6203
|
+
}
|
|
6204
|
+
});
|
|
5926
6205
|
var _agentReg = new Map;
|
|
5927
6206
|
server.tool("register_agent", "Register this agent session. Returns agent_id for use in heartbeat/set_focus.", { name: exports_external.string(), session_id: exports_external.string().optional() }, async (a) => {
|
|
5928
6207
|
const existing = [..._agentReg.values()].find((x) => x.name === a.name);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hasna/prompts",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.6",
|
|
4
4
|
"description": "Reusable prompt library for AI agents — CLI + MCP server + REST API + web dashboard",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -49,6 +49,7 @@
|
|
|
49
49
|
"dashboard"
|
|
50
50
|
],
|
|
51
51
|
"publishConfig": {
|
|
52
|
+
"registry": "https://registry.npmjs.org",
|
|
52
53
|
"access": "public"
|
|
53
54
|
},
|
|
54
55
|
"repository": {
|