Nerdur commited on
Commit
0bca1dd
Β·
verified Β·
1 Parent(s): bf28655

Upload install_functions.py

Browse files
Files changed (1) hide show
  1. install_functions.py +51 -25
install_functions.py CHANGED
@@ -3,6 +3,7 @@ import urllib.request
3
  import urllib.error
4
  import sys
5
  import os
 
6
 
7
  base_url = sys.argv[1] if len(sys.argv) > 1 else 'http://localhost:8080'
8
  token = sys.argv[2] if len(sys.argv) > 2 else ''
@@ -11,7 +12,12 @@ if not token:
11
  print("❌ No token provided.")
12
  sys.exit(1)
13
 
 
 
 
 
14
  def post(endpoint, data_dict):
 
15
  data = json.dumps(data_dict).encode()
16
  req = urllib.request.Request(
17
  base_url + endpoint,
@@ -23,65 +29,85 @@ def post(endpoint, data_dict):
23
  method='POST'
24
  )
25
  try:
26
- with urllib.request.urlopen(req) as r:
27
  return r.status, r.read().decode()
28
  except urllib.error.HTTPError as e:
29
  return e.code, e.read().decode()
30
  except Exception as e:
 
31
  return 0, str(e)
32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  # ── FUNCTIONS (Filter/Pipe klase) ──────────────────────────
34
  functions = [
35
  {
36
  "id": "context_detector",
37
  "name": "Context Detector",
38
  "type": "filter",
39
- "content": open("/app/context_detector.py").read() if os.path.exists("/app/context_detector.py") else "",
40
  "meta": {"description": "Automatski prepoznaje kontekst svake poruke."}
41
  },
42
  {
43
  "id": "token_saver",
44
  "name": "Token Saver",
45
  "type": "filter",
46
- "content": open("/app/token_compressor.py").read() if os.path.exists("/app/token_compressor.py") else "",
47
  "meta": {"description": "Ε tedi tokene ograničavanjem historije razgovora."}
48
  }
49
  ]
50
 
 
 
 
51
  print(f"Instaliram {len(functions)} funkcija (filtera)...")
52
  for func in functions:
53
- if not func["content"]:
54
- print(f"⚠️ {func['name']} β€” fajl nije pronaΔ‘en, preskačem")
55
- continue
56
- code, body = post('/api/v1/functions/create', func)
57
- if code == 200:
58
- print(f"βœ… {func['name']} β€” kreirana")
59
- elif 'already exists' in body.lower() or code == 409:
60
- print(f"⚠️ {func['name']} β€” veΔ‡ postoji")
61
- else:
62
- print(f"❌ {func['name']} β€” greΕ‘ka {code}: {body[:100]}")
63
 
64
  # ── TOOLS (Tools klasa) ────────────────────────────────────
65
  tools = [
66
  {
67
  "id": "smart_matcher",
68
  "name": "Smart Matcher",
69
- "content": open("/app/smart_matcher.py").read() if os.path.exists("/app/smart_matcher.py") else "",
70
  "meta": {"description": "Sparuje stavke iz dvije liste s confidence scorom."}
71
  }
72
  ]
73
 
 
 
 
74
  print(f"Instaliram {len(tools)} tool(a)...")
75
  for tool in tools:
76
- if not tool["content"]:
77
- print(f"⚠️ {tool['name']} β€” fajl nije pronaΔ‘en, preskačem")
78
- continue
79
- code, body = post('/api/v1/tools/create', tool)
80
- if code == 200:
81
- print(f"βœ… {tool['name']} β€” kreiran")
82
- elif 'already exists' in body.lower() or code == 409:
83
- print(f"⚠️ {tool['name']} β€” veΔ‡ postoji")
84
- else:
85
- print(f"❌ {tool['name']} β€” greΕ‘ka {code}: {body[:100]}")
86
 
87
- print("Gotovo!")
 
3
  import urllib.error
4
  import sys
5
  import os
6
+ import time
7
 
8
  base_url = sys.argv[1] if len(sys.argv) > 1 else 'http://localhost:8080'
9
  token = sys.argv[2] if len(sys.argv) > 2 else ''
 
12
  print("❌ No token provided.")
13
  sys.exit(1)
14
 
15
+ # Čekaj malo prije nego őto počneő - WebUI trebat vremena
16
+ print("Čekam da se WebUI stabilizira...")
17
+ time.sleep(5)
18
+
19
  def post(endpoint, data_dict):
20
+ """Sigurna POST funkcija s error handlingom"""
21
  data = json.dumps(data_dict).encode()
22
  req = urllib.request.Request(
23
  base_url + endpoint,
 
29
  method='POST'
30
  )
31
  try:
32
+ with urllib.request.urlopen(req, timeout=10) as r:
33
  return r.status, r.read().decode()
34
  except urllib.error.HTTPError as e:
35
  return e.code, e.read().decode()
36
  except Exception as e:
37
+ print(f"⚠️ POST error on {endpoint}: {str(e)}")
38
  return 0, str(e)
39
 
40
+ def read_file_safe(filepath):
41
+ """Siguran čitač datoteka"""
42
+ try:
43
+ if os.path.exists(filepath) and os.path.getsize(filepath) > 0:
44
+ with open(filepath, 'r', encoding='utf-8') as f:
45
+ return f.read()
46
+ else:
47
+ print(f"⚠️ {filepath} je prazna ili ne postoji")
48
+ return None
49
+ except Exception as e:
50
+ print(f"❌ Error reading {filepath}: {e}")
51
+ return None
52
+
53
  # ── FUNCTIONS (Filter/Pipe klase) ──────────────────────────
54
  functions = [
55
  {
56
  "id": "context_detector",
57
  "name": "Context Detector",
58
  "type": "filter",
59
+ "content": read_file_safe("/app/context_detector.py"),
60
  "meta": {"description": "Automatski prepoznaje kontekst svake poruke."}
61
  },
62
  {
63
  "id": "token_saver",
64
  "name": "Token Saver",
65
  "type": "filter",
66
+ "content": read_file_safe("/app/token_compressor.py"),
67
  "meta": {"description": "Ε tedi tokene ograničavanjem historije razgovora."}
68
  }
69
  ]
70
 
71
+ # Filtriraj funkcije koje nisu učitane
72
+ functions = [f for f in functions if f["content"]]
73
+
74
  print(f"Instaliram {len(functions)} funkcija (filtera)...")
75
  for func in functions:
76
+ try:
77
+ code, body = post('/api/v1/functions/create', func)
78
+ if code == 200:
79
+ print(f"βœ… {func['name']} β€” kreirana")
80
+ elif code == 409 or 'already exists' in body.lower():
81
+ print(f"⚠️ {func['name']} β€” veΔ‡ postoji (preskačem)")
82
+ else:
83
+ print(f"❌ {func['name']} β€” greΕ‘ka {code}")
84
+ except Exception as e:
85
+ print(f"❌ {func['name']} β€” exception: {e}")
86
 
87
  # ── TOOLS (Tools klasa) ────────────────────────────────────
88
  tools = [
89
  {
90
  "id": "smart_matcher",
91
  "name": "Smart Matcher",
92
+ "content": read_file_safe("/app/smart_matcher.py"),
93
  "meta": {"description": "Sparuje stavke iz dvije liste s confidence scorom."}
94
  }
95
  ]
96
 
97
+ # Filtriraj toolove koje nisu učitane
98
+ tools = [t for t in tools if t["content"]]
99
+
100
  print(f"Instaliram {len(tools)} tool(a)...")
101
  for tool in tools:
102
+ try:
103
+ code, body = post('/api/v1/tools/create', tool)
104
+ if code == 200:
105
+ print(f"βœ… {tool['name']} β€” kreiran")
106
+ elif code == 409 or 'already exists' in body.lower():
107
+ print(f"⚠️ {tool['name']} β€” veΔ‡ postoji (preskačem)")
108
+ else:
109
+ print(f"❌ {tool['name']} β€” greΕ‘ka {code}")
110
+ except Exception as e:
111
+ print(f"❌ {tool['name']} β€” exception: {e}")
112
 
113
+ print("βœ… Gotovo!")