@jmanuelcorral/bond-app 0.1.4 → 0.1.6
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 +241 -7
- package/app/brand/TOKENS.md +5 -5
- package/app/layouts/bond.json +10 -14
- package/app/mods/bond-agents/api.js +35 -0
- package/app/mods/bond-agents/branch.svg +1 -0
- package/app/mods/bond-agents/brand.js +53 -2
- package/app/mods/bond-agents/config.js +8 -0
- package/app/mods/bond-agents/eye-off.svg +1 -0
- package/app/mods/bond-agents/eye.svg +1 -0
- package/app/mods/bond-agents/main.js +316 -330
- package/app/mods/bond-agents/model.js +400 -6
- package/app/mods/bond-board/api.js +35 -0
- package/app/mods/bond-board/branch.svg +1 -0
- package/app/mods/bond-board/brand.js +53 -2
- package/app/mods/bond-board/config.js +8 -0
- package/app/mods/bond-board/main.js +1054 -366
- package/app/mods/bond-board/model.js +138 -17
- package/app/mods/bond-pane/close.svg +1 -0
- package/app/mods/bond-pane/hide.svg +1 -0
- package/app/mods/bond-pane/main.js +103 -0
- package/app/mods/bond-pane/mod.json +1 -0
- package/app/mods/bond-pane/model.js +18 -0
- package/app/mods/bond-pane/split-down.svg +1 -0
- package/app/mods/bond-pane/split-right.svg +1 -0
- package/app/mods/bond-pane/unzoom.svg +1 -0
- package/app/mods/bond-pane/zoom.svg +1 -0
- package/app/mods/bond-settings/api.js +35 -0
- package/app/mods/bond-settings/brand.js +53 -2
- package/app/mods/bond-settings/config.js +8 -0
- package/app/mods/bond-settings/main.js +1645 -87
- package/app/mods/bond-settings/mod.json +1 -1
- package/app/mods/bond-settings/pi-models.js +144 -0
- package/app/mods/bond-status/api.js +35 -0
- package/app/mods/bond-status/brand.js +53 -2
- package/app/mods/bond-status/config.js +8 -0
- package/app/mods/bond-status/main.js +37 -15
- package/app/mods/bond-status/model.js +19 -1
- package/app/mods/bond-tabs/brand.js +53 -2
- package/app/mods/bond-tabs/main.js +45 -3
- package/app/mods/bond-titlebar/brand.js +53 -2
- package/app/mods/bond-titlebar/main.js +115 -55
- package/app/mods/bond-titlebar/version.js +3 -0
- package/app/themes/bond-dark.json +5 -5
- package/app/themes/bond-light.json +5 -5
- package/package.json +1 -1
- package/runner/Dockerfile +58 -15
- package/runner/SPIKE-BOND.md +89 -0
- package/runner/build-config/.none +0 -0
- package/runner/build-creds/.none +0 -0
- package/runner/config.example/README.md +27 -0
- package/runner/config.example/pi/models.json +22 -0
- package/runner/credentials.example.env +27 -0
- package/runner/entrypoint.sh +16 -0
- package/runner/setup.sh +927 -0
- package/runner/versions.env +61 -0
- package/scripts/app-install.ts +64 -1
- package/src/cli.ts +53 -3
- package/src/config.ts +8 -0
- package/src/core/agy-login.ts +682 -0
- package/src/core/delivery.ts +3 -1
- package/src/core/hub-session.ts +9 -7
- package/src/core/orchestrator.ts +1186 -168
- package/src/core/repo-host.ts +60 -0
- package/src/core/setup.ts +239 -0
- package/src/core/watchdog.ts +6 -6
- package/src/db/database.ts +55 -1
- package/src/db/settings-repo.ts +440 -7
- package/src/db/ticket-repo.ts +73 -17
- package/src/index.ts +4 -4
- package/src/runner/azure-devops-client.ts +163 -0
- package/src/runner/docker-client.ts +84 -5
- package/src/runner/github-client.ts +146 -0
- package/src/runner/hc-client.ts +40 -5
- package/src/runner/image-builder.ts +268 -10
- package/src/runner/jira-client.ts +368 -0
- package/src/runner/tool-catalog.ts +84 -0
- package/src/server/app.ts +585 -9
- package/src/types.ts +168 -1
package/src/db/settings-repo.ts
CHANGED
|
@@ -1,19 +1,128 @@
|
|
|
1
1
|
import type { DatabaseSync } from 'node:sqlite';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
2
4
|
import {
|
|
3
5
|
SUPPORTED_AGENTS,
|
|
4
6
|
isValidAgent,
|
|
5
7
|
validateGitHubLogin,
|
|
6
8
|
validateGitHubToken,
|
|
9
|
+
validateJiraSite,
|
|
10
|
+
normalizeJiraSite,
|
|
11
|
+
validateJiraEmail,
|
|
12
|
+
validateJiraToken,
|
|
7
13
|
type AgentCredential,
|
|
8
14
|
type AgentCredentialSummary,
|
|
9
15
|
type GitHubCredentialSummary,
|
|
16
|
+
type AzureDevOpsCredentialSummary,
|
|
17
|
+
type JiraCredential,
|
|
18
|
+
type JiraCredentialSummary,
|
|
10
19
|
} from '../types.ts';
|
|
20
|
+
import { APT_PACKAGE_RE, DEFAULT_TOOLS, TOOL_IDS } from '../runner/tool-catalog.ts';
|
|
21
|
+
|
|
22
|
+
/** A `claude setup-token` token lasts one year from when it was made (stored). */
|
|
23
|
+
function withExpiry(summary: AgentCredentialSummary): AgentCredentialSummary {
|
|
24
|
+
if (summary.agent !== 'claude' || summary.provider !== 'setup-token' || !summary.updated_at) return summary;
|
|
25
|
+
const ends = new Date(summary.updated_at);
|
|
26
|
+
if (Number.isNaN(ends.getTime())) return summary;
|
|
27
|
+
ends.setFullYear(ends.getFullYear() + 1);
|
|
28
|
+
return { ...summary, expires_at: ends.toISOString() };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* The account a login credential belongs to, for Ajustes (never the token): codex's ChatGPT
|
|
33
|
+
* email (from the id_token's claims) and copilot's GitHub login. Null when unreadable.
|
|
34
|
+
*/
|
|
35
|
+
export function credentialAccount(agent: string, config: string | null | undefined): string | undefined {
|
|
36
|
+
if (!config) return undefined;
|
|
37
|
+
try {
|
|
38
|
+
if (agent === 'codex') {
|
|
39
|
+
const doc = JSON.parse(config) as { tokens?: { id_token?: string } };
|
|
40
|
+
const payload = doc.tokens?.id_token?.split('.')[1];
|
|
41
|
+
if (!payload) return undefined;
|
|
42
|
+
const claims = JSON.parse(Buffer.from(payload, 'base64url').toString('utf8')) as Record<string, unknown>;
|
|
43
|
+
const profile = claims['https://api.openai.com/profile'] as { email?: unknown } | undefined;
|
|
44
|
+
const email = typeof claims.email === 'string' ? claims.email : profile?.email;
|
|
45
|
+
return typeof email === 'string' ? email : undefined;
|
|
46
|
+
}
|
|
47
|
+
if (agent === 'copilot') {
|
|
48
|
+
// copilot's config.json starts with // comment lines: not strict JSON.
|
|
49
|
+
const doc = JSON.parse(config.replace(/^\s*\/\/.*$/gm, '')) as { lastLoggedInUser?: { login?: unknown } };
|
|
50
|
+
const login = doc.lastLoggedInUser?.login;
|
|
51
|
+
return typeof login === 'string' ? login : undefined;
|
|
52
|
+
}
|
|
53
|
+
} catch {
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* pi's models.json with every provider keyed: Ajustes never shows the saved key, so a provider
|
|
61
|
+
* saved without one keeps the key it already had (same provider name), and one that never had
|
|
62
|
+
* any gets "none" (pi refuses a provider without apiKey even when the server does not check).
|
|
63
|
+
* Anything that is not a pi models document is returned untouched.
|
|
64
|
+
*/
|
|
65
|
+
export function keepPiKeys(previous: string | null | undefined, next: string): string {
|
|
66
|
+
let doc: { providers?: Record<string, Record<string, unknown>> };
|
|
67
|
+
try {
|
|
68
|
+
doc = JSON.parse(next);
|
|
69
|
+
} catch {
|
|
70
|
+
return next;
|
|
71
|
+
}
|
|
72
|
+
if (!doc?.providers || typeof doc.providers !== 'object') return next;
|
|
73
|
+
let old: Record<string, Record<string, unknown>> = {};
|
|
74
|
+
try {
|
|
75
|
+
old = (JSON.parse(previous ?? '{}') as { providers?: Record<string, Record<string, unknown>> }).providers ?? {};
|
|
76
|
+
} catch {}
|
|
77
|
+
let changed = false;
|
|
78
|
+
for (const [name, entry] of Object.entries(doc.providers)) {
|
|
79
|
+
if (!entry || typeof entry !== 'object' || (typeof entry.apiKey === 'string' && entry.apiKey.trim())) continue;
|
|
80
|
+
const kept = old[name]?.apiKey;
|
|
81
|
+
entry.apiKey = typeof kept === 'string' && kept.trim() ? kept : 'none';
|
|
82
|
+
changed = true;
|
|
83
|
+
}
|
|
84
|
+
return changed ? JSON.stringify(doc, null, 2) : next;
|
|
85
|
+
}
|
|
11
86
|
|
|
12
87
|
export class SettingsRepository {
|
|
13
88
|
private db: DatabaseSync;
|
|
89
|
+
private dataDir?: string;
|
|
14
90
|
|
|
15
|
-
constructor(db: DatabaseSync) {
|
|
91
|
+
constructor(db: DatabaseSync, dataDir?: string) {
|
|
16
92
|
this.db = db;
|
|
93
|
+
this.dataDir = dataDir;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
setDataDir(dataDir: string): void {
|
|
97
|
+
this.dataDir = dataDir;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private getAgySummary(): AgentCredentialSummary | null {
|
|
101
|
+
if (!this.dataDir || this.dataDir === ':memory:') return null;
|
|
102
|
+
const tokenPath = path.join(this.dataDir, 'antigravity-oauth-token');
|
|
103
|
+
try {
|
|
104
|
+
if (!fs.existsSync(tokenPath)) {
|
|
105
|
+
return {
|
|
106
|
+
agent: 'agy',
|
|
107
|
+
configured: false,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
const stat = fs.statSync(tokenPath);
|
|
111
|
+
const content = fs.readFileSync(tokenPath, 'utf8');
|
|
112
|
+
const parsed = JSON.parse(content);
|
|
113
|
+
const email = typeof parsed?.id_token_email === 'string' ? parsed.id_token_email : undefined;
|
|
114
|
+
return {
|
|
115
|
+
agent: 'agy',
|
|
116
|
+
configured: true,
|
|
117
|
+
email,
|
|
118
|
+
updated_at: stat.mtime.toISOString(),
|
|
119
|
+
};
|
|
120
|
+
} catch {
|
|
121
|
+
return {
|
|
122
|
+
agent: 'agy',
|
|
123
|
+
configured: false,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
17
126
|
}
|
|
18
127
|
|
|
19
128
|
setCredential(agent: string, configJson: string, provider?: string): AgentCredential {
|
|
@@ -32,6 +141,16 @@ export class SettingsRepository {
|
|
|
32
141
|
`);
|
|
33
142
|
|
|
34
143
|
stmt.run(agent, provider || null, configJson, now);
|
|
144
|
+
|
|
145
|
+
if (agent === 'agy' && this.dataDir && this.dataDir !== ':memory:') {
|
|
146
|
+
const tokenPath = path.join(this.dataDir, 'antigravity-oauth-token');
|
|
147
|
+
try {
|
|
148
|
+
fs.mkdirSync(this.dataDir, { recursive: true, mode: 0o700 });
|
|
149
|
+
fs.writeFileSync(tokenPath, configJson, { mode: 0o600 });
|
|
150
|
+
fs.chmodSync(tokenPath, 0o600);
|
|
151
|
+
} catch {}
|
|
152
|
+
}
|
|
153
|
+
|
|
35
154
|
return {
|
|
36
155
|
agent,
|
|
37
156
|
provider,
|
|
@@ -41,6 +160,20 @@ export class SettingsRepository {
|
|
|
41
160
|
}
|
|
42
161
|
|
|
43
162
|
getCredential(agent: string): AgentCredential | null {
|
|
163
|
+
if (agent === 'agy' && this.dataDir && this.dataDir !== ':memory:') {
|
|
164
|
+
const tokenPath = path.join(this.dataDir, 'antigravity-oauth-token');
|
|
165
|
+
try {
|
|
166
|
+
if (fs.existsSync(tokenPath)) {
|
|
167
|
+
const content = fs.readFileSync(tokenPath, 'utf8');
|
|
168
|
+
const stat = fs.statSync(tokenPath);
|
|
169
|
+
return {
|
|
170
|
+
agent: 'agy',
|
|
171
|
+
config_json: content,
|
|
172
|
+
updated_at: stat.mtime.toISOString(),
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
} catch {}
|
|
176
|
+
}
|
|
44
177
|
const stmt = this.db.prepare('SELECT * FROM agent_credentials WHERE agent = ?');
|
|
45
178
|
const row = stmt.get(agent) as unknown as Record<string, unknown> | undefined;
|
|
46
179
|
if (!row) return null;
|
|
@@ -53,38 +186,63 @@ export class SettingsRepository {
|
|
|
53
186
|
}
|
|
54
187
|
|
|
55
188
|
getSummary(agent: string): AgentCredentialSummary {
|
|
189
|
+
if (agent === 'agy' && this.dataDir && this.dataDir !== ':memory:') {
|
|
190
|
+
const agySummary = this.getAgySummary();
|
|
191
|
+
if (agySummary) return agySummary;
|
|
192
|
+
}
|
|
56
193
|
const cred = this.getCredential(agent);
|
|
57
|
-
|
|
194
|
+
const account = credentialAccount(agent, cred?.config_json);
|
|
195
|
+
return withExpiry({
|
|
58
196
|
agent,
|
|
59
197
|
provider: cred?.provider,
|
|
60
198
|
configured: Boolean(cred),
|
|
61
199
|
updated_at: cred?.updated_at,
|
|
62
|
-
|
|
200
|
+
...(account ? { email: account } : {}),
|
|
201
|
+
});
|
|
63
202
|
}
|
|
64
203
|
|
|
65
204
|
listSummaries(): AgentCredentialSummary[] {
|
|
66
|
-
const
|
|
205
|
+
const agySummary = this.dataDir && this.dataDir !== ':memory:' ? this.getAgySummary() : null;
|
|
206
|
+
const stmt = this.db.prepare('SELECT agent, provider, config_json, updated_at FROM agent_credentials');
|
|
67
207
|
const rows = stmt.all() as unknown as Array<Record<string, unknown>>;
|
|
68
|
-
const configuredMap = new Map<string, { provider?: string; updated_at: string }>();
|
|
208
|
+
const configuredMap = new Map<string, { provider?: string; updated_at: string; email?: string }>();
|
|
69
209
|
|
|
70
210
|
for (const row of rows) {
|
|
71
211
|
configuredMap.set(String(row.agent), {
|
|
72
212
|
provider: row.provider ? String(row.provider) : undefined,
|
|
73
213
|
updated_at: String(row.updated_at),
|
|
214
|
+
email: credentialAccount(String(row.agent), row.config_json ? String(row.config_json) : null),
|
|
74
215
|
});
|
|
75
216
|
}
|
|
76
217
|
|
|
77
218
|
return SUPPORTED_AGENTS.map((agent) => {
|
|
219
|
+
if (agent === 'agy' && agySummary) {
|
|
220
|
+
return agySummary;
|
|
221
|
+
}
|
|
78
222
|
const info = configuredMap.get(agent);
|
|
79
|
-
return {
|
|
223
|
+
return withExpiry({
|
|
80
224
|
agent,
|
|
81
225
|
provider: info?.provider,
|
|
82
226
|
configured: Boolean(info),
|
|
83
227
|
updated_at: info?.updated_at,
|
|
84
|
-
|
|
228
|
+
...(info?.email ? { email: info.email } : {}),
|
|
229
|
+
});
|
|
85
230
|
});
|
|
86
231
|
}
|
|
87
232
|
|
|
233
|
+
deleteCredential(agent: string): boolean {
|
|
234
|
+
if (agent === 'agy' && this.dataDir && this.dataDir !== ':memory:') {
|
|
235
|
+
const tokenPath = path.join(this.dataDir, 'antigravity-oauth-token');
|
|
236
|
+
try {
|
|
237
|
+
if (fs.existsSync(tokenPath)) {
|
|
238
|
+
fs.unlinkSync(tokenPath);
|
|
239
|
+
}
|
|
240
|
+
} catch {}
|
|
241
|
+
}
|
|
242
|
+
const res = this.db.prepare('DELETE FROM agent_credentials WHERE agent = ?').run(agent);
|
|
243
|
+
return res.changes > 0;
|
|
244
|
+
}
|
|
245
|
+
|
|
88
246
|
/** D20: store (or replace) the GitHub credential taken from the user's gh session. */
|
|
89
247
|
setGitHubCredential(token: string, login?: string | null): GitHubCredentialSummary {
|
|
90
248
|
if (!validateGitHubToken(token)) {
|
|
@@ -124,4 +282,279 @@ export class SettingsRepository {
|
|
|
124
282
|
deleteGitHubCredential(): boolean {
|
|
125
283
|
return this.db.prepare('DELETE FROM github_credential WHERE id = 1').run().changes > 0;
|
|
126
284
|
}
|
|
285
|
+
|
|
286
|
+
// --- Alta de ticket: repos favoritos (★) -------------------------------------------------
|
|
287
|
+
|
|
288
|
+
/** The favorite repos, in the order they were added (clone URLs, GitHub or Azure). */
|
|
289
|
+
getFavoriteRepos(): string[] {
|
|
290
|
+
try {
|
|
291
|
+
const parsed = JSON.parse(this.getSetting('favorite_repos') ?? '[]');
|
|
292
|
+
return Array.isArray(parsed) ? parsed.map((u) => String(u)).filter(Boolean) : [];
|
|
293
|
+
} catch {
|
|
294
|
+
return [];
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
setFavoriteRepos(urls: string[]): string[] {
|
|
299
|
+
const clean = [...new Set(urls.map((u) => String(u).trim()).filter(Boolean))].slice(0, 50);
|
|
300
|
+
this.setSetting('favorite_repos', JSON.stringify(clean));
|
|
301
|
+
return clean;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// --- Plan bond-app-azure-jira P1: Azure DevOps (Azure Repos) ---------------------------
|
|
305
|
+
|
|
306
|
+
setAzureDevOpsCredential(orgUrl: string, pat: string, account: string | null, agentsEnabled: boolean): AzureDevOpsCredentialSummary {
|
|
307
|
+
if (!/^https:\/\/dev\.azure\.com\/[A-Za-z0-9][A-Za-z0-9_.-]{0,99}$/.test(orgUrl)) throw new Error('Invalid Azure DevOps organization URL');
|
|
308
|
+
if (!/^[A-Za-z0-9]{20,200}$/.test(pat)) throw new Error('Invalid Azure DevOps PAT format');
|
|
309
|
+
const now = new Date().toISOString();
|
|
310
|
+
this.db.prepare(`
|
|
311
|
+
INSERT INTO azure_devops_credential (id, org_url, pat, account, agents_enabled, updated_at) VALUES (1, ?, ?, ?, ?, ?)
|
|
312
|
+
ON CONFLICT(id) DO UPDATE SET org_url = excluded.org_url, pat = excluded.pat, account = excluded.account,
|
|
313
|
+
agents_enabled = excluded.agents_enabled, updated_at = excluded.updated_at
|
|
314
|
+
`).run(orgUrl, pat, account, agentsEnabled ? 1 : 0, now);
|
|
315
|
+
return this.getAzureDevOpsSummary();
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/** Only the «available to the agents» switch, keeping the PAT. */
|
|
319
|
+
setAzureDevOpsAgentsEnabled(enabled: boolean): AzureDevOpsCredentialSummary {
|
|
320
|
+
this.db.prepare('UPDATE azure_devops_credential SET agents_enabled = ? WHERE id = 1').run(enabled ? 1 : 0);
|
|
321
|
+
return this.getAzureDevOpsSummary();
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/** For the orchestrator only: clone/push (stdin), PR calls (header), agent env (0600 file). */
|
|
325
|
+
getAzureDevOpsCredential(): { org_url: string; pat: string; agents_enabled: boolean } | null {
|
|
326
|
+
const row = this.db.prepare('SELECT org_url, pat, agents_enabled FROM azure_devops_credential WHERE id = 1').get() as
|
|
327
|
+
| { org_url?: string; pat?: string; agents_enabled?: number }
|
|
328
|
+
| undefined;
|
|
329
|
+
if (!row?.pat || !row.org_url) return null;
|
|
330
|
+
return { org_url: String(row.org_url), pat: String(row.pat), agents_enabled: Number(row.agents_enabled ?? 1) !== 0 };
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
getAzureDevOpsSummary(): AzureDevOpsCredentialSummary {
|
|
334
|
+
const row = this.db.prepare('SELECT org_url, account, agents_enabled, updated_at FROM azure_devops_credential WHERE id = 1').get() as
|
|
335
|
+
| { org_url?: string; account?: string | null; agents_enabled?: number; updated_at?: string }
|
|
336
|
+
| undefined;
|
|
337
|
+
if (!row) return { configured: false };
|
|
338
|
+
return {
|
|
339
|
+
configured: true,
|
|
340
|
+
org_url: String(row.org_url),
|
|
341
|
+
...(row.account ? { account: String(row.account) } : {}),
|
|
342
|
+
agents_enabled: Number(row.agents_enabled ?? 1) !== 0,
|
|
343
|
+
updated_at: row.updated_at ? String(row.updated_at) : undefined,
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
deleteAzureDevOpsCredential(): boolean {
|
|
348
|
+
return this.db.prepare('DELETE FROM azure_devops_credential WHERE id = 1').run().changes > 0;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// --- Plan bond-app-azure-jira P2: Jira Cloud credentials ---------------------------
|
|
352
|
+
|
|
353
|
+
setJiraCredential(
|
|
354
|
+
site: string,
|
|
355
|
+
email: string,
|
|
356
|
+
token: string,
|
|
357
|
+
agentsEnabled = true,
|
|
358
|
+
accountName?: string | null
|
|
359
|
+
): JiraCredentialSummary {
|
|
360
|
+
if (!validateJiraSite(site)) {
|
|
361
|
+
throw new Error("Invalid Jira site URL (must be https://<name>.atlassian.net)");
|
|
362
|
+
}
|
|
363
|
+
if (!validateJiraEmail(email)) {
|
|
364
|
+
throw new Error("Invalid Jira email address");
|
|
365
|
+
}
|
|
366
|
+
if (!validateJiraToken(token)) {
|
|
367
|
+
throw new Error("Invalid Jira API token");
|
|
368
|
+
}
|
|
369
|
+
const normalizedSite = normalizeJiraSite(site);
|
|
370
|
+
const now = new Date().toISOString();
|
|
371
|
+
this.db.prepare(`
|
|
372
|
+
INSERT INTO jira_credential (id, site, email, token, agents_enabled, account_name, updated_at)
|
|
373
|
+
VALUES (1, ?, ?, ?, ?, ?, ?)
|
|
374
|
+
ON CONFLICT(id) DO UPDATE SET
|
|
375
|
+
site = excluded.site,
|
|
376
|
+
email = excluded.email,
|
|
377
|
+
token = excluded.token,
|
|
378
|
+
agents_enabled = excluded.agents_enabled,
|
|
379
|
+
account_name = excluded.account_name,
|
|
380
|
+
updated_at = excluded.updated_at
|
|
381
|
+
`).run(normalizedSite, email.trim(), token.trim(), agentsEnabled ? 1 : 0, accountName ?? null, now);
|
|
382
|
+
return this.getJiraSummary();
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
getJiraCredential(): JiraCredential | null {
|
|
386
|
+
const row = this.db.prepare('SELECT site, email, token, agents_enabled, account_name, updated_at FROM jira_credential WHERE id = 1').get() as
|
|
387
|
+
| { site: string; email: string; token: string; agents_enabled: number; account_name?: string | null; updated_at: string }
|
|
388
|
+
| undefined;
|
|
389
|
+
if (!row) return null;
|
|
390
|
+
return {
|
|
391
|
+
site: String(row.site),
|
|
392
|
+
email: String(row.email),
|
|
393
|
+
token: String(row.token),
|
|
394
|
+
agents_enabled: Boolean(row.agents_enabled),
|
|
395
|
+
account_name: row.account_name ? String(row.account_name) : null,
|
|
396
|
+
updated_at: String(row.updated_at),
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
getJiraSummary(): JiraCredentialSummary {
|
|
401
|
+
const row = this.db.prepare('SELECT site, email, agents_enabled, account_name, updated_at FROM jira_credential WHERE id = 1').get() as
|
|
402
|
+
| { site: string; email: string; agents_enabled: number; account_name?: string | null; updated_at: string }
|
|
403
|
+
| undefined;
|
|
404
|
+
if (!row) return { configured: false };
|
|
405
|
+
return {
|
|
406
|
+
configured: true,
|
|
407
|
+
site: String(row.site),
|
|
408
|
+
email: String(row.email),
|
|
409
|
+
agents_enabled: Boolean(row.agents_enabled),
|
|
410
|
+
account_name: row.account_name ? String(row.account_name) : null,
|
|
411
|
+
updated_at: String(row.updated_at),
|
|
412
|
+
};
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
deleteJiraCredential(): boolean {
|
|
416
|
+
return this.db.prepare('DELETE FROM jira_credential WHERE id = 1').run().changes > 0;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// --- W-P1: setup state (wizard) -----------------------------------------------------
|
|
420
|
+
// A small key/value store of the orchestrator's own configuration state. Only the
|
|
421
|
+
// selection and the setup_done flag live here; credentials stay in their own tables.
|
|
422
|
+
|
|
423
|
+
getSetting(key: string): string | null {
|
|
424
|
+
const row = this.db.prepare('SELECT value FROM app_settings WHERE key = ?').get(key) as
|
|
425
|
+
| { value?: string }
|
|
426
|
+
| undefined;
|
|
427
|
+
return row?.value !== undefined ? String(row.value) : null;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
setSetting(key: string, value: string): void {
|
|
431
|
+
const now = new Date().toISOString();
|
|
432
|
+
this.db.prepare(`
|
|
433
|
+
INSERT INTO app_settings (key, value, updated_at) VALUES (?, ?, ?)
|
|
434
|
+
ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated_at = excluded.updated_at
|
|
435
|
+
`).run(key, value, now);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/** The stored wizard state; unset means nothing selected and the wizard has not run. */
|
|
439
|
+
getSetupSelection(): {
|
|
440
|
+
setup_done: boolean;
|
|
441
|
+
agents_selected: string[];
|
|
442
|
+
suggestions_model: string | null;
|
|
443
|
+
default_model: string | null;
|
|
444
|
+
models: string[];
|
|
445
|
+
tools_selected: string[];
|
|
446
|
+
apt_extra: string[];
|
|
447
|
+
} {
|
|
448
|
+
const setupDone = this.getSetting('setup_done') === 'true';
|
|
449
|
+
let agentsSelected: string[] = [];
|
|
450
|
+
const raw = this.getSetting('agents_selected');
|
|
451
|
+
if (raw !== null) {
|
|
452
|
+
try {
|
|
453
|
+
const parsed = JSON.parse(raw);
|
|
454
|
+
if (Array.isArray(parsed)) agentsSelected = parsed.map((a) => String(a));
|
|
455
|
+
} catch {
|
|
456
|
+
// a corrupted value reads as "nothing selected"; PUT overwrites it.
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
// W-P4: the supi suggestions model; anything unreadable reads as «sin sugerencias».
|
|
460
|
+
let suggestionsModel: string | null = null;
|
|
461
|
+
const rawSug = this.getSetting('suggestions_model');
|
|
462
|
+
if (rawSug !== null) {
|
|
463
|
+
try {
|
|
464
|
+
const parsed = JSON.parse(rawSug);
|
|
465
|
+
if (typeof parsed === 'string' && parsed) suggestionsModel = parsed;
|
|
466
|
+
} catch {
|
|
467
|
+
// corrupt: same as unset; PUT overwrites it.
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
// D-M1: the default working model for pi.
|
|
471
|
+
let defaultModel: string | null = null;
|
|
472
|
+
const rawDef = this.getSetting('default_model');
|
|
473
|
+
if (rawDef !== null) {
|
|
474
|
+
try {
|
|
475
|
+
const parsed = JSON.parse(rawDef);
|
|
476
|
+
if (typeof parsed === 'string' && parsed) defaultModel = parsed;
|
|
477
|
+
} catch {
|
|
478
|
+
// corrupt: same as unset; PUT overwrites it.
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
// P3: full model ids configured for pi; corrupt or unset reads as [].
|
|
482
|
+
let models: string[] = [];
|
|
483
|
+
const rawModels = this.getSetting('pi_models');
|
|
484
|
+
if (rawModels !== null) {
|
|
485
|
+
try {
|
|
486
|
+
const parsed = JSON.parse(rawModels);
|
|
487
|
+
if (Array.isArray(parsed)) {
|
|
488
|
+
models = parsed.map((m) => String(m).trim()).filter(Boolean);
|
|
489
|
+
}
|
|
490
|
+
} catch {
|
|
491
|
+
// corrupt: fallback to []
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
// Plan bond-app-ajustes-imagen P2: never chosen → the catalog's defaults; a stored list
|
|
495
|
+
// (even empty: «sin herramientas») is kept as is. Unknown ids from an older catalog drop.
|
|
496
|
+
let toolsSelected: string[] = [...DEFAULT_TOOLS];
|
|
497
|
+
const rawTools = this.getSetting('tools_selected');
|
|
498
|
+
if (rawTools !== null) {
|
|
499
|
+
try {
|
|
500
|
+
const parsed = JSON.parse(rawTools);
|
|
501
|
+
if (Array.isArray(parsed)) toolsSelected = TOOL_IDS.filter((id) => parsed.includes(id));
|
|
502
|
+
} catch {
|
|
503
|
+
// corrupt: the defaults; PUT overwrites it.
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
let aptExtra: string[] = [];
|
|
507
|
+
const rawApt = this.getSetting('apt_extra');
|
|
508
|
+
if (rawApt !== null) {
|
|
509
|
+
try {
|
|
510
|
+
const parsed = JSON.parse(rawApt);
|
|
511
|
+
if (Array.isArray(parsed)) aptExtra = parsed.map((p) => String(p)).filter((p) => APT_PACKAGE_RE.test(p));
|
|
512
|
+
} catch {
|
|
513
|
+
// corrupt: none.
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
return {
|
|
517
|
+
setup_done: setupDone,
|
|
518
|
+
agents_selected: agentsSelected,
|
|
519
|
+
suggestions_model: suggestionsModel,
|
|
520
|
+
default_model: defaultModel,
|
|
521
|
+
models,
|
|
522
|
+
tools_selected: toolsSelected,
|
|
523
|
+
apt_extra: aptExtra,
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/** Persists only the fields given (setup_done only via an explicit write, W-P1). */
|
|
528
|
+
saveSetupSelection(selection: {
|
|
529
|
+
agents_selected?: string[];
|
|
530
|
+
setup_done?: boolean;
|
|
531
|
+
suggestions_model?: string | null;
|
|
532
|
+
default_model?: string | null;
|
|
533
|
+
models?: string[];
|
|
534
|
+
tools_selected?: string[];
|
|
535
|
+
apt_extra?: string[];
|
|
536
|
+
}): void {
|
|
537
|
+
if (selection.agents_selected !== undefined) {
|
|
538
|
+
this.setSetting('agents_selected', JSON.stringify(selection.agents_selected));
|
|
539
|
+
}
|
|
540
|
+
if (selection.setup_done !== undefined) {
|
|
541
|
+
this.setSetting('setup_done', selection.setup_done ? 'true' : 'false');
|
|
542
|
+
}
|
|
543
|
+
if (selection.suggestions_model !== undefined) {
|
|
544
|
+
// stored as JSON so null and a string id are told apart; null = «sin sugerencias».
|
|
545
|
+
this.setSetting('suggestions_model', JSON.stringify(selection.suggestions_model));
|
|
546
|
+
}
|
|
547
|
+
if (selection.default_model !== undefined) {
|
|
548
|
+
this.setSetting('default_model', JSON.stringify(selection.default_model));
|
|
549
|
+
}
|
|
550
|
+
if (selection.models !== undefined) {
|
|
551
|
+
this.setSetting('pi_models', JSON.stringify(selection.models));
|
|
552
|
+
}
|
|
553
|
+
if (selection.tools_selected !== undefined) {
|
|
554
|
+
this.setSetting('tools_selected', JSON.stringify(selection.tools_selected));
|
|
555
|
+
}
|
|
556
|
+
if (selection.apt_extra !== undefined) {
|
|
557
|
+
this.setSetting('apt_extra', JSON.stringify(selection.apt_extra));
|
|
558
|
+
}
|
|
559
|
+
}
|
|
127
560
|
}
|