mirror of
https://github.com/logos-storage/logos-storage-go.git
synced 2026-01-02 13:23:11 +00:00
5.6 KiB
5.6 KiB
Codex Client Go Library - AI Agent Guide
Project Overview
A lightweight Go client library for interacting with Codex decentralized storage nodes. The project provides:
- Core Library:
communities/codex_client.go- HTTP client wrapping Codex REST API - CLI Tools:
cmd/upload/andcmd/download/- Command-line utilities for file operations
Quickstart (build • run • test)
- Build CLIs:
go build -o bin/codex-upload ./cmd/upload && go build -o bin/codex-download ./cmd/download - Upload:
./bin/codex-upload -file test-data.bin -host localhost -port 8080 - Download:
./bin/codex-download -cid <CID> -file out.bin -host localhost -port 8080 - Unit tests:
go test -v ./communities - Integration test:
go test -v -tags=integration ./communities -run Integration
Architecture & Design Patterns
Client Structure
The CodexClient type in communities/codex_client.go is the central abstraction:
- Wraps Codex HTTP API (
/api/codex/v1/data/*) with Go-idiomatic methods - Supports both network downloads (
/network/stream) and local downloads (direct CID access) - Context-aware operations for cancellation support (
DownloadWithContext,LocalDownloadWithContext) - All uploads use
application/octet-streamwithContent-Dispositionheader for filenames
Data Flow Pattern
- Upload:
file → io.Reader → HTTP POST → CID string - Download:
CID string → HTTP GET → io.Writer → file - Network vs Local: Network download uses
/network/streamendpoint (retrieves from DHT), local uses direct/data/{cid}
Key Implementation Details
- Streaming with Cancellation: Custom
copyWithContext(64KB buffer) checks context cancellation between chunks - Default Timeout: 60s for HTTP operations (configurable via
SetRequestTimeout) - Error Handling: Errors wrapped with
%w; HTTP errors include status code and body - CID Format: CIDv1 + multibase base58btc (prefix
z), using therawcodec (Dv). Example:zDvZRwzm...
CID format cheatsheet
- Multibase prefix
z= base58btc;Dvdenotes CIDv1 + raw codec - CIDs commonly start with
zDv...in Codex (e.g.,zDvZRwzmRigWseNB...) - Upload responses may include a trailing newline; client trims via
strings.TrimSpace
CLI Commands & Usage
Build Commands
# Build upload tool
go build -o codex-upload ./cmd/upload
# Build download tool
go build -o codex-download ./cmd/download
# Or build both
go build ./cmd/...
Running CLI Tools
# Upload a file (defaults to localhost:8080)
go run ./cmd/upload -file test-data.bin -host localhost -port 8080
# Download by CID
go run ./cmd/download -cid <CID> -file output.bin -host localhost -port 8080
CLI notes:
- Download writes to an
io.Writer(a created file). On failure, the tool deletes the partial file. - Flags follow Go
flagconventions;-h/--helpprints usage.
Development Conventions
Module & Import Pattern
- Module name:
go-codex-client(defined ingo.mod) - Internal imports use:
"go-codex-client/communities" - No external dependencies beyond Go stdlib (HTTP, context, io operations)
Error Handling Style
- Use
log.Fatalfin CLI tools for unrecoverable errors - Return wrapped errors from library functions:
fmt.Errorf("context: %w", err) - HTTP errors include status codes and response body in error messages
Flag Usage Pattern (CLI)
Standard pattern across CLI tools:
var (
host = flag.String("host", "localhost", "Codex host")
port = flag.String("port", "8080", "Codex port")
// tool-specific flags...
)
flag.Parse()
Status & Known Issues
- Download CLI fixed: creates the output file and passes it as
io.Writertoclient.Download; removes the file on error.
Integration Notes
- Codex API Version: Uses v1 endpoints (
/api/codex/v1/) - Content Type: Always
application/octet-streamfor uploads - CID Response: Upload returns a CID string (often newline-terminated); client trims whitespace
- Network Operations: Assumes Codex node is running and accessible at specified host:port
Common failure modes (and fixes)
- 404 on download: CID not found or node can’t retrieve from network; verify CID and node connectivity
- 500/Bad Gateway: upstream node error; retry or check Codex node logs
- Timeouts: increase via
client.SetRequestTimeout(...)or setCODEX_TIMEOUT_MSin integration tests - Partial file on download error: download CLI already deletes the file on failure
Testing & Debugging
- Unit tests (stdlib) in
communities/codex_client_test.gocover:- Upload success (headers validated) returning CID
- Download success to a
bytes.Buffer - Cancellation: streaming handler exits on client cancel; fast and warning-free
- Run:
go test -v ./communities - Integration test (requires Codex node):
communities/codex_client_integration_test.go- Build tag-gated: run with
go test -v -tags=integration ./communities -run Integration - Env:
CODEX_HOST(defaultlocalhost),CODEX_API_PORT(default8080), optionalCODEX_TIMEOUT_MS - Uses random 1KB payload, logs hex preview, uploads, downloads, and verifies equality
- Includes LocalDownload test: uploads→triggers async download→polls HasCid (10s timeout)→verifies content
- All tests use
RemoveCidcleanup in defer blocks to prevent storage accumulation
- Build tag-gated: run with
- Debug by observing HTTP responses and Codex node logs; client timeout defaults to 60s
Repo Meta
.gitignoreexcludes build artifacts (bin/, binaries), coverage outputs, and IDE filesREADME.mddocuments build/run/test workflowsCOPYRIGHT.mdprovides MIT license