Discovery now applies the same per-tool policy decision as invocation: a connector lookup needs read, an action needs run, and each builtin carries the action and resource kind it checks. The model's tool schemas and the system prompt's summaries come from that one filtered set. A name that two declarations, or a declaration and a builtin, both claim is withheld from all of them. The stream client fixes the wire tools for the whole turn, so a guarded provider re-reads the owner's access and the running declarations before every model request. If an offered tool is no longer permitted or declared, the turn stops before the request instead of describing a stale set. A call to a declaration the registry no longer holds is refused before anything runs, naming nothing. Memory notes are left out of the prompt when the person may not read settings, so the tool filter is not bypassed by prompt context.
62 lines
1.9 KiB
Go
62 lines
1.9 KiB
Go
package chat
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/samcharles93/ai-sdk/chat"
|
|
"github.com/samcharles93/ai-sdk/core"
|
|
|
|
"github.com/samcharles93/archie-core/modules/chat/internal/store"
|
|
)
|
|
|
|
// errToolsChanged ends a turn whose offered tools a later model request may no
|
|
// longer show: access was withdrawn, the policy chain went away, or a
|
|
// connector stopped. The operator can send the message again with what remains.
|
|
var errToolsChanged = errors.New("this conversation's tools changed or are unavailable; send the message again")
|
|
|
|
// guardedProvider re-checks the turn's offered tools before every model
|
|
// request. The stream client builds the wire tool schemas once, so a tool that
|
|
// is no longer permitted must stop the turn rather than be omitted silently: a
|
|
// request that carried it would still describe it to the model.
|
|
type guardedProvider struct {
|
|
chat.Provider
|
|
m *Module
|
|
d Deps
|
|
t store.Turn
|
|
offered core.ToolSet
|
|
}
|
|
|
|
func (p guardedProvider) ChatStream(ctx context.Context, req chat.Request) (chat.Stream, error) {
|
|
ctx, err := p.refresh(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return p.Provider.ChatStream(ctx, req)
|
|
}
|
|
|
|
func (p guardedProvider) Chat(ctx context.Context, req chat.Request) (chat.Response, error) {
|
|
ctx, err := p.refresh(ctx)
|
|
if err != nil {
|
|
return chat.Response{}, err
|
|
}
|
|
return p.Provider.Chat(ctx, req)
|
|
}
|
|
|
|
// refresh acts as the conversation's owner now and confirms every offered tool
|
|
// is still permitted and declared. A missing tool ends the turn before the
|
|
// request is made, so the model never sees a stale schema or summary grow.
|
|
func (p guardedProvider) refresh(ctx context.Context) (context.Context, error) {
|
|
ctx, err := p.m.actingAs(ctx, p.d, p.t.Owner)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
current := p.m.tools(ctx, p.d, p.t)
|
|
for name := range p.offered {
|
|
if _, ok := current[name]; !ok {
|
|
return nil, errToolsChanged
|
|
}
|
|
}
|
|
return ctx, nil
|
|
}
|