The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | MAX_WEBHOOK_DELIVERY_AGE_MINUTES, |
| 3 | MAX_WEBHOOK_FILTERS, |
| 4 | ScheduledTaskWebhookFilter, |
| 5 | type ScheduledTaskWebhookFilterOp, |
| 6 | } from "@t3tools/contracts"; |
| 7 | import * as Option from "effect/Option"; |
| 8 | import * as Schema from "effect/Schema"; |
| 9 | import { mFallback, msg } from "gt-i18n"; |
| 10 | import type { MFunctionType } from "gt-i18n/types"; |
| 11 | |
| 12 | export const DEFAULT_WEBHOOK_PROMPT = "Handle this webhook:\n{{body}}"; |
| 13 | |
| 14 | /** Blank means "no limit"; undefined means the input is not a valid limit, which blocks saving. */ |
| 15 | export function parseMaxDeliveryAge(value: string): number | null | undefined { |
| 16 | if (value.trim() === "") return null; |
| 17 | const minutes = Number(value.trim()); |
| 18 | return Number.isInteger(minutes) && minutes > 0 && minutes <= MAX_WEBHOOK_DELIVERY_AGE_MINUTES |
| 19 | ? minutes |
| 20 | : undefined; |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * One editable filter row. `in` takes a comma-separated list, which is |
| 25 | * lossless because list values can't contain commas; `exists` ignores the value. |
| 26 | */ |
| 27 | export interface WebhookFilterDraft { |
| 28 | /** Stable row identity for the form; never sent to the server. */ |
| 29 | readonly id: string; |
| 30 | readonly path: string; |
| 31 | readonly op: ScheduledTaskWebhookFilterOp; |
| 32 | readonly value: string; |
| 33 | } |
| 34 | |
| 35 | export const WEBHOOK_FILTER_OPS: ReadonlyArray<{ |
| 36 | readonly op: ScheduledTaskWebhookFilterOp; |
| 37 | readonly label: string; |
| 38 | }> = [ |
| 39 | { op: "equals", label: msg("equals") }, |
| 40 | { op: "notEquals", label: msg("does not equal") }, |
| 41 | { op: "in", label: msg("is one of") }, |
| 42 | { op: "startsWith", label: msg("starts with") }, |
| 43 | { op: "exists", label: msg("exists") }, |
| 44 | ]; |
| 45 | |
| 46 | let lastWebhookFilterDraftId = 0; |
| 47 | const nextWebhookFilterDraftId = () => `filter-${++lastWebhookFilterDraftId}`; |
| 48 | |
| 49 | export function emptyWebhookFilterDraft(): WebhookFilterDraft { |
| 50 | return { id: nextWebhookFilterDraftId(), path: "", op: "equals", value: "" }; |
| 51 | } |
| 52 | |
| 53 | /** By id, so a removed row's identity never passes to the row after it. */ |
| 54 | export function removeWebhookFilterDraft( |
| 55 | drafts: ReadonlyArray<WebhookFilterDraft>, |
| 56 | id: string, |
| 57 | ): ReadonlyArray<WebhookFilterDraft> { |
| 58 | return drafts.filter((draft) => draft.id !== id); |
| 59 | } |
| 60 | |
| 61 | export function webhookFilterDrafts( |
| 62 | filters: ReadonlyArray<ScheduledTaskWebhookFilter> | undefined, |
| 63 | ): ReadonlyArray<WebhookFilterDraft> { |
| 64 | return (filters ?? []).map((filter) => ({ |
| 65 | id: nextWebhookFilterDraftId(), |
| 66 | path: filter.path, |
| 67 | op: filter.op, |
| 68 | value: |
| 69 | filter.op === "exists" ? "" : filter.op === "in" ? filter.value.join(", ") : filter.value, |
| 70 | })); |
| 71 | } |
| 72 | |
| 73 | const decodeFilter = Schema.decodeUnknownOption(ScheduledTaskWebhookFilter); |
| 74 | |
| 75 | /** |
| 76 | * Checks each row with the server's own schema, so a draft that parses here |
| 77 | * also saves. The error names the first row that would not. |
| 78 | */ |
| 79 | export function parseWebhookFilterDrafts( |
| 80 | drafts: ReadonlyArray<WebhookFilterDraft>, |
| 81 | resolve: MFunctionType = mFallback, |
| 82 | ): { readonly filters: ReadonlyArray<ScheduledTaskWebhookFilter> } | { readonly error: string } { |
| 83 | if (drafts.length > MAX_WEBHOOK_FILTERS) { |
| 84 | return { error: resolve(msg("Use at most {max} filters.", { max: MAX_WEBHOOK_FILTERS })) }; |
| 85 | } |
| 86 | const filters: ScheduledTaskWebhookFilter[] = []; |
| 87 | for (const [index, draft] of drafts.entries()) { |
| 88 | const path = draft.path.trim(); |
| 89 | const filter = decodeFilter( |
| 90 | draft.op === "exists" |
| 91 | ? { path, op: draft.op } |
| 92 | : draft.op === "in" |
| 93 | ? { |
| 94 | path, |
| 95 | op: draft.op, |
| 96 | value: draft.value |
| 97 | .split(",") |
| 98 | .map((item) => item.trim()) |
| 99 | .filter((item) => item !== ""), |
| 100 | } |
| 101 | : { path, op: draft.op, value: draft.value }, |
| 102 | ); |
| 103 | if (Option.isNone(filter)) { |
| 104 | return { |
| 105 | error: |
| 106 | draft.op === "exists" |
| 107 | ? resolve( |
| 108 | msg("Filter {number} needs a path under body, headers or query.", { |
| 109 | number: index + 1, |
| 110 | }), |
| 111 | ) |
| 112 | : draft.op === "in" |
| 113 | ? resolve( |
| 114 | msg( |
| 115 | "Filter {number} needs a path under body, headers or query, and at least one value.", |
| 116 | { number: index + 1 }, |
| 117 | ), |
| 118 | ) |
| 119 | : resolve( |
| 120 | msg("Filter {number} needs a path under body, headers or query, and a value.", { |
| 121 | number: index + 1, |
| 122 | }), |
| 123 | ), |
| 124 | }; |
| 125 | } |
| 126 | filters.push(filter.value); |
| 127 | } |
| 128 | return { filters }; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Whether a form's filter rows differ from the ones it opened with. A save |
| 133 | * leaves `filters` out unless they did, so editing only the title can't |
| 134 | * overwrite filters another device changed in the meantime. |
| 135 | */ |
| 136 | export function webhookFilterDraftsChanged( |
| 137 | saved: ReadonlyArray<WebhookFilterDraft>, |
| 138 | current: ReadonlyArray<WebhookFilterDraft>, |
| 139 | ): boolean { |
| 140 | return ( |
| 141 | saved.length !== current.length || |
| 142 | saved.some( |
| 143 | (filter, index) => |
| 144 | filter.path !== current[index]?.path || |
| 145 | filter.op !== current[index]?.op || |
| 146 | (filter.op !== "exists" && filter.value !== current[index]?.value), |
| 147 | ) |
| 148 | ); |
| 149 | } |