@xalia/agent 0.5.8 → 0.6.1

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.
Files changed (185) hide show
  1. package/README.md +23 -8
  2. package/dist/agent/src/agent/agent.js +173 -96
  3. package/dist/agent/src/agent/agentUtils.js +82 -53
  4. package/dist/agent/src/agent/compressingContextManager.js +102 -0
  5. package/dist/agent/src/agent/context.js +189 -0
  6. package/dist/agent/src/agent/dummyLLM.js +46 -5
  7. package/dist/agent/src/agent/iAgentEventHandler.js +2 -0
  8. package/dist/agent/src/agent/mcpServerManager.js +22 -23
  9. package/dist/agent/src/agent/nullAgentEventHandler.js +21 -0
  10. package/dist/agent/src/agent/nullPlatform.js +14 -0
  11. package/dist/agent/src/agent/openAILLMStreaming.js +12 -7
  12. package/dist/agent/src/agent/promptProvider.js +63 -0
  13. package/dist/agent/src/agent/repeatLLM.js +5 -5
  14. package/dist/agent/src/agent/sudoMcpServerManager.js +11 -9
  15. package/dist/agent/src/agent/tokenAuth.js +7 -7
  16. package/dist/agent/src/agent/tools.js +1 -1
  17. package/dist/agent/src/chat/client/chatClient.js +733 -0
  18. package/dist/agent/src/chat/client/connection.js +209 -0
  19. package/dist/agent/src/chat/client/connection.test.js +188 -0
  20. package/dist/agent/src/chat/client/constants.js +5 -0
  21. package/dist/agent/src/chat/client/index.js +15 -0
  22. package/dist/agent/src/chat/client/interfaces.js +2 -0
  23. package/dist/agent/src/chat/client/responseHandler.js +105 -0
  24. package/dist/agent/src/chat/client/sessionClient.js +331 -0
  25. package/dist/agent/src/chat/client/teamManager.js +2 -0
  26. package/dist/agent/src/chat/{apiKeyManager.js → data/apiKeyManager.js} +4 -0
  27. package/dist/agent/src/chat/data/dataModels.js +2 -0
  28. package/dist/agent/src/chat/data/database.js +749 -0
  29. package/dist/agent/src/chat/data/dbMcpServerConfigs.js +47 -0
  30. package/dist/agent/src/chat/protocol/connectionMessages.js +5 -0
  31. package/dist/agent/src/chat/protocol/constants.js +50 -0
  32. package/dist/agent/src/chat/protocol/errors.js +22 -0
  33. package/dist/agent/src/chat/protocol/messages.js +110 -0
  34. package/dist/agent/src/chat/server/chatContextManager.js +405 -0
  35. package/dist/agent/src/chat/server/connectionManager.js +352 -0
  36. package/dist/agent/src/chat/server/connectionManager.test.js +159 -0
  37. package/dist/agent/src/chat/server/conversation.js +198 -0
  38. package/dist/agent/src/chat/server/errorUtils.js +23 -0
  39. package/dist/agent/src/chat/server/openSession.js +869 -0
  40. package/dist/agent/src/chat/server/server.js +177 -0
  41. package/dist/agent/src/chat/server/sessionFileManager.js +161 -0
  42. package/dist/agent/src/chat/server/sessionRegistry.js +700 -0
  43. package/dist/agent/src/chat/server/sessionRegistry.test.js +97 -0
  44. package/dist/agent/src/chat/server/test-utils/mockFactories.js +307 -0
  45. package/dist/agent/src/chat/server/tools.js +243 -0
  46. package/dist/agent/src/chat/utils/agentSessionMap.js +66 -0
  47. package/dist/agent/src/chat/utils/approvalManager.js +85 -0
  48. package/dist/agent/src/{utils → chat/utils}/asyncLock.js +3 -3
  49. package/dist/agent/src/chat/{asyncQueue.js → utils/asyncQueue.js} +12 -2
  50. package/dist/agent/src/chat/utils/htmlToText.js +84 -0
  51. package/dist/agent/src/chat/utils/multiAsyncQueue.js +42 -0
  52. package/dist/agent/src/chat/utils/search.js +145 -0
  53. package/dist/agent/src/chat/utils/userResolver.js +46 -0
  54. package/dist/agent/src/chat/{websocket.js → utils/websocket.js} +2 -0
  55. package/dist/agent/src/test/agent.test.js +332 -0
  56. package/dist/agent/src/test/approvalManager.test.js +58 -0
  57. package/dist/agent/src/test/chatContextManager.test.js +392 -0
  58. package/dist/agent/src/test/clientServerConnection.test.js +158 -0
  59. package/dist/agent/src/test/compressingContextManager.test.js +65 -0
  60. package/dist/agent/src/test/context.test.js +83 -0
  61. package/dist/agent/src/test/conversation.test.js +89 -0
  62. package/dist/agent/src/test/db.test.js +262 -90
  63. package/dist/agent/src/test/dbMcpServerConfigs.test.js +72 -0
  64. package/dist/agent/src/test/dbTestTools.js +99 -0
  65. package/dist/agent/src/test/imageLoad.test.js +8 -7
  66. package/dist/agent/src/test/mcpServerManager.test.js +21 -18
  67. package/dist/agent/src/test/multiAsyncQueue.test.js +101 -0
  68. package/dist/agent/src/test/openaiStreaming.test.js +12 -11
  69. package/dist/agent/src/test/prompt.test.js +5 -4
  70. package/dist/agent/src/test/promptProvider.test.js +28 -0
  71. package/dist/agent/src/test/responseHandler.test.js +61 -0
  72. package/dist/agent/src/test/sudoMcpServerManager.test.js +14 -12
  73. package/dist/agent/src/test/testTools.js +109 -0
  74. package/dist/agent/src/test/tools.test.js +31 -0
  75. package/dist/agent/src/tool/agentChat.js +21 -10
  76. package/dist/agent/src/tool/agentMain.js +1 -1
  77. package/dist/agent/src/tool/chatMain.js +235 -58
  78. package/dist/agent/src/tool/commandPrompt.js +15 -9
  79. package/dist/agent/src/tool/files.js +20 -16
  80. package/dist/agent/src/tool/nodePlatform.js +47 -3
  81. package/dist/agent/src/tool/options.js +4 -4
  82. package/dist/agent/src/tool/prompt.js +19 -13
  83. package/eslint.config.mjs +14 -1
  84. package/package.json +14 -6
  85. package/scripts/chat_server +8 -0
  86. package/scripts/setup_chat +7 -2
  87. package/scripts/shutdown_chat_server +3 -0
  88. package/scripts/test_chat +135 -17
  89. package/src/agent/agent.ts +270 -135
  90. package/src/agent/agentUtils.ts +136 -95
  91. package/src/agent/compressingContextManager.ts +164 -0
  92. package/src/agent/context.ts +268 -0
  93. package/src/agent/dummyLLM.ts +76 -8
  94. package/src/agent/iAgentEventHandler.ts +54 -0
  95. package/src/agent/iplatform.ts +1 -0
  96. package/src/agent/mcpServerManager.ts +32 -30
  97. package/src/agent/nullAgentEventHandler.ts +20 -0
  98. package/src/agent/nullPlatform.ts +13 -0
  99. package/src/agent/openAILLMStreaming.ts +12 -6
  100. package/src/agent/promptProvider.ts +87 -0
  101. package/src/agent/repeatLLM.ts +5 -5
  102. package/src/agent/sudoMcpServerManager.ts +13 -11
  103. package/src/agent/tokenAuth.ts +7 -7
  104. package/src/agent/tools.ts +3 -1
  105. package/src/chat/client/chatClient.ts +900 -0
  106. package/src/chat/client/connection.test.ts +241 -0
  107. package/src/chat/client/connection.ts +276 -0
  108. package/src/chat/client/constants.ts +3 -0
  109. package/src/chat/client/index.ts +18 -0
  110. package/src/chat/client/interfaces.ts +34 -0
  111. package/src/chat/client/responseHandler.ts +131 -0
  112. package/src/chat/client/sessionClient.ts +443 -0
  113. package/src/chat/client/teamManager.ts +29 -0
  114. package/src/chat/{apiKeyManager.ts → data/apiKeyManager.ts} +6 -2
  115. package/src/chat/data/dataModels.ts +85 -0
  116. package/src/chat/data/database.ts +982 -0
  117. package/src/chat/data/dbMcpServerConfigs.ts +59 -0
  118. package/src/chat/protocol/connectionMessages.ts +49 -0
  119. package/src/chat/protocol/constants.ts +55 -0
  120. package/src/chat/protocol/errors.ts +16 -0
  121. package/src/chat/protocol/messages.ts +682 -0
  122. package/src/chat/server/README.md +127 -0
  123. package/src/chat/server/chatContextManager.ts +612 -0
  124. package/src/chat/server/connectionManager.test.ts +266 -0
  125. package/src/chat/server/connectionManager.ts +541 -0
  126. package/src/chat/server/conversation.ts +269 -0
  127. package/src/chat/server/errorUtils.ts +28 -0
  128. package/src/chat/server/openSession.ts +1332 -0
  129. package/src/chat/server/server.ts +177 -0
  130. package/src/chat/server/sessionFileManager.ts +239 -0
  131. package/src/chat/server/sessionRegistry.test.ts +138 -0
  132. package/src/chat/server/sessionRegistry.ts +1064 -0
  133. package/src/chat/server/test-utils/mockFactories.ts +422 -0
  134. package/src/chat/server/tools.ts +265 -0
  135. package/src/chat/utils/agentSessionMap.ts +76 -0
  136. package/src/chat/utils/approvalManager.ts +111 -0
  137. package/src/{utils → chat/utils}/asyncLock.ts +3 -3
  138. package/src/chat/{asyncQueue.ts → utils/asyncQueue.ts} +14 -3
  139. package/src/chat/utils/htmlToText.ts +61 -0
  140. package/src/chat/utils/multiAsyncQueue.ts +52 -0
  141. package/src/chat/utils/search.ts +139 -0
  142. package/src/chat/utils/userResolver.ts +48 -0
  143. package/src/chat/{websocket.ts → utils/websocket.ts} +2 -0
  144. package/src/test/agent.test.ts +487 -0
  145. package/src/test/approvalManager.test.ts +73 -0
  146. package/src/test/chatContextManager.test.ts +521 -0
  147. package/src/test/clientServerConnection.test.ts +207 -0
  148. package/src/test/compressingContextManager.test.ts +82 -0
  149. package/src/test/context.test.ts +105 -0
  150. package/src/test/conversation.test.ts +109 -0
  151. package/src/test/db.test.ts +351 -103
  152. package/src/test/dbMcpServerConfigs.test.ts +112 -0
  153. package/src/test/dbTestTools.ts +153 -0
  154. package/src/test/imageLoad.test.ts +7 -6
  155. package/src/test/mcpServerManager.test.ts +19 -14
  156. package/src/test/multiAsyncQueue.test.ts +125 -0
  157. package/src/test/openaiStreaming.test.ts +11 -10
  158. package/src/test/prompt.test.ts +4 -3
  159. package/src/test/promptProvider.test.ts +33 -0
  160. package/src/test/responseHandler.test.ts +78 -0
  161. package/src/test/sudoMcpServerManager.test.ts +22 -15
  162. package/src/test/testTools.ts +146 -0
  163. package/src/test/tools.test.ts +39 -0
  164. package/src/tool/agentChat.ts +26 -12
  165. package/src/tool/agentMain.ts +1 -1
  166. package/src/tool/chatMain.ts +283 -100
  167. package/src/tool/commandPrompt.ts +25 -9
  168. package/src/tool/files.ts +25 -19
  169. package/src/tool/nodePlatform.ts +52 -3
  170. package/src/tool/options.ts +4 -2
  171. package/src/tool/prompt.ts +22 -15
  172. package/test_data/dummyllm_script_crash.json +32 -0
  173. package/test_data/frog.png.b64 +1 -0
  174. package/vitest.config.ts +39 -0
  175. package/dist/agent/src/chat/client.js +0 -310
  176. package/dist/agent/src/chat/conversationManager.js +0 -502
  177. package/dist/agent/src/chat/db.js +0 -218
  178. package/dist/agent/src/chat/messages.js +0 -29
  179. package/dist/agent/src/chat/server.js +0 -158
  180. package/src/chat/client.ts +0 -445
  181. package/src/chat/conversationManager.ts +0 -730
  182. package/src/chat/db.ts +0 -304
  183. package/src/chat/messages.ts +0 -266
  184. package/src/chat/server.ts +0 -177
  185. /package/{frog.png → test_data/frog.png} +0 -0
package/src/chat/db.ts DELETED
@@ -1,304 +0,0 @@
1
- import { createClient, SupabaseClient } from "@supabase/supabase-js";
2
- import type * as supabase from "../../../supabase/database.types";
3
- import {
4
- AgentProfile,
5
- ApiKey,
6
- getLogger,
7
- SavedAgentProfile,
8
- } from "@xalia/xmcp/sdk";
9
- import { ChatCompletionMessageParam } from "../agent/agent";
10
-
11
- const logger = getLogger();
12
-
13
- export const SUPABASE_LOCAL_URL = "http://127.0.0.1:54321";
14
- export const SUPABASE_LOCAL_KEY =
15
- "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiw" +
16
- "icm9sZSI6InNlcnZpY2Vfcm9sZSIsImV4cCI6MTk4MzgxMjk5Nn0.EGIM96RAZx35lJz" +
17
- "dJsyH-qQwv8Hdp7fsn3W0YpN81IU";
18
-
19
- /**
20
- * 'name' -> 'name'
21
- * 'space/name' -> ['space', 'name']
22
- */
23
- export function resolveCompoundName(name: string): string | [string, string] {
24
- const components = name.split("/");
25
- if (components.length === 1) {
26
- return name;
27
- }
28
- if (components.length !== 2) {
29
- throw "invalid compound name";
30
- }
31
- return components as [string, string];
32
- }
33
-
34
- export type UserData = {
35
- uuid: supabase.Tables<"api_keys">["user_uuid"];
36
- nickname: supabase.Tables<"users">["nickname"];
37
- };
38
-
39
- export class Database {
40
- private client: SupabaseClient<Database>;
41
-
42
- constructor(supabaseUrl: string, supabaseKey: string) {
43
- this.client = createClient<Database>(supabaseUrl, supabaseKey);
44
- }
45
-
46
- async getUserDataFromApiKey(apiKey: string): Promise<UserData | undefined> {
47
- const { data, error } = await this.client
48
- .from("api_keys")
49
- .select("user_uuid, users ( nickname )")
50
- .eq("api_key", apiKey)
51
- .maybeSingle<{
52
- user_uuid: supabase.Tables<"api_keys">["user_uuid"];
53
- users: {
54
- nickname: supabase.Tables<"users">["nickname"];
55
- };
56
- }>();
57
-
58
- logger.debug(
59
- `[getUserDataFromApiKey]: got ${JSON.stringify({ data, error })}`
60
- );
61
-
62
- if (error) {
63
- throw error;
64
- }
65
-
66
- if (data === null) {
67
- return undefined;
68
- }
69
-
70
- return {
71
- uuid: data.user_uuid,
72
- nickname: data.users.nickname || `user ${data.user_uuid}`,
73
- };
74
- }
75
-
76
- async getUserFromUuid(user_uuid: string): Promise<UserData | undefined> {
77
- const { data, error } = await this.client
78
- .from("users")
79
- .select("*")
80
- .eq("uuid", user_uuid)
81
- .maybeSingle();
82
- if (error) {
83
- throw error;
84
- }
85
-
86
- return data;
87
- }
88
-
89
- async createUser(
90
- user_uuid: string,
91
- email: string,
92
- nickname: string,
93
- timezone?: string
94
- ): Promise<void> {
95
- const payload: supabase.TablesInsert<"users"> = {
96
- uuid: user_uuid,
97
- email,
98
- nickname,
99
- timezone: timezone || "UTC",
100
- };
101
-
102
- const { error } = await this.client.from("users").insert(payload);
103
- if (error) {
104
- throw error;
105
- }
106
- }
107
-
108
- async addApiKey(
109
- user_uuid: string,
110
- api_key: string,
111
- name: string,
112
- scopes: string[],
113
- is_default: boolean = false
114
- ): Promise<ApiKey> {
115
- const payload: supabase.TablesInsert<"api_keys"> = {
116
- user_uuid,
117
- api_key,
118
- name,
119
- scopes,
120
- is_default,
121
- };
122
- const { data, error } = await this.client
123
- .from("api_keys")
124
- .insert(payload)
125
- .select("*")
126
- .maybeSingle();
127
- if (error) {
128
- throw error;
129
- }
130
-
131
- return data;
132
- }
133
-
134
- async getSavedAgentProfileById(
135
- agentProfileId: string
136
- ): Promise<SavedAgentProfile | undefined> {
137
- const { data, error } = await this.client
138
- .from("agent_profiles")
139
- .select("*")
140
- .eq("uuid", agentProfileId)
141
- .maybeSingle<supabase.Tables<"agent_profiles">>();
142
- if (error) {
143
- throw error;
144
- }
145
-
146
- return data
147
- ? SavedAgentProfile.fromJSONObj(data as Record<string, unknown>)
148
- : undefined;
149
- }
150
-
151
- async getSavedAgentProfileByName(
152
- user_uuid: string,
153
- agentProfileName: string
154
- ): Promise<SavedAgentProfile | undefined> {
155
- const { data, error } = await this.client
156
- .from("agent_profiles")
157
- .select("*")
158
- .eq("user_uuid", user_uuid)
159
- .eq("profile_name", agentProfileName)
160
- .maybeSingle<supabase.Tables<"agent_profiles">>();
161
- if (error) {
162
- throw error;
163
- }
164
-
165
- return data
166
- ? SavedAgentProfile.fromJSONObj(data as Record<string, unknown>)
167
- : undefined;
168
- }
169
-
170
- async getAgentProfileById(
171
- agentProfileId: string
172
- ): Promise<AgentProfile | undefined> {
173
- const { data, error } = await this.client
174
- .from("agent_profiles")
175
- .select("profile")
176
- .eq("uuid", agentProfileId)
177
- .maybeSingle<{
178
- profile: supabase.Tables<"agent_profiles">["profile"];
179
- }>();
180
- if (error) {
181
- throw error;
182
- }
183
- return data
184
- ? AgentProfile.fromJSONObj(data.profile as Record<string, unknown>)
185
- : undefined;
186
- }
187
-
188
- async createAgentProfile(
189
- user_uuid: string,
190
- profileName: string,
191
- profile: AgentProfile
192
- ): Promise<string | undefined> {
193
- const payload: supabase.TablesInsert<"agent_profiles"> = {
194
- profile: profile as unknown as supabase.Json,
195
- user_uuid,
196
- profile_name: profileName,
197
- };
198
- const { data, error } = await this.client
199
- .from("agent_profiles")
200
- .upsert(payload)
201
- .select("uuid");
202
- if (error) {
203
- throw error;
204
- }
205
-
206
- if (!data || !data[0] || !data[0].uuid) {
207
- return undefined;
208
- }
209
-
210
- return data[0].uuid;
211
- }
212
-
213
- async updateAgentProfile(uuid: string, profile: AgentProfile): Promise<void> {
214
- const payload: supabase.TablesUpdate<"agent_profiles"> = {
215
- profile: profile as unknown as supabase.Json,
216
- };
217
- const { error } = await this.client
218
- .from("agent_profiles")
219
- .update(payload)
220
- .eq("uuid", uuid);
221
- if (error) {
222
- throw error;
223
- }
224
- }
225
-
226
- async clearAgentProfiles(): Promise<void> {
227
- await this.client.from("agent_profiles").delete().neq("uuid", "");
228
- }
229
-
230
- // TODO: is there a session model?
231
-
232
- async getSessionById(
233
- session_uuid: string
234
- ): Promise<supabase.Tables<"sessions"> | undefined> {
235
- const { data, error } = await this.client
236
- .from("sessions")
237
- .select("*")
238
- .eq("uuid", session_uuid)
239
- .maybeSingle();
240
- if (error) {
241
- throw error;
242
- }
243
- return data;
244
- }
245
-
246
- async getSessionByName(
247
- user_uuid: string,
248
- session_name: string
249
- ): Promise<supabase.Tables<"sessions"> | undefined> {
250
- const { data, error } = await this.client
251
- .from("sessions")
252
- .select("*")
253
- .eq("user_uuid", user_uuid)
254
- .eq("title", session_name)
255
- .maybeSingle();
256
- if (error) {
257
- throw error;
258
- }
259
- return data;
260
- }
261
-
262
- async createSession(
263
- user_uuid: string,
264
- title: string,
265
- agentProfileId: string
266
- ): Promise<string | undefined> {
267
- const payload: supabase.TablesInsert<"sessions"> = {
268
- agent_profile_uuid: agentProfileId,
269
- user_uuid,
270
- title: title,
271
- conversation: [],
272
- };
273
- const { data, error } = await this.client
274
- .from("sessions")
275
- .upsert(payload)
276
- .select("uuid");
277
- if (error) {
278
- throw error;
279
- }
280
-
281
- if (!data || !data[0] || !data[0].uuid) {
282
- return undefined;
283
- }
284
-
285
- return data[0].uuid;
286
- }
287
-
288
- async sessionConversationAppend(
289
- session_id: string,
290
- newEntries: ChatCompletionMessageParam[]
291
- ): Promise<void> {
292
- const { error } = await this.client.rpc("session_append_to_conversation", {
293
- session_id,
294
- messages: newEntries,
295
- });
296
- if (error) {
297
- throw error;
298
- }
299
- }
300
-
301
- async clearSessions(): Promise<void> {
302
- await this.client.from("sessions").delete().neq("uuid", "");
303
- }
304
- }
@@ -1,266 +0,0 @@
1
- import {
2
- ChatCompletionMessageParam,
3
- ChatCompletionAssistantMessageParam,
4
- ChatCompletionToolMessageParam,
5
- ChatCompletionMessageToolCall,
6
- } from "openai/resources.mjs";
7
- import { Tool } from "@modelcontextprotocol/sdk/types.js";
8
- import { McpServerBrief } from "@xalia/xmcp/sdk";
9
-
10
- /**
11
- * Message from a user to the server
12
- */
13
- export type ClientUserMessage = {
14
- type: "msg";
15
- message: string;
16
- };
17
-
18
- /**
19
- * Message to add an MCP server to the agent
20
- */
21
- export type ClientAddMcpServer = {
22
- type: "add_mcp_server";
23
- server_name: string;
24
- enable_all: boolean;
25
- };
26
-
27
- /**
28
- * Message to remove an MCP server from the agent
29
- */
30
- export type ClientRemoveMcpServer = {
31
- type: "remove_mcp_server";
32
- server_name: string;
33
- };
34
-
35
- /**
36
- * Enable a specific tool
37
- */
38
- export type ClientEnableMcpServerTool = {
39
- type: "enable_mcp_server_tool";
40
- server_name: string;
41
- tool: string;
42
- };
43
-
44
- /**
45
- * Disable a specific tool
46
- */
47
- export type ClientDisableMcpServerTool = {
48
- type: "disable_mcp_server_tool";
49
- server_name: string;
50
- tool: string;
51
- };
52
-
53
- export type ClientEnableAllMcpServerTools = {
54
- type: "enable_all_mcp_server_tools";
55
- server_name: string;
56
- };
57
-
58
- export type ClientDisableAllMcpServerTools = {
59
- type: "disable_all_mcp_server_tools";
60
- server_name: string;
61
- };
62
-
63
- export type ClientSetSystemPrompt = {
64
- type: "set_system_prompt";
65
- system_prompt: string;
66
- };
67
-
68
- export type ClientSetModel = {
69
- type: "set_model";
70
- model: string;
71
- };
72
-
73
- export type ClientToServer =
74
- | ClientUserMessage
75
- | ClientAddMcpServer
76
- | ClientRemoveMcpServer
77
- | ClientEnableMcpServerTool
78
- | ClientDisableMcpServerTool
79
- | ClientEnableAllMcpServerTools
80
- | ClientDisableAllMcpServerTools
81
- | ClientSetSystemPrompt
82
- | ClientSetModel;
83
-
84
- ///
85
- /// Server -> Client Messages
86
- ///
87
-
88
- /**
89
- * Error in response to an invalid request from the client
90
- */
91
- export type ServerError = {
92
- type: "error";
93
- message: string;
94
- };
95
-
96
- /**
97
- * (from server) Chat history
98
- */
99
- export type ServerHistory = {
100
- type: "history";
101
- /// Conversation history in the form we expect. The `name` attribute of
102
- /// `ChatCompletionUserMessageParam` (role: "user") holds the original
103
- /// message sender.
104
- messages: ChatCompletionMessageParam[];
105
- };
106
-
107
- export type ServerUserJoined = {
108
- type: "user_joined";
109
- user: string;
110
- };
111
-
112
- export type ServerUserLeft = {
113
- type: "user_left";
114
- user: string;
115
- };
116
-
117
- /**
118
- * Message from the server, informing us of a message in the chat history
119
- */
120
- export type ServerUserMessage = {
121
- type: "user_msg";
122
- message_id: string;
123
- message: string;
124
- from: string;
125
- };
126
-
127
- export type ServerAgentMessage = {
128
- type: "agent_msg";
129
- message_id: string;
130
- message: ChatCompletionAssistantMessageParam;
131
- };
132
-
133
- export type ServerAgentMessageChunk = {
134
- type: "agent_msg_chunk";
135
- message_id: string;
136
- message: string;
137
- end: boolean;
138
- };
139
-
140
- export type ServerMcpServerBriefs = {
141
- type: "mcp_server_briefs";
142
- server_briefs: McpServerBrief[];
143
- };
144
-
145
- /**
146
- * For information only (to keep the chat window consistent).
147
- */
148
- export type ServerToolCall = {
149
- type: "agent_tool_call";
150
- message_id: string;
151
- message: ChatCompletionMessageToolCall;
152
- };
153
-
154
- /**
155
- * For information only (to keep the chat window consistent)
156
- */
157
- export type ServerToolCallResult = {
158
- type: "tool_call_result";
159
- message_id: string;
160
- message: ChatCompletionToolMessageParam;
161
- };
162
-
163
- export type ServerTyping = {
164
- type: "typing";
165
- from: string;
166
- };
167
-
168
- export type ServerMcpServerAdded = {
169
- type: "mcp_server_added";
170
- server_name: string;
171
- tools: Tool[];
172
- enabled_tools: string[];
173
- };
174
-
175
- export type ServerMcpServerRemoved = {
176
- type: "mcp_server_removed";
177
- server_name: string;
178
- };
179
-
180
- export type ServerMcpServerToolEnabled = {
181
- type: "mcp_server_tool_enabled";
182
- server_name: string;
183
- tool: string;
184
- };
185
-
186
- export type ServerMcpServerToolDisabled = {
187
- type: "mcp_server_tool_disabled";
188
- server_name: string;
189
- tool: string;
190
- };
191
-
192
- export type ServerSystemPromptUpdated = {
193
- type: "system_prompt_updated";
194
- system_prompt: string;
195
- };
196
-
197
- export type ServerModelUpdated = {
198
- type: "model_updated";
199
- model: string;
200
- };
201
-
202
- export type ServerToClientStateUpdate =
203
- | ServerMcpServerAdded
204
- | ServerMcpServerRemoved
205
- | ServerMcpServerToolEnabled
206
- | ServerMcpServerToolDisabled
207
- | ServerSystemPromptUpdated
208
- | ServerModelUpdated;
209
-
210
- export type ServerOpenUrl = {
211
- type: "open_url";
212
- url: string;
213
- display_name: string;
214
- };
215
-
216
- export type ServerApproveToolCall = {
217
- type: "approve_tool_call";
218
- tbd: string;
219
- };
220
-
221
- export type ServerToClientActions = ServerOpenUrl | ServerApproveToolCall;
222
-
223
- export type ServerToClient =
224
- | ServerError
225
- | ServerUserJoined
226
- | ServerUserLeft
227
- | ServerUserMessage
228
- | ServerAgentMessage
229
- | ServerAgentMessageChunk
230
- | ServerMcpServerBriefs
231
- | ServerToolCall
232
- | ServerToolCallResult
233
- | ServerTyping
234
- | ServerToClientStateUpdate
235
- | ServerToClientActions;
236
-
237
- export function decodeAssistantMessageParam(
238
- msg: ChatCompletionAssistantMessageParam
239
- ): string {
240
- let text = "";
241
-
242
- if (msg.audio) {
243
- throw "decodeAssistantMessageParam; audio unimplemented";
244
- }
245
-
246
- if (msg.content) {
247
- if (typeof msg.content === "string") {
248
- text = msg.content;
249
- } else if (msg.content instanceof Array) {
250
- for (const c of msg.content) {
251
- switch (c.type) {
252
- case "text":
253
- text += c.text;
254
- break;
255
- case "refusal":
256
- text += c.refusal;
257
- break;
258
- default:
259
- throw Error("unexpected AssistantMessageParam.content entry");
260
- }
261
- }
262
- }
263
- }
264
-
265
- return text;
266
- }