4 changed files with 152 additions and 22 deletions
@ -1,3 +1,58 @@ |
|||||||
'use strict'; |
"use strict"; |
||||||
|
// DSH Memory - background service worker.
|
||||||
|
// Единственное место, откуда идут HTTP-запросы к локальному мосту (CORS не мешает).
|
||||||
|
|
||||||
console.log('dsh-memory ready'); |
const BRIDGE_BASE = "http://127.0.0.1:8766"; |
||||||
|
const TOKEN_KEY = "dshpro-bridge-token"; |
||||||
|
const TIMEOUT_MS = 65000; |
||||||
|
|
||||||
|
async function readToken() { |
||||||
|
const v = await chrome.storage.local.get(TOKEN_KEY); |
||||||
|
return v && v[TOKEN_KEY] ? String(v[TOKEN_KEY]) : ""; |
||||||
|
} |
||||||
|
|
||||||
|
async function postTool(payload) { |
||||||
|
const token = await readToken(); |
||||||
|
if (!token) return { ok: false, error: { code: "E_NO_TOKEN", message: "token not configured" } }; |
||||||
|
const ctrl = new AbortController(); |
||||||
|
const timer = setTimeout(function () { ctrl.abort(); }, TIMEOUT_MS); |
||||||
|
try { |
||||||
|
const res = await fetch(BRIDGE_BASE + "/tool", { |
||||||
|
method: "POST", |
||||||
|
headers: { "Content-Type": "application/json", Authorization: "Bearer " + token }, |
||||||
|
body: JSON.stringify(payload), |
||||||
|
signal: ctrl.signal |
||||||
|
}); |
||||||
|
const text = await res.text(); |
||||||
|
let json = null; |
||||||
|
try { json = JSON.parse(text); } catch (e) { json = null; } |
||||||
|
if (!json) return { ok: false, error: { code: "E_BAD_RESPONSE", message: text.slice(0, 300) } }; |
||||||
|
return json; |
||||||
|
} catch (e) { |
||||||
|
const msg = e && e.message ? e.message : String(e); |
||||||
|
return { ok: false, error: { code: "E_NETWORK", message: msg } }; |
||||||
|
} finally { |
||||||
|
clearTimeout(timer); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
async function getHealth() { |
||||||
|
const token = await readToken(); |
||||||
|
if (!token) return { ok: false, error: { code: "E_NO_TOKEN", message: "token not configured" } }; |
||||||
|
try { |
||||||
|
const res = await fetch(BRIDGE_BASE + "/health", { headers: { Authorization: "Bearer " + token } }); |
||||||
|
return await res.json(); |
||||||
|
} catch (e) { |
||||||
|
const msg = e && e.message ? e.message : String(e); |
||||||
|
return { ok: false, error: { code: "E_NETWORK", message: msg } }; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) { |
||||||
|
if (!msg || typeof msg !== "object") return false; |
||||||
|
if (msg.type === "DSH_BRIDGE_CALL") { postTool(msg.payload).then(sendResponse); return true; } |
||||||
|
if (msg.type === "DSH_BRIDGE_HEALTH") { getHealth().then(sendResponse); return true; } |
||||||
|
return false; |
||||||
|
}); |
||||||
|
|
||||||
|
console.log("dsh-memory background ready"); |
||||||
|
|||||||
@ -0,0 +1,58 @@ |
|||||||
|
(function (root, factory) { |
||||||
|
if (typeof module === "object" && module.exports) module.exports = factory(); |
||||||
|
else root.DSH_BRIDGE = factory(); |
||||||
|
}(typeof globalThis !== "undefined" ? globalThis : this, function () { |
||||||
|
"use strict"; |
||||||
|
const TOKEN_KEY = "dshpro-bridge-token"; |
||||||
|
|
||||||
|
function available() { |
||||||
|
return typeof chrome !== "undefined" && chrome.runtime && typeof chrome.runtime.sendMessage === "function"; |
||||||
|
} |
||||||
|
|
||||||
|
function send(payload) { |
||||||
|
return new Promise(function (resolve) { |
||||||
|
if (!available()) { |
||||||
|
resolve({ ok: false, error: { code: "E_NO_RUNTIME", message: "chrome.runtime unavailable" } }); |
||||||
|
return; |
||||||
|
} |
||||||
|
chrome.runtime.sendMessage({ type: "DSH_BRIDGE_CALL", payload: payload }, function (resp) { |
||||||
|
const last = chrome.runtime.lastError; |
||||||
|
if (last) resolve({ ok: false, error: { code: "E_RUNTIME", message: last.message } }); |
||||||
|
else resolve(resp || { ok: false, error: { code: "E_NO_RESPONSE", message: "empty" } }); |
||||||
|
}); |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
function sendCall(call) { |
||||||
|
if (!call || typeof call !== "object" || typeof call.tool !== "string") { |
||||||
|
return Promise.resolve({ ok: false, error: { code: "E_ARGS_INVALID", message: "call.tool required" } }); |
||||||
|
} |
||||||
|
const payload = { tool: call.tool, args: call.args || {} }; |
||||||
|
if (call.workspace) payload.workspace = call.workspace; |
||||||
|
if (call.confirmToken) payload.confirmToken = call.confirmToken; |
||||||
|
return send(payload); |
||||||
|
} |
||||||
|
|
||||||
|
function health() { |
||||||
|
return new Promise(function (resolve) { |
||||||
|
if (!available()) { |
||||||
|
resolve({ ok: false, error: { code: "E_NO_RUNTIME", message: "chrome.runtime unavailable" } }); |
||||||
|
return; |
||||||
|
} |
||||||
|
chrome.runtime.sendMessage({ type: "DSH_BRIDGE_HEALTH" }, function (resp) { |
||||||
|
const last = chrome.runtime.lastError; |
||||||
|
if (last) resolve({ ok: false, error: { code: "E_RUNTIME", message: last.message } }); |
||||||
|
else resolve(resp || { ok: false, error: { code: "E_NO_RESPONSE", message: "empty" } }); |
||||||
|
}); |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
function hasToken() { |
||||||
|
return new Promise(function (resolve) { |
||||||
|
if (typeof chrome === "undefined" || !chrome.storage) { resolve(false); return; } |
||||||
|
chrome.storage.local.get(TOKEN_KEY, function (v) { resolve(Boolean(v && v[TOKEN_KEY])); }); |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
return { sendCall: sendCall, health: health, hasToken: hasToken, available: available, TOKEN_KEY: TOKEN_KEY }; |
||||||
|
})); |
||||||
@ -1,13 +1,30 @@ |
|||||||
{ |
{ |
||||||
"manifest_version": 3, |
"manifest_version": 3, |
||||||
"name": "DSH Memory (unified)", |
"name": "DSH Memory (unified)", |
||||||
"version": "0.2.0", |
"version": "0.3.0", |
||||||
"description": "DSH tools + persistent memory, unified", |
"description": "DSH tools + persistent memory, unified", |
||||||
"permissions": ["storage", "scripting", "activeTab"], |
"permissions": [ |
||||||
"host_permissions": ["https://chat.deepseek.com/"], |
"storage", |
||||||
"background": { "service_worker": "background.js" }, |
"scripting", |
||||||
"content_scripts": [{ |
"activeTab" |
||||||
"matches": ["https://chat.deepseek.com/"], |
], |
||||||
"js": ["content/selectors.js", "content/parser.js"] |
"host_permissions": [ |
||||||
}] |
"https://chat.deepseek.com/", |
||||||
|
"http://127.0.0.1:8766/" |
||||||
|
], |
||||||
|
"background": { |
||||||
|
"service_worker": "background.js" |
||||||
|
}, |
||||||
|
"content_scripts": [ |
||||||
|
{ |
||||||
|
"matches": [ |
||||||
|
"https://chat.deepseek.com/" |
||||||
|
], |
||||||
|
"js": [ |
||||||
|
"content/selectors.js", |
||||||
|
"content/parser.js", |
||||||
|
"content/bridge-client.js" |
||||||
|
] |
||||||
|
} |
||||||
|
] |
||||||
} |
} |
||||||
|
|||||||
@ -1,11 +1,11 @@ |
|||||||
{ |
{ |
||||||
"name": "dsh-memory", |
"name": "dsh-memory", |
||||||
"version": "0.2.0", |
"version": "0.3.0", |
||||||
"private": true, |
"private": true, |
||||||
"description": "Unified DSH tools + persistent memory extension", |
"description": "Unified DSH tools + persistent memory extension", |
||||||
"type": "commonjs", |
"type": "commonjs", |
||||||
"scripts": { |
"scripts": { |
||||||
"test": "node test/parser.test.mjs && node test/parser-dom.test.mjs" |
"test": "node test/parser.test.mjs && node test/parser-dom.test.mjs" |
||||||
}, |
}, |
||||||
"license": "MIT" |
"license": "MIT" |
||||||
} |
} |
||||||
|
|||||||
Loading…
Reference in new issue