@bitkyc08/opencodex 2.99.0 → 2.100.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.
- package/AGENTS_INSTALL.md +19 -1
- package/README.md +37 -33
- package/SPONSORS.md +5 -1
- package/bin/ocx.mjs +1 -1
- package/gui/dist/assets/{App-C1hGwaN1.css → App-COo8Yopd.css} +1 -1
- package/gui/dist/assets/App-CitvEefy.js +59 -0
- package/gui/dist/assets/Tray-CeKVWQyB.js +1 -0
- package/gui/dist/assets/{brand-mark-wzPe3R-J.js → brand-mark-M92UnxX2.js} +1 -1
- package/gui/dist/assets/index-BP47do6-.js +94 -0
- package/gui/dist/assets/index-DmDooSAG.css +2 -0
- package/gui/dist/assets/{segment-thumb-features-fYEMxBaW.js → segment-thumb-features-CcxZBnxv.js} +1 -1
- package/gui/dist/classic/assets/App-aYgIQMxI.js +58 -0
- package/gui/dist/classic/assets/{Tray-BqZueZOx.js → Tray-D8jfUXpQ.js} +1 -1
- package/gui/dist/classic/assets/index-CfGqHooN.js +94 -0
- package/gui/dist/classic/assets/quota-summary-R-ih7OlS.js +1 -0
- package/gui/dist/classic/index.html +1 -1
- package/gui/dist/index.html +2 -2
- package/native/ocx-child/Cargo.lock +215 -0
- package/native/ocx-child/Cargo.toml +31 -0
- package/native/ocx-child/src/cli.rs +1901 -0
- package/native/ocx-child/src/codex_home.rs +429 -0
- package/native/ocx-child/src/codex_settings.rs +231 -0
- package/native/ocx-child/src/daemon.rs +363 -0
- package/native/ocx-child/src/http_wire.rs +619 -0
- package/native/ocx-child/src/journal.rs +977 -0
- package/native/ocx-child/src/lib.rs +22 -0
- package/native/ocx-child/src/main.rs +9 -0
- package/native/ocx-child/src/parity.rs +102 -0
- package/native/ocx-child/src/platform.rs +1487 -0
- package/native/ocx-child/src/relay.rs +1288 -0
- package/native/ocx-child/src/relay_auth.rs +166 -0
- package/native/ocx-child/src/routes.rs +236 -0
- package/native/ocx-child/src/state.rs +190 -0
- package/native/ocx-child/src/test_hooks.rs +58 -0
- package/native/ocx-child/src/toml_surgeon.rs +1156 -0
- package/package.json +8 -2
- package/src/adapters/anthropic.ts +28 -2
- package/src/adapters/claude-cli/adapter.ts +5 -25
- package/src/adapters/claude-cli/instruction-images.ts +31 -0
- package/src/adapters/claude-cli/stable-replay.ts +42 -14
- package/src/adapters/claude-cli/usage-admission.ts +53 -64
- package/src/adapters/coding-agent/protocol.ts +1 -1
- package/src/adapters/devin/live-models.ts +3 -0
- package/src/adapters/google-http.ts +3 -0
- package/src/adapters/google-vertex-key-project.ts +10 -2
- package/src/adapters/google.ts +40 -8
- package/src/adapters/kiro/usage.ts +3 -13
- package/src/adapters/openai-responses/passthrough.ts +5 -2
- package/src/adapters/openai-responses/request-strips.ts +32 -0
- package/src/adapters/openai-responses/tool-schema.ts +9 -4
- package/src/chat/inbound.ts +1 -1
- package/src/claude/auth-detect.ts +26 -8
- package/src/claude/inbound-content-options.ts +18 -3
- package/src/claude/inbound.ts +5 -2
- package/src/cli/bundled-bun-diagnostic.ts +91 -0
- package/src/cli/capabilities-agents-routing.ts +2 -2
- package/src/cli/capabilities-observe-system.ts +60 -0
- package/src/cli/combo-input.ts +6 -0
- package/src/cli/combo-result.ts +1 -1
- package/src/cli/combo.ts +4 -0
- package/src/cli/config-command.ts +16 -2
- package/src/cli/connect.ts +12 -10
- package/src/cli/dispatch.ts +4 -0
- package/src/cli/doctor.ts +11 -0
- package/src/cli/help.ts +1 -0
- package/src/cli/init.ts +4 -0
- package/src/cli/link-enrollment.ts +152 -25
- package/src/cli/link-local-control.ts +49 -0
- package/src/cli/link.ts +37 -10
- package/src/cli/provider-lifecycle-runtime.ts +4 -2
- package/src/cli/registry.ts +13 -0
- package/src/cli/star-prompt.ts +2 -0
- package/src/cli/status.ts +1 -1
- package/src/cli/telemetry-profile.ts +206 -0
- package/src/cli/telemetry.ts +260 -0
- package/src/client/hub-relay.ts +26 -0
- package/src/client/machine-api.ts +3 -0
- package/src/clients/config-export/t3code.ts +44 -5
- package/src/clients/omo-role-models.ts +1 -1
- package/src/codex/catalog/bundled.ts +44 -2
- package/src/codex/runtime.ts +12 -0
- package/src/codex/shim-restore-lock.ts +67 -30
- package/src/combos/types.ts +21 -1
- package/src/config/atomic-write.ts +24 -1
- package/src/config/diagnostics.ts +15 -0
- package/src/config/live-reconcile.ts +17 -0
- package/src/config/load-degrade.ts +16 -0
- package/src/config/provider-validation.ts +29 -0
- package/src/config/schema/config-schema.ts +24 -0
- package/src/config/schema/leaf-validators.ts +17 -0
- package/src/config/telemetry-carry.ts +30 -0
- package/src/config.ts +13 -1
- package/src/generated/compatibility-version.json +405 -153
- package/src/generated/model-metadata.ts +3 -3
- package/src/integrations/aside-profiles.ts +7 -1
- package/src/integrations/owned-refresh.ts +9 -2
- package/src/lab/artifacts/secure-fs.ts +12 -11
- package/src/lab/fabric/scratch.ts +11 -9
- package/src/lab/public/community.ts +2 -3
- package/src/lab/public/file-safety.ts +18 -20
- package/src/lab/public/mutation-lock.ts +109 -337
- package/src/lab/public/private-file.ts +4 -3
- package/src/lib/bun-runtime-preflight.ts +1 -1
- package/src/lib/codex-catalog-read-only.ts +122 -0
- package/src/lib/config-ownership.ts +49 -18
- package/src/lib/errors.ts +11 -0
- package/src/lib/gcp-adc.ts +167 -48
- package/src/lib/jsonc.ts +4 -2
- package/src/lib/local-control-socket.ts +88 -0
- package/src/lib/privacy.ts +15 -0
- package/src/lib/provider-client-headers.ts +3 -0
- package/src/lib/token-estimate.ts +13 -2
- package/src/link/child-binary-source.ts +228 -0
- package/src/link/ephemeral-child.ts +45 -0
- package/src/link/remote-plant.ts +306 -0
- package/src/link/remote-windows.ts +59 -5
- package/src/link/ssh-argv.ts +71 -8
- package/src/link/status-projection.ts +2 -0
- package/src/link/store.ts +5 -1
- package/src/oauth/claude-keychain-read.ts +165 -0
- package/src/oauth/local-token-detect.ts +14 -17
- package/src/protocols/envelope.ts +20 -2
- package/src/providers/api-key-selection.ts +8 -2
- package/src/providers/claude-cli-usage-state.ts +93 -0
- package/src/providers/github-copilot-auto.ts +25 -5
- package/src/providers/meta-muse-plan-refusal.ts +94 -0
- package/src/providers/model-rename-fields.ts +3 -0
- package/src/providers/quota/vendor-probes-key.ts +23 -4
- package/src/providers/quota/vendor-probes-oauth.ts +59 -0
- package/src/providers/quota/workbuddy.ts +2 -2
- package/src/providers/quota.ts +25 -18
- package/src/providers/registry/entries-core.ts +7 -4
- package/src/providers/registry/entries-extended.ts +39 -7
- package/src/providers/registry/model-seeds.ts +10 -2
- package/src/providers/resolved-model-policy.ts +4 -0
- package/src/providers/routed-config.ts +30 -3
- package/src/providers/stale-no-vision-seed-migration.ts +7 -0
- package/src/providers/vertex-environment-authority.ts +50 -0
- package/src/providers/workbuddy-credit-balance.ts +38 -6
- package/src/quota-pool/assemble.ts +14 -6
- package/src/router.ts +1 -0
- package/src/server/adapter-resolve.ts +11 -6
- package/src/server/audio-dictation.ts +21 -7
- package/src/server/auth-cors.ts +19 -0
- package/src/server/background-lifecycle.ts +71 -0
- package/src/server/chat-completions.ts +2 -0
- package/src/server/claude-messages.ts +30 -3
- package/src/server/index/live-sideband.ts +15 -1
- package/src/server/index/local-control-listener.ts +515 -0
- package/src/server/index/optional-listeners.ts +21 -1
- package/src/server/index/serve-options.ts +4 -3
- package/src/server/index/websocket-handler.ts +2 -0
- package/src/server/index.ts +3 -0
- package/src/server/management/combo-routes.ts +6 -1
- package/src/server/management/context.ts +1 -0
- package/src/server/management/link-child-preflight.ts +74 -0
- package/src/server/management/link-enrollment.ts +190 -25
- package/src/server/management/link-route-state.ts +6 -1
- package/src/server/management/link-routes.ts +27 -9
- package/src/server/management/model-rows.ts +4 -0
- package/src/server/management/oauth-account-routes.ts +9 -4
- package/src/server/management/provider-overwrite-carry.ts +9 -2
- package/src/server/management/provider-routes.ts +14 -0
- package/src/server/management/provider-thinking-display-patch.ts +54 -0
- package/src/server/management/route-registry.ts +7 -0
- package/src/server/management/session-routes.ts +4 -1
- package/src/server/management/telemetry-routes.ts +37 -0
- package/src/server/management/usage-aggregate-cache.ts +290 -67
- package/src/server/management-api.ts +11 -2
- package/src/server/messages-native-preflight.ts +166 -0
- package/src/server/messages-native.ts +143 -43
- package/src/server/messages-shared.ts +14 -0
- package/src/server/proxy-liveness.ts +99 -6
- package/src/server/request-log-conversation.ts +18 -2
- package/src/server/request-log.ts +7 -2
- package/src/server/responses/adapter-continuation.ts +1 -0
- package/src/server/responses/adapter-dispatch.ts +11 -28
- package/src/server/responses/adapter-failure-message.ts +68 -0
- package/src/server/responses/combo-stream-preflight.ts +14 -2
- package/src/server/responses/core-auth.ts +26 -5
- package/src/server/responses/core-codex-account.ts +8 -1
- package/src/server/responses/core-combo-failure.ts +6 -1
- package/src/server/responses/core-combo-native.ts +150 -37
- package/src/server/responses/core-combo.ts +15 -9
- package/src/server/responses/core-normalize.ts +1 -0
- package/src/server/responses/core-options.ts +7 -1
- package/src/server/responses/core-pipeline.ts +21 -1
- package/src/server/responses/fetch-helpers.ts +5 -0
- package/src/server/responses/opengateway-completion-guard.ts +7 -3
- package/src/server/responses/passthrough-delivery.ts +8 -1
- package/src/server/responses/passthrough-error.ts +22 -0
- package/src/server/responses/request-prepare.ts +7 -1
- package/src/server/responses/request-transport.ts +28 -5
- package/src/server/responses/run-turn-execution.ts +13 -0
- package/src/server/sse-frame-buffer.ts +5 -0
- package/src/server/ws-bridge.ts +10 -0
- package/src/service/launchd.ts +78 -4
- package/src/telemetry/aggregate.ts +348 -0
- package/src/telemetry/consent.ts +128 -0
- package/src/telemetry/contract.ts +403 -0
- package/src/telemetry/disclosure.ts +19 -0
- package/src/telemetry/install-id.ts +226 -0
- package/src/telemetry/payload.ts +129 -0
- package/src/telemetry/profile/client.ts +150 -0
- package/src/telemetry/profile/contract.ts +288 -0
- package/src/telemetry/profile/disclosure.ts +14 -0
- package/src/telemetry/profile/paths.ts +13 -0
- package/src/telemetry/profile/routes.ts +99 -0
- package/src/telemetry/profile/service.ts +234 -0
- package/src/telemetry/profile/state.ts +189 -0
- package/src/telemetry/profile/sync.ts +276 -0
- package/src/telemetry/routes.ts +183 -0
- package/src/telemetry/scheduler.ts +386 -0
- package/src/telemetry/sender.ts +97 -0
- package/src/telemetry/state.ts +334 -0
- package/src/types/combo-jev-effort-mode.ts +11 -0
- package/src/types/config.ts +3 -0
- package/src/types/provider.ts +11 -1
- package/src/types/request.ts +2 -0
- package/src/types/telemetry.ts +15 -0
- package/src/types/wire.ts +10 -0
- package/src/types.ts +5 -0
- package/src/update/index.ts +7 -1
- package/src/update/minisign.d.mts +17 -0
- package/src/update/minisign.mjs +88 -0
- package/src/update/signed-supply.d.mts +59 -0
- package/src/update/signed-supply.mjs +289 -0
- package/src/update/trust-root.json +38 -0
- package/src/update/update-bounded-json.d.mts +4 -0
- package/src/update/update-bounded-json.mjs +113 -0
- package/src/update/update-bytes.d.mts +2 -0
- package/src/update/update-bytes.mjs +23 -0
- package/src/update/update-candidate-id.d.mts +2 -0
- package/src/update/update-candidate-id.mjs +23 -0
- package/src/update/update-checkpoint-codec.d.mts +33 -0
- package/src/update/update-checkpoint-codec.mjs +217 -0
- package/src/update/update-contracts.d.mts +24 -0
- package/src/update/update-contracts.mjs +207 -0
- package/src/update/update-signature-threshold.d.mts +12 -0
- package/src/update/update-signature-threshold.mjs +47 -0
- package/src/update/update-signed-version.d.mts +5 -0
- package/src/update/update-signed-version.mjs +25 -0
- package/src/update/update-trust-anchor.d.mts +2 -0
- package/src/update/update-trust-anchor.mjs +2 -0
- package/src/usage/cache-diagnostic.ts +195 -50
- package/src/usage/expected-prices.ts +20 -2
- package/src/usage/filter-index.ts +1469 -0
- package/src/usage/ledger-scanner.ts +245 -4
- package/src/usage/source-filter.ts +10 -2
- package/src/usage/summary.ts +16 -0
- package/src/web-search/parse.ts +244 -38
- package/gui/dist/assets/App-BepstgEN.js +0 -59
- package/gui/dist/assets/Tray-BinxbEZo.js +0 -1
- package/gui/dist/assets/index-C-9X2yaI.js +0 -94
- package/gui/dist/assets/index-bnUWypyz.css +0 -2
- package/gui/dist/classic/assets/App-DX88_GCW.js +0 -58
- package/gui/dist/classic/assets/index-fSP71pLB.js +0 -94
- package/gui/dist/classic/assets/quota-summary-y2fl0WBv.js +0 -1
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { packageVersion } from "../lib/package-version";
|
|
4
|
+
import { detectInstallOwnership } from "../update";
|
|
5
|
+
import type { DailyTelemetryAggregate } from "./aggregate";
|
|
6
|
+
import {
|
|
7
|
+
ARCH_VALUES,
|
|
8
|
+
OS_VALUES,
|
|
9
|
+
TELEMETRY_SCHEMA_VERSION,
|
|
10
|
+
TELEMETRY_VERSION_RE,
|
|
11
|
+
validateTelemetryPayloadV1,
|
|
12
|
+
type TelemetryArch,
|
|
13
|
+
type TelemetryInstallChannel,
|
|
14
|
+
type TelemetryOs,
|
|
15
|
+
type TelemetryPayloadV1,
|
|
16
|
+
type TelemetryReleaseChannel,
|
|
17
|
+
} from "./contract";
|
|
18
|
+
|
|
19
|
+
export interface ResolveInstallChannelOptions {
|
|
20
|
+
env?: Record<string, string | undefined>;
|
|
21
|
+
exists?: (path: string) => boolean;
|
|
22
|
+
detectOwnership?: () => { installer?: string };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function resolveInstallChannel(options: ResolveInstallChannelOptions = {}): TelemetryInstallChannel {
|
|
26
|
+
const env = options.env ?? process.env;
|
|
27
|
+
if (env.OCX_DESKTOP_SUPERVISED === "1") return "desktop";
|
|
28
|
+
const exists = options.exists ?? existsSync;
|
|
29
|
+
try {
|
|
30
|
+
if (exists("/.dockerenv") || exists("/run/.containerenv")) return "docker";
|
|
31
|
+
} catch { /* ignore fs errors */ }
|
|
32
|
+
try {
|
|
33
|
+
const installer = (options.detectOwnership ?? detectInstallOwnership)()?.installer;
|
|
34
|
+
switch (installer) {
|
|
35
|
+
case "npm":
|
|
36
|
+
return "npm-global";
|
|
37
|
+
case "pnpm":
|
|
38
|
+
return "pnpm-global";
|
|
39
|
+
case "bun":
|
|
40
|
+
return "bun-global";
|
|
41
|
+
case "mise":
|
|
42
|
+
return "mise";
|
|
43
|
+
case "standalone":
|
|
44
|
+
return "standalone";
|
|
45
|
+
case "source":
|
|
46
|
+
return "source";
|
|
47
|
+
default:
|
|
48
|
+
return "unknown";
|
|
49
|
+
}
|
|
50
|
+
} catch {
|
|
51
|
+
return "unknown";
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function normalizeTelemetryVersion(raw = packageVersion("0.0.0")): {
|
|
56
|
+
version: string;
|
|
57
|
+
releaseChannel: TelemetryReleaseChannel;
|
|
58
|
+
} {
|
|
59
|
+
const stripped = raw.trim().replace(/^v/, "").split("+")[0] ?? "0.0.0";
|
|
60
|
+
const version = TELEMETRY_VERSION_RE.test(stripped) ? stripped : "0.0.0";
|
|
61
|
+
const releaseChannel: TelemetryReleaseChannel = version.includes("-preview.") ? "preview" : "latest";
|
|
62
|
+
return { version, releaseChannel };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function resolveTelemetryOs(platform: string = process.platform): TelemetryOs {
|
|
66
|
+
return (OS_VALUES as readonly string[]).includes(platform) && platform !== "other"
|
|
67
|
+
? (platform as TelemetryOs)
|
|
68
|
+
: "other";
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function resolveTelemetryArch(arch: string = process.arch): TelemetryArch {
|
|
72
|
+
return (ARCH_VALUES as readonly string[]).includes(arch) && arch !== "other"
|
|
73
|
+
? (arch as TelemetryArch)
|
|
74
|
+
: "other";
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export interface BuildTelemetryPayloadOptions {
|
|
78
|
+
aggregate: DailyTelemetryAggregate;
|
|
79
|
+
installDayHash: string;
|
|
80
|
+
newInstall: boolean;
|
|
81
|
+
batchId?: string;
|
|
82
|
+
version?: string;
|
|
83
|
+
releaseChannel?: TelemetryReleaseChannel;
|
|
84
|
+
os?: TelemetryOs;
|
|
85
|
+
arch?: TelemetryArch;
|
|
86
|
+
installChannel?: TelemetryInstallChannel;
|
|
87
|
+
now?: number;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function buildTelemetryPayloadV1(options: BuildTelemetryPayloadOptions): TelemetryPayloadV1 {
|
|
91
|
+
const versionInfo = normalizeTelemetryVersion(options.version);
|
|
92
|
+
const candidate: TelemetryPayloadV1 = {
|
|
93
|
+
schemaVersion: TELEMETRY_SCHEMA_VERSION,
|
|
94
|
+
batchId: options.batchId ?? randomUUID(),
|
|
95
|
+
day: options.aggregate.day,
|
|
96
|
+
installDayHash: options.installDayHash,
|
|
97
|
+
newInstall: options.newInstall,
|
|
98
|
+
env: {
|
|
99
|
+
version: options.version ?? versionInfo.version,
|
|
100
|
+
releaseChannel: options.releaseChannel ?? versionInfo.releaseChannel,
|
|
101
|
+
os: options.os ?? resolveTelemetryOs(),
|
|
102
|
+
arch: options.arch ?? resolveTelemetryArch(),
|
|
103
|
+
installChannel: options.installChannel ?? resolveInstallChannel(),
|
|
104
|
+
},
|
|
105
|
+
activeMinutes: options.aggregate.activeMinutes,
|
|
106
|
+
requests: {
|
|
107
|
+
completed: options.aggregate.requests.completed,
|
|
108
|
+
failed: options.aggregate.requests.failed,
|
|
109
|
+
incomplete: options.aggregate.requests.incomplete,
|
|
110
|
+
aborted: options.aggregate.requests.aborted,
|
|
111
|
+
},
|
|
112
|
+
models: options.aggregate.models.map(row => ({
|
|
113
|
+
providerKind: row.providerKind,
|
|
114
|
+
modelFamily: row.modelFamily,
|
|
115
|
+
requests: row.requests,
|
|
116
|
+
inputTokens: row.inputTokens,
|
|
117
|
+
cacheReadTokens: row.cacheReadTokens,
|
|
118
|
+
cacheWriteTokens: row.cacheWriteTokens,
|
|
119
|
+
outputTokens: row.outputTokens,
|
|
120
|
+
reasoningTokens: row.reasoningTokens,
|
|
121
|
+
})),
|
|
122
|
+
errors: { ...options.aggregate.errors },
|
|
123
|
+
};
|
|
124
|
+
const validated = validateTelemetryPayloadV1(candidate, options.now);
|
|
125
|
+
if (!validated.ok) {
|
|
126
|
+
throw new Error(`Invalid telemetry payload: ${validated.error}`);
|
|
127
|
+
}
|
|
128
|
+
return validated.payload;
|
|
129
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { signalWithTimeout } from "../../lib/abort";
|
|
2
|
+
import { parseCalendarUtcDayMs } from "../contract";
|
|
3
|
+
import {
|
|
4
|
+
PROFILE_SECRET_RE,
|
|
5
|
+
parseProfileRankV1,
|
|
6
|
+
type ProfileCreateV1,
|
|
7
|
+
type ProfileDayV1,
|
|
8
|
+
type ProfileRankV1,
|
|
9
|
+
type PublicProfileV1,
|
|
10
|
+
} from "./contract";
|
|
11
|
+
|
|
12
|
+
/** Collector base for profile calls. A constant, like the anonymous endpoint; not configurable. */
|
|
13
|
+
export const TELEMETRY_PROFILE_API_BASE = "https://telemetry.opencodex.me";
|
|
14
|
+
export const TELEMETRY_PROFILE_TIMEOUT_MS = 10_000;
|
|
15
|
+
const MAX_RETRY_AFTER_MS = 7 * 86_400_000;
|
|
16
|
+
const DEFAULT_RETRY_AFTER_MS = 15 * 60 * 1000;
|
|
17
|
+
|
|
18
|
+
export interface ProfileClientOptions {
|
|
19
|
+
baseUrl?: string;
|
|
20
|
+
fetch?: typeof fetch;
|
|
21
|
+
timeoutMs?: number;
|
|
22
|
+
signal?: AbortSignal;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type ProfileCallResult<T> =
|
|
26
|
+
| { ok: true; status: number; body: T }
|
|
27
|
+
| { ok: false; status?: number; error: string; code?: string; retryAfterMs?: number };
|
|
28
|
+
|
|
29
|
+
let testOverrides: ProfileClientOptions | null = null;
|
|
30
|
+
|
|
31
|
+
export function setProfileClientOverridesForTests(overrides: ProfileClientOptions | null): void {
|
|
32
|
+
testOverrides = overrides;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function retryAfterMs(header: string | null | undefined): number {
|
|
36
|
+
const trimmed = header?.trim() ?? "";
|
|
37
|
+
if (/^\d+$/.test(trimmed)) {
|
|
38
|
+
const seconds = Number(trimmed);
|
|
39
|
+
if (seconds > 0) return Math.min(seconds * 1000, MAX_RETRY_AFTER_MS);
|
|
40
|
+
}
|
|
41
|
+
return DEFAULT_RETRY_AFTER_MS;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function call<T>(
|
|
45
|
+
method: "GET" | "POST" | "DELETE",
|
|
46
|
+
path: string,
|
|
47
|
+
options: ProfileClientOptions,
|
|
48
|
+
init: { secret?: string; body?: unknown } = {},
|
|
49
|
+
): Promise<ProfileCallResult<T>> {
|
|
50
|
+
const base = options.baseUrl ?? testOverrides?.baseUrl ?? TELEMETRY_PROFILE_API_BASE;
|
|
51
|
+
const fetchImpl = options.fetch ?? testOverrides?.fetch ?? globalThis.fetch;
|
|
52
|
+
const timeoutMs = options.timeoutMs ?? testOverrides?.timeoutMs ?? TELEMETRY_PROFILE_TIMEOUT_MS;
|
|
53
|
+
const deadline = signalWithTimeout(timeoutMs, options.signal ?? testOverrides?.signal);
|
|
54
|
+
const headers: Record<string, string> = { accept: "application/json" };
|
|
55
|
+
if (init.body !== undefined) headers["content-type"] = "application/json";
|
|
56
|
+
if (init.secret !== undefined) headers.authorization = `Bearer ${init.secret}`;
|
|
57
|
+
try {
|
|
58
|
+
const response = await fetchImpl(new URL(path, base).toString(), {
|
|
59
|
+
method,
|
|
60
|
+
headers,
|
|
61
|
+
...(init.body !== undefined ? { body: JSON.stringify(init.body) } : {}),
|
|
62
|
+
signal: deadline.signal,
|
|
63
|
+
});
|
|
64
|
+
let body: unknown = null;
|
|
65
|
+
try {
|
|
66
|
+
body = await response.json();
|
|
67
|
+
} catch {
|
|
68
|
+
body = null;
|
|
69
|
+
}
|
|
70
|
+
if (!response.ok) {
|
|
71
|
+
const error = body && typeof body === "object" && typeof (body as { error?: unknown }).error === "string"
|
|
72
|
+
? (body as { error: string }).error
|
|
73
|
+
: `collector responded ${response.status}`;
|
|
74
|
+
const code = body && typeof body === "object" && typeof (body as { code?: unknown }).code === "string"
|
|
75
|
+
? (body as { code: string }).code
|
|
76
|
+
: undefined;
|
|
77
|
+
return {
|
|
78
|
+
ok: false,
|
|
79
|
+
status: response.status,
|
|
80
|
+
error,
|
|
81
|
+
...(code ? { code } : {}),
|
|
82
|
+
...(response.status === 429 || response.status === 503
|
|
83
|
+
? { retryAfterMs: retryAfterMs(response.headers?.get?.("retry-after")) }
|
|
84
|
+
: {}),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
return { ok: true, status: response.status, body: body as T };
|
|
88
|
+
} catch (error) {
|
|
89
|
+
return { ok: false, error: error instanceof Error ? error.message : String(error) };
|
|
90
|
+
} finally {
|
|
91
|
+
deadline.cleanup();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface CreatedProfile {
|
|
96
|
+
profile: PublicProfileV1;
|
|
97
|
+
secret: string;
|
|
98
|
+
/** The collector's own creation day; only later days are accepted, so it is the client's floor too. */
|
|
99
|
+
createdDay: string | null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export async function createProfile(
|
|
103
|
+
body: ProfileCreateV1,
|
|
104
|
+
options: ProfileClientOptions = {},
|
|
105
|
+
): Promise<ProfileCallResult<CreatedProfile>> {
|
|
106
|
+
const result = await call<{ profile?: PublicProfileV1; secret?: unknown; createdDay?: unknown }>("POST", "/v1/profiles", options, { body });
|
|
107
|
+
if (!result.ok) return result;
|
|
108
|
+
const secret = result.body?.secret;
|
|
109
|
+
const profile = result.body?.profile;
|
|
110
|
+
if (typeof secret !== "string" || !PROFILE_SECRET_RE.test(secret) || !profile || typeof profile.handle !== "string") {
|
|
111
|
+
return { ok: false, status: result.status, error: "collector returned an invalid profile" };
|
|
112
|
+
}
|
|
113
|
+
const rawDay = result.body?.createdDay;
|
|
114
|
+
const createdDay = typeof rawDay === "string" && parseCalendarUtcDayMs(rawDay) !== null ? rawDay : null;
|
|
115
|
+
return { ok: true, status: result.status, body: { profile, secret, createdDay } };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export async function submitProfileDay(
|
|
119
|
+
secret: string,
|
|
120
|
+
day: ProfileDayV1,
|
|
121
|
+
options: ProfileClientOptions = {},
|
|
122
|
+
): Promise<ProfileCallResult<{ deduplicated?: boolean }>> {
|
|
123
|
+
return call("POST", "/v1/profiles/me/days", options, { secret, body: day });
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export async function fetchOwnProfileRank(
|
|
127
|
+
secret: string,
|
|
128
|
+
options: ProfileClientOptions = {},
|
|
129
|
+
): Promise<ProfileCallResult<ProfileRankV1>> {
|
|
130
|
+
const result = await call<unknown>("GET", "/v1/profiles/me", options, { secret });
|
|
131
|
+
if (!result.ok) return result;
|
|
132
|
+
const rank = parseProfileRankV1(result.body);
|
|
133
|
+
if (!rank) return { ok: false, status: result.status, error: "collector returned an invalid rank" };
|
|
134
|
+
return { ok: true, status: result.status, body: rank };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Delete the profile and every row submitted for it. A 401 also counts as deleted: the collector
|
|
139
|
+
* no longer knows the secret, so nothing remains to delete.
|
|
140
|
+
*/
|
|
141
|
+
export async function deleteProfile(
|
|
142
|
+
secret: string,
|
|
143
|
+
options: ProfileClientOptions = {},
|
|
144
|
+
): Promise<{ deleted: boolean; error?: string; retryAfterMs?: number }> {
|
|
145
|
+
const result = await call<{ deleted?: boolean }>("DELETE", "/v1/profiles/me", options, { secret });
|
|
146
|
+
if (result.ok) return { deleted: true };
|
|
147
|
+
if (result.status === 401) return { deleted: true };
|
|
148
|
+
return { deleted: false, error: result.error, ...(result.retryAfterMs ? { retryAfterMs: result.retryAfterMs } : {}) };
|
|
149
|
+
}
|
|
150
|
+
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wire contract for the opt-in public profile (leaderboard). Shared by the proxy and the collector
|
|
3
|
+
* (services/telemetry-collector imports it), like ../contract.ts, so any change here needs a
|
|
4
|
+
* collector-first rollout. Import-free apart from the anonymous contract's closed rosters.
|
|
5
|
+
*
|
|
6
|
+
* Nothing here relates to the anonymous install identity: a profile day carries no install id,
|
|
7
|
+
* install-day hash, batch id, version, platform, channel, provider kind or error field.
|
|
8
|
+
*/
|
|
9
|
+
import { MODEL_FAMILIES, parseCalendarUtcDayMs, type TelemetryModelFamily } from "../contract";
|
|
10
|
+
|
|
11
|
+
export const PROFILE_SCHEMA_VERSION = 1 as const;
|
|
12
|
+
export const PROFILE_HANDLE_MIN = 3;
|
|
13
|
+
export const PROFILE_HANDLE_MAX = 24;
|
|
14
|
+
export const PROFILE_HANDLE_RE = /^[a-z0-9][a-z0-9-]{1,22}[a-z0-9]$/;
|
|
15
|
+
export const PROFILE_DISPLAY_NAME_MAX = 32;
|
|
16
|
+
export const PROFILE_MAX_DAY_REQUESTS = 1_000_000;
|
|
17
|
+
export const PROFILE_MAX_DAY_TOKENS = 10_000_000_000;
|
|
18
|
+
export const PROFILE_SUBMIT_MAX_AGE_DAYS = 7;
|
|
19
|
+
/** `ocxp_` + base64url of 32 random bytes. */
|
|
20
|
+
export const PROFILE_SECRET_RE = /^ocxp_[A-Za-z0-9_-]{43}$/;
|
|
21
|
+
|
|
22
|
+
export const PROFILE_RESERVED_HANDLES: readonly string[] = Object.freeze([
|
|
23
|
+
"about", "admin", "administrator", "anthropic", "api", "app", "dashboard", "deepseek", "delete", "docs",
|
|
24
|
+
"everyone", "github", "google", "help", "leaderboard", "lidge", "login", "logout", "me", "mod",
|
|
25
|
+
"moderator", "new", "null", "ocx", "official", "openai", "opencodex", "owner", "privacy", "profile",
|
|
26
|
+
"profiles", "rank", "root", "security", "settings", "staff", "stats", "support", "system", "team",
|
|
27
|
+
"telemetry", "test", "undefined", "xai",
|
|
28
|
+
]);
|
|
29
|
+
|
|
30
|
+
export const PROFILE_AVATARS = Object.freeze([
|
|
31
|
+
"slate", "red", "orange", "amber", "lime", "green", "teal", "cyan", "blue", "indigo", "violet", "pink",
|
|
32
|
+
] as const);
|
|
33
|
+
export type ProfileAvatar = typeof PROFILE_AVATARS[number];
|
|
34
|
+
|
|
35
|
+
export const PROFILE_PERIODS = Object.freeze(["day", "week", "month"] as const);
|
|
36
|
+
export type ProfilePeriod = typeof PROFILE_PERIODS[number];
|
|
37
|
+
export const PROFILE_PERIOD_DAYS: Readonly<Record<ProfilePeriod, number>> = Object.freeze({ day: 1, week: 7, month: 30 });
|
|
38
|
+
export const PROFILE_METRICS = Object.freeze(["tokens", "requests"] as const);
|
|
39
|
+
export type ProfileMetric = typeof PROFILE_METRICS[number];
|
|
40
|
+
|
|
41
|
+
export interface ProfileTokensV1 {
|
|
42
|
+
input: number;
|
|
43
|
+
cacheRead: number;
|
|
44
|
+
cacheWrite: number;
|
|
45
|
+
output: number;
|
|
46
|
+
reasoning: number;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface ProfileFamilyRowV1 {
|
|
50
|
+
family: TelemetryModelFamily;
|
|
51
|
+
requests: number;
|
|
52
|
+
tokens: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** One completed UTC day of a connected profile: totals only. */
|
|
56
|
+
export interface ProfileDayV1 {
|
|
57
|
+
schemaVersion: typeof PROFILE_SCHEMA_VERSION;
|
|
58
|
+
day: string;
|
|
59
|
+
requests: number;
|
|
60
|
+
tokens: ProfileTokensV1;
|
|
61
|
+
families: ProfileFamilyRowV1[];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface ProfileCreateV1 {
|
|
65
|
+
schemaVersion: typeof PROFILE_SCHEMA_VERSION;
|
|
66
|
+
handle: string;
|
|
67
|
+
displayName: string;
|
|
68
|
+
avatar: ProfileAvatar;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Public fields of a profile. Never an id, secret, creation day or moderation flag. */
|
|
72
|
+
export interface PublicProfileV1 {
|
|
73
|
+
handle: string;
|
|
74
|
+
displayName: string;
|
|
75
|
+
avatar: ProfileAvatar;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface ProfilePeriodRankV1 {
|
|
79
|
+
/** RANK() among non-hidden profiles with a positive metric in the window; null when outside that set. */
|
|
80
|
+
rank: number | null;
|
|
81
|
+
/** Size of the ranked set (0 when nobody is ranked). */
|
|
82
|
+
of: number;
|
|
83
|
+
tokens: number;
|
|
84
|
+
requests: number;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export interface ProfileRankV1 {
|
|
88
|
+
profile: PublicProfileV1;
|
|
89
|
+
metric: ProfileMetric;
|
|
90
|
+
periods: Record<ProfilePeriod, ProfilePeriodRankV1>;
|
|
91
|
+
/** Model-family totals over the month window, largest token total first. */
|
|
92
|
+
families: ProfileFamilyRowV1[];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const DAY_MS = 86_400_000;
|
|
96
|
+
const TOKEN_KEYS = ["input", "cacheRead", "cacheWrite", "output", "reasoning"] as const;
|
|
97
|
+
const DAY_KEYS = ["schemaVersion", "day", "requests", "tokens", "families"] as const;
|
|
98
|
+
const FAMILY_KEYS = ["family", "requests", "tokens"] as const;
|
|
99
|
+
const CREATE_KEYS = ["schemaVersion", "handle", "displayName", "avatar"] as const;
|
|
100
|
+
const FAMILY_SET: ReadonlySet<string> = new Set(MODEL_FAMILIES);
|
|
101
|
+
const AVATAR_SET: ReadonlySet<string> = new Set(PROFILE_AVATARS);
|
|
102
|
+
const RESERVED_SET: ReadonlySet<string> = new Set(PROFILE_RESERVED_HANDLES);
|
|
103
|
+
/** Letters, combining marks, digits, space and a few punctuation marks; nothing that can form markup or a URL scheme. */
|
|
104
|
+
const DISPLAY_NAME_CHARS_RE = /^[\p{L}\p{M}\p{N} ._'-]+$/u;
|
|
105
|
+
/** Rejects anything shaped like a domain (`example.com`, `x.io`), so a display name cannot be a link. */
|
|
106
|
+
const DOMAIN_LIKE_RE = /\.[a-z]{2,}/i;
|
|
107
|
+
|
|
108
|
+
type Result<T> = { ok: true; value: T } | { ok: false; error: string };
|
|
109
|
+
|
|
110
|
+
function isPlainRecord(value: unknown): value is Record<string, unknown> {
|
|
111
|
+
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
112
|
+
&& Object.getPrototypeOf(value) === Object.prototype;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function hasExactKeys(record: Record<string, unknown>, expected: readonly string[]): boolean {
|
|
116
|
+
const keys = Object.keys(record);
|
|
117
|
+
return keys.length === expected.length && expected.every(key => Object.prototype.hasOwnProperty.call(record, key));
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function isBoundedInt(value: unknown, max: number): value is number {
|
|
121
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0 && value <= max;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function normalizeProfileHandle(raw: string): string {
|
|
125
|
+
return raw.trim().toLowerCase();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function validateProfileHandle(raw: unknown): Result<string> {
|
|
129
|
+
if (typeof raw !== "string") return { ok: false, error: "handle must be a string" };
|
|
130
|
+
const handle = normalizeProfileHandle(raw);
|
|
131
|
+
if (handle.length < PROFILE_HANDLE_MIN || handle.length > PROFILE_HANDLE_MAX || !PROFILE_HANDLE_RE.test(handle)) {
|
|
132
|
+
return { ok: false, error: "handle must be 3-24 lowercase letters, digits or hyphens, starting and ending with a letter or digit" };
|
|
133
|
+
}
|
|
134
|
+
if (handle.includes("--")) return { ok: false, error: "handle must not contain consecutive hyphens" };
|
|
135
|
+
if (/^[0-9-]+$/.test(handle)) return { ok: false, error: "handle must contain a letter" };
|
|
136
|
+
if (RESERVED_SET.has(handle)) return { ok: false, error: "handle is reserved" };
|
|
137
|
+
return { ok: true, value: handle };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export function validateProfileDisplayName(raw: unknown): Result<string> {
|
|
141
|
+
if (typeof raw !== "string") return { ok: false, error: "displayName must be a string" };
|
|
142
|
+
const name = raw.normalize("NFC").trim();
|
|
143
|
+
const length = [...name].length;
|
|
144
|
+
if (length < 1 || length > PROFILE_DISPLAY_NAME_MAX) {
|
|
145
|
+
return { ok: false, error: `displayName must be 1-${PROFILE_DISPLAY_NAME_MAX} characters` };
|
|
146
|
+
}
|
|
147
|
+
if (!DISPLAY_NAME_CHARS_RE.test(name)) {
|
|
148
|
+
return { ok: false, error: "displayName may contain only letters, digits, spaces and . _ ' -" };
|
|
149
|
+
}
|
|
150
|
+
if (name.includes(" ")) return { ok: false, error: "displayName must not contain consecutive spaces" };
|
|
151
|
+
if (DOMAIN_LIKE_RE.test(name)) return { ok: false, error: "displayName must not look like a web address" };
|
|
152
|
+
return { ok: true, value: name };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function isProfileAvatar(value: unknown): value is ProfileAvatar {
|
|
156
|
+
return typeof value === "string" && AVATAR_SET.has(value);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function validateProfileCreateV1(input: unknown): Result<ProfileCreateV1> {
|
|
160
|
+
if (!isPlainRecord(input) || !hasExactKeys(input, CREATE_KEYS)) {
|
|
161
|
+
return { ok: false, error: "profile must have exactly schemaVersion, handle, displayName and avatar" };
|
|
162
|
+
}
|
|
163
|
+
if (input.schemaVersion !== PROFILE_SCHEMA_VERSION) return { ok: false, error: "unsupported schemaVersion" };
|
|
164
|
+
const handle = validateProfileHandle(input.handle);
|
|
165
|
+
if (!handle.ok) return handle;
|
|
166
|
+
const displayName = validateProfileDisplayName(input.displayName);
|
|
167
|
+
if (!displayName.ok) return displayName;
|
|
168
|
+
if (!isProfileAvatar(input.avatar)) return { ok: false, error: "avatar must be one of the palette colours" };
|
|
169
|
+
return {
|
|
170
|
+
ok: true,
|
|
171
|
+
value: { schemaVersion: PROFILE_SCHEMA_VERSION, handle: handle.value, displayName: displayName.value, avatar: input.avatar },
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function sumProfileTokens(tokens: ProfileTokensV1): number {
|
|
176
|
+
return tokens.input + tokens.cacheRead + tokens.cacheWrite + tokens.output + tokens.reasoning;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function parsePeriodRank(value: unknown): ProfilePeriodRankV1 | null {
|
|
180
|
+
if (!isPlainRecord(value)) return null;
|
|
181
|
+
const rank = value.rank === null ? null : isBoundedInt(value.rank, Number.MAX_SAFE_INTEGER) && value.rank > 0 ? value.rank : undefined;
|
|
182
|
+
if (rank === undefined) return null;
|
|
183
|
+
if (!isBoundedInt(value.of, Number.MAX_SAFE_INTEGER) || !isBoundedInt(value.tokens, Number.MAX_SAFE_INTEGER)
|
|
184
|
+
|| !isBoundedInt(value.requests, Number.MAX_SAFE_INTEGER)) return null;
|
|
185
|
+
return { rank, of: value.of, tokens: value.tokens, requests: value.requests };
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Parse a rank response from the collector, keeping only the documented fields. Anything that
|
|
190
|
+
* does not match is rejected rather than rendered.
|
|
191
|
+
*/
|
|
192
|
+
export function parseProfileRankV1(value: unknown): ProfileRankV1 | null {
|
|
193
|
+
if (!isPlainRecord(value) || !isPlainRecord(value.profile) || !isPlainRecord(value.periods)) return null;
|
|
194
|
+
const handle = validateProfileHandle(value.profile.handle);
|
|
195
|
+
const displayName = validateProfileDisplayName(value.profile.displayName);
|
|
196
|
+
if (!handle.ok || !displayName.ok || !isProfileAvatar(value.profile.avatar)) return null;
|
|
197
|
+
if (value.metric !== "tokens" && value.metric !== "requests") return null;
|
|
198
|
+
const periods = {} as Record<ProfilePeriod, ProfilePeriodRankV1>;
|
|
199
|
+
for (const period of PROFILE_PERIODS) {
|
|
200
|
+
const parsed = parsePeriodRank(value.periods[period]);
|
|
201
|
+
if (!parsed) return null;
|
|
202
|
+
periods[period] = parsed;
|
|
203
|
+
}
|
|
204
|
+
if (!Array.isArray(value.families)) return null;
|
|
205
|
+
const families: ProfileFamilyRowV1[] = [];
|
|
206
|
+
for (const row of value.families) {
|
|
207
|
+
if (!isPlainRecord(row) || typeof row.family !== "string" || !FAMILY_SET.has(row.family)) return null;
|
|
208
|
+
if (!isBoundedInt(row.requests, Number.MAX_SAFE_INTEGER) || !isBoundedInt(row.tokens, Number.MAX_SAFE_INTEGER)) return null;
|
|
209
|
+
families.push({ family: row.family as TelemetryModelFamily, requests: row.requests, tokens: row.tokens });
|
|
210
|
+
}
|
|
211
|
+
return {
|
|
212
|
+
profile: { handle: handle.value, displayName: displayName.value, avatar: value.profile.avatar },
|
|
213
|
+
metric: value.metric,
|
|
214
|
+
periods,
|
|
215
|
+
families,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function utcMidnight(epochMs: number): number {
|
|
220
|
+
const d = new Date(epochMs);
|
|
221
|
+
return Date.UTC(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate());
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* True for a completed UTC day no older than {@link PROFILE_SUBMIT_MAX_AGE_DAYS}. Today and the
|
|
226
|
+
* future are never submittable: only whole, finished days are ranked.
|
|
227
|
+
*/
|
|
228
|
+
export function isSubmittableProfileDay(day: string, now: number): boolean {
|
|
229
|
+
const dayMs = parseCalendarUtcDayMs(day);
|
|
230
|
+
if (dayMs === null) return false;
|
|
231
|
+
const todayMs = utcMidnight(now);
|
|
232
|
+
return dayMs < todayMs && dayMs >= todayMs - PROFILE_SUBMIT_MAX_AGE_DAYS * DAY_MS;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export function validateProfileDayV1(input: unknown, now: number = Date.now()): Result<ProfileDayV1> {
|
|
236
|
+
if (!isPlainRecord(input) || !hasExactKeys(input, DAY_KEYS)) {
|
|
237
|
+
return { ok: false, error: "day must have exactly schemaVersion, day, requests, tokens and families" };
|
|
238
|
+
}
|
|
239
|
+
if (input.schemaVersion !== PROFILE_SCHEMA_VERSION) return { ok: false, error: "unsupported schemaVersion" };
|
|
240
|
+
if (typeof input.day !== "string" || !isSubmittableProfileDay(input.day, now)) {
|
|
241
|
+
return { ok: false, error: `day must be a completed UTC day within the last ${PROFILE_SUBMIT_MAX_AGE_DAYS} days` };
|
|
242
|
+
}
|
|
243
|
+
if (!isBoundedInt(input.requests, PROFILE_MAX_DAY_REQUESTS) || input.requests === 0) {
|
|
244
|
+
return { ok: false, error: `requests must be an integer in 1..${PROFILE_MAX_DAY_REQUESTS}` };
|
|
245
|
+
}
|
|
246
|
+
const rawTokens = input.tokens;
|
|
247
|
+
if (!isPlainRecord(rawTokens) || !hasExactKeys(rawTokens, TOKEN_KEYS)) {
|
|
248
|
+
return { ok: false, error: "tokens must have exactly input, cacheRead, cacheWrite, output and reasoning" };
|
|
249
|
+
}
|
|
250
|
+
for (const key of TOKEN_KEYS) {
|
|
251
|
+
if (!isBoundedInt(rawTokens[key], PROFILE_MAX_DAY_TOKENS)) return { ok: false, error: `tokens.${key} is out of range` };
|
|
252
|
+
}
|
|
253
|
+
const tokens: ProfileTokensV1 = {
|
|
254
|
+
input: rawTokens.input as number,
|
|
255
|
+
cacheRead: rawTokens.cacheRead as number,
|
|
256
|
+
cacheWrite: rawTokens.cacheWrite as number,
|
|
257
|
+
output: rawTokens.output as number,
|
|
258
|
+
reasoning: rawTokens.reasoning as number,
|
|
259
|
+
};
|
|
260
|
+
const tokenSum = sumProfileTokens(tokens);
|
|
261
|
+
if (tokenSum > PROFILE_MAX_DAY_TOKENS) return { ok: false, error: "token total exceeds the daily limit" };
|
|
262
|
+
if (!Array.isArray(input.families) || input.families.length === 0 || input.families.length > MODEL_FAMILIES.length) {
|
|
263
|
+
return { ok: false, error: "families must list 1 or more model families" };
|
|
264
|
+
}
|
|
265
|
+
const seen = new Set<string>();
|
|
266
|
+
const families: ProfileFamilyRowV1[] = [];
|
|
267
|
+
let familyRequests = 0;
|
|
268
|
+
let familyTokens = 0;
|
|
269
|
+
for (const row of input.families) {
|
|
270
|
+
if (!isPlainRecord(row) || !hasExactKeys(row, FAMILY_KEYS)) {
|
|
271
|
+
return { ok: false, error: "family rows must have exactly family, requests and tokens" };
|
|
272
|
+
}
|
|
273
|
+
if (typeof row.family !== "string" || !FAMILY_SET.has(row.family)) return { ok: false, error: "unknown model family" };
|
|
274
|
+
if (seen.has(row.family)) return { ok: false, error: "duplicate model family" };
|
|
275
|
+
seen.add(row.family);
|
|
276
|
+
if (!isBoundedInt(row.requests, PROFILE_MAX_DAY_REQUESTS) || row.requests === 0) {
|
|
277
|
+
return { ok: false, error: "family requests must be positive" };
|
|
278
|
+
}
|
|
279
|
+
if (!isBoundedInt(row.tokens, PROFILE_MAX_DAY_TOKENS)) return { ok: false, error: "family tokens are out of range" };
|
|
280
|
+
familyRequests += row.requests;
|
|
281
|
+
familyTokens += row.tokens;
|
|
282
|
+
families.push({ family: row.family as TelemetryModelFamily, requests: row.requests, tokens: row.tokens });
|
|
283
|
+
}
|
|
284
|
+
if (familyRequests !== input.requests) return { ok: false, error: "family requests must sum to requests" };
|
|
285
|
+
if (familyTokens !== tokenSum) return { ok: false, error: "family tokens must sum to the token total" };
|
|
286
|
+
return { ok: true, value: { schemaVersion: PROFILE_SCHEMA_VERSION, day: input.day, requests: input.requests, tokens, families } };
|
|
287
|
+
}
|
|
288
|
+
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The public-profile disclosure, printed by `ocx telemetry profile connect` before its [y/N] prompt
|
|
3
|
+
* and mirrored by the dashboard card. Separate from the anonymous telemetry disclosure: connecting
|
|
4
|
+
* a profile is its own consent.
|
|
5
|
+
*/
|
|
6
|
+
export const PROFILE_DISCLOSURE_HEADING = "Public usage profile (opt-in, off unless you connect it):";
|
|
7
|
+
|
|
8
|
+
export const PROFILE_DISCLOSURE_POINTS: readonly string[] = Object.freeze([
|
|
9
|
+
"Public: the handle, display name and avatar colour you choose, your rank, and your daily request and token totals and model-family totals.",
|
|
10
|
+
"Sent once per completed UTC day after today, only while anonymous telemetry is on: those totals and nothing else.",
|
|
11
|
+
"Never sent: prompts, responses, API keys, account ids or e-mails, provider names, URLs, file paths, or your anonymous telemetry id.",
|
|
12
|
+
"Leaderboard: opencodex.me/dashboard lists connected profiles only. Disconnect any time (dashboard Home or 'ocx telemetry profile disconnect'); that deletes the profile and all its data on the server.",
|
|
13
|
+
]);
|
|
14
|
+
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { getConfigDir } from "../../config/paths";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Location of the opt-in public profile file. Kept free of I/O so the server's background
|
|
6
|
+
* lifecycle can check for a profile without loading any profile code.
|
|
7
|
+
*/
|
|
8
|
+
export const TELEMETRY_PROFILE_FILE = "telemetry-profile.json";
|
|
9
|
+
|
|
10
|
+
export function telemetryProfilePath(configDir = getConfigDir()): string {
|
|
11
|
+
return join(configDir, TELEMETRY_PROFILE_FILE);
|
|
12
|
+
}
|
|
13
|
+
|