PHP client for [Laya](https://github.com/NandhaKishorM/laya), the open-source System 1 decision engine. You give Laya some state (free text or a structured document) and a set of questions. It answers each question with a choice, a score or a yes/no probability, plus a confidence value.
The SDK is a thin layer over Laya's HTTP API. It builds the JSON request, sends it over any PSR-18 HTTP client, and wraps the response in small value objects.
## Requirements
- PHP 8.1+ (uses `readonly` properties and `never`-style `throw` expressions)
- A running Laya server (the default URL is `http://127.0.0.1:8000`)
- An HTTP client: Guzzle is installed with the package. If your app already uses another PSR-18 client (Symfony HttpClient, etc.), the SDK discovers and prefers it through `php-http/discovery`.
## Installation
```bash
composer require liquidsky/laya_php_sdk
```
## Quick start
```php
useLaya\Laya;
useLaya\Question\ChoiceQuestion;
useLaya\Question\QuestionSet;
useLaya\Question\ScoreQuestion;
useLaya\Question\YesNoQuestion;
$client=Laya::client('http://127.0.0.1:8000');
$questions=QuestionSet::make()
->add(
(newChoiceQuestion('category','What is this ticket about?'))
->option('billing','Payments, invoices, refunds')
->option('bug','Something is broken')
->option('other','Anything else')
)
->add(newScoreQuestion('urgency','How urgent is this?',['not urgent','soon','critical']))
->add(newYesNoQuestion('angry','Is the customer upset?'));
$decisions=$client->decide('My card was charged twice and nobody replies!!',$questions);
A string `$state` is wrapped as `{"body": "<string>"}`. An array is sent as-is. `model` is only included if you pass it.
3. **Read the answers.** The response's `answers.<name>` entries become `Decision` objects inside a `DecisionSet`. `routing.model` is exposed as `$decisions->routedModel`.
## Question types
| Class | Wire `type` | Constructor / builder | Read with |
| `confidence()` | Confidence as float, or `null` |
| `distribution()` | Full probability distribution from the response |
## Other endpoints
```php
$client->models(); // GET /models, returns the decoded JSON as an array
```
## Errors
- HTTP status 400 or above throws `Laya\Exception\UnexpectedResponseException`. Its message includes the server's `detail` field when present, and `statusCode()` returns the HTTP status.
- Invalid JSON in a response or request throws `JsonException`.
- Network failures surface as whatever your PSR-18 client throws (`Psr\Http\Client\ClientExceptionInterface`).
- `Laya::client()` throws if no PSR-18 client or PSR-17 factory can be discovered.
## Using your own HTTP client
`Laya::client()` auto-discovers dependencies. To control them (timeouts, auth headers, middleware), construct the client yourself: