@trayai/helix-sdk 0.92.0 → 0.93.0

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 (3) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +41 -3
  3. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -9,6 +9,18 @@ general availability, minor versions may include breaking changes.
9
9
  The SDK and [`@trayai/helix-cli`](https://www.npmjs.com/package/@trayai/helix-cli) are
10
10
  released together and share a version — install matching versions.
11
11
 
12
+ ## 0.93.0
13
+
14
+ ### Added
15
+
16
+ - **Scheduled (cron) functions.** Files under `functions/_scheduled/` can default-export
17
+ `defineSchedule({ cron, timezone?, handler })` to run on a schedule instead of an HTTP
18
+ route. `cron` is a 5-field standard expression; `timezone` is an IANA name (default UTC).
19
+ The handler receives a `ScheduleContext` — the core `ctx` surface plus `ctx.trigger`
20
+ (`scheduledAt`, `firedAt`, `isManual`) and none of the request-derived fields. Schedules
21
+ fire on the deployed platform; `helix deploy` registers them. `ScheduleContext` and
22
+ `ScheduleTrigger` are exported. See the README's "Scheduled functions" section.
23
+
12
24
  ## 0.92.0
13
25
 
14
26
  ### Changed
package/README.md CHANGED
@@ -12,6 +12,7 @@ The runtime SDK for **Helix** — a TypeScript-first platform for building integ
12
12
  - [The `ctx` object](#the-ctx-object)
13
13
  - [Authenticated HTTP](#authenticated-http)
14
14
  - [Capability modules](#capability-modules)
15
+ - [Scheduled functions](#scheduled-functions)
15
16
  - [Authentication](#authentication)
16
17
  - [Configuration](#configuration)
17
18
  - [Logging](#logging)
@@ -68,7 +69,7 @@ export default defineFunction(async (ctx) => {
68
69
 
69
70
  ## The `ctx` object
70
71
 
71
- Every handler receives a `ctx` (`TrayContext`). You can also reach the current context anywhere in the async call tree — including shared helpers — with `getContext()`, so there's no need to thread `ctx` through your code.
72
+ Every HTTP handler receives a `ctx` (`TrayContext`); scheduled handlers receive a `ScheduleContext` (see [Scheduled functions](#scheduled-functions)). You can also reach the current context anywhere in the async call tree — including shared helpers — with `getContext()`, so there's no need to thread `ctx` through your code.
72
73
 
73
74
  | Property | Description |
74
75
  |----------|-------------|
@@ -134,6 +135,43 @@ const file = await ctx.files().get('temp/a.json'); // { data, metadata } | null
134
135
 
135
136
  > `list` returns the immediate children of a prefix.
136
137
 
138
+ ## Scheduled functions
139
+
140
+ `functions/_scheduled/` is a reserved namespace for schedule definitions: every module file in it runs on a cron schedule instead of an HTTP route and must default-export `defineSchedule`. Don't create normal HTTP functions there — files in this directory never route:
141
+
142
+ ```typescript
143
+ // functions/_scheduled/daily-report.ts
144
+ import { defineSchedule } from '@trayai/helix-sdk';
145
+
146
+ export default defineSchedule({
147
+ cron: '0 6 * * *', // 5-field standard cron
148
+ timezone: 'Europe/London', // IANA timezone name; defaults to UTC
149
+ handler: async (ctx) => {
150
+ ctx.log.info('daily report', { firedAt: ctx.trigger.firedAt });
151
+ },
152
+ });
153
+ ```
154
+
155
+ - **`cron`** is a 5-field standard expression (minute, hour, day-of-month, month, day-of-week). An expression that constrains both day-of-month and day-of-week is rejected at build time — leave one of them `*`.
156
+ - **`timezone`** must be an IANA name (`"Europe/London"`); offset forms (`"+05:00"`) are rejected. Omit it for UTC.
157
+ - The handler receives a `ScheduleContext`: the core `ctx` surface (`ctx.http`, `ctx.http.authed()`, `ctx.kv()`, `ctx.files()`, `ctx.log`, `ctx.config`, `ctx.state`, and the invocation identifiers) plus `ctx.trigger` (a `ScheduleTrigger`):
158
+
159
+ | Property | Description |
160
+ |----------|-------------|
161
+ | `ctx.trigger.type` | Always `'schedule'` |
162
+ | `ctx.trigger.scheduledAt` | When the cron said the run should fire (`Date`) |
163
+ | `ctx.trigger.firedAt` | When it actually executed (`Date`) |
164
+ | `ctx.trigger.isManual` | `true` when the run was triggered manually rather than by the schedule |
165
+
166
+ There is no request and no user: the request-derived fields (`ctx.input`, `ctx.method`, `ctx.path`, `ctx.headers`, `ctx.status()`, `ctx.error()`, `ctx.request`, `ctx.identity`) do not exist on a schedule context. Return nothing — there is no response; throwing an error fails the run.
167
+
168
+ Things to know:
169
+
170
+ - Schedules fire **on the deployed platform**. The local dev server does not fire them; `helix deploy` registers the schedules present at deploy time.
171
+ - Overlapping runs are possible — write handlers to tolerate running concurrently.
172
+ - Files and directories starting with `_` below `_scheduled/` are treated as helpers, not schedules — the only files that belong in the directory besides schedule definitions.
173
+ - `getContext()` works inside schedule handlers; shared code can detect a scheduled run by checking for `ctx.trigger`.
174
+
137
175
  ## Authentication
138
176
 
139
177
  Authenticated features (`ctx.http.authed`, `ctx.kv`, `ctx.files`) require you to be logged in during local development:
@@ -218,10 +256,10 @@ The mock context is a Proxy: touching an always-present field (`http`, `db`, `lo
218
256
 
219
257
  | Import | Contents |
220
258
  |--------|----------|
221
- | `@trayai/helix-sdk` | `defineFunction`, `defineConfig`, `getContext`, `loadConfig`, `requireWorkspaceId`, `createConfigService`, and the public types |
259
+ | `@trayai/helix-sdk` | `defineFunction`, `defineSchedule`, `defineConfig`, `getContext`, `loadConfig`, `requireWorkspaceId`, `createConfigService`, and the public types |
222
260
  | `@trayai/helix-sdk/testing` | `runWithContext` |
223
261
 
224
- Key exported types include `TrayContext`, `TrayConfig`, `LoadedTrayConfig`, `HttpClient`, `KeyValueStore`, `FileSystemClient`, `Logger`, `ConfigService`, and `WorkspaceIdRequiredError`.
262
+ Key exported types include `TrayContext`, `ScheduleContext`, `ScheduleTrigger`, `TrayConfig`, `LoadedTrayConfig`, `HttpClient`, `KeyValueStore`, `FileSystemClient`, `Logger`, `ConfigService`, and `WorkspaceIdRequiredError`.
225
263
 
226
264
  ## Tech stack
227
265
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trayai/helix-sdk",
3
- "version": "0.92.0",
3
+ "version": "0.93.0",
4
4
  "description": "Helix SDK runtime — defineFunction, ctx, defineConfig, authed HTTP",
5
5
  "license": "Apache-2.0",
6
6
  "author": "trayio",