@vite-hub/schedule 0.1.0 → 0.3.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.
Files changed (54) hide show
  1. package/README.md +114 -96
  2. package/dist/console-wEhr0LWn.d.ts +330 -0
  3. package/dist/definition.d.ts +6 -2
  4. package/dist/definition.js +47 -1
  5. package/dist/due-BVMRQ93v.js +144 -0
  6. package/dist/durability.d.ts +14 -0
  7. package/dist/durability.js +23 -0
  8. package/dist/error-diagnostics-DSkCJC_L.js +266 -0
  9. package/dist/error-diagnostics-Mpeks7J0.js +166 -0
  10. package/dist/index.d.ts +59 -5
  11. package/dist/index.js +27 -2
  12. package/dist/invariant-DolT9EEx.js +22 -0
  13. package/dist/{kv-storage-Bv9ocZT-.d.ts → kv-storage-C0KZZB6v.d.ts} +2 -2
  14. package/dist/{kv-storage-DWinmLLy.js → kv-storage-iJChCS5A.js} +2 -2
  15. package/dist/object-Da1zyPCR.js +11 -0
  16. package/dist/{operations-tNUdj3G4.d.ts → operation-definition-8gOe-ubD.d.ts} +37 -15
  17. package/dist/{operations-gk2qzhYV.js → operations-DwZWFIUX.js} +75 -22
  18. package/dist/operations.d.ts +27 -4
  19. package/dist/operations.js +127 -730
  20. package/dist/os.d.ts +167 -0
  21. package/dist/os.js +1659 -0
  22. package/dist/runtime/console.d.ts +12 -0
  23. package/dist/runtime/console.js +85 -0
  24. package/dist/runtime/kv.d.ts +1 -1
  25. package/dist/runtime/kv.js +1 -1
  26. package/dist/runtime/process.d.ts +32 -5
  27. package/dist/runtime/process.js +401 -413
  28. package/dist/runtime/state.d.ts +2 -9
  29. package/dist/runtime/state.js +2 -2
  30. package/dist/runtime/static.d.ts +28 -2
  31. package/dist/runtime/static.js +2 -2
  32. package/dist/runtime.d.ts +34 -32
  33. package/dist/runtime.js +6 -5
  34. package/dist/started-BuGmnus4.js +201 -0
  35. package/dist/started-Bx7MDbrl.js +146 -0
  36. package/dist/{execute-BJrgNgQ0.js → started-errors-p7cu_rI7.js} +69 -20
  37. package/dist/{state-DSxmHPn3.js → state-B-Fuyfb4.js} +42 -22
  38. package/dist/state-Do2wV4Tx.d.ts +49 -0
  39. package/dist/{static-6g82xguw.js → static-DQwzzvPg.js} +63 -42
  40. package/dist/store-Df0-fzCl.d.ts +53 -0
  41. package/dist/time-zone-SBldrUgt.js +59 -0
  42. package/dist/{types-CHdV9FQU.d.ts → types-Cw5LjPuc.d.ts} +62 -8
  43. package/dist/vite.d.ts +255 -2
  44. package/dist/vite.js +5876 -2
  45. package/dist/{wait-until-BmIqRo4x.js → wait-until-Dy4Bn3ZN.js} +26 -1
  46. package/package.json +19 -24
  47. package/dist/definition-CMiOg1lg.js +0 -111
  48. package/dist/error-diagnostics-A_8Kp-48.js +0 -116
  49. package/dist/error-diagnostics-in7mif5s.js +0 -45
  50. package/dist/nuxt.d.ts +0 -17
  51. package/dist/nuxt.js +0 -27
  52. package/dist/store-QAOd8bxm.d.ts +0 -11
  53. package/dist/vite-BAVyZ8hZ.d.ts +0 -474
  54. package/dist/vite-BJwZkMrn.js +0 -5193
@@ -75,7 +75,7 @@ declare namespace StandardSchemaV1 {
75
75
  type InferOutput<Schema extends StandardTypedV1> = StandardTypedV1.InferOutput<Schema>;
76
76
  }
77
77
  //#endregion
78
- //#region ../internal/src/operations.d.ts
78
+ //#region ../internal/src/operation-definition.d.ts
79
79
  /** `read` never changes state. `write` changes state. `destructive` deletes or publishes. */
80
80
  type OperationKind = "read" | "write" | "destructive";
81
81
  /** The signed-in user of a call. */
@@ -98,7 +98,9 @@ interface OperationAccess {
98
98
  readonly mode: string;
99
99
  /**
100
100
  * The sections that the call may use. `"all"` when no section filter applies. An entry is a section id (`"kv"`, every
101
- * operation of the section) or `"<section>:read"` (only the operations that read).
101
+ * operation of the section), `"<section>:read"` (only the operations that read), or `"<section>:<scope>"` (only the
102
+ * calls of a scoped operation for that scope, such as `"blob:media"` for one Blob store). `"<section>:<scope>:read"`
103
+ * allows only the reads of that scope.
102
104
  */
103
105
  readonly sections: "all" | readonly string[];
104
106
  readonly user?: OperationUser;
@@ -116,6 +118,12 @@ interface OperationContext {
116
118
  env?: Record<string, unknown>;
117
119
  waitUntil?: (task: Promise<unknown>) => void;
118
120
  request?: Request;
121
+ /**
122
+ * The request event of the HTTP request that carries the call, when there is one: the Request of the Console route,
123
+ * with its Standard Context. Operations hand it to app hooks that read the request's own session, such as `identify`
124
+ * of Comments.
125
+ */
126
+ event?: unknown;
119
127
  /** Operations set a success status here, such as 202 for an accepted call. */
120
128
  response?: {
121
129
  status?: number;
@@ -138,9 +146,16 @@ interface OperationDefinition<Input, Output> {
138
146
  description: string;
139
147
  /**
140
148
  * Console section that grants access, such as `content`. `false`: any Console user. A function reads the section
141
- * from the input, for operations that serve several sections.
149
+ * from the input, for operations that serve several sections. Section ids cannot contain `:`; it is reserved for
150
+ * scope and read-only grants. A function that returns an invalid id denies the call.
142
151
  */
143
152
  section: string | false | ((input: Input) => string | undefined);
153
+ /**
154
+ * The part of a string `section` that one call uses, such as the Blob store that the input names. The call then needs
155
+ * the section (every scope) or `"<section>:<scope>"` (only this scope). A scope that is empty, is `read`, or has a
156
+ * `:` needs the whole section.
157
+ */
158
+ scope?: (input: Input) => string | undefined;
144
159
  /** `read` never changes state. `write` changes state. `destructive` deletes or publishes; the CLI asks for `--yes`, MCP marks `destructiveHint`, agents require approval by default. */
145
160
  kind: OperationKind;
146
161
  /** Standard Schema (valibot in this repo). Its Standard JSON Schema builds CLI flags and the MCP input schema. */
@@ -149,19 +164,14 @@ interface OperationDefinition<Input, Output> {
149
164
  cli?: {
150
165
  /** Defaults: namespace = section, command = verb. */
151
166
  namespace?: string;
167
+ /** A space groups commands: `jobs list` and `jobs show` run as `vitehub <namespace> jobs list` and `jobs show`. */
152
168
  command?: string;
153
169
  /** Input keys read from positional arguments, in order. */
154
170
  positional?: string[];
155
171
  /** Human output, written verbatim. Without it the CLI prints JSON. `--json` always prints JSON. */
156
172
  format?: (output: Output) => string;
157
- /** `vitehub <namespace>` runs this command when no command is named, such as `vitehub usage`. */
173
+ /** `vitehub <namespace>` runs this command when no command is named. */
158
174
  default?: boolean;
159
- /**
160
- * Route of the app that takes a signed dispatch of this operation, such as `/_vitehub/schedules/run`. With `--url`
161
- * and a dispatch key in the environment (`VITEHUB_DISPATCH_KEY`), the command signs the input and posts it there, so
162
- * a CI job needs no Console credentials. Without a key it calls the RPC.
163
- */
164
- dispatch?: string;
165
175
  };
166
176
  /** Largest request body in bytes. It can raise the 64 KiB default but not lower it. */
167
177
  maxRequestBytes?: number;
@@ -170,8 +180,16 @@ interface OperationDefinition<Input, Output> {
170
180
  * confirmation, and agents run it without approval. Without it, every call of a `destructive` operation asks.
171
181
  */
172
182
  readOnlyWhen?: (input: Input) => boolean;
173
- /** `false`: MCP does not offer the operation as a tool, such as an editor transport that only the Console uses. */
174
- mcp?: false;
183
+ /**
184
+ * How MCP offers the operation as a tool. `false`: MCP does not offer it, such as an editor transport that only the
185
+ * Console uses. `title`: the short title that clients show, instead of the name in words. `format`: the text of a
186
+ * result for the Agent, such as a list of lines. The result stays the structured content. Without `format`, the text
187
+ * is the result as JSON.
188
+ */
189
+ mcp?: false | {
190
+ title?: string;
191
+ format?: (output: NoInfer<Output>) => string;
192
+ };
175
193
  /** `false`: the operation never reads `context.user`, so the Console skips the session lookup for it. */
176
194
  user?: false;
177
195
  /**
@@ -182,24 +200,28 @@ interface OperationDefinition<Input, Output> {
182
200
  run: (input: Input, context: OperationContext) => Output | Promise<Output>;
183
201
  }
184
202
  /** An operation of any input and output, as a registry holds it. */
185
- interface AnyOperation extends Omit<OperationDefinition<unknown, unknown>, "cli" | "input" | "output" | "readOnlyWhen" | "run" | "section"> {
203
+ interface AnyOperation extends Omit<OperationDefinition<unknown, unknown>, "cli" | "input" | "mcp" | "output" | "readOnlyWhen" | "run" | "scope" | "section"> {
186
204
  readOnlyWhen?: (input: never) => boolean;
187
205
  cli?: {
188
206
  namespace?: string;
189
207
  command?: string;
190
208
  default?: boolean;
191
- dispatch?: string;
192
209
  positional?: string[];
193
210
  format?: (output: never) => string;
194
211
  };
212
+ mcp?: false | {
213
+ title?: string;
214
+ format?: (output: never) => string;
215
+ };
195
216
  input: StandardSchemaV1;
196
217
  output?: StandardSchemaV1;
197
218
  run: (input: never, context: OperationContext) => unknown;
219
+ scope?: (input: never) => string | undefined;
198
220
  section: string | false | ((input: never) => string | undefined);
199
221
  }
200
222
  /** The contribution of one plugin: operations, or a module that exports them. */
201
223
  interface OperationContributionEntry {
202
- /** Module the app imports, such as `@vite-hub/content/operations`. */
224
+ /** The absolute path of the module that generated code imports, such as `ownModule(import.meta.url, "operations")`. */
203
225
  module: string;
204
226
  /** Named export that holds an operation or an array of operations. Defaults to `default`. */
205
227
  export?: string;
@@ -1,6 +1,8 @@
1
+ import { t as scheduleErrorDiagnostics } from "./error-diagnostics-Mpeks7J0.js";
2
+ import { a as runScheduleFrom } from "./started-errors-p7cu_rI7.js";
3
+ import { r as getScheduleRunStore } from "./state-B-Fuyfb4.js";
4
+ import { i as startSchedule, r as listSchedules, t as cancelSchedule } from "./started-Bx7MDbrl.js";
1
5
  import { t as redactInspectionText } from "./inspect-DTfAo5gS.js";
2
- import { i as runSchedule } from "./execute-BJrgNgQ0.js";
3
- import { t as getScheduleRunStore } from "./state-DSxmHPn3.js";
4
6
  //#region src/runtime/operations.ts
5
7
  function isoDate(value) {
6
8
  if (value === void 0) return;
@@ -43,6 +45,7 @@ function summarizeManualScheduleRun(run) {
43
45
  ...error ? { error } : {},
44
46
  id: run.id,
45
47
  scheduleId: run.scheduleId,
48
+ scheduledAt: isoDate(run.scheduledAt) ?? "",
46
49
  ...startedAt ? { startedAt } : {},
47
50
  status: run.status
48
51
  };
@@ -74,22 +77,36 @@ async function listScheduleRuns(scheduleId, options = {}) {
74
77
  async function listScheduleRunAttempts(runId) {
75
78
  return [...await getScheduleRunStore().listAttempts(runId)].sort((left, right) => new Date(left.startedAt).getTime() - new Date(right.startedAt).getTime()).map(summarizeScheduleRunAttempt);
76
79
  }
77
- /** Raised for the failures of a Schedule operation. `status` and `code` reach every surface. */
78
- var ScheduleOperationError = class extends Error {
79
- code;
80
- fix;
81
- status;
82
- constructor(status, code, message, fix) {
83
- super(message);
84
- this.code = code;
85
- this.status = status;
86
- if (fix) this.fix = fix;
87
- }
88
- };
80
+ /** Gives an operation error its HTTP status and the redacted message that the CLI and the Console report. */
81
+ function withStatus(error, status, fix) {
82
+ return Object.defineProperties(error, {
83
+ ...fix && !Reflect.get(error, "fix") ? { fix: {
84
+ configurable: true,
85
+ enumerable: false,
86
+ value: fix,
87
+ writable: true
88
+ } } : {},
89
+ status: {
90
+ configurable: true,
91
+ enumerable: false,
92
+ value: status,
93
+ writable: true
94
+ },
95
+ statusMessage: {
96
+ configurable: true,
97
+ enumerable: false,
98
+ value: redactInspectionText(error.message),
99
+ writable: true
100
+ }
101
+ });
102
+ }
103
+ const errorFix = { SCHEDULE_INVALID_SCHEDULED_AT: "Pass `at` as an ISO 8601 date-time with a time zone, such as 2026-01-31T23:59:00Z or 2026-01-31T23:59:00+01:00." };
89
104
  const errorStatus = {
90
105
  SCHEDULE_DEFINITION_NOT_FOUND: 404,
91
106
  SCHEDULE_HISTORY_INCOMPLETE: 503,
107
+ SCHEDULE_INVALID_SCHEDULED_AT: 400,
92
108
  SCHEDULE_MANUAL_RUN_DISABLED: 403,
109
+ SCHEDULE_OPERATION_FAILED: 500,
93
110
  SCHEDULE_RUN_NOT_FOUND: 404
94
111
  };
95
112
  /** Gives a Schedule error the status and the redacted message that every surface reports. */
@@ -97,11 +114,10 @@ async function attempt(work) {
97
114
  try {
98
115
  return await work();
99
116
  } catch (error) {
100
- if (error instanceof ScheduleOperationError) throw error;
101
117
  const code = Reflect.get(Object(error), "code");
102
118
  const status = String(code) === code ? errorStatus[code] : void 0;
103
- if (status && error instanceof Error) throw new ScheduleOperationError(status, String(code), redactInspectionText(error.message));
104
- throw new ScheduleOperationError(500, "OPERATION_FAILED", "Schedule operation failed.");
119
+ if (status && error instanceof Error) throw withStatus(error, status, errorFix[String(code)]);
120
+ throw withStatus(scheduleErrorDiagnostics.SCHEDULE_OPERATION_FAILED({ cause: error }), 500);
105
121
  }
106
122
  }
107
123
  async function runScheduleRuns(input) {
@@ -110,7 +126,7 @@ async function runScheduleRuns(input) {
110
126
  async function runScheduleAttempts(input) {
111
127
  return await attempt(async () => {
112
128
  const run = await getScheduleRunStore().getRun(input.id);
113
- if (!run) throw new ScheduleOperationError(404, "SCHEDULE_RUN_NOT_FOUND", "Schedule Run was not found.", "List the runs of the Schedule with `vitehub schedule runs <name>` and pass one of their ids.");
129
+ if (!run) throw scheduleErrorDiagnostics.SCHEDULE_RUN_NOT_FOUND();
114
130
  return {
115
131
  attempts: await listScheduleRunAttempts(input.id),
116
132
  run: summarizeScheduleRun(run)
@@ -120,12 +136,49 @@ async function runScheduleAttempts(input) {
120
136
  /** The manual Static Schedule Definitions that the Console installed. Only `console.invoke` installs them. */
121
137
  const consoleSchedulesKey = Symbol.for("vitehub.console.schedules");
122
138
  /**
123
- * Runs a Static Schedule Definition that sets `manual: true`. The registry holds only those definitions, and it is empty
139
+ * Runs a Static Schedule Definition that sets `manual: true`, as if its cron fired at `at` (default: now). The registry holds only those definitions, and it is empty
124
140
  * unless Console invocation is enabled, so anything else is "not available".
125
141
  */
126
142
  async function runScheduleNow(input, registry = Reflect.get(globalThis, consoleSchedulesKey) ?? {}) {
127
- if (!Object.hasOwn(registry, input.name)) throw new ScheduleOperationError(404, "SCHEDULE_DEFINITION_NOT_FOUND", "Schedule run is not available. Set manual: true on the Schedule Definition and enable Console invocation.", "Set `manual: true` in defineSchedule() and keep `console.invoke` on (it is on in development).");
128
- return await attempt(async () => ({ run: summarizeManualScheduleRun(await runSchedule(input.name, { registry })) }));
143
+ if (!Object.hasOwn(registry, input.name)) throw withStatus(scheduleErrorDiagnostics.SCHEDULE_DEFINITION_NOT_FOUND(), 404);
144
+ return await attempt(async () => ({ run: summarizeManualScheduleRun(await runScheduleFrom(registry, input.name, { scheduledAt: input.at })) }));
145
+ }
146
+ function summarizeStartedSchedule(schedule) {
147
+ return {
148
+ ...schedule.at ? { at: schedule.at.toISOString() } : {},
149
+ createdAt: schedule.createdAt.toISOString(),
150
+ ...schedule.cron === void 0 ? {} : { cron: schedule.cron },
151
+ id: schedule.id,
152
+ ...schedule.payload === void 0 ? {} : { payload: schedule.payload },
153
+ name: schedule.name,
154
+ ...schedule.timeZone === void 0 ? {} : { timeZone: schedule.timeZone }
155
+ };
156
+ }
157
+ const startedErrorStatus = {
158
+ SCHEDULE_INVALID_OPTIONS: 400,
159
+ SCHEDULE_NOT_FOUND: 404,
160
+ SCHEDULE_NOT_STARTABLE: 404,
161
+ SCHEDULE_STORE_FAILED: 503,
162
+ SCHEDULE_STORE_MISSING: 503
163
+ };
164
+ function operationError(error) {
165
+ return withStatus(error, startedErrorStatus[error.code] ?? 500);
166
+ }
167
+ async function runScheduleList(input) {
168
+ const [error, schedules] = await listSchedules(input.name === void 0 ? {} : { name: input.name });
169
+ if (error) throw operationError(error);
170
+ return { schedules: schedules.map(summarizeStartedSchedule) };
171
+ }
172
+ async function runScheduleStart(input) {
173
+ const { name, ...options } = input;
174
+ const [error, schedule] = await startSchedule(name, options);
175
+ if (error) throw operationError(error);
176
+ return { schedule: summarizeStartedSchedule(schedule) };
177
+ }
178
+ async function runScheduleCancel(input) {
179
+ const [error, schedule] = await cancelSchedule(input.id);
180
+ if (error) throw operationError(error);
181
+ return { schedule: summarizeStartedSchedule(schedule) };
129
182
  }
130
183
  //#endregion
131
- export { listScheduleRunAttempts, listScheduleRuns, runScheduleAttempts, runScheduleNow, runScheduleRuns, summarizeScheduleRun, summarizeScheduleRunAttempt };
184
+ export { listScheduleRunAttempts, listScheduleRuns, runScheduleAttempts, runScheduleCancel, runScheduleList, runScheduleNow, runScheduleRuns, runScheduleStart, summarizeScheduleRun, summarizeScheduleRunAttempt };
@@ -1,5 +1,5 @@
1
- import { h as ScheduleRunStatus, l as ScheduleRunAttemptStatus } from "./types-CHdV9FQU.js";
2
- import { t as AnyOperation } from "./operations-tNUdj3G4.js";
1
+ import { h as ScheduleRunStatus, l as ScheduleRunAttemptStatus } from "./types-Cw5LjPuc.js";
2
+ import { t as AnyOperation } from "./operation-definition-8gOe-ubD.js";
3
3
  //#region src/runtime/operations.d.ts
4
4
  /** Error of a run or attempt. The stack is not included. */
5
5
  interface ScheduleRunErrorSummary {
@@ -37,6 +37,8 @@ interface ManualScheduleRunSummary {
37
37
  error?: ScheduleRunErrorSummary;
38
38
  id: string;
39
39
  scheduleId: string;
40
+ /** The fire time that the handler received. */
41
+ scheduledAt: string;
40
42
  startedAt?: string;
41
43
  status: ScheduleRunStatus;
42
44
  }
@@ -53,9 +55,30 @@ interface ScheduleAttemptsResult {
53
55
  interface ScheduleRunResult {
54
56
  run: ManualScheduleRunSummary;
55
57
  }
58
+ /** Serializable view of one started Schedule, as `vitehub schedule list` prints it. */
59
+ interface StartedScheduleSummary {
60
+ at?: string;
61
+ createdAt: string;
62
+ cron?: string;
63
+ id: string;
64
+ name: string;
65
+ payload?: unknown;
66
+ timeZone?: string;
67
+ }
68
+ /** Result of `schedule.list`. */
69
+ interface ScheduleListResult {
70
+ schedules: StartedScheduleSummary[];
71
+ }
72
+ /** Result of `schedule.start` and `schedule.cancel`. */
73
+ interface StartedScheduleResult {
74
+ schedule: StartedScheduleSummary;
75
+ }
56
76
  //#endregion
57
77
  //#region src/operations.d.ts
58
- /** The `schedule.*` operations: runs, attempts, and run. The Console, `vitehub schedule`, and agents run them. */
78
+ /**
79
+ * The `schedule.*` operations: runs, attempts, run, list, start, and cancel. The Console, `vitehub schedule`, and agents
80
+ * run them.
81
+ */
59
82
  export declare const scheduleOperations: readonly AnyOperation[];
60
83
  //#endregion
61
- export type { ScheduleAttemptsResult, ScheduleRunResult, ScheduleRunsResult };
84
+ export type { ScheduleAttemptsResult, ScheduleListResult, ScheduleRunResult, ScheduleRunsResult, StartedScheduleResult };