fix: respect triggerPatterns config, allow all messages when empty

- Check all trigger patterns, not just first one
- When triggerPatterns is empty, intercept all messages
- When configured, only process trigger/mention/thread-reply messages
- Remove DM auto-intercept when triggers are set
This commit is contained in:
Бородин Роман 2026-09-25 16:15:18 +03:00
parent 7db5bc6989
commit ce4ee83ca2
1 changed files with 37 additions and 20 deletions

View File

@ -227,6 +227,7 @@ export class MatrixBotClient {
public userId: string | null = null
private listeners: MessageListener[] = []
private readonly trigger: string
private readonly triggerPatterns: string[]
private readonly botName: string
private readonly threadIsolation: boolean
private readonly respondToThreadReplies: boolean
@ -236,6 +237,7 @@ export class MatrixBotClient {
constructor(options: MatrixOptions) {
this.options = options
this.trigger = options.triggerPatterns?.[0] || "!oc "
this.triggerPatterns = options.triggerPatterns || []
this.botName = options.botName || "opencode"
this.threadIsolation = options.threadIsolation !== false
this.respondToThreadReplies = options.respondToThreadReplies !== false
@ -375,28 +377,43 @@ export class MatrixBotClient {
let query = ""
const botNameQuery = extractBotNameQuery(body, this.botName)
if (body.startsWith(this.trigger + " ")) {
query = body.slice(this.trigger.length + 1).trim()
} else if (body.startsWith(this.trigger)) {
query = body.slice(this.trigger.length).trim()
} else if (body.includes(this.userId!)) {
query = body.replace(this.userId!, "").trim()
} else if (botNameQuery !== null) {
query = botNameQuery
} else if (isDM) {
if (this.triggerPatterns.length === 0) {
// No triggers configured — intercept all messages
query = body
} else if (this.threadIsolation && shouldHandleThreadReply({
enabled: this.respondToThreadReplies,
text: body,
threadRootEventId,
trigger: this.trigger,
botUserId: this.userId!,
})) {
// Implicit thread follow-up
query = body
log(`[THREAD] ${message.sender} in ${context.sessionId}: ${body}`)
} else {
return
// Check all trigger patterns
let matchedTrigger = ""
for (const pattern of this.triggerPatterns) {
if (body.startsWith(pattern + " ")) {
matchedTrigger = pattern
query = body.slice(pattern.length + 1).trim()
break
} else if (body.startsWith(pattern)) {
matchedTrigger = pattern
query = body.slice(pattern.length).trim()
break
}
}
// If no trigger matched, check bot mention
if (!matchedTrigger) {
if (body.includes(this.userId!)) {
query = body.replace(this.userId!, "").trim()
} else if (botNameQuery !== null) {
query = botNameQuery
} else if (this.threadIsolation && shouldHandleThreadReply({
enabled: this.respondToThreadReplies,
text: body,
threadRootEventId,
trigger: this.triggerPatterns[0],
botUserId: this.userId!,
})) {
query = body
log(`[THREAD] ${message.sender} in ${context.sessionId}: ${body}`)
} else {
return
}
}
}
query = query.replace(/^[:\s]+/, "").trim()