wire

package
v0.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2026 License: Apache-2.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package wire encodes and decodes NotebookLM's internal web RPC formats: batchexecute requests, chunked responses and the streamed chat envelope.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnswerBlockWanted added in v0.2.0

func AnswerBlockWanted(ctx context.Context) bool

AnswerBlockWanted reports whether ctx asks for the answer block.

func At

func At(v jsontext.Value, path ...int) (jsontext.Value, bool)

At walks positional indexes into nested arrays. ok is false when any step is out of range, null or not an array, or the value is null.

func Decode

func Decode(body []byte) ([]Frame, Trailers, error)

Decode parses a batchexecute or stream response body into frames.

func Elems

func Elems(v jsontext.Value, path ...int) []jsontext.Value

Elems returns the elements of the array at path (nil if absent or not an array).

func EncodeBatch

func EncodeBatch(calls []BatchItem, at string) (string, error)

EncodeBatch builds the form body f.req=...&at=...& for batchexecute.

func EncodeStream

func EncodeStream(params any, at string) (string, error)

EncodeStream builds the form body for GenerateFreeFormStreamed: f.req=[null,"<params JSON>"]&at=...&.

func Int

func Int(v jsontext.Value, path ...int) (int64, bool)

Int returns the integer at path.

func Pick

func Pick(frames []Frame, id string, policy NullPolicy) (jsontext.Value, error)

Pick returns the payload for id. The last non-null frame wins; an "er" frame, or a status on a null frame, is an error.

func Segments

func Segments(body []byte) ([][]byte, error)

Segments returns the raw top-level JSON segments of a response body with raw control characters escaped. Tools (fixture sanitizer) use it to rewrite bodies without depending on chunk lengths.

func SliceUTF16

func SliceUTF16(s string, start, end int) string

SliceUTF16 returns the part of s between UTF-16 code-unit offsets start and end, the unit citation offsets use. Offsets are clamped; a rune is kept when its first unit lies in [start, end).

func Str

func Str(v jsontext.Value, path ...int) string

Str returns the string at path, or "" when absent or not a string.

func WithAnswerBlock added in v0.2.0

func WithAnswerBlock(ctx context.Context) context.Context

WithAnswerBlock asks the stream transport to keep the answer block.

Types

type BatchItem

type BatchItem struct {
	ID     string // wire RPC id, e.g. "wXbhsf"
	Params any    // positional payload, JSON-encodable
}

BatchItem is one RPC call inside a batchexecute request.

type Citation

type Citation struct {
	N        int // marker number, 1-based; passage N-1 backs it
	SourceID string
	Text     string // cited passage text
	Start    int
	End      int
}

Citation ties a "[N]" marker to a passage of a source. Start and End are offsets into the answer text in UTF-16 code units (use SliceUTF16).

type Error

type Error struct {
	Code      int    // gRPC or numeric body code; 0 if none
	ID        string // RPC id involved
	Msg       string
	Status    *Status
	Drift     bool // response had frames, but not for this id
	SignedOut bool // no frames at all, or an HTML page
	ServerErr bool // ["er", id, code] frame
	Malformed bool
}

Error is a wire-level failure. The transport layer maps it to error kinds.

func (*Error) Error

func (e *Error) Error() string

type Frame

type Frame struct {
	ID        string
	Payload   jsontext.Value // inner JSON, unquoted; nil when null
	Status    *Status
	Index     string
	ServerErr bool
	ErrCode   int
}

Frame is one RPC result row: ["wrb.fr", id, payload, null, null, status, index] or a server error row ["er", id, code].

type Node

type Node struct {
	// contains filtered or unexported fields
}

Node is a lazy, allocation-free view of a JSON payload. It keeps the raw bytes, validated once by Parse, and walks them structurally, skipping unneeded values without decoding them. At/Str/Int on a raw jsontext.Value decode whole subtrees into fresh slices on every call; for a 100-notebook list that cost ~40k allocations, a Node walk costs none.

func Get added in v0.2.0

func Get(es []Node, i int) Node

Get returns element i of a slice from Elems or Head, or a null Node when i is out of range.

func Parse

func Parse(v jsontext.Value) (Node, error)

Parse validates v once and wraps it.

func (Node) At

func (n Node) At(path ...int) Node

At walks positional indexes. A missing index, null or non-array step yields a null Node.

func (Node) Elems

func (n Node) Elems() []Node

Elems returns the array's elements (nil for non-arrays). A null element is a null Node, as At would return.

func (Node) Head added in v0.2.0

func (n Node) Head(buf []Node) []Node

Head fills buf with the array's first len(buf) elements in one pass and returns the filled part; it stops scanning there. Reading several fields of a row this way costs one scan instead of one per At, and a caller's fixed-size buf stays on the stack.

func (Node) Int

func (n Node) Int() (int64, bool)

Int returns the numeric value truncated to int64.

func (Node) IsNull

func (n Node) IsNull() bool

func (Node) Len

func (n Node) Len() int

Len is the array length (0 for non-arrays).

func (Node) Str

func (n Node) Str() string

Str returns the string value, or "".

type NullPolicy

type NullPolicy int

NullPolicy says how Pick treats a frame whose payload is null without a status.

const (
	NullIsError NullPolicy = iota
	NullAllowed
	NullAllowedUnlessRejected
)

type Snapshot

type Snapshot struct {
	Text     string
	Thinking bool // type flag 2: reasoning chunk, not part of the answer
	// Final is the server's own end-of-answer marker (isFinalResponse,
	// payload position 4). It is set on the snapshot that carries it, which
	// is the last answer snapshot before the trailers, and on a rejection.
	// A stream cut short, or one from a server that omits the flag, never
	// sees Final; LastAnswer then falls back to the last non-thinking
	// snapshot.
	Final     bool
	Citations []Citation // re-derived from this snapshot only; ranges may be rewritten later
	// ConversationID is the id the stream carries. Live captures say this is
	// a per-stream session id; resolve the real conversation through hPTbtc.
	ConversationID string
	ResponseID     string
	Status         *Status // google.rpc.Status of a rejection (null answer + status)
	// AnswerBlock is a copy of the answer row's raw type block
	// [rich, null, null, passages, flag]. Only the Final answer snapshot
	// carries it (one copy per stream); saving the answer as a note
	// (CYK0Xb) is built from it.
	AnswerBlock []byte
}

Snapshot is one cumulative state of a streamed chat answer. Every snapshot holds the whole text so far, so the last one wins; there is no diffing.

func LastAnswer

func LastAnswer(snaps []Snapshot) Snapshot

LastAnswer returns the answer to use from a collected stream: the last snapshot with Final set, else the last non-thinking snapshot without a rejection status, else the zero Snapshot. Because snapshots are cumulative, that one snapshot is the whole answer.

type Status

type Status struct {
	Code    int
	Message string
	Details []jsontext.Value
}

Status is a google.rpc.Status found at position 5 of a wrb.fr frame. Code is a gRPC code: 3 INVALID_ARGUMENT, 5 NOT_FOUND, 7 PERMISSION_DENIED, 8 RESOURCE_EXHAUSTED, 9 FAILED_PRECONDITION, 16 UNAUTHENTICATED, etc.

type StreamDecoder

type StreamDecoder struct {
	// contains filtered or unexported fields
}

StreamDecoder parses a GenerateFreeFormStreamed body incrementally. It holds one top-level segment at a time (about one snapshot), never the whole body.

func NewStreamDecoder

func NewStreamDecoder(r io.Reader) *StreamDecoder

NewStreamDecoder reads the stream from r.

func (*StreamDecoder) KeepAnswerBlock added in v0.2.0

func (d *StreamDecoder) KeepAnswerBlock()

KeepAnswerBlock makes the decoder copy the final answer's type block into Snapshot.AnswerBlock. Off by default: only saving an answer as a note needs it, and the copy is the size of the rich answer.

func (*StreamDecoder) Next

func (d *StreamDecoder) Next() (Snapshot, error)

Next returns the next snapshot. It returns io.EOF after the last one. A *Error ends the stream (truncated body, server error frame, sign-in page); snapshots returned before it stay valid. Heartbeat chunks are skipped.

func (*StreamDecoder) Trailers

func (d *StreamDecoder) Trailers() Trailers

Trailers returns metadata seen so far (rotated f.sid). Complete after io.EOF.

type Trailers

type Trailers struct {
	FSID string // rotated f.sid from ["af.httprm", _, "<sid>", _]
}

Trailers carries metadata rows that change client state.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL