UNPKG

eve

Version:

Filesystem-first framework for durable backend AI agents that run anywhere.

4 lines 12.9 kB
import{maskLine,visibleLine}from"./line-editor.js";import{modelEditorMenuRows,reasoningPositions}from"./model-editor.js";import{clipVisible,renderInputText,renderInputWithBlockCursor,visibleLength,wrapVisibleLine}from"#cli/ui/terminal-text.js";import{renderCursorRow,renderOptionRow,renderOptionRowContinuation,resolveOptionRowState}from"#setup/cli/option-row.js";import{filterOptions,submitRowIndex}from"#setup/cli/select-state.js";function clip(e,t){return clipVisible(e,t)}function questionFooter(e,t){let n=t.colors;return[``,` ${n.dim(n.italic(e.join(` ${t.glyph.dot} `)))}`]}const BOLD_OR_DIM_CLOSE=`\x1B[22m`,DIM_OPEN=`\x1B[2m`,ANSI_FOREGROUND_COLOR=RegExp(`\x1B\\[(?:3[0-9]|9[0-7])m`,`g`);function dimWithEmphasis(e,t){return t.colors.dim(e.replaceAll(BOLD_OR_DIM_CLOSE,`${BOLD_OR_DIM_CLOSE}${DIM_OPEN}`))}function solidWithinDim(e,t){return t.color?`${BOLD_OR_DIM_CLOSE}${e}${DIM_OPEN}`:e}function foregroundWithEmphasis(e){let t=!1;return e.replaceAll(DIM_OPEN,``).replace(ANSI_FOREGROUND_COLOR,e=>e===`\x1B[34m`?(t=!0,e):e===`\x1B[39m`&&t?(t=!1,e):``)}function toneGlyph(e,t){let n=t.colors;switch(e){case`success`:return n.green(t.glyph.success);case`warning`:return n.yellow(t.glyph.warning);case`error`:return n.red(t.glyph.error);case`info`:return n.dim(t.glyph.dot)}}function renderIndicator(e,t){return e.color===`green`?t.colors.green(e.glyph):t.colors.yellow(e.glyph)}function renderStatusText(e,t){if(e.kind===`progress`)return t.colors.dim(e.text);let n=e.text.indexOf(e.emphasis);if(n===-1)return t.colors.dim(e.text);let r=n+e.emphasis.length;return`${t.colors.dim(e.text.slice(0,n))}${t.colors.yellow(e.text.slice(n,r))}${t.colors.dim(e.text.slice(r))}`}function renderFlowPanelStatus(e,t){return`${renderIndicator(e.indicator,t)} ${renderStatusText(e,t)}`}function renderFlowPanel(e,t,n){let r=t.colors,i=[r.dim(t.glyph.hrule.repeat(Math.max(1,n)))];e.title.length>0&&i.push(` ${r.bold(e.title)}`),i.push(``);let a=e.lines.slice(-6);for(let e of a){let n=e.tone===`info`?r.dim(e.text):e.text;i.push(` ${toneGlyph(e.tone,t)} ${n}`)}switch(a.length>0&&i.push(``),e.content.kind){case`question`:e.content.status!==void 0&&i.push(` ${renderFlowPanelStatus(e.content.status,t)}`,``),i.push(...e.content.rows);break;case`status`:i.push(` ${renderFlowPanelStatus(e.content.status,t)}`),e.content.preview!==void 0&&i.push(` ${r.dim(e.content.preview)}`);break;case`preview`:i.push(` ${renderIndicator(e.content.indicator,t)} ${r.dim(e.content.text)}`);break;case`idle`:i.push(` ${renderIndicator(e.content.indicator,t)} ${r.dim(`Working…`)}`);break}return i.map((e,t)=>t===0||e.length===0?clip(e,n):clip(` ${e}`,n))}function optionRow(e){let{option:t,theme:n}=e,r=e.railed===!0;return renderOptionRow({colors:n.colors,glyphs:{pointer:n.glyph.pointer,selectedPointer:n.glyph.selectedPointer,success:n.glyph.success,placeholder:r?n.glyph.caret:n.glyph.option,dot:r?``:n.glyph.dot,warning:n.glyph.warning},label:t.label,hint:t.hint,focusHint:t.focusHint,accent:t.accent,isCursor:e.isCursor,state:resolveOptionRowState(t,e.isChecked),placeholder:e.placeholder,hintPadding:e.hintPadding})}function selectPresentation(e){switch(e.kind){case`single`:return{selection:`single`,filter:void 0,layout:`plain`,edit:void 0};case`search`:return{selection:`single`,filter:{placeholder:e.placeholder},layout:e.layout??`plain`,edit:void 0};case`multi`:return{selection:`multiple`,filter:void 0,layout:`plain`,edit:void 0};case`searchable-multi`:return{selection:`multiple`,filter:{placeholder:e.placeholder},layout:`plain`,edit:void 0};case`stacked`:return{selection:`single`,filter:void 0,layout:`stacked`,edit:void 0};case`task-list`:return{selection:`single`,filter:void 0,layout:`task-list`,edit:void 0};case`inline-edit`:return{selection:`single`,filter:void 0,layout:e.layout,edit:e.edit}}}function selectMessageRows(e,t,n){if(e===``)return[];let r=e.split(` `).map((e,r)=>` ${t===`stacked`||r>0?n.colors.bold(e):e}`);return r.push(``),r}function searchFilter(e,t,n,r,i){let a=r.colors.dim(r.glyph.caret),o=a;return i?o=`${a} ${e.length>0?e+a:r.colors.dim(t??`type to search`)}`:e.length>0?o=e+a:t!==void 0&&(o=r.colors.dim(`> ${t}`)),n===void 0?o:`${o} ${r.colors.yellow(n)}`}function isRailedSearch(e){return e.filter!==void 0&&e.layout===`plain`&&e.selection===`single`&&e.edit===void 0}function selectViewSize(e){return e.search?e.railed?5:e.filter===``&&e.featuredLead>0?Math.min(e.featuredLead,8):8:e.optionCount}function noticeBody(e,t,n){return e.tone===`info`?n.colors.dim(e.text):e.tone===`success`&&t===`task-list`?n.colors.bold(e.text):e.text}function renameHint(e,n,r,i){let a=renderInputWithBlockCursor({...visibleLine({text:r.editor.text||r.defaultValue,cursor:r.editor.cursor},1/0),visible:n,inverse:i.colors.inverse});return r.editor.text.length>0&&(a=solidWithinDim(a,i)),{...e,hint:r.formatHint(a)}}function keyHint(n,r,i,c,l){let u=i.phase;if(u.kind===`inactive`)return n;let d=c.colors,f=maskLine(u.editor),p=u.kind!==`validating`&&u.kind!==`invalid`,m=``;u.kind===`editing`?m=` ${enterBadge(c,`validate`)}`:u.kind===`validating`?m=` ${d.yellow(c.glyph.validating)} ${d.dim(`validating`)}`:u.kind===`invalid`&&(m=` ${d.red(`${c.glyph.error} API key is not valid`)}`);let h=`${c.glyph.elbow} `,g=u.editor.text.length===0?`type your key`:void 0,_=visibleLine(g===void 0?f:{text:g,cursor:0},Math.max(1,l-visibleLength(`${h}${m}`)),c.glyph.ellipsis),v=p?renderInputWithBlockCursor({..._,visible:r,inverse:c.colors.inverse}):renderInputText(`${_.before}${_.under}${_.after}`);return{...n,hint:`${h}${v}${m}`}}function inlineEditOption(e,t,n,r,i){if(!t||n===void 0||e.value!==n.optionValue)return e;switch(n.editor.kind){case`rename`:return renameHint(e,n.caretVisible,n.editor,r);case`key`:return keyHint(e,n.caretVisible,n.editor,r,i)}}function optionWithoutStackedHint(e,t){if(t!==`stacked`||e.hint===void 0)return{option:e,stackedHint:void 0};let{hint:n,...r}=e;return{option:r,stackedHint:n}}function optionUsesPlaceholder(e,t){let n=e.filter!==void 0&&e.layout!==`task-list`,r=e.selection===`multiple`,i=e.layout!==`plain`;return!n&&!t&&(r||i)}function appendSelectOptionRows(e){let{rows:t,state:n,presentation:r,visible:i,start:a,end:o,cursor:s,visibleLabelWidth:c,width:l,theme:u}=e,f=u.colors,p=!1;for(let e=a;e<o;e+=1){let m=i[e],h=e===s,g=r.layout===`task-list`&&m.trailingAction===!0;g&&(appendSelectNotices(t,n.notices,r.layout,u,l),p=!0),g&&(e>a||(n.notices?.length??0)>0)&&t.push(``);let _=r.layout===`stacked`?Math.max(1,l-6):Math.max(1,l-Math.max(c,m.label.length)-9),{option:v,stackedHint:y}=optionWithoutStackedHint(inlineEditOption(m,h,r.edit,u,_),r.layout),b=isRailedSearch(r),x=n.cursorBadge??(b&&!g?enterBadge(u):void 0),S=h&&x!==void 0?` ${x}`:``;if(t.push(` ${optionRow({option:v,isCursor:h,isChecked:r.selection===`multiple`?n.select.selected.has(m.value):m.checked===!0,placeholder:b||optionUsesPlaceholder(r,g),railed:b,hintPadding:Math.max(0,c-v.label.length),theme:u})}${S}`),y!==void 0){let e=r.edit,n=h&&e!==void 0&&e.optionValue===m.value&&e.editor.kind===`key`&&e.editor.phase.kind!==`inactive`;for(let e of y.split(` `)){let r=h?n?e:foregroundWithEmphasis(e):dimWithEmphasis(e,u);t.push(` ${renderOptionRowContinuation(r)}`)}}m.description!==void 0&&(m.disabled===!0||h)&&t.push(` ${renderOptionRowContinuation(f.dim(m.description))}`),r.layout===`stacked`&&e<o-1&&t.push(``)}return p}function appendSubmitRow(e,t,n,r){if(n<0)return;let i=t===n,a=i?`${r.glyph.selectedPointer} ${r.colors.bold(`Submit`)}`:` Submit`;e.push(``,` ${renderCursorRow(a,i,r.colors)}`)}function appendSelectNotices(e,t,n,r,i){if(!(t===void 0||t.length===0)){e.push(``);for(let a of t){let t=toneGlyph(a.tone,r),o=` `.repeat(visibleLength(t)+1),l=Math.max(1,i-2-visibleLength(t)-1),u=wrapVisibleLine(a.text,l);for(let[i,s]of u.entries()){let c=noticeBody({...a,text:s},n,r);e.push(i===0?` ${t} ${c}`:` ${o}${c}`)}}}}function selectFooterHints(e,t,n){let r=[],i=`esc to cancel`,a=e.edit;if(a!==void 0&&t[n]?.value===a.optionValue)if(a.editor.kind===`key`){let e=a.editor.phase;if(e.kind!==`inactive`&&e.editor.text.length>0&&(i=`esc to clear`),e.kind===`validating`)return[i];r.push(`type your key`)}else r.push(`type to rename`);return e.filter!==void 0&&r.push(`type to filter`),r.push(`↑/↓ move`),r.push(e.selection===`multiple`?`space to toggle`:`enter to select`),e.selection===`multiple`&&r.push(`enter on Submit to confirm`),r.push(i),r}function renderActionQuestion(e,t,n){let r=[` ${t.colors.dim(`${t.glyph.dot} ${e.context}`)}`,``];for(let[n,i]of e.actions.entries())r.push(` ${optionRow({option:i,isCursor:n===e.cursor,isChecked:!1,placeholder:!0,hintPadding:0,theme:t})}`);return r.push(...questionFooter([`↑/↓ move`,`enter to select`,`esc to cancel`],t)),r.map(e=>clip(e,n))}function renderSelectQuestion(e,t,n){if(e.kind===`actions`)return renderActionQuestion(e,t,n);let r=t.colors,i=selectPresentation(e),a=i.filter?filterOptions(e.options,e.select.filter,e.searchAction):e.options,o=i.selection===`multiple`?submitRowIndex(a):-1,s=e.select.cursor,c=isRailedSearch(i),l=selectMessageRows(e.message,i.layout,t);i.filter!==void 0&&l.push(` ${c?` `:``}${searchFilter(e.select.filter,i.filter.placeholder,e.loadingFrame,t,c)}`);let u=0;for(;a[u]?.featured;)u+=1;let d=selectViewSize({search:i.filter!==void 0,filter:e.select.filter,featuredLead:u,optionCount:a.length,railed:c}),f=Math.max(0,Math.min(s-Math.floor(d/2),Math.max(0,a.length-d))),h=Math.min(f+d,a.length),g=a.slice(f,h).filter(e=>e.hint!==void 0||e.focusHint!==void 0).reduce((e,t)=>Math.max(e,t.label.length),0);a.length===0&&l.push(` ${r.dim(`(no matches)`)}`);let _=appendSelectOptionRows({rows:l,state:e,presentation:i,visible:a,start:f,end:h,cursor:s,visibleLabelWidth:g,width:n,theme:t});return appendSubmitRow(l,s,o,t),!c&&a.length>h-f&&l.push(` ${r.dim(`↑↓ ${a.length} options, showing ${f+1}${h}`)}`),_||appendSelectNotices(l,e.notices,i.layout,t,n),e.error!==void 0&&l.push(``,` ${r.red(e.error)}`),l.push(...questionFooter(c?[`esc to cancel`]:selectFooterHints(i,a,s),t)),l.map(e=>clip(e,n))}function enterBadge(e,t){return e.colors.dim(t===void 0?e.glyph.enter:`${e.glyph.enter} ${t}`)}function reasoningTrack(e){let{count:t,index:n,theme:r}=e,i=r.glyph,a=i.trackLine.repeat(e.connectorWidth),o=[];for(let e=0;e<t;e+=1)e>0&&o.push(a),o.push(e<n?i.trackFilled:e===n?i.trackCurrent:i.trackEmpty);if(!e.accent||n<0)return o.join(``);let s=o.slice(0,2*n+1).join(``);return`${r.colors.blue(s)}${o.slice(2*n+1).join(``)}`}function renderModelEditorQuestion(e,t,n){let{request:r,state:i}=e;switch(i.screen.kind){case`menu`:return renderModelEditorMenu(e,i.screen.cursor,t,n);case`model`:return renderModelEditorModelScreen(r,i.screen.select,t,n)}}function renderModelEditorMenu(e,t,i,a){let{request:o,state:s}=e,{draft:c}=s,l=modelEditorMenuRows(o,c,s.capabilities).map(e=>{if(e.disabled===!0&&e.value===`model`)return c.modelId===null?{...e}:{...e,hint:c.modelId};if(e.disabled===!0||e.value===`done`)return{...e};switch(e.value){case`model`:return{...e,hint:c.modelId??void 0};case`reasoning`:{let n=reasoningPositions(s.capabilities,c.reasoning),a=c.reasoning==="default"?-1:n.indexOf(c.reasoning),o=reasoningTrack({count:n.length,index:a,connectorWidth:1,accent:e.value===t,theme:i}),l=c.reasoning==="default"?`provider default`:c.reasoning;return{...e,hint:`${o} ${l}`}}case`tier`:return{...e,hint:c.tier===`priority`?`fast ${i.glyph.fast}`:`normal`};default:return{...e}}});return renderSelectQuestion({kind:`stacked`,message:``,options:l,select:{filter:``,cursor:Math.max(0,l.findIndex(e=>e.value===t)),selected:new Set}},i,a)}function renderModelEditorModelScreen(e,t,n,r){return renderSelectQuestion({kind:`search`,message:`Select the model`,options:e.model.kind===`pick`?e.model.options:[],placeholder:`type to search`,select:t},n,r)}function renderTextQuestion(n,r,i,s){let c=r.colors,l=[];for(let e of n.notices??[]){let t=e.tone===`info`?c.dim(e.text):e.text;l.push(`${toneGlyph(e.tone,r)} ${t}`)}l.push(...n.message.split(` `).map(e=>` ${c.bold(e)}`));let u=Math.max(4,i-4),d=n.mask?maskLine(n.editor):n.editor,f=n.editor.text.length===0?n.placeholder:void 0,p=renderInputWithBlockCursor({...visibleLine(f===void 0?d:{text:f,cursor:0},u,r.glyph.ellipsis),visible:s,inverse:c.inverse,render:f===void 0?renderInputText:e=>c.dim(renderInputText(e))});return l.push(` ${p}`),n.error!==void 0&&l.push(``,` ${c.red(n.error)}`),l.push(...questionFooter([`enter to submit`,`esc to cancel`],r)),l.map(e=>clip(e,i))}function renderAcknowledgeQuestion(e,t,n){let r=t.colors,i=[` ${r.bold(e.message)}`];if(e.lines.length>0){i.push(``);for(let t of e.lines)i.push(` ${r.dim(t)}`)}return i.push(...questionFooter([`enter to continue`],t)),i.map(e=>clip(e,n))}export{enterBadge,reasoningTrack,renderAcknowledgeQuestion,renderFlowPanel,renderModelEditorQuestion,renderSelectQuestion,renderTextQuestion};