Skip to content

Commit

Permalink
refactor(chain): stub out options.sendMessage and options.sendImage i…
Browse files Browse the repository at this point in the history
…f they don't exist
  • Loading branch information
ejfox committed Jul 10, 2024
1 parent 2622c2b commit da07373
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,16 @@ module.exports = (async () => {
logger.info(`[${chainId}] Last message: ${JSON.stringify(lastMessage)}`);

// if options doesn't come with a sendMessage, we need to stub it
if (!options?.sendMessage) {
options?.sendMessage = async (content) => {
if (!options.sendMessage) {
options.sendMessage = async (content) => {
logger.info(`[${chainId}] Stubbed sendMessage: ${content}`);
};
}
if (!options.sendImage) {
options.sendImage = async (image) => {
logger.info(`[${chainId}] Stubbed sendImage: <image data>`);
};
}

if (lastMessage.role === "user") {
await storeUserMessage(
Expand All @@ -117,7 +122,7 @@ module.exports = (async () => {
logger.info(
`[${chainId}] Generated LLM response: ${JSON.stringify(aiResponse)}`
);
await options?.sendMessage(aiResponse.content);
await options.sendMessage(aiResponse.content);
logger.info(`[${chainId}] Sent AI response`);
}

Expand Down Expand Up @@ -171,7 +176,7 @@ module.exports = (async () => {

if (capabilityResult.image) {
await options.sendImage(capabilityResult.image);
await options?.sendMessage(
await options.sendMessage(
`Image generated by ${call.slug}:${call.method}`
);
logger.info(`[${chainId}] Sent image from capability result`);
Expand All @@ -180,7 +185,7 @@ module.exports = (async () => {
capabilityCallCount++;
const aiResponse = await generateLLMResponse(messages, options);
messages.push(aiResponse);
await options?.sendMessage(aiResponse.content);
await options.sendMessage(aiResponse.content);
logger.info(`[${chainId}] Sent AI response after capability`);
}

Expand All @@ -199,13 +204,19 @@ module.exports = (async () => {
error,
chainId
) {
if (!options.sendMessage) {
options.sendMessage = async (content) => {
logger.info(`[${chainId}] Stubbed sendMessage: ${content}`);
};
}

if (retryCount < config.MAX_RETRY_COUNT) {
logger.warn(
`[${chainId}] Error processing message chain, retrying (${
retryCount + 1
}/${config.MAX_RETRY_COUNT}): ${error}`
);
await options?.sendMessage(
await options.sendMessage(
`An error occurred. Retrying (attempt ${retryCount + 1}/${
config.MAX_RETRY_COUNT
})...`
Expand All @@ -220,12 +231,12 @@ module.exports = (async () => {
logger.error(
`[${chainId}] Error processing message chain, maximum retries exceeded: ${error}`
);
await options?.sendMessage(
await options.sendMessage(
"I apologize, but I've encountered multiple errors while processing your request. Here's my best attempt at a response based on our conversation so far:"
);
const finalResponse = await generateFinalResponse(messages, options);
messages.push(finalResponse);
await options?.sendMessage(finalResponse.content);
await options.sendMessage(finalResponse.content);
return { messages, finalContent: finalResponse.content };
}
}
Expand Down

0 comments on commit da07373

Please sign in to comment.