@sageion/node 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Sageion
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,8 @@
1
- # Temporary Holding Version
1
+ # @sageion/node
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The official Sageion SDK for Node.js backends.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @sageion/node
package/index.js ADDED
@@ -0,0 +1,10 @@
1
+ // backend/node/sageion-node/index.js
2
+ const { encryptToken } = require('./lib/crypto');
3
+ const { dispatchWebhook, dispatchWebhookAsync } = require('./lib/webhook');
4
+
5
+ console.log("does it run anyway in dev wihtout running npk run dev??")
6
+ module.exports = {
7
+ encryptToken,
8
+ dispatchWebhook,
9
+ dispatchWebhookAsync
10
+ };
package/lib/crypto.js ADDED
@@ -0,0 +1,53 @@
1
+ // backend/node/sageion-node/lib/crypto.js
2
+ const crypto = require('node:crypto');
3
+
4
+ /**
5
+ * Encrypts a plaintext JWT using Sageion's Public Key.
6
+ *
7
+ * @param {string} plaintextToken - The JWT generated by your backend.
8
+ * @param {string} publicKeyPem - Your Sageion Public Key (from .env or file).
9
+ * @returns {string} The encrypted token to send to Sageion.
10
+ */
11
+ function encryptToken(plaintextToken, publicKeyPem) {
12
+ if (!plaintextToken || typeof plaintextToken !== 'string') {
13
+ throw new Error('[Sageion SDK] plaintextToken must be a non-empty string');
14
+ }
15
+ if (!publicKeyPem) {
16
+ throw new Error('[Sageion SDK] publicKeyPem is required');
17
+ }
18
+
19
+ // Validate the PEM format early to give a helpful error
20
+ try {
21
+ crypto.createPublicKey(publicKeyPem);
22
+ } catch (err) {
23
+ throw new Error(`[Sageion SDK] Invalid Public Key PEM: ${err.message}`);
24
+ }
25
+
26
+ const aesKey = crypto.randomBytes(32);
27
+ const nonce = crypto.randomBytes(12);
28
+
29
+ const cipher = crypto.createCipheriv('aes-256-gcm', aesKey, nonce);
30
+ const ciphertext = Buffer.concat([
31
+ cipher.update(plaintextToken, 'utf8'),
32
+ cipher.final(),
33
+ ]);
34
+ const authTag = cipher.getAuthTag();
35
+ const ciphertextWithTag = Buffer.concat([ciphertext, authTag]);
36
+
37
+ const rsaWrappedKey = crypto.publicEncrypt(
38
+ {
39
+ key: publicKeyPem,
40
+ padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
41
+ oaepHash: 'sha256',
42
+ },
43
+ aesKey
44
+ );
45
+
46
+ return [
47
+ rsaWrappedKey.toString('base64'),
48
+ nonce.toString('base64'),
49
+ ciphertextWithTag.toString('base64'),
50
+ ].join('.');
51
+ }
52
+
53
+ module.exports = { encryptToken };
package/lib/webhook.js ADDED
@@ -0,0 +1,53 @@
1
+ // backend/node/sageion-node/lib/webhook.js
2
+
3
+ /**
4
+ * Dispatches a webhook back to Sageion.
5
+ * Uses native Node 18+ fetch.
6
+ *
7
+ * @param {string} correlationId - The ID passed in the request headers.
8
+ * @param {number} statusCode - 200 (success/pause), 201 (created), 400 (bad request).
9
+ * @param {object} data - The payload.
10
+ * @param {object} options - { region: 'us', webhookUrl: '...' }
11
+ */
12
+ async function dispatchWebhook(correlationId, statusCode, data = {}, options = {}) {
13
+ if (!correlationId) return; // Silent skip if no correlation ID
14
+
15
+ const region = (options.region || process.env.SAGEION_REGION || 'us').toLowerCase();
16
+ const url = options.webhookUrl || `https://${region}.autobot2.tezkit.com/dev/webhook/callback`;
17
+
18
+ const payload = {
19
+ correlation_id: correlationId,
20
+ data: { status_code: statusCode, ...data }
21
+ };
22
+
23
+ console.log(`\n[Sageion SDK] 🚀 Firing webhook to ${url}`);
24
+ console.log(`[Sageion SDK] 📦 correlation_id: ${correlationId}, status_code: ${statusCode}`);
25
+
26
+ try {
27
+ const response = await fetch(url, {
28
+ method: 'POST',
29
+ headers: { 'Content-Type': 'application/json' },
30
+ body: JSON.stringify(payload),
31
+ });
32
+
33
+ if (!response.ok) {
34
+ const text = await response.text();
35
+ console.error(`[Sageion SDK] ❌ Webhook failed: HTTP ${response.status} - ${text}`);
36
+ } else {
37
+ console.log(`[Sageion SDK] ✅ Webhook accepted (HTTP ${response.status})`);
38
+ }
39
+ } catch (error) {
40
+ console.error(`[Sageion SDK] ❌ Network error firing webhook:`, error.message);
41
+ }
42
+ }
43
+
44
+ /**
45
+ * Fire-and-forget wrapper for the webhook.
46
+ * Use this when you don't want to block your API response.
47
+ */
48
+ function dispatchWebhookAsync(correlationId, statusCode, data = {}, options = {}) {
49
+ if (!correlationId) return;
50
+ setTimeout(() => dispatchWebhook(correlationId, statusCode, data, options), 0);
51
+ }
52
+
53
+ module.exports = { dispatchWebhook, dispatchWebhookAsync };
package/package.json CHANGED
@@ -1,6 +1,29 @@
1
1
  {
2
2
  "name": "@sageion/node",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "Official Sageion SDK for Node.js backends. Handles Webhook dispatch and Token encryption.",
5
+ "main": "index.js",
6
+ "files": [
7
+ "index.js",
8
+ "lib/"
9
+ ],
10
+ "scripts": {
11
+ "test": "node --test"
12
+ },
13
+ "keywords": [
14
+ "sageion",
15
+ "ai",
16
+ "agent",
17
+ "webhook",
18
+ "auth",
19
+ "crypto"
20
+ ],
21
+ "author": "Sageion",
22
+ "license": "MIT",
23
+ "engines": {
24
+ "node": ">=18.0.0"
25
+ },
26
+ "publishConfig": {
27
+ "access": "public"
28
+ }
6
29
  }