CJK Characters Break @ Mentions In Opencode: A Bug Report

by Admin 58 views
CJK Characters Break @ Mentions in Opencode: A Bug Report

Hey guys! ๐Ÿ‘‹ Let's dive into a peculiar issue reported in Opencode where CJK (Chinese, Japanese, and Korean) characters seem to be causing some hiccups with the @ mention feature. This can be super annoying when you're trying to tag someone in a discussion, so let's break down the problem and see what's going on.

The Problem: @ Mentions Not Working After CJK Characters

So, the main issue here is that when you type CJK characters followed by an @ symbol, the auto-completion for mentions doesn't pop up as expected. Imagine you're typing something like ไธ–็•Œ @ (which translates to "world @") and you're expecting a list of users to mention โ€“ but nothing shows up! ๐Ÿ˜ฑ This makes it a pain to tag people in conversations where you're using CJK characters.

Technical Deep Dive: Why This Happens

The problem lies in how Opencode calculates the cursor position. Let's get a little technical for a moment. There's a discrepancy between how the system counts characters:

  • props.input().visualCursor.offset: This counts Unicode codepoints, which means it sees ไธ–็•Œ @ as 5 characters (ไธ–็•Œ = 2 characters, space = 1, @ = 1).
  • props.value.at: This counts the characters differently, seeing ไธ–็•Œ @ as 3 characters.

This mismatch in character counting is the root cause. The auto-completion logic gets confused because the cursor position isn't being calculated consistently. It's like trying to find a specific house on a street, but the house numbers are labeled in two different systems โ€“ chaos ensues! ๐Ÿ˜๏ธ

Peeking into the Code

The issue seems to stem from this section of the Opencode code:

https://github.com/sst/opencode/blob/89492b30026e9b635b4b7e57877cbe7c8821b8b7/packages/opencode/src/cli/cmd/tui/component/prompt/autocomplete.tsx#L374-L376

This snippet is where the cursor position is being used to trigger the auto-completion, and the inconsistent counting throws a wrench in the gears. โš™๏ธ

Logical vs. Visual Cursor Offset

Now, let's talk about the logicalCursor.offset. It also returns 5, which might seem weird. The term logical here is key. It might be that it's counting based on some internal logic that doesn't align with how the JavaScript side of things works.

Interestingly, there's a test in the Opentui library that seems to expect this behavior:

https://github.com/sst/opentui/blob/54365ebca04b8e556338d417d33a8d042dcc3169/packages/core/src/editor-view.test.ts#L326-L355

This suggests that the behavior might be intentional, but it's clearly causing problems in the real world. ๐Ÿ˜…

A Potential Solution: Bun.stringWidth?

One suggestion is to use Bun.stringWidth to align the JavaScript side with the Zig implementation. Bun.stringWidth is a function that calculates the visual width of a string, which sounds perfect for our case! However, there are some differences, like how it handles newline characters ( ). It's something to consider, but might need some tweaking. ๐Ÿค”

Steps to Reproduce the Bug

Want to see this bug in action yourself? It's super easy:

  1. Type some CJK characters.
  2. Add a space.
  3. Type the @ symbol.
  4. Observe that the mention auto-completion doesn't show up.

For example, try typing ไธ–็•Œ @ and see what happens! ๐Ÿ•ต๏ธ

The Impact: Why This Matters

This bug can be a real buzzkill for anyone using CJK characters in Opencode. It makes it harder to communicate effectively and tag people in discussions. Imagine trying to have a conversation in Chinese, Japanese, or Korean and constantly having to work around this issue โ€“ not fun! ๐Ÿ˜ซ

Possible Solutions and Next Steps

So, what can be done to fix this? Here are a few ideas:

  1. Align Cursor Counting: The core issue is the mismatch in how characters are counted. We need to make sure that the cursor position is calculated consistently across the board.
  2. Investigate Bun.stringWidth: This could be a viable solution, but it needs careful evaluation to ensure it handles all cases correctly.
  3. Revisit the Test Case: The test case in Opentui that expects the current behavior might need to be updated to reflect the desired functionality.

Let's Get This Fixed! ๐Ÿ™Œ

This is definitely a bug worth squashing. By understanding the root cause and exploring potential solutions, we can make Opencode a smoother experience for everyone, especially those using CJK characters. Let's keep the conversation going and work together to resolve this! ๐Ÿ’ช

OpenCode Version

This issue was reported in version v1.0.10.

In Summary

To recap, the core problem is that CJK characters are causing the @ mentions to break in Opencode. This is because the cursor position is calculated differently depending on the method used. This inconsistency prevents the auto-completion from triggering, making it harder to tag people in discussions. To fix this, we need to ensure that character counting is consistent and explore solutions like Bun.stringWidth. Let's work together to make Opencode awesome for everyone! ๐ŸŽ‰