create-guardio 0.0.10 → 0.1.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.
Files changed (2) hide show
  1. package/index.mjs +65 -10
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -184,7 +184,7 @@ async function main() {
184
184
  guardio:
185
185
  "node --import tsx ./node_modules/@guardiojs/guardio/dist/cli.js --config guardio.config.ts",
186
186
  },
187
- dependencies: { "@guardiojs/guardio": "*" },
187
+ dependencies: { "@guardiojs/guardio": "*", zod: "^3.23.0" },
188
188
  devDependencies: {
189
189
  typescript: "^5.6.0",
190
190
  "@types/node": "^22.0.0",
@@ -238,27 +238,59 @@ export default config;
238
238
 
239
239
  if (addExamplePlugin) {
240
240
  await mkdir(resolve(guardioPath, "plugins", "example"), { recursive: true });
241
- const examplePluginContent = `import type {
241
+ const examplePluginContent = `import { z } from "zod";
242
+ import type {
243
+ PolicyPluginDefinition,
242
244
  PolicyPluginInterface,
243
245
  PolicyRequestContext,
244
246
  PolicyResult,
247
+ PolicyPluginContext,
245
248
  } from "@guardiojs/guardio";
246
249
 
250
+ // Config schema for dashboard validation (optional)
251
+ const configSchema = z.object({
252
+ allowAll: z.boolean().default(true).describe("Allow all tool calls"),
253
+ });
254
+
255
+ type Config = z.infer<typeof configSchema>;
256
+
247
257
  /**
248
258
  * Example policy plugin: implements PolicyPluginInterface.
249
259
  * Reference in guardio.config.ts with: { type: "policy", name: "example", path: "./plugins/example" }
250
- * Default export must be the plugin instance.
251
260
  */
252
261
  class ExamplePolicyPlugin implements PolicyPluginInterface {
253
262
  readonly name = "example";
254
263
 
255
- async evaluate(context: PolicyRequestContext): Promise<PolicyResult> {
256
- // Example: allow all calls. Replace with your policy logic.
257
- return Promise.resolve({ verdict: "allow" });
264
+ constructor(
265
+ private config: Config,
266
+ private context?: PolicyPluginContext,
267
+ ) {}
268
+
269
+ async evaluate(ctx: PolicyRequestContext): Promise<PolicyResult> {
270
+ // Access config: this.config.allowAll
271
+ // Access plugin storage: this.context?.pluginRepository
272
+ if (this.config.allowAll) {
273
+ return { verdict: "allow" };
274
+ }
275
+ return {
276
+ verdict: "block",
277
+ code: "EXAMPLE_BLOCKED",
278
+ reason: "Blocked by example policy",
279
+ };
258
280
  }
259
281
  }
260
282
 
261
- export default new ExamplePolicyPlugin();
283
+ // Export a PolicyPluginDefinition (factory-based)
284
+ const definition: PolicyPluginDefinition = {
285
+ name: "example",
286
+ factory: (config, context) => new ExamplePolicyPlugin(config as Config, context),
287
+ configSchema,
288
+ uiSchema: {
289
+ allowAll: { "ui:widget": "checkbox" },
290
+ },
291
+ };
292
+
293
+ export default definition;
262
294
  `;
263
295
  await writeFile(
264
296
  resolve(guardioPath, "plugins", "example", "index.ts"),
@@ -272,10 +304,33 @@ export default new ExamplePolicyPlugin();
272
304
 
273
305
  Add a plugin by setting \`path\` in \`guardio.config.ts\` to a directory that contains \`index.js\` or \`index.mjs\` (compile from \`index.ts\` with \`npm run build\`).
274
306
 
275
- - The directory must have \`index.js\` or \`index.mjs\` whose **default export is the plugin instance** (no descriptor).
276
- - Policy: implement \`PolicyPluginInterface\` (name, evaluate returning Promise<PolicyResult>). Config: \`{ "type": "policy", "name": "my-policy", "path": "./plugins/my-policy" }\`.
307
+ ## Policy plugins
308
+
309
+ Export a \`PolicyPluginDefinition\` with a factory function:
310
+
311
+ \`\`\`ts
312
+ import { z } from "zod";
313
+ import type { PolicyPluginDefinition } from "@guardiojs/guardio";
314
+
315
+ const configSchema = z.object({ ... });
316
+
317
+ const definition: PolicyPluginDefinition = {
318
+ name: "my-policy",
319
+ factory: (config, context) => new MyPolicyPlugin(config, context),
320
+ configSchema, // optional: enables dashboard validation
321
+ uiSchema: {}, // optional: custom form widgets
322
+ };
323
+
324
+ export default definition;
325
+ \`\`\`
326
+
327
+ Config: \`{ "type": "policy", "name": "my-policy", "path": "./plugins/my-policy" }\`
277
328
 
278
- Import types from "@guardiojs/guardio".${addExamplePlugin ? " See example/ for a policy plugin." : ""}
329
+ Custom plugins work exactly like built-in plugins:
330
+ - Create multiple instances with different configs via the dashboard
331
+ - Configs are stored in the database and validated against your schema
332
+ - Plugins receive a \`PolicyPluginContext\` with a scoped \`PluginRepository\` for persisting state
333
+ ${addExamplePlugin ? "\nSee example/ for a complete policy plugin." : ""}
279
334
  `;
280
335
  await writeFile(
281
336
  resolve(guardioPath, "plugins", "README.md"),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-guardio",
3
- "version": "0.0.10",
3
+ "version": "0.1.0",
4
4
  "description": "Scaffold a Guardio project",
5
5
  "type": "module",
6
6
  "bin": {