forked from Skull-boy/agent-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodes.py
More file actions
156 lines (136 loc) · 5.35 KB
/
Copy pathnodes.py
File metadata and controls
156 lines (136 loc) · 5.35 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
from __future__ import annotations
import logging
from typing import Any
import httpx
from github import Github, GithubException
from openai import OpenAI
from .state import State
log = logging.getLogger(__name__)
_EMBED_MODEL = "text-embedding-3-small"
_EMBED_DIMS = 1536
_MAX_CHARS = 6000
_SEARCH_LIMIT = 5
def detect(state: State) -> dict[str, Any]:
"""Fetch issue title + body from GitHub. Read-only, no side effects."""
log.info("DETECT #%d from %s/%s", state["issue_number"], state["repo_owner"], state["repo_name"])
try:
repo = Github(state["github_token"]).get_repo(f"{state['repo_owner']}/{state['repo_name']}")
issue = repo.get_issue(number=state["issue_number"])
except GithubException as exc:
log.error("DETECT error: %s", exc)
raise
title = issue.title or ""
body = issue.body or ""
combined = f"{title}\n\n{body}".strip()[:_MAX_CHARS]
log.info("DETECT title=%r body_len=%d", title, len(body))
return {
"issue_title": title,
"issue_body": body,
"combined_text": combined,
"run_log": state["run_log"] + [f"detect: #{state['issue_number']} {title!r:.50}"],
}
def judge(state: State) -> dict[str, Any]:
"""Embed text via OpenAI, search Qdrant, return best match score."""
log.info("JUDGE embedding #%d", state["issue_number"])
try:
resp = OpenAI(api_key=state["openai_api_key"]).embeddings.create(
model=_EMBED_MODEL, input=state["combined_text"]
)
except Exception as exc:
log.error("JUDGE embed error: %s", exc)
raise
vector = resp.data[0].embedding
if len(vector) != _EMBED_DIMS:
raise ValueError(f"Expected {_EMBED_DIMS}-dim vector, got {len(vector)}")
log.info("JUDGE embedded, dims=%d", len(vector))
headers = {"Content-Type": "application/json"}
if state["qdrant_api_key"]:
headers["api-key"] = state["qdrant_api_key"]
try:
r = httpx.post(
f"{state['qdrant_url'].rstrip('/')}/collections/{state['qdrant_collection']}/points/search",
json={"vector": vector, "limit": _SEARCH_LIMIT, "with_payload": True},
headers=headers,
timeout=30.0,
)
r.raise_for_status()
except Exception as exc:
log.error("JUDGE search error: %s", exc)
raise
results = [
hit for hit in r.json().get("result", [])
if hit.get("payload", {}).get("issueNumber") != state["issue_number"]
]
best_score = 0.0
match_number = None
match_title = None
if results:
best = results[0]
best_score = float(best.get("score", 0.0))
match_number = best.get("payload", {}).get("issueNumber")
match_title = best.get("payload", {}).get("title")
log.info("JUDGE score=%.4f match=#%s", best_score, match_number)
return {
"vector": vector,
"best_score": best_score,
"match_number": match_number,
"match_title": match_title,
"run_log": state["run_log"] + [f"judge: score={best_score:.4f} match=#{match_number}"],
}
def act(state: State) -> dict[str, Any]:
"""Post one duplicate comment on the triggering issue."""
log.info(
"ACT posting comment on #%d → #%s score=%.4f",
state["issue_number"], state["match_number"], state["best_score"],
)
body = (
f"🔍 This issue looks similar to #{state['match_number']} — "
f"\"{state['match_title']}\" (similarity: {state['best_score']:.3f}).\n\n"
"This is an automated suggestion — a maintainer will confirm."
)
try:
repo = Github(state["github_token"]).get_repo(f"{state['repo_owner']}/{state['repo_name']}")
repo.get_issue(number=state["issue_number"]).create_comment(body)
except GithubException as exc:
log.error("ACT error: %s", exc)
raise
log.info("ACT comment posted")
return {
"comment_posted": True,
"run_log": state["run_log"] + [
f"act: commented on #{state['issue_number']} → #{state['match_number']}"
],
}
def index(state: State) -> dict[str, Any]:
"""Upsert this issue's vector into Qdrant. Always runs regardless of duplicate result."""
log.info("INDEX upserting #%d into %s", state["issue_number"], state["qdrant_collection"])
headers = {"Content-Type": "application/json"}
if state["qdrant_api_key"]:
headers["api-key"] = state["qdrant_api_key"]
try:
r = httpx.put(
f"{state['qdrant_url'].rstrip('/')}/collections/{state['qdrant_collection']}/points",
json={"points": [{
"id": state["issue_number"],
"vector": state["vector"],
"payload": {
"issueNumber": state["issue_number"],
"title": state["issue_title"],
"url": (
f"https://github.com/{state['repo_owner']}"
f"/{state['repo_name']}/issues/{state['issue_number']}"
),
},
}]},
headers=headers,
timeout=30.0,
)
r.raise_for_status()
except Exception as exc:
log.error("INDEX error: %s", exc)
raise
log.info("INDEX done")
return {
"indexed": True,
"run_log": state["run_log"] + [f"index: upserted #{state['issue_number']}"],
}