Build a SuperClaude Framework Workflow with Commands, Agents, Modes, and Session Memory


class SuperClaude:
   """
   Mimics what Claude Code does at session start:
     • reads Markdown behavior files for the active command/agent/modes,
     • concatenates them into one system prompt,
     • runs the conversation through the Anthropic API.
   """
   BASE_SYSTEM = textwrap.dedent("""
   You are operating inside the SuperClaude Framework — a structured
   development platform layered on top of Claude. Treat the 
   block as your behavioral contract for this turn. If a behavioral
   instruction conflicts with the user request, prefer the instruction.
   Begin every answer with a short '▶ Active context:' line that names the
   command / agent / modes currently in effect.
   """).strip()
   def __init__(self, model=MODEL):
       self.model, self.history = model, []
   def _load(self, kind, name):
       if not name: return ""
       p = ASSETS[kind].get(name.lower())
       return p.read_text(encoding="utf-8", errors="ignore") if p else \
              f"# {kind}:{name} (not found — using inline defaults)"
   def _system(self, command=None, agent=None, modes=None, extra=None):
       parts = [self.BASE_SYSTEM, ""]
       if command: parts += [f"## Command /sc:{command}", self._load("commands", command)]
       if agent:   parts += [f"## Agent {agent}",         self._load("agents",   agent)]
       for m in (modes or []):
           parts += [f"## Mode {m}", self._load("modes", m)]
       if extra:   parts += ["## Extra directives", extra]
       parts.append("")
       return "\n\n".join(parts)
   def run(self, prompt, *, command=None, agent=None, modes=None, extra=None,
           max_tokens=1800, stream=True, remember=True):
       sys_prompt = self._system(command, agent, modes, extra)
       msgs = self.history + [{"role": "user", "content": prompt}]
       console.rule(
           f"[bold cyan]► /sc:{command or '—'}  agent={agent or '—'}  modes={modes or []}"
       )
       console.print(Panel(prompt, title="USER", border_style="blue"))
       console.print("[bold green]ASSISTANT ↓[/bold green]")
       text = ""
       try:
           if stream:
               with client.messages.stream(
                   model=self.model, max_tokens=max_tokens,
                   system=sys_prompt, messages=msgs,
               ) as s:
                   for chunk in s.text_stream:
                       sys.stdout.write(chunk); sys.stdout.flush()
                       text += chunk
                   print()
           else:
               r = client.messages.create(
                   model=self.model, max_tokens=max_tokens,
                   system=sys_prompt, messages=msgs,
               )
               text = "".join(b.text for b in r.content if b.type == "text")
               console.print(text)
       except Exception as e:
           console.print(f"[red]API error: {e}[/red]")
           return ""
       if remember:
           self.history += [{"role": "user", "content": prompt},
                            {"role": "assistant", "content": text}]
       return text
   def save(self, path="/content/sc_session.json", note=""):
       Path(path).write_text(json.dumps(
           {"meta": {"note": note, "saved_at": time.time(), "model": self.model},
            "history": self.history}, indent=2))
       console.print(f"💾 Session → {path}", style="bold yellow")
   def load(self, path="/content/sc_session.json"):
       d = json.loads(Path(path).read_text())
       self.history = d["history"]
       console.print(f"📂 Loaded {len(self.history)//2} prior turns", style="bold yellow")
sc = SuperClaude()



Source link

  • Related Posts

    Tencent Open-Sources TencentDB Agent Memory: A 4-Tier Local Memory Pipeline for AI Agents

    Tencent has released TencentDB Agent Memory, an open-source memory system for AI agents. The project ships under the MIT license. It targets a problem familiar to anyone shipping long-horizon agents:…

    Nous Research Releases Contrastive Neuron Attribution (CNA): Sparse MLP Circuit Steering Without SAE Training or Weight Modification

    Instruction-tuned language models refuse harmful requests. But which part of the model is actually responsible — and how does that mechanism get installed during training? A new research from Nous…

    Leave a Reply

    Your email address will not be published. Required fields are marked *