@garrix82/reactgenie-dsl 1.0.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 (153) hide show
  1. package/README.md +436 -0
  2. package/dist/__test__/dsl-descriptor.test.d.ts +1 -0
  3. package/dist/__test__/dsl-descriptor.test.js +27 -0
  4. package/dist/__test__/dsl-descriptor.test.js.map +1 -0
  5. package/dist/__test__/example_descriptor.d.ts +125 -0
  6. package/dist/__test__/example_descriptor.js +607 -0
  7. package/dist/__test__/example_descriptor.js.map +1 -0
  8. package/dist/__test__/food_descriptor.state.json +1 -0
  9. package/dist/__test__/food_descriptor.test.d.ts +74 -0
  10. package/dist/__test__/food_descriptor.test.js +205 -0
  11. package/dist/__test__/food_descriptor.test.js.map +1 -0
  12. package/dist/__test__/nl-interpreter-provider-selection.test.d.ts +1 -0
  13. package/dist/__test__/nl-interpreter-provider-selection.test.js +73 -0
  14. package/dist/__test__/nl-interpreter-provider-selection.test.js.map +1 -0
  15. package/dist/__test__/nl-interpreter.test.d.ts +1 -0
  16. package/dist/__test__/nl-interpreter.test.js +86 -0
  17. package/dist/__test__/nl-interpreter.test.js.map +1 -0
  18. package/dist/decorators/__test__/decorators.test.d.ts +1 -0
  19. package/dist/decorators/__test__/decorators.test.js +182 -0
  20. package/dist/decorators/__test__/decorators.test.js.map +1 -0
  21. package/dist/decorators/__test__/inheritance-descriptor.test.d.ts +1 -0
  22. package/dist/decorators/__test__/inheritance-descriptor.test.js +107 -0
  23. package/dist/decorators/__test__/inheritance-descriptor.test.js.map +1 -0
  24. package/dist/decorators/decorators.d.ts +20 -0
  25. package/dist/decorators/decorators.js +520 -0
  26. package/dist/decorators/decorators.js.map +1 -0
  27. package/dist/decorators/index.d.ts +2 -0
  28. package/dist/decorators/index.js +19 -0
  29. package/dist/decorators/index.js.map +1 -0
  30. package/dist/decorators/store.d.ts +12 -0
  31. package/dist/decorators/store.js +43 -0
  32. package/dist/decorators/store.js.map +1 -0
  33. package/dist/dsl/__test__/dsl-interpreter.test.d.ts +1 -0
  34. package/dist/dsl/__test__/dsl-interpreter.test.js +334 -0
  35. package/dist/dsl/__test__/dsl-interpreter.test.js.map +1 -0
  36. package/dist/dsl/__test__/parser.gen.test.d.ts +1 -0
  37. package/dist/dsl/__test__/parser.gen.test.js +283 -0
  38. package/dist/dsl/__test__/parser.gen.test.js.map +1 -0
  39. package/dist/dsl/dsl-interpreter.d.ts +66 -0
  40. package/dist/dsl/dsl-interpreter.js +767 -0
  41. package/dist/dsl/dsl-interpreter.js.map +1 -0
  42. package/dist/dsl/index.d.ts +1 -0
  43. package/dist/dsl/index.js +18 -0
  44. package/dist/dsl/index.js.map +1 -0
  45. package/dist/dsl/parser.gen.d.ts +10 -0
  46. package/dist/dsl/parser.gen.js +1524 -0
  47. package/dist/dsl/parser.gen.js.map +1 -0
  48. package/dist/dsl-descriptor.d.ts +75 -0
  49. package/dist/dsl-descriptor.js +151 -0
  50. package/dist/dsl-descriptor.js.map +1 -0
  51. package/dist/index.d.ts +5 -0
  52. package/dist/index.js +22 -0
  53. package/dist/index.js.map +1 -0
  54. package/dist/nl/__test__/context-aware-prompt.test.d.ts +1 -0
  55. package/dist/nl/__test__/context-aware-prompt.test.js +247 -0
  56. package/dist/nl/__test__/context-aware-prompt.test.js.map +1 -0
  57. package/dist/nl/__test__/context-selector.test.d.ts +1 -0
  58. package/dist/nl/__test__/context-selector.test.js +20 -0
  59. package/dist/nl/__test__/context-selector.test.js.map +1 -0
  60. package/dist/nl/__test__/nl-parser-groq-transport.test.d.ts +1 -0
  61. package/dist/nl/__test__/nl-parser-groq-transport.test.js +87 -0
  62. package/dist/nl/__test__/nl-parser-groq-transport.test.js.map +1 -0
  63. package/dist/nl/__test__/nl-parser-openai-parity.test.d.ts +1 -0
  64. package/dist/nl/__test__/nl-parser-openai-parity.test.js +206 -0
  65. package/dist/nl/__test__/nl-parser-openai-parity.test.js.map +1 -0
  66. package/dist/nl/__test__/nl-parser-openai-sampling.test.d.ts +1 -0
  67. package/dist/nl/__test__/nl-parser-openai-sampling.test.js +44 -0
  68. package/dist/nl/__test__/nl-parser-openai-sampling.test.js.map +1 -0
  69. package/dist/nl/__test__/nl-parser-openai-transport.test.d.ts +1 -0
  70. package/dist/nl/__test__/nl-parser-openai-transport.test.js +55 -0
  71. package/dist/nl/__test__/nl-parser-openai-transport.test.js.map +1 -0
  72. package/dist/nl/__test__/nl-parser-utils.test.d.ts +1 -0
  73. package/dist/nl/__test__/nl-parser-utils.test.js +70 -0
  74. package/dist/nl/__test__/nl-parser-utils.test.js.map +1 -0
  75. package/dist/nl/__test__/nl-parser.test.d.ts +1 -0
  76. package/dist/nl/__test__/nl-parser.test.js +64 -0
  77. package/dist/nl/__test__/nl-parser.test.js.map +1 -0
  78. package/dist/nl/__test__/parameter-tuning.test.d.ts +1 -0
  79. package/dist/nl/__test__/parameter-tuning.test.js +95 -0
  80. package/dist/nl/__test__/parameter-tuning.test.js.map +1 -0
  81. package/dist/nl/__test__/semantic-parsing-experiment.test.d.ts +1 -0
  82. package/dist/nl/__test__/semantic-parsing-experiment.test.js +178 -0
  83. package/dist/nl/__test__/semantic-parsing-experiment.test.js.map +1 -0
  84. package/dist/nl/context-selector.d.ts +17 -0
  85. package/dist/nl/context-selector.js +99 -0
  86. package/dist/nl/context-selector.js.map +1 -0
  87. package/dist/nl/index.d.ts +6 -0
  88. package/dist/nl/index.js +15 -0
  89. package/dist/nl/index.js.map +1 -0
  90. package/dist/nl/llm-monitoring.d.ts +49 -0
  91. package/dist/nl/llm-monitoring.js +243 -0
  92. package/dist/nl/llm-monitoring.js.map +1 -0
  93. package/dist/nl/llm-monitoring.test.d.ts +5 -0
  94. package/dist/nl/llm-monitoring.test.js +101 -0
  95. package/dist/nl/llm-monitoring.test.js.map +1 -0
  96. package/dist/nl/nl-parser-groq.d.ts +21 -0
  97. package/dist/nl/nl-parser-groq.js +357 -0
  98. package/dist/nl/nl-parser-groq.js.map +1 -0
  99. package/dist/nl/nl-parser-utils.d.ts +101 -0
  100. package/dist/nl/nl-parser-utils.js +255 -0
  101. package/dist/nl/nl-parser-utils.js.map +1 -0
  102. package/dist/nl/nl-parser.d.ts +30 -0
  103. package/dist/nl/nl-parser.js +433 -0
  104. package/dist/nl/nl-parser.js.map +1 -0
  105. package/dist/nl/prompt-gen.d.ts +93 -0
  106. package/dist/nl/prompt-gen.js +369 -0
  107. package/dist/nl/prompt-gen.js.map +1 -0
  108. package/dist/nl/prompt-res.d.ts +16 -0
  109. package/dist/nl/prompt-res.js +190 -0
  110. package/dist/nl/prompt-res.js.map +1 -0
  111. package/dist/nl-interpreter.d.ts +48 -0
  112. package/dist/nl-interpreter.js +155 -0
  113. package/dist/nl-interpreter.js.map +1 -0
  114. package/lib/__test__/dsl-descriptor.test.ts +27 -0
  115. package/lib/__test__/example_descriptor.ts +762 -0
  116. package/lib/__test__/food_descriptor.state.json +1 -0
  117. package/lib/__test__/food_descriptor.test.ts +331 -0
  118. package/lib/__test__/nl-interpreter-provider-selection.test.ts +126 -0
  119. package/lib/__test__/nl-interpreter.test.ts +129 -0
  120. package/lib/decorators/__test__/decorators.test.ts +177 -0
  121. package/lib/decorators/__test__/inheritance-descriptor.test.ts +92 -0
  122. package/lib/decorators/decorators.ts +754 -0
  123. package/lib/decorators/index.ts +2 -0
  124. package/lib/decorators/store.ts +47 -0
  125. package/lib/dsl/__test__/dsl-interpreter.test.ts +453 -0
  126. package/lib/dsl/__test__/parser.gen.test.ts +296 -0
  127. package/lib/dsl/dsl-interpreter.ts +974 -0
  128. package/lib/dsl/index.ts +1 -0
  129. package/lib/dsl/parser.gen.js +1479 -0
  130. package/lib/dsl/parser.pegjs +130 -0
  131. package/lib/dsl-descriptor.ts +241 -0
  132. package/lib/index.ts +5 -0
  133. package/lib/nl/__test__/context-aware-prompt.test.ts +372 -0
  134. package/lib/nl/__test__/context-selector.test.ts +27 -0
  135. package/lib/nl/__test__/nl-parser-groq-transport.test.ts +139 -0
  136. package/lib/nl/__test__/nl-parser-openai-parity.test.ts +381 -0
  137. package/lib/nl/__test__/nl-parser-openai-sampling.test.ts +73 -0
  138. package/lib/nl/__test__/nl-parser-openai-transport.test.ts +79 -0
  139. package/lib/nl/__test__/nl-parser-utils.test.ts +98 -0
  140. package/lib/nl/__test__/nl-parser.test.ts +119 -0
  141. package/lib/nl/__test__/parameter-tuning.test.ts +137 -0
  142. package/lib/nl/__test__/semantic-parsing-experiment.test.ts +260 -0
  143. package/lib/nl/context-selector.ts +123 -0
  144. package/lib/nl/index.ts +19 -0
  145. package/lib/nl/llm-monitoring.test.ts +136 -0
  146. package/lib/nl/llm-monitoring.ts +339 -0
  147. package/lib/nl/nl-parser-groq.ts +510 -0
  148. package/lib/nl/nl-parser-utils.ts +310 -0
  149. package/lib/nl/nl-parser.ts +616 -0
  150. package/lib/nl/prompt-gen.ts +607 -0
  151. package/lib/nl/prompt-res.ts +207 -0
  152. package/lib/nl-interpreter.ts +262 -0
  153. package/package.json +58 -0
@@ -0,0 +1,310 @@
1
+ import { AgentResponsePayload, PromptGen, ResponseSeverity } from "./prompt-gen";
2
+
3
+ export interface PromptWithBroadFallback extends PromptGen {
4
+ zero_shot_prompt_broad?: (user_utterance: string) => string;
5
+ }
6
+
7
+ export type CommandValidator = (command: string) => Promise<void>;
8
+
9
+ export interface SamplingParams {
10
+ temperature?: number;
11
+ top_p?: number;
12
+ frequency_penalty?: number;
13
+ presence_penalty?: number;
14
+ }
15
+
16
+ export const DEFAULT_SEMANTIC_PARSER_SAMPLING: Required<SamplingParams> = {
17
+ temperature: 0.2,
18
+ top_p: 1,
19
+ frequency_penalty: 0,
20
+ presence_penalty: 0,
21
+ };
22
+
23
+ const DSL_COMMAND_SCHEMA = {
24
+ name: "dsl_command",
25
+ schema: {
26
+ type: "object",
27
+ properties: {
28
+ command: {
29
+ type: "string",
30
+ description: "Single DSL command string to execute.",
31
+ },
32
+ },
33
+ required: ["command"],
34
+ additionalProperties: false,
35
+ },
36
+ } as const;
37
+
38
+ const AGENT_RESPONSE_SCHEMA = {
39
+ name: "agent_response",
40
+ schema: {
41
+ type: "object",
42
+ properties: {
43
+ minimalText: {
44
+ type: "string",
45
+ description: "Short, speech-friendly response.",
46
+ },
47
+ fullText: {
48
+ type: "string",
49
+ description: "More informative response for on-screen UI.",
50
+ },
51
+ type: {
52
+ type: "string",
53
+ enum: ["info", "error", "success", "warning"],
54
+ },
55
+ shouldSpeak: {
56
+ type: "boolean",
57
+ },
58
+ },
59
+ required: ["minimalText", "fullText", "type", "shouldSpeak"],
60
+ additionalProperties: false,
61
+ },
62
+ } as const;
63
+
64
+ export function getStructuredResponseFormat(strict: boolean) {
65
+ return {
66
+ type: "json_schema",
67
+ json_schema: {
68
+ name: DSL_COMMAND_SCHEMA.name,
69
+ strict,
70
+ schema: DSL_COMMAND_SCHEMA.schema,
71
+ },
72
+ } as const;
73
+ }
74
+
75
+ export function getStructuredResponseTextFormat(strict: boolean) {
76
+ return {
77
+ type: "json_schema",
78
+ name: DSL_COMMAND_SCHEMA.name,
79
+ strict,
80
+ schema: DSL_COMMAND_SCHEMA.schema,
81
+ } as const;
82
+ }
83
+
84
+ export function getStructuredAgentResponseFormat(strict: boolean) {
85
+ return {
86
+ type: "json_schema",
87
+ json_schema: {
88
+ name: AGENT_RESPONSE_SCHEMA.name,
89
+ strict,
90
+ schema: AGENT_RESPONSE_SCHEMA.schema,
91
+ },
92
+ } as const;
93
+ }
94
+
95
+ export function extractStructuredCommand(raw?: string | null): string | null {
96
+ if (!raw) return null;
97
+ try {
98
+ const parsed = JSON.parse(raw);
99
+ if (parsed && typeof parsed.command === "string") {
100
+ return parsed.command.trim();
101
+ }
102
+ } catch {
103
+ // ignore parse errors
104
+ }
105
+ return null;
106
+ }
107
+
108
+ export function extractResponseOutputText(response: any): string | null {
109
+ if (typeof response?.output_text === "string" && response.output_text.trim()) {
110
+ return response.output_text;
111
+ }
112
+
113
+ const output = Array.isArray(response?.output) ? response.output : [];
114
+ const parts: string[] = [];
115
+ for (const item of output) {
116
+ const content = Array.isArray(item?.content) ? item.content : [];
117
+ for (const part of content) {
118
+ if (typeof part?.text === "string" && part.text) {
119
+ parts.push(part.text);
120
+ } else if (typeof part?.output_text === "string" && part.output_text) {
121
+ parts.push(part.output_text);
122
+ }
123
+ }
124
+ }
125
+
126
+ return parts.length > 0 ? parts.join("") : null;
127
+ }
128
+
129
+ export function truncateText(value: string, maxLength: number): string {
130
+ if (value.length <= maxLength) {
131
+ return value;
132
+ }
133
+ return `${value.slice(0, Math.max(0, maxLength - 1)).trimEnd()}…`;
134
+ }
135
+
136
+ function normalizeMinimalText(value?: string): string {
137
+ const collapsed = (value || "Completed").replace(/\s+/g, " ").trim();
138
+ if (!collapsed) {
139
+ return "Completed";
140
+ }
141
+ const words = collapsed.split(" ").slice(0, 2);
142
+ return truncateText(words.join(" "), 24);
143
+ }
144
+
145
+ function normalizeSeverity(value: unknown): ResponseSeverity {
146
+ if (
147
+ value === "info" ||
148
+ value === "error" ||
149
+ value === "success" ||
150
+ value === "warning"
151
+ ) {
152
+ return value;
153
+ }
154
+ return "info";
155
+ }
156
+
157
+ export function normalizeAgentResponse(
158
+ payload: Partial<AgentResponsePayload>
159
+ ): AgentResponsePayload {
160
+ const minimal = normalizeMinimalText(payload.minimalText);
161
+ const full = truncateText(
162
+ (payload.fullText || payload.minimalText || "Completed").trim(),
163
+ 240
164
+ );
165
+ return {
166
+ minimalText: minimal || "Completed",
167
+ fullText: full || minimal || "Completed",
168
+ type: normalizeSeverity(payload.type),
169
+ shouldSpeak:
170
+ typeof payload.shouldSpeak === "boolean" ? payload.shouldSpeak : true,
171
+ };
172
+ }
173
+
174
+ export function extractStructuredAgentResponse(
175
+ raw?: string | null
176
+ ): AgentResponsePayload | null {
177
+ if (!raw) return null;
178
+ try {
179
+ const parsed = JSON.parse(raw);
180
+ if (!parsed || typeof parsed !== "object") {
181
+ return null;
182
+ }
183
+ return normalizeAgentResponse(parsed as Partial<AgentResponsePayload>);
184
+ } catch {
185
+ return null;
186
+ }
187
+ }
188
+
189
+ export function buildFallbackAgentResponse(
190
+ parsedDsl: string,
191
+ resultContext: string
192
+ ): AgentResponsePayload {
193
+ const lowResultContext = resultContext.toLowerCase();
194
+ const isError =
195
+ lowResultContext.includes("error") || lowResultContext.includes("failed");
196
+ const responseType: ResponseSeverity = isError ? "error" : "success";
197
+ const intent = parsedDsl.split(";")[0]?.trim() || "command";
198
+
199
+ if (isError) {
200
+ return {
201
+ minimalText: "Retry task",
202
+ fullText:
203
+ "I couldn't complete that request. Please try rephrasing or checking the target item.",
204
+ type: responseType,
205
+ shouldSpeak: true,
206
+ };
207
+ }
208
+
209
+ return {
210
+ minimalText: "Completed",
211
+ fullText: truncateText(`Done: ${intent}`, 240),
212
+ type: responseType,
213
+ shouldSpeak: true,
214
+ };
215
+ }
216
+
217
+ export function buildCommandRepairMessages(
218
+ userUtterance: string,
219
+ invalidCommand: string,
220
+ validationError: string,
221
+ lastUsedPrompt?: string
222
+ ) {
223
+ return [
224
+ {
225
+ role: "user",
226
+ content: lastUsedPrompt || `Original user utterance: ${userUtterance}`,
227
+ },
228
+ {
229
+ role: "assistant",
230
+ content: invalidCommand,
231
+ },
232
+ {
233
+ role: "user",
234
+ content:
235
+ "The previous DSL command is invalid.\n" +
236
+ `validation_error: ${validationError}\n` +
237
+ "Rewrite only the DSL command so it satisfies the original user utterance.\n" +
238
+ "Keep the original intent. Do not explain anything.",
239
+ },
240
+ ] as const;
241
+ }
242
+
243
+ export function buildCommandRepairInputText(
244
+ userUtterance: string,
245
+ invalidCommand: string,
246
+ validationError: string
247
+ ): string {
248
+ return (
249
+ "The previous DSL command is invalid.\n" +
250
+ `original_user_utterance: ${userUtterance}\n` +
251
+ `invalid_command: ${invalidCommand}\n` +
252
+ `validation_error: ${validationError}\n` +
253
+ "Rewrite only the DSL command so it satisfies the original user utterance.\n" +
254
+ "Keep the original intent. Do not explain anything."
255
+ );
256
+ }
257
+
258
+ export function normalizeBaseUrl(baseUrl: string): string {
259
+ return baseUrl.replace(/\/+$/, "");
260
+ }
261
+
262
+ export async function createJsonChatCompletion(params: {
263
+ endpoint: string;
264
+ apiKey: string;
265
+ body: Record<string, unknown>;
266
+ provider?: "openai" | "groq";
267
+ requestLabel: string;
268
+ invalidPayloadLabel: string;
269
+ }): Promise<any> {
270
+ const headers: Record<string, string> = {
271
+ "content-type": "application/json",
272
+ };
273
+ if (params.apiKey) {
274
+ headers.authorization = `Bearer ${params.apiKey}`;
275
+ }
276
+ if (params.provider) {
277
+ headers["x-genie-provider"] = params.provider;
278
+ }
279
+
280
+ const response = await fetch(params.endpoint, {
281
+ method: "POST",
282
+ headers,
283
+ body: JSON.stringify(params.body),
284
+ });
285
+
286
+ const text = await response.text();
287
+ let payload: any = null;
288
+ try {
289
+ payload = text ? JSON.parse(text) : null;
290
+ } catch {
291
+ payload = null;
292
+ }
293
+
294
+ if (!response.ok) {
295
+ const details = payload?.error
296
+ ? typeof payload.error === "string"
297
+ ? payload.error
298
+ : JSON.stringify(payload.error)
299
+ : text;
300
+ throw new Error(
301
+ `${params.requestLabel} (${response.status})${details ? `: ${details}` : ""}`
302
+ );
303
+ }
304
+
305
+ if (!payload || typeof payload !== "object") {
306
+ throw new Error(params.invalidPayloadLabel);
307
+ }
308
+
309
+ return payload;
310
+ }