@agent-assistant/turn-context

中文文档
0.4.37发布于 2026/10/04MITTypeScriptES Module构建来源已验证近 30 天下载 1.6万

Turn-scoped context assembly for Agent Assistant SDK

原始使用文档
文档目录21 节
  1. Purpose
  2. What it owns
  3. What it does NOT own
  4. Core design rule
  5. Installation
  6. Minimal assembly example
  7. Direct harness projection — zero transformation needed
  8. Direct execution adapter projection
  9. Multi-source assembly with memory, enrichment, and guardrai…
  10. Composition rules
  11. Layer order
  12. Identity precedence
  13. Instruction ordering
  14. Enrichment candidate inclusion
  15. Memory candidate inclusion
  16. Non-goals
  17. API reference
  18. createTurnContextAssembler(options?)
  19. assembler.assemble(input: TurnContextInput): Promise<TurnCo…
  20. TurnContextValidationError
  21. toExecutionRequest(assembly, userMessage, overrides?)

@agent-assistant/turn-context

Turn-scoped context assembly for the Agent Assistant SDK.


Purpose

@agent-assistant/turn-context is the runtime primitive that assembles the effective assistant-facing turn context for one bounded turn.

It sits:

  • above @agent-assistant/traits (consumes stable identity defaults)
  • adjacent to memory, policy, and coordination inputs
  • below product-specific prompts and UX logic
  • upstream of @agent-assistant/harness

It answers the question:

Given this assistant, this session, this turn, these memories, and these backstage signals — what should the visible assistant actually receive as its effective character + context bundle for this turn?


What it owns

  • Typed turn-context assembly inputs
  • Typed turn-context assembly outputs
  • Identity-preserving composition rules
  • Provenance-carrying enrichment packaging
  • Harness-projection compatibility (HarnessInstructions + HarnessPreparedContext)

What it does NOT own

  • Model execution or tool execution
  • Session persistence or memory retrieval
  • Policy evaluation or approval logic
  • Specialist orchestration or enrichment discovery
  • Product business rules, prompt phrasing, or domain heuristics

Core design rule

Runtime enrichment informs the assistant's expression for the turn; it does not replace assistant identity.

The assembly output preserves distinct sources:

  • stable identity inputs
  • product-authored overlays
  • runtime enrichment inputs
  • guardrail overlays

Installation

npm install @agent-assistant/turn-context

Minimal assembly example

import { createTurnContextAssembler } from '@agent-assistant/turn-context';

const assembler = createTurnContextAssembler();

const assembly = await assembler.assemble({
  assistantId: 'sage',
  turnId: 'turn-001',
  identity: {
    assistantName: 'Sage',
    baseInstructions: {
      systemPrompt: 'You are Sage, a workspace assistant.',
      developerPrompt: 'Keep answers focused and actionable.',
    },
  },
  shaping: {
    mode: 'review',
    expressionOverrides: { directness: 'high' },
  },
});

Direct harness projection — zero transformation needed

import { createTurnContextAssembler } from '@agent-assistant/turn-context';
import { createHarness } from '@agent-assistant/harness';

const assembler = createTurnContextAssembler();
const harness = createHarness({ model: myModelAdapter });

const assembly = await assembler.assemble(input);

const result = await harness.runTurn({
  assistantId: assembly.assistantId,
  turnId: assembly.turnId,
  sessionId: assembly.sessionId,
  userId: assembly.userId,
  message: incomingMessage,
  instructions: assembly.harnessProjection.instructions,  // direct — no reshaping
  context: assembly.harnessProjection.context,            // direct — no reshaping
  continuation: input.continuation,
});

The harnessProjection output is always a valid HarnessInstructions + HarnessPreparedContext. No adapter or transformation is needed between assemble() and harness.runTurn().

Direct execution adapter projection

Products that execute through an ExecutionAdapter can project the same assembly into the canonical ExecutionRequest shape with toExecutionRequest.

import {
  createTurnContextAssembler,
  toExecutionRequest,
} from '@agent-assistant/turn-context';

const assembler = createTurnContextAssembler();
const assembly = await assembler.assemble(input);

const request = toExecutionRequest(assembly, incomingMessage, {
  tools: readOnlyToolDescriptors,
  requirements: { toolUse: 'allowed', traceDepth: 'standard' },
  metadata: { route: 'founder-chat' },
});

const negotiation = adapter.negotiate(request);
const result = negotiation.supported
  ? await adapter.execute(request)
  : { status: 'unsupported', degradation: negotiation.reasons };

The projection preserves assistant/session/user/thread ids, rendered instructions, context blocks, response style, message metadata, and optional tools/requirements/continuation metadata. Execution adapters stay downstream of turn-context; products still own identity and policy.


Multi-source assembly with memory, enrichment, and guardrails

const assembly = await assembler.assemble({
  assistantId: 'sage',
  turnId: 'turn-001',
  sessionId: 'session-abc',
  userId: 'user-xyz',
  identity: {
    assistantName: 'Sage',
    traits: createTraitsProvider({
      voice: 'concise',
      formality: 'professional',
      proactivity: 'medium',
      riskPosture: 'moderate',
    }),
    baseInstructions: {
      systemPrompt: 'You are Sage, a workspace assistant.',
      developerPrompt: 'Keep answers focused and actionable.',
    },
  },
  shaping: {
    mode: 'review',
    instructionOverlays: [
      { id: 'ov-1', source: 'product', text: 'Emphasize key risks.', priority: 'high' },
    ],
    expressionOverrides: { directness: 'high' },
    responseStyle: { preferMarkdown: true },
  },
  memory: {
    // Pre-retrieved candidates from your memory system
    candidates: [
      { id: 'mem-1', text: 'User prefers bullet points.', scope: 'user', relevance: 0.9 },
    ],
  },
  enrichment: {
    // Pre-packaged backstage enrichment from coordination/connectivity
    candidates: [
      {
        id: 'enr-1',
        kind: 'specialist_memo',
        source: 'reviewer',
        content: 'PR has 2 critical issues.',
        importance: 'high',
      },
    ],
  },
  guardrails: {
    overlays: [
      {
        id: 'gr-1',
        source: 'workspace',
        rule: 'Do not speculate about unverified findings.',
        kind: 'truthfulness_constraint',
        priority: 'high',
      },
    ],
  },
});

// Inspect provenance after assembly
console.log(assembly.provenance.usedMemoryIds);     // ['mem-1']
console.log(assembly.provenance.usedEnrichmentIds); // ['enr-1']
console.log(assembly.provenance.usedGuardrailIds);  // ['gr-1']

// Inspect expression profile
console.log(assembly.expression.directness);        // 'high' (from expressionOverrides)
console.log(assembly.expression.humor);             // capped by guardrail if tone_constraint present

Composition rules

Layer order

  1. Base identity floor — identity.baseInstructions + identity.traits
  2. Turn shaping — shaping.mode, shaping.instructionOverlays, shaping.expressionOverrides
  3. Optional contextual additions — session, memory, enrichment
  4. Guardrail overlays — appended last as constraining overlays, not policy decisions
  5. Harness projection rendering

Identity precedence

  • Base identity is the non-overridable floor.
  • Traits may refine identity defaults but do not replace product-authored base instructions.
  • Turn shaping modulates expression but does not redefine who the assistant is.
  • Enrichment informs context but does not rewrite identity.
  • Guardrails constrain expression but do not become a substitute identity source.

Instruction ordering

System segments: [base-system] (sorted high→low priority)

Developer segments: [base-developer, ...instructionOverlays sorted by priority, mode-segment] + [...guardrailSegments sorted by priority] (guardrails appended to developer prompt in harness projection)

Enrichment candidate inclusion

  • Candidates with audience: 'product' are excluded from assistant-facing context (tracked in droppedEnrichmentIds).
  • Remaining candidates sorted by importance descending.
  • Capped at 8 enrichment blocks per turn.

Memory candidate inclusion

  • Sorted by relevance descending.
  • Capped at 10 memory blocks per turn.

Non-goals

This package is not:

  • A memory engine — it accepts pre-retrieved candidates only
  • A policy engine — guardrails shape expression; they do not evaluate allow/deny
  • A prompt-management CMS or DSL
  • A specialist orchestrator — it accepts pre-packaged enrichment
  • A model router or persona system
  • A product-intelligence layer or domain heuristic system
  • A replacement for product prompt craftsmanship

Products still own:

  • exact prompt text and prompt-stack authoring
  • domain-specific enrichment ranking and selection
  • which backstage agents exist and what their outputs mean
  • business and commercial escalation logic
  • product-specific policy rules and integration credentials

API reference

createTurnContextAssembler(options?)

Returns a TurnContextAssembler.

const assembler = createTurnContextAssembler();

assembler.assemble(input: TurnContextInput): Promise<TurnContextAssembly>

Assembles one turn context. Validates required inputs and throws TurnContextValidationError on failure.

Required inputs:

  • assistantId — non-empty string
  • turnId — non-empty string
  • identity — object with baseInstructions present; at least one of systemPrompt or developerPrompt must be a non-empty string

Optional inputs:

  • shaping — turn-scoped product behavioral shaping
  • session — session continuity signals
  • memory.candidates — pre-retrieved memory artifacts
  • enrichment.candidates — pre-packaged backstage enrichment
  • guardrails.overlays — expression guardrail overlays
  • continuation — prior turn continuation payload

TurnContextValidationError

Thrown when required input constraints are violated.

try {
  await assembler.assemble(input);
} catch (err) {
  if (err instanceof TurnContextValidationError) {
    console.error(err.field, err.reason);
  }
}

toExecutionRequest(assembly, userMessage, overrides?)

Projects a TurnContextAssembly and incoming message into the canonical @agent-assistant/harness ExecutionRequest used by execution adapters.

版本与发布记录

@agent-assistant/turn-context 共发布 65 个版本,首次发布于 2026/04/14;过去 12 个月发布了 65 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.4.37latest2026/10/041 个月前
0.4.362026/10/041 个月前
0.4.352026/05/235 个月前
0.4.332026/05/155 个月前
0.4.322026/05/125 个月前
0.4.312026/05/086 个月前

关于 @agent-assistant/turn-context 的常见问题

@agent-assistant/turn-context 是什么?

Turn-scoped context assembly for Agent Assistant SDK

如何安装 @agent-assistant/turn-context?

在项目目录运行 npm install @agent-assistant/turn-context,或使用 pnpm add / yarn add / bun add。

@agent-assistant/turn-context 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@agent-assistant/turn-context 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@agent-assistant/turn-context 可以商用吗?

@agent-assistant/turn-context 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@agent-assistant/turn-context 的下载量有多少?

根据 npm 官方下载统计,@agent-assistant/turn-context 在 2026/10/05 – 2026/11/03 这 30 天内被下载 15,613 次,日均约 520 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@agent-assistant/turn-context 还在维护吗?

最新版本 0.4.37 发布于 2026/10/04(1 个月前);至今共发布 65 个版本,其中过去 12 个月发布了 65 个;首次发布于 2026/04/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。