-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprompt-compression.ts
More file actions
128 lines (101 loc) · 3.37 KB
/
Copy pathprompt-compression.ts
File metadata and controls
128 lines (101 loc) · 3.37 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import "server-only";
import { createHash } from "node:crypto";
const DEFAULT_PROMPT = "Optimize my portfolio for best yield with low risk.";
function normalizeWhitespace(value: string) {
return value.replace(/\s+/g, " ").trim();
}
function pickIntentLabel(prompt: string) {
const lowered = prompt.toLowerCase();
if (/stable|stability|drawdown|defensive|safe|safer|low risk/.test(lowered)) {
return "low-risk yield optimization";
}
if (/rebalance|rotate|shift|move/.test(lowered)) {
return "portfolio rebalance for higher yield";
}
if (/highest|max|maximize|aggressive|best yield/.test(lowered)) {
return "maximum yield optimization";
}
return "balanced yield optimization";
}
function pickConstraintLabel(prompt: string) {
const lowered = prompt.toLowerCase();
if (/low risk|safe|safer|defensive/.test(lowered)) {
return "preserve downside protection";
}
if (/stablecoin|usdc|liquidity/.test(lowered)) {
return "keep liquidity available";
}
if (/mainnet/.test(lowered)) {
return "use active mainnet route";
}
return "prioritize efficient APY lift";
}
function formatHoldingValue(value: number) {
if (value >= 1000) {
return value.toFixed(0);
}
if (value >= 1) {
return value.toFixed(2);
}
return value.toFixed(4);
}
function buildPortfolioHash(portfolio: Record<string, number>) {
const normalized = Object.entries(portfolio)
.filter(([, value]) => value > 0)
.sort(([left], [right]) => left.localeCompare(right));
return createHash("sha256")
.update(JSON.stringify(normalized))
.digest("hex");
}
export interface CompressedOptimizationInput {
normalizedPrompt: string;
compactPrompt: string;
promptDigest: string;
portfolioDigest: string;
portfolioSummary: string;
portfolioSignature: string;
requestDocument: string;
}
export function compressOptimizationInput(
portfolio: Record<string, number>,
prompt?: string,
): CompressedOptimizationInput {
const sanitizedPrompt = normalizeWhitespace(prompt || DEFAULT_PROMPT).slice(0, 240);
const normalizedPrompt = sanitizedPrompt || DEFAULT_PROMPT;
const promptDigest = createHash("sha256")
.update(normalizedPrompt.toLowerCase())
.digest("hex");
const sortedHoldings = Object.entries(portfolio)
.filter(([, value]) => value > 0)
.sort((left, right) => right[1] - left[1]);
const topHoldings = sortedHoldings
.slice(0, 5)
.map(([symbol, value]) => `${symbol}:${formatHoldingValue(value)}`)
.join(" | ");
const totalValue = sortedHoldings.reduce((sum, [, value]) => sum + value, 0);
const portfolioSummary = sortedHoldings.length > 0
? `Total ${formatHoldingValue(totalValue)}; Holdings ${topHoldings}`
: "Total 0; Holdings none";
const portfolioSignature = sortedHoldings
.map(([symbol]) => symbol)
.join("|") || "empty";
const intent = pickIntentLabel(normalizedPrompt);
const constraint = pickConstraintLabel(normalizedPrompt);
const compactPrompt = normalizeWhitespace(
[
`Intent: ${intent}.`,
`Constraint: ${constraint}.`,
`Portfolio: ${portfolioSummary}.`,
`User request: ${normalizedPrompt}.`,
].join(" "),
);
return {
normalizedPrompt,
compactPrompt,
promptDigest,
portfolioDigest: buildPortfolioHash(portfolio),
portfolioSummary,
portfolioSignature,
requestDocument: `${compactPrompt} Signature: ${portfolioSignature}.`,
};
}