chore: tidy agent event streaming types
This commit is contained in:
@@ -6,6 +6,7 @@ import { piSpec } from "../agents/pi.js";
|
|||||||
import type { AgentMeta, AgentToolResult } from "../agents/types.js";
|
import type { AgentMeta, AgentToolResult } from "../agents/types.js";
|
||||||
import type { WarelayConfig } from "../config/config.js";
|
import type { WarelayConfig } from "../config/config.js";
|
||||||
import { isVerbose, logVerbose } from "../globals.js";
|
import { isVerbose, logVerbose } from "../globals.js";
|
||||||
|
import { emitAgentEvent } from "../infra/agent-events.js";
|
||||||
import { logError } from "../logger.js";
|
import { logError } from "../logger.js";
|
||||||
import { getChildLogger } from "../logging.js";
|
import { getChildLogger } from "../logging.js";
|
||||||
import { splitMediaFromOutput } from "../media/parse.js";
|
import { splitMediaFromOutput } from "../media/parse.js";
|
||||||
@@ -13,7 +14,6 @@ import { enqueueCommand } from "../process/command-queue.js";
|
|||||||
import type { runCommandWithTimeout } from "../process/exec.js";
|
import type { runCommandWithTimeout } from "../process/exec.js";
|
||||||
import { runPiRpc } from "../process/tau-rpc.js";
|
import { runPiRpc } from "../process/tau-rpc.js";
|
||||||
import { applyTemplate, type TemplateContext } from "./templating.js";
|
import { applyTemplate, type TemplateContext } from "./templating.js";
|
||||||
import { emitAgentEvent } from "../infra/agent-events.js";
|
|
||||||
import {
|
import {
|
||||||
formatToolAggregate,
|
formatToolAggregate,
|
||||||
shortenMeta,
|
shortenMeta,
|
||||||
@@ -161,7 +161,10 @@ type CommandReplyParams = {
|
|||||||
verboseLevel?: "off" | "on";
|
verboseLevel?: "off" | "on";
|
||||||
onPartialReply?: (payload: ReplyPayload) => Promise<void> | void;
|
onPartialReply?: (payload: ReplyPayload) => Promise<void> | void;
|
||||||
runId?: string;
|
runId?: string;
|
||||||
onAgentEvent?: (evt: { stream: string; data: Record<string, unknown> }) => void;
|
onAgentEvent?: (evt: {
|
||||||
|
stream: string;
|
||||||
|
data: Record<string, unknown>;
|
||||||
|
}) => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CommandReplyMeta = {
|
export type CommandReplyMeta = {
|
||||||
@@ -555,7 +558,7 @@ export async function runCommandReply(
|
|||||||
streamedAny = true;
|
streamedAny = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
const run = async () => {
|
const run = async () => {
|
||||||
const runId = params.runId ?? crypto.randomUUID();
|
const runId = params.runId ?? crypto.randomUUID();
|
||||||
const rpcPromptIndex =
|
const rpcPromptIndex =
|
||||||
promptIndex >= 0 ? promptIndex : finalArgv.length - 1;
|
promptIndex >= 0 ? promptIndex : finalArgv.length - 1;
|
||||||
@@ -572,15 +575,29 @@ export async function runCommandReply(
|
|||||||
}
|
}
|
||||||
return copy;
|
return copy;
|
||||||
})();
|
})();
|
||||||
|
type RpcStreamEvent = {
|
||||||
|
type?: string;
|
||||||
|
message?: {
|
||||||
|
role?: string;
|
||||||
|
content?: unknown;
|
||||||
|
toolCallId?: string;
|
||||||
|
tool_call_id?: string;
|
||||||
|
} & Record<string, unknown>;
|
||||||
|
toolName?: string;
|
||||||
|
toolCallId?: string;
|
||||||
|
args?: unknown;
|
||||||
|
[key: string]: unknown;
|
||||||
|
};
|
||||||
|
|
||||||
const rpcResult = await runPiRpc({
|
const rpcResult = await runPiRpc({
|
||||||
argv: rpcArgvForRun,
|
argv: rpcArgvForRun,
|
||||||
cwd: reply.cwd,
|
cwd: reply.cwd,
|
||||||
prompt: body,
|
prompt: body,
|
||||||
timeoutMs,
|
timeoutMs,
|
||||||
onEvent: (line: string) => {
|
onEvent: (line: string) => {
|
||||||
let ev: any;
|
let ev: RpcStreamEvent;
|
||||||
try {
|
try {
|
||||||
ev = JSON.parse(line);
|
ev = JSON.parse(line) as RpcStreamEvent;
|
||||||
} catch {
|
} catch {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -639,11 +656,7 @@ export async function runCommandReply(
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (
|
if (pendingToolName && toolName && toolName !== pendingToolName) {
|
||||||
pendingToolName &&
|
|
||||||
toolName &&
|
|
||||||
toolName !== pendingToolName
|
|
||||||
) {
|
|
||||||
flushPendingTool();
|
flushPendingTool();
|
||||||
}
|
}
|
||||||
if (!pendingToolName) pendingToolName = toolName;
|
if (!pendingToolName) pendingToolName = toolName;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import chalk from "chalk";
|
|
||||||
import { randomUUID } from "node:crypto";
|
import { randomUUID } from "node:crypto";
|
||||||
|
import chalk from "chalk";
|
||||||
import { Command } from "commander";
|
import { Command } from "commander";
|
||||||
import { agentCommand } from "../commands/agent.js";
|
import { agentCommand } from "../commands/agent.js";
|
||||||
import { healthCommand } from "../commands/health.js";
|
import { healthCommand } from "../commands/health.js";
|
||||||
|
|||||||
@@ -21,11 +21,11 @@ import {
|
|||||||
type SessionEntry,
|
type SessionEntry,
|
||||||
saveSessionStore,
|
saveSessionStore,
|
||||||
} from "../config/sessions.js";
|
} from "../config/sessions.js";
|
||||||
|
import { emitAgentEvent } from "../infra/agent-events.js";
|
||||||
import { runCommandWithTimeout } from "../process/exec.js";
|
import { runCommandWithTimeout } from "../process/exec.js";
|
||||||
import { defaultRuntime, type RuntimeEnv } from "../runtime.js";
|
import { defaultRuntime, type RuntimeEnv } from "../runtime.js";
|
||||||
import { normalizeE164 } from "../utils.js";
|
import { normalizeE164 } from "../utils.js";
|
||||||
import { sendViaIpc } from "../web/ipc.js";
|
import { sendViaIpc } from "../web/ipc.js";
|
||||||
import { emitAgentEvent } from "../infra/agent-events.js";
|
|
||||||
|
|
||||||
type AgentCommandOpts = {
|
type AgentCommandOpts = {
|
||||||
message: string;
|
message: string;
|
||||||
@@ -306,7 +306,7 @@ export async function agentCommand(
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
let result;
|
let result: Awaited<ReturnType<typeof runCommandReply>>;
|
||||||
try {
|
try {
|
||||||
result = await runCommandReply({
|
result = await runCommandReply({
|
||||||
reply: { ...replyCfg, mode: "command" },
|
reply: { ...replyCfg, mode: "command" },
|
||||||
|
|||||||
@@ -28,4 +28,3 @@ export function onAgentEvent(listener: (evt: AgentEventPayload) => void) {
|
|||||||
listeners.add(listener);
|
listeners.add(listener);
|
||||||
return () => listeners.delete(listener);
|
return () => listeners.delete(listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,13 +3,13 @@ import net from "node:net";
|
|||||||
import { getHealthSnapshot, type HealthSummary } from "../commands/health.js";
|
import { getHealthSnapshot, type HealthSummary } from "../commands/health.js";
|
||||||
import { getStatusSummary, type StatusSummary } from "../commands/status.js";
|
import { getStatusSummary, type StatusSummary } from "../commands/status.js";
|
||||||
import { defaultRuntime, type RuntimeEnv } from "../runtime.js";
|
import { defaultRuntime, type RuntimeEnv } from "../runtime.js";
|
||||||
|
import { type AgentEventPayload, onAgentEvent } from "./agent-events.js";
|
||||||
import {
|
import {
|
||||||
emitHeartbeatEvent,
|
emitHeartbeatEvent,
|
||||||
getLastHeartbeatEvent,
|
getLastHeartbeatEvent,
|
||||||
type HeartbeatEventPayload,
|
type HeartbeatEventPayload,
|
||||||
onHeartbeatEvent,
|
onHeartbeatEvent,
|
||||||
} from "./heartbeat-events.js";
|
} from "./heartbeat-events.js";
|
||||||
import { onAgentEvent, type AgentEventPayload } from "./agent-events.js";
|
|
||||||
|
|
||||||
type ControlRequest = {
|
type ControlRequest = {
|
||||||
type: "request";
|
type: "request";
|
||||||
|
|||||||
Reference in New Issue
Block a user