38 lines
1.7 KiB
TypeScript
38 lines
1.7 KiB
TypeScript
// 验证 DuckDB 处理 Excel:加载 data.xlsx 并按部门统计金额
|
||
const dec = new TextDecoder();
|
||
|
||
async function chat(messages: any[]): Promise<string> {
|
||
let url = "http://127.0.0.1:3100/api/chat";
|
||
let body: any = { messages };
|
||
let total = "", guard = 0;
|
||
while (guard++ < 8) {
|
||
const resp = await fetch(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) });
|
||
const reader = resp.body!.getReader();
|
||
let buf = "", out = "", approval: any = null;
|
||
while (true) {
|
||
const { done, value } = await reader.read();
|
||
if (done) break;
|
||
buf += dec.decode(value, { stream: true });
|
||
const lines = buf.split("\n"); buf = lines.pop() || "";
|
||
for (const line of lines) {
|
||
const t = line.trim(); if (!t.startsWith("data:")) continue;
|
||
let ev: any; try { ev = JSON.parse(t.slice(5).trim()); } catch { continue; }
|
||
if (ev.type === "token") out += ev.content;
|
||
else if (ev.type === "toolcall") console.log(" [tool]", ev.toolcall.function.name);
|
||
else if (ev.type === "approval_required") { approval = ev; }
|
||
else if (ev.type === "error") console.log(" [error]", ev.content);
|
||
}
|
||
}
|
||
total += out;
|
||
if (!approval) break;
|
||
url = "http://127.0.0.1:3100/api/approve";
|
||
body = { approved: true, tool_call_id: approval.tool_call_id };
|
||
}
|
||
return total;
|
||
}
|
||
|
||
console.log("=== test: DuckDB analyze Excel ===");
|
||
const r = await chat([{ role: "user", content: "Load data.xlsx with load_excel, then use query_duckdb to sum 金额 by 部门 (department)." }]);
|
||
console.log("\nfinal reply (first 600):\n", r.slice(0, 600));
|
||
console.log("\nverdict:", /技术/.test(r) && /销售|市场/.test(r) ? "PASS: DuckDB group-by works" : "check reply");
|