# B-connex Core AI & Voice File Map

Generated by Gwen (qwen2.5-coder:7b) on 2026-07-06 22:56:49

## app/Services/AI/AIProvider.php

The `app/Services/AI/AIProvider.php` file defines an interface for a service that provides artificial intelligence capabilities. The high-level purpose is to standardize methods for obtaining AI-related data and executing requests. The structural layout consists of an interface named `AIProvider`. Key methods include `name()`, which returns the provider's name, and `complete(array $messages, array $options = [])`, which processes a list of messages and optional parameters to generate a response. The interface does not have any dependencies as it is purely abstract.

---

## app/Services/AI/AIRouter.php

The `AIRouter` class in the `app/Services/AI/AIRouter.php` file serves as a router for AI providers, handling configuration and provider resolution based on tier and environment variables. It includes methods like `forTier`, `default`, and `withLocale`. The class depends on the `Config` and `Logger` services for configuration and logging, respectively. Key methods resolve AI providers by slug using strategies such as checking reachability and availability of different AI services like Copilot, Ollama, Anthropic, and a local provider. The `languageSystemMessage` method provides language-specific system messages based on locale.

---

## app/Services/AI/AnthropicProvider.php

The `AnthropicProvider` class in the file is a final implementation of an AI provider service for Anthropic's API, implementing the `AIProvider` interface. It includes methods to initialize with an API key and model, retrieve the provider name, and complete text generation tasks using specified messages and options. The `complete` method handles constructing a request payload, making an HTTP POST call to the Anthropic API, processing the response, and returning the generated text or error messages. The class utilizes cURL for HTTP requests and requires dependencies on PHP's strict typing and JSON handling functions.

---

## app/Services/AI/ContextIndexer.php

The `ContextIndexer` class in the `App\Services\AI` namespace is designed to index and retrieve relevant content from a file based on a query. It accepts a file path upon construction. The primary method, `retrieve`, processes the file's content by splitting it into sections, tokenizing both the query and each section's text, and scoring each section based on how well its tokens match the query tokens. Sections are then sorted by score, with higher scores indicating more relevance to the query. If a mandatory section is found (flagged as "Project one-liner"), it is included at the beginning of the result. The method limits the output size in bytes and ensures that only relevant sections are returned, effectively summarizing the file's content based on user input. The class depends on the `file_get_contents` function to read the file and has a private helper method `tokenize` for breaking down text into meaningful words while excluding common stopwords.

---

## app/Services/AI/CopilotProvider.php

The `CopilotProvider` class in the file is a final implementation of the `AIProvider` interface, designed to interact with an AI copilot service. It accepts a base URL and model as parameters during instantiation. The `name` method returns a string identifier for the provider. The `complete` method takes an array of messages and optional options, constructs a JSON payload, makes an HTTP POST request using cURL to the specified AI endpoint, and processes the response to return a completion result or error message. It depends on PHP's `curl` extension for making network requests.

---

## app/Services/AI/FileIndexer.php

The `FileIndexer` class in the `App\Services\AI` namespace provides a search functionality for PHP files, Markdown documents, SQL scripts, and JSON configurations within specified directories. It uses a tokenized query to match against file paths, filenames, and line content, scoring results based on relevance. The `search` method collects files using the `collectFiles` method, which recursively scans designated directories, filters files by extension, and returns a formatted string of matched code snippets and documentation excerpts up to a specified byte limit.

---

## app/Services/AI/LocalProvider.php

The `LocalProvider` class in the file serves as a concrete implementation of the `AIProvider` interface within the `App\Services\AI` namespace. It provides a local stub for AI completion services, offering methods to return the service's name and to generate a completion based on an array of messages with optional options. The `complete` method constructs a prompt from the last message content and appends a prefix indicating it's from a local stub, limiting the output to 280 characters.

---

## app/Services/AI/OllamaProvider.php

The `OllamaProvider` class in the `app/Services/AI/OllamaProvider.php` file is a final implementation of an AI provider service for interacting with an Ollama AI model. It handles configuration via constructor parameters and environment variables, provides methods to retrieve context packs and generate completions based on messages and options. Key methods include `name()`, `miraContextPack()`, and `complete()`. The class also includes utility methods for logging requests and responses, as well as guessing the caller of service methods. It depends on external libraries such as `curl` for HTTP requests, and custom classes like `FileIndexer`, `ContextIndexer`, and potentially others for code context and message processing.

---

## app/Services/AI/OpenAIProvider.php

This PHP class `OpenAIProvider` implements the `AIProvider` interface, providing methods for interacting with OpenAI's API. It includes a constructor to set an API key and model, a method `name()` to return the provider name along with the model, and a method `complete()` to send messages to OpenAI and receive completions. The `complete()` method handles HTTP requests using cURL, sending JSON payloads containing messages, options, and authentication details. It returns the AI-generated content or error messages based on API responses.

---

## app/Services/VoiceAI/ElevenLabsService.php

The `ElevenLabsService` class in the `app/Services/VoiceAI/ElevenLabsService.php` file provides a service for synthesizing text into speech using the ElevenLabs API. It is structured as a final class with private properties and methods, including a constructor that injects a configuration object to fetch the API key. The `synthesize` method sends HTTP POST requests to the ElevenLabs API if the API key is valid; otherwise, it generates mock audio data. Helper methods such as `getMockAudio`, which creates synthetic audio based on text length, and `linear2ulaw`, which converts linear PCM samples to μ-law encoded audio bytes, support the core functionality.

---

## app/Services/VoiceAI/MedicalRegistryService.php

The `MedicalRegistryService` class in the `app/Services/VoiceAI/MedicalRegistryService.php` file is a final class that manages operations related to medical registry entries. It interacts with a database through an instance of the `Database` class, which it injects via its constructor. The class provides methods for listing entries based on organization ID and optionally active status (`list`), retrieving a specific entry by ID and organization ID (`get`), creating new entries (`create`), updating existing entries (`update`), importing CSV data into the registry (`importCsv`), finding matches based on criteria (`findMatch`), and formatting entries for prompts in different verticals (`formatForPrompt`). The class also includes a private helper method, `nextAvailabilityLabel`, which processes availability JSON to determine the next available time slot.

---

## app/Services/VoiceAI/VoiceAIService.php

The `VoiceAIService` class in the provided file serves as a comprehensive service for managing and processing voice AI functionalities within an organization. It includes methods for checking entitlements, sending overage alerts, listing and managing agents, prompts, phone numbers, sessions, usage statistics, and calculating costs. The class interacts with a database to retrieve and store data, using a `Database` instance provided in its constructor along with a `Config` instance. Key methods handle the creation, updating, and deletion of voice AI agents and their associated prompts and phone numbers, as well as managing session data and usage tracking.

---

## app/Services/VoiceAI/VoicePhoneService.php

This PHP file defines the `VoicePhoneService` class within the `App\Services\VoiceAI` namespace, which is designed to manage phone numbers and their associated configurations using Twilio. It includes methods to check for Twilio credentials, search for available phone numbers, provision new phone numbers with specified providers, and build TwiML responses for voice interactions. The class depends on instances of `Database` and `Config`, and it uses an instance of `VoiceAIService` internally to handle provisioning details.

---

