@@ -18,8 +18,9 @@ export interface ArtifactState {
1818}
1919
2020export type ArtifactUpdateState = Pick < ArtifactState , 'title' | 'closed' > ;
21-
22- type Artifacts = MapStore < Record < string , ArtifactState > > ;
21+ type ArtifactsByPageName = Map < string , ArtifactState > ;
22+ type ArtifactsByMessageId = Map < string , ArtifactsByPageName > ;
23+ type Artifacts = MapStore < ArtifactsByMessageId > ;
2324
2425export class ChatStore {
2526 private globalExecutionQueue = Promise . resolve ( ) ;
@@ -31,8 +32,8 @@ export class ChatStore {
3132 currentDescription : WritableAtom < string | undefined > =
3233 import . meta. hot ?. data ?. currentDescription ?? atom < string | undefined > ( undefined ) ;
3334
34- artifacts : Artifacts = import . meta. hot ?. data ?. artifacts ?? map ( { } ) ;
35- artifactIdList : string [ ] = [ ] ;
35+ artifacts : Artifacts = import . meta. hot ?. data ?. artifacts ?? map ( new Map ( ) ) ;
36+ artifactIdList : { messageId : string ; pageName : string } [ ] = [ ] ;
3637 actionAlert : WritableAtom < ActionAlert | undefined > =
3738 import . meta. hot ?. data ?. actionAlert ?? atom < ActionAlert | undefined > ( undefined ) ;
3839
@@ -60,7 +61,12 @@ export class ChatStore {
6061 }
6162
6263 get firstArtifact ( ) : ArtifactState | undefined {
63- return this . getArtifact ( this . artifactIdList [ 0 ] ) ;
64+ if ( this . artifactIdList . length === 0 ) {
65+ return undefined ;
66+ }
67+
68+ const { messageId, pageName } = this . artifactIdList [ 0 ] ;
69+ return this . getArtifact ( messageId , pageName ) ;
6470 }
6571
6672 get description ( ) {
@@ -76,31 +82,30 @@ export class ChatStore {
7682 }
7783
7884 abortAllActions ( ) {
79- // TODO: what do we wanna do and how do we wanna recover from this?
8085 const artifacts = this . artifacts . get ( ) ;
8186
82- Object . values ( artifacts ) . forEach ( ( artifact ) => {
83- const actions = artifact . runner . actions . get ( ) ;
84-
85- Object . values ( actions ) . forEach ( ( action ) => {
86- if ( action . status === 'running' || action . status === 'pending' ) {
87- action . abort ( ) ;
88- }
87+ artifacts . values ( ) . forEach ( ( artifactByPageNames ) => {
88+ artifactByPageNames . values ( ) . forEach ( ( artifact ) => {
89+ const actions = artifact . runner . actions . get ( ) ;
90+ Object . values ( actions ) . forEach ( ( action ) => {
91+ if ( action . status === 'running' || action . status === 'pending' ) {
92+ action . abort ( ) ;
93+ }
94+ } ) ;
8995 } ) ;
9096 } ) ;
9197 }
9298
9399 addArtifact ( { messageId, name, title, id } : ArtifactCallbackData ) {
94- const artifact = this . getArtifact ( messageId ) ;
100+ const artifact = this . getArtifact ( messageId , name ) ;
95101 if ( artifact ) {
96102 return ;
97103 }
98104
99- if ( ! this . artifactIdList . includes ( messageId ) ) {
100- this . artifactIdList . push ( messageId ) ;
105+ if ( ! this . artifactIdList . includes ( { messageId, pageName : name } ) ) {
106+ this . artifactIdList . push ( { messageId, pageName : name } ) ;
101107 }
102-
103- this . artifacts . setKey ( messageId , {
108+ const newArtifact = {
104109 id,
105110 name,
106111 title,
@@ -112,21 +117,56 @@ export class ChatStore {
112117
113118 this . actionAlert . set ( alert ) ;
114119 } ) ,
115- } ) ;
120+ } ;
121+
122+ const artifactsByMessageId = this . artifacts . get ( ) ;
123+ let artifactsByPageName = artifactsByMessageId . get ( messageId ) ;
124+ if ( ! artifactsByPageName ) {
125+ artifactsByPageName = new Map ( ) ;
126+ artifactsByMessageId . set ( messageId , artifactsByPageName ) ;
127+ }
128+
129+ artifactsByPageName . set ( name , newArtifact ) ;
130+
131+ this . artifacts . set ( artifactsByMessageId ) ;
116132 }
117133
118- updateArtifact ( { messageId } : ArtifactCallbackData , state : Partial < ArtifactUpdateState > ) {
119- const artifact = this . getArtifact ( messageId ) ;
134+ updateArtifact ( { messageId, name } : ArtifactCallbackData , state : Partial < ArtifactUpdateState > ) {
135+ const artifact = this . getArtifact ( messageId , name ) ;
120136 if ( ! artifact ) {
121137 return ;
122138 }
123139
124- this . artifacts . setKey ( messageId , { ...artifact , ...state } ) ;
140+ const artifactsByMessageId = this . artifacts . get ( ) ;
141+ const artifactsByPageName = artifactsByMessageId . get ( messageId ) ;
142+ if ( ! artifactsByPageName ) {
143+ return ;
144+ }
145+ artifactsByPageName . set ( name , { ...artifact , ...state } ) ;
146+ artifactsByMessageId . set ( messageId , artifactsByPageName ) ;
147+
148+ this . artifacts . set ( artifactsByMessageId ) ;
125149 }
126150
127- private getArtifact ( id : string ) {
151+ private getArtifact ( messageId : string , pageName : string ) {
128152 const artifacts = this . artifacts . get ( ) ;
129- return artifacts [ id ] ;
153+ const artifactsByPageName = artifacts . get ( messageId ) ;
154+ if ( ! artifactsByPageName ) {
155+ return undefined ;
156+ }
157+
158+ return artifactsByPageName . get ( pageName ) ;
159+ }
160+
161+ private getArtifactByArtifactId ( messageId : string , artifactId : string ) {
162+ const artifacts = this . artifacts . get ( ) ;
163+
164+ const artifactsByPageName = artifacts . get ( messageId ) ;
165+ if ( ! artifactsByPageName ) {
166+ return undefined ;
167+ }
168+
169+ return artifactsByPageName . values ( ) . find ( ( artifact ) => artifact . id === artifactId ) ;
130170 }
131171
132172 setReloadedMessages ( messages : string [ ] ) {
@@ -138,8 +178,8 @@ export class ChatStore {
138178 }
139179
140180 private async _addAction ( data : ActionCallbackData ) {
141- const { messageId } = data ;
142- const artifact = this . getArtifact ( messageId ) ;
181+ const { messageId, artifactId } = data ;
182+ const artifact = this . getArtifactByArtifactId ( messageId , artifactId ) ;
143183
144184 if ( ! artifact ) {
145185 unreachable ( 'Artifact not found' ) ;
@@ -157,9 +197,9 @@ export class ChatStore {
157197 }
158198
159199 async _runAction ( data : ActionCallbackData , isRunning : boolean = false ) {
160- const { messageId } = data ;
200+ const { messageId, artifactId } = data ;
161201
162- const artifact = this . getArtifact ( messageId ) ;
202+ const artifact = this . getArtifactByArtifactId ( messageId , artifactId ) ;
163203 if ( ! artifact ) {
164204 unreachable ( 'Artifact not found' ) ;
165205 }
0 commit comments