From 936a20a03ed7dfb05dbd22d24e87caa99446ded6 Mon Sep 17 00:00:00 2001 From: Chili Palmer Date: Sun, 21 Jun 2026 14:06:50 +0200 Subject: [PATCH] Deduplicate one-shot request flow --- AUDIT.md | 1 + lib/bds/ai/one_shot.ex | 22 +++++----------------- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/AUDIT.md b/AUDIT.md index c911afd..3231ddc 100644 --- a/AUDIT.md +++ b/AUDIT.md @@ -31,6 +31,7 @@ after each item. - [x] `ai/chat_tools.ex` - [x] `ai/chat.ex` - [x] `ai/openai_compatible_runtime.ex` +- [x] `ai/one_shot.ex` --- diff --git a/lib/bds/ai/one_shot.ex b/lib/bds/ai/one_shot.ex index a2ebbe2..d376ace 100644 --- a/lib/bds/ai/one_shot.ex +++ b/lib/bds/ai/one_shot.ex @@ -167,28 +167,13 @@ defmodule BDS.AI.OneShot do end end - defp run_one_shot(:analyze_image = operation, payload, opts, formatter) do - runtime = Keyword.get(opts, :runtime, OpenAICompatibleRuntime) - - with {:ok, endpoint, model, mode} <- Runtime.resolve_target(operation, opts), - :ok <- Runtime.validate_target(operation, model, mode), - {:ok, payload} <- resolve_image_data_url(payload), - request <- build_one_shot_request(operation, payload, model, opts), - {:ok, response} <- - runtime.generate(Runtime.endpoint_with_model(endpoint, model), request, opts), - {:ok, json} <- extract_json_response(response), - usage <- Chat.normalize_usage(response.usage), - {:ok, result} <- formatter.(json, usage) do - {:ok, result} - end - end - defp run_one_shot(operation, payload, opts, formatter) do runtime = Keyword.get(opts, :runtime, OpenAICompatibleRuntime) with {:ok, endpoint, model, mode} <- Runtime.resolve_target(operation, opts), :ok <- Runtime.validate_target(operation, model, mode), - request <- build_one_shot_request(operation, payload, model, opts), + {:ok, prepared_payload} <- prepare_one_shot_payload(operation, payload), + request <- build_one_shot_request(operation, prepared_payload, model, opts), {:ok, response} <- runtime.generate(Runtime.endpoint_with_model(endpoint, model), request, opts), {:ok, json} <- extract_json_response(response), @@ -198,6 +183,9 @@ defmodule BDS.AI.OneShot do end end + defp prepare_one_shot_payload(:analyze_image, payload), do: resolve_image_data_url(payload) + defp prepare_one_shot_payload(_operation, payload), do: {:ok, payload} + defp build_one_shot_request(operation, payload, model, opts) do language = Keyword.get(opts, :language)