Skip to main content

CLI Examples

Practical examples and common use cases for lattice-ctl.

Daily Workflows

Start your day

bash
#!/bin/bash
# morning-startup.sh

# Start daemon
lattice-ctl daemon start

# Wait for daemon to be ready
sleep 2

# Check health
lattice-ctl ping

# List today's sessions
lattice-ctl sessions list --format json | \
  jq '.data.sessions[] | select(.createdAt | startswith("2024-01-15"))'

echo "✓ Lattice ready"

###End your day

bash
#!/bin/bash
# evening-shutdown.sh

# Stop all running agents
lattice-ctl agents list --format json | \
  jq -r '.data.agents[] | select(.status == "running") | .id' | \
while read agent_id; do
  echo "Stopping agent: $agent_id"
  lattice-ctl agents stop $agent_id
done

# Graceful shutdown
lattice-ctl shutdown

echo "✓ Lattice shut down"

Automation Scripts

Auto-restart on failure

bash
#!/bin/bash
# daemon-monitor.sh

while true; do
  if ! lattice-ctl ping > /dev/null 2>&1; then
    echo "$(date): Daemon not responding, restarting..."
    lattice-ctl daemon stop
    sleep 2
    lattice-ctl daemon start
    sleep 5
  fi
  sleep 10
done

Health check with notifications

bash
#!/bin/bash
# health-monitor.sh

STATUS=$(lattice-ctl health --format json)
HEALTH=$(echo $STATUS | jq -r '.data.status')

if [ "$HEALTH" != "healthy" ]; then
  echo "⚠️  Lattice unhealthy!"

  # Show which components are failing
  echo $STATUS | jq '.data.components'

  # Send notification (macOS)
  osascript -e 'display notification "Lattice daemon unhealthy" with title "Lattice Alert"'
fi

Session backup

bash
#!/bin/bash
# backup-sessions.sh

BACKUP_DIR="$HOME/lattice-backups"
mkdir -p "$BACKUP_DIR"

DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="$BACKUP_DIR/sessions_$DATE.json"

# Export all sessions
lattice-ctl sessions list --format json > "$BACKUP_FILE"

echo "✓ Sessions backed up to $BACKUP_FILE"

# Keep only last 7 days
find "$BACKUP_DIR" -name "sessions_*.json" -mtime +7 -delete

Integration Examples

With jq (JSON processing)

bash
# Count running agents
lattice-ctl agents list --format json | jq '.data.agents | length'

# Get specific session title
lattice-ctl sessions get session_123abc --format json | jq -r '.data.title'

# Filter sessions by workspace
lattice-ctl sessions list --format json | \
  jq '.data.sessions[] | select(.workspaceId == "workspace_abc123")'

# Get daemon uptime
lattice-ctl status --format json | jq '.data.uptimeSeconds'

With watch (monitoring)

bash
# Watch daemon status
watch -n 5 lattice-ctl status

# Monitor agent count
watch -n 2 'lattice-ctl status --format json | jq .data.activeAgents'

# Watch specific session
watch -n 1 'lattice-ctl sessions get session_123abc'

With cron (scheduling)

bash
# crontab -e

# Health check every hour
0 * * * * /path/to/health-monitor.sh

# Backup sessions daily at 2 AM
0 2 * * * /path/to/backup-sessions.sh

# Restart daemon weekly (Sunday 3 AM)
0 3 * * 0 /usr/local/bin/lattice-ctl daemon stop && sleep 5 && /usr/local/bin/lattice-ctl daemon start

Development Workflows

Test provider setup

bash
#!/bin/bash
# test-providers.sh

PROVIDERS=("ollama" "anthropic" "openai")

for provider in "${PROVIDERS[@]}"; do
  echo "Testing $provider..."

  if lattice-ctl provider test $provider > /dev/null 2>&1; then
    echo "  ✓ $provider OK"
  else
    echo "  ✗ $provider FAILED"
  fi
done

Debug session issues

bash
#!/bin/bash
# debug-session.sh

SESSION_ID=$1

if [ -z "$SESSION_ID" ]; then
  echo "Usage: $0 <session_id>"
  exit 1
fi

echo "=== Session Details ==="
lattice-ctl sessions get $SESSION_ID

echo -e "\n=== Raw JSON ==="
lattice-ctl sessions get $SESSION_ID --format json | jq '.'

echo -e "\n=== Message Count ==="
lattice-ctl sessions get $SESSION_ID --format json | jq '.data.messageCount'

Load testing

bash
#!/bin/bash
# load-test.sh

# Create 10 sessions concurrently
for i in {1..10}; do
  (
    SESSION=$(lattice-ctl sessions create \
      --workspace-id default \
      --title "Load Test $i" \
      --format json | jq -r '.data.id')

    echo "Created session $i: $SESSION"
  ) &
done

wait
echo "✓ All sessions created"

# Check daemon status
lattice-ctl status

Error Handling

Robust daemon start

bash
#!/bin/bash
# safe-start.sh

MAX_RETRIES=3
RETRY_DELAY=5

for i in $(seq 1 $MAX_RETRIES); do
  echo "Attempt $i/$MAX_RETRIES: Starting daemon..."

  if lattice-ctl daemon start; then
    # Wait for daemon to be ready
    sleep 2

    if lattice-ctl ping > /dev/null 2>&1; then
      echo "✓ Daemon started successfully"
      exit 0
    fi
  fi

  echo "Failed, retrying in ${RETRY_DELAY}s..."
  sleep $RETRY_DELAY
done

echo "✗ Failed to start daemon after $MAX_RETRIES attempts"
exit 1

Graceful shutdown with timeout

bash
#!/bin/bash
# graceful-shutdown.sh

TIMEOUT=30

echo "Sending shutdown command..."
lattice-ctl shutdown &

# Wait for daemon to stop
for i in $(seq 1 $TIMEOUT); do
  if ! lattice-ctl ping > /dev/null 2>&1; then
    echo "✓ Daemon shut down gracefully"
    exit 0
  fi
  sleep 1
done

echo "⚠️  Graceful shutdown timed out, forcing stop"
lattice-ctl daemon stop

CI/CD Integration

GitHub Actions

yaml
name: Test with Lattice

on: [push]

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Install Lattice
        run: |
          curl -fsSL https://uselovelace.com/lattice/install.sh | sh

      - name: Start daemon
        run: |
          lattice-ctl daemon start
          sleep 5
          lattice-ctl ping

      - name: Run tests
        run: |
          # Your tests here
          lattice-ctl sessions create --workspace-id test

      - name: Cleanup
        if: always()
        run: lattice-ctl daemon stop

Docker health check

dockerfile
FROM ubuntu:22.04

# Install Lattice
RUN curl -fsSL https://uselovelace.com/lattice/install.sh | sh

# Start daemon
CMD ["lattice-daemon", "--foreground"]

# Health check
HEALTHCHECK --interval=30s --timeout=3s \
  CMD lattice-ctl ping || exit 1

Shell Aliases

Add to ~/.bashrc or ~/.zshrc:

bash
# Lattice shortcuts
alias ld='lattice-ctl daemon'
alias ls-start='lattice-ctl daemon start'
alias ls-stop='lattice-ctl daemon stop'
alias ls-status='lattice-ctl status'
alias ls-ping='lattice-ctl ping'

# Session shortcuts
alias ls-sessions='lattice-ctl sessions list'
alias ls-agents='lattice-ctl agents list'

# Quick provider test
alias ls-test-ollama='lattice-ctl provider test ollama'
alias ls-test-anthropic='lattice-ctl provider test anthropic'

Related