Skip to content

Commit

Permalink
providers working better
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Mar 6, 2025
1 parent 1b97c62 commit 5af7cb6
Show file tree
Hide file tree
Showing 31 changed files with 330 additions and 327 deletions.
2 changes: 2 additions & 0 deletions packages/agent/src/server/api/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ export function agentRouter(
attachments,
source: "direct",
inReplyTo: undefined,
channelType: ChannelType.API,
};

const userMessage = {
Expand Down Expand Up @@ -873,6 +874,7 @@ export function agentRouter(
attachments: [],
source: "direct",
inReplyTo: undefined,
channelType: ChannelType.API,
};

const userMessage = {
Expand Down
8 changes: 4 additions & 4 deletions packages/agent/src/swarm/communityManager/actions/greet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ export const greetAction: Action = {
validate: async (
runtime: IAgentRuntime,
message: Memory,
_state: State
state: State
): Promise<boolean> => {
const room = await runtime.databaseAdapter.getRoom(message.roomId);
const room = state.data.room ?? await runtime.databaseAdapter.getRoom(message.roomId);
if(!room) {
throw new Error("No room found");
}
Expand Down Expand Up @@ -62,11 +62,11 @@ export const greetAction: Action = {
handler: async (
runtime: IAgentRuntime,
message: Memory,
_state: State,
state: State,
_options: any,
callback: HandlerCallback,
): Promise<void> => {
const room = await runtime.databaseAdapter.getRoom(message.roomId);
const room = state.data.room ?? await runtime.databaseAdapter.getRoom(message.roomId);
if(!room) {
throw new Error("No room found");
}
Expand Down
2 changes: 2 additions & 0 deletions packages/agent/src/swarm/scenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export class ScenarioService extends Service {
source: "scenario",
name: sender.character.name,
userName: sender.character.name,
channelType: ChannelType.GROUP,
},
};

Expand Down Expand Up @@ -136,6 +137,7 @@ export class ScenarioService extends Service {
source: "scenario",
name: sender.character.name,
userName: sender.character.name,
channelType: ChannelType.GROUP,
},
};

Expand Down
6 changes: 3 additions & 3 deletions packages/agent/src/swarm/socialMediaManager/actions/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ const twitterPostAction: Action = {
validate: async (
runtime: IAgentRuntime,
message: Memory,
_state: State
state: State
): Promise<boolean> => {
const room = await runtime.databaseAdapter.getRoom(message.roomId);
const room = state.data.room ?? await runtime.databaseAdapter.getRoom(message.roomId);
if (!room) {
throw new Error("No room found");
}
Expand Down Expand Up @@ -161,7 +161,7 @@ const twitterPostAction: Action = {
_responses: Memory[]
) => {
try {
const room = await runtime.databaseAdapter.getRoom(message.roomId);
const room = state.data.room ?? await runtime.databaseAdapter.getRoom(message.roomId);
if (!room) {
throw new Error("No room found");
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/actions/choice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ export const choiceAction: Action = {
validate: async (
runtime: IAgentRuntime,
message: Memory,
_state: State
state: State
): Promise<boolean> => {
// Get all tasks with options metadata
const pendingTasks = await runtime.databaseAdapter.getTasks({
roomId: message.roomId,
tags: ["AWAITING_CHOICE"],
});

const room = await runtime.databaseAdapter.getRoom(message.roomId);
const room = state.data.room ?? await runtime.databaseAdapter.getRoom(message.roomId);

const userRole = await getUserServerRole(
runtime,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/actions/followRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export const followRoomAction: Action = {
);
}

const room = await runtime.databaseAdapter.getRoom(message.roomId);
const room = state.data.room ?? await runtime.databaseAdapter.getRoom(message.roomId);

await runtime.getMemoryManager("messages").createMemory({
entityId: message.entityId,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/actions/muteRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export const muteRoomAction: Action = {
);
}

const room = await runtime.databaseAdapter.getRoom(message.roomId);
const room = state.data.room ?? await runtime.databaseAdapter.getRoom(message.roomId);

await runtime.getMemoryManager("messages").createMemory({
entityId: message.entityId,
Expand Down
211 changes: 110 additions & 101 deletions packages/core/src/actions/reply.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { composePrompt, parseJSONObjectFromText } from "../prompts";
import { type Action, type ActionExample, type Content, type HandlerCallback, type IAgentRuntime, type Memory, ModelTypes, type State } from "../types";
import {
type Action,
type ActionExample,
type Content,
type HandlerCallback,
type IAgentRuntime,
type Memory,
ModelTypes,
type State,
} from "../types";

const replyTemplate = `# Task: Generate dialog and actions for the character {{agentName}}.
{{providers}}
Expand All @@ -20,110 +29,110 @@ Response format should be formatted in a valid JSON block like this:
Your response should include the valid JSON block and nothing else.`;


export const replyAction = {
name: "REPLY",
similes: ["REPLY_TO_MESSAGE", "SEND_REPLY", "RESPOND"],
description: "Replies to the current conversation with the text from the generated message. Default if the agent is responding with a message and no other action. Use REPLY at the beginning of a chain of actions as an acknowledgement, and at the end of a chain of actions as a final response.",
validate: async (
_runtime: IAgentRuntime,
) => {
return true;
},
handler: async (
runtime: IAgentRuntime,
message: Memory,
state: State,
_options: any,
callback: HandlerCallback,
) => {

state = await runtime.composeState(message, [...(message.content.providers ?? []), "RECENT_MESSAGES"]);
name: "REPLY",
similes: ["REPLY_TO_MESSAGE", "SEND_REPLY", "RESPOND"],
description:
"Replies to the current conversation with the text from the generated message. Default if the agent is responding with a message and no other action. Use REPLY at the beginning of a chain of actions as an acknowledgement, and at the end of a chain of actions as a final response.",
validate: async (_runtime: IAgentRuntime) => {
return true;
},
handler: async (
runtime: IAgentRuntime,
message: Memory,
state: State,
_options: any,
callback: HandlerCallback
) => {
state = await runtime.composeState(message, [
...(message.content.providers ?? []),
"RECENT_MESSAGES",
]);

const prompt = composePrompt({
state,
template: replyTemplate,
});
console.log("*** replyAction prompt ****");
console.log(prompt);
const prompt = composePrompt({
state,
template: replyTemplate,
});
console.log("*** replyAction prompt ****");
console.log(prompt);

const response = await runtime.useModel(ModelTypes.TEXT_LARGE, {
prompt,
});
const response = await runtime.useModel(ModelTypes.TEXT_LARGE, {
prompt,
});

console.log("*** replyAction response ****");
console.log(response);
console.log("*** replyAction response ****");
console.log(response);

const responseContentObj = parseJSONObjectFromText(response) as Content;
const responseContentObj = parseJSONObjectFromText(response) as Content;

const responseContent = {
thought: responseContentObj.thought,
text: responseContentObj.message as string || "",
actions: ["REPLY_SENT"],
}
const responseContent = {
thought: responseContentObj.thought,
text: (responseContentObj.message as string) || "",
actions: ["REPLY_SENT"],
};

await callback(responseContent);
},
examples: [
[
{
name: "{{name1}}",
content: {
text: "Hello there!",
},
},
{
name: "{{name2}}",
content: {
text: "Hi! How can I help you today?",
actions: ["REPLY"],
},
},
],
[
{
name: "{{name1}}",
content: {
text: "What's your favorite color?",
},
},
{
name: "{{name2}}",
content: {
text: "I really like deep shades of blue. They remind me of the ocean and the night sky.",
actions: ["REPLY"],
},
},
],
[
{
name: "{{name1}}",
content: {
text: "Can you explain how neural networks work?",
},
},
{
name: "{{name2}}",
content: {
text: "Let me break that down for you in simple terms...",
actions: ["REPLY"],
},
},
],
[
{
name: "{{name1}}",
content: {
text: "Could you help me solve this math problem?",
},
},
{
name: "{{name2}}",
content: {
text: "Of course! Let's work through it step by step.",
actions: ["REPLY"],
},
},
]
] as ActionExample[][],
} as Action;
await callback(responseContent);
},
examples: [
[
{
name: "{{name1}}",
content: {
text: "Hello there!",
},
},
{
name: "{{name2}}",
content: {
text: "Hi! How can I help you today?",
actions: ["REPLY"],
},
},
],
[
{
name: "{{name1}}",
content: {
text: "What's your favorite color?",
},
},
{
name: "{{name2}}",
content: {
text: "I really like deep shades of blue. They remind me of the ocean and the night sky.",
actions: ["REPLY"],
},
},
],
[
{
name: "{{name1}}",
content: {
text: "Can you explain how neural networks work?",
},
},
{
name: "{{name2}}",
content: {
text: "Let me break that down for you in simple terms...",
actions: ["REPLY"],
},
},
],
[
{
name: "{{name1}}",
content: {
text: "Could you help me solve this math problem?",
},
},
{
name: "{{name2}}",
content: {
text: "Of course! Let's work through it step by step.",
actions: ["REPLY"],
},
},
],
] as ActionExample[][],
} as Action;
6 changes: 3 additions & 3 deletions packages/core/src/actions/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ const updateRoleAction: Action = {
validate: async (
runtime: IAgentRuntime,
message: Memory,
_state: State
state: State
): Promise<boolean> => {
logger.info("Starting role update validation");

Expand All @@ -212,7 +212,7 @@ const updateRoleAction: Action = {
return false;
}

const room = await runtime.databaseAdapter.getRoom(message.roomId);
const room = state.data.room ?? await runtime.databaseAdapter.getRoom(message.roomId);
if (!room) {
throw new Error("No room found");
}
Expand Down Expand Up @@ -278,7 +278,7 @@ const updateRoleAction: Action = {
await callback(response.content);
}

const room = await runtime.databaseAdapter.getRoom(message.roomId);
const room = state.data.room ?? await runtime.databaseAdapter.getRoom(message.roomId);
const world = await runtime.databaseAdapter.getWorld(room.worldId);

if (!room) {
Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/actions/sendMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ export const sendMessageAction: Action = {
}

const sourceEntityId = message.entityId;
const roomId = message.roomId;
const _agentId = runtime.agentId;
const room = await runtime.databaseAdapter.getRoom(roomId);
const room = state.data.room ?? await runtime.databaseAdapter.getRoom(message.roomId);
const worldId = room.worldId;

// Extract target and source information
Expand Down
11 changes: 2 additions & 9 deletions packages/core/src/actions/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,15 +720,8 @@ const updateSettingsAction: Action = {
_state: State
): Promise<boolean> => {
try {
// Validate that we're in a DM channel
const room = await runtime.databaseAdapter.getRoom(message.roomId);
if (!room) {
logger.error(`No room found for ID ${message.roomId}`);
return false;
}

if (room.type !== ChannelType.DM) {
logger.info(`Skipping settings in non-DM channel (type: ${room.type})`);
if (message.content.channelType !== ChannelType.DM) {
logger.info(`Skipping settings in non-DM channel (type: ${message.content.channelType})`);
return false;
}

Expand Down
Loading

0 comments on commit 5af7cb6

Please sign in to comment.