@frankieai/server 0.2.0 → 0.2.2

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.
@@ -0,0 +1,75 @@
1
+ // src/config.ts
2
+ import { readFile } from "fs/promises";
3
+ import { resolve } from "path";
4
+ import { parseFrankieConfig } from "@frankieai/core";
5
+ var ConfigError = class extends Error {
6
+ };
7
+ async function loadConfigFromFile(path) {
8
+ const target = resolve(path ?? process.env["FRANKIE_CONFIG"] ?? "frankie.config.json");
9
+ let raw;
10
+ try {
11
+ raw = await readFile(target, "utf8");
12
+ } catch {
13
+ throw new ConfigError(
14
+ `Could not read Frankie config at ${target}. Set FRANKIE_CONFIG to point at your frankie.config.json.`
15
+ );
16
+ }
17
+ try {
18
+ return parseFrankieConfig(applyEnvOverrides(JSON.parse(raw)));
19
+ } catch (error) {
20
+ throw new ConfigError(`Invalid Frankie config at ${target}: ${error.message}`);
21
+ }
22
+ }
23
+ function applyEnvOverrides(raw, env = process.env) {
24
+ const out = { ...raw };
25
+ const model = { ...isRecord(out["model"]) ? out["model"] : {} };
26
+ if (env["FRANKIE_MODEL_PROVIDER"]) model["provider"] = env["FRANKIE_MODEL_PROVIDER"];
27
+ if (env["FRANKIE_MODEL"]) model["model"] = env["FRANKIE_MODEL"];
28
+ if (env["FRANKIE_MODEL_BASE_URL"]) model["baseUrl"] = env["FRANKIE_MODEL_BASE_URL"];
29
+ if (Object.keys(model).length > 0) out["model"] = model;
30
+ const auth = { ...isRecord(out["auth"]) ? out["auth"] : {} };
31
+ if (env["FRANKIE_ALLOWED_ORIGINS"]) {
32
+ auth["allowedOrigins"] = env["FRANKIE_ALLOWED_ORIGINS"].split(",").map((o) => o.trim()).filter(Boolean);
33
+ }
34
+ if (env["FRANKIE_PUBLISHABLE_KEY"]) auth["publishableKey"] = env["FRANKIE_PUBLISHABLE_KEY"];
35
+ if (Object.keys(auth).length > 0) out["auth"] = auth;
36
+ return out;
37
+ }
38
+ function isRecord(value) {
39
+ return typeof value === "object" && value !== null && !Array.isArray(value);
40
+ }
41
+ function resolveSecrets(config, env = process.env) {
42
+ const missing = [];
43
+ const jwtSecret = env[config.auth.jwtSecretEnv];
44
+ if (!config.auth.jwksUrl && !jwtSecret) missing.push(config.auth.jwtSecretEnv);
45
+ const sessionSecret = env[config.auth.sessionSecretEnv];
46
+ if (!sessionSecret) missing.push(config.auth.sessionSecretEnv);
47
+ const providerApiKey = env[config.model.apiKeyEnv];
48
+ if (!providerApiKey) missing.push(config.model.apiKeyEnv);
49
+ if (missing.length) {
50
+ throw new ConfigError(`Missing required environment variables: ${missing.join(", ")}`);
51
+ }
52
+ if (sessionSecret.length < 32) {
53
+ throw new ConfigError(`${config.auth.sessionSecretEnv} must be at least 32 characters.`);
54
+ }
55
+ return {
56
+ config,
57
+ secrets: {
58
+ jwtSecret,
59
+ sessionSecret,
60
+ providerApiKey
61
+ }
62
+ };
63
+ }
64
+ async function loadResolvedConfig(path) {
65
+ return resolveSecrets(await loadConfigFromFile(path));
66
+ }
67
+
68
+ export {
69
+ ConfigError,
70
+ loadConfigFromFile,
71
+ applyEnvOverrides,
72
+ resolveSecrets,
73
+ loadResolvedConfig
74
+ };
75
+ //# sourceMappingURL=chunk-NF6IJVES.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/config.ts"],"sourcesContent":["import { readFile } from \"node:fs/promises\";\nimport { resolve } from \"node:path\";\nimport { parseFrankieConfig, type FrankieConfig } from \"@frankieai/core\";\n\n/**\n * Secrets never live in the config file — the file names the env var that\n * holds each one. `ResolvedConfig` is the config plus the resolved values.\n */\nexport interface ResolvedConfig {\n config: FrankieConfig;\n secrets: {\n /** HS256 secret the host signs end-user tokens with. Absent when using JWKS. */\n jwtSecret?: string;\n /** Signs Frankie's own session tokens. */\n sessionSecret: string;\n /** Key for the model provider named in `model.provider`. */\n providerApiKey: string;\n };\n}\n\nexport class ConfigError extends Error {}\n\nexport async function loadConfigFromFile(path?: string): Promise<FrankieConfig> {\n const target = resolve(path ?? process.env[\"FRANKIE_CONFIG\"] ?? \"frankie.config.json\");\n let raw: string;\n try {\n raw = await readFile(target, \"utf8\");\n } catch {\n throw new ConfigError(\n `Could not read Frankie config at ${target}. Set FRANKIE_CONFIG to point at your frankie.config.json.`,\n );\n }\n try {\n return parseFrankieConfig(applyEnvOverrides(JSON.parse(raw) as Record<string, unknown>));\n } catch (error) {\n throw new ConfigError(`Invalid Frankie config at ${target}: ${(error as Error).message}`);\n }\n}\n\n/**\n * A handful of fields have to move with the deployment rather than the config\n * file — the model and the origin allowlist are what an operator most often\n * wants to flip without a redeploy.\n */\nexport function applyEnvOverrides(\n raw: Record<string, unknown>,\n env: NodeJS.ProcessEnv = process.env,\n): Record<string, unknown> {\n const out = { ...raw };\n\n const model = { ...(isRecord(out[\"model\"]) ? out[\"model\"] : {}) };\n if (env[\"FRANKIE_MODEL_PROVIDER\"]) model[\"provider\"] = env[\"FRANKIE_MODEL_PROVIDER\"];\n if (env[\"FRANKIE_MODEL\"]) model[\"model\"] = env[\"FRANKIE_MODEL\"];\n if (env[\"FRANKIE_MODEL_BASE_URL\"]) model[\"baseUrl\"] = env[\"FRANKIE_MODEL_BASE_URL\"];\n if (Object.keys(model).length > 0) out[\"model\"] = model;\n\n const auth = { ...(isRecord(out[\"auth\"]) ? out[\"auth\"] : {}) };\n if (env[\"FRANKIE_ALLOWED_ORIGINS\"]) {\n auth[\"allowedOrigins\"] = env[\"FRANKIE_ALLOWED_ORIGINS\"].split(\",\").map((o) => o.trim()).filter(Boolean);\n }\n if (env[\"FRANKIE_PUBLISHABLE_KEY\"]) auth[\"publishableKey\"] = env[\"FRANKIE_PUBLISHABLE_KEY\"];\n if (Object.keys(auth).length > 0) out[\"auth\"] = auth;\n\n return out;\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\nexport function resolveSecrets(config: FrankieConfig, env: NodeJS.ProcessEnv = process.env): ResolvedConfig {\n const missing: string[] = [];\n\n const jwtSecret = env[config.auth.jwtSecretEnv];\n if (!config.auth.jwksUrl && !jwtSecret) missing.push(config.auth.jwtSecretEnv);\n\n const sessionSecret = env[config.auth.sessionSecretEnv];\n if (!sessionSecret) missing.push(config.auth.sessionSecretEnv);\n\n const providerApiKey = env[config.model.apiKeyEnv];\n if (!providerApiKey) missing.push(config.model.apiKeyEnv);\n\n if (missing.length) {\n throw new ConfigError(`Missing required environment variables: ${missing.join(\", \")}`);\n }\n\n if (sessionSecret!.length < 32) {\n throw new ConfigError(`${config.auth.sessionSecretEnv} must be at least 32 characters.`);\n }\n\n return {\n config,\n secrets: {\n jwtSecret,\n sessionSecret: sessionSecret!,\n providerApiKey: providerApiKey!,\n },\n };\n}\n\nexport async function loadResolvedConfig(path?: string): Promise<ResolvedConfig> {\n return resolveSecrets(await loadConfigFromFile(path));\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AACzB,SAAS,eAAe;AACxB,SAAS,0BAA8C;AAkBhD,IAAM,cAAN,cAA0B,MAAM;AAAC;AAExC,eAAsB,mBAAmB,MAAuC;AAC9E,QAAM,SAAS,QAAQ,QAAQ,QAAQ,IAAI,gBAAgB,KAAK,qBAAqB;AACrF,MAAI;AACJ,MAAI;AACF,UAAM,MAAM,SAAS,QAAQ,MAAM;AAAA,EACrC,QAAQ;AACN,UAAM,IAAI;AAAA,MACR,oCAAoC,MAAM;AAAA,IAC5C;AAAA,EACF;AACA,MAAI;AACF,WAAO,mBAAmB,kBAAkB,KAAK,MAAM,GAAG,CAA4B,CAAC;AAAA,EACzF,SAAS,OAAO;AACd,UAAM,IAAI,YAAY,6BAA6B,MAAM,KAAM,MAAgB,OAAO,EAAE;AAAA,EAC1F;AACF;AAOO,SAAS,kBACd,KACA,MAAyB,QAAQ,KACR;AACzB,QAAM,MAAM,EAAE,GAAG,IAAI;AAErB,QAAM,QAAQ,EAAE,GAAI,SAAS,IAAI,OAAO,CAAC,IAAI,IAAI,OAAO,IAAI,C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