pi-cliproxyapi-provider 0.4.14 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -3
- package/data/models-dev-fallback.json +1 -1
- package/package.json +1 -1
- package/src/catalog.ts +5 -2
- package/src/config.ts +25 -0
- package/src/matching.ts +82 -6
- package/src/models-dev.ts +6 -1
- package/src/provider.ts +4 -1
- package/src/types.ts +3 -0
package/package.json
CHANGED
package/src/catalog.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { cpaModelsCachePath, discoveryHeaders, modelsDevCachePath } from "./discovery.ts";
|
|
2
2
|
import { readCache, writeCache, type CacheEnvelope } from "./cache.ts";
|
|
3
3
|
import { fetchCpaModels, parseCpaModelsCache, type CpaModel } from "./cpa.ts";
|
|
4
|
-
import { fetchModelsDevCatalog, parseModelsDevCatalog, readBundledModelsDevFallback } from "./models-dev.ts";
|
|
4
|
+
import { fetchModelsDevCatalog, hasSourceProviderMetadata, parseModelsDevCatalog, readBundledModelsDevFallback } from "./models-dev.ts";
|
|
5
5
|
import { buildProviderModels, type BuildProviderModelsResult } from "./provider.ts";
|
|
6
6
|
import type { Gpt56ContextWindowMode } from "./settings.ts";
|
|
7
7
|
import type { CpaProviderConfig, ModelsDevCatalog } from "./types.ts";
|
|
@@ -205,7 +205,9 @@ export class ProviderCatalog {
|
|
|
205
205
|
private async loadMetadata(): Promise<{ data: ModelsDevCatalog; fetchedAt?: number; source: MetadataSource }> {
|
|
206
206
|
if (!this.options.config.modelsDevEnabled) return { data: {}, source: "disabled" };
|
|
207
207
|
const cached = await readCache(modelsDevCachePath(), parseModelsDevCatalog);
|
|
208
|
-
if (cached
|
|
208
|
+
if (cached && hasSourceProviderMetadata(cached.data)) {
|
|
209
|
+
return { data: cached.data, fetchedAt: cached.fetchedAt, source: "cache" };
|
|
210
|
+
}
|
|
209
211
|
return { data: await readBundledModelsDevFallback(this.options.bundledModelsDevPath), source: "bundled" };
|
|
210
212
|
}
|
|
211
213
|
|
|
@@ -229,6 +231,7 @@ export class ProviderCatalog {
|
|
|
229
231
|
this.options.config.modelAliases,
|
|
230
232
|
this.options.gpt56ContextWindow,
|
|
231
233
|
this.options.config.modelOverrides,
|
|
234
|
+
this.options.config.metadataFallbackProvider,
|
|
232
235
|
),
|
|
233
236
|
};
|
|
234
237
|
return this.snapshot;
|
package/src/config.ts
CHANGED
|
@@ -21,6 +21,7 @@ export const DEFAULT_CONFIG: CpaProviderConfig = {
|
|
|
21
21
|
authHeader: true,
|
|
22
22
|
headers: {},
|
|
23
23
|
modelsDevEnabled: true,
|
|
24
|
+
metadataFallbackProvider: "openrouter",
|
|
24
25
|
modelAliases: {},
|
|
25
26
|
modelOverrides: {},
|
|
26
27
|
};
|
|
@@ -49,16 +50,24 @@ export function parseBooleanEnv(value: string | undefined): boolean | undefined
|
|
|
49
50
|
return undefined;
|
|
50
51
|
}
|
|
51
52
|
|
|
53
|
+
function normalizeMetadataFallbackProvider(value: string | null): string | null {
|
|
54
|
+
return value?.trim().toLowerCase() === "none" ? null : value;
|
|
55
|
+
}
|
|
56
|
+
|
|
52
57
|
function normalizeConfig(config: CpaProviderConfig): CpaProviderConfig {
|
|
53
58
|
return {
|
|
54
59
|
...config,
|
|
55
60
|
authHeader: config.authRequired ? config.authHeader : false,
|
|
61
|
+
metadataFallbackProvider: normalizeMetadataFallbackProvider(config.metadataFallbackProvider),
|
|
56
62
|
};
|
|
57
63
|
}
|
|
58
64
|
|
|
59
65
|
function safeProjectConfig(projectConfig?: ConfigLayer): ConfigLayer | undefined {
|
|
60
66
|
if (!projectConfig) return undefined;
|
|
61
67
|
return {
|
|
68
|
+
...(projectConfig.metadataFallbackProvider !== undefined
|
|
69
|
+
? { metadataFallbackProvider: projectConfig.metadataFallbackProvider }
|
|
70
|
+
: {}),
|
|
62
71
|
...(projectConfig.modelAliases !== undefined ? { modelAliases: projectConfig.modelAliases } : {}),
|
|
63
72
|
...(projectConfig.modelOverrides !== undefined ? { modelOverrides: projectConfig.modelOverrides } : {}),
|
|
64
73
|
};
|
|
@@ -89,6 +98,13 @@ function projectConfigLayer(value: unknown, path: string): ConfigLayer {
|
|
|
89
98
|
}
|
|
90
99
|
|
|
91
100
|
const record = value as Record<string, unknown>;
|
|
101
|
+
if (
|
|
102
|
+
record.metadataFallbackProvider !== undefined &&
|
|
103
|
+
record.metadataFallbackProvider !== null &&
|
|
104
|
+
(typeof record.metadataFallbackProvider !== "string" || !record.metadataFallbackProvider.trim())
|
|
105
|
+
) {
|
|
106
|
+
throw new Error(`metadataFallbackProvider must be a non-empty string or null in project config file: ${path}`);
|
|
107
|
+
}
|
|
92
108
|
if (record.modelAliases !== undefined && !isStringMap(record.modelAliases)) {
|
|
93
109
|
throw new Error(`modelAliases must be an object with string values in project config file: ${path}`);
|
|
94
110
|
}
|
|
@@ -114,12 +130,14 @@ function envLayer(env: NodeJS.ProcessEnv): ConfigLayer {
|
|
|
114
130
|
const authRequired = parseBooleanEnv(env.CLIPROXYAPI_AUTH_REQUIRED);
|
|
115
131
|
const authHeader = parseBooleanEnv(env.CLIPROXYAPI_AUTH_HEADER);
|
|
116
132
|
const modelsDevEnabled = parseBooleanEnv(env.CLIPROXYAPI_MODELS_DEV_ENABLED);
|
|
133
|
+
const metadataFallbackProvider = env.CLIPROXYAPI_METADATA_FALLBACK_PROVIDER?.trim();
|
|
117
134
|
return {
|
|
118
135
|
...(env.CLIPROXYAPI_BASE_URL ? { baseUrl: env.CLIPROXYAPI_BASE_URL } : {}),
|
|
119
136
|
...(env.CLIPROXYAPI_PROVIDER_NAME ? { providerName: env.CLIPROXYAPI_PROVIDER_NAME } : {}),
|
|
120
137
|
...(authRequired !== undefined ? { authRequired } : {}),
|
|
121
138
|
...(authHeader !== undefined ? { authHeader } : {}),
|
|
122
139
|
...(modelsDevEnabled !== undefined ? { modelsDevEnabled } : {}),
|
|
140
|
+
...(metadataFallbackProvider ? { metadataFallbackProvider } : {}),
|
|
123
141
|
};
|
|
124
142
|
}
|
|
125
143
|
|
|
@@ -195,6 +213,13 @@ function validateConfigLayer(value: unknown, path: string): ConfigLayer {
|
|
|
195
213
|
throw new Error(`${field} must be a string in config file: ${path}`);
|
|
196
214
|
}
|
|
197
215
|
}
|
|
216
|
+
if (
|
|
217
|
+
record.metadataFallbackProvider !== undefined &&
|
|
218
|
+
record.metadataFallbackProvider !== null &&
|
|
219
|
+
(typeof record.metadataFallbackProvider !== "string" || !record.metadataFallbackProvider.trim())
|
|
220
|
+
) {
|
|
221
|
+
throw new Error(`metadataFallbackProvider must be a non-empty string or null in config file: ${path}`);
|
|
222
|
+
}
|
|
198
223
|
|
|
199
224
|
const booleanFields = ["authRequired", "authHeader", "modelsDevEnabled"];
|
|
200
225
|
for (const field of booleanFields) {
|
package/src/matching.ts
CHANGED
|
@@ -16,7 +16,7 @@ const CANONICAL_OWNER_PREFIXES: Record<string, string> = {
|
|
|
16
16
|
cohere: "cohere",
|
|
17
17
|
};
|
|
18
18
|
|
|
19
|
-
export type MetadataMatchMethod = "alias" | "exact" | "owner-prefix" | "suffix" | "normalized-suffix";
|
|
19
|
+
export type MetadataMatchMethod = "alias" | "exact" | "owner-prefix" | "owner-hint" | "suffix" | "normalized-suffix" | "provider-fallback";
|
|
20
20
|
|
|
21
21
|
export interface MetadataMatch {
|
|
22
22
|
metadataId: string;
|
|
@@ -28,15 +28,65 @@ export function normalizeModelName(value: string): string {
|
|
|
28
28
|
return value.toLowerCase().replace(/[^a-z0-9]/g, "");
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
+
function metadataModelName(metadataId: string, metadata: ModelsDevMetadata): string {
|
|
32
|
+
return metadata.id.split("/").at(-1) ?? metadataId.split("/").at(-1) ?? metadataId;
|
|
33
|
+
}
|
|
34
|
+
|
|
31
35
|
function oneMatch(candidates: string[]): string | undefined {
|
|
32
36
|
const unique = [...new Set(candidates)];
|
|
33
37
|
return unique.length === 1 ? unique[0] : undefined;
|
|
34
38
|
}
|
|
35
39
|
|
|
40
|
+
function identifierTokens(value: string): string[] {
|
|
41
|
+
return value.toLowerCase().split(/[^a-z0-9]+/).filter(Boolean);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function containsContiguousTokens(container: string[], sequence: string[]): boolean {
|
|
45
|
+
if (sequence.length === 0 || sequence.length > container.length) return false;
|
|
46
|
+
return container.some((_, start) =>
|
|
47
|
+
start + sequence.length <= container.length &&
|
|
48
|
+
sequence.every((token, offset) => container[start + offset] === token)
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function sourceProvider(metadataId: string, metadata: ModelsDevMetadata): string {
|
|
53
|
+
// sourceProvider is retained by current catalog snapshots. The prefix fallback
|
|
54
|
+
// keeps older bundled/cache snapshots useful until they are refreshed.
|
|
55
|
+
return metadata.sourceProvider ?? metadataId.split("/")[0] ?? metadataId;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function ownerHintMatch(
|
|
59
|
+
owner: string | undefined,
|
|
60
|
+
candidates: string[],
|
|
61
|
+
catalog: ModelsDevCatalog,
|
|
62
|
+
): string | undefined {
|
|
63
|
+
if (!owner) return undefined;
|
|
64
|
+
const ownerTokens = identifierTokens(owner);
|
|
65
|
+
const matches = candidates.flatMap((metadataId) => {
|
|
66
|
+
const metadata = catalog[metadataId];
|
|
67
|
+
if (!metadata) return [];
|
|
68
|
+
const provider = sourceProvider(metadataId, metadata);
|
|
69
|
+
const providerTokens = identifierTokens(provider);
|
|
70
|
+
if (!containsContiguousTokens(ownerTokens, providerTokens)) return [];
|
|
71
|
+
return [{ metadataId, tokenCount: providerTokens.length, characterCount: provider.length }];
|
|
72
|
+
});
|
|
73
|
+
if (matches.length === 0) return undefined;
|
|
74
|
+
|
|
75
|
+
const bestTokenCount = Math.max(...matches.map((match) => match.tokenCount));
|
|
76
|
+
const mostTokens = matches.filter((match) => match.tokenCount === bestTokenCount);
|
|
77
|
+
const bestCharacterCount = Math.max(...mostTokens.map((match) => match.characterCount));
|
|
78
|
+
return oneMatch(
|
|
79
|
+
mostTokens
|
|
80
|
+
.filter((match) => match.characterCount === bestCharacterCount)
|
|
81
|
+
.map((match) => match.metadataId),
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
36
85
|
export function findMetadataMatch(
|
|
37
86
|
cpaModel: Pick<CpaModel, "id" | "owned_by">,
|
|
38
87
|
catalog: ModelsDevCatalog,
|
|
39
88
|
aliases: Record<string, string>,
|
|
89
|
+
fallbackProvider?: string | null,
|
|
40
90
|
): MetadataMatch | undefined {
|
|
41
91
|
const alias = aliases[cpaModel.id];
|
|
42
92
|
if (alias && catalog[alias]) {
|
|
@@ -47,7 +97,20 @@ export function findMetadataMatch(
|
|
|
47
97
|
return { metadataId: cpaModel.id, metadata: catalog[cpaModel.id], method: "exact" };
|
|
48
98
|
}
|
|
49
99
|
|
|
50
|
-
const
|
|
100
|
+
const catalogKeys = Object.keys(catalog);
|
|
101
|
+
const exactMetadataCandidates = catalogKeys.filter((key) => catalog[key]?.id === cpaModel.id);
|
|
102
|
+
const exactMetadataKey = oneMatch(exactMetadataCandidates);
|
|
103
|
+
if (exactMetadataKey) {
|
|
104
|
+
return { metadataId: exactMetadataKey, metadata: catalog[exactMetadataKey], method: "exact" };
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const suffixCandidates = catalogKeys.filter((key) =>
|
|
108
|
+
metadataModelName(key, catalog[key]) === cpaModel.id
|
|
109
|
+
);
|
|
110
|
+
const normalizedId = normalizeModelName(cpaModel.id);
|
|
111
|
+
const normalizedSuffixCandidates = catalogKeys.filter(
|
|
112
|
+
(key) => normalizeModelName(metadataModelName(key, catalog[key])) === normalizedId,
|
|
113
|
+
);
|
|
51
114
|
const owner = cpaModel.owned_by?.trim().toLowerCase();
|
|
52
115
|
const canonicalOwner = owner ? CANONICAL_OWNER_PREFIXES[owner] : undefined;
|
|
53
116
|
if (canonicalOwner) {
|
|
@@ -57,18 +120,31 @@ export function findMetadataMatch(
|
|
|
57
120
|
}
|
|
58
121
|
}
|
|
59
122
|
|
|
123
|
+
const hintedKey = ownerHintMatch(owner, normalizedSuffixCandidates, catalog);
|
|
124
|
+
if (hintedKey) {
|
|
125
|
+
return { metadataId: hintedKey, metadata: catalog[hintedKey], method: "owner-hint" };
|
|
126
|
+
}
|
|
127
|
+
|
|
60
128
|
const suffixKey = oneMatch(suffixCandidates);
|
|
61
129
|
if (suffixKey) {
|
|
62
130
|
return { metadataId: suffixKey, metadata: catalog[suffixKey], method: "suffix" };
|
|
63
131
|
}
|
|
64
132
|
|
|
65
|
-
const
|
|
66
|
-
const normalizedSuffixKey = oneMatch(
|
|
67
|
-
Object.keys(catalog).filter((key) => normalizeModelName(key.split("/").at(-1) ?? key) === normalizedId),
|
|
68
|
-
);
|
|
133
|
+
const normalizedSuffixKey = oneMatch(normalizedSuffixCandidates);
|
|
69
134
|
if (normalizedSuffixKey) {
|
|
70
135
|
return { metadataId: normalizedSuffixKey, metadata: catalog[normalizedSuffixKey], method: "normalized-suffix" };
|
|
71
136
|
}
|
|
72
137
|
|
|
138
|
+
if (fallbackProvider) {
|
|
139
|
+
const normalizedFallbackProvider = fallbackProvider.trim().toLowerCase();
|
|
140
|
+
const fallbackKey = oneMatch(normalizedSuffixCandidates.filter((metadataId) => {
|
|
141
|
+
const metadata = catalog[metadataId];
|
|
142
|
+
return metadata && sourceProvider(metadataId, metadata).toLowerCase() === normalizedFallbackProvider;
|
|
143
|
+
}));
|
|
144
|
+
if (fallbackKey) {
|
|
145
|
+
return { metadataId: fallbackKey, metadata: catalog[fallbackKey], method: "provider-fallback" };
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
73
149
|
return undefined;
|
|
74
150
|
}
|
package/src/models-dev.ts
CHANGED
|
@@ -22,7 +22,8 @@ export function parseModelsDevCatalog(payload: unknown): ModelsDevCatalog {
|
|
|
22
22
|
for (const [modelId, metadata] of Object.entries(provider.models as Record<string, unknown>)) {
|
|
23
23
|
if (!isMetadata(metadata)) continue;
|
|
24
24
|
const canonicalId = metadata.id.includes("/") ? metadata.id : `${key}/${modelId}`;
|
|
25
|
-
|
|
25
|
+
const catalogKey = canonicalId.startsWith(`${key}/`) ? canonicalId : `${key}/${canonicalId}`;
|
|
26
|
+
catalog[catalogKey] = { ...metadata, id: canonicalId, sourceProvider: key };
|
|
26
27
|
}
|
|
27
28
|
continue;
|
|
28
29
|
}
|
|
@@ -48,6 +49,10 @@ export async function fetchModelsDevCatalog(timeoutMs?: number, signal?: AbortSi
|
|
|
48
49
|
}, timeoutMs, "models.dev fetch", signal);
|
|
49
50
|
}
|
|
50
51
|
|
|
52
|
+
export function hasSourceProviderMetadata(catalog: ModelsDevCatalog): boolean {
|
|
53
|
+
return Object.values(catalog).every((metadata) => typeof metadata.sourceProvider === "string");
|
|
54
|
+
}
|
|
55
|
+
|
|
51
56
|
export async function readBundledModelsDevFallback(path: string): Promise<ModelsDevCatalog> {
|
|
52
57
|
return parseModelsDevCatalog(JSON.parse(await readFile(path, "utf8")));
|
|
53
58
|
}
|
package/src/provider.ts
CHANGED
|
@@ -126,8 +126,10 @@ function emptyMatchMethods(): Record<MetadataMatchMethod, number> {
|
|
|
126
126
|
alias: 0,
|
|
127
127
|
exact: 0,
|
|
128
128
|
"owner-prefix": 0,
|
|
129
|
+
"owner-hint": 0,
|
|
129
130
|
suffix: 0,
|
|
130
131
|
"normalized-suffix": 0,
|
|
132
|
+
"provider-fallback": 0,
|
|
131
133
|
};
|
|
132
134
|
}
|
|
133
135
|
|
|
@@ -155,13 +157,14 @@ export function buildProviderModels(
|
|
|
155
157
|
aliases: Record<string, string>,
|
|
156
158
|
gpt56ContextWindow: Gpt56ContextWindowMode = "canonical",
|
|
157
159
|
overrides: ProviderModelOverrides = {},
|
|
160
|
+
metadataFallbackProvider: string | null = "openrouter",
|
|
158
161
|
): BuildProviderModelsResult {
|
|
159
162
|
const matchMethods = emptyMatchMethods();
|
|
160
163
|
const unmatchedModelIds: string[] = [];
|
|
161
164
|
let enriched = 0;
|
|
162
165
|
|
|
163
166
|
const models = cpaModels.map((cpaModel) => {
|
|
164
|
-
const match = findMetadataMatch(cpaModel, catalog, aliases);
|
|
167
|
+
const match = findMetadataMatch(cpaModel, catalog, aliases, metadataFallbackProvider);
|
|
165
168
|
if (!match) {
|
|
166
169
|
unmatchedModelIds.push(cpaModel.id);
|
|
167
170
|
return applyModelOverride(defaultModel(cpaModel, gpt56ContextWindow), overrides);
|
package/src/types.ts
CHANGED
|
@@ -25,12 +25,15 @@ export interface CpaProviderConfig {
|
|
|
25
25
|
authHeader: boolean;
|
|
26
26
|
headers: Record<string, string>;
|
|
27
27
|
modelsDevEnabled: boolean;
|
|
28
|
+
metadataFallbackProvider: string | null;
|
|
28
29
|
modelAliases: Record<string, string>;
|
|
29
30
|
modelOverrides: ProviderModelOverrides;
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
export interface ModelsDevMetadata {
|
|
33
34
|
id: string;
|
|
35
|
+
/** models.dev provider key retained for owner-hint matching. */
|
|
36
|
+
sourceProvider?: string;
|
|
34
37
|
name?: string;
|
|
35
38
|
reasoning?: boolean;
|
|
36
39
|
modalities?: {
|