Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 16ee1ddfbe | |||
| d24457d9d5 | |||
| 1b42ac7765 | |||
| 65490b1aaa | |||
| f803365a20 | |||
| 70b144f1e5 | |||
| 1bbaf2f07a | |||
| 48b741984f | |||
| 5ccaeb3b96 | |||
| dfce8cb9f6 | |||
| 149e94b34b |
@@ -0,0 +1,165 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""提交信息规范校验(CI 侧权威闸门)。
|
||||||
|
|
||||||
|
规则与本机 ~/.githooks/commit-msg 完全一致,防止 --no-verify 绕过。
|
||||||
|
放行:Merge / Revert " / fixup! / squash! / amend! 开头的自动生成提交。
|
||||||
|
|
||||||
|
存量历史口径:合并一条早已推送到别的远端分支的lineage(例如把生产分支合进 main)时,
|
||||||
|
被带进来的提交并非本次新增,它们的标题不合规属于既有事实。判定为「已在其它远端分支存在」
|
||||||
|
的提交只告警、不判红;本次真正新造的提交一律照旧严格校验。
|
||||||
|
豁免必须是可证明的:先抓全部远端分支头,用 `git branch -r --contains <sha>` 取证;
|
||||||
|
抓不到 / 证不出 ⇒ 不豁免(宁可红,也不假绿)。
|
||||||
|
"""
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
|
||||||
|
TYPES = "feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert"
|
||||||
|
RE = re.compile(r"^(?:" + TYPES + r")(?:\([a-zA-Z0-9._/-]+\))?!?: .{3,}$")
|
||||||
|
ALLOW = re.compile(r'^(?:Merge |Revert "|fixup! |squash! |amend! )')
|
||||||
|
|
||||||
|
# 本次推送的分支名,用来把「当前分支自己」排除在豁免证据之外
|
||||||
|
PUSH_BRANCH = (os.environ.get("GITHUB_REF_NAME") or "").strip()
|
||||||
|
|
||||||
|
HELP = """
|
||||||
|
\u274c 提交信息不合规,已拦截。
|
||||||
|
|
||||||
|
规范 : <类型>(<范围>): <简短描述>
|
||||||
|
类型 : feat 新功能 | fix 修缺陷 | docs 文档 | style 格式(不改逻辑)
|
||||||
|
refactor 重构 | perf 性能 | test 测试 | build 构建/依赖
|
||||||
|
ci CI 配置 | chore 杂务 | revert 回滚
|
||||||
|
示例 : feat(asset): 新增按歌曲生成应援序列
|
||||||
|
fix(parser): 修复表格解析越界导致的 500
|
||||||
|
docs: 补充 OSS 短签名接口说明
|
||||||
|
|
||||||
|
要点 : 类型小写、冒号后一个空格、描述 \u22653 字符
|
||||||
|
标题 \u226472 字符、结尾不加句号
|
||||||
|
破坏性变更类型后加 !,正文写 BREAKING CHANGE:
|
||||||
|
详见 : docs/提交规范.md
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
def sh(args):
|
||||||
|
return subprocess.run(args, capture_output=True, text=True)
|
||||||
|
|
||||||
|
|
||||||
|
def collect():
|
||||||
|
"""返回 [(sha, message)]:优先 Gitea 事件负载,取不到则退回 git log。"""
|
||||||
|
items = []
|
||||||
|
ev = os.environ.get("GITHUB_EVENT_PATH") or ""
|
||||||
|
if ev and os.path.exists(ev):
|
||||||
|
try:
|
||||||
|
with open(ev, encoding="utf-8") as fh:
|
||||||
|
d = json.load(fh)
|
||||||
|
for c in (d.get("commits") or []):
|
||||||
|
m = (c.get("message") or "").strip()
|
||||||
|
if m:
|
||||||
|
items.append(((c.get("id") or "").strip(), m))
|
||||||
|
except Exception as e:
|
||||||
|
print(" 事件负载解析失败(%s),回退到 git log" % e)
|
||||||
|
if not items:
|
||||||
|
out = sh(["git", "log", "-n", "5", "--format=%H%x1f%B%x1e"]).stdout
|
||||||
|
for rec in out.split("\x1e"):
|
||||||
|
rec = rec.strip()
|
||||||
|
if not rec:
|
||||||
|
continue
|
||||||
|
sha, _, msg = rec.partition("\x1f")
|
||||||
|
if msg.strip():
|
||||||
|
items.append((sha.strip(), msg.strip()))
|
||||||
|
return items
|
||||||
|
|
||||||
|
|
||||||
|
_fetched = None
|
||||||
|
|
||||||
|
|
||||||
|
def ensure_remote_heads():
|
||||||
|
"""抓全部远端分支头(浅历史足够判断 lineage 末端)。"""
|
||||||
|
global _fetched
|
||||||
|
if _fetched is None:
|
||||||
|
r = sh(["git", "fetch", "-q", "--depth", "200", "origin", "+refs/heads/*:refs/remotes/origin/*"])
|
||||||
|
_fetched = (r.returncode == 0, (r.stderr or "").strip())
|
||||||
|
if not _fetched[0]:
|
||||||
|
print(" \u26a0 无法抓取远端分支,存量豁免不可用(将严格校验): %s" % _fetched[1][:200])
|
||||||
|
return _fetched[0]
|
||||||
|
|
||||||
|
|
||||||
|
def refs_containing(sha):
|
||||||
|
"""除本次推送分支外,还有哪些远端分支包含该提交。"""
|
||||||
|
if not ensure_remote_heads():
|
||||||
|
return []
|
||||||
|
r = sh(["git", "branch", "-r", "--contains", sha])
|
||||||
|
if r.returncode != 0:
|
||||||
|
return []
|
||||||
|
refs = []
|
||||||
|
for line in r.stdout.splitlines():
|
||||||
|
ref = line.strip().lstrip("*").strip()
|
||||||
|
if not ref or "->" in ref:
|
||||||
|
continue
|
||||||
|
if PUSH_BRANCH and ref == "origin/" + PUSH_BRANCH:
|
||||||
|
continue
|
||||||
|
refs.append(ref)
|
||||||
|
return refs
|
||||||
|
|
||||||
|
|
||||||
|
def subject_of(m):
|
||||||
|
for line in m.splitlines():
|
||||||
|
s = line.strip()
|
||||||
|
if not s or s.startswith("#"):
|
||||||
|
continue
|
||||||
|
return s
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
def judge(subj):
|
||||||
|
if not subj:
|
||||||
|
return "空提交信息"
|
||||||
|
if ALLOW.match(subj):
|
||||||
|
return ""
|
||||||
|
if len(subj) > 72:
|
||||||
|
return "标题 %d 字符 > 72" % len(subj)
|
||||||
|
if not RE.match(subj):
|
||||||
|
return "不符合 <类型>(<范围>): <描述>"
|
||||||
|
if subj.endswith(".") or subj.endswith("\u3002"):
|
||||||
|
return "标题结尾不要加句号"
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
items = collect()
|
||||||
|
bad = []
|
||||||
|
excused = []
|
||||||
|
print("待校验提交 %d 条:" % len(items))
|
||||||
|
for sha, msg in items:
|
||||||
|
subj = subject_of(msg)
|
||||||
|
why = judge(subj)
|
||||||
|
if not why:
|
||||||
|
print(" \u2705 %s" % subj)
|
||||||
|
continue
|
||||||
|
refs = refs_containing(sha) if sha else []
|
||||||
|
if refs:
|
||||||
|
excused.append((sha, subj, why, refs))
|
||||||
|
print(" \u21aa [%s] %s ← 已存在于 %s,按存量历史放行" % (why, subj, ",".join(refs[:3])))
|
||||||
|
continue
|
||||||
|
bad.append((subj, why))
|
||||||
|
print(" \u274c [%s] %s" % (why, subj))
|
||||||
|
if excused:
|
||||||
|
print("")
|
||||||
|
print("存量豁免 %d 条(提交早于本次合并,已在其它远端分支存在):" % len(excused))
|
||||||
|
for sha, subj, why, refs in excused:
|
||||||
|
print(" %s %s [%s]" % (sha[:10], subj, ",".join(refs[:3])))
|
||||||
|
try:
|
||||||
|
with open("/tmp/ci-gate-status", "w") as fh:
|
||||||
|
fh.write("failure" if bad else "success")
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
if bad:
|
||||||
|
print(HELP)
|
||||||
|
return 1
|
||||||
|
print("\u2705 提交信息全部合规")
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
sys.exit(main())
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""把 CI 结果上报给主机侧 ci-bridge(bridge 负责落报告页 + 推微信)。"""
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import urllib.request
|
||||||
|
|
||||||
|
|
||||||
|
def rd(path, default=""):
|
||||||
|
try:
|
||||||
|
with open(path, encoding="utf-8") as fh:
|
||||||
|
return fh.read().strip()
|
||||||
|
except Exception:
|
||||||
|
return default
|
||||||
|
|
||||||
|
|
||||||
|
def git(*args):
|
||||||
|
try:
|
||||||
|
return subprocess.run(["git"] + list(args), capture_output=True,
|
||||||
|
text=True).stdout.strip()
|
||||||
|
except Exception:
|
||||||
|
return ""
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
repo = os.environ.get("GITHUB_REPOSITORY", "unknown/unknown")
|
||||||
|
sha = rd("/tmp/ci-sha") or git("rev-parse", "HEAD")
|
||||||
|
branch = os.environ.get("GITHUB_REF_NAME") or \
|
||||||
|
os.environ.get("GITHUB_REF", "").replace("refs/heads/", "")
|
||||||
|
gate = rd("/tmp/ci-gate-status", "skipped")
|
||||||
|
build = rd("/tmp/ci-build-status", "failure")
|
||||||
|
status = "success" if (gate == "success" and build == "success") else "failure"
|
||||||
|
|
||||||
|
log = rd("/tmp/ci-build-tail")
|
||||||
|
if gate != "success":
|
||||||
|
log = "提交信息规范校验未通过\n" + log
|
||||||
|
|
||||||
|
try:
|
||||||
|
count = int(rd("/tmp/ci-commits-count", "1") or 1)
|
||||||
|
except Exception:
|
||||||
|
count = 1
|
||||||
|
|
||||||
|
rep = {
|
||||||
|
"repo": repo,
|
||||||
|
"sha": sha,
|
||||||
|
"branch": branch,
|
||||||
|
"actor": os.environ.get("GITHUB_ACTOR", ""),
|
||||||
|
"status": status,
|
||||||
|
"commit_subject": git("log", "-1", "--format=%s"),
|
||||||
|
"commit_count": count,
|
||||||
|
"duration_s": rd("/tmp/ci-duration", "?"),
|
||||||
|
"preview_url": rd("/tmp/ci-preview-url"),
|
||||||
|
"log": log[-3500:],
|
||||||
|
"steps": [
|
||||||
|
{"name": "提交信息规范校验", "status": gate},
|
||||||
|
{"name": "编译与自检", "status": build},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
if rep["preview_url"]:
|
||||||
|
print("\U0001F310 网页预览: %s" % rep["preview_url"])
|
||||||
|
print("结果: %s | 构建: %s | 提交闸门: %s | 耗时: %ss"
|
||||||
|
% (status, build, gate, rep["duration_s"]))
|
||||||
|
|
||||||
|
base = os.environ.get("CI_BRIDGE_RESOLVED", "").rstrip("/")
|
||||||
|
if not base:
|
||||||
|
print("跳过上报:未找到可达的 ci-bridge")
|
||||||
|
return 0
|
||||||
|
data = json.dumps(rep, ensure_ascii=False).encode("utf-8")
|
||||||
|
req = urllib.request.Request(
|
||||||
|
base + "/api/ci/report", data=data, method="POST",
|
||||||
|
headers={"Content-Type": "application/json",
|
||||||
|
"X-CI-Token": os.environ.get("BRIDGE_TOKEN", "")})
|
||||||
|
try:
|
||||||
|
with urllib.request.urlopen(req, timeout=30) as resp:
|
||||||
|
print("\u2705 已上报 ci-bridge:", resp.read().decode()[:300])
|
||||||
|
except Exception as e:
|
||||||
|
print("\u274c 上报 ci-bridge 失败:", e)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
raise SystemExit(main())
|
||||||
@@ -0,0 +1,156 @@
|
|||||||
|
name: build
|
||||||
|
|
||||||
|
# 提交 -> 本机 Runner 编译 -> 微信通知 + 网页预览
|
||||||
|
# 实测结论(勿随意改动):
|
||||||
|
# 1. 不用任何第三方 action(作业容器不联网拉 action,全用镜像自带工具)
|
||||||
|
# 2. 必须自己 clone —— Runner 不会自动检出代码,工作区初始为空
|
||||||
|
# 3. act 以 `bash -e -o pipefail` 执行每步 -> 禁止 `cmd | head` 这种会 SIGPIPE 提前退出的管道
|
||||||
|
# 4. 工具链来自主机只读挂载 /opt/citools(go/uv),缓存写 /opt/citools-cache
|
||||||
|
# 5. bridge 从容器内走 docker 网关 172.17.0.1:8899(实测可达)
|
||||||
|
# 6. 校验/上报逻辑放在 .gitea/scripts/ 下的脚本里,YAML 只负责调用(避免 YAML 里嵌深缩进代码)
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- "**"
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
env:
|
||||||
|
BRIDGE_URL: ${{ vars.CI_BRIDGE_URL }}
|
||||||
|
BRIDGE_TOKEN: ${{ secrets.CI_BRIDGE_TOKEN }}
|
||||||
|
GIT_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: build-${{ github.ref }}
|
||||||
|
cancel-in-progress: true
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
timeout-minutes: 40
|
||||||
|
steps:
|
||||||
|
- name: 检出代码
|
||||||
|
run: |
|
||||||
|
set -uo pipefail
|
||||||
|
SRV="${GITHUB_SERVER_URL:-http://192.168.50.223:3000}"
|
||||||
|
HP="${SRV#*://}"
|
||||||
|
BR="${GITHUB_REF_NAME:-${GITHUB_REF#refs/heads/}}"
|
||||||
|
cd "${GITHUB_WORKSPACE:-$(pwd)}"
|
||||||
|
rm -rf ./* .git 2>/dev/null || true
|
||||||
|
git init -q .
|
||||||
|
git remote add origin "http://x-access-token:${GIT_TOKEN}@${HP}/${GITHUB_REPOSITORY}.git"
|
||||||
|
git fetch -q --depth 50 origin "$BR"
|
||||||
|
git checkout -q -B "$BR" FETCH_HEAD
|
||||||
|
git rev-parse HEAD > /tmp/ci-sha
|
||||||
|
date +%s > /tmp/ci-start
|
||||||
|
: > /tmp/ci-preview-url
|
||||||
|
echo skipped > /tmp/ci-build-status
|
||||||
|
git rev-list --count FETCH_HEAD > /tmp/ci-commits-count 2>/dev/null || echo 1 > /tmp/ci-commits-count
|
||||||
|
echo "已检出 ${GITHUB_REPOSITORY} 分支 ${BR}"
|
||||||
|
git log -1 --format=' %h %an %s'
|
||||||
|
|
||||||
|
- name: 提交信息规范校验
|
||||||
|
run: python3 .gitea/scripts/ci_gate.py
|
||||||
|
|
||||||
|
- name: 编译与自检
|
||||||
|
run: |
|
||||||
|
set +e; set +o pipefail
|
||||||
|
LOG=/tmp/ci-build.log; : > "$LOG"
|
||||||
|
run() { echo "-- $* --"; "$@" 2>&1 | tee -a "$LOG"; return "${PIPESTATUS[0]}"; }
|
||||||
|
runin(){ local d="$1"; shift; echo "-- ($d) $* --"; ( cd "$d" && "$@" ) 2>&1 | tee -a "$LOG"; return "${PIPESTATUS[0]}"; }
|
||||||
|
|
||||||
|
export PATH=/opt/citools/go/bin:/opt/citools/bin:$PATH
|
||||||
|
export GOCACHE=/opt/citools-cache/gocache GOMODCACHE=/opt/citools-cache/gomod UV_CACHE_DIR=/opt/citools-cache/uv
|
||||||
|
export GOPROXY=https://goproxy.cn,direct GOTOOLCHAIN=local GOFLAGS=-mod=mod GOPATH=/opt/citools-cache/gopath
|
||||||
|
mkdir -p "$GOCACHE" "$GOMODCACHE" "$UV_CACHE_DIR" "$GOPATH" 2>/dev/null
|
||||||
|
|
||||||
|
echo "工具链: $(go version 2>&1) | $(uv --version 2>&1) | $(node -v 2>&1) | $(python3 -V 2>&1)"
|
||||||
|
fail=0; ran=""
|
||||||
|
|
||||||
|
if [ -f go.mod ]; then
|
||||||
|
ran="$ran go"
|
||||||
|
run go build ./... || fail=1
|
||||||
|
run go vet ./... || fail=1
|
||||||
|
run go test ./... || fail=1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -f pyproject.toml ] || [ -f requirements.txt ] || [ -f setup.py ]; then
|
||||||
|
ran="$ran python"
|
||||||
|
if grep -qE '^check:' Makefile 2>/dev/null; then
|
||||||
|
run make UV=uv check || fail=1
|
||||||
|
else
|
||||||
|
run uv sync || fail=1
|
||||||
|
run uv run pytest -q || fail=1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
WEBPKG=""
|
||||||
|
for p in package.json web/package.json frontend/package.json ui/package.json; do
|
||||||
|
if [ -f "$p" ]; then WEBPKG="$p"; break; fi
|
||||||
|
done
|
||||||
|
if [ -n "$WEBPKG" ]; then
|
||||||
|
ran="$ran node"
|
||||||
|
D="$(dirname "$WEBPKG")"
|
||||||
|
if [ -f "$D/package-lock.json" ]; then
|
||||||
|
runin "$D" npm ci || fail=1
|
||||||
|
else
|
||||||
|
runin "$D" npm install || fail=1
|
||||||
|
fi
|
||||||
|
if grep -q '"build"[[:space:]]*:' "$WEBPKG" 2>/dev/null; then
|
||||||
|
runin "$D" npm run build || fail=1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z "$ran" ] && [ -f Makefile ]; then
|
||||||
|
ran="$ran make"
|
||||||
|
run make || fail=1
|
||||||
|
fi
|
||||||
|
if [ -z "$ran" ]; then
|
||||||
|
echo "未识别到构建入口(go.mod / pyproject.toml / package.json / Makefile 都没有),跳过编译"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ "$fail" -eq 0 ]; then echo success > /tmp/ci-build-status; else echo failure > /tmp/ci-build-status; fi
|
||||||
|
tail -c 4000 "$LOG" > /tmp/ci-build-tail
|
||||||
|
echo "构建结果: $(cat /tmp/ci-build-status)(识别:$ran)"
|
||||||
|
[ "$fail" -eq 0 ] || exit 1
|
||||||
|
|
||||||
|
- name: 发布网页预览
|
||||||
|
if: always()
|
||||||
|
run: |
|
||||||
|
set +e; set +o pipefail
|
||||||
|
: > /tmp/ci-preview-url
|
||||||
|
if [ "$(cat /tmp/ci-build-status 2>/dev/null)" != "success" ]; then
|
||||||
|
echo "构建未成功,跳过预览发布"; exit 0
|
||||||
|
fi
|
||||||
|
WEB=""
|
||||||
|
for d in web/dist dist build out public web/build frontend/dist; do
|
||||||
|
if [ -f "$d/index.html" ]; then WEB="$d"; break; fi
|
||||||
|
done
|
||||||
|
if [ -z "$WEB" ]; then echo "未发现静态网页产物目录,跳过预览发布"; exit 0; fi
|
||||||
|
tar -czf /tmp/ci-web.tgz -C "$WEB" . || { echo "打包失败"; exit 0; }
|
||||||
|
B="${BRIDGE_URL:-}"
|
||||||
|
for cand in "$B" "http://172.17.0.1:8899" "http://192.168.50.134:8899" "http://100.92.49.78:8899"; do
|
||||||
|
if [ -n "$cand" ]; then
|
||||||
|
curl -s -m 5 -o /dev/null "$cand/healthz" && { B="$cand"; break; }
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
SHA="$(cat /tmp/ci-sha)"
|
||||||
|
echo "发布预览到 $B(产物目录 $WEB)"
|
||||||
|
curl -sS -m 90 -X POST "$B/api/ci/publish?repo=${GITHUB_REPOSITORY}&sha=${SHA}" -H "X-CI-Token: ${BRIDGE_TOKEN}" -H 'Content-Type: application/gzip' --data-binary @/tmp/ci-web.tgz -o /tmp/ci-pub.json
|
||||||
|
cat /tmp/ci-pub.json; echo
|
||||||
|
URL="$(python3 -c "import json;print(json.load(open('/tmp/ci-pub.json')).get('url',''))" 2>/dev/null)"
|
||||||
|
if [ -n "$URL" ]; then echo "$URL" > /tmp/ci-preview-url; echo "网页预览: $URL"; fi
|
||||||
|
|
||||||
|
- name: 通知微信
|
||||||
|
if: always()
|
||||||
|
run: |
|
||||||
|
set +e; set +o pipefail
|
||||||
|
B="${BRIDGE_URL:-}"
|
||||||
|
for cand in "$B" "http://172.17.0.1:8899" "http://192.168.50.134:8899" "http://100.92.49.78:8899"; do
|
||||||
|
if [ -n "$cand" ]; then
|
||||||
|
curl -s -m 5 -o /dev/null "$cand/healthz" && { B="$cand"; break; }
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
echo "使用 ci-bridge: $B"
|
||||||
|
S=$(date +%s); T=$(cat /tmp/ci-start 2>/dev/null); [ -n "$T" ] || T=$S
|
||||||
|
echo $((S - T)) > /tmp/ci-duration
|
||||||
|
CI_BRIDGE_RESOLVED="$B" python3 .gitea/scripts/ci_report.py
|
||||||
+4
-1
@@ -71,7 +71,10 @@ func main() {
|
|||||||
if strings.TrimSpace(settingsRuntime.clientID) == "" || strings.TrimSpace(settingsRuntime.secret) == "" {
|
if strings.TrimSpace(settingsRuntime.clientID) == "" || strings.TrimSpace(settingsRuntime.secret) == "" {
|
||||||
log.Fatal("DingTalk credentials are required; configure DINGTALK_CLIENT_ID and DINGTALK_CLIENT_SECRET in the protected runtime environment")
|
log.Fatal("DingTalk credentials are required; configure DINGTALK_CLIENT_ID and DINGTALK_CLIENT_SECRET in the protected runtime environment")
|
||||||
}
|
}
|
||||||
client, err := weknora.NewClient(os.Getenv("WEKNORA_BASE_URL"), os.Getenv("WEKNORA_API_KEY"), 120*time.Second)
|
// 0 = no client-side total timeout. WeKnora's smart-reasoning agent can run
|
||||||
|
// 20+ tool rounds and stream far longer than 120s; the total-time cap cut the
|
||||||
|
// SSE mid-answer and the failure branch overwrote the streamed card.
|
||||||
|
client, err := weknora.NewClient(os.Getenv("WEKNORA_BASE_URL"), os.Getenv("WEKNORA_API_KEY"), 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,118 @@
|
|||||||
|
// 临时探针(不入库,用完即删):用真实 WeKnora + OSS 端点跑
|
||||||
|
// internal/outboundimage 的改写链路,量化"一条含 N 张真实图的答案"在
|
||||||
|
// 并发取图 + 预算可配下的出图结果与耗时。
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"context"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/outboundimage"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
refsFile := flag.String("refs-file", "", "文件:每行一个 resource:// 句柄")
|
||||||
|
budget := flag.Duration("budget", 180*time.Second, "改写预算")
|
||||||
|
maxImages := flag.Int("max", 50, "图片上限")
|
||||||
|
compress := flag.Bool("compress", true, "上传前压缩截图")
|
||||||
|
passes := flag.Int("passes", 1, "重复改写轮数(第二轮应命中已存在对象)")
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
refs := readRefs(*refsFile)
|
||||||
|
if len(refs) == 0 {
|
||||||
|
log.Fatal("probe: no references")
|
||||||
|
}
|
||||||
|
|
||||||
|
fetcher, err := outboundimage.NewWeKnoraFetcher(
|
||||||
|
os.Getenv("WEKNORA_BASE_URL"), os.Getenv("WEKNORA_API_KEY"),
|
||||||
|
&http.Client{Timeout: 30 * time.Second}, 10<<20)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("probe: fetcher: %v", err)
|
||||||
|
}
|
||||||
|
store, err := outboundimage.NewAliyunOSSStore(
|
||||||
|
os.Getenv("DOBOTAI_OUTBOUND_IMAGE_OSS_ENDPOINT"),
|
||||||
|
os.Getenv("DOBOTAI_OUTBOUND_IMAGE_OSS_REGION"),
|
||||||
|
os.Getenv("DOBOTAI_OUTBOUND_IMAGE_OSS_ACCESS_KEY_ID"),
|
||||||
|
os.Getenv("DOBOTAI_OUTBOUND_IMAGE_OSS_ACCESS_KEY_SECRET"),
|
||||||
|
os.Getenv("DOBOTAI_OUTBOUND_IMAGE_OSS_BUCKET"))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("probe: store: %v", err)
|
||||||
|
}
|
||||||
|
publicBase := os.Getenv("DOBOTAI_OUTBOUND_IMAGE_PUBLIC_BASE_URL")
|
||||||
|
publisher, err := outboundimage.NewStableHashPublisher(store, "dobotai/im-images", publicBase)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("probe: publisher: %v", err)
|
||||||
|
}
|
||||||
|
rewriter := outboundimage.NewRewriter(fetcher, publisher, *maxImages, outboundimage.WithImageCompression(*compress))
|
||||||
|
|
||||||
|
var answer strings.Builder
|
||||||
|
answer.WriteString("## 探针答案\n")
|
||||||
|
for i, ref := range refs {
|
||||||
|
fmt.Fprintf(&answer, "\n\n", i+1, ref)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("probe: refs=%d budget=%s max=%d compress=%v passes=%d bucket=%s", len(refs), *budget, *maxImages, *compress, *passes, os.Getenv("DOBOTAI_OUTBOUND_IMAGE_OSS_BUCKET"))
|
||||||
|
body := answer.String()
|
||||||
|
for pass := 1; pass <= *passes; pass++ {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), *budget)
|
||||||
|
started := time.Now()
|
||||||
|
out := rewriter.Rewrite(ctx, body)
|
||||||
|
elapsed := time.Since(started)
|
||||||
|
cancel()
|
||||||
|
|
||||||
|
published := regexp.MustCompile(`!\[图\d+\]\(https://`).FindAllString(out, -1)
|
||||||
|
leaked := strings.Count(out, "resource://")
|
||||||
|
fmt.Printf("RESULT pass=%d elapsed=%s refs=%d published=%d dropped=%d residual_resource_refs=%d out_bytes=%d\n",
|
||||||
|
pass, elapsed.Round(time.Millisecond), len(refs), len(published), len(refs)-len(published), leaked, len(out))
|
||||||
|
if len(published) > 0 {
|
||||||
|
fmt.Printf("FIRST_URL_%d=%s\nLAST_URL_%d=%s\n", pass, firstImageURL(out), pass, lastImageURL(out))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func readRefs(path string) []string {
|
||||||
|
if path == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
file, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("probe: refs file: %v", err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
seen := map[string]bool{}
|
||||||
|
refs := make([]string, 0, 64)
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
ref := strings.TrimSpace(scanner.Text())
|
||||||
|
if !strings.HasPrefix(ref, "resource://") || seen[ref] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seen[ref] = true
|
||||||
|
refs = append(refs, ref)
|
||||||
|
}
|
||||||
|
return refs
|
||||||
|
}
|
||||||
|
|
||||||
|
var imageURLRE = regexp.MustCompile(`!\[图\d+\]\((https://[^)]+)\)`)
|
||||||
|
|
||||||
|
func firstImageURL(content string) string {
|
||||||
|
if match := imageURLRE.FindStringSubmatch(content); len(match) > 1 {
|
||||||
|
return match[1]
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func lastImageURL(content string) string {
|
||||||
|
if matches := imageURLRE.FindAllStringSubmatch(content, -1); len(matches) > 0 {
|
||||||
|
return matches[len(matches)-1][1]
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
|
|
||||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/core"
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/core"
|
||||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/directory"
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/directory"
|
||||||
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/outboundtext"
|
||||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/reasoningmode"
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/reasoningmode"
|
||||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/weknora"
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/weknora"
|
||||||
)
|
)
|
||||||
@@ -675,6 +676,7 @@ func (h *Handler) finalizeCardOrMarkdown(ctx context.Context, webhookURL string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) rewriteOutboundContent(ctx context.Context, content string) string {
|
func (h *Handler) rewriteOutboundContent(ctx context.Context, content string) string {
|
||||||
|
content = outboundtext.NormalizeMath(content)
|
||||||
options := tableNormalizeOptions{groupWideTables: h.groupTablesFinal}
|
options := tableNormalizeOptions{groupWideTables: h.groupTablesFinal}
|
||||||
if h.outboundRewriter == nil {
|
if h.outboundRewriter == nil {
|
||||||
return normalizeMarkdownTables(sanitizeDisplayAnswer(content), options)
|
return normalizeMarkdownTables(sanitizeDisplayAnswer(content), options)
|
||||||
@@ -692,6 +694,7 @@ func (h *Handler) rewriteOutboundContent(ctx context.Context, content string) st
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) sanitizeOutboundPartial(ctx context.Context, content string) string {
|
func (h *Handler) sanitizeOutboundPartial(ctx context.Context, content string) string {
|
||||||
|
content = outboundtext.NormalizeMath(content)
|
||||||
sanitizer, ok := h.outboundRewriter.(OutboundPartialSanitizer)
|
sanitizer, ok := h.outboundRewriter.(OutboundPartialSanitizer)
|
||||||
if ok {
|
if ok {
|
||||||
content = sanitizer.SanitizePartial(content)
|
content = sanitizer.SanitizePartial(content)
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package dingtalk
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/outboundimage"
|
||||||
|
)
|
||||||
|
|
||||||
|
type diagFetcher struct{}
|
||||||
|
|
||||||
|
func (diagFetcher) Fetch(ctx context.Context, ref string) ([]byte, string, error) {
|
||||||
|
return []byte("PNGDATA"), "image/png", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type diagPublisher struct{}
|
||||||
|
|
||||||
|
func (diagPublisher) Publish(ctx context.Context, data []byte, contentType string) (string, error) {
|
||||||
|
return "https://weknora-api-test.dobot.cn/dingtalk/i/a87f73b8-15cb-546e-ba53-bf93dade916f", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// The outbound rewrite must publish a resource image and keep it in the card
|
||||||
|
// body. A reference whose handle contains "_x" (…ZM_pltQ) used to be mangled by
|
||||||
|
// the math normalizer, which made the image unpublishable and the sanitizer
|
||||||
|
// drop the whole line.
|
||||||
|
func TestRewriteOutboundContentKeepsPublishedImage(t *testing.T) {
|
||||||
|
content := "\n\n公式:$r = \\sqrt{X^2 + Y^2}$\n"
|
||||||
|
h := &Handler{
|
||||||
|
outboundRewriter: outboundimage.NewRewriter(diagFetcher{}, diagPublisher{}, 50),
|
||||||
|
groupTablesFinal: true,
|
||||||
|
}
|
||||||
|
out := h.rewriteOutboundContent(context.Background(), content)
|
||||||
|
if !strings.Contains(out, " {
|
||||||
|
t.Fatalf("published image missing from card content: %q", out)
|
||||||
|
}
|
||||||
|
if !strings.Contains(out, "r = √(X² + Y²)") {
|
||||||
|
t.Fatalf("formula not converted: %q", out)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
package outboundtext
|
||||||
|
|
||||||
|
import (
|
||||||
|
"regexp"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DingTalk cards render a small Markdown subset and no math typesetting, so a
|
||||||
|
// LaTeX formula reaches the user as raw source ($r = \sqrt{X^2 + Y^2}$).
|
||||||
|
// NormalizeMath rewrites such formulas into plain Unicode text that the card
|
||||||
|
// can show. It only touches content that actually carries math delimiters or
|
||||||
|
// well-known math commands, so ordinary prose is left byte-identical.
|
||||||
|
|
||||||
|
var (
|
||||||
|
mathBlockDelimRE = regexp.MustCompile(`\$\$([\s\S]+?)\$\$`)
|
||||||
|
mathSpanRE = regexp.MustCompile(`(?s)\$\$.+?\$\$|\\\[.+?\\\]|\\\(.+?\\\)|\$[^$\n]{1,400}\$`)
|
||||||
|
parenDelimRE = regexp.MustCompile(`\\\(([\s\S]+?)\\\)`)
|
||||||
|
bracketDelimRE = regexp.MustCompile(`\\\[([\s\S]+?)\\\]`)
|
||||||
|
sqrtRE = regexp.MustCompile(`\\sqrt\s*(?:\[([^\]]{1,20})\])?\s*\{([^{}]{1,200})\}`)
|
||||||
|
fracRE = regexp.MustCompile(`\\frac\s*\{([^{}]{1,200})\}\s*\{([^{}]{1,200})\}`)
|
||||||
|
supBracedRE = regexp.MustCompile(`\^\s*\{([^{}]{1,40})\}`)
|
||||||
|
subBracedRE = regexp.MustCompile(`_\s*\{([^{}]{1,40})\}`)
|
||||||
|
supCharRE = regexp.MustCompile(`\^([0-9A-Za-z+\-=()])`)
|
||||||
|
subCharRE = regexp.MustCompile(`_([0-9A-Za-z+\-=()])`)
|
||||||
|
commandRE = regexp.MustCompile(`\\([A-Za-z]+)`)
|
||||||
|
braceDelimRE = regexp.MustCompile(`\{([^{}]{0,200})\}`)
|
||||||
|
blankRunRE = regexp.MustCompile(`[ \t]{2,}`)
|
||||||
|
)
|
||||||
|
|
||||||
|
var mathCommands = map[string]string{
|
||||||
|
"times": "×", "cdot": "·", "div": "÷", "pm": "±", "mp": "∓",
|
||||||
|
"leq": "≤", "le": "≤", "geq": "≥", "ge": "≥", "neq": "≠", "ne": "≠",
|
||||||
|
"approx": "≈", "sim": "∼", "simeq": "≃", "equiv": "≡", "propto": "∝",
|
||||||
|
"infty": "∞", "partial": "∂", "nabla": "∇",
|
||||||
|
"alpha": "α", "beta": "β", "gamma": "γ", "delta": "δ", "Delta": "Δ",
|
||||||
|
"epsilon": "ε", "theta": "θ", "lambda": "λ", "mu": "μ", "rho": "ρ",
|
||||||
|
"sigma": "σ", "tau": "τ", "phi": "φ", "omega": "ω", "Omega": "Ω",
|
||||||
|
"eta": "η", "kappa": "κ", "nu": "ν", "xi": "ξ", "zeta": "ζ", "psi": "ψ",
|
||||||
|
"sum": "Σ", "prod": "Π", "int": "∫", "iint": "∬", "oint": "∮",
|
||||||
|
"in": "∈", "notin": "∉", "subset": "⊂", "subseteq": "⊆", "cup": "∪", "cap": "∩",
|
||||||
|
"rightarrow": "→", "to": "→", "longrightarrow": "→", "leftarrow": "←",
|
||||||
|
"Rightarrow": "⇒", "leftrightarrow": "↔", "mapsto": "↦",
|
||||||
|
"forall": "∀", "exists": "∃", "angle": "∠", "degree": "°", "circ": "°",
|
||||||
|
"perp": "⊥", "parallel": "∥", "therefore": "∴", "because": "∵",
|
||||||
|
"ldots": "…", "cdots": "…", "dots": "…", "prime": "′",
|
||||||
|
"quad": " ", "qquad": " ", "limits": "", "displaystyle": "",
|
||||||
|
"left": "", "right": "", "bmod": "mod ", "pmod": "mod ",
|
||||||
|
"text": "", "mathrm": "", "mathit": "", "operatorname": "",
|
||||||
|
"overline": "", "underline": "",
|
||||||
|
}
|
||||||
|
|
||||||
|
var escapedPunctuation = map[string]string{
|
||||||
|
`\\`: "\n", `\%`: "%", `\&`: "&", `\#`: "#", `\_`: "_", `\$`: "$",
|
||||||
|
`\{`: "{", `\}`: "}", `\,`: " ", `\;`: " ", `\!`: "",
|
||||||
|
`\(`: "", `\)`: "", `\[`: "", `\]`: "",
|
||||||
|
}
|
||||||
@@ -0,0 +1,128 @@
|
|||||||
|
package outboundtext
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
func NormalizeMath(markdown string) string {
|
||||||
|
if !carriesMath(markdown) {
|
||||||
|
return markdown
|
||||||
|
}
|
||||||
|
content := mathSpanRE.ReplaceAllStringFunc(markdown, convertMathSpan)
|
||||||
|
// Outside math spans only LaTeX commands are converted. Identifiers and URLs
|
||||||
|
// (resource://…_pltQ) must never be rewritten: a mangled reference can no
|
||||||
|
// longer be published, and the outbound sanitizer then drops the whole image.
|
||||||
|
content = commandRE.ReplaceAllStringFunc(content, replaceCommand)
|
||||||
|
content = blankRunRE.ReplaceAllString(content, " ")
|
||||||
|
return content
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertMathSpan(span string) string {
|
||||||
|
inner := span
|
||||||
|
switch {
|
||||||
|
case strings.HasPrefix(inner, "$$"):
|
||||||
|
inner = strings.TrimSuffix(strings.TrimPrefix(inner, "$$"), "$$")
|
||||||
|
return "\n" + convertMath(inner) + "\n"
|
||||||
|
case strings.HasPrefix(inner, `\[`):
|
||||||
|
inner = strings.TrimSuffix(strings.TrimPrefix(inner, `\[`), `\]`)
|
||||||
|
case strings.HasPrefix(inner, `\(`):
|
||||||
|
inner = strings.TrimSuffix(strings.TrimPrefix(inner, `\(`), `\)`)
|
||||||
|
default:
|
||||||
|
inner = strings.TrimSuffix(strings.TrimPrefix(inner, "$"), "$")
|
||||||
|
}
|
||||||
|
return convertMath(inner)
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertMath(inner string) string {
|
||||||
|
content := inner
|
||||||
|
for from, to := range escapedPunctuation {
|
||||||
|
content = strings.ReplaceAll(content, from, to)
|
||||||
|
}
|
||||||
|
for i := 0; i < 3; i++ {
|
||||||
|
next := sqrtRE.ReplaceAllStringFunc(content, expandRoot)
|
||||||
|
next = fracRE.ReplaceAllString(next, "($1)/($2)")
|
||||||
|
next = supBracedRE.ReplaceAllStringFunc(next, convertSuperscript)
|
||||||
|
next = subBracedRE.ReplaceAllStringFunc(next, convertSubscript)
|
||||||
|
next = supCharRE.ReplaceAllStringFunc(next, convertSuperscript)
|
||||||
|
next = subCharRE.ReplaceAllStringFunc(next, convertSubscript)
|
||||||
|
if next == content {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
content = next
|
||||||
|
}
|
||||||
|
content = commandRE.ReplaceAllStringFunc(content, replaceCommand)
|
||||||
|
content = braceDelimRE.ReplaceAllString(content, "$1")
|
||||||
|
return content
|
||||||
|
}
|
||||||
|
|
||||||
|
func carriesMath(markdown string) bool {
|
||||||
|
if !strings.Contains(markdown, "$") && !strings.Contains(markdown, `\(`) &&
|
||||||
|
!strings.Contains(markdown, `\[`) && !strings.Contains(markdown, `\sqrt`) &&
|
||||||
|
!strings.Contains(markdown, `\frac`) && !strings.Contains(markdown, `\times`) &&
|
||||||
|
!strings.Contains(markdown, `\times`) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func expandRoot(match string) string {
|
||||||
|
parts := sqrtRE.FindStringSubmatch(match)
|
||||||
|
degree, radicand := strings.TrimSpace(parts[1]), strings.TrimSpace(parts[2])
|
||||||
|
if degree != "" {
|
||||||
|
return "√[" + degree + "](" + radicand + ")"
|
||||||
|
}
|
||||||
|
return "√(" + radicand + ")"
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertSuperscript(match string) string {
|
||||||
|
body := strings.TrimPrefix(match, "^")
|
||||||
|
body = strings.TrimSuffix(strings.TrimPrefix(body, "{"), "}")
|
||||||
|
if mapped, ok := mapRun(body, superscripts); ok {
|
||||||
|
return mapped
|
||||||
|
}
|
||||||
|
return "^(" + body + ")"
|
||||||
|
}
|
||||||
|
|
||||||
|
func convertSubscript(match string) string {
|
||||||
|
body := strings.TrimPrefix(match, "_")
|
||||||
|
body = strings.TrimSuffix(strings.TrimPrefix(body, "{"), "}")
|
||||||
|
if mapped, ok := mapRun(body, subscripts); ok {
|
||||||
|
return mapped
|
||||||
|
}
|
||||||
|
return "_(" + body + ")"
|
||||||
|
}
|
||||||
|
|
||||||
|
func mapRun(body string, table map[rune]string) (string, bool) {
|
||||||
|
if body == "" {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
var b strings.Builder
|
||||||
|
for _, r := range body {
|
||||||
|
mapped, ok := table[r]
|
||||||
|
if !ok {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
b.WriteString(mapped)
|
||||||
|
}
|
||||||
|
return b.String(), true
|
||||||
|
}
|
||||||
|
|
||||||
|
func replaceCommand(match string) string {
|
||||||
|
name := strings.TrimPrefix(match, `\`)
|
||||||
|
if mapped, ok := mathCommands[name]; ok {
|
||||||
|
return mapped
|
||||||
|
}
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
|
||||||
|
var superscripts = map[rune]string{
|
||||||
|
'0': "⁰", '1': "¹", '2': "²", '3': "³", '4': "⁴", '5': "⁵", '6': "⁶",
|
||||||
|
'7': "⁷", '8': "⁸", '9': "⁹", '+': "⁺", '-': "⁻", '=': "⁼",
|
||||||
|
'(': "⁽", ')': "⁾", 'n': "ⁿ", 'i': "ⁱ", 'T': "ᵀ",
|
||||||
|
}
|
||||||
|
|
||||||
|
var subscripts = map[rune]string{
|
||||||
|
'0': "₀", '1': "₁", '2': "₂", '3': "₃", '4': "₄", '5': "₅", '6': "₆",
|
||||||
|
'7': "₇", '8': "₈", '9': "₉", '+': "₊", '-': "₋", '=': "₌",
|
||||||
|
'(': "₍", ')': "₎", 'a': "ₐ", 'e': "ₑ", 'i': "ᵢ", 'j': "ⱼ", 'k': "ₖ",
|
||||||
|
'n': "ₙ", 'o': "ₒ", 'p': "ₚ", 'r': "ᵣ", 's': "ₛ", 't': "ₜ", 'u': "ᵤ",
|
||||||
|
'v': "ᵥ", 'x': "ₓ",
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package outboundtext
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestNormalizeMath(t *testing.T) {
|
||||||
|
cases := []struct{ name, in, want string }{
|
||||||
|
{"inline sqrt", `$r = \sqrt{X^2 + Y^2}$`, `r = √(X² + Y²)`},
|
||||||
|
{"inline inside prose", `计算公式为 $r = \sqrt{X^2 + Y^2}$(单位:cm)`, `计算公式为 r = √(X² + Y²)(单位:cm)`},
|
||||||
|
{"block frac times", `$$\frac{a}{b} \times c$$`, "\n(a)/(b) × c\n"},
|
||||||
|
{"paren delimiters", `\(x \leq y\)`, `x ≤ y`},
|
||||||
|
{"bracket delimiters", `\[ \sum_{i=1}^{n} x_i \]`, ` Σᵢ₌₁ⁿ xᵢ `},
|
||||||
|
{"nth root", `$\sqrt[3]{V}$`, `√[3](V)`},
|
||||||
|
{"no math untouched", `C:\new\folder 与 100% 覆盖`, `C:\new\folder 与 100% 覆盖`},
|
||||||
|
{"dollar free prose", `价格是 5 元, 无公式`, `价格是 5 元, 无公式`},
|
||||||
|
{"resource url untouched", ` 公式 $r = \sqrt{X^2 + Y^2}$`, ` 公式 r = √(X² + Y²)`},
|
||||||
|
{"code span untouched", "`x_1` 与 $x_1$", "`x_1` 与 x₁"},
|
||||||
|
}
|
||||||
|
for _, tc := range cases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
got := NormalizeMath(tc.in)
|
||||||
|
if got != tc.want {
|
||||||
|
t.Fatalf("NormalizeMath(%q) = %q, want %q", tc.in, got, tc.want)
|
||||||
|
}
|
||||||
|
if again := NormalizeMath(got); again != got {
|
||||||
|
t.Fatalf("NormalizeMath is not idempotent: %q -> %q", got, again)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNormalizeMathKeepsPlainMarkdown(t *testing.T) {
|
||||||
|
plain := "### 标题\n\n- 项目一\n- 项目二\n\n普通段落,没有公式。"
|
||||||
|
if got := NormalizeMath(plain); got != plain {
|
||||||
|
t.Fatalf("plain markdown changed: %q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -68,3 +68,86 @@ func TestAskEventsQuickKeepsFullAnswer(t *testing.T) {
|
|||||||
t.Fatalf("quick answer must be untouched, got %q", result.Answer)
|
t.Fatalf("quick answer must be untouched, got %q", result.Answer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Production shape (turn efae317e, 给我CRA5的负载图): the agent narrates its
|
||||||
|
// steps, then publishes the answer — lead-in, image and all — as its own answer
|
||||||
|
// run. Cutting at the first structural line threw the lead-in and the image
|
||||||
|
// away; the run boundary keeps them.
|
||||||
|
func cra5Fixture() (string, string, string) {
|
||||||
|
narration := "用户想要 CRA5(CR5A)的末端负载曲线图。让我先在知识库中搜索相关的产品手册和 FAQ。"
|
||||||
|
body := "我已经找到了 CRA5(CR5A)的末端负载曲线图,并确认了负载曲线的读法说明。下面给出完整回答。\n\n" +
|
||||||
|
"CRA5(即 CR5A)的末端负载曲线如下:\n\n" +
|
||||||
|
"\n\n" +
|
||||||
|
"---\n\n" +
|
||||||
|
"## 一、负载曲线怎么读\n\n" +
|
||||||
|
"负载曲线反映的是:末端负载能力与负载质心偏心距离成负相关。"
|
||||||
|
return narration, body, narration + body
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAskEventsReasoningKeepsLeadInAndImageFromAnswerRun(t *testing.T) {
|
||||||
|
narration, body, answer := cra5Fixture()
|
||||||
|
// The heuristic on its own drops the lead-in and the image; the observation
|
||||||
|
// must win over it.
|
||||||
|
if heuristic := reasoningmode.TrimReasoningNarration(answer); heuristic == body {
|
||||||
|
t.Fatal("fixture no longer exposes the heuristic defect")
|
||||||
|
}
|
||||||
|
repo := newMemoryRepository()
|
||||||
|
client := &fakeEventStreamingWeKnora{
|
||||||
|
fakeWeKnora: &fakeWeKnora{},
|
||||||
|
events: []weknora.StreamEvent{
|
||||||
|
{ResponseType: "answer", FullAnswer: narration, AnswerRunID: "step-1-answer", AnswerRunStart: 0},
|
||||||
|
{ResponseType: "tool_call", ToolName: "knowledge_search"},
|
||||||
|
{ResponseType: "tool_result", ToolName: "knowledge_search"},
|
||||||
|
{ResponseType: "thinking"},
|
||||||
|
{ResponseType: "answer", FullAnswer: answer, Done: true, AnswerRunID: "final-answer", AnswerRunStart: len(narration)},
|
||||||
|
},
|
||||||
|
streamResult: weknora.ChatResult{RequestID: "request-cra5", Answer: answer},
|
||||||
|
}
|
||||||
|
service, err := New(repo, fixedResolver{}, client, session.GroupShared)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
req := streamQARequest("message-cra5")
|
||||||
|
req.ReasoningExecution = string(reasoningmode.ExecutionReasoning)
|
||||||
|
result, err := service.AskEvents(context.Background(), req, func(weknora.StreamEvent) {})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("AskEvents: %v", err)
|
||||||
|
}
|
||||||
|
if result.Answer != body {
|
||||||
|
t.Fatalf("delivered answer must keep the answer body (lead-in + image), got %q", result.Answer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAskEventsReasoningFallsBackWhenFinalRunInterrupted(t *testing.T) {
|
||||||
|
_, _, answer := reasoningAnswerFixture()
|
||||||
|
narration := "我需要先确认这台设备的型号和手册。"
|
||||||
|
repo := newMemoryRepository()
|
||||||
|
client := &fakeEventStreamingWeKnora{
|
||||||
|
fakeWeKnora: &fakeWeKnora{},
|
||||||
|
events: []weknora.StreamEvent{
|
||||||
|
{ResponseType: "answer", FullAnswer: narration, AnswerRunID: "step-1-answer", AnswerRunStart: 0},
|
||||||
|
{ResponseType: "tool_call", ToolName: "knowledge_search"},
|
||||||
|
{ResponseType: "tool_result", ToolName: "knowledge_search"},
|
||||||
|
// The last answer run keeps running across another tool call, so the
|
||||||
|
// stream cannot prove where the body starts: fall back to the heuristic.
|
||||||
|
{ResponseType: "answer", FullAnswer: answer, AnswerRunID: "step-2-answer", AnswerRunStart: len(narration)},
|
||||||
|
{ResponseType: "tool_call", ToolName: "knowledge_search"},
|
||||||
|
{ResponseType: "tool_result", ToolName: "knowledge_search"},
|
||||||
|
{ResponseType: "answer", FullAnswer: answer, Done: true, AnswerRunID: "step-2-answer", AnswerRunStart: len(narration)},
|
||||||
|
},
|
||||||
|
streamResult: weknora.ChatResult{RequestID: "request-interrupted", Answer: answer},
|
||||||
|
}
|
||||||
|
service, err := New(repo, fixedResolver{}, client, session.GroupShared)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
req := streamQARequest("message-interrupted")
|
||||||
|
req.ReasoningExecution = string(reasoningmode.ExecutionReasoning)
|
||||||
|
result, err := service.AskEvents(context.Background(), req, func(weknora.StreamEvent) {})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("AskEvents: %v", err)
|
||||||
|
}
|
||||||
|
if want := reasoningmode.TrimReasoningNarration(answer); result.Answer != want {
|
||||||
|
t.Fatalf("interrupted run must fall back to the heuristic: want %q, got %q", want, result.Answer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,102 @@
|
|||||||
|
package qa
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/reasoningmode"
|
||||||
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/weknora"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestReplayRecordedStreamDiagnostics replays a recorded WeKnora SSE stream
|
||||||
|
// (path in WK_RAW_STREAM) through the real client and prints what the narration
|
||||||
|
// observation sees. Temporary diagnostic, removed after the investigation.
|
||||||
|
func TestReplayRecordedStreamDiagnostics(t *testing.T) {
|
||||||
|
path := os.Getenv("WK_RAW_STREAM")
|
||||||
|
if path == "" {
|
||||||
|
t.Skip("WK_RAW_STREAM not set")
|
||||||
|
}
|
||||||
|
raw, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("read stream: %v", err)
|
||||||
|
}
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "text/event-stream")
|
||||||
|
_, _ = w.Write(raw)
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
client, err := weknora.NewClient(server.URL, "test-key", 30*time.Second)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("client: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var observation reasoningmode.NarrationObservation
|
||||||
|
runStarts := []string{}
|
||||||
|
eventTypes := map[string]int{}
|
||||||
|
observer := func(e weknora.StreamEvent) {
|
||||||
|
eventTypes[e.ResponseType]++
|
||||||
|
switch e.ResponseType {
|
||||||
|
case "answer":
|
||||||
|
if e.AnswerRunID != "" {
|
||||||
|
observation.Observed = true
|
||||||
|
}
|
||||||
|
if e.AnswerRunID != "" && e.AnswerRunID != lastRunID(runStarts) {
|
||||||
|
runStarts = append(runStarts, fmt.Sprintf("%s@%d", e.AnswerRunID, e.AnswerRunStart))
|
||||||
|
observation.AnswerRuns++
|
||||||
|
observation.LastRunStart = e.AnswerRunStart
|
||||||
|
observation.LastRunClean = true
|
||||||
|
}
|
||||||
|
case "thinking", "tool_call", "tool_result":
|
||||||
|
observation.ToolEvents++
|
||||||
|
observation.LastRunClean = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := client.AgentChatEvents(context.Background(), "session", weknora.AgentChatRequest{AgentID: "agent", Query: "q"}, observer)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("agent chat events: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("eventTypes=%v", eventTypes)
|
||||||
|
t.Logf("runs=%v", runStarts)
|
||||||
|
t.Logf("observation: observed=%v runs=%d tools=%d lastRunStart=%d lastRunClean=%v",
|
||||||
|
observation.Observed, observation.AnswerRuns, observation.ToolEvents, observation.LastRunStart, observation.LastRunClean)
|
||||||
|
|
||||||
|
content := result.Answer
|
||||||
|
observed := reasoningmode.TrimReasoningNarrationObserved(content, observation)
|
||||||
|
heuristic := reasoningmode.TrimReasoningNarration(content)
|
||||||
|
t.Logf("contentLen=%d observedKept=%d heuristicKept=%d", len(content), len(observed), len(heuristic))
|
||||||
|
if n := len(content) - len(observed); n > 0 {
|
||||||
|
t.Logf("observedDropPrefix=%q", firstRunes(content[:n], 160))
|
||||||
|
}
|
||||||
|
if n := len(content) - len(heuristic); n > 0 {
|
||||||
|
t.Logf("heuristicDropPrefix=%q", firstRunes(content[:n], 160))
|
||||||
|
}
|
||||||
|
t.Logf("observedKeptHead=%q", firstRunes(observed, 160))
|
||||||
|
}
|
||||||
|
|
||||||
|
func lastRunID(runs []string) string {
|
||||||
|
if len(runs) == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
id := runs[len(runs)-1]
|
||||||
|
if i := strings.LastIndex(id, "@"); i >= 0 {
|
||||||
|
return id[:i]
|
||||||
|
}
|
||||||
|
return id
|
||||||
|
}
|
||||||
|
|
||||||
|
func firstRunes(s string, n int) string {
|
||||||
|
r := []rune(s)
|
||||||
|
if len(r) <= n {
|
||||||
|
return string(r)
|
||||||
|
}
|
||||||
|
return string(r[:n]) + "…"
|
||||||
|
}
|
||||||
+24
-1
@@ -248,10 +248,33 @@ func (s *Service) ask(ctx context.Context, request core.QARequest, answerObserve
|
|||||||
// observing knowledge_search tool-call events. The flag feeds coverage
|
// observing knowledge_search tool-call events. The flag feeds coverage
|
||||||
// classification (image turns) and is persisted with the turn.
|
// classification (image turns) and is persisted with the turn.
|
||||||
var knowledgeSearched bool
|
var knowledgeSearched bool
|
||||||
|
// The stream itself separates the agent's step narration from the final
|
||||||
|
// answer body: every step publishes its answer chunks under its own answer
|
||||||
|
// run id (data.event_id), and the last run that no tool/thinking event
|
||||||
|
// interrupts is the final body. Track that structure so the delivered
|
||||||
|
// answer keeps the body's lead-in — and any image reference inside it —
|
||||||
|
// instead of guessing the boundary from the text.
|
||||||
|
var narrationObservation reasoningmode.NarrationObservation
|
||||||
|
lastAnswerRunID := ""
|
||||||
effectiveEventObserver := weknora.StreamObserver(func(e weknora.StreamEvent) {
|
effectiveEventObserver := weknora.StreamObserver(func(e weknora.StreamEvent) {
|
||||||
if isKnowledgeTool(e.ToolName) {
|
if isKnowledgeTool(e.ToolName) {
|
||||||
knowledgeSearched = true
|
knowledgeSearched = true
|
||||||
}
|
}
|
||||||
|
switch e.ResponseType {
|
||||||
|
case "answer":
|
||||||
|
if e.AnswerRunID != "" {
|
||||||
|
narrationObservation.Observed = true
|
||||||
|
if e.AnswerRunID != lastAnswerRunID {
|
||||||
|
lastAnswerRunID = e.AnswerRunID
|
||||||
|
narrationObservation.AnswerRuns++
|
||||||
|
narrationObservation.LastRunStart = e.AnswerRunStart
|
||||||
|
narrationObservation.LastRunClean = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "thinking", "tool_call", "tool_result":
|
||||||
|
narrationObservation.ToolEvents++
|
||||||
|
narrationObservation.LastRunClean = false
|
||||||
|
}
|
||||||
if eventObserver != nil {
|
if eventObserver != nil {
|
||||||
eventObserver(e)
|
eventObserver(e)
|
||||||
}
|
}
|
||||||
@@ -334,7 +357,7 @@ func (s *Service) ask(ctx context.Context, request core.QARequest, answerObserve
|
|||||||
// narration stays visible while the card streams (line by line) and is
|
// narration stays visible while the card streams (line by line) and is
|
||||||
// dropped once the final body starts.
|
// dropped once the final body starts.
|
||||||
if reasoningmode.IsReasoningExecution(request.ReasoningExecution) {
|
if reasoningmode.IsReasoningExecution(request.ReasoningExecution) {
|
||||||
if trimmed := reasoningmode.TrimReasoningNarration(chat.Answer); trimmed != chat.Answer {
|
if trimmed := reasoningmode.TrimReasoningNarrationObserved(chat.Answer, narrationObservation); trimmed != chat.Answer {
|
||||||
turn.Answer, chat.Answer = trimmed, trimmed
|
turn.Answer, chat.Answer = trimmed, trimmed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,90 @@
|
|||||||
|
package reasoningmode
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
// NarrationObservation carries what the WeKnora event stream knows about the
|
||||||
|
// boundary between an agent step's narration and the final answer body.
|
||||||
|
//
|
||||||
|
// The text-only heuristic (BodyStartOffset) cannot tell a step narrative from
|
||||||
|
// an answer lead-in such as "我已经找到了 CRA5 的末端负载曲线图…下面给出完整回答。":
|
||||||
|
// both are plain prose, so the heuristic starts the body at the first
|
||||||
|
// structural line and silently drops everything before it — including answer
|
||||||
|
// prose, image references and table rows that sit in that lead-in.
|
||||||
|
//
|
||||||
|
// The stream knows the boundary: every agent step writes its narrative to the
|
||||||
|
// answer channel as its own answer run (WeKnora's data.event_id), and the
|
||||||
|
// final answer body is the last run that no tool/thinking event interrupts.
|
||||||
|
type NarrationObservation struct {
|
||||||
|
// Observed reports that the stream carried answer run information at all
|
||||||
|
// (WeKnora tags answer chunks with data.event_id). Without it the text
|
||||||
|
// heuristic stays in charge, so older deployments keep their behaviour.
|
||||||
|
Observed bool
|
||||||
|
// AnswerRuns counts distinct answer runs (consecutive chunks sharing one
|
||||||
|
// event id count as one run).
|
||||||
|
AnswerRuns int
|
||||||
|
// ToolEvents counts thinking / tool_call / tool_result events.
|
||||||
|
ToolEvents int
|
||||||
|
// LastRunStart is the byte offset in the accumulated answer where the
|
||||||
|
// most recent answer run started.
|
||||||
|
LastRunStart int
|
||||||
|
// LastRunClean reports that no tool/thinking event arrived after that run
|
||||||
|
// started, which makes the run the final answer body rather than another
|
||||||
|
// narrative step.
|
||||||
|
LastRunClean bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// BodyStartOffset returns the observed body start and whether the observation
|
||||||
|
// is usable. An answer with no tool event carries no narration at all, so the
|
||||||
|
// whole answer is the body.
|
||||||
|
func (o NarrationObservation) BodyStartOffset(contentLength int) (int, bool) {
|
||||||
|
if !o.Observed {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
// A single answer run carries the whole answer: the agent published no
|
||||||
|
// separate step narration, so there is nothing that can be cut safely.
|
||||||
|
if o.AnswerRuns <= 1 {
|
||||||
|
return 0, true
|
||||||
|
}
|
||||||
|
if o.ToolEvents == 0 {
|
||||||
|
return 0, true
|
||||||
|
}
|
||||||
|
if o.LastRunClean && o.LastRunStart > 0 && o.LastRunStart < contentLength {
|
||||||
|
return o.LastRunStart, true
|
||||||
|
}
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// SplitReasoningAnswerObserved returns the final answer body using the stream
|
||||||
|
// observation when it is usable and the text heuristic otherwise. ok is false
|
||||||
|
// when the content carries no body boundary (nothing must be hidden).
|
||||||
|
func SplitReasoningAnswerObserved(content string, observation NarrationObservation) (string, bool) {
|
||||||
|
if offset, ok := observation.BodyStartOffset(len(content)); ok {
|
||||||
|
if offset <= 0 {
|
||||||
|
// The stream showed no narration at all: the whole answer is body.
|
||||||
|
return content, true
|
||||||
|
}
|
||||||
|
// An image reference in front of the boundary is answer content (the
|
||||||
|
// agent published a picture in its lead-in), never step narration, so
|
||||||
|
// the safe answer is to keep everything.
|
||||||
|
if strings.Contains(content[:offset], "![") {
|
||||||
|
return content, true
|
||||||
|
}
|
||||||
|
body := strings.TrimLeft(content[offset:], "\r\n")
|
||||||
|
if strings.TrimSpace(body) != "" {
|
||||||
|
return body, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return SplitReasoningAnswer(content)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TrimReasoningNarrationObserved drops the step narration using the stream
|
||||||
|
// observation, falling back to the text heuristic when the stream carried no
|
||||||
|
// usable boundary. Unlike the heuristic it never drops the answer lead-in or
|
||||||
|
// an image reference that belongs to the body.
|
||||||
|
func TrimReasoningNarrationObserved(content string, observation NarrationObservation) string {
|
||||||
|
body, ok := SplitReasoningAnswerObserved(content, observation)
|
||||||
|
if !ok {
|
||||||
|
return content
|
||||||
|
}
|
||||||
|
return body
|
||||||
|
}
|
||||||
@@ -0,0 +1,127 @@
|
|||||||
|
package reasoningmode
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// The production case this observation fixes: the agent narrates its steps,
|
||||||
|
// then the final answer opens with a lead-in ("我已经找到了…下面给出完整回答。"),
|
||||||
|
// carries the requested load-curve image, and only then reaches its first
|
||||||
|
// heading. The text heuristic started the body at the heading and dropped the
|
||||||
|
// lead-in plus the image reference with it.
|
||||||
|
const craLoadCurveAnswer = "我需要先确认 CRA5 与 CR5A 是同一型号,然后检索负载曲线。\n" +
|
||||||
|
"知识库检索命中 3 篇文档,其中用户手册含末端负载曲线图。\n" +
|
||||||
|
"我已经找到了 CRA5(CR5A)的末端负载曲线图,并确认了负载曲线的读法说明。下面给出完整回答。\n\n" +
|
||||||
|
"CRA5(即 CR5A)的末端负载曲线如下:\n\n" +
|
||||||
|
"\n\n" +
|
||||||
|
"---\n\n" +
|
||||||
|
"## 一、负载曲线怎么读\n\n" +
|
||||||
|
"负载曲线反映的是:末端负载能力与负载质心偏心距离成负相关。\n"
|
||||||
|
|
||||||
|
func TestNarrationObservationKeepsAnswerLeadInAndImage(t *testing.T) {
|
||||||
|
narration := strings.Index(craLoadCurveAnswer, "我已经找到了")
|
||||||
|
observation := NarrationObservation{
|
||||||
|
Observed: true, AnswerRuns: 3, ToolEvents: 5,
|
||||||
|
LastRunStart: narration, LastRunClean: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
body := TrimReasoningNarrationObserved(craLoadCurveAnswer, observation)
|
||||||
|
if !strings.HasPrefix(body, "我已经找到了") {
|
||||||
|
t.Fatalf("body must start at the answer lead-in, got %q", firstLine(body))
|
||||||
|
}
|
||||||
|
if !strings.Contains(body, "") {
|
||||||
|
t.Fatalf("body must keep the image reference, got %q", firstLine(body))
|
||||||
|
}
|
||||||
|
if strings.Contains(body, "我需要先确认") {
|
||||||
|
t.Fatalf("body must not contain the step narration, got %q", firstLine(body))
|
||||||
|
}
|
||||||
|
|
||||||
|
// The text-only heuristic is what dropped the lead-in and the image.
|
||||||
|
heuristic := TrimReasoningNarration(craLoadCurveAnswer)
|
||||||
|
if !strings.HasPrefix(heuristic, "## 一、负载曲线怎么读") {
|
||||||
|
t.Fatalf("heuristic is expected to start at the heading, got %q", firstLine(heuristic))
|
||||||
|
}
|
||||||
|
if strings.Contains(heuristic, "resource://") {
|
||||||
|
t.Fatalf("heuristic is expected to drop the image reference")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNarrationObservationFallsBackWithoutUsableBoundary(t *testing.T) {
|
||||||
|
narration := strings.Index(craLoadCurveAnswer, "我已经找到了")
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
observation NarrationObservation
|
||||||
|
}{
|
||||||
|
{"not observed", NarrationObservation{AnswerRuns: 3, ToolEvents: 5, LastRunStart: narration, LastRunClean: true}},
|
||||||
|
{"last run interrupted by a tool", NarrationObservation{Observed: true, AnswerRuns: 3, ToolEvents: 5, LastRunStart: narration}},
|
||||||
|
{"multi run starting at zero", NarrationObservation{Observed: true, AnswerRuns: 3, ToolEvents: 5, LastRunStart: 0, LastRunClean: true}},
|
||||||
|
{"run start beyond the answer", NarrationObservation{Observed: true, AnswerRuns: 3, ToolEvents: 5, LastRunStart: len(craLoadCurveAnswer) + 10, LastRunClean: true}},
|
||||||
|
}
|
||||||
|
for _, tc := range tests {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
got := TrimReasoningNarrationObserved(craLoadCurveAnswer, tc.observation)
|
||||||
|
if want := TrimReasoningNarration(craLoadCurveAnswer); got != want {
|
||||||
|
t.Fatalf("expected heuristic fallback %q, got %q", firstLine(want), firstLine(got))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNarrationObservationKeepsImageLeadIn(t *testing.T) {
|
||||||
|
// The reported production shape: the lead-in carries the answer prose plus
|
||||||
|
// the picture, and the boundary observed by the stream only lands after it.
|
||||||
|
content := "根据知识库中《末端负载曲线-CRA系列》文档,以下是 **CR5A 的末端负载曲线图**:\n\n" +
|
||||||
|
"\n\n" +
|
||||||
|
"### 如何查看该负载曲线\n负载能力与偏心距离成负相关。\n"
|
||||||
|
offset := strings.Index(content, "### 如何查看该负载曲线")
|
||||||
|
observation := NarrationObservation{Observed: true, AnswerRuns: 4, ToolEvents: 6, LastRunStart: offset, LastRunClean: true}
|
||||||
|
if got := TrimReasoningNarrationObserved(content, observation); got != content {
|
||||||
|
t.Fatalf("image lead-in must survive the trim, got %q", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNarrationObservationKeepsSingleRunAnswer(t *testing.T) {
|
||||||
|
// The quick-answer agent publishes the whole answer as a single answer run
|
||||||
|
// (with tool_call/tool_result steps around it). Nothing in that stream is
|
||||||
|
// narration, so the lead-in and the image must survive.
|
||||||
|
observation := NarrationObservation{Observed: true, AnswerRuns: 1, ToolEvents: 4, LastRunStart: 0, LastRunClean: true}
|
||||||
|
got := TrimReasoningNarrationObserved(craLoadCurveAnswer, observation)
|
||||||
|
if got != craLoadCurveAnswer {
|
||||||
|
t.Fatalf("single answer run must keep the whole answer, got %q", firstLine(got))
|
||||||
|
}
|
||||||
|
if !strings.Contains(got, "resource://") {
|
||||||
|
t.Fatalf("the image reference must survive for a single answer run")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNarrationObservationKeepsWholeAnswerWithoutToolSteps(t *testing.T) {
|
||||||
|
answer := "本机提供以下三种接线方式:\n\n| 方式 | 说明 |\n| --- | --- |\n| A | 直连 |\n"
|
||||||
|
observation := NarrationObservation{Observed: true, AnswerRuns: 1, ToolEvents: 0}
|
||||||
|
|
||||||
|
body, ok := SplitReasoningAnswerObserved(answer, observation)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("a run without tool steps has no narration, so the whole answer is the body")
|
||||||
|
}
|
||||||
|
if body != answer {
|
||||||
|
t.Fatalf("expected the whole answer, got %q", body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNarrationObservationTrimsBlankLineAtRunStart(t *testing.T) {
|
||||||
|
content := "叙述步骤。\n\n\n## 标题\n\n正文。\n"
|
||||||
|
observation := NarrationObservation{Observed: true, AnswerRuns: 2, ToolEvents: 1,
|
||||||
|
LastRunStart: strings.Index(content, "\n\n\n##"), LastRunClean: true}
|
||||||
|
|
||||||
|
body := TrimReasoningNarrationObserved(content, observation)
|
||||||
|
if body != "## 标题\n\n正文。\n" {
|
||||||
|
t.Fatalf("expected the blank lines before the run content to be trimmed, got %q", body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func firstLine(s string) string {
|
||||||
|
if i := strings.IndexByte(s, '\n'); i >= 0 {
|
||||||
|
return s[:i]
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
@@ -34,6 +34,12 @@ type StreamEvent struct {
|
|||||||
FullAnswer string
|
FullAnswer string
|
||||||
ReferenceCount int
|
ReferenceCount int
|
||||||
ToolName string
|
ToolName string
|
||||||
|
// AnswerRunID / AnswerRunStart describe the answer run the event belongs
|
||||||
|
// to. WeKnora tags every agent step's answer chunks with its own
|
||||||
|
// data.event_id, so a change of AnswerRunID marks the start of a new run
|
||||||
|
// and AnswerRunStart is the accumulated answer offset where it began.
|
||||||
|
AnswerRunID string
|
||||||
|
AnswerRunStart int
|
||||||
}
|
}
|
||||||
|
|
||||||
// StreamObserver is called with a safe StreamEvent for each SSE event.
|
// StreamObserver is called with a safe StreamEvent for each SSE event.
|
||||||
@@ -80,9 +86,8 @@ func NewClient(baseURL, apiKey string, timeout time.Duration) (*Client, error) {
|
|||||||
if strings.TrimSpace(apiKey) == "" {
|
if strings.TrimSpace(apiKey) == "" {
|
||||||
return nil, errors.New("WEKNORA_API_KEY is required")
|
return nil, errors.New("WEKNORA_API_KEY is required")
|
||||||
}
|
}
|
||||||
if timeout <= 0 {
|
// timeout <= 0 keeps Go's default: no client-side total timeout on the HTTP
|
||||||
timeout = 120 * time.Second
|
// client, so long-lived agent SSE streams are never cut by a duration cap.
|
||||||
}
|
|
||||||
return &Client{baseURL: parsed.String(), apiKey: apiKey, http: &http.Client{Timeout: timeout}}, nil
|
return &Client{baseURL: parsed.String(), apiKey: apiKey, http: &http.Client{Timeout: timeout}}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -227,6 +232,12 @@ func parseSSEWithEventObserver(reader io.Reader, answerObserver AnswerObserver,
|
|||||||
var result ChatResult
|
var result ChatResult
|
||||||
var dataLines []string
|
var dataLines []string
|
||||||
terminal := false
|
terminal := false
|
||||||
|
// A reasoning agent publishes every step narrative and the final answer
|
||||||
|
// body through the same answer channel, but each step is its own answer
|
||||||
|
// run (data.event_id). A change of run id marks a new run start, which is
|
||||||
|
// what lets callers separate narration from the final body instead of
|
||||||
|
// guessing from the text.
|
||||||
|
answerRunID, answerRunStart := "", 0
|
||||||
|
|
||||||
notify := func(e StreamEvent) {
|
notify := func(e StreamEvent) {
|
||||||
if eventObserver != nil {
|
if eventObserver != nil {
|
||||||
@@ -262,15 +273,20 @@ func parseSSEWithEventObserver(reader io.Reader, answerObserver AnswerObserver,
|
|||||||
ReferenceCount: len(event.KnowledgeReferences),
|
ReferenceCount: len(event.KnowledgeReferences),
|
||||||
})
|
})
|
||||||
case "answer":
|
case "answer":
|
||||||
|
if runID := extractAnswerRunID(event.Data); runID != "" && runID != answerRunID {
|
||||||
|
answerRunID, answerRunStart = runID, len(result.Answer)
|
||||||
|
}
|
||||||
hasContent := strings.TrimSpace(event.Content) != ""
|
hasContent := strings.TrimSpace(event.Content) != ""
|
||||||
result.Answer += event.Content
|
result.Answer += event.Content
|
||||||
if hasContent && answerObserver != nil {
|
if hasContent && answerObserver != nil {
|
||||||
answerObserver(result.Answer)
|
answerObserver(result.Answer)
|
||||||
}
|
}
|
||||||
notify(StreamEvent{
|
notify(StreamEvent{
|
||||||
ResponseType: "answer",
|
ResponseType: "answer",
|
||||||
Done: event.Done,
|
Done: event.Done,
|
||||||
FullAnswer: result.Answer,
|
FullAnswer: result.Answer,
|
||||||
|
AnswerRunID: answerRunID,
|
||||||
|
AnswerRunStart: answerRunStart,
|
||||||
})
|
})
|
||||||
if event.Done {
|
if event.Done {
|
||||||
terminal = true
|
terminal = true
|
||||||
@@ -348,6 +364,30 @@ func parseSSEWithEventObserver(reader io.Reader, answerObserver AnswerObserver,
|
|||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// extractAnswerRunID extracts data.event_id, the identifier of the answer run
|
||||||
|
// an answer chunk belongs to. A reasoning agent gives every step its own id,
|
||||||
|
// so a change of id marks the start of a new run and, once no tool event
|
||||||
|
// follows, of the final answer body. Older deployments that do not tag answer
|
||||||
|
// events yield an empty id, which callers treat as "no observation".
|
||||||
|
func extractAnswerRunID(data json.RawMessage) string {
|
||||||
|
if len(data) == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
var m map[string]json.RawMessage
|
||||||
|
if err := json.Unmarshal(data, &m); err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
v, ok := m["event_id"]
|
||||||
|
if !ok {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
var s string
|
||||||
|
if err := json.Unmarshal(v, &s); err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return strings.TrimSpace(s)
|
||||||
|
}
|
||||||
|
|
||||||
// extractToolName extracts the tool_name string from a JSON Data field.
|
// extractToolName extracts the tool_name string from a JSON Data field.
|
||||||
// The Data may be any JSON shape; we look for the "tool_name" key and return
|
// The Data may be any JSON shape; we look for the "tool_name" key and return
|
||||||
// its string value. Returns empty string if not found or not a string.
|
// its string value. Returns empty string if not found or not a string.
|
||||||
|
|||||||
@@ -267,3 +267,69 @@ func TestAgentChatEventsErrorTurn(t *testing.T) {
|
|||||||
t.Fatalf("expected error done event, got %+v", events[0])
|
t.Fatalf("expected error done event, got %+v", events[0])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestStreamEventAnswerRuns verifies answer chunks are attributed to the answer
|
||||||
|
// run that produced them: a reasoning agent publishes every step narrative and
|
||||||
|
// the final answer body through the same answer channel, each under its own
|
||||||
|
// data.event_id, so the run start marks where the final body begins.
|
||||||
|
func TestStreamEventAnswerRuns(t *testing.T) {
|
||||||
|
stream := "data: {\"response_type\":\"thinking\",\"content\":\"inner monologue\",\"done\":false}\n\n" +
|
||||||
|
"data: {\"response_type\":\"answer\",\"content\":\"我先检索知识库。\",\"data\":{\"event_id\":\"step-1-answer\"},\"done\":false}\n\n" +
|
||||||
|
"data: {\"response_type\":\"tool_call\",\"data\":{\"tool_name\":\"knowledge_search\",\"event_id\":\"step-1-tool\"},\"done\":false}\n\n" +
|
||||||
|
"data: {\"response_type\":\"tool_result\",\"data\":{\"tool_name\":\"knowledge_search\",\"event_id\":\"step-1-tool\"},\"done\":false}\n\n" +
|
||||||
|
"data: {\"response_type\":\"answer\",\"content\":\"我已经找到了负载曲线图:\",\"data\":{\"event_id\":\"final-answer\"},\"done\":false}\n\n" +
|
||||||
|
"data: {\"response_type\":\"answer\",\"content\":\"\",\"data\":{\"event_id\":\"final-answer\"},\"done\":true}\n\n"
|
||||||
|
|
||||||
|
var events []StreamEvent
|
||||||
|
result, err := parseSSEWithEventObserver(strings.NewReader(stream), nil, func(e StreamEvent) {
|
||||||
|
events = append(events, e)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if got := len([]rune("我先检索知识库。")); got == 0 {
|
||||||
|
t.Fatal("unreachable")
|
||||||
|
}
|
||||||
|
narration := len("我先检索知识库。")
|
||||||
|
if result.Answer != "我先检索知识库。我已经找到了负载曲线图:" {
|
||||||
|
t.Fatalf("unexpected answer: %q", result.Answer)
|
||||||
|
}
|
||||||
|
|
||||||
|
var answers []StreamEvent
|
||||||
|
for _, e := range events {
|
||||||
|
if e.ResponseType == "answer" {
|
||||||
|
answers = append(answers, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(answers) != 3 {
|
||||||
|
t.Fatalf("expected 3 answer events, got %d: %+v", len(answers), answers)
|
||||||
|
}
|
||||||
|
if answers[0].AnswerRunID != "step-1-answer" || answers[0].AnswerRunStart != 0 {
|
||||||
|
t.Fatalf("narration run: want id 'step-1-answer' start 0, got %+v", answers[0])
|
||||||
|
}
|
||||||
|
if answers[1].AnswerRunID != "final-answer" || answers[1].AnswerRunStart != narration {
|
||||||
|
t.Fatalf("final run must start at %d, got %+v", narration, answers[1])
|
||||||
|
}
|
||||||
|
if answers[2].AnswerRunID != "final-answer" || answers[2].AnswerRunStart != narration {
|
||||||
|
t.Fatalf("chunks of one run must share its start, got %+v", answers[2])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestStreamEventAnswerRunWithoutID verifies streams that carry no run id leave
|
||||||
|
// the run fields empty, so consumers fall back to the text heuristic.
|
||||||
|
func TestStreamEventAnswerRunWithoutID(t *testing.T) {
|
||||||
|
stream := "data: {\"response_type\":\"answer\",\"content\":\"hello \",\"done\":false}\n\n" +
|
||||||
|
"data: {\"response_type\":\"answer\",\"content\":\"world\",\"done\":true}\n\n"
|
||||||
|
|
||||||
|
var events []StreamEvent
|
||||||
|
if _, err := parseSSEWithEventObserver(strings.NewReader(stream), nil, func(e StreamEvent) {
|
||||||
|
events = append(events, e)
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatalf("unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
for i, e := range events {
|
||||||
|
if e.AnswerRunID != "" || e.AnswerRunStart != 0 {
|
||||||
|
t.Fatalf("event[%d] must carry no run info, got %+v", i, e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user