Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/job-trigger-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ The available value beneath a slug is:
| Custom webhook | Complete submitted JSON delivery, plus `provider`, `event`, `delivery_id` |
| Job lifecycle | `source_job_id`, `source_run_id`, `final_state`, `transitioned_at`, `observation_destination` |

Every trigger returned by `GET /api/v1/jobs` and `GET /api/v1/jobs/:id` (and the corresponding MCP `list_jobs` and `get_job` tools) includes a `reflection` object. It contains the stable `slug`, trigger kind/provider, whether the result is dynamic, and a flat `paths` list with nested path, basic value type, description, and occasional example. The Job editor consumes this same canonical contract for Mustache autocomplete. A custom webhook, or any webhook with a replacing handler, reports `dynamic: true` and an explicit `*`/`unknown` fallback because arbitrary handler output cannot be inferred safely.

Manual invocation accepts `{ "prompt": "...", "idempotencyKey": "..." }`. Parameter defaults and invocation, schedule, or webhook parameter overrides are not accepted.

Run inspection returns `context` as the complete persisted object and `invocation.trigger_id` as the trigger that fired. Parameter-era stored Jobs are migrated: old context and parameters are retained beneath `legacy_context` and `legacy_parameters` in the corresponding trigger context. They are not used as global template variables.
Expand Down
3 changes: 2 additions & 1 deletion packages/app/src/tenant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { JOB_SCHEMA, renderJobPrompt } from "./job-domain";
import { normalizeExecutionState, type RunHandle } from "./execution";
import { catchUpOccurrence, nextOccurrence, validateScheduleConfig, type ScheduleConfig } from "./schedule";
import { adaptWebhook, publicWebhookConfig, validateWebhookHandler, type WebhookProvider, type WebhookTriggerConfig } from "./webhook-trigger";
import { reflectTriggerContext } from "./trigger-context";

export { DEFAULT_CONTEXT_TEMPLATE, linearTicketPrompt, renderContextTemplate } from "./linear-source";

Expand Down Expand Up @@ -400,7 +401,7 @@ export class Tenant extends DurableObject<Env> {
try { config = JSON.parse(String(trigger.config)); } catch { /* present malformed state safely */ }
if (trigger.kind === "webhook") config = publicWebhookConfig(config as WebhookTriggerConfig);
const runtime = trigger.kind === "schedule" ? this.one("SELECT next_run_at,last_triggered_at FROM schedule_state WHERE trigger_id=?", trigger.id) as Row | undefined : undefined;
return { id: trigger.id, slug: trigger.slug, kind: trigger.kind, enabled: Boolean(trigger.enabled), config, createdAt: trigger.created_at, updatedAt: trigger.updated_at,
return { id: trigger.id, slug: trigger.slug, kind: trigger.kind, enabled: Boolean(trigger.enabled), config, reflection: reflectTriggerContext({ ...trigger, config }), createdAt: trigger.created_at, updatedAt: trigger.updated_at,
activity: { count: Number(activity?.count ?? 0), lastReceivedAt: activity?.last_received_at ?? null },
...(runtime ? { nextRunAt: runtime.next_run_at == null ? null : new Date(Number(runtime.next_run_at)).toISOString(), lastTriggeredAt: runtime.last_triggered_at ?? null } : {}) };
});
Expand Down
62 changes: 62 additions & 0 deletions packages/app/src/trigger-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
export type ContextValueType = "string" | "number" | "boolean" | "object" | "array" | "unknown";
export interface ContextPath { path: string; type: ContextValueType; description: string; example?: unknown; }
export interface TriggerContextReflection { slug: string; kind: string; provider?: string; dynamic: boolean; paths: ContextPath[]; }

const commonWebhook: ContextPath[] = [
{ path: "provider", type: "string", description: "Normalized webhook provider name." },
{ path: "event", type: "string", description: "Provider event name." },
{ path: "delivery_id", type: "string", description: "Stable delivery identifier used for deduplication." },
];

export const triggerContextCatalog: Record<string, ContextPath[]> = {
manual: [{ path: "prompt", type: "string", description: "Prompt supplied by the manual invocation.", example: "Review the failing build" }],
schedule: [
{ path: "scheduled_at", type: "string", description: "Scheduled occurrence as an ISO 8601 timestamp.", example: "2026-09-16T09:00:00.000Z" },
{ path: "cron", type: "string", description: "Configured cron expression.", example: "0 9 * * *" },
{ path: "timezone", type: "string", description: "Configured IANA timezone.", example: "UTC" },
],
linear: [...commonWebhook,
{ path: "issue", type: "object", description: "Complete Linear issue from the authenticated delivery." },
{ path: "issue.id", type: "string", description: "Linear issue UUID." },
{ path: "issue.identifier", type: "string", description: "Human-readable issue identifier.", example: "GEN-2019" },
{ path: "issue.title", type: "string", description: "Issue title." },
{ path: "issue.description", type: "string", description: "Issue description, when present." },
{ path: "issue.url", type: "string", description: "Canonical Linear issue URL." },
{ path: "issue.state.name", type: "string", description: "Current workflow state name." },
{ path: "issue.project.name", type: "string", description: "Project name, when present." },
],
github: [...commonWebhook,
{ path: "action", type: "string", description: "GitHub webhook action." },
{ path: "pull_request", type: "object", description: "Complete GitHub pull request payload." },
{ path: "pull_request.number", type: "number", description: "Pull request number.", example: 42 },
{ path: "pull_request.title", type: "string", description: "Pull request title." },
{ path: "pull_request.html_url", type: "string", description: "Pull request URL." },
{ path: "pull_request.base.ref", type: "string", description: "Base branch name." },
{ path: "pull_request.head.ref", type: "string", description: "Head branch name." },
{ path: "repository.full_name", type: "string", description: "Owner and repository name.", example: "acme/widgets" },
],
cloudflareTail: [...commonWebhook,
{ path: "outcome", type: "string", description: "Sanitized Worker invocation outcome." },
{ path: "scriptName", type: "string", description: "Worker script that emitted the Tail event." },
{ path: "eventTimestamp", type: "number", description: "Event timestamp in milliseconds." },
{ path: "logs", type: "array", description: "Sanitized console log records." },
{ path: "exceptions", type: "array", description: "Sanitized exception records." },
],
custom: [...commonWebhook, { path: "*", type: "unknown", description: "Additional submitted JSON fields are dynamic and may be nested." }],
jobLifecycle: [
{ path: "source_job_id", type: "string", description: "Job whose run reached a final state." },
{ path: "source_run_id", type: "string", description: "Run that reached a final state." },
{ path: "final_state", type: "string", description: "Observed final state: succeeded, failed, or stopped." },
{ path: "transitioned_at", type: "string", description: "State transition as an ISO 8601 timestamp." },
{ path: "observation_destination", type: "string", description: "Backend observation URL, when available." },
],
};

export function reflectTriggerContext(trigger: { slug?: unknown; kind?: unknown; config?: any }): TriggerContextReflection {
const kind = String(trigger.kind ?? ""), provider = kind === "webhook" ? String(trigger.config?.provider ?? "custom") : undefined;
const key = provider ?? kind, handler = kind === "webhook" && typeof trigger.config?.handlerCode === "string" && trigger.config.handlerCode.trim().length > 0;
return {
slug: String(trigger.slug ?? ""), kind, ...(provider ? { provider } : {}), dynamic: key === "custom" || handler,
paths: handler ? [{ path: "*", type: "unknown", description: "The custom handler replaces context with a dynamic JSON object; inspect the handler contract." }] : (triggerContextCatalog[key] ?? [{ path: "*", type: "unknown", description: "Context shape is not known." }]),
};
}
Loading