帮你快速理解、总结文档立即下载

write_files

最近更新时间:2026-08-27 17:42:02
我的收藏

方法介绍

write_files 用于向 Skill 中添加或更新资源文件。功能说明如下:
增量写入:只影响传入的文件路径,不会删除已有的其他文件。
覆盖更新:若目标路径已存在文件,新内容会覆盖原文件。
版本递增:写入成功后 Skill 版本号自动递增。
编码支持:通过 encode_utf8encode_base64 构建文件数据。

导入

from tencentdb_agent_memory.v3 import SkillClient
from tencentdb_agent_memory.v3.skill_client import encode_utf8, encode_base64

方法签名

def write_files(
self,
skill_id: str,
expected_version: int,
*,
files: Iterable[Dict[str, Any]],
team_id: Optional[str] = None,
agent_id: Optional[str] = None,
user_id: Optional[str] = None,
task_id: Optional[str] = None,
) -> Dict[str, Any]

使用示例

from tencentdb_agent_memory.v3 import SkillClient

with SkillClient(
endpoint="https://memory.tdai.tencentyun.com",
api_key="sk-xxxxxxxxxxxxxxxx",
service_id="tdai-mem-xxxxxxxx",
team_id="team-abc123",
agent_id="agt-xyz789",
user_id="usr-456",
) as client:
files = [
client.encode_utf8("docs/guide.md", "# Guide\\n\\nUsage instructions..."),
client.encode_utf8("examples/demo.py", "print('hello')"),
]

result = client.write_files(
"skill-abc123", # 必填,Skill ID(位置参数)
5, # 必填,期望的当前版本号(位置参数)
files=files, # 必填,文件列表,通过 encode_utf8/encode_base64 构建
)
print(result["version"]) # 6

请求参数

参数名
类型
必填
描述说明
skill_id
str
Skill ID(位置参数)
expected_version
int
期望的当前版本号,用于乐观锁校验(位置参数)
files
List[Dict]
文件列表,通过 encode_utf8encode_base64 构建
team_id
str
团队 ID,覆盖构造时的默认值
agent_id
str
Agent ID,覆盖构造时的默认值
user_id
str
用户 ID,覆盖构造时的默认值
task_id
str
Task ID,覆盖构造时的默认值

响应示例

{
"skill_id": "skill-abc123",
"version": 6,
"files_written": ["docs/guide.md", "examples/demo.py"]
}

Running Environment

Operating System: Ubuntu 24.04.3 LTS / x86_64

Runtime Version: Python 3.11.1

响应参数说明

参数名
类型
参数含义
skill_id
str
Skill ID
version
int
更新后的版本号
files_written
List[str]
成功写入的文件路径列表

错误码

错误码
触发场景
处理建议
404
Skill 不存在
检查 skill_id 是否正确
409
版本冲突,expected_version 与当前版本不匹配
重新获取最新版本号后重试
400
files 为空
检查 files 参数
401
API Key 无效或过期
检查 api_key 配置
500
服务内部错误
可有限重试