File size: 4,256 Bytes
d31ccb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**

 * Example plugin β€” the authoring + publishing reference. A folder under

 * `src/plugins/` with a `plugin.tsx` that default-exports a `HermesPlugin` is

 * all it takes; `discoverBundledPlugins()` finds and registers it (no import,

 * no registry edit). Delete this folder and everything below is gone.

 *

 * The ONLY import surface is `@hermes/plugin-sdk` (lint-enforced) β€” the

 * vscode-module model. This one plugin dogfoods the whole authoring kit:

 *  - `render()` contribution β€” full stateful React in a statusbar slot;

 *  - `ctx.storage` β€” the count survives reloads (namespaced persistence);

 *  - `host.onEvent('*')` β€” live gateway stream, counted in the tooltip;

 *  - PALETTE + KEYBINDS contracts β€” "Example: Reset click counter" in ⌘K

 *    and as a rebindable (default-unbound) action in the keybind panel;

 *  - plugin-local `atom` + `useValue` β€” module state, leaf subscription;

 *  - `haptic` / `host.notify` / `Tip` / `cn` β€” the design language.

 *

 * Ships OFF by default (`defaultEnabled: false`): it inventories in

 * Settings β–Έ Plugins and registers nothing until the user flips the switch.

 */

import {
  atom,
  cn,
  haptic,
  type HermesPlugin,
  host,
  type KeybindContribution,
  KEYBINDS_AREA,
  PALETTE_AREA,
  type PaletteContribution,
  STATUSBAR_AREAS,
  Tip,
  useValue
} from '@hermes/plugin-sdk'

const $clicks = atom(0)
const $events = atom(0)

function ClickCounter() {
  const count = useValue($clicks)
  const events = useValue($events)
  const gateway = useValue(host.state.gateway)

  return (
    <Tip label={`Example plugin β€” gateway ${gateway}, ${events} events heard`}>

      <button

        className={cn(

          'inline-flex h-full items-center gap-1 rounded-none px-1.5 text-[0.6875rem] tabular-nums transition-colors',

          'text-(--ui-text-tertiary) hover:bg-(--chrome-action-hover) hover:text-foreground',

          count > 0 && 'text-foreground'

        )}

        onClick={() => {

          haptic('tap')

          // Imperative read in the handler ($atom.get()), reactive read in the

          // render (useValue) β€” never a stale closure.

          const next = $clicks.get() + 1

          $clicks.set(next)



          if (next % 10 === 0) {

            host.notify({ kind: 'success', message: `Example plugin: ${next} clicks!` })

          }

        }}

        type="button"

      >

        <span aria-hidden>β—‰</span>

        <span>{count === 0 ? 'click me' : `clicked ${count}Γ—`}</span>

      </button>

    </Tip>
  )
}

const plugin: HermesPlugin = {
  id: 'example',
  name: 'Example Plugin',
  defaultEnabled: false,
  register(ctx) {
    // Persisted count: hydrate once, write through on every change.
    $clicks.set(ctx.storage.get('clicks', 0))
    $clicks.listen(clicks => ctx.storage.set('clicks', clicks))

    // Hear the live gateway stream (deltas, lifecycle, tools β€” everything).
    host.onEvent('*', () => $events.set($events.get() + 1))

    const reset = () => {
      $clicks.set(0)
      host.notify({ kind: 'info', message: 'Example plugin: counter reset' })
    }

    // Provenance (source: 'plugin:example') and the namespaced registry ids
    // (example:counter, …) are stamped by ctx β€” authors write plain
    // contributions. The shared `example.reset` ACTION id links the palette
    // row's hotkey hint to the keybind panel's live binding.
    ctx.registerMany([
      {
        id: 'counter',
        area: STATUSBAR_AREAS.right,
        order: 100,
        render: () => <ClickCounter />
      },
      {
        id: 'reset',
        area: PALETTE_AREA,
        data: {
          id: 'example.reset',
          action: 'example.reset',
          label: 'Example: Reset click counter',
          keywords: ['example', 'plugin', 'counter'],
          run: reset
        } satisfies PaletteContribution
      },
      {
        id: 'reset',
        area: KEYBINDS_AREA,
        data: {
          id: 'example.reset',
          label: 'Example: Reset click counter',
          defaults: [],
          run: reset
        } satisfies KeybindContribution
      }
    ])
  }
}

export default plugin