Let the user inject a message into a running chain instead of only being
able to abort it. While a chain streams, typing + Enter sends the text to
a new /api/chat/steer endpoint; it is drained at the next iteration
boundary, wrapped with a hint so the model actively classifies it
("complements the task -> keep working" vs. "changes course -> adjust"),
and stored as a Type="steer" user message so it survives reload.
Shared infrastructure (provider-agnostic):
- SteeringQueue: out-of-band queue keyed by sessionId, mirrored from
StreamingTracker (Enqueue/Drain/Clear).
- Program.cs registers it as a singleton and passes it to AnthropicService.
- ChatEndpoint: POST /api/chat/steer enqueues only while IsStreaming,
Clear() in the finally.
- ChatService.BuildHistoryFromDb groups Type="steer" so no two
consecutive user turns are produced on re-chat.
- Frontend api.js/chat.js/bubbles.js/history.js + chat.css render the
optimistic steer bubble and the steer_injected SSE event; Enter during
a stream sends a steer instead of being blocked.
Anthropic API path:
- Drain point B (tool boundary, merged into the tool_result user turn)
and C (turn end -> inject + continue instead of finalize).
Claude CLI path (kill-and-restart):
- The agentic loop runs inside the claude child, so true mid-turn inject
is impossible. On a pending steer the child is killed at the next tool
boundary, progress is rebuilt from the DB as a <work_in_progress>
transcript, and the CLI is restarted with the steer appended.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
197 lines
7.3 KiB
C#
197 lines
7.3 KiB
C#
using System;
|
||
using System.IO;
|
||
using System.Net;
|
||
using System.Net.Http;
|
||
using System.Runtime.InteropServices;
|
||
using Gott.Endpoints;
|
||
using Gott.Middleware;
|
||
using Gott.Services;
|
||
using Microsoft.AspNetCore.Builder;
|
||
using Microsoft.AspNetCore.StaticFiles;
|
||
using Microsoft.Extensions.Configuration;
|
||
using Microsoft.Extensions.DependencyInjection;
|
||
using Microsoft.Extensions.FileProviders;
|
||
using Microsoft.Extensions.Hosting;
|
||
|
||
var builder = WebApplication.CreateBuilder(args);
|
||
|
||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||
builder.Services.AddWindowsService();
|
||
|
||
var dataDir = Path.Combine(AppContext.BaseDirectory, "data");
|
||
Directory.CreateDirectory(dataDir);
|
||
|
||
var dbPath = Path.Combine(dataDir, "gott.db");
|
||
|
||
// ServerSettingsService – zentrale Settings aus DB
|
||
var serverSettings = new ServerSettingsService(dbPath);
|
||
serverSettings.MigrateFromAppSettings(builder.Environment.ContentRootPath);
|
||
builder.Services.AddSingleton(serverSettings);
|
||
|
||
// Proxy (erfordert Neustart – wird nur beim Start gelesen)
|
||
var proxyUrl = serverSettings.Get("ProxyUrl");
|
||
if (!string.IsNullOrWhiteSpace(proxyUrl))
|
||
{
|
||
builder.Services.AddHttpClient().ConfigureHttpClientDefaults(b =>
|
||
b.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
|
||
{
|
||
Proxy = new WebProxy(proxyUrl),
|
||
UseProxy = true
|
||
}));
|
||
}
|
||
else
|
||
{
|
||
builder.Services.AddHttpClient();
|
||
}
|
||
|
||
var authService = new AuthService(dbPath);
|
||
var dbService = new DatabaseService(dbPath);
|
||
dbService.EnsureDefaultUser(authService);
|
||
|
||
builder.Services.AddSingleton(authService);
|
||
builder.Services.AddSingleton(dbService);
|
||
builder.Services.AddSingleton(new ImageService());
|
||
builder.Services.AddSingleton(new StreamingTracker());
|
||
var steeringQueue = new SteeringQueue();
|
||
builder.Services.AddSingleton(steeringQueue);
|
||
builder.Services.AddSingleton(new RemoteConnectionManager());
|
||
var memoryService = new MemoryService(dataDir);
|
||
var skillService = new SkillService(dataDir);
|
||
builder.Services.AddSingleton(memoryService);
|
||
builder.Services.AddSingleton(skillService);
|
||
|
||
// Shared Services
|
||
var toolService = new ToolService(serverSettings);
|
||
builder.Services.AddSingleton(toolService);
|
||
var chatService = new ChatService(dbService, toolService, memoryService, skillService, serverSettings);
|
||
builder.Services.AddSingleton(chatService);
|
||
|
||
// Embedding-Services (lokal, ONNX)
|
||
var embeddingModelDir = serverSettings.Get("EmbeddingModelDir");
|
||
var embeddingMaxSeq = serverSettings.GetInt("EmbeddingMaxSeqLen", 256);
|
||
var embeddingService = new Gott.Services.EmbeddingService(
|
||
string.IsNullOrEmpty(embeddingModelDir)
|
||
? null
|
||
: Path.Combine(dataDir, "models", embeddingModelDir),
|
||
embeddingMaxSeq
|
||
);
|
||
builder.Services.AddSingleton(embeddingService);
|
||
var vectorSearchService = new Gott.Services.VectorSearchService(dbPath, embeddingService);
|
||
builder.Services.AddSingleton(vectorSearchService);
|
||
var embeddingBgService = new Gott.Services.EmbeddingBackgroundService(vectorSearchService, embeddingService, dbPath);
|
||
builder.Services.AddSingleton(embeddingBgService);
|
||
builder.Services.AddHostedService(sp => sp.GetRequiredService<Gott.Services.EmbeddingBackgroundService>());
|
||
|
||
// AnthropicService (Singleton – Token wird pro Request vom User aufgelöst)
|
||
builder.Services.AddSingleton<AnthropicService>(sp =>
|
||
{
|
||
var svc = new AnthropicService(
|
||
sp.GetRequiredService<IHttpClientFactory>(),
|
||
toolService, dbService, memoryService,
|
||
serverSettings,
|
||
vectorSearch: vectorSearchService,
|
||
embeddingBg: embeddingBgService,
|
||
steeringQueue: steeringQueue);
|
||
svc.SetChatService(chatService);
|
||
return svc;
|
||
});
|
||
|
||
// OpenAI-kompatibler Service (Nvidia etc.)
|
||
builder.Services.AddSingleton<OpenAiService>(sp =>
|
||
new OpenAiService(sp.GetRequiredService<IHttpClientFactory>(), dbService, sp.GetRequiredService<ToolService>(), serverSettings, dataDir, vectorSearchService));
|
||
|
||
// Telegram Bot (pro Session – Token aus Session-Config)
|
||
builder.Services.AddSingleton<TelegramService>(sp =>
|
||
new TelegramService(
|
||
sp.GetRequiredService<IHttpClientFactory>(),
|
||
dbService, chatService,
|
||
sp.GetRequiredService<AnthropicService>(),
|
||
sp.GetRequiredService<OpenAiService>(),
|
||
serverSettings));
|
||
|
||
// Krea Image Generation
|
||
builder.Services.AddSingleton(sp =>
|
||
new KreaService(sp.GetRequiredService<IHttpClientFactory>(), serverSettings, dataDir));
|
||
|
||
// Cronjob-Service (BackgroundService)
|
||
builder.Services.AddSingleton<CronjobService>();
|
||
builder.Services.AddHostedService<CronjobService>(sp => sp.GetRequiredService<CronjobService>());
|
||
|
||
// Key-Scanner-Service (BackgroundService mit Timer)
|
||
builder.Services.AddSingleton<KeyScannerService>();
|
||
builder.Services.AddHostedService<KeyScannerService>(sp => sp.GetRequiredService<KeyScannerService>());
|
||
|
||
// GitHub Event Scanner (Echtzeit-Monitoring öffentlicher Pushes)
|
||
builder.Services.AddSingleton<GitHubEventScannerService>();
|
||
builder.Services.AddHostedService<GitHubEventScannerService>(sp => sp.GetRequiredService<GitHubEventScannerService>());
|
||
|
||
// Claude CLI Service (für OAuth-basierte Chats via CLI)
|
||
builder.Services.AddSingleton<ClaudeCliService>();
|
||
|
||
var app = builder.Build();
|
||
|
||
// No-cache for static files (mobile browsers love caching too much)
|
||
app.Use(async (context, next) =>
|
||
{
|
||
var path = context.Request.Path.Value ?? "";
|
||
if (path.EndsWith(".html") || path.EndsWith(".js") || path.EndsWith(".css") || path == "/")
|
||
{
|
||
var headers = context.Response.Headers;
|
||
headers["Cache-Control"] = "no-cache, no-store, must-revalidate";
|
||
headers["Pragma"] = "no-cache";
|
||
headers["Expires"] = "0";
|
||
}
|
||
await next();
|
||
});
|
||
|
||
toolService.SetHttpClientFactory(app.Services.GetRequiredService<IHttpClientFactory>());
|
||
app.Services.GetRequiredService<AnthropicService>().SetTelegramService(app.Services.GetRequiredService<TelegramService>());
|
||
app.Services.GetRequiredService<AnthropicService>().SetOpenAiService(app.Services.GetRequiredService<OpenAiService>());
|
||
app.Services.GetRequiredService<AnthropicService>().SetCliService(app.Services.GetRequiredService<ClaudeCliService>());
|
||
await app.Services.GetRequiredService<OpenAiService>().LoadModelsAsync();
|
||
app.UseWebSockets();
|
||
|
||
// wwwroot: Quell-Verzeichnis bevorzugen (Live-Änderungen ohne Publish)
|
||
AppPaths.Initialize();
|
||
if (AppPaths.WwwRoot != Path.Combine(AppContext.BaseDirectory, "wwwroot"))
|
||
{
|
||
var fileProvider = new PhysicalFileProvider(AppPaths.WwwRoot);
|
||
app.UseDefaultFiles(new DefaultFilesOptions { FileProvider = fileProvider });
|
||
app.UseStaticFiles(new StaticFileOptions { FileProvider = fileProvider });
|
||
}
|
||
else
|
||
{
|
||
app.UseDefaultFiles();
|
||
app.UseStaticFiles();
|
||
}
|
||
|
||
app.UseMiddleware<AuthMiddleware>();
|
||
|
||
app.MapAuthEndpoints();
|
||
app.MapChatEndpoint();
|
||
app.MapHistoryEndpoint();
|
||
app.MapSessionEndpoints();
|
||
app.MapAgentEndpoints();
|
||
app.MapWorkspaceEndpoint();
|
||
app.MapInfoEndpoint();
|
||
app.MapModelsEndpoint();
|
||
app.MapBackupEndpoint();
|
||
app.MapSkillEndpoints();
|
||
app.MapProfileEndpoints();
|
||
app.MapSettingsEndpoints();
|
||
app.MapTelegramEndpoints();
|
||
app.MapCronjobEndpoints();
|
||
app.MapKreaEndpoints();
|
||
app.MapSharedEndpoints();
|
||
app.MapAdminEndpoints();
|
||
app.MapServerSettingsEndpoints();
|
||
app.MapApiKeysEndpoints();
|
||
app.MapOpenClawEndpoints();
|
||
app.MapRemoteEndpoints();
|
||
app.MapDatabaseExplorerEndpoints();
|
||
app.MapEmbeddingDebugEndpoints();
|
||
app.MapInternalEndpoints();
|
||
|
||
var port = app.Configuration.GetValue<int>("Port", 19000);
|
||
app.Run($"http://0.0.0.0:{port}");
|