-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathsession_response.go
More file actions
138 lines (134 loc) · 5.94 KB
/
Copy pathsession_response.go
File metadata and controls
138 lines (134 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package api
import (
"encoding/json"
"errors"
v1 "github.com/MiniMax-AI/OpenAgentCore/contracts/agents-api/v1"
"github.com/MiniMax-AI/OpenAgentCore/services/core/internal/sessions"
)
func sessionResponse(session sessions.Session, executorURL string) (v1.Session, error) {
var cfg configuration
if err := json.Unmarshal(session.Configuration, &cfg); err != nil || cfg.Agent.ID == "" || cfg.Agent.Model == "" {
return v1.Session{}, errors.New("unsupported stored session configuration")
}
if cfg.Agent.XAgentsCore != nil && session.Engine != "" {
cfg.Agent.XAgentsCore = &v1.AgentsCore{Harness: session.Engine, HarnessConfig: cfg.Agent.XAgentsCore.HarnessConfig}
}
// The pinned Session AgentTool resource union excludes the tool_search
// input declaration. Retain it in saved Agents and frozen execution input.
tools := make([]json.RawMessage, 0, len(cfg.Agent.Tools))
for _, raw := range cfg.Agent.Tools {
var tool struct {
Type string `json:"type"`
}
if json.Unmarshal(raw, &tool) != nil {
return v1.Session{}, errors.New("unsupported stored tool configuration")
}
if tool.Type != "tool_search" {
tools = append(tools, projectedMCPCredential(raw, cfg))
}
}
cfg.Agent.Tools = tools
environment, err := sessionEnvironment(session, cfg.Environment.Type, executorURL)
if err != nil {
return v1.Session{}, err
}
response := v1.Session{
ID: session.ID, Agent: cfg.Agent, Environment: environment, Usage: tokenUsage(session.Usage),
CreatedAt: session.CreatedAt.Unix(), LastActiveAt: session.CreatedAt.Unix(),
Metadata: session.Metadata, Object: "agent.session", Status: "idle",
RequiredActions: []v1.RequiredAction{}, VaultIDs: append([]string{}, cfg.VaultIDs...),
}
if turn := session.LastTurn; turn != nil {
active := turn.CreatedAt
if turn.StartedAt.After(active) {
active = turn.StartedAt
}
if turn.CompletedAt.After(active) {
active = turn.CompletedAt
}
response.LastActiveAt = active.Unix()
switch turn.Status {
case sessions.TurnQueued, sessions.TurnInProgress, sessions.TurnWaiting:
response.Status = "in_progress"
if turn.CancelRequestedAt.IsZero() && len(session.RequiredActions) > 0 {
response.Status = "requires_action"
for _, action := range session.RequiredActions {
response.RequiredActions = append(response.RequiredActions, v1.RequiredAction{
Type: action.Type, Arguments: action.Arguments, CallID: action.CallID, Name: action.Name, TurnID: action.TurnID,
})
}
}
case sessions.TurnFailed:
response.Status = "failed"
message := "The execution could not complete."
response.Error = &message
}
}
if activity := session.EnvironmentInputActivity; activity != nil {
if activity.Status != "idle" && activity.Status != "requires_action" && activity.Status != "failed" {
return v1.Session{}, errors.New("unsupported stored environment input activity")
}
response.Status, response.Error = activity.Status, nil
if activity.Status == "failed" {
message := "The initial input timed out waiting for the environment connection."
switch activity.Failure {
case "environment_unavailable":
message = "The environment is no longer available for this input."
case "runtime_preparation_failed":
message = "Runtime preparation failed before execution. Check the daemon logs and installed capabilities, then submit new input."
case "model_provider_required":
message = "This Session was created without a model provider and cannot run. Create a new Session with x_agents_core.model_provider or an Agent that has one saved."
}
response.Error = &message
}
response.LastActiveAt = activity.LastActiveAt.Unix()
response.RequiredActions = []v1.RequiredAction{}
if activity.Status == "requires_action" {
if activity.EnvironmentID == "" || activity.EnvironmentID != environment.ID {
return v1.Session{}, errors.New("invalid stored environment connection action")
}
response.RequiredActions = append(response.RequiredActions, v1.RequiredAction{Type: "environment_connection", EnvironmentID: activity.EnvironmentID})
}
}
// A hosted provisioning failure is terminal and supersedes the settled input
// activity: the Session reports its safe reason and failure time.
if failure := session.EnvironmentFailure; failure != nil {
if cfg.Environment.Type != "openai_hosted" && cfg.Environment.Type != "self_hosted" {
return v1.Session{}, errors.New("unsupported stored environment failure")
}
reason := failure.Reason
response.Status, response.Error, response.LastActiveAt = "failed", &reason, failure.FailedAt.Unix()
response.RequiredActions = []v1.RequiredAction{}
}
return response, nil
}
func sessionEnvironment(session sessions.Session, kind, executorURL string) (v1.SessionEnvironment, error) {
if kind == "none" {
return v1.SessionEnvironment{Type: "none"}, nil
}
environment := session.Environment
if (kind != "self_hosted" && kind != "openai_hosted") || environment == nil || environment.ID == "" || environment.SessionID != session.ID || environment.TenantID != session.TenantID {
return v1.SessionEnvironment{}, errors.New("unsupported stored session environment")
}
if kind == "openai_hosted" {
return hostedSessionEnvironment(*environment)
}
if executorURL == "" {
return v1.SessionEnvironment{}, errors.New("self-hosted executor origin unavailable")
}
var cfg struct {
Type string `json:"type"`
CapabilityDirectories []string `json:"capability_directories"`
WorkspaceDirectory string `json:"workspace_directory"`
}
if err := json.Unmarshal(environment.Configuration, &cfg); err != nil || cfg.Type != kind || cfg.WorkspaceDirectory == "" {
return v1.SessionEnvironment{}, errors.New("unsupported stored session environment configuration")
}
if cfg.CapabilityDirectories == nil {
cfg.CapabilityDirectories = []string{}
}
return v1.SessionEnvironment{
Type: kind, ID: environment.ID, CapabilityDirectories: &cfg.CapabilityDirectories,
RemoteURL: executorURL, WorkspaceDirectory: cfg.WorkspaceDirectory,
}, nil
}