tzero-mcp 11.0.0 → 11.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +56 -56
- package/CONTRIBUTING.md +71 -71
- package/LICENSE +21 -21
- package/README.md +64 -64
- package/bin/tzero-mcp.js +62 -62
- package/bin/tzero.js +61 -61
- package/index.d.ts +25 -25
- package/index.js +15 -15
- package/package.json +89 -88
- package/src/addmcp.py +1203 -1197
- package/src/app.py +1810 -1810
- package/src/main.py +60 -60
- package/src/pc_fixer_mcp.py +25 -25
- package/src/tzero/__init__.py +54 -54
- package/src/tzero/__main__.py +6 -6
- package/src/tzero/siber_akademi_icon.py +77 -77
- package/src/tzero/tzero_arch_test.py +463 -463
- package/src/tzero/tzero_benchmark.py +475 -475
- package/src/tzero/tzero_blackboard.py +257 -257
- package/src/tzero/tzero_bridge.py +546 -546
- package/src/tzero/tzero_budget.py +195 -195
- package/src/tzero/tzero_card.py +431 -0
- package/src/tzero/tzero_ci.py +153 -153
- package/src/tzero/tzero_commit.py +192 -192
- package/src/tzero/tzero_contracts.py +1093 -1093
- package/src/tzero/tzero_cost.py +166 -166
- package/src/tzero/tzero_dashboard.py +1960 -1960
- package/src/tzero/tzero_debt.py +461 -461
- package/src/tzero/tzero_deps.py +462 -462
- package/src/tzero/tzero_drift.py +208 -208
- package/src/tzero/tzero_explain.py +306 -306
- package/src/tzero/tzero_features.py +3024 -3024
- package/src/tzero/tzero_federation.py +299 -299
- package/src/tzero/tzero_fix.py +557 -228
- package/src/tzero/tzero_fixer/__init__.py +63 -63
- package/src/tzero/tzero_fixer/amalgamation.py +334 -334
- package/src/tzero/tzero_fixer/ast_syntax_healer.py +464 -464
- package/src/tzero/tzero_fixer/ast_type_modernizer.py +288 -288
- package/src/tzero/tzero_fixer/async_io_modernizer.py +127 -127
- package/src/tzero/tzero_fixer/automated_regression_suite.py +600 -600
- package/src/tzero/tzero_fixer/benchmarks_and_diagnostics.py +174 -174
- package/src/tzero/tzero_fixer/ci_cd_self_healing_gate.py +246 -246
- package/src/tzero/tzero_fixer/code_duplicity_and_refactorer.py +151 -151
- package/src/tzero/tzero_fixer/code_smells_healer.py +260 -260
- package/src/tzero/tzero_fixer/cwe_remediator.py +171 -171
- package/src/tzero/tzero_fixer/dead_code_eliminator.py +150 -150
- package/src/tzero/tzero_fixer/dependency_vulnerability_auditor.py +309 -309
- package/src/tzero/tzero_fixer/git_advanced_surgeon.py +280 -280
- package/src/tzero/tzero_fixer/hardware_and_disk_doctor.py +325 -325
- package/src/tzero/tzero_fixer/import_graph_healer.py +412 -412
- package/src/tzero/tzero_fixer/interactive_tui.py +91 -91
- package/src/tzero/tzero_fixer/knowledge_base.py +295 -295
- package/src/tzero/tzero_fixer/knowledge_base_extended.py +940 -940
- package/src/tzero/tzero_fixer/linters_and_static_analyzer.py +307 -307
- package/src/tzero/tzero_fixer/mcp_server.py +489 -489
- package/src/tzero/tzero_fixer/models.py +385 -385
- package/src/tzero/tzero_fixer/orchestrator.py +375 -375
- package/src/tzero/tzero_fixer/os_doctor.py +502 -502
- package/src/tzero/tzero_fixer/packaging_abi_surgeon.py +323 -323
- package/src/tzero/tzero_fixer/pc_fixer_doctor.py +600 -600
- package/src/tzero/tzero_fixer/performance_and_memory_profiler.py +274 -274
- package/src/tzero/tzero_fixer/polyglot_ast_parsers.py +313 -313
- package/src/tzero/tzero_fixer/polyglot_build_healer.py +364 -364
- package/src/tzero/tzero_fixer/polyglot_config_healer.py +318 -318
- package/src/tzero/tzero_fixer/polyglot_doctor.py +372 -372
- package/src/tzero/tzero_fixer/polyglot_linters.py +619 -619
- package/src/tzero/tzero_fixer/polyglot_schema_validator.py +305 -305
- package/src/tzero/tzero_fixer/security_cwe_database.py +444 -444
- package/src/tzero/tzero_fixer/security_deep_rules.py +114 -114
- package/src/tzero/tzero_fixer/security_rules_catalog.py +110 -110
- package/src/tzero/tzero_fixer/security_zero_leak.py +326 -326
- package/src/tzero/tzero_fixer/smart_patch_recommender.py +223 -223
- package/src/tzero/tzero_fixer/synthetic_workspace_generator.py +220 -220
- package/src/tzero/tzero_fixer/system_diagnostics_extended.py +493 -493
- package/src/tzero/tzero_fixer/system_repair_recipes.py +447 -447
- package/src/tzero/tzero_fixer/venv_doctor.py +309 -309
- package/src/tzero/tzero_golden.py +319 -319
- package/src/tzero/tzero_graph_hud.html +338 -338
- package/src/tzero/tzero_graph_rag.py +263 -263
- package/src/tzero/tzero_growth.py +415 -0
- package/src/tzero/tzero_guard.py +251 -243
- package/src/tzero/tzero_hooks.py +565 -565
- package/src/tzero/tzero_infra.py +274 -274
- package/src/tzero/tzero_jarvis/__init__.py +134 -124
- package/src/tzero/tzero_jarvis/causal_graph.py +310 -310
- package/src/tzero/tzero_jarvis/creative_director.py +333 -0
- package/src/tzero/tzero_jarvis/graph_visualizer.py +395 -395
- package/src/tzero/tzero_jarvis/hybrid_search.py +214 -214
- package/src/tzero/tzero_jarvis/latent_press.py +137 -137
- package/src/tzero/tzero_jarvis/memory_consolidation.py +212 -212
- package/src/tzero/tzero_jarvis/minhash_lsh.py +184 -184
- package/src/tzero/tzero_jarvis/orchestrator.py +335 -335
- package/src/tzero/tzero_jarvis/self_healing.py +308 -308
- package/src/tzero/tzero_jarvis/semantic_compressor.py +887 -887
- package/src/tzero/tzero_jarvis/swarm.py +261 -261
- package/src/tzero/tzero_jarvis/swarm_bft.py +144 -144
- package/src/tzero/tzero_jarvis/workspace_orchestrator.py +1167 -1167
- package/src/tzero/tzero_jarvis/zero_key_router.py +173 -173
- package/src/tzero/tzero_lsp.py +634 -634
- package/src/tzero/tzero_mcp.py +5314 -5237
- package/src/tzero/tzero_mutation.py +358 -358
- package/src/tzero/tzero_neural_cortex.py +1425 -1425
- package/src/tzero/tzero_packer.py +577 -577
- package/src/tzero/tzero_patch.py +624 -624
- package/src/tzero/tzero_pc_fixer.py +12699 -12699
- package/src/tzero/tzero_pipe.py +95 -95
- package/src/tzero/tzero_polyglot.py +799 -799
- package/src/tzero/tzero_proxy.py +127 -127
- package/src/tzero/tzero_reachability.py +272 -272
- package/src/tzero/tzero_replay.py +141 -141
- package/src/tzero/tzero_rust_bridge.py +198 -198
- package/src/tzero/tzero_scip.py +368 -368
- package/src/tzero/tzero_security.py +573 -573
- package/src/tzero/tzero_semantic_merge.py +189 -189
- package/src/tzero/tzero_snapshot.py +471 -471
- package/src/tzero/tzero_stubs.py +313 -313
- package/src/tzero/tzero_subagent_engine.py +459 -459
- package/src/tzero/tzero_superdesign.py +774 -774
- package/src/tzero/tzero_sync.py +241 -241
- package/src/tzero/tzero_task_benchmark.py +506 -458
- package/src/tzero/tzero_tui.py +313 -313
- package/src/tzero/tzero_turbo.py +264 -264
- package/src/tzero/tzero_v3.py +9997 -9900
- package/src/tzero.py +6 -6
- package/src/tzero_mcp.py +25 -0
- package/test/package.test.js +55 -55
|
@@ -1,214 +1,214 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
"""
|
|
3
|
-
T-ZERO ZERO-VECTOR-DB HYBRID IN-MEMORY SEARCH ENGINE
|
|
4
|
-
Developer: Toprak Ahmet Aydoğmuş (Siber Akademi)
|
|
5
|
-
Version: 4.4.0
|
|
6
|
-
|
|
7
|
-
Bu modül, harici hiçbir ağır Vektör Veritabanına (ChromaDB, Pinecone, FAISS vb.)
|
|
8
|
-
ihtiyaç duymadan; saf bellek içi (In-Memory) Ters İndeksleme (Inverted Index),
|
|
9
|
-
BM25 Anlamsal Sıralama ve T-Zero Latent Codebook (#code) eşleştirmesini
|
|
10
|
-
birleştiren mikro-saniye (<1 ms) hızında hibrit arama motorudur.
|
|
11
|
-
"""
|
|
12
|
-
|
|
13
|
-
from __future__ import annotations
|
|
14
|
-
import math
|
|
15
|
-
import re
|
|
16
|
-
import time
|
|
17
|
-
from typing import Dict, List, Optional, Set, Tuple, Any
|
|
18
|
-
from dataclasses import dataclass, field
|
|
19
|
-
from collections import defaultdict
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
@dataclass
|
|
23
|
-
class SearchDocument:
|
|
24
|
-
doc_id: str
|
|
25
|
-
doc_type: str # "code_symbol", "triplet", "memory", "causal_event"
|
|
26
|
-
title: str
|
|
27
|
-
content: str
|
|
28
|
-
discrete_code: Optional[int] = None
|
|
29
|
-
domain: str = "GENERIC"
|
|
30
|
-
metadata: Dict[str, Any] = field(default_factory=dict)
|
|
31
|
-
tokens: List[str] = field(default_factory=list)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
@dataclass
|
|
35
|
-
class SearchResult:
|
|
36
|
-
doc_id: str
|
|
37
|
-
doc_type: str
|
|
38
|
-
title: str
|
|
39
|
-
content: str
|
|
40
|
-
score: float
|
|
41
|
-
discrete_code: Optional[int] = None
|
|
42
|
-
domain: str = "GENERIC"
|
|
43
|
-
match_reasons: List[str] = field(default_factory=list)
|
|
44
|
-
|
|
45
|
-
def to_dict(self) -> Dict[str, Any]:
|
|
46
|
-
return {
|
|
47
|
-
"doc_id": self.doc_id,
|
|
48
|
-
"doc_type": self.doc_type,
|
|
49
|
-
"title": self.title,
|
|
50
|
-
"content": self.content,
|
|
51
|
-
"score": round(self.score, 4),
|
|
52
|
-
"discrete_code": self.discrete_code,
|
|
53
|
-
"domain": self.domain,
|
|
54
|
-
"match_reasons": self.match_reasons
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
class ZeroVectorDBSearchEngine:
|
|
59
|
-
"""
|
|
60
|
-
Sıfır Dış Bağımlılıklı, Bellek İçi BM25 + T-Zero Latent Codebook Hibrit Arama Motoru.
|
|
61
|
-
"""
|
|
62
|
-
|
|
63
|
-
def __init__(self, k1: float = 1.5, b: float = 0.75):
|
|
64
|
-
self.k1 = k1
|
|
65
|
-
self.b = b
|
|
66
|
-
self.documents: Dict[str, SearchDocument] = {}
|
|
67
|
-
self.inverted_index: Dict[str, Set[str]] = defaultdict(set)
|
|
68
|
-
self.code_index: Dict[int, Set[str]] = defaultdict(set)
|
|
69
|
-
self.domain_index: Dict[str, Set[str]] = defaultdict(set)
|
|
70
|
-
self.doc_lengths: Dict[str, int] = {}
|
|
71
|
-
self.avg_doc_len: float = 0.0
|
|
72
|
-
|
|
73
|
-
@staticmethod
|
|
74
|
-
def tokenize(text: str) -> List[str]:
|
|
75
|
-
"""
|
|
76
|
-
Gelişmiş regex tabanlı küçük harfe dönüştürücü ve temizleyici tokenizer.
|
|
77
|
-
"""
|
|
78
|
-
words = re.findall(r'[a-zA-ZçğıöşüÇĞİÖŞÜ0-9_#]+', text.lower())
|
|
79
|
-
return [w for w in words if len(w) > 1]
|
|
80
|
-
|
|
81
|
-
def add_document(self, doc_id: str, doc_type: str, title: str, content: str,
|
|
82
|
-
discrete_code: Optional[int] = None, domain: str = "GENERIC",
|
|
83
|
-
metadata: Optional[Dict[str, Any]] = None) -> SearchDocument:
|
|
84
|
-
tokens = self.tokenize(f"{title} {content} {domain}")
|
|
85
|
-
doc = SearchDocument(
|
|
86
|
-
doc_id=doc_id,
|
|
87
|
-
doc_type=doc_type,
|
|
88
|
-
title=title,
|
|
89
|
-
content=content,
|
|
90
|
-
discrete_code=discrete_code,
|
|
91
|
-
domain=domain.upper(),
|
|
92
|
-
metadata=metadata or {},
|
|
93
|
-
tokens=tokens
|
|
94
|
-
)
|
|
95
|
-
self.documents[doc_id] = doc
|
|
96
|
-
self.doc_lengths[doc_id] = len(tokens)
|
|
97
|
-
|
|
98
|
-
# Ters indekse ekle
|
|
99
|
-
for tok in set(tokens):
|
|
100
|
-
self.inverted_index[tok].add(doc_id)
|
|
101
|
-
|
|
102
|
-
# Codebook indeksine ekle
|
|
103
|
-
if discrete_code is not None:
|
|
104
|
-
self.code_index[discrete_code].add(doc_id)
|
|
105
|
-
|
|
106
|
-
# Domain indeksine ekle
|
|
107
|
-
self.domain_index[doc.domain].add(doc_id)
|
|
108
|
-
|
|
109
|
-
# Ortalama doküman uzunluğunu güncelle
|
|
110
|
-
total_len = sum(self.doc_lengths.values())
|
|
111
|
-
self.avg_doc_len = total_len / max(1, len(self.documents))
|
|
112
|
-
return doc
|
|
113
|
-
|
|
114
|
-
def search(self, query: str, top_k: int = 10, target_domain: Optional[str] = None) -> List[SearchResult]:
|
|
115
|
-
"""
|
|
116
|
-
Hibrit BM25 + Latent Codebook Arama Algoritması.
|
|
117
|
-
"""
|
|
118
|
-
if not self.documents:
|
|
119
|
-
return []
|
|
120
|
-
|
|
121
|
-
start_time = time.time()
|
|
122
|
-
query_tokens = self.tokenize(query)
|
|
123
|
-
|
|
124
|
-
# Query içinde discrete code (#8421 veya 8421) var mı?
|
|
125
|
-
code_matches = re.findall(r'#?(\d{4,5})', query)
|
|
126
|
-
queried_codes = {int(c) for c in code_matches}
|
|
127
|
-
|
|
128
|
-
n_docs = len(self.documents)
|
|
129
|
-
scores: Dict[str, float] = defaultdict(float)
|
|
130
|
-
reasons: Dict[str, List[str]] = defaultdict(list)
|
|
131
|
-
|
|
132
|
-
# 1. BM25 Skorlama
|
|
133
|
-
for tok in query_tokens:
|
|
134
|
-
posting_list = self.inverted_index.get(tok, set())
|
|
135
|
-
if not posting_list:
|
|
136
|
-
continue
|
|
137
|
-
|
|
138
|
-
df = len(posting_list)
|
|
139
|
-
# Standart Lucene/Robertson IDF
|
|
140
|
-
idf = math.log(1.0 + (n_docs - df + 0.5) / (df + 0.5))
|
|
141
|
-
|
|
142
|
-
for doc_id in posting_list:
|
|
143
|
-
doc = self.documents[doc_id]
|
|
144
|
-
tf = doc.tokens.count(tok)
|
|
145
|
-
doc_len = self.doc_lengths.get(doc_id, 1)
|
|
146
|
-
|
|
147
|
-
denom = tf + self.k1 * (1.0 - self.b + self.b * (doc_len / max(1.0, self.avg_doc_len)))
|
|
148
|
-
bm25_part = idf * ((tf * (self.k1 + 1.0)) / max(1e-5, denom))
|
|
149
|
-
scores[doc_id] += bm25_part
|
|
150
|
-
if len(reasons[doc_id]) < 3:
|
|
151
|
-
reasons[doc_id].append(f"token:{tok}")
|
|
152
|
-
|
|
153
|
-
# 2. Discrete Code (#8421) Eşleşme Güçlendirmesi (10x Boost)
|
|
154
|
-
for code in queried_codes:
|
|
155
|
-
for doc_id in self.code_index.get(code, set()):
|
|
156
|
-
scores[doc_id] += 15.0
|
|
157
|
-
reasons[doc_id].append(f"code_exact_match:#{code}")
|
|
158
|
-
|
|
159
|
-
# 3. Domain Hedefleme Güçlendirmesi
|
|
160
|
-
if target_domain:
|
|
161
|
-
target_domain_upper = target_domain.upper()
|
|
162
|
-
for doc_id in self.domain_index.get(target_domain_upper, set()):
|
|
163
|
-
scores[doc_id] += 5.0
|
|
164
|
-
reasons[doc_id].append(f"domain_match:{target_domain_upper}")
|
|
165
|
-
|
|
166
|
-
# Sırala ve formatla
|
|
167
|
-
ranked_doc_ids = sorted(scores.keys(), key=lambda did: scores[did], reverse=True)[:top_k]
|
|
168
|
-
|
|
169
|
-
results = []
|
|
170
|
-
for did in ranked_doc_ids:
|
|
171
|
-
doc = self.documents[did]
|
|
172
|
-
results.append(SearchResult(
|
|
173
|
-
doc_id=doc.doc_id,
|
|
174
|
-
doc_type=doc.doc_type,
|
|
175
|
-
title=doc.title,
|
|
176
|
-
content=doc.content,
|
|
177
|
-
score=scores[did],
|
|
178
|
-
discrete_code=doc.discrete_code,
|
|
179
|
-
domain=doc.domain,
|
|
180
|
-
match_reasons=reasons[did]
|
|
181
|
-
))
|
|
182
|
-
|
|
183
|
-
return results
|
|
184
|
-
|
|
185
|
-
def index_codebase_scanner_results(self, scanner_report: Dict[str, Any]) -> int:
|
|
186
|
-
"""
|
|
187
|
-
T-Zero CodebaseScanner (AST) sonuçlarını anında arama indeksine katar.
|
|
188
|
-
"""
|
|
189
|
-
count = 0
|
|
190
|
-
symbols = scanner_report.get("symbols", {})
|
|
191
|
-
for sym_name, sym_info in symbols.items():
|
|
192
|
-
doc_id = f"sym_{sym_name}_{count}"
|
|
193
|
-
doc_type = "code_symbol"
|
|
194
|
-
title = f"{sym_info.get('type', 'symbol')}: {sym_name}"
|
|
195
|
-
content = f"File: {sym_info.get('file', '')} Lines: {sym_info.get('lines', '')} Doc: {sym_info.get('docstring', '')}"
|
|
196
|
-
self.add_document(
|
|
197
|
-
doc_id=doc_id,
|
|
198
|
-
doc_type=doc_type,
|
|
199
|
-
title=title,
|
|
200
|
-
content=content,
|
|
201
|
-
domain="SOFTWARE",
|
|
202
|
-
metadata=sym_info
|
|
203
|
-
)
|
|
204
|
-
count += 1
|
|
205
|
-
return count
|
|
206
|
-
|
|
207
|
-
def get_stats(self) -> Dict[str, Any]:
|
|
208
|
-
return {
|
|
209
|
-
"total_documents": len(self.documents),
|
|
210
|
-
"total_vocabulary": len(self.inverted_index),
|
|
211
|
-
"total_discrete_codes_indexed": len(self.code_index),
|
|
212
|
-
"domains_covered": list(self.domain_index.keys()),
|
|
213
|
-
"avg_doc_length": round(self.avg_doc_len, 2)
|
|
214
|
-
}
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
"""
|
|
3
|
+
T-ZERO ZERO-VECTOR-DB HYBRID IN-MEMORY SEARCH ENGINE
|
|
4
|
+
Developer: Toprak Ahmet Aydoğmuş (Siber Akademi)
|
|
5
|
+
Version: 4.4.0
|
|
6
|
+
|
|
7
|
+
Bu modül, harici hiçbir ağır Vektör Veritabanına (ChromaDB, Pinecone, FAISS vb.)
|
|
8
|
+
ihtiyaç duymadan; saf bellek içi (In-Memory) Ters İndeksleme (Inverted Index),
|
|
9
|
+
BM25 Anlamsal Sıralama ve T-Zero Latent Codebook (#code) eşleştirmesini
|
|
10
|
+
birleştiren mikro-saniye (<1 ms) hızında hibrit arama motorudur.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
import math
|
|
15
|
+
import re
|
|
16
|
+
import time
|
|
17
|
+
from typing import Dict, List, Optional, Set, Tuple, Any
|
|
18
|
+
from dataclasses import dataclass, field
|
|
19
|
+
from collections import defaultdict
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@dataclass
|
|
23
|
+
class SearchDocument:
|
|
24
|
+
doc_id: str
|
|
25
|
+
doc_type: str # "code_symbol", "triplet", "memory", "causal_event"
|
|
26
|
+
title: str
|
|
27
|
+
content: str
|
|
28
|
+
discrete_code: Optional[int] = None
|
|
29
|
+
domain: str = "GENERIC"
|
|
30
|
+
metadata: Dict[str, Any] = field(default_factory=dict)
|
|
31
|
+
tokens: List[str] = field(default_factory=list)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass
|
|
35
|
+
class SearchResult:
|
|
36
|
+
doc_id: str
|
|
37
|
+
doc_type: str
|
|
38
|
+
title: str
|
|
39
|
+
content: str
|
|
40
|
+
score: float
|
|
41
|
+
discrete_code: Optional[int] = None
|
|
42
|
+
domain: str = "GENERIC"
|
|
43
|
+
match_reasons: List[str] = field(default_factory=list)
|
|
44
|
+
|
|
45
|
+
def to_dict(self) -> Dict[str, Any]:
|
|
46
|
+
return {
|
|
47
|
+
"doc_id": self.doc_id,
|
|
48
|
+
"doc_type": self.doc_type,
|
|
49
|
+
"title": self.title,
|
|
50
|
+
"content": self.content,
|
|
51
|
+
"score": round(self.score, 4),
|
|
52
|
+
"discrete_code": self.discrete_code,
|
|
53
|
+
"domain": self.domain,
|
|
54
|
+
"match_reasons": self.match_reasons
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class ZeroVectorDBSearchEngine:
|
|
59
|
+
"""
|
|
60
|
+
Sıfır Dış Bağımlılıklı, Bellek İçi BM25 + T-Zero Latent Codebook Hibrit Arama Motoru.
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
def __init__(self, k1: float = 1.5, b: float = 0.75):
|
|
64
|
+
self.k1 = k1
|
|
65
|
+
self.b = b
|
|
66
|
+
self.documents: Dict[str, SearchDocument] = {}
|
|
67
|
+
self.inverted_index: Dict[str, Set[str]] = defaultdict(set)
|
|
68
|
+
self.code_index: Dict[int, Set[str]] = defaultdict(set)
|
|
69
|
+
self.domain_index: Dict[str, Set[str]] = defaultdict(set)
|
|
70
|
+
self.doc_lengths: Dict[str, int] = {}
|
|
71
|
+
self.avg_doc_len: float = 0.0
|
|
72
|
+
|
|
73
|
+
@staticmethod
|
|
74
|
+
def tokenize(text: str) -> List[str]:
|
|
75
|
+
"""
|
|
76
|
+
Gelişmiş regex tabanlı küçük harfe dönüştürücü ve temizleyici tokenizer.
|
|
77
|
+
"""
|
|
78
|
+
words = re.findall(r'[a-zA-ZçğıöşüÇĞİÖŞÜ0-9_#]+', text.lower())
|
|
79
|
+
return [w for w in words if len(w) > 1]
|
|
80
|
+
|
|
81
|
+
def add_document(self, doc_id: str, doc_type: str, title: str, content: str,
|
|
82
|
+
discrete_code: Optional[int] = None, domain: str = "GENERIC",
|
|
83
|
+
metadata: Optional[Dict[str, Any]] = None) -> SearchDocument:
|
|
84
|
+
tokens = self.tokenize(f"{title} {content} {domain}")
|
|
85
|
+
doc = SearchDocument(
|
|
86
|
+
doc_id=doc_id,
|
|
87
|
+
doc_type=doc_type,
|
|
88
|
+
title=title,
|
|
89
|
+
content=content,
|
|
90
|
+
discrete_code=discrete_code,
|
|
91
|
+
domain=domain.upper(),
|
|
92
|
+
metadata=metadata or {},
|
|
93
|
+
tokens=tokens
|
|
94
|
+
)
|
|
95
|
+
self.documents[doc_id] = doc
|
|
96
|
+
self.doc_lengths[doc_id] = len(tokens)
|
|
97
|
+
|
|
98
|
+
# Ters indekse ekle
|
|
99
|
+
for tok in set(tokens):
|
|
100
|
+
self.inverted_index[tok].add(doc_id)
|
|
101
|
+
|
|
102
|
+
# Codebook indeksine ekle
|
|
103
|
+
if discrete_code is not None:
|
|
104
|
+
self.code_index[discrete_code].add(doc_id)
|
|
105
|
+
|
|
106
|
+
# Domain indeksine ekle
|
|
107
|
+
self.domain_index[doc.domain].add(doc_id)
|
|
108
|
+
|
|
109
|
+
# Ortalama doküman uzunluğunu güncelle
|
|
110
|
+
total_len = sum(self.doc_lengths.values())
|
|
111
|
+
self.avg_doc_len = total_len / max(1, len(self.documents))
|
|
112
|
+
return doc
|
|
113
|
+
|
|
114
|
+
def search(self, query: str, top_k: int = 10, target_domain: Optional[str] = None) -> List[SearchResult]:
|
|
115
|
+
"""
|
|
116
|
+
Hibrit BM25 + Latent Codebook Arama Algoritması.
|
|
117
|
+
"""
|
|
118
|
+
if not self.documents:
|
|
119
|
+
return []
|
|
120
|
+
|
|
121
|
+
start_time = time.time()
|
|
122
|
+
query_tokens = self.tokenize(query)
|
|
123
|
+
|
|
124
|
+
# Query içinde discrete code (#8421 veya 8421) var mı?
|
|
125
|
+
code_matches = re.findall(r'#?(\d{4,5})', query)
|
|
126
|
+
queried_codes = {int(c) for c in code_matches}
|
|
127
|
+
|
|
128
|
+
n_docs = len(self.documents)
|
|
129
|
+
scores: Dict[str, float] = defaultdict(float)
|
|
130
|
+
reasons: Dict[str, List[str]] = defaultdict(list)
|
|
131
|
+
|
|
132
|
+
# 1. BM25 Skorlama
|
|
133
|
+
for tok in query_tokens:
|
|
134
|
+
posting_list = self.inverted_index.get(tok, set())
|
|
135
|
+
if not posting_list:
|
|
136
|
+
continue
|
|
137
|
+
|
|
138
|
+
df = len(posting_list)
|
|
139
|
+
# Standart Lucene/Robertson IDF
|
|
140
|
+
idf = math.log(1.0 + (n_docs - df + 0.5) / (df + 0.5))
|
|
141
|
+
|
|
142
|
+
for doc_id in posting_list:
|
|
143
|
+
doc = self.documents[doc_id]
|
|
144
|
+
tf = doc.tokens.count(tok)
|
|
145
|
+
doc_len = self.doc_lengths.get(doc_id, 1)
|
|
146
|
+
|
|
147
|
+
denom = tf + self.k1 * (1.0 - self.b + self.b * (doc_len / max(1.0, self.avg_doc_len)))
|
|
148
|
+
bm25_part = idf * ((tf * (self.k1 + 1.0)) / max(1e-5, denom))
|
|
149
|
+
scores[doc_id] += bm25_part
|
|
150
|
+
if len(reasons[doc_id]) < 3:
|
|
151
|
+
reasons[doc_id].append(f"token:{tok}")
|
|
152
|
+
|
|
153
|
+
# 2. Discrete Code (#8421) Eşleşme Güçlendirmesi (10x Boost)
|
|
154
|
+
for code in queried_codes:
|
|
155
|
+
for doc_id in self.code_index.get(code, set()):
|
|
156
|
+
scores[doc_id] += 15.0
|
|
157
|
+
reasons[doc_id].append(f"code_exact_match:#{code}")
|
|
158
|
+
|
|
159
|
+
# 3. Domain Hedefleme Güçlendirmesi
|
|
160
|
+
if target_domain:
|
|
161
|
+
target_domain_upper = target_domain.upper()
|
|
162
|
+
for doc_id in self.domain_index.get(target_domain_upper, set()):
|
|
163
|
+
scores[doc_id] += 5.0
|
|
164
|
+
reasons[doc_id].append(f"domain_match:{target_domain_upper}")
|
|
165
|
+
|
|
166
|
+
# Sırala ve formatla
|
|
167
|
+
ranked_doc_ids = sorted(scores.keys(), key=lambda did: scores[did], reverse=True)[:top_k]
|
|
168
|
+
|
|
169
|
+
results = []
|
|
170
|
+
for did in ranked_doc_ids:
|
|
171
|
+
doc = self.documents[did]
|
|
172
|
+
results.append(SearchResult(
|
|
173
|
+
doc_id=doc.doc_id,
|
|
174
|
+
doc_type=doc.doc_type,
|
|
175
|
+
title=doc.title,
|
|
176
|
+
content=doc.content,
|
|
177
|
+
score=scores[did],
|
|
178
|
+
discrete_code=doc.discrete_code,
|
|
179
|
+
domain=doc.domain,
|
|
180
|
+
match_reasons=reasons[did]
|
|
181
|
+
))
|
|
182
|
+
|
|
183
|
+
return results
|
|
184
|
+
|
|
185
|
+
def index_codebase_scanner_results(self, scanner_report: Dict[str, Any]) -> int:
|
|
186
|
+
"""
|
|
187
|
+
T-Zero CodebaseScanner (AST) sonuçlarını anında arama indeksine katar.
|
|
188
|
+
"""
|
|
189
|
+
count = 0
|
|
190
|
+
symbols = scanner_report.get("symbols", {})
|
|
191
|
+
for sym_name, sym_info in symbols.items():
|
|
192
|
+
doc_id = f"sym_{sym_name}_{count}"
|
|
193
|
+
doc_type = "code_symbol"
|
|
194
|
+
title = f"{sym_info.get('type', 'symbol')}: {sym_name}"
|
|
195
|
+
content = f"File: {sym_info.get('file', '')} Lines: {sym_info.get('lines', '')} Doc: {sym_info.get('docstring', '')}"
|
|
196
|
+
self.add_document(
|
|
197
|
+
doc_id=doc_id,
|
|
198
|
+
doc_type=doc_type,
|
|
199
|
+
title=title,
|
|
200
|
+
content=content,
|
|
201
|
+
domain="SOFTWARE",
|
|
202
|
+
metadata=sym_info
|
|
203
|
+
)
|
|
204
|
+
count += 1
|
|
205
|
+
return count
|
|
206
|
+
|
|
207
|
+
def get_stats(self) -> Dict[str, Any]:
|
|
208
|
+
return {
|
|
209
|
+
"total_documents": len(self.documents),
|
|
210
|
+
"total_vocabulary": len(self.inverted_index),
|
|
211
|
+
"total_discrete_codes_indexed": len(self.code_index),
|
|
212
|
+
"domains_covered": list(self.domain_index.keys()),
|
|
213
|
+
"avg_doc_length": round(self.avg_doc_len, 2)
|
|
214
|
+
}
|