Lee-W commented on code in PR #76000:
URL: https://github.com/apache/airflow/pull/76000#discussion_r4627710187


##########
providers/common/ai/src/airflow/providers/common/ai/tools/openai_codex.py:
##########
@@ -0,0 +1,619 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+"""
+Give Airflow tools to an agent loop run by the OpenAI Codex SDK.
+
+.. note:: Experimental; see :ref:`howto/stability`.
+"""
+
+from __future__ import annotations
+
+try:
+    import uvicorn
+    from mcp import types as mcp_types
+    from mcp.server.lowlevel.server import Server
+    from openai_codex import ApprovalMode, CodexConfig, Sandbox, TurnResult
+    from openai_codex.generated.v2_all import (
+        AgentMessageThreadItem,
+        ItemCompletedNotification,
+        MessagePhase,
+        ThreadTokenUsageUpdatedNotification,
+        TurnCompletedNotification,
+        TurnStatus,
+    )
+except ImportError as e:
+    from airflow.providers.common.compat.sdk import 
AirflowOptionalProviderFeatureException
+
+    raise AirflowOptionalProviderFeatureException(e)
+
+import asyncio
+import contextlib
+import copy
+import hmac
+import json
+import logging
+import os
+import re
+import secrets
+import shutil
+import socket
+import tempfile
+import time
+from dataclasses import dataclass
+from typing import TYPE_CHECKING, Any
+
+from airflow.providers.common.ai.tools import AirflowTool, ToolCallError, 
_collect_tools
+from airflow.providers.common.ai.tools._from_toolset import tool_call_scope
+from airflow.providers.common.ai.utils.tool_definition import serialize_for_llm
+from airflow.providers.common.ai.utils.tool_metrics import calling_framework
+
+if TYPE_CHECKING:
+    from collections.abc import AsyncIterable, AsyncIterator, Awaitable, 
Callable, Iterator, MutableMapping
+
+    from mcp.server.context import ServerRequestContext
+    from openai_codex import AsyncTurnHandle
+    from openai_codex.generated.v2_all import ThreadItem, ThreadTokenUsage
+
+    from airflow.providers.common.ai.tools import ToolSource
+
+log = logging.getLogger(__name__)
+
+__all__ = ["AirflowTools"]
+
+_MCP_HINT = "Add the server to Codex's mcp_servers config instead."
+
+
+_MAX_ERROR_DETAIL_CHARS = 500
+# Every constant below was observed with openai-codex 0.163.0, not a 
documented guarantee.
+#
+# The ceiling on a tool result this adapter lets through. It is our own 
choice: unlike the Claude
+# CLI, Codex has no fixed clamp that we know of. A result over it is cut here, 
with a notice the
+# model can act on, instead of by Codex, whose truncation of an MCP result is 
unverified.
+_MAX_INLINE_RESULT_CHARS = 100_000
+# Set to the character ceiling so that Codex is unlikely to cut a result before
+# ``_truncate_result_text`` does. A token can be shorter than a character for 
some text (CJK,
+# emoji), so this is not a guarantee.
+_TOOL_OUTPUT_TOKEN_LIMIT = _MAX_INLINE_RESULT_CHARS
+# Codex's default budget is 5_000 bytes of input schema per MCP server; over 
it, Codex compresses
+# the schema and drops parameter descriptions. The largest schema of the 
toolsets this provider
+# ships is ~1 KB, so this leaves room; ``_build_config_overrides`` raises it 
for a larger one.
+_MIN_TOOL_INPUT_SCHEMA_BYTES = 65_536
+# Codex stops waiting for a tool after this long (its default is 60 s, too 
short for a SQL query).
+# What happens to the hook when Codex gives up is not verified.
+_DEFAULT_TOOL_TIMEOUT_SEC = 600.0
+# How long ``serve()`` waits for the HTTP server to start, and for open 
connections to drain on exit.
+_SERVER_START_TIMEOUT_SEC = 10.0
+# How long ``run()`` waits for Codex to confirm an interrupt before it raises 
the tool failure anyway.
+_INTERRUPT_TIMEOUT_SEC = 10.0
+_SERVER_SHUTDOWN_TIMEOUT_SEC = 5
+# The variable that carries the bearer token to the Codex child process. Only 
its name is in
+# ``codex --config`` (so in argv, visible to ``ps``); the value goes through 
the environment.
+_MCP_TOKEN_ENV_VAR = "AIRFLOW_CODEX_MCP_TOKEN"
+# ``server_name`` becomes an unquoted TOML key in 
``mcp_servers.<server_name>.*``.
+_SERVER_NAME_RE = re.compile(r"[A-Za-z0-9_-]+")
+
+
+def _truncate_result_text(text: str) -> str:
+    if len(text) <= _MAX_INLINE_RESULT_CHARS:
+        return text
+    notice = (
+        f"\n\n[Truncated: this result had {len(text)} characters and only the 
beginning is shown. "
+        "Narrow the request, for example by adding filters, selecting fewer 
columns or lowering "
+        "the LIMIT, to get a smaller result.]"
+    )
+    return text[: _MAX_INLINE_RESULT_CHARS - len(notice)] + notice
+
+
+def _bound_detail(text: str) -> str:

Review Comment:
   Agreed. Let me do it in a follow up so that this PR won't touch the claude 
part



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to