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

DeepSeek Harness Services and Dependencies: Service Base Class and Type Declarations

DeepSeek Harness Services and Dependencies: Service Base Class and Type Declarations

In the previous chapters we've been using built-in services such as ctx.tools, ctx.llm, and ctx.agents.

In this chapter we start from the service provider's perspective to see how to use the Service base class to provide a named service ourselves and give consumers complete type hints.


What Is a Service

In Harness, tools, llm, and agents are all services.

A service is a named capability mounted on ctx; any plugin can provide a service for other plugins to use.

Service: a capability that one plugin exposes to other plugins.

It occupies a stable ctx.(such as ctx.tools, ctx.llm, ctx.sessions), other plugins look up services by key rather than importing concrete implementations.

For example, ctx.tools is the tool runtime service, ctx.llm is the large model service, and ctx.agents is the agent service.

The service names, public methods, and source locations of built-in services are auto-generated by the repository into each service's subsystem page. When developing plugins, rely on these generated blocks and the services' TypeScript interfaces rather than any hand-written static service list.


Using a service: inject declaration

To use an existing service, declare inject in your plugin.

The framework guarantees that when apply runs, all services declared via inject are ready.

Example

// File path: scratch-plugin/src/use-tools.ts

// Declare dependency on the tools service

export

const

inject

=

[

'tools'

]

export

function

apply

(

ctx

:

Context

)

{

 

// ctx.tools is guaranteed to exist and be ready

  ctx.

tools

.

register

(

/* ... */

)

}

If a service isn't ready yet, your plugin will wait and won't run apply.


Providing a service: the Service base class

Derive a subclass from the Service base class and call super(ctx, 'service name') in the constructor to register a named service.

Example

// File path: scratch-plugin/src/metrics-service.ts

import

{

Service

,

type Context

}

from

'@deepseek-ai/cordis'

// Services may also depend on other services, declared with static inject

export

default

class

MetricsService

extends

Service

{

 

static

inject

=

[

'llm'

]

 

// This service requires the llm service to be ready first

  constructor

(

ctx

:

Context

)

{

   

// The second argument 'metrics' is the service name, mounted at ctx.metrics

   

super

(

ctx

,

'metrics'

)

 

}

 

// A service method exposed to the public

  record

(

event

:

string

,

value

:

number

)

{

   

// Record a metric here, e.g. the number of tool calls

   

// A real implementation would write to a metrics backend; omitted here

 

}

}

After loading this plugin, consumers can access it via ctx.metrics.

Example

// File path: scratch-plugin/src/use-metrics.ts

// Consumer declares dependency on the metrics service

export

const

inject

=

[

'metrics'

]

export

function

apply

(

ctx

:

Context

)

{

 

// Call the service method: record a tool call

  ctx.

metrics

.

record

(

'runoob_tool_call'

,

1

)

}


Type declarations: declare module merging

Use TypeScript declaration merging to give ctx.metrics the correct type.

This way you get autocomplete while coding, and the compiler can catch misspelled service names.

Example

// File path: scratch-plugin/src/metrics-service.ts

import

{

Service

,

type Context

}

from

'@deepseek-ai/cordis'

// Declaration merging: tell TypeScript that Context has a metrics field

declare module

'@deepseek-ai/cordis'

{

 

interface

Context

{

    metrics

:

MetricsService

 

}

}

export

default

class

MetricsService

extends

Service

{

  constructor

(

ctx

:

Context

)

{

   

super

(

ctx

,

'metrics'

)

 

}

  record

(

event

:

string

,

value

:

number

)

{

   

/* implementation omitted */

 

}

}

declare module merging is TypeScript's standard way to add types to existing modules.

Here the type and implementation of ctx.metrics are written in the same file to keep them from drifting apart.


Required and Optional Dependencies

Service dependencies come in two kinds: required and optional.

Required dependencies are declared with inject; if the service is absent, the plugin won't load at all.

Optional dependencies omit inject and are queried with ctx.get() at the point of use, possibly returning undefined.

Example

// File path: scratch-plugin/src/optional-dep.ts

// Required dependency: if the service is absent, the plugin won't load

export

const

inject

=

[

'tools'

]

// Optional dependency: omit inject, query with ctx.get() at the point of use

export

function

apply

(

ctx

:

Context

)

{

 

// ctx.get('metrics') may return undefined; call safely with optional chaining

 

const

metrics

=

ctx.

get

(

'metrics'

)

  metrics

?

.

record

(

'runoob_plugin_loaded'

,

1

)

}

Dependency type Syntax Behavior when the service is absent Applicable scenarios
Required dependency export const inject = ['tools'] Plugin doesn't load, stays PENDING Can't work properly without the service
Optional dependency Omit inject, query with ctx.get('metrics') Plugin loads normally, ctx.get() returns undefined Service is optional; must work properly even when absent

When to use an optional dependency? When the service is optional and your plugin must work properly even without it.


Behavior when a service disappears

If a required service disappears while the app is running (e.g. its provider is uninstalled), two things happen:

  1. Plugins depending on it are automatically disposed (releasing resources).
  2. When the service reappears, the plugin automatically reloads.

This prevents plugins from calling services that no longer exist.


Summary and self-test

A Service subclass provides a named service via super(ctx, 'name'), declare module merging completes the types, inject declares required dependencies, and ctx.get() queries optional dependencies.

Self-test:

  1. In what form does a service exist on ctx? Which argument determines the service name?
  2. How do you give ctx.metrics type hints?
  3. How do required and optional dependencies differ in code?