-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathProgram.cs
More file actions
81 lines (74 loc) · 2.69 KB
/
Copy pathProgram.cs
File metadata and controls
81 lines (74 loc) · 2.69 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
using System.ComponentModel;
using GitHub.Copilot.SDK;
using Microsoft.Extensions.AI;
// In-memory virtual filesystem
var virtualFs = new Dictionary<string, string>();
using var client = new CopilotClient(new CopilotClientOptions
{
CliPath = Environment.GetEnvironmentVariable("COPILOT_CLI_PATH"),
GitHubToken = Environment.GetEnvironmentVariable("GITHUB_TOKEN"),
});
await client.StartAsync();
try
{
await using var session = await client.CreateSessionAsync(new SessionConfig
{
Model = "claude-haiku-4.5",
AvailableTools = [],
Tools =
[
AIFunctionFactory.Create(
([Description("File path")] string path, [Description("File content")] string content) =>
{
virtualFs[path] = content;
return $"Created {path} ({content.Length} bytes)";
},
"create_file",
"Create or overwrite a file at the given path with the provided content"),
AIFunctionFactory.Create(
([Description("File path")] string path) =>
{
return virtualFs.TryGetValue(path, out var content)
? content
: $"Error: file not found: {path}";
},
"read_file",
"Read the contents of a file at the given path"),
AIFunctionFactory.Create(
() =>
{
return virtualFs.Count == 0
? "No files"
: string.Join("\n", virtualFs.Keys);
},
"list_files",
"List all files in the virtual filesystem"),
],
OnPermissionRequest = (request, invocation) =>
Task.FromResult(new PermissionRequestResult { Kind = "approved" }),
Hooks = new SessionHooks
{
OnPreToolUse = (input, invocation) =>
Task.FromResult<PreToolUseHookOutput?>(new PreToolUseHookOutput { PermissionDecision = "allow" }),
},
});
var response = await session.SendAndWaitAsync(new MessageOptions
{
Prompt = "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 != null)
{
Console.WriteLine(response.Data?.Content);
}
// Dump the virtual filesystem to prove nothing touched disk
Console.WriteLine("\n--- Virtual filesystem contents ---");
foreach (var (path, content) in virtualFs)
{
Console.WriteLine($"\n[{path}]");
Console.WriteLine(content);
}
}
finally
{
await client.StopAsync();
}