@@ -3,7 +3,7 @@ import type { BaseProbeResult } from "openclaw/plugin-sdk/channel-contract";
33import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime" ;
44import { resolveFetch } from "openclaw/plugin-sdk/fetch-runtime" ;
55import { readResponseWithLimit } from "openclaw/plugin-sdk/response-limit-runtime" ;
6- import { fetchWithTimeout } from "openclaw/plugin-sdk/text-utility-runtime" ;
6+ import { fetchWithTimeout , runChannelProbe } from "openclaw/plugin-sdk/text-utility-runtime" ;
77import { DiscordApiError , fetchDiscord } from "./api.js" ;
88import { normalizeDiscordToken } from "./token.js" ;
99
@@ -140,72 +140,67 @@ export async function probeDiscord(
140140 timeoutMs : number ,
141141 opts ?: { fetcher ?: typeof fetch ; includeApplication ?: boolean } ,
142142) : Promise < DiscordProbe > {
143- const started = Date . now ( ) ;
144- const fetcher = opts ?. fetcher ?? fetch ;
145- const includeApplication = opts ?. includeApplication === true ;
146- const normalized = normalizeDiscordToken ( token , "channels.discord.token" ) ;
147- const result : DiscordProbe = {
148- ok : false ,
149- status : null ,
150- error : null ,
151- elapsedMs : 0 ,
152- } ;
153- if ( ! normalized ) {
154- return {
155- ...result ,
156- error : "missing token" ,
157- elapsedMs : Date . now ( ) - started ,
158- } ;
159- }
160- let res : Response | undefined ;
161- try {
162- const getMeUrl = `${ DISCORD_API_BASE } /users/@me` ;
163- const getMeDeadlineMs = Date . now ( ) + timeoutMs ;
164- res = await fetchWithTimeout (
165- getMeUrl ,
166- { headers : { Authorization : `Bot ${ normalized } ` } } ,
167- timeoutMs ,
168- getResolvedFetch ( fetcher ) ,
169- ) ;
170- if ( ! res . ok ) {
171- result . status = res . status ;
172- result . error = `getMe failed (${ res . status } )` ;
173- return { ...result , elapsedMs : Date . now ( ) - started } ;
174- }
175- const json = await readDiscordProbeGetMeJson ( res , timeoutMs , getMeDeadlineMs ) ;
176- result . ok = true ;
177- result . bot = {
178- id : json . id ?? null ,
179- username : json . username ?? null ,
180- } ;
181- if ( includeApplication ) {
182- // Application metadata is optional. Keep its deadline inside the outer status budget so a
183- // stalled secondary response cannot discard the already-resolved bot identity.
184- const elapsedMs = Math . max ( 0 , Date . now ( ) - started ) ;
185- const completionReserveMs = Math . min (
186- DISCORD_PROBE_COMPLETION_RESERVE_MAX_MS ,
187- Math . max ( 1 , Math . floor ( timeoutMs / 10 ) ) ,
188- ) ;
189- const applicationTimeoutMs = Math . floor ( timeoutMs - elapsedMs - completionReserveMs ) ;
190- if ( applicationTimeoutMs > 0 ) {
191- result . application =
192- ( await fetchDiscordApplicationSummary ( normalized , applicationTimeoutMs , fetcher ) ) ??
193- undefined ;
143+ return await runChannelProbe (
144+ undefined ,
145+ async ( { startedAt } ) => {
146+ const fetcher = opts ?. fetcher ?? fetch ;
147+ const includeApplication = opts ?. includeApplication === true ;
148+ const normalized = normalizeDiscordToken ( token , "channels.discord.token" ) ;
149+ const result : Omit < DiscordProbe , "elapsedMs" > = {
150+ ok : false ,
151+ status : null ,
152+ error : null ,
153+ } ;
154+ if ( ! normalized ) {
155+ return { ...result , error : "missing token" } ;
194156 }
195- }
196- return { ...result , elapsedMs : Date . now ( ) - started } ;
197- } catch ( err ) {
198- return {
199- ...result ,
200- status : err instanceof Response ? err . status : result . status ,
201- error : formatErrorMessage ( err ) ,
202- elapsedMs : Date . now ( ) - started ,
203- } ;
204- } finally {
205- if ( res ?. bodyUsed !== true ) {
206- await res ?. body ?. cancel ( ) . catch ( ( ) => undefined ) ;
207- }
208- }
157+ let res : Response | undefined ;
158+ try {
159+ const getMeUrl = `${ DISCORD_API_BASE } /users/@me` ;
160+ const getMeDeadlineMs = Date . now ( ) + timeoutMs ;
161+ res = await fetchWithTimeout (
162+ getMeUrl ,
163+ { headers : { Authorization : `Bot ${ normalized } ` } } ,
164+ timeoutMs ,
165+ getResolvedFetch ( fetcher ) ,
166+ ) ;
167+ if ( ! res . ok ) {
168+ return { ...result , status : res . status , error : `getMe failed (${ res . status } )` } ;
169+ }
170+ const json = await readDiscordProbeGetMeJson ( res , timeoutMs , getMeDeadlineMs ) ;
171+ result . ok = true ;
172+ result . bot = {
173+ id : json . id ?? null ,
174+ username : json . username ?? null ,
175+ } ;
176+ if ( includeApplication ) {
177+ // Application metadata is optional. Keep its deadline inside the outer status budget so a
178+ // stalled secondary response cannot discard the already-resolved bot identity.
179+ const elapsedMs = Math . max ( 0 , Date . now ( ) - startedAt ) ;
180+ const completionReserveMs = Math . min (
181+ DISCORD_PROBE_COMPLETION_RESERVE_MAX_MS ,
182+ Math . max ( 1 , Math . floor ( timeoutMs / 10 ) ) ,
183+ ) ;
184+ const applicationTimeoutMs = Math . floor ( timeoutMs - elapsedMs - completionReserveMs ) ;
185+ if ( applicationTimeoutMs > 0 ) {
186+ result . application =
187+ ( await fetchDiscordApplicationSummary ( normalized , applicationTimeoutMs , fetcher ) ) ??
188+ undefined ;
189+ }
190+ }
191+ return result ;
192+ } finally {
193+ if ( res ?. bodyUsed !== true ) {
194+ await res ?. body ?. cancel ( ) . catch ( ( ) => undefined ) ;
195+ }
196+ }
197+ } ,
198+ ( error ) => ( {
199+ ok : false ,
200+ status : error instanceof Response ? error . status : null ,
201+ error : formatErrorMessage ( error ) ,
202+ } ) ,
203+ ) ;
209204}
210205
211206/**
0 commit comments