mirror of
https://github.com/langgenius/dify.git
synced 2026-07-26 22:28:32 +08:00
Co-authored-by: yunlu.wen <yunlu.wen@dify.ai> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Yunlu Wen <wylswz@163.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Yanli 盐粒 <yanli@dify.ai> Co-authored-by: 盐粒 Yanli <beautyyuyanli@gmail.com> Co-authored-by: zyssyz123 <916125788@qq.com> Co-authored-by: 盐粒 Yanli <mail@yanli.one>
142 lines
3.9 KiB
TypeScript
142 lines
3.9 KiB
TypeScript
import type { DeclaredOutputConfig } from '@dify/contracts/api/console/apps/types.gen'
|
|
import type { LexicalNode, NodeKey, SerializedLexicalNode } from 'lexical'
|
|
import type { AgentOutputTypeOptionValue } from './utils'
|
|
import { DecoratorNode } from 'lexical'
|
|
import AgentOutputBlockComponent from './component'
|
|
import { getAgentOutputToken } from './utils'
|
|
|
|
type SerializedNode = SerializedLexicalNode & {
|
|
name: string
|
|
outputType: AgentOutputTypeOptionValue
|
|
}
|
|
|
|
export class AgentOutputBlockNode extends DecoratorNode<React.JSX.Element> {
|
|
__name: string
|
|
__outputType: AgentOutputTypeOptionValue
|
|
__isEditing: boolean
|
|
__outputs: DeclaredOutputConfig[]
|
|
__onChange?: (outputs: DeclaredOutputConfig[], prompt?: string) => void
|
|
__onEdit?: (name: string, outputType: AgentOutputTypeOptionValue) => void
|
|
|
|
static override getType(): string {
|
|
return 'agent-output-block'
|
|
}
|
|
|
|
static override clone(node: AgentOutputBlockNode): AgentOutputBlockNode {
|
|
return new AgentOutputBlockNode(
|
|
node.__name,
|
|
node.__outputType,
|
|
node.__isEditing,
|
|
node.__outputs,
|
|
node.__onChange,
|
|
node.__onEdit,
|
|
node.__key,
|
|
)
|
|
}
|
|
|
|
override isInline(): boolean {
|
|
return true
|
|
}
|
|
|
|
constructor(
|
|
name: string,
|
|
outputType: AgentOutputTypeOptionValue,
|
|
isEditing = false,
|
|
outputs: DeclaredOutputConfig[] = [],
|
|
onChange?: (outputs: DeclaredOutputConfig[], prompt?: string) => void,
|
|
onEdit?: (name: string, outputType: AgentOutputTypeOptionValue) => void,
|
|
key?: NodeKey,
|
|
) {
|
|
super(key)
|
|
|
|
this.__name = name
|
|
this.__outputType = outputType
|
|
this.__isEditing = isEditing
|
|
this.__outputs = outputs
|
|
this.__onChange = onChange
|
|
this.__onEdit = onEdit
|
|
}
|
|
|
|
override createDOM(): HTMLElement {
|
|
const div = document.createElement('div')
|
|
div.classList.add('inline-flex', 'items-center', 'align-middle')
|
|
return div
|
|
}
|
|
|
|
override updateDOM(): false {
|
|
return false
|
|
}
|
|
|
|
override decorate(): React.JSX.Element {
|
|
return (
|
|
<AgentOutputBlockComponent
|
|
nodeKey={this.getKey()}
|
|
name={this.getName()}
|
|
outputType={this.getOutputType()}
|
|
isEditing={this.isEditing()}
|
|
outputs={this.getOutputs()}
|
|
onChange={this.getOnChange()}
|
|
onEdit={this.getOnEdit()}
|
|
/>
|
|
)
|
|
}
|
|
|
|
static override importJSON(serializedNode: SerializedNode): AgentOutputBlockNode {
|
|
return $createAgentOutputBlockNode(serializedNode.name, serializedNode.outputType)
|
|
}
|
|
|
|
override exportJSON(): SerializedNode {
|
|
return {
|
|
type: 'agent-output-block',
|
|
version: 1,
|
|
name: this.getName(),
|
|
outputType: this.getOutputType(),
|
|
}
|
|
}
|
|
|
|
getName(): string {
|
|
return this.getLatest().__name
|
|
}
|
|
|
|
getOutputType(): AgentOutputTypeOptionValue {
|
|
return this.getLatest().__outputType
|
|
}
|
|
|
|
isEditing(): boolean {
|
|
return this.getLatest().__isEditing
|
|
}
|
|
|
|
getOutputs(): DeclaredOutputConfig[] {
|
|
return this.getLatest().__outputs
|
|
}
|
|
|
|
getOnChange(): ((outputs: DeclaredOutputConfig[], prompt?: string) => void) | undefined {
|
|
return this.getLatest().__onChange
|
|
}
|
|
|
|
getOnEdit(): ((name: string, outputType: AgentOutputTypeOptionValue) => void) | undefined {
|
|
return this.getLatest().__onEdit
|
|
}
|
|
|
|
override getTextContent(): string {
|
|
return getAgentOutputToken(this.getName())
|
|
}
|
|
}
|
|
|
|
export function $createAgentOutputBlockNode(
|
|
name: string,
|
|
outputType: AgentOutputTypeOptionValue = 'string',
|
|
isEditing = false,
|
|
outputs: DeclaredOutputConfig[] = [],
|
|
onChange?: (outputs: DeclaredOutputConfig[], prompt?: string) => void,
|
|
onEdit?: (name: string, outputType: AgentOutputTypeOptionValue) => void,
|
|
): AgentOutputBlockNode {
|
|
return new AgentOutputBlockNode(name, outputType, isEditing, outputs, onChange, onEdit)
|
|
}
|
|
|
|
export function $isAgentOutputBlockNode(
|
|
node: AgentOutputBlockNode | LexicalNode | null | undefined,
|
|
): node is AgentOutputBlockNode {
|
|
return node instanceof AgentOutputBlockNode
|
|
}
|