responses-proxy 0.1.0

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 (161) hide show
  1. package/README.md +56 -0
  2. package/cli.js +118 -0
  3. package/dist/anthropic-messages.js +383 -0
  4. package/dist/anthropic-messages.test.js +209 -0
  5. package/dist/audit-log.js +138 -0
  6. package/dist/audit-log.test.js +480 -0
  7. package/dist/billing-expiration.js +70 -0
  8. package/dist/billing-expiration.test.js +114 -0
  9. package/dist/billing.js +716 -0
  10. package/dist/billing.test.js +228 -0
  11. package/dist/chatgpt-oauth-store.js +240 -0
  12. package/dist/chatgpt-oauth-store.test.js +88 -0
  13. package/dist/chatgpt-oauth.js +118 -0
  14. package/dist/chatgpt-oauth.test.js +63 -0
  15. package/dist/chatgpt-provider-auth.js +60 -0
  16. package/dist/chatgpt-provider-auth.test.js +101 -0
  17. package/dist/client/app-icon.svg +17 -0
  18. package/dist/client/assets/index-C7Vvhst8.js +14 -0
  19. package/dist/client/assets/index-DpqgYK3L.css +1 -0
  20. package/dist/client/favicon.svg +17 -0
  21. package/dist/client/index.html +31 -0
  22. package/dist/client-config-apply.js +345 -0
  23. package/dist/client-config-apply.test.js +185 -0
  24. package/dist/client-token-limits.js +111 -0
  25. package/dist/client-token-limits.test.js +129 -0
  26. package/dist/codex-config.js +47 -0
  27. package/dist/codex-setup.js +87 -0
  28. package/dist/codex-setup.test.js +30 -0
  29. package/dist/config.js +314 -0
  30. package/dist/cost-analytics.js +31 -0
  31. package/dist/cost-analytics.test.js +38 -0
  32. package/dist/customer-key-access.js +126 -0
  33. package/dist/customer-key-access.test.js +178 -0
  34. package/dist/customer-keys.js +209 -0
  35. package/dist/customer-keys.test.js +68 -0
  36. package/dist/customer-usage.js +18 -0
  37. package/dist/customer-usage.test.js +55 -0
  38. package/dist/dashboard-auth.js +318 -0
  39. package/dist/dashboard-auth.test.js +133 -0
  40. package/dist/dashboard-serving.test.js +235 -0
  41. package/dist/error-response.js +174 -0
  42. package/dist/error-response.test.js +88 -0
  43. package/dist/forward.js +357 -0
  44. package/dist/health-websocket-manager.js +174 -0
  45. package/dist/http-rate-limit.js +36 -0
  46. package/dist/http-rate-limit.test.js +62 -0
  47. package/dist/kiro-auth.js +136 -0
  48. package/dist/kiro-auth.test.js +234 -0
  49. package/dist/kiro-codewhisperer.js +646 -0
  50. package/dist/kiro-codewhisperer.test.js +219 -0
  51. package/dist/kiro-device-login.js +338 -0
  52. package/dist/kiro-eventstream.js +219 -0
  53. package/dist/kiro-eventstream.test.js +79 -0
  54. package/dist/kiro-forward.js +401 -0
  55. package/dist/kiro-import-cli.js +69 -0
  56. package/dist/kiro-import.js +94 -0
  57. package/dist/kiro-import.test.js +125 -0
  58. package/dist/kiro-token-store.js +196 -0
  59. package/dist/kiro-token-store.test.js +207 -0
  60. package/dist/krouter-usage.js +243 -0
  61. package/dist/model-combo-repository.js +147 -0
  62. package/dist/model-routing.js +69 -0
  63. package/dist/model-routing.test.js +41 -0
  64. package/dist/normalize-request.js +531 -0
  65. package/dist/normalize-request.test.js +277 -0
  66. package/dist/omv-public-firewall.test.js +11 -0
  67. package/dist/package.json +17 -0
  68. package/dist/prompt-cache-state.js +146 -0
  69. package/dist/prompt-cache-state.test.js +71 -0
  70. package/dist/prompt-cache.js +229 -0
  71. package/dist/provider-health-service.js +404 -0
  72. package/dist/provider-request-parameters.js +107 -0
  73. package/dist/provider-request-parameters.test.js +26 -0
  74. package/dist/provider-routing.js +114 -0
  75. package/dist/provider-routing.test.js +64 -0
  76. package/dist/provider-usage.js +314 -0
  77. package/dist/request-timeout-policy.js +61 -0
  78. package/dist/request-timeout-policy.test.js +40 -0
  79. package/dist/response-cache.js +69 -0
  80. package/dist/response-cache.test.js +28 -0
  81. package/dist/routing-combo-repository.js +300 -0
  82. package/dist/routing-engine.js +377 -0
  83. package/dist/routing-integration.js +155 -0
  84. package/dist/routing-simulation-engine.js +326 -0
  85. package/dist/rtk-layer.js +483 -0
  86. package/dist/rtk-layer.test.js +198 -0
  87. package/dist/runtime-provider-repository.js +1742 -0
  88. package/dist/runtime-provider-repository.test.js +1177 -0
  89. package/dist/schema.js +118 -0
  90. package/dist/schema.test.js +16 -0
  91. package/dist/sepay-webhook.js +87 -0
  92. package/dist/sepay-webhook.test.js +142 -0
  93. package/dist/server-body-limit.test.js +35 -0
  94. package/dist/server-client-token-limits.test.js +161 -0
  95. package/dist/server-codex-config-setup.test.js +76 -0
  96. package/dist/server-http-rate-limit.test.js +80 -0
  97. package/dist/server-response-cache.test.js +105 -0
  98. package/dist/server-routes-alias.test.js +39 -0
  99. package/dist/server-sepay-webhook-security.test.js +59 -0
  100. package/dist/server.js +5906 -0
  101. package/dist/session-log.js +178 -0
  102. package/dist/tailnet-funnel-script.test.js +33 -0
  103. package/dist/telegram-bot/actions.js +118 -0
  104. package/dist/telegram-bot/admin-actions.js +103 -0
  105. package/dist/telegram-bot/auth.js +46 -0
  106. package/dist/telegram-bot/auth.test.js +1 -0
  107. package/dist/telegram-bot/bot-identity-repository.js +189 -0
  108. package/dist/telegram-bot/bot-identity-repository.test.js +78 -0
  109. package/dist/telegram-bot/callbacks.js +30 -0
  110. package/dist/telegram-bot/codex-config-delivery.js +38 -0
  111. package/dist/telegram-bot/codex-config-delivery.test.js +75 -0
  112. package/dist/telegram-bot/commands/accounts.js +140 -0
  113. package/dist/telegram-bot/commands/apikey.js +737 -0
  114. package/dist/telegram-bot/commands/apply.js +265 -0
  115. package/dist/telegram-bot/commands/clients.js +13 -0
  116. package/dist/telegram-bot/commands/customer-billing.test.js +271 -0
  117. package/dist/telegram-bot/commands/grant.js +138 -0
  118. package/dist/telegram-bot/commands/grant.test.js +217 -0
  119. package/dist/telegram-bot/commands/help.js +52 -0
  120. package/dist/telegram-bot/commands/me.js +53 -0
  121. package/dist/telegram-bot/commands/models.js +6 -0
  122. package/dist/telegram-bot/commands/oauth.js +64 -0
  123. package/dist/telegram-bot/commands/plans.js +96 -0
  124. package/dist/telegram-bot/commands/providers.js +27 -0
  125. package/dist/telegram-bot/commands/quota.js +10 -0
  126. package/dist/telegram-bot/commands/renew-user.js +139 -0
  127. package/dist/telegram-bot/commands/renew-user.test.js +184 -0
  128. package/dist/telegram-bot/commands/renew.js +1369 -0
  129. package/dist/telegram-bot/commands/renew.test.js +1633 -0
  130. package/dist/telegram-bot/commands/start.js +212 -0
  131. package/dist/telegram-bot/commands/start.test.js +280 -0
  132. package/dist/telegram-bot/commands/status.js +6 -0
  133. package/dist/telegram-bot/commands/tailscale.js +15 -0
  134. package/dist/telegram-bot/commands/tailscale.test.js +76 -0
  135. package/dist/telegram-bot/commands/test.js +51 -0
  136. package/dist/telegram-bot/commands/test.test.js +14 -0
  137. package/dist/telegram-bot/commands/usage.js +10 -0
  138. package/dist/telegram-bot/config.js +98 -0
  139. package/dist/telegram-bot/config.test.js +42 -0
  140. package/dist/telegram-bot/customer-actions.js +160 -0
  141. package/dist/telegram-bot/customer-api-keys.js +68 -0
  142. package/dist/telegram-bot/customer-billing.js +72 -0
  143. package/dist/telegram-bot/customer-workspace-repository.js +134 -0
  144. package/dist/telegram-bot/customer-workspace-repository.test.js +47 -0
  145. package/dist/telegram-bot/dashboard-login.js +39 -0
  146. package/dist/telegram-bot/format.js +140 -0
  147. package/dist/telegram-bot/grants.js +370 -0
  148. package/dist/telegram-bot/grants.test.js +290 -0
  149. package/dist/telegram-bot/index.js +85 -0
  150. package/dist/telegram-bot/message-cleanup.js +55 -0
  151. package/dist/telegram-bot/message-cleanup.test.js +77 -0
  152. package/dist/telegram-bot/message-format.js +45 -0
  153. package/dist/telegram-bot/message-format.test.js +10 -0
  154. package/dist/telegram-bot/proxy-client.js +174 -0
  155. package/dist/telegram-bot/rate-limit.js +95 -0
  156. package/dist/telegram-bot/rate-limit.test.js +58 -0
  157. package/dist/telegram-bot/sessions.js +171 -0
  158. package/dist/telegram-bot/sessions.test.js +107 -0
  159. package/dist/telegram-bot/telegram-adapter.js +126 -0
  160. package/dist/telegram-bot/worker.js +63 -0
  161. package/package.json +39 -0
@@ -0,0 +1,531 @@
1
+ import { createHash } from "node:crypto";
2
+ import { buildPromptCacheLayout } from "./prompt-cache.js";
3
+ function normalizeInstructions(instructions) {
4
+ const trimmed = instructions
5
+ ?.replace(/\r\n/g, "\n")
6
+ .replace(/[ \t]+\n/g, "\n")
7
+ .replace(/\n{2,}/g, "\n")
8
+ .trim();
9
+ return trimmed ? trimmed : undefined;
10
+ }
11
+ function normalizeFunctionTool(tool) {
12
+ if (tool.type !== "function") {
13
+ return sortObjectKeys(tool);
14
+ }
15
+ const nested = isRecord(tool.function) ? tool.function : null;
16
+ const name = typeof tool.name === "string" ? tool.name : nested?.name;
17
+ if (typeof name !== "string" || !name.trim()) {
18
+ return tool;
19
+ }
20
+ const normalized = {
21
+ type: "function",
22
+ name: name.trim(),
23
+ };
24
+ const description = typeof tool.description === "string"
25
+ ? tool.description
26
+ : typeof nested?.description === "string"
27
+ ? nested.description
28
+ : undefined;
29
+ if (description) {
30
+ normalized.description = description;
31
+ }
32
+ const parameters = isRecord(tool.parameters)
33
+ ? tool.parameters
34
+ : isRecord(nested?.parameters)
35
+ ? nested.parameters
36
+ : undefined;
37
+ if (parameters) {
38
+ normalized.parameters = sortObjectKeys(parameters);
39
+ }
40
+ const strict = typeof tool.strict === "boolean"
41
+ ? tool.strict
42
+ : typeof nested?.strict === "boolean"
43
+ ? nested.strict
44
+ : undefined;
45
+ if (typeof strict === "boolean") {
46
+ normalized.strict = strict;
47
+ }
48
+ return sortObjectKeys(normalized);
49
+ }
50
+ function isRecord(value) {
51
+ return typeof value === "object" && value !== null && !Array.isArray(value);
52
+ }
53
+ function stringOrUndefined(value) {
54
+ return typeof value === "string" ? value : undefined;
55
+ }
56
+ function convertChatPart(part, role) {
57
+ if (part.type === "text" && typeof part.text === "string") {
58
+ return {
59
+ type: role === "assistant" ? "output_text" : "input_text",
60
+ text: part.text,
61
+ };
62
+ }
63
+ if (role === "user" && part.type === "image_url") {
64
+ const imageUrlValue = part.image_url;
65
+ const imageUrl = typeof imageUrlValue === "string"
66
+ ? imageUrlValue
67
+ : isRecord(imageUrlValue) && typeof imageUrlValue.url === "string"
68
+ ? imageUrlValue.url
69
+ : undefined;
70
+ if (imageUrl) {
71
+ return {
72
+ type: "input_image",
73
+ image_url: imageUrl,
74
+ };
75
+ }
76
+ }
77
+ return null;
78
+ }
79
+ function convertMessageContent(role, rawContent) {
80
+ if (typeof rawContent === "string") {
81
+ return rawContent;
82
+ }
83
+ if (!Array.isArray(rawContent)) {
84
+ return null;
85
+ }
86
+ const parts = rawContent
87
+ .map((part) => (isRecord(part) ? convertChatPart(part, role) : null))
88
+ .filter((part) => part !== null);
89
+ if (parts.length === 0) {
90
+ return null;
91
+ }
92
+ return parts;
93
+ }
94
+ function mergeInstructions(parts) {
95
+ const seen = new Set();
96
+ const normalized = parts
97
+ .map((part) => normalizeInstructions(part))
98
+ .filter((part) => typeof part === "string" && part.length > 0)
99
+ .filter((part) => {
100
+ if (seen.has(part)) {
101
+ return false;
102
+ }
103
+ seen.add(part);
104
+ return true;
105
+ })
106
+ .join("\n\n");
107
+ return normalized || undefined;
108
+ }
109
+ function convertMessagesToInput(body) {
110
+ const messages = body.messages ?? [];
111
+ const instructionParts = [];
112
+ const input = [];
113
+ for (const message of messages) {
114
+ if (message.role === "system" || message.role === "developer") {
115
+ if (typeof message.content === "string" && message.content.trim()) {
116
+ instructionParts.push(message.content);
117
+ }
118
+ else if (Array.isArray(message.content)) {
119
+ const content = convertMessageContent("user", message.content);
120
+ if (Array.isArray(content)) {
121
+ const text = content
122
+ .filter((part) => part.type === "input_text" && typeof part.text === "string")
123
+ .map((part) => part.text)
124
+ .join("\n");
125
+ if (text.trim()) {
126
+ instructionParts.push(text);
127
+ }
128
+ }
129
+ }
130
+ continue;
131
+ }
132
+ if (message.role === "user" || message.role === "assistant") {
133
+ const content = convertMessageContent(message.role, message.content);
134
+ if (content !== null) {
135
+ input.push({
136
+ role: message.role,
137
+ content,
138
+ });
139
+ }
140
+ if (message.role === "assistant" && Array.isArray(message.tool_calls)) {
141
+ for (const toolCall of message.tool_calls) {
142
+ const functionName = toolCall.function.name.trim();
143
+ if (!functionName) {
144
+ continue;
145
+ }
146
+ input.push({
147
+ type: "function_call",
148
+ call_id: toolCall.id ?? functionName,
149
+ name: functionName,
150
+ arguments: toolCall.function.arguments ?? "{}",
151
+ });
152
+ }
153
+ }
154
+ continue;
155
+ }
156
+ if (message.role === "tool") {
157
+ const output = typeof message.content === "string"
158
+ ? message.content
159
+ : Array.isArray(message.content)
160
+ ? message.content
161
+ .map((part) => isRecord(part) && part.type === "text" && typeof part.text === "string"
162
+ ? part.text
163
+ : "")
164
+ .filter(Boolean)
165
+ .join("\n")
166
+ : "";
167
+ if (message.tool_call_id && output) {
168
+ input.push({
169
+ type: "function_call_output",
170
+ call_id: message.tool_call_id,
171
+ output,
172
+ });
173
+ }
174
+ }
175
+ }
176
+ const directInstructions = normalizeInstructions(body.instructions);
177
+ if (directInstructions) {
178
+ instructionParts.push(directInstructions);
179
+ }
180
+ return {
181
+ input,
182
+ instructions: mergeInstructions(instructionParts),
183
+ };
184
+ }
185
+ function normalizeDirectInput(body) {
186
+ return {
187
+ input: normalizeInputValue(body.input),
188
+ instructions: normalizeInstructions(body.instructions),
189
+ };
190
+ }
191
+ export function normalizeResponsesRequest(body, options = {}) {
192
+ return normalizeResponsesRequestWithCache(body, options).request;
193
+ }
194
+ export function normalizeResponsesRequestWithCache(body, options = {}) {
195
+ const preserveMessagesPayload = options.preserveMessagesPayload === true && body.input === undefined && Array.isArray(body.messages);
196
+ const normalizedBase = body.input !== undefined
197
+ ? normalizeDirectInput(body)
198
+ : preserveMessagesPayload
199
+ ? {
200
+ input: normalizeInputValue(body.messages),
201
+ instructions: normalizeInstructions(body.instructions),
202
+ }
203
+ : convertMessagesToInput(body);
204
+ const isOpenClaw = options.openClawTokenOptimizationEnabled
205
+ ? isLikelyHermesPayload(body, normalizedBase.instructions)
206
+ : false;
207
+ const request = {
208
+ model: body.model,
209
+ input: normalizeInputValue(normalizedBase.input),
210
+ store: false,
211
+ stream: body.stream ?? false,
212
+ parallel_tool_calls: body.parallel_tool_calls ?? true,
213
+ };
214
+ const maxOutputTokensMode = options.maxOutputTokensPolicy?.mode ?? (options.stripMaxOutputTokens ? "strip" : "forward");
215
+ if (normalizedBase.instructions) {
216
+ request.instructions = normalizedBase.instructions;
217
+ }
218
+ if (body.tools?.length) {
219
+ request.tools = body.tools
220
+ .map((tool) => (isRecord(tool) ? normalizeFunctionTool(tool) : tool))
221
+ .sort(compareToolsForCacheStability);
222
+ }
223
+ if (body.tool_choice !== undefined) {
224
+ request.tool_choice = normalizeToolChoice(body.tool_choice);
225
+ }
226
+ if (body.reasoning != null) {
227
+ const reasoning = isOpenClaw
228
+ ? {
229
+ ...body.reasoning,
230
+ ...(body.reasoning.effort === undefined && options.defaultReasoningEffort
231
+ ? { effort: options.defaultReasoningEffort }
232
+ : {}),
233
+ ...(body.reasoning.summary === undefined && options.defaultReasoningSummary
234
+ ? { summary: options.defaultReasoningSummary }
235
+ : {}),
236
+ }
237
+ : body.reasoning;
238
+ request.reasoning = sanitizeReasoning(reasoning, options);
239
+ }
240
+ else if (isOpenClaw && options.defaultReasoningSummary) {
241
+ request.reasoning = sanitizeReasoning({
242
+ ...(options.defaultReasoningEffort ? { effort: options.defaultReasoningEffort } : {}),
243
+ summary: options.defaultReasoningSummary,
244
+ }, options);
245
+ }
246
+ if (body.text != null) {
247
+ request.text =
248
+ isOpenClaw && body.text.verbosity === undefined && options.defaultTextVerbosity
249
+ ? {
250
+ ...body.text,
251
+ verbosity: options.defaultTextVerbosity,
252
+ }
253
+ : body.text;
254
+ request.text = normalizeTextConfig(request.text);
255
+ }
256
+ else if (isOpenClaw && options.defaultTextVerbosity) {
257
+ request.text = {
258
+ verbosity: options.defaultTextVerbosity,
259
+ };
260
+ }
261
+ if (body.max_output_tokens !== undefined && maxOutputTokensMode !== "strip") {
262
+ request.max_output_tokens = body.max_output_tokens;
263
+ }
264
+ else if (isOpenClaw && options.defaultMaxOutputTokens !== undefined && maxOutputTokensMode !== "strip") {
265
+ request.max_output_tokens = options.defaultMaxOutputTokens;
266
+ }
267
+ if (body.max_tool_calls !== undefined) {
268
+ request.max_tool_calls = body.max_tool_calls;
269
+ }
270
+ if (body.temperature !== undefined) {
271
+ request.temperature = body.temperature;
272
+ }
273
+ if (body.top_p !== undefined) {
274
+ request.top_p = body.top_p;
275
+ }
276
+ if (body.metadata !== undefined) {
277
+ request.metadata = normalizeMetadata(body.metadata, {
278
+ stripVolatileKeys: isOpenClaw || options.promptCacheRedesignEnabled,
279
+ });
280
+ }
281
+ if (body.user !== undefined) {
282
+ request.user = body.user;
283
+ }
284
+ if (body.previous_response_id !== undefined) {
285
+ request.previous_response_id = body.previous_response_id;
286
+ }
287
+ if (body.truncation !== undefined) {
288
+ request.truncation = body.truncation;
289
+ }
290
+ else if (isOpenClaw && options.defaultTruncation) {
291
+ request.truncation = options.defaultTruncation;
292
+ }
293
+ if (body.include !== undefined) {
294
+ request.include = normalizeInclude(body.include);
295
+ }
296
+ const cacheLayout = buildPromptCacheLayout(request, {
297
+ enabled: options.promptCacheRedesignEnabled,
298
+ stableSummarizationEnabled: options.promptCacheStableSummarizationEnabled,
299
+ summaryTriggerItems: options.promptCacheSummaryTriggerItems,
300
+ summaryKeepRecentItems: options.promptCacheSummaryKeepRecentItems,
301
+ defaultRetention: options.defaultPromptCacheRetention,
302
+ retentionByFamilyEnabled: options.promptCacheRetentionByFamilyEnabled,
303
+ familyRetentionRules: options.promptCacheRetentionByFamilyRules,
304
+ retentionByStaticKeyEnabled: options.promptCacheRetentionByStaticKeyEnabled,
305
+ staticKeyRetentionRules: options.promptCacheRetentionByStaticKeyRules,
306
+ });
307
+ if (body.prompt_cache_key !== undefined) {
308
+ request.prompt_cache_key = body.prompt_cache_key;
309
+ }
310
+ else if (cacheLayout.promptCacheKey) {
311
+ request.prompt_cache_key = cacheLayout.promptCacheKey;
312
+ }
313
+ else if (isOpenClaw && options.autoPromptCacheKey) {
314
+ request.prompt_cache_key = buildOpenClawPromptCacheKey(body, request);
315
+ }
316
+ if (body.prompt_cache_retention !== undefined) {
317
+ request.prompt_cache_retention = body.prompt_cache_retention;
318
+ }
319
+ else if (cacheLayout.promptCacheRetention) {
320
+ request.prompt_cache_retention = cacheLayout.promptCacheRetention;
321
+ }
322
+ else if (request.prompt_cache_key !== undefined && options.defaultPromptCacheRetention) {
323
+ request.prompt_cache_retention = options.defaultPromptCacheRetention;
324
+ }
325
+ const finalCacheLayout = {
326
+ ...cacheLayout,
327
+ promptCacheKey: typeof request.prompt_cache_key === "string" ? request.prompt_cache_key : cacheLayout.promptCacheKey,
328
+ promptCacheRetention: typeof request.prompt_cache_retention === "string"
329
+ ? request.prompt_cache_retention
330
+ : cacheLayout.promptCacheRetention,
331
+ };
332
+ return {
333
+ request,
334
+ cacheLayout: finalCacheLayout,
335
+ };
336
+ }
337
+ function isLikelyHermesPayload(body, instructions) {
338
+ if (containsHermesMarker(instructions)) {
339
+ return true;
340
+ }
341
+ if (typeof body.input === "string" && containsHermesMarker(body.input)) {
342
+ return true;
343
+ }
344
+ if (Array.isArray(body.input)) {
345
+ for (const item of body.input) {
346
+ if (!isRecord(item)) {
347
+ continue;
348
+ }
349
+ const content = item.content;
350
+ if (typeof content === "string" && containsHermesMarker(content)) {
351
+ return true;
352
+ }
353
+ }
354
+ }
355
+ return Array.isArray(body.messages)
356
+ ? body.messages.some((message) => typeof message.content === "string" && containsHermesMarker(message.content))
357
+ : false;
358
+ }
359
+ function containsHermesMarker(value) {
360
+ if (typeof value !== "string") {
361
+ return false;
362
+ }
363
+ return value.includes("running inside Hermes") || value.includes("running inside OpenClaw");
364
+ }
365
+ function sanitizeReasoning(reasoning, options) {
366
+ const normalized = sortObjectKeys(reasoning);
367
+ if (!options.sanitizeReasoningSummary) {
368
+ return normalized;
369
+ }
370
+ if (normalized.summary !== "none") {
371
+ return normalized;
372
+ }
373
+ return {
374
+ ...normalized,
375
+ summary: "auto",
376
+ };
377
+ }
378
+ function buildOpenClawPromptCacheKey(body, normalizedRequest) {
379
+ const instructions = typeof normalizedRequest.instructions === "string" ? normalizedRequest.instructions : "";
380
+ const tools = Array.isArray(normalizedRequest.tools) ? normalizedRequest.tools : [];
381
+ const model = typeof body.model === "string" ? body.model : "unknown-model";
382
+ const toolSignature = JSON.stringify(tools.map((tool) => {
383
+ if (!isRecord(tool)) {
384
+ return tool;
385
+ }
386
+ return {
387
+ type: tool.type,
388
+ name: typeof tool.name === "string"
389
+ ? tool.name
390
+ : isRecord(tool.function) && typeof tool.function.name === "string"
391
+ ? tool.function.name
392
+ : undefined,
393
+ };
394
+ }));
395
+ const instructionsHash = shortHash(instructions);
396
+ const toolsHash = shortHash(toolSignature);
397
+ return `hermes:${model}:instr:${instructionsHash}:tools:${toolsHash}`;
398
+ }
399
+ function shortHash(value) {
400
+ return createHash("sha256").update(value).digest("hex").slice(0, 16);
401
+ }
402
+ function normalizeInputValue(input) {
403
+ if (typeof input === "string" || input === undefined) {
404
+ return input;
405
+ }
406
+ if (!Array.isArray(input)) {
407
+ return input;
408
+ }
409
+ return input.map((item) => normalizeInputItem(item));
410
+ }
411
+ function normalizeInputItem(item) {
412
+ if (!isRecord(item)) {
413
+ return item;
414
+ }
415
+ const normalized = sortObjectKeys(item);
416
+ if (normalized.role === "tool") {
417
+ const output = readToolContent(normalized.content);
418
+ if (typeof normalized.tool_call_id === "string" && normalized.tool_call_id.trim()) {
419
+ return {
420
+ type: "function_call_output",
421
+ call_id: normalized.tool_call_id.trim(),
422
+ output,
423
+ };
424
+ }
425
+ // Never let legacy `role: "tool"` leak upstream. If the call id is missing,
426
+ // downgrade the content into a plain assistant message instead of failing.
427
+ return {
428
+ role: "assistant",
429
+ content: output,
430
+ };
431
+ }
432
+ if (normalized.type === "function_call" && typeof normalized.arguments === "string") {
433
+ return {
434
+ ...normalized,
435
+ arguments: normalizeJsonString(normalized.arguments),
436
+ };
437
+ }
438
+ if (normalized.type === "function_call_output" && typeof normalized.output === "string") {
439
+ return {
440
+ ...normalized,
441
+ output: normalizeJsonString(normalized.output),
442
+ };
443
+ }
444
+ return normalized;
445
+ }
446
+ function readToolContent(content) {
447
+ if (typeof content === "string") {
448
+ return content;
449
+ }
450
+ if (!Array.isArray(content)) {
451
+ return "";
452
+ }
453
+ return content
454
+ .map((part) => isRecord(part) && typeof part.text === "string" ? part.text : "")
455
+ .filter(Boolean)
456
+ .join("\n");
457
+ }
458
+ function normalizeMetadata(metadata, options = {}) {
459
+ const volatileKeys = options.stripVolatileKeys ? OPENCLAW_VOLATILE_METADATA_KEYS : undefined;
460
+ const entries = Object.entries(metadata).filter(([key, value]) => key.trim().length > 0 &&
461
+ value.trim().length > 0 &&
462
+ !volatileKeys?.has(key.trim().toLowerCase()));
463
+ return Object.fromEntries(entries.sort(([left], [right]) => left.localeCompare(right)));
464
+ }
465
+ function normalizeToolChoice(value) {
466
+ if (typeof value === "string") {
467
+ return value;
468
+ }
469
+ if (!isRecord(value)) {
470
+ return value;
471
+ }
472
+ return sortObjectKeys(value);
473
+ }
474
+ function normalizeTextConfig(value) {
475
+ if (!isRecord(value)) {
476
+ return value;
477
+ }
478
+ const normalized = sortObjectKeys(value);
479
+ if (!isRecord(normalized.format)) {
480
+ return normalized;
481
+ }
482
+ return {
483
+ ...normalized,
484
+ format: sortObjectKeys(normalized.format),
485
+ };
486
+ }
487
+ function normalizeInclude(value) {
488
+ if (!Array.isArray(value)) {
489
+ return value;
490
+ }
491
+ return [...new Set(value.filter((entry) => typeof entry === "string"))].sort((left, right) => left.localeCompare(right));
492
+ }
493
+ function compareToolsForCacheStability(left, right) {
494
+ return JSON.stringify(sortObjectKeys(left)).localeCompare(JSON.stringify(sortObjectKeys(right)));
495
+ }
496
+ function sortObjectKeys(value) {
497
+ if (Array.isArray(value)) {
498
+ return value.map((item) => sortObjectKeys(item));
499
+ }
500
+ if (!isRecord(value)) {
501
+ return value;
502
+ }
503
+ const sortedEntries = Object.entries(value)
504
+ .filter(([, nested]) => nested !== undefined)
505
+ .sort(([left], [right]) => left.localeCompare(right))
506
+ .map(([key, nested]) => [key, sortObjectKeys(nested)]);
507
+ return Object.fromEntries(sortedEntries);
508
+ }
509
+ function normalizeJsonString(value) {
510
+ try {
511
+ const parsed = JSON.parse(value);
512
+ return JSON.stringify(sortObjectKeys(parsed));
513
+ }
514
+ catch {
515
+ return value;
516
+ }
517
+ }
518
+ const OPENCLAW_VOLATILE_METADATA_KEYS = new Set([
519
+ "request_id",
520
+ "trace_id",
521
+ "span_id",
522
+ "session_id",
523
+ "conversation_id",
524
+ "turn_id",
525
+ "message_id",
526
+ "event_id",
527
+ "client_request_id",
528
+ "requestid",
529
+ "traceparent",
530
+ "tracestate",
531
+ ]);