@anvia/core 0.12.2 → 0.12.3
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/dist/agent/index.js +2 -2
- package/dist/{chunk-TUDZBJGF.js → chunk-3CPOJSKK.js} +6 -6
- package/dist/chunk-3CPOJSKK.js.map +1 -0
- package/dist/{chunk-XUO3KR5T.js → chunk-FBN6IEZA.js} +2 -2
- package/dist/{chunk-XUO3KR5T.js.map → chunk-FBN6IEZA.js.map} +1 -1
- package/dist/{chunk-QNVLGAPV.js → chunk-LLA7EW5W.js} +2 -2
- package/dist/evals/index.js +10 -4
- package/dist/evals/index.js.map +1 -1
- package/dist/extractor/index.js +3 -3
- package/dist/index.js +2 -2
- package/dist/internal/agent.js +1 -1
- package/dist/mcp/index.js +13 -1
- package/dist/mcp/index.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-TUDZBJGF.js.map +0 -1
- /package/dist/{chunk-QNVLGAPV.js.map → chunk-LLA7EW5W.js.map} +0 -0
package/dist/agent/index.js
CHANGED
|
@@ -67,7 +67,7 @@ var Agent = class {
|
|
|
67
67
|
this.description = options.description;
|
|
68
68
|
this.model = options.model;
|
|
69
69
|
this.instructions = options.instructions;
|
|
70
|
-
this.staticContext = options.staticContext ?? [];
|
|
70
|
+
this.staticContext = [...options.staticContext ?? []];
|
|
71
71
|
this.temperature = options.temperature;
|
|
72
72
|
this.maxTokens = options.maxTokens;
|
|
73
73
|
this.additionalParams = options.additionalParams;
|
|
@@ -76,12 +76,12 @@ var Agent = class {
|
|
|
76
76
|
this.defaultMaxTurns = options.defaultMaxTurns ?? DEFAULT_MAX_TURNS;
|
|
77
77
|
this.hook = options.hook;
|
|
78
78
|
this.outputSchema = options.outputSchema;
|
|
79
|
-
this.observers = options.observers ?? [];
|
|
79
|
+
this.observers = [...options.observers ?? []];
|
|
80
80
|
this.approvals = options.approvals;
|
|
81
81
|
this.guardrails = [...options.guardrails ?? []];
|
|
82
|
-
this.dynamicContexts = options.dynamicContexts ?? [];
|
|
83
|
-
this.dynamicTools = options.dynamicTools ?? [];
|
|
84
|
-
this.middlewares = options.middlewares ?? options.toolMiddlewares ?? [];
|
|
82
|
+
this.dynamicContexts = [...options.dynamicContexts ?? []];
|
|
83
|
+
this.dynamicTools = [...options.dynamicTools ?? []];
|
|
84
|
+
this.middlewares = [...options.middlewares ?? options.toolMiddlewares ?? []];
|
|
85
85
|
this.toolMiddlewares = this.middlewares;
|
|
86
86
|
this.memory = options.memory;
|
|
87
87
|
this.eventStore = options.eventStore;
|
|
@@ -204,4 +204,4 @@ export {
|
|
|
204
204
|
Agent,
|
|
205
205
|
AgentSession
|
|
206
206
|
};
|
|
207
|
-
//# sourceMappingURL=chunk-
|
|
207
|
+
//# sourceMappingURL=chunk-3CPOJSKK.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/agent/agent.ts","../src/agent/ids.ts"],"sourcesContent":["import { z } from \"zod\";\nimport { isStreamingCompletionModel } from \"../completion/create-completion\";\nimport type {\n CompletionModel,\n Document,\n JsonObject,\n JsonValue,\n Message as MessageType,\n ToolChoice,\n} from \"../completion/index\";\nimport type { GuardrailPolicy } from \"../guardrails\";\nimport type { PromptHook } from \"../hooks\";\nimport { compact } from \"../internal/compact\";\nimport type { MemoryRegistration, SessionOptions } from \"../memory/types\";\nimport type { AgentObserverRegistration } from \"../observability\";\nimport { PromptRequest } from \"../request\";\nimport { createTool } from \"../tool/create-tool\";\nimport type { ToolSearchDocument } from \"../tool/dynamic-tools\";\nimport type { AgentMiddleware } from \"../tool/middleware\";\nimport { isSkillTool } from \"../tool/skill-tool-marker\";\nimport type {\n AnyTool,\n NormalizedToolOutput,\n Tool,\n ToolApprovalsOptions,\n ToolCallContext,\n} from \"../tool/tool\";\nimport { ToolSet } from \"../tool/tool-set\";\nimport type { VectorSearchIndex } from \"../vector-store\";\nimport { normalizeAgentId } from \"./ids\";\nimport type {\n AgentEventStoreRegistration,\n AgentOptions,\n AgentToolOptions,\n DynamicContextRegistration,\n DynamicToolRegistration,\n} from \"./types\";\n\nexport const DEFAULT_MAX_TURNS = 20;\n\nexport class Agent<M extends CompletionModel = CompletionModel> {\n readonly id: string;\n readonly name: string | undefined;\n readonly description: string | undefined;\n readonly model: M;\n readonly instructions: string | undefined;\n readonly staticContext: Document[];\n readonly temperature: number | undefined;\n readonly maxTokens: number | undefined;\n readonly additionalParams: JsonValue | undefined;\n readonly toolSet: ToolSet;\n readonly toolChoice: ToolChoice | undefined;\n readonly defaultMaxTurns: number | undefined;\n readonly hook: PromptHook | undefined;\n readonly outputSchema: JsonObject | undefined;\n readonly observers: AgentObserverRegistration[];\n readonly approvals: ToolApprovalsOptions | undefined;\n readonly guardrails: GuardrailPolicy[];\n readonly dynamicContexts: DynamicContextRegistration[];\n readonly dynamicTools: DynamicToolRegistration[];\n readonly middlewares: AgentMiddleware[];\n /**\n * @deprecated Use `middlewares` instead.\n */\n readonly toolMiddlewares: AgentMiddleware[];\n readonly memory: MemoryRegistration | undefined;\n readonly eventStore: AgentEventStoreRegistration | undefined;\n\n constructor(options: AgentOptions<M>) {\n this.id = normalizeAgentId(options.id);\n this.name = options.name;\n this.description = options.description;\n this.model = options.model;\n this.instructions = options.instructions;\n this.staticContext = [...(options.staticContext ?? [])];\n this.temperature = options.temperature;\n this.maxTokens = options.maxTokens;\n this.additionalParams = options.additionalParams;\n this.toolSet = options.toolSet ?? new ToolSet();\n this.toolChoice = options.toolChoice;\n this.defaultMaxTurns = options.defaultMaxTurns ?? DEFAULT_MAX_TURNS;\n this.hook = options.hook;\n this.outputSchema = options.outputSchema;\n this.observers = [...(options.observers ?? [])];\n this.approvals = options.approvals;\n this.guardrails = [...(options.guardrails ?? [])];\n this.dynamicContexts = [...(options.dynamicContexts ?? [])];\n this.dynamicTools = [...(options.dynamicTools ?? [])];\n this.middlewares = [...(options.middlewares ?? options.toolMiddlewares ?? [])];\n this.toolMiddlewares = this.middlewares;\n this.memory = options.memory;\n this.eventStore = options.eventStore;\n }\n\n prompt(prompt: string | MessageType | MessageType[]): PromptRequest<M> {\n return PromptRequest.fromAgent(this, prompt);\n }\n\n session(sessionId: string, options: SessionOptions = {}): AgentSession<M> {\n if (this.memory === undefined) {\n throw new Error(`Agent \"${this.id}\" has no memory store configured.`);\n }\n const normalized = sessionId.trim();\n if (normalized.length === 0) {\n throw new TypeError(\"Session id must be a non-empty string.\");\n }\n return new AgentSession(this, {\n sessionId: normalized,\n ...(options.userId !== undefined && { userId: options.userId }),\n ...(options.metadata !== undefined && { metadata: options.metadata }),\n });\n }\n\n asTool(options: AgentToolOptions): Tool<{ prompt: string }, string> {\n const description =\n options.description ?? this.description ?? `Prompt the ${options.name} agent.`;\n\n return createTool({\n name: options.name,\n description,\n input: z.object({\n prompt: z.string().describe(\"The prompt to send to the agent.\"),\n }),\n output: z.string(),\n execute: async ({ prompt }, context: ToolCallContext) => {\n const request = this.prompt(prompt);\n const childRequest =\n options.maxTurns === undefined ? request : request.maxTurns(options.maxTurns);\n if (\n options.stream === true &&\n context.emitStreamEvent !== undefined &&\n this.model.capabilities.streaming &&\n isStreamingCompletionModel(this.model)\n ) {\n let output = \"\";\n for await (const event of childRequest.stream()) {\n await context.emitStreamEvent(\n compact({\n agentId: this.id,\n agentName: this.name,\n event,\n }),\n );\n if (event.type === \"final\") {\n output = event.output;\n }\n }\n return output;\n }\n const response = await childRequest.send();\n return response.output;\n },\n });\n }\n\n getTool(toolName: string): AnyTool | undefined {\n const staticTool = this.toolSet.get(toolName);\n if (staticTool !== undefined) {\n return staticTool;\n }\n\n for (const registration of this.dynamicTools) {\n const dynamicTool = dynamicToolSetFromIndex(registration.index)?.get(toolName);\n if (dynamicTool !== undefined) {\n return dynamicTool;\n }\n }\n\n return undefined;\n }\n\n async callTool(\n toolName: string,\n args: string,\n context?: ToolCallContext,\n ): Promise<NormalizedToolOutput> {\n if (this.toolSet.contains(toolName)) {\n return this.toolSet.call(toolName, args, context);\n }\n\n for (const registration of this.dynamicTools) {\n const toolSet = dynamicToolSetFromIndex(registration.index);\n if (toolSet?.contains(toolName)) {\n return toolSet.call(toolName, args, context);\n }\n }\n\n return this.toolSet.call(toolName, args, context);\n }\n\n shouldApplyToolMiddleware(toolName: string): boolean {\n return !isSkillTool(this.getTool(toolName));\n }\n}\n\nexport class AgentSession<M extends CompletionModel = CompletionModel> {\n constructor(\n private readonly agent: Agent<M>,\n private readonly context: {\n sessionId: string;\n userId?: string | undefined;\n metadata?: JsonObject | undefined;\n },\n ) {}\n\n prompt(prompt: string | MessageType): PromptRequest<M> {\n if (Array.isArray(prompt)) {\n throw new TypeError(\"AgentSession.prompt does not accept Message[] transcripts.\");\n }\n return PromptRequest.fromAgent(this.agent, prompt, { memoryContext: this.context });\n }\n\n async messages(): Promise<MessageType[]> {\n const memory = this.agent.memory;\n if (memory === undefined) {\n throw new Error(`Agent \"${this.agent.id}\" has no memory store configured.`);\n }\n return memory.store.load(this.context);\n }\n\n async clear(): Promise<void> {\n const memory = this.agent.memory;\n if (memory === undefined) {\n throw new Error(`Agent \"${this.agent.id}\" has no memory store configured.`);\n }\n await memory.store.clear(this.context);\n }\n}\n\nfunction dynamicToolSetFromIndex(\n index: VectorSearchIndex<ToolSearchDocument>,\n): ToolSet | undefined {\n const maybeIndex = index as { toolSet?: unknown };\n return maybeIndex.toolSet instanceof ToolSet ? maybeIndex.toolSet : undefined;\n}\n","export function normalizeAgentId(id: string): string {\n if (typeof id !== \"string\") {\n throw new TypeError(\"Agent id must be a string.\");\n }\n\n const normalized = id.trim();\n if (normalized.length === 0) {\n throw new TypeError(\"Agent id must be a non-empty string.\");\n }\n\n return normalized;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,SAAS,SAAS;;;ACAX,SAAS,iBAAiB,IAAoB;AACnD,MAAI,OAAO,OAAO,UAAU;AAC1B,UAAM,IAAI,UAAU,4BAA4B;AAAA,EAClD;AAEA,QAAM,aAAa,GAAG,KAAK;AAC3B,MAAI,WAAW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AAAA,IAClF;AACA,WAAO,cAAc,UAAU,KAAK,OAAO,QAAQ,EAAE,eAAe,KAAK,QAAQ,CAAC;AAAA,EACpF;AAAA,EAEA,MAAM,WAAmC;AACvC,UAAM,SAAS,KAAK,MAAM;AAC1B,QAAI,WAAW,QAAW;AACxB,YAAM,IAAI,MAAM,UAAU,KAAK,MAAM,EAAE,mCAAmC;AAAA,IAC5E;AACA,WAAO,OAAO,MAAM,KAAK,KAAK,OAAO;AAAA,EACvC;AAAA,EAEA,MAAM,QAAuB;AAC3B,UAAM,SAAS,KAAK,MAAM;AAC1B,QAAI,WAAW,QAAW;AACxB,YAAM,IAAI,MAAM,UAAU,KAAK,MAAM,EAAE,mCAAmC;AAAA,IAC5E;AACA,UAAM,OAAO,MAAM,MAAM,KAAK,OAAO;AAAA,EACvC;AACF;AAEA,SAAS,wBACP,OACqB;AACrB,QAAM,aAAa;AACnB,SAAO,WAAW,mBAAmB,UAAU,WAAW,UAAU;AACtE;","names":[]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
Agent,
|
|
3
3
|
normalizeAgentId
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-3CPOJSKK.js";
|
|
5
5
|
import {
|
|
6
6
|
ToolSet
|
|
7
7
|
} from "./chunk-YVQBC3SJ.js";
|
|
@@ -212,4 +212,4 @@ var AgentBuilder = class {
|
|
|
212
212
|
export {
|
|
213
213
|
AgentBuilder
|
|
214
214
|
};
|
|
215
|
-
//# sourceMappingURL=chunk-
|
|
215
|
+
//# sourceMappingURL=chunk-FBN6IEZA.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/agent/builder.ts"],"sourcesContent":["import type { CompletionModel, Document, JsonObject, JsonValue, ToolChoice } from \"../completion\";\nimport { appendGuardrailPolicies } from \"../guardrails\";\nimport type { GuardrailPolicy, GuardrailPolicyInput } from \"../guardrails\";\nimport type { PromptHook } from \"../hooks\";\nimport type { McpServer } from \"../mcp\";\nimport { resolveMemoryOptions } from \"../memory/options\";\nimport type { MemoryOptions, MemoryRegistration, MemoryStore } from \"../memory/types\";\nimport type { AgentObserver, AgentObserverRegistration, ObserveOptions } from \"../observability\";\nimport { toProviderJsonSchema, type ZodSchema } from \"../schema/zod-schema\";\nimport type { SkillSet } from \"../skills\";\nimport type { ToolSearchDocument } from \"../tool/dynamic-tools\";\nimport type { AgentMiddleware, ToolMiddleware } from \"../tool/middleware\";\nimport type { AnyTool, ToolApprovalsOptions } from \"../tool/tool\";\nimport { ToolSet } from \"../tool/tool-set\";\nimport type { VectorSearchIndex } from \"../vector-store\";\nimport { Agent } from \"./agent\";\nimport { normalizeAgentId } from \"./ids\";\nimport type {\n AgentEventStore,\n AgentEventStoreOptions,\n AgentEventStoreRegistration,\n DynamicContextOptions,\n DynamicContextRegistration,\n DynamicToolOptions,\n DynamicToolRegistration,\n} from \"./types\";\n\nexport class AgentBuilder<M extends CompletionModel = CompletionModel> {\n private readonly agentId: string;\n private agentName: string | undefined;\n private agentDescription: string | undefined;\n private instructionBlocks: string[] = [];\n private contextDocs: Document[] = [];\n private temp: number | undefined;\n private maxTokenCount: number | undefined;\n private params: JsonValue | undefined;\n private choice: ToolChoice | undefined;\n private turns: number | undefined;\n private requestHook: PromptHook | undefined;\n private schema: JsonObject | undefined;\n private approvalOptions: ToolApprovalsOptions | undefined;\n private guardrailPolicies: GuardrailPolicy[] = [];\n private skillInstructionBlocks: string[] = [];\n private observerRegistrations: AgentObserverRegistration[] = [];\n private dynamicContextRegistrations: DynamicContextRegistration[] = [];\n private dynamicToolRegistrations: DynamicToolRegistration[] = [];\n private middlewareRegistrations: AgentMiddleware[] = [];\n private memoryRegistration: MemoryRegistration | undefined;\n private eventStoreRegistration: AgentEventStoreRegistration | undefined;\n private activeToolSet = new ToolSet();\n\n constructor(\n agentId: string,\n private readonly completionModel: M,\n ) {\n this.agentId = normalizeAgentId(agentId);\n }\n\n name(name: string): this {\n this.agentName = name;\n return this;\n }\n\n description(description: string): this {\n this.agentDescription = description;\n return this;\n }\n\n instructions(instructions: string): this {\n if (instructions.length > 0) {\n this.instructionBlocks.push(instructions);\n }\n return this;\n }\n\n context(text: string, id = `static_doc_${this.contextDocs.length}`): this {\n this.contextDocs.push({ id, text });\n return this;\n }\n\n dynamicContext<T>(index: VectorSearchIndex<T>, options: DynamicContextOptions<T>): this {\n this.dynamicContextRegistrations.push({ index, options } as DynamicContextRegistration);\n return this;\n }\n\n dynamicTools(index: VectorSearchIndex<ToolSearchDocument>, options: DynamicToolOptions): this {\n this.dynamicToolRegistrations.push({ index, options });\n return this;\n }\n\n tool(tool: AnyTool): this {\n this.activeToolSet.addTool(tool);\n return this;\n }\n\n tools(tools: AnyTool[]): this {\n this.activeToolSet.addTools(tools);\n return this;\n }\n\n mcp(servers: McpServer[]): this {\n for (const server of servers) {\n this.activeToolSet.addTools(server.tools);\n }\n return this;\n }\n\n skills(skillSet: SkillSet): this {\n if (skillSet.instructions.length > 0) {\n this.skillInstructionBlocks.push(skillSet.instructions);\n }\n this.activeToolSet.addTools(skillSet.tools);\n return this;\n }\n\n useToolSet(toolSet: ToolSet): this {\n toolSet.addTools(this.activeToolSet);\n this.activeToolSet = toolSet;\n return this;\n }\n\n temperature(temperature: number): this {\n this.temp = temperature;\n return this;\n }\n\n maxTokens(maxTokens: number): this {\n this.maxTokenCount = maxTokens;\n return this;\n }\n\n additionalParams(params: JsonValue): this {\n this.params = params;\n return this;\n }\n\n toolChoice(toolChoice: ToolChoice): this {\n this.choice = toolChoice;\n return this;\n }\n\n defaultMaxTurns(defaultMaxTurns: number): this {\n this.turns = defaultMaxTurns;\n return this;\n }\n\n hook(hook: PromptHook): this {\n this.requestHook = hook;\n return this;\n }\n\n middleware(middleware: AgentMiddleware): this {\n this.middlewareRegistrations.push(middleware);\n return this;\n }\n\n middlewares(middlewares: AgentMiddleware[]): this {\n this.middlewareRegistrations.push(...middlewares);\n return this;\n }\n\n /**\n * @deprecated Use `middleware` instead.\n */\n toolMiddleware(middleware: ToolMiddleware): this {\n return this.middleware(middleware);\n }\n\n /**\n * @deprecated Use `middlewares` instead.\n */\n toolMiddlewares(middlewares: ToolMiddleware[]): this {\n return this.middlewares(middlewares);\n }\n\n observe(observer: AgentObserver, options: ObserveOptions = {}): this {\n this.observerRegistrations.push({\n observer,\n failOnObserverError: options.failOnObserverError,\n });\n return this;\n }\n\n approvals(options: ToolApprovalsOptions): this {\n this.approvalOptions = options;\n return this;\n }\n\n guardrails(policies: GuardrailPolicyInput): this {\n this.guardrailPolicies = appendGuardrailPolicies(this.guardrailPolicies, policies);\n return this;\n }\n\n memory(store: MemoryStore, options: MemoryOptions = {}): this {\n this.memoryRegistration = {\n store,\n options: resolveMemoryOptions(options),\n };\n return this;\n }\n\n eventStore(store: AgentEventStore, options: AgentEventStoreOptions = {}): this {\n this.eventStoreRegistration = {\n store,\n options: {\n include: options.include ?? \"all\",\n },\n };\n return this;\n }\n\n outputSchema(schema: ZodSchema): this {\n this.schema = toProviderJsonSchema(schema);\n return this;\n }\n\n build(): Agent<M> {\n return new Agent({\n id: this.agentId,\n name: this.agentName,\n description: this.agentDescription,\n model: this.completionModel,\n instructions: this.buildInstructions(),\n staticContext: this.contextDocs,\n temperature: this.temp,\n maxTokens: this.maxTokenCount,\n additionalParams: this.params,\n toolSet: this.activeToolSet,\n toolChoice: this.choice,\n defaultMaxTurns: this.turns,\n hook: this.requestHook,\n outputSchema: this.schema,\n observers: this.observerRegistrations,\n approvals: this.approvalOptions,\n guardrails: this.guardrailPolicies,\n dynamicContexts: this.dynamicContextRegistrations,\n dynamicTools: this.dynamicToolRegistrations,\n middlewares: this.middlewareRegistrations,\n memory: this.memoryRegistration,\n eventStore: this.eventStoreRegistration,\n });\n }\n\n private buildInstructions(): string | undefined {\n const parts = [...this.instructionBlocks, ...this.skillInstructionBlocks].filter(\n (part): part is string => part !== undefined && part.length > 0,\n );\n return parts.length === 0 ? undefined : parts.join(\"\\n\\n\");\n }\n}\n"],"m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Aa,KAAK;AAAA,MAClB,QAAQ,KAAK;AAAA,MACb,YAAY,KAAK;AAAA,IACnB,CAAC;AAAA,EACH;AAAA,EAEQ,oBAAwC;AAC9C,UAAM,QAAQ,CAAC,GAAG,KAAK,mBAAmB,GAAG,KAAK,sBAAsB,EAAE;AAAA,MACxE,CAAC,SAAyB,SAAS,UAAa,KAAK,SAAS;AAAA,IAChE;AACA,WAAO,MAAM,WAAW,IAAI,SAAY,MAAM,KAAK,MAAM;AAAA,EAC3D;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/agent/builder.ts"],"sourcesContent":["import type { CompletionModel, Document, JsonObject, JsonValue, ToolChoice } from \"../completion\";\nimport type { GuardrailPolicy, GuardrailPolicyInput } from \"../guardrails\";\nimport { appendGuardrailPolicies } from \"../guardrails\";\nimport type { PromptHook } from \"../hooks\";\nimport type { McpServer } from \"../mcp\";\nimport { resolveMemoryOptions } from \"../memory/options\";\nimport type { MemoryOptions, MemoryRegistration, MemoryStore } from \"../memory/types\";\nimport type { AgentObserver, AgentObserverRegistration, ObserveOptions } from \"../observability\";\nimport { toProviderJsonSchema, type ZodSchema } from \"../schema/zod-schema\";\nimport type { SkillSet } from \"../skills\";\nimport type { ToolSearchDocument } from \"../tool/dynamic-tools\";\nimport type { AgentMiddleware, ToolMiddleware } from \"../tool/middleware\";\nimport type { AnyTool, ToolApprovalsOptions } from \"../tool/tool\";\nimport { ToolSet } from \"../tool/tool-set\";\nimport type { VectorSearchIndex } from \"../vector-store\";\nimport { Agent } from \"./agent\";\nimport { normalizeAgentId } from \"./ids\";\nimport type {\n AgentEventStore,\n AgentEventStoreOptions,\n AgentEventStoreRegistration,\n DynamicContextOptions,\n DynamicContextRegistration,\n DynamicToolOptions,\n DynamicToolRegistration,\n} from \"./types\";\n\nexport class AgentBuilder<M extends CompletionModel = CompletionModel> {\n private readonly agentId: string;\n private agentName: string | undefined;\n private agentDescription: string | undefined;\n private instructionBlocks: string[] = [];\n private contextDocs: Document[] = [];\n private temp: number | undefined;\n private maxTokenCount: number | undefined;\n private params: JsonValue | undefined;\n private choice: ToolChoice | undefined;\n private turns: number | undefined;\n private requestHook: PromptHook | undefined;\n private schema: JsonObject | undefined;\n private approvalOptions: ToolApprovalsOptions | undefined;\n private guardrailPolicies: GuardrailPolicy[] = [];\n private skillInstructionBlocks: string[] = [];\n private observerRegistrations: AgentObserverRegistration[] = [];\n private dynamicContextRegistrations: DynamicContextRegistration[] = [];\n private dynamicToolRegistrations: DynamicToolRegistration[] = [];\n private middlewareRegistrations: AgentMiddleware[] = [];\n private memoryRegistration: MemoryRegistration | undefined;\n private eventStoreRegistration: AgentEventStoreRegistration | undefined;\n private activeToolSet = new ToolSet();\n\n constructor(\n agentId: string,\n private readonly completionModel: M,\n ) {\n this.agentId = normalizeAgentId(agentId);\n }\n\n name(name: string): this {\n this.agentName = name;\n return this;\n }\n\n description(description: string): this {\n this.agentDescription = description;\n return this;\n }\n\n instructions(instructions: string): this {\n if (instructions.length > 0) {\n this.instructionBlocks.push(instructions);\n }\n return this;\n }\n\n context(text: string, id = `static_doc_${this.contextDocs.length}`): this {\n this.contextDocs.push({ id, text });\n return this;\n }\n\n dynamicContext<T>(index: VectorSearchIndex<T>, options: DynamicContextOptions<T>): this {\n this.dynamicContextRegistrations.push({ index, options } as DynamicContextRegistration);\n return this;\n }\n\n dynamicTools(index: VectorSearchIndex<ToolSearchDocument>, options: DynamicToolOptions): this {\n this.dynamicToolRegistrations.push({ index, options });\n return this;\n }\n\n tool(tool: AnyTool): this {\n this.activeToolSet.addTool(tool);\n return this;\n }\n\n tools(tools: AnyTool[]): this {\n this.activeToolSet.addTools(tools);\n return this;\n }\n\n mcp(servers: McpServer[]): this {\n for (const server of servers) {\n this.activeToolSet.addTools(server.tools);\n }\n return this;\n }\n\n skills(skillSet: SkillSet): this {\n if (skillSet.instructions.length > 0) {\n this.skillInstructionBlocks.push(skillSet.instructions);\n }\n this.activeToolSet.addTools(skillSet.tools);\n return this;\n }\n\n useToolSet(toolSet: ToolSet): this {\n toolSet.addTools(this.activeToolSet);\n this.activeToolSet = toolSet;\n return this;\n }\n\n temperature(temperature: number): this {\n this.temp = temperature;\n return this;\n }\n\n maxTokens(maxTokens: number): this {\n this.maxTokenCount = maxTokens;\n return this;\n }\n\n additionalParams(params: JsonValue): this {\n this.params = params;\n return this;\n }\n\n toolChoice(toolChoice: ToolChoice): this {\n this.choice = toolChoice;\n return this;\n }\n\n defaultMaxTurns(defaultMaxTurns: number): this {\n this.turns = defaultMaxTurns;\n return this;\n }\n\n hook(hook: PromptHook): this {\n this.requestHook = hook;\n return this;\n }\n\n middleware(middleware: AgentMiddleware): this {\n this.middlewareRegistrations.push(middleware);\n return this;\n }\n\n middlewares(middlewares: AgentMiddleware[]): this {\n this.middlewareRegistrations.push(...middlewares);\n return this;\n }\n\n /**\n * @deprecated Use `middleware` instead.\n */\n toolMiddleware(middleware: ToolMiddleware): this {\n return this.middleware(middleware);\n }\n\n /**\n * @deprecated Use `middlewares` instead.\n */\n toolMiddlewares(middlewares: ToolMiddleware[]): this {\n return this.middlewares(middlewares);\n }\n\n observe(observer: AgentObserver, options: ObserveOptions = {}): this {\n this.observerRegistrations.push({\n observer,\n failOnObserverError: options.failOnObserverError,\n });\n return this;\n }\n\n approvals(options: ToolApprovalsOptions): this {\n this.approvalOptions = options;\n return this;\n }\n\n guardrails(policies: GuardrailPolicyInput): this {\n this.guardrailPolicies = appendGuardrailPolicies(this.guardrailPolicies, policies);\n return this;\n }\n\n memory(store: MemoryStore, options: MemoryOptions = {}): this {\n this.memoryRegistration = {\n store,\n options: resolveMemoryOptions(options),\n };\n return this;\n }\n\n eventStore(store: AgentEventStore, options: AgentEventStoreOptions = {}): this {\n this.eventStoreRegistration = {\n store,\n options: {\n include: options.include ?? \"all\",\n },\n };\n return this;\n }\n\n outputSchema(schema: ZodSchema): this {\n this.schema = toProviderJsonSchema(schema);\n return this;\n }\n\n build(): Agent<M> {\n return new Agent({\n id: this.agentId,\n name: this.agentName,\n description: this.agentDescription,\n model: this.completionModel,\n instructions: this.buildInstructions(),\n staticContext: this.contextDocs,\n temperature: this.temp,\n maxTokens: this.maxTokenCount,\n additionalParams: this.params,\n toolSet: this.activeToolSet,\n toolChoice: this.choice,\n defaultMaxTurns: this.turns,\n hook: this.requestHook,\n outputSchema: this.schema,\n observers: this.observerRegistrations,\n approvals: this.approvalOptions,\n guardrails: this.guardrailPolicies,\n dynamicContexts: this.dynamicContextRegistrations,\n dynamicTools: this.dynamicToolRegistrations,\n middlewares: this.middlewareRegistrations,\n memory: this.memoryRegistration,\n eventStore: this.eventStoreRegistration,\n });\n }\n\n private buildInstructions(): string | undefined {\n const parts = [...this.instructionBlocks, ...this.skillInstructionBlocks].filter(\n (part): part is string => part !== undefined && part.length > 0,\n );\n return parts.length === 0 ? undefined : parts.join(\"\\n\\n\");\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA2BO,IAAM,eAAN,MAAgE;AAAA,EAwBrE,YACE,SACiB,iBACjB;AADiB;AAEjB,SAAK,UAAU,iBAAiB,OAAO;AAAA,EACzC;AAAA,EAHmB;AAAA,EAzBF;AAAA,EACT;AAAA,EACA;AAAA,EACA,oBAA8B,CAAC;AAAA,EAC/B,cAA0B,CAAC;AAAA,EAC3B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,oBAAuC,CAAC;AAAA,EACxC,yBAAmC,CAAC;AAAA,EACpC,wBAAqD,CAAC;AAAA,EACtD,8BAA4D,CAAC;AAAA,EAC7D,2BAAsD,CAAC;AAAA,EACvD,0BAA6C,CAAC;AAAA,EAC9C;AAAA,EACA;AAAA,EACA,gBAAgB,IAAI,QAAQ;AAAA,EASpC,KAAK,MAAoB;AACvB,SAAK,YAAY;AACjB,WAAO;AAAA,EACT;AAAA,EAEA,YAAY,aAA2B;AACrC,SAAK,mBAAmB;AACxB,WAAO;AAAA,EACT;AAAA,EAEA,aAAa,c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O;AAAA,EACT;AAAA,EAEA,QAAkB;AAChB,WAAO,IAAI,MAAM;AAAA,MACf,IAAI,KAAK;AAAA,MACT,MAAM,KAAK;AAAA,MACX,aAAa,KAAK;AAAA,MAClB,OAAO,KAAK;AAAA,MACZ,cAAc,KAAK,kBAAkB;AAAA,MACrC,eAAe,KAAK;AAAA,MACpB,aAAa,KAAK;AAAA,MAClB,WAAW,KAAK;AAAA,MAChB,kBAAkB,KAAK;AAAA,MACvB,SAAS,KAAK;AAAA,MACd,YAAY,KAAK;AAAA,MACjB,iBAAiB,KAAK;AAAA,MACtB,MAAM,KAAK;AAAA,MACX,cAAc,KAAK;AAAA,MACnB,WAAW,KAAK;AAAA,MAChB,WAAW,KAAK;AAAA,MAChB,YAAY,KAAK;AAAA,MACjB,iBAAiB,KAAK;AAAA,MACtB,cAAc,KAAK;AAAA,MACnB,aAAa,KAAK;AAAA,MAClB,QAAQ,KAAK;AAAA,MACb,YAAY,KAAK;AAAA,IACnB,CAAC;AAAA,EACH;AAAA,EAEQ,oBAAwC;AAC9C,UAAM,QAAQ,CAAC,GAAG,KAAK,mBAAmB,GAAG,KAAK,sBAAsB,EAAE;AAAA,MACxE,CAAC,SAAyB,SAAS,UAAa,KAAK,SAAS;AAAA,IAChE;AACA,WAAO,MAAM,WAAW,IAAI,SAAY,MAAM,KAAK,MAAM;AAAA,EAC3D;AACF;","names":[]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
AgentBuilder
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-FBN6IEZA.js";
|
|
4
4
|
import {
|
|
5
5
|
extractRagText
|
|
6
6
|
} from "./chunk-4NCRFQHS.js";
|
|
@@ -138,4 +138,4 @@ export {
|
|
|
138
138
|
Extractor,
|
|
139
139
|
ExtractorBuilder
|
|
140
140
|
};
|
|
141
|
-
//# sourceMappingURL=chunk-
|
|
141
|
+
//# sourceMappingURL=chunk-LLA7EW5W.js.map
|
package/dist/evals/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
ExtractorBuilder
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import "../chunk-
|
|
5
|
-
import "../chunk-
|
|
3
|
+
} from "../chunk-LLA7EW5W.js";
|
|
4
|
+
import "../chunk-FBN6IEZA.js";
|
|
5
|
+
import "../chunk-3CPOJSKK.js";
|
|
6
6
|
import "../chunk-4NCRFQHS.js";
|
|
7
7
|
import "../chunk-YK4WAAS4.js";
|
|
8
8
|
import "../chunk-XUUY2L2D.js";
|
|
@@ -152,11 +152,17 @@ function contains(options = {}) {
|
|
|
152
152
|
if (typeof expected !== "string" && !(expected instanceof RegExp)) {
|
|
153
153
|
return EvalOutcome.invalid("Contains expected value must be a string or RegExp.");
|
|
154
154
|
}
|
|
155
|
-
const passed = expected instanceof RegExp ? expected
|
|
155
|
+
const passed = expected instanceof RegExp ? regexMatches(expected, actual) : actual.includes(expected);
|
|
156
156
|
return passed ? EvalOutcome.pass(true) : EvalOutcome.fail(false, { comment: `Output did not contain ${String(expected)}.` });
|
|
157
157
|
}
|
|
158
158
|
};
|
|
159
159
|
}
|
|
160
|
+
function regexMatches(pattern, text) {
|
|
161
|
+
pattern.lastIndex = 0;
|
|
162
|
+
const matched = pattern.test(text);
|
|
163
|
+
pattern.lastIndex = 0;
|
|
164
|
+
return matched;
|
|
165
|
+
}
|
|
160
166
|
function semanticSimilarity(options) {
|
|
161
167
|
return {
|
|
162
168
|
name: options.name ?? "semantic_similarity",
|
package/dist/evals/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/evals/agent-target.ts","../../src/evals/metric.ts","../../src/evals/metrics.ts","../../src/evals/format.ts","../../src/evals/outcome.ts","../../src/evals/selectors.ts","../../src/evals/runner.ts"],"sourcesContent":["import type { Agent } from \"../agent/agent\";\nimport type { Message } from \"../completion\";\nimport type { PromptResponse } from \"../request/types\";\nimport type { EvalCase, EvalTarget } from \"./types\";\n\nexport type AgentEvalTargetOptions<Input, Output = PromptResponse> = {\n prompt?: ((input: Input, testCase: EvalCase<Input>) => string | Message) | undefined;\n output?: ((response: PromptResponse, testCase: EvalCase<Input>) => Output) | undefined;\n};\n\nexport function agentEvalTarget<Input>(\n agent: Agent,\n options?: AgentEvalTargetOptions<Input, PromptResponse>,\n): EvalTarget<Input, PromptResponse>;\nexport function agentEvalTarget<Input, Output>(\n agent: Agent,\n options: AgentEvalTargetOptions<Input, Output>,\n): EvalTarget<Input, Output>;\nexport function agentEvalTarget<Input, Output>(\n agent: Agent,\n options: AgentEvalTargetOptions<Input, Output | PromptResponse> = {},\n): EvalTarget<Input, Output | PromptResponse> {\n return async (input, testCase) => {\n const prompt = options.prompt?.(input, testCase) ?? String(input);\n const response = await agent.prompt(prompt).send();\n return options.output === undefined ? response : options.output(response, testCase);\n };\n}\n","import type { EvalMetric } from \"./types\";\n\nexport function defineMetric<Input, Output, Score, Expected>(\n metric: EvalMetric<Input, Output, Score, Expected>,\n): EvalMetric<Input, Output, Score, Expected> {\n return metric;\n}\n","import { z } from \"zod\";\nimport type { CompletionModel } from \"../completion\";\nimport { cosineSimilarity, type EmbeddingModel, embedText } from \"../embeddings\";\nimport { ExtractorBuilder } from \"../extractor\";\nimport type { ZodSchema } from \"../schema\";\nimport { errorMessage, formatValue, stableComparable } from \"./format\";\nimport { EvalOutcome } from \"./outcome\";\nimport { resolveActual, resolveActualText, resolveExpected, resolveJudgePrompt } from \"./selectors\";\nimport type { EvalMetric, SelectorOrValue, ValueSelector } from \"./types\";\n\nexport type ExactMatchOptions<Input, Output, Expected = unknown> = {\n name?: string | undefined;\n actual?: ValueSelector<Input, Output, Expected, unknown> | undefined;\n expected?: SelectorOrValue<Input, Output, Expected, unknown> | undefined;\n};\n\nexport function exactMatch<Input, Output, Expected = unknown>(\n options: ExactMatchOptions<Input, Output, Expected> = {},\n): EvalMetric<Input, Output, boolean, Expected> {\n return {\n name: options.name ?? \"exact_match\",\n async evaluate(args) {\n const actual = await resolveActual(options.actual, args);\n const expected = await resolveExpected(options.expected, args);\n if (expected === undefined) {\n return EvalOutcome.invalid(\"No expected value provided for exact match.\");\n }\n const passed = stableComparable(actual) === stableComparable(expected);\n return passed\n ? EvalOutcome.pass(true)\n : EvalOutcome.fail(false, { comment: `Expected ${formatValue(expected)}.` });\n },\n };\n}\n\nexport type ContainsOptions<Input, Output, Expected = unknown> = {\n name?: string | undefined;\n actual?: ValueSelector<Input, Output, Expected, string> | undefined;\n expected?: SelectorOrValue<Input, Output, Expected, string | RegExp> | undefined;\n};\n\nexport function contains<Input, Output, Expected = unknown>(\n options: ContainsOptions<Input, Output, Expected> = {},\n): EvalMetric<Input, Output, boolean, Expected> {\n return {\n name: options.name ?? \"contains\",\n async evaluate(args) {\n const actual = await resolveActualText(options.actual, args);\n const expected = await resolveExpected(options.expected, args);\n if (expected === undefined) {\n return EvalOutcome.invalid(\"No expected value provided for contains.\");\n }\n if (typeof expected !== \"string\" && !(expected instanceof RegExp)) {\n return EvalOutcome.invalid(\"Contains expected value must be a string or RegExp.\");\n }\n const passed = expected instanceof RegExp ? expected.test(actual) : actual.includes(expected);\n return passed\n ? EvalOutcome.pass(true)\n : EvalOutcome.fail(false, { comment: `Output did not contain ${String(expected)}.` });\n },\n };\n}\n\nexport type SemanticSimilarityOptions<Input, Output, Expected = unknown> = {\n name?: string | undefined;\n model: EmbeddingModel;\n threshold: number;\n actual?: ValueSelector<Input, Output, Expected, string> | undefined;\n expected?: SelectorOrValue<Input, Output, Expected, string> | undefined;\n};\n\nexport function semanticSimilarity<Input, Output, Expected = unknown>(\n options: SemanticSimilarityOptions<Input, Output, Expected>,\n): EvalMetric<Input, Output, number, Expected> {\n return {\n name: options.name ?? \"semantic_similarity\",\n async evaluate(args) {\n const actual = await resolveActualText(options.actual, args);\n const expected = await resolveExpected(options.expected, args);\n if (expected === undefined) {\n return EvalOutcome.invalid(\"No expected value provided for semantic similarity.\");\n }\n if (typeof expected !== \"string\") {\n return EvalOutcome.invalid(\"Semantic similarity expected value must be a string.\");\n }\n const [actualEmbedding, expectedEmbedding] = await Promise.all([\n embedText(options.model, actual),\n embedText(options.model, expected),\n ]);\n const score = cosineSimilarity(actualEmbedding.vector, expectedEmbedding.vector);\n return score >= options.threshold\n ? EvalOutcome.pass(score)\n : EvalOutcome.fail(score, { comment: `Similarity below threshold ${options.threshold}.` });\n },\n };\n}\n\nexport type LlmJudgeOptions<Input, Output, SchemaOutput, Expected = unknown> = {\n name?: string | undefined;\n model: CompletionModel;\n schema: ZodSchema<SchemaOutput>;\n passes(value: SchemaOutput): boolean;\n instructions?: string | undefined;\n retries?: number | undefined;\n prompt?: ValueSelector<Input, Output, Expected, string> | undefined;\n};\n\nexport function llmJudge<Input, Output, SchemaOutput, Expected = unknown>(\n options: LlmJudgeOptions<Input, Output, SchemaOutput, Expected>,\n): EvalMetric<Input, Output, SchemaOutput, Expected> {\n const extractor = new ExtractorBuilder(options.model, options.schema)\n .instructions(\n options.instructions ??\n \"Judge the eval case by the requested schema. Submit the judgment using the schema.\",\n )\n .retries(options.retries ?? 0)\n .build();\n\n return {\n name: options.name ?? \"llm_judge\",\n async evaluate(args) {\n try {\n const judgment = await extractor.extract(await resolveJudgePrompt(options.prompt, args));\n return options.passes(judgment) ? EvalOutcome.pass(judgment) : EvalOutcome.fail(judgment);\n } catch (error) {\n return EvalOutcome.invalid(errorMessage(error));\n }\n },\n };\n}\n\nexport type LlmScoreMetricScore = {\n score: number;\n feedback: string;\n};\n\nexport type LlmScoreOptions<Input, Output, Expected = unknown> = {\n name?: string | undefined;\n model: CompletionModel;\n threshold: number;\n criteria: string | string[];\n instructions?: string | undefined;\n retries?: number | undefined;\n prompt?: ValueSelector<Input, Output, Expected, string> | undefined;\n};\n\nexport function llmScore<Input, Output, Expected = unknown>(\n options: LlmScoreOptions<Input, Output, Expected>,\n): EvalMetric<Input, Output, LlmScoreMetricScore, Expected> {\n const criteria = Array.isArray(options.criteria) ? options.criteria.join(\"\\n\") : options.criteria;\n const extractor = new ExtractorBuilder(\n options.model,\n z.object({\n score: z.number(),\n feedback: z.string(),\n }),\n )\n .instructions(\n options.instructions ??\n `Score the eval case against these criteria:\\n${criteria}\\n\\nReturn a score between 0 and 1 and brief feedback.`,\n )\n .retries(options.retries ?? 0)\n .build();\n\n return {\n name: options.name ?? \"llm_score\",\n async evaluate(args) {\n try {\n const score = await extractor.extract(await resolveJudgePrompt(options.prompt, args));\n if (score.score < 0 || score.score > 1) {\n return EvalOutcome.invalid(`Score ${score.score} outside valid range [0, 1].`, {\n score,\n });\n }\n return score.score >= options.threshold\n ? EvalOutcome.pass(score, { comment: score.feedback })\n : EvalOutcome.fail(score, { comment: score.feedback });\n } catch (error) {\n return EvalOutcome.invalid(errorMessage(error));\n }\n },\n };\n}\n","export function defaultOutputValue(output: unknown): unknown {\n if (\n typeof output === \"object\" &&\n output !== null &&\n \"output\" in output &&\n typeof (output as { output?: unknown }).output === \"string\"\n ) {\n return (output as { output: string }).output;\n }\n return output;\n}\n\nexport function stableComparable(value: unknown): string {\n if (typeof value === \"string\") {\n return value;\n }\n return JSON.stringify(value);\n}\n\nexport function formatValue(value: unknown): string {\n if (typeof value === \"string\") {\n return value;\n }\n try {\n return JSON.stringify(value);\n } catch {\n return String(value);\n }\n}\n\nexport function errorMessage(error: unknown): string {\n return error instanceof Error ? error.message : String(error);\n}\n","import { compact } from \"../internal/compact\";\nimport type { EvalMetadata } from \"./types\";\n\nexport type EvalOutcome<Score = unknown> =\n | {\n outcome: \"pass\";\n score?: Score | undefined;\n comment?: string | undefined;\n metadata?: EvalMetadata | undefined;\n }\n | {\n outcome: \"fail\";\n score?: Score | undefined;\n comment?: string | undefined;\n metadata?: EvalMetadata | undefined;\n }\n | {\n outcome: \"invalid\";\n reason: string;\n score?: Score | undefined;\n comment?: string | undefined;\n metadata?: EvalMetadata | undefined;\n };\n\nexport const EvalOutcome = {\n pass<Score>(\n score?: Score,\n options: { comment?: string | undefined; metadata?: EvalMetadata | undefined } = {},\n ): EvalOutcome<Score> {\n return compact({\n outcome: \"pass\" as const,\n score,\n comment: options.comment,\n metadata: options.metadata,\n }) as EvalOutcome<Score>;\n },\n\n fail<Score>(\n score?: Score,\n options: { comment?: string | undefined; metadata?: EvalMetadata | undefined } = {},\n ): EvalOutcome<Score> {\n return compact({\n outcome: \"fail\" as const,\n score,\n comment: options.comment,\n metadata: options.metadata,\n }) as EvalOutcome<Score>;\n },\n\n invalid<Score = never>(\n reason: string,\n options: {\n score?: Score | undefined;\n comment?: string | undefined;\n metadata?: EvalMetadata | undefined;\n } = {},\n ): EvalOutcome<Score> {\n return compact({\n outcome: \"invalid\" as const,\n reason,\n score: options.score,\n comment: options.comment,\n metadata: options.metadata,\n }) as EvalOutcome<Score>;\n },\n};\n","import { defaultOutputValue, formatValue } from \"./format\";\nimport type { EvalMetricArgs, SelectorOrValue, ValueSelector } from \"./types\";\n\nexport async function resolveActual<Input, Output, Expected>(\n selector: ValueSelector<Input, Output, Expected, unknown> | undefined,\n args: EvalMetricArgs<Input, Output, Expected>,\n): Promise<unknown> {\n return selector === undefined ? defaultOutputValue(args.output) : selector(args);\n}\n\nexport async function resolveActualText<Input, Output, Expected>(\n selector: ValueSelector<Input, Output, Expected, string> | undefined,\n args: EvalMetricArgs<Input, Output, Expected>,\n): Promise<string> {\n const value = selector === undefined ? defaultOutputValue(args.output) : await selector(args);\n return typeof value === \"string\" ? value : JSON.stringify(value);\n}\n\nexport async function resolveExpected<Input, Output, Expected, Value>(\n selectorOrValue: SelectorOrValue<Input, Output, Expected, Value> | undefined,\n args: EvalMetricArgs<Input, Output, Expected>,\n): Promise<Value | Expected | undefined> {\n if (selectorOrValue === undefined) {\n return args.case.expected;\n }\n return typeof selectorOrValue === \"function\"\n ? (selectorOrValue as ValueSelector<Input, Output, Expected, Value>)(args)\n : selectorOrValue;\n}\n\nexport async function resolveJudgePrompt<Input, Output, Expected>(\n selector: ValueSelector<Input, Output, Expected, string> | undefined,\n args: EvalMetricArgs<Input, Output, Expected>,\n): Promise<string> {\n if (selector !== undefined) {\n return selector(args);\n }\n return [\n `Suite: ${args.suiteName}`,\n `Case: ${args.case.id}`,\n `Input: ${formatValue(args.case.input)}`,\n `Expected: ${formatValue(args.case.expected)}`,\n `Output: ${formatValue(defaultOutputValue(args.output))}`,\n ].join(\"\\n\\n\");\n}\n","import { compact } from \"../internal/compact\";\nimport { mapWithConcurrency } from \"../internal/concurrency\";\nimport { errorMessage } from \"./format\";\nimport { EvalOutcome, type EvalOutcome as EvalOutcomeType } from \"./outcome\";\nimport type {\n EvalCase,\n EvalCaseResult,\n EvalMetric,\n EvalMetricResult,\n EvalReporter,\n EvalSuiteResult,\n RunEvalSuiteOptions,\n} from \"./types\";\n\nexport async function runEvalSuite<Input, Output, Expected = unknown>(\n options: RunEvalSuiteOptions<Input, Output, Expected>,\n): Promise<EvalSuiteResult<Input, Output, Expected>> {\n const startedAt = Date.now();\n const results = await mapWithConcurrency(\n options.cases,\n Math.max(1, Math.trunc(options.concurrency ?? 1)),\n (testCase) => runEvalCase(options, testCase),\n );\n const counts = countOutcomes(results);\n return {\n name: options.name,\n results,\n ...counts,\n durationMs: Date.now() - startedAt,\n };\n}\n\nasync function runEvalCase<Input, Output, Expected>(\n options: RunEvalSuiteOptions<Input, Output, Expected>,\n testCase: EvalCase<Input, Expected>,\n): Promise<EvalCaseResult<Input, Output, Expected>> {\n let output: Output | undefined;\n let targetError: unknown;\n try {\n output = await options.target(testCase.input, testCase);\n } catch (error) {\n targetError = error;\n }\n\n const metrics: EvalMetricResult[] = [];\n for (const metric of options.metrics) {\n const outcome =\n targetError === undefined\n ? await safeEvaluate(options.name, testCase, output as Output, metric)\n : EvalOutcome.invalid(`Target failed: ${errorMessage(targetError)}`);\n const reporterErrors = await reportOutcome({\n suiteName: options.name,\n testCase,\n output,\n targetError,\n metric,\n outcome,\n reporters: options.reporters ?? [],\n failOnReporterError: options.failOnReporterError === true,\n });\n metrics.push({ metricName: metric.name, outcome, reporterErrors });\n }\n\n return compact({\n case: testCase,\n output,\n targetError,\n metrics,\n }) as EvalCaseResult<Input, Output, Expected>;\n}\n\nasync function safeEvaluate<Input, Output, Expected>(\n suiteName: string,\n testCase: EvalCase<Input, Expected>,\n output: Output,\n metric: EvalMetric<Input, Output, unknown, Expected>,\n): Promise<EvalOutcomeType> {\n try {\n return await metric.evaluate({ suiteName, case: testCase, output });\n } catch (error) {\n return EvalOutcome.invalid(errorMessage(error));\n }\n}\n\nasync function reportOutcome<Input, Output, Expected>(args: {\n suiteName: string;\n testCase: EvalCase<Input, Expected>;\n output: Output | undefined;\n targetError: unknown;\n metric: EvalMetric<Input, Output, unknown, Expected>;\n outcome: EvalOutcomeType;\n reporters: Array<EvalReporter<Input, Output, Expected>>;\n failOnReporterError: boolean;\n}): Promise<unknown[]> {\n const errors: unknown[] = [];\n for (const reporter of args.reporters) {\n try {\n await reporter.report({\n suiteName: args.suiteName,\n case: args.testCase,\n output: args.output,\n targetError: args.targetError,\n metric: args.metric,\n outcome: args.outcome,\n });\n } catch (error) {\n if (args.failOnReporterError) {\n throw error;\n }\n errors.push(error);\n }\n }\n return errors;\n}\n\nfunction countOutcomes(results: Array<EvalCaseResult<unknown, unknown, unknown>>): {\n passed: number;\n failed: number;\n invalid: number;\n} {\n let passed = 0;\n let failed = 0;\n let invalid = 0;\n for (const result of results) {\n for (const metric of result.metrics) {\n if (metric.outcome.outcome === \"pass\") passed += 1;\n if (metric.outcome.outcome === \"fail\") failed += 1;\n if (metric.outcome.outcome === \"invalid\") invalid += 1;\n }\n }\n return { passed, failed, invalid };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkBO,SAAS,gBACd,OACA,UAAkE,CAAC,GACvB;AAC5C,SAAO,OAAO,OAAO,aAAa;AAChC,UAAM,SAAS,QAAQ,SAAS,OAAO,QAAQ,KAAK,OAAO,KAAK;AAChE,UAAM,WAAW,MAAM,MAAM,OAAO,MAAM,EAAE,KAAK;AACjD,WAAO,QA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vD,CAAC;AACD,YAAQ,KAAK,EAAE,YAAY,OAAO,MAAM,SAAS,eAAe,CAAC;AAAA,EACnE;AAEA,SAAO,QAAQ;AAAA,IACb,MAAM;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,EACF,CAAC;AACH;AAEA,eAAe,aACb,WACA,UACA,QACA,QAC0B;AAC1B,MAAI;AACF,WAAO,MAAM,OAAO,SAAS,EAAE,WAAW,MAAM,UAAU,OAAO,CAAC;AAAA,EACpE,SAAS,OAAO;AACd,WAAO,YAAY,QAAQ,aAAa,KAAK,CAAC;AAAA,EAChD;AACF;AAEA,eAAe,cAAuC,MAS/B;AACrB,QAAM,SAAoB,CAAC;AAC3B,aAAW,YAAY,KAAK,WAAW;AACrC,QAAI;AACF,YAAM,SAAS,OAAO;AAAA,QACpB,WAAW,KAAK;AAAA,QAChB,MAAM,KAAK;AAAA,QACX,QAAQ,KAAK;AAAA,QACb,aAAa,KAAK;AAAA,QAClB,QAAQ,KAAK;AAAA,QACb,SAAS,KAAK;AAAA,MAChB,CAAC;AAAA,IACH,SAAS,OAAO;AACd,UAAI,KAAK,qBAAqB;AAC5B,cAAM;AAAA,MACR;AACA,aAAO,KAAK,KAAK;AAAA,IACnB;AAAA,EACF;AACA,SAAO;AACT;AAEA,SAAS,cAAc,SAIrB;AACA,MAAI,SAAS;AACb,MAAI,SAAS;AACb,MAAI,UAAU;AACd,aAAW,UAAU,SAAS;AAC5B,eAAW,UAAU,OAAO,SAAS;AACnC,UAAI,OAAO,QAAQ,YAAY,OAAQ,WAAU;AACjD,UAAI,OAAO,QAAQ,YAAY,OAAQ,WAAU;AACjD,UAAI,OAAO,QAAQ,YAAY,UAAW,YAAW;AAAA,IACvD;AAAA,EACF;AACA,SAAO,EAAE,QAAQ,QAAQ,QAAQ;AACnC;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/evals/agent-target.ts","../../src/evals/metric.ts","../../src/evals/metrics.ts","../../src/evals/format.ts","../../src/evals/outcome.ts","../../src/evals/selectors.ts","../../src/evals/runner.ts"],"sourcesContent":["import type { Agent } from \"../agent/agent\";\nimport type { Message } from \"../completion\";\nimport type { PromptResponse } from \"../request/types\";\nimport type { EvalCase, EvalTarget } from \"./types\";\n\nexport type AgentEvalTargetOptions<Input, Output = PromptResponse> = {\n prompt?: ((input: Input, testCase: EvalCase<Input>) => string | Message) | undefined;\n output?: ((response: PromptResponse, testCase: EvalCase<Input>) => Output) | undefined;\n};\n\nexport function agentEvalTarget<Input>(\n agent: Agent,\n options?: AgentEvalTargetOptions<Input, PromptResponse>,\n): EvalTarget<Input, PromptResponse>;\nexport function agentEvalTarget<Input, Output>(\n agent: Agent,\n options: AgentEvalTargetOptions<Input, Output>,\n): EvalTarget<Input, Output>;\nexport function agentEvalTarget<Input, Output>(\n agent: Agent,\n options: AgentEvalTargetOptions<Input, Output | PromptResponse> = {},\n): EvalTarget<Input, Output | PromptResponse> {\n return async (input, testCase) => {\n const prompt = options.prompt?.(input, testCase) ?? String(input);\n const response = await agent.prompt(prompt).send();\n return options.output === undefined ? response : options.output(response, testCase);\n };\n}\n","import type { EvalMetric } from \"./types\";\n\nexport function defineMetric<Input, Output, Score, Expected>(\n metric: EvalMetric<Input, Output, Score, Expected>,\n): EvalMetric<Input, Output, Score, Expected> {\n return metric;\n}\n","import { z } from \"zod\";\nimport type { CompletionModel } from \"../completion\";\nimport { cosineSimilarity, type EmbeddingModel, embedText } from \"../embeddings\";\nimport { ExtractorBuilder } from \"../extractor\";\nimport type { ZodSchema } from \"../schema\";\nimport { errorMessage, formatValue, stableComparable } from \"./format\";\nimport { EvalOutcome } from \"./outcome\";\nimport { resolveActual, resolveActualText, resolveExpected, resolveJudgePrompt } from \"./selectors\";\nimport type { EvalMetric, SelectorOrValue, ValueSelector } from \"./types\";\n\nexport type ExactMatchOptions<Input, Output, Expected = unknown> = {\n name?: string | undefined;\n actual?: ValueSelector<Input, Output, Expected, unknown> | undefined;\n expected?: SelectorOrValue<Input, Output, Expected, unknown> | undefined;\n};\n\nexport function exactMatch<Input, Output, Expected = unknown>(\n options: ExactMatchOptions<Input, Output, Expected> = {},\n): EvalMetric<Input, Output, boolean, Expected> {\n return {\n name: options.name ?? \"exact_match\",\n async evaluate(args) {\n const actual = await resolveActual(options.actual, args);\n const expected = await resolveExpected(options.expected, args);\n if (expected === undefined) {\n return EvalOutcome.invalid(\"No expected value provided for exact match.\");\n }\n const passed = stableComparable(actual) === stableComparable(expected);\n return passed\n ? EvalOutcome.pass(true)\n : EvalOutcome.fail(false, { comment: `Expected ${formatValue(expected)}.` });\n },\n };\n}\n\nexport type ContainsOptions<Input, Output, Expected = unknown> = {\n name?: string | undefined;\n actual?: ValueSelector<Input, Output, Expected, string> | undefined;\n expected?: SelectorOrValue<Input, Output, Expected, string | RegExp> | undefined;\n};\n\nexport function contains<Input, Output, Expected = unknown>(\n options: ContainsOptions<Input, Output, Expected> = {},\n): EvalMetric<Input, Output, boolean, Expected> {\n return {\n name: options.name ?? \"contains\",\n async evaluate(args) {\n const actual = await resolveActualText(options.actual, args);\n const expected = await resolveExpected(options.expected, args);\n if (expected === undefined) {\n return EvalOutcome.invalid(\"No expected value provided for contains.\");\n }\n if (typeof expected !== \"string\" && !(expected instanceof RegExp)) {\n return EvalOutcome.invalid(\"Contains expected value must be a string or RegExp.\");\n }\n const passed =\n expected instanceof RegExp ? regexMatches(expected, actual) : actual.includes(expected);\n return passed\n ? EvalOutcome.pass(true)\n : EvalOutcome.fail(false, { comment: `Output did not contain ${String(expected)}.` });\n },\n };\n}\n\nfunction regexMatches(pattern: RegExp, text: string): boolean {\n pattern.lastIndex = 0;\n const matched = pattern.test(text);\n pattern.lastIndex = 0;\n return matched;\n}\n\nexport type SemanticSimilarityOptions<Input, Output, Expected = unknown> = {\n name?: string | undefined;\n model: EmbeddingModel;\n threshold: number;\n actual?: ValueSelector<Input, Output, Expected, string> | undefined;\n expected?: SelectorOrValue<Input, Output, Expected, string> | undefined;\n};\n\nexport function semanticSimilarity<Input, Output, Expected = unknown>(\n options: SemanticSimilarityOptions<Input, Output, Expected>,\n): EvalMetric<Input, Output, number, Expected> {\n return {\n name: options.name ?? \"semantic_similarity\",\n async evaluate(args) {\n const actual = await resolveActualText(options.actual, args);\n const expected = await resolveExpected(options.expected, args);\n if (expected === undefined) {\n return EvalOutcome.invalid(\"No expected value provided for semantic similarity.\");\n }\n if (typeof expected !== \"string\") {\n return EvalOutcome.invalid(\"Semantic similarity expected value must be a string.\");\n }\n const [actualEmbedding, expectedEmbedding] = await Promise.all([\n embedText(options.model, actual),\n embedText(options.model, expected),\n ]);\n const score = cosineSimilarity(actualEmbedding.vector, expectedEmbedding.vector);\n return score >= options.threshold\n ? EvalOutcome.pass(score)\n : EvalOutcome.fail(score, { comment: `Similarity below threshold ${options.threshold}.` });\n },\n };\n}\n\nexport type LlmJudgeOptions<Input, Output, SchemaOutput, Expected = unknown> = {\n name?: string | undefined;\n model: CompletionModel;\n schema: ZodSchema<SchemaOutput>;\n passes(value: SchemaOutput): boolean;\n instructions?: string | undefined;\n retries?: number | undefined;\n prompt?: ValueSelector<Input, Output, Expected, string> | undefined;\n};\n\nexport function llmJudge<Input, Output, SchemaOutput, Expected = unknown>(\n options: LlmJudgeOptions<Input, Output, SchemaOutput, Expected>,\n): EvalMetric<Input, Output, SchemaOutput, Expected> {\n const extractor = new ExtractorBuilder(options.model, options.schema)\n .instructions(\n options.instructions ??\n \"Judge the eval case by the requested schema. Submit the judgment using the schema.\",\n )\n .retries(options.retries ?? 0)\n .build();\n\n return {\n name: options.name ?? \"llm_judge\",\n async evaluate(args) {\n try {\n const judgment = await extractor.extract(await resolveJudgePrompt(options.prompt, args));\n return options.passes(judgment) ? EvalOutcome.pass(judgment) : EvalOutcome.fail(judgment);\n } catch (error) {\n return EvalOutcome.invalid(errorMessage(error));\n }\n },\n };\n}\n\nexport type LlmScoreMetricScore = {\n score: number;\n feedback: string;\n};\n\nexport type LlmScoreOptions<Input, Output, Expected = unknown> = {\n name?: string | undefined;\n model: CompletionModel;\n threshold: number;\n criteria: string | string[];\n instructions?: string | undefined;\n retries?: number | undefined;\n prompt?: ValueSelector<Input, Output, Expected, string> | undefined;\n};\n\nexport function llmScore<Input, Output, Expected = unknown>(\n options: LlmScoreOptions<Input, Output, Expected>,\n): EvalMetric<Input, Output, LlmScoreMetricScore, Expected> {\n const criteria = Array.isArray(options.criteria) ? options.criteria.join(\"\\n\") : options.criteria;\n const extractor = new ExtractorBuilder(\n options.model,\n z.object({\n score: z.number(),\n feedback: z.string(),\n }),\n )\n .instructions(\n options.instructions ??\n `Score the eval case against these criteria:\\n${criteria}\\n\\nReturn a score between 0 and 1 and brief feedback.`,\n )\n .retries(options.retries ?? 0)\n .build();\n\n return {\n name: options.name ?? \"llm_score\",\n async evaluate(args) {\n try {\n const score = await extractor.extract(await resolveJudgePrompt(options.prompt, args));\n if (score.score < 0 || score.score > 1) {\n return EvalOutcome.invalid(`Score ${score.score} outside valid range [0, 1].`, {\n score,\n });\n }\n return score.score >= options.threshold\n ? EvalOutcome.pass(score, { comment: score.feedback })\n : EvalOutcome.fail(score, { comment: score.feedback });\n } catch (error) {\n return EvalOutcome.invalid(errorMessage(error));\n }\n },\n };\n}\n","export function defaultOutputValue(output: unknown): unknown {\n if (\n typeof output === \"object\" &&\n output !== null &&\n \"output\" in output &&\n typeof (output as { output?: unknown }).output === \"string\"\n ) {\n return (output as { output: string }).output;\n }\n return output;\n}\n\nexport function stableComparable(value: unknown): string {\n if (typeof value === \"string\") {\n return value;\n }\n return JSON.stringify(value);\n}\n\nexport function formatValue(value: unknown): string {\n if (typeof value === \"string\") {\n return value;\n }\n try {\n return JSON.stringify(value);\n } catch {\n return String(value);\n }\n}\n\nexport function errorMessage(error: unknown): string {\n return error instanceof Error ? error.message : String(error);\n}\n","import { compact } from \"../internal/compact\";\nimport type { EvalMetadata } from \"./types\";\n\nexport type EvalOutcome<Score = unknown> =\n | {\n outcome: \"pass\";\n score?: Score | undefined;\n comment?: string | undefined;\n metadata?: EvalMetadata | undefined;\n }\n | {\n outcome: \"fail\";\n score?: Score | undefined;\n comment?: string | undefined;\n metadata?: EvalMetadata | undefined;\n }\n | {\n outcome: \"invalid\";\n reason: string;\n score?: Score | undefined;\n comment?: string | undefined;\n metadata?: EvalMetadata | undefined;\n };\n\nexport const EvalOutcome = {\n pass<Score>(\n score?: Score,\n options: { comment?: string | undefined; metadata?: EvalMetadata | undefined } = {},\n ): EvalOutcome<Score> {\n return compact({\n outcome: \"pass\" as const,\n score,\n comment: options.comment,\n metadata: options.metadata,\n }) as EvalOutcome<Score>;\n },\n\n fail<Score>(\n score?: Score,\n options: { comment?: string | undefined; metadata?: EvalMetadata | undefined } = {},\n ): EvalOutcome<Score> {\n return compact({\n outcome: \"fail\" as const,\n score,\n comment: options.comment,\n metadata: options.metadata,\n }) as EvalOutcome<Score>;\n },\n\n invalid<Score = never>(\n reason: string,\n options: {\n score?: Score | undefined;\n comment?: string | undefined;\n metadata?: EvalMetadata | undefined;\n } = {},\n ): EvalOutcome<Score> {\n return compact({\n outcome: \"invalid\" as const,\n reason,\n score: options.score,\n comment: options.comment,\n metadata: options.metadata,\n }) as EvalOutcome<Score>;\n },\n};\n","import { defaultOutputValue, formatValue } from \"./format\";\nimport type { EvalMetricArgs, SelectorOrValue, ValueSelector } from \"./types\";\n\nexport async function resolveActual<Input, Output, Expected>(\n selector: ValueSelector<Input, Output, Expected, unknown> | undefined,\n args: EvalMetricArgs<Input, Output, Expected>,\n): Promise<unknown> {\n return selector === undefined ? defaultOutputValue(args.output) : selector(args);\n}\n\nexport async function resolveActualText<Input, Output, Expected>(\n selector: ValueSelector<Input, Output, Expected, string> | undefined,\n args: EvalMetricArgs<Input, Output, Expected>,\n): Promise<string> {\n const value = selector === undefined ? defaultOutputValue(args.output) : await selector(args);\n return typeof value === \"string\" ? value : JSON.stringify(value);\n}\n\nexport async function resolveExpected<Input, Output, Expected, Value>(\n selectorOrValue: SelectorOrValue<Input, Output, Expected, Value> | undefined,\n args: EvalMetricArgs<Input, Output, Expected>,\n): Promise<Value | Expected | undefined> {\n if (selectorOrValue === undefined) {\n return args.case.expected;\n }\n return typeof selectorOrValue === \"function\"\n ? (selectorOrValue as ValueSelector<Input, Output, Expected, Value>)(args)\n : selectorOrValue;\n}\n\nexport async function resolveJudgePrompt<Input, Output, Expected>(\n selector: ValueSelector<Input, Output, Expected, string> | undefined,\n args: EvalMetricArgs<Input, Output, Expected>,\n): Promise<string> {\n if (selector !== undefined) {\n return selector(args);\n }\n return [\n `Suite: ${args.suiteName}`,\n `Case: ${args.case.id}`,\n `Input: ${formatValue(args.case.input)}`,\n `Expected: ${formatValue(args.case.expected)}`,\n `Output: ${formatValue(defaultOutputValue(args.output))}`,\n ].join(\"\\n\\n\");\n}\n","import { compact } from \"../internal/compact\";\nimport { mapWithConcurrency } from \"../internal/concurrency\";\nimport { errorMessage } from \"./format\";\nimport { EvalOutcome, type EvalOutcome as EvalOutcomeType } from \"./outcome\";\nimport type {\n EvalCase,\n EvalCaseResult,\n EvalMetric,\n EvalMetricResult,\n EvalReporter,\n EvalSuiteResult,\n RunEvalSuiteOptions,\n} from \"./types\";\n\nexport async function runEvalSuite<Input, Output, Expected = unknown>(\n options: RunEvalSuiteOptions<Input, Output, Expected>,\n): Promise<EvalSuiteResult<Input, Output, Expected>> {\n const startedAt = Date.now();\n const results = await mapWithConcurrency(\n options.cases,\n Math.max(1, Math.trunc(options.concurrency ?? 1)),\n (testCase) => runEvalCase(options, testCase),\n );\n const counts = countOutcomes(results);\n return {\n name: options.name,\n results,\n ...counts,\n durationMs: Date.now() - startedAt,\n };\n}\n\nasync function runEvalCase<Input, Output, Expected>(\n options: RunEvalSuiteOptions<Input, Output, Expected>,\n testCase: EvalCase<Input, Expected>,\n): Promise<EvalCaseResult<Input, Output, Expected>> {\n let output: Output | undefined;\n let targetError: unknown;\n try {\n output = await options.target(testCase.input, testCase);\n } catch (error) {\n targetError = error;\n }\n\n const metrics: EvalMetricResult[] = [];\n for (const metric of options.metrics) {\n const outcome =\n targetError === undefined\n ? await safeEvaluate(options.name, testCase, output as Output, metric)\n : EvalOutcome.invalid(`Target failed: ${errorMessage(targetError)}`);\n const reporterErrors = await reportOutcome({\n suiteName: options.name,\n testCase,\n output,\n targetError,\n metric,\n outcome,\n reporters: options.reporters ?? [],\n failOnReporterError: options.failOnReporterError === true,\n });\n metrics.push({ metricName: metric.name, outcome, reporterErrors });\n }\n\n return compact({\n case: testCase,\n output,\n targetError,\n metrics,\n }) as EvalCaseResult<Input, Output, Expected>;\n}\n\nasync function safeEvaluate<Input, Output, Expected>(\n suiteName: string,\n testCase: EvalCase<Input, Expected>,\n output: Output,\n metric: EvalMetric<Input, Output, unknown, Expected>,\n): Promise<EvalOutcomeType> {\n try {\n return await metric.evaluate({ suiteName, case: testCase, output });\n } catch (error) {\n return EvalOutcome.invalid(errorMessage(error));\n }\n}\n\nasync function reportOutcome<Input, Output, Expected>(args: {\n suiteName: string;\n testCase: EvalCase<Input, Expected>;\n output: Output | undefined;\n targetError: unknown;\n metric: EvalMetric<Input, Output, unknown, Expected>;\n outcome: EvalOutcomeType;\n reporters: Array<EvalReporter<Input, Output, Expected>>;\n failOnReporterError: boolean;\n}): Promise<unknown[]> {\n const errors: unknown[] = [];\n for (const reporter of args.reporters) {\n try {\n await reporter.report({\n suiteName: args.suiteName,\n case: args.testCase,\n output: args.output,\n targetError: args.targetError,\n metric: args.metric,\n outcome: args.outcome,\n });\n } catch (error) {\n if (args.failOnReporterError) {\n throw error;\n }\n errors.push(error);\n }\n }\n return errors;\n}\n\nfunction countOutcomes(results: Array<EvalCaseResult<unknown, unknown, unknown>>): {\n passed: number;\n failed: number;\n invalid: number;\n} {\n let passed = 0;\n let failed = 0;\n let invalid = 0;\n for (const result of results) {\n for (const metric of result.metrics) {\n if (metric.outcome.outcome === \"pass\") passed += 1;\n if (metric.outcome.outcome === \"fail\") failed += 1;\n if (metric.outcome.outcome === \"invalid\") invalid += 1;\n }\n }\n return { passed, failed, invalid };\n}\n"],"mappings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AAW,UAAU,SAAS;AAC5B,eAAW,UAAU,OAAO,SAAS;AACnC,UAAI,OAAO,QAAQ,YAAY,OAAQ,WAAU;AACjD,UAAI,OAAO,QAAQ,YAAY,OAAQ,WAAU;AACjD,UAAI,OAAO,QAAQ,YAAY,UAAW,YAAW;AAAA,IACvD;AAAA,EACF;AACA,SAAO,EAAE,QAAQ,QAAQ,QAAQ;AACnC;","names":[]}
|
package/dist/extractor/index.js
CHANGED
|
@@ -2,9 +2,9 @@ import {
|
|
|
2
2
|
ExtractionError,
|
|
3
3
|
Extractor,
|
|
4
4
|
ExtractorBuilder
|
|
5
|
-
} from "../chunk-
|
|
6
|
-
import "../chunk-
|
|
7
|
-
import "../chunk-
|
|
5
|
+
} from "../chunk-LLA7EW5W.js";
|
|
6
|
+
import "../chunk-FBN6IEZA.js";
|
|
7
|
+
import "../chunk-3CPOJSKK.js";
|
|
8
8
|
import "../chunk-4NCRFQHS.js";
|
|
9
9
|
import "../chunk-YK4WAAS4.js";
|
|
10
10
|
import "../chunk-XUUY2L2D.js";
|
package/dist/index.js
CHANGED
package/dist/internal/agent.js
CHANGED
package/dist/mcp/index.js
CHANGED
|
@@ -96,10 +96,12 @@ async function connectMcp(connection) {
|
|
|
96
96
|
}
|
|
97
97
|
|
|
98
98
|
// src/mcp/connections.ts
|
|
99
|
+
import { readFileSync } from "fs";
|
|
99
100
|
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
100
101
|
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
101
102
|
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
102
103
|
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
104
|
+
var CORE_CLIENT_VERSION = readCorePackageVersion();
|
|
103
105
|
var mcp = {
|
|
104
106
|
stdio(options) {
|
|
105
107
|
return {
|
|
@@ -142,9 +144,19 @@ var mcp = {
|
|
|
142
144
|
function createSdkClient() {
|
|
143
145
|
return new Client({
|
|
144
146
|
name: "@anvia/core",
|
|
145
|
-
version:
|
|
147
|
+
version: CORE_CLIENT_VERSION
|
|
146
148
|
});
|
|
147
149
|
}
|
|
150
|
+
function readCorePackageVersion() {
|
|
151
|
+
try {
|
|
152
|
+
const packageJson = JSON.parse(
|
|
153
|
+
readFileSync(new URL("../../package.json", import.meta.url), "utf8")
|
|
154
|
+
);
|
|
155
|
+
return typeof packageJson.version === "string" ? packageJson.version : "0.0.0";
|
|
156
|
+
} catch {
|
|
157
|
+
return "0.0.0";
|
|
158
|
+
}
|
|
159
|
+
}
|
|
148
160
|
function asSdkTransport(transport) {
|
|
149
161
|
return transport;
|
|
150
162
|
}
|
package/dist/mcp/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/mcp/result.ts","../../src/mcp/tool.ts","../../src/mcp/connect.ts","../../src/mcp/connections.ts"],"sourcesContent":["import { isRecord } from \"../internal/compact\";\nimport type { McpToolCallContent, McpToolCallResult } from \"./types\";\n\nexport function createCallToolParams(\n name: string,\n args: unknown,\n): { name: string; arguments?: Record<string, unknown> } {\n if (args === null || args === undefined) {\n return { name };\n }\n\n if (!isRecord(args)) {\n throw new Error(\"MCP tool arguments must be a JSON object\");\n }\n\n return { name, arguments: args };\n}\n\nexport function mapMcpToolResult(result: McpToolCallResult): string {\n if (\"toolResult\" in result) {\n return serializeMcpValue(result.toolResult);\n }\n\n if (result.isError === true) {\n throw new Error(mcpErrorMessage(result.content));\n }\n\n return result.content.map(mapMcpContent).join(\"\");\n}\n\nfunction mcpErrorMessage(content: McpToolCallContent[]): string {\n const text = content\n .map((item) => (item.type === \"text\" ? item.text : undefined))\n .filter((item): item is string => item !== undefined)\n .join(\"\\n\");\n\n return text === \"\" ? \"MCP tool returned an error\" : text;\n}\n\nfunction mapMcpContent(content: McpToolCallContent): string {\n if (content.type === \"text\") {\n return content.text;\n }\n\n if (content.type === \"image\") {\n return `data:${content.mimeType};base64,${content.data}`;\n }\n\n if (content.type === \"resource\") {\n const mimeType =\n content.resource.mimeType === undefined ? \"\" : `data:${content.resource.mimeType};`;\n if (\"text\" in content.resource) {\n return `${mimeType}${content.resource.uri}:${content.resource.text}`;\n }\n\n return `${mimeType}${content.resource.uri}:${content.resource.blob}`;\n }\n\n throw new Error(`Unsupported MCP tool result content: ${serializeMcpValue(content)}`);\n}\n\nfunction serializeMcpValue(value: unknown): string {\n if (typeof value === \"string\") {\n return value;\n }\n\n const serialized = JSON.stringify(value);\n return serialized === undefined ? String(value) : serialized;\n}\n","import type { ToolDefinition } from \"../completion/index\";\nimport type { Tool } from \"../tool/index\";\nimport { createCallToolParams, mapMcpToolResult } from \"./result\";\nimport type { McpClient, McpToolDefinition } from \"./types\";\n\nconst MCP_TOOL_METADATA_KEY = Symbol.for(\"anvia.mcp.tool.metadata\");\n\nexport function createMcpTool(\n definition: McpToolDefinition,\n client: McpClient,\n serverName?: string,\n): Tool {\n const tool: Tool = {\n name: definition.name,\n definition(): ToolDefinition {\n return {\n name: definition.name,\n description: definition.description ?? \"\",\n parameters: definition.inputSchema,\n };\n },\n async call(args): Promise<string> {\n const result = await client.callTool(createCallToolParams(definition.name, args));\n return mapMcpToolResult(result);\n },\n };\n if (serverName !== undefined) {\n Object.defineProperty(tool, MCP_TOOL_METADATA_KEY, {\n value: { serverName },\n enumerable: false,\n });\n }\n return tool;\n}\n","import { createMcpTool } from \"./tool\";\nimport type { McpConnection, McpServer } from \"./types\";\n\nexport async function connectMcp(connection: McpConnection): Promise<McpServer> {\n const client = await connection.connect();\n let tools: Awaited<ReturnType<typeof client.listTools>>[\"tools\"];\n try {\n ({ tools } = await client.listTools());\n } catch (error) {\n try {\n await client.close();\n } catch {\n // Preserve the initialization failure so callers see the actionable MCP error.\n }\n throw error;\n }\n\n return {\n name: connection.name,\n tools: tools.map((tool) => createMcpTool(tool, client, connection.name)),\n close: () => client.close(),\n };\n}\n","import { Client } from \"@modelcontextprotocol/sdk/client/index.js\";\nimport { SSEClientTransport } from \"@modelcontextprotocol/sdk/client/sse.js\";\nimport { StdioClientTransport } from \"@modelcontextprotocol/sdk/client/stdio.js\";\nimport { StreamableHTTPClientTransport } from \"@modelcontextprotocol/sdk/client/streamableHttp.js\";\nimport type {\n McpClient,\n McpConnection,\n McpHttpOptions,\n McpSseOptions,\n McpStdioOptions,\n} from \"./types\";\n\nexport const mcp = {\n stdio(options: McpStdioOptions): McpConnection {\n return {\n name: options.name,\n async connect(): Promise<McpClient> {\n const { name: _name, ...server } = options;\n const client = createSdkClient();\n await client.connect(asSdkTransport(new StdioClientTransport(server)));\n return client as McpClient;\n },\n };\n },\n\n http(options: McpHttpOptions): McpConnection {\n return {\n name: options.name,\n async connect(): Promise<McpClient> {\n const client = createSdkClient();\n await client.connect(\n asSdkTransport(\n new StreamableHTTPClientTransport(new URL(options.url), options.transport),\n ),\n );\n return client as McpClient;\n },\n };\n },\n\n sse(options: McpSseOptions): McpConnection {\n return {\n name: options.name,\n async connect(): Promise<McpClient> {\n const client = createSdkClient();\n await client.connect(\n asSdkTransport(new SSEClientTransport(new URL(options.url), options.transport)),\n );\n return client as McpClient;\n },\n };\n },\n};\n\nfunction createSdkClient(): Client {\n return new Client({\n name: \"@anvia/core\",\n version: \"0.
|
|
1
|
+
{"version":3,"sources":["../../src/mcp/result.ts","../../src/mcp/tool.ts","../../src/mcp/connect.ts","../../src/mcp/connections.ts"],"sourcesContent":["import { isRecord } from \"../internal/compact\";\nimport type { McpToolCallContent, McpToolCallResult } from \"./types\";\n\nexport function createCallToolParams(\n name: string,\n args: unknown,\n): { name: string; arguments?: Record<string, unknown> } {\n if (args === null || args === undefined) {\n return { name };\n }\n\n if (!isRecord(args)) {\n throw new Error(\"MCP tool arguments must be a JSON object\");\n }\n\n return { name, arguments: args };\n}\n\nexport function mapMcpToolResult(result: McpToolCallResult): string {\n if (\"toolResult\" in result) {\n return serializeMcpValue(result.toolResult);\n }\n\n if (result.isError === true) {\n throw new Error(mcpErrorMessage(result.content));\n }\n\n return result.content.map(mapMcpContent).join(\"\");\n}\n\nfunction mcpErrorMessage(content: McpToolCallContent[]): string {\n const text = content\n .map((item) => (item.type === \"text\" ? item.text : undefined))\n .filter((item): item is string => item !== undefined)\n .join(\"\\n\");\n\n return text === \"\" ? \"MCP tool returned an error\" : text;\n}\n\nfunction mapMcpContent(content: McpToolCallContent): string {\n if (content.type === \"text\") {\n return content.text;\n }\n\n if (content.type === \"image\") {\n return `data:${content.mimeType};base64,${content.data}`;\n }\n\n if (content.type === \"resource\") {\n const mimeType =\n content.resource.mimeType === undefined ? \"\" : `data:${content.resource.mimeType};`;\n if (\"text\" in content.resource) {\n return `${mimeType}${content.resource.uri}:${content.resource.text}`;\n }\n\n return `${mimeType}${content.resource.uri}:${content.resource.blob}`;\n }\n\n throw new Error(`Unsupported MCP tool result content: ${serializeMcpValue(content)}`);\n}\n\nfunction serializeMcpValue(value: unknown): string {\n if (typeof value === \"string\") {\n return value;\n }\n\n const serialized = JSON.stringify(value);\n return serialized === undefined ? String(value) : serialized;\n}\n","import type { ToolDefinition } from \"../completion/index\";\nimport type { Tool } from \"../tool/index\";\nimport { createCallToolParams, mapMcpToolResult } from \"./result\";\nimport type { McpClient, McpToolDefinition } from \"./types\";\n\nconst MCP_TOOL_METADATA_KEY = Symbol.for(\"anvia.mcp.tool.metadata\");\n\nexport function createMcpTool(\n definition: McpToolDefinition,\n client: McpClient,\n serverName?: string,\n): Tool {\n const tool: Tool = {\n name: definition.name,\n definition(): ToolDefinition {\n return {\n name: definition.name,\n description: definition.description ?? \"\",\n parameters: definition.inputSchema,\n };\n },\n async call(args): Promise<string> {\n const result = await client.callTool(createCallToolParams(definition.name, args));\n return mapMcpToolResult(result);\n },\n };\n if (serverName !== undefined) {\n Object.defineProperty(tool, MCP_TOOL_METADATA_KEY, {\n value: { serverName },\n enumerable: false,\n });\n }\n return tool;\n}\n","import { createMcpTool } from \"./tool\";\nimport type { McpConnection, McpServer } from \"./types\";\n\nexport async function connectMcp(connection: McpConnection): Promise<McpServer> {\n const client = await connection.connect();\n let tools: Awaited<ReturnType<typeof client.listTools>>[\"tools\"];\n try {\n ({ tools } = await client.listTools());\n } catch (error) {\n try {\n await client.close();\n } catch {\n // Preserve the initialization failure so callers see the actionable MCP error.\n }\n throw error;\n }\n\n return {\n name: connection.name,\n tools: tools.map((tool) => createMcpTool(tool, client, connection.name)),\n close: () => client.close(),\n };\n}\n","import { readFileSync } from \"node:fs\";\nimport { Client } from \"@modelcontextprotocol/sdk/client/index.js\";\nimport { SSEClientTransport } from \"@modelcontextprotocol/sdk/client/sse.js\";\nimport { StdioClientTransport } from \"@modelcontextprotocol/sdk/client/stdio.js\";\nimport { StreamableHTTPClientTransport } from \"@modelcontextprotocol/sdk/client/streamableHttp.js\";\nimport type {\n McpClient,\n McpConnection,\n McpHttpOptions,\n McpSseOptions,\n McpStdioOptions,\n} from \"./types\";\n\nconst CORE_CLIENT_VERSION = readCorePackageVersion();\n\nexport const mcp = {\n stdio(options: McpStdioOptions): McpConnection {\n return {\n name: options.name,\n async connect(): Promise<McpClient> {\n const { name: _name, ...server } = options;\n const client = createSdkClient();\n await client.connect(asSdkTransport(new StdioClientTransport(server)));\n return client as McpClient;\n },\n };\n },\n\n http(options: McpHttpOptions): McpConnection {\n return {\n name: options.name,\n async connect(): Promise<McpClient> {\n const client = createSdkClient();\n await client.connect(\n asSdkTransport(\n new StreamableHTTPClientTransport(new URL(options.url), options.transport),\n ),\n );\n return client as McpClient;\n },\n };\n },\n\n sse(options: McpSseOptions): McpConnection {\n return {\n name: options.name,\n async connect(): Promise<McpClient> {\n const client = createSdkClient();\n await client.connect(\n asSdkTransport(new SSEClientTransport(new URL(options.url), options.transport)),\n );\n return client as McpClient;\n },\n };\n },\n};\n\nfunction createSdkClient(): Client {\n return new Client({\n name: \"@anvia/core\",\n version: CORE_CLIENT_VERSION,\n });\n}\n\nfunction readCorePackageVersion(): string {\n try {\n const packageJson = JSON.parse(\n readFileSync(new URL(\"../../package.json\", import.meta.url), \"utf8\"),\n ) as { version?: unknown };\n return typeof packageJson.version === \"string\" ? packageJson.version : \"0.0.0\";\n } catch {\n return \"0.0.0\";\n }\n}\n\nfunction asSdkTransport(transport: unknown): Parameters<Client[\"connect\"]>[0] {\n return transport as Parameters<Client[\"connect\"]>[0];\n}\n"],"mappings":";;;;;AAGO,SAAS,qBACd,MACA,MACuD;AACvD,MAAI,SAAS,QAAQ,SAAS,QAAW;AACvC,WAAO,EAAE,KAAK;AAAA,EAChB;AAEA,MAAI,CAAC,SAAS,IAAI,GAAG;AACnB,UAAM,IAAI,MAAM,0CAA0C;AAAA,EAC5D;AAEA,SAAO,EAAE,MAAM,WAAW,KAAK;AACjC;AAEO,SAAS,iBAAiB,QAAmC;AAClE,MAAI,gBAAgB,QAAQ;AAC1B,WAAO,kBAAkB,OAAO,UAAU;AAAA,EAC5C;AAEA,MAAI,OAAO,YAAY,MAAM;AAC3B,UAAM,IAAI,MAAM,gBAAgB,OAAO,OAAO,CAAC;AAAA,EACjD;AAEA,SAAO,OAAO,QAAQ,IAAI,aAAa,EAAE,KAAK,EAAE;AAClD;AAEA,SAAS,gBAAgB,SAAuC;AAC9D,QAAM,OAAO,QACV,IAAI,CAAC,SAAU,KAAK,SAAS,SAAS,KAAK,OAAO,MAAU,EAC5D,OAAO,CAAC,SAAyB,SAAS,MAAS,EACnD,KAAK,IAAI;AAEZ,SAAO,SAAS,KAAK,+BAA+B;AACtD;AAEA,SAAS,cAAc,SAAqC;AAC1D,MAAI,QAAQ,SAAS,QAAQ;AAC3B,WAAO,QAAQ;AAAA,EACjB;AAEA,MAAI,QAAQ,SAAS,SAAS;AAC5B,WAAO,QAAQ,QAAQ,QAAQ,WAAW,QAAQ,IAAI;AAAA,EACxD;AAEA,MAAI,QAAQ,SAAS,YAAY;AAC/B,UAAM,WACJ,QAAQ,SAAS,aAAa,SAAY,KAAK,QAAQ,QAAQ,SAAS,QAAQ;AAClF,QAAI,UAAU,QAAQ,UAAU;AAC9B,aAA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xC,MAAI;AACF,UAAM,cAAc,KAAK;AAAA,MACvB,aAAa,IAAI,IAAI,sBAAsB,YAAY,GAAG,GAAG,MAAM;AAAA,IACrE;AACA,WAAO,OAAO,YAAY,YAAY,WAAW,YAAY,UAAU;AAAA,EACzE,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAEA,SAAS,eAAe,WAAsD;AAC5E,SAAO;AACT;","names":[]}
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/agent/agent.ts","../src/agent/ids.ts"],"sourcesContent":["import { z } from \"zod\";\nimport { isStreamingCompletionModel } from \"../completion/create-completion\";\nimport type {\n CompletionModel,\n Document,\n JsonObject,\n JsonValue,\n Message as MessageType,\n ToolChoice,\n} from \"../completion/index\";\nimport type { GuardrailPolicy } from \"../guardrails\";\nimport type { PromptHook } from \"../hooks\";\nimport { compact } from \"../internal/compact\";\nimport type { MemoryRegistration, SessionOptions } from \"../memory/types\";\nimport type { AgentObserverRegistration } from \"../observability\";\nimport { PromptRequest } from \"../request\";\nimport { createTool } from \"../tool/create-tool\";\nimport type { ToolSearchDocument } from \"../tool/dynamic-tools\";\nimport type { AgentMiddleware } from \"../tool/middleware\";\nimport { isSkillTool } from \"../tool/skill-tool-marker\";\nimport type {\n AnyTool,\n NormalizedToolOutput,\n Tool,\n ToolApprovalsOptions,\n ToolCallContext,\n} from \"../tool/tool\";\nimport { ToolSet } from \"../tool/tool-set\";\nimport type { VectorSearchIndex } from \"../vector-store\";\nimport { normalizeAgentId } from \"./ids\";\nimport type {\n AgentEventStoreRegistration,\n AgentOptions,\n AgentToolOptions,\n DynamicContextRegistration,\n DynamicToolRegistration,\n} from \"./types\";\n\nexport const DEFAULT_MAX_TURNS = 20;\n\nexport class Agent<M extends CompletionModel = CompletionModel> {\n readonly id: string;\n readonly name: string | undefined;\n readonly description: string | undefined;\n readonly model: M;\n readonly instructions: string | undefined;\n readonly staticContext: Document[];\n readonly temperature: number | undefined;\n readonly maxTokens: number | undefined;\n readonly additionalParams: JsonValue | undefined;\n readonly toolSet: ToolSet;\n readonly toolChoice: ToolChoice | undefined;\n readonly defaultMaxTurns: number | undefined;\n readonly hook: PromptHook | undefined;\n readonly outputSchema: JsonObject | undefined;\n readonly observers: AgentObserverRegistration[];\n readonly approvals: ToolApprovalsOptions | undefined;\n readonly guardrails: GuardrailPolicy[];\n readonly dynamicContexts: DynamicContextRegistration[];\n readonly dynamicTools: DynamicToolRegistration[];\n readonly middlewares: AgentMiddleware[];\n /**\n * @deprecated Use `middlewares` instead.\n */\n readonly toolMiddlewares: AgentMiddleware[];\n readonly memory: MemoryRegistration | undefined;\n readonly eventStore: AgentEventStoreRegistration | undefined;\n\n constructor(options: AgentOptions<M>) {\n this.id = normalizeAgentId(options.id);\n this.name = options.name;\n this.description = options.description;\n this.model = options.model;\n this.instructions = options.instructions;\n this.staticContext = options.staticContext ?? [];\n this.temperature = options.temperature;\n this.maxTokens = options.maxTokens;\n this.additionalParams = options.additionalParams;\n this.toolSet = options.toolSet ?? new ToolSet();\n this.toolChoice = options.toolChoice;\n this.defaultMaxTurns = options.defaultMaxTurns ?? DEFAULT_MAX_TURNS;\n this.hook = options.hook;\n this.outputSchema = options.outputSchema;\n this.observers = options.observers ?? [];\n this.approvals = options.approvals;\n this.guardrails = [...(options.guardrails ?? [])];\n this.dynamicContexts = options.dynamicContexts ?? [];\n this.dynamicTools = options.dynamicTools ?? [];\n this.middlewares = options.middlewares ?? options.toolMiddlewares ?? [];\n this.toolMiddlewares = this.middlewares;\n this.memory = options.memory;\n this.eventStore = options.eventStore;\n }\n\n prompt(prompt: string | MessageType | MessageType[]): PromptRequest<M> {\n return PromptRequest.fromAgent(this, prompt);\n }\n\n session(sessionId: string, options: SessionOptions = {}): AgentSession<M> {\n if (this.memory === undefined) {\n throw new Error(`Agent \"${this.id}\" has no memory store configured.`);\n }\n const normalized = sessionId.trim();\n if (normalized.length === 0) {\n throw new TypeError(\"Session id must be a non-empty string.\");\n }\n return new AgentSession(this, {\n sessionId: normalized,\n ...(options.userId !== undefined && { userId: options.userId }),\n ...(options.metadata !== undefined && { metadata: options.metadata }),\n });\n }\n\n asTool(options: AgentToolOptions): Tool<{ prompt: string }, string> {\n const description =\n options.description ?? this.description ?? `Prompt the ${options.name} agent.`;\n\n return createTool({\n name: options.name,\n description,\n input: z.object({\n prompt: z.string().describe(\"The prompt to send to the agent.\"),\n }),\n output: z.string(),\n execute: async ({ prompt }, context: ToolCallContext) => {\n const request = this.prompt(prompt);\n const childRequest =\n options.maxTurns === undefined ? request : request.maxTurns(options.maxTurns);\n if (\n options.stream === true &&\n context.emitStreamEvent !== undefined &&\n this.model.capabilities.streaming &&\n isStreamingCompletionModel(this.model)\n ) {\n let output = \"\";\n for await (const event of childRequest.stream()) {\n await context.emitStreamEvent(\n compact({\n agentId: this.id,\n agentName: this.name,\n event,\n }),\n );\n if (event.type === \"final\") {\n output = event.output;\n }\n }\n return output;\n }\n const response = await childRequest.send();\n return response.output;\n },\n });\n }\n\n getTool(toolName: string): AnyTool | undefined {\n const staticTool = this.toolSet.get(toolName);\n if (staticTool !== undefined) {\n return staticTool;\n }\n\n for (const registration of this.dynamicTools) {\n const dynamicTool = dynamicToolSetFromIndex(registration.index)?.get(toolName);\n if (dynamicTool !== undefined) {\n return dynamicTool;\n }\n }\n\n return undefined;\n }\n\n async callTool(\n toolName: string,\n args: string,\n context?: ToolCallContext,\n ): Promise<NormalizedToolOutput> {\n if (this.toolSet.contains(toolName)) {\n return this.toolSet.call(toolName, args, context);\n }\n\n for (const registration of this.dynamicTools) {\n const toolSet = dynamicToolSetFromIndex(registration.index);\n if (toolSet?.contains(toolName)) {\n return toolSet.call(toolName, args, context);\n }\n }\n\n return this.toolSet.call(toolName, args, context);\n }\n\n shouldApplyToolMiddleware(toolName: string): boolean {\n return !isSkillTool(this.getTool(toolName));\n }\n}\n\nexport class AgentSession<M extends CompletionModel = CompletionModel> {\n constructor(\n private readonly agent: Agent<M>,\n private readonly context: {\n sessionId: string;\n userId?: string | undefined;\n metadata?: JsonObject | undefined;\n },\n ) {}\n\n prompt(prompt: string | MessageType): PromptRequest<M> {\n if (Array.isArray(prompt)) {\n throw new TypeError(\"AgentSession.prompt does not accept Message[] transcripts.\");\n }\n return PromptRequest.fromAgent(this.agent, prompt, { memoryContext: this.context });\n }\n\n async messages(): Promise<MessageType[]> {\n const memory = this.agent.memory;\n if (memory === undefined) {\n throw new Error(`Agent \"${this.agent.id}\" has no memory store configured.`);\n }\n return memory.store.load(this.context);\n }\n\n async clear(): Promise<void> {\n const memory = this.agent.memory;\n if (memory === undefined) {\n throw new Error(`Agent \"${this.agent.id}\" has no memory store configured.`);\n }\n await memory.store.clear(this.context);\n }\n}\n\nfunction dynamicToolSetFromIndex(\n index: VectorSearchIndex<ToolSearchDocument>,\n): ToolSet | undefined {\n const maybeIndex = index as { toolSet?: unknown };\n return maybeIndex.toolSet instanceof ToolSet ? maybeIndex.toolSet : undefined;\n}\n","export function normalizeAgentId(id: string): string {\n if (typeof id !== \"string\") {\n throw new TypeError(\"Agent id must be a string.\");\n }\n\n const normalized = id.trim();\n if (normalized.length === 0) {\n throw new TypeError(\"Agent id must be a non-empty string.\");\n }\n\n return normalized;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,SAAS,SAAS;;;ACAX,SAAS,iBAAiB,IAAoB;AACnD,MAAI,OAAO,OAAO,UAAU;AAC1B,UAAM,IAAI,UAAU,4BAA4B;AAAA,EAClD;AAEA,QAAM,aAAa,GAAG,KAAK;AAC3B,MAAI,WAAW,WAAW,GAAG;AAC3B,UAAM,IAAI,UAAU,sCAAsC;AAAA,EAC5D;AAEA,SAAO;AACT;;;AD2BO,IAAM,oBAAoB;AAE1B,IAAM,QAAN,MAAyD;AAAA,EACrD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,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