forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
81 lines (55 loc) · 2.28 KB
/
Copy pathmain.py
File metadata and controls
81 lines (55 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import asyncio
from pydantic import BaseModel, Field
from copilot import CopilotClient, define_tool
from copilot.generated.rpc import PermissionDecisionApproveOnce
# In-memory virtual filesystem
virtual_fs: dict[str, str] = {}
class CreateFileParams(BaseModel):
path: str = Field(description="File path")
content: str = Field(description="File content")
class ReadFileParams(BaseModel):
path: str = Field(description="File path")
@define_tool(description="Create or overwrite a file at the given path with the provided content")
def create_file(params: CreateFileParams) -> str:
virtual_fs[params.path] = params.content
return f"Created {params.path} ({len(params.content)} bytes)"
@define_tool(description="Read the contents of a file at the given path")
def read_file(params: ReadFileParams) -> str:
content = virtual_fs.get(params.path)
if content is None:
return f"Error: file not found: {params.path}"
return content
@define_tool(description="List all files in the virtual filesystem")
def list_files() -> str:
if not virtual_fs:
return "No files"
return "\n".join(virtual_fs.keys())
async def auto_approve_permission(request, invocation):
return PermissionDecisionApproveOnce()
async def auto_approve_tool(input_data, invocation):
return {"permissionDecision": "allow"}
async def main():
client = CopilotClient()
try:
session = await client.create_session(
on_permission_request=auto_approve_permission,
model="claude-haiku-4.5",
available_tools=[],
tools=[create_file, read_file, list_files],
hooks={"on_pre_tool_use": auto_approve_tool},
)
response = await session.send_and_wait(
"Create a file called plan.md with a brief 3-item project plan "
"for building a CLI tool. Then read it back and tell me what you wrote."
)
if response:
print(response.data.content)
# Dump the virtual filesystem to prove nothing touched disk
print("\n--- Virtual filesystem contents ---")
for path, content in virtual_fs.items():
print(f"\n[{path}]")
print(content)
await session.disconnect()
finally:
await client.stop()
asyncio.run(main())