Skip to content

Commit

Permalink
snapshot of the latest
Browse files Browse the repository at this point in the history
  • Loading branch information
stat committed Nov 14, 2024
1 parent 00396f1 commit 40d6013
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,29 @@

from cdp_agentkit_core.actions.social.twitter.account_details import AccountDetailsAction
from cdp_agentkit_core.actions.social.twitter.mentions_monitor_details import (
MentionsMonitorDetailsAction,
MentionsMonitorDetailsAction as TwitterMentionsMonitorDetailsAction,
)
from cdp_agentkit_core.actions.social.twitter.mentions_monitor import (
MentionsMonitor as TwitterMentionsMonitor,
)
from cdp_agentkit_core.actions.social.twitter.mentions_monitor_start import (
MentionsMonitorStartAction,
MentionsMonitorStartAction as TwitterMentionsMonitorStartAction,
)
from cdp_agentkit_core.actions.social.twitter.mentions_monitor_stop import (
MentionsMonitorStopAction as TwitterMentionsMonitorStoptAction
)
from cdp_agentkit_core.actions.social.twitter.mentions_monitor_stop import MentionsMonitorStopAction
from cdp_agentkit_core.actions.social.twitter.mentions_responder_details import (
MentionsResponderDetailsAction,
MentionsResponderDetailsAction as TwitterMentionsResponderDetailsAction,
)
from cdp_agentkit_core.actions.social.twitter.mentions_responder_start import (
MentionsResponderStartAction,
MentionsResponderStartAction as TwitterMentionsResponderStartAction,
)
from cdp_agentkit_core.actions.social.twitter.mentions_responder_stop import (
MentionsResponderStopAction,
MentionsResponderStopAction as TwitterMentionsResponderStopAction,
)
from cdp_agentkit_core.actions.social.twitter.post_tweet import (
PostTweetAction as TwitterPostTweetAction
)
from cdp_agentkit_core.actions.social.twitter.post_tweet import PostTweetAction


def get_all_twitter_actions() -> list[type[TwitterAction]]:
Expand All @@ -43,13 +50,15 @@ def get_all_twitter_actions() -> list[type[TwitterAction]]:
"TwitterAction",
"TwitterActionThread",
"TwitterContext",
"AccountDetailsAction",
"MentionsMonitorDetailsAction",
"MentionsMonitorStartAction",
"MentionsMonitorStopAction",
"MentionsResponderDetailsAction",
"MentionsResponderStartAction",
"MentionsResponderStopAction",
"PostTweetAction",

"TwitterAccountDetailsAction",
"TwitterMentionsMonitorDetailsAction",
"TwitterMentionsMonitorStartAction",
"TwitterMentionsMonitorStopAction",
"TwitterMentionsResponderDetailsAction",
"TwitterMentionsResponderStartAction",
"TwitterMentionsResponderStopAction",
"TwitterPostTweetAction",

"TWITTER_ACTIONS",
]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import contextvars
import enum
import time
import threading

from collections.abc import Callable
Expand All @@ -26,6 +27,8 @@ class ActionThread(threading.Thread):
fn: Callable | None = None
state: ActionThreadState = ActionThreadState.NONE
stopped_event: threading.Event | None = None
started_at: float = 0
stopped_at: float = 0

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand All @@ -52,6 +55,7 @@ def run(self):
if self.fn is None:
return

self.started_at = time.time()
self.fn()

def stop(self) -> threading.Event | None:
Expand All @@ -67,6 +71,7 @@ def stopped(self):
if self.stopped_event is not None:
self.stopped_event.set()

self.stopped_at = time.Time()
self.state = ActionThreadState.STOPPED

# def execute(fn):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import contextvars
import time
import threading
from queue import Queue
from time import sleep

import tweepy
from pydantic import BaseModel

from cdp_agentkit_core.actions.social.twitter import (
TwitterAction,
TwitterActionThread,
TwitterActionThreadState,
TwitterContext,
)

from cdp_agentkit_core.actions.social.twitter.context import context


class MentionsMonitor(TwitterActionThread):
CONTEXT_KEY: str = "mentions-monitor"

"""
https://developer.x.com/en/docs/x-api/rate-limits
"""
Expand Down Expand Up @@ -55,7 +53,7 @@ def monitor(self):

# self.state = TwitterActionThreadState.RUNNING
# while self.state == TwitterActionThreadState.RUNNING:
# time.sleep(1)
# sleep(1)

# self.stopped()
# return
Expand All @@ -71,7 +69,7 @@ def monitor(self):
self.waited += 1

if self.waited < self.backoff[self.backoff_index]:
time.sleep(1)
sleep(1)
continue

try:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
from collections.abc import Callable
from json import dumps

from pydantic import BaseModel

from cdp_agentkit_core.actions.social.twitter import TwitterAction, TwitterContext
from cdp_agentkit_core.actions.social.twitter import (
TwitterAction,
)
from cdp_agentkit_core.actions.social.twitter.context import context
from cdp_agentkit_core.actions.social.twitter.mentions_monitor import MentionsMonitor

MENTIONS_MONITOR_DETAILS_PROMPT = """show details for mentions."""
MENTIONS_MONITOR_DETAILS_PROMPT = """show details for the mention monitor."""


class MentionsMonitorDetailsInput(BaseModel):
pass


def mentions_monitor_details() -> str:
# print(f"size: {items} {items.qsize()}")
# return f"size: {items} {items.qsize()}"
return ""
pass
ctx = context()
monitor = ctx.get(MentionsMonitor.CONTEXT_KEY)

if monitor is None:
return "monitor has not been started."

data = {
"is-monitor-running": monitor.is_running(),
"mentions": ctx.mentions.get(),
"mentions-count": len(ctx.mentions.get()),
}

json = dumps(data)

return f"""
please find the status of the monitor in json format below:
{json}
"""


class MentionsMonitorDetailsAction(TwitterAction):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

from pydantic import BaseModel

from cdp_agentkit_core.actions.social.twitter import (
TwitterAction,
)
from cdp_agentkit_core.actions.social.twitter.context import context
from cdp_agentkit_core.actions.social.twitter.mentions_monitior import MentionsMonitor
from cdp_agentkit_core.actions.social.twitter.mentions_monitor import MentionsMonitor

MENTIONS_MONITOR_START_PROMPT = """
This tool will monitor mentions for the currently authenticated Twitter (X) user context."""
Expand All @@ -17,7 +20,7 @@ def mentions_monitor_start() -> str:
monitor = MentionsMonitor()

ctx = context()
ctx.set("mentions-monitor", monitor)
ctx.set(MentionsMonitor.CONTEXT_KEY, monitor)

monitor.start()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

from cdp_agentkit_core.actions.social.twitter import (
TwitterAction,
TwitterActionThreadState,
TwitterContext,
)
from cdp_agentkit_core.actions.social.twitter.context import context
from cdp_agentkit_core.actions.social.twitter.mentions_monitor import MentionsMonitor

MENTIONS_MONITOR_STOP_PROMPT = """
Stop monitoring Twitter (X) mentions
Expand All @@ -20,16 +19,16 @@ class MentionsMonitorStopInput(BaseModel):

def mentions_monitor_stop() -> str:
ctx = context()
thread = ctx.get("monitor-thread")
ctx.set("monitor-thread", None)
monitor = ctx.get(MentionsMonitor.CONTEXT_KEY)
ctx.set(MentionsMonitor.CONTEXT_KEY, None)

if thread is None:
if monitor is None:
return "monitor cannot be stopped, it is not running!"

if thread.is_running() is False:
if monitor.is_running() is False:
return "monitor has already stopped"

event = thread.stop()
event = monitor.stop()
event.wait()

return "successfully stopped monitoring for Twitter (X) mentions for the authenticated user."
Expand Down
3 changes: 2 additions & 1 deletion twitter-langchain/examples/chatbot/chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ def initialize_agent():

), config


# Chat Mode


def run_chat_mode(agent_executor, config):
"""Run the agent interactively based on user input."""
print("Starting chat mode... Type 'exit' to end.")
Expand Down
4 changes: 1 addition & 3 deletions twitter-langchain/twitter_langchain/twitter_api_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
"""Util that calls Twitter API."""

import contextvars
import inspect
from collections.abc import Callable
from typing import Any

from langchain_core.utils import get_from_dict_or_env
from pydantic import BaseModel, Field, model_validator

from cdp_agentkit_core.actions.social.twitter.context import TwitterContext, context
from cdp_agentkit_core.actions.social.twitter.mentions_monitor_start import MonitorMentionsThread
from cdp_agentkit_core.actions.social.twitter.context import context


class TwitterApiWrapper(BaseModel):
Expand Down

0 comments on commit 40d6013

Please sign in to comment.