|
| 1 | +using System.Text; |
| 2 | +using System.Text.Json; |
| 3 | +using CopilotHere.Commands.Mounts; |
| 4 | + |
| 5 | +namespace CopilotHere.Infrastructure; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Generates session information as JSON for use within the container. |
| 9 | +/// </summary> |
| 10 | +public static class SessionInfo |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Generates session info JSON string for standard Docker run mode. |
| 14 | + /// </summary> |
| 15 | + public static string Generate( |
| 16 | + AppContext ctx, |
| 17 | + string imageTag, |
| 18 | + string imageName, |
| 19 | + List<MountEntry> mounts, |
| 20 | + bool isYolo, |
| 21 | + bool airlockEnabled = false, |
| 22 | + string? airlockRulesPath = null) |
| 23 | + { |
| 24 | + var json = new StringBuilder(); |
| 25 | + json.Append('{'); |
| 26 | + json.Append($"\"copilot_here_version\":\"{BuildInfo.BuildDate}\","); |
| 27 | + json.Append($"\"image\":{{\"tag\":\"{imageTag}\",\"full_name\":\"{imageName}\"}},"); |
| 28 | + json.Append($"\"mode\":\"{(isYolo ? "yolo" : "standard")}\","); |
| 29 | + json.Append($"\"working_directory\":\"{JsonEncode(ctx.Paths.ContainerWorkDir)}\","); |
| 30 | + json.Append("\"mounts\":["); |
| 31 | + |
| 32 | + for (int i = 0; i < mounts.Count; i++) |
| 33 | + { |
| 34 | + var m = mounts[i]; |
| 35 | + if (i > 0) json.Append(','); |
| 36 | + json.Append('{'); |
| 37 | + json.Append($"\"host_path\":\"{JsonEncode(m.ResolvePath(ctx.Paths.UserHome))}\","); |
| 38 | + json.Append($"\"container_path\":\"{JsonEncode(m.GetContainerPath(ctx.Paths.UserHome))}\","); |
| 39 | + json.Append($"\"mode\":\"{(m.IsReadWrite ? "rw" : "ro")}\","); |
| 40 | + json.Append($"\"source\":\"{m.Source.ToString().ToLowerInvariant()}\""); |
| 41 | + json.Append('}'); |
| 42 | + } |
| 43 | + |
| 44 | + json.Append("],"); |
| 45 | + json.Append($"\"airlock\":{{\"enabled\":{(airlockEnabled ? "true" : "false")},"); |
| 46 | + json.Append($"\"rules_path\":\"{JsonEncode(airlockRulesPath ?? "")}\","); |
| 47 | + json.Append($"\"source\":\"{ctx.AirlockConfig.EnabledSource.ToString().ToLowerInvariant()}\"}}"); |
| 48 | + json.Append('}'); |
| 49 | + |
| 50 | + return json.ToString(); |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Generates extended session info JSON for Airlock mode with network config details. |
| 55 | + /// </summary> |
| 56 | + public static string GenerateWithNetworkConfig( |
| 57 | + AppContext ctx, |
| 58 | + string imageTag, |
| 59 | + string imageName, |
| 60 | + List<MountEntry> mounts, |
| 61 | + bool isYolo, |
| 62 | + string airlockRulesPath) |
| 63 | + { |
| 64 | + var baseInfo = Generate(ctx, imageTag, imageName, mounts, isYolo, true, airlockRulesPath); |
| 65 | + |
| 66 | + // Add network config details if rules file exists |
| 67 | + if (!File.Exists(airlockRulesPath)) |
| 68 | + return baseInfo; |
| 69 | + |
| 70 | + try |
| 71 | + { |
| 72 | + var rulesContent = File.ReadAllText(airlockRulesPath); |
| 73 | + using var doc = JsonDocument.Parse(rulesContent); |
| 74 | + var root = doc.RootElement; |
| 75 | + |
| 76 | + var networkConfig = new StringBuilder(); |
| 77 | + networkConfig.Append("\"network_config\":{"); |
| 78 | + |
| 79 | + var hasAnyField = false; |
| 80 | + |
| 81 | + // Extract mode |
| 82 | + if (root.TryGetProperty("mode", out var modeElement)) |
| 83 | + { |
| 84 | + networkConfig.Append($"\"mode\":\"{JsonEncode(modeElement.GetString() ?? "enforce")}\""); |
| 85 | + hasAnyField = true; |
| 86 | + } |
| 87 | + |
| 88 | + // Extract logging |
| 89 | + if (root.TryGetProperty("enable_logging", out var loggingElement)) |
| 90 | + { |
| 91 | + if (hasAnyField) networkConfig.Append(','); |
| 92 | + networkConfig.Append($"\"logging_enabled\":{(loggingElement.GetBoolean() ? "true" : "false")}"); |
| 93 | + hasAnyField = true; |
| 94 | + } |
| 95 | + |
| 96 | + // Count rules and extract sample domains |
| 97 | + if (root.TryGetProperty("rules", out var rulesElement) && rulesElement.ValueKind == JsonValueKind.Array) |
| 98 | + { |
| 99 | + var ruleCount = rulesElement.GetArrayLength(); |
| 100 | + if (hasAnyField) networkConfig.Append(','); |
| 101 | + networkConfig.Append($"\"rules_count\":{ruleCount}"); |
| 102 | + |
| 103 | + // Extract first few domains |
| 104 | + var domains = new List<string>(); |
| 105 | + foreach (var rule in rulesElement.EnumerateArray().Take(5)) |
| 106 | + { |
| 107 | + if (rule.TryGetProperty("pattern", out var pattern)) |
| 108 | + { |
| 109 | + var patternStr = pattern.GetString(); |
| 110 | + if (!string.IsNullOrEmpty(patternStr)) |
| 111 | + domains.Add(patternStr); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if (domains.Count > 0) |
| 116 | + { |
| 117 | + networkConfig.Append(",\"sample_domains\":["); |
| 118 | + for (int i = 0; i < domains.Count; i++) |
| 119 | + { |
| 120 | + if (i > 0) networkConfig.Append(','); |
| 121 | + networkConfig.Append($"\"{JsonEncode(domains[i])}\""); |
| 122 | + } |
| 123 | + networkConfig.Append(']'); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + networkConfig.Append('}'); |
| 128 | + |
| 129 | + // Insert network_config into airlock object |
| 130 | + // Remove the closing brace of airlock and root, add network_config, then close |
| 131 | + var insertPoint = baseInfo.LastIndexOf("}}", StringComparison.Ordinal); |
| 132 | + if (insertPoint > 0) |
| 133 | + { |
| 134 | + return baseInfo[..insertPoint] + "," + networkConfig + "}}"; |
| 135 | + } |
| 136 | + } |
| 137 | + catch |
| 138 | + { |
| 139 | + // Non-fatal - return base info without network config |
| 140 | + } |
| 141 | + |
| 142 | + return baseInfo; |
| 143 | + } |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// JSON-encodes a string for safe inclusion in JSON. |
| 147 | + /// </summary> |
| 148 | + private static string JsonEncode(string value) |
| 149 | + { |
| 150 | + if (string.IsNullOrEmpty(value)) |
| 151 | + return value; |
| 152 | + |
| 153 | + return value |
| 154 | + .Replace("\\", "\\\\") |
| 155 | + .Replace("\"", "\\\"") |
| 156 | + .Replace("\n", "\\n") |
| 157 | + .Replace("\r", "\\r") |
| 158 | + .Replace("\t", "\\t"); |
| 159 | + } |
| 160 | +} |
0 commit comments