Skip to content

Commit 67ea56b

Browse files
Merge branch 'develop' into feat/gitcoin_passport_plugin
2 parents 462a72c + 0404e29 commit 67ea56b

File tree

2 files changed

+138
-1
lines changed

2 files changed

+138
-1
lines changed

packages/_examples/plugin/src/plugins/samplePlugin.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Plugin } from "@elizaos/core";
22
import { createResourceAction } from "../actions/sampleAction";
33
import { sampleProvider } from "../providers/sampleProvider";
44
import { sampleEvaluator } from "../evaluators/sampleEvalutor";
5+
import SampleService from "../services/sampleService";
56

67
export const samplePlugin: Plugin = {
78
name: "sample",
@@ -10,6 +11,6 @@ export const samplePlugin: Plugin = {
1011
providers: [sampleProvider],
1112
evaluators: [sampleEvaluator],
1213
// separate examples will be added for services and clients
13-
services: [],
14+
services: [new SampleService()],
1415
clients: [],
1516
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import {
2+
Service,
3+
ServiceType,
4+
IAgentRuntime,
5+
// Memory,
6+
// State,
7+
elizaLogger,
8+
// stringToUuid,
9+
} from "@elizaos/core";
10+
// import { sampleProvider } from "../providers/sampleProvider"; // TODO: Uncomment this line to use the sampleProvider
11+
12+
// Add SAMPLE to ServiceType enum in types.ts
13+
declare module "@elizaos/core" {
14+
export enum ServiceType {
15+
SAMPLE = "sample",
16+
}
17+
}
18+
19+
// The SampleService is a simple service that logs "Hello world" every 15 minutes.
20+
export class SampleService extends Service {
21+
private runtime: IAgentRuntime | null = null;
22+
private intervalId: NodeJS.Timeout | null = null;
23+
private readonly DEFAULT_INTERVAL = 15 * 60 * 1000; // 15 minutes in milliseconds
24+
25+
static get serviceType(): ServiceType {
26+
return ServiceType.SAMPLE;
27+
}
28+
29+
private static isInitialized = false;
30+
31+
async initialize(runtime: IAgentRuntime): Promise<void> {
32+
// Verify if the service is already initialized
33+
if (SampleService.isInitialized) {
34+
return;
35+
}
36+
37+
this.runtime = runtime;
38+
39+
// Start the periodic task
40+
this.startPeriodicTask();
41+
SampleService.isInitialized = true;
42+
elizaLogger.info("SampleService initialized and started periodic task");
43+
}
44+
45+
private static activeTaskCount = 0;
46+
47+
private startPeriodicTask(): void {
48+
// Verify if a task is already active
49+
if (SampleService.activeTaskCount > 0) {
50+
elizaLogger.warn(
51+
"SampleService: Periodic task already running, skipping"
52+
);
53+
return;
54+
}
55+
56+
// Clear any existing interval
57+
if (this.intervalId) {
58+
clearInterval(this.intervalId);
59+
}
60+
61+
SampleService.activeTaskCount++;
62+
elizaLogger.info(
63+
`SampleService: Starting periodic task (active tasks: ${SampleService.activeTaskCount})`
64+
);
65+
66+
// Initial call immediately
67+
this.fetchSample();
68+
69+
// Set up periodic calls
70+
this.intervalId = setInterval(() => {
71+
this.fetchSample();
72+
}, this.DEFAULT_INTERVAL);
73+
}
74+
75+
private async fetchSample(): Promise<void> {
76+
if (!this.runtime) {
77+
elizaLogger.error("SampleService: Runtime not initialized");
78+
return;
79+
}
80+
81+
try {
82+
// Example of using the sampleProvider
83+
// Create dummy memory and state objects for the provider
84+
// const dummyMemory: Memory = {
85+
// id: stringToUuid("sample-service-trigger"),
86+
// userId: this.runtime.agentId,
87+
// agentId: this.runtime.agentId,
88+
// roomId: this.runtime.agentId,
89+
// content: { text: "Periodic sample fetch" },
90+
// createdAt: Date.now(),
91+
// };
92+
93+
// const dummyState: State = {
94+
// userId: this.runtime.agentId,
95+
// bio: "",
96+
// lore: "",
97+
// messageDirections: "",
98+
// postDirections: "",
99+
// roomId: this.runtime.agentId,
100+
// actors: "",
101+
// recentMessages: "",
102+
// recentMessagesData: [],
103+
// };
104+
// await sampleProvider.get(this.runtime, dummyMemory, dummyState);
105+
106+
// hello world log example
107+
elizaLogger.info("SampleService: Hello world");
108+
109+
elizaLogger.info(
110+
"SampleService: Successfully fetched and processed sample"
111+
);
112+
} catch (error) {
113+
elizaLogger.error("SampleService: Error fetching sample:", error);
114+
}
115+
}
116+
117+
// Method to stop the service
118+
stop(): void {
119+
if (this.intervalId) {
120+
clearInterval(this.intervalId);
121+
this.intervalId = null;
122+
SampleService.activeTaskCount--;
123+
elizaLogger.info(
124+
`SampleService stopped (active tasks: ${SampleService.activeTaskCount})`
125+
);
126+
}
127+
SampleService.isInitialized = false;
128+
}
129+
130+
// Method to manually trigger a sample fetch (for testing)
131+
async forceFetch(): Promise<void> {
132+
await this.fetchSample();
133+
}
134+
}
135+
136+
export default SampleService;

0 commit comments

Comments
 (0)