Skip to content

Commit 87ff526

Browse files
committed
catch all errors in sdk
1 parent e5d69d2 commit 87ff526

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

packages/agent-runtime/src/util/messages.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,29 @@ export function getCancelledAdditionalMessages(args: {
7373
params: Record<string, any> | undefined
7474
content?: Array<TextPart | ImagePart>
7575
pendingAgentResponse: string
76+
systemMessage: string
7677
}): Message[] {
77-
const { prompt, params, content, pendingAgentResponse } = args
78+
const { prompt, params, content, pendingAgentResponse, systemMessage } = args
7879

7980
const messages: Message[] = [
8081
{
8182
role: 'user',
8283
content: buildUserMessageContent(prompt, params, content),
8384
tags: ['USER_PROMPT'],
8485
},
85-
{role: 'user', content: [
86-
{type: 'text', text: `<previous_assistant_message>${pendingAgentResponse}</previous_assistant_message>`},
87-
{type: 'text', text: asSystemMessage('Response cancelled by user.')},
88-
]},
86+
{
87+
role: 'user',
88+
content: [
89+
{
90+
type: 'text',
91+
text: `<previous_assistant_message>${pendingAgentResponse}</previous_assistant_message>`,
92+
},
93+
{
94+
type: 'text',
95+
text: asSystemMessage(systemMessage),
96+
},
97+
],
98+
},
8999
]
90100

91101
return messages

sdk/src/run.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ import type {
3838
} from '@codebuff/common/tools/list'
3939
import type { Logger } from '@codebuff/common/types/contracts/logger'
4040
import type { CodebuffFileSystem } from '@codebuff/common/types/filesystem'
41-
import type { CodebuffSpawn } from '@codebuff/common/types/spawn'
4241
import type {
4342
ToolResultOutput,
4443
ToolResultPart,
4544
} from '@codebuff/common/types/messages/content-part'
4645
import type { PrintModeEvent } from '@codebuff/common/types/print-mode'
4746
import type { SessionState } from '@codebuff/common/types/session-state'
4847
import type { Source } from '@codebuff/common/types/source'
48+
import type { CodebuffSpawn } from '@codebuff/common/types/spawn'
4949

5050
export type CodebuffClientOptions = {
5151
apiKey?: string
@@ -194,23 +194,25 @@ export async function run({
194194
*
195195
* This includes the user'e message and pending assistant message.
196196
*/
197-
function getCancelledSessionState(): SessionState {
197+
function getCancelledSessionState(message: string): SessionState {
198198
const state = cloneDeep(sessionState)
199199
state.mainAgentState.messageHistory.push(
200200
...getCancelledAdditionalMessages({
201201
prompt,
202202
params,
203203
pendingAgentResponse,
204+
systemMessage: message,
204205
}),
205206
)
206207
return state
207208
}
208-
function getCancelledRunState(): RunState {
209+
function getCancelledRunState(message?: string): RunState {
210+
message = message ?? 'Run cancelled by user.'
209211
return {
210-
sessionState: getCancelledSessionState(),
212+
sessionState: getCancelledSessionState(message),
211213
output: {
212214
type: 'error',
213-
message: 'Run cancelled by user',
215+
message,
214216
},
215217
}
216218
}
@@ -416,8 +418,9 @@ export async function run({
416418
fields: ['id'],
417419
})
418420
if (!userInfo) {
419-
throw new Error('No user found for key')
421+
return getCancelledRunState('Invalid API key or user not found')
420422
}
423+
421424
const userId = userInfo.id
422425

423426
signal?.addEventListener('abort', () => {
@@ -446,7 +449,7 @@ export async function run({
446449
clientSessionId: promptId,
447450
userId,
448451
signal: signal ?? new AbortController().signal,
449-
})
452+
}).catch((error) => resolve(getCancelledRunState(error.message)))
450453

451454
return promise
452455
}

0 commit comments

Comments
 (0)