Files
AgenticCode/openspec/specs/agent-extraction/spec.md
local 471e9ce935 feat: migrate chat backend to Semantic Kernel with tool calling support
Replace manual HTTP proxy in ChatController with Semantic Kernel's
OpenAI chat completion service pointed at CLIProxyAPI. Add extraction
plugin with validation function for structured field extraction from
natural language, enabling an agentic loop with auto-retry and
human-in-the-loop escalation.

- Add Microsoft.SemanticKernel 1.74.0 with OpenAI connector
- Create ExtractedFields schema and ValidationResult models
- Create ExtractionPlugin with [KernelFunction] validation
- Rewrite ChatController to use IChatCompletionService streaming
- Configure FunctionChoiceBehavior.Auto() for tool calling
- Preserve existing SSE contract (client unchanged)
- Update tests to mock SK services, add plugin and integration tests
- Archive multi-turn-conversations and migrate-to-semantic-kernel changes
- Sync specs for agent-extraction, semantic-kernel-integration, chat-streaming

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 23:59:13 +01:00

3.3 KiB

Purpose

Define the autonomous agent-driven extraction pipeline — structured field extraction from natural language, schema-based validation via tool calling, autonomous retry logic, and human-in-the-loop clarification.

Requirements

Requirement: Structured field extraction from natural language

The agent SHALL extract a predefined set of key-value pairs from user-provided natural language text (e.g., email content) and return them as a structured JSON object.

Scenario: All fields extracted successfully

  • WHEN the user sends a message containing natural language with all required information
  • THEN the agent returns a JSON object with all predefined fields populated from the text

Scenario: Partial extraction

  • WHEN the user sends a message that contains some but not all required fields
  • THEN the agent extracts available fields and leaves missing fields as null

Requirement: Predefined extraction schema

The system SHALL define a fixed set of known field names and types as a strongly-typed C# class. All extraction output MUST conform to this schema.

Scenario: Output conforms to schema

  • WHEN the agent produces extracted fields
  • THEN every key in the output matches a field defined in the schema and values match expected types

Requirement: Autonomous validation via tool calling

The agent SHALL validate extracted fields by calling a validation tool function. The validation tool checks that all required fields are present and correctly typed.

Scenario: Validation passes

  • WHEN the agent calls the validation tool with a complete and correct extraction
  • THEN the tool returns a success result and the agent returns the final output to the user

Scenario: Validation fails with fixable errors

  • WHEN the validation tool returns errors for missing or malformed fields
  • THEN the agent re-reads the source text and attempts to fix the extraction without user intervention

Requirement: Autonomous retry with iteration cap

The agent SHALL retry extraction autonomously up to 3 times when validation fails. After exhausting retries, the agent MUST escalate to the user.

Scenario: Agent retries and succeeds

  • WHEN validation fails on the first attempt but the error is recoverable
  • THEN the agent retries extraction and calls validation again, up to 3 total attempts

Scenario: Agent exhausts retries and escalates

  • WHEN validation fails after 3 attempts
  • THEN the agent sends a natural language message to the user identifying the specific fields it could not resolve and asking for clarification

Requirement: Human-in-the-loop clarification

When the agent escalates to the user, the user SHALL be able to provide the missing information in natural language, and the agent SHALL incorporate the clarification and re-attempt extraction.

Scenario: User provides clarification

  • WHEN the agent asks for clarification about missing fields and the user responds
  • THEN the agent incorporates the user's response into the conversation context and produces an updated extraction

Scenario: Clarification via normal chat

  • WHEN the agent escalates for clarification
  • THEN the clarification request appears as a regular assistant message in the chat UI, and the user responds via the normal chat input