effective-model-selection.ts 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import type { AccountProviderStates } from "./account-provider-state.js";
  2. import type { EffectiveModelSelectionResult } from "@zcode/shared/model-selection";
  3. export type { EffectiveModelSelectionResult } from "@zcode/shared/model-selection";
  4. import {
  5. validateModelSelectionOptions,
  6. type ModelSelection,
  7. type ProviderRegistryView,
  8. } from "./registry.js";
  9. export type ModelSelectionProviderKind = "ordinary" | "account-plan" | "account-offpeak";
  10. export type ModelSelectionProviderClassifier = (providerId: string) => ModelSelectionProviderKind;
  11. /**
  12. * 只解析未来执行的意图,不改原选择、持久记录或已固定请求。
  13. * 原因:读取时清库会让临时失效永久丢失;账号对应也不能退化为同名模型跨任意供应商匹配。
  14. */
  15. export function resolveEffectiveModelSelection(input: {
  16. readonly selection: ModelSelection | null;
  17. readonly registry: ProviderRegistryView;
  18. readonly accountStates?: AccountProviderStates;
  19. readonly classifyProvider: ModelSelectionProviderClassifier;
  20. readonly resolveLegacyReasoningLevel?: (selection: ModelSelection) => string | undefined;
  21. }): EffectiveModelSelectionResult {
  22. const original = input.selection;
  23. if (!original)
  24. return Object.freeze({ effectiveSelection: null, selectionIssue: "selection-missing" });
  25. const kind = input.classifyProvider(original.providerId);
  26. let providerId = original.providerId;
  27. if (kind === "account-plan") {
  28. const current = Object.entries(input.accountStates ?? {}).filter(
  29. ([id, state]) => state.current === true && input.classifyProvider(id) === "account-plan",
  30. );
  31. if (current.length !== 1) {
  32. return Object.freeze({
  33. effectiveSelection: null,
  34. selectionIssue: "account-connection-unavailable",
  35. });
  36. }
  37. providerId = current[0]![0];
  38. }
  39. const provider = input.registry.providers.find(
  40. (candidate) => candidate.providerId === providerId,
  41. );
  42. if (!provider || (provider.config.visibility === "hidden" && kind !== "account-offpeak")) {
  43. return Object.freeze({ effectiveSelection: null, selectionIssue: "provider-not-found" });
  44. }
  45. const model = provider.models.find((candidate) => candidate.modelId === original.modelId);
  46. if (!model) return Object.freeze({ effectiveSelection: null, selectionIssue: "model-not-found" });
  47. let normalized = original;
  48. let validation = validateModelSelectionOptions(model, normalized);
  49. if (!validation.ok && validation.code === "reasoning-level-not-supported") {
  50. const reasoningLevel = input.resolveLegacyReasoningLevel?.({ ...original, providerId });
  51. if (reasoningLevel !== undefined) {
  52. const candidate = { ...original, options: { ...original.options, reasoningLevel } };
  53. const checked = validateModelSelectionOptions(model, candidate);
  54. if (checked.ok) {
  55. normalized = candidate;
  56. validation = checked;
  57. }
  58. }
  59. }
  60. const selection = Object.freeze({
  61. providerId,
  62. modelId: original.modelId,
  63. ...(validation.ok && normalized.options
  64. ? { options: Object.freeze({ ...normalized.options }) }
  65. : {}),
  66. });
  67. return Object.freeze({
  68. effectiveSelection: selection,
  69. ...(!validation.ok &&
  70. (validation.code === "reasoning-level-missing" ||
  71. validation.code === "reasoning-level-not-supported")
  72. ? { selectionIssue: validation.code }
  73. : {}),
  74. });
  75. }