@dvina/agents 0.1.3 → 0.1.5

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/index.cjs CHANGED
@@ -20,10 +20,211 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
+ LangchainAgentFactory: () => LangchainAgentFactory,
23
24
  ToolRegistry: () => ToolRegistry
24
25
  });
25
26
  module.exports = __toCommonJS(index_exports);
26
27
 
28
+ // src/runtime/langchain/factory.ts
29
+ var import_langgraph_checkpoint_postgres = require("@langchain/langgraph-checkpoint-postgres");
30
+ var import_langchain2 = require("langchain");
31
+
32
+ // src/runtime/langchain/agent.ts
33
+ var import_deepagents = require("deepagents");
34
+ var import_langchain = require("langchain");
35
+ var LangchainAgent = class {
36
+ deepAgent;
37
+ toolCalls = [];
38
+ constructor(params) {
39
+ let middlewares = [
40
+ (0, import_langchain.createMiddleware)({
41
+ name: "toolCallsReporter",
42
+ wrapToolCall: async (request, handler) => {
43
+ const res = await handler(request);
44
+ this.toolCalls.push({
45
+ name: request.tool.name || "",
46
+ input: request.toolCall.args,
47
+ output: res.toJSON()
48
+ });
49
+ return res;
50
+ }
51
+ })
52
+ ];
53
+ if (params.middleware) middlewares = [...middlewares, ...params.middleware];
54
+ params.middleware = middlewares;
55
+ this.deepAgent = (0, import_deepagents.createDeepAgent)(params);
56
+ }
57
+ async run(input) {
58
+ this.toolCalls = [];
59
+ const result = await this.deepAgent.invoke({
60
+ messages: input.messages.map(
61
+ (m) => new import_langchain.HumanMessage({
62
+ content: m.content.map((c) => {
63
+ if (c.type === "image") {
64
+ return {
65
+ type: "image_url",
66
+ image_url: { url: c.url }
67
+ };
68
+ }
69
+ return c;
70
+ })
71
+ })
72
+ )
73
+ });
74
+ const lastMessage = result.messages[result.messages.length - 1];
75
+ const usage = lastMessage.usage_metadata || {
76
+ input_tokens: 0,
77
+ output_tokens: 0,
78
+ total_tokens: 0
79
+ };
80
+ return {
81
+ message: lastMessage.content.toString(),
82
+ toolCalls: this.toolCalls,
83
+ usage: {
84
+ promptTokens: usage.input_tokens || 0,
85
+ completionTokens: usage.output_tokens || 0,
86
+ totalTokens: usage.total_tokens || 0
87
+ }
88
+ };
89
+ }
90
+ getLangchainAgent() {
91
+ return this.deepAgent;
92
+ }
93
+ };
94
+
95
+ // src/runtime/langchain/model-resolver.ts
96
+ var import_openai = require("@langchain/openai");
97
+ var LangchainModelResolver = class {
98
+ constructor(config) {
99
+ this.config = config;
100
+ }
101
+ resolve(modelString) {
102
+ const parts = modelString.split(":");
103
+ if (parts.length === 2) {
104
+ const [provider, modelName] = parts;
105
+ return this.resolveByProvider(provider, "default", modelName);
106
+ }
107
+ if (parts.length === 3) {
108
+ const [provider, configName, modelName] = parts;
109
+ return this.resolveByProvider(provider, configName, modelName);
110
+ }
111
+ throw new Error(
112
+ 'Model string must follow format "provider:modelName" (uses "default" config) or "provider:configName:modelName"'
113
+ );
114
+ }
115
+ resolveByProvider(provider, configName, modelName) {
116
+ switch (provider) {
117
+ case "openai":
118
+ return this.resolveOpenAI(configName, modelName);
119
+ case "azure":
120
+ return this.resolveAzure(configName, modelName);
121
+ default:
122
+ throw new Error(`Unsupported model provider: ${provider}`);
123
+ }
124
+ }
125
+ resolveOpenAI(configName, modelName) {
126
+ const providerConfig = this.config.openai?.[configName];
127
+ if (!providerConfig) {
128
+ throw new Error(`Configuration "${configName}" for provider "openai" is missing`);
129
+ }
130
+ return new import_openai.ChatOpenAI({
131
+ apiKey: providerConfig.apiKey,
132
+ modelName
133
+ });
134
+ }
135
+ resolveAzure(configName, deploymentName) {
136
+ const providerConfig = this.config.azure?.[configName];
137
+ if (!providerConfig) {
138
+ throw new Error(`Configuration "${configName}" for provider "azure" is missing`);
139
+ }
140
+ return new import_openai.AzureChatOpenAI({
141
+ azureOpenAIApiKey: providerConfig.apiKey,
142
+ azureOpenAIApiInstanceName: this.extractInstanceName(providerConfig.endpoint),
143
+ azureOpenAIApiDeploymentName: deploymentName,
144
+ azureOpenAIApiVersion: providerConfig.apiVersion
145
+ });
146
+ }
147
+ extractInstanceName(endpoint) {
148
+ try {
149
+ const url = new URL(endpoint);
150
+ return url.hostname.split(".")[0];
151
+ } catch (e) {
152
+ return endpoint;
153
+ }
154
+ }
155
+ };
156
+
157
+ // src/runtime/langchain/factory.ts
158
+ var LangchainAgentFactory = class {
159
+ constructor(modelConfig, postgresSaverConnString, postgresSaverSchema) {
160
+ this.modelConfig = modelConfig;
161
+ this.postgresSaverConnString = postgresSaverConnString;
162
+ this.postgresSaverSchema = postgresSaverSchema;
163
+ this.modelResolver = new LangchainModelResolver(this.modelConfig);
164
+ }
165
+ modelResolver;
166
+ createAgent(options) {
167
+ let deepAgentOptions = {
168
+ model: this.modelResolver.resolve(options.model)
169
+ };
170
+ let middlewares = [];
171
+ if (options.history)
172
+ deepAgentOptions.checkpointer = import_langgraph_checkpoint_postgres.PostgresSaver.fromConnString(this.postgresSaverConnString, {
173
+ schema: this.postgresSaverSchema
174
+ });
175
+ if (options.instructions) deepAgentOptions.systemPrompt = options.instructions;
176
+ if (options.tools) {
177
+ deepAgentOptions.tools = options.tools.tools.map(
178
+ (t) => (0, import_langchain2.tool)(
179
+ async (input, options2) => {
180
+ return await t.exec(input);
181
+ },
182
+ {
183
+ name: t.name,
184
+ description: t.description,
185
+ schema: t.schema
186
+ }
187
+ )
188
+ );
189
+ middlewares.push(
190
+ (0, import_langchain2.llmToolSelectorMiddleware)({
191
+ model: this.modelResolver.resolve(options.tools.model),
192
+ maxTools: 128,
193
+ alwaysInclude: options.tools.alwaysIncludeTools ?? []
194
+ })
195
+ );
196
+ }
197
+ if (options.summarization)
198
+ middlewares.push(
199
+ (0, import_langchain2.summarizationMiddleware)({
200
+ model: this.modelResolver.resolve(options.summarization.model),
201
+ trigger: {
202
+ tokens: options.summarization.triggerAtTokens
203
+ },
204
+ keep: {
205
+ messages: options.summarization.keepMessages
206
+ }
207
+ })
208
+ );
209
+ if (options.handoffs) {
210
+ options.handoffs.every((h) => {
211
+ if (!(h.agent instanceof LangchainAgent)) {
212
+ throw new Error("Handoff agent must be an instance of LangchainAgent");
213
+ }
214
+ });
215
+ deepAgentOptions.subagents = options.handoffs.map((h) => {
216
+ return {
217
+ name: h.name,
218
+ description: h.description,
219
+ runnable: h.agent.getLangchainAgent()
220
+ };
221
+ });
222
+ }
223
+ if (middlewares.length > 0) deepAgentOptions.middleware = middlewares;
224
+ return new LangchainAgent(deepAgentOptions);
225
+ }
226
+ };
227
+
27
228
  // src/core/tools/tool-registry.ts
28
229
  var ToolRegistry = class {
29
230
  entries = /* @__PURE__ */ new Map();
@@ -60,5 +261,6 @@ var ToolRegistry = class {
60
261
  };
61
262
  // Annotate the CommonJS export names for ESM import in node:
62
263
  0 && (module.exports = {
264
+ LangchainAgentFactory,
63
265
  ToolRegistry
64
266
  });
package/dist/index.d.cts CHANGED
@@ -67,6 +67,28 @@ interface AgentFactory {
67
67
  createAgent(options: CreateAgentOptions): Agent;
68
68
  }
69
69
 
70
+ type LangchainOpenAIConfig = {
71
+ apiKey: string;
72
+ };
73
+ type LangchainAzureConfig = {
74
+ apiKey: string;
75
+ endpoint: string;
76
+ apiVersion: string;
77
+ };
78
+ type LangchainModelConfig = {
79
+ openai?: Record<string, LangchainOpenAIConfig>;
80
+ azure?: Record<string, LangchainAzureConfig>;
81
+ };
82
+
83
+ declare class LangchainAgentFactory implements AgentFactory {
84
+ private modelConfig;
85
+ private postgresSaverConnString;
86
+ private postgresSaverSchema;
87
+ private modelResolver;
88
+ constructor(modelConfig: LangchainModelConfig, postgresSaverConnString: string, postgresSaverSchema: string);
89
+ createAgent(options: CreateAgentOptions): Agent;
90
+ }
91
+
70
92
  interface ToolProvider {
71
93
  getTools(): Promise<ToolDefinition[]> | ToolDefinition[];
72
94
  }
@@ -79,4 +101,4 @@ declare class ToolRegistry {
79
101
  invalidate(key: string): void;
80
102
  }
81
103
 
82
- export { type Agent, type AgentFactory, type AgentHandoff, type AgentResult, type AgentRunInput, type ContentBlock, type CreateAgentOptions, type HumanMessage, type Message, type ToolCall, type ToolDefinition, type ToolProvider, ToolRegistry, type UsageMeta };
104
+ export { type Agent, type AgentFactory, type AgentHandoff, type AgentResult, type AgentRunInput, type ContentBlock, type CreateAgentOptions, type HumanMessage, LangchainAgentFactory, type Message, type ToolCall, type ToolDefinition, type ToolProvider, ToolRegistry, type UsageMeta };
package/dist/index.d.ts CHANGED
@@ -67,6 +67,28 @@ interface AgentFactory {
67
67
  createAgent(options: CreateAgentOptions): Agent;
68
68
  }
69
69
 
70
+ type LangchainOpenAIConfig = {
71
+ apiKey: string;
72
+ };
73
+ type LangchainAzureConfig = {
74
+ apiKey: string;
75
+ endpoint: string;
76
+ apiVersion: string;
77
+ };
78
+ type LangchainModelConfig = {
79
+ openai?: Record<string, LangchainOpenAIConfig>;
80
+ azure?: Record<string, LangchainAzureConfig>;
81
+ };
82
+
83
+ declare class LangchainAgentFactory implements AgentFactory {
84
+ private modelConfig;
85
+ private postgresSaverConnString;
86
+ private postgresSaverSchema;
87
+ private modelResolver;
88
+ constructor(modelConfig: LangchainModelConfig, postgresSaverConnString: string, postgresSaverSchema: string);
89
+ createAgent(options: CreateAgentOptions): Agent;
90
+ }
91
+
70
92
  interface ToolProvider {
71
93
  getTools(): Promise<ToolDefinition[]> | ToolDefinition[];
72
94
  }
@@ -79,4 +101,4 @@ declare class ToolRegistry {
79
101
  invalidate(key: string): void;
80
102
  }
81
103
 
82
- export { type Agent, type AgentFactory, type AgentHandoff, type AgentResult, type AgentRunInput, type ContentBlock, type CreateAgentOptions, type HumanMessage, type Message, type ToolCall, type ToolDefinition, type ToolProvider, ToolRegistry, type UsageMeta };
104
+ export { type Agent, type AgentFactory, type AgentHandoff, type AgentResult, type AgentRunInput, type ContentBlock, type CreateAgentOptions, type HumanMessage, LangchainAgentFactory, type Message, type ToolCall, type ToolDefinition, type ToolProvider, ToolRegistry, type UsageMeta };
package/dist/index.js CHANGED
@@ -1,3 +1,203 @@
1
+ // src/runtime/langchain/factory.ts
2
+ import { PostgresSaver } from "@langchain/langgraph-checkpoint-postgres";
3
+ import { llmToolSelectorMiddleware, summarizationMiddleware, tool } from "langchain";
4
+
5
+ // src/runtime/langchain/agent.ts
6
+ import { createDeepAgent } from "deepagents";
7
+ import { createMiddleware, HumanMessage } from "langchain";
8
+ var LangchainAgent = class {
9
+ deepAgent;
10
+ toolCalls = [];
11
+ constructor(params) {
12
+ let middlewares = [
13
+ createMiddleware({
14
+ name: "toolCallsReporter",
15
+ wrapToolCall: async (request, handler) => {
16
+ const res = await handler(request);
17
+ this.toolCalls.push({
18
+ name: request.tool.name || "",
19
+ input: request.toolCall.args,
20
+ output: res.toJSON()
21
+ });
22
+ return res;
23
+ }
24
+ })
25
+ ];
26
+ if (params.middleware) middlewares = [...middlewares, ...params.middleware];
27
+ params.middleware = middlewares;
28
+ this.deepAgent = createDeepAgent(params);
29
+ }
30
+ async run(input) {
31
+ this.toolCalls = [];
32
+ const result = await this.deepAgent.invoke({
33
+ messages: input.messages.map(
34
+ (m) => new HumanMessage({
35
+ content: m.content.map((c) => {
36
+ if (c.type === "image") {
37
+ return {
38
+ type: "image_url",
39
+ image_url: { url: c.url }
40
+ };
41
+ }
42
+ return c;
43
+ })
44
+ })
45
+ )
46
+ });
47
+ const lastMessage = result.messages[result.messages.length - 1];
48
+ const usage = lastMessage.usage_metadata || {
49
+ input_tokens: 0,
50
+ output_tokens: 0,
51
+ total_tokens: 0
52
+ };
53
+ return {
54
+ message: lastMessage.content.toString(),
55
+ toolCalls: this.toolCalls,
56
+ usage: {
57
+ promptTokens: usage.input_tokens || 0,
58
+ completionTokens: usage.output_tokens || 0,
59
+ totalTokens: usage.total_tokens || 0
60
+ }
61
+ };
62
+ }
63
+ getLangchainAgent() {
64
+ return this.deepAgent;
65
+ }
66
+ };
67
+
68
+ // src/runtime/langchain/model-resolver.ts
69
+ import { AzureChatOpenAI, ChatOpenAI } from "@langchain/openai";
70
+ var LangchainModelResolver = class {
71
+ constructor(config) {
72
+ this.config = config;
73
+ }
74
+ resolve(modelString) {
75
+ const parts = modelString.split(":");
76
+ if (parts.length === 2) {
77
+ const [provider, modelName] = parts;
78
+ return this.resolveByProvider(provider, "default", modelName);
79
+ }
80
+ if (parts.length === 3) {
81
+ const [provider, configName, modelName] = parts;
82
+ return this.resolveByProvider(provider, configName, modelName);
83
+ }
84
+ throw new Error(
85
+ 'Model string must follow format "provider:modelName" (uses "default" config) or "provider:configName:modelName"'
86
+ );
87
+ }
88
+ resolveByProvider(provider, configName, modelName) {
89
+ switch (provider) {
90
+ case "openai":
91
+ return this.resolveOpenAI(configName, modelName);
92
+ case "azure":
93
+ return this.resolveAzure(configName, modelName);
94
+ default:
95
+ throw new Error(`Unsupported model provider: ${provider}`);
96
+ }
97
+ }
98
+ resolveOpenAI(configName, modelName) {
99
+ const providerConfig = this.config.openai?.[configName];
100
+ if (!providerConfig) {
101
+ throw new Error(`Configuration "${configName}" for provider "openai" is missing`);
102
+ }
103
+ return new ChatOpenAI({
104
+ apiKey: providerConfig.apiKey,
105
+ modelName
106
+ });
107
+ }
108
+ resolveAzure(configName, deploymentName) {
109
+ const providerConfig = this.config.azure?.[configName];
110
+ if (!providerConfig) {
111
+ throw new Error(`Configuration "${configName}" for provider "azure" is missing`);
112
+ }
113
+ return new AzureChatOpenAI({
114
+ azureOpenAIApiKey: providerConfig.apiKey,
115
+ azureOpenAIApiInstanceName: this.extractInstanceName(providerConfig.endpoint),
116
+ azureOpenAIApiDeploymentName: deploymentName,
117
+ azureOpenAIApiVersion: providerConfig.apiVersion
118
+ });
119
+ }
120
+ extractInstanceName(endpoint) {
121
+ try {
122
+ const url = new URL(endpoint);
123
+ return url.hostname.split(".")[0];
124
+ } catch (e) {
125
+ return endpoint;
126
+ }
127
+ }
128
+ };
129
+
130
+ // src/runtime/langchain/factory.ts
131
+ var LangchainAgentFactory = class {
132
+ constructor(modelConfig, postgresSaverConnString, postgresSaverSchema) {
133
+ this.modelConfig = modelConfig;
134
+ this.postgresSaverConnString = postgresSaverConnString;
135
+ this.postgresSaverSchema = postgresSaverSchema;
136
+ this.modelResolver = new LangchainModelResolver(this.modelConfig);
137
+ }
138
+ modelResolver;
139
+ createAgent(options) {
140
+ let deepAgentOptions = {
141
+ model: this.modelResolver.resolve(options.model)
142
+ };
143
+ let middlewares = [];
144
+ if (options.history)
145
+ deepAgentOptions.checkpointer = PostgresSaver.fromConnString(this.postgresSaverConnString, {
146
+ schema: this.postgresSaverSchema
147
+ });
148
+ if (options.instructions) deepAgentOptions.systemPrompt = options.instructions;
149
+ if (options.tools) {
150
+ deepAgentOptions.tools = options.tools.tools.map(
151
+ (t) => tool(
152
+ async (input, options2) => {
153
+ return await t.exec(input);
154
+ },
155
+ {
156
+ name: t.name,
157
+ description: t.description,
158
+ schema: t.schema
159
+ }
160
+ )
161
+ );
162
+ middlewares.push(
163
+ llmToolSelectorMiddleware({
164
+ model: this.modelResolver.resolve(options.tools.model),
165
+ maxTools: 128,
166
+ alwaysInclude: options.tools.alwaysIncludeTools ?? []
167
+ })
168
+ );
169
+ }
170
+ if (options.summarization)
171
+ middlewares.push(
172
+ summarizationMiddleware({
173
+ model: this.modelResolver.resolve(options.summarization.model),
174
+ trigger: {
175
+ tokens: options.summarization.triggerAtTokens
176
+ },
177
+ keep: {
178
+ messages: options.summarization.keepMessages
179
+ }
180
+ })
181
+ );
182
+ if (options.handoffs) {
183
+ options.handoffs.every((h) => {
184
+ if (!(h.agent instanceof LangchainAgent)) {
185
+ throw new Error("Handoff agent must be an instance of LangchainAgent");
186
+ }
187
+ });
188
+ deepAgentOptions.subagents = options.handoffs.map((h) => {
189
+ return {
190
+ name: h.name,
191
+ description: h.description,
192
+ runnable: h.agent.getLangchainAgent()
193
+ };
194
+ });
195
+ }
196
+ if (middlewares.length > 0) deepAgentOptions.middleware = middlewares;
197
+ return new LangchainAgent(deepAgentOptions);
198
+ }
199
+ };
200
+
1
201
  // src/core/tools/tool-registry.ts
2
202
  var ToolRegistry = class {
3
203
  entries = /* @__PURE__ */ new Map();
@@ -33,5 +233,6 @@ var ToolRegistry = class {
33
233
  }
34
234
  };
35
235
  export {
236
+ LangchainAgentFactory,
36
237
  ToolRegistry
37
238
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dvina/agents",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Standalone, provider-agnostic AI agent library",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -29,7 +29,7 @@
29
29
  "node": ">=24"
30
30
  },
31
31
  "dependencies": {
32
- "@langchain/core": "1.1.4",
32
+ "@langchain/core": "1.1.8",
33
33
  "@langchain/langgraph": "1.0.2",
34
34
  "@langchain/langgraph-checkpoint-postgres": "^1.0.0",
35
35
  "@langchain/openai": "^1.2.0",
@@ -1,48 +0,0 @@
1
- var __create = Object.create;
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
- var __hasOwnProp = Object.prototype.hasOwnProperty;
7
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
8
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
9
- }) : x)(function(x) {
10
- if (typeof require !== "undefined") return require.apply(this, arguments);
11
- throw Error('Dynamic require of "' + x + '" is not supported');
12
- });
13
- var __esm = (fn, res) => function __init() {
14
- return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
15
- };
16
- var __commonJS = (cb, mod) => function __require2() {
17
- return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
18
- };
19
- var __export = (target, all) => {
20
- for (var name in all)
21
- __defProp(target, name, { get: all[name], enumerable: true });
22
- };
23
- var __copyProps = (to, from, except, desc) => {
24
- if (from && typeof from === "object" || typeof from === "function") {
25
- for (let key of __getOwnPropNames(from))
26
- if (!__hasOwnProp.call(to, key) && key !== except)
27
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
28
- }
29
- return to;
30
- };
31
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
32
- // If the importer is in node compatibility mode or this is not an ESM
33
- // file that has been converted to a CommonJS file using a Babel-
34
- // compatible transform (i.e. "__esModule" has not been set), then set
35
- // "default" to the CommonJS "module.exports" for node compatibility.
36
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
37
- mod
38
- ));
39
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
40
-
41
- export {
42
- __require,
43
- __esm,
44
- __commonJS,
45
- __export,
46
- __toESM,
47
- __toCommonJS
48
- };