logic-sdk 0.7.1 → 0.7.3

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 (99) hide show
  1. package/README.md +9 -10
  2. package/dist/adapters/cloudflare-queue.d.ts +15 -0
  3. package/dist/adapters/cloudflare-queue.js +58 -0
  4. package/dist/adapters/drizzle-pg.d.ts +280 -0
  5. package/dist/adapters/drizzle-pg.js +78 -0
  6. package/dist/adapters/drizzle-sqlite.d.ts +273 -0
  7. package/dist/adapters/drizzle-sqlite.js +203 -0
  8. package/dist/adapters/postgres.d.ts +33 -0
  9. package/dist/adapters/postgres.js +129 -0
  10. package/dist/adapters/sqlite.d.ts +13 -0
  11. package/dist/adapters/sqlite.js +108 -0
  12. package/dist/adapters/stored-jobs.d.ts +89 -0
  13. package/dist/adapters/stored-jobs.js +370 -0
  14. package/dist/ai.d.ts +1 -32
  15. package/dist/ai.js +1 -40
  16. package/dist/cloudflare.d.ts +2 -55
  17. package/dist/cloudflare.js +2 -85
  18. package/dist/core/in-process.d.ts +9 -0
  19. package/dist/core/in-process.js +37 -0
  20. package/dist/core/jobs.d.ts +63 -0
  21. package/dist/core/jobs.js +514 -0
  22. package/dist/core/registry.d.ts +156 -0
  23. package/dist/core/registry.js +515 -0
  24. package/dist/core/testing.d.ts +75 -0
  25. package/dist/core/testing.js +232 -0
  26. package/dist/{types.d.ts → core/types.d.ts} +289 -64
  27. package/dist/drizzle/pg.d.ts +3 -236
  28. package/dist/drizzle/pg.js +2 -67
  29. package/dist/drizzle/sqlite.d.ts +3 -242
  30. package/dist/drizzle/sqlite.js +2 -194
  31. package/dist/fetch.d.ts +1 -19
  32. package/dist/fetch.js +1 -30
  33. package/dist/hosts/cloudflare.d.ts +43 -0
  34. package/dist/hosts/cloudflare.js +40 -0
  35. package/dist/hosts/node.d.ts +36 -0
  36. package/dist/hosts/node.js +103 -0
  37. package/dist/index.d.ts +5 -122
  38. package/dist/index.js +4 -492
  39. package/dist/integrations/ai.d.ts +32 -0
  40. package/dist/integrations/ai.js +40 -0
  41. package/dist/integrations/fetch.d.ts +19 -0
  42. package/dist/integrations/fetch.js +30 -0
  43. package/dist/integrations/orpc.d.ts +22 -0
  44. package/dist/integrations/orpc.js +37 -0
  45. package/dist/node.d.ts +1 -26
  46. package/dist/node.js +1 -76
  47. package/dist/orpc.d.ts +1 -21
  48. package/dist/orpc.js +1 -37
  49. package/dist/postgres.d.ts +2 -28
  50. package/dist/postgres.js +1 -82
  51. package/dist/sqlite.d.ts +2 -9
  52. package/dist/sqlite.js +1 -65
  53. package/dist/testing.d.ts +1 -75
  54. package/dist/testing.js +1 -227
  55. package/package.json +2 -12
  56. package/skills/logic-sdk/SKILL.md +6 -5
  57. package/src/adapters/cloudflare-queue.ts +65 -0
  58. package/src/adapters/drizzle-pg.ts +117 -0
  59. package/src/adapters/drizzle-sqlite.ts +241 -0
  60. package/src/adapters/postgres.ts +136 -0
  61. package/src/adapters/sqlite.ts +116 -0
  62. package/src/adapters/stored-jobs.ts +463 -0
  63. package/src/ai.ts +1 -62
  64. package/src/cloudflare.ts +2 -107
  65. package/src/core/in-process.ts +38 -0
  66. package/src/core/jobs.ts +548 -0
  67. package/src/core/registry.ts +719 -0
  68. package/src/core/testing.ts +309 -0
  69. package/src/{types.ts → core/types.ts} +280 -60
  70. package/src/drizzle/pg.ts +3 -110
  71. package/src/drizzle/sqlite.ts +3 -236
  72. package/src/fetch.ts +1 -31
  73. package/src/hosts/cloudflare.ts +55 -0
  74. package/src/hosts/node.ts +126 -0
  75. package/src/index.ts +17 -652
  76. package/src/integrations/ai.ts +63 -0
  77. package/src/integrations/fetch.ts +31 -0
  78. package/src/integrations/orpc.ts +47 -0
  79. package/src/node.ts +1 -99
  80. package/src/orpc.ts +1 -46
  81. package/src/postgres.ts +2 -90
  82. package/src/sqlite.ts +2 -74
  83. package/src/testing.ts +1 -305
  84. package/dist/jobs.d.ts +0 -44
  85. package/dist/jobs.js +0 -260
  86. package/dist/pg-boss.d.ts +0 -53
  87. package/dist/pg-boss.js +0 -213
  88. package/dist/stored-jobs.d.ts +0 -64
  89. package/dist/stored-jobs.js +0 -188
  90. package/src/jobs.ts +0 -307
  91. package/src/pg-boss.ts +0 -241
  92. package/src/stored-jobs.ts +0 -260
  93. /package/dist/{errors.d.ts → core/errors.d.ts} +0 -0
  94. /package/dist/{errors.js → core/errors.js} +0 -0
  95. /package/dist/{schedule.d.ts → core/schedule.d.ts} +0 -0
  96. /package/dist/{schedule.js → core/schedule.js} +0 -0
  97. /package/dist/{types.js → core/types.js} +0 -0
  98. /package/src/{errors.ts → core/errors.ts} +0 -0
  99. /package/src/{schedule.ts → core/schedule.ts} +0 -0
@@ -0,0 +1,156 @@
1
+ import type { Call, CallMetaOf, Config, ConfigOf, Constructor, Cron, CronCtx, DatabaseAdapter, DatabaseOptions, DbOf, EventDef, In, JobsOptions, Listener, ListenerCtx, ListenerMetaOf, Out, OwnModules, RegisteredCron, RegisteredEvent, RegisteredFn, RegisteredListener, Registry, RegistryOptions, Schema, ScopeOf, ServicesOf } from "./types.ts";
2
+ type TypedOptions<C extends Config, Mods extends Record<string, object>, T> = Omit<RegistryOptions<Mods>, "wrap" | "jobs"> & {
3
+ /** Background work: `jobs: { adapter: databaseQueue(), scope: { store, load } }`. Default: in-process, scope as-is (nothing waits). */
4
+ jobs?: JobsOptions<ScopeOf<C>, T>;
5
+ /** Wraps every call, including ones that fail validation or authorization: logging, tracing, audit. */
6
+ wrap?: (call: Call<CallMetaOf<C>>, next: () => Promise<unknown>) => Promise<unknown>;
7
+ } & ({} extends ServicesOf<C> ? {
8
+ services?: ServicesOf<C>;
9
+ } : {
10
+ services: ServicesOf<C>;
11
+ }) & ({} extends ConfigOf<C> ? {
12
+ config?: ConfigOf<C>;
13
+ } : {
14
+ config: ConfigOf<C>;
15
+ }) & ([DbOf<C>] extends [never] ? {
16
+ database?: undefined;
17
+ } : {
18
+ database: DatabaseOptions<DatabaseAdapter<unknown, DbOf<C>>>;
19
+ }) & {
20
+ modules: OwnModules<C, Mods>;
21
+ };
22
+ /** Keys `Config` doesn't know are errors, so a typo like `servises` can't be silently ignored. */
23
+ type OnlyKnownKeys<C> = {
24
+ [K in Exclude<keyof C, keyof Config>]: never;
25
+ };
26
+ /**
27
+ * Typed constructors for your app:
28
+ *
29
+ * ```ts
30
+ * export const { query, mutation, action, event, listener, createRegistry } = init<Config>();
31
+ * ```
32
+ */
33
+ export declare function init<C extends Config & OnlyKnownKeys<C>>(): {
34
+ /** Create the registry once at startup: `createRegistry({ modules, database, services })`. */
35
+ createRegistry: <const Mods extends Record<string, object>, T = ScopeOf<C>>(options: TypedOptions<C, Mods, T>) => Registry<ScopeOf<C>, Mods, ConfigOf<C>>;
36
+ /** Reads data. Can run other queries. No writes, no events, no services. */
37
+ query: Constructor<C, "query">;
38
+ /** Changes data inside one transaction. Can run queries and mutations (same transaction) and emit events. */
39
+ mutation: Constructor<C, "mutation">;
40
+ /** Talks to the outside world (payments, email, AI). No transaction; each mutation it runs commits on its own. */
41
+ action: Constructor<C, "action">;
42
+ /** Something that happened. Emitted by mutations, delivered after commit. */
43
+ event: <S extends Schema>(def: {
44
+ /**
45
+ * Stable id, local to its module: in a `notifications` module, `id: "send"` is `notifications.send`. Jobs store it,
46
+ * so set it before you rename the export and queued jobs keep finding this code. Defaults to the export name.
47
+ */
48
+ id?: string;
49
+ payload: S;
50
+ }) => EventDef<In<S>, Out<S>>;
51
+ /**
52
+ * Work on a schedule, as `scope` (a cron has no caller): a handler, like a listener's, run when the clock says. Export
53
+ * it from a module and call `registry.jobs.tick()` every minute (or from your platform's cron trigger). Must be
54
+ * idempotent: a retry runs the whole handler again.
55
+ * `cron({ schedule: "0 9 * * MON", scope: { kind: "system", job: "reports.weekly" }, async handler(ctx) { await ctx.run(send) } })`.
56
+ */
57
+ cron: <const Sc extends ScopeOf<C>>(def: CronDef<C, Sc>) => Cron<C>;
58
+ /**
59
+ * React to an event after commit, like an action. Must be idempotent.
60
+ * `listener({ event: () => bookings.confirmed, async handler(ctx, payload) {…} })`
61
+ */
62
+ listener: <O>(def: {
63
+ /**
64
+ * Stable id, local to its module: in a `notifications` module, `id: "send"` is `notifications.send`. Jobs store it,
65
+ * so set it before you rename the export and queued jobs keep finding this code. Defaults to the export name.
66
+ */
67
+ id?: string;
68
+ /** The event it reacts to, as a thunk so modules can import each other's barrels without cycles. */
69
+ event: () => EventDef<unknown, O>;
70
+ /**
71
+ * Tries in total, the first included, like Laravel's `$tries`. Default 1: a retry repeats side effects, so opt in.
72
+ * Once they're used, the job is `failed` (kept by `databaseQueue` and `outbox`, retried with `registry.jobs.retry(id)`).
73
+ */
74
+ attempts?: number;
75
+ /** Seconds after the event before the first try. Needs an adapter that holds jobs: `inProcess()` is refused. */
76
+ delay?: number;
77
+ /**
78
+ * Whether this event gets a job for this listener, like Laravel's `shouldQueue($event)`: false, and nothing is
79
+ * stored, run or counted. From the payload the handler gets (parsed), at commit, so sync and from the event only:
80
+ * a condition on the database belongs in the handler, since it can change before the job runs. A throw fails the
81
+ * mutation, as a bad payload does.
82
+ */
83
+ when?: (payload: O) => boolean;
84
+ /**
85
+ * Jobs with the same key run one at a time, in the order their events were emitted, like Laravel's message groups
86
+ * on SQS FIFO: `key: (m) => m.conversationId`. Waiting spends no attempt; a try backing off holds the key, a job
87
+ * that failed for good frees it. Per listener. Needs an adapter that runs a key's jobs in order: others are refused.
88
+ * Inside its handler, run functions with `ctx.run`: calling the registry directly for the same key waits on itself.
89
+ */
90
+ key?: (payload: O) => string;
91
+ /**
92
+ * While a job with the same value hasn't started, later ones are dropped, like Laravel's
93
+ * `ShouldBeUniqueUntilProcessing`: `unique: (p) => p.productId`. **It drops events, so the handler must read
94
+ * current state, never the payload's data**: for "X changed, recompute X". Once the job starts, the next event
95
+ * queues a new one.
96
+ */
97
+ unique?: (payload: O) => string;
98
+ /**
99
+ * Seconds a try may run, like Laravel's `$timeout`. Past it, `ctx.signal` aborts, no new `ctx.run` starts, and
100
+ * the try counts as failed (retried while `attempts` are left). JavaScript can't stop code mid-flight: a mutation
101
+ * already running still commits.
102
+ */
103
+ timeout?: number;
104
+ /**
105
+ * Its queue, one of `jobs.queues`: `queue: "mail"`. Default `"default"`. A worker takes its queues in the order it
106
+ * lists them: `work(registry, { queues: ["mail", "default"] })`.
107
+ */
108
+ queue?: string;
109
+ meta?: ListenerMetaOf<C>;
110
+ handler: (ctx: ListenerCtx<C, O>, payload: O) => Promise<unknown>;
111
+ /**
112
+ * Runs once its last try has failed, with the handler's ctx and payload, and the error: undo or flag what the
113
+ * handler started (`await ctx.run(orders.flagReceipt, { orderId })`). Not in a transaction: run mutations. At most
114
+ * once (a crash right after the failure skips it), and not for a job that can't run anymore (its payload or event
115
+ * changed, or `scope.load` returned null). A throw is reported to `onError`; the job stays failed.
116
+ */
117
+ failed?: (ctx: ListenerCtx<C, O>, payload: O, error: unknown) => Promise<unknown>;
118
+ }) => Listener<O, C>;
119
+ };
120
+ /** `cron({...})`: its handler's `ctx.scope` is the scope it declares, so a policy-free read of it is typed. */
121
+ type CronDef<C extends Config, Sc> = {
122
+ /**
123
+ * Stable id, local to its module, like every definer's. Defaults to the export name. Jobs store it, so set it before you
124
+ * rename the export.
125
+ */
126
+ id?: string;
127
+ /** Five fields: minute hour day-of-month month day-of-week. `"*\/15 * * * *"`, `"0 9 * * MON"`. */
128
+ schedule: string;
129
+ /** IANA name, like `"Europe/Madrid"`. Default UTC. Matched on the wall clock, like Laravel: mind DST at 1 to 3 am. */
130
+ timezone?: string;
131
+ /** Who it runs as: a cron has no caller. Every function the handler runs checks its policy against it. */
132
+ scope: Sc;
133
+ /** Tries in total, like a listener's. Default 1: a retry runs the whole handler again. */
134
+ attempts?: number;
135
+ /** Its queue, one of `jobs.queues`, like a listener's. Default `"default"`. */
136
+ queue?: string;
137
+ meta?: ListenerMetaOf<C>;
138
+ /** The work, like a listener's handler: an action's ctx, plus `ctx.cron` (`scheduledAt`, `attempt`). */
139
+ handler: (ctx: CronCtx<C, Sc>) => Promise<unknown>;
140
+ /** Runs once its last try has failed, like a listener's `failed`: the handler's ctx, and the error. */
141
+ failed?: (ctx: CronCtx<C, Sc>, error: unknown) => Promise<unknown>;
142
+ };
143
+ /** Create the registry once at startup. Prefer the `createRegistry` returned by `init()`: it's typed with your config. */
144
+ export declare function createRegistry<S extends object = object, const Mods extends Record<string, object> = Record<string, object>>(options: RegistryOptions<Mods>): Registry<S, Mods, unknown>;
145
+ /**
146
+ * What `createRegistry` finds in your modules, without a database or services: every function, event, listener and
147
+ * cron with its id. For tools (the CLI, docs generators) that inspect an app without running it.
148
+ */
149
+ export declare function describeModules(modules: Record<string, object>): {
150
+ ids: Map<object, string>;
151
+ functions: RegisteredFn[];
152
+ events: RegisteredEvent[];
153
+ listeners: RegisteredListener[];
154
+ crons: RegisteredCron[];
155
+ };
156
+ export {};