-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphql_schema.py
More file actions
314 lines (250 loc) · 8.54 KB
/
Copy pathgraphql_schema.py
File metadata and controls
314 lines (250 loc) · 8.54 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import strawberry
from typing import List, Optional
from datetime import datetime
from strawberry.types import Info
@strawberry.type
class Message:
id: str
content: str
role: str
timestamp: str
@strawberry.type
class ConversationResponse:
content: str
intent: str
session_id: str
timestamp: str
tokens: Optional[int] = None
@strawberry.type
class Vulnerability:
id: str
title: str
severity: str
description: str
url: str
found_at: str
@strawberry.type
class ScanSession:
session_id: str
target: str
status: str
vulnerabilities_found: int
started_at: str
completed_at: Optional[str] = None
@strawberry.type
class ExecutionResult:
stdout: str
stderr: str
return_code: int
execution_time: float
language: str
success: bool
@strawberry.type
class Provider:
name: str
status: str
models_count: int
@strawberry.type
class SystemStats:
total_requests: int
active_sessions: int
cache_hit_rate: float
uptime_seconds: int
@strawberry.input
class ChatInput:
message: str
session_id: Optional[str] = "default"
model: Optional[str] = "auto"
@strawberry.input
class CodeExecutionInput:
code: str
language: str
timeout: int = 30
args: Optional[List[str]] = None
@strawberry.input
class ScanInput:
target: str
mode: str = "balanced"
max_depth: int = 3
@strawberry.type
class Query:
@strawberry.field
async def chat_history(
self,
session_id: str = "default",
limit: int = 10
) -> List[Message]:
"""Get chat history for session"""
from src.cognitive.llm.conversation_engine import ConversationEngine
engine = ConversationEngine()
history = await engine.get_session_history(session_id, limit)
return [
Message(
id=f"{session_id}_{i}",
content=msg.get("content", ""),
role=msg.get("role", "user"),
timestamp=msg.get("timestamp", datetime.now().isoformat())
)
for i, msg in enumerate(history)
]
@strawberry.field
async def scan_sessions(self, limit: int = 10) -> List[ScanSession]:
"""Get recent scan sessions"""
from src.autonomous.autonomous_brain import AutonomousBrain
brain = AutonomousBrain()
sessions = await brain.get_recent_sessions(limit)
return [
ScanSession(
session_id=s["session_id"],
target=s["target"],
status=s["status"],
vulnerabilities_found=len(s.get("vulnerabilities", [])),
started_at=s["started_at"],
completed_at=s.get("completed_at")
)
for s in sessions
]
@strawberry.field
async def scan_status(self, session_id: str) -> Optional[ScanSession]:
"""Get scan session status"""
from src.autonomous.autonomous_brain import AutonomousBrain
brain = AutonomousBrain()
session = await brain.get_session(session_id)
if not session:
return None
return ScanSession(
session_id=session["session_id"],
target=session["target"],
status=session["status"],
vulnerabilities_found=len(session.get("vulnerabilities", [])),
started_at=session["started_at"],
completed_at=session.get("completed_at")
)
@strawberry.field
async def providers(self) -> List[Provider]:
"""Get available AI providers"""
from src.cognitive.llm.model_router import ModelRouter
router = ModelRouter()
providers_info = await router.get_providers_status()
return [
Provider(
name=p["name"],
status=p["status"],
models_count=len(p.get("models", []))
)
for p in providers_info
]
@strawberry.field
async def system_stats(self) -> SystemStats:
"""Get system statistics"""
from src.api.middleware import get_request_stats
import time
stats = get_request_stats()
return SystemStats(
total_requests=stats.get("total_requests", 0),
active_sessions=stats.get("active_sessions", 0),
cache_hit_rate=stats.get("cache_hit_rate", 0.0),
uptime_seconds=int(time.time() - stats.get("start_time", time.time()))
)
@strawberry.type
class Mutation:
@strawberry.mutation
async def send_message(self, input: ChatInput) -> ConversationResponse:
"""Send message to AI"""
from src.cognitive.llm.conversation_engine import ConversationEngine
engine = ConversationEngine()
result = await engine.process_conversation(
message=input.message,
session_id=input.session_id,
model=input.model
)
return ConversationResponse(
content=result["content"],
intent=result.get("intent", "unknown"),
session_id=input.session_id,
timestamp=datetime.now().isoformat(),
tokens=result.get("tokens")
)
@strawberry.mutation
async def execute_code(self, input: CodeExecutionInput) -> ExecutionResult:
"""Execute code in multiple languages"""
from src.execution.code_executor import get_executor
executor = get_executor()
result = await executor.execute(
code=input.code,
language=input.language,
timeout=input.timeout,
args=input.args
)
return ExecutionResult(
stdout=result.get("stdout", ""),
stderr=result.get("stderr", ""),
return_code=result.get("return_code", -1),
execution_time=result.get("execution_time", 0.0),
language=input.language,
success=result.get("success", False)
)
@strawberry.mutation
async def start_scan(self, input: ScanInput) -> ScanSession:
"""Start autonomous security scan"""
from src.autonomous.autonomous_brain import AutonomousBrain
brain = AutonomousBrain()
session = await brain.start_autonomous_session(
target_url=input.target,
mode=input.mode
)
return ScanSession(
session_id=session["session_id"],
target=input.target,
status="started",
vulnerabilities_found=0,
started_at=datetime.now().isoformat()
)
@strawberry.mutation
async def stop_scan(self, session_id: str) -> bool:
"""Stop autonomous scan"""
from src.autonomous.autonomous_brain import AutonomousBrain
brain = AutonomousBrain()
return await brain.stop_session(session_id)
@strawberry.mutation
async def clear_cache(self) -> bool:
"""Clear Redis cache"""
from src.cache.redis_cache import get_cache
cache = get_cache()
return cache.clear_all()
@strawberry.type
class Subscription:
@strawberry.subscription
async def scan_progress(self, session_id: str) -> ScanSession:
"""Subscribe to scan progress updates"""
import asyncio
from src.autonomous.autonomous_brain import AutonomousBrain
brain = AutonomousBrain()
while True:
session = await brain.get_session(session_id)
if not session:
break
yield ScanSession(
session_id=session["session_id"],
target=session["target"],
status=session["status"],
vulnerabilities_found=len(session.get("vulnerabilities", [])),
started_at=session["started_at"],
completed_at=session.get("completed_at")
)
if session["status"] in ["completed", "failed"]:
break
await asyncio.sleep(2)
@strawberry.subscription
async def llm_stream(self, prompt: str, model: str = "auto"):
"""Stream LLM responses"""
from src.cognitive.llm.model_router import ModelRouter
router = ModelRouter()
async for chunk in router.stream_generate(prompt, model):
yield chunk
# Create GraphQL schema
schema = strawberry.Schema(
query=Query,
mutation=Mutation,
subscription=Subscription
)