diff --git a/docs/README.md b/docs/README.md index cc14c4725..f4d34acf9 100644 --- a/docs/README.md +++ b/docs/README.md @@ -9,6 +9,7 @@ This directory is the entry point for the repository's shared documentation. The | [Development guidance](../AGENTS.md) | Product direction and enduring principles | Architecture principles or product boundaries change | | [Contributing](../CONTRIBUTING.md) | Label taxonomy, how to pick labels, and what a PR must satisfy | Process, labels, or submission requirements change | | [Unified project architecture](architecture/unified-project.md) | Business objects, shared entry points, and interaction rules | Object responsibilities, commands, persistence, or interaction rules change | +| [Workspace modes RFC (中文)](architecture/workspace-modes-rfc.zh-CN.md) | Proposed Agent-focused mode and a 1:2 chat/canvas layout, UI sketches, switching behavior, and acceptance | The mode design or its acceptance criteria change | | [Test contracts](architecture/test-contracts.md) | Test/production boundaries, how to avoid stale assertions and environment-dependent red runs | Test infrastructure, capability gates, or contract ownership change | | [Capability mapping contract](architecture/capability-contract.md) | Evidenced upstream vocabulary, the `(operation, profile) → capability` table, the untranslatable declarations, and how it feeds discovery | Upstream vocabulary, capability ids, or discovery matching change | | [Roadmap](development/roadmap.md) | Capability dependencies and delivery criteria | Scope, dependencies, or acceptance criteria change | diff --git a/docs/architecture/assets/workspace-modes/agent-mode.png b/docs/architecture/assets/workspace-modes/agent-mode.png new file mode 100644 index 000000000..e7083778a Binary files /dev/null and b/docs/architecture/assets/workspace-modes/agent-mode.png differ diff --git a/docs/architecture/assets/workspace-modes/canvas-mode.png b/docs/architecture/assets/workspace-modes/canvas-mode.png new file mode 100644 index 000000000..97e05b086 Binary files /dev/null and b/docs/architecture/assets/workspace-modes/canvas-mode.png differ diff --git a/docs/architecture/workspace-modes-rfc.zh-CN.md b/docs/architecture/workspace-modes-rfc.zh-CN.md new file mode 100644 index 000000000..c8aaf7cd4 --- /dev/null +++ b/docs/architecture/workspace-modes-rfc.zh-CN.md @@ -0,0 +1,244 @@ +# RFC:纯净模式与左聊右画的画布模式 + +关联 [#373](https://github.com/ThinkFlowLab/OpenDirector/issues/373) 第 5 项。提出人:@congw729。日期:2026-11-17。 + +这次想给 OpenDirector 加两种工作方式:**Agent 主导的纯净模式**,以及**左侧聊天、右侧画布的画布模式**。先把界面和交互讨论清楚,再做实现。 + +有时候用户只想描述目标,让 Agent 帮忙生成内容,再看结果、提修改意见。有时候又需要自己整理节点、调整引用和编辑时间轴。两种工作方式都应该方便,切换后也应该能接着做刚才的事。 + +## 1. 想改成什么 + +这次改动主要有三点: + +- **纯净模式**:以对话为主,主界面不展示画布。沿用当前聊天组件,给对话和结果预览更多空间。 +- **画布模式**:保留现在的画布功能。聊天框移到左边,右侧画布占工作区的 **2/3**,聊天占 **1/3**。 +- **随时切换**:两种模式打开同一项目、同一会话。切换时保留草稿、引用、运行中的任务和编辑状态。 + +界面继续沿用现有的深色背景、圆角面板、图标和节点样式。画布里的工具栏仍然竖着放,缩放、撤销等控件继续放在画布左下角。浅色主题也要支持,草图先用深色说明布局。 + +Issue 里暂时叫“简单模式 / 专业模式”。这里建议改成“纯净模式 / 画布模式”,名称直接对应用户看到的工作方式。 + +## 2. UI 草图 + +下面两张图用于说明布局,里面的项目、消息和视频都是示例。功能尚未实现。 + +### 纯净模式 + +![纯净模式:居中对话,结果内嵌,语言切换放入设置](assets/workspace-modes/agent-mode.png) + +对话区居中,桌面宽窗口下先按约 800px 的阅读宽度设计,窗口变窄时随之收缩。主界面保留项目选择、保存状态、设置和模式切换。会话历史、新建会话继续从聊天顶部进入,第一版不增加常驻侧栏。 + +输入框继续保留上传、`@` 引用、Agent、技能和模型选择。这里的操作和当前聊天框一致,用户之前配置的 Agent、模型和会话也继续使用。 + +生成的图片、视频等结果放在回复下面,能直接预览。执行详情默认收起,需要时展开。不要把每一次工具调用都铺满对话,也不要只留一句“已完成”,让用户自己去找文件。 + +**纯净模式顶栏不常驻“中文 / EN”。** 语言切换放在现有的“设置 → 通用”中。默认读取已保存的语言;没有设置时,中文浏览器使用中文,其余使用英文。界面语言不会改写用户内容,Agent 仍跟随用户提问的语言回复。 + +### 画布模式 + +![画布模式:左侧聊天占三分之一,右侧画布占三分之二](assets/workspace-modes/canvas-mode.png) + +顶栏下面按 **1:2** 分成聊天和画布两个区域。聊天面板保留当前的圆角和边距,画布在右侧独立占据空间。画布工具栏、节点操作、预览和底部控件都相对右侧区域定位。 + +现有的节点、连线、图片和视频编辑、资产库、历史版本、时间轴及导出入口继续保留。时间轴仍从节点进入编辑器,这次不另外增加一条常驻底部时间轴。 + +在聊天中点“在画布中查看”,切换后定位到同一个对象。用户在画布上选中内容后,也能继续引用到聊天里,让 Agent 接着处理。 + +## 3. 打开项目后怎么用 + +### 第一次进入 + +建议第一次进入时给用户两个清楚的入口:“纯净模式”和“画布模式”,各配一句说明和小预览。用户选过以后,下次恢复这个浏览器的选择。已有用户升级后也能直接选择画布模式,不根据模型配置数量推断他应该用哪种模式。 + +模式选择与模型配置引导分开处理。不要同时弹出两个窗口,也不要要求先把所有模型配齐才能进入项目。先选工作方式,再按用户要做的事提示所需配置。 + +项目打开顺序沿用现有逻辑:深链接指定的项目优先,其次是最近项目;没有项目时走原有的默认项目创建流程。选择模式不再额外新建、复制或重新打开项目。项目打开失败时,先显示错误和重试入口。 + +```mermaid +flowchart LR + A[打开项目] --> B{保存过模式偏好?} + B -->|有| C[恢复上次模式] + B -->|没有| D[选择纯净模式或画布模式] + D --> C + C --> E[继续当前会话与任务] + E --> F[按创作需要配置模型] +``` + +首次选择的具体样式还可以讨论,工作区的两个模式按钮始终可见。 + +### 在纯净模式里查看和修改结果 + +用户可以直接描述目标,也可以上传素材、用 `@` 引用项目里已有的内容。引用选择器不能只列出当前可见的画布节点;没有展示在画布上的资产也应能通过已有的项目查询找到。 + +建议在项目菜单里提供“项目素材”入口。用户可以从这里上传素材、新建时间轴,选入素材后打开共用编辑器。这样即使没有配置模型,也能直接开始本地剪辑,无需先给 Agent 发一条消息。 + +建议结果卡片先支持这些动作: + +| 用户要做的事 | 交互 | +| --- | --- | +| 看清图片或视频 | 点击打开现有大预览,支持播放和下载 | +| 让 Agent 修改 | 把这个结果作为引用继续对话 | +| 手工调整内容 | 打开现有图片、视频或时间轴编辑器,关闭后回到对话 | +| 整理节点或引用关系 | “在画布中查看”,切换模式并定位对象 | +| 检查生成候选 | 标明候选和当前采用结果;预览不会自动采纳 | +| 找到以前的成果 | 从会话历史和项目素材入口查找,不依赖当前消息是否仍在屏幕上 | + +这里建议直接复用现有编辑器。点击卡片先看预览,需要修改时再打开编辑器;整理节点和引用关系时进入画布。 + +结果卡片要能追溯到项目中的资产、结果版本或编辑文档。缩略图地址只负责显示,后续引用、编辑和采纳通过对象 ID 与版本处理。如果对象已经删除或当前版本有变化,显示实际状态,让用户选择下一步。 + +同一素材在多条消息中出现时,继续引用同一份资产。用户在画布中改了内容,回到纯净模式也能查到新的项目状态;历史生成记录仍保留当时的输入和结果。 + +### 从对话做到视频 + +可以从“帮我用这些素材做一段短片”开始,让 Agent 准备内容、生成候选、安排时间轴。用户在对话里查看结果,必要时打开编辑器或画布修改,再输出视频。 + +目前 Agent 已有可编辑时间轴包的导出能力。**MP4 输出仍由用户在浏览器中触发**,包括时间轴编辑器内导出,以及最新增加的画布直接输出入口。纯净模式应从对应的时间轴结果提供同样的操作,并显示渲染、发布和下载状态;有多个时间轴时让用户选定目标。 + +让 Agent 直接发起 MP4 渲染,还需要补客户端执行请求和回执。这次先复用浏览器已有的输出能力,后台渲染另提需求。 + +## 4. 模式切换时保留什么 + +用户切换模式,是为了换个角度继续工作。因此这次切换不应产生业务命令,不更换 Agent 或模型,也不重启项目、会话和生成任务。 + +| 切换时正在做什么 | 预期行为 | +| --- | --- | +| 正在输入消息,已经添加素材引用 | 保留文本、引用、上传和当前会话;输入区仍可见时保留光标 | +| Agent 正在回复或调用工具 | 继续同一轮执行,另一种模式接着显示结果 | +| 图片或视频正在生成 | 保留原任务,不重新提交;结果仍登记到原对象 | +| 节点参数或媒体编辑器有未保存内容 | 保留草稿、版本基线和编辑器;不强制先保存再切换 | +| 时间轴正在播放、渲染或发布 | 保留播放头、渲染任务和待发布结果 | +| 刚遇到保存失败、锁或版本冲突 | 保留错误与重试入口,切换不自动覆盖或重试 | +| 正在拖拽节点或连线 | 按原交互规则结束临时手势,再切换布局 | +| 画布隐藏后重新显示 | 恢复原来的视口和选中对象;完成容器测量后继续操作 | + +普通模式切换恢复原视口。“在画布中查看”则是用户明确要求定位,可以调整视口和选择。对象没有对应的可见画布项时,使用已有的显示操作;需要创建展示项的,明确由该操作完成,不混进模式切换。 + +模式偏好存在浏览器里,建议新增 `opendirector.workspaceMode.v1`,值为 `agent` 或 `canvas`。项目文件、资产和导出包都不带这个偏好。无法写入本地存储时,本页面照常切换,只提示偏好未保存。 + +其他标签页修改偏好时,当前页面不突然换布局;重新打开页面时再读取最新选择。模式变化也不进入业务撤销历史。 + +## 5. 画布移到右侧,需要一起处理的交互 + +现在聊天面板浮在画布右侧。改为左聊右画后,画布的可用宽度和左边界都会变化,不能只把聊天面板的 `right` 改成 `left`。 + +需要一起检查: + +- 节点拖动、框选、连线、素材拖入和双击创建,都按右侧画布的实际坐标计算。 +- 节点菜单、参数面板、选择工具条和大预览不能被聊天区遮住。 +- 聊天里的模型、技能、引用和执行详情面板要适应左侧位置,避免展开到窗口外。 +- 画布的复制、删除和输出快捷键只在正确区域生效;在聊天输入框里操作不能误触画布。 +- 画布缩放、适应视图和最小地图使用新的容器尺寸,切换模式不自动重排节点或保存一份错误视口。 +- 纯净模式里的对话不能被“折叠聊天”关成空白页;现有双 Shift 快捷键在这个模式里建议改为聚焦输入框。 + +**1:2 先作为默认比例。** 建议继续允许拖动分隔线调整宽度,并提供恢复默认比例的操作。画布模式折叠聊天后,画布可占满工作区,再展开时恢复分栏。窄窗口下建议切换“对话 / 画布”区域,保留项目和模式入口;具体断点在实际页面里验证,不在草图阶段定死。 + +## 6. 模型配置、任务与结果的规则 + +两种模式使用相同的模型配置、参数、引用校验和业务命令。纯净模式可以把细节收起来,不能因此换掉用户已经选定的值,或减少 Agent 可用的业务能力。 + +模型提示按当前任务来: + +- 用户只是导入素材、编辑时间轴,不要求配置生成模型。 +- 想生成图片时,提示所需的图片能力;生成视频或配音时再检查对应能力。 +- 使用应用内模型的 Agent,需要配置相应语言模型;外部 runtime 使用自己的模型来源时,按该 runtime 的就绪状态处理。 +- 模型存在、服务能连接、模型支持输入、当前请求通过校验,是几个不同的检查。没有连接测试结果时,只显示“已配置”。 + +这部分与 #373 第 1 项共用模型说明和配置入口,沿用现有表单,避免两边写出不同的用途解释。配置完成后回到原任务,保留用户输入,由用户继续提交。 + +任务是否提交、文件是否生成、哪个候选已采用,都按实际回执显示。用户通过预览和反馈判断效果,聊天回复结束后也要能继续检查和修改。用户在生成过程中改了内容或锁了对象,后到的结果也要遵守现有采纳规则。 + +“停止”沿用当前行为:停止 Agent 会结束当前这轮回复和后续编排;已提交的生成任务通过各自的取消操作处理。切换模式或隐藏面板都不调用这些操作。 + +只关闭或刷新页面、后端仍运行时,活动 Agent turn 可以重新连接。退出后端后的恢复取决于各类任务自身的持久化能力,不能承诺原 Agent turn 自动续跑。MP4 的浏览器渲染则需要页面继续打开,渲染完成、发布到项目和下载文件分别反馈。 + +## 7. 实现从哪里改 + +当前代码已经有一个共享的 `Workspace`,项目、编辑器和生成相关状态由它持有。建议继续保留这一层,在里面增加模式状态和布局分支。 + +```mermaid +flowchart TB + W[共享 Workspace] --> P[模式与布局状态] + W --> A[同一个 AssistantPanel] + W --> E[编辑器与时间轴输出] + P --> S[纯净模式的对话布局] + P --> C[画布模式的左右分栏] + A --> B[现有项目业务服务] + E --> B + C --> B + B --> D[项目、资产、引用、任务与结果] +``` + +### 保持组件和数据的归属 + +`AssistantPanel`、编辑器和时间轴输出 hook 要留在稳定的父组件下。模式切换只改变布局和可见性,不因为换了一棵 React 子树而重新挂载这些组件。画布也保留原有状态,隐藏时暂停无必要的绘制和缩略图播放,恢复时重新测量尺寸。 + +尤其不能直接复用 `installDocument`。它用于安装另一个项目文档,会清理选择、编辑器、本地历史等状态。模式切换只需要更新本地偏好和布局。 + +纯净模式从现有消息引用、项目快照和资源索引读取结果。缺少的信息通过只读查询补齐。两种模式共用项目数据和任务订阅,断线恢复后重新读取服务端状态。 + +### 主要代码入口 + +以下现状基于 `729b49bc` 核查。表中的新增文件名是实现建议。 + +| 位置 | 改动重点 | +| --- | --- | +| [CanvasWorkspace.tsx](../../src/canvas/CanvasWorkspace.tsx)、[WorkspaceView.tsx](../../src/canvas/workspace/WorkspaceView.tsx) | 保留共享生命周期,加入纯净布局与画布分栏;共用编辑器宿主 | +| [state.ts](../../src/canvas/workspace/state.ts)、[runtimeTypes.ts](../../src/canvas/workspace/runtimeTypes.ts)、[useWorkspaceChrome.ts](../../src/canvas/workspace/useWorkspaceChrome.ts) | 模式状态、首次选择与欢迎层顺序、焦点和快捷键 | +| 新增 `workspaceMode.ts` | 处理 `agent` / `canvas`、偏好读写及默认选择,不包含项目业务规则 | +| [AssistantPanel.tsx](../../src/canvas/AssistantPanel.tsx)、[useAssistantLayout.ts](../../src/canvas/useAssistantLayout.ts) | 复用会话和输入组件,调整左右分栏、居中阅读宽度及辅助面板位置 | +| [TimelineEditor.tsx](../../src/canvas/TimelineEditor.tsx)、[TimelineOutput.tsx](../../src/canvas/TimelineOutput.tsx) | 保留编辑草稿、播放头、渲染与待发布结果;为纯净模式复用输出操作 | +| [SettingsDialog.tsx](../../src/canvas/SettingsDialog.tsx)、[locale.ts](../../src/i18n/locale.ts) | 复用语言设置;只移除纯净模式顶栏的快捷切换 | +| [双语词条目录](../../src/i18n/messages) | 新增模式名称、选择说明和状态提示,中英文一起交付 | + +当前 `TimelineOutput` 已支持从画布直接生成并发布 MP4,但 hook 卸载时会取消活动渲染、清空待发布结果。拆布局时要把它和 `TimelineEditor` 一起保住。 + +本次先抽取布局真正需要的边界,不顺带重构整个工作区。预期不需要改项目 schema,也不需要在业务命令里增加模式参数。 + +## 8. 怎么拆实现、怎么验收 + +建议分三步做: + +1. **先完成模式和布局。** 加偏好、选择入口、左聊右画分栏;让同一个聊天组件能在两种布局里使用。先验证切换不会丢状态。 +2. **再完成纯净模式的使用路径。** 接入结果预览、项目素材、引用、共用编辑器和时间轴输出。空项目、已有项目和无需模型的本地素材编辑都要能用。 +3. **最后检查完整交互。** 包括两种语言、窄窗口、快捷键、任务中切换和失败重试,再结合新手使用反馈调整细节。 + +模式和布局可以先做一个 PR。向用户开放纯净模式前,先把查看结果、继续修改和输出视频走通。 + +验收优先看这些场景: + +| 场景 | 要检查的结果 | +| --- | --- | +| 初次打开、已有偏好、直接打开项目链接 | 打开正确项目,不因选择模式多创建项目;再次打开恢复选择 | +| 草稿、引用上传、Agent 回复中来回切换 | 同一会话和 turn,文本与引用不丢,上传继续,不重复提交 | +| 生成中切换,随后结果到达 | 候选归属正确,不覆盖用户后来改过或锁定的内容 | +| 编辑器有草稿、保存失败或版本冲突时切换 | 草稿、版本基线和错误保留,后续仍能正确保存或重试 | +| 时间轴播放、浏览器渲染、待发布阶段切换 | 播放头、输出任务和产物保留;发布冲突按原规则处理 | +| 隐藏画布再打开、拖动分栏、在聊天中输入快捷键 | 节点位置与视口正确,菜单不遮挡,聊天操作不误触画布 | +| 从纯净模式定位结果,再回到对话 | 使用同一资产和版本,不复制项目;已有编辑继续可见 | +| 无模型、只有语言模型、外部 Agent runtime | 只拦截确实缺少能力的操作;导入和本地编辑可继续使用 | +| 做完一段视频,保存并重新打开项目 | 时间轴与素材可继续编辑,下载的 MP4 实际可播放 | +| 切换中英文、键盘导航和窄窗口 | 文案可读,输入和焦点保留,主要操作都能找到 | + +切换前后还要比对项目 ID、对象版本和撤销历史,确认切换本身没有产生业务命令。后台任务在这期间完成的,单独记录它提交的变化。 + +可扩展现有的 `assistant_sessions`、`assistant_run_navigation`、`timeline_workflow`、`structured_editors` 和 MP4 输出浏览器用例。新增用例加入统一 manifest,具体命令按[验证指南](../development/validation.md)执行。模型替身用于重复测试失败和交错场景;真实服务验证另行记录,不把它们混在一起。 + +模式切换沿用[界面性能预算](../development/ui-performance.md):首个反馈 P90 不超过 100ms,可操作中位数不超过 500ms、P90 不超过 1s。空项目、媒体较多的项目、第一次打开与已经加载过的切换分别测量。减少工具栏数量或工具调用次数,本身不能说明视频做得更快;需要观察用户是否更容易完成同一个任务。 + +本 RFC 只提交设计与草图,没有实现或运行上述功能验收。 + +## 9. 这轮还需要讨论的细节 + +两种模式、画布的 1:2 布局、沿用现有风格,以及纯净模式把语言切换收进设置,是本提案的基础。下面几项先给出建议,方便评审时直接讨论取舍: + +| 问题 | 当前建议 | +| --- | --- | +| 首次进入是否先选模式? | 先展示两种入口,选过后记住;已有用户也能直接选画布 | +| 结果需要手工编辑时怎么打开? | 先打开现有编辑器,关闭后回到对话;整理节点时再进入画布 | +| 1:2 是否固定? | 作为默认比例,允许拖动并能恢复默认 | +| 窄窗口如何使用画布模式? | 在对话和画布之间切换可见区域,保留同一工作区 | + +#373 的模型说明、教程、样例和人物替换教学继续由各自任务推进。本方案提供相应入口,接入已经可用的内容;不把尚未完成的视频或样例放成空链接。 + +这轮先讨论上面几项,确认后再拆实现任务。实现时如果还缺服务或 Agent 入口,就把具体操作和验收要求单独列出来。 diff --git a/docs/development/i18n.md b/docs/development/i18n.md index e70dc2355..7b0cdf7d3 100644 --- a/docs/development/i18n.md +++ b/docs/development/i18n.md @@ -1,11 +1,11 @@ # 中英双语界面 -界面语言在 `zh-CN` 与 `en-US` 之间切换。中文是默认语言,也是词条键的基准;英文必须覆盖同一组键。 +界面语言在 `zh-CN` 与 `en-US` 之间切换。中文是词条键的基准;英文必须覆盖同一组键。 ## 语言选择 -- 用户可以在画布左上角的「中文 | EN」开关直接切换,也可以使用「设置 → 界面语言」。 -- 用户在「设置 → 界面语言」保存的选择优先。 +- 两种创作模式都可以在「设置 → 通用 → 语言」切换语言。纯净模式的设置入口在顶栏;画布模式在左下角,宽窗口下也保留左上角的「中文 | EN」开关。 +- 用户保存的语言选择优先,切换创作模式不会改变语言。 - 没有保存值时,浏览器语言以 `zh` 开头使用 `zh-CN`,以 `en` 开头使用 `en-US`,其他语言使用英文 `en-US`。 - 选择保存在 `localStorage` 的 `opendirector.locale.v1`,并同步到 `` 与 `data-locale`。 - 界面语言不影响 Agent 回复语言。Agent 始终跟随用户最新提问的语言,不参考当前界面语言。 @@ -96,6 +96,7 @@ npm run check:i18n | 模型与服务 | [models.ts](../../src/i18n/messages/zh-CN/models.ts) | [models.ts](../../src/i18n/messages/en-US/models.ts) | | 画布外壳 | [canvasShell.ts](../../src/i18n/messages/zh-CN/canvasShell.ts) | [canvasShell.ts](../../src/i18n/messages/en-US/canvasShell.ts) | | 画布工作区 | [canvasWorkspace.ts](../../src/i18n/messages/zh-CN/canvasWorkspace.ts) | [canvasWorkspace.ts](../../src/i18n/messages/en-US/canvasWorkspace.ts) | +| 创作模式 | [workspaceModes.ts](../../src/i18n/messages/zh-CN/workspaceModes.ts) | [workspaceModes.ts](../../src/i18n/messages/en-US/workspaceModes.ts) | | 生成器 | [generation.ts](../../src/i18n/messages/zh-CN/generation.ts) | [generation.ts](../../src/i18n/messages/en-US/generation.ts) | | 音色库 | [voiceLibrary.ts](../../src/i18n/messages/zh-CN/voiceLibrary.ts) | [voiceLibrary.ts](../../src/i18n/messages/en-US/voiceLibrary.ts) | | 时间线 | [timeline.ts](../../src/i18n/messages/zh-CN/timeline.ts) | [timeline.ts](../../src/i18n/messages/en-US/timeline.ts) | diff --git a/docs/development/lessons.md b/docs/development/lessons.md index b764df61d..bc646135e 100644 --- a/docs/development/lessons.md +++ b/docs/development/lessons.md @@ -30,6 +30,12 @@ When work exposes a consequential risk or a recurring problem, add a lesson or u - **Color profile follow-up:** [PR #287's P3 review](https://github.com/ThinkFlowLab/OpenDirector/pull/287#pullrequestreview-5870014881) found that a correct full-size PNG did not prove correct thumbnails: the library JPEG and the asset/history WebP used separate encoders that both omitted ICC. Check every displayed rendition and the actual embedded profile, not only dimensions or successful decoding. Preserve RGB profiles through resizing, but do not attach a non-RGB source profile after converting its channels to RGB. / **颜色配置补充:** 同一素材的全尺寸 PNG 正确,并不能证明素材库 JPEG 和资产/版本历史 WebP 正确。逐条检查显示路径的实际 ICC;缩放保留 RGB 配置,转换通道后不能继续附带非 RGB 源配置。 +## Asynchronous readiness must preserve newer focus + +- **Trigger:** Opening chat by default or retaining it while changing projects, when its data finishes loading after the user has focused another control. +- **Evidence:** [PR #380](https://github.com/ThinkFlowLab/OpenDirector/pull/380) reproduced delayed assistant reads closing the project picker and ending new-project renaming. Project creation and opening succeeded; the chat's readiness effect moved focus and triggered the other controls' dismissal handlers. +- **Check:** Hold the assistant read until the project picker or name editor is active, then release it and verify that both the control and its focus remain. Preserve focus requested by explicitly opening chat or changing conversations, while respecting a later user focus change. Distinguish a controlled reproduction from proof of the original CI event order. + ## Baseline failures still need tracking - **Trigger:** A relevant test fails and comparison shows that it also fails on the baseline. diff --git a/docs/guides/user-guide.md b/docs/guides/user-guide.md index a5847eb70..065af8c8f 100644 --- a/docs/guides/user-guide.md +++ b/docs/guides/user-guide.md @@ -2,11 +2,23 @@ [English](user-guide.md) · [简体中文](user-guide.zh-CN.md) · [Back to README](../../README.md) +## Choose a creation mode + +Use **Agent mode** and **Canvas mode** in the top bar to switch layouts. OpenDirector starts in Canvas mode when no preference is saved, and remembers your choice in this browser. + +- **Agent mode** centers the chat so you can describe a goal and work with the assistant. Press `Shift` twice to focus the input. To inspect or edit nodes and the timeline, switch to Canvas mode. +- **Canvas mode** places chat on the left and the canvas on the right, with a default 1:2 split. Drag the divider to resize the chat, or double-click it to restore the default width. Use **Collapse AI chat** to hide the chat and **Chat with AI** to bring it back. +- In a narrow window, Canvas mode shows one area at a time. Use **Show canvas** and **Chat with AI** to switch between them. + +Both modes share the same project, conversation, draft and attachments. Switching modes preserves the canvas view and keeps replies and generation tasks running without submitting them again. + +Change the interface language in **Settings → General → Language**. Agent mode has a settings button in the top bar; Canvas mode has one at the bottom left. The assistant's reply language follows your question. + ## Connect models For external agent harnesses (Codex, OpenCode, Claude Code, DeepSeek Harness and JiuwenSwarm), OpenDirector checks the backend's `PATH` first, then common CLI installation directories. These include user bins, npm/nvm, Bun, Volta, Windows Scoop and Python Scripts, and macOS Homebrew and user Python bins. It also checks Codex desktop installs, OpenCode's `.opencode/bin`, Claude's `.claude/local`, and configured npm, pipx and uv bin directories. It checks known directories without scanning the whole disk. Finding a CLI does not establish authentication or configuration readiness. For other locations, add the CLI directory to the backend's `PATH` and restart the backend. The Default agent continues to use the bundled OpenCode. -Click the gear at the bottom left to open **Settings**, then open **Model connections**. Add models for chat, image, video, or audio generation as needed. +Open **Settings → Model connections**. Add models for chat, image, video, or audio generation as needed. For optional Kling 4.0 and Flash video generation, follow [Generate videos with Kling](kling-video.md). Both support text, first-frame, and image-reference generation. Standard Kling 4.0 also supports first-and-last frames. Adding the service preserves your existing model selection. @@ -137,13 +149,13 @@ Codex web search citations appear as numbered source links when the search resul Click a source number to view its full URL in a bordered information card, then choose **Copy link** or **Open link**. Close the card with the button at its top right, by pressing `Esc`, or by clicking outside it. -Click **Chat with AI** at the bottom right, select a chat model, and describe what you want to create. You can upload, drag and drop, or paste assets, or type `@` to reference canvas content. The assistant can save text as requested and call configured generation models to create assets. +Use the chat in either creation mode, select a chat model, and describe what you want to create. If the chat is collapsed in Canvas mode, click **Chat with AI** to reopen it. You can upload, drag and drop, or paste assets, or type `@` to reference canvas content. The assistant can save text as requested and call configured generation models to create assets. Use **New conversation** at the top of the panel to start a separate chat. **Conversation history** lets you search and switch between this canvas's conversations. Switching saves your draft and restores the selected conversation's messages, reference attachments, and model choices; the active conversation also survives a page reload. Wait for uploads or replies to finish, or stop the reply, before switching. - Press `Enter` to send, or `Shift+Enter` for a new line. - You can stop the response while it is being generated. -- Click the collapse button at the top right to hide the chat panel. Drag its left edge to resize it. +- In Canvas mode, click **Collapse AI chat** to hide the chat panel. Drag its right edge to resize it, or double-click the edge to restore the default width. Asset generation may continue after the reply ends. Check its progress in **Generation history**. diff --git a/docs/guides/user-guide.zh-CN.md b/docs/guides/user-guide.zh-CN.md index 2f795b38a..3906919af 100644 --- a/docs/guides/user-guide.zh-CN.md +++ b/docs/guides/user-guide.zh-CN.md @@ -2,11 +2,23 @@ [English](user-guide.md) · [简体中文](user-guide.zh-CN.md) · [返回首页](../../README.zh-CN.md) +## 选择创作模式 + +在顶栏点击“纯净模式”或“画布模式”切换布局。没有保存过选择时,默认进入画布模式;之后会记住你在当前浏览器中的选择。 + +- **纯净模式**:对话居中,直接向助手描述创作目标。连按两次 `Shift` 可聚焦输入框。需要查看或编辑节点、时间线时,切到画布模式。 +- **画布模式**:左边聊天,右边画布,默认按 1:2 分栏。拖动分隔线调整对话宽度,双击恢复默认宽度。点击“收起 AI 对话”隐藏对话框,点击“与 AI 对话”恢复。 +- 窗口较窄时,画布模式一次显示一个区域,通过“查看画布”和“与 AI 对话”切换。 + +两种模式共用当前项目、会话、输入草稿和附件。切换会保留画布视角,正在进行的回复和生成任务会继续,不会重新提交。 + +界面语言在“设置 → 通用 → 语言”中切换。纯净模式的设置入口在顶栏,画布模式在左下角。助手的回复语言跟随你的提问。 + ## 接入模型 选择外部 Agent harness(Codex、OpenCode、Claude Code、DeepSeek Harness 和 JiuwenSwarm)时,OpenDirector 优先检查后端的 `PATH`,再查找常见 CLI 安装目录,包括用户命令目录、npm/nvm、Bun、Volta、Windows Scoop 与 Python Scripts,以及 macOS Homebrew 和用户 Python 命令目录。同时检查 Codex 桌面版、OpenCode 的 `.opencode/bin`、Claude 的 `.claude/local`,以及配置的 npm、pipx 和 uv 命令目录。检测仅访问已知目录,不扫描整个磁盘;找到 CLI 后仍需检查认证与配置是否就绪。其他安装位置可加入后端的 `PATH`,然后重启后端。Default Agent 仍使用应用内置的 OpenCode。 -点击左下角齿轮进入“设置”,打开“模型接入”,按用途添加对话、图片、视频或音频模型。 +打开“设置 → 模型接入”,按用途添加对话、图片、视频或音频模型。 需要接入可灵 4.0 或 Flash 时,参阅[使用可灵生成视频](kling-video.zh-CN.md)。两个型号均支持文生、首帧和图片参考,标准版 4.0 额外支持首尾帧。添加后保留已有模型选择。 @@ -137,13 +149,13 @@ Codex 网页检索引用在具有匹配来源时显示为编号链接;复制 点击来源编号可在带边框的信息卡片中查看完整链接,再选择“复制链接”或“打开链接”。可通过卡片右上角的关闭按钮、按 `Esc` 或点击卡片外部关闭。 -点击右下角“与 AI 对话”,选择对话模型后输入创作要求。可上传、拖放、粘贴素材,或输入 `@` 引用画布中的内容。助手可按要求保存文本,并调用已配置的生成模型制作素材。 +两种创作模式都可以使用对话。选择对话模型后输入创作要求;画布模式中若对话框已收起,点击“与 AI 对话”恢复。可上传、拖放、粘贴素材,或输入 `@` 引用画布中的内容。助手可按要求保存文本,并调用已配置的生成模型制作素材。 点击对话框顶部“新建”开始独立会话;点击“历史会话”可搜索并切换当前画布的旧会话。切换前会保存草稿,切回后恢复消息、参考附件和模型选择,刷新页面仍保留当前会话。回复或参考上传进行中,请先等待完成或停止回复后再切换。 - `Enter` 发送,`Shift+Enter` 换行。 - 回复过程中可停止输出。 -- 点击右上角收起按钮隐藏对话框;拖动左边缘调整宽度。 +- 画布模式中,点击“收起 AI 对话”隐藏对话框;拖动右边缘调整宽度,双击恢复默认宽度。 素材生成可能在回复结束后继续进行,可在“生成历史”查看进度。 diff --git a/scripts/verify_creation_browser.mjs b/scripts/verify_creation_browser.mjs index 35e135b57..f9f3443b6 100644 --- a/scripts/verify_creation_browser.mjs +++ b/scripts/verify_creation_browser.mjs @@ -164,6 +164,9 @@ try { await prompt.fill("Based on @"); await prompt.press("Enter"); await prompt.locator("[data-reference]").waitFor(); + // Selecting the target can pan the source outside the visible canvas. + await page.getByRole("button", { name: "Zoom options", exact: true }).click(); + await page.getByRole("button", { name: /^Fit all content/ }).click(); await source.locator(".cw-node-title").dblclick(); await page .getByRole("textbox", { name: "Node name", exact: true }) diff --git a/src/canvas/AgentPicker.tsx b/src/canvas/AgentPicker.tsx index 78e997e28..2bc870535 100644 --- a/src/canvas/AgentPicker.tsx +++ b/src/canvas/AgentPicker.tsx @@ -281,15 +281,22 @@ export function AgentPicker({ }, [open, editing, editingModel, setup]); useLayoutEffect(() => { if ((!editing && !editingModel) || !submenu.current || !menu.current) return; - const bounds = menu.current.getBoundingClientRect(); const layer = submenu.current; - layer.style.left = `${Math.max(8, bounds.left - 310)}px`; - layer.style.bottom = `${window.innerHeight - bounds.bottom}px`; + const position = () => { + const bounds = menu.current!.getBoundingClientRect(); + const left = bounds.right + 310 <= window.innerWidth - 8 ? bounds.right + 10 : bounds.left - 310; + layer.style.left = `${Math.max(8, Math.min(left, window.innerWidth - 308))}px`; + layer.style.bottom = `${window.innerHeight - bounds.bottom}px`; + }; + position(); layer.showPopover(); - const reposition = () => { setEditing(null); setEditingModel(null); }; - window.addEventListener("resize", reposition); + const observer = new ResizeObserver(position); + const host = menu.current.closest(".cw-assistant-panel"); + if (host) observer.observe(host); + window.addEventListener("resize", position); return () => { - window.removeEventListener("resize", reposition); + observer.disconnect(); + window.removeEventListener("resize", position); }; }, [editing, editingModel]); function keys(event: KeyboardEvent) { diff --git a/src/canvas/AssistantConversations.tsx b/src/canvas/AssistantConversations.tsx index c67f720e1..25c861955 100644 --- a/src/canvas/AssistantConversations.tsx +++ b/src/canvas/AssistantConversations.tsx @@ -18,6 +18,7 @@ export function AssistantConversations({ onRefresh, onChange, onClose, + collapsible = true, }: { activeId?: string; hasMessages: boolean; @@ -30,6 +31,7 @@ export function AssistantConversations({ onRefresh: () => void; onChange: (id?: string, create?: boolean) => Promise; onClose: () => void; + collapsible?: boolean; }) { const t = useT(); const [open, setOpen] = useState(false); @@ -148,7 +150,7 @@ export function AssistantConversations({ > - - + } {open && ( diff --git a/src/canvas/AssistantPanel.tsx b/src/canvas/AssistantPanel.tsx index 1da3e8e12..38f264a5a 100644 --- a/src/canvas/AssistantPanel.tsx +++ b/src/canvas/AssistantPanel.tsx @@ -61,6 +61,7 @@ export function AssistantPanel({ visibleNodes, selectedNodes, open, + collapsible = true, layout, selectionRequest, onClose, @@ -76,6 +77,7 @@ export function AssistantPanel({ visibleNodes: CanvasNode[]; selectedNodes: CanvasNode[]; open: boolean; + collapsible?: boolean; layout: AssistantLayout; selectionRequest: number; onClose: () => void; @@ -173,8 +175,24 @@ export function AssistantPanel({ chat.setContent(content, projectReferences.assets); }, [projectReferences.assets]); + const previouslyOpen = useRef(open); + const openFocusTarget = useRef(null); useEffect(() => { - if (open && chat.loaded) editor.current?.focus(); + if (open && !previouslyOpen.current) + openFocusTarget.current = document.activeElement; + previouslyOpen.current = open; + if (!open) { + openFocusTarget.current = null; + return; + } + if (!chat.loaded) return; + const active = document.activeElement; + // Loading a default-open chat must not dismiss a project menu or rename. + // An explicit open retains focus intent until the user chooses another control. + if (active === document.body || active?.closest("#canvas-assistant") || + active === openFocusTarget.current) + editor.current?.focus(); + openFocusTarget.current = null; }, [open, chat.loaded, value.conversationId]); const displayedAssets = [ ...new Map( @@ -346,7 +364,7 @@ export function AssistantPanel({ event.preventDefault(); event.stopPropagation(); if (mediaPreview) setMediaPreview(null); - else onClose(); + else if (collapsible) onClose(); } }} onDragOver={(event) => { @@ -385,6 +403,7 @@ export function AssistantPanel({ } onChange={changeConversation} onClose={onClose} + collapsible={collapsible} />
- {!layout.mobile && ( + {collapsible && !layout.mobile && (
(null); const [size, setSize] = useState({ width: 0, height: 0 }); + const [region, setRegion] = useState({ width: window.innerWidth, height: window.innerHeight }); useLayoutEffect(() => { if (!element) return; const measure = () => { const rect = element.getBoundingClientRect(); + const host = element.closest(".cw-canvas-region")?.getBoundingClientRect(); + if (host) setRegion(current => current.width === host.width && current.height === host.height + ? current : { width: host.width, height: host.height }); setSize((current) => current.width === rect.width && current.height === rect.height ? current @@ -40,17 +44,19 @@ export function SelectionBarAnchor({ measure(); const observer = new ResizeObserver(measure); observer.observe(element); + const host = element.closest(".cw-canvas-region"); + if (host) observer.observe(host); return () => observer.disconnect(); }, [element]); const bounds = boundsOfNodes(nodes, allNodes); if (!bounds) return null; - const narrow = window.innerWidth < 700; + const narrow = region.width < 700; const left = narrow ? SAFE_LEFT_NARROW : SAFE_LEFT; const safe = { x: left, y: SAFE_TOP, - width: Math.max(1, window.innerWidth - left - SAFE_RIGHT), - height: Math.max(1, window.innerHeight - SAFE_TOP - SAFE_BOTTOM), + width: Math.max(1, region.width - left - SAFE_RIGHT), + height: Math.max(1, region.height - SAFE_TOP - SAFE_BOTTOM), }; // Node coordinates start at the media frame; the title sits above it. const envelope = { diff --git a/src/canvas/SkillPicker.tsx b/src/canvas/SkillPicker.tsx index 31361d505..fee82e30d 100644 --- a/src/canvas/SkillPicker.tsx +++ b/src/canvas/SkillPicker.tsx @@ -83,17 +83,22 @@ export function SkillPicker({ useLayoutEffect(() => { if (!open || !root.current) return; const popover = root.current.querySelector(".cw-skill-popover")!; - const bounds = root.current.closest(".cw-assistant-composer")!.getBoundingClientRect(); - const width = Math.min(420, bounds.width, window.innerWidth - 16); - popover.style.width = `${width}px`; - popover.style.left = `${Math.max(8, Math.min(bounds.left, window.innerWidth - width - 8))}px`; - popover.style.bottom = `${window.innerHeight - bounds.top + 8}px`; - popover.style.maxHeight = `${Math.max(120, bounds.top - 24)}px`; + const composer = root.current.closest(".cw-assistant-composer")!; + const position = () => { + const bounds = composer.getBoundingClientRect(); + const width = Math.min(420, bounds.width, window.innerWidth - 16); + popover.style.width = `${width}px`; + popover.style.left = `${Math.max(8, Math.min(bounds.left, window.innerWidth - width - 8))}px`; + popover.style.bottom = `${window.innerHeight - bounds.top + 8}px`; + popover.style.maxHeight = `${Math.max(120, bounds.top - 24)}px`; + }; + position(); popover.showPopover(); if (!slash) popover.querySelector("input")?.focus({preventScroll: true}); - const dismiss = () => {setManual(false); setDismissed(true);}; - window.addEventListener("resize", dismiss); - return () => window.removeEventListener("resize", dismiss); + const observer = new ResizeObserver(position); + observer.observe(composer); + window.addEventListener("resize", position); + return () => { observer.disconnect(); window.removeEventListener("resize", position); }; }, [open, slash]); function close() { diff --git a/src/canvas/useAssistantLayout.ts b/src/canvas/useAssistantLayout.ts index d7ac398d1..d672fd45e 100644 --- a/src/canvas/useAssistantLayout.ts +++ b/src/canvas/useAssistantLayout.ts @@ -9,82 +9,64 @@ import { } from "react"; const KEY = "opendirector.assistant-layout.v1"; -const DEFAULT_WIDTH = 420; const MAX_WIDTH = 720; const COLLAPSE_AT = 200; +const GUTTER = 28; -function readWidth() { +function readWidth(): number | null { try { const width = JSON.parse(localStorage.getItem(KEY) ?? "null")?.width; return typeof width === "number" && Number.isFinite(width) && width > COLLAPSE_AT - ? Math.min(width, MAX_WIDTH) - : DEFAULT_WIDTH; + ? width + : null; } catch { - return DEFAULT_WIDTH; + return null; } } -/** OpenDirector's drag, keyboard, reset and collapse behavior, with room for the canvas header. */ +/** A left conversation column; the default column occupies one third of the workspace. */ export function useAssistantLayout( root: RefObject, open: boolean, onCollapse: () => void, ) { const [preferredWidth, setPreferredWidth] = useState(readWidth); - const [bounds, setBounds] = useState({ - viewport: window.innerWidth, - heading: 260, - actions: 200, - }); + const [viewport, setViewport] = useState(window.innerWidth); const [dragging, setDragging] = useState(false); - const drag = useRef<{ x: number; width: number; restore: number } | null>( - null, - ); - const mobile = bounds.viewport <= 700; - const headerSpace = bounds.viewport - bounds.heading - bounds.actions - 72; - const belowHeader = mobile || headerSpace <= COLLAPSE_AT; + const drag = useRef<{ + x: number; + width: number; + restore: number | null; + } | null>(null); + const mobile = viewport <= 700; + const defaultWidth = Math.max(COLLAPSE_AT + 1, viewport / 3 - GUTTER); const maxWidth = Math.min( - MAX_WIDTH, - belowHeader ? bounds.viewport - (mobile ? 20 : 28) : headerSpace, + Math.max(MAX_WIDTH, defaultWidth), + Math.max(COLLAPSE_AT + 1, viewport - 360 - GUTTER), ); - const width = mobile ? maxWidth : Math.min(preferredWidth, maxWidth); - + const width = mobile + ? Math.max(1, viewport - GUTTER) + : Math.min(preferredWidth ?? defaultWidth, maxWidth); const stop = () => { drag.current = null; setDragging(false); }; useLayoutEffect(() => { - const heading = root.current?.querySelector( - ".cw-project-heading", - ); - const actions = root.current?.querySelector(".cw-top-actions"); - const measure = () => - setBounds({ - viewport: window.innerWidth, - heading: heading?.offsetWidth ?? 260, - actions: actions?.offsetWidth ?? 200, - }); - const resize = () => { - stop(); - measure(); - }; + const host = root.current; + if (!host) return; + const measure = () => setViewport(host.clientWidth); const observer = new ResizeObserver(measure); - if (heading) observer.observe(heading); - if (actions) observer.observe(actions); - window.addEventListener("resize", resize); + observer.observe(host); measure(); - return () => { - observer.disconnect(); - window.removeEventListener("resize", resize); - }; + return () => observer.disconnect(); }, [root]); useEffect(() => { try { localStorage.setItem(KEY, JSON.stringify({ width: preferredWidth })); } catch { - /* Keep usable without storage. */ + /* The active layout remains usable without storage. */ } }, [preferredWidth]); useEffect(() => { @@ -94,7 +76,6 @@ export function useAssistantLayout( useEffect(() => { if (!open) stop(); }, [open]); - const resize = (next: number, restore = preferredWidth) => { if (next <= COLLAPSE_AT) { stop(); @@ -113,7 +94,7 @@ export function useAssistantLayout( const onPointerMove = (event: PointerEvent) => { if (drag.current) resize( - drag.current.width + drag.current.x - event.clientX, + drag.current.width + event.clientX - drag.current.x, drag.current.restore, ); }; @@ -124,9 +105,9 @@ export function useAssistantLayout( : event.key === "End" ? maxWidth : event.key === "ArrowLeft" - ? width + 10 + ? width - 10 : event.key === "ArrowRight" - ? width - 10 + ? width + 10 : null; if (next !== null) { event.preventDefault(); @@ -139,8 +120,7 @@ export function useAssistantLayout( maxWidth, mobile, dragging, - belowHeader, - toolbarOffset: open && !belowHeader ? width + 6 : 0, + columnWidth: open && !mobile ? width + GUTTER : 0, resizer: { onPointerDown, onPointerMove, @@ -148,9 +128,8 @@ export function useAssistantLayout( onPointerCancel: stop, onLostPointerCapture: stop, onKeyDown, - onDoubleClick: () => setPreferredWidth(DEFAULT_WIDTH), + onDoubleClick: () => setPreferredWidth(null), }, }; } - export type AssistantLayout = ReturnType; diff --git a/src/canvas/useCanvasWheelBoundary.ts b/src/canvas/useCanvasWheelBoundary.ts index 14618236d..093e2c878 100644 --- a/src/canvas/useCanvasWheelBoundary.ts +++ b/src/canvas/useCanvasWheelBoundary.ts @@ -6,6 +6,7 @@ import { MIN_ZOOM, MAX_ZOOM } from "./model.ts"; export function useCanvasWheelBoundary( root: RefObject, onNavigate: () => void, + enabled = true, ) { const flow = useReactFlow(); const navigate = useRef(onNavigate); @@ -19,8 +20,9 @@ export function useCanvasWheelBoundary( let pointer = { x: 0, y: 0 }; const wheel = (event: WheelEvent) => { if (!(event.ctrlKey || event.metaKey)) return; - // A React wheel handler can be passive. Capture natively so browser page - // zoom is cancelled even over controls rendered through a portal. + if (!enabled || !(event.target instanceof Node) || !root.current?.contains(event.target)) return; + // A React wheel handler can be passive. Capture natively so canvas + // controls zoom the canvas without also zooming the browser page. event.preventDefault(); // Own every modified wheel, including those over inputs and portals, so // React Flow cannot apply a second zoom or pan the underlying canvas. @@ -68,5 +70,5 @@ export function useCanvasWheelBoundary( document.removeEventListener("wheel", wheel, true); if (frame) window.cancelAnimationFrame(frame); }; - }, [root, flow]); + }, [root, flow, enabled]); } diff --git a/src/canvas/usePendingCanvasReferences.ts b/src/canvas/usePendingCanvasReferences.ts index d441811fb..e1261d4c0 100644 --- a/src/canvas/usePendingCanvasReferences.ts +++ b/src/canvas/usePendingCanvasReferences.ts @@ -93,6 +93,12 @@ export function usePendingCanvasReferences( return; } if (!latest.current || event.isComposing || event.keyCode === 229) return; + // A focused menu owns its dismissal; keep the staged references intact. + if ( + event.target instanceof Element && + event.target.closest('[role="menu"]') + ) + return; event.preventDefault(); suppressed.current = true; cancel(); diff --git a/src/canvas/useSelectionPresentation.ts b/src/canvas/useSelectionPresentation.ts index f09067a62..f07aecc25 100644 --- a/src/canvas/useSelectionPresentation.ts +++ b/src/canvas/useSelectionPresentation.ts @@ -28,6 +28,7 @@ export function useSelectionPresentation({ controlsKey, preferredZoom, scaleComposer = false, + preserveViewportKey = "", }: { root: RefObject; toolbar: RefObject; @@ -42,6 +43,8 @@ export function useSelectionPresentation({ /** Dedicated media editing may enlarge its source; ordinary selection preserves zoom. */ preferredZoom?: number; scaleComposer?: boolean; + /** Layout changes update control placement while preserving the user's viewport. */ + preserveViewportKey?: string; }) { const flow = useReactFlow(); const viewport = useViewport(); @@ -127,6 +130,8 @@ export function useSelectionPresentation({ }, [root, toolbar, composer, measurementKey, scaleComposer]); const lastLayout = useRef(""); + const lastPresentation = useRef({ layout: preserveViewportKey, intent: "" }); + const preservedIntent = useRef(null); const origin = useRef<{ key: string; viewport: Viewport } | null>(null); const animation = useRef(null); const stopPresentation = useCallback(() => { @@ -150,6 +155,12 @@ export function useSelectionPresentation({ }; useLayoutEffect(() => stopPresentation, [stopPresentation]); useLayoutEffect(() => { + const intent = JSON.stringify([nodeId, focusRequest, panel]); + if (lastPresentation.current.layout !== preserveViewportKey) { + preservedIntent.current = intent; + lastPresentation.current = { layout: preserveViewportKey, intent }; + origin.current = { key: intent, viewport: flow.getViewport() }; + } if (!bounds || blocked || obscured) stopPresentation(); // A new node must not be fitted using the previous node's generator height. if (measurement.key !== measurementKey) return; @@ -164,7 +175,11 @@ export function useSelectionPresentation({ if (!bounds || blocked || obscured || chrome.safe.width === 0) return; lastLayout.current = key; const current = flow.getViewport(); - const intent = JSON.stringify([nodeId, focusRequest, panel]); + if (preservedIntent.current === intent) { + stopPresentation(); + return; + } + preservedIntent.current = null; if (origin.current?.key !== intent) origin.current = { key: intent, @@ -202,6 +217,7 @@ export function useSelectionPresentation({ measurement.key, measurementKey, preferredZoom, + preserveViewportKey, ]); const placement = bounds diff --git a/src/canvas/workspace-modes.css b/src/canvas/workspace-modes.css new file mode 100644 index 000000000..66295a907 --- /dev/null +++ b/src/canvas/workspace-modes.css @@ -0,0 +1,118 @@ +/* One project surface with stable conversation and editor instances. */ +.cw-canvas-region { + position: absolute; + inset: 0 0 0 var(--assistant-column-width, 0px); + overflow: clip; + min-width: 0; +} +.cw-canvas-region[data-visible="false"] { + opacity: 0; + visibility: hidden; + pointer-events: none; +} +.cw-workspace .cw-top-actions { + transform: none; + transition: none; +} +.cw-canvas-top-actions { + display: flex; + align-items: center; + gap: 8px; +} +.cw-canvas-top-actions[hidden], +.cw-language-toggle[hidden] { display: none; } +.cw-workspace-mode-switch { + display: flex; + flex-shrink: 0; + padding: 3px; + border: 1px solid var(--cw-border); + border-radius: 10px; + background: var(--cw-surface); +} +.cw-workspace-mode-switch button { + padding: 7px 12px; + border-radius: 7px; + color: #99999e; + white-space: nowrap; +} +.cw-workspace-mode-switch button[aria-pressed="true"] { + color: #ededee; + background: #ffffff12; +} +.cw-workspace .cw-assistant-panel { + top: 78px; + left: 14px; + right: auto; + bottom: 14px; +} +.cw-workspace[data-workspace-mode="agent"] .cw-assistant-panel { + left: max(14px, calc((100% - 800px) / 2)); + width: min(800px, calc(100% - 28px)); +} +.cw-workspace .cw-assistant-resizer { + left: auto; + right: 0; +} +.cw-workspace[data-assistant-open="true"] .cw-side-panel:not(.from-left) { + right: 20px; +} +.cw-workspace[data-workspace-mode="canvas"] .cw-creation-assets { + right: 20px; + width: min(370px, calc(100vw - var(--assistant-column-width, 0px) - 40px)); +} +.cw-canvas-region .cw-side-panel.from-left { + max-width: calc(100% - 96px); +} +.cw-narrow-region-toggle { + position: absolute; + z-index: 35; + bottom: 14px; + right: 14px; + padding: 9px 16px; + border: 1px solid var(--cw-border); + border-radius: 20px; + background: var(--cw-surface); + color: inherit; +} +:root[data-theme="light"] .cw-workspace-mode-switch button { color: #757b86; } +:root[data-theme="light"] .cw-workspace-mode-switch button[aria-pressed="true"] { + color: #30343c; + background: #e4e7ed; +} +@media (max-width: 1000px) { + .cw-workspace .cw-topbar { gap: 8px; padding-inline: 12px; } + .cw-workspace .cw-project-heading { gap: 5px; } + .cw-workspace .cw-project-title { max-width: 180px; } + .cw-workspace .cw-save-status { display: none; } + .cw-workspace .cw-top-actions { gap: 5px; } + .cw-canvas-top-actions { gap: 2px; } + .cw-workspace-mode-switch button { padding-inline: 9px; } +} +@media (max-width: 700px) { + .cw-workspace .cw-canvas-region { left: 0; } + .cw-workspace[data-workspace-mode="canvas"] .cw-assistant-panel { + width: calc(100% - 28px); + bottom: 62px; + } + .cw-workspace .cw-project-title { max-width: 110px; } + .cw-workspace .cw-brand { width: 26px; height: 26px; margin-right: 0; } + .cw-workspace .cw-language-toggle, + .cw-workspace .cw-node-info { display: none; } + .cw-workspace[data-canvas-visible="false"] .cw-canvas-top-actions { display: none; } + .cw-canvas-top-actions { + position: absolute; + top: 72px; + right: 12px; + padding: 4px; + border-radius: 9px; + background: var(--cw-surface); + } + .cw-workspace[data-workspace-mode="canvas"] .cw-creation-assets { + top: 120px; + max-height: min(760px, calc(100dvh - 182px)); + } + .cw-workspace .cw-side-panel:not(.from-left) { + top: 120px; + } + .cw-workspace-mode-switch button { font-size: 12px; padding: 7px; } +} diff --git a/src/canvas/workspace.css b/src/canvas/workspace.css index 844adee60..d1f138420 100644 --- a/src/canvas/workspace.css +++ b/src/canvas/workspace.css @@ -2017,6 +2017,7 @@ } } .cw-context-menu { + position: fixed; max-height: calc(100vh - 80px); overflow-y: auto; } diff --git a/src/canvas/workspace/WorkspaceModeSwitch.tsx b/src/canvas/workspace/WorkspaceModeSwitch.tsx new file mode 100644 index 000000000..47bf64666 --- /dev/null +++ b/src/canvas/workspace/WorkspaceModeSwitch.tsx @@ -0,0 +1,31 @@ +import { useT } from "../../i18n/I18nProvider"; +import type { WorkspaceMode } from "../workspaceMode"; + +export function WorkspaceModeSwitch({ + mode, + onChange, +}: { + mode: WorkspaceMode; + onChange: (mode: WorkspaceMode) => void; +}) { + const t = useT(); + return ( +
+ {(["agent", "canvas"] as const).map((value) => ( + + ))} +
+ ); +} diff --git a/src/canvas/workspace/WorkspaceView.tsx b/src/canvas/workspace/WorkspaceView.tsx index 73ca99a34..c388c0daa 100644 --- a/src/canvas/workspace/WorkspaceView.tsx +++ b/src/canvas/workspace/WorkspaceView.tsx @@ -1,3 +1,4 @@ +import { WorkspaceModeSwitch } from "./WorkspaceModeSwitch"; import { Background, BackgroundVariant, MiniMap, SelectionMode, ViewportPortal } from "@xyflow/react"; import "@xyflow/react/dist/style.css"; @@ -91,6 +92,7 @@ import { TooltipButton } from "../TooltipButton"; import { VideoActionEditor } from "../VideoActionEditor"; import { captureVideoEndpoint, extractVideoAudio } from "../videoActionRuntime"; import "../workspace.css"; +import "../workspace-modes.css"; import type { WorkspaceRuntime } from "./runtimeTypes.ts"; import { DirectorStage, IconButton, helpShortcuts, nodeTypes, edgeTypes, referenceSideKeys, incomingNodes } from "./workspaceChrome.tsx"; @@ -106,6 +108,11 @@ export function WorkspaceView({ rt }: { rt: WorkspaceRuntime }) { addNode, assetListAnchor, assistantLayout, + assistantVisible, + canvasRegion, + canvasVisible, + workspaceMode, + changeWorkspaceMode, assistantOpen, assistantSelectionRequest, backendCatalog, @@ -341,15 +348,17 @@ export function WorkspaceView({ rt }: { rt: WorkspaceRuntime }) { className={`cw-workspace${panning ? " cw-workspace-panning" : ""}${spacePressed ? " cw-workspace-space-panning" : ""}`} ref={root} data-testid="canvas-workspace" - data-edge-motion={pageVisible} - data-assistant-open={assistantOpen} + data-edge-motion={pageVisible && canvasVisible} + data-assistant-open={assistantVisible} + data-workspace-mode={workspaceMode} + data-narrow={assistantLayout.mobile} + data-canvas-visible={canvasVisible} data-assistant-resizing={assistantLayout.dragging} data-canvas-interacting={interacting} style={ { "--assistant-width": `${assistantLayout.width}px`, - "--assistant-toolbar-offset": `${assistantLayout.toolbarOffset}px`, - "--assistant-top": assistantLayout.belowHeader ? "78px" : "14px", + "--assistant-column-width": `${assistantLayout.columnWidth}px`, "--cw-selection-padding": `${SELECTION_PADDING / zoom}px`, } as CSSProperties } @@ -441,6 +450,8 @@ export function WorkspaceView({ rt }: { rt: WorkspaceRuntime }) { }} onDrop={(event) => { if ( + !canvasVisible || + !(event.target as HTMLElement).closest(".cw-canvas-region") || event.defaultPrevented || (event.target as HTMLElement).closest("[role=dialog]") ) @@ -455,12 +466,161 @@ export function WorkspaceView({ rt }: { rt: WorkspaceRuntime }) { } }} > +
+
+ OpenDirector + + switchDocument(() => openWorkspaceProject(project)) + } + onCreate={() => { + const id = crypto.randomUUID(); + void switchDocument(async () => { + const { projects } = await listWorkspaceProjects(); + const names = new Set(projects.map(project => project.name)); + const base = t("canvas.project.defaultName"); + let name = base; + for (let suffix = 2; names.has(name); suffix++) name = `${base} ${suffix}`; + const project = await createWorkspaceProject(name, id); + const document = await openWorkspaceProject(project); + await saveWorkspaceDocument(document); + return document; + }).then(success => { if (success) setEditProjectNameId(id); }); + }} + onRename={renameCurrentProject} + onRenameProject={renameProject} + onDeleteProject={deleteProject} + /> + + {workspaceMode === "canvas" && } + + + {saveStatusText} + + {saveStatus.state === "failed" && ( +
+ + +
+ )} +
+
+ + {workspaceMode === "agent" && { setSettingsCategory("general"); setSettingsOpen(true); }} + />} + +
+
+
)} -
-
- OpenDirector - - switchDocument(() => openWorkspaceProject(project)) - } - onCreate={() => { - const id = crypto.randomUUID(); - void switchDocument(async () => { - const { projects } = await listWorkspaceProjects(); - const names = new Set(projects.map(project => project.name)); - const base = t("canvas.project.defaultName"); - let name = base; - for (let suffix = 2; names.has(name); suffix++) name = `${base} ${suffix}`; - const project = await createWorkspaceProject(name, id); - const document = await openWorkspaceProject(project); - await saveWorkspaceDocument(document); - return document; - }).then(success => { if (success) setEditProjectNameId(id); }); - }} - onRename={renameCurrentProject} - onRenameProject={renameProject} - onDeleteProject={deleteProject} - /> -
- - - -
- - - - {saveStatusText} - - {saveStatus.state === "failed" && ( -
- - -
- )} -
-
- - { - void persist(historyRef.current.present, false).then( - (saved) => { - if (saved) togglePanel("search"); - }, - ); - }} - /> - - togglePanel("history")} - /> - importInput.current?.click()} - /> - - { - const file = event.target.files?.[0]; - event.target.value = ""; - if (file) void importDocument(file); - }} - /> -
-
+ )} - {hasToolbar && single?.type === "timeline" && !lockedIds.has(single.id) && ( + {canvasVisible && hasToolbar && single?.type === "timeline" && !lockedIds.has(single.id) && ( void timelineOutput.run(single, () => persist(doc, false))} /> @@ -2279,6 +2301,16 @@ export function WorkspaceView({ rt }: { rt: WorkspaceRuntime }) { )} +
+ {workspaceMode === "canvas" && assistantLayout.mobile && assistantOpen && ( + + )} + {ready && ( selected.includes(node.id))} - open={assistantOpen} + open={assistantVisible} + collapsible={workspaceMode === "canvas"} layout={assistantLayout} selectionRequest={assistantSelectionRequest} onClose={() => { + if (workspaceMode === "agent") return; setAssistantOpen(false); requestAnimationFrame(() => root.current diff --git a/src/canvas/workspace/runtimeTypes.ts b/src/canvas/workspace/runtimeTypes.ts index 4763f4a78..0822fb059 100644 --- a/src/canvas/workspace/runtimeTypes.ts +++ b/src/canvas/workspace/runtimeTypes.ts @@ -15,6 +15,7 @@ import type { CanvasNodeKind, } from "../model.ts"; import { SaveQueue } from "../saveQueue.ts"; +import type { WorkspaceMode } from "../workspaceMode"; import { multiSelectionGeometry } from "../selectionReferences.ts"; import type { SubjectMediaSlot } from "../subjectMedia.ts"; import type { CanvasToolId, ToolParameters } from "../toolCatalog.ts"; @@ -82,6 +83,11 @@ export interface WorkspaceRuntime extends WorkspaceState, WorkspaceSetters { >; selectionAtPointerDown: RefObject; root: RefObject; + canvasRegion: RefObject; + workspaceMode: WorkspaceMode; + changeWorkspaceMode: (mode: WorkspaceMode) => void; + assistantVisible: boolean; + canvasVisible: boolean; composerElement: RefObject; toolbarElement: RefObject; fileInput: RefObject; diff --git a/src/canvas/workspace/state.ts b/src/canvas/workspace/state.ts index 5f70bebe0..e4dd74ce6 100644 --- a/src/canvas/workspace/state.ts +++ b/src/canvas/workspace/state.ts @@ -250,7 +250,7 @@ export function createWorkspaceState(): WorkspaceState { welcomeOpen: false, activeGenerations: [], arrivingNodes: new Set(), - assistantOpen: false, + assistantOpen: true, assistantSelectionRequest: 0, presentationFocus: 0, zoom: 1, diff --git a/src/canvas/workspace/useWorkspaceChrome.ts b/src/canvas/workspace/useWorkspaceChrome.ts index e2878f53f..9e6554e79 100644 --- a/src/canvas/workspace/useWorkspaceChrome.ts +++ b/src/canvas/workspace/useWorkspaceChrome.ts @@ -25,6 +25,7 @@ export function useWorkspaceChrome(rt: WorkspaceRuntime) { setWelcomeOpen, setupChecked, switching, + workspaceMode, } = rt; useEffect(() => { @@ -46,6 +47,10 @@ export function useWorkspaceChrome(rt: WorkspaceRuntime) { const toggleAssistantShortcut = useRef(() => {}); toggleAssistantShortcut.current = () => { if (!ready || switching || document.querySelector("dialog[open], .cw-modal-backdrop")) return; + if (workspaceMode === "agent") { + root.current?.querySelector(".cw-assistant-composer [contenteditable=true]")?.focus({ preventScroll: true }); + return; + } if (assistantOpen) { setAssistantOpen(false); const target = assistantReturnFocus.current; diff --git a/src/canvas/workspace/useWorkspaceDocument.ts b/src/canvas/workspace/useWorkspaceDocument.ts index 61f5b78fe..7b8a2bddf 100644 --- a/src/canvas/workspace/useWorkspaceDocument.ts +++ b/src/canvas/workspace/useWorkspaceDocument.ts @@ -46,7 +46,6 @@ export function useWorkspaceDocument(rt: WorkspaceRuntime) { saveQueue, setActionSession, setArrivingNodes, - setAssistantOpen, setAssistantSelectionRequest, setBusinessHistory, setComposerNodeId, @@ -285,7 +284,6 @@ export function useWorkspaceDocument(rt: WorkspaceRuntime) { flowSurface.current?.cancel(); setSelectedEdges([]); setPanel(null); - setAssistantOpen(false); setAssistantSelectionRequest(0); setMenu(null); setRename(null); diff --git a/src/canvas/workspace/useWorkspaceInteraction.tsx b/src/canvas/workspace/useWorkspaceInteraction.tsx index 01662b618..54b0ec93e 100644 --- a/src/canvas/workspace/useWorkspaceInteraction.tsx +++ b/src/canvas/workspace/useWorkspaceInteraction.tsx @@ -37,6 +37,9 @@ export function useWorkspaceInteraction(rt: WorkspaceRuntime) { arrivingNodes, assistantLayout, assistantOpen, + canvasRegion, + canvasVisible, + workspaceMode, begin, bypassSnapping, cancelResultImport, @@ -600,7 +603,7 @@ export function useWorkspaceInteraction(rt: WorkspaceRuntime) { } : mediaBounds; const selectionPresentation = useSelectionPresentation({ - root, + root: canvasRegion, toolbar: toolbarElement, composer: composerElement, // Generators are screen-space UI, like the toolbar and sidebar. Only their @@ -613,6 +616,7 @@ export function useWorkspaceInteraction(rt: WorkspaceRuntime) { panel, focusRequest: presentationFocus, blocked: + !canvasVisible || composerExpanded || interacting || references.connecting || @@ -620,9 +624,10 @@ export function useWorkspaceInteraction(rt: WorkspaceRuntime) { pointerGesture.current.moved || !selectionControls, controlsKey: `${assistantOpen}:${assistantLayout.width}:${hasComposer}:${hasToolbar}:${composerExpanded}:${activeAction?.token ?? ""}:${activeAction?.tool === "outpaint" ? JSON.stringify(presentationBounds) : ""}`, + preserveViewportKey: `${workspaceMode}:${canvasVisible}:${assistantOpen}:${assistantLayout.width}`, }); revealCreatedNodes.current = (nodes, allNodes) => { - if (switchPending.current || viewportMoving.current || interacting || pointerGesture.current.active || + if (!canvasVisible || switchPending.current || viewportMoving.current || interacting || pointerGesture.current.active || references.connecting || spacePressed || selectedRef.current.length > 1 || editing || preview || activeAction || textEditingId || composerExpanded) return; const focused = document.activeElement; @@ -631,7 +636,7 @@ export function useWorkspaceInteraction(rt: WorkspaceRuntime) { const bounds = boundsOfNodes(nodes, allNodes); if (bounds) selectionPresentation.reveal(bounds); }; - useCanvasWheelBoundary(root, selectionPresentation.userNavigation); + useCanvasWheelBoundary(canvasRegion, selectionPresentation.userNavigation, canvasVisible); const flowNodes: FlowNode[] = useMemo( () => orderNodesForCanvas(doc.nodes).map((n) => ({ @@ -755,7 +760,7 @@ export function useWorkspaceInteraction(rt: WorkspaceRuntime) { const onWorkspaceKey = (event: React.KeyboardEvent) => { bypassSnapping.current = event.metaKey || event.ctrlKey; if (bypassSnapping.current) clearGuides(); - if (!ready || switchPending.current || event.nativeEvent.isComposing) return; + if (!canvasVisible || !ready || switchPending.current || event.nativeEvent.isComposing) return; const el = event.target as HTMLElement; const mod = event.metaKey || event.ctrlKey; const key = event.key.toLowerCase(); diff --git a/src/canvas/workspace/useWorkspaceRuntime.ts b/src/canvas/workspace/useWorkspaceRuntime.ts index 218838717..b152c8e45 100644 --- a/src/canvas/workspace/useWorkspaceRuntime.ts +++ b/src/canvas/workspace/useWorkspaceRuntime.ts @@ -1,7 +1,8 @@ import { SaveQueue } from "../saveQueue.ts"; import { useKeyPress, useReactFlow, type Edge } from "@xyflow/react"; -import { useEffect, useMemo, useRef } from "react"; +import { useEffect, useMemo, useRef, useState } from "react"; +import { readWorkspaceMode, saveWorkspaceMode, type WorkspaceMode } from "../workspaceMode"; import { useNodeVersionCounts } from "../NodeVersionHistory"; import { CreationEditorSession } from "../../backend/creationEditorSession"; @@ -115,6 +116,20 @@ export function useWorkspaceRuntime(): WorkspaceRuntime { } })(); const { notifications, notify, dismiss } = useCanvasNotifications(); + const [workspaceMode, setWorkspaceMode] = useState(readWorkspaceMode); + const preferenceWarningShown = useRef(false); + const changeWorkspaceMode = (mode: WorkspaceMode) => { + if (mode === workspaceMode) return; + pointerGesture.current.cancel(); + flowSurface.current?.cancel(); + runtime.references?.cancel(); + runtime.setMenu(null); + setWorkspaceMode(mode); + if (!saveWorkspaceMode(mode) && !preferenceWarningShown.current) { + preferenceWarningShown.current = true; + notify(t("workspaceModes.preferenceFailed")); + } + }; const editorOrigin = useRef(null); const mediaPublications = useRef( new WeakMap(), @@ -191,6 +206,7 @@ export function useWorkspaceRuntime(): WorkspaceRuntime { } | null>(null); const referenceTarget = useRef(null); const root = useRef(null); + const canvasRegion = useRef(null); const assistantLayout = useAssistantLayout(root, assistantOpen, () => { setAssistantOpen(false); requestAnimationFrame(() => @@ -199,6 +215,8 @@ export function useWorkspaceRuntime(): WorkspaceRuntime { ?.focus(), ); }); + const assistantVisible = workspaceMode === "agent" || assistantOpen; + const canvasVisible = workspaceMode === "canvas" && (!assistantLayout.mobile || !assistantOpen); const toolbarElement = useRef(null); const composerElement = useRef(null); const focusSelection = (id: string) => { @@ -245,11 +263,13 @@ export function useWorkspaceRuntime(): WorkspaceRuntime { [backendCatalog.models], ); - const center = (): Point => - flow.screenToFlowPosition({ - x: window.innerWidth / 2 - (panel ? 150 : 0), - y: window.innerHeight * 0.32, + const center = (): Point => { + const bounds = canvasRegion.current?.getBoundingClientRect(); + return flow.screenToFlowPosition({ + x: (bounds?.left ?? 0) + (bounds?.width ?? window.innerWidth) / 2 - (panel ? 150 : 0), + y: (bounds?.top ?? 0) + (bounds?.height ?? window.innerHeight) * 0.32, }); + }; // A node freezes the dialog's model at creation. Changing the picker later // must not rewrite a model the viewer already has on the canvas, so this is // the only place the assistant selection is read. @@ -335,6 +355,11 @@ export function useWorkspaceRuntime(): WorkspaceRuntime { runtime.uploadSource = uploadSource; runtime.referenceTarget = referenceTarget; runtime.root = root; + runtime.canvasRegion = canvasRegion; + runtime.workspaceMode = workspaceMode; + runtime.changeWorkspaceMode = changeWorkspaceMode; + runtime.assistantVisible = assistantVisible; + runtime.canvasVisible = canvasVisible; runtime.assistantLayout = assistantLayout; runtime.toolbarElement = toolbarElement; runtime.composerElement = composerElement; diff --git a/src/canvas/workspace/useWorkspaceTransfer.ts b/src/canvas/workspace/useWorkspaceTransfer.ts index 48d743697..46329f942 100644 --- a/src/canvas/workspace/useWorkspaceTransfer.ts +++ b/src/canvas/workspace/useWorkspaceTransfer.ts @@ -832,7 +832,7 @@ export function useWorkspaceTransfer(rt: WorkspaceRuntime) { }; useEffect(() => { const onPaste = (event: ClipboardEvent) => { - if (event.defaultPrevented || !ready || switchPending.current) return; + if (!rt.canvasVisible || event.defaultPrevented || !ready || switchPending.current) return; const target = event.target; if (!(target instanceof HTMLElement)) return; if ( diff --git a/src/canvas/workspaceMode.ts b/src/canvas/workspaceMode.ts new file mode 100644 index 000000000..ec6499691 --- /dev/null +++ b/src/canvas/workspaceMode.ts @@ -0,0 +1,29 @@ +export type WorkspaceMode = "agent" | "canvas"; + +export const WORKSPACE_MODE_KEY = "opendirector.workspaceMode.v1"; + +/** A browser preference: never part of project content or undo history. */ +export function readWorkspaceMode( + storage?: Pick, +): WorkspaceMode { + try { + return (storage ?? window.localStorage).getItem(WORKSPACE_MODE_KEY) === "agent" + ? "agent" + : "canvas"; + } catch { + return "canvas"; + } +} + +/** Report preference failures separately from successful in-memory switching. */ +export function saveWorkspaceMode( + mode: WorkspaceMode, + storage?: Pick, +): boolean { + try { + (storage ?? window.localStorage).setItem(WORKSPACE_MODE_KEY, mode); + return true; + } catch { + return false; + } +} diff --git a/src/i18n/messages/en-US/index.ts b/src/i18n/messages/en-US/index.ts index 29898459a..424cb6bf0 100644 --- a/src/i18n/messages/en-US/index.ts +++ b/src/i18n/messages/en-US/index.ts @@ -1,3 +1,4 @@ +import { workspaceModes } from "./workspaceModes.ts"; import { voiceLibrary } from "./voiceLibrary.ts"; import { trace } from "./trace.ts"; import { modelSetup } from "./modelSetup.ts"; @@ -23,6 +24,7 @@ type ZhCatalog = typeof import("../zh-CN/index.ts").zhCN; /** `Record` makes a missing English key a compile error. */ export const enUS: Record = { ...common, + ...workspaceModes, ...voiceLibrary, ...settings, ...modelSetup, diff --git a/src/i18n/messages/en-US/workspaceModes.ts b/src/i18n/messages/en-US/workspaceModes.ts new file mode 100644 index 000000000..b8cb2f3fa --- /dev/null +++ b/src/i18n/messages/en-US/workspaceModes.ts @@ -0,0 +1,7 @@ +export const workspaceModes = { + "workspaceModes.label": "Creation mode", + "workspaceModes.agent": "Agent mode", + "workspaceModes.canvas": "Canvas mode", + "workspaceModes.preferenceFailed": "Switched modes. This browser could not save your preference.", + "workspaceModes.showCanvas": "Show canvas", +} as const; diff --git a/src/i18n/messages/zh-CN/index.ts b/src/i18n/messages/zh-CN/index.ts index 3561f1e89..610083861 100644 --- a/src/i18n/messages/zh-CN/index.ts +++ b/src/i18n/messages/zh-CN/index.ts @@ -1,3 +1,4 @@ +import { workspaceModes } from "./workspaceModes.ts"; import { voiceLibrary } from "./voiceLibrary.ts"; import { trace } from "./trace.ts"; import { modelSetup } from "./modelSetup.ts"; @@ -20,6 +21,7 @@ import { tools } from "./tools.ts"; /** Canonical copy. Every other locale must provide the same keys. */ export const zhCN = { ...common, + ...workspaceModes, ...voiceLibrary, ...settings, ...modelSetup, diff --git a/src/i18n/messages/zh-CN/workspaceModes.ts b/src/i18n/messages/zh-CN/workspaceModes.ts new file mode 100644 index 000000000..225ebf058 --- /dev/null +++ b/src/i18n/messages/zh-CN/workspaceModes.ts @@ -0,0 +1,7 @@ +export const workspaceModes = { + "workspaceModes.label": "创作模式", + "workspaceModes.agent": "纯净模式", + "workspaceModes.canvas": "画布模式", + "workspaceModes.preferenceFailed": "已切换,本浏览器未能保存模式偏好。", + "workspaceModes.showCanvas": "查看画布", +} as const; diff --git a/tests/browser/assistant_auxiliary_panels.mjs b/tests/browser/assistant_auxiliary_panels.mjs index 06c9269cb..0856d2ab1 100644 --- a/tests/browser/assistant_auxiliary_panels.mjs +++ b/tests/browser/assistant_auxiliary_panels.mjs @@ -129,15 +129,15 @@ try { async function assertLayout(panel) { const chat = await assistant.boundingBox(); const box = await panel.boundingBox(); - assert.ok(box && chat); + assert.ok(box); assert.ok( box.x >= 0 && box.x + box.width <= page.viewportSize().width, "auxiliary panel must stay inside the viewport", ); - if (page.viewportSize().width >= chat.width + box.width + 52) + if (await assistant.isVisible() && chat && page.viewportSize().width >= chat.width + box.width + 52) assert.ok( - box.x + box.width <= chat.x - 10, - "with room available, auxiliary panel must sit left of chat", + box.x >= chat.x + chat.width + 10, + `with room available, auxiliary panel must sit right of chat: ${JSON.stringify({ width: page.viewportSize().width, chat, panel: box })}`, ); // A visible DOM node can still be hidden behind the assistant. await panel.locator("button").first().click({ trial: true }); @@ -156,7 +156,7 @@ try { await auxiliary(kind).waitFor({ state: "detached" }); } - await chatButton.click(); + if (!(await assistant.isVisible())) await chatButton.click(); await editor.fill(draft); for (const locale of ["zh-CN", "en-US"]) { // Use the actual language switch rather than reloading with new storage. @@ -170,11 +170,16 @@ try { assert.equal(await page.locator("html").getAttribute("lang"), locale); for (const width of [1720, 1024, 800, 600, 390]) { await page.setViewportSize({ width, height: 1120 }); + // Narrow canvas mode switches between chat and canvas. Its auxiliary + // controls belong to the canvas; returning to chat retains the draft. + const alongsideChat = width > 700; + if (!alongsideChat && await assistant.isVisible()) + await page.locator(".cw-narrow-region-toggle").click(); for (const kind of ["search", "history"]) { await openAuxiliary(kind, locale); await assertChat( - true, - `${locale}/${width}: opening ${kind} preserves chat`, + alongsideChat, + `${locale}/${width}: opening ${kind} preserves the chosen region`, ); await assertLayout(auxiliary(kind)); if ( @@ -191,11 +196,15 @@ try { }); } await closeAuxiliary(kind, locale); - await assertChat(true, `closing ${kind} preserves chat`); + await assertChat(alongsideChat, `closing ${kind} preserves the chosen region`); await openAuxiliary(kind, locale); await trigger(kind, locale).click(); await auxiliary(kind).waitFor({ state: "detached" }); - await assertChat(true, `toggling ${kind} preserves chat`); + await assertChat(alongsideChat, `toggling ${kind} preserves the chosen region`); + } + if (!alongsideChat) { + await chatButton.click(); + await assertChat(true, "returning from narrow canvas preserves the chat draft"); } } await page.setViewportSize({ width: 1720, height: 1120 }); diff --git a/tests/browser/canvas_crop.mjs b/tests/browser/canvas_crop.mjs index cada38470..c80269a2c 100644 --- a/tests/browser/canvas_crop.mjs +++ b/tests/browser/canvas_crop.mjs @@ -63,9 +63,9 @@ async function saved() { await page.getByText("Saved on this device", { exact: true }).waitFor(); } async function menu(itemId, action) { - await page - .locator(".react-flow__pane") - .click({ position: { x: 1400, y: 800 } }); + const pane = page.locator(".react-flow__pane"); + const canvas = await pane.boundingBox(); + await pane.click({ position: { x: canvas.width - 200, y: canvas.height - 200 } }); await page.keyboard.press("Escape"); await page .locator(`.react-flow__node[data-id="${itemId}"] .cw-node-title`) diff --git a/tests/browser/canvas_switch.mjs b/tests/browser/canvas_switch.mjs index 8a7d3045f..faff75f1a 100644 --- a/tests/browser/canvas_switch.mjs +++ b/tests/browser/canvas_switch.mjs @@ -62,8 +62,34 @@ try { await page.getByText("Saved on this device", { exact: true }).waitFor(); await picker.click({ trial: true }); }; - await page.goto(`${web}/ai-tool/ai-canvas/${projects[0].id}`); - await ready(projects[0]); + // A slow default-open chat must not steal focus and dismiss the project picker. + let releaseAssistant; + const assistantGate = new Promise(resolve => { releaseAssistant = resolve; }); + const assistantPath = `**/creation/projects/${projects[0].id}/assistant`; + await page.route(assistantPath, async route => { + const response = await route.fetch(); + await assistantGate; + await route.fulfill({ response }); + }, { times: 1 }); + const releaseDeadline = setTimeout(() => releaseAssistant(), 15000); + try { + await page.goto(`${web}/ai-tool/ai-canvas/${projects[0].id}`); + await ready(projects[0]); + await picker.click(); + const target = page.getByRole("menuitemradio", { name: projects[1].name, exact: true }); + await target.waitFor(); + releaseAssistant(); + await page.waitForFunction(() => document.querySelector('#canvas-assistant [role="textbox"]')?.isContentEditable); + await page.evaluate(() => new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)))); + assert.equal(await picker.getAttribute("aria-expanded"), "true", "chat loading must keep the project picker open"); + assert.equal(await page.locator('.cw-project-search input').evaluate(element => element === document.activeElement), true); + await target.click({ trial: true }); + await page.keyboard.press("Escape"); + } finally { + clearTimeout(releaseDeadline); + releaseAssistant(); + await page.unroute(assistantPath); + } const samples = []; for (let index = 0; index < 12; index++) { const target = projects[(index + 1) % 2]; diff --git a/tests/browser/canvas_text_markdown.mjs b/tests/browser/canvas_text_markdown.mjs index 7705c1a2c..0b620cf4f 100644 --- a/tests/browser/canvas_text_markdown.mjs +++ b/tests/browser/canvas_text_markdown.mjs @@ -128,9 +128,12 @@ async function checkTextNode(resizeZoom) { // Normal selection can still fit before the next resize begins. await page.locator(".react-flow__pane").click({ position: { x: 20, y: 80 } }); const fitBox = await node.boundingBox(); - await page.mouse.move(1000, 800); + const canvas = await page.locator(".react-flow__pane").boundingBox(); + const panStart = { x: canvas.x + canvas.width / 2, y: canvas.y + canvas.height * 0.8 }; + await page.mouse.move(panStart.x, panStart.y); await page.mouse.down({ button: "middle" }); - await page.mouse.move(1000 + 100 - fitBox.x, 800 + 140 - fitBox.y, { steps: 8 }); + await page.mouse.move(panStart.x + canvas.x + 100 - fitBox.x, + panStart.y + canvas.y + 140 - fitBox.y, { steps: 8 }); await page.mouse.up({ button: "middle" }); const beforeFit = await page.locator(".react-flow__viewport").evaluate((element) => element.style.transform); await node.click({ position: { x: 30, y: 40 } }); diff --git a/tests/browser/canvas_wheel.mjs b/tests/browser/canvas_wheel.mjs index cc6405362..22750146a 100644 --- a/tests/browser/canvas_wheel.mjs +++ b/tests/browser/canvas_wheel.mjs @@ -1,4 +1,4 @@ -/** Isolated fixture only: modified wheels belong to the canvas, never page UI. */ +/** Isolated fixture only: modified wheels zoom only within the canvas region. */ import assert from "node:assert/strict"; const { chromium } = await import( process.env.PLAYWRIGHT_MODULE ?? "playwright-core" @@ -63,17 +63,28 @@ try { ); }); const metrics = () => - page.evaluate(() => ({ - zoom: new DOMMatrix( + page.evaluate(() => { + const viewport = new DOMMatrix( getComputedStyle(document.querySelector(".react-flow__viewport")) .transform, - ).a, - width: window.innerWidth, - ratio: window.devicePixelRatio, - scale: window.visualViewport.scale, - })); + ); + return { + zoom: viewport.a, + x: viewport.e, + y: viewport.f, + width: window.innerWidth, + ratio: window.devicePixelRatio, + scale: window.visualViewport.scale, + }; + }); const controls = []; - async function zoomWheel(locator, name) { + const outsideCanvas = []; + async function modifiedWheel(locator, name, zoomCanvas = true) { + assert.equal( + await locator.evaluate((element) => Boolean(element.closest(".cw-canvas-region"))), + zoomCanvas, + `${name}: verify the wheel target's region`, + ); await locator.hover(); await page.waitForTimeout(100); const before = await metrics(); @@ -96,50 +107,69 @@ try { } await page.waitForTimeout(180); const after = await metrics(); - assert.ok(after.zoom > before.zoom, `${name}: wheel must zoom the canvas`); - assert.deepEqual( - { ...after, zoom: before.zoom }, - before, - `${name}: page UI must stay fixed`, - ); + if (zoomCanvas) { + assert.ok(after.zoom > before.zoom, `${name}: wheel must zoom the canvas`); + assert.deepEqual( + { ...after, zoom: before.zoom, x: before.x, y: before.y }, + before, + `${name}: page UI must stay fixed`, + ); + } else { + assert.deepEqual( + { zoom: after.zoom, x: after.x, y: after.y }, + { zoom: before.zoom, x: before.x, y: before.y }, + `${name}: wheel must preserve the canvas viewport`, + ); + } const prevented = await page.evaluate( (count) => window.__modifiedWheels.slice(count), count, ); - assert.ok( - prevented.length && prevented.every(Boolean), - `${name}: native default must be cancelled`, - ); + if (zoomCanvas) { + assert.ok( + prevented.length && prevented.every(Boolean), + `${name}: native default must be cancelled`, + ); + } // Meta+wheel follows the same boundary without changing ordinary scrolling. - assert.equal( - await locator.evaluate((element) => { - const rect = element.getBoundingClientRect(); - return !element.dispatchEvent( - new WheelEvent("wheel", { - bubbles: true, - cancelable: true, - deltaY: 120, - metaKey: true, - clientX: rect.left + rect.width / 2, - clientY: rect.top + rect.height / 2, - }), - ); - }), - true, - ); - await page.waitForFunction( - (zoom) => - Math.abs( - new DOMMatrix( - getComputedStyle(document.querySelector(".react-flow__viewport")) - .transform, - ).a - zoom, - ) < 0.001, - before.zoom, - ); - controls.push(name); + const metaPrevented = await locator.evaluate((element) => { + const rect = element.getBoundingClientRect(); + return !element.dispatchEvent( + new WheelEvent("wheel", { + bubbles: true, + cancelable: true, + deltaY: 120, + metaKey: true, + clientX: rect.left + rect.width / 2, + clientY: rect.top + rect.height / 2, + }), + ); + }); + if (zoomCanvas) { + assert.equal(metaPrevented, true); + await page.waitForFunction( + (zoom) => + Math.abs( + new DOMMatrix( + getComputedStyle(document.querySelector(".react-flow__viewport")) + .transform, + ).a - zoom, + ) < 0.001, + before.zoom, + ); + controls.push(name); + } else { + await page.waitForTimeout(180); + const afterMeta = await metrics(); + assert.deepEqual( + { zoom: afterMeta.zoom, x: afterMeta.x, y: afterMeta.y }, + { zoom: before.zoom, x: before.x, y: before.y }, + `${name}: Meta+wheel must preserve the canvas viewport`, + ); + outsideCanvas.push(name); + } } - await zoomWheel( + await modifiedWheel( page.getByRole("toolbar", { name: "画布创作工具" }), "left toolbar", ); @@ -159,7 +189,7 @@ try { await material.waitFor(); await material.click(); await page.getByRole("toolbar", { name: "节点操作", exact: true }).waitFor(); - await zoomWheel( + await modifiedWheel( page.getByRole("toolbar", { name: "节点操作", exact: true }), "node toolbar", ); @@ -173,7 +203,7 @@ try { "\n", ), ); - await zoomWheel(editor, "node composer"); + await modifiedWheel(editor, "node composer"); await editor.evaluate((element) => (element.scrollTop = 0)); const originalZoom = (await metrics()).zoom; await editor.hover(); @@ -188,7 +218,7 @@ try { exact: true, }); await parameters.waitFor(); - await zoomWheel(parameters, "portaled parameters"); + await modifiedWheel(parameters, "portaled parameters", false); await parameters .getByRole("button", { name: "关闭参数", exact: true }) .evaluate((element) => element.click()); @@ -204,18 +234,20 @@ try { await page .getByRole("dialog", { name: "展开的节点会话框", exact: true }) .waitFor(); - await zoomWheel(editor, "expanded composer"); + await modifiedWheel(editor, "expanded composer"); await page.getByRole("button", { name: "收起会话框", exact: true }).click(); - await page.locator(".cw-chat-button").click(); - await zoomWheel( + const chatToggle = page.locator(".cw-chat-button"); + if (await chatToggle.getAttribute("aria-expanded") === "false") await chatToggle.click(); + await modifiedWheel( page.locator('.cw-assistant-composer [role="textbox"]'), "assistant sidebar", + false, ); await page.getByRole("button", { name: "收起 AI 对话", exact: true }).click(); await page.getByRole("button", { name: "资产库", exact: true }).click(); const library = page.getByRole("dialog", { name: "资产库", exact: true }); await library.waitFor(); - await zoomWheel(library, "asset library dialog"); + await modifiedWheel(library, "asset library dialog", false); await page.getByRole("button", { name: "关闭资产库", exact: true }).click(); // The same trusted wheel still changes React Flow zoom over real canvas areas. async function canvasWheel(point, name) { @@ -270,8 +302,9 @@ try { JSON.stringify({ isolated: true, controlsZoomCanvasOnly: controls, + outsideCanvasRetainsViewport: outsideCanvas, canvasAndNodeZoom: true, - nativePageZoomPrevented: true, + nativePageZoomPreventedOnCanvas: true, ordinaryEditorScroll: true, errors, }), diff --git a/tests/browser/context_menu_dismissal.mjs b/tests/browser/context_menu_dismissal.mjs index 28c242613..9c64fbdc1 100644 --- a/tests/browser/context_menu_dismissal.mjs +++ b/tests/browser/context_menu_dismissal.mjs @@ -55,6 +55,17 @@ try { await page.getByText("Saved on this device", { exact: true }).waitFor(); const node = page.locator(`.react-flow__node[data-id="${fixture.node}"]`); const menu = page.locator(".cw-context-menu"); + const canvasPoint = async () => { + const point = await page.locator(".react-flow__pane").evaluate((pane) => { + const rect = pane.getBoundingClientRect(); + return [ + { x: rect.right - 100, y: rect.bottom - 180 }, + { x: rect.left + 100, y: rect.bottom - 180 }, + ].find(({ x, y }) => document.elementFromPoint(x, y) === pane); + }); + assert.ok(point, "Dismissal starts on the visible canvas outside menus and nodes"); + return point; + }; const openNodeMenu = async () => { await node.click({ button: "right" }); await menu.waitFor(); @@ -67,10 +78,11 @@ try { const before = await page .locator(".react-flow__viewport") .getAttribute("style"); - await page.mouse.move(1100, 750); + const dragStart = await canvasPoint(); + await page.mouse.move(dragStart.x, dragStart.y); await page.mouse.down(); await menu.waitFor({ state: "detached" }); - await page.mouse.move(1170, 800, { steps: 10 }); + await page.mouse.move(dragStart.x + 70, dragStart.y + 50, { steps: 10 }); await page.mouse.up(); assert.notEqual( await page.locator(".react-flow__viewport").getAttribute("style"), @@ -80,16 +92,25 @@ try { (await node.getAttribute("class")).split(" ").includes("selected"), ); await openNodeMenu(); - await page.mouse.click(1100, 750); + const clickPoint = await canvasPoint(); + await page.mouse.click(clickPoint.x, clickPoint.y); await menu.waitFor({ state: "detached" }); await openNodeMenu(); const menuItem = menu.getByRole("menuitem").first(); + const pending = page.locator("[data-pending-references]"); + await pending.waitFor(); await menuItem.focus(); await page.keyboard.press("Escape"); await menu.waitFor({ state: "detached" }); + assert.equal(await pending.isVisible(), true, "Menu Escape preserves staged chat references"); assert.ok( (await node.getAttribute("class")).split(" ").includes("selected"), ); + await page.locator("#canvas-assistant [contenteditable=true]").focus(); + await page.keyboard.press("Escape"); + await pending.waitFor({ state: "detached" }); + assert.equal(await page.locator("#canvas-assistant").isVisible(), true, + "Chat Escape clears staged references while keeping the conversation open"); // The node composer stops pointerdown propagation; clicking it must still // dismiss the menu instead of leaving it stuck open over the canvas. The // menu can cover the composer, so send the pointerdown straight at it. @@ -125,7 +146,8 @@ try { await page.keyboard.press("Escape"); await menu.waitFor({ state: "detached" }); await openPortMenu(); - await page.mouse.click(1100, 750); + const portClickPoint = await canvasPoint(); + await page.mouse.click(portClickPoint.x, portClickPoint.y); await menu.waitFor({ state: "detached" }); console.log(JSON.stringify({ mode: "unified", passed: true })); } diff --git a/tests/browser/created_node_focus.mjs b/tests/browser/created_node_focus.mjs index d33af2ccd..b3db06ee9 100644 --- a/tests/browser/created_node_focus.mjs +++ b/tests/browser/created_node_focus.mjs @@ -23,7 +23,8 @@ try { type: "project.create", payload: { name: `Viewport focus ${projectId.slice(0, 8)}` } }); await page.goto(`${base}/ai-tool/ai-canvas/${projectId}?creation=unified`); await page.getByText("Saved on this device", { exact: true }).waitFor(); - await page.locator(".cw-chat-button").click(); + const chatToggle = page.locator(".cw-chat-button"); + if (await chatToggle.getAttribute("aria-expanded") === "false") await chatToggle.click(); await page.locator(".cw-assistant-panel:not([hidden])").waitFor(); headers["X-OpenDirector-Session"] = await page.evaluate(async () => (await import("/src/backend/api.ts")).sessionId); const create = async (x, y) => { @@ -38,9 +39,10 @@ try { const checkCentered = async (id) => { await page.waitForFunction((id) => { const node = document.querySelector(`.react-flow__node[data-id="${id}"]`).getBoundingClientRect(); - const panel = document.querySelector(".cw-assistant-panel").getBoundingClientRect(); - return node.left > 75 && node.right < panel.left - 10 && node.top > 72 && node.bottom < innerHeight - 65 && - Math.abs((node.left + node.right) / 2 - (80 + panel.left - 16) / 2) < 50; + const canvas = document.querySelector(".cw-canvas-region").getBoundingClientRect(); + const left = canvas.left + (canvas.width < 700 ? 64 : 80), right = canvas.right - 20; + return node.left > left - 5 && node.right < right + 10 && node.top > canvas.top + 72 && node.bottom < canvas.bottom - 65 && + Math.abs((node.left + node.right) / 2 - (left + right) / 2) < 50; }, id); }; const editor = page.locator(".cw-assistant-panel [contenteditable=true]").first(); @@ -51,7 +53,8 @@ try { assert.equal(await page.locator(".react-flow__node.selected").count(), 0); await page.screenshot({ path: "/tmp/created-node-focus.png" }); await page.waitForTimeout(250); - await page.mouse.move(100, 800); + const canvas = await page.locator(".cw-canvas-region").boundingBox(); + await page.mouse.move(canvas.x + 100, 800); await page.mouse.down(); const before = await transform(); await create(5000, 5000); diff --git a/tests/browser/default_entry.mjs b/tests/browser/default_entry.mjs index 6b10b32d1..567e4b381 100644 --- a/tests/browser/default_entry.mjs +++ b/tests/browser/default_entry.mjs @@ -187,7 +187,8 @@ try { await page .getByRole("button", { name: name + " renamed", exact: true }) .waitFor(); - await page.getByRole("button", { name: "与 AI 对话", exact: true }).click(); + const chatToggle = page.getByRole("button", { name: "与 AI 对话", exact: true }); + if (await chatToggle.getAttribute("aria-expanded") === "false") await chatToggle.click(); const message = page.getByRole("textbox", { name: "消息内容", exact: true }); await message.waitFor(); assert.equal((await message.innerText()).replace(/\u200b/g, "").trim(), ""); diff --git a/tests/browser/double_shift_chat.mjs b/tests/browser/double_shift_chat.mjs index 3b108ecba..779f2447d 100644 --- a/tests/browser/double_shift_chat.mjs +++ b/tests/browser/double_shift_chat.mjs @@ -24,6 +24,9 @@ try { const trigger = page.locator(".cw-project-chevron"); await trigger.waitFor(); await page.locator('.cw-workspace[aria-busy="false"]').waitFor(); + // Canvas mode now starts with chat expanded; establish the closed state + // before testing the shortcut's open/focus/close cycle. + await page.getByRole("button", { name: "Collapse AI chat", exact: true }).click(); await trigger.focus(); const tap = async () => { await page.keyboard.press("Shift"); await page.keyboard.press("Shift"); }; await tap(); diff --git a/tests/browser/jiuwenswarm_picker.mjs b/tests/browser/jiuwenswarm_picker.mjs index 5a371f1fa..1ec32b9a7 100644 --- a/tests/browser/jiuwenswarm_picker.mjs +++ b/tests/browser/jiuwenswarm_picker.mjs @@ -86,7 +86,8 @@ try { ); await page.goto(base); await page.getByText("已保存到本机", { exact: true }).waitFor(); - await page.getByRole("button", { name: "与 AI 对话", exact: true }).click(); + const chatToggle = page.getByRole("button", { name: "与 AI 对话", exact: true }); + if (await chatToggle.getAttribute("aria-expanded") === "false") await chatToggle.click(); const trigger = page.getByRole("button", { name: /^Agent:/ }); await trigger.click(); await page @@ -158,7 +159,7 @@ try { await page.waitForTimeout(100); } await page.reload(); - await page.getByRole("button", { name: "与 AI 对话", exact: true }).click(); + if (await chatToggle.getAttribute("aria-expanded") === "false") await chatToggle.click(); await page .getByRole("button", { name: "Agent:JiuwenSwarm · Swarm Alternate", diff --git a/tests/browser/kling_video.mjs b/tests/browser/kling_video.mjs index 7b9518c2f..95841a642 100644 --- a/tests/browser/kling_video.mjs +++ b/tests/browser/kling_video.mjs @@ -126,6 +126,8 @@ try { }); await page.goto(`${base}/ai-tool/ai-canvas/${projectId}?creation=unified`); await page.getByText(text.saved, { exact: true }).waitFor(); + const chatToggle = page.locator(".cw-chat-button"); + if (await chatToggle.getAttribute("aria-expanded") === "true") await chatToggle.click(); assert.equal( await page.locator(".cw-chat-button").getAttribute("aria-expanded"), "false", @@ -501,6 +503,9 @@ try { ); await unopened.getByText(text.saved, { exact: true }).waitFor(); await until(async () => assistantHeld, Boolean, "assistant read held"); + // Keep the canvas-only path while the assistant's model selection is loading. + const chatToggle = unopened.locator(".cw-chat-button"); + if (await chatToggle.getAttribute("aria-expanded") === "true") await chatToggle.click(); assert.equal( await unopened.locator(".cw-chat-button").getAttribute("aria-expanded"), "false", diff --git a/tests/browser/manifest.json b/tests/browser/manifest.json index ea78287e6..3b1bc1999 100644 --- a/tests/browser/manifest.json +++ b/tests/browser/manifest.json @@ -57,6 +57,13 @@ ], "note": "Needs --assistant-fixture services." }, + { + "file": "workspace_modes.mjs", + "fixture": true, + "previewSafe": true, + "lanes": ["extendedFixture", "production"], + "note": "Mode layout, drafts, references, viewport and active turn/job preservation in both languages; needs --assistant-fixture services." + }, { "file": "canvas_crop.mjs", "previewSafe": true, diff --git a/tests/browser/provider_settings.mjs b/tests/browser/provider_settings.mjs index 5cd8f904c..f1f384223 100644 --- a/tests/browser/provider_settings.mjs +++ b/tests/browser/provider_settings.mjs @@ -175,7 +175,8 @@ try { await page.getByRole("button", { name: "Cancel", exact: true }).click(); assert.equal((await call("/api/model-configs")).length, models.length); await page.getByRole("button", { name: "Close settings", exact: true }).click(); - await page.locator(".cw-chat-button").click(); + const chatToggle = page.locator(".cw-chat-button"); + if (await chatToggle.getAttribute("aria-expanded") === "false") await chatToggle.click(); await page.getByRole("button", { name: "Model settings: Multimodal", exact: true }).click(); const picker = page.getByRole("dialog", { name: "Model settings", exact: true }); await picker.getByRole("tab", { name: /Video/ }).click(); diff --git a/tests/browser/timeline_workflow.mjs b/tests/browser/timeline_workflow.mjs index 8b78dac51..665818ade 100644 --- a/tests/browser/timeline_workflow.mjs +++ b/tests/browser/timeline_workflow.mjs @@ -337,21 +337,44 @@ try { await page.goto(full ? base + "/" : url); await saved(); if (full) { - await page.getByRole("button", { name: "Switch project", exact: true }).click(); - const [response] = await Promise.all([ - page.waitForResponse((r) => r.url().endsWith("/commands") && - r.request().postDataJSON()?.type === "project.create"), - page.getByRole("menuitem", { name: "New canvas project", exact: true }).click(), - ]); - assert.equal((await response.json()).status, "applied"); - pid = response.request().postDataJSON().projectId; - url = base + `/ai-tool/ai-canvas/${pid}`; - const projectName = page.getByRole("textbox", { name: "Project name", exact: true }); - const name = "Full creation " + crypto.randomUUID().slice(0, 8); - await projectName.fill(name); - await projectName.press("Enter"); - await page.getByRole("button", { name, exact: true }).waitFor(); - await saved(); + await page.waitForFunction(() => document.querySelector('#canvas-assistant [role="textbox"]')?.isContentEditable); + // The new project's chat can load after its name editor has already opened. + let releaseAssistant; + const assistantGate = new Promise(resolve => { releaseAssistant = resolve; }); + const assistantPath = /\/creation\/projects\/[^/]+\/assistant$/; + await page.route(assistantPath, async route => { + const response = await route.fetch(); + await assistantGate; + await route.fulfill({ response }); + }, { times: 1 }); + const releaseDeadline = setTimeout(() => releaseAssistant(), 20000); + try { + await page.getByRole("button", { name: "Switch project", exact: true }).click(); + const [response] = await Promise.all([ + page.waitForResponse((r) => r.url().endsWith("/commands") && + r.request().postDataJSON()?.type === "project.create"), + page.getByRole("menuitem", { name: "New canvas project", exact: true }).click(), + ]); + assert.equal((await response.json()).status, "applied"); + pid = response.request().postDataJSON().projectId; + url = base + `/ai-tool/ai-canvas/${pid}`; + const projectName = page.getByRole("textbox", { name: "Project name", exact: true }); + await projectName.waitFor(); + releaseAssistant(); + await page.waitForFunction(() => document.querySelector('#canvas-assistant [role="textbox"]')?.isContentEditable); + await page.evaluate(() => new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)))); + assert.equal(await projectName.isVisible(), true, "chat loading must keep the new project's name editor open"); + assert.equal(await projectName.evaluate(element => element === document.activeElement), true); + const name = "Full creation " + crypto.randomUUID().slice(0, 8); + await projectName.fill(name); + await projectName.press("Enter"); + await page.getByRole("button", { name, exact: true }).waitFor(); + await saved(); + } finally { + clearTimeout(releaseDeadline); + releaseAssistant(); + await page.unroute(assistantPath); + } await page .getByRole("toolbar", { name: "Canvas creation tools" }) .getByRole("button", { name: "Add Timeline", exact: true }) diff --git a/tests/browser/workspace_modes.mjs b/tests/browser/workspace_modes.mjs new file mode 100644 index 000000000..156b85cc6 --- /dev/null +++ b/tests/browser/workspace_modes.mjs @@ -0,0 +1,355 @@ +/** Mode changes preserve the same workspace and running work (#386). + * Requires creation_server.py --assistant-fixture; no real models are used. + */ +import assert from "node:assert/strict"; +import { mkdir } from "node:fs/promises"; +import { join } from "node:path"; +import { launchBrowser } from "./engine.mjs"; +import { chooseGenerationModel } from "./model_picker.mjs"; + +const base = process.env.CREATION_WEB_URL ?? "http://127.0.0.1:5180"; +const headers = { + "X-OpenDirector-Request": "1", + "X-OpenDirector-Session": crypto.randomUUID(), + "Content-Type": "application/json", +}; +async function api(path, body) { + const response = await fetch(base + "/api" + path, { + headers, + ...(body ? { method: "POST", body: JSON.stringify(body) } : {}), + }); + assert.ok(response.ok, await response.clone().text()); + return response.json(); +} +async function until(read, matches, label) { + const deadline = Date.now() + 20000; + while (true) { + const value = await read(); + if (matches(value)) return value; + assert.ok(Date.now() < deadline, `${label}: ${JSON.stringify(value)}`); + await new Promise((resolve) => setTimeout(resolve, 50)); + } +} +const health = await api("/creation-test-health"); +assert.equal(health.isolated, true); +assert.equal(health.assistantFixture, true); +const projectId = crypto.randomUUID(); +const root = `/creation/projects/${projectId}`; +await api(root + "/commands", { + projectId, + commandId: crypto.randomUUID(), + type: "project.create", + payload: { name: `Workspace modes ${projectId.slice(0, 8)}` }, +}); +const state = () => api(root); +const conversation = () => api(root + "/assistant"); +const browser = await launchBrowser(); +let page; +try { + page = await browser.newPage({ + locale: "zh-CN", + viewport: { width: 1720, height: 1120 }, + }); + page.setDefaultTimeout(15000); + const errors = [], commands = [], stops = []; + page.on("pageerror", (error) => errors.push(error.message)); + page.on("request", (request) => { + if (/\/assistant\/turns\/[^/]+\/stop$/.test(request.url())) + stops.push(request.url()); + if (request.method() === "POST" && request.url().endsWith("/commands")) + commands.push(request.postDataJSON()); + }); + await page.route(/\/assistant\/runtimes(?:\?.*)?$/, (route) => + route.fulfill({ json: [{ + id: "default", label: "Default", group: "default", modelSource: "settings", + available: true, status: "ready", modelOverride: false, supportsSkills: true, + }] }), + ); + await page.addInitScript(() => { + localStorage.setItem("opendirector.modelSetup.v1", "dismissed"); + }); + await page.goto(`${base}/ai-tool/ai-canvas/${projectId}`); + await page.locator('.cw-workspace[aria-busy="false"]').waitFor(); + const workspace = page.locator("[data-workspace-mode]"); + const canvas = page.getByTestId("canvas-region"); + const assistant = page.locator("#canvas-assistant"); + const editor = assistant.locator('[contenteditable="true"]').first(); + const mode = (value) => page.getByTestId(`workspace-mode-${value}`); + const settleLayout = () => page.evaluate(() => new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(resolve)))); + const selectMode = async (value) => { + await mode(value).click(); + await until(() => workspace.getAttribute("data-workspace-mode"), (current) => current === value, `mode ${value}`); + await settleLayout(); + }; + const viewport = () => page.locator(".react-flow__viewport").evaluate((element) => { + const value = new DOMMatrix(getComputedStyle(element).transform); + return [value.a, value.d, value.e, value.f]; + }); + const selection = () => page.locator(".react-flow__node.selected").evaluateAll((nodes) => nodes.map((node) => node.dataset.id).sort()); + const editorText = () => editor.evaluate((element) => element.textContent.replaceAll("\u200b", "")); + async function setLocale(locale) { + const current = await page.locator("html").getAttribute("lang"); + await page.getByRole("button", { name: current === "zh-CN" ? "设置" : "Settings", exact: true }).click(); + await page.locator("#settings-language").selectOption(locale); + await page.locator(".cw-settings-close").click(); + assert.equal(await page.locator("html").getAttribute("lang"), locale); + } + async function screenshot(name) { + if (!process.env.CREATION_SCREENSHOTS) return; + await mkdir(process.env.CREATION_SCREENSHOTS, { recursive: true }); + await page.screenshot({ path: join(process.env.CREATION_SCREENSHOTS, `workspace-${name}.png`) }); + } + async function assertLayout(value) { + const chat = await assistant.boundingBox(); + assert.ok(chat && await assistant.isVisible(), "chat remains visible"); + if (value === "canvas") { + const area = await canvas.boundingBox(); + assert.ok(area && await canvas.isVisible()); + assert.ok(chat.x + chat.width <= area.x + 2, "chat is left of the canvas without overlapping it"); + assert.ok(Math.abs(area.width / chat.width - 2) < 0.15, "default canvas/chat widths are 2:1"); + } else { + assert.equal(await canvas.isVisible(), false, "agent mode hides the canvas"); + assert.equal(await canvas.locator(".react-flow__node").evaluateAll((nodes) => nodes.some((node) => node.checkVisibility({ opacityProperty: true, visibilityProperty: true }))), false, "React Flow children must not paint through the hidden canvas"); + assert.ok(Math.abs(chat.x + chat.width / 2 - page.viewportSize().width / 2) < 20, "agent chat is centered"); + assert.ok(chat.width <= 860, "wide agent chat keeps a readable width"); + } + } + + // A fresh browser starts in canvas mode with chat already expanded. + assert.equal(await workspace.getAttribute("data-workspace-mode"), "canvas"); + await editor.waitFor(); + assert.equal(await page.locator(".cw-chat-button").getAttribute("aria-expanded"), "true"); + await assertLayout("canvas"); + await page.setViewportSize({ width: 2560, height: 1440 }); + await settleLayout(); + await assertLayout("canvas"); + await page.setViewportSize({ width: 1720, height: 1120 }); + await settleLayout(); + await until(conversation, (value) => !!value.modelSelections.agent, "default agent model must be ready"); + + await page.getByRole("toolbar", { name: "画布创作工具" }).getByRole("button", { name: "添加图片", exact: true }).click(); + await chooseGenerationModel(page, { label: "fixture-image" }); + const prompt = "Keep the node composer prompt across modes"; + await page.getByRole("textbox", { name: "生成提示词", exact: true }).fill(prompt); + const prepared = await until(state, (value) => value.generationSpecs.some((spec) => spec.prompt === prompt && spec.modelConfigId), "image configuration must persist"); + const specId = prepared.generationSpecs.find((spec) => spec.prompt === prompt).id; + const selected = await selection(); + assert.equal(selected.length, 1); + + // Change zoom through the real canvas gesture, then wait for its view save. + const beforeZoom = await viewport(); + const viewSaved = page.waitForResponse((response) => response.request().method() === "PUT" && response.url().includes(`${root}/views/`) && response.ok()); + const canvasBox = await canvas.boundingBox(); + await page.mouse.move(canvasBox.x + canvasBox.width * 0.8, canvasBox.y + canvasBox.height * 0.7); + await page.keyboard.down("Control"); + try { await page.mouse.wheel(0, -180); } finally { await page.keyboard.up("Control"); } + await until(viewport, (value) => value[0] !== beforeZoom[0], "canvas zoom must change"); + await viewSaved; + const savedViewport = await viewport(); + + const draft = "Keep this draft / 保留这份草稿"; + await editor.fill(draft); + await assistant.locator('input[type="file"]').setInputFiles({ + name: "mode-reference.txt", mimeType: "text/plain", buffer: Buffer.from("Mode switch reference"), + }); + const savedChat = await until(conversation, (value) => value.draft.includes(draft) && value.assets.length === 1, "draft and reference must persist"); + const draftText = await editorText(); + await editor.evaluate((element) => { window.__workspaceModeEditor = element; }); + const snapshotBeforeSwitch = await state(); + const commandCount = commands.length; + + for (const locale of ["zh-CN", "en-US"]) { + await setLocale(locale); + for (const value of ["agent", "canvas", "agent", "canvas"]) { + await selectMode(value); + await assertLayout(value); + assert.equal(await editor.evaluate((element) => element === window.__workspaceModeEditor), true, "mode and language changes keep the input DOM instance"); + assert.equal(await editorText(), draftText); + const current = await conversation(); + assert.equal(current.conversationId, savedChat.conversationId); + assert.deepEqual(current.assets, savedChat.assets); + if (value === "canvas") { + assert.deepEqual(await selection(), selected); + assert.deepEqual(await viewport(), savedViewport); + assert.equal(await page.getByRole("textbox", { name: locale === "zh-CN" ? "生成提示词" : "Prompt", exact: true }).evaluate((element) => element.isContentEditable ? element.textContent.replaceAll("\u200b", "") : element.value), prompt); + } + } + await screenshot(`${locale}-canvas`); + await selectMode("agent"); + await screenshot(`${locale}-agent`); + // Hidden canvas handlers must not act on its retained selection. + await mode("agent").focus(); + await page.keyboard.press("Delete"); + await assistant.hover(); + await page.keyboard.down("Control"); + try { await page.mouse.wheel(0, -120); } finally { await page.keyboard.up("Control"); } + assert.deepEqual(await viewport(), savedViewport, "wheel over agent chat must not zoom the hidden canvas"); + await page.keyboard.press("Shift"); + await page.keyboard.press("Shift"); + await until(() => editor.evaluate((element) => element === document.activeElement || element.contains(document.activeElement)), Boolean, "double Shift focuses the agent composer"); + assert.equal(await assistant.isVisible(), true); + await selectMode("canvas"); + assert.deepEqual(await selection(), selected, "Delete in agent mode leaves canvas selection intact"); + } + assert.deepEqual(await state(), snapshotBeforeSwitch, "layout changes preserve project objects, versions and history cursor"); + assert.equal(commands.length, commandCount, "mode changes do not submit project commands"); + + // The language control remains usable in agent mode through Settings. + await selectMode("agent"); + await setLocale("zh-CN"); + assert.equal(await page.locator(".cw-language-toggle").isVisible(), false); + assert.equal(await page.evaluate(() => localStorage.getItem("opendirector.workspaceMode.v1")), "agent"); + await page.reload(); + await page.locator('.cw-workspace[aria-busy="false"]').waitFor(); + await editor.waitFor(); + assert.equal(await workspace.getAttribute("data-workspace-mode"), "agent"); + assert.equal((await conversation()).conversationId, savedChat.conversationId); + await until(editorText, (value) => value === draftText, "reload restores the same draft"); + assert.deepEqual((await conversation()).assets, savedChat.assets); + await selectMode("canvas"); + + // Narrow layouts still expose both mode controls and the same input instance. + await editor.evaluate((element) => { window.__workspaceModeEditor = element; }); + for (const width of [600, 390]) { + await page.setViewportSize({ width, height: 900 }); + await selectMode("agent"); + for (const locale of ["zh-CN", "en-US"]) { + await setLocale(locale); + for (const value of ["agent", "canvas"]) { + await mode(value).click({ trial: true }); + const button = await mode(value).boundingBox(); + assert.ok(button.x >= 0 && button.x + button.width <= width, "mode buttons stay inside the narrow window"); + } + assert.equal(await editor.evaluate((element) => element === window.__workspaceModeEditor && element.isContentEditable), true); + await editor.click({ trial: true }); + assert.equal(await editorText(), draftText); + assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= window.innerWidth), true, "narrow page must not overflow horizontally"); + await screenshot(`${locale}-${width}-agent`); + await selectMode("canvas"); + await page.locator(".cw-narrow-region-toggle").click(); + assert.equal(await canvas.isVisible(), true); + assert.equal(await assistant.isVisible(), false); + assert.equal(await page.locator(".cw-narrow-region-toggle").isVisible(), false, "narrow canvas has only the existing chat toggle"); + await page.locator(".cw-chat-button").click(); + assert.equal(await assistant.isVisible(), true); + assert.equal(await editorText(), draftText); + await selectMode("agent"); + } + } + await page.setViewportSize({ width: 1720, height: 1120 }); + await setLocale("zh-CN"); + await selectMode("canvas"); + + // The earlier narrow-window checks deliberately preserve the zoom/pan. + // Start this separate execution scenario by explicitly fitting the canvas. + await workspace.focus(); + await page.keyboard.press("Shift+1"); + const node = page.locator(`.react-flow__node[data-id="${selected[0]}"]`); + const nodeSurface = node.locator(".cw-placeholder-symbol"); + await nodeSurface.click(); + await page.getByRole("button", { name: "提交生成", exact: true }).waitFor(); + const beforeDrag = (await state()).canvasItems.find((item) => item.id === selected[0]).position; + const dragZoom = (await viewport())[0]; + const handle = await nodeSurface.boundingBox(); + const origin = { x: handle.x + handle.width / 2, y: handle.y + handle.height / 2 }; + await page.mouse.move(origin.x, origin.y); + await page.mouse.down(); + await page.mouse.move(origin.x + 80, origin.y + 40, { steps: 12 }); + await page.mouse.up(); + const moved = await until(state, (value) => { + const position = value.canvasItems.find((item) => item.id === selected[0]).position; + return position.x !== beforeDrag.x || position.y !== beforeDrag.y; + }, "drag in the right canvas must persist its new position"); + const afterDrag = moved.canvasItems.find((item) => item.id === selected[0]).position; + assert.ok(Math.abs((afterDrag.x - beforeDrag.x) * dragZoom - 80) < 16); + assert.ok(Math.abs((afterDrag.y - beforeDrag.y) * dragZoom - 40) < 16); + + // Context menus use screen coordinates even inside the offset canvas region. + const surface = await nodeSurface.boundingBox(); + const point = { x: surface.x + surface.width / 2, y: surface.y + surface.height / 2 }; + await page.mouse.click(point.x, point.y, { button: "right" }); + const menu = page.locator(".cw-context-menu").first(); + await menu.waitFor(); + const menuBox = await menu.boundingBox(); + assert.ok(Math.abs(menuBox.x - Math.max(8, Math.min(point.x, 1720 - menuBox.width - 8))) < 3, "node context menu uses the click's screen x"); + assert.ok(menuBox.x >= 0 && menuBox.x + menuBox.width <= 1720); + await page.keyboard.press("Escape"); + await menu.waitFor({ state: "detached" }); + const area = await canvas.boundingBox(); + const blank = { x: area.x + area.width * 0.6, y: area.y + 170 }; + await page.mouse.click(blank.x, blank.y, { button: "right" }); + await menu.waitFor(); + const blankMenu = await menu.boundingBox(); + assert.ok(Math.abs(blankMenu.x - blank.x) < 20 && Math.abs(blankMenu.y - blank.y) < 20, "blank canvas menu opens beside the actual pointer"); + assert.ok(blankMenu.x + blankMenu.width <= 1720 && blankMenu.y + blankMenu.height <= 1120); + await page.keyboard.press("Escape"); + await menu.waitFor({ state: "detached" }); + + // Hold an actual stored Job and Agent turn while changing modes. + await api("/creation-test-gate", { open: false }); + await api("/creation-test-assistant-gate", { open: false }); + const callsBefore = await api("/creation-test-health"); + await nodeSurface.click(); + await page.getByRole("button", { name: "提交生成", exact: true }).click(); + const running = await until(state, (value) => value.jobs.some((job) => job.status === "running" && job.inputSnapshot.spec.id === specId), "image job must be running"); + const jobId = running.jobs.find((job) => job.inputSnapshot.spec.id === specId).id; + await editor.fill("Keep this Agent turn running across workspace modes"); + await assistant.getByRole("button", { name: "发送消息", exact: true }).click(); + const turn = await until(conversation, (value) => !!value.activeTurnId && value.messages.at(-1)?.status === "streaming", "assistant turn must be running"); + await editor.fill(draft); + const runningCommandCount = commands.length; + for (const value of ["agent", "canvas", "agent", "canvas"]) { + await selectMode(value); + assert.equal(await editor.evaluate((element) => element === window.__workspaceModeEditor), true); + assert.equal((await conversation()).activeTurnId, turn.activeTurnId); + assert.equal((await state()).jobs.find((job) => job.id === jobId).status, "running"); + } + assert.equal(commands.length, runningCommandCount); + assert.deepEqual(stops, []); + assert.equal(commands.filter((command) => command.type === "job.cancel").length, 0); + await api("/creation-test-assistant-gate", { open: true }); + await api("/creation-test-gate", { open: true }); + const complete = await until(conversation, (value) => value.messages.at(-1)?.status === "complete", "original assistant turn must complete"); + assert.equal(complete.messages.length, turn.messages.length); + const finished = await until(state, (value) => value.jobs.find((job) => job.id === jobId)?.status === "succeeded", "original image job must complete"); + assert.equal(finished.jobs.length, running.jobs.length); + const callsAfter = await api("/creation-test-health"); + assert.equal(callsAfter.calls.length, callsBefore.calls.length + 1); + assert.equal(callsAfter.assistantContexts.length, callsBefore.assistantContexts.length + 1); + assert.ok((await editorText()).includes(draft)); + if (process.env.CREATION_SCREENSHOTS) { + for (const locale of ["zh-CN", "en-US"]) { + await setLocale(locale); + await selectMode("canvas"); + await workspace.focus(); + await page.keyboard.press("Shift+1"); + let previous, stable = 0; + await until(viewport, (value) => { + stable = JSON.stringify(value) === JSON.stringify(previous) ? stable + 1 : 0; + previous = value; + return stable >= 3; + }, "fit viewport must settle before the review screenshot"); + const dismissNotification = page.locator(".cw-toast:not(.cw-toast-exiting) .cw-toast-close"); + while (await dismissNotification.count()) { + await dismissNotification.first().click(); + } + await page.waitForFunction(() => !document.querySelector(".cw-toast")); + await screenshot(`${locale}-canvas-final`); + await selectMode("agent"); + await screenshot(`${locale}-agent-final`); + } + } + assert.deepEqual(errors, []); + console.log("PASS workspace modes: bilingual layout, persistent preference, same input/draft/reference/session, viewport/selection and uninterrupted tasks"); +} catch (error) { + console.error(error); + if (page) console.error({ page: (await page.locator("body").innerText()).slice(-6000) }); + throw error; +} finally { + try { + await api("/creation-test-assistant-gate", { open: true }); + await api("/creation-test-gate", { open: true }); + } finally { + await browser.close(); + } +} diff --git a/tests/workspaceMode.test.ts b/tests/workspaceMode.test.ts new file mode 100644 index 000000000..acc26636b --- /dev/null +++ b/tests/workspaceMode.test.ts @@ -0,0 +1,42 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { + readWorkspaceMode, + saveWorkspaceMode, + WORKSPACE_MODE_KEY, +} from "../src/canvas/workspaceMode.ts"; + +test("an unset or unsupported preference keeps the canvas entry point", () => { + for (const value of [null, "", "simple", "professional", '"agent"']) { + assert.equal(readWorkspaceMode({ getItem: () => value }), "canvas"); + } +}); + +test("each saved mode is restored without writing project or other preferences", () => { + const values = new Map([["opendirector.locale.v1", "zh-CN"]]); + const storage = { + getItem: (key: string) => values.get(key) ?? null, + setItem: (key: string, value: string) => { values.set(key, value); }, + }; + for (const mode of ["agent", "canvas"] as const) { + assert.equal(saveWorkspaceMode(mode, storage), true); + assert.equal(readWorkspaceMode(storage), mode); + assert.deepEqual([...values], [ + ["opendirector.locale.v1", "zh-CN"], + [WORKSPACE_MODE_KEY, mode], + ]); + } +}); + +test("a denied storage read still opens the canvas", () => { + assert.equal(readWorkspaceMode({ getItem: () => { throw new Error("denied"); } }), "canvas"); +}); + +test("a failed write is reported without replacing the previous preference", () => { + const storage = { + getItem: () => "canvas", + setItem: () => { throw new Error("quota exceeded"); }, + }; + assert.equal(saveWorkspaceMode("agent", storage), false); + assert.equal(readWorkspaceMode(storage), "canvas"); +});