@frontmcp/plugins 0.3.1 → 0.4.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/LICENSE +201 -0
- package/package.json +3 -3
- package/src/cache/README.md +186 -0
- package/src/cache/cache.plugin.d.ts +0 -1
- package/src/cache/cache.plugin.js +43 -27
- package/src/cache/cache.plugin.js.map +1 -1
- package/src/cache/cache.symbol.d.ts +2 -2
- package/src/cache/cache.symbol.js.map +1 -1
- package/src/cache/cache.types.js.map +1 -1
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@frontmcp/plugins",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "FrontMCP plugins to extend the SDK",
|
|
5
5
|
"author": "AgentFront <info@agentfront.dev>",
|
|
6
6
|
"homepage": "https://docs.agentfront.dev",
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
}
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"
|
|
35
|
-
"@frontmcp/sdk": "^0.
|
|
34
|
+
"ioredis": "^5.8.0",
|
|
35
|
+
"@frontmcp/sdk": "^0.4.0"
|
|
36
36
|
},
|
|
37
37
|
"type": "commonjs"
|
|
38
38
|
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
# Cache Plugin
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
### Purpose
|
|
6
|
+
|
|
7
|
+
Provides **transparent response caching** for tools based on their input payloads — reducing redundant computation and
|
|
8
|
+
improving response time.
|
|
9
|
+
|
|
10
|
+
### Storage
|
|
11
|
+
|
|
12
|
+
Supports **in-memory** caching out of the box and optional **Redis** backends. Choose `type: 'memory'`, `type: 'redis'`,
|
|
13
|
+
or `type: 'redis-client'` when registering the plugin.
|
|
14
|
+
|
|
15
|
+
### Keying
|
|
16
|
+
|
|
17
|
+
Cache entries are keyed using a **deterministic hash** of each tool’s validated input (`ctx.input`).
|
|
18
|
+
|
|
19
|
+
### Lifecycle Hooks
|
|
20
|
+
|
|
21
|
+
- **Before execution:** `ToolHook.Will('execute')` checks the cache store.
|
|
22
|
+
- **After execution:** `ToolHook.Did('execute')` writes new responses with the resolved TTL.
|
|
23
|
+
|
|
24
|
+
---
|
|
25
|
+
|
|
26
|
+
## 🔄 How It Works
|
|
27
|
+
|
|
28
|
+
1. Before a tool executes, the plugin hashes `ctx.input` and checks the configured store for a cached entry.
|
|
29
|
+
2. On a **cache hit**, the cached output is returned immediately, and a flag
|
|
30
|
+
|
|
31
|
+
```json
|
|
32
|
+
{ "___cached__": true }
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
is added to the response (useful for debugging).
|
|
36
|
+
|
|
37
|
+
3. If no cached entry exists, the tool runs normally, and the output is stored with the resolved TTL.
|
|
38
|
+
4. When `slideWindow` is enabled, each read refreshes the TTL to keep hot entries alive.
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## ⚙️ Requirements
|
|
43
|
+
|
|
44
|
+
- Memory mode (`type: 'memory'` or omitted) needs no external services; data resets when the process restarts.
|
|
45
|
+
- Redis mode (`type: 'redis'`) provisions a `CacheRedisProvider` using your host/port (optionally password/db).
|
|
46
|
+
- Redis client mode (`type: 'redis-client'`) reuses an existing `ioredis` client that you supply.
|
|
47
|
+
- Only enable caching for **deterministic** tools whose outputs depend solely on inputs.
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## 🧩 Registering the Plugin
|
|
52
|
+
|
|
53
|
+
Assume your app class is decorated with `@App` and exposes a `plugins` array.
|
|
54
|
+
|
|
55
|
+
### 1. Default configuration (in-memory, 1-day TTL)
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
plugins: [CachePlugin];
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### 2. Custom default TTL (memory)
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
plugins: [
|
|
65
|
+
CachePlugin.init({
|
|
66
|
+
type: 'memory',
|
|
67
|
+
defaultTTL: 300, // 5 minutes
|
|
68
|
+
}),
|
|
69
|
+
];
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### 3. Redis connection via config
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
plugins: [
|
|
76
|
+
CachePlugin.init({
|
|
77
|
+
type: 'redis',
|
|
78
|
+
defaultTTL: 600,
|
|
79
|
+
config: {
|
|
80
|
+
host: '127.0.0.1',
|
|
81
|
+
port: 6379,
|
|
82
|
+
// password, db optional
|
|
83
|
+
},
|
|
84
|
+
}),
|
|
85
|
+
];
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### 4. Reuse an existing Redis client
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
plugins: [
|
|
92
|
+
CachePlugin.init({
|
|
93
|
+
type: 'redis-client',
|
|
94
|
+
defaultTTL: 900,
|
|
95
|
+
client: existingRedis,
|
|
96
|
+
}),
|
|
97
|
+
];
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
## 🧠 Tool-Level Configuration
|
|
103
|
+
|
|
104
|
+
Caching is **opt-in** per tool. Add the `cache` field in your tool’s metadata.
|
|
105
|
+
|
|
106
|
+
### Minimal example
|
|
107
|
+
|
|
108
|
+
Uses plugin defaults.
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
@Tool({
|
|
112
|
+
name: 'create-expense',
|
|
113
|
+
cache: true,
|
|
114
|
+
})
|
|
115
|
+
export default class CreateExpenseTool extends ToolContext {
|
|
116
|
+
// ...
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Custom TTL and sliding window
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
@Tool({
|
|
124
|
+
name: 'get-expense-by-id',
|
|
125
|
+
cache: {
|
|
126
|
+
ttl: 60, // 1 minute
|
|
127
|
+
slideWindow: true, // refresh TTL on read
|
|
128
|
+
},
|
|
129
|
+
})
|
|
130
|
+
export default class GetExpenseTool extends ToolContext {
|
|
131
|
+
// ...
|
|
132
|
+
}
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
---
|
|
136
|
+
|
|
137
|
+
## ⚖️ Behavior Details
|
|
138
|
+
|
|
139
|
+
| Behavior | Description |
|
|
140
|
+
| ------------------ | ---------------------------------------------------------------------- |
|
|
141
|
+
| **Key Derivation** | Deterministic hash from `ctx.input`. Changing input changes cache key. |
|
|
142
|
+
| **Cache Hits** | Adds `___cached__: true` to the output (for observability only). |
|
|
143
|
+
| **Default TTL** | Plugin `defaultTTL` → falls back to `86400` seconds (1 day). |
|
|
144
|
+
| **Sliding Window** | Extends TTL on reads when `slideWindow` is true. |
|
|
145
|
+
| **Store Choice** | Memory is node-local; Redis enables multi-instance sharing. |
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## 🧹 Invalidation Strategies
|
|
150
|
+
|
|
151
|
+
| Strategy | Use When | Notes |
|
|
152
|
+
| ----------------------- | -------------------------- | ---------------------------------------- |
|
|
153
|
+
| **Time-based** | Data changes often | Use short TTLs |
|
|
154
|
+
| **Input Shaping** | Input determines freshness | Include relevant identifiers in input |
|
|
155
|
+
| **Manual Invalidation** | You need explicit control | Extend or wrap the plugin to delete keys |
|
|
156
|
+
|
|
157
|
+
---
|
|
158
|
+
|
|
159
|
+
## 🧩 Troubleshooting
|
|
160
|
+
|
|
161
|
+
| Symptom | Possible Cause | Fix |
|
|
162
|
+
| --------------------------- | -------------------------------------------- | ------------------------------------------------------------ |
|
|
163
|
+
| No cache hits | Tool missing `cache` config or store offline | Add `cache: {}` to tool metadata and verify store connection |
|
|
164
|
+
| Output unexpectedly cached | Previous result reused | Lower TTL or modify input for unique cache key |
|
|
165
|
+
| Need tenant/session scoping | Same input shared across tenants | Include tenant/session IDs in the input payload |
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
## 🧾 Reference
|
|
170
|
+
|
|
171
|
+
### Plugin options (registration)
|
|
172
|
+
|
|
173
|
+
| Option | Type | Default | Description |
|
|
174
|
+
| ------------ | ------------------------------------------------ | ---------- | -------------------------------------------------- |
|
|
175
|
+
| `type` | `'memory' \| 'redis' \| 'redis-client'` | `'memory'` | Selects the backing store. |
|
|
176
|
+
| `defaultTTL` | `number` | `86400` | Default time-to-live (seconds). |
|
|
177
|
+
| `config` | `{ host: string; port: number; password?; db? }` | — | Redis connection details when `type: 'redis'`. |
|
|
178
|
+
| `client` | `Redis` (from `ioredis`) | — | Existing Redis client when `type: 'redis-client'`. |
|
|
179
|
+
|
|
180
|
+
### Tool metadata (`@Tool` / `tool`)
|
|
181
|
+
|
|
182
|
+
| Option | Type | Description |
|
|
183
|
+
| ------------------- | --------- | ------------------------------------- |
|
|
184
|
+
| `cache` | `true` | Enable caching with plugin defaults. |
|
|
185
|
+
| `cache.ttl` | `number` | Override TTL (seconds) for this tool. |
|
|
186
|
+
| `cache.slideWindow` | `boolean` | Refresh TTL on reads for this tool. |
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { DynamicPlugin, FlowCtxOf, ProviderType } from '@frontmcp/sdk';
|
|
2
2
|
import { CachePluginOptions } from './cache.types';
|
|
3
3
|
export default class CachePlugin extends DynamicPlugin<CachePluginOptions> {
|
|
4
|
-
private readonly defaultTTL;
|
|
5
4
|
static dynamicProviders: (options: CachePluginOptions) => ProviderType[];
|
|
6
5
|
static defaultOptions: CachePluginOptions;
|
|
7
6
|
options: CachePluginOptions;
|
|
@@ -15,37 +15,51 @@ let CachePlugin = CachePlugin_1 = class CachePlugin extends sdk_1.DynamicPlugin
|
|
|
15
15
|
};
|
|
16
16
|
}
|
|
17
17
|
async willReadCache(flowCtx) {
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
const { tool, toolContext } = flowCtx.state;
|
|
19
|
+
if (!tool || !toolContext)
|
|
20
|
+
return;
|
|
21
|
+
const { cache } = toolContext.metadata;
|
|
22
|
+
if (!cache || typeof toolContext.input === 'undefined') {
|
|
23
|
+
// no cache or no input, skip
|
|
21
24
|
return;
|
|
22
25
|
}
|
|
23
|
-
const
|
|
24
|
-
const hash = hashObject(
|
|
25
|
-
const cached = await
|
|
26
|
-
if (
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
26
|
+
const cacheStore = this.get(cache_symbol_1.CacheStoreToken);
|
|
27
|
+
const hash = hashObject({ tool: tool.fullName, input: toolContext.input });
|
|
28
|
+
const cached = await cacheStore.getValue(hash);
|
|
29
|
+
if (cached !== undefined && cached !== null) {
|
|
30
|
+
if (cache === true || (cache.ttl && cache.slideWindow)) {
|
|
31
|
+
const ttl = cache === true ? this.options.defaultTTL : cache.ttl ?? this.options.defaultTTL;
|
|
32
|
+
await cacheStore.setValue(hash, cached, ttl);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* double check if cache still valid based on tool output schema
|
|
36
|
+
*/
|
|
37
|
+
if (!tool.safeParseOutput(cached).success) {
|
|
38
|
+
await cacheStore.delete(hash);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* cache hit, set output to the main flow context
|
|
43
|
+
*/
|
|
44
|
+
flowCtx.state.rawOutput = cached;
|
|
45
|
+
/**
|
|
46
|
+
* call respond to bypass tool execution
|
|
47
|
+
*/
|
|
48
|
+
toolContext.respond(cached);
|
|
36
49
|
}
|
|
37
50
|
}
|
|
38
51
|
async willWriteCache(flowCtx) {
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
52
|
+
const { tool, toolContext } = flowCtx.state;
|
|
53
|
+
if (!tool || !toolContext)
|
|
54
|
+
return;
|
|
55
|
+
const { cache } = toolContext.metadata;
|
|
56
|
+
if (!cache || typeof toolContext.input === 'undefined') {
|
|
42
57
|
return;
|
|
43
58
|
}
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
await redis.setValue(hash, ctx.output, ttl);
|
|
59
|
+
const cacheStore = this.get(cache_symbol_1.CacheStoreToken);
|
|
60
|
+
const ttl = cache === true ? this.options.defaultTTL : cache.ttl ?? this.options.defaultTTL;
|
|
61
|
+
const hash = hashObject({ tool: tool.fullName, input: toolContext.input });
|
|
62
|
+
await cacheStore.setValue(hash, toolContext.output, ttl);
|
|
49
63
|
}
|
|
50
64
|
};
|
|
51
65
|
CachePlugin.dynamicProviders = (options) => {
|
|
@@ -91,7 +105,7 @@ CachePlugin = CachePlugin_1 = tslib_1.__decorate([
|
|
|
91
105
|
providers: [
|
|
92
106
|
/* add providers that always loaded with the plugin or default providers */
|
|
93
107
|
{
|
|
94
|
-
// this is default provider for cache, will be overridden if dynamicProviders based on config
|
|
108
|
+
// this is a default provider for cache, will be overridden if dynamicProviders based on config
|
|
95
109
|
name: 'cache:memory',
|
|
96
110
|
provide: cache_symbol_1.CacheStoreToken,
|
|
97
111
|
useValue: new cache_memory_provider_1.default(60 * 60 * 24),
|
|
@@ -103,14 +117,16 @@ CachePlugin = CachePlugin_1 = tslib_1.__decorate([
|
|
|
103
117
|
exports.default = CachePlugin;
|
|
104
118
|
function hashObject(obj) {
|
|
105
119
|
const keys = Object.keys(obj).sort();
|
|
106
|
-
|
|
107
|
-
|
|
120
|
+
return keys.reduce((acc, key) => {
|
|
121
|
+
acc += key + ':';
|
|
122
|
+
const val = obj[key];
|
|
108
123
|
if (typeof val === 'object' && val !== null) {
|
|
109
124
|
acc += hashObject(val);
|
|
110
125
|
}
|
|
111
126
|
else {
|
|
112
127
|
acc += val;
|
|
113
128
|
}
|
|
129
|
+
acc += ';';
|
|
114
130
|
return acc;
|
|
115
131
|
}, '');
|
|
116
132
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.plugin.js","sourceRoot":"","sources":["../../../src/cache/cache.plugin.ts"],"names":[],"mappings":";;;;AAAA,
|
|
1
|
+
{"version":3,"file":"cache.plugin.js","sourceRoot":"","sources":["../../../src/cache/cache.plugin.ts"],"names":[],"mappings":";;;;AAAA,uCAAyF;AACzF,oGAAkE;AAClE,sGAAoE;AAEpE,iDAAiD;AAelC,IAAM,WAAW,mBAAjB,MAAM,WAAY,SAAQ,mBAAiC;IA4BxE,YAAY,UAA8B,aAAW,CAAC,cAAc;QAClE,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,OAAO,GAAG;YACb,UAAU,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE;YACxB,GAAG,OAAO;SACX,CAAC;IACJ,CAAC;IAGK,AAAN,KAAK,CAAC,aAAa,CAAC,OAAqC;QACvD,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,GAAG,OAAO,CAAC,KAAK,CAAC;QAC5C,IAAI,CAAC,IAAI,IAAI,CAAC,WAAW;YAAE,OAAO;QAElC,MAAM,EAAE,KAAK,EAAE,GAAG,WAAW,CAAC,QAAQ,CAAC;QACvC,IAAI,CAAC,KAAK,IAAI,OAAO,WAAW,CAAC,KAAK,KAAK,WAAW,EAAE,CAAC;YACvD,6BAA6B;YAC7B,OAAO;QACT,CAAC;QACD,MAAM,UAAU,GAAG,IAAI,CAAC,GAAG,CAAC,8BAAe,CAAC,CAAC;QAC7C,MAAM,IAAI,GAAG,UAAU,CAAC,EAAE,IAAI,EAAE,IAAI,C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AAQ,CAAC,GAAG,CAAC,SAAS,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;;;;iDAa3C;AAxFkB,WAAW;IAb/B,IAAA,YAAM,EAAC;QACN,IAAI,EAAE,OAAO;QACb,WAAW,EAAE,uCAAuC;QACpD,SAAS,EAAE;YACT,2EAA2E;YAC3E;gBACE,+FAA+F;gBAC/F,IAAI,EAAE,cAAc;gBACpB,OAAO,EAAE,8BAAe;gBACxB,QAAQ,EAAE,IAAI,+BAAmB,CAAC,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC;aAChD;SACF;KACF,CAAC;;GACmB,WAAW,CAyF/B;kBAzFoB,WAAW;AA2FhC,SAAS,UAAU,CAAC,GAAQ;IAC1B,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;IACrC,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QAC9B,GAAG,IAAI,GAAG,GAAG,GAAG,CAAC;QACjB,MAAM,GAAG,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC;QACrB,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,IAAI,EAAE,CAAC;YAC5C,GAAG,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC;QACzB,CAAC;aAAM,CAAC;YACN,GAAG,IAAI,GAAG,CAAC;QACb,CAAC;QACD,GAAG,IAAI,GAAG,CAAC;QACX,OAAO,GAAG,CAAC;IACb,CAAC,EAAE,EAAE,CAAC,CAAC;AACT,CAAC","sourcesContent":["import { DynamicPlugin, FlowCtxOf, Plugin, ProviderType, ToolHook } from '@frontmcp/sdk';\nimport CacheRedisProvider from './providers/cache-redis.provider';\nimport CacheMemoryProvider from './providers/cache-memory.provider';\nimport { CachePluginOptions } from './cache.types';\nimport { CacheStoreToken } from './cache.symbol';\n\n@Plugin({\n name: 'cache',\n description: 'Cache plugin for caching tool results',\n providers: [\n /* add providers that always loaded with the plugin or default providers */\n {\n // this is a default provider for cache, will be overridden if dynamicProviders based on config\n name: 'cache:memory',\n provide: CacheStoreToken,\n useValue: new CacheMemoryProvider(60 * 60 * 24),\n },\n ],\n})\nexport default class CachePlugin extends DynamicPlugin<CachePluginOptions> {\n static override dynamicProviders = (options: CachePluginOptions) => {\n const providers: ProviderType[] = [];\n switch (options.type) {\n case 'redis':\n case 'redis-client':\n providers.push({\n name: 'cache:redis',\n provide: CacheStoreToken,\n useValue: new CacheRedisProvider(options),\n });\n break;\n case 'memory':\n providers.push({\n name: 'cache:memory',\n provide: CacheStoreToken,\n useValue: new CacheMemoryProvider(options.defaultTTL),\n });\n break;\n }\n return providers;\n };\n\n static defaultOptions: CachePluginOptions = {\n type: 'memory',\n };\n options: CachePluginOptions;\n\n constructor(options: CachePluginOptions = CachePlugin.defaultOptions) {\n super();\n this.options = {\n defaultTTL: 60 * 60 * 24,\n ...options,\n };\n }\n\n @ToolHook.Will('execute', { priority: 1000 })\n async willReadCache(flowCtx: FlowCtxOf<'tools:call-tool'>) {\n const { tool, toolContext } = flowCtx.state;\n if (!tool || !toolContext) return;\n\n const { cache } = toolContext.metadata;\n if (!cache || typeof toolContext.input === 'undefined') {\n // no cache or no input, skip\n return;\n }\n const cacheStore = this.get(CacheStoreToken);\n const hash = hashObject({ tool: tool.fullName, input: toolContext.input });\n const cached = await cacheStore.getValue(hash);\n\n if (cached !== undefined && cached !== null) {\n if (cache === true || (cache.ttl && cache.slideWindow)) {\n const ttl = cache === true ? this.options.defaultTTL : cache.ttl ?? this.options.defaultTTL;\n await cacheStore.setValue(hash, cached, ttl);\n }\n\n /**\n * double check if cache still valid based on tool output schema\n */\n if (!tool.safeParseOutput(cached).success) {\n await cacheStore.delete(hash);\n return;\n }\n /**\n * cache hit, set output to the main flow context\n */\n flowCtx.state.rawOutput = cached;\n\n /**\n * call respond to bypass tool execution\n */\n toolContext.respond(cached);\n }\n }\n\n @ToolHook.Did('execute', { priority: 1000 })\n async willWriteCache(flowCtx: FlowCtxOf<'tools:call-tool'>) {\n const { tool, toolContext } = flowCtx.state;\n if (!tool || !toolContext) return;\n const { cache } = toolContext.metadata;\n if (!cache || typeof toolContext.input === 'undefined') {\n return;\n }\n const cacheStore = this.get(CacheStoreToken);\n const ttl = cache === true ? this.options.defaultTTL : cache.ttl ?? this.options.defaultTTL;\n\n const hash = hashObject({ tool: tool.fullName, input: toolContext.input });\n await cacheStore.setValue(hash, toolContext.output, ttl);\n }\n}\n\nfunction hashObject(obj: any) {\n const keys = Object.keys(obj).sort();\n return keys.reduce((acc, key) => {\n acc += key + ':';\n const val = obj[key];\n if (typeof val === 'object' && val !== null) {\n acc += hashObject(val);\n } else {\n acc += val;\n }\n acc += ';';\n return acc;\n }, '');\n}\n"]}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { CacheStoreInterface } from
|
|
2
|
-
import { Token } from
|
|
1
|
+
import { CacheStoreInterface } from './cache.types';
|
|
2
|
+
import { Token } from '@frontmcp/sdk';
|
|
3
3
|
export declare const CacheStoreToken: Token<CacheStoreInterface>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.symbol.js","sourceRoot":"","sources":["../../../src/cache/cache.symbol.ts"],"names":[],"mappings":";;;AAGa,QAAA,eAAe,GAA+B,MAAM,CAAC,oBAAoB,CAAC,CAAC","sourcesContent":["import {CacheStoreInterface} from
|
|
1
|
+
{"version":3,"file":"cache.symbol.js","sourceRoot":"","sources":["../../../src/cache/cache.symbol.ts"],"names":[],"mappings":";;;AAGa,QAAA,eAAe,GAA+B,MAAM,CAAC,oBAAoB,CAAC,CAAC","sourcesContent":["import { CacheStoreInterface } from './cache.types';\nimport { Token } from '@frontmcp/sdk';\n\nexport const CacheStoreToken: Token<CacheStoreInterface> = Symbol('plugin:cache:store');\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.types.js","sourceRoot":"","sources":["../../../src/cache/cache.types.ts"],"names":[],"mappings":"","sourcesContent":["import {Redis as RedisClient} from 'ioredis';\n\ndeclare global {\n interface ExtendFrontMcpToolMetadata {\n cache?: CachePluginToolOptions | true;\n }\n}\n\nexport interface CachePluginToolOptions {\n /**\n * Time to live in seconds. Default is 1 day.\n */\n ttl?: number; // default 1 day\n\n /**\n * If true, the cache value will be updated with the new value after the TTL.\n * Default is false.\n */\n slideWindow?: boolean;\n}\n\nexport interface BaseCachePluginOptions {\n defaultTTL?: number; // default 1 day\n}\n\nexport interface RedisClientCachePluginOptions extends BaseCachePluginOptions {\n type: 'redis-client';\n client: RedisClient;\n}\n\nexport interface RedisCachePluginOptions extends BaseCachePluginOptions {\n type: 'redis';\n config: {\n host: string;\n port: number;\n password?: string;\n db?: number;\n };\n}\n\nexport type MemoryCachePluginOptions = BaseCachePluginOptions & {\n type: 'memory';\n};\n\nexport type RedisCacheOptions = RedisClientCachePluginOptions | RedisCachePluginOptions;\n\nexport type CachePluginOptions = MemoryCachePluginOptions | RedisCacheOptions;\n\nexport interface CacheStoreInterface {\n setValue(key: string, value: any, ttlSeconds?: number): Promise<void>;\n\n getValue<T = any>(key: string, defaultValue?: T): Promise<T | undefined>;\n\n delete(key: string): Promise<void>;\n\n exists(key: string): Promise<boolean>;\n\n close(): Promise<void>;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"cache.types.js","sourceRoot":"","sources":["../../../src/cache/cache.types.ts"],"names":[],"mappings":"","sourcesContent":["import { Redis as RedisClient } from 'ioredis';\n\ndeclare global {\n interface ExtendFrontMcpToolMetadata {\n cache?: CachePluginToolOptions | true;\n }\n}\n\nexport interface CachePluginToolOptions {\n /**\n * Time to live in seconds. Default is 1 day.\n */\n ttl?: number; // default 1 day\n\n /**\n * If true, the cache value will be updated with the new value after the TTL.\n * Default is false.\n */\n slideWindow?: boolean;\n}\n\nexport interface BaseCachePluginOptions {\n defaultTTL?: number; // default 1 day\n}\n\nexport interface RedisClientCachePluginOptions extends BaseCachePluginOptions {\n type: 'redis-client';\n client: RedisClient;\n}\n\nexport interface RedisCachePluginOptions extends BaseCachePluginOptions {\n type: 'redis';\n config: {\n host: string;\n port: number;\n password?: string;\n db?: number;\n };\n}\n\nexport type MemoryCachePluginOptions = BaseCachePluginOptions & {\n type: 'memory';\n};\n\nexport type RedisCacheOptions = RedisClientCachePluginOptions | RedisCachePluginOptions;\n\nexport type CachePluginOptions = MemoryCachePluginOptions | RedisCacheOptions;\n\nexport interface CacheStoreInterface {\n setValue(key: string, value: any, ttlSeconds?: number): Promise<void>;\n\n getValue<T = any>(key: string, defaultValue?: T): Promise<T | undefined>;\n\n delete(key: string): Promise<void>;\n\n exists(key: string): Promise<boolean>;\n\n close(): Promise<void>;\n}\n"]}
|