# Datalumo PHP SDK

Official PHP SDK for the [datalumo](https://datalumo.app) API.

## Requirements

- PHP 8.2+

## Installation

```bash
composer require datalumo/php-sdk
```

## Quick start

```php
use Datalumo\PhpSdk\Datalumo;

$datalumo = new Datalumo('your-api-token');
```

## Collections

Collections are containers for your content. Use them to manage your data.

### List collections

```php
$response = $datalumo->collections()->list();

foreach ($response->data as $collection) {
    echo $collection->name;
}

// Filter by project or paginate
$response = $datalumo->collections()->list(project: 'docs', page: 2);
```

### Create a collection

```php
$collection = $datalumo->collections()->create('My Collection');
$collection = $datalumo->collections()->create('My Collection', project: 'docs');
```

### Update a collection

```php
$collection = $datalumo->collections()->update('collection-id', 'New Name');
```

### Delete a collection

```php
$datalumo->collections()->delete('collection-id');
```

## Entries

Entries are the individual pieces of content inside a collection.

```php
$entries = $datalumo->entries('collection-id');
```

### List entries

```php
$response = $entries->list();
$response = $entries->list(page: 2);
```

### Create an entry

```php
$entry = $entries->create([
    'raw_text' => 'The content to index',
    'title' => 'My Entry',
    'meta' => ['author' => 'John'],
    'source_url' => 'https://example.com/page',
    'source_type' => 'articles',
    'source_id' => '42',
]);
```

Only `raw_text` is required. All other fields are optional.

### Upsert an entry

Create or update by `source_type` and `source_id`:

```php
$entry = $entries->upsert([
    'raw_text' => 'Updated content',
    'source_type' => 'articles',
    'source_id' => '42',
]);
```

### Batch upsert

Create or update up to 50 entries at once:

```php
$result = $entries->batchUpsert([
    ['raw_text' => 'First entry', 'source_type' => 'articles', 'source_id' => '1'],
    ['raw_text' => 'Second entry', 'source_type' => 'articles', 'source_id' => '2'],
]);

echo $result['created']; // 2
echo $result['updated']; // 0
```

### Update an entry

```php
$entry = $entries->update('entry-id', ['title' => 'Updated Title']);
```

### Delete an entry

```php
$entries->delete('entry-id');
$entries->deleteBySource('articles', '42');
```

## Integrations

Integrations are the consumption layer — search, summarise, and chat all go through an integration. Each integration connects to one or more collections.

### List integrations

```php
$response = $datalumo->integrations()->list();
$response = $datalumo->integrations()->list(type: 'chatbot', project: 'support');
```

### Get an integration

```php
$integration = $datalumo->integrations()->get('integration-id');
```

### Create an integration

```php
$integration = $datalumo->integrations()->create([
    'type' => 'chatbot',
    'name' => 'Support Bot',
    'collection_ids' => ['col-1', 'col-2'],
    'accent_color' => '#3b82f6',
    'allowed_domains' => ['example.com'],
    'welcome_message' => 'How can I help?',
    'persona' => 'friendly',
]);
```

### Update / delete

```php
$datalumo->integrations()->update('integration-id', ['name' => 'Updated Bot']);
$datalumo->integrations()->delete('integration-id');
```

### Record an event

```php
$datalumo->integrations()->recordEvent('integration-id', [
    'event_type' => 'thumbs_up', // 'click', 'thumbs_up', or 'thumbs_down'
    'meta' => ['url' => 'https://example.com/page'],
]);
```

## Search

```php
$results = $datalumo->integrations()->search('integration-id', [
    'query' => 'how do refunds work',
    'threshold' => 0.3,
    'meta' => ['category' => 'billing'],
    'per_page' => 10,
    'page' => 1,
]);

foreach ($results->data as $entry) {
    echo $entry->title;
}

echo $results->summarisable; // true if good for summarisation
```

## Summarise

```php
$summary = $datalumo->integrations()->summarise('integration-id', [
    'query' => 'explain your refund policy',
    'format' => 'html', // 'markdown' (default) or 'html'
    'locale' => 'en',
]);

echo $summary->summary;
echo $summary->references;
echo $summary->hasRelevantResults;
```

## Chat

```php
$response = $datalumo->integrations()->chat('integration-id', [
    'message' => 'What is your refund policy?',
]);

echo $response->message;
echo $response->conversationId;

// Continue the conversation
$followUp = $datalumo->integrations()->chat('integration-id', [
    'message' => 'How long do I have?',
    'conversation_id' => $response->conversationId,
]);
```

## Streaming

The summarise and chat endpoints support streaming via SSE:

### Stream chat

```php
$stream = $datalumo->integrations()->streamChat('integration-id', [
    'message' => 'What is your refund policy?',
]);

foreach ($stream->text() as $chunk) {
    echo $chunk;
    flush();
}
```

### Stream summarise

```php
$stream = $datalumo->integrations()->streamSummarise('integration-id', [
    'query' => 'explain the refund policy',
]);

foreach ($stream->text() as $chunk) {
    echo $chunk;
    flush();
}
```

### Full text

```php
$stream = $datalumo->integrations()->streamChat('integration-id', [
    'message' => 'hello',
]);

$fullResponse = $stream->fullText();
```

### Raw stream events

```php
foreach ($stream as $event) {
    if ($event->isTextDelta()) {
        echo $event->data;
    } elseif ($event->isCitation()) {
        // $event->citation() returns ['title' => '...', 'url' => '...']
    } elseif ($event->isStreamEnd()) {
        break;
    }
}
```

## Error handling

```php
use Datalumo\PhpSdk\Exceptions\AuthenticationException;
use Datalumo\PhpSdk\Exceptions\ValidationException;
use Datalumo\PhpSdk\Exceptions\NotFoundException;
use Datalumo\PhpSdk\Exceptions\QuotaExceededException;
use Datalumo\PhpSdk\Exceptions\DatalumoException;

try {
    $datalumo->collections()->create('');
} catch (ValidationException $e) {
    $e->getMessage(); // "The name field is required."
    $e->errors();     // ['name' => ['The name field is required.']]
} catch (AuthenticationException $e) {
    // 401 or 403
} catch (QuotaExceededException $e) {
    // 402
} catch (NotFoundException $e) {
    // 404
} catch (DatalumoException $e) {
    // Any other API error
}
```

## Data objects

All API responses are returned as typed data objects:

| Class | Properties |
|-------|-----------|
| `Collection` | `id`, `organisationId`, `name`, `project`, `createdAt`, `updatedAt` |
| `Entry` | `id`, `collectionId`, `title`, `rawText`, `meta`, `sourceUrl`, `sourceType`, `sourceId`, `createdAt`, `updatedAt` |
| `Integration` | `id`, `name`, `project`, `type`, `accentColor`, `allowedDomains`, `isActive`, `settings`, `collectionIds`, `createdAt`, `updatedAt` |
| `PaginatedResponse` | `data`, `currentPage`, `lastPage`, `perPage`, `total`, `hasMorePages()` |
| `SearchResult` | Same as `PaginatedResponse` plus `summarisable` |
| `SummaryResponse` | `summary`, `references`, `data`, `hasRelevantResults` |
| `ChatResponse` | `conversationId`, `message` |
| `StreamResponse` | Iterable of `StreamEvent`, plus `text()`, `fullText()`, `conversationId()` |
| `StreamEvent` | `type`, `data`, `raw`, plus `isTextDelta()`, `isCitation()`, `isStreamEnd()`, `isError()` |
