How to automate cli interactions with gh copilot using expect

I want to automatically press

  1. Enter (select generic shell command)
  2. Enter (Copy command to clipboard)
  3. Up (Move cursor to Exit)
  4. Enter (Exit gh cli)

so I can Ctrl+V to paste the suggested terminal command.

Question

How do I use expect (linux tool) to do that?
I can’t get the correct combination of keywords like timeout, interact, send.

I also suspect gh copilot copying to clipboard is messing with the interactivity design, or there are side-effects from a previous run of ./copilot_auto.sh how to list files affecting current run.

Manual workflow to be automated

Input1: gh copilot suggest how to list files

Output1:

Welcome to GitHub Copilot in the CLI!
version 0.5.4-beta (2024-01-04)

I'm powered by AI, so surprises and mistakes are possible. Make sure to verify any generated code or suggestions, and share feedback so that we can learn and improve. For more information, see https://gh.io/gh-copilot-transparency

? What kind of command can I help you with?  [Use arrows to move, type to filter]
> generic shell command
  gh command
  git command

Input 2: Press Enter to select generic shell command

Output 2: (2nd and 3rd options disappear from output 1 and suggestion appears)

? What kind of command can I help you with?
> generic shell command

Suggestion:                                                                                                                                                                      
                                                                                                                                                                                 
  ls                                                                                                                                                                             

? Select an option  [Use arrows to move, type to filter]
> Copy command to clipboard
  Explain command
  Revise command
  Rate response
  Exit

Input 3: Press Enter to select Copy command to clipboard

Output 3: Command copied to clipboard appears as confirmation, then options repeat

? Select an option
> Copy command to clipboard

Command copied to clipboard!

? Select an option  [Use arrows to move, type to filter]
> Copy command to clipboard
  Explain command
  Revise command
  Rate response
  Exit

Automation Attempts

Currently I tried to use expect:
Timeout is used to wait for copilot cli to get to the option screen. I tweaked expect code from copilot, I have weak understanding of expect mechanics.

#!/usr/bin/expect

set input_command [join $argv " "]
spawn gh copilot suggest $input_command

interact {
    timeout 1 { send "r" }
}
# interact
# sleep 1
# send "r"

Questions on Expect mechanics

Example 1: Why does below not send Enter like the timeout example?

interact
sleep 1
send "r"

Example 2: Why does below timeout 1 fail like below but timeout 2 works to continuously send up key?

Why does Enter key have a shorter timeout required than Up key?

Where is this ['x1b' '] below coming from? (This is where i suspect problems due to clipboard or side-effect of previous run)

Input code

#!/usr/bin/expect

set input_command [join $argv " "]
spawn gh copilot suggest $input_command

interact {
    timeout 1 { send "33[A" }
}

Output

spawn gh copilot suggest how to list files

Welcome to GitHub Copilot in the CLI!
version 0.5.4-beta (2024-01-04)

I'm powered by AI, so surprises and mistakes are possible. Make sure to verify any generated code or suggestions, and share feedback so that we can learn and improve. For more information, see https://gh.io/gh-copilot-transparency

? What kind of command can I help you with? 1R  [Use arrows to move, type to filter]

✗ Error: could not prompt: unexpected escape sequence from terminal: ['x1b' ']']

^[[7;1R^[]11;rgb:1f1f/2424/2828^[^[[11;1R% 

Example 3: If I try to mix both Enter and Up keys to get closer to the 4 key sequence mentioned at the start, it only presses Up key and ignores Enter:

Why is interact infinite looping, and why is it only executing the last command?

#!/usr/bin/expect

set input_command [join $argv " "]
spawn gh copilot suggest $input_command

interact {
    timeout 1 { send "r" }
    timeout 2 { send "33[A" }
}

2

For everything that you spawn or send, you should expect some response.
For this scenario, you want to

  • spawn the gh command with some arguments
  • expect to see the “what kind of command” prompt
  • send “hit enter”
  • expect to see the “select an option” prompt
  • send “hit enter”
  • expect to see the “select an option” prompt again
  • send “hit Down 4 times and enter”
    • or maybe typing the exit response is simpler: send “Exitr”
  • expect the process to end

Translating that into (untested) code

spawn gh copilot suggest {*}$args
expect "What kind of command can I help you with"
send "r"
expect "Select an option"
send "r"
expect "Select an option"
send "Exitr"
expect eof

The problem with interact is that it sends control away from the code into the user’s hands.
You can provide interact some options to control it, such as “pattern action” pairs to do things when the user types something, or “timeout action” when the user doesn’t type anything.
But you haven’t provided any way to return control back to the program.
The interact command is just sitting there waiting for you to type, hitting Enter every second that you don’t.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật