|
1
|
#!/usr/bin/env python3 |
|
2
|
""" |
|
3
|
Navegador hook for OpenAI Codex / ChatGPT with function calling. |
|
4
|
|
|
5
|
OpenAI agents can call navegador directly as a function tool. Register the |
|
6
|
functions below in your assistant's tool list, then call this script to |
|
7
|
dispatch them. |
|
8
|
|
|
9
|
Install: |
|
10
|
Use the function schemas in openai-tools.json alongside this dispatcher. |
|
11
|
Your agent calls: python3 navegador-openai.py <json_args> |
|
12
|
|
|
13
|
Example tool call your agent would make: |
|
14
|
{"name": "nav_search", "arguments": {"query": "authentication", "all": true}} |
|
15
|
|
|
16
|
This script reads JSON from stdin or argv[1] and dispatches to navegador CLI. |
|
17
|
""" |
|
18
|
|
|
19
|
import json |
|
20
|
import os |
|
21
|
import subprocess |
|
22
|
import sys |
|
23
|
|
|
24
|
NAV_DB = os.environ.get("NAVEGADOR_DB", ".navegador/graph.db") |
|
25
|
NAV_CMD = os.environ.get("NAVEGADOR_CMD", "navegador") |
|
26
|
|
|
27
|
DISPATCH = { |
|
28
|
"nav_ingest": lambda a: [NAV_CMD, "ingest", a["path"], "--json"], |
|
29
|
"nav_context": lambda a: [NAV_CMD, "context", a["file_path"], "--format", "json"], |
|
30
|
"nav_function": lambda a: [NAV_CMD, "function", a["name"], |
|
31
|
"--file", a.get("file_path", ""), "--format", "json"], |
|
32
|
"nav_class": lambda a: [NAV_CMD, "class", a["name"], |
|
33
|
"--file", a.get("file_path", ""), "--format", "json"], |
|
34
|
"nav_explain": lambda a: [NAV_CMD, "explain", a["name"], "--format", "json"], |
|
35
|
"nav_search": lambda a: [NAV_CMD, "search", a["query"], |
|
36
|
"--format", "json", |
|
37
|
*( ["--all"] if a.get("all") else [] ), |
|
38
|
*( ["--docs"] if a.get("by_docstring") else [] )], |
|
39
|
"nav_concept": lambda a: [NAV_CMD, "concept", a["name"], "--format", "json"], |
|
40
|
"nav_domain": lambda a: [NAV_CMD, "domain", a["name"], "--format", "json"], |
|
41
|
"nav_stats": lambda a: [NAV_CMD, "stats", "--json"], |
|
42
|
"nav_query": lambda a: [NAV_CMD, "query", a["cypher"]], |
|
43
|
"nav_decorated": lambda a: [NAV_CMD, "decorated", a["decorator"], "--format", "json"], |
|
44
|
} |
|
45
|
|
|
46
|
|
|
47
|
def main(): |
|
48
|
raw = sys.argv[1] if len(sys.argv) > 1 else sys.stdin.read() |
|
49
|
try: |
|
50
|
call = json.loads(raw) |
|
51
|
except json.JSONDecodeError as e: |
|
52
|
print(json.dumps({"error": str(e)})) |
|
53
|
sys.exit(1) |
|
54
|
|
|
55
|
name = call.get("name") or call.get("function", {}).get("name", "") |
|
56
|
arguments = call.get("arguments") or call.get("function", {}).get("arguments", {}) |
|
57
|
if isinstance(arguments, str): |
|
58
|
arguments = json.loads(arguments) |
|
59
|
|
|
60
|
if name not in DISPATCH: |
|
61
|
print(json.dumps({"error": f"Unknown tool: {name}"})) |
|
62
|
sys.exit(1) |
|
63
|
|
|
64
|
cmd = DISPATCH[name](arguments) |
|
65
|
result = subprocess.run( |
|
66
|
["--db", NAV_DB] and ([cmd[0]] + ["--db", NAV_DB] + cmd[1:]), |
|
67
|
capture_output=True, text=True, |
|
68
|
) |
|
69
|
sys.stdout.write(result.stdout or result.stderr) |
|
70
|
|
|
71
|
|
|
72
|
if __name__ == "__main__": |
|
73
|
main() |
|
74
|
|