aimb-x/agentgit
repositories / aimb-x / agentgit

agentgit public

git clone https://git.169.58.159.131.nip.io/aimb-x/agentgit.git
agentgit/cli.py 4888 bytes 1 lines
1
"""agentgit — operations CLI: users, agents, projects, indexing, search, server."""
2
from __future__ import annotations
3
4
import argparse
5
import json
6
import os
7
import sys
8
from pathlib import Path
9
10
from . import indexer, mcp
11
from .db import Store
12
13
DATA = Path(os.environ.get("AGENTGIT_DATA", "/var/lib/agentgit")).resolve()
14
DB = Path(os.environ.get("AGENTGIT_DB", str(DATA / "agentgit.sqlite3")))
15
16
17
def store() -> Store:
18
DATA.mkdir(parents=True, exist_ok=True)
19
return Store(DB)
20
21
22
def out(obj) -> None:
23
print(json.dumps(obj, ensure_ascii=False, indent=1, default=str))
24
25
26
def main(argv=None) -> int:
27
ap = argparse.ArgumentParser(prog="agentgit")
28
sub = ap.add_subparsers(dest="cmd", required=True)
29
30
sub.add_parser("init", help="create the database")
31
32
u = sub.add_parser("user", help="create a user (owner)")
33
u.add_argument("name")
34
u.add_argument("--email", default="")
35
36
a = sub.add_parser("agent", help="register an agent for a user")
37
a.add_argument("user")
38
a.add_argument("name")
39
a.add_argument("--description", default="")
40
a.add_argument("--model", default="")
41
42
p = sub.add_parser("project", help="add a project")
43
p.add_argument("user")
44
p.add_argument("name")
45
p.add_argument("--url", default="")
46
p.add_argument("--path", default="")
47
p.add_argument("--ref", default="")
48
p.add_argument("--visibility", default="owner", choices=["owner", "public"])
49
50
i = sub.add_parser("index", help="index a project now (synchronous)")
51
i.add_argument("project", help="id or name")
52
53
s = sub.add_parser("search", help="search the knowledge base")
54
s.add_argument("query")
55
s.add_argument("--project", default="")
56
s.add_argument("--limit", type=int, default=8)
57
58
sub.add_parser("stats", help="stats")
59
sub.add_parser("projects", help="list projects")
60
61
v = sub.add_parser("serve", help="run the MCP server")
62
v.add_argument("--host", default="127.0.0.1")
63
v.add_argument("--port", type=int, default=8188)
64
65
args = ap.parse_args(argv)
66
st = store()
67
68
if args.cmd == "init":
69
out({"db": str(DB), "ok": True})
70
elif args.cmd == "user":
71
uid, token = st.create_user(args.name, args.email)
72
out({"user_id": uid, "name": args.name, "token": token,
73
"note": "the token is shown once"})
74
elif args.cmd == "agent":
75
row = st.user_by_name(args.user)
76
if not row:
77
sys.exit(f"user {args.user!r} does not exist")
78
aid, token = st.create_agent(row["id"], args.name, args.description, args.model)
79
out({"agent_id": aid, "name": args.name, "user": args.user, "token": token})
80
elif args.cmd == "project":
81
row = st.user_by_name(args.user)
82
if not row:
83
sys.exit(f"user {args.user!r} does not exist")
84
path = str(Path(args.path).resolve()) if args.path else ""
85
pid = st.add_project(row["id"], args.name, args.url, args.ref, path, args.visibility)
86
st.enqueue_job(pid)
87
out({"project_id": pid, "name": args.name, "status": "queued"})
88
elif args.cmd == "index":
89
proj = st.project(int(args.project)) if args.project.isdigit() else st.project_by_name(
90
args.project, only_public=False)
91
if not proj:
92
sys.exit("project does not exist")
93
job = st.enqueue_job(proj["id"])
94
st.mark_project(proj["id"], "running")
95
try:
96
res = mcp.index_project(st, proj, DATA)
97
st.finish_job(job, True, json.dumps(res))
98
proj = st.project(proj["id"])
99
out({"ok": True, **res, "status": proj["status"]})
100
except Exception as e:
101
st.mark_project(proj["id"], "failed", str(e)[:400])
102
st.finish_job(job, False, str(e)[:400])
103
out({"ok": False, "error": str(e)})
104
return 1
105
elif args.cmd == "search":
106
if args.project:
107
proj = st.project(int(args.project)) if args.project.isdigit() else st.project_by_name(
108
args.project, only_public=False)
109
ids = [proj["id"]] if proj else []
110
else:
111
ids = st.project_ids()
112
hits = st.search(args.query, ids, args.limit)
113
out([
114
{"project": h["project_id"], "path": h["path"], "lines": f'{h["start_line"]}-{h["end_line"]}',
115
"score": round(float(h["score"]), 3), "snippet": h["snip"]}
116
for h in hits
117
])
118
elif args.cmd == "stats":
119
out(st.stats())
120
elif args.cmd == "projects":
121
out([{k: p.get(k) for k in ("id", "name", "url", "path", "visibility", "status",
122
"files", "chunks", "commit_sha", "indexed_at", "error")}
123
for p in st.projects()])
124
elif args.cmd == "serve":
125
from .httpd import serve
126
127
serve(args.host, args.port)
128
return 0
129
130
131
if __name__ == "__main__":
132
raise SystemExit(main())