mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 20:14:30 +00:00
refactor(outbound): share tool payload extraction
This commit is contained in:
@@ -49,6 +49,7 @@ import {
|
|||||||
import { executePollAction, executeSendAction } from "./outbound-send-service.js";
|
import { executePollAction, executeSendAction } from "./outbound-send-service.js";
|
||||||
import { ensureOutboundSessionEntry, resolveOutboundSessionRoute } from "./outbound-session.js";
|
import { ensureOutboundSessionEntry, resolveOutboundSessionRoute } from "./outbound-session.js";
|
||||||
import { resolveChannelTarget, type ResolvedMessagingTarget } from "./target-resolver.js";
|
import { resolveChannelTarget, type ResolvedMessagingTarget } from "./target-resolver.js";
|
||||||
|
import { extractToolPayload } from "./tool-payload.js";
|
||||||
|
|
||||||
export type MessageActionRunnerGateway = {
|
export type MessageActionRunnerGateway = {
|
||||||
url?: string;
|
url?: string;
|
||||||
@@ -156,30 +157,6 @@ export function getToolResult(
|
|||||||
return "toolResult" in result ? result.toolResult : undefined;
|
return "toolResult" in result ? result.toolResult : undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
function extractToolPayload(result: AgentToolResult<unknown>): unknown {
|
|
||||||
if (result.details !== undefined) {
|
|
||||||
return result.details;
|
|
||||||
}
|
|
||||||
const textBlock = Array.isArray(result.content)
|
|
||||||
? result.content.find(
|
|
||||||
(block) =>
|
|
||||||
block &&
|
|
||||||
typeof block === "object" &&
|
|
||||||
(block as { type?: unknown }).type === "text" &&
|
|
||||||
typeof (block as { text?: unknown }).text === "string",
|
|
||||||
)
|
|
||||||
: undefined;
|
|
||||||
const text = (textBlock as { text?: string } | undefined)?.text;
|
|
||||||
if (text) {
|
|
||||||
try {
|
|
||||||
return JSON.parse(text);
|
|
||||||
} catch {
|
|
||||||
return text;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result.content ?? result;
|
|
||||||
}
|
|
||||||
|
|
||||||
function applyCrossContextMessageDecoration({
|
function applyCrossContextMessageDecoration({
|
||||||
params,
|
params,
|
||||||
message,
|
message,
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import { dispatchChannelMessageAction } from "../../channels/plugins/message-act
|
|||||||
import { appendAssistantMessageToSessionTranscript } from "../../config/sessions.js";
|
import { appendAssistantMessageToSessionTranscript } from "../../config/sessions.js";
|
||||||
import { throwIfAborted } from "./abort.js";
|
import { throwIfAborted } from "./abort.js";
|
||||||
import { sendMessage, sendPoll } from "./message.js";
|
import { sendMessage, sendPoll } from "./message.js";
|
||||||
|
import { extractToolPayload } from "./tool-payload.js";
|
||||||
|
|
||||||
export type OutboundGatewayContext = {
|
export type OutboundGatewayContext = {
|
||||||
url?: string;
|
url?: string;
|
||||||
@@ -37,30 +38,6 @@ export type OutboundSendContext = {
|
|||||||
silent?: boolean;
|
silent?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
function extractToolPayload(result: AgentToolResult<unknown>): unknown {
|
|
||||||
if (result.details !== undefined) {
|
|
||||||
return result.details;
|
|
||||||
}
|
|
||||||
const textBlock = Array.isArray(result.content)
|
|
||||||
? result.content.find(
|
|
||||||
(block) =>
|
|
||||||
block &&
|
|
||||||
typeof block === "object" &&
|
|
||||||
(block as { type?: unknown }).type === "text" &&
|
|
||||||
typeof (block as { text?: unknown }).text === "string",
|
|
||||||
)
|
|
||||||
: undefined;
|
|
||||||
const text = (textBlock as { text?: string } | undefined)?.text;
|
|
||||||
if (text) {
|
|
||||||
try {
|
|
||||||
return JSON.parse(text);
|
|
||||||
} catch {
|
|
||||||
return text;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result.content ?? result;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function executeSendAction(params: {
|
export async function executeSendAction(params: {
|
||||||
ctx: OutboundSendContext;
|
ctx: OutboundSendContext;
|
||||||
to: string;
|
to: string;
|
||||||
|
|||||||
25
src/infra/outbound/tool-payload.ts
Normal file
25
src/infra/outbound/tool-payload.ts
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
import type { AgentToolResult } from "@mariozechner/pi-agent-core";
|
||||||
|
|
||||||
|
export function extractToolPayload(result: AgentToolResult<unknown>): unknown {
|
||||||
|
if (result.details !== undefined) {
|
||||||
|
return result.details;
|
||||||
|
}
|
||||||
|
const textBlock = Array.isArray(result.content)
|
||||||
|
? result.content.find(
|
||||||
|
(block) =>
|
||||||
|
block &&
|
||||||
|
typeof block === "object" &&
|
||||||
|
(block as { type?: unknown }).type === "text" &&
|
||||||
|
typeof (block as { text?: unknown }).text === "string",
|
||||||
|
)
|
||||||
|
: undefined;
|
||||||
|
const text = (textBlock as { text?: string } | undefined)?.text;
|
||||||
|
if (text) {
|
||||||
|
try {
|
||||||
|
return JSON.parse(text);
|
||||||
|
} catch {
|
||||||
|
return text;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result.content ?? result;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user