Spaces:
Sleeping
Sleeping
Download runtime-dist/runtime/simpleAgent.js from kenken999/gemikenwork: direct link, hf CLI and curl.
- Browser
- Download file 3.04 kB
-
https://huggingface.co/spaces/kenken999/gemikenwork/resolve/main/runtime-dist/runtime/simpleAgent.js
- Command line
-
hf download hf://spaces/kenken999/gemikenwork/runtime-dist/runtime/simpleAgent.js
-
curl -L -o simpleAgent.js https://huggingface.co/spaces/kenken999/gemikenwork/resolve/main/runtime-dist/runtime/simpleAgent.js
3.04 kB
| export class SimpleAgentRuntime { | |
| agentName; | |
| respondToAllBubbleMessages; | |
| enableVoiceReplies; | |
| hooks; | |
| disclosedSessions = new Set(); | |
| constructor(options = {}) { | |
| this.agentName = options.agentName ?? process.env.WA_AGENT_NAME ?? "AI Agent"; | |
| this.respondToAllBubbleMessages = options.respondToAllBubbleMessages ?? process.env.WA_AGENT_RESPOND_TO_ALL === "true"; | |
| this.enableVoiceReplies = options.enableVoiceReplies ?? process.env.WA_AGENT_ENABLE_VOICE !== "false"; | |
| this.hooks = options.hooks ?? []; | |
| } | |
| async handleObservation(session, observation, enqueue, synthesizeSpeech) { | |
| for (const hook of this.hooks) { | |
| await hook.afterObservation?.(observation, session); | |
| } | |
| if (observation.type !== "chatMessage" || observation.scope !== "bubble") { | |
| return; | |
| } | |
| if (!this.shouldRespond(observation)) { | |
| return; | |
| } | |
| const reply = this.makeReply(session.id, observation); | |
| await this.enqueueWithHooks({ type: "startTyping", channel: "bubble" }, session, enqueue); | |
| await new Promise((resolve) => setTimeout(resolve, Math.min(900, Math.max(250, reply.length * 12)))); | |
| await this.enqueueWithHooks({ type: "say", channel: "bubble", text: reply }, session, enqueue); | |
| await this.enqueueWithHooks({ type: "stopTyping", channel: "bubble" }, session, enqueue); | |
| if (this.enableVoiceReplies && synthesizeSpeech) { | |
| try { | |
| await synthesizeSpeech(reply); | |
| } | |
| catch (error) { | |
| console.error("Failed to synthesize voice reply", error); | |
| } | |
| } | |
| } | |
| shouldRespond(observation) { | |
| const authorName = observation.author?.name?.toLowerCase(); | |
| if (authorName && authorName === this.agentName.toLowerCase()) { | |
| return false; | |
| } | |
| if (this.respondToAllBubbleMessages) { | |
| return true; | |
| } | |
| const normalized = observation.message.toLowerCase(); | |
| return normalized.includes(this.agentName.toLowerCase()) || normalized.includes("@agent") || normalized.includes("ai agent"); | |
| } | |
| makeReply(sessionId, observation) { | |
| const prefix = this.disclosedSessions.has(sessionId) ? "" : "I am an AI agent. "; | |
| this.disclosedSessions.add(sessionId); | |
| const author = observation.author?.name ? `${observation.author.name}, ` : ""; | |
| return `${prefix}${author}I heard: "${observation.message}". This prototype is wired for WorkAdventure text and voice responses.`; | |
| } | |
| async enqueueWithHooks(action, session, enqueue) { | |
| let nextAction = action; | |
| for (const hook of this.hooks) { | |
| if (!nextAction) { | |
| return; | |
| } | |
| if (hook.beforeAction) { | |
| nextAction = await hook.beforeAction(nextAction, session); | |
| } | |
| } | |
| if (nextAction) { | |
| enqueue(nextAction); | |
| } | |
| } | |
| } | |
| //# sourceMappingURL=simpleAgent.js.map |