@adminforth/agent 1.43.28 → 1.44.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/agent/checkpointer.ts +2 -1
- package/agent/systemPrompt.ts +2 -1
- package/agentEvents.ts +61 -0
- package/agentResponseEvents.ts +1 -206
- package/apiBasedTools.ts +7 -3
- package/dist/agent/checkpointer.d.ts +29 -0
- package/dist/agent/languageDetect.d.ts +10 -0
- package/dist/agent/middleware/apiBasedTools.d.ts +3 -0
- package/dist/agent/middleware/openAiResponsesContinuation.d.ts +1 -0
- package/dist/agent/middleware/sequenceDebug.d.ts +46 -0
- package/dist/agent/simpleAgent.d.ts +61 -0
- package/dist/agent/skills/registry.d.ts +13 -0
- package/dist/agent/systemPrompt.d.ts +11 -0
- package/dist/agent/systemPrompt.js +3 -1
- package/dist/agent/toolCallEvents.d.ts +27 -0
- package/dist/agent/tools/apiTool.d.ts +6 -0
- package/dist/agent/tools/fetchSkill.d.ts +8 -0
- package/dist/agent/tools/fetchToolSchema.d.ts +9 -0
- package/dist/agent/tools/getUserLocation.d.ts +8 -0
- package/dist/agent/tools/index.d.ts +4 -0
- package/dist/agentEvents.d.ts +52 -0
- package/dist/agentEvents.js +1 -0
- package/dist/agentResponseEvents.d.ts +1 -0
- package/dist/agentResponseEvents.js +1 -144
- package/dist/apiBasedTools.d.ts +29 -0
- package/dist/apiBasedTools.js +5 -3
- package/dist/index.d.ts +58 -0
- package/dist/index.js +251 -59
- package/dist/sanitizeSpeechText.d.ts +1 -0
- package/dist/surfaces/web-sse/createSseEventEmitter.d.ts +14 -0
- package/dist/surfaces/web-sse/createSseEventEmitter.js +196 -0
- package/dist/types.d.ts +94 -0
- package/index.ts +279 -59
- package/package.json +2 -2
- package/surfaces/web-sse/createSseEventEmitter.ts +261 -0
- package/tsconfig.json +2 -1
- package/types.ts +6 -0
|
@@ -1,144 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export function createAgentEventStream(res, options = {}) {
|
|
3
|
-
let isStreamClosed = false;
|
|
4
|
-
let activeBlock = null;
|
|
5
|
-
res.writeHead(200, Object.assign({ "Content-Type": "text/event-stream", "Cache-Control": "no-cache", "Connection": "keep-alive" }, (options.vercelAiUiMessageStream
|
|
6
|
-
? { "x-vercel-ai-ui-message-stream": "v1" }
|
|
7
|
-
: {})));
|
|
8
|
-
const stream = {
|
|
9
|
-
send(obj) {
|
|
10
|
-
if (isStreamClosed || res.writableEnded || res.destroyed) {
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
res.write(`data: ${JSON.stringify(obj)}\n\n`);
|
|
14
|
-
},
|
|
15
|
-
endActiveBlock() {
|
|
16
|
-
if (!activeBlock) {
|
|
17
|
-
return;
|
|
18
|
-
}
|
|
19
|
-
stream.send({
|
|
20
|
-
type: `${activeBlock.type}-end`,
|
|
21
|
-
id: activeBlock.id,
|
|
22
|
-
});
|
|
23
|
-
activeBlock = null;
|
|
24
|
-
},
|
|
25
|
-
startBlock(type) {
|
|
26
|
-
if ((activeBlock === null || activeBlock === void 0 ? void 0 : activeBlock.type) === type) {
|
|
27
|
-
return activeBlock.id;
|
|
28
|
-
}
|
|
29
|
-
stream.endActiveBlock();
|
|
30
|
-
const id = randomUUID();
|
|
31
|
-
activeBlock = { type, id };
|
|
32
|
-
stream.send({
|
|
33
|
-
type: `${type}-start`,
|
|
34
|
-
id,
|
|
35
|
-
});
|
|
36
|
-
return id;
|
|
37
|
-
},
|
|
38
|
-
start(messageId) {
|
|
39
|
-
stream.send({
|
|
40
|
-
type: "start",
|
|
41
|
-
messageId,
|
|
42
|
-
});
|
|
43
|
-
},
|
|
44
|
-
textDelta(delta) {
|
|
45
|
-
const textId = stream.startBlock("text");
|
|
46
|
-
stream.send({
|
|
47
|
-
type: "text-delta",
|
|
48
|
-
id: textId,
|
|
49
|
-
delta,
|
|
50
|
-
});
|
|
51
|
-
},
|
|
52
|
-
reasoningDelta(delta) {
|
|
53
|
-
const reasoningId = stream.startBlock("reasoning");
|
|
54
|
-
stream.send({
|
|
55
|
-
type: "reasoning-delta",
|
|
56
|
-
id: reasoningId,
|
|
57
|
-
delta,
|
|
58
|
-
});
|
|
59
|
-
},
|
|
60
|
-
toolCall(event) {
|
|
61
|
-
if (options.closeActiveBlockOnToolStart && event.phase === "start") {
|
|
62
|
-
stream.endActiveBlock();
|
|
63
|
-
}
|
|
64
|
-
stream.send({
|
|
65
|
-
type: "data-tool-call",
|
|
66
|
-
data: event,
|
|
67
|
-
});
|
|
68
|
-
},
|
|
69
|
-
transcript(text, language) {
|
|
70
|
-
stream.send({
|
|
71
|
-
type: "transcript",
|
|
72
|
-
data: {
|
|
73
|
-
text,
|
|
74
|
-
language,
|
|
75
|
-
},
|
|
76
|
-
});
|
|
77
|
-
},
|
|
78
|
-
response(text, sessionId, turnId) {
|
|
79
|
-
stream.send({
|
|
80
|
-
type: "response",
|
|
81
|
-
data: {
|
|
82
|
-
text,
|
|
83
|
-
sessionId,
|
|
84
|
-
turnId,
|
|
85
|
-
},
|
|
86
|
-
});
|
|
87
|
-
},
|
|
88
|
-
speechResponse(transcript, response, sessionId, turnId) {
|
|
89
|
-
stream.send({
|
|
90
|
-
type: "speech-response",
|
|
91
|
-
data: {
|
|
92
|
-
transcript,
|
|
93
|
-
response,
|
|
94
|
-
sessionId,
|
|
95
|
-
turnId,
|
|
96
|
-
},
|
|
97
|
-
});
|
|
98
|
-
},
|
|
99
|
-
audioStart(mimeType, format, sampleRate, channelCount, bitsPerSample) {
|
|
100
|
-
stream.send({
|
|
101
|
-
type: "audio-start",
|
|
102
|
-
data: {
|
|
103
|
-
mimeType,
|
|
104
|
-
format,
|
|
105
|
-
sampleRate,
|
|
106
|
-
channelCount,
|
|
107
|
-
bitsPerSample,
|
|
108
|
-
},
|
|
109
|
-
});
|
|
110
|
-
},
|
|
111
|
-
audioDelta(value) {
|
|
112
|
-
stream.send({
|
|
113
|
-
type: "audio-delta",
|
|
114
|
-
data: {
|
|
115
|
-
base64: Buffer.from(value).toString("base64"),
|
|
116
|
-
},
|
|
117
|
-
});
|
|
118
|
-
},
|
|
119
|
-
audioDone() {
|
|
120
|
-
stream.send({
|
|
121
|
-
type: "audio-done",
|
|
122
|
-
});
|
|
123
|
-
},
|
|
124
|
-
error(error) {
|
|
125
|
-
stream.send({
|
|
126
|
-
type: "error",
|
|
127
|
-
error,
|
|
128
|
-
});
|
|
129
|
-
},
|
|
130
|
-
end() {
|
|
131
|
-
if (isStreamClosed || res.writableEnded || res.destroyed) {
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
stream.endActiveBlock();
|
|
135
|
-
stream.send({
|
|
136
|
-
type: "finish",
|
|
137
|
-
});
|
|
138
|
-
res.write("data: [DONE]\n\n");
|
|
139
|
-
isStreamClosed = true;
|
|
140
|
-
res.end();
|
|
141
|
-
},
|
|
142
|
-
};
|
|
143
|
-
return stream;
|
|
144
|
-
}
|
|
1
|
+
export { createSseEventEmitter } from "./surfaces/web-sse/createSseEventEmitter.js";
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type AdminUser, type IAdminForth } from 'adminforth';
|
|
2
|
+
export type ApiBasedToolCallParams = {
|
|
3
|
+
adminUser?: AdminUser;
|
|
4
|
+
adminuser?: AdminUser;
|
|
5
|
+
abortSignal?: AbortSignal;
|
|
6
|
+
inputs?: Record<string, unknown>;
|
|
7
|
+
userTimeZone?: string;
|
|
8
|
+
acceptLanguage?: string;
|
|
9
|
+
};
|
|
10
|
+
export type ApiBasedTool = {
|
|
11
|
+
description?: string;
|
|
12
|
+
input_schema?: unknown;
|
|
13
|
+
output_schema?: unknown;
|
|
14
|
+
call: (params?: ApiBasedToolCallParams) => Promise<string>;
|
|
15
|
+
};
|
|
16
|
+
export declare function formatApiBasedToolCall(params: {
|
|
17
|
+
adminforth: IAdminForth;
|
|
18
|
+
adminUser?: AdminUser;
|
|
19
|
+
inputs?: Record<string, unknown>;
|
|
20
|
+
toolName: string;
|
|
21
|
+
userTimeZone?: string;
|
|
22
|
+
}): Promise<string | undefined>;
|
|
23
|
+
export declare function prepareApiBasedTools(adminforth: IAdminForth, hiddenResourceIds?: Iterable<string>): Record<string, ApiBasedTool>;
|
|
24
|
+
export declare function serializeApiBasedTool(tool: ApiBasedTool | undefined): {
|
|
25
|
+
description: string | undefined;
|
|
26
|
+
input_schema: unknown;
|
|
27
|
+
output_schema: unknown;
|
|
28
|
+
call: string;
|
|
29
|
+
} | null;
|
package/dist/apiBasedTools.js
CHANGED
|
@@ -341,7 +341,7 @@ function validationErrorResponse(error, details) {
|
|
|
341
341
|
}
|
|
342
342
|
function callOpenApiSchema(params) {
|
|
343
343
|
return __awaiter(this, void 0, void 0, function* () {
|
|
344
|
-
const { adminforth, adminUser, abortSignal, inputs, schema, toolName, userTimeZone } = params;
|
|
344
|
+
const { adminforth, adminUser, abortSignal, inputs, schema, toolName, userTimeZone, acceptLanguage } = params;
|
|
345
345
|
const method = schema.method.toUpperCase();
|
|
346
346
|
const normalizedInputs = normalizeDateTimeInputsToUtc((inputs !== null && inputs !== void 0 ? inputs : {}), adminforth, userTimeZone);
|
|
347
347
|
const hasRequestBody = !METHODS_WITHOUT_REQUEST_BODY.has(method);
|
|
@@ -353,7 +353,8 @@ function callOpenApiSchema(params) {
|
|
|
353
353
|
}
|
|
354
354
|
const response = createDirectToolResponse();
|
|
355
355
|
logger.info(`Calling OpenAPI tool "${toolName}" with direct handler`);
|
|
356
|
-
const
|
|
356
|
+
const lang = acceptLanguage !== null && acceptLanguage !== void 0 ? acceptLanguage : "en";
|
|
357
|
+
const tr = (msg, category, trParams, pluralizationNumber) => adminforth.tr(msg, category, lang, trParams, pluralizationNumber);
|
|
357
358
|
const output = yield schema.handler({
|
|
358
359
|
body,
|
|
359
360
|
query,
|
|
@@ -410,7 +411,7 @@ export function prepareApiBasedTools(adminforth, hiddenResourceIds = []) {
|
|
|
410
411
|
description: schema.description,
|
|
411
412
|
input_schema: schema.request_schema,
|
|
412
413
|
output_schema: schema.response_schema,
|
|
413
|
-
call: (...args_1) => __awaiter(this, [...args_1], void 0, function* ({ adminUser, adminuser, abortSignal, inputs, userTimeZone } = {}) {
|
|
414
|
+
call: (...args_1) => __awaiter(this, [...args_1], void 0, function* ({ adminUser, adminuser, abortSignal, inputs, userTimeZone, acceptLanguage } = {}) {
|
|
414
415
|
if (isHiddenResourceCall(hiddenResourceIdSet, inputs)) {
|
|
415
416
|
return YAML.stringify({
|
|
416
417
|
error: 'RESOURCE_NOT_AVAILABLE',
|
|
@@ -425,6 +426,7 @@ export function prepareApiBasedTools(adminforth, hiddenResourceIds = []) {
|
|
|
425
426
|
toolName,
|
|
426
427
|
inputs,
|
|
427
428
|
userTimeZone,
|
|
429
|
+
acceptLanguage,
|
|
428
430
|
});
|
|
429
431
|
const processedOutput = yield applyToolOverride({
|
|
430
432
|
adminforth,
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { AdminUser, AdminForthResource, IAdminForth, IHttpServer } from "adminforth";
|
|
2
|
+
import { AdminForthPlugin } from "adminforth";
|
|
3
|
+
import type { PluginOptions } from './types.js';
|
|
4
|
+
import { createSequenceDebugCollector } from "./agent/middleware/sequenceDebug.js";
|
|
5
|
+
import { type PreviousUserMessage } from "./agent/languageDetect.js";
|
|
6
|
+
import type { AgentEventEmitter } from "./agentEvents.js";
|
|
7
|
+
import type { CurrentPageContext } from "./agent/tools/getUserLocation.js";
|
|
8
|
+
export type { AgentEvent, AgentEventEmitter } from "./agentEvents.js";
|
|
9
|
+
type AgentTurnRunInput = {
|
|
10
|
+
prompt: string;
|
|
11
|
+
sessionId: string;
|
|
12
|
+
turnId: string;
|
|
13
|
+
previousUserMessages: PreviousUserMessage[];
|
|
14
|
+
modeName?: string | null;
|
|
15
|
+
userTimeZone: string;
|
|
16
|
+
currentPage?: CurrentPageContext;
|
|
17
|
+
abortSignal?: AbortSignal;
|
|
18
|
+
adminUser: AdminUser;
|
|
19
|
+
sequenceDebugCollector: ReturnType<typeof createSequenceDebugCollector>;
|
|
20
|
+
emit?: AgentEventEmitter;
|
|
21
|
+
};
|
|
22
|
+
type RunAndPersistAgentResponseInput = Omit<AgentTurnRunInput, "turnId" | "sequenceDebugCollector" | "previousUserMessages"> & {
|
|
23
|
+
failureLogMessage: string;
|
|
24
|
+
abortLogMessage: string;
|
|
25
|
+
};
|
|
26
|
+
type HandleTurnInput = Omit<RunAndPersistAgentResponseInput, "failureLogMessage" | "abortLogMessage"> & {
|
|
27
|
+
emit: AgentEventEmitter;
|
|
28
|
+
failureLogMessage?: string;
|
|
29
|
+
abortLogMessage?: string;
|
|
30
|
+
};
|
|
31
|
+
export default class AdminForthAgentPlugin extends AdminForthPlugin {
|
|
32
|
+
options: PluginOptions;
|
|
33
|
+
agentSystemPromptPromise: Promise<string>;
|
|
34
|
+
private checkpointer;
|
|
35
|
+
private parseBody;
|
|
36
|
+
private createNewTurn;
|
|
37
|
+
private getSessionTurns;
|
|
38
|
+
private getPreviousUserMessages;
|
|
39
|
+
private getChatSurfaceSessionId;
|
|
40
|
+
private getOrCreateChatSurfaceSession;
|
|
41
|
+
private getCheckpointer;
|
|
42
|
+
private getInternalAgentResourceIds;
|
|
43
|
+
constructor(options: PluginOptions);
|
|
44
|
+
modifyResourceConfig(adminforth: IAdminForth, resourceConfig: AdminForthResource): Promise<void>;
|
|
45
|
+
validateConfigAfterDiscover(adminforth: IAdminForth, resourceConfig: AdminForthResource): void;
|
|
46
|
+
instanceUniqueRepresentation(pluginOptions: any): string;
|
|
47
|
+
private runAgentTurn;
|
|
48
|
+
private runAndPersistAgentResponse;
|
|
49
|
+
handleTurn(input: HandleTurnInput): Promise<{
|
|
50
|
+
text: string;
|
|
51
|
+
turnId: any;
|
|
52
|
+
aborted: boolean;
|
|
53
|
+
failed: boolean;
|
|
54
|
+
}>;
|
|
55
|
+
private createChatSurfaceEventEmitter;
|
|
56
|
+
private handleChatSurfaceMessage;
|
|
57
|
+
setupEndpoints(server: IHttpServer): void;
|
|
58
|
+
}
|