From a096ba5f516c7110825e07c4f2b473a60bf98048 Mon Sep 17 00:00:00 2001 From: hermes-agent Date: Thu, 17 Sep 2026 09:59:39 +0800 Subject: [PATCH 01/19] =?UTF-8?q?fix(dingtalk):=20=E6=8A=95=E6=94=BE=20AI?= =?UTF-8?q?=20=E5=8D=A1=E7=89=87=E6=97=B6=E5=BC=80=E5=90=AF=E5=AE=BD?= =?UTF-8?q?=E5=B1=8F=E8=87=AA=E9=80=82=E5=BA=94=E5=B8=83=E5=B1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PC 端卡片宽度过窄:创建卡片(createAndDeliver)请求体从未请求宽屏布局。 修复落在创建卡片的 JSON 载荷层 —— cardData.cardParamMap 的钉钉保留系统 字段 sys_full_json_obj 内传 {"config":{"autoLayout":true}}。config 为 对象类型,钉钉规定复杂类型变量必须走该保留字段;放在 cardParamMap 兄弟 节点会因变量未赋值而静默失效(宽度不变、无报错)。 - 仅影响 PC 宽屏渲染;移动端保持自适应布局不变。 - 未改动富文本组件宽度/padding,也未改动模板内静态内容。 - 流式更新(streaming)请求体不带该字段,避免中途变更布局。 新增回归守卫测试:投放体(direct/group)必须含该字段且 autoLayout=true, 流式更新体必须不含;防止退化为静默失效写法。 验证:gofmt 干净、go vet ok、go test ./... 全绿; A/B 探针(A 带字段 / B 对照)经 PC 端人工确认 A 卡片变宽。 --- deploy/card_widescreen_probe.py | 121 ++++++++++++++++++++++++ deploy/list_tables.sql | 5 + deploy/probe_actors.sql | 8 ++ deploy/test_switch_card_widescreen.sh | 67 +++++++++++++ internal/channels/dingtalk/card.go | 19 ++++ internal/channels/dingtalk/card_test.go | 111 ++++++++++++++++++++++ 6 files changed, 331 insertions(+) create mode 100644 deploy/card_widescreen_probe.py create mode 100644 deploy/list_tables.sql create mode 100644 deploy/probe_actors.sql create mode 100644 deploy/test_switch_card_widescreen.sh diff --git a/deploy/card_widescreen_probe.py b/deploy/card_widescreen_probe.py new file mode 100644 index 0000000..80ebfed --- /dev/null +++ b/deploy/card_widescreen_probe.py @@ -0,0 +1,121 @@ +#!/usr/bin/env python3 +"""One-shot DingTalk AI card probe (test bot, private IM_ROBOT space). + +Sends two cards to the owner's private robot chat so PC width can be compared: + A: body carries sys_full_json_obj = {"config":{"autoLayout":true}} + B: control, same body without the layout field +Card body mirrors internal/channels/dingtalk/card.go buildCardBody() exactly. + +Credentials are read from the running container's environment and are never +printed. Only masked ids, HTTP statuses and DingTalk response bodies are shown. +""" +import json +import subprocess +import sys +import urllib.error +import urllib.request +import uuid + +CONTAINER = "dobotai-dingtalk" +TOKEN_URL = "https://api.dingtalk.com/v1.0/oauth2/accessToken" +CREATE_URL = "https://api.dingtalk.com/v1.0/card/instances/createAndDeliver" +STREAM_URL = "https://api.dingtalk.com/v1.0/card/streaming" +OWNER_ID = "243143382023108067" # 姚章浩 (channel_actors.dingtalk, test tenant) +WIDE = json.dumps({"config": {"autoLayout": True}}, separators=(",", ":")) + +CONTENT = ( + "**{tag}**\n\n" + "这是一条 PC 宽度对照测试卡片,由测试机器人在私聊里直接推送。\n\n" + "- 投放体里的布局字段:{field}\n" + "- 期望:{expect}\n" + "- 请在 PC 端对比两张卡片的宽度差异\n\n" + "移动端请忽略,宽屏只影响桌面端渲染。" +) + + +def env(key: str) -> str: + out = subprocess.run( + ["docker", "exec", CONTAINER, "printenv", key], + capture_output=True, text=True, check=True, + ).stdout.strip() + if not out: + sys.exit(f"missing env {key}") + return out + + +def call(method: str, url: str, payload: dict, token: str = "") -> tuple[int, str]: + data = json.dumps(payload).encode() + req = urllib.request.Request(url, data=data, method=method) + req.add_header("Content-Type", "application/json") + if token: + req.add_header("x-acs-dingtalk-access-token", token) + try: + with urllib.request.urlopen(req, timeout=30) as resp: + return resp.status, resp.read(4096).decode("utf-8", "replace") + except urllib.error.HTTPError as e: + return e.code, e.read(4096).decode("utf-8", "replace") + + +def mask(v: str) -> str: + return v[:6] + "..." + v[-4:] if len(v) > 12 else "***" + + +def main() -> None: + app_key, app_secret = env("DINGTALK_CLIENT_ID"), env("DINGTALK_CLIENT_SECRET") + tpl = env("DINGTALK_CARD_TEMPLATE_ID") + print(f"app_key={mask(app_key)} template={mask(tpl)} target={mask(OWNER_ID)}") + + status, body = call("POST", TOKEN_URL, {"appKey": app_key, "appSecret": app_secret}) + token = json.loads(body).get("accessToken", "") + print(f"token: http={status} got={'yes' if token else 'NO'}") + if not token: + sys.exit("no token") + + for tag, with_wide in (("A · 已带 autoLayout=true", True), ("B · 对照组(无布局字段)", False)): + param_map = { + "content": "", + "feedbackSelected": "", + "feedbackReasonSelected": "", + "showFeedbackReasons": "false", + "feedbackMessage": "", + } + if with_wide: + param_map["sys_full_json_obj"] = WIDE + out_track_id = str(uuid.uuid4()) + card_body = { + "cardTemplateId": tpl, + "outTrackId": out_track_id, + "callbackType": "STREAM", + "cardData": {"cardParamMap": param_map}, + "userIdType": 1, + "openSpaceId": "dtv1.card//IM_ROBOT." + OWNER_ID, + "imRobotOpenSpaceModel": {"supportForward": True}, + "imRobotOpenDeliverModel": { + "robotCode": app_key, + "spaceType": "IM_ROBOT", + "extension": {}, + }, + } + st, rb = call("POST", CREATE_URL, card_body, token) + print(f"[{tag}] create http={st} resp={rb[:300]}") + + text = CONTENT.format( + tag=tag, + field=("sys_full_json_obj = " + WIDE) if with_wide else "(无)", + expect="PC 端铺满窗口(宽屏)" if with_wide else "PC 端窄卡片(默认宽度)", + ) + stream_body = { + "outTrackId": out_track_id, + "guid": str(uuid.uuid4()), + "key": "content", + "content": text, + "isFull": True, + "isFinalize": True, + "isError": False, + } + st2, rb2 = call("PUT", STREAM_URL, stream_body, token) + print(f"[{tag}] finalize http={st2} resp={rb2[:200]} outTrackId={mask(out_track_id)}") + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/deploy/list_tables.sql b/deploy/list_tables.sql new file mode 100644 index 0000000..4f06940 --- /dev/null +++ b/deploy/list_tables.sql @@ -0,0 +1,5 @@ +\pset pager off +SELECT table_schema || '.' || table_name AS t +FROM information_schema.tables +WHERE table_schema NOT IN ('pg_catalog', 'information_schema') +ORDER BY 1; \ No newline at end of file diff --git a/deploy/probe_actors.sql b/deploy/probe_actors.sql new file mode 100644 index 0000000..269ace2 --- /dev/null +++ b/deploy/probe_actors.sql @@ -0,0 +1,8 @@ +\pset pager off +\echo == columns == +SELECT column_name, data_type +FROM information_schema.columns +WHERE table_schema = 'public' AND table_name = 'channel_actors' +ORDER BY ordinal_position; +\echo == rows == +SELECT * FROM channel_actors ORDER BY 1 LIMIT 6; \ No newline at end of file diff --git a/deploy/test_switch_card_widescreen.sh b/deploy/test_switch_card_widescreen.sh new file mode 100644 index 0000000..d7657a2 --- /dev/null +++ b/deploy/test_switch_card_widescreen.sh @@ -0,0 +1,67 @@ +#!/usr/bin/env bash +# Test-server switch to the wide-screen (autoLayout) card image. +# Rollback tag + 0600 env backup first; refuses unless the DOBOTAI_IMAGE +# substitution is exactly 1 before and exactly 1 after. Read-only readback +# at the end. No secrets are printed. +set -euo pipefail + +NEW_IMAGE="dobotai-dingtalk:local-cardfix-ba090d17" +RUNTIME_ENV="/opt/dobotai/container/runtime.env" +OLD_IMAGE="$(grep -m1 '^DOBOTAI_IMAGE=' "$RUNTIME_ENV" | cut -d= -f2-)" +ROLLBACK_TAG="dobotai-dingtalk:rollback-$(echo "$OLD_IMAGE" | sed 's/.*://')-before-card-widescreen" +STAMP="$(date +%Y%m%dT%H%M%S)" +BACKUP_DIR="/opt/dobotai/container/backups" +BACKUP="$BACKUP_DIR/runtime.env.before-card-widescreen.$STAMP" + +echo "== preflight ==" +docker image inspect "$NEW_IMAGE" >/dev/null +[ "$OLD_IMAGE" != "$NEW_IMAGE" ] || { echo "ABORT: runtime.env already points at $NEW_IMAGE" >&2; exit 1; } +before_count="$(grep -c '^DOBOTAI_IMAGE=' "$RUNTIME_ENV")" +[ "$before_count" = "1" ] || { echo "ABORT: $before_count DOBOTAI_IMAGE lines before edit" >&2; exit 1; } +echo "old_image=$OLD_IMAGE" +echo "new_image=$NEW_IMAGE" +echo "rollback_tag=$ROLLBACK_TAG" + +echo "== tag rollback target ==" +if docker image inspect "$ROLLBACK_TAG" >/dev/null 2>&1; then + echo "rollback tag already exists, keeping it (no overwrite)" +else + docker tag "$OLD_IMAGE" "$ROLLBACK_TAG" + echo "tagged $OLD_IMAGE -> $ROLLBACK_TAG" +fi + +echo "== backup runtime.env ==" +mkdir -p "$BACKUP_DIR" +install -m 0600 "$RUNTIME_ENV" "$BACKUP" +echo "backup=$BACKUP" + +echo "== rewrite DOBOTAI_IMAGE ==" +tmp="$(mktemp /opt/dobotai/container/.runtime.env.XXXXXX)" +awk -v new="$NEW_IMAGE" '/^DOBOTAI_IMAGE=/ { sub(/^DOBOTAI_IMAGE=.*/, "DOBOTAI_IMAGE=" new) } { print }' "$RUNTIME_ENV" >"$tmp" +after_count="$(grep -c "^DOBOTAI_IMAGE=$NEW_IMAGE\$" "$tmp")" +if [ "$after_count" != "1" ] || [ "$(wc -l <"$tmp")" != "$(wc -l <"$RUNTIME_ENV")" ]; then + rm -f "$tmp" + echo "ABORT: post-edit verification failed (match=$after_count)" >&2 + exit 1 +fi +chmod 0644 "$tmp" +mv "$tmp" "$RUNTIME_ENV" +grep -n '^DOBOTAI_IMAGE=' "$RUNTIME_ENV" + +echo "== recreate container ==" +cd /opt/dobotai/container +docker compose --env-file runtime.env -f compose.yaml up -d --no-deps --force-recreate dobotai-dingtalk + +echo "== readback ==" +sleep 3 +curl -fsS http://127.0.0.1:8090/healthz; echo +docker inspect dobotai-dingtalk --format 'image={{.Config.Image}} status={{.State.Status}} started={{.State.StartedAt}}' +for lit in sys_full_json_obj autoLayout _CARD_DEBUG_TOOL_ENTRY; do + if docker exec dobotai-dingtalk grep -aqF "$lit" /proc/1/root/usr/local/bin/dobotai; then + echo "literal $lit: PRESENT" + else + echo "literal $lit: ABSENT" + fi +done +docker logs dobotai-dingtalk --since 60s 2>&1 | tail -5 +echo "rollback: DOBOTAI_IMAGE=$OLD_IMAGE (+ compose up -d --no-deps --force-recreate dobotai-dingtalk)" \ No newline at end of file diff --git a/internal/channels/dingtalk/card.go b/internal/channels/dingtalk/card.go index 305e8c2..eaaab20 100644 --- a/internal/channels/dingtalk/card.go +++ b/internal/channels/dingtalk/card.go @@ -21,6 +21,24 @@ const ( cardCreateURL = "https://api.dingtalk.com/v1.0/card/instances/createAndDeliver" cardStreamingURL = "https://api.dingtalk.com/v1.0/card/streaming" cardDebugPrivateKey = "_CARD_DEBUG_TOOL_ENTRY" + + // cardWideScreenJSONKey is DingTalk's reserved system field inside + // cardData.cardParamMap. Template variables whose value is a complex type + // (object / array of objects) must be passed through this field as a JSON + // string; plain string/number/boolean variables go in cardParamMap + // directly. Putting a complex value anywhere else (for example as a + // sibling of cardParamMap under cardData) leaves the template variable + // unassigned and silently falls back to the default layout. + cardWideScreenJSONKey = "sys_full_json_obj" + + // cardWideScreenJSON configures the AI card's root "config": PC clients + // render the card wide (auto layout) when autoLayout is true. The card + // template must declare an object variable "config" with a boolean child + // "autoLayout" (card builder > 数据源 > 编辑变量) and the value must be + // passed when the card is created - setting it statically inside the + // template does not take effect. Mobile clients keep their own adaptive + // layout, so this only widens the card on desktop. + cardWideScreenJSON = `{"config":{"autoLayout":true}}` ) // CardRequest is the input for sending a final AI card. @@ -229,6 +247,7 @@ func (s *CardSender) buildCardBody(req CardRequest, outTrackID string) map[strin "feedbackReasonSelected": "", "showFeedbackReasons": "false", "feedbackMessage": "", + cardWideScreenJSONKey: cardWideScreenJSON, }, }, "userIdType": 1, diff --git a/internal/channels/dingtalk/card_test.go b/internal/channels/dingtalk/card_test.go index c28d75d..a2d24f4 100644 --- a/internal/channels/dingtalk/card_test.go +++ b/internal/channels/dingtalk/card_test.go @@ -1289,6 +1289,117 @@ func TestOAuthAcrossStartAndUpdates(t *testing.T) { } } +// --------------------------------------------------------------------------- +// Test: PC wide-screen layout config is delivered through the reserved +// cardParamMap system field, never as a sibling of cardParamMap +// --------------------------------------------------------------------------- + +func TestCardWideScreenAutoLayoutParam(t *testing.T) { + cases := []struct { + name string + req CardRequest + }{ + { + name: "direct", + req: CardRequest{ + ConversationType: core.ConversationDirect, + ExternalUserID: "user-456", + Content: "Hello from AI", + }, + }, + { + name: "group", + req: CardRequest{ + ConversationType: core.ConversationGroup, + ExternalChatID: "chat-789", + ExternalUserID: "user-456", + Content: "Hello from AI", + }, + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + ts := newCardTestServer() + defer ts.Close() + + sender, err := newCardSenderForTest("id", "secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + if _, err := sender.StartCard(context.Background(), tc.req); err != nil { + t.Fatalf("StartCard: %v", err) + } + + cardData, ok := ts.lastCreateBody["cardData"].(map[string]any) + if !ok { + t.Fatalf("cardData missing or wrong type: %#v", ts.lastCreateBody["cardData"]) + } + // Regression guard: a complex-typed value placed next to + // cardParamMap leaves the template variable unassigned and the + // card silently renders in the default narrow layout. + for _, badKey := range []string{"config", cardWideScreenJSONKey} { + if _, present := cardData[badKey]; present { + t.Fatalf("%q must not be a sibling of cardParamMap: %#v", badKey, cardData) + } + } + paramMap, ok := cardData["cardParamMap"].(map[string]any) + if !ok { + t.Fatalf("cardParamMap missing or wrong type: %#v", cardData["cardParamMap"]) + } + raw, ok := paramMap[cardWideScreenJSONKey].(string) + if !ok { + t.Fatalf("%s must be a JSON string, got %#v", cardWideScreenJSONKey, paramMap[cardWideScreenJSONKey]) + } + var decoded struct { + Config struct { + AutoLayout bool `json:"autoLayout"` + } `json:"config"` + } + if err := json.Unmarshal([]byte(raw), &decoded); err != nil { + t.Fatalf("%s is not valid JSON (%q): %v", cardWideScreenJSONKey, raw, err) + } + if !decoded.Config.AutoLayout { + t.Fatalf("config.autoLayout=false in %q, want true", raw) + } + // Plain string variables must keep going through cardParamMap directly. + if paramMap["content"] != tc.req.Content { + t.Errorf("content=%v, want %v", paramMap["content"], tc.req.Content) + } + }) + } +} + +// --------------------------------------------------------------------------- +// Test: streaming updates only carry content (no layout config) +// --------------------------------------------------------------------------- + +func TestCardWideScreenNotSentOnStreamingUpdate(t *testing.T) { + ts := newCardTestServer() + defer ts.Close() + + sender, err := newCardSenderForTest("id", "secret", "tpl", ts.Client(), ts.URL()+"/token", ts.URL()+"/create") + if err != nil { + t.Fatal(err) + } + outTrackID, err := sender.StartCard(context.Background(), CardRequest{ + ConversationType: core.ConversationDirect, + ExternalUserID: "user-456", + Content: "placeholder", + }) + if err != nil { + t.Fatalf("StartCard: %v", err) + } + if err := sender.UpdateCard(context.Background(), outTrackID, "streamed chunk", false, false); err != nil { + t.Fatalf("UpdateCard: %v", err) + } + if _, present := ts.lastStreamBody[cardWideScreenJSONKey]; present { + t.Fatalf("streaming body must not carry %s: %#v", cardWideScreenJSONKey, ts.lastStreamBody) + } + if ts.lastStreamBody["key"] != "content" { + t.Errorf("stream key=%v, want content", ts.lastStreamBody["key"]) + } +} + // --------------------------------------------------------------------------- // Helpers // --------------------------------------------------------------------------- From 15f2a111624ba7324696611847175caa6a9da1e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9A=E7=AB=A0=E6=B5=A9?= Date: Thu, 17 Sep 2026 10:58:59 +0800 Subject: [PATCH 02/19] =?UTF-8?q?fix(dingtalk):=20=E6=8E=A8=E7=90=86?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E5=8D=A1=E7=89=87=E9=9A=90=E8=97=8F=E6=8E=A8?= =?UTF-8?q?=E7=90=86=E5=B9=B6=E5=8F=AA=E7=95=99=E6=AD=A3=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - reasoningmode: 新增正文边界识别(结构行 + 前置空行)与流式 PartialFeed: 分步叙述按行拼接显示,正文开始时只输出正文,卡片更新即刷新清掉推理区 - dingtalk: 事件流与流式回调统一改走 PartialFeed - qa: 推理执行的最终答案在入库/投递前去掉步骤叙述(快速模式行为不变) 语料回归:生产 118 条内建智能推理答案中 106 条识别出正文边界(被隐藏的 均为 59-164 字过渡句),12 条无推理的问候类答案保持原样;go vet 与 go test ./... 全绿。 --- internal/channels/dingtalk/handler.go | 25 +++-- .../dingtalk/reasoning_partial_test.go | 36 +++++++ internal/qa/reasoning_answer_trim_test.go | 70 +++++++++++++ internal/qa/service.go | 10 ++ internal/reasoningmode/answer_split.go | 98 +++++++++++++++++++ internal/reasoningmode/answer_split_test.go | 98 +++++++++++++++++++ internal/reasoningmode/corpus_replay_test.go | 56 +++++++++++ internal/reasoningmode/partial_feed.go | 86 ++++++++++++++++ internal/reasoningmode/partial_feed_test.go | 87 ++++++++++++++++ 9 files changed, 559 insertions(+), 7 deletions(-) create mode 100644 internal/qa/reasoning_answer_trim_test.go create mode 100644 internal/reasoningmode/answer_split.go create mode 100644 internal/reasoningmode/answer_split_test.go create mode 100644 internal/reasoningmode/corpus_replay_test.go create mode 100644 internal/reasoningmode/partial_feed.go create mode 100644 internal/reasoningmode/partial_feed_test.go diff --git a/internal/channels/dingtalk/handler.go b/internal/channels/dingtalk/handler.go index 6a5e6cb..b29ddf4 100644 --- a/internal/channels/dingtalk/handler.go +++ b/internal/channels/dingtalk/handler.go @@ -490,11 +490,13 @@ func (h *Handler) handleStreaming(ctx context.Context, webhookURL string, req co // Streaming observer with the configured update throttle (500ms by default). var lastUpdate time.Time var lastPartialContent string + feed := reasoningmode.NewPartialFeed(req.ReasoningExecution) observer := func(fullContent string) { - if strings.TrimSpace(fullContent) == "" { + visible := feed.OnAnswer(fullContent) + if strings.TrimSpace(visible) == "" { return } - fullContent = decorateReasoningPartial(fullContent, req.ReasoningExecution) + fullContent = decorateReasoningPartial(visible, req.ReasoningExecution) now := h.now() if lastUpdate.IsZero() || now.Sub(lastUpdate) >= h.cardUpdateInterval { partialContent := h.sanitizeOutboundPartial(fullContent) @@ -551,17 +553,26 @@ func (h *Handler) handleEventStreaming(ctx context.Context, webhookURL string, r pump := newCardUpdatePump(ctx, h.cardUpdateInterval, func(updateCtx context.Context, content string) error { return streamingCard.UpdateCard(updateCtx, outTrackID, content, false, false) }) - answerStarted := false + feed := reasoningmode.NewPartialFeed(req.ReasoningExecution) result, err := eventQA.AskEvents(ctx, req, func(event weknora.StreamEvent) { - if event.ResponseType == "answer" && strings.TrimSpace(event.FullAnswer) != "" { - answerStarted = true - visibleAnswer := decorateReasoningPartial(event.FullAnswer, req.ReasoningExecution) + if event.ResponseType == "answer" { + visible := feed.OnAnswer(event.FullAnswer) + if strings.TrimSpace(visible) == "" { + return + } + visibleAnswer := decorateReasoningPartial(visible, req.ReasoningExecution) if partialContent := h.sanitizeOutboundPartial(visibleAnswer); strings.TrimSpace(partialContent) != "" { pump.Offer(partialContent) } return } - if answerStarted { + switch event.ResponseType { + case "tool_call", "tool_result", "thinking", "reflection": + // A reasoning step ended: the next narrative segment starts a new + // line so the card shows the reasoning process line by line. + feed.StepBoundary() + } + if feed.Started() { return } if content := renderProgressEvent(event); content != "" { diff --git a/internal/channels/dingtalk/reasoning_partial_test.go b/internal/channels/dingtalk/reasoning_partial_test.go index e698f1c..cd1c51c 100644 --- a/internal/channels/dingtalk/reasoning_partial_test.go +++ b/internal/channels/dingtalk/reasoning_partial_test.go @@ -1,6 +1,7 @@ package dingtalk import ( + "strings" "testing" "gitea.zheromiku.top/Weknora/dobotai-platform/internal/reasoningmode" @@ -19,3 +20,38 @@ func TestDecorateReasoningPartialLeavesQuickAnswerUnchanged(t *testing.T) { t.Fatalf("got=%q", got) } } + +func TestReasoningStreamingShowsStepsLineByLineThenDropsThemForBody(t *testing.T) { + execution := string(reasoningmode.ExecutionReasoning) + first := "我先确认一下设备的型号和手册。" + second := "手册里有关节扭矩表,让我核对一下。" + body := "## CR20V 关节电机最大旋转扭矩\n\n| 关节 | 扭矩 |" + feed := reasoningmode.NewPartialFeed(execution) + + if got, want := decorateReasoningPartial(feed.OnAnswer(first), execution), "【智能推理模式】\n\n"+first; got != want { + t.Fatalf("step1 got=%q want=%q", got, want) + } + feed.StepBoundary() + if got, want := decorateReasoningPartial(feed.OnAnswer(first+second), execution), "【智能推理模式】\n\n"+first+"\n\n"+second; got != want { + t.Fatalf("step2 got=%q want=%q", got, want) + } + answer := first + second + "\n\n" + body + if got, want := decorateReasoningPartial(feed.OnAnswer(answer), execution), "【智能推理模式】\n\n"+body; got != want { + t.Fatalf("refresh got=%q want=%q", got, want) + } + got := feed.OnAnswer(answer + "\n| J1 | 98 N·m |") + if strings.Contains(got, first) || !strings.Contains(got, "J1") { + t.Fatalf("reasoning must stay hidden while the body streams, got=%q", got) + } +} + +func TestQuickStreamingKeepsAccumulatedAnswer(t *testing.T) { + feed := reasoningmode.NewPartialFeed(string(reasoningmode.ExecutionQuick)) + if got := feed.OnAnswer("快速答案"); got != "快速答案" { + t.Fatalf("got=%q", got) + } + feed.StepBoundary() + if got := feed.OnAnswer("快速答案追加"); got != "快速答案追加" { + t.Fatalf("got=%q", got) + } +} diff --git a/internal/qa/reasoning_answer_trim_test.go b/internal/qa/reasoning_answer_trim_test.go new file mode 100644 index 0000000..54e8489 --- /dev/null +++ b/internal/qa/reasoning_answer_trim_test.go @@ -0,0 +1,70 @@ +package qa + +import ( + "context" + "testing" + + "gitea.zheromiku.top/Weknora/dobotai-platform/internal/reasoningmode" + "gitea.zheromiku.top/Weknora/dobotai-platform/internal/session" + "gitea.zheromiku.top/Weknora/dobotai-platform/internal/weknora" +) + +func reasoningAnswerFixture() (string, string, string) { + narration := "我需要先确认这台设备的型号和手册。" + body := "## CR20V 关节电机最大旋转扭矩\n\n| 关节 | 扭矩 |\n| --- | --- |\n| J1 | 98 N·m |" + return narration, body, narration + "\n\n" + body +} + +func reasoningEventClient(answer string) *fakeEventStreamingWeKnora { + return &fakeEventStreamingWeKnora{ + fakeWeKnora: &fakeWeKnora{}, + events: []weknora.StreamEvent{ + {ResponseType: "answer", FullAnswer: "我需要先确认这台设备的型号和手册。"}, + {ResponseType: "tool_call", ToolName: "knowledge_search"}, + {ResponseType: "tool_result", ToolName: "knowledge_search"}, + {ResponseType: "answer", FullAnswer: answer, Done: true}, + }, + streamResult: weknora.ChatResult{RequestID: "request-reasoning", Answer: answer}, + } +} + +func TestAskEventsReasoningKeepsOnlyAnswerBody(t *testing.T) { + _, body, answer := reasoningAnswerFixture() + repo := newMemoryRepository() + client := reasoningEventClient(answer) + service, err := New(repo, fixedResolver{}, client, session.GroupShared) + if err != nil { + t.Fatal(err) + } + req := streamQARequest("message-reasoning") + 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 body only, got %q (streamCalls=%d)", result.Answer, client.streamCalls) + } + repo.mu.Lock() + stored := repo.turns["tenant:dingtalk:message-reasoning"] + repo.mu.Unlock() + if stored.Answer != body { + t.Fatalf("stored answer must keep the body only, got %q", stored.Answer) + } +} + +func TestAskEventsQuickKeepsFullAnswer(t *testing.T) { + _, _, answer := reasoningAnswerFixture() + repo := newMemoryRepository() + service, err := New(repo, fixedResolver{}, reasoningEventClient(answer), session.GroupShared) + if err != nil { + t.Fatal(err) + } + result, err := service.AskEvents(context.Background(), streamQARequest("message-quick"), func(weknora.StreamEvent) {}) + if err != nil { + t.Fatalf("AskEvents: %v", err) + } + if result.Answer != answer { + t.Fatalf("quick answer must be untouched, got %q", result.Answer) + } +} diff --git a/internal/qa/service.go b/internal/qa/service.go index 47bfcc7..866545c 100644 --- a/internal/qa/service.go +++ b/internal/qa/service.go @@ -328,6 +328,16 @@ func (s *Service) ask(ctx context.Context, request core.QARequest, answerObserve turn.WeKnoraRequestID = chat.RequestID turn.KnowledgeSearched = knowledgeSearched turn.AnswerMessageID = newID() + // A reasoning run reports every agent step's narrative through WeKnora's + // answer channel and appends the final answer body to that same + // accumulation. Keep the body only in the stored/delivered answer: the + // narration stays visible while the card streams (line by line) and is + // dropped once the final body starts. + if reasoningmode.IsReasoningExecution(request.ReasoningExecution) { + if trimmed := reasoningmode.TrimReasoningNarration(chat.Answer); trimmed != chat.Answer { + turn.Answer, chat.Answer = trimmed, trimmed + } + } if err := s.repository.FinishTurn(ctx, turn); err != nil { return core.QAResult{}, fmt.Errorf("finish turn: %w", err) } diff --git a/internal/reasoningmode/answer_split.go b/internal/reasoningmode/answer_split.go new file mode 100644 index 0000000..9fbc2d1 --- /dev/null +++ b/internal/reasoningmode/answer_split.go @@ -0,0 +1,98 @@ +package reasoningmode + +import ( + "regexp" + "strings" +) + +// Reasoning agent runs report every intermediate step through WeKnora's +// response_type=answer channel: the step narratives are concatenated without +// any separator and the final answer body is appended to that same +// accumulation. A card update therefore shows one run-on "narration + body" +// blob. These helpers locate the boundary so callers can hide the narration +// and keep the body only. +// +// Calibration against 118 production answers of the built-in smart-reasoning +// agent: 106 had a structural first body line (97 heading, 4 bold-only line, +// 3 table row, 2 blockquote) and every one of them was directly preceded by a +// blank line; the remaining 12 answers were greetings without narration. The +// hidden narration was a short lead-in (median 111 chars, max 526 chars). + +var ( + answerHeadingLineRE = regexp.MustCompile(`^#{1,6}[\t ]+\S`) + answerBoldLineRE = regexp.MustCompile(`^\*\*[^*\n]+\*\*[\t ]*$`) + answerTableRowRE = regexp.MustCompile(`^\|.*\|[\t ]*$`) + answerQuoteLineRE = regexp.MustCompile(`^>[\t ]*\S`) + answerFenceLineRE = regexp.MustCompile("^(`{3,}|~{3,})") +) + +// IsReasoningExecution reports whether execution runs the multi-step reasoning +// agent whose answer stream mixes step narration with the final answer body. +func IsReasoningExecution(execution string) bool { + switch ExecutionMode(strings.TrimSpace(execution)) { + case ExecutionReasoning, ExecutionReasoningSingle: + return true + } + return false +} + +// SplitReasoningAnswer returns the final answer body. ok is false when the +// content carries no body boundary yet (nothing must be hidden in that case). +func SplitReasoningAnswer(content string) (string, bool) { + offset, ok := BodyStartOffset(content) + if !ok { + return content, false + } + return content[offset:], true +} + +// TrimReasoningNarration drops the step narration that precedes the final +// answer body. Content without a body boundary is returned unchanged. +func TrimReasoningNarration(content string) string { + body, ok := SplitReasoningAnswer(content) + if !ok { + return content + } + return body +} + +// BodyStartOffset returns the byte offset where the final answer body starts. +// A line qualifies as the body start only when it is a structural Markdown +// line (heading, bold-only line, table row or blockquote), directly follows a +// blank line and is not inside a fenced code block. +func BodyStartOffset(content string) (int, bool) { + lines := strings.Split(content, "\n") + fence := "" + offset := 0 + previousBlank := false + for i, raw := range lines { + trimmed := strings.TrimSpace(raw) + if marker := answerFenceLineRE.FindString(trimmed); marker != "" && + (fence == "" || strings.HasPrefix(trimmed, fence)) { + if fence == "" { + fence = marker + } else { + fence = "" + } + previousBlank = false + offset += len(raw) + 1 + continue + } + if i > 0 && fence == "" && previousBlank && isAnswerBodyLine(trimmed) { + return offset, true + } + previousBlank = trimmed == "" + offset += len(raw) + 1 + } + return 0, false +} + +func isAnswerBodyLine(trimmed string) bool { + if trimmed == "" { + return false + } + return answerHeadingLineRE.MatchString(trimmed) || + answerBoldLineRE.MatchString(trimmed) || + answerTableRowRE.MatchString(trimmed) || + answerQuoteLineRE.MatchString(trimmed) +} diff --git a/internal/reasoningmode/answer_split_test.go b/internal/reasoningmode/answer_split_test.go new file mode 100644 index 0000000..34be957 --- /dev/null +++ b/internal/reasoningmode/answer_split_test.go @@ -0,0 +1,98 @@ +package reasoningmode + +import "testing" + +func TestBodyStartOffsetRecognizesStructuralBodyLines(t *testing.T) { + leadIn := "根据检索到的资料,关于 CR20V 各关节电机最大旋转扭矩的说明如下:" + heading := "## CR20V 关节电机最大旋转扭矩" + tableBody := "**Magician 传感器套件(Magician Sensor Kit)重量如下:**\n\n| 项目 | 重量 |\n| --- | --- |\n| 整机 | 1.2 kg |" + rowBody := "| 关节 | 扭矩 |\n| --- | --- |" + quoteBody := "> 提示:以下参数来自产品手册。" + + tests := []struct { + name string + answer string + want string + ok bool + }{ + { + name: "narration then heading", + answer: "我已经收集了充分的信息。现在我可以明确回答用户的问题了。\n\n关键发现:\n1. 手册给出额定扭矩\n\n需要向用户说明\n\n" + heading + "\n\n各关节参数如下。", + want: heading + "\n\n各关节参数如下。", + ok: true, + }, + { + name: "lead-in then bold body line", + answer: "我已经获取到完整且明确的信息,可以给出准确答案了。\n\n" + tableBody, + want: tableBody, + ok: true, + }, + { + name: "short lead-in then table", + answer: leadIn + "\n\n" + rowBody, + want: rowBody, + ok: true, + }, + { + name: "narration then blockquote", + answer: "我需要先确认使用场景。\n\n" + quoteBody, + want: quoteBody, + ok: true, + }, + { + name: "greeting has no body boundary", + answer: "你好!我是越疆智能助手,可以帮你查询机械臂资料。", + want: "你好!我是越疆智能助手,可以帮你查询机械臂资料。", + ok: false, + }, + { + name: "body from the first line is left alone", + answer: heading + "\n\n各关节参数如下。", + want: heading + "\n\n各关节参数如下。", + ok: false, + }, + { + name: "structural line inside a fence is ignored", + answer: "我先确认一下配置项。\n\n```\n## 这是代码里的注释\n| a | b |\n```\n\n参考上面的写法。", + want: "我先确认一下配置项。\n\n```\n## 这是代码里的注释\n| a | b |\n```\n\n参考上面的写法。", + ok: false, + }, + { + name: "structural line glued to narration is not a boundary", + answer: "需要向用户说明" + heading, + want: "需要向用户说明" + heading, + ok: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + body, ok := SplitReasoningAnswer(tt.answer) + if ok != tt.ok { + t.Fatalf("ok = %v, want %v", ok, tt.ok) + } + if body != tt.want { + t.Fatalf("body = %q, want %q", body, tt.want) + } + if trimmed := TrimReasoningNarration(tt.answer); trimmed != tt.want { + t.Fatalf("TrimReasoningNarration = %q, want %q", trimmed, tt.want) + } + }) + } +} + +func TestIsReasoningExecution(t *testing.T) { + tests := map[string]bool{ + "reasoning": true, + " reasoning ": true, + "reasoning_single": true, + "quick": false, + "": false, + "something": false, + } + for execution, want := range tests { + if got := IsReasoningExecution(execution); got != want { + t.Fatalf("IsReasoningExecution(%q) = %v, want %v", execution, got, want) + } + } +} diff --git a/internal/reasoningmode/corpus_replay_test.go b/internal/reasoningmode/corpus_replay_test.go new file mode 100644 index 0000000..3713a4a --- /dev/null +++ b/internal/reasoningmode/corpus_replay_test.go @@ -0,0 +1,56 @@ +package reasoningmode + +import ( + "bufio" + "encoding/json" + "os" + "strings" + "testing" +) + +// TestReplayProductionCorpus replays real production answers of the built-in +// smart-reasoning agent through the body-boundary rule. +// Enable with REASONING_CORPUS=. +func TestReplayProductionCorpus(t *testing.T) { + path := os.Getenv("REASONING_CORPUS") + if path == "" { + t.Skip("set REASONING_CORPUS to replay production answers") + } + file, err := os.Open(path) + if err != nil { + t.Fatalf("open corpus: %v", err) + } + defer file.Close() + var total, trimmed int + scanner := bufio.NewScanner(file) + scanner.Buffer(make([]byte, 0, 1<<20), 1<<22) + for scanner.Scan() { + var row struct { + ID string `json:"id"` + Content string `json:"content"` + } + if err := json.Unmarshal(scanner.Bytes(), &row); err != nil { + t.Fatalf("decode row: %v", err) + } + total++ + body, ok := SplitReasoningAnswer(row.Content) + if !ok { + continue + } + trimmed++ + hidden := strings.TrimSpace(strings.TrimSuffix(row.Content, body)) + t.Logf("id=%s hidden=%d body=%d head=%q", row.ID[:8], len([]rune(hidden)), len([]rune(body)), shorten(hidden, 60)) + } + if err := scanner.Err(); err != nil { + t.Fatalf("scan corpus: %v", err) + } + t.Logf("total=%d narration_trimmed=%d unchanged=%d", total, trimmed, total-trimmed) +} + +func shorten(value string, limit int) string { + runes := []rune(value) + if len(runes) <= limit { + return value + } + return string(runes[len(runes)-limit:]) +} diff --git a/internal/reasoningmode/partial_feed.go b/internal/reasoningmode/partial_feed.go new file mode 100644 index 0000000..2fc07b2 --- /dev/null +++ b/internal/reasoningmode/partial_feed.go @@ -0,0 +1,86 @@ +package reasoningmode + +import "strings" + +// PartialFeed assembles the streaming card content of one turn. +// +// A reasoning run streams every agent step's narrative through the answer +// channel and appends the final answer body to the same accumulation, so the +// raw text reaches the card as one run-on paragraph. The feed +// +// - joins step segments with a blank line, so the card shows the reasoning +// line by line instead of glued sentences, and +// - switches to the final body only as soon as the body starts. The DingTalk +// card update replaces the card content, so that switch is what the user +// sees as the "refresh": the reasoning block disappears and the body is +// streamed on its own. +// +// Quick executions are a pass-through and keep the previous behaviour. +type PartialFeed struct { + reasoning bool + seen string + display string + stepPending bool + bodyOnly string + bodyStarted bool + started bool +} + +// NewPartialFeed builds the feed for one turn's execution mode. +func NewPartialFeed(execution string) *PartialFeed { + return &PartialFeed{reasoning: IsReasoningExecution(execution)} +} + +// Started reports whether any answer text has been streamed yet. +func (f *PartialFeed) Started() bool { + return f.started +} + +// StepBoundary marks the end of an agent step (tool call or tool result) so +// the next narrative segment starts on a new line. +func (f *PartialFeed) StepBoundary() { + if !f.reasoning { + return + } + f.stepPending = true +} + +// OnAnswer records the accumulated answer text reported by the stream and +// returns the content the card must show right now. +func (f *PartialFeed) OnAnswer(fullAnswer string) string { + if !f.reasoning { + if strings.TrimSpace(fullAnswer) != "" { + f.started = true + } + return fullAnswer + } + + delta := fullAnswer + switch { + case strings.HasPrefix(fullAnswer, f.seen): + delta = fullAnswer[len(f.seen):] + case fullAnswer != f.seen: + // The stream restarted (new WeKnora session or a retry): treat the + // text as a fresh segment instead of gluing it to the previous one. + f.stepPending = true + } + f.seen = fullAnswer + + if delta != "" { + f.started = true + if f.stepPending && strings.TrimSpace(f.display) != "" { + f.display += "\n\n" + } + f.stepPending = false + f.display += delta + } + + if body, ok := SplitReasoningAnswer(f.display); ok { + f.bodyStarted = true + f.bodyOnly = body + } + if f.bodyStarted { + return f.bodyOnly + } + return f.display +} diff --git a/internal/reasoningmode/partial_feed_test.go b/internal/reasoningmode/partial_feed_test.go new file mode 100644 index 0000000..91998d4 --- /dev/null +++ b/internal/reasoningmode/partial_feed_test.go @@ -0,0 +1,87 @@ +package reasoningmode + +import ( + "strings" + "testing" +) + +func TestPartialFeedJoinsReasoningSegmentsAndRefreshesToBody(t *testing.T) { + feed := NewPartialFeed(string(ExecutionReasoning)) + + // Step 1: first narrative segment streams in. + step1 := "我先查一下知识库。" + if got := feed.OnAnswer(step1); got != step1 { + t.Fatalf("first segment = %q, want %q", got, step1) + } + if !feed.Started() { + t.Fatal("Started must be true after the first answer text") + } + + // Step 1 ends with a tool call, step 2 starts with its own narrative. + feed.StepBoundary() + step2 := "找到了相关文档,我确认一下参数。" + got := feed.OnAnswer(step1 + step2) + want := step1 + "\n\n" + step2 + if got != want { + t.Fatalf("second segment = %q, want %q (segments must be line separated)", got, want) + } + + // Step 2 ends, step 3 carries the final answer body. + feed.StepBoundary() + body := "## CR20V 关节电机最大旋转扭矩\n\n| 关节 | 扭矩 |" + got = feed.OnAnswer(step1 + step2 + body) + if got != body { + t.Fatalf("body-only content = %q, want %q (reasoning must be hidden)", got, body) + } + + // The body keeps streaming; the visible content stays body-only. + grown := body + "\n| J1 | 100 N·m |" + if got := feed.OnAnswer(step1 + step2 + grown); got != grown { + t.Fatalf("grown body = %q, want %q", got, grown) + } + if strings.Contains(got, "我先查一下知识库") { + t.Fatalf("reasoning leaked into the refreshed card: %q", got) + } +} + +func TestPartialFeedKeepsGluedNarrationReadable(t *testing.T) { + // WeKnora concatenates step narratives without any separator; the feed must + // not let the card show them as one run-on sentence. + feed := NewPartialFeed(string(ExecutionReasoning)) + first := "我正在理解问题。" + feed.OnAnswer(first) + feed.StepBoundary() + second := "正在检索知识库。" + if got := feed.OnAnswer(first + second); got != first+"\n\n"+second { + t.Fatalf("glued narration = %q", got) + } +} + +func TestPartialFeedStartsANewSegmentWhenTheStreamRestarts(t *testing.T) { + feed := NewPartialFeed(string(ExecutionReasoning)) + feed.OnAnswer("第一段推理。") + got := feed.OnAnswer("重新开始的一段推理。") + if got != "第一段推理。\n\n重新开始的一段推理。" { + t.Fatalf("restart segment = %q", got) + } +} + +func TestPartialFeedQuickExecutionIsPassThrough(t *testing.T) { + feed := NewPartialFeed(string(ExecutionQuick)) + narrativeAndBody := "我已收集信息。\n\n## 标题\n\n正文" + if got := feed.OnAnswer(narrativeAndBody); got != narrativeAndBody { + t.Fatalf("quick execution = %q, want the raw stream unchanged", got) + } + feed.StepBoundary() + if got := feed.OnAnswer(narrativeAndBody); got != narrativeAndBody { + t.Fatalf("quick execution after boundary = %q, want the raw stream unchanged", got) + } +} + +func TestPartialFeedWithoutBoundaryKeepsShowingReasoning(t *testing.T) { + feed := NewPartialFeed(string(ExecutionReasoning)) + greeting := "你好!我是越疆智能助手。" + if got := feed.OnAnswer(greeting); got != greeting { + t.Fatalf("greeting = %q, want unchanged", got) + } +} From fcca33050ad6ee5c73f37c087175a143ff7e9b75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9A=E7=AB=A0=E6=B5=A9?= Date: Thu, 17 Sep 2026 11:08:23 +0800 Subject: [PATCH 03/19] =?UTF-8?q?test:=20=E9=80=9A=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E6=9C=8D=E9=95=9C=E5=83=8F=E5=88=87=E6=8D=A2?= =?UTF-8?q?=E8=84=9A=E6=9C=AC=EF=BC=88=E5=85=88=E6=89=93=E5=9B=9E=E6=BB=9A?= =?UTF-8?q?=E6=A0=87=E7=AD=BE+env=200600=20=E5=A4=87=E4=BB=BD=EF=BC=8C?= =?UTF-8?q?=E8=AF=BB=E5=9B=9E=E6=A0=A1=E9=AA=8C=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy/test_switch_image.sh | 66 +++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 deploy/test_switch_image.sh diff --git a/deploy/test_switch_image.sh b/deploy/test_switch_image.sh new file mode 100644 index 0000000..e1ce8de --- /dev/null +++ b/deploy/test_switch_image.sh @@ -0,0 +1,66 @@ +#!/usr/bin/env bash +# Generic test-server image switch: rollback tag first, 0600 runtime.env backup, +# post-edit verification (exactly 1 DOBOTAI_IMAGE line, unchanged line count), +# container recreate, then read-only readback incl. binary literal presence. +# Usage: bash test_switch_image.sh