9 changed files with 132 additions and 5 deletions
@ -0,0 +1,41 @@ |
|||||||
|
'use strict'; |
||||||
|
|
||||||
|
// Selectors for assistant replies in DeepSeek chat.
|
||||||
|
// Fallback hierarchy: from specific to generic. Order matters.
|
||||||
|
const MESSAGE_SELECTORS = [ |
||||||
|
'.ds-markdown', |
||||||
|
'[class="markdown"]', |
||||||
|
'[data-testid="message"]', |
||||||
|
'[class*="message"]' |
||||||
|
]; |
||||||
|
|
||||||
|
// Collect text leaf nodes only (no nested blocks).
|
||||||
|
// Join with LF to keep paragraph separators.
|
||||||
|
function collectText(root) { |
||||||
|
if (!root) return ''; |
||||||
|
const parts = []; |
||||||
|
const walk = function (n) { |
||||||
|
if (!n) return; |
||||||
|
if (n.nodeType === 3) { |
||||||
|
const t = (n.nodeValue || '').trim(); |
||||||
|
if (t.length > 0) parts.push(t); |
||||||
|
return; |
||||||
|
} |
||||||
|
const kids = n.childNodes; |
||||||
|
if (!kids) return; |
||||||
|
for (let i = 0; i < kids.length; i++) walk(kids[i]); |
||||||
|
}; |
||||||
|
walk(root); |
||||||
|
return parts.join(String.fromCharCode(10)); |
||||||
|
}; |
||||||
|
|
||||||
|
const DSH_SELECTORS = { |
||||||
|
MESSAGE_SELECTORS: MESSAGE_SELECTORS, |
||||||
|
collectText: collectText |
||||||
|
}; |
||||||
|
|
||||||
|
globalThis.DSH_SELECTORS = DSH_SELECTORS; |
||||||
|
|
||||||
|
if (typeof module !== 'undefined' && module.exports) { |
||||||
|
module.exports = DSH_SELECTORS; |
||||||
|
} |
||||||
@ -1,13 +1,13 @@ |
|||||||
{ |
{ |
||||||
"manifest_version": 3, |
"manifest_version": 3, |
||||||
"name": "DSH Memory (unified)", |
"name": "DSH Memory (unified)", |
||||||
"version": "0.1.0", |
"version": "0.2.0", |
||||||
"description": "DSH tools + persistent memory, unified", |
"description": "DSH tools + persistent memory, unified", |
||||||
"permissions": ["storage", "scripting", "activeTab"], |
"permissions": ["storage", "scripting", "activeTab"], |
||||||
"host_permissions": ["https://chat.deepseek.com/"], |
"host_permissions": ["https://chat.deepseek.com/"], |
||||||
"background": { "service_worker": "background.js" }, |
"background": { "service_worker": "background.js" }, |
||||||
"content_scripts": [{ |
"content_scripts": [{ |
||||||
"matches": ["https://chat.deepseek.com/"], |
"matches": ["https://chat.deepseek.com/"], |
||||||
"js": ["content/parser.js"] |
"js": ["content/selectors.js", "content/parser.js"] |
||||||
}] |
}] |
||||||
} |
} |
||||||
|
|||||||
@ -1,11 +1,11 @@ |
|||||||
{ |
{ |
||||||
"name": "dsh-memory", |
"name": "dsh-memory", |
||||||
"version": "0.1.0", |
"version": "0.2.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" |
"test": "node test/parser.test.mjs && node test/parser-dom.test.mjs" |
||||||
}, |
}, |
||||||
"license": "MIT" |
"license": "MIT" |
||||||
} |
} |
||||||
|
|||||||
@ -0,0 +1,4 @@ |
|||||||
|
<div class="ds-markdown"> |
||||||
|
<p>Reply:</p> |
||||||
|
<pre>⟦DSH_B64⟧eyJ0b29sIjoiZHNoX3JlYWQiLCJhcmdzIjp7InBhdGgiOiJhLnR4dCJ9fQ==⟦/DSH_B64⟧</pre> |
||||||
|
</div> |
||||||
@ -0,0 +1,4 @@ |
|||||||
|
<div class="ds-markdown"> |
||||||
|
<p>Broken:</p> |
||||||
|
<pre>⟦DSH⟧{"tool":"dsh_read","args":{</pre> |
||||||
|
</div> |
||||||
@ -0,0 +1,4 @@ |
|||||||
|
<div class="ds-markdown"> |
||||||
|
<p>Reply:</p> |
||||||
|
<pre>⟦DSH⟧{"tool":"dsh_read","args":{"path":"a.txt"}}⟦/DSH⟧</pre> |
||||||
|
</div> |
||||||
@ -0,0 +1,7 @@ |
|||||||
|
<div class="ds-markdown"> |
||||||
|
<p>Reply:</p> |
||||||
|
<pre>⟦DSH_FILE path="t.txt"⟧ |
||||||
|
line one |
||||||
|
line two |
||||||
|
⟦/DSH_FILE⟧</pre> |
||||||
|
</div> |
||||||
@ -0,0 +1,42 @@ |
|||||||
|
import fs from 'fs'; |
||||||
|
import path from 'path'; |
||||||
|
import assert from 'assert'; |
||||||
|
import { createRequire } from 'module'; |
||||||
|
const require = createRequire(import.meta.url); |
||||||
|
require('../content/selectors.js'); |
||||||
|
const parser = require('../content/parser.js'); |
||||||
|
const OPEN = '<div class="ds-markdown">'; |
||||||
|
const CLOSE = '</div>'; |
||||||
|
|
||||||
|
function loadFixture(name) { |
||||||
|
const html = fs.readFileSync(path.join(process.cwd(), 'test/fixtures', name), 'utf8'); |
||||||
|
const a = html.indexOf(OPEN); |
||||||
|
const b = html.lastIndexOf(CLOSE); |
||||||
|
const inner = (a >= 0 && b > a) ? html.slice(a + OPEN.length, b) : ''; |
||||||
|
const text = inner.split('<').join('').split('>').join('').trim(); |
||||||
|
const node = { nodeType: 1, className: 'ds-markdown', childNodes: [{ nodeType: 3, nodeValue: text }] }; |
||||||
|
return { querySelectorAll: function (sel) { return sel === '.ds-markdown' ? [node] : []; } }; |
||||||
|
} |
||||||
|
|
||||||
|
const tests = []; |
||||||
|
function t(name, fn) { tests.push({ name: name, fn: fn }); } |
||||||
|
|
||||||
|
t('dsh count', function () { assert.equal(parser.parseChatText(loadFixture('reply_dsh.html')).calls.length, 1); }); |
||||||
|
t('dsh tool', function () { assert.equal(parser.parseChatText(loadFixture('reply_dsh.html')).calls[0].tool, 'dsh_read'); }); |
||||||
|
t('dsh path', function () { assert.equal(parser.parseChatText(loadFixture('reply_dsh.html')).calls[0].args.path, 'a.txt'); }); |
||||||
|
t('b64 count', function () { assert.equal(parser.parseChatText(loadFixture('reply_b64.html')).calls.length, 1); }); |
||||||
|
t('b64 tool', function () { assert.equal(parser.parseChatText(loadFixture('reply_b64.html')).calls[0].tool, 'dsh_read'); }); |
||||||
|
t('file path', function () { assert.equal(parser.parseChatText(loadFixture('reply_file.html')).calls[0].args.path, 't.txt'); }); |
||||||
|
t('file content', function () { assert.ok(parser.parseChatText(loadFixture('reply_file.html')).calls[0].args.content.indexOf('line one') >= 0); }); |
||||||
|
t('broken no throw', function () { |
||||||
|
const r = parser.parseChatText(loadFixture('reply_broken.html')); |
||||||
|
assert.equal(r.calls.length, 0); |
||||||
|
}); |
||||||
|
|
||||||
|
let pass = 0, fail = 0; |
||||||
|
for (const x of tests) { |
||||||
|
try { x.fn(); console.log('ok ' + x.name); pass++; } |
||||||
|
catch (e) { console.log('FAIL ' + x.name + ' ' + e.message); fail++; } |
||||||
|
} |
||||||
|
console.log(pass + '/' + (pass + fail)); |
||||||
|
process.exit(fail > 0 ? 1 : 0); |
||||||
Loading…
Reference in new issue