heartcode 0.1.27 → 0.1.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +123 -0
- package/README.md +84 -30
- package/SKILL.md +37 -11
- package/bin/heartcode.js +252 -56
- package/package.json +1 -1
- package/prompts/agent.md +131 -22
- package/src/agent.js +498 -77
- package/src/background.js +105 -0
- package/src/client.js +14 -3
- package/src/config.js +1 -1
- package/src/guided.js +7 -2
- package/src/headless.js +10 -4
- package/src/memory.js +6 -2
- package/src/research.js +142 -0
- package/src/sessions.js +4 -1
- package/src/settings.html +331 -76
- package/src/settings.js +73 -0
- package/src/telegram.js +81 -0
- package/src/tools.js +163 -53
- package/src/ui.js +235 -34
- package/src/web.js +150 -40
package/src/ui.js
CHANGED
|
@@ -9,7 +9,7 @@ import { log } from './log.js'
|
|
|
9
9
|
import { HOME } from './config.js'
|
|
10
10
|
import { listen, transcribePcm, voiceProblem } from './voice.js'
|
|
11
11
|
|
|
12
|
-
const color = (process.stdout.isTTY || Boolean(process.env.FORCE_COLOR)) && !process.env.NO_COLOR
|
|
12
|
+
export const color = (process.stdout.isTTY || Boolean(process.env.FORCE_COLOR)) && !process.env.NO_COLOR
|
|
13
13
|
// 24-bit where the terminal says so, else the nearest of 256 (Terminal.app, xterm-256color), else 16.
|
|
14
14
|
const depth = !color ? 0 : /truecolor|24bit/i.test(process.env.COLORTERM ?? '') ? 3 : /256/.test(process.env.TERM ?? '') || process.env.TERM_PROGRAM === 'Apple_Terminal' ? 2 : 1
|
|
15
15
|
const to256 = ([r, g, b]) => {
|
|
@@ -118,7 +118,7 @@ export const keyId = (key, s = '') => CONTROL_KEYS[key.sequence ?? s] ?? [key.ct
|
|
|
118
118
|
// next one). /voice records (Space in an empty box, or a key in keybindings.json); Alt+T, Alt+W,
|
|
119
119
|
// Alt+N/P (or Ctrl+PgDn/PgUp), Alt+1…9: tabs; Shift+↑↓ and Alt+↑↓ scroll the conversation (as
|
|
120
120
|
// PgUp/PgDn and the mouse wheel do). keybindings.json can move any of them.
|
|
121
|
-
export const DEFAULT_KEYS = { 'alt+s': 'send_first', 'alt+a': 'send_all', 'alt+m': 'queue_mode', 'shift+up': 'scroll_line_up', 'shift+down': 'scroll_line_down', 'alt+up': 'scroll_up', 'alt+down': 'scroll_down', 'alt+t': 'new_tab', 'alt+w': 'close_tab', 'alt+n': 'next_tab', 'alt+p': 'prev_tab', 'ctrl+pagedown': 'next_tab', 'ctrl+pageup': 'prev_tab', ...Object.fromEntries([1, 2, 3, 4, 5, 6, 7, 8, 9].map((n) => [`alt+${n}`, `tab_${n}`])) }
|
|
121
|
+
export const DEFAULT_KEYS = { 'alt+b': 'background_log', 'alt+s': 'send_first', 'alt+a': 'send_all', 'alt+m': 'queue_mode', 'shift+up': 'scroll_line_up', 'shift+down': 'scroll_line_down', 'alt+up': 'scroll_up', 'alt+down': 'scroll_down', 'alt+t': 'new_tab', 'alt+w': 'close_tab', 'alt+n': 'next_tab', 'alt+p': 'prev_tab', 'ctrl+pagedown': 'next_tab', 'ctrl+pageup': 'prev_tab', ...Object.fromEntries([1, 2, 3, 4, 5, 6, 7, 8, 9].map((n) => [`alt+${n}`, `tab_${n}`])) }
|
|
122
122
|
export function loadKeys(file = path.join(HOME, 'keybindings.json')) {
|
|
123
123
|
let raw = {}
|
|
124
124
|
try {
|
|
@@ -646,6 +646,7 @@ export class UI {
|
|
|
646
646
|
this.isCommand = (l) => l.startsWith('/') // the app's: only real commands (a path like /Users/… is a message)
|
|
647
647
|
this.keys = { ...DEFAULT_KEYS, ...loadKeys() }
|
|
648
648
|
this.waiters = []
|
|
649
|
+
this.freed = [] // asks waiting for the one on screen (a command's picker beside a task's question)
|
|
649
650
|
this.closed = false
|
|
650
651
|
this.rl = null
|
|
651
652
|
this.onInterrupt = null
|
|
@@ -663,6 +664,154 @@ export class UI {
|
|
|
663
664
|
this.scroll = 0
|
|
664
665
|
this.asking = false
|
|
665
666
|
this.frame = 0
|
|
667
|
+
this.dialog = null // a box over the screen (the background commands' logs)
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
// A dialog: a box in the middle, 70% of the screen each way, the conversation going on around it;
|
|
671
|
+
// drawn again twice a second while it's open. { title, draw(w, h) → lines, key?(key), click?(col, row),
|
|
672
|
+
// close?(), toggle?: the key action that closes it too }. While it's open every key is its own (Ctrl+C
|
|
673
|
+
// aside); Esc, or a click outside it, closes it.
|
|
674
|
+
setDialog(dialog) {
|
|
675
|
+
const was = this.dialog
|
|
676
|
+
this.dialog = dialog
|
|
677
|
+
clearInterval(this.dialogTimer)
|
|
678
|
+
this.dialogTimer = dialog ? setInterval(() => this.render(), 500) : null
|
|
679
|
+
this.dialogTimer?.unref()
|
|
680
|
+
if (was && was !== dialog) was.close?.()
|
|
681
|
+
this.render()
|
|
682
|
+
}
|
|
683
|
+
// Text to read (a command's answer: /help, /context, /diff…) in a dialog: ↑/↓, PgUp/PgDn, Home/End
|
|
684
|
+
// and the wheel scroll it, Esc closes it. Without the full screen: printed.
|
|
685
|
+
showText(title, text) {
|
|
686
|
+
if (!this.full) return this.print(text)
|
|
687
|
+
const all = String(text).split('\n')
|
|
688
|
+
let top = 0
|
|
689
|
+
let page = 10
|
|
690
|
+
let total = 0
|
|
691
|
+
this.setDialog({
|
|
692
|
+
title,
|
|
693
|
+
key: (key) => {
|
|
694
|
+
const by = { up: key.wheel ? -3 : -1, down: key.wheel ? 3 : 1, pageup: -page, pagedown: page, home: -1e9, end: 1e9 }[key.name]
|
|
695
|
+
if (by) top = Math.max(0, Math.min(top + by, total - page))
|
|
696
|
+
},
|
|
697
|
+
draw: (w, h) => {
|
|
698
|
+
const rows = all.flatMap((l) => wrapAnsi(l, w))
|
|
699
|
+
total = rows.length
|
|
700
|
+
page = h - 2
|
|
701
|
+
top = Math.max(0, Math.min(top, total - page))
|
|
702
|
+
const shown = rows.slice(top, top + page)
|
|
703
|
+
const where = total > page ? `${top + 1}–${Math.min(total, top + page)} of ${total} lines · ↑/↓ PgUp/PgDn scroll · ` : ''
|
|
704
|
+
return [...shown, '', c.faint(`${where}Esc close`)]
|
|
705
|
+
},
|
|
706
|
+
})
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
// where it is: { x, y, w, h } (screen cells, from 0; the inside, without the border)
|
|
710
|
+
#dialogBox(cols, rows) {
|
|
711
|
+
const w = Math.max(30, Math.floor(cols * 0.7))
|
|
712
|
+
const h = Math.max(8, Math.floor(rows * 0.7))
|
|
713
|
+
return { x: Math.floor((cols - w) / 2) + 1, y: Math.floor((rows - h) / 2) + 1, w: Math.min(w, cols) - 2, h: Math.min(h, rows) - 2 }
|
|
714
|
+
}
|
|
715
|
+
// d: { title, lines(w, h) → lines (at most h), fill?: always 70% tall too (a log), cursor?(w) →
|
|
716
|
+
// { row, col } in the lines (else hidden) }. The box is 70% of the screen wide, as tall as what's in
|
|
717
|
+
// it (70% at most), in the middle, with a margin inside.
|
|
718
|
+
#drawDialog(cols, rows, d) {
|
|
719
|
+
const PX = 2 // the margin inside: columns each side
|
|
720
|
+
const PY = 1 // and rows above and below
|
|
721
|
+
const max = this.#dialogBox(cols, rows)
|
|
722
|
+
const name = String(d.title ?? '')
|
|
723
|
+
const iwMax = max.w - 2 * PX
|
|
724
|
+
const w = iwMax
|
|
725
|
+
const lines = d.lines(w, max.h - 2 * PY).slice(0, max.h - 2 * PY)
|
|
726
|
+
const h = d.fill ? max.h - 2 * PY : lines.length
|
|
727
|
+
const W = w + 2 * PX
|
|
728
|
+
const x = Math.floor((cols - W - 2) / 2) + 1
|
|
729
|
+
const y = Math.floor((rows - h - 2 * PY - 2) / 2) + 1
|
|
730
|
+
this.dialogAt = { x, y, W, H: h + 2 * PY, cx: x + 1 + PX, cy: y + 1 + PY } // for the mouse (screen cells, from 1)
|
|
731
|
+
const paint = (t) => (background ? `${background}${t.replace(/\x1b\[(0|49)?m/g, (m) => m + background)}\x1b[0m` : `${t}\x1b[0m`)
|
|
732
|
+
const pad = (t, n) => {
|
|
733
|
+
const cut = fit(t, n)
|
|
734
|
+
return cut + ' '.repeat(Math.max(0, n - width(cut.replace(/\x1b\[[0-9;]*m/g, ''))))
|
|
735
|
+
}
|
|
736
|
+
const title = name ? ` ${width(name) > W - 6 ? `${fit(name, W - 7)}…` : name} ` : '' // too long: cut (a question shows all of it inside)
|
|
737
|
+
const row = (i, t) => `\x1b[${y + i};${x}H${paint(t)}`
|
|
738
|
+
let out = row(0, c.brand('╭─') + c.bold(title) + c.brand('─'.repeat(Math.max(0, W - width(title) - 1)) + '╮'))
|
|
739
|
+
const blank = c.brand('│') + ' '.repeat(W) + c.brand('│')
|
|
740
|
+
for (let i = 0; i < PY; i++) out += row(1 + i, blank)
|
|
741
|
+
for (let i = 0; i < h; i++) out += row(1 + PY + i, c.brand('│') + ' '.repeat(PX) + pad(lines[i] ?? '', w) + ' '.repeat(PX) + c.brand('│'))
|
|
742
|
+
for (let i = 0; i < PY; i++) out += row(1 + PY + h + i, blank)
|
|
743
|
+
out += row(2 + 2 * PY + h - 1, c.brand('╰' + '─'.repeat(W) + '╯'))
|
|
744
|
+
const cur = d.cursor?.(w)
|
|
745
|
+
return out + (cur ? `\x1b[${y + 1 + PY + cur.row};${x + 1 + PX + Math.min(cur.col, w - 1)}H\x1b[?25h` : '\x1b[?25l')
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
// The question being asked, as a dialog: its title; what's typed (the filter, or the answer) with the
|
|
749
|
+
// cursor; a picker's choices. A short list (a few, one to choose) is shown as cards: the name, its
|
|
750
|
+
// description under it. A permission (confirm) shows what just happened (the change or command it
|
|
751
|
+
// asks about) above the choices. A picker's body (pick's option): a few lines saying what's going on.
|
|
752
|
+
#question() {
|
|
753
|
+
const rl = this.rl
|
|
754
|
+
const m = this.menu
|
|
755
|
+
const plain = (t) => String(t ?? '').replace(/\x1b\[[0-9;]*m/g, '')
|
|
756
|
+
const title = plain(rl._prompt).replace(/\s*›?\s*$/, '').replace(/:\s*$/, '').trim()
|
|
757
|
+
const field = (w) => {
|
|
758
|
+
const pre = '› '
|
|
759
|
+
const room = w - pre.length - 1
|
|
760
|
+
const from = Math.max(0, rl.cursor - room)
|
|
761
|
+
return { text: c.brand(pre) + c.bright(rl.line.slice(from, from + room)), col: pre.length + rl.cursor - from }
|
|
762
|
+
}
|
|
763
|
+
const padTo = (t, n) => t + ' '.repeat(Math.max(0, n - width(plain(t))))
|
|
764
|
+
const pick = m.pick
|
|
765
|
+
const choices = pick ? m.items.filter((x) => !x.header) : []
|
|
766
|
+
const cards = pick && !pick.multi && pick.items.length <= 6 && !m.items.some((x) => x.header)
|
|
767
|
+
const body = pick?.body ? String(pick.body).split('\n') : []
|
|
768
|
+
const hint = pick ? (pick.fixed ? '↑↓ move · Enter choose · Esc close' : pick.multi ? '↑↓ move · Space tick · Ctrl+A all · Ctrl+N none · Enter take · Esc cancel · type to filter' : `↑↓ move · Enter choose · Esc cancel${cards ? '' : ' · type to filter'}`) : 'Enter send · Esc cancel'
|
|
769
|
+
let at = 0
|
|
770
|
+
const long = (w) => width(title) > w // too long for the border: all of it inside too
|
|
771
|
+
return {
|
|
772
|
+
title: title.length > 200 ? title.slice(0, 200) : title,
|
|
773
|
+
cursor: (w) => (pick?.fixed || (cards && pick) ? null : { row: at, col: field(w).col }),
|
|
774
|
+
lines: (w, h) => {
|
|
775
|
+
const head = long(w) ? [...wrapAnsi(c.bold(title), w).map((l) => l), ''] : []
|
|
776
|
+
const text = body.flatMap((l) => wrapAnsi(/^https?:\/\/\S+$/.test(l.trim()) ? c.info(l) : c.muted(l), w)) // an address in its link color
|
|
777
|
+
const intro = [...head, ...text, ...(text.length ? [''] : [])]
|
|
778
|
+
if (!pick) {
|
|
779
|
+
const recent = this.askAbout ? this.#recent(w, Math.min(8, h - intro.length - 5)) : []
|
|
780
|
+
const top = [...recent, ...(recent.length ? [c.ghost('─'.repeat(w)), ''] : []), ...intro]
|
|
781
|
+
at = top.length
|
|
782
|
+
return [...top, field(w).text, '', c.faint(hint)]
|
|
783
|
+
}
|
|
784
|
+
let list
|
|
785
|
+
if (cards) {
|
|
786
|
+
list = choices.length
|
|
787
|
+
? choices.flatMap((x, i) => {
|
|
788
|
+
const on = m.items[m.sel] === x
|
|
789
|
+
const name = on ? `${c.brand('❯')} ${c.bright(c.bold(x.item.label))}` : ` ${c.text(x.item.label)}`
|
|
790
|
+
const desc = x.item.desc ? (on ? ` ${c.muted(x.item.desc)}` : ` ${c.faint(x.item.desc)}`) : null
|
|
791
|
+
const rows = [name, ...(desc ? [desc] : [])].map((r) => (on ? c.select(padTo(r, w)) : r))
|
|
792
|
+
return i < choices.length - 1 && choices.some((y) => y.item.desc) ? [...rows, ''] : rows // a gap between cards that have two lines
|
|
793
|
+
})
|
|
794
|
+
: [c.faint(`nothing matches "${rl.line}"`)]
|
|
795
|
+
}
|
|
796
|
+
const filter = pick.fixed || cards ? [] : [field(w).text, c.ghost('─'.repeat(w))]
|
|
797
|
+
const room = h - intro.length - filter.length - 2
|
|
798
|
+
if (!cards) list = m.items.length ? this.#menuLines(w, Math.max(1, Math.min(room - (m.items.length > room ? 1 : 0), m.items.length))).slice(1) : [c.faint(`nothing matches "${rl.line}"`)]
|
|
799
|
+
const recent = this.askAbout ? this.#recent(w, Math.min(8, room - list.length - 1)) : []
|
|
800
|
+
const top = [...recent, ...(recent.length ? [c.ghost('─'.repeat(w)), ''] : []), ...intro]
|
|
801
|
+
at = top.length
|
|
802
|
+
return [...top, ...filter, ...list, '', c.faint(hint)]
|
|
803
|
+
},
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
// The last `n` rows of this task's output at width w (what a question in the middle of the work is about).
|
|
808
|
+
#recent(w, n) {
|
|
809
|
+
if (n <= 0) return []
|
|
810
|
+
const out = []
|
|
811
|
+
const from = Math.max(0, (this.inputMark ?? 0) + 1) // this task's: from after your message
|
|
812
|
+
for (let i = this.log.length - 1; i >= from && out.length < n; i--) out.unshift(...wrapAnsi(this.log[i], w))
|
|
813
|
+
while (out.length && !out[0].trim()) out.shift()
|
|
814
|
+
return out.slice(-n)
|
|
666
815
|
}
|
|
667
816
|
|
|
668
817
|
// One model reply: thinking (dim, its own block) kept apart from the answer.
|
|
@@ -771,7 +920,16 @@ export class UI {
|
|
|
771
920
|
if (!this.full) return
|
|
772
921
|
if (b & 64) return (b & 0xc3) === 64 || (b & 0xc3) === 65 ? this.scrollBy(b & 1 ? -3 : 3) : undefined // 64 up, 65 down
|
|
773
922
|
if ((b & 3) !== 0 && !up) return // the left button only (a release says 3 in some terminals)
|
|
923
|
+
if (this.dialog) {
|
|
924
|
+
// the wheel scrolls it; a click inside goes to it, outside closes it
|
|
925
|
+
const d = this.dialogAt
|
|
926
|
+
if (b & 64) return (b & 0xc3) === 64 || (b & 0xc3) === 65 ? (this.dialog.key?.({ name: b & 1 ? 'down' : 'up', wheel: true }), this.render()) : undefined
|
|
927
|
+
if ((b & 32) || up || !d) return
|
|
928
|
+
if (x < d.x || y < d.y || x > d.x + d.W + 1 || y > d.y + d.H + 1) return this.setDialog(null) // outside it
|
|
929
|
+
return this.dialog.click?.(x - d.cx, y - d.cy), this.render()
|
|
930
|
+
}
|
|
774
931
|
if (!(b & 32) && !up && y <= 3 && this.onTabClick) return this.onTabClick(x - 1) // the tab bar's 3 rows
|
|
932
|
+
|
|
775
933
|
const v = this.viewAt
|
|
776
934
|
if (!v) return
|
|
777
935
|
const r = Math.min(Math.max(y - 1 - v.first, 0), v.avail - 1) // a drag past the edge stops at it
|
|
@@ -880,9 +1038,12 @@ export class UI {
|
|
|
880
1038
|
const { out, rl, menu } = this
|
|
881
1039
|
const rows = out.rows || 24
|
|
882
1040
|
const cols = out.columns || 80
|
|
1041
|
+
// A question (a picker, a permission, something to type) is a dialog over the screen; the input
|
|
1042
|
+
// box behind it stays as it was.
|
|
1043
|
+
const asked = this.asking && !this.histOn && rl
|
|
883
1044
|
// The input box: the rows around the cursor, at most INPUT_ROWS; the rules count the rest.
|
|
884
1045
|
const typing = !this.asking && rl && this.mainPrompt // working: what's typed meanwhile shows in the box
|
|
885
|
-
const input = this.asking || typing ? layoutInput(this.asking ? rl._prompt : this.mainPrompt, rl.line, rl.cursor, cols) : { rows: [''], cur: { row: 0, col: 0 } }
|
|
1046
|
+
const input = asked ? layoutInput(this.mainPrompt ?? ' ', '', 0, cols) : this.asking || typing ? layoutInput(this.asking ? rl._prompt : this.mainPrompt, rl.line, rl.cursor, cols) : { rows: [''], cur: { row: 0, col: 0 } }
|
|
886
1047
|
const first = Math.max(0, Math.min(input.cur.row - INPUT_ROWS + 1, input.rows.length - INPUT_ROWS))
|
|
887
1048
|
const box = input.rows.slice(first, first + INPUT_ROWS)
|
|
888
1049
|
const inputRows = box.length
|
|
@@ -908,7 +1069,7 @@ export class UI {
|
|
|
908
1069
|
if (this.ahead.length > 5) bottom.push(c.faint(` … ${this.ahead.length - 5} more`))
|
|
909
1070
|
}
|
|
910
1071
|
if (this.noticeText) bottom.push(fit(`${c.warn('!')} ${c.muted(this.noticeText)}`, cols))
|
|
911
|
-
if (menu.on) bottom.push(...this.#menuLines(cols))
|
|
1072
|
+
if (menu.on && !asked) bottom.push(...this.#menuLines(cols))
|
|
912
1073
|
bottom.push(first ? note(first, '↑') : rule)
|
|
913
1074
|
const head = [...(this.tabBar?.(cols) ?? []), ...(this.header?.(cols) ?? [])].map((l) => fit(l, cols))
|
|
914
1075
|
const inputTop = rows - below.length - inputRows + 1 // first row of the input
|
|
@@ -923,8 +1084,13 @@ export class UI {
|
|
|
923
1084
|
for (let i = Math.max(0, s.row - top); i <= Math.min(view.length - 1, e.row - top); i++) view[i] = highlight(view[i], top + i === s.row ? s.col : 0, top + i === e.row ? e.col + 1 : Infinity)
|
|
924
1085
|
}
|
|
925
1086
|
// a theme with a background: every row painted with it (a style ending its own background goes back to it)
|
|
926
|
-
|
|
1087
|
+
let row = background ? (line) => `${background}${line.replace(/\x1b\[(0|49)?m/g, (m) => m + background)}\x1b[K\x1b[0m` : (line) => `${line}\x1b[0m\x1b[K`
|
|
927
1088
|
let s = '\x1b[?25l'
|
|
1089
|
+
// a dialog open: everything behind it dimmed
|
|
1090
|
+
if (this.dialog || asked) {
|
|
1091
|
+
const plainRow = row
|
|
1092
|
+
row = (line) => plainRow(`\x1b[2m${line.replace(/\x1b\[(0|22)?m/g, (m) => m + '\x1b[2m')}`)
|
|
1093
|
+
}
|
|
928
1094
|
for (let r = 0; r < inputTop - 1; r++) {
|
|
929
1095
|
const i = r - head.length
|
|
930
1096
|
const line = i < 0 ? head[r] : i < avail ? (view[i] ?? '') : bottom[i - avail]
|
|
@@ -932,7 +1098,9 @@ export class UI {
|
|
|
932
1098
|
}
|
|
933
1099
|
box.forEach((line, i) => (s += `\x1b[${inputTop + i};1H${row(line)}`))
|
|
934
1100
|
below.forEach((line, i) => (s += `\x1b[${inputTop + inputRows + i};1H${row(line)}`))
|
|
935
|
-
if (this.
|
|
1101
|
+
if (this.dialog) s += this.#drawDialog(cols, rows, { title: this.dialog.title, fill: this.dialog.fill, lines: (w, h) => this.dialog.draw(w, h) }) // over everything, the cursor hidden
|
|
1102
|
+
else if (asked) s += this.#drawDialog(cols, rows, this.#question()) // a question: its cursor in the box
|
|
1103
|
+
else if (this.asking || typing) s += `\x1b[${inputTop + input.cur.row - first};${input.cur.col + 1}H\x1b[?25h`
|
|
936
1104
|
else s += `\x1b[${inputTop};1H`
|
|
937
1105
|
out.write(s)
|
|
938
1106
|
}
|
|
@@ -949,14 +1117,22 @@ export class UI {
|
|
|
949
1117
|
if (!this.full) return write(s, key)
|
|
950
1118
|
if (this.sel) (this.sel = null), this.render() // a key ends a selection
|
|
951
1119
|
const bound = !(key.ctrl && key.name === 'c') && this.keys[keyId(key, s)] // ~/.heart/keybindings.json
|
|
1120
|
+
// a dialog: its keys, all of them; Esc (or its own key) closes it; Ctrl+C still stops and quits
|
|
1121
|
+
if (this.dialog && !(key.ctrl && key.name === 'c')) {
|
|
1122
|
+
if (key.name === 'escape' || (bound && bound === this.dialog.toggle)) return this.setDialog(null)
|
|
1123
|
+
return this.dialog.key?.(key, s), this.render()
|
|
1124
|
+
}
|
|
952
1125
|
if (this.recording) return this.recording.key(key, bound) // voice: Space, Enter, Esc; the rest waits
|
|
953
1126
|
// a fixed dialog (pick(…, { fixed })): only its keys, nothing typed and no shortcuts
|
|
954
1127
|
if (this.menu.pick?.fixed && !['up', 'down', 'return', 'escape'].includes(key.name) && !(key.ctrl && key.name === 'c')) return
|
|
955
1128
|
// In a multi-select: Ctrl+A ticks all, Ctrl+N unticks all (before tab shortcuts claim Ctrl+N)
|
|
956
1129
|
if (this.menu.pick?.multi && key.ctrl && (key.name === 'a' || key.name === 'n')) {
|
|
957
|
-
for (const x of this.menu.pick.items) x.checked = key.name === 'a'
|
|
1130
|
+
for (const x of this.menu.pick.items) if (!x.only) x.checked = key.name === 'a'
|
|
958
1131
|
return this.refreshMenu()
|
|
959
1132
|
}
|
|
1133
|
+
// Option+← sends what Alt+B does in many terminals (Esc b): with text in the box, it's the word jump
|
|
1134
|
+
if (bound === 'background_log' && this.rl.line) return write(s, key)
|
|
1135
|
+
|
|
960
1136
|
if (bound) return this.#action(bound, write)
|
|
961
1137
|
// Space in an empty box starts voice straight away — no /voice needed (only when it's set up,
|
|
962
1138
|
// and not in a menu or a mid-task yes/no question, where Space types a space as usual)
|
|
@@ -1014,13 +1190,13 @@ export class UI {
|
|
|
1014
1190
|
if (m.pick) {
|
|
1015
1191
|
if (key.name === 'up' || key.name === 'down') return this.#move(key.name === 'up' ? -1 : 1)
|
|
1016
1192
|
if (key.name === 'tab') return
|
|
1017
|
-
if (m.pick.multi && key.name === 'space' && it) {
|
|
1193
|
+
if (m.pick.multi && key.name === 'space' && it && !it.item.only) {
|
|
1018
1194
|
it.item.checked = !it.item.checked // Space ticks; Enter takes the ticked ones; Ctrl+A/N all/none (handled earlier, before tab shortcuts)
|
|
1019
1195
|
return this.refreshMenu()
|
|
1020
1196
|
}
|
|
1021
1197
|
if (key.name === 'escape' || (key.name === 'return' && it)) {
|
|
1022
1198
|
const ticked = m.pick.items.filter((x) => x.checked)
|
|
1023
|
-
m.pick.chosen = key.name !== 'return' ? null : m.pick.multi ? (ticked.length ? ticked : [it.item]) : it.item
|
|
1199
|
+
m.pick.chosen = key.name !== 'return' ? null : m.pick.multi ? (ticked.length && !it.item.only ? ticked : [it.item]) : it.item // only: an entry that stands alone (Enter on it takes just it)
|
|
1024
1200
|
rl.line = ''
|
|
1025
1201
|
rl.cursor = 0
|
|
1026
1202
|
return write('\r', { name: 'return' })
|
|
@@ -1103,8 +1279,7 @@ export class UI {
|
|
|
1103
1279
|
rl.cursor = 0
|
|
1104
1280
|
m.items = []
|
|
1105
1281
|
m.off = null
|
|
1106
|
-
if (
|
|
1107
|
-
else if (line) this.ahead.push(line) // a /command runs when the task is done (the queue)
|
|
1282
|
+
if (line && !this.onNow?.(line)) this.ahead.push(line) // a command that leaves the conversation alone runs now, beside the task (onNow); the rest when it's done (the queue)
|
|
1108
1283
|
return this.render()
|
|
1109
1284
|
}
|
|
1110
1285
|
if (['up', 'down', 'tab'].includes(key.name) || key.name === 'paste-start' || key.name === 'paste-end') {
|
|
@@ -1140,15 +1315,11 @@ export class UI {
|
|
|
1140
1315
|
case 'next_tab':
|
|
1141
1316
|
case 'prev_tab':
|
|
1142
1317
|
return this.onTab?.(what)
|
|
1143
|
-
case 'voice':
|
|
1318
|
+
case 'voice':
|
|
1144
1319
|
if (m.pick || (this.asking && !this.histOn)) return
|
|
1145
|
-
return this.
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
rl.line = rl.line.slice(0, rl.cursor) + pad + text + rl.line.slice(rl.cursor)
|
|
1149
|
-
rl.cursor += pad.length + text.length
|
|
1150
|
-
this.render()
|
|
1151
|
-
})
|
|
1320
|
+
return this.dictateIn()
|
|
1321
|
+
case 'background_log':
|
|
1322
|
+
return this.onPanel?.()
|
|
1152
1323
|
case 'stop':
|
|
1153
1324
|
return this.asking ? undefined : this.onEscape?.()
|
|
1154
1325
|
case 'cycle_permissions':
|
|
@@ -1191,6 +1362,17 @@ export class UI {
|
|
|
1191
1362
|
write('\r', { name: 'return' })
|
|
1192
1363
|
}
|
|
1193
1364
|
|
|
1365
|
+
// The words go where the cursor is, in the box (also while it works).
|
|
1366
|
+
async dictateIn() {
|
|
1367
|
+
const text = await this.dictate()
|
|
1368
|
+
if (!text) return
|
|
1369
|
+
const rl = this.rl
|
|
1370
|
+
const pad = rl.line && !/\s$/.test(rl.line.slice(0, rl.cursor)) ? ' ' : ''
|
|
1371
|
+
rl.line = rl.line.slice(0, rl.cursor) + pad + text + rl.line.slice(rl.cursor)
|
|
1372
|
+
rl.cursor += pad.length + text.length
|
|
1373
|
+
this.render()
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1194
1376
|
// Voice mode (/voice, or a key bound to voice): Space to talk, Space again to pause (and again to go on), Enter
|
|
1195
1377
|
// (or the voice key) to finish, Esc to cancel. While you speak, what's been heard so far shows in
|
|
1196
1378
|
// the input box (where the cursor was), redone about every second with the quickest model; a
|
|
@@ -1334,10 +1516,10 @@ export class UI {
|
|
|
1334
1516
|
this.render()
|
|
1335
1517
|
}
|
|
1336
1518
|
|
|
1337
|
-
#menuLines(cols) {
|
|
1519
|
+
#menuLines(cols, rows) {
|
|
1338
1520
|
const m = this.menu
|
|
1339
1521
|
const width = cols - 1
|
|
1340
|
-
const max = m.pick ? Math.max(5, (this.out.rows || 24) - 8) : MENU_ROWS
|
|
1522
|
+
const max = rows ?? (m.pick ? Math.max(5, (this.out.rows || 24) - 8) : MENU_ROWS)
|
|
1341
1523
|
const first = Math.max(0, Math.min(m.sel - max + 1, m.items.length - max))
|
|
1342
1524
|
const shown = m.items.slice(first, first + max)
|
|
1343
1525
|
const pad = m.items.some((x) => x.header) ? ' ' : ''
|
|
@@ -1431,7 +1613,14 @@ export class UI {
|
|
|
1431
1613
|
}
|
|
1432
1614
|
|
|
1433
1615
|
// history: this prompt's messages go into ↑/↓ history (only the main prompt, never answers like keys).
|
|
1616
|
+
// One question on screen at a time: a command run beside a task (onNow) and the task can both ask.
|
|
1617
|
+
async #free() {
|
|
1618
|
+
while (this.full && this.asking) await new Promise((r) => this.freed.push(r))
|
|
1619
|
+
}
|
|
1620
|
+
|
|
1434
1621
|
ask(prompt, { initial = '', suggest = false, history = false } = {}) {
|
|
1622
|
+
if (this.dialog && !history) this.setDialog(null) // a question (a permission, a picker) needs you: the dialog makes way
|
|
1623
|
+
if (this.full && this.asking) return this.#free().then(() => this.ask(prompt, { initial, suggest, history }))
|
|
1435
1624
|
if (history && this.kept) [initial, this.kept] = [initial || this.kept, ''] // typed before a submit()
|
|
1436
1625
|
this.stopSpin()
|
|
1437
1626
|
const rl = this.#rl()
|
|
@@ -1457,6 +1646,7 @@ export class UI {
|
|
|
1457
1646
|
return Promise.resolve(l)
|
|
1458
1647
|
}
|
|
1459
1648
|
if (this.closed) return Promise.resolve(null)
|
|
1649
|
+
if (this.busySince && !history) this.onWaiting?.(prompt) // asked mid-task: you may be away (Telegram)
|
|
1460
1650
|
this.menu.on = Boolean(suggest && (this.suggest || this.menu.pick) && this.full)
|
|
1461
1651
|
this.histOn = history
|
|
1462
1652
|
this.histAt = -1
|
|
@@ -1497,6 +1687,7 @@ export class UI {
|
|
|
1497
1687
|
}
|
|
1498
1688
|
if (draft) [rl.line, rl.cursor] = [draft, draft.length]
|
|
1499
1689
|
resolve(l)
|
|
1690
|
+
this.freed.shift()?.()
|
|
1500
1691
|
}),
|
|
1501
1692
|
)
|
|
1502
1693
|
}
|
|
@@ -1510,7 +1701,7 @@ export class UI {
|
|
|
1510
1701
|
// multi: tick several with Space; returns the ticked items (an array), or null.
|
|
1511
1702
|
// quiet: the question and answer aren't left in the conversation (a question before it starts).
|
|
1512
1703
|
// fixed: a dialog: no typing to filter, no other keys meanwhile.
|
|
1513
|
-
async pick(title, items, { filter = '', multi = false, quiet = false, fixed = false } = {}) {
|
|
1704
|
+
async pick(title, items, { filter = '', multi = false, quiet = false, fixed = false, body = '' } = {}) {
|
|
1514
1705
|
if (!this.full) {
|
|
1515
1706
|
items.forEach((it, i) => this.print(` ${i + 1}. ${it.group ? `${it.group} · ` : ''}${it.label}${it.desc ? ' ' + c.dim(it.desc) : ''}`))
|
|
1516
1707
|
const a = await this.ask(`${title} ${c.dim(multi ? '(numbers, like 1,3)' : '(number)')}: `)
|
|
@@ -1518,8 +1709,9 @@ export class UI {
|
|
|
1518
1709
|
const chosen = String(a ?? '').split(/[\s,]+/).map((n) => items[Number(n) - 1]).filter(Boolean)
|
|
1519
1710
|
return chosen.length ? chosen : null
|
|
1520
1711
|
}
|
|
1521
|
-
|
|
1522
|
-
|
|
1712
|
+
await this.#free()
|
|
1713
|
+
if (multi) for (const it of items) if (!it.only) it.checked ??= false
|
|
1714
|
+
this.menu.pick = { items, chosen: null, multi, quiet, fixed, body }
|
|
1523
1715
|
try {
|
|
1524
1716
|
await this.ask(`${c.bold(title)} ${c.dim('›')} `, { suggest: true, initial: filter })
|
|
1525
1717
|
return this.menu.pick.chosen
|
|
@@ -1544,16 +1736,12 @@ export class UI {
|
|
|
1544
1736
|
const mode = this.permission === 'plan' ? (this.beforePlan ?? 'ask') : this.permission
|
|
1545
1737
|
if (mode === 'all' || (mode === 'commands' && kind === 'edit')) return true
|
|
1546
1738
|
if (this.full) {
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
const a = (await this.pick(`${c.yellow('?')} ${question}`, opts))?.value ?? false
|
|
1554
|
-
if (a === 'all') this.setPermission('all')
|
|
1555
|
-
if (a !== 'why') return Boolean(a)
|
|
1556
|
-
return (await this.ask(`${c.bold('What should it do instead?')} `))?.trim() || false
|
|
1739
|
+
this.askAbout = true // the dialog shows what it's about (the diff, the command) above the choices
|
|
1740
|
+
try {
|
|
1741
|
+
return await this.#confirmFull(question)
|
|
1742
|
+
} finally {
|
|
1743
|
+
this.askAbout = false
|
|
1744
|
+
}
|
|
1557
1745
|
}
|
|
1558
1746
|
const a = await this.ask(`${c.yellow('?')} ${question} ${c.dim('[Y/n or type feedback]')} `)
|
|
1559
1747
|
if (a === null) return false
|
|
@@ -1562,6 +1750,18 @@ export class UI {
|
|
|
1562
1750
|
if (/^(n|no)$/i.test(t)) return false
|
|
1563
1751
|
return t
|
|
1564
1752
|
}
|
|
1753
|
+
async #confirmFull(question) {
|
|
1754
|
+
const opts = [
|
|
1755
|
+
{ label: 'Yes', value: true },
|
|
1756
|
+
{ label: "Yes, and don't ask again", value: 'all' },
|
|
1757
|
+
{ label: 'No', value: false },
|
|
1758
|
+
{ label: 'No, and tell the model what to do instead', value: 'why' },
|
|
1759
|
+
]
|
|
1760
|
+
const a = (await this.pick(`${c.yellow('?')} ${question}`, opts))?.value ?? false
|
|
1761
|
+
if (a === 'all') this.setPermission('all')
|
|
1762
|
+
if (a !== 'why') return Boolean(a)
|
|
1763
|
+
return (await this.ask(`${c.bold('What should it do instead?')} `))?.trim() || false
|
|
1764
|
+
}
|
|
1565
1765
|
|
|
1566
1766
|
write(s) {
|
|
1567
1767
|
if (!this.full) this.stopSpin() // inline, the spinner shares the line; full screen it has its own row
|
|
@@ -1665,6 +1865,7 @@ export class UI {
|
|
|
1665
1865
|
|
|
1666
1866
|
close() {
|
|
1667
1867
|
this.busySince = null // closing ends any work in progress: the spinner stops too
|
|
1868
|
+
clearInterval(this.dialogTimer)
|
|
1668
1869
|
this.stopSpin()
|
|
1669
1870
|
this.rl?.close()
|
|
1670
1871
|
this.leaveFullscreen()
|