[feat] solution/backend,frontend: 에이전트 도구·런타임·빌더 채팅창 — 2단계
런타임이 채널을 모르므로 채널·챗봇 심사 없이 에이전트 전체를 빌더 화면에서 검증할 수 있다. 웹훅 핸들러 안에 짜면 빌더에서 같은 걸 못 쓰고, 심사가 끝나야 무엇 하나 확인되지 않는다 — 카톡은 나중에 붙는 두 번째 입구다. - services/agent/tools.py: 도구 넷 + 등급 셋(READ·REVERSIBLE·SEMI). ★ 도구는 반드시 services/* 를 통과한다 — crud 를 직접 부르면 스키마 검증· 출처 필수·정정본 보호가 아무 증상 없이 사라진다. 테스트가 소스로 검사한다 - services/agent/runtime.py: 발화 → 도구 선택(LLM 1콜) → 실행 → 응답 - services/prompts/agent.py: LLM 네 겹 규약대로 프롬프트만 여기 - router/v1/agent/chat.py + features/agent/AgentChatDock.tsx(/sites 우하단) 모델에게 맡기지 않은 셋: - 등급 — 응답 스키마에 칸 자체가 없다. 모델이 정하면 프롬프트에 끼어든 한 줄이 확인 절차를 건너뛴다 - 결과 문구 — 도구가 만든다. 모델이 쓰면 하지 않은 일을 했다고 말할 수 있고 사장님에게는 그 말이 사실로 보인다 - key — set_fact 의 key 는 업종 스키마가 최종 판정이다 확인(SEMI)은 실행하지 않고 되묻는다. 돌아온 confirm 값을 믿지 않고 도구는 레지스트리에서 다시 찾고 인자는 도구가 다시 검증한다 — 확인 절차가 검증을 건너뛰는 구멍이 되면 안 된다. 값을 고치면 재발행 안내를 함께 낸다 — fact 는 바뀌어도 사이트는 안 바뀐다. test_agent_runtime.py 17 passed(LLM 은 monkeypatch, 실제 모델 호출 없음). 전체 796 passed / 50 failed — 그 50건은 HEAD 에서도 동일한 기존 이슈. npm run lint 통과 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
16b17bc91c
commit
b1a34ba58d
12 files changed
+1009
-7
No files matched your search
@@ -0,0 +1,198 @@
|
||||
import {useEffect, useRef, useState} from 'react';
|
||||
import {Loader2, MessageSquare, Send, X} from 'lucide-react';
|
||||
import {Button} from '@/components/ui/button';
|
||||
import {agentApi} from './api';
|
||||
|
||||
/**
|
||||
* 사장님 에이전트 대화창 — **빌더 화면의 입구**.
|
||||
*
|
||||
* ★ 카카오톡보다 이걸 먼저 만든다. 런타임이 채널을 모르므로(services/agent/runtime.py),
|
||||
* 채널·챗봇 심사 없이 여기서 에이전트 전체를 검증할 수 있다. 카톡은 나중에 붙는
|
||||
* 두 번째 입구다(docs/AGENT.md).
|
||||
*
|
||||
* ★ 가게를 먼저 고르게 한다. 사업장이 여럿인 사장님에게 "어느 가게 이야기인지" 를
|
||||
* 화면이 말하지 않으면, 엉뚱한 가게를 고쳐 놓고도 그 사실을 모른다.
|
||||
*
|
||||
* ★ 확인이 필요한 답(needs_confirm)은 **버튼으로만** 진행한다. 서버가 도구와 인자를
|
||||
* 다시 검증하므로 여기서 값을 만들지 않고 받은 것을 그대로 돌려보낸다.
|
||||
*/
|
||||
type Reply = {
|
||||
reply: string;
|
||||
tool: string | null;
|
||||
args?: Record<string, unknown>;
|
||||
needs_confirm: boolean;
|
||||
rejected?: boolean;
|
||||
};
|
||||
|
||||
type Turn = {who: 'owner' | 'agent'; text: string; pending?: {tool: string; args: Record<string, unknown>}};
|
||||
|
||||
type Site = {place_id: string; name: string};
|
||||
|
||||
export function AgentChatDock({sites}: {sites: Site[]}) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [enabled, setEnabled] = useState<boolean | null>(null);
|
||||
const [placeId, setPlaceId] = useState('');
|
||||
const [turns, setTurns] = useState<Turn[]>([]);
|
||||
const [draft, setDraft] = useState('');
|
||||
const [busy, setBusy] = useState(false);
|
||||
const endRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
useEffect(() => {
|
||||
void agentApi<{enabled: boolean}>('/status')
|
||||
.then((s) => setEnabled(s.enabled))
|
||||
.catch(() => setEnabled(false));
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (!placeId && sites.length > 0) setPlaceId(sites[0].place_id);
|
||||
}, [sites, placeId]);
|
||||
|
||||
useEffect(() => {
|
||||
endRef.current?.scrollIntoView({behavior: 'smooth'});
|
||||
}, [turns, open]);
|
||||
|
||||
// 가게가 없으면 대화할 대상이 없다. 상태를 못 읽었을 때도 접는다.
|
||||
if (enabled === null || sites.length === 0) return null;
|
||||
|
||||
async function send(body: {message?: string; confirm?: {tool: string; args: Record<string, unknown>}}, echo: string) {
|
||||
setTurns((t) => [...t, {who: 'owner', text: echo}]);
|
||||
setDraft('');
|
||||
setBusy(true);
|
||||
try {
|
||||
const res = await agentApi<Reply>(`/chat/${placeId}`, body);
|
||||
setTurns((t) => [
|
||||
...t,
|
||||
{
|
||||
who: 'agent',
|
||||
text: res.reply,
|
||||
pending: res.needs_confirm && res.tool ? {tool: res.tool, args: res.args ?? {}} : undefined,
|
||||
},
|
||||
]);
|
||||
} catch (e) {
|
||||
setTurns((t) => [...t, {who: 'agent', text: e instanceof Error ? e.message : '요청을 처리하지 못했습니다.'}]);
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
const ask = () => {
|
||||
const message = draft.trim();
|
||||
if (message) void send({message}, message);
|
||||
};
|
||||
|
||||
if (!open) {
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setOpen(true)}
|
||||
className="fixed bottom-5 right-5 z-40 flex items-center gap-2 rounded-full border border-border bg-card px-4 py-3 text-sm font-bold shadow-lg transition-colors hover:bg-muted"
|
||||
>
|
||||
<MessageSquare className="size-4" />
|
||||
말로 고치기
|
||||
</button>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<section className="fixed bottom-5 right-5 z-40 flex h-[32rem] w-[min(24rem,calc(100vw-2.5rem))] flex-col rounded-xl border border-border bg-card shadow-xl">
|
||||
<header className="flex shrink-0 items-center justify-between gap-2 border-b border-border px-3 py-2">
|
||||
<div className="flex min-w-0 items-center gap-2">
|
||||
<MessageSquare className="size-4 shrink-0" />
|
||||
<h2 className="shrink-0 text-sm font-bold">말로 고치기</h2>
|
||||
{/* 어느 가게 이야기인지 화면이 말한다 — 여럿일 때 이게 없으면 엉뚱한 가게를 고친다. */}
|
||||
<select
|
||||
id="agent-place"
|
||||
value={placeId}
|
||||
onChange={(e) => {
|
||||
setPlaceId(e.target.value);
|
||||
setTurns([]);
|
||||
}}
|
||||
className="min-w-0 flex-1 truncate rounded-md border border-border bg-background px-1.5 py-1 text-xs"
|
||||
>
|
||||
{sites.map((s) => (
|
||||
<option key={s.place_id} value={s.place_id}>
|
||||
{s.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
<button type="button" onClick={() => setOpen(false)} aria-label="닫기" className="shrink-0 rounded-md p-1 hover:bg-muted">
|
||||
<X className="size-4" />
|
||||
</button>
|
||||
</header>
|
||||
|
||||
<div className="flex min-h-0 flex-1 flex-col gap-2 overflow-y-auto px-3 py-3">
|
||||
{turns.length === 0 && (
|
||||
<div className="text-xs text-muted-foreground">
|
||||
<p className="font-bold">이렇게 말해 보세요</p>
|
||||
<ul className="mt-1.5 flex flex-col gap-1">
|
||||
<li>· 체크인 시간 3시로 바꿔줘</li>
|
||||
<li>· 지금 저장된 정보 보여줘</li>
|
||||
<li>· 내 사이트 발행됐어?</li>
|
||||
</ul>
|
||||
{!enabled && (
|
||||
<p className="mt-3 text-destructive">
|
||||
대화 기능이 아직 켜져 있지 않습니다. 관리자에게 문의해 주세요.
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{turns.map((turn, i) => (
|
||||
<div
|
||||
key={i}
|
||||
className={
|
||||
turn.who === 'owner'
|
||||
? 'self-end rounded-lg bg-primary px-3 py-2 text-xs text-primary-foreground'
|
||||
: 'self-start rounded-lg bg-muted px-3 py-2 text-xs'
|
||||
}
|
||||
>
|
||||
<p className="whitespace-pre-wrap break-words">{turn.text}</p>
|
||||
{turn.pending && (
|
||||
<div className="mt-2 flex gap-2">
|
||||
<Button
|
||||
size="sm"
|
||||
variant="primary"
|
||||
disabled={busy}
|
||||
onClick={() => void send({confirm: turn.pending}, '네, 해주세요')}
|
||||
>
|
||||
네, 해주세요
|
||||
</Button>
|
||||
<Button
|
||||
size="sm"
|
||||
variant="ghost"
|
||||
disabled={busy}
|
||||
onClick={() => setTurns((t) => [...t, {who: 'agent', text: '알겠습니다. 그대로 두겠습니다.'}])}
|
||||
>
|
||||
아니요
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
{busy && <Loader2 className="size-4 shrink-0 animate-spin self-start text-muted-foreground" />}
|
||||
<div ref={endRef} />
|
||||
</div>
|
||||
|
||||
<div className="flex shrink-0 gap-2 border-t border-border px-3 py-2">
|
||||
<input
|
||||
id="agent-input"
|
||||
type="text"
|
||||
value={draft}
|
||||
onChange={(e) => setDraft(e.target.value)}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key === 'Enter' && !e.nativeEvent.isComposing) ask();
|
||||
}}
|
||||
disabled={busy || !enabled}
|
||||
maxLength={500}
|
||||
placeholder="체크인 시간 3시로 바꿔줘"
|
||||
className="min-w-0 flex-1 rounded-md border border-border bg-background px-2 py-1.5 text-xs"
|
||||
/>
|
||||
<Button size="sm" variant="primary" disabled={busy || !enabled || !draft.trim()} onClick={ask}>
|
||||
<Send className="size-4" />
|
||||
<span className="sr-only">보내기</span>
|
||||
</Button>
|
||||
</div>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import {SocialConnectionCard} from '@/features/social/SocialConnectionCard';
|
||||
import {KakaoChannelCard} from '@/features/agent/KakaoChannelCard';
|
||||
import {AgentChatDock} from '@/features/agent/AgentChatDock';
|
||||
import {SocialConnectionNotice} from '@/features/social/SocialConnectionNotice';
|
||||
import {useMemo, useState} from 'react';
|
||||
import {Link, useNavigate} from 'react-router';
|
||||
@@ -502,6 +503,10 @@ export function SitesPage() {
|
||||
)}
|
||||
|
||||
</PageContainer>
|
||||
|
||||
{/* 떠 있는 대화창. 목록 위가 아니라 화면 구석인 이유는, 이게 '또 하나의 카드' 가 아니라
|
||||
어느 화면에서든 부를 수 있는 창구이기 때문이다(AgentChatDock 주석). */}
|
||||
<AgentChatDock sites={rows.map((row) => ({place_id: String(row.place_id), name: row.name}))} />
|
||||
</AppShell>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user