|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace tobimori\Seo\Ai; |
| 4 | + |
| 5 | +/** |
| 6 | + * Value object representing a streamed AI response chunk. |
| 7 | + */ |
| 8 | +final class Chunk |
| 9 | +{ |
| 10 | + public const string TYPE_STREAM_START = 'stream-start'; |
| 11 | + public const string TYPE_STREAM_END = 'stream-end'; |
| 12 | + public const string TYPE_TEXT_START = 'text-start'; |
| 13 | + public const string TYPE_TEXT_DELTA = 'text-delta'; |
| 14 | + public const string TYPE_TEXT_COMPLETE = 'text-complete'; |
| 15 | + public const string TYPE_THINKING_START = 'thinking-start'; |
| 16 | + public const string TYPE_THINKING_DELTA = 'thinking-delta'; |
| 17 | + public const string TYPE_THINKING_COMPLETE = 'thinking-complete'; |
| 18 | + public const string TYPE_TOOL_CALL = 'tool-call'; |
| 19 | + public const string TYPE_TOOL_RESULT = 'tool-result'; |
| 20 | + public const string TYPE_ERROR = 'error'; |
| 21 | + |
| 22 | + private function __construct( |
| 23 | + public readonly string $type, |
| 24 | + public readonly mixed $payload = null, |
| 25 | + public readonly ?string $text = null |
| 26 | + ) { |
| 27 | + } |
| 28 | + |
| 29 | + public static function streamStart(array $payload = []): self |
| 30 | + { |
| 31 | + return new self(self::TYPE_STREAM_START, $payload); |
| 32 | + } |
| 33 | + |
| 34 | + public static function streamEnd(array $payload = []): self |
| 35 | + { |
| 36 | + return new self(self::TYPE_STREAM_END, $payload); |
| 37 | + } |
| 38 | + |
| 39 | + public static function textStart(array $payload = []): self |
| 40 | + { |
| 41 | + return new self(self::TYPE_TEXT_START, $payload); |
| 42 | + } |
| 43 | + |
| 44 | + public static function textDelta(string $text, array $payload = []): self |
| 45 | + { |
| 46 | + return new self(self::TYPE_TEXT_DELTA, $payload, $text); |
| 47 | + } |
| 48 | + |
| 49 | + public static function textComplete(array $payload = []): self |
| 50 | + { |
| 51 | + return new self(self::TYPE_TEXT_COMPLETE, $payload); |
| 52 | + } |
| 53 | + |
| 54 | + public static function thinkingStart(array $payload = []): self |
| 55 | + { |
| 56 | + return new self(self::TYPE_THINKING_START, $payload); |
| 57 | + } |
| 58 | + |
| 59 | + public static function thinkingDelta(string $text, array $payload = []): self |
| 60 | + { |
| 61 | + return new self(self::TYPE_THINKING_DELTA, $payload, $text); |
| 62 | + } |
| 63 | + |
| 64 | + public static function thinkingComplete(array $payload = []): self |
| 65 | + { |
| 66 | + return new self(self::TYPE_THINKING_COMPLETE, $payload); |
| 67 | + } |
| 68 | + |
| 69 | + public static function toolCall(array $payload = []): self |
| 70 | + { |
| 71 | + return new self(self::TYPE_TOOL_CALL, $payload); |
| 72 | + } |
| 73 | + |
| 74 | + public static function toolResult(array $payload = []): self |
| 75 | + { |
| 76 | + return new self(self::TYPE_TOOL_RESULT, $payload); |
| 77 | + } |
| 78 | + |
| 79 | + public static function error(string $message, array $payload = []): self |
| 80 | + { |
| 81 | + return new self(self::TYPE_ERROR, [ |
| 82 | + 'message' => $message, |
| 83 | + 'data' => $payload, |
| 84 | + ]); |
| 85 | + } |
| 86 | + |
| 87 | + public function isStreamStart(): bool |
| 88 | + { |
| 89 | + return $this->type === self::TYPE_STREAM_START; |
| 90 | + } |
| 91 | + |
| 92 | + public function isStreamEnd(): bool |
| 93 | + { |
| 94 | + return $this->type === self::TYPE_STREAM_END; |
| 95 | + } |
| 96 | + |
| 97 | + public function isTextStart(): bool |
| 98 | + { |
| 99 | + return $this->type === self::TYPE_TEXT_START; |
| 100 | + } |
| 101 | + |
| 102 | + public function isTextDelta(): bool |
| 103 | + { |
| 104 | + return $this->type === self::TYPE_TEXT_DELTA; |
| 105 | + } |
| 106 | + |
| 107 | + public function isTextComplete(): bool |
| 108 | + { |
| 109 | + return $this->type === self::TYPE_TEXT_COMPLETE; |
| 110 | + } |
| 111 | + |
| 112 | + public function isThinkingStart(): bool |
| 113 | + { |
| 114 | + return $this->type === self::TYPE_THINKING_START; |
| 115 | + } |
| 116 | + |
| 117 | + public function isThinkingDelta(): bool |
| 118 | + { |
| 119 | + return $this->type === self::TYPE_THINKING_DELTA; |
| 120 | + } |
| 121 | + |
| 122 | + public function isThinkingComplete(): bool |
| 123 | + { |
| 124 | + return $this->type === self::TYPE_THINKING_COMPLETE; |
| 125 | + } |
| 126 | + |
| 127 | + public function isToolCall(): bool |
| 128 | + { |
| 129 | + return $this->type === self::TYPE_TOOL_CALL; |
| 130 | + } |
| 131 | + |
| 132 | + public function isToolResult(): bool |
| 133 | + { |
| 134 | + return $this->type === self::TYPE_TOOL_RESULT; |
| 135 | + } |
| 136 | + |
| 137 | + public function isError(): bool |
| 138 | + { |
| 139 | + return $this->type === self::TYPE_ERROR; |
| 140 | + } |
| 141 | +} |
0 commit comments