Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 34 additions & 16 deletions ui/src/workflow/nodes/knowledge-write-node/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,62 @@
label-position="top"
require-asterisk-position="right"
label-width="auto"
ref="IntentClassifyNodeFormRef"
ref="KnowledgeWriteRef"
hide-required-asterisk
>
<el-form-item
prop="document_list"
:label="$t('common.inputContent')"
:rules="{
prop="document_list"
:label="$t('common.inputContent')"
:rules="{
message: $t('workflow.nodes.textToSpeechNode.content.label'),
trigger: 'change',
required: true,
}"
>
<template #label>
}"
>
<template #label>
<div class="flex-between">
<div>
<span
>{{ $t('common.inputContent')
}}<span class="color-danger">*</span></span
>
<span>{{ $t('common.inputContent') }}<span class="color-danger">*</span></span>
</div>
</div>
</template>
<NodeCascader
</template>
<NodeCascader
ref="nodeCascaderRef"
:nodeModel="nodeModel"
class="w-full"
:placeholder="$t('workflow.nodes.textToSpeechNode.content.label')"
v-model="form_data.document_list"
/>
</el-form-item>
</el-form-item>
</el-form>
</el-card>
</NodeContainer>
</template>

<script setup lang="ts">
import NodeContainer from '@/workflow/common/NodeContainer.vue'
import { computed } from 'vue'
import { computed, onMounted, ref } from 'vue'
import { set } from 'lodash'
import NodeCascader from '@/workflow/common/NodeCascader.vue'
import { isLastNode } from '@/workflow/common/data'

const props = defineProps<{ nodeModel: any }>()

const KnowledgeWriteRef = ref()
const nodeCascaderRef = ref()
const form = {
document_list: [],
}

const validate = async () => {
let ps = [
KnowledgeWriteRef.value?.validate(),
nodeCascaderRef.value ? nodeCascaderRef.value.validate() : Promise.resolve(''),
]
return Promise.all(ps).catch((err) => {
return Promise.reject({ node: props.nodeModel, errMessage: err })
})
}

const form_data = computed({
get: () => {
if (props.nodeModel.properties.node_data) {
Expand All @@ -68,6 +77,15 @@ const form_data = computed({
set(props.nodeModel.properties, 'node_data', value)
},
})

onMounted(() => {
if (typeof props.nodeModel.properties.node_data?.is_result === 'undefined') {
if (isLastNode(props.nodeModel)) {
set(props.nodeModel.properties.node_data, 'is_result', true)
}
}
set(props.nodeModel, 'validate', validate)
})
</script>

<style lang="scss" scoped></style>
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a few improvements that can be made to enhance the code:

  1. Remove Redundant Code: The set function call in the computed property can be simplified using direct assignment.

  2. Optimize Event Handling: Ensure all event handlers are properly bound and optimized, especially on component initialization.

  3. Code Readability: Add comments where needed for better understanding of the logic flow.

Here's the updated version with these improvements:

<!-- Updated Form Template -->
<el-form
  label-position="top"
  require-asterisk-position="right"
  label-width="auto"
  ref="IntentClassifyNodeFormRef"
  hide-required-asterisk
>
  <!-- Input Content Field -->
  <el-form-item
    prop="document_list"
    :label="$t('common.inputContent')"
    :rules="{
      message: $t('workflow.nodes.textToSpeechNode.content.label'),
      trigger: 'change',
      required: true,
    }"
  >
    <template #label>
      <div class="flex-between">
        <div>
          {{ $t('common.inputContent') }}
          <span class="color-danger">*</span>
        </div>
      </div>
    </template>
    <NodeCascader
      ref="nodeCascaderRef"
      :nodeModel="nodeModel"
      class="w-full"
      :placeholder="$t('workflow.nodes.textToSpeechNode.content.label')"
      v-model="form_data.document_list"
    />
  </el-form-item>
</el-form>

Updated Script Setup

<script setup lang="ts">
import NodeContainer from '@/workflow/common/NodeContainer.vue';
import { computed, onMounted, ref } from 'vue';
import { set } from 'lodash';
import NodeCascader from '@/workflow/common/NodeCascader.vue';
import { isLastNode } from '@/workflow/common/data';

const props = defineProps<{ nodeModel: any }>();
// Remove redundant usage of set in form_data computed property
const form_data = computed({
  get: () => {
    return props.nodeModel.properties.node_data || {};
  },
  // Simplified setter
  set(value) {
    set(props.nodeModel.properties, 'node_data', value);
  },
});

+// Function to handle validation
async function validate() {
  let ps = [
    KnowledgeWriteRef.value?.validate(),
    nodeCascaderRef.value ? nodeCascaderRef.value.validate() : Promise.resolve(''),
  ];
  try {
    await Promise.all(ps);
    return null; // Return success after successful validation
  } catch (err) {
    throw new Error({ node: props.nodeModel, errMessage: err });
  }
}

/* Initialization Logic */
onMounted(() => {
  if (!props.nodeModel.properties.node_data.is_result) {
    if (isLastNode(props.nodeModel)) {
      props.nodeModel.properties.node_data.is_result = true;
    }
  }
  // Bind custom validate function to props.nodeModel properties
  set(props.nodeModel, 'validate', validate);
});
</script>

<style lang="scss" scoped></style>

Summary

  • Removed redundancy with setting form data directly.
  • Enhanced readability by adding comments.
  • Added validation logic as a separate function for reusability and clarity.
  • Ensured proper initialization and error handling.

These changes should make the code cleaner, more maintainable, and easier to understand.

Loading