Skip to content
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
16 changes: 15 additions & 1 deletion examples/interrupt_agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,30 @@ import (
"github.com/vearne/agentscope-go/pkg/memory"
"github.com/vearne/agentscope-go/pkg/message"
"github.com/vearne/agentscope-go/pkg/model"
"github.com/vearne/agentscope-go/pkg/studio"
"github.com/vearne/agentscope-go/pkg/tool"
)

func main() {
ctx := context.Background()

if err := studio.Init(
studio.WithURL("http://localhost:3000"),
studio.WithProject("interrupt-studio"),
); err != nil {
log.Printf("Warning: studio init failed (studio may not be running): %v", err)
log.Println("Continuing without studio integration...")
}
defer func() { _ = studio.Shutdown(ctx) }()

apiKey := os.Getenv("OPENAI_API_KEY")
if apiKey == "" {
log.Fatal("Please set OPENAI_API_KEY environment variable")
}

m := model.NewOpenAIChatModel("gpt-4o", apiKey, "", false)
baseURL := os.Getenv("OPENAI_BASE_URL")
modelName := os.Getenv("OPENAI_MODEL")
m := model.NewOpenAIChatModel(modelName, apiKey, baseURL, false)
f := formatter.NewOpenAIChatFormatter()

tk := tool.NewToolkit()
Expand Down
11 changes: 8 additions & 3 deletions pkg/agent/deep.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ func (a *DeepAgent) Interrupt() {
}

func (a *DeepAgent) HandleInterrupt(ctx context.Context, msg *message.Msg) (*message.Msg, error) {
// Use a context that is not cancelled so that Studio forwarding and
// post-reply hooks can complete. The caller's ctx is already cancelled
// because Interrupt() invoked cancelFunc().
studioCtx := context.WithoutCancel(ctx)

resp := &message.Msg{
ID: utils.ShortUUID(),
Name: a.name,
Expand All @@ -219,16 +224,16 @@ func (a *DeepAgent) HandleInterrupt(ctx context.Context, msg *message.Msg) (*mes
Metadata: map[string]interface{}{"_is_interrupted": true},
Timestamp: time.Now().Format("2006-01-02 15:04:05.000"),
}
if err := a.mem.Add(ctx, resp); err != nil {
if err := a.mem.Add(studioCtx, resp); err != nil {
return nil, fmt.Errorf("add interrupted message to memory: %w", err)
}

for _, h := range a.hooks.postReply {
h(ctx, a, msg, resp)
h(studioCtx, a, msg, resp)
}

if sc := studio.GetClient(); sc != nil {
studio.ForwardMessage(ctx, a.name, "assistant", resp)
studio.ForwardMessage(studioCtx, a.name, "assistant", resp)
}
return resp, nil
}
Expand Down
11 changes: 8 additions & 3 deletions pkg/agent/react.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,11 @@ func (a *ReActAgent) Interrupt() {
}

func (a *ReActAgent) HandleInterrupt(ctx context.Context, msg *message.Msg) (*message.Msg, error) {
// Use a context that is not cancelled so that Studio forwarding and
// post-reply hooks can complete. The caller's ctx is already cancelled
// because Interrupt() invoked cancelFunc().
studioCtx := context.WithoutCancel(ctx)

resp := &message.Msg{
ID: utils.ShortUUID(),
Name: a.name,
Expand All @@ -502,16 +507,16 @@ func (a *ReActAgent) HandleInterrupt(ctx context.Context, msg *message.Msg) (*me
Metadata: map[string]interface{}{"_is_interrupted": true},
Timestamp: time.Now().Format("2006-01-02 15:04:05.000"),
}
if err := a.mem.Add(ctx, resp); err != nil {
if err := a.mem.Add(studioCtx, resp); err != nil {
return nil, fmt.Errorf("add interrupted message to memory: %w", err)
}

for _, h := range a.hooks.postReply {
h(ctx, a, msg, resp)
h(studioCtx, a, msg, resp)
}

if sc := studio.GetClient(); sc != nil {
studio.ForwardMessage(ctx, a.name, "assistant", resp)
studio.ForwardMessage(studioCtx, a.name, "assistant", resp)
}
return resp, nil
}
Expand Down
1 change: 1 addition & 0 deletions pkg/model/anthropic.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func NewAnthropicChatModel(modelName, apiKey, baseURL string, stream bool, opts
httpClient: &http.Client{
Timeout: 300 * time.Second,
},
config: &ModelConfig{},
}

for _, opt := range opts {
Expand Down
1 change: 1 addition & 0 deletions pkg/model/gemini.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func NewGeminiChatModel(modelName, apiKey, baseURL string, stream bool, opts ...
httpClient: &http.Client{
Timeout: 300 * time.Second,
},
config: &ModelConfig{},
}

for _, opt := range opts {
Expand Down
1 change: 1 addition & 0 deletions pkg/model/openai.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func NewOpenAIChatModel(modelName, apiKey, baseURL string, stream bool, opts ...
httpClient: &http.Client{
Timeout: 300 * time.Second,
},
config: &ModelConfig{},
}

for _, opt := range opts {
Expand Down
Loading