tools.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /** Agent-facing current-profile management using the same service as Web controls. */
  2. import { assertNever } from '@deepseek-ai/dsh-util-values'
  3. import type { Context } from '@deepseek-ai/cordis'
  4. import type {} from './index.ts'
  5. import type { PluginEntryId } from './types.ts'
  6. import { defineTool } from '@deepseek-ai/dsh-tools'
  7. /** Required services for the management tool. */
  8. export const inject = ['tools', 'pluginManager']
  9. /** Register one management tool for discovery and the four persistent actions.
  10. * @param ctx Agent-scoped tool registration context.
  11. */
  12. export function apply(ctx: Context): void {
  13. ctx.tools.register(defineTool({
  14. name: 'plugin_manager',
  15. description: 'List plugins or bundles in the current profile, enable or disable them, install a bundle, or remove an installed bundle. Changes affect every session in this profile. List first to obtain exact identifiers. Package installation can execute allowed build scripts. Live profiles apply changes immediately; startup profiles require restart.',
  16. parameters: {
  17. action: { type: 'string', required: true, enum: ['list_plugins', 'list_bundles', 'set_plugin', 'set_bundle', 'install_bundle', 'remove_bundle'], description: 'Management operation.' },
  18. target: { type: 'string', description: 'Plugin entry id, bundle package name, or installation spec, according to action.' },
  19. enabled: { type: 'boolean', description: 'Required for set operations; defaults to true for installation.' },
  20. offset: { type: 'number', description: 'Zero-based list offset; defaults to 0.' },
  21. limit: { type: 'number', description: 'List page size, from 1 to 100; defaults to 25.' },
  22. },
  23. output: {
  24. schema: { type: 'string' },
  25. render: (_args, value) => [{ type: 'text', text: value }],
  26. },
  27. async execute(args) {
  28. const manager = ctx.pluginManager
  29. switch (args.action) {
  30. case 'list_plugins':
  31. case 'list_bundles': {
  32. const offset = args.offset ?? 0
  33. const limit = args.limit ?? 25
  34. if (!Number.isInteger(offset) || offset < 0 || !Number.isInteger(limit) || limit < 1 || limit > 100) {
  35. throw new Error('offset must be a non-negative integer and limit must be an integer from 1 to 100')
  36. }
  37. const rows = args.action === 'list_plugins' ? await manager.listPlugins() : await manager.listBundles()
  38. const entries = rows.slice(offset, offset + limit)
  39. return JSON.stringify({ entries, total: rows.length,
  40. nextOffset: offset + entries.length < rows.length ? offset + entries.length : null })
  41. }
  42. case 'set_plugin':
  43. case 'set_bundle': {
  44. if (args.target === undefined || args.enabled === undefined) throw new Error('target and enabled are required')
  45. return JSON.stringify(await (args.action === 'set_plugin'
  46. ? manager.setPluginEnabled(args.target as PluginEntryId, args.enabled)
  47. : manager.setBundleEnabled(args.target, args.enabled)))
  48. }
  49. case 'install_bundle':
  50. if (args.target === undefined) throw new Error('target package spec is required')
  51. return JSON.stringify(await manager.installBundle(args.target, args.enabled === undefined ? {} : { enabled: args.enabled }))
  52. case 'remove_bundle':
  53. if (args.target === undefined) throw new Error('target bundle name is required')
  54. return JSON.stringify(await manager.removeBundle(args.target))
  55. /* v8 ignore next -- tool JSON validation rejects actions outside the declared enum */
  56. default: return assertNever(args.action)
  57. }
  58. },
  59. presentCall: args => ({ card: 'generic', title: 'Manage profile plugins', kind: args.action.startsWith('list_') ? 'read' : 'other', rawInput: args }),
  60. }))
  61. }