Compare commits
1 Commits
claude/iss
...
claude/iss
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c2f2cfe3ea |
@@ -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,11 +21,10 @@ export type CostEvent =
|
||||
export type CommentaryEvent =
|
||||
| { type: "agent_commentary"; agentId: string; jobId: string; text: string };
|
||||
|
||||
// External agent state changes (e.g. Kimi, Perplexity picking up or completing tasks)
|
||||
export type AgentExternalEvent =
|
||||
| { type: "agent:external_state"; agentId: string; state: string; taskSummary?: string };
|
||||
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 | AgentExternalEvent;
|
||||
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();
|
||||
}
|
||||
@@ -16,7 +16,7 @@ const DEFAULT_TIMMY: TimmyState = {
|
||||
|
||||
const _state: WorldState = {
|
||||
timmyState: { ...DEFAULT_TIMMY },
|
||||
agentStates: { alpha: "idle", beta: "idle", gamma: "idle", delta: "idle", kimi: "idle", perplexity: "idle" },
|
||||
agentStates: { alpha: "idle", beta: "idle", gamma: "idle", delta: "idle" },
|
||||
updatedAt: new Date().toISOString(),
|
||||
};
|
||||
|
||||
@@ -34,10 +34,8 @@ export function setAgentStateInWorld(agentId: string, agentState: string): void
|
||||
_deriveTimmy();
|
||||
}
|
||||
|
||||
const WORKSHOP_AGENTS = ["alpha", "beta", "gamma", "delta"];
|
||||
|
||||
function _deriveTimmy(): void {
|
||||
const states = WORKSHOP_AGENTS.map(id => _state.agentStates[id] ?? "idle");
|
||||
const states = Object.values(_state.agentStates);
|
||||
if (states.includes("working")) {
|
||||
_state.timmyState.activity = "working";
|
||||
_state.timmyState.mood = "focused";
|
||||
|
||||
@@ -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,20 +270,17 @@ function translateEvent(ev: BusEvent): object | null {
|
||||
text: ev.text,
|
||||
};
|
||||
|
||||
// ── External agent state (Kimi, Perplexity) (#11) ─────────────────────────
|
||||
case "agent:external_state": {
|
||||
updateAgentWorld(ev.agentId, ev.state);
|
||||
void logWorldEvent(
|
||||
`agent:${ev.state}`,
|
||||
`${ev.agentId} is now ${ev.state}${ev.taskSummary ? `: ${ev.taskSummary.slice(0, 80)}` : ""}`,
|
||||
ev.agentId,
|
||||
);
|
||||
const msgs: object[] = [{ type: "agent_state", agentId: ev.agentId, state: ev.state }];
|
||||
if (ev.taskSummary) {
|
||||
msgs.push({ type: "agent_task_summary", agentId: ev.agentId, summary: ev.taskSummary });
|
||||
}
|
||||
return msgs;
|
||||
}
|
||||
// ── 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;
|
||||
@@ -321,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 {
|
||||
@@ -353,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) {
|
||||
@@ -452,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;
|
||||
15
lib/db/migrations/0010_tower_log.sql
Normal file
15
lib/db/migrations/0010_tower_log.sql
Normal file
@@ -0,0 +1,15 @@
|
||||
-- Migration: Tower Log narrative event feed (#7)
|
||||
-- Adds the tower_log table that stores prose narrative entries about
|
||||
-- Workshop activity, generated by Haiku on key events.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tower_log (
|
||||
id TEXT PRIMARY KEY,
|
||||
narrative TEXT NOT NULL,
|
||||
event_type TEXT NOT NULL,
|
||||
agent_id TEXT,
|
||||
job_id TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_tower_log_created_at
|
||||
ON tower_log(created_at DESC);
|
||||
@@ -14,3 +14,4 @@ export * from "./relay-accounts";
|
||||
export * from "./relay-event-queue";
|
||||
export * from "./job-debates";
|
||||
export * from "./session-messages";
|
||||
export * from "./tower-log";
|
||||
|
||||
10
lib/db/src/schema/tower-log.ts
Normal file
10
lib/db/src/schema/tower-log.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { pgTable, text, timestamp } from "drizzle-orm/pg-core";
|
||||
|
||||
export const towerLog = pgTable("tower_log", {
|
||||
id: text("id").primaryKey(),
|
||||
narrative: text("narrative").notNull(),
|
||||
eventType: text("event_type").notNull(),
|
||||
agentId: text("agent_id"),
|
||||
jobId: text("job_id"),
|
||||
createdAt: timestamp("created_at").notNull().defaultNow(),
|
||||
});
|
||||
@@ -702,6 +702,85 @@
|
||||
padding: 12px; margin: 0;
|
||||
max-height: 400px; overflow-y: auto;
|
||||
}
|
||||
|
||||
/* ── Tower Log button ────────────────────────────────────────────── */
|
||||
#open-tower-log-btn {
|
||||
font-family: 'Courier New', monospace; font-size: 11px; font-weight: bold;
|
||||
color: #ccaaff; background: rgba(25, 10, 45, 0.85); border: 1px solid #663399;
|
||||
padding: 7px 18px; cursor: pointer; letter-spacing: 2px;
|
||||
box-shadow: 0 0 14px #44116622;
|
||||
transition: background 0.15s, box-shadow 0.15s, color 0.15s;
|
||||
border-radius: 2px;
|
||||
min-height: 36px;
|
||||
}
|
||||
#open-tower-log-btn:hover, #open-tower-log-btn:active {
|
||||
background: rgba(45, 18, 80, 0.95);
|
||||
box-shadow: 0 0 20px #55228844;
|
||||
color: #eeddff;
|
||||
}
|
||||
|
||||
/* ── Tower Log panel (bottom sheet) ─────────────────────────────── */
|
||||
#tower-log-panel {
|
||||
position: fixed; bottom: -100%; left: 0; right: 0;
|
||||
height: 65vh;
|
||||
background: rgba(6, 3, 14, 0.97);
|
||||
border-top: 1px solid #2a1040;
|
||||
z-index: 100;
|
||||
font-family: 'Courier New', monospace;
|
||||
transition: bottom 0.35s cubic-bezier(0.4, 0, 0.2, 1);
|
||||
box-shadow: 0 -8px 32px rgba(80, 30, 130, 0.18);
|
||||
display: flex; flex-direction: column;
|
||||
}
|
||||
#tower-log-panel.open { bottom: 60px; }
|
||||
|
||||
.tlog-header {
|
||||
display: flex; align-items: center; gap: 8px;
|
||||
padding: 14px 20px 10px;
|
||||
border-bottom: 1px solid #2a1040;
|
||||
font-size: 12px; letter-spacing: 3px; color: #9966cc;
|
||||
flex-shrink: 0;
|
||||
text-shadow: 0 0 8px #66228866;
|
||||
}
|
||||
.tlog-header span { flex: 1; }
|
||||
#tower-log-close {
|
||||
background: transparent; border: 1px solid #2a1040;
|
||||
color: #664488; font-family: 'Courier New', monospace;
|
||||
font-size: 14px; padding: 3px 8px; cursor: pointer;
|
||||
transition: color 0.2s, border-color 0.2s; border-radius: 2px;
|
||||
}
|
||||
#tower-log-close:hover { color: #bb88ff; border-color: #8844bb; }
|
||||
|
||||
#tower-log-list {
|
||||
flex: 1; overflow-y: auto; padding: 12px 20px;
|
||||
overscroll-behavior: contain;
|
||||
}
|
||||
|
||||
.tlog-empty {
|
||||
color: #44224466; font-size: 11px; letter-spacing: 1px;
|
||||
line-height: 1.8; text-align: center;
|
||||
margin-top: 40px; padding: 0 20px;
|
||||
}
|
||||
|
||||
.tlog-entry {
|
||||
padding: 8px 0;
|
||||
border-bottom: 1px solid #1a0a2a;
|
||||
display: flex; gap: 10px; align-items: baseline;
|
||||
animation: tlog-fade-in 0.4s ease-out;
|
||||
}
|
||||
.tlog-entry:last-child { border-bottom: none; }
|
||||
@keyframes tlog-fade-in {
|
||||
from { opacity: 0; transform: translateY(4px); }
|
||||
to { opacity: 1; transform: translateY(0); }
|
||||
}
|
||||
.tlog-time {
|
||||
font-size: 9px; color: #443355; letter-spacing: 0.5px;
|
||||
white-space: nowrap; flex-shrink: 0; min-width: 48px;
|
||||
}
|
||||
.tlog-text {
|
||||
font-size: 11px; color: #bb99dd; line-height: 1.5;
|
||||
letter-spacing: 0.3px;
|
||||
}
|
||||
.tlog-new { color: #ddbbff; text-shadow: 0 0 6px #9944cc44; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
@@ -744,6 +823,7 @@
|
||||
<button id="open-panel-btn">⚡ SUBMIT JOB</button>
|
||||
<button id="open-session-btn">⚡ FUND SESSION</button>
|
||||
<button id="open-history-btn">⏱ HISTORY</button>
|
||||
<button id="open-tower-log-btn">📜 TOWER LOG</button>
|
||||
<a id="relay-admin-btn" href="/admin/relay">⚙ RELAY ADMIN</a>
|
||||
</div>
|
||||
|
||||
@@ -923,6 +1003,17 @@
|
||||
<span class="recovery-text">GPU context lost — recovering...</span>
|
||||
</div>
|
||||
|
||||
<!-- ── Tower Log panel (bottom sheet) ────────────────────────────── -->
|
||||
<div id="tower-log-panel">
|
||||
<div class="tlog-header">
|
||||
<span>📜 TOWER LOG</span>
|
||||
<button id="tower-log-close">✕</button>
|
||||
</div>
|
||||
<div id="tower-log-list">
|
||||
<div class="tlog-empty" id="tower-log-empty">The chronicle awaits… events will appear here as Timmy works his magic.</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// Show Relay Admin button if admin token is stored in localStorage
|
||||
(function() {
|
||||
|
||||
@@ -5,27 +5,18 @@
|
||||
* unused (x, z) position. No other file needs to be edited.
|
||||
*
|
||||
* Fields:
|
||||
* id — unique string key used in WebSocket messages and state maps
|
||||
* label — display name shown in the 3D HUD and chat panel
|
||||
* color — hex integer (0xRRGGBB) used for Three.js materials and lights
|
||||
* role — human-readable role string shown under the label sprite
|
||||
* specialization — optional capability description shown in agent inspect card
|
||||
* direction — cardinal facing direction (for future mesh orientation use)
|
||||
* x, z — world-space position on the horizontal plane (y is always 0)
|
||||
* id — unique string key used in WebSocket messages and state maps
|
||||
* label — display name shown in the 3D HUD and chat panel
|
||||
* color — hex integer (0xRRGGBB) used for Three.js materials and lights
|
||||
* role — human-readable role string shown under the label sprite
|
||||
* direction — cardinal facing direction (for future mesh orientation use)
|
||||
* x, z — world-space position on the horizontal plane (y is always 0)
|
||||
*/
|
||||
export const AGENT_DEFS = [
|
||||
{ id: 'alpha', label: 'ALPHA', color: 0x00ff88, role: 'orchestrator', direction: 'north', x: 0, z: -6 },
|
||||
{ id: 'beta', label: 'BETA', color: 0x00aaff, role: 'worker', direction: 'east', x: 6, z: 0 },
|
||||
{ id: 'gamma', label: 'GAMMA', color: 0xff6600, role: 'validator', direction: 'south', x: 0, z: 6 },
|
||||
{ id: 'delta', label: 'DELTA', color: 0xaa00ff, role: 'monitor', direction: 'west', x: -6, z: 0 },
|
||||
{
|
||||
id: 'kimi', label: 'KIMI', color: 0x00d4ff, role: 'analyst',
|
||||
specialization: 'Long Context Analysis', direction: 'northwest', x: -10, z: -10,
|
||||
},
|
||||
{
|
||||
id: 'perplexity', label: 'PERPLEXITY', color: 0xff6b9d, role: 'researcher',
|
||||
specialization: 'Real-time Research', direction: 'northeast', x: 10, z: -10,
|
||||
},
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,13 +7,10 @@ const CRYSTAL_POS = new THREE.Vector3(0.6, 1.15, -4.1);
|
||||
|
||||
const agentStates = Object.fromEntries(AGENT_DEFS.map(d => [d.id, 'idle']));
|
||||
|
||||
// Workshop agents that drive Timmy's mood (excludes external agents Kimi/Perplexity)
|
||||
const WORKSHOP_AGENT_IDS = ['alpha', 'beta', 'gamma', 'delta'];
|
||||
|
||||
function deriveTimmyState() {
|
||||
if (agentStates.gamma === 'working') return 'working';
|
||||
if (agentStates.beta === 'thinking' || agentStates.alpha === 'thinking') return 'thinking';
|
||||
if (WORKSHOP_AGENT_IDS.some(id => agentStates[id] !== 'idle')) return 'active';
|
||||
if (Object.values(agentStates).some(s => s !== 'idle')) return 'active';
|
||||
return 'idle';
|
||||
}
|
||||
|
||||
@@ -100,108 +97,9 @@ function _pickMouthGeo(smileAmount) {
|
||||
|
||||
// ── Build Timmy ───────────────────────────────────────────────────────────────
|
||||
|
||||
// ── External agent bodies (Kimi, Perplexity) ──────────────────────────────────
|
||||
const _extBodies = {};
|
||||
|
||||
export function initAgents(sceneRef) {
|
||||
scene = sceneRef;
|
||||
timmy = buildTimmy(scene);
|
||||
_initKimiBody(scene);
|
||||
_initPerplexityBody(scene);
|
||||
}
|
||||
|
||||
function _initKimiBody(sc) {
|
||||
const group = new THREE.Group();
|
||||
group.position.set(-10, 1.2, -10);
|
||||
|
||||
const mat = new THREE.MeshStandardMaterial({
|
||||
color: 0x00d4ff, emissive: 0x004466, emissiveIntensity: 0.4,
|
||||
roughness: 0.15, metalness: 0.4,
|
||||
});
|
||||
const core = new THREE.Mesh(new THREE.OctahedronGeometry(0.38, 0), mat);
|
||||
group.add(core);
|
||||
|
||||
const ringMat = new THREE.MeshStandardMaterial({
|
||||
color: 0x00d4ff, emissive: 0x0088aa, emissiveIntensity: 0.6,
|
||||
roughness: 0.1, metalness: 0.6, transparent: true, opacity: 0.7,
|
||||
});
|
||||
const ring1 = new THREE.Mesh(new THREE.TorusGeometry(0.60, 0.025, 6, 32), ringMat);
|
||||
ring1.rotation.x = Math.PI / 3;
|
||||
group.add(ring1);
|
||||
|
||||
const ring2 = new THREE.Mesh(new THREE.TorusGeometry(0.76, 0.018, 6, 32), ringMat.clone());
|
||||
ring2.rotation.x = Math.PI / 2;
|
||||
ring2.rotation.z = Math.PI / 4;
|
||||
group.add(ring2);
|
||||
|
||||
const light = new THREE.PointLight(0x00d4ff, 0.5, 8);
|
||||
group.add(light);
|
||||
|
||||
sc.add(group);
|
||||
_extBodies.kimi = { group, core, ring1, ring2, light, mat, pulsePhase: Math.random() * Math.PI * 2 };
|
||||
}
|
||||
|
||||
function _initPerplexityBody(sc) {
|
||||
const group = new THREE.Group();
|
||||
group.position.set(10, 1.2, -10);
|
||||
|
||||
const mat = new THREE.MeshStandardMaterial({
|
||||
color: 0xff6b9d, emissive: 0x660033, emissiveIntensity: 0.4,
|
||||
roughness: 0.2, metalness: 0.3,
|
||||
});
|
||||
const core = new THREE.Mesh(new THREE.IcosahedronGeometry(0.32, 0), mat);
|
||||
group.add(core);
|
||||
|
||||
const scanMat = new THREE.MeshStandardMaterial({
|
||||
color: 0xff6b9d, emissive: 0xaa2255, emissiveIntensity: 0.7,
|
||||
roughness: 0.1, metalness: 0.5, transparent: true, opacity: 0.65,
|
||||
});
|
||||
const scanRings = [0, Math.PI / 3, -Math.PI / 3].map(angle => {
|
||||
const r = new THREE.Mesh(new THREE.TorusGeometry(0.55, 0.022, 6, 28), scanMat.clone());
|
||||
r.rotation.x = Math.PI / 2 + angle;
|
||||
r.rotation.z = angle * 0.5;
|
||||
group.add(r);
|
||||
return r;
|
||||
});
|
||||
|
||||
const light = new THREE.PointLight(0xff6b9d, 0.5, 8);
|
||||
group.add(light);
|
||||
|
||||
sc.add(group);
|
||||
_extBodies.perplexity = { group, core, scanRings, light, mat, pulsePhase: Math.random() * Math.PI * 2 };
|
||||
}
|
||||
|
||||
function _updateExtBodies(t) {
|
||||
_updateExtBody('kimi', t);
|
||||
_updateExtBody('perplexity', t);
|
||||
}
|
||||
|
||||
function _updateExtBody(id, t) {
|
||||
const body = _extBodies[id];
|
||||
if (!body) return;
|
||||
const state = agentStates[id] || 'idle';
|
||||
const isActive = state === 'working' || state === 'active';
|
||||
const isThinking = state === 'thinking';
|
||||
|
||||
const speedMult = isActive ? 2.5 : isThinking ? 1.5 : 0.6;
|
||||
const emissI = isActive ? 1.2 : isThinking ? 0.7 : 0.25;
|
||||
const lightI = isActive ? 1.2 : isThinking ? 0.6 : 0.2;
|
||||
const bobAmp = isActive ? 0.10 : 0.04;
|
||||
|
||||
body.group.position.y = 1.2 + Math.sin(t * 0.0008 + body.pulsePhase) * bobAmp;
|
||||
body.mat.emissiveIntensity = emissI;
|
||||
body.light.intensity = lightI;
|
||||
|
||||
if (id === 'kimi') {
|
||||
body.core.rotation.y += 0.008 * speedMult;
|
||||
body.core.rotation.x += 0.003 * speedMult;
|
||||
body.ring1.rotation.z += 0.012 * speedMult;
|
||||
body.ring2.rotation.x += 0.007 * speedMult;
|
||||
} else {
|
||||
body.core.rotation.y += 0.006 * speedMult;
|
||||
body.core.rotation.z += 0.009 * speedMult;
|
||||
body.scanRings.forEach((r, i) => { r.rotation.y += (0.015 + i * 0.008) * speedMult; });
|
||||
}
|
||||
}
|
||||
|
||||
function buildTimmy(sc) {
|
||||
@@ -519,7 +417,6 @@ export function updateAgents(time) {
|
||||
const t = time * 0.001;
|
||||
const dt = _lastFrameTime > 0 ? Math.min((time - _lastFrameTime) * 0.001, 0.05) : 0.016;
|
||||
_lastFrameTime = time;
|
||||
_updateExtBodies(time);
|
||||
|
||||
const vs = deriveTimmyState();
|
||||
const pulse = Math.sin(t * 1.8 + timmy.pulsePhase);
|
||||
@@ -992,19 +889,5 @@ export function disposeAgents() {
|
||||
timmy.bubbleTex?.dispose();
|
||||
timmy.bubbleMat?.dispose();
|
||||
timmy = null;
|
||||
|
||||
// Dispose external agent bodies
|
||||
for (const body of Object.values(_extBodies)) {
|
||||
body.group.traverse(obj => {
|
||||
if (obj.geometry) obj.geometry.dispose();
|
||||
if (obj.material) {
|
||||
const mats = Array.isArray(obj.material) ? obj.material : [obj.material];
|
||||
mats.forEach(m => m.dispose());
|
||||
}
|
||||
});
|
||||
if (scene) scene.remove(body.group);
|
||||
}
|
||||
for (const k of Object.keys(_extBodies)) delete _extBodies[k];
|
||||
|
||||
scene = null;
|
||||
}
|
||||
|
||||
@@ -12,12 +12,7 @@
|
||||
*/
|
||||
|
||||
import * as THREE from 'three';
|
||||
import { colorToCss, AGENT_DEFS } from './agent-defs.js';
|
||||
|
||||
// Specialization lookup built once from AGENT_DEFS
|
||||
const _specializations = Object.fromEntries(
|
||||
AGENT_DEFS.filter(d => d.specialization).map(d => [d.id, d.specialization])
|
||||
);
|
||||
import { colorToCss } from './agent-defs.js';
|
||||
|
||||
const _proj = new THREE.Vector3();
|
||||
let _camera = null;
|
||||
@@ -25,7 +20,6 @@ let _labels = []; // { el, worldPos: THREE.Vector3, id }
|
||||
|
||||
// ── State cache (updated from WS) ────────────────────────────────────────────
|
||||
const _states = {};
|
||||
const _lastTasks = {};
|
||||
|
||||
// ── Inspect popup ─────────────────────────────────────────────────────────────
|
||||
let _inspectEl = null;
|
||||
@@ -106,10 +100,6 @@ function _makeLabel(container, id, name, role, color, worldPos) {
|
||||
return { el, worldPos, id, color };
|
||||
}
|
||||
|
||||
export function setLabelLastTask(id, summary) {
|
||||
_lastTasks[id] = summary;
|
||||
}
|
||||
|
||||
export function setLabelState(id, state) {
|
||||
_states[id] = state;
|
||||
const entry = _labels.find(l => l.id === id);
|
||||
@@ -128,17 +118,13 @@ export function showInspectPopup(id, screenX, screenY) {
|
||||
|
||||
const state = _states[id] || 'idle';
|
||||
const uptime = Math.floor(performance.now() / 1000);
|
||||
const spec = _specializations[id];
|
||||
const lastTask = _lastTasks[id];
|
||||
_inspectEl.innerHTML = `
|
||||
<div style="color:${entry.color};font-weight:bold;letter-spacing:2px;font-size:12px;margin-bottom:6px;">
|
||||
${id.toUpperCase()}
|
||||
</div>
|
||||
${spec ? `<div style="color:${entry.color}99;margin-bottom:4px;font-size:10px;letter-spacing:1px;">⬡ ${spec}</div>` : ''}
|
||||
<div style="color:#aaa;margin-bottom:2px;">state : <span style="color:${entry.color}">${state}</span></div>
|
||||
<div style="color:#aaa;margin-bottom:2px;">uptime : ${uptime}s</div>
|
||||
<div style="color:#aaa;margin-bottom:2px;">network: <span style="color:#44ff88">connected</span></div>
|
||||
${lastTask ? `<div style="color:#888;font-size:9px;margin-top:4px;border-top:1px solid #333;padding-top:4px;">last: ${lastTask.slice(0, 60)}</div>` : ''}
|
||||
<div style="color:#aaa;">network: <span style="color:#44ff88">connected</span></div>
|
||||
`;
|
||||
_inspectEl.style.left = `${screenX}px`;
|
||||
_inspectEl.style.top = `${screenY}px`;
|
||||
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
import { initEffects, updateEffects, disposeEffects, updateJobIndicators } from './effects.js';
|
||||
import { initUI, updateUI } from './ui.js';
|
||||
import { initInteraction, disposeInteraction, registerSlapTarget } from './interaction.js';
|
||||
import { initWebSocket, getConnectionState, getJobCount } from './websocket.js';
|
||||
import { initWebSocket, getConnectionState, getJobCount, initTowerLog } from './websocket.js';
|
||||
import { initPaymentPanel } from './payment.js';
|
||||
import { initSessionPanel } from './session.js';
|
||||
import { initHistoryPanel } from './history.js';
|
||||
@@ -45,6 +45,7 @@ function buildWorld(firstInit, stateSnapshot) {
|
||||
if (firstInit) {
|
||||
initUI();
|
||||
initWebSocket(scene);
|
||||
initTowerLog();
|
||||
initPaymentPanel();
|
||||
initSessionPanel();
|
||||
initHistoryPanel();
|
||||
|
||||
@@ -3,7 +3,7 @@ import { scene } from './world.js'; // Import the scene
|
||||
import { setAgentState, setSpeechBubble, applyAgentStates, setMood, TIMMY_WORLD_POS } from './agents.js';
|
||||
import { appendSystemMessage, appendDebateMessage, showCostTicker, updateCostTicker } from './ui.js';
|
||||
import { sentiment } from './edge-worker-client.js';
|
||||
import { setLabelState, setLabelLastTask } from './hud-labels.js';
|
||||
import { setLabelState } from './hud-labels.js';
|
||||
import { createJobIndicator, dissolveJobIndicator } from './effects.js';
|
||||
import { getPubkey } from './nostr-identity.js';
|
||||
|
||||
@@ -22,6 +22,7 @@ let jobCount = 0;
|
||||
let reconnectTimer = null;
|
||||
let visitorId = null;
|
||||
const RECONNECT_DELAY_MS = 5000;
|
||||
let _towerLogHistory = [];
|
||||
|
||||
// Map to keep track of active job indicator positions for offsetting
|
||||
const _jobIndicatorOffsets = new Map();
|
||||
@@ -122,19 +123,11 @@ function handleMessage(msg) {
|
||||
break;
|
||||
}
|
||||
|
||||
case 'agent_task_summary': {
|
||||
if (msg.agentId && msg.summary) {
|
||||
setLabelLastTask(msg.agentId, msg.summary);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 'job_completed': {
|
||||
if (jobCount > 0) jobCount--;
|
||||
if (msg.agentId) {
|
||||
setAgentState(msg.agentId, 'idle');
|
||||
setLabelState(msg.agentId, 'idle');
|
||||
setLabelLastTask(msg.agentId, `job ${(msg.jobId || '').slice(0, 8)} completed`);
|
||||
}
|
||||
appendSystemMessage(`job ${(msg.jobId || '').slice(0, 8)} complete`);
|
||||
|
||||
@@ -198,6 +191,23 @@ function handleMessage(msg) {
|
||||
break;
|
||||
}
|
||||
|
||||
case 'tower_log_history': {
|
||||
// Load history when panel opens
|
||||
if (Array.isArray(msg.entries)) {
|
||||
_towerLogHistory = msg.entries;
|
||||
_renderTowerLog();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 'tower_log_entry': {
|
||||
// New entry streamed in real time
|
||||
_towerLogHistory.push(msg);
|
||||
if (_towerLogHistory.length > 20) _towerLogHistory.shift();
|
||||
_renderTowerLog(msg.id); // pass id to highlight new entry
|
||||
break;
|
||||
}
|
||||
|
||||
case 'agent_count':
|
||||
case 'visitor_count':
|
||||
break;
|
||||
@@ -213,3 +223,79 @@ export function sendVisitorMessage(text) {
|
||||
|
||||
export function getConnectionState() { return connectionState; }
|
||||
export function getJobCount() { return jobCount; }
|
||||
|
||||
// ── Tower Log panel ────────────────────────────────────────────────────────
|
||||
|
||||
function _renderTowerLog(newId) {
|
||||
const list = document.getElementById('tower-log-list');
|
||||
const empty = document.getElementById('tower-log-empty');
|
||||
if (!list) return;
|
||||
|
||||
if (_towerLogHistory.length === 0) {
|
||||
if (empty) empty.style.display = 'block';
|
||||
return;
|
||||
}
|
||||
if (empty) empty.style.display = 'none';
|
||||
|
||||
// Remove old entries (keep only the empty placeholder and rebuild)
|
||||
Array.from(list.querySelectorAll('.tlog-entry')).forEach(el => el.remove());
|
||||
|
||||
for (const entry of _towerLogHistory) {
|
||||
const el = document.createElement('div');
|
||||
el.className = 'tlog-entry' + (entry.id === newId ? ' tlog-new' : '');
|
||||
el.dataset.id = entry.id;
|
||||
|
||||
const t = document.createElement('div');
|
||||
t.className = 'tlog-time';
|
||||
const d = new Date(entry.createdAt);
|
||||
t.textContent = d.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
|
||||
|
||||
const n = document.createElement('div');
|
||||
n.className = 'tlog-text';
|
||||
n.textContent = entry.narrative;
|
||||
|
||||
el.appendChild(t);
|
||||
el.appendChild(n);
|
||||
list.appendChild(el);
|
||||
}
|
||||
|
||||
// Auto-scroll to bottom
|
||||
list.scrollTop = list.scrollHeight;
|
||||
|
||||
// Fade new entry highlight after 3s
|
||||
if (newId) {
|
||||
setTimeout(() => {
|
||||
const el = list.querySelector(`[data-id="${newId}"]`);
|
||||
if (el) el.classList.remove('tlog-new');
|
||||
}, 3000);
|
||||
}
|
||||
}
|
||||
|
||||
export function initTowerLog() {
|
||||
const openBtn = document.getElementById('open-tower-log-btn');
|
||||
const panel = document.getElementById('tower-log-panel');
|
||||
const closeBtn = document.getElementById('tower-log-close');
|
||||
if (!openBtn || !panel || !closeBtn) return;
|
||||
|
||||
openBtn.addEventListener('click', () => {
|
||||
panel.classList.add('open');
|
||||
// Fetch history if empty
|
||||
if (_towerLogHistory.length === 0) {
|
||||
fetch('/api/tower-log')
|
||||
.then(r => r.json())
|
||||
.then(data => {
|
||||
if (Array.isArray(data.entries)) {
|
||||
_towerLogHistory = data.entries;
|
||||
_renderTowerLog();
|
||||
}
|
||||
})
|
||||
.catch(() => {});
|
||||
} else {
|
||||
_renderTowerLog();
|
||||
}
|
||||
});
|
||||
|
||||
closeBtn.addEventListener('click', () => {
|
||||
panel.classList.remove('open');
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user