@contractspec/lib.ai-agent 1.44.1 → 1.45.0
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.
- package/README.md +1 -1
- package/dist/agent/agent-factory.d.ts +2 -2
- package/dist/agent/agent-factory.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/spec/registry.d.ts +5 -36
- package/dist/spec/registry.d.ts.map +1 -1
- package/dist/spec/registry.js +8 -60
- package/dist/spec/registry.js.map +1 -1
- package/dist/telemetry/adapter.d.ts +1 -1
- package/dist/telemetry/adapter.js +3 -3
- package/dist/telemetry/adapter.js.map +1 -1
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -28,7 +28,7 @@ import { ToolExecutor } from '@contractspec/lib.ai-agent/tools';
|
|
|
28
28
|
import { InMemoryAgentMemory } from '@contractspec/lib.ai-agent/memory';
|
|
29
29
|
|
|
30
30
|
const SupportAgent = defineAgent({
|
|
31
|
-
meta: { name: 'support.bot', version: 1, owners: ['team-support'], domain: 'operations' },
|
|
31
|
+
meta: { name: 'support.bot', version: '1.0.0', owners: ['team-support'], domain: 'operations' },
|
|
32
32
|
instructions: 'Resolve support tickets. Escalate whenever confidence < 0.75.',
|
|
33
33
|
tools: [{ name: 'search_knowledge', description: 'Query support corpus', schema: { type: 'object', properties: { question: { type: 'string' } }, required: ['question'] } }],
|
|
34
34
|
policy: { confidence: { min: 0.75 }, escalation: { auto: true } },
|
|
@@ -69,7 +69,7 @@ declare class AgentFactory {
|
|
|
69
69
|
* @param version - Optional specific version
|
|
70
70
|
* @param options - Optional creation options
|
|
71
71
|
*/
|
|
72
|
-
create(name: string, version?:
|
|
72
|
+
create(name: string, version?: string, options?: CreateAgentOptions): Promise<ContractSpecAgent>;
|
|
73
73
|
/**
|
|
74
74
|
* Create an agent from a spec directly.
|
|
75
75
|
*
|
|
@@ -85,7 +85,7 @@ declare class AgentFactory {
|
|
|
85
85
|
* @param name - Agent name
|
|
86
86
|
* @param version - Optional specific version
|
|
87
87
|
*/
|
|
88
|
-
getOrCreate(name: string, version?:
|
|
88
|
+
getOrCreate(name: string, version?: string): Promise<ContractSpecAgent>;
|
|
89
89
|
/**
|
|
90
90
|
* Clear the agent cache.
|
|
91
91
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agent-factory.js","names":[],"sources":["../../src/agent/agent-factory.ts"],"sourcesContent":["import type { LanguageModel, Tool } from 'ai';\nimport type { KnowledgeRetriever } from '@contractspec/lib.knowledge/retriever';\nimport type { AgentSpec } from '../spec/spec';\nimport type { AgentRegistry } from '../spec/registry';\nimport type { ToolHandler } from '../types';\nimport type { AgentSessionStore } from '../session/store';\nimport type { TelemetryCollector } from '../telemetry/adapter';\nimport { ContractSpecAgent } from './contract-spec-agent';\n\n/**\n * Factory configuration for creating agents.\n */\nexport interface AgentFactoryConfig {\n /** Default language model to use */\n defaultModel: LanguageModel;\n /** Agent registry for looking up specs */\n registry: AgentRegistry;\n /** Global tool handlers map */\n toolHandlers: Map<string, ToolHandler>;\n /** Optional knowledge retriever */\n knowledgeRetriever?: KnowledgeRetriever;\n /** Optional session store */\n sessionStore?: AgentSessionStore;\n /** Optional telemetry collector */\n telemetryCollector?: TelemetryCollector;\n /** Additional tools to provide to all agents */\n additionalTools?: Record<string, Tool<unknown, unknown>>;\n}\n\n/**\n * Options for creating an agent instance.\n */\nexport interface CreateAgentOptions {\n /** Override the default model */\n model?: LanguageModel;\n /** Additional tool handlers specific to this instance */\n toolHandlers?: Map<string, ToolHandler>;\n /** Additional tools for this instance */\n additionalTools?: Record<string, Tool<unknown, unknown>>;\n}\n\n/**\n * Factory for creating ContractSpec agents from specs.\n *\n * Provides a centralized way to instantiate agents with\n * consistent configuration.\n *\n * @example\n * ```typescript\n * const factory = createAgentFactory({\n * defaultModel: mistral('mistral-large-latest'),\n * registry: agentRegistry,\n * toolHandlers: globalToolHandlers,\n * sessionStore: mySessionStore,\n * });\n *\n * const agent = await factory.create('support.bot');\n * const result = await agent.generate({ prompt: 'Help me with...' });\n * ```\n */\nexport class AgentFactory {\n private readonly config: AgentFactoryConfig;\n private readonly cache = new Map<string, ContractSpecAgent>();\n\n constructor(config: AgentFactoryConfig) {\n this.config = config;\n }\n\n /**\n * Create an agent by name.\n *\n * @param name - Agent name (e.g., \"support.bot\")\n * @param version - Optional specific version\n * @param options - Optional creation options\n */\n async create(\n name: string,\n version?:
|
|
1
|
+
{"version":3,"file":"agent-factory.js","names":[],"sources":["../../src/agent/agent-factory.ts"],"sourcesContent":["import type { LanguageModel, Tool } from 'ai';\nimport type { KnowledgeRetriever } from '@contractspec/lib.knowledge/retriever';\nimport type { AgentSpec } from '../spec/spec';\nimport type { AgentRegistry } from '../spec/registry';\nimport type { ToolHandler } from '../types';\nimport type { AgentSessionStore } from '../session/store';\nimport type { TelemetryCollector } from '../telemetry/adapter';\nimport { ContractSpecAgent } from './contract-spec-agent';\n\n/**\n * Factory configuration for creating agents.\n */\nexport interface AgentFactoryConfig {\n /** Default language model to use */\n defaultModel: LanguageModel;\n /** Agent registry for looking up specs */\n registry: AgentRegistry;\n /** Global tool handlers map */\n toolHandlers: Map<string, ToolHandler>;\n /** Optional knowledge retriever */\n knowledgeRetriever?: KnowledgeRetriever;\n /** Optional session store */\n sessionStore?: AgentSessionStore;\n /** Optional telemetry collector */\n telemetryCollector?: TelemetryCollector;\n /** Additional tools to provide to all agents */\n additionalTools?: Record<string, Tool<unknown, unknown>>;\n}\n\n/**\n * Options for creating an agent instance.\n */\nexport interface CreateAgentOptions {\n /** Override the default model */\n model?: LanguageModel;\n /** Additional tool handlers specific to this instance */\n toolHandlers?: Map<string, ToolHandler>;\n /** Additional tools for this instance */\n additionalTools?: Record<string, Tool<unknown, unknown>>;\n}\n\n/**\n * Factory for creating ContractSpec agents from specs.\n *\n * Provides a centralized way to instantiate agents with\n * consistent configuration.\n *\n * @example\n * ```typescript\n * const factory = createAgentFactory({\n * defaultModel: mistral('mistral-large-latest'),\n * registry: agentRegistry,\n * toolHandlers: globalToolHandlers,\n * sessionStore: mySessionStore,\n * });\n *\n * const agent = await factory.create('support.bot');\n * const result = await agent.generate({ prompt: 'Help me with...' });\n * ```\n */\nexport class AgentFactory {\n private readonly config: AgentFactoryConfig;\n private readonly cache = new Map<string, ContractSpecAgent>();\n\n constructor(config: AgentFactoryConfig) {\n this.config = config;\n }\n\n /**\n * Create an agent by name.\n *\n * @param name - Agent name (e.g., \"support.bot\")\n * @param version - Optional specific version\n * @param options - Optional creation options\n */\n async create(\n name: string,\n version?: string,\n options?: CreateAgentOptions\n ): Promise<ContractSpecAgent> {\n const spec = this.config.registry.require(name, version);\n return this.createFromSpec(spec, options);\n }\n\n /**\n * Create an agent from a spec directly.\n *\n * @param spec - Agent specification\n * @param options - Optional creation options\n */\n async createFromSpec(\n spec: AgentSpec,\n options?: CreateAgentOptions\n ): Promise<ContractSpecAgent> {\n // Merge tool handlers\n const mergedHandlers = new Map<string, ToolHandler>(\n this.config.toolHandlers\n );\n if (options?.toolHandlers) {\n for (const [key, handler] of options.toolHandlers) {\n mergedHandlers.set(key, handler);\n }\n }\n\n // Merge additional tools\n const mergedTools = {\n ...this.config.additionalTools,\n ...options?.additionalTools,\n };\n\n return ContractSpecAgent.create({\n spec,\n model: options?.model ?? this.config.defaultModel,\n toolHandlers: mergedHandlers,\n knowledgeRetriever: this.config.knowledgeRetriever,\n sessionStore: this.config.sessionStore,\n telemetryCollector: this.config.telemetryCollector,\n additionalTools: mergedTools,\n });\n }\n\n /**\n * Get or create a cached agent instance.\n *\n * Use this when you want to reuse agent instances.\n *\n * @param name - Agent name\n * @param version - Optional specific version\n */\n async getOrCreate(\n name: string,\n version?: string\n ): Promise<ContractSpecAgent> {\n const spec = this.config.registry.require(name, version);\n const cacheKey = `${spec.meta.key}.v${spec.meta.version}`;\n\n let agent = this.cache.get(cacheKey);\n if (!agent) {\n agent = await this.createFromSpec(spec);\n this.cache.set(cacheKey, agent);\n }\n\n return agent;\n }\n\n /**\n * Clear the agent cache.\n */\n clearCache(): void {\n this.cache.clear();\n }\n\n /**\n * List all available agent names.\n */\n listAvailable(): string[] {\n return this.config.registry.listNames();\n }\n}\n\n/**\n * Create an agent factory.\n */\nexport function createAgentFactory(config: AgentFactoryConfig): AgentFactory {\n return new AgentFactory(config);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AA4DA,IAAa,eAAb,MAA0B;CACxB,AAAiB;CACjB,AAAiB,wBAAQ,IAAI,KAAgC;CAE7D,YAAY,QAA4B;AACtC,OAAK,SAAS;;;;;;;;;CAUhB,MAAM,OACJ,MACA,SACA,SAC4B;EAC5B,MAAM,OAAO,KAAK,OAAO,SAAS,QAAQ,MAAM,QAAQ;AACxD,SAAO,KAAK,eAAe,MAAM,QAAQ;;;;;;;;CAS3C,MAAM,eACJ,MACA,SAC4B;EAE5B,MAAM,iBAAiB,IAAI,IACzB,KAAK,OAAO,aACb;AACD,MAAI,SAAS,aACX,MAAK,MAAM,CAAC,KAAK,YAAY,QAAQ,aACnC,gBAAe,IAAI,KAAK,QAAQ;EAKpC,MAAM,cAAc;GAClB,GAAG,KAAK,OAAO;GACf,GAAG,SAAS;GACb;AAED,SAAO,kBAAkB,OAAO;GAC9B;GACA,OAAO,SAAS,SAAS,KAAK,OAAO;GACrC,cAAc;GACd,oBAAoB,KAAK,OAAO;GAChC,cAAc,KAAK,OAAO;GAC1B,oBAAoB,KAAK,OAAO;GAChC,iBAAiB;GAClB,CAAC;;;;;;;;;;CAWJ,MAAM,YACJ,MACA,SAC4B;EAC5B,MAAM,OAAO,KAAK,OAAO,SAAS,QAAQ,MAAM,QAAQ;EACxD,MAAM,WAAW,GAAG,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK;EAEhD,IAAI,QAAQ,KAAK,MAAM,IAAI,SAAS;AACpC,MAAI,CAAC,OAAO;AACV,WAAQ,MAAM,KAAK,eAAe,KAAK;AACvC,QAAK,MAAM,IAAI,UAAU,MAAM;;AAGjC,SAAO;;;;;CAMT,aAAmB;AACjB,OAAK,MAAM,OAAO;;;;;CAMpB,gBAA0B;AACxB,SAAO,KAAK,OAAO,SAAS,WAAW;;;;;;AAO3C,SAAgB,mBAAmB,QAA0C;AAC3E,QAAO,IAAI,aAAa,OAAO"}
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { AgentFactory, AgentFactoryConfig, CreateAgentOptions, createAgentFactor
|
|
|
8
8
|
import "./agent/index.js";
|
|
9
9
|
import { ApprovalRequest, ApprovalStatus, ApprovalStore, ApprovalWorkflow, InMemoryApprovalStore, createApprovalWorkflow } from "./approval/workflow.js";
|
|
10
10
|
import "./approval/index.js";
|
|
11
|
+
import "./spec/index.js";
|
|
11
12
|
import { buildToolHandlers, createToolHandler, specToolToAISDKTool, specToolsToAISDKTools } from "./tools/tool-adapter.js";
|
|
12
13
|
import { createKnowledgeQueryTool } from "./tools/knowledge-tool.js";
|
|
13
14
|
import { McpClientConfig, McpClientResult, createMcpToolsets, mcpServerToTools } from "./tools/mcp-client.js";
|
package/dist/index.js
CHANGED
|
@@ -9,6 +9,7 @@ import { ContractSpecAgent } from "./agent/contract-spec-agent.js";
|
|
|
9
9
|
import { AgentFactory, createAgentFactory } from "./agent/agent-factory.js";
|
|
10
10
|
import "./agent/index.js";
|
|
11
11
|
import { AgentRegistry, createAgentRegistry } from "./spec/registry.js";
|
|
12
|
+
import "./spec/index.js";
|
|
12
13
|
import { createMcpToolsets, mcpServerToTools } from "./tools/mcp-client.js";
|
|
13
14
|
import { agentToMcpServer, createAgentMcpServer } from "./tools/mcp-server.js";
|
|
14
15
|
import "./tools/index.js";
|
package/dist/spec/registry.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AgentSpec } from "./spec.js";
|
|
2
|
+
import { SpecContractRegistry } from "@contractspec/lib.contracts/registry";
|
|
2
3
|
|
|
3
4
|
//#region src/spec/registry.d.ts
|
|
4
5
|
|
|
@@ -7,40 +8,12 @@ import { AgentSpec } from "./spec.js";
|
|
|
7
8
|
*
|
|
8
9
|
* Provides registration, lookup, and version management for agent specs.
|
|
9
10
|
*/
|
|
10
|
-
declare class AgentRegistry {
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Register an agent specification.
|
|
14
|
-
*
|
|
15
|
-
* @param spec - The agent specification to register
|
|
16
|
-
* @returns This registry for chaining
|
|
17
|
-
* @throws Error if the spec is already registered
|
|
18
|
-
*/
|
|
19
|
-
register(spec: AgentSpec): this;
|
|
20
|
-
/**
|
|
21
|
-
* Unregister an agent specification.
|
|
22
|
-
*
|
|
23
|
-
* @param name - Agent name
|
|
24
|
-
* @param version - Agent version
|
|
25
|
-
* @returns True if the spec was removed
|
|
26
|
-
*/
|
|
27
|
-
unregister(name: string, version: number): boolean;
|
|
28
|
-
/**
|
|
29
|
-
* List all registered agent specifications.
|
|
30
|
-
*/
|
|
31
|
-
list(): AgentSpec[];
|
|
11
|
+
declare class AgentRegistry extends SpecContractRegistry<'agent', AgentSpec> {
|
|
12
|
+
constructor(items?: AgentSpec[]);
|
|
32
13
|
/**
|
|
33
14
|
* List all unique agent names (without versions).
|
|
34
15
|
*/
|
|
35
16
|
listNames(): string[];
|
|
36
|
-
/**
|
|
37
|
-
* Get an agent specification by name and optional version.
|
|
38
|
-
*
|
|
39
|
-
* @param name - Agent name
|
|
40
|
-
* @param version - Optional version. If omitted, returns the latest version.
|
|
41
|
-
* @returns The agent spec or undefined if not found
|
|
42
|
-
*/
|
|
43
|
-
get(name: string, version?: number): AgentSpec | undefined;
|
|
44
17
|
/**
|
|
45
18
|
* Get an agent specification or throw if not found.
|
|
46
19
|
*
|
|
@@ -49,14 +22,14 @@ declare class AgentRegistry {
|
|
|
49
22
|
* @returns The agent spec
|
|
50
23
|
* @throws Error if the spec is not found
|
|
51
24
|
*/
|
|
52
|
-
require(name: string, version?:
|
|
25
|
+
require(name: string, version?: string): AgentSpec;
|
|
53
26
|
/**
|
|
54
27
|
* Check if an agent is registered.
|
|
55
28
|
*
|
|
56
29
|
* @param name - Agent name
|
|
57
30
|
* @param version - Optional version
|
|
58
31
|
*/
|
|
59
|
-
has(name: string, version?:
|
|
32
|
+
has(name: string, version?: string): boolean;
|
|
60
33
|
/**
|
|
61
34
|
* Get all versions of an agent.
|
|
62
35
|
*
|
|
@@ -64,10 +37,6 @@ declare class AgentRegistry {
|
|
|
64
37
|
* @returns Array of specs sorted by version (ascending)
|
|
65
38
|
*/
|
|
66
39
|
getVersions(name: string): AgentSpec[];
|
|
67
|
-
/**
|
|
68
|
-
* Clear all registered specs.
|
|
69
|
-
*/
|
|
70
|
-
clear(): void;
|
|
71
40
|
}
|
|
72
41
|
/**
|
|
73
42
|
* Create a new agent registry.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"registry.d.ts","names":[],"sources":["../../src/spec/registry.ts"],"sourcesContent":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"registry.d.ts","names":[],"sources":["../../src/spec/registry.ts"],"sourcesContent":[],"mappings":";;;;;;;AASA;;;AAwB2C,cAxB9B,aAAA,SAAsB,oBAwBQ,CAAA,OAAA,EAxBsB,SAwBtB,CAAA,CAAA;EA0Bd,WAAA,CAAA,KAAA,CAAA,EAjDA,SAiDA,EAAA;EAlDM;;AAkEnC;;;;;;;;;;2CA1C2C;;;;;;;;;;;;;;6BA0Bd;;;;;iBAgBb,mBAAA,CAAA,GAAuB"}
|
package/dist/spec/registry.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { compareVersions } from "compare-versions";
|
|
2
|
+
import { SpecContractRegistry } from "@contractspec/lib.contracts/registry";
|
|
2
3
|
|
|
3
4
|
//#region src/spec/registry.ts
|
|
4
5
|
/**
|
|
@@ -6,66 +7,19 @@ import { agentKey } from "./spec.js";
|
|
|
6
7
|
*
|
|
7
8
|
* Provides registration, lookup, and version management for agent specs.
|
|
8
9
|
*/
|
|
9
|
-
var AgentRegistry = class {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
* Register an agent specification.
|
|
13
|
-
*
|
|
14
|
-
* @param spec - The agent specification to register
|
|
15
|
-
* @returns This registry for chaining
|
|
16
|
-
* @throws Error if the spec is already registered
|
|
17
|
-
*/
|
|
18
|
-
register(spec) {
|
|
19
|
-
const key = agentKey(spec.meta);
|
|
20
|
-
if (this.specs.has(key)) throw new Error(`Duplicate agent spec registered for ${key}`);
|
|
21
|
-
this.specs.set(key, spec);
|
|
22
|
-
return this;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Unregister an agent specification.
|
|
26
|
-
*
|
|
27
|
-
* @param name - Agent name
|
|
28
|
-
* @param version - Agent version
|
|
29
|
-
* @returns True if the spec was removed
|
|
30
|
-
*/
|
|
31
|
-
unregister(name, version) {
|
|
32
|
-
return this.specs.delete(`${name}.v${version}`);
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* List all registered agent specifications.
|
|
36
|
-
*/
|
|
37
|
-
list() {
|
|
38
|
-
return [...this.specs.values()];
|
|
10
|
+
var AgentRegistry = class extends SpecContractRegistry {
|
|
11
|
+
constructor(items) {
|
|
12
|
+
super("agent", items);
|
|
39
13
|
}
|
|
40
14
|
/**
|
|
41
15
|
* List all unique agent names (without versions).
|
|
42
16
|
*/
|
|
43
17
|
listNames() {
|
|
44
18
|
const names = /* @__PURE__ */ new Set();
|
|
45
|
-
for (const spec of this.
|
|
19
|
+
for (const spec of this.items.values()) names.add(spec.meta.key);
|
|
46
20
|
return [...names];
|
|
47
21
|
}
|
|
48
22
|
/**
|
|
49
|
-
* Get an agent specification by name and optional version.
|
|
50
|
-
*
|
|
51
|
-
* @param name - Agent name
|
|
52
|
-
* @param version - Optional version. If omitted, returns the latest version.
|
|
53
|
-
* @returns The agent spec or undefined if not found
|
|
54
|
-
*/
|
|
55
|
-
get(name, version) {
|
|
56
|
-
if (version != null) return this.specs.get(`${name}.v${version}`);
|
|
57
|
-
let latest;
|
|
58
|
-
let maxVersion = -Infinity;
|
|
59
|
-
for (const spec of this.specs.values()) {
|
|
60
|
-
if (spec.meta.key !== name) continue;
|
|
61
|
-
if (spec.meta.version > maxVersion) {
|
|
62
|
-
latest = spec;
|
|
63
|
-
maxVersion = spec.meta.version;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return latest;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
23
|
* Get an agent specification or throw if not found.
|
|
70
24
|
*
|
|
71
25
|
* @param name - Agent name
|
|
@@ -95,14 +49,8 @@ var AgentRegistry = class {
|
|
|
95
49
|
*/
|
|
96
50
|
getVersions(name) {
|
|
97
51
|
const versions = [];
|
|
98
|
-
for (const spec of this.
|
|
99
|
-
return versions.sort((a, b) => a.meta.version
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Clear all registered specs.
|
|
103
|
-
*/
|
|
104
|
-
clear() {
|
|
105
|
-
this.specs.clear();
|
|
52
|
+
for (const spec of this.items.values()) if (spec.meta.key === name) versions.push(spec);
|
|
53
|
+
return versions.sort((a, b) => compareVersions(a.meta.version, b.meta.version));
|
|
106
54
|
}
|
|
107
55
|
};
|
|
108
56
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"registry.js","names":["
|
|
1
|
+
{"version":3,"file":"registry.js","names":["versions: AgentSpec[]"],"sources":["../../src/spec/registry.ts"],"sourcesContent":["import type { AgentSpec } from './spec';\nimport { compareVersions } from 'compare-versions';\nimport { SpecContractRegistry } from '@contractspec/lib.contracts/registry';\n\n/**\n * Registry for managing agent specifications.\n *\n * Provides registration, lookup, and version management for agent specs.\n */\nexport class AgentRegistry extends SpecContractRegistry<'agent', AgentSpec> {\n public constructor(items?: AgentSpec[]) {\n super('agent', items);\n }\n\n /**\n * List all unique agent names (without versions).\n */\n listNames(): string[] {\n const names = new Set<string>();\n for (const spec of this.items.values()) {\n names.add(spec.meta.key);\n }\n return [...names];\n }\n\n /**\n * Get an agent specification or throw if not found.\n *\n * @param name - Agent name\n * @param version - Optional version\n * @returns The agent spec\n * @throws Error if the spec is not found\n */\n require(name: string, version?: string): AgentSpec {\n const spec = this.get(name, version);\n if (!spec) {\n throw new Error(\n `Agent spec not found for ${name}${version != null ? `.v${version}` : ''}`\n );\n }\n return spec;\n }\n\n /**\n * Check if an agent is registered.\n *\n * @param name - Agent name\n * @param version - Optional version\n */\n has(name: string, version?: string): boolean {\n return this.get(name, version) !== undefined;\n }\n\n /**\n * Get all versions of an agent.\n *\n * @param name - Agent name\n * @returns Array of specs sorted by version (ascending)\n */\n getVersions(name: string): AgentSpec[] {\n const versions: AgentSpec[] = [];\n for (const spec of this.items.values()) {\n if (spec.meta.key === name) {\n versions.push(spec);\n }\n }\n return versions.sort((a, b) =>\n compareVersions(a.meta.version, b.meta.version)\n );\n }\n}\n\n/**\n * Create a new agent registry.\n */\nexport function createAgentRegistry(): AgentRegistry {\n return new AgentRegistry();\n}\n"],"mappings":";;;;;;;;;AASA,IAAa,gBAAb,cAAmC,qBAAyC;CAC1E,AAAO,YAAY,OAAqB;AACtC,QAAM,SAAS,MAAM;;;;;CAMvB,YAAsB;EACpB,MAAM,wBAAQ,IAAI,KAAa;AAC/B,OAAK,MAAM,QAAQ,KAAK,MAAM,QAAQ,CACpC,OAAM,IAAI,KAAK,KAAK,IAAI;AAE1B,SAAO,CAAC,GAAG,MAAM;;;;;;;;;;CAWnB,QAAQ,MAAc,SAA6B;EACjD,MAAM,OAAO,KAAK,IAAI,MAAM,QAAQ;AACpC,MAAI,CAAC,KACH,OAAM,IAAI,MACR,4BAA4B,OAAO,WAAW,OAAO,KAAK,YAAY,KACvE;AAEH,SAAO;;;;;;;;CAST,IAAI,MAAc,SAA2B;AAC3C,SAAO,KAAK,IAAI,MAAM,QAAQ,KAAK;;;;;;;;CASrC,YAAY,MAA2B;EACrC,MAAMA,WAAwB,EAAE;AAChC,OAAK,MAAM,QAAQ,KAAK,MAAM,QAAQ,CACpC,KAAI,KAAK,KAAK,QAAQ,KACpB,UAAS,KAAK,KAAK;AAGvB,SAAO,SAAS,MAAM,GAAG,MACvB,gBAAgB,EAAE,KAAK,SAAS,EAAE,KAAK,QAAQ,CAChD;;;;;;AAOL,SAAgB,sBAAqC;AACnD,QAAO,IAAI,eAAe"}
|
|
@@ -3,14 +3,14 @@
|
|
|
3
3
|
* Parse agent ID into name and version.
|
|
4
4
|
*/
|
|
5
5
|
function parseAgentId(agentId) {
|
|
6
|
-
const match = agentId.match(/^(.+)\.v(\
|
|
6
|
+
const match = agentId.match(/^(.+)\.v(\s+)$/);
|
|
7
7
|
if (match) return {
|
|
8
8
|
name: match[1],
|
|
9
|
-
version:
|
|
9
|
+
version: match[2]
|
|
10
10
|
};
|
|
11
11
|
return {
|
|
12
12
|
name: agentId,
|
|
13
|
-
version: 1
|
|
13
|
+
version: "1.0.0"
|
|
14
14
|
};
|
|
15
15
|
}
|
|
16
16
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"adapter.js","names":["toolSample: OperationMetricSample","stepSample: OperationMetricSample","noopTelemetryCollector: TelemetryCollector"],"sources":["../../src/telemetry/adapter.ts"],"sourcesContent":["import type { StepResult, ToolSet } from 'ai';\n\n/**\n * Metric sample compatible with @contractspec/lib.evolution OperationMetricSample.\n */\nexport interface OperationMetricSample {\n operation: { name: string; version:
|
|
1
|
+
{"version":3,"file":"adapter.js","names":["toolSample: OperationMetricSample","stepSample: OperationMetricSample","noopTelemetryCollector: TelemetryCollector"],"sources":["../../src/telemetry/adapter.ts"],"sourcesContent":["import type { StepResult, ToolSet } from 'ai';\n\n/**\n * Metric sample compatible with @contractspec/lib.evolution OperationMetricSample.\n */\nexport interface OperationMetricSample {\n operation: { name: string; version: string };\n durationMs: number;\n success: boolean;\n timestamp: Date;\n metadata?: Record<string, unknown>;\n}\n\n/**\n * Interface for collecting telemetry metrics.\n *\n * Implementations can send metrics to:\n * - @contractspec/lib.evolution for self-improvement\n * - PostHog for analytics\n * - Custom monitoring systems\n */\nexport interface TelemetryCollector {\n /**\n * Collect a metric sample.\n */\n collect(sample: OperationMetricSample): Promise<void>;\n}\n\n/**\n * Parse agent ID into name and version.\n */\nfunction parseAgentId(agentId: string): { name: string; version: string } {\n const match = agentId.match(/^(.+)\\.v(\\s+)$/);\n if (match) {\n // eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n return { name: match[1]!, version: match[2]! };\n }\n return { name: agentId, version: '1.0.0' };\n}\n\n/**\n * Track an agent step for telemetry.\n *\n * Called from ContractSpecAgent.onStepFinish to feed metrics\n * to the evolution engine.\n *\n * @param collector - Telemetry collector\n * @param agentId - Agent identifier (e.g., \"support.bot.v1\")\n * @param step - AI SDK step result\n * @param durationMs - Optional step duration in milliseconds\n */\nexport async function trackAgentStep(\n collector: TelemetryCollector,\n agentId: string,\n step: StepResult<ToolSet>,\n durationMs?: number\n): Promise<void> {\n const { name, version } = parseAgentId(agentId);\n\n // Track tool invocations\n for (const toolCall of step.toolCalls ?? []) {\n const toolSample: OperationMetricSample = {\n operation: { name: `${name}.${toolCall.toolName}`, version },\n durationMs: durationMs ?? 0,\n success:\n step.toolResults?.some(\n (r) => r.toolCallId === toolCall.toolCallId && r.output !== undefined\n ) ?? false,\n timestamp: new Date(),\n metadata: {\n agentId,\n toolName: toolCall.toolName,\n finishReason: step.finishReason,\n },\n };\n await collector.collect(toolSample);\n }\n\n // Track overall step\n const stepSample: OperationMetricSample = {\n operation: { name, version },\n durationMs: durationMs ?? 0,\n success: step.finishReason !== 'error',\n timestamp: new Date(),\n metadata: {\n agentId,\n finishReason: step.finishReason,\n tokenUsage: step.usage,\n toolCallCount: step.toolCalls?.length ?? 0,\n },\n };\n await collector.collect(stepSample);\n}\n\n/**\n * In-memory telemetry collector for testing.\n */\nexport class InMemoryTelemetryCollector implements TelemetryCollector {\n private readonly samples: OperationMetricSample[] = [];\n\n async collect(sample: OperationMetricSample): Promise<void> {\n this.samples.push(sample);\n }\n\n /**\n * Get all collected samples.\n */\n getSamples(): OperationMetricSample[] {\n return [...this.samples];\n }\n\n /**\n * Get samples for a specific operation.\n */\n getSamplesForOperation(operationName: string): OperationMetricSample[] {\n return this.samples.filter((s) => s.operation.name === operationName);\n }\n\n /**\n * Clear all samples.\n */\n clear(): void {\n this.samples.length = 0;\n }\n}\n\n/**\n * Create an in-memory telemetry collector.\n */\nexport function createInMemoryTelemetryCollector(): InMemoryTelemetryCollector {\n return new InMemoryTelemetryCollector();\n}\n\n/**\n * No-op telemetry collector that discards all metrics.\n */\nexport const noopTelemetryCollector: TelemetryCollector = {\n collect: async () => {\n /* noop */\n },\n};\n"],"mappings":";;;;AA+BA,SAAS,aAAa,SAAoD;CACxE,MAAM,QAAQ,QAAQ,MAAM,iBAAiB;AAC7C,KAAI,MAEF,QAAO;EAAE,MAAM,MAAM;EAAK,SAAS,MAAM;EAAK;AAEhD,QAAO;EAAE,MAAM;EAAS,SAAS;EAAS;;;;;;;;;;;;;AAc5C,eAAsB,eACpB,WACA,SACA,MACA,YACe;CACf,MAAM,EAAE,MAAM,YAAY,aAAa,QAAQ;AAG/C,MAAK,MAAM,YAAY,KAAK,aAAa,EAAE,EAAE;EAC3C,MAAMA,aAAoC;GACxC,WAAW;IAAE,MAAM,GAAG,KAAK,GAAG,SAAS;IAAY;IAAS;GAC5D,YAAY,cAAc;GAC1B,SACE,KAAK,aAAa,MACf,MAAM,EAAE,eAAe,SAAS,cAAc,EAAE,WAAW,OAC7D,IAAI;GACP,2BAAW,IAAI,MAAM;GACrB,UAAU;IACR;IACA,UAAU,SAAS;IACnB,cAAc,KAAK;IACpB;GACF;AACD,QAAM,UAAU,QAAQ,WAAW;;CAIrC,MAAMC,aAAoC;EACxC,WAAW;GAAE;GAAM;GAAS;EAC5B,YAAY,cAAc;EAC1B,SAAS,KAAK,iBAAiB;EAC/B,2BAAW,IAAI,MAAM;EACrB,UAAU;GACR;GACA,cAAc,KAAK;GACnB,YAAY,KAAK;GACjB,eAAe,KAAK,WAAW,UAAU;GAC1C;EACF;AACD,OAAM,UAAU,QAAQ,WAAW;;;;;AAMrC,IAAa,6BAAb,MAAsE;CACpE,AAAiB,UAAmC,EAAE;CAEtD,MAAM,QAAQ,QAA8C;AAC1D,OAAK,QAAQ,KAAK,OAAO;;;;;CAM3B,aAAsC;AACpC,SAAO,CAAC,GAAG,KAAK,QAAQ;;;;;CAM1B,uBAAuB,eAAgD;AACrE,SAAO,KAAK,QAAQ,QAAQ,MAAM,EAAE,UAAU,SAAS,cAAc;;;;;CAMvE,QAAc;AACZ,OAAK,QAAQ,SAAS;;;;;;AAO1B,SAAgB,mCAA+D;AAC7E,QAAO,IAAI,4BAA4B;;;;;AAMzC,MAAaC,yBAA6C,EACxD,SAAS,YAAY,IAGtB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contractspec/lib.ai-agent",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.45.0",
|
|
4
4
|
"description": "AI agent orchestration with MCP and tool support",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"contractspec",
|
|
@@ -40,13 +40,14 @@
|
|
|
40
40
|
"@ai-sdk/mistral": "3.0.1",
|
|
41
41
|
"@ai-sdk/openai": "3.0.1",
|
|
42
42
|
"@modelcontextprotocol/sdk": "^1.24.3",
|
|
43
|
-
"@contractspec/lib.contracts": "1.
|
|
44
|
-
"@contractspec/lib.knowledge": "1.
|
|
43
|
+
"@contractspec/lib.contracts": "1.45.0",
|
|
44
|
+
"@contractspec/lib.knowledge": "1.45.0",
|
|
45
|
+
"compare-versions": "^6.1.1",
|
|
45
46
|
"zod": "^4.1.13"
|
|
46
47
|
},
|
|
47
48
|
"devDependencies": {
|
|
48
|
-
"@contractspec/tool.tsdown": "1.
|
|
49
|
-
"@contractspec/tool.typescript": "1.
|
|
49
|
+
"@contractspec/tool.tsdown": "1.45.0",
|
|
50
|
+
"@contractspec/tool.typescript": "1.45.0",
|
|
50
51
|
"tsdown": "^0.18.3",
|
|
51
52
|
"typescript": "^5.9.3"
|
|
52
53
|
},
|