MCPcopy Create free account
hub / github.com/TheSmallHanCat/flow2api / _append_openai_reference_images

Function _append_openai_reference_images

src/api/routes.py:325–360  ·  view source on GitHub ↗
(
    model: str,
    messages: List[ChatMessage],
    images: List[bytes],
)

Source from the content-addressed store, hash-verified

323
324
325async def _append_openai_reference_images(
326 model: str,
327 messages: List[ChatMessage],
328 images: List[bytes],
329) -> List[bytes]:
330 model_config = MODEL_CONFIG.get(model)
331 if not model_config or model_config["type"] != "image" or len(messages) <= 1:
332 return images
333
334 debug_logger.log_info(f"[CONTEXT] 开始查找历史参考图,消息数量: {len(messages)}")
335
336 for msg in reversed(messages[:-1]):
337 if msg.role == "assistant" and isinstance(msg.content, str):
338 matches = MARKDOWN_IMAGE_RE.findall(msg.content)
339 if not matches:
340 continue
341
342 for image_url in reversed(matches):
343 if not image_url.startswith("http") and "/tmp/" not in image_url:
344 continue
345 try:
346 downloaded_bytes = await retrieve_image_data(image_url)
347 if downloaded_bytes:
348 images.insert(0, downloaded_bytes)
349 debug_logger.log_info(
350 f"[CONTEXT] ✅ 添加历史参考图: {image_url}"
351 )
352 return images
353 debug_logger.log_warning(
354 f"[CONTEXT] 图片下载失败或为空,尝试下一个: {image_url}"
355 )
356 except Exception as exc:
357 debug_logger.log_error(
358 f"[CONTEXT] 处理参考图时出错: {str(exc)}"
359 )
360 return images
361
362
363async def _extract_prompt_and_images_from_gemini_contents(

Callers 1

Calls 4

retrieve_image_dataFunction · 0.85
log_infoMethod · 0.80
log_warningMethod · 0.80
log_errorMethod · 0.80

Tested by

no test coverage detected