This commit is contained in:
@@ -1,38 +1,116 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using System.Linq;
|
||||
|
||||
namespace VisionAsist.Models;
|
||||
|
||||
public class Selector
|
||||
{
|
||||
public static void selector(string text)
|
||||
private static List<OllamaMessage> _chatHistory = new();
|
||||
public static string CurrentModel = "qwen3.5:4b";
|
||||
|
||||
public static event Action<string>? OnLogUpdate;
|
||||
private static void UpdateUI(string text) => OnLogUpdate?.Invoke(text);
|
||||
|
||||
public static async Task<string> selector(string text)
|
||||
{
|
||||
if (text.Contains("вижен"))
|
||||
try
|
||||
{
|
||||
string novision = text.Replace("вижен", "").Trim();
|
||||
foreach (var module in Core.modulelist)
|
||||
UpdateUI($"\n[Вы]: {text}\n");
|
||||
_chatHistory.Add(new OllamaMessage { role = "user", content = text });
|
||||
|
||||
while (true)
|
||||
{
|
||||
foreach (var command in module.commands)
|
||||
string toolsJson = Core.GetToolsManifestJson();
|
||||
string fullContent = "";
|
||||
string toolCallsRaw = "";
|
||||
bool isThinking = false;
|
||||
bool hasStartedContent = false;
|
||||
|
||||
UpdateUI("\n[ИИ]: ");
|
||||
|
||||
await foreach (var line in OllamaService.SendChatStreamAsync(CurrentModel, _chatHistory, toolsJson))
|
||||
{
|
||||
if (command.Contains("*"))
|
||||
using var doc = JsonDocument.Parse(line);
|
||||
var root = doc.RootElement;
|
||||
if (!root.TryGetProperty("message", out var message)) continue;
|
||||
|
||||
// 1. ОБРАБОТКА 'thinking' (если есть в этом чанке)
|
||||
if (message.TryGetProperty("thinking", out var thinkingEl))
|
||||
{
|
||||
string wopo = command.Replace("*", "").Trim();
|
||||
if (novision.Contains(wopo))
|
||||
string thinkPart = thinkingEl.GetString() ?? "";
|
||||
if (!string.IsNullOrEmpty(thinkPart))
|
||||
{
|
||||
|
||||
Console.WriteLine(module.Module.Execute(command));
|
||||
break;
|
||||
if (!isThinking)
|
||||
{
|
||||
UpdateUI("\n[Мысли]: ");
|
||||
isThinking = true;
|
||||
}
|
||||
UpdateUI(thinkPart);
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
// 2. ОБРАБОТКА 'content' (если есть в этом чанке)
|
||||
if (message.TryGetProperty("content", out var contentEl))
|
||||
{
|
||||
if (command == novision)
|
||||
string part = contentEl.GetString() ?? "";
|
||||
if (!string.IsNullOrEmpty(part))
|
||||
{
|
||||
Console.WriteLine(module.Module.Execute(novision));
|
||||
break;
|
||||
// Если мы только что "думали", переключаемся на ответ
|
||||
if (isThinking)
|
||||
{
|
||||
isThinking = false;
|
||||
hasStartedContent = true;
|
||||
UpdateUI("\n[Ответ]: ");
|
||||
}
|
||||
else if (!hasStartedContent && !string.IsNullOrWhiteSpace(part))
|
||||
{
|
||||
UpdateUI("\n[Ответ]: ");
|
||||
hasStartedContent = true;
|
||||
}
|
||||
|
||||
fullContent += part;
|
||||
UpdateUI(part);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. ОБРАБОТКА 'tool_calls' (если есть в этом чанке)
|
||||
if (message.TryGetProperty("tool_calls", out var toolsEl))
|
||||
{
|
||||
toolCallsRaw = toolsEl.GetRawText();
|
||||
}
|
||||
}
|
||||
|
||||
_chatHistory.Add(new OllamaMessage { role = "assistant", content = fullContent });
|
||||
|
||||
// 4. ВЫПОЛНЕНИЕ ИНСТРУМЕНТОВ (после завершения стрима)
|
||||
if (!string.IsNullOrEmpty(toolCallsRaw))
|
||||
{
|
||||
using var toolDoc = JsonDocument.Parse(toolCallsRaw);
|
||||
foreach (var call in toolDoc.RootElement.EnumerateArray())
|
||||
{
|
||||
string toolName = call.GetProperty("function").GetProperty("name").GetString() ?? "";
|
||||
string argsJson = call.GetProperty("function").GetProperty("arguments").GetRawText();
|
||||
|
||||
UpdateUI($"\n\n[ДЕЙСТВИЕ]: {toolName}\n[АРГУМЕНТЫ]: {argsJson}");
|
||||
string result = Core.CallTool(toolName, argsJson);
|
||||
UpdateUI($"\n[РЕЗУЛЬТАТ]: {result}\n");
|
||||
|
||||
_chatHistory.Add(new OllamaMessage { role = "tool", content = result });
|
||||
}
|
||||
continue; // Снова к ИИ с результатами инструментов
|
||||
}
|
||||
return fullContent;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
UpdateUI($"\n[ОШИБКА]: {ex.Message}");
|
||||
return ex.Message;
|
||||
}
|
||||
}
|
||||
|
||||
public static void ClearHistory() => _chatHistory.Clear();
|
||||
}
|
||||
Reference in New Issue
Block a user