Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

DeepSeek Harness event system: emit / bail / serial / waterfall

DeepSeek Harness event system: emit / bail / serial / waterfall

How do plugins communicate in a loosely coupled way? Events are Cordis's core communication mechanism.

Harness makes heavy use of events to implement extensible extension points; this article clarifies the five dispatch modes.


Basic Usage

Events have two ends: listening and emitting.

Example

// Listen for an event: register a callback

ctx.

on

(

'event-name'

,

(

payload

)

=>

{

 

// Handle the event

}

)

// Emit an event: broadcast to all listeners

ctx.

emit

(

'event-name'

,

payload

)

Listeners registered via ctx.on are automatically removed when the plugin is unloaded.


Five dispatch modes

Cordis provides multiple dispatch modes for different interaction contracts.

behavior diagram of five dispatch modes

Mode Dispatch method Whether to await Order Whether there is a return value Typical scenario
emit Broadcast ctx.emit No In registration order No Notification type: all listeners observe
bail Short-circuit ctx.bail No In registration order Is Decision type: the first valid return value wins
serial Order ctx.serial Is In registration order Is Phased initialization: execute in order and wait
waterfall Pipeline ctx.waterfall No In registration order Is Processing chain: wrap downstream return values layer by layer
parallel Parallelism ctx.parallel Is All in parallel No Fan-out: multiple listeners process in parallel

Each event has exactly one dispatch mode and can only be dispatched through the corresponding method.

The official getting-started doc marks waterfall as "not awaited"; but since listeners are usually async functions, tutorial examples commonly write await ctx.waterfall(...) to wait for the final result of the whole processing chain. Just remember this difference; neither style is wrong.


emit: broadcast

All listeners execute synchronously, and return values are ignored.

Example

// Emitter: broadcast a "ready" message

ctx.

emit

(

'my-plugin/ready'

,

{

id

:

'runoob-worker-1'

}

)

// Listener: receive the ready message and print a log

ctx.

on

(

'my-plugin/ready'

,

(

{

id

}

)

=>

{

  console.

log

(

`$

{

id

}

is ready`

)

}

)


bail: short-circuit

Listeners run in order, and the first return value that is not null, false, or undefined becomes the final result.

Example

// Dispatcher: do a check and take the first result that "has an opinion"

const

result

=

ctx.

bail

(

'some-check'

,

input

)

// Listener: return 'blocked' if the interception condition is met, otherwise continue

ctx.

on

(

'some-check'

,

(

input

)

=>

{

 

if

(

shouldBlock

(

input

)

)

return

'blocked'

 

// Returning null / false / undefined means "I have no opinion", letting later listeners continue

}

)


serial: sequential execution

Listeners execute one by one in registration order, waiting for async results.

The first return value that is not null, false, or undefined terminates subsequent execution.

Example

// Run a phased initialization in order, waiting for each phase to complete

await ctx.

serial

(

'setup-phase'

,

context

)


waterfall: pipeline

Each listener can wrap the downstream return value, forming a processing chain.

A listener receives (...args, next); calling next() executes downstream listeners, and the downstream return value is returned to the current wrapping layer through next().

Example

// Dispatcher: the initial value is input, passed to the first listener

const

output

=

await ctx.

waterfall

(

'my-plugin/transform'

,

input

,

async

(

)

=>

input

)

// Listener: must call next(), then process the downstream result before returning

ctx.

on

(

'my-plugin/transform'

,

async

(

_input

,

next

)

=>

{

 

const

downstream

=

await next

(

)

 

return

downstream.

trim

(

)

}

)

A waterfall listener must call next(). Not calling next short-circuits the whole pipeline; this is intentional design, used to implement interception or gateway logic.

A policy listener, when it has decision authority, can return directly without calling next(), thereby blocking the whole chain.

Listeners that only annotate or observe must delegate downstream.


Type-safe events

Use TypeScript declaration merging to provide type safety for events.

Example

// File path: scratch-plugin/src/events.ts

import

'@deepseek-ai/cordis'

// Declaration merging: register event names and signatures

declare module

'@deepseek-ai/cordis'

{

 

interface

Events

{

   

'my-plugin/ready'

:

(

payload

:

{

id

:

string

}

)

=>

void

   

'my-plugin/check'

:

(

input

:

string

)

=>

boolean

|

undefined

   

'my-plugin/transform'

:

(

input

:

string

,

next

:

(

)

=>

Promise

<

string

)

=>

Promise

<

string

}

}

// From then on ctx.on('my-plugin/ready', ...) and ctx.emit('my-plugin/ready', ...)

// have their parameters inferred automatically; misspelling an event name or parameter type causes a compile-time error


Cordis events and session records

Harness's Cordis events follow namespace/action naming, such as agent/step, agent/request, agent/request-error, tools/result, and session/event.

Note the distinction between two kinds of events:

Events What It Is How to observe
agent/step, tools/result, etc. Cordis events, dispatched in real time Directly ctx.on('tools/result', ...)
turn/、step/、tool/call、tool/result、compaction/* Persisted session event types Listen to session/event and check event.type

turn/*, step/*, tool/call, tool/result, and compaction/* are persisted session event types, not same-named Cordis events.

To observe them, listen to session/event and check event.type.


Hands-on example: a logging plugin

The official docs use the plugin below to log tool calls and tool results; it listens to the Cordis event tools/result.

Example

// File path: scratch-plugin/src/tool-logger.ts

import

type

{

Context

}

from

'@deepseek-ai/cordis'

import

'@deepseek-ai/dsh-tools'

export

const

name

=

'tool-logger'

export

function

apply

(

ctx

:

Context

)

{

 

// Listen to the tools/result event: triggered every time a tool execution completes

  ctx.

on

(

'tools/result'

,

(

exec

,

result

)

=>

{

   

// Print the tool name and parameters

    console.

log

(

`

[

tool

]

$

{

exec.

name

}

(

$

{

JSON.

stringify

(

exec.

arguments

)

}

)

`

)

   

// Concatenate the text blocks in the result content and print only the first 100 characters

   

const

text

=

result.

content

      .

map

(

block

=>

block.

type

===

'text'

?

block.

text

:

''

)

      .

join

(

''

)

    console.

log

(

`

[

tool result

]

$

{

text.

slice

(

0

,

100

)

}

`

)

 

}

)

}

After each tool call by the Agent, this plugin prints the tool name, parameters, and a result summary to the terminal.

The listener is registered via ctx.on and is automatically removed when the plugin is unloaded.


Summary and self-test

The five dispatch modes cover five kinds of contracts: broadcast, decision, in-order, pipeline, and parallel; waterfall must call next(); declare module merging gives type safety to event names and parameters.

Self-test:

  1. Which method triggers each of the five dispatch modes? Which have return values?
  2. What happens if a waterfall listener does not call next()? Is it a bug or a design?
  3. For persisted session events like turn/start, which event should you listen to and which field should you check?