mirror of
https://github.com/open-webui/computer.git
synced 2026-11-18 11:51:55 +03:00
refac
This commit is contained in:
@@ -177,6 +177,8 @@
|
||||
return _t('chat.tool.checkTask', { id: args.task_id || '?' });
|
||||
case 'kill_task':
|
||||
return _t('chat.tool.killTask', { id: args.task_id || '?' });
|
||||
case 'image_generate':
|
||||
return args.image || args.images?.length ? 'Edit image' : 'Generate image';
|
||||
case 'web_search':
|
||||
return _t('chat.tool.webSearch', { query: args.query || '?' });
|
||||
case 'read_url': {
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
const isRejected = $derived(item.status === 'rejected');
|
||||
const isPending = $derived(item.status === 'pending');
|
||||
const imageToolOutput = $derived.by(() => {
|
||||
if (!['generate_image', 'edit_image'].includes(toolName) || !pairedOutput?.output) {
|
||||
if (toolName !== 'image_generate' || !pairedOutput?.output) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
|
||||
@@ -674,7 +674,7 @@ def _parse_image_data_uri(result: str) -> tuple[str, str] | None:
|
||||
|
||||
def _tool_result_for_model(tool_name: str, result: str) -> str:
|
||||
"""Return the tool result text to send back to the model."""
|
||||
if tool_name not in {"generate_image", "edit_image"}:
|
||||
if tool_name != "image_generate":
|
||||
return result
|
||||
|
||||
try:
|
||||
@@ -966,7 +966,7 @@ def build_artifact_item(tool_name: str, arguments: dict, result: str) -> dict |
|
||||
|
||||
def build_image_item(tool_name: str, result: str) -> dict | None:
|
||||
"""Build a renderable image output item for image-generation tools."""
|
||||
if tool_name not in {"generate_image", "edit_image"}:
|
||||
if tool_name != "image_generate":
|
||||
return None
|
||||
|
||||
try:
|
||||
|
||||
+49
-54
@@ -1439,67 +1439,60 @@ async def browser_evaluate(javascript: str, *, __context__: dict) -> str:
|
||||
return await client.evaluate(javascript)
|
||||
|
||||
|
||||
async def generate_image(
|
||||
async def image_generate(
|
||||
prompt: str,
|
||||
size: Optional[str] = None,
|
||||
n: int = 1,
|
||||
*,
|
||||
__context__: dict,
|
||||
) -> str:
|
||||
"""Generate image files from a text prompt.
|
||||
:param prompt: Detailed description of the image to generate.
|
||||
:param size: Optional image size, such as 1024x1024.
|
||||
:param n: Number of images to generate, from 1 to 4.
|
||||
"""
|
||||
from cptr.utils.images import generate_images
|
||||
|
||||
images = await generate_images(
|
||||
prompt,
|
||||
user_id=__context__.get("user_id"),
|
||||
size=size,
|
||||
n=n,
|
||||
workspace=__context__.get("workspace"),
|
||||
)
|
||||
return json.dumps(
|
||||
{
|
||||
"status": "success",
|
||||
"images": [image.as_dict() for image in images],
|
||||
},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
|
||||
|
||||
async def edit_image(
|
||||
prompt: str,
|
||||
image_ids: list[str],
|
||||
image: Optional[str] = None,
|
||||
images: Optional[list[str]] = None,
|
||||
size: Optional[str] = None,
|
||||
n: int = 1,
|
||||
background: Optional[str] = None,
|
||||
*,
|
||||
__context__: dict,
|
||||
) -> str:
|
||||
"""Edit existing cptr image files from a text prompt.
|
||||
:param prompt: Description of the requested image changes.
|
||||
:param image_ids: List of cptr file IDs or /api/files/... URLs for the source images.
|
||||
"""Generate or edit image files from a prompt.
|
||||
:param prompt: Detailed description of the image to create or the edits to make.
|
||||
:param image: Optional source image file id, /api/files/... URL, or workspace path for edit mode.
|
||||
:param images: Optional source image file ids, /api/files/... URLs, or workspace paths for edit mode.
|
||||
:param size: Optional image size, such as 1024x1024.
|
||||
:param n: Number of images to create, from 1 to 4.
|
||||
:param background: Optional background setting supported by the image provider.
|
||||
"""
|
||||
from cptr.utils.images import edit_images
|
||||
image_refs: list[str] = []
|
||||
if image:
|
||||
image_refs.append(image)
|
||||
if images:
|
||||
image_refs.extend(images)
|
||||
|
||||
if image_refs:
|
||||
from cptr.utils.images import edit_images
|
||||
|
||||
results = await edit_images(
|
||||
prompt,
|
||||
image_refs,
|
||||
user_id=__context__.get("user_id"),
|
||||
size=size,
|
||||
n=n,
|
||||
background=background,
|
||||
workspace=__context__.get("workspace"),
|
||||
)
|
||||
kind = "edit"
|
||||
else:
|
||||
from cptr.utils.images import generate_images
|
||||
|
||||
results = await generate_images(
|
||||
prompt,
|
||||
user_id=__context__.get("user_id"),
|
||||
size=size,
|
||||
n=n,
|
||||
workspace=__context__.get("workspace"),
|
||||
)
|
||||
kind = "generation"
|
||||
|
||||
images = await edit_images(
|
||||
prompt,
|
||||
image_ids,
|
||||
user_id=__context__.get("user_id"),
|
||||
size=size,
|
||||
n=n,
|
||||
background=background,
|
||||
workspace=__context__.get("workspace"),
|
||||
)
|
||||
return json.dumps(
|
||||
{
|
||||
"status": "success",
|
||||
"images": [image.as_dict() for image in images],
|
||||
"kind": kind,
|
||||
"images": [result.as_dict() for result in results],
|
||||
},
|
||||
ensure_ascii=False,
|
||||
)
|
||||
@@ -1529,8 +1522,7 @@ TOOLS: dict[str, dict] = {
|
||||
"update_automation": {"fn": update_automation, "auto": False},
|
||||
"toggle_automation": {"fn": toggle_automation, "auto": False},
|
||||
"delete_automation": {"fn": delete_automation, "auto": False},
|
||||
"generate_image": {"fn": generate_image, "auto": False},
|
||||
"edit_image": {"fn": edit_image, "auto": False},
|
||||
"image_generate": {"fn": image_generate, "auto": False},
|
||||
}
|
||||
|
||||
# Browser tools — conditionally included in schemas based on browser.enabled
|
||||
@@ -2128,13 +2120,16 @@ async def get_tool_list() -> list[dict]:
|
||||
"true",
|
||||
"1",
|
||||
)
|
||||
if (await Config.get("images.generation_enabled")) not in (True, "true", "1"):
|
||||
tools.pop("generate_image", None)
|
||||
if (await Config.get("images.edit_enabled")) not in (True, "true", "1"):
|
||||
tools.pop("edit_image", None)
|
||||
images_generation_enabled = (await Config.get("images.generation_enabled")) in (
|
||||
True,
|
||||
"true",
|
||||
"1",
|
||||
)
|
||||
images_edit_enabled = (await Config.get("images.edit_enabled")) in (True, "true", "1")
|
||||
if not (images_generation_enabled or images_edit_enabled):
|
||||
tools.pop("image_generate", None)
|
||||
except Exception:
|
||||
tools.pop("generate_image", None)
|
||||
tools.pop("edit_image", None)
|
||||
tools.pop("image_generate", None)
|
||||
pass
|
||||
|
||||
schemas = [_fn_to_schema(name, t["fn"]) for name, t in tools.items()]
|
||||
|
||||
Reference in New Issue
Block a user