@librechat/agents 3.0.0-rc12 → 3.0.0-rc13

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.
Files changed (51) hide show
  1. package/dist/cjs/instrumentation.cjs +21 -0
  2. package/dist/cjs/instrumentation.cjs.map +1 -0
  3. package/dist/cjs/llm/openai/index.cjs +0 -4
  4. package/dist/cjs/llm/openai/index.cjs.map +1 -1
  5. package/dist/cjs/main.cjs +1 -0
  6. package/dist/cjs/main.cjs.map +1 -1
  7. package/dist/cjs/run.cjs +57 -5
  8. package/dist/cjs/run.cjs.map +1 -1
  9. package/dist/cjs/stream.cjs +7 -0
  10. package/dist/cjs/stream.cjs.map +1 -1
  11. package/dist/cjs/utils/misc.cjs +8 -1
  12. package/dist/cjs/utils/misc.cjs.map +1 -1
  13. package/dist/cjs/utils/title.cjs +54 -25
  14. package/dist/cjs/utils/title.cjs.map +1 -1
  15. package/dist/esm/instrumentation.mjs +19 -0
  16. package/dist/esm/instrumentation.mjs.map +1 -0
  17. package/dist/esm/llm/openai/index.mjs +0 -4
  18. package/dist/esm/llm/openai/index.mjs.map +1 -1
  19. package/dist/esm/main.mjs +1 -1
  20. package/dist/esm/run.mjs +57 -5
  21. package/dist/esm/run.mjs.map +1 -1
  22. package/dist/esm/stream.mjs +7 -0
  23. package/dist/esm/stream.mjs.map +1 -1
  24. package/dist/esm/utils/misc.mjs +8 -2
  25. package/dist/esm/utils/misc.mjs.map +1 -1
  26. package/dist/esm/utils/title.mjs +54 -25
  27. package/dist/esm/utils/title.mjs.map +1 -1
  28. package/dist/types/instrumentation.d.ts +1 -0
  29. package/dist/types/run.d.ts +2 -1
  30. package/dist/types/utils/misc.d.ts +1 -0
  31. package/package.json +5 -1
  32. package/src/instrumentation.ts +22 -0
  33. package/src/llm/anthropic/llm.spec.ts +1 -1
  34. package/src/llm/openai/index.ts +0 -5
  35. package/src/run.ts +82 -10
  36. package/src/scripts/ant_web_search.ts +1 -1
  37. package/src/scripts/handoff-test.ts +1 -1
  38. package/src/scripts/multi-agent-chain.ts +4 -4
  39. package/src/scripts/multi-agent-conditional.ts +4 -4
  40. package/src/scripts/multi-agent-document-review-chain.ts +4 -4
  41. package/src/scripts/multi-agent-parallel.ts +10 -8
  42. package/src/scripts/multi-agent-sequence.ts +3 -3
  43. package/src/scripts/multi-agent-supervisor.ts +5 -3
  44. package/src/scripts/multi-agent-test.ts +2 -2
  45. package/src/scripts/simple.ts +8 -0
  46. package/src/scripts/test-custom-prompt-key.ts +4 -4
  47. package/src/scripts/test-handoff-input.ts +3 -3
  48. package/src/scripts/test-multi-agent-list-handoff.ts +2 -2
  49. package/src/stream.ts +9 -2
  50. package/src/utils/misc.ts +33 -21
  51. package/src/utils/title.ts +80 -40
@@ -71,7 +71,7 @@ async function testSequentialAgentChain() {
71
71
  agentId: 'researcher',
72
72
  provider: Providers.ANTHROPIC,
73
73
  clientOptions: {
74
- modelName: 'claude-3-5-sonnet-latest',
74
+ modelName: 'claude-haiku-4-5',
75
75
  apiKey: process.env.ANTHROPIC_API_KEY,
76
76
  },
77
77
  instructions: `You are a Research Agent specializing in gathering initial information.
@@ -88,7 +88,7 @@ async function testSequentialAgentChain() {
88
88
  agentId: 'analyst',
89
89
  provider: Providers.ANTHROPIC,
90
90
  clientOptions: {
91
- modelName: 'claude-3-5-sonnet-latest',
91
+ modelName: 'claude-haiku-4-5',
92
92
  apiKey: process.env.ANTHROPIC_API_KEY,
93
93
  },
94
94
  instructions: `You are an Analysis Agent that builds upon research findings.
@@ -105,7 +105,7 @@ async function testSequentialAgentChain() {
105
105
  agentId: 'reviewer',
106
106
  provider: Providers.ANTHROPIC,
107
107
  clientOptions: {
108
- modelName: 'claude-3-5-sonnet-latest',
108
+ modelName: 'claude-haiku-4-5',
109
109
  apiKey: process.env.ANTHROPIC_API_KEY,
110
110
  },
111
111
  instructions: `You are a Critical Review Agent that evaluates the work done so far.
@@ -122,7 +122,7 @@ async function testSequentialAgentChain() {
122
122
  agentId: 'summarizer',
123
123
  provider: Providers.ANTHROPIC,
124
124
  clientOptions: {
125
- modelName: 'claude-3-5-sonnet-latest',
125
+ modelName: 'claude-haiku-4-5',
126
126
  apiKey: process.env.ANTHROPIC_API_KEY,
127
127
  },
128
128
  instructions: `You are a Summary Agent that creates the final comprehensive output.
@@ -32,7 +32,7 @@ async function testConditionalMultiAgent() {
32
32
  agentId: 'classifier',
33
33
  provider: Providers.ANTHROPIC,
34
34
  clientOptions: {
35
- modelName: 'claude-3-5-sonnet-latest',
35
+ modelName: 'claude-haiku-4-5',
36
36
  apiKey: process.env.ANTHROPIC_API_KEY,
37
37
  },
38
38
  instructions:
@@ -43,7 +43,7 @@ async function testConditionalMultiAgent() {
43
43
  agentId: 'technical_expert',
44
44
  provider: Providers.ANTHROPIC,
45
45
  clientOptions: {
46
- modelName: 'claude-3-5-sonnet-latest',
46
+ modelName: 'claude-haiku-4-5',
47
47
  apiKey: process.env.ANTHROPIC_API_KEY,
48
48
  },
49
49
  instructions:
@@ -54,7 +54,7 @@ async function testConditionalMultiAgent() {
54
54
  agentId: 'business_expert',
55
55
  provider: Providers.ANTHROPIC,
56
56
  clientOptions: {
57
- modelName: 'claude-3-5-sonnet-latest',
57
+ modelName: 'claude-haiku-4-5',
58
58
  apiKey: process.env.ANTHROPIC_API_KEY,
59
59
  },
60
60
  instructions:
@@ -65,7 +65,7 @@ async function testConditionalMultiAgent() {
65
65
  agentId: 'general_assistant',
66
66
  provider: Providers.ANTHROPIC,
67
67
  clientOptions: {
68
- modelName: 'claude-3-5-sonnet-latest',
68
+ modelName: 'claude-haiku-4-5',
69
69
  apiKey: process.env.ANTHROPIC_API_KEY,
70
70
  },
71
71
  instructions:
@@ -48,7 +48,7 @@ async function testDocumentReviewChain() {
48
48
  agentId: 'grammar_checker',
49
49
  provider: Providers.ANTHROPIC,
50
50
  clientOptions: {
51
- modelName: 'claude-3-5-sonnet-latest',
51
+ modelName: 'claude-haiku-4-5',
52
52
  apiKey: process.env.ANTHROPIC_API_KEY,
53
53
  },
54
54
  instructions: `You are a Grammar and Style Checker.
@@ -65,7 +65,7 @@ async function testDocumentReviewChain() {
65
65
  agentId: 'fact_checker',
66
66
  provider: Providers.ANTHROPIC,
67
67
  clientOptions: {
68
- modelName: 'claude-3-5-sonnet-latest',
68
+ modelName: 'claude-haiku-4-5',
69
69
  apiKey: process.env.ANTHROPIC_API_KEY,
70
70
  },
71
71
  instructions: `You are a Fact Checker.
@@ -82,7 +82,7 @@ async function testDocumentReviewChain() {
82
82
  agentId: 'tone_reviewer',
83
83
  provider: Providers.ANTHROPIC,
84
84
  clientOptions: {
85
- modelName: 'claude-3-5-sonnet-latest',
85
+ modelName: 'claude-haiku-4-5',
86
86
  apiKey: process.env.ANTHROPIC_API_KEY,
87
87
  },
88
88
  instructions: `You are a Tone and Audience Reviewer.
@@ -99,7 +99,7 @@ async function testDocumentReviewChain() {
99
99
  agentId: 'final_editor',
100
100
  provider: Providers.ANTHROPIC,
101
101
  clientOptions: {
102
- modelName: 'claude-3-5-sonnet-latest',
102
+ modelName: 'claude-haiku-4-5',
103
103
  apiKey: process.env.ANTHROPIC_API_KEY,
104
104
  },
105
105
  instructions: `You are the Final Editor.
@@ -2,11 +2,12 @@ import { config } from 'dotenv';
2
2
  config();
3
3
 
4
4
  import { HumanMessage, BaseMessage } from '@langchain/core/messages';
5
- import { Run } from '@/run';
6
- import { Providers, GraphEvents } from '@/common';
5
+ import type * as t from '@/types';
7
6
  import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
8
7
  import { ToolEndHandler, ModelEndHandler } from '@/events';
9
- import type * as t from '@/types';
8
+ import { Providers, GraphEvents } from '@/common';
9
+ import { sleep } from '@/utils/run';
10
+ import { Run } from '@/run';
10
11
 
11
12
  const conversationHistory: BaseMessage[] = [];
12
13
 
@@ -35,7 +36,7 @@ async function testParallelMultiAgent() {
35
36
  agentId: 'researcher',
36
37
  provider: Providers.ANTHROPIC,
37
38
  clientOptions: {
38
- modelName: 'claude-3-5-sonnet-latest',
39
+ modelName: 'claude-haiku-4-5',
39
40
  apiKey: process.env.ANTHROPIC_API_KEY,
40
41
  },
41
42
  instructions: `You are a research coordinator in a multi-agent analysis workflow. Your sole job is to:
@@ -59,7 +60,7 @@ async function testParallelMultiAgent() {
59
60
  agentId: 'analyst1',
60
61
  provider: Providers.ANTHROPIC,
61
62
  clientOptions: {
62
- modelName: 'claude-3-5-sonnet-latest',
63
+ modelName: 'claude-haiku-4-5',
63
64
  apiKey: process.env.ANTHROPIC_API_KEY,
64
65
  },
65
66
  instructions: `You are a FINANCIAL AND ECONOMIC ANALYST in a parallel analysis workflow.
@@ -82,7 +83,7 @@ async function testParallelMultiAgent() {
82
83
  agentId: 'analyst2',
83
84
  provider: Providers.ANTHROPIC,
84
85
  clientOptions: {
85
- modelName: 'claude-3-5-sonnet-latest',
86
+ modelName: 'claude-haiku-4-5',
86
87
  apiKey: process.env.ANTHROPIC_API_KEY,
87
88
  },
88
89
  instructions: `You are a TECHNICAL AND IMPLEMENTATION ANALYST in a parallel analysis workflow.
@@ -106,7 +107,7 @@ async function testParallelMultiAgent() {
106
107
  agentId: 'analyst3',
107
108
  provider: Providers.ANTHROPIC,
108
109
  clientOptions: {
109
- modelName: 'claude-3-5-sonnet-latest',
110
+ modelName: 'claude-haiku-4-5',
110
111
  apiKey: process.env.ANTHROPIC_API_KEY,
111
112
  },
112
113
  instructions: `You are a MARKET AND INDUSTRY ANALYST in a parallel analysis workflow.
@@ -130,7 +131,7 @@ async function testParallelMultiAgent() {
130
131
  agentId: 'summarizer',
131
132
  provider: Providers.ANTHROPIC,
132
133
  clientOptions: {
133
- modelName: 'claude-3-5-sonnet-latest',
134
+ modelName: 'claude-haiku-4-5',
134
135
  apiKey: process.env.ANTHROPIC_API_KEY,
135
136
  },
136
137
  instructions: `You are the SYNTHESIS AND SUMMARY EXPERT in a multi-agent workflow.
@@ -332,6 +333,7 @@ IMPORTANT: Each analyst must produce substantive analysis (200+ words), not just
332
333
  console.log('\n\nActive agents during execution:', activeAgents.size);
333
334
  console.log('Final content parts:', contentParts.length, 'parts');
334
335
  console.dir(contentParts, { depth: null });
336
+ await sleep(3000);
335
337
  } catch (error) {
336
338
  console.error('Error in parallel multi-agent test:', error);
337
339
  }
@@ -30,7 +30,7 @@ async function testSequentialMultiAgent() {
30
30
  agentId: 'agent_a',
31
31
  provider: Providers.ANTHROPIC,
32
32
  clientOptions: {
33
- modelName: 'claude-3-5-sonnet-latest',
33
+ modelName: 'claude-haiku-4-5',
34
34
  apiKey: process.env.ANTHROPIC_API_KEY,
35
35
  },
36
36
  instructions: `You are Agent A, the first agent in a sequential workflow.
@@ -46,7 +46,7 @@ async function testSequentialMultiAgent() {
46
46
  agentId: 'agent_b',
47
47
  provider: Providers.ANTHROPIC,
48
48
  clientOptions: {
49
- modelName: 'claude-3-5-sonnet-latest',
49
+ modelName: 'claude-haiku-4-5',
50
50
  apiKey: process.env.ANTHROPIC_API_KEY,
51
51
  },
52
52
  instructions: `You are Agent B, the second agent in a sequential workflow.
@@ -62,7 +62,7 @@ async function testSequentialMultiAgent() {
62
62
  agentId: 'agent_c',
63
63
  provider: Providers.ANTHROPIC,
64
64
  clientOptions: {
65
- modelName: 'claude-3-5-sonnet-latest',
65
+ modelName: 'claude-haiku-4-5',
66
66
  apiKey: process.env.ANTHROPIC_API_KEY,
67
67
  },
68
68
  instructions: `You are Agent C, the final agent in a sequential workflow.
@@ -2,11 +2,12 @@ import { config } from 'dotenv';
2
2
  config();
3
3
 
4
4
  import { HumanMessage, BaseMessage } from '@langchain/core/messages';
5
- import { Run } from '@/run';
6
- import { Providers, GraphEvents } from '@/common';
5
+ import type * as t from '@/types';
7
6
  import { ChatModelStreamHandler, createContentAggregator } from '@/stream';
8
7
  import { ToolEndHandler, ModelEndHandler } from '@/events';
9
- import type * as t from '@/types';
8
+ import { Providers, GraphEvents } from '@/common';
9
+ import { sleep } from '@/utils/run';
10
+ import { Run } from '@/run';
10
11
 
11
12
  const conversationHistory: BaseMessage[] = [];
12
13
 
@@ -353,6 +354,7 @@ async function testSupervisorMultiAgent() {
353
354
  console.log(` transfer_to_legal_advisor`);
354
355
  console.log('─'.repeat(60));
355
356
  }
357
+ await sleep(3000);
356
358
  } catch (error) {
357
359
  console.error('Error in supervisor multi-agent test:', error);
358
360
  }
@@ -22,7 +22,7 @@ async function testMultiAgentHandoff() {
22
22
  agentId: 'flight_assistant',
23
23
  provider: Providers.ANTHROPIC,
24
24
  clientOptions: {
25
- modelName: 'claude-3-5-sonnet-latest',
25
+ modelName: 'claude-haiku-4-5',
26
26
  apiKey: process.env.ANTHROPIC_API_KEY,
27
27
  },
28
28
  instructions:
@@ -33,7 +33,7 @@ async function testMultiAgentHandoff() {
33
33
  agentId: 'hotel_assistant',
34
34
  provider: Providers.ANTHROPIC,
35
35
  clientOptions: {
36
- modelName: 'claude-3-5-sonnet-latest',
36
+ modelName: 'claude-haiku-4-5',
37
37
  apiKey: process.env.ANTHROPIC_API_KEY,
38
38
  },
39
39
  instructions:
@@ -18,6 +18,7 @@ import {
18
18
  import { GraphEvents, Providers, TitleMethod } from '@/common';
19
19
  import { getLLMConfig } from '@/utils/llmConfig';
20
20
  import { getArgs } from '@/scripts/args';
21
+ import { sleep } from '@/utils/run';
21
22
  import { Run } from '@/run';
22
23
 
23
24
  const conversationHistory: BaseMessage[] = [];
@@ -145,7 +146,9 @@ async function testStandardStreaming(): Promise<void> {
145
146
  });
146
147
 
147
148
  const config = {
149
+ runId: 'test-simple-script',
148
150
  configurable: {
151
+ user_id: 'user-123',
149
152
  thread_id: 'conversation-num-1',
150
153
  },
151
154
  streamMode: 'values',
@@ -177,6 +180,10 @@ async function testStandardStreaming(): Promise<void> {
177
180
  contentParts,
178
181
  // titleMethod: TitleMethod.STRUCTURED,
179
182
  chainOptions: {
183
+ configurable: {
184
+ user_id: 'user-123',
185
+ thread_id: 'conversation-num-1',
186
+ },
180
187
  callbacks: [
181
188
  {
182
189
  handleLLMEnd,
@@ -193,6 +200,7 @@ async function testStandardStreaming(): Promise<void> {
193
200
  console.log('Collected usage metadata:', collectedUsage);
194
201
  console.log('Generated Title:', titleResult);
195
202
  console.log('Collected title usage metadata:', collected);
203
+ await sleep(5000);
196
204
  }
197
205
 
198
206
  process.on('unhandledRejection', (reason, promise) => {
@@ -24,7 +24,7 @@ async function testCustomPromptKey() {
24
24
  agentId: 'supervisor',
25
25
  provider: Providers.ANTHROPIC,
26
26
  clientOptions: {
27
- modelName: 'claude-3-5-sonnet-latest',
27
+ modelName: 'claude-haiku-4-5',
28
28
  apiKey: process.env.ANTHROPIC_API_KEY,
29
29
  },
30
30
  instructions: `You are a Task Supervisor managing different agents:
@@ -41,7 +41,7 @@ async function testCustomPromptKey() {
41
41
  agentId: 'researcher',
42
42
  provider: Providers.ANTHROPIC,
43
43
  clientOptions: {
44
- modelName: 'claude-3-5-sonnet-latest',
44
+ modelName: 'claude-haiku-4-5',
45
45
  apiKey: process.env.ANTHROPIC_API_KEY,
46
46
  },
47
47
  instructions: `You are a Research Agent. You receive research queries to investigate.
@@ -52,7 +52,7 @@ async function testCustomPromptKey() {
52
52
  agentId: 'designer',
53
53
  provider: Providers.ANTHROPIC,
54
54
  clientOptions: {
55
- modelName: 'claude-3-5-sonnet-latest',
55
+ modelName: 'claude-haiku-4-5',
56
56
  apiKey: process.env.ANTHROPIC_API_KEY,
57
57
  },
58
58
  instructions: `You are a Design Agent. You receive design requirements to implement.
@@ -63,7 +63,7 @@ async function testCustomPromptKey() {
63
63
  agentId: 'coder',
64
64
  provider: Providers.ANTHROPIC,
65
65
  clientOptions: {
66
- modelName: 'claude-3-5-sonnet-latest',
66
+ modelName: 'claude-haiku-4-5',
67
67
  apiKey: process.env.ANTHROPIC_API_KEY,
68
68
  },
69
69
  instructions: `You are a Coding Agent. You receive technical specifications to implement.
@@ -84,7 +84,7 @@ async function testHandoffInput() {
84
84
  agentId: 'supervisor',
85
85
  provider: Providers.ANTHROPIC,
86
86
  clientOptions: {
87
- modelName: 'claude-3-5-sonnet-latest',
87
+ modelName: 'claude-haiku-4-5',
88
88
  apiKey: process.env.ANTHROPIC_API_KEY,
89
89
  },
90
90
  instructions: `You are a Task Supervisor. You have access to two specialist agents:
@@ -100,7 +100,7 @@ async function testHandoffInput() {
100
100
  agentId: 'analyst',
101
101
  provider: Providers.ANTHROPIC,
102
102
  clientOptions: {
103
- modelName: 'claude-3-5-sonnet-latest',
103
+ modelName: 'claude-haiku-4-5',
104
104
  apiKey: process.env.ANTHROPIC_API_KEY,
105
105
  },
106
106
  instructions: `You are a Data Analyst. Follow the supervisor's instructions carefully.
@@ -111,7 +111,7 @@ async function testHandoffInput() {
111
111
  agentId: 'writer',
112
112
  provider: Providers.ANTHROPIC,
113
113
  clientOptions: {
114
- modelName: 'claude-3-5-sonnet-latest',
114
+ modelName: 'claude-haiku-4-5',
115
115
  apiKey: process.env.ANTHROPIC_API_KEY,
116
116
  },
117
117
  instructions: `You are a Content Writer. Follow the supervisor's instructions carefully.
@@ -93,7 +93,7 @@ async function testSupervisorListHandoff() {
93
93
  const specialistConfig = {
94
94
  provider: Providers.ANTHROPIC,
95
95
  clientOptions: {
96
- modelName: 'claude-3-5-sonnet-latest',
96
+ modelName: 'claude-haiku-4-5',
97
97
  apiKey: process.env.ANTHROPIC_API_KEY,
98
98
  },
99
99
  instructions: `You are an Adaptive Specialist. Your agent ID indicates your role:
@@ -114,7 +114,7 @@ async function testSupervisorListHandoff() {
114
114
  agentId: 'supervisor',
115
115
  provider: Providers.ANTHROPIC,
116
116
  clientOptions: {
117
- modelName: 'claude-3-5-sonnet-latest',
117
+ modelName: 'claude-haiku-4-5',
118
118
  apiKey: process.env.ANTHROPIC_API_KEY,
119
119
  },
120
120
  instructions: `You are a Task Supervisor with access to 5 specialist agents:
package/src/stream.ts CHANGED
@@ -398,9 +398,13 @@ export function createContentAggregator(): t.ContentAggregatorResult {
398
398
 
399
399
  const updateContent = (
400
400
  index: number,
401
- contentPart: t.MessageContentComplex,
401
+ contentPart?: t.MessageContentComplex,
402
402
  finalUpdate = false
403
403
  ): void => {
404
+ if (!contentPart) {
405
+ console.warn('No content part found in \'updateContent\'');
406
+ return;
407
+ }
404
408
  const partType = contentPart.type ?? '';
405
409
  if (!partType) {
406
410
  console.warn('No content type found in content part');
@@ -578,7 +582,10 @@ export function createContentAggregator(): t.ContentAggregatorResult {
578
582
  event === GraphEvents.ON_AGENT_UPDATE &&
579
583
  (data as t.AgentUpdate | undefined)?.agent_update
580
584
  ) {
581
- const contentPart = data as t.AgentUpdate;
585
+ const contentPart = data as t.AgentUpdate | undefined;
586
+ if (!contentPart) {
587
+ return;
588
+ }
582
589
  updateContent(contentPart.agent_update.index, contentPart);
583
590
  } else if (event === GraphEvents.ON_REASONING_DELTA) {
584
591
  const reasoningDelta = data as t.ReasoningDeltaEvent;
package/src/utils/misc.ts CHANGED
@@ -1,26 +1,31 @@
1
+ export function isPresent(value: string | null | undefined): value is string {
2
+ return value != null && value !== '';
3
+ }
4
+
1
5
  /**
2
6
  * Unescapes a c-escaped string
3
7
  * @param str The string to unescape
4
8
  * @returns The unescaped string
5
9
  */
6
- const unescapeString = (string: string): string => string.replace(/\\(.)/g, (_, char) => {
7
- switch (char) {
8
- case 'n':
9
- return '\n';
10
- case 't':
11
- return '\t';
12
- case 'r':
13
- return '\r';
14
- case '"':
15
- return '"';
16
- case '\'':
17
- return '\'';
18
- case '\\':
19
- return '\\';
20
- default:
21
- return char;
22
- }
23
- });
10
+ const unescapeString = (string: string): string =>
11
+ string.replace(/\\(.)/g, (_, char) => {
12
+ switch (char) {
13
+ case 'n':
14
+ return '\n';
15
+ case 't':
16
+ return '\t';
17
+ case 'r':
18
+ return '\r';
19
+ case '"':
20
+ return '"';
21
+ case '\'':
22
+ return '\'';
23
+ case '\\':
24
+ return '\\';
25
+ default:
26
+ return char;
27
+ }
28
+ });
24
29
 
25
30
  /**
26
31
  * Recursively unescapes all string values in an object
@@ -36,10 +41,17 @@ export function unescapeObject(obj: unknown, key?: string): unknown {
36
41
  return unescaped;
37
42
  }
38
43
  if (Array.isArray(obj)) {
39
- return obj.map((value) => unescapeObject(value, key === 'contextPaths' ? 'filePath' : ''));
44
+ return obj.map((value) =>
45
+ unescapeObject(value, key === 'contextPaths' ? 'filePath' : '')
46
+ );
40
47
  }
41
48
  if (typeof obj === 'object' && obj !== null) {
42
- return Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, unescapeObject(value, key)]));
49
+ return Object.fromEntries(
50
+ Object.entries(obj).map(([key, value]) => [
51
+ key,
52
+ unescapeObject(value, key),
53
+ ])
54
+ );
43
55
  }
44
56
  return obj;
45
- }
57
+ }
@@ -1,7 +1,8 @@
1
1
  import { z } from 'zod';
2
- import { RunnableLambda } from '@langchain/core/runnables';
3
2
  import { ChatPromptTemplate } from '@langchain/core/prompts';
3
+ import { RunnableLambda, RunnableSequence } from '@langchain/core/runnables';
4
4
  import type { Runnable, RunnableConfig } from '@langchain/core/runnables';
5
+ import type { AIMessage } from '@langchain/core/messages';
5
6
  import type * as t from '@/types';
6
7
  import { ContentTypes } from '@/common';
7
8
 
@@ -42,7 +43,55 @@ export const createTitleRunnable = async (
42
43
 
43
44
  const titlePrompt = ChatPromptTemplate.fromTemplate(
44
45
  _titlePrompt ?? defaultTitlePrompt
45
- );
46
+ ).withConfig({ runName: 'TitlePrompt' });
47
+
48
+ const titleOnlyInnerChain = RunnableSequence.from([titlePrompt, titleLLM]);
49
+ const combinedInnerChain = RunnableSequence.from([titlePrompt, combinedLLM]);
50
+
51
+ /** Wrap titleOnlyChain in RunnableLambda to create parent span */
52
+ const titleOnlyChain = new RunnableLambda({
53
+ func: async (
54
+ input: { convo: string },
55
+ config?: Partial<RunnableConfig>
56
+ ): Promise<{ title: string }> => {
57
+ return await titleOnlyInnerChain.invoke(input, config);
58
+ },
59
+ }).withConfig({ runName: 'TitleOnlyChain' });
60
+
61
+ /** Wrap combinedChain in RunnableLambda to create parent span */
62
+ const combinedChain = new RunnableLambda({
63
+ func: async (
64
+ input: { convo: string },
65
+ config?: Partial<RunnableConfig>
66
+ ): Promise<{ language: string; title: string }> => {
67
+ return await combinedInnerChain.invoke(input, config);
68
+ },
69
+ }).withConfig({ runName: 'TitleLanguageChain' });
70
+
71
+ /** Runnable to add default values if needed */
72
+ const addDefaults = new RunnableLambda({
73
+ func: (
74
+ result: { language: string; title: string } | undefined
75
+ ): { language: string; title: string } => ({
76
+ language: result?.language ?? 'English',
77
+ title: result?.title ?? '',
78
+ }),
79
+ }).withConfig({ runName: 'AddDefaults' });
80
+
81
+ const combinedChainInner = RunnableSequence.from([
82
+ combinedChain,
83
+ addDefaults,
84
+ ]);
85
+
86
+ /** Wrap combinedChainWithDefaults in RunnableLambda to create parent span */
87
+ const combinedChainWithDefaults = new RunnableLambda({
88
+ func: async (
89
+ input: { convo: string },
90
+ config?: Partial<RunnableConfig>
91
+ ): Promise<{ language: string; title: string }> => {
92
+ return await combinedChainInner.invoke(input, config);
93
+ },
94
+ }).withConfig({ runName: 'CombinedChainWithDefaults' });
46
95
 
47
96
  return new RunnableLambda({
48
97
  func: async (
@@ -53,28 +102,17 @@ export const createTitleRunnable = async (
53
102
  },
54
103
  config?: Partial<RunnableConfig>
55
104
  ): Promise<{ language: string; title: string } | { title: string }> => {
105
+ const invokeInput = { convo: input.convo };
106
+
56
107
  if (input.skipLanguage) {
57
- return (await titlePrompt.pipe(titleLLM).invoke(
58
- {
59
- convo: input.convo,
60
- },
61
- config
62
- )) as { title: string };
108
+ return (await titleOnlyChain.invoke(invokeInput, config)) as {
109
+ title: string;
110
+ };
63
111
  }
64
112
 
65
- const result = (await titlePrompt.pipe(combinedLLM).invoke(
66
- {
67
- convo: input.convo,
68
- },
69
- config
70
- )) as { language: string; title: string } | undefined;
71
-
72
- return {
73
- language: result?.language ?? 'English',
74
- title: result?.title ?? '',
75
- };
113
+ return await combinedChainWithDefaults.invoke(invokeInput, config);
76
114
  },
77
- });
115
+ }).withConfig({ runName: 'TitleGenerator' });
78
116
  };
79
117
 
80
118
  const defaultCompletionPrompt = `Provide a concise, 5-word-or-less title for the conversation, using title case conventions. Only return the title itself.
@@ -88,22 +126,11 @@ export const createCompletionTitleRunnable = async (
88
126
  ): Promise<Runnable> => {
89
127
  const completionPrompt = ChatPromptTemplate.fromTemplate(
90
128
  titlePrompt ?? defaultCompletionPrompt
91
- );
129
+ ).withConfig({ runName: 'CompletionTitlePrompt' });
92
130
 
93
- return new RunnableLambda({
94
- func: async (
95
- input: {
96
- convo: string;
97
- inputText: string;
98
- skipLanguage: boolean;
99
- },
100
- config?: Partial<RunnableConfig>
101
- ): Promise<{ title: string }> => {
102
- const promptOutput = await completionPrompt.invoke({
103
- convo: input.convo,
104
- });
105
-
106
- const response = await model.invoke(promptOutput, config);
131
+ /** Runnable to extract content from model response */
132
+ const extractContent = new RunnableLambda({
133
+ func: (response: AIMessage): { title: string } => {
107
134
  let content = '';
108
135
  if (typeof response.content === 'string') {
109
136
  content = response.content;
@@ -116,10 +143,23 @@ export const createCompletionTitleRunnable = async (
116
143
  .map((part) => part.text)
117
144
  .join('');
118
145
  }
119
- const title = content.trim();
120
- return {
121
- title,
122
- };
146
+ return { title: content.trim() };
147
+ },
148
+ }).withConfig({ runName: 'ExtractTitle' });
149
+
150
+ const innerChain = RunnableSequence.from([
151
+ completionPrompt,
152
+ model,
153
+ extractContent,
154
+ ]);
155
+
156
+ /** Wrap in RunnableLambda to create a parent span for LangFuse */
157
+ return new RunnableLambda({
158
+ func: async (
159
+ input: { convo: string },
160
+ config?: Partial<RunnableConfig>
161
+ ): Promise<{ title: string }> => {
162
+ return await innerChain.invoke(input, config);
123
163
  },
124
- });
164
+ }).withConfig({ runName: 'CompletionTitleChain' });
125
165
  };