@@ -173,7 +173,6 @@ fn parse_format_is_png(s: &str) -> Result<bool, String> {
173173///
174174/// **Invariants preserved (identical for both callers):**
175175/// - JSON/PNG format selection from the parsed `is_png` flag (magic-byte sniffing is import-only)
176- /// - PNG + memory hard rejection
177176/// - Memory-source pubkey validation
178177/// - Secret exclusion (env_vars never enter the manifest via `build_snapshot`)
179178/// - Output filename derived from the agent display name
@@ -182,18 +181,10 @@ pub(crate) async fn materialize_snapshot_bytes(
182181 memory_source_pubkey : Option < String > ,
183182 memory_level : MemoryLevel ,
184183 is_png : bool ,
184+ avatar_png_data_url : Option < String > ,
185185 app : AppHandle ,
186186 state : State < ' _ , AppState > ,
187187) -> Result < SnapshotPayload , String > {
188- // Eagerly reject PNG + memory — avoid an unnecessary relay round-trip.
189- if is_png && memory_level != MemoryLevel :: None {
190- return Err (
191- "Cannot export memory to .agent.png — use JSON format for memory-bearing \
192- snapshots."
193- . to_string ( ) ,
194- ) ;
195- }
196-
197188 // ── Load definition record and memory-source instance under lock ─────────
198189 let ( record, memory_pubkey) = {
199190 let _store_guard = state
@@ -274,7 +265,9 @@ pub(crate) async fn materialize_snapshot_bytes(
274265 let slug = crate :: util:: slugify ( & display_name, "agent" , 50 ) ;
275266
276267 if is_png {
277- let png_bytes = encode_snapshot_png ( & snapshot, avatar_bytes. as_deref ( ) )
268+ let png_body_avatar_bytes =
269+ resolve_png_body_avatar_bytes ( avatar_png_data_url. as_deref ( ) , avatar_bytes) ;
270+ let png_bytes = encode_snapshot_png ( & snapshot, png_body_avatar_bytes. as_deref ( ) )
278271 . map_err ( |e| format ! ( "Failed to encode .agent.png: {e}" ) ) ?;
279272 validate_snapshot_encode_size ( png_bytes. len ( ) , true ) ?;
280273 Ok ( SnapshotPayload {
@@ -292,6 +285,17 @@ pub(crate) async fn materialize_snapshot_bytes(
292285 }
293286}
294287
288+ /// Choose bytes for the PNG image body without changing the source avatar the
289+ /// manifest preserves for import.
290+ fn resolve_png_body_avatar_bytes (
291+ avatar_png_data_url : Option < & str > ,
292+ store_avatar_bytes : Option < Vec < u8 > > ,
293+ ) -> Option < Vec < u8 > > {
294+ avatar_png_data_url
295+ . and_then ( crate :: managed_agents:: agent_snapshot:: decode_avatar_data_url)
296+ . or ( store_avatar_bytes)
297+ }
298+
295299/// Export an agent definition as a `buzz-agent-snapshot v1` file.
296300///
297301/// `id` is a definition slug or a keyed-instance pubkey.
@@ -309,6 +313,7 @@ pub async fn export_agent_snapshot(
309313 memory_source_pubkey : Option < String > ,
310314 memory_level : String ,
311315 format : String ,
316+ avatar_png_data_url : Option < String > ,
312317 app : AppHandle ,
313318 state : State < ' _ , AppState > ,
314319) -> Result < bool , String > {
@@ -320,6 +325,7 @@ pub async fn export_agent_snapshot(
320325 memory_source_pubkey,
321326 memory_level,
322327 is_png,
328+ avatar_png_data_url,
323329 app. clone ( ) ,
324330 state,
325331 )
@@ -370,15 +376,23 @@ pub async fn encode_agent_snapshot_for_send(
370376 memory_source_pubkey : Option < String > ,
371377 memory_level : String ,
372378 format : String ,
379+ avatar_png_data_url : Option < String > ,
373380 app : AppHandle ,
374381 state : State < ' _ , AppState > ,
375382) -> Result < EncodedSnapshotPayload , String > {
376383 let memory_level = parse_memory_level ( & memory_level) ?;
377384 let is_png = parse_format_is_png ( & format) ?;
378385
379- let payload =
380- materialize_snapshot_bytes ( id, memory_source_pubkey, memory_level, is_png, app, state)
381- . await ?;
386+ let payload = materialize_snapshot_bytes (
387+ id,
388+ memory_source_pubkey,
389+ memory_level,
390+ is_png,
391+ avatar_png_data_url,
392+ app,
393+ state,
394+ )
395+ . await ?;
382396
383397 Ok ( EncodedSnapshotPayload {
384398 file_bytes : payload. bytes ,
@@ -388,3 +402,66 @@ pub async fn encode_agent_snapshot_for_send(
388402
389403#[ cfg( test) ]
390404mod tests;
405+
406+ #[ cfg( test) ]
407+ mod png_body_tests {
408+ use super :: * ;
409+ use base64:: Engine as _;
410+ use png:: Decoder ;
411+
412+ #[ test]
413+ fn frontend_raster_becomes_png_body_without_replacing_manifest_source ( ) {
414+ let avatar = image:: DynamicImage :: ImageRgb8 ( image:: RgbImage :: from_pixel (
415+ 3 ,
416+ 2 ,
417+ image:: Rgb ( [ 0x12 , 0x34 , 0x56 ] ) ,
418+ ) ) ;
419+ let mut source_png = Vec :: new ( ) ;
420+ avatar
421+ . write_to (
422+ & mut std:: io:: Cursor :: new ( & mut source_png) ,
423+ image:: ImageFormat :: Png ,
424+ )
425+ . unwrap ( ) ;
426+ let avatar_data_url = format ! (
427+ "data:image/png;base64,{}" ,
428+ base64:: engine:: general_purpose:: STANDARD . encode( & source_png)
429+ ) ;
430+ let snapshot = crate :: managed_agents:: agent_snapshot:: AgentSnapshot {
431+ format : crate :: managed_agents:: agent_snapshot:: FORMAT_DISCRIMINATOR . to_string ( ) ,
432+ version : crate :: managed_agents:: agent_snapshot:: FORMAT_VERSION ,
433+ definition : crate :: managed_agents:: agent_snapshot:: AgentSnapshotDefinition {
434+ name : "Agent" . to_string ( ) ,
435+ system_prompt : None ,
436+ runtime : None ,
437+ model : None ,
438+ provider : None ,
439+ parallelism : None ,
440+ respond_to : None ,
441+ respond_to_allowlist : vec ! [ ] ,
442+ name_pool : vec ! [ ] ,
443+ idle_timeout_seconds : None ,
444+ max_turn_duration_seconds : None ,
445+ } ,
446+ profile : crate :: managed_agents:: agent_snapshot:: AgentSnapshotProfile {
447+ display_name : "Agent" . to_string ( ) ,
448+ about : None ,
449+ avatar_data_url : None ,
450+ avatar_url : Some ( "https://relay.example/media/avatar.png" . to_string ( ) ) ,
451+ } ,
452+ memory : crate :: managed_agents:: agent_snapshot:: AgentSnapshotMemory {
453+ level : MemoryLevel :: None ,
454+ entries : vec ! [ ] ,
455+ } ,
456+ } ;
457+ let body_bytes = resolve_png_body_avatar_bytes ( Some ( & avatar_data_url) , None ) ;
458+ let png_bytes = encode_snapshot_png ( & snapshot, body_bytes. as_deref ( ) ) . unwrap ( ) ;
459+ let reader = Decoder :: new ( std:: io:: Cursor :: new ( & png_bytes) )
460+ . read_info ( )
461+ . unwrap ( ) ;
462+ let decoded = import:: decode_snapshot_from_bytes ( & png_bytes) . unwrap ( ) ;
463+
464+ assert_eq ! ( ( reader. info( ) . width, reader. info( ) . height) , ( 3 , 2 ) ) ;
465+ assert_eq ! ( decoded. profile. avatar_url, snapshot. profile. avatar_url) ;
466+ }
467+ }
0 commit comments