Scaffold Matrix + GitOps + Claude automation stack
build-agent / build-and-push (push) Failing after 18s

This commit is contained in:
2026-08-23 10:25:57 +00:00
commit 357791d6e3
12 changed files with 576 additions and 0 deletions
+55
View File
@@ -0,0 +1,55 @@
import { MatrixClient, SimpleFsStorageProvider, AutojoinRoomsMixin } from "matrix-bot-sdk";
import { runChatTask } from "./runner.js";
const HOMESERVER_URL = process.env.MATRIX_HOMESERVER_URL;
const ACCESS_TOKEN = process.env.MATRIX_BOT_TOKEN;
const CONTROL_ROOM_ID = process.env.MATRIX_CONTROL_ROOM_ID;
const GITEA_URL = process.env.GITEA_URL;
// "!claude owner/repo do the thing" — everything after the repo slug is the instruction.
function parseCommand(text) {
const match = text.match(/^!claude\s+([^\s/]+\/[^\s/]+)\s+(.+)$/s);
if (!match) return null;
const [, repoFullName, instruction] = match;
return { repoFullName, instruction: instruction.trim() };
}
export async function startMatrixBot() {
if (!HOMESERVER_URL || !ACCESS_TOKEN || !CONTROL_ROOM_ID) {
console.warn("Matrix env vars not set — skipping bot startup");
return;
}
const storage = new SimpleFsStorageProvider("/workspace/matrix-bot-storage.json");
const client = new MatrixClient(HOMESERVER_URL, ACCESS_TOKEN, storage);
AutojoinRoomsMixin.setupOnClient(client);
const selfUserId = await client.getUserId();
client.on("room.message", async (roomId, event) => {
// Invite-only control room enforces who can reach the bot at all; this just
// scopes command handling to that one room and ignores the bot's own messages.
if (roomId !== CONTROL_ROOM_ID) return;
if (event.sender === selfUserId) return;
const body = event.content?.body;
if (!body) return;
const cmd = parseCommand(body);
if (!cmd) return;
const [owner, repo] = cmd.repoFullName.split("/");
await client.sendText(roomId, `Working on it: ${cmd.repoFullName}${cmd.instruction}`);
try {
const cloneUrl = `${GITEA_URL}/${owner}/${repo}.git`;
const pr = await runChatTask({ owner, repo, cloneUrl, instruction: cmd.instruction });
await client.sendText(roomId, `Opened PR: ${pr.html_url}`);
} catch (err) {
console.error("chat task failed", err);
await client.sendText(roomId, `Failed: ${err.message}`);
}
});
await client.start();
console.log("Matrix bot started, room:", CONTROL_ROOM_ID);
}