File size: 2,854 Bytes
c0bb696
 
 
98b4260
 
 
 
0fbb0a0
98b4260
c0bb696
 
 
4801335
c0bb696
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4801335
c0bb696
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b570a90
c0bb696
 
 
 
 
98b4260
c0bb696
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0fbb0a0
c0bb696
 
 
 
0fbb0a0
 
 
 
c0bb696
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98b4260
 
c0bb696
 
98b4260
c0bb696
 
98b4260
c0bb696
b570a90
c0bb696
 
98b4260
c0bb696
 
b570a90
 
60ada88
 
b570a90
 
c0bb696
 
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
/** @format */

"use strict";
const ID_RESPONSES = "id_responses";
const ID_DOC_NAMES = "id_doc_names";
const ID_DOCS = "id_docs;";
const ID_RAG = "id_rag";
const ID_THREAD = "id_thread";

const UaDb = {
  create(id, data) {
    if (localStorage.getItem(id)) {
      consoe.error(`ID ${id} already exists.`);
      return;
    }
    localStorage.setItem(id, data);
  },
  read(id) {
    const data = localStorage.getItem(id);
    if (data === null) {
      xlog(`UaDb.read  ${id} not found.`);
      return "";
    }
    return data;
  },
  update(id, data) {
    if (!localStorage.getItem(id)) {
      xlog(`UaDb.update ${id} not found.`);
      return;
    }
    localStorage.setItem(id, data);
  },
  delete(id) {
    if (!localStorage.getItem(id)) {
      consoe.error(`ID ${id} not found.`);
      return;
    }
    localStorage.removeItem(id);
  },
  save(id, data) {
    localStorage.setItem(id, data);
  },
  getAllIds() {
    const ids = [];
    for (let i = 0; i < localStorage.length; i++) {
      ids.push(localStorage.key(i));
    }
    return ids;
  },
  saveArray(id, arr) {
    const str = JSON.stringify(arr);
    UaDb.save(id, str);
  },
  readArray(id) {
    const str = UaDb.read(id);
    if (str.trim().length == 0) return [];
    const arr = JSON.parse(str);
    return arr;
  },
  saveJson(id, js) {
    const str = JSON.stringify(js);
    UaDb.save(id, str);
  },
  readJson(id) {
    const str = UaDb.read(id);
    if (!str) return {};
    const js = JSON.parse(str);
    return js;
  },
};

const DataMgr = {
  docs: [],
  doc_names: [],
  linkToName(link) {
    const parts = link.split("/");
    return parts[parts.length - 1];
  },
  async loadDoc(link) {
    this.readDbDocNames();
    try {
      const text = await requestGet(link);
      const doc = cleanDoc(text);
      const name = this.linkToName(link);
      if (this.doc_names.includes(name)) {
        alert(`Il docuento ${name} è già caricato`);
        return;
      }
      this.doc_names.push(name);
      this.docs.push(doc);
      this.saveDbDocs();
      return doc;
    } catch (err) {
      alert("loadDoc()\n" + err + "\n" + link);
    }
  },
  addDoc(name, text) {
    const doc = cleanDoc(text);
    this.docs.push(doc);
    this.doc_names.push(name);
    this.saveDbDocs();
  },
  saveDbDocs() {
    UaDb.saveArray(ID_DOC_NAMES, this.doc_names);
    UaDb.saveArray(ID_DOCS, this.docs);
  },
  readDbDocs() {
    this.docs = UaDb.readArray(ID_DOCS);
  },
  readDbDocNames() {
    this.doc_names = UaDb.readArray(ID_DOC_NAMES);
  },
  deleteJsonDati() {
    const ids = UaDb.getAllIds();
    for (const id of ids) {
      if ([ID_DOCS, ID_DOC_NAMES].includes(id)) continue;
      UaDb.delete(id);
    }
    Rag.ragQuery = "";
    Rag.ragContext = "";
    Rag.ragAnswer = "";
    Rag.answers= [];
    Rag.prompts = [];
    ThreadMgr.rows = [];
  },
};