Skip to content

Commit 6a5f6a5

Browse files
committed
fix: add createSystemTurn method to handle system messages in session management
1 parent 23815ea commit 6a5f6a5

4 files changed

Lines changed: 28 additions & 1 deletion

File tree

endpoints/context.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export type AgentEndpointsContext = {
3333
runAndPersistAgentResponse(input: RunAndPersistAgentResponseInput): Promise<RunAndPersistAgentResponseResult>;
3434
getSessionTurns(sessionId: string): Promise<SessionTurn[]>;
3535
createNewTurn(sessionId: string, prompt: string, response?: string): Promise<string>;
36+
createSystemTurn(sessionId: string, systemMessage: string): Promise<string>;
3637
getChatSurfaceConnectActionAdapters(): ChatSurfaceAdapterWithConnectAction[];
3738
createChatSurfaceLinkToken(surface: string, adminUser: AdminUser): string;
3839
handleChatSurfaceMessage(
@@ -50,6 +51,7 @@ export type CoreEndpointsContext = Pick<
5051
export type SessionEndpointsContext = Pick<
5152
AgentEndpointsContext,
5253
"adminforth" | "options" | "parseBody" | "getSessionTurns" | "createNewTurn"
54+
| "createSystemTurn"
5355
>;
5456

5557
export type ChatSurfaceEndpointsContext = Pick<

endpoints/sessions.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { IHttpServer } from "adminforth";
22
import { Filters, Sorts } from "adminforth";
33
import { randomUUID } from "crypto";
44
import { z } from "zod";
5+
import { AGENT_SYSTEM_TURN_PROMPT } from "../sessionStore.js";
56
import type { SessionEndpointsContext } from "./context.js";
67

78
const addSystemMessageBodySchema = z.object({
@@ -71,7 +72,14 @@ export function setupSessionEndpoints(ctx: SessionEndpointsContext, server: IHtt
7172
title: session[ctx.options.sessionResource.titleField],
7273
timestamp: session[ctx.options.sessionResource.createdAtField],
7374
messages: turns.flatMap(turn => {
74-
const messages: Array<{ text: string; role: 'user' | 'assistant' }> = [];
75+
const messages: Array<{ text: string; role: 'user' | 'assistant' | 'system' }> = [];
76+
if (turn.prompt === AGENT_SYSTEM_TURN_PROMPT) {
77+
messages.push({
78+
text: turn.response,
79+
role: 'system',
80+
});
81+
return messages;
82+
}
7583
if (turn.prompt) {
7684
messages.push({
7785
text: turn.prompt,
@@ -166,6 +174,7 @@ export function setupSessionEndpoints(ctx: SessionEndpointsContext, server: IHtt
166174
error: 'Unauthorized'
167175
};
168176
}
177+
await ctx.createSystemTurn(data.sessionId, data.systemMessage);
169178
return {
170179
ok: true
171180
}

index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ export default class AdminForthAgentPlugin extends AdminForthPlugin {
162162
runAndPersistAgentResponse: this.agentTurnService.runAndPersistAgentResponse.bind(this.agentTurnService),
163163
getSessionTurns: this.sessionStore.getSessionTurns.bind(this.sessionStore),
164164
createNewTurn: this.sessionStore.createNewTurn.bind(this.sessionStore),
165+
createSystemTurn: this.sessionStore.createSystemTurn.bind(this.sessionStore),
165166
getChatSurfaceConnectActionAdapters: this.chatSurfaceService.getConnectActionAdapters.bind(this.chatSurfaceService),
166167
createChatSurfaceLinkToken: this.chatSurfaceService.createLinkToken.bind(this.chatSurfaceService),
167168
handleChatSurfaceMessage: this.chatSurfaceService.handleMessage.bind(this.chatSurfaceService),

sessionStore.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import type { ChatSurfaceIncomingMessage } from "adminforth";
55
import type { PreviousUserMessage } from "./agent/languageDetect.js";
66
import type { PluginOptions } from "./types.js";
77

8+
export const AGENT_SYSTEM_TURN_PROMPT = "__adminforth_system_message__";
9+
810
export class AgentSessionStore {
911
constructor(
1012
private getAdminforth: () => IAdminForth,
@@ -23,6 +25,18 @@ export class AgentSessionStore {
2325
return newTurn.createdRecord[this.options.turnResource.idField];
2426
}
2527

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+
2640
async getSessionTurns(sessionId: string) {
2741
const turns = await this.getAdminforth().resource(this.options.turnResource.resourceId).list(
2842
[Filters.EQ(this.options.turnResource.sessionIdField, sessionId)],
@@ -45,6 +59,7 @@ export class AgentSessionStore {
4559
);
4660
return turns
4761
.reverse()
62+
.filter((turn) => turn[this.options.turnResource.promptField] !== AGENT_SYSTEM_TURN_PROMPT)
4863
.map((turn): PreviousUserMessage => ({
4964
text: turn[this.options.turnResource.promptField],
5065
}));

0 commit comments

Comments
 (0)