feat: unify installation model catalog
This commit is contained in:
@@ -6,7 +6,7 @@ import {
|
||||
type AuthOperationPrecondition,
|
||||
} from "../auth/authOperation";
|
||||
import { getSettings } from "./settings";
|
||||
import { getWorkspace, listWorkspaces } from "./workspaces";
|
||||
import { listWorkspaces } from "./workspaces";
|
||||
import {
|
||||
WORKSPACE_POLICY_ERROR, WORKSPACE_SUMMARY_ERROR, WorkspaceSelectionError, workspacePolicyGate,
|
||||
workspacePreferences, type WorkspacePreference,
|
||||
@@ -34,24 +34,6 @@ async function selectedPreferences(precondition?: AuthOperationPrecondition): Pr
|
||||
});
|
||||
}
|
||||
|
||||
function reconcileWorkspacePolicy(preferences: WorkspacePreference, allowed: readonly string[], defaultModel?: string) {
|
||||
if (allowed.length === 0) throw new WorkspaceSelectionError(WORKSPACE_POLICY_ERROR);
|
||||
const selected = preferences.provider && preferences.model
|
||||
? `${preferences.provider}/${preferences.model}`
|
||||
: undefined;
|
||||
if (selected && allowed.includes(selected)) return preferences;
|
||||
const replacement = defaultModel && allowed.includes(defaultModel) ? defaultModel : allowed[0];
|
||||
const separator = replacement.indexOf("/");
|
||||
if (separator <= 0 || separator === replacement.length - 1) {
|
||||
throw new WorkspaceSelectionError(WORKSPACE_POLICY_ERROR);
|
||||
}
|
||||
return workspacePreferences.save({
|
||||
...preferences,
|
||||
provider: replacement.slice(0, separator),
|
||||
model: replacement.slice(separator + 1),
|
||||
});
|
||||
}
|
||||
|
||||
async function ensureWorkspaceSelectionPolicy(precondition?: AuthOperationPrecondition): Promise<WorkspacePreference> {
|
||||
while (true) {
|
||||
requireAuthOperationPrecondition(precondition);
|
||||
@@ -104,45 +86,8 @@ async function ensureWorkspaceSelectionPolicy(precondition?: AuthOperationPrecon
|
||||
throw new WorkspaceSelectionError(WORKSPACE_POLICY_ERROR);
|
||||
}
|
||||
workspacePolicyGate.select(workspaceId);
|
||||
const outcome = await Promise.race([
|
||||
getWorkspace(workspaceId).then(
|
||||
(record) => ({ kind: "record" as const, record }),
|
||||
() => ({ kind: "error" as const }),
|
||||
),
|
||||
workspacePolicyGate.waitForCurrent(() => workspacePreferences.load()).then(
|
||||
(selection) => ({ kind: "selection" as const, selection }),
|
||||
),
|
||||
]);
|
||||
requireAuthOperationPrecondition(precondition);
|
||||
if (outcome.kind === "selection") {
|
||||
if (outcome.selection.workspaceId !== workspaceId) continue;
|
||||
return outcome.selection;
|
||||
}
|
||||
if (outcome.kind === "error") {
|
||||
requireAuthOperationPrecondition(precondition);
|
||||
if (workspacePreferences.load().workspaceId !== workspaceId) continue;
|
||||
workspacePolicyGate.reject(workspaceId);
|
||||
throw new WorkspaceSelectionError(WORKSPACE_POLICY_ERROR);
|
||||
}
|
||||
const { record } = outcome;
|
||||
requireAuthOperationPrecondition(precondition);
|
||||
if (workspacePreferences.load().workspaceId !== workspaceId) continue;
|
||||
let selection: WorkspacePreference;
|
||||
try {
|
||||
selection = reconcileWorkspacePolicy(
|
||||
preferences,
|
||||
record.workspace.llm_policy.allowed,
|
||||
record.workspace.llm_policy.default,
|
||||
);
|
||||
} catch (error) {
|
||||
requireAuthOperationPrecondition(precondition);
|
||||
workspacePolicyGate.reject(workspaceId);
|
||||
throw error;
|
||||
}
|
||||
requireAuthOperationPrecondition(precondition);
|
||||
workspacePolicyGate.resolve(workspaceId);
|
||||
requireAuthOperationPrecondition(precondition);
|
||||
if (workspacePreferences.load().workspaceId === workspaceId) return selection;
|
||||
return workspacePreferences.load();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user