Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(server): add user add target defaults#2888

Open
qin-ctx wants to merge 3 commits into
mainfrom
feat/user-add-target-defaults
Open

feat(server): add user add target defaults#2888
qin-ctx wants to merge 3 commits into
mainfrom
feat/user-add-target-defaults

Conversation

@qin-ctx

@qin-ctx qin-ctx commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

Description

本 PR 增加“用户默认添加目标”能力,用于让部署配置或用户个人配置决定 add_resource / add_skill 在未显式传目标时写到哪里,同时保持旧行为兼容:如果没有配置默认值,现有调用路径不变。

配置示例:

{
  "server": {
    "user_config_defaults": {
      "add_targets": {
        "resource_uri": "viking://user/resources",
        "skill_uri": "viking://user/skills"
      }
    }
  }
}

解析规则:

  • add_resource:显式 to / parent 优先;否则使用当前用户配置里的 add_targets.resource_uri;再否则使用 server.user_config_defaults.add_targets.resource_uri;都没有时保持旧行为。
  • add_skill:显式 target_uri 优先;否则使用当前用户配置里的 add_targets.skill_uri;再否则使用 server.user_config_defaults.add_targets.skill_uri;都没有时保持旧行为。
  • resource_uri 按默认父目录处理,等价于 parent=<uri>, create_parent=true
  • viking://user/... 会按请求用户解析,例如 Alice 的 viking://user/resources 会落到 Alice 的 user resource 根。
  • resource_uri 支持可写 resource 目录:viking://resources / viking://resources/...viking://user/resources / viking://user/resources/...viking://user/{user_id}/resources / ...viking://user/{user_id}/peers/{peer_id}/resources / ...
  • skill_uri v1 只允许 viking://user/skillsviking://agent/skills

用户覆盖配置通过 HTTP 管理:

curl -X PATCH http://localhost:1933/api/v1/user-settings/add-locations \
  -H "Content-Type: application/json" \
  -H "X-API-Key: <user-key>" \
  -d '{"resource_uri":"viking://user/resources/project-a"}'

创建账号或注册用户时也可写入初始用户配置,字段统一为 user_config。在创建账号场景中,user_config 作用于 admin_user_id 指定的首个管理员用户。

创建账号并初始化首个管理员用户配置:

curl -X POST http://localhost:1933/api/v1/admin/accounts \
  -H "Content-Type: application/json" \
  -H "X-API-Key: <root-key>" \
  -d '{
    "account_id": "acme-private",
    "admin_user_id": "alice",
    "user_config": {
      "add_targets": {
        "resource_uri": "viking://user/resources",
        "skill_uri": "viking://user/skills"
      }
    }
  }'

在已有账号下注册用户并初始化用户配置:

curl -X POST http://localhost:1933/api/v1/admin/accounts/acme/users \
  -H "Content-Type: application/json" \
  -H "X-API-Key: <root-or-admin-key>" \
  -d '{
    "user_id": "bob",
    "role": "user",
    "user_config": {
      "add_targets": {
        "resource_uri": "viking://user/resources/project-a"
      }
    }
  }'

对应 CLI 示例:

ov --sudo admin create-account acme-private --admin alice \
  --user-config-json '{"add_targets":{"resource_uri":"viking://user/resources","skill_uri":"viking://user/skills"}}'

ov admin register-user acme bob --role user \
  --user-config-json '{"add_targets":{"resource_uri":"viking://user/resources/project-a"}}'

Python SDK 示例:

client.admin_register_user(
    "acme",
    "bob",
    role="user",
    user_config={"add_targets": {"resource_uri": "viking://user/resources/project-a"}},
)

Go SDK 示例:

_, err := client.AdminRegisterUserWithOptions(ctx, "acme", "bob", "user", &openviking.AdminRegisterUserOptions{
    UserConfig: map[string]any{
        "add_targets": map[string]any{"resource_uri": "viking://user/resources/project-a"},
    },
})

Related Issue

无。

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)
  • Performance improvement
  • Test update

Changes Made

  • 新增 server.user_config_defaults.add_targets 配置模型、运行时校验和服务端默认目标解析逻辑。
  • 新增用户设置 HTTP API:GET/PATCH/DELETE /api/v1/user-settings/add-locations,并将用户配置存储为 viking://user/{user_id}/settings/user_config.json
  • ResourceService.add_resource / add_skill 接入默认目标解析,CLI、SDK、MCP、Web 等调用方省略目标时自动受益。
  • 管理端创建账号/注册用户支持初始用户配置:HTTP 请求体、Python SDK、Go SDK options 方法、CLI JSON 参数均已覆盖,字段/参数统一命名为 user_config
  • 更新中英文配置/API/SDK 文档,补充配置格式、优先级规则、URI 约束和使用示例。

Testing

  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have tested this on the following platforms:
    • Linux
    • macOS
    • Windows

已执行:

ruff check openviking/server/user_config.py openviking/server/routers/admin.py openviking/server/routers/user_settings.py openviking/service/resource_service.py tests/server/test_add_locations.py tests/server/test_admin_api.py
ruff check openviking/server/routers/admin.py tests/server/test_admin_api.py sdk/python/openviking_sdk/client.py sdk/python/tests/test_async_client_behaviors.py
pytest tests/server/test_add_locations.py tests/server/test_admin_api.py::test_create_user_paths_accept_initial_user_config -q
pytest tests/server/test_admin_api.py::test_create_user_paths_accept_initial_user_config -q
PYTHONPATH=sdk/python pytest sdk/python/tests/test_async_client_behaviors.py -q
PYTHONPATH=sdk/python pytest sdk/python/tests/test_async_client_behaviors.py::test_admin_create_paths_accept_initial_user_config sdk/python/tests/test_async_client_behaviors.py::test_search_uses_session_wrapper_session_id_in_payload -q
cargo test -p ov_cli cli_parses_search_context_type
GOCACHE=/private/tmp/openviking-go-cache go test ./...
git diff --check

Go 测试在本地需要 loopback 监听权限,已在提升权限后通过。

Checklist

  • My code follows the project's coding style
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

Screenshots (if applicable)

不适用。

Additional Notes

本 PR 没有新增独立的 CLI/SDK “设置 add locations”接口;CLI/SDK 只在创建/注册用户路径支持初始化用户配置。普通 add 调用继续通过服务端解析默认目标,避免客户端各自保存默认值造成语义分叉。

Allow deployments and per-user settings to provide default add targets while keeping explicit request targets authoritative.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

1 participant