@animalabs/agent-framework 0.4.1 → 0.5.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/dist/src/gate/event-gate.d.ts +27 -0
- package/dist/src/gate/event-gate.d.ts.map +1 -1
- package/dist/src/gate/event-gate.js +286 -22
- package/dist/src/gate/event-gate.js.map +1 -1
- package/dist/src/gate/types.d.ts +65 -0
- package/dist/src/gate/types.d.ts.map +1 -1
- package/dist/test/event-gate-rate-limit.test.d.ts +2 -0
- package/dist/test/event-gate-rate-limit.test.d.ts.map +1 -0
- package/dist/test/event-gate-rate-limit.test.js +627 -0
- package/dist/test/event-gate-rate-limit.test.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/gate/event-gate.ts +309 -22
- package/src/gate/types.ts +66 -1
package/dist/src/gate/types.d.ts
CHANGED
|
@@ -5,9 +5,50 @@
|
|
|
5
5
|
* against declarative policies to decide whether they should trigger inference.
|
|
6
6
|
* Policies are read from a `gate.json` file with hot-reload support.
|
|
7
7
|
*/
|
|
8
|
+
/**
|
|
9
|
+
* Token-bucket configuration for the `rate_limit` behavior.
|
|
10
|
+
*
|
|
11
|
+
* Each matching event consumes one token; events that arrive with an empty
|
|
12
|
+
* bucket are denied (decision: trigger=false). Tokens regenerate at one per
|
|
13
|
+
* `refillIntervalMs`, capped at `tokens`. Buckets are partitioned by the
|
|
14
|
+
* value of `keyBy` resolved against event metadata — so a per-user limit
|
|
15
|
+
* uses `keyBy: "senderId"`, a per-channel limit uses `keyBy: "channelId"`,
|
|
16
|
+
* etc. With no `keyBy` (or when the resolved value is missing), one shared
|
|
17
|
+
* bucket is used for the policy.
|
|
18
|
+
*/
|
|
19
|
+
export interface RateLimitConfig {
|
|
20
|
+
/** Max bucket capacity, also the initial fill. Must be > 0. */
|
|
21
|
+
tokens: number;
|
|
22
|
+
/** Milliseconds between token regenerations. Must be > 0. */
|
|
23
|
+
refillIntervalMs: number;
|
|
24
|
+
/**
|
|
25
|
+
* Metadata field name to partition buckets by. If absent or the metadata
|
|
26
|
+
* field is missing on a given event, all events share one bucket.
|
|
27
|
+
*/
|
|
28
|
+
keyBy?: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Counter configuration for the `passive_sample` behavior.
|
|
32
|
+
*
|
|
33
|
+
* Increments a counter on every match. When the counter reaches `every`,
|
|
34
|
+
* the policy fires (trigger=true) and the counter resets to zero. Useful
|
|
35
|
+
* for "decide whether to act every Nth observation" — chat passive
|
|
36
|
+
* sampling, periodic analysis of sensor streams, etc. Counters are
|
|
37
|
+
* partitioned by `keyBy` the same way `rate_limit` buckets are.
|
|
38
|
+
*/
|
|
39
|
+
export interface PassiveSampleConfig {
|
|
40
|
+
/** Trigger every N-th match. Must be > 0. */
|
|
41
|
+
every: number;
|
|
42
|
+
/** Optional metadata field name for separate per-key counters. */
|
|
43
|
+
keyBy?: string;
|
|
44
|
+
}
|
|
8
45
|
/** How a matching policy affects inference triggering. */
|
|
9
46
|
export type GateBehavior = 'always' | 'skip' | {
|
|
10
47
|
debounce: number;
|
|
48
|
+
} | {
|
|
49
|
+
rate_limit: RateLimitConfig;
|
|
50
|
+
} | {
|
|
51
|
+
passive_sample: PassiveSampleConfig;
|
|
11
52
|
};
|
|
12
53
|
/** Criteria for matching an event. All specified fields must match (AND logic). */
|
|
13
54
|
export interface GatePolicyMatch {
|
|
@@ -32,12 +73,26 @@ export interface GatePolicyMatch {
|
|
|
32
73
|
* Supports `*` wildcard; not full gitignore syntax.
|
|
33
74
|
*/
|
|
34
75
|
pathGlob?: string;
|
|
76
|
+
/**
|
|
77
|
+
* Match when ANY listed metadata field is truthy (Boolean(metadata[name])).
|
|
78
|
+
* Useful for OR-ing flag-style metadata, e.g.:
|
|
79
|
+
* metadataTrue: ["isMention", "isPrivate", "isReplyToBot"]
|
|
80
|
+
* matches if any of those is set. Empty/omitted = no metadata constraint.
|
|
81
|
+
*/
|
|
82
|
+
metadataTrue?: string[];
|
|
35
83
|
}
|
|
36
84
|
/** A single gate policy — one rule in the ordered policy list. */
|
|
37
85
|
export interface GatePolicy {
|
|
38
86
|
name: string;
|
|
39
87
|
match: GatePolicyMatch;
|
|
40
88
|
behavior: GateBehavior;
|
|
89
|
+
/**
|
|
90
|
+
* Names of other policies whose state to clear when THIS policy fires
|
|
91
|
+
* (decision.trigger === true). Lets you express interactions like "a
|
|
92
|
+
* direct invocation should reset the passive-sample counter", without
|
|
93
|
+
* coupling the behaviors to each other in code. Unknown names are ignored.
|
|
94
|
+
*/
|
|
95
|
+
resets?: string[];
|
|
41
96
|
}
|
|
42
97
|
/** Top-level gate configuration, stored in gate.json. */
|
|
43
98
|
export interface GateConfig {
|
|
@@ -83,6 +138,16 @@ export interface GatePolicyStats {
|
|
|
83
138
|
pendingCount: number;
|
|
84
139
|
nextDeliveryMs: number | null;
|
|
85
140
|
};
|
|
141
|
+
/** Present for `rate_limit` behaviors. */
|
|
142
|
+
rateLimitState?: {
|
|
143
|
+
bucketCount: number;
|
|
144
|
+
deniedCount: number;
|
|
145
|
+
};
|
|
146
|
+
/** Present for `passive_sample` behaviors. */
|
|
147
|
+
passiveSampleState?: {
|
|
148
|
+
counterCount: number;
|
|
149
|
+
fireCount: number;
|
|
150
|
+
};
|
|
86
151
|
}
|
|
87
152
|
/** Full gate status returned by gate:status tool. */
|
|
88
153
|
export interface GateStatus {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/gate/types.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAMH,0DAA0D;AAC1D,MAAM,MAAM,YAAY,GACpB,QAAQ,GACR,MAAM,GACN;IAAE,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/gate/types.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAMH;;;;;;;;;;GAUG;AACH,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,EAAE,MAAM,CAAC;QACrB,SAAS,EAAE,MAAM,CAAC;KACnB,CAAC;CACH;AAED,qDAAqD;AACrD,MAAM,WAAW,UAAU;IACzB,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE,MAAM,GAAG,SAAS,GAAG,SAAS,CAAC;IAC7C,mBAAmB,EAAE,MAAM,GAAG,IAAI,CAAC;IACnC,OAAO,EAAE,QAAQ,GAAG,MAAM,CAAC;IAC3B,QAAQ,EAAE,eAAe,EAAE,CAAC;IAC5B,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,yDAAyD;IACzD,gBAAgB,EAAE,MAAM,CAAC;IACzB;;;;;;OAMG;IACH,gBAAgB,EAAE;QAChB,SAAS,EAAE,MAAM,CAAC;QAClB,OAAO,EAAE,MAAM,CAAC;QAChB,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE;YAAE,SAAS,EAAE,MAAM,CAAC;YAAC,OAAO,EAAE,MAAM,CAAA;SAAE,CAAC,CAAC;KACrE,CAAC;CACH"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event-gate-rate-limit.test.d.ts","sourceRoot":"","sources":["../../test/event-gate-rate-limit.test.ts"],"names":[],"mappings":""}
|