|
| 1 | +import { AI_LOGGING_SETTLE_MS } from '@void/platform/ai-logging'; |
| 2 | +import { cfAccessHeaders } from '../shared/cf-access.ts'; |
| 3 | +import { ensurePlatformAccessToken } from './cf-access.ts'; |
| 4 | +import { OperatorClientError } from './operator-client.ts'; |
| 5 | +import { sanitizeTerminalText } from './output.ts'; |
| 6 | +import { readPlatformDiscoveryResponse } from './platform-discovery.ts'; |
| 7 | + |
| 8 | +/** The first platform release with per-project AI logging settings. */ |
| 9 | +export const AI_LOGGING_PLATFORM_VERSION = '1.8.0'; |
| 10 | + |
| 11 | +type Fields = Record<string, unknown>; |
| 12 | + |
| 13 | +function text(value: unknown): string { |
| 14 | + return typeof value === 'string' ? sanitizeTerminalText(value) : ''; |
| 15 | +} |
| 16 | + |
| 17 | +function record(value: unknown): Fields | null { |
| 18 | + return value && typeof value === 'object' && !Array.isArray(value) ? (value as Fields) : null; |
| 19 | +} |
| 20 | + |
| 21 | +/** `14:03:20 UTC`, or null when the value is not a time. */ |
| 22 | +export function utcTime(value: unknown, offsetMs = 0): string | null { |
| 23 | + const time = typeof value === 'string' ? Date.parse(value) : Number.NaN; |
| 24 | + if (Number.isNaN(time)) { |
| 25 | + return null; |
| 26 | + } |
| 27 | + return `${new Date(time + offsetMs).toISOString().slice(11, 19)} UTC`; |
| 28 | +} |
| 29 | + |
| 30 | +/** `plan "pro"`, naming the default plan an unknown plan ID follows. */ |
| 31 | +function planLabel(fields: Fields): string { |
| 32 | + const plan = `plan "${text(fields.planId)}"`; |
| 33 | + if (fields.planKnown === false) { |
| 34 | + return `${plan} (not in catalog; uses default plan "${text(fields.defaultPlanId)}")`; |
| 35 | + } |
| 36 | + return plan; |
| 37 | +} |
| 38 | + |
| 39 | +/** Where the effective mode comes from, or null for a platform that does not say. */ |
| 40 | +function sourceText(fields: Fields): string | null { |
| 41 | + if (typeof fields.planId !== 'string') { |
| 42 | + return null; |
| 43 | + } |
| 44 | + const planDefault = `${planLabel(fields)} default: ${text(fields.planMode) || 'inherit'}`; |
| 45 | + switch (fields.source) { |
| 46 | + case 'project': |
| 47 | + return `set on project; ${planDefault}`; |
| 48 | + case 'plan': |
| 49 | + return `from ${planLabel(fields)}`; |
| 50 | + case 'gateway': |
| 51 | + return `AI Gateway setting; ${planDefault}`; |
| 52 | + default: |
| 53 | + return null; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +function effective(fields: Fields): string { |
| 58 | + return text(fields.effective) || text(fields.mode); |
| 59 | +} |
| 60 | + |
| 61 | +function withSource(mode: string, fields: Fields): string { |
| 62 | + const source = sourceText(fields); |
| 63 | + return source ? `${mode} (${source})` : mode; |
| 64 | +} |
| 65 | + |
| 66 | +/** The `project ai-logging` result: the setting and when new requests follow it. */ |
| 67 | +export function aiLoggingChangeLines(projectId: string, result: Fields): Array<string> { |
| 68 | + const lines = [ |
| 69 | + `AI logging for ${sanitizeTerminalText(projectId)}: ${withSource(effective(result), result)}`, |
| 70 | + ]; |
| 71 | + const expectedBy = utcTime(result.expectedBy); |
| 72 | + if (expectedBy) { |
| 73 | + lines.push( |
| 74 | + `New AI requests are expected to follow it by ${expectedBy} (normally within 2 minutes).`, |
| 75 | + ); |
| 76 | + } |
| 77 | + return lines; |
| 78 | +} |
| 79 | + |
| 80 | +/** The `project ai-logging --plan` preview: current and new effective modes. */ |
| 81 | +export function aiLoggingPreviewLines(projectId: string, preview: Fields): Array<string> { |
| 82 | + const previous = text(preview.previous); |
| 83 | + const next = effective(preview); |
| 84 | + const change = previous ? `${previous} → ${next}` : next; |
| 85 | + return [ |
| 86 | + `AI logging for ${sanitizeTerminalText(projectId)}: ${withSource(change, preview)}`, |
| 87 | + 'No change was made. Run the command again without --plan to apply it.', |
| 88 | + ]; |
| 89 | +} |
| 90 | + |
| 91 | +const STATES: Record<string, (fields: Fields) => string> = { |
| 92 | + applied: () => 'applied', |
| 93 | + pending: (fields) => { |
| 94 | + const by = utcTime(fields.publishedAt, AI_LOGGING_SETTLE_MS); |
| 95 | + return by ? `pending (expected by ${by})` : 'pending (normally within 2 minutes)'; |
| 96 | + }, |
| 97 | + pending_plan_publish: () => 'pending (the plan default is still being published)', |
| 98 | + drift: (fields) => `drift (published: ${text(fields.published) || 'unknown value'})`, |
| 99 | + unknown: () => 'state unknown (the published setting could not be read)', |
| 100 | +}; |
| 101 | + |
| 102 | +/** |
| 103 | + * `project show` line, such as `metadata (from plan "pro") — applied`, or null |
| 104 | + * when the response has no AI logging data. |
| 105 | + */ |
| 106 | +export function aiLoggingStatusLine(value: unknown): string | null { |
| 107 | + const fields = record(value); |
| 108 | + if (!fields || typeof fields.mode !== 'string') { |
| 109 | + return null; |
| 110 | + } |
| 111 | + const state = |
| 112 | + typeof fields.state === 'string' && Object.hasOwn(STATES, fields.state) |
| 113 | + ? STATES[fields.state] |
| 114 | + : undefined; |
| 115 | + const summary = withSource(effective(fields), fields); |
| 116 | + return `AI Gateway logging: ${state ? `${summary} — ${state(fields)}` : summary}`; |
| 117 | +} |
| 118 | + |
| 119 | +type DiscoveryProbe = { kind: 'version'; version: string } | { kind: 'missing' | 'unknown' }; |
| 120 | + |
| 121 | +async function probePlatformVersion(origin: string): Promise<DiscoveryProbe> { |
| 122 | + ensurePlatformAccessToken(origin); |
| 123 | + let response: Response; |
| 124 | + try { |
| 125 | + response = await fetch(`${origin}/.well-known/void-platform`, { |
| 126 | + headers: { Accept: 'application/json', ...cfAccessHeaders(process.env, origin) }, |
| 127 | + redirect: 'manual', |
| 128 | + signal: AbortSignal.timeout(15_000), |
| 129 | + }); |
| 130 | + } catch { |
| 131 | + return { kind: 'unknown' }; |
| 132 | + } |
| 133 | + if (response.status === 404) { |
| 134 | + await response.body?.cancel(); |
| 135 | + return { kind: 'missing' }; |
| 136 | + } |
| 137 | + try { |
| 138 | + return { |
| 139 | + kind: 'version', |
| 140 | + version: (await readPlatformDiscoveryResponse(origin, response)).platformVersion, |
| 141 | + }; |
| 142 | + } catch { |
| 143 | + return { kind: 'unknown' }; |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +/** |
| 148 | + * Refuses `project ai-logging` on a platform before AI logging settings existed. |
| 149 | + * It reads the platform version instead of guessing from a 404, because "Project |
| 150 | + * not found" is also a 404. When the version cannot be read, the request runs and |
| 151 | + * reports its own error. |
| 152 | + */ |
| 153 | +export async function assertAiLoggingPlatform(origin: string): Promise<void> { |
| 154 | + const probe = await probePlatformVersion(origin); |
| 155 | + if (probe.kind === 'unknown') { |
| 156 | + return; |
| 157 | + } |
| 158 | + if (probe.kind === 'version') { |
| 159 | + const { isOlderPlatformVersion } = await import('./client.ts'); |
| 160 | + // "hosted" and development builds are not release versions and pass. |
| 161 | + if (isOlderPlatformVersion(probe.version, AI_LOGGING_PLATFORM_VERSION) !== true) { |
| 162 | + return; |
| 163 | + } |
| 164 | + } |
| 165 | + throw new OperatorClientError( |
| 166 | + 'This platform does not support AI logging policies. Run `void platform upgrade`.', |
| 167 | + 'unsupported', |
| 168 | + ); |
| 169 | +} |
0 commit comments