Repository navigation
Expand file tree
/
Copy pathfacts.ts
More file actions
233 lines (212 loc) · 7.4 KB
/
Copy pathfacts.ts
File metadata and controls
233 lines (212 loc) · 7.4 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import {
FACTS as BUILD_TIME_FACTS,
type PublishedReleaseFact,
type RepoFacts,
type ProviderFact,
type ModelFact,
} from "./facts.generated";
const KV_KEY = "facts:current";
export type { PublishedReleaseFact, RepoFacts, ProviderFact, ModelFact };
export const BUILD_FACTS: RepoFacts = {
...BUILD_TIME_FACTS,
sourceRevision:
process.env.NEXT_PUBLIC_CODEWHALE_SOURCE_REVISION || BUILD_TIME_FACTS.sourceRevision,
sourceCommittedAt:
process.env.NEXT_PUBLIC_CODEWHALE_SOURCE_COMMITTED_AT ||
BUILD_TIME_FACTS.sourceCommittedAt,
};
interface KVNamespace {
get(key: string): Promise<string | null>;
put(key: string, value: string, opts?: { expirationTtl?: number }): Promise<void>;
}
export type FactsSource = "build" | "kv";
export interface FactsResolution {
facts: RepoFacts;
source: FactsSource;
reason:
| "no-kv-snapshot"
| "invalid-kv-snapshot"
| "kv-missing-source-provenance"
| "build-missing-source-provenance"
| "same-source-revision"
| "kv-source-newer"
| "build-source-newer-or-ambiguous";
buildSourceRevision: string | null;
kvSourceRevision: string | null;
}
function isRecord(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === "object" && !Array.isArray(value);
}
function isModelFact(value: unknown): value is ModelFact {
if (!isRecord(value)) return false;
return (
typeof value.id === "string" &&
(value.provider === null || typeof value.provider === "string") &&
(value.contextWindow === null || typeof value.contextWindow === "number") &&
(value.maxOutput === null || typeof value.maxOutput === "number") &&
typeof value.reasoning === "boolean" &&
(value.addedAt === null || typeof value.addedAt === "string")
);
}
function isPublishedRelease(value: unknown): value is PublishedReleaseFact {
if (!isRecord(value)) return false;
return (
typeof value.tag === "string" &&
typeof value.version === "string" &&
value.tag === `v${value.version}` &&
typeof value.publishedAt === "string" &&
Number.isFinite(Date.parse(value.publishedAt)) &&
value.url === `https://github.com/Hmbown/CodeWhale/releases/tag/${value.tag}`
);
}
export function isRepoFacts(value: unknown): value is RepoFacts {
if (!isRecord(value)) return false;
const sourceRevisionValid =
value.sourceRevision === null ||
(typeof value.sourceRevision === "string" && /^[0-9a-f]{40}$/i.test(value.sourceRevision));
const sourceCommittedAtValid =
value.sourceCommittedAt === null ||
(typeof value.sourceCommittedAt === "string" &&
Number.isFinite(Date.parse(value.sourceCommittedAt)));
const sourceProvenancePaired =
(value.sourceRevision === null) === (value.sourceCommittedAt === null);
return (
typeof value.generatedAt === "string" &&
sourceRevisionValid &&
sourceCommittedAtValid &&
sourceProvenancePaired &&
typeof value.version === "string" &&
Array.isArray(value.crates) &&
Array.isArray(value.sandboxBackends) &&
Array.isArray(value.providers) &&
value.providers.every(
(provider) =>
isRecord(provider) &&
typeof provider.id === "string" &&
typeof provider.label === "string" &&
typeof provider.env === "string",
) &&
Array.isArray(value.models) &&
value.models.every(isModelFact) &&
(value.defaultModel === null || typeof value.defaultModel === "string") &&
(value.nodeEngines === null || typeof value.nodeEngines === "string") &&
(value.toolCount === null || typeof value.toolCount === "number") &&
(value.license === null || typeof value.license === "string") &&
(value.latestPublishedRelease === null ||
isPublishedRelease(value.latestPublishedRelease))
);
}
function newestPublishedRelease(
build: PublishedReleaseFact | null,
cached: PublishedReleaseFact | null,
): PublishedReleaseFact | null {
if (!build) return cached;
if (!cached) return build;
return Date.parse(cached.publishedAt) > Date.parse(build.publishedAt) ? cached : build;
}
/**
* Select the mechanical fact snapshot independently from the latest published
* release. A legacy or malformed KV value cannot replace a newer build merely
* because it exists. Source commit timestamps provide ordering, while an exact
* revision match permits the runtime snapshot for the same source.
*/
export function resolveFacts(build: RepoFacts, cached: unknown): FactsResolution {
if (cached == null) {
return {
facts: build,
source: "build",
reason: "no-kv-snapshot",
buildSourceRevision: build.sourceRevision,
kvSourceRevision: null,
};
}
if (!isRepoFacts(cached)) {
return {
facts: build,
source: "build",
reason: "invalid-kv-snapshot",
buildSourceRevision: build.sourceRevision,
kvSourceRevision: null,
};
}
const latestPublishedRelease = newestPublishedRelease(
build.latestPublishedRelease,
cached.latestPublishedRelease,
);
const withLatestRelease = (facts: RepoFacts): RepoFacts => ({
...facts,
latestPublishedRelease,
});
if (!cached.sourceRevision || !cached.sourceCommittedAt) {
return {
facts: withLatestRelease(build),
source: "build",
reason: "kv-missing-source-provenance",
buildSourceRevision: build.sourceRevision,
kvSourceRevision: cached.sourceRevision,
};
}
if (!build.sourceRevision || !build.sourceCommittedAt) {
return {
facts: withLatestRelease(build),
source: "build",
reason: "build-missing-source-provenance",
buildSourceRevision: build.sourceRevision,
kvSourceRevision: cached.sourceRevision,
};
}
if (cached.sourceRevision === build.sourceRevision) {
return {
facts: withLatestRelease(cached),
source: "kv",
reason: "same-source-revision",
buildSourceRevision: build.sourceRevision,
kvSourceRevision: cached.sourceRevision,
};
}
const buildTime = Date.parse(build.sourceCommittedAt);
const cachedTime = Date.parse(cached.sourceCommittedAt);
if (Number.isFinite(buildTime) && Number.isFinite(cachedTime) && cachedTime > buildTime) {
return {
facts: withLatestRelease(cached),
source: "kv",
reason: "kv-source-newer",
buildSourceRevision: build.sourceRevision,
kvSourceRevision: cached.sourceRevision,
};
}
return {
facts: withLatestRelease(build),
source: "build",
reason: "build-source-newer-or-ambiguous",
buildSourceRevision: build.sourceRevision,
kvSourceRevision: cached.sourceRevision,
};
}
async function getKv(): Promise<KVNamespace | undefined> {
if (process.env.NEXT_PHASE === "phase-production-build") {
return undefined;
}
try {
const mod = await import("@opennextjs/cloudflare");
const ctx = await mod.getCloudflareContext({ async: true });
return (ctx.env as { CURATED_KV?: KVNamespace }).CURATED_KV;
} catch {
return undefined;
}
}
export async function getFactsWithProvenance(): Promise<FactsResolution> {
try {
const kv = await getKv();
if (!kv) return resolveFacts(BUILD_FACTS, null);
const raw = await kv.get(KV_KEY);
if (!raw) return resolveFacts(BUILD_FACTS, null);
return resolveFacts(BUILD_FACTS, JSON.parse(raw) as unknown);
} catch {
return resolveFacts(BUILD_FACTS, null);
}
}
/** Resolved facts for the current request, with fail-safe build precedence. */
export async function getFacts(): Promise<RepoFacts> {
return (await getFactsWithProvenance()).facts;
}