/**
* Sarashina API (SB Intuitions) provider for pi.
*
* Sarashina exposes an OpenAI-compatible Chat Completions endpoint at
* https://api.sarashina.ai/v1, so the built-in "openai-completions" API
* implementation is used as-is; no custom streaming is needed.
*
* Compat notes (verified against pi-ai's openai-completions implementation):
* - Auto-detected defaults for an unknown provider would send `store`,
* `stream_options`, `strict` on tools, the `developer` role for reasoning
* models, and prompt-cache fields. Sarashina ignores undocumented params,
* but we disable them explicitly to keep the wire format minimal.
* - `reasoning_effort` is only sent for the thinking model, and only when a
* thinking level other than off is selected (Sarashina returns 400 if it is
* sent to a non-thinking model; pi already gates it on `model.reasoning`).
* - The thinking model's encrypted `reasoning` field is captured as a thinking
* block whose signature is "reasoning", and replayed verbatim on subsequent
* requests, preserving thinking context across turns.
* - Sarashina only accepts message `content` as a plain string; the OpenAI-style
* array-of-parts form (which pi emits for user messages) returns
* 400 "Invalid JSON". An `onPayload` rewrite inside streamSarashina flattens
* text parts back into a string before the request is sent.
* - `sarashina3-guard` is a moderation-only model: the API documents `tools`
* and `tool_choice` as unsupported for it (verified: 400 "Bad Request"). pi
* attaches `tools` whenever the agent has tools (and `tools: []` once
* history has tool calls), so the same rewrite strips both fields for that
* model only.
* - The thinking model's encrypted `reasoning` would render in the TUI as an
* unreadable base64 "thinking" block; a markdown transformer replaces the
* display with a placeholder while leaving the stored content (and thus
* replay) untouched.
*
* Usage:
* SARASHINA_API_KEY=... pi -e ./packages/pi-sarashina --model sarashina/sarashina3-nano
* pi install ./packages/pi-sarashina # persistent install
*/
import { openAICompletionsApi } from "@earendil-works/pi-ai";
import type {
Api,
AssistantMessageEventStream,
Context,
Model,
SimpleStreamOptions,
} from "@earendil-works/pi-ai";
import type { ExtensionAPI, ExtensionContext, ProviderModelConfig } from "@earendil-works/pi-coding-agent";
// Compat flags shared by all Sarashina models: don't send params the API does
// not document. maxTokensField defaults to "max_completion_tokens", which is
// what Sarashina expects (range 1-8000).
const SARASHINA_COMPAT: ProviderModelConfig["compat"] = {
supportsStore: false,
supportsUsageInStreaming: false,
supportsStrictMode: false,
supportsDeveloperRole: false,
requiresToolResultName: false,
supportsLongCacheRetention: false,
};
const FREE_COST = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 };
const openaiCompletions = openAICompletionsApi();
// Sarashina-specific wire fixes, applied inside streamSarashina's onPayload
// so they hold for every caller of the provider stream — not only the paths
// that emit before_provider_request. (In pi 0.85.1 the agent streamFn injects
// onPayload into summarization calls too, but any path that invokes the
// provider stream directly would otherwise bypass the rewrite.)
// - Sarashina rejects OpenAI-style content part arrays with 400 "Invalid
// JSON"; flatten text parts back to a plain string.
// - sarashina3-guard is moderation-only: the API documents tools/tool_choice
// as unsupported for it (verified: 400 "Bad Request"), and pi attaches
// `tools` whenever the agent has tools (plus `tools: []` once history has
// tool calls), so strip both fields for that model.
function rewriteSarashinaPayload(payload: unknown, modelId: string): unknown {
const p = payload as
| { messages?: Array<{ content?: unknown }>; tools?: unknown; tool_choice?: unknown }
| undefined;
if (!p) return payload;
if (modelId === "sarashina3-guard") {
delete p.tools;
delete p.tool_choice;
}
if (!Array.isArray(p.messages)) return p;
for (const message of p.messages) {
if (Array.isArray(message.content)) {
message.content = (message.content as Array<{ type?: string; text?: string }>)
.filter((part) => part?.type === "text" && typeof part.text === "string")
.map((part) => part.text)
.join("\n");
}
}
return p;
}
// Provider-level retries honoring Retry-After, scoped to sarashina only.
// Rationale: Sarashina rate-limits per fixed 60-second window. pi's agent-level
// retry (on by default) ignores Retry-After and waits 2s/4s/8s, so it can burn
// all attempts inside a single window; pi's provider-level retry waits exactly
// Retry-After but ships disabled (retry.provider.maxRetries = 0, a global
// setting). We turn it on here unless the caller already configured it, so a
// 429 waits out the window instead of failing fast. Losing the race still
// falls back to the agent-level retry.
function streamSarashina(
model: Model<Api>,
context: Context,
options?: SimpleStreamOptions,
): AssistantMessageEventStream {
const callerOnPayload = options?.onPayload;
return openaiCompletions.streamSimple(model, context, {
...options,
maxRetries: options?.maxRetries || 3,
// Compose after any caller-supplied onPayload (which is what carries
// before_provider_request emissions) so the sarashina rewrite always
// runs last and cannot be bypassed.
onPayload: async (payload, m) =>
rewriteSarashinaPayload((await callerOnPayload?.(payload, m)) ?? payload, model.id),
});
}
const MODELS: ProviderModelConfig[] = [
{
id: "sarashina3.2-mini-thinking",
name: "Sarashina 3.2 mini (thinking)",
reasoning: true,
// Sarashina only accepts low/medium/high. Unsupported levels are hidden
// (clamped to the nearest supported one); "off" stays selectable and
// sends no reasoning_effort at all.
thinkingLevelMap: {
minimal: null,
low: "low",
medium: "medium",
high: "high",
},
input: ["text"],
cost: FREE_COST,
contextWindow: 128000,
maxTokens: 8000,
compat: { ...SARASHINA_COMPAT, supportsReasoningEffort: true },
},
{
id: "sarashina3-mini",
name: "Sarashina 3 mini",
reasoning: false,
input: ["text"],
cost: FREE_COST,
contextWindow: 128000,
maxTokens: 8000,
compat: { ...SARASHINA_COMPAT, supportsReasoningEffort: false },
},
{
id: "sarashina3-nano",
name: "Sarashina 3 nano",
reasoning: false,
input: ["text"],
cost: FREE_COST,
contextWindow: 128000,
maxTokens: 8000,
compat: { ...SARASHINA_COMPAT, supportsReasoningEffort: false },
},
{
// Moderation model: returns e.g. "安全" or a harmful-category verdict.
// No tools, no thinking.
id: "sarashina3-guard",
name: "Sarashina 3 guard",
reasoning: false,
input: ["text"],
cost: FREE_COST,
contextWindow: 64000,
maxTokens: 8000,
compat: { ...SARASHINA_COMPAT, supportsReasoningEffort: false },
},
];
export default function (pi: ExtensionAPI) {
pi.registerProvider("sarashina", {
name: "Sarashina",
// SARASHINA_BASE_URL can point at a local logging proxy for tracing;
// unset it talks to the real endpoint.
baseUrl: process.env.SARASHINA_BASE_URL ?? "https://api.sarashina.ai/v1",
// Resolved per request from process.env only - pi does not read .env files.
apiKey: "$SARASHINA_API_KEY",
authHeader: true, // Authorization: Bearer <key>
api: "openai-completions",
models: MODELS,
// Invoked because model.api matches the provider's api value; delegates
// to the stock openai-completions stream with sarashina-scoped retry.
streamSimple: streamSarashina,
});
// The thinking model's `reasoning` is an opaque encrypted blob; rendering it
// as "thinking" text is unreadable. The transformer below swaps the displayed
// markdown for a placeholder — display-only, the stored thinking content is
// untouched so replay still sends the base64 verbatim. While a sarashina
// model is selected we also relabel collapsed thinking blocks, restoring
// pi's default when another provider's model is selected.
let sarashinaActive = false;
const applyThinkingUI = (active: boolean, ctx: ExtensionContext) => {
sarashinaActive = active;
if (active) {
ctx.ui.setHiddenThinkingLabel("(encrypted reasoning)");
} else {
ctx.ui.setHiddenThinkingLabel();
}
};
pi.on("session_start", (_event, ctx) => applyThinkingUI(ctx.model?.provider === "sarashina", ctx));
pi.on("model_select", (event, ctx) => applyThinkingUI(event.model.provider === "sarashina", ctx));
// Base64 blob heuristic: long, base64-charset-only thinking markdown.
// Keeps a hypothetical future plaintext-reasoning sarashina model readable.
const ENCRYPTED_REASONING_MD = /^[A-Za-z0-9+/=\s]+$/;
pi.registerMarkdownTransformer((markdown, { messageType }) => {
if (messageType !== "assistant-thinking" || !sarashinaActive) return markdown;
const text = markdown.trim();
if (text.length < 64 || !ENCRYPTED_REASONING_MD.test(text)) return markdown;
return "*(encrypted reasoning — replayed verbatim on subsequent turns)*";
});
}
// Tiny logging reverse proxy: 127.0.0.1:8787 -> https://api.sarashina.ai
// Logs request line, response status/headers (retry-after!), and each SSE
// line with a timestamp delta so stalls in the stream are visible.
import http from "node:http";
import https from "node:https";
const UPSTREAM = process.env.UPSTREAM_HOST ?? "api.sarashina.ai";
const PORT = Number(process.env.TRACE_PORT ?? 8787);
const log = (...a) => console.log(new Date().toISOString(), ...a);
http.createServer((req, res) => {
const t0 = Date.now();
const chunks = [];
// Propagate downstream disconnects upstream: if the client (pi) aborts or
// its socket dies, tear down the in-flight upstream request/response
// instead of leaving generation running and sockets leaking.
let u = null;
let ur = null;
res.on("close", () => {
u?.destroy();
ur?.destroy();
});
req.on("data", (c) => chunks.push(c));
req.on("end", () => {
const body = Buffer.concat(chunks);
log(`--> ${req.method} ${req.url} body=${body.length}B`);
u = https.request(
{ host: UPSTREAM, path: req.url, method: req.method,
headers: { ...req.headers, host: UPSTREAM } },
(upstreamRes) => {
ur = upstreamRes;
log(`<-- ${ur.statusCode} ${req.url} retry-after=${ur.headers["retry-after"] ?? "-"} +${Date.now() - t0}ms`);
res.writeHead(ur.statusCode, ur.headers);
let sse = "";
ur.on("data", (c) => {
res.write(c);
sse += c.toString("utf8");
let i;
while ((i = sse.indexOf("\n")) >= 0) {
const line = sse.slice(0, i).trimEnd();
sse = sse.slice(i + 1);
if (line) log(` +${String(Date.now() - t0).padStart(6)}ms ${line.slice(0, 160)}`);
}
});
ur.on("end", () => { res.end(); log(` EOF ${req.url} +${Date.now() - t0}ms`); });
ur.on("error", (e) => { log(` ERR(up-res) ${req.url} ${e.message}`); res.destroy(e); });
},
);
u.on("error", (e) => {
log(` ERR ${req.url} ${e.message}`);
if (!res.headersSent) { res.writeHead(502).end(); } else { res.destroy(e); }
});
u.write(body);
u.end();
});
}).listen(PORT, "127.0.0.1", () => log(`listening on http://127.0.0.1:${PORT}`));
package main
import (
"bufio"
"flag"
"fmt"
"os"
"regexp"
"sort"
"strings"
)
var (
verticalFlag = flag.Bool("vertical", false, "use vertical layout (top to bottom)")
versionFlag = flag.Bool("version", false, "show version")
)
type Graph struct {
edges map[string][]string
indegree map[string]int
nodes map[string]bool
}
func NewGraph() *Graph {
return &Graph{
edges: make(map[string][]string),
indegree: make(map[string]int),
nodes: make(map[string]bool),
}
}
func (g *Graph) AddEdge(from, to string) {
if !g.nodes[from] {
g.nodes[from] = true
g.indegree[from] = 0
}
if !g.nodes[to] {
g.nodes[to] = true
g.indegree[to] = 0
}
// Check if edge already exists to avoid duplicates
for _, existing := range g.edges[from] {
if existing == to {
return
}
}
g.edges[from] = append(g.edges[from], to)
g.indegree[to]++
}
func parseEdges(input string) ([][]string, []string, error) {
var edges [][]string
var singleNodes []string
lines := strings.Split(input, "\n")
edgePattern := regexp.MustCompile(`^\s*([^#\s][^->]*?)\s*->\s*([^#\s].*?)\s*$`)
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
// Remove inline comments (everything after first #)
commentPos := strings.Index(line, "#")
if commentPos >= 0 {
line = strings.TrimSpace(line[:commentPos])
if line == "" {
continue
}
}
matches := edgePattern.FindStringSubmatch(line)
if len(matches) == 3 {
from := strings.TrimSpace(matches[1])
to := strings.TrimSpace(matches[2])
edges = append(edges, []string{from, to})
} else {
// Single node without -> (e.g., "G")
singleNodes = append(singleNodes, line)
}
}
return edges, singleNodes, nil
}
func readInput(filename string) (string, error) {
if filename == "-" || filename == "" {
scanner := bufio.NewScanner(os.Stdin)
var content strings.Builder
for scanner.Scan() {
content.WriteString(scanner.Text())
content.WriteString("\n")
}
return content.String(), scanner.Err()
}
data, err := os.ReadFile(filename)
if err != nil {
return "", err
}
return string(data), nil
}
func topologicalSort(g *Graph) ([][]string, bool) {
// Copy indegree map for processing
indeg := make(map[string]int)
for k, v := range g.indegree {
indeg[k] = v
}
// Initialize queue with nodes having indegree 0
queue := []string{}
for node := range indeg {
if indeg[node] == 0 {
queue = append(queue, node)
}
}
// Kahn's algorithm with layer tracking
layers := [][]string{}
// Sort initial queue for deterministic output
sort.Strings(queue)
for len(queue) > 0 {
// Current layer consists of all nodes in the queue
currentLayer := []string{}
nextQueue := []string{}
// Process all nodes in current layer
for _, node := range queue {
currentLayer = append(currentLayer, node)
// For each neighbor, reduce indegree
for _, neighbor := range g.edges[node] {
indeg[neighbor]--
if indeg[neighbor] == 0 {
nextQueue = append(nextQueue, neighbor)
}
}
}
layers = append(layers, currentLayer)
queue = nextQueue
sort.Strings(queue)
}
// Check for cycles - count processed nodes
processed := 0
for _, layer := range layers {
processed += len(layer)
}
hasCycle := processed < len(g.nodes)
// If cycle exists, add remaining nodes to last layer
if hasCycle {
remaining := []string{}
for node := range g.nodes {
found := false
for _, layer := range layers {
for _, n := range layer {
if n == node {
found = true
break
}
}
if found {
break
}
}
if !found {
remaining = append(remaining, node)
}
}
if remaining != nil && len(remaining) > 0 {
sort.Strings(remaining)
layers = append(layers, remaining)
}
}
return layers, hasCycle
}
func renderGraph(g *Graph, layers [][]string, hasCycle bool) {
// For now, simple rendering
if hasCycle {
fmt.Printf("cycle detected\n")
}
// Simple horizontal layout
if !*verticalFlag {
// Print each layer horizontally
for i, layer := range layers {
if i > 0 {
fmt.Print(" ")
}
for j, node := range layer {
if j > 0 {
fmt.Print(" ")
}
fmt.Printf("[%s]", node)
}
fmt.Println()
// Print connections if not last layer
if i < len(layers)-1 {
for j := range layer {
fmt.Print(" ")
if j == 0 {
fmt.Print("──▶")
} else {
fmt.Print(" ")
}
}
fmt.Println()
}
}
} else {
// Vertical layout
for i, layer := range layers {
for _, node := range layer {
fmt.Printf("[%s]\n", node)
}
if i < len(layers)-1 {
fmt.Println(" ↓")
}
}
}
}
func main() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
fmt.Fprintf(flag.CommandLine.Output(), " gratui [file]\n")
fmt.Fprintf(flag.CommandLine.Output(), "Options:\n")
flag.PrintDefaults()
}
flag.Parse()
if *versionFlag {
fmt.Println("gratui v0.1.0")
return
}
var filename string
if len(flag.Args()) > 0 {
filename = flag.Args()[0]
} else {
filename = "-"
}
content, err := readInput(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading input: %v\n", err)
os.Exit(1)
}
edges, singleNodes, err := parseEdges(content)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing edges: %v\n", err)
os.Exit(1)
}
// Build graph
graph := NewGraph()
for _, edge := range edges {
graph.AddEdge(edge[0], edge[1])
}
// Add single nodes
for _, node := range singleNodes {
if !graph.nodes[node] {
graph.nodes[node] = true
graph.indegree[node] = 0
}
}
// Topological sort with cycle detection
layers, hasCycle := topologicalSort(graph)
// Render graph
renderGraph(graph, layers, hasCycle)
}