From ce4ee83ca2f3997e67f397b5a04627d3598a024c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D0=BE=D1=80=D0=BE=D0=B4=D0=B8=D0=BD=20=D0=A0=D0=BE?= =?UTF-8?q?=D0=BC=D0=B0=D0=BD?= Date: Fri, 25 Sep 2026 16:15:18 +0300 Subject: [PATCH] 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 --- plugins/matrix-plugin/matrix-client.ts | 57 +++++++++++++++++--------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/plugins/matrix-plugin/matrix-client.ts b/plugins/matrix-plugin/matrix-client.ts index a1e5aa3..678b01c 100644 --- a/plugins/matrix-plugin/matrix-client.ts +++ b/plugins/matrix-plugin/matrix-client.ts @@ -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()