$error], $code, $error); } /** * @since 0.1.0 */ public static function failed(string $code, string $error): self { return new self(self::ERROR, null, null, ['summary' => $error], $code, $error); } /** * @since 0.1.0 */ public function ok(): bool { return $this->status === self::OK; } /** * The transcript part for call `$id` of tool `$name`. * * @since 0.1.0 * @return array */ public function toPart(string $id, string $name): array { $part = ['type' => 'tool_result', 'id' => $id, 'name' => $name, 'status' => $this->status]; if ($this->status === self::OK) { $part['data'] = $this->data; } if ($this->content !== null) { $part['content'] = $this->content; } if ($this->code !== '') { $part['code'] = $this->code; } if ($this->error !== '') { $part['error'] = $this->error; } if ($this->ui !== []) { $part['ui'] = $this->ui; } if ($this->tainted) { $part['tainted'] = true; } return $part; } /** * A stored part read back, old (`result`) or new (`data`). * * @since 0.1.0 * @param array $part */ public static function fromPart(array $part): self { return new self( (string) ($part['status'] ?? self::OK), $part['data'] ?? $part['result'] ?? null, isset($part['content']) ? (string) $part['content'] : null, is_array($part['ui'] ?? null) ? $part['ui'] : [], (string) ($part['code'] ?? ''), (string) ($part['error'] ?? ''), (bool) ($part['tainted'] ?? false), ); } }