Repository navigation
Expand file tree
/
Copy pathresponses_projection.py
More file actions
689 lines (561 loc) · 21.2 KB
/
Copy pathresponses_projection.py
File metadata and controls
689 lines (561 loc) · 21.2 KB
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
"""
responses_projection — /v1/responses 的后端投影层。
目标
----
Codex CLI 会把大量运行时提示、完整工具 schema、长历史、以及工具输出一并塞进
/v1/responses 请求里。腾讯后端对这类 agentic payload 很容易触发内容审核,或者
因为上下文过长而表现不稳定。
本模块在保持外部 OpenAI Responses 兼容的前提下,只对发往后端的 Chat body 做
"最小语义闭包"投影:
- 固定短 system 摘要替换 Codex/Claude Code harness
- 保留最新用户意图
- 保留最近一段真实 assistant/tool 链路
- 把更早历史压缩成规则摘要
- 把 tool schema 收敛成结构字段
- 把超长 tool output / tool arguments 压缩成可继续推理的摘要
"""
from __future__ import annotations
import json
from typing import Any
AGENTIC_TOOL_NAMES = {
"exec_command",
"write_stdin",
"update_plan",
"request_user_input",
"view_image",
"get_goal",
"create_goal",
"update_goal",
"apply_patch",
"tool_search_tool",
}
HARNESS_USER_MARKERS = (
"# AGENTS.md instructions",
"<environment_context>",
"<permissions instructions>",
"<collaboration_mode>",
"<skills_instructions>",
"<system-reminder>",
"# claudeMd",
)
HARNESS_SYSTEM_MARKERS = (
"You are a coding agent running in the Codex CLI",
"Within this context, Codex refers to",
"# AGENTS.md spec",
"<permissions instructions>",
"<collaboration_mode>",
"<skills_instructions>",
"The following deferred tools are now available via ToolSearch.",
"### Available skills",
"## request_user_input availability",
"You are Claude Code",
)
BASE_SYSTEM_PROMPT = (
"You are a coding assistant serving an OpenAI-compatible CLI. "
"Be precise, concise, safe, and action-oriented. "
"Use available tools when needed, follow repository instructions and durable user context, "
"and continue from the preserved recent context. "
"If earlier history was condensed, rely on the preserved recent messages and rerun tools when exact old details are required."
)
HISTORY_PREFIX = "Earlier conversation summary (condensed):"
MAX_SYSTEM_GUIDANCE_CHARS = 1200
MAX_USER_CHARS = 3200
MAX_ASSISTANT_CHARS = 1800
MAX_TOOL_OUTPUT_CHARS = 1600
MAX_TOOL_ARGS_CHARS = 900
MAX_HISTORY_SUMMARY_CHARS = 2200
MAX_HISTORY_ITEMS = 10
MAX_TAIL_MESSAGES = 8
MAX_TAIL_CHARS = 7000
SCHEMA_KEEP_KEYS = {
"type",
"properties",
"required",
"items",
"enum",
"oneOf",
"anyOf",
"allOf",
"additionalProperties",
"format",
"minimum",
"maximum",
"minItems",
"maxItems",
"minLength",
"maxLength",
"nullable",
}
def project_responses_chat_body(body: dict) -> tuple[dict, dict]:
"""把 Responses 转出来的 Chat body 投影成更适合腾讯后端的最小上下文。"""
projected = dict(body)
messages = list(body.get("messages") or [])
tools = list(body.get("tools") or [])
projected_tools, tool_stats = _project_tools(tools)
if projected_tools:
projected["tools"] = projected_tools
elif "tools" in projected:
projected["tools"] = []
aggressive = _looks_like_agentic_cli(messages, tools)
if not aggressive:
projected["messages"] = _project_messages_conservative(messages)
return projected, {
"mode": "conservative",
"aggressive": False,
"original_messages": len(messages),
"projected_messages": len(projected["messages"]),
"original_message_chars": _messages_size(messages),
"projected_message_chars": _messages_size(projected["messages"]),
**tool_stats,
}
tool_name_by_call_id = _build_tool_call_name_map(messages)
preserved_guidance: list[str] = []
conversation: list[dict] = []
dropped_harness_messages = 0
for msg in messages:
if not isinstance(msg, dict):
continue
role = msg.get("role")
text = _content_to_text(msg.get("content", ""))
if role == "system":
if _looks_like_harness_system(text):
dropped_harness_messages += 1
continue
guidance = _truncate_text(text, MAX_SYSTEM_GUIDANCE_CHARS)
if guidance:
preserved_guidance.append(guidance)
continue
if role == "user" and _looks_like_harness_user(text):
dropped_harness_messages += 1
continue
projected_msg = _project_conversation_message(msg)
if projected_msg is not None:
conversation.append(projected_msg)
if not conversation:
conversation = _project_messages_conservative(messages)
tail_start = _choose_tail_start(conversation)
tail_start = _expand_tail_for_tool_context(conversation, tail_start)
latest_user_idx = _latest_user_index(conversation)
anchor_user = None
if latest_user_idx is not None and latest_user_idx < tail_start:
anchor_user = dict(conversation[latest_user_idx])
omitted: list[dict] = []
for idx, msg in enumerate(conversation):
if idx >= tail_start:
break
if latest_user_idx is not None and idx == latest_user_idx and anchor_user is not None:
continue
omitted.append(msg)
final_messages: list[dict] = [{"role": "system", "content": BASE_SYSTEM_PROMPT}]
guidance_message = _merge_guidance_messages(preserved_guidance)
if guidance_message:
final_messages.append({"role": "system", "content": guidance_message})
history_summary = _build_history_summary(omitted, tool_name_by_call_id)
if history_summary:
final_messages.append({"role": "system", "content": history_summary})
if anchor_user is not None:
final_messages.append(anchor_user)
final_messages.extend(conversation[tail_start:])
projected["messages"] = final_messages
return projected, {
"mode": "aggressive",
"aggressive": True,
"dropped_harness_messages": dropped_harness_messages,
"preserved_guidance_messages": len(preserved_guidance),
"summarized_history_messages": len(omitted),
"anchor_user_preserved": anchor_user is not None,
"tail_messages": len(conversation[tail_start:]),
"original_messages": len(messages),
"projected_messages": len(final_messages),
"original_message_chars": _messages_size(messages),
"projected_message_chars": _messages_size(final_messages),
**tool_stats,
}
def _looks_like_agentic_cli(messages: list[dict], tools: list[dict]) -> bool:
tool_names = {
_tool_name(tool)
for tool in tools
if _tool_name(tool)
}
if tool_names & AGENTIC_TOOL_NAMES:
return True
for msg in messages:
if not isinstance(msg, dict):
continue
text = _content_to_text(msg.get("content", ""))
if _looks_like_harness_user(text) or _looks_like_harness_system(text):
return True
return False
def _project_messages_conservative(messages: list[dict]) -> list[dict]:
out: list[dict] = []
for msg in messages:
projected = _project_conversation_message(msg, conservative=True)
if projected is not None:
out.append(projected)
return out
def _project_conversation_message(msg: dict, conservative: bool = False) -> dict | None:
if not isinstance(msg, dict):
return None
role = msg.get("role")
out = dict(msg)
if role == "system":
text = _content_to_text(msg.get("content", ""))
out["content"] = _truncate_text(text, MAX_SYSTEM_GUIDANCE_CHARS)
return out
if role == "user":
text = _content_to_text(msg.get("content", ""))
out["content"] = _truncate_text(text, MAX_USER_CHARS)
return out
if role == "assistant":
text = _content_to_text(msg.get("content", ""))
out["content"] = _summarize_free_text(text, MAX_ASSISTANT_CHARS)
tool_calls = []
for tool_call in msg.get("tool_calls") or []:
projected_call = _project_tool_call(tool_call)
if projected_call is not None:
tool_calls.append(projected_call)
if tool_calls:
out["tool_calls"] = tool_calls
elif "tool_calls" in out:
out.pop("tool_calls", None)
return out
if role == "tool":
out["content"] = _summarize_tool_output(_content_to_text(msg.get("content", "")))
return out
if conservative:
text = _content_to_text(msg.get("content", ""))
out["content"] = _truncate_text(text, MAX_ASSISTANT_CHARS)
return out
return None
def _project_tool_call(tool_call: dict) -> dict | None:
if not isinstance(tool_call, dict):
return None
function = tool_call.get("function") or {}
name = function.get("name", "")
arguments = function.get("arguments", "")
return {
"id": tool_call.get("id"),
"type": tool_call.get("type", "function"),
"function": {
"name": name,
"arguments": _summarize_tool_arguments(name, arguments),
},
}
def _summarize_tool_arguments(name: str, arguments: Any) -> str:
if not isinstance(arguments, str):
try:
return json.dumps(arguments, ensure_ascii=False)
except Exception:
return json.dumps({"summary": _truncate_text(str(arguments), 240)}, ensure_ascii=False)
if len(arguments) <= MAX_TOOL_ARGS_CHARS:
return arguments
if name == "apply_patch":
return json.dumps(
{"summary": "Large apply_patch payload omitted; a patch was prepared or applied in a previous step."},
ensure_ascii=False,
)
try:
parsed = json.loads(arguments)
except Exception:
return json.dumps({"summary": _truncate_text(arguments, 320)}, ensure_ascii=False)
return json.dumps(_shrink_json_value(parsed), ensure_ascii=False)
def _shrink_json_value(value: Any, depth: int = 0, key: str = "") -> Any:
if depth >= 4:
return "<omitted>"
if isinstance(value, dict):
out = {}
items = list(value.items())
for idx, (item_key, item_value) in enumerate(items):
if idx >= 12:
out["_omitted_keys"] = len(items) - idx
break
out[item_key] = _shrink_json_value(item_value, depth + 1, item_key)
return out
if isinstance(value, list):
trimmed = [_shrink_json_value(item, depth + 1, key) for item in value[:6]]
if len(value) > 6:
trimmed.append(f"<omitted {len(value) - 6} items>")
return trimmed
if isinstance(value, str):
limit = 240 if key in {"cmd", "chars", "patch", "content", "text", "question"} else 120
return _truncate_text(value, limit)
return value
def _project_tools(tools: list[dict]) -> tuple[list[dict], dict]:
projected = []
original_chars = _tools_size(tools)
for tool in tools:
if not isinstance(tool, dict):
continue
if tool.get("type") != "function":
continue
function = tool.get("function") or tool
name = function.get("name")
if not name:
continue
projected_function: dict[str, Any] = {"name": name}
if "parameters" in function:
projected_function["parameters"] = _project_schema(function.get("parameters"))
if "strict" in function:
projected_function["strict"] = function.get("strict")
projected.append({"type": "function", "function": projected_function})
return projected, {
"original_tools": len(tools),
"projected_tools": len(projected),
"original_tool_chars": original_chars,
"projected_tool_chars": _tools_size(projected),
}
def _project_schema(schema: Any, depth: int = 0) -> Any:
if depth >= 6:
return {"type": "object"}
if isinstance(schema, dict):
out: dict[str, Any] = {}
for key, value in schema.items():
if key not in SCHEMA_KEEP_KEYS:
continue
if key == "properties" and isinstance(value, dict):
out["properties"] = {
prop: _project_schema(prop_schema, depth + 1)
for prop, prop_schema in value.items()
}
elif key == "items":
out["items"] = _project_schema(value, depth + 1)
elif key in {"oneOf", "anyOf", "allOf"} and isinstance(value, list):
out[key] = [_project_schema(item, depth + 1) for item in value[:6]]
elif key == "additionalProperties" and isinstance(value, dict):
out[key] = _project_schema(value, depth + 1)
else:
out[key] = value
return out or {"type": "object"}
if isinstance(schema, list):
return [_project_schema(item, depth + 1) for item in schema[:6]]
return schema
def _choose_tail_start(messages: list[dict]) -> int:
if not messages:
return 0
start = len(messages) - 1
total_chars = 0
kept = 0
for idx in range(len(messages) - 1, -1, -1):
cost = _message_cost(messages[idx])
if kept > 0 and (kept >= MAX_TAIL_MESSAGES or total_chars + cost > MAX_TAIL_CHARS):
break
start = idx
total_chars += cost
kept += 1
return start
def _expand_tail_for_tool_context(messages: list[dict], start: int) -> int:
if start <= 0 or not messages:
return start
needed_call_ids = {
msg.get("tool_call_id")
for msg in messages[start:]
if isinstance(msg, dict) and msg.get("role") == "tool" and msg.get("tool_call_id")
}
if not needed_call_ids:
return start
expanded = start
for idx in range(start - 1, -1, -1):
msg = messages[idx]
if msg.get("role") != "assistant":
continue
call_ids = {
tool_call.get("id")
for tool_call in msg.get("tool_calls") or []
if isinstance(tool_call, dict)
}
if call_ids & needed_call_ids:
expanded = idx
needed_call_ids -= call_ids
if not needed_call_ids:
break
return expanded
def _latest_user_index(messages: list[dict]) -> int | None:
for idx in range(len(messages) - 1, -1, -1):
if messages[idx].get("role") == "user":
return idx
return None
def _build_history_summary(messages: list[dict], tool_name_by_call_id: dict[str, str]) -> str:
lines: list[str] = []
total_chars = 0
summarized = 0
for msg in messages:
line = _history_line(msg, tool_name_by_call_id)
if not line:
continue
if summarized >= MAX_HISTORY_ITEMS or total_chars + len(line) > MAX_HISTORY_SUMMARY_CHARS:
break
lines.append(f"- {line}")
total_chars += len(line)
summarized += 1
remaining = len(messages) - summarized
if remaining > 0:
lines.append(f"- {remaining} earlier messages or tool results were further condensed.")
if not lines:
return ""
return HISTORY_PREFIX + "\n" + "\n".join(lines)
def _history_line(msg: dict, tool_name_by_call_id: dict[str, str]) -> str:
role = msg.get("role")
text = _content_to_text(msg.get("content", ""))
if role == "user":
return f"User asked: {_truncate_text(text, 220)}"
if role == "assistant":
tool_names = [
(tool_call.get("function") or {}).get("name")
for tool_call in msg.get("tool_calls") or []
if isinstance(tool_call, dict)
]
tool_names = [name for name in tool_names if name]
if text and tool_names:
return f"Assistant replied: {_truncate_text(text, 160)} Then called tools: {', '.join(tool_names[:4])}."
if tool_names:
return f"Assistant called tools: {', '.join(tool_names[:4])}."
if text:
return f"Assistant replied: {_truncate_text(text, 180)}"
return ""
if role == "tool":
tool_name = tool_name_by_call_id.get(msg.get("tool_call_id", ""), "tool")
summary = _tool_output_inline_summary(text)
return f"Tool {tool_name} returned: {summary}"
if role == "system":
return f"System guidance: {_truncate_text(text, 180)}"
return ""
def _build_tool_call_name_map(messages: list[dict]) -> dict[str, str]:
mapping: dict[str, str] = {}
for msg in messages:
if not isinstance(msg, dict) or msg.get("role") != "assistant":
continue
for tool_call in msg.get("tool_calls") or []:
if not isinstance(tool_call, dict):
continue
call_id = tool_call.get("id")
name = (tool_call.get("function") or {}).get("name")
if call_id and name:
mapping[call_id] = name
return mapping
def _merge_guidance_messages(messages: list[str]) -> str:
merged: list[str] = []
total = 0
for message in messages[:2]:
text = message.strip()
if not text:
continue
if total + len(text) > MAX_SYSTEM_GUIDANCE_CHARS:
text = _truncate_text(text, MAX_SYSTEM_GUIDANCE_CHARS - total)
merged.append(text)
total += len(text)
if total >= MAX_SYSTEM_GUIDANCE_CHARS:
break
if not merged:
return ""
if len(merged) == 1:
return merged[0]
return "Additional instructions:\n" + "\n\n".join(merged)
def _summarize_tool_output(text: str) -> str:
text = (text or "").strip()
if not text:
return ""
if len(text) <= MAX_TOOL_OUTPUT_CHARS and text.count("\n") <= 24:
return text
lines = text.splitlines()
exit_line = next((line.strip() for line in lines if "Process exited with code" in line), "")
useful_lines = []
saw_output = False
for line in lines:
stripped = line.rstrip()
if stripped == "Output:":
saw_output = True
continue
if (
stripped.startswith("Chunk ID:")
or stripped.startswith("Wall time:")
or stripped.startswith("Original token count:")
or stripped.startswith("Process exited with code")
):
continue
useful_lines.append(stripped)
body_lines = useful_lines
head = body_lines[:10]
tail = body_lines[-6:] if len(body_lines) > 16 else []
omitted = max(len(body_lines) - len(head) - len(tail), 0)
parts: list[str] = []
if exit_line:
parts.append(exit_line)
if head:
parts.append("Key output:")
parts.extend(head)
if omitted:
parts.append(f"... [omitted {omitted} lines] ...")
if tail:
parts.append("Recent tail:")
parts.extend(tail)
summary = "\n".join(part for part in parts if part).strip()
return _truncate_text(summary or text, MAX_TOOL_OUTPUT_CHARS)
def _tool_output_inline_summary(text: str) -> str:
summarized = _summarize_tool_output(text)
summarized = summarized.replace("\n", " | ")
return _truncate_text(summarized, 220)
def _summarize_free_text(text: str, limit: int) -> str:
text = (text or "").strip()
if not text:
return ""
if len(text) <= limit:
return text
head = text[: limit // 2].rstrip()
tail = text[-(limit // 3):].lstrip()
omitted = len(text) - len(head) - len(tail)
return f"{head}\n... [{omitted} chars omitted] ...\n{tail}"
def _truncate_text(text: str, limit: int) -> str:
text = (text or "").strip()
if not text:
return ""
if len(text) <= limit:
return text
return text[: max(limit - 24, 0)].rstrip() + f" ... [truncated {len(text) - max(limit - 24, 0)} chars]"
def _content_to_text(content: Any) -> str:
if content is None:
return ""
if isinstance(content, str):
return content
if isinstance(content, list):
parts = []
for block in content:
if isinstance(block, dict):
if "text" in block:
parts.append(str(block.get("text", "")))
elif "output" in block:
parts.append(str(block.get("output", "")))
elif isinstance(block, str):
parts.append(block)
return "".join(parts)
return str(content)
def _looks_like_harness_user(text: str) -> bool:
return any(marker in text for marker in HARNESS_USER_MARKERS)
def _looks_like_harness_system(text: str) -> bool:
return any(marker in text for marker in HARNESS_SYSTEM_MARKERS)
def _message_cost(msg: dict) -> int:
cost = len(_content_to_text(msg.get("content", "")))
for tool_call in msg.get("tool_calls") or []:
if not isinstance(tool_call, dict):
continue
function = tool_call.get("function") or {}
cost += len(function.get("name", ""))
cost += len(function.get("arguments", ""))
return cost
def _messages_size(messages: list[dict]) -> int:
total = 0
for msg in messages:
if not isinstance(msg, dict):
continue
total += _message_cost(msg)
total += len(msg.get("role", ""))
return total
def _tool_name(tool: dict) -> str:
if not isinstance(tool, dict):
return ""
function = tool.get("function") or tool
return str(function.get("name", "") or "")
def _tools_size(tools: list[dict]) -> int:
try:
return len(json.dumps(tools, ensure_ascii=False))
except Exception:
return 0