@foldrun/core 12.1.0 → 12.2.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.
@@ -39,6 +39,7 @@ export interface EgressGrant {
39
39
  secrets: Record<string, {
40
40
  value: string;
41
41
  hosts: string[];
42
+ supply?: SupplyGrant;
42
43
  }>;
43
44
  /** Secret name → the vault entry it came from, as secret-health keys it:
44
45
  * "account:NAME" or "workspace:<ws>:NAME". Two workspaces each holding an
@@ -173,4 +174,16 @@ export declare function proxyModelEnv(env: Record<string, string | undefined>, l
173
174
  * granting the name to `host`. Longest values first, so one that contains
174
175
  * another is replaced whole. */
175
176
  export declare function unsubstitute(text: string, knownSecrets: Record<string, string>, host: string, grant: EgressGrant): string;
177
+ /** A grant of one of the PLATFORM's model keys ("foldrun's models",
178
+ * model-supply.ts). It carries no value: the proxy reads the key from its
179
+ * own environment per request, so the key is never in a lease, never in
180
+ * Redis and never in the pod. The proxy fills it only in the provider's
181
+ * auth header on the provider's model routes, and meters every call it
182
+ * makes (foldrun-platform model-gateway.ts) — the bill comes from there,
183
+ * not from what the step reports. */
184
+ export interface SupplyGrant {
185
+ provider: string;
186
+ }
187
+ /** Grant the platform's key for `provider` under `name`, to its host. */
188
+ export declare function addSupplyGrant(grant: EgressGrant, name: string, provider: string, host: string): void;
176
189
  export declare function addGrant(grant: EgressGrant, name: string, value: string, host: string): void;
@@ -130,9 +130,27 @@ export function unsubstitute(text, knownSecrets, host, grant) {
130
130
  }
131
131
  return out;
132
132
  }
133
+ /** Grant the platform's key for `provider` under `name`, to its host. */
134
+ export function addSupplyGrant(grant, name, provider, host) {
135
+ const h = host.toLowerCase();
136
+ const existing = grant.secrets[name];
137
+ // A name already granted as an ordinary secret is never turned into the
138
+ // platform's key, nor the other way round: two meanings under one name
139
+ // is how a value would be filled where it was not meant to be.
140
+ if (existing && existing.supply?.provider !== provider)
141
+ throw new Error(`egress: ${name} is already granted as another credential`);
142
+ if (existing) {
143
+ if (!existing.hosts.includes(h))
144
+ existing.hosts.push(h);
145
+ }
146
+ else
147
+ grant.secrets[name] = { value: "", hosts: [h], supply: { provider } };
148
+ }
133
149
  export function addGrant(grant, name, value, host) {
134
150
  const h = host.toLowerCase();
135
151
  const existing = grant.secrets[name];
152
+ if (existing?.supply)
153
+ throw new Error(`egress: ${name} is the platform's model key and cannot be granted a value`);
136
154
  if (existing) {
137
155
  if (!existing.hosts.includes(h))
138
156
  existing.hosts.push(h);
@@ -0,0 +1,34 @@
1
+ import { type WireFormat } from "./providers.ts";
2
+ export type SupplyProviderName = "anthropic" | "openai" | "gemini" | "openrouter";
3
+ export interface SupplyProvider {
4
+ name: SupplyProviderName;
5
+ /** The preset the step's provider block is built from (providers.ts). */
6
+ preset: string;
7
+ format: WireFormat;
8
+ /** The one host the key is granted to. */
9
+ host: string;
10
+ /** Where the platform's key lives, in the worker's and the proxy's
11
+ * environment. Never in the vault, never in a lease. */
12
+ env: string;
13
+ /** The header the key may be filled in, and its exact shape: the whole
14
+ * value of x-api-key, or "Bearer <key>" in authorization. Anywhere else —
15
+ * the URL, the body, another header — is refused. */
16
+ auth: "x-api-key" | "bearer";
17
+ /** Paths (the URL's pathname, exact) a call is metered on: POST only. */
18
+ metered: string[];
19
+ /** Paths that cost nothing and may pass unmetered (GET model lists, a
20
+ * free token count). Anything not listed in either is refused. */
21
+ free: {
22
+ method: "GET" | "POST";
23
+ path: string;
24
+ }[];
25
+ }
26
+ export declare const SUPPLY_PROVIDERS: Record<SupplyProviderName, SupplyProvider>;
27
+ export declare const SUPPLY_PROVIDER_NAMES: SupplyProviderName[];
28
+ export declare function isSupplyProvider(name: unknown): name is SupplyProviderName;
29
+ /** The placeholder name a supply grant is filed under in a step's lease. */
30
+ export declare const SUPPLY_KEY_NAME = "FOLDRUN_SUPPLY_KEY";
31
+ /** The provider block a step on foldrun's models runs with: the preset's
32
+ * endpoint and format, the operator's tier map, and the key as a
33
+ * placeholder only. */
34
+ export declare function supplyProviderBlock(provider: SupplyProviderName, tiers: Partial<Record<"fast" | "default" | "max", string>>): Record<string, unknown>;
@@ -0,0 +1,81 @@
1
+ // "foldrun's models": the platform's own keys for a few providers, which a
2
+ // hosted account may choose instead of bringing its own (3 Nov 2026).
3
+ //
4
+ // One table, read by both halves:
5
+ // - the runner (core) builds the step's provider block from it and grants
6
+ // the key to the egress proxy as a supply grant (egress.ts SupplyGrant),
7
+ // so the key never reaches the pod;
8
+ // - the proxy's model gateway (foldrun-platform model-gateway.ts) fills
9
+ // that grant only in the provider's auth header, on the provider's model
10
+ // routes, and meters every call — the bill comes from the gateway.
11
+ //
12
+ // A provider here is offered only where the platform holds its key
13
+ // (`env`), the operator lists it, and the price list names the model.
14
+ import { providerPreset } from "./providers.js";
15
+ export const SUPPLY_PROVIDERS = {
16
+ anthropic: {
17
+ name: "anthropic",
18
+ preset: "anthropic",
19
+ format: "anthropic",
20
+ host: "api.anthropic.com",
21
+ env: "FOLDRUN_SUPPLY_ANTHROPIC_API_KEY",
22
+ auth: "x-api-key",
23
+ metered: ["/v1/messages"],
24
+ free: [
25
+ { method: "POST", path: "/v1/messages/count_tokens" },
26
+ { method: "GET", path: "/v1/models" },
27
+ ],
28
+ },
29
+ openrouter: {
30
+ name: "openrouter",
31
+ preset: "openrouter",
32
+ format: "anthropic",
33
+ host: "openrouter.ai",
34
+ env: "FOLDRUN_SUPPLY_OPENROUTER_API_KEY",
35
+ auth: "bearer",
36
+ metered: ["/api/v1/messages"],
37
+ free: [{ method: "GET", path: "/api/v1/models" }],
38
+ },
39
+ openai: {
40
+ name: "openai",
41
+ preset: "openai",
42
+ format: "openai",
43
+ host: "api.openai.com",
44
+ env: "FOLDRUN_SUPPLY_OPENAI_API_KEY",
45
+ auth: "bearer",
46
+ metered: ["/v1/chat/completions"],
47
+ free: [{ method: "GET", path: "/v1/models" }],
48
+ },
49
+ gemini: {
50
+ name: "gemini",
51
+ preset: "gemini",
52
+ format: "openai",
53
+ host: "generativelanguage.googleapis.com",
54
+ env: "FOLDRUN_SUPPLY_GEMINI_API_KEY",
55
+ auth: "bearer",
56
+ metered: ["/v1beta/openai/chat/completions"],
57
+ free: [{ method: "GET", path: "/v1beta/openai/models" }],
58
+ },
59
+ };
60
+ export const SUPPLY_PROVIDER_NAMES = Object.keys(SUPPLY_PROVIDERS);
61
+ export function isSupplyProvider(name) {
62
+ return typeof name === "string" && Object.hasOwn(SUPPLY_PROVIDERS, name);
63
+ }
64
+ /** The placeholder name a supply grant is filed under in a step's lease. */
65
+ export const SUPPLY_KEY_NAME = "FOLDRUN_SUPPLY_KEY";
66
+ /** The provider block a step on foldrun's models runs with: the preset's
67
+ * endpoint and format, the operator's tier map, and the key as a
68
+ * placeholder only. */
69
+ export function supplyProviderBlock(provider, tiers) {
70
+ const p = SUPPLY_PROVIDERS[provider];
71
+ const preset = providerPreset(p.preset);
72
+ if (!preset?.baseUrl)
73
+ throw new Error(`model supply: no preset endpoint for ${provider}`);
74
+ return {
75
+ name: p.preset,
76
+ base_url: preset.baseUrl,
77
+ format: p.format,
78
+ token: `\${${SUPPLY_KEY_NAME}}`,
79
+ ...(Object.keys(tiers).length ? { models: tiers } : {}),
80
+ };
81
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@foldrun/core",
3
- "version": "12.1.0",
3
+ "version": "12.2.0",
4
4
  "description": "Agents are just folders. The format, the runner, the checks.",
5
5
  "license": "FSL-1.1-ALv2",
6
6
  "type": "module",
package/src/egress.ts CHANGED
@@ -106,7 +106,7 @@ export interface LeaseWho {
106
106
 
107
107
  export interface EgressGrant {
108
108
  /** Secret name → the hosts that secret may be substituted for. */
109
- secrets: Record<string, { value: string; hosts: string[] }>;
109
+ secrets: Record<string, { value: string; hosts: string[]; supply?: SupplyGrant }>;
110
110
  /** Secret name → the vault entry it came from, as secret-health keys it:
111
111
  * "account:NAME" or "workspace:<ws>:NAME". Two workspaces each holding an
112
112
  * API_KEY are two credentials, and one's refusal must not paint the
@@ -260,9 +260,34 @@ export function unsubstitute(text: string, knownSecrets: Record<string, string>,
260
260
  return out;
261
261
  }
262
262
 
263
+ /** A grant of one of the PLATFORM's model keys ("foldrun's models",
264
+ * model-supply.ts). It carries no value: the proxy reads the key from its
265
+ * own environment per request, so the key is never in a lease, never in
266
+ * Redis and never in the pod. The proxy fills it only in the provider's
267
+ * auth header on the provider's model routes, and meters every call it
268
+ * makes (foldrun-platform model-gateway.ts) — the bill comes from there,
269
+ * not from what the step reports. */
270
+ export interface SupplyGrant {
271
+ provider: string;
272
+ }
273
+
274
+ /** Grant the platform's key for `provider` under `name`, to its host. */
275
+ export function addSupplyGrant(grant: EgressGrant, name: string, provider: string, host: string): void {
276
+ const h = host.toLowerCase();
277
+ const existing = grant.secrets[name];
278
+ // A name already granted as an ordinary secret is never turned into the
279
+ // platform's key, nor the other way round: two meanings under one name
280
+ // is how a value would be filled where it was not meant to be.
281
+ if (existing && existing.supply?.provider !== provider) throw new Error(`egress: ${name} is already granted as another credential`);
282
+ if (existing) {
283
+ if (!existing.hosts.includes(h)) existing.hosts.push(h);
284
+ } else grant.secrets[name] = { value: "", hosts: [h], supply: { provider } };
285
+ }
286
+
263
287
  export function addGrant(grant: EgressGrant, name: string, value: string, host: string): void {
264
288
  const h = host.toLowerCase();
265
289
  const existing = grant.secrets[name];
290
+ if (existing?.supply) throw new Error(`egress: ${name} is the platform's model key and cannot be granted a value`);
266
291
  if (existing) {
267
292
  if (!existing.hosts.includes(h)) existing.hosts.push(h);
268
293
  // A name granted again with a different value is a rotation: the
@@ -0,0 +1,109 @@
1
+ // "foldrun's models": the platform's own keys for a few providers, which a
2
+ // hosted account may choose instead of bringing its own (3 Nov 2026).
3
+ //
4
+ // One table, read by both halves:
5
+ // - the runner (core) builds the step's provider block from it and grants
6
+ // the key to the egress proxy as a supply grant (egress.ts SupplyGrant),
7
+ // so the key never reaches the pod;
8
+ // - the proxy's model gateway (foldrun-platform model-gateway.ts) fills
9
+ // that grant only in the provider's auth header, on the provider's model
10
+ // routes, and meters every call — the bill comes from the gateway.
11
+ //
12
+ // A provider here is offered only where the platform holds its key
13
+ // (`env`), the operator lists it, and the price list names the model.
14
+
15
+ import { providerPreset, type WireFormat } from "./providers.ts";
16
+
17
+ export type SupplyProviderName = "anthropic" | "openai" | "gemini" | "openrouter";
18
+
19
+ export interface SupplyProvider {
20
+ name: SupplyProviderName;
21
+ /** The preset the step's provider block is built from (providers.ts). */
22
+ preset: string;
23
+ format: WireFormat;
24
+ /** The one host the key is granted to. */
25
+ host: string;
26
+ /** Where the platform's key lives, in the worker's and the proxy's
27
+ * environment. Never in the vault, never in a lease. */
28
+ env: string;
29
+ /** The header the key may be filled in, and its exact shape: the whole
30
+ * value of x-api-key, or "Bearer <key>" in authorization. Anywhere else —
31
+ * the URL, the body, another header — is refused. */
32
+ auth: "x-api-key" | "bearer";
33
+ /** Paths (the URL's pathname, exact) a call is metered on: POST only. */
34
+ metered: string[];
35
+ /** Paths that cost nothing and may pass unmetered (GET model lists, a
36
+ * free token count). Anything not listed in either is refused. */
37
+ free: { method: "GET" | "POST"; path: string }[];
38
+ }
39
+
40
+ export const SUPPLY_PROVIDERS: Record<SupplyProviderName, SupplyProvider> = {
41
+ anthropic: {
42
+ name: "anthropic",
43
+ preset: "anthropic",
44
+ format: "anthropic",
45
+ host: "api.anthropic.com",
46
+ env: "FOLDRUN_SUPPLY_ANTHROPIC_API_KEY",
47
+ auth: "x-api-key",
48
+ metered: ["/v1/messages"],
49
+ free: [
50
+ { method: "POST", path: "/v1/messages/count_tokens" },
51
+ { method: "GET", path: "/v1/models" },
52
+ ],
53
+ },
54
+ openrouter: {
55
+ name: "openrouter",
56
+ preset: "openrouter",
57
+ format: "anthropic",
58
+ host: "openrouter.ai",
59
+ env: "FOLDRUN_SUPPLY_OPENROUTER_API_KEY",
60
+ auth: "bearer",
61
+ metered: ["/api/v1/messages"],
62
+ free: [{ method: "GET", path: "/api/v1/models" }],
63
+ },
64
+ openai: {
65
+ name: "openai",
66
+ preset: "openai",
67
+ format: "openai",
68
+ host: "api.openai.com",
69
+ env: "FOLDRUN_SUPPLY_OPENAI_API_KEY",
70
+ auth: "bearer",
71
+ metered: ["/v1/chat/completions"],
72
+ free: [{ method: "GET", path: "/v1/models" }],
73
+ },
74
+ gemini: {
75
+ name: "gemini",
76
+ preset: "gemini",
77
+ format: "openai",
78
+ host: "generativelanguage.googleapis.com",
79
+ env: "FOLDRUN_SUPPLY_GEMINI_API_KEY",
80
+ auth: "bearer",
81
+ metered: ["/v1beta/openai/chat/completions"],
82
+ free: [{ method: "GET", path: "/v1beta/openai/models" }],
83
+ },
84
+ };
85
+
86
+ export const SUPPLY_PROVIDER_NAMES = Object.keys(SUPPLY_PROVIDERS) as SupplyProviderName[];
87
+
88
+ export function isSupplyProvider(name: unknown): name is SupplyProviderName {
89
+ return typeof name === "string" && Object.hasOwn(SUPPLY_PROVIDERS, name);
90
+ }
91
+
92
+ /** The placeholder name a supply grant is filed under in a step's lease. */
93
+ export const SUPPLY_KEY_NAME = "FOLDRUN_SUPPLY_KEY";
94
+
95
+ /** The provider block a step on foldrun's models runs with: the preset's
96
+ * endpoint and format, the operator's tier map, and the key as a
97
+ * placeholder only. */
98
+ export function supplyProviderBlock(provider: SupplyProviderName, tiers: Partial<Record<"fast" | "default" | "max", string>>): Record<string, unknown> {
99
+ const p = SUPPLY_PROVIDERS[provider];
100
+ const preset = providerPreset(p.preset);
101
+ if (!preset?.baseUrl) throw new Error(`model supply: no preset endpoint for ${provider}`);
102
+ return {
103
+ name: p.preset,
104
+ base_url: preset.baseUrl,
105
+ format: p.format,
106
+ token: `\${${SUPPLY_KEY_NAME}}`,
107
+ ...(Object.keys(tiers).length ? { models: tiers } : {}),
108
+ };
109
+ }