@oneie/sdk 0.16.2 → 0.16.3
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.
- package/dist/.build-fingerprint +1 -0
- package/dist/billing.d.ts +50 -5
- package/dist/billing.d.ts.map +1 -1
- package/dist/billing.js +59 -5
- package/dist/billing.js.map +1 -1
- package/dist/free-tier.d.ts +83 -0
- package/dist/free-tier.d.ts.map +1 -0
- package/dist/free-tier.js +106 -0
- package/dist/free-tier.js.map +1 -0
- package/dist/gateway-meta.d.ts +68 -0
- package/dist/gateway-meta.d.ts.map +1 -0
- package/dist/gateway-meta.js +80 -0
- package/dist/gateway-meta.js.map +1 -0
- package/dist/gateway-route.d.ts +36 -0
- package/dist/gateway-route.d.ts.map +1 -0
- package/dist/gateway-route.js +59 -0
- package/dist/gateway-route.js.map +1 -0
- package/dist/generated/base-manifest.d.ts.map +1 -1
- package/dist/generated/base-manifest.js +5 -0
- package/dist/generated/base-manifest.js.map +1 -1
- package/dist/generated/fn-map.d.ts +2 -2
- package/dist/generated/fn-map.d.ts.map +1 -1
- package/dist/generated/fn-map.js +116 -2
- package/dist/generated/fn-map.js.map +1 -1
- package/dist/generated/schemas.d.ts +34 -0
- package/dist/generated/schemas.d.ts.map +1 -1
- package/dist/generated/schemas.js +15 -0
- package/dist/generated/schemas.js.map +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/receivers.d.ts +184 -2
- package/dist/receivers.d.ts.map +1 -1
- package/dist/receivers.js +1079 -851
- package/dist/receivers.js.map +1 -1
- package/dist/work-contract-grammar.json +67 -0
- package/package.json +10 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
c3c025e698760b358295bf1388e5aefb1530f09b6cab06ae43de27f25c2adff9
|
package/dist/billing.d.ts
CHANGED
|
@@ -58,6 +58,50 @@ export type ComputeBurnInput = {
|
|
|
58
58
|
};
|
|
59
59
|
};
|
|
60
60
|
export declare function computeBurn(p: ComputeBurnInput): BillingBurn;
|
|
61
|
+
/**
|
|
62
|
+
* The monthly cap on a sponsored child — a workspace whose credits are paid from
|
|
63
|
+
* an ancestor's pool — when it has no allocation and its parent set no
|
|
64
|
+
* `monthly_cap`. 1 credit = $0.001, so $50. Until this existed a NULL cap meant
|
|
65
|
+
* unlimited: a child past every budget was walked to the sponsor's pool, served,
|
|
66
|
+
* and (the refusal returning before the row) never recorded.
|
|
67
|
+
*/
|
|
68
|
+
export declare const SPONSOR_DEFAULT_MONTHLY_CREDITS = 50000;
|
|
69
|
+
export type BudgetAlloc = {
|
|
70
|
+
monthly_credits: number;
|
|
71
|
+
period_used: number;
|
|
72
|
+
};
|
|
73
|
+
export type BudgetSource = 'allocation' | 'parent_cap' | 'sponsor_default' | 'invalid';
|
|
74
|
+
export type BudgetVerdict = {
|
|
75
|
+
ok: true;
|
|
76
|
+
} | {
|
|
77
|
+
ok: false;
|
|
78
|
+
reason: 'budget_exhausted';
|
|
79
|
+
source: BudgetSource;
|
|
80
|
+
limit: number;
|
|
81
|
+
used: number;
|
|
82
|
+
needed: number;
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* May a sponsored child spend `needed` more credits of its ancestor's pool this
|
|
86
|
+
* month? Pure. The pre-flight gates (web `lib/credits.ts`, channels
|
|
87
|
+
* `credit-gate.ts`) call it BEFORE the model whenever the funder is not the
|
|
88
|
+
* workspace itself, with the same D1 reads debitPool uses: the parent's
|
|
89
|
+
* `monthly_cap`, this month's `credit_burns` where workspace OR origin is the
|
|
90
|
+
* child, and the tightest transfer allocation.
|
|
91
|
+
*
|
|
92
|
+
* FAIL-CLOSED: a non-finite input (a NaN makes every `>` false, which reads as
|
|
93
|
+
* room) refuses with source `invalid`. A cap of 0 is a cap of 0 (`!= null`,
|
|
94
|
+
* never truthiness). Reaching a limit exactly is allowed; exceeding it is not —
|
|
95
|
+
* the same `>` debitPool uses. `sponsored: false` never refuses: a self-funded
|
|
96
|
+
* workspace spends its own money, and debitPool still judges it after the turn.
|
|
97
|
+
*/
|
|
98
|
+
export declare function budgetVerdict(p: {
|
|
99
|
+
alloc: BudgetAlloc | null;
|
|
100
|
+
parentCap: number | null;
|
|
101
|
+
monthToDate: number;
|
|
102
|
+
needed: number;
|
|
103
|
+
sponsored: boolean;
|
|
104
|
+
}): BudgetVerdict;
|
|
61
105
|
/** `live` = the blob carried the model · `fallback` = rateFor's last-resort constant. */
|
|
62
106
|
export type RateSource = 'live' | 'fallback';
|
|
63
107
|
export type SpendInput = {
|
|
@@ -110,15 +154,16 @@ export declare function spend(p: SpendInput): Spend | null;
|
|
|
110
154
|
* meter's old default was NOT (measured 2026-09-17, 444 ids), so it priced at the
|
|
111
155
|
* hard fallback in `rateFor` and never at a live rate.
|
|
112
156
|
*
|
|
113
|
-
* Moved 4.5 → 4.7 on 2026-09-26, the operator's
|
|
114
|
-
* task:01a0d887f8eba48aa2b5f342).
|
|
115
|
-
*
|
|
116
|
-
*
|
|
157
|
+
* Moved 4.5 → 4.7 on 2026-09-26 and BACK to 4.5 on 2026-09-27, the operator's
|
|
158
|
+
* call (speed row task:01a0d887f8eba48aa2b5f342). Measured on production: 4.7's
|
|
159
|
+
* short-answer ttft p50 was 2,415 ms (n=5) against 4.5's 1,348 ms, and three
|
|
160
|
+
* traced 4.7 turns ended at 16.0–22.5 s. 4.7 is cheaper per token ($1.60/$4.80
|
|
161
|
+
* vs $2/$6); speed decided it.
|
|
117
162
|
*
|
|
118
163
|
* It lives beside `rateFor` on purpose: the id the bill names and the id the turn
|
|
119
164
|
* runs on are one fact, and this subpath is the one both workers already import.
|
|
120
165
|
*/
|
|
121
|
-
export declare const DEFAULT_MODEL = "x-ai/grok-4.
|
|
166
|
+
export declare const DEFAULT_MODEL = "x-ai/grok-4.5";
|
|
122
167
|
/** Which rung of the walk answered. The page names it; the bill does not care. */
|
|
123
168
|
export type ModelRung = 'agent' | 'group' | 'ancestor' | 'platform';
|
|
124
169
|
/** A group (workspace) and the default model it carries, if any. */
|
package/dist/billing.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"billing.d.ts","sourceRoot":"","sources":["../src/billing.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,MAAM,SAAS,GAAG;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAAC,eAAe,CAAC,EAAE,MAAM,CAAA;CAAE,CAAA;AACtF,MAAM,MAAM,QAAQ,GAAG;IAAE,MAAM,EAAE,SAAS,EAAE,CAAA;CAAE,CAAA;AAE9C;;;GAGG;AACH,eAAO,MAAM,eAAe,OAAQ,CAAA;AAEpC,qEAAqE;AACrE,eAAO,MAAM,cAAc,QAAsB,CAAA;AAEjD,2EAA2E;AAC3E,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEjD;AAED,gEAAgE;AAChE,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAErD;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CACrB,OAAO,EAAE,MAAM,EACf,MAAM,CAAC,EAAE,QAAQ,GAAG,IAAI,GACvB;IAAE,YAAY,EAAE,MAAM,CAAC;IAAC,aAAa,EAAE,MAAM,CAAA;CAAE,CA6BjD;AAED,MAAM,MAAM,WAAW,GAAG;IACxB,SAAS,EAAE,MAAM,CAAA;IACjB,MAAM,EAAE,MAAM,CAAA;IACd,cAAc,EAAE,MAAM,CAAA;IACtB,YAAY,EAAE,MAAM,CAAA;IACpB,aAAa,EAAE,MAAM,CAAA;IACrB,eAAe,EAAE,MAAM,CAAA;IACvB,eAAe,EAAE,MAAM,CAAA;IACvB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAA;IACxB,KAAK,EAAE,MAAM,GAAG,IAAI,CAAA;CACrB,CAAA;AAED,MAAM,MAAM,gBAAgB,GAAG;IAC7B,QAAQ,EAAE,MAAM,CA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Q,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CACzB;AAED,MAAM,WAAW,aAAa;IAC5B,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,SAAS,CAAA;IACf,iEAAiE;IACjE,IAAI,CAAC,EAAE,MAAM,CAAA;CACd;AAID;;;GAGG;AACH,wBAAgB,YAAY,CAAC,CAAC,GAAE,YAAiB,GAAG,aAAa,CAOhE"}
|
package/dist/billing.js
CHANGED
|
@@ -76,6 +76,59 @@ export function computeBurn(p) {
|
|
|
76
76
|
model: p.model ?? null,
|
|
77
77
|
};
|
|
78
78
|
}
|
|
79
|
+
// ─── The sponsor budget (checked BEFORE the model) ────────────────────────────
|
|
80
|
+
/**
|
|
81
|
+
* The monthly cap on a sponsored child — a workspace whose credits are paid from
|
|
82
|
+
* an ancestor's pool — when it has no allocation and its parent set no
|
|
83
|
+
* `monthly_cap`. 1 credit = $0.001, so $50. Until this existed a NULL cap meant
|
|
84
|
+
* unlimited: a child past every budget was walked to the sponsor's pool, served,
|
|
85
|
+
* and (the refusal returning before the row) never recorded.
|
|
86
|
+
*/
|
|
87
|
+
export const SPONSOR_DEFAULT_MONTHLY_CREDITS = 50_000;
|
|
88
|
+
/**
|
|
89
|
+
* May a sponsored child spend `needed` more credits of its ancestor's pool this
|
|
90
|
+
* month? Pure. The pre-flight gates (web `lib/credits.ts`, channels
|
|
91
|
+
* `credit-gate.ts`) call it BEFORE the model whenever the funder is not the
|
|
92
|
+
* workspace itself, with the same D1 reads debitPool uses: the parent's
|
|
93
|
+
* `monthly_cap`, this month's `credit_burns` where workspace OR origin is the
|
|
94
|
+
* child, and the tightest transfer allocation.
|
|
95
|
+
*
|
|
96
|
+
* FAIL-CLOSED: a non-finite input (a NaN makes every `>` false, which reads as
|
|
97
|
+
* room) refuses with source `invalid`. A cap of 0 is a cap of 0 (`!= null`,
|
|
98
|
+
* never truthiness). Reaching a limit exactly is allowed; exceeding it is not —
|
|
99
|
+
* the same `>` debitPool uses. `sponsored: false` never refuses: a self-funded
|
|
100
|
+
* workspace spends its own money, and debitPool still judges it after the turn.
|
|
101
|
+
*/
|
|
102
|
+
export function budgetVerdict(p) {
|
|
103
|
+
if (!p.sponsored)
|
|
104
|
+
return { ok: true };
|
|
105
|
+
const alloc = p.alloc ?? null;
|
|
106
|
+
const neededOk = Number.isFinite(p.needed) && p.needed >= 0;
|
|
107
|
+
const mtdOk = Number.isFinite(p.monthToDate) && p.monthToDate >= 0;
|
|
108
|
+
const allocOk = alloc === null
|
|
109
|
+
|| (Number.isFinite(alloc.monthly_credits) && Number.isFinite(alloc.period_used) && alloc.period_used >= 0);
|
|
110
|
+
const capOk = p.parentCap == null || Number.isFinite(p.parentCap);
|
|
111
|
+
if (!neededOk || !mtdOk || !allocOk || !capOk) {
|
|
112
|
+
return {
|
|
113
|
+
ok: false, reason: 'budget_exhausted', source: 'invalid', limit: 0,
|
|
114
|
+
used: mtdOk ? p.monthToDate : 0, needed: neededOk ? p.needed : 0,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
// A turn costs at least one credit.
|
|
118
|
+
const n = Math.max(1, Math.ceil(p.needed));
|
|
119
|
+
if (alloc !== null && alloc.period_used + n > alloc.monthly_credits) {
|
|
120
|
+
return { ok: false, reason: 'budget_exhausted', source: 'allocation', limit: alloc.monthly_credits, used: alloc.period_used, needed: n };
|
|
121
|
+
}
|
|
122
|
+
if (p.parentCap != null) {
|
|
123
|
+
if (p.monthToDate + n > p.parentCap) {
|
|
124
|
+
return { ok: false, reason: 'budget_exhausted', source: 'parent_cap', limit: p.parentCap, used: p.monthToDate, needed: n };
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else if (alloc === null && p.monthToDate + n > SPONSOR_DEFAULT_MONTHLY_CREDITS) {
|
|
128
|
+
return { ok: false, reason: 'budget_exhausted', source: 'sponsor_default', limit: SPONSOR_DEFAULT_MONTHLY_CREDITS, used: p.monthToDate, needed: n };
|
|
129
|
+
}
|
|
130
|
+
return { ok: true };
|
|
131
|
+
}
|
|
79
132
|
/**
|
|
80
133
|
* Price one model call — the ONE price every door writes (text/billing.md
|
|
81
134
|
* § The ledger). Before it, the same turn priced three ways: channels at a flat
|
|
@@ -126,15 +179,16 @@ export function spend(p) {
|
|
|
126
179
|
* meter's old default was NOT (measured 2026-09-17, 444 ids), so it priced at the
|
|
127
180
|
* hard fallback in `rateFor` and never at a live rate.
|
|
128
181
|
*
|
|
129
|
-
* Moved 4.5 → 4.7 on 2026-09-26, the operator's
|
|
130
|
-
* task:01a0d887f8eba48aa2b5f342).
|
|
131
|
-
*
|
|
132
|
-
*
|
|
182
|
+
* Moved 4.5 → 4.7 on 2026-09-26 and BACK to 4.5 on 2026-09-27, the operator's
|
|
183
|
+
* call (speed row task:01a0d887f8eba48aa2b5f342). Measured on production: 4.7's
|
|
184
|
+
* short-answer ttft p50 was 2,415 ms (n=5) against 4.5's 1,348 ms, and three
|
|
185
|
+
* traced 4.7 turns ended at 16.0–22.5 s. 4.7 is cheaper per token ($1.60/$4.80
|
|
186
|
+
* vs $2/$6); speed decided it.
|
|
133
187
|
*
|
|
134
188
|
* It lives beside `rateFor` on purpose: the id the bill names and the id the turn
|
|
135
189
|
* runs on are one fact, and this subpath is the one both workers already import.
|
|
136
190
|
*/
|
|
137
|
-
export const DEFAULT_MODEL = 'x-ai/grok-4.
|
|
191
|
+
export const DEFAULT_MODEL = 'x-ai/grok-4.5';
|
|
138
192
|
const named = (v) => typeof v === 'string' && v.trim().length > 0;
|
|
139
193
|
/**
|
|
140
194
|
* Nearest wins: agent → group → ancestor → platform. Pure — no I/O, no throw.
|
package/dist/billing.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"billing.js","sourceRoot":"","sources":["../src/billing.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAKH;;;GAGG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,KAAK,CAAA;AAEpC,qEAAqE;AACrE,MAAM,CAAC,MAAM,cAAc,G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CAAC,CAAC,SAAS,IAAI,EAAE,EAAE,CAAC;QAClC,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC;YAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,EAAE,CAAA;IACtF,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,aAAa,EAAE,IAAI,EAAE,UAAU,EAAE,CAAA;AAC3F,CAAC"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/** Maximum output tokens a free-model request may use. */
|
|
2
|
+
export declare const FREE_MAX_OUTPUT_TOKENS = 4000;
|
|
3
|
+
/** Maximum requests a free-model workspace may make per UTC day. */
|
|
4
|
+
export declare const FREE_REQUESTS_PER_DAY = 50;
|
|
5
|
+
/** Fallback model ID when no tenant-chosen model is active and the default is the paid model. */
|
|
6
|
+
export declare const FREE_DEFAULT_MODEL = "deepseek/deepseek-v4-flash";
|
|
7
|
+
/** TTL for the free-tier daily counter in KV (26 hours, past the UTC day boundary). */
|
|
8
|
+
export declare const FREE_TIER_COUNTER_TTL_S: number;
|
|
9
|
+
/**
|
|
10
|
+
* Describes a free model's identity and limits.
|
|
11
|
+
*/
|
|
12
|
+
export interface FreeModel {
|
|
13
|
+
id: string;
|
|
14
|
+
maxTokensPerRequest: number;
|
|
15
|
+
maxRequestsPerDay: number;
|
|
16
|
+
}
|
|
17
|
+
/** Read-only list of models available under the free tier. */
|
|
18
|
+
export declare const FREE_MODELS: readonly FreeModel[];
|
|
19
|
+
/**
|
|
20
|
+
* Look up a free model by its id string.
|
|
21
|
+
* @returns the matching FreeModel, or `null` if not found.
|
|
22
|
+
*/
|
|
23
|
+
export declare function freeModel(id: unknown): FreeModel | null;
|
|
24
|
+
/**
|
|
25
|
+
* Result of a free-model usage check.
|
|
26
|
+
*/
|
|
27
|
+
export type FreeModelVerdict = {
|
|
28
|
+
ok: true;
|
|
29
|
+
model: FreeModel;
|
|
30
|
+
} | {
|
|
31
|
+
ok: false;
|
|
32
|
+
status: 402;
|
|
33
|
+
error: 'model_not_free' | 'free_request_cap' | 'free_daily_cap';
|
|
34
|
+
model: string;
|
|
35
|
+
topupUrl: string;
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Validate a chat request against free-model limits.
|
|
39
|
+
* @returns a verdict – either success with the model, or an error with a top-up URL.
|
|
40
|
+
*/
|
|
41
|
+
export declare function checkFreeModel(p: {
|
|
42
|
+
workspace: string;
|
|
43
|
+
model: unknown;
|
|
44
|
+
maxTokens: number;
|
|
45
|
+
usedToday: number;
|
|
46
|
+
}): FreeModelVerdict;
|
|
47
|
+
/**
|
|
48
|
+
* Build a key used to store the free-tier request counter in a key-value store.
|
|
49
|
+
* @param workspace The workspace identifier.
|
|
50
|
+
* @param now Optional epoch ms timestamp (defaults to current time).
|
|
51
|
+
* @returns A string of the form `free:<workspace>:<YYYY-MM-DD>` (UTC date).
|
|
52
|
+
*/
|
|
53
|
+
export declare function freeTierDayKey(workspace: string, now?: number): string;
|
|
54
|
+
/**
|
|
55
|
+
* Final verdict for a chat turn under the free-tier rules.
|
|
56
|
+
* Rules (in order):
|
|
57
|
+
* 1. If `chosen` is false and (model is undefined/empty or equals DEFAULT_MODEL),
|
|
58
|
+
* treat as unchosen default → use FREE_DEFAULT_MODEL with `downgraded: true`.
|
|
59
|
+
* Otherwise use the given model as-is with `downgraded: false`.
|
|
60
|
+
* 2. If the resolved model is not free → 402 'model_not_free'.
|
|
61
|
+
* 3. If usedToday is invalid or >= the model's daily cap → 402 'free_daily_cap'.
|
|
62
|
+
* 4. maxOutputTokens is clamped, never refused.
|
|
63
|
+
*/
|
|
64
|
+
export type FreeTierVerdict = {
|
|
65
|
+
ok: true;
|
|
66
|
+
model: string;
|
|
67
|
+
maxOutputTokens: number;
|
|
68
|
+
downgraded: boolean;
|
|
69
|
+
} | {
|
|
70
|
+
ok: false;
|
|
71
|
+
status: 402;
|
|
72
|
+
error: 'model_not_free' | 'free_daily_cap';
|
|
73
|
+
model: string;
|
|
74
|
+
topupUrl: string;
|
|
75
|
+
};
|
|
76
|
+
export declare function freeTierVerdict(p: {
|
|
77
|
+
workspace: string;
|
|
78
|
+
model: string | undefined;
|
|
79
|
+
chosen: boolean;
|
|
80
|
+
maxOutputTokens: number | undefined;
|
|
81
|
+
usedToday: number;
|
|
82
|
+
}): FreeTierVerdict;
|
|
83
|
+
//# sourceMappingURL=free-tier.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"free-tier.d.ts","sourceRoot":"","sources":["../src/free-tier.ts"],"names":[],"mappings":"AAOA,0DAA0D;AAC1D,eAAO,MAAM,sBAAsB,OAAO,CAAC;AAE3C,oEAAoE;AACpE,eAAO,MAAM,qBAAqB,KAAK,CAAC;AAExC,iGAAiG;AACjG,eAAO,MAAM,kBAAkB,+BAA+B,CAAC;AAE/D,uFAAuF;AACvF,eAAO,MAAM,uBAAuB,QAAe,CAAC;AAEpD;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,EAAE,EAAE,MAAM,CAAC;IACX,mBAAmB,EAAE,MAAM,CAAC;IAC5B,iBAAiB,EAAE,MAAM,CAAC;CAC3B;AAED,8DAA8D;AAC9D,eAAO,MAAM,WAAW,EAAE,SAAS,SAAS,EAG1C,CAAC;AAEH;;;GAGG;AACH,wBAAgB,SAAS,CAAC,EAAE,EAAE,OAAO,GAAG,SAAS,GAAG,IAAI,CAGvD;AAED;;GAEG;AACH,MAAM,MAAM,gBAAgB,GACxB;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,SAAS,CAAA;CAAE,GAC9B;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,GAAG,CAAC;IAAC,KAAK,EAAE,gBAAgB,GAAG,kBAAkB,GAAG,gBAAgB,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC;AAEjI;;;GAGG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,OAAO,CAAC;IAAC,SAAS,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GAAG,gBAAgB,CAgB/H;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,GAAG,GAAE,MAAmB,GAAG,MAAM,CAGlF;AAED;;;;;;;;;GASG;AACH,MAAM,MAAM,eAAe,GACvB;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,eAAe,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,OAAO,CAAA;CAAE,GACzE;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,GAAG,CAAC;IAAC,KAAK,EAAE,gBAAgB,GAAG,gBAAgB,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC;AAE5G,wBAAgB,eAAe,CAAC,CAAC,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;IAAC,MAAM,EAAE,OAAO,CAAC;IAAC,eAAe,EAAE,MAAM,GAAG,SAAS,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GAAG,eAAe,CAoD7K"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Free-tier logic for workspaces with no funder anywhere up their parent chain.
|
|
3
|
+
* A tenant-chosen paid model is refused (402 model_not_free), never silently
|
|
4
|
+
* downgraded — only the unchosen house default runs on the free model.
|
|
5
|
+
*/
|
|
6
|
+
import { DEFAULT_MODEL } from "./billing.js";
|
|
7
|
+
/** Maximum output tokens a free-model request may use. */
|
|
8
|
+
export const FREE_MAX_OUTPUT_TOKENS = 4000;
|
|
9
|
+
/** Maximum requests a free-model workspace may make per UTC day. */
|
|
10
|
+
export const FREE_REQUESTS_PER_DAY = 50;
|
|
11
|
+
/** Fallback model ID when no tenant-chosen model is active and the default is the paid model. */
|
|
12
|
+
export const FREE_DEFAULT_MODEL = 'deepseek/deepseek-v4-flash';
|
|
13
|
+
/** TTL for the free-tier daily counter in KV (26 hours, past the UTC day boundary). */
|
|
14
|
+
export const FREE_TIER_COUNTER_TTL_S = 26 * 60 * 60;
|
|
15
|
+
/** Read-only list of models available under the free tier. */
|
|
16
|
+
export const FREE_MODELS = Object.freeze([
|
|
17
|
+
Object.freeze({ id: 'deepseek/deepseek-v4-flash', maxTokensPerRequest: FREE_MAX_OUTPUT_TOKENS, maxRequestsPerDay: FREE_REQUESTS_PER_DAY }),
|
|
18
|
+
Object.freeze({ id: 'google/gemini-3.5-flash-lite', maxTokensPerRequest: FREE_MAX_OUTPUT_TOKENS, maxRequestsPerDay: FREE_REQUESTS_PER_DAY }),
|
|
19
|
+
]);
|
|
20
|
+
/**
|
|
21
|
+
* Look up a free model by its id string.
|
|
22
|
+
* @returns the matching FreeModel, or `null` if not found.
|
|
23
|
+
*/
|
|
24
|
+
export function freeModel(id) {
|
|
25
|
+
if (typeof id !== 'string')
|
|
26
|
+
return null;
|
|
27
|
+
return FREE_MODELS.find(m => m.id === id) ?? null;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Validate a chat request against free-model limits.
|
|
31
|
+
* @returns a verdict – either success with the model, or an error with a top-up URL.
|
|
32
|
+
*/
|
|
33
|
+
export function checkFreeModel(p) {
|
|
34
|
+
const modelId = String(p.model);
|
|
35
|
+
const found = freeModel(p.model);
|
|
36
|
+
if (!found) {
|
|
37
|
+
return { ok: false, status: 402, error: 'model_not_free', model: modelId, topupUrl: `/u/${p.workspace}/settings/billing` };
|
|
38
|
+
}
|
|
39
|
+
const { maxTokens, usedToday } = p;
|
|
40
|
+
const refuse = (error) => ({ ok: false, status: 402, error, model: modelId, topupUrl: `/u/${p.workspace}/settings/billing` });
|
|
41
|
+
if (typeof maxTokens !== 'number' || !Number.isFinite(maxTokens) || maxTokens < 0 || maxTokens > found.maxTokensPerRequest) {
|
|
42
|
+
return refuse('free_request_cap');
|
|
43
|
+
}
|
|
44
|
+
if (typeof usedToday !== 'number' || !Number.isFinite(usedToday) || usedToday < 0 || usedToday >= found.maxRequestsPerDay) {
|
|
45
|
+
return refuse('free_daily_cap');
|
|
46
|
+
}
|
|
47
|
+
return { ok: true, model: found };
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Build a key used to store the free-tier request counter in a key-value store.
|
|
51
|
+
* @param workspace The workspace identifier.
|
|
52
|
+
* @param now Optional epoch ms timestamp (defaults to current time).
|
|
53
|
+
* @returns A string of the form `free:<workspace>:<YYYY-MM-DD>` (UTC date).
|
|
54
|
+
*/
|
|
55
|
+
export function freeTierDayKey(workspace, now = Date.now()) {
|
|
56
|
+
const date = new Date(now).toISOString().slice(0, 10);
|
|
57
|
+
return `free:${workspace}:${date}`;
|
|
58
|
+
}
|
|
59
|
+
export function freeTierVerdict(p) {
|
|
60
|
+
// 1. Resolve model and downgraded flag
|
|
61
|
+
let resolved;
|
|
62
|
+
let downgraded;
|
|
63
|
+
if (!p.chosen && (!p.model || p.model === '' || p.model === DEFAULT_MODEL)) {
|
|
64
|
+
resolved = FREE_DEFAULT_MODEL;
|
|
65
|
+
downgraded = true;
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
resolved = p.model ?? "";
|
|
69
|
+
downgraded = false;
|
|
70
|
+
}
|
|
71
|
+
const found = freeModel(resolved);
|
|
72
|
+
if (!found) {
|
|
73
|
+
return {
|
|
74
|
+
ok: false,
|
|
75
|
+
status: 402,
|
|
76
|
+
error: 'model_not_free',
|
|
77
|
+
model: p.model ?? '',
|
|
78
|
+
topupUrl: `/u/${p.workspace}/settings/billing`
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
// 2. Daily cap check
|
|
82
|
+
if (typeof p.usedToday !== 'number' ||
|
|
83
|
+
!Number.isFinite(p.usedToday) ||
|
|
84
|
+
p.usedToday < 0 ||
|
|
85
|
+
p.usedToday >= found.maxRequestsPerDay) {
|
|
86
|
+
return {
|
|
87
|
+
ok: false,
|
|
88
|
+
status: 402,
|
|
89
|
+
error: 'free_daily_cap',
|
|
90
|
+
model: resolved,
|
|
91
|
+
topupUrl: `/u/${p.workspace}/settings/billing`
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
// 3. Clamp maxOutputTokens
|
|
95
|
+
let maxOutputTokens;
|
|
96
|
+
if (typeof p.maxOutputTokens === 'number' &&
|
|
97
|
+
Number.isFinite(p.maxOutputTokens) &&
|
|
98
|
+
p.maxOutputTokens > 0) {
|
|
99
|
+
maxOutputTokens = Math.min(Math.floor(p.maxOutputTokens), found.maxTokensPerRequest);
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
maxOutputTokens = found.maxTokensPerRequest;
|
|
103
|
+
}
|
|
104
|
+
return { ok: true, model: found.id, maxOutputTokens, downgraded };
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=free-tier.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"free-tier.js","sourceRoot":"","sources":["../src/free-tier.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C,0DAA0D;AAC1D,MAAM,CAAC,MAAM,sBAAsB,GAAG,IAAI,CAAC;AAE3C,oEAAoE;AACpE,MAAM,CAAC,MAAM,qBAAqB,GAAG,EAAE,CAAC;AAExC,iGAAiG;AACjG,MAAM,CAAC,MAAM,kBAAkB,GAAG,4BAA4B,CAAC;AAE/D,uFAAuF;AACvF,MAAM,CAA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E,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,CAAC,EAAE,EAAE,eAAe,EAAE,UAAU,EAAE,CAAC;AACpE,CAAC"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* gateway-meta.ts — five words on every model call.
|
|
3
|
+
*
|
|
4
|
+
* Lives in the SDK (moved from channels/src/gateway-meta.ts, 2026-09-28) so
|
|
5
|
+
* every door that calls a model — channels, the web app's paidModel/orChat
|
|
6
|
+
* (one.ie/web/src/lib/paid-model.ts), a CLI — writes the SAME five keys, and
|
|
7
|
+
* the gateway_calls mirror can join any of them. channels/src/gateway-meta.ts
|
|
8
|
+
* re-exports this file so its imports keep working.
|
|
9
|
+
*
|
|
10
|
+
* Cloudflare AI Gateway logs tokens, reasoning tokens, OpenRouter's reported
|
|
11
|
+
* cost and latency for every request channels makes, and keeps 100,000 of
|
|
12
|
+
* them. Until 2026-09-25 every entry's `metadata` was null, so the only way to
|
|
13
|
+
* say WHO paid for a call was a time window and a model name
|
|
14
|
+
* (text/one-router.md § Measured — the bake's reconcile). The gateway accepts
|
|
15
|
+
* a `cf-aig-metadata` header: a flat JSON object, AT MOST FIVE entries, values
|
|
16
|
+
* string | number | boolean, no nesting. Logs can then be filtered by any key.
|
|
17
|
+
*
|
|
18
|
+
* Five is the whole budget, so these are join keys, not a record. Everything
|
|
19
|
+
* else about a call — receiver, thread, promise, task, step, tags, who routed
|
|
20
|
+
* it — is reachable from `signal` in the substrate. The same words a signal
|
|
21
|
+
* envelope carries (one.ie/web/src/lib/signal-meta.ts: tags, actor, source):
|
|
22
|
+
*
|
|
23
|
+
* group the workspace slug that pays (Groups)
|
|
24
|
+
* actor the persona or human the call ran as (Actors)
|
|
25
|
+
* signal the signal id this call belongs to (Events — the join key)
|
|
26
|
+
* via the door: agent:run · chat · orchestrate · campaign · bake
|
|
27
|
+
* tags bare routing words, space-joined (the SignalMeta.tags —
|
|
28
|
+
* written at five sites and, before this header, read at none)
|
|
29
|
+
*
|
|
30
|
+
* Pure: no env, no fetch. `gatewayMetadataHeader` is the only export a call
|
|
31
|
+
* site needs; it returns {} when there is nothing to say, so spreading it into
|
|
32
|
+
* a provider's headers is always safe.
|
|
33
|
+
*
|
|
34
|
+
* READING IT BACK. The gateway's logs API returns each entry with `metadata`
|
|
35
|
+
* (these five, as sent), `tokens_in`, `tokens_out`,
|
|
36
|
+
* `usage_metadata.output_reasoning_tokens`, `cost` (OpenRouter's own number),
|
|
37
|
+
* `duration`, `success`, `cached`:
|
|
38
|
+
* GET /accounts/{acc}/ai-gateway/gateways/channels/logs
|
|
39
|
+
* ?start_date=…&end_date=…&per_page=50&page=N&order_by=created_at
|
|
40
|
+
* auth: X-Auth-Email + X-Auth-Key (CLOUDFLARE_EMAIL / CLOUDFLARE_GLOBAL_API_KEY
|
|
41
|
+
* in one.ie/web/.env). per_page is capped at 50 (7001 above it).
|
|
42
|
+
* `.claude/scripts/bake.sh --reconcile` is the worked reader: it matches a row
|
|
43
|
+
* by these tags first and by time window only for entries made before this
|
|
44
|
+
* header shipped. Measured 2026-09-25 before the header: 18 calls, every entry
|
|
45
|
+
* `metadata: null`, and the estimate the rows carried was 4x under the logged
|
|
46
|
+
* cost — reasoning tokens are in `usage_metadata`, never in the answer text.
|
|
47
|
+
* This header is BUILT on dev and proven only once channels is deployed with it;
|
|
48
|
+
* until then production entries stay null.
|
|
49
|
+
*/
|
|
50
|
+
export type GatewayMeta = {
|
|
51
|
+
group?: string;
|
|
52
|
+
actor?: string;
|
|
53
|
+
signal?: string;
|
|
54
|
+
via?: string;
|
|
55
|
+
/** bare words; an array is space-joined, a string is kept as is */
|
|
56
|
+
tags?: string[] | string;
|
|
57
|
+
};
|
|
58
|
+
/** The gateway's own ceiling. Not a knob. */
|
|
59
|
+
export declare const GATEWAY_META_MAX_ENTRIES = 5;
|
|
60
|
+
/** Per-value clip, so a runaway tag list cannot make the header itself the cost. */
|
|
61
|
+
export declare const GATEWAY_META_MAX_VALUE = 200;
|
|
62
|
+
/** The five keys, in the order they are kept when a caller sends more than fit. */
|
|
63
|
+
export declare const GATEWAY_META_KEYS: readonly ["group", "actor", "signal", "via", "tags"];
|
|
64
|
+
/** The flat object the header carries — exported so a test can read it without parsing. */
|
|
65
|
+
export declare function gatewayMetadata(meta?: GatewayMeta | null): Record<string, string>;
|
|
66
|
+
/** `{ 'cf-aig-metadata': '{…}' }`, or `{}` when there is nothing to say. */
|
|
67
|
+
export declare function gatewayMetadataHeader(meta?: GatewayMeta | null): Record<string, string>;
|
|
68
|
+
//# sourceMappingURL=gateway-meta.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gateway-meta.d.ts","sourceRoot":"","sources":["../src/gateway-meta.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AAEH,MAAM,MAAM,WAAW,GAAG;IACxB,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,mEAAmE;IACnE,IAAI,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,CAAA;CACzB,CAAA;AAED,6CAA6C;AAC7C,eAAO,MAAM,wBAAwB,IAAI,CAAA;AACzC,oFAAoF;AACpF,eAAO,MAAM,sBAAsB,MAAM,CAAA;AACzC,mFAAmF;AACnF,eAAO,MAAM,iBAAiB,sDAAuD,CAAA;AAIrF,2FAA2F;AAC3F,wBAAgB,eAAe,CAAC,IAAI,CAAC,EAAE,WAAW,GAAG,IAAI,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAYjF;AAED,4EAA4E;AAC5E,wBAAgB,qBAAqB,CAAC,IAAI,CAAC,EAAE,WAAW,GAAG,IAAI,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAGvF"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* gateway-meta.ts — five words on every model call.
|
|
3
|
+
*
|
|
4
|
+
* Lives in the SDK (moved from channels/src/gateway-meta.ts, 2026-09-28) so
|
|
5
|
+
* every door that calls a model — channels, the web app's paidModel/orChat
|
|
6
|
+
* (one.ie/web/src/lib/paid-model.ts), a CLI — writes the SAME five keys, and
|
|
7
|
+
* the gateway_calls mirror can join any of them. channels/src/gateway-meta.ts
|
|
8
|
+
* re-exports this file so its imports keep working.
|
|
9
|
+
*
|
|
10
|
+
* Cloudflare AI Gateway logs tokens, reasoning tokens, OpenRouter's reported
|
|
11
|
+
* cost and latency for every request channels makes, and keeps 100,000 of
|
|
12
|
+
* them. Until 2026-09-25 every entry's `metadata` was null, so the only way to
|
|
13
|
+
* say WHO paid for a call was a time window and a model name
|
|
14
|
+
* (text/one-router.md § Measured — the bake's reconcile). The gateway accepts
|
|
15
|
+
* a `cf-aig-metadata` header: a flat JSON object, AT MOST FIVE entries, values
|
|
16
|
+
* string | number | boolean, no nesting. Logs can then be filtered by any key.
|
|
17
|
+
*
|
|
18
|
+
* Five is the whole budget, so these are join keys, not a record. Everything
|
|
19
|
+
* else about a call — receiver, thread, promise, task, step, tags, who routed
|
|
20
|
+
* it — is reachable from `signal` in the substrate. The same words a signal
|
|
21
|
+
* envelope carries (one.ie/web/src/lib/signal-meta.ts: tags, actor, source):
|
|
22
|
+
*
|
|
23
|
+
* group the workspace slug that pays (Groups)
|
|
24
|
+
* actor the persona or human the call ran as (Actors)
|
|
25
|
+
* signal the signal id this call belongs to (Events — the join key)
|
|
26
|
+
* via the door: agent:run · chat · orchestrate · campaign · bake
|
|
27
|
+
* tags bare routing words, space-joined (the SignalMeta.tags —
|
|
28
|
+
* written at five sites and, before this header, read at none)
|
|
29
|
+
*
|
|
30
|
+
* Pure: no env, no fetch. `gatewayMetadataHeader` is the only export a call
|
|
31
|
+
* site needs; it returns {} when there is nothing to say, so spreading it into
|
|
32
|
+
* a provider's headers is always safe.
|
|
33
|
+
*
|
|
34
|
+
* READING IT BACK. The gateway's logs API returns each entry with `metadata`
|
|
35
|
+
* (these five, as sent), `tokens_in`, `tokens_out`,
|
|
36
|
+
* `usage_metadata.output_reasoning_tokens`, `cost` (OpenRouter's own number),
|
|
37
|
+
* `duration`, `success`, `cached`:
|
|
38
|
+
* GET /accounts/{acc}/ai-gateway/gateways/channels/logs
|
|
39
|
+
* ?start_date=…&end_date=…&per_page=50&page=N&order_by=created_at
|
|
40
|
+
* auth: X-Auth-Email + X-Auth-Key (CLOUDFLARE_EMAIL / CLOUDFLARE_GLOBAL_API_KEY
|
|
41
|
+
* in one.ie/web/.env). per_page is capped at 50 (7001 above it).
|
|
42
|
+
* `.claude/scripts/bake.sh --reconcile` is the worked reader: it matches a row
|
|
43
|
+
* by these tags first and by time window only for entries made before this
|
|
44
|
+
* header shipped. Measured 2026-09-25 before the header: 18 calls, every entry
|
|
45
|
+
* `metadata: null`, and the estimate the rows carried was 4x under the logged
|
|
46
|
+
* cost — reasoning tokens are in `usage_metadata`, never in the answer text.
|
|
47
|
+
* This header is BUILT on dev and proven only once channels is deployed with it;
|
|
48
|
+
* until then production entries stay null.
|
|
49
|
+
*/
|
|
50
|
+
/** The gateway's own ceiling. Not a knob. */
|
|
51
|
+
export const GATEWAY_META_MAX_ENTRIES = 5;
|
|
52
|
+
/** Per-value clip, so a runaway tag list cannot make the header itself the cost. */
|
|
53
|
+
export const GATEWAY_META_MAX_VALUE = 200;
|
|
54
|
+
/** The five keys, in the order they are kept when a caller sends more than fit. */
|
|
55
|
+
export const GATEWAY_META_KEYS = ['group', 'actor', 'signal', 'via', 'tags'];
|
|
56
|
+
const clip = (s) => (s.length > GATEWAY_META_MAX_VALUE ? s.slice(0, GATEWAY_META_MAX_VALUE) : s);
|
|
57
|
+
/** The flat object the header carries — exported so a test can read it without parsing. */
|
|
58
|
+
export function gatewayMetadata(meta) {
|
|
59
|
+
if (!meta)
|
|
60
|
+
return {};
|
|
61
|
+
const out = {};
|
|
62
|
+
for (const k of GATEWAY_META_KEYS) {
|
|
63
|
+
const v = meta[k];
|
|
64
|
+
if (v === undefined || v === null)
|
|
65
|
+
continue;
|
|
66
|
+
const s = Array.isArray(v) ? v.map((t) => String(t).trim()).filter(Boolean).join(' ') : String(v).trim();
|
|
67
|
+
if (!s)
|
|
68
|
+
continue;
|
|
69
|
+
out[k] = clip(s);
|
|
70
|
+
if (Object.keys(out).length >= GATEWAY_META_MAX_ENTRIES)
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
/** `{ 'cf-aig-metadata': '{…}' }`, or `{}` when there is nothing to say. */
|
|
76
|
+
export function gatewayMetadataHeader(meta) {
|
|
77
|
+
const m = gatewayMetadata(meta);
|
|
78
|
+
return Object.keys(m).length ? { 'cf-aig-metadata': JSON.stringify(m) } : {};
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=gateway-meta.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gateway-meta.js","sourceRoot":"","sources":["../src/gateway-meta.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgDG;AAWH,6CAA6C;AAC7C,MAAM,CAAC,MAAM,wBAAwB,GAAG,CAAC,CAAA;AACzC,oFAAoF;AACpF,MAAM,CAAC,MAAM,sBAAsB,GAAG,GAAG,CAAA;AACzC,mFAAmF;AACnF,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,CAAU,CAAA;AAErF,MAAM,IAAI,GAAG,CAAC,CAAS,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,MAAM,GAAG,sBAAsB,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,sBAAsB,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;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the hourly invoice mirror
|
|
3
|
+
* (gateway_calls) sees it with its five words. Doors the gateway cannot carry stay direct
|
|
4
|
+
* and are listed in text/credits.md "Doors the gateway cannot carry".
|
|
5
|
+
*/
|
|
6
|
+
export type GatewayEnv = {
|
|
7
|
+
CF_AI_GATEWAY_ACCOUNT_ID?: string;
|
|
8
|
+
CF_AI_GATEWAY_ID?: string;
|
|
9
|
+
};
|
|
10
|
+
export declare const GATEWAY_PROVIDERS: readonly [{
|
|
11
|
+
readonly provider: "openai";
|
|
12
|
+
readonly direct: "https://api.openai.com/v1";
|
|
13
|
+
}, {
|
|
14
|
+
readonly provider: "anthropic";
|
|
15
|
+
readonly direct: "https://api.anthropic.com";
|
|
16
|
+
}, {
|
|
17
|
+
readonly provider: "google-ai-studio";
|
|
18
|
+
readonly direct: "https://generativelanguage.googleapis.com";
|
|
19
|
+
}, {
|
|
20
|
+
readonly provider: "elevenlabs";
|
|
21
|
+
readonly direct: "https://api.elevenlabs.io";
|
|
22
|
+
}, {
|
|
23
|
+
readonly provider: "openrouter";
|
|
24
|
+
readonly direct: "https://openrouter.ai/api/v1";
|
|
25
|
+
}];
|
|
26
|
+
export declare const GATEWAY_CANNOT_CARRY: readonly ["https://api.bfl.ml", "https://api.dev.runwayml.com", "https://api.exa.ai", "https://api.firecrawl.dev", "https://openrouter.ai/api/v1/videos"];
|
|
27
|
+
export declare function gatewayBase(env: GatewayEnv | null | undefined): string | null;
|
|
28
|
+
export declare function gatewayUrl(env: GatewayEnv | null | undefined, directUrl: string): string;
|
|
29
|
+
export declare function isGatewayed(url: string): boolean;
|
|
30
|
+
export declare function workersAiGateway(env: GatewayEnv | null | undefined, meta?: Record<string, string>): {
|
|
31
|
+
gateway: {
|
|
32
|
+
id: string;
|
|
33
|
+
metadata?: Record<string, string>;
|
|
34
|
+
};
|
|
35
|
+
} | undefined;
|
|
36
|
+
//# sourceMappingURL=gateway-route.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"gateway-route.d.ts","sourceRoot":"","sources":["../src/gateway-route.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,MAAM,MAAM,UAAU,GAAG;IACvB,wBAAwB,CAAC,EAAE,MAAM,CAAA;IACjC,gBAAgB,CAAC,EAAE,MAAM,CAAA;CAC1B,CAAA;AAED,eAAO,MAAM,iBAAiB;;;;;;;;;;;;;;;EAMpB,CAAA;AAEV,eAAO,MAAM,oBAAoB,2JAMvB,CAAA;AAEV,wBAAgB,WAAW,CAAC,GAAG,EAAE,UAAU,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,GAAG,IAAI,CAM7E;AAED,wBAAgB,UAAU,CAAC,GAAG,EAAE,UAAU,GAAG,IAAI,GAAG,SAAS,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAmBxF;AAED,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAEhD;AAED,wBAAgB,gBAAgB,CAC9B,GAAG,EAAE,UAAU,GAAG,IAAI,GAAG,SAAS,EAClC,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAC5B;IAAE,OAAO,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;KAAE,CAAA;CAAE,GAAG,SAAS,CAW5E"}
|