-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexpress-chat-server.ts
More file actions
172 lines (146 loc) · 4.6 KB
/
Copy pathexpress-chat-server.ts
File metadata and controls
172 lines (146 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import type {
GoodMemory,
MarkdownArtifactBundle,
} from "goodmemory";
import { createGoodMemory } from "goodmemory";
import {
runGoodMemoryThinChatTurn,
} from "./support/http-chat";
import type {
ThinChatAssistantInput,
ThinChatErrorBody,
ThinChatResponseBody,
ThinChatTurnResult,
} from "./support/http-chat";
import { withLocalDefaultRuntime } from "./support/local-default-runtime";
type ThinChatSuccessResult = Extract<ThinChatTurnResult, { statusCode: 200 }>;
export interface ExpressChatRequest {
body?: unknown;
}
export interface ExpressChatResponse {
status(code: number): ExpressChatResponse;
json(body: ThinChatResponseBody | ThinChatErrorBody): unknown;
}
export type ExpressChatHandler = (
request: ExpressChatRequest,
response: ExpressChatResponse,
) => Promise<void> | void;
export interface ExpressLikeApp {
post(path: string, handler: ExpressChatHandler): unknown;
}
export interface RegisterExpressGoodMemoryChatRouteInput {
drainJobs?: boolean;
generateAssistantText?(
input: ThinChatAssistantInput,
): Promise<string> | string;
memory?: GoodMemory;
path?: string;
}
export interface RegisteredExpressGoodMemoryChatRoute {
memory: GoodMemory;
path: string;
}
class CapturedExpressResponse implements ExpressChatResponse {
body: ThinChatResponseBody | ThinChatErrorBody | undefined;
statusCode = 200;
status(code: number): ExpressChatResponse {
this.statusCode = code;
return this;
}
json(body: ThinChatResponseBody | ThinChatErrorBody): unknown {
this.body = body;
return body;
}
}
class CapturedExpressApp implements ExpressLikeApp {
readonly routes = new Map<string, ExpressChatHandler>();
post(path: string, handler: ExpressChatHandler): unknown {
this.routes.set(path, handler);
return undefined;
}
async inject(path: string, body: unknown): Promise<ThinChatTurnResult> {
const handler = this.routes.get(path);
if (!handler) {
throw new Error(`No Express route registered for ${path}.`);
}
const response = new CapturedExpressResponse();
await handler({ body }, response);
return {
statusCode: response.statusCode as ThinChatTurnResult["statusCode"],
body: response.body ?? { error: "No response body was sent." },
} as ThinChatTurnResult;
}
}
function requireSuccessResult(result: ThinChatTurnResult): ThinChatSuccessResult {
if (result.statusCode !== 200) {
throw new Error(`Expected a successful Express example response.`);
}
return result;
}
export function registerExpressGoodMemoryChatRoute(
app: ExpressLikeApp,
input: RegisterExpressGoodMemoryChatRouteInput = {},
): RegisteredExpressGoodMemoryChatRoute {
const memory = input.memory ?? createGoodMemory({});
const path = input.path ?? "/chat";
app.post(path, async (request, response) => {
const result = await runGoodMemoryThinChatTurn({
body: request.body,
drainJobs: input.drainJobs,
generateAssistantText: input.generateAssistantText,
memory,
});
response.status(result.statusCode).json(result.body);
});
return { memory, path };
}
export async function runExpressChatServerExample(): Promise<{
artifacts: MarkdownArtifactBundle;
firstResponse: ThinChatSuccessResult;
routePath: string;
secondResponse: ThinChatSuccessResult;
}> {
return withLocalDefaultRuntime("goodmemory-example-express-chat", async () => {
const app = new CapturedExpressApp();
const memory = createGoodMemory({});
const registered = registerExpressGoodMemoryChatRoute(app, {
drainJobs: true,
memory,
});
const firstResponse = requireSuccessResult(
await app.inject(registered.path, {
userId: "express-user",
workspaceId: "express-workspace",
sessionId: "express-s1",
turnId: "express-turn-1",
message:
"Remember that the migration rollout is blocked on staging smoke verification.",
}),
);
const secondResponse = requireSuccessResult(
await app.inject(registered.path, {
userId: "express-user",
workspaceId: "express-workspace",
sessionId: "express-s2",
turnId: "express-turn-2",
message: "What is blocking the migration rollout?",
}),
);
const exported = await memory.exportMemory({
scope: {
userId: "express-user",
workspaceId: "express-workspace",
},
});
return {
artifacts: exported.artifacts,
firstResponse,
routePath: registered.path,
secondResponse,
};
});
}
if (import.meta.main) {
const result = await runExpressChatServerExample();
console.log(JSON.stringify(result, null, 2));
}