[refactor] streamline crate structure, update dependencies, and integrate CLI functionalities

This commit is contained in:
2025-08-13 14:05:13 +02:00
parent 128db0f733
commit 5c64677e79
17 changed files with 812 additions and 1235 deletions

View File

@@ -2,9 +2,7 @@
name = "polyscribe-protocol"
version = "0.1.0"
edition = "2024"
license = "MIT"
[dependencies]
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.142"
thiserror = "1"

View File

@@ -1,90 +1,60 @@
// SPDX-License-Identifier: MIT
// PolyScribe Protocol (PSP/1): JSON-RPC 2.0 over NDJSON on stdio
use serde::{Deserialize, Serialize};
use serde_json::Value;
/// Plugin capabilities as reported by `--capabilities`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Capabilities {
pub name: String,
pub version: String,
/// Protocol identifier (e.g., "psp/1")
pub protocol: String,
/// Role (e.g., pipeline, tool, generator)
pub role: String,
/// Supported command names
pub commands: Vec<String>,
}
/// Generic JSON-RPC 2.0 request for PSP/1
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcRequest {
pub jsonrpc: String, // "2.0"
#[derive(Debug, Serialize, Deserialize)]
pub struct Request {
pub id: String,
pub method: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub params: Option<serde_json::Value>,
pub params: Option<Value>,
}
/// Error object for JSON-RPC 2.0
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcError {
pub code: i64,
#[derive(Debug, Serialize, Deserialize)]
pub struct Response {
pub id: String,
pub result: Option<Value>,
pub error: Option<ErrorObj>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ErrorObj {
pub code: i32,
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<serde_json::Value>,
pub data: Option<Value>,
}
/// Generic JSON-RPC 2.0 response for PSP/1
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum StreamItem {
/// Progress notification (out-of-band in stream, not a JSON-RPC response)
Progress(Progress),
/// A proper JSON-RPC response with a result
Result(JsonRpcResponse),
#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "event", content = "data")]
pub enum ProgressEvent {
Started,
Message(String),
Percent(f32),
Finished,
}
/// JSON-RPC 2.0 Response envelope containing either result or error.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonRpcResponse {
pub jsonrpc: String, // "2.0"
pub id: String,
#[serde(flatten)]
pub outcome: JsonRpcOutcome,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum JsonRpcOutcome {
Ok { result: serde_json::Value },
Err { error: JsonRpcError },
}
/// Progress event structure for PSP/1 streaming
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Progress {
/// 0..=100
pub pct: u8,
/// Short phase name
pub stage: Option<String>,
/// Human-friendly detail
pub message: Option<String>,
}
/// Convenience helpers to build items
impl StreamItem {
pub fn progress(pct: u8, stage: impl Into<Option<String>>, message: impl Into<Option<String>>) -> Self {
StreamItem::Progress(Progress { pct, stage: stage.into(), message: message.into() })
}
pub fn ok(id: impl Into<String>, result: serde_json::Value) -> Self {
StreamItem::Result(JsonRpcResponse { jsonrpc: "2.0".into(), id: id.into(), outcome: JsonRpcOutcome::Ok { result } })
}
pub fn err(id: impl Into<String>, code: i64, message: impl Into<String>, data: Option<serde_json::Value>) -> Self {
StreamItem::Result(JsonRpcResponse {
jsonrpc: "2.0".into(),
impl Response {
pub fn ok(id: impl Into<String>, result: Value) -> Self {
Self {
id: id.into(),
outcome: JsonRpcOutcome::Err { error: JsonRpcError { code, message: message.into(), data } },
})
result: Some(result),
error: None,
}
}
pub fn err(
id: impl Into<String>,
code: i32,
message: impl Into<String>,
data: Option<Value>,
) -> Self {
Self {
id: id.into(),
result: None,
error: Some(ErrorObj {
code,
message: message.into(),
data,
}),
}
}
}