-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
142 lines (121 loc) · 4 KB
/
Program.cs
File metadata and controls
142 lines (121 loc) · 4 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System.Text.Json;
using System.Text.Json.Serialization;
namespace CopilotExt;
/// <summary>
/// Main entry point. Reads JSON-lines from stdin, dispatches to CopilotRunner,
/// writes structured cell output back via stdout.
/// </summary>
class Program
{
private static readonly JsonSerializerOptions JsonOpts = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
WriteIndented = false
};
static async Task Main()
{
using var reader = Console.In;
string? line;
while ((line = await reader.ReadLineAsync()) != null)
{
if (string.IsNullOrWhiteSpace(line)) continue;
string? type = GetMessageType(line);
switch (type)
{
case "init":
SendRegister();
break;
case "activate":
var msg = JsonSerializer.Deserialize<ActivateMessage>(line, JsonOpts);
if (msg != null)
_ = Task.Run(() => HandleActivate(msg));
break;
case "deactivate":
// Future: cancel running processes
break;
}
}
}
static void SendRegister()
{
var msg = new { type = "register", prefix = "copilot", name = "copilot-ext", version = "0.1.0" };
SendMessage(msg);
}
static async Task HandleActivate(ActivateMessage msg)
{
SendMessage(new { type = "status", id = msg.Id, message = "⏳ Asking Copilot..." });
try
{
string userPrompt = string.Join(" ", msg.Params);
string fullPrompt = PromptBuilder.Build(userPrompt, msg.GridCols, msg.GridRows);
string? rawOutput = await CopilotRunner.RunAsync(fullPrompt);
if (rawOutput == null)
{
SendMessage(new { type = "error", id = msg.Id, message = "Copilot CLI timed out or failed." });
return;
}
var cells = OutputParser.Parse(rawOutput, msg.GridCols, msg.GridRows);
if (cells.Count == 0)
{
cells.Add(new CellWrite { Row = 0, Col = 0, Value = rawOutput.Trim() });
}
SendMessage(new WriteCellsMessage { Type = "write", Id = msg.Id, Cells = cells.ToArray() });
}
catch (Exception ex)
{
SendMessage(new { type = "error", id = msg.Id, message = ex.Message });
}
}
static string? GetMessageType(string json)
{
try
{
using var doc = JsonDocument.Parse(json);
if (doc.RootElement.TryGetProperty("type", out var t))
return t.GetString();
}
catch { }
return null;
}
static readonly object _writeLock = new();
static void SendMessage(object msg)
{
string json = JsonSerializer.Serialize(msg, JsonOpts);
lock (_writeLock)
{
Console.WriteLine(json);
Console.Out.Flush();
}
}
}
// ── Protocol message types ──────────────────────────────────────────────
class ActivateMessage
{
public string Type { get; set; } = "";
public string Id { get; set; } = "";
public CellPosition Anchor { get; set; } = new();
public string[] Params { get; set; } = [];
public int GridCols { get; set; }
public int GridRows { get; set; }
}
class CellPosition
{
public int Row { get; set; }
public int Col { get; set; }
}
class CellWrite
{
[JsonPropertyName("r")]
public int Row { get; set; }
[JsonPropertyName("c")]
public int Col { get; set; }
[JsonPropertyName("v")]
public string Value { get; set; } = "";
}
class WriteCellsMessage
{
public string Type { get; set; } = "write";
public string Id { get; set; } = "";
public CellWrite[] Cells { get; set; } = [];
}