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

DeepSeek Harness Declaring Dependencies: inject and Built-in Services

DeepSeek Harness Declaring Dependencies: inject and Built-in Services

Earlier we learned about plugins that run independently, but most plugins need others' capabilities: the tool registry, LLM, and Agent.

This chapter introduces how to declare dependencies and how the framework ensures apply runs only after dependencies are ready.


What Is a Service

A service is a named capability that one plugin exposes to other plugins.

In Harness, tools, llm, and agents are all services, mounted on ctx: ctx.tools, ctx.llm, ctx.agents.

Built-in Services What It Is Typical usage
ctx.tools Tool Runtime (ToolRuntime) Register / call tools
ctx.llm Large Language Model Service (LLM) Register adapters, initiate model requests
ctx.agents Agent Service (Agent) Manage sub-agents

Declare Dependencies with inject

If your plugin needs a service, put it into the inject array.

The framework ensures these services are ready before loading your plugin.

Example

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

import

type

{

Context

}

from

'@deepseek-ai/cordis'

export

const

name

=

'my-tool-plugin'

// Declare dependency: requires the tools service

export

const

inject

=

[

'tools'

]

export

function

apply

(

ctx

:

Context

)

{

 

// When reaching here, ctx.tools is definitely ready

  ctx.

tools

.

register

(

/* ... */

)

}

When apply executes, all services declared in inject are already ready.

This is the framework's guarantee; you do not need to do any waiting in your code.


Dependency Not Ready: Plugin Waits

If a service is not yet ready, the plugin will not execute.

Its Fiber stays in the PENDING state, waiting for the service to appear.

In the lifecycle, PENDING means 'declared, but required dependencies not ready'.

Load plugins only after dependencies are ready

If the service never comes, the plugin keeps waiting; no error occurs and apply is not executed.


Required and Optional Dependencies

inject declares required dependencies: the plugin does not load when a service is absent.

If a service is optional, use an optional dependency: do not write inject, and query with ctx.get() at the point of use.

Example

// Optional dependency: omit inject and query with ctx.get()

import

type

{

Context

}

from

'@deepseek-ai/cordis'

export

function

apply

(

ctx

:

Context

)

{

 

// The metrics service may or may not be present

 

const

metrics

=

ctx.

get

(

'metrics'

)

 

// Optional chaining: skip if absent, no error

  metrics

?

.

record

(

'plugin_loaded'

,

1

)

}


What Happens When a Service Disappears

If a required service disappears at runtime (for example, its provider is uninstalled), two things happen.

First, plugins depending on it are automatically disposed (resources released).

Second, when the service reappears, the plugin automatically reloads.

This prevents a plugin from calling a service that no longer exists.

Tip: this rule works with automatic cleanup — uninstalling triggers the disposer described in Part 8.


Summary and self-test

Use inject to declare required services; the framework guarantees apply runs only after services are ready; query optional services with ctx.get().

1. What does the inject array declare?

2. What state is a plugin in when a service is not ready?

3. How should an optional dependency be declared?