@agent-native/core 0.107.2 → 0.108.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 (228) hide show
  1. package/corpus/README.md +2 -2
  2. package/corpus/core/CHANGELOG.md +6 -0
  3. package/corpus/core/docs/content/actions.mdx +48 -0
  4. package/corpus/core/docs/content/locales/ar-SA/actions.mdx +38 -0
  5. package/corpus/core/docs/content/locales/de-DE/actions.mdx +38 -0
  6. package/corpus/core/docs/content/locales/es-ES/actions.mdx +38 -0
  7. package/corpus/core/docs/content/locales/fr-FR/actions.mdx +38 -0
  8. package/corpus/core/docs/content/locales/hi-IN/actions.mdx +38 -0
  9. package/corpus/core/docs/content/locales/ja-JP/actions.mdx +38 -0
  10. package/corpus/core/docs/content/locales/ko-KR/actions.mdx +38 -0
  11. package/corpus/core/docs/content/locales/pt-BR/actions.mdx +38 -0
  12. package/corpus/core/docs/content/locales/zh-CN/actions.mdx +38 -0
  13. package/corpus/core/docs/content/locales/zh-TW/actions.mdx +38 -0
  14. package/corpus/core/package.json +5 -1
  15. package/corpus/core/src/a2a-claims.ts +52 -0
  16. package/corpus/core/src/action.ts +6 -2
  17. package/corpus/core/src/audit/record.ts +6 -0
  18. package/corpus/core/src/client/feature-flags/FeatureFlagsPanel.tsx +519 -0
  19. package/corpus/core/src/client/feature-flags/helpers.ts +77 -0
  20. package/corpus/core/src/client/feature-flags/index.ts +13 -0
  21. package/corpus/core/src/client/feature-flags/types.ts +29 -0
  22. package/corpus/core/src/client/feature-flags/use-feature-flag.ts +26 -0
  23. package/corpus/core/src/client/index.ts +11 -0
  24. package/corpus/core/src/extensions/routes.ts +1 -1
  25. package/corpus/core/src/feature-flags/a2a-action-route.ts +56 -0
  26. package/corpus/core/src/feature-flags/actions/get-feature-flags.ts +24 -0
  27. package/corpus/core/src/feature-flags/actions/list-feature-flags.ts +69 -0
  28. package/corpus/core/src/feature-flags/actions/set-feature-flag.ts +89 -0
  29. package/corpus/core/src/feature-flags/index.ts +21 -0
  30. package/corpus/core/src/feature-flags/permissions.ts +53 -0
  31. package/corpus/core/src/feature-flags/plugin.ts +44 -0
  32. package/corpus/core/src/feature-flags/registry.ts +96 -0
  33. package/corpus/core/src/feature-flags/store.ts +176 -0
  34. package/corpus/core/src/index.ts +15 -0
  35. package/corpus/core/src/localization/default-messages.ts +47 -0
  36. package/corpus/core/src/server/action-discovery.ts +12 -0
  37. package/corpus/core/src/server/action-routes.ts +47 -5
  38. package/corpus/core/src/server/index.ts +1 -0
  39. package/corpus/core/src/settings/org-settings.ts +13 -0
  40. package/corpus/core/src/settings/store.ts +57 -0
  41. package/corpus/core/src/templates/default/.agents/skills/feature-flags/SKILL.md +169 -0
  42. package/corpus/core/src/templates/default/AGENTS.md +1 -0
  43. package/corpus/core/src/templates/headless/.agents/skills/feature-flags/SKILL.md +169 -0
  44. package/corpus/core/src/templates/workspace-core/.agents/skills/feature-flags/SKILL.md +169 -0
  45. package/corpus/core/src/templates/workspace-core/AGENTS.md +1 -1
  46. package/corpus/core/src/templates/workspace-root/AGENTS.md +1 -1
  47. package/corpus/core/src/vite/action-types-plugin.ts +12 -0
  48. package/corpus/templates/analytics/.agents/skills/feature-flags/SKILL.md +169 -0
  49. package/corpus/templates/analytics/AGENTS.md +15 -3
  50. package/corpus/templates/analytics/actions/list-workspace-feature-flags.ts +15 -0
  51. package/corpus/templates/analytics/actions/navigate.ts +2 -2
  52. package/corpus/templates/analytics/actions/set-workspace-feature-flag.ts +45 -0
  53. package/corpus/templates/analytics/actions/view-screen.ts +13 -0
  54. package/corpus/templates/analytics/app/components/agents/FeatureFlagsFleetPanel.tsx +243 -0
  55. package/corpus/templates/analytics/app/components/layout/CommandPalette.tsx +11 -3
  56. package/corpus/templates/analytics/app/hooks/use-navigation-state.ts +3 -1
  57. package/corpus/templates/analytics/app/i18n/zh-TW.ts +17 -0
  58. package/corpus/templates/analytics/app/i18n-data.ts +251 -0
  59. package/corpus/templates/analytics/app/pages/Agents.tsx +22 -2
  60. package/corpus/templates/analytics/changelog/2026-07-17-manage-feature-flag-rollouts-across-your-organization-s-apps.md +6 -0
  61. package/corpus/templates/analytics/server/lib/db-admin-connections.ts +13 -5
  62. package/corpus/templates/analytics/server/lib/workspace-feature-flags.ts +347 -0
  63. package/corpus/templates/assets/.agents/skills/feature-flags/SKILL.md +169 -0
  64. package/corpus/templates/brain/.agents/skills/feature-flags/SKILL.md +169 -0
  65. package/corpus/templates/calendar/.agents/skills/feature-flags/SKILL.md +169 -0
  66. package/corpus/templates/chat/.agents/skills/feature-flags/SKILL.md +169 -0
  67. package/corpus/templates/clips/.agents/skills/feature-flags/SKILL.md +169 -0
  68. package/corpus/templates/clips/server/plugins/feature-flags.ts +11 -0
  69. package/corpus/templates/clips/shared/feature-flags.ts +30 -37
  70. package/corpus/templates/content/.agents/skills/feature-flags/SKILL.md +169 -0
  71. package/corpus/templates/design/.agents/skills/feature-flags/SKILL.md +169 -0
  72. package/corpus/templates/design/AGENTS.md +5 -3
  73. package/corpus/templates/design/actions/add-fusion-screens.ts +4 -6
  74. package/corpus/templates/design/actions/apply-fusion-edits.ts +4 -6
  75. package/corpus/templates/design/actions/create-fusion-app.ts +5 -4
  76. package/corpus/templates/design/actions/deploy-fusion-app.ts +4 -3
  77. package/corpus/templates/design/actions/get-fusion-deploy-status.ts +4 -6
  78. package/corpus/templates/design/actions/list-fusion-edits.ts +4 -3
  79. package/corpus/templates/design/actions/push-fusion-app.ts +4 -6
  80. package/corpus/templates/design/actions/queue-fusion-edit.ts +4 -3
  81. package/corpus/templates/design/actions/send-fusion-message.ts +4 -6
  82. package/corpus/templates/design/actions/sync-fusion-app.ts +4 -3
  83. package/corpus/templates/design/app/components/editor/PromptDialog.tsx +1 -1
  84. package/corpus/templates/design/app/pages/Index.tsx +8 -6
  85. package/corpus/templates/design/server/plugins/feature-flags.ts +5 -0
  86. package/corpus/templates/design/shared/full-app.ts +10 -7
  87. package/corpus/templates/dispatch/.agents/skills/feature-flags/SKILL.md +169 -0
  88. package/corpus/templates/forms/.agents/skills/feature-flags/SKILL.md +169 -0
  89. package/corpus/templates/macros/.agents/skills/feature-flags/SKILL.md +169 -0
  90. package/corpus/templates/mail/.agents/skills/feature-flags/SKILL.md +169 -0
  91. package/corpus/templates/plan/.agents/skills/feature-flags/SKILL.md +169 -0
  92. package/corpus/templates/slides/.agents/skills/feature-flags/SKILL.md +169 -0
  93. package/corpus/templates/tasks/.agents/skills/feature-flags/SKILL.md +169 -0
  94. package/dist/a2a-claims.d.ts +10 -0
  95. package/dist/a2a-claims.d.ts.map +1 -0
  96. package/dist/a2a-claims.js +41 -0
  97. package/dist/a2a-claims.js.map +1 -0
  98. package/dist/action.d.ts +6 -2
  99. package/dist/action.d.ts.map +1 -1
  100. package/dist/action.js.map +1 -1
  101. package/dist/audit/record.d.ts +3 -0
  102. package/dist/audit/record.d.ts.map +1 -1
  103. package/dist/audit/record.js +3 -0
  104. package/dist/audit/record.js.map +1 -1
  105. package/dist/client/feature-flags/FeatureFlagsPanel.d.ts +10 -0
  106. package/dist/client/feature-flags/FeatureFlagsPanel.d.ts.map +1 -0
  107. package/dist/client/feature-flags/FeatureFlagsPanel.js +143 -0
  108. package/dist/client/feature-flags/FeatureFlagsPanel.js.map +1 -0
  109. package/dist/client/feature-flags/helpers.d.ts +16 -0
  110. package/dist/client/feature-flags/helpers.d.ts.map +1 -0
  111. package/dist/client/feature-flags/helpers.js +50 -0
  112. package/dist/client/feature-flags/helpers.js.map +1 -0
  113. package/dist/client/feature-flags/index.d.ts +5 -0
  114. package/dist/client/feature-flags/index.d.ts.map +1 -0
  115. package/dist/client/feature-flags/index.js +4 -0
  116. package/dist/client/feature-flags/index.js.map +1 -0
  117. package/dist/client/feature-flags/types.d.ts +27 -0
  118. package/dist/client/feature-flags/types.d.ts.map +1 -0
  119. package/dist/client/feature-flags/types.js +2 -0
  120. package/dist/client/feature-flags/types.js.map +1 -0
  121. package/dist/client/feature-flags/use-feature-flag.d.ts +8 -0
  122. package/dist/client/feature-flags/use-feature-flag.d.ts.map +1 -0
  123. package/dist/client/feature-flags/use-feature-flag.js +15 -0
  124. package/dist/client/feature-flags/use-feature-flag.js.map +1 -0
  125. package/dist/client/index.d.ts +1 -0
  126. package/dist/client/index.d.ts.map +1 -1
  127. package/dist/client/index.js +1 -0
  128. package/dist/client/index.js.map +1 -1
  129. package/dist/collab/routes.d.ts +1 -1
  130. package/dist/extensions/routes.d.ts.map +1 -1
  131. package/dist/extensions/routes.js +1 -1
  132. package/dist/extensions/routes.js.map +1 -1
  133. package/dist/feature-flags/a2a-action-route.d.ts +14 -0
  134. package/dist/feature-flags/a2a-action-route.d.ts.map +1 -0
  135. package/dist/feature-flags/a2a-action-route.js +50 -0
  136. package/dist/feature-flags/a2a-action-route.js.map +1 -0
  137. package/dist/feature-flags/actions/get-feature-flags.d.ts +3 -0
  138. package/dist/feature-flags/actions/get-feature-flags.d.ts.map +1 -0
  139. package/dist/feature-flags/actions/get-feature-flags.js +18 -0
  140. package/dist/feature-flags/actions/get-feature-flags.js.map +1 -0
  141. package/dist/feature-flags/actions/list-feature-flags.d.ts +28 -0
  142. package/dist/feature-flags/actions/list-feature-flags.d.ts.map +1 -0
  143. package/dist/feature-flags/actions/list-feature-flags.js +60 -0
  144. package/dist/feature-flags/actions/list-feature-flags.js.map +1 -0
  145. package/dist/feature-flags/actions/set-feature-flag.d.ts +26 -0
  146. package/dist/feature-flags/actions/set-feature-flag.d.ts.map +1 -0
  147. package/dist/feature-flags/actions/set-feature-flag.js +78 -0
  148. package/dist/feature-flags/actions/set-feature-flag.js.map +1 -0
  149. package/dist/feature-flags/index.d.ts +5 -0
  150. package/dist/feature-flags/index.d.ts.map +1 -0
  151. package/dist/feature-flags/index.js +5 -0
  152. package/dist/feature-flags/index.js.map +1 -0
  153. package/dist/feature-flags/permissions.d.ts +13 -0
  154. package/dist/feature-flags/permissions.d.ts.map +1 -0
  155. package/dist/feature-flags/permissions.js +37 -0
  156. package/dist/feature-flags/permissions.js.map +1 -0
  157. package/dist/feature-flags/plugin.d.ts +17 -0
  158. package/dist/feature-flags/plugin.d.ts.map +1 -0
  159. package/dist/feature-flags/plugin.js +29 -0
  160. package/dist/feature-flags/plugin.js.map +1 -0
  161. package/dist/feature-flags/registry.d.ts +23 -0
  162. package/dist/feature-flags/registry.d.ts.map +1 -0
  163. package/dist/feature-flags/registry.js +59 -0
  164. package/dist/feature-flags/registry.js.map +1 -0
  165. package/dist/feature-flags/store.d.ts +31 -0
  166. package/dist/feature-flags/store.d.ts.map +1 -0
  167. package/dist/feature-flags/store.js +124 -0
  168. package/dist/feature-flags/store.js.map +1 -0
  169. package/dist/index.d.ts +1 -0
  170. package/dist/index.d.ts.map +1 -1
  171. package/dist/index.js +1 -0
  172. package/dist/index.js.map +1 -1
  173. package/dist/localization/default-messages.d.ts +42 -0
  174. package/dist/localization/default-messages.d.ts.map +1 -1
  175. package/dist/localization/default-messages.js +42 -0
  176. package/dist/localization/default-messages.js.map +1 -1
  177. package/dist/observability/routes.d.ts +1 -1
  178. package/dist/progress/routes.d.ts +1 -1
  179. package/dist/resources/handlers.d.ts +1 -1
  180. package/dist/server/action-discovery.d.ts.map +1 -1
  181. package/dist/server/action-discovery.js +12 -0
  182. package/dist/server/action-discovery.js.map +1 -1
  183. package/dist/server/action-routes.d.ts +3 -0
  184. package/dist/server/action-routes.d.ts.map +1 -1
  185. package/dist/server/action-routes.js +45 -5
  186. package/dist/server/action-routes.js.map +1 -1
  187. package/dist/server/index.d.ts +1 -0
  188. package/dist/server/index.d.ts.map +1 -1
  189. package/dist/server/index.js +1 -0
  190. package/dist/server/index.js.map +1 -1
  191. package/dist/settings/org-settings.d.ts +2 -0
  192. package/dist/settings/org-settings.d.ts.map +1 -1
  193. package/dist/settings/org-settings.js +5 -1
  194. package/dist/settings/org-settings.js.map +1 -1
  195. package/dist/settings/store.d.ts +8 -0
  196. package/dist/settings/store.d.ts.map +1 -1
  197. package/dist/settings/store.js +48 -0
  198. package/dist/settings/store.js.map +1 -1
  199. package/dist/templates/chat/.agents/skills/feature-flags/SKILL.md +169 -0
  200. package/dist/templates/default/.agents/skills/feature-flags/SKILL.md +169 -0
  201. package/dist/templates/default/AGENTS.md +1 -0
  202. package/dist/templates/headless/.agents/skills/feature-flags/SKILL.md +169 -0
  203. package/dist/templates/workspace-core/.agents/skills/feature-flags/SKILL.md +169 -0
  204. package/dist/templates/workspace-core/AGENTS.md +1 -1
  205. package/dist/templates/workspace-root/AGENTS.md +1 -1
  206. package/dist/vite/action-types-plugin.d.ts.map +1 -1
  207. package/dist/vite/action-types-plugin.js +12 -0
  208. package/dist/vite/action-types-plugin.js.map +1 -1
  209. package/docs/content/actions.mdx +48 -0
  210. package/docs/content/locales/ar-SA/actions.mdx +38 -0
  211. package/docs/content/locales/de-DE/actions.mdx +38 -0
  212. package/docs/content/locales/es-ES/actions.mdx +38 -0
  213. package/docs/content/locales/fr-FR/actions.mdx +38 -0
  214. package/docs/content/locales/hi-IN/actions.mdx +38 -0
  215. package/docs/content/locales/ja-JP/actions.mdx +38 -0
  216. package/docs/content/locales/ko-KR/actions.mdx +38 -0
  217. package/docs/content/locales/pt-BR/actions.mdx +38 -0
  218. package/docs/content/locales/zh-CN/actions.mdx +38 -0
  219. package/docs/content/locales/zh-TW/actions.mdx +38 -0
  220. package/package.json +5 -1
  221. package/src/templates/chat/.agents/skills/feature-flags/SKILL.md +169 -0
  222. package/src/templates/default/.agents/skills/feature-flags/SKILL.md +169 -0
  223. package/src/templates/default/AGENTS.md +1 -0
  224. package/src/templates/headless/.agents/skills/feature-flags/SKILL.md +169 -0
  225. package/src/templates/workspace-core/.agents/skills/feature-flags/SKILL.md +169 -0
  226. package/src/templates/workspace-core/AGENTS.md +1 -1
  227. package/src/templates/workspace-root/AGENTS.md +1 -1
  228. package/corpus/templates/clips/actions/get-feature-flags.ts +0 -30
@@ -0,0 +1,56 @@
1
+ import { getHeader } from "h3";
2
+ import * as jose from "jose";
3
+
4
+ import { verifyA2ATokenWithClaims } from "../a2a-claims.js";
5
+ import type { ActionRouteAuthAdapter } from "../server/action-routes.js";
6
+
7
+ const FLAG_ACTION_SCOPES = {
8
+ "list-feature-flags": "flags:read",
9
+ "set-feature-flag": "flags:write",
10
+ } as const;
11
+
12
+ export function declaresFeatureFlagDelegation(token: string): boolean {
13
+ try {
14
+ const raw = jose.decodeJwt(token);
15
+ const scopes =
16
+ typeof raw.scope === "string"
17
+ ? raw.scope.split(/\s+/)
18
+ : Array.isArray(raw.scope)
19
+ ? raw.scope.filter(
20
+ (scope): scope is string => typeof scope === "string",
21
+ )
22
+ : [];
23
+ return scopes.some((scope) => scope.startsWith("flags:"));
24
+ } catch {
25
+ return false;
26
+ }
27
+ }
28
+
29
+ /**
30
+ * Narrow opt-in adapter for fleet flag control. It owns Bearer credentials only
31
+ * for these two actions; malformed owned bearers reject rather than falling
32
+ * back to a browser cookie.
33
+ */
34
+ export function createFeatureFlagA2AActionRouteAuth(
35
+ actionName: keyof typeof FLAG_ACTION_SCOPES,
36
+ ): ActionRouteAuthAdapter {
37
+ return {
38
+ async resolveCaller(event) {
39
+ const header = getHeader(event, "authorization");
40
+ if (!header?.startsWith("Bearer ")) return null;
41
+ const token = header.slice(7);
42
+ if (!declaresFeatureFlagDelegation(token)) return null;
43
+ const claims = await verifyA2ATokenWithClaims(token, event);
44
+ if (!claims || !claims.scope.includes(FLAG_ACTION_SCOPES[actionName])) {
45
+ throw new Error("Invalid feature flag delegation");
46
+ }
47
+ return {
48
+ owner: claims.email,
49
+ orgId: claims.orgId,
50
+ anonymous: false,
51
+ delegationJti: claims.jti,
52
+ delegationIssuer: claims.issuer,
53
+ };
54
+ },
55
+ };
56
+ }
@@ -0,0 +1,24 @@
1
+ import { z } from "zod";
2
+
3
+ import { defineAction } from "../../action.js";
4
+ import { listFeatureFlags } from "../registry.js";
5
+ import { evaluateFeatureFlag } from "../store.js";
6
+
7
+ export default defineAction({
8
+ description:
9
+ "Return the boolean values of every feature flag registered by this app for the current caller. Unknown or unconfigured flags always evaluate to false.",
10
+ schema: z.object({}),
11
+ http: { method: "GET" },
12
+ run: async (_args, ctx) => {
13
+ const scope = { userEmail: ctx?.userEmail, orgId: ctx?.orgId };
14
+ const values = Object.fromEntries(
15
+ await Promise.all(
16
+ listFeatureFlags().map(async ({ key }) => [
17
+ key,
18
+ await evaluateFeatureFlag(key, scope).catch(() => false),
19
+ ]),
20
+ ),
21
+ );
22
+ return values;
23
+ },
24
+ });
@@ -0,0 +1,69 @@
1
+ import { z } from "zod";
2
+
3
+ import { defineAction } from "../../action.js";
4
+ import { requireFeatureFlagManager } from "../permissions.js";
5
+ import { listFeatureFlags } from "../registry.js";
6
+ import { evaluateFeatureFlagRules, getFeatureFlagRules } from "../store.js";
7
+
8
+ export default defineAction({
9
+ description:
10
+ "List this app's registered feature flags and their current rules. Organization owner/admin only (or the explicit no-org administrator).",
11
+ schema: z.object({}),
12
+ http: { method: "GET" },
13
+ toolCallable: false,
14
+ run: async (_args, ctx) => {
15
+ const definitions = listFeatureFlags();
16
+ let manager;
17
+ try {
18
+ manager = await requireFeatureFlagManager(ctx ?? {});
19
+ } catch (error) {
20
+ if (
21
+ error instanceof Error &&
22
+ "statusCode" in error &&
23
+ (error as { statusCode?: number }).statusCode === 403
24
+ ) {
25
+ return {
26
+ contractVersion: 1,
27
+ status: "forbidden" as const,
28
+ reason: "forbidden" as const,
29
+ flags: [],
30
+ canManage: false,
31
+ };
32
+ }
33
+ throw error;
34
+ }
35
+ if (definitions.length === 0)
36
+ return {
37
+ contractVersion: 1,
38
+ status: "no-definitions" as const,
39
+ reason: "no-definitions" as const,
40
+ flags: [],
41
+ canManage: true,
42
+ };
43
+ const flags = await Promise.all(
44
+ definitions.map(async (definition) => {
45
+ const rules = await getFeatureFlagRules(definition.key, manager);
46
+ return {
47
+ ...definition,
48
+ rules,
49
+ enabledForCurrentUser: evaluateFeatureFlagRules(
50
+ definition.key,
51
+ rules,
52
+ {
53
+ userEmail: manager.email,
54
+ userKey: manager.email,
55
+ orgId: manager.orgId,
56
+ },
57
+ ),
58
+ };
59
+ }),
60
+ );
61
+ return {
62
+ contractVersion: 1,
63
+ status: "ready" as const,
64
+ reason: "ready" as const,
65
+ flags,
66
+ canManage: true,
67
+ };
68
+ },
69
+ });
@@ -0,0 +1,89 @@
1
+ import { z } from "zod";
2
+
3
+ import { defineAction } from "../../action.js";
4
+ import { requireFeatureFlagManager } from "../permissions.js";
5
+ import { getFeatureFlagDefinition } from "../registry.js";
6
+ import {
7
+ defaultFeatureFlagRules,
8
+ mutateFeatureFlagRules,
9
+ normalizeFeatureFlagRules,
10
+ } from "../store.js";
11
+
12
+ const rulesSchema = z.object({
13
+ mode: z.enum(["off", "on", "rules"]),
14
+ emails: z.array(z.string().email()).max(500).optional(),
15
+ orgIds: z.array(z.string().min(1).max(200)).max(500).optional(),
16
+ percentage: z.number().int().min(0).max(100).optional(),
17
+ });
18
+
19
+ const schema = z.discriminatedUnion("operation", [
20
+ z.object({
21
+ operation: z.literal("enable-for-current-user"),
22
+ key: z.string(),
23
+ }),
24
+ z.object({ operation: z.literal("off"), key: z.string() }),
25
+ z.object({
26
+ operation: z.literal("replace-rules"),
27
+ key: z.string(),
28
+ rules: rulesSchema,
29
+ }),
30
+ ]);
31
+
32
+ export default defineAction({
33
+ description:
34
+ "Atomically manage one registered feature flag: enable it for the current user, turn it off immediately for the active scope, or replace its full rules. Organization owner/admin only (or the explicit no-org administrator).",
35
+ schema,
36
+ // Keep the strict discriminated union for runtime validation, but advertise
37
+ // an object-shaped schema so agent tool registries can expose the action.
38
+ // Root-level JSON Schema unions are intentionally rejected by the agent.
39
+ agentInputSchema: z.object({
40
+ operation: z.enum(["enable-for-current-user", "off", "replace-rules"]),
41
+ key: z.string(),
42
+ rules: rulesSchema.optional(),
43
+ }),
44
+ toolCallable: false,
45
+ audit: {
46
+ target: (args, _result, meta) => ({
47
+ type: "feature-flag",
48
+ id: args.key,
49
+ ownerEmail: meta.userEmail,
50
+ visibility: meta.orgId ? "org" : "private",
51
+ }),
52
+ summary: (args) => `Feature flag ${args.key}: ${args.operation}`,
53
+ },
54
+ run: async (args, ctx) => {
55
+ const manager = await requireFeatureFlagManager(ctx ?? {});
56
+ if (!getFeatureFlagDefinition(args.key)) {
57
+ throw new Error(`Unknown feature flag: ${args.key}`);
58
+ }
59
+
60
+ const persistedRules = await mutateFeatureFlagRules(
61
+ args.key,
62
+ manager,
63
+ (current) => {
64
+ let rules;
65
+ if (args.operation === "off") {
66
+ rules = defaultFeatureFlagRules();
67
+ } else if (args.operation === "replace-rules") {
68
+ rules = normalizeFeatureFlagRules(args.rules);
69
+ } else {
70
+ rules = normalizeFeatureFlagRules({
71
+ ...current,
72
+ // A globally-on flag already includes this user. Do not
73
+ // accidentally narrow it to a one-email rollout.
74
+ mode: current.mode === "on" ? "on" : "rules",
75
+ emails: [...current.emails, manager.email],
76
+ });
77
+ }
78
+ return { ...rules, updatedAt: Date.now(), updatedBy: manager.email };
79
+ },
80
+ );
81
+ return {
82
+ contractVersion: 1 as const,
83
+ status: "ready" as const,
84
+ key: args.key,
85
+ rules: persistedRules,
86
+ scope: { orgId: manager.orgId },
87
+ };
88
+ },
89
+ });
@@ -0,0 +1,21 @@
1
+ export {
2
+ defineFeatureFlag,
3
+ defineFeatureFlags,
4
+ getFeatureFlagDefinition,
5
+ listFeatureFlags,
6
+ registerFeatureFlags,
7
+ type FeatureFlagDefinition,
8
+ } from "./registry.js";
9
+ export {
10
+ defaultFeatureFlagRules,
11
+ evaluateFeatureFlag,
12
+ evaluateFeatureFlagRules,
13
+ isFeatureFlagEnabled,
14
+ getFeatureFlagRules,
15
+ normalizeFeatureFlagRules,
16
+ type FeatureFlagMode,
17
+ type FeatureFlagRules,
18
+ type FeatureFlagScope,
19
+ } from "./store.js";
20
+ export { createFeatureFlagsPlugin } from "./plugin.js";
21
+ export { createFeatureFlagA2AActionRouteAuth } from "./a2a-action-route.js";
@@ -0,0 +1,53 @@
1
+ import { getOrgRoleForEmail } from "../mcp/actions/service-token-access.js";
2
+ import { canManageOrg } from "../org/permissions.js";
3
+
4
+ export class FeatureFlagPermissionError extends Error {
5
+ statusCode: number;
6
+
7
+ constructor(message: string, statusCode: number) {
8
+ super(message);
9
+ this.statusCode = statusCode;
10
+ }
11
+ }
12
+
13
+ function productionAdminAllowlist(): Set<string> {
14
+ return new Set(
15
+ (process.env.AGENT_NATIVE_FEATURE_FLAG_ADMIN_EMAILS ?? "")
16
+ .split(",")
17
+ .map((email) => email.trim().toLowerCase())
18
+ .filter(Boolean),
19
+ );
20
+ }
21
+
22
+ /** Require an org admin/owner, or an explicit production no-org allowlist entry. */
23
+ export async function requireFeatureFlagManager(scope: {
24
+ userEmail?: string;
25
+ orgId?: string | null;
26
+ }): Promise<{ email: string; orgId: string | null }> {
27
+ const email = scope.userEmail?.trim().toLowerCase();
28
+ if (!email) {
29
+ throw new FeatureFlagPermissionError(
30
+ "Sign in to manage feature flags.",
31
+ 401,
32
+ );
33
+ }
34
+ const orgId = scope.orgId?.trim() || null;
35
+ if (orgId) {
36
+ const role = await getOrgRoleForEmail(orgId, email);
37
+ if (!canManageOrg(role)) {
38
+ throw new FeatureFlagPermissionError(
39
+ "Only organization owners or admins can manage feature flags.",
40
+ 403,
41
+ );
42
+ }
43
+ return { email, orgId };
44
+ }
45
+ if (process.env.NODE_ENV !== "production") return { email, orgId: null };
46
+ if (!productionAdminAllowlist().has(email)) {
47
+ throw new FeatureFlagPermissionError(
48
+ "Feature flag management without an organization requires an explicit admin allowlist entry.",
49
+ 403,
50
+ );
51
+ }
52
+ return { email, orgId: null };
53
+ }
@@ -0,0 +1,44 @@
1
+ import { getSetting } from "../settings/store.js";
2
+ import {
3
+ registerFeatureFlags,
4
+ type FeatureFlagDefinition,
5
+ } from "./registry.js";
6
+ import { mutateFeatureFlagRules } from "./store.js";
7
+
8
+ type NitroPluginDef = (nitroApp: any) => void | Promise<void>;
9
+
10
+ /** A tiny startup plugin for app-local, explicit feature-flag registration. */
11
+ export function createFeatureFlagsPlugin(options: {
12
+ flags: readonly FeatureFlagDefinition[];
13
+ /**
14
+ * One-time compatibility bridge for apps that previously stored global
15
+ * booleans together in a single settings object. Only legacy `true` values
16
+ * are copied, and an explicit rule in the new store always wins.
17
+ */
18
+ legacyBooleanSetting?: {
19
+ settingKey: string;
20
+ flagKeys: readonly string[];
21
+ };
22
+ }): NitroPluginDef {
23
+ return async () => {
24
+ registerFeatureFlags(options.flags);
25
+ if (!options.legacyBooleanSetting) return;
26
+
27
+ const legacy = await getSetting(options.legacyBooleanSetting.settingKey);
28
+ if (!legacy) return;
29
+ await Promise.all(
30
+ options.legacyBooleanSetting.flagKeys.map(async (key) => {
31
+ if (legacy[key] !== true) return;
32
+ await mutateFeatureFlagRules(key, {}, (current) => {
33
+ if (current.updatedAt !== null) return current;
34
+ return {
35
+ ...current,
36
+ mode: "on",
37
+ updatedAt: Date.now(),
38
+ updatedBy: "legacy-settings-migration",
39
+ };
40
+ });
41
+ }),
42
+ );
43
+ };
44
+ }
@@ -0,0 +1,96 @@
1
+ /**
2
+ * App-owned feature-flag definitions. Core intentionally starts with an empty
3
+ * registry: a flag key is part of an app's stable contract, not framework
4
+ * configuration that Core should guess at.
5
+ */
6
+ export interface FeatureFlagDefinition {
7
+ key: string;
8
+ /** Boolean flags are always default-off; explicit in operator metadata. */
9
+ defaultValue?: false;
10
+ displayName?: string;
11
+ description?: string;
12
+ }
13
+
14
+ const registry = new Map<string, FeatureFlagDefinition>();
15
+
16
+ function normalizeDefinition(
17
+ definition: FeatureFlagDefinition,
18
+ ): FeatureFlagDefinition {
19
+ const key = definition.key.trim();
20
+ if (!/^[A-Za-z][A-Za-z0-9._-]{0,63}$/.test(key)) {
21
+ throw new Error(
22
+ "Feature flag keys must be stable strings containing only letters, numbers, dots, underscores, or hyphens (1-64 characters).",
23
+ );
24
+ }
25
+ return {
26
+ key,
27
+ defaultValue: false,
28
+ ...(definition.displayName?.trim() && {
29
+ displayName: definition.displayName.trim(),
30
+ }),
31
+ ...(definition.description?.trim() && {
32
+ description: definition.description.trim(),
33
+ }),
34
+ };
35
+ }
36
+
37
+ /** Define one app-local feature flag for registration at server startup. */
38
+ export function defineFeatureFlag(
39
+ definition: FeatureFlagDefinition,
40
+ ): FeatureFlagDefinition {
41
+ return Object.freeze(normalizeDefinition(definition));
42
+ }
43
+
44
+ /** Define a small app-owned feature-flag registry. */
45
+ export function defineFeatureFlags(
46
+ definitions: readonly FeatureFlagDefinition[],
47
+ ): readonly FeatureFlagDefinition[] {
48
+ const seen = new Set<string>();
49
+ return Object.freeze(
50
+ definitions.map((definition) => {
51
+ const normalized = defineFeatureFlag(definition);
52
+ if (seen.has(normalized.key)) {
53
+ throw new Error(`Duplicate feature flag key: ${normalized.key}`);
54
+ }
55
+ seen.add(normalized.key);
56
+ return normalized;
57
+ }),
58
+ );
59
+ }
60
+
61
+ /** Register definitions once at Nitro startup. Re-registering identical data is safe for HMR. */
62
+ export function registerFeatureFlags(
63
+ definitions: readonly FeatureFlagDefinition[],
64
+ ): void {
65
+ for (const rawDefinition of definitions) {
66
+ const definition = defineFeatureFlag(rawDefinition);
67
+ const existing = registry.get(definition.key);
68
+ if (!existing) {
69
+ registry.set(definition.key, definition);
70
+ continue;
71
+ }
72
+ if (
73
+ existing.displayName !== definition.displayName ||
74
+ existing.description !== definition.description
75
+ ) {
76
+ throw new Error(
77
+ `Feature flag ${definition.key} was registered with conflicting metadata.`,
78
+ );
79
+ }
80
+ }
81
+ }
82
+
83
+ export function listFeatureFlags(): readonly FeatureFlagDefinition[] {
84
+ return [...registry.values()].sort((a, b) => a.key.localeCompare(b.key));
85
+ }
86
+
87
+ export function getFeatureFlagDefinition(
88
+ key: string,
89
+ ): FeatureFlagDefinition | null {
90
+ return registry.get(key) ?? null;
91
+ }
92
+
93
+ /** Test-only registry reset; not exported from package entrypoints. */
94
+ export function _resetFeatureFlagRegistryForTests(): void {
95
+ registry.clear();
96
+ }
@@ -0,0 +1,176 @@
1
+ import { getOrgSetting, mutateOrgSetting } from "../settings/org-settings.js";
2
+ import { getSetting, mutateSetting } from "../settings/store.js";
3
+ import {
4
+ getFeatureFlagDefinition,
5
+ type FeatureFlagDefinition,
6
+ } from "./registry.js";
7
+
8
+ export type FeatureFlagMode = "off" | "on" | "rules";
9
+
10
+ export interface FeatureFlagRules {
11
+ version: 1;
12
+ mode: FeatureFlagMode;
13
+ emails: string[];
14
+ orgIds: string[];
15
+ percentage: number;
16
+ updatedAt: number | null;
17
+ updatedBy: string | null;
18
+ }
19
+
20
+ export interface FeatureFlagScope {
21
+ userEmail?: string;
22
+ /** Canonical authenticated identity. V1 callers use normalized email. */
23
+ userKey?: string;
24
+ orgId?: string | null;
25
+ }
26
+
27
+ export const FEATURE_FLAG_SETTINGS_PREFIX = "feature-flag:";
28
+
29
+ function settingKey(key: string): string {
30
+ return `${FEATURE_FLAG_SETTINGS_PREFIX}${key}`;
31
+ }
32
+
33
+ export function defaultFeatureFlagRules(): FeatureFlagRules {
34
+ return {
35
+ version: 1,
36
+ mode: "off",
37
+ emails: [],
38
+ orgIds: [],
39
+ percentage: 0,
40
+ updatedAt: null,
41
+ updatedBy: null,
42
+ };
43
+ }
44
+
45
+ function stringList(value: unknown): string[] {
46
+ if (!Array.isArray(value)) return [];
47
+ return [
48
+ ...new Set(
49
+ value
50
+ .filter((item): item is string => typeof item === "string")
51
+ .map((item) => item.trim())
52
+ .filter(Boolean),
53
+ ),
54
+ ].sort();
55
+ }
56
+
57
+ export function normalizeFeatureFlagRules(value: unknown): FeatureFlagRules {
58
+ if (!value || typeof value !== "object") return defaultFeatureFlagRules();
59
+ const raw = value as Record<string, unknown>;
60
+ const mode: FeatureFlagMode =
61
+ raw.mode === "on" || raw.mode === "rules" || raw.mode === "off"
62
+ ? raw.mode
63
+ : "off";
64
+ const percentage =
65
+ typeof raw.percentage === "number" && Number.isFinite(raw.percentage)
66
+ ? Math.max(0, Math.min(100, Math.floor(raw.percentage)))
67
+ : 0;
68
+ return {
69
+ version: 1,
70
+ mode,
71
+ emails: stringList(raw.emails).map((email) => email.toLowerCase()),
72
+ orgIds: stringList(raw.orgIds),
73
+ percentage,
74
+ updatedAt:
75
+ typeof raw.updatedAt === "number" && Number.isSafeInteger(raw.updatedAt)
76
+ ? raw.updatedAt
77
+ : null,
78
+ updatedBy:
79
+ typeof raw.updatedBy === "string" && raw.updatedBy.trim()
80
+ ? raw.updatedBy.trim().toLowerCase()
81
+ : null,
82
+ };
83
+ }
84
+
85
+ export async function getFeatureFlagRules(
86
+ key: string,
87
+ scope: Pick<FeatureFlagScope, "orgId">,
88
+ ): Promise<FeatureFlagRules> {
89
+ if (!getFeatureFlagDefinition(key)) return defaultFeatureFlagRules();
90
+ // An organization-specific rule overrides the global rule. The fallback is
91
+ // what makes global exact-org targeting meaningful for callers in an org.
92
+ const stored = scope.orgId?.trim()
93
+ ? ((await getOrgSetting(scope.orgId, settingKey(key))) ??
94
+ (await getSetting(settingKey(key))))
95
+ : await getSetting(settingKey(key));
96
+ return normalizeFeatureFlagRules(stored);
97
+ }
98
+
99
+ /**
100
+ * Atomically derive one flag's scoped rules. An org's first override starts
101
+ * from the global fallback, then becomes independently CAS-protected.
102
+ */
103
+ export async function mutateFeatureFlagRules(
104
+ key: string,
105
+ scope: Pick<FeatureFlagScope, "orgId">,
106
+ updater: (
107
+ current: FeatureFlagRules,
108
+ ) => FeatureFlagRules | Promise<FeatureFlagRules>,
109
+ ): Promise<FeatureFlagRules> {
110
+ if (!getFeatureFlagDefinition(key)) {
111
+ throw new Error(`Unknown feature flag: ${key}`);
112
+ }
113
+ const mutate = async (stored: Record<string, unknown> | null) => {
114
+ const fallback =
115
+ stored == null && scope.orgId?.trim()
116
+ ? await getSetting(settingKey(key))
117
+ : null;
118
+ return {
119
+ ...(await updater(normalizeFeatureFlagRules(stored ?? fallback))),
120
+ };
121
+ };
122
+ const persisted = scope.orgId?.trim()
123
+ ? await mutateOrgSetting(scope.orgId, settingKey(key), mutate)
124
+ : await mutateSetting(settingKey(key), mutate);
125
+ return normalizeFeatureFlagRules(persisted);
126
+ }
127
+
128
+ function rolloutBucket(input: string): number {
129
+ // FNV-1a is deliberately tiny, deterministic, and independent of runtime.
130
+ let hash = 0x811c9dc5;
131
+ for (let index = 0; index < input.length; index += 1) {
132
+ hash ^= input.charCodeAt(index);
133
+ hash = Math.imul(hash, 0x01000193);
134
+ }
135
+ return (hash >>> 0) % 100;
136
+ }
137
+
138
+ export function evaluateFeatureFlagRules(
139
+ key: string,
140
+ rules: FeatureFlagRules,
141
+ scope: FeatureFlagScope,
142
+ ): boolean {
143
+ if (rules.mode === "off") return false;
144
+ if (rules.mode === "on") return true;
145
+ const email = scope.userEmail?.trim().toLowerCase();
146
+ if (email && rules.emails.includes(email)) return true;
147
+ if (scope.orgId && rules.orgIds.includes(scope.orgId)) return true;
148
+ const userKey = scope.userKey?.trim() || email;
149
+ if (!userKey || rules.percentage <= 0) return false;
150
+ return rolloutBucket(`${key}:${userKey}`) < rules.percentage;
151
+ }
152
+
153
+ export async function evaluateFeatureFlag(
154
+ key: string,
155
+ scope: FeatureFlagScope = {},
156
+ ): Promise<boolean> {
157
+ if (!getFeatureFlagDefinition(key)) return false;
158
+ try {
159
+ return evaluateFeatureFlagRules(
160
+ key,
161
+ await getFeatureFlagRules(key, scope),
162
+ scope,
163
+ );
164
+ } catch {
165
+ // A feature flag must never become an availability dependency.
166
+ return false;
167
+ }
168
+ }
169
+
170
+ /** Ergonomic app-action guard. Accepts either a registered definition or its key. */
171
+ export async function isFeatureFlagEnabled(
172
+ flag: string | FeatureFlagDefinition,
173
+ scope: FeatureFlagScope = {},
174
+ ): Promise<boolean> {
175
+ return evaluateFeatureFlag(typeof flag === "string" ? flag : flag.key, scope);
176
+ }
@@ -142,6 +142,21 @@ export {
142
142
  type AutomationWorkflowDefinition,
143
143
  type JsonSchema,
144
144
  } from "./automation/index.js";
145
+ export {
146
+ createFeatureFlagsPlugin,
147
+ defineFeatureFlag,
148
+ defineFeatureFlags,
149
+ evaluateFeatureFlag,
150
+ getFeatureFlagDefinition,
151
+ getFeatureFlagRules,
152
+ isFeatureFlagEnabled,
153
+ listFeatureFlags,
154
+ registerFeatureFlags,
155
+ type FeatureFlagDefinition,
156
+ type FeatureFlagMode,
157
+ type FeatureFlagRules,
158
+ type FeatureFlagScope,
159
+ } from "./feature-flags/index.js";
145
160
 
146
161
  // Server
147
162
  export {