@elevasis/sdk 0.2.0 → 0.3.1

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.
@@ -0,0 +1,174 @@
1
+ import { createServer } from 'http';
2
+
3
+ // src/server/index.ts
4
+ function readBody(req) {
5
+ return new Promise((resolve, reject) => {
6
+ const chunks = [];
7
+ req.on("data", (chunk) => chunks.push(chunk));
8
+ req.on("end", () => resolve(Buffer.concat(chunks).toString()));
9
+ req.on("error", reject);
10
+ });
11
+ }
12
+ function json(res, status, data) {
13
+ res.writeHead(status, { "Content-Type": "application/json" });
14
+ res.end(JSON.stringify(data));
15
+ }
16
+ function resolveNext(next, data) {
17
+ if (next === null) return null;
18
+ if (next.type === "linear") return next.target;
19
+ for (const route of next.routes) {
20
+ if (route.condition(data)) return route.target;
21
+ }
22
+ return next.default;
23
+ }
24
+ async function executeWorkflow(workflow, input, context) {
25
+ let currentData = workflow.contract.inputSchema.parse(input);
26
+ let stepId = workflow.entryPoint;
27
+ while (stepId !== null) {
28
+ if (context.signal?.aborted) {
29
+ throw new Error("Execution cancelled");
30
+ }
31
+ const step = workflow.steps[stepId];
32
+ if (!step) {
33
+ throw new Error(`Step '${stepId}' not found in workflow '${workflow.config.resourceId}'`);
34
+ }
35
+ const stepInput = step.inputSchema.parse(currentData);
36
+ const rawOutput = await step.handler(stepInput, context);
37
+ currentData = step.outputSchema.parse(rawOutput);
38
+ stepId = resolveNext(step.next, currentData);
39
+ }
40
+ if (workflow.contract.outputSchema) {
41
+ currentData = workflow.contract.outputSchema.parse(currentData);
42
+ }
43
+ return currentData;
44
+ }
45
+ function withLogCapture(fn) {
46
+ const logs = [];
47
+ const orig = { log: console.log, warn: console.warn, error: console.error };
48
+ const capture = (level, origFn) => (...args) => {
49
+ logs.push({ level, message: args.map(String).join(" "), timestamp: Date.now() });
50
+ origFn(...args);
51
+ };
52
+ console.log = capture("info", orig.log);
53
+ console.warn = capture("warn", orig.warn);
54
+ console.error = capture("error", orig.error);
55
+ return fn().then((result) => ({ result, logs })).finally(() => {
56
+ console.log = orig.log;
57
+ console.warn = orig.warn;
58
+ console.error = orig.error;
59
+ });
60
+ }
61
+ function startServer(org) {
62
+ const port = parseInt(process.env.PORT || "3000");
63
+ const workflows = new Map(
64
+ (org.workflows ?? []).map((w) => [w.config.resourceId, w])
65
+ );
66
+ const agents = new Map(
67
+ (org.agents ?? []).map((a) => [a.config.resourceId, a])
68
+ );
69
+ const controllers = /* @__PURE__ */ new Map();
70
+ const server = createServer(async (req, res) => {
71
+ try {
72
+ if (req.url === "/health" && req.method === "GET") {
73
+ res.writeHead(200).end("ok");
74
+ return;
75
+ }
76
+ if (req.url === "/manifest" && req.method === "GET") {
77
+ json(res, 200, {
78
+ workflows: (org.workflows ?? []).map((w) => ({
79
+ resourceId: w.config.resourceId,
80
+ name: w.config.name,
81
+ type: w.config.type,
82
+ status: w.config.status,
83
+ description: w.config.description,
84
+ version: w.config.version
85
+ })),
86
+ agents: (org.agents ?? []).map((a) => ({
87
+ resourceId: a.config.resourceId,
88
+ name: a.config.name,
89
+ type: a.config.type,
90
+ status: a.config.status,
91
+ description: a.config.description,
92
+ version: a.config.version
93
+ }))
94
+ });
95
+ return;
96
+ }
97
+ if (req.url === "/execute" && req.method === "POST") {
98
+ const body = JSON.parse(await readBody(req));
99
+ const { resourceId, executionId, input } = body;
100
+ const workflow = workflows.get(resourceId);
101
+ if (workflow) {
102
+ const controller = new AbortController();
103
+ controllers.set(executionId, controller);
104
+ const deadline = req.headers["x-elevasis-deadline"];
105
+ const timer = deadline ? setTimeout(() => controller.abort(), Number(deadline) - Date.now()) : void 0;
106
+ const context = {
107
+ executionId,
108
+ organizationId: "",
109
+ organizationName: "",
110
+ resourceId,
111
+ executionDepth: 0,
112
+ store: /* @__PURE__ */ new Map(),
113
+ signal: controller.signal,
114
+ logger: {
115
+ debug: (msg) => console.log(`[debug] ${msg}`),
116
+ info: (msg) => console.log(`[info] ${msg}`),
117
+ warn: (msg) => console.warn(`[warn] ${msg}`),
118
+ error: (msg) => console.error(`[error] ${msg}`)
119
+ }
120
+ };
121
+ try {
122
+ const { result: output, logs } = await withLogCapture(
123
+ () => executeWorkflow(workflow, input, context)
124
+ );
125
+ json(res, 200, { status: "completed", output, logs });
126
+ } catch (err) {
127
+ const { logs } = await withLogCapture(async () => {
128
+ });
129
+ json(res, 500, { status: "failed", error: String(err), logs });
130
+ } finally {
131
+ controllers.delete(executionId);
132
+ if (timer) clearTimeout(timer);
133
+ }
134
+ return;
135
+ }
136
+ const agent = agents.get(resourceId);
137
+ if (agent) {
138
+ json(res, 501, {
139
+ status: "failed",
140
+ error: `Agent execution is not supported in SDK server v0.3.0. Resource '${resourceId}' is an agent.`,
141
+ logs: []
142
+ });
143
+ return;
144
+ }
145
+ json(res, 404, { error: `Resource not found: ${resourceId}` });
146
+ return;
147
+ }
148
+ if (req.url === "/cancel" && req.method === "POST") {
149
+ const body = JSON.parse(await readBody(req));
150
+ const { executionId } = body;
151
+ const controller = controllers.get(executionId);
152
+ if (controller) {
153
+ controller.abort();
154
+ controllers.delete(executionId);
155
+ json(res, 200, { cancelled: true });
156
+ } else {
157
+ json(res, 404, { error: `No running execution: ${executionId}` });
158
+ }
159
+ return;
160
+ }
161
+ res.writeHead(404).end();
162
+ } catch (err) {
163
+ console.error("Unhandled server error:", err);
164
+ if (!res.headersSent) {
165
+ json(res, 500, { error: "Internal server error" });
166
+ }
167
+ }
168
+ });
169
+ server.listen(port, () => {
170
+ console.log(`Elevasis SDK server listening on port ${port}`);
171
+ });
172
+ }
173
+
174
+ export { startServer };
package/package.json CHANGED
@@ -1,32 +1,51 @@
1
1
  {
2
2
  "name": "@elevasis/sdk",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "SDK for building Elevasis organization resources",
5
+ "comment:bin": "IMPORTANT: This package shares the 'elevasis' binary name with @repo/cli. They never conflict because @elevasis/sdk must NEVER be added as a dependency of any workspace package (apps/*, packages/*, organizations/*). Workspace projects use @repo/cli for the 'elevasis' binary. External developers (outside the workspace) get this SDK's binary via npm install.",
5
6
  "type": "module",
7
+ "bin": {
8
+ "elevasis": "./dist/cli.cjs",
9
+ "elevasis-sdk": "./dist/cli.cjs"
10
+ },
6
11
  "exports": {
7
12
  ".": {
8
13
  "types": "./dist/index.d.ts",
9
14
  "import": "./dist/index.js"
15
+ },
16
+ "./server": {
17
+ "import": "./dist/server/index.js"
10
18
  }
11
19
  },
12
20
  "files": [
13
21
  "dist/index.js",
14
- "dist/index.d.ts"
22
+ "dist/index.d.ts",
23
+ "dist/server/index.js",
24
+ "dist/cli.cjs"
15
25
  ],
26
+ "scripts": {
27
+ "build": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.core-dts.json && tsc -p tsconfig.build.json && tsup && rollup -c rollup.dts.config.mjs && esbuild src/cli/index.ts --bundle --platform=node --outfile=dist/cli.cjs --format=cjs --external:esbuild --external:jiti --banner:js=\"#!/usr/bin/env node\"",
28
+ "check-types": "tsc --noEmit"
29
+ },
30
+ "dependencies": {
31
+ "esbuild": "^0.25.0",
32
+ "jiti": "^2.0.0"
33
+ },
16
34
  "peerDependencies": {
17
35
  "zod": "^4.1.0"
18
36
  },
19
37
  "devDependencies": {
38
+ "@repo/core": "workspace:*",
39
+ "@repo/typescript-config": "workspace:*",
40
+ "@types/node": "^22.0.0",
41
+ "chalk": "^5.3.0",
42
+ "commander": "^11.0.0",
43
+ "dotenv": "^16.0.0",
44
+ "ora": "^7.0.1",
20
45
  "rollup": "^4.59.0",
21
46
  "rollup-plugin-dts": "^6.3.0",
22
47
  "tsup": "^8.0.0",
23
48
  "typescript": "5.9.2",
24
- "zod": "^4.1.0",
25
- "@repo/core": "0.0.0",
26
- "@repo/typescript-config": "0.0.0"
27
- },
28
- "scripts": {
29
- "build": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.core-dts.json && tsc -p tsconfig.build.json && tsup && rollup -c rollup.dts.config.mjs",
30
- "check-types": "tsc --noEmit"
49
+ "zod": "^4.1.0"
31
50
  }
32
- }
51
+ }