Skip to content

Commit 5e7079f

Browse files
committed
remove zod schema for server action
1 parent 6d0ed85 commit 5e7079f

File tree

4 files changed

+146
-173
lines changed

4 files changed

+146
-173
lines changed

backend/src/websockets/websocket-action.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import {
22
cancelUserInput,
33
startUserInput,
44
} from '@codebuff/agent-runtime/live-user-inputs'
5+
import { callMainPrompt } from '@codebuff/agent-runtime/main-prompt'
56
import { calculateUsageAndBalance } from '@codebuff/billing'
67
import { trackEvent } from '@codebuff/common/analytics'
78
import { AnalyticsEvent } from '@codebuff/common/constants/analytics-events'
9+
import { getErrorObject } from '@codebuff/common/util/error'
810
import db from '@codebuff/internal/db/index'
911
import * as schema from '@codebuff/internal/db/schema'
10-
import { getErrorObject } from '@codebuff/common/util/error'
1112
import { eq } from 'drizzle-orm'
1213

1314
import { protec } from './middleware'
@@ -22,7 +23,6 @@ import type { Logger } from '@codebuff/common/types/contracts/logger'
2223
import type { ParamsExcluding } from '@codebuff/common/types/function-params'
2324
import type { ClientMessage } from '@codebuff/common/websockets/websocket-schema'
2425
import type { WebSocket } from 'ws'
25-
import { callMainPrompt } from '@codebuff/agent-runtime/main-prompt'
2626

2727
/**
2828
* Generates a usage response object for the client
@@ -330,9 +330,12 @@ export const onWebsocketAction = async (params: {
330330
}
331331

332332
// Register action handlers
333-
subscribeToAction('prompt', protec.run({ baseAction: onPrompt }))
334-
subscribeToAction('init', protec.run({ baseAction: onInit, silent: true }))
333+
subscribeToAction('prompt', protec.run<'prompt'>({ baseAction: onPrompt }))
334+
subscribeToAction(
335+
'init',
336+
protec.run<'init'>({ baseAction: onInit, silent: true }),
337+
)
335338
subscribeToAction(
336339
'cancel-user-input',
337-
protec.run({ baseAction: onCancelUserInput }),
340+
protec.run<'cancel-user-input'>({ baseAction: onCancelUserInput }),
338341
)

common/src/actions.ts

Lines changed: 126 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,16 @@
11
import { z } from 'zod/v4'
22

3-
import { GrantTypeValues } from './types/grant'
4-
import { mcpConfigSchema } from './types/mcp'
5-
import { toolMessageSchema } from './types/messages/codebuff-message'
6-
import { printModeEventSchema } from './types/print-mode'
7-
import {
8-
AgentOutputSchema,
9-
SessionStateSchema,
10-
toolCallSchema,
11-
} from './types/session-state'
12-
133
import type { CostMode } from './old-constants'
4+
import type { GrantType } from './types/grant'
5+
import type { MCPConfig } from './types/mcp'
146
import type { ToolMessage } from './types/messages/codebuff-message'
157
import type {
168
TextPart,
179
ImagePart,
1810
ToolResultOutput,
1911
} from './types/messages/content-part'
20-
import type { SessionState } from './types/session-state'
12+
import type { PrintModeEvent } from './types/print-mode'
13+
import type { AgentOutput, SessionState, ToolCall } from './types/session-state'
2114
import type { ProjectFileContext } from './util/file'
2215

2316
export const FileChangeSchema = z.object({
@@ -98,120 +91,125 @@ export type ClientAction<T extends ClientActionType = ClientActionType> = {
9891
>
9992
}[T]
10093

101-
export const UsageReponseSchema = z.object({
102-
type: z.literal('usage-response'),
103-
usage: z.number(),
104-
remainingBalance: z.number(),
105-
balanceBreakdown: z
106-
.record(
107-
z.enum([GrantTypeValues[0], ...GrantTypeValues.slice(1)]),
108-
z.number(),
109-
)
110-
.optional(),
111-
next_quota_reset: z.coerce.date().nullable(),
112-
autoTopupAdded: z.number().optional(),
113-
})
114-
export type UsageResponse = z.infer<typeof UsageReponseSchema>
115-
116-
export const InitResponseSchema = z
117-
.object({
118-
type: z.literal('init-response'),
119-
message: z.string().optional(),
120-
agentNames: z.record(z.string(), z.string()).optional(),
121-
})
122-
.merge(
123-
UsageReponseSchema.omit({
124-
type: true,
125-
}),
126-
)
127-
export type InitResponse = z.infer<typeof InitResponseSchema>
128-
129-
export const MessageCostResponseSchema = z.object({
130-
type: z.literal('message-cost-response'),
131-
promptId: z.string(),
132-
credits: z.number(),
133-
agentId: z.string().optional(),
134-
})
135-
export type MessageCostResponse = z.infer<typeof MessageCostResponseSchema>
136-
137-
export const PromptResponseSchema = z.object({
138-
type: z.literal('prompt-response'),
139-
promptId: z.string(),
140-
sessionState: SessionStateSchema,
141-
toolCalls: z.array(toolCallSchema).optional(),
142-
toolResults: z.array(toolMessageSchema).optional(),
143-
output: AgentOutputSchema.optional(),
144-
})
145-
export type PromptResponse = z.infer<typeof PromptResponseSchema>
146-
147-
export const SERVER_ACTION_SCHEMA = z.discriminatedUnion('type', [
148-
z.object({
149-
type: z.literal('response-chunk'),
150-
userInputId: z.string(),
151-
chunk: z.union([z.string(), printModeEventSchema]),
152-
}),
153-
z.object({
154-
type: z.literal('subagent-response-chunk'),
155-
userInputId: z.string(),
156-
agentId: z.string(),
157-
agentType: z.string(),
158-
chunk: z.string(),
159-
prompt: z.string().optional(),
160-
forwardToPrompt: z.boolean().optional(),
161-
}),
162-
z.object({
163-
type: z.literal('handlesteps-log-chunk'),
164-
userInputId: z.string(),
165-
agentId: z.string(),
166-
level: z.enum(['debug', 'info', 'warn', 'error']),
167-
data: z.any(),
168-
message: z.string().optional(),
169-
}),
170-
PromptResponseSchema,
171-
z.object({
172-
type: z.literal('read-files'),
173-
filePaths: z.array(z.string()),
174-
requestId: z.string(),
175-
}),
176-
z.object({
177-
type: z.literal('tool-call-request'),
178-
userInputId: z.string(),
179-
requestId: z.string(),
180-
toolName: z.string(),
181-
input: z.record(z.string(), z.any()),
182-
timeout: z.number().optional(),
183-
mcpConfig: mcpConfigSchema.optional(),
184-
}),
185-
InitResponseSchema,
186-
UsageReponseSchema,
187-
MessageCostResponseSchema,
188-
189-
z.object({
190-
type: z.literal('action-error'),
191-
message: z.string(),
192-
error: z.string().optional(),
193-
remainingBalance: z.number().optional(),
194-
}),
195-
z.object({
196-
type: z.literal('prompt-error'),
197-
userInputId: z.string(),
198-
message: z.string(),
199-
error: z.string().optional(),
200-
remainingBalance: z.number().optional(),
201-
}),
202-
z.object({
203-
// The server is imminently going to shutdown, and the client should reconnect
204-
type: z.literal('request-reconnect'),
205-
}),
206-
z.object({
207-
type: z.literal('request-mcp-tool-data'),
208-
requestId: z.string(),
209-
mcpConfig: mcpConfigSchema,
210-
toolNames: z.string().array().optional(),
211-
}),
212-
])
213-
214-
type ServerActionAny = z.infer<typeof SERVER_ACTION_SCHEMA>
215-
export type ServerAction<
216-
T extends ServerActionAny['type'] = ServerActionAny['type'],
217-
> = Extract<ServerActionAny, { type: T }>
94+
type ServerActionResponseChunk = {
95+
type: 'response-chunk'
96+
userInputId: string
97+
chunk: string | PrintModeEvent
98+
}
99+
100+
type ServerActionSubagentResponseChunk = {
101+
type: 'subagent-response-chunk'
102+
userInputId: string
103+
agentId: string
104+
agentType: string
105+
chunk: string
106+
prompt?: string
107+
forwardToPrompt?: boolean
108+
}
109+
110+
type ServerActionHandleStepsLogChunk = {
111+
type: 'handlesteps-log-chunk'
112+
userInputId: string
113+
agentId: string
114+
level: 'debug' | 'info' | 'warn' | 'error'
115+
data: any
116+
message?: string
117+
}
118+
119+
export type PromptResponse = {
120+
type: 'prompt-response'
121+
promptId: string
122+
sessionState: SessionState
123+
toolCalls?: ToolCall[]
124+
toolResults?: ToolMessage[]
125+
output?: AgentOutput
126+
}
127+
128+
type ServerActionReadFiles = {
129+
type: 'read-files'
130+
filePaths: string[]
131+
requestId: string
132+
}
133+
134+
type ServerActionToolCallRequest = {
135+
type: 'tool-call-request'
136+
userInputId: string
137+
requestId: string
138+
toolName: string
139+
input: Record<string, any>
140+
timeout?: number
141+
mcpConfig?: MCPConfig
142+
}
143+
144+
export type InitResponse = {
145+
type: 'init-response'
146+
message?: string
147+
agentNames?: Record<string, string>
148+
} & Omit<UsageResponse, 'type'>
149+
150+
export type UsageResponse = {
151+
type: 'usage-response'
152+
usage: number
153+
remainingBalance: number
154+
balanceBreakdown?: Record<GrantType, number>
155+
next_quota_reset: Date | null
156+
autoTopupAdded?: number
157+
}
158+
159+
export type MessageCostResponse = {
160+
type: 'message-cost-response'
161+
promptId: string
162+
credits: number
163+
agentId?: string
164+
}
165+
166+
type ServerActionActionError = {
167+
type: 'action-error'
168+
message: string
169+
error?: string
170+
remainingBalance?: number
171+
}
172+
173+
type ServerActionPromptError = {
174+
type: 'prompt-error'
175+
userInputId: string
176+
message: string
177+
error?: string
178+
remainingBalance?: number
179+
}
180+
181+
type ServerActionRequestReconnect = {
182+
// The server is imminently going to shutdown, and the client should reconnect
183+
type: 'request-reconnect'
184+
}
185+
186+
type ServerActionRequestMcpToolData = {
187+
type: 'request-mcp-tool-data'
188+
requestId: string
189+
mcpConfig: MCPConfig
190+
toolNames?: string[]
191+
}
192+
193+
type ServerActionAny =
194+
| ServerActionResponseChunk
195+
| ServerActionSubagentResponseChunk
196+
| ServerActionHandleStepsLogChunk
197+
| PromptResponse
198+
| ServerActionReadFiles
199+
| ServerActionToolCallRequest
200+
| InitResponse
201+
| UsageResponse
202+
| MessageCostResponse
203+
| ServerActionActionError
204+
| ServerActionPromptError
205+
| ServerActionRequestReconnect
206+
| ServerActionRequestMcpToolData
207+
type ServerActionType = ServerActionAny['type']
208+
export type ServerAction<T extends ServerActionType = ServerActionType> = {
209+
[K in ServerActionType]: Extract<
210+
ServerActionAny,
211+
{
212+
type: K
213+
}
214+
>
215+
}[T]

0 commit comments

Comments
 (0)