@contractspec/lib.ai-agent 7.0.6 → 7.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (114) hide show
  1. package/README.md +71 -130
  2. package/dist/agent/agent-factory.d.ts +8 -8
  3. package/dist/agent/agent-factory.js +1360 -1358
  4. package/dist/agent/contract-spec-agent.d.ts +8 -8
  5. package/dist/agent/contract-spec-agent.js +1360 -1358
  6. package/dist/agent/index.js +1621 -1619
  7. package/dist/agent/json-runner.d.ts +2 -2
  8. package/dist/agent/json-runner.js +1360 -1358
  9. package/dist/agent/unified-agent.d.ts +3 -3
  10. package/dist/agent/unified-agent.js +1621 -1619
  11. package/dist/approval/index.d.ts +1 -1
  12. package/dist/approval/index.js +618 -618
  13. package/dist/approval/workflow.js +618 -618
  14. package/dist/exporters/claude-agent-exporter.d.ts +1 -1
  15. package/dist/exporters/claude-agent-exporter.js +619 -619
  16. package/dist/exporters/index.d.ts +2 -2
  17. package/dist/exporters/index.js +626 -625
  18. package/dist/exporters/opencode-exporter.js +625 -625
  19. package/dist/exporters/types.d.ts +1 -1
  20. package/dist/i18n/catalogs/index.d.ts +1 -1
  21. package/dist/i18n/catalogs/index.js +587 -587
  22. package/dist/i18n/index.d.ts +7 -7
  23. package/dist/i18n/index.js +618 -618
  24. package/dist/i18n/locale.d.ts +1 -1
  25. package/dist/i18n/locale.js +2 -2
  26. package/dist/i18n/messages.js +587 -587
  27. package/dist/index.js +619 -619
  28. package/dist/interop/index.d.ts +2 -2
  29. package/dist/interop/index.js +619 -619
  30. package/dist/interop/spec-consumer.d.ts +1 -1
  31. package/dist/interop/spec-consumer.js +619 -619
  32. package/dist/interop/tool-consumer.d.ts +2 -2
  33. package/dist/interop/tool-consumer.js +619 -619
  34. package/dist/knowledge/index.js +618 -618
  35. package/dist/knowledge/injector.js +618 -618
  36. package/dist/memory/in-memory.d.ts +1 -1
  37. package/dist/memory/index.d.ts +1 -1
  38. package/dist/node/agent/agent-factory.js +1360 -1358
  39. package/dist/node/agent/contract-spec-agent.js +1360 -1358
  40. package/dist/node/agent/index.js +1621 -1619
  41. package/dist/node/agent/json-runner.js +1360 -1358
  42. package/dist/node/agent/unified-agent.js +1621 -1619
  43. package/dist/node/approval/index.js +618 -618
  44. package/dist/node/approval/workflow.js +618 -618
  45. package/dist/node/exporters/claude-agent-exporter.js +619 -619
  46. package/dist/node/exporters/index.js +626 -625
  47. package/dist/node/exporters/opencode-exporter.js +625 -625
  48. package/dist/node/i18n/catalogs/index.js +587 -587
  49. package/dist/node/i18n/index.js +618 -618
  50. package/dist/node/i18n/locale.js +2 -2
  51. package/dist/node/i18n/messages.js +587 -587
  52. package/dist/node/index.js +619 -619
  53. package/dist/node/interop/index.js +619 -619
  54. package/dist/node/interop/spec-consumer.js +619 -619
  55. package/dist/node/interop/tool-consumer.js +619 -619
  56. package/dist/node/knowledge/index.js +618 -618
  57. package/dist/node/knowledge/injector.js +618 -618
  58. package/dist/node/providers/claude-agent-sdk/adapter.js +799 -799
  59. package/dist/node/providers/claude-agent-sdk/index.js +799 -799
  60. package/dist/node/providers/claude-agent-sdk/tool-bridge.js +618 -618
  61. package/dist/node/providers/index.js +795 -795
  62. package/dist/node/providers/opencode-sdk/adapter.js +671 -671
  63. package/dist/node/providers/opencode-sdk/agent-bridge.js +619 -619
  64. package/dist/node/providers/opencode-sdk/index.js +673 -673
  65. package/dist/node/providers/opencode-sdk/tool-bridge.js +618 -618
  66. package/dist/node/providers/registry.js +619 -619
  67. package/dist/node/providers/types.js +618 -618
  68. package/dist/node/spec/index.js +619 -619
  69. package/dist/node/spec/registry.js +619 -619
  70. package/dist/node/spec/spec.js +618 -618
  71. package/dist/node/telemetry/index.js +622 -620
  72. package/dist/node/telemetry/posthog.js +622 -620
  73. package/dist/node/tools/index.js +902 -902
  74. package/dist/node/tools/knowledge-tool.js +618 -618
  75. package/dist/node/tools/mcp-server.js +619 -619
  76. package/dist/node/tools/tool-adapter.js +619 -619
  77. package/dist/providers/claude-agent-sdk/adapter.d.ts +1 -1
  78. package/dist/providers/claude-agent-sdk/adapter.js +799 -799
  79. package/dist/providers/claude-agent-sdk/index.d.ts +1 -1
  80. package/dist/providers/claude-agent-sdk/index.js +799 -799
  81. package/dist/providers/claude-agent-sdk/session-bridge.d.ts +1 -2
  82. package/dist/providers/claude-agent-sdk/tool-bridge.d.ts +1 -1
  83. package/dist/providers/claude-agent-sdk/tool-bridge.js +618 -618
  84. package/dist/providers/index.d.ts +1 -1
  85. package/dist/providers/index.js +795 -795
  86. package/dist/providers/opencode-sdk/adapter.d.ts +1 -1
  87. package/dist/providers/opencode-sdk/adapter.js +671 -671
  88. package/dist/providers/opencode-sdk/agent-bridge.js +619 -619
  89. package/dist/providers/opencode-sdk/index.d.ts +1 -1
  90. package/dist/providers/opencode-sdk/index.js +673 -673
  91. package/dist/providers/opencode-sdk/tool-bridge.d.ts +1 -1
  92. package/dist/providers/opencode-sdk/tool-bridge.js +618 -618
  93. package/dist/providers/registry.js +619 -619
  94. package/dist/providers/types.d.ts +1 -1
  95. package/dist/providers/types.js +618 -618
  96. package/dist/spec/index.d.ts +1 -1
  97. package/dist/spec/index.js +619 -619
  98. package/dist/spec/registry.d.ts +1 -1
  99. package/dist/spec/registry.js +619 -619
  100. package/dist/spec/spec.d.ts +1 -1
  101. package/dist/spec/spec.js +618 -618
  102. package/dist/telemetry/index.d.ts +1 -1
  103. package/dist/telemetry/index.js +622 -620
  104. package/dist/telemetry/posthog.d.ts +1 -1
  105. package/dist/telemetry/posthog.js +622 -620
  106. package/dist/tools/index.d.ts +5 -5
  107. package/dist/tools/index.js +902 -902
  108. package/dist/tools/knowledge-tool.d.ts +1 -1
  109. package/dist/tools/knowledge-tool.js +618 -618
  110. package/dist/tools/mcp-server.d.ts +1 -1
  111. package/dist/tools/mcp-server.js +619 -619
  112. package/dist/tools/tool-adapter.d.ts +2 -2
  113. package/dist/tools/tool-adapter.js +619 -619
  114. package/package.json +7 -7
package/README.md CHANGED
@@ -1,137 +1,78 @@
1
1
  # @contractspec/lib.ai-agent
2
2
 
3
- [![npm version](https://img.shields.io/npm/v/@contractspec/lib.ai-agent)](https://www.npmjs.com/package/@contractspec/lib.ai-agent)
4
- [![npm downloads](https://img.shields.io/npm/dt/@contractspec/lib.ai-agent)](https://www.npmjs.com/package/@contractspec/lib.ai-agent)
5
- [![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/lssm-tech/contractspec)
3
+ Website: https://contractspec.io
6
4
 
7
- Website: https://contractspec.io/
5
+ **Core AI agent runtime for ContractSpec with tool orchestration, MCP integration, session state, memory, and telemetry.**
8
6
 
9
- **AI governance for ContractSpec** — Constrain what AI agents can change, enforce contracts they must respect.
7
+ ## What It Provides
10
8
 
11
- Stateful AI agent orchestration with type-safe specs, tool execution, knowledge bindings, and per-tenant guardrails. Agents read contracts as their source of truth, enabling triage, growth experiments, and DevOps automation with contract-enforced safety.
9
+ - Provides the central agent runtime used by chat, automation, and higher-level orchestration packages.
10
+ - Supports tools, sessions, memory, approvals, providers, telemetry, and MCP-aware workflows.
11
+ - Acts as the stable public API for agent execution across multiple runtimes and delivery surfaces.
12
+ - `src/providers/` contains provider integrations and provider-facing adapters.
12
13
 
13
- ## Features
14
+ ## Installation
14
15
 
15
- - Type-safe `AgentSpec` + registry for declarative agent definitions
16
- - Tool loop orchestration with runtime `maxSteps` overrides and confidence-aware escalation hooks
17
- - Memory framework that mixes working memory with long-term persistence hooks
18
- - Tool registry/executor with structured input validation, timeout, and cooldown controls
19
- - Approval workflow helpers for human-in-the-loop gates (see `/approval`)
20
- - MCP client support for `stdio`, `sse`, and `http` transports
21
- - Adapter-first runtime interop surfaces for LangGraph, LangChain, and workflow-devkit style integrations
22
-
23
- ## Runtime adapter surfaces
24
-
25
- `AgentSpec.runtime` and `interop/runtime-adapters` expose optional ports for:
26
-
27
- - checkpoint storage
28
- - suspend/resume orchestration
29
- - retry classification
30
- - approval gateways
31
-
32
- These ports are optional by design and keep external runtime dependencies decoupled from core agent contracts.
33
-
34
- ## Operations as Tools
35
-
36
- **Preferred pattern:** One contract multiple surfaces. Reference a ContractSpec operation in your agent; the tool handler and input schema are derived automatically.
37
-
38
- ```ts
39
- import { defineAgent } from '@contractspec/lib.ai-agent';
40
- import { defineQuery } from '@contractspec/lib.contracts-spec';
41
- import { OperationSpecRegistry } from '@contractspec/lib.contracts-spec';
42
- import { createAgentFactory } from '@contractspec/lib.ai-agent/agent/agent-factory';
43
-
44
- // 1. Define operation (existing pattern)
45
- const SearchKnowledgeQuery = defineQuery({
46
- meta: { key: 'knowledge.search', version: '1.0.0', owners: ['@platform'], goal: '...', context: '...' },
47
- io: { input: SearchInputModel, output: SearchOutputModel },
48
- });
49
-
50
- // 2. Register and bind handler
51
- const ops = new OperationSpecRegistry().register(SearchKnowledgeQuery).bind(SearchKnowledgeQuery, handler);
52
-
53
- // 3. Reference in agent
54
- const agent = defineAgent({
55
- meta: { key: 'support.bot', version: '1.0.0', owners: ['@platform'] },
56
- instructions: 'Resolve support tickets.',
57
- tools: [
58
- { name: 'search_knowledge', operationRef: { key: 'knowledge.search', version: '1.0.0' }, automationSafe: true },
59
- ],
60
- });
61
-
62
- // 4. Create factory with operationRegistry
63
- const factory = createAgentFactory({
64
- defaultModel,
65
- registry: agentRegistry,
66
- toolHandlers: new Map(),
67
- operationRegistry: ops,
68
- });
69
- ```
70
-
71
- **Output rendering:** When tool output should render via PresentationSpec, FormSpec, or DataViewSpec, add `outputPresentation`, `outputForm`, or `outputDataView` to `AgentToolConfig` (at most one per tool). The tool adapter wraps raw output as `{ presentationKey, data }`, `{ formKey, defaultValues }`, or `{ dataViewKey, items }` for `ToolResultRenderer`. OperationSpec can also declare these refs; when the tool has no output refs, the operation's refs are used as fallback.
72
-
73
- **Fallback (inline tools):** When the tool is not an operation (LLM subcalls, external APIs), use inline `AgentToolConfig` with `schema` and a manual handler in `toolHandlers`.
74
-
75
- ## Bundle spec / surface-runtime integration
76
-
77
- When building planner agents for `@contractspec/lib.surface-runtime`, planner tools (e.g. `propose-patch`) from `@contractspec/lib.surface-runtime/runtime/planner-tools` map to `AgentToolConfig`. Wire `proposePatchToolConfig` into `AgentSpec.tools`; the handler should validate via `validatePatchProposal` and return `SurfacePatchProposal`. Tools from surface-runtime planner-tools are `AgentToolConfig`-compatible.
78
-
79
- ## Quickstart
80
-
81
- ```ts
82
- import { AgentRegistry, AgentRunner, defineAgent } from '@contractspec/lib.ai-agent';
83
- import { ToolExecutor } from '@contractspec/lib.ai-agent/tools';
84
- import { InMemoryAgentMemory } from '@contractspec/lib.ai-agent/memory';
85
-
86
- const SupportAgent = defineAgent({
87
- meta: { name: 'support.bot', version: '1.0.0', owners: ['team-support'], domain: 'operations' },
88
- instructions: 'Resolve support tickets. Escalate whenever confidence < 0.75.',
89
- tools: [{ name: 'search_knowledge', description: 'Query support corpus', schema: { type: 'object', properties: { question: { type: 'string' } }, required: ['question'] } }],
90
- policy: { confidence: { min: 0.75 }, escalation: { auto: true } },
91
- });
92
-
93
- const registry = new AgentRegistry().register(SupportAgent);
94
- const runner = new AgentRunner({
95
- registry,
96
- llm: mistralProvider,
97
- toolExecutor: new ToolExecutor({ tools: [searchKnowledgeTool] }),
98
- memoryManager: new InMemoryAgentMemory(),
99
- });
100
-
101
- const result = await runner.run({ agent: 'support.bot', input: 'My payout failed.', tenantId: 'acme' });
102
- if (result.requiresEscalation) notifyHuman(result);
103
- ```
104
-
105
- See `examples/ai-support-bot` for a full workflow including ticket ingestion and approval queues.
106
-
107
- ## MCP Client Tooling
108
-
109
- ```ts
110
- import { createAgentFactory } from '@contractspec/lib.ai-agent/agent/agent-factory';
111
-
112
- const factory = createAgentFactory({
113
- defaultModel,
114
- registry,
115
- toolHandlers,
116
- mcpServers: [
117
- {
118
- name: 'filesystem',
119
- command: 'npx',
120
- args: ['-y', '@modelcontextprotocol/server-filesystem', '/workspace'],
121
- toolPrefix: 'fs',
122
- },
123
- {
124
- name: 'posthog',
125
- transport: 'http',
126
- url: process.env.POSTHOG_MCP_URL!,
127
- accessTokenEnvVar: 'POSTHOG_MCP_TOKEN',
128
- toolPrefix: 'ph',
129
- },
130
- ],
131
- });
132
-
133
- const agent = await factory.create('support.bot');
134
- const result = await agent.generate({ prompt: 'Summarize today\'s incidents.' });
135
-
136
- await agent.cleanup();
137
- ```
16
+ `npm install @contractspec/lib.ai-agent`
17
+
18
+ or
19
+
20
+ `bun add @contractspec/lib.ai-agent`
21
+
22
+ ## Usage
23
+
24
+ Import the root entrypoint from `@contractspec/lib.ai-agent`, or choose a documented subpath when you only need one part of the package surface.
25
+
26
+ ## Architecture
27
+
28
+ - `src/agent/`, `src/session/`, and `src/memory/` contain the runtime core for execution state and persistence hooks.
29
+ - `src/tools/`, `src.providers/`, and `src.interop/` connect providers, tools, and MCP-aware runtime surfaces.
30
+ - `src.telemetry/`, `src.approval/`, `src.knowledge/`, `src.schema/`, and `src.spec/` round out agent policy and observability surfaces.
31
+ - `src/index.ts` is the root public barrel and package entrypoint.
32
+ - `src/types.ts` is shared public type definitions.
33
+
34
+ ## Public Entry Points
35
+
36
+ - Large multi-subpath library exporting agent runtime, approval, memory, knowledge, providers, schema, telemetry, tools, and types.
37
+ - Export `.` resolves through `./src/index.ts`.
38
+ - Export `./agent` resolves through `./src/agent/index.ts`.
39
+ - Export `./agent/agent-factory` resolves through `./src/agent/agent-factory.ts`.
40
+ - Export `./agent/contract-spec-agent` resolves through `./src/agent/contract-spec-agent.ts`.
41
+ - Export `./agent/json-runner` resolves through `./src/agent/json-runner.ts`.
42
+ - Export `./agent/unified-agent` resolves through `./src/agent/unified-agent.ts`.
43
+ - Export `./approval` resolves through `./src/approval/index.ts`.
44
+ - Export `./approval/workflow` resolves through `./src/approval/workflow.ts`.
45
+ - Export `./exporters` resolves through `./src/exporters/index.ts`.
46
+ - Export `./exporters/claude-agent-exporter` resolves through `./src/exporters/claude-agent-exporter.ts`.
47
+ - The package publishes 66 total export subpaths; keep docs aligned with `package.json`.
48
+
49
+ ## Local Commands
50
+
51
+ - `bun run dev` contractspec-bun-build dev
52
+ - `bun run build` bun run prebuild && bun run build:bundle && bun run build:types
53
+ - `bun run test` — bun test
54
+ - `bun run lint` — bun lint:fix
55
+ - `bun run lint:check` — biome check .
56
+ - `bun run lint:fix` biome check --write --unsafe --only=nursery/useSortedClasses . && biome check --write .
57
+ - `bun run typecheck` — tsc --noEmit
58
+ - `bun run publish:pkg` — bun publish --tolerate-republish --ignore-scripts --verbose
59
+ - `bun run publish:pkg:canary` bun publish:pkg --tag canary
60
+ - `bun run clean` — rimraf dist .turbo
61
+ - `bun run build:bundle` — contractspec-bun-build transpile
62
+ - `bun run build:types` — contractspec-bun-build types
63
+ - `bun run prebuild` contractspec-bun-build prebuild
64
+
65
+ ## Recent Updates
66
+
67
+ - Missing dependencies (thanks to knip).
68
+ - Replace eslint+prettier by biomejs to optimize speed.
69
+ - Agentic workflows — subagents, memory tools, and next steps.
70
+ - Vnext ai-native.
71
+ - Backend operations + frontend rendering support.
72
+ - Add latest models and align defaults.
73
+
74
+ ## Notes
75
+
76
+ - High blast radius used by multiple bundles and libs.
77
+ - `AgentSpec` interface is a public contract; do not change its shape without a migration plan.
78
+ - MCP transport adapters must stay runtime-agnostic (no Node/browser-specific globals).
@@ -1,16 +1,16 @@
1
- import type { LanguageModel, Tool } from 'ai';
2
- import type { KnowledgeRetriever } from '@contractspec/lib.knowledge/retriever';
3
1
  import type { ModelSelector } from '@contractspec/lib.ai-providers/selector-types';
4
2
  import type { OperationSpecRegistry } from '@contractspec/lib.contracts-spec/operations/registry';
5
- import type { AgentSpec } from '../spec/spec';
6
- import type { AgentRegistry } from '../spec/registry';
7
- import type { ToolHandler } from '../types';
8
- import type { McpClientConfig } from '../tools/mcp-client';
9
- import type { SubagentRegistry } from '../tools/tool-adapter';
10
- import type { AgentMemoryStore } from '../tools/agent-memory-store';
3
+ import type { KnowledgeRetriever } from '@contractspec/lib.knowledge/retriever';
4
+ import type { LanguageModel, Tool } from 'ai';
11
5
  import type { AgentSessionStore } from '../session/store';
6
+ import type { AgentRegistry } from '../spec/registry';
7
+ import type { AgentSpec } from '../spec/spec';
12
8
  import type { TelemetryCollector } from '../telemetry/adapter';
13
9
  import type { PostHogLLMConfig, PostHogTracingOptions } from '../telemetry/posthog-types';
10
+ import type { AgentMemoryStore } from '../tools/agent-memory-store';
11
+ import type { McpClientConfig } from '../tools/mcp-client';
12
+ import type { SubagentRegistry } from '../tools/tool-adapter';
13
+ import type { ToolHandler } from '../types';
14
14
  import { ContractSpecAgent } from './contract-spec-agent';
15
15
  /**
16
16
  * Factory configuration for creating agents.