pi-cliproxyapi-provider 0.1.0 → 0.2.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/src/discovery.ts CHANGED
@@ -1,22 +1,6 @@
1
1
  import { join } from "node:path";
2
- import { cacheDir, providerCacheKey, type CpaProviderConfig } from "./config.ts";
3
- import { fetchCpaModels, type CpaModel } from "./cpa.ts";
4
- import { getCachedOrFetch, isFresh, readCache } from "./cache.ts";
5
- import { fetchModelsDevCatalog, readBundledModelsDevFallback } from "./models-dev.ts";
6
- import type { ModelsDevCatalog } from "./types.ts";
7
-
8
- export interface DiscoveryResult {
9
- cpaModels: CpaModel[];
10
- modelsDevCatalog: ModelsDevCatalog;
11
- sources: {
12
- cpa: "fresh" | "cache" | "stale";
13
- modelsDev: "fresh" | "cache" | "stale" | "bundled" | "disabled";
14
- };
15
- errors: {
16
- cpa?: unknown;
17
- modelsDev?: unknown;
18
- };
19
- }
2
+ import { cacheDir, providerCacheKey } from "./config.ts";
3
+ import type { CpaProviderConfig } from "./types.ts";
20
4
 
21
5
  export function cpaModelsCachePath(config: CpaProviderConfig): string {
22
6
  return join(cacheDir(), providerCacheKey(config), "cpa-models.json");
@@ -32,67 +16,3 @@ export function discoveryHeaders(config: CpaProviderConfig, apiKey?: string): Re
32
16
  ...(config.authHeader && apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),
33
17
  };
34
18
  }
35
-
36
- export async function discoverModels(options: {
37
- config: CpaProviderConfig;
38
- bundledModelsDevPath: string;
39
- force?: boolean;
40
- discoveryApiKey?: string;
41
- }): Promise<DiscoveryResult> {
42
- const cpa = await getCachedOrFetch({
43
- path: cpaModelsCachePath(options.config),
44
- ttlSeconds: options.config.cpaCacheTtlSeconds,
45
- force: options.force,
46
- fetchFresh: () => fetchCpaModels(options.config.baseUrl, discoveryHeaders(options.config, options.discoveryApiKey)),
47
- });
48
-
49
- if (!options.config.modelsDevEnabled) {
50
- return {
51
- cpaModels: cpa.data,
52
- modelsDevCatalog: {},
53
- sources: { cpa: cpa.source, modelsDev: "disabled" },
54
- errors: { cpa: cpa.error },
55
- };
56
- }
57
-
58
- if (!options.force) {
59
- const cachedModelsDev = await readCache<ModelsDevCatalog>(modelsDevCachePath());
60
- if (isFresh(cachedModelsDev, options.config.modelsDevCacheTtlSeconds)) {
61
- return {
62
- cpaModels: cpa.data,
63
- modelsDevCatalog: cachedModelsDev.data,
64
- sources: { cpa: cpa.source, modelsDev: "cache" },
65
- errors: { cpa: cpa.error },
66
- };
67
- }
68
-
69
- return {
70
- cpaModels: cpa.data,
71
- modelsDevCatalog: await readBundledModelsDevFallback(options.bundledModelsDevPath),
72
- sources: { cpa: cpa.source, modelsDev: "bundled" },
73
- errors: { cpa: cpa.error },
74
- };
75
- }
76
-
77
- try {
78
- const modelsDev = await getCachedOrFetch({
79
- path: modelsDevCachePath(),
80
- ttlSeconds: options.config.modelsDevCacheTtlSeconds,
81
- force: true,
82
- fetchFresh: fetchModelsDevCatalog,
83
- });
84
- return {
85
- cpaModels: cpa.data,
86
- modelsDevCatalog: modelsDev.data,
87
- sources: { cpa: cpa.source, modelsDev: modelsDev.source },
88
- errors: { cpa: cpa.error, modelsDev: modelsDev.error },
89
- };
90
- } catch (error) {
91
- return {
92
- cpaModels: cpa.data,
93
- modelsDevCatalog: await readBundledModelsDevFallback(options.bundledModelsDevPath),
94
- sources: { cpa: cpa.source, modelsDev: "bundled" },
95
- errors: { cpa: cpa.error, modelsDev: error },
96
- };
97
- }
98
- }
package/src/models-dev.ts CHANGED
@@ -32,6 +32,9 @@ export function parseModelsDevCatalog(payload: unknown): ModelsDevCatalog {
32
32
  }
33
33
  }
34
34
 
35
+ if (Object.keys(catalog).length === 0 && Object.keys(record).length > 0) {
36
+ throw new Error("models.dev catalog contained no valid models");
37
+ }
35
38
  return catalog;
36
39
  }
37
40
 
package/src/runtime.ts ADDED
@@ -0,0 +1,41 @@
1
+ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
2
+ import { ProviderCatalog, type CatalogRefreshResult, type CatalogSnapshot, type RefreshTarget } from "./catalog.ts";
3
+ import { buildUnavailableProviderModels } from "./provider.ts";
4
+ import { buildProviderRegistration } from "./registration.ts";
5
+ import type { CpaProviderConfig } from "./types.ts";
6
+
7
+ export interface ProviderRuntimeOptions {
8
+ pi: ExtensionAPI;
9
+ config: CpaProviderConfig;
10
+ catalog: ProviderCatalog;
11
+ }
12
+
13
+ export class ProviderRuntime {
14
+ private registeredFingerprint?: string;
15
+ private readonly options: ProviderRuntimeOptions;
16
+
17
+ constructor(options: ProviderRuntimeOptions) {
18
+ this.options = options;
19
+ }
20
+
21
+ async start(): Promise<CatalogSnapshot> {
22
+ const snapshot = await this.options.catalog.load();
23
+ this.register(snapshot, true);
24
+ return snapshot;
25
+ }
26
+
27
+ async refresh(target: RefreshTarget = "all", mode: "background" | "manual" = "manual"): Promise<CatalogRefreshResult> {
28
+ const result = await this.options.catalog.refresh(target, mode);
29
+ if (result.models.updated || result.metadata.updated) this.register(result.snapshot, false);
30
+ return result;
31
+ }
32
+
33
+ private register(snapshot: CatalogSnapshot, force: boolean): void {
34
+ const models = snapshot.built.models.length > 0 ? snapshot.built.models : buildUnavailableProviderModels();
35
+ const fingerprint = JSON.stringify(models);
36
+ if (!force && fingerprint === this.registeredFingerprint) return;
37
+ const registration = buildProviderRegistration(this.options.config, models);
38
+ this.options.pi.registerProvider(registration.providerName, registration.config);
39
+ this.registeredFingerprint = fingerprint;
40
+ }
41
+ }
package/src/types.ts CHANGED
@@ -6,8 +6,6 @@ export interface CpaProviderConfig {
6
6
  authRequired: boolean;
7
7
  authHeader: boolean;
8
8
  headers: Record<string, string>;
9
- cpaCacheTtlSeconds: number;
10
- modelsDevCacheTtlSeconds: number;
11
9
  modelsDevEnabled: boolean;
12
10
  modelAliases: Record<string, string>;
13
11
  }