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
 
 
 
 
 
 
 
 
 
 
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
"""Inkling 2 prompt normalization, chat-template rendering, and incremental tool/thinking parser."""

from __future__ import annotations

import json
import re
from typing import Any

MSG_MODEL = "<|message_model|>"
CONTENT_TEXT = "<|content_text|>"
CONTENT_THINK = "<|content_thinking|>"
CONTENT_TOOL_JSON = "<|content_invoke_tool_json|>"
CONTENT_TOOL_TEXT = "<|content_invoke_tool_text|>"
CONTENT_TOOL_ERROR = "<|content_tool_error|>"
END_MESSAGE = "<|end_message|>"
END_SAMPLING = "<|content_model_end_sampling|>"
ENDOFTEXT = "<|endoftext|>"

STOP_TOKENS = frozenset({END_SAMPLING, ENDOFTEXT})
STOP_IDS = frozenset({200006, 199999})

_EFFORT_ALIASES = {
    "off": "none",
    "none": "none",
    "minimal": "minimal",
    "low": "low",
    "medium": "medium",
    "high": "high",
    "xhigh": "xhigh",
    "max": "max",
}


class RequestError(Exception):
    def __init__(self, status: int, message: str):
        super().__init__(message)
        self.status = status
        self.message = message


def _flatten_content(content: Any) -> Any:
    if content is None:
        return ""
    if isinstance(content, str):
        return content
    if isinstance(content, list):
        has_image = any(
            isinstance(p, dict) and p.get("type") in ("image", "input_image", "image_url")
            for p in content
        )
        if has_image:
            return content
        parts: list[str] = []
        for part in content:
            if isinstance(part, str):
                parts.append(part)
            elif isinstance(part, dict) and isinstance(part.get("text"), str):
                parts.append(part["text"])
        return "".join(parts)
    return str(content)


def _parse_args(raw: Any) -> dict[str, Any]:
    if raw is None or raw == "":
        return {}
    if isinstance(raw, dict):
        return raw
    if isinstance(raw, str):
        try:
            parsed = json.loads(raw)
        except ValueError:
            return {}
        return parsed if isinstance(parsed, dict) else {}
    return {}


def normalize_messages(messages: Any) -> list[dict[str, Any]]:
    """Normalize OpenAI chat messages for Inkling 2's chat_template.jinja.

    Key template invariants enforced here:
    - 'developer' role maps to 'system'.
    - Assistant tool_calls[].function.arguments MUST be a dict (parsed object), never a JSON string.
    - Tool messages keep tool_call_id and/or name so the template resolves the header name.
    """
    if not isinstance(messages, list) or not messages:
        raise RequestError(400, "messages must be a non-empty list")

    call_names: dict[str, str] = {}
    out: list[dict[str, Any]] = []
    for msg in messages:
        if not isinstance(msg, dict):
            raise RequestError(400, "each message must be an object")
        role = msg.get("role")
        if role == "developer":
            role = "system"
        if role not in ("system", "user", "assistant", "tool"):
            raise RequestError(400, f"unsupported message role: {role!r}")

        norm: dict[str, Any] = {
            "role": role,
            "content": _flatten_content(msg.get("content")),
        }
        if isinstance(msg.get("name"), str) and msg["name"]:
            norm["name"] = msg["name"]

        if role == "assistant":
            rc = msg.get("reasoning_content")
            if isinstance(rc, str) and rc:
                norm["reasoning_content"] = rc
            raw_calls = msg.get("tool_calls")
            if isinstance(raw_calls, list) and raw_calls:
                calls = []
                for tc in raw_calls:
                    if not isinstance(tc, dict):
                        continue
                    fn = tc.get("function") if isinstance(tc.get("function"), dict) else tc
                    name = fn.get("name")
                    if not isinstance(name, str) or not name:
                        continue
                    tc_id = tc.get("id")
                    if isinstance(tc_id, str) and tc_id:
                        call_names[tc_id] = name
                    calls.append({
                        "type": "function",
                        "id": tc_id if isinstance(tc_id, str) else "",
                        "function": {
                            "name": name,
                            "arguments": _parse_args(fn.get("arguments")),
                        },
                    })
                if calls:
                    norm["tool_calls"] = calls
                    if "name" in norm and len(calls) == 1 and norm["name"] != calls[0]["function"]["name"]:
                        del norm["name"]
        elif role == "tool":
            tc_id = msg.get("tool_call_id")
            if isinstance(tc_id, str) and tc_id:
                norm["tool_call_id"] = tc_id
                if "name" not in norm and tc_id in call_names:
                    norm["name"] = call_names[tc_id]
        out.append(norm)
    return out


def normalize_tools(tools: Any) -> list[dict[str, Any]] | None:
    if tools is None:
        return None
    if not isinstance(tools, list):
        raise RequestError(400, "tools must be a list")
    if not tools:
        return None
    out = []
    for t in tools:
        if not isinstance(t, dict):
            raise RequestError(400, "each tool must be an object")
        fn = t.get("function") if isinstance(t.get("function"), dict) else t
        if not isinstance(fn.get("name"), str) or not fn["name"]:
            raise RequestError(400, "each tool must have a function.name")
        out.append(t)
    return out


def resolve_reasoning_effort(body: dict[str, Any]) -> str | float:
    raw = body.get("reasoning_effort")
    if raw is None and isinstance(body.get("reasoning"), dict):
        raw = body["reasoning"].get("effort")
    if raw is None and isinstance(body.get("chat_template_kwargs"), dict):
        ctk = body["chat_template_kwargs"]
        if "reasoning_effort" in ctk:
            raw = ctk["reasoning_effort"]
        elif ctk.get("enable_thinking") is False:
            raw = "none"
    if raw is None and body.get("enable_thinking") is False:
        raw = "none"
    if raw is None:
        return 0.9
    if isinstance(raw, str):
        key = raw.strip().lower()
        if key in _EFFORT_ALIASES:
            return _EFFORT_ALIASES[key]
        try:
            val = float(key)
            return max(0.0, min(0.99, val))
        except ValueError as exc:
            raise RequestError(400, f"Unknown reasoning_effort: {raw!r}") from exc
    if isinstance(raw, (int, float)) and not isinstance(raw, bool):
        return max(0.0, min(0.99, float(raw)))
    raise RequestError(400, f"Invalid reasoning_effort: {raw!r}")


def render_prompt_text(
    tokenizer: Any,
    messages: list[dict[str, Any]],
    tools: list[dict[str, Any]] | None,
    reasoning_effort: str | float,
) -> str:
    kwargs: dict[str, Any] = {
        "tokenize": False,
        "add_generation_prompt": True,
        "reasoning_effort": reasoning_effort,
    }
    if tools:
        kwargs["tools"] = tools
    text = tokenizer.apply_chat_template(messages, **kwargs)
    if not isinstance(text, str):
        raise RequestError(500, "chat_template did not return a string")
    return text


def render_prompt_ids(
    tokenizer: Any,
    messages: list[dict[str, Any]],
    tools: list[dict[str, Any]] | None,
    reasoning_effort: str | float,
) -> list[int]:
    text = render_prompt_text(tokenizer, messages, tools, reasoning_effort)
    ids = tokenizer.encode(text, add_special_tokens=False)
    if isinstance(ids, dict) or hasattr(ids, "input_ids"):
        ids = ids["input_ids"] if isinstance(ids, dict) else ids.input_ids
    return list(ids)


def _partial_suffix_len(buf: str, markers: tuple[str, ...]) -> int:
    best = 0
    for marker in markers:
        limit = min(len(buf), len(marker) - 1)
        for k in range(limit, best, -1):
            if buf.endswith(marker[:k]):
                best = k
                break
    return best


def _parse_invoke_json(recipient: str, payload: str) -> dict[str, Any] | None:
    text = payload.strip()
    if not text:
        return None
    try:
        obj = json.loads(text)
    except ValueError:
        m = re.search(r"\{.*\}", text, re.DOTALL)
        if not m:
            return None
        try:
            obj = json.loads(m.group(0))
        except ValueError:
            return None
    if not isinstance(obj, dict):
        return None
    name = obj.get("name") or recipient.strip()
    if not isinstance(name, str) or not name:
        return None
    args = obj.get("args", obj.get("arguments", {}))
    if isinstance(args, str):
        try:
            args = json.loads(args)
        except ValueError:
            args = {}
    if not isinstance(args, dict):
        args = {}
    return {"name": name, "arguments": args}


class InklingStreamParser:
    """Incremental parser for Inkling 2 block stream.

    Segments arrive as:
      <|message_model|><|content_thinking|>...<|end_message|>
      <|message_model|><|content_text|>...<|end_message|>
      <|message_model|>NAME<|content_invoke_tool_json|>{"name":"NAME","args":{...}}<|end_message|>
      <|content_model_end_sampling|>
    """

    _CONTENT_MARKERS = (
        CONTENT_THINK,
        CONTENT_TEXT,
        CONTENT_TOOL_JSON,
        CONTENT_TOOL_TEXT,
        CONTENT_TOOL_ERROR,
    )
    _STOP_OR_NEXT = (END_MESSAGE, END_SAMPLING, ENDOFTEXT, MSG_MODEL)

    def __init__(self, tool_id_prefix: str = "call_inkling_") -> None:
        self._buf = ""
        self._state = "seek_header"
        self._recipient = ""
        self._tool_buf = ""
        self.reasoning_content = ""
        self.content = ""
        self.tool_calls: list[dict[str, Any]] = []
        self.malformed = False
        self.stopped = False
        self._next_id = 1
        self._prefix = tool_id_prefix

    def feed(self, piece: str) -> list[dict[str, Any]]:
        if self.stopped:
            return []
        if piece:
            self._buf += piece
        events: list[dict[str, Any]] = []

        while self._buf and not self.stopped:
            if self._state == "seek_header":
                stop_pos = self._find_earliest(self._buf, (END_SAMPLING, ENDOFTEXT))
                hdr_pos = self._buf.find(MSG_MODEL)
                if stop_pos is not None and (hdr_pos < 0 or stop_pos < hdr_pos):
                    pre = self._buf[:stop_pos]
                    if pre:
                        self._emit_text(pre, events)
                    self._buf = ""
                    self.stopped = True
                    break
                if hdr_pos >= 0:
                    pre = self._buf[:hdr_pos]
                    if pre:
                        if any(m in pre for m in self._CONTENT_MARKERS):
                            self._state = "in_header"
                            continue
                        self._emit_text(pre, events)
                    self._buf = self._buf[hdr_pos + len(MSG_MODEL) :]
                    self._recipient = ""
                    self._state = "in_header"
                    continue
                mk_pos = self._find_earliest(self._buf, self._CONTENT_MARKERS)
                if mk_pos is not None:
                    self._recipient = self._buf[:mk_pos]
                    self._buf = self._buf[mk_pos:]
                    self._state = "in_header"
                    continue
                hold = _partial_suffix_len(
                    self._buf, (MSG_MODEL, END_SAMPLING, ENDOFTEXT, *self._CONTENT_MARKERS)
                )
                safe = self._buf[:-hold] if hold else self._buf
                if safe:
                    if "<|" not in safe and len(safe) > 32:
                        self._emit_text(safe, events)
                        self._buf = self._buf[len(safe) :]
                        continue
                break

            if self._state == "in_header":
                stop_pos = self._find_earliest(self._buf, (END_SAMPLING, ENDOFTEXT))
                found_marker = None
                found_pos = None
                for mk in self._CONTENT_MARKERS:
                    p = self._buf.find(mk)
                    if p >= 0 and (found_pos is None or p < found_pos):
                        found_pos = p
                        found_marker = mk
                if stop_pos is not None and (found_pos is None or stop_pos < found_pos):
                    self._buf = ""
                    self.stopped = True
                    break
                if found_marker is not None and found_pos is not None:
                    self._recipient += self._buf[:found_pos]
                    self._buf = self._buf[found_pos + len(found_marker) :]
                    if found_marker == CONTENT_THINK:
                        self._state = "in_think"
                    elif found_marker == CONTENT_TOOL_JSON:
                        self._tool_buf = ""
                        self._state = "in_tool_json"
                    else:
                        self._state = "in_text"
                    continue
                break

            if self._state in ("in_think", "in_text"):
                end_pos = self._buf.find(END_MESSAGE)
                stop_pos = self._find_earliest(self._buf, (END_SAMPLING, ENDOFTEXT))
                next_hdr = self._buf.find(MSG_MODEL)
                cut = None
                cut_kind = None
                for pos, kind, length in (
                    (end_pos, "end", len(END_MESSAGE)),
                    (stop_pos, "stop", 0),
                    (next_hdr, "hdr", 0),
                ):
                    if pos is not None and pos >= 0 and (cut is None or pos < cut):
                        cut = pos
                        cut_kind = (kind, length)
                if cut is not None:
                    body = self._buf[:cut]
                    if body:
                        if self._state == "in_think":
                            self._emit_think(body, events)
                        else:
                            self._emit_text(body, events)
                    kind, length = cut_kind
                    if kind == "end":
                        self._buf = self._buf[cut + length :]
                        self._state = "seek_header"
                    elif kind == "hdr":
                        self._buf = self._buf[cut:]
                        self._state = "seek_header"
                    else:
                        self._buf = ""
                        self.stopped = True
                    continue
                hold = _partial_suffix_len(self._buf, self._STOP_OR_NEXT)
                safe = self._buf[:-hold] if hold else self._buf
                if safe:
                    if self._state == "in_think":
                        self._emit_think(safe, events)
                    else:
                        self._emit_text(safe, events)
                    self._buf = self._buf[len(safe) :]
                break

            if self._state == "in_tool_json":
                end_pos = self._buf.find(END_MESSAGE)
                stop_pos = self._find_earliest(self._buf, (END_SAMPLING, ENDOFTEXT))
                cut = None
                cut_kind = None
                for pos, kind, length in (
                    (end_pos, "end", len(END_MESSAGE)),
                    (stop_pos, "stop", 0),
                ):
                    if pos is not None and pos >= 0 and (cut is None or pos < cut):
                        cut = pos
                        cut_kind = (kind, length)
                if cut is not None:
                    self._tool_buf += self._buf[:cut]
                    kind, length = cut_kind
                    self._finalize_tool(events)
                    if kind == "end":
                        self._buf = self._buf[cut + length :]
                        self._state = "seek_header"
                    else:
                        self._buf = ""
                        self.stopped = True
                    continue
                hold = _partial_suffix_len(self._buf, (END_MESSAGE, END_SAMPLING, ENDOFTEXT))
                safe = self._buf[:-hold] if hold else self._buf
                if safe:
                    self._tool_buf += safe
                    self._buf = self._buf[len(safe) :]
                break

        return events

    def finish(self) -> list[dict[str, Any]]:
        events = self.feed("")
        if self._state == "in_tool_json" and (self._tool_buf or self._buf):
            self._tool_buf += self._buf
            self._buf = ""
            self._finalize_tool(events)
        elif self._buf:
            cleaned = self._buf
            for tok in (END_SAMPLING, ENDOFTEXT, END_MESSAGE):
                cleaned = cleaned.replace(tok, "")
            if cleaned:
                if self._state == "in_think":
                    self._emit_think(cleaned, events)
                elif self._state == "in_text":
                    self._emit_text(cleaned, events)
                elif self._state == "seek_header" and not cleaned.startswith("<|"):
                    self._emit_text(cleaned, events)
            self._buf = ""
        self._state = "done"
        return events

    def _emit_think(self, text: str, events: list[dict[str, Any]]) -> None:
        self.reasoning_content += text
        events.append({"reasoning_content": text})

    def _emit_text(self, text: str, events: list[dict[str, Any]]) -> None:
        self.content += text
        events.append({"content": text})

    def _finalize_tool(self, events: list[dict[str, Any]]) -> None:
        parsed = _parse_invoke_json(self._recipient, self._tool_buf)
        if parsed is None:
            self.malformed = True
            raw = self._tool_buf
            self._tool_buf = ""
            if raw:
                self._emit_text(raw, events)
            return
        idx = len(self.tool_calls)
        call_id = f"{self._prefix}{self._next_id}"
        self._next_id += 1
        wire = {
            "id": call_id,
            "type": "function",
            "function": {
                "name": parsed["name"],
                "arguments": json.dumps(parsed["arguments"], ensure_ascii=False),
            },
        }
        self.tool_calls.append(wire)
        events.append({"tool_calls": [{**wire, "index": idx}]})
        self._tool_buf = ""

    @staticmethod
    def _find_earliest(buf: str, needles: tuple[str, ...]) -> int | None:
        best: int | None = None
        for n in needles:
            pos = buf.find(n)
            if pos >= 0 and (best is None or pos < best):
                best = pos
        return best


def parse_complete_output(raw_text: str) -> dict[str, Any]:
    stream = InklingStreamParser()
    stream.feed(raw_text)
    stream.finish()
    return {
        "content": stream.content,
        "reasoning_content": stream.reasoning_content,
        "tool_calls": stream.tool_calls,
        "malformed": stream.malformed,
    }