From be3531b3ad4fb1c44841640a093b0aaeadefca31 Mon Sep 17 00:00:00 2001 From: zhi Date: Tue, 21 Apr 2026 12:27:20 +0000 Subject: [PATCH] feat: role-code-mapper script + updated install.sh - scripts/role-code-mapper: echoes role-code for a given role name - install.sh now: 1. Copies ClawRoles to ~/.openclaw/ClawRoles 2. Copies role-code-mapper to ~/.openclaw/bin 3. Installs routers to PrismFacet (if present) 4. Registers rules in PrismFacet rules.json Co-Authored-By: Claude Opus 4.6 (1M context) --- install.sh | 74 ++++++++++++++++++++++------------------ scripts/role-code-mapper | 27 +++++++++++++++ 2 files changed, 68 insertions(+), 33 deletions(-) create mode 100755 scripts/role-code-mapper diff --git a/install.sh b/install.sh index 01c6915..e6414c1 100755 --- a/install.sh +++ b/install.sh @@ -2,12 +2,17 @@ set -euo pipefail # ClawRoles installer -# Installs routers and registers rules into PrismFacet. +# +# 1. Copies ClawRoles to ~/.openclaw/ClawRoles +# 2. Copies role-code-mapper to ~/.openclaw/bin +# 3. Installs routers into PrismFacet +# 4. Registers rules in PrismFacet's rules.json # # Usage: ./install.sh [--prism-dir ] -# --prism-dir: PrismFacet plugin directory (default: ~/.openclaw/plugins/prism-facet) SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +INSTALL_DIR="${HOME}/.openclaw/ClawRoles" +BIN_DIR="${HOME}/.openclaw/bin" PRISM_DIR="${HOME}/.openclaw/plugins/prism-facet" # Parse args @@ -18,51 +23,55 @@ while [[ $# -gt 0 ]]; do esac done +# 1. Copy ClawRoles to ~/.openclaw/ClawRoles +echo "Installing ClawRoles to ${INSTALL_DIR}..." +mkdir -p "$INSTALL_DIR" +cp -r "$SCRIPT_DIR"/roles "$INSTALL_DIR/" +cp -r "$SCRIPT_DIR"/positions "$INSTALL_DIR/" +cp -r "$SCRIPT_DIR"/routers "$INSTALL_DIR/" +cp -r "$SCRIPT_DIR"/scripts "$INSTALL_DIR/" +cp "$SCRIPT_DIR"/install.sh "$INSTALL_DIR/" +echo " ClawRoles copied." + +# 2. Copy role-code-mapper to ~/.openclaw/bin +echo "Installing role-code-mapper..." +mkdir -p "$BIN_DIR" +cp "$SCRIPT_DIR/scripts/role-code-mapper" "$BIN_DIR/role-code-mapper" +chmod +x "$BIN_DIR/role-code-mapper" +echo " role-code-mapper installed to ${BIN_DIR}/" + +# 3. Install routers into PrismFacet if [[ ! -d "$PRISM_DIR" ]]; then - echo "Error: PrismFacet not found at $PRISM_DIR" - echo "Install PrismFacet first, or specify --prism-dir" - exit 1 + echo "Warning: PrismFacet not found at $PRISM_DIR — skipping router/rule registration." + echo "Run again after installing PrismFacet, or specify --prism-dir." + echo "Done (partial)." + exit 0 fi ROUTERS_DIR="${PRISM_DIR}/routers" RULES_FILE="${PRISM_DIR}/rules.json" - mkdir -p "$ROUTERS_DIR" -# 1. Install routers (compile TS to JS via node --experimental-strip-types) -echo "Installing routers..." -for router_ts in "$SCRIPT_DIR"/routers/*.ts; do +echo "Installing routers to PrismFacet..." +for router_ts in "$INSTALL_DIR"/routers/*.ts; do name=$(basename "$router_ts" .ts) - # Use node to strip types and output JS - node --experimental-strip-types -e " - import { readFileSync, writeFileSync } from 'fs'; - const src = readFileSync('${router_ts}', 'utf8'); - // Simple type stripping: remove type annotations - const js = src - .replace(/: \{ agentId: string \}/g, '') - .replace(/: string/g, '') - .replace(/import type .*/g, ''); - writeFileSync('${ROUTERS_DIR}/${name}.js', js); - " 2>/dev/null || { - # Fallback: just copy and strip types manually with sed - sed -E 's/: \{ agentId: string \}//g; s/: string//g; s/import type .*//g' \ - "$router_ts" > "${ROUTERS_DIR}/${name}.js" - } + # Strip TypeScript types for JS output + sed -E 's/: \{ agentId: string \}//g; s/: string//g' \ + "$router_ts" > "${ROUTERS_DIR}/${name}.js" echo " Installed router: $name" done -# 2. Register rules +# 4. Register rules in PrismFacet echo "Registering rules..." -# Load existing rules if [[ -f "$RULES_FILE" ]]; then RULES=$(cat "$RULES_FILE") else RULES="{}" fi -# Scan roles/ and register role:{name} → ROLE.md path -for role_dir in "$SCRIPT_DIR"/roles/*/; do +# Roles +for role_dir in "$INSTALL_DIR"/roles/*/; do role_name=$(basename "$role_dir") role_file="${role_dir}ROLE.md" if [[ -f "$role_file" ]]; then @@ -72,12 +81,12 @@ d = json.load(sys.stdin) d['role:${role_name}'] = '${role_file}' print(json.dumps(d, indent=2)) ") - echo " Registered rule: role:${role_name} → ${role_file}" + echo " rule: role:${role_name}" fi done -# Scan positions/ and register position:{name} → POSITION.md path -for pos_dir in "$SCRIPT_DIR"/positions/*/; do +# Positions +for pos_dir in "$INSTALL_DIR"/positions/*/; do pos_name=$(basename "$pos_dir") pos_file="${pos_dir}POSITION.md" if [[ -f "$pos_file" ]]; then @@ -87,11 +96,10 @@ d = json.load(sys.stdin) d['position:${pos_name}'] = '${pos_file}' print(json.dumps(d, indent=2)) ") - echo " Registered rule: position:${pos_name} → ${pos_file}" + echo " rule: position:${pos_name}" fi done -# Write rules echo "$RULES" > "$RULES_FILE" echo "Done. Restart OpenClaw gateway to apply changes." diff --git a/scripts/role-code-mapper b/scripts/role-code-mapper new file mode 100755 index 0000000..eab1f41 --- /dev/null +++ b/scripts/role-code-mapper @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +set -euo pipefail + +ROLES_DIR="${HOME}/.openclaw/ClawRoles/roles" + +if [[ $# -lt 1 ]]; then + echo "Usage: role-code-mapper " >&2 + exit 1 +fi + +ROLE="$1" +ROLE_FILE="${ROLES_DIR}/${ROLE}/ROLE.md" + +if [[ ! -f "$ROLE_FILE" ]]; then + echo "Error: ROLE.md not found for role '${ROLE}'" >&2 + exit 1 +fi + +# Extract role-code from YAML frontmatter +CODE=$(sed -n '/^---$/,/^---$/{ /^role-code:/{ s/^role-code:[[:space:]]*//; p; q; } }' "$ROLE_FILE") + +if [[ -z "$CODE" ]]; then + echo "Error: role-code not found in ${ROLE_FILE}" >&2 + exit 1 +fi + +echo "$CODE"