contextplate 0.1.0__py3-none-any.whl

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.
Files changed (49) hide show
  1. contextplate/__init__.py +7 -0
  2. contextplate/__main__.py +5 -0
  3. contextplate/adapters/__init__.py +50 -0
  4. contextplate/adapters/aider.py +28 -0
  5. contextplate/adapters/base.py +85 -0
  6. contextplate/adapters/claude.py +181 -0
  7. contextplate/adapters/codex.py +119 -0
  8. contextplate/adapters/copilot.py +57 -0
  9. contextplate/adapters/core.py +197 -0
  10. contextplate/adapters/cursor.py +55 -0
  11. contextplate/adapters/gemini.py +31 -0
  12. contextplate/adopt.py +596 -0
  13. contextplate/cli.py +308 -0
  14. contextplate/config.py +306 -0
  15. contextplate/doctor.py +147 -0
  16. contextplate/frontmatter.py +226 -0
  17. contextplate/globs.py +84 -0
  18. contextplate/model.py +185 -0
  19. contextplate/presets.py +165 -0
  20. contextplate/scaffold.py +162 -0
  21. contextplate/templates/check.sh +44 -0
  22. contextplate/templates/hooks/format.py +74 -0
  23. contextplate/templates/hooks/guard.py +121 -0
  24. contextplate/templates/hooks/session_context.py +79 -0
  25. contextplate/templates/scaffold/AGENTS.md +29 -0
  26. contextplate/templates/scaffold/agents/agents/researcher.md +16 -0
  27. contextplate/templates/scaffold/agents/agents/reviewer.md +20 -0
  28. contextplate/templates/scaffold/agents/agents/test-runner.md +17 -0
  29. contextplate/templates/scaffold/agents/rules/ai-assets.md +12 -0
  30. contextplate/templates/scaffold/agents/rules/docs.md +11 -0
  31. contextplate/templates/scaffold/agents/skills/adr/SKILL.md +12 -0
  32. contextplate/templates/scaffold/agents/skills/adr/template.md +21 -0
  33. contextplate/templates/scaffold/agents/skills/handoff/SKILL.md +17 -0
  34. contextplate/templates/scaffold/agents/skills/plan-task/SKILL.md +15 -0
  35. contextplate/templates/scaffold/agents/skills/plan-task/template.md +28 -0
  36. contextplate/templates/scaffold/ai_app/data/README.md +5 -0
  37. contextplate/templates/scaffold/ai_app/evals/README.md +7 -0
  38. contextplate/templates/scaffold/ai_app/prompts/README.md +17 -0
  39. contextplate/templates/scaffold/docs/agent-context.md +42 -0
  40. contextplate/templates/scaffold/docs/architecture.md +27 -0
  41. contextplate/templates/scaffold/docs/decisions/0001-record-architecture-decisions.md +22 -0
  42. contextplate/templates/scaffold/docs/glossary.md +7 -0
  43. contextplate/templates/scaffold/work/handoff.md +22 -0
  44. contextplate/writer.py +396 -0
  45. contextplate-0.1.0.dist-info/METADATA +201 -0
  46. contextplate-0.1.0.dist-info/RECORD +49 -0
  47. contextplate-0.1.0.dist-info/WHEEL +4 -0
  48. contextplate-0.1.0.dist-info/entry_points.txt +3 -0
  49. contextplate-0.1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,197 @@
1
+ """Harness-neutral outputs: AGENTS.md blocks, .gitignore, check script, hook runtime."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ from importlib import resources
7
+
8
+ from ..writer import Output
9
+ from .base import Context, one_line
10
+
11
+ COMMAND_LABELS = [
12
+ ("install", "Install"),
13
+ ("dev", "Run locally"),
14
+ ("check", "Check everything (lint, types, tests)"),
15
+ ("lint", "Lint"),
16
+ ("typecheck", "Type check"),
17
+ ("test", "Test"),
18
+ ("format", "Format"),
19
+ ]
20
+
21
+
22
+ def template(*parts: str) -> str:
23
+ return resources.files("contextplate").joinpath("templates", *parts).read_text(encoding="utf-8")
24
+
25
+
26
+ def _shell_quote(s: str) -> str:
27
+ return "'" + s.replace("'", "'\\''") + "'"
28
+
29
+
30
+ def commands_block(ctx: Context) -> str:
31
+ cfg = ctx.config
32
+ cmds = dict(cfg.commands)
33
+ cmds["check"] = cfg.check_command
34
+ lines = ["## Commands", ""]
35
+ for key, label in COMMAND_LABELS:
36
+ if cmds.get(key):
37
+ lines.append(f"- {label}: `{cmds[key]}`")
38
+ if len(lines) == 2:
39
+ lines.append(
40
+ "- None configured yet. Add them under `[commands]` in `.agents/contextplate.toml`, then run `ctxplate sync`."
41
+ )
42
+ return "\n".join(lines)
43
+
44
+
45
+ def _paths_hint(paths: list[str]) -> str:
46
+ shown = ", ".join(f"`{p}`" for p in paths[:3])
47
+ return shown + (", ..." if len(paths) > 3 else "")
48
+
49
+
50
+ def index_block(ctx: Context) -> str:
51
+ """Pointers for tools without native rules, skills, or subagents.
52
+
53
+ compact (default): names and paths only, because tools with native support
54
+ already inject skill and role descriptions. full: include descriptions.
55
+ """
56
+ cfg = ctx.config
57
+ full = cfg.index == "full"
58
+ lines = ["## Load on demand", "", "Read only what your task needs.", ""]
59
+ for r in ctx.rules:
60
+ if r.always:
61
+ lines.append(f"- Always follow `{r.source}`.")
62
+ else:
63
+ lines.append(f"- `{r.source}` when working on {_paths_hint(r.expanded_paths)}")
64
+ if ctx.skills:
65
+ if full:
66
+ lines.append("- Skills (`.agents/skills/<name>/SKILL.md`):")
67
+ lines.extend(f" - `{s.name}`: {one_line(s.description)}" for s in ctx.skills)
68
+ else:
69
+ lines.append(
70
+ "- Skills (`.agents/skills/<name>/SKILL.md`): "
71
+ + ", ".join(f"`{s.name}`" for s in ctx.skills)
72
+ )
73
+ if ctx.agents:
74
+ if full:
75
+ lines.append(
76
+ "- Roles to delegate to (`.agents/agents/<name>.md`; without subagents, follow the file yourself):"
77
+ )
78
+ lines.extend(f" - `{a.name}`: {one_line(a.description)}" for a in ctx.agents)
79
+ else:
80
+ lines.append(
81
+ "- Roles to delegate to (`.agents/agents/<name>.md`; without subagents, follow the file yourself): "
82
+ + ", ".join(f"`{a.name}`" for a in ctx.agents)
83
+ )
84
+ docs = [
85
+ f"`{p}`"
86
+ for p in ("docs/architecture.md", "docs/glossary.md", "docs/decisions/")
87
+ if (ctx.root / p).exists()
88
+ ]
89
+ if docs:
90
+ lines.append("- Docs: " + ", ".join(docs))
91
+ if cfg.harnesses:
92
+ lines.append(
93
+ "- Per-tool files (`.claude/`, `.cursor/`, `.github/instructions/`, ...) are generated: edit `.agents/`, then run `ctxplate sync`."
94
+ )
95
+ return "\n".join(lines)
96
+
97
+
98
+ def gitignore_block(ctx: Context) -> str:
99
+ cfg = ctx.config
100
+ lines = ["# secrets", ".env", ".env.*", "!.env.example"]
101
+ if "claude" in cfg.harnesses:
102
+ lines += [
103
+ "# personal agent settings",
104
+ "CLAUDE.local.md",
105
+ ".claude/settings.local.json",
106
+ ".claude/agent-memory-local/",
107
+ ".claude/worktrees/",
108
+ ]
109
+ if cfg.module("work"):
110
+ lines += ["# throwaway agent files", "work/scratch/*", "!work/scratch/.gitkeep"]
111
+ if cfg.module("ai_app"):
112
+ lines += [
113
+ "# AI app data and eval outputs",
114
+ "data/raw/*",
115
+ "!data/raw/.gitkeep",
116
+ "data/processed/",
117
+ "evals/results/*",
118
+ "!evals/results/.gitkeep",
119
+ ]
120
+ return "\n".join(lines)
121
+
122
+
123
+ def check_script(ctx: Context) -> str:
124
+ cfg = ctx.config
125
+ steps = [(k, cfg.commands[k]) for k in ("lint", "typecheck", "test") if cfg.commands.get(k)]
126
+ step_lines = "\n".join(f"step {k} {_shell_quote(v)}" for k, v in steps)
127
+ if not step_lines:
128
+ step_lines = 'echo "No checks configured. Set [commands] lint, typecheck, test in .agents/contextplate.toml, then run ctxplate sync."'
129
+ return template("check.sh").replace("{{steps}}", step_lines)
130
+
131
+
132
+ def hooks_runtime(ctx: Context, owned_paths: list[str]) -> list[Output]:
133
+ cfg = ctx.config
134
+ outs = [
135
+ Output(f".agents/hooks/{name}", content=template("hooks", name), executable=True)
136
+ for name in ("guard.py", "session_context.py", "format.py")
137
+ ]
138
+ session = {"max_lines": 40}
139
+ if cfg.module("work"):
140
+ session.update({"handoff": "work/handoff.md", "plans": "work/plans"})
141
+ hook_cfg = {
142
+ "no_edit": cfg.no_edit,
143
+ "generated": sorted(
144
+ set(owned_paths) | {o.path for o in outs} | {".agents/hooks/config.json"}
145
+ ),
146
+ "format": cfg.format,
147
+ "session": session,
148
+ }
149
+ outs.append(Output(".agents/hooks/config.json", content=json.dumps(hook_cfg, indent=2) + "\n"))
150
+ return outs
151
+
152
+
153
+ def generated_prefixes(owned_paths: list[str]) -> list[str]:
154
+ """Collapse owned output paths into directory prefixes for ignore files."""
155
+ out: list[str] = []
156
+ for p in sorted(owned_paths):
157
+ parts = p.split("/")
158
+ entry = "/".join(parts[:2]) + "/" if len(parts) > 2 else p
159
+ if entry not in out:
160
+ out.append(entry)
161
+ return out
162
+
163
+
164
+ def formatter_ignore(path: str, owned_paths: list[str]) -> Output:
165
+ body = "# files generated by ctxplate (formatting them only creates drift)\n" + "\n".join(
166
+ generated_prefixes(owned_paths)
167
+ )
168
+ return Output(path, strategy="block", block_id="generated", content=body)
169
+
170
+
171
+ def outputs(ctx: Context) -> list[Output]:
172
+ cfg = ctx.config
173
+ outs = [
174
+ Output(
175
+ "AGENTS.md",
176
+ strategy="block",
177
+ block_id="commands",
178
+ content=commands_block(ctx),
179
+ position="bottom",
180
+ )
181
+ ]
182
+ if cfg.index != "off":
183
+ outs.append(
184
+ Output(
185
+ "AGENTS.md",
186
+ strategy="block",
187
+ block_id="index",
188
+ content=index_block(ctx),
189
+ position="bottom",
190
+ )
191
+ )
192
+ outs += [
193
+ Output(".gitignore", strategy="block", block_id="agents", content=gitignore_block(ctx)),
194
+ ]
195
+ if cfg.module("check_script"):
196
+ outs.append(Output("scripts/check.sh", content=check_script(ctx), executable=True))
197
+ return outs
@@ -0,0 +1,55 @@
1
+ """Cursor: reads AGENTS.md and .agents/skills natively.
2
+
3
+ Generates .cursor/rules/*.mdc (description, globs, alwaysApply),
4
+ .cursor/agents/*.md subagents, and .cursor/mcp.json.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ from .. import frontmatter
10
+ from ..writer import Output
11
+ from .base import Context, mcp_cursor, one_line
12
+
13
+
14
+ def mdc(r) -> str:
15
+ # Cursor's own editor writes globs unquoted and comma-separated; match that.
16
+ globs = ",".join(r.expanded_paths)
17
+ head = (
18
+ "---\n"
19
+ f"description: {frontmatter.fmt_scalar(one_line(r.description))}\n"
20
+ f"globs: {globs}\n"
21
+ f"alwaysApply: {'true' if r.always else 'false'}\n"
22
+ "---\n"
23
+ )
24
+ return head + "\n" + r.body.lstrip("\n")
25
+
26
+
27
+ def outputs(ctx: Context) -> list[Output]:
28
+ outs = [
29
+ Output(f".cursor/rules/{r.name}.mdc", content=mdc(r), owner="cursor") for r in ctx.rules
30
+ ]
31
+ for a in ctx.agents:
32
+ meta: dict = {"name": a.name, "description": a.description}
33
+ model = ctx.model_for("cursor", a.model)
34
+ if model:
35
+ meta["model"] = model
36
+ if a.read_only:
37
+ meta["readonly"] = True
38
+ meta.update(a.overrides.get("cursor", {}))
39
+ outs.append(
40
+ Output(
41
+ f".cursor/agents/{a.name}.md",
42
+ content=frontmatter.compose(meta, a.body),
43
+ owner="cursor",
44
+ )
45
+ )
46
+ if ctx.config.mcp:
47
+ outs.append(
48
+ Output(
49
+ ".cursor/mcp.json",
50
+ strategy="json",
51
+ data={"mcpServers": {n: mcp_cursor(s) for n, s in ctx.config.mcp.items()}},
52
+ owner="cursor",
53
+ )
54
+ )
55
+ return outs
@@ -0,0 +1,31 @@
1
+ """Gemini CLI: point context.fileName at AGENTS.md; skills come from .agents/skills.
2
+
3
+ Generates .gemini/settings.json (merged) and .gemini/agents/*.md subagents.
4
+ """
5
+
6
+ from __future__ import annotations
7
+
8
+ from .. import frontmatter
9
+ from ..writer import Output
10
+ from .base import Context, mcp_gemini
11
+
12
+
13
+ def outputs(ctx: Context) -> list[Output]:
14
+ data: dict = {"context": {"fileName": ["AGENTS.md", "GEMINI.md"]}}
15
+ if ctx.config.mcp:
16
+ data["mcpServers"] = {n: mcp_gemini(s) for n, s in ctx.config.mcp.items()}
17
+ outs = [Output(".gemini/settings.json", strategy="json", data=data, owner="gemini")]
18
+ for a in ctx.agents:
19
+ meta: dict = {"name": a.name, "description": a.description}
20
+ model = ctx.model_for("gemini", a.model)
21
+ if model:
22
+ meta["model"] = model
23
+ meta.update(a.overrides.get("gemini", {}))
24
+ outs.append(
25
+ Output(
26
+ f".gemini/agents/{a.name}.md",
27
+ content=frontmatter.compose(meta, a.body),
28
+ owner="gemini",
29
+ )
30
+ )
31
+ return outs