@jsonstudio/llms 0.6.802 → 0.6.938

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (186) hide show
  1. package/dist/bridge/routecodex-adapter.d.ts +74 -0
  2. package/dist/config-unified/enhanced-path-resolver.d.ts +5 -0
  3. package/dist/config-unified/unified-config.d.ts +26 -0
  4. package/dist/conversion/codec-registry.d.ts +10 -0
  5. package/dist/conversion/codecs/gemini-openai-codec.d.ts +16 -0
  6. package/dist/conversion/codecs/openai-openai-codec.d.ts +12 -0
  7. package/dist/conversion/codecs/responses-openai-codec.d.ts +12 -0
  8. package/dist/conversion/compat/profiles/chat-gemini.json +12 -0
  9. package/dist/conversion/config/config-manager.d.ts +212 -0
  10. package/dist/conversion/hub/config/types.d.ts +26 -0
  11. package/dist/conversion/hub/core/detour-registry.d.ts +9 -0
  12. package/dist/conversion/hub/core/hub-context.d.ts +21 -0
  13. package/dist/conversion/hub/core/index.d.ts +3 -0
  14. package/dist/conversion/hub/core/stage-driver.d.ts +30 -0
  15. package/dist/conversion/hub/format-adapters/anthropic-format-adapter.d.ts +16 -0
  16. package/dist/conversion/hub/format-adapters/chat-format-adapter.d.ts +17 -0
  17. package/dist/conversion/hub/format-adapters/gemini-format-adapter.d.ts +16 -0
  18. package/dist/conversion/hub/format-adapters/index.d.ts +21 -0
  19. package/dist/conversion/hub/hub-feature.d.ts +1 -0
  20. package/dist/conversion/hub/node-support.d.ts +19 -0
  21. package/dist/conversion/hub/pipeline/compat/compat-pipeline-executor.js +11 -0
  22. package/dist/conversion/hub/pipeline/compat/compat-types.d.ts +3 -0
  23. package/dist/conversion/hub/pipeline/hub-pipeline.d.ts +7 -0
  24. package/dist/conversion/hub/pipeline/hub-pipeline.js +71 -14
  25. package/dist/conversion/hub/pipeline/stages/req_process/req_process_stage1_tool_governance/index.js +4 -0
  26. package/dist/conversion/hub/pipeline/stages/resp_outbound/resp_outbound_stage1_client_remap/index.js +23 -1
  27. package/dist/conversion/hub/pipelines/inbound.d.ts +22 -0
  28. package/dist/conversion/hub/pipelines/outbound.d.ts +22 -0
  29. package/dist/conversion/hub/policy/policy-engine.d.ts +46 -0
  30. package/dist/conversion/hub/policy/policy-engine.js +176 -0
  31. package/dist/conversion/hub/policy/protocol-spec.d.ts +50 -0
  32. package/dist/conversion/hub/policy/protocol-spec.js +105 -0
  33. package/dist/conversion/hub/process/chat-process.d.ts +32 -0
  34. package/dist/conversion/hub/registry.d.ts +28 -0
  35. package/dist/conversion/hub/response/chat-response-utils.d.ts +6 -0
  36. package/dist/conversion/hub/response/provider-response.js +31 -0
  37. package/dist/conversion/hub/semantic-mappers/gemini-mapper.d.ts +7 -0
  38. package/dist/conversion/hub/semantic-mappers/gemini-mapper.js +87 -1
  39. package/dist/conversion/hub/semantic-mappers/index.d.ts +4 -0
  40. package/dist/conversion/hub/semantic-mappers/responses-mapper.d.ts +21 -0
  41. package/dist/conversion/hub/standardized-bridge.d.ts +12 -0
  42. package/dist/conversion/hub/types/chat-schema.d.ts +112 -0
  43. package/dist/conversion/hub/types/errors.d.ts +5 -0
  44. package/dist/conversion/hub/types/format-envelope.d.ts +7 -0
  45. package/dist/conversion/hub/types/index.d.ts +6 -0
  46. package/dist/conversion/hub/types/json.d.ts +9 -0
  47. package/dist/conversion/hub/types/node.d.ts +31 -0
  48. package/dist/conversion/responses/responses-openai-bridge.js +263 -10
  49. package/dist/conversion/schema-validator.d.ts +7 -0
  50. package/dist/conversion/shared/args-mapping.d.ts +18 -0
  51. package/dist/conversion/shared/chat-request-filters.d.ts +9 -0
  52. package/dist/conversion/shared/errors.d.ts +1 -1
  53. package/dist/conversion/shared/gemini-tool-utils.js +61 -0
  54. package/dist/conversion/shared/jsonish.d.ts +3 -0
  55. package/dist/conversion/shared/mcp-injection.d.ts +2 -0
  56. package/dist/conversion/shared/media.d.ts +1 -0
  57. package/dist/conversion/shared/openai-message-normalize.d.ts +1 -0
  58. package/dist/conversion/shared/payload-budget.d.ts +13 -0
  59. package/dist/conversion/shared/reasoning-mapping.d.ts +5 -0
  60. package/dist/conversion/shared/responses-request-adapter.d.ts +1 -28
  61. package/dist/conversion/shared/responses-request-adapter.js +1 -430
  62. package/dist/conversion/shared/snapshot-hooks.js +58 -3
  63. package/dist/conversion/shared/tool-governor.js +8 -2
  64. package/dist/conversion/shared/tool-harvester.d.ts +31 -0
  65. package/dist/conversion/shared/tool-mapping.js +10 -29
  66. package/dist/conversion/types.d.ts +33 -0
  67. package/dist/filters/builtin/add-fields-filter.d.ts +8 -0
  68. package/dist/filters/builtin/blacklist-filter.d.ts +8 -0
  69. package/dist/filters/builtin/whitelist-filter.d.ts +8 -0
  70. package/dist/filters/engine.d.ts +16 -0
  71. package/dist/filters/special/request-tool-choice-policy.d.ts +11 -0
  72. package/dist/filters/special/response-finish-invariants.d.ts +11 -0
  73. package/dist/filters/special/response-openai-to-responses-bridge.d.ts +13 -0
  74. package/dist/filters/special/response-tool-arguments-blacklist.d.ts +12 -0
  75. package/dist/filters/special/response-tool-arguments-schema-converge.d.ts +13 -0
  76. package/dist/filters/special/response-tool-arguments-stringify.d.ts +9 -0
  77. package/dist/filters/special/response-tool-arguments-whitelist.d.ts +11 -0
  78. package/dist/filters/special/tool-filter-hooks.d.ts +19 -0
  79. package/dist/filters/special/tool-post-constraints.d.ts +31 -0
  80. package/dist/filters/types.d.ts +68 -0
  81. package/dist/filters/utils/fieldmap-loader.d.ts +2 -0
  82. package/dist/filters/utils/snapshot-writer.d.ts +10 -0
  83. package/dist/guidance/index.d.ts +3 -0
  84. package/dist/guidance/index.js +78 -83
  85. package/dist/http/sse-response.d.ts +22 -0
  86. package/dist/router/virtual-router/bootstrap.d.ts +6 -0
  87. package/dist/router/virtual-router/bootstrap.js +49 -5
  88. package/dist/router/virtual-router/classifier.d.ts +10 -0
  89. package/dist/router/virtual-router/engine-selection.js +98 -11
  90. package/dist/router/virtual-router/engine.js +177 -31
  91. package/dist/router/virtual-router/error-center.d.ts +10 -0
  92. package/dist/router/virtual-router/features.d.ts +3 -0
  93. package/dist/router/virtual-router/routing-instructions.d.ts +23 -1
  94. package/dist/router/virtual-router/routing-instructions.js +120 -30
  95. package/dist/router/virtual-router/types.d.ts +11 -0
  96. package/dist/servertool/engine.js +189 -16
  97. package/dist/servertool/handlers/apply-patch-guard.js +269 -0
  98. package/dist/servertool/handlers/exec-command-guard.js +558 -0
  99. package/dist/servertool/handlers/followup-message-trimmer.d.ts +16 -0
  100. package/dist/servertool/handlers/followup-message-trimmer.js +198 -0
  101. package/dist/servertool/handlers/followup-request-builder.d.ts +17 -0
  102. package/dist/servertool/handlers/followup-request-builder.js +122 -0
  103. package/dist/servertool/handlers/gemini-empty-reply-continue.js +252 -51
  104. package/dist/servertool/handlers/iflow-model-error-retry.js +12 -22
  105. package/dist/servertool/handlers/stop-message-auto.js +237 -75
  106. package/dist/servertool/handlers/vision.js +15 -27
  107. package/dist/servertool/handlers/web-search.js +17 -43
  108. package/dist/servertool/server-side-tools.d.ts +3 -0
  109. package/dist/servertool/server-side-tools.js +3 -0
  110. package/dist/sse/json-to-sse/anthropic-json-to-sse-converter.d.ts +2 -1
  111. package/dist/sse/json-to-sse/chat-json-to-sse-converter.d.ts +80 -0
  112. package/dist/sse/json-to-sse/event-generators/chat.d.ts +55 -0
  113. package/dist/sse/json-to-sse/event-generators/responses.d.ts +99 -0
  114. package/dist/sse/json-to-sse/gemini-json-to-sse-converter.d.ts +2 -1
  115. package/dist/sse/json-to-sse/responses-json-to-sse-converter.d.ts +80 -0
  116. package/dist/sse/json-to-sse/sequencers/anthropic-sequencer.d.ts +1 -1
  117. package/dist/sse/json-to-sse/sequencers/chat-sequencer.d.ts +2 -2
  118. package/dist/sse/json-to-sse/sequencers/gemini-sequencer.d.ts +1 -1
  119. package/dist/sse/json-to-sse/sequencers/responses-sequencer.d.ts +40 -0
  120. package/dist/sse/shared/chat-serializer.d.ts +4 -0
  121. package/dist/sse/shared/constants.d.ts +272 -0
  122. package/dist/sse/shared/serializers/anthropic-event-serializer.d.ts +1 -1
  123. package/dist/sse/shared/serializers/base-serializer.d.ts +158 -0
  124. package/dist/sse/shared/serializers/chat-event-serializer.d.ts +82 -0
  125. package/dist/sse/shared/serializers/gemini-event-serializer.d.ts +1 -1
  126. package/dist/sse/shared/serializers/index.d.ts +2 -1
  127. package/dist/sse/shared/serializers/responses-event-serializer.d.ts +123 -0
  128. package/dist/sse/shared/serializers/types.d.ts +51 -0
  129. package/dist/sse/shared/utils.d.ts +254 -0
  130. package/dist/sse/shared/writer.d.ts +2 -2
  131. package/dist/sse/sse-to-json/anthropic-sse-to-json-converter.d.ts +1 -1
  132. package/dist/sse/sse-to-json/builders/anthropic-response-builder.d.ts +1 -1
  133. package/dist/sse/sse-to-json/builders/response-builder.d.ts +1 -1
  134. package/dist/sse/sse-to-json/chat-sse-to-json-converter.d.ts +2 -1
  135. package/dist/sse/sse-to-json/gemini-sse-to-json-converter.d.ts +2 -1
  136. package/dist/sse/sse-to-json/parsers/sse-parser.d.ts +73 -0
  137. package/dist/sse/sse-to-json/responses-sse-to-json-converter.d.ts +1 -1
  138. package/dist/sse/types/chat-types.d.ts +1 -1
  139. package/dist/sse/types/responses-types.d.ts +1 -1
  140. package/dist/tools/apply-patch/execution-capturer.d.ts +13 -0
  141. package/dist/tools/apply-patch/execution-capturer.js +158 -0
  142. package/dist/tools/apply-patch/regression-capturer.d.ts +1 -0
  143. package/dist/tools/apply-patch/regression-capturer.js +5 -4
  144. package/dist/tools/apply-patch/structured.js +109 -13
  145. package/dist/tools/apply-patch/validator.js +112 -18
  146. package/dist/tools/tool-registry.d.ts +8 -0
  147. package/dist/tools/tool-registry.js +2 -1
  148. package/package.json +4 -4
  149. package/dist/conversion/compat/actions/apply-patch-format-fixer.js +0 -233
  150. package/dist/conversion/config/compat-profiles.json +0 -38
  151. package/dist/conversion/hub/pipeline/context-limit.d.ts +0 -13
  152. package/dist/conversion/hub/pipeline/context-limit.js +0 -55
  153. package/dist/conversion/hub/response/server-side-tools.d.ts +0 -26
  154. package/dist/conversion/hub/response/server-side-tools.js +0 -383
  155. package/dist/conversion/shared/bridge-conversation-store.d.ts +0 -41
  156. package/dist/conversion/shared/bridge-conversation-store.js +0 -279
  157. package/dist/conversion/shared/bridge-request-adapter.d.ts +0 -28
  158. package/dist/conversion/shared/bridge-request-adapter.js +0 -430
  159. package/dist/conversion/shared/responses-id-utils.js +0 -42
  160. package/dist/conversion/shared/responses-instructions.js +0 -113
  161. package/dist/conversion/shared/responses-message-utils.d.ts +0 -15
  162. package/dist/conversion/shared/responses-message-utils.js +0 -206
  163. package/dist/conversion/shared/responses-metadata.js +0 -1
  164. package/dist/conversion/shared/responses-output-utils.d.ts +0 -7
  165. package/dist/conversion/shared/responses-output-utils.js +0 -108
  166. package/dist/conversion/shared/responses-types.d.ts +0 -33
  167. package/dist/conversion/shared/tool-normalizers.d.ts +0 -4
  168. package/dist/conversion/shared/tool-normalizers.js +0 -84
  169. package/dist/filters/special/request-streaming-to-nonstreaming.d.ts +0 -13
  170. package/dist/filters/special/request-streaming-to-nonstreaming.js +0 -39
  171. package/dist/filters/special/response-apply-patch-toon-decode.d.ts +0 -23
  172. package/dist/filters/special/response-apply-patch-toon-decode.js +0 -460
  173. package/dist/filters/special/response-tool-arguments-toon-decode.d.ts +0 -10
  174. package/dist/filters/special/response-tool-arguments-toon-decode.js +0 -154
  175. package/dist/servertool/flow-types.d.ts +0 -40
  176. package/dist/servertool/flow-types.js +0 -1
  177. package/dist/servertool/orchestration-types.d.ts +0 -33
  178. package/dist/servertool/orchestration-types.js +0 -1
  179. package/dist/servertool/vision-tool.d.ts +0 -2
  180. package/dist/servertool/vision-tool.js +0 -185
  181. package/dist/tools/patch-args-normalizer.d.ts +0 -15
  182. package/dist/tools/patch-args-normalizer.js +0 -472
  183. package/dist/utils/toon.d.ts +0 -4
  184. package/dist/utils/toon.js +0 -75
  185. /package/dist/{conversion/compat/actions/apply-patch-format-fixer.d.ts → servertool/handlers/apply-patch-guard.d.ts} +0 -0
  186. /package/dist/{conversion/shared/responses-types.js → servertool/handlers/exec-command-guard.d.ts} +0 -0
@@ -1,206 +0,0 @@
1
- import { normalizeFunctionCallId, normalizeFunctionCallOutputId } from './responses-id-utils.js';
2
- export function normalizeResponseRole(role) {
3
- if (typeof role === 'string') {
4
- const normalized = role.toLowerCase();
5
- if (normalized === 'system' || normalized === 'assistant' || normalized === 'user' || normalized === 'tool') {
6
- return normalized;
7
- }
8
- }
9
- return 'user';
10
- }
11
- export function normalizeArgumentsBySchema(argsStringOrObj, _functionName, _tools) {
12
- if (typeof argsStringOrObj === 'string')
13
- return argsStringOrObj;
14
- try {
15
- return JSON.stringify(argsStringOrObj ?? {});
16
- }
17
- catch {
18
- return String(argsStringOrObj);
19
- }
20
- }
21
- export function normalizeToolOutput(entry) {
22
- const out = entry?.output;
23
- if (typeof out === 'string')
24
- return out;
25
- if (out && typeof out === 'object') {
26
- try {
27
- return JSON.stringify(out);
28
- }
29
- catch {
30
- return String(out);
31
- }
32
- }
33
- return null;
34
- }
35
- function collectText(value) {
36
- if (!value)
37
- return '';
38
- if (typeof value === 'string')
39
- return value;
40
- if (Array.isArray(value)) {
41
- return value.map(part => collectText(part)).join('');
42
- }
43
- if (typeof value === 'object') {
44
- const record = value;
45
- if (typeof record.text === 'string')
46
- return record.text;
47
- if (Array.isArray(record.content))
48
- return collectText(record.content);
49
- if (typeof record.content === 'string')
50
- return record.content;
51
- }
52
- return '';
53
- }
54
- function extractUserTextFromEntry(entry) {
55
- if (!entry || typeof entry !== 'object')
56
- return '';
57
- const directContent = entry.content ?? entry.message?.content;
58
- if (typeof directContent === 'string')
59
- return directContent.trim();
60
- if (Array.isArray(directContent)) {
61
- return directContent.map(block => collectText(block)).join('').trim();
62
- }
63
- const text = entry.text ?? entry.message?.text;
64
- if (typeof text === 'string')
65
- return text.trim();
66
- return '';
67
- }
68
- export function buildResponsesInputFromChatMessages(options) {
69
- const { messages, tools } = options;
70
- const input = [];
71
- const systemParts = [];
72
- const originalSystemMessages = [];
73
- let latestUserInstruction = null;
74
- const pendingToolCallIds = [];
75
- const knownToolCallIds = new Set();
76
- const toolCallIdAliases = new Map();
77
- for (const m of messages) {
78
- if (!m || typeof m !== 'object')
79
- continue;
80
- const role = normalizeResponseRole(m.role || 'user');
81
- const content = m.content;
82
- const collectedText = collectText(content);
83
- const text = role === 'system' ? collectedText : collectedText.trim();
84
- if (role === 'system') {
85
- if (collectedText && collectedText.length) {
86
- originalSystemMessages.push(collectedText);
87
- systemParts.push(collectedText);
88
- }
89
- continue;
90
- }
91
- if (role === 'tool') {
92
- const rawToolId = m.tool_call_id ||
93
- m.call_id ||
94
- m.tool_use_id ||
95
- m.id ||
96
- undefined;
97
- let callId = typeof rawToolId === 'string' ? rawToolId : undefined;
98
- if (callId && toolCallIdAliases.has(callId)) {
99
- callId = toolCallIdAliases.get(callId);
100
- }
101
- if (!callId || !knownToolCallIds.has(callId)) {
102
- const fallbackId = pendingToolCallIds.length ? pendingToolCallIds.shift() : undefined;
103
- if (fallbackId) {
104
- if (callId && callId !== fallbackId) {
105
- toolCallIdAliases.set(callId, fallbackId);
106
- }
107
- callId = fallbackId;
108
- }
109
- }
110
- else if (callId) {
111
- const idx = pendingToolCallIds.indexOf(callId);
112
- if (idx >= 0)
113
- pendingToolCallIds.splice(idx, 1);
114
- }
115
- const normalizedCallId = normalizeFunctionCallId({
116
- callId,
117
- fallback: `fc_call_${input.length + 1}`
118
- });
119
- if (callId && callId !== normalizedCallId) {
120
- toolCallIdAliases.set(callId, normalizedCallId);
121
- }
122
- const normalizedId = normalizeFunctionCallOutputId({
123
- callId: normalizedCallId,
124
- fallback: `fc_tool_${input.length + 1}`
125
- });
126
- const entry = {
127
- type: 'function_call_output',
128
- id: normalizedId,
129
- call_id: normalizedCallId,
130
- output: text || undefined
131
- };
132
- input.push(entry);
133
- continue;
134
- }
135
- const toolCalls = Array.isArray(m.tool_calls) ? m.tool_calls : [];
136
- if (toolCalls.length) {
137
- for (const tc of toolCalls) {
138
- try {
139
- const rawId = typeof tc?.id === 'string' && tc.id
140
- ? String(tc.id)
141
- : `call_${Math.random().toString(36).slice(2, 8)}`;
142
- const normalizedCallId = normalizeFunctionCallId({
143
- callId: rawId,
144
- fallback: `fc_call_${input.length + 1}`
145
- });
146
- if (rawId !== normalizedCallId) {
147
- toolCallIdAliases.set(rawId, normalizedCallId);
148
- }
149
- const fn = tc?.function || {};
150
- const name = typeof fn?.name === 'string' ? String(fn.name) : 'tool';
151
- const argsRaw = fn?.arguments;
152
- const args = typeof argsRaw === 'string'
153
- ? argsRaw
154
- : normalizeArgumentsBySchema(argsRaw, name, tools);
155
- const entry = {
156
- type: 'function_call',
157
- id: normalizedCallId,
158
- call_id: normalizedCallId,
159
- name,
160
- arguments: args
161
- };
162
- input.push(entry);
163
- knownToolCallIds.add(normalizedCallId);
164
- pendingToolCallIds.push(normalizedCallId);
165
- }
166
- catch {
167
- // ignore malformed tool_call
168
- }
169
- }
170
- continue;
171
- }
172
- if (text) {
173
- const tRole = role === 'assistant' ? 'output_text' : 'input_text';
174
- const entry = {
175
- type: 'message',
176
- role,
177
- content: [{ type: tRole, text }]
178
- };
179
- input.push(entry);
180
- if (role === 'user') {
181
- latestUserInstruction = text;
182
- }
183
- }
184
- }
185
- if (!latestUserInstruction) {
186
- for (let i = input.length - 1; i >= 0; i -= 1) {
187
- const entry = input[i];
188
- const roleSource = entry?.role ?? entry?.message?.role;
189
- const role = typeof roleSource === 'string' ? roleSource.toLowerCase() : '';
190
- if (role !== 'user')
191
- continue;
192
- const text = extractUserTextFromEntry(entry);
193
- if (text) {
194
- latestUserInstruction = text;
195
- break;
196
- }
197
- }
198
- }
199
- const combinedSystemInstruction = systemParts.join('\n\n').trim();
200
- return {
201
- input,
202
- combinedSystemInstruction: combinedSystemInstruction.length ? combinedSystemInstruction : undefined,
203
- latestUserInstruction: latestUserInstruction || undefined,
204
- originalSystemMessages
205
- };
206
- }
@@ -1 +0,0 @@
1
- export const RESPONSES_RAW_SYSTEM_METADATA_KEY = '__rcc_raw_system';
@@ -1,7 +0,0 @@
1
- type UnknownRecord = Record<string, unknown>;
2
- export declare function extractResponsesOutputSegments(response: UnknownRecord | undefined): {
3
- textParts: string[];
4
- reasoningParts: string[];
5
- };
6
- export declare function normalizeResponsesContentPart(part: unknown, reasoningCollector: string[]): UnknownRecord | null;
7
- export {};
@@ -1,108 +0,0 @@
1
- import { extractReasoningSegments, sanitizeReasoningTaggedText } from './reasoning-utils.js';
2
- function collectMessageTexts(blocks, accumulator) {
3
- const pushText = (value) => {
4
- if (typeof value === 'string' && value.trim().length) {
5
- const cleaned = extractReasoningSegments(value, accumulator.reasoningParts);
6
- if (cleaned.length) {
7
- accumulator.textParts.push(cleaned);
8
- }
9
- }
10
- };
11
- if (typeof blocks === 'string') {
12
- pushText(blocks);
13
- return;
14
- }
15
- if (!Array.isArray(blocks))
16
- return;
17
- for (const block of blocks) {
18
- if (!block || typeof block !== 'object')
19
- continue;
20
- const type = typeof block.type === 'string' ? String(block.type).toLowerCase() : '';
21
- if (type === 'text' || type === 'input_text' || type === 'output_text' || type === 'commentary') {
22
- pushText(block.text ?? block.content);
23
- continue;
24
- }
25
- if (Array.isArray(block.content)) {
26
- collectMessageTexts(block.content, accumulator);
27
- continue;
28
- }
29
- if (typeof block.text === 'string') {
30
- pushText(block.text);
31
- }
32
- }
33
- }
34
- export function extractResponsesOutputSegments(response) {
35
- if (!response || typeof response !== 'object') {
36
- return { textParts: [], reasoningParts: [] };
37
- }
38
- const textParts = [];
39
- const reasoningParts = [];
40
- const outputItems = Array.isArray(response.output) ? response.output : [];
41
- for (const item of outputItems) {
42
- if (!item || typeof item !== 'object')
43
- continue;
44
- const type = typeof item.type === 'string' ? String(item.type).toLowerCase() : '';
45
- if (type === 'message') {
46
- const message = item.message && typeof item.message === 'object'
47
- ? item.message
48
- : item;
49
- const content = Array.isArray(message.content) ? message.content : [];
50
- collectMessageTexts(content, { textParts, reasoningParts });
51
- continue;
52
- }
53
- if (type === 'output_text') {
54
- if (typeof item.text === 'string') {
55
- const cleaned = extractReasoningSegments(item.text, reasoningParts);
56
- if (cleaned.length) {
57
- textParts.push(cleaned);
58
- }
59
- }
60
- continue;
61
- }
62
- if (type === 'reasoning') {
63
- const content = Array.isArray(item.content) ? item.content : [];
64
- for (const block of content) {
65
- if (block && typeof block === 'object' && typeof block.text === 'string') {
66
- const sanitized = sanitizeReasoningTaggedText(block.text);
67
- if (sanitized.length)
68
- reasoningParts.push(sanitized);
69
- }
70
- }
71
- }
72
- }
73
- return { textParts, reasoningParts };
74
- }
75
- export function normalizeResponsesContentPart(part, reasoningCollector) {
76
- if (part == null) {
77
- return null;
78
- }
79
- if (typeof part === 'string') {
80
- const cleaned = extractReasoningSegments(part, reasoningCollector);
81
- return { type: 'output_text', text: cleaned };
82
- }
83
- if (typeof part !== 'object') {
84
- return { type: 'output_text', text: sanitizeReasoningTaggedText(String(part)) };
85
- }
86
- const clone = { ...part };
87
- delete clone._initialText;
88
- delete clone._hasDelta;
89
- if (typeof clone.text === 'string') {
90
- clone.text = extractReasoningSegments(clone.text, reasoningCollector);
91
- }
92
- if (clone.output_text && typeof clone.output_text === 'object' && typeof clone.output_text.text === 'string') {
93
- clone.output_text = {
94
- ...clone.output_text,
95
- text: extractReasoningSegments(clone.output_text.text, reasoningCollector)
96
- };
97
- }
98
- if (typeof clone.content === 'string') {
99
- clone.content = extractReasoningSegments(clone.content, reasoningCollector);
100
- }
101
- if (!clone.type) {
102
- clone.type = 'output_text';
103
- }
104
- if (clone.type === 'output_text' && typeof clone.text !== 'string') {
105
- clone.text = '';
106
- }
107
- return clone;
108
- }
@@ -1,33 +0,0 @@
1
- export type ResponsesContentPart = {
2
- type: string;
3
- text?: string;
4
- content?: unknown;
5
- };
6
- export type ResponsesInputItem = {
7
- type: string;
8
- role?: string;
9
- content?: Array<ResponsesContentPart> | null;
10
- name?: string;
11
- arguments?: unknown;
12
- call_id?: string;
13
- output?: unknown;
14
- function?: {
15
- name?: string;
16
- arguments?: unknown;
17
- };
18
- message?: {
19
- role?: string;
20
- content?: Array<ResponsesContentPart>;
21
- };
22
- id?: string;
23
- tool_call_id?: string;
24
- tool_use_id?: string;
25
- text?: string;
26
- };
27
- export type ResponsesToolDefinition = {
28
- type: string;
29
- name?: string;
30
- description?: string;
31
- strict?: boolean;
32
- parameters?: unknown;
33
- };
@@ -1,4 +0,0 @@
1
- import type { ChatToolDefinition, MissingField } from '../hub/types/chat-envelope.js';
2
- import type { JsonValue } from '../hub/types/json.js';
3
- export declare function normalizeAnthropicTools(rawTools: JsonValue | undefined, missing?: MissingField[]): ChatToolDefinition[] | undefined;
4
- export declare function normalizeGeminiTools(rawTools: JsonValue | undefined, missing?: MissingField[]): ChatToolDefinition[] | undefined;
@@ -1,84 +0,0 @@
1
- import { isJsonObject, jsonClone } from '../hub/types/json.js';
2
- export function normalizeAnthropicTools(rawTools, missing) {
3
- if (!Array.isArray(rawTools) || rawTools.length === 0)
4
- return undefined;
5
- const tools = [];
6
- rawTools.forEach((entry, index) => {
7
- if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
8
- missing?.push({
9
- path: `tools[${index}]`,
10
- reason: 'invalid_entry',
11
- originalValue: jsonClone(entry)
12
- });
13
- return;
14
- }
15
- const nameValue = entry.name;
16
- if (typeof nameValue !== 'string' || !nameValue.trim())
17
- return;
18
- const desc = entry.description;
19
- const schema = entry.input_schema;
20
- const parameters = schema && typeof schema === 'object' && !Array.isArray(schema)
21
- ? schema
22
- : { type: 'object', properties: {} };
23
- tools.push({
24
- type: 'function',
25
- function: {
26
- name: nameValue,
27
- description: typeof desc === 'string' ? desc : undefined,
28
- parameters
29
- }
30
- });
31
- });
32
- return tools.length ? tools : undefined;
33
- }
34
- export function normalizeGeminiTools(rawTools, missing) {
35
- if (!rawTools)
36
- return undefined;
37
- const arr = Array.isArray(rawTools) ? rawTools : [rawTools];
38
- const tools = [];
39
- arr.forEach((entry, index) => {
40
- if (!entry || typeof entry !== 'object') {
41
- missing?.push({
42
- path: `tools[${index}]`,
43
- reason: 'invalid_entry',
44
- originalValue: jsonClone(entry)
45
- });
46
- return;
47
- }
48
- const obj = entry;
49
- const declarations = Array.isArray(obj.functionDeclarations) ? obj.functionDeclarations : undefined;
50
- if (declarations && declarations.length) {
51
- declarations.forEach((decl, declIndex) => {
52
- if (!decl || typeof decl !== 'object') {
53
- missing?.push({ path: `tools[${index}].functionDeclarations[${declIndex}]`, reason: 'invalid_entry' });
54
- return;
55
- }
56
- const def = decl;
57
- const name = typeof def.name === 'string' ? def.name : undefined;
58
- if (!name)
59
- return;
60
- tools.push({
61
- type: 'function',
62
- function: {
63
- name,
64
- description: typeof def.description === 'string' ? def.description : undefined,
65
- parameters: isJsonObject(def.parameters) ? jsonClone(def.parameters) : { type: 'object', properties: {} }
66
- }
67
- });
68
- });
69
- return;
70
- }
71
- const name = typeof obj.name === 'string' ? obj.name : undefined;
72
- if (!name)
73
- return;
74
- tools.push({
75
- type: 'function',
76
- function: {
77
- name,
78
- description: typeof obj.description === 'string' ? obj.description : undefined,
79
- parameters: isJsonObject(obj.parameters) ? jsonClone(obj.parameters) : { type: 'object', properties: {} }
80
- }
81
- });
82
- });
83
- return tools.length ? tools : undefined;
84
- }
@@ -1,13 +0,0 @@
1
- import type { Filter, FilterContext, FilterResult, JsonObject } from '../types.js';
2
- /**
3
- * RequestStreamingToNonStreamingFilter (request_post)
4
- * - Unifies provider requests to non-streaming regardless of inbound stream flag.
5
- * - Preserves original intent via `originalStream` and `_originalStreamOptions`,
6
- * but ensures `stream: false` is sent to providers.
7
- */
8
- export declare class RequestStreamingToNonStreamingFilter implements Filter<JsonObject> {
9
- readonly name = "request_streaming_to_nonstreaming";
10
- readonly stage: FilterContext['stage'];
11
- apply(input: JsonObject, ctx: FilterContext): FilterResult<JsonObject>;
12
- private shouldPreserveStream;
13
- }
@@ -1,39 +0,0 @@
1
- /**
2
- * RequestStreamingToNonStreamingFilter (request_post)
3
- * - Unifies provider requests to non-streaming regardless of inbound stream flag.
4
- * - Preserves original intent via `originalStream` and `_originalStreamOptions`,
5
- * but ensures `stream: false` is sent to providers.
6
- */
7
- export class RequestStreamingToNonStreamingFilter {
8
- name = 'request_streaming_to_nonstreaming';
9
- stage = 'request_post';
10
- apply(input, ctx) {
11
- try {
12
- const out = JSON.parse(JSON.stringify(input || {}));
13
- if (this.shouldPreserveStream(out, ctx)) {
14
- return { ok: true, data: out };
15
- }
16
- if (out.stream === true) {
17
- out.originalStream = true;
18
- out.stream = false;
19
- }
20
- if (out.stream_options != null) {
21
- out._originalStreamOptions = out.stream_options;
22
- delete out.stream_options;
23
- }
24
- return { ok: true, data: out };
25
- }
26
- catch {
27
- return { ok: true, data: input };
28
- }
29
- }
30
- shouldPreserveStream(payload, ctx) {
31
- if (ctx.stream === true) {
32
- return true;
33
- }
34
- if (payload.stream === true) {
35
- return true;
36
- }
37
- return false;
38
- }
39
- }
@@ -1,23 +0,0 @@
1
- import type { Filter, FilterContext, FilterResult, JsonObject } from '../types.js';
2
- /**
3
- * Response-side apply_patch arguments 规范化(TOON + 结构化 JSON → 统一 diff 文本)。
4
- *
5
- * 目标:
6
- * - 对上游模型:仍然可以使用结构化 JSON(changes 数组)或 toon 字段输出补丁;
7
- * - 对下游客户端(Codex CLI 等):始终看到 { input, patch } 形式的统一 diff 文本,
8
- * 与本地 apply_patch 工具的旧语义完全一致(对客户端透明)。
9
- *
10
- * 支持两种入参形态(arguments 反序列化后):
11
- * 1) { toon: "<*** Begin Patch ... *** End Patch>" }
12
- * 2) StructuredApplyPatchPayload(含 changes 数组等字段)
13
- *
14
- * 输出:
15
- * - arguments 统一替换为 JSON 字符串:{ input: patchText, patch: patchText }。
16
- *
17
- * Stage: response_pre(在 arguments stringify 之前运行)。
18
- */
19
- export declare class ResponseApplyPatchToonDecodeFilter implements Filter<JsonObject> {
20
- readonly name = "response_apply_patch_toon_decode";
21
- readonly stage: FilterContext['stage'];
22
- apply(input: JsonObject): FilterResult<JsonObject>;
23
- }