listcut

package
v1.138.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package listcut cuts a list response to fit a model client's context budget on whole items (#1915): the result stays valid JSON, says how many of the list's items it shows, and, where the operation declares paging parameters, names the request that reads on from the last one shown.

It holds what the api gateway and the GraphQL kind share: finding the one list a JSON value carries, cutting it, the search for the most items that render within a budget, and the continuation pagenext computes for them. Rendering is the caller's, because only the caller knows the envelope the list is wrapped in.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FitItems

func FitItems(total int, try func(n int) bool) (int, bool)

FitItems finds the most items of a list of total that a result renders within budget with. try renders the result holding the first n items and reports whether it fits. The count returned is below total -- a result that fits whole is not cut -- and at least one: a result that cannot hold even one item has no cut that hands the caller anything, and false is returned.

func Hint

func Hint(c Cut, budget int, readTool, exportTool string) string

Hint is the steer on a cut list. The read-only way on is named first, as a call to readTool with next_arguments; exportTool, when not empty, is offered after it as the way to get everything in one call.

func Keep

func Keep(v any, path []string, n int) any

Keep returns v with the list at path cut to its first n items. v is not changed: the objects on the path are copied, so the caller's decoded body stays whole for the next attempt.

Types

type Count

type Count struct {
	// Shown is the number of items kept: the first ones.
	Shown int `json:"shown"`
	// Total is the number of items in the list.
	Total int `json:"total"`
}

Count is how much of a list a cut result shows.

type Cut

type Cut struct {
	// Body is the value holding the kept items and everything around them.
	Body any
	// Shown is the number of items kept: the first ones.
	Shown int
	// Total is the number of items in the list.
	Total int
	// NextQuery is the query of the request that reads on, or nil when the
	// operation declares no paging that can be advanced.
	NextQuery map[string]any
	// Style is how NextQuery continues; empty when NextQuery is nil.
	Style pagenext.Style
}

Cut is one list body cut to fit.

func Fit

func Fit(body any, query map[string]any, plan *pagenext.Plan, fits func(Cut) bool) (Cut, bool)

Fit cuts the list in body to the most items for which fits reports the rendered result within the budget. fits receives each candidate cut and renders it; the last call is made with the cut returned. It reports false when body carries no list or not even one item fits. query is the request the body answered, and plan, when not nil, how that request pages.

type Items

type Items struct {
	// Path is the keys walked from the value to the list; empty when the
	// value is itself the list.
	Path []string
	// Total is the number of items in the list.
	Total int
}

Items locates the one list a JSON value carries.

func FindItems

func FindItems(v any) (Items, bool)

FindItems locates the list v is cut on: v itself when it is an array, or the one array an object carries, found through objects that carry one object and no array (data.users.edges). An object with two arrays, or with no array within reach, has no list the cut could name, and neither has a scalar.

Jump to

Keyboard shortcuts

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