Skip to content

Self-improving AI Employees for everyone

fix(picker): resolve $ref and Optional unions in node output schemas

trohitgc8efcad61f8a
diff --git a/client/src/components/parameterPanel/InputSection.tsx b/client/src/components/parameterPanel/InputSection.tsx
index e4a42bf5..62acb9ce 100644
--- a/client/src/components/parameterPanel/InputSection.tsx
+++ b/client/src/components/parameterPanel/InputSection.tsx
@@ -55,22 +55,84 @@ async function fetchNodeOutputSchema(
});
}
+/**
+ * Resolve the indirection Pydantic emits so a property carries a usable
+ * `type`.
+ *
+ * Two shapes reach us that a naive reader mistypes as 'any':
+ * - `Optional[X]` -> { anyOf: [<X>, { type: 'null' }] } (no top-level type)
+ * - a nested BaseModel -> { $ref: '#/$defs/Name' } (body lives in $defs)
+ *
+ * Every field on the trigger output models is Optional, so without this
+ * every field in the variable picker typed as 'any' and nested models
+ * (telegramReceive.media, whatsappReceive.group_info) had no expandable
+ * leaves to drag.
+ */
+function resolveSchemaNode(
+ prop: Record<string, any>,
+ root: Record<string, any>,
+ seen: Set<string>,
+): { node: Record<string, any>; seen: Set<string> } {
+ if (typeof prop.$ref === 'string') {
+ // Only local "#/$defs/Name" pointers are emitted; anything else is
+ // left alone rather than guessed at.
+ const name = prop.$ref.startsWith('#/$defs/') ? prop.$ref.slice('#/$defs/'.length) : '';
+ // A name already expanded on this path is a cycle (a self-referencing
+ // model). Stop and let the caller render it as an opaque leaf rather
+ // than recursing until the stack blows and takes the panel with it.
+ if (!name || seen.has(name)) return { node: prop, seen };
+ const target = root.$defs?.[name];
+ if (!target || typeof target !== 'object') return { node: prop, seen };
+ return resolveSchemaNode(target, root, new Set(seen).add(name));
+ }
+
+ const union = prop.anyOf ?? prop.oneOf;
+ if (Array.isArray(union)) {
+ // Pick the first non-null branch — `Optional[X]` is by far the common
+ // case, and a genuine multi-type union has no single shape to show.
+ const branch = union.find(
+ (entry) => entry && typeof entry === 'object' && entry.type !== 'null',
+ );
+ if (branch) return resolveSchemaNode(branch, root, seen);
+ }
+
+ return { node: prop, seen };
+}
+
/**
* Flatten a JSON-Schema-7 "object" shape into the plain
* { field: 'primitive-type-name' | nestedObject } map the variable
* panel expects.
*/
-function jsonSchemaToShape(schema: Record<string, any> | null | undefined): Record<string, any> | null {
+// eslint-disable-next-line react-refresh/only-export-components -- pure schema
+// helper, exported so the $ref/anyOf resolution can be tested directly.
+export function jsonSchemaToShape(
+ schema: Record<string, any> | null | undefined,
+ root?: Record<string, any>,
+ seen: Set<string> = new Set(),
+): Record<string, any> | null {
if (!schema || typeof schema !== 'object') return null;
const props = schema.properties;
if (!props || typeof props !== 'object') return null;
+ // $defs live on the document root, so recursion must keep a handle on it.
+ const document = root ?? schema;
+
const out: Record<string, any> = {};
for (const [key, raw] of Object.entries(props)) {
if (!raw || typeof raw !== 'object') continue;
- const prop = raw as Record<string, any>;
- if (prop.type === 'object' && prop.properties) {
- out[key] = jsonSchemaToShape(prop) ?? 'object';
+ // The expanded-$ref chain travels with the value so a cycle is caught
+ // across the mutual recursion between these two functions, not just
+ // within one of them.
+ const { node: prop, seen: nested } = resolveSchemaNode(
+ raw as Record<string, any>,
+ document,
+ seen,
+ );
+ // A resolved $ref body sometimes omits `type: 'object'`, so treat the
+ // presence of `properties` as sufficient.
+ if (prop.properties) {
+ out[key] = jsonSchemaToShape(prop, document, nested) ?? 'object';
} else if (prop.type === 'array') {
out[key] = 'array';
} else if (typeof prop.type === 'string') {
diff --git a/client/src/components/parameterPanel/__tests__/jsonSchemaToShape.test.ts b/client/src/components/parameterPanel/__tests__/jsonSchemaToShape.test.ts
new file mode 100644
index 00000000..b57241e0
--- /dev/null
+++ b/client/src/components/parameterPanel/__tests__/jsonSchemaToShape.test.ts
@@ -0,0 +1,144 @@
+/**
+ * jsonSchemaToShape — JSON-Schema-7 flattening for the variable picker.
+ *
+ * The backend serves output schemas straight from Pydantic
+ * (`model_json_schema()`), which expresses `Optional[X]` as an `anyOf` with
+ * no top-level `type`, and a nested BaseModel as a `$ref` into `$defs`.
+ * Every field on the trigger output models is Optional, so without
+ * resolution the picker typed all of them 'any' and nested blocks
+ * (telegramReceive.media, whatsappReceive.group_info) had no draggable
+ * leaves.
+ */
+import { describe, it, expect } from 'vitest';
+
+import { jsonSchemaToShape } from '../InputSection';
+
+describe('jsonSchemaToShape', () => {
+ it('returns null for a non-object or property-less schema', () => {
+ expect(jsonSchemaToShape(null)).toBeNull();
+ expect(jsonSchemaToShape(undefined)).toBeNull();
+ expect(jsonSchemaToShape({})).toBeNull();
+ });
+
+ it('maps plain primitives, normalising integer to number', () => {
+ const shape = jsonSchemaToShape({
+ properties: {
+ name: { type: 'string' },
+ count: { type: 'integer' },
+ ratio: { type: 'number' },
+ ok: { type: 'boolean' },
+ items: { type: 'array' },
+ },
+ });
+
+ expect(shape).toEqual({
+ name: 'string',
+ count: 'number',
+ ratio: 'number',
+ ok: 'boolean',
+ items: 'array',
+ });
+ });
+
+ it('unwraps Optional[X] to the underlying primitive type', () => {
+ // Exactly what Pydantic emits for `text: Optional[str] = None`.
+ const shape = jsonSchemaToShape({
+ properties: {
+ text: { anyOf: [{ type: 'string' }, { type: 'null' }], default: null },
+ message_id: { anyOf: [{ type: 'integer' }, { type: 'null' }], default: null },
+ },
+ });
+
+ expect(shape).toEqual({ text: 'string', message_id: 'number' });
+ });
+
+ it('resolves a $ref into $defs so nested fields become draggable leaves', () => {
+ // The telegramReceive.media shape.
+ const shape = jsonSchemaToShape({
+ properties: {
+ media: {
+ anyOf: [{ $ref: '#/$defs/TelegramMedia' }, { type: 'null' }],
+ default: null,
+ },
+ },
+ $defs: {
+ TelegramMedia: {
+ type: 'object',
+ properties: {
+ kind: { anyOf: [{ type: 'string' }, { type: 'null' }] },
+ file_id: { anyOf: [{ type: 'string' }, { type: 'null' }] },
+ file_size: { anyOf: [{ type: 'integer' }, { type: 'null' }] },
+ },
+ },
+ },
+ });
+
+ expect(shape).toEqual({
+ media: { kind: 'string', file_id: 'string', file_size: 'number' },
+ });
+ });
+
+ it('resolves a bare $ref without an Optional wrapper', () => {
+ const shape = jsonSchemaToShape({
+ properties: { group_info: { $ref: '#/$defs/GroupInfo' } },
+ $defs: {
+ GroupInfo: { type: 'object', properties: { subject: { type: 'string' } } },
+ },
+ });
+
+ expect(shape).toEqual({ group_info: { subject: 'string' } });
+ });
+
+ it('keeps $defs reachable through nested recursion', () => {
+ const shape = jsonSchemaToShape({
+ properties: {
+ outer: {
+ type: 'object',
+ properties: { inner: { $ref: '#/$defs/Leaf' } },
+ },
+ },
+ $defs: { Leaf: { type: 'object', properties: { v: { type: 'string' } } } },
+ });
+
+ expect(shape).toEqual({ outer: { inner: { v: 'string' } } });
+ });
+
+ it('does not hang on a self-referencing $ref', () => {
+ const shape = jsonSchemaToShape({
+ properties: { node: { $ref: '#/$defs/Node' } },
+ $defs: {
+ Node: {
+ type: 'object',
+ properties: {
+ label: { type: 'string' },
+ child: { $ref: '#/$defs/Node' },
+ },
+ },
+ },
+ });
+
+ expect(shape?.node).toMatchObject({ label: 'string' });
+ });
+
+ it('falls back to any for an unresolvable ref or an untyped property', () => {
+ const shape = jsonSchemaToShape({
+ properties: {
+ missing: { $ref: '#/$defs/NotThere' },
+ external: { $ref: 'https://example.com/schema.json' },
+ untyped: { description: 'no type at all' },
+ },
+ });
+
+ expect(shape).toEqual({ missing: 'any', external: 'any', untyped: 'any' });
+ });
+
+ it('picks the first non-null branch of a multi-type union', () => {
+ const shape = jsonSchemaToShape({
+ properties: {
+ chat_id: { anyOf: [{ type: 'integer' }, { type: 'string' }, { type: 'null' }] },
+ },
+ });
+
+ expect(shape).toEqual({ chat_id: 'number' });
+ });
+});
diff --git a/server/nodes/telegram/_send.py b/server/nodes/telegram/_send.py
index 845f28a2..71d08e5c 100644
--- a/server/nodes/telegram/_send.py
+++ b/server/nodes/telegram/_send.py
@@ -3,10 +3,12 @@
Both entry points — the workflow node (``telegram_send.py``) and the direct
WebSocket command (``_handlers.py:handle_telegram_send``) — route through
``perform_send`` here. They used to carry two independent copies of the
-message-type dispatch, which had already drifted: the WebSocket copy silently
-ignored ``silent`` and ``reply_to_message_id``, and validated nothing, so a
-``location`` command with a null latitude reached python-telegram-bot and
-raised a TypeError instead of a legible error.
+message-type dispatch, which had already drifted: the WebSocket copy never
+forwarded ``silent`` or ``reply_to_message_id`` (neither name appeared in that
+file), so a caller could ask for a silent threaded reply over the socket and
+get a loud top-level message instead. Its per-type checks were otherwise
+equivalent to the node's; validating through ``TelegramSendParams`` now keeps
+the two from diverging again as message types are added.
Dispatch is a table keyed by message type, so adding a type is one entry plus
one ``Literal`` member rather than a new branch in two files.