feat: Enhance Mattermost integration by adding sync commands and context checks for recent messages
This commit is contained in:
@@ -35,6 +35,78 @@ async function resolveSyncContent(directory, stdoutText) {
|
||||
return (generated || "").trim()
|
||||
}
|
||||
|
||||
function extractPromptText(event) {
|
||||
const candidates = [
|
||||
event?.properties?.text,
|
||||
event?.properties?.message,
|
||||
event?.properties?.prompt,
|
||||
event?.text,
|
||||
event?.message,
|
||||
event?.prompt,
|
||||
]
|
||||
|
||||
for (const candidate of candidates) {
|
||||
if (typeof candidate === "string" && candidate.trim()) {
|
||||
return candidate
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.stringify(event)
|
||||
} catch {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
function requiresFreshMattermost(promptText) {
|
||||
const text = promptText
|
||||
.normalize("NFD")
|
||||
.replace(/[\u0300-\u036f]/g, "")
|
||||
.toLowerCase()
|
||||
|
||||
const freshnessTerms = [
|
||||
"ultimo",
|
||||
"ultimos",
|
||||
"ultima",
|
||||
"ultimas",
|
||||
"latest",
|
||||
"last",
|
||||
"reciente",
|
||||
"recent",
|
||||
"nuevo",
|
||||
"new",
|
||||
"actualiza",
|
||||
"sync",
|
||||
"sincroniza",
|
||||
"revisa",
|
||||
"dijo",
|
||||
"menciono",
|
||||
"respondio",
|
||||
"contesto",
|
||||
"check",
|
||||
"look at",
|
||||
"said",
|
||||
"mentioned",
|
||||
"replied",
|
||||
]
|
||||
|
||||
const mattermostTerms = [
|
||||
"mattermost",
|
||||
"mensaje",
|
||||
"mensajes",
|
||||
"message",
|
||||
"messages",
|
||||
"jeff",
|
||||
"manager",
|
||||
"fidelity-preguntas",
|
||||
]
|
||||
|
||||
return (
|
||||
freshnessTerms.some((term) => text.includes(term)) &&
|
||||
mattermostTerms.some((term) => text.includes(term))
|
||||
)
|
||||
}
|
||||
|
||||
export const MattermostInbox = async ({ $, directory, client }) => {
|
||||
let lastSyncAt = 0
|
||||
|
||||
@@ -43,7 +115,7 @@ export const MattermostInbox = async ({ $, directory, client }) => {
|
||||
await writeFile(statusPath, `${JSON.stringify(data, null, 2)}\n`, "utf8")
|
||||
}
|
||||
|
||||
async function sync(reason) {
|
||||
async function sync(reason, options = {}) {
|
||||
const command = await resolveSyncCommand(directory)
|
||||
if (!command) return
|
||||
|
||||
@@ -54,7 +126,7 @@ export const MattermostInbox = async ({ $, directory, client }) => {
|
||||
const minIntervalMs = Math.max(1, Number.isNaN(intervalMinutes) ? 15 : intervalMinutes) * 60 * 1000
|
||||
const now = Date.now()
|
||||
|
||||
if (now - lastSyncAt < minIntervalMs) return
|
||||
if (!options.force && now - lastSyncAt < minIntervalMs) return
|
||||
lastSyncAt = now
|
||||
|
||||
const inboxDir = path.join(directory, "ai/inbox")
|
||||
@@ -78,6 +150,7 @@ export const MattermostInbox = async ({ $, directory, client }) => {
|
||||
await writeStatus(statusPath, {
|
||||
syncedAt: nowIso(),
|
||||
reason,
|
||||
forced: Boolean(options.force),
|
||||
changed: previous !== `${output}\n` && previous !== output,
|
||||
commandConfigured: true,
|
||||
commandSource: process.env.FIDELITY_MATTERMOST_SYNC_CMD?.trim() ? "env" : "workspace-default",
|
||||
@@ -86,6 +159,7 @@ export const MattermostInbox = async ({ $, directory, client }) => {
|
||||
await writeStatus(statusPath, {
|
||||
syncedAt: nowIso(),
|
||||
reason,
|
||||
forced: Boolean(options.force),
|
||||
changed: false,
|
||||
commandConfigured: true,
|
||||
commandSource: process.env.FIDELITY_MATTERMOST_SYNC_CMD?.trim() ? "env" : "workspace-default",
|
||||
@@ -96,6 +170,7 @@ export const MattermostInbox = async ({ $, directory, client }) => {
|
||||
await writeStatus(statusPath, {
|
||||
syncedAt: nowIso(),
|
||||
reason,
|
||||
forced: Boolean(options.force),
|
||||
changed: false,
|
||||
commandConfigured: true,
|
||||
commandSource: process.env.FIDELITY_MATTERMOST_SYNC_CMD?.trim() ? "env" : "workspace-default",
|
||||
@@ -123,7 +198,14 @@ export const MattermostInbox = async ({ $, directory, client }) => {
|
||||
}
|
||||
|
||||
if (event.type === "tui.prompt.append") {
|
||||
await sync("tui.prompt.append")
|
||||
const promptText = extractPromptText(event)
|
||||
const forceFreshMattermost = requiresFreshMattermost(promptText)
|
||||
await sync(
|
||||
forceFreshMattermost
|
||||
? "tui.prompt.append:fresh-mattermost-request"
|
||||
: "tui.prompt.append",
|
||||
{ force: forceFreshMattermost },
|
||||
)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user