qlogicagent 0.2.1 → 0.4.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 (226) hide show
  1. package/README.md +45 -45
  2. package/package.json +56 -42
  3. package/dist/agent/agent.d.ts +0 -43
  4. package/dist/agent/agent.js +0 -113
  5. package/dist/agent/tool-loop.d.ts +0 -64
  6. package/dist/agent/tool-loop.js +0 -575
  7. package/dist/agent/types.d.ts +0 -175
  8. package/dist/agent/types.js +0 -14
  9. package/dist/cli/main.d.ts +0 -11
  10. package/dist/cli/main.js +0 -23
  11. package/dist/cli/stdio-server.d.ts +0 -45
  12. package/dist/cli/stdio-server.js +0 -463
  13. package/dist/config/config.d.ts +0 -17
  14. package/dist/config/config.js +0 -21
  15. package/dist/contracts/hooks.d.ts +0 -120
  16. package/dist/contracts/hooks.js +0 -7
  17. package/dist/contracts/index.d.ts +0 -10
  18. package/dist/contracts/index.js +0 -10
  19. package/dist/contracts/planner.d.ts +0 -35
  20. package/dist/contracts/planner.js +0 -2
  21. package/dist/contracts/skill-candidate.d.ts +0 -63
  22. package/dist/contracts/skill-candidate.js +0 -195
  23. package/dist/contracts/todo.d.ts +0 -14
  24. package/dist/contracts/todo.js +0 -9
  25. package/dist/index.d.ts +0 -13
  26. package/dist/index.js +0 -15
  27. package/dist/llm/builtin-providers.d.ts +0 -10
  28. package/dist/llm/builtin-providers.js +0 -531
  29. package/dist/llm/index.d.ts +0 -15
  30. package/dist/llm/index.js +0 -14
  31. package/dist/llm/llm-client.d.ts +0 -43
  32. package/dist/llm/llm-client.js +0 -67
  33. package/dist/llm/model-catalog.d.ts +0 -53
  34. package/dist/llm/model-catalog.js +0 -191
  35. package/dist/llm/provider-def.d.ts +0 -59
  36. package/dist/llm/provider-def.js +0 -12
  37. package/dist/llm/provider-registry.d.ts +0 -54
  38. package/dist/llm/provider-registry.js +0 -147
  39. package/dist/llm/transport.d.ts +0 -62
  40. package/dist/llm/transport.js +0 -27
  41. package/dist/llm/transports/anthropic-messages.d.ts +0 -31
  42. package/dist/llm/transports/anthropic-messages.js +0 -293
  43. package/dist/llm/transports/openai-chat.d.ts +0 -36
  44. package/dist/llm/transports/openai-chat.js +0 -165
  45. package/dist/orchestration/agent-registry.d.ts +0 -41
  46. package/dist/orchestration/agent-registry.js +0 -116
  47. package/dist/orchestration/approval-aware-tool-plan.d.ts +0 -32
  48. package/dist/orchestration/approval-aware-tool-plan.js +0 -87
  49. package/dist/orchestration/context-compression.d.ts +0 -220
  50. package/dist/orchestration/context-compression.js +0 -583
  51. package/dist/orchestration/conversation-repair.d.ts +0 -61
  52. package/dist/orchestration/conversation-repair.js +0 -429
  53. package/dist/orchestration/curator-scheduler.d.ts +0 -119
  54. package/dist/orchestration/curator-scheduler.js +0 -135
  55. package/dist/orchestration/embedded-failover-policy.d.ts +0 -110
  56. package/dist/orchestration/embedded-failover-policy.js +0 -168
  57. package/dist/orchestration/error-classification.d.ts +0 -12
  58. package/dist/orchestration/error-classification.js +0 -77
  59. package/dist/orchestration/failover-classification.d.ts +0 -8
  60. package/dist/orchestration/failover-classification.js +0 -381
  61. package/dist/orchestration/failover-error.d.ts +0 -33
  62. package/dist/orchestration/failover-error.js +0 -198
  63. package/dist/orchestration/fork-subagent.d.ts +0 -100
  64. package/dist/orchestration/fork-subagent.js +0 -98
  65. package/dist/orchestration/index.d.ts +0 -120
  66. package/dist/orchestration/index.js +0 -267
  67. package/dist/orchestration/memory-flush-policy.d.ts +0 -57
  68. package/dist/orchestration/memory-flush-policy.js +0 -85
  69. package/dist/orchestration/memory-provider.d.ts +0 -14
  70. package/dist/orchestration/memory-provider.js +0 -2
  71. package/dist/orchestration/parallel-tool-calls.d.ts +0 -41
  72. package/dist/orchestration/parallel-tool-calls.js +0 -59
  73. package/dist/orchestration/prompt-cache-strategy.d.ts +0 -126
  74. package/dist/orchestration/prompt-cache-strategy.js +0 -228
  75. package/dist/orchestration/reactive-compact.d.ts +0 -73
  76. package/dist/orchestration/reactive-compact.js +0 -78
  77. package/dist/orchestration/retry-loop.d.ts +0 -22
  78. package/dist/orchestration/retry-loop.js +0 -24
  79. package/dist/orchestration/skill-candidate.d.ts +0 -52
  80. package/dist/orchestration/skill-candidate.js +0 -141
  81. package/dist/orchestration/skill-consolidation.d.ts +0 -123
  82. package/dist/orchestration/skill-consolidation.js +0 -220
  83. package/dist/orchestration/skill-improvement.d.ts +0 -59
  84. package/dist/orchestration/skill-improvement.js +0 -66
  85. package/dist/orchestration/skill-similarity.d.ts +0 -98
  86. package/dist/orchestration/skill-similarity.js +0 -131
  87. package/dist/orchestration/streaming-tool-executor.d.ts +0 -73
  88. package/dist/orchestration/streaming-tool-executor.js +0 -96
  89. package/dist/orchestration/team-orchestration.d.ts +0 -195
  90. package/dist/orchestration/team-orchestration.js +0 -369
  91. package/dist/orchestration/team-tool-loop-wiring.d.ts +0 -92
  92. package/dist/orchestration/team-tool-loop-wiring.js +0 -147
  93. package/dist/orchestration/tool-choice-policy.d.ts +0 -54
  94. package/dist/orchestration/tool-choice-policy.js +0 -164
  95. package/dist/orchestration/tool-loop-state.d.ts +0 -50
  96. package/dist/orchestration/tool-loop-state.js +0 -133
  97. package/dist/orchestration/tool-schema.d.ts +0 -39
  98. package/dist/orchestration/tool-schema.js +0 -297
  99. package/dist/orchestration/transcript-repair.d.ts +0 -42
  100. package/dist/orchestration/transcript-repair.js +0 -426
  101. package/dist/orchestration/turn-loop-guard.d.ts +0 -86
  102. package/dist/orchestration/turn-loop-guard.js +0 -92
  103. package/dist/orchestration/web-browser-policy.d.ts +0 -17
  104. package/dist/orchestration/web-browser-policy.js +0 -39
  105. package/dist/runtime/context-compression.d.ts +0 -61
  106. package/dist/runtime/context-compression.js +0 -274
  107. package/dist/runtime/hook-registry.d.ts +0 -12
  108. package/dist/runtime/hook-registry.js +0 -53
  109. package/dist/runtime/memory-hooks.d.ts +0 -23
  110. package/dist/runtime/memory-hooks.js +0 -65
  111. package/dist/runtime/tool-eligibility.d.ts +0 -59
  112. package/dist/runtime/tool-eligibility.js +0 -111
  113. package/dist/skills/index.d.ts +0 -108
  114. package/dist/skills/index.js +0 -82
  115. package/dist/skills/memory-extractor.d.ts +0 -64
  116. package/dist/skills/memory-extractor.js +0 -173
  117. package/dist/skills/memory-query-tool.d.ts +0 -43
  118. package/dist/skills/memory-query-tool.js +0 -127
  119. package/dist/skills/memory-store.d.ts +0 -66
  120. package/dist/skills/memory-store.js +0 -228
  121. package/dist/skills/memory-tool.d.ts +0 -67
  122. package/dist/skills/memory-tool.js +0 -192
  123. package/dist/skills/portable-tool.d.ts +0 -71
  124. package/dist/skills/portable-tool.js +0 -14
  125. package/dist/skills/qmemory-adapter.d.ts +0 -52
  126. package/dist/skills/qmemory-adapter.js +0 -165
  127. package/dist/skills/skill-frontmatter.d.ts +0 -19
  128. package/dist/skills/skill-frontmatter.js +0 -344
  129. package/dist/skills/skill-guard.d.ts +0 -23
  130. package/dist/skills/skill-guard.js +0 -229
  131. package/dist/skills/skill-loader.d.ts +0 -16
  132. package/dist/skills/skill-loader.js +0 -303
  133. package/dist/skills/skill-source.d.ts +0 -119
  134. package/dist/skills/skill-source.js +0 -126
  135. package/dist/skills/skill-types.d.ts +0 -199
  136. package/dist/skills/skill-types.js +0 -6
  137. package/dist/skills/think-tool.d.ts +0 -16
  138. package/dist/skills/think-tool.js +0 -59
  139. package/dist/skills/todo-tool.d.ts +0 -63
  140. package/dist/skills/todo-tool.js +0 -114
  141. package/dist/skills/tools/agent-tool.d.ts +0 -91
  142. package/dist/skills/tools/agent-tool.js +0 -142
  143. package/dist/skills/tools/apply-patch-tool.d.ts +0 -29
  144. package/dist/skills/tools/apply-patch-tool.js +0 -184
  145. package/dist/skills/tools/ask-user-tool.d.ts +0 -80
  146. package/dist/skills/tools/ask-user-tool.js +0 -121
  147. package/dist/skills/tools/brief-tool.d.ts +0 -74
  148. package/dist/skills/tools/brief-tool.js +0 -95
  149. package/dist/skills/tools/browser-tool.d.ts +0 -114
  150. package/dist/skills/tools/browser-tool.js +0 -155
  151. package/dist/skills/tools/checkpoint-tool.d.ts +0 -66
  152. package/dist/skills/tools/checkpoint-tool.js +0 -102
  153. package/dist/skills/tools/config-tool.d.ts +0 -63
  154. package/dist/skills/tools/config-tool.js +0 -143
  155. package/dist/skills/tools/cron-tool.d.ts +0 -116
  156. package/dist/skills/tools/cron-tool.js +0 -175
  157. package/dist/skills/tools/edit-tool.d.ts +0 -43
  158. package/dist/skills/tools/edit-tool.js +0 -70
  159. package/dist/skills/tools/exec-tool.d.ts +0 -102
  160. package/dist/skills/tools/exec-tool.js +0 -133
  161. package/dist/skills/tools/image-generate-tool.d.ts +0 -62
  162. package/dist/skills/tools/image-generate-tool.js +0 -67
  163. package/dist/skills/tools/instructions-tool.d.ts +0 -103
  164. package/dist/skills/tools/instructions-tool.js +0 -187
  165. package/dist/skills/tools/lsp-tool.d.ts +0 -153
  166. package/dist/skills/tools/lsp-tool.js +0 -227
  167. package/dist/skills/tools/mcp-client-types.d.ts +0 -269
  168. package/dist/skills/tools/mcp-client-types.js +0 -53
  169. package/dist/skills/tools/mcp-tool.d.ts +0 -249
  170. package/dist/skills/tools/mcp-tool.js +0 -503
  171. package/dist/skills/tools/memory-tool.d.ts +0 -74
  172. package/dist/skills/tools/memory-tool.js +0 -88
  173. package/dist/skills/tools/monitor-tool.d.ts +0 -113
  174. package/dist/skills/tools/monitor-tool.js +0 -131
  175. package/dist/skills/tools/music-generate-tool.d.ts +0 -55
  176. package/dist/skills/tools/music-generate-tool.js +0 -62
  177. package/dist/skills/tools/notify-tool.d.ts +0 -53
  178. package/dist/skills/tools/notify-tool.js +0 -62
  179. package/dist/skills/tools/patch-tool.d.ts +0 -45
  180. package/dist/skills/tools/patch-tool.js +0 -505
  181. package/dist/skills/tools/pdf-tool.d.ts +0 -66
  182. package/dist/skills/tools/pdf-tool.js +0 -88
  183. package/dist/skills/tools/plan-mode-tool.d.ts +0 -59
  184. package/dist/skills/tools/plan-mode-tool.js +0 -122
  185. package/dist/skills/tools/read-tool.d.ts +0 -51
  186. package/dist/skills/tools/read-tool.js +0 -84
  187. package/dist/skills/tools/repl-tool.d.ts +0 -70
  188. package/dist/skills/tools/repl-tool.js +0 -69
  189. package/dist/skills/tools/search-tool.d.ts +0 -112
  190. package/dist/skills/tools/search-tool.js +0 -225
  191. package/dist/skills/tools/send-message-tool.d.ts +0 -51
  192. package/dist/skills/tools/send-message-tool.js +0 -76
  193. package/dist/skills/tools/skill-list-tool.d.ts +0 -33
  194. package/dist/skills/tools/skill-list-tool.js +0 -54
  195. package/dist/skills/tools/skill-manage-tool.d.ts +0 -73
  196. package/dist/skills/tools/skill-manage-tool.js +0 -153
  197. package/dist/skills/tools/skill-view-tool.d.ts +0 -37
  198. package/dist/skills/tools/skill-view-tool.js +0 -72
  199. package/dist/skills/tools/sleep-tool.d.ts +0 -49
  200. package/dist/skills/tools/sleep-tool.js +0 -81
  201. package/dist/skills/tools/structured-output-tool.d.ts +0 -116
  202. package/dist/skills/tools/structured-output-tool.js +0 -176
  203. package/dist/skills/tools/task-tool.d.ts +0 -104
  204. package/dist/skills/tools/task-tool.js +0 -161
  205. package/dist/skills/tools/team-tool.d.ts +0 -89
  206. package/dist/skills/tools/team-tool.js +0 -105
  207. package/dist/skills/tools/tool-search-tool.d.ts +0 -51
  208. package/dist/skills/tools/tool-search-tool.js +0 -110
  209. package/dist/skills/tools/tts-tool.d.ts +0 -38
  210. package/dist/skills/tools/tts-tool.js +0 -45
  211. package/dist/skills/tools/video-edit-tool.d.ts +0 -69
  212. package/dist/skills/tools/video-edit-tool.js +0 -74
  213. package/dist/skills/tools/video-generate-tool.d.ts +0 -62
  214. package/dist/skills/tools/video-generate-tool.js +0 -66
  215. package/dist/skills/tools/video-merge-tool.d.ts +0 -105
  216. package/dist/skills/tools/video-merge-tool.js +0 -92
  217. package/dist/skills/tools/video-upscale-tool.d.ts +0 -45
  218. package/dist/skills/tools/video-upscale-tool.js +0 -52
  219. package/dist/skills/tools/web-fetch-tool.d.ts +0 -78
  220. package/dist/skills/tools/web-fetch-tool.js +0 -92
  221. package/dist/skills/tools/web-search-tool.d.ts +0 -57
  222. package/dist/skills/tools/web-search-tool.js +0 -86
  223. package/dist/skills/tools/worktree-tool.d.ts +0 -69
  224. package/dist/skills/tools/worktree-tool.js +0 -147
  225. package/dist/skills/tools/write-tool.d.ts +0 -45
  226. package/dist/skills/tools/write-tool.js +0 -81
@@ -1,426 +0,0 @@
1
- const TOOL_CALL_NAME_MAX_CHARS = 64;
2
- const TOOL_CALL_NAME_RE = /^[A-Za-z0-9_-]+$/;
3
- const TOOL_CALL_TYPES = new Set(["toolCall", "toolUse", "functionCall"]);
4
- const TRANSIENT_ERROR_RE = /rate limit|rate.limited|overloaded|too many requests|quota exceeded|timed?\s*out|billing|insufficient.*(balance|quota|credit)|try again later/i;
5
- function isRawToolCallBlock(block) {
6
- if (!block || typeof block !== "object") {
7
- return false;
8
- }
9
- const type = block.type;
10
- return typeof type === "string" && TOOL_CALL_TYPES.has(type);
11
- }
12
- function hasToolCallInput(block) {
13
- const hasInput = "input" in block ? block.input !== undefined && block.input !== null : false;
14
- const hasArguments = "arguments" in block ? block.arguments !== undefined && block.arguments !== null : false;
15
- return hasInput || hasArguments;
16
- }
17
- function hasNonEmptyStringField(value) {
18
- return typeof value === "string" && value.trim().length > 0;
19
- }
20
- function hasToolCallId(block) {
21
- return hasNonEmptyStringField(block.id);
22
- }
23
- function trimNonEmptyString(value) {
24
- if (typeof value !== "string") {
25
- return undefined;
26
- }
27
- const trimmed = value.trim();
28
- return trimmed || undefined;
29
- }
30
- function normalizeAllowedToolNames(allowedToolNames) {
31
- if (!allowedToolNames) {
32
- return null;
33
- }
34
- const normalized = new Set();
35
- for (const name of allowedToolNames) {
36
- if (typeof name !== "string") {
37
- continue;
38
- }
39
- const trimmed = name.trim();
40
- if (trimmed) {
41
- normalized.add(trimmed.toLowerCase());
42
- }
43
- }
44
- return normalized.size > 0 ? normalized : null;
45
- }
46
- function hasToolCallName(block, allowedToolNames) {
47
- if (typeof block.name !== "string") {
48
- return false;
49
- }
50
- const trimmed = block.name.trim();
51
- if (!trimmed) {
52
- return false;
53
- }
54
- if (trimmed.length > TOOL_CALL_NAME_MAX_CHARS || !TOOL_CALL_NAME_RE.test(trimmed)) {
55
- return false;
56
- }
57
- if (!allowedToolNames) {
58
- return true;
59
- }
60
- return allowedToolNames.has(trimmed.toLowerCase());
61
- }
62
- function redactSessionsSpawnAttachmentsArgs(value) {
63
- if (!value || typeof value !== "object") {
64
- return value;
65
- }
66
- const record = value;
67
- const raw = record.attachments;
68
- if (!Array.isArray(raw)) {
69
- return value;
70
- }
71
- const next = raw.map((item) => {
72
- if (!item || typeof item !== "object") {
73
- return item;
74
- }
75
- const attachment = item;
76
- if (!Object.hasOwn(attachment, "content")) {
77
- return item;
78
- }
79
- const { content: _content, ...rest } = attachment;
80
- return { ...rest, content: "__OPENCLAW_REDACTED__" };
81
- });
82
- return { ...record, attachments: next };
83
- }
84
- function sanitizeToolCallBlock(block) {
85
- const rawName = typeof block.name === "string" ? block.name : undefined;
86
- const trimmedName = rawName?.trim();
87
- const hasTrimmedName = typeof trimmedName === "string" && trimmedName.length > 0;
88
- const normalizedName = hasTrimmedName ? trimmedName : undefined;
89
- const nameChanged = hasTrimmedName && rawName !== trimmedName;
90
- const isDelegatedSessionSpawnToolCall = normalizedName?.toLowerCase() === "sessions_spawn";
91
- if (!isDelegatedSessionSpawnToolCall) {
92
- if (!nameChanged) {
93
- return block;
94
- }
95
- return { ...block, name: normalizedName };
96
- }
97
- const nextArgs = redactSessionsSpawnAttachmentsArgs(block.arguments);
98
- const nextInput = redactSessionsSpawnAttachmentsArgs(block.input);
99
- if (nextArgs === block.arguments && nextInput === block.input && !nameChanged) {
100
- return block;
101
- }
102
- const next = { ...block };
103
- if (nameChanged && normalizedName) {
104
- next.name = normalizedName;
105
- }
106
- if (nextArgs !== block.arguments || Object.hasOwn(block, "arguments")) {
107
- next.arguments = nextArgs;
108
- }
109
- if (nextInput !== block.input || Object.hasOwn(block, "input")) {
110
- next.input = nextInput;
111
- }
112
- return next;
113
- }
114
- function normalizeToolResultName(message, fallbackName) {
115
- const rawToolName = message.toolName;
116
- const normalizedToolName = trimNonEmptyString(rawToolName);
117
- if (normalizedToolName) {
118
- if (rawToolName === normalizedToolName) {
119
- return message;
120
- }
121
- return { ...message, toolName: normalizedToolName };
122
- }
123
- const normalizedFallback = trimNonEmptyString(fallbackName);
124
- if (normalizedFallback) {
125
- return { ...message, toolName: normalizedFallback };
126
- }
127
- if (typeof rawToolName === "string") {
128
- return { ...message, toolName: "unknown" };
129
- }
130
- return message;
131
- }
132
- function extractToolCallsFromAssistantLike(message) {
133
- const content = message.content;
134
- if (!Array.isArray(content)) {
135
- return [];
136
- }
137
- const toolCalls = [];
138
- for (const block of content) {
139
- if (!block || typeof block !== "object") {
140
- continue;
141
- }
142
- const record = block;
143
- if (typeof record.id !== "string" || !record.id) {
144
- continue;
145
- }
146
- if (typeof record.type === "string" && TOOL_CALL_TYPES.has(record.type)) {
147
- toolCalls.push({
148
- id: record.id,
149
- name: typeof record.name === "string" ? record.name : undefined,
150
- });
151
- }
152
- }
153
- return toolCalls;
154
- }
155
- function extractToolResultIdLike(message) {
156
- if (typeof message.toolCallId === "string" && message.toolCallId) {
157
- return message.toolCallId;
158
- }
159
- if (typeof message.toolUseId === "string" && message.toolUseId) {
160
- return message.toolUseId;
161
- }
162
- return null;
163
- }
164
- function defaultMissingToolResult(params) {
165
- return {
166
- role: "toolResult",
167
- toolCallId: params.toolCallId,
168
- toolName: params.toolName ?? "unknown",
169
- content: [
170
- {
171
- type: "text",
172
- text: "[qlogicagent] missing tool result in session history; inserted synthetic error result for transcript repair.",
173
- },
174
- ],
175
- isError: true,
176
- timestamp: 0,
177
- };
178
- }
179
- export function stripToolResultDetails(messages) {
180
- let touched = false;
181
- const out = [];
182
- for (const message of messages) {
183
- if (!message || typeof message !== "object" || message.role !== "toolResult") {
184
- out.push(message);
185
- continue;
186
- }
187
- if (!("details" in message)) {
188
- out.push(message);
189
- continue;
190
- }
191
- const sanitized = { ...message };
192
- delete sanitized.details;
193
- touched = true;
194
- out.push(sanitized);
195
- }
196
- return touched ? out : [...messages];
197
- }
198
- export function repairToolCallInputs(messages, options) {
199
- let droppedToolCalls = 0;
200
- let droppedAssistantMessages = 0;
201
- let changed = false;
202
- const out = [];
203
- const allowedToolNames = normalizeAllowedToolNames(options?.allowedToolNames);
204
- for (const message of messages) {
205
- if (!message || typeof message !== "object") {
206
- out.push(message);
207
- continue;
208
- }
209
- if (message.role !== "assistant" || !Array.isArray(message.content)) {
210
- out.push(message);
211
- continue;
212
- }
213
- const nextContent = [];
214
- let droppedInMessage = 0;
215
- let messageChanged = false;
216
- for (const block of message.content) {
217
- if (isRawToolCallBlock(block) &&
218
- (!hasToolCallInput(block) ||
219
- !hasToolCallId(block) ||
220
- !hasToolCallName(block, allowedToolNames))) {
221
- droppedToolCalls += 1;
222
- droppedInMessage += 1;
223
- changed = true;
224
- messageChanged = true;
225
- continue;
226
- }
227
- if (isRawToolCallBlock(block)) {
228
- const blockName = typeof block.name === "string" ? block.name.trim() : undefined;
229
- if (blockName?.toLowerCase() === "sessions_spawn") {
230
- const sanitized = sanitizeToolCallBlock(block);
231
- if (sanitized !== block) {
232
- changed = true;
233
- messageChanged = true;
234
- }
235
- nextContent.push(sanitized);
236
- continue;
237
- }
238
- if (typeof block.name === "string") {
239
- const trimmedName = block.name.trim();
240
- if (trimmedName && trimmedName !== block.name) {
241
- nextContent.push({ ...block, name: trimmedName });
242
- changed = true;
243
- messageChanged = true;
244
- continue;
245
- }
246
- }
247
- }
248
- nextContent.push(block);
249
- }
250
- if (droppedInMessage > 0) {
251
- if (nextContent.length === 0) {
252
- droppedAssistantMessages += 1;
253
- changed = true;
254
- continue;
255
- }
256
- out.push({ ...message, content: nextContent });
257
- continue;
258
- }
259
- if (messageChanged) {
260
- out.push({ ...message, content: nextContent });
261
- continue;
262
- }
263
- out.push(message);
264
- }
265
- return {
266
- messages: changed ? out : [...messages],
267
- droppedToolCalls,
268
- droppedAssistantMessages,
269
- };
270
- }
271
- export function sanitizeToolCallInputs(messages, options) {
272
- return repairToolCallInputs(messages, options).messages;
273
- }
274
- export function repairToolUseResultPairing(messages, options) {
275
- const out = [];
276
- const added = [];
277
- const seenToolResultIds = new Set();
278
- let droppedDuplicateCount = 0;
279
- let droppedOrphanCount = 0;
280
- let moved = false;
281
- let changed = false;
282
- const createMissingToolResult = options?.createMissingToolResult ?? (defaultMissingToolResult);
283
- const pushToolResult = (message) => {
284
- const id = extractToolResultIdLike(message);
285
- if (id && seenToolResultIds.has(id)) {
286
- droppedDuplicateCount += 1;
287
- changed = true;
288
- return;
289
- }
290
- if (id) {
291
- seenToolResultIds.add(id);
292
- }
293
- out.push(message);
294
- };
295
- for (let i = 0; i < messages.length; i += 1) {
296
- const message = messages[i];
297
- if (!message || typeof message !== "object") {
298
- out.push(message);
299
- continue;
300
- }
301
- if (message.role !== "assistant") {
302
- if (message.role !== "toolResult") {
303
- out.push(message);
304
- }
305
- else {
306
- droppedOrphanCount += 1;
307
- changed = true;
308
- }
309
- continue;
310
- }
311
- const stopReason = message.stopReason;
312
- if (stopReason === "error" || stopReason === "aborted") {
313
- out.push(message);
314
- continue;
315
- }
316
- const toolCalls = extractToolCallsFromAssistantLike(message);
317
- if (toolCalls.length === 0) {
318
- out.push(message);
319
- continue;
320
- }
321
- const toolCallIds = new Set(toolCalls.map((toolCall) => toolCall.id));
322
- const toolCallNamesById = new Map(toolCalls.map((toolCall) => [toolCall.id, toolCall.name]));
323
- const spanResultsById = new Map();
324
- const remainder = [];
325
- let j = i + 1;
326
- for (; j < messages.length; j += 1) {
327
- const next = messages[j];
328
- if (!next || typeof next !== "object") {
329
- remainder.push(next);
330
- continue;
331
- }
332
- if (next.role === "assistant") {
333
- break;
334
- }
335
- if (next.role === "toolResult") {
336
- const id = extractToolResultIdLike(next);
337
- if (id && toolCallIds.has(id)) {
338
- if (seenToolResultIds.has(id)) {
339
- droppedDuplicateCount += 1;
340
- changed = true;
341
- continue;
342
- }
343
- const normalizedToolResult = normalizeToolResultName(next, toolCallNamesById.get(id));
344
- if (normalizedToolResult !== next) {
345
- changed = true;
346
- }
347
- if (!spanResultsById.has(id)) {
348
- spanResultsById.set(id, normalizedToolResult);
349
- }
350
- continue;
351
- }
352
- }
353
- if (next.role !== "toolResult") {
354
- remainder.push(next);
355
- }
356
- else {
357
- droppedOrphanCount += 1;
358
- changed = true;
359
- }
360
- }
361
- out.push(message);
362
- if (spanResultsById.size > 0 && remainder.length > 0) {
363
- moved = true;
364
- changed = true;
365
- }
366
- for (const call of toolCalls) {
367
- const existing = spanResultsById.get(call.id);
368
- if (existing) {
369
- pushToolResult(existing);
370
- }
371
- else {
372
- const missing = createMissingToolResult({
373
- toolCallId: call.id,
374
- toolName: call.name,
375
- });
376
- added.push(missing);
377
- changed = true;
378
- pushToolResult(missing);
379
- }
380
- }
381
- for (const rem of remainder) {
382
- out.push(rem);
383
- }
384
- i = j - 1;
385
- }
386
- const changedOrMoved = changed || moved;
387
- return {
388
- messages: changedOrMoved ? out : [...messages],
389
- added,
390
- droppedDuplicateCount,
391
- droppedOrphanCount,
392
- moved: changedOrMoved,
393
- };
394
- }
395
- export function sanitizeToolUseResultPairing(messages, options) {
396
- return repairToolUseResultPairing(messages, options).messages;
397
- }
398
- export function stripTransientErrorMessages(messages) {
399
- let changed = false;
400
- const out = [];
401
- for (const message of messages) {
402
- if (!message || typeof message !== "object") {
403
- out.push(message);
404
- continue;
405
- }
406
- if (message.role !== "assistant" || message.stopReason !== "error") {
407
- out.push(message);
408
- continue;
409
- }
410
- const content = message.content;
411
- const text = typeof content === "string"
412
- ? content
413
- : Array.isArray(content)
414
- ? content
415
- .filter((block) => block.type === "text")
416
- .map((block) => block.text ?? "")
417
- .join(" ")
418
- : "";
419
- if (!TRANSIENT_ERROR_RE.test(text)) {
420
- out.push(message);
421
- continue;
422
- }
423
- changed = true;
424
- }
425
- return changed ? out : [...messages];
426
- }
@@ -1,86 +0,0 @@
1
- /**
2
- * Turn Loop Guard — Abort signal + token budget warning for preventive termination.
3
- *
4
- * Aligned with Claude Code's multi-condition termination:
5
- * 1. AbortSignal from user cancellation
6
- * 2. Token budget pre-check (don't send API call if over budget)
7
- * 3. max_output_tokens escalation on truncation
8
- * 4. Prompt-too-long reactive compact trigger
9
- */
10
- export interface TurnLoopGuardConfig {
11
- /** Context window size in tokens. */
12
- contextWindowTokens: number;
13
- /** Buffer reserved for response (default: 13000). */
14
- responseBufferTokens: number;
15
- /** Max output tokens for current model. */
16
- maxOutputTokens: number;
17
- /** Abort signal for user cancellation. */
18
- abortSignal?: AbortSignal;
19
- /** Whether reactive compact is available. */
20
- reactiveCompactEnabled: boolean;
21
- /** Whether max_output_tokens escalation is allowed. */
22
- outputEscalationEnabled: boolean;
23
- }
24
- export type TokenWarningState = {
25
- level: "ok";
26
- } | {
27
- level: "warning";
28
- usagePercent: number;
29
- remainingTokens: number;
30
- } | {
31
- level: "blocking";
32
- usagePercent: number;
33
- reason: "prompt_too_long" | "budget_exhausted";
34
- };
35
- export interface TurnLoopGuardState {
36
- /** Current accumulated prompt tokens. */
37
- promptTokens: number;
38
- /** Whether reactive compact has already been attempted this turn. */
39
- hasAttemptedReactiveCompact: boolean;
40
- /** Current max_output_tokens (may escalate). */
41
- currentMaxOutputTokens: number;
42
- /** Number of consecutive max_tokens truncations. */
43
- consecutiveTruncations: number;
44
- /** Whether the turn has been aborted. */
45
- aborted: boolean;
46
- }
47
- /** Initial guard state. */
48
- export declare function createTurnLoopGuardState(config: TurnLoopGuardConfig): TurnLoopGuardState;
49
- /**
50
- * Calculate token warning state before making an API call.
51
- * If level is "blocking", do NOT send the API call.
52
- */
53
- export declare function calculateTokenWarningState(state: TurnLoopGuardState, config: TurnLoopGuardConfig): TokenWarningState;
54
- /**
55
- * Handle API error and determine recovery action.
56
- */
57
- export type ApiErrorRecovery = {
58
- action: "reactive_compact";
59
- } | {
60
- action: "escalate_output_tokens";
61
- newMax: number;
62
- } | {
63
- action: "retry";
64
- reason: string;
65
- } | {
66
- action: "abort";
67
- reason: string;
68
- };
69
- export declare function resolveApiErrorRecovery(error: {
70
- type?: string;
71
- status?: number;
72
- message?: string;
73
- }, state: TurnLoopGuardState, config: TurnLoopGuardConfig): ApiErrorRecovery;
74
- /**
75
- * Handle finish_reason === "max_tokens" — escalate max_output_tokens.
76
- *
77
- * CC pattern: double the output token budget up to model max.
78
- */
79
- export declare function resolveOutputTokenEscalation(state: TurnLoopGuardState, config: TurnLoopGuardConfig, modelMaxOutput: number): {
80
- shouldEscalate: boolean;
81
- newMax: number;
82
- };
83
- /**
84
- * Check if the turn should be aborted (user cancellation).
85
- */
86
- export declare function shouldAbortTurn(state: TurnLoopGuardState, config: TurnLoopGuardConfig): boolean;
@@ -1,92 +0,0 @@
1
- /**
2
- * Turn Loop Guard — Abort signal + token budget warning for preventive termination.
3
- *
4
- * Aligned with Claude Code's multi-condition termination:
5
- * 1. AbortSignal from user cancellation
6
- * 2. Token budget pre-check (don't send API call if over budget)
7
- * 3. max_output_tokens escalation on truncation
8
- * 4. Prompt-too-long reactive compact trigger
9
- */
10
- /** Initial guard state. */
11
- export function createTurnLoopGuardState(config) {
12
- return {
13
- promptTokens: 0,
14
- hasAttemptedReactiveCompact: false,
15
- currentMaxOutputTokens: config.maxOutputTokens,
16
- consecutiveTruncations: 0,
17
- aborted: config.abortSignal?.aborted ?? false,
18
- };
19
- }
20
- /**
21
- * Calculate token warning state before making an API call.
22
- * If level is "blocking", do NOT send the API call.
23
- */
24
- export function calculateTokenWarningState(state, config) {
25
- if (state.aborted) {
26
- return { level: "blocking", usagePercent: 100, reason: "budget_exhausted" };
27
- }
28
- const effectiveLimit = config.contextWindowTokens - config.responseBufferTokens - state.currentMaxOutputTokens;
29
- const usagePercent = effectiveLimit > 0 ? (state.promptTokens / effectiveLimit) * 100 : 100;
30
- if (state.promptTokens >= effectiveLimit) {
31
- if (state.hasAttemptedReactiveCompact || !config.reactiveCompactEnabled) {
32
- return { level: "blocking", usagePercent, reason: "prompt_too_long" };
33
- }
34
- return { level: "blocking", usagePercent, reason: "prompt_too_long" };
35
- }
36
- // Warning at 85%
37
- if (usagePercent >= 85) {
38
- return { level: "warning", usagePercent, remainingTokens: effectiveLimit - state.promptTokens };
39
- }
40
- return { level: "ok" };
41
- }
42
- export function resolveApiErrorRecovery(error, state, config) {
43
- const msg = error.message?.toLowerCase() ?? "";
44
- const status = error.status ?? 0;
45
- // Prompt too long (413 or specific error message)
46
- if (status === 413 || msg.includes("prompt_too_long") || msg.includes("context_length_exceeded")) {
47
- if (!state.hasAttemptedReactiveCompact && config.reactiveCompactEnabled) {
48
- return { action: "reactive_compact" };
49
- }
50
- return { action: "abort", reason: "prompt_too_long_unrecoverable" };
51
- }
52
- // 5xx transient errors
53
- if (status >= 500 && status < 600) {
54
- return { action: "retry", reason: `server_error_${status}` };
55
- }
56
- // Rate limit
57
- if (status === 429) {
58
- return { action: "retry", reason: "rate_limited" };
59
- }
60
- return { action: "abort", reason: msg || "unknown_error" };
61
- }
62
- /**
63
- * Handle finish_reason === "max_tokens" — escalate max_output_tokens.
64
- *
65
- * CC pattern: double the output token budget up to model max.
66
- */
67
- export function resolveOutputTokenEscalation(state, config, modelMaxOutput) {
68
- if (!config.outputEscalationEnabled) {
69
- return { shouldEscalate: false, newMax: state.currentMaxOutputTokens };
70
- }
71
- // Don't escalate more than 3 times
72
- if (state.consecutiveTruncations >= 3) {
73
- return { shouldEscalate: false, newMax: state.currentMaxOutputTokens };
74
- }
75
- const newMax = Math.min(state.currentMaxOutputTokens * 2, modelMaxOutput);
76
- if (newMax <= state.currentMaxOutputTokens) {
77
- return { shouldEscalate: false, newMax: state.currentMaxOutputTokens };
78
- }
79
- return { shouldEscalate: true, newMax };
80
- }
81
- /**
82
- * Check if the turn should be aborted (user cancellation).
83
- */
84
- export function shouldAbortTurn(state, config) {
85
- if (state.aborted)
86
- return true;
87
- if (config.abortSignal?.aborted) {
88
- state.aborted = true;
89
- return true;
90
- }
91
- return false;
92
- }
@@ -1,17 +0,0 @@
1
- import type { WebCapabilityDescriptorContract, WebCapabilityFamily, WebDegradationTarget, WebEscalationReason, WebTaskMode } from "qlogicagent-runtime-contracts";
2
- export interface ResolveWebBrowserPolicyInput {
3
- capability?: Pick<WebCapabilityDescriptorContract, "capabilityFamily" | "taskMode" | "degradationTarget" | "upgradeTargets" | "escalationReasons" | "supportsLoginState" | "supportsPersistentProfile" | "supportsTransactionality">;
4
- needsInteractiveSession?: boolean;
5
- needsLoginState?: boolean;
6
- needsPersistentSession?: boolean;
7
- needsStateChangingAction?: boolean;
8
- browserAvailable?: boolean;
9
- }
10
- export interface ResolveWebBrowserPolicyResult {
11
- selectedCapabilityFamily: WebCapabilityFamily;
12
- selectedTaskMode?: WebTaskMode;
13
- shouldEscalateToBrowser: boolean;
14
- degradationTarget?: WebDegradationTarget;
15
- escalationReasons: WebEscalationReason[];
16
- }
17
- export declare function resolveWebBrowserPolicy(params: ResolveWebBrowserPolicyInput): ResolveWebBrowserPolicyResult;
@@ -1,39 +0,0 @@
1
- function pushReason(reasons, enabled, reason) {
2
- if (enabled) {
3
- reasons.add(reason);
4
- }
5
- }
6
- export function resolveWebBrowserPolicy(params) {
7
- const capability = params.capability;
8
- const reasons = new Set(capability?.escalationReasons ?? []);
9
- pushReason(reasons, params.needsInteractiveSession === true, "requires-interaction");
10
- pushReason(reasons, params.needsLoginState === true, "requires-login-state");
11
- pushReason(reasons, params.needsPersistentSession === true, "requires-persistent-session");
12
- pushReason(reasons, params.needsStateChangingAction === true, "requires-state-changing-action");
13
- const shouldEscalateToBrowser = capability?.capabilityFamily === "browser-execution"
14
- || capability?.taskMode === "interactive-browser"
15
- || reasons.size > 0;
16
- if (!shouldEscalateToBrowser) {
17
- return {
18
- selectedCapabilityFamily: "web-intelligence",
19
- selectedTaskMode: capability?.taskMode,
20
- shouldEscalateToBrowser: false,
21
- escalationReasons: [],
22
- };
23
- }
24
- if (params.browserAvailable !== false) {
25
- return {
26
- selectedCapabilityFamily: "browser-execution",
27
- selectedTaskMode: "interactive-browser",
28
- shouldEscalateToBrowser: true,
29
- escalationReasons: [...reasons],
30
- };
31
- }
32
- return {
33
- selectedCapabilityFamily: capability?.capabilityFamily ?? "web-intelligence",
34
- selectedTaskMode: capability?.taskMode,
35
- shouldEscalateToBrowser: false,
36
- degradationTarget: capability?.degradationTarget,
37
- escalationReasons: [...reasons],
38
- };
39
- }