@reminix/langchain 0.0.17 → 0.0.21
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 +21 -34
- package/dist/chat-agent.d.ts +24 -0
- package/dist/chat-agent.d.ts.map +1 -0
- package/dist/chat-agent.js +122 -0
- package/dist/chat-agent.js.map +1 -0
- package/dist/index.d.ts +7 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -1
- package/dist/index.js.map +1 -1
- package/dist/message-utils.d.ts +14 -0
- package/dist/message-utils.d.ts.map +1 -0
- package/dist/message-utils.js +78 -0
- package/dist/message-utils.js.map +1 -0
- package/dist/task-agent.d.ts +22 -0
- package/dist/task-agent.d.ts.map +1 -0
- package/dist/task-agent.js +86 -0
- package/dist/task-agent.js.map +1 -0
- package/dist/thread-agent.d.ts +23 -0
- package/dist/thread-agent.d.ts.map +1 -0
- package/dist/thread-agent.js +83 -0
- package/dist/thread-agent.js.map +1 -0
- package/package.json +4 -4
- package/dist/agent-adapter.d.ts +0 -90
- package/dist/agent-adapter.d.ts.map +0 -1
- package/dist/agent-adapter.js +0 -158
- package/dist/agent-adapter.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @reminix/langchain
|
|
2
2
|
|
|
3
|
-
Reminix Runtime
|
|
3
|
+
Reminix Runtime chat agent for [LangChain](https://js.langchain.com). Serve any LangChain runnable as a REST API.
|
|
4
4
|
|
|
5
5
|
> **Ready to go live?** [Deploy to Reminix Cloud](https://reminix.com/docs/deployment) for zero-config hosting, or [self-host](https://reminix.com/docs/deployment/self-hosting) on your own infrastructure.
|
|
6
6
|
|
|
@@ -16,22 +16,12 @@ This will also install `@reminix/runtime` as a dependency.
|
|
|
16
16
|
|
|
17
17
|
```typescript
|
|
18
18
|
import { ChatOpenAI } from '@langchain/openai';
|
|
19
|
-
import {
|
|
20
|
-
|
|
21
|
-
const llm = new ChatOpenAI({ model: 'gpt-4o' });
|
|
22
|
-
serveAgent(llm, { name: 'my-chatbot', port: 8080 });
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
For more flexibility (e.g., serving multiple agents), use `wrapAgent` and `serve` separately:
|
|
26
|
-
|
|
27
|
-
```typescript
|
|
28
|
-
import { ChatOpenAI } from '@langchain/openai';
|
|
29
|
-
import { wrapAgent } from '@reminix/langchain';
|
|
19
|
+
import { LangChainChatAgent } from '@reminix/langchain';
|
|
30
20
|
import { serve } from '@reminix/runtime';
|
|
31
21
|
|
|
32
22
|
const llm = new ChatOpenAI({ model: 'gpt-4o' });
|
|
33
|
-
const agent =
|
|
34
|
-
serve({ agents: [agent]
|
|
23
|
+
const agent = new LangChainChatAgent(llm, { name: 'my-chatbot' });
|
|
24
|
+
serve({ agents: [agent] });
|
|
35
25
|
```
|
|
36
26
|
|
|
37
27
|
Your agent is now available at:
|
|
@@ -39,34 +29,27 @@ Your agent is now available at:
|
|
|
39
29
|
|
|
40
30
|
## API Reference
|
|
41
31
|
|
|
42
|
-
### `
|
|
32
|
+
### `new LangChainChatAgent(runnable, options)`
|
|
43
33
|
|
|
44
|
-
|
|
34
|
+
Create a LangChain chat agent for use with Reminix Runtime.
|
|
45
35
|
|
|
46
36
|
| Parameter | Type | Default | Description |
|
|
47
37
|
|-----------|------|---------|-------------|
|
|
48
38
|
| `runnable` | `Runnable` | required | Any LangChain runnable (LLM, chain, agent, etc.) |
|
|
49
39
|
| `options.name` | `string` | `"langchain-agent"` | Name for the agent (used in URL path) |
|
|
50
|
-
| `options.
|
|
51
|
-
| `options.
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
Wrap a LangChain runnable for use with Reminix Runtime. Use this with `serve` from `@reminix/runtime` for multi-agent setups.
|
|
56
|
-
|
|
57
|
-
| Parameter | Type | Default | Description |
|
|
58
|
-
|-----------|------|---------|-------------|
|
|
59
|
-
| `runnable` | `Runnable` | required | Any LangChain runnable (LLM, chain, agent, etc.) |
|
|
60
|
-
| `name` | `string` | `"langchain-agent"` | Name for the agent (used in URL path) |
|
|
40
|
+
| `options.description` | `string` | `"langchain chat agent"` | Description shown in agent metadata |
|
|
41
|
+
| `options.instructions` | `string` | — | System instructions prepended to messages |
|
|
42
|
+
| `options.tags` | `string[]` | — | Tags for categorizing/filtering agents |
|
|
43
|
+
| `options.metadata` | `Record<string, unknown>` | — | Custom metadata merged into agent info |
|
|
61
44
|
|
|
62
|
-
**Returns:** `
|
|
45
|
+
**Returns:** `LangChainChatAgent` - A Reminix chat agent instance
|
|
63
46
|
|
|
64
47
|
### Example with a Chain
|
|
65
48
|
|
|
66
49
|
```typescript
|
|
67
50
|
import { ChatOpenAI } from '@langchain/openai';
|
|
68
51
|
import { ChatPromptTemplate } from '@langchain/core/prompts';
|
|
69
|
-
import {
|
|
52
|
+
import { LangChainChatAgent } from '@reminix/langchain';
|
|
70
53
|
import { serve } from '@reminix/runtime';
|
|
71
54
|
|
|
72
55
|
// Create a chain
|
|
@@ -77,9 +60,9 @@ const prompt = ChatPromptTemplate.fromMessages([
|
|
|
77
60
|
const llm = new ChatOpenAI({ model: 'gpt-4o' });
|
|
78
61
|
const chain = prompt.pipe(llm);
|
|
79
62
|
|
|
80
|
-
//
|
|
81
|
-
const agent =
|
|
82
|
-
serve({ agents: [agent]
|
|
63
|
+
// Create agent and serve
|
|
64
|
+
const agent = new LangChainChatAgent(chain, { name: 'my-chain' });
|
|
65
|
+
serve({ agents: [agent] });
|
|
83
66
|
```
|
|
84
67
|
|
|
85
68
|
## Endpoint Input/Output Formats
|
|
@@ -91,7 +74,9 @@ Execute the agent. Input keys are passed directly to the LangChain runnable.
|
|
|
91
74
|
**Request:**
|
|
92
75
|
```json
|
|
93
76
|
{
|
|
94
|
-
"input":
|
|
77
|
+
"input": {
|
|
78
|
+
"input": "Hello, how are you?"
|
|
79
|
+
}
|
|
95
80
|
}
|
|
96
81
|
```
|
|
97
82
|
|
|
@@ -108,7 +93,9 @@ For streaming responses, set `stream: true` in the request:
|
|
|
108
93
|
|
|
109
94
|
```json
|
|
110
95
|
{
|
|
111
|
-
"input":
|
|
96
|
+
"input": {
|
|
97
|
+
"input": "Tell me a story"
|
|
98
|
+
},
|
|
112
99
|
"stream": true
|
|
113
100
|
}
|
|
114
101
|
```
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LangChain chat agent for Reminix Runtime.
|
|
3
|
+
*
|
|
4
|
+
* Accepts a CompiledStateGraph (from createAgent, with tools) or a plain Runnable.
|
|
5
|
+
*/
|
|
6
|
+
import type { Runnable } from '@langchain/core/runnables';
|
|
7
|
+
import { Agent, type AgentRequest, type AgentResponse } from '@reminix/runtime';
|
|
8
|
+
export interface LangChainChatAgentOptions {
|
|
9
|
+
name?: string;
|
|
10
|
+
description?: string;
|
|
11
|
+
instructions?: string;
|
|
12
|
+
tags?: string[];
|
|
13
|
+
metadata?: Record<string, unknown>;
|
|
14
|
+
}
|
|
15
|
+
export declare class LangChainChatAgent extends Agent {
|
|
16
|
+
private agent;
|
|
17
|
+
private _isGraph;
|
|
18
|
+
constructor(agent: Runnable, options?: LangChainChatAgentOptions);
|
|
19
|
+
private buildLangChainInput;
|
|
20
|
+
private extractOutput;
|
|
21
|
+
invoke(request: AgentRequest): Promise<AgentResponse>;
|
|
22
|
+
invokeStream(request: AgentRequest): AsyncGenerator<string, void, unknown>;
|
|
23
|
+
}
|
|
24
|
+
//# sourceMappingURL=chat-agent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chat-agent.d.ts","sourceRoot":"","sources":["../src/chat-agent.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAE1D,OAAO,EACL,KAAK,EAGL,KAAK,YAAY,EACjB,KAAK,aAAa,EACnB,MAAM,kBAAkB,CAAC;AAI1B,MAAM,WAAW,yBAAyB;IACxC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAYD,qBAAa,kBAAmB,SAAQ,KAAK;IAC3C,OAAO,CAAC,KAAK,CAAW;IACxB,OAAO,CAAC,QAAQ,CAAU;gBAEd,KAAK,EAAE,QAAQ,EAAE,OAAO,GAAE,yBAA8B;IAgBpE,OAAO,CAAC,mBAAmB;IA0B3B,OAAO,CAAC,aAAa;IAqBf,MAAM,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC;IAMpD,YAAY,CAAC,OAAO,EAAE,YAAY,GAAG,cAAc,CAAC,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC;CAgClF"}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LangChain chat agent for Reminix Runtime.
|
|
3
|
+
*
|
|
4
|
+
* Accepts a CompiledStateGraph (from createAgent, with tools) or a plain Runnable.
|
|
5
|
+
*/
|
|
6
|
+
import { SystemMessage } from '@langchain/core/messages';
|
|
7
|
+
import { Agent, AGENT_TYPES, buildMessagesFromInput, } from '@reminix/runtime';
|
|
8
|
+
import { toLangChainMessage } from './message-utils.js';
|
|
9
|
+
/**
|
|
10
|
+
* Detect if a runnable is a CompiledStateGraph (from langgraph createAgent).
|
|
11
|
+
*/
|
|
12
|
+
function isCompiledStateGraph(agent) {
|
|
13
|
+
return ('getState' in agent &&
|
|
14
|
+
typeof agent.getState === 'function');
|
|
15
|
+
}
|
|
16
|
+
export class LangChainChatAgent extends Agent {
|
|
17
|
+
agent;
|
|
18
|
+
_isGraph;
|
|
19
|
+
constructor(agent, options = {}) {
|
|
20
|
+
super(options.name ?? 'langchain-agent', {
|
|
21
|
+
description: options.description ?? 'langchain chat agent',
|
|
22
|
+
streaming: true,
|
|
23
|
+
inputSchema: AGENT_TYPES['chat'].inputSchema,
|
|
24
|
+
outputSchema: AGENT_TYPES['chat'].outputSchema,
|
|
25
|
+
type: 'chat',
|
|
26
|
+
framework: 'langchain',
|
|
27
|
+
instructions: options.instructions,
|
|
28
|
+
tags: options.tags,
|
|
29
|
+
metadata: options.metadata,
|
|
30
|
+
});
|
|
31
|
+
this.agent = agent;
|
|
32
|
+
this._isGraph = isCompiledStateGraph(agent);
|
|
33
|
+
}
|
|
34
|
+
buildLangChainInput(request) {
|
|
35
|
+
const messages = buildMessagesFromInput(request);
|
|
36
|
+
if ('messages' in request.input) {
|
|
37
|
+
const lcMessages = messages.map((m) => toLangChainMessage(m));
|
|
38
|
+
if (this.instructions) {
|
|
39
|
+
lcMessages.unshift(new SystemMessage({ content: this.instructions }));
|
|
40
|
+
}
|
|
41
|
+
if (this._isGraph) {
|
|
42
|
+
return { messages: lcMessages };
|
|
43
|
+
}
|
|
44
|
+
return lcMessages;
|
|
45
|
+
}
|
|
46
|
+
else if ('prompt' in request.input) {
|
|
47
|
+
const prompt = request.input.prompt;
|
|
48
|
+
if (this._isGraph) {
|
|
49
|
+
return { messages: [new SystemMessage({ content: String(prompt) })] };
|
|
50
|
+
}
|
|
51
|
+
return prompt;
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
if (this._isGraph) {
|
|
55
|
+
return { messages: [new SystemMessage({ content: JSON.stringify(request.input) })] };
|
|
56
|
+
}
|
|
57
|
+
return request.input;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
extractOutput(response) {
|
|
61
|
+
if (this._isGraph) {
|
|
62
|
+
// CompiledStateGraph returns { messages: BaseMessage[] }
|
|
63
|
+
const messages = response.messages;
|
|
64
|
+
const lastMsg = messages[messages.length - 1];
|
|
65
|
+
if (lastMsg) {
|
|
66
|
+
const content = lastMsg.content;
|
|
67
|
+
return typeof content === 'string' ? content : String(content);
|
|
68
|
+
}
|
|
69
|
+
return '';
|
|
70
|
+
}
|
|
71
|
+
if (response && typeof response === 'object' && 'content' in response) {
|
|
72
|
+
const content = response.content;
|
|
73
|
+
return typeof content === 'string' ? content : String(content);
|
|
74
|
+
}
|
|
75
|
+
else if (response && typeof response === 'object') {
|
|
76
|
+
return response;
|
|
77
|
+
}
|
|
78
|
+
return String(response);
|
|
79
|
+
}
|
|
80
|
+
async invoke(request) {
|
|
81
|
+
const invokeInput = this.buildLangChainInput(request);
|
|
82
|
+
const response = await this.agent.invoke(invokeInput);
|
|
83
|
+
return { output: this.extractOutput(response) };
|
|
84
|
+
}
|
|
85
|
+
async *invokeStream(request) {
|
|
86
|
+
const streamInput = this.buildLangChainInput(request);
|
|
87
|
+
for await (const chunk of await this.agent.stream(streamInput)) {
|
|
88
|
+
let content;
|
|
89
|
+
if (this._isGraph) {
|
|
90
|
+
// Graph streams events — extract text content from message chunks
|
|
91
|
+
if (chunk && typeof chunk === 'object' && 'messages' in chunk) {
|
|
92
|
+
const messages = chunk.messages;
|
|
93
|
+
const lastMsg = messages[messages.length - 1];
|
|
94
|
+
if (lastMsg) {
|
|
95
|
+
content =
|
|
96
|
+
typeof lastMsg.content === 'string' ? lastMsg.content : String(lastMsg.content);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
else if (chunk && typeof chunk === 'object' && 'content' in chunk) {
|
|
107
|
+
content =
|
|
108
|
+
typeof chunk.content === 'string'
|
|
109
|
+
? chunk.content
|
|
110
|
+
: String(chunk.content);
|
|
111
|
+
}
|
|
112
|
+
else if (typeof chunk === 'object') {
|
|
113
|
+
content = JSON.stringify(chunk);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
content = String(chunk);
|
|
117
|
+
}
|
|
118
|
+
yield content;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
//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angChainChatAgentOptions } from './chat-agent.js';
|
|
3
|
+
export { LangChainThreadAgent } from './thread-agent.js';
|
|
4
|
+
export type { LangChainThreadAgentOptions } from './thread-agent.js';
|
|
5
|
+
export { LangChainTaskAgent } from './task-agent.js';
|
|
6
|
+
export type { LangChainTaskAgentOptions } from './task-agent.js';
|
|
7
|
+
export { toLangChainMessage, fromLangChainMessage } from './message-utils.js';
|
|
3
8
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,YAAY,EAAE,yBAAyB,EAAE,MAAM,iBAAiB,CAAC;AACjE,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AACzD,YAAY,EAAE,2BAA2B,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,YAAY,EAAE,yBAAyB,EAAE,MAAM,iBAAiB,CAAC;AACjE,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { LangChainChatAgent } from './chat-agent.js';
|
|
2
|
+
export { LangChainThreadAgent } from './thread-agent.js';
|
|
3
|
+
export { LangChainTaskAgent } from './task-agent.js';
|
|
4
|
+
export { toLangChainMessage, fromLangChainMessage } from './message-utils.js';
|
|
2
5
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAErD,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAEzD,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AAErD,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Message conversion utilities between Reminix and LangChain formats.
|
|
3
|
+
*/
|
|
4
|
+
import { type BaseMessage } from '@langchain/core/messages';
|
|
5
|
+
import { type Message } from '@reminix/runtime';
|
|
6
|
+
/**
|
|
7
|
+
* Convert a Reminix message to a LangChain message.
|
|
8
|
+
*/
|
|
9
|
+
export declare function toLangChainMessage(message: Message): BaseMessage;
|
|
10
|
+
/**
|
|
11
|
+
* Convert a LangChain BaseMessage to a Reminix message.
|
|
12
|
+
*/
|
|
13
|
+
export declare function fromLangChainMessage(lcMessage: BaseMessage): Message;
|
|
14
|
+
//# sourceMappingURL=message-utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message-utils.d.ts","sourceRoot":"","sources":["../src/message-utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAKL,KAAK,WAAW,EACjB,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAwB,KAAK,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAEtE;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,OAAO,EAAE,OAAO,GAAG,WAAW,CA6BhE;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,WAAW,GAAG,OAAO,CAwCpE"}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Message conversion utilities between Reminix and LangChain formats.
|
|
3
|
+
*/
|
|
4
|
+
import { HumanMessage, AIMessage, SystemMessage, ToolMessage, } from '@langchain/core/messages';
|
|
5
|
+
import { messageContentToText } from '@reminix/runtime';
|
|
6
|
+
/**
|
|
7
|
+
* Convert a Reminix message to a LangChain message.
|
|
8
|
+
*/
|
|
9
|
+
export function toLangChainMessage(message) {
|
|
10
|
+
const { role } = message;
|
|
11
|
+
const contentStr = messageContentToText(message.content);
|
|
12
|
+
switch (role) {
|
|
13
|
+
case 'user':
|
|
14
|
+
return new HumanMessage({ content: contentStr });
|
|
15
|
+
case 'assistant': {
|
|
16
|
+
const toolCalls = message.tool_calls?.map((tc) => ({
|
|
17
|
+
id: tc.id,
|
|
18
|
+
name: tc.function.name,
|
|
19
|
+
args: JSON.parse(tc.function.arguments),
|
|
20
|
+
type: 'tool_call',
|
|
21
|
+
}));
|
|
22
|
+
return new AIMessage({
|
|
23
|
+
content: contentStr,
|
|
24
|
+
...(toolCalls && toolCalls.length > 0 && { tool_calls: toolCalls }),
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
case 'system':
|
|
28
|
+
return new SystemMessage({ content: contentStr });
|
|
29
|
+
case 'tool':
|
|
30
|
+
return new ToolMessage({
|
|
31
|
+
content: contentStr,
|
|
32
|
+
tool_call_id: message.tool_call_id || 'unknown',
|
|
33
|
+
});
|
|
34
|
+
default:
|
|
35
|
+
return new HumanMessage({ content: contentStr });
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Convert a LangChain BaseMessage to a Reminix message.
|
|
40
|
+
*/
|
|
41
|
+
export function fromLangChainMessage(lcMessage) {
|
|
42
|
+
const type = lcMessage._getType();
|
|
43
|
+
const content = typeof lcMessage.content === 'string' ? lcMessage.content : JSON.stringify(lcMessage.content);
|
|
44
|
+
switch (type) {
|
|
45
|
+
case 'human':
|
|
46
|
+
return { role: 'user', content };
|
|
47
|
+
case 'ai': {
|
|
48
|
+
const msg = { role: 'assistant', content };
|
|
49
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
50
|
+
const aiMsg = lcMessage;
|
|
51
|
+
if (aiMsg.tool_calls && aiMsg.tool_calls.length > 0) {
|
|
52
|
+
msg.tool_calls = aiMsg.tool_calls.map((tc) => ({
|
|
53
|
+
id: tc.id || 'unknown',
|
|
54
|
+
type: 'function',
|
|
55
|
+
function: {
|
|
56
|
+
name: tc.name,
|
|
57
|
+
arguments: JSON.stringify(tc.args),
|
|
58
|
+
},
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
return msg;
|
|
62
|
+
}
|
|
63
|
+
case 'system':
|
|
64
|
+
return { role: 'system', content };
|
|
65
|
+
case 'tool': {
|
|
66
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
67
|
+
const toolMsg = lcMessage;
|
|
68
|
+
return {
|
|
69
|
+
role: 'tool',
|
|
70
|
+
content,
|
|
71
|
+
tool_call_id: toolMsg.tool_call_id || 'unknown',
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
default:
|
|
75
|
+
return { role: 'user', content };
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
//# sourceMappingURL=message-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"message-utils.js","sourceRoot":"","sources":["../src/message-utils.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,YAAY,EACZ,SAAS,EACT,aAAa,EACb,WAAW,GAEZ,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,oBAAoB,EAAgB,MAAM,kBAAkB,CAAC;AAEtE;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAAC,OAAgB;IACjD,MAAM,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC;IACzB,MAAM,UAAU,GAAG,oBAAoB,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAEzD,QAAQ,IAAI,EAAE,CAAC;QACb,KAAK,MAAM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or a plain Runnable.
|
|
5
|
+
* Returns structured output from a single-shot task execution.
|
|
6
|
+
*/
|
|
7
|
+
import type { Runnable } from '@langchain/core/runnables';
|
|
8
|
+
import { Agent, type AgentRequest, type AgentResponse } from '@reminix/runtime';
|
|
9
|
+
export interface LangChainTaskAgentOptions {
|
|
10
|
+
name?: string;
|
|
11
|
+
description?: string;
|
|
12
|
+
instructions?: string;
|
|
13
|
+
tags?: string[];
|
|
14
|
+
metadata?: Record<string, unknown>;
|
|
15
|
+
}
|
|
16
|
+
export declare class LangChainTaskAgent extends Agent {
|
|
17
|
+
private agent;
|
|
18
|
+
private _isGraph;
|
|
19
|
+
constructor(agent: Runnable, options?: LangChainTaskAgentOptions);
|
|
20
|
+
invoke(request: AgentRequest): Promise<AgentResponse>;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=task-agent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"task-agent.d.ts","sourceRoot":"","sources":["../src/task-agent.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAE1D,OAAO,EAAE,KAAK,EAAe,KAAK,YAAY,EAAE,KAAK,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAE7F,MAAM,WAAW,yBAAyB;IACxC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAYD,qBAAa,kBAAmB,SAAQ,KAAK;IAC3C,OAAO,CAAC,KAAK,CAAW;IACxB,OAAO,CAAC,QAAQ,CAAU;gBAEd,KAAK,EAAE,QAAQ,EAAE,OAAO,GAAE,yBAA8B;IAgB9D,MAAM,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC;CA+C5D"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LangChain task agent for Reminix Runtime.
|
|
3
|
+
*
|
|
4
|
+
* Accepts a CompiledStateGraph (from createAgent, with tools) or a plain Runnable.
|
|
5
|
+
* Returns structured output from a single-shot task execution.
|
|
6
|
+
*/
|
|
7
|
+
import { HumanMessage } from '@langchain/core/messages';
|
|
8
|
+
import { Agent, AGENT_TYPES } from '@reminix/runtime';
|
|
9
|
+
/**
|
|
10
|
+
* Detect if a runnable is a CompiledStateGraph (from langgraph createAgent).
|
|
11
|
+
*/
|
|
12
|
+
function isCompiledStateGraph(agent) {
|
|
13
|
+
return ('getGraph' in agent &&
|
|
14
|
+
typeof agent.getGraph === 'function');
|
|
15
|
+
}
|
|
16
|
+
export class LangChainTaskAgent extends Agent {
|
|
17
|
+
agent;
|
|
18
|
+
_isGraph;
|
|
19
|
+
constructor(agent, options = {}) {
|
|
20
|
+
super(options.name ?? 'langchain-task-agent', {
|
|
21
|
+
description: options.description ?? 'langchain task agent',
|
|
22
|
+
streaming: false,
|
|
23
|
+
inputSchema: AGENT_TYPES['task'].inputSchema,
|
|
24
|
+
outputSchema: AGENT_TYPES['task'].outputSchema,
|
|
25
|
+
type: 'task',
|
|
26
|
+
framework: 'langchain',
|
|
27
|
+
instructions: options.instructions,
|
|
28
|
+
tags: options.tags,
|
|
29
|
+
metadata: options.metadata,
|
|
30
|
+
});
|
|
31
|
+
this.agent = agent;
|
|
32
|
+
this._isGraph = isCompiledStateGraph(agent);
|
|
33
|
+
}
|
|
34
|
+
async invoke(request) {
|
|
35
|
+
const task = request.input.task;
|
|
36
|
+
const prompt = typeof task === 'string' ? task : JSON.stringify(request.input);
|
|
37
|
+
let output;
|
|
38
|
+
if (this._isGraph) {
|
|
39
|
+
// CompiledStateGraph: invoke with { messages } containing the task prompt
|
|
40
|
+
const result = await this.agent.invoke({
|
|
41
|
+
messages: [new HumanMessage({ content: prompt })],
|
|
42
|
+
});
|
|
43
|
+
// Extract structured output from the last AI message
|
|
44
|
+
const messages = result.messages;
|
|
45
|
+
const lastMessage = messages[messages.length - 1];
|
|
46
|
+
const content = lastMessage?.content;
|
|
47
|
+
if (typeof content === 'string') {
|
|
48
|
+
try {
|
|
49
|
+
output = JSON.parse(content);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
output = content;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
output = content;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
// Plain Runnable: invoke directly with the prompt
|
|
61
|
+
const result = await this.agent.invoke(prompt);
|
|
62
|
+
if (result && typeof result === 'object' && 'content' in result) {
|
|
63
|
+
const content = result.content;
|
|
64
|
+
if (typeof content === 'string') {
|
|
65
|
+
try {
|
|
66
|
+
output = JSON.parse(content);
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
output = content;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
output = content;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
else if (result && typeof result === 'object') {
|
|
77
|
+
output = result;
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
output = String(result);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return { output };
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
//# sourceMappingURL=task-agent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"task-agent.js","sourceRoot":"","sources":["../src/task-agent.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAGxD,OAAO,EAAE,KAAK,EAAE,WAAW,EAAyC,MAAM,kBAAkB,CAAC;AAU7F;;GAEG;AACH,SAAS,oBAAoB,CAAC,KAAe;IAC3C,OAAO,CACL,UA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,EAAE,CAAC;IACpB,CAAC;CACF"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LangChain thread agent for Reminix Runtime.
|
|
3
|
+
*
|
|
4
|
+
* Accepts a CompiledStateGraph (from createAgent, with tools) or a plain Runnable.
|
|
5
|
+
* Returns the full message thread including tool calls and results.
|
|
6
|
+
*/
|
|
7
|
+
import type { Runnable } from '@langchain/core/runnables';
|
|
8
|
+
import { Agent, type AgentRequest, type AgentResponse, type StreamEvent } from '@reminix/runtime';
|
|
9
|
+
export interface LangChainThreadAgentOptions {
|
|
10
|
+
name?: string;
|
|
11
|
+
description?: string;
|
|
12
|
+
instructions?: string;
|
|
13
|
+
tags?: string[];
|
|
14
|
+
metadata?: Record<string, unknown>;
|
|
15
|
+
}
|
|
16
|
+
export declare class LangChainThreadAgent extends Agent {
|
|
17
|
+
private agent;
|
|
18
|
+
private _isGraph;
|
|
19
|
+
constructor(agent: Runnable, options?: LangChainThreadAgentOptions);
|
|
20
|
+
invoke(request: AgentRequest): Promise<AgentResponse>;
|
|
21
|
+
invokeStream(request: AgentRequest): AsyncGenerator<string | StreamEvent, void, unknown>;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=thread-agent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"thread-agent.d.ts","sourceRoot":"","sources":["../src/thread-agent.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAE1D,OAAO,EACL,KAAK,EAGL,KAAK,YAAY,EACjB,KAAK,aAAa,EAClB,KAAK,WAAW,EAGjB,MAAM,kBAAkB,CAAC;AAI1B,MAAM,WAAW,2BAA2B;IAC1C,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACpC;AAaD,qBAAa,oBAAqB,SAAQ,KAAK;IAC7C,OAAO,CAAC,KAAK,CAAW;IACxB,OAAO,CAAC,QAAQ,CAAU;gBAEd,KAAK,EAAE,QAAQ,EAAE,OAAO,GAAE,2BAAgC;IAgBhE,MAAM,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC;IAsCpD,YAAY,CAAC,OAAO,EAAE,YAAY,GAAG,cAAc,CAAC,MAAM,GAAG,WAAW,EAAE,IAAI,EAAE,OAAO,CAAC;CAShG"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LangChain thread agent for Reminix Runtime.
|
|
3
|
+
*
|
|
4
|
+
* Accepts a CompiledStateGraph (from createAgent, with tools) or a plain Runnable.
|
|
5
|
+
* Returns the full message thread including tool calls and results.
|
|
6
|
+
*/
|
|
7
|
+
import { SystemMessage } from '@langchain/core/messages';
|
|
8
|
+
import { Agent, AGENT_TYPES, buildMessagesFromInput, } from '@reminix/runtime';
|
|
9
|
+
import { toLangChainMessage, fromLangChainMessage } from './message-utils.js';
|
|
10
|
+
/**
|
|
11
|
+
* Detect if a runnable is a CompiledStateGraph (from langgraph createAgent).
|
|
12
|
+
* CompiledStateGraphs accept { messages: BaseMessage[] } and return { messages: BaseMessage[] }.
|
|
13
|
+
*/
|
|
14
|
+
function isCompiledStateGraph(agent) {
|
|
15
|
+
return ('getState' in agent &&
|
|
16
|
+
typeof agent.getState === 'function');
|
|
17
|
+
}
|
|
18
|
+
export class LangChainThreadAgent extends Agent {
|
|
19
|
+
agent;
|
|
20
|
+
_isGraph;
|
|
21
|
+
constructor(agent, options = {}) {
|
|
22
|
+
super(options.name ?? 'langchain-thread-agent', {
|
|
23
|
+
description: options.description ?? 'langchain thread agent',
|
|
24
|
+
streaming: true,
|
|
25
|
+
inputSchema: AGENT_TYPES['thread'].inputSchema,
|
|
26
|
+
outputSchema: AGENT_TYPES['thread'].outputSchema,
|
|
27
|
+
type: 'thread',
|
|
28
|
+
framework: 'langchain',
|
|
29
|
+
instructions: options.instructions,
|
|
30
|
+
tags: options.tags,
|
|
31
|
+
metadata: options.metadata,
|
|
32
|
+
});
|
|
33
|
+
this.agent = agent;
|
|
34
|
+
this._isGraph = isCompiledStateGraph(agent);
|
|
35
|
+
}
|
|
36
|
+
async invoke(request) {
|
|
37
|
+
const inputMessages = buildMessagesFromInput(request);
|
|
38
|
+
const lcMessages = inputMessages.map((m) => toLangChainMessage(m));
|
|
39
|
+
if (this.instructions) {
|
|
40
|
+
lcMessages.unshift(new SystemMessage({ content: this.instructions }));
|
|
41
|
+
}
|
|
42
|
+
let resultMessages;
|
|
43
|
+
if (this._isGraph) {
|
|
44
|
+
// CompiledStateGraph: invoke with { messages } dict, result is { messages }
|
|
45
|
+
const result = await this.agent.invoke({ messages: lcMessages });
|
|
46
|
+
resultMessages = result.messages;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
// Plain Runnable: invoke with messages array, result is a single message
|
|
50
|
+
const result = await this.agent.invoke(lcMessages);
|
|
51
|
+
if (Array.isArray(result)) {
|
|
52
|
+
resultMessages = result;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
resultMessages = [...lcMessages, result];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
// Convert all result messages to Reminix format
|
|
59
|
+
const output = resultMessages.map((m) => {
|
|
60
|
+
const msg = fromLangChainMessage(m);
|
|
61
|
+
// Strip undefined fields
|
|
62
|
+
const clean = { role: msg.role, content: msg.content };
|
|
63
|
+
if (msg.tool_calls)
|
|
64
|
+
clean.tool_calls = msg.tool_calls;
|
|
65
|
+
if (msg.tool_call_id)
|
|
66
|
+
clean.tool_call_id = msg.tool_call_id;
|
|
67
|
+
if (msg.name)
|
|
68
|
+
clean.name = msg.name;
|
|
69
|
+
return clean;
|
|
70
|
+
});
|
|
71
|
+
return { output };
|
|
72
|
+
}
|
|
73
|
+
async *invokeStream(request) {
|
|
74
|
+
// Reuse invoke logic to get full message thread, then yield each message as an event
|
|
75
|
+
const result = await this.invoke(request);
|
|
76
|
+
const messages = result.output;
|
|
77
|
+
for (const message of messages) {
|
|
78
|
+
const event = { type: 'message', message };
|
|
79
|
+
yield event;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=thread-agent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"thread-agent.js","sourceRoot":"","sources":["../src/thread-agent.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,aAAa,EAAoB,MAAM,0BAA0B,CAAC;AAG3E,OAAO,EACL,KAAK,EACL,WAAW,EACX,sBAAsB,GAMvB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAU9E;;;GAGG;AACH,SAAS,oBAAoB,CAAC,KAAe;IAC3C,OAAO,CACL,UAAU,IAAI,KAAK;QACnB,OAAQ,KAA4C,CAAC,QAAQ,KAAK,UAAU,CAC7E,CAAC;AACJ,CAAC;AAED,MAAM,OAAO,oBAAqB,SAAQ,KAAK;IACrC,KAAK,CAAW;IAChB,QAAQ,CAAU;IAE1B,YAAY,KAAe,EAAE,UAAuC,EAAE;QACpE,KAAK,CAAC,OAAO,CAAC,IAAI,IAAI,wBAAwB,EAAE;YAC9C,WAAW,EAAE,OAAO,CAAC,WAAW,IAAI,wBAAwB;YAC5D,SAAS,EAAE,IAAI;YACf,WAAW,EAAE,WAAW,CAAC,QAAQ,CAAC,CAAC,WAAW;YAC9C,YAAY,EAAE,WAAW,CAAC,QAAQ,CAAC,CAAC,YAAY;YAChD,IAAI,EAAE,QAAQ;YACd,SAAS,EAAE,WAAW;YACtB,YAAY,EAAE,OAAO,CAAC,YAAY;YAClC,IAAI,EAA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AG,GAAG,oBAAoB,CAAC,CAAC,CAAC,CAAC;YACpC,yBAAyB;YACzB,MAAM,KAAK,GAA4B,EAAE,IAAI,EAAE,GAAG,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,CAAC,OAAO,EAAE,CAAC;YAChF,IAAI,GAAG,CAAC,UAAU;gBAAE,KAAK,CAAC,UAAU,GAAG,GAAG,CAAC,UAAU,CAAC;YACtD,IAAI,GAAG,CAAC,YAAY;gBAAE,KAAK,CAAC,YAAY,GAAG,GAAG,CAAC,YAAY,CAAC;YAC5D,IAAI,GAAG,CAAC,IAAI;gBAAE,KAAK,CAAC,IAAI,GAAG,GAAG,CAAC,IAAI,CAAC;YACpC,OAAO,KAAK,CAAC;QACf,CAAC,CAAC,CAAC;QAEH,OAAO,EAAE,MAAM,EAAE,CAAC;IACpB,CAAC;IAED,KAAK,CAAC,CAAC,YAAY,CAAC,OAAqB;QACvC,qFAAqF;QACrF,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QAC1C,MAAM,QAAQ,GAAG,MAAM,CAAC,MAAmB,CAAC;QAC5C,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;YAC/B,MAAM,KAAK,GAAiB,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC;YACzD,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reminix/langchain",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "Reminix
|
|
3
|
+
"version": "0.0.21",
|
|
4
|
+
"description": "Reminix agents for LangChain - serve AI agents as REST APIs",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": {
|
|
7
7
|
"name": "Reminix Team",
|
|
@@ -45,10 +45,10 @@
|
|
|
45
45
|
"LICENSE"
|
|
46
46
|
],
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@reminix/runtime": "^0.0.
|
|
48
|
+
"@reminix/runtime": "^0.0.21"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
|
-
"@langchain/core": ">=1.
|
|
51
|
+
"@langchain/core": ">=1.0.0"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"@langchain/core": "^1.1.12",
|
package/dist/agent-adapter.d.ts
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* LangChain adapter for Reminix Runtime.
|
|
3
|
-
*/
|
|
4
|
-
import type { Runnable } from '@langchain/core/runnables';
|
|
5
|
-
import { AgentAdapter, type ServeOptions, type AgentInvokeRequest, type AgentInvokeResponse } from '@reminix/runtime';
|
|
6
|
-
/**
|
|
7
|
-
* Adapter for LangChain agents and runnables.
|
|
8
|
-
*/
|
|
9
|
-
export declare class LangChainAgentAdapter extends AgentAdapter {
|
|
10
|
-
static adapterName: string;
|
|
11
|
-
private agent;
|
|
12
|
-
private _name;
|
|
13
|
-
/**
|
|
14
|
-
* Initialize the adapter.
|
|
15
|
-
*
|
|
16
|
-
* @param agent - A LangChain runnable (e.g., ChatModel, chain, agent).
|
|
17
|
-
* @param name - Name for the agent.
|
|
18
|
-
*/
|
|
19
|
-
constructor(agent: Runnable, name?: string);
|
|
20
|
-
get name(): string;
|
|
21
|
-
/**
|
|
22
|
-
* Convert a Reminix message to a LangChain message.
|
|
23
|
-
*/
|
|
24
|
-
private toLangChainMessage;
|
|
25
|
-
/**
|
|
26
|
-
* Build LangChain input from invoke request.
|
|
27
|
-
*/
|
|
28
|
-
private buildLangChainInput;
|
|
29
|
-
/**
|
|
30
|
-
* Handle an invoke request.
|
|
31
|
-
*
|
|
32
|
-
* For both task-oriented and chat-style operations. Expects input with 'messages' key
|
|
33
|
-
* or a 'prompt' key for simple text generation.
|
|
34
|
-
*
|
|
35
|
-
* @param request - The invoke request with input data.
|
|
36
|
-
* @returns The invoke response with the output.
|
|
37
|
-
*/
|
|
38
|
-
invoke(request: AgentInvokeRequest): Promise<AgentInvokeResponse>;
|
|
39
|
-
/**
|
|
40
|
-
* Handle a streaming invoke request.
|
|
41
|
-
*
|
|
42
|
-
* @param request - The invoke request with input data.
|
|
43
|
-
* @yields JSON-encoded chunks from the stream.
|
|
44
|
-
*/
|
|
45
|
-
invokeStream(request: AgentInvokeRequest): AsyncGenerator<string, void, unknown>;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Wrap a LangChain agent for use with Reminix Runtime.
|
|
49
|
-
*
|
|
50
|
-
* @param agent - A LangChain runnable (e.g., ChatModel, chain, agent).
|
|
51
|
-
* @param name - Name for the agent.
|
|
52
|
-
* @returns A LangChainAgentAdapter instance.
|
|
53
|
-
*
|
|
54
|
-
* @example
|
|
55
|
-
* ```typescript
|
|
56
|
-
* import { ChatOpenAI } from '@langchain/openai';
|
|
57
|
-
* import { wrap } from '@reminix/langchain';
|
|
58
|
-
* import { serve } from '@reminix/runtime';
|
|
59
|
-
*
|
|
60
|
-
* const llm = new ChatOpenAI({ model: 'gpt-4' });
|
|
61
|
-
* const agent = wrapAgent(llm, 'my-agent');
|
|
62
|
-
* serve({ agents: [agent], port: 8080 });
|
|
63
|
-
* ```
|
|
64
|
-
*/
|
|
65
|
-
export declare function wrapAgent(agent: Runnable, name?: string): LangChainAgentAdapter;
|
|
66
|
-
/**
|
|
67
|
-
* Options for wrapping and serving a LangChain runnable.
|
|
68
|
-
*/
|
|
69
|
-
export interface WrapAndServeOptions extends ServeOptions {
|
|
70
|
-
name?: string;
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Wrap a LangChain runnable and serve it immediately.
|
|
74
|
-
*
|
|
75
|
-
* This is a convenience function that combines `wrapAgent` and `serve` for single-agent setups.
|
|
76
|
-
*
|
|
77
|
-
* @param agent - A LangChain runnable (e.g., ChatModel, chain, agent).
|
|
78
|
-
* @param options - Combined adapter and server options.
|
|
79
|
-
*
|
|
80
|
-
* @example
|
|
81
|
-
* ```typescript
|
|
82
|
-
* import { ChatOpenAI } from '@langchain/openai';
|
|
83
|
-
* import { serveAgent } from '@reminix/langchain';
|
|
84
|
-
*
|
|
85
|
-
* const llm = new ChatOpenAI({ model: 'gpt-4' });
|
|
86
|
-
* serveAgent(llm, { name: 'my-agent', port: 8080 });
|
|
87
|
-
* ```
|
|
88
|
-
*/
|
|
89
|
-
export declare function serveAgent(agent: Runnable, options?: WrapAndServeOptions): void;
|
|
90
|
-
//# sourceMappingURL=agent-adapter.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"agent-adapter.d.ts","sourceRoot":"","sources":["../src/agent-adapter.ts"],"names":[],"mappings":"AAAA;;GAEG;AASH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,2BAA2B,CAAC;AAE1D,OAAO,EACL,YAAY,EAGZ,KAAK,YAAY,EACjB,KAAK,kBAAkB,EACvB,KAAK,mBAAmB,EAEzB,MAAM,kBAAkB,CAAC;AAE1B;;GAEG;AACH,qBAAa,qBAAsB,SAAQ,YAAY;IACrD,MAAM,CAAC,WAAW,SAAe;IAEjC,OAAO,CAAC,KAAK,CAAW;IACxB,OAAO,CAAC,KAAK,CAAS;IAEtB;;;;;OAKG;gBACS,KAAK,EAAE,QAAQ,EAAE,IAAI,GAAE,MAA0B;IAM7D,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAsB1B;;OAEG;IACH,OAAO,CAAC,mBAAmB;IAa3B;;;;;;;;OAQG;IACG,MAAM,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,mBAAmB,CAAC;IAkBvE;;;;;OAKG;IACI,YAAY,CAAC,OAAO,EAAE,kBAAkB,GAAG,cAAc,CAAC,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC;CAgBxF;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,SAAS,CACvB,KAAK,EAAE,QAAQ,EACf,IAAI,GAAE,MAA0B,GAC/B,qBAAqB,CAEvB;AAED;;GAEG;AACH,MAAM,WAAW,mBAAoB,SAAQ,YAAY;IACvD,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,QAAQ,EAAE,OAAO,GAAE,mBAAwB,GAAG,IAAI,CAInF"}
|
package/dist/agent-adapter.js
DELETED
|
@@ -1,158 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* LangChain adapter for Reminix Runtime.
|
|
3
|
-
*/
|
|
4
|
-
import { HumanMessage, AIMessage, SystemMessage, ToolMessage, } from '@langchain/core/messages';
|
|
5
|
-
import { AgentAdapter, serve, messageContentToText, } from '@reminix/runtime';
|
|
6
|
-
/**
|
|
7
|
-
* Adapter for LangChain agents and runnables.
|
|
8
|
-
*/
|
|
9
|
-
export class LangChainAgentAdapter extends AgentAdapter {
|
|
10
|
-
static adapterName = 'langchain';
|
|
11
|
-
agent;
|
|
12
|
-
_name;
|
|
13
|
-
/**
|
|
14
|
-
* Initialize the adapter.
|
|
15
|
-
*
|
|
16
|
-
* @param agent - A LangChain runnable (e.g., ChatModel, chain, agent).
|
|
17
|
-
* @param name - Name for the agent.
|
|
18
|
-
*/
|
|
19
|
-
constructor(agent, name = 'langchain-agent') {
|
|
20
|
-
super();
|
|
21
|
-
this.agent = agent;
|
|
22
|
-
this._name = name;
|
|
23
|
-
}
|
|
24
|
-
get name() {
|
|
25
|
-
return this._name;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Convert a Reminix message to a LangChain message.
|
|
29
|
-
*/
|
|
30
|
-
toLangChainMessage(message) {
|
|
31
|
-
const { role } = message;
|
|
32
|
-
const contentStr = messageContentToText(message.content);
|
|
33
|
-
switch (role) {
|
|
34
|
-
case 'user':
|
|
35
|
-
return new HumanMessage({ content: contentStr });
|
|
36
|
-
case 'assistant':
|
|
37
|
-
return new AIMessage({ content: contentStr });
|
|
38
|
-
case 'system':
|
|
39
|
-
case 'developer':
|
|
40
|
-
return new SystemMessage({ content: contentStr });
|
|
41
|
-
case 'tool':
|
|
42
|
-
return new ToolMessage({
|
|
43
|
-
content: contentStr,
|
|
44
|
-
tool_call_id: message.tool_call_id || 'unknown',
|
|
45
|
-
});
|
|
46
|
-
default:
|
|
47
|
-
return new HumanMessage({ content: contentStr });
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Build LangChain input from invoke request.
|
|
52
|
-
*/
|
|
53
|
-
buildLangChainInput(request) {
|
|
54
|
-
const input = request.input;
|
|
55
|
-
if ('messages' in input) {
|
|
56
|
-
const messages = input.messages;
|
|
57
|
-
return messages.map((m) => this.toLangChainMessage(m));
|
|
58
|
-
}
|
|
59
|
-
else if ('prompt' in input) {
|
|
60
|
-
return input.prompt;
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
return input;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Handle an invoke request.
|
|
68
|
-
*
|
|
69
|
-
* For both task-oriented and chat-style operations. Expects input with 'messages' key
|
|
70
|
-
* or a 'prompt' key for simple text generation.
|
|
71
|
-
*
|
|
72
|
-
* @param request - The invoke request with input data.
|
|
73
|
-
* @returns The invoke response with the output.
|
|
74
|
-
*/
|
|
75
|
-
async invoke(request) {
|
|
76
|
-
const invokeInput = this.buildLangChainInput(request);
|
|
77
|
-
const response = await this.agent.invoke(invokeInput);
|
|
78
|
-
// Extract output from response
|
|
79
|
-
let output;
|
|
80
|
-
if (response && typeof response === 'object' && 'content' in response) {
|
|
81
|
-
output = typeof response.content === 'string' ? response.content : String(response.content);
|
|
82
|
-
}
|
|
83
|
-
else if (response && typeof response === 'object') {
|
|
84
|
-
output = response;
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
output = String(response);
|
|
88
|
-
}
|
|
89
|
-
return { output };
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Handle a streaming invoke request.
|
|
93
|
-
*
|
|
94
|
-
* @param request - The invoke request with input data.
|
|
95
|
-
* @yields JSON-encoded chunks from the stream.
|
|
96
|
-
*/
|
|
97
|
-
async *invokeStream(request) {
|
|
98
|
-
const streamInput = this.buildLangChainInput(request);
|
|
99
|
-
// Stream from the runnable
|
|
100
|
-
for await (const chunk of await this.agent.stream(streamInput)) {
|
|
101
|
-
let content;
|
|
102
|
-
if (chunk && typeof chunk === 'object' && 'content' in chunk) {
|
|
103
|
-
content = typeof chunk.content === 'string' ? chunk.content : String(chunk.content);
|
|
104
|
-
}
|
|
105
|
-
else if (typeof chunk === 'object') {
|
|
106
|
-
content = JSON.stringify(chunk);
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
content = String(chunk);
|
|
110
|
-
}
|
|
111
|
-
yield JSON.stringify({ chunk: content });
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Wrap a LangChain agent for use with Reminix Runtime.
|
|
117
|
-
*
|
|
118
|
-
* @param agent - A LangChain runnable (e.g., ChatModel, chain, agent).
|
|
119
|
-
* @param name - Name for the agent.
|
|
120
|
-
* @returns A LangChainAgentAdapter instance.
|
|
121
|
-
*
|
|
122
|
-
* @example
|
|
123
|
-
* ```typescript
|
|
124
|
-
* import { ChatOpenAI } from '@langchain/openai';
|
|
125
|
-
* import { wrap } from '@reminix/langchain';
|
|
126
|
-
* import { serve } from '@reminix/runtime';
|
|
127
|
-
*
|
|
128
|
-
* const llm = new ChatOpenAI({ model: 'gpt-4' });
|
|
129
|
-
* const agent = wrapAgent(llm, 'my-agent');
|
|
130
|
-
* serve({ agents: [agent], port: 8080 });
|
|
131
|
-
* ```
|
|
132
|
-
*/
|
|
133
|
-
export function wrapAgent(agent, name = 'langchain-agent') {
|
|
134
|
-
return new LangChainAgentAdapter(agent, name);
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Wrap a LangChain runnable and serve it immediately.
|
|
138
|
-
*
|
|
139
|
-
* This is a convenience function that combines `wrapAgent` and `serve` for single-agent setups.
|
|
140
|
-
*
|
|
141
|
-
* @param agent - A LangChain runnable (e.g., ChatModel, chain, agent).
|
|
142
|
-
* @param options - Combined adapter and server options.
|
|
143
|
-
*
|
|
144
|
-
* @example
|
|
145
|
-
* ```typescript
|
|
146
|
-
* import { ChatOpenAI } from '@langchain/openai';
|
|
147
|
-
* import { serveAgent } from '@reminix/langchain';
|
|
148
|
-
*
|
|
149
|
-
* const llm = new ChatOpenAI({ model: 'gpt-4' });
|
|
150
|
-
* serveAgent(llm, { name: 'my-agent', port: 8080 });
|
|
151
|
-
* ```
|
|
152
|
-
*/
|
|
153
|
-
export function serveAgent(agent, options = {}) {
|
|
154
|
-
const { port, hostname, name } = options;
|
|
155
|
-
const wrappedAgent = wrapAgent(agent, name);
|
|
156
|
-
serve({ agents: [wrappedAgent], port, hostname });
|
|
157
|
-
}
|
|
158
|
-
//# sourceMappingURL=agent-adapter.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"agent-adapter.js","sourceRoot":"","sources":["../src/agent-adapter.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,YAAY,EACZ,SAAS,EACT,aAAa,EACb,WAAW,GAEZ,MAAM,0BAA0B,CAAC;AAGlC,OAAO,EACL,YAAY,EACZ,KAAK,EACL,oBAAoB,GAKrB,MAAM,kBAAkB,CAAC;AAE1B;;GAEG;AACH,MAAM,OAAO,qBAAsB,SAAQ,YAAY;IACrD,MAAM,CAAC,WAAW,GAAG,WAAW,CAAC;IAEzB,KAAK,CAAW;IAChB,KAAK,CAAS;IAEtB;;;;;OAKG;IACH,YAAY,KAAe,EAAE,OAAe,iBAAiB;QAC3D,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,KAAK,GAAG,IA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