@@ -79,6 +79,31 @@ pub fn extract_archive_bytes(data: &[u8], dest: &Path) -> Result<()> {
7979 Ok ( ( ) )
8080}
8181
82+ /// Extract only the agent code from an archive (no tasks/ required).
83+ pub async fn extract_agent_only ( data : & [ u8 ] , dest : & Path ) -> Result < ( String , String ) > {
84+ if data. len ( ) > MAX_ARCHIVE_SIZE {
85+ anyhow:: bail!(
86+ "Archive too large: {} bytes (max {})" ,
87+ data. len( ) ,
88+ MAX_ARCHIVE_SIZE
89+ ) ;
90+ }
91+
92+ tokio:: fs:: create_dir_all ( dest)
93+ . await
94+ . context ( "Failed to create extraction directory" ) ?;
95+ let dest_owned = dest. to_path_buf ( ) ;
96+ let data_vec = data. to_vec ( ) ;
97+ tokio:: task:: spawn_blocking ( move || extract_archive_bytes ( & data_vec, & dest_owned) )
98+ . await
99+ . context ( "Extract task panicked" ) ??;
100+
101+ let root = find_agent_root ( dest) ?;
102+ let agent_code = load_agent_code ( & root) ?;
103+ let agent_language = detect_agent_language ( & root) ;
104+ Ok ( ( agent_code, agent_language) )
105+ }
106+
82107pub async fn extract_uploaded_archive ( data : & [ u8 ] , dest : & Path ) -> Result < ExtractedArchive > {
83108 if data. len ( ) > MAX_ARCHIVE_SIZE {
84109 anyhow:: bail!(
@@ -119,6 +144,20 @@ pub async fn extract_uploaded_archive(data: &[u8], dest: &Path) -> Result<Extrac
119144 } )
120145}
121146
147+ fn find_agent_root ( base : & Path ) -> Result < PathBuf > {
148+ if base. join ( "agent_code" ) . exists ( ) {
149+ return Ok ( base. to_path_buf ( ) ) ;
150+ }
151+ for entry in std:: fs:: read_dir ( base) . context ( "Failed to read extracted directory" ) ? {
152+ let entry = entry?;
153+ let path = entry. path ( ) ;
154+ if path. is_dir ( ) && path. join ( "agent_code" ) . exists ( ) {
155+ return Ok ( path) ;
156+ }
157+ }
158+ anyhow:: bail!( "No agent_code/ found in archive at {}" , base. display( ) )
159+ }
160+
122161fn find_archive_root ( base : & Path ) -> Result < PathBuf > {
123162 if base. join ( "tasks" ) . exists ( ) || base. join ( "agent_code" ) . exists ( ) {
124163 return Ok ( base. to_path_buf ( ) ) ;
0 commit comments