@gilangranggaid/kilua-agent 1.0.3 → 1.0.4
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.
- package/bin/kilua.mjs +116 -84
- package/package.json +1 -1
package/bin/kilua.mjs
CHANGED
|
@@ -5,7 +5,7 @@ import path from "node:path";
|
|
|
5
5
|
import os from "node:os";
|
|
6
6
|
import { execSync } from "node:child_process";
|
|
7
7
|
|
|
8
|
-
// ponytail: single-file agent CLI
|
|
8
|
+
// ponytail: single-file agent CLI supporting real-time streaming for 9router, Anthropic & OpenAI proxies.
|
|
9
9
|
const KILUA_DIR = path.join(os.homedir(), ".kilua");
|
|
10
10
|
const SETTINGS_PATH = path.join(KILUA_DIR, "settings.json");
|
|
11
11
|
const CLAUDE_SETTINGS_PATH = path.join(os.homedir(), ".claude", "settings.json");
|
|
@@ -165,7 +165,7 @@ async function runInteractiveConfig() {
|
|
|
165
165
|
model
|
|
166
166
|
};
|
|
167
167
|
} else {
|
|
168
|
-
console.log("Contoh Base URL: https://tokenharbor.ai/v1
|
|
168
|
+
console.log("Contoh Base URL: https://tokenharbor.ai/v1");
|
|
169
169
|
const baseUrl = (await promptQuestion(rl, "Base URL (https://tokenharbor.ai/v1): ")).trim() || "https://tokenharbor.ai/v1";
|
|
170
170
|
const key = (await promptQuestion(rl, "API Key: ")).trim();
|
|
171
171
|
const model = (await promptQuestion(rl, "Model (deepseek-v4.1-flash:free): ")).trim() || "deepseek-v4.1-flash:free";
|
|
@@ -183,50 +183,39 @@ async function runInteractiveConfig() {
|
|
|
183
183
|
return settings;
|
|
184
184
|
}
|
|
185
185
|
|
|
186
|
-
//
|
|
187
|
-
async function
|
|
188
|
-
const
|
|
189
|
-
const
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
const blk = { ...evt.content_block };
|
|
210
|
-
if (blk.type === "tool_use") blk._raw_json = "";
|
|
211
|
-
blocks[evt.index] = blk;
|
|
212
|
-
} else if (evt.type === "content_block_delta") {
|
|
213
|
-
const idx = evt.index;
|
|
214
|
-
if (!blocks[idx]) blocks[idx] = { type: "text", text: "" };
|
|
215
|
-
if (evt.delta?.type === "text_delta") {
|
|
216
|
-
blocks[idx].text = (blocks[idx].text || "") + (evt.delta.text || "");
|
|
217
|
-
} else if (evt.delta?.type === "input_json_delta") {
|
|
218
|
-
blocks[idx]._raw_json = (blocks[idx]._raw_json || "") + (evt.delta.partial_json || "");
|
|
219
|
-
}
|
|
220
|
-
} else if (evt.type === "content_block_stop") {
|
|
221
|
-
const idx = evt.index;
|
|
222
|
-
if (blocks[idx]?.type === "tool_use" && blocks[idx]._raw_json) {
|
|
223
|
-
try { blocks[idx].input = JSON.parse(blocks[idx]._raw_json); } catch {}
|
|
224
|
-
delete blocks[idx]._raw_json;
|
|
186
|
+
// Stream reader universal untuk SSE (Anthropic & 9router)
|
|
187
|
+
async function readSseStream(res, onChunk) {
|
|
188
|
+
const reader = res.body.getReader();
|
|
189
|
+
const decoder = new TextDecoder("utf-8");
|
|
190
|
+
let buffer = "";
|
|
191
|
+
|
|
192
|
+
while (true) {
|
|
193
|
+
const { done, value } = await reader.read();
|
|
194
|
+
if (done) break;
|
|
195
|
+
buffer += decoder.decode(value, { stream: true });
|
|
196
|
+
const lines = buffer.split("\n");
|
|
197
|
+
buffer = lines.pop() || "";
|
|
198
|
+
|
|
199
|
+
for (const rawLine of lines) {
|
|
200
|
+
const line = rawLine.trim();
|
|
201
|
+
if (!line || line.startsWith("event:") || line.startsWith(":")) continue;
|
|
202
|
+
if (line.startsWith("data:")) {
|
|
203
|
+
const jsonStr = line.slice(5).trim();
|
|
204
|
+
if (!jsonStr || jsonStr === "[DONE]") continue;
|
|
205
|
+
try {
|
|
206
|
+
const parsed = JSON.parse(jsonStr);
|
|
207
|
+
onChunk(parsed);
|
|
208
|
+
} catch {}
|
|
225
209
|
}
|
|
226
210
|
}
|
|
227
211
|
}
|
|
228
212
|
|
|
229
|
-
|
|
213
|
+
if (buffer.trim().startsWith("data:")) {
|
|
214
|
+
try {
|
|
215
|
+
const parsed = JSON.parse(buffer.trim().slice(5).trim());
|
|
216
|
+
onChunk(parsed);
|
|
217
|
+
} catch {}
|
|
218
|
+
}
|
|
230
219
|
}
|
|
231
220
|
|
|
232
221
|
// Loop untuk Anthropic / 9router local (/v1/messages)
|
|
@@ -251,7 +240,7 @@ async function runAnthropicLoop(settings, convo) {
|
|
|
251
240
|
body: JSON.stringify({
|
|
252
241
|
model: settings.model,
|
|
253
242
|
max_tokens: 4096,
|
|
254
|
-
stream:
|
|
243
|
+
stream: true,
|
|
255
244
|
system: SYSTEM,
|
|
256
245
|
messages: convo,
|
|
257
246
|
tools: TOOLS_ANTHROPIC
|
|
@@ -268,20 +257,58 @@ async function runAnthropicLoop(settings, convo) {
|
|
|
268
257
|
return;
|
|
269
258
|
}
|
|
270
259
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
const
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
260
|
+
let printedHeader = false;
|
|
261
|
+
let fullText = "";
|
|
262
|
+
const toolBlocks = [];
|
|
263
|
+
|
|
264
|
+
await readSseStream(res, (evt) => {
|
|
265
|
+
// 1. Text deltas
|
|
266
|
+
let deltaText = "";
|
|
267
|
+
if (typeof evt.delta?.text === "string") deltaText = evt.delta.text;
|
|
268
|
+
else if (typeof evt.delta?.content === "string") deltaText = evt.delta.content;
|
|
269
|
+
else if (evt.choices?.[0]?.delta?.content) deltaText = evt.choices[0].delta.content;
|
|
270
|
+
else if (evt.content_block?.text) deltaText = evt.content_block.text;
|
|
271
|
+
|
|
272
|
+
if (deltaText) {
|
|
273
|
+
if (!printedHeader) {
|
|
274
|
+
process.stdout.write("\r\x1b[2KKILUA > ");
|
|
275
|
+
printedHeader = true;
|
|
276
|
+
}
|
|
277
|
+
process.stdout.write(deltaText);
|
|
278
|
+
fullText += deltaText;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// 2. Tool deltas
|
|
282
|
+
if (evt.type === "content_block_start" && evt.content_block?.type === "tool_use") {
|
|
283
|
+
toolBlocks[evt.index] = { ...evt.content_block, _raw_json: "" };
|
|
284
|
+
} else if (evt.delta?.type === "input_json_delta") {
|
|
285
|
+
const idx = evt.index;
|
|
286
|
+
if (toolBlocks[idx]) {
|
|
287
|
+
toolBlocks[idx]._raw_json = (toolBlocks[idx]._raw_json || "") + (evt.delta.partial_json || "");
|
|
288
|
+
}
|
|
289
|
+
} else if (evt.type === "content_block_stop") {
|
|
290
|
+
const idx = evt.index;
|
|
291
|
+
if (toolBlocks[idx]?._raw_json) {
|
|
292
|
+
try { toolBlocks[idx].input = JSON.parse(toolBlocks[idx]._raw_json); } catch {}
|
|
293
|
+
delete toolBlocks[idx]._raw_json;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
if (printedHeader) console.log();
|
|
299
|
+
else process.stdout.write("\r\x1b[2K");
|
|
277
300
|
|
|
278
|
-
|
|
301
|
+
const activeTools = toolBlocks.filter(Boolean);
|
|
302
|
+
const assistantBlocks = [];
|
|
303
|
+
if (fullText) assistantBlocks.push({ type: "text", text: fullText });
|
|
304
|
+
for (const tb of activeTools) assistantBlocks.push(tb);
|
|
279
305
|
|
|
280
|
-
|
|
281
|
-
|
|
306
|
+
convo.push({ role: "assistant", content: assistantBlocks.length ? assistantBlocks : fullText });
|
|
307
|
+
|
|
308
|
+
if (!activeTools.length) break;
|
|
282
309
|
|
|
283
310
|
const toolResultContent = [];
|
|
284
|
-
for (const tu of
|
|
311
|
+
for (const tu of activeTools) {
|
|
285
312
|
const name = tu.name;
|
|
286
313
|
const args = tu.input || {};
|
|
287
314
|
console.log(`\x1b[36m⚡ Tool: ${name}\x1b[0m(${JSON.stringify(args)})`);
|
|
@@ -295,29 +322,6 @@ async function runAnthropicLoop(settings, convo) {
|
|
|
295
322
|
}
|
|
296
323
|
convo.push({ role: "user", content: toolResultContent });
|
|
297
324
|
}
|
|
298
|
-
console.log();
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
// Parser tangguh untuk OpenAI format
|
|
302
|
-
async function parseOpenAiResponse(res) {
|
|
303
|
-
const text = await res.text();
|
|
304
|
-
const trimmed = text.trim();
|
|
305
|
-
if (!trimmed.startsWith("data:")) {
|
|
306
|
-
try { return JSON.parse(trimmed); } catch {}
|
|
307
|
-
}
|
|
308
|
-
let content = "";
|
|
309
|
-
const lines = text.split("\n");
|
|
310
|
-
for (const line of lines) {
|
|
311
|
-
if (!line.startsWith("data:")) continue;
|
|
312
|
-
const jsonStr = line.slice(5).trim();
|
|
313
|
-
if (!jsonStr || jsonStr === "[DONE]") continue;
|
|
314
|
-
try {
|
|
315
|
-
const parsed = JSON.parse(jsonStr);
|
|
316
|
-
const delta = parsed.choices?.[0]?.delta?.content;
|
|
317
|
-
if (delta) content += delta;
|
|
318
|
-
} catch {}
|
|
319
|
-
}
|
|
320
|
-
return { choices: [{ message: { role: "assistant", content } }] };
|
|
321
325
|
}
|
|
322
326
|
|
|
323
327
|
// Loop untuk OpenAI / TokenHarbor / Gemini OpenAI protocol
|
|
@@ -343,7 +347,13 @@ async function runOpenAiLoop(settings, convo) {
|
|
|
343
347
|
Authorization: `Bearer ${settings.api_key}`,
|
|
344
348
|
"x-goog-api-key": settings.api_key
|
|
345
349
|
},
|
|
346
|
-
body: JSON.stringify({
|
|
350
|
+
body: JSON.stringify({
|
|
351
|
+
model: settings.model,
|
|
352
|
+
stream: true,
|
|
353
|
+
messages: [{ role: "system", content: SYSTEM }, ...convo],
|
|
354
|
+
tools: TOOLS_OPENAI,
|
|
355
|
+
tool_choice: "auto"
|
|
356
|
+
})
|
|
347
357
|
});
|
|
348
358
|
} catch (e) {
|
|
349
359
|
console.error("\n[!] Connection error:", e.message);
|
|
@@ -356,14 +366,37 @@ async function runOpenAiLoop(settings, convo) {
|
|
|
356
366
|
return;
|
|
357
367
|
}
|
|
358
368
|
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
369
|
+
let printedHeader = false;
|
|
370
|
+
let fullText = "";
|
|
371
|
+
const toolCallsMap = {};
|
|
372
|
+
|
|
373
|
+
await readSseStream(res, (evt) => {
|
|
374
|
+
const choice = evt.choices?.[0];
|
|
375
|
+
const delta = choice?.delta;
|
|
376
|
+
if (delta?.content) {
|
|
377
|
+
if (!printedHeader) {
|
|
378
|
+
process.stdout.write("\r\x1b[2KKILUA > ");
|
|
379
|
+
printedHeader = true;
|
|
380
|
+
}
|
|
381
|
+
process.stdout.write(delta.content);
|
|
382
|
+
fullText += delta.content;
|
|
383
|
+
}
|
|
384
|
+
if (delta?.tool_calls) {
|
|
385
|
+
for (const tc of delta.tool_calls) {
|
|
386
|
+
const idx = tc.index ?? 0;
|
|
387
|
+
if (!toolCallsMap[idx]) toolCallsMap[idx] = { id: tc.id, function: { name: tc.function?.name || "", arguments: "" } };
|
|
388
|
+
if (tc.function?.name) toolCallsMap[idx].function.name = tc.function.name;
|
|
389
|
+
if (tc.function?.arguments) toolCallsMap[idx].function.arguments += tc.function.arguments;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
if (printedHeader) console.log();
|
|
395
|
+
else process.stdout.write("\r\x1b[2K");
|
|
363
396
|
|
|
364
|
-
|
|
397
|
+
const toolCalls = Object.values(toolCallsMap);
|
|
398
|
+
convo.push({ role: "assistant", content: fullText, tool_calls: toolCalls.length ? toolCalls : undefined });
|
|
365
399
|
|
|
366
|
-
const toolCalls = message.tool_calls || [];
|
|
367
400
|
if (!toolCalls.length) break;
|
|
368
401
|
|
|
369
402
|
for (const tc of toolCalls) {
|
|
@@ -376,7 +409,6 @@ async function runOpenAiLoop(settings, convo) {
|
|
|
376
409
|
convo.push({ role: "tool", tool_call_id: tc.id, name, content: output });
|
|
377
410
|
}
|
|
378
411
|
}
|
|
379
|
-
console.log();
|
|
380
412
|
}
|
|
381
413
|
|
|
382
414
|
async function runAgent(settings, userPrompt, convo) {
|