Skip to content

Commit c4c3303

Browse files
[AI-SDK] Add default in-memory session store with custom store option
1 parent 68b48f4 commit c4c3303

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed

.changeset/eleven-kings-fix.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@thirdweb-dev/ai-sdk-provider": patch
3+
---
4+
5+
Default in-memory session store across thirdweb AI instances, option to pass your own

apps/playground-web/src/app/ai/ai-sdk/components/chat-container.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export function ChatContainer() {
7676
return (
7777
<Reasoning
7878
key={`${message.id}-reasoning-${i}`}
79-
className="w-full"
79+
className="w-full max-w-md"
8080
isStreaming={status === "streaming"}
8181
>
8282
<ReasoningTrigger />

packages/ai-sdk-provider/src/exports/thirdweb.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
export { createThirdwebAI, type ThirdwebProvider } from "../provider.js";
1+
export {
2+
createThirdwebAI,
3+
type SessionStore,
4+
type ThirdwebProvider,
5+
} from "../provider.js";
26
export type {
37
MonitorTransactionInput,
48
MonitorTransactionOutput,

packages/ai-sdk-provider/src/provider.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ export class ThirdwebProvider implements ProviderV2 {
544544

545545
constructor(config: ThirdwebConfig = {}) {
546546
this.config = config;
547-
this.session = new SessionStore();
547+
this.session = config.sessionStore || memorySessionStore;
548548
}
549549

550550
chat(id?: string, settings: ThirdwebSettings = {}) {
@@ -581,22 +581,29 @@ export class ThirdwebProvider implements ProviderV2 {
581581
}
582582
}
583583

584-
class SessionStore {
584+
export interface SessionStore {
585+
getSessionId(chatId: string): string | null;
586+
setSessionId(chatId: string, sessionId: string): void;
587+
clearSessionId(chatId: string): void;
588+
}
589+
590+
class InMemorySessionStore implements SessionStore {
585591
private sessionId: Map<string, string> = new Map();
586592

587-
getSessionId(chatId: string) {
593+
getSessionId(chatId: string): string | null {
588594
return this.sessionId.get(chatId) || null;
589595
}
590-
591-
setSessionId(chatId: string, sessionId: string) {
596+
setSessionId(chatId: string, sessionId: string): void {
592597
this.sessionId.set(chatId, sessionId);
593598
}
594-
595-
clearSessionId(chatId: string) {
599+
clearSessionId(chatId: string): void {
596600
this.sessionId.delete(chatId);
597601
}
598602
}
599603

604+
// singleton session store used as default if no session store is provided
605+
const memorySessionStore = new InMemorySessionStore();
606+
600607
// Factory function for easier usage
601608
export function createThirdwebAI(config: ThirdwebConfig = {}) {
602609
return new ThirdwebProvider(config);

packages/ai-sdk-provider/src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { UIDataTypes, UIMessage } from "ai";
2+
import type { SessionStore } from "./provider.js";
23
import type {
34
MonitorTransactionInput,
45
MonitorTransactionOutput,
@@ -26,6 +27,11 @@ export interface ThirdwebConfig {
2627
* Optional: The user wallet auth token (JWT) for executing transactions
2728
*/
2829
walletAuthToken?: string;
30+
/**
31+
* Optional: The session store to use for storing and retrieving thirdweb AI session IDs from ai SDK chat ids.
32+
* If not provided, a default in-memory session store will be used.
33+
*/
34+
sessionStore?: SessionStore;
2935
}
3036

3137
export interface ThirdwebSettings {

0 commit comments

Comments
 (0)