@lenserfight/sdk 2.1.0 → 2.2.0

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.
Files changed (3) hide show
  1. package/index.cjs +1 -1
  2. package/index.mjs +139 -81
  3. package/package.json +1 -1
package/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const A=100;class w{constructor(t){this.rpcClient=t}async browse(t,r=20){const e=Math.max(1,Math.min(r,A)),{data:n,error:a}=await this.rpcClient.rpc("fn_list_agents_by_owner",{p_owner_lenser_id:t.ownerId});if(a)throw new Error(`@lenserfight/sdk: fn_list_agents_by_owner failed — ${JSON.stringify(a)}`);return{items:(Array.isArray(n)?n:[]).slice(0,e),nextCursor:null}}async getById(t){const{data:r,error:e}=await this.rpcClient.rpc("fn_get_agent_profile",{p_ai_lenser_id:t});if(e)throw new Error(`@lenserfight/sdk: fn_get_agent_profile failed — ${JSON.stringify(e)}`);return r??null}async getLensBindings(t){const{data:r,error:e}=await this.rpcClient.rpc("fn_list_agent_lens_bindings",{p_ai_lenser_id:t,p_limit:50,p_offset:0});if(e)throw new Error(`@lenserfight/sdk: fn_list_agent_lens_bindings failed — ${JSON.stringify(e)}`);return Array.isArray(r)?r:[]}async getModelBindings(t){const{data:r,error:e}=await this.rpcClient.rpc("fn_list_agent_model_bindings",{p_ai_lenser_id:t,p_limit:50,p_offset:0});if(e)throw new Error(`@lenserfight/sdk: fn_list_agent_model_bindings failed — ${JSON.stringify(e)}`);return Array.isArray(r)?r:[]}}const v=100;class g{constructor(t){this.rpcClient=t}async browse(t={},r,e=20){const n=Math.max(1,Math.min(e,v)),a={p_search:t.search??null,p_category:t.category??null,p_status:t.status??null,p_cursor_created_at:r?.created_at??null,p_cursor_id:r?.id??null,p_limit:n},{data:s,error:l}=await this.rpcClient.rpc("fn_browse_battles",a);if(l)throw new Error(`@lenserfight/sdk: browse failed — ${JSON.stringify(l)}`);return Array.isArray(s)?s:[]}}function y(i){if(!i.url)throw new Error("@lenserfight/sdk: createClient requires `url`");if(!i.anonKey)throw new Error("@lenserfight/sdk: createClient requires `anonKey`");const t=i.fetch??globalThis.fetch;if(typeof t!="function")throw new Error("@lenserfight/sdk: global fetch unavailable — pass `fetch` in options");const r=i.url.replace(/\/+$/,""),e=i.apiKey??i.anonKey;return{async rpc(n,a){const s=await t(`${r}/rest/v1/rpc/${n}`,{method:"POST",headers:{apikey:i.anonKey,authorization:`Bearer ${e}`,"content-type":"application/json"},body:JSON.stringify(a??{})});if(!s.ok){const o=await s.text().catch(()=>"");return{data:null,error:{status:s.status,message:o||s.statusText}}}return{data:await s.json().catch(()=>null),error:null}}}}const E=100;function p(i){if(!i)return[];const t=i;return Array.isArray(t.data)?t.data:Array.isArray(i)?i:[]}function h(i){return{id:i.id,title:i.title??"",description:i.description??null,author:{id:i.lenser_id??"",handle:i.author_handle??"",displayName:i.author_handle??"",avatarUrl:null},tags:Array.isArray(i.tags)?i.tags:[],visibility:i.visibility??"public",status:i.status??"published",outputKind:null,latestVersionNumber:null,createdAt:i.created_at??""}}class m{constructor(t){this.rpcClient=t}async browse(t={},r=0,e=20){const n=Math.max(1,Math.min(e,E));if(t.search){const{data:l,error:o}=await this.rpcClient.rpc("fn_mcp_lens_search",{p_query:t.search,p_visibility:"public",p_limit:n,p_offset:Math.max(0,r)});if(o)throw new Error(`@lenserfight/sdk: fn_mcp_lens_search failed — ${JSON.stringify(o)}`);return p(l).map(h)}const{data:a,error:s}=await this.rpcClient.rpc("fn_mcp_lens_list",{p_limit:n,p_offset:Math.max(0,r),p_visibility:"public",p_status:t.status??null,p_lenser_id:null,p_include_archived:!1});if(s)throw new Error(`@lenserfight/sdk: fn_mcp_lens_list failed — ${JSON.stringify(s)}`);return p(a).map(h)}async search(t,r={},e=0,n=20){return this.browse({...r,search:t},e,n)}async getById(t){const{data:r,error:e}=await this.rpcClient.rpc("fn_get_lens_detail_bootstrap",{p_lens_id:t});if(e)throw new Error(`@lenserfight/sdk: fn_get_lens_detail_bootstrap failed — ${JSON.stringify(e)}`);return!r||r.error?null:r}async getVersion(t){const[r,e]=await Promise.all([this.rpcClient.rpc("fn_get_lens_version_detail",{p_version_id:t}),this.rpcClient.rpc("fn_get_lens_version_parameters",{p_version_id:t})]);if(r.error)throw new Error(`@lenserfight/sdk: fn_get_lens_version_detail failed — ${JSON.stringify(r.error)}`);if(!r.data)return null;const n=Array.isArray(r.data)?r.data[0]:r.data;return n?{...n,parameters:Array.isArray(e.data)?e.data:e.data??[]}:null}async resolveTemplate(t,r,e){const{data:n,error:a}=await this.rpcClient.rpc("fn_mcp_lens_resolve_template",{p_lens_id:t,p_version_id:e?.versionId??null});if(a)throw new Error(`@lenserfight/sdk: fn_mcp_lens_resolve_template failed — ${JSON.stringify(a)}`);if(!n)throw new Error(`@lenserfight/sdk: lens ${t} not found`);const s=n;let l=s.template_body;const o=[],d=[];for(const c of s.parameters??[]){const f=`[[:${c.id}]]`,u=r[c.label]??r[c.label.toLowerCase()]??Object.entries(r).find(([S])=>S.toLowerCase()===c.label.toLowerCase())?.[1];u!==void 0?(l=l.split(f).join(u),d.push(c.label)):c.optional?l=l.split(f).join(""):o.push(c.label)}return{resolvedPrompt:l,lensId:s.lens_id,versionId:s.version_id,lensTitle:s.title??t,lensDescription:s.description??null,paramsUsed:d,missing:o}}async getParameterContracts(t){const{data:r,error:e}=await this.rpcClient.rpc("fn_get_version_contracts",{p_version_id:t});if(e)throw new Error(`@lenserfight/sdk: fn_get_version_contracts failed — ${JSON.stringify(e)}`);if(!r)return[];const n=Array.isArray(r)?r[0]:r;if(!n)return[];const a=n.input_contract;if(!a)return[];const s=a.parameters??a.inputs;return Array.isArray(s)?s:[]}async extractParams(t){const r=await this.getParameterContracts(t);return{params:r,labels:r.map(e=>e.label)}}async validateParams(t,r){const e=await this.getParameterContracts(t),n=new Set(Object.keys(r).map(o=>o.toLowerCase())),a=new Set(e.map(o=>o.label.toLowerCase())),s=e.filter(o=>o.required&&!n.has(o.label.toLowerCase())).map(o=>o.label),l=Object.keys(r).filter(o=>!a.has(o.toLowerCase()));return{valid:s.length===0&&l.length===0,missing:s,unknown:l,total:e.length,provided:Object.keys(r).length}}}class b{constructor(t){this.rpcClient=t}async getContractByVersion(t){const{data:r,error:e}=await this.rpcClient.rpc("fn_get_version_contracts",{p_version_id:t});if(e)throw new Error(`@lenserfight/sdk: fn_get_version_contracts failed — ${JSON.stringify(e)}`);if(!r)return null;const n=Array.isArray(r)?r[0]:r;if(!n)return null;const a=n.input_contract;return a?{contentHash:"",body:a,publishedBy:"",publishedAt:"",supersedesHash:null}:null}async getContractByHash(t){return null}async getManifest(t){const r=await this.getContractByVersion(t);return r?{specVersion:r.body?.specVersion??"1.0.0",contentHash:r.contentHash,body:r.body,channel:null,signatures:[]}:null}async getDependencies(t){return[]}async checkCompatibility(t,r){const e=await this.getContractByVersion(t);if(!e)return{compatible:!1,missingScopes:[],warnings:["Contract not found"]};const n=e.body?.requiredScopes??[],a=r.filter(s=>!n.includes(s));return{compatible:a.length===0,missingScopes:a,warnings:[]}}}class C{constructor(t){this.rpcClient=t}async renderPrompt(t,r={}){const{data:e,error:n}=await this.rpcClient.rpc("fn_battles_render_prompt",{p_template_id:t,p_variables:r});if(n)throw new Error(`@lenserfight/sdk: renderPrompt failed — ${JSON.stringify(n)}`);return typeof e=="string"?e:""}}const $=new Set(["completed","failed","cancelled"]),O=3e3,N=12e4;class k{constructor(t){this.rpcClient=t}async browse(t){const{data:r,error:e}=await this.rpcClient.rpc("fn_mcp_workflow_list",{p_limit:t?.limit??20,p_offset:t?.offset??0,p_visibility:t?.visibility??null,p_lenser_id:null});if(e)throw new Error(`@lenserfight/sdk: fn_mcp_workflow_list failed — ${JSON.stringify(e)}`);const n=r?.data;return Array.isArray(n)?n:[]}async getById(t){const{data:r,error:e}=await this.rpcClient.rpc("fn_mcp_workflow_get",{p_workflow_id:t});if(e)throw new Error(`@lenserfight/sdk: fn_mcp_workflow_get failed — ${JSON.stringify(e)}`);return r??null}async startRun(t,r,e){const{data:n,error:a}=await this.rpcClient.rpc("fn_mcp_workflow_run_start",{p_workflow_id:t,p_inputs:r??{},p_global_model_id:e?.modelId??null,p_idempotency_key:e?.idempotencyKey??null,p_metadata:{}});if(a)throw new Error(`@lenserfight/sdk: fn_mcp_workflow_run_start failed — ${JSON.stringify(a)}`);if(!n)throw new Error(`@lenserfight/sdk: workflow ${t} not found or inaccessible`);return n}async getRunStatus(t){const{data:r,error:e}=await this.rpcClient.rpc("fn_mcp_workflow_run_status",{p_run_id:t});if(e)throw new Error(`@lenserfight/sdk: fn_mcp_workflow_run_status failed — ${JSON.stringify(e)}`);if(!r)throw new Error(`@lenserfight/sdk: run ${t} not found`);const n=r;return{id:n.id,status:n.status,activeNodeId:n.active_node_id??null,creditsSpent:n.credits_spent??0}}async getRunLogs(t){const{data:r,error:e}=await this.rpcClient.rpc("fn_mcp_workflow_run_logs",{p_run_id:t});if(e)throw new Error(`@lenserfight/sdk: fn_mcp_workflow_run_logs failed — ${JSON.stringify(e)}`);const n=r?.node_results;return Array.isArray(n)?n.map(a=>{const s=a;return{nodeId:s.node_id,status:s.status,result:s.result??null,error:s.error??null,durationMs:s.duration_ms??0,tokenCount:s.token_count??0}}):[]}async awaitRun(t,r,e){const n=e?.timeoutMs??N,a=await this.startRun(t,r,e),s=Date.now()+n;for(;Date.now()<s;){await new Promise(o=>setTimeout(o,O));const l=await this.getRunStatus(a.id);if($.has(l.status)){if(l.status==="completed")return this.getRunLogs(a.id);throw new Error(`@lenserfight/sdk: workflow run ${a.id} ended with status "${l.status}"`)}}throw new Error(`@lenserfight/sdk: workflow run ${a.id} timed out after ${n}ms`)}}class _{agents;battles;lenses;protocols;templates;workflows;rpc;constructor(t){this.rpc=t,this.agents=new w(t),this.battles=new g(t),this.lenses=new m(t),this.protocols=new b(t),this.templates=new C(t),this.workflows=new k(t)}async rpcCall(t,r){const{data:e,error:n}=await this.rpc.rpc(t,r);if(n)throw new Error(`@lenserfight/sdk: ${t} failed — ${JSON.stringify(n)}`);return e}}function L(i){const t=y(i);return new _(t)}function M(i){return new _(i)}const T="1.0.0";exports.AgentClient=w;exports.BattleClient=g;exports.LensClient=m;exports.LenserFightClient=_;exports.ProtocolClient=b;exports.SDK_VERSION=T;exports.TemplateClient=C;exports.WorkflowClient=k;exports.createClient=L;exports.createClientFromRpc=M;exports.createFetchRpcClient=y;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const v=100;class w{constructor(t){this.rpcClient=t}async browse(t,e=20){const r=Math.max(1,Math.min(e,v)),{data:n,error:a}=await this.rpcClient.rpc("fn_list_agents_by_owner",{p_owner_lenser_id:t.ownerId});if(a)throw new Error(`@lenserfight/sdk: fn_list_agents_by_owner failed — ${JSON.stringify(a)}`);return{items:(Array.isArray(n)?n:[]).slice(0,r),nextCursor:null}}async getById(t){const{data:e,error:r}=await this.rpcClient.rpc("fn_get_agent_profile",{p_ai_lenser_id:t});if(r)throw new Error(`@lenserfight/sdk: fn_get_agent_profile failed — ${JSON.stringify(r)}`);return e??null}async getLensBindings(t){const{data:e,error:r}=await this.rpcClient.rpc("fn_list_agent_lens_bindings",{p_ai_lenser_id:t,p_limit:50,p_offset:0});if(r)throw new Error(`@lenserfight/sdk: fn_list_agent_lens_bindings failed — ${JSON.stringify(r)}`);return Array.isArray(e)?e:[]}async getModelBindings(t){const{data:e,error:r}=await this.rpcClient.rpc("fn_list_agent_model_bindings",{p_ai_lenser_id:t,p_limit:50,p_offset:0});if(r)throw new Error(`@lenserfight/sdk: fn_list_agent_model_bindings failed — ${JSON.stringify(r)}`);return Array.isArray(e)?e:[]}}const A=100;class g{constructor(t){this.rpcClient=t}async browse(t={},e,r=20){const n=Math.max(1,Math.min(r,A)),a={p_search:t.search??null,p_category:t.category??null,p_status:t.status??null,p_cursor_created_at:e?.created_at??null,p_cursor_id:e?.id??null,p_limit:n},{data:i,error:l}=await this.rpcClient.rpc("fn_browse_battles",a);if(l)throw new Error(`@lenserfight/sdk: browse failed — ${JSON.stringify(l)}`);return Array.isArray(i)?i:[]}}function y(s){if(!s.url)throw new Error("@lenserfight/sdk: createClient requires `url`");if(!s.anonKey)throw new Error("@lenserfight/sdk: createClient requires `anonKey`");const t=s.fetch??globalThis.fetch;if(typeof t!="function")throw new Error("@lenserfight/sdk: global fetch unavailable — pass `fetch` in options");const e=s.url.replace(/\/+$/,""),r=s.apiKey??s.anonKey;return{async rpc(n,a){const i=await t(`${e}/rest/v1/rpc/${n}`,{method:"POST",headers:{apikey:s.anonKey,authorization:`Bearer ${r}`,"content-type":"application/json"},body:JSON.stringify(a??{})});if(!i.ok){const o=await i.text().catch(()=>"");return{data:null,error:{status:i.status,message:o||i.statusText}}}return{data:await i.json().catch(()=>null),error:null}}}}const E=100;function f(s){if(!s)return[];const t=s;return Array.isArray(t.data)?t.data:Array.isArray(s)?s:[]}function $(s){return{id:s.id,key:s.key,label:s.label??null,description:s.description??null,category:s.category,type:s.type,required:s.required??!0,placeholder:s.placeholder??null,helpText:s.help_text??null,options:s.options??null,validationSchema:s.validation_schema??null,icon:s.icon??null,color:s.color??null,isSystem:s.is_system??!1,maxLength:s.max_length??null,minLength:s.min_length??null,sortOrder:s.sort_order??0}}function O(s){const t=s.tool;return{id:s.id,label:s.label,toolId:s.tool_id??"",optional:s.optional??!1,tool:t?$(t):null}}function h(s){return{id:s.id,title:s.title??"",description:s.description??null,author:{id:s.lenser_id??"",handle:s.author_handle??"",displayName:s.author_handle??"",avatarUrl:null},tags:Array.isArray(s.tags)?s.tags:[],visibility:s.visibility??"public",status:s.status??"published",outputKind:null,latestVersionNumber:null,createdAt:s.created_at??""}}class m{constructor(t){this.rpcClient=t}async browse(t={},e=0,r=20){const n=Math.max(1,Math.min(r,E));if(t.search){const{data:l,error:o}=await this.rpcClient.rpc("fn_mcp_lens_search",{p_query:t.search,p_visibility:"public",p_limit:n,p_offset:Math.max(0,e)});if(o)throw new Error(`@lenserfight/sdk: fn_mcp_lens_search failed — ${JSON.stringify(o)}`);return f(l).map(h)}const{data:a,error:i}=await this.rpcClient.rpc("fn_mcp_lens_list",{p_limit:n,p_offset:Math.max(0,e),p_visibility:"public",p_status:t.status??null,p_lenser_id:null,p_include_archived:!1});if(i)throw new Error(`@lenserfight/sdk: fn_mcp_lens_list failed — ${JSON.stringify(i)}`);return f(a).map(h)}async search(t,e={},r=0,n=20){return this.browse({...e,search:t},r,n)}async getById(t){const{data:e,error:r}=await this.rpcClient.rpc("fn_get_lens_detail_bootstrap",{p_lens_id:t});if(r)throw new Error(`@lenserfight/sdk: fn_get_lens_detail_bootstrap failed — ${JSON.stringify(r)}`);return!e||e.error?null:e}async getVersion(t){const[e,r]=await Promise.all([this.rpcClient.rpc("fn_get_lens_version_detail",{p_version_id:t}),this.rpcClient.rpc("fn_get_lens_version_parameters",{p_version_id:t})]);if(e.error)throw new Error(`@lenserfight/sdk: fn_get_lens_version_detail failed — ${JSON.stringify(e.error)}`);if(r.error)throw new Error(`@lenserfight/sdk: fn_get_lens_version_parameters failed — ${JSON.stringify(r.error)}`);if(!e.data)return null;const n=Array.isArray(e.data)?e.data[0]:e.data;if(!n)return null;const i=(Array.isArray(r.data)?r.data:[]).map(O);return{id:n.id,lensId:n.lens_id,versionNumber:n.version_number,status:n.status,changelog:n.changelog??null,parameterCount:i.length,createdAt:n.created_at,templateBody:n.template_body,publishedAt:n.published_at??null,parameters:i}}async getLatestVersion(t){const{data:e,error:r}=await this.rpcClient.rpc("fn_get_lens_detail_bootstrap",{p_lens_id:t});if(r)throw new Error(`@lenserfight/sdk: fn_get_lens_detail_bootstrap failed — ${JSON.stringify(r)}`);if(!e||e.error)return null;const n=e.head_version_id;return n?this.getVersion(n):null}async resolveTemplate(t,e,r){const{data:n,error:a}=await this.rpcClient.rpc("fn_mcp_lens_resolve_template",{p_lens_id:t,p_version_id:r?.versionId??null});if(a)throw new Error(`@lenserfight/sdk: fn_mcp_lens_resolve_template failed — ${JSON.stringify(a)}`);if(!n)throw new Error(`@lenserfight/sdk: lens ${t} not found`);const i=n;let l=i.template_body;const o=[],d=[];for(const c of i.parameters??[]){const u=`[[:${c.id}]]`,p=e[c.label]??e[c.label.toLowerCase()]??Object.entries(e).find(([S])=>S.toLowerCase()===c.label.toLowerCase())?.[1];p!==void 0?(l=l.split(u).join(p),d.push(c.label)):c.optional?l=l.split(u).join(""):o.push(c.label)}return{resolvedPrompt:l,lensId:i.lens_id,versionId:i.version_id,lensTitle:i.title??t,lensDescription:i.description??null,paramsUsed:d,missing:o}}async getParameterContracts(t){const{data:e,error:r}=await this.rpcClient.rpc("fn_get_version_contracts",{p_version_id:t});if(r)throw new Error(`@lenserfight/sdk: fn_get_version_contracts failed — ${JSON.stringify(r)}`);if(!e)return[];const n=Array.isArray(e)?e[0]:e;if(!n)return[];const a=n.input_contract;if(!a)return[];const i=a.parameters??a.inputs;return Array.isArray(i)?i:[]}async extractParams(t){const e=await this.getParameterContracts(t);return{params:e,labels:e.map(r=>r.label)}}async validateParams(t,e){const r=await this.getParameterContracts(t),n=new Set(Object.keys(e).map(o=>o.toLowerCase())),a=new Set(r.map(o=>o.label.toLowerCase())),i=r.filter(o=>o.required&&!n.has(o.label.toLowerCase())).map(o=>o.label),l=Object.keys(e).filter(o=>!a.has(o.toLowerCase()));return{valid:i.length===0&&l.length===0,missing:i,unknown:l,total:r.length,provided:Object.keys(e).length}}}class b{constructor(t){this.rpcClient=t}async getContractByVersion(t){const{data:e,error:r}=await this.rpcClient.rpc("fn_get_version_contracts",{p_version_id:t});if(r)throw new Error(`@lenserfight/sdk: fn_get_version_contracts failed — ${JSON.stringify(r)}`);if(!e)return null;const n=Array.isArray(e)?e[0]:e;if(!n)return null;const a=n.input_contract;return a?{contentHash:"",body:a,publishedBy:"",publishedAt:"",supersedesHash:null}:null}async getContractByHash(t){return null}async getManifest(t){const e=await this.getContractByVersion(t);return e?{specVersion:e.body?.specVersion??"1.0.0",contentHash:e.contentHash,body:e.body,channel:null,signatures:[]}:null}async getDependencies(t){return[]}async checkCompatibility(t,e){const r=await this.getContractByVersion(t);if(!r)return{compatible:!1,missingScopes:[],warnings:["Contract not found"]};const n=r.body?.requiredScopes??[],a=e.filter(i=>!n.includes(i));return{compatible:a.length===0,missingScopes:a,warnings:[]}}}class C{constructor(t){this.rpcClient=t}async renderPrompt(t,e={}){const{data:r,error:n}=await this.rpcClient.rpc("fn_battles_render_prompt",{p_template_id:t,p_variables:e});if(n)throw new Error(`@lenserfight/sdk: renderPrompt failed — ${JSON.stringify(n)}`);return typeof r=="string"?r:""}}const N=new Set(["completed","failed","cancelled"]),L=3e3,M=12e4;class k{constructor(t){this.rpcClient=t}async browse(t){const{data:e,error:r}=await this.rpcClient.rpc("fn_mcp_workflow_list",{p_limit:t?.limit??20,p_offset:t?.offset??0,p_visibility:t?.visibility??null,p_lenser_id:null});if(r)throw new Error(`@lenserfight/sdk: fn_mcp_workflow_list failed — ${JSON.stringify(r)}`);const n=e?.data;return Array.isArray(n)?n:[]}async getById(t){const{data:e,error:r}=await this.rpcClient.rpc("fn_mcp_workflow_get",{p_workflow_id:t});if(r)throw new Error(`@lenserfight/sdk: fn_mcp_workflow_get failed — ${JSON.stringify(r)}`);return e??null}async startRun(t,e,r){const{data:n,error:a}=await this.rpcClient.rpc("fn_mcp_workflow_run_start",{p_workflow_id:t,p_inputs:e??{},p_global_model_id:r?.modelId??null,p_idempotency_key:r?.idempotencyKey??null,p_metadata:{}});if(a)throw new Error(`@lenserfight/sdk: fn_mcp_workflow_run_start failed — ${JSON.stringify(a)}`);if(!n)throw new Error(`@lenserfight/sdk: workflow ${t} not found or inaccessible`);return n}async getRunStatus(t){const{data:e,error:r}=await this.rpcClient.rpc("fn_mcp_workflow_run_status",{p_run_id:t});if(r)throw new Error(`@lenserfight/sdk: fn_mcp_workflow_run_status failed — ${JSON.stringify(r)}`);if(!e)throw new Error(`@lenserfight/sdk: run ${t} not found`);const n=e;return{id:n.id,status:n.status,activeNodeId:n.active_node_id??null,creditsSpent:n.credits_spent??0}}async getRunLogs(t){const{data:e,error:r}=await this.rpcClient.rpc("fn_mcp_workflow_run_logs",{p_run_id:t});if(r)throw new Error(`@lenserfight/sdk: fn_mcp_workflow_run_logs failed — ${JSON.stringify(r)}`);const n=e?.node_results;return Array.isArray(n)?n.map(a=>{const i=a;return{nodeId:i.node_id,status:i.status,result:i.result??null,error:i.error??null,durationMs:i.duration_ms??0,tokenCount:i.token_count??0}}):[]}async awaitRun(t,e,r){const n=r?.timeoutMs??M,a=await this.startRun(t,e,r),i=Date.now()+n;for(;Date.now()<i;){await new Promise(o=>setTimeout(o,L));const l=await this.getRunStatus(a.id);if(N.has(l.status)){if(l.status==="completed")return this.getRunLogs(a.id);throw new Error(`@lenserfight/sdk: workflow run ${a.id} ended with status "${l.status}"`)}}throw new Error(`@lenserfight/sdk: workflow run ${a.id} timed out after ${n}ms`)}}class _{agents;battles;lenses;protocols;templates;workflows;rpc;constructor(t){this.rpc=t,this.agents=new w(t),this.battles=new g(t),this.lenses=new m(t),this.protocols=new b(t),this.templates=new C(t),this.workflows=new k(t)}async rpcCall(t,e){const{data:r,error:n}=await this.rpc.rpc(t,e);if(n)throw new Error(`@lenserfight/sdk: ${t} failed — ${JSON.stringify(n)}`);return r}}function T(s){const t=y(s);return new _(t)}function J(s){return new _(s)}const R="1.0.0";exports.AgentClient=w;exports.BattleClient=g;exports.LensClient=m;exports.LenserFightClient=_;exports.ProtocolClient=b;exports.SDK_VERSION=R;exports.TemplateClient=C;exports.WorkflowClient=k;exports.createClient=T;exports.createClientFromRpc=J;exports.createFetchRpcClient=y;
package/index.mjs CHANGED
@@ -78,71 +78,102 @@ class m {
78
78
  p_cursor_created_at: r?.created_at ?? null,
79
79
  p_cursor_id: r?.id ?? null,
80
80
  p_limit: n
81
- }, { data: s, error: l } = await this.rpcClient.rpc("fn_browse_battles", a);
81
+ }, { data: i, error: l } = await this.rpcClient.rpc("fn_browse_battles", a);
82
82
  if (l)
83
83
  throw new Error(
84
84
  `@lenserfight/sdk: browse failed — ${JSON.stringify(l)}`
85
85
  );
86
- return Array.isArray(s) ? s : [];
86
+ return Array.isArray(i) ? i : [];
87
87
  }
88
88
  }
89
- function b(i) {
90
- if (!i.url)
89
+ function b(s) {
90
+ if (!s.url)
91
91
  throw new Error("@lenserfight/sdk: createClient requires `url`");
92
- if (!i.anonKey)
92
+ if (!s.anonKey)
93
93
  throw new Error("@lenserfight/sdk: createClient requires `anonKey`");
94
- const t = i.fetch ?? globalThis.fetch;
94
+ const t = s.fetch ?? globalThis.fetch;
95
95
  if (typeof t != "function")
96
96
  throw new Error("@lenserfight/sdk: global fetch unavailable — pass `fetch` in options");
97
- const r = i.url.replace(/\/+$/, ""), e = i.apiKey ?? i.anonKey;
97
+ const r = s.url.replace(/\/+$/, ""), e = s.apiKey ?? s.anonKey;
98
98
  return {
99
99
  async rpc(n, a) {
100
- const s = await t(`${r}/rest/v1/rpc/${n}`, {
100
+ const i = await t(`${r}/rest/v1/rpc/${n}`, {
101
101
  method: "POST",
102
102
  headers: {
103
- apikey: i.anonKey,
103
+ apikey: s.anonKey,
104
104
  authorization: `Bearer ${e}`,
105
105
  "content-type": "application/json"
106
106
  },
107
107
  body: JSON.stringify(a ?? {})
108
108
  });
109
- if (!s.ok) {
110
- const o = await s.text().catch(() => "");
109
+ if (!i.ok) {
110
+ const o = await i.text().catch(() => "");
111
111
  return {
112
112
  data: null,
113
- error: { status: s.status, message: o || s.statusText }
113
+ error: { status: i.status, message: o || i.statusText }
114
114
  };
115
115
  }
116
- return { data: await s.json().catch(() => null), error: null };
116
+ return { data: await i.json().catch(() => null), error: null };
117
117
  }
118
118
  };
119
119
  }
120
120
  const k = 100;
121
- function u(i) {
122
- if (!i) return [];
123
- const t = i;
124
- return Array.isArray(t.data) ? t.data : Array.isArray(i) ? i : [];
121
+ function p(s) {
122
+ if (!s) return [];
123
+ const t = s;
124
+ return Array.isArray(t.data) ? t.data : Array.isArray(s) ? s : [];
125
125
  }
126
- function p(i) {
126
+ function C(s) {
127
127
  return {
128
- id: i.id,
129
- title: i.title ?? "",
130
- description: i.description ?? null,
128
+ id: s.id,
129
+ key: s.key,
130
+ label: s.label ?? null,
131
+ description: s.description ?? null,
132
+ category: s.category,
133
+ type: s.type,
134
+ required: s.required ?? !0,
135
+ placeholder: s.placeholder ?? null,
136
+ helpText: s.help_text ?? null,
137
+ options: s.options ?? null,
138
+ validationSchema: s.validation_schema ?? null,
139
+ icon: s.icon ?? null,
140
+ color: s.color ?? null,
141
+ isSystem: s.is_system ?? !1,
142
+ maxLength: s.max_length ?? null,
143
+ minLength: s.min_length ?? null,
144
+ sortOrder: s.sort_order ?? 0
145
+ };
146
+ }
147
+ function v(s) {
148
+ const t = s.tool;
149
+ return {
150
+ id: s.id,
151
+ label: s.label,
152
+ toolId: s.tool_id ?? "",
153
+ optional: s.optional ?? !1,
154
+ tool: t ? C(t) : null
155
+ };
156
+ }
157
+ function f(s) {
158
+ return {
159
+ id: s.id,
160
+ title: s.title ?? "",
161
+ description: s.description ?? null,
131
162
  author: {
132
- id: i.lenser_id ?? "",
133
- handle: i.author_handle ?? "",
134
- displayName: i.author_handle ?? "",
163
+ id: s.lenser_id ?? "",
164
+ handle: s.author_handle ?? "",
165
+ displayName: s.author_handle ?? "",
135
166
  avatarUrl: null
136
167
  },
137
- tags: Array.isArray(i.tags) ? i.tags : [],
138
- visibility: i.visibility ?? "public",
139
- status: i.status ?? "published",
168
+ tags: Array.isArray(s.tags) ? s.tags : [],
169
+ visibility: s.visibility ?? "public",
170
+ status: s.status ?? "published",
140
171
  outputKind: null,
141
172
  latestVersionNumber: null,
142
- createdAt: i.created_at ?? ""
173
+ createdAt: s.created_at ?? ""
143
174
  };
144
175
  }
145
- class C {
176
+ class A {
146
177
  constructor(t) {
147
178
  this.rpcClient = t;
148
179
  }
@@ -162,9 +193,9 @@ class C {
162
193
  });
163
194
  if (o)
164
195
  throw new Error(`@lenserfight/sdk: fn_mcp_lens_search failed — ${JSON.stringify(o)}`);
165
- return u(l).map(p);
196
+ return p(l).map(f);
166
197
  }
167
- const { data: a, error: s } = await this.rpcClient.rpc("fn_mcp_lens_list", {
198
+ const { data: a, error: i } = await this.rpcClient.rpc("fn_mcp_lens_list", {
168
199
  p_limit: n,
169
200
  p_offset: Math.max(0, r),
170
201
  p_visibility: "public",
@@ -172,9 +203,9 @@ class C {
172
203
  p_lenser_id: null,
173
204
  p_include_archived: !1
174
205
  });
175
- if (s)
176
- throw new Error(`@lenserfight/sdk: fn_mcp_lens_list failed — ${JSON.stringify(s)}`);
177
- return u(a).map(p);
206
+ if (i)
207
+ throw new Error(`@lenserfight/sdk: fn_mcp_lens_list failed — ${JSON.stringify(i)}`);
208
+ return p(a).map(f);
178
209
  }
179
210
  /**
180
211
  * Full-text search across public lenses.
@@ -204,12 +235,39 @@ class C {
204
235
  ]);
205
236
  if (r.error)
206
237
  throw new Error(`@lenserfight/sdk: fn_get_lens_version_detail failed — ${JSON.stringify(r.error)}`);
238
+ if (e.error)
239
+ throw new Error(`@lenserfight/sdk: fn_get_lens_version_parameters failed — ${JSON.stringify(e.error)}`);
207
240
  if (!r.data) return null;
208
241
  const n = Array.isArray(r.data) ? r.data[0] : r.data;
209
- return n ? {
210
- ...n,
211
- parameters: Array.isArray(e.data) ? e.data : e.data ?? []
212
- } : null;
242
+ if (!n) return null;
243
+ const i = (Array.isArray(e.data) ? e.data : []).map(v);
244
+ return {
245
+ id: n.id,
246
+ lensId: n.lens_id,
247
+ versionNumber: n.version_number,
248
+ status: n.status,
249
+ changelog: n.changelog ?? null,
250
+ parameterCount: i.length,
251
+ createdAt: n.created_at,
252
+ templateBody: n.template_body,
253
+ publishedAt: n.published_at ?? null,
254
+ parameters: i
255
+ };
256
+ }
257
+ /**
258
+ * Get the latest (HEAD) version of a lens with its parameters.
259
+ * Convenience wrapper: resolves `head_version_id` via `fn_get_lens_detail_bootstrap`,
260
+ * then delegates to `getVersion()`.
261
+ */
262
+ async getLatestVersion(t) {
263
+ const { data: r, error: e } = await this.rpcClient.rpc("fn_get_lens_detail_bootstrap", {
264
+ p_lens_id: t
265
+ });
266
+ if (e)
267
+ throw new Error(`@lenserfight/sdk: fn_get_lens_detail_bootstrap failed — ${JSON.stringify(e)}`);
268
+ if (!r || r.error) return null;
269
+ const n = r.head_version_id;
270
+ return n ? this.getVersion(n) : null;
213
271
  }
214
272
  /**
215
273
  * Resolve a lens template by substituting `[[:paramId]]` tokens with the
@@ -227,19 +285,19 @@ class C {
227
285
  throw new Error(`@lenserfight/sdk: fn_mcp_lens_resolve_template failed — ${JSON.stringify(a)}`);
228
286
  if (!n)
229
287
  throw new Error(`@lenserfight/sdk: lens ${t} not found`);
230
- const s = n;
231
- let l = s.template_body;
288
+ const i = n;
289
+ let l = i.template_body;
232
290
  const o = [], _ = [];
233
- for (const c of s.parameters ?? []) {
234
- const d = `[[:${c.id}]]`, f = r[c.label] ?? r[c.label.toLowerCase()] ?? Object.entries(r).find(([w]) => w.toLowerCase() === c.label.toLowerCase())?.[1];
235
- f !== void 0 ? (l = l.split(d).join(f), _.push(c.label)) : c.optional ? l = l.split(d).join("") : o.push(c.label);
291
+ for (const c of i.parameters ?? []) {
292
+ const d = `[[:${c.id}]]`, u = r[c.label] ?? r[c.label.toLowerCase()] ?? Object.entries(r).find(([w]) => w.toLowerCase() === c.label.toLowerCase())?.[1];
293
+ u !== void 0 ? (l = l.split(d).join(u), _.push(c.label)) : c.optional ? l = l.split(d).join("") : o.push(c.label);
236
294
  }
237
295
  return {
238
296
  resolvedPrompt: l,
239
- lensId: s.lens_id,
240
- versionId: s.version_id,
241
- lensTitle: s.title ?? t,
242
- lensDescription: s.description ?? null,
297
+ lensId: i.lens_id,
298
+ versionId: i.version_id,
299
+ lensTitle: i.title ?? t,
300
+ lensDescription: i.description ?? null,
243
301
  paramsUsed: _,
244
302
  missing: o
245
303
  };
@@ -259,8 +317,8 @@ class C {
259
317
  if (!n) return [];
260
318
  const a = n.input_contract;
261
319
  if (!a) return [];
262
- const s = a.parameters ?? a.inputs;
263
- return Array.isArray(s) ? s : [];
320
+ const i = a.parameters ?? a.inputs;
321
+ return Array.isArray(i) ? i : [];
264
322
  }
265
323
  /**
266
324
  * Extract the parameters declared by a lens version — the contracts plus their labels.
@@ -273,19 +331,19 @@ class C {
273
331
  * Validate supplied values against a lens version's parameter contracts.
274
332
  */
275
333
  async validateParams(t, r) {
276
- const e = await this.getParameterContracts(t), n = new Set(Object.keys(r).map((o) => o.toLowerCase())), a = new Set(e.map((o) => o.label.toLowerCase())), s = e.filter((o) => o.required && !n.has(o.label.toLowerCase())).map((o) => o.label), l = Object.keys(r).filter(
334
+ const e = await this.getParameterContracts(t), n = new Set(Object.keys(r).map((o) => o.toLowerCase())), a = new Set(e.map((o) => o.label.toLowerCase())), i = e.filter((o) => o.required && !n.has(o.label.toLowerCase())).map((o) => o.label), l = Object.keys(r).filter(
277
335
  (o) => !a.has(o.toLowerCase())
278
336
  );
279
337
  return {
280
- valid: s.length === 0 && l.length === 0,
281
- missing: s,
338
+ valid: i.length === 0 && l.length === 0,
339
+ missing: i,
282
340
  unknown: l,
283
341
  total: e.length,
284
342
  provided: Object.keys(r).length
285
343
  };
286
344
  }
287
345
  }
288
- class A {
346
+ class S {
289
347
  constructor(t) {
290
348
  this.rpcClient = t;
291
349
  }
@@ -341,11 +399,11 @@ class A {
341
399
  const e = await this.getContractByVersion(t);
342
400
  if (!e)
343
401
  return { compatible: !1, missingScopes: [], warnings: ["Contract not found"] };
344
- const n = e.body?.requiredScopes ?? [], a = r.filter((s) => !n.includes(s));
402
+ const n = e.body?.requiredScopes ?? [], a = r.filter((i) => !n.includes(i));
345
403
  return { compatible: a.length === 0, missingScopes: a, warnings: [] };
346
404
  }
347
405
  }
348
- class S {
406
+ class $ {
349
407
  constructor(t) {
350
408
  this.rpcClient = t;
351
409
  }
@@ -367,8 +425,8 @@ class S {
367
425
  return typeof e == "string" ? e : "";
368
426
  }
369
427
  }
370
- const v = /* @__PURE__ */ new Set(["completed", "failed", "cancelled"]), $ = 3e3, E = 12e4;
371
- class O {
428
+ const E = /* @__PURE__ */ new Set(["completed", "failed", "cancelled"]), O = 3e3, N = 12e4;
429
+ class L {
372
430
  constructor(t) {
373
431
  this.rpcClient = t;
374
432
  }
@@ -449,14 +507,14 @@ class O {
449
507
  throw new Error(`@lenserfight/sdk: fn_mcp_workflow_run_logs failed — ${JSON.stringify(e)}`);
450
508
  const n = r?.node_results;
451
509
  return Array.isArray(n) ? n.map((a) => {
452
- const s = a;
510
+ const i = a;
453
511
  return {
454
- nodeId: s.node_id,
455
- status: s.status,
456
- result: s.result ?? null,
457
- error: s.error ?? null,
458
- durationMs: s.duration_ms ?? 0,
459
- tokenCount: s.token_count ?? 0
512
+ nodeId: i.node_id,
513
+ status: i.status,
514
+ result: i.result ?? null,
515
+ error: i.error ?? null,
516
+ durationMs: i.duration_ms ?? 0,
517
+ tokenCount: i.token_count ?? 0
460
518
  };
461
519
  }) : [];
462
520
  }
@@ -468,11 +526,11 @@ class O {
468
526
  * @param timeoutMs Maximum wait time in milliseconds (default 120 s).
469
527
  */
470
528
  async awaitRun(t, r, e) {
471
- const n = e?.timeoutMs ?? E, a = await this.startRun(t, r, e), s = Date.now() + n;
472
- for (; Date.now() < s; ) {
473
- await new Promise((o) => setTimeout(o, $));
529
+ const n = e?.timeoutMs ?? N, a = await this.startRun(t, r, e), i = Date.now() + n;
530
+ for (; Date.now() < i; ) {
531
+ await new Promise((o) => setTimeout(o, O));
474
532
  const l = await this.getRunStatus(a.id);
475
- if (v.has(l.status)) {
533
+ if (E.has(l.status)) {
476
534
  if (l.status === "completed")
477
535
  return this.getRunLogs(a.id);
478
536
  throw new Error(`@lenserfight/sdk: workflow run ${a.id} ended with status "${l.status}"`);
@@ -490,7 +548,7 @@ class h {
490
548
  workflows;
491
549
  rpc;
492
550
  constructor(t) {
493
- this.rpc = t, this.agents = new g(t), this.battles = new m(t), this.lenses = new C(t), this.protocols = new A(t), this.templates = new S(t), this.workflows = new O(t);
551
+ this.rpc = t, this.agents = new g(t), this.battles = new m(t), this.lenses = new A(t), this.protocols = new S(t), this.templates = new $(t), this.workflows = new L(t);
494
552
  }
495
553
  /**
496
554
  * Direct RPC escape hatch. Use sparingly — anything that's part of the
@@ -503,24 +561,24 @@ class h {
503
561
  return e;
504
562
  }
505
563
  }
506
- function N(i) {
507
- const t = b(i);
564
+ function M(s) {
565
+ const t = b(s);
508
566
  return new h(t);
509
567
  }
510
- function M(i) {
511
- return new h(i);
568
+ function J(s) {
569
+ return new h(s);
512
570
  }
513
- const L = "1.0.0";
571
+ const T = "1.0.0";
514
572
  export {
515
573
  g as AgentClient,
516
574
  m as BattleClient,
517
- C as LensClient,
575
+ A as LensClient,
518
576
  h as LenserFightClient,
519
- A as ProtocolClient,
520
- L as SDK_VERSION,
521
- S as TemplateClient,
522
- O as WorkflowClient,
523
- N as createClient,
524
- M as createClientFromRpc,
577
+ S as ProtocolClient,
578
+ T as SDK_VERSION,
579
+ $ as TemplateClient,
580
+ L as WorkflowClient,
581
+ M as createClient,
582
+ J as createClientFromRpc,
525
583
  b as createFetchRpcClient
526
584
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lenserfight/sdk",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "LenserFight public client SDK — browse and execute lenses, workflows, battles, agents, and protocols from any Supabase-backed deployment",
5
5
  "license": "MIT",
6
6
  "main": "./index.cjs",