pytest-copilot 2.0.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.
- pytest_copilot/__init__.py +133 -0
- pytest_copilot/copilot/__init__.py +47 -0
- pytest_copilot/copilot/_command_job.py +109 -0
- pytest_copilot/copilot/api.py +12 -0
- pytest_copilot/copilot/client.py +58 -0
- pytest_copilot/copilot/commands.py +265 -0
- pytest_copilot/copilot/config.py +133 -0
- pytest_copilot/copilot/contracts.py +222 -0
- pytest_copilot/copilot/controls.py +230 -0
- pytest_copilot/copilot/events.py +795 -0
- pytest_copilot/copilot/fixtures.py +289 -0
- pytest_copilot/copilot/personas.py +607 -0
- pytest_copilot/copilot/requests.py +350 -0
- pytest_copilot/copilot/result.py +309 -0
- pytest_copilot/copilot/runner.py +266 -0
- pytest_copilot/copilot/skills.py +104 -0
- pytest_copilot/copilot/task.py +546 -0
- pytest_copilot/core/__init__.py +77 -0
- pytest_copilot/core/errors.py +44 -0
- pytest_copilot/core/evals.py +521 -0
- pytest_copilot/core/plugin.py +403 -0
- pytest_copilot/core/prompt.py +167 -0
- pytest_copilot/core/result.py +543 -0
- pytest_copilot/core/serialization.py +128 -0
- pytest_copilot/core/skill.py +406 -0
- pytest_copilot/core/skill_evals.py +148 -0
- pytest_copilot/core/skill_grading.py +78 -0
- pytest_copilot/core/validation.py +83 -0
- pytest_copilot/execution/__init__.py +15 -0
- pytest_copilot/execution/cost.py +159 -0
- pytest_copilot/execution/rate_limiter.py +177 -0
- pytest_copilot/execution/servers.py +442 -0
- pytest_copilot/execution/skill_tools.py +228 -0
- pytest_copilot/fixtures/__init__.py +18 -0
- pytest_copilot/fixtures/factories.py +44 -0
- pytest_copilot/fixtures/iteration.py +16 -0
- pytest_copilot/onboarding.py +413 -0
- pytest_copilot/plugin.py +562 -0
- pytest_copilot/plugin_options.py +32 -0
- pytest_copilot/plugin_report.py +18 -0
- pytest_copilot/reporting/__init__.py +16 -0
- pytest_copilot/reporting/collector.py +203 -0
- pytest_copilot/reporting/generator.py +55 -0
- pytest_copilot/reporting/schema.py +5 -0
- pytest_copilot/testing/__init__.py +17 -0
- pytest_copilot/testing/banking.py +479 -0
- pytest_copilot/testing/banking_mcp.py +167 -0
- pytest_copilot/testing/todo.py +380 -0
- pytest_copilot/testing/todo_mcp.py +156 -0
- pytest_copilot/testing/types.py +31 -0
- pytest_copilot-2.0.0.dist-info/METADATA +177 -0
- pytest_copilot-2.0.0.dist-info/RECORD +55 -0
- pytest_copilot-2.0.0.dist-info/WHEEL +4 -0
- pytest_copilot-2.0.0.dist-info/entry_points.txt +5 -0
- pytest_copilot-2.0.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
"""Plugin loading infrastructure for testing plugin directories.
|
|
2
|
+
|
|
3
|
+
A "plugin" is a directory with a ``plugin.json`` manifest that bundles
|
|
4
|
+
custom agents, skills, MCP servers, hooks, instructions, and extensions.
|
|
5
|
+
Both GitHub Copilot CLI and Claude Code use this format (with minor differences).
|
|
6
|
+
|
|
7
|
+
Supports three directory layouts:
|
|
8
|
+
|
|
9
|
+
1. **Standalone plugin** — directory with ``plugin.json`` at root.
|
|
10
|
+
2. **GitHub Copilot project** — ``.github/`` directory with ``agents/``,
|
|
11
|
+
``copilot-instructions.md``, etc.
|
|
12
|
+
3. **Claude Code project** — ``.claude/`` directory with ``agents/``,
|
|
13
|
+
``CLAUDE.md``, ``commands/``, etc.
|
|
14
|
+
|
|
15
|
+
Example::
|
|
16
|
+
|
|
17
|
+
from pytest_copilot.core.plugin import load_plugin
|
|
18
|
+
from pytest_copilot.copilot.task import CopilotTask
|
|
19
|
+
|
|
20
|
+
plugin = load_plugin("my-plugin/")
|
|
21
|
+
# → Plugin(metadata=PluginMetadata(name="my-plugin"), ...)
|
|
22
|
+
|
|
23
|
+
agent = CopilotTask.from_plugin(
|
|
24
|
+
"my-plugin/",
|
|
25
|
+
model="gpt-5.6-sol",
|
|
26
|
+
)
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
from __future__ import annotations
|
|
30
|
+
|
|
31
|
+
import json
|
|
32
|
+
from dataclasses import dataclass, field
|
|
33
|
+
from pathlib import Path
|
|
34
|
+
from typing import Any
|
|
35
|
+
|
|
36
|
+
from pytest_copilot.core.evals import load_custom_agent, load_instruction_file
|
|
37
|
+
from pytest_copilot.core.skill import Skill
|
|
38
|
+
from pytest_copilot.core.validation import _string_field, _validate_mcp_servers
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
@dataclass(slots=True, frozen=True)
|
|
42
|
+
class HookDefinition:
|
|
43
|
+
"""A lifecycle hook from a plugin's hooks configuration.
|
|
44
|
+
|
|
45
|
+
Hooks allow plugins to execute shell commands at specific lifecycle events.
|
|
46
|
+
|
|
47
|
+
Example ``hooks.json``::
|
|
48
|
+
|
|
49
|
+
[
|
|
50
|
+
{
|
|
51
|
+
"event": "tool.execution_complete",
|
|
52
|
+
"command": "npm run lint",
|
|
53
|
+
"pattern": "*.ts"
|
|
54
|
+
}
|
|
55
|
+
]
|
|
56
|
+
"""
|
|
57
|
+
|
|
58
|
+
event: str # e.g., "tool.execution_complete", "session.start"
|
|
59
|
+
command: str # Shell command to execute
|
|
60
|
+
pattern: str = "" # Optional file pattern filter
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@dataclass(slots=True, frozen=True)
|
|
64
|
+
class PluginMetadata:
|
|
65
|
+
"""Plugin manifest metadata.
|
|
66
|
+
|
|
67
|
+
Extracted from ``plugin.json`` or inferred from the directory name.
|
|
68
|
+
"""
|
|
69
|
+
|
|
70
|
+
name: str
|
|
71
|
+
version: str = ""
|
|
72
|
+
description: str = ""
|
|
73
|
+
author: str = ""
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@dataclass(slots=True, frozen=True)
|
|
77
|
+
class Plugin:
|
|
78
|
+
"""A loaded plugin with all its components resolved.
|
|
79
|
+
|
|
80
|
+
Created by :func:`load_plugin`. Contains everything discovered from
|
|
81
|
+
the plugin directory: agents, skills, MCP servers, hooks, instructions,
|
|
82
|
+
and extensions.
|
|
83
|
+
"""
|
|
84
|
+
|
|
85
|
+
metadata: PluginMetadata
|
|
86
|
+
path: Path
|
|
87
|
+
agents: list[dict[str, Any]] = field(default_factory=list)
|
|
88
|
+
skills: list[Skill] = field(default_factory=list)
|
|
89
|
+
mcp_servers: dict[str, dict[str, Any]] = field(default_factory=dict)
|
|
90
|
+
hooks: list[HookDefinition] = field(default_factory=list)
|
|
91
|
+
instructions: str = ""
|
|
92
|
+
extensions: list[Path] = field(default_factory=list)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def load_plugin(path: str | Path) -> Plugin:
|
|
96
|
+
"""Load a plugin from a directory containing plugin.json.
|
|
97
|
+
|
|
98
|
+
Supports both GitHub Copilot CLI and Claude Code plugin formats.
|
|
99
|
+
Discovers and loads: custom agents, skills, MCP servers, hooks,
|
|
100
|
+
instructions, and extensions.
|
|
101
|
+
|
|
102
|
+
For ``.github/`` directories, discovers agents from ``agents/`` and
|
|
103
|
+
instructions from ``copilot-instructions.md``.
|
|
104
|
+
|
|
105
|
+
For ``.claude/`` directories, discovers agents from ``agents/`` and
|
|
106
|
+
instructions from ``CLAUDE.md``.
|
|
107
|
+
|
|
108
|
+
Args:
|
|
109
|
+
path: Path to the plugin directory (must contain plugin.json,
|
|
110
|
+
or be a ``.github/`` / ``.claude/`` project config directory)
|
|
111
|
+
|
|
112
|
+
Returns:
|
|
113
|
+
Plugin with all components resolved
|
|
114
|
+
|
|
115
|
+
Raises:
|
|
116
|
+
FileNotFoundError: If plugin.json doesn't exist (for non-project dirs)
|
|
117
|
+
ValueError: If plugin.json is invalid
|
|
118
|
+
"""
|
|
119
|
+
path = Path(path).resolve()
|
|
120
|
+
|
|
121
|
+
if not path.is_dir():
|
|
122
|
+
msg = f"Plugin path is not a directory: {path}"
|
|
123
|
+
raise FileNotFoundError(msg)
|
|
124
|
+
|
|
125
|
+
# Standard plugin with plugin.json (check first — plugins may also have agents/)
|
|
126
|
+
manifest_path = path / "plugin.json"
|
|
127
|
+
if manifest_path.exists():
|
|
128
|
+
return _load_from_manifest(path, manifest_path)
|
|
129
|
+
|
|
130
|
+
# Detect project config directories (no plugin.json)
|
|
131
|
+
dir_name = path.name
|
|
132
|
+
if dir_name == ".github":
|
|
133
|
+
return _load_project_directory(path, format_hint="github")
|
|
134
|
+
if dir_name == ".claude":
|
|
135
|
+
return _load_project_directory(path, format_hint="claude")
|
|
136
|
+
# Claude Code project root: has CLAUDE.md or .claude/ subdirectory
|
|
137
|
+
if (path / "CLAUDE.md").exists() or (path / ".claude").is_dir():
|
|
138
|
+
return _load_project_directory(path, format_hint="claude")
|
|
139
|
+
# GitHub project root: has .github/agents/
|
|
140
|
+
github_dir = path / ".github"
|
|
141
|
+
if github_dir.is_dir() and (github_dir / "agents").is_dir():
|
|
142
|
+
return _load_project_directory(github_dir, format_hint="github")
|
|
143
|
+
|
|
144
|
+
msg = f"No plugin.json, CLAUDE.md, .claude/, or .github/agents/ found in: {path}"
|
|
145
|
+
raise FileNotFoundError(msg)
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def _load_from_manifest(path: Path, manifest_path: Path) -> Plugin:
|
|
149
|
+
"""Load a plugin from a plugin.json manifest."""
|
|
150
|
+
manifest = _read_json(manifest_path)
|
|
151
|
+
|
|
152
|
+
if not isinstance(manifest, dict):
|
|
153
|
+
msg = f"{manifest_path}: must be a JSON object, got {type(manifest).__name__}"
|
|
154
|
+
raise ValueError(msg)
|
|
155
|
+
|
|
156
|
+
# Parse metadata
|
|
157
|
+
metadata = _parse_metadata(manifest, manifest_path)
|
|
158
|
+
|
|
159
|
+
# Discover agents
|
|
160
|
+
agents = _discover_agents(path)
|
|
161
|
+
|
|
162
|
+
# Discover skills
|
|
163
|
+
skills = _discover_skills(path)
|
|
164
|
+
_validate_skill_reference_names(skills)
|
|
165
|
+
|
|
166
|
+
# Parse MCP servers from manifest
|
|
167
|
+
mcp_servers = _parse_mcp_servers(manifest, manifest_path)
|
|
168
|
+
|
|
169
|
+
# Parse hooks
|
|
170
|
+
hooks = _parse_hooks(path, manifest)
|
|
171
|
+
|
|
172
|
+
# Discover instructions
|
|
173
|
+
instructions = _discover_instructions(path, manifest)
|
|
174
|
+
|
|
175
|
+
# Discover extensions
|
|
176
|
+
extensions = _discover_extensions(path)
|
|
177
|
+
|
|
178
|
+
return Plugin(
|
|
179
|
+
metadata=metadata,
|
|
180
|
+
path=path,
|
|
181
|
+
agents=agents,
|
|
182
|
+
skills=skills,
|
|
183
|
+
mcp_servers=mcp_servers,
|
|
184
|
+
hooks=hooks,
|
|
185
|
+
instructions=instructions,
|
|
186
|
+
extensions=extensions,
|
|
187
|
+
)
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def _has_agent_md_files(agents_dir: Path) -> bool:
|
|
191
|
+
"""Check if directory contains .agent.md or .md agent files."""
|
|
192
|
+
if not agents_dir.is_dir():
|
|
193
|
+
return False
|
|
194
|
+
return any(agents_dir.glob("*.agent.md")) or any(
|
|
195
|
+
p for p in agents_dir.glob("*.md") if not p.name.endswith(".agent.md")
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
def _load_project_directory(path: Path, *, format_hint: str) -> Plugin:
|
|
200
|
+
"""Load from a project root or .github/.claude config directory."""
|
|
201
|
+
metadata = PluginMetadata(name=path.name)
|
|
202
|
+
|
|
203
|
+
agents: list[dict[str, Any]] = []
|
|
204
|
+
skills: list[Skill] = []
|
|
205
|
+
extensions: list[Path] = []
|
|
206
|
+
mcp_servers: dict[str, dict[str, Any]] = {}
|
|
207
|
+
instruction_parts: list[str] = []
|
|
208
|
+
|
|
209
|
+
if format_hint == "claude":
|
|
210
|
+
# Claude Code: check root and .claude/ subdirectory
|
|
211
|
+
claude_dir = path / ".claude" if (path / ".claude").is_dir() else path
|
|
212
|
+
agents = _discover_agents(claude_dir)
|
|
213
|
+
skills = _discover_skills(claude_dir)
|
|
214
|
+
# Instructions from CLAUDE.md at root and .claude/
|
|
215
|
+
_append_file_content(instruction_parts, path / "CLAUDE.md")
|
|
216
|
+
if claude_dir != path:
|
|
217
|
+
_append_file_content(instruction_parts, claude_dir / "CLAUDE.md")
|
|
218
|
+
# MCP servers from .mcp.json
|
|
219
|
+
mcp_json = path / ".mcp.json"
|
|
220
|
+
if mcp_json.exists():
|
|
221
|
+
mcp_servers = _load_claude_project_mcp_servers(mcp_json)
|
|
222
|
+
elif format_hint == "github":
|
|
223
|
+
agents = _discover_agents(path)
|
|
224
|
+
skills = _discover_skills(path)
|
|
225
|
+
extensions = _discover_extensions(path)
|
|
226
|
+
_append_file_content(instruction_parts, path / "copilot-instructions.md")
|
|
227
|
+
_append_file_content(instruction_parts, path.parent / "copilot-instructions.md")
|
|
228
|
+
|
|
229
|
+
_validate_skill_reference_names(skills)
|
|
230
|
+
instructions = "\n\n".join(instruction_parts)
|
|
231
|
+
|
|
232
|
+
return Plugin(
|
|
233
|
+
metadata=metadata,
|
|
234
|
+
path=path,
|
|
235
|
+
agents=agents,
|
|
236
|
+
skills=skills,
|
|
237
|
+
mcp_servers=mcp_servers,
|
|
238
|
+
hooks=[],
|
|
239
|
+
instructions=instructions,
|
|
240
|
+
extensions=extensions,
|
|
241
|
+
)
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
def _append_file_content(parts: list[str], file_path: Path, *, required: bool = False) -> None:
|
|
245
|
+
"""Load instructions, allowing absence only for conventionally discovered files."""
|
|
246
|
+
if required or file_path.exists():
|
|
247
|
+
parts.append(load_instruction_file(file_path)["content"])
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def _parse_metadata(manifest: dict[str, Any], source: Path) -> PluginMetadata:
|
|
251
|
+
"""Extract metadata from plugin.json manifest."""
|
|
252
|
+
return PluginMetadata(
|
|
253
|
+
name=_string_field(manifest, "name", source, required=True),
|
|
254
|
+
version=_string_field(manifest, "version", source),
|
|
255
|
+
description=_string_field(manifest, "description", source),
|
|
256
|
+
author=_string_field(manifest, "author", source),
|
|
257
|
+
)
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
def _load_claude_project_mcp_servers(mcp_json: Path) -> dict[str, dict[str, Any]]:
|
|
261
|
+
"""Load MCP server definitions from a Claude project ``.mcp.json`` file."""
|
|
262
|
+
raw = _read_json(mcp_json)
|
|
263
|
+
|
|
264
|
+
if not isinstance(raw, dict):
|
|
265
|
+
raise ValueError(f"{mcp_json}: must contain a JSON object")
|
|
266
|
+
if "mcpServers" not in raw and "mcp_servers" not in raw:
|
|
267
|
+
raise ValueError(f"{mcp_json}: missing MCP server mapping ('mcpServers' or 'mcp_servers')")
|
|
268
|
+
|
|
269
|
+
return _parse_mcp_servers(raw, mcp_json)
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
def _read_json(path: Path) -> Any:
|
|
273
|
+
"""Decode configuration with a file-specific error for invalid JSON or UTF-8."""
|
|
274
|
+
try:
|
|
275
|
+
return json.loads(path.read_text(encoding="utf-8"))
|
|
276
|
+
except (json.JSONDecodeError, UnicodeError) as exc:
|
|
277
|
+
raise ValueError(f"{path}: Invalid JSON configuration: {exc}") from exc
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def _discover_agents(plugin_dir: Path) -> list[dict[str, Any]]:
|
|
281
|
+
"""Discover and load custom agents from agents/ subdirectory."""
|
|
282
|
+
agents_dir = plugin_dir / "agents"
|
|
283
|
+
if not agents_dir.exists():
|
|
284
|
+
return []
|
|
285
|
+
if not agents_dir.is_dir():
|
|
286
|
+
raise ValueError(f"{agents_dir}: custom agents path must be a directory")
|
|
287
|
+
|
|
288
|
+
agents: list[dict[str, Any]] = []
|
|
289
|
+
|
|
290
|
+
seen_names: set[str] = set()
|
|
291
|
+
for agent_file in sorted(agents_dir.glob("*.md")):
|
|
292
|
+
agent = load_custom_agent(agent_file)
|
|
293
|
+
if agent["name"] in seen_names:
|
|
294
|
+
raise ValueError(f"{agent_file}: Duplicate custom agent name '{agent['name']}'")
|
|
295
|
+
agents.append(agent)
|
|
296
|
+
seen_names.add(agent["name"])
|
|
297
|
+
|
|
298
|
+
return agents
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
def _discover_skills(plugin_dir: Path) -> list[Skill]:
|
|
302
|
+
"""Discover and load skills from skills/ subdirectory."""
|
|
303
|
+
skills_dir = plugin_dir / "skills"
|
|
304
|
+
if not skills_dir.exists():
|
|
305
|
+
return []
|
|
306
|
+
if not skills_dir.is_dir():
|
|
307
|
+
raise ValueError(f"{skills_dir}: skills path must be a directory")
|
|
308
|
+
|
|
309
|
+
skills: list[Skill] = []
|
|
310
|
+
for skill_dir in sorted(skills_dir.iterdir()):
|
|
311
|
+
if not skill_dir.is_dir():
|
|
312
|
+
continue
|
|
313
|
+
skills.append(Skill.from_path(skill_dir))
|
|
314
|
+
|
|
315
|
+
return skills
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
def _validate_skill_reference_names(skills: list[Skill]) -> None:
|
|
319
|
+
"""Reject duplicate relative reference paths across loaded skills."""
|
|
320
|
+
owners: dict[str, str] = {}
|
|
321
|
+
for skill in skills:
|
|
322
|
+
for reference_name in skill.references:
|
|
323
|
+
owner = owners.get(reference_name)
|
|
324
|
+
if owner is not None:
|
|
325
|
+
msg = (
|
|
326
|
+
f"Duplicate skill reference filename '{reference_name}' in skills "
|
|
327
|
+
f"'{owner}' and '{skill.name}'. Reference filenames must be unique "
|
|
328
|
+
"across a loaded plugin so they cannot overwrite each other."
|
|
329
|
+
)
|
|
330
|
+
raise ValueError(msg)
|
|
331
|
+
owners[reference_name] = skill.name
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
def _parse_mcp_servers(manifest: dict[str, Any], source: Path) -> dict[str, dict[str, Any]]:
|
|
335
|
+
"""Validate Copilot and Claude MCP mappings without discarding invalid entries."""
|
|
336
|
+
fields = [field for field in ("mcp_servers", "mcpServers") if field in manifest]
|
|
337
|
+
if not fields:
|
|
338
|
+
return {}
|
|
339
|
+
if len(fields) != 1:
|
|
340
|
+
raise ValueError(f"{source}: specify only one of 'mcp_servers' and 'mcpServers'")
|
|
341
|
+
return _validate_mcp_servers(manifest[fields[0]], source, field=fields[0])
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
def _parse_hooks(plugin_dir: Path, manifest: dict[str, Any]) -> list[HookDefinition]:
|
|
345
|
+
"""Parse hooks from hooks.json or plugin.json hooks field."""
|
|
346
|
+
hooks_file = plugin_dir / "hooks.json"
|
|
347
|
+
source = plugin_dir / "plugin.json"
|
|
348
|
+
raw = manifest.get("hooks", [])
|
|
349
|
+
# Validate both declared sources; a valid file cannot hide invalid inline hooks.
|
|
350
|
+
if not isinstance(raw, list):
|
|
351
|
+
raise ValueError(f"{source}: 'hooks' must be a list")
|
|
352
|
+
hooks = [_hook_from_dict(entry, source) for entry in raw]
|
|
353
|
+
if hooks_file.exists():
|
|
354
|
+
raw = _read_json(hooks_file)
|
|
355
|
+
if not isinstance(raw, list):
|
|
356
|
+
raise ValueError(f"{hooks_file}: hooks must be a list")
|
|
357
|
+
return [_hook_from_dict(entry, hooks_file) for entry in raw]
|
|
358
|
+
return hooks
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
def _hook_from_dict(data: Any, source: Path) -> HookDefinition:
|
|
362
|
+
"""Create a HookDefinition from a dict."""
|
|
363
|
+
if not isinstance(data, dict):
|
|
364
|
+
msg = f"{source}: Hook entry must be an object, got {type(data).__name__}"
|
|
365
|
+
raise ValueError(msg)
|
|
366
|
+
return HookDefinition(
|
|
367
|
+
event=_string_field(data, "event", source, required=True),
|
|
368
|
+
command=_string_field(data, "command", source, required=True),
|
|
369
|
+
pattern=_string_field(data, "pattern", source, allow_empty=True),
|
|
370
|
+
)
|
|
371
|
+
|
|
372
|
+
|
|
373
|
+
def _discover_instructions(plugin_dir: Path, manifest: dict[str, Any]) -> str:
|
|
374
|
+
"""Discover and concatenate instruction content."""
|
|
375
|
+
parts: list[str] = []
|
|
376
|
+
|
|
377
|
+
# Well-known instruction files
|
|
378
|
+
well_known = ["copilot-instructions.md", "CLAUDE.md"]
|
|
379
|
+
for filename in well_known:
|
|
380
|
+
_append_file_content(parts, plugin_dir / filename)
|
|
381
|
+
|
|
382
|
+
# Files listed in plugin.json instructions field
|
|
383
|
+
listed = manifest.get("instructions", [])
|
|
384
|
+
if isinstance(listed, str):
|
|
385
|
+
listed = [listed]
|
|
386
|
+
if not isinstance(listed, list) or any(
|
|
387
|
+
not isinstance(entry, str) or not entry.strip() for entry in listed
|
|
388
|
+
):
|
|
389
|
+
raise ValueError(
|
|
390
|
+
f"{plugin_dir / 'plugin.json'}: 'instructions' must be a path or list of paths"
|
|
391
|
+
)
|
|
392
|
+
for entry in listed:
|
|
393
|
+
_append_file_content(parts, plugin_dir / entry, required=True)
|
|
394
|
+
|
|
395
|
+
return "\n\n".join(parts)
|
|
396
|
+
|
|
397
|
+
|
|
398
|
+
def _discover_extensions(plugin_dir: Path) -> list[Path]:
|
|
399
|
+
"""Discover extension.mjs files in extensions/ subdirectory."""
|
|
400
|
+
ext_dir = plugin_dir / "extensions"
|
|
401
|
+
if not ext_dir.is_dir():
|
|
402
|
+
return []
|
|
403
|
+
return sorted(ext_dir.glob("extension.mjs"))
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"""Prompt loading and management."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass, field
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
import yaml
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass(slots=True, frozen=True)
|
|
13
|
+
class Prompt:
|
|
14
|
+
"""A loadable prompt configuration.
|
|
15
|
+
|
|
16
|
+
Example YAML:
|
|
17
|
+
name: banking-assistant
|
|
18
|
+
version: "1.0"
|
|
19
|
+
description: Concise banking responses
|
|
20
|
+
system_prompt: |
|
|
21
|
+
You are a banking assistant.
|
|
22
|
+
Be brief and always include account balances.
|
|
23
|
+
|
|
24
|
+
Example usage:
|
|
25
|
+
prompt = Prompt.from_yaml("prompts/banking.yaml")
|
|
26
|
+
agent = CopilotTask(instructions=prompt.system_prompt, ...)
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
name: str
|
|
30
|
+
system_prompt: str = ""
|
|
31
|
+
version: str = "1.0"
|
|
32
|
+
description: str = ""
|
|
33
|
+
metadata: dict[str, Any] = field(default_factory=dict)
|
|
34
|
+
|
|
35
|
+
@classmethod
|
|
36
|
+
def from_yaml(cls, path: str | Path) -> Prompt:
|
|
37
|
+
"""Load a prompt from a YAML file.
|
|
38
|
+
|
|
39
|
+
Args:
|
|
40
|
+
path: Path to YAML file
|
|
41
|
+
|
|
42
|
+
Returns:
|
|
43
|
+
Prompt instance
|
|
44
|
+
|
|
45
|
+
Raises:
|
|
46
|
+
FileNotFoundError: If file doesn't exist
|
|
47
|
+
ValueError: If required fields are missing
|
|
48
|
+
"""
|
|
49
|
+
path = Path(path)
|
|
50
|
+
if not path.exists():
|
|
51
|
+
raise FileNotFoundError(f"Prompt file not found: {path}")
|
|
52
|
+
|
|
53
|
+
with path.open(encoding="utf-8") as f:
|
|
54
|
+
data = yaml.safe_load(f) or {}
|
|
55
|
+
|
|
56
|
+
if "name" not in data:
|
|
57
|
+
raise ValueError(f"Prompt file missing required 'name' field: {path}")
|
|
58
|
+
|
|
59
|
+
return cls(
|
|
60
|
+
name=data["name"],
|
|
61
|
+
system_prompt=data.get("system_prompt", ""),
|
|
62
|
+
version=str(data.get("version", "1.0")),
|
|
63
|
+
description=data.get("description", ""),
|
|
64
|
+
metadata=data.get("metadata", {}),
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
@classmethod
|
|
68
|
+
def from_dict(cls, data: dict[str, Any]) -> Prompt:
|
|
69
|
+
"""Create a Prompt from a dictionary."""
|
|
70
|
+
if "name" not in data:
|
|
71
|
+
raise ValueError("Prompt data missing required 'name' field")
|
|
72
|
+
|
|
73
|
+
return cls(
|
|
74
|
+
name=data["name"],
|
|
75
|
+
system_prompt=data.get("system_prompt", ""),
|
|
76
|
+
version=str(data.get("version", "1.0")),
|
|
77
|
+
description=data.get("description", ""),
|
|
78
|
+
metadata=data.get("metadata", {}),
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
def __repr__(self) -> str:
|
|
82
|
+
return f"Prompt({self.name!r}, version={self.version!r})"
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def load_prompts(directory: str | Path) -> list[Prompt]:
|
|
86
|
+
"""Load all prompt YAML files from a directory.
|
|
87
|
+
|
|
88
|
+
Args:
|
|
89
|
+
directory: Path to directory containing .yaml/.yml files
|
|
90
|
+
|
|
91
|
+
Returns:
|
|
92
|
+
List of Prompt instances sorted by name
|
|
93
|
+
|
|
94
|
+
Example:
|
|
95
|
+
prompts = load_prompts("prompts/")
|
|
96
|
+
|
|
97
|
+
@pytest.mark.parametrize("prompt", prompts, ids=lambda p: p.name)
|
|
98
|
+
async def test_prompts(copilot_run, prompt):
|
|
99
|
+
agent = CopilotTask(instructions=prompt.system_prompt, ...)
|
|
100
|
+
"""
|
|
101
|
+
directory = Path(directory)
|
|
102
|
+
if not directory.exists():
|
|
103
|
+
raise FileNotFoundError(f"Prompts directory not found: {directory}")
|
|
104
|
+
|
|
105
|
+
prompts = []
|
|
106
|
+
yaml_files = set()
|
|
107
|
+
|
|
108
|
+
for path in sorted(directory.glob("*.yaml")):
|
|
109
|
+
prompts.append(Prompt.from_yaml(path))
|
|
110
|
+
yaml_files.add(path.stem)
|
|
111
|
+
|
|
112
|
+
for path in sorted(directory.glob("*.yml")):
|
|
113
|
+
if path.stem not in yaml_files:
|
|
114
|
+
prompts.append(Prompt.from_yaml(path))
|
|
115
|
+
|
|
116
|
+
return sorted(prompts, key=lambda p: p.name)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def load_prompt(path: str | Path) -> Prompt:
|
|
120
|
+
"""Load a single prompt from a YAML file.
|
|
121
|
+
|
|
122
|
+
Args:
|
|
123
|
+
path: Path to YAML file
|
|
124
|
+
|
|
125
|
+
Returns:
|
|
126
|
+
Prompt instance
|
|
127
|
+
"""
|
|
128
|
+
return Prompt.from_yaml(path)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def load_system_prompts(directory: str | Path) -> dict[str, str]:
|
|
132
|
+
"""Load all system prompts from a directory as a simple dict.
|
|
133
|
+
|
|
134
|
+
This is a convenience function for quick parametrization.
|
|
135
|
+
For full Prompt metadata, use load_prompts() instead.
|
|
136
|
+
|
|
137
|
+
Args:
|
|
138
|
+
directory: Path to directory containing .yaml/.yml or .md files
|
|
139
|
+
|
|
140
|
+
Returns:
|
|
141
|
+
Dict mapping prompt name to system_prompt content
|
|
142
|
+
|
|
143
|
+
Example:
|
|
144
|
+
prompts = load_system_prompts(Path("prompts/"))
|
|
145
|
+
# {"concise": "Be brief...", "detailed": "Explain..."}
|
|
146
|
+
|
|
147
|
+
@pytest.mark.parametrize("prompt_name,system_prompt", prompts.items())
|
|
148
|
+
async def test_with_prompt(copilot_run, prompt_name, system_prompt):
|
|
149
|
+
agent = CopilotTask(instructions=system_prompt, ...)
|
|
150
|
+
"""
|
|
151
|
+
directory = Path(directory)
|
|
152
|
+
if not directory.exists():
|
|
153
|
+
raise FileNotFoundError(f"Prompts directory not found: {directory}")
|
|
154
|
+
|
|
155
|
+
result = {}
|
|
156
|
+
|
|
157
|
+
# Load from YAML files
|
|
158
|
+
for prompt in load_prompts(directory):
|
|
159
|
+
result[prompt.name] = prompt.system_prompt
|
|
160
|
+
|
|
161
|
+
# Also load from .md files (plain markdown = system prompt content)
|
|
162
|
+
for path in sorted(directory.glob("*.md")):
|
|
163
|
+
name = path.stem
|
|
164
|
+
if name not in result: # YAML takes precedence
|
|
165
|
+
result[name] = path.read_text(encoding="utf-8")
|
|
166
|
+
|
|
167
|
+
return result
|