diff --git a/packages/app/src/ui.ts b/packages/app/src/ui.ts index 11724ab6..29e0118c 100644 --- a/packages/app/src/ui.ts +++ b/packages/app/src/ui.ts @@ -75,20 +75,23 @@ const jobId=${JSON.stringify(jobId ?? null)},contextCatalog=${JSON.stringify(tri const kinds={manual:'Manual',schedule:'Schedule',linear:'Linear',github:'GitHub',cloudflareTail:'Cloudflare Tail',jobLifecycle:'Job lifecycle'}; const providerKind=t=>t.kind==='webhook'?t.config.provider:t.kind; const reflection=t=>t.reflection?.paths||contextCatalog[providerKind(t)]||[]; -const contextPaths=()=>triggers.flatMap(t=>reflection(t).filter(x=>x.path!=='*').map(x=>({path:t.slug+'.'+x.path,type:x.type,description:x.description}))); +const triggerHints=()=>triggers.map(t=>({path:t.slug+'.*',type:'context',description:kinds[providerKind(t)]+' trigger context'})); +const contextPaths=()=>triggers.flatMap(t=>[{path:t.slug+'.*',type:'context',description:kinds[providerKind(t)]+' trigger context'},...reflection(t).filter(x=>x.path!=='*').map(x=>({path:t.slug+'.'+x.path,type:x.type,description:x.description}))]); const nameOf=(items,id)=>items.find(x=>String(x.id)===String(id))?.name||String(id||'Not selected'); async function refreshAvailability(){availability=await request('/api/job-trigger-availability');const select=$('#add-trigger'),selected=select.value;select.replaceChildren(new Option('Add trigger…',''),...Object.entries(kinds).filter(([kind])=>availability[kind]).map(([kind,label])=>new Option(label,kind)));select.value=selected;select.disabled=false} const ruleLabels={status:'Status',label:'Label',assignee:'Assignee',creator:'Creator',owner:'Owner'}; const ruleOptions=type=>type==='status'?resources.options.statuses:type==='label'?resources.options.labels:resources.options.users; const ruleSummary=rule=>ruleLabels[rule.type]+' is '+nameOf(ruleOptions(rule.type),rule.targetId); function summary(t){const c=t.config||{},k=providerKind(t);if(k==='manual')return 'Prompt entered when run';if(k==='schedule')return c.cron+' · '+c.timezone;if(k==='linear')return nameOf(resources.projects,c.projectId)+' · '+(c.matchRules||[]).map(ruleSummary).join(' + ');if(k==='github'){const i=resources.installations.find(x=>String(x.installationId)===String(c.installationId));const repos=resources.repositories.get(String(c.installationId))||[];return (i?.accountLogin||c.installationId)+' · '+(repos.find(x=>String(x.id)===String(c.repositoryId))?.fullName||c.repositoryId)+' · '+c.event+'/'+c.action}if(k==='cloudflareTail')return 'Managed signed Tail endpoint'+(c.handlerCode?' · handler':'');return (c.sourceJobIds||[]).map(id=>resources.jobs.find(j=>j.id===id)?.name||id).join(', ')+' · '+(c.states||[]).join(', ')} -function render(){const rows=$('#trigger-rows');rows.innerHTML=triggers.map((t,i)=>''+esc(t.slug)+''+esc(kinds[providerKind(t)])+(t.actionRequired?'Action required':'')+''+esc(t.actionRequired||summary(t))+'
').join('');$('#trigger-empty').classList.toggle('hidden',triggers.length>0);const paths=contextPaths();$('#context-paths').innerHTML=paths.length?'Available context: '+paths.slice(0,6).map(x=>'{{'+esc(x.path)+'}}').join(' · ')+(paths.length>6?' · '+(paths.length-6)+' more':''):'Add a trigger to expose structured context.';updateSuggestions()} +function render(){const rows=$('#trigger-rows');rows.innerHTML=triggers.map((t,i)=>''+esc(t.slug)+''+esc(kinds[providerKind(t)])+(t.actionRequired?'Action required':'')+''+esc(t.actionRequired||summary(t))+'
').join('');$('#trigger-empty').classList.toggle('hidden',triggers.length>0);const hints=triggerHints();$('#context-paths').innerHTML=hints.length?'Available context: '+hints.map(x=>'').join(''):'Add a trigger to expose context.';updateSuggestions()} const promptEditor=form.elements.promptTemplate,suggestionMenu=$('#context-suggestions');let matches=[],activeSuggestion=0,expression=null; function currentExpression(){const end=promptEditor.selectionStart,start=promptEditor.value.lastIndexOf('{{',end);if(start<0||promptEditor.value.slice(start+2,end).includes('}}'))return null;const raw=promptEditor.value.slice(start+2,end);if(!/^\\s*[A-Za-z0-9_.-]*$/.test(raw))return null;return {start,end,query:raw.trim()}} function closeSuggestions(){matches=[];expression=null;suggestionMenu.classList.add('hidden');suggestionMenu.replaceChildren();promptEditor.setAttribute('aria-expanded','false');promptEditor.removeAttribute('aria-activedescendant')} function updateSuggestions(){expression=currentExpression();if(!expression)return closeSuggestions();matches=contextPaths().filter(x=>x.path.toLowerCase().includes(expression.query.toLowerCase())).slice(0,12);if(!matches.length)return closeSuggestions();activeSuggestion=Math.min(activeSuggestion,matches.length-1);suggestionMenu.innerHTML=matches.map((x,i)=>'').join('');suggestionMenu.classList.remove('hidden');promptEditor.setAttribute('aria-expanded','true');promptEditor.setAttribute('aria-activedescendant','context-option-'+activeSuggestion);$('#context-announcement').textContent=matches.length+' context suggestions available. '+matches[activeSuggestion].path+' selected.'} function selectSuggestion(index=activeSuggestion){const choice=matches[index];if(!choice||!expression)return;const before=promptEditor.value.slice(0,expression.start+2),after=promptEditor.value.slice(expression.end),closing=after.startsWith('}}')?'':'}}';promptEditor.value=before+choice.path+closing+after;const caret=before.length+choice.path.length+closing.length;promptEditor.setSelectionRange(caret,caret);closeSuggestions();promptEditor.focus();promptEditor.dispatchEvent(new Event('input',{bubbles:true}))} +function insertHint(path){const start=promptEditor.selectionStart,end=promptEditor.selectionEnd,value='{{'+path+'}}';promptEditor.setRangeText(value,start,end,'end');promptEditor.focus();promptEditor.dispatchEvent(new Event('input',{bubbles:true}));$('#context-announcement').textContent=value+' inserted.'} promptEditor.addEventListener('input',()=>{activeSuggestion=0;updateSuggestions()});promptEditor.addEventListener('click',updateSuggestions);promptEditor.addEventListener('keyup',e=>{if(!['ArrowDown','ArrowUp','Escape','Tab'].includes(e.key))updateSuggestions()});promptEditor.addEventListener('keydown',e=>{if(suggestionMenu.classList.contains('hidden'))return;if(e.key==='ArrowDown'||e.key==='ArrowUp'){e.preventDefault();activeSuggestion=(activeSuggestion+(e.key==='ArrowDown'?1:-1)+matches.length)%matches.length;updateSuggestions()}else if(e.key==='Tab'||e.key==='Enter'){e.preventDefault();selectSuggestion()}else if(e.key==='Escape'){e.preventDefault();closeSuggestions()}});suggestionMenu.addEventListener('mousedown',e=>e.preventDefault());suggestionMenu.addEventListener('click',e=>{const option=e.target.closest('[data-suggestion]');if(option)selectSuggestion(Number(option.dataset.suggestion))});promptEditor.addEventListener('blur',()=>setTimeout(closeSuggestions,100)); +$('#context-paths').addEventListener('click',e=>{const hint=e.target.closest('[data-context-hint]');if(hint)insertHint(hint.dataset.contextHint)}); function nextSlug(){let n=1;while(reservedSlugs.has('trigger-'+n))n++;const slug='trigger-'+n;reservedSlugs.add(slug);return slug} async function repositories(installationId){const key=String(installationId);if(resources.repositories.has(key))return resources.repositories.get(key);const response=await fetch('/api/github/installations/'+encodeURIComponent(key)+'/repositories');const items=response.ok?await response.json():[];resources.repositories.set(key,items);return items} function options(select,items,label,value='id'){select.replaceChildren(...items.map(x=>new Option(x[label]||x.name,String(x[value]))))} diff --git a/packages/app/test/ui.test.ts b/packages/app/test/ui.test.ts index c63dbb1d..5e4954f3 100644 --- a/packages/app/test/ui.test.ts +++ b/packages/app/test/ui.test.ts @@ -90,6 +90,19 @@ describe("pages", () => { } }); + it("shows synchronized wildcard trigger hints while retaining detailed autocomplete", () => { + const html = jobPage({ email: "owner@example.com" }, "job-1"); + expect(html).toContain("const triggerHints=()=>triggers.map"); + expect(html).toContain("path:t.slug+'.*'"); + expect(html).toContain('data-context-hint="'); + expect(html).toContain("insertHint(hint.dataset.contextHint)"); + expect(html).toContain("reflection(t).filter(x=>x.path!=='*')"); + expect(html).not.toContain("paths.slice(0,6)"); + expect(html).toContain("context suggestions available"); + expect(html).toContain("+' inserted.'"); + expectInlineScriptsToParse(html); + }); + it("renders accessible icon actions for editing and removing triggers", () => { const editor = jobPage({ email: "owner@example.com" }, "job-1"); expect(editor).toContain('aria-label="Edit trigger" title="Edit trigger"');