-
-
Notifications
You must be signed in to change notification settings - Fork 80.9k
Expand file tree
/
Copy pathprovider-policy-api.ts
More file actions
97 lines (82 loc) · 3.02 KB
/
Copy pathprovider-policy-api.ts
File metadata and controls
97 lines (82 loc) · 3.02 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
import type { ModelDefinitionConfig } from "openclaw/plugin-sdk/provider-model-shared";
import type { ModelProviderConfig } from "openclaw/plugin-sdk/provider-model-types";
import { DEEPSEEK_MODEL_CATALOG } from "./models.js";
type ModelDefinitionDraft = Partial<ModelDefinitionConfig> &
Pick<ModelDefinitionConfig, "id" | "name">;
/**
* Build a lookup from the bundled DeepSeek model catalog so we can hydrate
* missing metadata (contextWindow, cost, maxTokens) into user-configured
* model rows without overwriting explicit overrides.
*/
function buildCatalogIndex(): Map<string, ModelDefinitionConfig> {
const index = new Map<string, ModelDefinitionConfig>();
for (const model of DEEPSEEK_MODEL_CATALOG) {
index.set(model.id, model);
}
return index;
}
function isPositiveNumber(value: unknown): value is number {
return typeof value === "number" && Number.isFinite(value) && value > 0;
}
function hasCostValues(cost: unknown): cost is ModelDefinitionConfig["cost"] {
if (!cost || typeof cost !== "object") {
return false;
}
const c = cost as Record<string, unknown>;
return (
typeof c.input === "number" ||
typeof c.output === "number" ||
typeof c.cacheRead === "number" ||
typeof c.cacheWrite === "number"
);
}
/**
* Provider policy surface for DeepSeek.
*
* Hydrates missing `contextWindow`, `cost`, and `maxTokens` from the bundled
* catalog for matching model ids. Explicit user overrides are preserved.
*/
export function normalizeConfig(params: {
provider: string;
providerConfig: ModelProviderConfig;
}): ModelProviderConfig {
const { providerConfig } = params;
if (!Array.isArray(providerConfig.models) || providerConfig.models.length === 0) {
return providerConfig;
}
const catalog = buildCatalogIndex();
let mutated = false;
const nextModels = providerConfig.models.map((model) => {
const raw = model as ModelDefinitionDraft;
const catalogEntry = catalog.get(raw.id);
if (!catalogEntry) {
return model;
}
let modelMutated = false;
const patched: Record<string, unknown> = {};
// Hydrate contextWindow from catalog when missing or not a positive number.
if (!isPositiveNumber(raw.contextWindow) && isPositiveNumber(catalogEntry.contextWindow)) {
patched.contextWindow = catalogEntry.contextWindow;
modelMutated = true;
}
// Hydrate maxTokens from catalog when missing or not a positive number.
if (!isPositiveNumber(raw.maxTokens) && isPositiveNumber(catalogEntry.maxTokens)) {
patched.maxTokens = catalogEntry.maxTokens;
modelMutated = true;
}
// Hydrate cost from catalog when missing or when all fields are zero/absent.
if (!hasCostValues(raw.cost) && hasCostValues(catalogEntry.cost)) {
patched.cost = catalogEntry.cost;
modelMutated = true;
}
if (!modelMutated) {
return model;
}
mutated = true;
return { ...raw, ...patched };
});
if (!mutated) {
return providerConfig;
}
return { ...providerConfig, models: nextModels as ModelDefinitionConfig[] };
}