Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: include agent name on system actions #37

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions pkg/controller/handlers/knowledge/knowledge.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package knowledge
import (
"github.com/acorn-io/baaah/pkg/router"
"github.com/gptscript-ai/otto/pkg/knowledge"
v1 "github.com/gptscript-ai/otto/pkg/storage/apis/otto.gptscript.ai/v1"
wclient "github.com/thedadams/workspace-provider/pkg/client"
)

Expand Down Expand Up @@ -48,7 +49,15 @@ func (a *Handler) RemoveWorkspace(req router.Request, resp router.Response) erro
status := knowledged.GetKnowledgeWorkspaceStatus()

if status.HasKnowledge {
if err := a.ingester.DeleteKnowledge(req.Ctx, knowledged.GetNamespace(), status.KnowledgeWorkspaceID); err != nil {
var agentName string
switch obj := knowledged.(type) {
case *v1.Agent:
agentName = obj.Name
case *v1.Thread:
agentName = obj.Spec.AgentName
}

if err := a.ingester.DeleteKnowledge(req.Ctx, agentName, knowledged.GetNamespace(), status.KnowledgeWorkspaceID); err != nil {
return err
}
}
Expand All @@ -62,13 +71,21 @@ func (a *Handler) RemoveWorkspace(req router.Request, resp router.Response) erro

// TODO(thedadams): add another handler that pulls the status logs off the run and stores them.
func (a *Handler) IngestKnowledge(req router.Request, resp router.Response) error {
knowleged := req.Object.(knowledge.Knowledgeable)
status := knowleged.GetKnowledgeWorkspaceStatus()
knowledged := req.Object.(knowledge.Knowledgeable)
status := knowledged.GetKnowledgeWorkspaceStatus()
if status.KnowledgeGeneration == status.ObservedKnowledgeGeneration || !status.HasKnowledge {
return nil
}

if err := a.ingester.IngestKnowledge(req.Ctx, knowleged.GetNamespace(), status.KnowledgeWorkspaceID); err != nil {
var agentName string
switch obj := knowledged.(type) {
case *v1.Agent:
agentName = obj.Name
case *v1.Thread:
agentName = obj.Spec.AgentName
}

if err := a.ingester.IngestKnowledge(req.Ctx, agentName, knowledged.GetNamespace(), status.KnowledgeWorkspaceID); err != nil {
return err
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/invoke/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
v1 "github.com/gptscript-ai/otto/pkg/storage/apis/otto.gptscript.ai/v1"
)

func (i *Invoker) SystemAction(ctx context.Context, generateName, namespace, tool, input string, env ...string) (*Response, error) {
func (i *Invoker) SystemAction(ctx context.Context, generateName, agentName, namespace, tool, input string, env ...string) (*Response, error) {
thread, err := i.NewThread(ctx, i.uncached, namespace, NewThreadOptions{
AgentName: agentName,
ThreadGenerateName: generateName,
})
if err != nil {
Expand All @@ -19,6 +20,7 @@ func (i *Invoker) SystemAction(ctx context.Context, generateName, namespace, too

func (i *Invoker) SystemActionWithThread(ctx context.Context, thread *v1.Thread, tool, input string, env ...string) (*Response, error) {
return i.createRunFromRemoteTool(ctx, i.uncached, thread, tool, input, runOptions{
Env: env,
AgentName: thread.Spec.AgentName,
Env: env,
})
}
24 changes: 20 additions & 4 deletions pkg/knowledge/knowledge.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,22 @@ func NewIngester(invoker *invoke.Invoker, knowledgeTool string) *Ingester {
}
}

func (i *Ingester) IngestKnowledge(ctx context.Context, namespace, knowledgeWorkspaceID string) error {
func (i *Ingester) IngestKnowledge(ctx context.Context, agentName, namespace, knowledgeWorkspaceID string) error {
knowledgeTool, tag, ok := strings.Cut(i.knowledgeTool, "@")
if ok {
tag = "@" + tag
}

run, err := i.invoker.SystemAction(ctx, "ingest-", namespace, knowledgeTool+"/ingest.gpt"+tag, workspace.GetDir(knowledgeWorkspaceID), "GPTSCRIPT_DATASET="+workspace.KnowledgeIDFromWorkspaceID(knowledgeWorkspaceID))
run, err := i.invoker.SystemAction(
ctx,
"ingest-",
agentName,
namespace,
knowledgeTool+"/ingest.gpt"+tag,
workspace.GetDir(knowledgeWorkspaceID),
// These are environment variables passed to the script
"GPTSCRIPT_DATASET="+workspace.KnowledgeIDFromWorkspaceID(knowledgeWorkspaceID),
)
if err != nil {
return err
}
Expand All @@ -46,13 +55,20 @@ func (i *Ingester) IngestKnowledge(ctx context.Context, namespace, knowledgeWork
return nil
}

func (i *Ingester) DeleteKnowledge(ctx context.Context, namespace, knowledgeWorkspaceID string) error {
func (i *Ingester) DeleteKnowledge(ctx context.Context, agentName, namespace, knowledgeWorkspaceID string) error {
knowledgeTool, tag, ok := strings.Cut(i.knowledgeTool, "@")
if ok {
tag = "@" + tag
}

run, err := i.invoker.SystemAction(ctx, "ingest-delete-", namespace, knowledgeTool+"/delete.gpt"+tag, workspace.KnowledgeIDFromWorkspaceID(knowledgeWorkspaceID))
run, err := i.invoker.SystemAction(
ctx,
"ingest-delete-",
agentName,
namespace,
knowledgeTool+"/delete.gpt"+tag,
workspace.KnowledgeIDFromWorkspaceID(knowledgeWorkspaceID),
)
if err != nil {
return err
}
Expand Down