@adminforth/agent 2.0.9 → 2.1.0
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 +333 -10
- package/dist/application/ports.d.ts +66 -0
- package/dist/application/runTurnUseCase.d.ts +68 -0
- package/dist/application/runTurnUseCase.js +358 -0
- package/dist/{agentEvents.d.ts → domain/agentEvents.d.ts} +1 -1
- package/dist/{agent → domain}/systemPrompt.js +1 -1
- package/dist/{agent/turn → domain}/turnTypes.d.ts +41 -37
- package/dist/{agent/turn/VegaLiteStreamBuffer.d.ts → domain/vegaLiteStreamBuffer.d.ts} +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.js +40 -35
- package/dist/{agent/runtime/AgentContext.d.ts → llm/agentContext.d.ts} +3 -3
- package/dist/{agent/simpleAgent.d.ts → llm/agentModels.d.ts} +18 -16
- package/dist/llm/agentModels.js +65 -0
- package/dist/{agent/runtime/AgentRuntime.d.ts → llm/agentRuntime.d.ts} +11 -2
- package/dist/{agent/runtime/AgentRuntime.js → llm/agentRuntime.js} +38 -6
- package/dist/llm/langGraphLlm.d.ts +25 -0
- package/dist/llm/langGraphLlm.js +54 -0
- package/dist/{agent/middleware/apiBasedTools.d.ts → llm/middleware/apiToolsMiddleware.d.ts} +1 -1
- package/dist/{agent/middleware/apiBasedTools.js → llm/middleware/apiToolsMiddleware.js} +8 -14
- package/dist/{agent → llm}/middleware/sequenceDebug.d.ts +1 -1
- package/dist/{agent/models/AgentModelFactory.d.ts → llm/modelFactory.d.ts} +1 -1
- package/dist/{agent/models/AgentModelFactory.js → llm/modelFactory.js} +1 -1
- package/dist/llm/streamAdapter.d.ts +18 -0
- package/dist/llm/streamAdapter.js +117 -0
- package/dist/{sessionStore.d.ts → persistence/sessionStore.d.ts} +12 -2
- package/dist/{sessionStore.js → persistence/sessionStore.js} +33 -0
- package/dist/{agent/tools/AgentToolProvider.d.ts → tools/agentToolProvider.d.ts} +1 -1
- package/dist/{agent/tools/AgentToolProvider.js → tools/agentToolProvider.js} +1 -1
- package/dist/{apiBasedTools.js → tools/apiBasedTools.js} +10 -6
- package/dist/{agent/tools → tools}/apiTool.d.ts +1 -1
- package/dist/{agent/tools → tools}/fetchSkill.js +1 -1
- package/dist/{agent/tools → tools}/fetchToolSchema.d.ts +1 -1
- package/dist/{agent/tools → tools}/index.d.ts +1 -1
- package/dist/{agent/tools → tools}/navigateUser.d.ts +3 -3
- package/dist/{endpoints → transport/http}/context.d.ts +2 -2
- package/dist/transport/http/context.js +1 -0
- package/dist/{endpoints/core.js → transport/http/coreEndpoints.js} +3 -3
- package/dist/{endpoints/sessions.js → transport/http/sessionEndpoints.js} +7 -1
- package/dist/transport/sse/sseWriter.d.ts +29 -0
- package/dist/{surfaces/web-sse/createSseEventEmitter.js → transport/sse/sseWriter.js} +15 -0
- package/dist/{chatSurfaceService.d.ts → transport/surfaces/chatSurfaceService.d.ts} +3 -3
- package/dist/{chatSurfaceService.js → transport/surfaces/chatSurfaceService.js} +7 -5
- package/dist/{agent/speech/SpeechTurnService.d.ts → transport/surfaces/speechTurnService.d.ts} +1 -1
- package/dist/{agent/speech/SpeechTurnService.js → transport/surfaces/speechTurnService.js} +2 -2
- package/dist/types.d.ts +10 -4
- package/package.json +11 -2
- package/.woodpecker/buildRelease.sh +0 -13
- package/.woodpecker/buildSlackNotify.sh +0 -46
- package/.woodpecker/release.yml +0 -57
- package/agent/checkpointer.ts +0 -329
- package/agent/languageDetect.ts +0 -92
- package/agent/middleware/apiBasedTools.ts +0 -174
- package/agent/middleware/openAiResponsesContinuation.ts +0 -92
- package/agent/middleware/sequenceDebug.ts +0 -429
- package/agent/models/AgentModeResolver.ts +0 -9
- package/agent/models/AgentModelFactory.ts +0 -28
- package/agent/runtime/AgentContext.ts +0 -30
- package/agent/runtime/AgentRuntime.ts +0 -89
- package/agent/simpleAgent.ts +0 -199
- package/agent/skills/registry.ts +0 -162
- package/agent/speech/SpeechTurnService.ts +0 -179
- package/agent/systemPrompt.ts +0 -133
- package/agent/toolCallEvents.ts +0 -135
- package/agent/tools/AgentToolProvider.ts +0 -28
- package/agent/tools/apiTool.ts +0 -66
- package/agent/tools/fetchSkill.ts +0 -68
- package/agent/tools/fetchToolSchema.ts +0 -47
- package/agent/tools/getUserLocation.ts +0 -45
- package/agent/tools/index.ts +0 -31
- package/agent/tools/navigateUser.ts +0 -210
- package/agent/turn/TurnContextBuilder.ts +0 -36
- package/agent/turn/TurnLifecycleService.ts +0 -47
- package/agent/turn/TurnPersistenceService.ts +0 -33
- package/agent/turn/TurnPromptBuilder.ts +0 -51
- package/agent/turn/TurnStreamConsumer.ts +0 -69
- package/agent/turn/VegaLiteStreamBuffer.ts +0 -90
- package/agent/turn/turnTypes.ts +0 -100
- package/agentEvents.ts +0 -75
- package/agentTurnService.ts +0 -314
- package/apiBasedTools.ts +0 -702
- package/build.log +0 -67
- package/chatSurfaceService.ts +0 -319
- package/custom/ChatFooter.vue +0 -173
- package/custom/ChatHeader.vue +0 -78
- package/custom/ChatSurface.vue +0 -160
- package/custom/CustomAutoScrollContainer.vue +0 -164
- package/custom/SessionsHistory.vue +0 -88
- package/custom/chat.ts +0 -82
- package/custom/composables/agentAudio/utils.ts +0 -220
- package/custom/composables/agentStore/constants.ts +0 -19
- package/custom/composables/agentStore/pageContext.ts +0 -8
- package/custom/composables/agentStore/useAgentChat.ts +0 -241
- package/custom/composables/agentStore/useAgentPlaceholder.ts +0 -142
- package/custom/composables/agentStore/useAgentSessions.ts +0 -350
- package/custom/composables/useAgentAudio.ts +0 -272
- package/custom/composables/useAgentStore.ts +0 -510
- package/custom/composables/useAgentTransitions.ts +0 -58
- package/custom/conversation_area/ConversationArea.vue +0 -301
- package/custom/conversation_area/MessageRenderer.vue +0 -49
- package/custom/conversation_area/ProcessingTimeline.vue +0 -286
- package/custom/conversation_area/ReasoningRenderer.vue +0 -79
- package/custom/conversation_area/SystemMessageRenderer.vue +0 -28
- package/custom/conversation_area/TextRenderer.vue +0 -182
- package/custom/conversation_area/ThreeDotsAnimation.vue +0 -35
- package/custom/conversation_area/ToolApprovalRenderer.vue +0 -98
- package/custom/conversation_area/ToolRenderer.vue +0 -178
- package/custom/conversation_area/ToolsGroup.vue +0 -59
- package/custom/env.d.ts +0 -7
- package/custom/incremark_code_renderers/IncremarkShikiCodeBlock.vue +0 -393
- package/custom/incremark_code_renderers/incremarkCodeHighlight.ts +0 -285
- package/custom/incremark_code_renderers/incremarkRenderer.ts +0 -653
- package/custom/incremark_code_renderers/renderIncremarkMarkdown.ts +0 -118
- package/custom/package.json +0 -30
- package/custom/pnpm-lock.yaml +0 -2394
- package/custom/public/agentAudio/agent-processing.mp3 +0 -0
- package/custom/skills/analyze_data/SKILL.md +0 -25
- package/custom/skills/charts/SKILL.md +0 -193
- package/custom/skills/fetch_data/SKILL.md +0 -31
- package/custom/skills/mutate_data/SKILL.md +0 -121
- package/custom/speech_recognition_frontend/AudioLines.vue +0 -43
- package/custom/speech_recognition_frontend/MicrophoneButon.vue +0 -185
- package/custom/speech_recognition_frontend/types/voice-activity-detection.d.ts +0 -22
- package/custom/speech_recognition_frontend/voiceActivityDetection.ts +0 -62
- package/custom/tsconfig.json +0 -15
- package/custom/types.ts +0 -108
- package/custom/utils.ts +0 -38
- package/dist/agent/middleware/openAiResponsesContinuation.d.ts +0 -1
- package/dist/agent/middleware/openAiResponsesContinuation.js +0 -66
- package/dist/agent/models/AgentModeResolver.d.ts +0 -9
- package/dist/agent/models/AgentModeResolver.js +0 -9
- package/dist/agent/simpleAgent.js +0 -118
- package/dist/agent/turn/TurnContextBuilder.d.ts +0 -14
- package/dist/agent/turn/TurnContextBuilder.js +0 -31
- package/dist/agent/turn/TurnLifecycleService.d.ts +0 -24
- package/dist/agent/turn/TurnLifecycleService.js +0 -47
- package/dist/agent/turn/TurnPersistenceService.d.ts +0 -13
- package/dist/agent/turn/TurnPersistenceService.js +0 -35
- package/dist/agent/turn/TurnPromptBuilder.d.ts +0 -19
- package/dist/agent/turn/TurnPromptBuilder.js +0 -43
- package/dist/agent/turn/TurnStreamConsumer.d.ts +0 -11
- package/dist/agent/turn/TurnStreamConsumer.js +0 -84
- package/dist/agentTurnService.d.ts +0 -24
- package/dist/agentTurnService.js +0 -269
- package/dist/surfaces/web-sse/createSseEventEmitter.d.ts +0 -14
- package/endpoints/chatSurfaces.ts +0 -66
- package/endpoints/context.ts +0 -52
- package/endpoints/core.ts +0 -181
- package/endpoints/sessions.ts +0 -183
- package/errors.ts +0 -10
- package/index.ts +0 -192
- package/pnpm-workspace.yaml +0 -4
- package/sanitizeSpeechText.ts +0 -32
- package/sessionStore.ts +0 -105
- package/surfaces/web-sse/createSseEventEmitter.ts +0 -311
- package/tsconfig.json +0 -14
- package/types.ts +0 -127
- /package/dist/{agent/turn/turnTypes.js → application/ports.js} +0 -0
- /package/dist/{agentEvents.js → domain/agentEvents.js} +0 -0
- /package/dist/{agent → domain}/languageDetect.d.ts +0 -0
- /package/dist/{agent → domain}/languageDetect.js +0 -0
- /package/dist/{agent → domain}/systemPrompt.d.ts +0 -0
- /package/dist/{agent → domain}/toolCallEvents.d.ts +0 -0
- /package/dist/{agent → domain}/toolCallEvents.js +0 -0
- /package/dist/{endpoints/context.js → domain/turnTypes.js} +0 -0
- /package/dist/{agent/turn/VegaLiteStreamBuffer.js → domain/vegaLiteStreamBuffer.js} +0 -0
- /package/dist/{agent/runtime/AgentContext.js → llm/agentContext.js} +0 -0
- /package/dist/{agent → llm}/middleware/sequenceDebug.js +0 -0
- /package/dist/{agent/checkpointer.d.ts → persistence/checkpointStore.d.ts} +0 -0
- /package/dist/{agent/checkpointer.js → persistence/checkpointStore.js} +0 -0
- /package/dist/{errors.d.ts → shared/errors.d.ts} +0 -0
- /package/dist/{errors.js → shared/errors.js} +0 -0
- /package/dist/{sanitizeSpeechText.d.ts → shared/sanitizeSpeechText.d.ts} +0 -0
- /package/dist/{sanitizeSpeechText.js → shared/sanitizeSpeechText.js} +0 -0
- /package/dist/{apiBasedTools.d.ts → tools/apiBasedTools.d.ts} +0 -0
- /package/dist/{agent/tools → tools}/apiTool.js +0 -0
- /package/dist/{agent/tools → tools}/fetchSkill.d.ts +0 -0
- /package/dist/{agent/tools → tools}/fetchToolSchema.js +0 -0
- /package/dist/{agent/tools → tools}/getUserLocation.d.ts +0 -0
- /package/dist/{agent/tools → tools}/getUserLocation.js +0 -0
- /package/dist/{agent/tools → tools}/index.js +0 -0
- /package/dist/{agent/tools → tools}/navigateUser.js +0 -0
- /package/dist/{agent → tools}/skills/registry.d.ts +0 -0
- /package/dist/{agent → tools}/skills/registry.js +0 -0
- /package/dist/{endpoints/chatSurfaces.d.ts → transport/http/chatSurfaceEndpoints.d.ts} +0 -0
- /package/dist/{endpoints/chatSurfaces.js → transport/http/chatSurfaceEndpoints.js} +0 -0
- /package/dist/{endpoints/core.d.ts → transport/http/coreEndpoints.d.ts} +0 -0
- /package/dist/{endpoints/sessions.d.ts → transport/http/sessionEndpoints.d.ts} +0 -0
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import { MicVAD, utils } from "@ricky0123/vad-web";
|
|
2
|
-
|
|
3
|
-
let VADInstance: any = null;
|
|
4
|
-
let recordedAudioChunks: Float32Array[] = [];
|
|
5
|
-
let onVoiceStopCallback: () => void = () => {};
|
|
6
|
-
let onVoiceStartCallback: () => void = () => {};
|
|
7
|
-
let onUpdateCallback: (amplitude: number) => void = () => {};
|
|
8
|
-
|
|
9
|
-
async function createVADInstance(){
|
|
10
|
-
VADInstance = await MicVAD.new({
|
|
11
|
-
onFrameProcessed: ({ isSpeech }) => {
|
|
12
|
-
onUpdateCallback(isSpeech);
|
|
13
|
-
},
|
|
14
|
-
onSpeechEnd: (audio) => {
|
|
15
|
-
recordedAudioChunks.push(audio);
|
|
16
|
-
onVoiceStopCallback();
|
|
17
|
-
},
|
|
18
|
-
onSpeechStart: () => {
|
|
19
|
-
onVoiceStartCallback();
|
|
20
|
-
},
|
|
21
|
-
onnxWASMBasePath: "https://cdn.jsdelivr.net/npm/onnxruntime-web@1.22.0/dist/",
|
|
22
|
-
baseAssetPath: "https://cdn.jsdelivr.net/npm/@ricky0123/vad-web@0.0.29/dist/",
|
|
23
|
-
})
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export async function requestMicAndStartVAD(
|
|
27
|
-
onVoiceStop: () => void,
|
|
28
|
-
onVoiceStart: () => void,
|
|
29
|
-
onUpdate: (amplitude: number) => void
|
|
30
|
-
) {
|
|
31
|
-
onVoiceStopCallback = onVoiceStop;
|
|
32
|
-
onVoiceStartCallback = onVoiceStart;
|
|
33
|
-
onUpdateCallback = onUpdate;
|
|
34
|
-
|
|
35
|
-
if (!VADInstance) {
|
|
36
|
-
await createVADInstance();
|
|
37
|
-
}
|
|
38
|
-
VADInstance?.start();
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export async function stopUserMedia() {
|
|
42
|
-
await VADInstance?.pause();
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export async function getRecord() {
|
|
46
|
-
const totalSamples = recordedAudioChunks.reduce((sum, chunk) => sum + chunk.length, 0);
|
|
47
|
-
if (totalSamples === 0) {
|
|
48
|
-
return null;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const mergedAudio = new Float32Array(totalSamples);
|
|
52
|
-
let offset = 0;
|
|
53
|
-
for (const chunk of recordedAudioChunks) {
|
|
54
|
-
mergedAudio.set(chunk, offset);
|
|
55
|
-
offset += chunk.length;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const wavBuffer = utils.encodeWAV(mergedAudio, 1, 16000, 1, 16);
|
|
59
|
-
const recordToReturn = new Blob([wavBuffer], { type: 'audio/wav' });
|
|
60
|
-
recordedAudioChunks = [];
|
|
61
|
-
return recordToReturn;
|
|
62
|
-
}
|
package/custom/tsconfig.json
DELETED
package/custom/types.ts
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
export interface IPartData {
|
|
2
|
-
toolCallId?: string;
|
|
3
|
-
toolName?: string;
|
|
4
|
-
sessionId?: string;
|
|
5
|
-
phase?: 'start' | 'end';
|
|
6
|
-
label?: string;
|
|
7
|
-
input?: any;
|
|
8
|
-
output?: any;
|
|
9
|
-
durationMs?: number;
|
|
10
|
-
toolInfo?: string;
|
|
11
|
-
status?: 'pending' | 'processing' | 'approved' | 'rejected';
|
|
12
|
-
messages?: string[];
|
|
13
|
-
error?: string;
|
|
14
|
-
}
|
|
15
|
-
export interface IPart {
|
|
16
|
-
type: 'reasoning' | 'data-tool-call' | 'data-rendering' | 'data-tool-approval' | 'text';
|
|
17
|
-
text?: string;
|
|
18
|
-
state?: 'started' | 'thinking' | 'processing' | 'streaming' | 'done';
|
|
19
|
-
data?: IPartData;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export interface IFormattedToolCallPart {
|
|
23
|
-
type: 'data-tool-call';
|
|
24
|
-
toolInfo: IPartData;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface IToolGroup {
|
|
28
|
-
title: string;
|
|
29
|
-
groupedTools: IFormattedToolCallPart[];
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface IMessage {
|
|
33
|
-
role: 'user' | 'assistant' | 'system';
|
|
34
|
-
metadata?: any,
|
|
35
|
-
parts: IPart[];
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export interface IAgentSession {
|
|
39
|
-
sessionId: string;
|
|
40
|
-
title: string;
|
|
41
|
-
timestamp: string;
|
|
42
|
-
messages: IMessage[];
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export interface ISessionsListItem {
|
|
46
|
-
sessionId: string;
|
|
47
|
-
title: string;
|
|
48
|
-
timestamp: string;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export type SpeechStreamEvent =
|
|
52
|
-
| {
|
|
53
|
-
type: 'error';
|
|
54
|
-
error: string;
|
|
55
|
-
}
|
|
56
|
-
| {
|
|
57
|
-
type: 'transcript';
|
|
58
|
-
data: {
|
|
59
|
-
text: string;
|
|
60
|
-
language?: string;
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
| {
|
|
64
|
-
type: 'speech-response';
|
|
65
|
-
data: {
|
|
66
|
-
transcript: {
|
|
67
|
-
text: string;
|
|
68
|
-
language?: string;
|
|
69
|
-
};
|
|
70
|
-
response: {
|
|
71
|
-
text: string;
|
|
72
|
-
};
|
|
73
|
-
sessionId: string;
|
|
74
|
-
turnId: string;
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
| {
|
|
78
|
-
type: 'audio-start';
|
|
79
|
-
data: {
|
|
80
|
-
mimeType: string;
|
|
81
|
-
format: string;
|
|
82
|
-
sampleRate: number;
|
|
83
|
-
channelCount: number;
|
|
84
|
-
bitsPerSample: number;
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
| {
|
|
88
|
-
type: 'audio-delta';
|
|
89
|
-
data: {
|
|
90
|
-
base64: string;
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
| {
|
|
94
|
-
type: 'audio-done';
|
|
95
|
-
}
|
|
96
|
-
| {
|
|
97
|
-
type: 'data-tool-call';
|
|
98
|
-
data: any;
|
|
99
|
-
}
|
|
100
|
-
| {
|
|
101
|
-
type: 'open-page';
|
|
102
|
-
data: {
|
|
103
|
-
targetPath: string;
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
| {
|
|
107
|
-
type: 'finish';
|
|
108
|
-
};
|
package/custom/utils.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { IMessage, IPart } from "./types";
|
|
2
|
-
|
|
3
|
-
export function remToPx(rem: number): number {
|
|
4
|
-
const rootFontSize = parseFloat(getComputedStyle(document.documentElement).fontSize);
|
|
5
|
-
return rem * rootFontSize;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export function pxToRem(px: number): number {
|
|
9
|
-
const rootFontSize = parseFloat(getComputedStyle(document.documentElement).fontSize);
|
|
10
|
-
return px / rootFontSize;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export function getMessageParts(message: IMessage): IPart[] {
|
|
15
|
-
return message.parts?.length
|
|
16
|
-
? message.parts
|
|
17
|
-
: [{ text: '', type: 'reasoning', state: 'streaming' }];
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function addNewLineBeforeTitles(text: string): string {
|
|
21
|
-
return text.replace(/(\*\*[^*]+\*\*)/g, '\n\n$1');
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function extractTitleAndTextFromReasoning(reasoningText: string): { title: string | null; body: string } {
|
|
25
|
-
const match = reasoningText.match(/^\*\*(.*?)\*\*(.*)$/s);
|
|
26
|
-
|
|
27
|
-
if (!match) {
|
|
28
|
-
return {
|
|
29
|
-
title: null,
|
|
30
|
-
body: addNewLineBeforeTitles(reasoningText)
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return {
|
|
35
|
-
title: match[1].trim(),
|
|
36
|
-
body: addNewLineBeforeTitles(match[2].trim())
|
|
37
|
-
};
|
|
38
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function createOpenAiResponsesContinuationMiddleware(): import("langchain").AgentMiddleware<undefined, undefined, unknown, readonly (import("@langchain/core/tools").ClientTool | import("@langchain/core/tools").ServerTool)[]>;
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { AIMessage } from "@langchain/core/messages";
|
|
11
|
-
import { createMiddleware } from "langchain";
|
|
12
|
-
function getTurnKey(context) {
|
|
13
|
-
return `${context.sessionId}:${context.turnId}`;
|
|
14
|
-
}
|
|
15
|
-
function getResponseId(message) {
|
|
16
|
-
var _a;
|
|
17
|
-
const metadata = message.response_metadata;
|
|
18
|
-
return (_a = metadata === null || metadata === void 0 ? void 0 : metadata.id) !== null && _a !== void 0 ? _a : null;
|
|
19
|
-
}
|
|
20
|
-
function getPreviousResponseId(modelSettings) {
|
|
21
|
-
return modelSettings === null || modelSettings === void 0 ? void 0 : modelSettings.previous_response_id;
|
|
22
|
-
}
|
|
23
|
-
function getContinuationMessages(messages, previousResponseId) {
|
|
24
|
-
var _a;
|
|
25
|
-
let continuationStartIndex = null;
|
|
26
|
-
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
27
|
-
const message = messages[index];
|
|
28
|
-
if (AIMessage.isInstance(message) &&
|
|
29
|
-
((_a = message.response_metadata) === null || _a === void 0 ? void 0 : _a.id) ===
|
|
30
|
-
previousResponseId) {
|
|
31
|
-
continuationStartIndex = index + 1;
|
|
32
|
-
break;
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
if (continuationStartIndex === null) {
|
|
36
|
-
return null;
|
|
37
|
-
}
|
|
38
|
-
return messages.slice(continuationStartIndex);
|
|
39
|
-
}
|
|
40
|
-
export function createOpenAiResponsesContinuationMiddleware() {
|
|
41
|
-
const responseIdsByTurn = new Map();
|
|
42
|
-
return createMiddleware({
|
|
43
|
-
name: "OpenAiResponsesContinuationMiddleware",
|
|
44
|
-
wrapModelCall(request, handler) {
|
|
45
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
-
var _a;
|
|
47
|
-
const context = request.runtime.context;
|
|
48
|
-
const turnKey = getTurnKey(context);
|
|
49
|
-
const previousResponseId = (_a = getPreviousResponseId(request.modelSettings)) !== null && _a !== void 0 ? _a : responseIdsByTurn.get(turnKey);
|
|
50
|
-
const continuationMessages = previousResponseId
|
|
51
|
-
? getContinuationMessages(request.messages, previousResponseId)
|
|
52
|
-
: null;
|
|
53
|
-
const response = yield handler(previousResponseId && continuationMessages
|
|
54
|
-
? Object.assign(Object.assign({}, request), { messages: continuationMessages, modelSettings: Object.assign(Object.assign({}, request.modelSettings), { previous_response_id: previousResponseId }) }) : request);
|
|
55
|
-
const responseId = getResponseId(response);
|
|
56
|
-
if (responseId) {
|
|
57
|
-
responseIdsByTurn.set(turnKey, responseId);
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
responseIdsByTurn.delete(turnKey);
|
|
61
|
-
}
|
|
62
|
-
return response;
|
|
63
|
-
});
|
|
64
|
-
},
|
|
65
|
-
});
|
|
66
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { PluginOptions } from "../../types.js";
|
|
2
|
-
export declare class AgentModeResolver {
|
|
3
|
-
private readonly options;
|
|
4
|
-
constructor(options: PluginOptions);
|
|
5
|
-
resolve(modeName?: string | null): {
|
|
6
|
-
name: string;
|
|
7
|
-
completionAdapter: import("../simpleAgent.js").AgentModeCompletionAdapter;
|
|
8
|
-
};
|
|
9
|
-
}
|
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { logger, } from "adminforth";
|
|
11
|
-
import { BaseCallbackHandler } from "@langchain/core/callbacks/base";
|
|
12
|
-
function isLangChainAgentCompletionAdapter(adapter) {
|
|
13
|
-
return typeof adapter
|
|
14
|
-
.getLangChainAgentSpec === "function";
|
|
15
|
-
}
|
|
16
|
-
function getAgentChatModelSpec(params) {
|
|
17
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
18
|
-
var _a;
|
|
19
|
-
const spec = yield params.adapter.getLangChainAgentSpec({
|
|
20
|
-
maxTokens: params.maxTokens,
|
|
21
|
-
purpose: params.purpose,
|
|
22
|
-
});
|
|
23
|
-
return {
|
|
24
|
-
model: spec.model,
|
|
25
|
-
middleware: ((_a = spec.middleware) !== null && _a !== void 0 ? _a : []),
|
|
26
|
-
};
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
function getFiniteNumber(value) {
|
|
30
|
-
return typeof value === "number" && Number.isFinite(value)
|
|
31
|
-
? value
|
|
32
|
-
: undefined;
|
|
33
|
-
}
|
|
34
|
-
function extractTokenUsage(output) {
|
|
35
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
|
36
|
-
const llmOutputTokenUsage = (_a = output.llmOutput) === null || _a === void 0 ? void 0 : _a.tokenUsage;
|
|
37
|
-
const promptTokens = getFiniteNumber(llmOutputTokenUsage === null || llmOutputTokenUsage === void 0 ? void 0 : llmOutputTokenUsage.promptTokens);
|
|
38
|
-
const completionTokens = getFiniteNumber(llmOutputTokenUsage === null || llmOutputTokenUsage === void 0 ? void 0 : llmOutputTokenUsage.completionTokens);
|
|
39
|
-
if (promptTokens !== undefined || completionTokens !== undefined) {
|
|
40
|
-
return {
|
|
41
|
-
InputTokens: promptTokens !== null && promptTokens !== void 0 ? promptTokens : 0,
|
|
42
|
-
outputTokens: completionTokens !== null && completionTokens !== void 0 ? completionTokens : 0,
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
let InputTokens = 0;
|
|
46
|
-
let outputTokens = 0;
|
|
47
|
-
for (const generationBatch of output.generations) {
|
|
48
|
-
for (const generation of generationBatch) {
|
|
49
|
-
if (!("message" in generation) || !generation.message) {
|
|
50
|
-
continue;
|
|
51
|
-
}
|
|
52
|
-
const message = generation.message;
|
|
53
|
-
InputTokens +=
|
|
54
|
-
(_f = (_c = getFiniteNumber((_b = message.usage_metadata) === null || _b === void 0 ? void 0 : _b.input_tokens)) !== null && _c !== void 0 ? _c : getFiniteNumber((_e = (_d = message.response_metadata) === null || _d === void 0 ? void 0 : _d.tokenUsage) === null || _e === void 0 ? void 0 : _e.promptTokens)) !== null && _f !== void 0 ? _f : 0;
|
|
55
|
-
outputTokens +=
|
|
56
|
-
(_l = (_h = getFiniteNumber((_g = message.usage_metadata) === null || _g === void 0 ? void 0 : _g.output_tokens)) !== null && _h !== void 0 ? _h : getFiniteNumber((_k = (_j = message.response_metadata) === null || _j === void 0 ? void 0 : _j.tokenUsage) === null || _k === void 0 ? void 0 : _k.completionTokens)) !== null && _l !== void 0 ? _l : 0;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
return { InputTokens, outputTokens };
|
|
60
|
-
}
|
|
61
|
-
class AgentLlmMetricsLogger extends BaseCallbackHandler {
|
|
62
|
-
constructor() {
|
|
63
|
-
super(...arguments);
|
|
64
|
-
this.name = "AgentLlmMetricsLogger";
|
|
65
|
-
this.lc_prefer_streaming = true;
|
|
66
|
-
this.pendingRuns = new Map();
|
|
67
|
-
}
|
|
68
|
-
handleLLMStart(_llm, _prompts, runId) {
|
|
69
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
-
this.pendingRuns.set(runId, { startedAt: Date.now() });
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
handleLLMNewToken(_token, _chunk, runId) {
|
|
74
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
75
|
-
const pendingRun = this.pendingRuns.get(runId);
|
|
76
|
-
if (!pendingRun || pendingRun.firstTokenAt !== undefined) {
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
pendingRun.firstTokenAt = Date.now();
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
handleLLMEnd(output, runId) {
|
|
83
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
84
|
-
const pendingRun = this.pendingRuns.get(runId);
|
|
85
|
-
if (!pendingRun) {
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
this.pendingRuns.delete(runId);
|
|
89
|
-
const finishedAt = Date.now();
|
|
90
|
-
const rtt = finishedAt - pendingRun.startedAt;
|
|
91
|
-
const ttft = pendingRun.firstTokenAt === undefined
|
|
92
|
-
? rtt
|
|
93
|
-
: pendingRun.firstTokenAt - pendingRun.startedAt;
|
|
94
|
-
const { InputTokens, outputTokens } = extractTokenUsage(output);
|
|
95
|
-
logger.info({ InputTokens, outputTokens, ttft, rtt }, "LLM call finished");
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
handleLLMError(_error, runId) {
|
|
99
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
100
|
-
this.pendingRuns.delete(runId);
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
export function createAgentLlmMetricsLogger() {
|
|
105
|
-
return new AgentLlmMetricsLogger();
|
|
106
|
-
}
|
|
107
|
-
export function createAgentChatModel(params) {
|
|
108
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
109
|
-
if (!isLangChainAgentCompletionAdapter(params.adapter)) {
|
|
110
|
-
throw new Error("AdminForth Agent requires completionAdapter to implement getLangChainAgentSpec({ maxTokens, purpose }).");
|
|
111
|
-
}
|
|
112
|
-
return yield getAgentChatModelSpec({
|
|
113
|
-
adapter: params.adapter,
|
|
114
|
-
maxTokens: params.maxTokens,
|
|
115
|
-
purpose: params.purpose,
|
|
116
|
-
});
|
|
117
|
-
});
|
|
118
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { AdminUser, IAdminForth } from "adminforth";
|
|
2
|
-
import type { AgentTurnContext, BaseAgentTurnInput } from "./turnTypes.js";
|
|
3
|
-
export type UserContextProvider = {
|
|
4
|
-
getUserTimeZone(adminUser: AdminUser): Promise<string | null | undefined> | string | null | undefined;
|
|
5
|
-
};
|
|
6
|
-
export declare class TurnContextBuilder {
|
|
7
|
-
private readonly getAdminforth;
|
|
8
|
-
private readonly userContextProvider?;
|
|
9
|
-
constructor(getAdminforth: () => IAdminForth, userContextProvider?: UserContextProvider | undefined);
|
|
10
|
-
build(input: {
|
|
11
|
-
base: BaseAgentTurnInput;
|
|
12
|
-
turnId: string;
|
|
13
|
-
}): Promise<AgentTurnContext>;
|
|
14
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
export class TurnContextBuilder {
|
|
11
|
-
constructor(getAdminforth, userContextProvider) {
|
|
12
|
-
this.getAdminforth = getAdminforth;
|
|
13
|
-
this.userContextProvider = userContextProvider;
|
|
14
|
-
}
|
|
15
|
-
build(input) {
|
|
16
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
17
|
-
var _a, _b, _c, _d;
|
|
18
|
-
const adminforth = this.getAdminforth();
|
|
19
|
-
return {
|
|
20
|
-
adminUser: input.base.adminUser,
|
|
21
|
-
sessionId: input.base.sessionId,
|
|
22
|
-
turnId: input.turnId,
|
|
23
|
-
abortSignal: input.base.abortSignal,
|
|
24
|
-
currentPage: input.base.currentPage,
|
|
25
|
-
chatSurface: input.base.chatSurface,
|
|
26
|
-
userTimeZone: (_c = (_a = input.base.userTimeZone) !== null && _a !== void 0 ? _a : yield ((_b = this.userContextProvider) === null || _b === void 0 ? void 0 : _b.getUserTimeZone(input.base.adminUser))) !== null && _c !== void 0 ? _c : "UTC",
|
|
27
|
-
adminPublicOrigin: (_d = input.base.adminPublicOrigin) !== null && _d !== void 0 ? _d : adminforth.config.baseUrlSlashed,
|
|
28
|
-
};
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import type { AgentSessionStore } from "../../sessionStore.js";
|
|
2
|
-
import type { PluginOptions } from "../../types.js";
|
|
3
|
-
import type { BaseAgentTurnInput } from "./turnTypes.js";
|
|
4
|
-
import { TurnPersistenceService } from "./TurnPersistenceService.js";
|
|
5
|
-
export declare class TurnLifecycleService {
|
|
6
|
-
private readonly sessionStore;
|
|
7
|
-
private readonly persistence;
|
|
8
|
-
private readonly options;
|
|
9
|
-
constructor(sessionStore: AgentSessionStore, persistence: TurnPersistenceService, options: PluginOptions);
|
|
10
|
-
start(input: BaseAgentTurnInput): Promise<{
|
|
11
|
-
turnId: any;
|
|
12
|
-
previousUserMessages: import("../languageDetect.js").PreviousUserMessage[];
|
|
13
|
-
}>;
|
|
14
|
-
resume(input: BaseAgentTurnInput): Promise<{
|
|
15
|
-
turnId: any;
|
|
16
|
-
previousUserMessages: import("../languageDetect.js").PreviousUserMessage[];
|
|
17
|
-
initialResponse: string;
|
|
18
|
-
}>;
|
|
19
|
-
finish(input: {
|
|
20
|
-
turnId: string;
|
|
21
|
-
responseText: string;
|
|
22
|
-
debugHistory?: unknown;
|
|
23
|
-
}): Promise<void>;
|
|
24
|
-
}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
export class TurnLifecycleService {
|
|
11
|
-
constructor(sessionStore, persistence, options) {
|
|
12
|
-
this.sessionStore = sessionStore;
|
|
13
|
-
this.persistence = persistence;
|
|
14
|
-
this.options = options;
|
|
15
|
-
}
|
|
16
|
-
start(input) {
|
|
17
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
18
|
-
const previousUserMessages = yield this.sessionStore.getPreviousUserMessages(input.sessionId);
|
|
19
|
-
const turnId = yield this.sessionStore.createNewTurn(input.sessionId, input.prompt);
|
|
20
|
-
yield this.persistence.touchSession(input.sessionId);
|
|
21
|
-
return {
|
|
22
|
-
turnId,
|
|
23
|
-
previousUserMessages,
|
|
24
|
-
};
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
resume(input) {
|
|
28
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
-
const latestTurn = yield this.sessionStore.getLatestTurn(input.sessionId);
|
|
30
|
-
if (!latestTurn) {
|
|
31
|
-
throw new Error(`No agent turn found for session "${input.sessionId}".`);
|
|
32
|
-
}
|
|
33
|
-
return {
|
|
34
|
-
turnId: latestTurn[this.options.turnResource.idField],
|
|
35
|
-
previousUserMessages: yield this.sessionStore.getPreviousUserMessages(input.sessionId),
|
|
36
|
-
initialResponse: latestTurn[this.options.turnResource.responseField] === "not_finished"
|
|
37
|
-
? ""
|
|
38
|
-
: String(latestTurn[this.options.turnResource.responseField]),
|
|
39
|
-
};
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
finish(input) {
|
|
43
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
-
yield this.persistence.saveTurnResponse(input);
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { IAdminForth } from "adminforth";
|
|
2
|
-
import type { PluginOptions } from "../../types.js";
|
|
3
|
-
export declare class TurnPersistenceService {
|
|
4
|
-
private readonly getAdminforth;
|
|
5
|
-
private readonly options;
|
|
6
|
-
constructor(getAdminforth: () => IAdminForth, options: PluginOptions);
|
|
7
|
-
touchSession(sessionId: string): Promise<void>;
|
|
8
|
-
saveTurnResponse(input: {
|
|
9
|
-
turnId: string;
|
|
10
|
-
responseText: string;
|
|
11
|
-
debugHistory?: unknown;
|
|
12
|
-
}): Promise<void>;
|
|
13
|
-
}
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
export class TurnPersistenceService {
|
|
11
|
-
constructor(getAdminforth, options) {
|
|
12
|
-
this.getAdminforth = getAdminforth;
|
|
13
|
-
this.options = options;
|
|
14
|
-
}
|
|
15
|
-
touchSession(sessionId) {
|
|
16
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
17
|
-
yield this.getAdminforth().resource(this.options.sessionResource.resourceId).update(sessionId, {
|
|
18
|
-
[this.options.sessionResource.createdAtField]: new Date().toISOString(),
|
|
19
|
-
});
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
saveTurnResponse(input) {
|
|
23
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
24
|
-
const turnUpdates = {
|
|
25
|
-
[this.options.turnResource.responseField]: input.responseText,
|
|
26
|
-
};
|
|
27
|
-
if (this.options.turnResource.debugField) {
|
|
28
|
-
turnUpdates[this.options.turnResource.debugField] = input.debugHistory;
|
|
29
|
-
}
|
|
30
|
-
yield this.getAdminforth()
|
|
31
|
-
.resource(this.options.turnResource.resourceId)
|
|
32
|
-
.update(input.turnId, turnUpdates);
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import type { AdminUser, IAdminForth } from "adminforth";
|
|
2
|
-
import { HumanMessage, SystemMessage } from "langchain";
|
|
3
|
-
import { type PreviousUserMessage } from "../languageDetect.js";
|
|
4
|
-
import type { AgentModeCompletionAdapter } from "../simpleAgent.js";
|
|
5
|
-
export declare class TurnPromptBuilder {
|
|
6
|
-
private readonly options;
|
|
7
|
-
constructor(options: {
|
|
8
|
-
getAgentSystemPrompt: () => Promise<string>;
|
|
9
|
-
getAdminforth: () => IAdminForth;
|
|
10
|
-
});
|
|
11
|
-
build(input: {
|
|
12
|
-
prompt: string;
|
|
13
|
-
previousUserMessages: PreviousUserMessage[];
|
|
14
|
-
adminUser: AdminUser;
|
|
15
|
-
completionAdapter: AgentModeCompletionAdapter;
|
|
16
|
-
chatSurface?: string;
|
|
17
|
-
abortSignal?: AbortSignal;
|
|
18
|
-
}): Promise<(SystemMessage<import("@langchain/core/messages").MessageStructure<import("@langchain/core/messages").MessageToolSet>> | HumanMessage<import("@langchain/core/messages").MessageStructure<import("@langchain/core/messages").MessageToolSet>>)[]>;
|
|
19
|
-
}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { logger } from "adminforth";
|
|
11
|
-
import { HumanMessage, SystemMessage } from "langchain";
|
|
12
|
-
import { detectUserLanguage } from "../languageDetect.js";
|
|
13
|
-
import { buildAgentTurnSystemPrompt } from "../systemPrompt.js";
|
|
14
|
-
import { getErrorMessage, isAbortError } from "../../errors.js";
|
|
15
|
-
export class TurnPromptBuilder {
|
|
16
|
-
constructor(options) {
|
|
17
|
-
this.options = options;
|
|
18
|
-
}
|
|
19
|
-
build(input) {
|
|
20
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
21
|
-
const adminforth = this.options.getAdminforth();
|
|
22
|
-
const userLanguage = yield detectUserLanguage(input.completionAdapter, input.prompt, input.previousUserMessages).catch((error) => {
|
|
23
|
-
var _a;
|
|
24
|
-
if (((_a = input.abortSignal) === null || _a === void 0 ? void 0 : _a.aborted) || isAbortError(error)) {
|
|
25
|
-
throw error;
|
|
26
|
-
}
|
|
27
|
-
logger.warn(`Failed to detect user language: ${getErrorMessage(error)}`);
|
|
28
|
-
return null;
|
|
29
|
-
});
|
|
30
|
-
const systemPrompt = buildAgentTurnSystemPrompt({
|
|
31
|
-
agentSystemPrompt: yield this.options.getAgentSystemPrompt(),
|
|
32
|
-
adminUser: input.adminUser,
|
|
33
|
-
usernameField: adminforth.config.auth.usernameField,
|
|
34
|
-
userLanguage,
|
|
35
|
-
chatSurface: input.chatSurface,
|
|
36
|
-
});
|
|
37
|
-
return [
|
|
38
|
-
new SystemMessage(systemPrompt),
|
|
39
|
-
new HumanMessage(input.prompt),
|
|
40
|
-
];
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
}
|