Files
lightcore-cli/lightcore_cli/media_schema.py
T
Shiny 2d4659291d feat: 同步 Media CLI 对标素材与文件管理更新
同步 LightCore d14de92a4 中的 CLI 变更。
2026-11-22 17:06:49 +08:00

345 lines
16 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"),
"priority": ("hard_deadline", "blocks_followup", "scheduled", "when_available"),
"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": "搜索关键词",
"videoFileUrl": "采集得到的视频文件地址,省略时保留现有地址。",
"audioFileUrl": "采集得到的音频文件地址,省略时保留现有地址。",
"excludeKeyword": "排除关键词,多个词用空格分隔",
"searchScope": "搜索范围,all 为标题和正文,默认 all",
"matchMode": "多词匹配方式,all 为全部命中,默认 all",
"isValid": "社媒账号是否有效",
"priority": "备忘优先级。hard_deadline 需要截止时间。",
"assessment": "人工选题评估。high / medium / low 对应 A / B / C。",
"referencePoints": "本次保存的全部参考点。空数组清空。",
"topicNotes": "人工填写的选题备注。空字符串清空。",
"forTopicSelection": "只返回 A、B 和评估完整的 C 档,按评估等级排序。",
"relatedMemoIds": "关联的 Media 备忘 ID。更新时省略保留,空数组清空。",
"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 存储客户端密钥,使用私有输入文件。",
"folderId": "文件夹 ID,0 表示根目录。",
"dueAt": "截止时刻,使用带时区的 RFC3339。空字符串清除。",
"remindAt": "具体提醒时刻,使用带时区的 RFC3339。空字符串清除提醒及按天设置。",
"remindDaysBefore": "按北京时间在截止前 N 天提醒,和 remindAt 二选一。",
"textContent": "文件的纯文本内容,例如 OCR 结果。空字符串清除。",
}
ARRAY_OBJECTS = {
"transcriptSegments": ("start_ms end_ms text", "start_ms end_ms text"),
"files": ("fileId expectedUpdatedAt", "fileId expectedUpdatedAt"),
"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"},
"saveDocument": {"create": "requestId title", "update": "requestId documentId expectedUpdatedAt"},
"saveMemo": {"create": "requestId title", "update": "requestId memoId expectedUpdatedAt"},
"saveFolder": {"create": "requestId name", "update": "requestId folderId 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",
"saveReferenceAssessment": "requestId referenceId expectedUpdatedAt assessment",
"saveReference": "requestId platform platformContentId sourceUrl contentType collectedAt creator",
"saveReferenceTranscript": "requestId referenceId expectedUpdatedAt transcriptStatus",
"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",
"saveFile": "requestId fileId expectedUpdatedAt",
"moveFiles": "requestId folderId files",
}
EXAMPLE_FIELDS = {
"saveAccount": "brandName",
"saveContent": "title body contentFormat mediaFileIds researchSources researchFacts imageDetails",
"saveReference": "description viewCount",
"saveReferenceTranscript": "transcriptText transcriptSegments transcriptModel transcribedAt",
"saveReferenceAssessment": "referencePoints topicNotes",
"saveDocument": "title bodyMarkdown fileIds folderId",
"saveProject": "name description documents",
"saveEpisode": "name sortOrder",
"saveResource": "name imageFileIds",
"saveVideo": "name mode durationSeconds body referenceImages",
"saveImageDetail": "text prompt referenceFileIds",
"saveMemo": "dueAt remindDaysBefore priority",
"saveFile": "description textContent",
"saveFolder": "parentId",
}
UPDATE_EXAMPLE_FIELDS = {
"saveAccount": "validityNote",
"saveContent": "title",
"saveDocument": "bodyMarkdown folderId",
"saveProject": "description",
"saveEpisode": "sortOrder",
"saveResource": "prompt",
"saveVideo": "body",
"saveMemo": "status relatedMemoIds fileIds",
"saveFolder": "name",
}
SAMPLE_VALUES = {
"profileUrl": "https://www.tiktok.com/@example",
"platformContentId": "7710240323204631829",
"platformCreatorId": "7556875931332969479",
"referenceId": 1,
"topicNotes": "适合护理选题,可参考开头的提问。",
"sourceUrl": "https://www.tiktok.com/@example/video/7710240323204631829",
"collectedAt": "2026-11-21T00:37:00+08:00",
"transcribedAt": "2026-11-21T01:00:00+08:00",
"transcriptStatus": "ready",
"transcriptText": "示例转写内容。",
"transcriptModel": "provider/model",
"start_ms": 0,
"end_ms": 1000,
"accountKey": "example-media-account",
"username": "example-admin",
"bodyMarkdown": "示例正文",
"bucket": "media",
"contentId": 58,
"documentId": 7,
"episodeId": 3,
"expectedUpdatedAt": "2026-01-01T00:00:00Z",
"dueAt": "2026-12-23T00:00:00+08:00",
"remindDaysBefore": 7,
"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 == "platform" and endpoint in ("getReferenceList", "saveReference"):
return ("tiktok", "douyin", "xiaohongshu")
if field == "transcriptStatus":
states = ("pending", "ready", "no_audio", "no_speech", "failed")
return states if endpoint == "saveReferenceTranscript" else (*states, "not_applicable")
if field == "assessment":
return ("high", "medium", "low", "excluded", "unrated")
if field == "sort" and endpoint == "getReferenceList":
return ("updated", "views", "favorites", "published", "assessment")
if field == "type":
if endpoint in ("getResourceList", "saveResource"):
return RESOURCE_TYPES
if endpoint in ("getFileList", "searchFiles"):
return FILE_TYPES
if field == "status" and endpoint in ("getMemoList", "saveMemo"):
return ("open", "done")
return ENUMS.get(field)
def field_schema(endpoint, field):
values = enum_for(endpoint, field)
if values:
result = {"type": "string", "enum": list(values)}
elif field == "creator" and endpoint == "saveReference":
result = object_schema(
endpoint,
"platformCreatorId handle displayName profileUrl bio followerCount totalLikesCount collectedAt".split(),
["platformCreatorId", "collectedAt"],
)
elif field in ("platformCreatorId", "platformContentId"):
result = {"type": "string", "minLength": 1}
elif field.endswith("Count") or field in ("start_ms", "end_ms"):
result = {"type": "integer", "minimum": 0}
elif field == "durationSeconds" and endpoint == "saveReference":
result = {"type": "integer", "minimum": 0, "maximum": 2147483647}
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 == "referencePoints":
result = {
"type": "array",
"items": {"type": "string", "enum": ["topic_angle", "opening", "structure", "presentation"]},
"uniqueItems": True,
}
elif field == "sourceKeys":
result = {"type": "array", "items": {"type": "string"}}
elif field in ("required", "isValid", "forTopicSelection"):
result = {"type": "boolean"}
elif field in ("id", "pageNo", "pageSize", "sortOrder", "durationSeconds", "remindDaysBefore") 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 in ("sortOrder", "folderId", "parentId", "remindDaysBefore") else 1
else:
result = {"type": "string"}
if field in ("expectedUpdatedAt", "publishedAt", "publishedFrom", "publishedTo", "collectedAt", "transcribedAt"):
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 endpoint == "saveReference" and field in ("publishedAt", "durationSeconds"):
result.pop("description", None)
if field in ("name", "title", "fileName") and endpoint != "saveReference":
result.update(minLength=1, maxLength=255 if endpoint in ("saveFolder", "saveFile") else 240)
if field in ("accountKey", "accountName", "creatorName"):
result.update(minLength=1, maxLength=160)
if field in ("username", "displayName") and endpoint != "saveReference":
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 == "saveReferenceTranscript":
result["if"] = {"properties": {"transcriptStatus": {"const": "ready"}}}
result["then"] = {"required": ["transcriptText", "transcribedAt"]}
if endpoint == "saveReferenceAssessment":
result["if"] = {"properties": {"assessment": {"const": "low"}}}
result["then"] = {
"required": ["referencePoints", "topicNotes"],
"properties": {"referencePoints": {"minItems": 1}, "topicNotes": {"pattern": r"\S"}},
}
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}
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()))
result = {field: example_value(field, schema["properties"][field]) for field in fields}
if endpoint == "saveReference":
result["contentType"] = "video"
if endpoint == "saveMemo" and mode == "update":
result["status"] = "done"
return result