microsoft/TypeAgent
Publicmirrored fromhttps://github.com/microsoft/TypeAgentAvailable
dotnet/agentLauncher/src/AgentSettings.cs
122lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Text.Json.Serialization; |
| 5 | |
| 6 | namespace AgentLauncher; |
| 7 | |
| 8 | public class AgentSettings |
| 9 | { |
| 10 | private static AgentSettings? _instance; |
| 11 | private static readonly object _lock = new(); |
| 12 | |
| 13 | [JsonPropertyName("nodePath")] |
| 14 | public string? NodePath { get; set; } |
| 15 | |
| 16 | [JsonPropertyName("timeoutMs")] |
| 17 | public int TimeoutMs { get; set; } = 60000; |
| 18 | |
| 19 | [JsonPropertyName("verboseLogging")] |
| 20 | public bool VerboseLogging { get; set; } = false; |
| 21 | |
| 22 | [JsonPropertyName("environment")] |
| 23 | public Dictionary<string, string>? Environment { get; set; } |
| 24 | |
| 25 | [JsonPropertyName("workingDirectory")] |
| 26 | public string? WorkingDirectory { get; set; } |
| 27 | |
| 28 | public static AgentSettings Instance |
| 29 | { |
| 30 | get |
| 31 | { |
| 32 | lock (_lock) |
| 33 | { |
| 34 | if (_instance == null) |
| 35 | { |
| 36 | _instance = new AgentSettings(); |
| 37 | _instance.LoadFromEnvironment(); |
| 38 | } |
| 39 | return _instance; |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | private void LoadFromEnvironment() |
| 45 | { |
| 46 | var nodePathEnv = System.Environment.GetEnvironmentVariable("TYPEAGENT_NODE_PATH"); |
| 47 | if (!string.IsNullOrWhiteSpace(nodePathEnv)) |
| 48 | { |
| 49 | NodePath = nodePathEnv; |
| 50 | Program.Log($"Node path from environment: {NodePath}"); |
| 51 | } |
| 52 | |
| 53 | var timeoutEnv = System.Environment.GetEnvironmentVariable("TYPEAGENT_TIMEOUT"); |
| 54 | if (!string.IsNullOrWhiteSpace(timeoutEnv) && int.TryParse(timeoutEnv, out var timeout)) |
| 55 | { |
| 56 | TimeoutMs = timeout; |
| 57 | Program.Log($"Timeout from environment: {TimeoutMs}ms"); |
| 58 | } |
| 59 | |
| 60 | var verboseEnv = System.Environment.GetEnvironmentVariable("TYPEAGENT_VERBOSE"); |
| 61 | if (!string.IsNullOrWhiteSpace(verboseEnv) && bool.TryParse(verboseEnv, out var verbose)) |
| 62 | { |
| 63 | VerboseLogging = verbose; |
| 64 | Program.Log($"Verbose logging from environment: {VerboseLogging}"); |
| 65 | } |
| 66 | |
| 67 | var workdirEnv = System.Environment.GetEnvironmentVariable("TYPEAGENT_WORKDIR"); |
| 68 | if (!string.IsNullOrWhiteSpace(workdirEnv)) |
| 69 | { |
| 70 | WorkingDirectory = workdirEnv; |
| 71 | Program.Log($"Working directory from environment: {WorkingDirectory}"); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | public string GetResolvedScriptPath() |
| 76 | { |
| 77 | var fallbackPaths = new[] |
| 78 | { |
| 79 | Path.Combine(AppContext.BaseDirectory, "Scripts", "agent-uri-handler.bundle.js"), |
| 80 | Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), |
| 81 | "AgentLauncher", "Scripts", "agent-uri-handler.bundle.js"), |
| 82 | "D:\\repos\\TypeAgent\\ts\\packages\\uriHandler\\bundle\\agent-uri-handler.bundle.js", |
| 83 | "D:\\repos\\TypeAgent\\ts\\packages\\uriHandler\\dist\\index.js" |
| 84 | }; |
| 85 | |
| 86 | foreach (var path in fallbackPaths) |
| 87 | { |
| 88 | if (File.Exists(path)) |
| 89 | { |
| 90 | Program.Log($"Using script path: {path}"); |
| 91 | return path; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | Program.Log($"WARN: No script found in any fallback locations"); |
| 96 | return fallbackPaths[0]; |
| 97 | } |
| 98 | |
| 99 | public string GetResolvedNodePath() |
| 100 | { |
| 101 | if (!string.IsNullOrWhiteSpace(NodePath)) |
| 102 | { |
| 103 | var expanded = System.Environment.ExpandEnvironmentVariables(NodePath); |
| 104 | if (File.Exists(expanded)) |
| 105 | { |
| 106 | return expanded; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return "node"; |
| 111 | } |
| 112 | |
| 113 | public string? GetResolvedWorkingDirectory() |
| 114 | { |
| 115 | if (string.IsNullOrWhiteSpace(WorkingDirectory)) |
| 116 | { |
| 117 | return null; |
| 118 | } |
| 119 | |
| 120 | return System.Environment.ExpandEnvironmentVariables(WorkingDirectory); |
| 121 | } |
| 122 | } |
| 123 | |