Skip to content

Commit 057adfa

Browse files
authored
Merge pull request #11 from DoraFactory/fix/publish
chore: clean up code structure and remove unused imports in publish
2 parents 644d60f + 50f1922 commit 057adfa

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

src/poll_agent/main.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from __future__ import annotations
22

33
import asyncio
4+
import json
45
import logging
56
import sys
67
import time
78
import traceback
89
import uuid
10+
from typing import Callable, Optional
911

1012
try:
1113
from google.adk.models import google_llm as _google_llm
@@ -25,6 +27,18 @@ def _truncate_for_log(text: str, max_len: int = 900) -> str:
2527
return f"{text[:max_len]}…<truncated len={len(text)}>"
2628

2729

30+
def _find_publish_all_impl(runner) -> Optional[Callable[[object], dict]]:
31+
agent = getattr(runner, "agent", None)
32+
sub_agents = getattr(agent, "sub_agents", None)
33+
if not isinstance(sub_agents, list):
34+
return None
35+
for sub_agent in sub_agents:
36+
impl = getattr(sub_agent, "_publish_all_impl", None)
37+
if callable(impl):
38+
return impl
39+
return None
40+
41+
2842
def main() -> int:
2943
logging.basicConfig(
3044
level=logging.INFO,
@@ -208,6 +222,30 @@ def _thread_main() -> None:
208222
else:
209223
logging.warning("[agent=poll_orchestrator] no final response produced.")
210224

225+
publish_called = any("tool_call: publish_all" in call for call in tool_calls)
226+
publish_result_like = bool(final_text) and all(
227+
marker in final_text for marker in ('"targets_count"', '"published_count"', '"x_posted_count"')
228+
)
229+
if final_text and not publish_called and not publish_result_like:
230+
publish_impl = _find_publish_all_impl(runner)
231+
if publish_impl:
232+
logging.warning(
233+
"[main] publish_all was not called by model; triggering deterministic fallback publish."
234+
)
235+
try:
236+
publish_result = publish_impl(final_text)
237+
final_text = json.dumps(publish_result, ensure_ascii=False)
238+
tool_calls.append("fallback_call: publish_all")
239+
logging.info(
240+
"[main] fallback publish result: %s",
241+
_truncate_for_log(final_text),
242+
)
243+
except Exception as publish_exc:
244+
logging.error("[main] fallback publish failed: %s", publish_exc)
245+
traceback.print_exc()
246+
else:
247+
logging.error("[main] publish_all fallback unavailable (implementation not found).")
248+
211249
iteration_ok = bool(final_text)
212250
log_metric(
213251
"poll_agent.run_end",

src/poll_agent/sub_agents/publish_agent.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1074,11 +1074,13 @@ def html_escape(text):
10741074
model=f"xai/{settings.agent_model}",
10751075
)
10761076

1077-
return Agent(
1077+
agent = Agent(
10781078
name="publish_agent",
10791079
model=grok_llm,
10801080
include_contents='none',
10811081
instruction=instruction_text,
10821082
description="Publishes poll results to configured platforms (World MACI API + Twitter/X + Telegram).",
10831083
tools=[publish_all],
10841084
)
1085+
setattr(agent, "_publish_all_impl", publish_all)
1086+
return agent

0 commit comments

Comments
 (0)