Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"test:lint": "@php vendor/bin/pint --config https://raw.githubusercontent.com/DIJ-digital/pint-config/main/pint.json",
"test:unit": "pest",
"test:types": "phpstan",
"test:refactor": "rector --dry-run",
"test:refactor": "rector",
"test": [
"@test:lint",
"@test:type-coverage",
Expand Down
10 changes: 9 additions & 1 deletion peck.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
{
"preset": "base",
"ignore": {
"words": ["php", "langfuse", "param", "compilable", "eval"]
"words": [
"php",
"langfuse",
"param",
"compilable",
"eval",
"sha",
"tokenizer"
]
}
}
16 changes: 8 additions & 8 deletions src/Contracts/TransporterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,34 @@
interface TransporterInterface
{
/**
* @param array<string, mixed> $options
* @param array<string, mixed> $options
*/
public function request(string $method, string $uri, array $options = []): ResponseInterface;

/**
* @param array<string, mixed> $options
* @param array<string, mixed> $options
*/
public function get(string $uri, array $options = []): ResponseInterface;

/**
* @param array<string, mixed> $options
* @param array<string, mixed> $options
*/
public function post(string $uri, array $options = []): ResponseInterface;

/**
* @param array<string, mixed> $data
* @param array<string, mixed> $options
* @param array<string, mixed> $data
* @param array<string, mixed> $options
*/
public function postJson(string $uri, array $data = [], array $options = []): ResponseInterface;

/**
* @param array<string, mixed> $options
* @param array<string, mixed> $options
*/
public function delete(string $uri, array $options = []): ResponseInterface;

/**
* @param array<string, mixed> $data
* @param array<string, mixed> $options
* @param array<string, mixed> $data
* @param array<string, mixed> $options
*/
public function patchJson(string $uri, array $data = [], array $options = []): ResponseInterface;
}
10 changes: 8 additions & 2 deletions src/Langfuse.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ public function __construct(
private readonly TransporterInterface $transporter,
private readonly string $environment = 'default',
private readonly string $label = 'latest',
) {
}
) {}

public function prompt(): Prompt
{
Expand All @@ -37,4 +36,11 @@ public function score(): Score
transporter: $this->transporter,
);
}

public function session(): Session
{
return new Session(
transporter: $this->transporter,
);
}
}
39 changes: 39 additions & 0 deletions src/Responses/SessionListResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace DIJ\Langfuse\PHP\Responses;

use DIJ\Langfuse\PHP\ValueObjects\MetaData;

readonly class SessionListResponse
{
/**
* @param array<int, SessionResponse> $data
*/
public function __construct(
public array $data,
public MetaData $meta,
) {}

/**
* @param array{
* data: array<int, array{
* id: string,
* createdAt: string,
* projectId: string,
* environment?: string|null,
* bookmarked?: bool|null,
* public?: bool|null
* }>,
* meta: array{page: int, limit: int, totalPages: int, totalItems: int}
* } $data
*/
public static function fromArray(array $data): self
{
return new self(
data: array_map(fn (array $item): SessionResponse => SessionResponse::fromArray($item), $data['data']),
meta: MetaData::fromArray($data['meta']),
);
}
}
45 changes: 45 additions & 0 deletions src/Responses/SessionResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace DIJ\Langfuse\PHP\Responses;

readonly class SessionResponse
{
/**
* @param array<int, mixed> $traces
*/
public function __construct(
public string $id,
public string $createdAt,
public string $projectId,
public ?string $environment = null,
public ?bool $bookmarked = null,
public ?bool $public = null,
public array $traces = [],
) {}

/**
* @param array{
* id: string,
* createdAt: string,
* projectId: string,
* environment?: string|null,
* bookmarked?: bool|null,
* public?: bool|null,
* traces?: array<int, mixed>
* } $data
*/
public static function fromArray(array $data): self
{
return new self(
id: $data['id'],
createdAt: $data['createdAt'],
projectId: $data['projectId'],
environment: $data['environment'] ?? null,
bookmarked: $data['bookmarked'] ?? null,
public: $data['public'] ?? null,
traces: $data['traces'] ?? [],
);
}
}
77 changes: 77 additions & 0 deletions src/Session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace DIJ\Langfuse\PHP;

use DIJ\Langfuse\PHP\Contracts\TransporterInterface;
use DIJ\Langfuse\PHP\Responses\SessionListResponse;
use DIJ\Langfuse\PHP\Responses\SessionResponse;
use JsonException;

class Session
{
public function __construct(private readonly TransporterInterface $transporter) {}

/**
* @throws JsonException
*/
public function get(string $sessionId): SessionResponse
{
$response = $this->transporter->get(
uri: sprintf('/api/public/sessions/%s', urlencode($sessionId)),
);

/** @var array{
* id: string,
* createdAt: string,
* projectId: string,
* environment: string|null,
* bookmarked: bool|null,
* public: bool|null,
* traces: array<int, mixed>
* } $data
*/
$data = json_decode($response->getBody()->getContents(), true, flags: JSON_THROW_ON_ERROR);

return SessionResponse::fromArray($data);
}

/**
* @throws JsonException
*/
public function list(
?int $page = null,
?int $limit = null,

Check failure on line 45 in src/Session.php

View workflow job for this annotation

GitHub Actions / Formats P8.4 - ubuntu-latest - prefer-lowest

Method DIJ\Langfuse\PHP\Session::list() has parameter $environment with no value type specified in iterable type array.

Check failure on line 45 in src/Session.php

View workflow job for this annotation

GitHub Actions / Formats P8.4 - ubuntu-latest - prefer-lowest

Method DIJ\Langfuse\PHP\Session::list() has parameter $environment with no value type specified in iterable type array.

Check failure on line 45 in src/Session.php

View workflow job for this annotation

GitHub Actions / Formats P8.4 - ubuntu-latest - prefer-stable

Method DIJ\Langfuse\PHP\Session::list() has parameter $environment with no value type specified in iterable type array.
?string $fromTimestamp = null,
?string $toTimestamp = null,
?array $environment = null,
): SessionListResponse {
$response = $this->transporter->get(
uri: '/api/public/sessions',
options: ['query' => array_filter([
'page' => $page,
'limit' => $limit,
'fromTimestamp' => $fromTimestamp,
'toTimestamp' => $toTimestamp,
'environment' => $environment,
])]
);

/** @var array{
* data: array<int, array{
* id: string,
* createdAt: string,
* projectId: string,
* environment: string|null,
* bookmarked: bool|null,
* public: bool|null
* }>,
* meta: array{page: int, limit: int, totalPages: int, totalItems: int}
* } $data
*/
$data = json_decode($response->getBody()->getContents(), true, flags: JSON_THROW_ON_ERROR);

return SessionListResponse::fromArray($data);
}
}
51 changes: 51 additions & 0 deletions src/Testing/Responses/GetSessionListResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace DIJ\Langfuse\PHP\Testing\Responses;

use GuzzleHttp\Psr7\Response;

class GetSessionListResponse extends Response
{
/**
* @param array<array<string>|string> $headers
*/
public function __construct(int $status = 200, array $headers = [], string $version = '1.1', ?string $reason = null)
{
parent::__construct($status, $headers, (string) json_encode($this->payload()), $version, $reason);
}

/**
* @return array<string, mixed>
*/
public function payload(): array
{
return [
'data' => [
[
'id' => 'session-abc123',
'createdAt' => '2025-01-22T10:00:00.000Z',
'projectId' => 'proj-abc123',
'environment' => 'production',
'bookmarked' => false,
'public' => false,
],
[
'id' => 'session-def456',
'createdAt' => '2025-01-22T09:00:00.000Z',
'projectId' => 'proj-abc123',
'environment' => 'development',
'bookmarked' => true,
'public' => true,
],
],
'meta' => [
'page' => 1,
'limit' => 10,
'totalPages' => 1,
'totalItems' => 2,
],
];
}
}
47 changes: 47 additions & 0 deletions src/Testing/Responses/GetSessionResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace DIJ\Langfuse\PHP\Testing\Responses;

use GuzzleHttp\Psr7\Response;

class GetSessionResponse extends Response
{
/**
* @param array<array<string>|string> $headers
* @param array<string, mixed> $data
*/
public function __construct(int $status = 200, array $headers = [], string $version = '1.1', ?string $reason = null, array $data = [])
{
parent::__construct($status, $headers, (string) json_encode($this->payload($data)), $version, $reason);
}

/**
* @param array<string, mixed> $data
* @return array<string, mixed>
*/
public function payload(array $data = []): array
{
return array_merge([
'id' => 'session-abc123',
'createdAt' => '2025-01-22T10:00:00.000Z',
'projectId' => 'proj-abc123',
'environment' => 'production',
'bookmarked' => false,
'public' => false,
'traces' => [
[
'id' => 'trace-001',
'name' => 'trace-in-session',
'timestamp' => '2025-01-22T10:30:00.000Z',
],
[
'id' => 'trace-002',
'name' => 'another-trace',
'timestamp' => '2025-01-22T10:35:00.000Z',
],
],
], $data);
}
}
Loading
Loading