File size: 5,530 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"use client";

import { useState, useEffect, useRef } from "react";
import { Card, Toggle } from "@/shared/components";
import { useTranslations } from "next-intl";

export default function SystemPromptTab() {
  const [config, setConfig] = useState({ enabled: false, prefixPrompt: "", suffixPrompt: "" });
  const [loading, setLoading] = useState(true);
  const [status, setStatus] = useState("");
  const [debounceTimer, setDebounceTimer] = useState(null);
  const configRef = useRef(config);
  const t = useTranslations("settings");

  useEffect(() => {
    fetch("/api/settings/system-prompt")
      .then((res) => res.json())
      .then((data) => {
        setConfig({
          enabled: data?.enabled ?? false,
          prefixPrompt: data?.prefixPrompt ?? "",
          suffixPrompt: data?.suffixPrompt ?? "",
        });
        setLoading(false);
      })
      .catch(() => setLoading(false));
  }, []);

  const save = async (updates) => {
    const newConfig = { ...configRef.current, ...updates };
    setConfig(newConfig);
    configRef.current = newConfig;
    setStatus("");
    try {
      const res = await fetch("/api/settings/system-prompt", {
        method: "PUT",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(newConfig),
      });
      if (res.ok) {
        setStatus("saved");
        setTimeout(() => setStatus(""), 2000);
      }
    } catch {
      setStatus("error");
    }
  };

  const handleFieldChange = (field, text) => {
    const updated = { ...configRef.current, [field]: text };
    setConfig(updated);
    configRef.current = updated;
    if (debounceTimer) clearTimeout(debounceTimer);
    setDebounceTimer(
      setTimeout(() => {
        save({ [field]: text });
      }, 800)
    );
  };

  return (
    <Card>
      <div className="flex items-center gap-3 mb-5">
        <div className="p-2 rounded-lg bg-amber-500/10 text-amber-500">
          <span className="material-symbols-outlined text-[20px]" aria-hidden="true">
            edit_note
          </span>
        </div>
        <div className="flex-1">
          <h3 className="text-lg font-semibold">{t("globalSystemPrompt")}</h3>
        </div>
        <div className="flex items-center gap-3">
          {status === "saved" && (
            <span className="text-xs font-medium text-emerald-500 flex items-center gap-1">
              <span className="material-symbols-outlined text-[14px]">check_circle</span>{" "}
              {t("saved")}
            </span>
          )}
          <Toggle
            checked={config.enabled}
            onChange={() => save({ enabled: !config.enabled })}
            disabled={loading}
          />
        </div>
      </div>

      {config.enabled && (
        <div className="flex flex-col gap-5">
          {/* Before Prompt — injected BEFORE agent/provider instructions */}
          <div className="flex flex-col gap-2">
            <label className="text-sm font-medium text-text-secondary flex items-center gap-1.5">
              <span className="material-symbols-outlined text-[16px]">vertical_align_top</span>
              {t("beforePromptLabel")}
            </label>
            <p className="text-xs text-text-muted/70">{t("beforePromptDesc")}</p>
            <div className="relative">
              <textarea
                value={config.prefixPrompt}
                onChange={(e) => handleFieldChange("prefixPrompt", e.target.value)}
                placeholder={t("beforePromptPlaceholder")}
                rows={9}
                className="w-full px-4 py-3 rounded-lg border border-border/50 bg-surface/30 text-sm
                           placeholder:text-text-muted/50 resize-y min-h-[220px]
                           focus:outline-none focus:ring-1 focus:ring-amber-500/30 focus:border-amber-500/50
                           transition-colors"
                disabled={loading}
              />
              <div className="absolute bottom-2 right-3 text-xs text-text-muted/60 tabular-nums">
                {t("chars", { count: config.prefixPrompt.length })}
              </div>
            </div>
          </div>

          {/* After Prompt — injected AFTER agent/provider instructions */}
          <div className="flex flex-col gap-2">
            <label className="text-sm font-medium text-text-secondary flex items-center gap-1.5">
              <span className="material-symbols-outlined text-[16px]">vertical_align_bottom</span>
              {t("afterPromptLabel")}
            </label>
            <p className="text-xs text-text-muted/70">{t("afterPromptDesc")}</p>
            <div className="relative">
              <textarea
                value={config.suffixPrompt}
                onChange={(e) => handleFieldChange("suffixPrompt", e.target.value)}
                placeholder={t("afterPromptPlaceholder")}
                rows={9}
                className="w-full px-4 py-3 rounded-lg border border-border/50 bg-surface/30 text-sm
                           placeholder:text-text-muted/50 resize-y min-h-[220px]
                           focus:outline-none focus:ring-1 focus:ring-amber-500/30 focus:border-amber-500/50
                           transition-colors"
                disabled={loading}
              />
              <div className="absolute bottom-2 right-3 text-xs text-text-muted/60 tabular-nums">
                {t("chars", { count: config.suffixPrompt.length })}
              </div>
            </div>
          </div>
        </div>
      )}
    </Card>
  );
}