noema-cli 0.0.13 → 0.0.14
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/index.js +4 -4
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
var h=()=>({nextSessionId:1,sessions:new Map}),
|
|
2
|
-
`)},
|
|
3
|
-
`);o=c.pop()??"";for(let a of c){let d=
|
|
4
|
-
`);return}e(process.stdin,r,
|
|
1
|
+
var h=()=>({nextSessionId:1,sessions:new Map}),S=e=>{let t=`session-${e.nextSessionId}`;return e.nextSessionId+=1,t},x=(e,t)=>{e.sessions.has(t)||e.sessions.set(t,{id:t})};var y=async(e,t,r,s)=>{let n=[],o=s??(i=>n.push(i));switch(e.method){case"initialize":{let i=w(e.params),c=N(i);return{responses:[{jsonrpc:"2.0",id:e.id,result:{protocolVersion:c,agentCapabilities:{loadSession:v(),mcpCapabilities:{http:!1,sse:!1},promptCapabilities:{audio:!1,embeddedContext:!1,image:!1},sessionCapabilities:{}},agentInfo:{name:"noema-cli",title:"Noema CLI",version:"0.0.1"},authMethods:[]}}],notifications:n}}case"session/new":{let i=S(t);return x(t,i),{responses:[{jsonrpc:"2.0",id:e.id,result:{sessionId:i}}],notifications:n}}case"session/prompt":{let i=w(e.params),c=typeof i?.sessionId=="string"?i.sessionId:null;if(c===null)return T(e.id);let a=O(r);if(a===null)return M(e.id);x(t,c);let d=A(i);try{await a(c,d,g=>{o(j(c,g))})}catch(g){return U(e.id,g)}return{responses:[{jsonrpc:"2.0",id:e.id,result:{stopReason:"end_turn"}}],notifications:n}}default:return{responses:[{jsonrpc:"2.0",id:e.id,error:{code:-32601,message:"Method not found"}}],notifications:n}}},w=e=>l(e)?e:null,l=e=>typeof e=="object"&&e!==null,O=e=>typeof e=="function"?e:l(e)&&typeof e.runPrompt=="function"?e.runPrompt:null,A=e=>e===null?"":(Array.isArray(e.prompt)?e.prompt:[]).map(I).join(""),j=(e,t)=>({jsonrpc:"2.0",method:"session/update",params:{sessionId:e,update:{sessionUpdate:"agent_message_chunk",content:{type:"text",text:t}}}}),I=e=>{if(!l(e))return"";if(e.type==="text"&&typeof e.text=="string")return e.text;if(l(e.content)){let t=e.content;if(t.type==="text"&&typeof t.text=="string")return t.text}return""},N=e=>e===null?1:typeof e.protocolVersion=="number"?e.protocolVersion:1,v=()=>{let e=process.env.NOEMA_SESSION_PERSISTENCE;if(e===void 0||e.length===0)return!0;let t=e.toLowerCase();return!(t==="0"||t==="false"||t==="disabled"||t==="off")},T=e=>({responses:[{jsonrpc:"2.0",id:e,error:{code:-32602,message:"Invalid params"}}],notifications:[]}),M=e=>({responses:[{jsonrpc:"2.0",id:e,error:{code:-32e3,message:"Missing prompt runner"}}],notifications:[]}),U=(e,t)=>({responses:[{jsonrpc:"2.0",id:e,error:{code:-32e3,message:t instanceof Error?t.message:"Provider error"}}],notifications:[]});import{createInterface as C}from"readline";var R=(e=process.stdin,t=process.stdout,r=h(),s)=>{C({input:e}).on("line",o=>{_(o,t,r,s)})},_=async(e,t,r,s)=>{let n=e.trim();if(n.length===0)return;let o;try{o=JSON.parse(n)}catch{p(t,{jsonrpc:"2.0",id:null,error:{code:-32700,message:"Parse error"}});return}if(!J(o)){P(o)&&p(t,{jsonrpc:"2.0",id:o.id??null,error:{code:-32600,message:"Invalid Request"}});return}let{responses:i,notifications:c}=await y(o,r,s,a=>{p(t,a)});for(let a of c)p(t,a);for(let a of i)p(t,a)},p=(e,t)=>{e.write(`${JSON.stringify(t)}
|
|
2
|
+
`)},J=e=>!E(e)||e.jsonrpc!=="2.0"||typeof e.method!="string"?!1:P(e),P=e=>E(e)?typeof e.id=="string"||typeof e.id=="number"||e.id===null:!1,E=e=>typeof e=="object"&&e!==null;var f=class{config;baseUrl;constructor(t){this.config=t,this.baseUrl=t.baseUrl??"https://openrouter.ai/api/v1"}async*streamPrompt(t){let r=await fetch(`${this.baseUrl}/chat/completions`,{method:"POST",headers:{Authorization:`Bearer ${this.config.apiKey}`,"Content-Type":"application/json"},body:JSON.stringify({model:this.config.model,messages:[{role:"user",content:t}],stream:!0})});if(!r.ok)throw new Error(`OpenRouter error: ${r.status} ${r.statusText}`);if(r.body===null){let i=await r.json(),c=$(i);c.length>0&&(yield c);return}let s=r.body.getReader(),n=new TextDecoder,o="";for(;;){let i=await s.read();if(i.done)break;o+=n.decode(i.value,{stream:!0});let c=o.split(`
|
|
3
|
+
`);o=c.pop()??"";for(let a of c){let d=L(a);d!==null&&(yield d)}}}},L=e=>{let t=e.trim();if(t.length===0||!t.startsWith("data:"))return null;let r=t.slice(5).trim();if(r==="[DONE]")return null;try{let s=JSON.parse(r),n=s?.choices?.[0]?.delta?.content??s?.choices?.[0]?.message?.content;return typeof n=="string"&&n.length>0?n:null}catch{return null}},$=e=>{let t=V(e),s=(Array.isArray(t?.choices)?t.choices:[])[0];if(!u(s))return"";let n=u(s.message)?s.message:null,o=u(s.delta)?s.delta:null,i=n?.content??o?.content;return typeof i=="string"?i:""},V=e=>u(e)?e:null,u=e=>typeof e=="object"&&e!==null;var m=class{provider;sessions=new Map;constructor(t){this.provider=t}async runPrompt(t,r,s){let n="";for await(let o of this.provider.streamPrompt(r))n+=o,s(o);return this.sessions.set(t,{lastPrompt:r,lastResponse:n}),{responseText:n}}getLastInteraction(t){return this.sessions.get(t)??null}};import{readFileSync as k}from"fs";var b=(e,t=process.argv.slice(2),r=process.stdout)=>{if(t.includes("--version")||t.includes("-v")){r.write(`${B()}
|
|
4
|
+
`);return}e(process.stdin,r,D())},D=()=>{let e=null;return async(t,r,s)=>{let n=process.env.OPENROUTER_API_KEY;if(n===void 0||n.length===0)throw new Error("OPENROUTER_API_KEY is required");let o=process.env.OPENROUTER_MODEL;if(o===void 0||o.length===0)throw new Error("OPENROUTER_MODEL is required");return e===null&&(e=new m(new f({apiKey:n,model:o}))),e.runPrompt(t,r,s)}},B=()=>{try{let e=new URL("../package.json",import.meta.url),t=k(e,"utf8"),r=JSON.parse(t);return typeof r?.version=="string"?r.version:"unknown"}catch{return"unknown"}};var de="0.0.1";var K=(e=process.argv.slice(2),t=process.stdout)=>{b((r,s,n)=>{R(r,s,void 0,n)},e,t)};import.meta.url===`file://${process.argv[1]}`&&K();export{de as cliVersion,K as runCli,b as runMain};
|
|
5
5
|
//# sourceMappingURL=index.js.map
|