-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: external test #2205
Closed
Closed
feat: external test #2205
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,180 @@ | ||
import { spawn } from "node:child_process"; | ||
import { stringToUuid } from "../packages/core/dist/index.js"; | ||
import path from "path"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
import fs from "fs/promises"; | ||
|
||
export const DEFAULT_CHARACTER = "trump"; | ||
export const DEFAULT_AGENT_ID = stringToUuid(DEFAULT_CHARACTER ?? uuidv4()); | ||
|
||
function projectRoot() { | ||
return path.join(import.meta.dirname, ".."); | ||
// return "/Users/piotr/Documents/GitHub/Sifchain/eliza" | ||
} | ||
|
||
function log(message) { | ||
console.log(message); | ||
} | ||
|
||
function logError(error) { | ||
log("ERROR: " + error.message); | ||
log(error); // Print stack trace | ||
} | ||
|
||
async function runProcess(command, args = [], directory = projectRoot()) { | ||
try { | ||
throw new Error("Not implemented yet"); | ||
// TODO | ||
// const result = await $`cd ${directory} && ${command} ${args}`; | ||
return result.stdout.trim(); | ||
} catch (error) { | ||
throw new Error(`Command failed: ${error.message}`); | ||
} | ||
} | ||
|
||
async function installProjectDependencies() { | ||
log("Installing dependencies..."); | ||
return await runProcess("pnpm", ["install", "-r"]); | ||
} | ||
|
||
async function buildProject() { | ||
log("Building project..."); | ||
return await runProcess("pnpm", ["build"]); | ||
} | ||
|
||
async function writeEnvFile(entries) { | ||
const envContent = Object.entries(entries) | ||
.map(([key, value]) => `${key}=${value}`) | ||
.join("\n"); | ||
await fs.writeFile(".env", envContent); | ||
} | ||
|
||
async function startAgent(character = DEFAULT_CHARACTER) { | ||
log(`Starting agent for character: ${character}`); | ||
const proc = spawn( | ||
"node", | ||
[ | ||
"--loader", | ||
"ts-node/esm", | ||
"src/index.ts", | ||
"--isRoot", | ||
`--character=characters/${character}.character.json`, | ||
"--non-interactive", | ||
], | ||
{ | ||
cwd: path.join(projectRoot(), "agent"), | ||
shell: false, | ||
stdio: "inherit", | ||
} | ||
); | ||
const startTime = Date.now(); | ||
while (true) { | ||
try { | ||
const response = await fetch("http://127.0.0.1:3000/", { | ||
method: "GET", | ||
}); | ||
if (response.ok) break; | ||
} catch (error) {} | ||
if (Date.now() - startTime > 120000) { | ||
throw new Error("Timeout waiting for process to start"); | ||
} else { | ||
await sleep(1000); | ||
} | ||
} | ||
await sleep(1000); | ||
return proc; | ||
} | ||
|
||
async function stopAgent(proc) { | ||
log("Stopping agent..." + JSON.stringify(proc.pid)); | ||
proc.kill(); | ||
const startTime = Date.now(); | ||
while (true) { | ||
if (proc.killed) break; | ||
if (Date.now() - startTime > 60000) { | ||
throw new Error("Timeout waiting for the process to terminate"); | ||
} | ||
await sleep(1000); | ||
} | ||
await sleep(1000); | ||
} | ||
|
||
async function sleep(ms) { | ||
await new Promise((resolve) => setTimeout(resolve, ms)); | ||
} | ||
|
||
async function sendPostRequest(url, method, payload) { | ||
try { | ||
const response = await fetch(url, { | ||
method: method, | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify(payload), | ||
}); | ||
if (!response.ok) | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
const data = await response.json(); | ||
return data; | ||
} catch (error) { | ||
throw new Error(`Failed to send message: ${error.message}`); | ||
} | ||
} | ||
|
||
async function send(message) { | ||
const url = `http://127.0.0.1:3000/${DEFAULT_AGENT_ID}/message`; | ||
return await sendPostRequest(url, "POST", { | ||
text: message, | ||
userId: "user", | ||
userName: "User", | ||
}); | ||
} | ||
|
||
async function runIntegrationTest(fn) { | ||
log(fn); | ||
const skip = fn.hasOwnProperty("skipIf") ? fn.skipIf : false; | ||
if (skip) { | ||
log( | ||
fn.description | ||
? `Skipping test ${fn.description}...` | ||
: "Skipping test..." | ||
); | ||
} else { | ||
log( | ||
fn.description | ||
? `Running test ${fn.description}...` | ||
: "Running test..." | ||
); | ||
const proc = await startAgent(); | ||
try { | ||
await fn(); | ||
log( | ||
fn.description | ||
? `✓ Test ${fn.description} passed` | ||
: "✓ Test passed" | ||
); | ||
} catch (error) { | ||
log( | ||
fn.description | ||
? `✗ Test ${fn.description} failed` | ||
: "✗ Test failed" | ||
); | ||
logError(error); | ||
} finally { | ||
await stopAgent(proc); | ||
} | ||
} | ||
} | ||
|
||
export { | ||
projectRoot, | ||
runProcess, | ||
installProjectDependencies, | ||
buildProject, | ||
writeEnvFile, | ||
startAgent, | ||
stopAgent, | ||
send, | ||
runIntegrationTest, | ||
log, | ||
logError, | ||
sleep, | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
runProcess
function currently throws immediately and contains unreachable code. Consider implementing process execution using eitherspawn()
orexec()
from thechild_process
module instead of the commented template literal syntax. Here's an example:This would provide proper process handling and output capture.
Spotted by Graphite Reviewer
Is this helpful? React 👍 or 👎 to let us know.