@inkeep/agents-core 0.58.20 → 0.59.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 (42) hide show
  1. package/dist/auth/auth-config-utils.d.ts +49 -0
  2. package/dist/auth/auth-config-utils.js +133 -0
  3. package/dist/auth/auth-schema.d.ts +102 -85
  4. package/dist/auth/auth-schema.js +1 -0
  5. package/dist/auth/auth-types.d.ts +170 -0
  6. package/dist/auth/auth-types.js +53 -0
  7. package/dist/auth/auth-validation-schemas.d.ts +186 -152
  8. package/dist/auth/auth.d.ts +43 -1286
  9. package/dist/auth/auth.js +61 -70
  10. package/dist/auth/cookie-names.d.ts +7 -0
  11. package/dist/auth/cookie-names.js +13 -0
  12. package/dist/auth/email-send-status-store.js +15 -3
  13. package/dist/auth/init.js +2 -1
  14. package/dist/auth/password-reset-link-store.js +8 -1
  15. package/dist/auth/permissions.d.ts +13 -13
  16. package/dist/data-access/index.d.ts +4 -3
  17. package/dist/data-access/index.js +3 -3
  18. package/dist/data-access/manage/contextConfigs.d.ts +12 -12
  19. package/dist/data-access/manage/triggers.d.ts +2 -2
  20. package/dist/data-access/runtime/apiKeys.d.ts +4 -4
  21. package/dist/data-access/runtime/apps.d.ts +4 -4
  22. package/dist/data-access/runtime/auth.d.ts +9 -9
  23. package/dist/data-access/runtime/auth.js +19 -21
  24. package/dist/data-access/runtime/conversations.d.ts +4 -4
  25. package/dist/data-access/runtime/messages.d.ts +9 -9
  26. package/dist/data-access/runtime/organizations.d.ts +28 -4
  27. package/dist/data-access/runtime/organizations.js +131 -9
  28. package/dist/data-access/runtime/scheduledTriggerInvocations.d.ts +3 -3
  29. package/dist/data-access/runtime/tasks.d.ts +2 -2
  30. package/dist/db/manage/manage-schema.d.ts +361 -361
  31. package/dist/db/runtime/runtime-schema.d.ts +302 -302
  32. package/dist/index.d.ts +5 -3
  33. package/dist/index.js +4 -3
  34. package/dist/middleware/no-auth.d.ts +2 -2
  35. package/dist/utils/error.d.ts +51 -48
  36. package/dist/utils/error.js +3 -0
  37. package/dist/validation/schemas.d.ts +1641 -1641
  38. package/drizzle/runtime/0023_lazy_energizer.sql +1 -0
  39. package/drizzle/runtime/0024_moaning_kingpin.sql +1 -0
  40. package/drizzle/runtime/meta/0024_snapshot.json +4270 -0
  41. package/drizzle/runtime/meta/_journal.json +7 -0
  42. package/package.json +16 -3
@@ -0,0 +1,49 @@
1
+ import { AgentsRunDatabaseClient } from "../db/runtime/runtime-client.js";
2
+
3
+ //#region src/auth/auth-config-utils.d.ts
4
+ declare function getInitialOrganization(dbClient: AgentsRunDatabaseClient, userId: string): Promise<{
5
+ id: string;
6
+ } | null>;
7
+ /**
8
+ * Build the list of trusted origins for Better Auth.
9
+ * Includes static origins from env, SSO provider issuers from the DB,
10
+ * and (for /sso/register POST requests) the issuer from the request body
11
+ * so OIDC discovery is trusted before the provider is persisted.
12
+ */
13
+ declare function getTrustedOrigins(dbClient: AgentsRunDatabaseClient, request: Request | undefined): Promise<string[]>;
14
+ /**
15
+ * Extracts the root domain from a URL for cross-subdomain cookie sharing.
16
+ *
17
+ * When the API and UI share a common 3-part parent (e.g., api.pilot.inkeep.com
18
+ * and pilot.inkeep.com both share .pilot.inkeep.com), the function auto-computes
19
+ * the shared parent. When domains don't share a 3-part parent (e.g.,
20
+ * api.agents.inkeep.com and app.inkeep.com), set AUTH_COOKIE_DOMAIN explicitly.
21
+ *
22
+ * Examples (auto-computed from baseURL):
23
+ * - https://api.pilot.inkeep.com -> .pilot.inkeep.com
24
+ * - https://pilot.inkeep.com -> .pilot.inkeep.com
25
+ * - http://localhost:3002 -> undefined (no domain for localhost)
26
+ *
27
+ * With AUTH_COOKIE_DOMAIN=.inkeep.com:
28
+ * - Any *.inkeep.com URL -> .inkeep.com
29
+ */
30
+ declare function extractCookieDomain(baseURL: string, explicitDomain?: string): string | undefined;
31
+ declare function hasCredentialAccount(dbClient: AgentsRunDatabaseClient, userId: string): Promise<boolean>;
32
+ /**
33
+ * Checks whether an SSO user should be auto-provisioned into an organization.
34
+ * Reads the per-provider `autoProvision` flag from `allowedAuthMethods` JSON.
35
+ * Returns false if:
36
+ * - The provider has no organizationId or providerId
37
+ * - The organization doesn't exist
38
+ * - The SSO provider entry has autoProvision disabled (or is missing)
39
+ * - The user is already a member
40
+ */
41
+ declare function shouldAutoProvision(dbClient: AgentsRunDatabaseClient, user: {
42
+ id: string;
43
+ email: string;
44
+ }, provider: {
45
+ organizationId?: string | null;
46
+ providerId?: string;
47
+ }): Promise<boolean>;
48
+ //#endregion
49
+ export { extractCookieDomain, getInitialOrganization, getTrustedOrigins, hasCredentialAccount, shouldAutoProvision };
@@ -0,0 +1,133 @@
1
+ import { env } from "../env.js";
2
+ import { getInitialOrganization as getInitialOrganization$1, queryHasCredentialAccount, queryMemberExists, queryOrgAllowedAuthMethods, queryPendingInvitationExists, querySsoProviderIssuers } from "../data-access/runtime/auth.js";
3
+ import { parseAllowedAuthMethods } from "./auth-types.js";
4
+
5
+ //#region src/auth/auth-config-utils.ts
6
+ async function getInitialOrganization(dbClient, userId) {
7
+ return getInitialOrganization$1(dbClient)(userId);
8
+ }
9
+ /**
10
+ * Build the list of trusted origins for Better Auth.
11
+ * Includes static origins from env, SSO provider issuers from the DB,
12
+ * and (for /sso/register POST requests) the issuer from the request body
13
+ * so OIDC discovery is trusted before the provider is persisted.
14
+ */
15
+ async function getTrustedOrigins(dbClient, request) {
16
+ const staticOrigins = [
17
+ "http://localhost:3000",
18
+ "http://localhost:3002",
19
+ env.INKEEP_AGENTS_MANAGE_UI_URL,
20
+ env.INKEEP_AGENTS_API_URL,
21
+ env.TRUSTED_ORIGIN
22
+ ].filter((origin) => typeof origin === "string" && origin.length > 0);
23
+ const dynamicOrigins = [];
24
+ if ((request?.url?.includes("/sso/register") || request?.url?.includes("/sso-provider/create")) && request?.method === "POST") try {
25
+ const body = await request.clone().json();
26
+ const rawUrl = body.issuer || body.oidcConfig?.discoveryUrl || body.oidcConfig?.issuer;
27
+ if (rawUrl) {
28
+ const issuerOrigin = new URL(rawUrl).origin;
29
+ dynamicOrigins.push(issuerOrigin);
30
+ const discoveryOrigins = await fetchOidcDiscoveryOrigins(rawUrl);
31
+ dynamicOrigins.push(...discoveryOrigins);
32
+ }
33
+ } catch {}
34
+ try {
35
+ const providers = await querySsoProviderIssuers(dbClient)();
36
+ const issuerOrigins = providers.map((p) => {
37
+ try {
38
+ return new URL(p.issuer).origin;
39
+ } catch {
40
+ return null;
41
+ }
42
+ }).filter((origin) => origin !== null);
43
+ const discoveryResults = await Promise.all(providers.map((p) => fetchOidcDiscoveryOrigins(p.issuer)));
44
+ return [
45
+ ...staticOrigins,
46
+ ...dynamicOrigins,
47
+ ...issuerOrigins,
48
+ ...discoveryResults.flat()
49
+ ];
50
+ } catch {
51
+ return [...staticOrigins, ...dynamicOrigins];
52
+ }
53
+ }
54
+ async function fetchOidcDiscoveryOrigins(issuer) {
55
+ try {
56
+ const discoveryUrl = issuer.endsWith("/") ? `${issuer}.well-known/openid-configuration` : `${issuer}/.well-known/openid-configuration`;
57
+ const res = await fetch(discoveryUrl, { signal: AbortSignal.timeout(5e3) });
58
+ if (!res.ok) return [];
59
+ const doc = await res.json();
60
+ const endpointKeys = [
61
+ "authorization_endpoint",
62
+ "token_endpoint",
63
+ "userinfo_endpoint",
64
+ "jwks_uri",
65
+ "revocation_endpoint",
66
+ "introspection_endpoint"
67
+ ];
68
+ const origins = [];
69
+ for (const key of endpointKeys) if (typeof doc[key] === "string") try {
70
+ origins.push(new URL(doc[key]).origin);
71
+ } catch {}
72
+ return [...new Set(origins)];
73
+ } catch {
74
+ return [];
75
+ }
76
+ }
77
+ /**
78
+ * Extracts the root domain from a URL for cross-subdomain cookie sharing.
79
+ *
80
+ * When the API and UI share a common 3-part parent (e.g., api.pilot.inkeep.com
81
+ * and pilot.inkeep.com both share .pilot.inkeep.com), the function auto-computes
82
+ * the shared parent. When domains don't share a 3-part parent (e.g.,
83
+ * api.agents.inkeep.com and app.inkeep.com), set AUTH_COOKIE_DOMAIN explicitly.
84
+ *
85
+ * Examples (auto-computed from baseURL):
86
+ * - https://api.pilot.inkeep.com -> .pilot.inkeep.com
87
+ * - https://pilot.inkeep.com -> .pilot.inkeep.com
88
+ * - http://localhost:3002 -> undefined (no domain for localhost)
89
+ *
90
+ * With AUTH_COOKIE_DOMAIN=.inkeep.com:
91
+ * - Any *.inkeep.com URL -> .inkeep.com
92
+ */
93
+ function extractCookieDomain(baseURL, explicitDomain) {
94
+ if (explicitDomain) return explicitDomain.startsWith(".") ? explicitDomain : `.${explicitDomain}`;
95
+ try {
96
+ const hostname = new URL(baseURL).hostname;
97
+ if (hostname === "localhost" || hostname.match(/^\d+\.\d+\.\d+\.\d+$/)) return;
98
+ const parts = hostname.split(".");
99
+ if (parts.length < 2) return;
100
+ let domainParts;
101
+ if (parts.length === 3) domainParts = parts;
102
+ else if (parts.length > 3) domainParts = parts.slice(1);
103
+ else domainParts = parts;
104
+ return `.${domainParts.join(".")}`;
105
+ } catch {
106
+ return;
107
+ }
108
+ }
109
+ async function hasCredentialAccount(dbClient, userId) {
110
+ return queryHasCredentialAccount(dbClient)(userId);
111
+ }
112
+ /**
113
+ * Checks whether an SSO user should be auto-provisioned into an organization.
114
+ * Reads the per-provider `autoProvision` flag from `allowedAuthMethods` JSON.
115
+ * Returns false if:
116
+ * - The provider has no organizationId or providerId
117
+ * - The organization doesn't exist
118
+ * - The SSO provider entry has autoProvision disabled (or is missing)
119
+ * - The user is already a member
120
+ */
121
+ async function shouldAutoProvision(dbClient, user, provider) {
122
+ if (!provider.organizationId || !provider.providerId) return false;
123
+ const org = await queryOrgAllowedAuthMethods(dbClient)(provider.organizationId);
124
+ if (!org) return false;
125
+ const ssoEntry = parseAllowedAuthMethods(org.allowedAuthMethods).find((m) => m.method === "sso" && m.providerId === provider.providerId);
126
+ if (!ssoEntry || !ssoEntry.enabled || !ssoEntry.autoProvision) return false;
127
+ if (await queryMemberExists(dbClient)(user.id, provider.organizationId)) return false;
128
+ if (await queryPendingInvitationExists(dbClient)(user.email, provider.organizationId)) return false;
129
+ return true;
130
+ }
131
+
132
+ //#endregion
133
+ export { extractCookieDomain, getInitialOrganization, getTrustedOrigins, hasCredentialAccount, shouldAutoProvision };