File size: 5,192 Bytes
6111b2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"use client";

import { useState, useEffect } from "react";
import { Modal, Button, Input, Select } from "@/shared/components";
import { useTranslations } from "next-intl";

interface Memory {
  id: string;
  type: "factual" | "episodic" | "procedural" | "semantic";
  key: string;
  content: string;
  metadata: Record<string, unknown>;
}

interface Props {
  memory: Memory | null;
  isOpen: boolean;
  onClose: () => void;
  onSaved: () => void;
}

export default function EditMemoryModal({ memory, isOpen, onClose, onSaved }: Props) {
  const t = useTranslations("memory");
  const [type, setType] = useState<"factual" | "episodic" | "procedural" | "semantic">("factual");
  const [key, setKey] = useState("");
  const [content, setContent] = useState("");
  const [metadataStr, setMetadataStr] = useState("{}");
  const [metadataError, setMetadataError] = useState("");
  const [isSaving, setIsSaving] = useState(false);
  const [error, setError] = useState("");

  useEffect(() => {
    if (memory && isOpen) {
      setType(memory.type);
      setKey(memory.key);
      setContent(memory.content);
      setMetadataStr(JSON.stringify(memory.metadata ?? {}, null, 2));
      setMetadataError("");
      setError("");
    }
  }, [memory, isOpen]);

  const handleMetadataChange = (value: string) => {
    setMetadataStr(value);
    try {
      JSON.parse(value);
      setMetadataError("");
    } catch {
      setMetadataError(t("editModal.metadataInvalid"));
    }
  };

  const handleSave = async () => {
    if (!memory) return;
    if (metadataError) return;
    setIsSaving(true);
    setError("");
    try {
      let metadata: Record<string, unknown> = {};
      try {
        metadata = JSON.parse(metadataStr);
      } catch {
        setError(t("editModal.metadataInvalid"));
        return;
      }
      const res = await fetch(`/api/memory/${memory.id}`, {
        method: "PUT",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ type, key, content, metadata }),
      });
      if (res.ok) {
        onSaved();
        onClose();
      } else {
        const data = await res.json().catch(() => null);
        setError(data?.error?.message ?? t("editModal.saveFailed"));
      }
    } catch {
      setError(t("editModal.saveFailed"));
    } finally {
      setIsSaving(false);
    }
  };

  return (
    <Modal

      isOpen={isOpen}

      onClose={onClose}

      title={t("editModal.title")}

      footer={

        <>

          <Button variant="outline" onClick={onClose} disabled={isSaving}>

            {t("cancel")}

          </Button>

          <Button

            onClick={handleSave}

            loading={isSaving}

            disabled={!key.trim() || !content.trim() || Boolean(metadataError)}

          >

            {t("save")}

          </Button>

        </>
      }
    >
      <div className="space-y-4">

        {error && (

          <div className="p-3 rounded-lg bg-red-500/10 border border-red-500/20 text-xs text-red-400">

            {error}

          </div>

        )}

        <div>

          <label className="block text-sm font-medium mb-1">{t("type")}</label>

          <Select

            value={type}

            onChange={(e) => setType(e.target.value as typeof type)}

            className="w-full"

          >

            <option value="factual">{t("factual")}</option>

            <option value="episodic">{t("episodic")}</option>

            <option value="procedural">{t("procedural")}</option>

            <option value="semantic">{t("semantic")}</option>

          </Select>

        </div>

        <div>

          <label className="block text-sm font-medium mb-1">{t("key")}</label>

          <Input

            value={key}

            onChange={(e) => setKey(e.target.value)}

            placeholder={t("keyPlaceholder")}

            className="w-full"

          />

        </div>

        <div>

          <label className="block text-sm font-medium mb-1">{t("content")}</label>

          <textarea

            value={content}

            onChange={(e) => setContent(e.target.value)}

            placeholder={t("contentPlaceholder")}

            rows={4}

            className="w-full px-3 py-2 rounded-lg bg-background border border-border text-sm focus:outline-none focus:ring-1 focus:ring-violet-500 resize-y"

          />

        </div>

        <div>

          <label className="block text-sm font-medium mb-1">{t("editModal.metadataLabel")}</label>

          <textarea

            value={metadataStr}

            onChange={(e) => handleMetadataChange(e.target.value)}

            rows={4}

            spellCheck={false}

            className={`w-full px-3 py-2 rounded-lg bg-background border text-xs font-mono focus:outline-none focus:ring-1 focus:ring-violet-500 resize-y ${

              metadataError ? "border-red-500" : "border-border"

            }`}

          />

          {metadataError && (

            <p className="text-xs text-red-400 mt-1">{metadataError}</p>

          )}

        </div>

      </div>
    </Modal>
  );
}