-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexternal-consumer-quickstart.mjs
More file actions
191 lines (165 loc) · 5.75 KB
/
external-consumer-quickstart.mjs
File metadata and controls
191 lines (165 loc) · 5.75 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env node
const HELP = `Civilis Risk OS external consumer quickstart
Canonical replay:
stage fresh intel -> quote -> quote-buy -> claim-proof -> claim -> resolve-proof -> resolve
Core env:
RISK_OS_BASE_URL
RISK_OS_ACTION=quote|quote-buy|claim-proof|claim|resolve-proof|resolve|help
RISK_OS_INTEL_ITEM_ID
RISK_OS_BUYER_AGENT_ID=sage
Auth env for strict proof mode:
RISK_OS_CLAIMANT_TOKEN
RISK_OS_EVALUATOR_TOKEN
Optional signature env:
RISK_OS_CLAIMANT_SIGNATURE
RISK_OS_EVALUATOR_SIGNATURE
Canonical historical path:
16 -> 34 -> 11 -> 10 -> 36
`;
const baseUrl = (process.env.RISK_OS_BASE_URL || 'http://127.0.0.1:3020').replace(/\/$/, '');
const action = process.env.RISK_OS_ACTION || 'quote';
const itemId = Number(process.env.RISK_OS_INTEL_ITEM_ID || '0');
const buyerAgentId = process.env.RISK_OS_BUYER_AGENT_ID || 'sage';
const protectedPurchaseId = Number(process.env.RISK_OS_PROTECTED_PURCHASE_ID || '0');
const claimId = Number(process.env.RISK_OS_CLAIM_ID || '0');
const claimType = process.env.RISK_OS_CLAIM_TYPE || 'misleading_or_invalid_intel';
const claimReason = process.env.RISK_OS_CLAIM_REASON || 'external-consumer-quickstart';
const resolutionDecision = process.env.RISK_OS_RESOLUTION_DECISION || 'refund';
const resolutionReason = process.env.RISK_OS_RESOLUTION_REASON || 'external-consumer-resolution';
const claimantToken = process.env.RISK_OS_CLAIMANT_TOKEN || '';
const claimantSignature = process.env.RISK_OS_CLAIMANT_SIGNATURE || '';
const evaluatorToken = process.env.RISK_OS_EVALUATOR_TOKEN || '';
const evaluatorSignature = process.env.RISK_OS_EVALUATOR_SIGNATURE || '';
async function request(path, options = {}) {
const response = await fetch(`${baseUrl}${path}`, options);
const text = await response.text();
const body = text ? JSON.parse(text) : null;
if (!response.ok) {
throw new Error(`${response.status} ${response.statusText}: ${JSON.stringify(body)}`);
}
return body;
}
async function runQuote() {
if (!itemId) {
throw new Error('Set RISK_OS_INTEL_ITEM_ID from a fresh stage-risk-os-demo run before requesting a quote');
}
return request('/api/risk/quote/intel', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
intelItemId: itemId,
buyerAgentId,
}),
});
}
async function runBuy() {
if (!itemId) {
throw new Error('Set RISK_OS_INTEL_ITEM_ID from a fresh stage-risk-os-demo run before running quote-buy');
}
const quote = await runQuote();
const buy = await request(`/api/intel/items/${itemId}/buy`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
buyerAgentId,
purchaseMode: 'challengeable',
quoteId: quote.quote_id,
}),
});
return { quote, buy };
}
async function runClaimProof() {
const purchaseId = protectedPurchaseId || Number(process.env.RISK_OS_PROTECTED_PURCHASE_ID_REQUIRED || '0');
if (!purchaseId) {
throw new Error('Set RISK_OS_PROTECTED_PURCHASE_ID before requesting claim-proof');
}
const params = new URLSearchParams({
claimType,
reasonText: claimReason,
});
return request(`/api/risk/purchases/${purchaseId}/claim-proof?${params.toString()}`);
}
async function runClaim() {
const purchaseId = protectedPurchaseId || Number(process.env.RISK_OS_PROTECTED_PURCHASE_ID_REQUIRED || '0');
if (!purchaseId) {
throw new Error('Set RISK_OS_PROTECTED_PURCHASE_ID before creating a claim');
}
return request('/api/risk/claims', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...(claimantToken ? { 'x-civilis-risk-claimant-token': claimantToken } : {}),
...(claimantSignature ? { 'x-civilis-risk-claimant-signature': claimantSignature } : {}),
},
body: JSON.stringify({
protectedPurchaseId: purchaseId,
claimType,
reasonText: claimReason,
evidence: {
source: 'external-consumer-quickstart',
},
}),
});
}
async function runResolveProof() {
const id = claimId || Number(process.env.RISK_OS_CLAIM_ID_REQUIRED || '0');
if (!id) {
throw new Error('Set RISK_OS_CLAIM_ID before requesting resolve-proof');
}
const params = new URLSearchParams({
decision: resolutionDecision,
decisionReason: resolutionReason,
});
return request(`/api/risk/claims/${id}/resolve-proof?${params.toString()}`);
}
async function runResolve() {
const id = claimId || Number(process.env.RISK_OS_CLAIM_ID_REQUIRED || '0');
if (!id) {
throw new Error('Set RISK_OS_CLAIM_ID before resolving a claim');
}
return request(`/api/risk/claims/${id}/resolve`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...(evaluatorToken ? { 'x-civilis-risk-evaluator-token': evaluatorToken } : {}),
...(evaluatorSignature ? { 'x-civilis-risk-evaluator-signature': evaluatorSignature } : {}),
},
body: JSON.stringify({
decision: resolutionDecision,
decisionReason: resolutionReason,
}),
});
}
async function main() {
let output;
switch (action) {
case 'help':
console.log(HELP);
return;
case 'quote':
output = await runQuote();
break;
case 'quote-buy':
output = await runBuy();
break;
case 'claim-proof':
output = await runClaimProof();
break;
case 'claim':
output = await runClaim();
break;
case 'resolve-proof':
output = await runResolveProof();
break;
case 'resolve':
output = await runResolve();
break;
default:
throw new Error(`Unsupported RISK_OS_ACTION: ${action}. Set RISK_OS_ACTION=help for usage.`);
}
console.log(JSON.stringify(output, null, 2));
}
main().catch((error) => {
console.error('[external-consumer-quickstart] failed:', error.message);
process.exit(1);
});