-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptHost.cs
More file actions
157 lines (148 loc) · 5.25 KB
/
Copy pathScriptHost.cs
File metadata and controls
157 lines (148 loc) · 5.25 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
namespace Cdp.ScriptableIde;
public static class ScriptHost
{
/// <summary>Serialize Console.Out redirect — MCP stdio process must not see CSX prints.</summary>
private static readonly SemaphoreSlim ConsoleGate = new(1, 1);
private static readonly ScriptOptions DefaultOptions = ScriptOptions.Default
.AddReferences(typeof(ScriptGlobals).Assembly)
.AddImports(
"System",
"System.IO",
"System.Threading",
"System.Threading.Tasks",
"Cdp.ScriptableIde");
public static async Task<ScriptReport> CheckAsync(string code, CancellationToken cancellationToken = default)
{
_ = cancellationToken;
try
{
var script = CSharpScript.Create(code, DefaultOptions, typeof(ScriptGlobals));
var diags = script.Compile();
var items = CsxDiagnosticProjection.FromDiagnostics(diags);
var legacy = CsxDiagnosticProjection.ToLegacyStrings(items);
return new ScriptReport
{
Ok = items.Count == 0,
Mode = "check",
Diagnostics = legacy,
DiagnosticItems = items,
Evidence = Cdp.Evidence.EvidencePreprocess.ToDto(CsxDiagnosticProjection.ToEvidence(items)),
Error = items.Count == 0 ? null : string.Join("\n", legacy)
};
}
catch (Exception ex)
{
return new ScriptReport { Ok = false, Mode = "check", Error = ex.Message, Diagnostics = [ex.Message] };
}
}
public static Task<ScriptReport> RunAsync(
string code,
ScriptToolBus bus,
string mode = "run",
CancellationToken cancellationToken = default)
{
var plan = new PlanContext
{
PrimaryRoot = Environment.CurrentDirectory,
WorkRoot = Environment.CurrentDirectory,
PlanId = ""
};
return RunAsync(code, bus, plan, mode, cancellationToken);
}
public static async Task<ScriptReport> RunAsync(
string code,
ScriptToolBus bus,
PlanContext plan,
string mode = "run",
CancellationToken cancellationToken = default)
{
ProjectSettingsLoader.Hydrate(plan);
var globals = new ScriptGlobals(bus, plan);
await ConsoleGate.WaitAsync(cancellationToken).ConfigureAwait(false);
var captured = new StringBuilder();
var writer = new StringWriter(captured);
var prevOut = Console.Out;
Console.SetOut(writer);
try
{
return await RunCoreAsync(code, bus, plan, mode, globals, captured, cancellationToken)
.ConfigureAwait(false);
}
finally
{
Console.SetOut(prevOut);
writer.Dispose();
ConsoleGate.Release();
}
}
private static async Task<ScriptReport> RunCoreAsync(
string code,
ScriptToolBus bus,
PlanContext plan,
string mode,
ScriptGlobals globals,
StringBuilder captured,
CancellationToken cancellationToken)
{
try
{
var state = await CSharpScript.RunAsync(code, DefaultOptions, globals, cancellationToken: cancellationToken)
.ConfigureAwait(false);
var result = state.ReturnValue?.ToString();
return new ScriptReport
{
Ok = true,
Mode = mode,
Result = result,
Steps = bus.Steps.ToArray(),
PlanId = plan.PlanId,
PrimaryRoot = plan.PrimaryRoot,
WorkRoot = plan.WorkRoot,
ConsoleOut = CapturedOrNull(captured)
};
}
catch (CompilationErrorException cex)
{
var items = CsxDiagnosticProjection.FromDiagnostics(cex.Diagnostics);
var legacy = CsxDiagnosticProjection.ToLegacyStrings(items);
return new ScriptReport
{
Ok = false,
Mode = mode,
Error = string.Join("\n", legacy),
Diagnostics = legacy,
DiagnosticItems = items,
Evidence = Cdp.Evidence.EvidencePreprocess.ToDto(CsxDiagnosticProjection.ToEvidence(items)),
Steps = bus.Steps.ToArray(),
PlanId = plan.PlanId,
PrimaryRoot = plan.PrimaryRoot,
WorkRoot = plan.WorkRoot,
ConsoleOut = CapturedOrNull(captured)
};
}
catch (Exception ex)
{
return new ScriptReport
{
Ok = false,
Mode = mode,
Error = ex.Message,
Diagnostics = [ex.Message],
Steps = bus.Steps.ToArray(),
PlanId = plan.PlanId,
PrimaryRoot = plan.PrimaryRoot,
WorkRoot = plan.WorkRoot,
ConsoleOut = CapturedOrNull(captured)
};
}
}
private static string? CapturedOrNull(StringBuilder sb)
{
var t = sb.ToString();
return string.IsNullOrEmpty(t) ? null : t;
}
}