forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai_agent_api.py
More file actions
276 lines (239 loc) · 8.63 KB
/
Copy pathai_agent_api.py
File metadata and controls
276 lines (239 loc) · 8.63 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
#!/usr/bin/env python3
"""MisakaNet AI Agent API — Simple REST API for AI agents.
Provides optimized endpoints for AI agents to search and access failure lessons.
Usage:
# Start API server
python3 scripts/ai_agent_api.py
# Custom port
python3 scripts/ai_agent_api.py --port 9090
# Endpoints:
# GET /api/search?q=<query>&format=json|markdown
# GET /api/summary
# GET /api/content-signals
# GET /api/schema
"""
from __future__ import annotations
import argparse
import json
import sys
from pathlib import Path
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import urlparse, parse_qs
REPO_ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(REPO_ROOT))
# Import search engine
try:
from misakanet.search.engine import MisakaNetSearchEngine
HAS_BM25 = True
except ImportError:
HAS_BM25 = False
try:
from scripts.build_sag_index import search as sag_search
SAG_DB = REPO_ROOT / "data" / "sag.db"
HAS_SAG = SAG_DB.exists()
except ImportError:
HAS_SAG = False
# Import lessons
LESSONS_DIR = REPO_ROOT / "lessons"
class AIAgentHandler(BaseHTTPRequestHandler):
"""HTTP handler for AI Agent API."""
def do_GET(self):
"""Handle GET requests."""
parsed = urlparse(self.path)
path = parsed.path
params = parse_qs(parsed.query)
# Route to appropriate handler
if path == "/api/search":
self.handle_search(params)
elif path == "/api/summary":
self.handle_summary(params)
elif path == "/api/content-signals":
self.handle_content_signals(params)
elif path == "/api/schema":
self.handle_schema(params)
elif path == "/api/robots-txt":
self.handle_robots_txt(params)
else:
self.send_error(404, "Endpoint not found")
def handle_search(self, params):
"""Search failure lessons."""
query = params.get("q", [""])[0]
fmt = params.get("format", ["json"])[0]
top = int(params.get("top", ["5"])[0])
if not query:
self.send_json(400, {"error": "q parameter is required"})
return
# Search using available engine
results = []
if HAS_SAG:
results = sag_search(SAG_DB, query, top=top)
elif HAS_BM25:
engine = MisakaNetSearchEngine()
results = engine.search(query, top=top)
# Format response
if fmt == "markdown":
response = self.format_markdown(results, query)
self.send_response(200)
self.send_header("Content-Type", "text/markdown; charset=utf-8")
self.end_headers()
self.wfile.write(response.encode())
else:
response = {
"query": query,
"results": results,
"metadata": {
"totalResults": len(results),
"format": "json",
"source": "misakanet"
}
}
self.send_json(200, response)
def handle_summary(self, params):
"""Get summary of available lessons."""
# Count lessons by domain
domains = {}
total_lessons = 0
if LESSONS_DIR.exists():
for lesson_file in LESSONS_DIR.glob("**/*.md"):
total_lessons += 1
# Extract domain from path
rel_path = lesson_file.relative_to(LESSONS_DIR)
domain = rel_path.parts[0] if len(rel_path.parts) > 1 else "general"
domains[domain] = domains.get(domain, 0) + 1
response = {
"summary": {
"totalLessons": total_lessons,
"domains": domains,
"version": "1.0.0",
"timestamp": "2026-08-24T00:00:00Z"
},
"links": {
"search": "/api/search?q={query}",
"contentSignals": "/api/content-signals",
"schema": "/api/schema"
}
}
self.send_json(200, response)
def handle_content_signals(self, params):
"""Return content signals policy."""
response = {
"version": "1.0",
"policy": "allow",
"supportedUseCases": ["training", "search", "agent"],
"attributionRequired": False,
"allowedPaths": [
"/public/",
"/open/",
"/docs/",
"/api/search",
"/api/summary"
],
"disallowedPaths": [
"/api/private/",
"/private/",
"/member-only/"
]
}
self.send_json(200, response)
def handle_schema(self, params):
"""Return JSON-LD schema."""
schema = {
"@context": "https://schema.org",
"@graph": [
{
"@type": "WebSite",
"name": "MisakaNet",
"url": "https://misakanet.org",
"description": "Git-backed failure-memory for AI coding agents",
"potentialAction": {
"@type": "SearchAction",
"target": {
"@type": "EntryPoint",
"urlTemplate": "https://misakanet.org/api/search?q={search_term_string}"
},
"query-input": "required name=search_term_string"
}
},
{
"@type": "Dataset",
"name": "Failure Lessons Knowledge Base",
"description": "Structured dataset of failure-recovery lessons for AI agents",
"distribution": {
"@type": "DataDownload",
"contentUrl": "https://misakanet.org/api/summary",
"encodingFormat": "application/json"
},
"license": "https://misakanet.org/license"
}
]
}
self.send_json(200, schema)
def handle_robots_txt(self, params):
"""Return robots.txt content."""
robots_txt = """User-agent: *
Allow: /public/
Allow: /open/
Allow: /docs/
Allow: /api/search
Allow: /api/summary
Disallow: /api/private/
Disallow: /private/
Crawl-delay: 1
User-agent: GPTBot
Allow: /open/
Allow: /docs/
Allow: /api/
User-agent: ClaudeBot
Allow: /open/
Allow: /docs/
Allow: /api/
Sitemap: https://misakanet.org/sitemap.xml
"""
self.send_response(200)
self.send_header("Content-Type", "text/plain; charset=utf-8")
self.end_headers()
self.wfile.write(robots_txt.encode())
def format_markdown(self, results, query):
"""Format results as markdown."""
if not results:
return f"# Search Results for: {query}\n\nNo results found.\n"
lines = [f"# Search Results for: {query}\n"]
for i, result in enumerate(results, 1):
title = result.get("title", "Untitled")
score = result.get("score", 0)
snippet = result.get("snippet", result.get("content", "")[:200])
lines.append(f"## {i}. {title} (score: {score:.2f})\n")
lines.append(f"{snippet}\n")
return "\n".join(lines)
def send_json(self, code, data):
"""Send JSON response."""
self.send_response(code)
self.send_header("Content-Type", "application/json; charset=utf-8")
self.send_header("X-Content-Signals-Policy", "allow")
self.send_header("X-AI-Agent-Support", "true")
self.end_headers()
self.wfile.write(json.dumps(data, ensure_ascii=False, indent=2).encode())
def log_message(self, format, *args):
"""Suppress default logging."""
pass
def main():
"""Start the AI Agent API server."""
parser = argparse.ArgumentParser(description="MisakaNet AI Agent API")
parser.add_argument("--port", type=int, default=8080, help="Port to listen on")
parser.add_argument("--host", default="0.0.0.0", help="Host to bind to")
args = parser.parse_args()
server = HTTPServer((args.host, args.port), AIAgentHandler)
print(f"🤖 MisakaNet AI Agent API running on http://{args.host}:{args.port}")
print(f" Endpoints:")
print(f" - GET /api/search?q=<query>&format=json|markdown")
print(f" - GET /api/summary")
print(f" - GET /api/content-signals")
print(f" - GET /api/schema")
print(f" - GET /api/robots-txt")
try:
server.serve_forever()
except KeyboardInterrupt:
print("\n🛑 Shutting down...")
server.shutdown()
if __name__ == "__main__":
main()