Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:AIPexStudio AIPex AIPex transformToolEvent

From Leeroopedia
Knowledge Sources
Domains AI_Agent, Event_Processing
Last Updated 2026-10-24 12:00 GMT

Overview

Concrete transformation method that converts OpenAI Agents SDK tool-call stream events into normalized AgentEvents with business-level failure detection and error sanitization.

Description

The transformToolEvent method inspects `RunItemStreamEvent` objects from the SDK's streaming runner. For `tool_called` events it emits a `tool_call_start` event with extracted tool name and arguments. For `tool_output` events it checks the status and inspects the result object for `success: false` to detect business-level failures. On failure or error status, it extracts the error message from multiple locations (item output, rawItem output, rawItem error), sanitizes it by masking sensitive patterns (Bearer tokens, API keys), truncates to 500 characters, and emits a `tool_call_error` event. On success it emits `tool_call_complete` with the parsed result.

Usage

This method is called internally by `runExecution` for each `run_item_stream_event`. It is not called directly by consumers but is documented as the implementation of the Tool Event Normalization principle.

Code Reference

Source Location

  • Repository: AIPex
  • File: packages/core/src/agent/aipex.ts
  • Lines: L504-743

Signature

private transformToolEvent(event: RunItemStreamEvent): AgentEvent | undefined

// Helper methods:
private extractToolName(item: RunItemStreamEvent["item"]): string
private extractToolArguments(item: RunItemStreamEvent["item"]): unknown
private extractToolOutput(item: RunItemStreamEvent["item"]): unknown
private sanitizeErrorMessage(message: string, maxLength: number): string

Import

// Internal method — not exported. Called by AIPex.runExecution().

I/O Contract

Inputs

Name Type Required Description
event RunItemStreamEvent Yes SDK stream event (name: "tool_called" or "tool_output")

Outputs

Name Type Description
returns undefined tool_call_start, tool_call_complete, tool_call_error, or undefined for non-tool events

Usage Examples

Transformation Logic (Illustrative)

// Called internally during streaming:
// const toolEvent = this.transformToolEvent(streamEvent);
// if (toolEvent) yield toolEvent;

// Example transformations:
// SDK event: { name: "tool_called", item: { rawItem: { name: "click", arguments: '{"uid":"42"}' } } }
// → AgentEvent: { type: "tool_call_start", toolName: "click", params: { uid: "42" } }

// SDK event: { name: "tool_output", item: { rawItem: { status: "completed" }, output: '{"success":false,"error":"Element not found"}' } }
// → AgentEvent: { type: "tool_call_error", toolName: "click", error: Error("Element not found") }

Related Pages

Implements Principle

Uses Heuristics

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment