-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsite-funded-codex-policy.ts
More file actions
116 lines (109 loc) · 3.6 KB
/
Copy pathsite-funded-codex-policy.ts
File metadata and controls
116 lines (109 loc) · 3.6 KB
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
/*
* This file is part of CoCalc: Copyright © 2026, SageMath, Inc.
* License: MS-RSL – see https://github.com/sagemathinc/cocalc-ai/blob/master/LICENSE.md
*/
import { getServerSettings } from "@cocalc/database/settings/server-settings";
import {
DEFAULT_SITE_FUNDED_CODEX_POLICY,
getSiteFundedCodexPrice,
type SiteFundedCodexPolicy,
usdToMicrousd,
} from "@cocalc/util/ai/site-funded-codex";
import { to_bool } from "@cocalc/util/db-schema/site-defaults";
type Settings = Record<string, unknown>;
function positiveInteger(
value: unknown,
fallback: number,
maximum = Number.MAX_SAFE_INTEGER,
): number {
const parsed = Number(value);
if (!Number.isSafeInteger(parsed) || parsed <= 0) return fallback;
return Math.min(parsed, maximum);
}
function positiveUsdMicrousd(value: unknown, fallback: number): number {
try {
const parsed = usdToMicrousd(`${value ?? ""}`);
return parsed > 0 ? parsed : fallback;
} catch {
return fallback;
}
}
export type SiteFundedCodexConfiguration = {
enabled: boolean;
policy: SiteFundedCodexPolicy;
globalPoolWeeklyLimitMicrousd: number;
freePoolWeeklyLimitMicrousd: number;
paidPoolWeeklyLimitMicrousd: number;
globalConcurrency: number;
};
export function siteFundedCodexConfigurationFromSettings(
settings: Settings,
): SiteFundedCodexConfiguration {
const model = `${settings.site_funded_codex_model ?? "gpt-5.6-luna"}`.trim();
// This is deliberately an eager fail-closed validation.
getSiteFundedCodexPrice(model);
const policy: SiteFundedCodexPolicy = {
...DEFAULT_SITE_FUNDED_CODEX_POLICY,
model,
reasoning:
settings.site_funded_codex_reasoning === "low"
? "low"
: DEFAULT_SITE_FUNDED_CODEX_POLICY.reasoning,
serviceTier: "standard",
maxTurnCostMicrousd: positiveUsdMicrousd(
settings.site_funded_codex_max_turn_usd,
DEFAULT_SITE_FUNDED_CODEX_POLICY.maxTurnCostMicrousd,
),
maxTurnDurationMs:
positiveInteger(
settings.site_funded_codex_max_turn_seconds,
DEFAULT_SITE_FUNDED_CODEX_POLICY.maxTurnDurationMs / 1_000,
24 * 60 * 60,
) * 1_000,
contextWindowTokens: positiveInteger(
settings.site_funded_codex_max_input_tokens_per_request,
DEFAULT_SITE_FUNDED_CODEX_POLICY.contextWindowTokens,
272_000,
),
maxOutputTokensPerRequest: positiveInteger(
settings.site_funded_codex_max_output_tokens_per_request,
DEFAULT_SITE_FUNDED_CODEX_POLICY.maxOutputTokensPerRequest,
128_000,
),
maxRequestsPerTurn: positiveInteger(
settings.site_funded_codex_max_requests_per_turn,
DEFAULT_SITE_FUNDED_CODEX_POLICY.maxRequestsPerTurn,
10_000,
),
};
policy.autoCompactTokenLimit = Math.min(
policy.contextWindowTokens - 1,
Math.floor(policy.contextWindowTokens * 0.75),
);
return {
enabled: to_bool(settings.site_funded_codex_enabled),
policy,
globalPoolWeeklyLimitMicrousd: positiveUsdMicrousd(
settings.site_funded_codex_global_pool_weekly_usd,
100 * 1_000_000,
),
freePoolWeeklyLimitMicrousd: positiveUsdMicrousd(
settings.site_funded_codex_free_pool_weekly_usd,
100 * 1_000_000,
),
paidPoolWeeklyLimitMicrousd: positiveUsdMicrousd(
settings.site_funded_codex_paid_pool_weekly_usd,
100 * 1_000_000,
),
globalConcurrency: positiveInteger(
settings.site_funded_codex_global_concurrency,
50,
100_000,
),
};
}
export async function getSiteFundedCodexConfiguration(): Promise<SiteFundedCodexConfiguration> {
return siteFundedCodexConfigurationFromSettings(
(await getServerSettings()) as Settings,
);
}