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()