2025-03-05 15:09:10 +01:00
|
|
|
import { StockbrokerState, StockbrokerUpdate } from "../types";
|
2025-02-27 15:41:47 -08:00
|
|
|
import { ChatOpenAI } from "@langchain/openai";
|
|
|
|
|
import { typedUi } from "@langchain/langgraph-sdk/react-ui/server";
|
|
|
|
|
import type ComponentMap from "../../uis/index";
|
|
|
|
|
import { z } from "zod";
|
|
|
|
|
import { LangGraphRunnableConfig } from "@langchain/langgraph";
|
|
|
|
|
import { findToolCall } from "../../find-tool-call";
|
2025-03-05 18:02:46 -08:00
|
|
|
import { format, subDays } from "date-fns";
|
|
|
|
|
import { Price } from "../../types";
|
|
|
|
|
|
|
|
|
|
async function getPricesForTicker(ticker: string): Promise<{
|
|
|
|
|
oneDayPrices: Price[];
|
|
|
|
|
thirtyDayPrices: Price[];
|
|
|
|
|
}> {
|
|
|
|
|
if (!process.env.FINANCIAL_DATASETS_API_KEY) {
|
|
|
|
|
throw new Error("Financial datasets API key not set");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
|
method: "GET",
|
|
|
|
|
headers: { "X-API-KEY": process.env.FINANCIAL_DATASETS_API_KEY },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const url = "https://api.financialdatasets.ai/prices";
|
|
|
|
|
|
|
|
|
|
const oneMonthAgo = format(subDays(new Date(), 30), "yyyy-MM-dd");
|
|
|
|
|
const now = format(new Date(), "yyyy-MM-dd");
|
|
|
|
|
|
|
|
|
|
const queryParamsOneDay = new URLSearchParams({
|
|
|
|
|
ticker,
|
|
|
|
|
interval: "minute",
|
|
|
|
|
interval_multiplier: "5",
|
|
|
|
|
start_date: now,
|
|
|
|
|
end_date: now,
|
|
|
|
|
limit: "5000",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const queryParamsThirtyDays = new URLSearchParams({
|
|
|
|
|
ticker,
|
|
|
|
|
interval: "minute",
|
|
|
|
|
interval_multiplier: "30",
|
|
|
|
|
start_date: oneMonthAgo,
|
|
|
|
|
end_date: now,
|
|
|
|
|
limit: "5000",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const [resOneDay, resThirtyDays] = await Promise.all([
|
|
|
|
|
fetch(`${url}?${queryParamsOneDay.toString()}`, options),
|
|
|
|
|
fetch(`${url}?${queryParamsThirtyDays.toString()}`, options),
|
|
|
|
|
]);
|
|
|
|
|
if (!resOneDay.ok || !resThirtyDays.ok) {
|
|
|
|
|
throw new Error("Failed to fetch prices");
|
|
|
|
|
}
|
|
|
|
|
const { prices: pricesOneDay } = await resOneDay.json();
|
|
|
|
|
const { prices: pricesThirtyDays } = await resThirtyDays.json();
|
|
|
|
|
|
|
|
|
|
console.log("pricesOneDay", pricesOneDay.length);
|
|
|
|
|
console.log("pricesThirtyDays", pricesThirtyDays.length);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
oneDayPrices: pricesOneDay,
|
|
|
|
|
thirtyDayPrices: pricesThirtyDays,
|
|
|
|
|
};
|
|
|
|
|
}
|
2025-02-27 15:41:47 -08:00
|
|
|
|
|
|
|
|
const llm = new ChatOpenAI({ model: "gpt-4o-mini", temperature: 0 });
|
|
|
|
|
|
|
|
|
|
const getStockPriceSchema = z.object({
|
|
|
|
|
ticker: z.string().describe("The ticker symbol of the company"),
|
|
|
|
|
});
|
|
|
|
|
const getPortfolioSchema = z.object({
|
|
|
|
|
get_portfolio: z.boolean().describe("Should be true."),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const STOCKBROKER_TOOLS = [
|
|
|
|
|
{
|
|
|
|
|
name: "stock-price",
|
|
|
|
|
description: "A tool to get the stock price of a company",
|
|
|
|
|
schema: getStockPriceSchema,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "portfolio",
|
|
|
|
|
description:
|
|
|
|
|
"A tool to get the user's portfolio details. Only call this tool if the user requests their portfolio details.",
|
|
|
|
|
schema: getPortfolioSchema,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
export async function callTools(
|
|
|
|
|
state: StockbrokerState,
|
|
|
|
|
config: LangGraphRunnableConfig,
|
2025-03-05 15:09:10 +01:00
|
|
|
): Promise<StockbrokerUpdate> {
|
2025-02-27 15:41:47 -08:00
|
|
|
const ui = typedUi<typeof ComponentMap>(config);
|
|
|
|
|
|
|
|
|
|
const message = await llm.bindTools(STOCKBROKER_TOOLS).invoke([
|
|
|
|
|
{
|
|
|
|
|
role: "system",
|
|
|
|
|
content:
|
|
|
|
|
"You are a stockbroker agent that uses tools to get the stock price of a company",
|
|
|
|
|
},
|
|
|
|
|
...state.messages,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const stockbrokerToolCall = message.tool_calls?.find(
|
|
|
|
|
findToolCall("stock-price")<typeof getStockPriceSchema>,
|
|
|
|
|
);
|
|
|
|
|
const portfolioToolCall = message.tool_calls?.find(
|
2025-03-05 18:02:46 -08:00
|
|
|
findToolCall("portfolio")<typeof getPortfolioSchema>,
|
2025-02-27 15:41:47 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (stockbrokerToolCall) {
|
2025-03-05 18:02:46 -08:00
|
|
|
const prices = await getPricesForTicker(stockbrokerToolCall.args.ticker);
|
|
|
|
|
ui.write("stock-price", {
|
|
|
|
|
ticker: stockbrokerToolCall.args.ticker,
|
|
|
|
|
...prices,
|
|
|
|
|
});
|
2025-02-27 15:41:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (portfolioToolCall) {
|
|
|
|
|
ui.write("portfolio", {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
2025-02-28 14:15:37 -08:00
|
|
|
messages: [message],
|
2025-03-05 12:09:57 -08:00
|
|
|
ui: ui.collect as StockbrokerUpdate["ui"],
|
2025-02-27 15:41:47 -08:00
|
|
|
timestamp: Date.now(),
|
|
|
|
|
};
|
|
|
|
|
}
|