gencow 0.1.288 → 0.1.289
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/lib/opencode-command.mjs +11 -3
- package/lib/opencode-config.mjs +26 -4
- package/package.json +3 -3
- package/runtime/server.mjs +103 -102
package/lib/opencode-command.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
1
|
+
import { chmodSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { homedir, hostname } from "node:os";
|
|
3
3
|
import { dirname, join } from "node:path";
|
|
4
4
|
import { BOLD, CYAN, DIM, RESET, error, info, log, success } from "./output.mjs";
|
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
applyOpencodeEdits,
|
|
10
10
|
hasGencowKey,
|
|
11
11
|
hasGencowProvider,
|
|
12
|
+
hasKeyFileRef,
|
|
12
13
|
planOpencodeEdits,
|
|
13
14
|
resolveOpencodePaths,
|
|
14
15
|
} from "./opencode-config.mjs";
|
|
@@ -66,7 +67,8 @@ export function createOpencodeCommands(overrides = {}) {
|
|
|
66
67
|
}
|
|
67
68
|
function writeState(state) {
|
|
68
69
|
mkdirSync(dirname(statePath()), { recursive: true });
|
|
69
|
-
writeFileSync(statePath(), `${JSON.stringify(state, null, 2)}\n
|
|
70
|
+
writeFileSync(statePath(), `${JSON.stringify(state, null, 2)}\n`, { mode: 0o600 });
|
|
71
|
+
if (process.platform !== "win32") chmodSync(statePath(), 0o600); // an older 644 file keeps its mode on rewrite
|
|
70
72
|
}
|
|
71
73
|
|
|
72
74
|
async function verifyKey(baseURL, key) {
|
|
@@ -135,7 +137,13 @@ export function createOpencodeCommands(overrides = {}) {
|
|
|
135
137
|
const sameAccount = state && state.userId === creds.userId && state.platformUrl === creds.platformUrl;
|
|
136
138
|
const fresh = sameAccount && existingKey && hasGencowProvider(paths) && Date.parse(state.expiresAt) - deps.nowImpl() > RENEW_WITHIN_MS;
|
|
137
139
|
if (fresh && !flags.force && (await verifyKey(baseURL, existingKey)) === 200) {
|
|
138
|
-
|
|
140
|
+
if (hasKeyFileRef(paths)) {
|
|
141
|
+
return { ok: true, code: "ALREADY_CONFIGURED", changed: false, model: `gencow/${GENCOW_MODEL_ID}`, expiresAt: state.expiresAt };
|
|
142
|
+
}
|
|
143
|
+
// A setup from an older CLI wrote only auth.json, which OpenCode Desktop does not read. The token is still
|
|
144
|
+
// valid, so add the key file and the config reference without minting or revoking anything.
|
|
145
|
+
applyOpencodeEdits({ paths, plan: planOpencodeEdits({ paths, baseURL, apiKey: existingKey }) });
|
|
146
|
+
return { ok: true, code: "CONFIGURED", changed: true, repaired: true, model: `gencow/${GENCOW_MODEL_ID}`, expiresAt: state.expiresAt };
|
|
139
147
|
}
|
|
140
148
|
|
|
141
149
|
// Refuse before minting when the OpenCode files cannot be edited safely, so no unused token is created.
|
package/lib/opencode-config.mjs
CHANGED
|
@@ -11,6 +11,12 @@ import { dirname, join } from "node:path";
|
|
|
11
11
|
*/
|
|
12
12
|
export const GENCOW_PROVIDER_ID = "gencow";
|
|
13
13
|
export const GENCOW_MODEL_ID = "llm/code";
|
|
14
|
+
/**
|
|
15
|
+
* The key lives in a private file that the OpenCode config references with `{file:...}`. OpenCode Desktop keeps
|
|
16
|
+
* its own credential store (opencode.db) and ignores auth.json, so a config reference is the one channel both
|
|
17
|
+
* Desktop and the CLI read (verified: Desktop on macOS, CLI 1.19.5 and a current dev build).
|
|
18
|
+
*/
|
|
19
|
+
export const GENCOW_KEY_FILE_REF = "{file:~/.gencow/opencode.key}";
|
|
14
20
|
|
|
15
21
|
export class OpencodeSetupError extends Error {
|
|
16
22
|
constructor(code, message, nextAction) {
|
|
@@ -31,6 +37,7 @@ export function resolveOpencodePaths({ env = process.env, home } = {}) {
|
|
|
31
37
|
configJson: join(configDir, "opencode.json"),
|
|
32
38
|
configJsonc: join(configDir, "opencode.jsonc"),
|
|
33
39
|
authJson: join(dataHome, "opencode", "auth.json"),
|
|
40
|
+
keyFile: join(home, ".gencow", "opencode.key"),
|
|
34
41
|
};
|
|
35
42
|
}
|
|
36
43
|
|
|
@@ -121,7 +128,7 @@ export function gencowProviderConfig(baseURL) {
|
|
|
121
128
|
return {
|
|
122
129
|
npm: "@ai-sdk/openai-compatible",
|
|
123
130
|
name: "Gencow",
|
|
124
|
-
options: { baseURL },
|
|
131
|
+
options: { baseURL, apiKey: GENCOW_KEY_FILE_REF },
|
|
125
132
|
models: { [GENCOW_MODEL_ID]: { name: "Gencow Code" } },
|
|
126
133
|
};
|
|
127
134
|
}
|
|
@@ -179,6 +186,8 @@ export function planOpencodeEdits({ paths, baseURL, apiKey }) {
|
|
|
179
186
|
return {
|
|
180
187
|
configText,
|
|
181
188
|
authText,
|
|
189
|
+
keyFileText: readText(paths.keyFile),
|
|
190
|
+
nextKeyFileText: apiKey,
|
|
182
191
|
nextConfigText: `${JSON.stringify(nextConfig, null, 2)}\n`,
|
|
183
192
|
nextAuthText: `${JSON.stringify(nextAuth, null, 2)}\n`,
|
|
184
193
|
keptUserModel: userHasModel,
|
|
@@ -196,11 +205,17 @@ export function applyOpencodeEdits({ paths, plan }) {
|
|
|
196
205
|
if (plan.configText !== null && !existsSync(`${paths.configJson}.bak`)) {
|
|
197
206
|
writeFileSync(`${paths.configJson}.bak`, plan.configText, { mode: 0o600 });
|
|
198
207
|
}
|
|
199
|
-
atomicWrite(paths.
|
|
208
|
+
atomicWrite(paths.keyFile, plan.nextKeyFileText, 0o600);
|
|
200
209
|
try {
|
|
201
|
-
atomicWrite(paths.
|
|
210
|
+
atomicWrite(paths.configJson, plan.nextConfigText);
|
|
211
|
+
try {
|
|
212
|
+
atomicWrite(paths.authJson, plan.nextAuthText, 0o600);
|
|
213
|
+
} catch (error) {
|
|
214
|
+
restore(paths.configJson, plan.configText);
|
|
215
|
+
throw error;
|
|
216
|
+
}
|
|
202
217
|
} catch (error) {
|
|
203
|
-
restore(paths.
|
|
218
|
+
restore(paths.keyFile, plan.keyFileText);
|
|
204
219
|
throw error;
|
|
205
220
|
}
|
|
206
221
|
} catch (error) {
|
|
@@ -221,6 +236,13 @@ export function hasGencowProvider(paths) {
|
|
|
221
236
|
});
|
|
222
237
|
}
|
|
223
238
|
|
|
239
|
+
/** True when the provider block already points at the key file and the key file exists (Desktop-readable setup). */
|
|
240
|
+
export function hasKeyFileRef(paths) {
|
|
241
|
+
const text = readText(paths.configJson);
|
|
242
|
+
const ref = text ? parseJsonc(text)?.value?.provider?.[GENCOW_PROVIDER_ID]?.options?.apiKey : null;
|
|
243
|
+
return ref === GENCOW_KEY_FILE_REF && Boolean(readText(paths.keyFile)?.trim());
|
|
244
|
+
}
|
|
245
|
+
|
|
224
246
|
export function hasGencowKey(authJsonPath) {
|
|
225
247
|
const text = readText(authJsonPath);
|
|
226
248
|
const parsed = text ? parseJsonc(text) : null;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gencow",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.289",
|
|
4
4
|
"description": "Gencow — AI Backend Engine",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -42,9 +42,9 @@
|
|
|
42
42
|
"postgres": "^3.4.9",
|
|
43
43
|
"@gencow/client": "0.2.8",
|
|
44
44
|
"@gencow/core": "0.1.48",
|
|
45
|
+
"@gencow/react": "0.2.8",
|
|
45
46
|
"@gencow/shared": "0.0.0",
|
|
46
|
-
"@gencow/server": "0.1.3"
|
|
47
|
-
"@gencow/react": "0.2.8"
|
|
47
|
+
"@gencow/server": "0.1.3"
|
|
48
48
|
},
|
|
49
49
|
"scripts": {
|
|
50
50
|
"build": "node scripts/bundle-core.mjs && node scripts/bundle-shared.mjs && node scripts/bundle-server.mjs",
|