Feat: The structured output of the variable query can also be clicked. #10866 (#10952)

### What problem does this PR solve?

Feat: The structured output of the variable query can also be clicked.
#10866

### Type of change


- [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
balibabu
2025-11-03 12:30:30 +08:00
committed by GitHub
parent b47361432a
commit a52bdf0b7e
6 changed files with 61 additions and 23 deletions

View File

@ -929,3 +929,11 @@ export const HALF_PLACEHOLDER_NODE_HEIGHT =
export const DROPDOWN_HORIZONTAL_OFFSET = 28; export const DROPDOWN_HORIZONTAL_OFFSET = 28;
export const DROPDOWN_VERTICAL_OFFSET = 74; export const DROPDOWN_VERTICAL_OFFSET = 74;
export const PREVENT_CLOSE_DELAY = 300; export const PREVENT_CLOSE_DELAY = 300;
export enum JsonSchemaDataType {
String = 'string',
Number = 'number',
Boolean = 'boolean',
Array = 'array',
Object = 'object',
}

View File

@ -36,6 +36,7 @@ import {
useShowSecondaryMenu, useShowSecondaryMenu,
} from '@/pages/agent/hooks/use-build-structured-output'; } from '@/pages/agent/hooks/use-build-structured-output';
import { useBuildQueryVariableOptions } from '@/pages/agent/hooks/use-get-begin-query'; import { useBuildQueryVariableOptions } from '@/pages/agent/hooks/use-get-begin-query';
import { hasJsonSchemaChild } from '@/pages/agent/utils/filter-agent-structured-output';
import { PromptIdentity } from '../../agent-form/use-build-prompt-options'; import { PromptIdentity } from '../../agent-form/use-build-prompt-options';
import { StructuredOutputSecondaryMenu } from '../structured-output-secondary-menu'; import { StructuredOutputSecondaryMenu } from '../structured-output-secondary-menu';
import { ProgrammaticTag } from './constant'; import { ProgrammaticTag } from './constant';
@ -109,6 +110,10 @@ function VariablePickerMenuItem({
if (shouldShowSecondary) { if (shouldShowSecondary) {
const filteredStructuredOutput = filterStructuredOutput(x.value); const filteredStructuredOutput = filterStructuredOutput(x.value);
if (!hasJsonSchemaChild(filteredStructuredOutput)) {
return null;
}
return ( return (
<StructuredOutputSecondaryMenu <StructuredOutputSecondaryMenu
key={x.value} key={x.value}

View File

@ -29,6 +29,7 @@ import {
useFindAgentStructuredOutputLabel, useFindAgentStructuredOutputLabel,
useShowSecondaryMenu, useShowSecondaryMenu,
} from '../../hooks/use-build-structured-output'; } from '../../hooks/use-build-structured-output';
import { hasJsonSchemaChild } from '../../utils/filter-agent-structured-output';
import { StructuredOutputSecondaryMenu } from './structured-output-secondary-menu'; import { StructuredOutputSecondaryMenu } from './structured-output-secondary-menu';
type Item = { type Item = {
@ -156,7 +157,13 @@ export function GroupedSelectWithSecondaryMenu({
if (shouldShowSecondary) { if (shouldShowSecondary) {
const filteredStructuredOutput = filterStructuredOutput( const filteredStructuredOutput = filterStructuredOutput(
option.value, option.value,
type,
); );
if (!hasJsonSchemaChild(filteredStructuredOutput)) {
return null;
}
return ( return (
<StructuredOutputSecondaryMenu <StructuredOutputSecondaryMenu
key={option.value} key={option.value}

View File

@ -5,10 +5,10 @@ import {
HoverCardTrigger, HoverCardTrigger,
} from '@/components/ui/hover-card'; } from '@/components/ui/hover-card';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
import { get, isPlainObject } from 'lodash'; import { get, isEmpty, isPlainObject } from 'lodash';
import { ChevronRight } from 'lucide-react'; import { ChevronRight } from 'lucide-react';
import { PropsWithChildren, ReactNode, useCallback } from 'react'; import { PropsWithChildren, ReactNode, useCallback } from 'react';
import { VariableType } from '../../constant'; import { JsonSchemaDataType, VariableType } from '../../constant';
type DataItem = { label: ReactNode; value: string; parentLabel?: ReactNode }; type DataItem = { label: ReactNode; value: string; parentLabel?: ReactNode };
@ -16,8 +16,9 @@ type StructuredOutputSecondaryMenuProps = {
data: DataItem; data: DataItem;
click(option: { label: ReactNode; value: string }): void; click(option: { label: ReactNode; value: string }): void;
filteredStructuredOutput: JSONSchema; filteredStructuredOutput: JSONSchema;
type?: VariableType; type?: VariableType | JsonSchemaDataType;
} & PropsWithChildren; } & PropsWithChildren;
export function StructuredOutputSecondaryMenu({ export function StructuredOutputSecondaryMenu({
data, data,
click, click,
@ -34,6 +35,12 @@ export function StructuredOutputSecondaryMenu({
[click, type], [click, type],
); );
const handleMenuClick = useCallback(() => {
if (isEmpty(type) || type === JsonSchemaDataType.Object) {
click(data);
}
}, [click, data, type]);
const renderAgentStructuredOutput = useCallback( const renderAgentStructuredOutput = useCallback(
(values: any, option: { label: ReactNode; value: string }) => { (values: any, option: { label: ReactNode; value: string }) => {
if (isPlainObject(values) && 'properties' in values) { if (isPlainObject(values) && 'properties' in values) {
@ -56,7 +63,7 @@ export function StructuredOutputSecondaryMenu({
{key} {key}
<span className="text-text-secondary">{dataType}</span> <span className="text-text-secondary">{dataType}</span>
</div> </div>
{dataType === 'object' && {dataType === JsonSchemaDataType.Object &&
renderAgentStructuredOutput(value, nextOption)} renderAgentStructuredOutput(value, nextOption)}
</li> </li>
); );
@ -74,7 +81,7 @@ export function StructuredOutputSecondaryMenu({
<HoverCard key={data.value} openDelay={100} closeDelay={100}> <HoverCard key={data.value} openDelay={100} closeDelay={100}>
<HoverCardTrigger asChild> <HoverCardTrigger asChild>
<li <li
onClick={() => click(data)} onClick={handleMenuClick}
className="hover:bg-bg-card py-1 px-2 text-text-primary rounded-sm text-sm flex justify-between items-center" className="hover:bg-bg-card py-1 px-2 text-text-primary rounded-sm text-sm flex justify-between items-center"
> >
{data.label} <ChevronRight className="size-3.5 text-text-secondary" /> {data.label} <ChevronRight className="size-3.5 text-text-secondary" />

View File

@ -26,12 +26,10 @@ export function useShowSecondaryMenu() {
} }
export function useFilterStructuredOutputByValue() { export function useFilterStructuredOutputByValue() {
const { getOperatorTypeFromId, getNode, clickedNodeId } = useGraphStore( const { getNode } = useGraphStore((state) => state);
(state) => state,
);
const filterStructuredOutput = useCallback( const filterStructuredOutput = useCallback(
(value: string) => { (value: string, type?: string) => {
const node = getNode(getNodeId(value)); const node = getNode(getNodeId(value));
const structuredOutput = get( const structuredOutput = get(
node, node,
@ -40,12 +38,12 @@ export function useFilterStructuredOutputByValue() {
const filteredStructuredOutput = filterAgentStructuredOutput( const filteredStructuredOutput = filterAgentStructuredOutput(
structuredOutput, structuredOutput,
getOperatorTypeFromId(clickedNodeId), type,
); );
return filteredStructuredOutput; return filteredStructuredOutput;
}, },
[clickedNodeId, getNode, getOperatorTypeFromId], [getNode],
); );
return filterStructuredOutput; return filterStructuredOutput;

View File

@ -1,6 +1,6 @@
import { JSONSchema } from '@/components/jsonjoy-builder'; import { JSONSchema } from '@/components/jsonjoy-builder';
import { Operator } from '@/constants/agent'; import { get, isPlainObject } from 'lodash';
import { isPlainObject } from 'lodash'; import { JsonSchemaDataType } from '../constant';
// Loop operators can only accept variables of type list. // Loop operators can only accept variables of type list.
@ -8,6 +8,7 @@ import { isPlainObject } from 'lodash';
export function filterLoopOperatorInput( export function filterLoopOperatorInput(
structuredOutput: JSONSchema, structuredOutput: JSONSchema,
type: string,
path = [], path = [],
) { ) {
if (typeof structuredOutput === 'boolean') { if (typeof structuredOutput === 'boolean') {
@ -23,9 +24,9 @@ export function filterLoopOperatorInput(
(pre, [key, value]) => { (pre, [key, value]) => {
if ( if (
typeof value !== 'boolean' && typeof value !== 'boolean' &&
(value.type === 'array' || hasArrayChild(value)) (value.type === type || hasArrayChild(value))
) { ) {
pre[key] = filterLoopOperatorInput(value, path); pre[key] = filterLoopOperatorInput(value, type, path);
} }
return pre; return pre;
}, },
@ -40,7 +41,7 @@ export function filterLoopOperatorInput(
export function filterAgentStructuredOutput( export function filterAgentStructuredOutput(
structuredOutput: JSONSchema, structuredOutput: JSONSchema,
operator?: string, type?: string,
) { ) {
if (typeof structuredOutput === 'boolean') { if (typeof structuredOutput === 'boolean') {
return structuredOutput; return structuredOutput;
@ -49,8 +50,8 @@ export function filterAgentStructuredOutput(
structuredOutput.properties && structuredOutput.properties &&
isPlainObject(structuredOutput.properties) isPlainObject(structuredOutput.properties)
) { ) {
if (operator === Operator.Iteration) { if (type) {
return filterLoopOperatorInput(structuredOutput); return filterLoopOperatorInput(structuredOutput, type);
} }
return structuredOutput; return structuredOutput;
@ -59,13 +60,16 @@ export function filterAgentStructuredOutput(
return structuredOutput; return structuredOutput;
} }
export function hasArrayChild(data: Record<string, any> | Array<any>) { export function hasSpecificTypeChild(
data: Record<string, any> | Array<any>,
type: string,
) {
if (Array.isArray(data)) { if (Array.isArray(data)) {
for (const value of data) { for (const value of data) {
if (isPlainObject(value) && value.type === 'array') { if (isPlainObject(value) && value.type === type) {
return true; return true;
} }
if (hasArrayChild(value)) { if (hasSpecificTypeChild(value, type)) {
return true; return true;
} }
} }
@ -73,11 +77,11 @@ export function hasArrayChild(data: Record<string, any> | Array<any>) {
if (isPlainObject(data)) { if (isPlainObject(data)) {
for (const value of Object.values(data)) { for (const value of Object.values(data)) {
if (isPlainObject(value) && value.type === 'array') { if (isPlainObject(value) && value.type === type) {
return true; return true;
} }
if (hasArrayChild(value)) { if (hasSpecificTypeChild(value, type)) {
return true; return true;
} }
} }
@ -85,3 +89,12 @@ export function hasArrayChild(data: Record<string, any> | Array<any>) {
return false; return false;
} }
export function hasArrayChild(data: Record<string, any> | Array<any>) {
return hasSpecificTypeChild(data, JsonSchemaDataType.Array);
}
export function hasJsonSchemaChild(data: JSONSchema) {
const properties = get(data, 'properties') ?? {};
return isPlainObject(properties) && Object.keys(properties).length > 0;
}