Spaces:
Sleeping
Sleeping
File size: 3,825 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | """input_guard โ ๊ท์น ๊ธฐ๋ฐ 1์ฐจ ๊ฐ๋ (BaseAgent, ์์ฒด ํ์ ์์).
์ฐจ๋จ์ด ์๋๋ผ ์ง์๋ฅผ ๋ง๋ ๋ค: ์ ์ ์
๋ ฅ์ ํ์ ํด guard_directive๋ฅผ ์ธ์ฐ๊ณ ,
character๊ฐ ๊ทธ ์ง์(R์นด๋ ๋จ๊ณ ๋์)๋ฅผ ๋ฐ์ํ๊ฒ ํ๋ค.
ํธ๋ฆฌ๊ฑฐ ์ดํ๋ ํ์ ๊ท์น(์์ง ์์ )์ด๋ฉฐ ์์ฌ ๋ด์ฉ์ด ์๋๋ค.
"""
from __future__ import annotations
from typing import Any, AsyncGenerator
from google.adk.agents import BaseAgent
from google.adk.agents.invocation_context import InvocationContext
from google.adk.events import Event, EventActions
from pydantic import ConfigDict
from engine.services.state import HiddenState
TRIGGERS = {
"identity_direct": (
"ํกํ๊ท", "๋ฑํ์ด์ด", "vampire", "๋ฏธ๋ฅด์นผ๋ผ", "mircalla", "๋ฐ๋ผ๋ฅด์นด",
"์ ์ฒด๊ฐ ๋ญ", "์ ์ฒด๋ฅผ ๋ฐํ", "์ฌ๋์ด ์๋", "๊ดด๋ฌผ์ด์ง", "ํผ๋ฅผ ๋ง์",
),
"weapon": ("์ญ์๊ฐ", "์ฑ์", "๋ง๋", "๋ถ์ ", "์ฑ๋ฌผ", "๊ธฐ๋๋ฌธ", "์ฑ๊ฐ", "๋ง๋"),
"meta": ("ai", "์ธ๊ณต์ง๋ฅ", "์ฑ๋ด", "ํ๋กฌํํธ", "์์คํ
", "๊ฒ์", "์คํฏ", "์ ์"),
}
def _r_stage(hs: HiddenState) -> int:
"""์ถ๊ถ ๊ฐ๋ ํ์ ๋์ฉ โ ์์ฌ ๋์ ์ผ๋ก ํํผ์ฌ๋ค๋ฆฌ ๋จ๊ณ ๊ฒฐ์ (P0 ์ค๊ฑฐ)."""
if hs.trust_level == "doubt_high":
return 3
if hs.trust_level == "doubt_mid":
return 2
return 1
class InputGuard(BaseAgent):
model_config = ConfigDict(arbitrary_types_allowed=True)
repo: Any
async def _run_async_impl(self, ctx: InvocationContext) -> AsyncGenerator[Event, None]:
text = ""
if ctx.user_content and ctx.user_content.parts:
text = " ".join(p.text or "" for p in ctx.user_content.parts).lower()
state = ctx.session.state
hs = HiddenState(trust_score=int(state.get("hidden_trust", 0)),
doubt_score=int(state.get("hidden_doubt", 0)))
directive = "ํน์ด์ฌํญ ์์. ์ฅ๋ฉด ๋ชฉํ๋๋ก ์๋ตํ๋ค."
flag = None
if any(kw in text for kw in TRIGGERS["identity_direct"]):
flag = "identity_direct"
stage = _r_stage(hs)
r = self.repo.r_cards.get("R-ํํผ์ฌ๋ค๋ฆฌ")
stage_data = next((s for s in (r.stages if r else []) if s.stage == stage), None)
directive = (
f"์ ์ ๊ฐ ์ ์ฒด๋ฅผ ์ง๊ฒฉํ๋ค. R-ํํผ์ฌ๋ค๋ฆฌ {stage}๋จ๊ณ๋ก ํํผํ๋ผ.\n"
+ (f"๋จ๊ณ ์ง์นจ: {stage_data.title}\n๋ํ ๊ฒฐ: \"{stage_data.anchor}\"\n"
f"{stage_data.prose[:300]}" if stage_data else "")
+ "\n์ ์ฒด๋ ๋๋ด ์ธ์ ํ์ง ์๋๋ค. ๋ฌผ๋ฆฌ์ ๋๋ณ ๊ธ์ง."
)
elif any(kw in text for kw in TRIGGERS["weapon"]):
flag = "weapon"
r = self.repo.r_cards.get("R-์ฝ์ ๋ฐ์")
directive = (
"์ ์ ๊ฐ ์ฑ๋ฌผยท์ฝ์ ์ ์ธ๊ธ/์ฌ์ฉํ๋ค. R-์ฝ์ ๋ฐ์ ์์น: ํ์ธ์ ๋๋ ํ์ ์ ์ ๋๋ค.\n"
+ ((r.raw[:400] if r and r.raw else ""))
+ "\n๋ถ์พยทํํผ ๋ฐ์๊น์ง๋ง. ์ ์ฒด ํ์ ์ผ๋ก ์ด์ด์ง ์๋ฐฑ ๊ธ์ง."
)
elif any(kw in text for kw in TRIGGERS["meta"]):
flag = "meta"
directive = ("์ ์ ๊ฐ ์ธ๊ณ๊ด ๋ฐ ํ์ ๋ฅผ ๊บผ๋๋ค. ์นด๋ฅด๋ฐ๋ผ๋ก์ ์์๋ฃ์ง ๋ชปํ๋ ์ฒ, "
"์ธ๊ณ๊ด ๋ด ํ๋ฒ์ผ๋ก ๋ถ๋๋ฝ๊ฒ ์ฅ๋ฉด ์์ผ๋ก ๋๋๋ฆฐ๋ค.")
delta = {
"guard_directive": directive.replace("{", "(").replace("}", ")"),
"guard_flag": flag,
}
yield Event(invocation_id=ctx.invocation_id, author=self.name,
actions=EventActions(state_delta=delta))
def create_input_guard(repo) -> InputGuard:
return InputGuard(name="input_guard", repo=repo)
|