-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.ts
More file actions
101 lines (83 loc) · 2.94 KB
/
main.ts
File metadata and controls
101 lines (83 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { readFile } from "node:fs/promises";
import { resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { runAgent } from "./agent.js";
import {
createOllamaExtractor,
extractNumbersDeterministic,
} from "./extract.js";
import { createFilePublisher } from "./publish.js";
import { createMcpPublisher } from "./mcp_publisher.js";
const CURRENT_DIR = fileURLToPath(new URL(".", import.meta.url));
const DEFAULT_OUTPUT_DIR = fileURLToPath(new URL("./out", import.meta.url));
const usage = `Usage:
node --import tsx main.ts --input "12 145 18 1099 31 176 102 87"
node --import tsx main.ts --file ./sample-input.txt
Options:
--input <text> Raw input string
--file <path> Read raw input from file
--threshold <number> Verification threshold, default 0.7
--out <path> Output directory, default ./out
--publisher <mode> file | mcp, default file
--ollama-model <name> Enable real LLM extraction via Ollama
--ollama-url <url> Override Ollama base URL
`;
const parseArgs = (args: string[]) => {
const values: Record<string, string> = {};
for (let index = 0; index < args.length; index += 1) {
const arg = args[index];
if (!arg.startsWith("--")) continue;
const key = arg.slice(2);
const value = args[index + 1];
if (!value || value.startsWith("--")) {
values[key] = "true";
continue;
}
values[key] = value;
index += 1;
}
return values;
};
const isMain = process.argv[1] != null &&
resolve(process.argv[1]) === fileURLToPath(import.meta.url);
if (isMain) {
const args = parseArgs(process.argv.slice(2));
if (args.help === "true") {
console.log(usage);
process.exit(0);
}
const rawInput = args.input
? args.input
: args.file
? await readFile(args.file, "utf8")
: null;
if (!rawInput) {
console.error(usage);
process.exit(1);
}
const threshold = args.threshold ? Number(args.threshold) : 0.7;
const outputDir = args.out ?? DEFAULT_OUTPUT_DIR;
const publisherMode = args.publisher ?? "file";
const extractor = args["ollama-model"]
? createOllamaExtractor({
model: args["ollama-model"],
baseUrl: args["ollama-url"],
})
: extractNumbersDeterministic;
const publish = publisherMode === "mcp"
? createMcpPublisher({ outputDir, cwd: CURRENT_DIR })
: createFilePublisher(outputDir);
const result = await runAgent(rawInput, extractor, publish, threshold);
console.log(JSON.stringify(result, null, 2));
if (publisherMode === "mcp") {
const remoteMode = process.env.BB_PUBLISH_MODE;
if (remoteMode === "ssm") {
const target = process.env.BB_PUBLIC_URL ?? process.env.BB_INSTANCE_ID ?? "remote target";
console.log(`Published result to ${target} through MCP and SSM`);
} else {
console.log(`Published result through MCP to ${outputDir}/index.html`);
}
} else {
console.log(`Wrote output to ${outputDir}/index.html`);
}
}