"""Offline Obsidian verification lab for Chapter 11 (claude-code)."""
import difflib
import json
import re
import tempfile
from pathlib import Path
VERSION = "CPython 3.12.14 offline Obsidian Vault/Markdown semantic verifier"
def case_cli_install_and_auth_commands():
install_cmd = "npm install -g @anthropic-ai/claude-code"
auth_cmd = "claude"
pkg_match = re.search(r"npm install -g (@[\w-]+/[\w-]+)", install_cmd)
alt_provider_env = {
"ANTHROPIC_BASE_URL": "https://api.example.invalid/v1",
"ANTHROPIC_MODEL": "custom-compatible-model",
}
expected = {
"npm_package": "@anthropic-ai/claude-code",
"auth_binary": "claude",
"supports_custom_api_env": True,
}
actual = {
"npm_package": pkg_match.group(1) if pkg_match else "",
"auth_binary": auth_cmd.strip(),
"supports_custom_api_env": bool(alt_provider_env["ANTHROPIC_BASE_URL"] and alt_provider_env["ANTHROPIC_MODEL"]),
}
return {
"name": "cli_install_and_auth_commands",
"summary": "校验 Claude Code 全局 npm 包安装命令、首次 OAuth 授权入口以及兼容模型环境变量配置",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_claudian_plugin_bundle_layout():
with tempfile.TemporaryDirectory() as tmp:
vault = Path(tmp) / "MyVault"
plugin_dir = vault / ".obsidian" / "plugins" / "claudian"
plugin_dir.mkdir(parents=True)
manifest = {
"id": "claudian",
"name": "Claudian",
"version": "1.0.0",
"minAppVersion": "1.5.0",
"description": "Embed Claude Code into Obsidian",
}
(plugin_dir / "main.js").write_text("module.exports = {};\n", encoding="utf-8")
(plugin_dir / "manifest.json").write_text(json.dumps(manifest, ensure_ascii=False), encoding="utf-8")
(plugin_dir / "styles.css").write_text(".claudian-chat { display: flex; }\n", encoding="utf-8")
(vault / ".obsidian" / "community-plugins.json").write_text(json.dumps(["claudian"]), encoding="utf-8")
bundle_files = sorted(p.name for p in plugin_dir.iterdir() if p.is_file())
parsed_manifest = json.loads((plugin_dir / "manifest.json").read_text(encoding="utf-8"))
enabled = json.loads((vault / ".obsidian" / "community-plugins.json").read_text(encoding="utf-8"))
expected = {
"bundle_files": ["main.js", "manifest.json", "styles.css"],
"manifest_id": "claudian",
"enabled_in_registry": True,
"correct_relative_dir": ".obsidian/plugins/claudian",
}
actual = {
"bundle_files": bundle_files,
"manifest_id": parsed_manifest["id"],
"enabled_in_registry": "claudian" in enabled,
"correct_relative_dir": ".obsidian/plugins/claudian",
}
return {
"name": "claudian_plugin_bundle_layout",
"summary": "在临时仓库验证 .obsidian/plugins/claudian/ 三件套文件结构与 community-plugins.json 启用登记",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_chat_prompt_and_inline_diff():
prompt = "帮我写一篇关于「2026年最值得关注的5个AI编程工具」的文献笔记,格式用我 vault 里最常用的文献笔记模板,从今天更新的网页抓取信息"
slash_triggers = ["/", "$"]
before_tokens = ["本地", "笔记", "工具", "支持", "双链"]
after_tokens = ["本地", "Markdown", "笔记", "工具", "支持", "双链"]
added_words = [
tok[2:] for tok in difflib.ndiff(before_tokens, after_tokens)
if tok.startswith("+ ")
]
expected = {
"prompt_mentions_template": True,
"command_triggers": ["/", "$"],
"inline_added_words": ["Markdown"],
"open_chat_command": "Claudian: Open Chat",
}
actual = {
"prompt_mentions_template": "文献笔记模板" in prompt,
"command_triggers": slash_triggers,
"inline_added_words": added_words,
"open_chat_command": "Claudian: Open Chat",
}
return {
"name": "chat_prompt_and_inline_diff",
"summary": "解析文献笔记生成指令、斜杠技能触发符以及行内编辑的词级差异预览结果",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_agent_mentions_plan_and_mcp():
chat_input = "@notes/AI综述.md @mcp:filesystem #保持精简中文输出"
mentions = re.findall(r"@([\w:./\u4e00-\u9fff-]+)", chat_input)
instructions = re.findall(r"#([\w\u4e00-\u9fff-]+)", chat_input)
mcp_transports = ["stdio", "sse", "http"]
plan_mode_flow = ["explore_design", "present_plan_for_approval", "execute_implementation"]
expected = {
"mentions": ["notes/AI综述.md", "mcp:filesystem"],
"instruction_directives": ["保持精简中文输出"],
"mcp_transports": ["stdio", "sse", "http"],
"plan_mode_steps": 3,
}
actual = {
"mentions": mentions,
"instruction_directives": instructions,
"mcp_transports": mcp_transports,
"plan_mode_steps": len(plan_mode_flow),
}
return {
"name": "agent_mentions_plan_and_mcp",
"summary": "校验 @ 提及上下文、# 指令模式、Shift+Tab 规划模式阶段及 MCP 三类传输协议",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_cli_install_and_auth_commands(),
case_claudian_plugin_bundle_layout(),
case_chat_prompt_and_inline_diff(),
case_agent_mentions_plan_and_mcp(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 Obsidian Vault/Markdown/JSON 语义验证器(未启动桌面端 Electron GUI)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 Obsidian Vault/Markdown/JSON 语义验证器(未启动桌面端 Electron GUI)。
入口文件:projects/obsidian-claude-code-lab/explore-claude-code.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-claude-code.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Obsidian Vault/Markdown/JSON 语义验证器(未启动桌面端 Electron GUI)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联12章) · 含当前章节或实例 · mbskycom202612a2227.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2229.zip