Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
refactor(chat): a rating reaches the employee's next turn, and only that
trohitg1ca1bde75326
diff --git a/client/src/features/chat/ChatPane.tsx b/client/src/features/chat/ChatPane.tsxindex 4cecd688..fba93f90 100644--- a/client/src/features/chat/ChatPane.tsx+++ b/client/src/features/chat/ChatPane.tsx@@ -141,7 +141,7 @@ export function ChatPane({ host, ref }: { host: ChatHost; ref?: Ref<ChatPaneHandrate: (messageId, value) =>feedbackMutate({ messageId, value },- { onSuccess: (reaches) => value && notify(feedbackThanks(persona.name, reaches), 'success') },+ { onSuccess: () => value && notify(feedbackThanks(persona.name), 'success') },),}),[sessionId, editingId, busy, editMutate, switchMutate, regenerateMutate, feedbackMutate, notify, persona.name],diff --git a/client/src/features/chat/__tests__/branches.test.tsx b/client/src/features/chat/__tests__/branches.test.tsxindex e85e370a..31df58d4 100644--- a/client/src/features/chat/__tests__/branches.test.tsx+++ b/client/src/features/chat/__tests__/branches.test.tsx@@ -206,7 +206,7 @@ describe('rating and copying an answer', () => {expect(good).toHaveAttribute('aria-pressed', 'false');fireEvent.click(good);await waitFor(() => expect(screen.getByRole('button', { name: 'Good reply' })).toHaveAttribute('aria-pressed', 'true'));- await waitFor(() => expect(chat.notify).toHaveBeenCalledWith(feedbackThanks('Maya', ['next_turn']), 'success'));+ await waitFor(() => expect(chat.notify).toHaveBeenCalledWith(feedbackThanks('Maya'), 'success'));expect(requests('set_chat_feedback')[0]).toMatchObject({ message_id: 'a1', value: 'up' });// Pressed again: the rating is taken back, without a thank-you.fireEvent.click(screen.getByRole('button', { name: 'Good reply' }));@@ -250,6 +250,6 @@ describe('the copy', () => {expect(branchRefusalText('run_in_progress', 'Maya')).toContain('Maya is still answering');expect(branchRefusalText('older_generation', 'Maya')).toContain('before Maya restarted');expect(branchRefusalText('something', 'Maya')).toBe('That didn’t go through. Try again.');- expect(feedbackThanks('Maya', ['next_turn', 'memory'])).toBe('Thanks — Maya will see this next time and remember it.');+ expect(feedbackThanks('Maya')).toBe('Thanks — Maya will see this next time.');});});diff --git a/client/src/features/chat/data/branches.ts b/client/src/features/chat/data/branches.tsindex 167d6492..18002a3a 100644--- a/client/src/features/chat/data/branches.ts+++ b/client/src/features/chat/data/branches.ts@@ -40,7 +40,6 @@ type Reply = {run_id?: string | null;message_id?: string | null;delivery?: string;- reaches?: unknown;};export interface StartedRun {@@ -120,12 +119,9 @@ export function useSwitchChatBranch(sessionId: string, scope: ThreadScope, onRef});}-/** Where a rating goes: `next_turn` (the employee reads it before its next- * answer) and `memory` (a Memory tool keeps it). */-export type FeedbackReach = 'next_turn' | 'memory';-/** Rate an answer good or bad, or take the rating back (`null`). Shown at- * once; put back when the server refuses. */+ * once; put back when the server refuses. The employee reads a rating+ * before its next answer. */export function useSetChatFeedback(sessionId: string, scope: ThreadScope, onRefused?: (error: ChatBranchError) => void) {const { request, refetch, queryClient } = useBranchRequest(sessionId, scope);const key = chatThreadKey(sessionId, scope);@@ -133,11 +129,9 @@ export function useSetChatFeedback(sessionId: string, scope: ThreadScope, onRefuqueryClient.setQueryData<ChatThreadData>(key, (data) =>data ? { ...data, messages: data.messages.map((message) => (message.id === messageId ? { ...message, feedback: value } : message)) } : data,);- return useMutation<FeedbackReach[], ChatBranchError, { messageId: string; value: Feedback | null }, { before: Feedback | null }>({+ return useMutation<void, ChatBranchError, { messageId: string; value: Feedback | null }, { before: Feedback | null }>({mutationFn: async ({ messageId, value }) => {- const reply = await request('set_chat_feedback', { message_id: messageId, value });- const reaches = Array.isArray(reply.reaches) ? reply.reaches : [];- return reaches.filter((reach): reach is FeedbackReach => reach === 'next_turn' || reach === 'memory');+ await request('set_chat_feedback', { message_id: messageId, value });},onMutate: ({ messageId, value }) => {const before = queryClient.getQueryData<ChatThreadData>(key)?.messages.find((message) => message.id === messageId)?.feedback ?? null;diff --git a/client/src/features/chat/turns/runCopy.ts b/client/src/features/chat/turns/runCopy.tsindex 4d87414e..c3d3cd4e 100644--- a/client/src/features/chat/turns/runCopy.ts+++ b/client/src/features/chat/turns/runCopy.ts@@ -111,8 +111,6 @@ export function branchRefusalText(code: string, name: string): string {}/** What the owner hears after rating an answer: where the rating goes. */-export function feedbackThanks(name: string, reaches: readonly string[]): string {- return reaches.includes('memory')- ? `Thanks — ${name} will see this next time and remember it.`- : `Thanks — ${name} will see this next time.`;+export function feedbackThanks(name: string): string {+ return `Thanks — ${name} will see this next time.`;}diff --git a/server/services/chat/feedback.py b/server/services/chat/feedback.pyindex ff980a39..0f9f83c2 100644--- a/server/services/chat/feedback.py+++ b/server/services/chat/feedback.py@@ -5,30 +5,20 @@employee a note for its next turn in the chat (``[feedback]{...}[/feedback]``keyed per answer: changing the rating before that turn replaces the note,and taking it back drops one not yet told). It answers where the rating-reaches: ``next_turn``, plus whatever a registered listener adds-(``register_feedback_listener``; a plugin that keeps it somewhere the-employee reads later answers that place, such as ``memory``).+reaches: ``next_turn``."""from __future__ import annotationsimport jsonfrom datetime import datetime, timezone-from typing import Any, Awaitable, Callable, Dict, List, Optional+from typing import Any, Dict, List, Optionalfrom sqlmodel import select-from core.logging import get_loggerfrom models.chat import ChatFeedbackfrom models.database import ChatMessage-logger = get_logger(__name__)--#: ``await listener(database=..., workflow_id=..., message=<row dict>,-#: value="up"|"down")`` -> a reach it added (``"memory"``), or None.-FeedbackListener = Callable[..., Awaitable[Optional[str]]]-_LISTENERS: List[FeedbackListener] = []-VALUES = ("up", "down")#: How much of the answer the note quotes._EXCERPT_CHARS = 300@@ -42,13 +32,6 @@ class FeedbackRefused(ValueError):self.code = code-def register_feedback_listener(listener: FeedbackListener) -> None:- """Run ``listener`` after each rating the owner gives (not a rating taken- back). Registering the same listener twice is a no-op."""- if listener not in _LISTENERS:- _LISTENERS.append(listener)--def feedback_note(answer: str, value: str) -> str:"""The line the employee reads before its next answer."""excerpt = " ".join((answer or "").split())@@ -94,16 +77,7 @@ async def set_feedback(database: Any, *, session_id: str, message_uid: str, valuawait drop_note(database, session_id=session_id, key=key)return []await upsert_note(database, session_id=session_id, key=key, kind="feedback", text=feedback_note(row_dict["message"], value))- reaches = ["next_turn"]- for listener in list(_LISTENERS):- try:- reach = await listener(database=database, workflow_id=session_id, message=row_dict, value=value)- except Exception: # noqa: BLE001 - the note reaches the employee either way- logger.warning("Feedback listener failed", listener=getattr(listener, "__qualname__", repr(listener)), exc_info=True)- continue- if isinstance(reach, str) and reach and reach not in reaches:- reaches.append(reach)- return reaches+ return ["next_turn"]async def feedback_for(database: Any, session_id: str) -> Dict[str, str]:@@ -114,11 +88,9 @@ async def feedback_for(database: Any, session_id: str) -> Dict[str, str]:__all__ = [- "FeedbackListener","FeedbackRefused","VALUES","feedback_for","feedback_note",- "register_feedback_listener","set_feedback",]diff --git a/server/services/chat/handlers.py b/server/services/chat/handlers.pyindex d111abd4..2e6bddb8 100644--- a/server/services/chat/handlers.py+++ b/server/services/chat/handlers.py@@ -749,9 +749,9 @@ async def handle_switch_chat_branch(data: Dict[str, Any], websocket: WebSocket)@ws_handler("message_id")async def handle_set_chat_feedback(data: Dict[str, Any], websocket: WebSocket) -> Dict[str, Any]:"""Rate an answer (``value``: ``up`` or ``down``) or take the rating back- (``null``). Answers where it reaches (``reaches``: ``next_turn``, and- ``memory`` when the employee keeps it too); ``not_found`` for a message- that is not an answer in this chat."""+ (``null``). Answers where it reaches (``reaches``: ``next_turn``, the+ employee's next turn; none for a rating taken back); ``not_found`` for a+ message that is not an answer in this chat."""session_id = session_id_of(data)database = container.database()try:diff --git a/server/tests/services/chat/test_branches.py b/server/tests/services/chat/test_branches.pyindex cc516de4..bcf3a950 100644--- a/server/tests/services/chat/test_branches.py+++ b/server/tests/services/chat/test_branches.py@@ -327,23 +327,6 @@ async def test_rating_an_answer(chat):assert (await chat.handlers.handle_set_chat_feedback({"session_id": "wf", "message_id": reply["id"], "value": "meh"}, None))["error"] == "invalid_request"-async def test_a_rating_listener_adds_where_it_reaches(chat, monkeypatch):- from services.chat import feedback-- heard = []-- async def remember(*, database, workflow_id, message, value):- heard.append((workflow_id, message["uid"], value))- return "memory"-- monkeypatch.setattr(feedback, "_LISTENERS", [])- feedback.register_feedback_listener(remember)- await conversation(chat)- reply = (await thread(chat))["messages"][1]- rated = await chat.handlers.handle_set_chat_feedback({"session_id": "wf", "message_id": reply["id"], "value": "down"}, None)- assert rated["reaches"] == ["next_turn", "memory"] and heard == [("wf", reply["id"], "down")]--async def test_clearing_the_chat_forgets_versions_and_ratings(chat):from sqlmodel import select