mirror of
https://github.com/Direct-Dev-Ru/go-lcg.git
synced 2025-11-16 01:29:55 +00:00
before result functions add
This commit is contained in:
107
cmd/explain.go
Normal file
107
cmd/explain.go
Normal file
@@ -0,0 +1,107 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/atotto/clipboard"
|
||||
"github.com/direct-dev-ru/linux-command-gpt/config"
|
||||
"github.com/direct-dev-ru/linux-command-gpt/gpt"
|
||||
)
|
||||
|
||||
// ExplainDeps инъекция зависимостей для вывода и окружения
|
||||
type ExplainDeps struct {
|
||||
DisableHistory bool
|
||||
PrintColored func(string, string)
|
||||
ColorPurple string
|
||||
ColorGreen string
|
||||
ColorRed string
|
||||
ColorYellow string
|
||||
GetCommand func(gpt.Gpt3, string) (string, float64)
|
||||
}
|
||||
|
||||
// ShowDetailedExplanation делает дополнительный запрос с подробным описанием и альтернативами
|
||||
func ShowDetailedExplanation(command string, gpt3 gpt.Gpt3, system, originalCmd string, timeout int, level int, deps ExplainDeps) {
|
||||
var detailedSystem string
|
||||
switch level {
|
||||
case 1: // v — кратко
|
||||
detailedSystem = "Ты опытный Linux-инженер. Объясни КРАТКО, по делу: что делает команда и самые важные ключи. Без сравнений и альтернатив. Минимум текста. Пиши на русском."
|
||||
case 2: // vv — средне
|
||||
detailedSystem = "Ты опытный Linux-инженер. Дай сбалансированное объяснение: назначение команды, разбор основных ключей, 1-2 примера. Кратко упомяни 1-2 альтернативы без глубокого сравнения. Пиши на русском."
|
||||
default: // vvv — максимально подробно
|
||||
detailedSystem = "Ты опытный Linux-инженер. Дай подробное объяснение команды с полным разбором ключей, подкоманд, сценариев применения, примеров. Затем предложи альтернативные способы решения задачи другой командой/инструментами (со сравнениями и когда что лучше применять). Пиши на русском."
|
||||
}
|
||||
|
||||
ask := fmt.Sprintf("Объясни подробно команду и предложи альтернативы. Исходная команда: %s. Исходное задание пользователя: %s", command, originalCmd)
|
||||
detailed := gpt.NewGpt3(gpt3.ProviderType, config.AppConfig.Host, gpt3.ApiKey, gpt3.Model, detailedSystem, 0.2, timeout)
|
||||
|
||||
deps.PrintColored("\n🧠 Получаю подробное объяснение...\n", deps.ColorPurple)
|
||||
explanation, elapsed := deps.GetCommand(*detailed, ask)
|
||||
if explanation == "" {
|
||||
deps.PrintColored("❌ Не удалось получить подробное объяснение.\n", deps.ColorRed)
|
||||
return
|
||||
}
|
||||
|
||||
deps.PrintColored(fmt.Sprintf("✅ Готово за %.2f сек\n", elapsed), deps.ColorGreen)
|
||||
deps.PrintColored("\nВНИМАНИЕ: ОТВЕТ СФОРМИРОВАН ИИ. ТРЕБУЕТСЯ ПРОВЕРКА И КРИТИЧЕСКИЙ АНАЛИЗ. ВОЗМОЖНЫ ОШИБКИ И ГАЛЛЮЦИНАЦИИ.\n", deps.ColorRed)
|
||||
deps.PrintColored("\n📖 Подробное объяснение и альтернативы:\n\n", deps.ColorYellow)
|
||||
fmt.Println(explanation)
|
||||
|
||||
fmt.Printf("\nДействия: (c)копировать, (s)сохранить, (r)перегенерировать, (n)ничего: ")
|
||||
var choice string
|
||||
fmt.Scanln(&choice)
|
||||
switch strings.ToLower(choice) {
|
||||
case "c":
|
||||
clipboard.WriteAll(explanation)
|
||||
fmt.Println("✅ Объяснение скопировано в буфер обмена")
|
||||
case "s":
|
||||
saveExplanation(explanation, gpt3.Model, originalCmd, command, config.AppConfig.ResultFolder)
|
||||
case "r":
|
||||
fmt.Println("🔄 Перегенерирую подробное объяснение...")
|
||||
ShowDetailedExplanation(command, gpt3, system, originalCmd, timeout, level, deps)
|
||||
default:
|
||||
fmt.Println(" Возврат в основное меню.")
|
||||
}
|
||||
|
||||
if !deps.DisableHistory && (strings.ToLower(choice) == "c" || strings.ToLower(choice) == "s" || strings.ToLower(choice) == "n") {
|
||||
SaveToHistory(config.AppConfig.ResultHistory, config.AppConfig.ResultFolder, originalCmd, command, system, explanation)
|
||||
}
|
||||
}
|
||||
|
||||
// saveExplanation сохраняет подробное объяснение и альтернативные способы
|
||||
func saveExplanation(explanation string, model string, originalCmd string, commandResponse string, resultFolder string) {
|
||||
timestamp := time.Now().Format("2006-01-02_15-04-05")
|
||||
filename := fmt.Sprintf("gpt_explanation_%s_%s.md", model, timestamp)
|
||||
filePath := path.Join(resultFolder, filename)
|
||||
title := truncateTitle(originalCmd)
|
||||
content := fmt.Sprintf(
|
||||
"# %s\n\n## Prompt\n\n%s\n\n## Command\n\n%s\n\n## Explanation and Alternatives (model: %s)\n\n%s\n",
|
||||
title,
|
||||
originalCmd,
|
||||
commandResponse,
|
||||
model,
|
||||
explanation,
|
||||
)
|
||||
if err := os.WriteFile(filePath, []byte(content), 0644); err != nil {
|
||||
fmt.Println("Failed to save explanation:", err)
|
||||
} else {
|
||||
fmt.Printf("Saved to %s\n", filePath)
|
||||
}
|
||||
}
|
||||
|
||||
// truncateTitle сокращает строку до 120 символов (по рунам), добавляя " ..." при усечении
|
||||
func truncateTitle(s string) string {
|
||||
const maxLen = 120
|
||||
if runeCount := len([]rune(s)); runeCount <= maxLen {
|
||||
return s
|
||||
}
|
||||
const head = 116
|
||||
r := []rune(s)
|
||||
if len(r) <= head {
|
||||
return s
|
||||
}
|
||||
return string(r[:head]) + " ..."
|
||||
}
|
||||
157
cmd/history.go
Normal file
157
cmd/history.go
Normal file
@@ -0,0 +1,157 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type HistoryEntry struct {
|
||||
Index int `json:"index"`
|
||||
Command string `json:"command"`
|
||||
Response string `json:"response"`
|
||||
Explanation string `json:"explanation,omitempty"`
|
||||
System string `json:"system_prompt"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
}
|
||||
|
||||
func read(historyPath string) ([]HistoryEntry, error) {
|
||||
data, err := os.ReadFile(historyPath)
|
||||
if err != nil || len(data) == 0 {
|
||||
return nil, err
|
||||
}
|
||||
var items []HistoryEntry
|
||||
if err := json.Unmarshal(data, &items); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
func write(historyPath string, entries []HistoryEntry) error {
|
||||
for i := range entries {
|
||||
entries[i].Index = i + 1
|
||||
}
|
||||
out, err := json.MarshalIndent(entries, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.MkdirAll(filepath.Dir(historyPath), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(historyPath, out, 0644)
|
||||
}
|
||||
|
||||
func ShowHistory(historyPath string, printColored func(string, string), colorYellow string) {
|
||||
items, err := read(historyPath)
|
||||
if err != nil || len(items) == 0 {
|
||||
printColored("📝 История пуста\n", colorYellow)
|
||||
return
|
||||
}
|
||||
printColored("📝 История (из файла):\n", colorYellow)
|
||||
for _, h := range items {
|
||||
ts := h.Timestamp.Format("2006-01-02 15:04:05")
|
||||
fmt.Printf("%d. [%s] %s → %s\n", h.Index, ts, h.Command, h.Response)
|
||||
}
|
||||
}
|
||||
|
||||
func ViewHistoryEntry(historyPath string, id int, printColored func(string, string), colorYellow, colorBold, colorGreen string) {
|
||||
items, err := read(historyPath)
|
||||
if err != nil || len(items) == 0 {
|
||||
fmt.Println("История пуста или недоступна")
|
||||
return
|
||||
}
|
||||
var h *HistoryEntry
|
||||
for i := range items {
|
||||
if items[i].Index == id {
|
||||
h = &items[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
if h == nil {
|
||||
fmt.Println("Запись не найдена")
|
||||
return
|
||||
}
|
||||
printColored("\n📋 Команда:\n", colorYellow)
|
||||
printColored(fmt.Sprintf(" %s\n\n", h.Response), colorBold+colorGreen)
|
||||
if strings.TrimSpace(h.Explanation) != "" {
|
||||
printColored("\n📖 Подробное объяснение:\n\n", colorYellow)
|
||||
fmt.Println(h.Explanation)
|
||||
}
|
||||
}
|
||||
|
||||
func DeleteHistoryEntry(historyPath string, id int) error {
|
||||
items, err := read(historyPath)
|
||||
if err != nil || len(items) == 0 {
|
||||
return fmt.Errorf("история пуста или недоступна")
|
||||
}
|
||||
pos := -1
|
||||
for i := range items {
|
||||
if items[i].Index == id {
|
||||
pos = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if pos == -1 {
|
||||
return fmt.Errorf("запись не найдена")
|
||||
}
|
||||
items = append(items[:pos], items[pos+1:]...)
|
||||
return write(historyPath, items)
|
||||
}
|
||||
|
||||
func SaveToHistory(historyPath, resultFolder, cmdText, response, system string, explanationOptional ...string) error {
|
||||
var explanation string
|
||||
if len(explanationOptional) > 0 {
|
||||
explanation = explanationOptional[0]
|
||||
}
|
||||
items, _ := read(historyPath)
|
||||
duplicateIndex := -1
|
||||
for i, h := range items {
|
||||
if strings.EqualFold(strings.TrimSpace(h.Command), strings.TrimSpace(cmdText)) {
|
||||
duplicateIndex = i
|
||||
break
|
||||
}
|
||||
}
|
||||
entry := HistoryEntry{
|
||||
Index: len(items) + 1,
|
||||
Command: cmdText,
|
||||
Response: response,
|
||||
Explanation: explanation,
|
||||
System: system,
|
||||
Timestamp: time.Now(),
|
||||
}
|
||||
if duplicateIndex == -1 {
|
||||
items = append(items, entry)
|
||||
return write(historyPath, items)
|
||||
}
|
||||
fmt.Printf("\nЗапрос уже есть в истории от %s. Перезаписать? (y/N): ", items[duplicateIndex].Timestamp.Format("2006-01-02 15:04:05"))
|
||||
var ans string
|
||||
fmt.Scanln(&ans)
|
||||
if strings.ToLower(ans) == "y" || strings.ToLower(ans) == "yes" {
|
||||
entry.Index = items[duplicateIndex].Index
|
||||
items[duplicateIndex] = entry
|
||||
return write(historyPath, items)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CheckAndSuggestFromHistory(historyPath, cmdText string) (bool, *HistoryEntry) {
|
||||
items, err := read(historyPath)
|
||||
if err != nil || len(items) == 0 {
|
||||
return false, nil
|
||||
}
|
||||
for _, h := range items {
|
||||
if strings.EqualFold(strings.TrimSpace(h.Command), strings.TrimSpace(cmdText)) {
|
||||
fmt.Printf("\nВ истории найден похожий запрос от %s. Показать сохраненный результат? (y/N): ", h.Timestamp.Format("2006-01-02 15:04:05"))
|
||||
var ans string
|
||||
fmt.Scanln(&ans)
|
||||
if strings.ToLower(ans) == "y" || strings.ToLower(ans) == "yes" {
|
||||
return true, &h
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
Reference in New Issue
Block a user