Documentation
¶
Overview ¶
Package wire encodes and decodes NotebookLM's internal web RPC formats: batchexecute requests, chunked responses and the streamed chat envelope.
Index ¶
- func AnswerBlockWanted(ctx context.Context) bool
- func At(v jsontext.Value, path ...int) (jsontext.Value, bool)
- func Decode(body []byte) ([]Frame, Trailers, error)
- func Elems(v jsontext.Value, path ...int) []jsontext.Value
- func EncodeBatch(calls []BatchItem, at string) (string, error)
- func EncodeStream(params any, at string) (string, error)
- func Int(v jsontext.Value, path ...int) (int64, bool)
- func Pick(frames []Frame, id string, policy NullPolicy) (jsontext.Value, error)
- func Segments(body []byte) ([][]byte, error)
- func SliceUTF16(s string, start, end int) string
- func Str(v jsontext.Value, path ...int) string
- func WithAnswerBlock(ctx context.Context) context.Context
- type BatchItem
- type Citation
- type Error
- type Frame
- type Node
- type NullPolicy
- type Snapshot
- type Status
- type StreamDecoder
- type Trailers
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AnswerBlockWanted ¶ added in v0.2.0
AnswerBlockWanted reports whether ctx asks for the answer block.
func At ¶
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 EncodeBatch ¶
EncodeBatch builds the form body f.req=...&at=...& for batchexecute.
func EncodeStream ¶
EncodeStream builds the form body for GenerateFreeFormStreamed: f.req=[null,"<params JSON>"]&at=...&.
func Pick ¶
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 ¶
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 ¶
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).
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.
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
Get returns element i of a slice from Elems or Head, or a null Node when i is out of range.
func (Node) At ¶
At walks positional indexes. A missing index, null or non-array step yields a null Node.
func (Node) Elems ¶
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
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.
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 ¶
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 ¶
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.