b7f52be4c9
CI / Run CI (push) Has been cancelled
CI / check-backend (push) Has been cancelled
CI / check-frontend (push) Has been cancelled
CI / tests (push) Has been cancelled
CI / e2e-tests (push) Has been cancelled
Copilot Setup Steps / copilot-setup-steps (push) Has been cancelled
65 lines
1.7 KiB
TypeScript
65 lines
1.7 KiB
TypeScript
import { atom } from 'recoil';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
const COPILOT_THREAD_ID_KEY = 'chainlit-copilot-thread-id';
|
|
export const COPILOT_THREAD_CHANGED_EVENT_KEY =
|
|
'chainlit-copilot-thread-changed';
|
|
export class CopilotThreadChangedEventParams {
|
|
newThreadId?: string;
|
|
}
|
|
|
|
export const copilotThreadIdState = atom<string>({
|
|
key: 'CopilotThreadId',
|
|
default: '',
|
|
effects: [
|
|
({ setSelf, onSet }) => {
|
|
const savedValue = localStorage.getItem(COPILOT_THREAD_ID_KEY);
|
|
|
|
if (savedValue != null) {
|
|
try {
|
|
const parsedValue = JSON.parse(savedValue);
|
|
setSelf(parsedValue);
|
|
} catch (_error) {
|
|
const newThreadId = uuidv4();
|
|
localStorage.setItem(
|
|
COPILOT_THREAD_ID_KEY,
|
|
JSON.stringify(newThreadId)
|
|
);
|
|
setSelf(newThreadId);
|
|
}
|
|
} else {
|
|
const newThreadId = uuidv4();
|
|
localStorage.setItem(
|
|
COPILOT_THREAD_ID_KEY,
|
|
JSON.stringify(newThreadId)
|
|
);
|
|
setSelf(newThreadId);
|
|
}
|
|
|
|
onSet((newValue, _, isReset) => {
|
|
if (isReset) {
|
|
localStorage.removeItem(COPILOT_THREAD_ID_KEY);
|
|
} else {
|
|
localStorage.setItem(COPILOT_THREAD_ID_KEY, JSON.stringify(newValue));
|
|
}
|
|
});
|
|
}
|
|
]
|
|
});
|
|
|
|
export const getChainlitCopilotThreadId = () => {
|
|
const threadId = localStorage.getItem(COPILOT_THREAD_ID_KEY);
|
|
return threadId ? JSON.parse(threadId) : null;
|
|
};
|
|
|
|
export const clearChainlitCopilotThreadId = (newThreadId?: string) => {
|
|
window.dispatchEvent(
|
|
new CustomEvent<CopilotThreadChangedEventParams>(
|
|
COPILOT_THREAD_CHANGED_EVENT_KEY,
|
|
{
|
|
detail: { newThreadId }
|
|
}
|
|
)
|
|
);
|
|
};
|