Skip to content

Refactor TextGateway and related classes to support SingleTurnResponse for LLM interactions#1

Open
vinitkadam03 wants to merge 14 commits into0.xfrom
reafctor-to-single-turn-gateways
Open

Refactor TextGateway and related classes to support SingleTurnResponse for LLM interactions#1
vinitkadam03 wants to merge 14 commits into0.xfrom
reafctor-to-single-turn-gateways

Conversation

@vinitkadam03
Copy link
Copy Markdown
Owner

  • Updated TextGateway interface to replace TextResponse with SingleTurnResponse for generateText and streamText methods.
  • Implemented SingleTurnResponse in FakeTextGateway and OpenAiGateway, ensuring proper handling of tool calls and responses.
  • Introduced TextOrchestrator to manage multi-step tool loops and streamline response generation.
  • Removed deprecated onToolInvocation methods and adjusted tool invocation handling.
  • Enhanced parsing logic in OpenAi and Prism gateways to accommodate new response structure.
  • Added tests to validate new functionality and ensure backward compatibility.

@vinitkadam03 vinitkadam03 force-pushed the reafctor-to-single-turn-gateways branch 2 times, most recently from 46b1fc1 to c6cb2d6 Compare April 3, 2026 19:24
@vinitkadam03 vinitkadam03 force-pushed the reafctor-to-single-turn-gateways branch 3 times, most recently from 724e6d8 to 2eb0604 Compare April 15, 2026 20:18
…e for LLM interactions

- Updated TextGateway interface to replace TextResponse with SingleTurnResponse for generateText and streamText methods.
- Implemented SingleTurnResponse in FakeTextGateway and OpenAiGateway, ensuring proper handling of tool calls and responses.
- Introduced TextOrchestrator to manage multi-step tool loops and streamline response generation.
- Removed deprecated onToolInvocation methods and adjusted tool invocation handling.
- Enhanced parsing logic in OpenAi and Prism gateways to accommodate new response structure.
- Added tests to validate new functionality and ensure backward compatibility.
…port

- Added `previousResponseId` parameter to `generateText` and `streamText` methods in TextGateway and its implementations.
- Introduced `buildContinuationBody` method in BuildsTextRequests to create lightweight continuation requests.
- Updated TextOrchestrator to manage `previousResponseId` for multi-step interactions.
- Enhanced response handling in OpenAiGateway and other classes to accommodate new stateful continuation logic.
- Added serialization for tool result outputs to streamline API requests.
- Updated SingleTurnResponse and StreamEnd to include response ID for better tracking of interactions.
- Simplified the tool name comparison by removing the method_exists check for 'name', directly using class_basename for matching.
- This change enhances code readability and maintains functionality.
… stateful continuation support

- Updated variable usage to enhance clarity and maintainability by replacing direct calls with a single variable for last results.
- Adjusted the buildFinalResponse method to utilize the last result for structured data handling.
- Removed the raw data parameter from SingleTurnResponse to simplify the response structure.
…d streamline response handling

- Updated generateText and streamText methods to include previousResponseId parameter for stateful interactions.
- Replaced TextResponse with SingleTurnResponse in response parsing and handling across relevant classes.
- Simplified response processing logic by removing unnecessary parameters and enhancing clarity.
- Adjusted tool invocation handling to improve maintainability and performance in streaming responses.
- Introduced handling for server tool blocks to improve event emission during text streaming.
- Updated tool call filtering to exclude synthetic structured output, ensuring accurate tool call tracking.
- Adjusted finish reason logic to correctly identify stop conditions based on real tool calls.
- Enhanced response parsing to accommodate structured data extraction when applicable.
- Removed unused parameters `tools` and `schema` from `streamText` and `processTextStream` methods to simplify method signatures and improve clarity.
- Updated relevant gateway implementations to reflect these changes, enhancing maintainability.
- Introduced handling for reasoning text and signatures in text streaming and response parsing.
- Updated `processTextStream` to capture and emit reasoning details during tool calls.
- Enhanced `mapAssistantMessage` to include reasoning text and signature in assistant messages.
- Added methods to extract and attach reasoning data to tool calls for improved replay accuracy.
…nse and streamline response handling

- Updated generateText and streamText methods to include previousResponseId parameter for stateful interactions.
- Replaced TextResponse with SingleTurnResponse in response parsing and handling across relevant classes.
- Simplified method signatures by removing unused parameters and enhancing clarity.
- Enhanced text streaming processes to improve event emission and tool call tracking.
- Adjusted response processing logic to accommodate new stateful continuation support.
…d continuation support

- Introduced `buildContinuationBody` method to streamline the creation of continuation requests using `previous_response_id`.
- Enhanced `mergeSharedResponsesRequestOptions` to consolidate shared options for both initial and continuation requests.
- Updated `generateText` and `streamText` methods to utilize continuation logic, ensuring consistent request structure.
- Added tests to validate the correct handling of tool choices and options in follow-up requests.
StepLoop pairs with the existing Step DTO it produces and reads more
concretely than "Orchestrator". SingleTurnResponse is an internal
contract between gateways and the step loop, so it belongs under
`Laravel\Ai\Gateway` alongside its consumer rather than under
`Laravel\Ai\Responses` where user-facing response types live.

No behavior change.
…tract

These gateways landed on 0.x with the pre-refactor shape (per-gateway
tool loop, recursive continueWithToolResults, TextResponse return type).
Convert them to implement the new TextGateway contract: one LLM call
per invocation, returning SingleTurnResponse; the multi-step loop is
handled by StepLoop in the provider trait.

Behavioral quirks preserved:
- Ollama: force FinishReason::ToolCalls when tool_calls are populated
  regardless of done_reason, since real Ollama responses can report
  "stop" alongside tool calls.
- OpenRouter: treat finish_reason "error" with inline error payload as
  a stream Error event.
@vinitkadam03 vinitkadam03 force-pushed the reafctor-to-single-turn-gateways branch from 2eb0604 to 78ec5d3 Compare April 16, 2026 05:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant