MCPcopy Create free account
hub / github.com/Louisym/MiniCC / ContentBlock

Class ContentBlock

tutorials/12_session_persistence.py:73–135  ·  view source on GitHub ↗

消息内容块。 对应源码: session.rs:18-33 pub enum ContentBlock { Text { text: String }, ToolUse { id, name, input }, ToolResult { tool_use_id, tool_name, output, is_error }, }

Source from the content-addressed store, hash-verified

71
72@dataclass
73class ContentBlock:
74 """
75 消息内容块。
76
77 对应源码: session.rs:18-33
78 pub enum ContentBlock {
79 Text { text: String },
80 ToolUse { id, name, input },
81 ToolResult { tool_use_id, tool_name, output, is_error },
82 }
83 """
84 block_type: str # "text", "tool_use", "tool_result"
85 text: str = ""
86 tool_id: str = ""
87 tool_name: str = ""
88 tool_input: str = ""
89 tool_use_id: str = ""
90 output: str = ""
91 is_error: bool = False
92
93 def to_dict(self) -> dict:
94 """序列化为字典(用于 JSON)"""
95 if self.block_type == "text":
96 return {"type": "text", "text": self.text}
97 elif self.block_type == "tool_use":
98 return {
99 "type": "tool_use",
100 "id": self.tool_id,
101 "name": self.tool_name,
102 "input": self.tool_input,
103 }
104 elif self.block_type == "tool_result":
105 return {
106 "type": "tool_result",
107 "tool_use_id": self.tool_use_id,
108 "tool_name": self.tool_name,
109 "output": self.output,
110 "is_error": self.is_error,
111 }
112 return {}
113
114 @staticmethod
115 def from_dict(data: dict) -> "ContentBlock":
116 """从字典反序列化"""
117 block_type = data["type"]
118 if block_type == "text":
119 return ContentBlock(block_type="text", text=data["text"])
120 elif block_type == "tool_use":
121 return ContentBlock(
122 block_type="tool_use",
123 tool_id=data["id"],
124 tool_name=data["name"],
125 tool_input=data["input"],
126 )
127 elif block_type == "tool_result":
128 return ContentBlock(
129 block_type="tool_result",
130 tool_use_id=data["tool_use_id"],

Callers 4

from_dictMethod · 0.70
user_textMethod · 0.70
assistant_textMethod · 0.70
mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected