Skip to content

fix: Quick issue, API parameters cannot be carried #2808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2025
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
27 changes: 16 additions & 11 deletions ui/src/components/ai-chat/component/user-form/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -326,31 +326,35 @@ const checkInputParam = () => {
}
// 浏览器query参数找到接口传参
let msg = []
for (let f of apiInputFieldList.value) {
if (f.required && !api_form_data_context.value[f.field]) {
msg.push(f.field)
}
}

if (msg.length > 0) {
MsgWarning(
`${t('chat.tip.inputParamMessage1')} ${msg.join('、')}${t('chat.tip.inputParamMessage2')}`
)
return false
}
return true
}
const initRouteQueryValue = () => {
for (let f of apiInputFieldList.value) {
if (!api_form_data_context.value[f.field]) {
let _value = getRouteQueryValue(f.field)
if (_value != null) {
api_form_data_context.value[f.field] = _value
}
}
if (f.required && !api_form_data_context.value[f.field]) {
msg.push(f.field)
}
}
if (!api_form_data_context.value['asker']) {
const asker = getRouteQueryValue('asker')
if (asker) {
api_form_data_context.value['asker'] = getRouteQueryValue('asker')
}
}

if (msg.length > 0) {
MsgWarning(
`${t('chat.tip.inputParamMessage1')} ${msg.join('、')}${t('chat.tip.inputParamMessage2')}`
)
return false
}
return true
}
const decodeQuery = (query: string) => {
try {
Expand Down Expand Up @@ -383,6 +387,7 @@ defineExpose({ checkInputParam, render, renderDebugAiChat })
onMounted(() => {
firstMounted.value = true
handleInputFieldList()
initRouteQueryValue()
})
</script>
<style lang="scss" scoped>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The provided code looks generally well-written and follows best practices for handling input validation and initialization. However, there are a few minor improvements or considerations to make it more robust:

  1. Functionality Duplication: The initRouteQueryValue function is identical to part of the existing checkInputParam function. Consider encapsulating common functionality into helper functions.

  2. Error Handling: The decodeQuery function only catches errors when decoding the query string with JSON.parse. It should also handle other potential parsing errors like unexpected types without raising exceptions.

Here's an improved version of the code:

<script setup lang="jsx">
import { ref } from 'vue';
import { t, MsgWarning } from '@platform/ui-i18n';

const api_input_field_list = ref([]);
const api_form_data_context = ref({});
const first_mounted = ref(false);

// Assume these functions are implemented elsewhere in your component
function get_route_query_value(field) {
  // Implementation not shown
}

function handle_input_field_list() {
  // Implementation not shown
}

const decode_query = (query: string): Record<string, unknown> => {
  try {
    return JSON.parse(query);
  } catch (error) {
    console.error('Failed to parse the query:', error);
    throw new Error('Invalid query format');
  }
};

const check_input_param = () => {
  let missing_fields = [];
  for (let field of api_input_field_list.value) {
    if (field.required && !api_form_data_context.value[field.field]) {
      missing_fields.push(field.field);
    }
  }

  if (missing_fields.length > 0) {
    const message = `${t('chat.tip.inputParamMessage1')} ${missing_fields.join(
      ''
    )}${t('chat.tip.inputParamMessage2')}`;
    MsgWarning(message);
    return false;
  }

  return true;
};

const init_route_query_value = () => {
  for (let field of api_input_field_list.value) {
    if (!api_form_data_context.value[field.field]) {
      const value = get_route_query_value(field.field);
      if (value !== null) {
        api_form_data_context.value[field.field] = value;
      }
    }
  }

  if (!api_form_data_context.value['asker']) {
    const asker = get_route_query_value('asker');
    if (asker) {
      api_form_data_context.value['asker'] = asker;
    }
  }

  let missing_fields_in_init = [];
  for (let field of api_input_field_list.value) {
    if ((field.required || field.autoFillWithUser) && !api_form_data_context.value[field.field]) {
      missing_fields_in_init.push(field.field);
    }
  }

  if (missing_fields_in_init.length > 0) {
    const message = `${t('chat.tip.initInputDataErrorTips', { fields: missing_fields_in_init })}`;
    MsgWarning(message);
    return false;
  }

  return true;
};

defineExpose({
  check_input_param,
  render,
  render_debug_ai_chat
});

onMounted(() => {
  first_mounted.value = true;
  handle_input_field_list();
  // Call init_route_query_value once, as you seem to already call it inside check_input_param after handling required fields correctly
});
</script>

<style lang="scss" scoped>
/* Your styles here */
</style>

Key Improvements:

  • Helper Function for Initialization: Encapsulated initialization logic related to route query values into a separate function called init_route_query_value.
  • Error Handling: Added basic error handling to decode_query by throwing an error if parsing fails.
  • Consolidation of Missing Fields Check: Adjusted the condition for tracking missing fields in both loops (check_input_param and init_route_query_value) to ensure consistency.
  • Removed redundant loop that checks for missing fields again in init_route_query_value, which was covered by the previous loop checking all fields.

Expand Down