@cursor/sdk 1.0.24 → 1.0.25

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 (77) hide show
  1. package/dist/cjs/{833.js → 318.js} +4 -4
  2. package/dist/cjs/642.js +1 -1
  3. package/dist/cjs/880.js +1 -0
  4. package/dist/cjs/agent/options.d.ts +9 -0
  5. package/dist/cjs/agent/options.d.ts.map +1 -1
  6. package/dist/cjs/agent/usage-types.d.ts +31 -0
  7. package/dist/cjs/agent/usage-types.d.ts.map +1 -1
  8. package/dist/cjs/agent.d.ts +16 -0
  9. package/dist/cjs/agent.d.ts.map +1 -1
  10. package/dist/cjs/analytics.d.ts +1 -0
  11. package/dist/cjs/analytics.d.ts.map +1 -1
  12. package/dist/cjs/cloud-agent.d.ts +3 -1
  13. package/dist/cjs/cloud-agent.d.ts.map +1 -1
  14. package/dist/cjs/cloud-api-client.d.ts +28 -0
  15. package/dist/cjs/cloud-api-client.d.ts.map +1 -1
  16. package/dist/cjs/executor-types.d.ts +1 -6
  17. package/dist/cjs/executor-types.d.ts.map +1 -1
  18. package/dist/cjs/index.d.ts +1 -1
  19. package/dist/cjs/index.d.ts.map +1 -1
  20. package/dist/cjs/index.js +1 -1
  21. package/dist/cjs/local-executor.d.ts.map +1 -1
  22. package/dist/cjs/options.d.ts +9 -0
  23. package/dist/cjs/options.d.ts.map +1 -1
  24. package/dist/cjs/platform.d.ts +22 -1
  25. package/dist/cjs/platform.d.ts.map +1 -1
  26. package/dist/cjs/public-api.d.ts +2 -2
  27. package/dist/cjs/public-api.d.ts.map +1 -1
  28. package/dist/cjs/sdk-config.d.ts +14 -0
  29. package/dist/cjs/sdk-config.d.ts.map +1 -1
  30. package/dist/cjs/sqlite.js +1 -1
  31. package/dist/cjs/stubs.d.ts +8 -1
  32. package/dist/cjs/stubs.d.ts.map +1 -1
  33. package/dist/cjs/usage-types.d.ts +31 -0
  34. package/dist/cjs/usage-types.d.ts.map +1 -1
  35. package/dist/esm/{833.js → 318.js} +3 -3
  36. package/dist/esm/357.js +1 -1
  37. package/dist/esm/642.js +1 -1
  38. package/dist/esm/agent/options.d.ts +9 -0
  39. package/dist/esm/agent/options.d.ts.map +1 -1
  40. package/dist/esm/agent/usage-types.d.ts +31 -0
  41. package/dist/esm/agent/usage-types.d.ts.map +1 -1
  42. package/dist/esm/agent.d.ts +16 -0
  43. package/dist/esm/agent.d.ts.map +1 -1
  44. package/dist/esm/analytics.d.ts +1 -0
  45. package/dist/esm/analytics.d.ts.map +1 -1
  46. package/dist/esm/cloud-agent.d.ts +3 -1
  47. package/dist/esm/cloud-agent.d.ts.map +1 -1
  48. package/dist/esm/cloud-api-client.d.ts +28 -0
  49. package/dist/esm/cloud-api-client.d.ts.map +1 -1
  50. package/dist/esm/executor-types.d.ts +1 -6
  51. package/dist/esm/executor-types.d.ts.map +1 -1
  52. package/dist/esm/index.d.ts +1 -1
  53. package/dist/esm/index.d.ts.map +1 -1
  54. package/dist/esm/index.js +1 -1
  55. package/dist/esm/local-executor.d.ts.map +1 -1
  56. package/dist/esm/options.d.ts +9 -0
  57. package/dist/esm/options.d.ts.map +1 -1
  58. package/dist/esm/platform.d.ts +22 -1
  59. package/dist/esm/platform.d.ts.map +1 -1
  60. package/dist/esm/public-api.d.ts +2 -2
  61. package/dist/esm/public-api.d.ts.map +1 -1
  62. package/dist/esm/sdk-config.d.ts +14 -0
  63. package/dist/esm/sdk-config.d.ts.map +1 -1
  64. package/dist/esm/sqlite.js +1 -1
  65. package/dist/esm/stubs.d.ts +8 -1
  66. package/dist/esm/stubs.d.ts.map +1 -1
  67. package/dist/esm/usage-types.d.ts +31 -0
  68. package/dist/esm/usage-types.d.ts.map +1 -1
  69. package/package.json +7 -7
  70. package/dist/cjs/84.js +0 -1
  71. package/dist/cjs/prefetched-blobs.d.ts +0 -13
  72. package/dist/cjs/prefetched-blobs.d.ts.map +0 -1
  73. package/dist/esm/.tsbuildinfo.schemas +0 -1
  74. package/dist/esm/prefetched-blobs.d.ts +0 -13
  75. package/dist/esm/prefetched-blobs.d.ts.map +0 -1
  76. /package/dist/cjs/{833.js.LICENSE.txt → 318.js.LICENSE.txt} +0 -0
  77. /package/dist/esm/{833.js.LICENSE.txt → 318.js.LICENSE.txt} +0 -0
package/dist/cjs/642.js CHANGED
@@ -1 +1 @@
1
- "use strict";exports.id=642,exports.ids=[642],exports.modules={"./src/agent/cloud-agent.ts"(t,e,i){i.r(e),i.d(e,{archiveCloudAgent:()=>U,cancelCloudRun:()=>x,createCloudAgent:()=>A,deleteCloudAgent:()=>T,getCloudAgent:()=>P,getCloudMe:()=>$,getCloudRun:()=>N,listCloudAgents:()=>E,listCloudModels:()=>L,listCloudRepositories:()=>F,listCloudRuns:()=>M,resumeCloudAgent:()=>k,unarchiveCloudAgent:()=>q,validateCloudModelAvailability:()=>D});var n=i("../cursor-sdk-local-runtime/dist/run-store/index.js"),s=i("./src/agent/analytics.ts"),r=i("./src/agent/cloud-api-client.ts"),o=i("./src/agent/errors.ts");function a(t){if(void 0===t)return;const e=[];for(const[i,n]of Object.entries(t))if("command"in n){if(void 0!==n.cwd)throw new o.j1(`Cloud MCP server "${i}" cannot include cwd.`);e.push(Object.assign({name:i},n))}else e.push(Object.assign({name:i},n));return e.length>0?e:void 0}var d=i("./src/agent/run-interaction-accumulator.ts"),u=i("./src/agent/subagent-conversion.ts"),l=i("./src/agent/tool-call-utils.ts"),c=i("./src/agent/types/delta-types.ts"),h=i("./src/agent/usage-types.ts"),v=function(t,e,i,n){return new(i||(i=Promise))((function(s,r){function o(t){try{d(n.next(t))}catch(t){r(t)}}function a(t){try{d(n.throw(t))}catch(t){r(t)}}function d(t){var e;t.done?s(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(o,a)}d((n=n.apply(t,e||[])).next())}))},f=function(t){return this instanceof f?(this.v=t,this):new f(t)};function g(t){if(""===t)throw new o.j1("API key is required for cloud operations. Caller passed an empty apiKey explicitly; refusing to fall back to CURSOR_API_KEY.",{isRetryable:!1,code:"unauthenticated"});const e=null!=t?t:process.env.CURSOR_API_KEY;if(!e)throw new o.j1("API key is required for cloud operations. Set CURSOR_API_KEY or pass apiKey.",{isRetryable:!1,code:"unauthenticated"});return e}const m=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function p(t){if(!t.startsWith("bc-")||!m.test(t.slice(3)))throw new o.j1("Agent ID must be in the format 'bc-<uuid>'",{isRetryable:!1})}function y(t){if(!t.startsWith("run-")||!m.test(t.slice(4)))throw new o.j1("Run ID must be in the format 'run-<uuid>'",{isRetryable:!1})}function w(t){switch(t){case"CREATING":case"RUNNING":return"running";case"FINISHED":return"finished";case"CANCELLED":return"cancelled";default:return"error"}}function b(t){return"running"===t||"completed"===t||"error"===t}const _=new Set(["assistant","thinking","tool_call"]);class I{constructor(){this.queue=[],this.waiters=[],this.closed=!1}push(t){if(this.closed)return;const e=this.waiters.shift();if(!e)return this.queue.length>=8192?(this.overflow=new o.LF("Cloud agent message buffer overflow; consumer is too slow",{code:"stream_buffer_overflow",errorName:"CursorSdkError"}),void(this.closed=!0)):void this.queue.push(t);e({value:t,done:!1})}close(){var t;if(!this.closed)for(this.closed=!0;this.waiters.length>0;)null===(t=this.waiters.shift())||void 0===t||t({value:void 0,done:!0})}stream(){return function(t,e,i){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var n,s=i.apply(t,e||[]),r=[];return n=Object.create(("function"==typeof AsyncIterator?AsyncIterator:Object).prototype),o("next"),o("throw"),o("return",(function(t){return function(e){return Promise.resolve(e).then(t,u)}})),n[Symbol.asyncIterator]=function(){return this},n;function o(t,e){s[t]&&(n[t]=function(e){return new Promise((function(i,n){r.push([t,e,i,n])>1||a(t,e)}))},e&&(n[t]=e(n[t])))}function a(t,e){try{(i=s[t](e)).value instanceof f?Promise.resolve(i.value.v).then(d,u):l(r[0][2],i)}catch(t){l(r[0][3],t)}var i}function d(t){a("next",t)}function u(t){a("throw",t)}function l(t,e){t(e),r.shift(),r.length&&a(r[0][0],r[0][1])}}(this,arguments,(function*(){for(;;){const t=this.queue.shift();if(t){yield yield f(t);continue}if(this.overflow)throw this.overflow;if(this.closed)return yield f(void 0);const e=yield f(new Promise((t=>{this.waiters.push(t)})));if(e.done)return yield f(void 0);yield yield f(e.value)}}))}}const S=new Set(["unauthorized","forbidden","not_found"]);function O(t,e){return v(this,void 0,void 0,(function*(){t<=0||(null==e?void 0:e.aborted)||(yield new Promise((i=>{const n=setTimeout((()=>{null==e||e.removeEventListener("abort",s),i()}),t),s=()=>{clearTimeout(n),i()};null==e||e.addEventListener("abort",s,{once:!0})})))}))}class C{constructor(t,e,i,n){this.client=t,this.buffer=new I,this.listeners=new Set,this.streamStarted=!1,this.emittedLegacyFrameIds=new Set,this.appliedInteractionUpdates=new Set,this.id=e.id,this.agentId=e.agentId,this.requestId=e.requestId,this._status=w(e.status),this.createdAt=Date.parse(e.createdAt);const s=(0,r.O)(e);this._result=s.result,this._error="error"===this._status?s.error:void 0,this._durationMs=s.durationMs,this._git=s.git,this._model=n,this.sendOptions=i}get status(){return this._status}get result(){return this._result}get error(){return"error"===this._status?this._error:void 0}get usage(){return this._usage}get durationMs(){return this._durationMs}get git(){return this._git}get model(){return this._model}supports(t){return"stream"===t||"wait"===t||"cancel"===t||"conversation"===t}unsupportedReason(t){if(!this.supports(t))return`Unknown run operation "${t}"`}stream(){try{return this.ensureStreamStarted(),this.buffer.stream()}catch(t){throw(0,o.Nt)(t,{operation:"run.stream"})}}conversation(){return v(this,void 0,void 0,(function*(){var t,e;try{this.ensureStreamStarted();try{yield this.streamPromise}catch(t){}return null!==(e=null===(t=this.accumulator)||void 0===t?void 0:t.conversation())&&void 0!==e?e:[]}catch(t){throw(0,o.Nt)(t,{operation:"run.conversation"})}}))}wait(){return v(this,void 0,void 0,(function*(){try{if("running"!==this._status)return this.buildResult();this.ensureStreamStarted();try{yield this.streamPromise}catch(t){"running"===this._status&&(this._error=(0,o.D7)(t),this.setStatus("error"))}return this.buildResult()}catch(t){throw(0,o.Nt)(t,{operation:"run.wait"})}}))}cancel(){return v(this,void 0,void 0,(function*(){var t;try{if("cancelled"===this._status)return;yield this.client.cancelRun({agentId:this.agentId,runId:this.id}),null===(t=this.abortController)||void 0===t||t.abort(),this.setStatus("cancelled")}catch(t){throw(0,o.Nt)(t,{operation:"run.cancel"})}}))}onDidChangeStatus(t){return this.listeners.add(t),()=>this.listeners.delete(t)}abortClientStream(){var t;null===(t=this.abortController)||void 0===t||t.abort()}disposeClientStream(){return v(this,void 0,void 0,(function*(){this.abortClientStream(),void 0!==this.streamPromise&&(yield function(t){return v(this,void 0,void 0,(function*(){let e;try{yield Promise.race([t.catch((()=>{})),new Promise((t=>{var i;e=setTimeout(t,2e3),null===(i=e.unref)||void 0===i||i.call(e)}))])}finally{void 0!==e&&clearTimeout(e)}}))}(this.streamPromise))}))}ensureStreamStarted(){var t,e;if(this.streamStarted)return;this.streamStarted=!0,this.abortController=new AbortController;const i=new d.e4({onDelta:null===(t=this.sendOptions)||void 0===t?void 0:t.onDelta,onStep:null===(e=this.sendOptions)||void 0===e?void 0:e.onStep});this.accumulator=i,this.streamPromise=this.runStreamLoop(i,this.abortController.signal),this.streamPromise.catch((()=>{}))}runStreamLoop(t,e){return v(this,void 0,void 0,(function*(){let i=0;const n=Date.now()+72e5;try{do{const r=yield this.runOneStreamAttempt(t,e);if(e.aborted)break;if("received-result"===r)break;if(yield this.refetchAndSync(),e.aborted||"running"!==this._status)break;if(Date.now()>=n)break;if(i>=6){yield this.pollRunUntilTerminal(e,n);break}const o=Math.min((s=i,Math.min(3e4,1e3*Math.pow(2,s))),Math.max(0,n-Date.now()));i++,yield O(o,e)}while("running"===this._status&&!e.aborted);"running"===this._status&&Date.now()>=n&&this.setWaitDeadlineError(),yield t.flushPendingStep()}finally{this.buffer.close()}var s}))}applyInteractionUpdate(t,e){return v(this,void 0,void 0,(function*(){var i;if(yield t.apply(e),this._usage=null!==(i=t.usage())&&void 0!==i?i:this._usage,"turn-ended"!==e.type)return;const n=(0,h.q)(e.usage);n&&this.buffer.push({type:"usage",agent_id:this.agentId,run_id:this.id,usage:n})}))}runOneStreamAttempt(t,e){return v(this,void 0,void 0,(function*(){const i=this.lastEventId;let n=!1;try{const i=yield this.client.streamRun(Object.assign({agentId:this.agentId,runId:this.id,signal:e},void 0!==this.lastEventId?{lastEventId:this.lastEventId}:{}));yield function(t,e,i,n,s){return v(this,void 0,void 0,(function*(){var a,d,u,h,v,f,g,m,p;const y=t.body.getReader(),I=new TextDecoder;let O="";try{for(;;){if(null==s?void 0:s.aborted)return;const{value:t,done:C}=yield y.read();if(C)break;O+=I.decode(t,{stream:!0});let R=O.indexOf("\n\n");for(;-1!==R;){const t=O.slice(0,R);O=O.slice(R+2),R=O.indexOf("\n\n");const s=t.split("\n");let I,C;const j=[];for(const t of s)t.startsWith("id: ")?C=t.slice(4).trim():t.startsWith("event: ")?I=t.slice(7).trim():t.startsWith("data: ")&&j.push(t.slice(6));if(void 0!==C&&C.length>0&&(void 0===I||!_.has(I))&&(null===(a=n.onEventId)||void 0===a||a.call(n,C)),"done"===I)return void(yield y.cancel());if(0===j.length)continue;const A=j.join("\n");let D;try{D=JSON.parse(A)}catch(t){throw new o.Dr(`Malformed SSE event: ${A}`,{isRetryable:!0})}if(I&&(!_.has(I)||void 0===C||!1!==(null===(d=n.shouldEmitLegacyFrame)||void 0===d?void 0:d.call(n,C))))switch(I){case"assistant":{const t=(0,l.u)(D)?D:{};n.onMessage({type:"assistant",agent_id:e,run_id:i,message:{role:"assistant",content:[{type:"text",text:null!==(u=t.text)&&void 0!==u?u:""}]}});break}case"thinking":{const t=(0,l.u)(D)?D:{};n.onMessage({type:"thinking",agent_id:e,run_id:i,text:null!==(h=t.text)&&void 0!==h?h:""});break}case"tool_call":{const t=(0,l.u)(D)?D:{},s=(0,l.u)(t.data)?t.data:t,r=b(s.status)?s.status:"running",o="string"==typeof s.callId?s.callId:"",a="string"==typeof s.name?s.name:"unknown";n.onMessage(Object.assign(Object.assign(Object.assign({type:"tool_call",agent_id:e,run_id:i,call_id:o,name:a,status:r},"args"in s?{args:s.args}:{}),"result"in s?{result:s.result}:{}),(0,l.D)(s.truncated)?{truncated:s.truncated}:{}));break}case"interaction_update":{if(void 0===n.onInteractionUpdate)break;if(!1===(null===(v=n.shouldApplyInteractionUpdate)||void 0===v?void 0:v.call(n,C,A)))break;const t=c.Zs.safeParse(D);if(!t.success)break;yield n.onInteractionUpdate(t.data);break}case"status":{const t=((0,l.u)(D)?D:{}).status;t&&(null===(f=n.onStatusChange)||void 0===f||f.call(n,w(t)),n.onMessage({type:"status",agent_id:e,run_id:i,status:t}));break}case"result":if((0,l.u)(D)){const t=D,e=Object.assign(Object.assign({},t),"string"==typeof t.text&&void 0===t.result?{result:t.text}:{});null===(g=n.onResult)||void 0===g||g.call(n,(0,r.O)(e))}return void(yield y.cancel());case"heartbeat":break;case"error":{const t=(0,l.u)(D)?D:{},e=null!==(m=t.code)&&void 0!==m?m:"UNKNOWN",i=null!==(p=t.message)&&void 0!==p?p:"Unknown error";if(S.has(e))throw new o.j1(`[${e}] ${i}`,{code:e,isRetryable:!1});throw new o.c$(`[${e}] ${i}`,{code:e,isRetryable:!1})}}}}}catch(t){if((null==s?void 0:s.aborted)||"object"==typeof t&&null!==t&&"name"in t&&"AbortError"===t.name)return;throw t}finally{try{yield y.cancel()}catch(t){}}}))}(i,this.agentId,this.id,{onMessage:t=>this.buffer.push(t),onResult:t=>{var e,i;this._result=t.result;const s=w(null!==(e=t.status)&&void 0!==e?e:"FINISHED");this._error="error"===s?null!==(i=t.error)&&void 0!==i?i:this._error:void 0,this._durationMs=t.durationMs,this._git=t.git,this.setStatus(s),n=!0},onStatusChange:t=>{"running"===t&&this.setStatus(t)},onInteractionUpdate:e=>this.applyInteractionUpdate(t,e),onEventId:t=>{this.lastEventId=t,this.emittedLegacyFrameIds.clear()},shouldEmitLegacyFrame:t=>!this.emittedLegacyFrameIds.has(t)&&(this.emittedLegacyFrameIds.add(t),!0),shouldApplyInteractionUpdate:(t,e)=>{var i;const n=void 0!==t&&t.length>0?t:`${null!==(i=this.lastEventId)&&void 0!==i?i:""}\0${e}`;return!this.appliedInteractionUpdates.has(n)&&(this.appliedInteractionUpdates.add(n),!0)}},e)}catch(t){if(function(t){return t instanceof Error&&t.message.startsWith("[invalid_last_event_id]")}(t))this.lastEventId===i&&(this.lastEventId=void 0);else if(function(t){if(t instanceof o.v3||t instanceof o.j1)return!0;if(t instanceof Error){const e=/^\[([a-z_]+)\]/.exec(t.message);if(null!==e&&S.has(e[1]))return!0}return!1}(t))throw"running"===this._status&&(this._error=(0,o.D7)(t),this.setStatus("error")),t}return n?"received-result":"stream-dropped"}))}pollRunUntilTerminal(t,e){return v(this,void 0,void 0,(function*(){for(;"running"===this._status&&!t.aborted;){const i=e-Date.now();if(i<=0)return void this.setWaitDeadlineError();if(yield O(Math.min(15e3,i),t),t.aborted)return;yield this.refetchAndSync()}}))}setWaitDeadlineError(){this._error={message:"Timed out waiting for the cloud run to finish."},this.setStatus("error")}setStatus(t){if(this._status!==t){this._status=t;for(const e of this.listeners)e(t)}}refetchAndSync(){return v(this,void 0,void 0,(function*(){let t;try{t=yield this.client.getRun({agentId:this.agentId,runId:this.id})}catch(t){if(t instanceof o.v3||t instanceof o.j1)throw"running"===this._status&&(this._error=(0,o.D7)(t),this.setStatus("error")),t;return}const e=(0,r.O)(t);void 0!==e.result&&(this._result=e.result);const i=w(t.status);"error"===i&&void 0!==e.error?this._error=e.error:"error"!==i&&(this._error=void 0),void 0!==e.durationMs&&(this._durationMs=e.durationMs),void 0!==e.git&&(this._git=e.git),"running"===this._status&&this.setStatus(i)}))}buildResult(){const t="running"===this._status?"error":this._status;return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({id:this.id},void 0!==this.requestId?{requestId:this.requestId}:{}),{status:t}),void 0!==this._result?{result:this._result}:{}),"error"===t&&void 0!==this._error?{error:this._error}:{}),void 0!==this._model?{model:this._model}:{}),void 0!==this._durationMs?{durationMs:this._durationMs}:{}),void 0!==this._git?{git:this._git}:{}),void 0!==this._usage?{usage:this._usage}:{})}}class R{constructor(t,e,i,n){this.client=e,this.options=i,this.activeRuns=new Set,this.agentId=t,this.serverCreated=n,this._model=i.model}get model(){return this._model}send(t,e){return v(this,void 0,void 0,(function*(){var i,n,r,d,l,c,h,v,f,g,m,p,y,w,b,_,I,S,O,R;const A=null!==(r=null===(n=null===(i=this.options.cloud)||void 0===i?void 0:i.env)||void 0===n?void 0:n.type)&&void 0!==r?r:"cloud";let D=!1;try{if(void 0!==(null===(d=null==e?void 0:e.local)||void 0===d?void 0:d.customTools))throw new o.j1("Custom local tools are only supported for local SDK agents. Create the agent with getLocalPlatformOptions() or omit local.customTools.",{isRetryable:!1});const i=function(t){var e;return"string"==typeof t?{text:t}:{text:t.text,images:null===(e=t.images)||void 0===e?void 0:e.map((t=>"url"in t?{url:t.url,dimension:t.dimension}:{data:t.data,mimeType:t.mimeType,dimension:t.dimension}))}}(t),n=!this.serverCreated,r=null!==(l=null==e?void 0:e.mcpServers)&&void 0!==l?l:this.options.mcpServers,k=null!==(c=null==e?void 0:e.model)&&void 0!==c?c:this._model,E=void 0!==(null==e?void 0:e.mode)?e.mode:n?this.options.mode:void 0,M=null===(h=this.options.cloud)||void 0===h?void 0:h.envVars,N=null===(v=null==e?void 0:e.cloud)||void 0===v?void 0:v.envVars,x=void 0!==(null==e?void 0:e.onStep)||void 0!==(null==e?void 0:e.onDelta)?Object.assign(Object.assign({},void 0!==e.onStep?{onStep:e.onStep}:{}),void 0!==e.onDelta?{onDelta:e.onDelta}:{}):void 0;let K;if(n){const t=(0,u.SB)(this.options.agents),n=j(M),s=yield this.client.createAgent(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({},n?{}:{agentId:this.agentId}),{prompt:i}),void 0!==k?{model:k}:{}),void 0!==this.options.name?{name:this.options.name}:{}),{mcpServers:a(r)}),void 0!==t?{customSubagents:t}:{}),{env:null===(f=this.options.cloud)||void 0===f?void 0:f.env}),j(M)?{envVars:M}:{}),j(N)?{runEnvVars:N}:{}),{repos:null===(g=this.options.cloud)||void 0===g?void 0:g.repos,workOnCurrentBranch:null===(m=this.options.cloud)||void 0===m?void 0:m.workOnCurrentBranch,autoCreatePR:null===(p=this.options.cloud)||void 0===p?void 0:p.autoCreatePR,skipReviewerRequest:null===(y=this.options.cloud)||void 0===y?void 0:y.skipReviewerRequest}),void 0!==E?{mode:E}:{}),{idempotencyKey:null!==(w=null==e?void 0:e.idempotencyKey)&&void 0!==w?w:this.options.idempotencyKey});if(n)this.agentId=s.agent.id;else if(s.agent.id!==this.agentId)throw new o.c$(`Server returned mismatched agent id "${s.agent.id}"; expected "${this.agentId}". This indicates a backend bug.`,{isRetryable:!1});this.serverCreated=!0,K=new C(this.client,s.run,x,k)}else{const t=yield this.client.createRun(this.agentId,Object.assign(Object.assign(Object.assign({prompt:i,mcpServers:a(r)},void 0!==(null==e?void 0:e.model)?{model:e.model}:{}),j(N)?{envVars:N}:{}),void 0!==E?{mode:E}:{}),{idempotencyKey:null==e?void 0:e.idempotencyKey});K=new C(this.client,t.run,x,k)}void 0!==(null==e?void 0:e.model)&&(this._model=e.model);const P=null==k?void 0:k.params;(0,s.Zn)(this.options.apiKey,Object.assign(Object.assign({agent_id:this.agentId,run_id:K.id,request_id:K.requestId,runtime:A,model:null!==(b=null==k?void 0:k.id)&&void 0!==b?b:"default"},P&&P.length>0?{model_params:JSON.stringify(P)}:{}),{prompt_length:i.text.length,has_images:(null!==(I=null===(_=i.images)||void 0===_?void 0:_.length)&&void 0!==I?I:0)>0,is_resume:!n,mcp_server_count:Object.keys(null!=r?r:{}).length,cloud_repo_count:null===(O=null===(S=this.options.cloud)||void 0===S?void 0:S.repos)||void 0===O?void 0:O.length,cloud_auto_create_pr:null===(R=this.options.cloud)||void 0===R?void 0:R.autoCreatePR})),D=!0,this.activeRuns.add(K);const U=this.options.apiKey,q=this.agentId,T=K.id;return K.wait().then((t=>{var e;(0,s.G5)(U,{agent_id:q,run_id:T,request_id:K.requestId,runtime:A,end_reason:"finished"===t.status?"success":"cancelled"===t.status?"abort":"error",error_code:"error"===t.status?null===(e=t.error)||void 0===e?void 0:e.code:void 0})})).finally((()=>{this.activeRuns.delete(K)})),K.ensureStreamStarted(),K}catch(t){const e=(0,o.Nt)(t,{operation:"agent.send"});throw D||(0,s.CI)({apiKey:this.options.apiKey,runtime:A,error:e}),e}}))}close(){for(const t of this.activeRuns)t.abortClientStream();(0,s.rd)()}reload(){return v(this,void 0,void 0,(function*(){}))}[Symbol.asyncDispose](){return v(this,void 0,void 0,(function*(){yield Promise.allSettled(Array.from(this.activeRuns,(t=>t.disposeClientStream()))),yield(0,s.rd)()}))}listArtifacts(){return v(this,void 0,void 0,(function*(){try{return this.serverCreated?(yield this.client.listArtifacts(this.agentId)).items.map((t=>({path:t.path,sizeBytes:t.sizeBytes,updatedAt:t.updatedAt}))):[]}catch(t){throw(0,o.Nt)(t,{operation:"agent.listArtifacts"})}}))}downloadArtifact(t){return v(this,void 0,void 0,(function*(){try{if(!this.serverCreated)throw new o.j1("Agent has not been created yet; call send() first.",{isRetryable:!1});const{url:e}=yield this.client.getArtifactDownloadUrl({agentId:this.agentId,path:t});let i;try{i=yield fetch(e)}catch(t){throw new o.Dr("Failed to download artifact",{cause:t,isRetryable:!0})}if(!i.ok)throw new o.Dr(`Failed to download artifact: ${i.status}`,{status:i.status,isRetryable:i.status>=500});return Buffer.from(yield i.arrayBuffer())}catch(t){throw(0,o.Nt)(t,{operation:"agent.downloadArtifact"})}}))}}function j(t){return void 0!==t&&Object.keys(t).length>0}function A(t){var e,i;const s=g(t.apiKey);if(void 0!==t.agentId&&j(null===(e=t.cloud)||void 0===e?void 0:e.envVars))throw new o.j1("cloud.envVars cannot be combined with agentId; omit agentId so the server can mint an agent id.",{isRetryable:!1});const a=new r.N(s),d=null!==(i=t.agentId)&&void 0!==i?i:(0,n.M2)();return new R(d,a,t,!1)}function D(t){return v(this,arguments,void 0,(function*({apiKey:t,model:e}){const i="string"==typeof e?e.trim():e.id.trim();if(0===i.length)throw new o.j1("Model id is required. Pass a non-empty `model.id` or `model: '<model-id>'`.",{isRetryable:!1,code:"invalid_model"});let n,s;try{n=g(t)}catch(t){if(t instanceof o.j1)return;throw t}try{s=(yield new r.N(n).listModels()).items}catch(t){if(t instanceof o.Dr||t instanceof o.v3||t instanceof o.OE)return;throw t}const a=s.some((t=>{var e;return t.id===i||!0===(null===(e=t.aliases)||void 0===e?void 0:e.includes(i))}));if(!a)throw new o.j1(`Model '${i}' is not available or invalid. Call Cursor.models.list() to see valid model ids.`,{isRetryable:!1,code:"invalid_model"})}))}function k(t,e){const i=g(e.apiKey),n=new r.N(i);return new R(t,n,Object.assign({},e),!0)}function E(t){return v(this,void 0,void 0,(function*(){const e=g(t.apiKey),i=new r.N(e),n=yield i.listAgents({limit:t.limit,cursor:t.cursor,prUrl:t.prUrl,includeArchived:t.includeArchived});return{items:n.items.map(K),nextCursor:n.nextCursor}}))}function M(t,e){return v(this,void 0,void 0,(function*(){const i=g(e.apiKey),n=new r.N(i),s=yield n.listRuns(t,{limit:e.limit,cursor:e.cursor});return{items:s.items.map((t=>new C(n,t))),nextCursor:s.nextCursor}}))}function N(t,e){return v(this,void 0,void 0,(function*(){y(t),p(e.agentId);const i=g(e.apiKey),n=new r.N(i),s=yield n.getRun({agentId:e.agentId,runId:t});return new C(n,s)}))}function x(t,e){return v(this,void 0,void 0,(function*(){y(t),p(e.agentId);const i=g(e.apiKey),n=new r.N(i);yield n.cancelRun({agentId:e.agentId,runId:t})}))}function K(t){var e,i,n;return{agentId:t.id,name:null!==(e=t.name)&&void 0!==e?e:"",summary:null!==(i=t.name)&&void 0!==i?i:"",lastModified:new Date(t.updatedAt).getTime(),createdAt:new Date(t.createdAt).getTime(),archived:"ARCHIVED"===t.status,runtime:"cloud",env:t.env,repos:null===(n=t.repos)||void 0===n?void 0:n.map((t=>t.url))}}function P(t,e){return v(this,void 0,void 0,(function*(){const i=g(e.apiKey),n=new r.N(i);return K(yield n.getAgent(t))}))}function U(t,e){return v(this,void 0,void 0,(function*(){const i=g(e.apiKey),n=new r.N(i);yield n.archiveAgent(t)}))}function q(t,e){return v(this,void 0,void 0,(function*(){const i=g(e.apiKey),n=new r.N(i);yield n.unarchiveAgent(t)}))}function T(t,e){return v(this,void 0,void 0,(function*(){const i=g(e.apiKey),n=new r.N(i);yield n.deleteAgent(t)}))}function $(t){return v(this,void 0,void 0,(function*(){const e=g(t.apiKey);return new r.N(e).getMe()}))}function L(t){return v(this,void 0,void 0,(function*(){const e=g(t.apiKey),i=new r.N(e);return(yield i.listModels()).items}))}function F(t){return v(this,void 0,void 0,(function*(){const e=g(t.apiKey),i=new r.N(e);return(yield i.listRepositories()).items}))}}};
1
+ "use strict";exports.id=642,exports.ids=[642],exports.modules={"./src/agent/cloud-agent.ts"(t,e,i){i.r(e),i.d(e,{archiveCloudAgent:()=>T,cancelCloudRun:()=>K,createCloudAgent:()=>A,deleteCloudAgent:()=>L,getCloudAgent:()=>q,getCloudAgentUsage:()=>P,getCloudMe:()=>F,getCloudRun:()=>M,listCloudAgents:()=>k,listCloudModels:()=>W,listCloudRepositories:()=>V,listCloudRuns:()=>E,resumeCloudAgent:()=>N,unarchiveCloudAgent:()=>$,validateCloudModelAvailability:()=>D});var n=i("../cursor-sdk-local-runtime/dist/run-store/index.js"),s=i("./src/agent/analytics.ts"),r=i("./src/agent/cloud-api-client.ts"),o=i("./src/agent/errors.ts");function a(t){if(void 0===t)return;const e=[];for(const[i,n]of Object.entries(t))if("command"in n){if(void 0!==n.cwd)throw new o.j1(`Cloud MCP server "${i}" cannot include cwd.`);e.push(Object.assign({name:i},n))}else e.push(Object.assign({name:i},n));return e.length>0?e:void 0}var d=i("./src/agent/run-interaction-accumulator.ts"),u=i("./src/agent/subagent-conversion.ts"),l=i("./src/agent/tool-call-utils.ts"),c=i("./src/agent/types/delta-types.ts"),h=i("./src/agent/usage-types.ts"),v=function(t,e,i,n){return new(i||(i=Promise))((function(s,r){function o(t){try{d(n.next(t))}catch(t){r(t)}}function a(t){try{d(n.throw(t))}catch(t){r(t)}}function d(t){var e;t.done?s(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(o,a)}d((n=n.apply(t,e||[])).next())}))},g=function(t){return this instanceof g?(this.v=t,this):new g(t)};function f(t){if(""===t)throw new o.j1("API key is required for cloud operations. Caller passed an empty apiKey explicitly; refusing to fall back to CURSOR_API_KEY.",{isRetryable:!1,code:"unauthenticated"});const e=null!=t?t:process.env.CURSOR_API_KEY;if(!e)throw new o.j1("API key is required for cloud operations. Set CURSOR_API_KEY or pass apiKey.",{isRetryable:!1,code:"unauthenticated"});return e}const m=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function p(t){if(!t.startsWith("bc-")||!m.test(t.slice(3)))throw new o.j1("Agent ID must be in the format 'bc-<uuid>'",{isRetryable:!1})}function y(t){if(!t.startsWith("run-")||!m.test(t.slice(4)))throw new o.j1("Run ID must be in the format 'run-<uuid>'",{isRetryable:!1})}function w(t){switch(t){case"CREATING":case"RUNNING":return"running";case"FINISHED":return"finished";case"CANCELLED":return"cancelled";default:return"error"}}function b(t){return"running"===t||"completed"===t||"error"===t}const _=new Set(["assistant","thinking","tool_call"]);class I{constructor(){this.queue=[],this.waiters=[],this.closed=!1}push(t){if(this.closed)return;const e=this.waiters.shift();if(!e)return this.queue.length>=8192?(this.overflow=new o.LF("Cloud agent message buffer overflow; consumer is too slow",{code:"stream_buffer_overflow",errorName:"CursorSdkError"}),void(this.closed=!0)):void this.queue.push(t);e({value:t,done:!1})}close(){var t;if(!this.closed)for(this.closed=!0;this.waiters.length>0;)null===(t=this.waiters.shift())||void 0===t||t({value:void 0,done:!0})}stream(){return function(t,e,i){if(!Symbol.asyncIterator)throw new TypeError("Symbol.asyncIterator is not defined.");var n,s=i.apply(t,e||[]),r=[];return n=Object.create(("function"==typeof AsyncIterator?AsyncIterator:Object).prototype),o("next"),o("throw"),o("return",(function(t){return function(e){return Promise.resolve(e).then(t,u)}})),n[Symbol.asyncIterator]=function(){return this},n;function o(t,e){s[t]&&(n[t]=function(e){return new Promise((function(i,n){r.push([t,e,i,n])>1||a(t,e)}))},e&&(n[t]=e(n[t])))}function a(t,e){try{(i=s[t](e)).value instanceof g?Promise.resolve(i.value.v).then(d,u):l(r[0][2],i)}catch(t){l(r[0][3],t)}var i}function d(t){a("next",t)}function u(t){a("throw",t)}function l(t,e){t(e),r.shift(),r.length&&a(r[0][0],r[0][1])}}(this,arguments,(function*(){for(;;){const t=this.queue.shift();if(t){yield yield g(t);continue}if(this.overflow)throw this.overflow;if(this.closed)return yield g(void 0);const e=yield g(new Promise((t=>{this.waiters.push(t)})));if(e.done)return yield g(void 0);yield yield g(e.value)}}))}}const S=new Set(["unauthorized","forbidden","not_found"]);function O(t,e){return v(this,void 0,void 0,(function*(){t<=0||(null==e?void 0:e.aborted)||(yield new Promise((i=>{const n=setTimeout((()=>{null==e||e.removeEventListener("abort",s),i()}),t),s=()=>{clearTimeout(n),i()};null==e||e.addEventListener("abort",s,{once:!0})})))}))}class j{constructor(t,e,i,n){this.client=t,this.buffer=new I,this.listeners=new Set,this.streamStarted=!1,this.emittedLegacyFrameIds=new Set,this.appliedInteractionUpdates=new Set,this.id=e.id,this.agentId=e.agentId,this.requestId=e.requestId,this._status=w(e.status),this.createdAt=Date.parse(e.createdAt);const s=(0,r.O)(e);this._result=s.result,this._error="error"===this._status?s.error:void 0,this._durationMs=s.durationMs,this._git=s.git,this._model=n,this.sendOptions=i}get status(){return this._status}get result(){return this._result}get error(){return"error"===this._status?this._error:void 0}get usage(){return this._usage}get durationMs(){return this._durationMs}get git(){return this._git}get model(){return this._model}supports(t){return"stream"===t||"wait"===t||"cancel"===t||"conversation"===t}unsupportedReason(t){if(!this.supports(t))return`Unknown run operation "${t}"`}stream(){try{return this.ensureStreamStarted(),this.buffer.stream()}catch(t){throw(0,o.Nt)(t,{operation:"run.stream"})}}conversation(){return v(this,void 0,void 0,(function*(){var t,e;try{this.ensureStreamStarted();try{yield this.streamPromise}catch(t){}return null!==(e=null===(t=this.accumulator)||void 0===t?void 0:t.conversation())&&void 0!==e?e:[]}catch(t){throw(0,o.Nt)(t,{operation:"run.conversation"})}}))}wait(){return v(this,void 0,void 0,(function*(){try{if("running"!==this._status)return this.buildResult();this.ensureStreamStarted();try{yield this.streamPromise}catch(t){"running"===this._status&&(this._error=(0,o.D7)(t),this.setStatus("error"))}return this.buildResult()}catch(t){throw(0,o.Nt)(t,{operation:"run.wait"})}}))}cancel(){return v(this,void 0,void 0,(function*(){var t;try{if("cancelled"===this._status)return;yield this.client.cancelRun({agentId:this.agentId,runId:this.id}),null===(t=this.abortController)||void 0===t||t.abort(),this.setStatus("cancelled")}catch(t){throw(0,o.Nt)(t,{operation:"run.cancel"})}}))}onDidChangeStatus(t){return this.listeners.add(t),()=>this.listeners.delete(t)}abortClientStream(){var t;null===(t=this.abortController)||void 0===t||t.abort()}disposeClientStream(){return v(this,void 0,void 0,(function*(){this.abortClientStream(),void 0!==this.streamPromise&&(yield function(t){return v(this,void 0,void 0,(function*(){let e;try{yield Promise.race([t.catch((()=>{})),new Promise((t=>{var i;e=setTimeout(t,2e3),null===(i=e.unref)||void 0===i||i.call(e)}))])}finally{void 0!==e&&clearTimeout(e)}}))}(this.streamPromise))}))}ensureStreamStarted(){var t,e;if(this.streamStarted)return;this.streamStarted=!0,this.abortController=new AbortController;const i=new d.e4({onDelta:null===(t=this.sendOptions)||void 0===t?void 0:t.onDelta,onStep:null===(e=this.sendOptions)||void 0===e?void 0:e.onStep});this.accumulator=i,this.streamPromise=this.runStreamLoop(i,this.abortController.signal),this.streamPromise.catch((()=>{}))}runStreamLoop(t,e){return v(this,void 0,void 0,(function*(){let i=0;const n=Date.now()+72e5;try{do{const r=yield this.runOneStreamAttempt(t,e);if(e.aborted)break;if("received-result"===r)break;if(yield this.refetchAndSync(),e.aborted||"running"!==this._status)break;if(Date.now()>=n)break;if(i>=6){yield this.pollRunUntilTerminal(e,n);break}const o=Math.min((s=i,Math.min(3e4,1e3*Math.pow(2,s))),Math.max(0,n-Date.now()));i++,yield O(o,e)}while("running"===this._status&&!e.aborted);"running"===this._status&&Date.now()>=n&&this.setWaitDeadlineError(),yield t.flushPendingStep()}finally{this.buffer.close()}var s}))}applyInteractionUpdate(t,e){return v(this,void 0,void 0,(function*(){var i;if(yield t.apply(e),this._usage=null!==(i=t.usage())&&void 0!==i?i:this._usage,"turn-ended"!==e.type)return;const n=(0,h.q)(e.usage);n&&this.buffer.push({type:"usage",agent_id:this.agentId,run_id:this.id,usage:n})}))}runOneStreamAttempt(t,e){return v(this,void 0,void 0,(function*(){const i=this.lastEventId;let n=!1;try{const i=yield this.client.streamRun(Object.assign({agentId:this.agentId,runId:this.id,signal:e},void 0!==this.lastEventId?{lastEventId:this.lastEventId}:{}));yield function(t,e,i,n,s){return v(this,void 0,void 0,(function*(){var a,d,u,h,v,g,f,m,p;const y=t.body.getReader(),I=new TextDecoder;let O="";try{for(;;){if(null==s?void 0:s.aborted)return;const{value:t,done:j}=yield y.read();if(j)break;O+=I.decode(t,{stream:!0});let C=O.indexOf("\n\n");for(;-1!==C;){const t=O.slice(0,C);O=O.slice(C+2),C=O.indexOf("\n\n");const s=t.split("\n");let I,j;const R=[];for(const t of s)t.startsWith("id: ")?j=t.slice(4).trim():t.startsWith("event: ")?I=t.slice(7).trim():t.startsWith("data: ")&&R.push(t.slice(6));if(void 0!==j&&j.length>0&&(void 0===I||!_.has(I))&&(null===(a=n.onEventId)||void 0===a||a.call(n,j)),"done"===I)return void(yield y.cancel());if(0===R.length)continue;const A=R.join("\n");let D;try{D=JSON.parse(A)}catch(t){throw new o.Dr(`Malformed SSE event: ${A}`,{isRetryable:!0})}if(I&&(!_.has(I)||void 0===j||!1!==(null===(d=n.shouldEmitLegacyFrame)||void 0===d?void 0:d.call(n,j))))switch(I){case"assistant":{const t=(0,l.u)(D)?D:{};n.onMessage({type:"assistant",agent_id:e,run_id:i,message:{role:"assistant",content:[{type:"text",text:null!==(u=t.text)&&void 0!==u?u:""}]}});break}case"thinking":{const t=(0,l.u)(D)?D:{};n.onMessage({type:"thinking",agent_id:e,run_id:i,text:null!==(h=t.text)&&void 0!==h?h:""});break}case"tool_call":{const t=(0,l.u)(D)?D:{},s=(0,l.u)(t.data)?t.data:t,r=b(s.status)?s.status:"running",o="string"==typeof s.callId?s.callId:"",a="string"==typeof s.name?s.name:"unknown";n.onMessage(Object.assign(Object.assign(Object.assign({type:"tool_call",agent_id:e,run_id:i,call_id:o,name:a,status:r},"args"in s?{args:s.args}:{}),"result"in s?{result:s.result}:{}),(0,l.D)(s.truncated)?{truncated:s.truncated}:{}));break}case"interaction_update":{if(void 0===n.onInteractionUpdate)break;if(!1===(null===(v=n.shouldApplyInteractionUpdate)||void 0===v?void 0:v.call(n,j,A)))break;const t=c.Zs.safeParse(D);if(!t.success)break;yield n.onInteractionUpdate(t.data);break}case"status":{const t=((0,l.u)(D)?D:{}).status;t&&(null===(g=n.onStatusChange)||void 0===g||g.call(n,w(t)),n.onMessage({type:"status",agent_id:e,run_id:i,status:t}));break}case"result":if((0,l.u)(D)){const t=D,e=Object.assign(Object.assign({},t),"string"==typeof t.text&&void 0===t.result?{result:t.text}:{});null===(f=n.onResult)||void 0===f||f.call(n,(0,r.O)(e))}return void(yield y.cancel());case"heartbeat":break;case"error":{const t=(0,l.u)(D)?D:{},e=null!==(m=t.code)&&void 0!==m?m:"UNKNOWN",i=null!==(p=t.message)&&void 0!==p?p:"Unknown error";if(S.has(e))throw new o.j1(`[${e}] ${i}`,{code:e,isRetryable:!1});throw new o.c$(`[${e}] ${i}`,{code:e,isRetryable:!1})}}}}}catch(t){if((null==s?void 0:s.aborted)||"object"==typeof t&&null!==t&&"name"in t&&"AbortError"===t.name)return;throw t}finally{try{yield y.cancel()}catch(t){}}}))}(i,this.agentId,this.id,{onMessage:t=>this.buffer.push(t),onResult:t=>{var e,i;this._result=t.result;const s=w(null!==(e=t.status)&&void 0!==e?e:"FINISHED");this._error="error"===s?null!==(i=t.error)&&void 0!==i?i:this._error:void 0,this._durationMs=t.durationMs,this._git=t.git,this.setStatus(s),n=!0},onStatusChange:t=>{"running"===t&&this.setStatus(t)},onInteractionUpdate:e=>this.applyInteractionUpdate(t,e),onEventId:t=>{this.lastEventId=t,this.emittedLegacyFrameIds.clear()},shouldEmitLegacyFrame:t=>!this.emittedLegacyFrameIds.has(t)&&(this.emittedLegacyFrameIds.add(t),!0),shouldApplyInteractionUpdate:(t,e)=>{var i;const n=void 0!==t&&t.length>0?t:`${null!==(i=this.lastEventId)&&void 0!==i?i:""}\0${e}`;return!this.appliedInteractionUpdates.has(n)&&(this.appliedInteractionUpdates.add(n),!0)}},e)}catch(t){if(function(t){return t instanceof Error&&t.message.startsWith("[invalid_last_event_id]")}(t))this.lastEventId===i&&(this.lastEventId=void 0);else if(function(t){if(t instanceof o.v3||t instanceof o.j1)return!0;if(t instanceof Error){const e=/^\[([a-z_]+)\]/.exec(t.message);if(null!==e&&S.has(e[1]))return!0}return!1}(t))throw"running"===this._status&&(this._error=(0,o.D7)(t),this.setStatus("error")),t}return n?"received-result":"stream-dropped"}))}pollRunUntilTerminal(t,e){return v(this,void 0,void 0,(function*(){for(;"running"===this._status&&!t.aborted;){const i=e-Date.now();if(i<=0)return void this.setWaitDeadlineError();if(yield O(Math.min(15e3,i),t),t.aborted)return;yield this.refetchAndSync()}}))}setWaitDeadlineError(){this._error={message:"Timed out waiting for the cloud run to finish."},this.setStatus("error")}setStatus(t){if(this._status!==t){this._status=t;for(const e of this.listeners)e(t)}}refetchAndSync(){return v(this,void 0,void 0,(function*(){let t;try{t=yield this.client.getRun({agentId:this.agentId,runId:this.id})}catch(t){if(t instanceof o.v3||t instanceof o.j1)throw"running"===this._status&&(this._error=(0,o.D7)(t),this.setStatus("error")),t;return}const e=(0,r.O)(t);void 0!==e.result&&(this._result=e.result);const i=w(t.status);"error"===i&&void 0!==e.error?this._error=e.error:"error"!==i&&(this._error=void 0),void 0!==e.durationMs&&(this._durationMs=e.durationMs),void 0!==e.git&&(this._git=e.git),"running"===this._status&&this.setStatus(i)}))}buildResult(){const t="running"===this._status?"error":this._status;return Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({id:this.id},void 0!==this.requestId?{requestId:this.requestId}:{}),{status:t}),void 0!==this._result?{result:this._result}:{}),"error"===t&&void 0!==this._error?{error:this._error}:{}),void 0!==this._model?{model:this._model}:{}),void 0!==this._durationMs?{durationMs:this._durationMs}:{}),void 0!==this._git?{git:this._git}:{}),void 0!==this._usage?{usage:this._usage}:{})}}class C{constructor(t,e,i,n){this.client=e,this.options=i,this.activeRuns=new Set,this.agentId=t,this.serverCreated=n,this._model=i.model}get model(){return this._model}send(t,e){return v(this,void 0,void 0,(function*(){var i,n,r,d,l,c,h,v,g,f,m,p,y,w,b,_,I,S,O,C,A;const D=null!==(r=null===(n=null===(i=this.options.cloud)||void 0===i?void 0:i.env)||void 0===n?void 0:n.type)&&void 0!==r?r:"cloud";let N=!1;try{if(void 0!==(null===(d=null==e?void 0:e.local)||void 0===d?void 0:d.customTools))throw new o.j1("Custom local tools are only supported for local SDK agents. Create the agent with getLocalPlatformOptions() or omit local.customTools.",{isRetryable:!1});const i=function(t){var e;return"string"==typeof t?{text:t}:{text:t.text,images:null===(e=t.images)||void 0===e?void 0:e.map((t=>"url"in t?{url:t.url,dimension:t.dimension}:{data:t.data,mimeType:t.mimeType,dimension:t.dimension}))}}(t),n=!this.serverCreated,r=null!==(l=null==e?void 0:e.mcpServers)&&void 0!==l?l:this.options.mcpServers,k=null!==(c=null==e?void 0:e.model)&&void 0!==c?c:this._model,E=void 0!==(null==e?void 0:e.mode)?e.mode:n?this.options.mode:void 0,M=null===(h=this.options.cloud)||void 0===h?void 0:h.envVars,K=null===(v=null==e?void 0:e.cloud)||void 0===v?void 0:v.envVars,x=void 0!==(null==e?void 0:e.onStep)||void 0!==(null==e?void 0:e.onDelta)?Object.assign(Object.assign({},void 0!==e.onStep?{onStep:e.onStep}:{}),void 0!==e.onDelta?{onDelta:e.onDelta}:{}):void 0;let P;if(n){const t=(0,u.SB)(this.options.agents),n=R(M),s=yield this.client.createAgent(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({},n?{}:{agentId:this.agentId}),{prompt:i}),void 0!==k?{model:k}:{}),void 0!==this.options.name?{name:this.options.name}:{}),{mcpServers:a(r)}),void 0!==t?{customSubagents:t}:{}),{env:null===(g=this.options.cloud)||void 0===g?void 0:g.env}),R(M)?{envVars:M}:{}),R(K)?{runEnvVars:K}:{}),void 0!==(null===(f=this.options.cloud)||void 0===f?void 0:f.metadata)?{metadata:this.options.cloud.metadata}:{}),{repos:null===(m=this.options.cloud)||void 0===m?void 0:m.repos,workOnCurrentBranch:null===(p=this.options.cloud)||void 0===p?void 0:p.workOnCurrentBranch,autoCreatePR:null===(y=this.options.cloud)||void 0===y?void 0:y.autoCreatePR,skipReviewerRequest:null===(w=this.options.cloud)||void 0===w?void 0:w.skipReviewerRequest}),void 0!==E?{mode:E}:{}),{idempotencyKey:null!==(b=null==e?void 0:e.idempotencyKey)&&void 0!==b?b:this.options.idempotencyKey});if(n)this.agentId=s.agent.id;else if(s.agent.id!==this.agentId)throw new o.c$(`Server returned mismatched agent id "${s.agent.id}"; expected "${this.agentId}". This indicates a backend bug.`,{isRetryable:!1});this.serverCreated=!0,P=new j(this.client,s.run,x,k)}else{const t=yield this.client.createRun(this.agentId,Object.assign(Object.assign(Object.assign({prompt:i,mcpServers:a(r)},void 0!==(null==e?void 0:e.model)?{model:e.model}:{}),R(K)?{envVars:K}:{}),void 0!==E?{mode:E}:{}),{idempotencyKey:null==e?void 0:e.idempotencyKey});P=new j(this.client,t.run,x,k)}void 0!==(null==e?void 0:e.model)&&(this._model=e.model);const U=null==k?void 0:k.params;(0,s.Zn)(this.options.apiKey,Object.assign(Object.assign({agent_id:this.agentId,run_id:P.id,request_id:P.requestId,runtime:D,model:null!==(_=null==k?void 0:k.id)&&void 0!==_?_:"default"},U&&U.length>0?{model_params:JSON.stringify(U)}:{}),{prompt_length:i.text.length,has_images:(null!==(S=null===(I=i.images)||void 0===I?void 0:I.length)&&void 0!==S?S:0)>0,is_resume:!n,mcp_server_count:Object.keys(null!=r?r:{}).length,cloud_repo_count:null===(C=null===(O=this.options.cloud)||void 0===O?void 0:O.repos)||void 0===C?void 0:C.length,cloud_auto_create_pr:null===(A=this.options.cloud)||void 0===A?void 0:A.autoCreatePR})),N=!0,this.activeRuns.add(P);const q=this.options.apiKey,T=this.agentId,$=P.id;return P.wait().then((t=>{var e;(0,s.G5)(q,{agent_id:T,run_id:$,request_id:P.requestId,runtime:D,end_reason:"finished"===t.status?"success":"cancelled"===t.status?"abort":"error",error_code:"error"===t.status?null===(e=t.error)||void 0===e?void 0:e.code:void 0})})).finally((()=>{this.activeRuns.delete(P)})),P.ensureStreamStarted(),P}catch(t){const e=(0,o.Nt)(t,{operation:"agent.send"});throw N||(0,s.CI)({apiKey:this.options.apiKey,runtime:D,error:e}),e}}))}close(){for(const t of this.activeRuns)t.abortClientStream();(0,s.rd)()}reload(){return v(this,void 0,void 0,(function*(){}))}[Symbol.asyncDispose](){return v(this,void 0,void 0,(function*(){yield Promise.allSettled(Array.from(this.activeRuns,(t=>t.disposeClientStream()))),yield(0,s.rd)()}))}listArtifacts(){return v(this,void 0,void 0,(function*(){try{return this.serverCreated?(yield this.client.listArtifacts(this.agentId)).items.map((t=>({path:t.path,sizeBytes:t.sizeBytes,updatedAt:t.updatedAt}))):[]}catch(t){throw(0,o.Nt)(t,{operation:"agent.listArtifacts"})}}))}getUsage(t){return v(this,void 0,void 0,(function*(){try{return x(yield this.client.getAgentUsage(this.agentId,t))}catch(t){throw(0,o.Nt)(t,{operation:"agent.getUsage"})}}))}downloadArtifact(t){return v(this,void 0,void 0,(function*(){try{if(!this.serverCreated)throw new o.j1("Agent has not been created yet; call send() first.",{isRetryable:!1});const{url:e}=yield this.client.getArtifactDownloadUrl({agentId:this.agentId,path:t});let i;try{i=yield fetch(e)}catch(t){throw new o.Dr("Failed to download artifact",{cause:t,isRetryable:!0})}if(!i.ok)throw new o.Dr(`Failed to download artifact: ${i.status}`,{status:i.status,isRetryable:i.status>=500});return Buffer.from(yield i.arrayBuffer())}catch(t){throw(0,o.Nt)(t,{operation:"agent.downloadArtifact"})}}))}}function R(t){return void 0!==t&&Object.keys(t).length>0}function A(t){var e,i;const s=f(t.apiKey);if(void 0!==t.agentId&&R(null===(e=t.cloud)||void 0===e?void 0:e.envVars))throw new o.j1("cloud.envVars cannot be combined with agentId; omit agentId so the server can mint an agent id.",{isRetryable:!1});const a=new r.N(s),d=null!==(i=t.agentId)&&void 0!==i?i:(0,n.M2)();return new C(d,a,t,!1)}function D(t){return v(this,arguments,void 0,(function*({apiKey:t,model:e}){const i="string"==typeof e?e.trim():e.id.trim();if(0===i.length)throw new o.j1("Model id is required. Pass a non-empty `model.id` or `model: '<model-id>'`.",{isRetryable:!1,code:"invalid_model"});let n,s;try{n=f(t)}catch(t){if(t instanceof o.j1)return;throw t}try{s=(yield new r.N(n).listModels()).items}catch(t){if(t instanceof o.Dr||t instanceof o.v3||t instanceof o.OE)return;throw t}const a=s.some((t=>{var e;return t.id===i||!0===(null===(e=t.aliases)||void 0===e?void 0:e.includes(i))}));if(!a)throw new o.j1(`Model '${i}' is not available or invalid. Call Cursor.models.list() to see valid model ids.`,{isRetryable:!1,code:"invalid_model"})}))}function N(t,e){const i=f(e.apiKey),n=new r.N(i);return new C(t,n,Object.assign({},e),!0)}function k(t){return v(this,void 0,void 0,(function*(){const e=f(t.apiKey),i=new r.N(e),n=yield i.listAgents({limit:t.limit,cursor:t.cursor,prUrl:t.prUrl,includeArchived:t.includeArchived});return{items:n.items.map(U),nextCursor:n.nextCursor}}))}function E(t,e){return v(this,void 0,void 0,(function*(){const i=f(e.apiKey),n=new r.N(i),s=yield n.listRuns(t,{limit:e.limit,cursor:e.cursor});return{items:s.items.map((t=>new j(n,t))),nextCursor:s.nextCursor}}))}function M(t,e){return v(this,void 0,void 0,(function*(){y(t),p(e.agentId);const i=f(e.apiKey),n=new r.N(i),s=yield n.getRun({agentId:e.agentId,runId:t});return new j(n,s)}))}function K(t,e){return v(this,void 0,void 0,(function*(){y(t),p(e.agentId);const i=f(e.apiKey),n=new r.N(i);yield n.cancelRun({agentId:e.agentId,runId:t})}))}function x(t){return Object.assign(Object.assign({usage:t.totalUsage},void 0!==t.cost?{cost:t.cost}:{}),{runs:t.runs.map((t=>Object.assign({runId:t.id,usage:t.usage},void 0!==t.cost?{cost:t.cost}:{})))})}function P(t,e){return v(this,void 0,void 0,(function*(){const i=f(e.apiKey),n=new r.N(i);return x(yield n.getAgentUsage(t,{runId:e.runId}))}))}function U(t){var e,i,n;return Object.assign({agentId:t.id,name:null!==(e=t.name)&&void 0!==e?e:"",summary:null!==(i=t.name)&&void 0!==i?i:"",lastModified:new Date(t.updatedAt).getTime(),createdAt:new Date(t.createdAt).getTime(),archived:"ARCHIVED"===t.status,runtime:"cloud",env:t.env,repos:null===(n=t.repos)||void 0===n?void 0:n.map((t=>t.url))},void 0!==t.metadata?{metadata:t.metadata}:{})}function q(t,e){return v(this,void 0,void 0,(function*(){const i=f(e.apiKey),n=new r.N(i);return U(yield n.getAgent(t))}))}function T(t,e){return v(this,void 0,void 0,(function*(){const i=f(e.apiKey),n=new r.N(i);yield n.archiveAgent(t)}))}function $(t,e){return v(this,void 0,void 0,(function*(){const i=f(e.apiKey),n=new r.N(i);yield n.unarchiveAgent(t)}))}function L(t,e){return v(this,void 0,void 0,(function*(){const i=f(e.apiKey),n=new r.N(i);yield n.deleteAgent(t)}))}function F(t){return v(this,void 0,void 0,(function*(){const e=f(t.apiKey);return new r.N(e).getMe()}))}function W(t){return v(this,void 0,void 0,(function*(){const e=f(t.apiKey),i=new r.N(e);return(yield i.listModels()).items}))}function V(t){return v(this,void 0,void 0,(function*(){const e=f(t.apiKey),i=new r.N(e);return(yield i.listRepositories()).items}))}}};