mirror of
https://github.com/langgenius/dify.git
synced 2026-10-04 13:47:55 +08:00
feat(cli): adopt generated oRPC contract for unary endpoints (#37090)
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
27 files changed
+798
-184
No files matched your search
@@ -1,24 +1,24 @@
|
||||
import type { SessionListResponse } from '@dify/contracts/api/openapi/types.gen'
|
||||
import type { OpenApiClient } from '@/http/orpc'
|
||||
import type { HttpClient } from '@/http/types'
|
||||
import { createOpenApiClient } from '@/http/orpc'
|
||||
|
||||
export class AccountSessionsClient {
|
||||
private readonly http: HttpClient
|
||||
private readonly orpc: OpenApiClient
|
||||
|
||||
constructor(http: HttpClient) {
|
||||
this.http = http
|
||||
this.orpc = createOpenApiClient(http)
|
||||
}
|
||||
|
||||
async list(q?: { page?: number, limit?: number }): Promise<SessionListResponse> {
|
||||
return this.http.get<SessionListResponse>('account/sessions', {
|
||||
searchParams: { page: q?.page, limit: q?.limit },
|
||||
})
|
||||
return this.orpc.account.sessions.get({ query: { page: q?.page, limit: q?.limit } })
|
||||
}
|
||||
|
||||
async revoke(sessionId: string): Promise<void> {
|
||||
await this.http.delete(`account/sessions/${encodeURIComponent(sessionId)}`)
|
||||
await this.orpc.account.sessions.bySessionId.delete({ params: { session_id: sessionId } })
|
||||
}
|
||||
|
||||
async revokeSelf(): Promise<void> {
|
||||
await this.http.delete('account/sessions/self')
|
||||
await this.orpc.account.sessions.self.delete()
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,16 @@
|
||||
import type { AccountResponse } from '@dify/contracts/api/openapi/types.gen'
|
||||
import type { OpenApiClient } from '@/http/orpc'
|
||||
import type { HttpClient } from '@/http/types'
|
||||
import { createOpenApiClient } from '@/http/orpc'
|
||||
|
||||
export class AccountClient {
|
||||
private readonly http: HttpClient
|
||||
private readonly orpc: OpenApiClient
|
||||
|
||||
constructor(http: HttpClient) {
|
||||
this.http = http
|
||||
this.orpc = createOpenApiClient(http)
|
||||
}
|
||||
|
||||
async get(): Promise<AccountResponse> {
|
||||
return this.http.get<AccountResponse>('account')
|
||||
return this.orpc.account.get()
|
||||
}
|
||||
}
|
||||
+13
-7
@@ -1,5 +1,7 @@
|
||||
import type { OpenApiClient } from '@/http/orpc'
|
||||
import type { SseEvent } from '@/http/sse'
|
||||
import type { HttpClient } from '@/http/types'
|
||||
import { createOpenApiClient } from '@/http/orpc'
|
||||
import { parseSSE } from '@/http/sse'
|
||||
import { normalizeDifyStream } from '@/http/sse-dify'
|
||||
|
||||
@@ -36,9 +38,14 @@ export type StreamOptions = {
|
||||
|
||||
export class AppRunClient {
|
||||
private readonly http: HttpClient
|
||||
private readonly orpc: OpenApiClient
|
||||
|
||||
constructor(http: HttpClient) {
|
||||
this.http = http
|
||||
// Mixed class (SPEC §4.4): runStream / reconnectStream are SSE and stay on the raw
|
||||
// `http.stream` facade; stopTask / submitHumanInput are plain JSON and go through the
|
||||
// generated oRPC contract. Both facades share this one transport.
|
||||
this.orpc = createOpenApiClient(http)
|
||||
}
|
||||
|
||||
async runStream(
|
||||
@@ -59,9 +66,8 @@ export class AppRunClient {
|
||||
}
|
||||
|
||||
async stopTask(appId: string, taskId: string): Promise<void> {
|
||||
await this.http.post(`apps/${encodeURIComponent(appId)}/tasks/${encodeURIComponent(taskId)}/stop`, {
|
||||
json: {},
|
||||
timeoutMs: 30_000,
|
||||
await this.orpc.apps.byAppId.tasks.byTaskId.stop.post({
|
||||
params: { app_id: appId, task_id: taskId },
|
||||
})
|
||||
}
|
||||
|
||||
@@ -71,10 +77,10 @@ export class AppRunClient {
|
||||
action: string,
|
||||
inputs: Record<string, unknown>,
|
||||
): Promise<void> {
|
||||
await this.http.post(
|
||||
`apps/${encodeURIComponent(appId)}/form/human_input/${encodeURIComponent(formToken)}`,
|
||||
{ json: { action, inputs }, timeoutMs: 30_000 },
|
||||
)
|
||||
await this.orpc.apps.byAppId.form.humanInput.byFormToken.post({
|
||||
params: { app_id: appId, form_token: formToken },
|
||||
body: { action, inputs },
|
||||
})
|
||||
}
|
||||
|
||||
async reconnectStream(
|
||||
|
||||
+11
-6
@@ -1,5 +1,7 @@
|
||||
import type { AppDescribeResponse, AppListResponse } from '@dify/contracts/api/openapi/types.gen'
|
||||
import type { OpenApiClient } from '@/http/orpc'
|
||||
import type { HttpClient } from '@/http/types'
|
||||
import { createOpenApiClient } from '@/http/orpc'
|
||||
|
||||
export type ListQuery = {
|
||||
readonly workspaceId: string
|
||||
@@ -11,15 +13,15 @@ export type ListQuery = {
|
||||
}
|
||||
|
||||
export class AppsClient {
|
||||
private readonly http: HttpClient
|
||||
private readonly orpc: OpenApiClient
|
||||
|
||||
constructor(http: HttpClient) {
|
||||
this.http = http
|
||||
this.orpc = createOpenApiClient(http)
|
||||
}
|
||||
|
||||
async list(q: ListQuery): Promise<AppListResponse> {
|
||||
return this.http.get<AppListResponse>('apps', {
|
||||
searchParams: {
|
||||
return this.orpc.apps.get({
|
||||
query: {
|
||||
workspace_id: q.workspaceId,
|
||||
page: q.page ?? 1,
|
||||
limit: q.limit ?? 20,
|
||||
@@ -31,9 +33,12 @@ export class AppsClient {
|
||||
}
|
||||
|
||||
async describe(appId: string, workspaceId: string, fields?: readonly string[]): Promise<AppDescribeResponse> {
|
||||
return this.http.get<AppDescribeResponse>(`apps/${encodeURIComponent(appId)}/describe`, {
|
||||
searchParams: {
|
||||
return this.orpc.apps.byAppId.describe.get({
|
||||
params: { app_id: appId },
|
||||
query: {
|
||||
workspace_id: workspaceId,
|
||||
// The backend parses a comma-separated string (validator splits on ','); the contract
|
||||
// types `fields` as a string accordingly, so join here rather than send an array.
|
||||
fields: fields !== undefined && fields.length > 0 ? fields.join(',') : undefined,
|
||||
},
|
||||
})
|
||||
|
||||
+22
-21
@@ -5,40 +5,41 @@ import type {
|
||||
MemberListResponse,
|
||||
MemberRoleUpdatePayload,
|
||||
} from '@dify/contracts/api/openapi/types.gen'
|
||||
import type { OpenApiClient } from '@/http/orpc'
|
||||
import type { HttpClient } from '@/http/types'
|
||||
import { createOpenApiClient } from '@/http/orpc'
|
||||
|
||||
/**
|
||||
* Thin client for /openapi/v1/workspaces/<id>/members.
|
||||
* Thin client for /openapi/v1/workspaces/<id>/members, over the generated oRPC contract.
|
||||
*
|
||||
* Errors are surfaced as BaseError via classifyResponse on non-2xx
|
||||
* (400/403/404/422). The CLI's AuthedCommand base layer maps those to
|
||||
* user-visible messages — clients never swallow status codes here.
|
||||
* Non-2xx (400/403/404/422) surface as BaseError — the oRPC client maps them at the transport
|
||||
* seam. The CLI's AuthedCommand base layer renders those for the user; clients never swallow codes.
|
||||
*/
|
||||
export class MembersClient {
|
||||
private readonly http: HttpClient
|
||||
private readonly orpc: OpenApiClient
|
||||
|
||||
constructor(http: HttpClient) {
|
||||
this.http = http
|
||||
this.orpc = createOpenApiClient(http)
|
||||
}
|
||||
|
||||
async list(workspaceId: string, q?: { page?: number, limit?: number }): Promise<MemberListResponse> {
|
||||
return this.http.get<MemberListResponse>(
|
||||
`workspaces/${encodeURIComponent(workspaceId)}/members`,
|
||||
{ searchParams: { page: q?.page, limit: q?.limit } },
|
||||
)
|
||||
return this.orpc.workspaces.byWorkspaceId.members.get({
|
||||
params: { workspace_id: workspaceId },
|
||||
query: { page: q?.page, limit: q?.limit },
|
||||
})
|
||||
}
|
||||
|
||||
async invite(workspaceId: string, payload: MemberInvitePayload): Promise<MemberInviteResponse> {
|
||||
return this.http.post<MemberInviteResponse>(
|
||||
`workspaces/${encodeURIComponent(workspaceId)}/members`,
|
||||
{ json: payload },
|
||||
)
|
||||
return this.orpc.workspaces.byWorkspaceId.members.post({
|
||||
params: { workspace_id: workspaceId },
|
||||
body: payload,
|
||||
})
|
||||
}
|
||||
|
||||
async remove(workspaceId: string, memberId: string): Promise<MemberActionResponse> {
|
||||
return this.http.delete<MemberActionResponse>(
|
||||
`workspaces/${encodeURIComponent(workspaceId)}/members/${encodeURIComponent(memberId)}`,
|
||||
)
|
||||
return this.orpc.workspaces.byWorkspaceId.members.byMemberId.delete({
|
||||
params: { workspace_id: workspaceId, member_id: memberId },
|
||||
})
|
||||
}
|
||||
|
||||
async updateRole(
|
||||
@@ -46,9 +47,9 @@ export class MembersClient {
|
||||
memberId: string,
|
||||
payload: MemberRoleUpdatePayload,
|
||||
): Promise<MemberActionResponse> {
|
||||
return this.http.put<MemberActionResponse>(
|
||||
`workspaces/${encodeURIComponent(workspaceId)}/members/${encodeURIComponent(memberId)}/role`,
|
||||
{ json: payload },
|
||||
)
|
||||
return this.orpc.workspaces.byWorkspaceId.members.byMemberId.role.put({
|
||||
params: { workspace_id: workspaceId, member_id: memberId },
|
||||
body: payload,
|
||||
})
|
||||
}
|
||||
}
|
||||
+6
-3
@@ -1,19 +1,22 @@
|
||||
import type { ServerVersionResponse } from '@dify/contracts/api/openapi/types.gen'
|
||||
import type { OpenApiClient } from '@/http/orpc'
|
||||
import type { HttpClient } from '@/http/types'
|
||||
import { createOpenApiClient } from '@/http/orpc'
|
||||
|
||||
// Used by every /_version probe call site (the version command and the
|
||||
// per-command auto-nudge). Both must construct their HTTP client with this
|
||||
// timeout + retryAttempts: 0, otherwise the default 30s/3-retry budget kicks in.
|
||||
// The oRPC client below inherits that http instance's policy, so the probe timeout still holds.
|
||||
export const META_PROBE_TIMEOUT_MS = 2000
|
||||
|
||||
export class MetaClient {
|
||||
private readonly http: HttpClient
|
||||
private readonly orpc: OpenApiClient
|
||||
|
||||
constructor(http: HttpClient) {
|
||||
this.http = http
|
||||
this.orpc = createOpenApiClient(http)
|
||||
}
|
||||
|
||||
async serverVersion(): Promise<ServerVersionResponse> {
|
||||
return this.http.get<ServerVersionResponse>('_version')
|
||||
return this.orpc.version.get()
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,10 @@ import { afterEach, describe, expect, it } from 'vitest'
|
||||
import { isHttpClientError } from '@/errors/base'
|
||||
import { WorkspacesClient } from './workspaces.js'
|
||||
|
||||
// WorkspacesClient.switch is covered in members.test.ts; this file covers list().
|
||||
// WorkspacesClient.switch is covered in members.test.ts; this file covers list(), which now
|
||||
// routes through the generated oRPC contract (OpenAPILink over http.request). The happy path
|
||||
// asserts the on-the-wire GET /openapi/v1/workspaces is unchanged; the 401 case asserts the
|
||||
// oRPC client surfaces it as the CLI's classified HttpClientError.
|
||||
|
||||
function makeClient(host: string): WorkspacesClient {
|
||||
return new WorkspacesClient(testHttpClient(host, 'dfoa_test'))
|
||||
|
||||
@@ -1,15 +1,20 @@
|
||||
import type { WorkspaceDetailResponse, WorkspaceListResponse } from '@dify/contracts/api/openapi/types.gen'
|
||||
import type { OpenApiClient } from '@/http/orpc'
|
||||
import type { HttpClient } from '@/http/types'
|
||||
import { createOpenApiClient } from '@/http/orpc'
|
||||
|
||||
export class WorkspacesClient {
|
||||
private readonly http: HttpClient
|
||||
private readonly orpc: OpenApiClient
|
||||
|
||||
constructor(http: HttpClient) {
|
||||
this.http = http
|
||||
// oRPC client over the same transport (UA+bearer / retry / timeout / error-map) — SPEC §4.4:
|
||||
// one transport, a contract facade. Both methods are standard unary JSON, so both go through
|
||||
// the generated contract.
|
||||
this.orpc = createOpenApiClient(http)
|
||||
}
|
||||
|
||||
async list(): Promise<WorkspaceListResponse> {
|
||||
return this.http.get<WorkspaceListResponse>('workspaces')
|
||||
return this.orpc.workspaces.get()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -22,6 +27,6 @@ export class WorkspacesClient {
|
||||
* server's state.
|
||||
*/
|
||||
async switch(workspaceId: string): Promise<WorkspaceDetailResponse> {
|
||||
return this.http.post<WorkspaceDetailResponse>(`workspaces/${encodeURIComponent(workspaceId)}/switch`)
|
||||
return this.orpc.workspaces.byWorkspaceId.switch.post({ params: { workspace_id: workspaceId } })
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user