fix: improve push to github option (#1111)
* feat: better push to githubbutton * added url update on push to github
This commit is contained in:
@@ -92,6 +92,7 @@ export function useGit() {
|
||||
},
|
||||
onAuthFailure: (url, _auth) => {
|
||||
toast.error(`Error Authenticating with ${url.split('/')[2]}`);
|
||||
throw `Error Authenticating with ${url.split('/')[2]}`;
|
||||
},
|
||||
onAuthSuccess: (url, auth) => {
|
||||
saveGitAuth(url, auth);
|
||||
@@ -107,6 +108,8 @@ export function useGit() {
|
||||
return { workdir: webcontainer.workdir, data };
|
||||
} catch (error) {
|
||||
console.error('Git clone error:', error);
|
||||
|
||||
// toast.error(`Git clone error ${(error as any).message||""}`);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
|
||||
@@ -2,6 +2,11 @@ import type { Message } from 'ai';
|
||||
import { createScopedLogger } from '~/utils/logger';
|
||||
import type { ChatHistoryItem } from './useChatHistory';
|
||||
|
||||
export interface IChatMetadata {
|
||||
gitUrl: string;
|
||||
gitBranch?: string;
|
||||
}
|
||||
|
||||
const logger = createScopedLogger('ChatHistory');
|
||||
|
||||
// this is used at the top level and never rejects
|
||||
@@ -53,6 +58,7 @@ export async function setMessages(
|
||||
urlId?: string,
|
||||
description?: string,
|
||||
timestamp?: string,
|
||||
metadata?: IChatMetadata,
|
||||
): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const transaction = db.transaction('chats', 'readwrite');
|
||||
@@ -69,6 +75,7 @@ export async function setMessages(
|
||||
urlId,
|
||||
description,
|
||||
timestamp: timestamp ?? new Date().toISOString(),
|
||||
metadata,
|
||||
});
|
||||
|
||||
request.onsuccess = () => resolve();
|
||||
@@ -204,6 +211,7 @@ export async function createChatFromMessages(
|
||||
db: IDBDatabase,
|
||||
description: string,
|
||||
messages: Message[],
|
||||
metadata?: IChatMetadata,
|
||||
): Promise<string> {
|
||||
const newId = await getNextId(db);
|
||||
const newUrlId = await getUrlId(db, newId); // Get a new urlId for the duplicated chat
|
||||
@@ -214,6 +222,8 @@ export async function createChatFromMessages(
|
||||
messages,
|
||||
newUrlId, // Use the new urlId
|
||||
description,
|
||||
undefined, // Use the current timestamp
|
||||
metadata,
|
||||
);
|
||||
|
||||
return newUrlId; // Return the urlId instead of id for navigation
|
||||
@@ -230,5 +240,19 @@ export async function updateChatDescription(db: IDBDatabase, id: string, descrip
|
||||
throw new Error('Description cannot be empty');
|
||||
}
|
||||
|
||||
await setMessages(db, id, chat.messages, chat.urlId, description, chat.timestamp);
|
||||
await setMessages(db, id, chat.messages, chat.urlId, description, chat.timestamp, chat.metadata);
|
||||
}
|
||||
|
||||
export async function updateChatMetadata(
|
||||
db: IDBDatabase,
|
||||
id: string,
|
||||
metadata: IChatMetadata | undefined,
|
||||
): Promise<void> {
|
||||
const chat = await getMessages(db, id);
|
||||
|
||||
if (!chat) {
|
||||
throw new Error('Chat not found');
|
||||
}
|
||||
|
||||
await setMessages(db, id, chat.messages, chat.urlId, chat.description, chat.timestamp, metadata);
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
setMessages,
|
||||
duplicateChat,
|
||||
createChatFromMessages,
|
||||
type IChatMetadata,
|
||||
} from './db';
|
||||
|
||||
export interface ChatHistoryItem {
|
||||
@@ -21,6 +22,7 @@ export interface ChatHistoryItem {
|
||||
description?: string;
|
||||
messages: Message[];
|
||||
timestamp: string;
|
||||
metadata?: IChatMetadata;
|
||||
}
|
||||
|
||||
const persistenceEnabled = !import.meta.env.VITE_DISABLE_PERSISTENCE;
|
||||
@@ -29,7 +31,7 @@ export const db = persistenceEnabled ? await openDatabase() : undefined;
|
||||
|
||||
export const chatId = atom<string | undefined>(undefined);
|
||||
export const description = atom<string | undefined>(undefined);
|
||||
|
||||
export const chatMetadata = atom<IChatMetadata | undefined>(undefined);
|
||||
export function useChatHistory() {
|
||||
const navigate = useNavigate();
|
||||
const { id: mixedId } = useLoaderData<{ id?: string }>();
|
||||
@@ -65,6 +67,7 @@ export function useChatHistory() {
|
||||
setUrlId(storedMessages.urlId);
|
||||
description.set(storedMessages.description);
|
||||
chatId.set(storedMessages.id);
|
||||
chatMetadata.set(storedMessages.metadata);
|
||||
} else {
|
||||
navigate('/', { replace: true });
|
||||
}
|
||||
@@ -81,6 +84,21 @@ export function useChatHistory() {
|
||||
return {
|
||||
ready: !mixedId || ready,
|
||||
initialMessages,
|
||||
updateChatMestaData: async (metadata: IChatMetadata) => {
|
||||
const id = chatId.get();
|
||||
|
||||
if (!db || !id) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await setMessages(db, id, initialMessages, urlId, description.get(), undefined, metadata);
|
||||
chatMetadata.set(metadata);
|
||||
} catch (error) {
|
||||
toast.error('Failed to update chat metadata');
|
||||
console.error(error);
|
||||
}
|
||||
},
|
||||
storeMessageHistory: async (messages: Message[]) => {
|
||||
if (!db || messages.length === 0) {
|
||||
return;
|
||||
@@ -109,7 +127,7 @@ export function useChatHistory() {
|
||||
}
|
||||
}
|
||||
|
||||
await setMessages(db, chatId.get() as string, messages, urlId, description.get());
|
||||
await setMessages(db, chatId.get() as string, messages, urlId, description.get(), undefined, chatMetadata.get());
|
||||
},
|
||||
duplicateCurrentChat: async (listItemId: string) => {
|
||||
if (!db || (!mixedId && !listItemId)) {
|
||||
@@ -125,13 +143,13 @@ export function useChatHistory() {
|
||||
console.log(error);
|
||||
}
|
||||
},
|
||||
importChat: async (description: string, messages: Message[]) => {
|
||||
importChat: async (description: string, messages: Message[], metadata?: IChatMetadata) => {
|
||||
if (!db) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const newId = await createChatFromMessages(db, description, messages);
|
||||
const newId = await createChatFromMessages(db, description, messages, metadata);
|
||||
window.location.href = `/chat/${newId}`;
|
||||
toast.success('Chat imported successfully');
|
||||
} catch (error) {
|
||||
|
||||
@@ -434,7 +434,7 @@ export class WorkbenchStore {
|
||||
return syncedFiles;
|
||||
}
|
||||
|
||||
async pushToGitHub(repoName: string, githubUsername?: string, ghToken?: string) {
|
||||
async pushToGitHub(repoName: string, commitMessage?: string, githubUsername?: string, ghToken?: string) {
|
||||
try {
|
||||
// Use cookies if username and token are not provided
|
||||
const githubToken = ghToken || Cookies.get('githubToken');
|
||||
@@ -523,7 +523,7 @@ export class WorkbenchStore {
|
||||
const { data: newCommit } = await octokit.git.createCommit({
|
||||
owner: repo.owner.login,
|
||||
repo: repo.name,
|
||||
message: 'Initial commit from your app',
|
||||
message: commitMessage || 'Initial commit from your app',
|
||||
tree: newTree.sha,
|
||||
parents: [latestCommitSha],
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user