@void/platform 1.7.0 → 1.9.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.
@@ -0,0 +1,133 @@
1
+ /**
2
+ * AI Gateway logging policy shared by the platform API, proxy and installer.
3
+ *
4
+ * The API saves a project override (and plan defaults) in Postgres and publishes the
5
+ * effective mode to ROUTING KV under `aiLoggingKey(projectId)`. The proxy reads only
6
+ * that key. A missing key means `inherit`: the gateway's own `collect_logs` setting.
7
+ */
8
+ /** Every mode an operator can choose. `inherit` clears an override. */
9
+ export declare const AI_LOGGING_MODES: readonly ["inherit", "off", "metadata", "full"];
10
+ export type AiLoggingMode = (typeof AI_LOGGING_MODES)[number];
11
+ /** Modes that are saved and published. `inherit` is never stored: no row, no key. */
12
+ export declare const PUBLISHED_AI_LOGGING_MODES: readonly ["off", "metadata", "full"];
13
+ export type PublishedAiLoggingMode = (typeof PUBLISHED_AI_LOGGING_MODES)[number];
14
+ /** Where an effective mode comes from. `gateway` means nothing is set (inherit). */
15
+ export type AiLoggingSource = "project" | "plan" | "gateway";
16
+ /**
17
+ * How a change between two effective modes moves logging. Only
18
+ * `off < metadata < full` has a known order: `inherit` follows the gateway setting,
19
+ * which the platform never reads, so any change to or from it is `unknown`.
20
+ */
21
+ export type AiLoggingPublishOrder = "same" | "tighten" | "loosen" | "unknown";
22
+ export declare function isAiLoggingMode(value: unknown): value is AiLoggingMode;
23
+ export declare function isPublishedAiLoggingMode(value: unknown): value is PublishedAiLoggingMode;
24
+ /** ROUTING KV key that holds a project's published AI logging mode. */
25
+ export declare function aiLoggingKey(projectId: string): string;
26
+ /**
27
+ * Reads a published KV value: a missing key is `inherit`, a stored mode is that mode.
28
+ * Anything else, including a literal `inherit` or a mode from a newer platform,
29
+ * returns `undefined` so callers fail closed instead of logging more than intended.
30
+ */
31
+ export declare function parsePublishedAiLogging(value: string | null): AiLoggingMode | undefined;
32
+ /** Effective mode: the project override, else the plan default, else inherit. */
33
+ export declare function resolveAiLogging(projectMode: AiLoggingMode | null | undefined, planMode: AiLoggingMode | null | undefined): Readonly<{
34
+ mode: AiLoggingMode;
35
+ source: AiLoggingSource;
36
+ }>;
37
+ /** Compares two effective modes (not saved project fields) to order a publish. */
38
+ export declare function publishOrder(previousEffective: AiLoggingMode, nextEffective: AiLoggingMode): AiLoggingPublishOrder;
39
+ /**
40
+ * How long after a KV write new requests are expected to follow it. KV may take up to
41
+ * 60 s (plus the proxy's 30 s `cacheTtl`) to reach other locations. A target, not a
42
+ * guarantee.
43
+ */
44
+ export declare const AI_LOGGING_SETTLE_MS = 12e4;
45
+ /** Edge cache time of the proxy's key read: KV's minimum, the same as the project token floor. */
46
+ export declare const AI_LOGGING_CACHE_TTL_SECONDS = 30;
47
+ /**
48
+ * ROUTING KV marker written just before `aiLoggingKey(projectId)` is removed for a
49
+ * project that now inherits. A removed key leaves no publish time behind, so readback
50
+ * reads this marker to report `pending` while other locations may still serve the old
51
+ * value. Only the API reads it (never the proxy), and it expires after the settle time.
52
+ * Its prefix does not start with the published key prefix, so key listings skip it.
53
+ */
54
+ export declare function aiLoggingClearedKey(projectId: string): string;
55
+ /** KV expiry of the cleared marker, in seconds (KV's minimum is 60). */
56
+ export declare const AI_LOGGING_CLEARED_TTL_SECONDS: number;
57
+ /** The KV value and options of a cleared marker written at `at` (ms). */
58
+ export declare function aiLoggingClearedMarker(at: number): Readonly<{
59
+ value: string;
60
+ metadata: Readonly<{
61
+ at: number;
62
+ }>;
63
+ expirationTtl: number;
64
+ }>;
65
+ /** Reads a cleared marker's time; anything missing or malformed is `null`. */
66
+ export declare function parseAiLoggingClearedAt(metadata: unknown): number | null;
67
+ /** ROUTING KV metadata written with every published key. `at` is the publish time in ms. */
68
+ export type AiLoggingKeyMetadata = Readonly<{
69
+ mode: PublishedAiLoggingMode;
70
+ at: number;
71
+ }>;
72
+ /** Reads the metadata of a published key; anything malformed is `null` (treated as drift). */
73
+ export declare function parseAiLoggingKeyMetadata(value: unknown): AiLoggingKeyMetadata | null;
74
+ /** `platform_settings` key that holds plan defaults, kept apart from `plans:catalog`. */
75
+ export declare const PLAN_AI_LOGGING_SETTING = "plans:ai-logging";
76
+ /**
77
+ * A key for a project with no row may belong to a project whose create transaction
78
+ * has written KV but not committed yet. Cleanup leaves keys this young alone.
79
+ */
80
+ export declare const AI_LOGGING_ORPHAN_GRACE_MS: number;
81
+ /**
82
+ * A plan-default fan-out that is still running (or stopped on an error).
83
+ * `revision` is the plan catalog revision that started it, so a page that began
84
+ * before a newer plan change cannot move the newer run's cursor.
85
+ */
86
+ export type PlanAiLoggingPublish = Readonly<{
87
+ revision: number;
88
+ planIds: ReadonlyArray<string>;
89
+ includeUnknown: boolean;
90
+ cursor: string | null;
91
+ complete: boolean;
92
+ error?: string;
93
+ }>;
94
+ export type PlanAiLoggingSettings = Readonly<{
95
+ /** Plan ID -> default mode. A missing plan inherits. */
96
+ modes: Readonly<Record<string, PublishedAiLoggingMode>>;
97
+ publish: PlanAiLoggingPublish | null;
98
+ }>;
99
+ /**
100
+ * Reads the saved plan defaults. A missing row means every plan inherits. A mode the
101
+ * platform does not know is refused (so a bad value never loosens logging);
102
+ * `inherit` entries are dropped. Entries for plans not in the catalog stay in the
103
+ * object but are never looked up (see `planAiLogging`).
104
+ */
105
+ export declare function parsePlanAiLoggingSettings(value: unknown): PlanAiLoggingSettings;
106
+ /** The parts of a plan catalog the logging resolver needs. */
107
+ export type AiLoggingPlanCatalog = Readonly<{
108
+ defaultPlanId: string;
109
+ plans: ReadonlyArray<Readonly<{
110
+ id: string;
111
+ }>>;
112
+ }>;
113
+ export type PlanAiLogging = Readonly<{
114
+ planId: string;
115
+ /** False when the account's plan ID is not in the catalog. */
116
+ planKnown: boolean;
117
+ /** The catalog default plan, whose logging an unknown plan ID follows. */
118
+ defaultPlanId: string;
119
+ planMode: PublishedAiLoggingMode | null;
120
+ }>;
121
+ /**
122
+ * The plan default for an account. A plan ID that is not in the catalog takes the
123
+ * default plan's logging (unlike limits, which fall back to the built-in free plan),
124
+ * so an operator who sets `off` on every plan gets `off` for every account.
125
+ */
126
+ export declare function planAiLogging(settings: PlanAiLoggingSettings, catalog: AiLoggingPlanCatalog, planId: string): PlanAiLogging;
127
+ /** Whether a running plan publish still has to reach accounts on this plan. */
128
+ export declare function planPublishPending(publish: PlanAiLoggingPublish | null, plan: Pick<PlanAiLogging, "planId" | "planKnown">): boolean;
129
+ /**
130
+ * Reads a plan's `aiLogging` field from a plan change. `undefined` keeps the current
131
+ * value and `inherit` clears it.
132
+ */
133
+ export declare function parsePlanAiLoggingInput(value: unknown): AiLoggingMode | undefined;
@@ -0,0 +1,190 @@
1
+ // src/ai-logging.ts
2
+ /**
3
+ * AI Gateway logging policy shared by the platform API, proxy and installer.
4
+ *
5
+ * The API saves a project override (and plan defaults) in Postgres and publishes the
6
+ * effective mode to ROUTING KV under `aiLoggingKey(projectId)`. The proxy reads only
7
+ * that key. A missing key means `inherit`: the gateway's own `collect_logs` setting.
8
+ */
9
+ /** Every mode an operator can choose. `inherit` clears an override. */
10
+ const AI_LOGGING_MODES = [
11
+ "inherit",
12
+ "off",
13
+ "metadata",
14
+ "full"
15
+ ];
16
+ /** Modes that are saved and published. `inherit` is never stored: no row, no key. */
17
+ const PUBLISHED_AI_LOGGING_MODES = [
18
+ "off",
19
+ "metadata",
20
+ "full"
21
+ ];
22
+ const AI_LOGGING_KEY_PREFIX = "ai-logging:";
23
+ function isAiLoggingMode(value) {
24
+ return AI_LOGGING_MODES.some((mode) => mode === value);
25
+ }
26
+ function isPublishedAiLoggingMode(value) {
27
+ return PUBLISHED_AI_LOGGING_MODES.some((mode) => mode === value);
28
+ }
29
+ /** ROUTING KV key that holds a project's published AI logging mode. */
30
+ function aiLoggingKey(projectId) {
31
+ return `${AI_LOGGING_KEY_PREFIX}${projectId}`;
32
+ }
33
+ /**
34
+ * Reads a published KV value: a missing key is `inherit`, a stored mode is that mode.
35
+ * Anything else, including a literal `inherit` or a mode from a newer platform,
36
+ * returns `undefined` so callers fail closed instead of logging more than intended.
37
+ */
38
+ function parsePublishedAiLogging(value) {
39
+ if (value === null) return "inherit";
40
+ return isPublishedAiLoggingMode(value) ? value : void 0;
41
+ }
42
+ /** Effective mode: the project override, else the plan default, else inherit. */
43
+ function resolveAiLogging(projectMode, planMode) {
44
+ if (projectMode && projectMode !== "inherit") return {
45
+ mode: projectMode,
46
+ source: "project"
47
+ };
48
+ if (planMode && planMode !== "inherit") return {
49
+ mode: planMode,
50
+ source: "plan"
51
+ };
52
+ return {
53
+ mode: "inherit",
54
+ source: "gateway"
55
+ };
56
+ }
57
+ const LOGGING_RANK = {
58
+ off: 0,
59
+ metadata: 1,
60
+ full: 2
61
+ };
62
+ /** Compares two effective modes (not saved project fields) to order a publish. */
63
+ function publishOrder(previousEffective, nextEffective) {
64
+ if (previousEffective === nextEffective) return "same";
65
+ if (previousEffective === "inherit" || nextEffective === "inherit") return "unknown";
66
+ return LOGGING_RANK[nextEffective] < LOGGING_RANK[previousEffective] ? "tighten" : "loosen";
67
+ }
68
+ /**
69
+ * How long after a KV write new requests are expected to follow it. KV may take up to
70
+ * 60 s (plus the proxy's 30 s `cacheTtl`) to reach other locations. A target, not a
71
+ * guarantee.
72
+ */
73
+ const AI_LOGGING_SETTLE_MS = 12e4;
74
+ /** Edge cache time of the proxy's key read: KV's minimum, the same as the project token floor. */
75
+ const AI_LOGGING_CACHE_TTL_SECONDS = 30;
76
+ const AI_LOGGING_CLEARED_KEY_PREFIX = "ai-logging-cleared:";
77
+ /**
78
+ * ROUTING KV marker written just before `aiLoggingKey(projectId)` is removed for a
79
+ * project that now inherits. A removed key leaves no publish time behind, so readback
80
+ * reads this marker to report `pending` while other locations may still serve the old
81
+ * value. Only the API reads it (never the proxy), and it expires after the settle time.
82
+ * Its prefix does not start with the published key prefix, so key listings skip it.
83
+ */
84
+ function aiLoggingClearedKey(projectId) {
85
+ return `${AI_LOGGING_CLEARED_KEY_PREFIX}${projectId}`;
86
+ }
87
+ /** KV expiry of the cleared marker, in seconds (KV's minimum is 60). */
88
+ const AI_LOGGING_CLEARED_TTL_SECONDS = AI_LOGGING_SETTLE_MS / 1e3;
89
+ /** The KV value and options of a cleared marker written at `at` (ms). */
90
+ function aiLoggingClearedMarker(at) {
91
+ return {
92
+ value: "inherit",
93
+ metadata: { at },
94
+ expirationTtl: 120
95
+ };
96
+ }
97
+ /** Reads a cleared marker's time; anything missing or malformed is `null`. */
98
+ function parseAiLoggingClearedAt(metadata) {
99
+ if (typeof metadata !== "object" || metadata === null || Array.isArray(metadata)) return null;
100
+ const { at } = metadata;
101
+ return typeof at === "number" && Number.isSafeInteger(at) ? at : null;
102
+ }
103
+ /** Reads the metadata of a published key; anything malformed is `null` (treated as drift). */
104
+ function parseAiLoggingKeyMetadata(value) {
105
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return null;
106
+ const { mode, at } = value;
107
+ if (!isPublishedAiLoggingMode(mode) || typeof at !== "number" || !Number.isSafeInteger(at)) return null;
108
+ return {
109
+ mode,
110
+ at
111
+ };
112
+ }
113
+ /** `platform_settings` key that holds plan defaults, kept apart from `plans:catalog`. */
114
+ const PLAN_AI_LOGGING_SETTING = "plans:ai-logging";
115
+ /**
116
+ * A key for a project with no row may belong to a project whose create transaction
117
+ * has written KV but not committed yet. Cleanup leaves keys this young alone.
118
+ */
119
+ const AI_LOGGING_ORPHAN_GRACE_MS = 6e5;
120
+ function settingsRecord(value, name) {
121
+ if (typeof value !== "object" || value === null || Array.isArray(value)) throw new Error(`${name} must be an object.`);
122
+ return value;
123
+ }
124
+ function parsePlanAiLoggingPublish(value) {
125
+ if (value === null || value === void 0) return null;
126
+ const { revision, planIds, includeUnknown, cursor, complete, error } = settingsRecord(value, "AI logging publish");
127
+ if (!Number.isSafeInteger(revision) || !Array.isArray(planIds) || !planIds.every((id) => typeof id === "string") || typeof includeUnknown !== "boolean" || cursor !== null && typeof cursor !== "string" || typeof complete !== "boolean" || error !== void 0 && typeof error !== "string") throw new Error("Invalid AI logging publish progress.");
128
+ return {
129
+ revision,
130
+ planIds,
131
+ includeUnknown,
132
+ cursor,
133
+ complete,
134
+ ...error === void 0 ? {} : { error }
135
+ };
136
+ }
137
+ /**
138
+ * Reads the saved plan defaults. A missing row means every plan inherits. A mode the
139
+ * platform does not know is refused (so a bad value never loosens logging);
140
+ * `inherit` entries are dropped. Entries for plans not in the catalog stay in the
141
+ * object but are never looked up (see `planAiLogging`).
142
+ */
143
+ function parsePlanAiLoggingSettings(value) {
144
+ if (value === null || value === void 0) return {
145
+ modes: {},
146
+ publish: null
147
+ };
148
+ const input = settingsRecord(value, "Plan AI logging settings");
149
+ const saved = settingsRecord(input.modes ?? {}, "Plan AI logging modes");
150
+ const modes = {};
151
+ for (const [planId, mode] of Object.entries(saved)) {
152
+ if (!isAiLoggingMode(mode)) throw new Error(`Invalid AI logging mode for plan ${planId}.`);
153
+ if (mode !== "inherit") modes[planId] = mode;
154
+ }
155
+ return {
156
+ modes,
157
+ publish: parsePlanAiLoggingPublish(input.publish)
158
+ };
159
+ }
160
+ /**
161
+ * The plan default for an account. A plan ID that is not in the catalog takes the
162
+ * default plan's logging (unlike limits, which fall back to the built-in free plan),
163
+ * so an operator who sets `off` on every plan gets `off` for every account.
164
+ */
165
+ function planAiLogging(settings, catalog, planId) {
166
+ const planKnown = catalog.plans.some((plan) => plan.id === planId);
167
+ const lookup = planKnown ? planId : catalog.defaultPlanId;
168
+ const planMode = Object.hasOwn(settings.modes, lookup) ? settings.modes[lookup] : null;
169
+ return {
170
+ planId,
171
+ planKnown,
172
+ defaultPlanId: catalog.defaultPlanId,
173
+ planMode
174
+ };
175
+ }
176
+ /** Whether a running plan publish still has to reach accounts on this plan. */
177
+ function planPublishPending(publish, plan) {
178
+ if (!publish || publish.complete) return false;
179
+ return plan.planKnown ? publish.planIds.includes(plan.planId) : publish.includeUnknown;
180
+ }
181
+ /**
182
+ * Reads a plan's `aiLogging` field from a plan change. `undefined` keeps the current
183
+ * value and `inherit` clears it.
184
+ */
185
+ function parsePlanAiLoggingInput(value) {
186
+ if (value === void 0) return;
187
+ if (!isAiLoggingMode(value)) throw new Error(`aiLogging must be one of ${AI_LOGGING_MODES.join(", ")}.`);
188
+ return value;
189
+ }
190
+ export { AI_LOGGING_CACHE_TTL_SECONDS, AI_LOGGING_CLEARED_TTL_SECONDS, AI_LOGGING_MODES, AI_LOGGING_ORPHAN_GRACE_MS, AI_LOGGING_SETTLE_MS, PLAN_AI_LOGGING_SETTING, PUBLISHED_AI_LOGGING_MODES, aiLoggingClearedKey, aiLoggingClearedMarker, aiLoggingKey, isAiLoggingMode, isPublishedAiLoggingMode, parseAiLoggingClearedAt, parseAiLoggingKeyMetadata, parsePlanAiLoggingInput, parsePlanAiLoggingSettings, parsePublishedAiLogging, planAiLogging, planPublishPending, publishOrder, resolveAiLogging };