@alveus-ai/std 0.1.58 → 0.1.60
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/examples/action-gate-demo.ts +161 -0
- package/package.json +2 -2
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Action Gate Demo: a verified refund
|
|
3
|
+
*
|
|
4
|
+
* A support agent proposes a refund. Before anything runs, the proposal is
|
|
5
|
+
* frozen and checked against a refund policy by a pinned scorer. The refund
|
|
6
|
+
* executes ONLY when every check passes, with the exact payload that was
|
|
7
|
+
* checked, and a postcondition check then confirms the refund took effect.
|
|
8
|
+
* An over-limit refund is revised once (lowered to the limit) and re-checked.
|
|
9
|
+
*
|
|
10
|
+
* Policy:
|
|
11
|
+
* - refunds up to $500
|
|
12
|
+
* - the transaction is within the 30-day refund window
|
|
13
|
+
* - a reason is given
|
|
14
|
+
* - refunds over $200 need an approval on record
|
|
15
|
+
*
|
|
16
|
+
* Every terminal run yields an Action Receipt written by the platform, bound to
|
|
17
|
+
* hashes of the exact code and payload, which the agent can't forge.
|
|
18
|
+
*
|
|
19
|
+
* Usage:
|
|
20
|
+
* alveus deploy
|
|
21
|
+
* alveus run @yourscope/action-gate-demo --input '{"amount": 120, "daysSincePurchase": 5, "reason": "Duplicate charge"}'
|
|
22
|
+
* alveus run @yourscope/action-gate-demo --input '{"amount": 800, "daysSincePurchase": 10, "reason": "Item arrived damaged", "approvalRecorded": true}'
|
|
23
|
+
* alveus run @yourscope/action-gate-demo --input '{"amount": 90, "daysSincePurchase": 45, "reason": "Changed my mind"}'
|
|
24
|
+
* alveus receipt <execution-id>
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
import { agent$ } from '@alveus-ai/core';
|
|
28
|
+
import type { ExecutionContext, ScorerInput, ScorerOutput } from '@alveus-ai/core';
|
|
29
|
+
import { actionGate } from '@alveus-ai/std';
|
|
30
|
+
|
|
31
|
+
const REFUND_LIMIT = 500;
|
|
32
|
+
const REFUND_WINDOW_DAYS = 30;
|
|
33
|
+
const APPROVAL_OVER = 200;
|
|
34
|
+
|
|
35
|
+
interface RefundInput {
|
|
36
|
+
amount: number;
|
|
37
|
+
daysSincePurchase: number;
|
|
38
|
+
reason?: string;
|
|
39
|
+
/** A recorded approval flag the gate checks. Not a live approval step. */
|
|
40
|
+
approvalRecorded?: boolean;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Scorer: deterministic policy checks over the frozen proposal. It only
|
|
45
|
+
* measures; it never decides whether the refund runs.
|
|
46
|
+
*/
|
|
47
|
+
const policyScorer = agent$(async (_state, input: ScorerInput, _ctx: ExecutionContext) => {
|
|
48
|
+
const proposal = input.subject as { input?: RefundInput };
|
|
49
|
+
const refund = proposal?.input ?? ({} as RefundInput);
|
|
50
|
+
const amount = refund.amount;
|
|
51
|
+
|
|
52
|
+
const check = (id: string): { verdict: boolean; reasoning: string } => {
|
|
53
|
+
if (id === 'within_limit') {
|
|
54
|
+
const ok = typeof amount === 'number' && amount > 0 && amount <= REFUND_LIMIT;
|
|
55
|
+
return {
|
|
56
|
+
verdict: ok,
|
|
57
|
+
reasoning: ok
|
|
58
|
+
? `$${amount} is within the $${REFUND_LIMIT} limit`
|
|
59
|
+
: `$${amount} is over the $${REFUND_LIMIT} limit`,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
if (id === 'transaction_eligible') {
|
|
63
|
+
const days = refund.daysSincePurchase;
|
|
64
|
+
const ok = typeof days === 'number' && days >= 0 && days <= REFUND_WINDOW_DAYS;
|
|
65
|
+
return {
|
|
66
|
+
verdict: ok,
|
|
67
|
+
reasoning: ok
|
|
68
|
+
? `Purchased ${days} days ago, inside the ${REFUND_WINDOW_DAYS}-day window`
|
|
69
|
+
: `Purchased ${days} days ago, outside the ${REFUND_WINDOW_DAYS}-day window`,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
if (id === 'reason_given') {
|
|
73
|
+
const ok = typeof refund.reason === 'string' && refund.reason.trim().length > 0;
|
|
74
|
+
return { verdict: ok, reasoning: ok ? 'A reason is on the proposal' : 'No reason was given' };
|
|
75
|
+
}
|
|
76
|
+
if (id === 'approval_recorded') {
|
|
77
|
+
const needed = typeof amount === 'number' && amount > APPROVAL_OVER;
|
|
78
|
+
const ok = !needed || refund.approvalRecorded === true;
|
|
79
|
+
return {
|
|
80
|
+
verdict: ok,
|
|
81
|
+
reasoning: !needed
|
|
82
|
+
? `No approval needed at $${APPROVAL_OVER} or less`
|
|
83
|
+
: ok
|
|
84
|
+
? 'An approval is on record'
|
|
85
|
+
: `Over $${APPROVAL_OVER} needs an approval on record`,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
return { verdict: false, reasoning: 'Unknown check' };
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const output: ScorerOutput = {
|
|
92
|
+
results: input.criteria.map((c) => ({
|
|
93
|
+
criterionId: c.criterionId,
|
|
94
|
+
...check(c.criterionId),
|
|
95
|
+
method: 'deterministic',
|
|
96
|
+
})),
|
|
97
|
+
};
|
|
98
|
+
return output;
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Execute step: issues the refund. It runs ONLY when every check passed, with
|
|
103
|
+
* the exact frozen payload that was checked. In production this would call a
|
|
104
|
+
* payments API via `ctx.fetch`; keep it idempotent so a retry never refunds twice.
|
|
105
|
+
*/
|
|
106
|
+
const issueRefund = agent$(
|
|
107
|
+
async (_state, payload: { action: string; input: RefundInput }, _ctx: ExecutionContext) => {
|
|
108
|
+
return { refunded: true, amount: payload.input.amount, currency: 'USD' };
|
|
109
|
+
},
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Revise step: lowers an over-limit refund to the limit so it can be
|
|
114
|
+
* re-checked. It cannot change the purchase date, add a reason or record an
|
|
115
|
+
* approval, so those proposals are re-checked unchanged and denied.
|
|
116
|
+
*/
|
|
117
|
+
const lowerToLimit = agent$(
|
|
118
|
+
async (_state, event: { action: string; input: RefundInput; feedback: string[] }) => {
|
|
119
|
+
return { input: { ...event.input, amount: Math.min(event.input.amount, REFUND_LIMIT) } };
|
|
120
|
+
},
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
/** Postcondition: confirms the refund was recorded for the amount that was approved. */
|
|
124
|
+
const refundRecorded = agent$(async (_state, input: ScorerInput, _ctx: ExecutionContext) => {
|
|
125
|
+
const subject = input.subject as {
|
|
126
|
+
input?: RefundInput;
|
|
127
|
+
result?: { refunded?: boolean; amount?: number };
|
|
128
|
+
};
|
|
129
|
+
const ok = subject?.result?.refunded === true && subject.result.amount === subject.input?.amount;
|
|
130
|
+
const output: ScorerOutput = {
|
|
131
|
+
results: input.criteria.map((c) => ({
|
|
132
|
+
criterionId: c.criterionId,
|
|
133
|
+
verdict: ok,
|
|
134
|
+
reasoning: ok
|
|
135
|
+
? `A refund of $${subject?.result?.amount} is on record`
|
|
136
|
+
: 'No matching refund is on record',
|
|
137
|
+
method: 'deterministic',
|
|
138
|
+
})),
|
|
139
|
+
};
|
|
140
|
+
return output;
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
const gate = actionGate({
|
|
144
|
+
criteria: [
|
|
145
|
+
{ id: 'within_limit' },
|
|
146
|
+
{ id: 'transaction_eligible' },
|
|
147
|
+
{ id: 'reason_given' },
|
|
148
|
+
{ id: 'approval_recorded' },
|
|
149
|
+
],
|
|
150
|
+
scorer: policyScorer,
|
|
151
|
+
execute: issueRefund,
|
|
152
|
+
revise: lowerToLimit,
|
|
153
|
+
maxRevisions: 1,
|
|
154
|
+
verifyCriteria: [{ id: 'refund_recorded' }],
|
|
155
|
+
verify: refundRecorded,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
/** Deployable root: proposes the refund to the gate, labelled by its amount. */
|
|
159
|
+
export const verifiedRefund = agent$((_state, input: RefundInput, ctx: ExecutionContext) =>
|
|
160
|
+
ctx.call(gate, { action: 'issue-refund', input }, { label: `refund $${input?.amount}` }),
|
|
161
|
+
);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alveus-ai/std",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.60",
|
|
4
4
|
"description": "Standard library of reusable agents and utilities for Alveus",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"access": "public"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"zod": "^4.
|
|
49
|
+
"zod": "^4.6.7"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
52
52
|
"@alveus-ai/core": "^0.1.0"
|