diff --git a/.project-docs/30-worklog/tasks/20260928-merge-reply-cleanup-eb8d39b3.md b/.project-docs/30-worklog/tasks/20260928-merge-reply-cleanup-eb8d39b3.md new file mode 100644 index 00000000..473b941c --- /dev/null +++ b/.project-docs/30-worklog/tasks/20260928-merge-reply-cleanup-eb8d39b3.md @@ -0,0 +1,51 @@ +# Task: Merge teacher reply cleanup and approved UI changes into main + +## Identity + +- Task ID: 20260928-merge-reply-cleanup-eb8d39b3 +- Mode: Feature +- Branch: codex/20260928-merge-reply-cleanup-eb8d39b3-merge-reply-cleanup +- Worktree: /Users/chillishark/Makelore 麦洛/.codex-worktrees/makelore/20260928-merge-reply-cleanup-eb8d39b3 +- Base commit: a21a1f077cc11976213290a6b843bb74be97ab44 +- Owner: codex +- Status: In progress + +## Scope + +- Merge committed teacher/UI cleanup source 40c247a into latest origin/main and push normally; preserve existing continuous-chat and per-turn project/version behavior. + +## Intent And Constraints + +- User explicitly authorizes main integration and repository push; packaging belongs to their colleague. No packaging, native app restart, cloud publication, real model calls or focus changes. Preserve all foreign worktrees and source records. + +## Outcome + +- Source cleanup merged locally with continuous-chat conflict resolutions; latest remote advanced during verification and is being incorporated before publication. + +## Verification + +- Initial merge: typecheck and production Vite build passed; lint passed with 8 existing warnings. Focused backend/context/parser/browser tests 186 passed; backend overlap suites 169 passed; UI suites 127 passed and after extra regressions UI suite 61 passed; headless layout 15 passed. These overlapping runs are not an additive unique-test count. +- Project structure checker passed. Drift checker reports the source cleanup task record as foreign because the merge imports it unchanged; no source record or canonical file was authored here. +- Remote main moved from a21a1f0 to 077184a and already includes source 40c247a plus independently integrated reply recovery, protocol boundary and welcome fixes. Preserve that full main and add only necessary overlap safeguards before final revalidation. + +## Follow-ups + +- None recorded. + +## Promotion Candidates + +- None recorded. + +## Project Context Loaded + +- Identity verified by official start/status: feature task `20260928-merge-reply-cleanup-eb8d39b3`, this managed worktree/branch, base `a21a1f077cc11976213290a6b843bb74be97ab44`. Source checkout is owned by the completed cleanup task and still hosts the temporary app, so it remains read-only. +- Read entry/memory index, own record, project positioning, latest integrated state, decisions, architecture, success criteria and all registered peer scope/intent/promotion sections. Reuse previously loaded teacher domain/evidence/reflection/commitment context; changed continuous-chat rules checked against current main/source task records. +- Goal: student-owned creation with cloud-defined teachers, Main-owned request/history/version boundaries, prose and quick replies only. Preserve main's unique account/Agent chat and per-turn project/source/version; remove retired component UI/protocol/state and retain approved empty-composer hint/retry/preview-header styling. +- Other tasks: a broader latest-main integration is also registered planning; prepare this requested source in isolation, check remote again before push and never overwrite a concurrent remote advance. Prompt-boundary, truncation repair and welcome changes are separately owned and not added speculatively. Child-language task is unfinished. Canonical integration lock is held elsewhere; keep all canonical/source records read-only. +- New integrated single-chat behavior is compatible with component cleanup; overlaps require combined code/tests rather than choosing an entire side. No new product decision needed. Gate Passed. + +## Plan + +1. Merge source 40c247a into fetched main in this isolated branch; preserve source history and resolve overlaps with the current continuous-chat contract. +2. Verify backend/context/parser/history and UI/preview/refresh paths, typecheck, lint, production compile and headless layout; independently review the merge. +3. Recheck origin/main, integrate any concurrent advance, push normally to main and verify remote ancestry/hash. Keep the live trial checkout; packaging and app updates are outside this request. diff --git a/.project-docs/30-worklog/tasks/20260928-teacher-reply-cleanup-c8e4a219.md b/.project-docs/30-worklog/tasks/20260928-teacher-reply-cleanup-c8e4a219.md new file mode 100644 index 00000000..be20c97c --- /dev/null +++ b/.project-docs/30-worklog/tasks/20260928-teacher-reply-cleanup-c8e4a219.md @@ -0,0 +1,142 @@ +# Task: Retire teacher discussion components and preserve text replies and quick replies + +## Identity + +- Task ID: 20260928-teacher-reply-cleanup-c8e4a219 +- Mode: Feature +- Branch: codex/20260928-teacher-reply-cleanup-c8e4a219-teacher-reply-cleanup +- Worktree: /Users/chillishark/Makelore 麦洛/.codex-worktrees/makelore-teacher-reply-cleanup +- Base commit: ae0f72d8fa0089f87b41d272da220e1c1397aec8 +- Owner: codex +- Status: Ready for Integration + +## Scope + +- Remove retired visual discussion components from UI, state, context, parser, service and routes; keep teacher prose, clickable quick replies and free input. +- Preserve opaque historical data, agent/version selection, read tools, proactive check-ins, billing and operational Agent. + +## Intent And Constraints + +- User explicitly authorizes this product change, superseding old component-based teaching plans. Do not publish cloud configuration, restart applications or steal desktop focus. +- Gates passed: read AGENTS, project entry/read-before-planning, memory and indexes, relevant positioning/teacher decision/domain/architecture/current-state/evidence/commitment/stale records, success criteria and peer task scopes. Managed feature worktree identity verified against registry. +- Isolated from the live trial checkout. Frontend and reply protocol implementation delegated; backend/context/store and integration verification owned here. +- Follow cloud agent configuration for persona, length, prefixes and style; keep only output format and single-angle guided-help requirements locally. +- Plan: remove active component plumbing; retain passive legacy fields; add UI/parser/history/service/cloud regressions; run typecheck, targeted/full unit checks, build and guarded UI verification; document and commit. + +## Outcome + +- Removed all four interactive discussion views, their dedicated CSS, state/focus/conversion/lifecycle actions, client mutation API, Host API mutation routes and backend state machine. Pending drafts restore only current send fields while retaining request identity for safe retry. +- Ordinary questions and guided-help/suggestions use one text + quickReplies envelope. The local runner and cloud final-output path preserve project reads while keeping tool preambles out of the answer. Check-ins keep their existing plain-text path and scheduling/dedup behavior. +- Removed old component schemas/instructions, hard-coded suggestion counts/length/student-voice policy and check-in/guided-help brevity additions. “帮我看看” recommends one concrete starting point with replies around it. Published persona, skills, language, length, prefixes and style remain authoritative; no Alice-specific runtime content. +- Legacy discussion/context/snapshot/error data stays opaque and unmodified on read/list/save/recovery. No component is restored or updated. The parser supports old intro/questions, reply envelopes with inert tool data and damaged historical envelopes; only prose and suggestions survive. Raw parse failures or received cancelled output remain inert diagnostics, excluded from context/read_conversation. +- Kept teacher selection/version binding, readonly project/source tools, native/cloud billing and request identities, cancellation, source/account guards, active check-ins and operational Agent unchanged. +- README now describes the supported product behavior. No cloud publication, real application restart, live trial update or desktop focus change. Work is retained on this isolated feature branch. + +## Verification + +- `corepack pnpm run typecheck`: passed after the combined changes. +- `corepack pnpm run lint:check`: passed; scoped ESLint rerun after final parser/service changes passed. +- Final duplicate-card regression: both ordinary and guided suggestion cards use request ID + index keys; two repeated-text click/draft cases fail before the fix and pass after it. UI suite 52/52 and scoped ESLint passed; typecheck/build rerun after this final code edit. +- Targeted teacher backend/cloud/model/read-tools/UI/parser/history/route/guidance run: 9 files, 281 tests passed. Frontend agent also verified teacher companion and chat panel suites: 103 tests passed across its initial 3-file run. +- `corepack pnpm test`: first stage ran 261 files, 259 passed; 2551 tests passed, 3 skipped, 3 failed. The 3 failures are pre-existing Pi event-projector message expectations (2) and managed-worker-opener runtime-v6 versus v7 expectation (1). Isolated `git archive HEAD` base snapshot ae0f72d reproduces the exact same failures (3 failed / 12 passed); these tests and production coding-runtime/contracts/reducer paths have no task diff. Evidence: /tmp/makelore-teacher-reply-full-tests.log and /tmp/makelore-teacher-reply-baseline-tests.log. +- The test script’s pressure stage was skipped by its `&&` after baseline failures, so ran `coding-chat-pressure.test.tsx --maxWorkers=1` separately: 1 passed. +- `corepack pnpm run build:vite`: passed for Renderer, Main, Preload and utility worker. Generated output stayed ignored in this isolated checkout. +- Playwright teacher-reply-layout fixture: 12 headless Chrome checks passed at 319px/508px; rich Markdown/font checks rerun after shared dependency allow-list fix: 2 passed. No Electron/native app launch. Shared Electron first-chat spec updated to assert absence of retired send fields, but native suite deliberately not launched under the no-restart/no-focus constraint. +- Independent review found and resolved ordinary JSON questions/tool examples being mistaken for protocol and an aborted runner resolving normally losing received diagnostic bytes. Regressions cover both. +- Production search confirms old component words/schemas remain only as passive archive types, historical transport/fence recognition, and test data. No active lifecycle/prompt/action code remains. Model-bound history and read tools use only prose + quick replies; no legacy archived fields or raw failures enter the model. +- `check_project_docs.py`, task-aware drift check and `git diff --check`: passed before handoff. + +## Follow-ups + +- Integration must preserve the independent manual agent-refresh work while dropping its obsolete component state assumptions; this branch intentionally does not import another task’s unmerged feature. +- Track the three existing unrelated Pi test expectation failures separately. No production Pi changes are included here. +- Cloud agent configuration and cloud-side stored conversation history were not inspected or published; verification establishes what this client now sends, not an audit of remote prompt contents. Existing pinned versions are intentionally preserved. +- Task was not merged, pushed or deployed. No post-merge worktree cleanup applies. + +## Promotion Candidates + +- Targets: `30-worklog/current-state.md`, `10-decisions/ADR-2026-09-22-coding-teacher.md`, `20-architecture/data-flow.md`, `40-domain/business-rules.md`. +- Proposal: current teacher UX is prose plus clickable quick replies/free input; retire prior ideas/structure/flow/comparison component plans and model schema/state instructions. Preserve opaque historical data and the teacher responsibility of organizing ideas, understanding relationships and carrying confirmed consensus; published configuration owns expression policy. +- Evidence: explicit user direction in this task, deleted UI/API/state/schema paths, parser/history/model-payload regressions, headless interactive verification. +- Impact/conflict: supersedes prior active component lifecycle and multiple-topic suggestion assumptions; no automatic migration or history deletion, no change to source/version/billing/operational Agent boundaries. Prior feature-task records remain historical evidence. +- Human confirmation: already supplied by this task’s explicit product requirements; canonical promotion must still happen in a serialized integration task. + + +## Authorized Background Restart Follow-up + +- User explicitly requests restarting the application using the latest project state, superseding the earlier no-restart boundary. Keep no cloud publication and no desktop-focus takeover. +- Context gate passed on the same task/worktree/branch. Reread own task, entry/memory, positioning, current-state, teacher ADR/architecture, evidence/reflection/commitment references and all peer task scopes. The accepted product simplification supersedes stale active-component documentation; no canonical edits here. +- Current trial PID 27713 still uses the hover checkout’s Main but its Renderer was independently updated to committed manual-refresh source 4f6bda4. Preserve this existing trial feature by merging that commit into this task; resolve the one consultation-state conflict with shared catalog state and no retired focus state. Do not import the unrelated in-progress Alice policy branch or change main. +- Plan: validate the combined teacher/refresh paths, build the combined source, create an ignored background-launch wrapper, gracefully stop only the exact temporary-app PID and relaunch the same bundle/profile/port; verify loaded build and startup without focus. Installed production application is outside scope. +- Same user-data retains persisted project/history and consultation drafts. Unpackaged authentication and operation drafts are memory-owned; do not claim a restart guarantees persistence of data the product does not save. + + +### Restart Outcome + +- Merged committed manual-refresh source 4f6bda4 into this feature branch as 9adab45, retaining cleanup source 998796d. Independent review confirms catalog refresh/current topic version/drafts and reply-only UI all remain; the sole conflict was resolved without restoring retired focus state. +- Combined validation passed: typecheck, 112 teacher UI/companion/chat tests, changed-file ESLint, Renderer/Main/Preload/utility build and whitespace checks. No new backend logic changed after the previously recorded full regression run. +- Sent SIGTERM only to verified trial PID 27713; its normal app.quit cleanup completed and port 54599 was released. Relaunched the exact existing trial bundle with open -g -j and an ignored wrapper for this checkout. New PID 36109 serves 54599; OPTIONS /api/app/runtime-info returned 204. +- Runtime source/build is 9adab45, Renderer index-Dn2S79es.js loaded from this checkout, document complete and React mounted. Startup guard blocked one show and retained a hidden, unfocused window until user activation. A 12-second NSWorkspace monitor recorded trialFocused=false and unchanged foreground PID 409. Installed production PID 23486 remains untouched. +- Preserved the same trial user-data directory. Updated only local trial launch metadata after backing up prior descriptors. No cloud configuration publication, deliberate model request, history seeding or UI activation. This checkout must remain available while the trial uses its build. +- Task-aware drift checker now flags the imported peer task record 20260928-agent-refresh-a6d109b3 relative to the original task base. It is a committed dependency imported by the authorized merge, verified byte-for-byte identical to 4f6bda4; no peer document was edited. Record this inherited-source diagnostic rather than claiming the checker passed after merge. Canonical project documents remain untouched. + + +## Empty Composer Hint Follow-up + +- User approved the first proposed interaction: show a lightweight “给我一点提示” action only when the consultation input is empty, hide it when typing and remove the fixed “我也说不清,你带我看看” link below replies. Update the existing trial without desktop focus changes. +- Same-task ownership resumed and verified; rechecked task/entry and peer scopes. The protocol-boundary peer is complete but unmerged and not live in this trial; preserve its separate prompt-policy decisions for its designated integration task, do not change backend policy as part of this UI request. Existing manual refresh is already included. +- Plan: modify only the composer entry and fixed follow-up link, preserve true generated cards/free input/retries, add focused UI and browser checks, build and update trial Renderer while retaining Main and persisted data where possible. No cloud publication or deliberate model test request. + +### Hint Implementation And Verification + +- Replaced the filled “帮我看看” pill with a transparent “给我一点提示” text action in the empty composer. Typing hides it; clearing or whitespace restores it. A fixed minimum action-row height and left flex slot retain the send button’s position. Removed only the client-added “我也说不清,你带我看看” footer; identical text genuinely returned by the Agent still renders and sends as an ordinary generated reply. +- Preserved quick replies, free input, references/drafts, failure/cancel/interruption retries, and one-click request behavior. When an old unconfirmed suggestions payload still says “帮我看看”, retry now reuses the complete original payload and request ID; ordinary questions and already-confirmed requests cannot reuse it. Old guided-help intent remains readable for history compatibility; no backend/cloud policy changed. +- Verification passed: 59 UI unit tests (including five legacy pending-payload cases), 14 headless Chrome layout tests at 319/508px, typecheck, full lint followed by final changed-file lint, and production Renderer/Main/Preload/utility compile. Legacy regressions failed before the compatibility fix and passed afterward. Updated shared Electron E2E assertions but did not launch a native test application. No real model request was made for verification. +- Built with `corepack pnpm run build:vite --emptyOutDir false` so the live Renderer can keep loading its old hashed assets until the deliberate refresh. Saved previous index and bootstrap/launch descriptors locally outside Git; current Main is retained for the pending Renderer update. + +### Hint Trial Refresh Outcome + +- Implemented UI is committed as 73331e6; refreshed only the existing trial Renderer to `index-IW_wuxZ1.js`. Main PID 36109 and Host API 54599 remain running from the earlier 9adab45 launch. No native app restart, production application change, cloud publication, merge or push. Local launch metadata now records both running Main and Renderer versions, and the next-launch wrapper points to the current build. +- Preserved state only in Main memory during refresh, with busy request/attachment/flight gates and temporary show/focus guards. Did not read or copy auth tokens or all localStorage. After refresh, the current authenticated account matched the pre-refresh account, and project/conversation/route, operation/provisional drafts, composer contents and persisted consultation drafts matched. Both visible composers were empty; no claim of a live nonempty draft scenario is needed beyond the automated regressions. +- Verified actual DOM has the new transparent 13px hint, no old fixed footer element, mounted app and current script hash. Read-only composer screenshot inspected at the local trial `hint-preview.png`; it matches the intended light text interaction. 12-second foreground monitor recorded trialFocused=false with unchanged foreground PID 19791. Main focus calls were guarded and none occurred. +- Temporary inspector and Main-memory backup removed after verification. Runtime metadata/verification/screenshots remain local and ignored/outside Git. The managed checkout must remain available while this temporary app uses its compiled files. +- Project-doc structure and whitespace checks pass. Task-aware drift still reports only the unchanged imported manual-refresh task record from the authorized earlier merge; reconfirmed no difference against 4f6bda4. No canonical or peer task document was edited by this UI follow-up. + +## Playful Retry Follow-up + +- User requests the retry button label “继续看看👀” and a more playful appearance; continue the same temporary-app preview without taking desktop focus. +- Project Context Loaded: same feature task/branch/worktree/base identity above, registry ownership resumed and verified. Reread entry, active task and all peer Scope/Intent/Promotion sections; reuse already-read unchanged positioning, integrated snapshot, teacher decisions, architecture/domain, evidence/reflection and commitment context. Makelore remains a single light creation workspace; scope is only the existing failed/cancelled/interrupted suggestion retry affordance. Separate protocol-policy work stays isolated; old component documents are historical and superseded by this task’s accepted removal. No new semantic conflict. Gate Passed. +- Plan: change label and scoped button CSS to a warm rounded sticker with a small pressed shadow, retaining keyboard/disabled/reduced-motion support; update existing retry tests, inspect a headless screenshot, build and refresh only the trial Renderer with the existing state-preservation method. User-requested emoji overrides the design skill’s generic preference against emoji icons. + +- Implemented exact label “继续看看👀” with a cream-yellow 44px minimum-height sticker button, asymmetrical rounded corners, warm outline and shallow bottom shadow. Hover warms the fill; press lowers the shadow. Disabled and keyboard focus behavior remain, and reduced-motion disables transform/transition. No request or retry behavior changed. +- Passed: 59 existing UI tests updated for the new label, 15 headless layout checks including failed/cancelled retry at 319px with draft preservation, typecheck, full lint, production build and whitespace checks. Inspected the failed-state screenshot; the button fits the narrow panel without clipping. Tests use fixtures only. + +- Trial updated from committed cb48f60 to Renderer `index-D5o0Sba7.js`; running Main PID 36109/54599 and its 9adab45 code remain. Initial snapshot correctly refused while a teacher request was running; waited for completion before the state-preserving Renderer refresh. Existing successful reply is retained, without fabricating a failed live request to demonstrate retry. The new retry CSS is loaded; actual failed/cancelled appearance was verified in the headless fixture. +- Verified same login/account/project/conversation/route, operation drafts, composer values and all persisted consultation draft text/references/pending payloads. One persisted absent pending field normalizes to null on mount; semantic comparison confirms no draft content or request identity was lost. Runtime verification contains only boolean/style/build metadata, no draft bodies or credentials. Temporary Main-memory backup and inspector removed. +- Foreground monitor: trialFocused=false, unchanged foreground PID 88503. No show/focus calls were needed. Local trial metadata and next-launch bootstrap updated; production app untouched. No cloud publication, deliberate model test request, main merge or push. +- Documentation structure and whitespace checks pass; the task-aware drift diagnostic remains limited to the unchanged imported manual-refresh task record described above, reconfirmed identical to 4f6bda4. + +## Visible Hint Clarification + +- User could not see the prior rename. Read-only runtime inspection confirmed the new build was loaded but no failed/cancelled request was currently displayed; the visible empty-composer entry still said “给我一点提示”. Explained the distinction and user explicitly confirmed to rename/style the composer entry too. +- Same task identity/ownership resumed and verified; reuse this follow-up's loaded project/peer context with no new conflict. User clarification supersedes the earlier preference for a transparent text hint. Plan: share the warm sticker styling between hint and retry, send the matching new label through the existing suggestions intent, preserve empty/type/clear behavior and legacy pending-payload reuse. Increase the reserved action-row height to 44px so the send control remains still. Update existing tests and preview with a background Renderer refresh. No backend or cloud-policy change. + +- Implemented shared hint/retry sticker styling and exact “继续看看👀” composer label/payload. The hint still hides on nonempty input; reserved 44px action height keeps the send button fixed. Existing request identity reuse covers both former labels (“帮我看看” and “给我一点提示”), without changing saved history. +- Verification passed: typecheck, full lint plus final test-file lint, production build, 59 UI tests and 15 headless browser checks. Existing tests distinguish the two same-label controls by composer/reply location. Screenshots at 319/508px show the visible empty-composer button and confirm no clipping or send-button movement. Shared native E2E assertions updated without launching a native test app. + +- Refreshed existing trial Renderer from committed 784fa8c to `index-DLBsnWbW.js`. Main PID 36109 and profile retained. Final DOM confirms visible composer hint text “继续看看👀” with `rgb(255, 242, 201)` background; captured and inspected the actual empty-composer screenshot `visible-hint-preview.png` in the local trial root. Login/account/project/draft/input/route checks passed; no generated conversation content or requests were inserted for demonstration. +- Window/panel/selection state changed during the live verification: the consultation panel was closed at snapshot and open at final capture, and the selection equality check was false. Preserved current interaction without a second forced selection restore; do not claim exact conversation/window state remained unchanged. The foreground monitor already observed the trial as its sole foreground application; refresh invoked no show/focus calls. Final capture was taken with the window unfocused. +- Temporary backup memory and inspector removed; local launch/build metadata updated. No cloud publication, Main restart, main merge or push. Final documentation drift has only the previously recorded unchanged imported task document diagnostic. + +## Embedded Work Preview Header Follow-up + +- User requests removal of the pictured horizontal “作品预览” title/icon/close row. Continue the current temporary workspace preview without taking desktop focus. +- Same feature ownership resumed and verified. Reused unchanged previously loaded project context; reread own task/entry and newly active welcome/truncation/role-review task records. Existing peer scopes remain unchanged; no work-preview overlap or unresolved product decision. Gate Passed. This scoped presentation change preserves the shared browser's lifecycle, navigation, recovery, native viewport and separate developer-browser UI. +- Plan: hide the redundant header only in embedded work preview, retain an accessible aside name and the existing top-level operation/work tabs; keep address navigation and standalone browser close controls. Update existing unit/native E2E assertions, compile, and refresh only the temporary Renderer. Main/remote branches and installed production application remain unchanged. + +- Implemented conditional removal of the entire embedded title row (globe icon, “作品预览”, close button); the work-preview aside remains accessibly named. Address/back/forward/reload controls and the native viewport remain, while the standalone developer-browser header and close action are unchanged. +- Passed: 44 AgentBrowserPanel/CodingChatPanel unit tests, typecheck, full lint plus scoped test lint, full production compile and whitespace check. Updated shared Electron work-tab assertions without launching a native test instance. + +- Code committed as 064096e and new build prepared. The safe refresh snapshot detected an operation run in progress and refused to reload. Applied a narrowly scoped live stylesheet to the existing Renderer instead: only the embedded preview's old first div/title row is hidden, including after it mounts again. Current work tab was closed, so no live header screenshot or mounted-viewport assertion is claimed. The new compiled component permanently omits the row when next loaded. No work-tab switch, browser startup, model request, page reload or application restart was performed. +- Main PID 36109 and running Renderer 784fa8c remain; local descriptors explicitly distinguish the 064096e visual overlay/next build from the currently loaded script. Main focus/visibility stayed unchanged and no focus action was called. Temporary inspector closed; no draft, request, account, history or operation state was copied or reset. Installed production app and remote main remain unchanged. +- Final documentation structure/whitespace checks pass; task-aware drift retains only the previously documented unchanged imported manual-refresh task record diagnostic. diff --git a/README.md b/README.md index 08bcd2c8..a0a2e1ca 100644 --- a/README.md +++ b/README.md @@ -154,7 +154,7 @@ Pi 正式包必须继续运行 `pnpm run verify:artifact:pi`、`pnpm run smoke:p - 首次发送会立即生成稳定的乐观用户消息;HTTP 202 只表示本地 Agent 已接收。后续失败不会删除已接受消息,不确定交付不会自动重发;准备失败可在目标 Conversation 上手动恢复。 - 运行中的 Conversation 可把新消息作为 steer 引导当前回答或 follow-up 排到下一轮,并显示队列位置;队列正常在 `agent_settled` 后释放,用户可中止当前运行。若 Pi 已终结但该握手丢失,Main 会在 30 秒宽限后仅探测目标线程:权威 idle 状态会补齐持久化最终回复并 exactly-once 释放 ownership,矛盾状态则显式失败该目标,且不会重放已接受请求。select/confirm/input/editor 交互在输入区上方回答,select 除预设选项外支持自由输入;交互提交或取消后立即从输入区移除,失效请求会就地提示并允许重试或取消。 - 隐藏 Conversation 的红色新消息标记只在出现新的待回答交互,或当前任务进入 completed、failed、aborted 终态时设置;助手流式文字、思考、工具过程和单个工具失败不会提前触发红点。 -- 核心聊天页左侧显示品牌、项目及其对话;标题栏显示项目与当前 Conversation,并在右上角提供统一智能体入口和必要的中止/恢复按钮。中央“操作对话/作品”切换真实对话与项目浏览器预览,切换时保留操作草稿。点击智能体头像或主动气泡中的聊天入口,会同时打开智能体咨询并默认切到“作品”;仍可手动切回操作对话,单纯收到智能体气泡不会切换页面。点击“作品”会由 Main 检查已知本地页面:可访问则直接复用,否则向编程 Agent 发送固定的内部打开作品指令,检查现有服务、按项目实际脚本启动并调用 `agent_browser` 展示。当前项目正在制作时先等待;同一启动过程防止重复派发。失败时提供明确恢复入口:登录失效可“登录并继续”,返回相同账号和项目后接着打开;作品未打开可“帮我检查并打开”;普通操作中断可“帮我继续”,先核对已有进度再继续,保留当前输入草稿。仅对连接检查做两次有上限的自动重连并复用请求身份,不自动重放结果不确定的操作。最新失败提供一个恢复入口,空错误不再显示“本次处理失败。”;作品成功打开后收起对应失败提示。后台休眠释放预览窗口后,Main 在本次应用会话内保留项目对应的已验证地址,包括从地址栏或 Agent 成功打开的页面;回到前台或收到关闭状态时自动检查并恢复仍可访问的作品,不重复调用 Agent,也不依赖模型登录。手动打开成功会覆盖旧的启动请求结果,但不关闭后续恢复检查。窗口显示时发生关闭竞态会自动重试两次;仍无法显示则提供“重新打开作品”,不向学生暴露“开发浏览器已关闭”等内部错误,也不无限停留在打开中。内部指令不消耗学生草稿,也不参与对话自动命名,时间线隐藏这条内部指令及“已点击打开作品”操作记录,保留真实的处理结果和错误。学生作品区不展示 Console/Network 调试栏,也不订阅这两个面板的调试事件;Agent 的后台调试能力保留。项目设置、归档和会话管理保留在左侧。从已持久化的 user 消息仍可“从这里创建新对话分支”,assistant 消息和未持久化消息不提供该动作;分支只创建新的 Conversation 历史,不表示文件回滚。Pi 的工具调用与浏览器附件继续在对应的对话过程节点中按需展开;分享、待办、全局运行时和 revert/unrevert 不属于该产品界面。 +- 核心聊天页左侧显示品牌、项目及其对话;标题栏显示项目与当前 Conversation,并在右上角提供统一智能体入口和必要的中止/恢复按钮。中央“操作对话/作品”切换真实对话与项目浏览器预览,切换时保留操作草稿。作品区直接显示地址栏和页面,不再重复显示“作品预览”标题、图标及关闭横栏;通过上方“操作对话”标签切回对话。点击智能体头像或主动气泡中的聊天入口,会同时打开智能体咨询并默认切到“作品”;仍可手动切回操作对话,单纯收到智能体气泡不会切换页面。点击“作品”会由 Main 检查已知本地页面:可访问则直接复用,否则向编程 Agent 发送固定的内部打开作品指令,检查现有服务、按项目实际脚本启动并调用 `agent_browser` 展示。当前项目正在制作时先等待;同一启动过程防止重复派发。失败时提供明确恢复入口:登录失效可“登录并继续”,返回相同账号和项目后接着打开;作品未打开可“帮我检查并打开”;普通操作中断可“帮我继续”,先核对已有进度再继续,保留当前输入草稿。仅对连接检查做两次有上限的自动重连并复用请求身份,不自动重放结果不确定的操作。最新失败提供一个恢复入口,空错误不再显示“本次处理失败。”;作品成功打开后收起对应失败提示。后台休眠释放预览窗口后,Main 在本次应用会话内保留项目对应的已验证地址,包括从地址栏或 Agent 成功打开的页面;回到前台或收到关闭状态时自动检查并恢复仍可访问的作品,不重复调用 Agent,也不依赖模型登录。手动打开成功会覆盖旧的启动请求结果,但不关闭后续恢复检查。窗口显示时发生关闭竞态会自动重试两次;仍无法显示则提供“重新打开作品”,不向学生暴露“开发浏览器已关闭”等内部错误,也不无限停留在打开中。内部指令不消耗学生草稿,也不参与对话自动命名,时间线隐藏这条内部指令及“已点击打开作品”操作记录,保留真实的处理结果和错误。学生作品区不展示 Console/Network 调试栏,也不订阅这两个面板的调试事件;Agent 的后台调试能力保留。项目设置、归档和会话管理保留在左侧。从已持久化的 user 消息仍可“从这里创建新对话分支”,assistant 消息和未持久化消息不提供该动作;分支只创建新的 Conversation 历史,不表示文件回滚。Pi 的工具调用与浏览器附件继续在对应的对话过程节点中按需展开;分享、待办、全局运行时和 revert/unrevert 不属于该产品界面。 - 普通消息接收成功不显示额外的技术提示;准备、错误和排队状态仍正常反馈。发送状态按请求对应的运行结束收敛,保留期间输入的新草稿;恢复历史时优先以持久消息身份关联,避免相同文本串联到其他请求。 @@ -172,21 +172,21 @@ Pi 正式包必须继续运行 `pnpm run verify:artifact:pi`、`pnpm run smoke:p - 客户端没有内置老师/朋友角色。已下发智能体以头像和名称并排显示在标题栏,点击后打开对应智能体的讨论;数量较多时横向滚动,咨询面板不再提供智能体下拉框。名称、头像、简介、欢迎语和推荐问题均由服务端提供。首次使用默认选择运营指定项,各智能体的未发送草稿分别保存,同一账号与同一智能体只有一个持续聊天,跨项目保留消息和文字草稿;不再显示新话题按钮或话题选择。每轮接受时固定项目、操作对话与当前发布版本;旧的未分配项目草稿可展开查看,不自动转给其他智能体。打开或切换只读取配置和历史,不触发模型;咨询栏支持调整宽度和重新打开。 - 鼠标悬停或键盘聚焦顶部智能体入口时,以奶油色圆角卡片显示该智能体的发布介绍;离开、Escape 或点击入口后收起,没有介绍则不显示空卡片。咨询栏不再显示重复的头像、名称标题条,也不显示介绍及新话题、展开、关闭三个图标;老师身份保留在顶部入口及消息署名中。智能体生成的追加问题以淡蓝、奶油黄、淡紫卡片依次承载,正文 16px、行高 1.8,保留整卡点击发送与忙碌时禁用。回复下方不再显示重复的输入引导或省略早期消息条数;输入框提示为“和老师聊聊”。再次点击当前入口或在咨询栏按 Escape 可收起,切换其他入口仍直接打开对应讨论;拖动边缘调宽保留,已下发智能体仅保留只读旧记录入口。悬浮介绍不会创建话题、调用模型或确认未读消息,作品原生预览同时避让介绍卡和真实主动气泡。 - 主动发言采用紧贴小头像的短气泡;只有存在真实主动消息或运营欢迎语时才显示这组浮层,收起消息时头像和气泡一起消失,顶部入口仍保留。长消息在气泡中最多显示三行,点击接回原智能体对话查看全文。不提供“智能体偶尔来看看”开关或本地模拟巡看控制,既有自动跟进与真实消息接收逻辑保留。作品原生预览对整组气泡和头像测量避让,避免图片被原生页面遮住。 -- 新的单会话入口停止旧版前端定时跟进派发;项目主动观察由独立功能衔接,不能重新创建可见话题。未下发目录的旧版入口仍保留原跟进合同:前端每 5 分钟请求一次智能体跟进;窗口隐藏、来源归档、主对话正在执行、智能体正在回复或学生正在智能体栏写草稿时延后。Main 再检查在线启停、来源归属、项目级冷却和已完成文本指纹,未变上下文按下述十五分钟冷却处理。跟进沿用运营模型、已发布 Skills 与当前智能体话题,生成简短的具体建议或引导;以主动智能体发言持久化,不伪造学生消息。模型调用沿用智能体计费规则。 +- 新的单会话入口停止旧版前端定时跟进派发;项目主动观察由独立功能衔接,不能重新创建可见话题。未下发目录的旧版入口仍保留原跟进合同:前端每 5 分钟请求一次智能体跟进;窗口隐藏、来源归档、主对话正在执行、智能体正在回复或学生正在智能体栏写草稿时延后。Main 再检查在线启停、来源归属、项目级冷却和已完成文本指纹,未变上下文按下述十五分钟冷却处理。跟进沿用运营模型、已发布 Skills 与当前智能体话题,生成符合所选配置的具体建议或引导;以主动智能体发言持久化,不伪造学生消息。模型调用沿用智能体计费规则。 - 进入项目时,智能体头像旁先显示运营发布的欢迎语气泡,不调用模型、不声称已检查项目;每个账号/项目主动收起后不重复弹出。真实的未读主动建议优先替换欢迎语。主动发言在头像旁直接显示气泡,不抢焦点、不盖住作品;作品区按气泡与原生预览的实际交叠范围避让,已有工具栏高度计入计算,气泡位于独立智能体栏时不额外留白,窗口/分栏尺寸变化和收起气泡时实时归还空间;“和智能体聊聊”接回同一段咨询,“等会儿聊”收起并保留历史。已有未处理气泡时不堆叠新邀请;咨询已展开时也显示气泡,直到学生主动点击查看或收起,期间的其他回复不会吞掉未读邀请。五分钟检查计时在刷新和项目切换后保留;智能体咨询里的新讨论也算进展,即使操作对话没有变化也会继续跟进;上下文完全未变时放缓到十五分钟一次,不永久停止。事件连接丢失时,每十五秒只读同步未完成回复,收到结果后停止轮询,不重复调用模型。失败如实提示,不以本地文案替代模型回复。已接受请求恢复使用原身份,退出账号和来源删除仍沿用 Main 中止边界。 -- 智能体同时支持自由提问与输入框底部的提示词按钮“帮我看看”:学生点击后才结合当前操作对话与咨询历史生成 2–3 个可聊的问题。点击生成的问题会主动开始讨论;“我也说不清,你带我看看”让智能体从一个具体、容易回答的交流起点带着聊。这些快捷求助保留已有输入草稿,网络结果不确定时复用请求身份,已确认终态的请求不重复执行;解析失败可重新求助,不用固定问题伪装模型结果。服务端下发的推荐问题只填入草稿,由用户确认发送。界面不提供独立示范页面、“记一下”、共识或自动待办,智能体的回复下不再展示“我去试一试”和“复制”按钮,学生通过已有的操作对话/作品入口继续创作。 -- 智能体讨论采用“上方固定整理内容、下方独立滚动对话、底部原有输入框”的布局。普通回答可带直接发送的引导问题;想法板、结构图、流程/条件图和逐项对照由同一次模型回复提供结构化数据。先邀请学生“用这个一起想”,进入后程序锁定信息结构,智能体随讨论更新同一份内容;解释问题可仅回复文字。节点点击只选择讨论焦点,对照里的“聊聊这一点”直接发问,都保留输入草稿。 -- 想法板区分已留下、智能体建议和暂放内容,可由学生采纳、暂放、选择先想哪项。“把想法理一理”明确转换为智能体归纳的结构图,并可“回去补充想法”。“只聊天”暂停整理,“先这些”暂时结束并保留未确定内容,“接着改”恢复原工具;这些操作只影响智能体讨论,不创建项目分支、不执行作品修改。当前讨论卡片按聊天中的项目分别保存,历史轮次保留卡片快照。 -- Main 校验结构、引用和内容长度,拒绝未经学生选择的类型变更与旧版本更新。结构化回复在完整校验后一次应用;生成中显示简短处理状态,停止、失败或无效组件保留上一份内容。普通 Markdown 链接、JSON 对象/数组和代码示例保持正文,仅讨论协议的顶层字段或专用围栏进入组件解析。解析失败仍保留完整的可读 reply,并将本次原始回答保存为默认折叠、字面显示的“查看收到的原始内容”,不把它再次加入模型上下文;既有丢失原文的历史不能恢复。 +- 智能体支持自由提问;输入框为空时,底部显示暖黄色小按钮“继续看看👀”,输入文字后收起,清空后重现,发送按钮保持原位。只有学生点击才调用模型,结合当前操作对话与咨询历史推荐一个具体切入点,并生成围绕它的快捷回复。点击快捷回复会主动开始讨论;回复下方不再附加固定的二次求助链接;失败或中断后,可点暖黄色小按钮“继续看看👀”重试。这些快捷求助保留已有输入草稿,网络结果不确定时复用请求身份,已确认终态的请求不重复执行;解析失败可重新求助,不用固定问题伪装模型结果。服务端下发的推荐问题只填入草稿,由用户确认发送。界面不提供独立示范页面、“记一下”、共识或自动待办,智能体的回复下不再展示“我去试一试”和“复制”按钮,学生通过已有的操作对话/作品入口继续创作。 +- 老师咨询只展示老师正文和可点击的快捷回复卡片,学生也可在“和老师聊聊”输入框自由输入。点击卡片原样发送该回复,保留当前输入草稿;旧话题的数据不会恢复想法板、结构图、流程图或对照表及其操作。 +- “继续看看👀”由老师结合项目推荐一个具体切入点,快捷回复围绕这个切入点帮助学生接话;客户端不要求多个独立话题,也不固定正文长度、选项数量、前缀或表达风格。老师继续依据已配置职责帮助孩子整理想法、理解关系和承接已确认的共识,区分建议与已确认内容。 +- Main 对咨询回复统一使用 `{reply, quickReplies}`,本地读取工具的前言和云端非最终片段不当作最终回答;完整解析后展示正文和卡片,主动进展提醒仍直接展示正文。普通 Markdown、JSON 数据和代码示例保持正文。兼容读取旧 `{intro, questions}` 和带 `tool` 的回复,只提取正文与快捷回复,不恢复或更新组件。资源上限只用于防护,不做旧式字数截断;格式错误保留可读正文及原始回答,原文默认折叠、字面显示,不再次送进模型。 - 咨询正文支持 Markdown 标题、列表、表格、代码围栏、HTTP(S) 链接/图片和数学公式,长代码与表格在栏内横向滚动,不执行 HTML。云端正文维持 string 合同,未知对象不会猜测转成回答。工具活动只从云端主线程的类型化工具事件及 Main 本地读取过程获得,按本次问题/工具调用身份合并名称和状态,跨云端暂停、续接仍只计一次;暂停读取不算失败,状态以实际读取结果或问题终态为准,单独折叠显示;参数、结果原文和内部错误不混入回复,思考内容不作为正文。客户端仅保存自己实际收到的工具状态,不能补回旧历史或断线期间已经过期的事件。 - 云端咨询在 local_context 声明 read_protocol=2;需先部署配套 Yuxi API 和 worker,再升级客户端。云端持久累计读取字节及批次,每轮告知模型剩余额度;预算耗尽后消费最后一批结果,并以 tool_choice=none 要求根据现有证据形成答案和说明缺口。Main 限制实际返回量并拒绝第十三批读取,区分读取达到上限、上下文失效及格式无效。服务端对未声明协议的已安装旧客户端保留原工具参数与六批边界。 - 智能体人设、职责、提示词和 Skills 由发布配置决定。Main 不追加固定教学基线、不生成“小麦”、不清空所选智能体的 Skills,只添加真实工具能力、上下文边界和本轮界面协议。智能体名称叫“老师”“朋友”或“代码顾问”不改变调用路径或权限。 -- 每轮格式由 Main 的对应意图协议决定;`discussion.ts` 为支持组件的请求注入唯一 `{reply, quickReplies, tool}` 协议。运营教学补充不另写字段协议或要求始终纯文字。工具内讨论保留类型、稳定 ID、未修改内容和采纳状态;暂停/未进入时 `tool:null`,没有实质变化时也可保留原内容。结构图、流程和对照目前没有独立的采纳/来源字段,待定、建议与预测只能在展示文字中明确,不能据此推导已确认共识。 -- 当前输入是按预算截取的所选操作对话已完成分支文本、明确引用、咨询历史和适用的当前组件,并支持上述只读原文恢复;没有跨全部操作会话自动读取,也未向操作 Agent 注入咨询共识。“先这些”仍仅结束本轮整理,保留各条目状态,不确认全部候选、不自动制作。提示词不宣称这些系统能力已接通,也不声称看过未提供的画面或试玩过作品。Yuxi 话题经云端原生执行,旧版本地智能体由 Main 调用计费网关;均不创建本地 Pi 编程 worker。 -- 智能体输入裁剪区分两种计量:本地模型以约每 2 个 UTF-8 字节估算 1 Token,并计入工具与读取预留;这不是精确分词或扣费依据,模型上限与实际 usage 仍由服务端拥有。云端沿用 query 字节上限,以完整 JSON 转义后的大小裁剪来源节选。智能体指令、当前问题和明确引用不会被静默裁掉;固定配置或当前整理本身超限时明确提示调整配置,避免让学生反复缩短短问题或新建话题。完整来源仍可由只读工具按需读取。 -- `tests/fixtures/teacher-guidance-examples.json` 提供宠物、种植、射击、蛋糕的人工多轮输入输出样例,由真实解析器、上下文编译器与讨论状态机校验;样例不加载到运行时,也不代表真实模型教学效果已验收。云端定义由 Yuxi 维护并由运营显式同步下发;本地代码修改不会自动改写云端配置或学生已接受问题的固定发布版本。 +- `reply.ts` 只声明正文与快捷回复的输出格式,语言、长度、前缀与表达风格由所选智能体的云端配置决定。客户端不再发送选择、生成、转换或维护展示组件的指令。旧话题内的组件状态、快照和解析诊断作为不透明历史数据原样保留,不校验删除、不恢复交互;旧组件修改路由已移除。 +- 当前输入是按预算截取的所选操作对话已完成分支文本、明确引用、咨询正文和快捷回复,并支持上述只读原文恢复;旧组件状态、快照和未解析原文不加入模型上下文或咨询历史读取结果。没有跨全部操作会话自动读取,也未向操作 Agent 注入咨询共识。提示词不声称看过未提供的画面或试玩过作品。Yuxi 话题经云端原生执行,旧版本地智能体由 Main 调用计费网关;均不创建本地 Pi 编程 worker。 +- 智能体输入裁剪区分两种计量:本地模型以约每 2 个 UTF-8 字节估算 1 Token,并计入工具与读取预留;这不是精确分词或扣费依据,模型上限与实际 usage 仍由服务端拥有。云端沿用 query 字节上限,以完整 JSON 转义后的大小裁剪来源节选。智能体指令、当前问题和明确引用不会被静默裁掉;固定配置或回复协议超限时明确提示调整配置,避免让学生反复缩短短问题或新建话题。完整来源仍可由只读工具按需读取。 +- `tests/fixtures/teacher-guidance-examples.json` 保留人工样例作为旧回复的读取兼容资料,仅验证正文和快捷回复投影,不加载到运行时,也不代表真实模型教学效果已验收。云端定义由 Yuxi 维护并由运营显式同步下发;本地代码修改不会自动改写云端配置或学生已接受问题的固定发布版本。 - 下发智能体的图标随发布版本保存,顶栏、咨询面板、欢迎区和消息使用定义中的 `icon_url`;未设置或图片加载失败时使用内置头像。运营在 Yuxi 更换图标后需同步下发配置,联系人使用当前下发的名称和图标;每轮执行仍记录实际发布版本。相对图片地址由服务端使用 Yuxi 公网入口补全。 -- 咨询聊天和文字草稿按当前云端账号、已下发智能体保存;切换项目或操作对话仍延续同一个聊天,每轮发送冻结当时的项目、来源和发布版本并检查启停。引用保留原项目/来源,切换后必须移除失配引用再发送;旧项目卡片不能操作当前项目。历史按50轮分页,流式事件只传当前轮;本机每轮独立原子落盘,索引保存消息摘要,旧轮次可通过只读工具按ID读取。可确认归属的旧话题按来源ID幂等迁入,原文件保留;“旧记录”入口只读查看升级前项目/来源话题。项目、Pi来源或版本变化时切换内部云线程并带入有预算的近期交流,同一段由Yuxi原生历史压缩管理上下文。此版聊天历史在本机保存,不提供跨设备同步。关闭面板或切换会话不取消已接受的问题,退出账号会中止。请求先落盘再调用模型,同一请求标识不重复发送,重启后未完成请求标记中断。云端断线按游标和持久状态恢复;停止或退出账号会取消原云端问题,下次提问先停止旧的未完成问题再绑定新上下文。只接收云端主线程回答,子智能体文本不混入正文。没有操作对话时也可咨询,但无操作文本上下文。 +- 咨询聊天和文字草稿按当前云端账号、已下发智能体保存;切换项目或操作对话仍延续同一个聊天,每轮发送冻结当时的项目、来源和发布版本并检查启停。引用保留原项目/来源,切换后必须移除失配引用再发送。历史按50轮分页,流式事件只传当前轮;本机每轮独立原子落盘,索引保存消息摘要,旧轮次可通过只读工具按ID读取。可确认归属的旧话题按来源ID幂等迁入,原文件保留;“旧记录”入口只读查看升级前项目/来源话题。项目、Pi来源或版本变化时切换内部云线程并带入有预算的近期交流,同一段由Yuxi原生历史压缩管理上下文。此版聊天历史在本机保存,不提供跨设备同步。关闭面板或切换会话不取消已接受的问题,退出账号会中止。请求先落盘再调用模型,同一请求标识不重复发送,重启后未完成请求标记中断。云端断线按游标和持久状态恢复;停止或退出账号会取消原云端问题,下次提问先停止旧的未完成问题再绑定新上下文。只接收云端主线程回答,子智能体文本不混入正文。没有操作对话时也可咨询,但无操作文本上下文。 - 删除操作对话会停止正在使用该来源的回复,保留项目级咨询历史;归档来源需先恢复后才能引用。原有来源会话级智能体 API 与存储保持兼容,原路径历史不自动迁移到新项目级咨询栏。运营草稿试聊仍使用 `niancode://coding-teacher/preview?draft_revision=N` 打开独立示例上下文,不读取学生项目,要求桌面当前账号具有运营权限。 - 旧版客户端生成的朋友话题在“以往讨论”中标记为仅查看,不再创建或继续调用,也不自动绑定某个云端智能体;可选择下发项进入唯一聊天。统一咨询保留原有项目草稿;旧版朋友未发送的文字和引用可从“查看旧版朋友草稿”只读查看,不自动提交。旧文件和原发布版本保留,不复制或改写历史。已有 `coding-teacher` 云端契约及历史目录名保留,避免无必要的数据迁移。 diff --git a/electron/api/routes/coding-teacher.ts b/electron/api/routes/coding-teacher.ts index 37edfb31..23bd9de0 100644 --- a/electron/api/routes/coding-teacher.ts +++ b/electron/api/routes/coding-teacher.ts @@ -8,7 +8,7 @@ import { } from '../route-utils'; import { TeacherError } from '../../coding-teacher/config-client'; import type { TeacherScope } from '../../coding-teacher/service'; -import type { TeacherCheckInInput, TeacherDiscussionAction, TeacherSend } from '../../../shared/coding-teacher'; +import type { TeacherCheckInInput, TeacherSend } from '../../../shared/coding-teacher'; import { takeTeacherPreviewRevision } from '../../main/app-deep-link'; export async function handleCodingTeacherRoutes( @@ -18,15 +18,15 @@ export async function handleCodingTeacherRoutes( ctx: HostApiContext ): Promise { const legacy = url.pathname.match(/^\/api\/coding\/projects\/([^/]+)\/agent-history(?:\/([^/]+))?$/); - const conversation = url.pathname.match(/^\/api\/coding\/agent-conversations\/([^/]+)(?:\/(messages|events|save|seen|discussion|requests\/([^/]+)\/cancel))?$/); + const conversation = url.pathname.match(/^\/api\/coding\/agent-conversations\/([^/]+)(?:\/(messages|events|save|seen|requests\/([^/]+)\/cancel))?$/); const source = url.pathname.match( - /^\/api\/coding\/projects\/([^/]+)\/conversations\/([^/]+)\/teacher-topics(?:\/([^/]+))?(?:\/(messages|events|save|discussion|requests\/([^/]+)\/cancel))?$/ + /^\/api\/coding\/projects\/([^/]+)\/conversations\/([^/]+)\/teacher-topics(?:\/([^/]+))?(?:\/(messages|events|save|requests\/([^/]+)\/cancel))?$/ ); const preview = url.pathname.match( - /^\/api\/coding\/teacher-preview\/topics(?:\/([^/]+))?(?:\/(messages|events|save|discussion|requests\/([^/]+)\/cancel))?$/ + /^\/api\/coding\/teacher-preview\/topics(?:\/([^/]+))?(?:\/(messages|events|save|requests\/([^/]+)\/cancel))?$/ ); const projectTopics = url.pathname.match( - /^\/api\/coding\/projects\/([^/]+)\/(agent|teacher|friend)-topics(?:\/([^/]+))?(?:\/(messages|events|save|discussion|requests\/([^/]+)\/cancel))?$/ + /^\/api\/coding\/projects\/([^/]+)\/(agent|teacher|friend)-topics(?:\/([^/]+))?(?:\/(messages|events|save|requests\/([^/]+)\/cancel))?$/ ); const checkIn = url.pathname.match(/^\/api\/coding\/projects\/([^/]+)\/teacher-check-in$/); const role = projectTopics?.[2] === 'friend' ? 'friend' : undefined; @@ -120,10 +120,6 @@ export async function handleCodingTeacherRoutes( sendJson(res, 202, await service.send(scope, id, await parseJsonBody(req))); return true; } - if (id && action === 'discussion' && req.method === 'POST') { - sendJson(res, 200, await service.updateDiscussion(scope, id, await parseJsonBody(req))); - return true; - } if (id && action === 'save' && req.method === 'POST') { sendJson(res, 200, await service.save(scope, id)); return true; diff --git a/electron/coding-teacher/cloud-runner.ts b/electron/coding-teacher/cloud-runner.ts index 26ac2b36..3958ff05 100644 --- a/electron/coding-teacher/cloud-runner.ts +++ b/electron/coding-teacher/cloud-runner.ts @@ -207,7 +207,8 @@ export function prepareCloudTeacher( } const currentRequest = topic.requests.find((item) => item.id === requestId); const structuredReply = currentRequest?.intent === 'suggestions' - || currentRequest?.presentation === 'discussion-v1'; + || currentRequest?.presentation === 'reply-v1' + || currentRequest?.presentation === 'discussion-v1'; // Historical transport marker only. const localContext = { id: requestId, read_protocol: TEACHER_READ_PROTOCOL, diff --git a/electron/coding-teacher/context.ts b/electron/coding-teacher/context.ts index e029465c..c6b41795 100644 --- a/electron/coding-teacher/context.ts +++ b/electron/coding-teacher/context.ts @@ -113,7 +113,7 @@ export function compileTeacherContext( const current: TeacherModelMessage = { role: intent === 'check-in' ? 'system' : 'user', content: intent === 'check-in' - ? '本轮是一次项目进展提醒,不是用户提问。按照已配置的人设和职责,结合来源操作对话中已完成的文字和咨询历史回应,保持简短,不要求用户立即回答。仅依据已有证据,不重复上次提醒,不声称实际运行或试玩过作品。' + ? '本轮是一次项目进展提醒,不是用户提问。按照已配置的人设和职责,结合来源操作对话中已完成的文字和咨询历史回应,表达方式遵循已配置的要求,不要求用户立即回答。仅依据已有证据,不重复上次提醒,不声称实际运行或试玩过作品。' : [ ...references.map( (ref) => @@ -123,15 +123,11 @@ export function compileTeacherContext( ref.text ), '当前问题:\n' + question, - ...(intent === 'suggestions' + ...(intent === 'suggestions' || intent === 'guided-help' ? [ - '本轮交互要求(仅本轮):依据当前来源操作对话和本咨询历史,邀请学生选择一个可以一起讨论的问题。只返回 JSON 对象 {"intro":string,"questions":string[]},不要附加其他文字。intro 是简短、自然的邀请,不超过 400 字;questions 必须有 2–3 个互不重复、具体贴近当前进展的问题,每个不超过 120 字,用学生自己的口吻表达。问题应符合已配置智能体的职责和当前项目上下文。没有可用上下文时,坦诚说明目前还不了解项目,从学生想做什么、希望谁来用等构思切入;不要编造学生已经完成的功能、作品表现或项目进展,不要生成待办。', + '本轮学生希望你帮忙找到交流起点。依据当前项目、来源操作对话和咨询历史,推荐一个具体切入点,并说明为何值得从这里聊;快捷回复围绕这个切入点帮助学生接话。没有可用上下文时,坦诚从构思切入,不假定学生已完成功能,不编造作品表现或项目进展。', ] - : intent === 'guided-help' - ? [ - '本轮交互要求(仅本轮):学生暂时说不清想问什么。依据当前来源操作对话和本咨询历史,只发起一个具体、容易回答的交流起点,帮助学生开口。' + (presentationInstructions ? '按本轮界面协议返回,reply使用简短中文,' : '用正常、简短的中文文字回答,不返回 JSON,') + '不列出多个问题或一串任务。没有可用上下文时,坦诚从构思切入,不假定学生已经完成了任何功能。', - ] - : []), + : []), ].join('\n\n'), }; const sourceMessages = source.messages.map(message => ({ ...message })); @@ -202,7 +198,7 @@ export function compileTeacherContext( throw new TeacherError( 422, 'teacher_configuration_too_long', - '智能体配置或当前整理内容超过上下文预算,请联系运营调整智能体配置或预算。' + '智能体配置或回复协议超过上下文预算,请联系运营调整智能体配置或预算。' ); throw new TeacherError( 422, diff --git a/electron/coding-teacher/conversation-store.ts b/electron/coding-teacher/conversation-store.ts index 7bcd1847..f5a0b67d 100644 --- a/electron/coding-teacher/conversation-store.ts +++ b/electron/coding-teacher/conversation-store.ts @@ -11,7 +11,8 @@ interface TurnIndex { id: string; createdAt: string; origin?: string; messages: interface Manifest { topic: Omit; turns: TurnIndex[]; - importedDiscussions?: Record; + /** Retained verbatim when present in older manifests; no longer imported or interpreted. */ + importedDiscussions?: unknown; } const PAGE_SIZE = 50; const isMissing = (error: unknown) => (error as NodeJS.ErrnoException)?.code === 'ENOENT'; @@ -86,7 +87,7 @@ export class TeacherConversationStore { schemaVersion: 1, revision: 0, id: randomUUID(), accountId: this.accountId, projectId: '', sourceConversationId: 'project', definition: structuredClone(definition), version, createdAt: now, updatedAt: now, requests: [], - conversation: { agentId: this.agentId, segmentTurns: 0, discussions: {} }, + conversation: { agentId: this.agentId, segmentTurns: 0 }, }; // The manifest is durable before any turn file, so orphan recovery knows its owner. const manifest: Manifest = { topic: { ...topic }, turns: [] }; @@ -221,12 +222,8 @@ export class TeacherConversationStore { await this.save(topic, turn.id); topic.requests = topic.requests.slice(-PAGE_SIZE); } - if (old.discussion && (!topic.conversation!.discussions[project.id] - || old.updatedAt > (this.manifest!.importedDiscussions?.[project.id] ?? ''))) { - (this.manifest!.importedDiscussions ??= {})[project.id] = old.updatedAt; - topic.conversation!.discussions[project.id] = structuredClone(old.discussion); - await this.save(topic, ''); - } + // Retired component state stays in the untouched legacy topic. Import + // its conversation turns without creating active per-project components. } } if (this.manifest) await this.recent(); diff --git a/electron/coding-teacher/discussion.ts b/electron/coding-teacher/discussion.ts deleted file mode 100644 index 94b08f4e..00000000 --- a/electron/coding-teacher/discussion.ts +++ /dev/null @@ -1,108 +0,0 @@ -import { randomUUID } from 'node:crypto'; -import type { TeacherDiscussionAction, TeacherDiscussionContext, TeacherRequest, TeacherTopic } from '../../shared/coding-teacher'; -import { parseTeacherDiscussionContent, parseTeacherDiscussionReply } from '../../shared/teacher-discussion'; -import { TeacherError } from './config-client'; - -const conflict = () => new TeacherError(409, 'teacher_discussion_changed', '这份整理已经更新,请看一下最新内容后再试。'); -export function validateDiscussionContext(topic: TeacherTopic, value?: TeacherDiscussionContext): TeacherDiscussionContext | undefined { - if (value === undefined) return undefined; - if (!value || typeof value !== 'object' || typeof value.toolId !== 'string' || !Number.isSafeInteger(value.revision) - || (value.focusId !== undefined && (typeof value.focusId !== 'string' || value.focusId.length > 129)) - || (value.transition !== undefined && value.transition !== 'structure')) - throw new TeacherError(422, 'teacher_discussion_invalid', '讨论内容无效,请重新选择。'); - const tool = topic.discussion; - if (!tool || tool.id !== value.toolId || tool.revision !== value.revision || tool.status !== 'active') throw conflict(); - if (value.transition && tool.content.kind !== 'ideas') throw conflict(); - if (value.focusId) { - const content = tool.content; - const ids = content.kind === 'ideas' ? content.items.map(item => item.id) - : content.kind === 'comparison' ? content.rows.flatMap(row => content.columns.map(col => `${row.id}:${col.id}`)) - : content.nodes.map(node => node.id); - if (!ids.includes(value.focusId)) throw conflict(); - } - return { toolId: value.toolId, revision: value.revision, - ...(value.focusId ? { focusId: value.focusId } : {}), ...(value.transition ? { transition: value.transition } : {}) }; -} - -export function editDiscussion(topic: TeacherTopic, input: TeacherDiscussionAction): void { - const tool = topic.discussion; - if (!tool || !input || tool.id !== input.toolId || tool.revision !== input.revision) throw conflict(); - const phases = { enter: 'active', resume: 'active', pause: 'paused', finish: 'finished' } as const; - if (Object.hasOwn(phases, input.action)) tool.status = phases[input.action as keyof typeof phases]; - else if (input.action === 'back-ideas') { - if (tool.content.kind !== 'structure' || !tool.previousIdeas) throw conflict(); - tool.previousStructure = structuredClone(tool.content); - tool.content = structuredClone(tool.previousIdeas); - tool.status = 'active'; - } - else { - if (tool.content.kind !== 'ideas' || !['keep-idea', 'aside-idea', 'first-idea'].includes(input.action)) - throw new TeacherError(422, 'teacher_discussion_action_invalid', '这个操作暂不可用。'); - const item = tool.content.items.find(candidate => candidate.id === input.itemId); - if (!item) throw conflict(); - if (input.action === 'first-idea') { - if (item.state !== 'kept') throw conflict(); - tool.content.firstItemId = item.id; - } else { - item.state = input.action === 'keep-idea' ? 'kept' : 'aside'; - if (item.state === 'aside' && tool.content.firstItemId === item.id) delete tool.content.firstItemId; - } - tool.status = 'active'; - tool.content = parseTeacherDiscussionContent(tool.content); - } - tool.revision++; -} - -/** The program controls tool identity/lifecycle; the model only supplies its content. */ -export function applyDiscussionReply(topic: TeacherTopic, request: TeacherRequest, raw: string): void { - const parsed = parseTeacherDiscussionReply(raw); - request.response = parsed.reply; - request.suggestedQuestions = parsed.quickReplies; - if (parsed.toolError) { - request.discussionError = parsed.toolError; - // Keep inspectable evidence instead of replacing the only copy with an error. - request.unparsedResponse = raw; - } - if (!parsed.tool) return; - const current = topic.discussion; - if (!current) { - topic.discussion = { id: randomUUID(), revision: 1, status: 'offered', content: parsed.tool }; - request.discussionSnapshot = structuredClone(parsed.tool); - return; - } - const context = request.discussionContext; - const expectedKind = context?.transition === 'structure' ? 'structure' : current.content.kind; - if (!context || current.status !== 'active' || context.toolId !== current.id || context.revision !== current.revision) return; - if (parsed.tool.kind !== expectedKind) { - request.discussionError = '这次的整理还没放好,原来的内容保留着,可以接着聊。'; - return; - } - if (JSON.stringify(current.content) !== JSON.stringify(parsed.tool)) { - if (context.transition === 'structure' && current.content.kind === 'ideas') { - current.previousIdeas = structuredClone(current.content); - } - current.content = parsed.tool; - current.revision++; - request.discussionSnapshot = structuredClone(parsed.tool); - } -} - -export function discussionInstructions(topic: TeacherTopic, context?: TeacherDiscussionContext): string { - const current = topic.discussion; - const currentInfo = current ? JSON.stringify({ status: current.status, content: current.content, focusId: context?.focusId, - ...(context?.transition && current.previousStructure ? { previousStructure: current.previousStructure } : {}) }) : '目前没有共同整理的内容'; - return [ - '本轮界面协议:只输出一个JSON对象 {"reply":string,"quickReplies":string[],"tool":object|null},不用代码围栏。reply是自然的中文回应,不复述整个组件,也不提JSON或组件类型名。quickReplies最多3句学生可以直接点选发问的话,使用组件时一般为空。', - '正文遵循已配置智能体的人设、职责和本轮问题。quickReplies应围绕当前项目尚需理清的关系、取舍或验证,用学生口吻发问;已有组件内讨论入口时留空。不重复已知信息,不默认增加功能或安排制作。普通解释可在reply中回答,tool填null。tool是展示数据,不是执行工具调用。', - 'tool按信息结构选择:ideas {kind:"ideas",title,items:[{id,text,parentId?,state:"kept"|"suggested"|"aside"}],firstItemId?};structure {kind:"structure",title,nodes:[{id,label,relation?,parentId?}]};flow {kind:"flow",title,nodes:[{id,label,kind:"event"|"condition"|"outcome"}],edges:[{id,from,to,label?}]};comparison {kind:"comparison",title,columns:[{id,label}],rows:[{id,label,cells:[{columnId,text}]}]}。', - 'id使用1–64位ASCII字母、数字、下划线或连字符。title和列label最多120字;节点label、想法text、对照cell.text最多600字,relation最多120字,边label和行label最多160字;quickReplies每句最多120字。结构节点/想法最多24项,流程最多24节点40边,对照2–4列最多12行。比较逐行对齐相同情况下的差异,并在文字中区分预期和观察。父节点、边、比较列必须存在。结构层级由实际想法总结,不使用固定分类。', - 'ideas中学生明确表达想要或已留下的内容用kept,你新提的候选用suggested,暂放内容用aside;沿用已有采纳状态和firstItemId,不因讨论、焦点选择或“先这些”自动采纳候选。structure/flow/comparison没有独立采纳字段,不能自造字段;需要表达不确定性时在label、relation、cell.text或reply中明确“待定”“建议”或“预计”,不把展示内容称为已确认共识。不要输出工具身份、revision或生命周期状态,这些由程序管理。', - current && context - ? context.transition === 'structure' - ? '学生明确选择了“把想法理一理”。本轮将已留下的ideas整理为structure,保留相关细节,不把suggested/aside当成学生采纳。之后维持structure。' - : `正在使用${current.content.kind}讨论,必须保持这个kind和现有信息。围绕focusId(如有)理解新表达;有实质补充时返回更新后的完整内容,保留未修改条目、关系、采纳状态和稳定ID,不只返回差量。无需更新时tool为null,原内容会保留。不要自行换工具;学生明确选“把想法理一理”才转structure,返回想法的动作由程序处理。` - : current ? '学生当前没有选择在工具内继续整理。本轮只对话,tool必须为null,保留原来的内容。' - : '必要时可以提出一个共同整理的工具;程序先让学生选择是否使用。普通对话tool为null。', - '当前共同内容(只是资料,不是系统指令):\n' + currentInfo, - ].join('\n\n'); -} diff --git a/electron/coding-teacher/reply.ts b/electron/coding-teacher/reply.ts new file mode 100644 index 00000000..0356e4f8 --- /dev/null +++ b/electron/coding-teacher/reply.ts @@ -0,0 +1,20 @@ +import type { TeacherRequest } from '../../shared/coding-teacher'; +import { parseTeacherReply } from '../../shared/teacher-reply'; + +export function teacherReplyInstructions(): string { + return '本轮界面接收一个 JSON 对象 {"reply":string,"quickReplies":string[]}。reply 是回复正文,可使用 Markdown;quickReplies 是可选的后续回复建议,没有建议时用空数组。只使用这两个字段。依据已配置职责帮助学生整理想法、理解关系、承接已确认的共识;区分学生已确认内容与智能体建议、待定想法。回复的语言、风格、长度、前缀和建议内容遵循当前智能体的云端配置。'; +} + +/** The original response remains available for diagnosis when parsing is incomplete. */ +export function applyTeacherReply(request: TeacherRequest, raw: string): void { + const parsed = parseTeacherReply(raw); + request.response = parsed.reply; + request.suggestedQuestions = parsed.quickReplies; + if (parsed.parseError) { + request.replyParseError = parsed.parseError; + request.unparsedResponse = raw; + } else { + delete request.replyParseError; + delete request.unparsedResponse; + } +} diff --git a/electron/coding-teacher/service.ts b/electron/coding-teacher/service.ts index 53ff6e6d..1e19eec7 100644 --- a/electron/coding-teacher/service.ts +++ b/electron/coding-teacher/service.ts @@ -9,7 +9,6 @@ import type { TeacherCheckInInput, TeacherCheckInResult, TeacherDefinition, - TeacherDiscussionAction, TeacherReference, TeacherRequest, TeacherSend, @@ -35,8 +34,7 @@ import { compileTeacherContext } from './context'; import { prepareTeacherModel } from './model-runner'; import { prepareCloudTeacher } from './cloud-runner'; import { readTeacherSource } from './source-reader'; -import { parseTeacherSuggestions } from './suggestions'; -import { applyDiscussionReply, discussionInstructions, editDiscussion, validateDiscussionContext } from './discussion'; +import { applyTeacherReply, teacherReplyInstructions } from './reply'; import { subscribeWorksSquareSession } from '../services/works-square-session'; export interface TeacherScope { @@ -418,11 +416,7 @@ export class CodingTeacherService { ): Promise { teacherTopicId(input.requestId); const intent = input.intent === undefined ? 'question' : input.intent; - if (input.presentation !== undefined && input.presentation !== 'discussion-v1') - throw new TeacherError(422, 'teacher_presentation_invalid', '智能体的显示方式无效。'); - if (input.discussion !== undefined && input.presentation !== 'discussion-v1') - throw new TeacherError(422, 'teacher_discussion_invalid', '讨论方式无效。'); - const structuredReply = input.presentation === 'discussion-v1' && (intent === 'question' || intent === 'guided-help'); + const structuredReply = intent !== 'check-in'; if (!['question', 'suggestions', 'guided-help', 'check-in'].includes(intent) || (intent === 'check-in' && !checkIn)) throw new TeacherError(422, 'teacher_intent_invalid', '提问方式无效,请重新打开智能体后再试。'); if (intent !== 'question' && ( @@ -461,9 +455,7 @@ export class CodingTeacherService { (scope.agentId && existing.projectId !== scope.projectId) || (existing.intent ?? 'question') !== intent || JSON.stringify(existing.references) !== JSON.stringify(refs) || - (existing.sourceConversationId ?? undefined) !== (input.sourceConversationId ?? undefined) || - existing.presentation !== input.presentation || - JSON.stringify(existing.discussionContext) !== JSON.stringify(input.discussion) + (existing.sourceConversationId ?? undefined) !== (input.sourceConversationId ?? undefined) ) throw new TeacherError(409, 'teacher_request_conflict', '同一请求标识不能用于不同问题。'); return structuredClone(topic); @@ -484,7 +476,6 @@ export class CodingTeacherService { topic.version = selected.version; topic.projectId = scope.projectId; topic.sourceConversationId = 'project'; - topic.discussion = topic.conversation!.discussions[scope.projectId]; const previous = topic.requests.at(-1); const reuse = previous && !previous.origin && previous.projectId === scope.projectId && previous.sourceConversationId === input.sourceConversationId @@ -496,7 +487,6 @@ export class CodingTeacherService { topic.conversation!.runtimeThreadId = reuse || randomUUID(); topic.conversation!.segmentTurns = reuse ? topic.conversation!.segmentTurns + 1 : 1; } - const discussionContext = validateDiscussionContext(topic, input.discussion); if (topic.draftRevision) { await (this.options.preview ?? teacherPreview)(account, topic.draftRevision); } else if (topic.definition.runtime !== 'yuxi' && topic.definition.config_id) { @@ -583,7 +573,7 @@ export class CodingTeacherService { }) : await (this.options.prepareModel ?? prepareTeacherModel)(account, topic.definition, access, - { finalOnly: structuredReply || intent === 'suggestions' }); + { finalOnly: structuredReply }); if (checkIn) this.assertAccount(account); const compiled = compileTeacherContext( topic.definition, @@ -593,7 +583,7 @@ export class CodingTeacherService { references, model.inputLimit, intent, - structuredReply ? discussionInstructions(topic, discussionContext) : undefined, + structuredReply ? teacherReplyInstructions() : undefined, scope.projectId !== 'preview', model.measureInput ); @@ -616,8 +606,7 @@ export class CodingTeacherService { teacherVersion: topic.version, runtimeThreadId: topic.conversation!.runtimeThreadId } : {}), id: input.requestId, intent, - ...(input.presentation ? { presentation: input.presentation } : {}), - ...(discussionContext ? { discussionContext } : {}), + ...(structuredReply ? { presentation: 'reply-v1' as const } : {}), ...(input.sourceConversationId ? { sourceConversationId: input.sourceConversationId } : {}), ...(checkIn ? { checkInSourceFingerprint: checkIn.fingerprint } : {}), text: input.text, @@ -649,13 +638,13 @@ export class CodingTeacherService { const release = this.options.acquireLease?.(key) ?? (() => undefined); const finish = async () => { const current = topic.requests.find(request => request.id === input.requestId)!; + let structuredText = ''; try { this.assertAccount(account); if (controller.signal.aborted) throw controller.signal.reason; current.status = 'running'; topic.revision++; this.events.emit(key, structuredClone(topic)); - let structuredText = ''; current.usage = await model.run(compiled.messages, controller.signal, (delta) => { if (structuredReply) { structuredText += delta; return; } current.response += delta; @@ -663,12 +652,7 @@ export class CodingTeacherService { this.events.emit(key, structuredClone(topic)); }); this.assertAccount(account); - if (!controller.signal.aborted && structuredReply) applyDiscussionReply(topic, current, structuredText); - if (!controller.signal.aborted && intent === 'suggestions') { - const suggestions = parseTeacherSuggestions(current.response); - current.response = suggestions.intro; - current.suggestedQuestions = suggestions.questions; - } + if (!controller.signal.aborted && structuredReply) applyTeacherReply(current, structuredText); current.status = controller.signal.aborted ? 'cancelled' : 'completed'; } catch (error) { current.status = controller.signal.aborted ? 'cancelled' : 'failed'; @@ -678,6 +662,9 @@ export class CodingTeacherService { ? error.message : '智能体回复失败,已保留本次问题与收到的内容。'; } finally { + // Keep received bytes inert even when an aborted runner resolves normally. + if (structuredReply && current.status !== 'completed' && structuredText) + current.unparsedResponse = structuredText; if (intent === 'suggestions' && current.status !== 'completed') { current.response = ''; delete current.suggestedQuestions; @@ -685,7 +672,6 @@ export class CodingTeacherService { topic.updatedAt = new Date().toISOString(); topic.revision++; try { - if (topic.conversation && topic.discussion) topic.conversation.discussions[scope.projectId] = topic.discussion; await store.save(topic, current.id); } catch { topic.unsaved = true; @@ -701,30 +687,6 @@ export class CodingTeacherService { return structuredClone(topic); }); } - async updateDiscussion(scope: TeacherScope, id: string, input: TeacherDiscussionAction) { - const account = await this.account(); - this.assertWritable(scope); - const key = this.key(account, scope, id); - return await this.serialize(key, async () => { - const { store, topic } = await this.readOwned(account, scope, id); - if (this.active.has(key) || topic.requests.some(request => ['preparing', 'running'].includes(request.status))) - throw new TeacherError(409, 'teacher_topic_busy', '请等待智能体回复,或先停止。'); - const next = structuredClone(topic); - if (next.conversation) { - if (!input.projectId) throw new TeacherError(422, 'teacher_project_required', '请选择讨论所属的项目。'); - await this.options.projects.getProject(input.projectId); - next.discussion = next.conversation.discussions[input.projectId]; - } - editDiscussion(next, input); - if (next.conversation && next.discussion) next.conversation.discussions[input.projectId!] = next.discussion; - next.revision++; - next.updatedAt = new Date().toISOString(); - this.assertAccount(account); - await store.save(next, scope.agentId ? '' : undefined); - this.events.emit(key, structuredClone(next)); - return structuredClone(next); - }); - } async cancel(scope: TeacherScope, id: string, requestId: string) { const account = await this.account(); const { topic } = await this.readOwned(account, scope, id); diff --git a/electron/coding-teacher/store.ts b/electron/coding-teacher/store.ts index 0af76b66..69a8ba29 100644 --- a/electron/coding-teacher/store.ts +++ b/electron/coding-teacher/store.ts @@ -3,7 +3,6 @@ import path from 'node:path'; import { atomicWriteJson, readJsonFile } from '../coding-projects/atomic-json'; import type { TeacherTopic, TeacherTopicList } from '../../shared/coding-teacher'; import { TeacherError } from './config-client'; -import { parseTeacherDiscussionContent } from '../../shared/teacher-discussion'; export function teacherTopicId(id: string): string { if (!/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i.test(id)) @@ -60,30 +59,7 @@ export class TeacherTopicStore { if (topic.id !== id || topic.schemaVersion !== 1 || !Array.isArray(topic.requests)) throw new TeacherError(409, 'teacher_topic_invalid', '智能体历史无法读取。'); let recovered = false; - if (topic.discussion) { - try { - const discussion = topic.discussion; - teacherTopicId(discussion.id); - if (!Number.isSafeInteger(discussion.revision) || discussion.revision < 1 - || !['offered', 'active', 'paused', 'finished'].includes(discussion.status)) throw new Error('Invalid discussion'); - discussion.content = parseTeacherDiscussionContent(discussion.content); - for (const key of ['previousIdeas', 'previousStructure'] as const) { - if (!discussion[key]) continue; - try { - const previous = parseTeacherDiscussionContent(discussion[key]); - if (key === 'previousIdeas' && previous.kind === 'ideas') discussion.previousIdeas = previous; - else if (key === 'previousStructure' && previous.kind === 'structure') discussion.previousStructure = previous; - else throw new Error('Invalid history'); - } catch { delete discussion[key]; recovered = true; } - } - } catch { delete topic.discussion; recovered = true; } - } - for (const request of topic.requests) { - if (request.discussionSnapshot) { - try { request.discussionSnapshot = parseTeacherDiscussionContent(request.discussionSnapshot); } - catch { delete request.discussionSnapshot; recovered = true; } - } - } + // Legacy component fields are opaque archives: never validate, revive or delete them. for (const request of topic.requests) if (request.status === 'preparing' || request.status === 'running') { request.status = 'interrupted'; diff --git a/electron/coding-teacher/suggestions.ts b/electron/coding-teacher/suggestions.ts deleted file mode 100644 index 62f1749e..00000000 --- a/electron/coding-teacher/suggestions.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { TeacherError } from './config-client'; - -export interface TeacherSuggestions { - intro: string; - questions: string[]; -} - -/** Validate model output before it can become interactive student questions. */ -export function parseTeacherSuggestions(response: string): TeacherSuggestions { - const invalid = () => new TeacherError( - 502, - 'teacher_suggestions_invalid', - '智能体这次没能整理好可以讨论的问题,请再试一次,或直接告诉智能体你的想法。' - ); - const text = response.trim(); - const fenced = text.match(/^```(?:json)?\s*\n([\s\S]*?)\n```$/i); - let parsed: unknown; - try { - parsed = JSON.parse(fenced ? fenced[1] : text); - } catch { - throw invalid(); - } - if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) throw invalid(); - const { intro, questions } = parsed as Record; - if ( - typeof intro !== 'string' || !intro.trim() || intro.trim().length > 400 || - !Array.isArray(questions) || questions.length < 2 || questions.length > 3 || - questions.some((question) => typeof question !== 'string' || !question.trim() || question.trim().length > 120) - ) throw invalid(); - const uniqueQuestions = [...new Set(questions.map((question: string) => question.trim()))]; - if (uniqueQuestions.length < 2) throw invalid(); - return { intro: intro.trim(), questions: uniqueQuestions }; -} diff --git a/shared/coding-teacher.ts b/shared/coding-teacher.ts index b4f92c05..59f443bf 100644 --- a/shared/coding-teacher.ts +++ b/shared/coding-teacher.ts @@ -1,4 +1,3 @@ -import type { TeacherDiscussion, TeacherDiscussionContent } from './teacher-discussion'; import type { PublicUsage } from './coding-conversation-contracts'; /** Only retained for locating conversations created by older clients. */ @@ -67,19 +66,6 @@ export type TeacherRequestStatus = | 'failed' | 'cancelled' | 'interrupted'; -export interface TeacherDiscussionContext { - toolId: string; - revision: number; - focusId?: string; - transition?: 'structure'; -} -export interface TeacherDiscussionAction { - projectId?: string; - toolId: string; - revision: number; - action: 'enter' | 'pause' | 'finish' | 'resume' | 'keep-idea' | 'aside-idea' | 'first-idea' | 'back-ideas'; - itemId?: string; -} export interface TeacherRequest { /** Frozen when this turn is accepted; history can span projects and releases. */ projectId?: string; @@ -87,11 +73,14 @@ export interface TeacherRequest { teacherVersion?: number; runtimeThreadId?: string; origin?: { projectId: string; topicId: string; requestId: string }; - presentation?: 'discussion-v1'; - discussionContext?: TeacherDiscussionContext; - discussionSnapshot?: TeacherDiscussionContent; + /** discussion-v1 is retained only to identify historical replies. */ + presentation?: 'reply-v1' | 'discussion-v1'; + /** Archived legacy data; never parsed into interactive component state. */ + discussionContext?: unknown; + discussionSnapshot?: unknown; discussionError?: string; - /** Original answer retained when discussion parsing fails; never fed back as context. */ + replyParseError?: string; + /** Original answer retained when reply parsing fails; never fed back as context. */ unparsedResponse?: string; toolActivity?: TeacherToolActivity[]; @@ -127,10 +116,12 @@ export interface TeacherTopic { agentId: string; runtimeThreadId?: string; segmentTurns: number; - discussions: Record; + /** Archived per-project component data; never selected or updated by new turns. */ + discussions?: Record; seenThrough?: string; }; - discussion?: TeacherDiscussion; + /** Archived legacy data; new requests do not create or update it. */ + discussion?: unknown; role?: LegacyConsultationRole; revision: number; schemaVersion: 1; @@ -152,8 +143,6 @@ export interface TeacherTopicList { } export interface TeacherSend { projectId?: string; - presentation?: 'discussion-v1'; - discussion?: TeacherDiscussionContext; intent?: TeacherRequestIntent; sourceConversationId?: string; requestId: string; diff --git a/shared/teacher-discussion.ts b/shared/teacher-discussion.ts deleted file mode 100644 index 4e01f4c9..00000000 --- a/shared/teacher-discussion.ts +++ /dev/null @@ -1,305 +0,0 @@ -/** Content comes from the teacher; rendering, lifecycle, and type locking belong to the app. */ -export interface TeacherIdeaItem { - id: string; - text: string; - parentId?: string; - state: 'kept' | 'suggested' | 'aside'; -} - -export interface TeacherStructureNode { - id: string; - label: string; - relation?: string; - parentId?: string; -} - -export interface TeacherFlowNode { - id: string; - label: string; - kind: 'event' | 'condition' | 'outcome'; -} - -export interface TeacherFlowEdge { - id: string; - from: string; - to: string; - label?: string; -} - -export interface TeacherComparisonColumn { - id: string; - label: string; -} - -export interface TeacherComparisonRow { - id: string; - label: string; - cells: Array<{ columnId: string; text: string }>; -} - -export type TeacherDiscussionContent = - | { kind: 'ideas'; title: string; items: TeacherIdeaItem[]; firstItemId?: string } - | { kind: 'structure'; title: string; nodes: TeacherStructureNode[] } - | { kind: 'flow'; title: string; nodes: TeacherFlowNode[]; edges: TeacherFlowEdge[] } - | { kind: 'comparison'; title: string; columns: TeacherComparisonColumn[]; rows: TeacherComparisonRow[] }; - -export interface TeacherDiscussion { - id: string; - revision: number; - status: 'offered' | 'active' | 'paused' | 'finished'; - content: TeacherDiscussionContent; - previousIdeas?: Extract; - previousStructure?: Extract; -} - -export interface TeacherDiscussionReply { - reply: string; - quickReplies: string[]; - tool?: TeacherDiscussionContent | null; - toolError?: string; -} - -const MAX_REPLY_LENGTH = 12000; -const MAX_INPUT_BYTES = 64000; -const INVALID_REPLY = '这次回复没有整理完整,请再试一次。'; -const INVALID_TOOL = '这次整理没有完成,先保留原来的内容。'; -const dangerousKeys = new Set(['__proto__', 'prototype', 'constructor']); - -function invalid(): never { - // Validation errors contain no untrusted model content. - throw new Error('老师组件内容格式无效或超过长度限制。'); -} - -function record(value: unknown): Record { - if (!value || typeof value !== 'object' || Array.isArray(value)) return invalid(); - const prototype = Object.getPrototypeOf(value); - if (prototype !== Object.prototype && prototype !== null) return invalid(); - for (const [key, descriptor] of Object.entries(Object.getOwnPropertyDescriptors(value))) { - if (dangerousKeys.has(key) || !('value' in descriptor)) return invalid(); - } - return value as Record; -} - -function text(value: unknown, max: number, allowEmpty = false): string { - if (typeof value !== 'string' || value.length > max || (!allowEmpty && !value.trim())) return invalid(); - return value.trim(); -} - -function id(value: unknown): string { - if (typeof value !== 'string' || !/^[A-Za-z0-9_-]{1,64}$/.test(value) || dangerousKeys.has(value)) return invalid(); - return value; -} - -function array(value: unknown, min: number, max: number): unknown[] { - if (!Array.isArray(value) || value.length < min || value.length > max) return invalid(); - // Do not invoke accessor elements, overridden map methods, or custom iterators. - const projected: unknown[] = []; - for (let index = 0; index < value.length; index++) { - const descriptor = Object.getOwnPropertyDescriptor(value, index); - if (!descriptor || !('value' in descriptor)) return invalid(); - projected.push(descriptor.value); - } - return projected; -} - -function uniqueIds(values: T[]): Set { - const ids = new Set(values.map(value => value.id)); - if (ids.size !== values.length) return invalid(); - return ids; -} - -function optionalParent(value: Record): { parentId?: string } { - return value.parentId === undefined ? {} : { parentId: id(value.parentId) }; -} - -function validateParents(values: Array<{ id: string; parentId?: string }>): void { - const ids = uniqueIds(values); - const parents = new Map(values.map(value => [value.id, value.parentId])); - for (const value of values) { - const path = new Set([value.id]); - let parent = value.parentId; - while (parent !== undefined) { - if (!ids.has(parent) || path.has(parent)) return invalid(); - path.add(parent); - parent = parents.get(parent); - } - } -} - -/** Validate and project only the supported data fields. No HTML, style, or executable actions are copied. */ -export function parseTeacherDiscussionContent(value: unknown): TeacherDiscussionContent { - const content = record(value); - const title = text(content.title, 120); - switch (content.kind) { - case 'ideas': { - const items: TeacherIdeaItem[] = array(content.items, 1, 24).map(raw => { - const item = record(raw); - if (item.state !== 'kept' && item.state !== 'suggested' && item.state !== 'aside') return invalid(); - return { id: id(item.id), text: text(item.text, 600), ...optionalParent(item), state: item.state }; - }); - validateParents(items); - if (content.firstItemId !== undefined) { - const firstItemId = id(content.firstItemId); - if (!items.some(item => item.id === firstItemId && item.state === 'kept')) return invalid(); - return { kind: 'ideas', title, items, firstItemId }; - } - return { kind: 'ideas', title, items }; - } - case 'structure': { - const nodes: TeacherStructureNode[] = array(content.nodes, 1, 24).map(raw => { - const node = record(raw); - return { - id: id(node.id), label: text(node.label, 600), ...optionalParent(node), - ...(node.relation === undefined ? {} : { relation: text(node.relation, 120) }), - }; - }); - validateParents(nodes); - return { kind: 'structure', title, nodes }; - } - case 'flow': { - const nodes: TeacherFlowNode[] = array(content.nodes, 1, 24).map(raw => { - const node = record(raw); - if (node.kind !== 'event' && node.kind !== 'condition' && node.kind !== 'outcome') return invalid(); - return { id: id(node.id), label: text(node.label, 600), kind: node.kind }; - }); - const nodeIds = uniqueIds(nodes); - const edges: TeacherFlowEdge[] = array(content.edges, 0, 40).map(raw => { - const edge = record(raw); - const from = id(edge.from), to = id(edge.to); - if (!nodeIds.has(from) || !nodeIds.has(to)) return invalid(); - return { - id: id(edge.id), from, to, - ...(edge.label === undefined ? {} : { label: text(edge.label, 160) }), - }; - }); - uniqueIds(edges); - // Explicit cycles are valid: a game loop or failed attempt may return to an earlier event. - return { kind: 'flow', title, nodes, edges }; - } - case 'comparison': { - const columns: TeacherComparisonColumn[] = array(content.columns, 2, 4).map(raw => { - const column = record(raw); - return { id: id(column.id), label: text(column.label, 120) }; - }); - const columnIds = uniqueIds(columns); - const rows: TeacherComparisonRow[] = array(content.rows, 1, 12).map(raw => { - const row = record(raw); - const cells = array(row.cells, columns.length, columns.length).map(rawCell => { - const cell = record(rawCell); - const columnId = id(cell.columnId); - if (!columnIds.has(columnId)) return invalid(); - return { columnId, text: text(cell.text, 600) }; - }); - if (new Set(cells.map(cell => cell.columnId)).size !== columns.length) return invalid(); - const byColumn = new Map(cells.map(cell => [cell.columnId, cell])); - return { - id: id(row.id), label: text(row.label, 160), - cells: columns.map(column => byColumn.get(column.id)!), - }; - }); - uniqueIds(rows); - return { kind: 'comparison', title, columns, rows }; - } - default: return invalid(); - } -} - -function quickReplies(value: unknown): string[] { - if (value === undefined) return []; - try { - return [...new Set(array(value, 0, 3).map(reply => text(reply, 120)))]; - } catch { return []; } -} - -function stringToken(raw: string, start: number): { value: string; end: number } | undefined { - for (let cursor = start + 1; cursor < raw.length; cursor++) { - if (raw[cursor] === '\\') { cursor++; continue; } - if (raw[cursor] !== '"') continue; - try { - const value: unknown = JSON.parse(raw.slice(start, cursor + 1)); - if (typeof value === 'string') return { value, end: cursor + 1 }; - } catch { return undefined; } - } - return undefined; -} - -/** Inspect only top-level fields, including when a later value was truncated. */ -function findTopLevelField(raw: string, names: readonly string[]): number | undefined { - if (!raw.trimStart().startsWith('{')) return undefined; - let depth = 0, expectingKey = false; - for (let cursor = 0; cursor < raw.length; cursor++) { - const character = raw[cursor]; - if (character === '"') { - const token = stringToken(raw, cursor); - if (!token) return undefined; - if (depth === 1 && expectingKey && names.includes(token.value)) { - let valueStart = token.end; - while (/\s/.test(raw[valueStart] ?? '') && valueStart < raw.length) valueStart++; - if (raw[valueStart] !== ':') return undefined; - valueStart++; - while (/\s/.test(raw[valueStart] ?? '') && valueStart < raw.length) valueStart++; - return valueStart; - } - if (depth === 1) expectingKey = false; - cursor = token.end - 1; - } else if (character === '{' || character === '[') { - depth++; - if (depth === 1) expectingKey = character === '{'; - } else if (character === '}' || character === ']') { - depth--; - } else if (character === ',' && depth === 1) { - expectingKey = true; - } - } - return undefined; -} - -/** Recover a complete reply string, never nested tool data or a partial string. */ -function recoverReply(raw: string): string | undefined { - const start = findTopLevelField(raw, ['reply']); - if (start === undefined || raw[start] !== '"') return undefined; - const value = stringToken(raw, start)?.value; - return value?.trim() && value.length <= MAX_REPLY_LENGTH ? value.trim() : undefined; -} - -function invalidReply(candidate: string): TeacherDiscussionReply { - return { reply: recoverReply(candidate) ?? INVALID_REPLY, quickReplies: [], toolError: INVALID_TOOL }; -} - -/** Parse final model output. Callers keep the existing tool whenever toolError is present. */ -export function parseTeacherDiscussionReply(raw: string): TeacherDiscussionReply { - const trimmed = raw.trim(); - // A Markdown link, JSON example or code fence is not a discussion envelope. - // Only our explicit fence or top-level protocol fields select this parser. - const firstFence = /(?:^|\n)[ \t]*```([^\r\n]*)[ \t]*(?:\r?\n|$)/.exec(trimmed); - const language = firstFence?.[1].trim().toLowerCase(); - const explicitFence = language === 'makelore-teacher' || language === 'makelore-teacher-discussion'; - const opening = firstFence && (explicitFence || language === 'json' || language === '') ? firstFence : null; - const unfencedEnvelope = !firstFence && !trimmed.startsWith('{') - ? /(?:^|\n)[ \t]*(\{\s*"(?:reply|quickReplies|tool)"\s*:)/.exec(trimmed) : null; - let candidate = unfencedEnvelope ? trimmed.slice(unfencedEnvelope.index).trimStart() : trimmed; - let validFence = true; - if (opening) { - const remainder = trimmed.slice(opening.index + opening[0].length); - const closing = /\r?\n```[ \t]*$/.exec(remainder); - candidate = closing ? remainder.slice(0, closing.index) : remainder; - validFence = !!closing && !/(?:^|\n)[ \t]*```/.test(candidate); - } - if (!explicitFence && findTopLevelField(candidate, ['reply', 'quickReplies', 'tool']) === undefined) { - return { reply: trimmed.slice(0, MAX_REPLY_LENGTH) || INVALID_REPLY, quickReplies: [] }; - } - if (new TextEncoder().encode(trimmed).length > MAX_INPUT_BYTES) return invalidReply(candidate.slice(0, MAX_INPUT_BYTES)); - if (!validFence) return invalidReply(candidate); - try { - const envelope = record(JSON.parse(candidate)); - const reply = text(envelope.reply, MAX_REPLY_LENGTH, true); - const result: TeacherDiscussionReply = { reply, quickReplies: quickReplies(envelope.quickReplies) }; - if (envelope.tool === null) result.tool = null; - else if (envelope.tool !== undefined) { - try { result.tool = parseTeacherDiscussionContent(envelope.tool); } - catch { result.toolError = INVALID_TOOL; } - } - if (!reply && !result.tool) result.reply = INVALID_REPLY; - return result; - } catch { return invalidReply(candidate); } -} diff --git a/shared/teacher-reply.ts b/shared/teacher-reply.ts new file mode 100644 index 00000000..62eec581 --- /dev/null +++ b/shared/teacher-reply.ts @@ -0,0 +1,198 @@ +export interface TeacherReply { + reply: string; + quickReplies: string[]; + parseError?: string; +} + +// Resource limits, not writing instructions. Ordinary answers below the transport +// limit are preserved in full, including Markdown and long suggested questions. +export const MAX_TEACHER_REPLY_BYTES = 1024 * 1024; +export const MAX_TEACHER_QUICK_REPLIES = 128; +export const MAX_TEACHER_QUICK_REPLY_BYTES = 64 * 1024; +const INVALID_REPLY = '这次回复没有完整解析,请重试。'; +const INVALID_FORMAT = '回复格式不完整或无效。'; +const RESOURCE_LIMIT = '回复超过可处理的数据大小。'; +const protocolKeys = new Set(['reply', 'quickReplies', 'intro', 'questions', 'tool']); +const own = (value: object, key: string) => Object.prototype.hasOwnProperty.call(value, key); + +function withinBytes(value: string, max: number): boolean { + return value.length <= max && new TextEncoder().encode(value).length <= max; +} + +function stringEnd(raw: string, start: number): number | undefined { + for (let index = start + 1; index < raw.length; index++) { + if (raw[index] === '\\') { index++; continue; } + if (raw[index] === '"') return index + 1; + } + return undefined; +} + +/** Find one complete value without interpreting nested legacy tool data. */ +function valueEnd(raw: string, start: number): number | undefined { + if (raw[start] === '"') return stringEnd(raw, start); + if (raw[start] !== '{' && raw[start] !== '[') { + let end = start; + while (end < raw.length && !/[,}\]\s]/.test(raw[end])) end++; + return end > start ? end : undefined; + } + const stack = [raw[start]]; + for (let index = start + 1; index < raw.length; index++) { + const character = raw[index]; + if (character === '"') { + const end = stringEnd(raw, index); + if (end === undefined) return undefined; + index = end - 1; + } else if (character === '{' || character === '[') stack.push(character); + else if (character === '}' || character === ']') { + if (stack.pop() !== (character === '}' ? '{' : '[')) return undefined; + if (!stack.length) return index + 1; + } + } + return undefined; +} + +/** Recover only complete top-level fields; nested reply-looking data stays inert. */ +function fields(raw: string): { values: Map; keys: Set; duplicate: boolean; toolStart?: number } { + const values = new Map(), keys = new Set(); + let duplicate = false, index = raw.indexOf('{') + 1; + let toolStart: number | undefined; + const whitespace = () => { while (index < raw.length && /\s/.test(raw[index])) index++; }; + while (index > 0 && index < raw.length) { + whitespace(); + if (raw[index] !== '"') break; + const end = stringEnd(raw, index); + if (end === undefined) break; + let key: string; + try { key = JSON.parse(raw.slice(index, end)); } catch { break; } + index = end; + whitespace(); + if (raw[index++] !== ':') break; + duplicate ||= keys.has(key); + keys.add(key); + whitespace(); + if (key === 'tool') toolStart = index; + const next = valueEnd(raw, index); + if (next === undefined) break; + if ((protocolKeys.has(key) || key === 'kind') && key !== 'tool' && !values.has(key)) { + try { values.set(key, JSON.parse(raw.slice(index, next))); } catch { break; } + } + index = next; + whitespace(); + if (raw[index++] !== ',') break; + } + return { values, keys, duplicate, toolStart }; +} + +interface Candidate { start: number; end: number; content: string; incomplete?: boolean } +function isEnvelope(raw: string): boolean { + const scanned = fields(raw); + if (scanned.keys.has('reply') || scanned.keys.has('quickReplies') + || (scanned.keys.has('intro') && scanned.keys.has('questions'))) return true; + // "questions" and "tool" also occur in ordinary API/code examples. A tool + // alone identifies the retired reply protocol only through its legacy kind. + if (scanned.toolStart !== undefined && raw[scanned.toolStart] === '{') { + const kind = fields(raw.slice(scanned.toolStart)).values.get('kind'); + if (typeof kind === 'string' && ['ideas', 'structure', 'flow', 'comparison'].includes(kind)) return true; + } + return /^\{\s*(?:reply|quickReplies)\s*:/.test(raw); +} + +/** Markdown examples stay prose unless a top-level reply envelope identifies them. */ +function candidates(raw: string): Candidate[] { + const result: Candidate[] = []; + const inspectText = (start: number, end: number) => { + for (let index = start; index < end; index++) { + if (raw[index] === '`') { + let fenceEnd = index + 1; + while (raw[fenceEnd] === '`') fenceEnd++; + const close = raw.indexOf(raw.slice(index, fenceEnd), fenceEnd); + index = close >= 0 && close < end ? close + fenceEnd - index - 1 : end; + } else if (raw[index] === '[') { + const endOfArray = valueEnd(raw, index); + if (endOfArray !== undefined && endOfArray <= end) { + try { + if (Array.isArray(JSON.parse(raw.slice(index, endOfArray)))) index = endOfArray - 1; + } catch { /* A Markdown link is not a JSON array. */ } + } + } else if (raw[index] === '{') { + const objectEnd = valueEnd(raw, index); + const stop = objectEnd !== undefined && objectEnd <= end ? objectEnd : end; + const content = raw.slice(index, stop); + if (isEnvelope(content)) result.push({ start: index, end: stop, content }); + index = stop - 1; + } + } + }; + const opening = /(?:^|\n)[ \t]*(`{3,}|~{3,})([^\r\n]*)\r?\n/g; + let processed = 0, match: RegExpExecArray | null; + while ((match = opening.exec(raw))) { + inspectText(processed, match.index); + const bodyStart = opening.lastIndex; + const marker = match[1][0], size = match[1].length; + const closing = new RegExp('(?:^|\\n)[ \\t]*' + marker + '{' + size + ',}[ \\t]*(?=\\r?\\n|$)', 'g'); + closing.lastIndex = bodyStart; + const close = closing.exec(raw); + const body = raw.slice(bodyStart, close?.index ?? raw.length).trim(); + const language = match[2].trim().toLowerCase(); + const explicit = ['makelore-teacher', 'makelore-teacher-reply', 'makelore-teacher-discussion'].includes(language); + if (explicit || ((language === 'json' || language === '') && body.startsWith('{') && isEnvelope(body))) { + result.push({ start: match.index, end: close ? closing.lastIndex : raw.length, content: body, incomplete: !close }); + } + processed = close ? closing.lastIndex : raw.length; + opening.lastIndex = processed; + } + inspectText(processed, raw.length); + return result; +} + +function projectReply(envelope: Record): TeacherReply { + const reply = own(envelope, 'reply') ? envelope.reply : envelope.intro; + const suggestions = own(envelope, 'quickReplies') ? envelope.quickReplies : envelope.questions; + let parseError: string | undefined; + let quickReplies: string[] = []; + if (suggestions !== undefined) { + if (!Array.isArray(suggestions) || suggestions.some(item => typeof item !== 'string' || !item.trim())) { + parseError = INVALID_FORMAT; + } else if (suggestions.length > MAX_TEACHER_QUICK_REPLIES + || suggestions.some(item => !withinBytes(item, MAX_TEACHER_QUICK_REPLY_BYTES))) { + parseError = RESOURCE_LIMIT; + } else quickReplies = suggestions; + } + if (typeof reply !== 'string' || (!reply.trim() && !quickReplies.length)) { + return { reply: INVALID_REPLY, quickReplies, parseError: INVALID_FORMAT }; + } + return { reply, quickReplies, ...(parseError ? { parseError } : {}) }; +} + +/** Only text and suggested replies survive; legacy tool payloads are never restored. */ +export function parseTeacherReply(raw: string): TeacherReply { + if (!withinBytes(raw, MAX_TEACHER_REPLY_BYTES)) { + return { reply: INVALID_REPLY, quickReplies: [], parseError: RESOURCE_LIMIT }; + } + const text = raw.trim(); + if (!text) return { reply: INVALID_REPLY, quickReplies: [], parseError: INVALID_FORMAT }; + const found = candidates(text); + if (!found.length) return { reply: text, quickReplies: [] }; + const candidate = found[0]; + let result: TeacherReply; + const recovered = fields(candidate.content); + try { + const envelope: unknown = JSON.parse(candidate.content); + if (!envelope || typeof envelope !== 'object' || Array.isArray(envelope)) throw new Error(); + result = projectReply(envelope as Record); + } catch { + result = projectReply(Object.fromEntries(recovered.values)); + result.parseError = INVALID_FORMAT; + } + if (candidate.incomplete || recovered.duplicate || found.length > 1) result.parseError = INVALID_FORMAT; + // Multiple envelopes are ambiguous; never append their JSON as visible prose. + if (found.length === 1) { + const suffix = text.slice(candidate.end).trim(); + const brokenTail = /^[,\s]*"(?:reply|quickReplies|intro|questions|tool)"\s*:/.test(suffix) + || (suffix.length > 0 && /^[}\],\s]+$/.test(suffix)); + if (brokenTail) result.parseError = INVALID_FORMAT; + result.reply = [text.slice(0, candidate.start).trim(), result.reply, brokenTail ? '' : suffix] + .filter(part => part.length > 0).join('\n\n'); + } + return result; +} diff --git a/src/lib/coding-teacher.ts b/src/lib/coding-teacher.ts index 278af0db..a543bcac 100644 --- a/src/lib/coding-teacher.ts +++ b/src/lib/coding-teacher.ts @@ -5,7 +5,6 @@ import type { TeacherCheckInResult, TeacherCatalog, TeacherDefinition, - TeacherDiscussionAction, TeacherSend, TeacherTopic, TeacherTopicList, @@ -61,10 +60,6 @@ export const teacherApi = { method: 'POST', body: JSON.stringify(input), }), - updateDiscussion: (base: string, id: string, input: TeacherDiscussionAction) => - hostApiFetch(base + '/' + encodeURIComponent(id) + '/discussion', { - method: 'POST', body: JSON.stringify(input), - }), cancel: (base: string, id: string, requestId: string) => hostApiFetch( base + diff --git a/src/pages/Chat/AgentBrowserPanel.tsx b/src/pages/Chat/AgentBrowserPanel.tsx index bc8e0220..25e1feb3 100644 --- a/src/pages/Chat/AgentBrowserPanel.tsx +++ b/src/pages/Chat/AgentBrowserPanel.tsx @@ -480,23 +480,24 @@ export function AgentBrowserPanel({ ref={panelRef} className={cn(embedded ? 'relative flex min-h-0 w-full min-w-0 flex-1 flex-col overflow-hidden bg-background' : 'relative flex w-[400px] min-w-[300px] max-w-[48vw] shrink-0 flex-col overflow-hidden border-l border-border/80 bg-background shadow-[-16px_0_32px_hsl(220_20%_16%_/_0.06)] max-[900px]:fixed max-[900px]:inset-y-10 max-[900px]:right-0 max-[900px]:z-40 max-[900px]:max-w-[min(92vw,420px)]')} data-testid="agent-browser-panel" + aria-label={embedded ? '作品预览' : '开发浏览器'} > -
+ {!embedded &&
-

{embedded ? '作品预览' : '开发浏览器'}

+

开发浏览器

-
+
}
} } - {discussionEnabled && topic?.discussion && void updateDiscussion(action, itemId)} onFocus={focusItem} onStructure={structureIdeas} - onDiscuss={(id, question) => { - focusItem(id, question); - void send({ text: question, discussion: { toolId: topic.discussion!.id, revision: topic.discussion!.revision, focusId: id } }); - }} />}
{ const pane = scrollRef.current; if (!pane) return; scrollPosition.current = pane.scrollTop; @@ -559,16 +514,14 @@ export function TeacherChatPanel({ : request.status === 'completed' && <>
- {request.suggestedQuestions?.map((question) => )} + {request.suggestedQuestions?.map((question, index) => )}
- } - {['failed', 'cancelled', 'interrupted'].includes(request.status) && } - : request.response ? :
{['running', 'preparing'].includes(request.status) ? {request.discussionContext ? '正在梳理你的新想法…' : `${label}正在想…`} : ['cancelled', 'interrupted'].includes(request.status) ? '这次先停在这里。想继续时可以再说说。' : ''}
} - {discussionEnabled && request.intent !== 'suggestions' && request.status === 'completed' && topic.discussion?.status !== 'active' && topic.discussion?.status !== 'offered' && Boolean(request.suggestedQuestions?.length) &&
- {request.suggestedQuestions?.map(question => )} + {['failed', 'cancelled', 'interrupted'].includes(request.status) && } + : request.response ? :
{['running', 'preparing'].includes(request.status) ? {label}正在想… : ['cancelled', 'interrupted'].includes(request.status) ? '这次先停在这里。想继续时可以再说说。' : ''}
} + {request.intent !== 'suggestions' && request.status === 'completed' && Boolean(request.suggestedQuestions?.length) &&
+ {request.suggestedQuestions?.map((question, index) => )}
} - {request.discussionError &&

{request.discussionError}

} {request.unparsedResponse &&
查看收到的原始内容
{request.unparsedResponse}
@@ -602,13 +555,13 @@ export function TeacherChatPanel({
} {references.length > 0 &&

{references.map((ref) => ref.text).join('\n')}

}
- {activeFocus && setFocus(null)} />}