@vorim/sdk 1.0.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -0
- package/dist/integrations/crewai.cjs +141 -0
- package/dist/integrations/crewai.cjs.map +1 -0
- package/dist/integrations/crewai.d.cts +174 -0
- package/dist/integrations/crewai.d.ts +174 -0
- package/dist/integrations/crewai.js +111 -0
- package/dist/integrations/crewai.js.map +1 -0
- package/dist/integrations/langchain.cjs +197 -0
- package/dist/integrations/langchain.cjs.map +1 -0
- package/dist/integrations/langchain.d.cts +145 -0
- package/dist/integrations/langchain.d.ts +145 -0
- package/dist/integrations/langchain.js +169 -0
- package/dist/integrations/langchain.js.map +1 -0
- package/dist/integrations/llamaindex.cjs +162 -0
- package/dist/integrations/llamaindex.cjs.map +1 -0
- package/dist/integrations/llamaindex.d.cts +131 -0
- package/dist/integrations/llamaindex.d.ts +131 -0
- package/dist/integrations/llamaindex.js +134 -0
- package/dist/integrations/llamaindex.js.map +1 -0
- package/dist/integrations/openai.cjs +208 -0
- package/dist/integrations/openai.cjs.map +1 -0
- package/dist/integrations/openai.d.cts +189 -0
- package/dist/integrations/openai.d.ts +189 -0
- package/dist/integrations/openai.js +181 -0
- package/dist/integrations/openai.js.map +1 -0
- package/package.json +51 -4
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/integrations/langchain.ts
|
|
21
|
+
var langchain_exports = {};
|
|
22
|
+
__export(langchain_exports, {
|
|
23
|
+
VorimCallbackHandler: () => VorimCallbackHandler,
|
|
24
|
+
createVorimAgent: () => createVorimAgent,
|
|
25
|
+
wrapTool: () => wrapTool,
|
|
26
|
+
wrapTools: () => wrapTools
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(langchain_exports);
|
|
29
|
+
function wrapTool(tool, config) {
|
|
30
|
+
const { vorim, agentId, permissionMap = {}, defaultPermission = "agent:execute", asyncAudit = true } = config;
|
|
31
|
+
const originalCall = tool._call.bind(tool);
|
|
32
|
+
tool._call = async function vorimGuardedCall(arg, runManager, parentConfig) {
|
|
33
|
+
const scope = permissionMap[tool.name] ?? defaultPermission;
|
|
34
|
+
const { allowed, reason } = await vorim.check(agentId, scope);
|
|
35
|
+
if (!allowed) {
|
|
36
|
+
const event2 = {
|
|
37
|
+
agent_id: agentId,
|
|
38
|
+
event_type: "tool_call",
|
|
39
|
+
action: tool.name,
|
|
40
|
+
resource: truncate(JSON.stringify(arg), 500),
|
|
41
|
+
permission: scope,
|
|
42
|
+
result: "denied",
|
|
43
|
+
metadata: { reason, framework: "langchain" }
|
|
44
|
+
};
|
|
45
|
+
emitAudit(vorim, event2, asyncAudit);
|
|
46
|
+
throw new Error(`Vorim: permission denied for "${tool.name}" \u2014 scope "${scope}"${reason ? `: ${reason}` : ""}`);
|
|
47
|
+
}
|
|
48
|
+
const start = Date.now();
|
|
49
|
+
let result;
|
|
50
|
+
try {
|
|
51
|
+
result = await originalCall(arg, runManager, parentConfig);
|
|
52
|
+
} catch (err) {
|
|
53
|
+
const event2 = {
|
|
54
|
+
agent_id: agentId,
|
|
55
|
+
event_type: "tool_call",
|
|
56
|
+
action: tool.name,
|
|
57
|
+
resource: truncate(JSON.stringify(arg), 500),
|
|
58
|
+
permission: scope,
|
|
59
|
+
result: "error",
|
|
60
|
+
latency_ms: Date.now() - start,
|
|
61
|
+
error_code: err instanceof Error ? err.name : "UNKNOWN",
|
|
62
|
+
metadata: { error: err instanceof Error ? err.message : String(err), framework: "langchain" }
|
|
63
|
+
};
|
|
64
|
+
emitAudit(vorim, event2, asyncAudit);
|
|
65
|
+
throw err;
|
|
66
|
+
}
|
|
67
|
+
const event = {
|
|
68
|
+
agent_id: agentId,
|
|
69
|
+
event_type: "tool_call",
|
|
70
|
+
action: tool.name,
|
|
71
|
+
resource: truncate(JSON.stringify(arg), 500),
|
|
72
|
+
permission: scope,
|
|
73
|
+
result: "success",
|
|
74
|
+
latency_ms: Date.now() - start,
|
|
75
|
+
metadata: { framework: "langchain" }
|
|
76
|
+
};
|
|
77
|
+
emitAudit(vorim, event, asyncAudit);
|
|
78
|
+
return result;
|
|
79
|
+
};
|
|
80
|
+
return tool;
|
|
81
|
+
}
|
|
82
|
+
function wrapTools(tools, config) {
|
|
83
|
+
return tools.map((t) => wrapTool(t, config));
|
|
84
|
+
}
|
|
85
|
+
var VorimCallbackHandler = class _VorimCallbackHandler {
|
|
86
|
+
name = "VorimCallbackHandler";
|
|
87
|
+
vorim;
|
|
88
|
+
agentId;
|
|
89
|
+
runMap = /* @__PURE__ */ new Map();
|
|
90
|
+
// Flags to satisfy BaseCallbackHandler interface
|
|
91
|
+
ignoreLLM = true;
|
|
92
|
+
ignoreChain = true;
|
|
93
|
+
ignoreAgent = true;
|
|
94
|
+
ignoreRetriever = true;
|
|
95
|
+
ignoreCustomEvent = true;
|
|
96
|
+
lc_serializable = false;
|
|
97
|
+
get lc_id() {
|
|
98
|
+
return ["vorim", "callbacks", "VorimCallbackHandler"];
|
|
99
|
+
}
|
|
100
|
+
get lc_namespace() {
|
|
101
|
+
return ["vorim", "callbacks"];
|
|
102
|
+
}
|
|
103
|
+
constructor(vorim, agentId) {
|
|
104
|
+
this.vorim = vorim;
|
|
105
|
+
this.agentId = agentId;
|
|
106
|
+
}
|
|
107
|
+
async handleToolStart(tool, input, runId, _parentRunId, _tags, _metadata, runName) {
|
|
108
|
+
const toolName = runName ?? tool.id?.[tool.id.length - 1] ?? "unknown";
|
|
109
|
+
this.runMap.set(runId, { tool: toolName, input, startTime: Date.now() });
|
|
110
|
+
}
|
|
111
|
+
async handleToolEnd(_output, runId) {
|
|
112
|
+
const run = this.runMap.get(runId);
|
|
113
|
+
if (!run) return;
|
|
114
|
+
this.runMap.delete(runId);
|
|
115
|
+
await this.vorim.emit({
|
|
116
|
+
agent_id: this.agentId,
|
|
117
|
+
event_type: "tool_call",
|
|
118
|
+
action: run.tool,
|
|
119
|
+
resource: truncate(run.input, 500),
|
|
120
|
+
result: "success",
|
|
121
|
+
latency_ms: Date.now() - run.startTime,
|
|
122
|
+
metadata: { framework: "langchain" }
|
|
123
|
+
}).catch(() => {
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
async handleToolError(err, runId) {
|
|
127
|
+
const run = this.runMap.get(runId);
|
|
128
|
+
if (!run) return;
|
|
129
|
+
this.runMap.delete(runId);
|
|
130
|
+
await this.vorim.emit({
|
|
131
|
+
agent_id: this.agentId,
|
|
132
|
+
event_type: "tool_call",
|
|
133
|
+
action: run.tool,
|
|
134
|
+
resource: truncate(run.input, 500),
|
|
135
|
+
result: "error",
|
|
136
|
+
latency_ms: Date.now() - run.startTime,
|
|
137
|
+
error_code: err.name,
|
|
138
|
+
metadata: { error: err.message, framework: "langchain" }
|
|
139
|
+
}).catch(() => {
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
// Required by BaseCallbackHandler — no-op for events we don't need
|
|
143
|
+
copy() {
|
|
144
|
+
return new _VorimCallbackHandler(this.vorim, this.agentId);
|
|
145
|
+
}
|
|
146
|
+
toJSON() {
|
|
147
|
+
return { lc: 1, type: "not_implemented", id: this.lc_id };
|
|
148
|
+
}
|
|
149
|
+
toJSONNotImplemented() {
|
|
150
|
+
return this.toJSON();
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
async function createVorimAgent(config) {
|
|
154
|
+
const { vorim, name, capabilities, scopes, description, tools: rawTools, permissionMap, defaultPermission, asyncAudit } = config;
|
|
155
|
+
const registration = await vorim.register({
|
|
156
|
+
name,
|
|
157
|
+
description,
|
|
158
|
+
capabilities,
|
|
159
|
+
scopes
|
|
160
|
+
});
|
|
161
|
+
const agentId = registration.agent.agent_id;
|
|
162
|
+
const agentConfig = { vorim, agentId, permissionMap, defaultPermission, asyncAudit };
|
|
163
|
+
const tools = wrapTools(rawTools, agentConfig);
|
|
164
|
+
const callbackHandler = new VorimCallbackHandler(vorim, agentId);
|
|
165
|
+
return {
|
|
166
|
+
/** The Vorim agent_id. */
|
|
167
|
+
agentId,
|
|
168
|
+
/** The full agent registration result. */
|
|
169
|
+
registration,
|
|
170
|
+
/** Tools wrapped with Vorim permission checks + audit. */
|
|
171
|
+
tools,
|
|
172
|
+
/** Callback handler for audit trail observability. */
|
|
173
|
+
callbackHandler,
|
|
174
|
+
/** The private key (store securely — shown once). */
|
|
175
|
+
privateKey: registration.private_key
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
function truncate(str, max) {
|
|
179
|
+
return str.length > max ? str.slice(0, max) + "\u2026" : str;
|
|
180
|
+
}
|
|
181
|
+
function emitAudit(vorim, event, async) {
|
|
182
|
+
if (async) {
|
|
183
|
+
vorim.emit(event).catch(() => {
|
|
184
|
+
});
|
|
185
|
+
} else {
|
|
186
|
+
vorim.emit(event).catch(() => {
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
191
|
+
0 && (module.exports = {
|
|
192
|
+
VorimCallbackHandler,
|
|
193
|
+
createVorimAgent,
|
|
194
|
+
wrapTool,
|
|
195
|
+
wrapTools
|
|
196
|
+
});
|
|
197
|
+
//# sourceMappingURL=langchain.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/langchain.ts"],"sourcesContent":["// ============================================================================\n// VORIM SDK — LangChain / LangGraph JS Integration\n// Provides tool wrapping with permission checks + audit trail,\n// a callback handler for observability, and a factory for creating\n// Vorim-secured LangGraph ReAct agents.\n//\n// Peer dependencies:\n// @langchain/core >=0.3.0\n// @langchain/langgraph >=0.2.0\n// ============================================================================\n\nimport type { VorimSDK } from '../index.js';\nimport type { PermissionScope, AuditEventInput } from '../types.js';\n\n// ─── Re-declared LangChain types (peer dependency — not bundled) ──────────\n// These mirror the actual types from @langchain/core so consumers don't\n// need to import them separately for basic usage.\n\n/** Minimal subset of @langchain/core Serialized */\ninterface Serialized {\n lc: number;\n type: string;\n id: string[];\n kwargs?: Record<string, unknown>;\n}\n\n// ─── Configuration ────────────────────────────────────────────────────────\n\nexport interface VorimLangChainConfig {\n /** Vorim SDK instance. */\n vorim: VorimSDK;\n /** The Vorim agent_id to associate with this LangChain agent. */\n agentId: string;\n /** Map tool names → Vorim permission scopes. Unmapped tools default to 'agent:execute'. */\n permissionMap?: Record<string, PermissionScope>;\n /** Default permission scope for tools not in permissionMap. @default 'agent:execute' */\n defaultPermission?: PermissionScope;\n /** Whether to emit audit events asynchronously (fire-and-forget). @default true */\n asyncAudit?: boolean;\n}\n\n// ─── Tool Wrapper ─────────────────────────────────────────────────────────\n\n/**\n * Wraps a LangChain `StructuredTool` (or `DynamicStructuredTool`) with\n * Vorim permission checks before execution and audit event emission after.\n *\n * Works with any tool that has `name`, `description`, `schema`, and `_call`.\n *\n * @example\n * ```ts\n * import { DynamicStructuredTool } from \"@langchain/core/tools\";\n * import { wrapTool } from \"@vorim/sdk/integrations/langchain\";\n *\n * const guarded = wrapTool(myTool, {\n * vorim, agentId: \"agid_acme_a1b2c3d4\",\n * permissionMap: { search_docs: \"agent:read\" },\n * });\n * ```\n */\nexport function wrapTool<T extends LangChainTool>(\n tool: T,\n config: VorimLangChainConfig,\n): T {\n const { vorim, agentId, permissionMap = {}, defaultPermission = 'agent:execute', asyncAudit = true } = config;\n\n const originalCall = tool._call.bind(tool);\n\n // Override _call to inject permission check + audit\n (tool as any)._call = async function vorimGuardedCall(\n arg: unknown,\n runManager?: unknown,\n parentConfig?: unknown,\n ): Promise<unknown> {\n const scope = permissionMap[tool.name] ?? defaultPermission;\n\n // 1. Permission check\n const { allowed, reason } = await vorim.check(agentId, scope);\n\n if (!allowed) {\n const event: AuditEventInput = {\n agent_id: agentId,\n event_type: 'tool_call',\n action: tool.name,\n resource: truncate(JSON.stringify(arg), 500),\n permission: scope,\n result: 'denied',\n metadata: { reason, framework: 'langchain' },\n };\n emitAudit(vorim, event, asyncAudit);\n throw new Error(`Vorim: permission denied for \"${tool.name}\" — scope \"${scope}\"${reason ? `: ${reason}` : ''}`);\n }\n\n // 2. Execute the original tool\n const start = Date.now();\n let result: unknown;\n try {\n result = await originalCall(arg, runManager, parentConfig);\n } catch (err) {\n const event: AuditEventInput = {\n agent_id: agentId,\n event_type: 'tool_call',\n action: tool.name,\n resource: truncate(JSON.stringify(arg), 500),\n permission: scope,\n result: 'error',\n latency_ms: Date.now() - start,\n error_code: err instanceof Error ? err.name : 'UNKNOWN',\n metadata: { error: err instanceof Error ? err.message : String(err), framework: 'langchain' },\n };\n emitAudit(vorim, event, asyncAudit);\n throw err;\n }\n\n // 3. Audit success\n const event: AuditEventInput = {\n agent_id: agentId,\n event_type: 'tool_call',\n action: tool.name,\n resource: truncate(JSON.stringify(arg), 500),\n permission: scope,\n result: 'success',\n latency_ms: Date.now() - start,\n metadata: { framework: 'langchain' },\n };\n emitAudit(vorim, event, asyncAudit);\n\n return result;\n };\n\n return tool;\n}\n\n/**\n * Wraps an array of LangChain tools with Vorim permission + audit.\n */\nexport function wrapTools<T extends LangChainTool>(\n tools: T[],\n config: VorimLangChainConfig,\n): T[] {\n return tools.map(t => wrapTool(t, config));\n}\n\n// ─── Callback Handler ─────────────────────────────────────────────────────\n\n/**\n * LangChain callback handler that emits Vorim audit events for every\n * tool invocation. This is a **non-intrusive** observability layer —\n * it does not block or modify tool execution.\n *\n * Attach it to any LangChain invoke/stream call:\n * ```ts\n * const handler = new VorimCallbackHandler(vorim, \"agid_acme_a1b2c3d4\");\n * await agent.invoke({ messages }, { callbacks: [handler] });\n * ```\n */\nexport class VorimCallbackHandler {\n name = 'VorimCallbackHandler';\n private vorim: VorimSDK;\n private agentId: string;\n private runMap = new Map<string, { tool: string; input: string; startTime: number }>();\n\n // Flags to satisfy BaseCallbackHandler interface\n ignoreLLM = true;\n ignoreChain = true;\n ignoreAgent = true;\n ignoreRetriever = true;\n ignoreCustomEvent = true;\n lc_serializable = false;\n\n get lc_id(): string[] { return ['vorim', 'callbacks', 'VorimCallbackHandler']; }\n get lc_namespace(): string[] { return ['vorim', 'callbacks']; }\n\n constructor(vorim: VorimSDK, agentId: string) {\n this.vorim = vorim;\n this.agentId = agentId;\n }\n\n async handleToolStart(\n tool: Serialized,\n input: string,\n runId: string,\n _parentRunId?: string,\n _tags?: string[],\n _metadata?: Record<string, unknown>,\n runName?: string,\n ): Promise<void> {\n const toolName = runName ?? tool.id?.[tool.id.length - 1] ?? 'unknown';\n this.runMap.set(runId, { tool: toolName, input, startTime: Date.now() });\n }\n\n async handleToolEnd(\n _output: unknown,\n runId: string,\n ): Promise<void> {\n const run = this.runMap.get(runId);\n if (!run) return;\n this.runMap.delete(runId);\n\n await this.vorim.emit({\n agent_id: this.agentId,\n event_type: 'tool_call',\n action: run.tool,\n resource: truncate(run.input, 500),\n result: 'success',\n latency_ms: Date.now() - run.startTime,\n metadata: { framework: 'langchain' },\n }).catch(() => {}); // never throw from callback\n }\n\n async handleToolError(\n err: Error,\n runId: string,\n ): Promise<void> {\n const run = this.runMap.get(runId);\n if (!run) return;\n this.runMap.delete(runId);\n\n await this.vorim.emit({\n agent_id: this.agentId,\n event_type: 'tool_call',\n action: run.tool,\n resource: truncate(run.input, 500),\n result: 'error',\n latency_ms: Date.now() - run.startTime,\n error_code: err.name,\n metadata: { error: err.message, framework: 'langchain' },\n }).catch(() => {});\n }\n\n // Required by BaseCallbackHandler — no-op for events we don't need\n copy(): VorimCallbackHandler {\n return new VorimCallbackHandler(this.vorim, this.agentId);\n }\n\n toJSON() {\n return { lc: 1, type: 'not_implemented', id: this.lc_id };\n }\n\n toJSONNotImplemented() {\n return this.toJSON();\n }\n}\n\n// ─── Agent Factory ────────────────────────────────────────────────────────\n\nexport interface CreateVorimAgentConfig extends VorimLangChainConfig {\n /** Display name for the agent when registering with Vorim. */\n name: string;\n /** Agent capabilities (e.g. [\"web_search\", \"code_execution\"]). */\n capabilities: string[];\n /** Initial permission scopes to grant. */\n scopes: PermissionScope[];\n /** Optional description. */\n description?: string;\n}\n\n/**\n * Registers a new agent with Vorim and returns wrapped tools + callback handler\n * ready to use with `createReactAgent` or any LangChain agent.\n *\n * @example\n * ```ts\n * import { createReactAgent } from \"@langchain/langgraph/prebuilt\";\n * import { ChatOpenAI } from \"@langchain/openai\";\n * import { createVorimAgent } from \"@vorim/sdk/integrations/langchain\";\n *\n * const { agentId, tools, callbackHandler } = await createVorimAgent({\n * vorim,\n * name: \"research-agent\",\n * capabilities: [\"web_search\"],\n * scopes: [\"agent:read\", \"agent:execute\"],\n * tools: [searchTool, analysisTool],\n * });\n *\n * const agent = createReactAgent({\n * llm: new ChatOpenAI({ model: \"gpt-4o\" }),\n * tools,\n * });\n *\n * const result = await agent.invoke(\n * { messages: [{ role: \"user\", content: \"Research AI trends\" }] },\n * { callbacks: [callbackHandler] },\n * );\n * ```\n */\nexport async function createVorimAgent<T extends LangChainTool>(config: CreateVorimAgentConfig & { tools: T[] }) {\n const { vorim, name, capabilities, scopes, description, tools: rawTools, permissionMap, defaultPermission, asyncAudit } = config;\n\n // Register agent with Vorim\n const registration = await vorim.register({\n name,\n description,\n capabilities,\n scopes,\n });\n\n const agentId = registration.agent.agent_id;\n const agentConfig: VorimLangChainConfig = { vorim, agentId, permissionMap, defaultPermission, asyncAudit };\n\n // Wrap tools with permission checks\n const tools = wrapTools(rawTools, agentConfig);\n\n // Create callback handler for audit trail\n const callbackHandler = new VorimCallbackHandler(vorim, agentId);\n\n return {\n /** The Vorim agent_id. */\n agentId,\n /** The full agent registration result. */\n registration,\n /** Tools wrapped with Vorim permission checks + audit. */\n tools,\n /** Callback handler for audit trail observability. */\n callbackHandler,\n /** The private key (store securely — shown once). */\n privateKey: registration.private_key,\n };\n}\n\n// ─── Internal helpers ─────────────────────────────────────────────────────\n\n/** Minimal interface matching any LangChain StructuredTool / DynamicStructuredTool. */\ninterface LangChainTool {\n name: string;\n description: string;\n schema: unknown;\n _call(arg: any, runManager?: any, parentConfig?: any): Promise<any>;\n}\n\nfunction truncate(str: string, max: number): string {\n return str.length > max ? str.slice(0, max) + '…' : str;\n}\n\nfunction emitAudit(vorim: VorimSDK, event: AuditEventInput, async: boolean): void {\n if (async) {\n vorim.emit(event).catch(() => {});\n } else {\n vorim.emit(event).catch(() => {});\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA4DO,SAAS,SACd,MACA,QACG;AACH,QAAM,EAAE,OAAO,SAAS,gBAAgB,CAAC,GAAG,oBAAoB,iBAAiB,aAAa,KAAK,IAAI;AAEvG,QAAM,eAAe,KAAK,MAAM,KAAK,IAAI;AAGzC,EAAC,KAAa,QAAQ,eAAe,iBACnC,KACA,YACA,cACkB;AAClB,UAAM,QAAQ,cAAc,KAAK,IAAI,KAAK;AAG1C,UAAM,EAAE,SAAS,OAAO,IAAI,MAAM,MAAM,MAAM,SAAS,KAAK;AAE5D,QAAI,CAAC,SAAS;AACZ,YAAMA,SAAyB;AAAA,QAC7B,UAAU;AAAA,QACV,YA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M;AACtD;AAEA,SAAS,UAAU,OAAiB,OAAwB,OAAsB;AAChF,MAAI,OAAO;AACT,UAAM,KAAK,KAAK,EAAE,MAAM,MAAM;AAAA,IAAC,CAAC;AAAA,EAClC,OAAO;AACL,UAAM,KAAK,KAAK,EAAE,MAAM,MAAM;AAAA,IAAC,CAAC;AAAA,EAClC;AACF;","names":["event"]}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { VorimSDK, PermissionScope, AgentRegistrationResult } from '../index.cjs';
|
|
2
|
+
|
|
3
|
+
/** Minimal subset of @langchain/core Serialized */
|
|
4
|
+
interface Serialized {
|
|
5
|
+
lc: number;
|
|
6
|
+
type: string;
|
|
7
|
+
id: string[];
|
|
8
|
+
kwargs?: Record<string, unknown>;
|
|
9
|
+
}
|
|
10
|
+
interface VorimLangChainConfig {
|
|
11
|
+
/** Vorim SDK instance. */
|
|
12
|
+
vorim: VorimSDK;
|
|
13
|
+
/** The Vorim agent_id to associate with this LangChain agent. */
|
|
14
|
+
agentId: string;
|
|
15
|
+
/** Map tool names → Vorim permission scopes. Unmapped tools default to 'agent:execute'. */
|
|
16
|
+
permissionMap?: Record<string, PermissionScope>;
|
|
17
|
+
/** Default permission scope for tools not in permissionMap. @default 'agent:execute' */
|
|
18
|
+
defaultPermission?: PermissionScope;
|
|
19
|
+
/** Whether to emit audit events asynchronously (fire-and-forget). @default true */
|
|
20
|
+
asyncAudit?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Wraps a LangChain `StructuredTool` (or `DynamicStructuredTool`) with
|
|
24
|
+
* Vorim permission checks before execution and audit event emission after.
|
|
25
|
+
*
|
|
26
|
+
* Works with any tool that has `name`, `description`, `schema`, and `_call`.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { DynamicStructuredTool } from "@langchain/core/tools";
|
|
31
|
+
* import { wrapTool } from "@vorim/sdk/integrations/langchain";
|
|
32
|
+
*
|
|
33
|
+
* const guarded = wrapTool(myTool, {
|
|
34
|
+
* vorim, agentId: "agid_acme_a1b2c3d4",
|
|
35
|
+
* permissionMap: { search_docs: "agent:read" },
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
declare function wrapTool<T extends LangChainTool>(tool: T, config: VorimLangChainConfig): T;
|
|
40
|
+
/**
|
|
41
|
+
* Wraps an array of LangChain tools with Vorim permission + audit.
|
|
42
|
+
*/
|
|
43
|
+
declare function wrapTools<T extends LangChainTool>(tools: T[], config: VorimLangChainConfig): T[];
|
|
44
|
+
/**
|
|
45
|
+
* LangChain callback handler that emits Vorim audit events for every
|
|
46
|
+
* tool invocation. This is a **non-intrusive** observability layer —
|
|
47
|
+
* it does not block or modify tool execution.
|
|
48
|
+
*
|
|
49
|
+
* Attach it to any LangChain invoke/stream call:
|
|
50
|
+
* ```ts
|
|
51
|
+
* const handler = new VorimCallbackHandler(vorim, "agid_acme_a1b2c3d4");
|
|
52
|
+
* await agent.invoke({ messages }, { callbacks: [handler] });
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
declare class VorimCallbackHandler {
|
|
56
|
+
name: string;
|
|
57
|
+
private vorim;
|
|
58
|
+
private agentId;
|
|
59
|
+
private runMap;
|
|
60
|
+
ignoreLLM: boolean;
|
|
61
|
+
ignoreChain: boolean;
|
|
62
|
+
ignoreAgent: boolean;
|
|
63
|
+
ignoreRetriever: boolean;
|
|
64
|
+
ignoreCustomEvent: boolean;
|
|
65
|
+
lc_serializable: boolean;
|
|
66
|
+
get lc_id(): string[];
|
|
67
|
+
get lc_namespace(): string[];
|
|
68
|
+
constructor(vorim: VorimSDK, agentId: string);
|
|
69
|
+
handleToolStart(tool: Serialized, input: string, runId: string, _parentRunId?: string, _tags?: string[], _metadata?: Record<string, unknown>, runName?: string): Promise<void>;
|
|
70
|
+
handleToolEnd(_output: unknown, runId: string): Promise<void>;
|
|
71
|
+
handleToolError(err: Error, runId: string): Promise<void>;
|
|
72
|
+
copy(): VorimCallbackHandler;
|
|
73
|
+
toJSON(): {
|
|
74
|
+
lc: number;
|
|
75
|
+
type: string;
|
|
76
|
+
id: string[];
|
|
77
|
+
};
|
|
78
|
+
toJSONNotImplemented(): {
|
|
79
|
+
lc: number;
|
|
80
|
+
type: string;
|
|
81
|
+
id: string[];
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
interface CreateVorimAgentConfig extends VorimLangChainConfig {
|
|
85
|
+
/** Display name for the agent when registering with Vorim. */
|
|
86
|
+
name: string;
|
|
87
|
+
/** Agent capabilities (e.g. ["web_search", "code_execution"]). */
|
|
88
|
+
capabilities: string[];
|
|
89
|
+
/** Initial permission scopes to grant. */
|
|
90
|
+
scopes: PermissionScope[];
|
|
91
|
+
/** Optional description. */
|
|
92
|
+
description?: string;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Registers a new agent with Vorim and returns wrapped tools + callback handler
|
|
96
|
+
* ready to use with `createReactAgent` or any LangChain agent.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* import { createReactAgent } from "@langchain/langgraph/prebuilt";
|
|
101
|
+
* import { ChatOpenAI } from "@langchain/openai";
|
|
102
|
+
* import { createVorimAgent } from "@vorim/sdk/integrations/langchain";
|
|
103
|
+
*
|
|
104
|
+
* const { agentId, tools, callbackHandler } = await createVorimAgent({
|
|
105
|
+
* vorim,
|
|
106
|
+
* name: "research-agent",
|
|
107
|
+
* capabilities: ["web_search"],
|
|
108
|
+
* scopes: ["agent:read", "agent:execute"],
|
|
109
|
+
* tools: [searchTool, analysisTool],
|
|
110
|
+
* });
|
|
111
|
+
*
|
|
112
|
+
* const agent = createReactAgent({
|
|
113
|
+
* llm: new ChatOpenAI({ model: "gpt-4o" }),
|
|
114
|
+
* tools,
|
|
115
|
+
* });
|
|
116
|
+
*
|
|
117
|
+
* const result = await agent.invoke(
|
|
118
|
+
* { messages: [{ role: "user", content: "Research AI trends" }] },
|
|
119
|
+
* { callbacks: [callbackHandler] },
|
|
120
|
+
* );
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
declare function createVorimAgent<T extends LangChainTool>(config: CreateVorimAgentConfig & {
|
|
124
|
+
tools: T[];
|
|
125
|
+
}): Promise<{
|
|
126
|
+
/** The Vorim agent_id. */
|
|
127
|
+
agentId: string;
|
|
128
|
+
/** The full agent registration result. */
|
|
129
|
+
registration: AgentRegistrationResult;
|
|
130
|
+
/** Tools wrapped with Vorim permission checks + audit. */
|
|
131
|
+
tools: T[];
|
|
132
|
+
/** Callback handler for audit trail observability. */
|
|
133
|
+
callbackHandler: VorimCallbackHandler;
|
|
134
|
+
/** The private key (store securely — shown once). */
|
|
135
|
+
privateKey: string;
|
|
136
|
+
}>;
|
|
137
|
+
/** Minimal interface matching any LangChain StructuredTool / DynamicStructuredTool. */
|
|
138
|
+
interface LangChainTool {
|
|
139
|
+
name: string;
|
|
140
|
+
description: string;
|
|
141
|
+
schema: unknown;
|
|
142
|
+
_call(arg: any, runManager?: any, parentConfig?: any): Promise<any>;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export { type CreateVorimAgentConfig, VorimCallbackHandler, type VorimLangChainConfig, createVorimAgent, wrapTool, wrapTools };
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { VorimSDK, PermissionScope, AgentRegistrationResult } from '../index.js';
|
|
2
|
+
|
|
3
|
+
/** Minimal subset of @langchain/core Serialized */
|
|
4
|
+
interface Serialized {
|
|
5
|
+
lc: number;
|
|
6
|
+
type: string;
|
|
7
|
+
id: string[];
|
|
8
|
+
kwargs?: Record<string, unknown>;
|
|
9
|
+
}
|
|
10
|
+
interface VorimLangChainConfig {
|
|
11
|
+
/** Vorim SDK instance. */
|
|
12
|
+
vorim: VorimSDK;
|
|
13
|
+
/** The Vorim agent_id to associate with this LangChain agent. */
|
|
14
|
+
agentId: string;
|
|
15
|
+
/** Map tool names → Vorim permission scopes. Unmapped tools default to 'agent:execute'. */
|
|
16
|
+
permissionMap?: Record<string, PermissionScope>;
|
|
17
|
+
/** Default permission scope for tools not in permissionMap. @default 'agent:execute' */
|
|
18
|
+
defaultPermission?: PermissionScope;
|
|
19
|
+
/** Whether to emit audit events asynchronously (fire-and-forget). @default true */
|
|
20
|
+
asyncAudit?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Wraps a LangChain `StructuredTool` (or `DynamicStructuredTool`) with
|
|
24
|
+
* Vorim permission checks before execution and audit event emission after.
|
|
25
|
+
*
|
|
26
|
+
* Works with any tool that has `name`, `description`, `schema`, and `_call`.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* import { DynamicStructuredTool } from "@langchain/core/tools";
|
|
31
|
+
* import { wrapTool } from "@vorim/sdk/integrations/langchain";
|
|
32
|
+
*
|
|
33
|
+
* const guarded = wrapTool(myTool, {
|
|
34
|
+
* vorim, agentId: "agid_acme_a1b2c3d4",
|
|
35
|
+
* permissionMap: { search_docs: "agent:read" },
|
|
36
|
+
* });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
declare function wrapTool<T extends LangChainTool>(tool: T, config: VorimLangChainConfig): T;
|
|
40
|
+
/**
|
|
41
|
+
* Wraps an array of LangChain tools with Vorim permission + audit.
|
|
42
|
+
*/
|
|
43
|
+
declare function wrapTools<T extends LangChainTool>(tools: T[], config: VorimLangChainConfig): T[];
|
|
44
|
+
/**
|
|
45
|
+
* LangChain callback handler that emits Vorim audit events for every
|
|
46
|
+
* tool invocation. This is a **non-intrusive** observability layer —
|
|
47
|
+
* it does not block or modify tool execution.
|
|
48
|
+
*
|
|
49
|
+
* Attach it to any LangChain invoke/stream call:
|
|
50
|
+
* ```ts
|
|
51
|
+
* const handler = new VorimCallbackHandler(vorim, "agid_acme_a1b2c3d4");
|
|
52
|
+
* await agent.invoke({ messages }, { callbacks: [handler] });
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
declare class VorimCallbackHandler {
|
|
56
|
+
name: string;
|
|
57
|
+
private vorim;
|
|
58
|
+
private agentId;
|
|
59
|
+
private runMap;
|
|
60
|
+
ignoreLLM: boolean;
|
|
61
|
+
ignoreChain: boolean;
|
|
62
|
+
ignoreAgent: boolean;
|
|
63
|
+
ignoreRetriever: boolean;
|
|
64
|
+
ignoreCustomEvent: boolean;
|
|
65
|
+
lc_serializable: boolean;
|
|
66
|
+
get lc_id(): string[];
|
|
67
|
+
get lc_namespace(): string[];
|
|
68
|
+
constructor(vorim: VorimSDK, agentId: string);
|
|
69
|
+
handleToolStart(tool: Serialized, input: string, runId: string, _parentRunId?: string, _tags?: string[], _metadata?: Record<string, unknown>, runName?: string): Promise<void>;
|
|
70
|
+
handleToolEnd(_output: unknown, runId: string): Promise<void>;
|
|
71
|
+
handleToolError(err: Error, runId: string): Promise<void>;
|
|
72
|
+
copy(): VorimCallbackHandler;
|
|
73
|
+
toJSON(): {
|
|
74
|
+
lc: number;
|
|
75
|
+
type: string;
|
|
76
|
+
id: string[];
|
|
77
|
+
};
|
|
78
|
+
toJSONNotImplemented(): {
|
|
79
|
+
lc: number;
|
|
80
|
+
type: string;
|
|
81
|
+
id: string[];
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
interface CreateVorimAgentConfig extends VorimLangChainConfig {
|
|
85
|
+
/** Display name for the agent when registering with Vorim. */
|
|
86
|
+
name: string;
|
|
87
|
+
/** Agent capabilities (e.g. ["web_search", "code_execution"]). */
|
|
88
|
+
capabilities: string[];
|
|
89
|
+
/** Initial permission scopes to grant. */
|
|
90
|
+
scopes: PermissionScope[];
|
|
91
|
+
/** Optional description. */
|
|
92
|
+
description?: string;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Registers a new agent with Vorim and returns wrapped tools + callback handler
|
|
96
|
+
* ready to use with `createReactAgent` or any LangChain agent.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* import { createReactAgent } from "@langchain/langgraph/prebuilt";
|
|
101
|
+
* import { ChatOpenAI } from "@langchain/openai";
|
|
102
|
+
* import { createVorimAgent } from "@vorim/sdk/integrations/langchain";
|
|
103
|
+
*
|
|
104
|
+
* const { agentId, tools, callbackHandler } = await createVorimAgent({
|
|
105
|
+
* vorim,
|
|
106
|
+
* name: "research-agent",
|
|
107
|
+
* capabilities: ["web_search"],
|
|
108
|
+
* scopes: ["agent:read", "agent:execute"],
|
|
109
|
+
* tools: [searchTool, analysisTool],
|
|
110
|
+
* });
|
|
111
|
+
*
|
|
112
|
+
* const agent = createReactAgent({
|
|
113
|
+
* llm: new ChatOpenAI({ model: "gpt-4o" }),
|
|
114
|
+
* tools,
|
|
115
|
+
* });
|
|
116
|
+
*
|
|
117
|
+
* const result = await agent.invoke(
|
|
118
|
+
* { messages: [{ role: "user", content: "Research AI trends" }] },
|
|
119
|
+
* { callbacks: [callbackHandler] },
|
|
120
|
+
* );
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
declare function createVorimAgent<T extends LangChainTool>(config: CreateVorimAgentConfig & {
|
|
124
|
+
tools: T[];
|
|
125
|
+
}): Promise<{
|
|
126
|
+
/** The Vorim agent_id. */
|
|
127
|
+
agentId: string;
|
|
128
|
+
/** The full agent registration result. */
|
|
129
|
+
registration: AgentRegistrationResult;
|
|
130
|
+
/** Tools wrapped with Vorim permission checks + audit. */
|
|
131
|
+
tools: T[];
|
|
132
|
+
/** Callback handler for audit trail observability. */
|
|
133
|
+
callbackHandler: VorimCallbackHandler;
|
|
134
|
+
/** The private key (store securely — shown once). */
|
|
135
|
+
privateKey: string;
|
|
136
|
+
}>;
|
|
137
|
+
/** Minimal interface matching any LangChain StructuredTool / DynamicStructuredTool. */
|
|
138
|
+
interface LangChainTool {
|
|
139
|
+
name: string;
|
|
140
|
+
description: string;
|
|
141
|
+
schema: unknown;
|
|
142
|
+
_call(arg: any, runManager?: any, parentConfig?: any): Promise<any>;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export { type CreateVorimAgentConfig, VorimCallbackHandler, type VorimLangChainConfig, createVorimAgent, wrapTool, wrapTools };
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// src/integrations/langchain.ts
|
|
2
|
+
function wrapTool(tool, config) {
|
|
3
|
+
const { vorim, agentId, permissionMap = {}, defaultPermission = "agent:execute", asyncAudit = true } = config;
|
|
4
|
+
const originalCall = tool._call.bind(tool);
|
|
5
|
+
tool._call = async function vorimGuardedCall(arg, runManager, parentConfig) {
|
|
6
|
+
const scope = permissionMap[tool.name] ?? defaultPermission;
|
|
7
|
+
const { allowed, reason } = await vorim.check(agentId, scope);
|
|
8
|
+
if (!allowed) {
|
|
9
|
+
const event2 = {
|
|
10
|
+
agent_id: agentId,
|
|
11
|
+
event_type: "tool_call",
|
|
12
|
+
action: tool.name,
|
|
13
|
+
resource: truncate(JSON.stringify(arg), 500),
|
|
14
|
+
permission: scope,
|
|
15
|
+
result: "denied",
|
|
16
|
+
metadata: { reason, framework: "langchain" }
|
|
17
|
+
};
|
|
18
|
+
emitAudit(vorim, event2, asyncAudit);
|
|
19
|
+
throw new Error(`Vorim: permission denied for "${tool.name}" \u2014 scope "${scope}"${reason ? `: ${reason}` : ""}`);
|
|
20
|
+
}
|
|
21
|
+
const start = Date.now();
|
|
22
|
+
let result;
|
|
23
|
+
try {
|
|
24
|
+
result = await originalCall(arg, runManager, parentConfig);
|
|
25
|
+
} catch (err) {
|
|
26
|
+
const event2 = {
|
|
27
|
+
agent_id: agentId,
|
|
28
|
+
event_type: "tool_call",
|
|
29
|
+
action: tool.name,
|
|
30
|
+
resource: truncate(JSON.stringify(arg), 500),
|
|
31
|
+
permission: scope,
|
|
32
|
+
result: "error",
|
|
33
|
+
latency_ms: Date.now() - start,
|
|
34
|
+
error_code: err instanceof Error ? err.name : "UNKNOWN",
|
|
35
|
+
metadata: { error: err instanceof Error ? err.message : String(err), framework: "langchain" }
|
|
36
|
+
};
|
|
37
|
+
emitAudit(vorim, event2, asyncAudit);
|
|
38
|
+
throw err;
|
|
39
|
+
}
|
|
40
|
+
const event = {
|
|
41
|
+
agent_id: agentId,
|
|
42
|
+
event_type: "tool_call",
|
|
43
|
+
action: tool.name,
|
|
44
|
+
resource: truncate(JSON.stringify(arg), 500),
|
|
45
|
+
permission: scope,
|
|
46
|
+
result: "success",
|
|
47
|
+
latency_ms: Date.now() - start,
|
|
48
|
+
metadata: { framework: "langchain" }
|
|
49
|
+
};
|
|
50
|
+
emitAudit(vorim, event, asyncAudit);
|
|
51
|
+
return result;
|
|
52
|
+
};
|
|
53
|
+
return tool;
|
|
54
|
+
}
|
|
55
|
+
function wrapTools(tools, config) {
|
|
56
|
+
return tools.map((t) => wrapTool(t, config));
|
|
57
|
+
}
|
|
58
|
+
var VorimCallbackHandler = class _VorimCallbackHandler {
|
|
59
|
+
name = "VorimCallbackHandler";
|
|
60
|
+
vorim;
|
|
61
|
+
agentId;
|
|
62
|
+
runMap = /* @__PURE__ */ new Map();
|
|
63
|
+
// Flags to satisfy BaseCallbackHandler interface
|
|
64
|
+
ignoreLLM = true;
|
|
65
|
+
ignoreChain = true;
|
|
66
|
+
ignoreAgent = true;
|
|
67
|
+
ignoreRetriever = true;
|
|
68
|
+
ignoreCustomEvent = true;
|
|
69
|
+
lc_serializable = false;
|
|
70
|
+
get lc_id() {
|
|
71
|
+
return ["vorim", "callbacks", "VorimCallbackHandler"];
|
|
72
|
+
}
|
|
73
|
+
get lc_namespace() {
|
|
74
|
+
return ["vorim", "callbacks"];
|
|
75
|
+
}
|
|
76
|
+
constructor(vorim, agentId) {
|
|
77
|
+
this.vorim = vorim;
|
|
78
|
+
this.agentId = agentId;
|
|
79
|
+
}
|
|
80
|
+
async handleToolStart(tool, input, runId, _parentRunId, _tags, _metadata, runName) {
|
|
81
|
+
const toolName = runName ?? tool.id?.[tool.id.length - 1] ?? "unknown";
|
|
82
|
+
this.runMap.set(runId, { tool: toolName, input, startTime: Date.now() });
|
|
83
|
+
}
|
|
84
|
+
async handleToolEnd(_output, runId) {
|
|
85
|
+
const run = this.runMap.get(runId);
|
|
86
|
+
if (!run) return;
|
|
87
|
+
this.runMap.delete(runId);
|
|
88
|
+
await this.vorim.emit({
|
|
89
|
+
agent_id: this.agentId,
|
|
90
|
+
event_type: "tool_call",
|
|
91
|
+
action: run.tool,
|
|
92
|
+
resource: truncate(run.input, 500),
|
|
93
|
+
result: "success",
|
|
94
|
+
latency_ms: Date.now() - run.startTime,
|
|
95
|
+
metadata: { framework: "langchain" }
|
|
96
|
+
}).catch(() => {
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
async handleToolError(err, runId) {
|
|
100
|
+
const run = this.runMap.get(runId);
|
|
101
|
+
if (!run) return;
|
|
102
|
+
this.runMap.delete(runId);
|
|
103
|
+
await this.vorim.emit({
|
|
104
|
+
agent_id: this.agentId,
|
|
105
|
+
event_type: "tool_call",
|
|
106
|
+
action: run.tool,
|
|
107
|
+
resource: truncate(run.input, 500),
|
|
108
|
+
result: "error",
|
|
109
|
+
latency_ms: Date.now() - run.startTime,
|
|
110
|
+
error_code: err.name,
|
|
111
|
+
metadata: { error: err.message, framework: "langchain" }
|
|
112
|
+
}).catch(() => {
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
// Required by BaseCallbackHandler — no-op for events we don't need
|
|
116
|
+
copy() {
|
|
117
|
+
return new _VorimCallbackHandler(this.vorim, this.agentId);
|
|
118
|
+
}
|
|
119
|
+
toJSON() {
|
|
120
|
+
return { lc: 1, type: "not_implemented", id: this.lc_id };
|
|
121
|
+
}
|
|
122
|
+
toJSONNotImplemented() {
|
|
123
|
+
return this.toJSON();
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
async function createVorimAgent(config) {
|
|
127
|
+
const { vorim, name, capabilities, scopes, description, tools: rawTools, permissionMap, defaultPermission, asyncAudit } = config;
|
|
128
|
+
const registration = await vorim.register({
|
|
129
|
+
name,
|
|
130
|
+
description,
|
|
131
|
+
capabilities,
|
|
132
|
+
scopes
|
|
133
|
+
});
|
|
134
|
+
const agentId = registration.agent.agent_id;
|
|
135
|
+
const agentConfig = { vorim, agentId, permissionMap, defaultPermission, asyncAudit };
|
|
136
|
+
const tools = wrapTools(rawTools, agentConfig);
|
|
137
|
+
const callbackHandler = new VorimCallbackHandler(vorim, agentId);
|
|
138
|
+
return {
|
|
139
|
+
/** The Vorim agent_id. */
|
|
140
|
+
agentId,
|
|
141
|
+
/** The full agent registration result. */
|
|
142
|
+
registration,
|
|
143
|
+
/** Tools wrapped with Vorim permission checks + audit. */
|
|
144
|
+
tools,
|
|
145
|
+
/** Callback handler for audit trail observability. */
|
|
146
|
+
callbackHandler,
|
|
147
|
+
/** The private key (store securely — shown once). */
|
|
148
|
+
privateKey: registration.private_key
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
function truncate(str, max) {
|
|
152
|
+
return str.length > max ? str.slice(0, max) + "\u2026" : str;
|
|
153
|
+
}
|
|
154
|
+
function emitAudit(vorim, event, async) {
|
|
155
|
+
if (async) {
|
|
156
|
+
vorim.emit(event).catch(() => {
|
|
157
|
+
});
|
|
158
|
+
} else {
|
|
159
|
+
vorim.emit(event).catch(() => {
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
export {
|
|
164
|
+
VorimCallbackHandler,
|
|
165
|
+
createVorimAgent,
|
|
166
|
+
wrapTool,
|
|
167
|
+
wrapTools
|
|
168
|
+
};
|
|
169
|
+
//# sourceMappingURL=langchain.js.map
|