textv1

import textv1 "pocketone.dev/ext/contracts/llm/text/v1"

Package textv1 is the llm.text/v1 contract: a language model answers a prompt or a conversation. Each call names its model as provider/model ("vultr/glm-5.2"), a provider being one the app's admins added in Settings → LLM.

A wire contract at /api/_c/llm.text/v1. In pb_hooks it's $llm.text.

Source: ext/contracts/llm/text/v1.

Variables

var Contract = extkit.NewContract[Text]("llm.text/v1", extkit.One).JS(extkit.JSHooks, nil).JSTypes(jsTypes)

Contract is llm.text/v1.

var W = extkit.NewWire(Contract, "llm", func(inner Text, g *extkit.Guard[Text]) Text {
	return &guardText{inner, g}
},
	extkit.Op[Text]{
		Name:   "complete",
		Desc:   "Answers a prompt or a conversation.",
		Method: "POST",
		Path:   "complete",
		In:     reflect.TypeOf(CompleteInput{}),
		Out:    extkit.OutJSON,
		Invoke: func(ctx context.Context, impl Text, a extkit.Auth, req extkit.OpRequest) (any, error) {
			var in CompleteInput
			if err := extkit.DecodeInput(req.JSON, &in); err != nil {
				return nil, err
			}
			return impl.Complete(ctx, a, in)
		},
	},
	extkit.Op[Text]{
		Name:   "stream",
		Desc:   "Answers like Complete, sending the answer as it's written: a chunk with text (or reasoning) for each piece, then one with done and the whole result.",
		Method: "POST",
		Path:   "stream",
		In:     reflect.TypeOf(CompleteInput{}),
		Out:    extkit.OutStream,
		Invoke: func(ctx context.Context, impl Text, a extkit.Auth, req extkit.OpRequest) (any, error) {
			var in CompleteInput
			if err := extkit.DecodeInput(req.JSON, &in); err != nil {
				return nil, err
			}
			return impl.Stream(ctx, a, in)
		},
	},
	extkit.Op[Text]{
		Name:   "count",
		Desc:   "Says about how many tokens a text or a conversation is, for budgeting what to send.",
		Method: "POST",
		Path:   "count",
		Scope:  "llm:read",
		In:     reflect.TypeOf(CountInput{}),
		Out:    extkit.OutJSON,
		Invoke: func(ctx context.Context, impl Text, a extkit.Auth, req extkit.OpRequest) (any, error) {
			var in CountInput
			if err := extkit.DecodeInput(req.JSON, &in); err != nil {
				return nil, err
			}
			return impl.Count(ctx, a, in)
		},
	},
	extkit.Op[Text]{
		Name:   "models",
		Desc:   "Lists the text models of every provider in Settings → LLM.",
		Method: "GET",
		Path:   "models",
		Scope:  "llm:read",
		In:     reflect.TypeOf(llm.ModelsInput{}),
		Out:    extkit.OutJSON,
		Invoke: func(ctx context.Context, impl Text, a extkit.Auth, req extkit.OpRequest) (any, error) {
			var in llm.ModelsInput
			if err := extkit.DecodeQuery(req.Query, &in); err != nil {
				return nil, err
			}
			return impl.Models(ctx, a, in)
		},
	},
)

W is llm.text/v1's wire side: routes at /api/_c/llm.text/v1/<op>, action hooks, MCP tools and the client.

Functions

OnComplete

func OnComplete(owner any, fn func(e *extkit.CallEvent, in *CompleteInput) error)

OnComplete binds fn to complete calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse. in is the call's input; changes reach the provider.

OnCount

func OnCount(owner any, fn func(e *extkit.CallEvent, in *CountInput) error)

OnCount binds fn to count calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse. in is the call's input; changes reach the provider.

OnModels

func OnModels(owner any, fn func(e *extkit.CallEvent, in *llm.ModelsInput) error)

OnModels binds fn to models calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse. in is the call's input; changes reach the provider.

OnStream

func OnStream(owner any, fn func(e *extkit.CallEvent, in *CompleteInput) error)

OnStream binds fn to stream calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse. in is the call's input; changes reach the provider.

Types

Chunk

type Chunk struct {
	Text      string          `json:"text,omitempty"`
	Reasoning string          `json:"reasoning,omitempty"`
	Done      bool            `json:"done,omitempty"`
	Result    *CompleteResult `json:"result,omitempty"`
}

Chunk is one entry of a streamed answer: a piece of text (or of reasoning) as it's written, or, last, done with the whole result.

Client

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

Client is llm.text/v1 over HTTP, for the CLI and other apps. It implements Text; the token it was made with is the credential, so the extkit.Auth argument is ignored.

NewClient

func NewClient(baseURL, token string, hc *http.Client) *Client

NewClient calls llm.text/v1 on the app at baseURL with token (a PocketBase auth token or an API token).

Client.Complete

func (c *Client) Complete(ctx context.Context, _ extkit.Auth, in CompleteInput) (CompleteResult, error)

Client.Count

func (c *Client) Count(ctx context.Context, _ extkit.Auth, in CountInput) (CountResult, error)

Client.Models

func (c *Client) Models(ctx context.Context, _ extkit.Auth, in llm.ModelsInput) (llm.Models, error)

Client.Stream

func (c *Client) Stream(ctx context.Context, _ extkit.Auth, in CompleteInput) (extkit.Stream, error)

CompleteInput

type CompleteInput struct {
	// Model is "provider/model" (required).
	Model    string    `json:"model"`
	System   string    `json:"system,omitempty"`
	Prompt   string    `json:"prompt,omitempty"`
	Messages []Message `json:"messages,omitempty"`
	// Schema, when set, is a JSON schema the answer must match; the result's Data is then the parsed answer.
	Schema json.RawMessage `json:"schema,omitempty"`
	// MaxTokens caps the answer's length, thinking included (0: the provider's default).
	MaxTokens   int      `json:"maxTokens,omitempty"`
	Temperature *float64 `json:"temperature,omitempty"` // nil: the provider's default
	Stop        []string `json:"stop,omitempty"`
	// Reasoning is how much a reasoning model thinks first: "" (its default), "off", "low" or "high". A model that
	// doesn't reason ignores it.
	Reasoning string      `json:"reasoning,omitempty"`
	Options   llm.Options `json:"options,omitempty"`
	// Enrich adds the best passages from the app's search buckets after the prompt (search/v1).
	Enrich *llm.Enrich `json:"enrich,omitempty"`
}

CompleteInput is a prompt: Prompt, or Messages for a conversation (Prompt is then added as the last user message).

CompleteResult

type CompleteResult struct {
	Text string          `json:"text"`
	Data json.RawMessage `json:"data,omitempty"` // the parsed answer, when a Schema was given
	// Reasoning is the model's thinking, when the provider returns it separately.
	Reasoning  string    `json:"reasoning,omitempty"`
	StopReason string    `json:"stopReason,omitempty"` // "stop", "length" or "cancelled"
	Model      string    `json:"model"`                // the provider/model that answered
	Usage      llm.Usage `json:"usage"`
	// Passages are what Enrich added to the prompt, best first (cite them, or show where the answer came from).
	Passages []llm.Passage `json:"passages,omitempty"`
}

CompleteResult is the model's answer.

CountInput

type CountInput struct {
	Model    string    `json:"model,omitempty"` // optional: the count is an estimate either way
	Text     string    `json:"text,omitempty"`
	System   string    `json:"system,omitempty"`
	Messages []Message `json:"messages,omitempty"`
}

CountInput is a text, or a conversation as a call would send it.

CountResult

type CountResult struct {
	Tokens int  `json:"tokens"`
	Exact  bool `json:"exact"`
}

CountResult is a token count. Exact is false when it's an estimate (about four characters a token).

Message

type Message struct {
	Role    string `json:"role"` // system, user or assistant
	Content string `json:"content"`
}

Message is one turn of a conversation.

Text

type Text interface {
	// Complete answers a prompt or a conversation.
	//
	//op: POST complete
	Complete(ctx context.Context, a extkit.Auth, in CompleteInput) (CompleteResult, error)
	// Stream answers like Complete, sending the answer as it's written: a chunk with text (or reasoning) for each
	// piece, then one with done and the whole result.
	//
	//op: POST stream
	Stream(ctx context.Context, a extkit.Auth, in CompleteInput) (extkit.Stream, error)
	// Count says about how many tokens a text or a conversation is, for budgeting what to send.
	//
	//op: POST count scope=llm:read
	Count(ctx context.Context, a extkit.Auth, in CountInput) (CountResult, error)
	// Models lists the text models of every provider in Settings → LLM.
	//
	//op: GET models scope=llm:read
	Models(ctx context.Context, a extkit.Auth, in llm.ModelsInput) (llm.Models, error)
}

Text answers prompts with a language model.