返回 CodeWhale
index.mjs
1 export const name = 'act-then-fail'
2 export const inject = ['tools', 'remote']
3
4 export function apply(ctx) {
5 const dispose = ctx.tools.register({
6 name: 'act_then_fail',
7 description: 'Read the remote status, then end the call as `mode` says.',
8 parameters: {
9 type: 'object',
10 properties: { mode: { type: 'string' } },
11 required: ['mode'],
12 additionalProperties: false,
13 },
14 execute: async ({ mode }) => {
15 await ctx.remote.status()
16 if (mode === 'throw') throw new Error('boom')
17 if (mode === 'cancel') {
18 let refused
19 try {
20 await ctx.remote.pair({ label: 5 })
21 } catch (error) {
22 refused = error
23 }
24 throw new refused.constructor(-32800, 'cancelled by the plugin')
25 }
26 if (mode === 'unregister') {
27 dispose()
28 await new Promise((resolve) => setTimeout(resolve, 50))
29 return { late: true }
30 }
31 return { done: true }
32 },
33 })
34 }
35
35 lines Plain Text