Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | 1x 1x 1x 1x 1x 1x 1x 208x 1x 183x 1x 1x 1x 1x 47x 1x 45x 45x 43x 45x 1x 37x 1x 7x 7x 7x 7x 1x 4x 4x 2x 2x 2x 2x 2x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import type { AppContext } from '@api/types/hono';
const DUMMY_JWT_SECRET = 'default-dev-jwt-secret';
/**
* Base URL the API uses to call *itself*.
*
* The internal skills (judging, embedding, prompt generation) are ordinary
* gateway requests that the server sends back to its own `/v1`, so this has to
* name the port it is actually listening on. Deriving it from `PORT` rather
* than hardcoding one keeps the all-in-one image (3000), the gateway-only image
* (8787) and `wrangler dev` (8787) all correct without configuration.
*
* Getting this wrong is invisible: every internal call fails to connect, each
* caller swallows the error, and optimization simply stops happening while
* ordinary requests carry on being served.
*/
export const getApiUrl = (c: AppContext) =>
c.env.API_URL ?? `http://localhost:${c.env.PORT ?? 8787}`;
export const AUTH_COOKIE_MAX_AGE = 60 * 60 * 24 * 7; // 1 week in seconds
/**
* Supabase URL for local development.
*
* Using default Supabase URL for local development.
*
* @see https://supabase.com/docs/guides/local-development
*/
const getSupabaseUrl = (c: AppContext): string | undefined =>
c.env.SUPABASE_URL ??
(c.env.NODE_ENV !== 'production' ? 'http://127.0.0.1:54321' : undefined);
/**
* PostgREST URL.
*
* For Supabase, we simply need to add /rest/v1 to the Supabase URL.
*/
export const getPostgrestUrl = (c: AppContext) => {
const postgrestUrl = c.env.POSTGREST_URL;
if (postgrestUrl) {
return postgrestUrl;
}
const supabaseUrl = getSupabaseUrl(c);
if (supabaseUrl) {
return `${supabaseUrl}/rest/v1`;
}
throw new Error(
'POSTGREST_URL environment variable is required in production.',
);
};
/**
* libSQL database URL.
*
* `file:` points at an embedded SQLite database, which is what the
* single-container deployment uses; `libsql://` or `https://` points at a
* remote database (Turso), which is what a Workers deployment or any
* multi-instance deployment needs.
*/
export const getLibsqlUrl = (c: AppContext): string | undefined =>
// Optional access: these two are read on every request by the storage
// middleware, including from apps constructed without bindings, where
// `c.env` is undefined.
c.env?.LIBSQL_URL;
/** Auth token for a remote libSQL database. Unused by `file:` databases. */
export const getLibsqlAuthToken = (c: AppContext): string | undefined =>
c.env?.LIBSQL_AUTH_TOKEN;
/**
* How long a cached response stays valid, in seconds.
*
* `CacheStorageConnector.setCache` takes no TTL, so the backend decides.
*/
export const CACHE_TTL_SECONDS = 60 * 60; // 1 hour
/**
* Supabase Secret key
*/
export const getSupabaseSecretKey = (c: AppContext): string | undefined => {
const key = c.env.SUPABASE_SECRET_KEY;
if (key) {
return key;
} else if (c.env.NODE_ENV !== 'production') {
// Default to development key used by supabase
return 'sb_secret_N7UND0UgjKTVK-Uodkm0Hg_xSvEMPvz';
}
};
/**
* PostgREST Service Role.
*
* This is the key used to authenticate requests to the PostgREST API.
* For Supabase, this is the same as its secret key.
*/
export const getPostgrestServiceRoleKey = (c: AppContext): string => {
const key = c.env.POSTGREST_SERVICE_ROLE_KEY ?? getSupabaseSecretKey(c);
if (key) {
return key;
}
throw new Error(
'POSTGREST_SERVICE_ROLE_KEY environment variable is required in production. Set it to a strong, random secret.',
);
};
export const getAccessPassword = (c: AppContext): string | undefined =>
c.env.ACCESS_PASSWORD;
export const getAuthJwtSecret = (c: AppContext): string => {
const secret = c.env.AUTH_JWT_SECRET;
if (!secret && c.env.NODE_ENV === 'production') {
throw new Error(
'AUTH_JWT_SECRET environment variable is required in production. Set it to a strong, random secret.',
);
}
return secret ?? DUMMY_JWT_SECRET;
};
/**
* Bearer token for API authentication.
*
* If not set, API requests without JWT authentication will be allowed through.
* Set this to require Bearer token authentication for API access.
*/
export const getBearerToken = (c: AppContext): string | undefined =>
c.env.BEARER_TOKEN;
/**
* Encryption key for AI provider API keys.
*
* You should absolutely change this in production!
*/
export const getAiProviderApiKeyEncryptionKey = (c: AppContext): string => {
const key = c.env.AI_PROVIDER_API_KEY_ENCRYPTION_KEY;
if (key) {
return key;
} else if (c.env.NODE_ENV !== 'production') {
return 'default-32-byte-key-change-in-prod';
}
throw new Error(
'AI_PROVIDER_API_KEY_ENCRYPTION_KEY environment variable is required in production. Set it to a strong, random secret.',
);
};
/**
* Origins allowed to make credentialed cross-origin requests to the API.
*
* `WEB_APP_URL` accepts a comma-separated list. The Docker deployment serves the
* dashboard and proxies `/v1/*` from the same nginx origin, and Vite proxies the
* same paths in development, so CORS only matters when the dashboard is hosted
* separately — hence the empty production default rather than a permissive one.
*/
export const getAllowedOrigins = (c: AppContext): string[] => {
const webAppUrl = c.env.WEB_APP_URL;
if (webAppUrl) {
return webAppUrl
.split(',')
.map((origin) => origin.trim())
.filter(Boolean);
}
if (c.env.NODE_ENV === 'production') {
return [];
}
return [
'http://localhost:3000',
'http://localhost:3001',
'http://localhost:8787',
];
};
/**
* Special skills that super-agents uses internally. We auto generate these if they don't exist.
*/
export const SA_SKILLS = [
'judge',
'extract-task-and-outcome',
'create-evaluations',
'system-prompt-seeding',
'system-prompt-seeding-with-context',
'system-prompt-reflection',
'embedding',
];
|