summaryrefslogtreecommitdiff
path: root/src/Agent.Core/Localization
diff options
context:
space:
mode:
authortanaka takashi <isazaka@gmail.com>2026-08-09 21:08:20 +0900
committertanaka takashi <isazaka@gmail.com>2026-08-09 21:08:20 +0900
commitb07b41cd435991cd254957af79055080c85b346e (patch)
tree27720d3ce14e4d0b84670a55ac26a2f877ce3a20 /src/Agent.Core/Localization
parent2fefd29e86c3b926d0cf4a362251d3f119688612 (diff)
feat: AI Agent Sandbox MCP Server v1を実装
Specifications、Design、ImplementationPlanの優先順位を整理し、単一Task実行モデル、typed option優先、設定可能なresource上限、local Ollamaを必須とするRelease Gateを文書化。 .NET 10/C# 14の5 production project、3 test project、E2E Clientを構築。typed YAML、en/ja locale、fixed/user prompt、stdioおよびloopback限定Streamable HTTP、agent_run/agent_status、Session、Ollama text/tool/Vision、TaskGraphとtoken accountingを実装。 Windows sandboxとしてfixed AppContainer、canonical/reparse検査、能力別ACL、path+SHA-256 executable approval、child-process restriction、Job Object、task journal、atomic file transaction、crash recoveryを実装。WFPのIPv4/IPv6 default denyとexact allow、dynamic session、temporary loopback exemption、管理者/BFE診断を追加。 Security/Recovery test、MCP/Ollama integration test、stdio/HTTP Operational E2E、Release Bundle生成・checksum・manifest・fresh extraction検査を追加。Requirement TraceabilityとPhase 0〜11の進捗、初回セットアップ可能なREADMEを整備。 Debug/Release/publish/objをrepository rootのbuild/へ集約し、旧project-local bin/objへの依存を除去。artifacts/とbuild/をGit管理外に設定し、clean checkoutでもsolution buildからE2E Clientまで生成可能にした。
Diffstat (limited to 'src/Agent.Core/Localization')
-rw-r--r--src/Agent.Core/Localization/LanguageContext.cs3
-rw-r--r--src/Agent.Core/Localization/LocaleCatalog.cs97
-rw-r--r--src/Agent.Core/Localization/LocaleException.cs4
3 files changed, 104 insertions, 0 deletions
diff --git a/src/Agent.Core/Localization/LanguageContext.cs b/src/Agent.Core/Localization/LanguageContext.cs
new file mode 100644
index 0000000..f8878b5
--- /dev/null
+++ b/src/Agent.Core/Localization/LanguageContext.cs
@@ -0,0 +1,3 @@
+namespace Agent.Core.Localization;
+
+public sealed record LanguageContext(string Locale);
diff --git a/src/Agent.Core/Localization/LocaleCatalog.cs b/src/Agent.Core/Localization/LocaleCatalog.cs
new file mode 100644
index 0000000..3c49283
--- /dev/null
+++ b/src/Agent.Core/Localization/LocaleCatalog.cs
@@ -0,0 +1,97 @@
+using System.Text.Json;
+using System.Text.RegularExpressions;
+
+namespace Agent.Core.Localization;
+
+public sealed partial class LocaleCatalog
+{
+ private readonly IReadOnlyDictionary<string, IReadOnlyDictionary<string, string>> _locales;
+ private readonly IReadOnlyDictionary<string, string> _fallback;
+
+ private LocaleCatalog(string selectedLocale, IReadOnlyDictionary<string, IReadOnlyDictionary<string, string>> locales, IReadOnlyDictionary<string, string> fallback)
+ {
+ SelectedLocale = selectedLocale;
+ _locales = locales;
+ _fallback = fallback;
+ }
+
+ public string SelectedLocale { get; }
+ public IReadOnlyCollection<string> Keys => _fallback.Keys.ToArray();
+
+ public static LocaleCatalog Load(string directory, string selectedLocale, string fallbackLocale = "en")
+ {
+ if (!Directory.Exists(directory)) throw new LocaleException($"Locale directory does not exist: {directory}");
+
+ var locales = new Dictionary<string, IReadOnlyDictionary<string, string>>(StringComparer.OrdinalIgnoreCase);
+ foreach (string file in Directory.EnumerateFiles(directory, "*.json", SearchOption.TopDirectoryOnly))
+ {
+ string locale = Path.GetFileNameWithoutExtension(file);
+ locales.Add(locale, ReadFlatObject(file));
+ }
+
+ if (!locales.TryGetValue(fallbackLocale, out IReadOnlyDictionary<string, string>? fallback))
+ throw new LocaleException($"Fallback locale '{fallbackLocale}' is missing.");
+
+ HashSet<string> baselineKeys = fallback.Keys.ToHashSet(StringComparer.Ordinal);
+ Dictionary<string, HashSet<string>> baselinePlaceholders = fallback.ToDictionary(static p => p.Key, static p => Placeholders(p.Value), StringComparer.Ordinal);
+ foreach ((string locale, IReadOnlyDictionary<string, string> values) in locales)
+ {
+ string? unknown = values.Keys.FirstOrDefault(key => !baselineKeys.Contains(key));
+ if (unknown is not null) throw new LocaleException($"Locale '{locale}' contains unknown key '{unknown}'.");
+ foreach ((string key, string message) in values)
+ {
+ if (!baselinePlaceholders[key].SetEquals(Placeholders(message)))
+ throw new LocaleException($"Locale '{locale}' has mismatched placeholders for key '{key}'.");
+ }
+ }
+
+ if (!locales.ContainsKey(selectedLocale)) throw new LocaleException($"Selected locale '{selectedLocale}' is missing.");
+ return new LocaleCatalog(selectedLocale, locales, fallback);
+ }
+
+ public string Get(string key)
+ {
+ if (_locales[SelectedLocale].TryGetValue(key, out string? selected)) return selected;
+ return _fallback.TryGetValue(key, out string? fallback) ? fallback : key;
+ }
+
+ public string Format(string key, IReadOnlyDictionary<string, object?> values)
+ {
+ string template = Get(key);
+ return PlaceholderRegex().Replace(template, match =>
+ {
+ string name = match.Groups[1].Value;
+ if (!values.TryGetValue(name, out object? value)) throw new LocaleException($"Missing placeholder value '{name}' for key '{key}'.");
+ return Convert.ToString(value, System.Globalization.CultureInfo.InvariantCulture) ?? string.Empty;
+ });
+ }
+
+ private static Dictionary<string, string> ReadFlatObject(string file)
+ {
+ try
+ {
+ using JsonDocument document = JsonDocument.Parse(File.ReadAllText(file));
+ if (document.RootElement.ValueKind != JsonValueKind.Object) throw new LocaleException($"Locale file must contain a JSON object: {file}");
+ var result = new Dictionary<string, string>(StringComparer.Ordinal);
+ foreach (JsonProperty property in document.RootElement.EnumerateObject())
+ {
+ if (property.Value.ValueKind != JsonValueKind.String) throw new LocaleException($"Locale value '{property.Name}' must be a string.");
+ if (!result.TryAdd(property.Name, property.Value.GetString()!)) throw new LocaleException($"Duplicate locale key '{property.Name}'.");
+ }
+ return result;
+ }
+ catch (LocaleException)
+ {
+ throw;
+ }
+ catch (Exception ex) when (ex is JsonException or IOException or UnauthorizedAccessException)
+ {
+ throw new LocaleException($"Could not load locale file '{file}': {ex.Message}", ex);
+ }
+ }
+
+ private static HashSet<string> Placeholders(string text) => PlaceholderRegex().Matches(text).Select(static m => m.Groups[1].Value).ToHashSet(StringComparer.Ordinal);
+
+ [GeneratedRegex("\\{([A-Za-z][A-Za-z0-9_]*)\\}", RegexOptions.CultureInvariant)]
+ private static partial Regex PlaceholderRegex();
+}
diff --git a/src/Agent.Core/Localization/LocaleException.cs b/src/Agent.Core/Localization/LocaleException.cs
new file mode 100644
index 0000000..788a45c
--- /dev/null
+++ b/src/Agent.Core/Localization/LocaleException.cs
@@ -0,0 +1,4 @@
+namespace Agent.Core.Localization;
+
+public sealed class LocaleException(string message, Exception? innerException = null)
+ : Exception(message, innerException);