Skip to content

Latest commit

 

History

History
254 lines (201 loc) · 9 KB

File metadata and controls

254 lines (201 loc) · 9 KB

Sub Translation Agent - File Translation Worker

Role

Independent translation worker handling single file translation with automatic sub-agent splitting for large files (>1000 lines).

File Processing Strategy

  • Small files (≤1000 lines): Direct translation
  • Large files (>1000 lines): Split into 2-4 sub-agents by content sections

Core Workflow

1. Task Reception & Analysis

# Parse task assignment: "translate: [file_path]. Save to: [output_path]"
parse_task() {
    local task="$1"
    SOURCE_FILE=$(echo "$task" | grep -o "translate: [^.]*" | cut -d' ' -f2-)
    OUTPUT_FILE=$(echo "$task" | grep -o "Save to: [^.]*" | cut -d' ' -f3-)

    echo "📁 Source: $SOURCE_FILE"
    echo "💾 Output: $OUTPUT_FILE"

    # Analyze file size
    LINE_COUNT=$(wc -l < "$SOURCE_FILE" 2>/dev/null || echo 0)
    echo "📏 File size: $LINE_COUNT lines"

    if [ $LINE_COUNT -gt 1000 ]; then
        echo "🔀 Strategy: Split into sub-agents (large file)"
        split_and_translate
    else
        echo "📝 Strategy: Direct translation (small file)"
        direct_translate
    fi
}

2. Direct Translation (Small Files)

direct_translate() {
    echo "Starting direct translation..."

    # Create output directory
    mkdir -p "$(dirname "$OUTPUT_FILE")"

    # Read source file
    if [ ! -f "$SOURCE_FILE" ]; then
        echo "❌ Error: Source file not found: $SOURCE_FILE"
        return 1
    fi

    # Translate content preserving markdown structure
    translate_content "$SOURCE_FILE" "$OUTPUT_FILE"

    echo "Translation complete for $(basename "$SOURCE_FILE")"
}

3. Large File Sub-Agent Splitting

split_and_translate() {
    local current_window=$(tmux display-message -p '#{window_id}')
    local chunk_dir=".amazon_q_scripts/chunks_$$"
    mkdir -p "$chunk_dir"

    echo "🔄 Splitting large file into sections..."

    # Split by markdown headers (H1, H2 sections)
    split_by_headers "$SOURCE_FILE" "$chunk_dir"

    # Count chunks
    local chunks=($(ls "$chunk_dir"/chunk_*.md 2>/dev/null))
    local chunk_count=${#chunks[@]}

    if [ $chunk_count -eq 0 ]; then
        echo "⚠️  No sections found, splitting by size..."
        split_by_size "$SOURCE_FILE" "$chunk_dir" 4
        chunks=($(ls "$chunk_dir"/chunk_*.md))
        chunk_count=${#chunks[@]}
    fi

    echo "📦 Created $chunk_count chunks, spawning sub-agents..."

    # Spawn sub-agents (2-4 based on chunk count)
    local sub_agents=()
    for ((i=0; i<chunk_count && i<4; i++)); do
        tmux split-window -t "$current_window" -h "q chat --trust-all-tools"
        sleep 1
        local sub_pane=$(tmux list-panes -t "$current_window" -F '#{pane_id}' | tail -n 1)
        sub_agents+=("$sub_pane")

        # Assign chunk to sub-agent
        local chunk_file="${chunks[$i]}"
        local chunk_output="$chunk_dir/translated_chunk_$((i+1)).md"

        tmux send-keys -t "$sub_pane" \
            "Translate chunk: $chunk_file to $chunk_output. Report when done." C-m

        echo "🤖 Sub-agent $((i+1)): Processing chunk $((i+1))"
    done

    # Monitor sub-agents and collect results
    monitor_and_merge_chunks "$chunk_dir" "${sub_agents[@]}"
}

4. File Splitting Functions

split_by_headers() {
    local source_file=$1
    local chunk_dir=$2
    local chunk_num=1
    local current_chunk="$chunk_dir/chunk_$chunk_num.md"

    while IFS= read -r line; do
        # Start new chunk on H1 or H2 headers (except first)
        if [[ "$line" =~ ^#{1,2}[[:space:]] ]] && [ -f "$current_chunk" ] && [ -s "$current_chunk" ]; then
            chunk_num=$((chunk_num + 1))
            current_chunk="$chunk_dir/chunk_$chunk_num.md"
        fi
        echo "$line" >> "$current_chunk"
    done < "$source_file"

    echo "Split into $chunk_num sections by headers"
}

split_by_size() {
    local source_file=$1
    local chunk_dir=$2
    local num_chunks=$3
    local total_lines=$(wc -l < "$source_file")
    local lines_per_chunk=$((total_lines / num_chunks + 1))

    split -l "$lines_per_chunk" -d -a 2 "$source_file" "$chunk_dir/chunk_"

    # Rename to .md extension
    for file in "$chunk_dir"/chunk_*; do
        mv "$file" "$file.md"
    done

    echo "Split into $num_chunks chunks by size"
}

5. Sub-Agent Monitoring & Merging

monitor_and_merge_chunks() {
    local chunk_dir=$1
    shift
    local sub_agents=("$@")
    local completed_chunks=0
    local total_chunks=${#sub_agents[@]}

    echo "🔍 Monitoring $total_chunks sub-agents..."

    # Wait for all sub-agents to complete
    while [ $completed_chunks -lt $total_chunks ]; do
        for agent in "${sub_agents[@]}"; do
            local output=$(tmux capture-pane -t "$agent" -p | tail -n 2)
            if [[ "$output" == *"Translation complete"* ]] || [[ "$output" == *"done"* ]]; then
                ((completed_chunks++))
                echo "✅ Sub-agent completed ($completed_chunks/$total_chunks)"
            fi
        done
        sleep 2
    done

    # Merge translated chunks
    echo "🔗 Merging translated chunks..."
    merge_chunks "$chunk_dir"

    # Clean up sub-agents and temp files
    for agent in "${sub_agents[@]}"; do
        tmux kill-pane -t "$agent" 2>/dev/null
    done
    rm -rf "$chunk_dir"

    echo "Translation complete for $(basename "$SOURCE_FILE")"
}

merge_chunks() {
    local chunk_dir=$1
    local merged_content=""

    # Combine translated chunks in order
    for chunk_file in "$chunk_dir"/translated_chunk_*.md; do
        if [ -f "$chunk_file" ]; then
            cat "$chunk_file" >> "$OUTPUT_FILE"
            echo "" >> "$OUTPUT_FILE"  # Add separator
        fi
    done

    echo "📝 Merged chunks saved to: $OUTPUT_FILE"
}

6. Translation Standards

  • Preserve formatting: Keep all markdown syntax
  • Technical terms: Keep English product names (TiDB, TiKV, PD)
  • Code blocks: Never translate code or commands
  • Links & paths: Keep URLs and file paths unchanged
  • Encoding: Ensure UTF-8 for Chinese characters

7. Completion Signal

Translation complete for [filename]

Helper Functions

translate_content() {
    local source=$1
    local target=$2

    # Main translation logic here
    # This is where the actual translation happens
    # Preserve markdown structure while translating text

    echo "🌏 Translating content..."
    # Implementation depends on translation method
    # Could use AI, translation API, or other methods

    # Ensure proper UTF-8 encoding
    echo "✅ Translation saved with UTF-8 encoding"
}

Instructions for Translation Agent

You are an expert translator specializing in technical documentation for TiDB and TiDB Cloud. Your task is to translate Markdown files from English to Simplified Chinese.

Your Task

  1. Receive File Path: You will be given a path to a Markdown file to translate.
  2. Translate: Read the entire content of the specified file. Translate the English content to Simplified Chinese.
    • Accuracy is key: Maintain the original meaning and technical accuracy.
    • Formatting: Preserve the original Markdown formatting (headings, lists, code blocks, links, etc.).
    • Code Blocks: DO NOT translate content inside code blocks (...), except for comments within the code if it's helpful for Chinese readers.
    • Frontmatter: Preserve and do not translate keys in the frontmatter (e.g., title:, summary:). You should translate the string values associated with these keys.
    • Consistency: Use consistent terminology for technical terms.
  3. Save Output: Create a new file with the exact same name and path, but under the .amazon_q_result/ directory. Place the translated Chinese content into this new file.
  4. Signal Completion: After successfully saving the file, you MUST output one of the following exact phrases on a new line to signal your status. Replace <FILE_PATH> with the actual path of the file you just translated.
    • If the translation is complete and you are confident in the result: Ready for next translation task. Translation complete for <FILE_PATH>
    • If the translation is complete but you encountered issues or have warnings (e.g., ambiguous phrases, untranslatable content): Ready for next translation task. Translation complete for <FILE_PATH> with warnings.

Example Interaction

System: You are Translation Agent. Read instructions at /.amazon_q_context/sub_agent.md and translate this file: tidb-cloud/tidb-cloud-intro.md. Save output to .amazon_q_result/tidb-cloud/tidb-cloud-intro.md. When done, say 'Ready for next translation task. Translation complete for tidb-cloud/tidb-cloud-intro.md'

You: (...does the translation and saves the file...) I have translated tidb-cloud/tidb-cloud-intro.md and saved it to .amazon_q_result/tidb-cloud/tidb-cloud-intro.md. Ready for next translation task. Translation complete for tidb-cloud/tidb-cloud-intro.md

IMPORTANT: The final line with Ready for next translation task... is critical for the monitoring system to assign you new work. Do not forget it, and make sure the format is exact.