@sna-sdk/core 0.6.0 → 0.6.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,172 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/node/index.ts
31
+ var node_exports = {};
32
+ __export(node_exports, {
33
+ startSnaServer: () => startSnaServer
34
+ });
35
+ module.exports = __toCommonJS(node_exports);
36
+
37
+ // ../../node_modules/.pnpm/tsup@8.5.1_jiti@2.6.1_postcss@8.5.8_tsx@4.21.0_typescript@5.9.3/node_modules/tsup/assets/cjs_shims.js
38
+ var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.tagName.toUpperCase() === "SCRIPT" ? document.currentScript.src : new URL("main.js", document.baseURI).href;
39
+ var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
40
+
41
+ // src/electron/index.ts
42
+ var import_child_process = require("child_process");
43
+ var import_url = require("url");
44
+ var import_fs = __toESM(require("fs"), 1);
45
+ var import_path = __toESM(require("path"), 1);
46
+ function resolveStandaloneScript() {
47
+ const selfPath = (0, import_url.fileURLToPath)(importMetaUrl);
48
+ let script = import_path.default.resolve(import_path.default.dirname(selfPath), "../server/standalone.js");
49
+ if (script.includes(".asar") && !script.includes(".asar.unpacked")) {
50
+ script = script.replace(/(\.asar)([/\\])/, ".asar.unpacked$2");
51
+ }
52
+ if (!import_fs.default.existsSync(script)) {
53
+ throw new Error(
54
+ `SNA standalone script not found: ${script}
55
+ Ensure "@sna-sdk/core" is listed in asarUnpack in your electron-builder config.`
56
+ );
57
+ }
58
+ return script;
59
+ }
60
+ function resolveNativeBinding(override) {
61
+ if (override) {
62
+ if (!import_fs.default.existsSync(override)) {
63
+ console.warn(`[sna] SNA nativeBinding override not found: ${override}`);
64
+ return void 0;
65
+ }
66
+ return override;
67
+ }
68
+ const BINDING_REL = import_path.default.join("better-sqlite3", "build", "Release", "better_sqlite3.node");
69
+ const resourcesPath = process.resourcesPath;
70
+ if (resourcesPath) {
71
+ const unpackedBase = import_path.default.join(resourcesPath, "app.asar.unpacked", "node_modules");
72
+ const candidates = [
73
+ import_path.default.join(unpackedBase, BINDING_REL),
74
+ // nested under @sna-sdk/core if hoisting differs
75
+ import_path.default.join(unpackedBase, "@sna-sdk", "core", "node_modules", BINDING_REL)
76
+ ];
77
+ for (const c of candidates) {
78
+ if (import_fs.default.existsSync(c)) return c;
79
+ }
80
+ }
81
+ const selfPath = (0, import_url.fileURLToPath)(importMetaUrl);
82
+ const local = import_path.default.resolve(import_path.default.dirname(selfPath), "../../node_modules", BINDING_REL);
83
+ if (import_fs.default.existsSync(local)) return local;
84
+ return void 0;
85
+ }
86
+ function buildNodePath() {
87
+ const resourcesPath = process.resourcesPath;
88
+ if (!resourcesPath) return void 0;
89
+ const unpacked = import_path.default.join(resourcesPath, "app.asar.unpacked", "node_modules");
90
+ if (!import_fs.default.existsSync(unpacked)) return void 0;
91
+ const existing = process.env.NODE_PATH;
92
+ return existing ? `${unpacked}${import_path.default.delimiter}${existing}` : unpacked;
93
+ }
94
+ async function startSnaServer(options) {
95
+ const port = options.port ?? 3099;
96
+ const cwd = options.cwd ?? import_path.default.dirname(options.dbPath);
97
+ const readyTimeout = options.readyTimeout ?? 15e3;
98
+ const { onLog } = options;
99
+ const standaloneScript = resolveStandaloneScript();
100
+ const nativeBinding = resolveNativeBinding(options.nativeBinding);
101
+ const nodePath = buildNodePath();
102
+ const env = {
103
+ ...process.env,
104
+ SNA_PORT: String(port),
105
+ SNA_DB_PATH: options.dbPath,
106
+ ...options.maxSessions != null ? { SNA_MAX_SESSIONS: String(options.maxSessions) } : {},
107
+ ...options.permissionMode ? { SNA_PERMISSION_MODE: options.permissionMode } : {},
108
+ ...options.model ? { SNA_MODEL: options.model } : {},
109
+ ...nativeBinding ? { SNA_SQLITE_NATIVE_BINDING: nativeBinding } : {},
110
+ ...nodePath ? { NODE_PATH: nodePath } : {},
111
+ // Consumer overrides last so they can always win
112
+ ...options.env ?? {}
113
+ };
114
+ const proc = (0, import_child_process.fork)(standaloneScript, [], {
115
+ cwd,
116
+ env,
117
+ stdio: "pipe"
118
+ });
119
+ let stdoutBuf = "";
120
+ let isReady = false;
121
+ const readyListeners = [];
122
+ proc.stdout?.on("data", (chunk) => {
123
+ stdoutBuf += chunk.toString();
124
+ const lines = stdoutBuf.split("\n");
125
+ stdoutBuf = lines.pop() ?? "";
126
+ for (const line of lines) {
127
+ if (onLog) onLog(line);
128
+ if (!isReady && line.includes("API server ready")) {
129
+ isReady = true;
130
+ readyListeners.splice(0).forEach((cb) => cb());
131
+ }
132
+ }
133
+ });
134
+ proc.stderr?.on("data", (chunk) => {
135
+ if (onLog) {
136
+ chunk.toString().split("\n").filter(Boolean).forEach(onLog);
137
+ }
138
+ });
139
+ await new Promise((resolve, reject) => {
140
+ if (isReady) return resolve();
141
+ const timer = setTimeout(() => {
142
+ reject(new Error(`SNA server did not become ready within ${readyTimeout}ms`));
143
+ }, readyTimeout);
144
+ readyListeners.push(() => {
145
+ clearTimeout(timer);
146
+ resolve();
147
+ });
148
+ proc.on("exit", (code) => {
149
+ if (!isReady) {
150
+ clearTimeout(timer);
151
+ reject(new Error(`SNA server process exited (code=${code ?? "null"}) before becoming ready`));
152
+ }
153
+ });
154
+ proc.on("error", (err) => {
155
+ if (!isReady) {
156
+ clearTimeout(timer);
157
+ reject(err);
158
+ }
159
+ });
160
+ });
161
+ return {
162
+ process: proc,
163
+ port,
164
+ stop() {
165
+ proc.kill("SIGTERM");
166
+ }
167
+ };
168
+ }
169
+ // Annotate the CommonJS export names for ESM import in node:
170
+ 0 && (module.exports = {
171
+ startSnaServer
172
+ });
@@ -0,0 +1,2 @@
1
+ export { SnaServerHandle, SnaServerOptions, startSnaServer } from '../electron/index.js';
2
+ import 'child_process';
@@ -0,0 +1,4 @@
1
+ import { startSnaServer } from "../electron/index.js";
2
+ export {
3
+ startSnaServer
4
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sna-sdk/core",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "Skills-Native Application runtime — server, providers, session management, database, and CLI",
5
5
  "type": "module",
6
6
  "bin": {
@@ -75,6 +75,13 @@
75
75
  "require": "./dist/electron/index.cjs",
76
76
  "import": "./dist/electron/index.js",
77
77
  "default": "./dist/electron/index.js"
78
+ },
79
+ "./node": {
80
+ "source": "./src/node/index.ts",
81
+ "types": "./dist/node/index.d.ts",
82
+ "require": "./dist/node/index.cjs",
83
+ "import": "./dist/node/index.js",
84
+ "default": "./dist/node/index.js"
78
85
  }
79
86
  },
80
87
  "engines": {