fix: remove obvious/redundant code comments per PR review
Remove comments that restate what the code does (e.g., "Optimistic user message", "Extract usage and title from data parts", "Subscribe to the thread's event stream"). Keep comments that explain WHY (mid-stream adapter, catchup chunks, graphql-sse reconnection). https://claude.ai/code/session_01EcJHCeJy44AKw2rafgQcnu
This commit is contained in:
@@ -40,7 +40,6 @@ export const AgentChatStreamSubscriptionEffect = () => {
|
||||
|
||||
useAgentChat(ensureThreadIdForSend);
|
||||
|
||||
// Subscribe to the thread's event stream
|
||||
const subscriptionThreadId =
|
||||
currentAIChatThread !== null && isValidUuid(currentAIChatThread)
|
||||
? currentAIChatThread
|
||||
@@ -48,7 +47,6 @@ export const AgentChatStreamSubscriptionEffect = () => {
|
||||
|
||||
useAgentChatSubscription(subscriptionThreadId);
|
||||
|
||||
// Sync fetched messages to the displayed messages atom when no stream is active
|
||||
const agentChatFetchedMessages = useAtomComponentFamilyStateValue(
|
||||
agentChatFetchedMessagesComponentFamilyState,
|
||||
{ threadId: currentAIChatThread },
|
||||
|
||||
@@ -89,9 +89,6 @@ export const useAgentChat = (
|
||||
|
||||
const browsingContext = getBrowsingContext();
|
||||
|
||||
// Optimistic user message — always placed in the main messages atom.
|
||||
// The next REFETCH_MESSAGES event will reconcile with server state,
|
||||
// moving it to the queued list if the server queued it.
|
||||
const optimisticUserMessage: ExtendedUIMessage = {
|
||||
id: v4(),
|
||||
role: 'user',
|
||||
@@ -145,9 +142,6 @@ export const useAgentChat = (
|
||||
|
||||
const responseBody = await response.json();
|
||||
|
||||
// If the server queued the message (stream was active), remove the
|
||||
// optimistic entry from the main conversation — the refetch below will
|
||||
// place it in the dedicated queue list instead.
|
||||
if (responseBody.queued) {
|
||||
const latestMessages = store.get(
|
||||
agentChatMessagesComponentFamilyState.atomFamily(atomKey),
|
||||
|
||||
@@ -162,8 +162,6 @@ export const useAgentChatSubscription = (threadId: string | null) => {
|
||||
agentChatMessagesComponentFamilyState.atomFamily(atomKey),
|
||||
);
|
||||
|
||||
// Find and replace the streaming assistant message by ID, or append it.
|
||||
// This preserves optimistic user messages that aren't yet in fetched data.
|
||||
const streamingMsgIndex = currentMessages.findIndex(
|
||||
(message) => message.id === messageToFlush.id,
|
||||
);
|
||||
@@ -188,13 +186,10 @@ export const useAgentChatSubscription = (threadId: string | null) => {
|
||||
latestMessage = message;
|
||||
|
||||
if (!isDefined(throttleTimer)) {
|
||||
// Leading edge: flush immediately so the first chunk renders instantly
|
||||
flushToAtom();
|
||||
|
||||
// Then suppress further flushes for THROTTLE_MS
|
||||
throttleTimer = setTimeout(() => {
|
||||
throttleTimer = null;
|
||||
// Trailing edge: flush whatever accumulated during the window
|
||||
flushToAtom();
|
||||
}, THROTTLE_MS);
|
||||
}
|
||||
@@ -206,7 +201,6 @@ export const useAgentChatSubscription = (threadId: string | null) => {
|
||||
for await (const message of messageStream) {
|
||||
const extendedMessage = message as ExtendedUIMessage;
|
||||
|
||||
// Extract usage and title from data parts
|
||||
const titlePart = extendedMessage.parts.find(
|
||||
(part) => part.type === 'data-thread-title',
|
||||
);
|
||||
@@ -255,7 +249,6 @@ export const useAgentChatSubscription = (threadId: string | null) => {
|
||||
scheduleAtomUpdate(extendedMessage);
|
||||
}
|
||||
|
||||
// Stream finished -- flush last message immediately
|
||||
if (isDefined(throttleTimer)) {
|
||||
clearTimeout(throttleTimer);
|
||||
throttleTimer = null;
|
||||
@@ -299,7 +292,6 @@ export const useAgentChatSubscription = (threadId: string | null) => {
|
||||
}
|
||||
|
||||
case 'message-persisted': {
|
||||
// Close the current stream writer so readUIMessageStream finishes
|
||||
if (isDefined(writerRef.current)) {
|
||||
writerRef.current.close().catch(() => {});
|
||||
writerRef.current = null;
|
||||
|
||||
-2
@@ -115,8 +115,6 @@ export class AgentChatController {
|
||||
);
|
||||
}
|
||||
|
||||
// Server decides: if the thread has an active stream, queue the message;
|
||||
// otherwise start streaming immediately.
|
||||
if (isDefined(thread.activeStreamId)) {
|
||||
const message = await this.agentChatService.queueMessage({
|
||||
threadId,
|
||||
|
||||
+2
-8
@@ -2,14 +2,8 @@ import { Field, ObjectType } from '@nestjs/graphql';
|
||||
|
||||
import GraphQLJSON from 'graphql-type-json';
|
||||
|
||||
// The `event` field is typed as GraphQLJSON because the payload is a
|
||||
// discriminated union (AgentChatSubscriptionEvent from twenty-shared)
|
||||
// whose variants carry different shapes:
|
||||
// - { type: 'stream-chunk', chunk: Record<string, unknown> }
|
||||
// - { type: 'message-persisted', messageId: string }
|
||||
// - { type: 'queue-updated' }
|
||||
// - { type: 'stream-error', code: string, message: string }
|
||||
// Clients should cast this field to AgentChatSubscriptionEvent.
|
||||
// Typed as JSON because the payload is AgentChatSubscriptionEvent
|
||||
// (a discriminated union defined in twenty-shared).
|
||||
@ObjectType('AgentChatEvent')
|
||||
export class AgentChatEventDTO {
|
||||
@Field(() => String)
|
||||
|
||||
-2
@@ -68,8 +68,6 @@ export class AgentChatStreamingService {
|
||||
);
|
||||
}
|
||||
|
||||
// Persist the user message before enqueuing the job so the frontend
|
||||
// refetch (triggered when the controller returns) finds it in the DB.
|
||||
const savedUserMessage = await this.agentChatService.addMessage({
|
||||
threadId,
|
||||
uiMessage: {
|
||||
|
||||
Reference in New Issue
Block a user