@feltdb/feltkit 0.3.17 → 0.3.19

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 (188) hide show
  1. package/dist/app.d.ts +246 -13
  2. package/dist/app.js +215 -6
  3. package/dist/app.js.map +1 -1
  4. package/dist/appboundry/build.d.ts +70 -0
  5. package/dist/appboundry/build.js +62 -2
  6. package/dist/appboundry/build.js.map +1 -1
  7. package/dist/appboundry/guest.js +57 -3
  8. package/dist/appboundry/guest.js.map +1 -1
  9. package/dist/appboundry/host.js +7 -0
  10. package/dist/appboundry/host.js.map +1 -1
  11. package/dist/authorize.js +1 -0
  12. package/dist/authorize.js.map +1 -1
  13. package/dist/cli/compose.d.ts +31 -0
  14. package/dist/cli/compose.js +274 -0
  15. package/dist/cli/compose.js.map +1 -0
  16. package/dist/cli/create.d.ts +201 -0
  17. package/dist/cli/create.js +1139 -0
  18. package/dist/cli/create.js.map +1 -0
  19. package/dist/cli/deploy-appboundry-self-hosted.d.ts +2 -0
  20. package/dist/cli/deploy-appboundry-self-hosted.js +1 -1
  21. package/dist/cli/deploy-appboundry-self-hosted.js.map +1 -1
  22. package/dist/cli/deploy-appboundry.js +3 -0
  23. package/dist/cli/deploy-appboundry.js.map +1 -1
  24. package/dist/cli/deploy.d.ts +1 -0
  25. package/dist/cli/deploy.js +66 -6
  26. package/dist/cli/deploy.js.map +1 -1
  27. package/dist/cli/domain.d.ts +174 -0
  28. package/dist/cli/domain.js +1119 -0
  29. package/dist/cli/domain.js.map +1 -0
  30. package/dist/cli/main.d.ts +2 -0
  31. package/dist/cli/main.js +141 -7
  32. package/dist/cli/main.js.map +1 -1
  33. package/dist/cli/shell.d.ts +7 -0
  34. package/dist/cli/shell.js +59 -0
  35. package/dist/cli/shell.js.map +1 -0
  36. package/dist/client/index.d.ts +192 -5
  37. package/dist/client/index.js +301 -28
  38. package/dist/client/index.js.map +1 -1
  39. package/dist/component.d.ts +174 -0
  40. package/dist/component.js +77 -0
  41. package/dist/component.js.map +1 -0
  42. package/dist/composition/catalog.d.ts +3 -0
  43. package/dist/composition/catalog.js +217 -0
  44. package/dist/composition/catalog.js.map +1 -0
  45. package/dist/composition/contract.d.ts +165 -0
  46. package/dist/composition/contract.js +12 -0
  47. package/dist/composition/contract.js.map +1 -0
  48. package/dist/composition/execute.d.ts +110 -0
  49. package/dist/composition/execute.js +350 -0
  50. package/dist/composition/execute.js.map +1 -0
  51. package/dist/composition/generate.d.ts +105 -0
  52. package/dist/composition/generate.js +274 -0
  53. package/dist/composition/generate.js.map +1 -0
  54. package/dist/composition/plan.d.ts +131 -0
  55. package/dist/composition/plan.js +558 -0
  56. package/dist/composition/plan.js.map +1 -0
  57. package/dist/composition/profiles.d.ts +20 -0
  58. package/dist/composition/profiles.js +81 -0
  59. package/dist/composition/profiles.js.map +1 -0
  60. package/dist/composition/reconcile.d.ts +153 -0
  61. package/dist/composition/reconcile.js +282 -0
  62. package/dist/composition/reconcile.js.map +1 -0
  63. package/dist/composition/recover.d.ts +89 -0
  64. package/dist/composition/recover.js +407 -0
  65. package/dist/composition/recover.js.map +1 -0
  66. package/dist/composition/resolve.d.ts +2 -0
  67. package/dist/composition/resolve.js +208 -0
  68. package/dist/composition/resolve.js.map +1 -0
  69. package/dist/dashboard/assets/{index-DnIOVGF-.js → index-BrC1AtZ1.js} +5 -5
  70. package/dist/dashboard/index.html +1 -1
  71. package/dist/docs.d.ts +1 -1
  72. package/dist/docs.js +2 -0
  73. package/dist/docs.js.map +1 -1
  74. package/dist/index.d.ts +7 -3
  75. package/dist/index.js +2 -0
  76. package/dist/index.js.map +1 -1
  77. package/dist/react/auth.d.ts +131 -0
  78. package/dist/react/auth.js +830 -0
  79. package/dist/react/auth.js.map +1 -0
  80. package/dist/react/context.d.ts +8 -0
  81. package/dist/react/context.js +14 -0
  82. package/dist/react/context.js.map +1 -0
  83. package/dist/react/index.d.ts +3 -18
  84. package/dist/react/index.js +5 -13
  85. package/dist/react/index.js.map +1 -1
  86. package/dist/runtime/accounts.d.ts +33 -2
  87. package/dist/runtime/accounts.js +167 -27
  88. package/dist/runtime/accounts.js.map +1 -1
  89. package/dist/runtime/activity.d.ts +8 -1
  90. package/dist/runtime/activity.js.map +1 -1
  91. package/dist/runtime/audit.d.ts +43 -0
  92. package/dist/runtime/audit.js +52 -0
  93. package/dist/runtime/audit.js.map +1 -0
  94. package/dist/runtime/authority.d.ts +8 -1
  95. package/dist/runtime/authority.js +11 -2
  96. package/dist/runtime/authority.js.map +1 -1
  97. package/dist/runtime/challenges.d.ts +51 -0
  98. package/dist/runtime/challenges.js +121 -0
  99. package/dist/runtime/challenges.js.map +1 -0
  100. package/dist/runtime/changes.d.ts +22 -0
  101. package/dist/runtime/changes.js +51 -3
  102. package/dist/runtime/changes.js.map +1 -1
  103. package/dist/runtime/claims.d.ts +76 -0
  104. package/dist/runtime/claims.js +100 -0
  105. package/dist/runtime/claims.js.map +1 -0
  106. package/dist/runtime/context.d.ts +47 -5
  107. package/dist/runtime/contract.js +17 -3
  108. package/dist/runtime/contract.js.map +1 -1
  109. package/dist/runtime/data.d.ts +50 -4
  110. package/dist/runtime/data.js +113 -35
  111. package/dist/runtime/data.js.map +1 -1
  112. package/dist/runtime/escape-hatch.d.ts +37 -0
  113. package/dist/runtime/escape-hatch.js +76 -0
  114. package/dist/runtime/escape-hatch.js.map +1 -0
  115. package/dist/runtime/instance.js +2 -1
  116. package/dist/runtime/instance.js.map +1 -1
  117. package/dist/runtime/live.d.ts +48 -0
  118. package/dist/runtime/live.js +154 -0
  119. package/dist/runtime/live.js.map +1 -0
  120. package/dist/runtime/outbox.js +6 -0
  121. package/dist/runtime/outbox.js.map +1 -1
  122. package/dist/runtime/runtime.d.ts +103 -5
  123. package/dist/runtime/runtime.js +411 -41
  124. package/dist/runtime/runtime.js.map +1 -1
  125. package/dist/runtime/search.d.ts +54 -0
  126. package/dist/runtime/search.js +66 -0
  127. package/dist/runtime/search.js.map +1 -0
  128. package/dist/runtime/services-rules.d.ts +12 -0
  129. package/dist/runtime/services-rules.js +8 -0
  130. package/dist/runtime/services-rules.js.map +1 -0
  131. package/dist/runtime/services.js +5 -3
  132. package/dist/runtime/services.js.map +1 -1
  133. package/dist/runtime/sessions.d.ts +16 -2
  134. package/dist/runtime/sessions.js +40 -4
  135. package/dist/runtime/sessions.js.map +1 -1
  136. package/dist/runtime/tenancy.d.ts +198 -0
  137. package/dist/runtime/tenancy.js +397 -0
  138. package/dist/runtime/tenancy.js.map +1 -0
  139. package/dist/runtime/tenant-scope.d.ts +9 -0
  140. package/dist/runtime/tenant-scope.js +127 -0
  141. package/dist/runtime/tenant-scope.js.map +1 -0
  142. package/dist/runtime/transactions.d.ts +83 -3
  143. package/dist/runtime/transactions.js +53 -11
  144. package/dist/runtime/transactions.js.map +1 -1
  145. package/dist/server/admin.js +52 -0
  146. package/dist/server/admin.js.map +1 -1
  147. package/dist/server/components.d.ts +44 -0
  148. package/dist/server/components.js +212 -0
  149. package/dist/server/components.js.map +1 -0
  150. package/dist/server/index.d.ts +4 -1
  151. package/dist/server/index.js +324 -63
  152. package/dist/server/index.js.map +1 -1
  153. package/dist/server/mcp.js +6 -3
  154. package/dist/server/mcp.js.map +1 -1
  155. package/dist/server/project.d.ts +27 -0
  156. package/dist/server/project.js +63 -0
  157. package/dist/server/project.js.map +1 -1
  158. package/dist/server/public.d.ts +2 -0
  159. package/dist/server/public.js +2 -0
  160. package/dist/server/public.js.map +1 -1
  161. package/dist/server/session-http.d.ts +48 -0
  162. package/dist/server/session-http.js +116 -0
  163. package/dist/server/session-http.js.map +1 -0
  164. package/dist/server/tenancy.d.ts +40 -0
  165. package/dist/server/tenancy.js +191 -0
  166. package/dist/server/tenancy.js.map +1 -0
  167. package/dist/testing/index.d.ts +19 -0
  168. package/dist/testing/index.js +22 -2
  169. package/dist/testing/index.js.map +1 -1
  170. package/docs/AGENT-OPERATIONS.md +1 -0
  171. package/docs/APPBOUNDRY.md +11 -1
  172. package/docs/ARCHITECTURE.md +5 -3
  173. package/docs/AUTH.md +267 -0
  174. package/docs/COMPONENTS.md +91 -0
  175. package/docs/GUARANTEES.md +74 -7
  176. package/docs/README.md +4 -2
  177. package/docs/RESOLVER.md +4 -2
  178. package/docs/SECURITY.md +5 -4
  179. package/docs/TRANSACTIONS.md +98 -1
  180. package/docs/UPSTREAM-GAPS.md +2 -0
  181. package/package.json +15 -12
  182. package/skills/.claude-plugin/marketplace.json +1 -1
  183. package/skills/.claude-plugin/plugin.json +1 -1
  184. package/skills/feltkit-data/SKILL.md +6 -0
  185. package/skills/feltkit-services/SKILL.md +1 -1
  186. package/dist/runtime/account-links.d.ts +0 -19
  187. package/dist/runtime/account-links.js +0 -45
  188. package/dist/runtime/account-links.js.map +0 -1
package/dist/app.d.ts CHANGED
@@ -10,7 +10,8 @@ import type { DecisionRuntimeOption, ModelReferenceOption } from "./runtime/deci
10
10
  */
11
11
  import type { Schema } from "@appport/sdk";
12
12
  import { type Doc, type InferShape, type ShapeInput, type Simplify } from "./schema.js";
13
- import type { MutationCtx, QueryCtx } from "./runtime/context.js";
13
+ import type { AgentCtx, MutationCtx, QueryCtx } from "./runtime/context.js";
14
+ import { type ClaimDeclaration } from "./runtime/claims.js";
14
15
  declare const APP: unique symbol;
15
16
  declare const COLLECTION: unique symbol;
16
17
  declare const FUNCTION: unique symbol;
@@ -18,7 +19,7 @@ declare const FUNCTION: unique symbol;
18
19
  export interface EventDefinition<Payload = unknown> {
19
20
  readonly name: string;
20
21
  readonly payload: Schema<Payload>;
21
- /** Who may observe it: "authenticated" (default), "public", or a permission. */
22
+ /** Who may observe it: a permission, "authenticated" or "public". Always declared. */
22
23
  readonly access: Access;
23
24
  readonly description?: string;
24
25
  /** Type only. */
@@ -50,6 +51,13 @@ export interface AccountsOptions {
50
51
  roles: string[];
51
52
  /** `false` closes sign-up; existing accounts still sign in. Default open. */
52
53
  signUp?: boolean;
54
+ /** Sign in with an e-mail address and a password. Default true. */
55
+ password?: boolean;
56
+ /**
57
+ * Sign in with a link (and an 8-digit code) e-mailed to the address: no password. A new address
58
+ * gets an account when sign-up is open. Needs `email`.
59
+ */
60
+ magicLink?: boolean;
53
61
  /**
54
62
  * Account e-mail (verifying an address, resetting a password), delivered by AppPort Services'
55
63
  * notifications through Mailerport. Development keeps every message in a local inbox (printed
@@ -65,6 +73,50 @@ export interface AccountEmailOptions {
65
73
  identity?: string;
66
74
  /** "required": an account signs in only once its address is verified. Default "optional": a verification e-mail is sent and the session says whether the address is verified. */
67
75
  verify?: "required" | "optional";
76
+ /**
77
+ * Your own text for account e-mail, by purpose. Each receives `{ app, link, code, email,
78
+ * expiresInMinutes }` and returns `{ subject, text, html? }`. The link and code are computed when
79
+ * the message is delivered and are never stored.
80
+ */
81
+ templates?: Partial<Record<"verify" | "reset" | "magic-link" | "email-change", (input: import("./runtime/runtime.js").AccountMailInput) => {
82
+ subject: string;
83
+ text: string;
84
+ html?: string;
85
+ }>> & {
86
+ /** A workspace invitation (`auth.tenant`). */
87
+ invitation?: (input: import("./runtime/runtime.js").InvitationMailInput) => {
88
+ subject: string;
89
+ text: string;
90
+ html?: string;
91
+ };
92
+ };
93
+ }
94
+ /** `auth.tenant`. */
95
+ export interface TenantOptions {
96
+ /** What your app calls a tenant, for messages and the built-in screens. Default "workspace". */
97
+ name?: string;
98
+ /**
99
+ * The roles a member can hold, highest first (e.g. ["owner", "admin", "member", "guest"]). Each is
100
+ * one of your `roles`: in a session working in a tenant, the member holds that role's permissions
101
+ * there. The first is the tenant's top role (its creator gets it; a tenant always keeps one).
102
+ */
103
+ roles: readonly string[];
104
+ /**
105
+ * Typed claims each membership carries beside its role, set only through the server (a member manager or an
106
+ * invitation; never a request). A schema (`{ plan: s.enum(["free", "pro"]) }`) or a claim spec, plain data a
107
+ * composition record can hold (`{ plan: { type: "enum", values: ["free", "pro"] } }`; types enum, string, boolean,
108
+ * number, integer; an optional `default`). On every call each claim is classified at the boundary (verified, default,
109
+ * unavailable, invalid: runtime/claims.ts); `app.requireClaim` is the rule that refuses without one.
110
+ */
111
+ claims?: Record<string, ClaimDeclaration>;
112
+ /** Who may create a tenant: any signed-in person ("anyone", the default), or nobody through the API ("none"). */
113
+ create?: "anyone" | "none";
114
+ /** Invite people by e-mail with a role. Default true (7 days; `ttlMs` up to 30 days). */
115
+ invitations?: boolean | {
116
+ ttlMs?: number;
117
+ };
118
+ /** Let a person switch between their tenants. Default true. */
119
+ switching?: boolean;
68
120
  }
69
121
  /** A sign-in provider: its OAuth client id, and where its client secret is kept. */
70
122
  export interface ProviderClient {
@@ -122,6 +174,23 @@ export interface FeltKitOptions {
122
174
  authenticate?: (request: AuthenticateRequest) => PrincipalInput | null | Promise<PrincipalInput | null>;
123
175
  /** Session lifetime. Default 7 days. */
124
176
  sessionTtlMs?: number;
177
+ /**
178
+ * How browsers hold sessions. The browser client gets an HttpOnly cookie (`__Host-fk_session` over
179
+ * HTTPS), with a CSRF token on every unsafe request; CLI, API and service clients keep bearer tokens.
180
+ * cookie "auto" (default): Secure in production and over HTTPS. "secure" always, "insecure" never.
181
+ * trustProxy how many reverse proxies in front append X-Forwarded-For / set X-Forwarded-Proto.
182
+ * Default 0: those headers are ignored (a client could forge them).
183
+ */
184
+ session?: {
185
+ cookie?: "auto" | "secure" | "insecure";
186
+ trustProxy?: number;
187
+ };
188
+ /**
189
+ * Tenancy: people (and agents) belong to tenants — workspaces, organizations, teams — with a role
190
+ * in each, and a session works in one tenant at a time (chosen by the person, checked against the
191
+ * live membership on every request). See TenantOptions.
192
+ */
193
+ tenant?: TenantOptions;
125
194
  /**
126
195
  * Built-in accounts: people sign up and sign in to the app with an e-mail address and a
127
196
  * password, stored in the app's own FeltDB (only a scrypt hash of the password). A new
@@ -172,6 +241,15 @@ export interface FeltKitOptions {
172
241
  /** Per decision. Default 20 s. */
173
242
  decisionTimeoutMs?: number;
174
243
  };
244
+ /**
245
+ * Runtime components: application code that must run with the application but is not a function —
246
+ * a gateway for provider webhooks and WebSockets, for example. Each runs in the application's process
247
+ * on its port (`feltkit dev`, `feltkit start`, every `feltkit deploy` target that runs FeltKit), acts
248
+ * through the application's functions as its declared principal, and reports into /_feltkit/health:
249
+ * the application is not ready while a required component is not. See `defineComponent` and
250
+ * docs/COMPONENTS.md.
251
+ */
252
+ components?: Record<string, import("./component.js").ComponentDeclaration>;
175
253
  activity?: {
176
254
  /** What to record. Default: "all" in development/test, "writes" in production. */
177
255
  record?: "all" | "writes" | "errors";
@@ -223,6 +301,20 @@ export interface CollectionOptions {
223
301
  readonly name?: string;
224
302
  readonly fields: readonly string[];
225
303
  }>;
304
+ /**
305
+ * Records belong to a tenant (`auth.tenant`). FeltKit keeps `tenantId` (or `field`) set to the
306
+ * caller's current tenant on every insert and limits every read, update and delete to it: another
307
+ * tenant's records do not exist for the call. The field is added when the shape does not declare it.
308
+ */
309
+ tenant?: true | {
310
+ field: string;
311
+ };
312
+ /**
313
+ * String fields `ctx.db.search(collection, { text, … })` looks in: every word of the text must
314
+ * occur in one of them (case- and accent-insensitive). A bounded scan of the records matching the
315
+ * search's equality `where`; FeltDB has no text index yet (docs/UPSTREAM-GAPS.md).
316
+ */
317
+ search?: readonly string[];
226
318
  }
227
319
  export interface CollectionDefinition<S extends ShapeInput = ShapeInput> {
228
320
  readonly [COLLECTION]: true;
@@ -235,10 +327,22 @@ export interface CollectionDefinition<S extends ShapeInput = ShapeInput> {
235
327
  readonly name?: string;
236
328
  readonly fields: readonly string[];
237
329
  }>;
330
+ /** Tenant-scoped (see CollectionOptions.tenant): the field holding the tenant id. */
331
+ readonly tenant?: {
332
+ readonly field: string;
333
+ };
334
+ /** Searchable string fields (CollectionOptions.search). */
335
+ readonly search?: readonly string[];
238
336
  /** Type-only marker. */
239
337
  readonly $doc?: Doc<S>;
240
338
  }
241
339
  export type Access = "public" | "authenticated" | (string & {});
340
+ /**
341
+ * Every function, event and agent names who may use it. Deny by default: there is no implicit rule, so a
342
+ * caller gets nothing by being signed in unless the declaration says "authenticated" in so many words.
343
+ * Returns the access, or throws a TypeError that says what to write.
344
+ */
345
+ export declare function checkAccess(access: unknown, what: string): Access;
242
346
  export interface CapabilityExposure {
243
347
  /** AppPort capability name, e.g. "tasks.complete". */
244
348
  name: string;
@@ -324,10 +428,11 @@ export interface FunctionConfig<Kind extends FunctionKind, A extends ShapeInput,
324
428
  /** Output contract. Required when the function is exposed as a capability. */
325
429
  returns?: Schema<R>;
326
430
  /**
327
- * Who may call it: "public", "authenticated" (default), or a permission string such
328
- * as "tasks.write". Checked before the handler runs, on every interface.
431
+ * Who may call it: a permission string such as "tasks.write", "authenticated" (any signed-in
432
+ * caller, an explicit choice) or "public" (anyone). Required: FeltKit grants nothing by default.
433
+ * Checked before the handler runs, on every interface.
329
434
  */
330
- access?: Access;
435
+ access: Access;
331
436
  description?: string;
332
437
  /** Publish this function as an AppPort capability (discoverable by agents and other apps). */
333
438
  capability?: CapabilityExposure | string;
@@ -374,20 +479,100 @@ export interface FunctionDefinition<Kind extends FunctionKind = FunctionKind, Ar
374
479
  readonly $args?: Args;
375
480
  readonly $result?: Result;
376
481
  }
482
+ /**
483
+ * An application agent (`app.agent`): code that runs after a mutation commits, as the agent's own
484
+ * principal. See `FeltKitApp.agent`.
485
+ */
486
+ export interface AgentConfig<Input extends Record<string, unknown> = Record<string, unknown>> {
487
+ /** The agent's identity: its principal id (type "agent") and the name people see. */
488
+ principal: {
489
+ id: string;
490
+ displayName?: string;
491
+ };
492
+ /** Role names from `feltkit({ roles })`: the agent's authority, and nothing else. */
493
+ roles: readonly string[];
494
+ /**
495
+ * What triggers it, besides `ctx.afterCommit.agent(name, input)` (always available): an
496
+ * `app.event` (or several). A mutation that publishes it with `ctx.events.publish` records the
497
+ * run in its own transaction; the handler's input is `{ event, payload, scope? }`.
498
+ */
499
+ on?: EventDefinition<any> | readonly EventDefinition<any>[];
500
+ /** The permission the agent needs to run (checked against its roles, then `feltkit({ authorize })`). Required: an agent, too, gets nothing by default. */
501
+ access: Access;
502
+ description?: string;
503
+ /** Attempts before the run is recorded failed (default 5), and the first retry's delay (default 1000 ms, doubling, at most 10 min). */
504
+ retry?: {
505
+ maxAttempts?: number;
506
+ backoffMs?: number;
507
+ };
508
+ /** How long a run holds its claim before the services worker treats it as abandoned and runs it again (default 300 000 ms). */
509
+ leaseMs?: number;
510
+ handler: (ctx: AgentCtx, input: Input) => unknown;
511
+ }
512
+ export interface AgentDefinition {
513
+ readonly name: string;
514
+ readonly principal: {
515
+ readonly id: string;
516
+ readonly type: "agent";
517
+ readonly displayName?: string;
518
+ };
519
+ readonly roles: readonly string[];
520
+ /** Event names that trigger it. */
521
+ readonly on: readonly string[];
522
+ readonly retry: {
523
+ readonly maxAttempts: number;
524
+ readonly backoffMs: number;
525
+ };
526
+ readonly leaseMs: number;
527
+ /** The handler as a mutation (internal: run only by the runtime, under the name `agent/<name>`). */
528
+ readonly function: FunctionDefinition<"mutation">;
529
+ }
530
+ export declare const AGENT_DEFAULT_ATTEMPTS = 5;
531
+ export declare const AGENT_DEFAULT_BACKOFF_MS = 1000;
532
+ export declare const AGENT_MAX_BACKOFF_MS = 600000;
533
+ export declare const AGENT_DEFAULT_LEASE_MS = 300000;
377
534
  export type QueryDefinition<Args = any, Result = any> = FunctionDefinition<"query", Args, Result>;
378
535
  export type MutationDefinition<Args = any, Result = any> = FunctionDefinition<"mutation", Args, Result>;
379
- type Handler<Kind extends FunctionKind, R> = (ctx: Kind extends "query" ? QueryCtx : MutationCtx) => R | Promise<R>;
380
- export declare class FeltKitApp {
536
+ /** The value type a claim declaration (a schema or a claim spec) carries. */
537
+ export type ClaimValueOf<D> = D extends Schema<infer V> ? V : D extends {
538
+ type: "enum";
539
+ } ? string : D extends {
540
+ type: "string";
541
+ } ? string : D extends {
542
+ type: "boolean";
543
+ } ? boolean : D extends {
544
+ type: "number" | "integer";
545
+ } ? number : unknown;
546
+ /** The typed claims of an app's tenant memberships (`auth.tenant.claims`), plus `role`. */
547
+ export type TenantClaimsOf<O> = O extends {
548
+ auth?: {
549
+ tenant?: infer T;
550
+ };
551
+ } ? T extends {
552
+ roles: readonly (infer R)[];
553
+ claims?: infer C;
554
+ } ? {
555
+ role: R & string;
556
+ } & (C extends Record<string, ClaimDeclaration> ? {
557
+ [K in keyof C]?: ClaimValueOf<C[K]>;
558
+ } : {}) : {
559
+ role: string;
560
+ } : {
561
+ role: string;
562
+ };
563
+ export declare class FeltKitApp<O extends FeltKitOptions = FeltKitOptions> {
381
564
  #private;
382
565
  readonly [APP] = true;
383
- readonly options: Readonly<FeltKitOptions>;
566
+ readonly options: Readonly<O>;
384
567
  readonly collections: Map<string, CollectionDefinition<ShapeInput>>;
385
568
  readonly integrations: Map<string, IntegrationDefinition<Record<string, IntegrationOperation>>>;
386
569
  /** Ephemeral events (see `event`), by name. */
387
570
  readonly events: Map<string, EventDefinition<unknown>>;
388
571
  /** Data migrations, in declaration order (see `migration`). */
389
572
  readonly migrations: MigrationDefinition[];
390
- constructor(options: FeltKitOptions);
573
+ /** Application agents (see `agent`), by name. */
574
+ readonly agents: Map<string, AgentDefinition>;
575
+ constructor(options: O);
391
576
  get name(): string;
392
577
  /**
393
578
  * The application's version: `feltkit({ version })`, else the project's package.json
@@ -408,10 +593,60 @@ export declare class FeltKitApp {
408
593
  */
409
594
  event<S extends Schema>(name: string, config: {
410
595
  payload: S;
411
- access?: Access;
596
+ access: Access;
412
597
  description?: string;
413
598
  }): EventDefinition<S extends Schema<infer T> ? T : unknown>;
599
+ /**
600
+ * An application agent: a handler that runs after a mutation commits, **as the agent** (principal
601
+ * type "agent", `roles`, never the triggering caller's authority), through the same authority as
602
+ * any call (roles → `access` → `feltkit({ authorize })`). A mutation triggers it with
603
+ * `ctx.afterCommit.agent(name, input)`, or by publishing an event named in `on`. The run is
604
+ * recorded in the mutation's transaction (the after-commit outbox), so it happens for every
605
+ * committed mutation and never for one that did not commit; it runs at least once, with a lease,
606
+ * retries with backoff, and its writes commit at most once per trigger (`ctx.trigger.idempotencyKey`).
607
+ *
608
+ * export const assistant = app.agent("assistant", {
609
+ * principal: { id: "assistant", displayName: "Assistant" },
610
+ * roles: ["assistant"],
611
+ * access: "messages.write",
612
+ * handler: async (ctx, { roomId }) => { ... ctx.db.insert(messages, { roomId, text }) ... },
613
+ * });
614
+ * // in messages:send — if (text.includes("@assistant")) ctx.afterCommit.agent("assistant", { roomId });
615
+ *
616
+ * See docs/TRANSACTIONS.md ("Application agents"). Not available in an AppBoundry artifact.
617
+ */
618
+ agent<Input extends Record<string, unknown> = Record<string, unknown>>(name: string, config: AgentConfig<Input>): AgentDefinition;
414
619
  collection<S extends ShapeInput>(name: string, shape: S, options?: CollectionOptions): CollectionDefinition<S>;
620
+ /**
621
+ * The tenant a call works in, with this app's typed role and claims (`auth.tenant`), or null when
622
+ * the call has none. The same value as `ctx.auth.tenant`, typed.
623
+ *
624
+ * const tenant = app.requireTenant(ctx); // { id, name, role: "owner" | "admin" | …, claims: { role, plan? } }
625
+ */
626
+ tenant(ctx: {
627
+ readonly auth: {
628
+ readonly tenant: import("./runtime/tenancy.js").TenantContext | null;
629
+ };
630
+ }): import("./runtime/tenancy.js").TenantContext<TenantClaimsOf<O>> | null;
631
+ /** Like `tenant(ctx)`, but a call without a tenant fails (FAILED_PRECONDITION, "choose a workspace"). */
632
+ requireTenant(ctx: {
633
+ readonly auth: {
634
+ readonly tenant: import("./runtime/tenancy.js").TenantContext | null;
635
+ };
636
+ }): import("./runtime/tenancy.js").TenantContext<TenantClaimsOf<O>>;
637
+ /**
638
+ * The authorization rule over a membership claim: the call's tenant must present `name` (set through the server and
639
+ * valid under the declaration now, or the declaration's default) with a value among `allowed` (or one `allowed(value)`
640
+ * accepts). Anything else is FORBIDDEN: no tenant, a claim never set, a stale value the declaration no longer accepts,
641
+ * or a value outside `allowed`. Nothing a client sends can satisfy it: claims are read from the live membership only.
642
+ *
643
+ * app.requireClaim(ctx, "plan", ["pro"]);
644
+ */
645
+ requireClaim<K extends keyof TenantClaimsOf<O> & string>(ctx: {
646
+ readonly auth: {
647
+ readonly tenant: import("./runtime/tenancy.js").TenantContext | null;
648
+ };
649
+ }, name: K, allowed: ReadonlyArray<NonNullable<TenantClaimsOf<O>[K]>> | ((value: NonNullable<TenantClaimsOf<O>[K]>) => boolean)): NonNullable<TenantClaimsOf<O>[K]>;
415
650
  /**
416
651
  * A data migration: code that rewrites stored records so they satisfy the schema your
417
652
  * collections now declare (a new required field, a changed type, a removed field or
@@ -434,15 +669,13 @@ export declare class FeltKitApp {
434
669
  */
435
670
  integration<Ops extends Record<string, IntegrationOperation>>(name: string, config: IntegrationConfig<Ops>): IntegrationDefinition<Ops>;
436
671
  /** A read-only function. Its database handle cannot write. */
437
- query<R>(handler: Handler<"query", R>): QueryDefinition<{}, Awaited<R>>;
438
672
  query<A extends ShapeInput = {}, R = unknown>(config: FunctionConfig<"query", A, R>): QueryDefinition<Simplify<InferShape<A>>, Awaited<R>>;
439
673
  /** A function that changes state. All of its writes commit atomically, or none do. */
440
- mutation<R>(handler: Handler<"mutation", R>): MutationDefinition<{}, Awaited<R>>;
441
674
  mutation<A extends ShapeInput = {}, R = unknown>(config: FunctionConfig<"mutation", A, R>): MutationDefinition<Simplify<InferShape<A>>, Awaited<R>>;
442
675
  private define;
443
676
  }
444
677
  /** Define a FeltKit application. */
445
- export declare function feltkit(options: FeltKitOptions): FeltKitApp;
678
+ export declare function feltkit<const O extends FeltKitOptions>(options: O): FeltKitApp<O>;
446
679
  export declare function isFeltKitApp(value: unknown): value is FeltKitApp;
447
680
  export declare function isFunctionDefinition(value: unknown): value is FunctionDefinition;
448
681
  export declare function isEventDefinition(value: unknown): value is EventDefinition;