-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentitlement_example_test.go
More file actions
100 lines (89 loc) · 3.32 KB
/
entitlement_example_test.go
File metadata and controls
100 lines (89 loc) · 3.32 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
package core_test
import . "dappco.re/go"
// ExampleEntitlement_UsagePercent calculates usage percentage through
// `Entitlement.UsagePercent` for usage-gated agent features. Usage checks separate policy
// decisions from the action body.
func ExampleEntitlement_UsagePercent() {
e := Entitlement{Limit: 100, Used: 75}
Println(e.UsagePercent())
// Output: 75
}
// ExampleEntitlement_NearLimit_threshold checks the near-limit threshold through
// `Entitlement.NearLimit` for usage-gated agent features. Usage checks separate policy
// decisions from the action body.
func ExampleEntitlement_NearLimit_threshold() {
e := Entitlement{Limit: 100, Used: 90}
Println(e.NearLimit(0.8))
// Output: true
}
// ExampleEntitlementChecker declares an entitlement checker through `EntitlementChecker`
// for usage-gated agent features. Usage checks separate policy decisions from the action
// body.
func ExampleEntitlementChecker() {
var checker EntitlementChecker = func(action string, quantity int, _ Context) Entitlement {
return Entitlement{Allowed: action == "deploy" && quantity <= 1}
}
Println(checker("deploy", 1, Background()).Allowed)
// Output: true
}
// ExampleUsageRecorder declares a usage recorder through `UsageRecorder` for usage-gated
// agent features. Usage checks separate policy decisions from the action body.
func ExampleUsageRecorder() {
var recorded string
var recorder UsageRecorder = func(action string, quantity int, _ Context) {
recorded = Sprintf("%s:%d", action, quantity)
}
recorder("ai.credits", 3, Background())
Println(recorded)
// Output: ai.credits:3
}
// ExampleCore_Entitled checks an entitlement through `Core.Entitled` for usage-gated agent
// features. Usage checks separate policy decisions from the action body.
func ExampleCore_Entitled() {
c := New()
e := c.Entitled("deploy")
Println(e.Allowed)
Println(e.Unlimited)
// Output:
// true
// true
}
// ExampleCore_SetEntitlementChecker installs an entitlement checker through
// `Core.SetEntitlementChecker` for usage-gated agent features. Usage checks separate
// policy decisions from the action body.
func ExampleCore_SetEntitlementChecker() {
c := New()
c.SetEntitlementChecker(func(action string, qty int, _ Context) Entitlement {
limits := map[string]int{"social.accounts": 5, "ai.credits": 100}
usage := map[string]int{"social.accounts": 3, "ai.credits": 95}
limit, ok := limits[action]
if !ok {
return Entitlement{Allowed: false, Reason: "not in package"}
}
used := usage[action]
remaining := limit - used
if qty > remaining {
return Entitlement{Allowed: false, Limit: limit, Used: used, Remaining: remaining, Reason: "limit exceeded"}
}
return Entitlement{Allowed: true, Limit: limit, Used: used, Remaining: remaining}
})
Println(c.Entitled("social.accounts", 2).Allowed)
Println(c.Entitled("social.accounts", 5).Allowed)
Println(c.Entitled("ai.credits").NearLimit(0.9))
// Output:
// true
// false
// true
}
// ExampleCore_RecordUsage records metered usage through `Core.RecordUsage` for usage-gated
// agent features. Usage checks separate policy decisions from the action body.
func ExampleCore_RecordUsage() {
c := New()
var recorded string
c.SetUsageRecorder(func(action string, qty int, _ Context) {
recorded = Concat(action, ":", Sprint(qty))
})
c.RecordUsage("ai.credits", 10)
Println(recorded)
// Output: ai.credits:10
}