Skip to content

[Refactor] simplify conversation services#866

Merged
dingyi222666 merged 1 commit into
v1-devfrom
refactor/core-services
May 18, 2026
Merged

[Refactor] simplify conversation services#866
dingyi222666 merged 1 commit into
v1-devfrom
refactor/core-services

Conversation

@dingyi222666
Copy link
Copy Markdown
Member

This pr refactors core conversation services to simplify conversation resolution, management, runtime request handling, and typed constraint errors.

New Features

None

Bug fixes

  • Preserve managed conversation permission checks while reducing duplicated target lookup paths.
  • Keep conversation request abort, idle timeout, and round-decision cleanup behavior centralized and easier to reason about.
  • Use typed conversation and constraint errors for consistent conversation management failure handling.

Other Changes

  • Refactor ConversationService to consolidate managed constraint lookup, model fallback selection, ACL lookup, and target conversation matching.
  • Refactor ConversationRuntime to extract request key building, platform validation, abort signal linking, human message construction, and reply formatting.
  • Move reusable conversation error types into conversation_types.ts.
  • Simplify conversation management middleware formatting and error handling around the new typed errors.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 18, 2026

Warning

Rate limit exceeded

@dingyi222666 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 25 minutes and 13 seconds before requesting another review.

You’ve run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: bf38541b-b387-4fe6-82a6-931b0d0347c9

📥 Commits

Reviewing files that changed from the base of the PR and between 0c6b6ed and 43b6fff.

📒 Files selected for processing (5)
  • packages/core/src/middlewares/conversation/resolve_conversation.ts
  • packages/core/src/middlewares/system/conversation_manage.ts
  • packages/core/src/services/conversation.ts
  • packages/core/src/services/conversation_runtime.ts
  • packages/core/src/services/conversation_types.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/core-services

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@dingyi222666 dingyi222666 merged commit 2806be6 into v1-dev May 18, 2026
5 checks passed
@dingyi222666 dingyi222666 deleted the refactor/core-services branch May 18, 2026 04:58
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 43b6fffa28

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines 1383 to +1387
...options,
mode: 'context'
})
...(lookupMode === 'target' ? { permission: 'manage' } : {}),
mode: lookupMode
}
const resolved = await this.resolveConversation(session, lookupOptions)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Check manage permission before active resolution

updateConversationUsage() now calls resolveConversation() in 'active' mode before assertManageAllowed(), and active resolution can create a new conversation when none exists (see resolveActiveMode create path). That means a user who is blocked by manageMode: 'admin' can still mutate state (new conversation row) by running chatluna.use.*, then receive an authorization error afterward. The previous flow checked permissions before any active-mode side effects.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the conversation service and its middlewares to improve code structure, error handling, and performance. Key changes include the introduction of custom error classes, extraction of reusable logic into helper functions, and optimization of conversation resolution flows. Feedback suggests improving the readability of the parseLockOption function with a switch statement and optimizing database queries in collectAclConversationIds by using the $in operator to reduce the number of calls.

Comment on lines +788 to +796
function parseLockOption(
raw: string | undefined
): boolean | null | 'toggle' | undefined {
if (raw === 'reset') return null
if (raw === 'true' || raw === 'on' || raw === 'lock') return true
if (raw === 'false' || raw === 'off' || raw === 'unlock') return false
if (raw === 'toggle') return 'toggle'
return undefined
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The chain of if statements in parseLockOption can be replaced with a switch statement for improved readability and structure. This makes the different cases clearer at a glance.

function parseLockOption(
    raw: string | undefined
): boolean | null | 'toggle' | undefined {
    switch (raw) {
        case 'reset':
            return null
        case 'true':
        case 'on':
        case 'lock':
            return true
        case 'false':
        case 'off':
        case 'unlock':
            return false
        case 'toggle':
            return 'toggle'
        default:
            return undefined
    }
}

Comment on lines +1743 to 1776
private async collectAclConversationIds(
session: Session,
required: ConstraintPermission
) {
const acl: ACLRecord[] = []
const fetch = async (
principalType: 'user' | 'guild',
principalId: string,
permission: ConstraintPermission
) => {
acl.push(
...((await this.ctx.database.get('chatluna_acl', {
principalType: 'guild',
principalId: guildId,
permission: 'manage'
principalType,
principalId,
permission
})) as ACLRecord[])
)

if (required === 'view') {
acl.push(
...((await this.ctx.database.get('chatluna_acl', {
principalType: 'guild',
principalId: guildId,
permission: 'view'
})) as ACLRecord[])
)
}
}

const conversationIds = Array.from(
new Set(acl.map((item) => item.conversationId))
)

const matches: ConversationRecord[] = []

for (let i = 0; i < conversationIds.length; i += 200) {
const ids = conversationIds.slice(i, i + 200)
const conversations = (await this.ctx.database.get(
'chatluna_conversation',
{
id: {
$in: ids
}
}
)) as ConversationRecord[]
await fetch('user', session.userId, 'manage')
if (required === 'view') {
await fetch('user', session.userId, 'view')
}

matches.push(...conversations.filter(matched))
const guildId = session.guildId ?? session.channelId
if (guildId != null) {
await fetch('guild', guildId, 'manage')
if (required === 'view') {
await fetch('guild', guildId, 'view')
}
}

return matches
return Array.from(new Set(acl.map((item) => item.conversationId)))
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The collectAclConversationIds function currently makes up to four separate database calls to fetch ACLs. This can be optimized by using the $in operator to fetch permissions for 'view' and 'manage' in a single query for each principal type ('user' and 'guild'), reducing the number of database calls to a maximum of two.

    private async collectAclConversationIds(
        session: Session,
        required: ConstraintPermission
    ) {
        const acl: ACLRecord[] = []
        const permissions = required === 'view' ? ['view', 'manage'] : ['manage']

        const userAcls = await this.ctx.database.get('chatluna_acl', {
            principalType: 'user',
            principalId: session.userId,
            permission: { $in: permissions }
        })
        acl.push(...(userAcls as ACLRecord[]))

        const guildId = session.guildId ?? session.channelId
        if (guildId != null) {
            const guildAcls = await this.ctx.database.get('chatluna_acl', {
                principalType: 'guild',
                principalId: guildId,
                permission: { $in: permissions }
            })
            acl.push(...(guildAcls as ACLRecord[]))
        }

        return Array.from(new Set(acl.map((item) => item.conversationId)))
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant