|
|
const chalk = require("chalk"); |
|
|
const { Telemetry } = require("../../../../models/telemetry"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const httpSocket = { |
|
|
name: "httpSocket", |
|
|
startupConfig: { |
|
|
params: { |
|
|
handler: { |
|
|
required: true, |
|
|
}, |
|
|
muteUserReply: { |
|
|
required: false, |
|
|
default: true, |
|
|
}, |
|
|
introspection: { |
|
|
required: false, |
|
|
default: true, |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
plugin: function ({ |
|
|
handler, |
|
|
muteUserReply = true, // Do not post messages to "USER" back to frontend. |
|
|
introspection = false, // when enabled will attach socket to Aibitat object with .introspect method which reports status updates to frontend. |
|
|
}) { |
|
|
return { |
|
|
name: this.name, |
|
|
setup(aibitat) { |
|
|
aibitat.onError(async (error) => { |
|
|
let errorMessage = |
|
|
error?.message || "An error occurred while running the agent."; |
|
|
console.error(chalk.red(` error: ${errorMessage}`), error); |
|
|
aibitat.introspect( |
|
|
`Error encountered while running: ${errorMessage}` |
|
|
); |
|
|
handler.send( |
|
|
JSON.stringify({ type: "wssFailure", content: errorMessage }) |
|
|
); |
|
|
aibitat.terminate(); |
|
|
}); |
|
|
|
|
|
aibitat.introspect = (messageText) => { |
|
|
if (!introspection) return; |
|
|
handler.send( |
|
|
JSON.stringify({ type: "statusResponse", content: messageText }) |
|
|
); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
aibitat.socket = { |
|
|
send: (type = "__unhandled", content = "") => { |
|
|
handler.send(JSON.stringify({ type, content })); |
|
|
}, |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
aibitat.onMessage((message) => { |
|
|
if (message.from !== "USER") |
|
|
Telemetry.sendTelemetry("agent_chat_sent"); |
|
|
if (message.from === "USER" && muteUserReply) return; |
|
|
handler.send(JSON.stringify(message)); |
|
|
handler.close(); |
|
|
}); |
|
|
|
|
|
aibitat.onTerminate(() => { |
|
|
handler.close(); |
|
|
}); |
|
|
}, |
|
|
}; |
|
|
}, |
|
|
}; |
|
|
|
|
|
module.exports = { |
|
|
httpSocket, |
|
|
}; |
|
|
|