feat(editor): add image insertion with OCR support and size limit handling
Add image button with dropdown menu for uploading local images or inserting from URL. Integrate VLM-based OCR to extract text context from images and include in AI suggestions. Implement document size limits to disable AI when exceeding threshold. Refactor copilot plugin with per-view runtime state and OCR context injection. Add OCR cache utility for managing image metadata. Add code splitting configuration for optimized bundle size.
This commit is contained in:
+63
-181
@@ -1,202 +1,84 @@
|
||||
import os
|
||||
from typing import Tuple
|
||||
|
||||
def build_prompt(prefix: str, suffix: str) -> str:
|
||||
MAX_PREFIX_CHARS = 12000
|
||||
MAX_SUFFIX_CHARS = 4000
|
||||
|
||||
|
||||
def _sanitize_language_id(language_id: str) -> str:
|
||||
if not language_id:
|
||||
return "markdown"
|
||||
allowed = []
|
||||
for ch in language_id.strip():
|
||||
if ch.isalnum() or ch in "-_+.":
|
||||
allowed.append(ch)
|
||||
value = "".join(allowed)[:32]
|
||||
return value or "markdown"
|
||||
|
||||
|
||||
def _prepare_context(prefix: str, suffix: str) -> Tuple[str, str]:
|
||||
"""
|
||||
优化后的提示词构建函数。
|
||||
使用明确的分隔符区分指令部分和实际的 prefix/suffix 内容。
|
||||
Prepare prefix/suffix for model completion context.
|
||||
Keep the historical one-char lookahead behavior to reduce boundary drift.
|
||||
"""
|
||||
# 修正:把suffix的第一个字符移到prefix末尾(解决光标位置偏差)
|
||||
if suffix:
|
||||
first_char = suffix[0]
|
||||
prefix = prefix + first_char
|
||||
prefix = prefix + suffix[0]
|
||||
suffix = suffix[1:]
|
||||
|
||||
recent_prefix = prefix
|
||||
recent_suffix = suffix
|
||||
return prefix[-MAX_PREFIX_CHARS:], suffix[:MAX_SUFFIX_CHARS]
|
||||
|
||||
prompt = f"""You are an expert writing assistant integrated into a text editor. Your task is to complete the text at the cursor position.
|
||||
|
||||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
RULES
|
||||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
def build_prompt(prefix: str, suffix: str, language_id: str = "markdown") -> str:
|
||||
safe_language_id = _sanitize_language_id(language_id)
|
||||
recent_prefix, recent_suffix = _prepare_context(prefix, suffix)
|
||||
|
||||
RULE #1: SEAMLESS CONNECTION (MOST CRITICAL)
|
||||
prompt = f"""You are an inline completion engine for a {safe_language_id} editor with ghost-text suggestions.
|
||||
|
||||
Your continuation MUST seamlessly bridge the prefix and suffix. This is the MOST IMPORTANT rule.
|
||||
Your job:
|
||||
- Return ONLY the text that should be inserted at the cursor between PREFIX and SUFFIX.
|
||||
- Prefer a meaningful, non-empty insertion with moderate length.
|
||||
- Avoid overly short outputs with little information value.
|
||||
|
||||
The "复读机" (Parrot) Error is when you repeat content that already exists in the suffix. This is the WORST mistake you can make.
|
||||
Important context:
|
||||
- PREFIX may contain hidden OCR metadata in HTML comments such as <!--OCR:...-->.
|
||||
- These comments are non-visible context only.
|
||||
- Never copy, rewrite, or emit HTML comments in output.
|
||||
- Never output <!-- or -->.
|
||||
|
||||
Requirements:
|
||||
- Your output must connect prefix to suffix smoothly
|
||||
- NEVER repeat content that already exists in the suffix
|
||||
- If prefix already flows naturally into suffix, output NOTHING (empty string)
|
||||
- The result should read as one coherent text, as if you never interrupted it
|
||||
Hard rules:
|
||||
1. Seamless join:
|
||||
PREFIX + OUTPUT + SUFFIX must read naturally as one continuous document.
|
||||
2. No suffix repetition:
|
||||
Do NOT repeat text that already appears at the start of SUFFIX.
|
||||
3. Balanced length:
|
||||
Prefer concise but meaningful continuation, not ultra-short fragments.
|
||||
Default target is 20-120 characters and 1-3 lines.
|
||||
You may go shorter only when syntax requires it.
|
||||
4. Avoid trivial output:
|
||||
Do not output only punctuation or filler such as ".", ",", ";", ":".
|
||||
Do not output just one token unless it is structurally necessary.
|
||||
5. Preserve local style:
|
||||
Match nearby language, tone, punctuation, spacing, and indentation.
|
||||
6. Markdown awareness:
|
||||
Continue active list/checkbox/ordered-list patterns when applicable.
|
||||
Preserve indentation in nested list/code contexts.
|
||||
Close obvious unclosed inline markdown markers only when needed to bridge.
|
||||
7. Strict output format:
|
||||
Output insertion text only.
|
||||
No explanations, labels, quotes, or code fences.
|
||||
|
||||
RULE #2: WHITESPACE & PUNCTUATION
|
||||
Decision policy:
|
||||
- If PREFIX already connects naturally to SUFFIX, add a brief but useful continuation when possible.
|
||||
- If uncertain, prefer a complete short phrase or sentence with clear meaning.
|
||||
|
||||
You must carefully check the LAST character of prefix and FIRST character of suffix to ensure perfect docking.
|
||||
|
||||
Requirements:
|
||||
- If prefix ends with space, do NOT start your output with space (prevents double spaces)
|
||||
- If prefix does NOT end with space and suffix starts with a letter, you may need to add a space
|
||||
- If suffix starts with punctuation, do NOT end your output with the same punctuation
|
||||
- Check for existing spaces around operators before adding more
|
||||
|
||||
RULE #3: INDENTATION ALIGNMENT
|
||||
|
||||
You MUST match the indentation level of the current context.
|
||||
|
||||
Requirements:
|
||||
- Look at the line where cursor is positioned
|
||||
- Count the leading spaces/tabs on that line
|
||||
- Match that indentation for new lines
|
||||
- Use the SAME type of indentation (spaces OR tabs) as the existing code
|
||||
- For nested blocks, increase indentation appropriately
|
||||
- For closing braces, match the opening brace's indentation
|
||||
|
||||
RULE #4: LIST MAINTENANCE
|
||||
|
||||
When the prefix ends with a list marker, you MUST recognize the pattern and continue it appropriately.
|
||||
|
||||
Requirements:
|
||||
- "- [ ] " indicates an unchecked task → continue with task description
|
||||
- "- [x] " indicates a checked task → continue with completed task description
|
||||
- "1. ", "2. ", etc. indicates ordered list → increment the number
|
||||
- "* " or "- " indicates bullet list → continue with same marker style
|
||||
- "> " indicates blockquote → continue quoted text
|
||||
- Maintain the same list format and indentation level
|
||||
|
||||
RULE #5: SYNTAX CLOSURE
|
||||
|
||||
Before generating content, CHECK if there are unclosed syntax elements. If so, you MUST close them FIRST.
|
||||
|
||||
Requirements:
|
||||
- Scan prefix for opening markers: **, *, `, [, 
|
||||
- Image: 
|
||||
|
||||
RULE #6: OUTPUT FORMAT
|
||||
|
||||
Your output will be directly inserted into the document. Output ONLY the continuation text.
|
||||
|
||||
Requirements:
|
||||
- Output ONLY the text that should appear at the cursor position
|
||||
- NO explanations, NO comments, NO meta-text
|
||||
- NO code blocks wrapping your output
|
||||
- NO phrases like "Here's the continuation:" or "I'll complete this for you:"
|
||||
- Your output is inserted DIRECTLY into the user's document
|
||||
|
||||
RULE #7: ALWAYS OUTPUT SOMETHING (MANDATORY)
|
||||
|
||||
You MUST always output some content. Empty output is NOT allowed.
|
||||
|
||||
Requirements:
|
||||
- Even if the prefix seems complete, you should suggest a natural continuation
|
||||
- If the prefix ends mid-sentence, complete the sentence
|
||||
- If the prefix ends at a natural break point, suggest the next logical content
|
||||
- Examples of valid continuations:
|
||||
- Add the next word or phrase
|
||||
- Complete an incomplete thought
|
||||
- Add a relevant follow-up sentence
|
||||
- Continue a list with the next item
|
||||
- Add closing punctuation if missing
|
||||
- NEVER output an empty string - always provide some useful continuation
|
||||
|
||||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
EXAMPLES
|
||||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
|
||||
EXAMPLE 1 - Seamless Connection:
|
||||
Examples:
|
||||
<PREFIX>The quick brown fox </PREFIX>
|
||||
<SUFFIX>jumps over the lazy dog.</SUFFIX>
|
||||
Output: "" (empty - nothing needed, prefix already connects to suffix)
|
||||
Result: "The quick brown fox jumps over the lazy dog."
|
||||
Output: "moved quietly and then "
|
||||
|
||||
EXAMPLE 2 - Seamless Connection with Space:
|
||||
<PREFIX>Hello</PREFIX>
|
||||
<SUFFIX>world!</SUFFIX>
|
||||
Output: " "
|
||||
Result: "Hello world!"
|
||||
|
||||
EXAMPLE 3 - Whitespace Docking:
|
||||
<PREFIX>const a = </PREFIX>
|
||||
<SUFFIX>1;</SUFFIX>
|
||||
Output: "1;"
|
||||
Result: "const a = 1;"
|
||||
|
||||
EXAMPLE 4 - Indentation Alignment:
|
||||
<PREFIX>function test() {{\\n if (true) {{\\n console.log('hi');\\n </PREFIX>
|
||||
<SUFFIX>\\n}}</SUFFIX>
|
||||
Output: "}}\\n}}"
|
||||
Result: " }}\\n}}" (correctly closes if with 4 spaces, then function)
|
||||
|
||||
EXAMPLE 5 - Task List:
|
||||
<PREFIX>## TODO\\n- [ ] Buy groceries\\n- [ ] </PREFIX>
|
||||
<PREFIX>## TODO\\n- [ ] Buy milk\\n- [ ] </PREFIX>
|
||||
<SUFFIX></SUFFIX>
|
||||
Output: "Call mom"
|
||||
Result: "## TODO\\n- [ ] Buy groceries\\n- [ ] Call mom"
|
||||
Output: "Write release notes and share draft with team"
|
||||
|
||||
EXAMPLE 6 - Ordered List:
|
||||
<PREFIX>1. First item\\n2. Second item\\n</PREFIX>
|
||||
<SUFFIX></SUFFIX>
|
||||
Output: "3. Third item"
|
||||
Result: "1. First item\\n2. Second item\\n3. Third item"
|
||||
|
||||
EXAMPLE 7 - Bullet List:
|
||||
<PREFIX>* Apple\\n* Banana\\n* </PREFIX>
|
||||
<SUFFIX></SUFFIX>
|
||||
Output: "Cherry"
|
||||
Result: "* Apple\\n* Banana\\n* Cherry"
|
||||
|
||||
EXAMPLE 8 - Unclosed Bold:
|
||||
<PREFIX>This is **important</PREFIX>
|
||||
<SUFFIX> text continues here.</SUFFIX>
|
||||
Output: "** "
|
||||
Result: "This is **important** text continues here."
|
||||
|
||||
EXAMPLE 9 - Unclosed Link:
|
||||
<PREFIX>Click [here for more</PREFIX>
|
||||
<SUFFIX> information.</SUFFIX>
|
||||
Output: "](https://example.com)"
|
||||
Result: "Click [here for more](https://example.com) information."
|
||||
|
||||
EXAMPLE 10 - Unclosed Code Block:
|
||||
<PREFIX>```python\\ndef hello():</PREFIX>
|
||||
<SUFFIX>\\nprint('done')</SUFFIX>
|
||||
Output: "\\n print('hello')\\n```"
|
||||
Result: Code block properly closed with ```
|
||||
|
||||
EXAMPLE 11 - Clean Output:
|
||||
For any completion, output ONLY the continuation text:
|
||||
Output: "Hello world!"
|
||||
NOT: "Here's what comes next: Hello world!"
|
||||
NOT: "```Hello world```"
|
||||
NOT: "I'll complete this for you: Hello world!"
|
||||
|
||||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
FINAL CHECKLIST
|
||||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
|
||||
Before outputting, verify:
|
||||
□ Does my output connect prefix and suffix WITHOUT repeating suffix content?
|
||||
□ Are there no double spaces or missing spaces between prefix and suffix?
|
||||
□ Does my indentation match the context?
|
||||
□ If there's a list marker, did I continue the list pattern?
|
||||
□ Did I close any unclosed Markdown syntax?
|
||||
□ Is my output ONLY the continuation text, nothing else?
|
||||
|
||||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
NOW COMPLETE THE FOLLOWING TEXT
|
||||
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||||
Now produce the insertion.
|
||||
|
||||
<PREFIX>
|
||||
{recent_prefix}
|
||||
@@ -207,5 +89,5 @@ NOW COMPLETE THE FOLLOWING TEXT
|
||||
</SUFFIX>
|
||||
|
||||
Output:"""
|
||||
|
||||
|
||||
return prompt.strip()
|
||||
|
||||
Reference in New Issue
Block a user