This reverts commit 54fcef9887.
Please do not submit unrelated docs and codes in the same commit. I am reverting this commit so that the related files can be submitted in better-organized future commits.
46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
# Copyright 2026 Google LLC
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
from typing import TypeGuard
|
|
|
|
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, ToolMessage
|
|
|
|
|
|
def is_ai_message(message: BaseMessage) -> TypeGuard[AIMessage]:
|
|
return isinstance(message, AIMessage)
|
|
|
|
|
|
def is_human_message(message: BaseMessage) -> TypeGuard[HumanMessage]:
|
|
return isinstance(message, HumanMessage)
|
|
|
|
|
|
def is_tool_message(message: BaseMessage) -> TypeGuard[ToolMessage]:
|
|
return isinstance(message, ToolMessage)
|
|
|
|
|
|
def is_tool_for_name(tool_message: ToolMessage, name: str) -> bool:
|
|
return tool_message.name == name
|
|
|
|
|
|
def get_screenshot_message_for_llm(screenshot_base64: str):
|
|
prefix = "" if screenshot_base64.startswith("data:image") else "data:image/jpeg;base64,"
|
|
return HumanMessage(
|
|
content=[
|
|
{
|
|
"type": "image_url",
|
|
"image_url": {"url": f"{prefix}{screenshot_base64}"},
|
|
}
|
|
]
|
|
)
|