@rsconcept/rstool 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 +21 -0
- package/README.md +150 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +585 -0
- package/dist/index.js.map +1 -0
- package/dist/mappers/model-adapter.d.ts +27 -0
- package/dist/mappers/model-adapter.js +248 -0
- package/dist/mappers/model-adapter.js.map +1 -0
- package/dist/mappers/schema-adapter.d.ts +22 -0
- package/dist/mappers/schema-adapter.js +89 -0
- package/dist/mappers/schema-adapter.js.map +1 -0
- package/dist/mappers/types.d.ts +23 -0
- package/dist/mappers/types.js +22 -0
- package/dist/mappers/types.js.map +1 -0
- package/dist/models/analysis.d.ts +18 -0
- package/dist/models/analysis.js +1 -0
- package/dist/models/analysis.js.map +1 -0
- package/dist/models/common.d.ts +15 -0
- package/dist/models/common.js +12 -0
- package/dist/models/common.js.map +1 -0
- package/dist/models/constituenta.d.ts +38 -0
- package/dist/models/constituenta.js +1 -0
- package/dist/models/constituenta.js.map +1 -0
- package/dist/models/diagnostic.d.ts +17 -0
- package/dist/models/diagnostic.js +1 -0
- package/dist/models/diagnostic.js.map +1 -0
- package/dist/models/evaluation.d.ts +23 -0
- package/dist/models/evaluation.js +1 -0
- package/dist/models/evaluation.js.map +1 -0
- package/dist/models/index.d.ts +13 -0
- package/dist/models/index.js +491 -0
- package/dist/models/index.js.map +1 -0
- package/dist/models/model-value.d.ts +37 -0
- package/dist/models/model-value.js +1 -0
- package/dist/models/model-value.js.map +1 -0
- package/dist/models/rstool-agent.d.ts +36 -0
- package/dist/models/rstool-agent.js +480 -0
- package/dist/models/rstool-agent.js.map +1 -0
- package/dist/models/session.d.ts +29 -0
- package/dist/models/session.js +1 -0
- package/dist/models/session.js.map +1 -0
- package/dist/models/tool-contract.d.ts +33 -0
- package/dist/models/tool-contract.js +6 -0
- package/dist/models/tool-contract.js.map +1 -0
- package/dist/session/session-store.d.ts +26 -0
- package/dist/session/session-store.js +66 -0
- package/dist/session/session-store.js.map +1 -0
- package/dist/wrapper/client.d.ts +30 -0
- package/dist/wrapper/client.js +96 -0
- package/dist/wrapper/client.js.map +1 -0
- package/dist/wrapper/stdio-wrapper.d.ts +1 -0
- package/dist/wrapper/stdio-wrapper.js +679 -0
- package/dist/wrapper/stdio-wrapper.js.map +1 -0
- package/docs/CONSTITUENTA.md +55 -0
- package/docs/DIAGNOSTICS.md +125 -0
- package/docs/DOMAIN.md +89 -0
- package/docs/GRAMMAR-REF.md +98 -0
- package/docs/PORTAL-API.md +48 -0
- package/docs/README.md +15 -0
- package/docs/SYNTAX.md +139 -0
- package/docs/TYPIFICATION.md +79 -0
- package/package.json +76 -0
- package/skills/README.md +15 -0
- package/skills/rstool-helper/EXAMPLES.md +154 -0
- package/skills/rstool-helper/REFERENCE.md +169 -0
- package/skills/rstool-helper/SKILL.md +148 -0
- package/src/index.ts +43 -0
- package/src/mappers/model-adapter.ts +276 -0
- package/src/mappers/schema-adapter.ts +87 -0
- package/src/mappers/types.ts +35 -0
- package/src/models/analysis.ts +13 -0
- package/src/models/common.ts +17 -0
- package/src/models/constituenta.ts +35 -0
- package/src/models/diagnostic.ts +12 -0
- package/src/models/evaluation.ts +25 -0
- package/src/models/index.ts +33 -0
- package/src/models/model-value.ts +31 -0
- package/src/models/rstool-agent.test.ts +300 -0
- package/src/models/rstool-agent.ts +143 -0
- package/src/models/session.ts +22 -0
- package/src/models/tool-contract.ts +47 -0
- package/src/session/session-store.ts +81 -0
- package/src/wrapper/client.ts +116 -0
- package/src/wrapper/stdio-wrapper.ts +225 -0
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { spawn, type ChildProcessByStdio } from 'node:child_process';
|
|
2
|
+
import readline from 'node:readline';
|
|
3
|
+
import { type Readable, type Writable } from 'node:stream';
|
|
4
|
+
|
|
5
|
+
export interface WrapperResponse<T = unknown> {
|
|
6
|
+
id: string | number | null;
|
|
7
|
+
ok: boolean;
|
|
8
|
+
result?: T;
|
|
9
|
+
error?: {
|
|
10
|
+
code: string;
|
|
11
|
+
message: string;
|
|
12
|
+
details?: unknown;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface RSToolWrapperClientOptions {
|
|
17
|
+
command?: string;
|
|
18
|
+
args?: string[];
|
|
19
|
+
cwd?: string;
|
|
20
|
+
shell?: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export class RSToolWrapperClient {
|
|
24
|
+
private process: ChildProcessByStdio<Writable, Readable, null>;
|
|
25
|
+
private input: readline.Interface;
|
|
26
|
+
private pending = new Map<string, (value: WrapperResponse) => void>();
|
|
27
|
+
private requestCounter = 1;
|
|
28
|
+
|
|
29
|
+
public constructor(options: RSToolWrapperClientOptions = {}) {
|
|
30
|
+
this.process = spawn(options.command ?? 'npm', options.args ?? ['run', 'wrapper'], {
|
|
31
|
+
cwd: options.cwd ?? process.cwd(),
|
|
32
|
+
shell: options.shell ?? true,
|
|
33
|
+
stdio: ['pipe', 'pipe', 'inherit']
|
|
34
|
+
});
|
|
35
|
+
this.input = readline.createInterface({
|
|
36
|
+
input: this.process.stdout,
|
|
37
|
+
crlfDelay: Infinity
|
|
38
|
+
});
|
|
39
|
+
this.input.on('line', line => this.handleLine(line));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public async waitUntilReady(): Promise<void> {
|
|
43
|
+
for (;;) {
|
|
44
|
+
const line = await this.readOneEvent();
|
|
45
|
+
let response: WrapperResponse<{ ready: boolean }> | null = null;
|
|
46
|
+
try {
|
|
47
|
+
response = JSON.parse(line) as WrapperResponse<{ ready: boolean }>;
|
|
48
|
+
} catch {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (response.ok && response.result?.ready) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
public async call<T>(method: string, params: unknown = {}): Promise<T> {
|
|
58
|
+
const id = String(this.requestCounter++);
|
|
59
|
+
const payload = JSON.stringify({ id, method, params });
|
|
60
|
+
const responsePromise = new Promise<WrapperResponse>(resolve => {
|
|
61
|
+
this.pending.set(id, resolve);
|
|
62
|
+
});
|
|
63
|
+
this.process.stdin.write(`${payload}\n`);
|
|
64
|
+
const response = await responsePromise;
|
|
65
|
+
if (!response.ok) {
|
|
66
|
+
throw new Error(`${response.error?.code ?? 'UNKNOWN'}: ${response.error?.message ?? 'Request failed'}`);
|
|
67
|
+
}
|
|
68
|
+
return response.result as T;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public async close(): Promise<void> {
|
|
72
|
+
this.input.close();
|
|
73
|
+
this.process.stdin.end();
|
|
74
|
+
if (!this.process.killed) {
|
|
75
|
+
this.process.kill();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
private async readOneEvent(): Promise<string> {
|
|
80
|
+
return new Promise((resolve, reject) => {
|
|
81
|
+
const onLine = (line: string) => {
|
|
82
|
+
cleanup();
|
|
83
|
+
resolve(line);
|
|
84
|
+
};
|
|
85
|
+
const onExit = () => {
|
|
86
|
+
cleanup();
|
|
87
|
+
reject(new Error('Wrapper exited before ready'));
|
|
88
|
+
};
|
|
89
|
+
const cleanup = () => {
|
|
90
|
+
this.input.off('line', onLine);
|
|
91
|
+
this.process.off('exit', onExit);
|
|
92
|
+
};
|
|
93
|
+
this.input.on('line', onLine);
|
|
94
|
+
this.process.on('exit', onExit);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
private handleLine(line: string): void {
|
|
99
|
+
let parsed: WrapperResponse;
|
|
100
|
+
try {
|
|
101
|
+
parsed = JSON.parse(line) as WrapperResponse;
|
|
102
|
+
} catch {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (parsed.id === null || parsed.id === undefined) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const id = String(parsed.id);
|
|
109
|
+
const resolver = this.pending.get(id);
|
|
110
|
+
if (!resolver) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
this.pending.delete(id);
|
|
114
|
+
resolver(parsed);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import readline from 'node:readline';
|
|
3
|
+
|
|
4
|
+
import { RSToolAgent } from '../models/rstool-agent';
|
|
5
|
+
|
|
6
|
+
interface StdioRequest {
|
|
7
|
+
id: string | number;
|
|
8
|
+
method: string;
|
|
9
|
+
params?: unknown;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface StdioResponse {
|
|
13
|
+
id: string | number | null;
|
|
14
|
+
ok: boolean;
|
|
15
|
+
result?: unknown;
|
|
16
|
+
error?: {
|
|
17
|
+
code: string;
|
|
18
|
+
message: string;
|
|
19
|
+
details?: unknown;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const tool = new RSToolAgent();
|
|
24
|
+
|
|
25
|
+
const METHODS = [
|
|
26
|
+
'createSession',
|
|
27
|
+
'addOrUpdateConstituenta',
|
|
28
|
+
'analyzeExpression',
|
|
29
|
+
'getFormState',
|
|
30
|
+
'listDiagnostics',
|
|
31
|
+
'commitStep',
|
|
32
|
+
'exportSession',
|
|
33
|
+
'importSession',
|
|
34
|
+
'setConstituentaValue',
|
|
35
|
+
'setConstituentaValues',
|
|
36
|
+
'clearConstituentaValues',
|
|
37
|
+
'getModelState',
|
|
38
|
+
'evaluateExpression',
|
|
39
|
+
'evaluateConstituenta',
|
|
40
|
+
'recalculateModel'
|
|
41
|
+
] as const;
|
|
42
|
+
|
|
43
|
+
function writeResponse(response: StdioResponse): void {
|
|
44
|
+
if (!process.stdout.writable || process.stdout.destroyed || process.stdout.writableEnded) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
process.stdout.write(`${JSON.stringify(response)}\n`);
|
|
49
|
+
} catch {
|
|
50
|
+
// The client might have already closed stdout (EPIPE). Safe to ignore.
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function asObject(value: unknown): Record<string, unknown> {
|
|
55
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
56
|
+
return {};
|
|
57
|
+
}
|
|
58
|
+
return value as Record<string, unknown>;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function requiredString(input: Record<string, unknown>, key: string): string {
|
|
62
|
+
const value = input[key];
|
|
63
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
64
|
+
throw new Error(`Missing or invalid "${key}"`);
|
|
65
|
+
}
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async function handleRequest(request: StdioRequest): Promise<StdioResponse> {
|
|
70
|
+
try {
|
|
71
|
+
const params = asObject(request.params);
|
|
72
|
+
switch (request.method) {
|
|
73
|
+
case 'ping':
|
|
74
|
+
return { id: request.id, ok: true, result: { pong: true } };
|
|
75
|
+
case 'methods':
|
|
76
|
+
return { id: request.id, ok: true, result: METHODS };
|
|
77
|
+
case 'createSession':
|
|
78
|
+
return {
|
|
79
|
+
id: request.id,
|
|
80
|
+
ok: true,
|
|
81
|
+
result: tool.createSession(params.initial as never)
|
|
82
|
+
};
|
|
83
|
+
case 'addOrUpdateConstituenta':
|
|
84
|
+
return {
|
|
85
|
+
id: request.id,
|
|
86
|
+
ok: true,
|
|
87
|
+
result: tool.addOrUpdateConstituenta(requiredString(params, 'sessionId'), params.input as never)
|
|
88
|
+
};
|
|
89
|
+
case 'analyzeExpression':
|
|
90
|
+
return {
|
|
91
|
+
id: request.id,
|
|
92
|
+
ok: true,
|
|
93
|
+
result: tool.analyzeExpression(requiredString(params, 'sessionId'), params.input as never)
|
|
94
|
+
};
|
|
95
|
+
case 'getFormState':
|
|
96
|
+
return {
|
|
97
|
+
id: request.id,
|
|
98
|
+
ok: true,
|
|
99
|
+
result: tool.getFormState(requiredString(params, 'sessionId'))
|
|
100
|
+
};
|
|
101
|
+
case 'listDiagnostics':
|
|
102
|
+
return {
|
|
103
|
+
id: request.id,
|
|
104
|
+
ok: true,
|
|
105
|
+
result: tool.listDiagnostics(requiredString(params, 'sessionId'), params.filters as never)
|
|
106
|
+
};
|
|
107
|
+
case 'commitStep':
|
|
108
|
+
return {
|
|
109
|
+
id: request.id,
|
|
110
|
+
ok: true,
|
|
111
|
+
result: tool.commitStep(requiredString(params, 'sessionId'), params.message as string | undefined)
|
|
112
|
+
};
|
|
113
|
+
case 'exportSession':
|
|
114
|
+
return {
|
|
115
|
+
id: request.id,
|
|
116
|
+
ok: true,
|
|
117
|
+
result: tool.exportSession(requiredString(params, 'sessionId'))
|
|
118
|
+
};
|
|
119
|
+
case 'importSession':
|
|
120
|
+
return {
|
|
121
|
+
id: request.id,
|
|
122
|
+
ok: true,
|
|
123
|
+
result: tool.importSession(requiredString(params, 'payload'))
|
|
124
|
+
};
|
|
125
|
+
case 'setConstituentaValue':
|
|
126
|
+
return {
|
|
127
|
+
id: request.id,
|
|
128
|
+
ok: true,
|
|
129
|
+
result: await tool.setConstituentaValue(requiredString(params, 'sessionId'), params.input as never)
|
|
130
|
+
};
|
|
131
|
+
case 'setConstituentaValues':
|
|
132
|
+
return {
|
|
133
|
+
id: request.id,
|
|
134
|
+
ok: true,
|
|
135
|
+
result: await tool.setConstituentaValues(requiredString(params, 'sessionId'), params.input as never)
|
|
136
|
+
};
|
|
137
|
+
case 'clearConstituentaValues':
|
|
138
|
+
return {
|
|
139
|
+
id: request.id,
|
|
140
|
+
ok: true,
|
|
141
|
+
result: await tool.clearConstituentaValues(requiredString(params, 'sessionId'), params.input as never)
|
|
142
|
+
};
|
|
143
|
+
case 'getModelState':
|
|
144
|
+
return {
|
|
145
|
+
id: request.id,
|
|
146
|
+
ok: true,
|
|
147
|
+
result: tool.getModelState(requiredString(params, 'sessionId'))
|
|
148
|
+
};
|
|
149
|
+
case 'evaluateExpression':
|
|
150
|
+
return {
|
|
151
|
+
id: request.id,
|
|
152
|
+
ok: true,
|
|
153
|
+
result: tool.evaluateExpression(requiredString(params, 'sessionId'), params.input as never)
|
|
154
|
+
};
|
|
155
|
+
case 'evaluateConstituenta':
|
|
156
|
+
return {
|
|
157
|
+
id: request.id,
|
|
158
|
+
ok: true,
|
|
159
|
+
result: tool.evaluateConstituenta(requiredString(params, 'sessionId'), params.input as never)
|
|
160
|
+
};
|
|
161
|
+
case 'recalculateModel':
|
|
162
|
+
return {
|
|
163
|
+
id: request.id,
|
|
164
|
+
ok: true,
|
|
165
|
+
result: tool.recalculateModel(requiredString(params, 'sessionId'))
|
|
166
|
+
};
|
|
167
|
+
default:
|
|
168
|
+
return {
|
|
169
|
+
id: request.id ?? null,
|
|
170
|
+
ok: false,
|
|
171
|
+
error: {
|
|
172
|
+
code: 'METHOD_NOT_FOUND',
|
|
173
|
+
message: `Unknown method: ${request.method}`
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
} catch (error) {
|
|
178
|
+
return {
|
|
179
|
+
id: request.id ?? null,
|
|
180
|
+
ok: false,
|
|
181
|
+
error: {
|
|
182
|
+
code: 'INTERNAL_ERROR',
|
|
183
|
+
message: error instanceof Error ? error.message : 'Unknown error',
|
|
184
|
+
details: error
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const input = readline.createInterface({
|
|
191
|
+
input: process.stdin,
|
|
192
|
+
crlfDelay: Infinity
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
writeResponse({
|
|
196
|
+
id: null,
|
|
197
|
+
ok: true,
|
|
198
|
+
result: {
|
|
199
|
+
ready: true,
|
|
200
|
+
wrapper: 'rstool-stdio',
|
|
201
|
+
contractVersion: tool.contractVersion
|
|
202
|
+
}
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
input.on('line', line => {
|
|
206
|
+
if (!line.trim()) {
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
try {
|
|
210
|
+
const request = JSON.parse(line) as StdioRequest;
|
|
211
|
+
if (!('id' in request) || !('method' in request)) {
|
|
212
|
+
throw new Error('Request must include "id" and "method"');
|
|
213
|
+
}
|
|
214
|
+
void handleRequest(request).then(writeResponse);
|
|
215
|
+
} catch (error) {
|
|
216
|
+
writeResponse({
|
|
217
|
+
id: null,
|
|
218
|
+
ok: false,
|
|
219
|
+
error: {
|
|
220
|
+
code: 'BAD_REQUEST',
|
|
221
|
+
message: error instanceof Error ? error.message : 'Invalid JSON'
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
});
|