fix(mcp): make mcp use less tokens by striping useless tags
This commit is contained in:
+13
-1
@@ -1,5 +1,6 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from sys import stderr
|
from sys import stderr
|
||||||
|
from copy import deepcopy
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
from re import sub as re_sub
|
from re import sub as re_sub
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
@@ -571,6 +572,14 @@ class Convertor:
|
|||||||
|
|
||||||
return markdownify(body)
|
return markdownify(body)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _strip_noise_tags(cls, page: Selector) -> Selector:
|
||||||
|
"""Return a copy of the Selector with noise tags removed."""
|
||||||
|
clean_root = deepcopy(page._root)
|
||||||
|
for element in clean_root.iter(*{"script", "style", "noscript", "svg"}):
|
||||||
|
element.drop_tree()
|
||||||
|
return Selector(root=clean_root, url=page.url)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _extract_content(
|
def _extract_content(
|
||||||
cls,
|
cls,
|
||||||
@@ -587,6 +596,7 @@ class Convertor:
|
|||||||
else:
|
else:
|
||||||
if main_content_only:
|
if main_content_only:
|
||||||
page = cast(Selector, page.css("body").first) or page
|
page = cast(Selector, page.css("body").first) or page
|
||||||
|
page = cls._strip_noise_tags(page)
|
||||||
|
|
||||||
pages = [page] if not css_selector else cast(Selectors, page.css(css_selector))
|
pages = [page] if not css_selector else cast(Selectors, page.css(css_selector))
|
||||||
for page in pages:
|
for page in pages:
|
||||||
@@ -596,7 +606,9 @@ class Convertor:
|
|||||||
case "html":
|
case "html":
|
||||||
yield page.html_content
|
yield page.html_content
|
||||||
case "text":
|
case "text":
|
||||||
txt_content = page.get_all_text(strip=True)
|
txt_content = page.get_all_text(
|
||||||
|
strip=True, ignore_tags=("script", "style", "noscript", "svg", "iframe")
|
||||||
|
)
|
||||||
for s in (
|
for s in (
|
||||||
"\n",
|
"\n",
|
||||||
"\r",
|
"\r",
|
||||||
|
|||||||
Reference in New Issue
Block a user