pi-cliproxyapi-provider 0.1.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/LICENSE +21 -0
- package/README.md +153 -0
- package/data/models-dev-fallback.json +1 -0
- package/extensions/index.ts +29 -0
- package/package.json +41 -0
- package/src/auth.ts +9 -0
- package/src/cache.ts +55 -0
- package/src/commands.ts +159 -0
- package/src/config.ts +164 -0
- package/src/cpa.ts +49 -0
- package/src/discovery.ts +98 -0
- package/src/matching.ts +73 -0
- package/src/models-dev.ts +50 -0
- package/src/network.ts +28 -0
- package/src/provider.ts +113 -0
- package/src/registration.ts +26 -0
- package/src/types.ts +50 -0
package/src/provider.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type { CpaModel } from "./cpa.ts";
|
|
2
|
+
import { findMetadataMatch, type MetadataMatchMethod } from "./matching.ts";
|
|
3
|
+
import type { InputModality, ModelsDevCatalog, ModelsDevMetadata, ProviderModelConfigLike } from "./types.ts";
|
|
4
|
+
|
|
5
|
+
export const PI_MODEL_DEFAULTS = {
|
|
6
|
+
reasoning: false,
|
|
7
|
+
input: ["text"] as InputModality[],
|
|
8
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
9
|
+
contextWindow: 128000,
|
|
10
|
+
maxTokens: 16384,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export interface BuildProviderModelsStats {
|
|
14
|
+
total: number;
|
|
15
|
+
enriched: number;
|
|
16
|
+
unmatched: number;
|
|
17
|
+
matchMethods: Record<MetadataMatchMethod, number>;
|
|
18
|
+
unmatchedModelIds: string[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface BuildProviderModelsResult {
|
|
22
|
+
models: ProviderModelConfigLike[];
|
|
23
|
+
stats: BuildProviderModelsStats;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function inputFromMetadata(metadata: ModelsDevMetadata): InputModality[] {
|
|
27
|
+
const input = metadata.modalities?.input ?? [];
|
|
28
|
+
return input.includes("image") ? ["text", "image"] : ["text"];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function costFromMetadata(metadata: ModelsDevMetadata): ProviderModelConfigLike["cost"] {
|
|
32
|
+
return {
|
|
33
|
+
input: metadata.cost?.input ?? 0,
|
|
34
|
+
output: metadata.cost?.output ?? 0,
|
|
35
|
+
cacheRead: metadata.cost?.cache_read ?? 0,
|
|
36
|
+
cacheWrite: metadata.cost?.cache_write ?? 0,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function modelFromMetadata(cpaModel: CpaModel, metadata: ModelsDevMetadata): ProviderModelConfigLike {
|
|
41
|
+
return {
|
|
42
|
+
id: cpaModel.id,
|
|
43
|
+
name: metadata.name ?? cpaModel.id,
|
|
44
|
+
reasoning: metadata.reasoning ?? PI_MODEL_DEFAULTS.reasoning,
|
|
45
|
+
input: inputFromMetadata(metadata),
|
|
46
|
+
cost: costFromMetadata(metadata),
|
|
47
|
+
contextWindow: metadata.limit?.context ?? PI_MODEL_DEFAULTS.contextWindow,
|
|
48
|
+
maxTokens: metadata.limit?.output ?? PI_MODEL_DEFAULTS.maxTokens,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function cloneModelDefaults(): typeof PI_MODEL_DEFAULTS {
|
|
53
|
+
return {
|
|
54
|
+
...PI_MODEL_DEFAULTS,
|
|
55
|
+
input: [...PI_MODEL_DEFAULTS.input],
|
|
56
|
+
cost: { ...PI_MODEL_DEFAULTS.cost },
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function defaultModel(cpaModel: CpaModel): ProviderModelConfigLike {
|
|
61
|
+
return {
|
|
62
|
+
id: cpaModel.id,
|
|
63
|
+
name: cpaModel.id,
|
|
64
|
+
...cloneModelDefaults(),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function emptyMatchMethods(): Record<MetadataMatchMethod, number> {
|
|
69
|
+
return {
|
|
70
|
+
alias: 0,
|
|
71
|
+
exact: 0,
|
|
72
|
+
"owner-prefix": 0,
|
|
73
|
+
suffix: 0,
|
|
74
|
+
"normalized-suffix": 0,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function buildUnavailableProviderModels(id = "login-required"): ProviderModelConfigLike[] {
|
|
79
|
+
return [{ id, name: id, ...cloneModelDefaults() }];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function buildProviderModels(
|
|
83
|
+
cpaModels: CpaModel[],
|
|
84
|
+
catalog: ModelsDevCatalog,
|
|
85
|
+
aliases: Record<string, string>,
|
|
86
|
+
): BuildProviderModelsResult {
|
|
87
|
+
const matchMethods = emptyMatchMethods();
|
|
88
|
+
const unmatchedModelIds: string[] = [];
|
|
89
|
+
let enriched = 0;
|
|
90
|
+
|
|
91
|
+
const models = cpaModels.map((cpaModel) => {
|
|
92
|
+
const match = findMetadataMatch(cpaModel, catalog, aliases);
|
|
93
|
+
if (!match) {
|
|
94
|
+
unmatchedModelIds.push(cpaModel.id);
|
|
95
|
+
return defaultModel(cpaModel);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
enriched += 1;
|
|
99
|
+
matchMethods[match.method] += 1;
|
|
100
|
+
return modelFromMetadata(cpaModel, match.metadata);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
models,
|
|
105
|
+
stats: {
|
|
106
|
+
total: cpaModels.length,
|
|
107
|
+
enriched,
|
|
108
|
+
unmatched: unmatchedModelIds.length,
|
|
109
|
+
matchMethods,
|
|
110
|
+
unmatchedModelIds,
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ProviderConfig } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { CpaProviderConfig } from "./types.ts";
|
|
3
|
+
import type { ProviderModelConfigLike } from "./types.ts";
|
|
4
|
+
|
|
5
|
+
export interface ProviderRegistration {
|
|
6
|
+
providerName: string;
|
|
7
|
+
config: ProviderConfig;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function buildProviderRegistration(
|
|
11
|
+
config: CpaProviderConfig,
|
|
12
|
+
models: ProviderModelConfigLike[],
|
|
13
|
+
): ProviderRegistration {
|
|
14
|
+
return {
|
|
15
|
+
providerName: config.providerName,
|
|
16
|
+
config: {
|
|
17
|
+
name: `CLIProxyAPI (${config.providerName})`,
|
|
18
|
+
baseUrl: config.baseUrl,
|
|
19
|
+
api: "openai-completions",
|
|
20
|
+
apiKey: config.authRequired ? "$CLIPROXYAPI_API_KEY" : "cliproxyapi-no-auth",
|
|
21
|
+
authHeader: config.authRequired && config.authHeader,
|
|
22
|
+
headers: Object.keys(config.headers).length > 0 ? config.headers : undefined,
|
|
23
|
+
models,
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export type InputModality = "text" | "image";
|
|
2
|
+
|
|
3
|
+
export interface CpaProviderConfig {
|
|
4
|
+
providerName: string;
|
|
5
|
+
baseUrl: string;
|
|
6
|
+
authRequired: boolean;
|
|
7
|
+
authHeader: boolean;
|
|
8
|
+
headers: Record<string, string>;
|
|
9
|
+
cpaCacheTtlSeconds: number;
|
|
10
|
+
modelsDevCacheTtlSeconds: number;
|
|
11
|
+
modelsDevEnabled: boolean;
|
|
12
|
+
modelAliases: Record<string, string>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface ModelsDevMetadata {
|
|
16
|
+
id: string;
|
|
17
|
+
name?: string;
|
|
18
|
+
reasoning?: boolean;
|
|
19
|
+
modalities?: {
|
|
20
|
+
input?: string[];
|
|
21
|
+
output?: string[];
|
|
22
|
+
};
|
|
23
|
+
limit?: {
|
|
24
|
+
context?: number;
|
|
25
|
+
output?: number;
|
|
26
|
+
};
|
|
27
|
+
cost?: {
|
|
28
|
+
input?: number;
|
|
29
|
+
output?: number;
|
|
30
|
+
cache_read?: number;
|
|
31
|
+
cache_write?: number;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export type ModelsDevCatalog = Record<string, ModelsDevMetadata>;
|
|
36
|
+
|
|
37
|
+
export interface ProviderModelConfigLike {
|
|
38
|
+
id: string;
|
|
39
|
+
name: string;
|
|
40
|
+
reasoning: boolean;
|
|
41
|
+
input: InputModality[];
|
|
42
|
+
cost: {
|
|
43
|
+
input: number;
|
|
44
|
+
output: number;
|
|
45
|
+
cacheRead: number;
|
|
46
|
+
cacheWrite: number;
|
|
47
|
+
};
|
|
48
|
+
contextWindow: number;
|
|
49
|
+
maxTokens: number;
|
|
50
|
+
}
|