Repository navigation
Expand file tree
/
Copy pathcommon.js
More file actions
371 lines (342 loc) · 21.6 KB
/
Copy pathcommon.js
File metadata and controls
371 lines (342 loc) · 21.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Shared by the side panel and the Settings page: settings and their defaults, platform keys, the developer
// contact, commands that run in the extension's folder, the update check, and the AI requests (sent through the
// chat you're signed in to in this browser, see webchat.js).
// The chats the AI can use, in the order Auto tries them. No API key: you sign in to the site in this browser once.
// cookie: the login cookie that says you're signed in there (read with chrome.cookies, nothing is sent anywhere).
const AI_CHATS = {
claude: { label: "Claude", site: "claude.ai", cookieUrl: "https://claude.ai/", cookie: /^sessionKey$/ },
chatgpt: { label: "ChatGPT", site: "chatgpt.com", cookieUrl: "https://chatgpt.com/", cookie: /^__Secure-next-auth\.session-token(\.\d+)?$/ },
gemini: { label: "Gemini", site: "gemini.google.com", cookieUrl: "https://gemini.google.com/", cookie: /^(__Secure-1PSID|SID)$/ }
};
// The chats you're signed in to in this browser, in Auto's order. A login cookie can outlive the session; a chat
// that turns out to be signed out is skipped when it's used (generateStructured / generateText).
async function signedInChats() {
if (!chrome.cookies?.getAll) return Object.keys(AI_CHATS); // no cookies permission: try them all
const out = [];
for (const [id, c] of Object.entries(AI_CHATS)) {
const jar = await chrome.cookies.getAll({ url: c.cookieUrl }).catch(() => []);
if (jar.some((k) => c.cookie.test(k.name) && k.value && (!k.expirationDate || k.expirationDate * 1000 > Date.now()))) out.push(id);
}
return out;
}
// Settings from before 1.36 that held API keys and models: removed from storage on update (background.js).
const OLD_AI_SETTINGS = ["provider", "anthropicKey", "openaiKey", "geminiKey", "localBaseUrl", "localApiKey", "models", "speed", "apiKey", "model"];
const DEFAULTS = {
aiEnabled: true, // off: the brief comes from patterns in the ticket (no AI); NetBox, BMC, ping and parts still work
aiChat: "auto", // auto (the first chat you're signed in to: Claude, ChatGPT, Gemini) | claude | chatgpt | gemini
jiraHosts: [], // extra self-hosted Jira hostnames, e.g. ["jira.example.com"]
privacyMode: false, // swap sensitive values for placeholders before sending anything to the AI
privacyHide: { people: true, contact: true, network: true, serials: true },
privacyWords: "", // extra words to hide (customer names, project codes), one per line
autoRun: true, // analyze automatically when a Jira issue opens
serverField: "", // optional Jira field name/id holding the server name
locationField: "", // optional Jira field name/id holding the location
teamContext: "", // runbooks / team notes appended to the prompt
customInstructions: "", // extra free-form instructions added to every prompt
pingAuto: true, // ping the server's IP addresses automatically when a brief is shown
bmcUrl: "https://{ip}", // how to open a BMC web UI; {ip} is replaced
bmcUser: "ADMIN", // username for the BMC login (the password is pasted in the panel)
netboxEnabled: true,
netboxUrl: "https://netbox.prod.lambdalabs.cloud",
netboxToken: "", // optional; by default your NetBox browser login is used
podcrawlerUrl: "https://podcrawler.aws.lambdalabs.cloud/#/racking",
podcrawlerStep: true, // add a "rescan in Podcrawler" step to replacement tickets
// Lambda parts catalog (Google Sheet shared by link): Lambda P/N ↔ manufacturer P/N ↔ description
catalogUrl: "https://docs.google.com/spreadsheets/d/1HkdgvcwHJ-HuDR90mmsVTNbuUqEtjRmYWpKF5hgCDAY/edit#gid=613788530",
slackIn: "browser", // clicking a name opens the chat in Slack in Chrome ("browser") or just opens the desktop app ("app")
// Appearance (Settings → Appearance; applied right away, see applyLook)
theme: "system", // system | light | dark
accent: "lambda", // lambda | blue | teal | green | orange | pink | graphite
sectionColors: "colorful", // colorful (each section its own colour) | plain (all in the accent colour)
layout: "glance", // glance (At a glance + details folded) | open (everything open) | classic (no At a glance, everything open)
density: "comfortable", // comfortable | compact
textSize: "normal", // small | normal | large
jumpBar: true // the row of section chips under the header
};
// ---------- Appearance ----------
// Theme, colours and layout as data-* attributes on <html> (styles.css → Appearance). Also kept in localStorage
// so the page starts in the right theme before chrome.storage answers, and re-applied when Settings change.
const LOOK_KEYS = ["theme", "accent", "sectionColors", "layout", "density", "textSize", "jumpBar"];
function applyLook(s) {
const d = document.documentElement.dataset;
const set = (k, v) => (v ? (d[k] = v) : delete d[k]);
set("theme", s.theme === "system" ? "" : s.theme);
set("accent", s.accent === "lambda" ? "" : s.accent);
set("sections", s.sectionColors === "plain" ? "plain" : "");
set("layout", s.layout);
set("density", s.density === "compact" ? "compact" : "");
set("text", s.textSize === "normal" ? "" : s.textSize);
set("jump", s.jumpBar === false ? "off" : "");
try { localStorage.setItem("look", JSON.stringify(Object.fromEntries(LOOK_KEYS.map((k) => [k, s[k]])))); } catch {}
if (["open", "classic"].includes(s.layout)) document.querySelectorAll("#result details.fold").forEach((x) => (x.open = true));
}
if (typeof document !== "undefined") {
try { applyLook({ ...DEFAULTS, ...JSON.parse(localStorage.getItem("look") || "{}") }); } catch {}
const load = () => chrome.storage.local.get(LOOK_KEYS).then((st) => applyLook({ ...DEFAULTS, ...st }));
load();
chrome.storage.onChanged.addListener((ch, area) => { if (area === "local" && LOOK_KEYS.some((k) => k in ch)) load(); });
}
async function getSettings() {
const stored = await chrome.storage.local.get(Object.keys(DEFAULTS));
const s = { ...DEFAULTS, ...stored, privacyHide: { ...DEFAULTS.privacyHide, ...(stored.privacyHide || {}) } };
if (s.aiChat !== "auto" && !AI_CHATS[s.aiChat]) s.aiChat = DEFAULTS.aiChat;
// aiChats: the chats to try, in order. Auto: the ones you're signed in to (none: the AI waits for a sign-in).
// A chat you picked yourself is always tried, and asks you to sign in if needed.
s.aiChats = s.aiChat === "auto" ? await signedInChats() : [s.aiChat];
s.aiSite = s.aiChats[0] || null;
return s;
}
// "Claude", "ChatGPT" or "Gemini": the chat that answers (the one that last answered, after a fall-through).
function aiChatName(s) {
return AI_CHATS[s.aiSite]?.label || "an AI chat";
}
// Nothing to set up any more (no key): the AI is used whenever it's switched on. Kept for older callers.
function missingConfig() {
return "";
}
// On, and a chat to use: with Auto and no chat signed in, the panel shows the basic brief and asks you to sign in.
const aiActive = (s) => s.aiEnabled !== false && !!s.aiSite;
const aiNeedsSignIn = (s) => s.aiEnabled !== false && !s.aiSite;
// Keyboard shortcuts as shown in hints (Mac vs Windows/Linux).
const IS_MAC = /mac/i.test(navigator.userAgentData?.platform || navigator.platform || "");
const IS_WIN = /win/i.test(navigator.userAgentData?.platform || navigator.platform || "");
// Installed from the Chrome Web Store (Chrome adds update_url): the store updates it, and its files live in the profile.
const IS_STORE = "update_url" in chrome.runtime.getManifest();
const KEYS = IS_MAC
? { paste: "⌘V", quickAccess: "⌘⇧Space", slackSearch: "⌘K" }
: { paste: "Ctrl+V", quickAccess: "Ctrl+Shift+Space", slackSearch: "Ctrl+K" };
// ---------- Developer contact ----------
// The extension's developer: help, bugs and feature ideas. Linked at the bottom of the panel and of Settings.
const DEVELOPER = { name: "Hadi Khodja", slack: "https://lambda.slack.com/team/U0AAMLCUCUA" };
function fillDevContact() {
for (const el of document.querySelectorAll(".dev-contact")) {
const a = Object.assign(document.createElement("a"), { href: DEVELOPER.slack, target: "_blank", rel: "noopener", textContent: `${DEVELOPER.name} on Slack` });
el.replaceChildren("💬 Help, bugs or feature ideas? Message ", a);
}
}
// ---------- Commands that run in the extension's folder ----------
// They find the lambda-rackside folder themselves, wherever it was unzipped (Desktop, Downloads…): the browser
// records the folder of an unpacked extension, by extension ID, in each profile's preferences.
function browserDataDir() {
const edge = /\bEdg\//.test(navigator.userAgent), brave = !!navigator.brave;
if (IS_WIN) return `$env:LOCALAPPDATA\\${edge ? "Microsoft\\Edge" : brave ? "BraveSoftware\\Brave-Browser" : "Google\\Chrome"}\\User Data`;
return `~/Library/Application\\ Support/${edge ? "Microsoft\\ Edge" : brave ? "BraveSoftware/Brave-Browser" : "Google/Chrome"}`;
}
// mac / win: the command to run once the folder is found, as "$d" (Terminal on a Mac, PowerShell on Windows).
function folderCommand(mac, win) {
const id = chrome.runtime.id, dir = browserDataDir();
if (IS_STORE) {
// A store install is unpacked by Chrome into <profile>/Extensions/<id>/<version>_0.
const ver = chrome.runtime.getManifest().version;
if (IS_WIN) {
return `$id='${id}'; $d = Resolve-Path "${dir}\\*\\Extensions\\${id}\\${ver}_*" -ErrorAction SilentlyContinue | Select-Object -Last 1 | % { $_.Path }; ` +
`if ($d) { ${win} } else { 'Lambda Rackside folder not found: see Settings' }`;
}
const base = IS_MAC ? dir : "~/.config/google-chrome";
return `d=$(ls -d ${base}/*/Extensions/${id}/${ver}_* 2>/dev/null | tail -1); ` +
`if [ -n "$d" ]; then ${mac}; else echo "Lambda Rackside folder not found: see Settings"; fi`;
}
if (IS_WIN) {
return `$id='${id}'; $d = Get-ChildItem "${dir}\\*\\Secure Preferences","${dir}\\*\\Preferences" -ErrorAction SilentlyContinue | ` +
`% { if ((Get-Content -Raw $_.FullName) -match ('"' + $id + '":\\{.*?"path":"([^"]*)"')) { [regex]::Unescape($Matches[1]) } } | Select-Object -First 1; ` +
`if ($d) { ${win} } else { 'lambda-rackside folder not found: see Settings' }`;
}
if (!IS_MAC) return `d=~/Desktop/lambda-rackside; ${mac}`; // Linux: adjust the path
return `for f in ${dir}/*/Secure\\ Preferences ${dir}/*/Preferences; do d=$(plutil -extract extensions.settings.${id}.path raw -o - "$f" 2>/dev/null) && break; done; ` +
`if [ -n "$d" ]; then ${mac}; else echo "lambda-rackside folder not found: see Settings"; fi`;
}
// ---------- Updates ----------
// The latest version is read from the private GitHub repo with the user's own GitHub login in Chrome, so only
// people who can see the repo get it. Checked at most every 2 hours (in the background, and when the panel opens).
const UPDATE_REPO = "MarshallStunt/lambda-rackside";
const UPDATE_EVERY_MS = 2 * 60 * 60 * 1000;
const UPDATE_LINKS = {
changes: `https://github.com/${UPDATE_REPO}/commits/main`,
zip: `https://github.com/${UPDATE_REPO}/archive/refs/heads/main.zip`
};
function isNewerVersion(a, b) {
const pa = String(a).split(".").map(Number), pb = String(b).split(".").map(Number);
for (let i = 0; i < 3; i++) if ((pa[i] || 0) !== (pb[i] || 0)) return (pa[i] || 0) > (pb[i] || 0);
return false;
}
// { ok, latest, current, available, error, at }
async function checkForUpdate(force = false) {
const current = chrome.runtime.getManifest().version;
if (IS_STORE) return { ok: true, store: true, latest: current, current, available: false, at: Date.now() };
const { updateCheck } = await chrome.storage.local.get("updateCheck");
let r = updateCheck;
if (force || !r || Date.now() - r.at > UPDATE_EVERY_MS) {
try {
// Redirects to a short-lived raw.githubusercontent.com link when the GitHub login can see the repo.
const res = await fetch(`https://github.com/${UPDATE_REPO}/raw/main/manifest.json`, { credentials: "include", cache: "no-store" });
if (res.status === 404) throw new Error("sign in to GitHub in Chrome with an account that can see the lambda-rackside repo");
if (!res.ok) throw new Error(`GitHub answered HTTP ${res.status}`);
const latest = (await res.json().catch(() => ({}))).version;
if (!latest) throw new Error("sign in to GitHub in Chrome with an account that can see the lambda-rackside repo");
r = { ok: true, latest, at: Date.now() };
} catch (e) {
r = { ok: false, error: e.message, at: Date.now() };
}
await chrome.storage.local.set({ updateCheck: r });
}
return { ...r, current, available: !!r.ok && isNewerVersion(r.latest, current) };
}
// Red "NEW" badge on the toolbar icon while a newer version is waiting (not after "Later" for that version).
async function showUpdateBadge(u) {
const { updateLater } = await chrome.storage.local.get("updateLater");
const on = !!u.available && updateLater !== u.latest;
await chrome.action.setBadgeText({ text: on ? "NEW" : "" });
if (on) await chrome.action.setBadgeBackgroundColor({ color: "#d93025" });
await chrome.action.setTitle({ title: on ? `Lambda Rackside: version ${u.latest} is available` : "Lambda Rackside" });
}
// One click: download the new version with the GitHub login, then the helper puts it in the extension's folder
// (or runs git pull in a git copy) and updates itself. Returns the helper's answer.
async function updateNow() {
const res = await fetch(UPDATE_LINKS.zip, { credentials: "include", cache: "no-store" });
if (!res.ok) throw new Error(res.status === 404 ? "sign in to GitHub in Chrome with an account that can see the repo" : `GitHub answered HTTP ${res.status}`);
const bytes = new Uint8Array(await res.arrayBuffer());
let bin = "";
for (let i = 0; i < bytes.length; i += 0x8000) bin += String.fromCharCode(...bytes.subarray(i, i + 0x8000));
const r = await sendNative({ action: "self_update", zip: btoa(bin) });
if (!r.ok && (/unknown action/i.test(r.error || "") || r.notInstalled)) {
throw new Error("the helper on this computer is too old (or not installed) to update by itself: use “Update by hand” below once");
}
if (!r.ok) throw new Error(r.error);
return r;
}
// The update steps (shared by the panel banner and Settings): Update now, or by hand.
function updateSteps() {
const el = (tag, props = {}, ...kids) => { const e = Object.assign(document.createElement(tag), props); e.append(...kids.filter(Boolean)); return e; };
const status = el("span", { className: "muted" });
const now = el("button", { type: "button", className: "primary", textContent: "⬇️ Update now" });
now.onclick = async () => {
now.disabled = true;
status.style.color = "";
status.textContent = "Downloading from GitHub and installing…";
try {
const r = await updateNow();
status.textContent = `✓ Updated${r.method === "git" ? " (git pull)" : ` (${r.files} files)`}${r.helper ? `, helper ${r.helper}` : ""}. Reloading the extension: reopen this panel.`;
setTimeout(() => chrome.runtime.reload(), 1500);
} catch (e) {
status.textContent = `Couldn't update: ${e.message}.`;
status.style.color = "var(--err)";
now.disabled = false;
}
};
// By hand: git copies pull; ZIP copies get the new ZIP downloading and their folder opened.
const zipNote = "Not a git copy: the new ZIP is downloading. Unzip it and copy its files into the folder that just opened, then reload the extension.";
const cmd = folderCommand(
`if [ -d "$d/.git" ]; then git -C "$d" pull; else open "${UPDATE_LINKS.zip}"; open "$d"; echo "${zipNote}"; fi`,
`if (Test-Path "$d\\.git") { git -C "$d" pull } else { Start-Process '${UPDATE_LINKS.zip}'; explorer.exe $d; '${zipNote}' }`);
const copy = el("button", { type: "button", className: "mini", textContent: "Copy" });
copy.onclick = async () => { await navigator.clipboard.writeText(cmd); copy.textContent = "Copied ✓"; setTimeout(() => (copy.textContent = "Copy"), 1500); };
const reload = el("button", { type: "button", className: "mini", textContent: "🔄 Reload extension" });
reload.onclick = () => chrome.runtime.reload();
return el("div", { className: "update-steps" },
el("div", { className: "row update-now" }, now, status),
el("details", {},
el("summary", {}, "Update by hand"),
el("ol", {},
el("li", {}, "Paste this in ", IS_WIN ? "PowerShell" : "Terminal", ". It finds your lambda-rackside folder and pulls (git copy) or downloads the new ZIP and opens the folder (ZIP copy):",
el("div", { className: "row" }, el("input", { type: "text", readOnly: true, className: "mono", value: cmd }), copy),
"Or ", el("a", { href: UPDATE_LINKS.zip, target: "_blank", textContent: "download the ZIP" }), " and copy its files over your lambda-rackside folder."),
el("li", {}, reload, " then reopen this panel."),
el("li", { className: "muted" }, "If ping or BMC then says the helper is out of date, run the setup command again (Settings → Start)."))));
}
// ---------- HTTP helpers ----------
// Best-effort parse of a JSON object that is still being streamed: closes open strings, arrays and
// objects, dropping a half-written key/value at the end. Returns null if nothing usable yet.
function parsePartialJson(text) {
const t = String(text || "");
let s = t.slice(Math.max(0, t.indexOf("{")));
if (!s.startsWith("{")) return null;
for (let tries = 0; tries < 8 && s; tries++) {
const closers = [];
let inStr = false, esc = false, cut = 1;
for (let i = 0; i < s.length; i++) {
const c = s[i];
if (inStr) {
if (esc) esc = false;
else if (c === "\\") esc = true;
else if (c === '"') inStr = false;
continue;
}
if (c === '"') inStr = true;
else if (c === "{" || c === "[") { closers.push(c === "{" ? "}" : "]"); cut = i + 1; }
else if (c === "}" || c === "]") closers.pop();
else if (c === ",") cut = i;
}
const body = (inStr && !esc ? s + '"' : s).replace(/[\s,:]+$/, "");
try {
return JSON.parse(body + closers.reverse().join(""));
} catch {
s = s.slice(0, cut); // drop the incomplete tail and try again
}
}
return null;
}
// Pull a JSON object out of model text (handles ```json fences and <think> blocks).
function parseJsonLoose(text) {
let t = String(text || "").replace(/<think>[\s\S]*?<\/think>/gi, "").trim();
const fence = t.match(/```(?:json)?\s*([\s\S]*?)```/i);
if (fence) t = fence[1];
const start = t.indexOf("{");
const end = t.lastIndexOf("}");
if (start === -1 || end <= start) throw new Error("The chat didn't answer in the expected format. Try Re-analyze.");
return JSON.parse(t.slice(start, end + 1));
}
// ---------- AI requests (through the browser chat, webchat.js) ----------
// The panel shows progress and "sign in to Claude" messages through this hook (set by the side panel).
let aiNoteHook = null;
function aiModelLabel(s) {
return `${aiChatName(s)} (your browser chat)`;
}
// Runs one request on the first chat that works. With Auto, a chat that's signed out, asking for a human check,
// out of messages or not loading is skipped quietly for the next one you're signed in to; only the last one left
// shows its window for you to sign in. Sets s.aiSite to the chat that answered.
const SKIP_TO_NEXT = new Set(["login", "check", "limit", "timeout", "layout", "closed"]);
async function askChats(s, opts) {
if (typeof LambdaWebChat === "undefined") throw new Error("The AI works in the side panel and Settings only.");
const chats = s.aiChats?.length ? s.aiChats : [s.aiChat !== "auto" ? s.aiChat : "claude"];
const skipped = [];
for (let i = 0; i < chats.length; i++) {
const last = i === chats.length - 1;
s.aiSite = chats[i];
try {
const text = await LambdaWebChat.ask({ ...opts, site: chats[i], reveal: last,
onNote: (t, needsYou) => aiNoteHook?.(t, needsYou) });
if (skipped.length) aiNoteHook?.(null, true);
return text;
} catch (e) {
if (last || !SKIP_TO_NEXT.has(e.code)) throw skipped.length ? new Error(`${e.message} (also tried ${skipped.join(", ")})`) : e;
skipped.push(`${AI_CHATS[chats[i]].label}: ${e.message}`);
aiNoteHook?.(`${AI_CHATS[chats[i]].label} isn't available (${e.message}). Trying ${AI_CHATS[chats[i + 1]].label}…`, true);
}
}
}
// A JSON answer: the schema goes in the request, the answer is read from the chat's code block.
// onPartial(obj) gets the partly written result while the chat writes. Returns { result, model }.
async function generateStructured(s, { system, user, schema, onPartial, signal }) {
let last = 0;
const text = await askChats(s, {
signal,
system: `${system}\n\nAnswer with ONLY one JSON object in a single \`\`\`json code block written right in your reply (not in an artifact, canvas or file), no text before or after it. Do not use tools or web search. It must match this JSON Schema:\n${JSON.stringify(schema)}`,
user,
onText: (full) => {
if (!onPartial || Date.now() - last < 150) return;
last = Date.now();
const obj = parsePartialJson(full.replace(/^[\s\S]*?```(?:json)?/i, ""));
if (obj) onPartial(obj);
}
});
if (!text) throw new Error(`${aiChatName(s)} returned an empty answer.`);
return { result: parseJsonLoose(text), model: aiModelLabel(s) };
}
// Free text. messages: [{ role: "user" | "assistant", content }], the last one is the question.
// conversation: an id (a ticket's chat) so follow-ups continue the same chat. Returns { text, model }.
async function generateText(s, { system, messages, conversation, signal }) {
const history = messages.slice(0, -1), user = messages.at(-1)?.content || "";
const text = (await askChats(s, { system: `${system}\nWrite the answer right in your reply (no artifact, canvas or file).`, user, history, conversation, signal })).replace(/<think>[\s\S]*?<\/think>/gi, "").trim();
if (!text) throw new Error(`${aiChatName(s)} returned an empty answer.`);
return { text, model: aiModelLabel(s) };
}