chatv1

import chatv1 "pocketone.dev/ext/contracts/llm/chat/v1"

Package chatv1 is the llm.chat/v1 contract: stored conversations with the app's language model. A chat is an id the app keeps (on its own records, or handed to its frontend), with an optional title and the app's own meta; the provider stores its messages, sends the recent ones with each question (and a summary of the older ones), and saves both turns. It calls llm.text/v1 for the answers. Chats aren't tied to users: an app that has them puts the user in meta and decides who sees what.

A wire contract at /api/_c/llm.chat/v1. In pb_hooks it's $llm.chat.new, $llm.chat.open(id) and $llm.chat.list.

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

Variables

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

Contract is llm.chat/v1.

var ErrNoChat = &extkit.Error{Code: "not_found", Status: 404, Message: "no chat with that id"}

ErrNoChat: no chat has that id.

var W = extkit.NewWire(Contract, "chat", func(inner Chats, g *extkit.Guard[Chats]) Chats {
	return &guardChats{inner, g}
},
	extkit.Op[Chats]{
		Name:   "new",
		Desc:   "Starts a chat and returns it; its id is how the app finds it again.",
		Method: "POST",
		Path:   "new",
		In:     reflect.TypeOf(NewInput{}),
		Out:    extkit.OutJSON,
		Invoke: func(ctx context.Context, impl Chats, a extkit.Auth, req extkit.OpRequest) (any, error) {
			var in NewInput
			if err := extkit.DecodeInput(req.JSON, &in); err != nil {
				return nil, err
			}
			return impl.New(ctx, a, in)
		},
	},
	extkit.Op[Chats]{
		Name:   "send",
		Desc:   "Asks the chat a question: the provider sends the system text, the chat's summary, its recent messages and the question (with Context after it) to llm.text/v1, saves both turns, and folds older messages into the summary when it's due.",
		Method: "POST",
		Path:   "send",
		In:     reflect.TypeOf(SendInput{}),
		Out:    extkit.OutJSON,
		Invoke: func(ctx context.Context, impl Chats, a extkit.Auth, req extkit.OpRequest) (any, error) {
			var in SendInput
			if err := extkit.DecodeInput(req.JSON, &in); err != nil {
				return nil, err
			}
			return impl.Send(ctx, a, in)
		},
	},
	extkit.Op[Chats]{
		Name:   "send_stream",
		Desc:   "Is Send with the answer as it's written: llm.text/v1 chunks, the last one with done and the result.",
		Method: "POST",
		Path:   "send-stream",
		In:     reflect.TypeOf(SendInput{}),
		Out:    extkit.OutStream,
		Invoke: func(ctx context.Context, impl Chats, a extkit.Auth, req extkit.OpRequest) (any, error) {
			var in SendInput
			if err := extkit.DecodeInput(req.JSON, &in); err != nil {
				return nil, err
			}
			return impl.SendStream(ctx, a, in)
		},
	},
	extkit.Op[Chats]{
		Name:   "get",
		Desc:   "Returns a chat.",
		Method: "POST",
		Path:   "get",
		Scope:  "chat:read",
		In:     reflect.TypeOf(Ref{}),
		Out:    extkit.OutJSON,
		Invoke: func(ctx context.Context, impl Chats, a extkit.Auth, req extkit.OpRequest) (any, error) {
			var in Ref
			if err := extkit.DecodeInput(req.JSON, &in); err != nil {
				return nil, err
			}
			return impl.Get(ctx, a, in)
		},
	},
	extkit.Op[Chats]{
		Name:   "history",
		Desc:   "Returns a chat's messages, oldest first: the last Limit before the message Before (all when 0 and \"\").",
		Method: "POST",
		Path:   "history",
		Scope:  "chat:read",
		In:     reflect.TypeOf(HistoryInput{}),
		Out:    extkit.OutJSON,
		Invoke: func(ctx context.Context, impl Chats, a extkit.Auth, req extkit.OpRequest) (any, error) {
			var in HistoryInput
			if err := extkit.DecodeInput(req.JSON, &in); err != nil {
				return nil, err
			}
			return impl.History(ctx, a, in)
		},
	},
	extkit.Op[Chats]{
		Name:   "update",
		Desc:   "Sets a chat's title or meta (fields left out stay).",
		Method: "POST",
		Path:   "update",
		In:     reflect.TypeOf(UpdateInput{}),
		Out:    extkit.OutJSON,
		Invoke: func(ctx context.Context, impl Chats, a extkit.Auth, req extkit.OpRequest) (any, error) {
			var in UpdateInput
			if err := extkit.DecodeInput(req.JSON, &in); err != nil {
				return nil, err
			}
			return impl.Update(ctx, a, in)
		},
	},
	extkit.Op[Chats]{
		Name:   "list",
		Desc:   "Returns chats, most recently updated first; Meta keeps those whose meta has each of its top-level keys with that value.",
		Method: "POST",
		Path:   "list",
		Scope:  "chat:read",
		In:     reflect.TypeOf(ListInput{}),
		Out:    extkit.OutJSON,
		Invoke: func(ctx context.Context, impl Chats, a extkit.Auth, req extkit.OpRequest) (any, error) {
			var in ListInput
			if err := extkit.DecodeInput(req.JSON, &in); err != nil {
				return nil, err
			}
			return impl.List(ctx, a, in)
		},
	},
	extkit.Op[Chats]{
		Name:   "forget",
		Desc:   "Deletes a chat, its messages and the model's state for it.",
		Method: "POST",
		Path:   "forget",
		In:     reflect.TypeOf(Ref{}),
		Out:    extkit.OutJSON,
		Invoke: func(ctx context.Context, impl Chats, a extkit.Auth, req extkit.OpRequest) (any, error) {
			var in Ref
			if err := extkit.DecodeInput(req.JSON, &in); err != nil {
				return nil, err
			}
			return impl.Forget(ctx, a, in)
		},
	},
	extkit.Op[Chats]{
		Name:   "forget_where",
		Desc:   "Deletes every chat whose meta matches (as in List), e.g. all of a user the app deleted.",
		Method: "POST",
		Path:   "forget-where",
		In:     reflect.TypeOf(ListInput{}),
		Out:    extkit.OutJSON,
		Invoke: func(ctx context.Context, impl Chats, a extkit.Auth, req extkit.OpRequest) (any, error) {
			var in ListInput
			if err := extkit.DecodeInput(req.JSON, &in); err != nil {
				return nil, err
			}
			return impl.ForgetWhere(ctx, a, in)
		},
	},
)

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

Functions

OnForget

func OnForget(owner any, fn func(e *extkit.CallEvent, in *Ref) error)

OnForget binds fn to forget 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.

OnForgetWhere

func OnForgetWhere(owner any, fn func(e *extkit.CallEvent, in *ListInput) error)

OnForgetWhere binds fn to forget_where 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.

OnGet

func OnGet(owner any, fn func(e *extkit.CallEvent, in *Ref) error)

OnGet binds fn to get 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.

OnHistory

func OnHistory(owner any, fn func(e *extkit.CallEvent, in *HistoryInput) error)

OnHistory binds fn to history 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.

OnList

func OnList(owner any, fn func(e *extkit.CallEvent, in *ListInput) error)

OnList binds fn to list 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.

OnNew

func OnNew(owner any, fn func(e *extkit.CallEvent, in *NewInput) error)

OnNew binds fn to new 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.

OnSend

func OnSend(owner any, fn func(e *extkit.CallEvent, in *SendInput) error)

OnSend binds fn to send 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.

OnSendStream

func OnSendStream(owner any, fn func(e *extkit.CallEvent, in *SendInput) error)

OnSendStream binds fn to send_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.

OnUpdate

func OnUpdate(owner any, fn func(e *extkit.CallEvent, in *UpdateInput) error)

OnUpdate binds fn to update 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

Chat

type Chat struct {
	ID       string          `json:"id"`
	Title    string          `json:"title"`
	Summary  string          `json:"summary"`
	Meta     json.RawMessage `json:"meta"`
	Messages int             `json:"messages"`
	Created  time.Time       `json:"created"`
	Updated  time.Time       `json:"updated"`
}

Chat is a stored conversation.

ChatList

type ChatList struct {
	Chats []Chat `json:"chats"`
}

ChatList is a page of chats.

Chats

type Chats interface {
	// New starts a chat and returns it; its id is how the app finds it again.
	//
	//op: POST new
	New(ctx context.Context, a extkit.Auth, in NewInput) (Chat, error)
	// Send asks the chat a question: the provider sends the system text, the chat's summary, its recent messages and
	// the question (with Context after it) to llm.text/v1, saves both turns, and folds older messages into the summary
	// when it's due.
	//
	//op: POST send
	Send(ctx context.Context, a extkit.Auth, in SendInput) (SendResult, error)
	// SendStream is Send with the answer as it's written: llm.text/v1 chunks, the last one with done and the result.
	//
	//op: POST send-stream
	SendStream(ctx context.Context, a extkit.Auth, in SendInput) (extkit.Stream, error)
	// Get returns a chat.
	//
	//op: POST get scope=chat:read
	Get(ctx context.Context, a extkit.Auth, in Ref) (Chat, error)
	// History returns a chat's messages, oldest first: the last Limit before the message Before (all when 0 and "").
	//
	//op: POST history scope=chat:read
	History(ctx context.Context, a extkit.Auth, in HistoryInput) (History, error)
	// Update sets a chat's title or meta (fields left out stay).
	//
	//op: POST update
	Update(ctx context.Context, a extkit.Auth, in UpdateInput) (Chat, error)
	// List returns chats, most recently updated first; Meta keeps those whose meta has each of its top-level keys
	// with that value.
	//
	//op: POST list scope=chat:read
	List(ctx context.Context, a extkit.Auth, in ListInput) (ChatList, error)
	// Forget deletes a chat, its messages and the model's state for it.
	//
	//op: POST forget
	Forget(ctx context.Context, a extkit.Auth, in Ref) (Forgotten, error)
	// ForgetWhere deletes every chat whose meta matches (as in List), e.g. all of a user the app deleted.
	//
	//op: POST forget-where
	ForgetWhere(ctx context.Context, a extkit.Auth, in ListInput) (Forgotten, error)
}

Chats stores an app's conversations with its language model.

Client

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

Client is llm.chat/v1 over HTTP, for the CLI and other apps. It implements Chats; 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.chat/v1 on the app at baseURL with token (a PocketBase auth token or an API token).

Client.Forget

func (c *Client) Forget(ctx context.Context, _ extkit.Auth, in Ref) (Forgotten, error)

Client.ForgetWhere

func (c *Client) ForgetWhere(ctx context.Context, _ extkit.Auth, in ListInput) (Forgotten, error)

Client.Get

func (c *Client) Get(ctx context.Context, _ extkit.Auth, in Ref) (Chat, error)

Client.History

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

Client.List

func (c *Client) List(ctx context.Context, _ extkit.Auth, in ListInput) (ChatList, error)

Client.New

func (c *Client) New(ctx context.Context, _ extkit.Auth, in NewInput) (Chat, error)

Client.Send

func (c *Client) Send(ctx context.Context, _ extkit.Auth, in SendInput) (SendResult, error)

Client.SendStream

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

Client.Update

func (c *Client) Update(ctx context.Context, _ extkit.Auth, in UpdateInput) (Chat, error)

Forgotten

type Forgotten struct {
	Count int `json:"count"`
}

Forgotten says how many chats were deleted.

History

type History struct {
	Messages []Message `json:"messages"`
}

History is a page of messages, oldest first.

HistoryInput

type HistoryInput struct {
	ID     string `json:"id"`
	Limit  int    `json:"limit,omitempty"`
	Before string `json:"before,omitempty"` // a message id
}

HistoryInput picks a page of a chat's messages.

ListInput

type ListInput struct {
	Meta   map[string]any `json:"meta,omitempty"`
	Limit  int            `json:"limit,omitempty"` // default 50
	Offset int            `json:"offset,omitempty"`
}

ListInput filters and pages chats.

Message

type Message struct {
	ID      string    `json:"id"`
	Role    string    `json:"role"` // user or assistant
	Content string    `json:"content"`
	Created time.Time `json:"created"`
}

Message is one saved turn.

NewInput

type NewInput struct {
	Title string          `json:"title,omitempty"`
	Meta  json.RawMessage `json:"meta,omitempty"`
}

NewInput is a new chat's optional title and meta (the app's own JSON object).

Ref

type Ref struct {
	ID string `json:"id"`
}

Ref names a chat.

SendInput

type SendInput struct {
	ID   string `json:"id"`
	Text string `json:"text"`
	// System is the preamble, sent first on every turn (keep it the same for the prompt cache).
	System string `json:"system,omitempty"`
	// Window is how many recent messages to send (default 12); older ones reach the model through the summary.
	Window int `json:"window,omitempty"`
	// SummarizeEvery folds the messages that left the window into the summary once that many have (default 6).
	SummarizeEvery int `json:"summarizeEvery,omitempty"`
	// Context is text sent after the question (the passages that answer it, say). The chat keeps the question as sent, to send it the same way next turn; history shows the question alone.
	Context     string          `json:"context,omitempty"`
	Schema      json.RawMessage `json:"schema,omitempty"`
	MaxTokens   int             `json:"maxTokens,omitempty"`
	Temperature *float64        `json:"temperature,omitempty"`
	// Model is the text model to answer with ("provider/model", required).
	Model string `json:"model"`
	// Reasoning is how much a reasoning model thinks first: "", "off", "low", "medium" or "high".
	Reasoning string `json:"reasoning,omitempty"` // Enrich adds the best passages from the app's search buckets after the question, beside Context (search/v1);
	// the answer's Passages lists them.
	Enrich *llm.Enrich `json:"enrich,omitempty"`
}

SendInput is a question for a chat and how to ask it.

SendResult

type SendResult struct {
	Answer     textv1.CompleteResult `json:"answer"`
	Summarized bool                  `json:"summarized,omitempty"`
}

SendResult is the answer, and whether this turn folded older messages into the summary.

UpdateInput

type UpdateInput struct {
	ID    string          `json:"id"`
	Title *string         `json:"title,omitempty"`
	Meta  json.RawMessage `json:"meta,omitempty"`
}

UpdateInput changes a chat's title or meta.