Spaces:
Runtime error
Runtime error
File size: 2,615 Bytes
b9d9891 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
import { Conversation } from '@/types/chat';
import { useMemo, useReducer } from 'react';
// Extracts property names from initial state of reducer to allow typesafe dispatch objects
export type FieldNames<T> = {
[K in keyof T]: T[K] extends string ? K : K;
}[keyof T];
// Returns the Action Type for the dispatch object to be used for typing in things like context
export type ActionType<T> =
| { type: 'reset' }
| { type?: 'change'; field: FieldNames<T>; value: any };
// Returns a typed dispatch and state
export const useCreateReducer = <T>({ initialState }: { initialState: T }) => {
type Action =
| { type: 'reset' }
| { type?: 'change'; field: FieldNames<T>; value: any };
const reducer = (state: T, action: Action) => {
if (!action.type) return { ...state, [action.field]: action.value };
if (action.type === 'reset') return initialState;
throw new Error();
};
const [state, dispatch] = useReducer(reducer, initialState);
return useMemo(() => ({ state, dispatch }), [state, dispatch]);
};
export const updateConversation = (
updatedConversation: Conversation,
allConversations: Conversation[],
) => {
const updatedConversations = allConversations.map((c) => {
if (c.id === updatedConversation.id) {
return updatedConversation;
}
return c;
});
saveConversation(updatedConversation);
saveConversations(updatedConversations);
return {
single: updatedConversation,
all: updatedConversations,
};
};
export const saveConversation = (conversation: Conversation) => {
localStorage.setItem('selectedConversation', JSON.stringify(conversation));
};
export const saveConversations = (conversations: Conversation[]) => {
localStorage.setItem('conversationHistory', JSON.stringify(conversations));
};
export function throttle<T extends (...args: any[]) => any>(
func: T,
limit: number,
): T {
let lastFunc: ReturnType<typeof setTimeout>;
let lastRan: number;
return ((...args) => {
if (!lastRan) {
func(...args);
lastRan = Date.now();
} else {
clearTimeout(lastFunc);
lastFunc = setTimeout(() => {
if (Date.now() - lastRan >= limit) {
func(...args);
lastRan = Date.now();
}
}, limit - (Date.now() - lastRan));
}
}) as T;
}
export const DEFAULT_SYSTEM_PROMPT =
process.env.NEXT_PUBLIC_DEFAULT_SYSTEM_PROMPT ||
"Follow the user's instructions carefully. Respond using markdown.";
export const DEFAULT_TEMPERATURE =
parseFloat(process.env.NEXT_PUBLIC_DEFAULT_TEMPERATURE || "1");
|