import{r as e}from"./core-DcH9fClu.js";import{Zn as t,nr as n,rr as r}from"./tauri-BdW-vCOV.js";var i=`orchestra.mock.roles`;function a(e){return`${e}-${Math.random().toString(36).slice(2,8)}-${Date.now().toString(36)}`}function o(){return new Date().toISOString()}function s(e){return e.trim().toLowerCase().replace(/[^a-z0-9]+/g,`-`).replace(/^-+|-+$/g,``)||`role`}function c(){let e=window.localStorage.getItem(i);return e?JSON.parse(e):null}function l(e){window.localStorage.setItem(i,JSON.stringify(e))}function u(e){return{id:e.id,slug:e.slug,name:e.name,description:e.description,provider:e.provider,model:e.model,thinkingLevel:e.thinkingLevel,capacity:e.capacity,policyIds:e.policyIds??[],directPermissions:e.directPermissions??[],archived:e.archived,createdAt:e.createdAt,updatedAt:e.updatedAt}}function d(e){let t=[],n=e.name.trim(),r=e.provider?.trim()||``,i=e.model?.trim()||``,a=e.thinkingLevel?.trim().toLowerCase()||`off`,o=e.compactionWindow?.trim()||null;return n||t.push({code:`required`,path:`name`,message:`Role name is required.`}),(e.capacity<1||!Number.isFinite(e.capacity))&&t.push({code:`invalid`,path:`capacity`,message:`Role capacity must be at least 1.`}),r&&!i&&t.push({code:`required`,path:`model`,message:`Select a model when a provider is configured.`}),!r&&i&&t.push({code:`required`,path:`provider`,message:`Select a provider when a model is configured.`}),o&&!(/^(?:[1-9]\d?|99)%$/.test(o)||/^\d+$/.test(o)||/^off$/i.test(o))&&t.push({code:`invalid`,path:`compactionWindow`,message:"Compaction window must be `10%`, a positive token reserve like `16000`, or `off`."}),[`off`,`minimal`,`low`,`medium`,`high`,`xhigh`].includes(a)||t.push({code:`invalid`,path:`thinkingLevel`,message:`Thinking level must be one of: off, minimal, low, medium, high, xhigh.`}),{valid:t.length===0,errors:t}}function f(e,t){let n=e.name.trim(),r=e.description?.trim()||null,i=e.systemPrompt?.trim()||null,c=e.provider?.trim()||null,l=e.model?.trim()||null,u=e.thinkingLevel?.trim().toLowerCase()||`off`,d=e.compactionWindow?.trim()||null,f=o(),p=m(),h=s(n),g=h,_=2;for(;p.some(e=>e.slug===g&&e.id!==t?.id);)g=`${h}-${_}`,_+=1;return{id:t?.id??a(`role`),slug:g,name:n,description:r,systemPrompt:i,provider:c,model:l,thinkingLevel:[`off`,`minimal`,`low`,`medium`,`high`,`xhigh`].includes(u)?u:`off`,capacity:Math.max(1,Math.floor(e.capacity||0)),compactionWindow:d,policyIds:Array.from(new Set(e.policyIds??t?.policyIds??[])).sort(),directPermissions:Array.from(new Set(e.directPermissions??t?.directPermissions??[])).sort(),archived:t?.archived??!1,createdAt:t?.createdAt??f,updatedAt:f}}function p(){return r(o())}function m(){let e=c();if(e){let t=e.map(e=>({...e,compactionWindow:e.compactionWindow??null,policyIds:e.policyIds??[],directPermissions:e.directPermissions??[]}));return JSON.stringify(t)!==JSON.stringify(e)&&l(t),t}let t=p();return l(t),t}function h(){return t()?.client.workers??null}async function g(r=!1){let i=t();return i?i.client.catalog.listRoles(r):n()?e(`list_roles`,{includeArchived:r}):m().filter(e=>r||!e.archived).map(u)}async function _(t){let r=h();if(r)return r.getRole(t);if(!n()){let e=m().find(e=>e.id===t);if(!e)throw Error(`Role ${t} was not found`);return e}return e(`get_role`,{roleId:t})}async function v(t){let r=h();return r?r.validateRole(t):n()?e(`validate_role`,{input:t}):d(t)}async function y(t){let r=h();if(r)return r.createRole(t);if(!n()){let e=d(t);if(!e.valid)throw Error(e.errors.map(e=>`${e.path}: ${e.message}`).join(`; `));let n=f(t);return l([n,...m()]),n}return e(`create_role`,{input:t})}async function b(t,r){let i=h();if(i)return i.updateRole(t,r);if(!n()){let e=d(r);if(!e.valid)throw Error(e.errors.map(e=>`${e.path}: ${e.message}`).join(`; `));let n=m(),i=n.find(e=>e.id===t);if(!i)throw Error(`Role ${t} was not found`);let a=f(r,i);return l(n.map(e=>e.id===t?a:e)),a}return e(`update_role`,{roleId:t,input:r})}async function x(t){let r=h();if(r)return r.archiveRole(t);if(!n()){let e=m(),n=e.find(e=>e.id===t);if(!n)throw Error(`Role ${t} was not found`);let r={...n,archived:!0,updatedAt:o()};return l(e.map(e=>e.id===t?r:e)),r}return e(`archive_role`,{roleId:t})}export{b as a,g as i,y as n,v as o,_ as r,x as t};