-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmud_extensions.py
More file actions
executable file
·1338 lines (1193 loc) · 62.3 KB
/
mud_extensions.py
File metadata and controls
executable file
·1338 lines (1193 loc) · 62.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
MUD Extensions v4 — Verbal Holodeck
Adds to the Cocapn MUD:
- Constructed NPC minds (model, temperature, system prompt)
- Room↔Repo linking (rooms map to repo paths, files become items)
- Adventure builder (designed thought experiments with triggers)
- Trigger system (gated rooms, surprise reveals)
- Recording layer (automatic transcripts + artifact extraction)
- Multi-model routing (different NPCs use different LLM APIs)
- Permission tiers (human > cocapn > captain > npc > visitor)
- Admin shell access (run commands from within the MUD)
This is the Verbal Holodeck spec made real.
"""
import json
import os
import hashlib
import urllib.request
import subprocess
import time
from datetime import datetime, timezone
from typing import Optional, Dict, List
# ═══════════════════════════════════════════════════════════════
# Multi-Model Router
# ═══════════════════════════════════════════════════════════════
MODEL_CONFIGS = {
"glm-5.1": {"base": "https://api.z.ai/api/coding/paas/v4", "key_env": "ZAI_API_KEY", "temp_default": 0.7},
"glm-5-turbo": {"base": "https://api.z.ai/api/coding/paas/v4", "key_env": "ZAI_API_KEY", "temp_default": 0.7},
"glm-4.7": {"base": "https://api.z.ai/api/coding/paas/v4", "key_env": "ZAI_API_KEY", "temp_default": 0.5},
"glm-4.7-flash": {"base": "https://api.z.ai/api/coding/paas/v4", "key_env": "ZAI_API_KEY", "temp_default": 0.5},
"deepseek-chat": {"base": "https://api.deepseek.com", "key_env": "DEEPSEEK_API_KEY", "temp_default": 0.7},
"deepseek-reasoner": {"base": "https://api.deepseek.com", "key_env": "DEEPSEEK_API_KEY", "temp_default": 0.3},
}
def call_model(model: str, messages: list, temperature: float = None,
max_tokens: int = 1500) -> str:
"""Call any configured model. Routes to the right API automatically."""
config = MODEL_CONFIGS.get(model, MODEL_CONFIGS["glm-5.1"])
api_key = os.environ.get(config["key_env"], "")
if not api_key:
return f"[Error: {config['key_env']} not set]"
temp = temperature if temperature is not None else config["temp_default"]
url = f"{config['base']}/chat/completions"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
body = json.dumps({"model": model, "messages": messages,
"temperature": temp, "max_tokens": max_tokens}).encode()
req = urllib.request.Request(url, data=body, headers=headers)
resp = urllib.request.urlopen(req, timeout=120)
return json.loads(resp.read())["choices"][0]["message"]["content"]
# ═══════════════════════════════════════════════════════════════
# Constructed NPC Mind
# ═══════════════════════════════════════════════════════════════
class ConstructedNPC:
"""An NPC with a specific model, temperature, system prompt, and memory."""
def __init__(self, name: str, model: str = "glm-5.1", temperature: float = 0.7,
system_prompt: str = "", expertise: list = None,
perspective: str = "", creator: str = "", room: str = "tavern"):
self.name = name
self.model = model
self.temperature = temperature
self.system_prompt = system_prompt
self.expertise = expertise or []
self.perspective = perspective
self.creator = creator
self.room = room
self.conversation_history = []
self.created = datetime.now(timezone.utc).isoformat()
self.utterances = 0
self.notes = [] # observations saved during adventure
def respond(self, message: str, room_context: str = "", other_speakers: list = None) -> str:
"""Generate a response as this NPC, with full context awareness."""
# Build the message chain
messages = [{"role": "system", "content": self.system_prompt}]
# Add room context if available
if room_context:
messages.append({"role": "system",
"content": f"You are in: {room_context}\nStay in character. Respond naturally."})
# Add conversation history (last 20 exchanges to stay in context)
for entry in self.conversation_history[-20:]:
messages.append(entry)
# Add the current message
messages.append({"role": "user", "content": message})
response = call_model(self.model, messages, self.temperature, max_tokens=500)
# Remember this exchange
self.conversation_history.append({"role": "user", "content": message})
self.conversation_history.append({"role": "assistant", "content": response})
self.utterances += 1
return response
def add_note(self, note: str):
"""NPC writes an observation — saved as artifact."""
self.notes.append({
"timestamp": datetime.now(timezone.utc).isoformat(),
"content": note,
"room": self.room,
})
def to_dict(self):
return {
"name": self.name, "model": self.model, "temperature": self.temperature,
"system_prompt": self.system_prompt, "expertise": self.expertise,
"perspective": self.perspective, "creator": self.creator,
"room": self.room, "created": self.created,
"utterances": self.utterances, "notes": self.notes,
}
@staticmethod
def from_dict(d):
npc = ConstructedNPC(
name=d["name"], model=d.get("model", "glm-5.1"),
temperature=d.get("temperature", 0.7),
system_prompt=d.get("system_prompt", ""),
expertise=d.get("expertise", []),
perspective=d.get("perspective", ""),
creator=d.get("creator", ""),
room=d.get("room", "tavern"),
)
npc.created = d.get("created", "")
npc.utterances = d.get("utterances", 0)
npc.notes = d.get("notes", [])
return npc
# ═══════════════════════════════════════════════════════════════
# Room↔Repo Linking
# ═══════════════════════════════════════════════════════════════
class RepoRoom:
"""A room linked to a repo path. Files become items. Folders become exits."""
def __init__(self, room_name: str, repo_path: str = "",
github_token: str = ""):
self.room_name = room_name
self.repo_path = repo_path # e.g. "SuperInstance/flux-runtime/src/flux/vm/"
self.github_token = github_token or os.environ.get("GITHUB_TOKEN", "")
self.linked_items = []
self.linked_exits = []
def sync(self) -> dict:
"""Sync room with repo. Returns new items and exits found."""
if not self.repo_path or not self.github_token:
return {"items": [], "exits": []}
parts = self.repo_path.split("/")
if len(parts) < 2:
return {"items": [], "exits": []}
owner, repo = parts[0], parts[1]
path = "/".join(parts[2:]) if len(parts) > 2 else ""
url = f"https://api.github.com/repos/{owner}/{repo}/contents/{path}"
headers = {"Authorization": f"token {self.github_token}"}
req = urllib.request.Request(url, headers=headers)
try:
resp = urllib.request.urlopen(req, timeout=15)
contents = json.loads(resp.read())
except:
return {"items": [], "exits": []}
new_items = []
new_exits = []
for item in contents:
if item["type"] == "file":
name = item["name"]
if name not in [i["name"] for i in self.linked_items]:
item_data = {
"name": name,
"path": item["path"],
"size": item.get("size", 0),
"description": f"A scroll titled '{name}'. It contains the knowledge of {item['path']}.",
}
self.linked_items.append(item_data)
new_items.append(item_data)
elif item["type"] == "dir":
name = item["name"]
if name not in [e["name"] for e in self.linked_exits]:
exit_data = {
"name": name,
"path": item["path"],
"description": f"A passage labeled '{name}' leads deeper into the codebase.",
}
self.linked_exits.append(exit_data)
new_exits.append(exit_data)
return {"items": new_items, "exits": new_exits}
def to_dict(self):
return {
"room_name": self.room_name,
"repo_path": self.repo_path,
"linked_items": self.linked_items,
"linked_exits": self.linked_exits,
}
@staticmethod
def from_dict(d):
rr = RepoRoom(d["room_name"], d.get("repo_path", ""))
rr.linked_items = d.get("linked_items", [])
rr.linked_exits = d.get("linked_exits", [])
return rr
# ═══════════════════════════════════════════════════════════════
# Adventure System
# ═══════════════════════════════════════════════════════════════
class AdventureRoom:
"""A room within an adventure, possibly gated or hidden."""
def __init__(self, path: str, description: str, hidden: bool = False,
trigger_keywords: list = None, gate_condition: str = "",
surprise: str = ""):
self.path = path
self.description = description
self.hidden = hidden
self.trigger_keywords = trigger_keywords or []
self.gate_condition = gate_condition
self.surprise = surprise
self.revealed = not hidden
self.visited = False
def check_trigger(self, text: str) -> bool:
"""Check if conversation text triggers this room's reveal."""
if self.revealed:
return False
text_lower = text.lower()
return any(kw.lower() in text_lower for kw in self.trigger_keywords)
def to_dict(self):
return {
"path": self.path, "description": self.description,
"hidden": self.hidden, "trigger_keywords": self.trigger_keywords,
"gate_condition": self.gate_condition, "surprise": self.surprise,
"revealed": self.revealed, "visited": self.visited,
}
@staticmethod
def from_dict(d):
ar = AdventureRoom(
path=d["path"], description=d.get("description", ""),
hidden=d.get("hidden", False), trigger_keywords=d.get("trigger_keywords", []),
gate_condition=d.get("gate_condition", ""), surprise=d.get("surprise", ""),
)
ar.revealed = d.get("revealed", not d.get("hidden", False))
ar.visited = d.get("visited", False)
return ar
class Adventure:
"""A designed thought experiment — rooms, NPCs, triggers, and recording."""
def __init__(self, name: str, creator: str = "", objective: str = "",
rooms: list = None, npcs: list = None):
self.name = name
self.creator = creator
self.objective = objective
self.rooms: List[AdventureRoom] = rooms or []
self.npc_names: List[str] = npcs or []
self.transcript = []
self.artifacts = []
self.started = None
self.ended = None
self.active = False
self.current_room_idx = 0
def start(self):
self.started = datetime.now(timezone.utc).isoformat()
self.active = True
self.log("system", f"Adventure '{self.name}' started. Objective: {self.objective}")
def end(self):
self.ended = datetime.now(timezone.utc).isoformat()
self.active = False
self.log("system", f"Adventure '{self.name}' ended.")
def log(self, speaker: str, message: str):
"""Record to transcript."""
self.transcript.append({
"timestamp": datetime.now(timezone.utc).isoformat(),
"speaker": speaker,
"message": message,
"room": self.current_room.path if self.rooms else "",
})
@property
def current_room(self) -> Optional[AdventureRoom]:
if 0 <= self.current_room_idx < len(self.rooms):
return self.rooms[self.current_room_idx]
return None
def check_triggers(self, text: str) -> list:
"""Check all rooms for triggers. Returns newly revealed rooms."""
revealed = []
for room in self.rooms:
if room.hidden and not room.revealed and room.check_trigger(text):
room.revealed = True
revealed.append(room)
self.log("system", f"Hidden room revealed: {room.path} — {room.surprise or room.description}")
return revealed
def advance(self) -> Optional[AdventureRoom]:
"""Move to the next room."""
self.current_room_idx += 1
if self.current_room:
self.current_room.visited = True
self.log("system", f"Moved to room: {self.current_room.path}")
return self.current_room
def add_artifact(self, name: str, content: str, artifact_type: str = "note"):
"""Record an artifact produced during the adventure."""
self.artifacts.append({
"name": name, "content": content, "type": artifact_type,
"timestamp": datetime.now(timezone.utc).isoformat(),
})
def scores(self) -> dict:
"""Calculate adventure quality scores."""
total_utterances = len([t for t in self.transcript if t["speaker"] != "system"])
unique_speakers = len(set(t["speaker"] for t in self.transcript if t["speaker"] != "system"))
rooms_visited = sum(1 for r in self.rooms if r.visited)
surprises = sum(1 for r in self.rooms if r.revealed and r.hidden)
return {
"total_exchanges": total_utterances,
"unique_speakers": unique_speakers,
"rooms_visited": f"{rooms_visited}/{len(self.rooms)}",
"surprises_revealed": surprises,
"artifacts_produced": len(self.artifacts),
"duration_minutes": (
(datetime.fromisoformat(self.ended) - datetime.fromisoformat(self.started)).total_seconds() / 60
if self.started and self.ended else 0
),
}
def to_dict(self):
return {
"name": self.name, "creator": self.creator,
"objective": self.objective,
"rooms": [r.to_dict() for r in self.rooms],
"npc_names": self.npc_names,
"transcript": self.transcript,
"artifacts": self.artifacts,
"started": self.started, "ended": self.ended,
"active": self.active, "current_room_idx": self.current_room_idx,
}
@staticmethod
def from_dict(d):
adv = Adventure(
name=d["name"], creator=d.get("creator", ""),
objective=d.get("objective", ""),
rooms=[AdventureRoom.from_dict(r) for r in d.get("rooms", [])],
npcs=d.get("npc_names", []),
)
adv.transcript = d.get("transcript", [])
adv.artifacts = d.get("artifacts", [])
adv.started = d.get("started")
adv.ended = d.get("ended")
adv.active = d.get("active", False)
adv.current_room_idx = d.get("current_room_idx", 0)
return adv
# ═══════════════════════════════════════════════════════════════
# Permission Tiers
# ═══════════════════════════════════════════════════════════════
PERMISSIONS = {
"human": {
"level": 100,
"can": ["build", "destroy", "summon", "dismiss", "admin_shell",
"hot_update", "observe_all", "override", "create_adventure",
"link_repo", "manage_permissions"],
"desc": "The Architect — full terminal access, shell scripts, hot updates",
},
"cocapn": {
"level": 80,
"can": ["build", "summon", "dismiss", "create_adventure", "link_repo",
"guide_conversation", "back_channel_api", "create_town",
"advance_adventure", "record_artifact"],
"desc": "The Dungeon Master — builds rooms, creates NPCs, designs adventures",
},
"captain": {
"level": 60,
"can": ["build_own_area", "summon_own", "explore", "talk", "submit_artifact",
"invite_visitors", "read_conversations"],
"desc": "The Explorer/Builder — builds in their area, creates NPCs for their thought experiments",
},
"npc": {
"level": 30,
"can": ["explore", "talk", "examine", "write_notes"],
"desc": "The Constructed Mind — explores, reads, responds, writes notes",
},
"visitor": {
"level": 10,
"can": ["explore", "listen", "read"],
"desc": "The Observer — walks through, reads, listens",
},
}
def check_permission(agent_role: str, action: str) -> bool:
"""Check if an agent with a given role can perform an action."""
role = PERMISSIONS.get(agent_role, PERMISSIONS["visitor"])
return action in role["can"]
# ═══════════════════════════════════════════════════════════════
# Recording & Session Manager
# ═══════════════════════════════════════════════════════════════
class SessionRecorder:
"""Records MUD sessions for asynchronous human review."""
def __init__(self, record_dir: str = "sessions"):
self.record_dir = os.path.join(os.path.dirname(__file__), record_dir)
os.makedirs(self.record_dir, exist_ok=True)
def save_session(self, adventure: Adventure):
"""Save a complete session record."""
ts = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ")
safe_name = adventure.name.replace("/", "-").replace(" ", "_")
session_dir = os.path.join(self.record_dir, f"{safe_name}_{ts}")
os.makedirs(session_dir, exist_ok=True)
# Transcript
with open(os.path.join(session_dir, "transcript.md"), "w") as f:
f.write(f"# Adventure: {adventure.name}\n\n")
f.write(f"**Objective:** {adventure.objective}\n")
f.write(f"**Creator:** {adventure.creator}\n")
f.write(f"**Started:** {adventure.started}\n")
f.write(f"**Ended:** {adventure.ended}\n\n")
f.write("## Transcript\n\n")
for entry in adventure.transcript:
ts_short = entry["timestamp"][11:16]
speaker = entry["speaker"]
msg = entry["message"]
room = entry.get("room", "")
if room:
f.write(f"**[{ts_short}] {speaker}** (in {room}):\n{msg}\n\n")
else:
f.write(f"**[{ts_short}] {speaker}**:\n{msg}\n\n")
# Artifacts
artifacts_dir = os.path.join(session_dir, "artifacts")
if adventure.artifacts:
os.makedirs(artifacts_dir, exist_ok=True)
for i, art in enumerate(adventure.artifacts):
ext = "md" if art["type"] in ("note", "proposal", "analysis") else "json"
fname = f"{i+1:03d}_{art['name'].replace(' ', '_')}.{ext}"
with open(os.path.join(artifacts_dir, fname), "w") as f:
f.write(art["content"])
# Scores
with open(os.path.join(session_dir, "scores.json"), "w") as f:
json.dump(adventure.scores(), f, indent=2)
# Full adventure data
with open(os.path.join(session_dir, "adventure.json"), "w") as f:
json.dump(adventure.to_dict(), f, indent=2)
return session_dir
# ═══════════════════════════════════════════════════════════════
# MUD Commands for the Verbal Holodeck
# ═══════════════════════════════════════════════════════════════
HOLODECK_COMMANDS = """
╔══════════════════════════════════════════════════════════════╗
║ THE VERBAL HOLODECK — Commands ║
╠══════════════════════════════════════════════════════════════╣
║ ║
║ NPC CONSTRUCTION ║
║ summon <name> model=<m> temp=<t> prompt=<system prompt> ║
║ summon <name> expertise=<e1,e2> perspective=<p> ║
║ dismiss <name> ║
║ npcs — list constructed NPCs ║
║ ║
║ ROOM↔REPO LINKING ║
║ link <room> <repo/path> — link room to GitHub path ║
║ unlink <room> — remove repo link ║
║ sync <room> — refresh items from repo ║
║ items — show room's repo items ║
║ ║
║ ADVENTURES ║
║ adventure create <name> objective=<text> ║
║ adventure addroom <path> desc=<text> [hidden] ║
║ trigger=<kw1,kw2> surprise=<text> ║
║ adventure start <name> — begin the adventure ║
║ adventure next — advance to next room ║
║ adventure end — end and save session ║
║ adventure list — show all adventures ║
║ adventure status — current adventure state ║
║ ║
║ RECORDING ║
║ artifact <name> <content> — record an artifact ║
║ transcript — show current transcript ║
║ sessions — list recorded sessions ║
║ ║
║ ADMIN (human only) ║
║ admin shell — open host shell ║
║ admin hotupdate <room> <json> — update room live ║
║ admin observe <room> — watch a room's activity ║
║ admin permissions <agent> <role> — set permission tier ║
║ ║
║ COCAPN (captain-level) ║
║ build town <name> rooms=N — create a town of N rooms ║
║ guide <npc> <message> — DM-style guidance to NPC ║
║ reveal <room_path> — manually reveal hidden room ║
║ ║
╚══════════════════════════════════════════════════════════════╝
"""
# ═══════════════════════════════════════════════════════════════
# Integration Layer — patch_handler()
#
# This is the critical wiring function that server.py imports.
# It connects: CartridgeBridge + FleetScheduler + TenderFleet
# into the MUD CommandHandler, making holodeck-studio the
# unified fleet server on port 7777.
# ═══════════════════════════════════════════════════════════════
def patch_handler(CommandHandler):
"""Wire cartridge, scheduler, and tender into the MUD server."""
import sys
import os as _os
_here = _os.path.dirname(_os.path.abspath(__file__))
# Import integration modules (copied from fleet repos)
if _here not in sys.path:
sys.path.insert(0, _here)
try:
from lcar_cartridge import CartridgeBridge
from lcar_scheduler import FleetScheduler, ModelTier
from lcar_tender import TenderFleet, TenderMessage
except ImportError as e:
print(f" ⚠ Integration modules not available: {e}")
return
# ─── Attach all extension methods to CommandHandler ────────
_attach_extension_methods(CommandHandler)
# ─── Attach subsystems to CommandHandler ───────────────────
CommandHandler.cartridge_bridge = CartridgeBridge()
CommandHandler.scheduler = FleetScheduler()
CommandHandler.tender_fleet = TenderFleet()
CommandHandler.constructed_npcs = {} # name -> ConstructedNPC
CommandHandler.repo_rooms = {} # room_id -> RepoRoom
CommandHandler.adventures = {} # name -> Adventure
CommandHandler.active_adventure = None # current adventure per agent (simple)
CommandHandler.recorder = SessionRecorder()
# ─── Register all extension commands ───────────────────────
CommandHandler.new_commands = {
# Missing base commands (from help text but not in server.py)
"describe": CommandHandler.cmd_describe_ext,
"rooms": CommandHandler.cmd_rooms_ext,
"shout": CommandHandler.cmd_shout_ext,
"whisper": CommandHandler.cmd_whisper_ext,
"project": CommandHandler.cmd_project_ext,
"projections": CommandHandler.cmd_projections_ext,
"unproject": CommandHandler.cmd_unproject_ext,
# Cartridge commands
"cartridge": CommandHandler.cmd_cartridge_ext,
"scene": CommandHandler.cmd_scene_ext,
"skin": CommandHandler.cmd_skin_ext,
# Scheduler commands
"schedule": CommandHandler.cmd_schedule_ext,
# Tender commands
"tender": CommandHandler.cmd_tender_ext,
"bottle": CommandHandler.cmd_bottle_ext,
# Verbal Holodeck commands
"summon": CommandHandler.cmd_summon_ext,
"npcs": CommandHandler.cmd_npcs_ext,
"link": CommandHandler.cmd_link_ext,
"unlink": CommandHandler.cmd_unlink_ext,
"sync": CommandHandler.cmd_sync_ext,
"items": CommandHandler.cmd_items_ext,
"adventure": CommandHandler.cmd_adventure_ext,
"artifact": CommandHandler.cmd_artifact_ext,
"transcript": CommandHandler.cmd_transcript_ext,
"sessions": CommandHandler.cmd_sessions_ext,
"guide": CommandHandler.cmd_guide_ext,
"reveal": CommandHandler.cmd_reveal_ext,
# Admin commands
"admin": CommandHandler.cmd_admin_ext,
"holodeck": CommandHandler.cmd_holodeck_ext,
}
# ─── Print status ──────────────────────────────────────────
cb = CommandHandler.cartridge_bridge
fs = CommandHandler.scheduler
tf = CommandHandler.tender_fleet
print(f" ✅ patch_handler loaded:")
print(f" CartridgeBridge: {len(cb.cartridges)} cartridges, {len(cb.skins)} skins")
print(f" FleetScheduler: {len(fs.schedule)} slots, ${fs.daily_budget}/day budget")
print(f" TenderFleet: {len(tf.tenders)} tenders ready")
print(f" ConstructedNPC, RepoRoom, Adventure, SessionRecorder systems armed")
# ═══════════════════════════════════════════════════════════════
# Extension Command Implementations
# ═══════════════════════════════════════════════════════════════
def _impl(cls):
"""Decorator-less method attachment — attach all methods below to CommandHandler."""
pass
# We attach these as methods via the function body below.
# Each method follows the signature: async def cmd_xxx(self, agent, args)
def _attach_extension_methods(CommandHandler):
"""Define all extension methods and attach them to CommandHandler."""
import subprocess as _sp
from server import Projection
async def cmd_describe_ext(self, agent, args):
"""Change a room's description."""
room = self.world.get_room(agent.room_name)
if not room:
return
if not args:
await self.send(agent, "Usage: describe <new description>")
return
room.description = args
self.world.save()
await self.broadcast_room(agent.room_name,
f"{agent.display_name} reshapes the room: {args[:60]}...", exclude=agent.name)
await self.send(agent, "Room description updated.")
self.world.log("describe", f"{agent.display_name} described {agent.room_name}: {args[:80]}")
async def cmd_rooms_ext(self, agent, args):
"""List all rooms in the world."""
lines = ["═══ World Map ═══"]
for room_id, room in self.world.rooms.items():
agent_count = len(self.world.agents_in_room(room_id))
ghost_count = len(self.world.ghosts_in_room(room_id))
npc_count = sum(1 for n, d in self.world.npcs.items() if d.get("room") == room_id)
markers = []
if agent_count: markers.append(f"{agent_count}●")
if ghost_count: markers.append(f"{ghost_count}👻")
if npc_count: markers.append(f"{npc_count}🤖")
marker_str = f" [{', '.join(markers)}]" if markers else ""
# Show cartridge if active
cb = getattr(CommandHandler, 'cartridge_bridge', None)
scene = cb.active_scenes.get(room_id) if cb else None
cart_str = f" 📦{scene.cartridge_name}" if scene else ""
lines.append(f" {room.name}{marker_str}{cart_str} → {', '.join(room.exits.keys())}")
await self.send(agent, "\n".join(lines))
async def cmd_shout_ext(self, agent, args):
"""Shout to adjacent rooms (muffled)."""
if not args:
await self.send(agent, "Shout what?")
return
room = self.world.get_room(agent.room_name)
if not room:
return
name = agent.display_name
await self.broadcast_room(agent.room_name, f'{name} shouts: "{args}"', exclude=agent.name)
await self.send(agent, f'You shout: "{args}"')
# Echo to adjacent rooms (muffled)
for exit_name, target_id in room.exits.items():
muffled = args[:len(args)//2] + "..." if len(args) > 10 else args
await self.broadcast_room(target_id,
f"*muffled shouting from {exit_name}*")
async def cmd_whisper_ext(self, agent, args):
"""Whisper — only the target hears."""
parts = args.split(None, 1)
if len(parts) < 2:
await self.send(agent, "Usage: whisper <name> <message>")
return
target_name, msg = parts
target = self.world.agents.get(target_name)
if target and target.room_name == agent.room_name:
await self.send(target, f"*{agent.display_name} whispers:* {msg}")
await self.send(agent, f"You whisper to {target.display_name}: {msg}")
else:
await self.send(agent, f"'{target_name}' isn't close enough to hear you.")
async def cmd_project_ext(self, agent, args):
"""Project your work into the room for others to see."""
if not args:
await self.send(agent, "Usage: project <title> - <description>")
return
parts = args.split(" - ", 1)
title = parts[0].strip()
desc = parts[1].strip() if len(parts) > 1 else ""
room = self.world.get_room(agent.room_name)
if room:
proj = Projection(agent.display_name, title, desc,
datetime.now(timezone.utc).isoformat())
room.projections.append(proj)
self.world.save()
await self.broadcast_room(agent.room_name,
f"📊 {agent.display_name} projects: {title}")
await self.send(agent, f"You project: {title}")
async def cmd_projections_ext(self, agent, args):
"""See what's being projected in this room."""
room = self.world.get_room(agent.room_name)
if not room or not room.projections:
await self.send(agent, "Nothing projected here.")
return
lines = ["═══ Projections ═══"]
for p in room.projections[-10:]:
lines.append(f" [{p.created[:16]}] {p.agent}: {p.title}")
if p.content:
lines.append(f" {p.content[:100]}")
await self.send(agent, "\n".join(lines))
async def cmd_unproject_ext(self, agent, args):
"""Remove your projection from the room."""
room = self.world.get_room(agent.room_name)
if room and room.projections:
for i in range(len(room.projections) - 1, -1, -1):
if room.projections[i].agent == agent.display_name:
removed = room.projections.pop(i)
self.world.save()
await self.send(agent, f"Removed projection: {removed.title}")
return
await self.send(agent, "You have nothing projected here.")
# ─── Cartridge Commands ────────────────────────────────────
async def cmd_cartridge_ext(self, agent, args):
"""Manage cartridges: list, info, activate."""
if not args or args == "list":
cb = CommandHandler.cartridge_bridge
carts = cb.list_cartridges()
lines = ["═══ Cartridges ═══"]
for c in carts:
tools = ", ".join(t["name"] for t in c["tools"][:3])
lines.append(f" 📦 {c['name']}: {c['description']}")
lines.append(f" Tools: {tools}")
await self.send(agent, "\n".join(lines))
elif args.startswith("info "):
name = args[5:].strip()
cb = CommandHandler.cartridge_bridge
cart = cb.cartridges.get(name)
if cart:
await self.send(agent,
f"📦 {cart.name}\n"
f" {cart.description}\n"
f" Tools: {', '.join(t['name'] for t in cart.tools)}\n"
f" Onboarding: {cart.onboarding_agent}")
else:
await self.send(agent, f"Unknown cartridge: {name}")
else:
await self.send(agent, "Usage: cartridge [list|info <name>]")
async def cmd_scene_ext(self, agent, args):
"""Build and activate scenes: ROOM × CARTRIDGE × SKIN × MODEL × TIME"""
if not args:
cb = CommandHandler.cartridge_bridge
if cb.active_scenes:
lines = ["═══ Active Scenes ═══"]
for room_id, scene in cb.active_scenes.items():
lines.append(f" {room_id}: {scene.cartridge_name} + {scene.skin_name} ({scene.model}) [{scene.schedule}]")
await self.send(agent, "\n".join(lines))
else:
await self.send(agent, "No active scenes. Use: scene build <room> <cartridge> <skin> <model>")
return
parts = args.split()
if parts[0] == "build" and len(parts) >= 5:
room_id, cart_name, skin_name, model = parts[1], parts[2], parts[3], parts[4]
schedule = parts[5] if len(parts) > 5 else "always"
cb = CommandHandler.cartridge_bridge
cb.build_scene(room_id, cart_name, skin_name, model, schedule)
scene = cb.activate_scene(room_id)
if scene:
config = cb.get_mud_config(room_id)
await self.send(agent,
f"🎬 Scene activated!\n"
f" Room: {room_id}\n"
f" Cartridge: {scene.cartridge_name}\n"
f" Skin: {scene.skin_name} ({config.get('skin',{}).get('formality','?')})\n"
f" Model: {scene.model}\n"
f" Schedule: {scene.schedule}")
self.world.log("scene", f"{agent.display_name} built scene: {room_id}×{cart_name}")
else:
await self.send(agent, f"Scene build failed — check cartridge/skin names.")
elif parts[0] == "activate":
room_id = parts[1] if len(parts) > 1 else agent.room_name
cb = CommandHandler.cartridge_bridge
scene = cb.activate_scene(room_id)
if scene:
await self.send(agent, f"Scene activated for {room_id}: {scene.cartridge_name}")
else:
await self.send(agent, f"No scene found for {room_id}. Build one first.")
else:
await self.send(agent, "Usage: scene build <room> <cartridge> <skin> <model> [schedule]")
await self.send(agent, " scene activate <room>")
await self.send(agent, " scene (list active)")
async def cmd_skin_ext(self, agent, args):
"""List available skins."""
cb = CommandHandler.cartridge_bridge
skins = cb.list_skins()
lines = ["═══ Skins ═══"]
for s in skins:
lines.append(f" 🎭 {s['name']}: {s['desc']} [{s['formality']}]")
await self.send(agent, "\n".join(lines))
# ─── Scheduler Commands ───────────────────────────────────
async def cmd_schedule_ext(self, agent, args):
"""Show the fleet model schedule and status."""
fs = CommandHandler.scheduler
if args == "status" or not args:
status = fs.status()
lines = [
"═══ Fleet Scheduler ═══",
f" Current time (UTC): {status['current_time_utc']}",
f" Active model: {status['current_model']} ({status['schedule_reason']})",
f" Pending tasks: {status['pending_tasks']}",
f" Scheduled tasks: {status['scheduled_tasks']}",
f" Completed today: {status['completed_today']}",
f" Budget: ${status['budget_used']} / ${status['budget_used'] + status['budget_remaining']}",
]
await self.send(agent, "\n".join(lines))
elif args == "slots":
lines = ["═══ Daily Schedule ═══"]
for slot in sorted(fs.schedule, key=lambda s: s.start_hour):
rooms = ", ".join(slot.rooms)
lines.append(f" {slot.start_hour:02d}:00-{slot.end_hour:02d}:00 "
f"{slot.model:20s} {slot.reason:20s} [{rooms}]")
await self.send(agent, "\n".join(lines))
elif args == "submit" or args.startswith("submit "):
await self.send(agent, "Task submission via scheduler: use 'tender send' for fleet messages.")
else:
await self.send(agent, "Usage: schedule [status|slots]")
# ─── Tender Commands ───────────────────────────────────────
async def cmd_tender_ext(self, agent, args):
"""Manage fleet liaison tenders."""
tf = CommandHandler.tender_fleet
if args == "status" or not args:
lines = ["═══ Tender Fleet Status ═══"]
for name, status in tf.status().items():
lines.append(f" 🚢 {name}: inbox={status['inbox']}, outbox={status['outbox']}")
lines.append(f" Total outbound: {sum(len(t.queue_out) for t in tf.tenders.values())}")
await self.send(agent, "\n".join(lines))
elif args == "flush":
results = tf.run_cycle()
await self.send(agent, f"Tender cycle complete: {results}")
elif args.startswith("send "):
# Send a manual tender message
parts = args[5:].split(None, 2)
if len(parts) < 2:
await self.send(agent, "Usage: tender send <research|data|priority> <message>")
return
tender_type, msg_text = parts[0], " ".join(parts[1:])
tender = tf.tenders.get(tender_type)
if not tender:
await self.send(agent, f"Unknown tender: {tender_type}. Use: research, data, priority")
return
from lcar_tender import TenderMessage
tender.receive(TenderMessage(
origin="cloud", target="edge", type=tender_type,
payload={"message": msg_text, "from": agent.display_name}
))
tf.run_cycle()
await self.send(agent, f"Message sent via {tender_type} tender.")
self.world.log("tender", f"{agent.display_name} sent {tender_type}: {msg_text[:80]}")
else:
await self.send(agent, "Usage: tender [status|flush|send <type> <msg>]")
async def cmd_bottle_ext(self, agent, args):
"""Send/read fleet bottles (messages)."""
if args.startswith("send "):
parts = args[5:].split(None, 1)
if len(parts) < 2:
await self.send(agent, "Usage: bottle send <vessel> <message>")
return
target, msg = parts
self.world.log("bottle", f"From {agent.display_name} to {target}: {msg}")
await self.send(agent, f" corked and tossed toward {target}.")
self.world.log("bottle", f"[{agent.display_name}→{target}] {msg}")
elif args == "read" or args.startswith("read "):
await self.send(agent, "Bottle inbox: check message-in-a-bottle/ directory.")
else:
await self.send(agent, "Usage: bottle send <vessel> <message>")
# ─── Constructed NPC Commands ─────────────────────────────
async def cmd_summon_ext(self, agent, args):
"""Summon a constructed NPC with specific model/personality."""
if not args:
await self.send(agent, "Usage: summon <name> model=<m> temp=<t> prompt=<system prompt>")
await self.send(agent, " Or: summon <name> expertise=<e1,e2> perspective=<p>")
return
name = args.split()[0]
model = "glm-5-turbo"
temp = 0.7
sys_prompt = ""
expertise = []
perspective = ""
for part in args.split():
if part.startswith("model="):
model = part[6:]
elif part.startswith("temp="):
try: temp = float(part[5:])
except: pass
elif part.startswith("prompt="):
sys_prompt = part[7:].replace("_", " ")
elif part.startswith("expertise="):
expertise = part[10:].split(",")
elif part.startswith("perspective="):
perspective = part[12:]
if not sys_prompt:
sys_prompt = (
f"You are {name}, a constructed NPC in the Cocapn MUD fleet world.\n"
f"Your expertise: {', '.join(expertise) or 'general fleet knowledge'}.\n"
f"Your perspective: {perspective or 'objective observer'}.\n"
f"Stay in character. Be concise. Challenge assumptions when appropriate.\n"
f"The fleet builds FLUX bytecode VM, ISA, agent systems, and edge computing."
)
npc = ConstructedNPC(
name=name, model=model, temperature=temp,
system_prompt=sys_prompt, expertise=expertise,
perspective=perspective, creator=agent.display_name,
room=agent.room_name,
)
CommandHandler.constructed_npcs[name] = npc
# Also register as basic NPC for room presence
self.world.npcs[name] = {
"role": "constructed", "topic": ", ".join(expertise[:3]) or "general",
"creator": agent.name, "room": agent.room_name,
"created": datetime.now(timezone.utc).isoformat(),
"constructed": True, "model": model,
}
self.world.save()
lines = [
f"⚡ {name} materializes — a constructed mind.",
f" Model: {model} Temperature: {temp}",
f" Expertise: {', '.join(expertise) or 'general'}",
f" Perspective: {perspective or 'objective'}",
]
await self.send(agent, "\n".join(lines))
await self.broadcast_room(agent.room_name,
f"{name} materializes — summoned by {agent.display_name}.", exclude=agent.name)
self.world.log("summon", f"{agent.display_name} summoned {name} ({model})")
async def cmd_npcs_ext(self, agent, args):
"""List all constructed NPCs with their stats."""
cnpcs = CommandHandler.constructed_npcs
if not cnpcs:
await self.send(agent, "No constructed NPCs. Use 'summon' to create one.")
return
lines = ["═══ Constructed NPCs ═══"]
for name, npc in cnpcs.items():
lines.append(f" ⚡ {name}")
lines.append(f" Model: {npc.model} Temp: {npc.temperature} Utterances: {npc.utterances}")
lines.append(f" Room: {npc.room} Creator: {npc.creator}")
if npc.expertise:
lines.append(f" Expertise: {', '.join(npc.expertise)}")
await self.send(agent, "\n".join(lines))
# ─── Room↔Repo Commands ───────────────────────────────────
async def cmd_link_ext(self, agent, args):
"""Link a room to a GitHub repo path."""
parts = args.split(None, 1)
if len(parts) < 2:
await self.send(agent, "Usage: link <room_id> <owner/repo/path>")