Back to home

davidgereb

dsh-lib-context-injection

Context injection library to be used in DeepSeek Harness plugins.

Stars
0
Language
JavaScript
Created
Aug 15, 2026
Updated
Aug 15, 2026

Introduction

dsh-lib-context-injection

Shared agent context-injection helpers for dsh web plugins.

Several dsh web host plugins need to push context into a live agent's session with no browser in the loop, and they used to each implement the same two patterns by hand:

  • Follow-up message injectionagent.followup(createUserMessage(...)): dsh-plugin-cost-lens fires scheduled off-peak sends, releases deferred (cheapass) prompts, and wakes interrupted work at the next off-peak start; dsh-plugin-watchdog wakes a crash-resumed agent with a "continue where you left off" prompt.
  • Model-selection restoration — deriving the session's last durable route (provider / model / reasoning effort) from its request/header + assistant/message events and installing it via installModelSelection, so the persona prompt's {{model}} / {{provider}} variables resolve on headless/resumed agents (which have no browser to install the selection).

This package is the single implementation of both patterns.

Install

Host-only ESM library; it runs inside the dsh server process and imports @deepseek-ai/dsh-llm + @deepseek-ai/dsh-agent (both shipped with dsh web).

This is a dependency package, not a Cordis plugin — there is no loader entry to add to cordis.patch.yml, and dsh plugin add will not make anything load it. If you use one of the consuming plugins (cost-lens, watchdog), it is installed automatically as their git dependency — you don't need to install it yourself.

From GitHub (add as a dependency of your own host code)

pnpm ≥ 11 note. pnpm 11 blocks git-hosted transitive dependencies (blockExoticSubdeps defaults to true). If you install this into a dsh profile (or any pnpm workspace) as a direct dependency, pass the flag below; as a transitive dependency of dsh-plugin-cost-lens / dsh-plugin-watchdog, their install commands already include it.

npm install --save github:davidgereb/dsh-lib-context-injection --config.block-exotic-subdeps=false
# or with pnpm:
pnpm add github:davidgereb/dsh-lib-context-injection --config.block-exotic-subdeps=false

From a local checkout

npm install /path/to/dsh-lib-context-injection
# or with pnpm:
pnpm add /path/to/dsh-lib-context-injection

API

ExportPurpose
deriveModelSelection(events, opts?)Fold session events into the last durable { provider, model, reasoningEffort? } route. opts.fallbackProvider / opts.fallbackModel cover sessions with no recorded route; opts.includeReasoningEffort also carries the effort from request/header config.
installModelSelectionOn(target, selection, opts?)Safe wrapper around @deepseek-ai/dsh-agent's installModelSelection — never throws; logs via opts.logger.warn (tagged with opts.plugin) and returns false on failure. target is an agent (uses agent.ctx) or a raw agent-scoped context.
ensureModelSelection(target, events, opts?)deriveModelSelection + installModelSelectionOn with two guards: per-context dedupe (WeakSet — one install per context per process) and an optional "route already carried" skip (opts.skipIfRouted, default true): when the agent's options.model already carries a route, the {{model}} variable resolves on its own, so nothing is installed.
injectUserMessage(agent, message)agent.followup(createUserMessage(...)). message.text becomes a text block (or pass full message.content); message.plugin becomes the default { kind: "plugin", plugin } source (or pass message.source explicitly). Returns the created message.

Usage

import { deriveModelSelection, injectUserMessage, ensureModelSelection } from "dsh-lib-context-injection";

// wake an agent with a plugin-tagged follow-up
injectUserMessage(agent, { text: "Continue where you left off.", plugin: "my-plugin" });

// restore the session's model route on a headless/resumed agent
ensureModelSelection(agent, agent.session?.events ?? [], {
  fallbackProvider: "deepseek",
  fallbackModel: "deepseek-v4-flash",
  logger: ctx.logger,
  plugin: "my-plugin"
});

Cordis scoped-context note. The resume setup(agentCtx) callback passes a Cordis scoped-context proxy; reading a non-injected property on it (e.g. agentCtx.ctx) throws cannot get property "ctx" without inject. installModelSelectionOn/ensureModelSelection handle this: they probe target.ctx in a guarded try/catch and fall back to using the raw context itself — the same as the pre-library callers did.

Layout / build

dsh-lib-context-injection/
├── index.js              # the library (host-only)
├── package.json          # dsh-lib-context-injection
└── scripts/
    └── smoke-test.mjs    # derive/install/inject unit checks

No build step — index.js is the package.

Testing

node scripts/smoke-test.mjs

Consumers

Compatibility. Tested against dsh 0.1.0-rc.6 on Node.js v24.19.0 (dsh web profile). Older or newer dsh releases may change the internals this plugin hooks into — check the changelog before upgrading.


⚠️ AI-generated, provided as-is. This project was written with the assistance of an AI. It is provided AS IS without warranty of any kind, express or implied. The author cannot be held responsible for any damage, data loss, or misbehaviour that results from using it. Use at your own risk.