forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from JoeyKhd/develop
Develop
- Loading branch information
Showing
17 changed files
with
592 additions
and
9 deletions.
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
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
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
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,6 @@ | ||
* | ||
|
||
!dist/** | ||
!package.json | ||
!readme.md | ||
!tsup.config.ts |
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,3 @@ | ||
import eslintGlobalConfig from "../../eslint.config.mjs"; | ||
|
||
export default [...eslintGlobalConfig]; |
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,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" | ||
} | ||
} |
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,2 @@ | ||
export * from "./startAnyone.ts"; | ||
export * from "./stopAnyone.ts"; |
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,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; |
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,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; |
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,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
27
packages/plugin-anyone/src/services/AnyoneClientService.ts
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,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; | ||
} | ||
} | ||
} |
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,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; | ||
} | ||
} |
Oops, something went wrong.