shuvcode 2.0.0-alpha-7 → 2.0.0-alpha-9
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/client/api.d.ts +17 -0
- package/client/api.js +0 -0
- package/client/generated/client-error.d.ts +6 -0
- package/client/generated/client-error.js +8 -0
- package/client/generated/client.d.ts +226 -0
- package/client/generated/client.js +1189 -0
- package/client/generated/index.d.ts +3 -0
- package/client/generated/index.js +3 -0
- package/client/generated/types.d.ts +6854 -0
- package/client/generated/types.js +27 -0
- package/client/index.d.ts +4 -0
- package/client/index.js +12 -0
- package/client/service.d.ts +19 -0
- package/client/service.js +235 -0
- package/package.json +24 -13
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export const isUnauthorizedError = (value) => typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "UnauthorizedError";
|
|
2
|
+
export const isInvalidRequestError = (value) => typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "InvalidRequestError";
|
|
3
|
+
export const isAgentNotFoundError = (value) => typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "AgentNotFoundError";
|
|
4
|
+
export const isInvalidCursorError = (value) => typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "InvalidCursorError";
|
|
5
|
+
export const isSessionNotFoundError = (value) => typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "SessionNotFoundError";
|
|
6
|
+
export const isMessageNotFoundError = (value) => typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "MessageNotFoundError";
|
|
7
|
+
export const isConflictError = (value) => typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "ConflictError";
|
|
8
|
+
export const isCommandNotFoundError = (value) => typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "CommandNotFoundError";
|
|
9
|
+
export const isCommandEvaluationError = (value) => typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "CommandEvaluationError";
|
|
10
|
+
export const isSkillNotFoundError = (value) => typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "SkillNotFoundError";
|
|
11
|
+
export const isServiceUnavailableError = (value) => typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "ServiceUnavailableError";
|
|
12
|
+
export const isSessionBusyError = (value) => typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "SessionBusyError";
|
|
13
|
+
export const isUnknownError = (value) => typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "UnknownError";
|
|
14
|
+
export const isInstructionEntryValueTooLargeError = (value) => typeof value === "object" &&
|
|
15
|
+
value !== null &&
|
|
16
|
+
"_tag" in value &&
|
|
17
|
+
value["_tag"] === "InstructionEntryValueTooLargeError";
|
|
18
|
+
export const isProviderNotFoundError = (value) => typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "ProviderNotFoundError";
|
|
19
|
+
export const isMcpServerNotFoundError = (value) => typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "McpServerNotFoundError";
|
|
20
|
+
export const isFormNotFoundError = (value) => typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "FormNotFoundError";
|
|
21
|
+
export const isFormAlreadySettledError = (value) => typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "FormAlreadySettledError";
|
|
22
|
+
export const isFormInvalidAnswerError = (value) => typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "FormInvalidAnswerError";
|
|
23
|
+
export const isPermissionNotFoundError = (value) => typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "PermissionNotFoundError";
|
|
24
|
+
export const isPtyNotFoundError = (value) => typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "PtyNotFoundError";
|
|
25
|
+
export const isShellNotFoundError = (value) => typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "ShellNotFoundError";
|
|
26
|
+
export const isQuestionNotFoundError = (value) => typeof value === "object" && value !== null && "_tag" in value && value["_tag"] === "QuestionNotFoundError";
|
|
27
|
+
export const isProjectCopyError = (value) => typeof value === "object" && value !== null && "name" in value && value["name"] === "ProjectCopyError";
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export * from "./generated/index";
|
|
2
|
+
export type { AgentApi, CatalogApi, CommandApi, EventApi, IntegrationApi, ModelApi, PluginApi, ProviderApi, ReferenceApi, WebSearchApi, SessionApi, SkillApi, } from "./api.js";
|
|
3
|
+
export type { EventSubscribeOutput as OpenCodeEvent } from "./generated/types";
|
|
4
|
+
export type OpenCodeClient = ReturnType<typeof import("./generated/client").make>;
|
package/client/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
var v=Object.defineProperty;var O=(n)=>n;function q(n,s){this[n]=O.bind(null,s)}var C=(n,s)=>{for(var i in s)v(n,i,{get:s[i],enumerable:!0,configurable:!0,set:q.bind(s,i)})};class d extends Error{reason;name="ClientError";constructor(n,s){super(n,s);this.reason=n}}var R={};C(R,{make:()=>k});var P=16777216;function k(n){let s=n.fetch??globalThis.fetch,i=(e,t)=>{let o=new URL(e.path,n.baseUrl);for(let[y,a]of Object.entries(e.query??{}))f(o.searchParams,y,a);let l=new Headers(n.headers);for(let[y,a]of Object.entries(e.headers??{}))if(a!==void 0&&a!==null)l.set(y,String(a));for(let[y,a]of new Headers(t?.headers))l.set(y,a);if(e.body!==void 0&&!l.has("content-type"))l.set("content-type","application/json");return{url:o,init:{method:e.method,signal:t?.signal,headers:l,body:e.body===void 0?void 0:JSON.stringify(e.body)}}},u=async(e,t)=>{try{let o=i(e,t);return await s(o.url,o.init)}catch(o){throw new d("Transport",{cause:o})}},g=async(e,t)=>{if(t.declaredStatuses.includes(e.status))throw await h(e);try{await e.body?.cancel()}catch{}throw new d("UnexpectedStatus",{cause:{status:e.status}})},r=async(e,t)=>{let o=await u(e,t);if(o.status!==e.successStatus)return g(o,e);if(e.binary)return new Uint8Array(await o.arrayBuffer());if(e.empty){try{await o.body?.cancel()}catch{}return}return await h(o)},S=(e,t)=>({async*[Symbol.asyncIterator](){let o=await u(e,t);if(o.status!==e.successStatus)await g(o,e);if(!D(o,"text/event-stream")){try{await o.body?.cancel()}catch{}throw new d("UnsupportedContentType")}if(o.body===null)throw new d("MalformedResponse");let l=o.body.getReader(),y=new TextDecoder,a="";try{while(!0){let p;try{p=await l.read()}catch(I){throw new d("Transport",{cause:I})}if(a+=y.decode(p.value,{stream:!p.done}),a.length>P)throw new d("SseEventTooLarge");let b=!p.done&&a.endsWith("\r");if(b)a=a.slice(0,-1);if(a=a.replaceAll(`\r
|
|
2
|
+
`,`
|
|
3
|
+
`).replaceAll("\r",`
|
|
4
|
+
`),b)a+="\r";if(p.done&&a!=="")a+=`
|
|
5
|
+
|
|
6
|
+
`;let c=a.indexOf(`
|
|
7
|
+
|
|
8
|
+
`);while(c>=0){let I=a.slice(0,c);a=a.slice(c+2);let x=I.split(`
|
|
9
|
+
`).flatMap((m)=>m.startsWith("data:")?[m.slice(5).trimStart()]:[]).join(`
|
|
10
|
+
`);if(x!=="")try{yield JSON.parse(x)}catch(m){throw new d("MalformedResponse",{cause:m})}c=a.indexOf(`
|
|
11
|
+
|
|
12
|
+
`)}if(p.done)return}}finally{try{await l.cancel()}catch{}l.releaseLock()}}});return{health:{get:(e)=>r({method:"GET",path:"/api/health",successStatus:200,declaredStatuses:[401,400],empty:!1},e),stop:(e,t)=>r({method:"POST",path:"/api/service/stop",body:{instanceID:e.instanceID},successStatus:200,declaredStatuses:[401,400],empty:!1},t)},server:{get:(e)=>r({method:"GET",path:"/api/server",successStatus:200,declaredStatuses:[401,400],empty:!1},e)},location:{get:(e,t)=>r({method:"GET",path:"/api/location",query:{location:e?.location},successStatus:200,declaredStatuses:[401,400],empty:!1},t)},agent:{list:(e,t)=>r({method:"GET",path:"/api/agent",query:{location:e?.location},successStatus:200,declaredStatuses:[401,400],empty:!1},t),get:(e,t)=>r({method:"GET",path:`/api/agent/${encodeURIComponent(e.agentID)}`,query:{location:e.location},successStatus:200,declaredStatuses:[404,401,400],empty:!1},t)},plugin:{list:(e,t)=>r({method:"GET",path:"/api/plugin",query:{location:e?.location},successStatus:200,declaredStatuses:[401,400],empty:!1},t)},session:{list:(e,t)=>r({method:"GET",path:"/api/session",query:{workspace:e?.workspace,limit:e?.limit,order:e?.order,search:e?.search,parentID:e?.parentID,directory:e?.directory,project:e?.project,subpath:e?.subpath,cursor:e?.cursor},successStatus:200,declaredStatuses:[400,401],empty:!1},t),create:(e,t)=>r({method:"POST",path:"/api/session",body:{id:e?.id,title:e?.title,agent:e?.agent,model:e?.model,location:e?.location,policy:e?.policy},successStatus:200,declaredStatuses:[401,400],empty:!1},t).then((o)=>o.data),active:(e)=>r({method:"GET",path:"/api/session/active",successStatus:200,declaredStatuses:[401,400],empty:!1},e).then((t)=>t.data),get:(e,t)=>r({method:"GET",path:`/api/session/${encodeURIComponent(e.sessionID)}`,successStatus:200,declaredStatuses:[404,400,401],empty:!1},t).then((o)=>o.data),remove:(e,t)=>r({method:"DELETE",path:`/api/session/${encodeURIComponent(e.sessionID)}`,successStatus:204,declaredStatuses:[404,400,401],empty:!0},t),fork:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/fork`,body:{boundary:e.boundary,policy:e.policy},successStatus:200,declaredStatuses:[404,400,401],empty:!1},t).then((o)=>o.data),switchAgent:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/agent`,body:{agent:e.agent},successStatus:204,declaredStatuses:[404,400,401],empty:!0},t),switchModel:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/model`,body:{model:e.model},successStatus:204,declaredStatuses:[404,400,401],empty:!0},t),rename:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/rename`,body:{title:e.title},successStatus:204,declaredStatuses:[404,400,401],empty:!0},t),move:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/move`,body:{directory:e.directory,workspaceID:e.workspaceID},successStatus:204,declaredStatuses:[404,400,401],empty:!0},t),prompt:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/prompt`,body:{id:e.id,text:e.text,files:e.files,agents:e.agents,output:e.output,metadata:e.metadata,delivery:e.delivery,resume:e.resume},successStatus:200,declaredStatuses:[409,400,404,401],empty:!1},t).then((o)=>o.data),command:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/command`,body:{id:e.id,command:e.command,arguments:e.arguments,agent:e.agent,model:e.model,files:e.files,agents:e.agents,delivery:e.delivery,resume:e.resume},successStatus:200,declaredStatuses:[409,400,404,500,401],empty:!1},t).then((o)=>o.data),skill:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/skill`,body:{id:e.id,skill:e.skill,resume:e.resume},successStatus:204,declaredStatuses:[404,400,401],empty:!0},t),synthetic:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/synthetic`,body:{id:e.id,text:e.text,description:e.description,metadata:e.metadata,delivery:e.delivery,resume:e.resume},successStatus:200,declaredStatuses:[409,404,400,401],empty:!1},t).then((o)=>o.data),shell:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/shell`,body:{id:e.id,command:e.command},successStatus:204,declaredStatuses:[404,400,401],empty:!0},t),compact:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/compact`,body:{id:e.id},successStatus:200,declaredStatuses:[409,404,400,401],empty:!1},t).then((o)=>o.data),wait:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/wait`,successStatus:204,declaredStatuses:[404,503,400,401],empty:!0},t),revert:{stage:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/revert/stage`,body:{messageID:e.messageID,files:e.files},successStatus:200,declaredStatuses:[404,409,500,400,401],empty:!1},t).then((o)=>o.data),clear:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/revert/clear`,successStatus:204,declaredStatuses:[404,409,500,400,401],empty:!0},t),commit:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/revert/commit`,successStatus:204,declaredStatuses:[404,409,400,401],empty:!0},t)},context:(e,t)=>r({method:"GET",path:`/api/session/${encodeURIComponent(e.sessionID)}/context`,successStatus:200,declaredStatuses:[404,500,400,401],empty:!1},t).then((o)=>o.data),pending:{list:(e,t)=>r({method:"GET",path:`/api/session/${encodeURIComponent(e.sessionID)}/pending`,successStatus:200,declaredStatuses:[404,400,401],empty:!1},t).then((o)=>o.data)},instructions:{entry:{list:(e,t)=>r({method:"GET",path:`/api/session/${encodeURIComponent(e.sessionID)}/instructions/entries`,successStatus:200,declaredStatuses:[404,400,401],empty:!1},t).then((o)=>o.data),put:(e,t)=>r({method:"PUT",path:`/api/session/${encodeURIComponent(e.sessionID)}/instructions/entries/${encodeURIComponent(e.key)}`,body:{value:e.value},successStatus:204,declaredStatuses:[404,413,400,401],empty:!0},t),remove:(e,t)=>r({method:"DELETE",path:`/api/session/${encodeURIComponent(e.sessionID)}/instructions/entries/${encodeURIComponent(e.key)}`,successStatus:204,declaredStatuses:[404,400,401],empty:!0},t)}},generate:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/generate`,body:{prompt:e.prompt},successStatus:200,declaredStatuses:[404,503,400,401],empty:!1},t).then((o)=>o.data),log:(e,t)=>S({method:"GET",path:`/api/experimental/session/${encodeURIComponent(e.sessionID)}/log`,query:{after:e.after,follow:e.follow},successStatus:200,declaredStatuses:[404,400,401],empty:!1},t),interrupt:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/interrupt`,successStatus:204,declaredStatuses:[404,400,401],empty:!0},t),background:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/background`,successStatus:204,declaredStatuses:[404,400,401],empty:!0},t),message:(e,t)=>r({method:"GET",path:`/api/session/${encodeURIComponent(e.sessionID)}/message/${encodeURIComponent(e.messageID)}`,successStatus:200,declaredStatuses:[404,400,401],empty:!1},t).then((o)=>o.data)},message:{list:(e,t)=>r({method:"GET",path:`/api/session/${encodeURIComponent(e.sessionID)}/message`,query:{limit:e.limit,order:e.order,cursor:e.cursor},successStatus:200,declaredStatuses:[400,404,500,401],empty:!1},t)},model:{list:(e,t)=>r({method:"GET",path:"/api/model",query:{location:e?.location},successStatus:200,declaredStatuses:[503,401,400],empty:!1},t),default:(e,t)=>r({method:"GET",path:"/api/model/default",query:{location:e?.location},successStatus:200,declaredStatuses:[503,401,400],empty:!1},t)},generate:{text:(e,t)=>r({method:"POST",path:"/api/generate",query:{location:e.location},body:{prompt:e.prompt,model:e.model},successStatus:200,declaredStatuses:[400,503,401],empty:!1},t).then((o)=>o.data)},provider:{list:(e,t)=>r({method:"GET",path:"/api/provider",query:{location:e?.location},successStatus:200,declaredStatuses:[503,401,400],empty:!1},t),get:(e,t)=>r({method:"GET",path:`/api/provider/${encodeURIComponent(e.providerID)}`,query:{location:e.location},successStatus:200,declaredStatuses:[404,503,401,400],empty:!1},t)},integration:{list:(e,t)=>r({method:"GET",path:"/api/integration",query:{location:e?.location},successStatus:200,declaredStatuses:[401,400],empty:!1},t),get:(e,t)=>r({method:"GET",path:`/api/integration/${encodeURIComponent(e.integrationID)}`,query:{location:e.location},successStatus:200,declaredStatuses:[401,400],empty:!1},t),wellknown:{add:(e,t)=>r({method:"POST",path:"/api/experimental/integration/wellknown",query:{location:e.location},body:{url:e.url},successStatus:204,declaredStatuses:[400,401],empty:!0},t)},connect:{key:(e,t)=>r({method:"POST",path:`/api/integration/${encodeURIComponent(e.integrationID)}/connect/key`,query:{location:e.location},body:{key:e.key,label:e.label},successStatus:204,declaredStatuses:[400,401],empty:!0},t)},oauth:{connect:(e,t)=>r({method:"POST",path:`/api/integration/${encodeURIComponent(e.integrationID)}/connect/oauth`,query:{location:e.location},body:{methodID:e.methodID,inputs:e.inputs,label:e.label},successStatus:200,declaredStatuses:[400,401],empty:!1},t),status:(e,t)=>r({method:"GET",path:`/api/integration/${encodeURIComponent(e.integrationID)}/connect/oauth/${encodeURIComponent(e.attemptID)}`,query:{location:e.location},successStatus:200,declaredStatuses:[401,400],empty:!1},t),complete:(e,t)=>r({method:"POST",path:`/api/integration/${encodeURIComponent(e.integrationID)}/connect/oauth/${encodeURIComponent(e.attemptID)}/complete`,query:{location:e.location},body:{code:e.code},successStatus:204,declaredStatuses:[400,401],empty:!0},t),cancel:(e,t)=>r({method:"DELETE",path:`/api/integration/${encodeURIComponent(e.integrationID)}/connect/oauth/${encodeURIComponent(e.attemptID)}`,query:{location:e.location},successStatus:204,declaredStatuses:[401,400],empty:!0},t)},command:{connect:(e,t)=>r({method:"POST",path:`/api/integration/${encodeURIComponent(e.integrationID)}/connect/command`,query:{location:e.location},body:{methodID:e.methodID,label:e.label},successStatus:200,declaredStatuses:[400,401],empty:!1},t),status:(e,t)=>r({method:"GET",path:`/api/integration/${encodeURIComponent(e.integrationID)}/connect/command/${encodeURIComponent(e.attemptID)}`,query:{location:e.location},successStatus:200,declaredStatuses:[401,400],empty:!1},t),cancel:(e,t)=>r({method:"DELETE",path:`/api/integration/${encodeURIComponent(e.integrationID)}/connect/command/${encodeURIComponent(e.attemptID)}`,query:{location:e.location},successStatus:204,declaredStatuses:[401,400],empty:!0},t)}},auth:{status:(e,t)=>r({method:"GET",path:"/api/auth/status",query:{location:e?.location},successStatus:200,declaredStatuses:[401,400],empty:!1},t)},mcp:{list:(e,t)=>r({method:"GET",path:"/api/mcp",query:{location:e?.location},successStatus:200,declaredStatuses:[401,400],empty:!1},t),add:(e,t)=>r({method:"PUT",path:`/api/mcp/${encodeURIComponent(e.server)}`,query:{location:e.location},body:{config:e.config},successStatus:204,declaredStatuses:[401,400],empty:!0},t),remove:(e,t)=>r({method:"DELETE",path:`/api/mcp/${encodeURIComponent(e.server)}`,query:{location:e.location},successStatus:204,declaredStatuses:[404,401,400],empty:!0},t),connect:(e,t)=>r({method:"POST",path:`/api/mcp/${encodeURIComponent(e.server)}/connect`,query:{location:e.location},successStatus:204,declaredStatuses:[404,401,400],empty:!0},t),disconnect:(e,t)=>r({method:"POST",path:`/api/mcp/${encodeURIComponent(e.server)}/disconnect`,query:{location:e.location},successStatus:204,declaredStatuses:[404,401,400],empty:!0},t),resource:{catalog:(e,t)=>r({method:"GET",path:"/api/mcp/resource",query:{location:e?.location},successStatus:200,declaredStatuses:[401,400],empty:!1},t)}},credential:{update:(e,t)=>r({method:"PATCH",path:`/api/credential/${encodeURIComponent(e.credentialID)}`,query:{location:e.location},body:{label:e.label},successStatus:204,declaredStatuses:[401,400],empty:!0},t),remove:(e,t)=>r({method:"DELETE",path:`/api/credential/${encodeURIComponent(e.credentialID)}`,query:{location:e.location},successStatus:204,declaredStatuses:[401,400],empty:!0},t)},project:{list:(e)=>r({method:"GET",path:"/api/project",successStatus:200,declaredStatuses:[401,400],empty:!1},e),current:(e,t)=>r({method:"GET",path:"/api/project/current",query:{location:e?.location},successStatus:200,declaredStatuses:[401,400],empty:!1},t),directories:(e,t)=>r({method:"GET",path:`/api/project/${encodeURIComponent(e.projectID)}/directories`,query:{location:e.location},successStatus:200,declaredStatuses:[401,400],empty:!1},t)},form:{request:{list:(e,t)=>r({method:"GET",path:"/api/form/request",query:{location:e?.location},successStatus:200,declaredStatuses:[401,400],empty:!1},t)},list:(e,t)=>r({method:"GET",path:`/api/session/${encodeURIComponent(e.sessionID)}/form`,successStatus:200,declaredStatuses:[404,400,401],empty:!1},t).then((o)=>o.data),create:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/form`,body:{id:e.id,title:e.title,metadata:e.metadata,fields:e.fields},successStatus:200,declaredStatuses:[404,409,400,401],empty:!1},t).then((o)=>o.data),get:(e,t)=>r({method:"GET",path:`/api/session/${encodeURIComponent(e.sessionID)}/form/${encodeURIComponent(e.formID)}`,successStatus:200,declaredStatuses:[404,400,401],empty:!1},t).then((o)=>o.data),state:(e,t)=>r({method:"GET",path:`/api/session/${encodeURIComponent(e.sessionID)}/form/${encodeURIComponent(e.formID)}/state`,successStatus:200,declaredStatuses:[404,400,401],empty:!1},t).then((o)=>o.data),reply:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/form/${encodeURIComponent(e.formID)}/reply`,body:{answer:e.answer},successStatus:204,declaredStatuses:[404,409,400,401],empty:!0},t),cancel:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/form/${encodeURIComponent(e.formID)}/cancel`,successStatus:204,declaredStatuses:[404,409,400,401],empty:!0},t)},permission:{request:{list:(e,t)=>r({method:"GET",path:"/api/permission/request",query:{location:e?.location},successStatus:200,declaredStatuses:[401,400],empty:!1},t)},saved:{list:(e,t)=>r({method:"GET",path:"/api/permission/saved",query:{projectID:e?.projectID},successStatus:200,declaredStatuses:[401,400],empty:!1},t).then((o)=>o.data),remove:(e,t)=>r({method:"DELETE",path:`/api/permission/saved/${encodeURIComponent(e.id)}`,successStatus:204,declaredStatuses:[401,400],empty:!0},t)},create:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/permission`,body:{id:e.id,action:e.action,resources:e.resources,save:e.save,metadata:e.metadata,source:e.source,agent:e.agent},successStatus:200,declaredStatuses:[404,400,401],empty:!1},t).then((o)=>o.data),list:(e,t)=>r({method:"GET",path:`/api/session/${encodeURIComponent(e.sessionID)}/permission`,successStatus:200,declaredStatuses:[404,400,401],empty:!1},t).then((o)=>o.data),get:(e,t)=>r({method:"GET",path:`/api/session/${encodeURIComponent(e.sessionID)}/permission/${encodeURIComponent(e.requestID)}`,successStatus:200,declaredStatuses:[404,400,401],empty:!1},t).then((o)=>o.data),reply:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/permission/${encodeURIComponent(e.requestID)}/reply`,body:{reply:e.reply,message:e.message},successStatus:204,declaredStatuses:[404,400,401],empty:!0},t)},file:{read:(e,t)=>r({method:"GET",path:`/api/fs/read/${A(e.path)}`,query:{location:e.location},successStatus:200,declaredStatuses:[401,400],empty:!1,binary:!0},t),list:(e,t)=>r({method:"GET",path:"/api/fs/list",query:{location:e?.location,path:e?.path},successStatus:200,declaredStatuses:[401,400],empty:!1},t),find:(e,t)=>r({method:"GET",path:"/api/fs/find",query:{location:e.location,query:e.query,type:e.type,limit:e.limit},successStatus:200,declaredStatuses:[401,400],empty:!1},t)},command:{list:(e,t)=>r({method:"GET",path:"/api/command",query:{location:e?.location},successStatus:200,declaredStatuses:[401,400],empty:!1},t)},skill:{list:(e,t)=>r({method:"GET",path:"/api/skill",query:{location:e?.location},successStatus:200,declaredStatuses:[401,400],empty:!1},t)},event:{subscribe:(e)=>S({method:"GET",path:"/api/event",successStatus:200,declaredStatuses:[401,400],empty:!1},e)},pty:{list:(e,t)=>r({method:"GET",path:"/api/pty",query:{location:e?.location},successStatus:200,declaredStatuses:[401,400],empty:!1},t),create:(e,t)=>r({method:"POST",path:"/api/pty",query:{location:e?.location},body:{command:e?.command,args:e?.args,cwd:e?.cwd,title:e?.title,env:e?.env},successStatus:200,declaredStatuses:[401,400],empty:!1},t),get:(e,t)=>r({method:"GET",path:`/api/pty/${encodeURIComponent(e.ptyID)}`,query:{location:e.location},successStatus:200,declaredStatuses:[404,401,400],empty:!1},t),update:(e,t)=>r({method:"PUT",path:`/api/pty/${encodeURIComponent(e.ptyID)}`,query:{location:e.location},body:{title:e.title,size:e.size},successStatus:200,declaredStatuses:[404,401,400],empty:!1},t),remove:(e,t)=>r({method:"DELETE",path:`/api/pty/${encodeURIComponent(e.ptyID)}`,query:{location:e.location},successStatus:204,declaredStatuses:[404,401,400],empty:!0},t)},shell:{list:(e,t)=>r({method:"GET",path:"/api/shell",query:{location:e?.location},successStatus:200,declaredStatuses:[401,400],empty:!1},t),create:(e,t)=>r({method:"POST",path:"/api/shell",query:{location:e.location},body:{command:e.command,cwd:e.cwd,timeout:e.timeout,metadata:e.metadata},successStatus:200,declaredStatuses:[401,400],empty:!1},t),get:(e,t)=>r({method:"GET",path:`/api/shell/${encodeURIComponent(e.id)}`,query:{location:e.location},successStatus:200,declaredStatuses:[404,401,400],empty:!1},t),timeout:(e,t)=>r({method:"PATCH",path:`/api/shell/${encodeURIComponent(e.id)}/timeout`,query:{location:e.location},body:{timeout:e.timeout},successStatus:200,declaredStatuses:[404,401,400],empty:!1},t),output:(e,t)=>r({method:"GET",path:`/api/shell/${encodeURIComponent(e.id)}/output`,query:{location:e.location,cursor:e.cursor,limit:e.limit},successStatus:200,declaredStatuses:[404,401,400],empty:!1},t),remove:(e,t)=>r({method:"DELETE",path:`/api/shell/${encodeURIComponent(e.id)}`,query:{location:e.location},successStatus:204,declaredStatuses:[404,401,400],empty:!0},t)},question:{request:{list:(e,t)=>r({method:"GET",path:"/api/question/request",query:{location:e?.location},successStatus:200,declaredStatuses:[401,400],empty:!1},t)},list:(e,t)=>r({method:"GET",path:`/api/session/${encodeURIComponent(e.sessionID)}/question`,successStatus:200,declaredStatuses:[404,400,401],empty:!1},t).then((o)=>o.data),reply:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/question/${encodeURIComponent(e.requestID)}/reply`,body:{answers:e.answers},successStatus:204,declaredStatuses:[404,400,401],empty:!0},t),reject:(e,t)=>r({method:"POST",path:`/api/session/${encodeURIComponent(e.sessionID)}/question/${encodeURIComponent(e.requestID)}/reject`,successStatus:204,declaredStatuses:[404,400,401],empty:!0},t)},reference:{list:(e,t)=>r({method:"GET",path:"/api/reference",query:{location:e?.location},successStatus:200,declaredStatuses:[401,400],empty:!1},t)},projectCopy:{create:(e,t)=>r({method:"POST",path:`/experimental/project/${encodeURIComponent(e.projectID)}/copy`,query:{location:e.location},body:{strategy:e.strategy,directory:e.directory,name:e.name},successStatus:200,declaredStatuses:[400,401],empty:!1},t),remove:(e,t)=>r({method:"DELETE",path:`/experimental/project/${encodeURIComponent(e.projectID)}/copy`,query:{location:e.location},body:{directory:e.directory,force:e.force},successStatus:204,declaredStatuses:[400,401],empty:!0},t),refresh:(e,t)=>r({method:"POST",path:`/experimental/project/${encodeURIComponent(e.projectID)}/copy/refresh`,query:{location:e.location},successStatus:204,declaredStatuses:[400,401],empty:!0},t)},vcs:{get:(e,t)=>r({method:"GET",path:"/api/vcs",query:{location:e?.location},successStatus:200,declaredStatuses:[401,400],empty:!1},t),status:(e,t)=>r({method:"GET",path:"/api/vcs/status",query:{location:e?.location},successStatus:200,declaredStatuses:[401,400],empty:!1},t),diff:(e,t)=>r({method:"GET",path:"/api/vcs/diff",query:{location:e.location,mode:e.mode,context:e.context},successStatus:200,declaredStatuses:[401,400],empty:!1},t)},debug:{location:{list:(e)=>r({method:"GET",path:"/api/debug/location",successStatus:200,declaredStatuses:[401,400],empty:!1},e),evict:(e,t)=>r({method:"DELETE",path:"/api/debug/location",query:{location:e?.location},successStatus:204,declaredStatuses:[401,400],empty:!0},t)}},websearch:{providers:(e,t)=>r({method:"GET",path:"/api/websearch/provider",query:{location:e?.location},successStatus:200,declaredStatuses:[503,401,400],empty:!1},t),query:(e,t)=>r({method:"POST",path:"/api/websearch",query:{location:e.location},body:{query:e.query,providerID:e.providerID},successStatus:200,declaredStatuses:[400,503,401],empty:!1},t)}}}function A(n){return n.split("/").map(encodeURIComponent).join("/")}function f(n,s,i){if(i===void 0)return;if(i===null){n.append(s,"null");return}if(Array.isArray(i)){for(let u of i)f(n,s,u);return}if(typeof i==="object"){for(let[u,g]of Object.entries(i))f(n,`${s}[${u}]`,g);return}n.append(s,String(i))}async function h(n){if(!D(n,"application/json")&&!n.headers.get("content-type")?.includes("+json")){try{await n.body?.cancel()}catch{}throw new d("UnsupportedContentType")}let s;try{s=await n.text()}catch(i){throw new d("Transport",{cause:i})}if(s==="")throw new d("MalformedResponse");try{return JSON.parse(s)}catch(i){throw new d("MalformedResponse",{cause:i})}}function D(n,s){return n.headers.get("content-type")?.split(";",1)[0]?.trim().toLowerCase()===s}var L=(n)=>typeof n==="object"&&n!==null&&("_tag"in n)&&n._tag==="UnauthorizedError",N=(n)=>typeof n==="object"&&n!==null&&("_tag"in n)&&n._tag==="InvalidRequestError",M=(n)=>typeof n==="object"&&n!==null&&("_tag"in n)&&n._tag==="AgentNotFoundError",T=(n)=>typeof n==="object"&&n!==null&&("_tag"in n)&&n._tag==="InvalidCursorError",U=(n)=>typeof n==="object"&&n!==null&&("_tag"in n)&&n._tag==="SessionNotFoundError",j=(n)=>typeof n==="object"&&n!==null&&("_tag"in n)&&n._tag==="MessageNotFoundError",V=(n)=>typeof n==="object"&&n!==null&&("_tag"in n)&&n._tag==="ConflictError",G=(n)=>typeof n==="object"&&n!==null&&("_tag"in n)&&n._tag==="CommandNotFoundError",$=(n)=>typeof n==="object"&&n!==null&&("_tag"in n)&&n._tag==="CommandEvaluationError",_=(n)=>typeof n==="object"&&n!==null&&("_tag"in n)&&n._tag==="SkillNotFoundError",J=(n)=>typeof n==="object"&&n!==null&&("_tag"in n)&&n._tag==="ServiceUnavailableError",Q=(n)=>typeof n==="object"&&n!==null&&("_tag"in n)&&n._tag==="SessionBusyError",W=(n)=>typeof n==="object"&&n!==null&&("_tag"in n)&&n._tag==="UnknownError",B=(n)=>typeof n==="object"&&n!==null&&("_tag"in n)&&n._tag==="InstructionEntryValueTooLargeError",z=(n)=>typeof n==="object"&&n!==null&&("_tag"in n)&&n._tag==="ProviderNotFoundError",H=(n)=>typeof n==="object"&&n!==null&&("_tag"in n)&&n._tag==="McpServerNotFoundError",K=(n)=>typeof n==="object"&&n!==null&&("_tag"in n)&&n._tag==="FormNotFoundError",X=(n)=>typeof n==="object"&&n!==null&&("_tag"in n)&&n._tag==="FormAlreadySettledError",Y=(n)=>typeof n==="object"&&n!==null&&("_tag"in n)&&n._tag==="FormInvalidAnswerError",Z=(n)=>typeof n==="object"&&n!==null&&("_tag"in n)&&n._tag==="PermissionNotFoundError",ee=(n)=>typeof n==="object"&&n!==null&&("_tag"in n)&&n._tag==="PtyNotFoundError",te=(n)=>typeof n==="object"&&n!==null&&("_tag"in n)&&n._tag==="ShellNotFoundError",ne=(n)=>typeof n==="object"&&n!==null&&("_tag"in n)&&n._tag==="QuestionNotFoundError",re=(n)=>typeof n==="object"&&n!==null&&("name"in n)&&n.name==="ProjectCopyError";export{W as isUnknownError,L as isUnauthorizedError,_ as isSkillNotFoundError,te as isShellNotFoundError,U as isSessionNotFoundError,Q as isSessionBusyError,J as isServiceUnavailableError,ne as isQuestionNotFoundError,ee as isPtyNotFoundError,z as isProviderNotFoundError,re as isProjectCopyError,Z as isPermissionNotFoundError,j as isMessageNotFoundError,H as isMcpServerNotFoundError,N as isInvalidRequestError,T as isInvalidCursorError,B as isInstructionEntryValueTooLargeError,K as isFormNotFoundError,Y as isFormInvalidAnswerError,X as isFormAlreadySettledError,V as isConflictError,G as isCommandNotFoundError,$ as isCommandEvaluationError,M as isAgentNotFoundError,R as OpenCode,d as ClientError};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { DiscoverOptions, Endpoint, EnsureOptions, StopOptions } from "../service.js";
|
|
2
|
+
export * from "../service.js";
|
|
3
|
+
/** Discover a healthy, compatible local service without starting one. */
|
|
4
|
+
export declare function discover(options?: DiscoverOptions): Promise<Endpoint | undefined>;
|
|
5
|
+
/** Ensure a healthy, compatible local service is running. */
|
|
6
|
+
export declare function ensure(options?: EnsureOptions): Promise<Endpoint>;
|
|
7
|
+
/** Stop the registered local service. */
|
|
8
|
+
export declare function stop(options?: StopOptions): Promise<void>;
|
|
9
|
+
/** Create HTTP authentication headers for a service endpoint. */
|
|
10
|
+
export declare function headers(endpoint: Endpoint): {
|
|
11
|
+
authorization: string;
|
|
12
|
+
} | undefined;
|
|
13
|
+
/** Promise-based local service lifecycle operations. */
|
|
14
|
+
export declare const Service: {
|
|
15
|
+
discover: typeof discover;
|
|
16
|
+
ensure: typeof ensure;
|
|
17
|
+
stop: typeof stop;
|
|
18
|
+
headers: typeof headers;
|
|
19
|
+
};
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
export * from "../service.js";
|
|
6
|
+
/** Discover a healthy, compatible local service without starting one. */
|
|
7
|
+
export async function discover(options = {}) {
|
|
8
|
+
return (await discoverLocal(options))?.endpoint;
|
|
9
|
+
}
|
|
10
|
+
async function discoverLocal(options) {
|
|
11
|
+
const found = (await registered(options.file)).service;
|
|
12
|
+
if (found?.state !== "ready")
|
|
13
|
+
return undefined;
|
|
14
|
+
if (options.version !== undefined && found.version !== options.version)
|
|
15
|
+
return undefined;
|
|
16
|
+
return found;
|
|
17
|
+
}
|
|
18
|
+
/** Ensure a healthy, compatible local service is running. */
|
|
19
|
+
export async function ensure(options = {}) {
|
|
20
|
+
const deadline = Date.now() + 120_000;
|
|
21
|
+
const contenders = new Set();
|
|
22
|
+
let announced = false;
|
|
23
|
+
let lastSpawn = 0;
|
|
24
|
+
let spawnDelay = 5_000;
|
|
25
|
+
let ownerHeld = false;
|
|
26
|
+
const announce = (reason, previousVersion) => {
|
|
27
|
+
if (announced)
|
|
28
|
+
return;
|
|
29
|
+
announced = true;
|
|
30
|
+
options.onStart?.(reason, previousVersion);
|
|
31
|
+
};
|
|
32
|
+
const spawnContender = () => {
|
|
33
|
+
const [command, ...args] = options.command ?? ["opencode", "serve", "--service"];
|
|
34
|
+
if (command === undefined)
|
|
35
|
+
throw new Error("Missing service command");
|
|
36
|
+
try {
|
|
37
|
+
const child = spawn(command, args, { detached: true, stdio: "ignore" });
|
|
38
|
+
let error;
|
|
39
|
+
child.once("error", (cause) => {
|
|
40
|
+
error = new Error("Failed to start server", { cause });
|
|
41
|
+
});
|
|
42
|
+
child.unref();
|
|
43
|
+
return { child, error: () => error };
|
|
44
|
+
}
|
|
45
|
+
catch (cause) {
|
|
46
|
+
throw new Error("Failed to start server", { cause });
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
while (true) {
|
|
50
|
+
if (Date.now() >= deadline)
|
|
51
|
+
throw new Error("Timed out waiting for the background service to start");
|
|
52
|
+
const registration = await registered(options.file, true);
|
|
53
|
+
if (registration.service !== undefined) {
|
|
54
|
+
ownerHeld = false;
|
|
55
|
+
spawnDelay = 5_000;
|
|
56
|
+
const service = registration.service;
|
|
57
|
+
const compatible = !service.legacy && (options.version === undefined || service.version === options.version);
|
|
58
|
+
if (compatible && service.state === "ready")
|
|
59
|
+
return service.endpoint;
|
|
60
|
+
if (compatible && service.state === "failed")
|
|
61
|
+
throw new Error("Background service failed to start");
|
|
62
|
+
if (!compatible) {
|
|
63
|
+
announce("version-mismatch", service.version);
|
|
64
|
+
await kill(service, options).catch(() => undefined);
|
|
65
|
+
lastSpawn = 0;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
if (lastSpawn === 0 && registration.info !== undefined)
|
|
70
|
+
lastSpawn = Date.now();
|
|
71
|
+
const failure = [...contenders].map(contenderFailure).find((error) => error !== undefined);
|
|
72
|
+
if (failure !== undefined)
|
|
73
|
+
throw failure;
|
|
74
|
+
const finished = [...contenders].filter(contenderFinished);
|
|
75
|
+
if (finished.some((item) => item.child.exitCode === 0)) {
|
|
76
|
+
ownerHeld = true;
|
|
77
|
+
spawnDelay = Math.min(spawnDelay * 2, 30_000);
|
|
78
|
+
}
|
|
79
|
+
finished.forEach((item) => contenders.delete(item));
|
|
80
|
+
// Keep one candidate plus one lock probe so a pre-lock stall cannot block recovery.
|
|
81
|
+
if (contenders.size < 2 && Date.now() - lastSpawn >= spawnDelay) {
|
|
82
|
+
announce("missing");
|
|
83
|
+
contenders.add(spawnContender());
|
|
84
|
+
lastSpawn = Date.now();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
await delay(1_000);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
function contenderFailure(contender) {
|
|
91
|
+
const error = contender.error();
|
|
92
|
+
if (error !== undefined)
|
|
93
|
+
return error;
|
|
94
|
+
if (contender.child.exitCode !== null && contender.child.exitCode !== 0)
|
|
95
|
+
return new Error(`Server process exited with code ${contender.child.exitCode}`);
|
|
96
|
+
if (contender.child.signalCode !== null)
|
|
97
|
+
return new Error(`Server process terminated by ${contender.child.signalCode}`);
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
function contenderFinished(contender) {
|
|
101
|
+
return contender.error() !== undefined || contender.child.exitCode !== null || contender.child.signalCode !== null;
|
|
102
|
+
}
|
|
103
|
+
/** Stop the registered local service. */
|
|
104
|
+
export async function stop(options = {}) {
|
|
105
|
+
const existing = await find(options);
|
|
106
|
+
if (existing !== undefined)
|
|
107
|
+
await kill(existing, options);
|
|
108
|
+
}
|
|
109
|
+
function fallback() {
|
|
110
|
+
return join(process.env["XDG_STATE_HOME"] ?? join(homedir(), ".local", "state"), "opencode", "service.json");
|
|
111
|
+
}
|
|
112
|
+
/** Create HTTP authentication headers for a service endpoint. */
|
|
113
|
+
export function headers(endpoint) {
|
|
114
|
+
if (endpoint.auth === undefined)
|
|
115
|
+
return undefined;
|
|
116
|
+
return {
|
|
117
|
+
authorization: "Basic " + Buffer.from(endpoint.auth.username + ":" + endpoint.auth.password).toString("base64"),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
async function read(file) {
|
|
121
|
+
const text = await readFile(file ?? fallback(), "utf8").catch(() => undefined);
|
|
122
|
+
if (text === undefined)
|
|
123
|
+
return undefined;
|
|
124
|
+
try {
|
|
125
|
+
return JSON.parse(text);
|
|
126
|
+
}
|
|
127
|
+
catch {
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
async function probe(info, allowLegacy = false) {
|
|
132
|
+
const endpoint = {
|
|
133
|
+
url: info.url,
|
|
134
|
+
auth: info.password === undefined
|
|
135
|
+
? undefined
|
|
136
|
+
: { type: "basic", username: "opencode", password: info.password },
|
|
137
|
+
};
|
|
138
|
+
const response = await fetch(new URL("/api/health", info.url), {
|
|
139
|
+
headers: headers(endpoint),
|
|
140
|
+
signal: AbortSignal.timeout(2_000),
|
|
141
|
+
}).catch(() => undefined);
|
|
142
|
+
const body = (await response?.json().catch(() => undefined));
|
|
143
|
+
if (body !== undefined && "version" in body && "pid" in body) {
|
|
144
|
+
if (body.pid !== info.pid)
|
|
145
|
+
return undefined;
|
|
146
|
+
if (info.version !== undefined && body.version !== info.version)
|
|
147
|
+
return undefined;
|
|
148
|
+
return {
|
|
149
|
+
info,
|
|
150
|
+
endpoint,
|
|
151
|
+
version: body.version,
|
|
152
|
+
state: response?.ok ? "ready" : response?.status === 500 ? "failed" : "waiting",
|
|
153
|
+
legacy: false,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
if (!allowLegacy || body?.healthy !== true)
|
|
157
|
+
return undefined;
|
|
158
|
+
return { info, endpoint, state: "ready", legacy: true };
|
|
159
|
+
}
|
|
160
|
+
async function registered(file, allowLegacy = false) {
|
|
161
|
+
const info = await read(file);
|
|
162
|
+
if (info === undefined)
|
|
163
|
+
return { info: undefined, service: undefined };
|
|
164
|
+
return { info, service: await probe(info, allowLegacy) };
|
|
165
|
+
}
|
|
166
|
+
async function find(options) {
|
|
167
|
+
return (await registered(options.file, true)).service;
|
|
168
|
+
}
|
|
169
|
+
function signal(pid, name) {
|
|
170
|
+
try {
|
|
171
|
+
process.kill(pid, name);
|
|
172
|
+
}
|
|
173
|
+
catch { }
|
|
174
|
+
}
|
|
175
|
+
function stopped(pid) {
|
|
176
|
+
try {
|
|
177
|
+
process.kill(pid, 0);
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
async function waitUntilStopped(pid) {
|
|
185
|
+
for (let attempt = 0; attempt <= 100; attempt++) {
|
|
186
|
+
if (stopped(pid))
|
|
187
|
+
return true;
|
|
188
|
+
if (attempt < 100)
|
|
189
|
+
await delay(50);
|
|
190
|
+
}
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
function same(left, right) {
|
|
194
|
+
return left.id === right.id && left.version === right.version && left.url === right.url && left.pid === right.pid;
|
|
195
|
+
}
|
|
196
|
+
async function kill(service, options) {
|
|
197
|
+
const requested = await requestStop(service);
|
|
198
|
+
if (requested === "rejected")
|
|
199
|
+
return;
|
|
200
|
+
if (requested === "unsupported") {
|
|
201
|
+
const current = await find(options);
|
|
202
|
+
if (current === undefined || !same(current.info, service.info))
|
|
203
|
+
return;
|
|
204
|
+
signal(service.info.pid, "SIGTERM");
|
|
205
|
+
}
|
|
206
|
+
if (await waitUntilStopped(service.info.pid))
|
|
207
|
+
return;
|
|
208
|
+
const latest = await find(options);
|
|
209
|
+
if (latest === undefined || !same(latest.info, service.info))
|
|
210
|
+
return;
|
|
211
|
+
signal(service.info.pid, "SIGKILL");
|
|
212
|
+
if (!(await waitUntilStopped(service.info.pid)))
|
|
213
|
+
throw new Error(`Server process ${service.info.pid} is still running`);
|
|
214
|
+
}
|
|
215
|
+
async function requestStop(service) {
|
|
216
|
+
if (service.info.id === undefined || service.legacy)
|
|
217
|
+
return "unsupported";
|
|
218
|
+
const response = await fetch(new URL("/api/service/stop", service.info.url), {
|
|
219
|
+
method: "POST",
|
|
220
|
+
headers: { ...headers(service.endpoint), "content-type": "application/json" },
|
|
221
|
+
body: JSON.stringify({ instanceID: service.info.id }),
|
|
222
|
+
signal: AbortSignal.timeout(2_000),
|
|
223
|
+
}).catch(() => undefined);
|
|
224
|
+
if (response === undefined || response.status === 404 || response.status === 405)
|
|
225
|
+
return "unsupported";
|
|
226
|
+
const body = (await response.json().catch(() => undefined));
|
|
227
|
+
if (!response.ok || body?.accepted !== true)
|
|
228
|
+
return "rejected";
|
|
229
|
+
return "accepted";
|
|
230
|
+
}
|
|
231
|
+
function delay(milliseconds) {
|
|
232
|
+
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
233
|
+
}
|
|
234
|
+
/** Promise-based local service lifecycle operations. */
|
|
235
|
+
export const Service = { discover, ensure, stop, headers };
|
package/package.json
CHANGED
|
@@ -1,9 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "shuvcode",
|
|
3
|
+
"type": "module",
|
|
3
4
|
"bin": {
|
|
4
5
|
"shuvcode": "./bin/launcher.mjs"
|
|
5
6
|
},
|
|
6
|
-
"
|
|
7
|
+
"files": [
|
|
8
|
+
"bin",
|
|
9
|
+
"client"
|
|
10
|
+
],
|
|
11
|
+
"exports": {
|
|
12
|
+
"./client": {
|
|
13
|
+
"types": "./client/index.d.ts",
|
|
14
|
+
"import": "./client/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"version": "2.0.0-alpha-9",
|
|
7
18
|
"license": "MIT",
|
|
8
19
|
"repository": {
|
|
9
20
|
"type": "git",
|
|
@@ -19,17 +30,17 @@
|
|
|
19
30
|
"x64"
|
|
20
31
|
],
|
|
21
32
|
"optionalDependencies": {
|
|
22
|
-
"shuvcode-
|
|
23
|
-
"shuvcode-linux-
|
|
24
|
-
"shuvcode-
|
|
25
|
-
"shuvcode-
|
|
26
|
-
"shuvcode-
|
|
27
|
-
"shuvcode-
|
|
28
|
-
"shuvcode-
|
|
29
|
-
"shuvcode-linux-x64": "2.0.0-alpha-
|
|
30
|
-
"shuvcode-
|
|
31
|
-
"shuvcode-
|
|
32
|
-
"shuvcode-
|
|
33
|
-
"shuvcode-
|
|
33
|
+
"shuvcode-darwin-x64-baseline": "2.0.0-alpha-9",
|
|
34
|
+
"shuvcode-linux-x64-baseline": "2.0.0-alpha-9",
|
|
35
|
+
"shuvcode-darwin-arm64": "2.0.0-alpha-9",
|
|
36
|
+
"shuvcode-windows-x64-baseline": "2.0.0-alpha-9",
|
|
37
|
+
"shuvcode-windows-x64": "2.0.0-alpha-9",
|
|
38
|
+
"shuvcode-linux-x64-baseline-musl": "2.0.0-alpha-9",
|
|
39
|
+
"shuvcode-darwin-x64": "2.0.0-alpha-9",
|
|
40
|
+
"shuvcode-linux-x64-musl": "2.0.0-alpha-9",
|
|
41
|
+
"shuvcode-linux-arm64": "2.0.0-alpha-9",
|
|
42
|
+
"shuvcode-linux-arm64-musl": "2.0.0-alpha-9",
|
|
43
|
+
"shuvcode-windows-arm64": "2.0.0-alpha-9",
|
|
44
|
+
"shuvcode-linux-x64": "2.0.0-alpha-9"
|
|
34
45
|
}
|
|
35
46
|
}
|