@qoretechnologies/reqraft 0.10.53 → 0.10.54-pr.115.ge2d364c

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 (215) hide show
  1. package/.claude/CLAUDE.md +25 -0
  2. package/.tasks/INDEX.md +1 -0
  3. package/.tasks/STORY_NETWORK_ISOLATION.md +98 -0
  4. package/README.md +1 -1
  5. package/design/FORM_ENGINE_EXPRESSIONS.md +110 -0
  6. package/design/RENDER_EXPRESSION_TRANSPORT.md +18 -0
  7. package/dist/components/dpqlEditor/dpqlProbe.d.ts +37 -0
  8. package/dist/components/dpqlEditor/dpqlProbe.d.ts.map +1 -0
  9. package/dist/components/dpqlEditor/dpqlProbe.js +203 -0
  10. package/dist/components/dpqlEditor/dpqlProbe.js.map +1 -0
  11. package/dist/components/dpqlEditor/index.d.ts +4 -0
  12. package/dist/components/dpqlEditor/index.d.ts.map +1 -1
  13. package/dist/components/dpqlEditor/index.js +5 -1
  14. package/dist/components/dpqlEditor/index.js.map +1 -1
  15. package/dist/components/dpqlEditor/types.d.ts +29 -2
  16. package/dist/components/dpqlEditor/types.d.ts.map +1 -1
  17. package/dist/components/dpqlEditor/useDpqlProbe.d.ts +4 -0
  18. package/dist/components/dpqlEditor/useDpqlProbe.d.ts.map +1 -0
  19. package/dist/components/dpqlEditor/useDpqlProbe.js +29 -0
  20. package/dist/components/dpqlEditor/useDpqlProbe.js.map +1 -0
  21. package/dist/components/dpqlEditor/useDpqlSession.d.ts +5 -2
  22. package/dist/components/dpqlEditor/useDpqlSession.d.ts.map +1 -1
  23. package/dist/components/dpqlEditor/useDpqlSession.js +8 -2
  24. package/dist/components/dpqlEditor/useDpqlSession.js.map +1 -1
  25. package/dist/components/form/engine/CompactRow.d.ts.map +1 -1
  26. package/dist/components/form/engine/CompactRow.js +186 -21
  27. package/dist/components/form/engine/CompactRow.js.map +1 -1
  28. package/dist/components/form/engine/CompactToolbar.d.ts.map +1 -1
  29. package/dist/components/form/engine/CompactToolbar.js +5 -2
  30. package/dist/components/form/engine/CompactToolbar.js.map +1 -1
  31. package/dist/components/form/engine/FormEngine.d.ts +159 -11
  32. package/dist/components/form/engine/FormEngine.d.ts.map +1 -1
  33. package/dist/components/form/engine/FormEngine.js +376 -75
  34. package/dist/components/form/engine/FormEngine.js.map +1 -1
  35. package/dist/components/form/engine/FormFieldsSkeleton.d.ts +48 -0
  36. package/dist/components/form/engine/FormFieldsSkeleton.d.ts.map +1 -0
  37. package/dist/components/form/engine/FormFieldsSkeleton.js +38 -0
  38. package/dist/components/form/engine/FormFieldsSkeleton.js.map +1 -0
  39. package/dist/components/form/engine/OptionFieldMessages.d.ts +17 -2
  40. package/dist/components/form/engine/OptionFieldMessages.d.ts.map +1 -1
  41. package/dist/components/form/engine/OptionFieldMessages.js +61 -11
  42. package/dist/components/form/engine/OptionFieldMessages.js.map +1 -1
  43. package/dist/components/form/engine/_structuredData/SchemaDataView.d.ts.map +1 -1
  44. package/dist/components/form/engine/_structuredData/SchemaDataView.js +7 -1
  45. package/dist/components/form/engine/_structuredData/SchemaDataView.js.map +1 -1
  46. package/dist/components/form/engine/compactToolbarContext.d.ts +1 -0
  47. package/dist/components/form/engine/compactToolbarContext.d.ts.map +1 -1
  48. package/dist/components/form/engine/readFirst.d.ts +42 -1
  49. package/dist/components/form/engine/readFirst.d.ts.map +1 -1
  50. package/dist/components/form/engine/readFirst.js +74 -7
  51. package/dist/components/form/engine/readFirst.js.map +1 -1
  52. package/dist/components/form/engine/rendererTypes.d.ts +13 -0
  53. package/dist/components/form/engine/rendererTypes.d.ts.map +1 -1
  54. package/dist/components/form/engine/rendererTypes.js.map +1 -1
  55. package/dist/components/form/engine/rowMenuContext.d.ts +78 -0
  56. package/dist/components/form/engine/rowMenuContext.d.ts.map +1 -0
  57. package/dist/components/form/engine/rowMenuContext.js +103 -0
  58. package/dist/components/form/engine/rowMenuContext.js.map +1 -0
  59. package/dist/components/form/engine/typeChoices.d.ts +37 -0
  60. package/dist/components/form/engine/typeChoices.d.ts.map +1 -0
  61. package/dist/components/form/engine/typeChoices.js +25 -0
  62. package/dist/components/form/engine/typeChoices.js.map +1 -0
  63. package/dist/components/form/expressions/ExpressionField.d.ts +7 -1
  64. package/dist/components/form/expressions/ExpressionField.d.ts.map +1 -1
  65. package/dist/components/form/expressions/ExpressionField.js +181 -33
  66. package/dist/components/form/expressions/ExpressionField.js.map +1 -1
  67. package/dist/components/form/expressions/argumentPresence.d.ts +97 -0
  68. package/dist/components/form/expressions/argumentPresence.d.ts.map +1 -0
  69. package/dist/components/form/expressions/argumentPresence.js +203 -0
  70. package/dist/components/form/expressions/argumentPresence.js.map +1 -0
  71. package/dist/components/form/expressions/builder/argumentLabel.d.ts.map +1 -1
  72. package/dist/components/form/expressions/builder/argumentLabel.js +9 -4
  73. package/dist/components/form/expressions/builder/argumentLabel.js.map +1 -1
  74. package/dist/components/form/expressions/builder/index.d.ts +15 -2
  75. package/dist/components/form/expressions/builder/index.d.ts.map +1 -1
  76. package/dist/components/form/expressions/builder/index.js +19 -57
  77. package/dist/components/form/expressions/builder/index.js.map +1 -1
  78. package/dist/components/form/expressions/dpqlMockLsp.d.ts +17 -0
  79. package/dist/components/form/expressions/dpqlMockLsp.d.ts.map +1 -1
  80. package/dist/components/form/expressions/dpqlMockLsp.js +75 -9
  81. package/dist/components/form/expressions/dpqlMockLsp.js.map +1 -1
  82. package/dist/components/form/expressions/renderExpressionToText.d.ts.map +1 -1
  83. package/dist/components/form/expressions/renderExpressionToText.js +94 -19
  84. package/dist/components/form/expressions/renderExpressionToText.js.map +1 -1
  85. package/dist/components/form/fields/Field.d.ts.map +1 -1
  86. package/dist/components/form/fields/Field.js +3 -1
  87. package/dist/components/form/fields/Field.js.map +1 -1
  88. package/dist/components/form/fields/allowed-values/AllowedValues.d.ts +11 -1
  89. package/dist/components/form/fields/allowed-values/AllowedValues.d.ts.map +1 -1
  90. package/dist/components/form/fields/allowed-values/AllowedValues.js +14 -2
  91. package/dist/components/form/fields/allowed-values/AllowedValues.js.map +1 -1
  92. package/dist/components/form/fields/auto/AutoFormField.d.ts.map +1 -1
  93. package/dist/components/form/fields/auto/AutoFormField.js +13 -2
  94. package/dist/components/form/fields/auto/AutoFormField.js.map +1 -1
  95. package/dist/components/form/fields/rich-text/RichText.d.ts +11 -0
  96. package/dist/components/form/fields/rich-text/RichText.d.ts.map +1 -1
  97. package/dist/components/form/fields/rich-text/RichText.js +84 -5
  98. package/dist/components/form/fields/rich-text/RichText.js.map +1 -1
  99. package/dist/components/form/fields/template/ReadOnlyTemplateTag.d.ts +2 -1
  100. package/dist/components/form/fields/template/ReadOnlyTemplateTag.d.ts.map +1 -1
  101. package/dist/components/form/fields/template/ReadOnlyTemplateTag.js +3 -2
  102. package/dist/components/form/fields/template/ReadOnlyTemplateTag.js.map +1 -1
  103. package/dist/components/form/fields/template/TemplateField.d.ts +31 -1
  104. package/dist/components/form/fields/template/TemplateField.d.ts.map +1 -1
  105. package/dist/components/form/fields/template/TemplateField.js +507 -40
  106. package/dist/components/form/fields/template/TemplateField.js.map +1 -1
  107. package/dist/components/form/index.d.ts +1 -0
  108. package/dist/components/form/index.d.ts.map +1 -1
  109. package/dist/components/form/index.js +1 -0
  110. package/dist/components/form/index.js.map +1 -1
  111. package/dist/components/smartEditor/__fixtures__/mockLspServer.d.ts +1 -0
  112. package/dist/components/smartEditor/__fixtures__/mockLspServer.d.ts.map +1 -1
  113. package/dist/components/smartEditor/__fixtures__/mockLspServer.js +6 -1
  114. package/dist/components/smartEditor/__fixtures__/mockLspServer.js.map +1 -1
  115. package/dist/helpers/dpqlDetection.d.ts +58 -0
  116. package/dist/helpers/dpqlDetection.d.ts.map +1 -0
  117. package/dist/helpers/dpqlDetection.js +164 -0
  118. package/dist/helpers/dpqlDetection.js.map +1 -0
  119. package/dist/helpers/optionUiTypes.d.ts +1 -1
  120. package/dist/helpers/options.d.ts +37 -0
  121. package/dist/helpers/options.d.ts.map +1 -1
  122. package/dist/helpers/options.js +78 -1
  123. package/dist/helpers/options.js.map +1 -1
  124. package/dist/helpers/templateItems.d.ts +47 -0
  125. package/dist/helpers/templateItems.d.ts.map +1 -0
  126. package/dist/helpers/templateItems.js +88 -0
  127. package/dist/helpers/templateItems.js.map +1 -0
  128. package/dist/helpers/templates.d.ts +21 -0
  129. package/dist/helpers/templates.d.ts.map +1 -1
  130. package/dist/helpers/templates.js +49 -3
  131. package/dist/helpers/templates.js.map +1 -1
  132. package/dist/helpers/validations.d.ts.map +1 -1
  133. package/dist/helpers/validations.js +56 -9
  134. package/dist/helpers/validations.js.map +1 -1
  135. package/dist/hooks/useArgSchema.d.ts +2 -1
  136. package/dist/hooks/useArgSchema.d.ts.map +1 -1
  137. package/dist/hooks/useFetch/useFetch.d.ts +3 -1
  138. package/dist/hooks/useFetch/useFetch.d.ts.map +1 -1
  139. package/dist/hooks/useFetch/useFetch.js +47 -9
  140. package/dist/hooks/useFetch/useFetch.js.map +1 -1
  141. package/dist/hooks/useTemplates.d.ts +20 -0
  142. package/dist/hooks/useTemplates.d.ts.map +1 -1
  143. package/dist/hooks/useTemplates.js +34 -2
  144. package/dist/hooks/useTemplates.js.map +1 -1
  145. package/dist/index.d.ts +2 -1
  146. package/dist/index.d.ts.map +1 -1
  147. package/dist/index.js +5 -1
  148. package/dist/index.js.map +1 -1
  149. package/dist/stories/storyNetwork.d.ts +36 -0
  150. package/dist/stories/storyNetwork.d.ts.map +1 -0
  151. package/dist/stories/storyNetwork.js +82 -0
  152. package/dist/stories/storyNetwork.js.map +1 -0
  153. package/dist/utils/fetch.d.ts +151 -1
  154. package/dist/utils/fetch.d.ts.map +1 -1
  155. package/dist/utils/fetch.js +256 -44
  156. package/dist/utils/fetch.js.map +1 -1
  157. package/dist/utils/lspClient.d.ts +6 -0
  158. package/dist/utils/lspClient.d.ts.map +1 -1
  159. package/dist/utils/lspClient.js +62 -11
  160. package/dist/utils/lspClient.js.map +1 -1
  161. package/dist/utils/websocket.d.ts +6 -0
  162. package/dist/utils/websocket.d.ts.map +1 -1
  163. package/dist/utils/websocket.js +15 -7
  164. package/dist/utils/websocket.js.map +1 -1
  165. package/package.json +2 -2
  166. package/src/components/dpqlEditor/DpqlEditor.stories.tsx +0 -2
  167. package/src/components/dpqlEditor/dpqlProbe.ts +165 -0
  168. package/src/components/dpqlEditor/index.ts +4 -0
  169. package/src/components/dpqlEditor/types.ts +26 -2
  170. package/src/components/dpqlEditor/useDpqlProbe.ts +33 -0
  171. package/src/components/dpqlEditor/useDpqlSession.ts +20 -4
  172. package/src/components/form/engine/CompactRow.tsx +231 -11
  173. package/src/components/form/engine/CompactToolbar.tsx +10 -3
  174. package/src/components/form/engine/FormEngine.stories.tsx +331 -30
  175. package/src/components/form/engine/FormEngine.tsx +504 -87
  176. package/src/components/form/engine/FormFieldsSkeleton.tsx +76 -0
  177. package/src/components/form/engine/OptionFieldMessages.tsx +89 -11
  178. package/src/components/form/engine/_structuredData/SchemaDataView.tsx +11 -0
  179. package/src/components/form/engine/compactToolbarContext.ts +1 -1
  180. package/src/components/form/engine/readFirst.ts +86 -7
  181. package/src/components/form/engine/rendererTypes.ts +12 -0
  182. package/src/components/form/engine/rowMenuContext.ts +159 -0
  183. package/src/components/form/engine/typeChoices.ts +55 -0
  184. package/src/components/form/expressions/ExpressionField.stories.tsx +828 -31
  185. package/src/components/form/expressions/ExpressionField.tsx +234 -29
  186. package/src/components/form/expressions/argumentPresence.ts +200 -0
  187. package/src/components/form/expressions/builder/argumentLabel.tsx +10 -4
  188. package/src/components/form/expressions/builder/index.tsx +31 -75
  189. package/src/components/form/expressions/dpqlMockLsp.ts +81 -1
  190. package/src/components/form/expressions/renderExpressionToText.ts +106 -17
  191. package/src/components/form/fields/Field.tsx +3 -2
  192. package/src/components/form/fields/allowed-values/AllowedValues.tsx +13 -2
  193. package/src/components/form/fields/auto/AutoFormField.tsx +12 -2
  194. package/src/components/form/fields/rich-text/RichText.tsx +112 -19
  195. package/src/components/form/fields/template/ReadOnlyTemplateTag.tsx +4 -2
  196. package/src/components/form/fields/template/TemplateField.tsx +667 -95
  197. package/src/components/form/index.tsx +1 -0
  198. package/src/components/log/Log.stories.tsx +2 -1
  199. package/src/components/smartEditor/__fixtures__/mockLspServer.ts +6 -1
  200. package/src/helpers/dpqlDetection.ts +209 -0
  201. package/src/helpers/options.ts +70 -0
  202. package/src/helpers/templateItems.ts +81 -0
  203. package/src/helpers/templates.ts +51 -2
  204. package/src/helpers/validations.ts +59 -9
  205. package/src/hooks/useFetch/useFetch.stories.tsx +6 -5
  206. package/src/hooks/useFetch/useFetch.tsx +53 -7
  207. package/src/hooks/useStorage/useStorage.stories.tsx +2 -1
  208. package/src/hooks/useTemplates.ts +41 -0
  209. package/src/hooks/useWebSocket/useWebsocket.stories.tsx +2 -1
  210. package/src/index.tsx +14 -1
  211. package/src/stores/currentUser/currentUser.stories.tsx +2 -1
  212. package/src/stories/storyNetwork.ts +92 -0
  213. package/src/utils/fetch.ts +311 -21
  214. package/src/utils/lspClient.ts +68 -11
  215. package/src/utils/websocket.ts +20 -9
@@ -3,8 +3,9 @@
3
3
  // Owns the `IExpression` value and offers two views of the same AST —
4
4
  // Visual (the builder, Phase 3) and Text (DPQL, this phase). The DPQL text
5
5
  // editor bridges to the AST via the LSP `dpql/parse` (text → AST on edit)
6
- // and `dpql/serialize` (AST → text on entering Text mode); a live "Parsed"
7
- // line renders the current AST (`dpql/renderExpression`).
6
+ // and `dpql/serialize` (AST → text on entering Text mode); a debounced
7
+ // "Preview" box renders the current AST (`dpql/renderExpression`) when that
8
+ // rendering differs from the text the author typed.
8
9
  import {
9
10
  ReqoreButton,
10
11
  ReqoreControlGroup,
@@ -21,6 +22,15 @@ import { useRenderExpression } from './useRenderExpression';
21
22
  export type TExpressionMode = 'visual' | 'text';
22
23
 
23
24
  const PARSE_DEBOUNCE_MS = 300;
25
+ /**
26
+ * How long the Preview waits before catching up with the editor.
27
+ *
28
+ * It mirrors the AST, so without this it re-renders on every keystroke — a box
29
+ * appearing, changing and vanishing under the line being typed. Slightly
30
+ * SLOWER than the parse it follows, so the preview settles once after the text
31
+ * has stopped moving rather than twitching on the way there.
32
+ */
33
+ const PREVIEW_DEBOUNCE_MS = 400;
24
34
  /** Retry cadence for seeding the Text editor while its LSP session opens. */
25
35
  const SEED_RETRY_MS = 400;
26
36
  const SEED_MAX_TRIES = 20;
@@ -62,6 +72,12 @@ export interface IExpressionFieldProps {
62
72
  recordType?: string;
63
73
  /** Initial editor mode (default `visual`). */
64
74
  defaultMode?: TExpressionMode;
75
+ /**
76
+ * SEAM (reqraft): the host's per-`ui_type` editors, forwarded to the
77
+ * builder's operand fields. Without them an operand typed with one of the
78
+ * CONSUMER's ui_types renders "Unknown type!".
79
+ */
80
+ componentOverrides?: Record<string, React.FC<any>>;
65
81
  size?: string;
66
82
  /** Forwarded to the builder — operand reordering for varargs expressions. */
67
83
  reorder?: TExpressionReorder;
@@ -83,6 +99,7 @@ export const ExpressionField = memo(
83
99
  recordType,
84
100
  defaultMode = 'visual',
85
101
  size,
102
+ componentOverrides,
86
103
  reorder,
87
104
  }: IExpressionFieldProps) => {
88
105
  const { expressions } = useExpressions({
@@ -100,60 +117,204 @@ export const ExpressionField = memo(
100
117
  // Text mode state. `text` is the DPQL string the editor shows; the AST
101
118
  // (`value`) stays the source of truth, kept in sync via parse-on-edit.
102
119
  const [text, setText] = useState('');
120
+ /* The text the current AST was parsed FROM.
121
+ *
122
+ * The preview renders the AST, and the AST only moves on a SUCCESSFUL
123
+ * parse — so while an edit is mid-flight or simply invalid, the AST still
124
+ * describes the last thing that parsed. Showing it then states something
125
+ * the editor does not contain: deleting the `2` from `1 + 2` left
126
+ * "Unexpected end of input" sitting directly above a Preview confidently
127
+ * reading `1 + 2`. */
128
+ const [astText, setAstText] = useState('');
129
+ /**
130
+ * The server's answer to "can this expression's result satisfy the type
131
+ * this field declares?". `null` when nothing has been asked, or when the
132
+ * field declares no type worth asking about.
133
+ */
134
+ const [typeCheck, setTypeCheck] = useState<{
135
+ compatible: boolean;
136
+ mayFail: boolean;
137
+ inferred?: string;
138
+ target?: string;
139
+ fix?: string;
140
+ } | null>(null);
103
141
  const dpqlRef = useRef<IDpqlEditorRef>(null);
104
142
  const parseTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
105
143
 
106
144
  const ast = useMemo<IExpressionValue | undefined>(() => value?.value, [value]);
107
145
 
146
+ /**
147
+ * The type the expression's result has to satisfy, sent with every parse.
148
+ *
149
+ * `auto` and `any` are left out deliberately: they accept anything, so the
150
+ * server would answer "compatible" for everything and the round trip would
151
+ * buy nothing. A field offering a CHOICE of return types has no single
152
+ * answer either, so it asks nothing rather than asking about the wrong one.
153
+ */
154
+ const targetType = useMemo<string | undefined>(() => {
155
+ const declared = Array.isArray(returnType) ? undefined : (returnType ?? type);
156
+ return declared && declared !== 'auto' && declared !== 'any' ? declared : undefined;
157
+ }, [returnType, type]);
158
+
159
+ /** Read the type analysis off a parse result, or clear it. */
160
+ const readTypeCheck = useCallback(
161
+ (result?: { type_compatible?: boolean; coercion_may_fail?: boolean;
162
+ inferred_type?: string; target_type?: string;
163
+ suggested_fix?: { text: string } }): void => {
164
+ if (!result || result.type_compatible === undefined) {
165
+ setTypeCheck(null);
166
+ return;
167
+ }
168
+ setTypeCheck({
169
+ compatible: !!result.type_compatible,
170
+ mayFail: !!result.coercion_may_fail,
171
+ inferred: result.inferred_type,
172
+ target: result.target_type,
173
+ fix: result.suggested_fix?.text,
174
+ });
175
+ },
176
+ []
177
+ );
178
+
108
179
  // Keep a readable rendering of the current expression in sync — the
109
180
  // single live mirror of the AST (server `dpql/renderExpression` when
110
181
  // reachable, the client-side approximation otherwise).
182
+ //
183
+ // Debounced: it follows the AST, which follows the text, so rendering it
184
+ // eagerly put a box under the cursor that appeared, changed and vanished
185
+ // while the author was still typing.
111
186
  useEffect(() => {
112
187
  let live = true;
113
- renderRich(ast ?? {}, expressions).then((r) => {
114
- if (live) setPreview({ text: r.text, server: r.server });
115
- });
188
+ const timer = setTimeout(() => {
189
+ renderRich(ast ?? {}, expressions).then((r) => {
190
+ if (live) setPreview({ text: r.text, server: r.server });
191
+ });
192
+ }, PREVIEW_DEBOUNCE_MS);
116
193
  return () => {
117
194
  live = false;
195
+ clearTimeout(timer);
118
196
  };
119
197
  }, [renderRich, ast, expressions]);
120
198
 
121
199
  // Tracks whether the user has typed since entering Text mode, so a
122
200
  // slow seed response can't clobber their input.
123
201
  const userTypedRef = useRef(false);
124
- const astRef = useRef(ast);
202
+
203
+ /* A cleared value must clear the editor with it.
204
+ *
205
+ * "Clear value" empties the option (`handleValueChange(name, undefined)`),
206
+ * but the DPQL text is local state seeded once on entering Text mode, and
207
+ * the seeding effect returns early for an empty AST — it exists to fill the
208
+ * editor, not to empty it. So the value went and the text stayed: the row
209
+ * reported "This field is required" while still showing `1 + 2`, which
210
+ * reads as the clear having silently failed.
211
+ *
212
+ * Only an AST that HAD content and lost it clears the box. A field that
213
+ * simply starts empty is left alone, so this cannot wipe what an author is
214
+ * part-way through typing before their first parse lands.
215
+ */
216
+ const hadExpression = useRef(false);
125
217
  useEffect(() => {
126
- astRef.current = ast;
218
+ /* GONE, not merely incomplete.
219
+ *
220
+ * The first cut asked whether the AST still had an `exp`, and that is a
221
+ * state the value passes THROUGH while the author types: a half-written
222
+ * expression parses to something without one. So typing `1 + 2` cleared
223
+ * the editor mid-keystroke and the text vanished under the cursor.
224
+ *
225
+ * A clear removes the value itself, so that is what to watch: `value`
226
+ * empty, not an AST that is between shapes. Typing never produces that —
227
+ * `handleDpqlChange` only emits on a SUCCESSFUL parse, so an incomplete
228
+ * expression leaves the previous value in place. */
229
+ if (ast) {
230
+ hadExpression.current = true;
231
+ return;
232
+ }
233
+ if (hadExpression.current) {
234
+ hadExpression.current = false;
235
+ userTypedRef.current = false;
236
+ setText('');
237
+ }
127
238
  }, [ast]);
128
239
 
129
240
  // Text mode: parse the DPQL into the AST (debounced).
241
+ /* The session may not be attached when the debounce fires — the editor
242
+ mounts on the render that flips the mode, and its LSP session comes up
243
+ after that. `dpqlRef.current?.parse?.()` is optional all the way down, so
244
+ an early call resolves `undefined`, `readTypeCheck` CLEARS the analysis,
245
+ and nothing asks again: the author types once into a fresh Text field and
246
+ is told nothing about the type until they happen to type another
247
+ character.
248
+
249
+ The seeding effect below already retries for this exact reason
250
+ ("`serialize` resolves '' until the editor's LSP session is ready"); the
251
+ parse path needed the same care and did not have it. Same budget as
252
+ seeding, so a session that never arrives gives up rather than spinning.
253
+
254
+ Found by CI, not locally: the session is up before the debounce on a
255
+ fast machine, so the story asserting the type message passed here every
256
+ time and failed on the runner. */
130
257
  const handleDpqlChange = useCallback(
131
258
  (next: string) => {
132
259
  userTypedRef.current = true;
133
260
  setText(next);
134
261
  if (parseTimer.current) clearTimeout(parseTimer.current);
135
- parseTimer.current = setTimeout(async () => {
136
- const result = await dpqlRef.current?.parse?.(next);
262
+ let tries = 0;
263
+ const runParse = async (): Promise<void> => {
264
+ const parse = dpqlRef.current?.parse;
265
+ if (!parse) {
266
+ if (++tries < SEED_MAX_TRIES) {
267
+ parseTimer.current = setTimeout(runParse, SEED_RETRY_MS);
268
+ }
269
+ return;
270
+ }
271
+ const result = await parse(next, targetType);
272
+ readTypeCheck(result);
137
273
  if (result?.success && result.expression) {
274
+ setAstText(next);
138
275
  // `dpql/parse` returns the field-ready `{ is_expression, value }`.
139
276
  onChange(result.expression as IExpression);
140
277
  }
141
- }, PARSE_DEBOUNCE_MS);
278
+ };
279
+ parseTimer.current = setTimeout(runParse, PARSE_DEBOUNCE_MS);
142
280
  },
143
- [onChange]
281
+ [onChange, targetType, readTypeCheck]
144
282
  );
145
283
 
146
- // Seed the Text editor from the AST whenever Text mode becomes active
147
- // (including a `defaultMode='text'` mount). This must run as an
148
- // effect: the editor only mounts on the same render that flips the
149
- // mode (so `dpqlRef` is null inside the click handler), and
150
- // `serialize` resolves '' until the editor's LSP session is ready —
151
- // hence the brief retry loop.
284
+ /** Whether this visit to Text mode has already put the AST in the editor. */
285
+ const seededRef = useRef(false);
286
+
287
+ /* Seed the Text editor from the AST whenever Text mode becomes active
288
+ * (including a `defaultMode='text'` mount). This must run as an effect: the
289
+ * editor only mounts on the same render that flips the mode (so `dpqlRef`
290
+ * is null inside the click handler), and `serialize` resolves '' until the
291
+ * editor's LSP session is ready — hence the brief retry loop.
292
+ *
293
+ * It waits for an AST, because the AST can arrive AFTER the mode does. A
294
+ * host flips this shell into Text mode on the very render that accepts an
295
+ * expression, and the value it passes on that render is still the TEXT the
296
+ * author typed — the parsed AST only comes back through the form one render
297
+ * later. Keyed on the mode alone, the effect had already had its only turn
298
+ * by then: it found nothing to serialize, returned, and nothing asked
299
+ * again. The author landed in an empty editor with a Preview of their own
300
+ * expression sitting beside it, which reads as the text having been thrown
301
+ * away.
302
+ *
303
+ * Seeding happens at most ONCE per visit, and that is what makes watching
304
+ * the AST safe: `userTypedRef` guards what is being typed, but the AST
305
+ * MOVES as the author types — every successful parse replaces it — so an
306
+ * effect that re-seeded on each new AST would fight them for the editor. */
152
307
  useEffect(() => {
153
- if (mode !== 'text') return undefined;
154
- const seedAst = astRef.current;
155
- if (!seedAst?.exp) return undefined;
156
- userTypedRef.current = false;
308
+ if (mode !== 'text') {
309
+ // Leaving Text asks the question again on the next visit; the typing
310
+ // flag goes with it, or a stale one would suppress that visit's seed.
311
+ seededRef.current = false;
312
+ userTypedRef.current = false;
313
+ return undefined;
314
+ }
315
+ if (seededRef.current || userTypedRef.current) return undefined;
316
+ if (!ast?.exp) return undefined;
317
+ const seedAst = ast;
157
318
  let cancelled = false;
158
319
  let timer: ReturnType<typeof setTimeout> | null = null;
159
320
  let tries = 0;
@@ -161,7 +322,9 @@ export const ExpressionField = memo(
161
322
  const t = await dpqlRef.current?.serialize?.(seedAst);
162
323
  if (cancelled || userTypedRef.current) return;
163
324
  if (t) {
325
+ seededRef.current = true;
164
326
  setText(t);
327
+ setAstText(t);
165
328
  } else if (++tries < SEED_MAX_TRIES) {
166
329
  timer = setTimeout(seed, SEED_RETRY_MS);
167
330
  }
@@ -171,7 +334,7 @@ export const ExpressionField = memo(
171
334
  cancelled = true;
172
335
  if (timer) clearTimeout(timer);
173
336
  };
174
- }, [mode]);
337
+ }, [mode, ast]);
175
338
 
176
339
  // Switch to Text: the seeding effect above serializes the AST once
177
340
  // the editor's session is up.
@@ -183,13 +346,14 @@ export const ExpressionField = memo(
183
346
  const enterVisualMode = useCallback(async () => {
184
347
  if (parseTimer.current) clearTimeout(parseTimer.current);
185
348
  if (mode === 'text' && text) {
186
- const result = await dpqlRef.current?.parse?.(text);
349
+ const result = await dpqlRef.current?.parse?.(text, targetType);
350
+ readTypeCheck(result);
187
351
  if (result?.success && result.expression) {
188
352
  onChange(result.expression as IExpression);
189
353
  }
190
354
  }
191
355
  setMode('visual');
192
- }, [mode, text, onChange]);
356
+ }, [mode, text, onChange, targetType, readTypeCheck]);
193
357
 
194
358
  useEffect(
195
359
  () => () => {
@@ -232,11 +396,51 @@ export const ExpressionField = memo(
232
396
  readOnly={readOnly}
233
397
  height='48px'
234
398
  />
235
- {/* An empty query has nothing to parse — the box appears once a
236
- parse result exists rather than sitting there holding a
237
- placeholder token. */}
238
- {preview.text ? (
239
- <ReqoreMessage intent='info' size='small' title='Parsed' flat opaque={false}>
399
+ {/* What the server said about the result's type, when the field
400
+ declares one to check against.
401
+
402
+ Nothing is shown when the expression already fits, and nothing
403
+ is shown when the conversion is CERTAIN either — a number used
404
+ as text always works, and a warning about something that cannot
405
+ fail is what teaches people to ignore the ones that can. So this
406
+ appears for exactly two cases: the conversion is impossible
407
+ (danger), or it is attempted and may fail on the day (warning,
408
+ and the run checks the value itself). */}
409
+ {typeCheck && !typeCheck.compatible && (
410
+ <ReqoreMessage
411
+ intent={typeCheck.mayFail ? 'warning' : 'danger'}
412
+ size='small'
413
+ flat
414
+ opaque={false}
415
+ title={typeCheck.mayFail ? 'This may not fit' : 'This does not fit'}
416
+ >
417
+ {`The expression returns ${typeCheck.inferred}, and this field holds ${typeCheck.target}.`}
418
+ {typeCheck.mayFail ?
419
+ ' That conversion is attempted rather than guaranteed, so the value itself is checked when it runs.'
420
+ : ''}
421
+ {typeCheck.fix ? (
422
+ <div style={{ marginTop: '6px' }}>
423
+ <code data-testid='expression-type-fix'>{typeCheck.fix}</code>
424
+ </div>
425
+ ) : null}
426
+ </ReqoreMessage>
427
+ )}
428
+
429
+ {/* Shown only when it ADDS something.
430
+
431
+ It is a rendering of the AST, so for most text it repeats the
432
+ line directly above it — `1 + 2` previewing as `1 + 2` is a
433
+ second copy that flickers as you type. It earns the space when
434
+ the rendering DIFFERS: a template reference resolved to a chip,
435
+ or a normalised form. Comparison ignores surrounding whitespace,
436
+ which the renderer does not preserve.
437
+
438
+ An empty query has nothing to render either, so the box appears
439
+ once there is a result rather than holding a placeholder. */}
440
+ {preview.text &&
441
+ astText.trim() === text.trim() &&
442
+ preview.text.trim() !== text.trim() ? (
443
+ <ReqoreMessage intent='info' size='small' title='Preview' flat opaque={false}>
240
444
  {preview.server ? (
241
445
  // Server rendering shown through a read-only DpqlEditor:
242
446
  // template-ref chips + LSP token colours; diagnostics off
@@ -268,6 +472,7 @@ export const ExpressionField = memo(
268
472
  readOnly={readOnly}
269
473
  localTemplates={localTemplates ?? { items: [] }}
270
474
  serverHandled={serverHandled}
475
+ componentOverrides={componentOverrides}
271
476
  extraActions={extraActions}
272
477
  size={size}
273
478
  reorder={reorder}
@@ -0,0 +1,200 @@
1
+ // Copyright 2026 Qore Technologies, s.r.o.
2
+ /**
3
+ * Is this expression argument SUPPLIED, or is it still missing?
4
+ *
5
+ * DPQL has a null literal: `null` parses to `{exp: "value", args: [null]}` and
6
+ * round-trips, and an author writing it in the text editor means *"this is the
7
+ * value — nothing"*. It is the only way to assert that a call returned no
8
+ * value, which is how it was reported.
9
+ *
10
+ * The rule, stated once here because three different places had drawn it
11
+ * differently: **an argument that is explicitly null is supplied; one that is
12
+ * absent or `undefined` is still missing.**
13
+ *
14
+ * The builder used to ask `size(arg) === 0`, and lodash answers `0` for `null`
15
+ * as readily as for `{}`. So an explicit null was read as a missing argument
16
+ * and reset from the catalogue's declaration — which is how a saved draft ended
17
+ * up holding `{"type": "any", "required": true}`, an argument DEFINITION where
18
+ * its value should be, with the author's `null` gone and `required` (a schema
19
+ * key, never a value key) left behind to give it away.
20
+ */
21
+
22
+ /**
23
+ * True when `arg` carries no value yet — `undefined`, the `{}` the builder
24
+ * writes when a type change resets an argument, or any envelope with no
25
+ * `value` in it.
26
+ *
27
+ * `null` is NOT missing: it is the null literal.
28
+ *
29
+ * The rule used to be "an object with no keys at all", which let an envelope
30
+ * holding nothing but SCHEMA keys pass as a supplied value. That is how a saved
31
+ * draft kept `{type: 'any', required: true}` — an argument DEFINITION where the
32
+ * author's value belonged — and why re-opening the expression never repaired
33
+ * it: the builder looked at those two keys, concluded the argument was already
34
+ * supplied, and left the definition in place as data. `required` is a schema
35
+ * key; nothing ever reads it back off a stored value.
36
+ *
37
+ * Asking for the `value` instead makes the question the one that was always
38
+ * meant: has the author put something here?
39
+ *
40
+ * @param arg one entry of an expression's `args`
41
+ */
42
+ export const isExpressionArgumentMissing = (arg: unknown): boolean => {
43
+ if (arg === null) {
44
+ return false;
45
+ }
46
+
47
+ if (arg === undefined) {
48
+ return true;
49
+ }
50
+
51
+ if (typeof arg !== 'object') {
52
+ // a raw scalar argument — `0`, `false` and `''` are values too
53
+ return false;
54
+ }
55
+
56
+ return (arg as { value?: unknown }).value === undefined;
57
+ };
58
+
59
+ /**
60
+ * The shape the builder stores an argument in: `{type, value}`.
61
+ *
62
+ * A parse result carries its arguments RAW — `args: [null]`, `args: [3]` —
63
+ * because that is what the grammar produced. The builder's editors read
64
+ * `arg.value`, so a raw argument has to be widened before it is stored, and
65
+ * widening `null` is the whole point: dropped instead, the author's null
66
+ * becomes an argument with no `value` key at all, which every later reader
67
+ * treats as an empty field.
68
+ *
69
+ * An argument that is already an envelope is returned untouched.
70
+ *
71
+ * @param arg one entry of an expression's `args`
72
+ * @param uiType the type the catalogue declares for that position
73
+ */
74
+ export const asExpressionArgumentEnvelope = (arg: unknown, uiType?: string): unknown => {
75
+ if (arg !== null && (arg === undefined || typeof arg === 'object')) {
76
+ return arg;
77
+ }
78
+
79
+ return { value: arg, ...(uiType ? { type: uiType } : {}) };
80
+ };
81
+
82
+ /**
83
+ * Does this value still look like an expression AST?
84
+ *
85
+ * `{exp, args}` is the shape the builder and `dpql/parse` both produce. The
86
+ * check is structural on purpose: the flag that marks an option as holding an
87
+ * expression describes its VALUE, so the value is what should decide whether
88
+ * the flag survives a change that said nothing about it.
89
+ */
90
+ export const looksLikeExpressionAst = (value: unknown): boolean =>
91
+ !!value &&
92
+ typeof value === 'object' &&
93
+ !Array.isArray(value) &&
94
+ typeof (value as { exp?: unknown }).exp === 'string';
95
+
96
+ /**
97
+ * Whether an option should still be marked as holding an expression.
98
+ *
99
+ * The form engine used to delete the flag on ANY change that did not re-assert
100
+ * it, and most changes do not: an editor re-mounting, a revert, a validation
101
+ * pass. The value stayed the AST while the flag went, so the option degraded
102
+ * into a plain hash — which is how an assertion's Expected Value came back
103
+ * from a saved draft as a two-field object showing `exp` and `args`, and why
104
+ * `encodeTestValue` then stored it with `source: "literal"`.
105
+ *
106
+ * @param isFunction what the caller said: `true` sets the flag, `false` clears
107
+ * it deliberately (choosing a concrete type does this), `undefined` is no
108
+ * opinion at all
109
+ * @param value the option's new value
110
+ * @param wasExpression whether the option was marked before this change
111
+ */
112
+ export const shouldMarkAsExpression = (
113
+ isFunction: boolean | undefined,
114
+ value: unknown,
115
+ wasExpression: boolean | undefined
116
+ ): boolean => {
117
+ if (isFunction === true) {
118
+ return true;
119
+ }
120
+
121
+ if (isFunction === false) {
122
+ return false;
123
+ }
124
+
125
+ // No opinion: the flag belongs to the value, so it survives exactly as long
126
+ // as the value is still an expression.
127
+ return !!wasExpression && looksLikeExpressionAst(value);
128
+ };
129
+
130
+ /**
131
+ * Fill in an expression's missing arguments, leaving the supplied ones alone.
132
+ *
133
+ * Extracted from the builder so the decision it makes can be tested directly:
134
+ * a component test of the same behaviour passed just as happily with the defect
135
+ * in place, because the builder normalises on an interaction the harness never
136
+ * performed. A test that cannot fail is worse than none.
137
+ *
138
+ * @param expressions the served expression catalogue
139
+ * @param expression the chosen expression's `name`
140
+ * @param args the arguments so far — raw from a parse, or envelopes from the
141
+ * builder's own editors
142
+ */
143
+ export const addMissingExpressionArgs = (
144
+ expressions: any[] | undefined,
145
+ expression: string,
146
+ args: any[] = []
147
+ ): any[] => {
148
+ const newArgs = [...args];
149
+ const selected = expressions?.find((exp) => exp?.name === expression);
150
+
151
+ if (!selected) {
152
+ return newArgs;
153
+ }
154
+
155
+ if (selected.varargs) {
156
+ if (selected.subtype === 2) {
157
+ newArgs.push({ is_expression: true, value: { args: [] } });
158
+ } else {
159
+ newArgs.push(
160
+ ...Array.from({ length: (selected.min_args ?? 1) - 1 }, () => ({
161
+ type:
162
+ selected.args[0].ui_type === 'richtext' || selected.args[0].ui_type === 'number'
163
+ ? selected.args[0].ui_type
164
+ : undefined,
165
+ }))
166
+ );
167
+ }
168
+
169
+ return newArgs;
170
+ }
171
+
172
+ selected.args.forEach((arg: any, index: number) => {
173
+ /* A RAW argument, as `dpql/parse` produces it — `args: [null]`, `args: [3]`
174
+ — is widened into the `{type, value}` envelope the editors read. Widening
175
+ the null is the point: dropped instead, the author's `null` became an
176
+ argument with no `value` key, and the branch below then filled the gap
177
+ from the catalogue's DECLARATION, which is how a saved draft came back
178
+ holding `{type: "any", required: true}` where its value belonged. */
179
+ newArgs[index] = asExpressionArgumentEnvelope(newArgs[index], arg.ui_type);
180
+
181
+ /* "Missing" already means "carries no value", so an argument waiting for
182
+ its catalogue default is simply a missing one — it needs no separate
183
+ test. */
184
+ if (!isExpressionArgumentMissing(newArgs[index])) {
185
+ return;
186
+ }
187
+
188
+ newArgs[index] =
189
+ arg.default_value ?
190
+ { value: arg.default_value, type: arg.ui_type }
191
+ : {
192
+ type:
193
+ arg.ui_type === 'richtext' || arg.ui_type === 'number' || arg.ui_type === 'bool' ?
194
+ arg.ui_type
195
+ : undefined,
196
+ };
197
+ });
198
+
199
+ return newArgs;
200
+ };
@@ -49,9 +49,15 @@ export const ExpressionBuilderArgumentLabel = ({
49
49
  ...types.getExactMatches(schema.ui_type),
50
50
  ...types.getAcceptedTypes(schema.ui_type),
51
51
  ]);
52
- const { isValid, reason } = arg.is_expression
53
- ? validateFieldWithResult('expression', arg, { expressions })
54
- : validateFieldWithResult(argumentType, arg.value, schema, !schema.required);
52
+ /* `arg` is optional — an operand the author has not filled in yet has no
53
+ entry at all, and a literal carried straight from a parse can be the null
54
+ literal itself. Both were dereferenced unguarded here, which took the whole
55
+ expression editor down to its error boundary rather than drawing an empty
56
+ argument. */
57
+ const { isValid, reason } =
58
+ arg?.is_expression ?
59
+ validateFieldWithResult('expression', arg, { expressions })
60
+ : validateFieldWithResult(argumentType, arg?.value, schema, !schema.required);
55
61
 
56
62
  return (
57
63
  <ReqoreP
@@ -94,7 +100,7 @@ export const ExpressionBuilderArgumentLabel = ({
94
100
  {!matchesType ? (
95
101
  <ReqoreIcon icon='ErrorWarningLine' color='warning' size='10px' margin='both' />
96
102
  ) : null}
97
- {(argumentType || (arg.is_expression && arg.value?.exp)) &&
103
+ {(argumentType || (arg?.is_expression && arg.value?.exp)) &&
98
104
  types.getTypeDisplayName(argumentType) !== types.getTypeDisplayName(schema.ui_type) &&
99
105
  !matchesTypeExactly ? (
100
106
  <>