@noorm/marie-cli 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 (193) hide show
  1. package/JOY_ZONING.md +200 -0
  2. package/LICENSE.md +190 -0
  3. package/README.md +94 -0
  4. package/dist/cli-new/components/App.js +138 -0
  5. package/dist/cli-new/components/App.js.map +1 -0
  6. package/dist/cli-new/components/ApprovalDialog.js +31 -0
  7. package/dist/cli-new/components/ApprovalDialog.js.map +1 -0
  8. package/dist/cli-new/components/Banner.js +23 -0
  9. package/dist/cli-new/components/Banner.js.map +1 -0
  10. package/dist/cli-new/components/ChatArea.js +49 -0
  11. package/dist/cli-new/components/ChatArea.js.map +1 -0
  12. package/dist/cli-new/components/Header.js +20 -0
  13. package/dist/cli-new/components/Header.js.map +1 -0
  14. package/dist/cli-new/components/InputArea.js +97 -0
  15. package/dist/cli-new/components/InputArea.js.map +1 -0
  16. package/dist/cli-new/components/MessageBubble.js +114 -0
  17. package/dist/cli-new/components/MessageBubble.js.map +1 -0
  18. package/dist/cli-new/components/SessionSwitcher.js +46 -0
  19. package/dist/cli-new/components/SessionSwitcher.js.map +1 -0
  20. package/dist/cli-new/components/SetupWizard.js +283 -0
  21. package/dist/cli-new/components/SetupWizard.js.map +1 -0
  22. package/dist/cli-new/components/ToolCallDisplay.js +45 -0
  23. package/dist/cli-new/components/ToolCallDisplay.js.map +1 -0
  24. package/dist/cli-new/hooks/useGit.js +99 -0
  25. package/dist/cli-new/hooks/useGit.js.map +1 -0
  26. package/dist/cli-new/hooks/useMarie.js +249 -0
  27. package/dist/cli-new/hooks/useMarie.js.map +1 -0
  28. package/dist/cli-new/hooks/useSessions.js +75 -0
  29. package/dist/cli-new/hooks/useSessions.js.map +1 -0
  30. package/dist/cli-new/index.js +52 -0
  31. package/dist/cli-new/index.js.map +1 -0
  32. package/dist/cli-new/styles/theme.js +68 -0
  33. package/dist/cli-new/styles/theme.js.map +1 -0
  34. package/dist/cli-new/types/cli.js +2 -0
  35. package/dist/cli-new/types/cli.js.map +1 -0
  36. package/dist/extension.cjs +655 -0
  37. package/dist/monolith/adapters/CliMarieAdapter.js +72 -0
  38. package/dist/monolith/adapters/CliMarieAdapter.js.map +1 -0
  39. package/dist/monolith/adapters/VscodeMarieAdapter.js +81 -0
  40. package/dist/monolith/adapters/VscodeMarieAdapter.js.map +1 -0
  41. package/dist/monolith/cli/CliFileSystemPort.js +83 -0
  42. package/dist/monolith/cli/CliFileSystemPort.js.map +1 -0
  43. package/dist/monolith/cli/MarieToolDefinitionsCLI.js +438 -0
  44. package/dist/monolith/cli/MarieToolDefinitionsCLI.js.map +1 -0
  45. package/dist/monolith/cli/index.js +272 -0
  46. package/dist/monolith/cli/index.js.map +1 -0
  47. package/dist/monolith/cli/services/JoyAutomationServiceCLI.js +80 -0
  48. package/dist/monolith/cli/services/JoyAutomationServiceCLI.js.map +1 -0
  49. package/dist/monolith/cli/services/JoyServiceCLI.js +63 -0
  50. package/dist/monolith/cli/services/JoyServiceCLI.js.map +1 -0
  51. package/dist/monolith/cli/storage.js +119 -0
  52. package/dist/monolith/cli/storage.js.map +1 -0
  53. package/dist/monolith/domain/joy/JoyTools.js +513 -0
  54. package/dist/monolith/domain/joy/JoyTools.js.map +1 -0
  55. package/dist/monolith/domain/joy/RitualService.js +51 -0
  56. package/dist/monolith/domain/joy/RitualService.js.map +1 -0
  57. package/dist/monolith/domain/marie/MarieTypes.js +2 -0
  58. package/dist/monolith/domain/marie/MarieTypes.js.map +1 -0
  59. package/dist/monolith/infrastructure/ai/agents/MarieAscendant.js +230 -0
  60. package/dist/monolith/infrastructure/ai/agents/MarieAscendant.js.map +1 -0
  61. package/dist/monolith/infrastructure/ai/agents/MarieYOLO.js +207 -0
  62. package/dist/monolith/infrastructure/ai/agents/MarieYOLO.js.map +1 -0
  63. package/dist/monolith/infrastructure/ai/context/ContextArchiveService.js +129 -0
  64. package/dist/monolith/infrastructure/ai/context/ContextArchiveService.js.map +1 -0
  65. package/dist/monolith/infrastructure/ai/context/ContextManager.js +118 -0
  66. package/dist/monolith/infrastructure/ai/context/ContextManager.js.map +1 -0
  67. package/dist/monolith/infrastructure/ai/core/FileSystemPort.js +2 -0
  68. package/dist/monolith/infrastructure/ai/core/FileSystemPort.js.map +1 -0
  69. package/dist/monolith/infrastructure/ai/core/GhostPort.js +2 -0
  70. package/dist/monolith/infrastructure/ai/core/GhostPort.js.map +1 -0
  71. package/dist/monolith/infrastructure/ai/core/MarieAscensionTypes.js +2 -0
  72. package/dist/monolith/infrastructure/ai/core/MarieAscensionTypes.js.map +1 -0
  73. package/dist/monolith/infrastructure/ai/core/MarieEngine.js +590 -0
  74. package/dist/monolith/infrastructure/ai/core/MarieEngine.js.map +1 -0
  75. package/dist/monolith/infrastructure/ai/core/MarieEventDispatcher.js +161 -0
  76. package/dist/monolith/infrastructure/ai/core/MarieEventDispatcher.js.map +1 -0
  77. package/dist/monolith/infrastructure/ai/core/MarieLockManager.js +121 -0
  78. package/dist/monolith/infrastructure/ai/core/MarieLockManager.js.map +1 -0
  79. package/dist/monolith/infrastructure/ai/core/MarieProgressTracker.js +256 -0
  80. package/dist/monolith/infrastructure/ai/core/MarieProgressTracker.js.map +1 -0
  81. package/dist/monolith/infrastructure/ai/core/MariePulseService.js +67 -0
  82. package/dist/monolith/infrastructure/ai/core/MariePulseService.js.map +1 -0
  83. package/dist/monolith/infrastructure/ai/core/MarieResponse.js +101 -0
  84. package/dist/monolith/infrastructure/ai/core/MarieResponse.js.map +1 -0
  85. package/dist/monolith/infrastructure/ai/core/MarieSanitizer.js +86 -0
  86. package/dist/monolith/infrastructure/ai/core/MarieSanitizer.js.map +1 -0
  87. package/dist/monolith/infrastructure/ai/core/MarieSession.js +202 -0
  88. package/dist/monolith/infrastructure/ai/core/MarieSession.js.map +1 -0
  89. package/dist/monolith/infrastructure/ai/core/MarieStabilityMonitor.js +58 -0
  90. package/dist/monolith/infrastructure/ai/core/MarieStabilityMonitor.js.map +1 -0
  91. package/dist/monolith/infrastructure/ai/core/MarieToolMender.js +127 -0
  92. package/dist/monolith/infrastructure/ai/core/MarieToolMender.js.map +1 -0
  93. package/dist/monolith/infrastructure/ai/core/MarieToolProcessor.js +548 -0
  94. package/dist/monolith/infrastructure/ai/core/MarieToolProcessor.js.map +1 -0
  95. package/dist/monolith/infrastructure/ai/core/MarieYOLOTypes.js +2 -0
  96. package/dist/monolith/infrastructure/ai/core/MarieYOLOTypes.js.map +1 -0
  97. package/dist/monolith/infrastructure/ai/core/ReasoningBudget.js +125 -0
  98. package/dist/monolith/infrastructure/ai/core/ReasoningBudget.js.map +1 -0
  99. package/dist/monolith/infrastructure/ai/core/VscodeFileSystemPort.js +30 -0
  100. package/dist/monolith/infrastructure/ai/core/VscodeFileSystemPort.js.map +1 -0
  101. package/dist/monolith/infrastructure/ai/providers/AIProvider.js +2 -0
  102. package/dist/monolith/infrastructure/ai/providers/AIProvider.js.map +1 -0
  103. package/dist/monolith/infrastructure/ai/providers/AnthropicProvider.js +148 -0
  104. package/dist/monolith/infrastructure/ai/providers/AnthropicProvider.js.map +1 -0
  105. package/dist/monolith/infrastructure/ai/providers/CerebrasProvider.js +208 -0
  106. package/dist/monolith/infrastructure/ai/providers/CerebrasProvider.js.map +1 -0
  107. package/dist/monolith/infrastructure/ai/providers/OpenRouterProvider.js +404 -0
  108. package/dist/monolith/infrastructure/ai/providers/OpenRouterProvider.js.map +1 -0
  109. package/dist/monolith/infrastructure/ai/providers/OpenRouterStreamParser.js +283 -0
  110. package/dist/monolith/infrastructure/ai/providers/OpenRouterStreamParser.js.map +1 -0
  111. package/dist/monolith/infrastructure/config/ConfigService.js +398 -0
  112. package/dist/monolith/infrastructure/config/ConfigService.js.map +1 -0
  113. package/dist/monolith/infrastructure/services/MarieMemoryStore.js +140 -0
  114. package/dist/monolith/infrastructure/services/MarieMemoryStore.js.map +1 -0
  115. package/dist/monolith/infrastructure/tools/MarieToolDefinitions.js +1568 -0
  116. package/dist/monolith/infrastructure/tools/MarieToolDefinitions.js.map +1 -0
  117. package/dist/monolith/infrastructure/tools/PureStreamParser.js +147 -0
  118. package/dist/monolith/infrastructure/tools/PureStreamParser.js.map +1 -0
  119. package/dist/monolith/infrastructure/tools/SharedToolDefinitions.js +223 -0
  120. package/dist/monolith/infrastructure/tools/SharedToolDefinitions.js.map +1 -0
  121. package/dist/monolith/infrastructure/tools/ToolRegistry.js +29 -0
  122. package/dist/monolith/infrastructure/tools/ToolRegistry.js.map +1 -0
  123. package/dist/monolith/infrastructure/tools/ToolUtils.js +59 -0
  124. package/dist/monolith/infrastructure/tools/ToolUtils.js.map +1 -0
  125. package/dist/monolith/plumbing/analysis/CodeHealthService.js +146 -0
  126. package/dist/monolith/plumbing/analysis/CodeHealthService.js.map +1 -0
  127. package/dist/monolith/plumbing/analysis/ComplexityService.js +43 -0
  128. package/dist/monolith/plumbing/analysis/ComplexityService.js.map +1 -0
  129. package/dist/monolith/plumbing/analysis/DependencyService.js +51 -0
  130. package/dist/monolith/plumbing/analysis/DependencyService.js.map +1 -0
  131. package/dist/monolith/plumbing/analysis/DiscoveryService.js +49 -0
  132. package/dist/monolith/plumbing/analysis/DiscoveryService.js.map +1 -0
  133. package/dist/monolith/plumbing/analysis/JoyMapService.js +66 -0
  134. package/dist/monolith/plumbing/analysis/JoyMapService.js.map +1 -0
  135. package/dist/monolith/plumbing/analysis/LintService.js +132 -0
  136. package/dist/monolith/plumbing/analysis/LintService.js.map +1 -0
  137. package/dist/monolith/plumbing/analysis/MarieSentinelService.js +276 -0
  138. package/dist/monolith/plumbing/analysis/MarieSentinelService.js.map +1 -0
  139. package/dist/monolith/plumbing/analysis/QualityGuardrailService.js +119 -0
  140. package/dist/monolith/plumbing/analysis/QualityGuardrailService.js.map +1 -0
  141. package/dist/monolith/plumbing/analysis/SurgicalMender.js +70 -0
  142. package/dist/monolith/plumbing/analysis/SurgicalMender.js.map +1 -0
  143. package/dist/monolith/plumbing/analysis/TestService.js +104 -0
  144. package/dist/monolith/plumbing/analysis/TestService.js.map +1 -0
  145. package/dist/monolith/plumbing/filesystem/FileService.js +406 -0
  146. package/dist/monolith/plumbing/filesystem/FileService.js.map +1 -0
  147. package/dist/monolith/plumbing/filesystem/PathResolver.js +26 -0
  148. package/dist/monolith/plumbing/filesystem/PathResolver.js.map +1 -0
  149. package/dist/monolith/plumbing/git/GitService.js +71 -0
  150. package/dist/monolith/plumbing/git/GitService.js.map +1 -0
  151. package/dist/monolith/plumbing/lsp/SymbolService.js +36 -0
  152. package/dist/monolith/plumbing/lsp/SymbolService.js.map +1 -0
  153. package/dist/monolith/plumbing/terminal/ProcessRegistry.js +31 -0
  154. package/dist/monolith/plumbing/terminal/ProcessRegistry.js.map +1 -0
  155. package/dist/monolith/plumbing/terminal/TerminalService.js +180 -0
  156. package/dist/monolith/plumbing/terminal/TerminalService.js.map +1 -0
  157. package/dist/monolith/plumbing/ui/DecorationService.js +54 -0
  158. package/dist/monolith/plumbing/ui/DecorationService.js.map +1 -0
  159. package/dist/monolith/plumbing/utils/ErrorUtils.js +11 -0
  160. package/dist/monolith/plumbing/utils/ErrorUtils.js.map +1 -0
  161. package/dist/monolith/plumbing/utils/JsonUtils.js +360 -0
  162. package/dist/monolith/plumbing/utils/JsonUtils.js.map +1 -0
  163. package/dist/monolith/plumbing/utils/PrefixTree.js +153 -0
  164. package/dist/monolith/plumbing/utils/PrefixTree.js.map +1 -0
  165. package/dist/monolith/plumbing/utils/RetryUtils.js +141 -0
  166. package/dist/monolith/plumbing/utils/RetryUtils.js.map +1 -0
  167. package/dist/monolith/plumbing/utils/StreamTagDetector.js +128 -0
  168. package/dist/monolith/plumbing/utils/StreamTagDetector.js.map +1 -0
  169. package/dist/monolith/plumbing/utils/StringUtils.js +97 -0
  170. package/dist/monolith/plumbing/utils/StringUtils.js.map +1 -0
  171. package/dist/monolith/plumbing/utils/TimeoutUtils.js +21 -0
  172. package/dist/monolith/plumbing/utils/TimeoutUtils.js.map +1 -0
  173. package/dist/monolith/runtime/MarieRuntime.js +354 -0
  174. package/dist/monolith/runtime/MarieRuntime.js.map +1 -0
  175. package/dist/monolith/runtime/RuntimeAdapterBase.js +59 -0
  176. package/dist/monolith/runtime/RuntimeAdapterBase.js.map +1 -0
  177. package/dist/monolith/runtime/providerFactory.js +11 -0
  178. package/dist/monolith/runtime/providerFactory.js.map +1 -0
  179. package/dist/monolith/runtime/types.js +2 -0
  180. package/dist/monolith/runtime/types.js.map +1 -0
  181. package/dist/monolith/services/HealthService.js +38 -0
  182. package/dist/monolith/services/HealthService.js.map +1 -0
  183. package/dist/monolith/services/JoyAutomationService.js +131 -0
  184. package/dist/monolith/services/JoyAutomationService.js.map +1 -0
  185. package/dist/monolith/services/JoyLogService.js +48 -0
  186. package/dist/monolith/services/JoyLogService.js.map +1 -0
  187. package/dist/monolith/services/JoyService.js +190 -0
  188. package/dist/monolith/services/JoyService.js.map +1 -0
  189. package/dist/monolith/services/MarieGhostService.js +168 -0
  190. package/dist/monolith/services/MarieGhostService.js.map +1 -0
  191. package/dist/monolith/services/MarieSCMProvider.js +41 -0
  192. package/dist/monolith/services/MarieSCMProvider.js.map +1 -0
  193. package/package.json +168 -0
@@ -0,0 +1,283 @@
1
+ import { JsonUtils } from "../../../plumbing/utils/JsonUtils.js";
2
+ import { StreamTagDetector } from "../../../plumbing/utils/StreamTagDetector.js";
3
+ export class OpenRouterStreamParser {
4
+ tagDetector;
5
+ llamaToolMode = false;
6
+ thinkingMode = false;
7
+ llamaBufferParts = [];
8
+ toolCalls = {};
9
+ indexCount = 0;
10
+ constructor() {
11
+ this.tagDetector = new StreamTagDetector();
12
+ }
13
+ stripToolWrapperTags(text) {
14
+ if (!text)
15
+ return "";
16
+ return text
17
+ .replace(/<\|tool_calls_section_begin\|>/g, "")
18
+ .replace(/<\|tool_calls_section_end\|>/g, "")
19
+ .replace(/<\|tool_call_begin\|>/g, "")
20
+ .replace(/<\|tool_call_end\|>/g, "")
21
+ .replace(/<\|tool_call_argument_begin\|>/g, "")
22
+ .replace(/<\|tool_call_arguments_begin\|>/g, "")
23
+ .replace(/<tool_call>/g, "")
24
+ .replace(/<\/tool_call>/g, "")
25
+ .replace(/<tool>/g, "")
26
+ .replace(/<\/tool>/g, "")
27
+ .replace(/<function\b[^>]*>/g, "")
28
+ .replace(/<\/function>/g, "")
29
+ .replace(/<function_calls>/g, "")
30
+ .replace(/<\/function_calls>/g, "")
31
+ .trim();
32
+ }
33
+ processContent(chunk) {
34
+ const events = [];
35
+ let currentChunk = chunk;
36
+ while (true) {
37
+ const detection = this.tagDetector.process(currentChunk);
38
+ currentChunk = ""; // Clear chunk so subsequent loops pull from detector buffer
39
+ // 1. Process leading text (if any)
40
+ if (detection.text) {
41
+ if (this.llamaToolMode) {
42
+ this.llamaBufferParts.push(detection.text);
43
+ }
44
+ else if (this.thinkingMode) {
45
+ events.push({ type: "reasoning_delta", text: detection.text });
46
+ }
47
+ else {
48
+ events.push({ type: "content_delta", text: detection.text });
49
+ }
50
+ }
51
+ // 2. Process Tag (if any)
52
+ if (detection.type === "tag") {
53
+ const tag = detection.tag;
54
+ // --- 2a. Thought Transitions ---
55
+ if (tag === "<thought>") {
56
+ this.thinkingMode = true;
57
+ events.push({
58
+ type: "stage_change",
59
+ stage: "thinking",
60
+ label: "Thinking...",
61
+ });
62
+ continue;
63
+ }
64
+ else if (tag === "</thought>") {
65
+ this.thinkingMode = false;
66
+ events.push({
67
+ type: "stage_change",
68
+ stage: "responding",
69
+ label: "Generating response...",
70
+ });
71
+ continue;
72
+ }
73
+ // --- 2b. Bracketed Tool Tags (New) ---
74
+ const isBracketToolTag = tag.startsWith("[Tool Use:");
75
+ const isBracketResultTag = tag.startsWith("[Tool Result:");
76
+ if (isBracketToolTag || isBracketResultTag) {
77
+ // Finalize previous tool if we were in tool mode
78
+ if (this.llamaToolMode) {
79
+ this.finalizeCurrentBuffer(events);
80
+ }
81
+ if (isBracketToolTag) {
82
+ this.llamaToolMode = true;
83
+ this.llamaBufferParts.push(tag);
84
+ events.push({
85
+ type: "stage_change",
86
+ stage: "calling_tool",
87
+ label: "Using tool...",
88
+ });
89
+ }
90
+ else {
91
+ // Treat tool results as content
92
+ events.push({ type: "content_delta", text: tag });
93
+ }
94
+ continue;
95
+ }
96
+ // --- 2c. Tool Transitions (XML/Control Tags) ---
97
+ // Tightened: Avoid matching loose word-tags like <Plan> or <Thought>
98
+ const isExplicitToolTag = tag === "<tool>" ||
99
+ tag === "<|tool_call_begin|>" ||
100
+ tag === "<|tool_calls_section_begin|>" ||
101
+ tag === "<function_calls>" ||
102
+ tag.startsWith("<function") ||
103
+ tag === "<invoke" ||
104
+ tag === "<tool_code>" ||
105
+ tag === "<tool_call>";
106
+ const isDynamicToolTag = /<\|(?!.*_end)[\w_]{3,}\|>/.test(tag) || /^call:\d+>$/.test(tag);
107
+ const isStartTag = isExplicitToolTag || isDynamicToolTag;
108
+ const isEndTag = tag === "</tool>" ||
109
+ tag === "<|tool_call_end|>" ||
110
+ tag === "<|tool_calls_section_end|>" ||
111
+ tag === "</invoke>" ||
112
+ tag === "</function>" ||
113
+ tag === "</function_calls>" ||
114
+ tag === "</tool_code>" ||
115
+ tag === "</tool_call>" ||
116
+ tag === "</call>";
117
+ if (isEndTag) {
118
+ this.llamaBufferParts.push(tag);
119
+ // Validate: Check if we have actual content between section markers
120
+ const bufferContent = this.llamaBufferParts.join("");
121
+ // Check for empty tool section (begin/end with nothing useful between)
122
+ const hasBeginMarker = bufferContent.includes("<|tool_calls_section_begin|>") ||
123
+ bufferContent.includes("<|tool_call_begin|>");
124
+ const hasContentBetween = bufferContent.length > tag.length + 10; // Minimal content check
125
+ if (hasBeginMarker && !hasContentBetween) {
126
+ console.warn("[Marie] Empty tool section detected - no tool content between markers");
127
+ // Emit the buffer as content so user sees what happened
128
+ events.push({ type: "content_delta", text: bufferContent });
129
+ }
130
+ else {
131
+ // Use robust extraction from JsonUtils
132
+ const extracted = JsonUtils.extractToolCall(bufferContent);
133
+ if (extracted) {
134
+ const id = extracted.id || `call_${Date.now()}_${this.indexCount}`;
135
+ events.push({
136
+ type: "tool_call_delta",
137
+ index: this.indexCount,
138
+ id: id,
139
+ name: extracted.name,
140
+ argumentsDelta: JSON.stringify(extracted.input),
141
+ });
142
+ this.toolCalls[this.indexCount] = {
143
+ id: id,
144
+ name: extracted.name,
145
+ arguments: JSON.stringify(extracted.input),
146
+ };
147
+ this.indexCount++;
148
+ this.llamaToolMode = false; // Successfully extracted
149
+ }
150
+ else {
151
+ // Extraction failed. Do NOT leak raw tool wrapper tags to user-visible output.
152
+ // Try to salvage only non-wrapper text.
153
+ const cleaned = this.stripToolWrapperTags(bufferContent);
154
+ if (cleaned) {
155
+ console.warn("[Marie] Tool extraction failed, emitting cleaned fallback content");
156
+ events.push({ type: "content_delta", text: cleaned });
157
+ }
158
+ }
159
+ }
160
+ this.llamaToolMode = false;
161
+ this.llamaBufferParts = [];
162
+ }
163
+ else if (isStartTag) {
164
+ this.llamaToolMode = true;
165
+ events.push({
166
+ type: "stage_change",
167
+ stage: "calling_tool",
168
+ label: "Using tool...",
169
+ });
170
+ this.llamaBufferParts.push(tag);
171
+ }
172
+ else if (this.llamaToolMode) {
173
+ // Preservation: If we are in tool mode, ANY detected tag that isn't a transition
174
+ // must be preserved in the buffer (e.g. <parameter>)
175
+ this.llamaBufferParts.push(tag);
176
+ }
177
+ else {
178
+ // Unknown/non-transition tag: emit back as content delta
179
+ events.push({ type: "content_delta", text: tag });
180
+ }
181
+ // Continue loop to see if there are more tags in the detector buffer
182
+ continue;
183
+ }
184
+ // 3. No more tags in this iteration
185
+ break;
186
+ }
187
+ return events;
188
+ }
189
+ finalizeCurrentBuffer(events) {
190
+ if (this.llamaBufferParts.length === 0)
191
+ return;
192
+ const bufferContent = this.llamaBufferParts.join("");
193
+ const extracted = JsonUtils.extractToolCall(bufferContent);
194
+ if (extracted) {
195
+ const id = extracted.id || `call_${Date.now()}_${this.indexCount}`;
196
+ events.push({
197
+ type: "tool_call_delta",
198
+ index: this.indexCount,
199
+ id: id,
200
+ name: extracted.name,
201
+ argumentsDelta: JSON.stringify(extracted.input),
202
+ });
203
+ this.toolCalls[this.indexCount] = {
204
+ id: id,
205
+ name: extracted.name,
206
+ arguments: JSON.stringify(extracted.input),
207
+ };
208
+ this.indexCount++;
209
+ }
210
+ else {
211
+ const cleaned = this.stripToolWrapperTags(bufferContent);
212
+ if (cleaned) {
213
+ events.push({ type: "content_delta", text: cleaned });
214
+ }
215
+ }
216
+ this.llamaBufferParts = [];
217
+ this.llamaToolMode = false;
218
+ }
219
+ /**
220
+ * Returns any tools collected during the stream.
221
+ */
222
+ getCollectedToolCalls() {
223
+ return this.toolCalls;
224
+ }
225
+ /**
226
+ * Finalizes the parser, attempting to extract any last-minute tool calls
227
+ * from the remaining buffer, especially useful for truncated streams.
228
+ */
229
+ finalize(fullContent) {
230
+ const events = [];
231
+ // 1. If we are in tool mode, try to extract from buffer
232
+ const bufferContent = this.llamaBufferParts.join("");
233
+ let extracted = JsonUtils.extractToolCall(bufferContent);
234
+ // 2. If buffer fails or wasn't applicable, attempt extraction from full content as a last resort
235
+ // This catches models that output raw JSON without any tags at all.
236
+ if (!extracted) {
237
+ extracted = JsonUtils.extractToolCall(fullContent);
238
+ }
239
+ if (extracted) {
240
+ const id = extracted.id || `call_${Date.now()}_${this.indexCount}_final`;
241
+ console.log(`[Marie] Parser: Finalized extraction SUCCESS: ${extracted.name} (ID: ${id})`);
242
+ // Avoid duplicate events if this tool was already emitted during the stream
243
+ const alreadyCollected = Object.values(this.toolCalls).some((tc) => tc.name === extracted.name &&
244
+ (tc.arguments === JSON.stringify(extracted.input) ||
245
+ tc.arguments.includes(JSON.stringify(extracted.input))));
246
+ if (!alreadyCollected) {
247
+ events.push({
248
+ type: "tool_call_delta",
249
+ index: this.indexCount,
250
+ id: id,
251
+ name: extracted.name,
252
+ argumentsDelta: JSON.stringify(extracted.input),
253
+ });
254
+ this.toolCalls[this.indexCount] = {
255
+ id: id,
256
+ name: extracted.name,
257
+ arguments: JSON.stringify(extracted.input),
258
+ };
259
+ this.indexCount++;
260
+ }
261
+ // IMPORTANT: Clear buffer after successful extraction to prevent duplicate emission
262
+ this.llamaBufferParts = [];
263
+ }
264
+ else {
265
+ // CRITICAL: If we were swallowing text but failed to extract a tool,
266
+ // we MUST flush the swallowed text as content so the user doesn't lose it.
267
+ // BUT only if we haven't already processed this content!
268
+ const llamaBuffer = this.llamaBufferParts.join("");
269
+ const cleaned = this.stripToolWrapperTags(llamaBuffer);
270
+ if (cleaned && llamaBuffer !== bufferContent) {
271
+ events.push({ type: "content_delta", text: cleaned });
272
+ }
273
+ else if (cleaned && this.llamaToolMode) {
274
+ // Only emit if we were actually in tool mode (content was being swallowed)
275
+ events.push({ type: "content_delta", text: cleaned });
276
+ }
277
+ }
278
+ this.llamaToolMode = false;
279
+ this.llamaBufferParts = [];
280
+ return events;
281
+ }
282
+ }
283
+ //# sourceMappingURL=OpenRouterStreamParser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"OpenRouterStreamParser.js","sourceRoot":"","sources":["../../../../../src/monolith/infrastructure/ai/providers/OpenRouterStreamParser.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,sCAAsC,CAAC;AACjE,OAAO,EAAE,iBAAiB,EAAE,MAAM,8CAA8C,CAAC;AAEjF,MAAM,OAAO,sBAAsB;IACzB,WAAW,CAAoB;IAC/B,aAAa,GAAY,KAAK,CAAC;IAC/B,YAAY,GAAY,KAAK,CAAC;IAC9B,gBAAgB,GAAa,EAAE,CAAC;IAChC,SAAS,GAGb,EAAE,CAAC;IACC,UAAU,GAAW,CAAC,CAAC;IAE/B;QACE,IAAI,CAAC,WAAW,GAAG,IAAI,iBAAiB,EAAE,CAAC;IAC7C,CAAC;IAEO,oBAAoB,CAAC,IAAY;QACvC,IAAI,CAAC,IAAI;YAAE,OAAO,EAAE,CAAC;QACrB,OAAO,IAAI;aACR,OAAO,CAAC,iCAAiC,EAAE,EAAE,CAAC;aAC9C,OAAO,CAAC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