Browse Source

feat-parser-dom-v0-2-0

master
Serge Shutov 3 weeks ago
parent
commit
fddd6fc002
  1. 27
      content/parser.js
  2. 41
      content/selectors.js
  3. 4
      manifest.json
  4. 4
      package.json
  5. 4
      test/fixtures/reply_b64.html
  6. 4
      test/fixtures/reply_broken.html
  7. 4
      test/fixtures/reply_dsh.html
  8. 7
      test/fixtures/reply_file.html
  9. 42
      test/parser-dom.test.mjs

27
content/parser.js

@ -90,4 +90,29 @@ function extractCalls(text, maxCalls) { @@ -90,4 +90,29 @@ function extractCalls(text, maxCalls) {
return { calls: calls, consumed: pos };
}
module.exports = { extractCalls: extractCalls, b64DecodeUtf8: b64DecodeUtf8 };
function parseChatText(root) {
const SEL = (typeof globalThis !== 'undefined' && globalThis.DSH_SELECTORS) ? globalThis.DSH_SELECTORS : null;
if (!SEL || !root || typeof root.querySelectorAll !== 'function') {
return { calls: [], consumed: 0 };
}
const texts = [];
for (let k = 0; k < SEL.MESSAGE_SELECTORS.length; k++) {
const sel = SEL.MESSAGE_SELECTORS[k];
const nodes = root.querySelectorAll(sel);
if (!nodes || nodes.length === 0) continue;
for (let j = 0; j < nodes.length; j++) {
texts.push(SEL.collectText(nodes[j]));
}
if (texts.length > 0) break;
}
const joined = texts.join('\n');
return extractCalls(joined, 3);
}
if (typeof module !== 'undefined' && module.exports) {
module.exports = {
extractCalls: extractCalls,
b64DecodeUtf8: b64DecodeUtf8,
parseChatText: parseChatText
};
}

41
content/selectors.js

@ -0,0 +1,41 @@ @@ -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;
}

4
manifest.json

@ -1,13 +1,13 @@ @@ -1,13 +1,13 @@
{
"manifest_version": 3,
"name": "DSH Memory (unified)",
"version": "0.1.0",
"version": "0.2.0",
"description": "DSH tools + persistent memory, unified",
"permissions": ["storage", "scripting", "activeTab"],
"host_permissions": ["https://chat.deepseek.com/"],
"background": { "service_worker": "background.js" },
"content_scripts": [{
"matches": ["https://chat.deepseek.com/"],
"js": ["content/parser.js"]
"js": ["content/selectors.js", "content/parser.js"]
}]
}

4
package.json

@ -1,11 +1,11 @@ @@ -1,11 +1,11 @@
{
"name": "dsh-memory",
"version": "0.1.0",
"version": "0.2.0",
"private": true,
"description": "Unified DSH tools + persistent memory extension",
"type": "commonjs",
"scripts": {
"test": "node test/parser.test.mjs"
"test": "node test/parser.test.mjs && node test/parser-dom.test.mjs"
},
"license": "MIT"
}

4
test/fixtures/reply_b64.html vendored

@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
<div class="ds-markdown">
<p>Reply:</p>
<pre>⟦DSH_B64⟧eyJ0b29sIjoiZHNoX3JlYWQiLCJhcmdzIjp7InBhdGgiOiJhLnR4dCJ9fQ==⟦/DSH_B64⟧</pre>
</div>

4
test/fixtures/reply_broken.html vendored

@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
<div class="ds-markdown">
<p>Broken:</p>
<pre>⟦DSH⟧{"tool":"dsh_read","args":{</pre>
</div>

4
test/fixtures/reply_dsh.html vendored

@ -0,0 +1,4 @@ @@ -0,0 +1,4 @@
<div class="ds-markdown">
<p>Reply:</p>
<pre>⟦DSH⟧{"tool":"dsh_read","args":{"path":"a.txt"}}⟦/DSH⟧</pre>
</div>

7
test/fixtures/reply_file.html vendored

@ -0,0 +1,7 @@ @@ -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>

42
test/parser-dom.test.mjs

@ -0,0 +1,42 @@ @@ -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…
Cancel
Save