Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[🐴] Convo agent cleanup #4109

Merged
merged 4 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 10 additions & 32 deletions src/state/messages/convo/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ import {MessagesEventBus} from '#/state/messages/events/agent'
import {MessagesEventBusError} from '#/state/messages/events/types'
import {DM_SERVICE_HEADERS} from '#/state/queries/messages/const'

// TODO temporary
let DEBUG_ACTIVE_CHAT: string | undefined

export function isConvoItemMessage(
item: ConvoItem,
): item is ConvoItem & {type: 'message'} {
Expand Down Expand Up @@ -102,14 +99,6 @@ export class Convo {
this.ingestFirehose = this.ingestFirehose.bind(this)
this.onFirehoseConnect = this.onFirehoseConnect.bind(this)
this.onFirehoseError = this.onFirehoseError.bind(this)

if (DEBUG_ACTIVE_CHAT) {
logger.error(`Convo: another chat was already active`, {
convoId: this.convoId,
})
} else {
DEBUG_ACTIVE_CHAT = this.convoId
}
}

private commit() {
Expand Down Expand Up @@ -494,7 +483,6 @@ export class Convo {

suspend() {
this.dispatch({event: ConvoDispatchEvent.Suspend})
DEBUG_ACTIVE_CHAT = undefined
}

/**
Expand Down Expand Up @@ -602,15 +590,6 @@ export class Convo {
this.isFetchingHistory = true
this.commit()

/*
* Delay if paginating while scrolled to prevent momentum scrolling from
* jerking the list around, plus makes it feel a little more human.
*/
if (this.pastMessages.size > 0) {
await new Promise(y => setTimeout(y, 500))
// throw new Error('UNCOMMENT TO TEST RETRY')
}
Comment on lines -609 to -612
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested this on sim and doesn't seem to be an issue anymore, nice work @haileyok !


const nextCursor = this.oldestRev // for TS
const response = await networkRetry(2, () => {
return this.agent.api.chat.bsky.convo.getMessages(
Expand Down Expand Up @@ -679,6 +658,9 @@ export class Convo {
}
}
},
/*
* This is VERY important — we only want events for this convo.
*/
{convoId: this.convoId},
)
}
Expand Down Expand Up @@ -725,12 +707,6 @@ export class Convo {
*/
this.latestRev = ev.rev

/*
* This is VERY important. We don't want to insert any messages from
* your other chats.
*/
if (ev.convoId !== this.convoId) continue

if (
ChatBskyConvoDefs.isLogCreateMessage(ev) &&
ChatBskyConvoDefs.isMessageView(ev.message)
Expand Down Expand Up @@ -775,7 +751,7 @@ export class Convo {

private pendingMessageFailure: 'recoverable' | 'unrecoverable' | null = null

async sendMessage(message: ChatBskyConvoSendMessage.InputSchema['message']) {
sendMessage(message: ChatBskyConvoSendMessage.InputSchema['message']) {
// Ignore empty messages for now since they have no other purpose atm
if (!message.text.trim()) return

Expand Down Expand Up @@ -828,6 +804,9 @@ export class Convo {
})
const res = response.data

// remove from queue
this.pendingMessages.delete(id)

/*
* Insert into `newMessages` as soon as we have a real ID. That way, when
* we get an event log back, we can replace in situ.
Expand All @@ -836,15 +815,14 @@ export class Convo {
...res,
$type: 'chat.bsky.convo.defs#messageView',
})
this.pendingMessages.delete(id)
// render new message state, prior to firehose
this.commit()

// continue queue processing
await this.processPendingMessages()

this.commit()
} catch (e: any) {
logger.error(e, {context: `Convo: failed to send message`})
this.handleSendMessageFailure(e)
} finally {
this.isProcessingPendingMessages = false
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/state/messages/convo/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export type ConvoItem =
type DeleteMessage = (messageId: string) => Promise<void>
type SendMessage = (
message: ChatBskyConvoSendMessage.InputSchema['message'],
) => Promise<void>
) => void
type FetchMessageHistory = () => Promise<void>

export type ConvoStateUninitialized = {
Expand Down
Loading