@tenet-ai/sdk 0.1.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/dist/client.d.ts +145 -0
- package/dist/client.js +499 -0
- package/dist/client.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/integrations/index.d.ts +32 -0
- package/dist/integrations/index.js +42 -0
- package/dist/integrations/index.js.map +1 -0
- package/dist/integrations/langchain.d.ts +151 -0
- package/dist/integrations/langchain.js +255 -0
- package/dist/integrations/langchain.js.map +1 -0
- package/dist/integrations/openai.d.ts +166 -0
- package/dist/integrations/openai.js +250 -0
- package/dist/integrations/openai.js.map +1 -0
- package/dist/integrations/vercel-ai.d.ts +185 -0
- package/dist/integrations/vercel-ai.js +297 -0
- package/dist/integrations/vercel-ai.js.map +1 -0
- package/dist/types.d.ts +258 -0
- package/dist/types.js +22 -0
- package/dist/types.js.map +1 -0
- package/package.json +33 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TenetVercelAITracker = exports.TenetOpenAITracker = exports.TenetCallbackHandler = exports.ActorType = exports.ResultType = exports.OriginType = exports.IntentHandle = exports.TenetError = exports.Tenet = void 0;
|
|
4
|
+
var client_1 = require("./client");
|
|
5
|
+
Object.defineProperty(exports, "Tenet", { enumerable: true, get: function () { return client_1.Tenet; } });
|
|
6
|
+
Object.defineProperty(exports, "TenetError", { enumerable: true, get: function () { return client_1.TenetError; } });
|
|
7
|
+
Object.defineProperty(exports, "IntentHandle", { enumerable: true, get: function () { return client_1.IntentHandle; } });
|
|
8
|
+
var types_1 = require("./types");
|
|
9
|
+
Object.defineProperty(exports, "OriginType", { enumerable: true, get: function () { return types_1.OriginType; } });
|
|
10
|
+
Object.defineProperty(exports, "ResultType", { enumerable: true, get: function () { return types_1.ResultType; } });
|
|
11
|
+
Object.defineProperty(exports, "ActorType", { enumerable: true, get: function () { return types_1.ActorType; } });
|
|
12
|
+
// Framework integrations
|
|
13
|
+
var integrations_1 = require("./integrations");
|
|
14
|
+
Object.defineProperty(exports, "TenetCallbackHandler", { enumerable: true, get: function () { return integrations_1.TenetCallbackHandler; } });
|
|
15
|
+
Object.defineProperty(exports, "TenetOpenAITracker", { enumerable: true, get: function () { return integrations_1.TenetOpenAITracker; } });
|
|
16
|
+
Object.defineProperty(exports, "TenetVercelAITracker", { enumerable: true, get: function () { return integrations_1.TenetVercelAITracker; } });
|
|
17
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,mCAA2D;AAAlD,+FAAA,KAAK,OAAA;AAAE,oGAAA,UAAU,OAAA;AAAE,sGAAA,YAAY,OAAA;AACxC,iCA4BiB;AA1Bf,mGAAA,UAAU,OAAA;AACV,mGAAA,UAAU,OAAA;AACV,kGAAA,SAAS,OAAA;AA0BX,yBAAyB;AACzB,+CAIwB;AAHtB,oHAAA,oBAAoB,OAAA;AACpB,kHAAA,kBAAkB,OAAA;AAClB,oHAAA,oBAAoB,OAAA"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Framework integrations for the Tenet AI Node.js SDK.
|
|
3
|
+
*
|
|
4
|
+
* Each integration wraps a popular AI/LLM framework and automatically
|
|
5
|
+
* records agent actions in the Tenet audit trail. None of the integrations
|
|
6
|
+
* have hard dependencies on their respective framework packages -- types are
|
|
7
|
+
* defined inline so the SDK stays lightweight.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* // LangChain.js
|
|
12
|
+
* import { TenetCallbackHandler } from '@tenet-ai/sdk/integrations/langchain';
|
|
13
|
+
*
|
|
14
|
+
* // OpenAI Node SDK
|
|
15
|
+
* import { TenetOpenAITracker } from '@tenet-ai/sdk/integrations/openai';
|
|
16
|
+
*
|
|
17
|
+
* // Vercel AI SDK
|
|
18
|
+
* import { TenetVercelAITracker } from '@tenet-ai/sdk/integrations/vercel-ai';
|
|
19
|
+
*
|
|
20
|
+
* // Or import everything from the barrel:
|
|
21
|
+
* import {
|
|
22
|
+
* TenetCallbackHandler,
|
|
23
|
+
* TenetOpenAITracker,
|
|
24
|
+
* TenetVercelAITracker,
|
|
25
|
+
* } from '@tenet-ai/sdk/integrations';
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @module
|
|
29
|
+
*/
|
|
30
|
+
export { TenetCallbackHandler, type TenetCallbackHandlerConfig, type LangChainAgentAction, type LangChainAgentFinish, type LangChainLLMResult, type LangChainGeneration, type LangChainSerialized, } from "./langchain";
|
|
31
|
+
export { TenetOpenAITracker, type TenetOpenAITrackerConfig, type ChatMessage, type ToolCall, type ChatCompletionCreateParams, type ChatCompletionChoice, type ChatCompletionResponse, type OpenAIClientLike, } from "./openai";
|
|
32
|
+
export { TenetVercelAITracker, type TenetVercelAITrackerConfig, type VercelToolCall, type VercelToolResult, type VercelMessage, type VercelTokenUsage, type VercelGenerateParams, type VercelGenerateTextResult, type VercelStreamTextResult, type GenerateTextFn, type StreamTextFn, } from "./vercel-ai";
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Framework integrations for the Tenet AI Node.js SDK.
|
|
4
|
+
*
|
|
5
|
+
* Each integration wraps a popular AI/LLM framework and automatically
|
|
6
|
+
* records agent actions in the Tenet audit trail. None of the integrations
|
|
7
|
+
* have hard dependencies on their respective framework packages -- types are
|
|
8
|
+
* defined inline so the SDK stays lightweight.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* // LangChain.js
|
|
13
|
+
* import { TenetCallbackHandler } from '@tenet-ai/sdk/integrations/langchain';
|
|
14
|
+
*
|
|
15
|
+
* // OpenAI Node SDK
|
|
16
|
+
* import { TenetOpenAITracker } from '@tenet-ai/sdk/integrations/openai';
|
|
17
|
+
*
|
|
18
|
+
* // Vercel AI SDK
|
|
19
|
+
* import { TenetVercelAITracker } from '@tenet-ai/sdk/integrations/vercel-ai';
|
|
20
|
+
*
|
|
21
|
+
* // Or import everything from the barrel:
|
|
22
|
+
* import {
|
|
23
|
+
* TenetCallbackHandler,
|
|
24
|
+
* TenetOpenAITracker,
|
|
25
|
+
* TenetVercelAITracker,
|
|
26
|
+
* } from '@tenet-ai/sdk/integrations';
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @module
|
|
30
|
+
*/
|
|
31
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
32
|
+
exports.TenetVercelAITracker = exports.TenetOpenAITracker = exports.TenetCallbackHandler = void 0;
|
|
33
|
+
// LangChain.js integration
|
|
34
|
+
var langchain_1 = require("./langchain");
|
|
35
|
+
Object.defineProperty(exports, "TenetCallbackHandler", { enumerable: true, get: function () { return langchain_1.TenetCallbackHandler; } });
|
|
36
|
+
// OpenAI Node SDK integration
|
|
37
|
+
var openai_1 = require("./openai");
|
|
38
|
+
Object.defineProperty(exports, "TenetOpenAITracker", { enumerable: true, get: function () { return openai_1.TenetOpenAITracker; } });
|
|
39
|
+
// Vercel AI SDK integration
|
|
40
|
+
var vercel_ai_1 = require("./vercel-ai");
|
|
41
|
+
Object.defineProperty(exports, "TenetVercelAITracker", { enumerable: true, get: function () { return vercel_ai_1.TenetVercelAITracker; } });
|
|
42
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/integrations/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;;;AAEH,2BAA2B;AAC3B,yCAQqB;AAPnB,iHAAA,oBAAoB,OAAA;AAStB,8BAA8B;AAC9B,mCASkB;AARhB,4GAAA,kBAAkB,OAAA;AAUpB,4BAA4B;AAC5B,yCAYqB;AAXnB,iHAAA,oBAAoB,OAAA"}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LangChain.js integration for Tenet AI.
|
|
3
|
+
*
|
|
4
|
+
* Provides a callback handler that automatically records LangChain agent
|
|
5
|
+
* actions, LLM calls, and tool usage to the Tenet audit trail.
|
|
6
|
+
*
|
|
7
|
+
* This module defines the LangChain callback interface inline so there is
|
|
8
|
+
* no hard dependency on `@langchain/core`. The handler is structurally
|
|
9
|
+
* compatible with LangChain's `BaseCallbackHandler` and can be passed
|
|
10
|
+
* directly to any LangChain executor or chain via the `callbacks` option.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { Tenet } from '@tenet-ai/sdk';
|
|
15
|
+
* import { TenetCallbackHandler } from '@tenet-ai/sdk/integrations/langchain';
|
|
16
|
+
* import { ChatOpenAI } from '@langchain/openai';
|
|
17
|
+
* import { AgentExecutor, createReactAgent } from 'langchain/agents';
|
|
18
|
+
*
|
|
19
|
+
* const tenet = new Tenet({ apiKey: 'tenet_xxx' });
|
|
20
|
+
* const handler = new TenetCallbackHandler(tenet, {
|
|
21
|
+
* agentId: 'langchain-react-agent',
|
|
22
|
+
* tags: ['production'],
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* const executor = AgentExecutor.fromAgentAndTools({
|
|
26
|
+
* agent,
|
|
27
|
+
* tools,
|
|
28
|
+
* callbacks: [handler],
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* // All agent actions are automatically logged to Tenet
|
|
32
|
+
* const result = await executor.invoke({ input: 'What is the weather?' });
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @module
|
|
36
|
+
*/
|
|
37
|
+
import { Tenet } from "../client";
|
|
38
|
+
/** Mirrors `@langchain/core/agents` AgentAction. */
|
|
39
|
+
export interface LangChainAgentAction {
|
|
40
|
+
tool: string;
|
|
41
|
+
toolInput: string | Record<string, unknown>;
|
|
42
|
+
log: string;
|
|
43
|
+
}
|
|
44
|
+
/** Mirrors `@langchain/core/agents` AgentFinish. */
|
|
45
|
+
export interface LangChainAgentFinish {
|
|
46
|
+
returnValues: Record<string, unknown>;
|
|
47
|
+
log: string;
|
|
48
|
+
}
|
|
49
|
+
/** Mirrors a single generation from `@langchain/core/outputs`. */
|
|
50
|
+
export interface LangChainGeneration {
|
|
51
|
+
text: string;
|
|
52
|
+
generationInfo?: Record<string, unknown>;
|
|
53
|
+
}
|
|
54
|
+
/** Mirrors `@langchain/core/outputs` LLMResult. */
|
|
55
|
+
export interface LangChainLLMResult {
|
|
56
|
+
generations: LangChainGeneration[][];
|
|
57
|
+
llmOutput?: Record<string, unknown>;
|
|
58
|
+
}
|
|
59
|
+
/** Mirrors the serialized dict LangChain passes to callbacks. */
|
|
60
|
+
export interface LangChainSerialized {
|
|
61
|
+
_type?: string;
|
|
62
|
+
[key: string]: unknown;
|
|
63
|
+
}
|
|
64
|
+
/** Configuration options for {@link TenetCallbackHandler}. */
|
|
65
|
+
export interface TenetCallbackHandlerConfig {
|
|
66
|
+
/** Identifier for the agent being tracked. Defaults to `"langchain-agent"`. */
|
|
67
|
+
agentId?: string;
|
|
68
|
+
/** Explicit session ID. A random UUID is generated if omitted. */
|
|
69
|
+
sessionId?: string;
|
|
70
|
+
/** Tags to attach to every decision recorded by this handler. */
|
|
71
|
+
tags?: string[];
|
|
72
|
+
/** Arbitrary metadata attached to every decision. */
|
|
73
|
+
metadata?: Record<string, unknown>;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* LangChain callback handler that records agent actions in the Tenet audit
|
|
77
|
+
* trail.
|
|
78
|
+
*
|
|
79
|
+
* The handler accumulates state as the agent runs (LLM calls, tool
|
|
80
|
+
* invocations) and flushes everything to Tenet when the agent finishes
|
|
81
|
+
* (`on_agent_finish`) or when a chain/agent errors (`on_chain_error`).
|
|
82
|
+
*
|
|
83
|
+
* Because the class mirrors the `BaseCallbackHandler` shape from
|
|
84
|
+
* `@langchain/core/callbacks/base`, it can be used directly in any
|
|
85
|
+
* LangChain `callbacks` array without a hard dependency on LangChain
|
|
86
|
+
* packages.
|
|
87
|
+
*/
|
|
88
|
+
export declare class TenetCallbackHandler {
|
|
89
|
+
/** Required by LangChain callback protocol. */
|
|
90
|
+
readonly name = "TenetCallbackHandler";
|
|
91
|
+
private client;
|
|
92
|
+
private agentId;
|
|
93
|
+
private sessionId;
|
|
94
|
+
private tags;
|
|
95
|
+
private metadata;
|
|
96
|
+
private currentContext;
|
|
97
|
+
private actionsTaken;
|
|
98
|
+
private llmOutputs;
|
|
99
|
+
/**
|
|
100
|
+
* Create a new TenetCallbackHandler.
|
|
101
|
+
*
|
|
102
|
+
* @param client - An initialised {@link Tenet} client instance.
|
|
103
|
+
* @param config - Optional configuration overrides.
|
|
104
|
+
*/
|
|
105
|
+
constructor(client: Tenet, config?: TenetCallbackHandlerConfig);
|
|
106
|
+
/**
|
|
107
|
+
* Called when the LLM starts generating.
|
|
108
|
+
* Captures the prompts and serialized LLM config for context.
|
|
109
|
+
*/
|
|
110
|
+
handleLLMStart(serialized: LangChainSerialized, prompts: string[], _runId?: string, _parentRunId?: string, _extraParams?: Record<string, unknown>, _tags?: string[], _metadata?: Record<string, unknown>, _name?: string): Promise<void>;
|
|
111
|
+
/**
|
|
112
|
+
* Called when the LLM finishes generating.
|
|
113
|
+
* Stores the generation text and any LLM-level output metadata.
|
|
114
|
+
*/
|
|
115
|
+
handleLLMEnd(output: LangChainLLMResult, _runId?: string): Promise<void>;
|
|
116
|
+
/**
|
|
117
|
+
* Called when the LLM errors.
|
|
118
|
+
*/
|
|
119
|
+
handleLLMError(_error: Error, _runId?: string): Promise<void>;
|
|
120
|
+
/**
|
|
121
|
+
* Called when the agent selects a tool to invoke.
|
|
122
|
+
* Accumulates the action for later flushing.
|
|
123
|
+
*/
|
|
124
|
+
handleAgentAction(action: LangChainAgentAction, _runId?: string): Promise<void>;
|
|
125
|
+
/**
|
|
126
|
+
* Called when the agent completes. Flushes all accumulated state to Tenet
|
|
127
|
+
* as a single intent -> context -> decision -> execution chain.
|
|
128
|
+
*/
|
|
129
|
+
handleAgentEnd(finish: LangChainAgentFinish, _runId?: string): Promise<void>;
|
|
130
|
+
/**
|
|
131
|
+
* Called when a chain errors. Records the error in Tenet and resets state.
|
|
132
|
+
*/
|
|
133
|
+
handleChainError(error: Error, _runId?: string): Promise<void>;
|
|
134
|
+
handleChainStart(_serialized: LangChainSerialized, _inputs: Record<string, unknown>, _runId?: string): Promise<void>;
|
|
135
|
+
handleChainEnd(_outputs: Record<string, unknown>, _runId?: string): Promise<void>;
|
|
136
|
+
handleToolStart(_serialized: LangChainSerialized, _input: string, _runId?: string): Promise<void>;
|
|
137
|
+
handleToolEnd(_output: string, _runId?: string): Promise<void>;
|
|
138
|
+
handleToolError(_error: Error, _runId?: string): Promise<void>;
|
|
139
|
+
handleText(_text: string, _runId?: string): Promise<void>;
|
|
140
|
+
handleAgentEnd_(_finish: LangChainAgentFinish): Promise<void>;
|
|
141
|
+
/** Reset accumulated state between runs. */
|
|
142
|
+
private resetState;
|
|
143
|
+
/**
|
|
144
|
+
* Flush accumulated state to Tenet on successful agent finish.
|
|
145
|
+
*/
|
|
146
|
+
private flush;
|
|
147
|
+
/**
|
|
148
|
+
* Flush accumulated state to Tenet when the agent/chain errors.
|
|
149
|
+
*/
|
|
150
|
+
private flushError;
|
|
151
|
+
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* LangChain.js integration for Tenet AI.
|
|
4
|
+
*
|
|
5
|
+
* Provides a callback handler that automatically records LangChain agent
|
|
6
|
+
* actions, LLM calls, and tool usage to the Tenet audit trail.
|
|
7
|
+
*
|
|
8
|
+
* This module defines the LangChain callback interface inline so there is
|
|
9
|
+
* no hard dependency on `@langchain/core`. The handler is structurally
|
|
10
|
+
* compatible with LangChain's `BaseCallbackHandler` and can be passed
|
|
11
|
+
* directly to any LangChain executor or chain via the `callbacks` option.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { Tenet } from '@tenet-ai/sdk';
|
|
16
|
+
* import { TenetCallbackHandler } from '@tenet-ai/sdk/integrations/langchain';
|
|
17
|
+
* import { ChatOpenAI } from '@langchain/openai';
|
|
18
|
+
* import { AgentExecutor, createReactAgent } from 'langchain/agents';
|
|
19
|
+
*
|
|
20
|
+
* const tenet = new Tenet({ apiKey: 'tenet_xxx' });
|
|
21
|
+
* const handler = new TenetCallbackHandler(tenet, {
|
|
22
|
+
* agentId: 'langchain-react-agent',
|
|
23
|
+
* tags: ['production'],
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* const executor = AgentExecutor.fromAgentAndTools({
|
|
27
|
+
* agent,
|
|
28
|
+
* tools,
|
|
29
|
+
* callbacks: [handler],
|
|
30
|
+
* });
|
|
31
|
+
*
|
|
32
|
+
* // All agent actions are automatically logged to Tenet
|
|
33
|
+
* const result = await executor.invoke({ input: 'What is the weather?' });
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* @module
|
|
37
|
+
*/
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.TenetCallbackHandler = void 0;
|
|
40
|
+
const crypto_1 = require("crypto");
|
|
41
|
+
const types_1 = require("../types");
|
|
42
|
+
// ---------------------------------------------------------------------------
|
|
43
|
+
// Handler
|
|
44
|
+
// ---------------------------------------------------------------------------
|
|
45
|
+
/**
|
|
46
|
+
* LangChain callback handler that records agent actions in the Tenet audit
|
|
47
|
+
* trail.
|
|
48
|
+
*
|
|
49
|
+
* The handler accumulates state as the agent runs (LLM calls, tool
|
|
50
|
+
* invocations) and flushes everything to Tenet when the agent finishes
|
|
51
|
+
* (`on_agent_finish`) or when a chain/agent errors (`on_chain_error`).
|
|
52
|
+
*
|
|
53
|
+
* Because the class mirrors the `BaseCallbackHandler` shape from
|
|
54
|
+
* `@langchain/core/callbacks/base`, it can be used directly in any
|
|
55
|
+
* LangChain `callbacks` array without a hard dependency on LangChain
|
|
56
|
+
* packages.
|
|
57
|
+
*/
|
|
58
|
+
class TenetCallbackHandler {
|
|
59
|
+
/** Required by LangChain callback protocol. */
|
|
60
|
+
name = "TenetCallbackHandler";
|
|
61
|
+
client;
|
|
62
|
+
agentId;
|
|
63
|
+
sessionId;
|
|
64
|
+
tags;
|
|
65
|
+
metadata;
|
|
66
|
+
// Accumulated state per agent run
|
|
67
|
+
currentContext = {};
|
|
68
|
+
actionsTaken = [];
|
|
69
|
+
llmOutputs = [];
|
|
70
|
+
/**
|
|
71
|
+
* Create a new TenetCallbackHandler.
|
|
72
|
+
*
|
|
73
|
+
* @param client - An initialised {@link Tenet} client instance.
|
|
74
|
+
* @param config - Optional configuration overrides.
|
|
75
|
+
*/
|
|
76
|
+
constructor(client, config = {}) {
|
|
77
|
+
this.client = client;
|
|
78
|
+
this.agentId = config.agentId ?? "langchain-agent";
|
|
79
|
+
this.sessionId = config.sessionId ?? (0, crypto_1.randomUUID)();
|
|
80
|
+
this.tags = config.tags ?? [];
|
|
81
|
+
this.metadata = config.metadata ?? {};
|
|
82
|
+
}
|
|
83
|
+
// ----- LangChain BaseCallbackHandler interface methods -----
|
|
84
|
+
/**
|
|
85
|
+
* Called when the LLM starts generating.
|
|
86
|
+
* Captures the prompts and serialized LLM config for context.
|
|
87
|
+
*/
|
|
88
|
+
async handleLLMStart(serialized, prompts, _runId, _parentRunId, _extraParams, _tags, _metadata, _name) {
|
|
89
|
+
this.currentContext["prompts"] = prompts;
|
|
90
|
+
this.currentContext["llm_config"] = serialized;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Called when the LLM finishes generating.
|
|
94
|
+
* Stores the generation text and any LLM-level output metadata.
|
|
95
|
+
*/
|
|
96
|
+
async handleLLMEnd(output, _runId) {
|
|
97
|
+
this.llmOutputs.push({
|
|
98
|
+
generations: output.generations.map((gen) => gen.map((g) => g.text)),
|
|
99
|
+
llmOutput: output.llmOutput,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Called when the LLM errors.
|
|
104
|
+
*/
|
|
105
|
+
async handleLLMError(_error, _runId) {
|
|
106
|
+
// Errors are handled at chain/agent level
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Called when the agent selects a tool to invoke.
|
|
110
|
+
* Accumulates the action for later flushing.
|
|
111
|
+
*/
|
|
112
|
+
async handleAgentAction(action, _runId) {
|
|
113
|
+
this.actionsTaken.push({
|
|
114
|
+
tool: action.tool,
|
|
115
|
+
toolInput: action.toolInput,
|
|
116
|
+
log: action.log,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Called when the agent completes. Flushes all accumulated state to Tenet
|
|
121
|
+
* as a single intent -> context -> decision -> execution chain.
|
|
122
|
+
*/
|
|
123
|
+
async handleAgentEnd(finish, _runId) {
|
|
124
|
+
await this.flush(finish);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Called when a chain errors. Records the error in Tenet and resets state.
|
|
128
|
+
*/
|
|
129
|
+
async handleChainError(error, _runId) {
|
|
130
|
+
await this.flushError(error);
|
|
131
|
+
}
|
|
132
|
+
// The following no-op stubs satisfy the full BaseCallbackHandler interface
|
|
133
|
+
// so LangChain does not throw when dispatching events.
|
|
134
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
135
|
+
async handleChainStart(_serialized, _inputs, _runId) { }
|
|
136
|
+
async handleChainEnd(_outputs, _runId) { }
|
|
137
|
+
async handleToolStart(_serialized, _input, _runId) { }
|
|
138
|
+
async handleToolEnd(_output, _runId) { }
|
|
139
|
+
async handleToolError(_error, _runId) { }
|
|
140
|
+
async handleText(_text, _runId) { }
|
|
141
|
+
async handleAgentEnd_(_finish) { }
|
|
142
|
+
/* eslint-enable @typescript-eslint/no-unused-vars */
|
|
143
|
+
// ----- Internal helpers -----
|
|
144
|
+
/** Reset accumulated state between runs. */
|
|
145
|
+
resetState() {
|
|
146
|
+
this.currentContext = {};
|
|
147
|
+
this.actionsTaken = [];
|
|
148
|
+
this.llmOutputs = [];
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Flush accumulated state to Tenet on successful agent finish.
|
|
152
|
+
*/
|
|
153
|
+
async flush(finish) {
|
|
154
|
+
// Build action options from accumulated tool calls
|
|
155
|
+
const options = this.actionsTaken.map((action, i) => ({
|
|
156
|
+
action: action.tool,
|
|
157
|
+
score: 1.0 / (i + 1),
|
|
158
|
+
reason: (typeof action.log === "string" ? action.log : "").slice(0, 200),
|
|
159
|
+
}));
|
|
160
|
+
if (options.length === 0) {
|
|
161
|
+
options.push({
|
|
162
|
+
action: "direct_response",
|
|
163
|
+
score: 1.0,
|
|
164
|
+
reason: "Agent responded directly without tool use",
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
const goal = Array.isArray(this.currentContext["prompts"])
|
|
168
|
+
? String(this.currentContext["prompts"][0] ?? "Unknown goal").slice(0, 200)
|
|
169
|
+
: "Unknown goal";
|
|
170
|
+
const chosenAction = this.actionsTaken.length > 0
|
|
171
|
+
? this.actionsTaken[this.actionsTaken.length - 1].tool
|
|
172
|
+
: "direct_response";
|
|
173
|
+
const modelVersion = this.currentContext["llm_config"]?._type
|
|
174
|
+
?? "unknown";
|
|
175
|
+
try {
|
|
176
|
+
// 1. Create intent
|
|
177
|
+
const intent = await this.client.createIntent({
|
|
178
|
+
goal,
|
|
179
|
+
agentId: this.agentId,
|
|
180
|
+
sessionId: this.sessionId,
|
|
181
|
+
origin: types_1.OriginType.AGENT,
|
|
182
|
+
});
|
|
183
|
+
// 2. Snapshot context
|
|
184
|
+
await intent.snapshotContext({
|
|
185
|
+
input: this.currentContext,
|
|
186
|
+
actions_taken: this.actionsTaken,
|
|
187
|
+
llm_outputs: this.llmOutputs,
|
|
188
|
+
});
|
|
189
|
+
// 3. Record decision
|
|
190
|
+
await intent.decide({
|
|
191
|
+
options,
|
|
192
|
+
chosenAction,
|
|
193
|
+
confidence: 0.9,
|
|
194
|
+
modelVersion,
|
|
195
|
+
reasoning: finish.log ? finish.log.slice(0, 500) : "",
|
|
196
|
+
tags: this.tags,
|
|
197
|
+
metadata: this.metadata,
|
|
198
|
+
});
|
|
199
|
+
// 4. Record execution
|
|
200
|
+
await intent.execute({
|
|
201
|
+
action: chosenAction,
|
|
202
|
+
target: finish.returnValues,
|
|
203
|
+
result: types_1.ResultType.SUCCESS,
|
|
204
|
+
sideEffects: this.actionsTaken.map((a) => a.tool),
|
|
205
|
+
actor: types_1.ActorType.AGENT,
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
catch {
|
|
209
|
+
// Swallow errors so the agent run is not interrupted by tracking failures.
|
|
210
|
+
}
|
|
211
|
+
this.resetState();
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Flush accumulated state to Tenet when the agent/chain errors.
|
|
215
|
+
*/
|
|
216
|
+
async flushError(error) {
|
|
217
|
+
if (this.actionsTaken.length === 0 && Object.keys(this.currentContext).length === 0) {
|
|
218
|
+
this.resetState();
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
try {
|
|
222
|
+
const intent = await this.client.createIntent({
|
|
223
|
+
goal: "Error during agent execution",
|
|
224
|
+
agentId: this.agentId,
|
|
225
|
+
sessionId: this.sessionId,
|
|
226
|
+
origin: types_1.OriginType.AGENT,
|
|
227
|
+
});
|
|
228
|
+
await intent.snapshotContext({
|
|
229
|
+
input: this.currentContext,
|
|
230
|
+
error: error.message,
|
|
231
|
+
});
|
|
232
|
+
await intent.decide({
|
|
233
|
+
options: [{ action: "error", score: 0.0, reason: error.message.slice(0, 200) }],
|
|
234
|
+
chosenAction: "error",
|
|
235
|
+
confidence: 0.0,
|
|
236
|
+
modelVersion: "unknown",
|
|
237
|
+
reasoning: `Agent failed with error: ${error.message}`.slice(0, 500),
|
|
238
|
+
tags: this.tags,
|
|
239
|
+
metadata: this.metadata,
|
|
240
|
+
});
|
|
241
|
+
await intent.execute({
|
|
242
|
+
action: "error",
|
|
243
|
+
target: { error: error.message },
|
|
244
|
+
result: types_1.ResultType.FAILURE,
|
|
245
|
+
actor: types_1.ActorType.AGENT,
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
catch {
|
|
249
|
+
// Swallow errors so the original error propagates.
|
|
250
|
+
}
|
|
251
|
+
this.resetState();
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
exports.TenetCallbackHandler = TenetCallbackHandler;
|
|
255
|
+
//# sourceMappingURL=langchain.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"langchain.js","sourceRoot":"","sources":["../../src/integrations/langchain.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;;;AAEH,mCAAoC;AAEpC,oCAA2E;AAsE3E,8EAA8E;AAC9E,UAAU;AACV,8EAA8E;AAE9E;;;;;;;;;;;;GAYG;AACH,MAAa,oBAAoB;IAC/B,+CAA+C;IACtC,IAAI,GAAG,sBAAsB,CAAC;IAE/B,MAAM,CAAQ;IACd,OAAO,CAAS;IAChB,SAAS,CAAS;IAClB,IAAI,CAAW;IACf,QAAQ,CAA0B;IAE1C,kCAAkC;IAC1B,cAAc,GAA4B,EAAE,CAAC;IAC7C,YAAY,GAAmB,EAAE,CAAC;IAClC,UAAU,GAAsB,EAAE,CAAC;IAE3C;;;;;OAKG;IACH,YAAY,MAAa,EAAE,SAAqC,EAAE;QAChE,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,IAAI,iBAAiB,CAAC;QACnD,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,SAAS,IAAI,IAAA,mBAAU,GAAE,CAAC;QAClD,IAAI,CAAC,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