Getting Thread IDs
Thread IDs are generated by Docuworm when a conversation thread is established. Use helper functions or element methods to read them.
Before waiting for a thread ID, trigger thread creation by opening the widget or queueing uploads/sources.
Preferred async approach
Section titled “Preferred async approach”Use waitForThreadId() or getThreadId().
import { openWidget, waitForThreadId, getThreadId, getDocuwormThreadId,} from "https://cdn.docuworm.ai/docuworm.js";
const widget = document.querySelector("docuworm-chat");openWidget(widget);
const a = await waitForThreadId({ element: widget, timeoutMs: 30000 });const b = await getThreadId({ element: widget });const c = await getDocuwormThreadId({ element: widget });Notes:
getThreadId()andgetDocuwormThreadId()are equivalent aliases.- If the widget is not ready before timeout, an error is thrown.
Synchronous check
Section titled “Synchronous check”Use this when you need a non-blocking check:
import { getThreadIdSync, getDocuwormThreadIdSync,} from "https://cdn.docuworm.ai/docuworm.js";
const widget = document.querySelector("docuworm-chat");
const id1 = getThreadIdSync({ element: widget });const id2 = getDocuwormThreadIdSync({ element: widget });
if (!id1) { console.log("Thread not ready yet");}Element method access
Section titled “Element method access”The web component also exposes methods directly.
const widget = document.querySelector("docuworm-chat");
const immediate = widget?.getThreadIdSync?.() ?? null;const awaited = await widget?.waitForThreadId?.(30000);Sync pattern for production apps
Section titled “Sync pattern for production apps”const widget = document.querySelector("docuworm-chat");openWidget(widget);const threadId = await waitForThreadId({ element: widget, timeoutMs: 30000 });
await fetch("/api/docuworm/thread", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ threadId }),});React, Vue, Svelte notes
Section titled “React, Vue, Svelte notes”Use the same helper API and pass the framework-held element reference.
- React:
useRefanduseEffect - Vue:
refandonMounted - Svelte:
bind:thisandonMount