11import type { BaseChatModel } from "@langchain/core/language_models/chat_models" ;
2- import { createAgent , summarizationMiddleware } from "langchain" ;
32import {
43 logger ,
5- type AdminUser ,
64 type CompletionAdapter ,
7- type IAdminForth ,
85} from "adminforth" ;
96import { BaseCallbackHandler } from "@langchain/core/callbacks/base" ;
10- import { type BaseCheckpointSaver , type Messages } from "@langchain/langgraph" ;
117import type { LLMResult } from "@langchain/core/outputs" ;
12- import { z } from "zod" ;
13- import { createAgentTools } from "./tools/index.js" ;
14- import { createApiBasedToolsMiddleware } from "./middleware/apiBasedTools.js" ;
158import {
169 createSequenceDebugMiddleware ,
17- type SequenceDebugModelCallSink ,
1810} from "./middleware/sequenceDebug.js" ;
19- import type { ApiBasedTool } from "../apiBasedTools.js" ;
20- import type { ToolCallEventSink } from "./toolCallEvents.js" ;
21- import type { CurrentPageContext } from "./tools/getUserLocation.js" ;
22- import type { AgentEventEmitter } from "../agentEvents.js" ;
23-
24- export const contextSchema = z . object ( {
25- adminUser : z . custom < AdminUser > ( ) ,
26- userTimeZone : z . string ( ) ,
27- sessionId : z . string ( ) ,
28- turnId : z . string ( ) ,
29- abortSignal : z . custom < AbortSignal > ( ) . optional ( ) ,
30- currentPage : z . custom < CurrentPageContext > ( ) . optional ( ) ,
31- chatSurface : z . string ( ) . optional ( ) ,
32- adminBaseUrl : z . string ( ) . optional ( ) ,
33- adminPublicOrigin : z . string ( ) . optional ( ) ,
34- emitToolCallEvent : z . custom < ToolCallEventSink > ( ) ,
35- emitAgentEvent : z . custom < AgentEventEmitter > ( ) . optional ( ) ,
36- } ) ;
3711
3812export type AgentChatModel = BaseChatModel < any , any > ;
3913export type AgentModelPurpose = "primary" | "summary" ;
@@ -50,7 +24,7 @@ export type AgentModeCompletionAdapter = CompletionAdapter & {
5024 } ;
5125} ;
5226
53- type AgentMiddleware = ReturnType < typeof createSequenceDebugMiddleware > ;
27+ export type AgentMiddleware = ReturnType < typeof createSequenceDebugMiddleware > ;
5428
5529type AgentChatModelSpec = {
5630 model : AgentChatModel ;
@@ -202,7 +176,7 @@ class AgentLlmMetricsLogger extends BaseCallbackHandler {
202176 }
203177}
204178
205- function createAgentLlmMetricsLogger ( ) {
179+ export function createAgentLlmMetricsLogger ( ) {
206180 return new AgentLlmMetricsLogger ( ) ;
207181}
208182
@@ -223,104 +197,3 @@ export async function createAgentChatModel(params: {
223197 purpose : params . purpose ,
224198 } ) ;
225199}
226-
227- export async function callAgent ( params : {
228- name : string ;
229- model : AgentChatModel ;
230- summaryModel : AgentChatModel ;
231- modelMiddleware ?: AgentMiddleware [ ] ;
232- checkpointer ?: BaseCheckpointSaver ;
233- messages : Messages ;
234- adminUser : AdminUser ;
235- adminforth : IAdminForth ;
236- apiBasedTools : Record < string , ApiBasedTool > ;
237- customComponentsDir : string ;
238- pluginCustomFolderPaths : string [ ] ;
239- sessionId : string ;
240- turnId : string ;
241- currentPage ?: CurrentPageContext ;
242- chatSurface ?: string ;
243- adminPublicOrigin ?: string ;
244- userTimeZone : string ;
245- abortSignal ?: AbortSignal ;
246- emitToolCallEvent : ToolCallEventSink ;
247- emitAgentEvent ?: AgentEventEmitter ;
248- sequenceDebugSink : SequenceDebugModelCallSink ;
249- } ) {
250- const {
251- name,
252- model,
253- summaryModel,
254- modelMiddleware = [ ] ,
255- checkpointer,
256- messages,
257- adminUser,
258- adminforth,
259- apiBasedTools,
260- customComponentsDir,
261- pluginCustomFolderPaths,
262- sessionId,
263- turnId,
264- currentPage,
265- chatSurface,
266- adminPublicOrigin,
267- userTimeZone,
268- abortSignal,
269- emitToolCallEvent,
270- emitAgentEvent,
271- sequenceDebugSink,
272- } = params ;
273-
274- const tools = await createAgentTools (
275- customComponentsDir ,
276- apiBasedTools ,
277- pluginCustomFolderPaths ,
278- ) ;
279- const apiBasedToolsMiddleware = createApiBasedToolsMiddleware ( apiBasedTools , adminforth ) ;
280- const sequenceDebugMiddleware = createSequenceDebugMiddleware (
281- sequenceDebugSink ,
282- ) ;
283-
284- const middleware = [
285- apiBasedToolsMiddleware ,
286- ...modelMiddleware ,
287- sequenceDebugMiddleware ,
288- summarizationMiddleware ( {
289- model : summaryModel ,
290- trigger : { tokens : 1024 * 64 } ,
291- keep : { messages : 10 } ,
292- } ) ,
293- ] as const ;
294-
295- const agent = createAgent < undefined , typeof contextSchema , typeof middleware > ( {
296- name,
297- model,
298- checkpointer,
299- tools,
300- contextSchema,
301- middleware,
302- } ) ;
303-
304- return await agent . stream ( { messages } as any , {
305- streamMode : "messages" ,
306- recursionLimit : 100 ,
307- callbacks : [ createAgentLlmMetricsLogger ( ) ] ,
308- signal : abortSignal ,
309- configurable : {
310- thread_id : sessionId ,
311- } ,
312- context : {
313- adminUser,
314- userTimeZone,
315- sessionId,
316- turnId,
317- abortSignal,
318- currentPage,
319- chatSurface,
320- adminBaseUrl : adminforth . config . baseUrlSlashed ,
321- adminPublicOrigin,
322- emitToolCallEvent,
323- emitAgentEvent,
324- } ,
325- } ) ;
326- }
0 commit comments