Skip to content

feat(sessions): choose the working directory when creating a chat - #9

Merged
CjiW merged 2 commits into
masterfrom
agent/peer/create-workdir/session-create-workdir
Oct 21, 2026
Merged

CjiW merged 2 commits into
masterfrom
agent/peer/create-workdir/session-create-workdir

Conversation

@CjiW

@CjiW CjiW commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

目标

新建会话时能选工作目录;不填就用服务器默认目录。

为什么要两步

服务端 POST /api/chats/create 只接受 label / subscription_id / model,没有 work_dir,所以只能建完再设:

POST /api/chats/create  {label}                         → chat_id
POST /api/rpc            set_cwd {channel, chat_id, dir, force: true}

force: true 是「显式用户动作」语义(服务端 rpc_table.go 的注释里就写着这个用途):传什么路径就用什么路径,不会被既有 cwd 或自动推断值改写。Web 端 components/agent/api.ts 的 setCwd 同样是 force: true,两端行为一致。

set_cwd 失败不等于创建失败:会话保留,SessionsUiState.notice 弹一条 snackbar 说明原因,之后仍可再改。

目录浏览(修正:服务端其实是有的)

第一版写错了:工作目录在服务器上,Android 的 SAF 选目录拿不到这种路径,我据此以为只能手输 + 候选。
实际上 Web 端目录树走的就是现成接口:

POST /api/fs/list {"path": "/home/cjw", "show_hidden": false}
→ {"entries":[{"name":"xbot","isDir":true,...}]}

所以现在:工作目录输入框右侧图标 → 目录浏览对话框(列子目录 / 进 / 上一级 / 「使用此目录」);
只列 isDir 的项,空目录与读取失败分开提示,路径变化后的过期响应直接丢弃(连点不会覆盖新列表)。
服务端只返回名字,完整路径由客户端拼(joinPath / parentPath,FilePathTest 钉住根目录、尾斜杠、空值)。

另外仍保留「一点即填」的候选:

  • AppPrefs.recentWorkDirs:最近用过的,最新在前,最多 5 个(本地 SharedPreferences,只有 set_cwd 成功才记);
  • 现有会话里出现过的 work_dir 作为补充;
  • 合并逻辑是纯函数 data/WorkDirSuggestions.kt,去重/跳过空串/截断,由 WorkDirSuggestionsTest 覆盖。

候选在 SessionsViewModel.refresh() 里算好放进 state,UI 只渲染(项目约定:UI 不做数据推导)。

验证

  • ./gradlew :app:testDebugUnitTest ✅ 46 个用例(新增 4 个 WorkDirSuggestionsTest + 4 个 FilePathTest)
  • ./gradlew :app:lintDebug ✅
  • ./gradlew :app:assembleRelease ✅ 签名 CN=xbot
  • 试用包:https://minio.cjiw.site/uploads/share/android/xbot-android-24.apk
    (versionCode 24 / 0.3.5,sha256 3c0254d5…,公网回拉校验一致)

顺带

  • docs/agent/sessions.md:create → set_cwd 的流程、/api/fs/list 目录浏览、候选来源、失败语义;
  • AGENTS.md 补一条本次踩到的坑:worktree 里首次 assembleRelease 会报 SigningConfig "release" is missing required property "storeFile",因为 keystore.properties/keystore/ 是 gitignored,新 worktree 没有,需要从主仓库复制;aapt2/apksigner 也要用主仓库 tools/sdk 的绝对路径。

CjiW added 2 commits October 21, 2026 14:12
The server's POST /api/chats/create takes only label/subscription/model --
there is no work_dir field -- so the working directory has to be set right
after creation through RPC set_cwd with force=true, the same two-step flow
the web client uses. force matters: it is the explicit-user-action path, so
the server applies the path verbatim instead of substituting an inferred
one.

Create dialog gains a work-directory field plus one-tap candidates. There is
no directory browsing endpoint on the server (the path lives on the host, not
on the phone), so the candidates are the recently used paths (AppPrefs, max
5, newest first) merged with work_dir values from existing sessions -- a pure
function so it can be unit-tested.

set_cwd failing is not creation failing: the chat is kept and the reason is
surfaced through a one-shot notice/snackbar; the path can still be changed
later from the session.

versionCode 23 (0.3.5), CHANGELOG 0.3.5 entry, and docs/agent/sessions.md
covering the create -> set_cwd flow. AGENTS.md records the worktree gotcha
that release builds need keystore.properties copied in.
Free-text was the wrong shape for this: the working directory lives on the
host, so Android's SAF picker cannot reach it, but the server already exposes
the same listing the web file tree uses -- POST /api/fs/list {path,
show_hidden} -> entries[name, isDir, size, mode, modTime].

Tapping the folder icon on the work-dir field now opens a browser dialog:
list subdirectories, walk into them, go up, and '使用此目录' writes the path
back. Only directories are listed; an empty directory and a failed read are
reported differently, and results are dropped if the path moved on while the
request was in flight (fast tapping used to be able to overwrite a newer
listing).

Path assembly is client-side because the server returns bare names --
joinPath/parentPath handle root, trailing slash and empty input, pinned by
FilePathTest. The recent-path suggestions stay as one-tap chips.

versionCode 24 (still 0.3.5).
@CjiW
CjiW merged commit 6b166c2 into master Oct 21, 2026
1 check passed
@CjiW
CjiW deleted the agent/peer/create-workdir/session-create-workdir branch October 21, 2026 16:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant