cvm-server 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.
Files changed (33) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +83 -0
  3. package/apps/cvm-server/src/config.d.ts +18 -0
  4. package/apps/cvm-server/src/config.d.ts.map +1 -0
  5. package/apps/cvm-server/src/config.js +73 -0
  6. package/apps/cvm-server/src/logger.d.ts +13 -0
  7. package/apps/cvm-server/src/logger.d.ts.map +1 -0
  8. package/apps/cvm-server/src/logger.js +77 -0
  9. package/apps/cvm-server/src/main.d.ts +2 -0
  10. package/apps/cvm-server/src/main.d.ts.map +1 -0
  11. package/apps/cvm-server/src/main.js +57 -0
  12. package/bin/cvm-server.js +17 -0
  13. package/main.js +44 -0
  14. package/package.json +94 -0
  15. package/packages/mcp-server/src/index.js +28 -0
  16. package/packages/mcp-server/src/lib/mcp-server.js +233 -0
  17. package/packages/mongodb/src/index.js +28 -0
  18. package/packages/mongodb/src/lib/mongodb.js +94 -0
  19. package/packages/mongodb/src/lib/types.js +16 -0
  20. package/packages/parser/src/index.js +26 -0
  21. package/packages/parser/src/lib/bytecode.js +42 -0
  22. package/packages/parser/src/lib/compiler.js +104 -0
  23. package/packages/parser/src/lib/parser.js +91 -0
  24. package/packages/storage/src/index.js +28 -0
  25. package/packages/storage/src/lib/file-adapter.js +113 -0
  26. package/packages/storage/src/lib/mongodb-adapter.js +94 -0
  27. package/packages/storage/src/lib/storage-factory.js +58 -0
  28. package/packages/storage/src/lib/storage.js +16 -0
  29. package/packages/types/src/index.js +22 -0
  30. package/packages/types/src/lib/types.js +16 -0
  31. package/packages/vm/src/index.js +24 -0
  32. package/packages/vm/src/lib/vm-manager.js +231 -0
  33. package/packages/vm/src/lib/vm.js +97 -0
@@ -0,0 +1,231 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var vm_manager_exports = {};
20
+ __export(vm_manager_exports, {
21
+ VMManager: () => VMManager
22
+ });
23
+ module.exports = __toCommonJS(vm_manager_exports);
24
+ var import_vm = require("./vm.js");
25
+ var import_parser = require("@cvm/parser");
26
+ var import_storage = require("@cvm/storage");
27
+ class VMManager {
28
+ vms = /* @__PURE__ */ new Map();
29
+ storage;
30
+ constructor(storageAdapter) {
31
+ if (storageAdapter) {
32
+ this.storage = storageAdapter;
33
+ } else {
34
+ this.storage = import_storage.StorageFactory.create();
35
+ }
36
+ }
37
+ /**
38
+ * Initialize the VMManager (connect to database)
39
+ */
40
+ async initialize() {
41
+ await this.storage.connect();
42
+ }
43
+ /**
44
+ * Cleanup resources
45
+ */
46
+ async dispose() {
47
+ await this.storage.disconnect();
48
+ this.vms.clear();
49
+ }
50
+ /**
51
+ * Load and compile a program from source code
52
+ */
53
+ async loadProgram(programId, source) {
54
+ const compileResult = (0, import_parser.compile)(source);
55
+ if (!compileResult.success) {
56
+ throw new Error(`Compilation failed: ${compileResult.errors.join(", ")}`);
57
+ }
58
+ const program = {
59
+ id: programId,
60
+ name: programId,
61
+ source,
62
+ bytecode: compileResult.bytecode,
63
+ // VM decides internal format
64
+ created: /* @__PURE__ */ new Date()
65
+ };
66
+ await this.storage.saveProgram(program);
67
+ }
68
+ /**
69
+ * Start execution of a loaded program
70
+ */
71
+ async startExecution(programId, executionId) {
72
+ const program = await this.storage.getProgram(programId);
73
+ if (!program) {
74
+ throw new Error(`Program not found: ${programId}`);
75
+ }
76
+ const execution = {
77
+ id: executionId,
78
+ programId,
79
+ state: "ready",
80
+ pc: 0,
81
+ stack: [],
82
+ variables: {},
83
+ output: [],
84
+ created: /* @__PURE__ */ new Date()
85
+ };
86
+ await this.storage.saveExecution(execution);
87
+ const vm = new import_vm.VM();
88
+ this.vms.set(executionId, vm);
89
+ }
90
+ /**
91
+ * Get next action from execution (Claude polls this)
92
+ * This is READ-ONLY - just returns current state
93
+ */
94
+ async getNext(executionId) {
95
+ const execution = await this.storage.getExecution(executionId);
96
+ if (!execution) {
97
+ throw new Error(`Execution not found: ${executionId}`);
98
+ }
99
+ if (execution.state === "ready") {
100
+ const program = await this.storage.getProgram(execution.programId);
101
+ if (!program) {
102
+ throw new Error(`Program not found: ${execution.programId}`);
103
+ }
104
+ let vm = this.vms.get(executionId);
105
+ if (!vm) {
106
+ vm = new import_vm.VM();
107
+ this.vms.set(executionId, vm);
108
+ }
109
+ const initialState = {
110
+ pc: 0,
111
+ stack: [],
112
+ variables: /* @__PURE__ */ new Map(),
113
+ output: []
114
+ };
115
+ const state = vm.execute(program.bytecode, initialState);
116
+ execution.pc = state.pc;
117
+ execution.stack = state.stack;
118
+ execution.variables = Object.fromEntries(state.variables);
119
+ execution.output = state.output;
120
+ if (state.status === "complete") {
121
+ execution.state = "completed";
122
+ await this.storage.saveExecution(execution);
123
+ this.vms.delete(executionId);
124
+ return {
125
+ type: "completed",
126
+ message: "Execution completed"
127
+ };
128
+ } else if (state.status === "waiting_cc") {
129
+ execution.state = "waiting_cc";
130
+ execution.ccPrompt = state.ccPrompt;
131
+ await this.storage.saveExecution(execution);
132
+ return {
133
+ type: "waiting",
134
+ message: state.ccPrompt || "Waiting for input"
135
+ };
136
+ } else if (state.status === "error") {
137
+ execution.state = "error";
138
+ execution.error = state.error;
139
+ await this.storage.saveExecution(execution);
140
+ this.vms.delete(executionId);
141
+ return {
142
+ type: "error",
143
+ error: state.error
144
+ };
145
+ }
146
+ }
147
+ if (execution.state === "completed") {
148
+ return {
149
+ type: "completed",
150
+ message: "Execution completed"
151
+ };
152
+ } else if (execution.state === "error") {
153
+ return {
154
+ type: "error",
155
+ error: execution.error || "Unknown error"
156
+ };
157
+ } else if (execution.state === "waiting_cc") {
158
+ return {
159
+ type: "waiting",
160
+ message: execution.ccPrompt || "Waiting for input"
161
+ };
162
+ }
163
+ throw new Error(`Unexpected execution state: ${execution.state}`);
164
+ }
165
+ /**
166
+ * Report result from cognitive operation and continue execution
167
+ */
168
+ async reportCCResult(executionId, result) {
169
+ const execution = await this.storage.getExecution(executionId);
170
+ if (!execution) {
171
+ throw new Error(`Execution not found: ${executionId}`);
172
+ }
173
+ const program = await this.storage.getProgram(execution.programId);
174
+ if (!program) {
175
+ throw new Error(`Program not found: ${execution.programId}`);
176
+ }
177
+ let vm = this.vms.get(executionId);
178
+ if (!vm) {
179
+ vm = new import_vm.VM();
180
+ this.vms.set(executionId, vm);
181
+ }
182
+ const currentState = {
183
+ pc: execution.pc,
184
+ stack: execution.stack,
185
+ variables: new Map(Object.entries(execution.variables)),
186
+ status: "waiting_cc",
187
+ output: execution.output,
188
+ ccPrompt: void 0
189
+ };
190
+ const newState = vm.resume(currentState, result, program.bytecode);
191
+ execution.pc = newState.pc;
192
+ execution.stack = newState.stack;
193
+ execution.variables = Object.fromEntries(newState.variables);
194
+ execution.output = newState.output;
195
+ if (newState.status === "complete") {
196
+ execution.state = "completed";
197
+ this.vms.delete(executionId);
198
+ } else if (newState.status === "error") {
199
+ execution.state = "error";
200
+ execution.error = newState.error;
201
+ this.vms.delete(executionId);
202
+ } else if (newState.status === "waiting_cc") {
203
+ execution.state = "waiting_cc";
204
+ execution.ccPrompt = newState.ccPrompt;
205
+ } else {
206
+ execution.state = "running";
207
+ }
208
+ await this.storage.saveExecution(execution);
209
+ }
210
+ /**
211
+ * Get current execution status
212
+ */
213
+ async getExecutionStatus(executionId) {
214
+ const execution = await this.storage.getExecution(executionId);
215
+ if (!execution) {
216
+ throw new Error(`Execution not found: ${executionId}`);
217
+ }
218
+ return {
219
+ id: execution.id,
220
+ state: execution.state,
221
+ pc: execution.pc,
222
+ stack: execution.stack,
223
+ variables: execution.variables,
224
+ output: execution.output
225
+ };
226
+ }
227
+ }
228
+ // Annotate the CommonJS export names for ESM import in node:
229
+ 0 && (module.exports = {
230
+ VMManager
231
+ });
@@ -0,0 +1,97 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var vm_exports = {};
20
+ __export(vm_exports, {
21
+ VM: () => VM
22
+ });
23
+ module.exports = __toCommonJS(vm_exports);
24
+ var import_parser = require("@cvm/parser");
25
+ class VM {
26
+ execute(bytecode, initialState) {
27
+ const state = {
28
+ pc: initialState?.pc ?? 0,
29
+ stack: initialState?.stack ?? [],
30
+ variables: initialState?.variables ?? /* @__PURE__ */ new Map(),
31
+ status: "running",
32
+ output: initialState?.output ?? [],
33
+ ...initialState
34
+ };
35
+ while (state.status === "running" && state.pc < bytecode.length) {
36
+ const instruction = bytecode[state.pc];
37
+ switch (instruction.op) {
38
+ case import_parser.OpCode.HALT:
39
+ state.status = "complete";
40
+ break;
41
+ case import_parser.OpCode.PUSH:
42
+ state.stack.push(instruction.arg);
43
+ state.pc++;
44
+ break;
45
+ case import_parser.OpCode.POP:
46
+ state.stack.pop();
47
+ state.pc++;
48
+ break;
49
+ case import_parser.OpCode.LOAD:
50
+ state.stack.push(state.variables.get(instruction.arg) ?? "");
51
+ state.pc++;
52
+ break;
53
+ case import_parser.OpCode.STORE:
54
+ const value = state.stack.pop();
55
+ state.variables.set(instruction.arg, value);
56
+ state.pc++;
57
+ break;
58
+ case import_parser.OpCode.CONCAT:
59
+ const b = state.stack.pop();
60
+ const a = state.stack.pop();
61
+ state.stack.push(a + b);
62
+ state.pc++;
63
+ break;
64
+ case import_parser.OpCode.PRINT:
65
+ const printValue = state.stack.pop();
66
+ state.output.push(printValue);
67
+ state.pc++;
68
+ break;
69
+ case import_parser.OpCode.CC:
70
+ state.ccPrompt = state.stack.pop();
71
+ state.status = "waiting_cc";
72
+ break;
73
+ default:
74
+ state.status = "error";
75
+ state.error = `Unknown opcode: ${instruction.op}`;
76
+ }
77
+ }
78
+ return state;
79
+ }
80
+ resume(state, ccResult, bytecode) {
81
+ if (state.status !== "waiting_cc") {
82
+ throw new Error("Cannot resume: VM not waiting for CC");
83
+ }
84
+ const newState = {
85
+ ...state,
86
+ stack: [...state.stack, ccResult],
87
+ status: "running",
88
+ ccPrompt: void 0,
89
+ pc: state.pc + 1
90
+ };
91
+ return this.execute(bytecode, newState);
92
+ }
93
+ }
94
+ // Annotate the CommonJS export names for ESM import in node:
95
+ 0 && (module.exports = {
96
+ VM
97
+ });