Spaces:
Running
Running
File size: 4,206 Bytes
c6afb12 c85bc2e c6afb12 c85bc2e c6afb12 a290b8b c85bc2e c6afb12 c85bc2e c6afb12 | 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 | import { env } from '$env/dynamic/private';
import type { BucketDataState } from '$lib/types/bucket-state';
import type { ToolsDataset } from '$lib/types/tools-data';
import { adaptBucketTools } from './adapt-tools';
import { emptyToolsDataset } from './fallback-data';
import { toPublicBucketError } from './public-error';
import { fetchBucketRankingSnapshot } from './ranking-snapshot';
import { fetchBucketVisualizationSnapshot } from './tools-snapshot';
import type { BucketVisualizationSnapshot } from './types';
const DEFAULT_CACHE_TTL_MS = 5 * 60 * 1000;
let cached:
{ value: BucketVisualizationSnapshot; snapshotId: string; expiresAt: number } | undefined;
let adaptedCached: { value: ToolsDataset; snapshotId: string } | undefined;
let inFlight: Promise<BucketVisualizationSnapshot> | undefined;
function cacheTtlMs() {
const configured = Number(env.HF_BUCKET_CACHE_TTL_MS);
return Number.isInteger(configured) && configured > 0 ? configured : DEFAULT_CACHE_TTL_MS;
}
async function refreshToolsSnapshot() {
const rankingSnapshot = await fetchBucketRankingSnapshot({ refreshManifest: true });
if (cached?.snapshotId === rankingSnapshot.manifest.snapshot_id) {
cached.expiresAt = Date.now() + cacheTtlMs();
return cached.value;
}
const value = await fetchBucketVisualizationSnapshot(rankingSnapshot);
cached = {
value,
snapshotId: rankingSnapshot.manifest.snapshot_id,
expiresAt: Date.now() + cacheTtlMs()
};
return value;
}
export async function getHfBucketToolsSnapshot() {
if (cached && cached.expiresAt > Date.now()) return cached.value;
if (!inFlight) {
inFlight = refreshToolsSnapshot().finally(() => {
inFlight = undefined;
});
}
try {
return await inFlight;
} catch (error) {
if (cached) {
console.warn(
'[hf-bucket] Serving stale Tools visualization data after refresh failure.',
error
);
return cached.value;
}
throw error;
}
}
async function getHfBucketToolsSnapshotState(): Promise<
BucketDataState<BucketVisualizationSnapshot>
> {
if (!inFlight) {
inFlight = refreshToolsSnapshot().finally(() => {
inFlight = undefined;
});
}
try {
return {
status: 'ok',
source: 'remote',
data: await inFlight
};
} catch (error) {
if (cached) {
console.warn(
'[hf-bucket] Serving stale Tools visualization data after refresh failure.',
error
);
return {
status: 'stale',
source: 'stale',
data: cached.value,
error: toPublicBucketError(error)
};
}
console.error('[hf-bucket] Tools visualization data is unavailable.', error);
return {
status: 'unavailable',
source: 'unavailable',
data: emptyToolsDataset() as unknown as BucketVisualizationSnapshot,
error: toPublicBucketError(error)
};
}
}
export async function getHfBucketTools(): Promise<ToolsDataset> {
const state = await getHfBucketToolsState();
if (state.status === 'unavailable') throw new Error(state.error.message);
return state.data;
}
export async function getHfBucketToolsState(): Promise<BucketDataState<ToolsDataset>> {
const snapshotState = await getHfBucketToolsSnapshotState();
if (snapshotState.status === 'unavailable') {
return {
...snapshotState,
data: emptyToolsDataset()
};
}
try {
const snapshotId = snapshotState.data.manifest.snapshot_id;
const data =
adaptedCached?.snapshotId === snapshotId
? adaptedCached.value
: adaptBucketTools(snapshotState.data);
adaptedCached = { value: data, snapshotId };
if (snapshotState.status === 'stale') {
return {
status: 'stale',
source: 'stale',
data,
error: snapshotState.error
};
}
return {
status: 'ok',
source: snapshotState.source,
data
};
} catch (error) {
if (cached) {
console.warn(
'[hf-bucket] Serving stale Tools visualization data after adapt failure.',
error
);
return {
status: 'stale',
source: 'stale',
data: adaptBucketTools(cached.value),
error: toPublicBucketError(error)
};
}
console.error('[hf-bucket] Tools visualization data is unavailable.', error);
return {
status: 'unavailable',
source: 'unavailable',
data: emptyToolsDataset(),
error: toPublicBucketError(error)
};
}
}
|