1import{j as n,r as c}from"./vendor-CIikcnY8.js";import{u as ae,c as ie,j as re,l as oe,b as _}from"./index-LRgHn5RP.js";import{HelpSheet as le}from"./HelpSheet-xuJdDX2d.js";function K(h,$){const o=[],k=new RegExp("(\\*\\*|__)(.+?)\\1|(\\*|_)(?!\\s)(.+?)(?<!\\s)\\3|`([^`]+)`|(https?:\\/\\/[^\\s)]+)","g");let p=0;for(let s=k.exec(h);s;s=k.exec(h)){s.index>p&&o.push(h.slice(p,s.index));const d=`${$}-${s.index}`;s[2]!==void 0?o.push(n.jsx("strong",{children:s[2]},d)):s[4]!==void 0?o.push(n.jsx("em",{children:s[4]},d)):s[5]!==void 0?o.push(n.jsx("code",{children:s[5]},d)):s[6]!==void 0&&o.push(n.jsx("a",{href:s[6],target:"_blank",rel:"noreferrer",children:s[6]},d)),p=s.index+s[0].length}return p<h.length&&o.push(h.slice(p)),o}const Q=/^\s*[-*â¢]\s+/,X=/^\s*\d+[.)]\s+/;function ce({text:h}){const $=h.split(` 2`),o=[];let k=0,p=[],s=null;const d=()=>{p.length&&o.push(n.jsx("p",{children:K(p.join(" "),`p${o.length}`)},`p${o.length}`)),p=[]},y=()=>{if(!s)return;const w=s.items.map((m,f)=>n.jsx("li",{children:K(m,`l${o.length}-${f}`)},f));o.push(s.ordered?n.jsx("ol",{children:w},`l${o.length}`):n.jsx("ul",{children:w},`l${o.length}`)),s=null};for(const w of $){const m=w.trimEnd();if(!m.trim()){d(),y();continue}const f=m.match(/^(#{1,6})\s+(.*)$/);if(f){d(),y();const x=Math.min(f[1].length,4),T=["h2","h2","h3","h4"][x-1]??"h4";o.push(n.jsx(T,{id:`h-${k++}`,className:`rt-h${x}`,children:K(f[2],`h${o.length}`)},`h${o.length}`));continue}const u=Q.test(m),g=!u&&X.test(m);if(u||g){d();const x=m.replace(u?Q:X,"");s&&s.ordered===g?s.items.push(x):(y(),s={ordered:g,items:[x]});continue}y(),p.push(m)}return d(),y(),n.jsx("div",{className:"rt",children:o})}const de="orgami:assistant-width",pe={min:320,max:760,step:16,normal:400},ue={create_person:["team.create"],update_person:["team.update"],delete_person:["team.delete"],create_department:["departments.create"],update_department:["departments.update"],delete_department:["departments.delete"],create_company:["map.create"],update_company:["map.update"],delete_company:["map.delete"],create_project:["projects.create","tools.create"],update_project:["projects.update","tools.update"],delete_project:["projects.delete","tools.delete"],create_project_area:["projects.update","tools.update"],update_project_area:["projects.update","tools.update"],delete_project_area:["projects.update","tools.update"],add_project_member:["projects.update","tools.update"],update_project_member:["projects.update","tools.update"],remove_project_member:["projects.update","tools.update"],add_work:["tasks.create","regular_tasks.create"],update_work:["tasks.update","regular_tasks.update"],delete_work:["tasks.delete","regular_tasks.delete"],add_assignment:["assignments.create"],update_assignment:["assignments.update"],delete_assignment:["assignments.delete"],create_goal:["planning.create"],update_goal:["planning.update"],delete_goal:["planning.delete"]},Z="Add a person to Affiliate Prime",he=["How many people are in each company?","Who has no manager?",Z,"Which project areas have no owner?"],me={create:"Create",update:"Change",delete:"Delete"},G=()=>crypto.randomUUID?crypto.randomUUID():`m${Date.now()}-${Math.random().toString(36).slice(2)}`;function je({where:h,context:$,readOnly:o,onApplied:k,onClose:p}){const s=ae(),{can:d}=ie(),y=d("assistant.read"),w=d("assistant.delete"),m=e=>!o&&(ue[e]?.some(i=>d(i))??!0),f=e=>m(e.tool),[u,g]=c.useState([]),[x,T]=c.useState(!1),[v,O]=c.useState(""),[P,C]=c.useState(!1),[N,H]=c.useState(null),[A,W]=c.useState(null),[M,U]=c.useState(null),[S,b]=c.useState(null),E=c.useRef(new Map),L=c.useRef(null),D=c.useRef(null),R=re({storageKey:de,range:pe,side:"left",inside:!0});c.useEffect(()=>{const e=()=>{const i=D.current;i&&(i.style.height="auto",i.style.height=`${Math.min(i.scrollHeight+2,Math.max(120,Math.round(window.innerHeight*.33)))}px`)};return e(),window.addEventListener("resize",e),()=>window.removeEventListener("resize",e)},[v]),c.useEffect(()=>{D.current?.focus()},[]),c.useEffect(()=>{L.current?.scrollTo({top:L.current.scrollHeight,behavior:"smooth"})},[u]);async function ee(){b([]);try{b(await _.assistantThreads())}catch{b([])}}async function te(e){b(null),C(!0);try{const i=await _.assistantThread(e);
2E.current=new Map,g(i.map(t=>{const r=G();return E.current.set(r,t.id),t.role==="user"?{id:r,role:"user",text:t.text}:{id:r,role:"assistant",text:t.text,actions:t.actions,where:t.where}})),U(e),H(null)}finally{C(!1)}}function z(){g([]),U(null),b(null),H(null),E.current=new Map,D.current?.focus()}async function q(e){const i=e.trim();if(!i||P)return;O(""),C(!0);const t=G(),r=[...u.filter(a=>a.role==="user"||!a.pending&&!a.error).map(a=>{if(a.role==="user")return{role:"user",content:a.text};const j=(a.actions??[]).map(l=>l.state==="applied"?`[пÑименено] ${l.summary||l.title}`:l.state==="rejected"?`[оÑклонено] ${l.title}`:`[пÑедложено] ${l.title}${l.detail?` · ${l.detail}`:""}`);return{role:"assistant",content:[a.text,...j].filter(Boolean).join(` 3`)}}).filter(a=>a.content.trim()),{role:"user",content:i}];g(a=>[...a,{id:G(),role:"user",text:i},{id:t,role:"assistant",text:"",pending:!0}]);try{const a=await _.assistantChat(r,h,M,$);H(a.usage),U(a.threadId),E.current.set(t,a.messageId),g(j=>j.map(l=>l.id===t?{id:l.id,role:"assistant",text:a.text,actions:a.actions.map(I=>({...I,state:"proposed"})),where:h}:l))}catch(a){const j=a instanceof Error?a.message:"Something went wrong";g(l=>l.map(I=>I.id===t?{id:I.id,role:"assistant",text:"",error:j}:I))}finally{C(!1)}}const B=(e,i)=>g(t=>t.map(r=>r.role==="assistant"&&r.actions?{...r,actions:r.actions.map(a=>a.id===e?{...a,...i}:a)}:r)),F=e=>{const i=u.find(t=>t.role==="assistant"&&t.actions?.some(r=>r.id===e));return i?E.current.get(i.id):void 0};function se(e,i={}){const t=u.find(a=>a.role==="assistant"&&a.actions?.some(j=>j.id===e)),r={};return t&&t.role==="assistant"&&t.actions?.forEach((a,j)=>a.createdId&&(r[`#${j+1}`]=a.createdId)),{...r,...i}}async function Y(e,i={}){W(e.id);try{const t=await _.assistantApply(e.tool,e.args,e.title,{messageId:F(e.id),actionId:e.id,refs:se(e.id,i)});return B(e.id,{state:"applied",error:void 0,summary:t.summary,createdId:t.createdId}),k(e),{ok:!0,id:t.createdId}}catch(t){return B(e.id,{state:"failed",error:t instanceof Error?t.message:"Something went wrong"}),{ok:!1}}finally{W(null)}}function V(e){B(e.id,{state:"rejected"});const i=F(e.id);i&&_.assistantSkip(i,e.id).catch(()=>{})}const J=(e,i)=>{if(o||e.role!=="assistant"||!e.actions)return null;const t=e.actions.filter(a=>a.state==="proposed"||a.state==="failed");if(t.length<2||i==="bottom"&&t.length<3)return null;const r=t.filter(a=>f(a)&&!a.warning);return n.jsxs("div",{className:`action-bulk ${i}`,children:[r.length>0&&n.jsx("button",{type:"button",className:"btn small primary",disabled:!!A,onClick:()=>ne(e.actions),children:s("Apply all ({n})",{n:r.length})}),n.jsx("button",{type:"button",className:"btn small",disabled:!!A,onClick:()=>e.actions.filter(a=>a.state==="proposed").forEach(V),children:s("Skip all")})]})};async function ne(e){const i={};for(const[t,r]of e.entries()){if(!((r.state==="proposed"||r.state==="failed")&&!r.warning&&f(r)))continue;const a=await Y(r,i);if(!a.ok)break;a.id&&(i[`#${t+1}`]=a.id)}}return n.jsxs("aside",{ref:R.ref,className:"assistant",style:{width:R.width??void 0},"aria-label":s("Assistant"),children:[R.edge,n.jsxs("header",{className:"assistant-head",children:[n.jsxs("div",{className:"assistant-head-main",children:[n.jsxs("h3",{children:[s("Assistant"),n.jsx("button",{type:"button",className:`help-btn ${x?"on":""}`,"data-help":"assistant-help","aria-pressed":x,title:s("How this screen works"),onClick:()=>T(e=>!e),children:"?"}),y&&n.jsx("button",{type:"button",className:"link",onClick:()=>S?b(null):ee(),children:s(S?"back":"history")}),u.length>0&&n.jsx("button",{type:"button",className:"link",onClick:z,children:s("new chat")})]}),n.jsxs("p",{className:"hint",children:[s("Sees: {where}",{where:h}),N&&` · ${N.cost>0?`$${N.cost.toFixed(3)} · `:""}${(N.promptTokens+N.completionTokens).toLocaleString("en-US")} ${s("tokens")} · ${(N.ms/1e3).toFixed(1)}s`]})]}),n.jsx("button",{type:"button",className:"icon-btn",title:s("Close"),"aria-label":s("Close"),onClick:p,children:"Ã"})]}),x&&n.jsx(le,{topic:"assistant",onNeed:()=>{},onClose:()=>T(!1)}),S?n.jsxs("div",{className:"assistant-list","data-help":"assistant-threads",children:[n.jsx("p",{className:"hint",children:s("Past conversations, with the page they happened on.")}
3),n.jsxs("ul",{className:"thread-list",children:[S.map(e=>n.jsxs("li",{children:[n.jsxs("button",{type:"button",className:`thread-row ${e.id===M?"on":""}`,onClick:()=>te(e.id),children:[n.jsx("span",{className:"thread-title",children:e.title||s("Without a question")}),n.jsxs("span",{className:"thread-sub",children:[e.where||s("Map")," · ",new Date(e.at).toLocaleString(oe(s.lang),{dateStyle:"short",timeStyle:"short"})," · ",s.n(e.questions,"question","questions")]})]}),w&&n.jsx("button",{type:"button",className:"icon-btn tiny",title:s("Delete"),"aria-label":s("Delete"),onClick:async()=>{await _.assistantDeleteThread(e.id).catch(()=>{}),b(i=>(i??[]).filter(t=>t.id!==e.id)),e.id===M&&z()},children:"Ã"})]},e.id)),S.length===0&&n.jsx("li",{className:"hint",children:s("Nothing yet.")})]})]}):n.jsxs("div",{className:"assistant-list",ref:L,children:[u.length===0&&n.jsxs("div",{className:"assistant-empty",children:[n.jsx("p",{className:"hint",children:s("Ask about the structure, or say what to change â every change is shown as a card and applied only after you confirm it. The data (names, titles) is sent to OpenAI with each question.")}),n.jsx("div",{className:"assistant-chips","data-help":"assistant-chips",children:he.filter(e=>e!==Z||m("create_person")).map(e=>n.jsx("button",{type:"button",className:"assistant-chip",onClick:()=>q(s(e)),children:s(e)},e))})]}),u.map((e,i)=>n.jsxs("div",{className:`bubble ${e.role==="user"?"from-user":"from-bot"}`,children:[e.role==="assistant"&&e.where&&e.where!==u.slice(0,i).reverse().find(t=>t.role==="assistant")?.where&&n.jsx("span",{className:"bubble-where",children:e.where}),e.role==="assistant"&&e.error?n.jsx("p",{className:"error",children:s(e.error)}):e.role==="assistant"&&e.pending?n.jsxs("span",{className:"dots","aria-label":s("Thinkingâ¦"),children:[n.jsx("i",{}),n.jsx("i",{}),n.jsx("i",{})]}):e.role==="assistant"?n.jsx(ce,{text:e.text}):n.jsx("p",{children:e.text}),J(e,"top"),e.role==="assistant"&&e.actions?.map(t=>n.jsxs("div",{className:`action-card ${t.kind} ${t.state}`,"data-help":"assistant-action",children:[n.jsxs("div",{className:"action-head",children:[n.jsx("span",{className:`action-kind ${t.kind}`,children:s(me[t.kind])}),n.jsx("span",{className:"action-title",children:t.title})]}),t.detail&&n.jsx("p",{className:"hint",children:t.detail}),t.warning&&t.state!=="applied"&&n.jsx("p",{className:"action-warning",children:t.warning}),t.state==="proposed"||t.state==="failed"?n.jsxs("div",{className:"action-buttons",children:[f(t)&&n.jsx("button",{type:"button",className:`btn small ${t.risky?"danger":"primary"}`,disabled:A===t.id,onClick:()=>Y(t),children:A===t.id?"â¦":t.risky?s("Yes, delete"):t.state==="failed"?s("Try again"):s("Apply")}),!o&&n.jsx("button",{type:"button",className:"btn small",onClick:()=>V(t),children:s("Skip")}),!f(t)&&n.jsx("span",{className:"hint",children:s("View only")}),t.error&&n.jsx("span",{className:"error",children:s(t.error)})]}):n.jsx("p",{className:"action-state hint",children:t.state==="applied"?`${s("Applied")}${t.summary&&t.summary!==t.title?` · ${t.summary.replace(`${t.title} â `,"")}`:""}`:s("Skipped")})]},t.id)),J(e,"bottom")]},e.id))]}),n.jsxs("form",{className:"assistant-composer","data-help":"assistant-ask",onSubmit:e=>{e.preventDefault(),q(v)},children:[n.jsx("textarea",{ref:D,rows:1,value:v,"data-hotkey":"keep",placeholder:s("Ask, or say what to changeâ¦"),onChange:e=>O(e.target.value),onKeyDown:e=>{e.key==="Enter"&&!e.shiftKey&&(e.preventDefault(),q(v)),e.key==="Escape"&&e.stopPropagation()}}),n.jsxs("div",{className:"composer-foot",children:[n.jsx("span",{className:"hint",children:s("Enter sends · Shift+Enter is a new line")}),n.jsx("button",{type:"submit",className:"btn primary","data-hotkey":"submit",disabled:P||!v.trim(),children:s("Send")})]})]})]})}export{je as AssistantPanel};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.