Skip to content
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: Logging improvements #393

Merged
merged 5 commits into from
Nov 18, 2024
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"start:service:all": "pnpm --filter \"@ai16z/agent\" start:service:all --isRoot",
"stop:service:all": "pnpm --filter \"@ai16z/agent\" stop:service:all",
"start": "pnpm --filter \"@ai16z/agent\" start --isRoot",
"start:client": "pnpm --dir client start --isRoot",
"dev": "bash ./scripts/dev.sh",
"lint": "pnpm --dir packages/core lint && pnpm --dir packages/agent lint",
"prettier-check": "npx prettier --check .",
Expand Down
3 changes: 1 addition & 2 deletions packages/client-discord/src/voice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ import {
ITranscriptionService,
Memory,
ModelClass,
Service,
ServiceType,
State,
UUID,
} from "@ai16z/eliza";
import { stringToUuid } from "@ai16z/eliza/src/uuid.ts";
import { stringToUuid } from "@ai16z/eliza";

export function getWavHeader(
audioLength: number,
Expand Down
2 changes: 1 addition & 1 deletion packages/client-twitter/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export class ClientBase extends EventEmitter {
);
await this.setCookiesFromArray(cookiesArray);
} else {
console.log("Cookies file path:", cookiesFilePath);
elizaLogger.debug("Cookies file path:", cookiesFilePath);
if (fs.existsSync(cookiesFilePath)) {
const cookiesArray = JSON.parse(
fs.readFileSync(cookiesFilePath, "utf-8")
Expand Down
6 changes: 2 additions & 4 deletions packages/client-twitter/src/interactions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SearchMode, Tweet } from "agent-twitter-client";
import fs from "fs";
import { composeContext } from "@ai16z/eliza";
import { composeContext, elizaLogger } from "@ai16z/eliza";
import { generateMessageResponse, generateShouldRespond } from "@ai16z/eliza";
import { messageCompletionFooter, shouldRespondFooter } from "@ai16z/eliza";
import {
Expand Down Expand Up @@ -199,7 +199,7 @@ export class TwitterInteractionClient extends ClientBase {
console.log("skipping tweet with no text", tweet.id);
return { text: "", action: "IGNORE" };
}
console.log("handling tweet", tweet.id);
elizaLogger.log("handling tweet", tweet.id);
const formatTweet = (tweet: Tweet) => {
return ` ID: ${tweet.id}
From: ${tweet.name} (@${tweet.username})
Expand Down Expand Up @@ -267,8 +267,6 @@ export class TwitterInteractionClient extends ClientBase {
this.saveRequestMessage(message, state);
}

console.log("composeState done");

const shouldRespondContext = composeContext({
state,
template:
Expand Down
10 changes: 3 additions & 7 deletions packages/core/src/embedding.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

import path from "node:path";


import { models } from "./models.ts";
import { IAgentRuntime, ModelProviderName, ModelClass } from "./types.ts";
import fs from "fs";
Expand Down Expand Up @@ -29,8 +27,6 @@ async function getRemoteEmbedding(
// Construct full URL
const fullUrl = `${baseEndpoint}/embeddings`;

//console.log("Calling embedding API at:", fullUrl); // Debug log

const requestOptions = {
method: "POST",
headers: {
Expand All @@ -52,7 +48,7 @@ async function getRemoteEmbedding(
const response = await fetch(fullUrl, requestOptions);

if (!response.ok) {
console.error("API Response:", await response.text()); // Debug log
elizaLogger.error("API Response:", await response.text()); // Debug log
throw new Error(
`Embedding API Error: ${response.status} ${response.statusText}`
);
Expand All @@ -65,7 +61,7 @@ async function getRemoteEmbedding(
const data: EmbeddingResponse = await response.json();
return data?.data?.[0].embedding;
} catch (e) {
console.error("Full error details:", e);
elizaLogger.error("Full error details:", e);
throw e;
}
}
Expand Down Expand Up @@ -176,7 +172,7 @@ export async function retrieveCachedEmbedding(
input: string
) {
if (!input) {
console.log("No input to retrieve cached embedding for");
elizaLogger.log("No input to retrieve cached embedding for");
return null;
}

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/memory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { embed } from "./embedding.ts";
import elizaLogger from "./logger.ts";
import {
IAgentRuntime,
IMemoryManager,
Expand Down Expand Up @@ -160,7 +161,7 @@ export class MemoryManager implements IMemoryManager {
await this.runtime.databaseAdapter.getMemoryById(memory.id);

if (existingMessage) {
// console.log("Memory already exists, skipping");
elizaLogger.debug("Memory already exists, skipping");
return;
}
await this.runtime.databaseAdapter.createMemory(
Expand Down
5 changes: 1 addition & 4 deletions packages/core/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ export class AgentRuntime implements IAgentRuntime {
services: Map<ServiceType, Service> = new Map();
memoryManagers: Map<string, IMemoryManager> = new Map();

logging: boolean = false;

registerMemoryManager(manager: IMemoryManager): void {
if (!manager.tableName) {
throw new Error("Memory manager must have a tableName");
Expand Down Expand Up @@ -428,7 +426,6 @@ export class AgentRuntime implements IAgentRuntime {
state?: State,
callback?: HandlerCallback
): Promise<void> {
console.log("Processing actions", responses);
if (!responses[0].content?.action) {
elizaLogger.warn("No action found in the response content.");
return;
Expand Down Expand Up @@ -499,7 +496,7 @@ export class AgentRuntime implements IAgentRuntime {
async evaluate(message: Memory, state?: State, didRespond?: boolean) {
const evaluatorPromises = this.evaluators.map(
async (evaluator: Evaluator) => {
console.log("Evaluating", evaluator.name);
elizaLogger.log("Evaluating", evaluator.name);
if (!evaluator.handler) {
return null;
}
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,3 +670,9 @@ export enum ServiceType {
SPEECH_GENERATION = "speech_generation",
PDF = "pdf",
}

export enum LoggingLevel {
DEBUG = "debug",
VERBOSE = "verbose",
NONE = "none",
}
5 changes: 5 additions & 0 deletions packages/plugin-starknet/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ Reuse providers and utilities from the existing actions where possible. Add more

1. Add the action to the `actions` directory. Try to follow the naming convention of the other actions.
2. Export the action in the `index.ts` file.

## TODO:

1. Ekubo DCA
2. Unruggable
77 changes: 69 additions & 8 deletions packages/plugin-starknet/src/actions/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,46 @@ import {

import { getStarknetAccount, validateSettings } from "../utils/index.ts";

interface SwapContent {
sellTokenAddress: string;
buyTokenAddress: string;
sellAmount: string;
}

export function isSwapContent(content: SwapContent): content is SwapContent {
// Validate types
const validTypes =
typeof content.sellTokenAddress === "string" &&
typeof content.buyTokenAddress === "string" &&
typeof content.sellAmount === "string";
if (!validTypes) {
return false;
}

// Validate addresses (must be 32-bytes long with 0x prefix)
const validAddresses =
content.sellTokenAddress.startsWith("0x") &&
content.sellTokenAddress.length === 66 &&
content.buyTokenAddress.startsWith("0x") &&
content.buyTokenAddress.length === 66;

return validAddresses;
}

const swapTemplate = `Respond with a JSON markdown block containing only the extracted values. Use null for any values that cannot be determined.

These are known addresses you will get asked to swap, use these addresses for sellTokenAddress and buyTokenAddress:
- BROTHER/brother/$brother: 0x03b405a98c9e795d427fe82cdeeeed803f221b52471e3a757574a2b4180793ee
- BTC/btc: 0x03fe2b97c1fd336e750087d68b9b867997fd64a2661ff3ca5a7c771641e8e7ac
- ETH/eth: 0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7
- STRK/strk: 0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d
- LORDS/lords: 0x0124aeb495b947201f5fac96fd1138e326ad86195b98df6dec9009158a533b49

Example response:
\`\`\`json
{
"sellTokenAddress": "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
"buyTokenAddress": "0x053c91253bc9682c04929ca02ed00b3e423f6710d2ee7e0d5ebb06f3ecf368a8",
"buyTokenAddress": "0x124aeb495b947201f5fac96fd1138e326ad86195b98df6dec9009158a533b49",
"sellAmount": "1000000000000000000"
}
\`\`\`
Expand All @@ -48,7 +81,8 @@ export const executeSwap: Action = {
validate: async (runtime: IAgentRuntime, message: Memory) => {
return validateSettings(runtime);
},
description: "Perform a token swap using Avnu.",
description:
"Perform a token swap on starknet. Use this action when a user asks you to swap tokens anything.",
handler: async (
runtime: IAgentRuntime,
message: Memory,
Expand All @@ -75,6 +109,11 @@ export const executeSwap: Action = {

console.log("Response:", response);

if (!isSwapContent(response)) {
callback?.({ text: "Invalid swap content, please try again." });
return false;
}

try {
// Get quote
const quoteParams: QuoteRequest = {
Expand Down Expand Up @@ -105,7 +144,7 @@ export const executeSwap: Action = {
return true;
} catch (error) {
console.error("Error during token swap:", error);
callback?.({ text: `Error during swap: ${error.message}` });
callback?.({ text: `Error during swap:` });
return false;
}
},
Expand All @@ -114,19 +153,41 @@ export const executeSwap: Action = {
{
user: "{{user1}}",
content: {
text: "Swap 1 ETH for USDC on Starknet",
text: "Swap 10 ETH for LORDS",
},
},
{
user: "{{agent}}",
content: {
text: "Ok, I'll swap 10 ETH for LORDS",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "Swap 100 $lords on starknet",
},
},
{
user: "{{user2}}",
user: "{{agent}}",
content: {
text: "Ok, I'll swap 100 $lords on starknet",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "Buy LORDS on Starknet",
text: "Swap 0.5 BTC for LORDS",
},
},
{
user: "{{user2}}",
user: "{{agent}}",
content: {
text: "Executing swap...",
text: "Ok, I'll swap 0.5 BTC for LORDS",
},
},
],
Expand Down
36 changes: 18 additions & 18 deletions packages/plugin-starknet/src/actions/takeOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,7 @@ interface Order {
price: number;
}

const take_order: Action = {
name: "TAKE_STARKNET_ORDER",
similes: ["BUY_STARKNET_ORDER", "PLACE_STARKNET_ORDER"],
description:
"Records a starknet buy order based on the user's conviction level.",
examples: [],
validate: async (runtime: IAgentRuntime, message: Memory) => {
const text = (message.content as Content).text;
// Check if the message contains a ticker symbol
const tickerRegex = /\b[A-Z]{1,5}\b/g;
return tickerRegex.test(text);
},
handler: async (runtime: IAgentRuntime, message: Memory) => {
const text = (message.content as Content).text;
const userId = message.userId;

const template = `
const template = `

// CLAUDE TODO: Put the usual conversation context here

Expand All @@ -52,6 +36,21 @@ Determine if the user is trying to shill the ticker. if they are, respond with e
// - contractAddress: string
`;

const take_order: Action = {
name: "TAKE_STARKNET_ORDER",
similes: ["BUY_STARKNET_ORDER", "PLACE_STARKNET_ORDER"],
description:
"Records a starknet buy order based on the user's conviction level.",
validate: async (runtime: IAgentRuntime, message: Memory) => {
const text = (message.content as Content).text;
// Check if the message contains a ticker symbol
const tickerRegex = /\b[A-Z]{1,5}\b/g;
return tickerRegex.test(text);
},
handler: async (runtime: IAgentRuntime, message: Memory) => {
const text = (message.content as Content).text;
const userId = message.userId;

let ticker, contractAddress;

// TODO:
Expand Down Expand Up @@ -130,6 +129,7 @@ Determine if the user is trying to shill the ticker. if they are, respond with e
text: `Recorded a ${conviction} conviction buy order for ${ticker} (${contractAddress}) with an amount of ${buyAmount} at the price of ${currentPrice}.`,
};
},
};
examples: [] as ActionExample[][],
} as Action;

export default take_order;
Loading