mirror of
https://github.com/stackblitz-labs/bolt.diy
synced 2025-03-09 21:50:36 +00:00
fix: support for multiple artifacts to support newer llm
This commit is contained in:
parent
4404f4a5c0
commit
67ebc54ac1
@ -23,15 +23,16 @@ if (import.meta.hot) {
|
|||||||
|
|
||||||
interface ArtifactProps {
|
interface ArtifactProps {
|
||||||
messageId: string;
|
messageId: string;
|
||||||
|
artifactId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Artifact = memo(({ messageId }: ArtifactProps) => {
|
export const Artifact = memo(({ artifactId }: ArtifactProps) => {
|
||||||
const userToggledActions = useRef(false);
|
const userToggledActions = useRef(false);
|
||||||
const [showActions, setShowActions] = useState(false);
|
const [showActions, setShowActions] = useState(false);
|
||||||
const [allActionFinished, setAllActionFinished] = useState(false);
|
const [allActionFinished, setAllActionFinished] = useState(false);
|
||||||
|
|
||||||
const artifacts = useStore(workbenchStore.artifacts);
|
const artifacts = useStore(workbenchStore.artifacts);
|
||||||
const artifact = artifacts[messageId];
|
const artifact = artifacts[artifactId];
|
||||||
|
|
||||||
const actions = useStore(
|
const actions = useStore(
|
||||||
computed(artifact.runner.actions, (actions) => {
|
computed(artifact.runner.actions, (actions) => {
|
||||||
|
@ -25,12 +25,17 @@ export const Markdown = memo(({ children, html = false, limitedMarkdown = false
|
|||||||
div: ({ className, children, node, ...props }) => {
|
div: ({ className, children, node, ...props }) => {
|
||||||
if (className?.includes('__boltArtifact__')) {
|
if (className?.includes('__boltArtifact__')) {
|
||||||
const messageId = node?.properties.dataMessageId as string;
|
const messageId = node?.properties.dataMessageId as string;
|
||||||
|
const artifactId = node?.properties.dataArtifactId as string;
|
||||||
|
|
||||||
if (!messageId) {
|
if (!messageId) {
|
||||||
logger.error(`Invalid message id ${messageId}`);
|
logger.error(`Invalid message id ${messageId}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return <Artifact messageId={messageId} />;
|
if (!artifactId) {
|
||||||
|
logger.error(`Invalid artifact id ${artifactId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return <Artifact messageId={messageId} artifactId={artifactId} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (className?.includes('__boltThought__')) {
|
if (className?.includes('__boltThought__')) {
|
||||||
|
@ -12,6 +12,7 @@ const logger = createScopedLogger('MessageParser');
|
|||||||
|
|
||||||
export interface ArtifactCallbackData extends BoltArtifactData {
|
export interface ArtifactCallbackData extends BoltArtifactData {
|
||||||
messageId: string;
|
messageId: string;
|
||||||
|
artifactId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ActionCallbackData {
|
export interface ActionCallbackData {
|
||||||
@ -34,6 +35,7 @@ export interface ParserCallbacks {
|
|||||||
|
|
||||||
interface ElementFactoryProps {
|
interface ElementFactoryProps {
|
||||||
messageId: string;
|
messageId: string;
|
||||||
|
artifactId: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
type ElementFactory = (props: ElementFactoryProps) => string;
|
type ElementFactory = (props: ElementFactoryProps) => string;
|
||||||
@ -47,6 +49,7 @@ interface MessageState {
|
|||||||
position: number;
|
position: number;
|
||||||
insideArtifact: boolean;
|
insideArtifact: boolean;
|
||||||
insideAction: boolean;
|
insideAction: boolean;
|
||||||
|
artifactCounter: number;
|
||||||
currentArtifact?: BoltArtifactData;
|
currentArtifact?: BoltArtifactData;
|
||||||
currentAction: BoltActionData;
|
currentAction: BoltActionData;
|
||||||
actionId: number;
|
actionId: number;
|
||||||
@ -81,6 +84,7 @@ export class StreamingMessageParser {
|
|||||||
position: 0,
|
position: 0,
|
||||||
insideAction: false,
|
insideAction: false,
|
||||||
insideArtifact: false,
|
insideArtifact: false,
|
||||||
|
artifactCounter: 0,
|
||||||
currentAction: { content: '' },
|
currentAction: { content: '' },
|
||||||
actionId: 0,
|
actionId: 0,
|
||||||
};
|
};
|
||||||
@ -187,7 +191,11 @@ export class StreamingMessageParser {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else if (artifactCloseIndex !== -1) {
|
} else if (artifactCloseIndex !== -1) {
|
||||||
this._options.callbacks?.onArtifactClose?.({ messageId, ...currentArtifact });
|
this._options.callbacks?.onArtifactClose?.({
|
||||||
|
messageId,
|
||||||
|
artifactId: currentArtifact.id,
|
||||||
|
...currentArtifact,
|
||||||
|
});
|
||||||
|
|
||||||
state.insideArtifact = false;
|
state.insideArtifact = false;
|
||||||
state.currentArtifact = undefined;
|
state.currentArtifact = undefined;
|
||||||
@ -220,7 +228,9 @@ export class StreamingMessageParser {
|
|||||||
|
|
||||||
const artifactTitle = this.#extractAttribute(artifactTag, 'title') as string;
|
const artifactTitle = this.#extractAttribute(artifactTag, 'title') as string;
|
||||||
const type = this.#extractAttribute(artifactTag, 'type') as string;
|
const type = this.#extractAttribute(artifactTag, 'type') as string;
|
||||||
const artifactId = this.#extractAttribute(artifactTag, 'id') as string;
|
|
||||||
|
// const artifactId = this.#extractAttribute(artifactTag, 'id') as string;
|
||||||
|
const artifactId = `${messageId}-${state.artifactCounter++}`;
|
||||||
|
|
||||||
if (!artifactTitle) {
|
if (!artifactTitle) {
|
||||||
logger.warn('Artifact title missing');
|
logger.warn('Artifact title missing');
|
||||||
@ -240,11 +250,15 @@ export class StreamingMessageParser {
|
|||||||
|
|
||||||
state.currentArtifact = currentArtifact;
|
state.currentArtifact = currentArtifact;
|
||||||
|
|
||||||
this._options.callbacks?.onArtifactOpen?.({ messageId, ...currentArtifact });
|
this._options.callbacks?.onArtifactOpen?.({
|
||||||
|
messageId,
|
||||||
|
artifactId: currentArtifact.id,
|
||||||
|
...currentArtifact,
|
||||||
|
});
|
||||||
|
|
||||||
const artifactFactory = this._options.artifactElement ?? createArtifactElement;
|
const artifactFactory = this._options.artifactElement ?? createArtifactElement;
|
||||||
|
|
||||||
output += artifactFactory({ messageId });
|
output += artifactFactory({ messageId, artifactId });
|
||||||
|
|
||||||
i = openTagEnd + 1;
|
i = openTagEnd + 1;
|
||||||
} else {
|
} else {
|
||||||
|
@ -255,17 +255,17 @@ export class WorkbenchStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addArtifact({ messageId, title, id, type }: ArtifactCallbackData) {
|
addArtifact({ messageId, title, id, type }: ArtifactCallbackData) {
|
||||||
const artifact = this.#getArtifact(messageId);
|
const artifact = this.#getArtifact(id);
|
||||||
|
|
||||||
if (artifact) {
|
if (artifact) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.artifactIdList.includes(messageId)) {
|
if (!this.artifactIdList.includes(id)) {
|
||||||
this.artifactIdList.push(messageId);
|
this.artifactIdList.push(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.artifacts.setKey(messageId, {
|
this.artifacts.setKey(id, {
|
||||||
id,
|
id,
|
||||||
title,
|
title,
|
||||||
closed: false,
|
closed: false,
|
||||||
@ -284,14 +284,14 @@ export class WorkbenchStore {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
updateArtifact({ messageId }: ArtifactCallbackData, state: Partial<ArtifactUpdateState>) {
|
updateArtifact({ artifactId }: ArtifactCallbackData, state: Partial<ArtifactUpdateState>) {
|
||||||
const artifact = this.#getArtifact(messageId);
|
const artifact = this.#getArtifact(artifactId);
|
||||||
|
|
||||||
if (!artifact) {
|
if (!artifact) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.artifacts.setKey(messageId, { ...artifact, ...state });
|
this.artifacts.setKey(artifactId, { ...artifact, ...state });
|
||||||
}
|
}
|
||||||
addAction(data: ActionCallbackData) {
|
addAction(data: ActionCallbackData) {
|
||||||
// this._addAction(data);
|
// this._addAction(data);
|
||||||
@ -299,9 +299,9 @@ export class WorkbenchStore {
|
|||||||
this.addToExecutionQueue(() => this._addAction(data));
|
this.addToExecutionQueue(() => this._addAction(data));
|
||||||
}
|
}
|
||||||
async _addAction(data: ActionCallbackData) {
|
async _addAction(data: ActionCallbackData) {
|
||||||
const { messageId } = data;
|
const { artifactId } = data;
|
||||||
|
|
||||||
const artifact = this.#getArtifact(messageId);
|
const artifact = this.#getArtifact(artifactId);
|
||||||
|
|
||||||
if (!artifact) {
|
if (!artifact) {
|
||||||
unreachable('Artifact not found');
|
unreachable('Artifact not found');
|
||||||
@ -318,9 +318,9 @@ export class WorkbenchStore {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
async _runAction(data: ActionCallbackData, isStreaming: boolean = false) {
|
async _runAction(data: ActionCallbackData, isStreaming: boolean = false) {
|
||||||
const { messageId } = data;
|
const { artifactId } = data;
|
||||||
|
|
||||||
const artifact = this.#getArtifact(messageId);
|
const artifact = this.#getArtifact(artifactId);
|
||||||
|
|
||||||
if (!artifact) {
|
if (!artifact) {
|
||||||
unreachable('Artifact not found');
|
unreachable('Artifact not found');
|
||||||
|
Loading…
Reference in New Issue
Block a user