feat: add Tower Log narrative event feed (Fixes #7)
Some checks failed
CI / Typecheck & Lint (pull_request) Failing after 0s
Some checks failed
CI / Typecheck & Lint (pull_request) Failing after 0s
Adds the tower_log DB table, a narrateEvent method on AgentService (Haiku-powered, stub-safe), a tower-log service that persists and broadcasts entries, a GET /api/tower-log REST endpoint, WebSocket bootstrap and real-time push, and a bottom-sheet Tower Log panel in the-matrix UI with fade-in animations and auto-scroll. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -442,6 +442,89 @@ Respond ONLY with valid JSON: {"accepted": true/false, "reason": "..."}`,
|
||||
return "";
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Generate a short narrative entry for the Tower Log.
|
||||
* Uses Haiku (evalModel) with Timmy's wizardly persona.
|
||||
* Returns a single sentence under 100 characters.
|
||||
*
|
||||
* In STUB_MODE returns a canned narrative so the full flow
|
||||
* can be exercised without an Anthropic API key.
|
||||
*/
|
||||
async narrateEvent(eventType: string, context?: string): Promise<string> {
|
||||
const STUB_NARRATIVES: Record<string, string[]> = {
|
||||
"job:complete": [
|
||||
"Timmy conjures a brilliant solution, weaving lightning and wisdom.",
|
||||
"Another quest fulfilled — the Workshop hums with quiet satisfaction.",
|
||||
"The crystal ball glows as Timmy delivers yet another worthy result.",
|
||||
],
|
||||
"job:rejected": [
|
||||
"With gentle wisdom, Timmy declines — not all quests suit the Workshop.",
|
||||
"The Beta oracle speaks: this path shall not be walked today.",
|
||||
],
|
||||
"job:failed": [
|
||||
"The arcane machinery sputters — a job falters in the ether.",
|
||||
"Even wizards face setbacks; Timmy regroups and stands ready.",
|
||||
],
|
||||
"visitor:enter": [
|
||||
"A new traveler arrives, drawn by the Workshop's lightning glow.",
|
||||
"The Workshop doors swing open to welcome another seeker.",
|
||||
"Another soul finds the Workshop, guided by satoshi starlight.",
|
||||
],
|
||||
"visitor:leave": [
|
||||
"A visitor departs, carrying a spark of the Workshop's magic.",
|
||||
"The door closes softly — one more seeker returns to the world.",
|
||||
],
|
||||
"payment:eval": [
|
||||
"⚡ Lightning strikes — eval fee confirmed, wisdom unlocked.",
|
||||
"Sats flow in; the Workshop's scales tip toward action.",
|
||||
],
|
||||
"payment:work": [
|
||||
"⚡ Work payment confirmed — Gamma stirs to weave the answer.",
|
||||
"The Lightning Network delivers; Timmy's full power is unleashed.",
|
||||
],
|
||||
};
|
||||
|
||||
const candidates = STUB_NARRATIVES[eventType]
|
||||
?? ["The Workshop stirs with quiet, purposeful magic."];
|
||||
|
||||
if (STUB_MODE) {
|
||||
return candidates[Math.floor(Math.random() * candidates.length)]!;
|
||||
}
|
||||
|
||||
const EVENT_CONTEXT: Record<string, string> = {
|
||||
"job:complete": "A visitor's paid job completed successfully in Timmy's Workshop.",
|
||||
"job:rejected": "A visitor's job request was rejected after AI evaluation.",
|
||||
"job:failed": "A job failed unexpectedly in the Workshop.",
|
||||
"visitor:enter": "A new visitor just entered Timmy's Workshop.",
|
||||
"visitor:leave": "A visitor just left Timmy's Workshop.",
|
||||
"payment:eval": "A visitor paid the evaluation fee via Lightning.",
|
||||
"payment:work": "A visitor paid the work fee via Lightning, unlocking execution.",
|
||||
};
|
||||
|
||||
const baseContext = EVENT_CONTEXT[eventType] ?? "Something noteworthy happened in the Workshop.";
|
||||
const fullContext = context ? `${baseContext} ${context}` : baseContext;
|
||||
|
||||
try {
|
||||
const client = await getClient();
|
||||
const message = await client.messages.create({
|
||||
model: this.evalModel, // Haiku — cheap and fast
|
||||
max_tokens: 80,
|
||||
system: `You are the chronicler of Timmy's Workshop — a mystical tower powered by Bitcoin Lightning where an AI wizard named Timmy fulfills paid quests for visitors.
|
||||
Write a single vivid sentence (strictly under 100 characters) narrating what just happened.
|
||||
Style: wizardly, warm, slightly epic. Present tense. No quotes. No hashtags.`,
|
||||
messages: [{ role: "user", content: `Narrate this event: ${fullContext}` }],
|
||||
});
|
||||
const block = message.content[0];
|
||||
if (block?.type === "text") {
|
||||
const text = block.text!.trim().replace(/^["']|["']$/g, "");
|
||||
return text.slice(0, 120); // hard cap
|
||||
}
|
||||
return candidates[0]!;
|
||||
} catch (err) {
|
||||
logger.warn("narrateEvent failed", { eventType, err: String(err) });
|
||||
return candidates[Math.floor(Math.random() * candidates.length)]!;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export const agentService = new AgentService();
|
||||
|
||||
@@ -21,7 +21,10 @@ export type CostEvent =
|
||||
export type CommentaryEvent =
|
||||
| { type: "agent_commentary"; agentId: string; jobId: string; text: string };
|
||||
|
||||
export type BusEvent = JobEvent | SessionEvent | DebateEvent | CostEvent | CommentaryEvent;
|
||||
export type TowerLogEvent =
|
||||
| { type: "tower_log:entry"; id: string; narrative: string; eventType: string; agentId: string | null; jobId: string | null; createdAt: string };
|
||||
|
||||
export type BusEvent = JobEvent | SessionEvent | DebateEvent | CostEvent | CommentaryEvent | TowerLogEvent;
|
||||
|
||||
class EventBus extends EventEmitter {
|
||||
emit(event: "bus", data: BusEvent): boolean;
|
||||
|
||||
74
artifacts/api-server/src/lib/tower-log.ts
Normal file
74
artifacts/api-server/src/lib/tower-log.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* Tower Log — narrative event feed (#7).
|
||||
*
|
||||
* Generates a prose narrative entry via Haiku whenever a key Workshop event
|
||||
* occurs, persists it to the tower_log DB table, and emits it on the eventBus
|
||||
* so connected WebSocket clients receive it in real time.
|
||||
*/
|
||||
|
||||
import { randomUUID } from "crypto";
|
||||
import { db, towerLog } from "@workspace/db";
|
||||
import { desc } from "drizzle-orm";
|
||||
import { eventBus } from "./event-bus.js";
|
||||
import { agentService } from "./agent.js";
|
||||
import { makeLogger } from "./logger.js";
|
||||
|
||||
const logger = makeLogger("tower-log");
|
||||
|
||||
export interface TowerLogRow {
|
||||
id: string;
|
||||
narrative: string;
|
||||
eventType: string;
|
||||
agentId: string | null;
|
||||
jobId: string | null;
|
||||
createdAt: Date;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a narrative entry, persist it, and broadcast it via eventBus.
|
||||
* Non-fatal — errors are logged but never thrown.
|
||||
*/
|
||||
export async function addTowerLogEntry(
|
||||
eventType: string,
|
||||
context?: string,
|
||||
agentId?: string,
|
||||
jobId?: string,
|
||||
): Promise<void> {
|
||||
try {
|
||||
const narrative = await agentService.narrateEvent(eventType, context);
|
||||
const id = randomUUID();
|
||||
|
||||
await db.insert(towerLog).values({
|
||||
id,
|
||||
narrative,
|
||||
eventType,
|
||||
agentId: agentId ?? null,
|
||||
jobId: jobId ?? null,
|
||||
});
|
||||
|
||||
// Broadcast to connected WS clients
|
||||
eventBus.publish({
|
||||
type: "tower_log:entry",
|
||||
id,
|
||||
narrative,
|
||||
eventType,
|
||||
agentId: agentId ?? null,
|
||||
jobId: jobId ?? null,
|
||||
createdAt: new Date().toISOString(),
|
||||
});
|
||||
} catch (err) {
|
||||
logger.warn("addTowerLogEntry failed", { eventType, err: String(err) });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the most recent N entries from the DB, oldest-first.
|
||||
*/
|
||||
export async function getRecentTowerLog(limit = 20): Promise<TowerLogRow[]> {
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(towerLog)
|
||||
.orderBy(desc(towerLog.createdAt))
|
||||
.limit(limit);
|
||||
return rows.reverse();
|
||||
}
|
||||
@@ -32,6 +32,7 @@ import { eventBus, type BusEvent } from "../lib/event-bus.js";
|
||||
import { makeLogger } from "../lib/logger.js";
|
||||
import { getWorldState, setAgentStateInWorld } from "../lib/world-state.js";
|
||||
import { agentService } from "../lib/agent.js";
|
||||
import { addTowerLogEntry, getRecentTowerLog } from "../lib/tower-log.js";
|
||||
import { db, worldEvents } from "@workspace/db";
|
||||
|
||||
const logger = makeLogger("ws-events");
|
||||
@@ -269,6 +270,18 @@ function translateEvent(ev: BusEvent): object | null {
|
||||
text: ev.text,
|
||||
};
|
||||
|
||||
// ── Tower Log (#7) ────────────────────────────────────────────────────────
|
||||
case "tower_log:entry":
|
||||
return {
|
||||
type: "tower_log_entry",
|
||||
id: ev.id,
|
||||
narrative: ev.narrative,
|
||||
eventType: ev.eventType,
|
||||
agentId: ev.agentId,
|
||||
jobId: ev.jobId,
|
||||
createdAt: ev.createdAt,
|
||||
};
|
||||
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
@@ -306,6 +319,17 @@ async function sendWorldStateBootstrap(socket: WebSocket): Promise<void> {
|
||||
} catch {
|
||||
send(socket, { type: "world_state", ...getWorldState(), recentEvents: [] });
|
||||
}
|
||||
|
||||
// Send recent tower log entries
|
||||
try {
|
||||
const logEntries = await getRecentTowerLog(20);
|
||||
send(socket, {
|
||||
type: "tower_log_history",
|
||||
entries: logEntries,
|
||||
});
|
||||
} catch {
|
||||
/* non-fatal */
|
||||
}
|
||||
}
|
||||
|
||||
export function attachWebSocketServer(server: Server): void {
|
||||
@@ -338,6 +362,7 @@ export function attachWebSocketServer(server: Server): void {
|
||||
const formattedNpub = `${npub.slice(0, 8)}…${npub.slice(-4)}`;
|
||||
broadcastToAll(wss, { type: "chat", agentId: "timmy", text: `Welcome, Nostr user ${formattedNpub}! What can I help you with?` });
|
||||
}
|
||||
void addTowerLogEntry("visitor:enter", undefined, "timmy");
|
||||
|
||||
wss.clients.forEach(c => {
|
||||
if (c !== socket && c.readyState === 1) {
|
||||
@@ -437,13 +462,22 @@ export function attachWebSocketServer(server: Server): void {
|
||||
agentId = "gamma"; phase = "starting";
|
||||
} else if (ev.state === "complete") {
|
||||
agentId = "alpha"; phase = "complete";
|
||||
void addTowerLogEntry("job:complete", undefined, "alpha", ev.jobId);
|
||||
} else if (ev.state === "rejected") {
|
||||
agentId = "alpha"; phase = "rejected";
|
||||
void addTowerLogEntry("job:rejected", undefined, "beta", ev.jobId);
|
||||
} else if (ev.state === "failed") {
|
||||
void addTowerLogEntry("job:failed", undefined, "alpha", ev.jobId);
|
||||
}
|
||||
} else if (ev.type === "job:paid") {
|
||||
jobId = ev.jobId;
|
||||
agentId = "delta";
|
||||
phase = ev.invoiceType === "eval" ? "eval_paid" : "work_paid";
|
||||
if (ev.invoiceType === "eval") {
|
||||
void addTowerLogEntry("payment:eval", undefined, "delta", ev.jobId);
|
||||
} else if (ev.invoiceType === "work") {
|
||||
void addTowerLogEntry("payment:work", undefined, "delta", ev.jobId);
|
||||
}
|
||||
}
|
||||
|
||||
if (agentId && phase && jobId) {
|
||||
|
||||
@@ -18,6 +18,7 @@ import adminRelayRouter from "./admin-relay.js";
|
||||
import adminRelayQueueRouter from "./admin-relay-queue.js";
|
||||
import geminiRouter from "./gemini.js";
|
||||
import statsRouter from "./stats.js";
|
||||
import towerLogRouter from "./tower-log.js";
|
||||
|
||||
const router: IRouter = Router();
|
||||
|
||||
@@ -33,6 +34,7 @@ router.use(relayRouter);
|
||||
router.use(adminRelayRouter);
|
||||
router.use(adminRelayQueueRouter);
|
||||
router.use(demoRouter);
|
||||
router.use(towerLogRouter);
|
||||
router.use("/gemini", geminiRouter);
|
||||
router.use(testkitRouter);
|
||||
router.use(uiRouter);
|
||||
|
||||
21
artifacts/api-server/src/routes/tower-log.ts
Normal file
21
artifacts/api-server/src/routes/tower-log.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { Router, type Request, type Response } from "express";
|
||||
import { getRecentTowerLog } from "../lib/tower-log.js";
|
||||
import { makeLogger } from "../lib/logger.js";
|
||||
|
||||
const logger = makeLogger("tower-log-route");
|
||||
const router = Router();
|
||||
|
||||
/**
|
||||
* GET /api/tower-log — return the 20 most recent narrative entries, oldest first.
|
||||
*/
|
||||
router.get("/tower-log", async (_req: Request, res: Response) => {
|
||||
try {
|
||||
const entries = await getRecentTowerLog(20);
|
||||
res.json({ entries });
|
||||
} catch (err) {
|
||||
logger.error("GET /api/tower-log failed", { error: String(err) });
|
||||
res.status(500).json({ error: "tower_log_error" });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
Reference in New Issue
Block a user