@agent-native/core 0.12.12 → 0.12.14

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 (36) hide show
  1. package/dist/cli/workspace-dev.js +30 -2
  2. package/dist/cli/workspace-dev.js.map +1 -1
  3. package/dist/client/NewWorkspaceAppFlow.d.ts +9 -0
  4. package/dist/client/NewWorkspaceAppFlow.d.ts.map +1 -1
  5. package/dist/client/NewWorkspaceAppFlow.js +50 -4
  6. package/dist/client/NewWorkspaceAppFlow.js.map +1 -1
  7. package/dist/client/settings/SettingsPanel.d.ts.map +1 -1
  8. package/dist/client/settings/SettingsPanel.js +20 -16
  9. package/dist/client/settings/SettingsPanel.js.map +1 -1
  10. package/dist/client/settings/useBuilderStatus.d.ts +7 -7
  11. package/dist/client/settings/useBuilderStatus.d.ts.map +1 -1
  12. package/dist/client/settings/useBuilderStatus.js +1 -6
  13. package/dist/client/settings/useBuilderStatus.js.map +1 -1
  14. package/dist/server/auth.d.ts +2 -1
  15. package/dist/server/auth.d.ts.map +1 -1
  16. package/dist/server/auth.js.map +1 -1
  17. package/dist/server/builder-browser.d.ts +4 -4
  18. package/dist/server/builder-browser.d.ts.map +1 -1
  19. package/dist/server/builder-browser.js +1 -0
  20. package/dist/server/builder-browser.js.map +1 -1
  21. package/dist/server/core-routes-plugin.d.ts.map +1 -1
  22. package/dist/server/core-routes-plugin.js +33 -59
  23. package/dist/server/core-routes-plugin.js.map +1 -1
  24. package/dist/server/credential-provider.d.ts +20 -14
  25. package/dist/server/credential-provider.d.ts.map +1 -1
  26. package/dist/server/credential-provider.js +32 -45
  27. package/dist/server/credential-provider.js.map +1 -1
  28. package/dist/server/onboarding-html.d.ts +2 -1
  29. package/dist/server/onboarding-html.d.ts.map +1 -1
  30. package/dist/server/onboarding-html.js +105 -3
  31. package/dist/server/onboarding-html.js.map +1 -1
  32. package/dist/transcription/builder-transcription.d.ts.map +1 -1
  33. package/dist/transcription/builder-transcription.js +10 -3
  34. package/dist/transcription/builder-transcription.js.map +1 -1
  35. package/docs/content/template-mail.md +4 -0
  36. package/package.json +1 -1
@@ -46,20 +46,22 @@ export declare class FeatureNotConfiguredError extends Error {
46
46
  * Multi-tenant call sites must gate this explicitly before calling.
47
47
  */
48
48
  export declare function readDeployCredentialEnv(key: string): string | undefined;
49
+ type BuilderCredentialSource = "user" | "org" | "env";
50
+ /**
51
+ * Resolve a Builder credential for the current request. User/org credentials
52
+ * win; deployment env is only a fallback. This lets local/root .env keys keep
53
+ * a template working while still allowing users to connect their own Builder
54
+ * account from Settings or onboarding.
55
+ */
49
56
  export declare function resolveBuilderCredential(key: string): Promise<string | null>;
50
57
  /**
51
- * True when `BUILDER_PRIVATE_KEY` is set at the deployment level every
52
- * user of this deploy shares the operator's Builder identity, and per-user
53
- * connect/disconnect is disabled. UIs read this via `/builder/status` to
54
- * swap the "Connect Builder" prompts for a read-only "managed by deployment"
55
- * chip and to suppress the disconnect button.
58
+ * True when `BUILDER_PRIVATE_KEY` is set at the deployment level. This means
59
+ * a deploy-level fallback exists; it does not prevent per-user connect.
56
60
  */
57
61
  export declare function isBuilderEnvManaged(): boolean;
58
62
  /**
59
- * Resolve the Builder private key for the current request. In env-managed
60
- * mode (deploy-level `BUILDER_PRIVATE_KEY` set) returns the env value for
61
- * every caller. Otherwise reads the current user's per-user OAuth-stored
62
- * key from `app_secrets`.
63
+ * Resolve the Builder private key for the current request. User/org OAuth
64
+ * credentials win; deploy-level `BUILDER_PRIVATE_KEY` is the fallback.
63
65
  */
64
66
  export declare function resolveBuilderPrivateKey(): Promise<string | null>;
65
67
  /**
@@ -72,6 +74,11 @@ export declare function resolveBuilderAuthHeader(): Promise<string | null>;
72
74
  * (per-user or deployment-level).
73
75
  */
74
76
  export declare function resolveHasBuilderPrivateKey(): Promise<boolean>;
77
+ /**
78
+ * Resolve where the effective Builder private key came from. Used by status
79
+ * UIs so they can distinguish a deploy fallback from a user/org connection.
80
+ */
81
+ export declare function resolveBuilderCredentialSource(): Promise<BuilderCredentialSource | null>;
75
82
  /**
76
83
  * Resolve all per-user Builder credentials. Used by the status endpoint
77
84
  * and agent-chat-plugin to get orgName, userId, etc.
@@ -136,11 +143,9 @@ export declare function resolveSecret(key: string): Promise<string | null>;
136
143
  /**
137
144
  * True when a Builder private key is configured at the deployment level.
138
145
  *
139
- * This is the same check as `isBuilderEnvManaged()` (env-managed mode is
140
- * defined as "deploy-level BUILDER_PRIVATE_KEY is set"). Prefer
141
- * `isBuilderEnvManaged()` for new call sites — its name reflects what the
142
- * boolean means semantically. For "does this user have access to Builder
143
- * (env or per-user)?" use the async `resolveHasBuilderPrivateKey()`.
146
+ * This is the same env-only check as `isBuilderEnvManaged()`. For "does this
147
+ * request have access to Builder via user/org/env credentials?" use the async
148
+ * `resolveHasBuilderPrivateKey()`.
144
149
  */
145
150
  export declare function hasBuilderPrivateKey(): boolean;
146
151
  /** The origin for Builder-proxied API calls. Overridable for testing. */
@@ -160,4 +165,5 @@ export declare function getBuilderGatewayBaseUrl(): string;
160
165
  export declare function getBuilderImageGenerationBaseUrl(): string;
161
166
  /** Authorization header value for Builder-proxied calls (env-only). */
162
167
  export declare function getBuilderAuthHeader(): string | null;
168
+ export {};
163
169
  //# sourceMappingURL=credential-provider.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"credential-provider.d.ts","sourceRoot":"","sources":["../../src/server/credential-provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAIH;;;;;;;;GAQG;AACH,wBAAgB,2BAA2B,CACzC,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,EAChC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAC9B;IAAE,KAAK,EAAE,MAAM,GAAG,KAAK,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAK5C;AAED,qBAAa,yBAA0B,SAAQ,KAAK;IAClD,QAAQ,CAAC,kBAAkB,EA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;AACH,wBAAgB,oBAAoB,IAAI,OAAO,CAE9C;AAED,yEAAyE;AACzE,wBAAgB,qBAAqB,IAAI,MAAM,CAO9C;AAED;;;;;;GAMG;AACH,wBAAgB,wBAAwB,IAAI,MAAM,CAKjD;AAED;;;GAGG;AACH,wBAAgB,gCAAgC,IAAI,MAAM,CAKzD;AAED,uEAAuE;AACvE,wBAAgB,oBAAoB,IAAI,MAAM,GAAG,IAAI,CAGpD"}
@@ -53,35 +53,7 @@ export class FeatureNotConfiguredError extends Error {
53
53
  export function readDeployCredentialEnv(key) {
54
54
  return process.env[key] || undefined;
55
55
  }
56
- // ---------------------------------------------------------------------------
57
- // Builder credential resolution — two mutually-exclusive deployment modes:
58
- //
59
- // 1. **Single-tenant / env-managed.** When BUILDER_PRIVATE_KEY is set at
60
- // the deployment level, it is THE Builder identity for every user of
61
- // this deploy. The operator setting the env explicitly opts in to
62
- // "everyone shares one Builder space" — same shape as DATABASE_URL or
63
- // BETTER_AUTH_SECRET. The UI hides the per-user connect/disconnect
64
- // flow when env-managed (see `isBuilderEnvManaged`).
65
- //
66
- // 2. **Multi-tenant / per-user OAuth.** When the env is unset, each user
67
- // OAuth-connects their own Builder via the cli-auth flow. Their keys
68
- // land in `app_secrets` (scope=user, scopeId=email) via the callback
69
- // handler. They can disconnect via the settings panel.
70
- //
71
- // To run multi-tenant SaaS: leave the env unset. Setting BUILDER_PRIVATE_KEY
72
- // on a multi-tenant deploy will silently route every authenticated user
73
- // through the env-key owner's Builder identity — that was the KVesta Space
74
- // cross-tenant attribution leak (2026-04). The mode is binary: env-set
75
- // means single-tenant intent.
76
- // ---------------------------------------------------------------------------
77
- export async function resolveBuilderCredential(key) {
78
- // Env-managed mode wins when set: deploy-level Builder identity for
79
- // every user. Per-user app_secrets (left over from a previous OAuth
80
- // connection or a mode switch) are intentionally ignored — the
81
- // operator's deploy-level config is authoritative.
82
- const envValue = readDeployCredentialEnv(key);
83
- if (envValue)
84
- return envValue;
56
+ async function resolveScopedBuilderCredential(key) {
85
57
  const email = getRequestUserEmail();
86
58
  if (!email)
87
59
  return null;
@@ -95,7 +67,7 @@ export async function resolveBuilderCredential(key) {
95
67
  scopeId: email,
96
68
  });
97
69
  if (userSecret)
98
- return userSecret.value;
70
+ return { value: userSecret.value, source: "user" };
99
71
  // 2. Per-org shared credential: when one teammate connects Builder
100
72
  // as an owner/admin we write the OAuth result at org scope so
101
73
  // every member of that org gets the AI chat working without
@@ -109,7 +81,7 @@ export async function resolveBuilderCredential(key) {
109
81
  scopeId: orgId,
110
82
  });
111
83
  if (orgSecret)
112
- return orgSecret.value;
84
+ return { value: orgSecret.value, source: "org" };
113
85
  }
114
86
  }
115
87
  catch {
@@ -118,20 +90,27 @@ export async function resolveBuilderCredential(key) {
118
90
  return null;
119
91
  }
120
92
  /**
121
- * True when `BUILDER_PRIVATE_KEY` is set at the deployment level every
122
- * user of this deploy shares the operator's Builder identity, and per-user
123
- * connect/disconnect is disabled. UIs read this via `/builder/status` to
124
- * swap the "Connect Builder" prompts for a read-only "managed by deployment"
125
- * chip and to suppress the disconnect button.
93
+ * Resolve a Builder credential for the current request. User/org credentials
94
+ * win; deployment env is only a fallback. This lets local/root .env keys keep
95
+ * a template working while still allowing users to connect their own Builder
96
+ * account from Settings or onboarding.
97
+ */
98
+ export async function resolveBuilderCredential(key) {
99
+ const scoped = await resolveScopedBuilderCredential(key);
100
+ if (scoped)
101
+ return scoped.value;
102
+ return readDeployCredentialEnv(key) ?? null;
103
+ }
104
+ /**
105
+ * True when `BUILDER_PRIVATE_KEY` is set at the deployment level. This means
106
+ * a deploy-level fallback exists; it does not prevent per-user connect.
126
107
  */
127
108
  export function isBuilderEnvManaged() {
128
109
  return !!process.env.BUILDER_PRIVATE_KEY;
129
110
  }
130
111
  /**
131
- * Resolve the Builder private key for the current request. In env-managed
132
- * mode (deploy-level `BUILDER_PRIVATE_KEY` set) returns the env value for
133
- * every caller. Otherwise reads the current user's per-user OAuth-stored
134
- * key from `app_secrets`.
112
+ * Resolve the Builder private key for the current request. User/org OAuth
113
+ * credentials win; deploy-level `BUILDER_PRIVATE_KEY` is the fallback.
135
114
  */
136
115
  export async function resolveBuilderPrivateKey() {
137
116
  return resolveBuilderCredential("BUILDER_PRIVATE_KEY");
@@ -151,6 +130,16 @@ export async function resolveBuilderAuthHeader() {
151
130
  export async function resolveHasBuilderPrivateKey() {
152
131
  return !!(await resolveBuilderPrivateKey());
153
132
  }
133
+ /**
134
+ * Resolve where the effective Builder private key came from. Used by status
135
+ * UIs so they can distinguish a deploy fallback from a user/org connection.
136
+ */
137
+ export async function resolveBuilderCredentialSource() {
138
+ const scoped = await resolveScopedBuilderCredential("BUILDER_PRIVATE_KEY");
139
+ if (scoped)
140
+ return scoped.source;
141
+ return process.env.BUILDER_PRIVATE_KEY ? "env" : null;
142
+ }
154
143
  /**
155
144
  * Resolve all per-user Builder credentials. Used by the status endpoint
156
145
  * and agent-chat-plugin to get orgName, userId, etc.
@@ -311,11 +300,9 @@ export async function resolveSecret(key) {
311
300
  /**
312
301
  * True when a Builder private key is configured at the deployment level.
313
302
  *
314
- * This is the same check as `isBuilderEnvManaged()` (env-managed mode is
315
- * defined as "deploy-level BUILDER_PRIVATE_KEY is set"). Prefer
316
- * `isBuilderEnvManaged()` for new call sites — its name reflects what the
317
- * boolean means semantically. For "does this user have access to Builder
318
- * (env or per-user)?" use the async `resolveHasBuilderPrivateKey()`.
303
+ * This is the same env-only check as `isBuilderEnvManaged()`. For "does this
304
+ * request have access to Builder via user/org/env credentials?" use the async
305
+ * `resolveHasBuilderPrivateKey()`.
319
306
  */
320
307
  export function hasBuilderPrivateKey() {
321
308
  return !!process.env.BUILDER_PRIVATE_KEY;
@@ -1 +1 @@
1
- {"version":3,"file":"credential-provider.js","sourceRoot":"","sources":["../../src/server/credential-provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAE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gCAAgC;IAC9C,OAAO,CACL,OAAO,CAAC,GAAG,CAAC,iCAAiC;QAC7C,+CAA+C,CAChD,CAAC;AACJ,CAAC;AAED,uEAAuE;AACvE,MAAM,UAAU,oBAAoB;IAClC,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,mBAAmB,CAAC;IAC5C,OAAO,GAAG,CAAC,CAAC,CAAC,UAAU,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;AACtC,CAAC","sourcesContent":["/**\n * Credential provider abstraction.\n *\n * Every feature that needs an external credential (Anthropic API key,\n * Google OAuth tokens, OpenAI key, Slack bot token, etc.) should go through\n * one of the resolve*() helpers here instead of reading `process.env`\n * directly. That way the same feature can work in three modes:\n *\n * 1. User set their own key in .env → use it directly\n * 2. User connected Builder via `/cli-auth` → route through Builder proxy\n * 3. Neither → throw FeatureNotConfigured\n *\n * Templates catch FeatureNotConfigured and show a \"Connect Builder (1 click) /\n * set up your own key (guide)\" card.\n *\n * Today these helpers are used by the Builder-hosted LLM gateway, and the\n * shape is meant to grow to cover future managed credential integrations\n * (e.g. additional Builder-hosted services) without rewrites.\n */\n\nimport { getRequestUserEmail, getRequestOrgId } from \"./request-context.js\";\n\n/**\n * Decide which `app_secrets` scope a Builder/credential write should use.\n *\n * Org scope (\"everyone in this org sees these credentials\") wins when the\n * connecting user is an owner or admin of an active org — the write\n * privileges shared infra. A plain member or a user without an active\n * org falls through to per-user scope so a teammate can't silently\n * overwrite the org-shared connection.\n */\nexport function resolveCredentialWriteScope(\n email: string,\n orgId: string | null | undefined,\n role: string | null | undefined,\n): { scope: \"user\" | \"org\"; scopeId: string } {\n if (orgId && (role === \"owner\" || role === \"admin\")) {\n return { scope: \"org\", scopeId: orgId };\n }\n return { scope: \"user\", scopeId: email };\n}\n\nexport class FeatureNotConfiguredError extends Error {\n readonly requiredCredential: string;\n readonly builderConnectUrl?: string;\n readonly byokDocsUrl?: string;\n\n constructor(opts: {\n requiredCredential: string;\n message?: string;\n builderConnectUrl?: string;\n byokDocsUrl?: string;\n }) {\n super(\n opts.message ??\n `Feature requires credential \"${opts.requiredCredential}\". Connect Builder or set your own key.`,\n );\n this.name = \"FeatureNotConfiguredError\";\n this.requiredCredential = opts.requiredCredential;\n this.builderConnectUrl = opts.builderConnectUrl;\n this.byokDocsUrl = opts.byokDocsUrl;\n }\n}\n\n/**\n * Deployment-level credential fallback for single-tenant/local operation.\n * Multi-tenant call sites must gate this explicitly before calling.\n */\nexport function readDeployCredentialEnv(key: string): string | undefined {\n return process.env[key] || undefined;\n}\n\n// ---------------------------------------------------------------------------\n// Builder credential resolution — two mutually-exclusive deployment modes:\n//\n// 1. **Single-tenant / env-managed.** When BUILDER_PRIVATE_KEY is set at\n// the deployment level, it is THE Builder identity for every user of\n// this deploy. The operator setting the env explicitly opts in to\n// \"everyone shares one Builder space\" — same shape as DATABASE_URL or\n// BETTER_AUTH_SECRET. The UI hides the per-user connect/disconnect\n// flow when env-managed (see `isBuilderEnvManaged`).\n//\n// 2. **Multi-tenant / per-user OAuth.** When the env is unset, each user\n// OAuth-connects their own Builder via the cli-auth flow. Their keys\n// land in `app_secrets` (scope=user, scopeId=email) via the callback\n// handler. They can disconnect via the settings panel.\n//\n// To run multi-tenant SaaS: leave the env unset. Setting BUILDER_PRIVATE_KEY\n// on a multi-tenant deploy will silently route every authenticated user\n// through the env-key owner's Builder identity — that was the KVesta Space\n// cross-tenant attribution leak (2026-04). The mode is binary: env-set\n// means single-tenant intent.\n// ---------------------------------------------------------------------------\n\nexport async function resolveBuilderCredential(\n key: string,\n): Promise<string | null> {\n // Env-managed mode wins when set: deploy-level Builder identity for\n // every user. Per-user app_secrets (left over from a previous OAuth\n // connection or a mode switch) are intentionally ignored — the\n // operator's deploy-level config is authoritative.\n const envValue = readDeployCredentialEnv(key);\n if (envValue) return envValue;\n\n const email = getRequestUserEmail();\n if (!email) return null;\n\n try {\n const { readAppSecret } = await import(\"../secrets/storage.js\");\n\n // 1. Per-user override: a user can paste their own key in settings to\n // overrule the org-shared one (handy for a personal sandbox).\n const userSecret = await readAppSecret({\n key,\n scope: \"user\",\n scopeId: email,\n });\n if (userSecret) return userSecret.value;\n\n // 2. Per-org shared credential: when one teammate connects Builder\n // as an owner/admin we write the OAuth result at org scope so\n // every member of that org gets the AI chat working without\n // re-running the connect flow. Resolution falls back here\n // silently — the caller never has to know which scope answered.\n const orgId = getRequestOrgId();\n if (orgId) {\n const orgSecret = await readAppSecret({\n key,\n scope: \"org\",\n scopeId: orgId,\n });\n if (orgSecret) return orgSecret.value;\n }\n } catch {\n // Secrets table not ready — treat as missing.\n }\n return null;\n}\n\n/**\n * True when `BUILDER_PRIVATE_KEY` is set at the deployment level — every\n * user of this deploy shares the operator's Builder identity, and per-user\n * connect/disconnect is disabled. UIs read this via `/builder/status` to\n * swap the \"Connect Builder\" prompts for a read-only \"managed by deployment\"\n * chip and to suppress the disconnect button.\n */\nexport function isBuilderEnvManaged(): boolean {\n return !!process.env.BUILDER_PRIVATE_KEY;\n}\n\n/**\n * Resolve the Builder private key for the current request. In env-managed\n * mode (deploy-level `BUILDER_PRIVATE_KEY` set) returns the env value for\n * every caller. Otherwise reads the current user's per-user OAuth-stored\n * key from `app_secrets`.\n */\nexport async function resolveBuilderPrivateKey(): Promise<string | null> {\n return resolveBuilderCredential(\"BUILDER_PRIVATE_KEY\");\n}\n\n/**\n * Resolve the current user's Builder auth header.\n * Returns `\"Bearer <key>\"` or null.\n */\nexport async function resolveBuilderAuthHeader(): Promise<string | null> {\n const key = await resolveBuilderPrivateKey();\n return key ? `Bearer ${key}` : null;\n}\n\n/**\n * Check whether the current user has a Builder private key configured\n * (per-user or deployment-level).\n */\nexport async function resolveHasBuilderPrivateKey(): Promise<boolean> {\n return !!(await resolveBuilderPrivateKey());\n}\n\n/**\n * Resolve all per-user Builder credentials. Used by the status endpoint\n * and agent-chat-plugin to get orgName, userId, etc.\n */\nexport async function resolveBuilderCredentials(): Promise<{\n privateKey: string | null;\n publicKey: string | null;\n userId: string | null;\n orgName: string | null;\n orgKind: string | null;\n}> {\n const [privateKey, publicKey, userId, orgName, orgKind] = await Promise.all([\n resolveBuilderCredential(\"BUILDER_PRIVATE_KEY\"),\n resolveBuilderCredential(\"BUILDER_PUBLIC_KEY\"),\n resolveBuilderCredential(\"BUILDER_USER_ID\"),\n resolveBuilderCredential(\"BUILDER_ORG_NAME\"),\n resolveBuilderCredential(\"BUILDER_ORG_KIND\"),\n ]);\n return { privateKey, publicKey, userId, orgName, orgKind };\n}\n\nconst BUILDER_CREDENTIAL_KEYS = [\n \"BUILDER_PRIVATE_KEY\",\n \"BUILDER_PUBLIC_KEY\",\n \"BUILDER_USER_ID\",\n \"BUILDER_ORG_NAME\",\n \"BUILDER_ORG_KIND\",\n] as const;\n\n/**\n * Write Builder credentials to `app_secrets`.\n *\n * Scope decision (see `resolveCredentialWriteScope`): when the connecting\n * user is owner/admin of an active org we write at `scope: \"org\"` so every\n * member of that org auto-resolves the credentials via\n * `resolveBuilderCredential`'s org fallback — no per-user re-connect\n * needed. A plain member or a user with no active org writes at\n * `scope: \"user\"` (the safe default that doesn't trample the org's shared\n * connection).\n *\n * Returns the actual scope/scopeId used so the caller can show \"Connected\n * for Builder.io\" vs \"Connected (personal)\" in the UI.\n */\nexport async function writeBuilderCredentials(\n email: string,\n creds: {\n privateKey: string;\n publicKey: string;\n userId?: string | null;\n orgName?: string | null;\n orgKind?: string | null;\n },\n options?: { orgId?: string | null; role?: string | null },\n): Promise<{ scope: \"user\" | \"org\"; scopeId: string }> {\n const { writeAppSecret } = await import(\"../secrets/storage.js\");\n const target = resolveCredentialWriteScope(\n email,\n options?.orgId ?? null,\n options?.role ?? null,\n );\n\n const entries: Array<{ key: string; value: string }> = [\n { key: \"BUILDER_PRIVATE_KEY\", value: creds.privateKey },\n { key: \"BUILDER_PUBLIC_KEY\", value: creds.publicKey },\n ];\n if (creds.userId) {\n entries.push({ key: \"BUILDER_USER_ID\", value: creds.userId });\n }\n if (creds.orgName) {\n entries.push({ key: \"BUILDER_ORG_NAME\", value: creds.orgName });\n }\n if (creds.orgKind) {\n entries.push({ key: \"BUILDER_ORG_KIND\", value: creds.orgKind });\n }\n await Promise.all(\n entries.map(({ key, value }) =>\n writeAppSecret({\n key,\n value,\n scope: target.scope,\n scopeId: target.scopeId,\n }),\n ),\n );\n return target;\n}\n\n/**\n * Delete Builder credentials.\n *\n * Default behaviour: clears only this user's per-user override (so a\n * member can disconnect their personal Builder identity without\n * collapsing the org-wide connection for every teammate). To revoke the\n * org's shared connection, pass `{ orgId, role }` for an owner/admin —\n * matching the same authority gate `writeBuilderCredentials` uses on\n * write. Plain members can never reach the org-scoped row.\n */\nexport async function deleteBuilderCredentials(\n email: string,\n options?: { orgId?: string | null; role?: string | null },\n): Promise<{ scope: \"user\" | \"org\"; scopeId: string }> {\n const { deleteAppSecret } = await import(\"../secrets/storage.js\");\n const target = resolveCredentialWriteScope(\n email,\n options?.orgId ?? null,\n options?.role ?? null,\n );\n await Promise.all(\n BUILDER_CREDENTIAL_KEYS.map((key) =>\n deleteAppSecret({\n key,\n scope: target.scope,\n scopeId: target.scopeId,\n }).catch(() => {}),\n ),\n );\n return target;\n}\n\n// ---------------------------------------------------------------------------\n// Generic request-scoped secret resolution\n//\n// New consumers should prefer this over reading `process.env.X` directly.\n// User-pasted and shared secrets live in `app_secrets` (encrypted). The\n// settings UI / onboarding panels can write user, org, or workspace rows.\n// Deploy-level env vars are the fallback for unauthenticated/CLI/background\n// contexts where there's no user to scope by — never the silent fallback\n// for an authenticated request, since on a multi-tenant deploy that would\n// silently identify every user as whoever set the deploy-level key\n// (KVesta Space, 2026-04).\n// ---------------------------------------------------------------------------\n\n/**\n * Resolve a request-scoped secret. Reads from `app_secrets` first (current\n * user override, active org, then workspace row); falls back to `process.env`\n * only for unauthenticated/CLI/background contexts.\n */\nexport async function resolveSecret(key: string): Promise<string | null> {\n const email = getRequestUserEmail();\n if (email) {\n try {\n const { readAppSecret } = await import(\"../secrets/storage.js\");\n // Per-user override first.\n const userSecret = await readAppSecret({\n key,\n scope: \"user\",\n scopeId: email,\n });\n if (userSecret?.value) return userSecret.value;\n\n const orgId = getRequestOrgId();\n if (orgId) {\n // Fall back to the active org's shared row, when present. Builder\n // Connect uses this first-class org scope.\n const orgSecret = await readAppSecret({\n key,\n scope: \"org\",\n scopeId: orgId,\n });\n if (orgSecret?.value) return orgSecret.value;\n\n // Registered secrets historically used \"workspace\" scope for\n // org-shared configuration. Keep reading it so Settings status and\n // runtime resolution agree.\n const workspaceSecret = await readAppSecret({\n key,\n scope: \"workspace\",\n scopeId: orgId,\n });\n if (workspaceSecret?.value) return workspaceSecret.value;\n } else {\n const soloWorkspaceSecret = await readAppSecret({\n key,\n scope: \"workspace\",\n scopeId: `solo:${email}`,\n });\n if (soloWorkspaceSecret?.value) return soloWorkspaceSecret.value;\n }\n } catch {\n // Secrets table not ready — treat as missing.\n }\n // Authenticated multi-tenant context: never fall back to process.env.\n // The deploy-level value would silently impersonate the actual key\n // owner across every tenant.\n return null;\n }\n // Unauthenticated / local-dev / CLI / background context: env fallback\n // is safe because there's no user to mis-identify.\n return process.env[key] || null;\n}\n\n// ---------------------------------------------------------------------------\n// Synchronous helpers — env-only fallbacks for contexts where per-user\n// lookup isn't possible (sync isConfigured checks, CLI scripts).\n// ---------------------------------------------------------------------------\n\n/**\n * True when a Builder private key is configured at the deployment level.\n *\n * This is the same check as `isBuilderEnvManaged()` (env-managed mode is\n * defined as \"deploy-level BUILDER_PRIVATE_KEY is set\"). Prefer\n * `isBuilderEnvManaged()` for new call sites — its name reflects what the\n * boolean means semantically. For \"does this user have access to Builder\n * (env or per-user)?\" use the async `resolveHasBuilderPrivateKey()`.\n */\nexport function hasBuilderPrivateKey(): boolean {\n return !!process.env.BUILDER_PRIVATE_KEY;\n}\n\n/** The origin for Builder-proxied API calls. Overridable for testing. */\nexport function getBuilderProxyOrigin(): string {\n return (\n process.env.BUILDER_PROXY_ORIGIN ||\n process.env.AIR_HOST ||\n process.env.BUILDER_API_HOST ||\n \"https://ai-services.builder.io\"\n );\n}\n\n/**\n * Base URL for the public Builder LLM gateway (distinct from the internal\n * proxy origin above — the public gateway lives at\n * api.builder.io/agent-native/gateway, while the internal origin is\n * ai-services.builder.io).\n * Override via BUILDER_GATEWAY_BASE_URL for staging / testing.\n */\nexport function getBuilderGatewayBaseUrl(): string {\n return (\n process.env.BUILDER_GATEWAY_BASE_URL ||\n \"https://api.builder.io/agent-native/gateway/v1\"\n );\n}\n\n/**\n * Base URL for Builder-managed image generation.\n * Override via BUILDER_IMAGE_GENERATION_BASE_URL for staging / testing.\n */\nexport function getBuilderImageGenerationBaseUrl(): string {\n return (\n process.env.BUILDER_IMAGE_GENERATION_BASE_URL ||\n \"https://api.builder.io/agent-native/images/v1\"\n );\n}\n\n/** Authorization header value for Builder-proxied calls (env-only). */\nexport function getBuilderAuthHeader(): string | null {\n const key = process.env.BUILDER_PRIVATE_KEY;\n return key ? `Bearer ${key}` : null;\n}\n"]}
1
+ {"version":3,"file":"credential-provider.js","sourceRoot":"","sources":["../../src/server/credential-provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAE5E;;;;;;;;GAQG;AACH,MAAM,UAAU,2BAA2B,CACzC,KAAa,EACb,KAAgC,EAChC,IAA+B;IAE/B,IAAI,KAAK,IAAI,CAAC,IAAI,KAAK,OAAO,IAAI,IAAI,KAAK,OAAO,CAAC,EAAE,CAAC;QACpD,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;IAC1C,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC;AAC3C,CAAC;AAED,MAAM,OAAO,yBAA0B,SAAQ,KAAK;IACzC,kBAAkB,CAAS;IAC3B,iBAAiB,CAAU;IAC3B,WAAW,CAAU;IAE9B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["/**\n * Credential provider abstraction.\n *\n * Every feature that needs an external credential (Anthropic API key,\n * Google OAuth tokens, OpenAI key, Slack bot token, etc.) should go through\n * one of the resolve*() helpers here instead of reading `process.env`\n * directly. That way the same feature can work in three modes:\n *\n * 1. User set their own key in .env → use it directly\n * 2. User connected Builder via `/cli-auth` → route through Builder proxy\n * 3. Neither → throw FeatureNotConfigured\n *\n * Templates catch FeatureNotConfigured and show a \"Connect Builder (1 click) /\n * set up your own key (guide)\" card.\n *\n * Today these helpers are used by the Builder-hosted LLM gateway, and the\n * shape is meant to grow to cover future managed credential integrations\n * (e.g. additional Builder-hosted services) without rewrites.\n */\n\nimport { getRequestUserEmail, getRequestOrgId } from \"./request-context.js\";\n\n/**\n * Decide which `app_secrets` scope a Builder/credential write should use.\n *\n * Org scope (\"everyone in this org sees these credentials\") wins when the\n * connecting user is an owner or admin of an active org — the write\n * privileges shared infra. A plain member or a user without an active\n * org falls through to per-user scope so a teammate can't silently\n * overwrite the org-shared connection.\n */\nexport function resolveCredentialWriteScope(\n email: string,\n orgId: string | null | undefined,\n role: string | null | undefined,\n): { scope: \"user\" | \"org\"; scopeId: string } {\n if (orgId && (role === \"owner\" || role === \"admin\")) {\n return { scope: \"org\", scopeId: orgId };\n }\n return { scope: \"user\", scopeId: email };\n}\n\nexport class FeatureNotConfiguredError extends Error {\n readonly requiredCredential: string;\n readonly builderConnectUrl?: string;\n readonly byokDocsUrl?: string;\n\n constructor(opts: {\n requiredCredential: string;\n message?: string;\n builderConnectUrl?: string;\n byokDocsUrl?: string;\n }) {\n super(\n opts.message ??\n `Feature requires credential \"${opts.requiredCredential}\". Connect Builder or set your own key.`,\n );\n this.name = \"FeatureNotConfiguredError\";\n this.requiredCredential = opts.requiredCredential;\n this.builderConnectUrl = opts.builderConnectUrl;\n this.byokDocsUrl = opts.byokDocsUrl;\n }\n}\n\n/**\n * Deployment-level credential fallback for single-tenant/local operation.\n * Multi-tenant call sites must gate this explicitly before calling.\n */\nexport function readDeployCredentialEnv(key: string): string | undefined {\n return process.env[key] || undefined;\n}\n\n// ---------------------------------------------------------------------------\n// Builder credential resolution:\n//\n// 1. **Request-scoped credentials.** A signed-in user can connect Builder\n// through the CLI-auth flow. Owner/admin connections land at org scope;\n// member/no-org connections land at user scope.\n//\n// 2. **Deployment fallback.** BUILDER_PRIVATE_KEY in env still makes local\n// and single-tenant deploys work out of the box, but it no longer blocks\n// per-user connect. Request-scoped credentials win whenever present.\n//\n// To run multi-tenant SaaS: prefer leaving BUILDER_PRIVATE_KEY unset unless a\n// shared fallback identity is intentional.\n// ---------------------------------------------------------------------------\n\ntype BuilderCredentialSource = \"user\" | \"org\" | \"env\";\n\nasync function resolveScopedBuilderCredential(\n key: string,\n): Promise<{ value: string; source: \"user\" | \"org\" } | null> {\n const email = getRequestUserEmail();\n if (!email) return null;\n\n try {\n const { readAppSecret } = await import(\"../secrets/storage.js\");\n\n // 1. Per-user override: a user can paste their own key in settings to\n // overrule the org-shared one (handy for a personal sandbox).\n const userSecret = await readAppSecret({\n key,\n scope: \"user\",\n scopeId: email,\n });\n if (userSecret) return { value: userSecret.value, source: \"user\" };\n\n // 2. Per-org shared credential: when one teammate connects Builder\n // as an owner/admin we write the OAuth result at org scope so\n // every member of that org gets the AI chat working without\n // re-running the connect flow. Resolution falls back here\n // silently — the caller never has to know which scope answered.\n const orgId = getRequestOrgId();\n if (orgId) {\n const orgSecret = await readAppSecret({\n key,\n scope: \"org\",\n scopeId: orgId,\n });\n if (orgSecret) return { value: orgSecret.value, source: \"org\" };\n }\n } catch {\n // Secrets table not ready — treat as missing.\n }\n return null;\n}\n\n/**\n * Resolve a Builder credential for the current request. User/org credentials\n * win; deployment env is only a fallback. This lets local/root .env keys keep\n * a template working while still allowing users to connect their own Builder\n * account from Settings or onboarding.\n */\nexport async function resolveBuilderCredential(\n key: string,\n): Promise<string | null> {\n const scoped = await resolveScopedBuilderCredential(key);\n if (scoped) return scoped.value;\n return readDeployCredentialEnv(key) ?? null;\n}\n\n/**\n * True when `BUILDER_PRIVATE_KEY` is set at the deployment level. This means\n * a deploy-level fallback exists; it does not prevent per-user connect.\n */\nexport function isBuilderEnvManaged(): boolean {\n return !!process.env.BUILDER_PRIVATE_KEY;\n}\n\n/**\n * Resolve the Builder private key for the current request. User/org OAuth\n * credentials win; deploy-level `BUILDER_PRIVATE_KEY` is the fallback.\n */\nexport async function resolveBuilderPrivateKey(): Promise<string | null> {\n return resolveBuilderCredential(\"BUILDER_PRIVATE_KEY\");\n}\n\n/**\n * Resolve the current user's Builder auth header.\n * Returns `\"Bearer <key>\"` or null.\n */\nexport async function resolveBuilderAuthHeader(): Promise<string | null> {\n const key = await resolveBuilderPrivateKey();\n return key ? `Bearer ${key}` : null;\n}\n\n/**\n * Check whether the current user has a Builder private key configured\n * (per-user or deployment-level).\n */\nexport async function resolveHasBuilderPrivateKey(): Promise<boolean> {\n return !!(await resolveBuilderPrivateKey());\n}\n\n/**\n * Resolve where the effective Builder private key came from. Used by status\n * UIs so they can distinguish a deploy fallback from a user/org connection.\n */\nexport async function resolveBuilderCredentialSource(): Promise<BuilderCredentialSource | null> {\n const scoped = await resolveScopedBuilderCredential(\"BUILDER_PRIVATE_KEY\");\n if (scoped) return scoped.source;\n return process.env.BUILDER_PRIVATE_KEY ? \"env\" : null;\n}\n\n/**\n * Resolve all per-user Builder credentials. Used by the status endpoint\n * and agent-chat-plugin to get orgName, userId, etc.\n */\nexport async function resolveBuilderCredentials(): Promise<{\n privateKey: string | null;\n publicKey: string | null;\n userId: string | null;\n orgName: string | null;\n orgKind: string | null;\n}> {\n const [privateKey, publicKey, userId, orgName, orgKind] = await Promise.all([\n resolveBuilderCredential(\"BUILDER_PRIVATE_KEY\"),\n resolveBuilderCredential(\"BUILDER_PUBLIC_KEY\"),\n resolveBuilderCredential(\"BUILDER_USER_ID\"),\n resolveBuilderCredential(\"BUILDER_ORG_NAME\"),\n resolveBuilderCredential(\"BUILDER_ORG_KIND\"),\n ]);\n return { privateKey, publicKey, userId, orgName, orgKind };\n}\n\nconst BUILDER_CREDENTIAL_KEYS = [\n \"BUILDER_PRIVATE_KEY\",\n \"BUILDER_PUBLIC_KEY\",\n \"BUILDER_USER_ID\",\n \"BUILDER_ORG_NAME\",\n \"BUILDER_ORG_KIND\",\n] as const;\n\n/**\n * Write Builder credentials to `app_secrets`.\n *\n * Scope decision (see `resolveCredentialWriteScope`): when the connecting\n * user is owner/admin of an active org we write at `scope: \"org\"` so every\n * member of that org auto-resolves the credentials via\n * `resolveBuilderCredential`'s org fallback — no per-user re-connect\n * needed. A plain member or a user with no active org writes at\n * `scope: \"user\"` (the safe default that doesn't trample the org's shared\n * connection).\n *\n * Returns the actual scope/scopeId used so the caller can show \"Connected\n * for Builder.io\" vs \"Connected (personal)\" in the UI.\n */\nexport async function writeBuilderCredentials(\n email: string,\n creds: {\n privateKey: string;\n publicKey: string;\n userId?: string | null;\n orgName?: string | null;\n orgKind?: string | null;\n },\n options?: { orgId?: string | null; role?: string | null },\n): Promise<{ scope: \"user\" | \"org\"; scopeId: string }> {\n const { writeAppSecret } = await import(\"../secrets/storage.js\");\n const target = resolveCredentialWriteScope(\n email,\n options?.orgId ?? null,\n options?.role ?? null,\n );\n\n const entries: Array<{ key: string; value: string }> = [\n { key: \"BUILDER_PRIVATE_KEY\", value: creds.privateKey },\n { key: \"BUILDER_PUBLIC_KEY\", value: creds.publicKey },\n ];\n if (creds.userId) {\n entries.push({ key: \"BUILDER_USER_ID\", value: creds.userId });\n }\n if (creds.orgName) {\n entries.push({ key: \"BUILDER_ORG_NAME\", value: creds.orgName });\n }\n if (creds.orgKind) {\n entries.push({ key: \"BUILDER_ORG_KIND\", value: creds.orgKind });\n }\n await Promise.all(\n entries.map(({ key, value }) =>\n writeAppSecret({\n key,\n value,\n scope: target.scope,\n scopeId: target.scopeId,\n }),\n ),\n );\n return target;\n}\n\n/**\n * Delete Builder credentials.\n *\n * Default behaviour: clears only this user's per-user override (so a\n * member can disconnect their personal Builder identity without\n * collapsing the org-wide connection for every teammate). To revoke the\n * org's shared connection, pass `{ orgId, role }` for an owner/admin —\n * matching the same authority gate `writeBuilderCredentials` uses on\n * write. Plain members can never reach the org-scoped row.\n */\nexport async function deleteBuilderCredentials(\n email: string,\n options?: { orgId?: string | null; role?: string | null },\n): Promise<{ scope: \"user\" | \"org\"; scopeId: string }> {\n const { deleteAppSecret } = await import(\"../secrets/storage.js\");\n const target = resolveCredentialWriteScope(\n email,\n options?.orgId ?? null,\n options?.role ?? null,\n );\n await Promise.all(\n BUILDER_CREDENTIAL_KEYS.map((key) =>\n deleteAppSecret({\n key,\n scope: target.scope,\n scopeId: target.scopeId,\n }).catch(() => {}),\n ),\n );\n return target;\n}\n\n// ---------------------------------------------------------------------------\n// Generic request-scoped secret resolution\n//\n// New consumers should prefer this over reading `process.env.X` directly.\n// User-pasted and shared secrets live in `app_secrets` (encrypted). The\n// settings UI / onboarding panels can write user, org, or workspace rows.\n// Deploy-level env vars are the fallback for unauthenticated/CLI/background\n// contexts where there's no user to scope by — never the silent fallback\n// for an authenticated request, since on a multi-tenant deploy that would\n// silently identify every user as whoever set the deploy-level key\n// (KVesta Space, 2026-04).\n// ---------------------------------------------------------------------------\n\n/**\n * Resolve a request-scoped secret. Reads from `app_secrets` first (current\n * user override, active org, then workspace row); falls back to `process.env`\n * only for unauthenticated/CLI/background contexts.\n */\nexport async function resolveSecret(key: string): Promise<string | null> {\n const email = getRequestUserEmail();\n if (email) {\n try {\n const { readAppSecret } = await import(\"../secrets/storage.js\");\n // Per-user override first.\n const userSecret = await readAppSecret({\n key,\n scope: \"user\",\n scopeId: email,\n });\n if (userSecret?.value) return userSecret.value;\n\n const orgId = getRequestOrgId();\n if (orgId) {\n // Fall back to the active org's shared row, when present. Builder\n // Connect uses this first-class org scope.\n const orgSecret = await readAppSecret({\n key,\n scope: \"org\",\n scopeId: orgId,\n });\n if (orgSecret?.value) return orgSecret.value;\n\n // Registered secrets historically used \"workspace\" scope for\n // org-shared configuration. Keep reading it so Settings status and\n // runtime resolution agree.\n const workspaceSecret = await readAppSecret({\n key,\n scope: \"workspace\",\n scopeId: orgId,\n });\n if (workspaceSecret?.value) return workspaceSecret.value;\n } else {\n const soloWorkspaceSecret = await readAppSecret({\n key,\n scope: \"workspace\",\n scopeId: `solo:${email}`,\n });\n if (soloWorkspaceSecret?.value) return soloWorkspaceSecret.value;\n }\n } catch {\n // Secrets table not ready — treat as missing.\n }\n // Authenticated multi-tenant context: never fall back to process.env.\n // The deploy-level value would silently impersonate the actual key\n // owner across every tenant.\n return null;\n }\n // Unauthenticated / local-dev / CLI / background context: env fallback\n // is safe because there's no user to mis-identify.\n return process.env[key] || null;\n}\n\n// ---------------------------------------------------------------------------\n// Synchronous helpers — env-only fallbacks for contexts where per-user\n// lookup isn't possible (sync isConfigured checks, CLI scripts).\n// ---------------------------------------------------------------------------\n\n/**\n * True when a Builder private key is configured at the deployment level.\n *\n * This is the same env-only check as `isBuilderEnvManaged()`. For \"does this\n * request have access to Builder via user/org/env credentials?\" use the async\n * `resolveHasBuilderPrivateKey()`.\n */\nexport function hasBuilderPrivateKey(): boolean {\n return !!process.env.BUILDER_PRIVATE_KEY;\n}\n\n/** The origin for Builder-proxied API calls. Overridable for testing. */\nexport function getBuilderProxyOrigin(): string {\n return (\n process.env.BUILDER_PROXY_ORIGIN ||\n process.env.AIR_HOST ||\n process.env.BUILDER_API_HOST ||\n \"https://ai-services.builder.io\"\n );\n}\n\n/**\n * Base URL for the public Builder LLM gateway (distinct from the internal\n * proxy origin above — the public gateway lives at\n * api.builder.io/agent-native/gateway, while the internal origin is\n * ai-services.builder.io).\n * Override via BUILDER_GATEWAY_BASE_URL for staging / testing.\n */\nexport function getBuilderGatewayBaseUrl(): string {\n return (\n process.env.BUILDER_GATEWAY_BASE_URL ||\n \"https://api.builder.io/agent-native/gateway/v1\"\n );\n}\n\n/**\n * Base URL for Builder-managed image generation.\n * Override via BUILDER_IMAGE_GENERATION_BASE_URL for staging / testing.\n */\nexport function getBuilderImageGenerationBaseUrl(): string {\n return (\n process.env.BUILDER_IMAGE_GENERATION_BASE_URL ||\n \"https://api.builder.io/agent-native/images/v1\"\n );\n}\n\n/** Authorization header value for Builder-proxied calls (env-only). */\nexport function getBuilderAuthHeader(): string | null {\n const key = process.env.BUILDER_PRIVATE_KEY;\n return key ? `Bearer ${key}` : null;\n}\n"]}
@@ -23,6 +23,7 @@ export interface OnboardingHtmlOptions {
23
23
  tagline: string;
24
24
  description?: string;
25
25
  features?: string[];
26
+ runLocalCommand?: string;
26
27
  };
27
28
  /**
28
29
  * Optional preflight copy shown before redirecting through Google sign-in.
@@ -32,7 +33,7 @@ export interface OnboardingHtmlOptions {
32
33
  googleSignInNotice?: {
33
34
  host?: string;
34
35
  title: string;
35
- body: string;
36
+ body: string | string[];
36
37
  continueLabel?: string;
37
38
  cancelLabel?: string;
38
39
  };
@@ -1 +1 @@
1
- {"version":3,"file":"onboarding-html.d.ts","sourceRoot":"","sources":["../../src/server/onboarding-html.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAkCH,MAAM,WAAW,qBAAqB;IACpC;;;;OAIG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB;;;;OAIG;IACH,SAAS,CAAC,EAAE;QACV,OAAO,EAAE,MAAM,CAAC;QAChB,OAAO,EAAE,MAAM,CAAC;QAChB,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;KACrB,CAAC;IACF;;;;OAIG;IACH,kBAAkB,CAAC,EAAE;QACnB,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,KAAK,EAAE,MAAM,CAAC;QACd,IAAI,EAAE,MAAM,CAAC;QACb,aAAa,CAAC,EAAE,MAAM,CAAC;QACvB,WAAW,CAAC,EAAE,MAAM,CAAC;KACtB,CAAC;CACH;AAED,wBAAgB,iBAAiB,CAAC,IAAI,GAAE,qBAA0B,GAAG,MAAM,CA8uC1E;AAED,kDAAkD;AAClD,eAAO,MAAM,eAAe,QAAsB,CAAC;AAEnD;;;;GAIG;AACH,wBAAgB,oBAAoB,IAAI,MAAM,CAwG7C"}
1
+ {"version":3,"file":"onboarding-html.d.ts","sourceRoot":"","sources":["../../src/server/onboarding-html.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAkCH,MAAM,WAAW,qBAAqB;IACpC;;;;OAIG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB;;;;OAIG;IACH,SAAS,CAAC,EAAE;QACV,OAAO,EAAE,MAAM,CAAC;QAChB,OAAO,EAAE,MAAM,CAAC;QAChB,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;QACpB,eAAe,CAAC,EAAE,MAAM,CAAC;KAC1B,CAAC;IACF;;;;OAIG;IACH,kBAAkB,CAAC,EAAE;QACnB,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,KAAK,EAAE,MAAM,CAAC;QACd,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;QACxB,aAAa,CAAC,EAAE,MAAM,CAAC;QACvB,WAAW,CAAC,EAAE,MAAM,CAAC;KACtB,CAAC;CACH;AAED,wBAAgB,iBAAiB,CAAC,IAAI,GAAE,qBAA0B,GAAG,MAAM,CA41C1E;AAED,kDAAkD;AAClD,eAAO,MAAM,eAAe,QAAsB,CAAC;AAEnD;;;;GAIG;AACH,wBAAgB,oBAAoB,IAAI,MAAM,CAwG7C"}
@@ -45,6 +45,7 @@ export function getOnboardingHtml(opts = {}) {
45
45
  const appBasePath = normalizeAppBasePath(process.env.VITE_APP_BASE_PATH || process.env.APP_BASE_PATH);
46
46
  const marketing = opts.marketing;
47
47
  const hasMarketing = !!marketing;
48
+ const runLocalCommand = marketing?.runLocalCommand?.trim();
48
49
  const brandMarkSrc = withAppBasePath("/agent-native-icon-dark.svg");
49
50
  const esc = (s) => s
50
51
  .replace(/&/g, "&amp;")
@@ -52,6 +53,14 @@ export function getOnboardingHtml(opts = {}) {
52
53
  .replace(/>/g, "&gt;")
53
54
  .replace(/"/g, "&quot;");
54
55
  const googleSignInNotice = opts.googleSignInNotice;
56
+ const googleNoticeBodyHtml = googleSignInNotice
57
+ ? (Array.isArray(googleSignInNotice.body)
58
+ ? googleSignInNotice.body
59
+ : [googleSignInNotice.body])
60
+ .filter((body) => body.trim().length > 0)
61
+ .map((body, index) => `<p class="google-preflight-copy"${index === 0 ? ' id="google-preflight-copy"' : ""}>${esc(body)}</p>`)
62
+ .join("\n")
63
+ : "";
55
64
  const googleNoticeHtml = showGoogle && googleSignInNotice
56
65
  ? `
57
66
  <div
@@ -63,7 +72,7 @@ export function getOnboardingHtml(opts = {}) {
63
72
  aria-describedby="google-preflight-copy"
64
73
  >
65
74
  <p class="google-preflight-title" id="google-preflight-title">${esc(googleSignInNotice.title)}</p>
66
- <p class="google-preflight-copy" id="google-preflight-copy">${esc(googleSignInNotice.body)}</p>
75
+ ${googleNoticeBodyHtml}
67
76
  <div class="google-preflight-actions">
68
77
  <button type="button" class="btn-primary" id="google-preflight-continue" onclick="__anAcceptGoogleNotice()">${esc(googleSignInNotice.continueLabel ?? "Continue")}</button>
69
78
  <button type="button" class="btn-secondary" onclick="__anHideGoogleNotice()">${esc(googleSignInNotice.cancelLabel ?? "Cancel")}</button>
@@ -158,13 +167,68 @@ export function getOnboardingHtml(opts = {}) {
158
167
  display: inline-flex;
159
168
  align-items: center;
160
169
  gap: 0.375rem;
161
- margin-top: 2rem;
162
170
  font-size: 0.8125rem;
163
171
  color: #71717a;
164
172
  text-decoration: none;
165
173
  }
166
174
  .oss-link:hover { color: #a1a1aa; }
167
175
  .oss-link svg { width: 15px; height: 15px; flex-shrink: 0; }
176
+ .marketing-actions {
177
+ display: flex;
178
+ flex-wrap: wrap;
179
+ align-items: center;
180
+ gap: 0.75rem;
181
+ margin-top: 2rem;
182
+ }
183
+ .run-local-button {
184
+ display: inline-flex;
185
+ align-items: center;
186
+ justify-content: center;
187
+ min-height: 2.25rem;
188
+ padding: 0.5rem 0.875rem;
189
+ background: rgba(255,255,255,0.08);
190
+ color: #fff;
191
+ border: 1px solid rgba(255,255,255,0.14);
192
+ border-radius: 8px;
193
+ font-size: 0.8125rem;
194
+ font-weight: 500;
195
+ cursor: pointer;
196
+ }
197
+ .run-local-button:hover {
198
+ background: rgba(255,255,255,0.12);
199
+ border-color: rgba(255,255,255,0.24);
200
+ }
201
+ .run-local-panel {
202
+ max-width: 480px;
203
+ margin-top: 0.75rem;
204
+ padding: 0.75rem;
205
+ background: rgba(20,20,20,0.86);
206
+ border: 1px solid rgba(255,255,255,0.1);
207
+ border-radius: 10px;
208
+ box-shadow: 0 14px 36px rgba(0,0,0,0.28);
209
+ }
210
+ .run-local-panel[hidden] { display: none; }
211
+ .run-local-panel code {
212
+ display: block;
213
+ overflow-x: auto;
214
+ padding-bottom: 0.125rem;
215
+ color: #e5e5e5;
216
+ font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
217
+ font-size: 0.75rem;
218
+ line-height: 1.5;
219
+ white-space: nowrap;
220
+ }
221
+ .copy-run-local {
222
+ margin-top: 0.625rem;
223
+ padding: 0.375rem 0.625rem;
224
+ background: transparent;
225
+ color: #a1a1aa;
226
+ border: 1px solid rgba(255,255,255,0.12);
227
+ border-radius: 6px;
228
+ font-size: 0.75rem;
229
+ cursor: pointer;
230
+ }
231
+ .copy-run-local:hover { color: #fff; border-color: rgba(255,255,255,0.22); }
168
232
  .form-panel {
169
233
  flex: 0 0 440px;
170
234
  display: flex;
@@ -199,10 +263,18 @@ export function getOnboardingHtml(opts = {}) {
199
263
  <p class="app-tagline">${esc(marketing.tagline)}</p>
200
264
  ${marketing.description ? ` <p class="app-desc">${esc(marketing.description)}</p>\n` : ""}${marketing.features?.length
201
265
  ? ` <ul class="feature-list">\n${marketing.features.map((f) => ` <li>${esc(f)}</li>`).join("\n")}\n </ul>\n`
202
- : ""} <a class="oss-link" href="https://github.com/BuilderIO/agent-native" target="_blank" rel="noreferrer">
266
+ : ""} <div class="marketing-actions">
267
+ ${runLocalCommand ? ` <button type="button" class="run-local-button" id="run-local-button" aria-expanded="false" aria-controls="run-local-panel" onclick="__anToggleRunLocalCommand()">Run Locally</button>\n` : ""} <a class="oss-link" href="https://github.com/BuilderIO/agent-native" target="_blank" rel="noreferrer">
203
268
  <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 19c-4.3 1.4-4.3-2.5-6-3m12 5v-3.5c0-1 .1-1.4-.5-2 2.8-.3 5.5-1.4 5.5-6a4.6 4.6 0 00-1.3-3.2 4.2 4.2 0 00-.1-3.2s-1.1-.3-3.5 1.3a12.3 12.3 0 00-6.2 0C6.5 2.8 5.4 3.1 5.4 3.1a4.2 4.2 0 00-.1 3.2A4.6 4.6 0 004 9.5c0 4.6 2.7 5.7 5.5 6-.6.6-.6 1.2-.5 2V21"/></svg>
204
269
  Open source
205
270
  </a>
271
+ </div>
272
+ ${runLocalCommand
273
+ ? ` <div class="run-local-panel" id="run-local-panel" hidden data-command="${esc(runLocalCommand)}">
274
+ <code>${esc(runLocalCommand)}</code>
275
+ <button type="button" class="copy-run-local" id="copy-run-local" onclick="__anCopyRunLocalCommand()">Copy command</button>
276
+ </div>\n`
277
+ : ""}
206
278
  </div>
207
279
  </div>
208
280
  <div class="form-panel">`
@@ -616,6 +688,7 @@ ${hasMarketing
616
688
  font-size: 0.75rem;
617
689
  line-height: 1.55;
618
690
  }
691
+ .google-preflight-copy + .google-preflight-copy { margin-top: 0.5rem; }
619
692
  .google-preflight-actions {
620
693
  display: flex;
621
694
  gap: 0.5rem;
@@ -1274,6 +1347,35 @@ ${googleSignInNotice
1274
1347
  : `
1275
1348
  function __anShouldShowGoogleNotice() { return false; }`}
1276
1349
  ${starfieldScript}
1350
+ ${runLocalCommand
1351
+ ? `
1352
+ function __anToggleRunLocalCommand() {
1353
+ var panel = document.getElementById('run-local-panel');
1354
+ var button = document.getElementById('run-local-button');
1355
+ if (!panel || !button) return;
1356
+ var nextOpen = panel.hasAttribute('hidden');
1357
+ if (nextOpen) {
1358
+ panel.removeAttribute('hidden');
1359
+ } else {
1360
+ panel.setAttribute('hidden', '');
1361
+ }
1362
+ button.setAttribute('aria-expanded', String(nextOpen));
1363
+ }
1364
+ function __anCopyRunLocalCommand() {
1365
+ var panel = document.getElementById('run-local-panel');
1366
+ var button = document.getElementById('copy-run-local');
1367
+ if (!panel || !button) return;
1368
+ var command = panel.getAttribute('data-command') || '';
1369
+ var original = button.textContent || 'Copy command';
1370
+ function markCopied() {
1371
+ button.textContent = 'Copied';
1372
+ setTimeout(function() { button.textContent = original; }, 1600);
1373
+ }
1374
+ if (navigator.clipboard && navigator.clipboard.writeText) {
1375
+ navigator.clipboard.writeText(command).then(markCopied).catch(function() {});
1376
+ }
1377
+ }`
1378
+ : ""}
1277
1379
  </script>
1278
1380
  </body>
1279
1381
  </html>`;