feature(code-streaming): added code streaming to editor while AI is writing code
This commit is contained in:
@@ -12,6 +12,7 @@ import { TerminalStore } from './terminal';
|
||||
import JSZip from 'jszip';
|
||||
import { saveAs } from 'file-saver';
|
||||
import { Octokit } from "@octokit/rest";
|
||||
import * as nodePath from 'node:path';
|
||||
|
||||
export interface ArtifactState {
|
||||
id: string;
|
||||
@@ -258,7 +259,7 @@ export class WorkbenchStore {
|
||||
artifact.runner.addAction(data);
|
||||
}
|
||||
|
||||
async runAction(data: ActionCallbackData) {
|
||||
async runAction(data: ActionCallbackData, isStreaming: boolean = false) {
|
||||
const { messageId } = data;
|
||||
|
||||
const artifact = this.#getArtifact(messageId);
|
||||
@@ -266,8 +267,29 @@ export class WorkbenchStore {
|
||||
if (!artifact) {
|
||||
unreachable('Artifact not found');
|
||||
}
|
||||
if (data.action.type === 'file') {
|
||||
let wc = await webcontainer
|
||||
const fullPath = nodePath.join(wc.workdir, data.action.filePath);
|
||||
if (this.selectedFile.value !== fullPath) {
|
||||
this.setSelectedFile(fullPath);
|
||||
}
|
||||
if (this.currentView.value !== 'code') {
|
||||
this.currentView.set('code');
|
||||
}
|
||||
const doc = this.#editorStore.documents.get()[fullPath];
|
||||
if (!doc) {
|
||||
await artifact.runner.runAction(data, isStreaming);
|
||||
}
|
||||
|
||||
artifact.runner.runAction(data);
|
||||
this.#editorStore.updateFile(fullPath, data.action.content);
|
||||
|
||||
if (!isStreaming) {
|
||||
this.resetCurrentDocument();
|
||||
await artifact.runner.runAction(data);
|
||||
}
|
||||
} else {
|
||||
artifact.runner.runAction(data);
|
||||
}
|
||||
}
|
||||
|
||||
#getArtifact(id: string) {
|
||||
@@ -336,20 +358,20 @@ export class WorkbenchStore {
|
||||
}
|
||||
|
||||
async pushToGitHub(repoName: string, githubUsername: string, ghToken: string) {
|
||||
|
||||
|
||||
try {
|
||||
// Get the GitHub auth token from environment variables
|
||||
const githubToken = ghToken;
|
||||
|
||||
|
||||
const owner = githubUsername;
|
||||
|
||||
|
||||
if (!githubToken) {
|
||||
throw new Error('GitHub token is not set in environment variables');
|
||||
}
|
||||
|
||||
|
||||
// Initialize Octokit with the auth token
|
||||
const octokit = new Octokit({ auth: githubToken });
|
||||
|
||||
|
||||
// Check if the repository already exists before creating it
|
||||
let repo
|
||||
try {
|
||||
@@ -368,13 +390,13 @@ export class WorkbenchStore {
|
||||
throw error; // Some other error occurred
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Get all files
|
||||
const files = this.files.get();
|
||||
if (!files || Object.keys(files).length === 0) {
|
||||
throw new Error('No files found to push');
|
||||
}
|
||||
|
||||
|
||||
// Create blobs for each file
|
||||
const blobs = await Promise.all(
|
||||
Object.entries(files).map(async ([filePath, dirent]) => {
|
||||
@@ -389,13 +411,13 @@ export class WorkbenchStore {
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
const validBlobs = blobs.filter(Boolean); // Filter out any undefined blobs
|
||||
|
||||
|
||||
if (validBlobs.length === 0) {
|
||||
throw new Error('No valid files to push');
|
||||
}
|
||||
|
||||
|
||||
// Get the latest commit SHA (assuming main branch, update dynamically if needed)
|
||||
const { data: ref } = await octokit.git.getRef({
|
||||
owner: repo.owner.login,
|
||||
@@ -403,7 +425,7 @@ export class WorkbenchStore {
|
||||
ref: `heads/${repo.default_branch || 'main'}`, // Handle dynamic branch
|
||||
});
|
||||
const latestCommitSha = ref.object.sha;
|
||||
|
||||
|
||||
// Create a new tree
|
||||
const { data: newTree } = await octokit.git.createTree({
|
||||
owner: repo.owner.login,
|
||||
@@ -416,7 +438,7 @@ export class WorkbenchStore {
|
||||
sha: blob!.sha,
|
||||
})),
|
||||
});
|
||||
|
||||
|
||||
// Create a new commit
|
||||
const { data: newCommit } = await octokit.git.createCommit({
|
||||
owner: repo.owner.login,
|
||||
@@ -425,7 +447,7 @@ export class WorkbenchStore {
|
||||
tree: newTree.sha,
|
||||
parents: [latestCommitSha],
|
||||
});
|
||||
|
||||
|
||||
// Update the reference
|
||||
await octokit.git.updateRef({
|
||||
owner: repo.owner.login,
|
||||
@@ -433,7 +455,7 @@ export class WorkbenchStore {
|
||||
ref: `heads/${repo.default_branch || 'main'}`, // Handle dynamic branch
|
||||
sha: newCommit.sha,
|
||||
});
|
||||
|
||||
|
||||
alert(`Repository created and code pushed: ${repo.html_url}`);
|
||||
} catch (error) {
|
||||
console.error('Error pushing to GitHub:', error instanceof Error ? error.message : String(error));
|
||||
|
||||
Reference in New Issue
Block a user