@elimeleth/vaincentflow
v1.8.2-alpha.10
Published
Build assistant faster and easily
Downloads
46
Readme
import "dotenv/config"
import { Workflow, GlobalRouter } from "@elimeleth/vaincentflow"
import { MetaProvider } from "@builderbot/provider-meta"
const general = new Workflow(/.*/gim)
.addAction(async (ctx, { end, state, send, extensions: { provider, assistant } }) => {
const controller = new AbortController()
try {
if (ctx.caption) ctx.body = ctx.caption
const combinedMessage = ctx.messages.join(' ');
let thread_id: string = await state.get("thread_id");
if (!thread_id) {
const thread = await assistant.create_thread()
thread_id = thread.id
await state.update("thread_id", thread_id)
}
let answer = await assistant.invoke({
signal: controller.signal,
thread_id: thread_id,
message: ctx.body,
metadata: {
assistant_name: process.env.IMAGE
},
truncation_strategy: {
type: "last_messages",
last_messages: 4
},
tool_choice: "auto"
}, {
delay: 1500,
logger: console.log,
retries: 3
}) as { output: string, usage: any }
let chunks = answer.output.split("\n\n").filter(a => Boolean(a))
chunks = [...new Set(chunks)]
// typing ...
for (const chunk of chunks) {
if (chunk.length) {
const hasImage = hasImageUrl(chunk) // validate has image
if (hasImage?.isValid) {
try {
await send([
{
body: hasImage.body.replace(/(image:|imagen:)/gim, "").trim(),
media: hasImage.url
}
])
} catch (error) {
await send(chunk)
}
} else {
await send(chunk)
}
}
}
} catch (error) {
controller.abort();
return await end();
}
})
const router = new GlobalRouter({
provider: new MetaProvider({
jwtToken: process.env.JWT_TOKEN,
numberId: process.env.NUMBER_ID,
verifyToken: process.env.VERIFY_TOKEN,
version: "v20.0"
}),
donwloadConf: { auto: true }
llm: { provider: "openai", multimodal: { capture_intentions: true }},
openai_assistant: {
apiKey: process.env.OPENAI_API_KEY,
functions: [search]
},
pauseFn: {
pause: async (ctx) => {
if (ctx.key?.remoteJid) {
// someting logic
return true
}
return false
},
is_pause: async (ctx) => {
// someting logic return true or false
return false
}
}
})
router.addFlows([general]).listen();