| 1 | // Reports what the host gives a plugin: its settings (validated by this |
| 2 | // plugin's own `Config` schema, with defaults), and, per call, the workspace |
| 3 | // and its own data directory. |
| 4 | import { mkdir, readFile, writeFile } from 'node:fs/promises' |
| 5 | import { join } from 'node:path' |
| 6 | import Schema from '@deepseek-ai/schemastery' |
| 7 | |
| 8 | export const name = 'plugin-context' |
| 9 | export const inject = ['tools', 'commands'] |
| 10 | |
| 11 | export const Config = Schema.object({ |
| 12 | greeting: Schema.string().default('Hello'), |
| 13 | limit: Schema.number().min(1).max(10).default(3), |
| 14 | }) |
| 15 | |
| 16 | export function apply(ctx, config) { |
| 17 | ctx.tools.register({ |
| 18 | name: 'ctx_probe', |
| 19 | description: 'Report the plugin context.', |
| 20 | parameters: { type: 'object', properties: {}, additionalProperties: false }, |
| 21 | execute: (_input, exec) => ({ config, workspace: exec.workspace ?? null, dataDir: exec.dataDir ?? null, keys: Object.keys(exec).sort() }), |
| 22 | }) |
| 23 | ctx.tools.register({ |
| 24 | name: 'ctx_note', |
| 25 | description: 'Write a note into the plugin data directory and read it back.', |
| 26 | parameters: { type: 'object', properties: { text: { type: 'string' } }, required: ['text'], additionalProperties: false }, |
| 27 | async execute({ text }, exec) { |
| 28 | await mkdir(exec.dataDir, { recursive: true }) |
| 29 | const file = join(exec.dataDir, 'note.txt') |
| 30 | await writeFile(file, text) |
| 31 | return { file, text: await readFile(file, 'utf8') } |
| 32 | }, |
| 33 | }) |
| 34 | ctx.tools.register({ |
| 35 | name: 'ctx_frozen', |
| 36 | description: 'Try to change the call context.', |
| 37 | parameters: { type: 'object', properties: {}, additionalProperties: false }, |
| 38 | execute: (_input, exec) => { |
| 39 | let changed = false |
| 40 | try { |
| 41 | exec.workspace = '/elsewhere' |
| 42 | changed = exec.workspace === '/elsewhere' |
| 43 | } catch { |
| 44 | // frozen objects throw in strict mode |
| 45 | } |
| 46 | return { changed } |
| 47 | }, |
| 48 | }) |
| 49 | ctx.commands.register({ |
| 50 | name: 'ctx-probe', |
| 51 | description: 'Report the plugin context.', |
| 52 | handler: (invocation) => |
| 53 | ({ kind: 'success', text: JSON.stringify({ config, workspace: invocation.workspace ?? null, dataDir: invocation.dataDir ?? null }) }), |
| 54 | }) |
| 55 | } |
| 56 |