Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JS] there seems to be no supported way to look up an interrupt by name #2219

Open
csells opened this issue Mar 2, 2025 · 3 comments
Open

Comments

@csells
Copy link

csells commented Mar 2, 2025

Is your feature request related to a problem? Please describe.
When I've got a set of multiple interrupt definitions, I need a way to map from the interrupt result to a specific interrupt definition so that I can get a fully-formed response to feed into ai.generate(). The respond to interrupts sample hardcodes the single askQuestion interrupt, which doesn't work where there are multiple possible interrupts for the LLM to choose from.

Describe alternatives you've considered
The name of the interrupt used is available for each interrupt in the array returned by the ai.generate method via interrupts[i].toolRequest.name. However, you can't compare that to a list of available tools (including interrupts) because the value of each tool's name property is 'asyncFn'.

I can use the registry.actionsById property to look up an interrupt:

const interruptName = interrupt.toolRequest.name;
const interruptDefinition = ai.registry.actionsById[`/tool/${interruptName}`];

Unfortunately, the use of this map yields an error in the IDE:'Property 'actionsById' is private and only accessible within class 'Registry'.ts(2341)'

I can use the result of ai.registry.listActions():

const interruptName = interrupt.toolRequest.name;
const actions = await ai.registry.listActions();
const interruptDefinition = actions[`/tool/${interruptName}`];

The problem with that is the the IDE now complains when I call the respond method: Property 'respond' does not exist on type 'Action<ZodTypeAny, ZodTypeAny, ZodTypeAny, ActionRunOptions<ZodTypeAny>>'.ts(2339)

Of course, I could build my own map of interrupt names to interrupt definitions, but that seems redundant, since genkit is tracking this data itself.

I'd like a supported way to look up an interrupt by name so that I can avoid these issues, please and thank you!

Describe the solution you'd like
If tool.name returned a reasonable value or the actionsById map was available or even if I could get what I needed from listActions(), any of those would be fine.

If I ran the zoo, I think I'd prefer an API that didn't need a specific interrupt definition to respond to an interrupt; I'd rather pass in a list of interrupt definitions, interrupts and results as part of my resumption call to at.generate(). Second place would be something like ai.registration.interruptByName map or method. But really anything that doesn't pollute the code too much and that doesn't raise errors in the IDE works for me.

fyi: @mbleigh

@mbleigh
Copy link
Collaborator

mbleigh commented Mar 3, 2025

You actually don't need an interrupt definition to respond to an interrupt, the respond helper is just a convenience for constructing a correctly-named toolResponse part. You can also do:

const response = ai.generate({
  // ...
  tools: [someInterrupt, anotherInterrupt, aThirdInterrupt]
});

const responses = response.interrupts.map(interrupt => ({
  toolResponse: {
    name: interrupt.toolRequest.name,
    ref: interrupt.toolRequest.ref,
    output: {...}, // populate this however you'd like
  }
}));

// second generation
const secondResponse = await ai.generate({
  messages: response.messages,
  resume: {respond: responses},
})

Does that help? Probably ought to document that the helpers are just sugaring over tool responses...

@csells
Copy link
Author

csells commented Mar 3, 2025

that's super helpful; I can do that on the client side and avoid even sending the interrupts back to the server from the client as context. if that made it into the docs, I'd feel safer depending on it in my code.

@csells
Copy link
Author

csells commented Mar 3, 2025

that said, if you're going to provide a helper on the server-side, there should be a good way to call it in the face of multiple interrupts, so I think the original issue stands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

2 participants