Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/green-tables-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chat": patch
---

Expose thread.toJSON() on the public Thread type so generated declarations match the runtime API and docs.
70 changes: 70 additions & 0 deletions packages/chat/src/types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { execSync } from "node:child_process";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, expect, it } from "vitest";

function createTempProject(source: string): string {
const tempDir = mkdtempSync(join(tmpdir(), "chat-public-types-"));

const tsconfig = {
compilerOptions: {
target: "ES2022",
module: "ESNext",
moduleResolution: "bundler",
strict: true,
skipLibCheck: true,
noEmit: true,
typeRoots: [join(import.meta.dirname, "../../../node_modules/@types")],
paths: {
chat: [join(import.meta.dirname, "index.ts")],
},
},
include: [join(tempDir, "index.ts")],
};

writeFileSync(
join(tempDir, "tsconfig.json"),
JSON.stringify(tsconfig, null, 2)
);
writeFileSync(join(tempDir, "index.ts"), source);

return tempDir;
}

describe("chat public types", () => {
it("exposes Thread.toJSON() to consumers", () => {
const tempDir = createTempProject(`
import type { SerializedThread, Thread } from "chat";

declare const thread: Thread<{ count: number }>;

const serialized: SerializedThread = thread.toJSON();

serialized satisfies SerializedThread;
`);

try {
execSync(`pnpm exec tsc --project ${tempDir}/tsconfig.json --noEmit`, {
cwd: join(import.meta.dirname, ".."),
encoding: "utf-8",
stdio: "pipe",
});
} catch (error) {
const execError = error as { stdout?: string; stderr?: string };
const output = execError.stdout || execError.stderr || String(error);
rmSync(tempDir, { recursive: true, force: true });

expect.fail(
"Consumer Thread.toJSON() type-check failed:\n\n" +
output +
"\n\nSnippet tested:\n" +
'import type { SerializedThread, Thread } from "chat";\n' +
"declare const thread: Thread<{ count: number }>;\n" +
"const serialized: SerializedThread = thread.toJSON();\n"
);
}

rmSync(tempDir, { recursive: true, force: true });
});
});
7 changes: 7 additions & 0 deletions packages/chat/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { ChatElement } from "./jsx-runtime";
import type { Logger, LogLevel } from "./logger";
import type { Message } from "./message";
import type { ModalElement } from "./modals";
import type { SerializedThread } from "./thread";

// =============================================================================
// Re-exports from extracted modules
Expand Down Expand Up @@ -940,6 +941,12 @@ export interface Thread<TState = Record<string, unknown>, TRawMessage = unknown>
*/
subscribe(): Promise<void>;

/**
* Serialize the thread to a plain JSON object.
* Use this to persist thread context or pass it across workflow boundaries.
*/
toJSON(): SerializedThread;

/**
* Unsubscribe from this thread.
*
Expand Down