-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial simple script logic (#4)
- Loading branch information
1 parent
add884c
commit ce54e72
Showing
6 changed files
with
314 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ node_modules/ | |
.idea/ | ||
dist/ | ||
.DS_Store | ||
*.rc | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,118 @@ | ||
import * as readline from "node:readline"; | ||
|
||
import { | ||
AiEngine, | ||
isAgentMessage, | ||
isAiEngineMessage, | ||
isConfirmationMessage, | ||
isStopMessage, | ||
isTaskSelectionMessage, | ||
} from "@fetchai/ai-engine-sdk"; | ||
|
||
const apiBaseUrl = "https://engine-staging.sandbox-london-b.fetch-ai.com"; | ||
const apiKey = process.env["API_KEY"] ?? ""; | ||
const apiKey = process.env["AV_API_KEY"] ?? ""; | ||
|
||
const snooze = (ms: number) => | ||
new Promise((resolve) => setTimeout(resolve, ms)); | ||
|
||
const main = async () => { | ||
const aiEngine = new AiEngine(apiKey, { apiBaseUrl }); | ||
|
||
const session = await aiEngine.createSession(); | ||
await session.start("Find a holiday destination"); | ||
|
||
let emptyCount = 0; | ||
while (emptyCount < 12) { | ||
const messages = await session.getMessages(); | ||
if (messages.length === 0) { | ||
emptyCount++; | ||
} else { | ||
emptyCount = 0; | ||
} | ||
const rl = readline.promises.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout, | ||
}); | ||
|
||
console.log("MSGS", messages); | ||
|
||
for (const message of messages) { | ||
if (isTaskSelectionMessage(message)) { | ||
console.log("TASK", message); | ||
await session.submitTaskSelection(message, [message.options[1]!]); | ||
} else if (isAgentMessage(message)) { | ||
console.log("AGENT", message); | ||
|
||
if (message.text.includes("please provide the request id")) { | ||
await session.submitResponse(message, "14"); | ||
} else if (message.text.includes("please provide your email address")) { | ||
await session.submitResponse(message, "edward.fitzgerald@fetch.ai"); | ||
} else if (message.text.includes("please provide your full name")) { | ||
await session.submitResponse(message, "Edward FitzGerald"); | ||
} else if (message.text.includes("please provide your response")) { | ||
await session.submitResponse(message, "I am happy with the result"); | ||
} | ||
} else if (isAiEngineMessage(message)) { | ||
console.log("ENGINE", message); | ||
} else if (isConfirmationMessage(message)) { | ||
console.log("CONFIRM", message); | ||
await session.submitConfirmation(message); | ||
const aiEngine = new AiEngine(apiKey); | ||
|
||
const functionGroups = await aiEngine.getFunctionGroups(); | ||
const publicGroup = functionGroups.find((g) => g.name === "Fetch Verified"); | ||
if (publicGroup === undefined) { | ||
throw new Error('Could not find "Public" function group.'); | ||
} | ||
|
||
const session = await aiEngine.createSession(publicGroup.uuid); | ||
await session.start(await rl.question("What is your objective: ")); | ||
|
||
try { | ||
let emptyCount = 0; | ||
let sessionEnded = false; | ||
while (emptyCount < 12) { | ||
const messages = await session.getMessages(); | ||
if (messages.length === 0) { | ||
emptyCount++; | ||
} else { | ||
console.log("???", message); | ||
emptyCount = 0; | ||
} | ||
} | ||
|
||
// console.log("Chilling for 3 seconds..."); | ||
await snooze(1200); | ||
} | ||
for (const message of messages) { | ||
if (isTaskSelectionMessage(message)) { | ||
console.log("Please select a task from the list below:"); | ||
console.log(""); | ||
for (const option of message.options) { | ||
console.log(`${option.key}: ${option.title}`); | ||
} | ||
|
||
const optionIndex = parseInt( | ||
await rl.question("\nEnter task number: "), | ||
); | ||
|
||
// check the index | ||
if (optionIndex < 0 || optionIndex >= message.options.length) { | ||
throw new Error("Invalid task number"); | ||
} | ||
|
||
await session.delete(); | ||
await session.submitTaskSelection(message, [ | ||
message.options[optionIndex]!, | ||
]); | ||
} else if (isAgentMessage(message)) { | ||
console.log("Agent: ", message.text); | ||
|
||
console.log(aiEngine); | ||
console.log(session); | ||
const response = await rl.question("User (enter to skip): "); | ||
if (response === "exit") { | ||
break; | ||
} | ||
|
||
if (response !== "") { | ||
await session.submitResponse(message, response); | ||
} | ||
} else if (isAiEngineMessage(message)) { | ||
console.log("Engine:", message.text); | ||
} else if (isConfirmationMessage(message)) { | ||
console.log("Confirm:", message.payload); | ||
|
||
const response = await rl.question( | ||
"\nPress enter to confirm, otherwise explain issue:\n", | ||
); | ||
|
||
if (response === "") { | ||
console.log("Sending confirmation..."); | ||
await session.submitConfirmation(message); | ||
} else { | ||
await session.rejectConfirmation(message, response); | ||
} | ||
} else if (isStopMessage(message)) { | ||
console.log("\nSession has ended"); | ||
sessionEnded = true; | ||
break; | ||
} else { | ||
console.error("???", message); | ||
} | ||
} | ||
|
||
// if the session has concluded then break | ||
if (sessionEnded) { | ||
break; | ||
} | ||
|
||
await snooze(1200); | ||
} | ||
} catch (e) { | ||
console.error("Error", e); | ||
} finally { | ||
// clean up the session | ||
await session.delete(); | ||
|
||
// stop the readline interface | ||
rl.close(); | ||
} | ||
}; | ||
|
||
main(); |
Oops, something went wrong.