feat: bootstrap personal stock agent

This commit is contained in:
2026-05-11 21:10:55 +08:00
commit 437565c16c
21 changed files with 5014 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
import { mkdir, readFile, writeFile } from "node:fs/promises";
import { dirname } from "node:path";
import { normalizeSymbol } from "./symbols";
import type { WatchStock } from "../shared/types";
export class WatchlistStore {
constructor(private readonly path: string) {}
async list(): Promise<WatchStock[]> {
return this.read();
}
async add(symbolInput: string, name?: string): Promise<WatchStock> {
const normalized = normalizeSymbol(symbolInput);
const stocks = await this.read();
const existing = stocks.find((stock) => stock.display === normalized.display);
if (existing) {
return existing;
}
const stock: WatchStock = {
...normalized,
id: normalized.display,
name: name?.trim() || undefined,
addedAt: new Date().toISOString()
};
await this.write([...stocks, stock]);
return stock;
}
async remove(symbolInput: string): Promise<void> {
const normalized = normalizeSymbol(symbolInput);
const stocks = await this.read();
await this.write(stocks.filter((stock) => stock.display !== normalized.display));
}
private async read(): Promise<WatchStock[]> {
try {
const content = await readFile(this.path, "utf8");
const parsed = JSON.parse(content) as WatchStock[];
return Array.isArray(parsed) ? parsed : [];
} catch (error) {
if (isMissingFile(error)) {
return [];
}
throw error;
}
}
private async write(stocks: WatchStock[]): Promise<void> {
await mkdir(dirname(this.path), { recursive: true });
await writeFile(this.path, `${JSON.stringify(stocks, null, 2)}\n`, "utf8");
}
}
function isMissingFile(error: unknown): boolean {
return typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
}