Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Fuel Business Growth with Passio's AI Advisor Modules in Nutrition
export interface PassioAdvisorResponse {
/* Unique identifier for the thread */
threadId: string
/* Unique identifier for the message */
messageId: string
/* Raw content of the message */
rawContent: string
/* Content of the message with markup */
markupContent: string
/* Optional array of tools used */
tools?: string[]
/* Optional array of food ingredients (mostly received on sendImage response) */
extractedIngredients?: PassioAdvisorFoodInfo[]
}
import type { PassioAdvisorResponse } from './PassioAdvisorResponse'
export type PassioAdvisorMessageResponseError = {
status: 'Error'
message: string
}
export type PassioAdvisorMessageResponseSuccess = {
status: 'Success'
response: PassioAdvisorResponse
}
export type PassioAdvisorMessageResultStatus =
| PassioAdvisorMessageResponseError
| PassioAdvisorMessageResponseSuccess
Sends a text message to the Nutrition Advisor.
/**
* Sends a text message to the Nutrition Advisor.
* @param message - The message to be sent.
* @returns A Promise resolving to the status of the message sending.
*/
sendMessage(message: string): Promise<PassioAdvisorMessageResultStatus | null>
import { NutritionAdvisor } from '@passiolife/nutritionai-react-native-sdk-v3'
const response = await NutritionAdvisor.sendMessage(message)
switch (response?.status) {
case 'Success':
// get your response here {response.response}
return
case 'Error':
// get your erorr response here {response.message}
return
}
it's returns PassioAdvisorMessageResultStatus
export type messageType = 'text' | 'image' | 'response' | 'typing'
export interface ChatEntity {
type: messageType
message?: string
uri?: string
response?: PassioAdvisorResponse | null
}
export type SDKStatus = 'Success' | 'Error' | 'Init'
import { useEffect, useState } from 'react'
import {
NutritionAdvisor,
PassioAdvisorMessageResultStatus,
} from '@passiolife/nutritionai-react-native-sdk-v3'
export const useNutritionAdvisor = () => {
const [configureStatus, setConfigureStatus] = useState<SDKStatus>('Init')
const [messages, setMessage] = useState<ChatEntity[]>([])
useEffect(() => {
const initializeNutritionAdvisor = async () => {
try {
const conversationResponse = await NutritionAdvisor.initConversation()
setConfigureStatus(
conversationResponse?.status === 'Success' ? 'Success' : 'Error'
) }
} catch (err) {
console.error(`PassioSDK Error ${err}`)
setConfigureStatus('Error')
}
}
initializeNutritionAdvisor()
}, [])
const handleAdvisorResponse = async (
response: PassioAdvisorMessageResultStatus | null,
message: string | undefined
) => {
setMessage((item) => {
if (response?.status === 'Success') {
const chatResponse: ChatEntity = {
type: 'response',
response: response.response,
}
return [...item.filter((it) => it.type !== 'typing'), chatResponse]
} else {
const chatResponse: ChatEntity = {
type: 'response',
response: null,
message: message,
}
return [...item.filter((it) => it.type !== 'typing'), chatResponse]
}
})
}
const sendMessage = async (message: string) => {
setMessage((item) => {
return [
...item,
{
type: 'text',
message: message,
},
{
type: 'typing',
},
]
})
const response = await NutritionAdvisor.sendMessage(message)
handleAdvisorResponse(response, message)
}
const sendImage = async (imgUrl: string) => {
setMessage((item) => {
return [
...item,
{
type: 'image',
uri: imgUrl,
},
{
type: 'typing',
},
]
})
const response = await NutritionAdvisor.sendImage(imgUrl)
handleAdvisorResponse(response, imgUrl)
}
return {
configureStatus,
messages,
sendMessage,
sendImage,
}
}
Sends an image to the Nutrition Advisor.
/**
* Sends an image to the Nutrition Advisor.
* @param imageURI - The local URI of the image to be sent.
* @returns A Promise resolving to the status of the image sending.
*/
sendImage(imageURI: string): Promise< | null>
import { NutritionAdvisor } from '@passiolife/nutritionai-react-native-sdk-v3'
const response = await NutritionAdvisor.sendImage(imageURI)
switch (response?.status) {
case 'Success':
// get your response here {response.response}
return
case 'Error':
// get your erorr response here {response.message}
return
}
it's returns PassioAdvisorMessageResultStatus
export type PassioAdvisorResultError = {
status: 'Error'
message: string
}
export type PassioAdvisorResultSuccess = {
status: 'Success'
}
export type PassioAdvisorResultStatus =
| PassioAdvisorResultSuccess
| PassioAdvisorResultError
/**
* Initiates a conversation with the Nutrition Advisor.
* @returns A Promise resolving to the status of the conversation initiation.
*/
initConversation(): Promise<PassioAdvisorResultStatus | null>
import { NutritionAdvisor } from '@passiolife/nutritionai-react-native-sdk-v3'
export type SDKStatus = 'Success' | 'Error' | 'Init'
const [loadingState, setLoadingState] = useState<SDKStatus>('Init')
useEffect(() => {
async function configure() {
try {
NutritionAdvisor.initConversation()
.then((item) => {
setLoadingState(
item?.status === 'Success' ? 'Success' : 'Error'
)
})
.catch((error) => {
setLoadingState(error)
})
} catch (err) {
console.error(`PassioSDK Error ${err}`)
setLoadingState('Error')
}
}
configure()
}, [])