Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5e9d315

Browse files
committedJan 10, 2025·
feat: dad jokes
1 parent ea9d1c0 commit 5e9d315

File tree

9 files changed

+319
-0
lines changed

9 files changed

+319
-0
lines changed
 

‎packages/plugin-dad-joke/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Dad Joke Plugin for Eliza
2+
3+
Ask for a dad joke!
4+
5+
This plugin was solely made for resarech and learning purposes. Feel free to use this as reference! Happy coding ☕️
6+
7+
## Contributing
8+
9+
Contributions are welcome! Please read our [Contributing Guide](CONTRIBUTING.md) for details.
10+
11+
## License
12+
13+
Built by [@pann0x](https://twitter.com/pann0x) on X.

‎packages/plugin-dad-joke/package.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "@elizaos/plugin-dad-joke",
3+
"version": "0.1.0",
4+
"main": "dist/index.js",
5+
"types": "dist/index.d.ts",
6+
"type": "module",
7+
"scripts": {
8+
"build": "tsup --format esm --dts",
9+
"dev": "tsc -w",
10+
"lint": "eslint src --ext .ts"
11+
},
12+
"author": "pann0x",
13+
"license": "MIT",
14+
"dependencies": {
15+
"@ai16z/client-direct": "0.1.6-alpha.4",
16+
"@ai16z/eliza": "0.1.6-alpha.4",
17+
"@ai16z/plugin-0g": "0.1.6-alpha.4",
18+
"dotenv": "^16.4.7"
19+
}
20+
}
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {Action, IAgentRuntime, Memory, State} from "@ai16z/eliza";
2+
import { dadJokeActionContent, dadJokeData } from "./types.ts";
3+
import { dadJokeProvider } from "./provider.ts";
4+
5+
export const getDadJokeAction: Action = {
6+
name: "GET_DAD_JOKE",
7+
description: "Retrieves a random dad joke",
8+
similes: [
9+
"DAD_JOKE",
10+
"JOKE",
11+
],
12+
examples: [
13+
[
14+
{
15+
user: "{{user1}}",
16+
content: {
17+
text: "Tell me a dad joke",
18+
} as dadJokeActionContent,
19+
},
20+
{
21+
user: "{{agentName}}",
22+
content: {
23+
text: "Why couldn't the bicycle stand up by itself? It was two tired.",
24+
action: "GET_DAD_JOKE",
25+
},
26+
},
27+
28+
],
29+
[
30+
{
31+
user: "{{user1}}",
32+
content: {
33+
text: "You got any dad jokes?",
34+
} as dadJokeActionContent,
35+
},
36+
{
37+
user: "{{agentName}}",
38+
content: {
39+
text: "What do you call a fake noodle? An impasta.",
40+
action: "GET_DAD_JOKE",
41+
},
42+
},
43+
],
44+
],
45+
46+
validate: async (
47+
runtime: IAgentRuntime,
48+
message: Memory,
49+
state?: State,
50+
): Promise<boolean> => {
51+
try {
52+
const content = message.content as dadJokeActionContent;
53+
return (
54+
typeof content.text === "string" &&
55+
content.text.toLowerCase().includes("joke")
56+
);
57+
} catch {
58+
return false;
59+
}
60+
},
61+
62+
handler: async (
63+
runtime: IAgentRuntime,
64+
message: Memory,
65+
state?: State,
66+
): Promise<dadJokeData> => {
67+
try {
68+
const response = await dadJokeProvider.get(runtime, message, state);
69+
if (!response.success) {
70+
throw new Error(response.error);
71+
}
72+
73+
return response.data;
74+
} catch (error) {
75+
throw new Error(error instanceof Error ? error.message : "Failed to retrieve dad joke");
76+
}
77+
}
78+
}
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { Evaluator, IAgentRuntime, Memory, State } from "@ai16z/eliza";
2+
import { dadJokeEvalContent, dadJokeEvalResponse } from "./types.ts";
3+
4+
//the evaluator for dad jokes
5+
//validates the response to ensure it is a dad joke
6+
export const dadJokeEvaluator: Evaluator = {
7+
name: "DAD_JOKE_EVALUATOR",
8+
description: "Validates dad joke responses",
9+
similes: [
10+
"DAD_JOKE_CHECKER",
11+
"JOKE_VALIDATOR",
12+
"DAD_JOKE_RESPONSE_VALIDATOR",
13+
],
14+
examples: [
15+
{
16+
context: "Validating complete dad joke response",
17+
messages: [
18+
{
19+
user: "{{user1}}",
20+
content: {
21+
text: "When does a joke become a dad joke? When the punchline becomes apparent.",
22+
},
23+
},
24+
],
25+
outcome: "When it becomes a groan-up.",
26+
},
27+
],
28+
validate: async (
29+
runtime: IAgentRuntime,
30+
message: Memory,
31+
state?: State,
32+
): Promise<boolean> => {
33+
try {
34+
const content = message.content as dadJokeEvalContent;
35+
return typeof content.text === "string";
36+
} catch {
37+
return false;
38+
}
39+
},
40+
41+
handler: async (
42+
runtime: IAgentRuntime,
43+
message: Memory,
44+
state?: State,
45+
): Promise<dadJokeEvalResponse> => {
46+
try {
47+
const content = message.content as dadJokeEvalContent;
48+
const text = content.text.toLowerCase();
49+
50+
//checks for the word "joke" in the response
51+
if (!text.includes("joke")) {
52+
return {
53+
success: false,
54+
response: "Response does not contain the word 'joke'",
55+
};
56+
}
57+
58+
return {
59+
success: true,
60+
response: "Dad joke response is valid",
61+
};
62+
63+
} catch {
64+
return {
65+
success: false,
66+
response: "Failed to validate dad joke response",
67+
};
68+
}
69+
},
70+
alwaysRun: true,
71+
}

‎packages/plugin-dad-joke/src/index.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Plugin } from "@ai16z/eliza";
2+
import { dadJokeProvider, initializeDadJokeProvider } from "./provider.ts";
3+
import { dadJokeEvaluator } from "./evaluator.ts";
4+
import { dadJokeConfig } from "./types.ts";
5+
import { getDadJokeAction } from "./action.ts";
6+
7+
export const dadJokePlugin: Plugin = {
8+
name: "Dad Joke",
9+
description: "Dad Joke Plugin for Eliza",
10+
actions: [
11+
getDadJokeAction
12+
],
13+
providers: [dadJokeProvider],
14+
evaluators: [dadJokeEvaluator],
15+
//rest of the plugin
16+
}
17+
18+
export const initializeDadJoke = (config: dadJokeConfig): void => {
19+
initializeDadJokeProvider(config);
20+
}
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { IAgentRuntime, Memory, State, Provider } from "@ai16z/eliza";
2+
import { dadJokeProviderResponse, dadJokeData, dadJokeConfig} from "./types.ts";
3+
4+
let providerConfig: dadJokeConfig;
5+
6+
export const dadJokeProvider: Provider = {
7+
get: async (
8+
runtime: IAgentRuntime,
9+
message: Memory,
10+
state?: State,
11+
): Promise<dadJokeProviderResponse> => {
12+
try {
13+
if (!providerConfig?.provider?.baseUrl) {
14+
throw new Error("Dad Joke API url is required");
15+
}
16+
17+
const baseUrl =
18+
providerConfig.provider.baseUrl ||
19+
"https://icanhazdadjoke.com";
20+
21+
// Fetch dad joke
22+
const response = await fetch(baseUrl, {
23+
headers: {
24+
Accept: "application/json",
25+
},
26+
});
27+
28+
if (!response.ok) {
29+
throw new Error(`API request failed: ${response.statusText}`);
30+
}
31+
32+
const data = await response.json();
33+
34+
// Transform API response to dadJokeData
35+
const dadJokeData: dadJokeData = {
36+
joke: data.joke,
37+
};
38+
39+
return {
40+
success: true,
41+
data: dadJokeData,
42+
};
43+
} catch (error) {
44+
return {
45+
success: false,
46+
error: error instanceof Error ? error.message : "Failed to retrieve dad joke",
47+
};
48+
}
49+
}
50+
}
51+
52+
export const initializeDadJokeProvider = (config: dadJokeConfig): void => {
53+
providerConfig = config;
54+
}

‎packages/plugin-dad-joke/src/types.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Content } from '@ai16z/eliza';
2+
3+
export interface dadJokeConfig {
4+
provider: {
5+
apiKey: string;
6+
baseUrl?: string;
7+
options?: string;
8+
};
9+
}
10+
11+
export interface dadJokeData {
12+
joke: string;
13+
//image?: ;
14+
}
15+
16+
export interface dadJokeActionContent extends Content {
17+
text: string;
18+
}
19+
20+
export interface dadJokeEvalContent extends Content {
21+
text: string;
22+
}
23+
24+
export interface dadJokeEvalResponse {
25+
success: boolean;
26+
response: string;
27+
}
28+
29+
export interface dadJokeProviderResponse {
30+
success: boolean;
31+
data?: dadJokeData;
32+
error?: string;
33+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": "../core/tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "dist",
5+
"rootDir": "./src"
6+
},
7+
"include": [
8+
"src"
9+
]
10+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { defineConfig } from "tsup";
2+
3+
export default defineConfig({
4+
entry: ["src/index.ts"],
5+
outDir: "dist",
6+
sourcemap: true,
7+
clean: true,
8+
format: ["esm"], // Ensure you're targeting CommonJS
9+
external: [
10+
"dotenv", // Externalize dotenv to prevent bundling
11+
"fs", // Externalize fs to use Node.js built-in module
12+
"path", // Externalize other built-ins if necessary
13+
"@reflink/reflink",
14+
"@node-llama-cpp",
15+
"https",
16+
"http",
17+
"agentkeepalive",
18+
// Add other modules you want to externalize
19+
],
20+
});

0 commit comments

Comments
 (0)
Please sign in to comment.