File size: 13,121 Bytes
199daad
 
13bbb15
199daad
13bbb15
 
 
199daad
 
d4523b3
199daad
 
 
 
c175ac0
 
199daad
d4523b3
199daad
edaeab6
 
 
 
 
 
 
 
199daad
d4523b3
199daad
13bbb15
d4523b3
 
 
 
13bbb15
 
d4523b3
 
 
 
 
13bbb15
199daad
 
13bbb15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4523b3
edaeab6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8312ded
 
 
 
 
 
edaeab6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8312ded
edaeab6
 
 
 
 
 
50d7f1c
 
 
 
 
 
8312ded
50d7f1c
 
 
 
 
 
 
edaeab6
 
 
c175ac0
13bbb15
 
edaeab6
13bbb15
 
 
 
 
edaeab6
c175ac0
13bbb15
d4523b3
13bbb15
 
edaeab6
13bbb15
edaeab6
13bbb15
 
 
edaeab6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c175ac0
edaeab6
 
 
 
 
 
 
c175ac0
 
 
edaeab6
 
 
 
 
 
 
 
 
c175ac0
 
edaeab6
 
 
 
c175ac0
 
 
 
 
 
edaeab6
 
 
 
 
 
 
 
 
 
 
 
c175ac0
edaeab6
 
 
13bbb15
 
edaeab6
c175ac0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
from __future__ import annotations

import json
import os
import urllib.error
import urllib.request
import uuid
from dataclasses import dataclass


@dataclass
class CodexResult:
    final_response: str
    finish_reason: str = "completed"
    tool_names: tuple[str, ...] = ()
    tool_events: tuple[str, ...] = ()


class CodexHarness:
    """OpenCode Go adapter with a restricted Marine function-call bridge.

    OpenCode Go is only the model endpoint.  It does not inherit the MCP
    server from the old Codex CLI process, so the adapter must expose the
    allowed Marine functions and feed their results back into the Responses
    loop.  Without this loop ordinary chat works but Ocean exports can only
    produce a prose answer (the failure seen in the Space UI).
    """

    def __init__(self, model=None, api_key=None, base_url=None,
                 session_root=None, system_prompt=None, **kwargs):
        self.model = model or os.environ.get("OPENCODE_GO_MODEL", "gpt-5.6-luna")
        self.api_key = (
            api_key
            or os.environ.get("OPENCODE_GO_API_KEY")
            or os.environ.get("OPENAI_API_KEY")
            or ""
        ).strip()
        self.base_url = (
            base_url
            or os.environ.get("OPENCODE_GO_BASE_URL")
            or os.environ.get("OPENAI_BASE_URL")
            or "https://opencode.ai/zen/go/v1"
        ).rstrip("/")
        self.system_prompt = system_prompt or ""

    @staticmethod
    def _text(payload: dict) -> str:
        text = str(payload.get("output_text") or "").strip()
        if text:
            return text
        parts = []
        for item in payload.get("output") or []:
            for content in item.get("content") or []:
                if content.get("type") in {"output_text", "text"}:
                    value = content.get("text")
                    if isinstance(value, str):
                        parts.append(value)
                    elif isinstance(value, dict) and isinstance(value.get("value"), str):
                        parts.append(value["value"])
        return "".join(parts).strip()

    @staticmethod
    def _tools() -> list[dict]:
        """OpenAI Responses-compatible schemas for the supported MCP tools."""
        def fn(name, description, properties=None, required=None):
            return {
                "type": "function",
                "name": name,
                "description": description,
                "parameters": {
                    "type": "object",
                    "properties": properties or {},
                    "required": required or [],
                    "additionalProperties": False,
                },
            }

        number = {"type": "number"}
        string = {"type": "string"}
        domain = {"type": "string", "enum": ["ocean", "tuna", "squid"]}
        tools = [
            fn("mcp_marine_marine_health", "Check the live school Marine API."),
            fn("mcp_marine_marine_domains", "Return live Ocean, Tuna and Squid data-center status."),
            fn("mcp_marine_marine_status", "Return detailed status for one data center.", {"domain": domain}),
            fn("mcp_marine_marine_catalog", "Return the live Ocean catalog."),
            fn("mcp_marine_marine_query", "Check whether an Ocean source/variable/date exists.", {
                "date": string, "variable": string, "source": string, "domain": domain,
            }, ["date", "variable", "source"]),
            fn("mcp_marine_marine_subset", "Create a NetCDF Ocean subset and return its download URL.", {
                "date": string, "lon_min": number, "lon_max": number,
                "lat_min": number, "lat_max": number, "variable": string,
                "source": string, "domain": domain, "depth": number,
            }, ["date", "lon_min", "lon_max", "lat_min", "lat_max", "variable", "source"]),
            fn("mcp_marine_marine_export", "Export Ocean data as netcdf, csv, xlsx, json, geotiff or png.", {
                "date": string, "lon_min": number, "lon_max": number,
                "lat_min": number, "lat_max": number, "variable": string,
                "source": string, "format": string, "domain": domain, "depth": number,
            }, ["date", "lon_min", "lon_max", "lat_min", "lat_max", "variable", "source", "format"]),
            fn("mcp_marine_marine_export_range", "Export an inclusive Ocean date range (maximum 31 days) and return one download URL per day.", {
                "start_date": string, "end_date": string, "lon_min": number,
                "lon_max": number, "lat_min": number, "lat_max": number,
                "variable": string, "source": string, "format": string,
                "domain": domain, "depth": number,
            }, ["start_date", "end_date", "lon_min", "lon_max", "lat_min", "lat_max", "variable", "source", "format"]),
            fn("mcp_marine_marine_download", "Turn a valid export token into a download URL.", {"token": string}, ["token"]),
            fn("mcp_marine_fisheries_catalog", "Return the live HF fisheries catalog.", {"domain": domain}),
            fn("mcp_marine_fisheries_inventory", "Search the live HF fisheries file inventory.", {
                "domain": domain, "keyword": string, "max_results": {"type": "integer"},
            }),
            fn("mcp_marine_fisheries_search", "Search live HF fisheries files.", {
                "query": string, "max_results": {"type": "integer"},
            }, ["query"]),
            fn("mcp_marine_fisheries_data_rules", "Return fisheries data interpretation rules."),
            fn("mcp_marine_fisheries_analyze_export", "Read and analyze a specific HF fisheries file, optionally exporting CSV.", {
                "repository": string, "path": string, "operation": string,
                "filters": {"type": "object"}, "metric": string,
            }, ["repository", "path"]),
        ]
        # The backend functions accept omitted optional values.  Responses
        # strict mode is intentionally not enabled so older OpenCode models
        # can omit optional keys.
        return tools

    @staticmethod
    def _jsonable(value):
        try:
            json.dumps(value, ensure_ascii=False)
            return value
        except TypeError:
            return {"status": "ok", "result": str(value)}

    @staticmethod
    def _run_tool(name: str, arguments: dict) -> dict:
        """Execute only the allow-listed Marine/HF functions.

        Importing the existing bridge keeps one source of truth for request
        validation and download URL construction.  No shell, filesystem or
        arbitrary Python tool is exposed to the model.
        """
        import marine_mcp

        short = str(name or "")
        if short.startswith("mcp_marine_"):
            short = short[len("mcp_marine_"):]
        aliases = {
            "marine_fisheries_catalog": "marine_fisheries_catalog",
            "fisheries_catalog": "fisheries_catalog",
        }
        short = aliases.get(short, short)
        allowed = {
            "marine_health", "marine_domains", "marine_status", "marine_catalog",
            "marine_query", "marine_subset", "marine_export", "marine_export_range", "marine_download",
            "marine_fisheries_catalog", "fisheries_catalog", "fisheries_inventory",
            "fisheries_search", "fisheries_data_rules", "fisheries_analyze_export",
        }
        if short not in allowed or not hasattr(marine_mcp, short):
            raise ValueError("unsupported Marine tool: " + str(name))
        args = dict(arguments or {})
        # OpenCode models sometimes emit depth=0 for every Ocean request.
        # ERA5, ERA5 accumulated fields, OISST and OC-CCI are 2-D products;
        # forwarding that synthetic depth makes the school API reject a valid
        # request with "has no depth dimension".  Preserve non-zero explicit
        # depths so an actually invalid user request still gets the server's
        # honest validation error.
        if short in {"marine_export", "marine_export_range", "marine_subset"}:
            source = str(args.get("source") or "").strip().lower()
            if source in {"era5", "era5_accum", "oisst", "occci", "oc-cci"}:
                try:
                    if float(args.get("depth")) == 0.0:
                        args.pop("depth", None)
                except (TypeError, ValueError):
                    pass
        result = getattr(marine_mcp, short)(**args)
        return CodexHarness._jsonable(result)

    def _request(self, body: dict, session_id: str) -> dict:
        request = urllib.request.Request(
            self.base_url + "/responses",
            data=json.dumps(body, ensure_ascii=False).encode("utf-8"),
            method="POST",
            headers={
                "Authorization": "Bearer " + self.api_key,
                "Content-Type": "application/json",
                "Accept": "application/json",
                "User-Agent": "Global-Marine-Foundation/4.5",
                "x-opencode-session": str(session_id),
            },
        )
        try:
            with urllib.request.urlopen(request, timeout=600) as response:
                return json.loads(response.read().decode("utf-8"))
        except urllib.error.HTTPError as exc:
            detail = exc.read().decode("utf-8", errors="replace")[:1200]
            raise RuntimeError(f"OpenCode Go API {exc.code}: {detail}") from exc
        except urllib.error.URLError as exc:
            raise RuntimeError(f"OpenCode Go connection failed: {exc.reason}") from exc

    @staticmethod
    def _function_calls(payload: dict) -> list[dict]:
        calls = []
        for item in payload.get("output") or []:
            if not isinstance(item, dict):
                continue
            if item.get("type") not in {"function_call", "tool_call"}:
                continue
            name = item.get("name") or item.get("function", {}).get("name")
            raw_args = item.get("arguments")
            if raw_args is None and isinstance(item.get("function"), dict):
                raw_args = item["function"].get("arguments")
            if isinstance(raw_args, str):
                try:
                    args = json.loads(raw_args or "{}")
                except json.JSONDecodeError as exc:
                    raise RuntimeError(f"Marine tool arguments are invalid: {exc}") from exc
            else:
                args = raw_args or {}
            calls.append({
                "name": str(name or ""),
                "arguments": args if isinstance(args, dict) else {},
                "call_id": item.get("call_id") or item.get("id") or str(uuid.uuid4()),
            })
        return calls

    def run(self, prompt, session_id=None):
        if not self.api_key:
            raise RuntimeError("OPENCODE_GO_API_KEY is not configured")
        request_session_id = str(session_id or uuid.uuid4())
        full_prompt = (self.system_prompt + "\n\n" + prompt).strip()
        tools = self._tools()
        payload = self._request({
            "model": self.model,
            "input": full_prompt,
            "tools": tools,
            "tool_choice": "auto",
        }, request_session_id)
        tool_names = []
        tool_events = []

        # Responses can contain multiple tool calls.  Execute them locally,
        # then continue the same Responses conversation until text is final.
        for _ in range(8):
            calls = self._function_calls(payload)
            if not calls:
                break
            outputs = []
            for call in calls:
                if call["name"] not in tool_names:
                    tool_names.append(call["name"])
                try:
                    value = self._run_tool(call["name"], call["arguments"])
                except Exception as exc:
                    value = {"status": "error", "detail": str(exc)[:1000]}
                if len(tool_events)<40:
                    tool_events.append(json.dumps({
                        "name":call["name"],
                        "arguments":call["arguments"],
                        "result":value,
                    },ensure_ascii=False,default=str)[:12000])
                outputs.append({
                    "type": "function_call_output",
                    "call_id": call["call_id"],
                    "output": json.dumps(value, ensure_ascii=False, default=str),
                })
            next_body = {
                "model": self.model,
                "previous_response_id": payload.get("id"),
                "input": outputs,
                "tools": tools,
                "tool_choice": "auto",
            }
            payload = self._request(next_body, request_session_id)
        else:
            raise RuntimeError("OpenCode Go tool loop exceeded 8 rounds")

        answer = self._text(payload)
        if not answer:
            raise RuntimeError("OpenCode Go returned no text: " + json.dumps(payload, ensure_ascii=False)[:1200])
        return CodexResult(final_response=answer, tool_names=tuple(tool_names), tool_events=tuple(tool_events))