MCPcopy
hub / github.com/MAA1999/M9A / parse_params

Function parse_params

agent/utils/params.py:6–34  ·  view source on GitHub ↗

解析 custom_action_param / custom_recognition_param JSON 字符串。 Args: raw: 原始 JSON 字符串,可为 None 或空串 required_keys: 必须存在的字段名 Returns: 解析后的 dict(raw 为空时返回空 dict) Raises: ValueError: JSON 格式错误、非对象类型、或缺少必填字段

(raw: str | None, *required_keys: str)

Source from the content-addressed store, hash-verified

4
5
6def parse_params(raw: str | None, *required_keys: str) -> dict:
7 """
8 解析 custom_action_param / custom_recognition_param JSON 字符串。
9
10 Args:
11 raw: 原始 JSON 字符串,可为 None 或空串
12 required_keys: 必须存在的字段名
13
14 Returns:
15 解析后的 dict(raw 为空时返回空 dict)
16
17 Raises:
18 ValueError: JSON 格式错误、非对象类型、或缺少必填字段
19 """
20 if not raw:
21 if required_keys:
22 raise ValueError(f"参数为空,需要字段: {list(required_keys)}")
23 return {}
24 try:
25 params = json.loads(raw)
26 except json.JSONDecodeError as e:
27 raise ValueError(f"JSON解析失败: {e}") from e
28 if not isinstance(params, dict):
29 raise ValueError(f"参数必须是对象,得到: {type(params).__name__}")
30 if required_keys:
31 missing = [k for k in required_keys if k not in params]
32 if missing:
33 raise ValueError(f"缺少必填字段: {missing}")
34 return params
35
36
37def coerce_like(value, default, key: str):

Callers 15

analyzeMethod · 0.90
analyzeMethod · 0.90
analyzeMethod · 0.90
analyzeMethod · 0.90
analyzeMethod · 0.90
analyzeMethod · 0.90
analyzeMethod · 0.90
analyzeMethod · 0.90
analyzeMethod · 0.90
analyzeMethod · 0.90
analyzeMethod · 0.90
analyzeMethod · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected