({
action,
resolve,
onError,
initialSessionState,
traceSessionId,
}: {
action: ServerAction<'prompt-response'> | ServerAction<'prompt-error'>
resolve: (value: RunReturnType) => any
onError: (error: { message: string }) => void
initialSessionState: SessionState
traceSessionId: string
})
| 834 | } |
| 835 | |
| 836 | async function handlePromptResponse({ |
| 837 | action, |
| 838 | resolve, |
| 839 | onError, |
| 840 | initialSessionState, |
| 841 | traceSessionId, |
| 842 | }: { |
| 843 | action: ServerAction<'prompt-response'> | ServerAction<'prompt-error'> |
| 844 | resolve: (value: RunReturnType) => any |
| 845 | onError: (error: { message: string }) => void |
| 846 | initialSessionState: SessionState |
| 847 | traceSessionId: string |
| 848 | }) { |
| 849 | if (action.type === 'prompt-error') { |
| 850 | onError({ message: action.message }) |
| 851 | |
| 852 | const statusCode = extractStatusCodeFromMessage(action.message) |
| 853 | resolve({ |
| 854 | sessionState: initialSessionState, |
| 855 | traceSessionId, |
| 856 | output: { |
| 857 | type: 'error', |
| 858 | message: action.message, |
| 859 | ...(statusCode !== undefined && { statusCode }), |
| 860 | }, |
| 861 | }) |
| 862 | } else if (action.type === 'prompt-response') { |
| 863 | // Stop enforcing session state schema! It's a black box we will pass back to the server. |
| 864 | // Only check the output schema. |
| 865 | const parsedOutput = AgentOutputSchema.safeParse(action.output) |
| 866 | if (!parsedOutput.success) { |
| 867 | const message = [ |
| 868 | 'Received invalid prompt response from server:', |
| 869 | JSON.stringify(parsedOutput.error.issues), |
| 870 | 'If this issues persists, please contact support@codebuff.com', |
| 871 | ].join('\n') |
| 872 | onError({ message }) |
| 873 | resolve({ |
| 874 | sessionState: initialSessionState, |
| 875 | traceSessionId, |
| 876 | output: { |
| 877 | type: 'error', |
| 878 | message, |
| 879 | }, |
| 880 | }) |
| 881 | return |
| 882 | } |
| 883 | const { sessionState, output } = action |
| 884 | |
| 885 | const state: RunState = { |
| 886 | sessionState, |
| 887 | traceSessionId, |
| 888 | output: output ?? { |
| 889 | type: 'error', |
| 890 | message: 'No output from agent', |
| 891 | }, |
| 892 | } |
| 893 | resolve(state) |
no test coverage detected