All projects & work

Discord Agent System

NexCore, my personal assistant in Discord.

Personal project

An infuriated Discord logo character uses a wrench to reconnect a glowing workflow between documents, memory and a completed task.
NexCore working through a stalled task. Concept artwork for the project.

TypeScript · Cloudflare Workers · Turso · Upstash · Python

View repository

The idea

I wanted to send my assistant a paper in Discord, ask it to look something up, and get the result in the same conversation. The hard part came after the first successful demo. If a Worker stopped halfway through, how would the next one know what had actually happened?

I built NexCore around that question. The Python bridge handles Discord, the TypeScript Worker coordinates model and tool calls, and Turso keeps the records that must survive either process restarting. A queued task can span several Worker invocations.

How NexCore carries a task across requests

How NexCore carries a task across requests
A Discord message can start a conversational turn or a durable task. The runner checkpoints its work in Turso. A separate delivery outbox carries the finished reply back to Discord.

The bridge owns the conversation with Discord

I keep the Gateway connection in a Python process. Its message handler ignores bot authors, checks my owner ID and, in a guild, checks whether I addressed the bot. That keeps ordinary channel traffic out of the assistant.

The bridge has separate client methods for uploading a document, analyzing images, submitting a task and streaming a turn. Once the Worker is responding, the bridge can edit a progress message in place. It owns Discord presentation while the Worker owns the task record.

Read the source · Python Discord Gateway bridge

How much of a conversation gets carried forward?

I keep recent messages, summaries and personal facts for different reasons. Recent messages tell the model what “that file” refers to. A preference can matter again several conversations later. A saved task checkpoint answers a different question entirely: where should this job resume?

When the execution context grows, I split off an older prefix for compaction. The split advances past tool-result messages so a retained result keeps the assistant call that produced it. If a single group exceeds capacity, the runner needs to pause rather than hand the model an orphaned result.

Read the source · Conversation state and personal context

Choosing a model and giving it tools

My model router expands a configured model into eligible provider and account routes. It tracks attempts and time remaining in the current invocation, consults route health, and classifies provider errors before trying a fallback. The task and its saved history live outside any one provider.

On the tool side I register adapters for documents, research, calculations and connected services. Discovery gives the model a smaller set of concrete tools for the job. The scheduler then uses dependencies and adapter metadata to decide which calls can share a batch.

  • Independent reads can run concurrently.
  • A dependent call can bind an argument to a field from an earlier result.
  • A failed dependency skips downstream calls. Conflicting writes wait their turn.

Read the source · Tool registry and capability adapters

Saving the decision before doing the work

I persist the model response before executing its requested tools. The action ledger keys each call by task, model step and call ID. It records the arguments, the adapter’s scope, the action status and the resulting receipt.

Consider a timeout just after a connected service accepts a write. A second Worker cannot infer the result from the broken connection. The runner can reuse an existing successful receipt. An ambiguous write gets an unknown outcome and pauses the task for review.

  • Each task has a generation, lease token and fence to coordinate competing workers.
  • A slice can commit a checkpoint and requeue when its time or request budget runs low.
  • Task inputs have sequence numbers. The saved input cursor tracks how far the runner has read.

Read the source · Durable execution runner

Finishing a task and delivering its reply

I keep separate outboxes for scheduling execution and delivering a reply. This matters when the model has finished and Discord is temporarily unavailable. The delivery worker only needs the saved payload and its stable delivery ID.

The delivery worker claims a short lease, sends the payload and commits the receipt. On a send error it defers the delivery. If the transport accepts a message but loses its acknowledgement, deduplication still depends on the transport supporting that stable ID.

The delivery boundary

The delivery path retries a saved payload independently of model inference and tool execution.

const lease = await store.claimDelivery(token, 30_000, deliveryId);
if (lease === undefined) return { status: "idle" };
try {
  await send(lease.id, lease.payload);
} catch {
  const delaySeconds = await store.deferDelivery(lease);
  return { status: "deferred", delaySeconds };
}
await store.completeDelivery(lease);
return { status: "delivered" };

Read the source · Execution and delivery outboxes

What recovery looks like from my side

If a Worker disappears, the minute-level scheduled handler runs recovery. Queue messages identify the task and generation. A busy task is retried later, and delivery messages have their own retry path. I can keep chatting while a longer job waits.

That distinction matters when I ask what happened. For an interrupted turn with audited actions, the assistant can summarize the counts of completed and failed actions. The record gives me a better starting point than asking another model to guess how far its predecessor got.

Read the source · Worker entry points

A research watch I can actually use

One reason I built these layers was to keep up with papers from Discord. I can ask NexCore to save a focused arXiv query or category, then ask for a digest from that watch later. The conversation can move on without losing the watch definition.

The research adapter parses paper IDs, titles, abstracts and categories from the arXiv feed. It strips the version suffix from an ID and builds a link to the abstract page. Each digest marks papers already seen for that watch, so I can tell which links deserve another look.

Read the source · Research watch and digest adapter

Scope & practical notes

  • Recovery still depends on provider availability and the saved task state.