Spaces:
Runtime error
Runtime error
File size: 2,742 Bytes
cd8bd0a | 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 | import { getProviderConnections, updateProviderConnection } from "@/lib/db/providers";
import { getSettings, updateSettings } from "@/lib/db/settings";
import { getCodexRequestDefaults } from "./requestDefaults";
type JsonRecord = Record<string, unknown>;
const MIGRATION_SETTING_KEY = "codexConnectionDefaultsMigrationV1";
function asRecord(value: unknown): JsonRecord {
return value && typeof value === "object" && !Array.isArray(value) ? (value as JsonRecord) : {};
}
function parseLegacyCodexServiceTier(value: unknown): { enabled: boolean } {
if (typeof value === "string") {
try {
return parseLegacyCodexServiceTier(JSON.parse(value));
} catch {
return { enabled: false };
}
}
const record = asRecord(value);
const tier = typeof record.tier === "string" ? record.tier.trim().toLowerCase() : "";
return { enabled: record.enabled === true && (!tier || tier === "priority" || tier === "fast") };
}
export async function migrateCodexConnectionDefaultsFromLegacySettings(): Promise<{
migrated: boolean;
updatedConnectionIds: string[];
legacyFastEnabled: boolean;
}> {
const settings = await getSettings();
if (settings[MIGRATION_SETTING_KEY]) {
return {
migrated: false,
updatedConnectionIds: [],
legacyFastEnabled: parseLegacyCodexServiceTier(settings.codexServiceTier).enabled,
};
}
const legacyFastEnabled = parseLegacyCodexServiceTier(settings.codexServiceTier).enabled;
const codexConnections = await getProviderConnections({ provider: "codex" });
const updatedConnectionIds: string[] = [];
for (const connection of codexConnections) {
const providerSpecificData = asRecord(connection.providerSpecificData);
const existingDefaults = getCodexRequestDefaults(providerSpecificData);
const nextDefaults: JsonRecord = { ...existingDefaults };
if (!existingDefaults.reasoningEffort) {
nextDefaults.reasoningEffort = "medium";
}
if (legacyFastEnabled && !existingDefaults.serviceTier) {
nextDefaults.serviceTier = "priority";
}
const defaultsChanged =
nextDefaults.reasoningEffort !== existingDefaults.reasoningEffort ||
nextDefaults.serviceTier !== existingDefaults.serviceTier;
if (!defaultsChanged) continue;
await updateProviderConnection(connection.id, {
providerSpecificData: {
...providerSpecificData,
requestDefaults: nextDefaults,
},
});
updatedConnectionIds.push(connection.id);
}
await updateSettings({
[MIGRATION_SETTING_KEY]: {
completedAt: new Date().toISOString(),
updatedConnectionIds,
legacyFastEnabled,
},
});
return {
migrated: true,
updatedConnectionIds,
legacyFastEnabled,
};
}
|