- Introduced new commands and skills for workspace memory curation, professional communication, and status reporting. - Updated existing commands to utilize new skills and improve clarity in instructions. - Created a new workspace context command to load reusable core and active project profile. - Enhanced Mattermost inbox integration with support for generic environment variables. - Established a clear separation between project-independent core logic and project-specific profiles. - Improved documentation across various files to reflect changes in workflow and command usage. - Added operational memory management rules to ensure accurate context promotion and correction. - Updated README and workflow documents to guide users in utilizing the new structure effectively.
243 lines
6.1 KiB
JavaScript
243 lines
6.1 KiB
JavaScript
import { access, mkdir, readFile, writeFile } from "node:fs/promises"
|
|
import path from "node:path"
|
|
|
|
async function safeRead(filePath) {
|
|
try {
|
|
return await readFile(filePath, "utf8")
|
|
} catch {
|
|
return null
|
|
}
|
|
}
|
|
|
|
function nowIso() {
|
|
return new Date().toISOString()
|
|
}
|
|
|
|
async function resolveSyncCommand(directory) {
|
|
const configured =
|
|
process.env.AIW_MATTERMOST_SYNC_CMD?.trim() ||
|
|
process.env.FIDELITY_MATTERMOST_SYNC_CMD?.trim()
|
|
if (configured) return configured
|
|
|
|
const fallbackScript = path.join(directory, "scripts/mattermost/sync.sh")
|
|
try {
|
|
await access(fallbackScript)
|
|
return `bash "${fallbackScript}"`
|
|
} catch {
|
|
return ""
|
|
}
|
|
}
|
|
|
|
async function resolveSyncContent(directory, stdoutText) {
|
|
const cleaned = (stdoutText || "").trim()
|
|
if (cleaned) return cleaned
|
|
|
|
const generatedPath = path.join(directory, "scripts/mattermost/generated/mattermost_context.jsonl")
|
|
const generated = await safeRead(generatedPath)
|
|
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",
|
|
"manager",
|
|
"stakeholder",
|
|
"supervisor",
|
|
"lead",
|
|
"inbox",
|
|
]
|
|
|
|
const configuredTerms = [
|
|
process.env.AIW_COMMUNICATION_FRESH_TERMS,
|
|
process.env.AIW_MANAGER_NAME,
|
|
process.env.AIW_PRIMARY_STAKEHOLDER,
|
|
process.env.FIDELITY_MANAGER_NAME,
|
|
"jeff",
|
|
"fidelity-preguntas",
|
|
]
|
|
.filter(Boolean)
|
|
.flatMap((value) => String(value).split(","))
|
|
.map((value) => value.trim().toLowerCase())
|
|
.filter(Boolean)
|
|
|
|
const sourceTerms = [
|
|
...mattermostTerms,
|
|
...configuredTerms,
|
|
]
|
|
|
|
return (
|
|
freshnessTerms.some((term) => text.includes(term)) &&
|
|
sourceTerms.some((term) => text.includes(term))
|
|
)
|
|
}
|
|
|
|
export const MattermostInbox = async ({ $, directory, client }) => {
|
|
let lastSyncAt = 0
|
|
|
|
async function writeStatus(statusPath, data) {
|
|
await mkdir(path.dirname(statusPath), { recursive: true })
|
|
await writeFile(statusPath, `${JSON.stringify(data, null, 2)}\n`, "utf8")
|
|
}
|
|
|
|
async function sync(reason, options = {}) {
|
|
const command = await resolveSyncCommand(directory)
|
|
if (!command) return
|
|
|
|
const intervalMinutes = Number.parseInt(
|
|
process.env.AIW_MATTERMOST_SYNC_INTERVAL_MINUTES ||
|
|
process.env.FIDELITY_MATTERMOST_SYNC_INTERVAL_MINUTES ||
|
|
"15",
|
|
10,
|
|
)
|
|
const minIntervalMs = Math.max(1, Number.isNaN(intervalMinutes) ? 15 : intervalMinutes) * 60 * 1000
|
|
const now = Date.now()
|
|
|
|
if (!options.force && now - lastSyncAt < minIntervalMs) return
|
|
lastSyncAt = now
|
|
|
|
const inboxDir = path.join(directory, "ai/inbox")
|
|
const latestPath = path.join(inboxDir, "mattermost-latest.md")
|
|
const statusPath = path.join(inboxDir, "mattermost-status.json")
|
|
|
|
const commandSource = process.env.AIW_MATTERMOST_SYNC_CMD?.trim()
|
|
? "aiw-env"
|
|
: process.env.FIDELITY_MATTERMOST_SYNC_CMD?.trim()
|
|
? "fidelity-env"
|
|
: "workspace-default"
|
|
|
|
try {
|
|
await mkdir(inboxDir, { recursive: true })
|
|
const stdoutText = await $`bash -lc ${command}`.text()
|
|
const output = await resolveSyncContent(directory, stdoutText)
|
|
const previous = await safeRead(latestPath)
|
|
|
|
if (output) {
|
|
if (previous !== `${output}\n` && previous !== output) {
|
|
const stamp = nowIso().replace(/[:]/g, "-")
|
|
const snapshotPath = path.join(inboxDir, `mattermost-${stamp}.md`)
|
|
await writeFile(latestPath, `${output}\n`, "utf8")
|
|
await writeFile(snapshotPath, `${output}\n`, "utf8")
|
|
}
|
|
|
|
await writeStatus(statusPath, {
|
|
syncedAt: nowIso(),
|
|
reason,
|
|
forced: Boolean(options.force),
|
|
changed: previous !== `${output}\n` && previous !== output,
|
|
commandConfigured: true,
|
|
commandSource,
|
|
})
|
|
} else {
|
|
await writeStatus(statusPath, {
|
|
syncedAt: nowIso(),
|
|
reason,
|
|
forced: Boolean(options.force),
|
|
changed: false,
|
|
commandConfigured: true,
|
|
commandSource,
|
|
note: "Sync command returned no output.",
|
|
})
|
|
}
|
|
} catch (error) {
|
|
await writeStatus(statusPath, {
|
|
syncedAt: nowIso(),
|
|
reason,
|
|
forced: Boolean(options.force),
|
|
changed: false,
|
|
commandConfigured: true,
|
|
commandSource,
|
|
error: error instanceof Error ? error.message : String(error),
|
|
})
|
|
|
|
await client.app.log({
|
|
body: {
|
|
service: "mattermost-inbox",
|
|
level: "warn",
|
|
message: "Mattermost sync failed",
|
|
extra: {
|
|
reason,
|
|
error: error instanceof Error ? error.message : String(error),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
}
|
|
|
|
return {
|
|
event: async ({ event }) => {
|
|
if (event.type === "session.created") {
|
|
await sync("session.created")
|
|
}
|
|
|
|
if (event.type === "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 },
|
|
)
|
|
}
|
|
},
|
|
}
|
|
}
|