opencode-pollinations-plugin 5.4.12 → 5.4.14

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.
@@ -221,7 +221,7 @@ async function handleConnectCommand(args) {
221
221
  // 1. Universal Validation (No Syntax Check) - Functional Check
222
222
  emitStatusToast('info', 'Vérification de la clé...', 'Pollinations Config');
223
223
  try {
224
- const models = await generatePollinationsConfig(key);
224
+ const models = await generatePollinationsConfig(key, true);
225
225
  // 2. Check if we got Enterprise models
226
226
  const enterpriseModels = models.filter(m => m.id.startsWith('enter/'));
227
227
  if (enterpriseModels.length > 0) {
@@ -47,50 +47,70 @@ export function loadConfig() {
47
47
  }
48
48
  function readConfigFromDisk() {
49
49
  let config = { ...DEFAULT_CONFIG_V5 };
50
- let keyFound = false;
51
- // 1. Custom Config
50
+ let finalKey = undefined;
51
+ let source = 'none';
52
+ // TIMESTAMP BASED PRIORITY LOGIC
53
+ // We want the most recently updated Valid Key to win.
54
+ let configTime = 0;
55
+ let authTime = 0;
52
56
  try {
53
- if (fs.existsSync(CONFIG_FILE)) {
57
+ if (fs.existsSync(CONFIG_FILE))
58
+ configTime = fs.statSync(CONFIG_FILE).mtime.getTime();
59
+ }
60
+ catch (e) { }
61
+ try {
62
+ if (fs.existsSync(AUTH_FILE))
63
+ authTime = fs.statSync(AUTH_FILE).mtime.getTime();
64
+ }
65
+ catch (e) { }
66
+ // 1. EXTRACT KEYS
67
+ let configKey = undefined;
68
+ if (fs.existsSync(CONFIG_FILE)) {
69
+ try {
54
70
  const raw = fs.readFileSync(CONFIG_FILE, 'utf-8');
55
71
  const custom = JSON.parse(raw);
56
- config = { ...config, ...custom };
57
- if (config.apiKey)
58
- keyFound = true;
72
+ config = { ...config, ...custom }; // Helper: We load the rest of config anyway
73
+ if (custom.apiKey && custom.apiKey.length > 5)
74
+ configKey = custom.apiKey;
59
75
  }
76
+ catch (e) { }
60
77
  }
61
- catch (e) {
62
- logConfig(`Error loading config: ${e}`);
63
- }
64
- // 2. Auth Store (Fallback ONLY if config.json didn't exist or we are in a clear state)
65
- // If config.json exists, it is the source of truth. We shouldn't resurrect old keys from auth.json
66
- // UNLESS config.json is "empty" (just created default).
67
- // But how to know?
68
- // New logic: If Custom Config was loaded (fs.exists(CONFIG_FILE)), we trust it.
69
- // We only check Auth Store if NO custom config exists OR if custom config is explicitly "manual" and we want to auto-discover?
70
- // User complaint: "Old key comes back".
71
- // Fix: If config.json exists, we DO NOT check auth.json.
72
- if (!fs.existsSync(CONFIG_FILE) && !keyFound) {
78
+ let authKey = undefined;
79
+ if (fs.existsSync(AUTH_FILE)) {
73
80
  try {
74
- if (fs.existsSync(AUTH_FILE)) {
75
- const raw = fs.readFileSync(AUTH_FILE, 'utf-8');
76
- const authData = JSON.parse(raw);
77
- const entry = authData['pollinations'] || authData['pollinations_enter'] || authData['pollinations_api_key'];
78
- if (entry) {
79
- const key = (typeof entry === 'object' && entry.key) ? entry.key : entry;
80
- if (key && typeof key === 'string' && key.length > 10) {
81
- config.apiKey = key;
82
- config.mode = 'pro';
83
- keyFound = true;
84
- }
85
- }
81
+ const raw = fs.readFileSync(AUTH_FILE, 'utf-8');
82
+ const authData = JSON.parse(raw);
83
+ const entry = authData['pollinations'] || authData['pollinations_enter'] || authData['pollinations_api_key'];
84
+ if (entry) {
85
+ const k = (typeof entry === 'object' && entry.key) ? entry.key : entry;
86
+ if (k && typeof k === 'string' && k.length > 10)
87
+ authKey = k;
86
88
  }
87
89
  }
88
- catch (e) {
89
- logConfig(`Error reading auth.json: ${e}`);
90
+ catch (e) { }
91
+ }
92
+ // 2. DETERMINE WINNER
93
+ // If both exist, newest wins. If one exists, it wins.
94
+ if (configKey && authKey) {
95
+ if (configTime >= authTime) {
96
+ finalKey = configKey;
97
+ source = 'config.json';
98
+ }
99
+ else {
100
+ finalKey = authKey;
101
+ source = 'auth.json';
90
102
  }
91
103
  }
92
- // 3. OpenCode Config (Fallback)
93
- if (!keyFound) {
104
+ else if (configKey) {
105
+ finalKey = configKey;
106
+ source = 'config.json';
107
+ }
108
+ else if (authKey) {
109
+ finalKey = authKey;
110
+ source = 'auth.json';
111
+ }
112
+ // 3. Fallback to OpenCode Global Config (Lowest Priority)
113
+ if (!finalKey) {
94
114
  try {
95
115
  if (fs.existsSync(OPENCODE_CONFIG_FILE)) {
96
116
  const raw = fs.readFileSync(OPENCODE_CONFIG_FILE, 'utf-8');
@@ -98,16 +118,24 @@ function readConfigFromDisk() {
98
118
  const nativeKey = data?.provider?.pollinations?.options?.apiKey ||
99
119
  data?.provider?.pollinations_enter?.options?.apiKey;
100
120
  if (nativeKey && nativeKey.length > 5 && nativeKey !== 'dummy') {
101
- config.apiKey = nativeKey;
102
- config.mode = 'pro';
103
- keyFound = true;
121
+ finalKey = nativeKey;
122
+ source = 'opencode.json';
104
123
  }
105
124
  }
106
125
  }
107
126
  catch (e) { }
108
127
  }
109
- if (!keyFound && config.mode === 'pro') {
110
- config.mode = 'manual';
128
+ // 4. APPLY
129
+ if (finalKey) {
130
+ config.apiKey = finalKey;
131
+ config.mode = 'pro';
132
+ // logConfig(`Loaded Key from ${source}`); // Debug
133
+ }
134
+ else {
135
+ // Ensure no phantom key remains
136
+ delete config.apiKey;
137
+ if (config.mode === 'pro')
138
+ config.mode = 'manual';
111
139
  }
112
140
  return { ...config, version: PKG_VERSION };
113
141
  }
@@ -9,5 +9,5 @@ interface OpenCodeModel {
9
9
  output?: number;
10
10
  };
11
11
  }
12
- export declare function generatePollinationsConfig(forceApiKey?: string): Promise<OpenCodeModel[]>;
12
+ export declare function generatePollinationsConfig(forceApiKey?: string, forceStrict?: boolean): Promise<OpenCodeModel[]>;
13
13
  export {};
@@ -54,7 +54,7 @@ function formatName(id, censored = true) {
54
54
  }
55
55
  // --- MAIN GENERATOR logic ---
56
56
  // --- MAIN GENERATOR logic ---
57
- export async function generatePollinationsConfig(forceApiKey) {
57
+ export async function generatePollinationsConfig(forceApiKey, forceStrict = false) {
58
58
  const config = loadConfig();
59
59
  const modelsOutput = [];
60
60
  log(`Starting Configuration (V5.1.22 Hot-Reload)...`);
@@ -122,6 +122,9 @@ export async function generatePollinationsConfig(forceApiKey) {
122
122
  }
123
123
  catch (e) {
124
124
  log(`Error fetching Enterprise models: ${e}`);
125
+ // STRICT MODE (Validation): Do not return fake fallback models.
126
+ if (forceStrict)
127
+ throw e;
125
128
  // Fallback Robust for Enterprise (User has Key but discovery failed)
126
129
  modelsOutput.push({ id: "enter/gpt-4o", name: "[Enter] GPT-4o (Fallback)", object: "model", variants: {} });
127
130
  // ...
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "opencode-pollinations-plugin",
3
3
  "displayName": "Pollinations AI (V5.1)",
4
- "version": "5.4.12",
4
+ "version": "5.4.14",
5
5
  "description": "Native Pollinations.ai Provider Plugin for OpenCode",
6
6
  "publisher": "pollinations",
7
7
  "repository": {