Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rude-maps-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@knocklabs/agent-toolkit": patch
---

feat: improve hitl methods based on real usage
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
"dist"
],
"dependencies": {
"@knocklabs/mgmt": "^0.1.0-rc.0",
"@knocklabs/node": "^0.6.18",
"@knocklabs/mgmt": "^0.1.0",
"@knocklabs/node": "^0.6.19",
"@modelcontextprotocol/sdk": "^1.7.0",
"json-schema-to-zod": "^2.6.1",
"uuid": "^11.1.0",
Expand Down
1 change: 1 addition & 0 deletions src/ai-sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ const createKnockToolkit = async (
description: wrapToolDescription(toolToWrap.description ?? ""),
execute: async (input: any, options: ToolExecutionOptions) => {
const toolExecution = {
id: options.toolCallId,
method,
args: input,
extra: {
Expand Down
15 changes: 13 additions & 2 deletions src/lib/human-in-the-loop/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Recipient } from "@knocklabs/node";

import { Config } from "@/types";

import { KnockClient } from "../knock-client";
Expand Down Expand Up @@ -34,17 +36,25 @@ async function triggerHumanInTheLoopWorkflow({
}) {
const knock = await knockClient.publicApi(config.environment);

if (inputConfig.onBeforeCallKnock) {
await inputConfig.onBeforeCallKnock(toolCall);
}

const result = await knock.workflows.trigger(inputConfig.workflow, {
data: {
type: "deferred_tool_call",
tool_call: toolCall,
metadata: inputConfig.metadata,
} as DeferredToolCallWorkflowData,
recipients: inputConfig.recipients,
recipients: inputConfig.recipients as Recipient[],
tenant: inputConfig.tenant,
actor: inputConfig.actor,
actor: inputConfig.actor as Recipient,
});

if (inputConfig.onAfterCallKnock) {
await inputConfig.onAfterCallKnock(toolCall, result);
}

return result;
}

Expand Down Expand Up @@ -77,6 +87,7 @@ function handleMessageInteraction(
workflow: message.source.key,
interaction: event.event_data,
toolCall: {
id: messageData.tool_call.id,
method: messageData.tool_call.method,
args: messageData.tool_call.args,
extra: messageData.tool_call.extra,
Expand Down
23 changes: 21 additions & 2 deletions src/lib/human-in-the-loop/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Message } from "@knocklabs/node/dist/src/resources/messages/interfaces";
import { WorkflowRun } from "@knocklabs/node/dist/src/resources/workflows/interfaces";

type Metadata = Record<string, unknown> | undefined;

Expand All @@ -22,7 +23,7 @@ export interface DeferredToolCallConfig {
/**
* The recipients to trigger the workflow for.
*/
recipients: string[];
recipients: string[] | Record<string, unknown>[];
/**
* Any extra data to pass to the workflow as context.
*/
Expand All @@ -34,10 +35,28 @@ export interface DeferredToolCallConfig {
/**
* The actor to trigger the workflow for.
*/
actor?: string;
actor?: string | Record<string, unknown>;

/**
* A function to call before calling the Knock API.
*/
onBeforeCallKnock?: (toolCall: DeferredToolCall) => Promise<void>;

/**
* A function to call after calling the Knock API.
*/
onAfterCallKnock?: (
toolCall: DeferredToolCall,
result: WorkflowRun
) => Promise<void>;
}

export type DeferredToolCall = {
/**
* The ID of the tool call. Should be automatically generated by the SDK.
*/
id: string;

/**
* The method of the tool that was called.
*/
Expand Down