Kissflow DocsCentro de ayuda
Este artículo aún no está disponible en español. Se muestra la versión original en inglés.

Agent SDK

Use Custom Agent SDK methods in app page JavaScript to start runs, read them, and handle completion.

Use the Custom Agent SDK in app page JavaScript to start a published agent, read an existing execution, and handle completion. This article shows the page-event pattern using kf.app.getAgent, execute, getExecution, and onExecutionComplete. The full method reference is at /sdk/app/agent.

Video: Agent SDK

Methods used in page code

MethodPurpose
kf.app.getAgent(agentId)Get the agent handle. It doesn't send a request.
await agent.getContract()Read {AgentId, Definition: {Name, Inputs, Outputs, MaxOutputItems}, Readiness}.
await agent.execute({ inputData: {...} })Start a Live run. Returns {ExecutionId, Status}. It is never retried automatically.
await agent.getExecution(executionId)Read one run.
agent.onExecutionComplete(executionId, onComplete, onWatchError)Watch a run until it finishes. Returns a function that stops the watch.

See the SDK reference for agents for the full signatures.

Errors

SDK calls reject with {isError: true, error, errorCode} when something goes wrong. There is no message: read error for the text and errorCode to decide what to do. errorCode is an AGENT_... code or a platform code (KISSFLOW_ERROR_...).

errorCodeMeaning
AGENT_EXECUTE_NOT_SENTThe start request was not sent, so the run did not start.
AGENT_EXECUTE_OUTCOME_UNKNOWNThe run may still have started. App code can't list runs, so tell the user the request may still be in progress instead of starting another.
AGENT_OBSERVATION_STOPPEDThe watch stopped, for example after about 6 minutes. The run may still finish; read it again later with getExecution.
AGENT_WATCH_ID_IN_USEThe watch ID is already in use.
AGENT_READ_TIMEOUTKissflow did not answer a read in time. Try again.
AGENT_OFFLINEA read was attempted while the user is offline.
AGENT_NOT_IN_APPThe call was made outside an app.
AGENT_INVALID_REQUESTAn agent ID or run ID that is not an ID.
AGENT_REQUEST_FAILEDA network or unclassified failure.
AGENT_UNEXPECTED_RESPONSEKissflow returned an unexpected response.

If the agent service doesn't answer a start request within 30 seconds, the platform returns 504 KISSFLOW_ERROR_30079 and the SDK reports it as AGENT_EXECUTE_OUTCOME_UNKNOWN. Show the error text for any code you don't handle.

Start from a page button

  1. Publish the agent and note its ID. Confirm its input names and types, for example with getContract().
  2. Add JavaScript to the relevant button click event in your app page. Use a page load event only when opening the page should start real work.
  3. Get the agent and call execute with the values collected from your page.
  4. Keep the execution ID in your page state and associate it with the business item if you need to retrieve it later.
// Place this in an app page's button click JavaScript action.
const agent = kf.app.getAgent("<AGENT_ID>");
try {
  const { ExecutionId, Status } = await agent.execute({
    inputData: { Complaint: "A delivered item is damaged." },
  });
  console.log("Keep this execution ID:", ExecutionId, Status);
} catch (e) {
  // e is { isError, error, errorCode }
  console.log(e.errorCode, e.error);
}

Complaint is an example input name. Replace it with your configured input key and values from your page. The signed-in person needs App Admin, Edit, or View permission on the app. Each tool uses its own connection's permissions.

Keep the button unavailable while your page is starting a run. If you get AGENT_EXECUTE_OUTCOME_UNKNOWN, the run may still have started and app code can't list runs, so tell the user the request may still be in progress instead of starting another.

Read an existing execution

Use getExecution when you have an execution ID. This lets you refresh a status or reconnect your page to a known run.

const agent = kf.app.getAgent("<AGENT_ID>");
try {
  const execution = await agent.getExecution("<SAVED_EXECUTION_ID>");
  console.log("Execution:", execution);
} catch (e) {
  console.log(e.errorCode, e.error);
}

The status is one of Queued, NotStarted, Running, Completed or Failed; more may be added, so treat any status other than Completed or Failed as still in progress. Output.items is present once the run is Completed, and Error once it is Failed. Results are shaped as {"items":[{...}]}. A failed run has no partial results, though tools may already have acted. The run record doesn't include the agent's working; add a Reason output to see why.

Handle completion on the page

onExecutionComplete watches a run and calls onComplete once when it finishes, with the same object getExecution returns. A Failed run is a result, so read Status. It returns a stop function, so you can end the watch when the page no longer needs it. Stopping never cancels the run.

const agent = kf.app.getAgent("<AGENT_ID>");
const stop = agent.onExecutionComplete(
  executionId,
  (execution) => {
    if (execution.Status === "Completed") console.log(execution.Output.items);
    else console.log(execution.Status, execution.Error);
  },
  (watchError) => {
    // For example AGENT_OBSERVATION_STOPPED after about 6 minutes.
    console.log(watchError.errorCode, watchError.error);
  }
);
// Call stop() to end the watch early.

onWatchError is also called if the host doesn't support watching or access was refused. The watch gives up after about 6 minutes with AGENT_OBSERVATION_STOPPED. That doesn't cancel the run. Keep the execution ID so you can read it later. Closing the page doesn't cancel the run either.

Where scripts can run

Form, field, gallery and table scripts run in a worker that stops after 1 minute. They and page event scripts also stop when the next script starts on the same form, component or page, and a pending call or watch ends with the script without calling back. In those scripts, keep the ExecutionId (for example, on the item) and read it later with getExecution, or run the agent from a custom component or custom page.

Test the event path

Test one successful run and one failure with safe data. A run started with execute uses the Live version and real tools. Check that repeated clicks do not start duplicate work, that outputs appear on the correct item, and that failed runs lead to a reviewable message. Tool actions are real during testing.

Page event scripts run inside the app. They do not add agent support to portals, public forms, or forms opened outside the app. For server-side calls, use the Agent API with securely stored credentials.

En esta página