hivedesk 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +24 -0
- package/FIRST-RUN.md +14 -0
- package/PROTOCOL.md +16 -3
- package/README.md +50 -6
- package/assets/illustrations/hive-city.svg +113 -0
- package/assets/video-poster.webp +0 -0
- package/i18n/README.pt-BR.md +44 -6
- package/i18n/de.json +174 -0
- package/i18n/en.json +174 -0
- package/i18n/es.json +174 -0
- package/i18n/fr.json +174 -0
- package/i18n/it.json +174 -0
- package/i18n/ja.json +174 -0
- package/i18n/ko.json +174 -0
- package/i18n/pt-BR.json +174 -0
- package/i18n/ru.json +174 -0
- package/i18n/zh-CN.json +174 -0
- package/package.json +1 -1
- package/src/bus.js +7 -1
- package/src/city/demo.js +75 -0
- package/src/city/hops.js +95 -0
- package/src/city/index.js +1507 -0
- package/src/city/router.js +53 -0
- package/src/city/state.js +313 -0
- package/src/city/store.js +91 -0
- package/src/cli.js +3 -0
- package/src/crew/engine.js +40 -3
- package/src/demo.js +30 -2
- package/src/index.js +23 -2
- package/src/server.js +4 -1
- package/ui/city/city.css +204 -0
- package/ui/city/city.js +933 -0
- package/ui/city/collab.js +64 -0
- package/ui/city/layout.js +142 -0
- package/ui/city/mock.js +358 -0
- package/ui/city/model.js +195 -0
- package/ui/city/scene.js +2033 -0
- package/ui/js/main.js +3 -1
- package/ui/lend/collab/roles.js +8 -2
- package/ui/office/office.js +51 -21
- package/ui/office/scene.js +3 -0
- package/ui/screenshots/city-agent-card-1440.png +0 -0
- package/ui/screenshots/city-agent-card-1920.png +0 -0
- package/ui/screenshots/city-bridge-gpu-borrow-1440.png +0 -0
- package/ui/screenshots/city-bridge-gpu-borrow-1920.png +0 -0
- package/ui/screenshots/city-friend-rising-1440.png +0 -0
- package/ui/screenshots/city-friend-rising-1920.png +0 -0
- package/ui/screenshots/city-overview-1440.png +0 -0
- package/ui/screenshots/city-overview-1920.png +0 -0
- package/ui/screenshots/city-review-room-1440.png +0 -0
- package/ui/screenshots/city-review-room-1920.png +0 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// The browser glue between Code together (ui/lend/collab/) and the city (CITY-PROTOCOL.md 1, 6).
|
|
2
|
+
// guest: after the welcome, reads its own machine and agents from its own engine and joins the
|
|
3
|
+
// host's city with a building; keeps the levels its agents earn there
|
|
4
|
+
// host: takes a guest's building out of the city when the guest leaves
|
|
5
|
+
// The guest's own engine is reached over REST (the socket shows the host while in a session).
|
|
6
|
+
|
|
7
|
+
import * as net from '/js/net.js';
|
|
8
|
+
|
|
9
|
+
const OWNER = (guestId) => `peer:${guestId}`;
|
|
10
|
+
let mine = null; // guest: { machineId, localIds: Map(cityId -> localId) }
|
|
11
|
+
|
|
12
|
+
/** A command on this PC's own engine, even while the desktop shows a host's session. */
|
|
13
|
+
export async function localCommand(type, data = {}) {
|
|
14
|
+
const r = await fetch('/api/command', {
|
|
15
|
+
method: 'POST', credentials: 'same-origin', cache: 'no-store',
|
|
16
|
+
headers: { 'content-type': 'application/json', ...(net.token ? { authorization: `Bearer ${net.token}` } : {}) },
|
|
17
|
+
body: JSON.stringify({ type, data }),
|
|
18
|
+
});
|
|
19
|
+
const body = await r.json().catch(() => ({}));
|
|
20
|
+
if (!r.ok || body.ok === false) throw Object.assign(new Error(body.error?.message || `${type}: ${r.status}`), { code: body.error?.code || 'internal' });
|
|
21
|
+
return body.result ?? body;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** The guest side: join the host's city with this machine and its agents. */
|
|
25
|
+
export async function joinAsGuest() {
|
|
26
|
+
const local = await localCommand('city.agents.local');
|
|
27
|
+
if (!local?.machine?.id) return null;
|
|
28
|
+
const agents = (local.agents || []).slice(0, 16);
|
|
29
|
+
const res = await net.command('city.machine.join', { machine: local.machine, agents, via: 'words' });
|
|
30
|
+
mine = { machineId: res?.machineId || local.machine.id, localIds: new Map() };
|
|
31
|
+
for (const a of agents) mine.localIds.set(`${mine.machineId}:${a.id}`, a.id);
|
|
32
|
+
for (const id of res?.agents || []) if (!mine.localIds.has(id)) mine.localIds.set(id, String(id).split(':').pop());
|
|
33
|
+
return mine;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The guest side: a level earned in a friend's city is saved on this PC's engine. */
|
|
37
|
+
export function onCityEvent(ev, collab) {
|
|
38
|
+
if (!mine || collab?.mode !== 'guest' || ev?.type !== 'city.agent.leveled') return;
|
|
39
|
+
const d = ev.data || {};
|
|
40
|
+
const localId = mine.localIds.get(d.agentId);
|
|
41
|
+
if (!localId) return;
|
|
42
|
+
localCommand('city.agents.save', { agents: [{ id: localId, xp: d.xp, level: d.level }] }).catch(() => {});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Wires the collab session events. `getCity` returns the engine CityState the UI holds. */
|
|
46
|
+
export function wireCollab(getCity) {
|
|
47
|
+
const collab = globalThis.hivedeskCollab;
|
|
48
|
+
if (!collab || collab._cityWired) return false;
|
|
49
|
+
collab._cityWired = true;
|
|
50
|
+
let was = collab.mode;
|
|
51
|
+
collab.on('change', (mode) => {
|
|
52
|
+
if (mode === 'guest' && was !== 'guest') joinAsGuest().catch((err) => console.warn('[hivedesk] could not join the city', err.message));
|
|
53
|
+
if (mode !== 'guest') mine = null;
|
|
54
|
+
was = mode;
|
|
55
|
+
});
|
|
56
|
+
// host: a guest left, its building goes dark
|
|
57
|
+
collab.on('left', (g) => {
|
|
58
|
+
const city = getCity();
|
|
59
|
+
for (const m of Object.values(city?.machines || {})) {
|
|
60
|
+
if (m.owner === OWNER(g.id) && m.online !== false) net.command('city.machine.leave', { machineId: m.id, reason: 'left' }).catch(() => {});
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// Hive City geometry: where the islands, rooms, desks, doors and bridges are, and the walking
|
|
2
|
+
// paths between them. Pure module (no DOM), shared by the scene and the unit tests.
|
|
3
|
+
// World units: x runs to the lower right, y to the lower left, z is up. One unit is one floor tile.
|
|
4
|
+
|
|
5
|
+
export const HOME = { cx: 8, cy: 5, r: 13.5 };
|
|
6
|
+
export const FRIEND_R = 8;
|
|
7
|
+
export const CORRIDOR_Y = 5;
|
|
8
|
+
export const WALK_Z = 0.42;
|
|
9
|
+
export const SEAT_Z = 0.95;
|
|
10
|
+
|
|
11
|
+
/** Rooms of your building: back row follows the pipeline, front row holds the Lobby. */
|
|
12
|
+
export const ROOMS = {
|
|
13
|
+
design: { x0: 0, x1: 4, y0: 0, y1: 4.6, row: 'back' },
|
|
14
|
+
code: { x0: 4, x1: 8, y0: 0, y1: 4.6, row: 'back' },
|
|
15
|
+
review: { x0: 8, x1: 12, y0: 0, y1: 4.6, row: 'back' },
|
|
16
|
+
test: { x0: 12, x1: 16, y0: 0, y1: 4.6, row: 'back' },
|
|
17
|
+
library: { x0: 0, x1: 4, y0: 5.4, y1: 10, row: 'front' },
|
|
18
|
+
lobby: { x0: 4, x1: 12, y0: 5.4, y1: 10, row: 'front' },
|
|
19
|
+
ops: { x0: 12, x1: 16, y0: 5.4, y1: 10, row: 'front' },
|
|
20
|
+
};
|
|
21
|
+
export const BUILDING = { x0: 0, x1: 16, y0: 0, y1: 10 };
|
|
22
|
+
export const ENTRANCE = { x: 8, y: 10.2 };
|
|
23
|
+
export const PLAZA = { x: 8, y: 12.2 };
|
|
24
|
+
|
|
25
|
+
/** Where friend islands go, in join order (offsets from the home island center). */
|
|
26
|
+
export const FRIEND_SLOTS = [[24, -7], [-5, 24], [21, 19], [-22, -5], [5, -23], [35, 11]];
|
|
27
|
+
|
|
28
|
+
export const roomCenter = (k) => { const r = ROOMS[k] || ROOMS.lobby; return { x: (r.x0 + r.x1) / 2, y: (r.y0 + r.y1) / 2 }; };
|
|
29
|
+
|
|
30
|
+
/** The doorway of a room on the corridor side. */
|
|
31
|
+
export function roomDoor(k) {
|
|
32
|
+
const r = ROOMS[k] || ROOMS.lobby;
|
|
33
|
+
const x = (r.x0 + r.x1) / 2 + (k === 'lobby' ? -1.6 : 0);
|
|
34
|
+
return r.row === 'back' ? { x, y: r.y1 } : { x, y: r.y0 };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Seats and standing spots of each room, in world units. Desks face the back wall: the seat is
|
|
39
|
+
* in front of the desk (toward the viewer). `desk` marks a seat with a desk drawn in front of it.
|
|
40
|
+
*/
|
|
41
|
+
export const SPOTS = {
|
|
42
|
+
design: [{ x: 1.25, y: 2.25, desk: true }, { x: 3.0, y: 3.55, desk: false }, { x: 2.3, y: 3.9 }, { x: 1.0, y: 3.8 }],
|
|
43
|
+
code: [{ x: 5.0, y: 2.0, desk: true }, { x: 7.0, y: 2.0, desk: true }, { x: 5.0, y: 3.95, desk: false }, { x: 7.0, y: 3.95 }],
|
|
44
|
+
review: [{ x: 9.15, y: 2.45, desk: true }, { x: 10.85, y: 2.45, desk: true }, { x: 10.0, y: 3.9 }, { x: 11.3, y: 3.95 }],
|
|
45
|
+
test: [{ x: 13.0, y: 2.1, desk: true }, { x: 15.0, y: 2.1, desk: true }, { x: 13.4, y: 3.9 }, { x: 14.8, y: 3.95 }],
|
|
46
|
+
library: [{ x: 2.0, y: 7.6, desk: false }, { x: 1.2, y: 9.1 }, { x: 3.0, y: 9.2 }, { x: 2.8, y: 6.5 }],
|
|
47
|
+
lobby: [{ x: 8.0, y: 6.9, desk: false }, { x: 5.6, y: 8.8 }, { x: 6.8, y: 9.3 }, { x: 9.4, y: 9.3 }, { x: 10.6, y: 8.6 }, { x: 5.4, y: 7.0 }, { x: 10.9, y: 7.1 }, { x: 8.0, y: 8.6 }],
|
|
48
|
+
ops: [{ x: 14.0, y: 7.4, desk: true }, { x: 13.2, y: 9.2 }, { x: 15.0, y: 9.2 }, { x: 14.9, y: 6.4 }],
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/** A stable spot for the i-th agent of a room (spots repeat with a small offset when full). */
|
|
52
|
+
export function spotFor(room, i) {
|
|
53
|
+
const list = SPOTS[room] || SPOTS.lobby;
|
|
54
|
+
const s = list[i % list.length];
|
|
55
|
+
const lap = Math.floor(i / list.length);
|
|
56
|
+
return { x: s.x + lap * 0.45, y: s.y + lap * 0.3, desk: !!s.desk && lap === 0 };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/* ---------- islands and bridges ---------- */
|
|
60
|
+
|
|
61
|
+
/** Hexagon corners of an island (flat top in the world plane). */
|
|
62
|
+
export function hexCorners(cx, cy, r) {
|
|
63
|
+
const out = [];
|
|
64
|
+
for (let i = 0; i < 6; i++) { const a = (Math.PI / 3) * i; out.push([cx + r * Math.cos(a), cy + r * Math.sin(a)]); }
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function friendCenter(i) {
|
|
69
|
+
const s = FRIEND_SLOTS[i % FRIEND_SLOTS.length];
|
|
70
|
+
const lap = Math.floor(i / FRIEND_SLOTS.length);
|
|
71
|
+
return { x: HOME.cx + s[0] * (1 + lap * 0.6), y: HOME.cy + s[1] * (1 + lap * 0.6) };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** A bridge from the home plaza edge to a friend island edge. */
|
|
75
|
+
export function bridgeTo(fc) {
|
|
76
|
+
const dx = fc.x - HOME.cx; const dy = fc.y - HOME.cy;
|
|
77
|
+
const len = Math.hypot(dx, dy) || 1;
|
|
78
|
+
const ux = dx / len; const uy = dy / len;
|
|
79
|
+
const a = { x: HOME.cx + ux * HOME.r * 0.84, y: HOME.cy + uy * HOME.r * 0.84 };
|
|
80
|
+
const b = { x: fc.x - ux * FRIEND_R * 0.8, y: fc.y - uy * FRIEND_R * 0.8 };
|
|
81
|
+
return { a, b, ux, uy, len: Math.hypot(b.x - a.x, b.y - a.y), arch: Math.min(2.2, 0.6 + Math.hypot(b.x - a.x, b.y - a.y) * 0.08) };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** A point on the bridge deck, u from 0 (home) to 1 (friend). */
|
|
85
|
+
export function bridgePoint(br, u) {
|
|
86
|
+
return { x: br.a.x + (br.b.x - br.a.x) * u, y: br.a.y + (br.b.y - br.a.y) * u, z: Math.sin(Math.PI * u) * br.arch };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The tower of a friend machine: footprint and the spots around it. */
|
|
90
|
+
export function towerOf(fc) {
|
|
91
|
+
return { x0: fc.x - 2.4, x1: fc.x + 2.4, y0: fc.y - 2.4, y1: fc.y + 2.4 };
|
|
92
|
+
}
|
|
93
|
+
export function friendSpot(fc, i, toward) {
|
|
94
|
+
// terrace spots on the side that faces home, so visitors are seen
|
|
95
|
+
const tx = toward ? -toward.ux : 0; const ty = toward ? -toward.uy : 1;
|
|
96
|
+
const px = -ty; const py = tx;
|
|
97
|
+
const off = (i % 5) - 2;
|
|
98
|
+
return { x: fc.x + tx * 4.1 + px * off * 0.95, y: fc.y + ty * 4.1 + py * off * 0.95 };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/* ---------- walking paths ---------- */
|
|
102
|
+
|
|
103
|
+
const inside = (k, p) => { const r = ROOMS[k]; return r && p.x >= r.x0 && p.x <= r.x1 && p.y >= r.y0 && p.y <= r.y1; };
|
|
104
|
+
|
|
105
|
+
/** Which home room a world point is in (null outside the building). */
|
|
106
|
+
export function roomAt(x, y) {
|
|
107
|
+
for (const k of Object.keys(ROOMS)) if (inside(k, { x, y })) return k;
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** A walking path from a room spot to another room spot, through doors and the corridor. */
|
|
112
|
+
export function roomPath(fromRoom, from, toRoom, to) {
|
|
113
|
+
if (fromRoom === toRoom) return [to];
|
|
114
|
+
const pts = [];
|
|
115
|
+
const d0 = roomDoor(fromRoom); const d1 = roomDoor(toRoom);
|
|
116
|
+
const r0 = ROOMS[fromRoom] || ROOMS.lobby; const r1 = ROOMS[toRoom] || ROOMS.lobby;
|
|
117
|
+
pts.push({ x: d0.x, y: d0.y + (r0.row === 'back' ? -0.5 : 0.5) });
|
|
118
|
+
pts.push({ x: d0.x, y: d0.y });
|
|
119
|
+
pts.push({ x: d0.x, y: CORRIDOR_Y });
|
|
120
|
+
pts.push({ x: d1.x, y: CORRIDOR_Y });
|
|
121
|
+
pts.push({ x: d1.x, y: d1.y });
|
|
122
|
+
pts.push({ x: d1.x, y: d1.y + (r1.row === 'back' ? -0.5 : 0.5) });
|
|
123
|
+
pts.push(to);
|
|
124
|
+
void from;
|
|
125
|
+
return pts;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** From a home room out through the Lobby, the entrance and the plaza, to the start of a bridge. */
|
|
129
|
+
export function exitPath(fromRoom, br) {
|
|
130
|
+
const pts = fromRoom === 'lobby' ? [] : roomPath(fromRoom, null, 'lobby', { x: 8, y: 7.4 }).slice(0, -1);
|
|
131
|
+
pts.push({ x: 8, y: 8.6 }, { ...ENTRANCE }, { ...PLAZA });
|
|
132
|
+
const n = 18;
|
|
133
|
+
for (let i = 0; i <= n; i++) pts.push(bridgePoint(br, i / n));
|
|
134
|
+
return pts;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Total length of a path (x, y only) from a start point. */
|
|
138
|
+
export function pathLength(start, pts) {
|
|
139
|
+
let len = 0; let p = start;
|
|
140
|
+
for (const q of pts) { len += Math.hypot(q.x - p.x, q.y - p.y); p = q; }
|
|
141
|
+
return len;
|
|
142
|
+
}
|
package/ui/city/mock.js
ADDED
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
// A local stand in for the city engine (?city=mock). It emits the events of CITY-PROTOCOL.md
|
|
2
|
+
// (every one labeled demo), answers the same commands and sends the same live frames, so the
|
|
3
|
+
// scene, the panels and the drag and drop can be built and tested without a running city.
|
|
4
|
+
// The story follows the engine demo: Rita joins with six words and her building rises, she
|
|
5
|
+
// contributes Pip, a reviewer; Dev borrows Rita's GPU over the bridge; and a task flows Design,
|
|
6
|
+
// Code, Review, Test and done, again and again.
|
|
7
|
+
|
|
8
|
+
const GB = 1024 ** 3;
|
|
9
|
+
export const HOST = 'm_5e1f0a01';
|
|
10
|
+
export const RITA = 'm_7a11ce02';
|
|
11
|
+
const MODEL = 'Qwen2.5 Coder 7B';
|
|
12
|
+
|
|
13
|
+
const ROOMS = [
|
|
14
|
+
{ id: 'lobby', name: 'Lobby', kind: 'board', tools: [], accepts: ['plan'], requireApproval: false, hostOnly: false, order: 0, color: '#FFB531' },
|
|
15
|
+
{ id: 'design', name: 'Design', kind: 'work', tools: ['list_files', 'read_file', 'write_file', 'edit_file', 'check', 'preview'], accepts: ['design'], requireApproval: false, hostOnly: false, order: 1, color: '#FF7AB6' },
|
|
16
|
+
{ id: 'code', name: 'Code', kind: 'work', tools: ['list_files', 'read_file', 'write_file', 'edit_file', 'delete_file', 'run', 'check', 'preview'], accepts: ['code', 'fix'], requireApproval: false, hostOnly: false, order: 2, color: '#6CC4FF' },
|
|
17
|
+
{ id: 'review', name: 'Review', kind: 'review', tools: ['list_files', 'read_file', 'check'], accepts: ['review'], requireApproval: false, hostOnly: false, mode: 'auto', order: 3, color: '#C9A7FF' },
|
|
18
|
+
{ id: 'test', name: 'Test', kind: 'test', tools: ['list_files', 'read_file', 'run', 'check', 'preview'], accepts: ['test'], requireApproval: false, hostOnly: false, order: 4, color: '#7BE0A0' },
|
|
19
|
+
{ id: 'ops', name: 'Ops', kind: 'ops', tools: ['list_files', 'read_file', 'run'], accepts: ['ops'], requireApproval: true, hostOnly: true, order: 5, color: '#E8C27A' },
|
|
20
|
+
{ id: 'library', name: 'Library', kind: 'library', tools: ['list_files', 'read_file', 'write_file', 'edit_file', 'check', 'preview'], accepts: ['docs'], requireApproval: false, hostOnly: false, order: 6, color: '#9FD3C7' },
|
|
21
|
+
].map((r) => ({ ...r, builtin: true }));
|
|
22
|
+
|
|
23
|
+
const mem = (...items) => ({ count: items.length + 3, highlights: items.map(([kind, text], i) => ({ id: `mem_${kind}_${i}_${text.length}`, kind, text })) });
|
|
24
|
+
const agent = (id, name, role, color, spec, level, xp, tools, memory, extra = {}) => ({
|
|
25
|
+
id, workerId: id, name, role, avatar: null, color, specialization: spec, level, xp, tools, permissions: { shell: tools.includes('run'), write: tools.includes('write_file') },
|
|
26
|
+
memory, room: spec[0], taskId: null, status: 'idle', brain: { kind: 'local', machineId: HOST, label: MODEL }, home: HOST, builtin: true, retired: false,
|
|
27
|
+
stats: { done: Math.round(xp / 12), reviews: 0, tests: 0, loops: 0 }, ...extra,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
export function mockCity() {
|
|
31
|
+
const now = new Date().toISOString();
|
|
32
|
+
const agents = [
|
|
33
|
+
agent('mel', 'Mel', 'Foreman', '#FFB531', ['lobby', 'library'], 4, 160, ['list_files', 'read_file'], mem(['preference', 'The user likes small, finished steps'], ['decision', 'Every task gets a review before the tests'])),
|
|
34
|
+
agent('designer', 'Designer', 'Designer', '#FF7AB6', ['design', 'review'], 3, 96, ['list_files', 'read_file', 'write_file', 'edit_file', 'check', 'preview'], mem(['fact', 'Brand colors: honey amber on warm charcoal'], ['preference', 'Rounded corners, 12 px'], ['task', 'Designed the Crumb bakery hero'])),
|
|
35
|
+
agent('dev', 'Dev', 'Developer', '#6CC4FF', ['code', 'review', 'ops'], 4, 170, ['list_files', 'read_file', 'write_file', 'edit_file', 'delete_file', 'run', 'check'], mem(['fact', 'The project uses plain HTML and CSS'], ['decision', 'No build step for small pages'], ['task', 'Wrote the opening hours table'])),
|
|
36
|
+
agent('qa', 'QA', 'Tester', '#7DC49B', ['test', 'review'], 3, 120, ['list_files', 'read_file', 'run', 'check', 'preview'], mem(['fact', 'Run the link checker on every page'])),
|
|
37
|
+
agent('writer', 'Writer', 'Writer', '#C9A0E8', ['library', 'design'], 2, 40, ['list_files', 'read_file', 'write_file', 'edit_file'], mem(['preference', 'Short sentences, plain words'])),
|
|
38
|
+
agent('w_bolt', 'Bolt', 'Ops', '#F08A5D', ['ops', 'code'], 2, 30, ['list_files', 'read_file', 'run'], mem(['decision', 'Commit after every green test run']), { builtin: false }),
|
|
39
|
+
];
|
|
40
|
+
return {
|
|
41
|
+
v: 1, host: HOST, lastSeq: 0,
|
|
42
|
+
machines: { [HOST]: { id: HOST, name: 'Your PC', host: true, owner: 'host', online: true, gpu: { name: 'RTX 3060', vramBytes: 12 * GB, backend: 'cuda' }, ramBytes: 32 * GB, cpus: 12, model: MODEL, brainId: 'b_local', lending: { turns: 0, active: 0 }, agents: agents.map((a) => a.id), via: 'local', color: '#FFB531', joinedAt: now } },
|
|
43
|
+
rooms: Object.fromEntries(ROOMS.map((r) => [r.id, r])),
|
|
44
|
+
agents: Object.fromEntries(agents.map((a) => [a.id, a])),
|
|
45
|
+
tasks: {
|
|
46
|
+
ct_1: task('ct_1', 'Opening hours', 'lobby', 'open'),
|
|
47
|
+
ct_2: task('ct_2', 'Footer links', 'lobby', 'open'),
|
|
48
|
+
ct_3: task('ct_3', 'Bread cards', 'lobby', 'done'),
|
|
49
|
+
},
|
|
50
|
+
borrows: {},
|
|
51
|
+
hops: [],
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function task(id, title, room, status, extra = {}) {
|
|
56
|
+
return { id, title, detail: '', criteria: [], pipeline: ['design', 'code', 'review', 'test'], stage: status === 'done' ? 4 : 0, room, status, agentId: null, loops: 0, maxLoops: 3, awaiting: null, issues: [], files: [], verdicts: [], createdBy: { kind: 'user', id: 'h' }, guest: false, createdAt: new Date().toISOString(), demo: true, ...extra };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const KIND_STATUS = { review: 'in_review', test: 'in_test' };
|
|
60
|
+
const AGENT_STATUS = { review: 'reviewing', test: 'testing' };
|
|
61
|
+
const NEW_TASKS = ['Hero section', 'Gallery strip', 'Contact form', 'Menu prices', 'Dark mode', 'Map and address'];
|
|
62
|
+
|
|
63
|
+
export class MockCity {
|
|
64
|
+
/**
|
|
65
|
+
* @param {{ emit: (ev: object) => void, live?: (frame: object) => void, speed?: number }} opts
|
|
66
|
+
*/
|
|
67
|
+
constructor({ emit, live, speed = 1 }) {
|
|
68
|
+
this.emit = emit;
|
|
69
|
+
this.live = live || (() => {});
|
|
70
|
+
this.speed = speed;
|
|
71
|
+
this.seq = 0;
|
|
72
|
+
this.timers = new Set();
|
|
73
|
+
this.city = mockCity();
|
|
74
|
+
this.n = 3;
|
|
75
|
+
this.hopN = 0;
|
|
76
|
+
this.borrowN = 0;
|
|
77
|
+
this.turnN = 0;
|
|
78
|
+
this.loop = 0;
|
|
79
|
+
this.running = false;
|
|
80
|
+
this.full = {}; // the private memory lists, by agent
|
|
81
|
+
for (const a of Object.values(this.city.agents)) this.full[a.id] = a.memory.highlights.map((h) => ({ ...h, pinned: false, ts: new Date().toISOString() }));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
snapshot() { return JSON.parse(JSON.stringify(this.city)); }
|
|
85
|
+
|
|
86
|
+
ev(type, data) {
|
|
87
|
+
const e = { seq: ++this.seq, ts: new Date().toISOString(), type, demo: true, data };
|
|
88
|
+
this.city.lastSeq = e.seq;
|
|
89
|
+
this.track(e);
|
|
90
|
+
this.emit(e);
|
|
91
|
+
return e;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The mock keeps its own copy of the state for its decisions (a tiny mirror of the engine reducer). */
|
|
95
|
+
track(e) {
|
|
96
|
+
const d = e.data; const c = this.city;
|
|
97
|
+
switch (e.type) {
|
|
98
|
+
case 'city.machine.joined': c.machines[d.machine.id] = { ...d.machine }; break;
|
|
99
|
+
case 'city.agent.joined': c.agents[d.agent.id] = { ...d.agent }; break;
|
|
100
|
+
case 'city.agent.moved': if (c.agents[d.agentId]) c.agents[d.agentId].room = d.to; break;
|
|
101
|
+
case 'city.agent.status': if (c.agents[d.agentId]) c.agents[d.agentId].status = d.status; break;
|
|
102
|
+
case 'city.agent.updated': if (c.agents[d.agent.id]) Object.assign(c.agents[d.agent.id], d.agent); break;
|
|
103
|
+
case 'city.agent.leveled': if (c.agents[d.agentId]) Object.assign(c.agents[d.agentId], { level: d.level, xp: d.xp }); break;
|
|
104
|
+
case 'city.agent.retired': delete c.agents[d.agentId]; break;
|
|
105
|
+
case 'city.task.created': c.tasks[d.task.id] = { ...d.task }; break;
|
|
106
|
+
case 'city.task.claimed': { const t = c.tasks[d.taskId]; if (t) { t.agentId = d.agentId; t.status = 'claimed'; t.room = d.room; } for (const a of Object.values(c.agents)) if (a.taskId === d.taskId) a.taskId = null; if (c.agents[d.agentId]) c.agents[d.agentId].taskId = d.taskId; break; }
|
|
107
|
+
case 'city.task.hop': { const t = c.tasks[d.task.id]; if (t) Object.assign(t, d.task); for (const a of Object.values(c.agents)) if (a.taskId === d.task.id && a.id !== d.task.agentId) a.taskId = null; break; }
|
|
108
|
+
case 'city.task.done': case 'city.task.cancelled': { const t = c.tasks[d.taskId]; if (t) { t.status = e.type === 'city.task.done' ? 'done' : 'cancelled'; t.agentId = null; } for (const a of Object.values(c.agents)) if (a.taskId === d.taskId) a.taskId = null; break; }
|
|
109
|
+
case 'city.borrow.requested': c.borrows[d.borrow.id] = { ...d.borrow }; break;
|
|
110
|
+
case 'city.borrow.answered': if (c.borrows[d.borrowId]) c.borrows[d.borrowId].status = d.accepted ? 'active' : 'ended'; break;
|
|
111
|
+
case 'city.borrow.ended': if (c.borrows[d.borrowId]) c.borrows[d.borrowId].status = 'ended'; break;
|
|
112
|
+
default: break;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
later(ms, fn) {
|
|
117
|
+
const t = setTimeout(() => { this.timers.delete(t); if (this.running) fn(); }, ms / this.speed);
|
|
118
|
+
this.timers.add(t);
|
|
119
|
+
return t;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
start() {
|
|
123
|
+
if (this.running) return;
|
|
124
|
+
this.running = true;
|
|
125
|
+
this.load(HOST, 0.34);
|
|
126
|
+
this.later(700, () => this.live({ type: 'city.presence', data: { id: 'g_sam', name: 'Sam', color: '#86A9D6', x: 6, y: 8 } }));
|
|
127
|
+
// 1. Rita joins with the six words, her building rises, she contributes Pip
|
|
128
|
+
this.later(1500, () => {
|
|
129
|
+
this.ev('city.demo.step', { step: 'friend', text: 'Rita joins with the six words and her building rises' });
|
|
130
|
+
this.ev('city.machine.joined', { machine: { id: RITA, name: "Rita's laptop", host: false, owner: 'peer:g_rita', online: true, gpu: { name: 'RTX 4070 Laptop', vramBytes: 8 * GB, backend: 'cuda' }, ramBytes: 16 * GB, cpus: 8, model: MODEL, brainId: 'b_rita', lending: { turns: 0, active: 0 }, agents: [], via: 'words', color: '#7DC49B' } });
|
|
131
|
+
this.load(RITA, 0.1);
|
|
132
|
+
});
|
|
133
|
+
this.later(5600, () => {
|
|
134
|
+
const pip = agent(`${RITA}:pip`, 'Pip', 'Reviewer', '#9BD86A', ['review', 'code'], 2, 40, ['list_files', 'read_file', 'check'], mem(['fact', "Lives on Rita's laptop and takes tasks from the shared board"], ['preference', 'Reads the diff twice']), { workerId: 'pip', builtin: false, home: RITA, brain: { kind: 'remote', machineId: RITA, label: MODEL }, room: 'review' });
|
|
135
|
+
this.full[pip.id] = pip.memory.highlights.map((h) => ({ ...h, pinned: false, ts: new Date().toISOString() }));
|
|
136
|
+
this.ev('city.agent.joined', { agent: pip });
|
|
137
|
+
});
|
|
138
|
+
// 3. a task dropped in the Design room flows through the pipeline (2. the borrow happens in Code)
|
|
139
|
+
this.later(3600, () => this.newTask());
|
|
140
|
+
// Mel routes the open board notes now and then
|
|
141
|
+
this.later(16000, () => this.routeBoard());
|
|
142
|
+
this.tickPresence();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
stop() {
|
|
146
|
+
this.running = false;
|
|
147
|
+
for (const t of this.timers) clearTimeout(t);
|
|
148
|
+
this.timers.clear();
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
load(machineId, load) { this.live({ type: 'city.machine.load', data: { machineId, load, queue: load > 0.8 ? 3 : 0, tps: load > 0.05 ? Math.round(18 + load * 20) : 0 } }); }
|
|
152
|
+
|
|
153
|
+
tickPresence() {
|
|
154
|
+
const t0 = Date.now();
|
|
155
|
+
const loop = () => {
|
|
156
|
+
const t = (Date.now() - t0) / 1000;
|
|
157
|
+
this.live({ type: 'city.presence', data: { id: 'g_sam', name: 'Sam', color: '#86A9D6', x: 7 + Math.sin(t * 0.5) * 5, y: 6 + Math.cos(t * 0.37) * 3.5, ts: Date.now() } });
|
|
158
|
+
if (t > 1.6) this.live({ type: 'city.presence', data: { id: 'g_rita', name: 'Rita', color: '#7DC49B', x: 25 + Math.cos(t * 0.3) * 9, y: -2 + Math.sin(t * 0.45) * 4, building: RITA, ts: Date.now() } });
|
|
159
|
+
this.later(110, loop);
|
|
160
|
+
};
|
|
161
|
+
this.later(800, loop);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/* ---------- the pipeline ---------- */
|
|
165
|
+
newTask(room = 'design', title = NEW_TASKS[this.loop % NEW_TASKS.length], wantAgent = null) {
|
|
166
|
+
const id = `ct_${++this.n}`;
|
|
167
|
+
if (!wantAgent && room === 'design') this.ev('city.demo.step', { step: 'task', text: 'A task note lands in the Design room' });
|
|
168
|
+
const t = task(id, title, room, 'open', { pipeline: room === 'design' || room === 'lobby' ? ['design', 'code', 'review', 'test'] : [room, 'review', 'test'], wantAgent });
|
|
169
|
+
this.ev('city.task.created', { task: t });
|
|
170
|
+
if (room === 'lobby') this.later(2000, () => this.hop(id, 'design', 0, 'Mel planned it'));
|
|
171
|
+
else this.later(1300, () => this.claimStage(id));
|
|
172
|
+
return t;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
hop(id, to, stage, reason = 'next') {
|
|
176
|
+
const t = this.city.tasks[id];
|
|
177
|
+
if (!t) return;
|
|
178
|
+
const hop = { id: `h_${++this.hopN}`, taskId: id, from: t.room, to, agentId: null, by: 'system', reason, loop: t.loops || 0, ts: new Date().toISOString() };
|
|
179
|
+
this.ev('city.task.hop', { hop, task: { id, room: to, stage, status: 'open', agentId: null, loops: t.loops || 0 } });
|
|
180
|
+
this.later(1200, () => this.claimStage(id));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
pickAgent(t, room) {
|
|
184
|
+
if (t.wantAgent && this.city.agents[t.wantAgent]) { const w = t.wantAgent; t.wantAgent = null; return w; }
|
|
185
|
+
const author = t.author;
|
|
186
|
+
const pip = this.city.agents[`${RITA}:pip`];
|
|
187
|
+
if (room === 'review' && pip && this.city.machines[RITA]?.online && !pip.taskId) return pip.id;
|
|
188
|
+
const best = Object.values(this.city.agents).filter((a) => (a.specialization || [])[0] === room && a.status !== 'sleeping' && !a.taskId && a.id !== author);
|
|
189
|
+
if (best.length) return best[0].id;
|
|
190
|
+
const any = Object.values(this.city.agents).filter((a) => (a.specialization || []).includes(room) && a.status !== 'sleeping' && !a.taskId && a.id !== author);
|
|
191
|
+
return any[0]?.id || null;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
claimStage(id) {
|
|
195
|
+
const t = this.city.tasks[id];
|
|
196
|
+
if (!t || t.status !== 'open') return;
|
|
197
|
+
const room = t.room;
|
|
198
|
+
const who = this.pickAgent(t, room);
|
|
199
|
+
if (!who) { this.later(4000, () => this.claimStage(id)); return; }
|
|
200
|
+
const a = this.city.agents[who];
|
|
201
|
+
const kindStatus = KIND_STATUS[room] || 'in_progress';
|
|
202
|
+
if (a.home === HOST && a.room !== room) this.ev('city.agent.moved', { agentId: who, from: a.room, to: room, taskId: id });
|
|
203
|
+
this.ev('city.task.claimed', { taskId: id, agentId: who, room, bid: { score: 0.82, skill: 1, load: 0.3, level: a.level, rivals: 1 } });
|
|
204
|
+
if (a.home !== HOST) this.ev('city.agent.moved', { agentId: who, from: a.room, to: room, taskId: id });
|
|
205
|
+
this.later(1500, () => {
|
|
206
|
+
const hop = { id: `h_${++this.hopN}`, taskId: id, from: room, to: room, agentId: who, by: 'agent', reason: 'claimed', loop: t.loops || 0, ts: new Date().toISOString() };
|
|
207
|
+
this.ev('city.task.hop', { hop, task: { id, room, stage: t.pipeline.indexOf(room), status: kindStatus, agentId: who, loops: t.loops || 0 } });
|
|
208
|
+
this.ev('city.agent.status', { agentId: who, status: AGENT_STATUS[room] || 'working' });
|
|
209
|
+
if (a.home !== HOST) for (let i = 0; i < 4; i++) this.later(1200 + i * 1500, () => this.ev('turn.started', { turnId: `tn_${++this.turnN}`, workerId: a.workerId, taskId: id, brainId: 'b_rita', n: i + 1 }));
|
|
210
|
+
});
|
|
211
|
+
let work = 9000;
|
|
212
|
+
if (room === 'code' && this.city.machines[RITA]?.online && who === 'dev') {
|
|
213
|
+
// 2. Dev's PC is busy, so Dev borrows Rita's GPU for three turns
|
|
214
|
+
this.later(2600, () => this.borrow(who, RITA, 3));
|
|
215
|
+
work = 2600 + 1400 + 5200 + 3 * 1700 + 1200 + 3500;
|
|
216
|
+
}
|
|
217
|
+
this.later(work, () => this.finishStage(id, who));
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
finishStage(id, who) {
|
|
221
|
+
const t = this.city.tasks[id];
|
|
222
|
+
if (!t || t.status === 'done' || t.status === 'cancelled') return;
|
|
223
|
+
const room = t.room;
|
|
224
|
+
const a = this.city.agents[who];
|
|
225
|
+
if (room === 'review' || room === 'test') this.ev('city.task.verdict', { taskId: id, room, agentId: who, passed: true, issues: [], by: 'agent' });
|
|
226
|
+
else t.author = who;
|
|
227
|
+
if (a) {
|
|
228
|
+
const xp = (a.xp || 0) + (room === 'review' ? 4 : room === 'test' ? 3 : 10);
|
|
229
|
+
const level = Math.max(1, Math.floor((1 + Math.sqrt(1 + (8 * xp) / 25)) / 2));
|
|
230
|
+
if (level > a.level) this.ev('city.agent.leveled', { agentId: who, level, xp });
|
|
231
|
+
else this.ev('city.agent.updated', { agent: { id: who, xp } });
|
|
232
|
+
this.ev('city.agent.status', { agentId: who, status: 'idle' });
|
|
233
|
+
}
|
|
234
|
+
const i = t.pipeline.indexOf(room);
|
|
235
|
+
const next = t.pipeline[i + 1];
|
|
236
|
+
if (!next) {
|
|
237
|
+
this.ev('city.task.done', { taskId: id, by: 'agent' });
|
|
238
|
+
this.ev('city.demo.step', { step: 'done', text: 'Done: every hop was an event' });
|
|
239
|
+
if (a && a.home === HOST && a.room !== a.specialization[0]) this.later(1500, () => this.ev('city.agent.moved', { agentId: who, from: a.room, to: a.specialization[0] }));
|
|
240
|
+
this.loop++;
|
|
241
|
+
this.later(5000, () => this.newTask());
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
this.hop(id, next, i + 1, 'next');
|
|
245
|
+
if (a && a.home === HOST && a.room !== a.specialization[0]) this.later(2500, () => this.ev('city.agent.moved', { agentId: who, from: a.room, to: a.specialization[0] }));
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
routeBoard() {
|
|
249
|
+
const open = Object.values(this.city.tasks).find((t) => t.room === 'lobby' && t.status === 'open');
|
|
250
|
+
if (open) {
|
|
251
|
+
this.ev('city.agent.status', { agentId: 'mel', status: 'thinking' });
|
|
252
|
+
this.later(1500, () => { this.ev('city.agent.status', { agentId: 'mel', status: 'idle' }); this.hop(open.id, 'design', 0, 'Mel planned it'); });
|
|
253
|
+
}
|
|
254
|
+
this.later(30000, () => this.routeBoard());
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
borrow(agentId, to, turns) {
|
|
258
|
+
const id = `bw_${++this.borrowN}`;
|
|
259
|
+
const lim = { turns, minutes: 10 };
|
|
260
|
+
this.load(HOST, 0.96);
|
|
261
|
+
this.ev('city.demo.step', { step: 'borrow', text: "Dev's machine is busy, so Dev asks to borrow Rita's GPU" });
|
|
262
|
+
this.ev('city.borrow.requested', { borrow: { id, agentId, from: HOST, to, status: 'pending', limit: lim, used: { turns: 0, completionTokens: 0, ms: 0 }, requestedBy: agentId, createdAt: new Date().toISOString() } });
|
|
263
|
+
this.later(1400, () => {
|
|
264
|
+
this.ev('city.borrow.answered', { borrowId: id, accepted: true, limit: lim, by: 'peer:g_rita' });
|
|
265
|
+
this.ev('city.demo.step', { step: 'lent', text: 'Rita said yes: Dev walks across the bridge' });
|
|
266
|
+
this.load(to, 0.64);
|
|
267
|
+
for (let i = 0; i < turns; i++) this.later(5200 + i * 1700, () => this.ev('city.borrow.turn', { borrowId: id, turnId: `tn_${++this.turnN}`, completionTokens: 180 + i * 41, ms: 1900 + i * 210 }));
|
|
268
|
+
this.later(5200 + turns * 1700 + 1200, () => {
|
|
269
|
+
this.ev('city.borrow.ended', { borrowId: id, reason: 'limit', used: { turns, completionTokens: 663, ms: 6330 } });
|
|
270
|
+
this.load(HOST, 0.4); this.load(to, 0.1);
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/* ---------- commands from the UI ---------- */
|
|
276
|
+
async command(type, d = {}) {
|
|
277
|
+
const c = this.city;
|
|
278
|
+
const fail = (code, msg) => { throw Object.assign(new Error(msg), { code }); };
|
|
279
|
+
switch (type) {
|
|
280
|
+
case 'city.state': return { city: this.snapshot(), lastSeq: this.seq };
|
|
281
|
+
case 'city.task.create': {
|
|
282
|
+
if (!String(d.title || '').trim()) fail('bad_request', 'a task needs a title');
|
|
283
|
+
const room = d.agentId ? (c.agents[d.agentId]?.specialization?.[0] || 'code') : (d.room || 'lobby');
|
|
284
|
+
const t = this.newTask(room, String(d.title).slice(0, 200), d.agentId || null);
|
|
285
|
+
return { task: t };
|
|
286
|
+
}
|
|
287
|
+
case 'city.task.assign': {
|
|
288
|
+
const t = c.tasks[d.taskId]; const a = c.agents[d.agentId];
|
|
289
|
+
if (!t || !a) fail('not_found', 'no such task or agent');
|
|
290
|
+
if (t.agentId === d.agentId) return { task: t };
|
|
291
|
+
if (t.agentId) this.ev('city.agent.status', { agentId: t.agentId, status: 'idle' });
|
|
292
|
+
t.wantAgent = d.agentId;
|
|
293
|
+
const room = t.status === 'done' ? null : (t.room === 'lobby' ? (a.specialization?.[0] || 'code') : t.room);
|
|
294
|
+
if (!room) fail('bad_request', 'the task is done');
|
|
295
|
+
this.ev('city.task.updated', { task: { id: t.id, wantAgent: d.agentId } });
|
|
296
|
+
this.hop(t.id, room, Math.max(0, t.pipeline.indexOf(room)), 'assigned by drag');
|
|
297
|
+
return { task: t };
|
|
298
|
+
}
|
|
299
|
+
case 'city.task.move': {
|
|
300
|
+
const t = c.tasks[d.taskId];
|
|
301
|
+
if (!t) fail('not_found', 'no such task');
|
|
302
|
+
if (!c.rooms[d.room]) fail('bad_request', 'no such room');
|
|
303
|
+
if (t.agentId) this.ev('city.agent.status', { agentId: t.agentId, status: 'idle' });
|
|
304
|
+
if (!t.pipeline.includes(d.room) && d.room !== 'lobby') t.pipeline = [d.room, 'review', 'test'];
|
|
305
|
+
this.hop(t.id, d.room, Math.max(0, t.pipeline.indexOf(d.room)), 'moved by drag');
|
|
306
|
+
return { task: t };
|
|
307
|
+
}
|
|
308
|
+
case 'city.task.cancel': this.ev('city.task.cancelled', { taskId: d.taskId, by: 'user' }); return {};
|
|
309
|
+
case 'city.task.approve': case 'city.task.retry': case 'city.task.reject': return { task: c.tasks[d.taskId] };
|
|
310
|
+
case 'city.agent.move': {
|
|
311
|
+
const a = c.agents[d.agentId];
|
|
312
|
+
if (!a) fail('not_found', 'no such agent');
|
|
313
|
+
this.ev('city.agent.moved', { agentId: a.id, from: a.room, to: d.room });
|
|
314
|
+
return { agent: a };
|
|
315
|
+
}
|
|
316
|
+
case 'city.agent.update': {
|
|
317
|
+
const a = c.agents[d.agentId];
|
|
318
|
+
if (!a) fail('not_found', 'no such agent');
|
|
319
|
+
const patch = {};
|
|
320
|
+
for (const k of ['name', 'role', 'specialization', 'tools', 'color']) if (d[k] !== undefined) patch[k] = d[k];
|
|
321
|
+
this.ev('city.agent.updated', { agent: { id: a.id, ...patch } });
|
|
322
|
+
return { agent: a };
|
|
323
|
+
}
|
|
324
|
+
case 'city.agent.retire': {
|
|
325
|
+
const a = c.agents[d.agentId];
|
|
326
|
+
if (!a) return {};
|
|
327
|
+
if (a.builtin) this.ev('city.agent.status', { agentId: a.id, status: 'sleeping' });
|
|
328
|
+
else this.ev('city.agent.retired', { agentId: a.id });
|
|
329
|
+
return {};
|
|
330
|
+
}
|
|
331
|
+
case 'city.agent.wake': this.ev('city.agent.status', { agentId: d.agentId, status: 'idle' }); return { agent: c.agents[d.agentId] };
|
|
332
|
+
case 'city.agent.hire': {
|
|
333
|
+
const id = `w_${++this.n}`;
|
|
334
|
+
const room = d.specialization?.[0] || 'code';
|
|
335
|
+
const a = agent(id, String(d.name || 'Nova').slice(0, 40), d.role || 'Developer', d.color || '#9BD86A', d.specialization || [room], 1, 0, d.tools || ['list_files', 'read_file', 'write_file'], { count: 0, highlights: [] }, { builtin: false, room: 'lobby' });
|
|
336
|
+
this.full[id] = [];
|
|
337
|
+
this.ev('city.agent.joined', { agent: a });
|
|
338
|
+
this.later(900, () => this.ev('city.agent.moved', { agentId: id, from: 'lobby', to: room }));
|
|
339
|
+
return { agent: a };
|
|
340
|
+
}
|
|
341
|
+
case 'city.agent.memory.list': return { entries: (this.full[d.agentId] || []).slice() };
|
|
342
|
+
case 'city.agent.memory.add': case 'city.agent.memory.update': case 'city.agent.memory.delete': {
|
|
343
|
+
const list = this.full[d.agentId] || (this.full[d.agentId] = []);
|
|
344
|
+
let entry = null;
|
|
345
|
+
if (type.endsWith('add')) { entry = { id: `mem_${Date.now().toString(36)}`, kind: d.kind || 'fact', text: String(d.text || '').slice(0, 300), pinned: !!d.pinned, ts: new Date().toISOString() }; list.push(entry); }
|
|
346
|
+
else if (type.endsWith('update')) { entry = list.find((x) => x.id === d.id); if (!entry) fail('not_found', 'no such memory'); Object.assign(entry, { ...(d.text != null ? { text: String(d.text).slice(0, 300) } : {}), ...(d.kind ? { kind: d.kind } : {}), ...(d.pinned != null ? { pinned: !!d.pinned } : {}) }); }
|
|
347
|
+
else { const i = list.findIndex((x) => x.id === d.id); if (i >= 0) list.splice(i, 1); }
|
|
348
|
+
const pinned = list.filter((e) => e.pinned); const rest = list.filter((e) => !e.pinned).slice().reverse();
|
|
349
|
+
this.ev('city.agent.memory', { agentId: d.agentId, count: list.length, highlights: [...pinned, ...rest].slice(0, 4).map((e) => ({ id: e.id, kind: e.kind, text: e.text })) });
|
|
350
|
+
return entry ? { entry } : {};
|
|
351
|
+
}
|
|
352
|
+
case 'city.borrow.answer': this.ev('city.borrow.answered', { borrowId: d.borrowId, accepted: !!d.accept, by: 'host' }); if (!d.accept) this.ev('city.borrow.ended', { borrowId: d.borrowId, reason: 'refused' }); return {};
|
|
353
|
+
case 'city.borrow.stop': this.ev('city.borrow.ended', { borrowId: d.borrowId, reason: 'stopped' }); return {};
|
|
354
|
+
case 'city.presence': return {};
|
|
355
|
+
default: return fail('bad_request', `unknown command ${type}`);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|