Skip to content

feat(content-write): 支持 mode=create 创建新文件#1608

Merged
qin-ctx merged 7 commits intovolcengine:mainfrom
A0nameless0man:feat/content-write-create-file
Apr 21, 2026
Merged

feat(content-write): 支持 mode=create 创建新文件#1608
qin-ctx merged 7 commits intovolcengine:mainfrom
A0nameless0man:feat/content-write-create-file

Conversation

@A0nameless0man
Copy link
Copy Markdown
Contributor

@A0nameless0man A0nameless0man commented Apr 21, 2026

Summary

POST /api/v1/content/write 新增 mode="create" 支持创建新文件,补齐 HTTP API 层的文件创建能力缺口。

背景:当前 content/write 端点要求目标文件已存在,不存在则返回 404。底层 VikingFS.write_file() 已支持创建(自动 _ensure_parent_dirs),但 HTTP 层未暴露。这阻塞了 MCP 插件 memwrite 等需要通过 API 创建新文件的消费方。

Changes

Python 服务层(openviking/storage/content_write.py

  • _validate_mode() 接受 "create"
  • 新增 _CREATE_ALLOWED_EXTENSIONS 白名单(.md/.txt/.json/.yaml/.yml/.toml/.py/.js/.ts),防止创建二进制文件
  • 新增 _validate_create_extension() 扩展名校验
  • 新增 _create_and_write() 方法:自动创建父目录 → 写入文件 → 语义刷新
  • write() 中 create 路径在 _safe_stat 之前分流,文件已存在时返回 HTTP 409

Rust CLI(crates/ov_cli/

  • ov write 新增 --mode 参数(replace | append | create),与 --append 互斥

测试

  • 服务层 9 个 test_create_mode_* 测试(使用 FakeVikingFS 隔离)
  • API 层 6 个测试:正常创建、文件已存在→409、非法扩展名→400、写后读验证等
  • 0 个回归(4 个预存失败与本次变更无关)

Behavior Matrix

mode 文件存在 行为
replace(默认) 覆写(不变)
replace 404 NotFoundError(不变)
append 追加(不变)
append 404 NotFoundError(不变)
create 409 AlreadyExistsError
create 创建文件 + 语义刷新

Backward Compatibility

  • mode 默认值仍为 "replace",不传参行为完全不变
  • 无 breaking change,纯增量扩展

Commits

  • 83bb014 添加 mode=create 服务层和 HTTP API 测试(TDD Red)
  • 133b5dd 实现 mode=create 文件创建功能
  • 7f82b13 ov write 支持 --mode 参数
  • 6142bdb 清理测试文件中的过时 TDD 注释
  • eab0616 修复 _safe_stat 未正确处理 AGFSNotFoundError 的问题
  • 5b41dff 添加 create mode 写后读集成测试

Related

Closes #1585

添加 _is_not_found() 辅助方法,统一检测 NotFoundError 和
AGFSNotFoundError 两种 not-found 异常。在 _safe_stat() 中使用该
方法替代原来的 isinstance(exc, NotFoundError) 检查,确保 AGFS
路径的 not-found 错误被正确包装为 NotFoundError。
验证 mode=create 写入文件后通过 GET /read 读回,确认内容一致。
@github-actions
Copy link
Copy Markdown

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🏅 Score: 85
🧪 PR contains tests
🔒 No security concerns identified
✅ No TODO sections
🔀 Multiple PR themes

Sub-PR theme: Add mode=create to content write API

Relevant files:

  • openviking/server/routers/content.py
  • openviking/storage/content_write.py
  • tests/server/test_api_content_write.py
  • tests/server/test_content_write_service.py

Sub-PR theme: Add --mode flag to Rust CLI write command

Relevant files:

  • crates/ov_cli/src/commands/content.rs
  • crates/ov_cli/src/handlers.rs
  • crates/ov_cli/src/main.rs

⚡ Recommended focus areas for review

Missing Import

Missing import of 'os' module, which is used in _validate_create_extension(). This will cause a NameError when mode=create is used.

from typing import Any, Dict, Optional

@github-actions
Copy link
Copy Markdown

PR Code Suggestions ✨

No code suggestions found for the PR.

1 similar comment
@github-actions
Copy link
Copy Markdown

PR Code Suggestions ✨

No code suggestions found for the PR.

@A0nameless0man
Copy link
Copy Markdown
Contributor Author

A0nameless0man commented Apr 21, 2026

Closes #1585

@qin-ctx qin-ctx merged commit 94699a8 into volcengine:main Apr 21, 2026
6 checks passed
r266-tech added a commit to r266-tech/OpenViking that referenced this pull request Apr 21, 2026
r266-tech added a commit to r266-tech/OpenViking that referenced this pull request Apr 21, 2026
qin-ctx pushed a commit that referenced this pull request Apr 22, 2026
* docs(filesystem): document write() mode=create (#1608)

* docs(filesystem): document write() mode=create zh (#1608)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[Feature]: Support file creation via content write API

3 participants