feat: modify body with ai & feedback on ai requests hoppscotch-common bindings (#4386)
Co-authored-by: jamesgeorge007 <25279263+jamesgeorge007@users.noreply.github.com>
This commit is contained in:
@@ -38,6 +38,8 @@ export const useRequestNameGeneration = (targetNameRef: Ref<string>) => {
|
||||
return ENABLE_AI_EXPERIMENTS.value && !!platform.experiments?.aiExperiments
|
||||
})
|
||||
|
||||
const lastTraceID = ref<string | null>(null)
|
||||
|
||||
const generateRequestName = async (
|
||||
requestContext: HoppRESTRequest | HoppGQLRequest | null
|
||||
) => {
|
||||
@@ -65,7 +67,8 @@ export const useRequestNameGeneration = (targetNameRef: Ref<string>) => {
|
||||
return
|
||||
}
|
||||
|
||||
targetNameRef.value = result.right
|
||||
targetNameRef.value = result.right.request_name
|
||||
lastTraceID.value = result.right.trace_id
|
||||
|
||||
isGenerateRequestNamePending.value = false
|
||||
}
|
||||
@@ -74,5 +77,122 @@ export const useRequestNameGeneration = (targetNameRef: Ref<string>) => {
|
||||
generateRequestName,
|
||||
isGenerateRequestNamePending,
|
||||
canDoRequestNameGeneration,
|
||||
lastTraceID,
|
||||
}
|
||||
}
|
||||
|
||||
export const useAIExperiments = () => {
|
||||
const currentUser = useReadonlyStream(
|
||||
platform.auth.getCurrentUserStream(),
|
||||
platform.auth.getCurrentUser()
|
||||
)
|
||||
|
||||
const ENABLE_AI_EXPERIMENTS = useSetting("ENABLE_AI_EXPERIMENTS")
|
||||
|
||||
const shouldEnableAIFeatures = computed(() => {
|
||||
// Request generation applies only to the authenticated state
|
||||
if (!currentUser.value) {
|
||||
return false
|
||||
}
|
||||
|
||||
return ENABLE_AI_EXPERIMENTS.value && !!platform.experiments?.aiExperiments
|
||||
})
|
||||
|
||||
return {
|
||||
shouldEnableAIFeatures,
|
||||
}
|
||||
}
|
||||
|
||||
export const useModifyRequestBody = (
|
||||
currentRequestBody: string,
|
||||
userPromptRef: Ref<string>,
|
||||
generatedRequestBodyRef: Ref<string>
|
||||
) => {
|
||||
const toast = useToast()
|
||||
const t = useI18n()
|
||||
|
||||
const lastTraceID = ref<string | null>(null)
|
||||
|
||||
const isModifyRequestBodyPending = ref(false)
|
||||
|
||||
const modifyRequestBodyForPlatform =
|
||||
platform.experiments?.aiExperiments?.modifyRequestBody
|
||||
|
||||
const modifyRequestBody = async () => {
|
||||
isModifyRequestBodyPending.value = true
|
||||
|
||||
if (!modifyRequestBodyForPlatform) {
|
||||
toast.error(t("request.modify_request_body_error"))
|
||||
isModifyRequestBodyPending.value = false
|
||||
return
|
||||
}
|
||||
|
||||
const result = await modifyRequestBodyForPlatform(
|
||||
currentRequestBody ?? "",
|
||||
userPromptRef.value
|
||||
)
|
||||
|
||||
if (result && E.isLeft(result)) {
|
||||
toast.error(t("request.modify_request_body_error"))
|
||||
isModifyRequestBodyPending.value = false
|
||||
return
|
||||
}
|
||||
|
||||
generatedRequestBodyRef.value = result.right.modified_body
|
||||
lastTraceID.value = result.right.trace_id
|
||||
|
||||
isModifyRequestBodyPending.value = false
|
||||
return result.right
|
||||
}
|
||||
|
||||
return {
|
||||
modifyRequestBody,
|
||||
isModifyRequestBodyPending,
|
||||
lastTraceID,
|
||||
}
|
||||
}
|
||||
|
||||
export const useSubmitFeedback = () => {
|
||||
const submitFeedbackForPlatform =
|
||||
platform.experiments?.aiExperiments?.submitFeedback
|
||||
|
||||
const t = useI18n()
|
||||
const toast = useToast()
|
||||
|
||||
const isSubmitFeedbackPending = ref(false)
|
||||
|
||||
const submitFeedback = async (
|
||||
rating: "positive" | "negative",
|
||||
traceID: string
|
||||
) => {
|
||||
if (!submitFeedbackForPlatform) {
|
||||
toast.error(t("ai_experiments.feedback_failure"))
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
isSubmitFeedbackPending.value = true
|
||||
|
||||
const res = await submitFeedbackForPlatform(
|
||||
rating === "positive" ? 1 : -1,
|
||||
traceID
|
||||
)
|
||||
|
||||
if (E.isLeft(res)) {
|
||||
toast.error(t("ai_experiments.feedback_failure"))
|
||||
isSubmitFeedbackPending.value = false
|
||||
return
|
||||
}
|
||||
|
||||
isSubmitFeedbackPending.value = false
|
||||
|
||||
toast.success(t("ai_experiments.feedback_success"))
|
||||
|
||||
return E.right(undefined)
|
||||
}
|
||||
|
||||
return {
|
||||
submitFeedback,
|
||||
isSubmitFeedbackPending,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user