Skip to content

Instantly share code, notes, and snippets.

@palmamartin
Forked from rockorager/worker.ts
Created June 20, 2026 07:20
Show Gist options
  • Select an option

  • Save palmamartin/b499aaabdfa6cff5b8807ccc14b9626c to your computer and use it in GitHub Desktop.

Select an option

Save palmamartin/b499aaabdfa6cff5b8807ccc14b9626c to your computer and use it in GitHub Desktop.
import type { PluginAPI, ThreadID } from '@ampcode/plugin'
export default function (amp: PluginAPI) {
const workerAgent = amp.getBuiltinAgent('deep', { reasoningEffort: 'high' })
amp.registerTool({
name: 'spawn_worker',
description:
'Launch a new independent worker thread with the provided instructions. ' +
'Returns after the thread is started; do not poll or wait for it. ' +
'It has tools to message you back if you instruct it to.',
inputSchema: {
type: 'object',
properties: {
instructions: {
type: 'string',
description: 'Instructions to send to the worker thread.',
},
},
required: ['instructions'],
},
async execute(input, ctx) {
const instructions = String(input.instructions || '').trim()
if (!instructions) {
throw new Error('instructions are required')
}
const thread = await workerAgent.createThread()
const message = `Message sent from Thread ${ctx.thread.id}
Send a message back to that thread when complete using the send_to_thread tool.
${instructions}`
await thread.appendUserMessage({
type: 'user-message',
content: message,
})
return `Started worker in ${thread.id}. Do not poll or wait for it.`
},
})
amp.registerTool({
name: 'send_to_thread',
description: 'Send a text user message to an existing Amp thread by thread ID.',
inputSchema: {
type: 'object',
properties: {
threadID: {
type: 'string',
description: 'Target Amp thread ID, for example T-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.',
},
message: {
type: 'string',
description: 'Text message to send to the target thread.',
},
},
required: ['threadID', 'message'],
},
async execute(input, ctx) {
const threadID = String(input.threadID || '').trim()
const message = String(input.message || '').trim()
if (!threadID) {
throw new Error('threadID is required')
}
if (!message) {
throw new Error('message is required')
}
const thread = amp.threads.get(threadID as ThreadID)
const forwardedMessage = `From Amp ThreadID ${ctx.thread.id}:
${message}`
await thread.appendUserMessage({
type: 'user-message',
content: forwardedMessage,
})
return `Sent message to ${threadID}.`
},
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment