@celljs/ta-cron 4.8.0 → 4.9.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.
- package/README.en.md +3 -3
- package/README.md +3 -3
- package/host/index.mjs +19 -12
- package/package.json +13 -4
- package/web/index.js +249 -281
- package/web-src/commands.spec.ts +0 -55
- package/web-src/commands.ts +0 -259
- package/web-src/cron-expression.spec.ts +0 -41
- package/web-src/cron-expression.ts +0 -150
- package/web-src/cron-protocol.ts +0 -151
- package/web-src/editor.tsx +0 -249
- package/web-src/extension.ts +0 -187
- package/web-src/form-util.spec.ts +0 -33
- package/web-src/form-util.ts +0 -141
- package/web-src/host.ts +0 -12
- package/web-src/i18n.ts +0 -175
- package/web-src/index.tsx +0 -35
- package/web-src/model-picker.spec.ts +0 -32
- package/web-src/model-picker.tsx +0 -227
- package/web-src/notifier.ts +0 -47
- package/web-src/page.ts +0 -126
- package/web-src/react.ts +0 -11
- package/web-src/reply.tsx +0 -80
- package/web-src/schedule-util.spec.ts +0 -48
- package/web-src/schedule-util.ts +0 -233
- package/web-src/scheduler.spec.ts +0 -254
- package/web-src/scheduler.ts +0 -380
- package/web-src/server-texts.spec.ts +0 -10
- package/web-src/server-texts.ts +0 -18
- package/web-src/style.ts +0 -133
- package/web-src/task-store.ts +0 -67
- package/web-src/task-util.ts +0 -174
- package/web-src/tasks-view.tsx +0 -279
- package/web-src/tool.ts +0 -135
package/web-src/scheduler.ts
DELETED
|
@@ -1,380 +0,0 @@
|
|
|
1
|
-
import type { TaExtensionHost, TaPackageService } from '@celljs/ta-protocol';
|
|
2
|
-
import { type CronService, Limits, NAME, type Run, type Task, type TaskInput, type TaskPatch } from './cron-protocol';
|
|
3
|
-
import { ScheduleUtil } from './schedule-util';
|
|
4
|
-
import { TaskStore } from './task-store';
|
|
5
|
-
import { TaskUtil } from './task-util';
|
|
6
|
-
|
|
7
|
-
/** What the scheduler needs of the Ta service: its sessions and the data of this package. */
|
|
8
|
-
export type CronHost = Pick<
|
|
9
|
-
TaExtensionHost,
|
|
10
|
-
| 'getServerInfo'
|
|
11
|
-
| 'listSessions'
|
|
12
|
-
| 'openSession'
|
|
13
|
-
| 'closeSession'
|
|
14
|
-
| 'setSessionName'
|
|
15
|
-
| 'prompt'
|
|
16
|
-
| 'abort'
|
|
17
|
-
| 'getState'
|
|
18
|
-
| 'getLastAssistantText'
|
|
19
|
-
| 'getExtensionData'
|
|
20
|
-
| 'setExtensionData'
|
|
21
|
-
>;
|
|
22
|
-
|
|
23
|
-
export interface SchedulerOptions {
|
|
24
|
-
now(): Date;
|
|
25
|
-
/** The longest the timer sleeps: it looks again at least this often, so a changed clock or a sleeping machine is caught up. */
|
|
26
|
-
tickMs: number;
|
|
27
|
-
/** How often a run is looked at while it works. */
|
|
28
|
-
pollMs: number;
|
|
29
|
-
timeoutMs: number;
|
|
30
|
-
parallel: number;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/** A run that has started or waits for a slot. */
|
|
34
|
-
interface Active {
|
|
35
|
-
run: Run;
|
|
36
|
-
task: Task;
|
|
37
|
-
sessionId?: string;
|
|
38
|
-
/** The task went while it ran: its end is not recorded. */
|
|
39
|
-
dropped?: boolean;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* The tasks, in the Ta service: one timer to the next time a task is due (at most a minute), and each run
|
|
44
|
-
* in a conversation of its own (the package's group, closed once it ended and nobody looks at it). A due
|
|
45
|
-
* task whose run has not ended is not run again: its next time comes instead. When the service starts,
|
|
46
|
-
* runs it left unfinished fail, a repeating task that was due while it was not running runs once (the
|
|
47
|
-
* missed times collapse into one), and a task that runs once and is more than five minutes late is missed.
|
|
48
|
-
*/
|
|
49
|
-
export class CronScheduler implements CronService, TaPackageService {
|
|
50
|
-
protected tasks: Task[] = [];
|
|
51
|
-
protected readonly store: TaskStore;
|
|
52
|
-
protected readonly options: SchedulerOptions;
|
|
53
|
-
protected readonly active = new Map<string, Active>();
|
|
54
|
-
protected readonly waiting: Active[] = [];
|
|
55
|
-
protected working = 0;
|
|
56
|
-
/** Conversations of runs that ended while someone looked at them: closed once nobody does. */
|
|
57
|
-
protected readonly lingering = new Set<string>();
|
|
58
|
-
protected timer?: ReturnType<typeof setTimeout>;
|
|
59
|
-
protected disposed = false;
|
|
60
|
-
|
|
61
|
-
constructor(
|
|
62
|
-
protected readonly host: CronHost,
|
|
63
|
-
options: Partial<SchedulerOptions> = {},
|
|
64
|
-
) {
|
|
65
|
-
this.store = new TaskStore(host);
|
|
66
|
-
this.options = { now: () => new Date(), tickMs: 60_000, pollMs: 1000, timeoutMs: Limits.RUN_TIMEOUT_MS, parallel: Limits.PARALLEL_RUNS, ...options };
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
async start(): Promise<void> {
|
|
70
|
-
this.tasks = await this.store.tasks();
|
|
71
|
-
const now = this.options.now();
|
|
72
|
-
for (const task of this.tasks) {
|
|
73
|
-
if (task.lastStatus === 'running') {
|
|
74
|
-
await this.reclaim(task, now);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
for (const task of [...this.tasks]) {
|
|
78
|
-
if (task.enabled && TaskUtil.missed(task, now)) {
|
|
79
|
-
await this.miss(task, now);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
await this.store.saveTasks(this.tasks);
|
|
83
|
-
this.arm();
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
async dispose(): Promise<void> {
|
|
87
|
-
this.disposed = true;
|
|
88
|
-
clearTimeout(this.timer);
|
|
89
|
-
await this.store.flush();
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
async list(): Promise<Task[]> {
|
|
93
|
-
return this.tasks.map(task => ({ ...task }));
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
async get(ref: string): Promise<Task> {
|
|
97
|
-
return { ...TaskUtil.find(this.tasks, ref) };
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
async runs(ref: string, limit = Limits.RUNS_KEPT): Promise<Run[]> {
|
|
101
|
-
const task = TaskUtil.find(this.tasks, ref);
|
|
102
|
-
return (await this.store.runs(task.id)).slice(0, Math.max(1, limit));
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
async add(input: TaskInput): Promise<Task> {
|
|
106
|
-
const cwd = (await this.host.getServerInfo()).cwd;
|
|
107
|
-
const task = TaskUtil.create(input, cwd, this.options.now());
|
|
108
|
-
await this.save([...this.tasks, task]);
|
|
109
|
-
return { ...task };
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
async update(ref: string, patch: TaskPatch): Promise<Task> {
|
|
113
|
-
const task = TaskUtil.find(this.tasks, ref);
|
|
114
|
-
const next = TaskUtil.patch(task, patch ?? {}, this.options.now());
|
|
115
|
-
await this.replace(next);
|
|
116
|
-
return { ...next };
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
setEnabled(ref: string, enabled: boolean): Promise<Task> {
|
|
120
|
-
return this.update(ref, { enabled: enabled === true });
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
async remove(ref: string): Promise<Task> {
|
|
124
|
-
const task = TaskUtil.find(this.tasks, ref);
|
|
125
|
-
await this.drop([task]);
|
|
126
|
-
return task;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
async clear(cwd?: string): Promise<Task[]> {
|
|
130
|
-
const gone = this.tasks.filter(task => !cwd || task.cwd === cwd);
|
|
131
|
-
await this.drop(gone);
|
|
132
|
-
return gone;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
async run(ref: string): Promise<Run> {
|
|
136
|
-
const task = TaskUtil.find(this.tasks, ref);
|
|
137
|
-
if (this.active.has(task.id)) {
|
|
138
|
-
throw new Error(`${task.name} is running now; wait for its run to end.`);
|
|
139
|
-
}
|
|
140
|
-
return this.begin(task, 'manual', this.options.now());
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/** Whether the conversation is that of a run (by the id of its open session). */
|
|
144
|
-
isRun(sessionId: string): boolean {
|
|
145
|
-
return [...this.active.values()].some(active => active.sessionId === sessionId);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
protected arm(): void {
|
|
149
|
-
clearTimeout(this.timer);
|
|
150
|
-
if (this.disposed) {
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
const now = this.options.now().getTime();
|
|
154
|
-
let delay = this.options.tickMs;
|
|
155
|
-
for (const task of this.tasks) {
|
|
156
|
-
if (task.enabled && task.nextRunAt) {
|
|
157
|
-
delay = Math.min(delay, Math.max(0, new Date(task.nextRunAt).getTime() - now));
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
this.timer = setTimeout(() => void this.tick().catch(error => console.error(`${NAME}: ${message(error)}`)), delay);
|
|
161
|
-
(this.timer as { unref?(): void }).unref?.();
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
protected async tick(): Promise<void> {
|
|
165
|
-
if (this.disposed) {
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
const now = this.options.now();
|
|
169
|
-
for (const task of this.tasks) {
|
|
170
|
-
if (!task.enabled || !task.nextRunAt || new Date(task.nextRunAt).getTime() > now.getTime()) {
|
|
171
|
-
continue;
|
|
172
|
-
}
|
|
173
|
-
if (this.active.has(task.id)) {
|
|
174
|
-
// Still at work: the time that came is let go.
|
|
175
|
-
await this.replace(this.advance(task, now));
|
|
176
|
-
} else {
|
|
177
|
-
await this.begin(task, 'schedule', now);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
await this.sweep();
|
|
181
|
-
this.arm();
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/** The task after a run started at `now` by its schedule: its next time, or done for one that runs once. */
|
|
185
|
-
protected advance(task: Task, now: Date): Task {
|
|
186
|
-
if (!ScheduleUtil.repeats(task.schedule)) {
|
|
187
|
-
const { nextRunAt: _done, ...rest } = task;
|
|
188
|
-
return { ...rest, enabled: false };
|
|
189
|
-
}
|
|
190
|
-
const next = ScheduleUtil.next(task.schedule, now);
|
|
191
|
-
return { ...task, nextRunAt: next?.toISOString() };
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
protected async begin(task: Task, trigger: Run['trigger'], now: Date): Promise<Run> {
|
|
195
|
-
const run: Run = { id: TaskUtil.newId('run'), taskId: task.id, trigger, status: 'running', startedAt: now.toISOString(), cwd: task.cwd };
|
|
196
|
-
const started = trigger === 'schedule' ? this.advance(task, now) : task;
|
|
197
|
-
await this.replace({ ...started, lastRunAt: run.startedAt, lastStatus: 'running' });
|
|
198
|
-
await this.store.saveRun(run);
|
|
199
|
-
const active: Active = { run, task: { ...task } };
|
|
200
|
-
this.active.set(task.id, active);
|
|
201
|
-
this.waiting.push(active);
|
|
202
|
-
this.pump();
|
|
203
|
-
return { ...run };
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
protected pump(): void {
|
|
207
|
-
while (this.working < this.options.parallel && this.waiting.length > 0 && !this.disposed) {
|
|
208
|
-
const active = this.waiting.shift() as Active;
|
|
209
|
-
this.working++;
|
|
210
|
-
void this.execute(active)
|
|
211
|
-
.catch(error => console.error(`${NAME}: ${message(error)}`))
|
|
212
|
-
.finally(() => {
|
|
213
|
-
this.working--;
|
|
214
|
-
this.pump();
|
|
215
|
-
});
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
protected async execute(active: Active): Promise<void> {
|
|
220
|
-
const { task, run } = active;
|
|
221
|
-
if (active.dropped) {
|
|
222
|
-
return;
|
|
223
|
-
}
|
|
224
|
-
try {
|
|
225
|
-
const now = this.options.now();
|
|
226
|
-
const { sessionId } = await this.host.openSession({
|
|
227
|
-
cwd: task.cwd,
|
|
228
|
-
group: NAME,
|
|
229
|
-
appendSystemPrompt: TaskUtil.systemPrompt(task, now),
|
|
230
|
-
...(task.model ? { model: task.model } : {}),
|
|
231
|
-
...(task.thinking ? { thinkingLevel: task.thinking as never } : {}),
|
|
232
|
-
});
|
|
233
|
-
active.sessionId = sessionId;
|
|
234
|
-
await this.host.setSessionName(sessionId, `${task.name} · ${ScheduleUtil.clock(now)}`).catch(() => undefined);
|
|
235
|
-
const state = await this.host.getState(sessionId);
|
|
236
|
-
run.sessionFile = state.sessionFile;
|
|
237
|
-
await this.store.saveRun(run);
|
|
238
|
-
await this.host.prompt(sessionId, { clientMessageId: run.id, message: task.prompt });
|
|
239
|
-
await this.settled(active, state.messageCount);
|
|
240
|
-
const { text } = await this.host.getLastAssistantText(sessionId);
|
|
241
|
-
if (!text?.trim()) {
|
|
242
|
-
throw new Error('The run ended without an answer; the model may have failed. Open its conversation to see.');
|
|
243
|
-
}
|
|
244
|
-
await this.finish(active, { status: 'success', summary: clip(text.trim()) });
|
|
245
|
-
} catch (error) {
|
|
246
|
-
await this.finish(active, { status: 'error', error: message(error), ...(error instanceof TimeoutError ? { reason: 'timeout' as const } : {}) });
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
protected async settled(active: Active, before: number): Promise<void> {
|
|
251
|
-
const sessionId = active.sessionId as string;
|
|
252
|
-
const start = Date.now();
|
|
253
|
-
for (;;) {
|
|
254
|
-
if (active.dropped || this.disposed) {
|
|
255
|
-
throw new Error('The task was removed while it ran.');
|
|
256
|
-
}
|
|
257
|
-
const state = await this.host.getState(sessionId);
|
|
258
|
-
if (!state.isStreaming && state.pendingMessageCount === 0 && state.messageCount > before) {
|
|
259
|
-
return;
|
|
260
|
-
}
|
|
261
|
-
if (Date.now() - start > this.options.timeoutMs) {
|
|
262
|
-
await this.host.abort(sessionId).catch(() => undefined);
|
|
263
|
-
throw new TimeoutError(`The run took longer than ${Math.round(this.options.timeoutMs / 60_000)} minutes and was stopped.`);
|
|
264
|
-
}
|
|
265
|
-
await new Promise(resolve => setTimeout(resolve, this.options.pollMs));
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
protected async finish(active: Active, end: Pick<Run, 'status' | 'summary' | 'error' | 'reason'>): Promise<void> {
|
|
270
|
-
this.active.delete(active.task.id);
|
|
271
|
-
const sessionId = active.sessionId;
|
|
272
|
-
if (sessionId) {
|
|
273
|
-
this.lingering.add(sessionId);
|
|
274
|
-
}
|
|
275
|
-
if (active.dropped || this.disposed) {
|
|
276
|
-
await this.sweep();
|
|
277
|
-
return;
|
|
278
|
-
}
|
|
279
|
-
const run: Run = { ...active.run, ...end, endedAt: this.options.now().toISOString() };
|
|
280
|
-
await this.store.saveRun(run);
|
|
281
|
-
const current = this.tasks.find(task => task.id === active.task.id);
|
|
282
|
-
if (current) {
|
|
283
|
-
const failed = run.status === 'error';
|
|
284
|
-
await this.replace({
|
|
285
|
-
...current,
|
|
286
|
-
lastStatus: run.status,
|
|
287
|
-
runCount: current.runCount + 1,
|
|
288
|
-
failCount: current.failCount + (failed ? 1 : 0),
|
|
289
|
-
});
|
|
290
|
-
await this.store.finished({ run, taskName: current.name, notify: ScheduleUtil.shouldNotify(ScheduleUtil.notifyLevel(current), failed) });
|
|
291
|
-
}
|
|
292
|
-
await this.sweep();
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
/** Closes the conversations of runs that ended, unless someone looks at one: then the next time. */
|
|
296
|
-
protected async sweep(): Promise<void> {
|
|
297
|
-
if (this.lingering.size === 0) {
|
|
298
|
-
return;
|
|
299
|
-
}
|
|
300
|
-
const open = new Map((await this.host.listSessions()).map(info => [info.sessionId, info]));
|
|
301
|
-
for (const sessionId of [...this.lingering]) {
|
|
302
|
-
const info = open.get(sessionId);
|
|
303
|
-
if (!info) {
|
|
304
|
-
this.lingering.delete(sessionId);
|
|
305
|
-
} else if (info.attached === 0 && !info.isStreaming) {
|
|
306
|
-
this.lingering.delete(sessionId);
|
|
307
|
-
await this.host.closeSession(sessionId).catch(() => undefined);
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
/** A run the service left unfinished when it stopped. */
|
|
313
|
-
protected async reclaim(task: Task, now: Date): Promise<void> {
|
|
314
|
-
for (const run of await this.store.runs(task.id)) {
|
|
315
|
-
if (run.status === 'running') {
|
|
316
|
-
await this.store.saveRun({
|
|
317
|
-
...run,
|
|
318
|
-
status: 'error',
|
|
319
|
-
reason: 'interrupted',
|
|
320
|
-
error: 'The Ta service stopped before the run ended.',
|
|
321
|
-
endedAt: now.toISOString(),
|
|
322
|
-
});
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
this.tasks = this.tasks.map(other => (other.id === task.id ? { ...other, lastStatus: 'error', failCount: other.failCount + 1 } : other));
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
/** A task that runs once, whose time passed while the service was not running. */
|
|
329
|
-
protected async miss(task: Task, now: Date): Promise<void> {
|
|
330
|
-
const run: Run = {
|
|
331
|
-
id: TaskUtil.newId('run'),
|
|
332
|
-
taskId: task.id,
|
|
333
|
-
trigger: 'schedule',
|
|
334
|
-
status: 'missed',
|
|
335
|
-
startedAt: task.nextRunAt ?? now.toISOString(),
|
|
336
|
-
endedAt: now.toISOString(),
|
|
337
|
-
error: 'The Ta service was not running at its time; run it by hand if it is still wanted.',
|
|
338
|
-
cwd: task.cwd,
|
|
339
|
-
};
|
|
340
|
-
await this.store.saveRun(run);
|
|
341
|
-
const { nextRunAt: _gone, ...rest } = task;
|
|
342
|
-
this.tasks = this.tasks.map(other => (other.id === task.id ? { ...rest, enabled: false, lastStatus: 'missed', lastRunAt: run.startedAt } : other));
|
|
343
|
-
await this.store.finished({ run, taskName: task.name, notify: ScheduleUtil.notifyLevel(task) !== 'never' });
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
protected async drop(tasks: Task[]): Promise<void> {
|
|
347
|
-
const ids = new Set(tasks.map(task => task.id));
|
|
348
|
-
for (const id of ids) {
|
|
349
|
-
const active = this.active.get(id);
|
|
350
|
-
if (active) {
|
|
351
|
-
active.dropped = true;
|
|
352
|
-
if (active.sessionId) {
|
|
353
|
-
await this.host.abort(active.sessionId).catch(() => undefined);
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
await this.store.removeRuns(id);
|
|
357
|
-
}
|
|
358
|
-
await this.save(this.tasks.filter(task => !ids.has(task.id)));
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
protected replace(task: Task): Promise<void> {
|
|
362
|
-
return this.save(this.tasks.map(other => (other.id === task.id ? task : other)));
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
protected async save(tasks: Task[]): Promise<void> {
|
|
366
|
-
this.tasks = tasks;
|
|
367
|
-
await this.store.saveTasks(tasks);
|
|
368
|
-
this.arm();
|
|
369
|
-
}
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
class TimeoutError extends Error {}
|
|
373
|
-
|
|
374
|
-
function clip(text: string): string {
|
|
375
|
-
return text.length > Limits.SUMMARY_MAX ? `${text.slice(0, Limits.SUMMARY_MAX - 1)}…` : text;
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
function message(error: unknown): string {
|
|
379
|
-
return error instanceof Error ? error.message : String(error);
|
|
380
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it } from 'bun:test';
|
|
2
|
-
import { CronServerTexts } from './server-texts';
|
|
3
|
-
|
|
4
|
-
describe('CronServerTexts', () => {
|
|
5
|
-
it("asks in the language of Ta's pages, else the computer's", () => {
|
|
6
|
-
expect(CronServerTexts.zh({ TA_LANGUAGE: 'zh-CN', LANG: 'en_US.UTF-8' })).toBe(true);
|
|
7
|
-
expect(CronServerTexts.zh({ LANG: 'en_US.UTF-8' })).toBe(false);
|
|
8
|
-
expect(CronServerTexts.zh({ LANG: 'zh_CN.UTF-8' })).toBe(true);
|
|
9
|
-
});
|
|
10
|
-
});
|
package/web-src/server-texts.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/** What the pi extension of ta-cron asks the user, in the language of Ta's pages (`TA_LANGUAGE`) or the computer's. */
|
|
2
|
-
export namespace CronServerTexts {
|
|
3
|
-
export function zh(env: NodeJS.ProcessEnv = process.env): boolean {
|
|
4
|
-
return /^zh/i.test(env.TA_LANGUAGE || env.LC_ALL || env.LC_MESSAGES || env.LANG || '');
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export function removeTask(name: string): [string, string] {
|
|
8
|
-
return zh()
|
|
9
|
-
? ['删除这个任务?', `${name} 和它的运行记录会被删除,运行产生的对话会保留。`]
|
|
10
|
-
: ['Delete the scheduled task?', `${name} and its runs go; their conversations stay.`];
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function removeTasks(count: number): [string, string] {
|
|
14
|
-
return zh()
|
|
15
|
-
? ['删除这个目录的所有定时任务?', `${count} 个任务和它们的运行记录会被删除。`]
|
|
16
|
-
: ['Delete the scheduled tasks of this directory?', `${count} tasks and their runs go.`];
|
|
17
|
-
}
|
|
18
|
-
}
|
package/web-src/style.ts
DELETED
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
/** The styles of the package; only the --ta-* variables of the page, so they follow its theme. */
|
|
2
|
-
export const STYLE = `
|
|
3
|
-
.tcr-page { box-sizing: border-box; width: 100%; max-width: 860px; margin: 0 auto; padding: 20px 24px 48px; }
|
|
4
|
-
.tcr-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; min-height: 32px; }
|
|
5
|
-
.tcr-stat { color: var(--ta-muted); font-size: 13px; }
|
|
6
|
-
.tcr-button { display: inline-flex; align-items: center; gap: 6px; height: 32px; padding: 0 12px; border: 1px solid var(--ta-border); border-radius: 8px; background: transparent; color: var(--ta-text); font: inherit; font-size: 13px; font-weight: 500; cursor: pointer; transition: background 120ms, opacity 120ms, transform 120ms var(--ta-ease); }
|
|
7
|
-
.tcr-button:hover:not(:disabled) { background: var(--ta-hover); }
|
|
8
|
-
.tcr-button:active:not(:disabled) { transform: scale(.98); }
|
|
9
|
-
.tcr-button:disabled { opacity: .45; cursor: default; }
|
|
10
|
-
.tcr-primary { background: var(--ta-brand); border-color: var(--ta-brand); color: var(--ta-brand-text); }
|
|
11
|
-
.tcr-primary:hover:not(:disabled) { background: var(--ta-brand); opacity: .88; }
|
|
12
|
-
.tcr-link { align-self: flex-start; border: 0; padding: 0; background: none; color: var(--ta-link); font: inherit; font-size: 12.5px; cursor: pointer; }
|
|
13
|
-
.tcr-link:hover { text-decoration: underline; }
|
|
14
|
-
.tcr-icon { display: inline-flex; align-items: center; justify-content: center; width: 28px; height: 28px; border: 0; border-radius: 7px; background: none; color: var(--ta-muted); cursor: pointer; }
|
|
15
|
-
.tcr-icon:hover { background: var(--ta-hover); color: var(--ta-text); }
|
|
16
|
-
.tcr-muted { color: var(--ta-muted); font-size: 12.5px; }
|
|
17
|
-
.tcr-pad { padding: 6px 2px; }
|
|
18
|
-
.tcr-chip { flex: none; padding: 0 7px; border-radius: 6px; background: var(--ta-chip); color: var(--ta-secondary); font-size: 11.5px; line-height: 20px; white-space: nowrap; }
|
|
19
|
-
.tcr-list { display: flex; flex-direction: column; gap: 8px; }
|
|
20
|
-
.tcr-task { border: 1px solid var(--ta-border-muted); border-radius: 12px; background: var(--ta-card); transition: border-color 120ms, box-shadow 160ms; animation: tcr-in 200ms var(--ta-ease) both; }
|
|
21
|
-
.tcr-task:hover, .tcr-task.tcr-open { border-color: var(--ta-border); }
|
|
22
|
-
.tcr-task.tcr-menu-open { position: relative; z-index: 5; }
|
|
23
|
-
.tcr-task.tcr-open { box-shadow: var(--ta-shadow-pop); }
|
|
24
|
-
.tcr-disabled .tcr-task-name, .tcr-disabled .tcr-chip { opacity: .55; }
|
|
25
|
-
@keyframes tcr-in { from { opacity: 0; transform: translateY(4px); } }
|
|
26
|
-
.tcr-task-row { display: flex; align-items: center; gap: 6px; padding: 0 8px 0 0; }
|
|
27
|
-
.tcr-task-main { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; height: 52px; padding: 0 6px 0 16px; border: 0; background: none; color: inherit; font: inherit; text-align: left; cursor: pointer; }
|
|
28
|
-
.tcr-task-name { min-width: 0; overflow: hidden; font-size: 14px; font-weight: 600; text-overflow: ellipsis; white-space: nowrap; }
|
|
29
|
-
.tcr-task-dir { flex: 1; min-width: 0; overflow: hidden; color: var(--ta-muted); font-size: 12.5px; text-overflow: ellipsis; white-space: nowrap; }
|
|
30
|
-
.tcr-task-state { flex: none; color: var(--ta-secondary); font-size: 12.5px; font-variant-numeric: tabular-nums; }
|
|
31
|
-
.tcr-dot { flex: none; width: 8px; height: 8px; border-radius: 50%; background: var(--ta-border); }
|
|
32
|
-
.tcr-success { background: var(--ta-green); }
|
|
33
|
-
.tcr-error, .tcr-missed { background: var(--ta-red); }
|
|
34
|
-
.tcr-running { background: var(--ta-blue); animation: tcr-pulse 1.2s ease-in-out infinite; }
|
|
35
|
-
.tcr-off { background: transparent; box-shadow: inset 0 0 0 1.5px var(--ta-border); }
|
|
36
|
-
.tcr-pulse { animation: tcr-fade 1.6s ease-in-out infinite; }
|
|
37
|
-
@keyframes tcr-pulse { 50% { transform: scale(1.5); opacity: .5; } }
|
|
38
|
-
@keyframes tcr-fade { 50% { opacity: .45; } }
|
|
39
|
-
.tcr-switch { position: relative; flex: none; width: 30px; height: 18px; cursor: pointer; }
|
|
40
|
-
.tcr-switch input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; }
|
|
41
|
-
.tcr-switch span { position: absolute; inset: 0; border-radius: 999px; background: var(--ta-border); transition: background 160ms; pointer-events: none; }
|
|
42
|
-
.tcr-switch span::after { content: ''; position: absolute; top: 2px; left: 2px; width: 14px; height: 14px; border-radius: 50%; background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,.25); transition: transform 180ms var(--ta-ease); }
|
|
43
|
-
.tcr-switch input:checked + span { background: var(--ta-green); }
|
|
44
|
-
.tcr-switch input:checked + span::after { transform: translateX(12px); }
|
|
45
|
-
.tcr-switch input:focus-visible + span { outline: 2px solid var(--ta-border-focus); outline-offset: 2px; }
|
|
46
|
-
.tcr-more { position: relative; }
|
|
47
|
-
.tcr-menu { position: absolute; top: calc(100% + 4px); right: 0; z-index: 30; min-width: 150px; padding: 4px; border: 1px solid var(--ta-border-muted); border-radius: 10px; background: var(--ta-raised); box-shadow: var(--ta-shadow-pop); animation: tcr-in 120ms var(--ta-ease); }
|
|
48
|
-
.tcr-menu-item { display: flex; align-items: center; gap: 8px; width: 100%; padding: 7px 10px; border: 0; border-radius: 7px; background: none; color: var(--ta-text); font: inherit; font-size: 13px; text-align: left; cursor: pointer; }
|
|
49
|
-
.tcr-menu-item:hover { background: var(--ta-hover); }
|
|
50
|
-
.tcr-danger { color: var(--ta-red); }
|
|
51
|
-
.tcr-detail { padding: 2px 16px 14px 34px; animation: tcr-in 160ms var(--ta-ease); }
|
|
52
|
-
.tcr-prompt-text { margin: 0 0 12px; padding: 10px 12px; border-radius: 8px; background: var(--ta-chip); color: var(--ta-secondary); font-size: 13px; line-height: 1.55; white-space: pre-wrap; max-height: 160px; overflow: auto; }
|
|
53
|
-
.tcr-label { margin: 0 0 6px; color: var(--ta-muted); font-size: 12px; font-weight: 500; }
|
|
54
|
-
.tcr-run { border-radius: 8px; }
|
|
55
|
-
.tcr-run.tcr-open { background: var(--ta-hover); }
|
|
56
|
-
.tcr-run-head { display: flex; align-items: center; gap: 10px; width: 100%; height: 34px; padding: 0 8px; border: 0; border-radius: 8px; background: none; color: inherit; font: inherit; font-size: 13px; text-align: left; cursor: pointer; }
|
|
57
|
-
.tcr-run-head:hover { background: var(--ta-hover); }
|
|
58
|
-
.tcr-run-time { flex: none; font-variant-numeric: tabular-nums; }
|
|
59
|
-
.tcr-run-status { flex: none; color: var(--ta-secondary); }
|
|
60
|
-
.tcr-run-line { flex: 1; min-width: 0; overflow: hidden; color: var(--ta-muted); text-overflow: ellipsis; white-space: nowrap; }
|
|
61
|
-
.tcr-run-body { display: flex; flex-direction: column; align-items: flex-start; gap: 10px; padding: 2px 10px 12px 26px; font-size: 13.5px; }
|
|
62
|
-
.tcr-run-body > :first-child { align-self: stretch; }
|
|
63
|
-
.tcr-run-error { color: var(--ta-red); white-space: pre-wrap; }
|
|
64
|
-
.tcr-empty { display: flex; flex-direction: column; align-items: center; padding: 48px 0 24px; text-align: center; }
|
|
65
|
-
.tcr-empty-title { margin-top: 14px; font-size: 17px; font-weight: 600; }
|
|
66
|
-
.tcr-empty-hint { margin-top: 6px; color: var(--ta-muted); font-size: 13px; }
|
|
67
|
-
.tcr-clock { width: 112px; height: 112px; }
|
|
68
|
-
.tcr-clock-face { fill: var(--ta-card); stroke: var(--ta-border); stroke-width: 2; }
|
|
69
|
-
.tcr-clock-tick { stroke: var(--ta-muted); stroke-width: 2; stroke-linecap: round; }
|
|
70
|
-
.tcr-clock-hour { stroke: var(--ta-text); stroke-width: 4; stroke-linecap: round; transform-origin: 60px 60px; transform: rotate(-60deg); }
|
|
71
|
-
.tcr-clock-hand { stroke: var(--ta-green); stroke-width: 2.5; stroke-linecap: round; transform-origin: 60px 60px; animation: tcr-spin 6s steps(60) infinite; }
|
|
72
|
-
.tcr-clock-pin { fill: var(--ta-green); }
|
|
73
|
-
@keyframes tcr-spin { to { transform: rotate(360deg); } }
|
|
74
|
-
.tcr-examples { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; width: 100%; max-width: 640px; margin-top: 26px; }
|
|
75
|
-
.tcr-example { display: flex; flex-direction: column; align-items: flex-start; gap: 10px; padding: 14px; border: 1px solid var(--ta-border-muted); border-radius: 12px; background: var(--ta-card); color: var(--ta-text); font: inherit; text-align: left; cursor: pointer; transition: border-color 120ms, transform 160ms var(--ta-ease), box-shadow 160ms; animation: tcr-in 240ms var(--ta-ease) both; }
|
|
76
|
-
.tcr-example:nth-child(2) { animation-delay: 60ms; }
|
|
77
|
-
.tcr-example:nth-child(3) { animation-delay: 120ms; }
|
|
78
|
-
.tcr-example:hover { border-color: var(--ta-border); transform: translateY(-2px); box-shadow: var(--ta-shadow-pop); }
|
|
79
|
-
.tcr-example svg { color: var(--ta-green); }
|
|
80
|
-
.tcr-example-text { font-size: 13.5px; font-weight: 500; }
|
|
81
|
-
@media (max-width: 640px) { .tcr-examples { grid-template-columns: 1fr; } .tcr-task-dir { display: none; } .tcr-page { padding: 16px 12px 40px; } }
|
|
82
|
-
.tcr-form { box-sizing: border-box; display: flex; flex-direction: column; gap: 16px; width: 100%; min-height: 0; overflow-y: auto; padding: 4px 2px 2px; }
|
|
83
|
-
.tcr-field { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
|
|
84
|
-
.tcr-field .tcr-label { margin: 0; }
|
|
85
|
-
.tcr-input { box-sizing: border-box; width: 100%; height: 36px; padding: 0 10px; border: 1px solid var(--ta-border-field); border-radius: 8px; background: var(--ta-input); color: var(--ta-text); font: inherit; font-size: 13.5px; outline: none; transition: border-color 120ms; }
|
|
86
|
-
.tcr-input:focus { border-color: var(--ta-border-focus); }
|
|
87
|
-
.tcr-prompt { height: auto; padding: 8px 10px; line-height: 1.5; resize: vertical; }
|
|
88
|
-
.tcr-mono { font-family: var(--ta-mono); }
|
|
89
|
-
.tcr-amount { width: 90px; flex: none; }
|
|
90
|
-
.tcr-row { display: flex; gap: 8px; }
|
|
91
|
-
.tcr-segments { display: flex; gap: 2px; padding: 2px; border-radius: 9px; background: var(--ta-chip); align-self: flex-start; max-width: 100%; overflow-x: auto; }
|
|
92
|
-
.tcr-segment { height: 28px; padding: 0 12px; border: 0; border-radius: 7px; background: none; color: var(--ta-secondary); font: inherit; font-size: 13px; white-space: nowrap; cursor: pointer; transition: background 140ms, color 140ms; }
|
|
93
|
-
.tcr-segment[aria-pressed='true'] { background: var(--ta-raised); color: var(--ta-text); box-shadow: 0 1px 2px rgba(0,0,0,.12); }
|
|
94
|
-
.tcr-select { display: flex; align-items: center; justify-content: space-between; gap: 8px; text-align: left; cursor: pointer; }
|
|
95
|
-
.tcr-select > span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
96
|
-
.tcr-select svg { flex: none; color: var(--ta-muted); }
|
|
97
|
-
.tcr-select:disabled { cursor: default; opacity: .6; }
|
|
98
|
-
.tcr-popup { position: fixed; z-index: 80; display: flex; flex-direction: column; min-width: 260px; max-height: 360px; border: 1px solid var(--ta-border-muted); border-radius: 10px; background: var(--ta-raised); box-shadow: var(--ta-shadow-pop); overflow: hidden; animation: tcr-in 120ms var(--ta-ease); }
|
|
99
|
-
.tcr-search { display: flex; align-items: center; gap: 8px; flex: none; padding: 0 10px; border-bottom: 1px solid var(--ta-border-muted); color: var(--ta-muted); }
|
|
100
|
-
.tcr-search input { flex: 1; min-width: 0; height: 36px; border: 0; background: none; color: var(--ta-text); font: inherit; font-size: 13.5px; outline: none; }
|
|
101
|
-
.tcr-options { flex: 1; min-height: 0; overflow-y: auto; padding: 4px; }
|
|
102
|
-
.tcr-option-group { padding: 8px 8px 4px; color: var(--ta-muted); font-size: 11.5px; font-weight: 500; }
|
|
103
|
-
.tcr-option { display: flex; align-items: center; gap: 6px; width: 100%; min-height: 30px; padding: 0 8px 0 4px; border: 0; border-radius: 6px; background: none; color: var(--ta-text); font: inherit; font-size: 13px; text-align: left; cursor: pointer; }
|
|
104
|
-
.tcr-option[data-active='true'] { background: var(--ta-hover); }
|
|
105
|
-
.tcr-option-check { display: inline-flex; flex: none; width: 16px; color: var(--ta-green); }
|
|
106
|
-
.tcr-option-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
107
|
-
.tcr-option-detail { flex: none; margin-left: auto; max-width: 45%; overflow: hidden; color: var(--ta-muted); font-size: 11.5px; text-overflow: ellipsis; white-space: nowrap; }
|
|
108
|
-
.tcr-option-empty { padding: 16px 8px; color: var(--ta-muted); font-size: 13px; text-align: center; }
|
|
109
|
-
.tcr-preview { color: var(--ta-green); font-size: 12.5px; }
|
|
110
|
-
.tcr-preview.tcr-bad { color: var(--ta-red); }
|
|
111
|
-
.tcr-folder { display: flex; align-items: center; gap: 10px; text-align: left; cursor: pointer; }
|
|
112
|
-
.tcr-folder-path { flex: 1; min-width: 0; overflow: hidden; color: var(--ta-muted); font-size: 12px; text-overflow: ellipsis; white-space: nowrap; direction: rtl; text-align: right; }
|
|
113
|
-
.tcr-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px 16px; }
|
|
114
|
-
.tcr-wide { grid-column: 1 / -1; }
|
|
115
|
-
.tcr-actions { display: flex; justify-content: flex-end; gap: 8px; }
|
|
116
|
-
.tcr-reply { margin: 4px 0; padding: 10px 12px; border: 1px solid var(--ta-border-muted); border-radius: 10px; background: var(--ta-card); font-size: 13px; }
|
|
117
|
-
.tcr-reply-head { display: flex; align-items: center; gap: 8px; color: var(--ta-secondary); }
|
|
118
|
-
.tcr-reply-head svg { color: var(--ta-green); }
|
|
119
|
-
.tcr-reply-head .tcr-link { margin-left: auto; }
|
|
120
|
-
.tcr-reply-task { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
|
|
121
|
-
.tcr-reply .tcr-prompt-text { margin: 8px 0 0; }
|
|
122
|
-
.tcr-reply-error { color: var(--ta-red); }
|
|
123
|
-
.tcr-help { margin: 8px 0 0; font-family: var(--ta-mono); font-size: 12px; line-height: 1.6; white-space: pre-wrap; color: var(--ta-secondary); }
|
|
124
|
-
@media (prefers-reduced-motion: reduce) { .tcr-clock-hand, .tcr-running, .tcr-pulse { animation: none; } }
|
|
125
|
-
`;
|
|
126
|
-
|
|
127
|
-
export function injectStyle(): () => void {
|
|
128
|
-
const element = document.createElement('style');
|
|
129
|
-
element.dataset.taPackage = 'ta-cron';
|
|
130
|
-
element.textContent = STYLE;
|
|
131
|
-
document.head.appendChild(element);
|
|
132
|
-
return () => element.remove();
|
|
133
|
-
}
|
package/web-src/task-store.ts
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import type { TaExtensionHost } from '@celljs/ta-protocol';
|
|
2
|
-
import { type FinishedRun, Keys, Limits, NAME, type Run, type Task } from './cron-protocol';
|
|
3
|
-
|
|
4
|
-
export type StoreHost = Pick<TaExtensionHost, 'getExtensionData' | 'setExtensionData'>;
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* The tasks and their runs, in the data of the package (one JSON file of the agent directory): the tasks
|
|
8
|
-
* under one key, the runs of each task under one of its own, the newest first. One write at a time, each
|
|
9
|
-
* after the one before, so that nothing written is lost.
|
|
10
|
-
*/
|
|
11
|
-
export class TaskStore {
|
|
12
|
-
protected writing: Promise<unknown> = Promise.resolve();
|
|
13
|
-
|
|
14
|
-
constructor(protected readonly host: StoreHost) {}
|
|
15
|
-
|
|
16
|
-
async tasks(): Promise<Task[]> {
|
|
17
|
-
const value = await this.host.getExtensionData(NAME, Keys.TASKS);
|
|
18
|
-
return Array.isArray(value) ? value.filter(TaskStore.isTask) : [];
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
saveTasks(tasks: Task[]): Promise<void> {
|
|
22
|
-
const copy = tasks.map(task => ({ ...task }));
|
|
23
|
-
return this.queue(() => this.host.setExtensionData(NAME, Keys.TASKS, copy));
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
async runs(taskId: string): Promise<Run[]> {
|
|
27
|
-
await this.writing.catch(() => undefined);
|
|
28
|
-
const value = await this.host.getExtensionData(NAME, Keys.runs(taskId));
|
|
29
|
-
return Array.isArray(value) ? (value as Run[]) : [];
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/** Adds the run, or replaces the one of its id; keeps the newest RUNS_KEPT. */
|
|
33
|
-
saveRun(run: Run): Promise<void> {
|
|
34
|
-
const copy = { ...run };
|
|
35
|
-
return this.queue(async () => {
|
|
36
|
-
const value = await this.host.getExtensionData(NAME, Keys.runs(run.taskId));
|
|
37
|
-
const runs = Array.isArray(value) ? (value as Run[]) : [];
|
|
38
|
-
const index = runs.findIndex(other => other.id === run.id);
|
|
39
|
-
const next = index >= 0 ? runs.map(other => (other.id === run.id ? copy : other)) : [copy, ...runs];
|
|
40
|
-
await this.host.setExtensionData(NAME, Keys.runs(run.taskId), next.slice(0, Limits.RUNS_KEPT));
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
removeRuns(taskId: string): Promise<void> {
|
|
45
|
-
return this.queue(() => this.host.setExtensionData(NAME, Keys.runs(taskId), null));
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
finished(finished: FinishedRun): Promise<void> {
|
|
49
|
-
return this.queue(() => this.host.setExtensionData(NAME, Keys.LATEST, finished));
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/** Once every write so far has landed. */
|
|
53
|
-
async flush(): Promise<void> {
|
|
54
|
-
await this.writing.catch(() => undefined);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
protected queue(write: () => Promise<void>): Promise<void> {
|
|
58
|
-
const next = this.writing.catch(() => undefined).then(write);
|
|
59
|
-
this.writing = next;
|
|
60
|
-
return next;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
protected static isTask(value: unknown): value is Task {
|
|
64
|
-
const task = value as Partial<Task> | undefined;
|
|
65
|
-
return typeof task?.id === 'string' && typeof task.prompt === 'string' && typeof task.schedule === 'object' && typeof task.cwd === 'string';
|
|
66
|
-
}
|
|
67
|
-
}
|