fix: code_run encoding compat; wechat truncate long code blocks & limit msg count
This commit is contained in:
@@ -8,6 +8,9 @@ def _d(b):
|
||||
except: return b.decode('gbk', 'replace')
|
||||
def _run(*a, **k):
|
||||
t = k.pop('text', 0) | k.pop('universal_newlines', 0)
|
||||
enc = k.pop('encoding', None)
|
||||
k.pop('errors', None)
|
||||
if enc: t = 1
|
||||
if t and isinstance(k.get('input'), str):
|
||||
k['input'] = k['input'].encode()
|
||||
r = _r(*a, **k)
|
||||
|
||||
@@ -194,7 +194,13 @@ _TAG_PATS = [r'<' + t + r'>.*?</' + t + r'>' for t in ('thinking', 'summary', 't
|
||||
_TAG_PATS.append(r'<file_content>.*?</file_content>')
|
||||
|
||||
def _strip_md(t):
|
||||
t = re.sub(r'(`{3,})[\s\S]*?\1', lambda m: m.group().strip('`').split('\n',1)[-1] if '\n' in m.group() else m.group().strip('`'), t)
|
||||
def _trunc_code(m):
|
||||
body = m.group().strip('`')
|
||||
if '\n' not in body: return body
|
||||
lines = body.split('\n', 1)[-1].split('\n') # drop language line
|
||||
if len(lines) > 10: return '\n'.join(lines[:10]) + '\n...'
|
||||
return '\n'.join(lines)
|
||||
t = re.sub(r'(`{3,})[\s\S]*?\1', _trunc_code, t)
|
||||
t = re.sub(r'`([^`]+)`', r'\1', t)
|
||||
t = re.sub(r'!\[.*?\]\(.*?\)', '', t)
|
||||
t = re.sub(r'\[([^\]]+)\]\([^\)]+\)', r'\1', t)
|
||||
@@ -266,7 +272,12 @@ def on_message(bot, msg):
|
||||
files = re.findall(r'\[FILE:([^\]]+)\]', result)
|
||||
files = [f for f in files if (f if os.path.isabs(f) else os.path.join(_TEMP_DIR, f)) not in media_paths]
|
||||
show = _clean(result)
|
||||
for chunk in _split(show):
|
||||
chunks = _split(show)
|
||||
_MAX_MSGS = 6
|
||||
if len(chunks) > _MAX_MSGS:
|
||||
keep = chunks[:3] + [f'...(省略{len(chunks) - 5}条)...'] + chunks[-2:]
|
||||
chunks = keep
|
||||
for chunk in chunks:
|
||||
try: bot.send_text(uid, chunk, context_token=ctx)
|
||||
except Exception as e: print(f'[WX] send err: {e}', file=sys.__stdout__)
|
||||
time.sleep(0.3)
|
||||
|
||||
Reference in New Issue
Block a user