@adminforth/agent 2.0.10 → 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} +0 -3
- 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 -712
- 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,11 +0,0 @@
|
|
|
1
|
-
import type { AgentEventEmitter } from "../../agentEvents.js";
|
|
2
|
-
export declare class TurnStreamConsumer {
|
|
3
|
-
consume(input: {
|
|
4
|
-
stream: AsyncIterable<["messages", [any, any]] | ["updates", Record<string, any>]>;
|
|
5
|
-
abortSignal?: AbortSignal;
|
|
6
|
-
emit?: AgentEventEmitter;
|
|
7
|
-
onInterrupt?: (interrupt: unknown) => void | Promise<void>;
|
|
8
|
-
}): Promise<{
|
|
9
|
-
text: string;
|
|
10
|
-
}>;
|
|
11
|
-
}
|
|
@@ -1,84 +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
|
-
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
11
|
-
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
12
|
-
var m = o[Symbol.asyncIterator], i;
|
|
13
|
-
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
14
|
-
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
15
|
-
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
16
|
-
};
|
|
17
|
-
import { VegaLiteStreamBuffer } from "./VegaLiteStreamBuffer.js";
|
|
18
|
-
export class TurnStreamConsumer {
|
|
19
|
-
consume(input) {
|
|
20
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
21
|
-
var _a, e_1, _b, _c;
|
|
22
|
-
var _d, _e, _f;
|
|
23
|
-
let fullResponse = "";
|
|
24
|
-
const textBuffer = new VegaLiteStreamBuffer();
|
|
25
|
-
try {
|
|
26
|
-
for (var _g = true, _h = __asyncValues(input.stream), _j; _j = yield _h.next(), _a = _j.done, !_a; _g = true) {
|
|
27
|
-
_c = _j.value;
|
|
28
|
-
_g = false;
|
|
29
|
-
const [mode, chunk] = _c;
|
|
30
|
-
if ((_d = input.abortSignal) === null || _d === void 0 ? void 0 : _d.aborted) {
|
|
31
|
-
throw new DOMException("This operation was aborted", "AbortError");
|
|
32
|
-
}
|
|
33
|
-
if (mode === "updates") {
|
|
34
|
-
if ("__interrupt__" in chunk) {
|
|
35
|
-
yield ((_e = input.onInterrupt) === null || _e === void 0 ? void 0 : _e.call(input, chunk.__interrupt__));
|
|
36
|
-
}
|
|
37
|
-
continue;
|
|
38
|
-
}
|
|
39
|
-
const [token, metadata] = chunk;
|
|
40
|
-
const nodeName = typeof (metadata === null || metadata === void 0 ? void 0 : metadata.langgraph_node) === "string"
|
|
41
|
-
? metadata.langgraph_node
|
|
42
|
-
: "";
|
|
43
|
-
if (nodeName && !["model", "model_request"].includes(nodeName)) {
|
|
44
|
-
continue;
|
|
45
|
-
}
|
|
46
|
-
const blocks = Array.isArray(token === null || token === void 0 ? void 0 : token.contentBlocks)
|
|
47
|
-
? token.contentBlocks
|
|
48
|
-
: Array.isArray(token === null || token === void 0 ? void 0 : token.content)
|
|
49
|
-
? token.content
|
|
50
|
-
: [];
|
|
51
|
-
const reasoningDelta = blocks
|
|
52
|
-
.filter((block) => (block === null || block === void 0 ? void 0 : block.type) === "reasoning")
|
|
53
|
-
.map((block) => { var _a; return String((_a = block.reasoning) !== null && _a !== void 0 ? _a : ""); })
|
|
54
|
-
.join("");
|
|
55
|
-
const textDelta = blocks
|
|
56
|
-
.filter((block) => (block === null || block === void 0 ? void 0 : block.type) === "text")
|
|
57
|
-
.map((block) => { var _a; return String((_a = block.text) !== null && _a !== void 0 ? _a : ""); })
|
|
58
|
-
.join("");
|
|
59
|
-
if (reasoningDelta) {
|
|
60
|
-
yield ((_f = input.emit) === null || _f === void 0 ? void 0 : _f.call(input, {
|
|
61
|
-
type: "reasoning-delta",
|
|
62
|
-
delta: reasoningDelta,
|
|
63
|
-
}));
|
|
64
|
-
}
|
|
65
|
-
if (textDelta) {
|
|
66
|
-
fullResponse += textDelta;
|
|
67
|
-
yield textBuffer.push(textDelta, input.emit);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
72
|
-
finally {
|
|
73
|
-
try {
|
|
74
|
-
if (!_g && !_a && (_b = _h.return)) yield _b.call(_h);
|
|
75
|
-
}
|
|
76
|
-
finally { if (e_1) throw e_1.error; }
|
|
77
|
-
}
|
|
78
|
-
yield textBuffer.flush(input.emit);
|
|
79
|
-
return {
|
|
80
|
-
text: fullResponse,
|
|
81
|
-
};
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { AgentModelFactory } from "./agent/models/AgentModelFactory.js";
|
|
2
|
-
import { AgentModeResolver } from "./agent/models/AgentModeResolver.js";
|
|
3
|
-
import { AgentRuntime } from "./agent/runtime/AgentRuntime.js";
|
|
4
|
-
import { TurnContextBuilder } from "./agent/turn/TurnContextBuilder.js";
|
|
5
|
-
import { TurnLifecycleService } from "./agent/turn/TurnLifecycleService.js";
|
|
6
|
-
import { TurnPromptBuilder } from "./agent/turn/TurnPromptBuilder.js";
|
|
7
|
-
import { TurnStreamConsumer } from "./agent/turn/TurnStreamConsumer.js";
|
|
8
|
-
import type { HandleTurnInput, RunAndPersistAgentResponseInput, RunAndPersistAgentResponseResult } from "./agent/turn/turnTypes.js";
|
|
9
|
-
export type { BaseAgentTurnInput, HandleSpeechTurnInput, HandleTurnInput, RunAndPersistAgentResponseInput, RunAndPersistAgentResponseResult, } from "./agent/turn/turnTypes.js";
|
|
10
|
-
export declare class AgentTurnService {
|
|
11
|
-
private readonly lifecycle;
|
|
12
|
-
private readonly contextBuilder;
|
|
13
|
-
private readonly modeResolver;
|
|
14
|
-
private readonly modelFactory;
|
|
15
|
-
private readonly promptBuilder;
|
|
16
|
-
private readonly runtime;
|
|
17
|
-
private readonly streamConsumer;
|
|
18
|
-
private readonly pendingInterrupts;
|
|
19
|
-
constructor(lifecycle: TurnLifecycleService, contextBuilder: TurnContextBuilder, modeResolver: AgentModeResolver, modelFactory: AgentModelFactory, promptBuilder: TurnPromptBuilder, runtime: AgentRuntime, streamConsumer: TurnStreamConsumer);
|
|
20
|
-
private prepareTurn;
|
|
21
|
-
private runAgentTurn;
|
|
22
|
-
runAndPersistAgentResponse(input: RunAndPersistAgentResponseInput): Promise<RunAndPersistAgentResponseResult>;
|
|
23
|
-
handleTurn(input: HandleTurnInput): Promise<RunAndPersistAgentResponseResult>;
|
|
24
|
-
}
|
package/dist/agentTurnService.js
DELETED
|
@@ -1,269 +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 { randomUUID } from "crypto";
|
|
12
|
-
import { Command } from "@langchain/langgraph";
|
|
13
|
-
import { createSequenceDebugCollector } from "./agent/middleware/sequenceDebug.js";
|
|
14
|
-
import { getErrorMessage, isAbortError } from "./errors.js";
|
|
15
|
-
function getApprovalDecision(input) {
|
|
16
|
-
return "approvalDecision" in input
|
|
17
|
-
&& (input.approvalDecision === "approve" || input.approvalDecision === "reject")
|
|
18
|
-
? input.approvalDecision
|
|
19
|
-
: undefined;
|
|
20
|
-
}
|
|
21
|
-
function getInterruptItems(interrupt) {
|
|
22
|
-
return Array.isArray(interrupt) ? interrupt : [interrupt];
|
|
23
|
-
}
|
|
24
|
-
function getHitlInterrupts(interrupt) {
|
|
25
|
-
return getInterruptItems(interrupt).flatMap((item) => {
|
|
26
|
-
const value = item && typeof item === "object" && "value" in item
|
|
27
|
-
? item.value
|
|
28
|
-
: item;
|
|
29
|
-
const actionRequests = value && typeof value === "object"
|
|
30
|
-
? value.actionRequests
|
|
31
|
-
: undefined;
|
|
32
|
-
const interruptId = item && typeof item === "object"
|
|
33
|
-
? item.id
|
|
34
|
-
: undefined;
|
|
35
|
-
return typeof interruptId === "string" && Array.isArray(actionRequests)
|
|
36
|
-
? [{ id: interruptId, count: actionRequests.length }]
|
|
37
|
-
: [];
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
function buildHitlDecision(decision, prompt) {
|
|
41
|
-
if (decision === "approve") {
|
|
42
|
-
return { type: "approve" };
|
|
43
|
-
}
|
|
44
|
-
return {
|
|
45
|
-
type: "reject",
|
|
46
|
-
message: prompt
|
|
47
|
-
? `User rejected the pending tool execution and sent a new instruction instead: ${prompt}`
|
|
48
|
-
: "User rejected executing this tool",
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
function buildHitlResumeValue(input) {
|
|
52
|
-
return {
|
|
53
|
-
decisions: Array.from({ length: input.count }, () => (buildHitlDecision(input.decision, input.prompt))),
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
function buildLangGraphResume(input) {
|
|
57
|
-
var _a;
|
|
58
|
-
const interrupts = (_a = input.interrupts) !== null && _a !== void 0 ? _a : [];
|
|
59
|
-
if (interrupts.length === 0) {
|
|
60
|
-
throw new Error("No pending approval interrupt found for resume.");
|
|
61
|
-
}
|
|
62
|
-
if (interrupts.length === 1) {
|
|
63
|
-
return buildHitlResumeValue({
|
|
64
|
-
decision: input.decision,
|
|
65
|
-
count: interrupts[0].count,
|
|
66
|
-
prompt: input.prompt,
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
return Object.fromEntries(interrupts.map((interrupt) => [
|
|
70
|
-
interrupt.id,
|
|
71
|
-
buildHitlResumeValue({
|
|
72
|
-
decision: input.decision,
|
|
73
|
-
count: interrupt.count,
|
|
74
|
-
prompt: input.prompt,
|
|
75
|
-
}),
|
|
76
|
-
]));
|
|
77
|
-
}
|
|
78
|
-
export class AgentTurnService {
|
|
79
|
-
constructor(lifecycle, contextBuilder, modeResolver, modelFactory, promptBuilder, runtime, streamConsumer) {
|
|
80
|
-
this.lifecycle = lifecycle;
|
|
81
|
-
this.contextBuilder = contextBuilder;
|
|
82
|
-
this.modeResolver = modeResolver;
|
|
83
|
-
this.modelFactory = modelFactory;
|
|
84
|
-
this.promptBuilder = promptBuilder;
|
|
85
|
-
this.runtime = runtime;
|
|
86
|
-
this.streamConsumer = streamConsumer;
|
|
87
|
-
this.pendingInterrupts = new Map();
|
|
88
|
-
}
|
|
89
|
-
prepareTurn(input) {
|
|
90
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
91
|
-
const sequenceDebugCollector = createSequenceDebugCollector();
|
|
92
|
-
const approvalDecision = getApprovalDecision(input);
|
|
93
|
-
const shouldResume = Boolean(approvalDecision);
|
|
94
|
-
const pendingInterrupts = this.pendingInterrupts.get(input.sessionId);
|
|
95
|
-
if (shouldResume && (!pendingInterrupts || pendingInterrupts.length === 0)) {
|
|
96
|
-
throw new Error(`No pending approval interrupt found for session "${input.sessionId}".`);
|
|
97
|
-
}
|
|
98
|
-
const lifecycleTurn = shouldResume
|
|
99
|
-
? yield this.lifecycle.resume(input)
|
|
100
|
-
: yield this.lifecycle.start(input);
|
|
101
|
-
const context = yield this.contextBuilder.build({
|
|
102
|
-
base: input,
|
|
103
|
-
turnId: lifecycleTurn.turnId,
|
|
104
|
-
});
|
|
105
|
-
return {
|
|
106
|
-
prompt: input.prompt,
|
|
107
|
-
sessionId: input.sessionId,
|
|
108
|
-
turnId: lifecycleTurn.turnId,
|
|
109
|
-
previousUserMessages: lifecycleTurn.previousUserMessages,
|
|
110
|
-
modeName: input.modeName,
|
|
111
|
-
context,
|
|
112
|
-
observability: {
|
|
113
|
-
emit: undefined,
|
|
114
|
-
sequenceDebugSink: sequenceDebugCollector,
|
|
115
|
-
},
|
|
116
|
-
resume: shouldResume
|
|
117
|
-
? {
|
|
118
|
-
decision: approvalDecision,
|
|
119
|
-
interrupts: pendingInterrupts,
|
|
120
|
-
}
|
|
121
|
-
: undefined,
|
|
122
|
-
initialResponse: shouldResume && "initialResponse" in lifecycleTurn
|
|
123
|
-
? lifecycleTurn.initialResponse
|
|
124
|
-
: undefined,
|
|
125
|
-
};
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
runAgentTurn(input) {
|
|
129
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
130
|
-
const selectedMode = this.modeResolver.resolve(input.modeName);
|
|
131
|
-
const [models, messages] = yield Promise.all([
|
|
132
|
-
this.modelFactory.create(selectedMode.completionAdapter),
|
|
133
|
-
this.promptBuilder.build({
|
|
134
|
-
prompt: input.prompt,
|
|
135
|
-
previousUserMessages: input.previousUserMessages,
|
|
136
|
-
adminUser: input.context.adminUser,
|
|
137
|
-
completionAdapter: selectedMode.completionAdapter,
|
|
138
|
-
chatSurface: input.context.chatSurface,
|
|
139
|
-
abortSignal: input.context.abortSignal,
|
|
140
|
-
}),
|
|
141
|
-
]);
|
|
142
|
-
const stream = yield this.runtime.stream({
|
|
143
|
-
models,
|
|
144
|
-
input: input.resume
|
|
145
|
-
? new Command({
|
|
146
|
-
resume: buildLangGraphResume({
|
|
147
|
-
decision: input.resume.decision,
|
|
148
|
-
interrupts: input.resume.interrupts,
|
|
149
|
-
prompt: input.prompt,
|
|
150
|
-
}),
|
|
151
|
-
})
|
|
152
|
-
: { messages },
|
|
153
|
-
context: input.context,
|
|
154
|
-
observability: input.observability,
|
|
155
|
-
});
|
|
156
|
-
let interrupted = false;
|
|
157
|
-
try {
|
|
158
|
-
return yield this.streamConsumer.consume({
|
|
159
|
-
stream: stream,
|
|
160
|
-
abortSignal: input.context.abortSignal,
|
|
161
|
-
emit: input.observability.emit,
|
|
162
|
-
onInterrupt: (interrupt) => __awaiter(this, void 0, void 0, function* () {
|
|
163
|
-
var _a, _b, _c;
|
|
164
|
-
interrupted = true;
|
|
165
|
-
const interrupts = getHitlInterrupts(interrupt);
|
|
166
|
-
const pendingInterrupts = (_a = this.pendingInterrupts.get(input.sessionId)) !== null && _a !== void 0 ? _a : [];
|
|
167
|
-
const mergedInterrupts = new Map(pendingInterrupts.map((pendingInterrupt) => [
|
|
168
|
-
pendingInterrupt.id,
|
|
169
|
-
pendingInterrupt.count,
|
|
170
|
-
]));
|
|
171
|
-
for (const pendingInterrupt of interrupts) {
|
|
172
|
-
mergedInterrupts.set(pendingInterrupt.id, pendingInterrupt.count);
|
|
173
|
-
}
|
|
174
|
-
this.pendingInterrupts.set(input.sessionId, [...mergedInterrupts.entries()].map(([id, count]) => ({ id, count })));
|
|
175
|
-
yield ((_c = (_b = input.observability).emit) === null || _c === void 0 ? void 0 : _c.call(_b, {
|
|
176
|
-
type: "interrupt",
|
|
177
|
-
sessionId: input.sessionId,
|
|
178
|
-
interrupt,
|
|
179
|
-
}));
|
|
180
|
-
}),
|
|
181
|
-
});
|
|
182
|
-
}
|
|
183
|
-
finally {
|
|
184
|
-
if (!interrupted) {
|
|
185
|
-
this.pendingInterrupts.delete(input.sessionId);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
runAndPersistAgentResponse(input) {
|
|
191
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
192
|
-
var _a, _b;
|
|
193
|
-
const preparedTurn = yield this.prepareTurn(input);
|
|
194
|
-
preparedTurn.observability.emit = input.emit;
|
|
195
|
-
let fullResponse = (_a = preparedTurn.initialResponse) !== null && _a !== void 0 ? _a : "";
|
|
196
|
-
let aborted = false;
|
|
197
|
-
let failed = false;
|
|
198
|
-
try {
|
|
199
|
-
const agentResponse = yield this.runAgentTurn(preparedTurn);
|
|
200
|
-
fullResponse += agentResponse.text;
|
|
201
|
-
}
|
|
202
|
-
catch (error) {
|
|
203
|
-
if (((_b = input.abortSignal) === null || _b === void 0 ? void 0 : _b.aborted) || isAbortError(error)) {
|
|
204
|
-
aborted = true;
|
|
205
|
-
logger.info(input.abortLogMessage);
|
|
206
|
-
}
|
|
207
|
-
else {
|
|
208
|
-
failed = true;
|
|
209
|
-
fullResponse = getErrorMessage(error);
|
|
210
|
-
logger.error(`${input.failureLogMessage}:\n${fullResponse}`);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
preparedTurn.observability.sequenceDebugSink.flush();
|
|
214
|
-
yield this.lifecycle.finish({
|
|
215
|
-
turnId: preparedTurn.turnId,
|
|
216
|
-
responseText: fullResponse,
|
|
217
|
-
debugHistory: preparedTurn.observability.sequenceDebugSink.getHistory(),
|
|
218
|
-
});
|
|
219
|
-
return {
|
|
220
|
-
text: fullResponse,
|
|
221
|
-
turnId: preparedTurn.turnId,
|
|
222
|
-
aborted,
|
|
223
|
-
failed,
|
|
224
|
-
};
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
handleTurn(input) {
|
|
228
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
229
|
-
var _a, _b;
|
|
230
|
-
yield input.emit({
|
|
231
|
-
type: "turn-started",
|
|
232
|
-
messageId: randomUUID(),
|
|
233
|
-
});
|
|
234
|
-
const agentResponse = yield this.runAndPersistAgentResponse({
|
|
235
|
-
prompt: input.prompt,
|
|
236
|
-
sessionId: input.sessionId,
|
|
237
|
-
modeName: input.modeName,
|
|
238
|
-
userTimeZone: input.userTimeZone,
|
|
239
|
-
currentPage: input.currentPage,
|
|
240
|
-
chatSurface: input.chatSurface,
|
|
241
|
-
adminPublicOrigin: input.adminPublicOrigin,
|
|
242
|
-
approvalDecision: input.approvalDecision,
|
|
243
|
-
abortSignal: input.abortSignal,
|
|
244
|
-
adminUser: input.adminUser,
|
|
245
|
-
emit: input.emit,
|
|
246
|
-
failureLogMessage: (_a = input.failureLogMessage) !== null && _a !== void 0 ? _a : "Agent response failed",
|
|
247
|
-
abortLogMessage: (_b = input.abortLogMessage) !== null && _b !== void 0 ? _b : "Agent response aborted",
|
|
248
|
-
});
|
|
249
|
-
if (agentResponse.failed) {
|
|
250
|
-
yield input.emit({
|
|
251
|
-
type: "error",
|
|
252
|
-
error: agentResponse.text,
|
|
253
|
-
});
|
|
254
|
-
}
|
|
255
|
-
else if (!agentResponse.aborted) {
|
|
256
|
-
yield input.emit({
|
|
257
|
-
type: "response",
|
|
258
|
-
text: agentResponse.text,
|
|
259
|
-
sessionId: input.sessionId,
|
|
260
|
-
turnId: agentResponse.turnId,
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
yield input.emit({
|
|
264
|
-
type: "finish",
|
|
265
|
-
});
|
|
266
|
-
return agentResponse;
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { AgentEventEmitter } from "../../agentEvents.js";
|
|
2
|
-
type AgentEventStreamResponse = {
|
|
3
|
-
writeHead: (statusCode: number, headers: Record<string, string>) => void;
|
|
4
|
-
write: (chunk: string) => unknown;
|
|
5
|
-
end: () => unknown;
|
|
6
|
-
writableEnded: boolean;
|
|
7
|
-
destroyed: boolean;
|
|
8
|
-
};
|
|
9
|
-
type AgentEventStreamOptions = {
|
|
10
|
-
vercelAiUiMessageStream?: boolean;
|
|
11
|
-
closeActiveBlockOnToolStart?: boolean;
|
|
12
|
-
};
|
|
13
|
-
export declare function createSseEventEmitter(res: AgentEventStreamResponse, options?: AgentEventStreamOptions): AgentEventEmitter;
|
|
14
|
-
export {};
|
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
IAdminForthEndpointHandlerInput,
|
|
3
|
-
IHttpServer,
|
|
4
|
-
} from "adminforth";
|
|
5
|
-
import type { ChatSurfaceEndpointsContext } from "./context.js";
|
|
6
|
-
|
|
7
|
-
function getHeaderValue(headers: Record<string, unknown>, name: string) {
|
|
8
|
-
const value = headers[name] ?? headers[name.toLowerCase()];
|
|
9
|
-
|
|
10
|
-
if (Array.isArray(value)) {
|
|
11
|
-
return value[0];
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
return typeof value === "string" ? value : undefined;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function getRequestOrigin(input: IAdminForthEndpointHandlerInput) {
|
|
18
|
-
const forwardedProto = getHeaderValue(input.headers, "x-forwarded-proto")?.split(",")[0]?.trim();
|
|
19
|
-
const forwardedHost = getHeaderValue(input.headers, "x-forwarded-host")?.split(",")[0]?.trim();
|
|
20
|
-
const host = forwardedHost ?? getHeaderValue(input.headers, "host");
|
|
21
|
-
|
|
22
|
-
if (!host) {
|
|
23
|
-
return undefined;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const proto = forwardedProto
|
|
27
|
-
?? (host.startsWith("localhost") || host.startsWith("127.0.0.1") ? "http" : "https");
|
|
28
|
-
|
|
29
|
-
return `${proto}://${host}`;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export function setupChatSurfaceEndpoints(ctx: ChatSurfaceEndpointsContext, server: IHttpServer) {
|
|
33
|
-
for (const adapter of ctx.options.chatSurfaceAdapters ?? []) {
|
|
34
|
-
server.endpoint({
|
|
35
|
-
method: "POST",
|
|
36
|
-
noAuth: true,
|
|
37
|
-
path: `/agent/surface/${adapter.name}/webhook`,
|
|
38
|
-
handler: async (endpointInput: IAdminForthEndpointHandlerInput) => {
|
|
39
|
-
const surfaceContext = {
|
|
40
|
-
body: endpointInput.body,
|
|
41
|
-
headers: endpointInput.headers,
|
|
42
|
-
abortSignal: endpointInput.abortSignal,
|
|
43
|
-
rawRequest: endpointInput._raw_express_req,
|
|
44
|
-
rawResponse: endpointInput._raw_express_res,
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const incoming = await adapter.parseIncomingMessage(surfaceContext);
|
|
48
|
-
if (!incoming) return { ok: true };
|
|
49
|
-
incoming.metadata = {
|
|
50
|
-
...incoming.metadata,
|
|
51
|
-
adminPublicOrigin: getRequestOrigin(endpointInput),
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
const sink = await adapter.createEventSink(surfaceContext, incoming);
|
|
55
|
-
|
|
56
|
-
try {
|
|
57
|
-
await ctx.handleChatSurfaceMessage(adapter, incoming, sink);
|
|
58
|
-
} finally {
|
|
59
|
-
await sink.close?.();
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return { ok: true };
|
|
63
|
-
},
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
}
|
package/endpoints/context.ts
DELETED
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ChatSurfaceAdapter,
|
|
3
|
-
ChatSurfaceEventSink,
|
|
4
|
-
ChatSurfaceIncomingMessage,
|
|
5
|
-
IAdminForth,
|
|
6
|
-
} from "adminforth";
|
|
7
|
-
import type {
|
|
8
|
-
HandleSpeechTurnInput,
|
|
9
|
-
HandleTurnInput,
|
|
10
|
-
RunAndPersistAgentResponseInput,
|
|
11
|
-
RunAndPersistAgentResponseResult,
|
|
12
|
-
} from "../agentTurnService.js";
|
|
13
|
-
import type { PluginOptions } from "../types.js";
|
|
14
|
-
|
|
15
|
-
export type SessionTurn = {
|
|
16
|
-
prompt: string;
|
|
17
|
-
response: string;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export type AgentEndpointsContext = {
|
|
21
|
-
adminforth: IAdminForth;
|
|
22
|
-
options: PluginOptions;
|
|
23
|
-
handleTurn(input: HandleTurnInput): Promise<RunAndPersistAgentResponseResult>;
|
|
24
|
-
handleSpeechTurn(input: HandleSpeechTurnInput): Promise<RunAndPersistAgentResponseResult | null>;
|
|
25
|
-
runAndPersistAgentResponse(input: RunAndPersistAgentResponseInput): Promise<RunAndPersistAgentResponseResult>;
|
|
26
|
-
getSessionTurns(sessionId: string): Promise<SessionTurn[]>;
|
|
27
|
-
createNewTurn(sessionId: string, prompt: string, response?: string): Promise<string>;
|
|
28
|
-
createSystemTurn(sessionId: string, systemMessage: string): Promise<string>;
|
|
29
|
-
handleChatSurfaceMessage(
|
|
30
|
-
adapter: ChatSurfaceAdapter,
|
|
31
|
-
incoming: ChatSurfaceIncomingMessage,
|
|
32
|
-
sink: ChatSurfaceEventSink,
|
|
33
|
-
): Promise<void>;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
export type CoreEndpointsContext = Pick<
|
|
37
|
-
AgentEndpointsContext,
|
|
38
|
-
"options" | "handleTurn" | "handleSpeechTurn"
|
|
39
|
-
>;
|
|
40
|
-
|
|
41
|
-
export type SessionEndpointsContext = Pick<
|
|
42
|
-
AgentEndpointsContext,
|
|
43
|
-
"adminforth" | "options" | "getSessionTurns" | "createNewTurn"
|
|
44
|
-
| "createSystemTurn"
|
|
45
|
-
>;
|
|
46
|
-
|
|
47
|
-
export type ChatSurfaceEndpointsContext = Pick<
|
|
48
|
-
AgentEndpointsContext,
|
|
49
|
-
| "adminforth"
|
|
50
|
-
| "options"
|
|
51
|
-
| "handleChatSurfaceMessage"
|
|
52
|
-
>;
|