permdock 0.0.0 → 0.1.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 (290) hide show
  1. package/README.md +268 -1
  2. package/dist/a2a/index.d.ts +178 -0
  3. package/dist/a2a/index.js +271 -0
  4. package/dist/ai-sdk/index.d.ts +74 -0
  5. package/dist/ai-sdk/index.js +137 -0
  6. package/dist/approval-required-TzNB9oQA.js +6 -0
  7. package/dist/approvals/index.d.ts +3 -0
  8. package/dist/approvals/index.js +4 -0
  9. package/dist/approvals-DaxY-Bdg.js +218 -0
  10. package/dist/approvers-aH0PJkmf.js +20 -0
  11. package/dist/arazzo-CTeVuXr5.js +70 -0
  12. package/dist/arazzo-aoTkqYsJ.js +54 -0
  13. package/dist/authzen/index.d.ts +48 -0
  14. package/dist/authzen/index.js +336 -0
  15. package/dist/better-auth/index.d.ts +92 -0
  16. package/dist/better-auth/index.js +324 -0
  17. package/dist/budget-JqcRqyhK.d.ts +11 -0
  18. package/dist/budget-NbEtbJC_.js +11 -0
  19. package/dist/capability-B68OFnZx.js +171 -0
  20. package/dist/capability-BDG_nkxt.d.ts +99 -0
  21. package/dist/catalog/index.d.ts +12 -0
  22. package/dist/catalog/index.js +2 -0
  23. package/dist/catalog-B0E6bggc.js +48 -0
  24. package/dist/catalog-H-vFGwWK.js +55 -0
  25. package/dist/catalog-doc-BSo_Q_-6.js +298 -0
  26. package/dist/catalog-idsHc0vo.js +2 -0
  27. package/dist/catalog-path-CnI0XTFK.js +12 -0
  28. package/dist/claude-agent/index.d.ts +81 -0
  29. package/dist/claude-agent/index.js +83 -0
  30. package/dist/clerk/index.d.ts +56 -0
  31. package/dist/clerk/index.js +280 -0
  32. package/dist/cli/bin.d.ts +1 -0
  33. package/dist/cli/bin.js +20 -0
  34. package/dist/cli/index.d.ts +46 -0
  35. package/dist/cli/index.js +5 -0
  36. package/dist/cloud/index.d.ts +91 -0
  37. package/dist/cloud/index.js +390 -0
  38. package/dist/cloud-BNQTrmrr.js +50 -0
  39. package/dist/cloud-Cuxr6Czw.js +117 -0
  40. package/dist/collect-B7_TIfmg.js +49 -0
  41. package/dist/collect-O-tOtbnk.js +350 -0
  42. package/dist/collect-fRPAQIWb.js +2 -0
  43. package/dist/commands-BMh_Etm2.js +93 -0
  44. package/dist/compact-y6vfu2_f.js +19 -0
  45. package/dist/compare-pfyVnBjE.js +12 -0
  46. package/dist/compile-CQbvyIBr.d.ts +30 -0
  47. package/dist/compile-D22iwvuV.js +558 -0
  48. package/dist/config-BPYmjTOp.js +26 -0
  49. package/dist/connection-JvqwHODC.js +194 -0
  50. package/dist/context-CKEAabd7.js +73 -0
  51. package/dist/context-gtAwAILx.js +6 -0
  52. package/dist/convex/index.d.ts +40 -0
  53. package/dist/convex/index.js +55 -0
  54. package/dist/create-BKF3dgFk.d.ts +162 -0
  55. package/dist/create-CSkhDU9T.js +222 -0
  56. package/dist/credential-B_rkUNlY.js +326 -0
  57. package/dist/credentials-BgRX3WEg.js +268 -0
  58. package/dist/describe-Bt1YD5uS.js +99 -0
  59. package/dist/describe-DernyItf.d.ts +37 -0
  60. package/dist/diff-BUzf5Poi.js +48 -0
  61. package/dist/diff-x5Il8LVa.js +418 -0
  62. package/dist/digest-B3kc_PgQ.d.ts +19 -0
  63. package/dist/digest-BI-6aa6d.js +27 -0
  64. package/dist/doctor-BGle8Gqi.js +1314 -0
  65. package/dist/doctor-Bqo6FwUa.js +53 -0
  66. package/dist/drizzle/index.d.ts +72 -0
  67. package/dist/drizzle/index.js +160 -0
  68. package/dist/duration-xK2jGFVc.js +25 -0
  69. package/dist/elysia/index.d.ts +71 -0
  70. package/dist/elysia/index.js +103 -0
  71. package/dist/errors-DSMnAVNa.js +43 -0
  72. package/dist/errors-DbZ1YOnb.d.ts +138 -0
  73. package/dist/errors-Iy71s7Oj.js +177 -0
  74. package/dist/errors-m3u9TmDu.js +27 -0
  75. package/dist/evaluations-DsAU9ppl.js +479 -0
  76. package/dist/eve/index.d.ts +95 -0
  77. package/dist/eve/index.js +181 -0
  78. package/dist/express/index.d.ts +46 -0
  79. package/dist/express/index.js +108 -0
  80. package/dist/fastify/index.d.ts +56 -0
  81. package/dist/fastify/index.js +141 -0
  82. package/dist/files-B_wAGYmt.js +121 -0
  83. package/dist/fixtures-Cvaw0gDG.js +64 -0
  84. package/dist/freeze-y1_jQL4v.js +31 -0
  85. package/dist/from-snapshot-BMGnhmgv.js +552 -0
  86. package/dist/generate-CH7ThJ14.js +173 -0
  87. package/dist/grantee-Bv04_Vzf.js +521 -0
  88. package/dist/headers-B5RRv3Xm.js +6 -0
  89. package/dist/headers-C8SHQccD.d.ts +6 -0
  90. package/dist/helpers-DnSOrhB9.js +682 -0
  91. package/dist/hono/index.d.ts +70 -0
  92. package/dist/hono/index.js +131 -0
  93. package/dist/hosted-BwKQ1BTA.js +265 -0
  94. package/dist/http-DPuDaChb.d.ts +13 -0
  95. package/dist/http-GgVApX1k.js +111 -0
  96. package/dist/index-CltcQ4Bf.d.ts +138 -0
  97. package/dist/index-LF_hS-yn.d.ts +177 -0
  98. package/dist/index-fG6NSXlc.d.ts +9 -0
  99. package/dist/index-xIxJLwOQ.d.ts +415 -0
  100. package/dist/index.d.ts +11 -0
  101. package/dist/index.js +21 -0
  102. package/dist/jwt/index.d.ts +87 -0
  103. package/dist/jwt/index.js +571 -0
  104. package/dist/kernel-YOGCWNLf.js +211 -0
  105. package/dist/kysely/index.d.ts +77 -0
  106. package/dist/kysely/index.js +129 -0
  107. package/dist/load-BZ24CpZP.js +69 -0
  108. package/dist/load-QOOm1z70.js +2 -0
  109. package/dist/load-jose-BL6LH4bL.js +11 -0
  110. package/dist/lru-8q4WZjBV.js +28 -0
  111. package/dist/manifest-DDQbC6K0.d.ts +118 -0
  112. package/dist/map-DowSO96j.js +158 -0
  113. package/dist/map-claims-B_6I1Rn-.js +360 -0
  114. package/dist/map-error-RJ3ZsUyA.js +16 -0
  115. package/dist/markers-B0hYLvMD.js +60 -0
  116. package/dist/may-use-CnFEjVEp.js +37 -0
  117. package/dist/mcp/index.d.ts +127 -0
  118. package/dist/mcp/index.js +563 -0
  119. package/dist/nest/index.d.ts +80 -0
  120. package/dist/nest/index.js +270 -0
  121. package/dist/next/client.d.ts +24 -0
  122. package/dist/next/client.js +31 -0
  123. package/dist/next/index.d.ts +110 -0
  124. package/dist/next/index.js +195 -0
  125. package/dist/next/plugin.d.ts +14 -0
  126. package/dist/next/plugin.js +2 -0
  127. package/dist/node/index.d.ts +43 -0
  128. package/dist/node/index.js +66 -0
  129. package/dist/openai/index.d.ts +71 -0
  130. package/dist/openai/index.js +105 -0
  131. package/dist/openapi/index.d.ts +93 -0
  132. package/dist/openapi/index.js +3 -0
  133. package/dist/openapi-CwePaF7C.js +164 -0
  134. package/dist/openapi-DWD0Uc_4.js +250 -0
  135. package/dist/openapi-DcqM8nU7.js +306 -0
  136. package/dist/openapi-import-BWRUNNeW.js +5959 -0
  137. package/dist/openapi-schema-D8j_T9tO.js +6902 -0
  138. package/dist/orpc/index.d.ts +64 -0
  139. package/dist/orpc/index.js +188 -0
  140. package/dist/otel/index.d.ts +13 -0
  141. package/dist/otel/index.js +202 -0
  142. package/dist/package-root-zGkxO3oN.js +30 -0
  143. package/dist/parse-DPfIvFMU.js +135 -0
  144. package/dist/pdp/index.d.ts +22 -0
  145. package/dist/pdp/index.js +795 -0
  146. package/dist/peer-BOr_kJE1.js +31 -0
  147. package/dist/permdock-yoZ7EjxX.js +2956 -0
  148. package/dist/permissions-B4apnahQ.d.ts +185 -0
  149. package/dist/permissions-B8vCg_bI.d.ts +52 -0
  150. package/dist/permissions-CofRqesP.js +142 -0
  151. package/dist/permissions-DC7GROPA.js +600 -0
  152. package/dist/pg-4GJWDUxG.js +953 -0
  153. package/dist/pins-CqE1jZuF.js +12 -0
  154. package/dist/plugin-4gr7IxFJ.js +65 -0
  155. package/dist/policy-ANp36PnI.js +971 -0
  156. package/dist/policy-CInmNGkM.d.ts +1959 -0
  157. package/dist/policy-Cq2fB8Q5.d.ts +20 -0
  158. package/dist/prisma/index.d.ts +135 -0
  159. package/dist/prisma/index.js +430 -0
  160. package/dist/problem-Cgp9-KZA.js +226 -0
  161. package/dist/prompt-CXYhLfsS.js +32 -0
  162. package/dist/protected-B4K3BKoX.js +138 -0
  163. package/dist/protected-BuPAycJT.d.ts +26 -0
  164. package/dist/protected-wY7nXhY9.d.ts +23 -0
  165. package/dist/provider-_wx46HpB.js +48 -0
  166. package/dist/provider-wTAUda2v.d.ts +6 -0
  167. package/dist/react/index.d.ts +6 -0
  168. package/dist/react/index.js +6 -0
  169. package/dist/react/provider-client.d.ts +2 -0
  170. package/dist/react/provider-client.js +3 -0
  171. package/dist/react-native/index.d.ts +42 -0
  172. package/dist/react-native/index.js +174 -0
  173. package/dist/refs-ChLSxbST.js +90 -0
  174. package/dist/relations-r4soIFZB.js +426 -0
  175. package/dist/revocations-Cy06JNx2.d.ts +21 -0
  176. package/dist/rls-BOFU3chG.js +233 -0
  177. package/dist/rls-CJVja9Ww.js +2119 -0
  178. package/dist/rls-graph-DxQ39PJj.js +371 -0
  179. package/dist/rls-helpers-BxRuif0l.js +1346 -0
  180. package/dist/rls-import-CXV6LBlS.js +942 -0
  181. package/dist/rls-migrate-DrFPbf8p.js +366 -0
  182. package/dist/row-check-BajTgQZ-.d.ts +43 -0
  183. package/dist/run-CJ6UvS3a.js +155 -0
  184. package/dist/runtime-DP9ttFh1.js +27 -0
  185. package/dist/saas-BRrZzJkM.js +1193 -0
  186. package/dist/schema-P8yoma__.js +296 -0
  187. package/dist/scim/index.d.ts +2 -0
  188. package/dist/scim/index.js +2 -0
  189. package/dist/scim-9NFa0XVZ.js +1445 -0
  190. package/dist/scopes-cKVp_WTw.js +212 -0
  191. package/dist/server/index.d.ts +123 -0
  192. package/dist/server/index.js +8 -0
  193. package/dist/sha256-BO0swRXL.js +150 -0
  194. package/dist/sink-Bd9PSyfq.js +141 -0
  195. package/dist/sink-Cfr9-7YC.d.ts +104 -0
  196. package/dist/skills-C_lpV4Jx.js +50 -0
  197. package/dist/skills-Dx3QLnMY.js +110 -0
  198. package/dist/snapshot-DE8dE_hz.js +1051 -0
  199. package/dist/solid/index.d.ts +63 -0
  200. package/dist/solid/index.js +197 -0
  201. package/dist/source-DoBBYJi2.js +6 -0
  202. package/dist/sources-DOpW5Ila.d.ts +347 -0
  203. package/dist/sources-DVHM3Mwq.js +284 -0
  204. package/dist/sql-Bll1d7y0.js +22 -0
  205. package/dist/src-CTNT-PBW.js +430 -0
  206. package/dist/ssf/index.d.ts +3 -0
  207. package/dist/ssf/index.js +622 -0
  208. package/dist/store-Bq90395i.d.ts +27 -0
  209. package/dist/store-C6hFW6ta.js +508 -0
  210. package/dist/stores-CkG2HrSg.js +140 -0
  211. package/dist/stores-CvOnu_2T.d.ts +41 -0
  212. package/dist/stream-BhMfZOwr.d.ts +12 -0
  213. package/dist/stream-Q_xlB6Ko.js +52 -0
  214. package/dist/style-CTm1AXDT.js +22 -0
  215. package/dist/subject-C6dLxqHU.js +24 -0
  216. package/dist/subject-settings-xt7V845g.js +82 -0
  217. package/dist/supabase/index.d.ts +67 -0
  218. package/dist/supabase/index.js +4 -0
  219. package/dist/supabase/middleware.d.ts +77 -0
  220. package/dist/supabase/middleware.js +68 -0
  221. package/dist/supabase-D8UlBA7Z.js +83 -0
  222. package/dist/supabase-DTxGtYdi.js +796 -0
  223. package/dist/supabase-hook-DW-xP4hU.js +2 -0
  224. package/dist/supabase-hook-D_OqGb_V.js +1528 -0
  225. package/dist/svelte/Protected.svelte +34 -0
  226. package/dist/svelte/index.d.ts +10 -0
  227. package/dist/svelte/index.js +53 -0
  228. package/dist/svelte/runtime.d.ts +6 -0
  229. package/dist/svelte/runtime.js +3 -0
  230. package/dist/svelte/source.d.ts +10 -0
  231. package/dist/svelte/source.js +8 -0
  232. package/dist/terminal/index.d.ts +171 -0
  233. package/dist/terminal/index.js +821 -0
  234. package/dist/testing/index.d.ts +656 -0
  235. package/dist/testing/index.js +3208 -0
  236. package/dist/testing/saas/index.d.ts +194 -0
  237. package/dist/testing/saas/index.js +3 -0
  238. package/dist/testing/saas/permissions.d.ts +2 -0
  239. package/dist/testing/saas/permissions.js +2 -0
  240. package/dist/text-diff-BdkkYSbi.js +15 -0
  241. package/dist/thenable-okc2xDsj.js +10 -0
  242. package/dist/timeout-D51niuQP.js +8 -0
  243. package/dist/trpc/index.d.ts +80 -0
  244. package/dist/trpc/index.js +205 -0
  245. package/dist/types-B8w5PFtS.d.ts +141 -0
  246. package/dist/types-B9dmiiab.d.ts +66 -0
  247. package/dist/types-BBK-exvJ.d.ts +177 -0
  248. package/dist/types-BvBk1wgn.d.ts +118 -0
  249. package/dist/types-CxquUByI.d.ts +9 -0
  250. package/dist/types-D6Z6-rKJ.d.ts +312 -0
  251. package/dist/types-DbWdEfcm.d.ts +87 -0
  252. package/dist/types-bva3i4Gf.d.ts +98 -0
  253. package/dist/types-i4BAOvdb.d.ts +97 -0
  254. package/dist/unplugin/index.d.ts +7 -0
  255. package/dist/unplugin/index.js +30 -0
  256. package/dist/usage-1ozMubLY.js +42 -0
  257. package/dist/usage-CUnK6Ou9.js +530 -0
  258. package/dist/usage-PyEor6GA.js +2 -0
  259. package/dist/validity-D0uRmJKJ.js +224 -0
  260. package/dist/verifier-B90agnhl.js +461 -0
  261. package/dist/version-CnSh33Fj.js +16 -0
  262. package/dist/vocabulary-Dk0fs-Jk.d.ts +56 -0
  263. package/dist/vue/index.d.ts +57 -0
  264. package/dist/vue/index.js +203 -0
  265. package/dist/webmcp/index.d.ts +75 -0
  266. package/dist/webmcp/index.js +313 -0
  267. package/package.json +423 -4
  268. package/rulesets/permdock.yaml +29 -0
  269. package/schemas/catalog-v1.json +507 -0
  270. package/schemas/openapi/oas-3.1.json +1298 -0
  271. package/schemas/openapi/oas-3.2.json +1516 -0
  272. package/schemas/openapi/overlay-1.1.json +79 -0
  273. package/schemas/supabase-claims-v1.json +139 -0
  274. package/schemas/supabase-manifest-v1.json +217 -0
  275. package/skills/permdock/SKILL.md +66 -0
  276. package/skills/permdock-agents/SKILL.md +77 -0
  277. package/skills/permdock-agents/references/runtimes.md +211 -0
  278. package/skills/permdock-approvals/SKILL.md +80 -0
  279. package/skills/permdock-approvals/references/stores-and-resume.md +83 -0
  280. package/skills/permdock-audit/SKILL.md +67 -0
  281. package/skills/permdock-credentials/SKILL.md +55 -0
  282. package/skills/permdock-credentials/references/tokens-keys-links.md +127 -0
  283. package/skills/permdock-data/SKILL.md +57 -0
  284. package/skills/permdock-data/references/queries-and-rls.md +54 -0
  285. package/skills/permdock-data/references/relationships.md +65 -0
  286. package/skills/permdock-tenancy/SKILL.md +77 -0
  287. package/skills/permdock-tenancy/references/ownership-and-custom-roles.md +83 -0
  288. package/skills/permdock-wire/SKILL.md +116 -0
  289. package/skills/permdock-wire/references/adapters.md +480 -0
  290. package/skills/permdock-wire/references/policy-options.md +43 -0
@@ -0,0 +1,211 @@
1
+ import { t as compact } from "./compact-y6vfu2_f.js";
2
+ import { t as describe } from "./describe-Bt1YD5uS.js";
3
+ import { o as deniedMessage } from "./errors-Iy71s7Oj.js";
4
+ import { t as createPermDock } from "./permdock-yoZ7EjxX.js";
5
+ import { t as mayUse } from "./may-use-CnFEjVEp.js";
6
+ import { l as storedApprovalToken, s as resumeDecision } from "./helpers-DnSOrhB9.js";
7
+ //#region src/agent/reason.ts
8
+ function modelReason(decision, permission, subjectId) {
9
+ if (decision.outcome === "denied") return deniedMessage(permission?.key ?? "unknown", subjectId, decision.denials, decision.alternatives.map((leaf) => leaf.key));
10
+ return describe(decision).detail;
11
+ }
12
+ function unmappedReason(toolName) {
13
+ return `Denied: unmapped tool ${toolName}.`;
14
+ }
15
+ function thrownReason(toolName) {
16
+ return `Denied: ${toolName} failed closed.`;
17
+ }
18
+ //#endregion
19
+ //#region src/agent/kernel.ts
20
+ function asActor(value) {
21
+ if (value === null || typeof value !== "object" || Array.isArray(value)) return;
22
+ const record = value;
23
+ if (typeof record.id !== "string" || typeof record.kind !== "string") return;
24
+ return {
25
+ id: record.id,
26
+ kind: record.kind
27
+ };
28
+ }
29
+ async function resolveTenant(tenant, context) {
30
+ if (tenant === void 0 || typeof tenant === "string") return tenant;
31
+ try {
32
+ return await tenant(context);
33
+ } catch {
34
+ return;
35
+ }
36
+ }
37
+ /** The resume token an agent run carries under the namespaced `permdockApproval` key. */
38
+ function approvalTokenOf(context) {
39
+ if (context === null || typeof context !== "object") return;
40
+ const token = context.permdockApproval;
41
+ return typeof token === "string" && token !== "" ? token : void 0;
42
+ }
43
+ function idOf(data) {
44
+ if (data === null || typeof data !== "object") return;
45
+ const id = data.id;
46
+ if (typeof id === "string" || typeof id === "number") return String(id);
47
+ }
48
+ function resourceRef(permission, data) {
49
+ return compact({
50
+ type: permission.resource,
51
+ id: idOf(data)
52
+ });
53
+ }
54
+ function runDecide(permdock, permission, data, adapter) {
55
+ return permdock.decide(permission, data, compact({
56
+ source: "adapter",
57
+ adapter,
58
+ boundary: "tool-args"
59
+ }));
60
+ }
61
+ function createAgentKernel(policy, options) {
62
+ const cache = /* @__PURE__ */ new WeakMap();
63
+ const instance = (context) => {
64
+ if (typeof context === "object" && context !== null) {
65
+ const hit = cache.get(context);
66
+ if (hit !== void 0) return hit;
67
+ }
68
+ const built = (async () => {
69
+ let user = null;
70
+ try {
71
+ user = await options.subject(context);
72
+ } catch {
73
+ user = null;
74
+ }
75
+ let actor;
76
+ if (options.actor !== void 0) try {
77
+ actor = asActor(await options.actor(context));
78
+ } catch {
79
+ actor = void 0;
80
+ }
81
+ let delegation;
82
+ if (options.delegation !== void 0) try {
83
+ delegation = await options.delegation(context);
84
+ } catch {
85
+ delegation = void 0;
86
+ }
87
+ const tenant = await resolveTenant(options.tenant, context);
88
+ return createPermDock(policy, user, compact({
89
+ tenant,
90
+ actor,
91
+ delegation,
92
+ memberships: options.memberships,
93
+ relations: options.relations,
94
+ entitlements: options.entitlements,
95
+ customRoles: options.customRoles,
96
+ policies: options.policies,
97
+ sink: options.sink,
98
+ limits: options.limits
99
+ }));
100
+ })();
101
+ if (typeof context === "object" && context !== null) {
102
+ cache.set(context, built);
103
+ const release = () => {
104
+ if (cache.get(context) === built) cache.delete(context);
105
+ };
106
+ built.then(release, release);
107
+ }
108
+ return built;
109
+ };
110
+ const evaluate = async (binding, toolName, args, context, decideOptions = {}) => {
111
+ try {
112
+ const permdock = await instance(context);
113
+ let data;
114
+ if (binding.data !== void 0) {
115
+ data = await binding.data(args);
116
+ if (data === null || data === void 0) {
117
+ const decision = {
118
+ outcome: "denied",
119
+ denials: [{
120
+ role: null,
121
+ reason: "validation"
122
+ }],
123
+ alternatives: []
124
+ };
125
+ return {
126
+ outcome: "denied",
127
+ decision,
128
+ permission: binding.permission,
129
+ reason: modelReason(decision, binding.permission, permdock.subject.principal?.id)
130
+ };
131
+ }
132
+ }
133
+ const raw = runDecide(permdock, binding.permission, data, options.adapter);
134
+ const decision = await resumeDecision({
135
+ decision: raw,
136
+ permission: binding.permission,
137
+ subject: permdock.subject,
138
+ store: options.store,
139
+ resource: resourceRef(binding.permission, data),
140
+ adapter: options.adapter,
141
+ token: decideOptions.resumeToken ?? await storedApprovalToken(options.store, raw, decideOptions.denyPending === true)
142
+ });
143
+ if (decision.outcome === "granted") return {
144
+ outcome: "granted",
145
+ decision,
146
+ permission: binding.permission,
147
+ data
148
+ };
149
+ if (decision.outcome === "approval-required") return {
150
+ outcome: "approval-required",
151
+ decision,
152
+ permission: binding.permission,
153
+ data,
154
+ token: decision.token,
155
+ summary: modelReason(decision, binding.permission, permdock.subject.principal?.id)
156
+ };
157
+ return {
158
+ outcome: "denied",
159
+ decision,
160
+ permission: binding.permission,
161
+ reason: modelReason(decision, binding.permission, permdock.subject.principal?.id)
162
+ };
163
+ } catch {
164
+ return {
165
+ outcome: "denied",
166
+ decision: null,
167
+ permission: binding.permission,
168
+ reason: thrownReason(toolName)
169
+ };
170
+ }
171
+ };
172
+ const tokenFor = async (binding, args, context) => {
173
+ try {
174
+ const permdock = await instance(context);
175
+ let data;
176
+ if (binding.data !== void 0) {
177
+ data = await binding.data(args);
178
+ if (data === null || data === void 0) return;
179
+ }
180
+ const raw = runDecide(permdock, binding.permission, data, options.adapter);
181
+ return raw.outcome === "approval-required" ? raw.token : void 0;
182
+ } catch {
183
+ return;
184
+ }
185
+ };
186
+ const decideTool = (toolName, args, context, decideOptions = {}) => {
187
+ const binding = options.tools[toolName];
188
+ if (binding === void 0) return Promise.resolve({
189
+ outcome: "denied",
190
+ decision: null,
191
+ permission: void 0,
192
+ reason: unmappedReason(toolName)
193
+ });
194
+ return evaluate(binding, toolName, args, context, decideOptions);
195
+ };
196
+ const allowedToolNames = async (context) => {
197
+ const permdock = await instance(context);
198
+ const allowed = /* @__PURE__ */ new Set();
199
+ for (const [name, binding] of Object.entries(options.tools)) if (mayUse(permdock, binding.permission)) allowed.add(name);
200
+ return allowed;
201
+ };
202
+ return {
203
+ instance,
204
+ decideTool,
205
+ evaluate,
206
+ tokenFor,
207
+ allowedToolNames
208
+ };
209
+ }
210
+ //#endregion
211
+ export { createAgentKernel as n, approvalTokenOf as t };
@@ -0,0 +1,77 @@
1
+ import { _r as Condition, hr as Subject, tn as WhereResult } from "../policy-CInmNGkM.js";
2
+ import { n as WithSubjectOptions, r as RelationsMapping, t as RowCheck } from "../row-check-BajTgQZ-.js";
3
+ import { t as MembershipsMapping } from "../compile-CQbvyIBr.js";
4
+ //#region src/kysely/types.d.ts
5
+ type KyselySelectQuery = {
6
+ select(expr: unknown): KyselySelectQuery;
7
+ where(column: string, op: string, value: unknown): KyselySelectQuery;
8
+ where(expression: unknown): KyselySelectQuery;
9
+ whereRef(left: string, op: string, right: string): KyselySelectQuery;
10
+ };
11
+ type KyselyExpressionBuilder = {
12
+ (left: unknown, op: string, right: unknown): unknown;
13
+ and(args: readonly unknown[]): unknown;
14
+ or(args: readonly unknown[]): unknown;
15
+ not(value: unknown): unknown;
16
+ lit(value: unknown): unknown;
17
+ val(value: unknown): unknown;
18
+ ref(column: string): unknown;
19
+ exists?(query: unknown): unknown;
20
+ selectFrom?(table: string): KyselySelectQuery;
21
+ };
22
+ /** Kysely's `sql` tag; `import { sql } from 'kysely'`. */
23
+ type KyselySql = {
24
+ (strings: TemplateStringsArray, ...values: unknown[]): unknown;
25
+ ref(reference: string): unknown;
26
+ };
27
+ type KyselyWhereOptions = {
28
+ readonly columns?: Readonly<Record<string, string>>;
29
+ /** Condition fields that are Postgres arrays; `contains` on them is `@>`. */
30
+ readonly listFields?: readonly string[];
31
+ readonly subject?: Subject;
32
+ readonly memberships?: MembershipsMapping;
33
+ /** Where the relation graph lives; graph grants compile to Postgres subqueries with it and are refused without. */
34
+ readonly relations?: RelationsMapping;
35
+ readonly now?: number;
36
+ /** Kysely's `sql` tag, for graph subqueries on runtimes without `require`; loaded from the `kysely` peer by default. */
37
+ readonly sql?: KyselySql;
38
+ };
39
+ type KyselyWithSubjectOptions = WithSubjectOptions & {
40
+ /** Kysely's `sql` tag, on runtimes without `require`; loaded from the `kysely` peer by default. */
41
+ readonly sql?: KyselySql;
42
+ };
43
+ //#endregion
44
+ //#region src/kysely/to-where.d.ts
45
+ export declare function toWhere(input: Condition | WhereResult, table: string, options?: KyselyWhereOptions): (eb: KyselyExpressionBuilder) => unknown;
46
+ type KyselyLike<Trx> = {
47
+ transaction(): {
48
+ execute<T>(fn: (trx: Trx) => Promise<T>): Promise<T>;
49
+ };
50
+ };
51
+ /**
52
+ * Runs `fn` in a transaction whose role and claims are `permdock.subject`'s, so the generated
53
+ * RLS policies decide for the same subject as the in-process checks.
54
+ */
55
+ export declare function withSubject<Trx, T>(db: KyselyLike<Trx>, permdock: {
56
+ readonly subject: Subject;
57
+ }, fn: (trx: Trx) => Promise<T>, options?: KyselyWithSubjectOptions): Promise<T>;
58
+ type KyselySelectable = {
59
+ selectFrom(table: never): {
60
+ select(selection: (eb: never) => unknown): {
61
+ where(where: never): {
62
+ limit(count: number): {
63
+ execute(): Promise<readonly {
64
+ readonly granted?: unknown;
65
+ }[]>;
66
+ };
67
+ };
68
+ };
69
+ };
70
+ };
71
+ /**
72
+ * One query for one row: `{ found: false }` when `key` matches nothing, otherwise whether the
73
+ * permission filter keeps the row. Throws when `key` matches more than one row.
74
+ */
75
+ export declare function checkRow(db: KyselySelectable, table: string, input: Condition | WhereResult, key: (eb: KyselyExpressionBuilder) => unknown, options?: KyselyWhereOptions): Promise<RowCheck>;
76
+ //#endregion
77
+ export type { KyselyExpressionBuilder, KyselySql, KyselyWhereOptions, KyselyWithSubjectOptions, MembershipsMapping, RelationsMapping, RowCheck, WithSubjectOptions };
@@ -0,0 +1,129 @@
1
+ import { t as compact } from "../compact-y6vfu2_f.js";
2
+ import { x as assertSafeKey } from "../permissions-DC7GROPA.js";
3
+ import { n as escapeLike, t as compileWhere } from "../compile-D22iwvuV.js";
4
+ import { i as rowCheckFrom, r as subjectStatements, t as statementTemplate } from "../subject-settings-xt7V845g.js";
5
+ //#region src/kysely/to-where.ts
6
+ function ident(name) {
7
+ assertSafeKey(name, "sql identifier");
8
+ if (!/^[A-Za-z_][A-Za-z0-9_]*$/u.test(name)) throw new Error(`PermDock: unsafe SQL identifier '${name}'`);
9
+ return name;
10
+ }
11
+ function col(table, field, columns) {
12
+ assertSafeKey(field, "condition field");
13
+ return `${ident(table)}.${ident(columns?.[field] ?? field)}`;
14
+ }
15
+ function existsExpr(eb, node, table, options) {
16
+ if (eb.exists === void 0 || eb.selectFrom === void 0) return eb.lit(false);
17
+ const m = (name) => `m.${ident(name)}`;
18
+ let query = eb.selectFrom(`${ident(node.table)} as m`).select(eb.lit(1)).whereRef(m(node.rowColumn), "=", col(table, node.rowField, options.columns)).where(m(node.user), "=", eb.val(node.userValue));
19
+ if (node.roles.length > 0) query = query.where(m(node.role), "in", node.roles);
20
+ if (node.expiresAt !== void 0) {
21
+ const expires = eb.ref(m(node.expiresAt));
22
+ query = query.where(eb.or([eb(expires, "is", null), eb(expires, ">", eb.val(node.now))]));
23
+ }
24
+ if (node.tenantColumn !== void 0 && node.tenantValue !== void 0) {
25
+ const tenant = eb.ref(m(node.tenantColumn));
26
+ query = query.where(eb.or([eb(tenant, "is", null), eb(tenant, "=", eb.val(node.tenantValue))]));
27
+ }
28
+ if (node.resourceColumn !== void 0 && node.resourceValue !== void 0) query = query.where(m(node.resourceColumn), "=", eb.val(node.resourceValue));
29
+ return eb.exists(query);
30
+ }
31
+ function containsExpr(eb, column, field, value, options) {
32
+ if (options.listFields?.includes(field) === true) return eb(column, "@>", eb.val([value]));
33
+ return typeof value === "string" ? eb(column, "like", eb.val(`%${escapeLike(value)}%`)) : eb(column, "@>", eb.val(value));
34
+ }
35
+ function loadSql(injected) {
36
+ if (injected !== void 0) return injected;
37
+ const loader = globalThis.process?.getBuiltinModule?.("node:module");
38
+ try {
39
+ if (loader === void 0) throw new Error("no module loader");
40
+ return loader.createRequire(import.meta.url)("kysely").sql;
41
+ } catch {
42
+ throw new Error("PermDock: graph grants and withSubject in permdock/kysely need the kysely peer; pass `sql` on runtimes without require");
43
+ }
44
+ }
45
+ function graphExpr(node, table, options) {
46
+ const sql = loadSql(options.sql);
47
+ const strings = [""];
48
+ const values = [];
49
+ for (const part of node.parts) {
50
+ if ("text" in part) {
51
+ strings[strings.length - 1] += part.text;
52
+ continue;
53
+ }
54
+ values.push("column" in part ? sql.ref(col(table, part.column, options.columns)) : "subject" in part ? node.subject : part.value);
55
+ strings.push("");
56
+ }
57
+ const template = strings;
58
+ Object.defineProperty(template, "raw", { value: strings });
59
+ return sql(template, ...values);
60
+ }
61
+ function render(eb, node, table, options) {
62
+ switch (node.kind) {
63
+ case "never": return eb.lit(false);
64
+ case "always": return eb.lit(true);
65
+ case "isNull": {
66
+ const column = eb.ref(col(table, node.field, options.columns));
67
+ return node.negated ? eb(column, "is not", null) : eb(column, "is", null);
68
+ }
69
+ case "and": return eb.and(node.items.map((item) => render(eb, item, table, options)));
70
+ case "or": return eb.or(node.items.map((item) => render(eb, item, table, options)));
71
+ case "not": return eb.not(render(eb, node.item, table, options));
72
+ case "exists": return existsExpr(eb, node, table, options);
73
+ case "sql": return graphExpr(node, table, options);
74
+ case "compare": {
75
+ const column = eb.ref(col(table, node.field, options.columns));
76
+ switch (node.op) {
77
+ case "eq": return eb(column, "=", eb.val(node.value));
78
+ case "ne": return eb(column, "!=", eb.val(node.value));
79
+ case "gt": return eb(column, ">", eb.val(node.value));
80
+ case "gte": return eb(column, ">=", eb.val(node.value));
81
+ case "lt": return eb(column, "<", eb.val(node.value));
82
+ case "lte": return eb(column, "<=", eb.val(node.value));
83
+ case "in": return eb(column, "in", node.value);
84
+ case "notIn": return eb(column, "not in", node.value);
85
+ case "contains": return containsExpr(eb, column, node.field, node.value, options);
86
+ /* v8 ignore next 4 */
87
+ default: {
88
+ const exhaustive = node.op;
89
+ throw new Error(`PermDock: unknown compare '${String(exhaustive)}'`);
90
+ }
91
+ }
92
+ }
93
+ /* v8 ignore next 6 */
94
+ default: throw new Error(`PermDock: unknown compiled node '${String(node)}'`);
95
+ }
96
+ }
97
+ function toWhere(input, table, options = {}) {
98
+ const compiled = compileWhere(input, compact({
99
+ subject: options.subject,
100
+ memberships: options.memberships,
101
+ now: options.now,
102
+ relations: options.relations
103
+ }));
104
+ return (eb) => render(eb, compiled, table, options);
105
+ }
106
+ /**
107
+ * Runs `fn` in a transaction whose role and claims are `permdock.subject`'s, so the generated
108
+ * RLS policies decide for the same subject as the in-process checks.
109
+ */
110
+ function withSubject(db, permdock, fn, options = {}) {
111
+ const statements = subjectStatements(permdock, options);
112
+ const sql = loadSql(options.sql);
113
+ return db.transaction().execute(async (trx) => {
114
+ for (const statement of statements) await sql(statementTemplate(statement), ...statement.values).execute(trx);
115
+ return fn(trx);
116
+ });
117
+ }
118
+ /**
119
+ * One query for one row: `{ found: false }` when `key` matches nothing, otherwise whether the
120
+ * permission filter keeps the row. Throws when `key` matches more than one row.
121
+ */
122
+ async function checkRow(db, table, input, key, options = {}) {
123
+ const sql = loadSql(options.sql);
124
+ const filter = toWhere(input, table, options);
125
+ const rows = await db.selectFrom(table).select(((eb) => sql`coalesce(${filter(eb)}, false)`.as("granted"))).where(key).limit(2).execute();
126
+ return rowCheckFrom(rows);
127
+ }
128
+ //#endregion
129
+ export { checkRow, toWhere, withSubject };
@@ -0,0 +1,69 @@
1
+ import { g as listPermissions } from "./permissions-DC7GROPA.js";
2
+ import { dirname, resolve } from "node:path";
3
+ import { pathToFileURL } from "node:url";
4
+ import { createJiti } from "jiti";
5
+ //#region src/cli/load.ts
6
+ /**
7
+ * Errors Node raises while resolving or parsing a module graph, before any of
8
+ * its code runs, so loading it again through jiti cannot repeat a side effect.
9
+ */
10
+ const NOT_NATIVE = /* @__PURE__ */ new Set([
11
+ "ERR_MODULE_NOT_FOUND",
12
+ "ERR_UNKNOWN_FILE_EXTENSION",
13
+ "ERR_UNSUPPORTED_DIR_IMPORT",
14
+ "ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX",
15
+ "ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING"
16
+ ]);
17
+ function loadsWithJiti(error) {
18
+ if (error instanceof SyntaxError) return true;
19
+ const code = error !== null && typeof error === "object" && "code" in error ? error.code : void 0;
20
+ return typeof code === "string" && NOT_NATIVE.has(code);
21
+ }
22
+ /**
23
+ * Imports a project module the way its bundler would. Node's own `import()`
24
+ * comes first; a module it cannot resolve or parse (tsconfig `paths`
25
+ * aliases, extensionless relative imports, `enum`, TSX) loads through jiti
26
+ * with the `paths` of the nearest `tsconfig.json` above it. Exports are
27
+ * returned as written; `default` is not merged into the namespace.
28
+ */
29
+ async function loadModule(abs) {
30
+ let loaded;
31
+ try {
32
+ loaded = await import(pathToFileURL(abs).href);
33
+ } catch (error) {
34
+ if (!loadsWithJiti(error)) throw error;
35
+ loaded = await createJiti(import.meta.url, {
36
+ interopDefault: false,
37
+ jsx: true,
38
+ tsconfigPaths: dirname(abs)
39
+ }).import(abs);
40
+ }
41
+ if (loaded === null || typeof loaded !== "object") throw new Error(`PermDock CLI: module '${abs}' did not export an object`);
42
+ return loaded;
43
+ }
44
+ function pickNamed(mod, names) {
45
+ for (const name of names) if (name in mod) return mod[name];
46
+ return mod["default"];
47
+ }
48
+ function asPermissionTree(value) {
49
+ if (value === null || typeof value !== "object") throw new Error("PermDock CLI: permissions export is not a permission tree");
50
+ return value;
51
+ }
52
+ function asPolicy(value) {
53
+ if (value === null || typeof value !== "object" || !("roles" in value) || !("permissions" in value)) throw new Error("PermDock CLI: policy export is not a Policy");
54
+ return value;
55
+ }
56
+ function leavesOf(tree) {
57
+ return listPermissions(tree);
58
+ }
59
+ /** The configured policy export, or `undefined` when unset or unloadable. */
60
+ async function loadConfiguredPolicy(cwd, path) {
61
+ if (path === void 0) return;
62
+ try {
63
+ return asPolicy(pickNamed(await loadModule(resolve(cwd, path)), ["policy"]));
64
+ } catch {
65
+ return;
66
+ }
67
+ }
68
+ //#endregion
69
+ export { loadModule as a, loadConfiguredPolicy as i, asPolicy as n, pickNamed as o, leavesOf as r, asPermissionTree as t };
@@ -0,0 +1,2 @@
1
+ import { a as loadModule, o as pickNamed } from "./load-BZ24CpZP.js";
2
+ export { loadModule, pickNamed };
@@ -0,0 +1,11 @@
1
+ //#region src/jwt/load-jose.ts
2
+ let cached;
3
+ function loadJose() {
4
+ cached ??= import("jose").catch((cause) => {
5
+ cached = void 0;
6
+ throw new Error("PermDock: permdock/jwt requires the optional peer \"jose\".", { cause });
7
+ });
8
+ return cached;
9
+ }
10
+ //#endregion
11
+ export { loadJose as t };
@@ -0,0 +1,28 @@
1
+ //#region src/agent/lru.ts
2
+ function boundedMap(limit) {
3
+ const map = /* @__PURE__ */ new Map();
4
+ return {
5
+ get: (key) => {
6
+ const value = map.get(key);
7
+ if (value !== void 0) {
8
+ map.delete(key);
9
+ map.set(key, value);
10
+ }
11
+ return value;
12
+ },
13
+ set: (key, value) => {
14
+ map.delete(key);
15
+ map.set(key, value);
16
+ if (map.size > limit) {
17
+ const oldest = map.keys().next();
18
+ if (oldest.done !== true) map.delete(oldest.value);
19
+ }
20
+ },
21
+ delete: (key) => {
22
+ map.delete(key);
23
+ },
24
+ size: () => map.size
25
+ };
26
+ }
27
+ //#endregion
28
+ export { boundedMap as t };
@@ -0,0 +1,118 @@
1
+ //#region src/supabase/manifest.d.ts
2
+ /**
3
+ * What `permdock supabase inspect --json` prints and `--out` writes to
4
+ * `permdock.manifest.json`: the contract between the generated hook and SQL
5
+ * helpers and a package that writes policies or claims next to them
6
+ * (better-supabase). `version` is the major of this shape; a field is only
7
+ * ever added within it. `schemas/supabase-manifest-v1.json` is its JSON Schema.
8
+ */
9
+ type SupabaseHookManifest = {
10
+ readonly $schema: "https://permdock.dev/schemas/supabase-manifest-v1.json";
11
+ readonly version: 1;
12
+ readonly hook: {
13
+ readonly schema: string;
14
+ readonly function: "custom_access_token_hook";
15
+ readonly out: string;
16
+ };
17
+ readonly helpers: {
18
+ readonly schema: string;
19
+ /**
20
+ * `permdock_has` and, per declared scope, `permitted_<scope>_ids`, each
21
+ * `(p_grant text)`, the membership-only `member_<scope>_ids()` and, for a
22
+ * scope with a membership source, `member_<scope>_ids_for(p_user uuid)`.
23
+ */
24
+ readonly functions: readonly string[];
25
+ };
26
+ readonly tenantClaim: string;
27
+ readonly budget: {
28
+ readonly bytes: number;
29
+ readonly measure: string;
30
+ };
31
+ readonly claims: readonly SupabaseHookClaim[];
32
+ readonly authzVersion: boolean;
33
+ /**
34
+ * With `authzVersion`, the function that bumps the listed users' `authz_ver`,
35
+ * for a membership source outside the hook. It is `security definer` and no
36
+ * client role may execute it: call it from a trigger owned by the table's owner.
37
+ */
38
+ readonly authzVersionBump?: {
39
+ readonly schema: string;
40
+ readonly function: "permdock_bump_authz_version_for";
41
+ readonly args: "p_users uuid[]";
42
+ };
43
+ /** The `supabase.hook.memberships` sources, in the order the hook reads them. */
44
+ readonly memberships: readonly SupabaseManifestMembership[];
45
+ readonly rls: SupabaseManifestRls;
46
+ /**
47
+ * Every `schema.table.column` a membership or an `attrs` claim is computed
48
+ * from. A client role that can insert or update one can grant itself access.
49
+ */
50
+ readonly decidingColumns: readonly string[];
51
+ /** The majors of the `-- permdock:hook` and `-- permdock:grants` marker lines. */
52
+ readonly markers: {
53
+ readonly hook: "v1";
54
+ readonly grants: "v1";
55
+ };
56
+ };
57
+ type SupabaseHookClaim = {
58
+ readonly name: string;
59
+ /** `permdock`, or the `<schema>.<function>` of a `supabase.hook.claims` entry. */
60
+ readonly source: string;
61
+ /** Whether the claim counts toward `budget.bytes`. */
62
+ readonly budget: boolean;
63
+ };
64
+ type SupabaseManifestColumn = {
65
+ readonly column: string;
66
+ };
67
+ /** A column of the source table, or a value every row has. */
68
+ type SupabaseManifestValue = SupabaseManifestColumn | {
69
+ readonly value: string | readonly string[];
70
+ };
71
+ /** One `fromTable` or `fromJunction` source. */
72
+ type SupabaseManifestMembership = {
73
+ /** `schema.table`, `public` when the source names no schema. */
74
+ readonly table: string;
75
+ readonly user: SupabaseManifestColumn;
76
+ /** A column for `fromTable`, the fixed scope for `fromJunction`. */
77
+ readonly scope: SupabaseManifestValue;
78
+ readonly id: SupabaseManifestColumn;
79
+ /** A role column, or the fixed roles every row holds. */
80
+ readonly role: SupabaseManifestValue;
81
+ /** A `jsonb` column of ancestor ids keyed by scope, or one column per ancestor scope. */
82
+ readonly within?: SupabaseManifestColumn | {
83
+ readonly columns: Readonly<Record<string, string>>;
84
+ };
85
+ readonly via?: SupabaseManifestValue;
86
+ readonly expiresAt?: SupabaseManifestColumn;
87
+ /** The columns of `table` that decide the membership. */
88
+ readonly columns: readonly string[];
89
+ };
90
+ type SupabaseManifestHelper = {
91
+ readonly name: string;
92
+ /** The argument list, `p_grant text`, or `''` for none. */
93
+ readonly args: string;
94
+ readonly returns: string;
95
+ /** The roles granted `execute`; a field view `anon` reads adds `anon` to the `authenticated` ones. */
96
+ readonly execute: readonly string[];
97
+ };
98
+ type SupabaseManifestRls = {
99
+ readonly schema: string;
100
+ /** Where the helpers read roles and memberships: the claims (`jwt`) or the tables (`database`). */
101
+ readonly mode: "jwt" | "database";
102
+ readonly tenantClaim: string;
103
+ /** The declared scopes, root first, with the Postgres type of their ids. */
104
+ readonly scopes: readonly {
105
+ readonly name: string;
106
+ readonly type: string;
107
+ readonly within?: string;
108
+ }[];
109
+ readonly helpers: readonly SupabaseManifestHelper[];
110
+ /**
111
+ * The tables `member_<scope>_ids_for` reads: an `rls.memberships` table
112
+ * mapped for the scope, else `rls.membershipSources`, else the hook's
113
+ * `memberships`.
114
+ */
115
+ readonly memberships: readonly SupabaseManifestMembership[];
116
+ };
117
+ //#endregion
118
+ export { SupabaseManifestMembership as a, SupabaseManifestHelper as i, SupabaseHookManifest as n, SupabaseManifestRls as o, SupabaseManifestColumn as r, SupabaseManifestValue as s, SupabaseHookClaim as t };