11import { logger } from "adminforth" ;
22import { randomUUID } from "crypto" ;
3- import type { IAdminForth } from "adminforth" ;
4- import type { BaseCheckpointSaver } from "@langchain/langgraph" ;
53import { AgentModelFactory } from "./agent/models/AgentModelFactory.js" ;
64import { AgentModeResolver } from "./agent/models/AgentModeResolver.js" ;
75import { createSequenceDebugCollector } from "./agent/middleware/sequenceDebug.js" ;
8- import { AgentRuntimeFactory } from "./agent/runtime/AgentRuntimeFactory .js" ;
9- import { AgentToolProvider } from "./agent/tools/AgentToolProvider .js" ;
6+ import { AgentRuntime } from "./agent/runtime/AgentRuntime .js" ;
7+ import { TurnContextBuilder } from "./agent/turn/TurnContextBuilder .js" ;
108import { TurnLifecycleService } from "./agent/turn/TurnLifecycleService.js" ;
11- import { TurnPersistenceService } from "./agent/turn/TurnPersistenceService.js" ;
129import { TurnPromptBuilder } from "./agent/turn/TurnPromptBuilder.js" ;
1310import { TurnStreamConsumer } from "./agent/turn/TurnStreamConsumer.js" ;
1411import type {
1512 BaseAgentTurnInput ,
16- HandleSpeechTurnInput ,
1713 HandleTurnInput ,
1814 PreparedAgentTurn ,
1915 RunAndPersistAgentResponseInput ,
2016 RunAndPersistAgentResponseResult ,
2117} from "./agent/turn/turnTypes.js" ;
2218import { getErrorMessage , isAbortError } from "./errors.js" ;
23- import type { AgentSessionStore } from "./sessionStore.js" ;
24- import { SpeechTurnService } from "./agent/speech/SpeechTurnService.js" ;
25- import type { PluginOptions } from "./types.js" ;
2619
2720export type {
2821 BaseAgentTurnInput ,
@@ -32,73 +25,32 @@ export type {
3225 RunAndPersistAgentResponseResult ,
3326} from "./agent/turn/turnTypes.js" ;
3427
35- type AgentTurnServiceOptions = {
36- getAdminforth : ( ) => IAdminForth ;
37- getPluginInstanceId : ( ) => string ;
38- options : PluginOptions ;
39- sessionStore : AgentSessionStore ;
40- getCheckpointer : ( ) => BaseCheckpointSaver ;
41- getInternalAgentResourceIds : ( ) => string [ ] ;
42- getAgentSystemPrompt : ( ) => Promise < string > ;
43- } ;
44-
4528export class AgentTurnService {
46- private readonly modeResolver : AgentModeResolver ;
47- private readonly modelFactory : AgentModelFactory ;
48- private readonly runtimeFactory : AgentRuntimeFactory ;
49- private readonly lifecycle : TurnLifecycleService ;
50- private readonly promptBuilder : TurnPromptBuilder ;
51- private readonly streamConsumer = new TurnStreamConsumer ( ) ;
52- private readonly speechTurnService : SpeechTurnService ;
53-
54- constructor ( private readonly serviceOptions : AgentTurnServiceOptions ) {
55- const toolProvider = new AgentToolProvider (
56- serviceOptions . getAdminforth ,
57- serviceOptions . getInternalAgentResourceIds ,
58- ) ;
59- const persistence = new TurnPersistenceService (
60- serviceOptions . getAdminforth ,
61- serviceOptions . options ,
62- ) ;
63-
64- this . modeResolver = new AgentModeResolver ( serviceOptions . options ) ;
65- this . modelFactory = new AgentModelFactory ( serviceOptions . options . maxTokens ?? 1000 ) ;
66- this . runtimeFactory = new AgentRuntimeFactory (
67- serviceOptions . getAdminforth ,
68- serviceOptions . getCheckpointer ,
69- toolProvider ,
70- ( ) => `adminforth-agent-${ serviceOptions . getPluginInstanceId ( ) } ` ,
71- ) ;
72- this . lifecycle = new TurnLifecycleService ( serviceOptions . sessionStore , persistence ) ;
73- this . promptBuilder = new TurnPromptBuilder ( {
74- getAdminforth : serviceOptions . getAdminforth ,
75- getAgentSystemPrompt : serviceOptions . getAgentSystemPrompt ,
76- } ) ;
77- this . speechTurnService = new SpeechTurnService (
78- this . runAndPersistAgentResponse . bind ( this ) ,
79- ) ;
80- }
29+ constructor (
30+ private readonly lifecycle : TurnLifecycleService ,
31+ private readonly contextBuilder : TurnContextBuilder ,
32+ private readonly modeResolver : AgentModeResolver ,
33+ private readonly modelFactory : AgentModelFactory ,
34+ private readonly promptBuilder : TurnPromptBuilder ,
35+ private readonly runtime : AgentRuntime ,
36+ private readonly streamConsumer : TurnStreamConsumer ,
37+ ) { }
8138
8239 private async prepareTurn ( input : BaseAgentTurnInput ) : Promise < PreparedAgentTurn > {
8340 const sequenceDebugCollector = createSequenceDebugCollector ( ) ;
8441 const { turnId, previousUserMessages } = await this . lifecycle . start ( input ) ;
42+ const context = await this . contextBuilder . build ( {
43+ base : input ,
44+ turnId,
45+ } ) ;
8546
8647 return {
8748 prompt : input . prompt ,
8849 sessionId : input . sessionId ,
8950 turnId,
9051 previousUserMessages,
9152 modeName : input . modeName ,
92- context : {
93- adminUser : input . adminUser ,
94- userTimeZone : input . userTimeZone ,
95- sessionId : input . sessionId ,
96- turnId,
97- abortSignal : input . abortSignal ,
98- currentPage : input . currentPage ,
99- chatSurface : input . chatSurface ,
100- adminPublicOrigin : input . adminPublicOrigin ,
101- } ,
53+ context,
10254 observability : {
10355 emit : undefined ,
10456 sequenceDebugSink : sequenceDebugCollector ,
@@ -119,8 +71,7 @@ export class AgentTurnService {
11971 abortSignal : input . context . abortSignal ,
12072 } ) ,
12173 ] ) ;
122- const runtime = this . runtimeFactory . create ( ) ;
123- const stream = await runtime . stream ( {
74+ const stream = await this . runtime . stream ( {
12475 models,
12576 messages,
12677 context : input . context ,
@@ -214,8 +165,4 @@ export class AgentTurnService {
214165
215166 return agentResponse ;
216167 }
217-
218- async handleSpeechTurn ( input : HandleSpeechTurnInput ) {
219- return this . speechTurnService . handle ( input ) ;
220- }
221168}
0 commit comments