@@ -94,18 +94,25 @@ impl PageService for PageServiceImpl {
94
94
. list_thread_messages ( & input. thread_id , None , None , None , None )
95
95
. await ?;
96
96
97
+ // use the first message source id for page
98
+ let code_source_id = thread_messages
99
+ . iter ( )
100
+ . find_map ( |m| m. code_source_id . as_deref ( ) )
101
+ . map ( ToOwned :: to_owned) ;
102
+
97
103
let debug_option = input
98
104
. debug_option
99
105
. as_ref ( )
100
106
. map ( |d| PageRunDebugOptionInput {
101
107
return_chat_completion_request : d. return_chat_completion_request ,
102
108
return_query_request : d. return_query_request ,
103
109
} ) ;
110
+
104
111
self . page_run (
105
112
policy,
106
113
author_id,
107
114
None ,
108
- None ,
115
+ code_source_id . as_deref ( ) ,
109
116
None ,
110
117
Some ( & thread_messages) ,
111
118
debug_option. as_ref ( ) ,
@@ -119,11 +126,17 @@ impl PageService for PageServiceImpl {
119
126
author_id : & ID ,
120
127
input : & CreatePageRunInput ,
121
128
) -> Result < PageRunStream > {
129
+ let code_source_id = if let Some ( code_query) = input. code_query . as_ref ( ) {
130
+ get_source_id ( self . context . clone ( ) , policy, code_query) . await
131
+ } else {
132
+ None
133
+ } ;
134
+
122
135
self . page_run (
123
136
policy,
124
137
author_id,
125
138
Some ( & input. title_prompt ) ,
126
- input . code_query . as_ref ( ) ,
139
+ code_source_id . as_deref ( ) ,
127
140
input. doc_query . as_ref ( ) ,
128
141
None ,
129
142
input. debug_option . as_ref ( ) ,
@@ -215,7 +228,7 @@ impl PageService for PageServiceImpl {
215
228
Some ( new_section_prompt) ,
216
229
& existing_page_sections,
217
230
& page_title,
218
- code_source_id,
231
+ code_source_id. as_deref ( ) ,
219
232
doc_query. as_ref( ) ,
220
233
None ,
221
234
db,
@@ -356,26 +369,14 @@ impl PageServiceImpl {
356
369
policy : & AccessPolicy ,
357
370
author_id : & ID ,
358
371
title_prompt : Option < & str > ,
359
- code_query : Option < & CodeQueryInput > ,
372
+ code_source_id : Option < & str > ,
360
373
doc_query : Option < & DocQueryInput > ,
361
374
thread_messages : Option < & [ Message ] > ,
362
375
debug_option : Option < & PageRunDebugOptionInput > ,
363
376
) -> Result < PageRunStream > {
364
- let code_source_id = if let Some ( code_query) = code_query {
365
- get_source_id ( self . context . clone ( ) , policy, code_query) . await
366
- } else {
367
- // use the first message source id for page
368
- thread_messages. and_then ( |messages| {
369
- messages
370
- . iter ( )
371
- . find_map ( |m| m. code_source_id . as_deref ( ) )
372
- . map ( ToOwned :: to_owned)
373
- } )
374
- } ;
375
-
376
377
let page_id = self
377
378
. db
378
- . create_page ( author_id. as_rowid ( ) ?, code_source_id. clone ( ) )
379
+ . create_page ( author_id. as_rowid ( ) ?, code_source_id)
379
380
. await ?
380
381
. as_id ( ) ;
381
382
@@ -397,6 +398,17 @@ impl PageServiceImpl {
397
398
)
398
399
. await ?;
399
400
401
+ let code_source_id = code_source_id. map ( ToOwned :: to_owned) ;
402
+ let doc_query = if let Some ( doc_query) = doc_query {
403
+ Some ( doc_query. to_owned ( ) )
404
+ } else {
405
+ code_source_id. as_ref ( ) . map ( |code_source_id| DocQueryInput {
406
+ content : title_prompt. unwrap_or_else ( || & page_title) . to_owned ( ) ,
407
+ search_public : false ,
408
+ source_ids : Some ( vec ! [ code_source_id. clone( ) ] ) ,
409
+ } )
410
+ } ;
411
+
400
412
let db = self . db . clone ( ) ;
401
413
let policy = policy. clone ( ) ;
402
414
let chat = self . chat . clone ( ) ;
@@ -405,7 +417,6 @@ impl PageServiceImpl {
405
417
let retrieval = self . retrieval . clone ( ) ;
406
418
let config = self . config . clone ( ) ;
407
419
let auth = self . auth . clone ( ) ;
408
- let doc_query = doc_query. map ( ToOwned :: to_owned) ;
409
420
let page_rowid = page_id. as_rowid ( ) ?;
410
421
let debug_option = debug_option. map ( |x| PageSectionRunDebugOptionInput {
411
422
return_chat_completion_request : x. return_chat_completion_request ,
@@ -496,7 +507,7 @@ impl PageServiceImpl {
496
507
None ,
497
508
& existed_sections,
498
509
& page_title,
499
- code_source_id. clone ( ) ,
510
+ code_source_id. as_deref ( ) ,
500
511
doc_query. as_ref( ) ,
501
512
thread_messages. as_deref( ) ,
502
513
db. clone( ) ,
@@ -719,7 +730,7 @@ async fn generate_section_with_attachments(
719
730
section_prompt : Option < String > ,
720
731
existing_sections : & [ PageSection ] ,
721
732
page_title : & str ,
722
- code_source_id : Option < String > ,
733
+ code_source_id : Option < & str > ,
723
734
doc_query : Option < & DocQueryInput > ,
724
735
thread_messages : Option < & [ Message ] > ,
725
736
db : DbConn ,
@@ -735,6 +746,7 @@ async fn generate_section_with_attachments(
735
746
let page_title = page_title. to_string ( ) ;
736
747
let thread_messages = thread_messages. map ( ToOwned :: to_owned) ;
737
748
let context_info_helper = context. read ( Some ( & policy) ) . await ?. helper ( ) ;
749
+ let code_source_id = code_source_id. map ( ToOwned :: to_owned) ;
738
750
let doc_query = doc_query. map ( ToOwned :: to_owned) ;
739
751
let debug_chat_request = debug
740
752
. map ( |d| d. return_chat_completion_request )
@@ -781,31 +793,29 @@ async fn generate_section_with_attachments(
781
793
& config. code_search_params,
782
794
None ,
783
795
) . await ?;
796
+ let debug_data = if debug_query_request {
797
+ Some ( AttachmentCodeQueryDebugData {
798
+ source_id: source_id. clone( ) ,
799
+ query: query_content,
800
+ } )
801
+ } else {
802
+ None
803
+ } ;
784
804
785
805
attachment. code = hits. iter( ) . map( |x| x. doc. clone( ) . into( ) ) . collect:: <Vec <_>>( ) ;
786
806
807
+ let hits = hits. into_iter( ) . map( |x| x. into( ) ) . collect:: <Vec <_>>( ) ;
787
808
if !hits. is_empty( ) {
788
- let hits = hits. into_iter( ) . map( |x| x. into( ) ) . collect:: <Vec <_>>( ) ;
789
809
db. update_page_section_code_attachments( section_id, & attachment. code. iter( ) . map( |c| c. into( ) ) . collect:: <Vec <_>>( ) ) . await ?;
790
-
791
- let debug_data = if debug_query_request {
792
- Some ( AttachmentCodeQueryDebugData {
793
- source_id: source_id. clone( ) ,
794
- query: query_content,
795
- } )
796
- } else {
797
- None
798
- } ;
799
-
800
- yield Ok ( SectionRunItem :: PageSectionAttachmentCode (
801
- PageSectionAttachmentCode {
802
- id: section_id. as_id( ) ,
803
- codes: hits,
804
-
805
- debug_data,
806
- }
807
- ) ) ;
808
810
}
811
+ yield Ok ( SectionRunItem :: PageSectionAttachmentCode (
812
+ PageSectionAttachmentCode {
813
+ id: section_id. as_id( ) ,
814
+ codes: hits,
815
+
816
+ debug_data,
817
+ }
818
+ ) ) ;
809
819
}
810
820
811
821
if let Some ( doc_query) = doc_query {
@@ -820,39 +830,39 @@ async fn generate_section_with_attachments(
820
830
true
821
831
}
822
832
} ) . collect:: <Vec <_>>( ) ;
823
-
824
- if !hits. is_empty( ) {
825
- let hits = futures:: future:: join_all( hits. into_iter( ) . map( |x| {
826
- let score = x. score;
827
- let doc = x. doc. clone( ) ;
828
- let auth = auth. clone( ) ;
829
- async move {
830
- AttachmentDocHit {
831
- score: score as f64 ,
832
- doc: attachment_doc_from_search( auth, doc) . await ,
833
- }
833
+ let debug_data = if debug_query_request {
834
+ Some ( AttachmentDocQueryDebugData {
835
+ source_ids: doc_query. source_ids. clone( ) . unwrap_or_default( ) ,
836
+ query: doc_query. content. clone( ) ,
837
+ } )
838
+ } else {
839
+ None
840
+ } ;
841
+
842
+ let hits = futures:: future:: join_all( hits. into_iter( ) . map( |x| {
843
+ let score = x. score;
844
+ let doc = x. doc. clone( ) ;
845
+ let auth = auth. clone( ) ;
846
+ async move {
847
+ AttachmentDocHit {
848
+ score: score as f64 ,
849
+ doc: attachment_doc_from_search( auth, doc) . await ,
834
850
}
835
- } ) ) . await ;
851
+ }
852
+ } ) ) . await ;
853
+ if !hits. is_empty( ) {
836
854
attachment. doc = hits. clone( ) . into_iter( ) . map( |x| x. doc) . collect:: <Vec <_>>( ) ;
837
855
db. update_page_section_doc_attachments( section_id, & attachment. doc. iter( ) . map( |c| c. into( ) ) . collect:: <Vec <_>>( ) ) . await ?;
838
-
839
- let debug_data = if debug_query_request {
840
- Some ( AttachmentDocQueryDebugData {
841
- source_ids: doc_query. source_ids. clone( ) . unwrap_or_default( ) ,
842
- query: doc_query. content. clone( ) ,
843
- } )
844
- } else {
845
- None
846
- } ;
847
- yield Ok ( SectionRunItem :: PageSectionAttachmentDoc (
848
- PageSectionAttachmentDoc {
849
- id: section_id. as_id( ) ,
850
- doc: hits,
851
-
852
- debug_data,
853
- }
854
- ) ) ;
855
856
}
857
+
858
+ yield Ok ( SectionRunItem :: PageSectionAttachmentDoc (
859
+ PageSectionAttachmentDoc {
860
+ id: section_id. as_id( ) ,
861
+ doc: hits,
862
+
863
+ debug_data,
864
+ }
865
+ ) ) ;
856
866
}
857
867
858
868
// Generate section content
0 commit comments