zhin-context-services

Details Zhin context services (config, database, cron, permission) and how to register or consume them in plugins.

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "zhin-context-services" with this command: npx skills add zhinjs/ai-skills/zhinjs-ai-skills-zhin-context-services

Zhin Context & Services Skill

Use this skill when developers need to interact with built-in contexts or provide custom services via defineContext.

Accessing Built-in Services

import { usePlugin } from '@zhin.js/core'

const plugin = usePlugin()

// Inject built-in contexts
const config = plugin.inject('config')
const permissions = plugin.inject('permission')
const cronService = plugin.inject('cron')

Use plugin.contextIsReady('service') to check if a context is available before using it.

Config Service

Load or read config files via ConfigService:

const configService = plugin.inject('config')
const config = configService?.get('zhin.config.yaml', {
  debug: false,
  plugins: []
})

Notes:

  • Supported formats: .json, .yaml, .yml
  • Config values can reference environment variables using ${ENV_NAME:default}

Cron Service

Register scheduled tasks using the Cron class and addCron:

import { Cron } from '@zhin.js/core'

plugin.addCron(new Cron('0 * * * *', async () => {
  plugin.logger.info('Hourly task')
}))

Cron entries are auto-started and stopped when the plugin is disposed.

Permission Service

The permission service provides checks in MessageCommand.permit(...). You can also extend it by pushing a new check function:

const permission = plugin.inject('permission')
permission?.add({
  name: /^role\(.+\)$/,
  check: async (name, message) => {
    // custom role validation
    return false
  }
})

Database Service

If the database context is enabled, use it via plugin.inject('database'):

const database = plugin.inject('database')
const users = await database?.select('User')

To define new models, call plugin.defineModel(name, definition) if the database service is active.

Defining Custom Contexts

import { defineContext, usePlugin } from '@zhin.js/core'

const plugin = usePlugin()

plugin.provide(defineContext({
  name: 'metrics',
  description: 'Metrics service',
  mounted: async () => ({
    counter: 0
  }),
  dispose: (value) => {
    // cleanup
  },
  extensions: {
    increment() {
      this.counter += 1
    }
  }
}))

Custom contexts can also extend Plugin.Extensions so you can call new helper methods from plugins.

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

General

zhin-plugin-lifecycle

No summary provided by upstream source.

Repository SourceNeeds Review
General

zhin-mcp-server

No summary provided by upstream source.

Repository SourceNeeds Review
General

zhin-command-middleware

No summary provided by upstream source.

Repository SourceNeeds Review
General

zhin-ai-integration

No summary provided by upstream source.

Repository SourceNeeds Review