Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions typescript/.changeset/cyan-llamas-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-onchain-agent": patch
---

Improved error handling, particulately for missing .env vars in next template
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ export async function prepareAgentkitAndWalletProvider(): Promise<{
agentkit: AgentKit;
walletProvider: WalletProvider;
}> {
if (!process.env.CDP_API_KEY_ID || !process.env.CDP_API_KEY_SECRET) {
throw new Error(
"I need both CDP_API_KEY_ID and CDP_API_KEY_SECRET in your .env file to connect to the Coinbase Developer Platform.",
);
}

try {
let walletDataStr: string | null = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ export async function prepareAgentkitAndWalletProvider(): Promise<{
agentkit: AgentKit;
walletProvider: WalletProvider;
}> {
if (!process.env.PRIVY_APP_ID || !process.env.PRIVY_APP_SECRET) {
throw new Error(
"I need both PRIVY_APP_ID and PRIVY_APP_SECRET in your .env file to set up your wallet.",
);
}

try {
// Initialize WalletProvider: https://docs.cdp.coinbase.com/agentkit/docs/wallet-management
const config: PrivyWalletConfig = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,35 @@ export async function prepareAgentkitAndWalletProvider(): Promise<{
agentkit: AgentKit;
walletProvider: WalletProvider;
}> {
try {
let walletData: WalletData | null = null;
let privateKey: Hex | null = null;
if (!process.env.CDP_API_KEY_ID || !process.env.CDP_API_KEY_SECRET) {
throw new Error(
"I need both CDP_API_KEY_ID and CDP_API_KEY_SECRET in your .env file to connect to the Coinbase Developer Platform.",
);
}

let walletData: WalletData | null = null;
let privateKey: Hex | null = null;

// Read existing wallet data if available
if (fs.existsSync(WALLET_DATA_FILE)) {
try {
walletData = JSON.parse(fs.readFileSync(WALLET_DATA_FILE, "utf8")) as WalletData;
privateKey = walletData.privateKey;
} catch (error) {
console.error("Error reading wallet data:", error);
// Continue without wallet data
}
// Read existing wallet data if available
if (fs.existsSync(WALLET_DATA_FILE)) {
try {
walletData = JSON.parse(fs.readFileSync(WALLET_DATA_FILE, "utf8")) as WalletData;
privateKey = walletData.privateKey;
} catch (error) {
console.error("Error reading wallet data:", error);
}
}

if (!privateKey) {
if (walletData?.smartWalletAddress) {
throw new Error(
`Smart wallet found but no private key provided. Either provide the private key, or delete ${WALLET_DATA_FILE} and try again.`,
);
}
privateKey = (process.env.PRIVATE_KEY || generatePrivateKey()) as Hex;
if (!privateKey) {
if (walletData?.smartWalletAddress) {
throw new Error(
`I found your smart wallet but can't access your private key. Please either provide the private key in your .env, or delete ${WALLET_DATA_FILE} to create a new wallet.`,
);
}
privateKey = (process.env.PRIVATE_KEY || generatePrivateKey()) as Hex;
}

try {
const signer = privateKeyToAccount(privateKey);

// Initialize WalletProvider: https://docs.cdp.coinbase.com/agentkit/docs/wallet-management
Expand All @@ -113,7 +118,7 @@ export async function prepareAgentkitAndWalletProvider(): Promise<{
});

// Save wallet data
const smartWalletAddress = await walletProvider.getAddress();
const smartWalletAddress = walletProvider.getAddress();
fs.writeFileSync(
WALLET_DATA_FILE,
JSON.stringify({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ export async function prepareAgentkitAndWalletProvider(): Promise<{
agentkit: AgentKit;
walletProvider: WalletProvider;
}> {
if (!process.env.PRIVY_APP_ID || !process.env.PRIVY_APP_SECRET) {
throw new Error(
"I need both PRIVY_APP_ID and PRIVY_APP_SECRET in your .env file to set up your wallet.",
);
}

try {
// Initialize WalletProvider: https://docs.cdp.coinbase.com/agentkit/docs/wallet-management
const config: PrivyWalletConfig = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ export async function createAgent(): Promise<ReturnType<typeof createReactAgent>
return agent;
}

try {
const { agentkit, walletProvider } = await prepareAgentkitAndWalletProvider();
if (!process.env.OPENAI_API_KEY) {
throw new Error("I need an OPENAI_API_KEY in your .env file to power my intelligence.");
}

const { agentkit, walletProvider } = await prepareAgentkitAndWalletProvider();

try {
// Initialize LLM: https://platform.openai.com/docs/models#gpt-4o
const llm = new ChatOpenAI({ model: "gpt-4o-mini" });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export async function POST(
return NextResponse.json({ response: agentResponse });
} catch (error) {
console.error("Error processing request:", error);
return NextResponse.json({ error: "Failed to process message" });
return NextResponse.json({
error:
error instanceof Error
? error.message
: "I'm sorry, I encountered an issue processing your message. Please try again later.",
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ export async function createAgent(): Promise<Agent> {
return agent;
}

if (!process.env.OPENAI_API_KEY) {
throw new Error("I need an OPENAI_API_KEY in your .env file to power my intelligence.");
}

const { agentkit, walletProvider } = await prepareAgentkitAndWalletProvider();

try {
// Initialize LLM: https://platform.openai.com/docs/models#gpt-4o
const model = openai("gpt-4o-mini");

const { agentkit, walletProvider } = await prepareAgentkitAndWalletProvider();

// Initialize Agent
const canUseFaucet = walletProvider.getNetwork().networkId == "base-sepolia";
const faucetMessage = `If you ever need funds, you can request them from the faucet.`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export async function POST(
return NextResponse.json({ response: text });
} catch (error) {
console.error("Error processing request:", error);
return NextResponse.json({ error: "Failed to process message" });
return NextResponse.json({
error:
error instanceof Error
? error.message
: "I'm sorry, I encountered an issue processing your message. Please try again later.",
});
}
}
Loading