|
1 | | -"""Example: Export a skill as a local directory for OpenAI Agents. |
| 1 | +"""Load a Musher skill as a local OpenAI shell skill and use it on this repo. |
2 | 2 |
|
3 | | -Exports a single skill to disk so it can be loaded as a local shell skill |
4 | | -by the OpenAI Agents SDK. |
| 3 | +Requires: pip install openai-agents |
5 | 4 | """ |
6 | 5 |
|
| 6 | +import asyncio |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +from agents import ( |
| 10 | + Agent, |
| 11 | + Runner, |
| 12 | + ShellCallOutcome, |
| 13 | + ShellCommandOutput, |
| 14 | + ShellCommandRequest, |
| 15 | + ShellResult, |
| 16 | + ShellTool, |
| 17 | +) |
| 18 | + |
7 | 19 | import musher |
8 | 20 |
|
9 | | -# NOTE: Bundle references below (e.g. "acme/agent-toolkit:2.0.0") are |
| 21 | +# NOTE: Bundle references below (e.g. "acme/engineering-workflows:2.0.0") are |
10 | 22 | # placeholders. Replace with a real bundle ref from your Musher registry. |
11 | 23 |
|
12 | 24 | # Credentials auto-discovered from MUSHER_API_KEY env var, keyring, |
13 | 25 | # or credential file. To override: musher.configure(token="your-token") |
14 | 26 |
|
15 | | -bundle = musher.pull("acme/agent-toolkit:2.0.0") |
| 27 | +PROJECT_DIR = Path(__file__).resolve().parents[2] |
| 28 | + |
| 29 | + |
| 30 | +class RepoShell: |
| 31 | + """Minimal local shell executor for the OpenAI Agents SDK.""" |
| 32 | + |
| 33 | + def __init__(self, cwd: Path) -> None: |
| 34 | + self.cwd = cwd |
| 35 | + |
| 36 | + async def __call__(self, request: ShellCommandRequest) -> ShellResult: |
| 37 | + outputs: list[ShellCommandOutput] = [] |
| 38 | + |
| 39 | + for command in request.data.action.commands: |
| 40 | + proc = await asyncio.create_subprocess_shell( |
| 41 | + command, |
| 42 | + cwd=self.cwd, |
| 43 | + stdout=asyncio.subprocess.PIPE, |
| 44 | + stderr=asyncio.subprocess.PIPE, |
| 45 | + ) |
| 46 | + stdout, stderr = await proc.communicate() |
| 47 | + |
| 48 | + outputs.append( |
| 49 | + ShellCommandOutput( |
| 50 | + command=command, |
| 51 | + stdout=stdout.decode("utf-8", errors="ignore"), |
| 52 | + stderr=stderr.decode("utf-8", errors="ignore"), |
| 53 | + outcome=ShellCallOutcome(type="exit", exit_code=proc.returncode), |
| 54 | + ) |
| 55 | + ) |
| 56 | + |
| 57 | + return ShellResult( |
| 58 | + output=outputs, |
| 59 | + max_output_length=request.data.action.max_output_length, |
| 60 | + provider_data={"working_directory": str(self.cwd)}, |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +bundle = musher.pull("acme/engineering-workflows:2.0.0") |
| 65 | +skill = bundle.skill("repo-maintainer") |
| 66 | +local = skill.export_openai_local_skill(dest=PROJECT_DIR / ".musher" / "openai" / "skills") |
| 67 | + |
| 68 | +agent = Agent( |
| 69 | + name="Repo Triage Assistant", |
| 70 | + model="gpt-4.1", |
| 71 | + instructions="Use the local skill when it helps. Keep the answer concise and actionable.", |
| 72 | + tools=[ |
| 73 | + ShellTool( |
| 74 | + executor=RepoShell(PROJECT_DIR), |
| 75 | + environment={"type": "local", "skills": [local.to_dict()]}, |
| 76 | + ) |
| 77 | + ], |
| 78 | +) |
| 79 | + |
| 80 | + |
| 81 | +async def main() -> None: |
| 82 | + result = await Runner.run( |
| 83 | + agent, |
| 84 | + "Use the repo-maintainer skill to inspect this repository and tell me the first onboarding issue I should fix.", |
| 85 | + ) |
| 86 | + print(result.final_output) |
16 | 87 |
|
17 | | -# Get a single skill |
18 | | -skill = bundle.skill("csv-insights") |
19 | 88 |
|
20 | | -# PREVIEW: export_openai_local_skill() is not yet implemented — will raise NotImplementedError. |
21 | | -local = skill.export_openai_local_skill() |
22 | | -print(f"Local skill: {local.name} at {local.path}") |
23 | | -print(f" Registration dict: {local.to_dict()}") |
| 89 | +if __name__ == "__main__": |
| 90 | + asyncio.run(main()) |
0 commit comments