Skip to content

Commit

Permalink
Merge pull request #2 from JoeyKhd/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
JoeyKhd authored Jan 7, 2025
2 parents 9cb5762 + c8f1c08 commit 8b9af19
Show file tree
Hide file tree
Showing 17 changed files with 592 additions and 9 deletions.
6 changes: 1 addition & 5 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,11 +514,7 @@ export async function createAgent(
cache: ICacheManager,
token: string
): Promise<AgentRuntime> {
elizaLogger.success(
elizaLogger.successesTitle,
"Creating runtime for character",
character.name
);
elizaLogger.log(`Creating runtime for character ${character.name}`);

nodePlugin ??= createNodePlugin();

Expand Down
10 changes: 8 additions & 2 deletions packages/adapter-sqlite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,19 @@ export class SqliteDatabaseAdapter
const content = JSON.stringify(memory.content);
const createdAt = memory.createdAt ?? Date.now();

let embeddingValue: Float32Array = new Float32Array(384);
// If embedding is not available, we just load an array with a length of 384
if (memory?.embedding && memory?.embedding?.length > 0) {
embeddingValue = new Float32Array(memory.embedding);
}

// Insert the memory with the appropriate 'unique' value
const sql = `INSERT OR REPLACE INTO memories (id, type, content, embedding, userId, roomId, agentId, \`unique\`, createdAt) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`;
this.db.prepare(sql).run(
memory.id ?? v4(),
tableName,
content,
new Float32Array(memory.embedding!), // Store as Float32Array
embeddingValue,
memory.userId,
memory.roomId,
memory.agentId,
Expand Down Expand Up @@ -707,4 +713,4 @@ export class SqliteDatabaseAdapter
return false;
}
}
}
}
2 changes: 1 addition & 1 deletion packages/core/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export class AgentRuntime implements IAgentRuntime {
this.ensureParticipantExists(this.agentId, this.agentId);
});

elizaLogger.success("Agent ID", this.agentId);
elizaLogger.success(`Agent ID: ${this.agentId}`);

this.fetch = (opts.fetch as typeof fetch) ?? this.fetch;

Expand Down
6 changes: 6 additions & 0 deletions packages/plugin-anyone/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*

!dist/**
!package.json
!readme.md
!tsup.config.ts
3 changes: 3 additions & 0 deletions packages/plugin-anyone/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import eslintGlobalConfig from "../../eslint.config.mjs";

export default [...eslintGlobalConfig];
21 changes: 21 additions & 0 deletions packages/plugin-anyone/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@elizaos/plugin-anyone",
"version": "0.1.7-alpha.2",
"main": "dist/index.js",
"type": "module",
"types": "dist/index.d.ts",
"dependencies": {
"@anyone-protocol/anyone-client": "^0.4.3",
"@elizaos/core": "workspace:*",
"axios": "^1.7.9",
"tsup": "8.3.5"
},
"scripts": {
"build": "tsup --format esm --dts",
"dev": "tsup --format esm --dts --watch",
"lint": "eslint --fix --cache ."
},
"peerDependencies": {
"whatwg-url": "7.1.0"
}
}
2 changes: 2 additions & 0 deletions packages/plugin-anyone/src/actions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./startAnyone.ts";
export * from "./stopAnyone.ts";
92 changes: 92 additions & 0 deletions packages/plugin-anyone/src/actions/startAnyone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import {
ActionExample,
HandlerCallback,
IAgentRuntime,
Memory,
State,
type Action,
} from "@elizaos/core";
import axios from "axios";
import { AnyoneClientService } from "../services/AnyoneClientService";
import { AnyoneProxyService } from "../services/AnyoneProxyService";

export const startAnyone: Action = {
name: "START_ANYONE",
similes: ["ANYONE"],
validate: async (_runtime: IAgentRuntime, _message: Memory) => {
return true;
},
description: "Start the Anyone client and proxy service",
handler: async (
_runtime: IAgentRuntime,
_message: Memory,
_state: State,
_options: { [key: string]: unknown },
_callback: HandlerCallback
): Promise<boolean> => {
await AnyoneClientService.initialize();
const anon = AnyoneClientService.getInstance();
const proxyService = AnyoneProxyService.getInstance();
await proxyService.initialize();

_callback({
text: `Started Anyone`,
});

return true;
},
examples: [
[
{
user: "{{user1}}",
content: { text: "Can you start Anyone for me?" },
},
{
user: "{{user2}}",
content: {
text: "I'll start Anyone right away",
action: "START_ANYONE",
},
},
],
[
{
user: "{{user1}}",
content: { text: "Initialize the Anyone client please" },
},
{
user: "{{user2}}",
content: {
text: "Starting Anyone now",
action: "START_ANYONE",
},
},
],
[
{
user: "{{user1}}",
content: { text: "I need to start using Anyone" },
},
{
user: "{{user2}}",
content: {
text: "I'll help you start Anyone",
action: "START_ANYONE",
},
},
],
[
{
user: "{{user1}}",
content: { text: "Launch Anyone for me" },
},
{
user: "{{user2}}",
content: {
text: "I'll launch Anyone for you now",
action: "START_ANYONE",
},
},
],
] as ActionExample[][],
} as Action;
91 changes: 91 additions & 0 deletions packages/plugin-anyone/src/actions/stopAnyone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import {
ActionExample,
HandlerCallback,
IAgentRuntime,
Memory,
State,
type Action,
} from "@elizaos/core";
import { AnyoneClientService } from "../services/AnyoneClientService";
import { AnyoneProxyService } from "../services/AnyoneProxyService";

export const stopAnyone: Action = {
name: "STOP_ANYONE",
similes: ["STOP_PROXY"],
validate: async (_runtime: IAgentRuntime, _message: Memory) => {
return true;
},
description: "Stop the Anyone client and proxy service",
handler: async (
_runtime: IAgentRuntime,
_message: Memory,
_state: State,
_options: { [key: string]: unknown },
_callback: HandlerCallback
): Promise<boolean> => {
const proxyService = AnyoneProxyService.getInstance();
proxyService.cleanup();

await AnyoneClientService.stop();

_callback({
text: `Stopped Anyone and cleaned up proxy`,
});

return true;
},
examples: [
[
{
user: "{{user1}}",
content: { text: "Can you stop Anyone for me?" },
},
{
user: "{{user2}}",
content: {
text: "I'll stop Anyone right away",
action: "STOP_ANYONE",
},
},
],
[
{
user: "{{user1}}",
content: { text: "Please shut down Anyone" },
},
{
user: "{{user2}}",
content: {
text: "Stopping Anyone now",
action: "STOP_ANYONE",
},
},
],
[
{
user: "{{user1}}",
content: { text: "I need to stop using Anyone" },
},
{
user: "{{user2}}",
content: {
text: "I'll help you stop Anyone",
action: "STOP_ANYONE",
},
},
],
[
{
user: "{{user1}}",
content: { text: "Close Anyone for me" },
},
{
user: "{{user2}}",
content: {
text: "I'll close Anyone for you now",
action: "STOP_ANYONE",
},
},
],
] as ActionExample[][],
} as Action;
10 changes: 10 additions & 0 deletions packages/plugin-anyone/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Plugin } from "@elizaos/core";
import { startAnyone } from "./actions/startAnyone.ts";
import { stopAnyone } from "./actions/stopAnyone.ts";
export * as actions from "./actions";

export const anyonePlugin: Plugin = {
name: "anyone",
description: "Proxy requests through Anyone",
actions: [startAnyone, stopAnyone],
};
27 changes: 27 additions & 0 deletions packages/plugin-anyone/src/services/AnyoneClientService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Anon } from "@anyone-protocol/anyone-client";

export class AnyoneClientService {
private static instance: Anon | null = null;

static getInstance(): Anon | null {
return this.instance;
}

static async initialize(): Promise<void> {
if (!this.instance) {
this.instance = new Anon({
displayLog: true,
socksPort: 9050,
autoTermsAgreement: true,
});
await this.instance.start();
}
}

static async stop(): Promise<void> {
if (this.instance) {
await this.instance.stop();
this.instance = null;
}
}
}
73 changes: 73 additions & 0 deletions packages/plugin-anyone/src/services/AnyoneProxyService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { AnonSocksClient } from "@anyone-protocol/anyone-client";
import axios from "axios";
import { AnyoneClientService } from "./AnyoneClientService";

export class AnyoneProxyService {
private static instance: AnyoneProxyService | null = null;
private sockClient: AnonSocksClient | null = null;
private originalAxios: any = null;
private originalDefaults: any = null;

static getInstance(): AnyoneProxyService {
if (!AnyoneProxyService.instance) {
AnyoneProxyService.instance = new AnyoneProxyService();
}
return AnyoneProxyService.instance;
}

async initialize(): Promise<void> {
await AnyoneClientService.initialize();
const anon = AnyoneClientService.getInstance();
if (!anon) {
throw new Error("Anyone client not initialized");
}

this.sockClient = new AnonSocksClient(anon);

// Store original axios configuration
this.originalDefaults = { ...axios.defaults };
this.originalAxios = {
request: axios.request,
get: axios.get,
post: axios.post,
put: axios.put,
delete: axios.delete,
patch: axios.patch,
};

// Create new defaults object instead of modifying existing one
axios.defaults = {
...axios.defaults,
...this.sockClient.axios.defaults,
};

// Apply proxy methods
axios.request = this.sockClient.axios.request.bind(
this.sockClient.axios
);
axios.get = this.sockClient.axios.get.bind(this.sockClient.axios);
axios.post = this.sockClient.axios.post.bind(this.sockClient.axios);
axios.put = this.sockClient.axios.put.bind(this.sockClient.axios);
axios.delete = this.sockClient.axios.delete.bind(this.sockClient.axios);
axios.patch = this.sockClient.axios.patch.bind(this.sockClient.axios);
}

cleanup(): void {
if (this.originalAxios && this.originalDefaults) {
// Create fresh axios defaults
axios.defaults = { ...this.originalDefaults };

// Create fresh bindings
axios.request = this.originalAxios.request.bind(axios);
axios.get = this.originalAxios.get.bind(axios);
axios.post = this.originalAxios.post.bind(axios);
axios.put = this.originalAxios.put.bind(axios);
axios.delete = this.originalAxios.delete.bind(axios);
axios.patch = this.originalAxios.patch.bind(axios);

this.originalAxios = null;
this.originalDefaults = null;
}
AnyoneProxyService.instance = null;
}
}
Loading

0 comments on commit 8b9af19

Please sign in to comment.