diff --git a/.changeset/agui-effect.md b/.changeset/agui-effect.md new file mode 100644 index 00000000..d9b9da31 --- /dev/null +++ b/.changeset/agui-effect.md @@ -0,0 +1,14 @@ +--- +"@systemfsoftware/agent": minor +"@systemfsoftware/agent-ai-decision": none +"@systemfsoftware/agent-platform-cloudflare": none +"@systemfsoftware/agent-platform-node": none +"@systemfsoftware/agent-pr-review": none +"@systemfsoftware/agent-storage-cloudflare": none +"@systemfsoftware/agent-storage-memory": none +"@systemfsoftware/agent-storage-postgres": none +"@systemfsoftware/agent-storage-sql": none +"@systemfsoftware/agent-storage-sqlite": none +"@systemfsoftware/agent-testing": none +--- +Adds `@systemfsoftware/agent/agui`: `toAgUiEvents` maps a Run's semantic event stream to AG-UI events, `makeAgUiHandler` serves them through `AgUiServer.handler`, which applies the AG-UI run lifecycle, and `fromAgUiInput` adapts an AG-UI run input into a Prompt. Suspended and interrupted Runs end with AG-UI `RunFinished` outcomes instead of `RunError`, state snapshots are not emitted, and subagent events are omitted unless `includeSubagents` is set. Tool results and failures are JSON-encoded in `TOOL_CALL_RESULT`, and a suspended Run reports one interrupt per pending approval. diff --git a/packages/effect-agent/package.json b/packages/effect-agent/package.json index ee573b32..1c4eed38 100644 --- a/packages/effect-agent/package.json +++ b/packages/effect-agent/package.json @@ -24,6 +24,7 @@ "./agent-registration": "./src/durable/AgentRegistration.ts", "./agent-runtime": "./src/engine/AgentRuntime.ts", "./agent-updates": "./src/core/AgentUpdates.ts", + "./agui": "./src/engine/AgUi.ts", "./browser-use": "./src/capabilities/BrowserUse.ts", "./budget": "./src/capabilities/Budget.ts", "./code-executor": "./src/sandbox/CodeExecutor.ts", @@ -168,6 +169,10 @@ "types": "./dist/core/AgentUpdates.d.mts", "default": "./dist/core/AgentUpdates.mjs" }, + "./agui": { + "types": "./dist/engine/AgUi.d.mts", + "default": "./dist/engine/AgUi.mjs" + }, "./browser-use": { "types": "./dist/capabilities/BrowserUse.d.mts", "default": "./dist/capabilities/BrowserUse.mjs" diff --git a/packages/effect-agent/src/engine/AgUi.ts b/packages/effect-agent/src/engine/AgUi.ts new file mode 100644 index 00000000..8395f2f5 --- /dev/null +++ b/packages/effect-agent/src/engine/AgUi.ts @@ -0,0 +1,587 @@ +// @effect-diagnostics experimentalApiUsage:off +import * as AgUi from 'effect/ai/AgUi' +import * as AgUiSchema from 'effect/ai/AgUiSchema' +import * as AgUiServer from 'effect/ai/AgUiServer' +import type * as Prompt from 'effect/ai/Prompt' +import * as Effect from 'effect/Effect' +import * as Match from 'effect/Match' +import * as Schema from 'effect/Schema' +import * as Stream from 'effect/Stream' +import type { + AgentUpdateEmitted, + ApprovalRequested, + BudgetWarning, + CompactionPerformed, + ModelRestarted, + ModelStarted, + ReasoningDelta, + RunCompleted, + RunEvent, + RunFailed, + RunInterrupted, + RunStarted, + RunSuspended, + SteeringApplied, + SubagentCompleted, + SubagentFailed, + SubagentInterrupted, + SubagentJoined, + SubagentProgress, + SubagentRequested, + SubagentStarted, + TextDelta, + ToolCallDeclared, + ToolCallFailed, + ToolCallStarted, + ToolCallSucceeded, + ToolProgress, + TurnCompleted, + TurnStarted, +} from '../core/RunEvent' +import type { RunTotals } from '../core/Usage' + +interface AgUiEventOptions { + readonly includeSubagents?: boolean | undefined +} + +interface MapperState { + readonly textMessageId: string | undefined + readonly reasoningMessageId: string | undefined + readonly stepName: string | undefined + readonly pendingApprovals: ReadonlyArray + readonly openSubagents: ReadonlyArray +} + +type Mapped = readonly [MapperState, ReadonlyArray] + +const initialState: MapperState = { + textMessageId: undefined, + reasoningMessageId: undefined, + stepName: undefined, + pendingApprovals: [], + openSubagents: [], +} + +const encodeJsonEvent = Schema.encodeEffect(Schema.fromJsonString(Schema.Json)) + +const encodeJson = (value: Schema.Json): Effect.Effect => encodeJsonEvent(value).pipe(Effect.orDie) + +const mapped = (value: Mapped): Effect.Effect => Effect.succeed(value) + +const subagentUsage = (usage: RunTotals | undefined): ReadonlyArray => tokenUsage(usage) ?? [] + +const pendingWith = (pending: ReadonlyArray, toolCallId: string): ReadonlyArray => + pending.includes(toolCallId) ? pending : [...pending, toolCallId] + +const pendingWithout = (pending: ReadonlyArray, toolCallId: string): ReadonlyArray => + pending.includes(toolCallId) ? pending.filter((value) => value !== toolCallId) : pending + +const suspensionId = (runId: string, sequence: number, toolCallId: string | undefined): string => + toolCallId === undefined + ? `${runId}:${sequence}:suspended` + : `${runId}:${sequence}:${toolCallId}:suspended` + +const suspendInterrupt = ( + runId: string, + sequence: number, + reason: string, + toolCallId: string | undefined, +): AgUiSchema.Interrupt => ({ + id: suspensionId(runId, sequence, toolCallId), + reason, + ...(toolCallId === undefined ? {} : { toolCallId }), +}) + +const suspensionInterrupts = ( + runId: string, + sequence: number, + reason: string, + pending: ReadonlyArray, +): readonly [AgUiSchema.Interrupt, ...Array] => { + const [head, ...rest] = pending + return [ + suspendInterrupt(runId, sequence, reason, head), + ...rest.map((toolCallId) => suspendInterrupt(runId, sequence, reason, toolCallId)), + ] +} + +const tokenUsage = (usage: RunTotals | undefined): ReadonlyArray | undefined => + usage === undefined + ? undefined + : [ + { + inputTokens: usage.inputTokens, + outputTokens: usage.outputTokens, + totalTokens: usage.inputTokens + usage.outputTokens, + }, + ] + +const closeText = (state: MapperState): Mapped => + state.textMessageId === undefined + ? [state, []] + : [ + { ...state, textMessageId: undefined }, + [{ type: 'TEXT_MESSAGE_END', messageId: state.textMessageId }], + ] + +const closeReasoning = (state: MapperState): Mapped => + state.reasoningMessageId === undefined + ? [state, []] + : [ + { ...state, reasoningMessageId: undefined }, + [ + { type: 'REASONING_MESSAGE_END', messageId: state.reasoningMessageId }, + { type: 'REASONING_END', messageId: state.reasoningMessageId }, + ], + ] + +const closeContent = (state: MapperState): Mapped => { + const [afterText, textEvents] = closeText(state) + const [afterReasoning, reasoningEvents] = closeReasoning(afterText) + return [afterReasoning, [...textEvents, ...reasoningEvents]] +} + +const closeStep = (state: MapperState): Mapped => + state.stepName === undefined + ? [state, []] + : [{ ...state, stepName: undefined }, [{ type: 'STEP_FINISHED', stepName: state.stepName }]] + +const closeOpen = (state: MapperState): Mapped => { + const [afterContent, contentEvents] = closeContent(state) + const [afterStep, stepEvents] = closeStep(afterContent) + return [afterStep, [...contentEvents, ...stepEvents]] +} + +const subagentWith = (open: ReadonlyArray, childRunId: string): ReadonlyArray => + open.includes(childRunId) ? open : [...open, childRunId] + +const subagentWithout = (open: ReadonlyArray, childRunId: string): ReadonlyArray => + open.includes(childRunId) ? open.filter((value) => value !== childRunId) : open + +const closeSubagents = (state: MapperState): Mapped => { + if (state.openSubagents.length === 0) return [state, []] + return [ + { ...state, openSubagents: [] }, + state.openSubagents.map((subagentRunId): AgUiSchema.Event => ({ + type: 'SUBAGENT_ERROR', + subagentRunId, + message: 'The subagent did not finish.', + })), + ] +} + +const closeAll = (state: MapperState): Mapped => { + const [afterOpen, openEvents] = closeOpen(state) + const [afterSubagents, subagentEvents] = closeSubagents(afterOpen) + return [afterSubagents, [...openEvents, ...subagentEvents]] +} + +const appendText = (state: MapperState, event: TextDelta): Mapped => { + const [closed, closing] = closeReasoning(state) + + if (closed.textMessageId !== undefined) { + return [ + closed, + [...closing, { type: 'TEXT_MESSAGE_CONTENT', messageId: closed.textMessageId, delta: event.text }], + ] + } + + const messageId = `${event.runId}:${event.sequence}:text` + + return [ + { ...closed, textMessageId: messageId }, + [ + ...closing, + { type: 'TEXT_MESSAGE_START', messageId, role: 'assistant' }, + { type: 'TEXT_MESSAGE_CONTENT', messageId, delta: event.text }, + ], + ] +} + +const appendReasoning = (state: MapperState, event: ReasoningDelta): Mapped => { + const [closed, closing] = closeText(state) + + if (closed.reasoningMessageId !== undefined) { + return [ + closed, + [...closing, { type: 'REASONING_MESSAGE_CONTENT', messageId: closed.reasoningMessageId, delta: event.text }], + ] + } + + const messageId = `${event.runId}:${event.sequence}:reasoning` + + return [ + { ...closed, reasoningMessageId: messageId }, + [ + ...closing, + { type: 'REASONING_START', messageId }, + { type: 'REASONING_MESSAGE_START', messageId, role: 'reasoning' }, + { type: 'REASONING_MESSAGE_CONTENT', messageId, delta: event.text }, + ], + ] +} + +const mapEvent = ( + state: MapperState, + event: RunEvent, + includeSubagents: boolean, +): Effect.Effect => + Match.value(event).pipe( + Match.tagsExhaustive({ + RunStarted: (event: RunStarted) => + mapped([ + initialState, + [ + { + type: 'RUN_STARTED', + threadId: event.threadId, + runId: event.runId, + protocolVersion: AgUiSchema.protocolVersion, + }, + ], + ]), + TurnStarted: (event: TurnStarted) => { + const [closed, closing] = closeOpen(state) + const stepName = `turn-${event.turn}` + return mapped([{ ...closed, stepName }, [...closing, { type: 'STEP_STARTED', stepName }]]) + }, + ModelStarted: (_event: ModelStarted) => mapped([state, []]), + ModelRestarted: (event: ModelRestarted) => + mapped([ + state, + [ + { + type: 'CUSTOM', + name: 'effect_agent.model_restarted', + value: { turn: event.turn, reason: event.reason }, + }, + ], + ]), + SteeringApplied: (event: SteeringApplied) => + mapped([ + state, + [ + { + type: 'CUSTOM', + name: 'effect_agent.steering_applied', + value: { inputIds: [...event.inputIds] }, + }, + ], + ]), + TextDelta: (event: TextDelta) => mapped(appendText(state, event)), + ReasoningDelta: (event: ReasoningDelta) => mapped(appendReasoning(state, event)), + ToolCallDeclared: (event: ToolCallDeclared) => { + const parentMessageId = state.textMessageId + const [closed, closing] = closeContent(state) + return Effect.map(encodeJson(event.parameters), (delta): Mapped => [ + closed, + [ + ...closing, + { + type: 'TOOL_CALL_START', + toolCallId: event.toolCallId, + toolCallName: event.toolName, + ...(parentMessageId === undefined ? {} : { parentMessageId }), + }, + { type: 'TOOL_CALL_ARGS', toolCallId: event.toolCallId, delta }, + { type: 'TOOL_CALL_END', toolCallId: event.toolCallId }, + ], + ]) + }, + ToolCallStarted: (event: ToolCallStarted) => + mapped([{ ...state, pendingApprovals: pendingWithout(state.pendingApprovals, event.toolCallId) }, []]), + ToolProgress: (_event: ToolProgress) => mapped([state, []]), + ToolCallSucceeded: (event: ToolCallSucceeded) => + Effect.map(encodeJson(event.result), (content): Mapped => [ + { ...state, pendingApprovals: pendingWithout(state.pendingApprovals, event.toolCallId) }, + [ + { + type: 'TOOL_CALL_RESULT', + messageId: `${event.runId}:${event.toolCallId}:result`, + toolCallId: event.toolCallId, + content, + role: 'tool', + }, + ], + ]), + ToolCallFailed: (event: ToolCallFailed) => + Effect.map( + encodeJson({ error: event.message, code: event.errorTag }), + (content): Mapped => [ + { ...state, pendingApprovals: pendingWithout(state.pendingApprovals, event.toolCallId) }, + [ + { + type: 'TOOL_CALL_RESULT', + messageId: `${event.runId}:${event.toolCallId}:error`, + toolCallId: event.toolCallId, + content, + role: 'tool', + }, + ], + ], + ), + ApprovalRequested: (event: ApprovalRequested) => + mapped([ + { ...state, pendingApprovals: pendingWith(state.pendingApprovals, event.toolCallId) }, + [ + { + type: 'CUSTOM', + name: 'effect_agent.approval_requested', + value: { toolCallId: event.toolCallId, toolName: event.toolName }, + }, + ], + ]), + TurnCompleted: (_event: TurnCompleted) => mapped(closeOpen(state)), + BudgetWarning: (event: BudgetWarning) => + mapped([ + state, + [ + { + type: 'CUSTOM', + name: 'effect_agent.budget_warning', + value: { limit: event.limit, consumed: event.consumed, limitValue: event.limitValue }, + }, + ], + ]), + CompactionPerformed: (event: CompactionPerformed) => + mapped([ + state, + [ + { + type: 'CUSTOM', + name: 'effect_agent.compaction_performed', + value: { + turn: event.turn, + kind: event.kind, + tokensBefore: event.tokensBeforeEstimate, + tokensAfter: event.tokensAfterEstimate, + }, + }, + ], + ]), + AgentUpdateEmitted: (_event: AgentUpdateEmitted) => mapped([state, []]), + RunCompleted: (event: RunCompleted) => { + const [closed, closing] = closeAll(state) + const usage = tokenUsage(event.usage) + return mapped([ + { ...closed, pendingApprovals: [] }, + [ + ...closing, + { + type: 'RUN_FINISHED', + threadId: event.threadId, + runId: event.runId, + result: event.output, + outcome: { type: 'success' }, + ...(usage === undefined ? {} : { usage }), + }, + ], + ]) + }, + RunFailed: (event: RunFailed) => { + const [closed, closing] = closeAll(state) + const usage = tokenUsage(event.usage) + return mapped([ + { ...closed, pendingApprovals: [] }, + [ + ...closing, + { + type: 'RUN_ERROR', + message: event.message, + code: event.errorTag, + ...(usage === undefined ? {} : { usage }), + }, + ], + ]) + }, + RunInterrupted: (event: RunInterrupted) => { + const [closed, closing] = closeAll(state) + return mapped([ + { ...closed, pendingApprovals: [] }, + [ + ...closing, + { type: 'CUSTOM', name: 'effect_agent.run_interrupted', value: { message: event.message } }, + { + type: 'RUN_FINISHED', + threadId: event.threadId, + runId: event.runId, + outcome: { type: 'cancelled' }, + }, + ], + ]) + }, + RunSuspended: (event: RunSuspended) => { + const [closed, closing] = closeAll(state) + const usage = tokenUsage(event.usage) + return mapped([ + { ...closed, pendingApprovals: [] }, + [ + ...closing, + { type: 'CUSTOM', name: 'effect_agent.run_suspended', value: { reason: event.reason } }, + { + type: 'RUN_FINISHED', + threadId: event.threadId, + runId: event.runId, + outcome: { + type: 'interrupt', + interrupts: suspensionInterrupts( + event.runId, + event.sequence, + event.reason, + state.pendingApprovals, + ), + }, + ...(usage === undefined ? {} : { usage }), + }, + ], + ]) + }, + SubagentRequested: (_event: SubagentRequested) => mapped([state, []]), + SubagentStarted: (event: SubagentStarted) => + mapped( + includeSubagents + ? [ + { ...state, openSubagents: subagentWith(state.openSubagents, event.childRunId) }, + [ + { + type: 'SUBAGENT_STARTED', + subagentRunId: event.childRunId, + name: event.targetAgentId, + parentToolCallId: event.toolCallId, + }, + ], + ] + : [state, []], + ), + SubagentProgress: (event: SubagentProgress) => + mapped( + includeSubagents + ? [ + state, + [ + { + type: 'CUSTOM', + name: 'effect_agent.subagent_progress', + value: { summary: event.summary }, + subagentRunId: event.childRunId, + }, + ], + ] + : [state, []], + ), + SubagentCompleted: (event: SubagentCompleted) => + mapped( + includeSubagents + ? [ + { ...state, openSubagents: subagentWithout(state.openSubagents, event.childRunId) }, + [ + { + type: 'SUBAGENT_FINISHED', + subagentRunId: event.childRunId, + outcome: { type: 'success' }, + }, + ], + ] + : [state, []], + ), + SubagentFailed: (event: SubagentFailed) => + mapped( + includeSubagents + ? [ + { ...state, openSubagents: subagentWithout(state.openSubagents, event.childRunId) }, + [ + { + type: 'SUBAGENT_ERROR', + subagentRunId: event.childRunId, + message: event.message, + code: event.errorTag, + }, + ], + ] + : [state, []], + ), + SubagentInterrupted: (event: SubagentInterrupted) => + mapped( + includeSubagents + ? [ + { ...state, openSubagents: subagentWithout(state.openSubagents, event.childRunId) }, + [ + { + type: 'CUSTOM', + name: 'effect_agent.subagent_interrupted', + value: { reason: event.reason }, + subagentRunId: event.childRunId, + }, + { + type: 'SUBAGENT_FINISHED', + subagentRunId: event.childRunId, + outcome: { type: 'suspended' }, + }, + ], + ] + : [state, []], + ), + SubagentJoined: (event: SubagentJoined) => + mapped( + includeSubagents + ? [ + { ...state, openSubagents: subagentWithout(state.openSubagents, event.childRunId) }, + [ + { + type: 'CUSTOM', + name: 'effect_agent.subagent_joined', + value: { usage: subagentUsage(event.usage) }, + subagentRunId: event.childRunId, + }, + ], + ] + : [state, []], + ), + }), + ) + +const toAgUiEventsOf = ( + events: Stream.Stream, + options: AgUiEventOptions | undefined, +): Stream.Stream => + events.pipe( + Stream.mapAccumEffect( + () => initialState, + (state, event) => mapEvent(state, event, options?.includeSubagents === true), + ), + ) + +const isRunEventStream = ( + value: Stream.Stream | AgUiEventOptions | undefined, +): value is Stream.Stream => Stream.isStream(value) + +export function toAgUiEvents( + events: Stream.Stream, + options?: AgUiEventOptions, +): Stream.Stream +export function toAgUiEvents( + options?: AgUiEventOptions, +): (events: Stream.Stream) => Stream.Stream +export function toAgUiEvents( + first?: Stream.Stream | AgUiEventOptions, + options?: AgUiEventOptions, +): + | Stream.Stream + | ((events: Stream.Stream) => Stream.Stream) +{ + return isRunEventStream(first) + ? toAgUiEventsOf(first, options) + : (events) => toAgUiEventsOf(events, first) +} + +export const fromAgUiInput = (input: AgUiSchema.RunAgentInput): Prompt.Prompt => AgUi.toPrompt(input) + +interface AgUiHandlerConfig { + readonly run: (input: AgUiSchema.RunAgentInput) => Stream.Stream + readonly includeSubagents?: boolean | undefined +} + +export const makeAgUiHandler = (config: AgUiHandlerConfig) => + AgUiServer.handler((input: AgUiSchema.RunAgentInput) => + toAgUiEventsOf(config.run(input), { includeSubagents: config.includeSubagents }) + ) diff --git a/packages/effect-agent/src/index.ts b/packages/effect-agent/src/index.ts index 0259ae3a..f470da39 100644 --- a/packages/effect-agent/src/index.ts +++ b/packages/effect-agent/src/index.ts @@ -79,6 +79,7 @@ export * as WakeScheduler from './durable/WakeScheduler' export * as WorkerAdmission from './durable/WorkerAdmission' export * as WorkerHost from './durable/WorkerHost' export * as AgentRuntime from './engine/AgentRuntime' +export * as AgUi from './engine/AgUi' export * as Compaction from './engine/Compaction' export * as ContextCompactor from './engine/ContextCompactor' export * as ContextHistory from './engine/ContextHistory'