Generated banner for ThinkWiki
Generated logo for ThinkWiki

ThinkWiki

LLM wiki kit

First committed by wzdavid

Published in wzdavid/ThinkWiki. Publisher page

38 GitHub stars

What it is

Install once, talk to your agent. ThinkWiki turns documents and notes into a local Markdown wiki with inbox review, content knowledge graph, and browsable HTML workspace.
— the description by wzdavid

AutoGineer overview, from the brain’s own files

ThinkWiki is an agent-native skill designed to turn scattered information into a structured, durable local workspace. By integrating directly with skills-compatible AI agents, it allows users to manage their knowledge base through natural conversation rather than manual CLI commands. It supports a wide range of input formats, including PDFs, web pages, and various document types, organizing them into a system that features an inbox for review, a local viewer, and an interactive content knowledge graph. The system is built to be local-first, ensuring that Markdown files remain the source of truth, while providing HTML-based interfaces for visualizing relationships between topics, entities, and claims.

How it works

Memory model: LLM wiki

AutoGineer overview, from the brain’s own files

ThinkWiki functions as a skill installed into an AI agent's environment. It uses a set of Python scripts to manage a local directory of Markdown files. The system processes inputs through an inbox workflow, where content is reviewed before being ingested into the wiki. It generates a series of HTML outputs, including a workspace home, an inbox console, a page viewer, and a graph explorer. Users interact with these features by asking their agent to perform tasks like clipping content, building the knowledge graph, or serving the workspace via a local loopback server.

Folders
FolderRoleFiles
.githubagent-config1
docsdocs41
scriptsscripts34
templatestemplates13
teststests1

The agent reads

Requirements

Runtimes: Python

API keys (by name): THINKWIKI_EMBED_API_KEY, THINKWIKI_LLM_API_KEY

Set it up

  1. Clone the repository at the commit AutoGineer scanned:

    git clone https://github.com/wzdavid/ThinkWiki.git && cd ThinkWiki && git checkout 89f477d7a4095ba2ecf49ef9332e240c6f1208a1
  2. cd ThinkWiki && claude

  3. AutoGineer scanned these files at commit 89f477d.

  4. Not scanned by AutoGineerThe author’s README also describes installing it from a clone of its repository. Read its install section: AutoGineer did not scan that path. https://github.com/wzdavid/ThinkWiki/blob/89f477d7a4095ba2ecf49ef9332e240c6f1208a1/README.md#manual-install-optional
  5. First committed by wzdavid. Published in wzdavid/ThinkWiki. Licence: MIT License.

Usage

AutoGineer overview, from the brain’s own files

  • Initialize a new local wiki workspace
  • Clip web pages, files, or text into an inbox for later processing
  • Ingest reviewed items into the permanent wiki structure
  • Query the wiki for information with evidence-based responses
  • Generate and visualize a content knowledge graph
  • Serve the wiki workspace as a local HTML site for browser-based navigation
“Create a local knowledge base named Research Notes in my workspace.”
The agent initializes a new ThinkWiki workspace structure in the specified directory.
“Clip this article into the inbox: https://example.com/article”
The agent saves the content of the URL into the ThinkWiki inbox for later review.
“What did we already decide about context budgets?”
The agent searches the existing wiki pages and provides an answer supported by evidence from the knowledge base.
“Build the knowledge graph and open the workspace home in my browser.”
The agent generates the graph visualization and starts the local server to display the workspace in the browser.

Documentation by wzdavid

ThinkWiki

Image: Release Image: License Image: Python

ThinkWiki is an agent-native local knowledge base skill. Talk to your agent to turn scattered documents, web pages, notes, and conversations into a durable Markdown workspace — with inbox review, local browsing, and an interactive content knowledge graph you can open in a browser.

Chinese version: README.zh.md

Repository: https://github.com/wzdavid/ThinkWiki

Works With Skills-Compatible Agents

ThinkWiki follows the open Agent Skills format (SKILL.md + bundled scripts). If your agent can install skills and run local commands, it can run ThinkWiki.

You do not need to memorize CLI commands. Install the skill once, then create and manage your wiki entirely through conversation.

Agent / HostTypical use
Claude CodeInstall the repo as a skill, then ask it to build and query your wiki
OpenClawFull skill workflow; use serve + the built-in browser to open HTML outputs
TraeInstall under .trae/skills, then manage the wiki in chat
Hermes AgentLocal skill directory + shell execution
OpenAI Codex / Codex CLISkills-compatible coding agents with local file access
CursorAgent mode with project skills
Gemini CLISkills-compatible CLI agent
GitHub CopilotAgent / coding workflows in VS Code that support skills
Other skills hostsAny product that loads SKILL.md and can run python3 scripts/thinkwiki ... locally

Requirement: a local environment with Python 3. ThinkWiki bootstraps its own runtime on first use. Pure cloud chat without local file or shell access cannot run a local wiki.

Get Started: Install Through Conversation

Point your agent at this repository and ask it to install the skill for you.

Example prompt:

Please install the ThinkWiki skill from https://github.com/wzdavid/ThinkWiki , bootstrap the runtime, and run a health check to confirm everything works on my machine.

The agent will typically clone or copy the skill into your host's skills directory, run bootstrap, and verify capabilities with doctor. You should not need to run those steps yourself unless you prefer manual setup.

After installation, start using it immediately:

Create a local knowledge base named My Wiki in my workspace.

Clip this article into the inbox first: https://example.com/article

Import this PDF into the wiki: /path/to/file.pdf

Answer from my wiki: what did we already decide about context budgets?

Build the knowledge graph and open the workspace home in my browser.

The agent reads SKILL.md, maps your intent to stable ThinkWiki actions, and reports what changed — including paths to Markdown pages and HTML outputs.

Why ThinkWiki

  • Agent-first: users talk to an agent, not to a long list of commands.

  • Local-first: Markdown files remain the source of truth on your machine.

  • HTML-first: inbox, viewer, graph, and governance pages are real browsable workspaces.

  • Knowledge-first: the graph is a content knowledge graph — topics, concepts, entities, claims, and governance — not just a file-link map.

What It Produces

ThinkWiki generates a workspace that includes:

  • output/index.html: unified workspace home

  • output/inbox/index.html: inbox review console

  • output/viewer/index.html: local page browser

  • output/graph/index.html: interactive graph explorer

  • output/graph/report.html: graph governance report

  • output/graph/entity-merge-review.html: entity merge review page

  • output/graph/entity-merge-plan.html: entity merge dry-run preview when requested

Preview
Workspace Home (output/index.html)

The unified workspace home summarizes recent changes, recommended next actions, inbox backlog, and a graph snapshot so you can decide whether to read, ingest, or explore.

Image: ThinkWiki workspace home preview

Inbox Review (output/inbox/index.html)

Review clipped web pages, files, and notes before formal ingest. Items are grouped into ready, review, and weak so you can process the highest-value captures first.

Image: ThinkWiki inbox review preview

Local Viewer (output/viewer/index.html)

Browse the entire wiki by page type, confidence, and status. Open any page directly in the viewer without leaving the local HTML workspace.

Image: ThinkWiki local viewer preview

Knowledge Graph (output/graph/index.html)

Explore the content knowledge graph interactively. Switch between knowledge, document, and suggested views to inspect semantic relations and candidate edges.

Image: ThinkWiki knowledge graph preview

Graph Governance Report (output/graph/report.html)

Inspect graph health signals such as isolated pages, hub stubs, fragile bridges, suggested links, and entity merge candidates before making structural changes.

Image: ThinkWiki graph governance report preview

Everyday Commands (In Plain Language)

Once ThinkWiki is installed, these are the kinds of requests users make most often:

You sayThinkWiki does
Create a wiki called Research NotesInitializes a local workspace
Clip / save this URL or file to inboxCaptures content for later review
Ingest the ready inbox itemsPromotes reviewed captures into wiki pages
What does my wiki say about X?Evidence-first Q&A from existing pages
Save this answer as a query pagePersists high-value outputs
Show me the knowledge graphBuilds/refreshes graph HTML
Review entity merge candidatesSurfaces alias collisions for manual confirmation
Open the wiki workspace in my browserRuns serve and returns http://127.0.0.1:8765/index.html

Core Capabilities

  • Initialize a local wiki workspace

  • Clip web pages, files, or pasted text into an inbox for later review

  • Import Markdown, PDF, DOCX, XLSX, XLS, PPTX, web pages, and plain text

  • Answer questions from existing wiki pages with evidence-first behavior

  • Save high-value outputs as query, synthesis, decision, or concept pages

  • Build a content knowledge graph with knowledge, document, and suggested views

  • Generate entity governance outputs, including ambiguous alias review and deterministic merge flows

  • Check runtime health, workspace status, and graph governance state

Content Knowledge Graph

ThinkWiki v1.6.0 builds a schema v2 graph with default_view = knowledge.

The knowledge view can contain:

  • page-backed nodes such as source, topic, concept, decision, synthesis, query, and entity

  • claim nodes extracted from structured wiki content

  • semantic relations such as about, belongs_to, depends_on, asserts, supports, contradicts, and suggests_related_to

This means the graph is not limited to raw file references. It can represent knowledge structure, evidence structure, and entity governance directly.

Browse HTML Outputs

Agent chat UIs usually cannot render ThinkWiki HTML inline. Ask your agent to start the built-in loopback server when you want to inspect inbox, viewer, graph, or governance pages:

Start the ThinkWiki output server and give me the workspace URL.

Or run it yourself:

python3 scripts/thinkwiki serve --root /path/to/my-wiki

By default this serves <wiki-root>/output/ at http://127.0.0.1:8765/. Start from http://127.0.0.1:8765/index.html.

On OpenClaw, after serve is running, the agent can also open the workspace home with:

openclaw browser --browser-profile openclaw open http://127.0.0.1:8765/index.html

On other agents, open the same URL in your system browser, or use whatever browser tool the host provides.

Use serve --print-urls when you only need the URL list without starting the server.

Environment Variables

Remote AI features are disabled by default. Configure only what you need:

Content generation (crystallize, digest)

Requires all three variables. Any OpenAI-compatible chat completion endpoint works (MiniMax, OpenAI, DeepSeek, local Ollama, etc.).

VariableRequiredPurpose
THINKWIKI_LLM_API_KEYYes, to enableAPI key for the configured LLM provider
THINKWIKI_LLM_BASE_URLYes, to enableChat completions URL, e.g. https://api.openai.com/v1/chat/completions
THINKWIKI_LLM_MODELYes, to enableModel name, e.g. gpt-4o-mini or MiniMax-M3
THINKWIKI_LLM_TEMPERATURENoOptional temperature override

Without a complete LLM configuration, crystallize and digest use local heuristics only.

Entity embedding (entity-merge-review, graph-report, health)

Enabled when THINKWIKI_EMBED_API_KEY is set. Defaults to SiliconFlow BGE-M3; override the endpoint or model if needed.

VariableRequiredPurpose
THINKWIKI_EMBED_API_KEYYes, to enableAPI key for the embedding provider
THINKWIKI_EMBED_BASE_URLNoEmbedding URL(s), comma-separated; default https://api.siliconflow.cn/v1/embeddings
THINKWIKI_EMBED_MODELNoEmbedding model; default BAAI/bge-m3

Register a free SiliconFlow key at https://siliconflow.cn if you want the default embedding setup.

Manual Install (Optional)

If you prefer to set up the skill yourself instead of asking an agent:

git clone https://github.com/wzdavid/ThinkWiki ThinkWiki
cd ThinkWiki
python3 scripts/thinkwiki bootstrap
python3 scripts/thinkwiki doctor --repo-root .

Then install the folder into your agent's skills directory according to that host's documentation.

CLI Reference (For Agents And Power Users)

ThinkWiki keeps one stable entry point that agents call under the hood:

python3 scripts/thinkwiki <command> [args]

Common commands: init, clip, ingest, inbox, viewer, graph, graph-report, entity-merge-review, entity-merge-apply, serve, health, status, doctor.

See SKILL.md for the agent behavior contract and intent mapping.

Repository Docs

  • README.md: concise English project overview

  • README.zh.md: Chinese overview

  • SKILL.md: skill behavior contract for agent hosts

  • CHANGELOG.md: release history

License

MIT

scripts/entity_merge_apply.py

Shown as plain text.

#!/usr/bin/env python3
from __future__ import annotations

"""
ThinkWiki Script: entity_merge_apply

Purpose:
- Preview or apply a deterministic entity merge into a canonical entity page.

Usage:
- Prefer `python scripts/thinkwiki entity-merge-apply ...`.
- Run `python scripts/<script> --help` for direct CLI details when the file exposes its own arguments.
"""


import argparse
import json
import os
import subprocess
import sys
from html import escape
from pathlib import Path

from utils import (
    ambiguous_entity_merge_candidates,
    append_log,
    file_uri,
    find_repo_root,
    parse_frontmatter,
    read_text,
    today_str,
    unique_strings,
    write_output_home,
    write_text,
)

FRONTMATTER_ORDER = [
    "title",
    "type",
    "created",
    "updated",
    "summary",
    "canonical_entity",
    "sources",
    "aliases",
    "topics",
    "tags",
    "confidence",
    "status",
    "maturity",
]


def _load_graph(root: Path) -> dict[str, object]:
    graph_path = root / "output" / "graph" / "graph.json"
    if not graph_path.exists():
        raise SystemExit("Graph data not found. Run `python scripts/thinkwiki graph --root <wiki-root>` first.")
    try:
        return json.loads(graph_path.read_text(encoding="utf-8"))
    except (json.JSONDecodeError, OSError) as exc:
        raise SystemExit(f"Invalid graph data: {graph_path} ({exc})") from exc


def _knowledge_entity_nodes(graph: dict[str, object]) -> list[dict[str, object]]:
    views = graph.get("views", {})
    if isinstance(views, dict):
        knowledge = views.get("knowledge", {})
        if isinstance(knowledge, dict):
            nodes = knowledge.get("nodes", [])
            if isinstance(nodes, list):
                return [
                    node for node in nodes
                    if isinstance(node, dict) and str(node.get("type", "") or "") == "entity"
                ]
    return []


def _meta_list(value: object) -> list[str]:
    if isinstance(value, list):
        return [str(item).strip() for item in value if str(item).strip()]
    if value:
        return [str(value).strip()]
    return []


def _relative_link(from_page: Path, to_page: Path) -> str:
    return Path(os.path.relpath(to_page, start=from_page.parent)).as_posix()


def _serialize_frontmatter(meta: dict[str, object]) -> str:
    lines = ["---"]
    handled: set[str] = set()
    for key in FRONTMATTER_ORDER:
        if key not in meta:
            continue
        handled.add(key)
        value = meta[key]
        if isinstance(value, list):
            lines.append(f"{key}:")
            for item in value:
                item_text = str(item).strip()
                if item_text:
                    lines.append(f"  - {item_text}")
        else:
            lines.append(f"{key}: {str(value).strip()}")
    for key in sorted(meta.keys()):
        if key in handled:
            continue
        value = meta[key]
        if isinstance(value, list):
            lines.append(f"{key}:")
            for item in value:
                item_text = str(item).strip()
                if item_text:
                    lines.append(f"  - {item_text}")
        else:
            lines.append(f"{key}: {str(value).strip()}")
    lines.append("---")
    return "\n".join(lines)


def _write_page(path: Path, meta: dict[str, object], body: str) -> None:
    write_text(path, _serialize_frontmatter(meta) + "\n\n" + body.strip())


def _merged_entity_body(merged_title: str, canonical_title: str, merged_page: Path, canonical_page: Path, aliases: list[str]) -> str:
    canonical_link = _relative_link(merged_page, canonical_page)
    alias_lines = "\n".join(f"- {alias}" for alias in aliases) or "- (none)"
    return "\n".join([
        f"# {merged_title}",
        "",
        "## Summary",
        "",
        f"This entity page has been merged into [{canonical_title}]({canonical_link}).",
        "",
        "## Canonical Entity",
        "",
        f"- [{canonical_title}]({canonical_link})",
        "",
        "## Aliases",
        "",
        alias_lines,
    ])


def _resolve_entity_id(candidates: list[dict[str, object]], selector: str) -> str:
    needle = selector.strip()
    if not needle:
        raise SystemExit("Canonical entity selector cannot be empty.")
    normalized = needle.casefold()
    matched_ids = []
    for item in candidates:
        for entity_id, title in zip(item.get("entityIds", []), item.get("titles", [])):
            entity_id_text = str(entity_id)
            title_text = str(title)
            if normalized in {entity_id_text.casefold(), title_text.casefold(), Path(entity_id_text).stem.casefold()}:
                matched_ids.append(entity_id_text)
    matched_ids = unique_strings(matched_ids)
    if len(matched_ids) != 1:
        raise SystemExit(f"Could not resolve canonical entity selector uniquely: {selector}")
    return matched_ids[0]


def _run_follow_up(root: Path, script_name: str) -> None:
    subprocess.run(
        [sys.executable, str(Path(__file__).with_name(script_name)), "--root", str(root)],
        check=True,
        cwd=root.parent,
        capture_output=True,
        text=True,
    )


def _build_merge_plan(
    *,
    root: Path,
    identity_key: str,
    canonical_entity_id: str,
    candidate: dict[str, object],
) -> dict[str, object]:
    entity_ids = [str(item) for item in candidate.get("entityIds", []) if str(item).strip()]
    merged_entity_ids = [entity_id for entity_id in entity_ids if entity_id != canonical_entity_id]
    if not merged_entity_ids:
        raise SystemExit("Nothing to merge: canonical entity is the only candidate.")

    canonical_page = root / canonical_entity_id
    if not canonical_page.exists():
        raise SystemExit(f"Canonical entity page not found: {canonical_entity_id}")
    canonical_meta, canonical_body = parse_frontmatter(read_text(canonical_page))
    canonical_title = str(canonical_meta.get("title") or canonical_page.stem).strip() or canonical_page.stem

    canonical_before_aliases = _meta_list(canonical_meta.get("aliases", []))
    canonical_before_sources = _meta_list(canonical_meta.get("sources", []))
    canonical_before_topics = _meta_list(canonical_meta.get("topics", []))
    merged_sources = list(canonical_before_sources)
    merged_topics = list(canonical_before_topics)
    merged_aliases = list(canonical_before_aliases)
    merged_pages: list[dict[str, object]] = []

    for merged_entity_id in merged_entity_ids:
        merged_page = root / merged_entity_id
        if not merged_page.exists():
            raise SystemExit(f"Merged entity page not found: {merged_entity_id}")
        merged_meta, _merged_body = parse_frontmatter(read_text(merged_page))
        merged_title = str(merged_meta.get("title") or merged_page.stem).strip() or merged_page.stem
        merged_sources.extend(_meta_list(merged_meta.get("sources", [])))
        merged_topics.extend(_meta_list(merged_meta.get("topics", [])))
        merged_aliases.extend([merged_title, *_meta_list(merged_meta.get("aliases", []))])
        merged_after_meta = dict(merged_meta)
        merged_after_meta["type"] = "entity"
        merged_after_meta["updated"] = today_str()
        merged_after_meta["status"] = "merged"
        merged_after_meta["canonical_entity"] = canonical_entity_id
        merged_after_meta["maturity"] = "merged"
        merged_after_meta["summary"] = f"This entity has been merged into {canonical_title}."
        merged_after_meta["sources"] = unique_strings(_meta_list(merged_meta.get("sources", [])) + merged_sources)
        merged_after_meta["topics"] = unique_strings(_meta_list(merged_meta.get("topics", [])) + merged_topics)
        merged_after_meta["aliases"] = unique_strings([merged_title, *_meta_list(merged_meta.get("aliases", []))])
        merged_after_body = _merged_entity_body(
            merged_title=merged_title,
            canonical_title=canonical_title,
            merged_page=merged_page,
            canonical_page=canonical_page,
            aliases=_meta_list(merged_after_meta.get("aliases", [])),
        )
        merged_pages.append({
            "id": merged_entity_id,
            "title": merged_title,
            "beforeStatus": str(merged_meta.get("status") or "").strip(),
            "afterStatus": "merged",
            "canonicalEntity": canonical_entity_id,
            "beforeAliases": _meta_list(merged_meta.get("aliases", [])),
            "afterAliases": _meta_list(merged_after_meta.get("aliases", [])),
            "afterMeta": merged_after_meta,
            "afterBody": merged_after_body,
        })

    canonical_after_meta = dict(canonical_meta)
    canonical_after_meta["type"] = "entity"
    canonical_after_meta["updated"] = today_str()
    canonical_after_meta["status"] = "active"
    canonical_after_meta["maturity"] = str(canonical_after_meta.get("maturity") or "emerging").strip() or "emerging"
    canonical_after_meta.pop("canonical_entity", None)
    canonical_after_meta["sources"] = unique_strings(merged_sources)
    canonical_after_meta["topics"] = unique_strings(merged_topics)
    canonical_after_meta["aliases"] = [
        item for item in unique_strings(merged_aliases)
        if item.casefold() != canonical_title.casefold()
    ]
    if not str(canonical_after_meta.get("summary") or "").strip():
        canonical_after_meta["summary"] = f"{canonical_title} is an entity tracked in ThinkWiki."

    return {
        "generated_at": today_str(),
        "identityKey": identity_key,
        "summary": "This is a dry-run merge preview." if merged_pages else "No entity pages will be merged.",
        "canonical": {
            "id": canonical_entity_id,
            "title": canonical_title,
            "before": {
                "status": str(canonical_meta.get("status") or "").strip(),
                "aliases": canonical_before_aliases,
                "sources": canonical_before_sources,
                "topics": canonical_before_topics,
            },
            "after": {
                "status": str(canonical_after_meta.get("status") or "").strip(),
                "aliases": _meta_list(canonical_after_meta.get("aliases", [])),
                "sources": _meta_list(canonical_after_meta.get("sources", [])),
                "topics": _meta_list(canonical_after_meta.get("topics", [])),
            },
            "addedAliases": [
                item for item in _meta_list(canonical_after_meta.get("aliases", []))
                if item not in canonical_before_aliases
            ],
            "addedSources": [
                item for item in _meta_list(canonical_after_meta.get("sources", []))
                if item not in canonical_before_sources
            ],
            "addedTopics": [
                item for item in _meta_list(canonical_after_meta.get("topics", []))
                if item not in canonical_before_topics
            ],
            "afterMeta": canonical_after_meta,
            "afterBody": canonical_body or f"# {canonical_title}",
        },
        "mergedPages": merged_pages,
        "stats": {
            "mergedPageCount": len(merged_pages),
            "addedAliasCount": len([
                item for item in _meta_list(canonical_after_meta.get("aliases", []))
                if item not in canonical_before_aliases
            ]),
            "addedSourceCount": len([
                item for item in _meta_list(canonical_after_meta.get("sources", []))
                if item not in canonical_before_sources
            ]),
            "addedTopicCount": len([
                item for item in _meta_list(canonical_after_meta.get("topics", []))
                if item not in canonical_before_topics
            ]),
        },
    }


def render_plan_markdown(plan: dict[str, object]) -> str:
    canonical = plan["canonical"]
    stats = plan["stats"]
    assert isinstance(canonical, dict)
    assert isinstance(stats, dict)
    lines = [
        "# ThinkWiki Entity Merge Plan",
        "",
        f"- Generated: {plan['generated_at']}",
        f"- Identity Key: {plan['identityKey']}",
        f"- Canonical: {canonical['id']}",
        f"- Merged Pages: {stats['mergedPageCount']}",
        f"- Added Aliases: {stats['addedAliasCount']}",
        f"- Added Sources: {stats['addedSourceCount']}",
        f"- Added Topics: {stats['addedTopicCount']}",
        "",
        "## Canonical Preview",
        "",
        f"- Title: {canonical['title']}",
        f"- Added Aliases: {', '.join(canonical['addedAliases']) or 'none'}",
        f"- Added Sources: {', '.join(canonical['addedSources']) or 'none'}",
        f"- Added Topics: {', '.join(canonical['addedTopics']) or 'none'}",
        "",
        "## Merged Pages",
        "",
    ]
    merged_pages = plan.get("mergedPages", [])
    if isinstance(merged_pages, list) and merged_pages:
        for item in merged_pages:
            lines.extend([
                f"- {item['id']}",
                f"  Title: {item['title']}",
                f"  Status: {item['beforeStatus'] or 'active'} -> {item['afterStatus']}",
                f"  Canonical: {item['canonicalEntity']}",
                "",
            ])
    else:
        lines.append("- No merged pages")
    return "\n".join(lines).rstrip()


def render_plan_html(plan: dict[str, object]) -> str:
    canonical = plan["canonical"]
    stats = plan["stats"]
    assert isinstance(canonical, dict)
    assert isinstance(stats, dict)
    merged_pages = plan.get("mergedPages", [])
    merged_cards = []
    if isinstance(merged_pages, list):
        for item in merged_pages:
            merged_cards.append(
                "<div class='record-card'>"
                "<strong>{title}</strong>"
                "<div class='record-meta'>{entity_id}</div>"
                "<p>Status {before_status} -> {after_status}</p>"
                "<p>Canonical: {canonical}</p>"
                "</div>".format(
                    title=escape(str(item.get("title") or "")),
                    entity_id=escape(str(item.get("id") or "")),
                    before_status=escape(str(item.get("beforeStatus") or "active")),
                    after_status=escape(str(item.get("afterStatus") or "merged")),
                    canonical=escape(str(item.get("canonicalEntity") or "")),
                )
            )
    merged_html = "\n".join(merged_cards) if merged_cards else "<div class='empty'>No merged pages.</div>"
    return """<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>ThinkWiki Entity Merge Plan</title>
  <style>
    :root {{
      --bg: #0b1020;
      --panel: #121935;
      --text: #edf2ff;
      --muted: #a8b3cf;
      --border: rgba(255,255,255,0.1);
      --accent: #8ab4ff;
    }}
    * {{ box-sizing: border-box; }}
    body {{ margin: 0; min-height: 100vh; font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; background: linear-gradient(180deg, #0b1020 0%, #10172f 100%); color: var(--text); padding: 24px; }}
    .shell {{ width: min(1080px, 100%); margin: 0 auto; border: 1px solid var(--border); border-radius: 24px; background: rgba(9, 13, 28, 0.86); padding: 28px; }}
    .lead, p {{ color: var(--muted); line-height: 1.6; }}
    .meta {{ display: flex; gap: 10px; flex-wrap: wrap; margin: 16px 0 24px; }}
    .badge {{ border: 1px solid var(--border); border-radius: 999px; padding: 6px 12px; color: var(--muted); background: rgba(255,255,255,0.03); font-size: 0.92rem; }}
    .stats {{ display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 24px; }}
    .stat {{ border: 1px solid var(--border); background: var(--panel); border-radius: 18px; padding: 16px; }}
    .stat strong {{ display: block; font-size: 1.4rem; margin-bottom: 6px; }}
    .layout {{ display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }}
    .panel {{ border: 1px solid var(--border); background: rgba(255,255,255,0.02); border-radius: 18px; padding: 18px; }}
    .record-card {{ border: 1px solid var(--border); background: var(--panel); border-radius: 16px; padding: 14px; margin-bottom: 10px; }}
    .record-card strong {{ display: block; margin-bottom: 6px; }}
    .record-meta {{ color: var(--accent); font-size: 0.88rem; margin-bottom: 8px; }}
    .empty {{ border: 1px dashed var(--border); border-radius: 16px; padding: 14px; color: var(--muted); }}
    ul {{ margin: 0; padding-left: 18px; color: var(--muted); }}
    a {{ color: var(--accent); }}
    @media (max-width: 860px) {{ .layout {{ grid-template-columns: 1fr; }} }}
  </style>
</head>
<body>
  <main class="shell">
    <h1>ThinkWiki Entity Merge Plan</h1>
    <p class="lead">Dry-run preview for entity merge apply. Review the canonical changes before writing files.</p>
    <div class="meta">
      <span class="badge">Generated {generated_at}</span>
      <span class="badge">Identity Key {identity_key}</span>
      <span class="badge"><a href="entity-merge-review.html">Open Entity Merge Review</a></span>
      <span class="badge"><a href="../index.html">Open Workspace Home</a></span>
    </div>
    <section class="stats">
      <div class="stat"><strong>{merged_page_count}</strong><span>Merged Pages</span></div>
      <div class="stat"><strong>{added_alias_count}</strong><span>Added Aliases</span></div>
      <div class="stat"><strong>{added_source_count}</strong><span>Added Sources</span></div>
      <div class="stat"><strong>{added_topic_count}</strong><span>Added Topics</span></div>
    </section>
    <div class="layout">
      <section class="panel">
        <h2>Canonical Preview</h2>
        <p>{canonical_title}</p>
        <ul>
          <li>Canonical Page: {canonical_id}</li>
          <li>Added Aliases: {added_aliases}</li>
          <li>Added Sources: {added_sources}</li>
          <li>Added Topics: {added_topics}</li>
        </ul>
      </section>
      <section class="panel">
        <h2>Merged Pages</h2>
        {merged_cards}
      </section>
    </div>
  </main>
</body>
</html>
""".format(
        generated_at=escape(str(plan.get("generated_at") or "")),
        identity_key=escape(str(plan.get("identityKey") or "")),
        merged_page_count=escape(str(stats.get("mergedPageCount", 0) or 0)),
        added_alias_count=escape(str(stats.get("addedAliasCount", 0) or 0)),
        added_source_count=escape(str(stats.get("addedSourceCount", 0) or 0)),
        added_topic_count=escape(str(stats.get("addedTopicCount", 0) or 0)),
        canonical_title=escape(str(canonical.get("title") or "")),
        canonical_id=escape(str(canonical.get("id") or "")),
        added_aliases=escape(", ".join(canonical.get("addedAliases", [])) or "none"),
        added_sources=escape(", ".join(canonical.get("addedSources", [])) or "none"),
        added_topics=escape(", ".join(canonical.get("addedTopics", [])) or "none"),
        merged_cards=merged_html,
    )


def main() -> int:
    parser = argparse.ArgumentParser(description="Apply a reviewed entity merge to canonicalize ambiguous entity pages.")
    parser.add_argument("--root", default=".", help="Wiki root path")
    parser.add_argument("--identity-key", required=True, help="Identity key from entity-merge-review")
    parser.add_argument("--canonical", required=True, help="Canonical entity page id or title")
    parser.add_argument("--dry-run", action="store_true", help="Preview the merge without writing entity pages")
    args = parser.parse_args()

    root = find_repo_root(Path(args.root))
    graph = _load_graph(root)
    entity_nodes = _knowledge_entity_nodes(graph)
    candidates, _ambiguous_entity_count = ambiguous_entity_merge_candidates(entity_nodes)
    selected = [
        item for item in candidates
        if str(item.get("identityKey") or "").casefold() == args.identity_key.strip().casefold()
    ]
    if len(selected) != 1:
        raise SystemExit(f"Identity key not found or ambiguous in current review: {args.identity_key}")
    candidate = selected[0]
    entity_ids = [str(item) for item in candidate.get("entityIds", []) if str(item).strip()]
    if len(entity_ids) < 2:
        raise SystemExit("Selected identity key does not contain enough entity pages to merge.")

    canonical_entity_id = _resolve_entity_id(selected, args.canonical)
    plan = _build_merge_plan(
        root=root,
        identity_key=args.identity_key.strip(),
        canonical_entity_id=canonical_entity_id,
        candidate=candidate,
    )
    canonical = plan["canonical"]
    merged_pages = plan["mergedPages"]
    assert isinstance(canonical, dict)
    assert isinstance(merged_pages, list)

    if args.dry_run:
        plan_dir = root / "output" / "graph"
        plan_json_path = plan_dir / "entity-merge-plan.json"
        plan_md_path = plan_dir / "entity-merge-plan.md"
        plan_html_path = plan_dir / "entity-merge-plan.html"
        write_text(plan_json_path, json.dumps(plan, ensure_ascii=False, indent=2))
        write_text(plan_md_path, render_plan_markdown(plan))
        write_text(plan_html_path, render_plan_html(plan))
        output_home = write_output_home(root)
        append_log(root, f"[{today_str()}] entity-merge-plan | {args.identity_key}", [
            f"- canonical: {canonical_entity_id}",
            *[f"- preview merge: {item['id']}" for item in merged_pages],
            "- plan html: output/graph/entity-merge-plan.html",
            "- hub: output/index.html",
        ])
        print("# ThinkWiki Entity Merge Plan")
        print("")
        print(f"- Root: {root}")
        print(f"- Identity Key: {args.identity_key}")
        print(f"- Canonical: {canonical_entity_id}")
        print(f"- Merged Pages: {plan['stats']['mergedPageCount']}")
        print(f"- Added Aliases: {plan['stats']['addedAliasCount']}")
        print(f"- Added Sources: {plan['stats']['addedSourceCount']}")
        print(f"- Added Topics: {plan['stats']['addedTopicCount']}")
        print("")
        for item in merged_pages:
            print(f"- preview merge: {item['id']} -> {canonical_entity_id}")
        print("")
        print("Entity merge plan: output/graph/entity-merge-plan.html")
        print(f"Entity merge plan URI: {file_uri(plan_html_path)}")
        print("Entity merge plan markdown: output/graph/entity-merge-plan.md")
        print("Entity merge plan data: output/graph/entity-merge-plan.json")
        print("Output hub: output/index.html")
        print(f"Output hub URI: {file_uri(output_home)}")
        return 0

    canonical_page = root / str(canonical["id"])
    _write_page(canonical_page, dict(canonical["afterMeta"]), str(canonical["afterBody"]))
    merged_page_paths: list[str] = []
    for item in merged_pages:
        merged_page = root / str(item["id"])
        _write_page(merged_page, dict(item["afterMeta"]), str(item["afterBody"]))
        merged_page_paths.append(str(item["id"]))

    _run_follow_up(root, "build_viewer.py")
    _run_follow_up(root, "build_graph.py")
    _run_follow_up(root, "graph_report.py")
    _run_follow_up(root, "entity_merge_review.py")

    append_log(root, f"[{today_str()}] entity-merge-apply | {args.identity_key}", [
        f"- canonical: {canonical_entity_id}",
        *[f"- merged: {item}" for item in merged_page_paths],
        "- viewer: output/viewer/index.html",
        "- graph: output/graph/index.html",
        "- graph report: output/graph/report.html",
        "- merge review: output/graph/entity-merge-review.html",
    ])
    print("# ThinkWiki Entity Merge Apply")
    print("")
    print(f"- Root: {root}")
    print(f"- Identity Key: {args.identity_key}")
    print(f"- Canonical: {canonical_entity_id}")
    print(f"- Merged Pages: {len(merged_page_paths)}")
    print("")
    for merged_page in merged_page_paths:
        print(f"- merged: {merged_page} -> {canonical_entity_id}")
    print("")
    print("Viewer: output/viewer/index.html")
    print("Graph: output/graph/index.html")
    print("Graph report: output/graph/report.html")
    print("Entity merge review: output/graph/entity-merge-review.html")
    print(f"Entity merge review URI: {file_uri(root / 'output' / 'graph' / 'entity-merge-review.html')}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
98 files · 5 not served here

Advantages

  • Enables natural language management of a knowledge base without requiring the user to memorize complex CLI commands. evidence: README.md
  • Maintains Markdown files as the local source of truth, ensuring data portability and ownership. evidence: README.md
  • Provides a rich, interactive content knowledge graph that maps semantic relations like claims and entities rather than just file links. evidence: README.md

Limitations

  • Requires a local environment with Python 3 and cannot function in pure cloud-based chat environments without local file or shell access. evidence: README.md
  • Agent chat UIs typically cannot render the generated HTML interfaces inline, requiring the use of a separate browser or a local loopback server. evidence: README.md

Security

Risk level: No risk found

This brain runs

Agent instructions that run or fetch something (5)

  • SKILL.md: If the user wants a new workspace, run `init`.
  • SKILL.md: After `viewer`, `graph`, `inbox`, or `graph-report`, run `serve` when the user wants to browse the results.
  • SKILL.md: If a long-running `serve` process is not practical in the current session, run `serve --print-urls` and tell the user to start `serve` locally, then open the printed workspace URL.
  • SKILL.md: After starting `serve`, give the user the workspace home URL first (`http://127.0.0.1:8765/index.html` by default) and, in OpenClaw, offer to open it with `openclaw browser --browser-profile openclaw open <url>`. · uses the network
  • SKILL.md: If the host cannot keep a long-running process alive, run `serve --print-urls` to show the URLs and ask the user to start `serve` in a local terminal or background job.

Attribution and licence

First committed by
GitHub avatar of wzdavidwzdavid
Publisher
Wei Zhang (Community)
Source
wzdavid/ThinkWiki · commit 89f477d
Licence
MIT License (files served here)
  • ATTRIBUTION.md (written by AutoGineer)
Synced
Contributors
wzdavid

AutoGineer is not affiliated with or endorsed by wzdavid.

Install from source

5 of its files are not served here, so there is no ZIP. wzdavid/ThinkWiki at 89f477d