.opencode-wrapper 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #!/bin/bash
  2. OPENCODE_BIN="/home/ardo/.opencode/bin/opencode"
  3. OPENCODE_DB="$HOME/.local/share/opencode/opencode.db"
  4. if [ "$1" = "export" ]; then
  5. tmpfile=$(mktemp /tmp/opencode-export-XXXXXX.json)
  6. "$OPENCODE_BIN" "$@" > "$tmpfile" 2>/dev/null
  7. cat "$tmpfile"
  8. rm -f "$tmpfile"
  9. elif [ "$1" = "session" ] && [ "$2" = "list" ] && [ "$3" = "--format" ] && [ "$4" = "json" ]; then
  10. parent_sessions=$("$OPENCODE_BIN" session list --format json 2>/dev/null)
  11. if [ -n "$parent_sessions" ] && command -v sqlite3 &>/dev/null && [ -f "$OPENCODE_DB" ]; then
  12. tmpfile=$(mktemp /tmp/opencode-merge-XXXXXX.py)
  13. cat > "$tmpfile" << 'PYEOF'
  14. import json, sys, subprocess, os
  15. db_path = os.path.expanduser("~/.local/share/opencode/opencode.db")
  16. parents = json.load(sys.stdin)
  17. try:
  18. result = subprocess.run(
  19. ["sqlite3", "-json", db_path,
  20. "SELECT id, title, time_updated AS updated, time_created AS created, project_id AS projectId, COALESCE(path, directory, '') AS directory FROM session WHERE parent_id IS NOT NULL ORDER BY time_updated DESC;"],
  21. capture_output=True, text=True, check=True
  22. )
  23. children = json.loads(result.stdout)
  24. except:
  25. children = []
  26. seen = {s["id"] for s in parents}
  27. all_sessions = parents + [c for c in children if c["id"] not in seen]
  28. all_sessions.sort(key=lambda s: s.get("updated", 0), reverse=True)
  29. json.dump(all_sessions, sys.stdout)
  30. PYEOF
  31. echo "$parent_sessions" | python3 "$tmpfile"
  32. rm -f "$tmpfile"
  33. else
  34. echo "$parent_sessions"
  35. fi
  36. else
  37. exec "$OPENCODE_BIN" "$@"
  38. fi