DEV Community

nodabnodab
nodabnodab

Posted on

Building a Slack LLM Secretary with Tool Chaining

This will be my second project.

AS I know there are three main types of representative AI agents: Claude Code, Open Claw, and Hermes. This time, I attempted to build a small AI assistant utilizing 'Open Claw'.

I believe the most powerful aspect of Open Claw is its ability to "bring together and utilize various tools."

Although it is not a large project, I intend to leverage this feature to issue commands in natural language and demonstrate that it can "act like a personal assistant."

The details are as follows: If you issue a command via the Slack chat window, the AI ​​agent receives the response and executes the command.

Function

There are four main types of commands.

  1. If a client asks a question, it tells them what it can do.
  2. Converts a docx file to a PDF file.
  3. Summarizes the contents of a docx file.
  4. Outputs a result indicating whether an Excel file or a URL sent by the user is successfully connected. For example, if a value of pass/200 appears, it means the URL is active.

Let me introduce just three key technologies for implementing this.

  1. The LLM selects the tool array, rather than using if/else routing.
SELECT_TOOLS_SCHEMA = {
    "type": "function",
    "function": {
        "name": "select_tools",
        ...
        "tools": {
            "type": "array",
            "items": { "enum": [...] },
        },
    },
}
Enter fullscreen mode Exit fullscreen mode
  1. The meaning changes according to the order of human language. Summarize this file and convert it to PDF. Convert this file to PDF and summarize it. These produce different results.
def describe_chain_semantics(tools):
    if tools == ["TEXT_SUMMARY", "PDF_CONVERT"]:
        return "요약 내용을 PDF로 생성합니다."
    if tools == ["PDF_CONVERT", "TEXT_SUMMARY"]:
        return "원본 문서를 PDF로 변환하고, 내용을 요약합니다."

# ...
if previous_tool == "TEXT_SUMMARY" and last_summary:
    create_summary_docx(last_summary, ...)  # 요약본 → PDF
else:
    convert_docx_to_pdf(input_path, ...)    # 원본 → PDF
Enter fullscreen mode Exit fullscreen mode

3.It has a session memory feature. It can remember past chats.
However, that is how the system stores the state. LLM only reads it.
I wanted to prevent the project from becoming too large.

def make_session_key(channel_id, user_id, thread_ts=None):
    if thread_ts:
        return f"{channel_id}:{thread_ts}"
    return f"{channel_id}:user:{user_id}"
Enter fullscreen mode Exit fullscreen mode

Troubleshooting:

This involved attempting to retrieve its tools immediately by reading the last conversation within Slack right after stopping and restarting the agent.
The cause was that, initially, last_seen_ts was stored only in memory (dict).
When the bot is turned off and on again, this value is initialized to {}, and last_seen = 0.
Then, the 10 most recent messages in the channel plus thread replies all satisfy the condition ts > 0, so they are processed all at once.
The key is to persist the cursor (watermark) based on the message ID (ts).

# polling_state.py
class LastSeenStore:
    def mark(self, channel_id, ts):
        if float(ts) > self.get(channel_id):
            self._state[channel_id] = float(ts)
            self._save()  # saving disk!
Enter fullscreen mode Exit fullscreen mode
last_seen_store = LastSeenStore()  # last_seen.json 로드
channel_last_ts = last_seen_store.get(channel_id)
pending = collect_pending_messages(channel_id, channel_last_ts)
Enter fullscreen mode Exit fullscreen mode
def seed_channel(...):
    latest = max(Channel Message + replys' ts)
    self._state[channel_id] = latest  # read cheack
Enter fullscreen mode Exit fullscreen mode

So, here is the answer: Save processed Slack message ts to a file by channel and process only subsequent messages.

What I Gained

We were able to prove that it is possible to create a decent AI assistant by combining ideas from Slack and OpenClaw! I am pleased with the results, as they were much better than expected. The important point is that processing tasks with the help of algorithmic programming, rather than relying too heavily on LLM, helps improve accuracy.

Thanks you for reading!

Top comments (0)