@adminforth/agent 1.45.0 → 1.46.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/agentTurnService.ts +526 -0
- package/build.log +1 -1
- package/chatSurfaceService.ts +370 -0
- package/dist/agentTurnService.d.ts +70 -0
- package/dist/agentTurnService.js +453 -0
- package/dist/chatSurfaceService.d.ts +32 -0
- package/dist/chatSurfaceService.js +265 -0
- package/dist/endpoints/chatSurfaces.d.ts +3 -0
- package/dist/endpoints/chatSurfaces.js +91 -0
- package/dist/endpoints/context.d.ts +30 -0
- package/dist/endpoints/context.js +1 -0
- package/dist/endpoints/core.d.ts +3 -0
- package/dist/endpoints/core.js +106 -0
- package/dist/endpoints/sessions.d.ts +3 -0
- package/dist/endpoints/sessions.js +177 -0
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +9 -0
- package/dist/index.d.ts +4 -47
- package/dist/index.js +37 -917
- package/dist/sessionStore.d.ts +19 -0
- package/dist/sessionStore.js +83 -0
- package/endpoints/chatSurfaces.ts +93 -0
- package/endpoints/context.ts +66 -0
- package/endpoints/core.ts +113 -0
- package/endpoints/sessions.ts +183 -0
- package/errors.ts +10 -0
- package/index.ts +48 -1053
- package/package.json +1 -1
- package/sessionStore.ts +94 -0
- package/agentResponseEvents.ts +0 -1
- package/dist/agentResponseEvents.d.ts +0 -1
- package/dist/agentResponseEvents.js +0 -1
package/package.json
CHANGED
package/sessionStore.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import type { AdminUser, IAdminForth } from "adminforth";
|
|
2
|
+
import { Filters, Sorts } from "adminforth";
|
|
3
|
+
import { randomUUID } from "crypto";
|
|
4
|
+
import type { ChatSurfaceIncomingMessage } from "adminforth";
|
|
5
|
+
import type { PreviousUserMessage } from "./agent/languageDetect.js";
|
|
6
|
+
import type { PluginOptions } from "./types.js";
|
|
7
|
+
|
|
8
|
+
export const AGENT_SYSTEM_TURN_PROMPT = "__adminforth_system_message__";
|
|
9
|
+
|
|
10
|
+
export class AgentSessionStore {
|
|
11
|
+
constructor(
|
|
12
|
+
private getAdminforth: () => IAdminForth,
|
|
13
|
+
private options: PluginOptions,
|
|
14
|
+
) {}
|
|
15
|
+
|
|
16
|
+
async createNewTurn(sessionId: string, prompt: string, response?: string) {
|
|
17
|
+
const turnId = randomUUID();
|
|
18
|
+
const turnRecord = {
|
|
19
|
+
[this.options.turnResource.idField]: turnId,
|
|
20
|
+
[this.options.turnResource.sessionIdField]: sessionId,
|
|
21
|
+
[this.options.turnResource.promptField]: prompt,
|
|
22
|
+
[this.options.turnResource.responseField]: response ?? "not_finished",
|
|
23
|
+
};
|
|
24
|
+
const newTurn = await this.getAdminforth().resource(this.options.turnResource.resourceId).create(turnRecord);
|
|
25
|
+
return newTurn.createdRecord[this.options.turnResource.idField];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async createSystemTurn(sessionId: string, systemMessage: string) {
|
|
29
|
+
const turnId = randomUUID();
|
|
30
|
+
const turnRecord = {
|
|
31
|
+
[this.options.turnResource.idField]: turnId,
|
|
32
|
+
[this.options.turnResource.sessionIdField]: sessionId,
|
|
33
|
+
[this.options.turnResource.promptField]: AGENT_SYSTEM_TURN_PROMPT,
|
|
34
|
+
[this.options.turnResource.responseField]: systemMessage,
|
|
35
|
+
};
|
|
36
|
+
const newTurn = await this.getAdminforth().resource(this.options.turnResource.resourceId).create(turnRecord);
|
|
37
|
+
return newTurn.createdRecord[this.options.turnResource.idField];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async getSessionTurns(sessionId: string) {
|
|
41
|
+
const turns = await this.getAdminforth().resource(this.options.turnResource.resourceId).list(
|
|
42
|
+
[Filters.EQ(this.options.turnResource.sessionIdField, sessionId)],
|
|
43
|
+
undefined,
|
|
44
|
+
undefined,
|
|
45
|
+
[Sorts.ASC(this.options.turnResource.createdAtField)]
|
|
46
|
+
);
|
|
47
|
+
return turns.map(turn => ({
|
|
48
|
+
prompt: turn[this.options.turnResource.promptField],
|
|
49
|
+
response: turn[this.options.turnResource.responseField],
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async getPreviousUserMessages(sessionId: string) {
|
|
54
|
+
const turns = await this.getAdminforth().resource(this.options.turnResource.resourceId).list(
|
|
55
|
+
[Filters.EQ(this.options.turnResource.sessionIdField, sessionId)],
|
|
56
|
+
2,
|
|
57
|
+
undefined,
|
|
58
|
+
[Sorts.DESC(this.options.turnResource.createdAtField)]
|
|
59
|
+
);
|
|
60
|
+
return turns
|
|
61
|
+
.reverse()
|
|
62
|
+
.filter((turn) => turn[this.options.turnResource.promptField] !== AGENT_SYSTEM_TURN_PROMPT)
|
|
63
|
+
.map((turn): PreviousUserMessage => ({
|
|
64
|
+
text: turn[this.options.turnResource.promptField],
|
|
65
|
+
}));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
getChatSurfaceSessionId(incoming: ChatSurfaceIncomingMessage) {
|
|
69
|
+
return `${incoming.surface}:${incoming.externalConversationId}`;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async getOrCreateChatSurfaceSession(
|
|
73
|
+
incoming: ChatSurfaceIncomingMessage,
|
|
74
|
+
adminUser: AdminUser,
|
|
75
|
+
) {
|
|
76
|
+
const sessionId = this.getChatSurfaceSessionId(incoming);
|
|
77
|
+
const sessionResource = this.getAdminforth().resource(this.options.sessionResource.resourceId);
|
|
78
|
+
const session = await sessionResource.get(
|
|
79
|
+
[Filters.EQ(this.options.sessionResource.idField, sessionId)]
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
if (session) {
|
|
83
|
+
return sessionId;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
await sessionResource.create({
|
|
87
|
+
[this.options.sessionResource.idField]: sessionId,
|
|
88
|
+
[this.options.sessionResource.titleField]: incoming.prompt.slice(0, 40) || "New Session",
|
|
89
|
+
[this.options.sessionResource.askerIdField]: adminUser.pk,
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
return sessionId;
|
|
93
|
+
}
|
|
94
|
+
}
|
package/agentResponseEvents.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { createSseEventEmitter } from "./surfaces/web-sse/createSseEventEmitter.js";
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { createSseEventEmitter } from "./surfaces/web-sse/createSseEventEmitter.js";
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { createSseEventEmitter } from "./surfaces/web-sse/createSseEventEmitter.js";
|