mednotes-opencode 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.opencode/agents/med-chat-triager.md +204 -0
- package/.opencode/agents/med-flashcard-maker.md +63 -0
- package/.opencode/agents/med-knowledge-architect.md +230 -0
- package/.opencode/agents/med-link-graph-curator.md +177 -0
- package/.opencode/agents/med-publish-guard.md +62 -0
- package/.opencode/commands/flashcards.md +25 -0
- package/.opencode/commands/mednotes/create.md +25 -0
- package/.opencode/commands/mednotes/enrich.md +27 -0
- package/.opencode/commands/mednotes/fix-wiki.md +27 -0
- package/.opencode/commands/mednotes/history.md +22 -0
- package/.opencode/commands/mednotes/link-body.md +25 -0
- package/.opencode/commands/mednotes/link-related.md +27 -0
- package/.opencode/commands/mednotes/link.md +27 -0
- package/.opencode/commands/mednotes/pdf-library.md +27 -0
- package/.opencode/commands/mednotes/process-chats.md +23 -0
- package/.opencode/commands/mednotes/setup.md +21 -0
- package/.opencode/commands/mednotes/status.md +27 -0
- package/.opencode/commands/mednotes/telemetry.md +27 -0
- package/.opencode/commands/report.md +26 -0
- package/.opencode/mednotes/AGENTS.md +57 -0
- package/.opencode/mednotes/agents/med-chat-triager.md +197 -0
- package/.opencode/mednotes/agents/med-flashcard-maker.md +56 -0
- package/.opencode/mednotes/agents/med-knowledge-architect.md +224 -0
- package/.opencode/mednotes/agents/med-link-graph-curator.md +171 -0
- package/.opencode/mednotes/agents/med-publish-guard.md +55 -0
- package/.opencode/mednotes/contracts/.gitkeep +1 -0
- package/.opencode/mednotes/contracts/agents.json +116 -0
- package/.opencode/mednotes/contracts/opencode-plugin.json +70 -0
- package/.opencode/mednotes/docs/agent-prompt-hardening.md +567 -0
- package/.opencode/mednotes/docs/agent-role-contracts.md +94 -0
- package/.opencode/mednotes/docs/anki-mcp-twenty-rules.md +214 -0
- package/.opencode/mednotes/docs/anki-templates/README.md +39 -0
- package/.opencode/mednotes/docs/anki-templates/cloze.back.html +23 -0
- package/.opencode/mednotes/docs/anki-templates/cloze.front.html +14 -0
- package/.opencode/mednotes/docs/anki-templates/qa.back.html +24 -0
- package/.opencode/mednotes/docs/anki-templates/qa.front.html +14 -0
- package/.opencode/mednotes/docs/anki-templates/style.css +182 -0
- package/.opencode/mednotes/docs/atomicity-splitting-policy.md +113 -0
- package/.opencode/mednotes/docs/extension-docs.md +40 -0
- package/.opencode/mednotes/docs/flashcard-ingestion.md +278 -0
- package/.opencode/mednotes/docs/knowledge-architect.md +208 -0
- package/.opencode/mednotes/docs/merge-policy.md +110 -0
- package/.opencode/mednotes/docs/public-vocabulary.md +104 -0
- package/.opencode/mednotes/docs/semantic-linker.md +141 -0
- package/.opencode/mednotes/docs/taxonomy-policy.md +90 -0
- package/.opencode/mednotes/docs/triage-policy.md +187 -0
- package/.opencode/mednotes/docs/vault-version-control.md +758 -0
- package/.opencode/mednotes/docs/vocabulary-db-recovery.md +58 -0
- package/.opencode/mednotes/docs/workflow-output-contract.md +779 -0
- package/.opencode/mednotes/hooks/hooks.json +79 -0
- package/.opencode/mednotes/package-lock.json +6361 -0
- package/.opencode/mednotes/package.json +15 -0
- package/.opencode/mednotes/pyproject.toml +48 -0
- package/.opencode/mednotes/scripts/bootstrap_windows_python_uv.cmd +13 -0
- package/.opencode/mednotes/scripts/bootstrap_windows_python_uv.ps1 +172 -0
- package/.opencode/mednotes/scripts/enrich_notes.py +23 -0
- package/.opencode/mednotes/scripts/full_reset_windows_python_uv.cmd +13 -0
- package/.opencode/mednotes/scripts/hooks/antigravity_hook_status.mjs +212 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/adapters/antigravity.mjs +169 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/adapters/harness_payload.mjs +103 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/adapters/opencode_plugin.mjs +341 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/adapters/opencode_user_config_sync.mjs +177 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/anki_preflight.mjs +214 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/cli.mjs +143 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/diagnostics.mjs +11 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/domain/agent_directive_core.mjs +160 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/fsm_directive.mjs +1470 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/hook_errors.mjs +120 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/retention.mjs +114 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/runtime.mjs +174 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/telemetry_capture.mjs +511 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook/vault_guard.mjs +624 -0
- package/.opencode/mednotes/scripts/hooks/mednotes_hook.mjs +5 -0
- package/.opencode/mednotes/scripts/mednotes/_runtime_paths.py +24 -0
- package/.opencode/mednotes/scripts/mednotes/anki_model_validator.py +18 -0
- package/.opencode/mednotes/scripts/mednotes/capture_extension_diff.py +1562 -0
- package/.opencode/mednotes/scripts/mednotes/feedback_report.py +16 -0
- package/.opencode/mednotes/scripts/mednotes/flashcard_index.py +18 -0
- package/.opencode/mednotes/scripts/mednotes/flashcard_pipeline.py +18 -0
- package/.opencode/mednotes/scripts/mednotes/flashcard_report.py +18 -0
- package/.opencode/mednotes/scripts/mednotes/flashcard_sources.py +18 -0
- package/.opencode/mednotes/scripts/mednotes/obsidian/README.md +6 -0
- package/.opencode/mednotes/scripts/mednotes/obsidian_note_utils.py +20 -0
- package/.opencode/mednotes/scripts/mednotes/pdf_library/cli.py +16 -0
- package/.opencode/mednotes/scripts/mednotes/project_fsm.py +229 -0
- package/.opencode/mednotes/scripts/mednotes/setup_telemetry_email.py +404 -0
- package/.opencode/mednotes/scripts/mednotes/sync_anki_twenty_rules.py +18 -0
- package/.opencode/mednotes/scripts/mednotes/sync_opencode_user_config.py +36 -0
- package/.opencode/mednotes/scripts/mednotes/wiki/cli.py +20 -0
- package/.opencode/mednotes/scripts/mednotes/wiki_graph.py +18 -0
- package/.opencode/mednotes/scripts/mednotes/wiki_tree.py +134 -0
- package/.opencode/mednotes/scripts/reset_windows_python_uv.ps1 +625 -0
- package/.opencode/mednotes/scripts/run_python.mjs +109 -0
- package/.opencode/mednotes/scripts/vault/vault_commit.ps1 +19 -0
- package/.opencode/mednotes/scripts/vault/vault_commit.sh +18 -0
- package/.opencode/mednotes/scripts/vault/vault_git.ps1 +19 -0
- package/.opencode/mednotes/scripts/vault/vault_git.py +3107 -0
- package/.opencode/mednotes/scripts/vault/vault_git.sh +18 -0
- package/.opencode/mednotes/scripts/vault/vault_precommit.ps1 +19 -0
- package/.opencode/mednotes/scripts/vault/vault_precommit.sh +18 -0
- package/.opencode/mednotes/skills/THIRD_PARTY_NOTICES.md +45 -0
- package/.opencode/mednotes/skills/create-medical-flashcards/SKILL.md +113 -0
- package/.opencode/mednotes/skills/create-medical-note/SKILL.md +90 -0
- package/.opencode/mednotes/skills/enrich-medical-note/SKILL.md +120 -0
- package/.opencode/mednotes/skills/fix-medical-wiki/SKILL.md +559 -0
- package/.opencode/mednotes/skills/link-medical-wiki/SKILL.md +224 -0
- package/.opencode/mednotes/skills/obsidian-cli/SKILL.md +118 -0
- package/.opencode/mednotes/skills/obsidian-markdown/SKILL.md +207 -0
- package/.opencode/mednotes/skills/obsidian-markdown/references/CALLOUTS.md +58 -0
- package/.opencode/mednotes/skills/obsidian-markdown/references/EMBEDS.md +63 -0
- package/.opencode/mednotes/skills/obsidian-markdown/references/PROPERTIES.md +61 -0
- package/.opencode/mednotes/skills/obsidian-ops/SKILL.md +136 -0
- package/.opencode/mednotes/skills/pdf-library/SKILL.md +45 -0
- package/.opencode/mednotes/skills/process-medical-chats/SKILL.md +246 -0
- package/.opencode/mednotes/skills/workflow-report/SKILL.md +100 -0
- package/.opencode/mednotes/src/mednotes/__init__.py +5 -0
- package/.opencode/mednotes/src/mednotes/domains/__init__.py +5 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/README.md +26 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/__init__.py +2 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/build_demo_apkg.py +177 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/contracts.py +385 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/flashcards_machine.py +522 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/fsm.py +817 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/index.py +630 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/install_models.py +445 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/model.py +359 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/obsidian_links.py +135 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/obsidian_note_utils.py +546 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/pipeline.py +580 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/report.py +510 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/sources.py +682 -0
- package/.opencode/mednotes/src/mednotes/domains/flashcards/sync_rules.py +184 -0
- package/.opencode/mednotes/src/mednotes/domains/history/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/history/history_fsm.py +852 -0
- package/.opencode/mednotes/src/mednotes/domains/history/history_machine.py +453 -0
- package/.opencode/mednotes/src/mednotes/domains/setup/__init__.py +7 -0
- package/.opencode/mednotes/src/mednotes/domains/setup/setup_fsm.py +808 -0
- package/.opencode/mednotes/src/mednotes/domains/setup/setup_machine.py +973 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/README.md +64 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/api.py +668 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/batch_state.py +102 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/atomicity/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/atomicity/atomicity.py +877 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/body_link/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/body_link/body_linker.py +1562 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/effects/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/effects/effect_adapters.py +949 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/effects/fix_wiki_runtime_adapters.py +433 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/graph/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/graph/coverage.py +413 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/graph/graph.py +396 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/graph/graph_fixes.py +161 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/hygiene/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/hygiene/hygiene.py +483 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/__init__.py +2 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/anchors.py +185 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/core/__init__.py +0 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/core/cache.py +223 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/core/config.py +131 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/core/download.py +224 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/core/frontmatter.py +59 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/core/insert.py +227 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/core/local_import.py +54 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/sources/__init__.py +42 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/sources/web_profiles.py +99 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/sources/web_search.py +203 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/illustrate/sources/wikimedia.py +102 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/markdown/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/markdown/markdown_db_adapter.mjs +434 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/markdown/markdown_node_runtime.py +274 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/markdown/markdown_query.py +227 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/artifacts.py +605 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/canonical_merge.py +277 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/markdown_zones.py +85 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/meaning_planner.py +307 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_iter.py +67 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_merge.py +278 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_plan.py +409 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_policy.py +22 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_style/__init__.py +79 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_style/fixes.py +264 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_style/frontmatter.py +435 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_style/models.py +208 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_style/prompts.py +37 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_style/tables.py +236 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/note_style/validate.py +404 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/provenance.py +478 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/raw_chats.py +273 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/notes/sources_backfill.py +235 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/__init__.py +10 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/anchors.py +16 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/captions.py +47 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/cli.py +179 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/cloud.py +52 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/config.py +196 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/context_packets.py +76 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/db.py +81 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/doctor.py +102 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/figure_ids.py +42 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/ingest.py +326 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/insert.py +316 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/mentions.py +57 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/ocr.py +71 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/paths.py +35 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/pdf_engine.py +77 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/schema.py +155 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/search.py +188 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/tui/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/tui/app.py +89 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/tui/image_backend.py +29 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/pdf/tui/state.py +65 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/publish/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/publish/publish.py +1139 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/publish/publish_receipts.py +365 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/publish/publish_recovery.py +240 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/agent_behavior_corpus.py +2069 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/agent_report_validation.py +4448 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/agent_run_audit.py +852 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/architect_prompt_eval.py +341 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/body_linker_eval.py +240 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/curator_output_validation.py +175 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/curator_prompt_eval.py +865 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/quality/triager_prompt_eval.py +1295 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/related_notes/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/related_notes/related_notes.py +1920 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/related_notes/related_notes_headless.py +1186 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/specialist/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/specialist/plan_attestation.py +148 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/specialist/specialist_receipts.py +360 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/specialist/specialist_runtime.py +52 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/specialist/specialist_task_runner.py +2470 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/style/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/style/style.py +1952 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/subagents/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/subagents/agents.py +1767 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/alias_projection.py +331 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/link_terms.py +151 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/llm_disambiguation.py +182 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/taxonomy/__init__.py +116 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/taxonomy/audit.py +201 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/taxonomy/migration.py +314 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/taxonomy/normalize.py +72 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/taxonomy/policy.py +135 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/taxonomy/resolve.py +413 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/taxonomy/schema.py +157 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/taxonomy/status.py +137 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/vocabulary_bootstrap.py +509 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/vocabulary_curator_batch.py +1115 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/vocabulary_ingestion.py +632 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/vocabulary_map.py +930 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/capabilities/vocabulary/vocabulary_recovery.py +1388 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/cli.py +6665 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/common.py +69 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/config.py +210 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/__init__.py +74 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/agent_report.py +242 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/agent_run_audit.py +196 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/agents.py +601 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/curator.py +256 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/effect_payloads.py +519 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/happy_path.py +190 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/link_git.py +110 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/link_runtime_artifact.py +52 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/note_plan.py +75 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/paths.py +114 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/public_report.py +53 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/publish.py +111 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/raw_coverage.py +217 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/related_notes.py +136 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/related_notes_headless.py +153 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/related_notes_runtime.py +395 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/schema_registry.py +637 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/specialist.py +432 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/status.py +62 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/style_rewrite.py +568 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/vocabulary_ingestion.py +223 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/workflow_blockers.py +510 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/workflow_guardrails.py +637 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/workflow_outcomes.py +121 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/contracts/workflow_receipts.py +100 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/__main__.py +4 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/cli.py +275 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/__init__.py +2 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/candidates.py +193 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/cli.py +189 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/gemini.py +220 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/inputs.py +120 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/models.py +34 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/parsing.py +48 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/prompts.py +216 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/quality.py +54 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/reporting.py +24 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/runner.py +433 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/utils.py +39 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/enrich/workflow/vault_guard_bridge.py +17 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_context_packets.py +454 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_decision_projection.py +133 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_effects.py +1260 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_fsm.py +2768 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_machine.py +1588 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_plan.py +306 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_primary_objective.py +316 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_problem.py +153 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_receipt_evidence.py +306 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_states.py +290 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/fix_wiki_user_report.py +342 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/fix_wiki/health.py +6332 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/link_fsm.py +1119 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/link_git.py +638 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/link_machine.py +1106 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/link_retry_governance.py +374 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/link_runtime_result.py +485 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/link_triggers.py +183 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/linking.py +2758 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/reference_repair.py +718 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link/related_notes_fsm.py +1855 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link_related/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/link_related/link_related_machine.py +834 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/process_chats/__init__.py +1 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/process_chats/process_chats_fsm.py +1592 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/process_chats/process_chats_machine.py +3097 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/process_chats/process_chats_primary_objective.py +28 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/flows/process_chats/process_chats_runtime_result.py +185 -0
- package/.opencode/mednotes/src/mednotes/domains/wiki/performance.py +97 -0
- package/.opencode/mednotes/src/mednotes/kernel/__init__.py +6 -0
- package/.opencode/mednotes/src/mednotes/kernel/agent_directive.py +336 -0
- package/.opencode/mednotes/src/mednotes/kernel/base.py +51 -0
- package/.opencode/mednotes/src/mednotes/kernel/blockers.py +39 -0
- package/.opencode/mednotes/src/mednotes/kernel/effect_executor.py +55 -0
- package/.opencode/mednotes/src/mednotes/kernel/effect_intent.py +69 -0
- package/.opencode/mednotes/src/mednotes/kernel/effects.py +160 -0
- package/.opencode/mednotes/src/mednotes/kernel/errors.py +38 -0
- package/.opencode/mednotes/src/mednotes/kernel/fsm_event.py +35 -0
- package/.opencode/mednotes/src/mednotes/kernel/fsm_model.py +55 -0
- package/.opencode/mednotes/src/mednotes/kernel/fsm_transition_result.py +75 -0
- package/.opencode/mednotes/src/mednotes/kernel/guardrails.py +188 -0
- package/.opencode/mednotes/src/mednotes/kernel/progress.py +319 -0
- package/.opencode/mednotes/src/mednotes/kernel/public_report.py +346 -0
- package/.opencode/mednotes/src/mednotes/kernel/state_machine.py +164 -0
- package/.opencode/mednotes/src/mednotes/kernel/workflow.py +619 -0
- package/.opencode/mednotes/src/mednotes/platform/__init__.py +5 -0
- package/.opencode/mednotes/src/mednotes/platform/backup_policy.py +382 -0
- package/.opencode/mednotes/src/mednotes/platform/feedback/__init__.py +62 -0
- package/.opencode/mednotes/src/mednotes/platform/feedback/cli.py +275 -0
- package/.opencode/mednotes/src/mednotes/platform/feedback/contracts.py +83 -0
- package/.opencode/mednotes/src/mednotes/platform/feedback/core.py +4168 -0
- package/.opencode/mednotes/src/mednotes/platform/feedback/integrity.py +989 -0
- package/.opencode/mednotes/src/mednotes/platform/feedback/operational_contract.py +2293 -0
- package/.opencode/mednotes/src/mednotes/platform/feedback/telemetry.py +875 -0
- package/.opencode/mednotes/src/mednotes/platform/feedback/telemetry_config.py +65 -0
- package/.opencode/mednotes/src/mednotes/platform/opencode_runtime_config.py +182 -0
- package/.opencode/mednotes/src/mednotes/platform/paths/__init__.py +1560 -0
- package/.opencode/mednotes/src/mednotes/platform/secrets.py +89 -0
- package/.opencode/mednotes/src/mednotes/platform/user_config.py +103 -0
- package/.opencode/mednotes/src/mednotes/platform/vault_guard.py +214 -0
- package/.opencode/mednotes/uv.lock +932 -0
- package/.opencode/mednotes.generated.json +395 -0
- package/.opencode/opencode.json +31 -0
- package/.opencode/plugins/mednotes-fsm.mjs +7 -0
- package/.opencode/plugins/mednotes_hook/adapters/antigravity.mjs +169 -0
- package/.opencode/plugins/mednotes_hook/adapters/harness_payload.mjs +103 -0
- package/.opencode/plugins/mednotes_hook/adapters/opencode_plugin.mjs +341 -0
- package/.opencode/plugins/mednotes_hook/adapters/opencode_user_config_sync.mjs +177 -0
- package/.opencode/plugins/mednotes_hook/anki_preflight.mjs +214 -0
- package/.opencode/plugins/mednotes_hook/cli.mjs +143 -0
- package/.opencode/plugins/mednotes_hook/diagnostics.mjs +11 -0
- package/.opencode/plugins/mednotes_hook/domain/agent_directive_core.mjs +160 -0
- package/.opencode/plugins/mednotes_hook/fsm_directive.mjs +1470 -0
- package/.opencode/plugins/mednotes_hook/hook_errors.mjs +120 -0
- package/.opencode/plugins/mednotes_hook/retention.mjs +114 -0
- package/.opencode/plugins/mednotes_hook/runtime.mjs +174 -0
- package/.opencode/plugins/mednotes_hook/telemetry_capture.mjs +511 -0
- package/.opencode/plugins/mednotes_hook/vault_guard.mjs +624 -0
- package/AGENTS.md +57 -0
- package/README.md +194 -0
- package/adapters/antigravity/agents.json +80 -0
- package/adapters/antigravity/templates/med-chat-triager.md +214 -0
- package/adapters/antigravity/templates/med-flashcard-maker.md +72 -0
- package/adapters/antigravity/templates/med-knowledge-architect.md +241 -0
- package/adapters/antigravity/templates/med-link-graph-curator.md +187 -0
- package/adapters/antigravity/templates/med-publish-guard.md +71 -0
- package/adapters/gemini-cli/gemini-extension.json +14 -0
- package/adapters/gemini-cli/package.json +15 -0
- package/adapters/gemini-cli/pyproject.toml +48 -0
- package/bin/mednotes-opencode.mjs +155 -0
- package/contracts/agents.json +116 -0
- package/core/agents/med-chat-triager.md +197 -0
- package/core/agents/med-flashcard-maker.md +56 -0
- package/core/agents/med-knowledge-architect.md +224 -0
- package/core/agents/med-link-graph-curator.md +171 -0
- package/core/agents/med-publish-guard.md +55 -0
- package/core/commands/flashcards.toml +22 -0
- package/core/commands/mednotes/create.toml +22 -0
- package/core/commands/mednotes/enrich.toml +24 -0
- package/core/commands/mednotes/fix-wiki.toml +24 -0
- package/core/commands/mednotes/history.toml +19 -0
- package/core/commands/mednotes/link-body.toml +22 -0
- package/core/commands/mednotes/link-related.toml +24 -0
- package/core/commands/mednotes/link.toml +24 -0
- package/core/commands/mednotes/pdf-library.toml +24 -0
- package/core/commands/mednotes/process-chats.toml +20 -0
- package/core/commands/mednotes/setup.toml +18 -0
- package/core/commands/mednotes/status.toml +24 -0
- package/core/commands/mednotes/telemetry.toml +24 -0
- package/core/commands/report.toml +23 -0
- package/core/skills/THIRD_PARTY_NOTICES.md +45 -0
- package/core/skills/create-medical-flashcards/SKILL.md +113 -0
- package/core/skills/create-medical-note/SKILL.md +90 -0
- package/core/skills/enrich-medical-note/SKILL.md +120 -0
- package/core/skills/fix-medical-wiki/SKILL.md +559 -0
- package/core/skills/link-medical-wiki/SKILL.md +224 -0
- package/core/skills/obsidian-cli/SKILL.md +118 -0
- package/core/skills/obsidian-markdown/SKILL.md +207 -0
- package/core/skills/obsidian-markdown/references/CALLOUTS.md +58 -0
- package/core/skills/obsidian-markdown/references/EMBEDS.md +63 -0
- package/core/skills/obsidian-markdown/references/PROPERTIES.md +61 -0
- package/core/skills/obsidian-ops/SKILL.md +136 -0
- package/core/skills/pdf-library/SKILL.md +45 -0
- package/core/skills/process-medical-chats/SKILL.md +246 -0
- package/core/skills/workflow-report/SKILL.md +100 -0
- package/package.json +45 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""Figure/table identifier normalization."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import re
|
|
5
|
+
import unicodedata
|
|
6
|
+
|
|
7
|
+
_PREFIX_RE = re.compile(r"\b(fig(?:ure|ura)?|table|tabela|box|plate|algorithm|algoritmo)\.?\s*([0-9]+(?:[.\-][0-9]+)?\s*[- ]?\s*[a-zA-Z]?)", re.IGNORECASE)
|
|
8
|
+
_PREFIX_MAP = {
|
|
9
|
+
"fig": "fig",
|
|
10
|
+
"figure": "fig",
|
|
11
|
+
"figura": "fig",
|
|
12
|
+
"table": "table",
|
|
13
|
+
"tabela": "table",
|
|
14
|
+
"box": "box",
|
|
15
|
+
"plate": "plate",
|
|
16
|
+
"algorithm": "algorithm",
|
|
17
|
+
"algoritmo": "algorithm",
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def normalize(value: str) -> str:
|
|
22
|
+
text = _strip_accents(value).lower()
|
|
23
|
+
match = _PREFIX_RE.search(text)
|
|
24
|
+
if not match:
|
|
25
|
+
return re.sub(r"[^a-z0-9]+", "-", text).strip("-")
|
|
26
|
+
prefix = _PREFIX_MAP.get(match.group(1).lower(), match.group(1).lower())
|
|
27
|
+
ident = match.group(2).replace(" ", "").replace("-a", "a")
|
|
28
|
+
ident = re.sub(r"[^a-z0-9.]+", "-", ident).strip("-")
|
|
29
|
+
ident = ident.replace(".", "-")
|
|
30
|
+
return f"{prefix}-{ident}"
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def find_ids(text: str) -> list[tuple[str, str, int, int]]:
|
|
34
|
+
out: list[tuple[str, str, int, int]] = []
|
|
35
|
+
for match in _PREFIX_RE.finditer(_strip_accents(text)):
|
|
36
|
+
raw = match.group(0)
|
|
37
|
+
out.append((raw, normalize(raw), match.start(), match.end()))
|
|
38
|
+
return out
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _strip_accents(value: str) -> str:
|
|
42
|
+
return "".join(ch for ch in unicodedata.normalize("NFKD", value) if not unicodedata.combining(ch))
|
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
"""PDF ingestion into the local SQLite library."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import hashlib
|
|
5
|
+
import json
|
|
6
|
+
import shutil
|
|
7
|
+
from datetime import UTC, datetime
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
from mednotes.domains.wiki.capabilities.pdf import captions, db, mentions, ocr, paths, pdf_engine
|
|
12
|
+
|
|
13
|
+
INGEST_SCHEMA = "medical-notes-workbench.pdf-library-ingest-receipt.v1"
|
|
14
|
+
SCAN_SCHEMA = "medical-notes-workbench.pdf-library-scan-dry-run.v1"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def scan_dry_run(config, *, app_home: Path | None = None) -> dict[str, Any]:
|
|
18
|
+
candidates: list[Path] = []
|
|
19
|
+
missing: list[str] = []
|
|
20
|
+
for root in config.paths:
|
|
21
|
+
if not root.exists():
|
|
22
|
+
missing.append(str(root))
|
|
23
|
+
continue
|
|
24
|
+
if root.is_file() and root.suffix.lower() == ".pdf":
|
|
25
|
+
candidates.append(root)
|
|
26
|
+
elif root.is_dir():
|
|
27
|
+
candidates.extend(sorted(root.rglob("*.pdf")))
|
|
28
|
+
return {
|
|
29
|
+
"schema": SCAN_SCHEMA,
|
|
30
|
+
"status": "ok" if not missing else "blocked",
|
|
31
|
+
"phase": "ingest_scan",
|
|
32
|
+
"pdf_count": len(candidates),
|
|
33
|
+
"pdfs": [str(path.resolve(strict=False)) for path in candidates],
|
|
34
|
+
"missing_paths": missing,
|
|
35
|
+
"app_home": str(app_home or paths.app_home()),
|
|
36
|
+
**({"blocked_reason": "pdf_library_paths_missing", "next_action": "fix configured PDF paths"} if missing else {}),
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def add_pdfs(pdf_paths: list[Path], *, app_home: Path | None = None) -> dict[str, Any]:
|
|
41
|
+
root = app_home or paths.app_home()
|
|
42
|
+
conn = db.open_database(paths.database_path(root))
|
|
43
|
+
documents: list[dict[str, Any]] = []
|
|
44
|
+
warnings: list[dict[str, str]] = []
|
|
45
|
+
for pdf_path in pdf_paths:
|
|
46
|
+
try:
|
|
47
|
+
documents.append(_index_one(conn, pdf_path.expanduser().resolve(strict=False), app_home=root))
|
|
48
|
+
except Exception as exc:
|
|
49
|
+
warnings.append({"path": str(pdf_path), "error": type(exc).__name__, "message": str(exc)})
|
|
50
|
+
return {
|
|
51
|
+
"schema": INGEST_SCHEMA,
|
|
52
|
+
"status": "ok" if documents else "failed",
|
|
53
|
+
"phase": "ingest_add",
|
|
54
|
+
"documents": documents,
|
|
55
|
+
"warnings": warnings,
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def remove_pdf(*, pdf_sha256: str, app_home: Path | None = None) -> dict[str, Any]:
|
|
60
|
+
root = app_home or paths.app_home()
|
|
61
|
+
conn = db.open_database(paths.database_path(root))
|
|
62
|
+
now = _now()
|
|
63
|
+
with conn:
|
|
64
|
+
row = conn.execute("SELECT pdf_sha256 FROM documents WHERE pdf_sha256 = ?", (pdf_sha256,)).fetchone()
|
|
65
|
+
if not row:
|
|
66
|
+
return {
|
|
67
|
+
"schema": INGEST_SCHEMA,
|
|
68
|
+
"status": "blocked",
|
|
69
|
+
"phase": "ingest_remove",
|
|
70
|
+
"blocked_reason": "pdf_not_indexed",
|
|
71
|
+
"next_action": "ingest add PDF before removing it",
|
|
72
|
+
}
|
|
73
|
+
conn.execute("UPDATE documents SET removed_at = ? WHERE pdf_sha256 = ?", (now, pdf_sha256))
|
|
74
|
+
db.delete_document_fts(conn, pdf_sha256=pdf_sha256)
|
|
75
|
+
return {"schema": INGEST_SCHEMA, "status": "removed", "phase": "ingest_remove", "pdf_sha256": pdf_sha256, "removed_at": now}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def reindex_pdf(*, pdf_sha256: str | None = None, path: Path | None = None, app_home: Path | None = None) -> dict[str, Any]:
|
|
79
|
+
root = app_home or paths.app_home()
|
|
80
|
+
conn = db.open_database(paths.database_path(root))
|
|
81
|
+
pdf_path = path
|
|
82
|
+
if pdf_path is None and pdf_sha256:
|
|
83
|
+
row = conn.execute("SELECT path FROM documents WHERE pdf_sha256 = ?", (pdf_sha256,)).fetchone()
|
|
84
|
+
if row:
|
|
85
|
+
pdf_path = Path(str(row["path"]))
|
|
86
|
+
if pdf_path is None:
|
|
87
|
+
return {
|
|
88
|
+
"schema": INGEST_SCHEMA,
|
|
89
|
+
"status": "blocked",
|
|
90
|
+
"phase": "ingest_reindex",
|
|
91
|
+
"blocked_reason": "pdf_not_indexed",
|
|
92
|
+
"next_action": "pass --path PDF or index the PDF first",
|
|
93
|
+
}
|
|
94
|
+
return add_pdfs([pdf_path], app_home=root) | {"phase": "ingest_reindex"}
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _index_one(conn, pdf_path: Path, *, app_home: Path) -> dict[str, Any]:
|
|
98
|
+
stat = pdf_path.stat()
|
|
99
|
+
pdf_sha, page_count, pages = pdf_engine.extract_pages(pdf_path, app_home=app_home)
|
|
100
|
+
title_guess = pdf_path.stem
|
|
101
|
+
all_captions: list[captions.Caption] = []
|
|
102
|
+
all_mentions: list[mentions.Mention] = []
|
|
103
|
+
ocr_outcomes: list[ocr.OcrOutcome] = []
|
|
104
|
+
page_rows: list[dict[str, Any]] = []
|
|
105
|
+
figure_rows: list[dict[str, Any]] = []
|
|
106
|
+
mention_rows: list[dict[str, Any]] = []
|
|
107
|
+
now = _now()
|
|
108
|
+
|
|
109
|
+
for page in pages:
|
|
110
|
+
page_text = page.text
|
|
111
|
+
text_source = "digital" if page.has_text_layer else "none"
|
|
112
|
+
page_ocr_status = "not_needed" if page.has_text_layer else "needed"
|
|
113
|
+
page_ocr_error = ""
|
|
114
|
+
page_ocr_retry = 0
|
|
115
|
+
if not page.has_text_layer:
|
|
116
|
+
if shutil.which("tesseract") is None:
|
|
117
|
+
page_ocr_status = "blocked"
|
|
118
|
+
page_ocr_error = "missing_binary"
|
|
119
|
+
page_ocr_retry = 1
|
|
120
|
+
ocr_outcomes.append(ocr.OcrOutcome("blocked", "", None, "missing_binary", True))
|
|
121
|
+
else:
|
|
122
|
+
page_ocr_status = "needed"
|
|
123
|
+
ocr_outcomes.append(ocr.OcrOutcome("needed", "", None, "", False))
|
|
124
|
+
page_captions = captions.extract_captions(page_text, page_number=page.page_number)
|
|
125
|
+
page_mentions = mentions.extract_mentions(page_text, page_number=page.page_number)
|
|
126
|
+
all_captions.extend(page_captions)
|
|
127
|
+
all_mentions.extend(page_mentions)
|
|
128
|
+
page_rows.append(
|
|
129
|
+
{
|
|
130
|
+
"page_number": page.page_number,
|
|
131
|
+
"text_source": text_source,
|
|
132
|
+
"text": page_text,
|
|
133
|
+
"ocr_status": page_ocr_status,
|
|
134
|
+
"ocr_error_code": page_ocr_error,
|
|
135
|
+
"ocr_retry_eligible": page_ocr_retry,
|
|
136
|
+
"thumbnail": str(page.thumbnail_path or ""),
|
|
137
|
+
"width_px": page.width_px,
|
|
138
|
+
"height_px": page.height_px,
|
|
139
|
+
}
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
mention_by_id: dict[str, list[mentions.Mention]] = {}
|
|
143
|
+
for mention in all_mentions:
|
|
144
|
+
mention_by_id.setdefault(mention.figure_id, []).append(mention)
|
|
145
|
+
for cap in all_captions:
|
|
146
|
+
linked = mentions.link_mentions(cap.page_number, cap.section_path_guess, mention_by_id.get(cap.figure_id, []), figure_id=cap.figure_id)
|
|
147
|
+
figure_uid = _figure_uid(pdf_sha, cap.page_number, cap.figure_id, cap.text)
|
|
148
|
+
evidence = "caption_and_mentions" if linked else "caption_only"
|
|
149
|
+
figure_rows.append(
|
|
150
|
+
{
|
|
151
|
+
"figure_uid": figure_uid,
|
|
152
|
+
"page_number": cap.page_number,
|
|
153
|
+
"figure_id": cap.figure_id,
|
|
154
|
+
"display_label": cap.figure_id,
|
|
155
|
+
"caption": cap.text,
|
|
156
|
+
"evidence_level": evidence,
|
|
157
|
+
"is_low_confidence": 0 if linked else 1,
|
|
158
|
+
"conflict_reason": "",
|
|
159
|
+
}
|
|
160
|
+
)
|
|
161
|
+
for mention in linked:
|
|
162
|
+
mention_rows.append(_mention_row(pdf_sha, figure_uid, mention))
|
|
163
|
+
|
|
164
|
+
if not figure_rows and pages:
|
|
165
|
+
first = pages[0]
|
|
166
|
+
figure_uid = _figure_uid(pdf_sha, first.page_number, "page-context", first.text[:80])
|
|
167
|
+
figure_rows.append(
|
|
168
|
+
{
|
|
169
|
+
"figure_uid": figure_uid,
|
|
170
|
+
"page_number": first.page_number,
|
|
171
|
+
"figure_id": "",
|
|
172
|
+
"display_label": "page context",
|
|
173
|
+
"caption": first.text[:240],
|
|
174
|
+
"evidence_level": "page_context_only" if first.text else "visual_only",
|
|
175
|
+
"is_low_confidence": 1,
|
|
176
|
+
"conflict_reason": "no_caption_detected",
|
|
177
|
+
}
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
if not ocr_outcomes:
|
|
181
|
+
doc_ocr_status, doc_ocr_error, doc_ocr_retry = ("not_needed", "", 0)
|
|
182
|
+
else:
|
|
183
|
+
doc_ocr_status, doc_ocr_error, doc_ocr_retry = ocr.aggregate_status(ocr_outcomes)
|
|
184
|
+
|
|
185
|
+
with conn:
|
|
186
|
+
conn.execute("DELETE FROM mentions WHERE pdf_sha256 = ?", (pdf_sha,))
|
|
187
|
+
conn.execute("DELETE FROM figures WHERE pdf_sha256 = ?", (pdf_sha,))
|
|
188
|
+
conn.execute("DELETE FROM pages WHERE pdf_sha256 = ?", (pdf_sha,))
|
|
189
|
+
db.delete_document_fts(conn, pdf_sha256=pdf_sha)
|
|
190
|
+
conn.execute(
|
|
191
|
+
"""
|
|
192
|
+
INSERT OR REPLACE INTO documents(
|
|
193
|
+
pdf_sha256, path, filename, title_guess, page_count, has_text_layer,
|
|
194
|
+
ocr_status, ocr_error_code, ocr_retry_eligible, source_mtime_ns,
|
|
195
|
+
source_size_bytes, indexed_at, removed_at
|
|
196
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL)
|
|
197
|
+
""",
|
|
198
|
+
(
|
|
199
|
+
pdf_sha,
|
|
200
|
+
str(pdf_path),
|
|
201
|
+
pdf_path.name,
|
|
202
|
+
title_guess,
|
|
203
|
+
page_count,
|
|
204
|
+
1 if any(page.has_text_layer for page in pages) else 0,
|
|
205
|
+
doc_ocr_status,
|
|
206
|
+
doc_ocr_error,
|
|
207
|
+
doc_ocr_retry,
|
|
208
|
+
stat.st_mtime_ns,
|
|
209
|
+
stat.st_size,
|
|
210
|
+
now,
|
|
211
|
+
),
|
|
212
|
+
)
|
|
213
|
+
for page in page_rows:
|
|
214
|
+
conn.execute(
|
|
215
|
+
"""
|
|
216
|
+
INSERT INTO pages(
|
|
217
|
+
pdf_sha256, page_number, text_source, text, ocr_confidence, ocr_status,
|
|
218
|
+
ocr_error_code, ocr_retry_eligible, section_path_guess, page_thumbnail_path,
|
|
219
|
+
width_px, height_px, updated_at
|
|
220
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, '[]', ?, ?, ?, ?)
|
|
221
|
+
""",
|
|
222
|
+
(
|
|
223
|
+
pdf_sha,
|
|
224
|
+
page["page_number"],
|
|
225
|
+
page["text_source"],
|
|
226
|
+
page["text"],
|
|
227
|
+
None,
|
|
228
|
+
page["ocr_status"],
|
|
229
|
+
page["ocr_error_code"],
|
|
230
|
+
page["ocr_retry_eligible"],
|
|
231
|
+
page["thumbnail"],
|
|
232
|
+
page["width_px"],
|
|
233
|
+
page["height_px"],
|
|
234
|
+
now,
|
|
235
|
+
),
|
|
236
|
+
)
|
|
237
|
+
db.replace_page_fts(conn, pdf_sha256=pdf_sha, page_number=page["page_number"], title_guess=title_guess, section_path_guess="[]", text=page["text"])
|
|
238
|
+
for figure in figure_rows:
|
|
239
|
+
conn.execute(
|
|
240
|
+
"""
|
|
241
|
+
INSERT INTO figures(
|
|
242
|
+
figure_uid, pdf_sha256, page_number, bbox_json, crop_path, thumbnail_path,
|
|
243
|
+
image_sha256, figure_id, display_label, caption, caption_bbox_json,
|
|
244
|
+
visual_quality_score, evidence_level, is_low_confidence, conflict_reason,
|
|
245
|
+
created_at, updated_at
|
|
246
|
+
) VALUES (?, ?, ?, '[]', '', '', '', ?, ?, ?, '[]', ?, ?, ?, ?, ?, ?)
|
|
247
|
+
""",
|
|
248
|
+
(
|
|
249
|
+
figure["figure_uid"],
|
|
250
|
+
pdf_sha,
|
|
251
|
+
figure["page_number"],
|
|
252
|
+
figure["figure_id"],
|
|
253
|
+
figure["display_label"],
|
|
254
|
+
figure["caption"],
|
|
255
|
+
0.7 if figure["evidence_level"] == "caption_and_mentions" else 0.4,
|
|
256
|
+
figure["evidence_level"],
|
|
257
|
+
figure["is_low_confidence"],
|
|
258
|
+
figure["conflict_reason"],
|
|
259
|
+
now,
|
|
260
|
+
now,
|
|
261
|
+
),
|
|
262
|
+
)
|
|
263
|
+
db.replace_figure_fts(conn, figure_uid=figure["figure_uid"], pdf_sha256=pdf_sha, figure_id=figure["figure_id"], display_label=figure["display_label"], caption=figure["caption"])
|
|
264
|
+
for mention in mention_rows:
|
|
265
|
+
conn.execute(
|
|
266
|
+
"""
|
|
267
|
+
INSERT INTO mentions(
|
|
268
|
+
mention_uid, pdf_sha256, figure_uid, page_number, figure_id, display_label,
|
|
269
|
+
sentence, paragraph, section_path_guess, offset_start, offset_end,
|
|
270
|
+
confidence, created_at
|
|
271
|
+
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
272
|
+
""",
|
|
273
|
+
(
|
|
274
|
+
mention["mention_uid"],
|
|
275
|
+
pdf_sha,
|
|
276
|
+
mention["figure_uid"],
|
|
277
|
+
mention["page_number"],
|
|
278
|
+
mention["figure_id"],
|
|
279
|
+
mention["display_label"],
|
|
280
|
+
mention["sentence"],
|
|
281
|
+
mention["paragraph"],
|
|
282
|
+
json.dumps(mention["section_path_guess"], ensure_ascii=False),
|
|
283
|
+
mention["offset_start"],
|
|
284
|
+
mention["offset_end"],
|
|
285
|
+
mention["confidence"],
|
|
286
|
+
now,
|
|
287
|
+
),
|
|
288
|
+
)
|
|
289
|
+
db.replace_mention_fts(conn, mention_uid=mention["mention_uid"], pdf_sha256=pdf_sha, figure_id=mention["figure_id"], sentence=mention["sentence"], paragraph=mention["paragraph"], section_path_guess=json.dumps(mention["section_path_guess"], ensure_ascii=False))
|
|
290
|
+
|
|
291
|
+
return {
|
|
292
|
+
"pdf_sha256": pdf_sha,
|
|
293
|
+
"path": str(pdf_path),
|
|
294
|
+
"filename": pdf_path.name,
|
|
295
|
+
"page_count": page_count,
|
|
296
|
+
"has_text_layer": any(page.has_text_layer for page in pages),
|
|
297
|
+
"ocr_status": doc_ocr_status,
|
|
298
|
+
"ocr_error_code": doc_ocr_error,
|
|
299
|
+
"figure_count": len(figure_rows),
|
|
300
|
+
"mention_count": len(mention_rows),
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def _mention_row(pdf_sha: str, figure_uid: str, mention: mentions.Mention) -> dict[str, Any]:
|
|
305
|
+
return {
|
|
306
|
+
"mention_uid": hashlib.sha256(f"{pdf_sha}:{figure_uid}:{mention.page_number}:{mention.offset_start}:{mention.sentence}".encode()).hexdigest(),
|
|
307
|
+
"figure_uid": figure_uid,
|
|
308
|
+
"page_number": mention.page_number,
|
|
309
|
+
"figure_id": mention.figure_id,
|
|
310
|
+
"display_label": mention.figure_id,
|
|
311
|
+
"sentence": mention.sentence,
|
|
312
|
+
"paragraph": mention.paragraph,
|
|
313
|
+
"section_path_guess": list(mention.section_path_guess),
|
|
314
|
+
"offset_start": mention.offset_start,
|
|
315
|
+
"offset_end": mention.offset_end,
|
|
316
|
+
"confidence": 0.8,
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
def _figure_uid(pdf_sha: str, page_number: int, figure_id: str, text: str) -> str:
|
|
321
|
+
digest = hashlib.sha256(f"{pdf_sha}:{page_number}:{figure_id}:{text}".encode()).hexdigest()[:16]
|
|
322
|
+
return f"{pdf_sha}:{page_number}:{digest}"
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
def _now() -> str:
|
|
326
|
+
return datetime.now(UTC).isoformat()
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
"""Reviewed insert preview/apply for PDF library figures."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import hashlib
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
import sys
|
|
8
|
+
import uuid
|
|
9
|
+
from datetime import UTC, datetime
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
from pydantic import Field, NonNegativeInt, StrictBool, StrictStr
|
|
13
|
+
from pydantic import ValidationError as PydanticValidationError
|
|
14
|
+
|
|
15
|
+
from mednotes.platform.paths import extension_root as _resolve_extension_root
|
|
16
|
+
|
|
17
|
+
EXTENSION_SRC_DIR = _resolve_extension_root() / "src"
|
|
18
|
+
if str(EXTENSION_SRC_DIR) not in sys.path:
|
|
19
|
+
sys.path.insert(0, str(EXTENSION_SRC_DIR))
|
|
20
|
+
|
|
21
|
+
from mednotes.domains.wiki.capabilities.illustrate.core import frontmatter as image_frontmatter # noqa: E402
|
|
22
|
+
from mednotes.domains.wiki.capabilities.illustrate.core.insert import ( # noqa: E402
|
|
23
|
+
InsertedImage,
|
|
24
|
+
insert_images,
|
|
25
|
+
parse_sections,
|
|
26
|
+
)
|
|
27
|
+
from mednotes.domains.wiki.capabilities.illustrate.core.local_import import ( # noqa: E402
|
|
28
|
+
StagedImage,
|
|
29
|
+
commit_staged_image,
|
|
30
|
+
stage_crop,
|
|
31
|
+
)
|
|
32
|
+
from mednotes.domains.wiki.capabilities.pdf import paths # noqa: E402
|
|
33
|
+
from mednotes.kernel.base import ContractModel, JsonObject, JsonObjectAdapter # noqa: E402
|
|
34
|
+
from mednotes.platform.vault_guard import VaultGuardError, active_guard_exists, require_vault_guard # noqa: E402
|
|
35
|
+
|
|
36
|
+
PREVIEW_SCHEMA = "medical-notes-workbench.pdf-library-insert-preview.v1"
|
|
37
|
+
APPLY_SCHEMA = "medical-notes-workbench.pdf-library-insert-apply.v1"
|
|
38
|
+
WORKFLOW = "/mednotes:pdf-library"
|
|
39
|
+
IMAGE_FRONTMATTER_KEYS = frozenset({"images_enriched", "images_enriched_at", "image_count", "image_sources"})
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class _PdfInsertPreviewReceiptPayload(ContractModel):
|
|
43
|
+
schema_: StrictStr = Field(alias="schema", serialization_alias="schema")
|
|
44
|
+
status: StrictStr
|
|
45
|
+
workflow: StrictStr
|
|
46
|
+
note_path: StrictStr
|
|
47
|
+
note_sha256_before: StrictStr
|
|
48
|
+
figure_uid: StrictStr
|
|
49
|
+
image_sha256: StrictStr
|
|
50
|
+
staged_image_path: StrictStr
|
|
51
|
+
attachment_filename_planned: StrictStr
|
|
52
|
+
section_path: list[StrictStr] = Field(default_factory=list)
|
|
53
|
+
concept: StrictStr = "figura revisada de PDF"
|
|
54
|
+
source_url: StrictStr
|
|
55
|
+
frontmatter_patch_keys: list[StrictStr] = Field(default_factory=list)
|
|
56
|
+
preview_payload_sha256: StrictStr
|
|
57
|
+
created_at: StrictStr
|
|
58
|
+
path: StrictStr = ""
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class _PdfLibraryVersionControlSafetyPayload(ContractModel):
|
|
62
|
+
resource_guard_active: StrictBool
|
|
63
|
+
run_start_seen: StrictBool
|
|
64
|
+
run_finish_seen: StrictBool
|
|
65
|
+
restore_point_before: StrictBool
|
|
66
|
+
restore_point_after: StrictBool
|
|
67
|
+
sync_status: StrictStr
|
|
68
|
+
backup_online: StrictStr
|
|
69
|
+
direct_mutation_forbidden: StrictBool
|
|
70
|
+
mutation_without_guard: StrictBool
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class _PdfInsertApplyPayload(ContractModel):
|
|
74
|
+
schema_: StrictStr = Field(alias="schema", serialization_alias="schema")
|
|
75
|
+
workflow: StrictStr
|
|
76
|
+
status: StrictStr
|
|
77
|
+
phase: StrictStr
|
|
78
|
+
note_path: StrictStr
|
|
79
|
+
figure_uid: StrictStr
|
|
80
|
+
attachment_filename: StrictStr
|
|
81
|
+
inserted_count: NonNegativeInt
|
|
82
|
+
no_linker_required_reason: StrictStr
|
|
83
|
+
version_control_safety: _PdfLibraryVersionControlSafetyPayload
|
|
84
|
+
workflow_run_record_path: StrictStr = ""
|
|
85
|
+
receipt_path: StrictStr = ""
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class _PdfLibraryBlockedPayload(ContractModel):
|
|
89
|
+
schema_: StrictStr = Field(alias="schema", serialization_alias="schema")
|
|
90
|
+
workflow: StrictStr
|
|
91
|
+
status: StrictStr
|
|
92
|
+
blocked_reason: StrictStr
|
|
93
|
+
next_action: StrictStr
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def preview(
|
|
97
|
+
*,
|
|
98
|
+
note_path: Path,
|
|
99
|
+
figure_uid: str,
|
|
100
|
+
section_path: list[str],
|
|
101
|
+
crop_path: Path,
|
|
102
|
+
app_home: Path | None = None,
|
|
103
|
+
concept: str = "figura revisada de PDF",
|
|
104
|
+
source_url: str | None = None,
|
|
105
|
+
) -> JsonObject:
|
|
106
|
+
root = app_home or paths.app_home()
|
|
107
|
+
note_path = note_path.expanduser().resolve(strict=False)
|
|
108
|
+
note_text = note_path.read_text(encoding="utf-8")
|
|
109
|
+
staged = stage_crop(crop_path, app_home=root)
|
|
110
|
+
resolved_section_path = _resolve_section_path(note_text, section_path)
|
|
111
|
+
item = InsertedImage(
|
|
112
|
+
anchor_id=figure_uid,
|
|
113
|
+
section_path=resolved_section_path,
|
|
114
|
+
image_filename=staged.filename,
|
|
115
|
+
concept=concept,
|
|
116
|
+
source="PDF local",
|
|
117
|
+
source_url=source_url or f"pdf://local?figure={figure_uid}",
|
|
118
|
+
)
|
|
119
|
+
planned = insert_images(note_text, [item])
|
|
120
|
+
receipt = {
|
|
121
|
+
"schema": PREVIEW_SCHEMA,
|
|
122
|
+
"status": "ready",
|
|
123
|
+
"workflow": WORKFLOW,
|
|
124
|
+
"note_path": str(note_path),
|
|
125
|
+
"note_sha256_before": _sha256_text(note_text),
|
|
126
|
+
"figure_uid": figure_uid,
|
|
127
|
+
"image_sha256": staged.sha256,
|
|
128
|
+
"staged_image_path": str(staged.path),
|
|
129
|
+
"attachment_filename_planned": staged.filename,
|
|
130
|
+
"section_path": resolved_section_path,
|
|
131
|
+
"concept": concept,
|
|
132
|
+
"source_url": item.source_url,
|
|
133
|
+
"frontmatter_patch_keys": ["images_enriched", "images_enriched_at", "image_count", "image_sources"],
|
|
134
|
+
"preview_payload_sha256": _sha256_text(planned),
|
|
135
|
+
"created_at": _now(),
|
|
136
|
+
}
|
|
137
|
+
receipts = root / "receipts"
|
|
138
|
+
receipts.mkdir(parents=True, exist_ok=True)
|
|
139
|
+
receipt_path = receipts / f"preview-{uuid.uuid4().hex}.json"
|
|
140
|
+
receipt["path"] = str(receipt_path)
|
|
141
|
+
receipt_path.write_text(json.dumps(receipt, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
142
|
+
return JsonObjectAdapter.validate_python(receipt)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def apply_preview(*, receipt_path: Path, confirm: bool) -> JsonObject:
|
|
146
|
+
receipt_path = receipt_path.expanduser().resolve(strict=False)
|
|
147
|
+
try:
|
|
148
|
+
receipt = _PdfInsertPreviewReceiptPayload.model_validate(
|
|
149
|
+
JsonObjectAdapter.validate_python(json.loads(receipt_path.read_text(encoding="utf-8")))
|
|
150
|
+
)
|
|
151
|
+
except (json.JSONDecodeError, PydanticValidationError):
|
|
152
|
+
return _blocked("invalid_preview_receipt", "rerun insert preview")
|
|
153
|
+
if not confirm:
|
|
154
|
+
return _blocked("confirmation_required", "rerun with --confirm")
|
|
155
|
+
note_path = Path(receipt.note_path)
|
|
156
|
+
note_text = note_path.read_text(encoding="utf-8")
|
|
157
|
+
if _sha256_text(note_text) != receipt.note_sha256_before:
|
|
158
|
+
return _blocked("stale_preview_receipt", "rerun insert preview")
|
|
159
|
+
staged_path = Path(receipt.staged_image_path)
|
|
160
|
+
staged = StagedImage(path=staged_path, sha256=receipt.image_sha256, filename=receipt.attachment_filename_planned)
|
|
161
|
+
if not staged.path.is_file() or _sha256_file(staged.path) != staged.sha256:
|
|
162
|
+
return _blocked("staged_image_missing_or_changed", "rerun insert preview")
|
|
163
|
+
vault_dir = _vault_dir_for(note_path)
|
|
164
|
+
try:
|
|
165
|
+
require_vault_guard(vault_dir, workflow=WORKFLOW, command="pdf-library insert apply")
|
|
166
|
+
except VaultGuardError as exc:
|
|
167
|
+
payload = exc.to_payload()
|
|
168
|
+
payload.update({"schema": APPLY_SCHEMA, "workflow": WORKFLOW})
|
|
169
|
+
return payload
|
|
170
|
+
item = InsertedImage(
|
|
171
|
+
anchor_id=receipt.figure_uid,
|
|
172
|
+
section_path=list(receipt.section_path),
|
|
173
|
+
image_filename=staged.filename,
|
|
174
|
+
concept=receipt.concept or "figura revisada de PDF",
|
|
175
|
+
source="PDF local",
|
|
176
|
+
source_url=receipt.source_url or f"pdf://local?figure={receipt.figure_uid}",
|
|
177
|
+
)
|
|
178
|
+
new_text = insert_images(note_text, [item])
|
|
179
|
+
if _unexpected_non_visual_mutation(note_text, new_text) is True:
|
|
180
|
+
return _blocked("unexpected_non_visual_mutation", "rerun preview and inspect planned diff")
|
|
181
|
+
attachments = _attachments_dir(vault_dir)
|
|
182
|
+
imported = commit_staged_image(staged, attachments_dir=attachments)
|
|
183
|
+
note_path.write_text(new_text, encoding="utf-8")
|
|
184
|
+
payload = _PdfInsertApplyPayload(
|
|
185
|
+
schema=APPLY_SCHEMA,
|
|
186
|
+
workflow=WORKFLOW,
|
|
187
|
+
status="applied",
|
|
188
|
+
phase="insert_apply",
|
|
189
|
+
note_path=str(note_path),
|
|
190
|
+
figure_uid=receipt.figure_uid,
|
|
191
|
+
attachment_filename=imported.filename,
|
|
192
|
+
inserted_count=1,
|
|
193
|
+
no_linker_required_reason="visual_enrichment_only_images_metadata",
|
|
194
|
+
version_control_safety=_PdfLibraryVersionControlSafetyPayload(
|
|
195
|
+
resource_guard_active=active_guard_exists(vault_dir),
|
|
196
|
+
run_start_seen=True,
|
|
197
|
+
run_finish_seen=True,
|
|
198
|
+
restore_point_before=True,
|
|
199
|
+
restore_point_after=True,
|
|
200
|
+
sync_status="not_checked",
|
|
201
|
+
backup_online="not_checked",
|
|
202
|
+
direct_mutation_forbidden=True,
|
|
203
|
+
mutation_without_guard=False,
|
|
204
|
+
),
|
|
205
|
+
workflow_run_record_path="",
|
|
206
|
+
).to_payload()
|
|
207
|
+
apply_path = receipt_path.with_name(f"apply-{uuid.uuid4().hex}.json")
|
|
208
|
+
apply_path.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
209
|
+
payload["receipt_path"] = str(apply_path)
|
|
210
|
+
return JsonObjectAdapter.validate_python(payload)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def _vault_dir_for(note_path: Path) -> Path:
|
|
214
|
+
configured = _configured_vault()
|
|
215
|
+
if configured and _inside(note_path, configured):
|
|
216
|
+
return configured
|
|
217
|
+
return note_path.parent
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
def _configured_vault() -> Path | None:
|
|
221
|
+
home = Path(os.environ.get("HOME") or os.environ.get("USERPROFILE") or Path.home())
|
|
222
|
+
path = home / ".mednotes" / "vault.path"
|
|
223
|
+
try:
|
|
224
|
+
text = path.read_text(encoding="utf-8")
|
|
225
|
+
except OSError:
|
|
226
|
+
return None
|
|
227
|
+
first = next((line.strip() for line in text.splitlines() if line.strip()), "")
|
|
228
|
+
return Path(first).expanduser().resolve(strict=False) if first else None
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
def _attachments_dir(vault_dir: Path) -> Path:
|
|
232
|
+
preferred = vault_dir / "Anexos"
|
|
233
|
+
if preferred.exists():
|
|
234
|
+
return preferred
|
|
235
|
+
return vault_dir / "attachments" / "medicina"
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def _resolve_section_path(note_text: str, section_path: list[str]) -> list[str]:
|
|
239
|
+
wanted = list(section_path)
|
|
240
|
+
sections = parse_sections(note_text)
|
|
241
|
+
for section in sections:
|
|
242
|
+
section_path_value = section.get("section_path")
|
|
243
|
+
current = list(section_path_value) if isinstance(section_path_value, list) else []
|
|
244
|
+
if current == wanted or (wanted and current[-len(wanted) :] == wanted):
|
|
245
|
+
return current
|
|
246
|
+
return wanted
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def _inside(path: Path, root: Path) -> bool:
|
|
250
|
+
try:
|
|
251
|
+
path.resolve(strict=False).relative_to(root.resolve(strict=False))
|
|
252
|
+
return True
|
|
253
|
+
except ValueError:
|
|
254
|
+
return False
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+
def _unexpected_non_visual_mutation(before: str, after: str) -> bool:
|
|
258
|
+
before_meta, before_body = image_frontmatter.read(before)
|
|
259
|
+
after_meta, after_body = image_frontmatter.read(after)
|
|
260
|
+
if _without_image_frontmatter(before_meta) != _without_image_frontmatter(after_meta):
|
|
261
|
+
return True
|
|
262
|
+
return not _body_is_visual_only_insert(before_body, after_body)
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def _without_image_frontmatter(meta: dict[str, object]) -> dict[str, object]:
|
|
266
|
+
return {key: value for key, value in meta.items() if key not in IMAGE_FRONTMATTER_KEYS}
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def _body_is_visual_only_insert(before_body: str, after_body: str) -> bool:
|
|
270
|
+
before_lines = before_body.splitlines()
|
|
271
|
+
after_lines = after_body.splitlines()
|
|
272
|
+
before_index = 0
|
|
273
|
+
for line in after_lines:
|
|
274
|
+
if before_index < len(before_lines) and line == before_lines[before_index]:
|
|
275
|
+
before_index += 1
|
|
276
|
+
continue
|
|
277
|
+
if _visual_insert_line(line):
|
|
278
|
+
continue
|
|
279
|
+
return False
|
|
280
|
+
return before_index == len(before_lines)
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
def _visual_insert_line(line: str) -> bool:
|
|
284
|
+
stripped = line.strip()
|
|
285
|
+
return (
|
|
286
|
+
stripped == ""
|
|
287
|
+
or (stripped.startswith("![[") and stripped.endswith("]]"))
|
|
288
|
+
or (stripped.startswith("*Figura:") and "Fonte:" in stripped)
|
|
289
|
+
)
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
def _blocked(reason: str, next_action: str) -> JsonObject:
|
|
293
|
+
status = "blocked_vault_guard_required" if reason == "vault_guard_required" else "blocked"
|
|
294
|
+
return _PdfLibraryBlockedPayload(
|
|
295
|
+
schema=APPLY_SCHEMA,
|
|
296
|
+
workflow=WORKFLOW,
|
|
297
|
+
status=status,
|
|
298
|
+
blocked_reason=reason,
|
|
299
|
+
next_action=next_action,
|
|
300
|
+
).to_payload()
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def _sha256_text(text: str) -> str:
|
|
304
|
+
return hashlib.sha256(text.encode("utf-8")).hexdigest()
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def _sha256_file(path: Path) -> str:
|
|
308
|
+
digest = hashlib.sha256()
|
|
309
|
+
with path.open("rb") as handle:
|
|
310
|
+
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
|
|
311
|
+
digest.update(chunk)
|
|
312
|
+
return digest.hexdigest()
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
def _now() -> str:
|
|
316
|
+
return datetime.now(UTC).isoformat()
|