Files
kefu/deploy/recognition-audit-20260916/complete_gateway_upload.py
T
2026-09-21 10:34:06 +08:00

36 lines
1.9 KiB
Python

from pathlib import Path
import hashlib
import json
import shutil
report = Path('C:/kefu/deploy/recognition-audit-20260916')
manifest = []
for root in (Path('C:/kefu/wechat_rpa'), Path('C:/wechat_rpa')):
path = root / 'model_gateway.py'
raw = path.read_bytes()
text = raw.decode('utf-8').replace('\r\n', '\n')
old = ''' upload_id = str((response.json() or {}).get("id") or "").strip()
if not upload_id:
raise GatewayError("Dify 上传成功但没返回文件 ID", retriable=False)'''
new = ''' try:
uploaded = response.json()
except ValueError as exc:
raise GatewayError("Dify 上传响应不是合法 JSON", retriable=False) from exc
if not isinstance(uploaded, dict):
raise GatewayError("Dify 上传响应不是 JSON 对象", retriable=False)
upload_id = uploaded.get("id")
if not isinstance(upload_id, str) or not upload_id.strip():
raise GatewayError("Dify 上传成功但没返回有效文件 ID", retriable=False)
upload_id = upload_id.strip()'''
assert old in text
backup = root / 'backups/recognition-audit-20260916/ai-context/model_gateway-before-upload-validation.py'
if backup.exists():
raise RuntimeError(f'Refuse to overwrite {backup}')
shutil.copy2(path, backup)
newline = '\r\n' if b'\r\n' in raw else '\n'
path.write_bytes(text.replace(old, new, 1).replace('\n', newline).encode('utf-8'))
shutil.copy2(report / 'test_gateway_failure_audit.py', root / 'test_gateway_failure_audit.py')
manifest.append({'path': str(path), 'backup': str(backup), 'before_sha256': hashlib.sha256(raw).hexdigest(), 'after_sha256': hashlib.sha256(path.read_bytes()).hexdigest()})
(report / 'gateway-upload-validation-manifest.json').write_text(json.dumps(manifest, ensure_ascii=False, indent=2), encoding='utf-8')
print('Validated Dify upload response shape and synchronized tests in both projects.')