javascriptlangchainlanggraphlangchain-js

How to delete a thread with MemorySaver?


I'm using langgraph.js to create a simple chatbot.

When the session has ended I'd like to be able to delete the thread but I haven't been able to find any way to do this in the docs.

     this.graph = new StateGraph(AssistantState)
            .addNode('route_node', this.routeNode.bind(this))
            .addNode('init_node', this.initNode.bind(this))
            .addNode('agent', this.agentNode.bind(this))

            .addEdge(START, 'route_node')
            .addConditionalEdges('route_node', async (state) => state.route)

            .addEdge('init_node', END)
            .addEdge('agent', END)

            .compile({ checkpointer: new MemorySaver() })

Any ideas how this is done? Thanks!


Solution

  • as far as I've investigated there is no built in way to delete a thread, which doesn't make sense in my opinion if that's the case. Anyways here is a workaround:

    
    function createEmptyCheckpoint() {
        return {
            v: 1,
            ts: new Date().toISOString(),
            id: crypto.randomUUID(),
            channel_values: {},
            channel_versions: {
                __start__: 1,
            },
            versions_seen: {
                __input__: {},
                __start__: {}
            },
            pending_sends: [],
        };
    }
    
    async function clearThread(checkpointer: any, threadId: string) {
        const config = {
            configurable: {
                thread_id: threadId,
                checkpoint_ns: ""
            }
        };
    
        const emptyCheckpoint = createEmptyCheckpoint();
    
        await checkpointer.put(config, emptyCheckpoint, {}, {});
    }
    

    https://github.com/langchain-ai/langchain/discussions/19744