Files
lightcore-cli/lightcore_cli/media_schema.py
T
Shiny 3eb720cfbd feat: 新增 lightcore-cli 命令行工具项目
新增完整的 lightcore-cli 命令行工具代码,包含:
- CLI 入口与参数解析
- System 和 Media 应用的客户端实现
- 丰富的子命令与操作接口
- 测试用例与项目配置
- .gitignore 与 README 文档
2026-11-20 01:35:46 +08:00

263 lines
11 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Request descriptions shared by Media help, schemas and examples."""
from uuid import uuid4
ENUMS = {
"platform": ("xiaohongshu", "douyin", "wechat_video", "bilibili"),
"contentType": ("image_text", "video"),
"contentFormat": ("plain_text", "html"),
"searchScope": ("all", "title", "body"),
"matchMode": ("all", "any"),
"role": ("owner", "admin"),
"status": ("enabled", "disabled"),
"mode": ("omni_reference", "first_last_frame"),
"frameRole": ("reference", "first", "last"),
}
RESOURCE_TYPES = ("character", "scene", "prop")
FILE_TYPES = ("image", "video", "document", "audio", "archive", "other")
DESCRIPTIONS = {
"requestId": "本次写入的 UUID。核实后重试同一输入时复用原值。",
"expectedUpdatedAt": "刚从详情读取的 updatedAt,保留原有时间精度。",
"publishedAt": "人工发布的实际时间,使用带时区的 RFC3339。",
"pageNo": "页码,从 1 开始",
"pageSize": "每页条数,范围 1–100",
"keyword": "搜索关键词",
"excludeKeyword": "排除关键词,多个词用空格分隔",
"searchScope": "搜索范围,all 为标题和正文,默认 all",
"matchMode": "多词匹配方式,all 为全部命中,默认 all",
"isValid": "社媒账号是否有效",
"bodyMarkdown": "Markdown 正文。",
"durationSeconds": "生成时长,1–3600 秒,新建默认 10 秒。",
"referenceImages": "参考图数组。修改时提交希望保留的完整数组。",
"mode": "视频制作模式。first_last_frame 要求参考图中各有一项 first 和 last。",
"frameRole": "参考图用途,省略时为 reference。",
"fileIds": "有序的 Media 文件 ID 数组。",
"imageDetails": "按稿件图片记录文案和提示词。fileId 必须属于当前稿件的封面、正文素材列表或正文引用。",
"researchFacts": "已核验事实,可引用 researchSources 中的 key。",
"researchSources": "稿件来源条目。",
"sourceKeys": "对应当前稿件 researchSources 内的 key。",
"documents": "项目文档关联数组。required 指定是否为本项目必读。",
"clientSecret": "Media 存储客户端密钥,使用私有输入文件。",
}
ARRAY_OBJECTS = {
"referenceImages": ("key name purpose prompt resourceIds referenceFileIds fileId frameRole", "name"),
"documents": ("documentId required", "documentId"),
"imageDetails": ("fileId text prompt referenceFileIds notes", "fileId"),
"researchFacts": ("text scope sourceKeys verifiedAt", "text"),
"researchSources": ("key title url fileIds notes", "key title"),
}
# New and existing records share a backend DTO. The rules below describe the
# conditions enforced by the application service and used by the examples.
CASES = {
"saveAccount": {
"create": "requestId accountKey platform accountName isValid",
"update": "requestId accountKey platform accountName isValid expectedUpdatedAt",
},
"saveContent": {"create": "requestId contentType title", "update": "requestId contentId expectedUpdatedAt"},
"saveReference": {
"create": "requestId platform contentType creatorName title",
"update": "requestId referenceId platform contentType creatorName title expectedUpdatedAt",
},
"saveDocument": {"create": "requestId title", "update": "requestId documentId expectedUpdatedAt"},
"saveProject": {"create": "requestId name", "update": "requestId projectId expectedUpdatedAt"},
"saveEpisode": {"create": "requestId projectId name", "update": "requestId episodeId expectedUpdatedAt"},
"saveResource": {"create": "requestId projectId name type", "update": "requestId resourceId expectedUpdatedAt"},
"saveVideo": {"create": "requestId episodeId name", "update": "requestId videoId expectedUpdatedAt"},
}
REQUIRED = {
"changePassword": "oldPassword newPassword",
"create": "username password displayName",
"resetPassword": "id password",
"setAccountValidity": "accountKey requestId isValid expectedUpdatedAt",
"saveImageDetail": "contentId requestId expectedUpdatedAt fileId",
"deleteReference": "referenceId requestId expectedUpdatedAt",
"markPublication": "requestId contentId accountKey publishedAt",
"deleteDocument": "documentId requestId expectedUpdatedAt",
"deleteProject": "requestId id expectedUpdatedAt",
"deleteEpisode": "requestId id expectedUpdatedAt",
"deleteResource": "requestId id expectedUpdatedAt",
"deleteVideo": "requestId id expectedUpdatedAt",
"saveConfig": "bucket clientSecret",
}
EXAMPLE_FIELDS = {
"saveAccount": "brandName",
"saveContent": "title body contentFormat mediaFileIds researchSources researchFacts imageDetails",
"saveReference": "title bodyMarkdown",
"saveDocument": "title bodyMarkdown fileIds",
"saveProject": "name description documents",
"saveEpisode": "name sortOrder",
"saveResource": "name imageFileIds",
"saveVideo": "name mode durationSeconds body referenceImages",
"saveImageDetail": "text prompt referenceFileIds",
}
UPDATE_EXAMPLE_FIELDS = {
"saveAccount": "validityNote",
"saveContent": "title",
"saveReference": "bodyMarkdown",
"saveDocument": "bodyMarkdown",
"saveProject": "description",
"saveEpisode": "sortOrder",
"saveResource": "prompt",
"saveVideo": "body",
}
SAMPLE_VALUES = {
"accountKey": "example-media-account",
"username": "example-admin",
"bodyMarkdown": "示例正文",
"bucket": "media",
"contentId": 58,
"documentId": 7,
"episodeId": 3,
"expectedUpdatedAt": "2026-01-01T00:00:00Z",
"fileId": 123,
"frameRole": "reference",
"key": "example-source",
"publishedAt": "2026-01-01T12:00:00+08:00",
"sourceKeys": ["example-source"],
"resourceIds": [],
"referenceFileIds": [],
"verifiedAt": "2026-01-01",
"durationSeconds": 10,
"mode": "omni_reference",
"password": "REPLACE_WITH_PASSWORD",
"oldPassword": "REPLACE_WITH_OLD_PASSWORD",
"newPassword": "REPLACE_WITH_NEW_PASSWORD",
"clientSecret": "REPLACE_WITH_CLIENT_SECRET",
}
def enum_for(endpoint, field):
if field == "type":
if endpoint in ("getResourceList", "saveResource"):
return RESOURCE_TYPES
if endpoint == "getFileList":
return FILE_TYPES
return ENUMS.get(field)
def field_schema(endpoint, field):
values = enum_for(endpoint, field)
if values:
result = {"type": "string", "enum": list(values)}
elif field in ARRAY_OBJECTS:
fields, required = ARRAY_OBJECTS[field]
items = object_schema(endpoint, fields.split(), required.split())
if field == "referenceImages":
items["properties"]["key"].update(format="uuid", description="参考图 UUID,新增时可省略,编辑已有参考图时保留原值。")
items["properties"]["fileId"]["minimum"] = 0
result = {"type": "array", "items": items}
elif field.endswith("Ids"):
result = {"type": "array", "items": {"type": "integer", "minimum": 1}}
elif field == "sourceKeys":
result = {"type": "array", "items": {"type": "string"}}
elif field in ("required", "isValid"):
result = {"type": "boolean"}
elif field in ("id", "pageNo", "pageSize", "sortOrder", "durationSeconds") or field.endswith("Id") and field != "requestId":
result = {"type": "integer"}
if field == "pageSize":
result.update(minimum=1, maximum=100, default=20)
elif field == "pageNo":
result.update(minimum=1, default=1)
elif field == "durationSeconds":
result.update(minimum=1, maximum=3600, default=10)
else:
result["minimum"] = 0 if field == "sortOrder" else 1
else:
result = {"type": "string"}
if field in ("expectedUpdatedAt", "publishedAt"):
result["format"] = "date-time"
elif field == "verifiedAt":
result["format"] = "date"
elif field == "requestId":
result["format"] = "uuid"
elif field in ("password", "oldPassword", "newPassword", "clientSecret"):
result["writeOnly"] = True
if field in DESCRIPTIONS:
result["description"] = DESCRIPTIONS[field]
if field == "name" or field == "title":
result.update(minLength=1, maxLength=240)
if field in ("accountKey", "accountName", "creatorName"):
result.update(minLength=1, maxLength=160)
if field in ("username", "displayName"):
result.update(minLength=3 if field == "username" else 1, maxLength=50)
if field in ("newPassword", "password"):
result["minLength"] = 8
return result
def object_schema(endpoint, fields, required):
return {
"type": "object",
"properties": {field: field_schema(endpoint, field) for field in fields},
"required": required,
}
def request_schema(endpoint, fields):
names = [field.removesuffix("*") for field in fields]
required = REQUIRED.get(endpoint)
result = object_schema(endpoint, names, required.split() if required else [
field[:-1] for field in fields if field.endswith("*")
])
if endpoint in CASES:
case_fields = {mode: values.split() for mode, values in CASES[endpoint].items()}
create, update = case_fields["create"], case_fields["update"]
result["required"] = [field for field in create if field in update]
if endpoint == "saveAccount":
result["description"] = "服务端通过 accountKey 查找账号。更新时传 expectedUpdatedAt,同时提交希望保留的账号资料。"
else:
selector = update[1]
result["properties"][selector].update(minimum=0, description="省略或填 0 表示新建,正整数表示更新。")
result["if"] = {"required": [selector], "properties": {selector: {"minimum": 1}}}
result["then"] = {"required": update}
result["else"] = {"required": create}
if endpoint == "saveReference":
result["description"] = "更新时提交希望保留的完整参考内容和附件字段。"
else:
result["description"] = "更新时省略不打算修改的字段。传入的数组按整组保存。"
result["x-required-by-operation"] = case_fields
return result
def example_value(name, schema):
if name == "requestId":
return str(uuid4())
if name in SAMPLE_VALUES:
return SAMPLE_VALUES[name]
if "enum" in schema:
return schema["enum"][0]
if schema["type"] == "object":
is_reference_image = "frameRole" in schema["properties"]
sample = {}
for field, spec in schema["properties"].items():
if is_reference_image and field == "key":
continue
sample[field] = example_value(field, spec)
if is_reference_image:
sample["fileId"] = 0
return sample
if schema["type"] == "array":
return [example_value(name, schema["items"])]
if schema["type"] == "integer":
return 123 if name.endswith("Ids") else 1
if schema["type"] == "boolean":
return True
if name == "url":
return "https://example.com/source"
return "示例"
def example_body(endpoint, schema, mode):
required = schema.get("x-required-by-operation", {}).get(mode, schema["required"])
example_fields = UPDATE_EXAMPLE_FIELDS if mode == "update" else EXAMPLE_FIELDS
fields = dict.fromkeys((*required, *example_fields.get(endpoint, "").split()))
return {field: example_value(field, schema["properties"][field]) for field in fields}