@kern0x1b/penstock-cli 26.10.3 → 26.10.4
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/dist/licensing.json +1 -1
- package/dist/penstock.js +458 -197
- package/dist/web/assets/{AgentSettings-C-1YBic4.js → AgentSettings-BvzT4Itb.js} +1 -1
- package/dist/web/assets/{AgentsDialog-CSu8pyh7.js → AgentsDialog-BqY3wKcV.js} +1 -1
- package/dist/web/assets/BpmnCanvas-BTdM1l9l.js +1905 -0
- package/dist/web/assets/BpmnDocument-uifU3NGG.js +24 -0
- package/dist/web/assets/{ConflictDialog-nygfF7Dn.js → ConflictDialog-C4A9-Psr.js} +1 -1
- package/dist/web/assets/{DiffDocument-DUrje9io.js → DiffDocument-BMTSevd7.js} +1 -1
- package/dist/web/assets/{DmnDocument-fn7AH7P1.js → DmnDocument-B8wWuhfX.js} +9 -9
- package/dist/web/assets/{FormDocument-BbyRqjib.js → FormDocument-bQ5VRsrh.js} +1 -1
- package/dist/web/assets/{FormStepView-BYRMBW0U.js → FormStepView-DbMCFSO1.js} +1 -1
- package/dist/web/assets/{GraphAwareLayoutService-BiLVDmB7.js → GraphAwareLayoutService-CePcz6pL.js} +1 -1
- package/dist/web/assets/NewFileDialog-DsoFURD9.js +47 -0
- package/dist/web/assets/{QuickOpen-D-u9hdSt.js → QuickOpen-mzlfRqvG.js} +1 -1
- package/dist/web/assets/{ScriptPane-BQorfmyv.js → ScriptPane-C9q5wLfO.js} +2 -2
- package/dist/web/assets/{ScriptRunDialog-DjBW0LRD.js → ScriptRunDialog-Dm-_0H1Z.js} +1 -1
- package/dist/web/assets/{SettingsDialog-DvYUAKpH.js → SettingsDialog-Bhwwtyg7.js} +1 -1
- package/dist/web/assets/{ShortcutsDialog-BQ5BYHj3.js → ShortcutsDialog-BXP0h7Tp.js} +1 -1
- package/dist/web/assets/{SourceDocument-BfHVXxuk.js → SourceDocument-DgrNCprS.js} +1 -1
- package/dist/web/assets/{TextDocument-BPepqeGu.js → TextDocument-D8N0uaQa.js} +1 -1
- package/dist/web/assets/{UnreadableFileLayer-D5B6nixz.js → UnreadableFileLayer-D2Rcxnt7.js} +1 -1
- package/dist/web/assets/{UploadDialog-BzIvQ1YS.js → UploadDialog-Dinga4TP.js} +1 -1
- package/dist/web/assets/apply-CzXxiRWK.js +1 -0
- package/dist/web/assets/{bpmnTheme-Divwrkjy.js → bpmnTheme-db4tYvop.js} +1 -1
- package/dist/web/assets/bridgeInstance-BPwbITOf.js +850 -0
- package/dist/web/assets/{commands-N2yKHjf_.js → commands-BAnBjEk3.js} +1 -1
- package/dist/web/assets/de-Cd75cvcZ.js +1 -0
- package/dist/web/assets/{dialog-DSf66Ory.js → dialog-KVjfqSrB.js} +1 -1
- package/dist/web/assets/{diff-DkfgVbKD.js → diff-Cw0ENHJR.js} +1 -1
- package/dist/web/assets/{documentBridge-DSWBQZRP.js → documentBridge-D5SaBThP.js} +8 -8
- package/dist/web/assets/{documents-C4vr2KnQ.js → documents-Dkor36oS.js} +1 -1
- package/dist/web/assets/{duplicate-C-AiqE1T.js → duplicate-ud-StKuO.js} +1 -1
- package/dist/web/assets/{engines-DG8KgTeX.js → engines-BQRXHtW8.js} +1 -1
- package/dist/web/assets/es-D2PUEg_Z.js +1 -0
- package/dist/web/assets/{formChecks-B_ZFz9DR.js → formChecks-DFrCuO4I.js} +1 -1
- package/dist/web/assets/fr-DkGYGarc.js +1 -0
- package/dist/web/assets/{frontend-BLfXy4OQ.js → frontend-B05iyhCp.js} +1 -1
- package/dist/web/assets/{httpHost-Cll6HteE.js → httpHost-DX-P_FDg.js} +1 -1
- package/dist/web/assets/{index.es-DQAdm_u9.js → index.es-DR-czZl7.js} +1 -1
- package/dist/web/assets/{index.es-B0wi5_fv.js → index.es-dFUgH_-g.js} +1 -1
- package/dist/web/assets/{intelligence-glU4hcHy.js → intelligence-BkxcJMU_.js} +1 -1
- package/dist/web/assets/ja-De4NAvK-.js +1 -0
- package/dist/web/assets/{jspdf.es.min-xdeBAA_m.js → jspdf.es.min-BMfOi0te.js} +3 -3
- package/dist/web/assets/ko-Ci27KQ5n.js +1 -0
- package/dist/web/assets/{lib-DJ1i09sd.js → lib-C3uYnGWx.js} +1 -1
- package/dist/web/assets/{engine-BPUfS3r2.js → moddle-CGpeSs3G.js} +1 -1
- package/dist/web/assets/{platform-BS7bgwr3.js → platform-zEAtGnYc.js} +1 -1
- package/dist/web/assets/preferences-K6VRM9Ah.js +1 -0
- package/dist/web/assets/pt-BR-CNmIseZJ.js +1 -0
- package/dist/web/assets/ru-BtK7W4xW.js +1 -0
- package/dist/web/assets/{run-Df8AD_as.js → run-fV7AmV4q.js} +1 -1
- package/dist/web/assets/{samples-chRLnLcq.js → samples-D-3Bquy_.js} +1 -1
- package/dist/web/assets/testCaseRuns-C-0blM3d.js +1 -0
- package/dist/web/assets/translator-Dvvasoz-.js +1 -0
- package/dist/web/assets/{ui-AICOEtWR.js → ui-TBzvKaKQ.js} +1 -1
- package/dist/web/assets/{useTrace-B3gDUL0X.js → useTrace-BgpeAcQR.js} +1 -1
- package/dist/web/assets/{web-BtxFF0jJ.js → web-qJCedJJw.js} +8 -8
- package/dist/web/assets/zh-DxI_A5kC.js +1 -0
- package/dist/web/index.html +20 -20
- package/package.json +4 -1
- package/dist/web/assets/BpmnCanvas-DnBLxMNQ.js +0 -1905
- package/dist/web/assets/BpmnDocument-BPpyqTzJ.js +0 -24
- package/dist/web/assets/NewFileDialog-Ce_cTeOv.js +0 -34
- package/dist/web/assets/apply-B0vauBJn.js +0 -1
- package/dist/web/assets/bridgeInstance-D0bzpNGz.js +0 -600
- package/dist/web/assets/de-izeWpfua.js +0 -1
- package/dist/web/assets/es-CRMYEYWD.js +0 -1
- package/dist/web/assets/fr-8pTyPyAI.js +0 -1
- package/dist/web/assets/ja-I1fjeFMy.js +0 -1
- package/dist/web/assets/ko-CFd9C-RA.js +0 -1
- package/dist/web/assets/preferences-C_EXTz45.js +0 -1
- package/dist/web/assets/pt-BR-Dl_APu95.js +0 -1
- package/dist/web/assets/ru-_vhcOTod.js +0 -1
- package/dist/web/assets/testCaseRuns-DLEqANBi.js +0 -1
- package/dist/web/assets/translator-CS9eR0My.js +0 -1
- package/dist/web/assets/zh-Do_q1RZ2.js +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
import"./rolldown-runtime-B8BYMn-z.js";import{c as e,h as t,i as n,r,s as i,u as a}from"./vendor-NJP34MHU.js";import{i as o,n as s}from"./translator-CS9eR0My.js";import{t as c}from"./bridgeInstance-D0bzpNGz.js";import{n as l,r as u}from"./httpHost-Cll6HteE.js";import{c as d,n as f}from"./ui-AICOEtWR.js";import{i as p,r as m}from"./preferences-C_EXTz45.js";var h=[`off`,`bottom`,`sidebar`],g=`bottom`;function aiChatPlaceOf(e){return h.find(t=>t===e)??`bottom`}function aiChatSurfaces(e){let t=e!==`off`;return{bottomInput:e===`bottom`,card:e===`bottom`,chatTab:t,chatsList:t,toolbarButton:t,paletteAction:t,agentProcess:t}}var _=null;function registerAgentsOpener(e){return _=e,()=>{_===e&&(_=null)}}async function openAgentsScreen(){if(_)_();else try{await c().agentsHost?.().openSettings()}catch(e){console.error(`The AI agents screen did not open:`,e)}}function normalise(e){return e.replace(/\\/g,`/`).replace(/^\.\//,``).replace(/\/+$/,``)}function pathsMatch(e,t){return normalise(e)===normalise(t)}function baseName(e){return normalise(e).split(`/`).filter(Boolean).pop()??e}var v=1e3,y=15e3,b=2e3,x={status:`loading`,entries:[],queue:[],permission:null,error:null,options:[]},S={text:``,mentions:[],dismissed:[],focus:0};function isLicenceRefusal(e){return e instanceof u&&e.status===402}function newClientId(){return`c${Date.now().toString(36)}${Math.random().toString(36).slice(2,10)}`}function upsert(e,t){let n=e.findIndex(e=>e.id===t.id);if(n<0){let n=[...e,t];return n.length>b?n.slice(n.length-b):n}let r=e.slice();return r[n]=t,r}function withPlan(e,t){let n=-1;for(let t=e.length-1;t>=0;--t)if(e[t].kind===`plan`){n=t;break}if(n<0)return upsert(e,{kind:`plan`,id:`plan`,items:t});let r=e.slice();return r[n]={kind:`plan`,id:e[n].id,items:t},r}function withApproved(e,t){let n=e.status===`done`&&e.diffs.length===0?t?.get(l(e)):void 0;return n?{...e,diffs:n}:e}function applyToTranscript(e,t,n){switch(t.type){case`entry`:{let r=t.entry.kind===`tool`?{...t.entry,tool:withApproved(t.entry.tool,n)}:t.entry,i=e.entries.find(e=>e.id===r.id);return i&&JSON.stringify(i)===JSON.stringify(r)?e:{...e,entries:upsert(e.entries,r)}}case`tool`:return{...e,entries:upsert(e.entries,{kind:`tool`,id:l(t.tool),tool:withApproved(t.tool,n)})};case`plan`:return{...e,entries:withPlan(e.entries,t.items)};case`queue`:return{...e,queue:t.queue};case`permission`:return{...e,permission:t.permission};case`error`:return{...e,error:t.error};case`options`:return{...e,options:t.options};default:return e}}function sameChats(e,t){return e.length===t.length&&e.every((e,n)=>JSON.stringify(e)===JSON.stringify(t[n]))}var AssistantStore=class{state;listeners=new Set;host;options;client;pending=new Map;approved=new Map;loading=new Map;openings=new Set;expected=null;waitingChat=null;manage;control=null;started=!1;disposed=!1;failures=0;resyncing=null;constructor(e,t={}){this.host=e,this.options=t,this.client=t.client??newClientId(),this.manage=t.onManageAgents??null;let n=t.place?t.place():g;this.state={enabled:n!==`off`,place:n,loaded:!1,connected:!1,choices:[],chats:[],transcripts:{},active:{},shown:{},folded:{},revealed:{},focused:null,agentId:null,drafts:{},picks:{},canManageAgents:!!t.onManageAgents,failure:null}}snapshot=()=>this.state;subscribe=e=>(this.listeners.add(e),()=>{this.listeners.delete(e)});commit(e){this.state={...this.state,...e},this.recordOpenings();for(let e of[...this.listeners])try{e()}catch(e){console.error(`An assistant listener failed:`,e)}}recordOpenings(){for(let e of Object.keys(this.state.active))!this.openings.has(e)&&this.settled(e)&&this.openings.add(e)}settled(e){if(!this.state.loaded)return!1;let t=this.activeChat(e);if(t===null)return!0;let n=this.state.transcripts[t.id];return n!==void 0&&n.status!==`loading`}opened(e){return!this.state.enabled||this.openings.has(e)}describe(e){return e instanceof u&&e.failure?e.failure:{key:`app.agent.failed`,detail:e instanceof Error?e.message:String(e)}}wait(e){return this.options.wait?this.options.wait(e):new Promise(t=>setTimeout(t,e))}allowed(){return!this.options.place||this.options.place()!==`off`}setPlace(e){if(e===this.state.place)return;let t=e!==`off`;this.commit({place:e,enabled:t}),t?this.started&&this.begin():this.halt()}start(){return this.disposed?Promise.resolve():(this.started=!0,this.allowed()?(this.state.enabled||this.commit({enabled:!0}),this.control&&!this.control.signal.aborted?Promise.resolve():this.begin()):(this.state.enabled&&this.commit({enabled:!1}),Promise.resolve()))}begin(){let e=new AbortController;this.control=e;let t=this.resync();return this.pump(e),t}halt(){this.control?.abort(),this.control=null,this.commit({connected:!1})}dispose(){this.disposed=!0,this.started=!1,this.halt(),this.listeners.clear()}async pump(e){for(;!e.signal.aborted;)try{let t=await this.host.next(this.client,e.signal);if(e.signal.aborted)return;this.failures>0&&(this.failures=0,this.state.connected||this.commit({connected:!0})),t&&this.receive(t)}catch{if(e.signal.aborted)return;this.failures+=1,this.state.connected&&this.commit({connected:!1});let t=Math.min(this.options.maxRetryMs??y,(this.options.retryMs??v)*2**Math.min(this.failures-1,6));if(await this.wait(t),e.signal.aborted)return;await this.resync()}}resync(){if(this.resyncing)return this.resyncing;let e=(async()=>{try{let[e,t]=await Promise.all([this.host.choices().catch(()=>this.state.choices),this.host.chats()]);this.commit({choices:e,loaded:!0,connected:!0,enabled:t.enabled&&this.allowed(),failure:null}),this.takeChats(t.chats);let n=Object.keys(this.state.transcripts);await Promise.all(n.map(e=>this.fetchChat(e,!0)))}catch(e){this.state.loaded||this.commit({loaded:!0}),this.commit({failure:this.describe(e)})}finally{this.resyncing=null}})();return this.resyncing=e,e}takeChats(e){let t=new Set(e.map(e=>e.id)),n=Object.fromEntries(Object.entries(this.state.active).map(([e,n])=>[e,n!==null&&t.has(n)?n:null])),r=Object.fromEntries(Object.entries(this.state.transcripts).filter(([e])=>t.has(e))),i=this.state.chats,a=!sameChats(i,e),o=Object.entries(n).some(([e,t])=>t!==this.state.active[e])||Object.keys(r).length!==Object.keys(this.state.transcripts).length;if(a||o){for(let t of e){let e=i.find(e=>e.id===t.id);(t.state===`working`||t.state===`waiting`||t.state===`connecting`||t.state===`failed`)&&e?.state!==t.state&&(this.state={...this.state,shown:{...this.state.shown,[t.id]:!0}},t.state===`waiting`&&(this.state={...this.state,folded:{...this.state.folded,[t.id]:!1}}))}this.commit({chats:e,active:n,transcripts:r});for(let t of e){let e=i.find(e=>e.id===t.id);t.state===`waiting`&&e?.state!==`waiting`&&this.revealWaiting(t.id),t.state===`waiting`&&!this.state.transcripts[t.id]&&this.open(t.id)}}}receive(e){switch(e.type){case`sync`:this.resync();return;case`chats`:this.takeChats(e.chats);return;case`enabled`:this.commit({enabled:e.enabled&&this.allowed()});return;default:{let t=e.chatId,n=this.state.transcripts[t];if(!n){this.state.chats.some(e=>e.id===t)&&(this.pending.set(t,[...this.pending.get(t)??[],e]),this.open(t));return}if(n.status===`loading`){this.pending.set(t,[...this.pending.get(t)??[],e]);return}let r=applyToTranscript(n,e,this.approved);if(r===n)return;e.type===`options`&&this.learn(t);let i={transcripts:{...this.state.transcripts,[t]:r}};if(e.type===`permission`&&e.permission&&e.permission.id!==n.permission?.id){i.folded={...this.state.folded,[t]:!1},i.shown={...this.state.shown,[t]:!0},this.commit(i),this.revealWaiting(t);return}this.commit(i)}}}fetchChat(e,t){let n=this.loading.get(e);if(n)return n;let r=this.state.transcripts[e];!t||!r?this.commit({transcripts:{...this.state.transcripts,[e]:{...r??x,status:`loading`}}}):this.commit({transcripts:{...this.state.transcripts,[e]:{...r,status:`loading`}}});let i=(async()=>{try{let t=await this.host.chat(e),n={status:`ready`,entries:t.entries,queue:t.queue,permission:t.permission,error:t.error,options:t.options};for(let t of this.pending.get(e)??[])n=applyToTranscript(n,t,this.approved);this.pending.delete(e),this.state.chats.some(t=>t.id===e)&&this.commit({transcripts:{...this.state.transcripts,[e]:n}}),this.mergeSummary(t)}catch(t){this.pending.delete(e);let n=t instanceof u?t.status:null,r={...this.state.transcripts};n===404?delete r[e]:r[e]={...r[e]??x,status:`failed`},this.commit({transcripts:r})}finally{this.loading.delete(e)}})();return this.loading.set(e,i),i}mergeSummary(e){let t=this.state.chats.findIndex(t=>t.id===e.chat.id);if(t<0||JSON.stringify(this.state.chats[t])===JSON.stringify(e.chat))return;let n=this.state.chats.slice();n[t]=e.chat,this.commit({chats:n})}open(e){let t=this.state.transcripts[e];return t&&t.status!==`failed`&&t.status!==`missing`?this.loading.get(e)??Promise.resolve():this.fetchChat(e,!1)}focus(e){if(e!==this.state.focused&&(this.commit({focused:e}),e&&this.waitingChat)){let e=this.waitingChat;this.waitingChat=null,this.openChat(e)}}unfocus(e){this.state.focused===e&&this.commit({focused:null})}activeChat(e){let t=e?this.state.active[e]:null;return t?this.state.chats.find(e=>e.id===t)??null:null}select(e,t){this.commit({active:{...this.state.active,[e]:t}}),t&&this.open(t)}expectChat(e,t){this.expected={chatId:e,file:t}}settle(e,t){if(!this.state.loaded)return;if(this.expected&&t&&pathsMatch(this.expected.file,t)){let{chatId:t}=this.expected;this.expected=null,this.select(e,t);return}if(e in this.state.active)return;let n=(t?this.state.chats.filter(e=>e.files.some(e=>pathsMatch(e,t))):[]).reduce((e,t)=>e===null||t.updated>e.updated?t:e,null);this.select(e,n?.id??null)}get onManageAgents(){return this.manage}setManageAgents(e){return this.manage=e,this.commit({canManageAgents:e!==null}),()=>{this.manage===e&&(this.manage=null,this.commit({canManageAgents:!1}))}}changeAgent(e,t){this.activeChat(e)&&this.select(e,null),this.pickAgent(t)}pickAgent(e){this.commit({agentId:e,picks:{}})}learn(e){let t=this.state.chats.find(t=>t.id===e)?.agentId,n=this.state.choices.find(e=>e.id===t);!n||(n.options??[]).length>0||this.host.choices().then(e=>this.commit({choices:e}),()=>void 0)}async pickOption(e,t,n,r){if(!t){let t=this.agentFor(null);if(!t)return;let i=this.state.picks[e],a=i&&i.agentId===t?i.values:{};this.commit({picks:{...this.state.picks,[e]:{agentId:t,values:{...a,[n.id]:r}}}});return}let i=this.state.transcripts[t.id],chosen=e=>e.map(e=>e.id===n.id?{...e,current:r}:e);i&&this.commit({transcripts:{...this.state.transcripts,[t.id]:{...i,options:chosen(i.options)}}});try{let e=await this.host.setOption(t.id,n.id,r),i=this.state.transcripts[t.id];i&&this.commit({transcripts:{...this.state.transcripts,[t.id]:{...i,options:e}}})}catch(e){let n=this.state.transcripts[t.id];n&&i&&this.commit({transcripts:{...this.state.transcripts,[t.id]:{...n,options:i.options}}}),this.commit({failure:this.describe(e)})}}agentFor(e){return e?e.agentId:this.state.agentId?this.state.agentId:this.state.choices.find(e=>e.available)?.id??null}reveal(e){this.commit({revealed:{...this.state.revealed,[e]:(this.state.revealed[e]??0)+1}})}revealWaiting(e){if(this.state.place===`sidebar`)for(let[t,n]of Object.entries(this.state.active))n===e&&this.reveal(t)}openChat(e){let t=this.state.focused;t?(this.select(t,e),this.reveal(t)):this.waitingChat=e}showCard(e){this.commit({shown:{...this.state.shown,[e]:!0},folded:{...this.state.folded,[e]:!1}})}returnToCard(e){let t=this.activeChat(e);t&&this.state.place===`bottom`&&this.showCard(t.id)}setFolded(e,t){!!this.state.folded[e]!==t&&this.commit({folded:{...this.state.folded,[e]:t}})}dismissCard(e){this.commit({shown:{...this.state.shown,[e]:!1}})}draft(e){return this.state.drafts[e]??S}setDraft(e,t){this.commit({drafts:{...this.state.drafts,[e]:{...this.draft(e),...t}}})}setText(e,t){t!==this.draft(e).text&&this.setDraft(e,{text:t})}mention(e,t){let n=this.draft(e),r=mentionKey(t);n.mentions.some(e=>mentionKey(e)===r)||this.setDraft(e,{mentions:[...n.mentions,t],dismissed:n.dismissed.filter(e=>e!==r)})}unmention(e,t){let n=this.draft(e);this.setDraft(e,{mentions:n.mentions.filter(e=>mentionKey(e)!==t),dismissed:n.dismissed.includes(t)?n.dismissed:[...n.dismissed,t]})}releaseDismissed(e,t){let n=this.draft(e),r=n.dismissed.filter(e=>!e.startsWith(C)||t.includes(e.slice(C.length)));r.length!==n.dismissed.length&&this.setDraft(e,{dismissed:r})}focusInput(e){this.state.place===`sidebar`&&this.reveal(e),this.setDraft(e,{focus:this.draft(e).focus+1})}compose(e,t,n){let r=this.draft(e),i=r.text.trim()===``?n:r.text.includes(n)?r.text:`${r.text.trimEnd()}\n${n}`,a=mentionKey(t),o=r.mentions.some(e=>mentionKey(e)===a)?r.mentions:[...r.mentions,t];this.setDraft(e,{text:i,mentions:o,dismissed:r.dismissed.filter(e=>e!==a),focus:r.focus+1})}clearDraft(e){this.setDraft(e,{text:``,mentions:[]})}async create(e,t=null){try{let n=await this.host.create(t??this.agentFor(null));return this.takeChats([n,...this.state.chats.filter(e=>e.id!==n.id)]),this.commit({transcripts:{...this.state.transcripts,[n.id]:{status:`ready`,entries:[],queue:[],permission:null,error:null,options:[]}}}),e&&this.select(e,n.id),n}catch(e){return this.commit({failure:this.describe(e)}),null}}async rename(e,t){let n=t.trim();if(n)try{let t=await this.host.rename(e,n);this.takeChats(this.state.chats.map(e=>e.id===t.id?t:e))}catch(e){this.commit({failure:this.describe(e)})}}async remove(e){try{await this.host.remove(e)}catch(e){if(!(e instanceof u&&e.status===404)){this.commit({failure:this.describe(e)});return}}this.takeChats(this.state.chats.filter(t=>t.id!==e))}async send(e){let t=e.text.trim();if(!t)return`refused`;let n=this.activeChat(e.editor);if(!await e.authorize())return`refused`;let r=n===null;try{if(!n){if(n=await this.create(e.editor),!n)return`failed`;await this.applyPicks(e.editor,n)}let r=n.id;return this.state.place===`sidebar`?(this.reveal(e.editor),this.commit({failure:null})):this.commit({shown:{...this.state.shown,[r]:!0},folded:{...this.state.folded,[r]:!1},failure:null}),(await this.host.prompt(r,t,e.context)).queued?`queued`:`sent`}catch(t){return isLicenceRefusal(t)?(r&&n&&await this.discard(e.editor,n.id),await e.authorize(),`refused`):(this.commit({failure:this.describe(t)}),`failed`)}}async discard(e,t){this.select(e,null),await this.remove(t)}async applyPicks(e,t){let n=this.state.picks[e];if(!n)return;let{[e]:r,...i}=this.state.picks;if(this.commit({picks:i}),n.agentId===t.agentId)for(let[e,r]of Object.entries(n.values))try{let n=await this.host.setOption(t.id,e,r),i=this.state.transcripts[t.id];i&&this.commit({transcripts:{...this.state.transcripts,[t.id]:{...i,options:n}}})}catch{}}async tryAgain(e,t){let n=[...this.state.transcripts[e]?.entries??[]].reverse().find(e=>e.kind===`user`);if(!n||n.kind!==`user`||!await t())return`refused`;try{return this.commit({shown:{...this.state.shown,[e]:!0},folded:{...this.state.folded,[e]:!1},failure:null}),(await this.host.prompt(e,n.text,null)).queued?`queued`:`sent`}catch(e){return isLicenceRefusal(e)?(await t(),`refused`):(this.commit({failure:this.describe(e)}),`failed`)}}async unqueue(e,t,n){let r=this.state.transcripts[e],i=r?r.queue.findIndex(e=>e.id===t):-1,a=i>=0&&r?r.queue[i]:null;r&&this.commit({transcripts:{...this.state.transcripts,[e]:{...r,queue:r.queue.filter(e=>e.id!==t)}}});try{await this.host.unqueue(e,t)}catch(t){if(t instanceof u&&t.status===404)return;this.commit({failure:this.describe(t)});let n=this.state.transcripts[e];if(a&&n&&!n.queue.some(e=>e.id===a.id)){let t=[...n.queue];t.splice(Math.min(i,t.length),0,a),this.commit({transcripts:{...this.state.transcripts,[e]:{...n,queue:t}}})}return}if(!a)return;let o=this.draft(n),s=o.text.trim()===``?a.text:`${o.text.trimEnd()}\n${a.text}`;this.setDraft(n,{text:s,focus:o.focus+1})}async stop(e){try{await this.host.stop(e)}catch(e){this.commit({failure:this.describe(e)})}}async signIn(e,t){if(await t())try{await this.host.signIn(e)}catch(e){if(isLicenceRefusal(e)){await t();return}this.commit({failure:this.describe(e)})}}async answer(e,t,n){let r=this.state.transcripts[e];if(r?.permission?.id===t){r.permission.options.find(e=>e.id===n)?.kind===`allow`&&r.permission.tool.diffs.length>0&&this.approved.set(l(r.permission.tool),r.permission.tool.diffs),this.commit({transcripts:{...this.state.transcripts,[e]:{...r,permission:null}}});try{await this.host.answer(e,t,n)}catch(t){if(t instanceof u&&t.status===409)return;this.commit({failure:this.describe(t)}),this.fetchChat(e,!0)}}}retry(e,t){return t&&this.select(t,e),this.fetchChat(e,!0)}dismissFailure(){this.state.failure!==null&&this.commit({failure:null})}},C=`element:`;function mentionKey(e){return e.kind===`element`?`${C}${e.id}`:`file:${e.path}`}function optionsFor(e,t,n,r){if(n)return e.transcripts[n.id]?.options??[];let i=e.picks[t],a=i&&i.agentId===r?i.values:{};return(e.choices.find(e=>e.id===r)?.options??[]).map(e=>a[e.id]!==void 0&&e.values.some(t=>t.id===a[e.id])?{...e,current:a[e.id]}:e)}var w=null,T=!1,release=null;function aiChatPlace(){return aiChatPlaceOf(p().aiChat)}function aiChatOn(){return aiChatPlace()!==`off`}function createAssistant(e={}){let t=c().agentHost?.();return t?new AssistantStore(t,{place:aiChatPlace,onManageAgents:()=>void openAgentsScreen(),...e}):null}function setAssistant(e){if(release?.(),release=null,w=e,T=!0,!e)return;let t=m(()=>{e.setPlace(aiChatPlace())}),refocused=()=>{e.snapshot().enabled&&e.resync()},n=typeof window>`u`?null:window;n?.addEventListener(`focus`,refocused),release=()=>{t(),n?.removeEventListener(`focus`,refocused)}}function getAssistant(){if(!T){T=!0;try{setAssistant(createAssistant())}catch{w=null}}return w}function installOpenChat(){let e=globalThis,open=e=>getAssistant()?.openChat(e);if(e.penstockOpenChat=open,e.pendingOpenChat){let t=e.pendingOpenChat;delete e.pendingOpenChat,open(t)}return()=>{e.penstockOpenChat===open&&delete e.penstockOpenChat}}function useTick(t,r,i=Object.is){let[,o]=e(0),s=n(r);s.current=r;let c=n(r()),l=r();return c.current=l,a(()=>t(()=>{let e=s.current();i(c.current,e)||(c.current=e,o(e=>e+1))}),[t]),l}var E=6e4,D=60*E,O=24*D;function ago(e,t=Date.now()){let n=t-e,r;try{r=new Intl.RelativeTimeFormat(s(),{numeric:`auto`,style:`short`})}catch{r=new Intl.RelativeTimeFormat(`en`,{numeric:`auto`,style:`short`})}return!Number.isFinite(n)||n<E?r.format(0,`minute`):n<D?r.format(-Math.floor(n/E),`minute`):n<O?r.format(-Math.floor(n/D),`hour`):r.format(-Math.floor(n/O),`day`)}var k=/(?:fill|stroke)\s*[:=]\s*["']?\s*(#[0-9a-fA-F]{3,8}|rgba?\([^)]*\)|[a-zA-Z]+)/g,A=new Set([`none`,`transparent`,`currentcolor`,`inherit`]),j=2e5,M=new Map;function isSingleColour(e){let t=new Set;for(let n of e.matchAll(k)){let e=n[1].toLowerCase().replace(/\s+/g,``);A.has(e)||t.add(e)}return t.size<=1&&!/<(?:linearGradient|radialGradient|image)\b/i.test(e)}var N=/^data:image\/svg\+xml;base64,([A-Za-z0-9+/=]+)$/,P=/^(?:data:image\/svg\+xml;base64,[A-Za-z0-9+/=]+|https?:\/\/\S+|\/[^\s/]\S*)$/;function loadable(e){return typeof e==`string`&&P.test(e)}function decoded(e){let t=N.exec(e);if(!t)return null;try{return atob(t[1])}catch{return``}}function singleColour(e){let t=decoded(e);if(t!==null)return Promise.resolve(t!==``&&t.length<=j&&isSingleColour(t));let n=M.get(e);return n||(n=fetch(e).then(e=>e.ok?e.text():``).then(t=>t!==``&&t.length<=j&&/\.svg(?:$|\?)/i.test(e)&&isSingleColour(t)).catch(()=>!1),M.set(e,n)),n}function monogramOf(e){return(Array.from(e.trim())[0]??`?`).toLocaleUpperCase()}function AgentLogo({name:t,icon:n,class:i,size:o=16}){let s=loadable(n)?n:null,[c,l]=e(null),[u,d]=e(!1);a(()=>{if(d(!1),l(null),!s)return;let e=!0;return singleColour(s).then(t=>{e&&l(t)}),()=>{e=!1}},[s]);let f=`agent-logo${o===20?` agent-logo--large`:``}${i?` ${i}`:``}`;return!s||u?r(`span`,{class:`${f} agent-logo--monogram type-badge`,"aria-hidden":`true`,children:monogramOf(t)}):c===!0?r(`span`,{class:`${f} agent-logo--mask`,style:{maskImage:`url("${s}")`,WebkitMaskImage:`url("${s}")`},"aria-hidden":`true`}):r(`img`,{class:f,src:s,alt:``,width:o,height:o,onError:()=>d(!0)})}var F=/^default\b/i,I=28,L=4;function settingsOf(e){let first=t=>e.find(e=>e.category===t)??null;return{model:first(`model`),level:first(`thought_level`),mode:first(`mode`)}}function currentOf(e){return e.values.find(t=>t.id===e.current)??null}function isDefaultValue(e){return F.test(e.id)||F.test(e.name)}function standsFor(e){let t=e.description?.trim()??``,n=t.split(/\s+/).length;return t!==``&&t.length<=I&&n<=L&&/^[A-Z0-9]/.test(t)&&!/[.!?]$/.test(t)?t:null}function rowName(e){let t=currentOf(e);return t?e.category===`model`?modelName(e)??t.name:isDefaultValue(t)?standsFor(t)??t.name:t.name:e.current}function modelName(e){let t=currentOf(e);return t?isDefaultValue(t)?standsFor(t):t.name:e.current}function levelName(e){let t=currentOf(e);return t?isDefaultValue(t)?null:t.name:e.current}function modelAndLevel(e){return[e.model?modelName(e.model):null,e.level?levelName(e.level):null].filter(e=>e!==null).join(` `)}function modeSuffix(e){let t=e.mode;if(!t)return null;let n=currentOf(t);return n&&isDefaultValue(n)?null:modeName(t)}function settingsLabel(e){return[modelAndLevel(e),modeSuffix(e)].filter(e=>e!==null&&e!==``).join(` · `)}function modeName(e){return currentOf(e)?.name??e.current}var R=6,z=22,B=2;function groupsOf(e){let t=[];for(let n of e){let e=t[t.length-1];e&&e.name===n.group?e.values.push(n):t.push({name:n.group,values:[n]})}return t}function isSegmented(e){let t=e.values;return t.length<2||t.length>R||!t.every(e=>!e.description&&!e.group)?!1:t.reduce((e,t)=>e+Array.from(t.name).length+B,0)<=z}var V=360,H=72,U=60,W=16,G={kind:`rows`};function Segmented({option:e,onPick:t}){return r(`div`,{class:`assistant-form__segments`,role:`radiogroup`,"aria-label":e.name,children:e.values.map(n=>r(`button`,{type:`button`,role:`radio`,"aria-checked":n.id===e.current,"data-nav":`segment`,class:`assistant-form__segment type-body`,onClick:()=>t(e,n.id),children:n.name},n.id))})}function Select({text:e,logo:t,onOpen:n,label:i,nav:a=`item`}){return r(`button`,{type:`button`,class:`assistant-form__select type-body`,"data-nav":a,"aria-label":`${i}: ${e}`,"aria-haspopup":`listbox`,onClick:n,children:[t,r(`span`,{class:`assistant-form__select-text`,children:e}),r(f,{name:`chevronDown`,class:`assistant-form__select-chevron`})]})}function Row({label:e,children:t}){return r(`div`,{class:`assistant-form__row`,children:[r(`span`,{class:`assistant-form__label type-help`,children:e}),r(`div`,{class:`assistant-form__control`,children:t})]})}function OptionList({option:e,onPick:t}){return r(`div`,{class:`assistant-form__choices`,role:`listbox`,"aria-label":e.name,children:groupsOf(e.values).map((n,i)=>r(`div`,{role:`presentation`,class:`assistant-form__group`,children:[n.name&&r(`div`,{class:`assistant-form__subheading type-help`,role:`presentation`,children:n.name}),n.values.map(n=>{let i=n.id===e.current;return r(`button`,{type:`button`,role:`option`,"aria-selected":i,"data-nav":`item`,"data-current":i?`true`:void 0,class:`assistant-form__item type-body`,onClick:()=>t(e,n.id),children:[r(`span`,{class:`assistant-form__check`,"aria-hidden":`true`,children:i&&r(f,{name:`check`})}),r(`span`,{class:`assistant-form__text`,children:[r(`span`,{class:`assistant-form__name`,children:n.name}),n.description&&r(`span`,{class:`assistant-form__description type-help`,children:n.description})]})]},n.id)})]},`${n.name??``}:${i}`))})}function navigate(e,t){let n=document.activeElement,r=Array.from(e.querySelectorAll(`[data-nav]`)),i=n?r.indexOf(n):-1;if(i<0)return!1;if(n?.dataset.nav===`segment`&&(t===`ArrowRight`||t===`ArrowLeft`)){let e=Array.from(n.parentElement?.querySelectorAll(`[data-nav="segment"]`)??[]);return e[(e.indexOf(n)+(t===`ArrowRight`?1:-1)+e.length)%e.length]?.focus(),!0}return t!==`ArrowDown`&&t!==`ArrowUp`?!1:(r[(i+(t===`ArrowDown`?1:-1)+r.length)%r.length]?.focus(),!0)}function listLimit(e,t){return Math.max(H,Math.min(V,Math.round(e-t-W-U)))}function AgentSettings({store:s,state:c,editor:l,chat:u,agentId:p,options:m,busy:h,started:g}){let _=n(null),v=n(null),y=n(null),[b,x]=e(!1),[S,C]=e(G),[w,T]=e(V),E=settingsOf(m),D=c.choices.find(e=>e.id===p),O=c.choices.filter(e=>e.available),k=D?.name??(p===``?o(`app.assistant.input.noAgent`):p),A=O.length===0&&c.canManageAgents,j=settingsLabel(E),M=[E.model,E.level,E.mode].filter(e=>e!==null),N=S.kind===`option`?M.find(e=>e.id===S.id)??null:null,close=()=>{x(!1),C(G)},back=()=>{C(G)},drill=e=>{y.current=document.activeElement,C(e)},pick=(e,t)=>{t!==e.current&&s.pickOption(l,u,e,t)},pickFromList=(e,t)=>{pick(e,t),back()},chooseAgent=e=>{e!==p&&s.changeAgent(l,e),back()},manage=()=>{close(),s.onManageAgents?.()};i(()=>{if(!b||S.kind===`rows`||!_.current)return;let e=_.current.closest(`.editor-canvas`);T(listLimit(_.current.getBoundingClientRect().top,e?e.getBoundingClientRect().top:0))},[b,S]),a(()=>{if(!b)return;let e=v.current;if(e){if(S.kind===`rows`){let t=y.current&&e.contains(y.current)?y.current:e.querySelector(`[data-nav]`);y.current=null,t?.focus()}else(e.querySelector(`[data-current="true"]`)??e.querySelector(`[data-nav="item"]:not(.assistant-form__back)`))?.focus()}},[b,S]);let P=S.kind===`agent`?o(`app.assistant.form.agent`):N?.name??``;return r(t,{children:[r(`button`,{ref:_,type:`button`,class:`assistant-settings type-body`,title:j===``?k:`${k}: ${j}`,"aria-label":j===``?k:o(`app.assistant.input.settingsLabel`,{agent:k,settings:j}),"aria-haspopup":A?void 0:`dialog`,"aria-expanded":A?void 0:b,onClick:()=>A?s.onManageAgents?.():b?close():x(!0),children:[r(AgentLogo,{name:k,icon:D?.icon}),j!==``&&r(`span`,{class:`assistant-settings__label`,children:j})]}),b&&r(d,{anchor:_.current,label:o(`app.assistant.form.title`),onClose:close,onEscape:()=>S.kind!==`rows`&&(back(),!0),width:320,class:`assistant-form`,children:[r(`div`,{ref:v,class:`assistant-form__body`,"data-view":S.kind,style:{"--assistant-form-list":`${w}px`},onKeyDown:e=>{let t=e.currentTarget;/^Arrow/.test(e.key)&&navigate(t,e.key)&&e.preventDefault()},children:[S.kind===`rows`&&r(t,{children:[r(Row,{label:o(`app.assistant.form.agent`),children:r(Select,{label:o(`app.assistant.form.agent`),text:k,logo:r(AgentLogo,{name:k,icon:D?.icon}),onOpen:()=>drill({kind:`agent`})})}),M.map(e=>{let t=rowName(e);return r(Row,{label:e.name,children:e.category!==`model`&&isSegmented(e)?r(Segmented,{option:e,onPick:pick}):r(Select,{label:e.name,text:t,onOpen:()=>drill({kind:`option`,id:e.id})})},e.id)}),h&&M.length>0&&r(`p`,{class:`assistant-form__note type-help`,children:o(`app.assistant.input.optionNextTurn`)})]}),S.kind!==`rows`&&r(t,{children:[r(`header`,{class:`assistant-form__head`,children:[r(`button`,{type:`button`,class:`assistant-form__back`,"data-nav":`item`,"aria-label":o(`app.assistant.form.back`),title:o(`app.assistant.form.back`),onClick:back,children:r(f,{name:`chevronLeft`})}),r(`h3`,{class:`assistant-form__title type-section-heading`,children:P})]}),r(`div`,{class:`assistant-form__scroll`,children:[S.kind===`agent`&&r(t,{children:[r(`div`,{class:`assistant-form__choices`,role:`listbox`,"aria-label":o(`app.assistant.form.agent`),children:O.map(e=>r(`button`,{type:`button`,role:`option`,"aria-selected":e.id===p,"data-nav":`item`,"data-current":e.id===p?`true`:void 0,class:`assistant-form__item type-body`,onClick:()=>chooseAgent(e.id),children:[r(`span`,{class:`assistant-form__check`,"aria-hidden":`true`,children:e.id===p&&r(f,{name:`check`})}),r(AgentLogo,{name:e.name,icon:e.icon}),r(`span`,{class:`assistant-form__text`,children:r(`span`,{class:`assistant-form__name`,children:e.name})})]},e.id))}),g&&r(`p`,{class:`assistant-form__note type-help`,children:o(`app.assistant.form.agentNew`)})]}),N&&r(OptionList,{option:N,onPick:pickFromList})]})]})]}),c.canManageAgents&&S.kind===`rows`&&r(`div`,{class:`assistant-form__foot`,children:r(`button`,{type:`button`,"data-nav":`item`,class:`assistant-form__manage type-body`,onClick:manage,children:o(`app.assistant.form.manage`)})})]})]})}export{aiChatPlaceOf as _,useTick as a,getAssistant as c,mentionKey as d,optionsFor as f,registerAgentsOpener as g,openAgentsScreen as h,ago as i,installOpenChat as l,pathsMatch as m,OptionList as n,aiChatOn as o,baseName as p,AgentLogo as r,createAssistant as s,AgentSettings as t,setAssistant as u,aiChatSurfaces as v};
|
|
1
|
+
import"./rolldown-runtime-B8BYMn-z.js";import{c as e,h as t,i as n,r,s as i,u as a}from"./vendor-NJP34MHU.js";import{i as o,n as s}from"./translator-Dvvasoz-.js";import{t as c}from"./bridgeInstance-BPwbITOf.js";import{n as l,r as u}from"./httpHost-DX-P_FDg.js";import{c as d,n as f}from"./ui-TBzvKaKQ.js";import{i as p,r as m}from"./preferences-K6VRM9Ah.js";var h=[`off`,`bottom`,`sidebar`],g=`bottom`;function aiChatPlaceOf(e){return h.find(t=>t===e)??`bottom`}function aiChatSurfaces(e){let t=e!==`off`;return{bottomInput:e===`bottom`,card:e===`bottom`,chatTab:t,chatsList:t,toolbarButton:t,paletteAction:t,agentProcess:t}}var _=null;function registerAgentsOpener(e){return _=e,()=>{_===e&&(_=null)}}async function openAgentsScreen(){if(_)_();else try{await c().agentsHost?.().openSettings()}catch(e){console.error(`The AI agents screen did not open:`,e)}}function normalise(e){return e.replace(/\\/g,`/`).replace(/^\.\//,``).replace(/\/+$/,``)}function pathsMatch(e,t){return normalise(e)===normalise(t)}function baseName(e){return normalise(e).split(`/`).filter(Boolean).pop()??e}var v=1e3,y=15e3,b=2e3,x={status:`loading`,entries:[],queue:[],permission:null,error:null,options:[]},S={text:``,mentions:[],dismissed:[],focus:0};function isLicenceRefusal(e){return e instanceof u&&e.status===402}function newClientId(){return`c${Date.now().toString(36)}${Math.random().toString(36).slice(2,10)}`}function upsert(e,t){let n=e.findIndex(e=>e.id===t.id);if(n<0){let n=[...e,t];return n.length>b?n.slice(n.length-b):n}let r=e.slice();return r[n]=t,r}function withPlan(e,t){let n=-1;for(let t=e.length-1;t>=0;--t)if(e[t].kind===`plan`){n=t;break}if(n<0)return upsert(e,{kind:`plan`,id:`plan`,items:t});let r=e.slice();return r[n]={kind:`plan`,id:e[n].id,items:t},r}function withApproved(e,t){let n=e.status===`done`&&e.diffs.length===0?t?.get(l(e)):void 0;return n?{...e,diffs:n}:e}function applyToTranscript(e,t,n){switch(t.type){case`entry`:{let r=t.entry.kind===`tool`?{...t.entry,tool:withApproved(t.entry.tool,n)}:t.entry,i=e.entries.find(e=>e.id===r.id);return i&&JSON.stringify(i)===JSON.stringify(r)?e:{...e,entries:upsert(e.entries,r)}}case`tool`:return{...e,entries:upsert(e.entries,{kind:`tool`,id:l(t.tool),tool:withApproved(t.tool,n)})};case`plan`:return{...e,entries:withPlan(e.entries,t.items)};case`queue`:return{...e,queue:t.queue};case`permission`:return{...e,permission:t.permission};case`error`:return{...e,error:t.error};case`options`:return{...e,options:t.options};default:return e}}function sameChats(e,t){return e.length===t.length&&e.every((e,n)=>JSON.stringify(e)===JSON.stringify(t[n]))}var AssistantStore=class{state;listeners=new Set;host;options;client;pending=new Map;approved=new Map;loading=new Map;openings=new Set;expected=null;waitingChat=null;manage;control=null;started=!1;disposed=!1;failures=0;resyncing=null;constructor(e,t={}){this.host=e,this.options=t,this.client=t.client??newClientId(),this.manage=t.onManageAgents??null;let n=t.place?t.place():g;this.state={enabled:n!==`off`,place:n,loaded:!1,connected:!1,choices:[],chats:[],transcripts:{},active:{},shown:{},folded:{},revealed:{},focused:null,agentId:null,drafts:{},picks:{},canManageAgents:!!t.onManageAgents,failure:null}}snapshot=()=>this.state;subscribe=e=>(this.listeners.add(e),()=>{this.listeners.delete(e)});commit(e){this.state={...this.state,...e},this.recordOpenings();for(let e of[...this.listeners])try{e()}catch(e){console.error(`An assistant listener failed:`,e)}}recordOpenings(){for(let e of Object.keys(this.state.active))!this.openings.has(e)&&this.settled(e)&&this.openings.add(e)}settled(e){if(!this.state.loaded)return!1;let t=this.activeChat(e);if(t===null)return!0;let n=this.state.transcripts[t.id];return n!==void 0&&n.status!==`loading`}opened(e){return!this.state.enabled||this.openings.has(e)}describe(e){return e instanceof u&&e.failure?e.failure:{key:`app.agent.failed`,detail:e instanceof Error?e.message:String(e)}}wait(e){return this.options.wait?this.options.wait(e):new Promise(t=>setTimeout(t,e))}allowed(){return!this.options.place||this.options.place()!==`off`}setPlace(e){if(e===this.state.place)return;let t=e!==`off`;this.commit({place:e,enabled:t}),t?this.started&&this.begin():this.halt()}start(){return this.disposed?Promise.resolve():(this.started=!0,this.allowed()?(this.state.enabled||this.commit({enabled:!0}),this.control&&!this.control.signal.aborted?Promise.resolve():this.begin()):(this.state.enabled&&this.commit({enabled:!1}),Promise.resolve()))}begin(){let e=new AbortController;this.control=e;let t=this.resync();return this.pump(e),t}halt(){this.control?.abort(),this.control=null,this.commit({connected:!1})}dispose(){this.disposed=!0,this.started=!1,this.halt(),this.listeners.clear()}async pump(e){for(;!e.signal.aborted;)try{let t=await this.host.next(this.client,e.signal);if(e.signal.aborted)return;this.failures>0&&(this.failures=0,this.state.connected||this.commit({connected:!0})),t&&this.receive(t)}catch{if(e.signal.aborted)return;this.failures+=1,this.state.connected&&this.commit({connected:!1});let t=Math.min(this.options.maxRetryMs??y,(this.options.retryMs??v)*2**Math.min(this.failures-1,6));if(await this.wait(t),e.signal.aborted)return;await this.resync()}}resync(){if(this.resyncing)return this.resyncing;let e=(async()=>{try{let[e,t]=await Promise.all([this.host.choices().catch(()=>this.state.choices),this.host.chats()]);this.commit({choices:e,loaded:!0,connected:!0,enabled:t.enabled&&this.allowed(),failure:null}),this.takeChats(t.chats);let n=Object.keys(this.state.transcripts);await Promise.all(n.map(e=>this.fetchChat(e,!0)))}catch(e){this.state.loaded||this.commit({loaded:!0}),this.commit({failure:this.describe(e)})}finally{this.resyncing=null}})();return this.resyncing=e,e}takeChats(e){let t=new Set(e.map(e=>e.id)),n=Object.fromEntries(Object.entries(this.state.active).map(([e,n])=>[e,n!==null&&t.has(n)?n:null])),r=Object.fromEntries(Object.entries(this.state.transcripts).filter(([e])=>t.has(e))),i=this.state.chats,a=!sameChats(i,e),o=Object.entries(n).some(([e,t])=>t!==this.state.active[e])||Object.keys(r).length!==Object.keys(this.state.transcripts).length;if(a||o){for(let t of e){let e=i.find(e=>e.id===t.id);(t.state===`working`||t.state===`waiting`||t.state===`connecting`||t.state===`failed`)&&e?.state!==t.state&&(this.state={...this.state,shown:{...this.state.shown,[t.id]:!0}},t.state===`waiting`&&(this.state={...this.state,folded:{...this.state.folded,[t.id]:!1}}))}this.commit({chats:e,active:n,transcripts:r});for(let t of e){let e=i.find(e=>e.id===t.id);t.state===`waiting`&&e?.state!==`waiting`&&this.revealWaiting(t.id),t.state===`waiting`&&!this.state.transcripts[t.id]&&this.open(t.id)}}}receive(e){switch(e.type){case`sync`:this.resync();return;case`chats`:this.takeChats(e.chats);return;case`enabled`:this.commit({enabled:e.enabled&&this.allowed()});return;default:{let t=e.chatId,n=this.state.transcripts[t];if(!n){this.state.chats.some(e=>e.id===t)&&(this.pending.set(t,[...this.pending.get(t)??[],e]),this.open(t));return}if(n.status===`loading`){this.pending.set(t,[...this.pending.get(t)??[],e]);return}let r=applyToTranscript(n,e,this.approved);if(r===n)return;e.type===`options`&&this.learn(t);let i={transcripts:{...this.state.transcripts,[t]:r}};if(e.type===`permission`&&e.permission&&e.permission.id!==n.permission?.id){i.folded={...this.state.folded,[t]:!1},i.shown={...this.state.shown,[t]:!0},this.commit(i),this.revealWaiting(t);return}this.commit(i)}}}fetchChat(e,t){let n=this.loading.get(e);if(n)return n;let r=this.state.transcripts[e];!t||!r?this.commit({transcripts:{...this.state.transcripts,[e]:{...r??x,status:`loading`}}}):this.commit({transcripts:{...this.state.transcripts,[e]:{...r,status:`loading`}}});let i=(async()=>{try{let t=await this.host.chat(e),n={status:`ready`,entries:t.entries,queue:t.queue,permission:t.permission,error:t.error,options:t.options};for(let t of this.pending.get(e)??[])n=applyToTranscript(n,t,this.approved);this.pending.delete(e),this.state.chats.some(t=>t.id===e)&&this.commit({transcripts:{...this.state.transcripts,[e]:n}}),this.mergeSummary(t)}catch(t){this.pending.delete(e);let n=t instanceof u?t.status:null,r={...this.state.transcripts};n===404?delete r[e]:r[e]={...r[e]??x,status:`failed`},this.commit({transcripts:r})}finally{this.loading.delete(e)}})();return this.loading.set(e,i),i}mergeSummary(e){let t=this.state.chats.findIndex(t=>t.id===e.chat.id);if(t<0||JSON.stringify(this.state.chats[t])===JSON.stringify(e.chat))return;let n=this.state.chats.slice();n[t]=e.chat,this.commit({chats:n})}open(e){let t=this.state.transcripts[e];return t&&t.status!==`failed`&&t.status!==`missing`?this.loading.get(e)??Promise.resolve():this.fetchChat(e,!1)}focus(e){if(e!==this.state.focused&&(this.commit({focused:e}),e&&this.waitingChat)){let e=this.waitingChat;this.waitingChat=null,this.openChat(e)}}unfocus(e){this.state.focused===e&&this.commit({focused:null})}activeChat(e){let t=e?this.state.active[e]:null;return t?this.state.chats.find(e=>e.id===t)??null:null}select(e,t){this.commit({active:{...this.state.active,[e]:t}}),t&&this.open(t)}expectChat(e,t){this.expected={chatId:e,file:t}}settle(e,t){if(!this.state.loaded)return;if(this.expected&&t&&pathsMatch(this.expected.file,t)){let{chatId:t}=this.expected;this.expected=null,this.select(e,t);return}if(e in this.state.active)return;let n=(t?this.state.chats.filter(e=>e.files.some(e=>pathsMatch(e,t))):[]).reduce((e,t)=>e===null||t.updated>e.updated?t:e,null);this.select(e,n?.id??null)}get onManageAgents(){return this.manage}setManageAgents(e){return this.manage=e,this.commit({canManageAgents:e!==null}),()=>{this.manage===e&&(this.manage=null,this.commit({canManageAgents:!1}))}}changeAgent(e,t){this.activeChat(e)&&this.select(e,null),this.pickAgent(t)}pickAgent(e){this.commit({agentId:e,picks:{}})}learn(e){let t=this.state.chats.find(t=>t.id===e)?.agentId,n=this.state.choices.find(e=>e.id===t);!n||(n.options??[]).length>0||this.host.choices().then(e=>this.commit({choices:e}),()=>void 0)}async pickOption(e,t,n,r){if(!t){let t=this.agentFor(null);if(!t)return;let i=this.state.picks[e],a=i&&i.agentId===t?i.values:{};this.commit({picks:{...this.state.picks,[e]:{agentId:t,values:{...a,[n.id]:r}}}});return}let i=this.state.transcripts[t.id],chosen=e=>e.map(e=>e.id===n.id?{...e,current:r}:e);i&&this.commit({transcripts:{...this.state.transcripts,[t.id]:{...i,options:chosen(i.options)}}});try{let e=await this.host.setOption(t.id,n.id,r),i=this.state.transcripts[t.id];i&&this.commit({transcripts:{...this.state.transcripts,[t.id]:{...i,options:e}}})}catch(e){let n=this.state.transcripts[t.id];n&&i&&this.commit({transcripts:{...this.state.transcripts,[t.id]:{...n,options:i.options}}}),this.commit({failure:this.describe(e)})}}agentFor(e){return e?e.agentId:this.state.agentId?this.state.agentId:this.state.choices.find(e=>e.available)?.id??null}reveal(e){this.commit({revealed:{...this.state.revealed,[e]:(this.state.revealed[e]??0)+1}})}revealWaiting(e){if(this.state.place===`sidebar`)for(let[t,n]of Object.entries(this.state.active))n===e&&this.reveal(t)}openChat(e){let t=this.state.focused;t?(this.select(t,e),this.reveal(t)):this.waitingChat=e}showCard(e){this.commit({shown:{...this.state.shown,[e]:!0},folded:{...this.state.folded,[e]:!1}})}returnToCard(e){let t=this.activeChat(e);t&&this.state.place===`bottom`&&this.showCard(t.id)}setFolded(e,t){!!this.state.folded[e]!==t&&this.commit({folded:{...this.state.folded,[e]:t}})}dismissCard(e){this.commit({shown:{...this.state.shown,[e]:!1}})}draft(e){return this.state.drafts[e]??S}setDraft(e,t){this.commit({drafts:{...this.state.drafts,[e]:{...this.draft(e),...t}}})}setText(e,t){t!==this.draft(e).text&&this.setDraft(e,{text:t})}mention(e,t){let n=this.draft(e),r=mentionKey(t);n.mentions.some(e=>mentionKey(e)===r)||this.setDraft(e,{mentions:[...n.mentions,t],dismissed:n.dismissed.filter(e=>e!==r)})}unmention(e,t){let n=this.draft(e);this.setDraft(e,{mentions:n.mentions.filter(e=>mentionKey(e)!==t),dismissed:n.dismissed.includes(t)?n.dismissed:[...n.dismissed,t]})}releaseDismissed(e,t){let n=this.draft(e),r=n.dismissed.filter(e=>!e.startsWith(C)||t.includes(e.slice(C.length)));r.length!==n.dismissed.length&&this.setDraft(e,{dismissed:r})}focusInput(e){this.state.place===`sidebar`&&this.reveal(e),this.setDraft(e,{focus:this.draft(e).focus+1})}compose(e,t,n){let r=this.draft(e),i=r.text.trim()===``?n:r.text.includes(n)?r.text:`${r.text.trimEnd()}\n${n}`,a=mentionKey(t),o=r.mentions.some(e=>mentionKey(e)===a)?r.mentions:[...r.mentions,t];this.setDraft(e,{text:i,mentions:o,dismissed:r.dismissed.filter(e=>e!==a),focus:r.focus+1})}clearDraft(e){this.setDraft(e,{text:``,mentions:[]})}async create(e,t=null){try{let n=await this.host.create(t??this.agentFor(null));return this.takeChats([n,...this.state.chats.filter(e=>e.id!==n.id)]),this.commit({transcripts:{...this.state.transcripts,[n.id]:{status:`ready`,entries:[],queue:[],permission:null,error:null,options:[]}}}),e&&this.select(e,n.id),n}catch(e){return this.commit({failure:this.describe(e)}),null}}async rename(e,t){let n=t.trim();if(n)try{let t=await this.host.rename(e,n);this.takeChats(this.state.chats.map(e=>e.id===t.id?t:e))}catch(e){this.commit({failure:this.describe(e)})}}async remove(e){try{await this.host.remove(e)}catch(e){if(!(e instanceof u&&e.status===404)){this.commit({failure:this.describe(e)});return}}this.takeChats(this.state.chats.filter(t=>t.id!==e))}async send(e){let t=e.text.trim();if(!t)return`refused`;let n=this.activeChat(e.editor);if(!await e.authorize())return`refused`;let r=n===null;try{if(!n){if(n=await this.create(e.editor),!n)return`failed`;await this.applyPicks(e.editor,n)}let r=n.id;return this.state.place===`sidebar`?(this.reveal(e.editor),this.commit({failure:null})):this.commit({shown:{...this.state.shown,[r]:!0},folded:{...this.state.folded,[r]:!1},failure:null}),(await this.host.prompt(r,t,e.context)).queued?`queued`:`sent`}catch(t){return isLicenceRefusal(t)?(r&&n&&await this.discard(e.editor,n.id),await e.authorize(),`refused`):(this.commit({failure:this.describe(t)}),`failed`)}}async discard(e,t){this.select(e,null),await this.remove(t)}async applyPicks(e,t){let n=this.state.picks[e];if(!n)return;let{[e]:r,...i}=this.state.picks;if(this.commit({picks:i}),n.agentId===t.agentId)for(let[e,r]of Object.entries(n.values))try{let n=await this.host.setOption(t.id,e,r),i=this.state.transcripts[t.id];i&&this.commit({transcripts:{...this.state.transcripts,[t.id]:{...i,options:n}}})}catch{}}async tryAgain(e,t){let n=[...this.state.transcripts[e]?.entries??[]].reverse().find(e=>e.kind===`user`);if(!n||n.kind!==`user`||!await t())return`refused`;try{return this.commit({shown:{...this.state.shown,[e]:!0},folded:{...this.state.folded,[e]:!1},failure:null}),(await this.host.prompt(e,n.text,null)).queued?`queued`:`sent`}catch(e){return isLicenceRefusal(e)?(await t(),`refused`):(this.commit({failure:this.describe(e)}),`failed`)}}async unqueue(e,t,n){let r=this.state.transcripts[e],i=r?r.queue.findIndex(e=>e.id===t):-1,a=i>=0&&r?r.queue[i]:null;r&&this.commit({transcripts:{...this.state.transcripts,[e]:{...r,queue:r.queue.filter(e=>e.id!==t)}}});try{await this.host.unqueue(e,t)}catch(t){if(t instanceof u&&t.status===404)return;this.commit({failure:this.describe(t)});let n=this.state.transcripts[e];if(a&&n&&!n.queue.some(e=>e.id===a.id)){let t=[...n.queue];t.splice(Math.min(i,t.length),0,a),this.commit({transcripts:{...this.state.transcripts,[e]:{...n,queue:t}}})}return}if(!a)return;let o=this.draft(n),s=o.text.trim()===``?a.text:`${o.text.trimEnd()}\n${a.text}`;this.setDraft(n,{text:s,focus:o.focus+1})}async stop(e){try{await this.host.stop(e)}catch(e){this.commit({failure:this.describe(e)})}}async signIn(e,t){if(await t())try{await this.host.signIn(e)}catch(e){if(isLicenceRefusal(e)){await t();return}this.commit({failure:this.describe(e)})}}async answer(e,t,n){let r=this.state.transcripts[e];if(r?.permission?.id===t){r.permission.options.find(e=>e.id===n)?.kind===`allow`&&r.permission.tool.diffs.length>0&&this.approved.set(l(r.permission.tool),r.permission.tool.diffs),this.commit({transcripts:{...this.state.transcripts,[e]:{...r,permission:null}}});try{await this.host.answer(e,t,n)}catch(t){if(t instanceof u&&t.status===409)return;this.commit({failure:this.describe(t)}),this.fetchChat(e,!0)}}}retry(e,t){return t&&this.select(t,e),this.fetchChat(e,!0)}dismissFailure(){this.state.failure!==null&&this.commit({failure:null})}},C=`element:`;function mentionKey(e){return e.kind===`element`?`${C}${e.id}`:`file:${e.path}`}function optionsFor(e,t,n,r){if(n)return e.transcripts[n.id]?.options??[];let i=e.picks[t],a=i&&i.agentId===r?i.values:{};return(e.choices.find(e=>e.id===r)?.options??[]).map(e=>a[e.id]!==void 0&&e.values.some(t=>t.id===a[e.id])?{...e,current:a[e.id]}:e)}var w=null,T=!1,release=null;function aiChatPlace(){return aiChatPlaceOf(p().aiChat)}function aiChatOn(){return aiChatPlace()!==`off`}function createAssistant(e={}){let t=c().agentHost?.();return t?new AssistantStore(t,{place:aiChatPlace,onManageAgents:()=>void openAgentsScreen(),...e}):null}function setAssistant(e){if(release?.(),release=null,w=e,T=!0,!e)return;let t=m(()=>{e.setPlace(aiChatPlace())}),refocused=()=>{e.snapshot().enabled&&e.resync()},n=typeof window>`u`?null:window;n?.addEventListener(`focus`,refocused),release=()=>{t(),n?.removeEventListener(`focus`,refocused)}}function getAssistant(){if(!T){T=!0;try{setAssistant(createAssistant())}catch{w=null}}return w}function installOpenChat(){let e=globalThis,open=e=>getAssistant()?.openChat(e);if(e.penstockOpenChat=open,e.pendingOpenChat){let t=e.pendingOpenChat;delete e.pendingOpenChat,open(t)}return()=>{e.penstockOpenChat===open&&delete e.penstockOpenChat}}function useTick(t,r,i=Object.is){let[,o]=e(0),s=n(r);s.current=r;let c=n(r()),l=r();return c.current=l,a(()=>t(()=>{let e=s.current();i(c.current,e)||(c.current=e,o(e=>e+1))}),[t]),l}var E=6e4,D=60*E,O=24*D;function ago(e,t=Date.now()){let n=t-e,r;try{r=new Intl.RelativeTimeFormat(s(),{numeric:`auto`,style:`short`})}catch{r=new Intl.RelativeTimeFormat(`en`,{numeric:`auto`,style:`short`})}return!Number.isFinite(n)||n<E?r.format(0,`minute`):n<D?r.format(-Math.floor(n/E),`minute`):n<O?r.format(-Math.floor(n/D),`hour`):r.format(-Math.floor(n/O),`day`)}var k=/(?:fill|stroke)\s*[:=]\s*["']?\s*(#[0-9a-fA-F]{3,8}|rgba?\([^)]*\)|[a-zA-Z]+)/g,A=new Set([`none`,`transparent`,`currentcolor`,`inherit`]),j=2e5,M=new Map;function isSingleColour(e){let t=new Set;for(let n of e.matchAll(k)){let e=n[1].toLowerCase().replace(/\s+/g,``);A.has(e)||t.add(e)}return t.size<=1&&!/<(?:linearGradient|radialGradient|image)\b/i.test(e)}var N=/^data:image\/svg\+xml;base64,([A-Za-z0-9+/=]+)$/,P=/^(?:data:image\/svg\+xml;base64,[A-Za-z0-9+/=]+|https?:\/\/\S+|\/[^\s/]\S*)$/;function loadable(e){return typeof e==`string`&&P.test(e)}function decoded(e){let t=N.exec(e);if(!t)return null;try{return atob(t[1])}catch{return``}}function singleColour(e){let t=decoded(e);if(t!==null)return Promise.resolve(t!==``&&t.length<=j&&isSingleColour(t));let n=M.get(e);return n||(n=fetch(e).then(e=>e.ok?e.text():``).then(t=>t!==``&&t.length<=j&&/\.svg(?:$|\?)/i.test(e)&&isSingleColour(t)).catch(()=>!1),M.set(e,n)),n}function monogramOf(e){return(Array.from(e.trim())[0]??`?`).toLocaleUpperCase()}function AgentLogo({name:t,icon:n,class:i,size:o=16}){let s=loadable(n)?n:null,[c,l]=e(null),[u,d]=e(!1);a(()=>{if(d(!1),l(null),!s)return;let e=!0;return singleColour(s).then(t=>{e&&l(t)}),()=>{e=!1}},[s]);let f=`agent-logo${o===20?` agent-logo--large`:``}${i?` ${i}`:``}`;return!s||u?r(`span`,{class:`${f} agent-logo--monogram type-badge`,"aria-hidden":`true`,children:monogramOf(t)}):c===!0?r(`span`,{class:`${f} agent-logo--mask`,style:{maskImage:`url("${s}")`,WebkitMaskImage:`url("${s}")`},"aria-hidden":`true`}):r(`img`,{class:f,src:s,alt:``,width:o,height:o,onError:()=>d(!0)})}var F=/^default\b/i,I=28,L=4;function settingsOf(e){let first=t=>e.find(e=>e.category===t)??null;return{model:first(`model`),level:first(`thought_level`),mode:first(`mode`)}}function currentOf(e){return e.values.find(t=>t.id===e.current)??null}function isDefaultValue(e){return F.test(e.id)||F.test(e.name)}function standsFor(e){let t=e.description?.trim()??``,n=t.split(/\s+/).length;return t!==``&&t.length<=I&&n<=L&&/^[A-Z0-9]/.test(t)&&!/[.!?]$/.test(t)?t:null}function rowName(e){let t=currentOf(e);return t?e.category===`model`?modelName(e)??t.name:isDefaultValue(t)?standsFor(t)??t.name:t.name:e.current}function modelName(e){let t=currentOf(e);return t?isDefaultValue(t)?standsFor(t):t.name:e.current}function levelName(e){let t=currentOf(e);return t?isDefaultValue(t)?null:t.name:e.current}function modelAndLevel(e){return[e.model?modelName(e.model):null,e.level?levelName(e.level):null].filter(e=>e!==null).join(` `)}function modeSuffix(e){let t=e.mode;if(!t)return null;let n=currentOf(t);return n&&isDefaultValue(n)?null:modeName(t)}function settingsLabel(e){return[modelAndLevel(e),modeSuffix(e)].filter(e=>e!==null&&e!==``).join(` · `)}function modeName(e){return currentOf(e)?.name??e.current}var R=6,z=22,B=2;function groupsOf(e){let t=[];for(let n of e){let e=t[t.length-1];e&&e.name===n.group?e.values.push(n):t.push({name:n.group,values:[n]})}return t}function isSegmented(e){let t=e.values;return t.length<2||t.length>R||!t.every(e=>!e.description&&!e.group)?!1:t.reduce((e,t)=>e+Array.from(t.name).length+B,0)<=z}var V=360,H=72,U=60,W=16,G={kind:`rows`};function Segmented({option:e,onPick:t}){return r(`div`,{class:`assistant-form__segments`,role:`radiogroup`,"aria-label":e.name,children:e.values.map(n=>r(`button`,{type:`button`,role:`radio`,"aria-checked":n.id===e.current,"data-nav":`segment`,class:`assistant-form__segment type-body`,onClick:()=>t(e,n.id),children:n.name},n.id))})}function Select({text:e,logo:t,onOpen:n,label:i,nav:a=`item`}){return r(`button`,{type:`button`,class:`assistant-form__select type-body`,"data-nav":a,"aria-label":`${i}: ${e}`,"aria-haspopup":`listbox`,onClick:n,children:[t,r(`span`,{class:`assistant-form__select-text`,children:e}),r(f,{name:`chevronDown`,class:`assistant-form__select-chevron`})]})}function Row({label:e,children:t}){return r(`div`,{class:`assistant-form__row`,children:[r(`span`,{class:`assistant-form__label type-help`,children:e}),r(`div`,{class:`assistant-form__control`,children:t})]})}function OptionList({option:e,onPick:t}){return r(`div`,{class:`assistant-form__choices`,role:`listbox`,"aria-label":e.name,children:groupsOf(e.values).map((n,i)=>r(`div`,{role:`presentation`,class:`assistant-form__group`,children:[n.name&&r(`div`,{class:`assistant-form__subheading type-help`,role:`presentation`,children:n.name}),n.values.map(n=>{let i=n.id===e.current;return r(`button`,{type:`button`,role:`option`,"aria-selected":i,"data-nav":`item`,"data-current":i?`true`:void 0,class:`assistant-form__item type-body`,onClick:()=>t(e,n.id),children:[r(`span`,{class:`assistant-form__check`,"aria-hidden":`true`,children:i&&r(f,{name:`check`})}),r(`span`,{class:`assistant-form__text`,children:[r(`span`,{class:`assistant-form__name`,children:n.name}),n.description&&r(`span`,{class:`assistant-form__description type-help`,children:n.description})]})]},n.id)})]},`${n.name??``}:${i}`))})}function navigate(e,t){let n=document.activeElement,r=Array.from(e.querySelectorAll(`[data-nav]`)),i=n?r.indexOf(n):-1;if(i<0)return!1;if(n?.dataset.nav===`segment`&&(t===`ArrowRight`||t===`ArrowLeft`)){let e=Array.from(n.parentElement?.querySelectorAll(`[data-nav="segment"]`)??[]);return e[(e.indexOf(n)+(t===`ArrowRight`?1:-1)+e.length)%e.length]?.focus(),!0}return t!==`ArrowDown`&&t!==`ArrowUp`?!1:(r[(i+(t===`ArrowDown`?1:-1)+r.length)%r.length]?.focus(),!0)}function listLimit(e,t){return Math.max(H,Math.min(V,Math.round(e-t-W-U)))}function AgentSettings({store:s,state:c,editor:l,chat:u,agentId:p,options:m,busy:h,started:g}){let _=n(null),v=n(null),y=n(null),[b,x]=e(!1),[S,C]=e(G),[w,T]=e(V),E=settingsOf(m),D=c.choices.find(e=>e.id===p),O=c.choices.filter(e=>e.available),k=D?.name??(p===``?o(`app.assistant.input.noAgent`):p),A=O.length===0&&c.canManageAgents,j=settingsLabel(E),M=[E.model,E.level,E.mode].filter(e=>e!==null),N=S.kind===`option`?M.find(e=>e.id===S.id)??null:null,close=()=>{x(!1),C(G)},back=()=>{C(G)},drill=e=>{y.current=document.activeElement,C(e)},pick=(e,t)=>{t!==e.current&&s.pickOption(l,u,e,t)},pickFromList=(e,t)=>{pick(e,t),back()},chooseAgent=e=>{e!==p&&s.changeAgent(l,e),back()},manage=()=>{close(),s.onManageAgents?.()};i(()=>{if(!b||S.kind===`rows`||!_.current)return;let e=_.current.closest(`.editor-canvas`);T(listLimit(_.current.getBoundingClientRect().top,e?e.getBoundingClientRect().top:0))},[b,S]),a(()=>{if(!b)return;let e=v.current;if(e){if(S.kind===`rows`){let t=y.current&&e.contains(y.current)?y.current:e.querySelector(`[data-nav]`);y.current=null,t?.focus()}else(e.querySelector(`[data-current="true"]`)??e.querySelector(`[data-nav="item"]:not(.assistant-form__back)`))?.focus()}},[b,S]);let P=S.kind===`agent`?o(`app.assistant.form.agent`):N?.name??``;return r(t,{children:[r(`button`,{ref:_,type:`button`,class:`assistant-settings type-body`,title:j===``?k:`${k}: ${j}`,"aria-label":j===``?k:o(`app.assistant.input.settingsLabel`,{agent:k,settings:j}),"aria-haspopup":A?void 0:`dialog`,"aria-expanded":A?void 0:b,onClick:()=>A?s.onManageAgents?.():b?close():x(!0),children:[r(AgentLogo,{name:k,icon:D?.icon}),j!==``&&r(`span`,{class:`assistant-settings__label`,children:j})]}),b&&r(d,{anchor:_.current,label:o(`app.assistant.form.title`),onClose:close,onEscape:()=>S.kind!==`rows`&&(back(),!0),width:320,class:`assistant-form`,children:[r(`div`,{ref:v,class:`assistant-form__body`,"data-view":S.kind,style:{"--assistant-form-list":`${w}px`},onKeyDown:e=>{let t=e.currentTarget;/^Arrow/.test(e.key)&&navigate(t,e.key)&&e.preventDefault()},children:[S.kind===`rows`&&r(t,{children:[r(Row,{label:o(`app.assistant.form.agent`),children:r(Select,{label:o(`app.assistant.form.agent`),text:k,logo:r(AgentLogo,{name:k,icon:D?.icon}),onOpen:()=>drill({kind:`agent`})})}),M.map(e=>{let t=rowName(e);return r(Row,{label:e.name,children:e.category!==`model`&&isSegmented(e)?r(Segmented,{option:e,onPick:pick}):r(Select,{label:e.name,text:t,onOpen:()=>drill({kind:`option`,id:e.id})})},e.id)}),h&&M.length>0&&r(`p`,{class:`assistant-form__note type-help`,children:o(`app.assistant.input.optionNextTurn`)})]}),S.kind!==`rows`&&r(t,{children:[r(`header`,{class:`assistant-form__head`,children:[r(`button`,{type:`button`,class:`assistant-form__back`,"data-nav":`item`,"aria-label":o(`app.assistant.form.back`),title:o(`app.assistant.form.back`),onClick:back,children:r(f,{name:`chevronLeft`})}),r(`h3`,{class:`assistant-form__title type-section-heading`,children:P})]}),r(`div`,{class:`assistant-form__scroll`,children:[S.kind===`agent`&&r(t,{children:[r(`div`,{class:`assistant-form__choices`,role:`listbox`,"aria-label":o(`app.assistant.form.agent`),children:O.map(e=>r(`button`,{type:`button`,role:`option`,"aria-selected":e.id===p,"data-nav":`item`,"data-current":e.id===p?`true`:void 0,class:`assistant-form__item type-body`,onClick:()=>chooseAgent(e.id),children:[r(`span`,{class:`assistant-form__check`,"aria-hidden":`true`,children:e.id===p&&r(f,{name:`check`})}),r(AgentLogo,{name:e.name,icon:e.icon}),r(`span`,{class:`assistant-form__text`,children:r(`span`,{class:`assistant-form__name`,children:e.name})})]},e.id))}),g&&r(`p`,{class:`assistant-form__note type-help`,children:o(`app.assistant.form.agentNew`)})]}),N&&r(OptionList,{option:N,onPick:pickFromList})]})]})]}),c.canManageAgents&&S.kind===`rows`&&r(`div`,{class:`assistant-form__foot`,children:r(`button`,{type:`button`,"data-nav":`item`,class:`assistant-form__manage type-body`,onClick:manage,children:o(`app.assistant.form.manage`)})})]})]})}export{aiChatPlaceOf as _,useTick as a,getAssistant as c,mentionKey as d,optionsFor as f,registerAgentsOpener as g,openAgentsScreen as h,ago as i,installOpenChat as l,pathsMatch as m,OptionList as n,aiChatOn as o,baseName as p,AgentLogo as r,createAssistant as s,AgentSettings as t,setAssistant as u,aiChatSurfaces as v};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import"./rolldown-runtime-B8BYMn-z.js";import{c as e,h as t,i as n,o as r,r as i,u as a}from"./vendor-NJP34MHU.js";import{i as o}from"./translator-CS9eR0My.js";import{t as s}from"./bridgeInstance-D0bzpNGz.js";import{a as c,c as l,i as u,n as d,r as f}from"./AgentSettings-C-1YBic4.js";import{c as p,f as m,l as h,n as g,p as _,r as v,s as y}from"./ui-AICOEtWR.js";var b=`general`,x=700,S=1e4,C=4;function matches(e,t){let n=t.trim().toLowerCase();return n===``||`${e.name} ${e.id} ${e.description}`.toLowerCase().includes(n)}function sections(e){let t=(e.catalog?.agents??[]).filter(t=>matches(t,e.query)),running=e=>e.enabled&&e.installed;return{on:t.filter(running),rest:t.filter(e=>!running(e))}}var AgentsStore=class{state={status:`loading`,failure:null,catalog:null,refreshing:!1,query:``,selected:null,errors:{}};listeners=new Set;host;options;polling=!1;disposed=!1;started=null;constructor(e,t={}){this.host=e,this.options=t}snapshot=()=>this.state;subscribe=e=>(this.listeners.add(e),()=>{this.listeners.delete(e)});commit(e){this.state={...this.state,...e};for(let e of[...this.listeners])try{e()}catch(e){console.error(`An agents listener failed:`,e)}}describe(e){return this.options.describe?this.options.describe(e):e instanceof Error?e.message:String(e)}start(){return this.started??=this.load(!0),this.started}reload(){return this.load(!1)}async load(e){try{let t=await this.host.catalog();if(this.disposed)return;this.commit({status:`ready`,failure:null,catalog:t,selected:this.state.selected??defaultSelection(t)}),this.watch(t),e&&t.stale&&this.refresh()}catch(e){if(this.disposed)return;this.commit({status:this.state.catalog?`ready`:`failed`,failure:this.describe(e)})}}async refresh(){if(!this.state.refreshing){this.commit({refreshing:!0});try{let e=await this.host.refresh();if(this.disposed)return;this.commit({status:`ready`,failure:null,refreshing:!1,catalog:e,selected:this.state.selected??defaultSelection(e)}),this.watch(e)}catch(e){if(this.disposed)return;this.commit({refreshing:!1,failure:this.describe(e),status:this.state.catalog?`ready`:`failed`})}}}select(e){this.commit({selected:e})}search(e){this.commit({query:e})}setError(e,t){let n={...this.state.errors};t===null?delete n[e]:n[e]=t,this.commit({errors:n})}replace(e){let t=this.state.catalog;if(!t)return;let n=t.agents.map(t=>t.id===e.id?e:t),r={...t,agents:n,busy:t.busy||e.job?.state===`running`};this.commit({catalog:r}),this.watch(r)}async act(e,t){this.setError(e,null);try{this.replace(await t()),this.options.changed?.()}catch(t){if(this.disposed)return;this.setError(e,this.describe(t)),this.load(!1)}}install(e){return this.act(e,()=>this.host.install(e))}uninstall(e){return this.act(e,()=>this.host.uninstall(e))}turn(e,t){return this.act(e,()=>this.host.turn(e,t))}refreshModels(e){return this.act(e,()=>this.host.refreshModels(e))}choose(e,t,n){return this.act(e,()=>this.host.choose(e,t,n))}watch(e){!e.busy||this.polling||this.disposed||(this.polling=!0,(async()=>{let e=0;try{for(;!this.disposed;){let t=Math.min((this.options.pollMs??x)*2**e,S);if(await(this.options.wait?this.options.wait(t):new Promise(e=>setTimeout(e,t))),this.disposed)return;let n;try{n=await this.host.catalog()}catch(t){e=Math.min(e+1,C),this.commit({failure:this.describe(t)});continue}if(this.disposed||(e=0,this.commit({catalog:n,failure:null}),!n.busy))return}}finally{this.polling=!1}})())}dispose(){this.disposed=!0,this.listeners.clear()}};function defaultSelection(e){return e.agents.find(e=>e.enabled&&e.installed)?.id??`general`}var w=280;function ValueSelect({id:r,option:a,chosen:s,onChoose:c}){let l=n(null),[u,f]=e(!1),m=a.values.find(e=>e.id===a.current)?.name??a.current,h=[{id:``,name:o(`app.agents.modelsOwn`,{value:m}),description:null,group:null},...a.values.map(e=>({id:e.id,name:e.name,description:null,group:null}))],_=h.find(e=>e.id===s)?.name??s,v={id:a.id,name:a.name,description:null,category:a.category??a.id,current:s,values:h};return i(t,{children:[i(`button`,{ref:l,id:r,type:`button`,class:`assistant-form__select type-body agents-select`,"aria-haspopup":`listbox`,"aria-expanded":u,onClick:()=>f(e=>!e),children:[i(`span`,{class:`assistant-form__select-text`,children:_}),i(g,{name:`chevronDown`,class:`assistant-form__select-chevron`})]}),u&&i(p,{anchor:l.current,label:a.name,onClose:()=>f(!1),width:320,class:`assistant-form`,children:i(`div`,{class:`assistant-form__scroll`,style:{"--assistant-form-list":`${w}px`},children:i(d,{option:v,onPick:(e,t)=>{f(!1),c(t===``?null:t)}})})})]})}var T=`https://nodejs.org/en/download`,E=`https://docs.astral.sh/uv/getting-started/installation/`,D=[`off`,`bottom`,`sidebar`],O={off:`app.agents.aiChatOff`,bottom:`app.agents.aiChatBottom`,sidebar:`app.agents.aiChatSidebar`},k={off:`app.agents.aiChatOffHelp`,bottom:`app.agents.aiChatBottomHelp`,sidebar:`app.agents.aiChatSidebarHelp`};function openLink(e){s().openExternalLink(e).catch(()=>void 0)}function reasonText(e){let t=o(e.key,e.params);return t===e.key?e.detail===``?o(`app.agents.reason.unknown`):e.detail:e.detail===``?t:`${t} ${e.detail}`}function stateWord(e){if(e.job?.state===`running`){if(e.job.kind===`install`)return e.job.percent===null?o(`app.agents.state.installing`):o(`app.agents.state.installingPercent`,{percent:e.job.percent});if(e.job.kind===`models`)return o(`app.agents.state.reading`)}return e.job?.state===`failed`&&e.job.kind!==`models`?o(`app.agents.state.failed`):e.updateAvailable?o(`app.agents.state.update`):e.enabled&&e.installed?o(`app.agents.state.on`):e.installed?o(`app.agents.state.installed`):``}function Row({agent:e,selected:t,onSelect:n}){let r=stateWord(e);return i(`button`,{type:`button`,role:`option`,"aria-selected":t,tabIndex:t?0:-1,"data-id":e.id,class:`agents-row type-body`,onClick:n,children:[i(f,{icon:e.icon,name:e.name}),i(`span`,{class:`agents-row__name`,children:e.name}),r!==``&&i(`span`,{class:`agents-row__state type-help${e.enabled&&e.installed?` is-on`:``}${e.job?.state===`failed`?` is-failed`:``}`,children:r})]})}function GeneralRow({selected:e,onSelect:t}){return i(`button`,{type:`button`,role:`option`,"aria-selected":e,tabIndex:e?0:-1,"data-id":b,class:`agents-row type-body`,onClick:t,children:[i(`span`,{class:`agents-general-icon`,"aria-hidden":`true`}),i(`span`,{class:`agents-row__name`,children:o(`app.agents.general`)})]})}function General({preferences:e}){let t=c(e.subscribe,e.aiChat);return i(`div`,{class:`agents-general`,children:i(`section`,{class:`agents-group`,"aria-labelledby":`agents-chat`,children:[i(`h3`,{id:`agents-chat`,class:`agents-group__heading type-section-heading`,children:o(`app.agents.aiChat`)}),i(`div`,{role:`radiogroup`,"aria-labelledby":`agents-chat`,class:`agents-group__choices`,children:D.map(n=>i(`div`,{children:[i(`label`,{class:`agents-check type-body`,children:[i(`input`,{type:`radio`,name:`agents-chat`,value:n,checked:t===n,"aria-describedby":`agents-chat-${n}`,onChange:()=>e.setAiChat(n)}),i(`span`,{children:o(O[n])})]}),i(`p`,{id:`agents-chat-${n}`,class:`agents-group__help type-help`,children:o(k[n])})]},n))})]})})}function Blocked({agent:e,node:n,uv:r}){if(e.blocked===null)return null;let a=e.blocked===`node`&&!n?T:e.blocked===`uv`&&!r?E:null;return i(`p`,{class:`agents-note type-help`,children:[o(`app.agents.blocked.${e.blocked}`),a&&i(t,{children:[` `,i(`a`,{class:`agents-link`,href:a,onClick:e=>{e.preventDefault(),openLink(a)},children:o(e.blocked===`node`?`app.agents.blocked.nodeLink`:`app.agents.blocked.uvLink`)})]})]})}function Progress({percent:e}){return i(`div`,{class:`agents-progress`,role:`progressbar`,"aria-label":o(`app.agents.state.installing`),"aria-valuemin":0,"aria-valuemax":100,"aria-valuenow":e??void 0,children:i(`span`,{class:`agents-progress__bar${e===null?` is-unknown`:``}`,style:e===null?void 0:{width:`${e}%`}})})}function Models({store:e,agent:n,error:r}){let a=n.job?.kind===`models`&&n.job.state===`running`,s=n.job?.kind===`models`&&n.job.state===`failed`?n.job.error:null,c=n.options,l=c&&c.readAt!==``&&Number.isFinite(Date.parse(c.readAt))?u(Date.parse(c.readAt)):null;return i(`section`,{class:`agents-group`,"aria-labelledby":`agents-models-${n.id}`,children:[i(`div`,{class:`agents-group__head`,children:[i(`h3`,{id:`agents-models-${n.id}`,class:`agents-group__heading type-section-heading`,children:o(`app.agents.models`)}),i(`button`,{type:`button`,class:`ui-button type-button agents-refresh`,disabled:a,onClick:()=>void e.refreshModels(n.id),children:[a&&i(v,{}),i(`span`,{children:o(a?`app.agents.modelsReading`:`app.agents.modelsRefresh`)})]})]}),c===null&&!a&&!s&&i(t,{children:[i(`p`,{class:`agents-note type-body`,children:o(`app.agents.modelsEmpty`)}),i(`p`,{class:`agents-note type-help`,children:o(`app.agents.modelsEmptyHint`)})]}),c?.options.map(t=>{let r=t.category??t.id;return i(h,{label:t.name,children:a=>i(ValueSelect,{id:a.id,option:t,chosen:n.chosen[r]??``,onChoose:t=>void e.choose(n.id,r,t)})},t.id)}),l!==null&&!a&&i(`p`,{class:`agents-note type-help`,children:o(`app.agents.modelsRead`,{time:l})}),s&&i(`p`,{class:`agents-note type-error-message`,role:`alert`,children:[reasonText(s),s.key===`app.agent.authRequired`&&i(`span`,{class:`agents-note__more type-help`,children:o(`app.agents.signInHint`)})]}),r&&i(`p`,{class:`agents-note type-error-message`,role:`alert`,children:r})]})}function Detail({store:e,agent:n,state:r}){let a=r.catalog,s=n.job?.kind===`install`&&n.job.state===`running`,c=n.job?.state===`failed`&&n.job.kind!==`models`?n.job.error:null,l=r.errors[n.id]??null,u=!n.installed&&n.how!==`none`&&n.how!==`custom`&&n.blocked===null,d=n.installed&&(n.installedVia===`download`||n.installedVia===`package`);return i(`div`,{class:`agents-detail__page`,children:[i(`header`,{class:`agents-detail__head`,children:[i(f,{icon:n.icon,name:n.name,size:20}),i(`h2`,{class:`agents-detail__name type-dialog-title`,children:n.name}),n.version&&i(`span`,{class:`agents-detail__version type-help`,children:o(`app.agents.version`,{version:n.version})})]}),n.description!==``&&i(`p`,{class:`agents-detail__text type-body`,children:n.description}),i(`dl`,{class:`agents-meta type-help`,children:[n.license&&i(t,{children:[i(`dt`,{children:o(`app.agents.license`)}),i(`dd`,{children:n.license})]}),n.website&&i(t,{children:[i(`dt`,{children:o(`app.agents.website`)}),i(`dd`,{children:i(`a`,{class:`agents-link`,href:n.website,onClick:e=>{e.preventDefault(),openLink(n.website)},children:n.website})})]}),n.repository&&n.repository!==n.website&&i(t,{children:[i(`dt`,{children:o(`app.agents.repository`)}),i(`dd`,{children:i(`a`,{class:`agents-link`,href:n.repository,onClick:e=>{e.preventDefault(),openLink(n.repository)},children:n.repository})})]}),i(`dt`,{children:o(`app.agents.status`)}),i(`dd`,{children:n.installed?`${o(`app.agents.via.${n.installedVia??`path`}`)}${n.installedVersion?` · ${o(`app.agents.version`,{version:n.installedVersion})}`:``}`:n.how===`none`?o(`app.agents.cannotInstall`):o(`app.agents.how.${n.how}`)})]}),i(`div`,{class:`agents-actions`,children:[u&&!s&&i(`button`,{type:`button`,class:`ui-button ui-button--primary type-button`,onClick:()=>void e.install(n.id),children:o(c?`app.agents.tryAgain`:`app.agents.install`)}),s&&i(`button`,{type:`button`,class:`ui-button type-button`,disabled:!0,children:[i(v,{}),i(`span`,{children:o(`app.agents.state.installing`)})]}),n.installed&&!s&&n.updateAvailable&&d&&i(`button`,{type:`button`,class:`ui-button type-button`,onClick:()=>void e.install(n.id),children:o(`app.agents.update`)}),n.installed&&!s&&i(`button`,{type:`button`,class:`ui-button type-button${n.enabled?``:` ui-button--primary`}`,onClick:()=>void e.turn(n.id,!n.enabled),children:n.enabled?o(`app.agents.turnOff`):o(`app.agents.turnOn`)}),d&&!s&&i(`button`,{type:`button`,class:`ui-button ui-button--quiet ui-button--danger type-button`,onClick:()=>void e.uninstall(n.id),children:o(`app.agents.uninstall`)})]}),s&&i(Progress,{percent:n.job?.percent??null}),!n.installed&&a&&i(Blocked,{agent:n,node:a.node,uv:a.uv}),!n.installed&&n.blocked!==null&&n.install&&i(`p`,{class:`agents-note type-help`,children:[o(`app.agents.byHand`),` `,i(`code`,{class:`type-code`,children:n.install})]}),c&&i(`p`,{class:`agents-note type-error-message`,role:`alert`,children:reasonText(c)}),l&&i(`p`,{class:`agents-note type-error-message`,role:`alert`,children:l}),n.installed&&i(Models,{store:e,agent:n,error:null})]})}function ListFoot({store:e,state:t}){let n=t.catalog,r=n?.updated&&Number.isFinite(Date.parse(n.updated))?u(Date.parse(n.updated)):null,a=t.failure??(n?.refreshError?reasonText(n.refreshError):null);return i(`footer`,{class:`agents-list__foot`,children:[i(`p`,{class:`agents-list__updated type-help`,role:`status`,children:t.refreshing?o(`app.agents.updating`):r===null?o(`app.agents.neverUpdated`):n?.source===`snapshot`?o(`app.agents.updatedSnapshot`,{time:r}):o(`app.agents.updated`,{time:r})}),a&&!t.refreshing&&i(`p`,{class:`agents-list__problem type-warning-message`,children:a}),i(`button`,{type:`button`,class:`ui-button type-button agents-refresh`,disabled:t.refreshing,onClick:()=>void e.refresh(),children:[t.refreshing&&i(v,{}),i(`span`,{children:o(`app.agents.refresh`)})]})]})}function AgentsScreen({store:t,preferences:r}){let s=c(t.subscribe,t.snapshot),l=n(null),u=n(null),d=n(!1),[f,p]=e(null);a(()=>{t.start()},[t]),a(()=>{if(s.status!==`ready`||d.current)return;d.current=!0;let e=document.activeElement;(!e||e===document.body||e.classList.contains(`ui-dialog__close`)||e.classList.contains(`ui-dialog`))&&u.current?.focus()},[s.status]),a(()=>{f!==null&&(l.current?.querySelector(`[data-id="${CSS.escape(f)}"]`)?.focus(),p(null))},[f]);let{on:m,rest:h}=sections(s),g=[b,...m.map(e=>e.id),...h.map(e=>e.id)],_=s.selected??`general`,x=s.catalog?.agents.find(e=>e.id===_)??null,move=e=>{let n=e.key===`ArrowDown`?1:e.key===`ArrowUp`?-1:0,r=e.key===`Home`?0:e.key===`End`?g.length-1:null;if(n===0&&r===null)return;e.preventDefault();let i=g.indexOf(_),a=r??Math.min(g.length-1,Math.max(0,(i<0?0:i)+n));g[a]!==void 0&&(t.select(g[a]),p(g[a]))};return s.status===`loading`?i(`div`,{class:`agents-screen is-loading`,role:`status`,children:[i(v,{}),i(`span`,{class:`type-help`,children:o(`app.agents.loading`)})]}):s.status===`failed`||!s.catalog?i(`div`,{class:`agents-screen is-empty`,children:i(y,{title:o(`app.agents.failed.title`),hint:s.failure??o(`app.agents.failed.hint`),actions:[{label:o(`app.assistant.tryAgain`),run:()=>void t.reload()}]})}):i(`div`,{class:`agents-screen`,children:[i(`nav`,{class:`agents-list`,"aria-label":o(`app.agents.list`),children:[i(`input`,{ref:u,type:`search`,class:`agents-list__search type-input`,value:s.query,placeholder:o(`app.agents.search`),"aria-label":o(`app.agents.search`),onInput:e=>t.search(e.currentTarget.value)}),i(`div`,{ref:l,class:`agents-list__scroll`,role:`listbox`,"aria-label":o(`app.agents.list`),onKeyDown:move,children:[i(GeneralRow,{selected:_===b,onSelect:()=>t.select(b)}),i(`h3`,{class:`agents-list__heading type-section-heading`,children:o(`app.agents.on`)}),m.length===0&&(s.query===``||h.length===0)&&i(`p`,{class:`agents-list__none type-help`,children:s.query===``?o(`app.agents.noneOn`):o(`app.agents.noMatch`)}),m.map(e=>i(Row,{agent:e,selected:_===e.id,onSelect:()=>t.select(e.id)},e.id)),i(`h3`,{class:`agents-list__heading type-section-heading`,children:o(`app.agents.all`)}),h.length===0&&m.length>0&&i(`p`,{class:`agents-list__none type-help`,children:o(`app.agents.allShown`)}),h.length===0&&m.length===0&&s.query!==``&&i(`p`,{class:`agents-list__none type-help`,children:o(`app.agents.noMatch`)}),h.map(e=>i(Row,{agent:e,selected:_===e.id,onSelect:()=>t.select(e.id)},e.id))]}),i(ListFoot,{store:t,state:s})]}),i(`section`,{class:`agents-detail`,"aria-label":x?.name??o(`app.agents.general`),children:_===`general`||x===null?i(General,{preferences:r}):i(Detail,{store:t,agent:x,state:s})})]})}function AgentsDialog({host:e,preferences:t,onClose:n}){let s=r(()=>new AgentsStore(e,{changed:()=>void l()?.resync()}),[e]);return a(()=>()=>s.dispose(),[s]),i(m,{title:o(`app.agents.title`),size:`large`,body:`split`,class:`agents-dialog`,onClose:n,footer:i(_,{actions:[{id:`done`,label:o(`app.common.done`),role:`primary`,onClick:n}]}),children:i(AgentsScreen,{store:s,preferences:t})})}export{AgentsDialog};
|
|
1
|
+
import"./rolldown-runtime-B8BYMn-z.js";import{c as e,h as t,i as n,o as r,r as i,u as a}from"./vendor-NJP34MHU.js";import{i as o}from"./translator-Dvvasoz-.js";import{t as s}from"./bridgeInstance-BPwbITOf.js";import{a as c,c as l,i as u,n as d,r as f}from"./AgentSettings-BvzT4Itb.js";import{c as p,f as m,l as h,n as g,p as _,r as v,s as y}from"./ui-TBzvKaKQ.js";var b=`general`,x=700,S=1e4,C=4;function matches(e,t){let n=t.trim().toLowerCase();return n===``||`${e.name} ${e.id} ${e.description}`.toLowerCase().includes(n)}function sections(e){let t=(e.catalog?.agents??[]).filter(t=>matches(t,e.query)),running=e=>e.enabled&&e.installed;return{on:t.filter(running),rest:t.filter(e=>!running(e))}}var AgentsStore=class{state={status:`loading`,failure:null,catalog:null,refreshing:!1,query:``,selected:null,errors:{}};listeners=new Set;host;options;polling=!1;disposed=!1;started=null;constructor(e,t={}){this.host=e,this.options=t}snapshot=()=>this.state;subscribe=e=>(this.listeners.add(e),()=>{this.listeners.delete(e)});commit(e){this.state={...this.state,...e};for(let e of[...this.listeners])try{e()}catch(e){console.error(`An agents listener failed:`,e)}}describe(e){return this.options.describe?this.options.describe(e):e instanceof Error?e.message:String(e)}start(){return this.started??=this.load(!0),this.started}reload(){return this.load(!1)}async load(e){try{let t=await this.host.catalog();if(this.disposed)return;this.commit({status:`ready`,failure:null,catalog:t,selected:this.state.selected??defaultSelection(t)}),this.watch(t),e&&t.stale&&this.refresh()}catch(e){if(this.disposed)return;this.commit({status:this.state.catalog?`ready`:`failed`,failure:this.describe(e)})}}async refresh(){if(!this.state.refreshing){this.commit({refreshing:!0});try{let e=await this.host.refresh();if(this.disposed)return;this.commit({status:`ready`,failure:null,refreshing:!1,catalog:e,selected:this.state.selected??defaultSelection(e)}),this.watch(e)}catch(e){if(this.disposed)return;this.commit({refreshing:!1,failure:this.describe(e),status:this.state.catalog?`ready`:`failed`})}}}select(e){this.commit({selected:e})}search(e){this.commit({query:e})}setError(e,t){let n={...this.state.errors};t===null?delete n[e]:n[e]=t,this.commit({errors:n})}replace(e){let t=this.state.catalog;if(!t)return;let n=t.agents.map(t=>t.id===e.id?e:t),r={...t,agents:n,busy:t.busy||e.job?.state===`running`};this.commit({catalog:r}),this.watch(r)}async act(e,t){this.setError(e,null);try{this.replace(await t()),this.options.changed?.()}catch(t){if(this.disposed)return;this.setError(e,this.describe(t)),this.load(!1)}}install(e){return this.act(e,()=>this.host.install(e))}uninstall(e){return this.act(e,()=>this.host.uninstall(e))}turn(e,t){return this.act(e,()=>this.host.turn(e,t))}refreshModels(e){return this.act(e,()=>this.host.refreshModels(e))}choose(e,t,n){return this.act(e,()=>this.host.choose(e,t,n))}watch(e){!e.busy||this.polling||this.disposed||(this.polling=!0,(async()=>{let e=0;try{for(;!this.disposed;){let t=Math.min((this.options.pollMs??x)*2**e,S);if(await(this.options.wait?this.options.wait(t):new Promise(e=>setTimeout(e,t))),this.disposed)return;let n;try{n=await this.host.catalog()}catch(t){e=Math.min(e+1,C),this.commit({failure:this.describe(t)});continue}if(this.disposed||(e=0,this.commit({catalog:n,failure:null}),!n.busy))return}}finally{this.polling=!1}})())}dispose(){this.disposed=!0,this.listeners.clear()}};function defaultSelection(e){return e.agents.find(e=>e.enabled&&e.installed)?.id??`general`}var w=280;function ValueSelect({id:r,option:a,chosen:s,onChoose:c}){let l=n(null),[u,f]=e(!1),m=a.values.find(e=>e.id===a.current)?.name??a.current,h=[{id:``,name:o(`app.agents.modelsOwn`,{value:m}),description:null,group:null},...a.values.map(e=>({id:e.id,name:e.name,description:null,group:null}))],_=h.find(e=>e.id===s)?.name??s,v={id:a.id,name:a.name,description:null,category:a.category??a.id,current:s,values:h};return i(t,{children:[i(`button`,{ref:l,id:r,type:`button`,class:`assistant-form__select type-body agents-select`,"aria-haspopup":`listbox`,"aria-expanded":u,onClick:()=>f(e=>!e),children:[i(`span`,{class:`assistant-form__select-text`,children:_}),i(g,{name:`chevronDown`,class:`assistant-form__select-chevron`})]}),u&&i(p,{anchor:l.current,label:a.name,onClose:()=>f(!1),width:320,class:`assistant-form`,children:i(`div`,{class:`assistant-form__scroll`,style:{"--assistant-form-list":`${w}px`},children:i(d,{option:v,onPick:(e,t)=>{f(!1),c(t===``?null:t)}})})})]})}var T=`https://nodejs.org/en/download`,E=`https://docs.astral.sh/uv/getting-started/installation/`,D=[`off`,`bottom`,`sidebar`],O={off:`app.agents.aiChatOff`,bottom:`app.agents.aiChatBottom`,sidebar:`app.agents.aiChatSidebar`},k={off:`app.agents.aiChatOffHelp`,bottom:`app.agents.aiChatBottomHelp`,sidebar:`app.agents.aiChatSidebarHelp`};function openLink(e){s().openExternalLink(e).catch(()=>void 0)}function reasonText(e){let t=o(e.key,e.params);return t===e.key?e.detail===``?o(`app.agents.reason.unknown`):e.detail:e.detail===``?t:`${t} ${e.detail}`}function stateWord(e){if(e.job?.state===`running`){if(e.job.kind===`install`)return e.job.percent===null?o(`app.agents.state.installing`):o(`app.agents.state.installingPercent`,{percent:e.job.percent});if(e.job.kind===`models`)return o(`app.agents.state.reading`)}return e.job?.state===`failed`&&e.job.kind!==`models`?o(`app.agents.state.failed`):e.updateAvailable?o(`app.agents.state.update`):e.enabled&&e.installed?o(`app.agents.state.on`):e.installed?o(`app.agents.state.installed`):``}function Row({agent:e,selected:t,onSelect:n}){let r=stateWord(e);return i(`button`,{type:`button`,role:`option`,"aria-selected":t,tabIndex:t?0:-1,"data-id":e.id,class:`agents-row type-body`,onClick:n,children:[i(f,{icon:e.icon,name:e.name}),i(`span`,{class:`agents-row__name`,children:e.name}),r!==``&&i(`span`,{class:`agents-row__state type-help${e.enabled&&e.installed?` is-on`:``}${e.job?.state===`failed`?` is-failed`:``}`,children:r})]})}function GeneralRow({selected:e,onSelect:t}){return i(`button`,{type:`button`,role:`option`,"aria-selected":e,tabIndex:e?0:-1,"data-id":b,class:`agents-row type-body`,onClick:t,children:[i(`span`,{class:`agents-general-icon`,"aria-hidden":`true`}),i(`span`,{class:`agents-row__name`,children:o(`app.agents.general`)})]})}function General({preferences:e}){let t=c(e.subscribe,e.aiChat);return i(`div`,{class:`agents-general`,children:i(`section`,{class:`agents-group`,"aria-labelledby":`agents-chat`,children:[i(`h3`,{id:`agents-chat`,class:`agents-group__heading type-section-heading`,children:o(`app.agents.aiChat`)}),i(`div`,{role:`radiogroup`,"aria-labelledby":`agents-chat`,class:`agents-group__choices`,children:D.map(n=>i(`div`,{children:[i(`label`,{class:`agents-check type-body`,children:[i(`input`,{type:`radio`,name:`agents-chat`,value:n,checked:t===n,"aria-describedby":`agents-chat-${n}`,onChange:()=>e.setAiChat(n)}),i(`span`,{children:o(O[n])})]}),i(`p`,{id:`agents-chat-${n}`,class:`agents-group__help type-help`,children:o(k[n])})]},n))})]})})}function Blocked({agent:e,node:n,uv:r}){if(e.blocked===null)return null;let a=e.blocked===`node`&&!n?T:e.blocked===`uv`&&!r?E:null;return i(`p`,{class:`agents-note type-help`,children:[o(`app.agents.blocked.${e.blocked}`),a&&i(t,{children:[` `,i(`a`,{class:`agents-link`,href:a,onClick:e=>{e.preventDefault(),openLink(a)},children:o(e.blocked===`node`?`app.agents.blocked.nodeLink`:`app.agents.blocked.uvLink`)})]})]})}function Progress({percent:e}){return i(`div`,{class:`agents-progress`,role:`progressbar`,"aria-label":o(`app.agents.state.installing`),"aria-valuemin":0,"aria-valuemax":100,"aria-valuenow":e??void 0,children:i(`span`,{class:`agents-progress__bar${e===null?` is-unknown`:``}`,style:e===null?void 0:{width:`${e}%`}})})}function Models({store:e,agent:n,error:r}){let a=n.job?.kind===`models`&&n.job.state===`running`,s=n.job?.kind===`models`&&n.job.state===`failed`?n.job.error:null,c=n.options,l=c&&c.readAt!==``&&Number.isFinite(Date.parse(c.readAt))?u(Date.parse(c.readAt)):null;return i(`section`,{class:`agents-group`,"aria-labelledby":`agents-models-${n.id}`,children:[i(`div`,{class:`agents-group__head`,children:[i(`h3`,{id:`agents-models-${n.id}`,class:`agents-group__heading type-section-heading`,children:o(`app.agents.models`)}),i(`button`,{type:`button`,class:`ui-button type-button agents-refresh`,disabled:a,onClick:()=>void e.refreshModels(n.id),children:[a&&i(v,{}),i(`span`,{children:o(a?`app.agents.modelsReading`:`app.agents.modelsRefresh`)})]})]}),c===null&&!a&&!s&&i(t,{children:[i(`p`,{class:`agents-note type-body`,children:o(`app.agents.modelsEmpty`)}),i(`p`,{class:`agents-note type-help`,children:o(`app.agents.modelsEmptyHint`)})]}),c?.options.map(t=>{let r=t.category??t.id;return i(h,{label:t.name,children:a=>i(ValueSelect,{id:a.id,option:t,chosen:n.chosen[r]??``,onChoose:t=>void e.choose(n.id,r,t)})},t.id)}),l!==null&&!a&&i(`p`,{class:`agents-note type-help`,children:o(`app.agents.modelsRead`,{time:l})}),s&&i(`p`,{class:`agents-note type-error-message`,role:`alert`,children:[reasonText(s),s.key===`app.agent.authRequired`&&i(`span`,{class:`agents-note__more type-help`,children:o(`app.agents.signInHint`)})]}),r&&i(`p`,{class:`agents-note type-error-message`,role:`alert`,children:r})]})}function Detail({store:e,agent:n,state:r}){let a=r.catalog,s=n.job?.kind===`install`&&n.job.state===`running`,c=n.job?.state===`failed`&&n.job.kind!==`models`?n.job.error:null,l=r.errors[n.id]??null,u=!n.installed&&n.how!==`none`&&n.how!==`custom`&&n.blocked===null,d=n.installed&&(n.installedVia===`download`||n.installedVia===`package`);return i(`div`,{class:`agents-detail__page`,children:[i(`header`,{class:`agents-detail__head`,children:[i(f,{icon:n.icon,name:n.name,size:20}),i(`h2`,{class:`agents-detail__name type-dialog-title`,children:n.name}),n.version&&i(`span`,{class:`agents-detail__version type-help`,children:o(`app.agents.version`,{version:n.version})})]}),n.description!==``&&i(`p`,{class:`agents-detail__text type-body`,children:n.description}),i(`dl`,{class:`agents-meta type-help`,children:[n.license&&i(t,{children:[i(`dt`,{children:o(`app.agents.license`)}),i(`dd`,{children:n.license})]}),n.website&&i(t,{children:[i(`dt`,{children:o(`app.agents.website`)}),i(`dd`,{children:i(`a`,{class:`agents-link`,href:n.website,onClick:e=>{e.preventDefault(),openLink(n.website)},children:n.website})})]}),n.repository&&n.repository!==n.website&&i(t,{children:[i(`dt`,{children:o(`app.agents.repository`)}),i(`dd`,{children:i(`a`,{class:`agents-link`,href:n.repository,onClick:e=>{e.preventDefault(),openLink(n.repository)},children:n.repository})})]}),i(`dt`,{children:o(`app.agents.status`)}),i(`dd`,{children:n.installed?`${o(`app.agents.via.${n.installedVia??`path`}`)}${n.installedVersion?` · ${o(`app.agents.version`,{version:n.installedVersion})}`:``}`:n.how===`none`?o(`app.agents.cannotInstall`):o(`app.agents.how.${n.how}`)})]}),i(`div`,{class:`agents-actions`,children:[u&&!s&&i(`button`,{type:`button`,class:`ui-button ui-button--primary type-button`,onClick:()=>void e.install(n.id),children:o(c?`app.agents.tryAgain`:`app.agents.install`)}),s&&i(`button`,{type:`button`,class:`ui-button type-button`,disabled:!0,children:[i(v,{}),i(`span`,{children:o(`app.agents.state.installing`)})]}),n.installed&&!s&&n.updateAvailable&&d&&i(`button`,{type:`button`,class:`ui-button type-button`,onClick:()=>void e.install(n.id),children:o(`app.agents.update`)}),n.installed&&!s&&i(`button`,{type:`button`,class:`ui-button type-button${n.enabled?``:` ui-button--primary`}`,onClick:()=>void e.turn(n.id,!n.enabled),children:n.enabled?o(`app.agents.turnOff`):o(`app.agents.turnOn`)}),d&&!s&&i(`button`,{type:`button`,class:`ui-button ui-button--quiet ui-button--danger type-button`,onClick:()=>void e.uninstall(n.id),children:o(`app.agents.uninstall`)})]}),s&&i(Progress,{percent:n.job?.percent??null}),!n.installed&&a&&i(Blocked,{agent:n,node:a.node,uv:a.uv}),!n.installed&&n.blocked!==null&&n.install&&i(`p`,{class:`agents-note type-help`,children:[o(`app.agents.byHand`),` `,i(`code`,{class:`type-code`,children:n.install})]}),c&&i(`p`,{class:`agents-note type-error-message`,role:`alert`,children:reasonText(c)}),l&&i(`p`,{class:`agents-note type-error-message`,role:`alert`,children:l}),n.installed&&i(Models,{store:e,agent:n,error:null})]})}function ListFoot({store:e,state:t}){let n=t.catalog,r=n?.updated&&Number.isFinite(Date.parse(n.updated))?u(Date.parse(n.updated)):null,a=t.failure??(n?.refreshError?reasonText(n.refreshError):null);return i(`footer`,{class:`agents-list__foot`,children:[i(`p`,{class:`agents-list__updated type-help`,role:`status`,children:t.refreshing?o(`app.agents.updating`):r===null?o(`app.agents.neverUpdated`):n?.source===`snapshot`?o(`app.agents.updatedSnapshot`,{time:r}):o(`app.agents.updated`,{time:r})}),a&&!t.refreshing&&i(`p`,{class:`agents-list__problem type-warning-message`,children:a}),i(`button`,{type:`button`,class:`ui-button type-button agents-refresh`,disabled:t.refreshing,onClick:()=>void e.refresh(),children:[t.refreshing&&i(v,{}),i(`span`,{children:o(`app.agents.refresh`)})]})]})}function AgentsScreen({store:t,preferences:r}){let s=c(t.subscribe,t.snapshot),l=n(null),u=n(null),d=n(!1),[f,p]=e(null);a(()=>{t.start()},[t]),a(()=>{if(s.status!==`ready`||d.current)return;d.current=!0;let e=document.activeElement;(!e||e===document.body||e.classList.contains(`ui-dialog__close`)||e.classList.contains(`ui-dialog`))&&u.current?.focus()},[s.status]),a(()=>{f!==null&&(l.current?.querySelector(`[data-id="${CSS.escape(f)}"]`)?.focus(),p(null))},[f]);let{on:m,rest:h}=sections(s),g=[b,...m.map(e=>e.id),...h.map(e=>e.id)],_=s.selected??`general`,x=s.catalog?.agents.find(e=>e.id===_)??null,move=e=>{let n=e.key===`ArrowDown`?1:e.key===`ArrowUp`?-1:0,r=e.key===`Home`?0:e.key===`End`?g.length-1:null;if(n===0&&r===null)return;e.preventDefault();let i=g.indexOf(_),a=r??Math.min(g.length-1,Math.max(0,(i<0?0:i)+n));g[a]!==void 0&&(t.select(g[a]),p(g[a]))};return s.status===`loading`?i(`div`,{class:`agents-screen is-loading`,role:`status`,children:[i(v,{}),i(`span`,{class:`type-help`,children:o(`app.agents.loading`)})]}):s.status===`failed`||!s.catalog?i(`div`,{class:`agents-screen is-empty`,children:i(y,{title:o(`app.agents.failed.title`),hint:s.failure??o(`app.agents.failed.hint`),actions:[{label:o(`app.assistant.tryAgain`),run:()=>void t.reload()}]})}):i(`div`,{class:`agents-screen`,children:[i(`nav`,{class:`agents-list`,"aria-label":o(`app.agents.list`),children:[i(`input`,{ref:u,type:`search`,class:`agents-list__search type-input`,value:s.query,placeholder:o(`app.agents.search`),"aria-label":o(`app.agents.search`),onInput:e=>t.search(e.currentTarget.value)}),i(`div`,{ref:l,class:`agents-list__scroll`,role:`listbox`,"aria-label":o(`app.agents.list`),onKeyDown:move,children:[i(GeneralRow,{selected:_===b,onSelect:()=>t.select(b)}),i(`h3`,{class:`agents-list__heading type-section-heading`,children:o(`app.agents.on`)}),m.length===0&&(s.query===``||h.length===0)&&i(`p`,{class:`agents-list__none type-help`,children:s.query===``?o(`app.agents.noneOn`):o(`app.agents.noMatch`)}),m.map(e=>i(Row,{agent:e,selected:_===e.id,onSelect:()=>t.select(e.id)},e.id)),i(`h3`,{class:`agents-list__heading type-section-heading`,children:o(`app.agents.all`)}),h.length===0&&m.length>0&&i(`p`,{class:`agents-list__none type-help`,children:o(`app.agents.allShown`)}),h.length===0&&m.length===0&&s.query!==``&&i(`p`,{class:`agents-list__none type-help`,children:o(`app.agents.noMatch`)}),h.map(e=>i(Row,{agent:e,selected:_===e.id,onSelect:()=>t.select(e.id)},e.id))]}),i(ListFoot,{store:t,state:s})]}),i(`section`,{class:`agents-detail`,"aria-label":x?.name??o(`app.agents.general`),children:_===`general`||x===null?i(General,{preferences:r}):i(Detail,{store:t,agent:x,state:s})})]})}function AgentsDialog({host:e,preferences:t,onClose:n}){let s=r(()=>new AgentsStore(e,{changed:()=>void l()?.resync()}),[e]);return a(()=>()=>s.dispose(),[s]),i(m,{title:o(`app.agents.title`),size:`large`,body:`split`,class:`agents-dialog`,onClose:n,footer:i(_,{actions:[{id:`done`,label:o(`app.common.done`),role:`primary`,onClick:n}]}),children:i(AgentsScreen,{store:s,preferences:t})})}export{AgentsDialog};
|