zot/packages/provider/refreshing.go
patriceckhart fa7d8d8be5 refactor: split source into packages/{provider,core,tui,agent}
Single Go module, four top-level packages under packages/. Import
paths become github.com/patriceckhart/zot/packages/<name>; downstream
consumers can depend on individual packages without pulling the rest.

Layout:
  packages/provider/     LLM clients + catalog
  packages/provider/auth/ credential store + OAuth + login server
  packages/core/         agent loop, sessions, cost
  packages/tui/          terminal toolkit + chat view
  packages/agent/        CLI wiring, system prompt
    extensions/ extproto/ modes/ tools/ skills/ swarm/
    sdk/  (was pkg/zotcore, package renamed zotcore -> sdk)
    ext/  (was pkg/zotext, package renamed zotext -> ext)

internal/ and pkg/ removed. The internal/assets logo moved into
packages/provider/auth/assets.

Public Go SDK identifiers renamed:
  pkg/zotcore (package zotcore) -> packages/agent/sdk (package sdk)
  pkg/zotext  (package zotext)  -> packages/agent/ext (package ext)

This breaks Go-based extensions and embedders; the JSON wire protocol
for extensions and RPC is unchanged, so non-Go extensions, already-
built extension binaries, and zot rpc consumers are unaffected.

Docs, examples, and the built-in write-zot-extension skill updated
for the new paths and identifiers. Shadow-bug fixes in code samples
(ext := ext.New -> e := ext.New).
2026-05-27 09:07:15 +02:00

57 lines
1.8 KiB
Go

package provider
import (
"context"
"sync"
)
// TokenRefresher is a callback that checks whether the current token
// is still valid and returns a fresh one if needed. The returned
// string is the new access token; if empty the old one is still fine.
// An error means refresh failed (network down, refresh token expired,
// etc.) — the caller should proceed with the stale token and let the
// API return 401 naturally.
type TokenRefresher func(ctx context.Context) (newToken string, err error)
// RefreshingClient wraps a Client and calls a TokenRefresher before
// every Stream call. When the refresher returns a new token, a fresh
// underlying client is built via the factory function.
type RefreshingClient struct {
mu sync.Mutex
inner Client
refresh TokenRefresher
factory func(token string) Client
}
// NewRefreshingClient wraps inner with automatic token refresh.
// refreshFn is called before each Stream; if it returns a non-empty
// token the factory rebuilds the underlying client with the new token.
func NewRefreshingClient(inner Client, refreshFn TokenRefresher, factory func(token string) Client) *RefreshingClient {
return &RefreshingClient{
inner: inner,
refresh: refreshFn,
factory: factory,
}
}
func (c *RefreshingClient) Name() string {
c.mu.Lock()
defer c.mu.Unlock()
return c.inner.Name()
}
func (c *RefreshingClient) Stream(ctx context.Context, req Request) (<-chan Event, error) {
if c.refresh != nil {
if newToken, err := c.refresh(ctx); err == nil && newToken != "" {
c.mu.Lock()
c.inner = c.factory(newToken)
c.mu.Unlock()
}
// On refresh error: proceed with the current client.
// The stale token will 401 and the user sees a clear error.
}
c.mu.Lock()
inner := c.inner
c.mu.Unlock()
return inner.Stream(ctx, req)
}