natureco-cli 5.7.0 → 5.7.1

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.
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Streaming tool-call delta accumulator.
3
+ *
4
+ * OpenAI-compatible providers stream tool calls as a sequence of partial
5
+ * `delta.tool_calls` chunks. Each chunk carries an `index`, optionally an
6
+ * `id`, and pieces of `function.name` / `function.arguments` that must be
7
+ * concatenated per index before the JSON arguments can be parsed.
8
+ *
9
+ * Broken (do-not-do) pattern, which crashes on multi-chunk arguments:
10
+ *
11
+ * const tc = delta.tool_calls[0];
12
+ * JSON.parse(tc.function.arguments); // SyntaxError on incomplete JSON
13
+ *
14
+ * Correct pattern (this module): keep a per-index buffer keyed on `index`,
15
+ * concat `name` and `arguments` as strings, JSON-parse only after `[DONE]`.
16
+ *
17
+ * See `~/.hermes/skills/llm-provider-integration/references/
18
+ * minimax-401-tool-calling-lessons.md` Lesson 2 for the failure-mode that
19
+ * motivated this. Both `src/utils/api.js` (non-REPL stream path) and
20
+ * `src/commands/repl.js` (REPL stream path) use this helper so the two
21
+ * implementations cannot drift.
22
+ */
23
+
24
+ /**
25
+ * Apply one streaming delta chunk to the per-index buffer.
26
+ * Mutates `buffer` in place and returns it for chaining.
27
+ *
28
+ * @param {Array<object>} buffer per-index accumulator (created externally,
29
+ * index = position; sparse entries fine)
30
+ * @param {Array<object>} deltas `parsed.choices[0].delta.tool_calls`
31
+ * @returns {Array<object>} the same buffer
32
+ */
33
+ function accumulateToolCallDeltas(buffer, deltas) {
34
+ if (!Array.isArray(deltas)) return buffer;
35
+ for (const tcDelta of deltas) {
36
+ if (!tcDelta || typeof tcDelta.index !== 'number') continue;
37
+ const idx = tcDelta.index;
38
+ if (!buffer[idx]) {
39
+ buffer[idx] = {
40
+ index: idx,
41
+ id: '',
42
+ type: 'function',
43
+ function: { name: '', arguments: '' },
44
+ };
45
+ }
46
+ const slot = buffer[idx];
47
+ if (tcDelta.id) slot.id = tcDelta.id;
48
+ if (tcDelta.type) slot.type = tcDelta.type;
49
+ if (tcDelta.function?.name) slot.function.name += tcDelta.function.name;
50
+ if (tcDelta.function?.arguments) slot.function.arguments += tcDelta.function.arguments;
51
+ }
52
+ return buffer;
53
+ }
54
+
55
+ /**
56
+ * Finalize accumulated buffer into the shape an OpenAI-compatible
57
+ * `assistant.tool_calls` array expects. Filters incomplete entries
58
+ * (missing name or id) and synthesizes ids when absent.
59
+ *
60
+ * @param {Array<object>} buffer from accumulateToolCallDeltas
61
+ * @returns {Array<{id:string,type:string,function:{name:string,arguments:string}}>}
62
+ */
63
+ function finalizeToolCalls(buffer) {
64
+ const out = [];
65
+ for (let i = 0; i < buffer.length; i++) {
66
+ const slot = buffer[i];
67
+ if (!slot || !slot.function.name) continue;
68
+ out.push({
69
+ id: slot.id || `call_${Date.now()}_${i}`,
70
+ type: slot.type || 'function',
71
+ function: { name: slot.function.name, arguments: slot.function.arguments || '' },
72
+ });
73
+ }
74
+ return out;
75
+ }
76
+
77
+ /**
78
+ * Parse the JSON arguments of a finalized tool call, returning {} on
79
+ * failure so callers don't have to repeat the try/catch.
80
+ *
81
+ * @param {string} argsJson
82
+ * @returns {object}
83
+ */
84
+ function parseToolArgs(argsJson) {
85
+ if (!argsJson || typeof argsJson !== 'string') return {};
86
+ try {
87
+ return JSON.parse(argsJson);
88
+ } catch {
89
+ return {};
90
+ }
91
+ }
92
+
93
+ module.exports = {
94
+ accumulateToolCallDeltas,
95
+ finalizeToolCalls,
96
+ parseToolArgs,
97
+ };
@@ -105,10 +105,12 @@ function toOpenAIFormat(toolDefs) {
105
105
  return toolDefs
106
106
  .filter(t => !['brave_search','brave-web-search','google_search','web_search','browse','open','search','shell','bash_command','execute_command','run_command','sql','query','lookup'].includes(t.name))
107
107
  .map(t => {
108
- // Alias varsa degistir
108
+ // Alias varsa degistir. Önceki kod ALIAS_MAP'i declare ediyordu ama
109
+ // değiştirmek için TOOL_ALIASES kullanıyordu (undefined) — alias hit
110
+ // olduğunda ReferenceError ile çöküyordu. ALIAS_MAP'e döndürüldü.
109
111
  const ALIAS_MAP = { 'brave_search':'duckduckgo_search','brave-web-search':'duckduckgo_search','google_search':'duckduckgo_search','web_search':'duckduckgo_search','browse':'browser','shell':'bash','bash_command':'bash','execute_command':'bash','run_command':'bash' };
110
112
  if (ALIAS_MAP[t.name]) {
111
- t = { ...t, name: TOOL_ALIASES[t.name] };
113
+ t = { ...t, name: ALIAS_MAP[t.name] };
112
114
  }
113
115
  // v5.4.21: Groq uyumluluk - additionalProperties: false kaldirildi
114
116
  // ve gereksiz kisitlamalar temizlendi