Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

fix(skill): replace complete multiline configuration values

Rudy Celeklic184fccd0562
diff --git a/docs/skill/mpt_agent.py b/docs/skill/mpt_agent.py
index 30d58a3d..6b9deacc 100644
--- a/docs/skill/mpt_agent.py
+++ b/docs/skill/mpt_agent.py
@@ -201,10 +201,22 @@ def _plain_config_value(text: str, key: str) -> str:
def _replace_config_value(text: str, key: str, value: object) -> str:
"""Replace one active field while preserving the configuration layout."""
pattern = re.compile(rf"(?m)^({re.escape(key)}\s*=\s*).*$")
- if not pattern.search(text):
+ match = pattern.search(text)
+ if match is None:
raise SkillError(f"configuration field not found in config.toml: {key}")
+ value_start = match.start() + len(match.group(1))
+ value_end = match.end()
+ while True:
+ try:
+ tomllib.loads("replacement = " + text[value_start:value_end])
+ break
+ except tomllib.TOMLDecodeError as exc:
+ next_line = text.find("\n", value_end + 1)
+ if value_end == len(text):
+ raise SkillError("configuration field is not valid TOML") from exc
+ value_end = len(text) if next_line < 0 else next_line
encoded = json.dumps(value, ensure_ascii=False)
- return pattern.sub(lambda match: f"{match.group(1)}{encoded}", text, count=1)
+ return text[:value_start] + encoded + text[value_end:]
def _has_configured_value(value: str) -> bool:
diff --git a/test/services/test_skill_multiline_config_replacement.py b/test/services/test_skill_multiline_config_replacement.py
new file mode 100644
index 00000000..39fcd7b9
--- /dev/null
+++ b/test/services/test_skill_multiline_config_replacement.py
@@ -0,0 +1,21 @@
+import importlib.util
+import tomllib
+from pathlib import Path
+from unittest.mock import patch
+
+
+spec = importlib.util.spec_from_file_location("mpt_multiline_fixture", Path(__file__).parents[2] / "docs" / "skill" / "mpt_agent.py")
+helper = importlib.util.module_from_spec(spec)
+spec.loader.exec_module(helper)
+
+
+def test_environment_key_update_replaces_complete_multiline_array(tmp_path):
+ destination = tmp_path / "config.toml"
+ destination.write_text('[app]\nllm_provider = "ollama"\npexels_api_keys = [\n "old-fixture",\n]\n# Retained settings\nvideo_source = "pexels"\n', encoding="utf-8")
+ with patch.dict(helper.os.environ, {"MPT_PEXELS_API_KEY": "new-fixture"}, clear=True):
+ helper.apply_environment_config(destination)
+ text = destination.read_text(encoding="utf-8")
+ document = tomllib.loads(text)
+ assert document["app"]["pexels_api_keys"] == ["new-fixture"]
+ assert document["app"]["video_source"] == "pexels"
+ assert "# Retained settings" in text