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:
parent
7db5bc6989
commit
ce4ee83ca2
|
|
@ -227,6 +227,7 @@ export class MatrixBotClient {
|
||||||
public userId: string | null = null
|
public userId: string | null = null
|
||||||
private listeners: MessageListener[] = []
|
private listeners: MessageListener[] = []
|
||||||
private readonly trigger: string
|
private readonly trigger: string
|
||||||
|
private readonly triggerPatterns: string[]
|
||||||
private readonly botName: string
|
private readonly botName: string
|
||||||
private readonly threadIsolation: boolean
|
private readonly threadIsolation: boolean
|
||||||
private readonly respondToThreadReplies: boolean
|
private readonly respondToThreadReplies: boolean
|
||||||
|
|
@ -236,6 +237,7 @@ export class MatrixBotClient {
|
||||||
constructor(options: MatrixOptions) {
|
constructor(options: MatrixOptions) {
|
||||||
this.options = options
|
this.options = options
|
||||||
this.trigger = options.triggerPatterns?.[0] || "!oc "
|
this.trigger = options.triggerPatterns?.[0] || "!oc "
|
||||||
|
this.triggerPatterns = options.triggerPatterns || []
|
||||||
this.botName = options.botName || "opencode"
|
this.botName = options.botName || "opencode"
|
||||||
this.threadIsolation = options.threadIsolation !== false
|
this.threadIsolation = options.threadIsolation !== false
|
||||||
this.respondToThreadReplies = options.respondToThreadReplies !== false
|
this.respondToThreadReplies = options.respondToThreadReplies !== false
|
||||||
|
|
@ -375,28 +377,43 @@ export class MatrixBotClient {
|
||||||
let query = ""
|
let query = ""
|
||||||
const botNameQuery = extractBotNameQuery(body, this.botName)
|
const botNameQuery = extractBotNameQuery(body, this.botName)
|
||||||
|
|
||||||
if (body.startsWith(this.trigger + " ")) {
|
if (this.triggerPatterns.length === 0) {
|
||||||
query = body.slice(this.trigger.length + 1).trim()
|
// No triggers configured — intercept all messages
|
||||||
} 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) {
|
|
||||||
query = body
|
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 {
|
} 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()
|
query = query.replace(/^[:\s]+/, "").trim()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue