install-korean-ime-fix.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. # opencode Korean IME Fix Installer
  4. # https://github.com/anomalyco/opencode/issues/14371
  5. #
  6. # Patches opencode to prevent Korean (and other CJK) IME last character
  7. # truncation when pressing Enter in Kitty and other terminals.
  8. #
  9. # Usage:
  10. # curl -fsSL https://raw.githubusercontent.com/claudianus/opencode/fix-zhipuai-coding-plan-thinking/patches/install-korean-ime-fix.sh | bash
  11. # # or from a cloned repo:
  12. # ./patches/install-korean-ime-fix.sh
  13. RED='\033[0;31m'
  14. GREEN='\033[0;32m'
  15. ORANGE='\033[38;5;214m'
  16. MUTED='\033[0;2m'
  17. NC='\033[0m'
  18. OPENCODE_DIR="${OPENCODE_DIR:-$HOME/.opencode}"
  19. OPENCODE_SRC="${OPENCODE_SRC:-$HOME/.opencode-src}"
  20. FORK_REPO="${FORK_REPO:-https://github.com/claudianus/opencode.git}"
  21. FORK_BRANCH="${FORK_BRANCH:-fix-zhipuai-coding-plan-thinking}"
  22. info() { echo -e "${MUTED}$*${NC}"; }
  23. warn() { echo -e "${ORANGE}$*${NC}"; }
  24. err() { echo -e "${RED}$*${NC}" >&2; }
  25. ok() { echo -e "${GREEN}$*${NC}"; }
  26. need() {
  27. if ! command -v "$1" >/dev/null 2>&1; then
  28. err "Error: $1 is required but not installed."
  29. exit 1
  30. fi
  31. }
  32. need git
  33. need bun
  34. # ── 1. Clone or update fork ────────────────────────────────────────────
  35. if [ -d "$OPENCODE_SRC/.git" ]; then
  36. info "Updating existing source at $OPENCODE_SRC ..."
  37. git -C "$OPENCODE_SRC" fetch origin "$FORK_BRANCH"
  38. git -C "$OPENCODE_SRC" checkout "$FORK_BRANCH"
  39. git -C "$OPENCODE_SRC" reset --hard "origin/$FORK_BRANCH"
  40. else
  41. info "Cloning fork (shallow) to $OPENCODE_SRC ..."
  42. git clone --depth 1 --branch "$FORK_BRANCH" "$FORK_REPO" "$OPENCODE_SRC"
  43. fi
  44. # ── 2. Verify the IME fix is present in source ────────────────────────
  45. PROMPT_FILE="$OPENCODE_SRC/packages/opencode/src/cli/cmd/tui/component/prompt/index.tsx"
  46. if [ ! -f "$PROMPT_FILE" ]; then
  47. err "Prompt file not found: $PROMPT_FILE"
  48. exit 1
  49. fi
  50. if grep -q "setTimeout(() => setTimeout" "$PROMPT_FILE"; then
  51. ok "IME fix already present in source."
  52. else
  53. warn "IME fix not found. Applying patch ..."
  54. # Apply the fix: replace onSubmit={submit} with double-deferred version
  55. sed -i 's|onSubmit={submit}|onSubmit={() => {\n // IME: double-defer so the last composed character (e.g. Korean\n // hangul) is flushed to plainText before we read it for submission.\n setTimeout(() => setTimeout(() => submit(), 0), 0)\n }}|' "$PROMPT_FILE"
  56. if grep -q "setTimeout(() => setTimeout" "$PROMPT_FILE"; then
  57. ok "Patch applied."
  58. else
  59. err "Failed to apply patch. The source may have changed."
  60. exit 1
  61. fi
  62. fi
  63. # ── 3. Install dependencies ────────────────────────────────────────────
  64. info "Installing dependencies (this may take a minute) ..."
  65. cd "$OPENCODE_SRC"
  66. bun install --frozen-lockfile 2>/dev/null || bun install
  67. # ── 4. Build (current platform only) ──────────────────────────────────
  68. info "Building opencode for current platform ..."
  69. cd "$OPENCODE_SRC/packages/opencode"
  70. bun run build --single
  71. # ── 5. Install binary ──────────────────────────────────────────────────
  72. mkdir -p "$OPENCODE_DIR/bin"
  73. PLATFORM=$(uname -s | tr '[:upper:]' '[:lower:]')
  74. ARCH=$(uname -m)
  75. [ "$ARCH" = "aarch64" ] && ARCH="arm64"
  76. [ "$ARCH" = "x86_64" ] && ARCH="x64"
  77. [ "$PLATFORM" = "darwin" ] && true
  78. [ "$PLATFORM" = "linux" ] && true
  79. BUILT_BINARY="$OPENCODE_SRC/packages/opencode/dist/opencode-${PLATFORM}-${ARCH}/bin/opencode"
  80. if [ ! -f "$BUILT_BINARY" ]; then
  81. BUILT_BINARY=$(find "$OPENCODE_SRC/packages/opencode/dist" -name "opencode" -type f -executable 2>/dev/null | head -1)
  82. fi
  83. if [ -f "$BUILT_BINARY" ]; then
  84. if [ -f "$OPENCODE_DIR/bin/opencode" ]; then
  85. cp "$OPENCODE_DIR/bin/opencode" "$OPENCODE_DIR/bin/opencode.bak.$(date +%Y%m%d%H%M%S)"
  86. fi
  87. cp "$BUILT_BINARY" "$OPENCODE_DIR/bin/opencode"
  88. chmod +x "$OPENCODE_DIR/bin/opencode"
  89. ok "Installed to $OPENCODE_DIR/bin/opencode"
  90. else
  91. err "Build failed - binary not found in dist/"
  92. info "Try running manually:"
  93. echo " cd $OPENCODE_SRC/packages/opencode && bun run build --single"
  94. exit 1
  95. fi
  96. echo ""
  97. ok "Done! Korean IME fix is now active."
  98. echo ""
  99. info "To uninstall and revert to the official release:"
  100. echo " curl -fsSL https://opencode.ai/install | bash"
  101. echo ""
  102. info "To update (re-pull and rebuild):"
  103. echo " $0"