Integrate IaC Code with AG-UI
The Agent-User Interaction Protocol (AG-UI) connects agents to user-facing applications. Over HTTP and Server-Sent Events (SSE), IaC Code streams text, thinking processes, tool calls, pipeline steps, and interactions that require user action to front ends such as web consoles, chat clients, and IDE extensions. This topic describes how to start the AG-UI service and complete a minimal integration.
Scenarios
AG-UI suits user interfaces that display agent execution in real time. Instead of merging all events into a single block of text, the client can display each of the following separately:
Model responses and thinking processes
Tool names, parameters, execution progress, and results
Pipeline steps and cloud resource plans
Permission requests, follow-up questions, and candidate plan selections
The started, completed, failed, and canceled states of a run
The AG-UI service of IaC Code is a protocol adaptation layer over the A2A execution kernel:
AG-UI client
↓ RunAgentInput / SSE
iac-code agui
↓ local A2A
IaC Code Agent / Pipeline / cloud APIAG-UI and A2A share the same agent execution logic, model configuration, cloud identity, and permission rules. Choose the integration protocol that matches your goal: AG-UI to build a user-facing interaction interface, A2A to let another agent or orchestration platform call IaC Code remotely, or ACP to integrate agent sessions into an IDE or editor.
Prerequisites
IaC Code installed and configured as described in Install and configure IaC Code
An absolute working directory that IaC Code can access
A front end or back end that can send HTTP POST requests and continuously read the SSE stream
If the task queries or manages cloud resources, an Alibaba Cloud identity with least-privilege access
For cross-host deployments, service authentication, a reverse proxy, and network access control
Start the AG-UI service
Install the optional AG-UI dependency:
python -m pip install "iac-code[agui]"Start the service in the project directory:
iac-code agui --host 127.0.0.1 --port 41243If you do not set
--a2a-url, the AG-UI service automatically starts and manages an A2A child process that listens only on the local loopback address. You usually do not need to start a separate A2A service.Check the service status:
curl http://127.0.0.1:41243/healthExpected result: the service returns JSON data in which
statusisokandprotocolisag-ui.
Connect the first client
Install the AG-UI JavaScript client:
npm install @ag-ui/clientThe following example uses the standard HttpAgent to send a message and output streaming text, tool calls, and pipeline steps:
import { HttpAgent, randomUUID } from "@ag-ui/client";
const agent = new HttpAgent({
url: "http://127.0.0.1:41243/",
threadId: randomUUID(),
});
const forwardedProps = {
iacCode: {
schemaVersion: 1,
rosInvocationId: randomUUID(),
cwd: process.cwd(),
runMode: "normal",
preferredLanguage: "zh-CN",
},
};
agent.messages.push({
id: randomUUID(),
role: "user",
content: "Review the ROS template in the current directory, output only the issues and suggested changes, and do not modify any files.",
});
const subscriber = {
onTextMessageContentEvent({ event }) {
process.stdout.write(event.delta);
},
onToolCallStartEvent({ event }) {
console.log(`\n[tool] ${event.toolCallName}`);
},
onStepStartedEvent({ event }) {
console.log(`\n[step] ${event.stepName}`);
},
onRunErrorEvent({ event }) {
console.error(`\n${event.code}: ${event.message}`);
},
};
await agent.runAgent({ forwardedProps }, subscriber);cwd must be an absolute path under a directory that the AG-UI service is allowed to access. rosInvocationId identifies the current business invocation and must stay unchanged across one interrupt-and-resume cycle.
Expected result: the client receives the run started event, then text, tool, or step events, and finally the run finished event. A standard AG-UI client can ignore IaC Code custom events that it does not recognize. Ignoring these events does not affect standard text and run-status handling.
Manage multi-turn conversations
Each user input corresponds to a new HTTP/SSE request. Reuse the same threadId for one conversation and use a new runId for each run. When you use the official client, the client generates the runId by default.
After the previous request completes, you can add a new message to the messages array of the same HttpAgent and call runAgent() again. Do not send the next round of messages over an SSE connection that has already ended.
Start a pipeline
Set forwardedProps.iacCode.runMode to pipeline:
const forwardedProps = {
iacCode: {
schemaVersion: 1,
rosInvocationId: randomUUID(),
cwd: process.cwd(),
runMode: "pipeline",
pipelineName: "selling",
candidatePresentation: "rich",
},
};The client must handle the standard step, text, thinking, tool, and activity snapshot events. Information that has no standard AG-UI counterpart, such as architecture candidates and stack progress, is sent through custom events in the IaC Code namespace.
Handle interrupts and resume runs
When IaC Code requires permission approval, additional information, or a plan selection, the current run ends with a RUN_FINISHED event that contains interrupts. The client must perform the following operations:
Render an interaction interface based on the
message,responseSchema, and options of each interrupt.Collect user answers for all pending interrupts. Do not make selections for the user or approve write operations by default.
Send a new request with the original
threadId, a newrunId, and the originalrosInvocationId.Submit answers that match the
responseSchemathrough the standardresumearray.
The answer structure required by each interrupt can differ. Do not use the permission decision structure to answer general questions or candidate plan selections. For the complete fields and examples, see AG-UI protocol reference.
Configure access to the production environment
For local development, bind to 127.0.0.1 first. To serve requests across hosts, set at least an access token and the allowed working directories:
export IAC_CODE_AGUI_AUTH_TOKEN="${AGUI_SERVICE_TOKEN}"
export IAC_CODE_AGUI_ALLOWED_CWDS="/srv/iac-code-workspaces"
iac-code agui --host 0.0.0.0 --port 41243The client includes the following header in the request:
Authorization: Bearer <token>Production deployments must also meet the following requirements:
Provide tokens, model credentials, and Alibaba Cloud identities through a secret management solution. Do not write them into configuration files or front-end code.
Use a private network, a firewall, or a reverse proxy to restrict access sources, and enable TLS.
Allow only specific working directories. Do not add the file system root directory to
IAC_CODE_AGUI_ALLOWED_CWDS.The A2A address that the AG-UI adapter connects to must be a local loopback address. Do not configure it to point to an arbitrary remote A2A service.
Browser applications usually access the service through a same-origin back end or a reverse proxy. The AG-UI service does not automatically open cross-origin access.
Set cancellation, reconnection, and timeout boundaries for the client, but do not treat SSE heartbeats as business events.
FAQ
Startup reports missing AG-UI dependencies
Confirm that iac-code[agui] is installed in the same Python environment that runs iac-code. If you installed it in a virtual environment, activate that environment before you run the startup command.
Requests return 401
Confirm that the server sets the expected IAC_CODE_AGUI_AUTH_TOKEN and that the client sends Authorization: Bearer <token> with the same secret. If requests pass through a reverse proxy, check whether the proxy preserves the request header.
Requests report an invalid working directory
forwardedProps.iacCode.cwd must be an absolute path under a directory allowed by IAC_CODE_AGUI_ALLOWED_CWDS or by A2A. The same threadId cannot switch to another working directory in subsequent requests.
New messages report that the task must be resumed first
The current thread still has pending permissions, questions, or plan selections. The client must display all pending interrupts and answer them through resume before it sends the next regular message.
The service reports that local A2A is unavailable
If you do not set --a2a-url, check whether the AG-UI process has permission to start a child process and whether the port is available. If you manage A2A separately, confirm that --a2a-url points to a local loopback address, and check whether the A2A tokens configured on both sides match.