@profullstack/chat-plugins 0.1.0 → 0.2.0
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/README.md +22 -0
- package/index.d.ts +41 -0
- package/package.json +1 -1
- package/src/index.js +12 -0
- package/src/money.js +372 -0
package/README.md
CHANGED
|
@@ -103,3 +103,25 @@ link-local, CGNAT, multicast and reserved ranges, redirects re-checked hop by ho
|
|
|
103
103
|
## License
|
|
104
104
|
|
|
105
105
|
MIT
|
|
106
|
+
|
|
107
|
+
## Money plugins (0.2.0)
|
|
108
|
+
|
|
109
|
+
`FINANCE_CATALOG` lists Mercury (business banking), CoinPay (crypto payments)
|
|
110
|
+
and moomoo (brokerage and trading); `DEFAULT_PLUGINS` are the ones apps turn
|
|
111
|
+
on for every account (Mercury and CoinPay; each person signs in to their own
|
|
112
|
+
account). The confirmation gate goes in your tool-call layer:
|
|
113
|
+
|
|
114
|
+
```js
|
|
115
|
+
import { applyPolicy, describeAction, policyFor, listTools } from "@profullstack/chat-plugins";
|
|
116
|
+
|
|
117
|
+
const policy = policyFor(plugin.url); // by server, not slug
|
|
118
|
+
const tools = applyPolicy(policy, await listTools(plugin), plugin.tradingMode ?? "paper");
|
|
119
|
+
// Only allowlisted reads run alone; every other tool (and any unknown name)
|
|
120
|
+
// is destructiveHint: true, so needsApproval() stops the loop for the person.
|
|
121
|
+
// describeAction(policy, tool, args) is the approval card: amount, currency,
|
|
122
|
+
// from, to, memo; or symbol, side, qty, type, prices, estimated cost, paper/live.
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
moomoo in paper mode offers reads plus `sim_trade_*` orders only; live order
|
|
126
|
+
tools appear only in `"live"` mode, and still ask. `redactLog()` masks tokens,
|
|
127
|
+
long digit runs and amounts before anything reaches a log.
|
package/index.d.ts
CHANGED
|
@@ -317,3 +317,44 @@ export function fetchJson(
|
|
|
317
317
|
export function parseWwwAuthenticate(header: string): Record<string, string>;
|
|
318
318
|
export function protectedResourceCandidates(resource: URL): string[];
|
|
319
319
|
export function authServerCandidates(issuer: string): string[];
|
|
320
|
+
|
|
321
|
+
// ── money plugins (src/money.js) ────────────────────────────────────────────
|
|
322
|
+
|
|
323
|
+
export type MoneyKind = "banking" | "crypto" | "trading";
|
|
324
|
+
export type TradingMode = "paper" | "live";
|
|
325
|
+
export interface PluginPolicy {
|
|
326
|
+
slug: string;
|
|
327
|
+
kind: MoneyKind;
|
|
328
|
+
/** Tools that only read. Exact names; anything else asks first. */
|
|
329
|
+
readOnly: ReadonlySet<string>;
|
|
330
|
+
/** OAuth scopes to ask for instead of everything the server offers. */
|
|
331
|
+
scopes?: string[];
|
|
332
|
+
/** Brokerage: tools that place, change or cancel PAPER orders. */
|
|
333
|
+
paperWrites?: ReadonlySet<string>;
|
|
334
|
+
}
|
|
335
|
+
export interface FinanceCatalogEntry {
|
|
336
|
+
slug: string;
|
|
337
|
+
name: string;
|
|
338
|
+
url: string;
|
|
339
|
+
description: string;
|
|
340
|
+
homepage: string;
|
|
341
|
+
icon: string;
|
|
342
|
+
/** On for every account (listed and switched on, not signed in). */
|
|
343
|
+
default: boolean;
|
|
344
|
+
}
|
|
345
|
+
export interface ActionSummary {
|
|
346
|
+
title: string;
|
|
347
|
+
lines: { label: string; value: string }[];
|
|
348
|
+
account?: TradingMode;
|
|
349
|
+
note?: string;
|
|
350
|
+
}
|
|
351
|
+
export const MODEL_CAN_BE_WRONG: string;
|
|
352
|
+
export const FINANCE_CATALOG: readonly FinanceCatalogEntry[];
|
|
353
|
+
export const DEFAULT_PLUGINS: readonly FinanceCatalogEntry[];
|
|
354
|
+
export const POLICIES: Record<string, PluginPolicy>;
|
|
355
|
+
export function policyFor(url: string): PluginPolicy | null;
|
|
356
|
+
export function needsConfirmation(policy: PluginPolicy, tool: string): boolean;
|
|
357
|
+
export function visibleInMode(policy: PluginPolicy, tool: string, mode: TradingMode): boolean;
|
|
358
|
+
export function applyPolicy<T extends { name: string; annotations?: object }>(policy: PluginPolicy | null, tools: T[], mode?: TradingMode): T[];
|
|
359
|
+
export function describeAction(policy: PluginPolicy | null, tool: string, args: Record<string, unknown>): ActionSummary | null;
|
|
360
|
+
export function redactLog(v: unknown): string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@profullstack/chat-plugins",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Chat plugins = remote MCP servers, with less hoops than connectors: paste a URL or pick from a catalog, sign-in is automatic (OAuth 2.1 + DCR + PKCE), and an agent loop runs the tools even on models with no native tool calling. Zero dependencies.",
|
|
6
6
|
"keywords": [
|
package/src/index.js
CHANGED
|
@@ -27,3 +27,15 @@ export { toOpenAITools, toolName, buildToolIndex, resolveTool, needsApproval, sl
|
|
|
27
27
|
export { runToolLoop, iterateToolLoop, parseToolCall, normalizeReply, toolPrompt, wrapToolResult } from "./loop.js";
|
|
28
28
|
export { CATALOG, makeCatalog, resolvePluginInput, uniqueSlug } from "./catalog.js";
|
|
29
29
|
export { sealToken, openToken } from "./seal.js";
|
|
30
|
+
export {
|
|
31
|
+
MODEL_CAN_BE_WRONG,
|
|
32
|
+
FINANCE_CATALOG,
|
|
33
|
+
DEFAULT_PLUGINS,
|
|
34
|
+
POLICIES,
|
|
35
|
+
policyFor,
|
|
36
|
+
needsConfirmation,
|
|
37
|
+
visibleInMode,
|
|
38
|
+
applyPolicy,
|
|
39
|
+
describeAction,
|
|
40
|
+
redactLog,
|
|
41
|
+
} from "./money.js";
|
package/src/money.js
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
// Money plugins: catalog entries for banking (Mercury), crypto payments
|
|
2
|
+
// (CoinPay) and brokerage (moomoo), which of them apps turn on by default,
|
|
3
|
+
// and the confirmation gate for their tools.
|
|
4
|
+
//
|
|
5
|
+
// The gate is meant for the app's tool-call layer, not a prompt: for a plugin
|
|
6
|
+
// with a policy, a tool may run on its own only when its exact name is on that
|
|
7
|
+
// plugin's read-only allowlist. applyPolicy() marks every other tool (a write,
|
|
8
|
+
// or a name never seen) destructiveHint: true before the tool loop sees it, so
|
|
9
|
+
// needsApproval() is true and the loop stops for the person. A server's own
|
|
10
|
+
// annotations cannot loosen it. Policies are chosen by server URL, not slug.
|
|
11
|
+
//
|
|
12
|
+
// Shared by c0mpute chat and Infernet chat so the two cannot drift. No Node
|
|
13
|
+
// imports: describeAction() also runs in the browser.
|
|
14
|
+
|
|
15
|
+
export const MODEL_CAN_BE_WRONG = "The model can be wrong. You decide: nothing here runs until you approve it.";
|
|
16
|
+
export const FINANCE_CATALOG = Object.freeze([
|
|
17
|
+
{
|
|
18
|
+
slug: "mercury",
|
|
19
|
+
name: "Mercury (business banking)",
|
|
20
|
+
url: "https://mcp.mercury.com/mcp",
|
|
21
|
+
description: "Your Mercury accounts in chat: balances, transactions, recipients, cards, statements and cash flow. Payment and transfer requests need your approval here and still go through Mercury's own approval. Sign in with your Mercury account.",
|
|
22
|
+
homepage: "https://mercury.com",
|
|
23
|
+
icon: "https://mercury.com/favicon.ico",
|
|
24
|
+
default: true
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
slug: "coinpay",
|
|
28
|
+
name: "CoinPay (crypto payments)",
|
|
29
|
+
url: "https://coinpayportal.com/api/mcp",
|
|
30
|
+
description: "Your CoinPay business in chat: crypto invoices, receivables aging, projects, contractors and pay runs. Creating anything needs your approval first. Sign in with your CoinPay account.",
|
|
31
|
+
homepage: "https://coinpayportal.com",
|
|
32
|
+
icon: "https://coinpayportal.com/favicon.ico",
|
|
33
|
+
default: true
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
slug: "moomoo",
|
|
37
|
+
name: "moomoo (brokerage and trading)",
|
|
38
|
+
url: "https://mcp.moomoo.com/mcp",
|
|
39
|
+
description: "Quotes, charts, screeners, news and your moomoo positions, balances and orders. Starts on paper trading; switching to live is your call, and every order needs your approval. The model can be wrong and you decide. Sign in with your moomoo account.",
|
|
40
|
+
homepage: "https://www.moomoo.com",
|
|
41
|
+
icon: "https://www.moomoo.com/favicon.ico",
|
|
42
|
+
default: false
|
|
43
|
+
}
|
|
44
|
+
]);
|
|
45
|
+
export const DEFAULT_PLUGINS = FINANCE_CATALOG.filter((e) => e.default);
|
|
46
|
+
const MERCURY_READ = new Set([
|
|
47
|
+
"getCurrentDate",
|
|
48
|
+
"getAccount",
|
|
49
|
+
"getAccounts",
|
|
50
|
+
"getAccountCards",
|
|
51
|
+
"getAccountStatements",
|
|
52
|
+
"getTransaction",
|
|
53
|
+
"getTransactionById",
|
|
54
|
+
"listTransactions",
|
|
55
|
+
"getCashflowSummary",
|
|
56
|
+
"getAutoTransferRules",
|
|
57
|
+
"listCategories",
|
|
58
|
+
"listCredit",
|
|
59
|
+
"getCreditAccountStatements",
|
|
60
|
+
"getOrganization",
|
|
61
|
+
"getRecipient",
|
|
62
|
+
"getRecipients",
|
|
63
|
+
"getRecipientInvite",
|
|
64
|
+
"listRecipientInvites",
|
|
65
|
+
"listRecipientsAttachments",
|
|
66
|
+
"getTreasury",
|
|
67
|
+
"getTreasuryTransactions",
|
|
68
|
+
"getInvest",
|
|
69
|
+
"getInvestActivity",
|
|
70
|
+
"getCard",
|
|
71
|
+
"listCards",
|
|
72
|
+
"listMerchants",
|
|
73
|
+
"getUser",
|
|
74
|
+
"getUsers",
|
|
75
|
+
"getWebhook",
|
|
76
|
+
"getWebhooks",
|
|
77
|
+
"listSendMoneyApprovalRequests",
|
|
78
|
+
"getSendMoneyApprovalRequest",
|
|
79
|
+
"listTransferMoneyApprovalRequests",
|
|
80
|
+
"getTransferMoneyApprovalRequest",
|
|
81
|
+
"listTasks",
|
|
82
|
+
"getTask",
|
|
83
|
+
"getSafeRequest",
|
|
84
|
+
"getSafeRequests",
|
|
85
|
+
"getAttachment",
|
|
86
|
+
"getCustomer",
|
|
87
|
+
"listCustomers",
|
|
88
|
+
"listProducts",
|
|
89
|
+
"getInvoice",
|
|
90
|
+
"listInvoices",
|
|
91
|
+
"listInvoiceAttachments",
|
|
92
|
+
"listInvoicePayments",
|
|
93
|
+
"listBudgets",
|
|
94
|
+
"getBudget",
|
|
95
|
+
"getBudgetMembers",
|
|
96
|
+
"getBudgetExpenditures",
|
|
97
|
+
"listReceipts",
|
|
98
|
+
"getReceipt",
|
|
99
|
+
"listReimbursements",
|
|
100
|
+
"getReimbursement",
|
|
101
|
+
"getBooksInstances",
|
|
102
|
+
"getBooksSourceAccounts",
|
|
103
|
+
"getBooksTransactions",
|
|
104
|
+
"getBooksTransaction",
|
|
105
|
+
"getBooksLedgers",
|
|
106
|
+
"getBooksLedger",
|
|
107
|
+
"getBooksLedgerBalances",
|
|
108
|
+
"getBooksLineEntries",
|
|
109
|
+
"listBooksJournalEntries",
|
|
110
|
+
"retrieveBooksJournalEntry",
|
|
111
|
+
"getBooksTagGroups",
|
|
112
|
+
"getBooksTags",
|
|
113
|
+
"getBooksCategorizationRules",
|
|
114
|
+
"getBooksProfitAndLoss",
|
|
115
|
+
"getBooksBalanceSheet",
|
|
116
|
+
"getBooksCashFlow",
|
|
117
|
+
"getBooksBills",
|
|
118
|
+
"getBooksBill",
|
|
119
|
+
"getBooksInvoices",
|
|
120
|
+
"getBooksInvoice"
|
|
121
|
+
]);
|
|
122
|
+
const COINPAY_READ = new Set([
|
|
123
|
+
"invoice_list",
|
|
124
|
+
"invoice_get",
|
|
125
|
+
"invoice_ar_aging",
|
|
126
|
+
"project_list",
|
|
127
|
+
"project_get",
|
|
128
|
+
"contractor_list",
|
|
129
|
+
"contractor_get",
|
|
130
|
+
"contractor_work_list",
|
|
131
|
+
"pay_run_list",
|
|
132
|
+
"pay_run_get",
|
|
133
|
+
"document_list",
|
|
134
|
+
"document_get"
|
|
135
|
+
]);
|
|
136
|
+
const MOOMOO_READ = new Set([
|
|
137
|
+
"account_authorized_trd_accs",
|
|
138
|
+
"account_funds",
|
|
139
|
+
"account_positions",
|
|
140
|
+
"account_orders_active",
|
|
141
|
+
"account_orders_history",
|
|
142
|
+
"account_orders_detail",
|
|
143
|
+
"account_order_fills_today",
|
|
144
|
+
"account_fills_history",
|
|
145
|
+
"account_trading_info",
|
|
146
|
+
"sim_trade_account_list",
|
|
147
|
+
"sim_trade_cash_info",
|
|
148
|
+
"sim_trade_position_list",
|
|
149
|
+
"sim_trade_history_order_list",
|
|
150
|
+
"sim_trade_max_buy_sell",
|
|
151
|
+
"quote_market_snapshot",
|
|
152
|
+
"quote_market_state",
|
|
153
|
+
"quote_stock_quote",
|
|
154
|
+
"quote_stock_feed",
|
|
155
|
+
"quote_stock_basicinfo",
|
|
156
|
+
"quote_stock_screen",
|
|
157
|
+
"quote_option_screen",
|
|
158
|
+
"quote_warrant_screen",
|
|
159
|
+
"quote_rt_ticker",
|
|
160
|
+
"quote_rt_data",
|
|
161
|
+
"quote_order_book",
|
|
162
|
+
"quote_cur_kline",
|
|
163
|
+
"quote_history_kline",
|
|
164
|
+
"quote_trading_days",
|
|
165
|
+
"quote_top_ten_brokers",
|
|
166
|
+
"quote_capital_flow",
|
|
167
|
+
"quote_capital_flow_history",
|
|
168
|
+
"quote_capital_distribution",
|
|
169
|
+
"quote_plate_list",
|
|
170
|
+
"quote_plate_stock",
|
|
171
|
+
"quote_owner_plate",
|
|
172
|
+
"quote_referencefuture_list",
|
|
173
|
+
"quote_future_info",
|
|
174
|
+
"quote_option_chain",
|
|
175
|
+
"quote_option_expiration_date",
|
|
176
|
+
"quote_option_volatility",
|
|
177
|
+
"quote_option_exercise_probability",
|
|
178
|
+
"quote_user_security",
|
|
179
|
+
"quote_user_security_group",
|
|
180
|
+
"quote_valuation_detail",
|
|
181
|
+
"quote_valuation_plate_stock_list",
|
|
182
|
+
"quote_valuation_index_stock_plate_list",
|
|
183
|
+
"quote_valuation_index_component_stock_list",
|
|
184
|
+
"quote_financials_statements",
|
|
185
|
+
"quote_financials_revenue_breakdown",
|
|
186
|
+
"quote_financials_earnings_price_history",
|
|
187
|
+
"quote_company_profile",
|
|
188
|
+
"quote_company_executives",
|
|
189
|
+
"quote_company_executive_background",
|
|
190
|
+
"quote_company_operational_efficiency",
|
|
191
|
+
"quote_corporate_actions_dividends",
|
|
192
|
+
"quote_corporate_actions_stock_splits",
|
|
193
|
+
"quote_corporate_actions_buybacks",
|
|
194
|
+
"quote_corporate_actions_rehab",
|
|
195
|
+
"quote_shareholders_overview",
|
|
196
|
+
"quote_shareholders_institutional",
|
|
197
|
+
"quote_shareholders_holding_changes",
|
|
198
|
+
"quote_shareholders_holder_detail",
|
|
199
|
+
"quote_insider_trade_list",
|
|
200
|
+
"quote_insider_holder_list",
|
|
201
|
+
"quote_short_interest",
|
|
202
|
+
"quote_daily_short_volume",
|
|
203
|
+
"quote_research_rating_summary",
|
|
204
|
+
"quote_research_analyst_consensus",
|
|
205
|
+
"quote_research_morningstar_report",
|
|
206
|
+
"quote_news_search",
|
|
207
|
+
"quote_community_search",
|
|
208
|
+
"quote_economic_calendar_search",
|
|
209
|
+
"quote_economic_calendar_hot",
|
|
210
|
+
"quote_ipo_list_us",
|
|
211
|
+
"quote_ipo_list_hk",
|
|
212
|
+
"quote_ipo_list_cn",
|
|
213
|
+
"quote_ipo_list_sg",
|
|
214
|
+
"quote_ipo_list_my"
|
|
215
|
+
]);
|
|
216
|
+
const MOOMOO_PAPER_WRITES = new Set(["sim_trade_input_order", "sim_trade_modify_order", "sim_trade_cancel_order"]);
|
|
217
|
+
export const POLICIES = {
|
|
218
|
+
mercury: {
|
|
219
|
+
slug: "mercury",
|
|
220
|
+
kind: "banking",
|
|
221
|
+
readOnly: MERCURY_READ,
|
|
222
|
+
scopes: ["read", "offline_access", "transactions:request", "transfers:request"]
|
|
223
|
+
},
|
|
224
|
+
coinpay: { slug: "coinpay", kind: "crypto", readOnly: COINPAY_READ },
|
|
225
|
+
moomoo: { slug: "moomoo", kind: "trading", readOnly: MOOMOO_READ, paperWrites: MOOMOO_PAPER_WRITES }
|
|
226
|
+
};
|
|
227
|
+
function parse(u) {
|
|
228
|
+
try {
|
|
229
|
+
return new URL(u);
|
|
230
|
+
} catch {
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
export function policyFor(url) {
|
|
235
|
+
const u = parse(url);
|
|
236
|
+
if (!u)
|
|
237
|
+
return null;
|
|
238
|
+
const host = u.hostname.toLowerCase().replace(/\.$/, "");
|
|
239
|
+
if (host === "mcp.mercury.com")
|
|
240
|
+
return POLICIES.mercury;
|
|
241
|
+
if (host === "mcp.moomoo.com")
|
|
242
|
+
return POLICIES.moomoo;
|
|
243
|
+
if (host === "coinpayportal.com" || host === "www.coinpayportal.com")
|
|
244
|
+
return POLICIES.coinpay;
|
|
245
|
+
return null;
|
|
246
|
+
}
|
|
247
|
+
export function needsConfirmation(policy, tool) {
|
|
248
|
+
return !policy.readOnly.has(tool);
|
|
249
|
+
}
|
|
250
|
+
export function visibleInMode(policy, tool, mode) {
|
|
251
|
+
if (policy.kind !== "trading" || mode === "live")
|
|
252
|
+
return true;
|
|
253
|
+
return policy.readOnly.has(tool) || !!policy.paperWrites?.has(tool);
|
|
254
|
+
}
|
|
255
|
+
export function applyPolicy(policy, tools, mode = "paper") {
|
|
256
|
+
if (!policy)
|
|
257
|
+
return tools;
|
|
258
|
+
return tools.filter((t) => visibleInMode(policy, t.name, mode)).map((t) => needsConfirmation(policy, t.name) ? { ...t, annotations: { ...t.annotations ?? {}, readOnlyHint: false, destructiveHint: true } } : { ...t, annotations: { ...t.annotations ?? {}, readOnlyHint: true, destructiveHint: false } });
|
|
259
|
+
}
|
|
260
|
+
const str = (v) => v === undefined || v === null ? "" : typeof v === "string" ? v : typeof v === "number" || typeof v === "boolean" ? String(v) : JSON.stringify(v);
|
|
261
|
+
function pick(args, names) {
|
|
262
|
+
const norm = (k) => k.toLowerCase().replace(/[_-]/g, "");
|
|
263
|
+
const want = names.map(norm);
|
|
264
|
+
const scan = (o) => {
|
|
265
|
+
for (const w of want) {
|
|
266
|
+
for (const [k, v] of Object.entries(o))
|
|
267
|
+
if (norm(k) === w && v !== undefined && v !== null && v !== "")
|
|
268
|
+
return str(v);
|
|
269
|
+
}
|
|
270
|
+
return "";
|
|
271
|
+
};
|
|
272
|
+
const top = scan(args);
|
|
273
|
+
if (top)
|
|
274
|
+
return top;
|
|
275
|
+
for (const v of Object.values(args)) {
|
|
276
|
+
if (v && typeof v === "object" && !Array.isArray(v)) {
|
|
277
|
+
const inner = scan(v);
|
|
278
|
+
if (inner)
|
|
279
|
+
return inner;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return "";
|
|
283
|
+
}
|
|
284
|
+
function humanTool(tool) {
|
|
285
|
+
const s = tool.replace(/([a-z])([A-Z])/g, "$1 $2").replace(/_/g, " ").toLowerCase();
|
|
286
|
+
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
287
|
+
}
|
|
288
|
+
const num = (s) => {
|
|
289
|
+
if (!s)
|
|
290
|
+
return null;
|
|
291
|
+
const n = Number(String(s).replace(/,/g, ""));
|
|
292
|
+
return Number.isFinite(n) ? n : null;
|
|
293
|
+
};
|
|
294
|
+
function tradingSummary(tool, args) {
|
|
295
|
+
const paper = tool.startsWith("sim_trade_");
|
|
296
|
+
const lines = [];
|
|
297
|
+
const add = (label, value) => {
|
|
298
|
+
if (value)
|
|
299
|
+
lines.push({ label, value });
|
|
300
|
+
};
|
|
301
|
+
const isCancel = /cancel/.test(tool);
|
|
302
|
+
const isChange = /modify|replace/.test(tool);
|
|
303
|
+
const isPlace = !isCancel && !isChange && /order|place|confirm/.test(tool);
|
|
304
|
+
const title = isCancel ? "Cancel order" : isChange ? "Change order" : /confirm/.test(tool) ? "Confirm order" : isPlace ? "Place order" : humanTool(tool);
|
|
305
|
+
add("Symbol", pick(args, ["code", "symbol", "ticker", "security"]));
|
|
306
|
+
add("Side", pick(args, ["side", "trd_side", "direction"]));
|
|
307
|
+
const qty = pick(args, ["qty", "quantity", "new_qty", "shares"]);
|
|
308
|
+
add("Quantity", qty);
|
|
309
|
+
const type = pick(args, ["order_type", "type"]);
|
|
310
|
+
add("Order type", type);
|
|
311
|
+
const price = pick(args, ["price", "limit_price", "new_price"]);
|
|
312
|
+
add("Limit price", price);
|
|
313
|
+
add("Stop / trigger price", pick(args, ["aux_price", "stop_price", "trigger_price"]));
|
|
314
|
+
add("Time in force", pick(args, ["time_in_force", "tif"]));
|
|
315
|
+
add("Order id", pick(args, ["order_id", "confirm_id"]));
|
|
316
|
+
if (isPlace || isChange) {
|
|
317
|
+
const q = num(qty);
|
|
318
|
+
const p = num(price);
|
|
319
|
+
lines.push({
|
|
320
|
+
label: "Estimated cost",
|
|
321
|
+
value: q !== null && p !== null && !/MARKET/i.test(type) ? `${(q * p).toLocaleString("en-US", { maximumFractionDigits: 4 })} (quantity x price, before fees; options and futures also multiply by the contract size)` : "at the market price, not known until it fills"
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
add("Account id", pick(args, ["acc_id", "account_id"]));
|
|
325
|
+
lines.push({ label: "Account", value: paper ? "PAPER (simulated, no real money)" : "LIVE (real money)" });
|
|
326
|
+
return { title, lines, account: paper ? "paper" : "live", note: MODEL_CAN_BE_WRONG };
|
|
327
|
+
}
|
|
328
|
+
function moneySummary(kind, tool, args) {
|
|
329
|
+
const lines = [];
|
|
330
|
+
const add = (label, value) => {
|
|
331
|
+
if (value)
|
|
332
|
+
lines.push({ label, value });
|
|
333
|
+
};
|
|
334
|
+
let amount = pick(args, ["amount", "amountUsd", "value", "total", "paymentAmount"]);
|
|
335
|
+
let currency = pick(args, ["currency", "currencyCode", "asset", "coin"]);
|
|
336
|
+
const items = Array.isArray(args.lineItems) ? args.lineItems : null;
|
|
337
|
+
if (!amount && items) {
|
|
338
|
+
const t = items.reduce((s, i) => s + (Number(i?.quantity) || 0) * (Number(i?.unit_price) || 0), 0);
|
|
339
|
+
amount = `${t.toLocaleString("en-US", { maximumFractionDigits: 8 })} before tax and discount (${items.length} line item${items.length === 1 ? "" : "s"})`;
|
|
340
|
+
if (!currency)
|
|
341
|
+
currency = "USD";
|
|
342
|
+
}
|
|
343
|
+
if (typeof args.minutes === "number")
|
|
344
|
+
add("Time", `${args.minutes} minutes`);
|
|
345
|
+
add("Amount", amount);
|
|
346
|
+
add("Currency", currency);
|
|
347
|
+
add("From", pick(args, ["fromAccountId", "sourceAccountId", "from", "accountId", "fromAccount", "businessId", "fromWallet"]));
|
|
348
|
+
add("To", pick(args, ["recipientId", "toAccountId", "destinationAccountId", "to", "recipient", "toAddress", "address", "clientId", "contractorId", "projectId"]));
|
|
349
|
+
add("Memo", pick(args, ["memo", "note", "notes", "externalMemo", "description", "purpose", "remark"]));
|
|
350
|
+
add("Payment method", pick(args, ["paymentMethod", "method", "network", "chain"]));
|
|
351
|
+
return {
|
|
352
|
+
title: humanTool(tool),
|
|
353
|
+
lines,
|
|
354
|
+
note: kind === "banking" ? "Mercury payment and transfer requests still need an approver in Mercury." : MODEL_CAN_BE_WRONG
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
export function describeAction(policy, tool, args) {
|
|
358
|
+
if (!policy)
|
|
359
|
+
return null;
|
|
360
|
+
const a = args && typeof args === "object" ? args : {};
|
|
361
|
+
return policy.kind === "trading" ? tradingSummary(tool, a) : moneySummary(policy.kind, tool, a);
|
|
362
|
+
}
|
|
363
|
+
export function redactLog(v) {
|
|
364
|
+
const s = v instanceof Error ? `${v.name}: ${v.message}` : typeof v === "string" ? v : (() => {
|
|
365
|
+
try {
|
|
366
|
+
return JSON.stringify(v);
|
|
367
|
+
} catch {
|
|
368
|
+
return String(v);
|
|
369
|
+
}
|
|
370
|
+
})();
|
|
371
|
+
return s.replace(/(bearer\s+)\S+/gi, "$1[redacted]").replace(/("?(?:access_token|refresh_token|id_token|client_secret|code|code_verifier|token|password|secret)"?\s*[:=]\s*"?)[^"&\s,}]+/gi, "$1[redacted]").replace(/\beyJ[\w-]+\.[\w-]+\.[\w-]+/g, "[jwt]").replace(/[A-Za-z0-9_\-+/=]{32,}/g, "[opaque]").replace(/\d[\d -]{6,}\d/g, "[number]").replace(/[$€£]\s?\d[\d,]*(\.\d+)?/g, "[amount]").slice(0, 500);
|
|
372
|
+
}
|