File size: 2,908 Bytes
f646e0b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""ํ„ด ๋กœ๊น… Plugin โ€” ํšก์ข…๋‹จ ๊ด€์‹ฌ์‚ฌ (ADK Plugin).

์ด๋ฒคํŠธ ์ŠคํŠธ๋ฆผ์—์„œ ์œ ์ € ์ž…๋ ฅ/๊ฐ€๋“œ ํŒ์ •/์บ๋ฆญํ„ฐ ์‘๋‹ต/๋””๋ ‰ํ„ฐ ๋„๊ตฌ ํ˜ธ์ถœ์„
ํฌ์ผ“ ์„ธ์…˜ ๋‹จ์œ„๋กœ ๊ธฐ๋กํ•œ๋‹ค. ์—ฐ๊ตฌ ๋ฐ์ดํ„ฐ ๊ฒธ์šฉ โ€” ์‚ญ์ œ ๊ธˆ์ง€.
"""

from __future__ import annotations

from typing import Optional

from google.adk.agents.invocation_context import InvocationContext
from google.adk.events import Event
from google.adk.plugins import BasePlugin

from engine.repositories.playlog import PlaylogRepository


class TurnLoggingPlugin(BasePlugin):
    def __init__(self, playlog: PlaylogRepository | None = None):
        super().__init__(name="turn_logging")
        self.playlog = playlog or PlaylogRepository()

    async def on_user_message_callback(self, *, invocation_context: InvocationContext,
                                       user_message) -> None:
        text = ""
        if user_message and user_message.parts:
            text = " ".join(p.text or "" for p in user_message.parts)
        self.playlog.append({
            "kind": "user_input",
            "session": invocation_context.session.id,
            "card": invocation_context.session.state.get("pocket_card_id"),
            "text": text,
        })
        return None

    async def on_event_callback(self, *, invocation_context: InvocationContext,
                                event: Event) -> Optional[Event]:
        state = invocation_context.session.state
        record = {
            "kind": "event",
            "session": invocation_context.session.id,
            "card": state.get("pocket_card_id"),
            "author": event.author,
        }
        if event.content and event.content.parts:
            texts = [p.text for p in event.content.parts if p.text]
            calls = [{"tool": p.function_call.name, "args": dict(p.function_call.args or {})}
                     for p in event.content.parts if p.function_call]
            if texts:
                record["text"] = "".join(texts)[:500]
            if calls:
                record["tool_calls"] = calls
        if event.actions and event.actions.state_delta:
            delta = event.actions.state_delta
            record["state_delta_keys"] = sorted(delta.keys())
            if "guard_flag" in delta:
                record["guard_flag"] = delta["guard_flag"]
        self.playlog.append(record)
        return None

    async def after_run_callback(self, *, invocation_context: InvocationContext) -> None:
        s = invocation_context.session.state
        self.playlog.append({
            "kind": "turn_summary",
            "session": invocation_context.session.id,
            "card": s.get("pocket_card_id"),
            "hidden_trust": s.get("hidden_trust"),
            "hidden_doubt": s.get("hidden_doubt"),
            "beats_hit": s.get("pocket_beats_hit"),
            "converged": s.get("pocket_converged"),
        })