Pass OpenClaw artifact prepare context
This commit is contained in:
parent
86cd52bf21
commit
9180e9258d
@ -568,9 +568,24 @@ func (o *SessionOrchestrator) openClawArtifactPrepare(
|
||||
sessionKey = strings.TrimSpace(sessionKey)
|
||||
runID = strings.TrimSpace(runID)
|
||||
if sessionKey == "" || runID == "" {
|
||||
log.Printf(
|
||||
"level=warn component=openclaw_gateway event=artifact_prepare_missing_context provider=%q hasOpenClawSessionKey=%t hasRunId=%t appThreadKey=%q",
|
||||
gatewayProvider,
|
||||
sessionKey != "",
|
||||
runID != "",
|
||||
openClawAppThreadKey(params),
|
||||
)
|
||||
return nil, &shared.RPCError{Code: -32602, Message: "openclaw artifact prepare requires openclawSessionKey and runId"}
|
||||
}
|
||||
prepareParams := openClawSessionPrepareParams(params, sessionKey, runID, artifactContract)
|
||||
log.Printf(
|
||||
"level=info component=openclaw_gateway event=artifact_prepare_context provider=%q hasOpenClawSessionKey=%t hasRunId=%t hasWorkspaceDir=%t expectedArtifactDirs=%d",
|
||||
gatewayProvider,
|
||||
strings.TrimSpace(shared.StringArg(prepareParams, "openclawSessionKey", "")) != "",
|
||||
strings.TrimSpace(shared.StringArg(prepareParams, "runId", "")) != "",
|
||||
strings.TrimSpace(shared.StringArg(prepareParams, "workspaceDir", "")) != "",
|
||||
len(shared.ListArg(prepareParams, "expectedArtifactDirs")),
|
||||
)
|
||||
prepareResult := o.openClawGatewayRequestWithRetry(
|
||||
gatewayProvider,
|
||||
"xworkmate.session.prepare",
|
||||
@ -579,17 +594,6 @@ func (o *SessionOrchestrator) openClawArtifactPrepare(
|
||||
notify,
|
||||
)
|
||||
if !prepareResult.OK {
|
||||
if openClawPrepareUnsupported(prepareResult.Error) {
|
||||
prepared := openClawLegacyPreparedArtifactScope(params, sessionKey, runID)
|
||||
log.Printf(
|
||||
"level=warn component=openclaw_gateway event=session_prepare_legacy_fallback provider=%q sessionId=%q runId=%q artifactScope=%q",
|
||||
gatewayProvider,
|
||||
sessionKey,
|
||||
runID,
|
||||
prepared.ArtifactScope,
|
||||
)
|
||||
return prepared, nil
|
||||
}
|
||||
return nil, gatewayRPCError(prepareResult.Error, "openclaw artifact prepare failed")
|
||||
}
|
||||
prepared := openClawPreparedArtifactScopeFromPayload(shared.AsMap(prepareResult.Payload))
|
||||
@ -599,48 +603,6 @@ func (o *SessionOrchestrator) openClawArtifactPrepare(
|
||||
return prepared, nil
|
||||
}
|
||||
|
||||
func openClawPrepareUnsupported(errorPayload map[string]any) bool {
|
||||
code := strings.ToUpper(strings.TrimSpace(shared.StringArg(errorPayload, "code", "")))
|
||||
message := strings.ToLower(strings.TrimSpace(shared.StringArg(errorPayload, "message", "")))
|
||||
if !strings.Contains(message, "xworkmate.session.prepare") {
|
||||
return false
|
||||
}
|
||||
return code == "INVALID_REQUEST" ||
|
||||
code == "METHOD_NOT_FOUND" ||
|
||||
code == "UNKNOWN_METHOD" ||
|
||||
strings.Contains(message, "unknown method") ||
|
||||
strings.Contains(message, "method not found")
|
||||
}
|
||||
|
||||
func openClawLegacyPreparedArtifactScope(params map[string]any, sessionKey string, runID string) *openClawPreparedArtifactScope {
|
||||
sessionKey = strings.TrimSpace(sessionKey)
|
||||
runID = strings.TrimSpace(runID)
|
||||
artifactScope := "tasks/" + sessionKey + "/" + runID
|
||||
workspaceRoot := openClawLegacyArtifactWorkspaceRoot(params)
|
||||
return &openClawPreparedArtifactScope{
|
||||
RemoteWorkingDirectory: workspaceRoot,
|
||||
RemoteWorkspaceRefKind: "remotePath",
|
||||
ArtifactScope: artifactScope,
|
||||
ArtifactDirectory: filepath.Join(workspaceRoot, filepath.FromSlash(artifactScope)),
|
||||
RelativeArtifactDirectory: artifactScope,
|
||||
ScopeKind: "task",
|
||||
}
|
||||
}
|
||||
|
||||
func openClawLegacyArtifactWorkspaceRoot(params map[string]any) string {
|
||||
for _, key := range []string{"remoteWorkingDirectoryHint", "remoteWorkingDirectory"} {
|
||||
value := strings.TrimSpace(shared.StringArg(params, key, ""))
|
||||
if value == "" {
|
||||
continue
|
||||
}
|
||||
cleaned := filepath.Clean(value)
|
||||
if strings.HasPrefix(cleaned, "/home/ubuntu/.openclaw/workspace") {
|
||||
return strings.TrimRight(cleaned, string(os.PathSeparator))
|
||||
}
|
||||
}
|
||||
return "/home/ubuntu/.openclaw/workspace"
|
||||
}
|
||||
|
||||
func openClawSessionPrepareParams(params map[string]any, openClawSessionKey string, runID string, artifactContract openClawArtifactContract) map[string]any {
|
||||
appThreadKey := openClawAppThreadKey(params)
|
||||
result := map[string]any{
|
||||
@ -654,15 +616,39 @@ func openClawSessionPrepareParams(params map[string]any, openClawSessionKey stri
|
||||
if len(artifactContract.ExpectedArtifactDirs) > 0 {
|
||||
result["expectedArtifactDirs"] = append([]string(nil), artifactContract.ExpectedArtifactDirs...)
|
||||
}
|
||||
if sessionID := strings.TrimSpace(shared.StringArg(params, "sessionId", "")); sessionID != "" {
|
||||
result["sessionId"] = sessionID
|
||||
}
|
||||
if threadID := strings.TrimSpace(shared.StringArg(params, "threadId", "")); threadID != "" {
|
||||
result["threadId"] = threadID
|
||||
if workspaceDir := openClawArtifactWorkspaceDir(params); workspaceDir != "" {
|
||||
result["workspaceDir"] = workspaceDir
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func openClawArtifactWorkspaceDir(params map[string]any) string {
|
||||
for _, key := range []string{"workspaceDir", "remoteWorkingDirectoryHint", "remoteWorkingDirectory"} {
|
||||
if value := strings.TrimSpace(shared.StringArg(params, key, "")); value != "" {
|
||||
return value
|
||||
}
|
||||
}
|
||||
if workingDirectory := strings.TrimSpace(shared.StringArg(params, "workingDirectory", "")); isOpenClawWorkspacePath(workingDirectory) {
|
||||
return workingDirectory
|
||||
}
|
||||
if configured := strings.TrimSpace(os.Getenv("OPENCLAW_WORKSPACE_DIR")); configured != "" {
|
||||
return configured
|
||||
}
|
||||
return "~/.openclaw/workspace"
|
||||
}
|
||||
|
||||
func isOpenClawWorkspacePath(path string) bool {
|
||||
path = strings.TrimSpace(path)
|
||||
if path == "" {
|
||||
return false
|
||||
}
|
||||
normalized := filepath.ToSlash(filepath.Clean(path))
|
||||
return strings.HasPrefix(normalized, "/home/ubuntu/.openclaw/workspace") ||
|
||||
strings.HasPrefix(normalized, "/Users/") && strings.Contains(normalized, "/.openclaw/workspace") ||
|
||||
strings.HasPrefix(normalized, "~/.openclaw/workspace") ||
|
||||
strings.HasPrefix(normalized, "$HOME/.openclaw/workspace")
|
||||
}
|
||||
|
||||
func openClawAppThreadKey(params map[string]any) string {
|
||||
if value := strings.TrimSpace(shared.StringArg(params, "appThreadKey", "")); value != "" {
|
||||
return value
|
||||
|
||||
@ -534,6 +534,15 @@ func TestExecuteSessionTaskGatewayAutoConnectsLocalOpenClaw(t *testing.T) {
|
||||
if _, ok := prepareParams["sessionKey"]; ok {
|
||||
t.Fatalf("expected prepare params to omit legacy sessionKey, got %#v", prepareParams)
|
||||
}
|
||||
if _, ok := prepareParams["sessionId"]; ok {
|
||||
t.Fatalf("expected prepare params to omit app sessionId, got %#v", prepareParams)
|
||||
}
|
||||
if _, ok := prepareParams["threadId"]; ok {
|
||||
t.Fatalf("expected prepare params to omit app threadId, got %#v", prepareParams)
|
||||
}
|
||||
if got := shared.StringArg(prepareParams, "workspaceDir", ""); got != "~/.openclaw/workspace" {
|
||||
t.Fatalf("expected bridge to supply OpenClaw workspaceDir, got %#v", prepareParams)
|
||||
}
|
||||
if got := shared.ListArg(prepareParams, "expectedArtifactDirs"); !sameAnyStringSlice(got, []string{"assets/images/", "reports/"}) {
|
||||
t.Fatalf("expected prepare expectedArtifactDirs from app contract, got %#v", prepareParams)
|
||||
}
|
||||
@ -742,7 +751,7 @@ func TestGatewayRequestSkillsStatusAutoConnectsOpenClaw(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteSessionTaskGatewayFallsBackWhenPrepareUnsupported(t *testing.T) {
|
||||
func TestExecuteSessionTaskGatewayFailsWhenPrepareUnsupported(t *testing.T) {
|
||||
gateway := newAcpFakeOpenClawGateway(t)
|
||||
gateway.unsupportedSessionPrepare.Store(true)
|
||||
defer gateway.Close()
|
||||
@ -767,26 +776,15 @@ func TestExecuteSessionTaskGatewayFallsBackWhenPrepareUnsupported(t *testing.T)
|
||||
},
|
||||
},
|
||||
})
|
||||
if rpcErr != nil {
|
||||
t.Fatalf("expected legacy prepare fallback response, got rpc error: %#v", rpcErr)
|
||||
if rpcErr == nil {
|
||||
t.Fatalf("expected prepare error without legacy fallback, got response: %#v", response)
|
||||
return
|
||||
}
|
||||
if got := response["output"]; got != "gateway pong" {
|
||||
t.Fatalf("expected gateway pong output after legacy prepare fallback, got %#v", response)
|
||||
if rpcErr.Code != -32002 || !strings.Contains(rpcErr.Message, "unknown method: xworkmate.session.prepare") {
|
||||
t.Fatalf("expected surfaced prepare unsupported error, got %#v", rpcErr)
|
||||
}
|
||||
if got := gateway.Methods(); !sameMethods(got, []string{"connect", "xworkmate.session.prepare", "chat.send", "xworkmate.tasks.get"}) {
|
||||
t.Fatalf("expected legacy prepare attempt to continue through chat/send and native task lookup, got %#v", got)
|
||||
}
|
||||
chatParams := gateway.LastChatSendParams()
|
||||
receipt := strings.TrimSpace(shared.StringArg(chatParams, "systemProvenanceReceipt", ""))
|
||||
sessionKey := shared.StringArg(chatParams, "sessionKey", "")
|
||||
runID := shared.StringArg(chatParams, "idempotencyKey", "")
|
||||
for _, expected := range []string{
|
||||
"artifactDirectory: /home/ubuntu/.openclaw/workspace/tasks/" + sessionKey + "/" + runID,
|
||||
"artifactScope: tasks/" + sessionKey + "/" + runID,
|
||||
} {
|
||||
if !strings.Contains(receipt, expected) {
|
||||
t.Fatalf("expected fallback provenance receipt to include %q, got %q", expected, receipt)
|
||||
}
|
||||
if got := gateway.Methods(); !sameMethods(got, []string{"connect", "xworkmate.session.prepare"}) {
|
||||
t.Fatalf("expected bridge to stop before chat.send when prepare is unsupported, got %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -215,6 +215,11 @@ if final is None:
|
||||
raise SystemExit("missing final OpenClaw result envelope")
|
||||
if not payloads or payloads[-1].get("done") is not True:
|
||||
raise SystemExit("missing SSE done marker")
|
||||
if isinstance(final.get("error"), dict) and final["error"]:
|
||||
error = final["error"]
|
||||
message = error.get("message") or json.dumps(error, ensure_ascii=False, sort_keys=True)
|
||||
preview = json.dumps(final, ensure_ascii=False, sort_keys=True)[:1500]
|
||||
raise SystemExit(f"OpenClaw smoke RPC error: {message}\nenvelope preview: {preview}")
|
||||
|
||||
result = terminal_result(final.get("result") or final.get("payload") or {})
|
||||
handle = result
|
||||
|
||||
Loading…
Reference in New Issue
Block a user