Ollama/Lilith.Ollama.cscsharp

Documentation

Ollama

Wiring between Lilith and the shared Agent-Core Ollama client.

Lilith.Ollama.cs

  • InitializeOllamaAsync — creates the client with model, system prompt, and optional auto-start of the Ollama service.
  • Exposes History, ContextMax, and CurrentContextTokens from the underlying client.
  • Subscribes to high context usage warnings for the console UI.
using System.Linq;using Agent.Core.Logging;using Agent.Core.Ollama;namespace Lilith.Agent;public partial class Lilith{    private OllamaClient _client = null!;    public ChatHistory History => _client.History;    public int ContextMax => _client.ContextMax;    public int CurrentContextTokens => _client.CurrentContextTokens;    private async Task InitializeOllamaAsync(bool startServerIfNotRunning)    {        _client = await OllamaClient.CreateClient(            Credentials.Model,            Credentials.EmbeddingModel,            Credentials.SystemPrompt,            Logger,            startServerIfNotRunning,            agentName: Credentials.AgentName);        _client.InitMemory(GetMemoryFilePath());        _client.ToolRegistry = BuildToolRegistry();        _client.OnContextHighUsage += pct =>            Logger.WriteLine("System", $"\u26a0 Context window is {pct}% full — history is getting long.", LogColors.Orange);    }    private async Task InjectMemoryContextIntoSystemPromptAsync()    {        if (_client.Memory is null || History.Count == 0)        {            return;        }        var leaves = _client.Memory.Leaves;        if (leaves.Count == 0)        {            return;        }        var lines = new List<string> { "", "Known memory (use retrieve_memory / get_memory_at_path; do not read these from workspace):" };        foreach (var leaf in leaves.OrderBy(l => l.Path, StringComparer.OrdinalIgnoreCase).Take(24))        {            lines.Add($"- {leaf.Path}: {leaf.Value}");        }        string block = string.Join("\n", lines);        var system = History[0];        if (!system.Content.Contains("Known memory", StringComparison.Ordinal))        {            History[0] = system with { Content = system.Content.TrimEnd() + block };        }        await Task.CompletedTask;    }}