2024-07-18 21:07:04 +00:00
|
|
|
import { atom, map, type MapStore, type ReadableAtom, type WritableAtom } from 'nanostores';
|
|
|
|
import type { EditorDocument, ScrollPosition } from '../../components/editor/codemirror/CodeMirrorEditor';
|
2024-07-17 18:54:46 +00:00
|
|
|
import type { BoltAction } from '../../types/actions';
|
|
|
|
import { unreachable } from '../../utils/unreachable';
|
|
|
|
import { ActionRunner } from '../runtime/action-runner';
|
|
|
|
import type { ActionCallbackData, ArtifactCallbackData } from '../runtime/message-parser';
|
|
|
|
import { webcontainer } from '../webcontainer';
|
2024-07-18 09:10:12 +00:00
|
|
|
import { chatStore } from './chat';
|
2024-07-18 21:07:04 +00:00
|
|
|
import { EditorStore } from './editor';
|
|
|
|
import { FilesStore, type FileMap } from './files';
|
2024-07-17 18:54:46 +00:00
|
|
|
import { PreviewsStore } from './previews';
|
|
|
|
|
2024-07-18 09:10:12 +00:00
|
|
|
const MIN_SPINNER_TIME = 200;
|
|
|
|
|
|
|
|
export type BaseActionState = BoltAction & {
|
2024-07-17 18:54:46 +00:00
|
|
|
status: 'running' | 'complete' | 'pending' | 'aborted';
|
2024-07-18 09:10:12 +00:00
|
|
|
executing: boolean;
|
2024-07-17 18:54:46 +00:00
|
|
|
abort?: () => void;
|
|
|
|
};
|
|
|
|
|
2024-07-18 09:10:12 +00:00
|
|
|
export type FailedActionState = BoltAction &
|
|
|
|
Omit<BaseActionState, 'status'> & {
|
|
|
|
status: 'failed';
|
|
|
|
error: string;
|
|
|
|
};
|
2024-07-17 18:54:46 +00:00
|
|
|
|
2024-07-18 09:10:12 +00:00
|
|
|
export type ActionState = BaseActionState | FailedActionState;
|
|
|
|
|
|
|
|
type BaseActionUpdate = Partial<Pick<BaseActionState, 'status' | 'executing' | 'abort'>>;
|
2024-07-17 18:54:46 +00:00
|
|
|
|
|
|
|
export type ActionStateUpdate =
|
2024-07-18 09:10:12 +00:00
|
|
|
| BaseActionUpdate
|
|
|
|
| (Omit<BaseActionUpdate, 'status'> & { status: 'failed'; error: string });
|
2024-07-17 18:54:46 +00:00
|
|
|
|
|
|
|
export interface ArtifactState {
|
|
|
|
title: string;
|
|
|
|
closed: boolean;
|
|
|
|
currentActionPromise: Promise<void>;
|
|
|
|
actions: MapStore<Record<string, ActionState>>;
|
|
|
|
}
|
|
|
|
|
|
|
|
type Artifacts = MapStore<Record<string, ArtifactState>>;
|
|
|
|
|
|
|
|
export class WorkbenchStore {
|
|
|
|
#actionRunner = new ActionRunner(webcontainer);
|
|
|
|
#previewsStore = new PreviewsStore(webcontainer);
|
2024-07-18 21:07:04 +00:00
|
|
|
#filesStore = new FilesStore(webcontainer);
|
|
|
|
#editorStore = new EditorStore(webcontainer);
|
2024-07-17 18:54:46 +00:00
|
|
|
|
|
|
|
artifacts: Artifacts = import.meta.hot?.data.artifacts ?? map({});
|
|
|
|
|
|
|
|
showWorkbench: WritableAtom<boolean> = import.meta.hot?.data.showWorkbench ?? atom(false);
|
|
|
|
|
|
|
|
get previews() {
|
|
|
|
return this.#previewsStore.previews;
|
|
|
|
}
|
|
|
|
|
2024-07-18 21:07:04 +00:00
|
|
|
get files() {
|
|
|
|
return this.#filesStore.files;
|
|
|
|
}
|
|
|
|
|
|
|
|
get currentDocument(): ReadableAtom<EditorDocument | undefined> {
|
|
|
|
return this.#editorStore.currentDocument;
|
|
|
|
}
|
|
|
|
|
|
|
|
get selectedFile(): ReadableAtom<string | undefined> {
|
|
|
|
return this.#editorStore.selectedFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
setDocuments(files: FileMap) {
|
|
|
|
this.#editorStore.setDocuments(files);
|
|
|
|
}
|
|
|
|
|
2024-07-17 18:54:46 +00:00
|
|
|
setShowWorkbench(show: boolean) {
|
|
|
|
this.showWorkbench.set(show);
|
|
|
|
}
|
|
|
|
|
2024-07-18 21:07:04 +00:00
|
|
|
setCurrentDocumentContent(newContent: string) {
|
|
|
|
const filePath = this.currentDocument.get()?.filePath;
|
|
|
|
|
|
|
|
if (!filePath) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.#editorStore.updateFile(filePath, newContent);
|
|
|
|
}
|
|
|
|
|
|
|
|
setCurrentDocumentScrollPosition(position: ScrollPosition) {
|
|
|
|
const editorDocument = this.currentDocument.get();
|
|
|
|
|
|
|
|
if (!editorDocument) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { filePath } = editorDocument;
|
|
|
|
|
|
|
|
this.#editorStore.updateScrollPosition(filePath, position);
|
|
|
|
}
|
|
|
|
|
|
|
|
setSelectedFile(filePath: string | undefined) {
|
|
|
|
this.#editorStore.setSelectedFile(filePath);
|
|
|
|
}
|
|
|
|
|
2024-07-18 09:10:12 +00:00
|
|
|
abortAllActions() {
|
|
|
|
for (const [, artifact] of Object.entries(this.artifacts.get())) {
|
|
|
|
for (const [, action] of Object.entries(artifact.actions.get())) {
|
|
|
|
if (action.status === 'running') {
|
|
|
|
action.abort?.();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-17 18:54:46 +00:00
|
|
|
addArtifact({ id, messageId, title }: ArtifactCallbackData) {
|
|
|
|
const artifacts = this.artifacts.get();
|
|
|
|
const artifactKey = getArtifactKey(id, messageId);
|
|
|
|
const artifact = artifacts[artifactKey];
|
|
|
|
|
|
|
|
if (artifact) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.artifacts.setKey(artifactKey, {
|
|
|
|
title,
|
|
|
|
closed: false,
|
|
|
|
actions: map({}),
|
|
|
|
currentActionPromise: Promise.resolve(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
updateArtifact({ id, messageId }: ArtifactCallbackData, state: Partial<ArtifactState>) {
|
|
|
|
const artifacts = this.artifacts.get();
|
|
|
|
const key = getArtifactKey(id, messageId);
|
|
|
|
const artifact = artifacts[key];
|
|
|
|
|
|
|
|
if (!artifact) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.artifacts.setKey(key, { ...artifact, ...state });
|
|
|
|
}
|
|
|
|
|
2024-07-18 09:10:12 +00:00
|
|
|
async addAction(data: ActionCallbackData) {
|
2024-07-17 18:54:46 +00:00
|
|
|
const { artifactId, messageId, actionId } = data;
|
|
|
|
|
|
|
|
const artifacts = this.artifacts.get();
|
|
|
|
const key = getArtifactKey(artifactId, messageId);
|
|
|
|
const artifact = artifacts[key];
|
|
|
|
|
|
|
|
if (!artifact) {
|
|
|
|
unreachable('Artifact not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
const actions = artifact.actions.get();
|
|
|
|
const action = actions[actionId];
|
|
|
|
|
|
|
|
if (action) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-18 09:10:12 +00:00
|
|
|
artifact.actions.setKey(actionId, { ...data.action, status: 'pending', executing: false });
|
2024-07-17 18:54:46 +00:00
|
|
|
|
2024-07-18 09:10:12 +00:00
|
|
|
artifact.currentActionPromise.then(() => {
|
|
|
|
if (chatStore.get().aborted) {
|
|
|
|
return;
|
|
|
|
}
|
2024-07-17 18:54:46 +00:00
|
|
|
|
2024-07-18 09:10:12 +00:00
|
|
|
this.#updateAction(key, actionId, { status: 'running' });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async runAction(data: ActionCallbackData) {
|
|
|
|
const { artifactId, messageId, actionId } = data;
|
|
|
|
|
|
|
|
const artifacts = this.artifacts.get();
|
|
|
|
const key = getArtifactKey(artifactId, messageId);
|
|
|
|
const artifact = artifacts[key];
|
|
|
|
|
|
|
|
if (!artifact) {
|
|
|
|
unreachable('Artifact not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
const actions = artifact.actions.get();
|
|
|
|
const action = actions[actionId];
|
2024-07-17 18:54:46 +00:00
|
|
|
|
2024-07-18 09:10:12 +00:00
|
|
|
if (!action) {
|
|
|
|
unreachable('Expected action to exist');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.executing || action.status === 'complete' || action.status === 'failed' || action.status === 'aborted') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact.currentActionPromise = artifact.currentActionPromise.then(async () => {
|
|
|
|
if (chatStore.get().aborted) {
|
|
|
|
return;
|
|
|
|
}
|
2024-07-17 18:54:46 +00:00
|
|
|
|
2024-07-18 09:10:12 +00:00
|
|
|
const abortController = new AbortController();
|
2024-07-17 18:54:46 +00:00
|
|
|
|
2024-07-18 09:10:12 +00:00
|
|
|
this.#updateAction(key, actionId, {
|
|
|
|
status: 'running',
|
|
|
|
executing: true,
|
|
|
|
abort: () => {
|
|
|
|
abortController.abort();
|
|
|
|
this.#updateAction(key, actionId, { status: 'aborted' });
|
|
|
|
},
|
|
|
|
});
|
2024-07-17 18:54:46 +00:00
|
|
|
|
2024-07-18 09:10:12 +00:00
|
|
|
try {
|
|
|
|
await Promise.all([
|
|
|
|
this.#actionRunner.runAction(data, abortController.signal),
|
|
|
|
new Promise((resolve) => setTimeout(resolve, MIN_SPINNER_TIME)),
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (!abortController.signal.aborted) {
|
|
|
|
this.#updateAction(key, actionId, { status: 'complete' });
|
|
|
|
}
|
2024-07-17 18:54:46 +00:00
|
|
|
} catch (error) {
|
|
|
|
this.#updateAction(key, actionId, { status: 'failed', error: 'Action failed' });
|
|
|
|
|
|
|
|
throw error;
|
2024-07-18 09:10:12 +00:00
|
|
|
} finally {
|
|
|
|
this.#updateAction(key, actionId, { executing: false });
|
2024-07-17 18:54:46 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#updateAction(artifactId: string, actionId: string, newState: ActionStateUpdate) {
|
|
|
|
const artifacts = this.artifacts.get();
|
|
|
|
const artifact = artifacts[artifactId];
|
|
|
|
|
|
|
|
if (!artifact) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const actions = artifact.actions.get();
|
|
|
|
|
|
|
|
artifact.actions.setKey(actionId, { ...actions[actionId], ...newState });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getArtifactKey(artifactId: string, messageId: string) {
|
|
|
|
return `${artifactId}_${messageId}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const workbenchStore = new WorkbenchStore();
|
|
|
|
|
|
|
|
if (import.meta.hot) {
|
|
|
|
import.meta.hot.data.artifacts = workbenchStore.artifacts;
|
|
|
|
import.meta.hot.data.showWorkbench = workbenchStore.showWorkbench;
|
|
|
|
}
|