🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
import type {
AgentStreamEvent,
LlmCancelData,
LlmExecuteData,
} from '@lobechat/agent-gateway-client';
import {
buildLlmRelayChannelId,
LLM_RELAY_CHANNEL_HEADER,
LLM_RELAY_CLIENT_ID_HEADER,
} from '@lobechat/agent-gateway-client';
import debug from 'debug';
import { getLlmRelayClientId } from './clientId';
const log = debug('lobe-client:llm-relay:one-shot');
/**
* Client half of the one-shot relay (T-667): an LLM call this tab asks the
* server for — a translation, a topic title, a provider check — whose provider
* only this device can reach. The tab subscribes to a fresh gateway channel,
* names it in the request headers, and executes the `llm_execute` the server
* dispatches on it with its own provider configuration (the same executor as
* relayed agent runs). The browser never calls the provider on its own.
*/
/** What a request must carry so the server relays its LLM calls back here. */
export interface OneShotRelayHandle {
channel: string;
headers: Record<string, string>;
}
/** A subscription to one channel; `ready` resolves once the gateway took it. */
export interface OneShotChannelSubscription {
close: () => void;
ready: Promise<void>;
}
export interface OneShotRelayDeps {
clientId?: () => string;
/**
* Stop what a run relayed on this channel left running here, once the
* channel is released. A sub-agent's calls name the child run, not the
* channel, so its in-flight call would otherwise outlive the wait.
*/
endOperation?: (operationId: string) => void;
/** The deployment can relay: `agent_llm_relay` is on and an Agent Gateway is configured. */
isAvailable: () => boolean;
/** Whether calls to `provider` leave from this device (shared `fetchOnClient` rule). */
isDeviceProvider: (provider: string) => boolean;
/** Run a relayed call this channel delivered. */
onCancel: (data: LlmCancelData) => void;
onExecute: (data: LlmExecuteData) => void;
/** Subscribe to `channel` on the gateway as its executor. */
subscribe: (
channel: string,
onEvent: (event: AgentStreamEvent) => void,
) => Promise<OneShotChannelSubscription>;
userId: () => string | undefined;
/**
* Resolves once the provider runtime state is loaded (bounded); nothing when
* it already is. A custom provider is device-only by settings that arrive
* after the page starts.
*/
whenProvidersKnown?: () => Promise<void> | undefined;
}
/** How long a request waits for its channel subscription before going ahead anyway. */
export const ONE_SHOT_SUBSCRIBE_TIMEOUT_MS = 5000;
const randomNonce = (): string =>
typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function'
? crypto.randomUUID()
: `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 12)}`;
type SubscribeOutcome = 'aborted' | 'failed' | 'ready' | 'timeout';
/**
* Waits for `promise` until `ms` pass (no limit when omitted) or `signal`
* aborts; says which happened.
*/
const withTimeout = (promise: Promise<void>, ms: number | undefined, signal?: AbortSignal) =>
new Promise<SubscribeOutcome>((resolve) => {
if (signal?.aborted) return resolve('aborted');
const timer = ms === undefined ? undefined : setTimeout(() => resolve('timeout'), ms);
signal?.addEventListener(
'abort',
() => {
clearTimeout(timer);
resolve('aborted');
},
{ once: true },
);
promise.then(
() => {
clearTimeout(timer);
resolve('ready');
},
() => {
clearTimeout(timer);
resolve('failed');
},
);
});
export class OneShotRelay {
constructor(private readonly deps: OneShotRelayDeps) {}
/**
* Whether a call to `provider` is relayed back to this tab: the deployment
* relays, the provider only runs from the device, and the user is known (a
* channel is named after them). Callers drop their browser fetcher on this,
* so it must match what `run` actually does.
*/
needsRelay(provider: string | undefined): provider is string {
return (
!!provider &&
this.deps.isAvailable() &&
this.deps.isDeviceProvider(provider) &&
!!this.deps.userId()
);
}
/**
* Run `request` with this tab standing by as the executor of its LLM calls
* to `provider`: `request` gets the headers to send, and the channel is
* released once it settles (`signal`: an aborted caller stops waiting for
* the subscription). A provider the server reaches itself runs
* `request` as-is, without a channel. A list stands by when any of them
* needs this tab — for a request whose provider only the server resolves;
* pass it as a function to read it once the provider runtime state is known.
*/
async run<T>(
provider: string | string[] | (() => string[]) | undefined,
request: (relay?: OneShotRelayHandle) => Promise<T>,
{
scope,
signal,
}: {
/** Bind the channel to a scope (`llmRelayChannelScope`), for a server that checks it. */
scope?: string;
signal?: AbortSignal;
} = {},
): Promise<T> {
const providersLoading =
provider && this.deps.isAvailable() ? this.deps.whenProvidersKnown?.() : undefined;
if (providersLoading) {
// Bounded by the dependency; an aborted caller stops waiting at once and
// its request settles without a channel.
await withTimeout(providersLoading, undefined, signal);
if (signal?.aborted) return request();
}
const resolved = typeof provider === 'function' ? provider() : provider;
const providers = Array.isArray(resolved) ? resolved : [resolved];
if (!providers.some((item) => this.needsRelay(item))) return request();
const channel = buildLlmRelayChannelId(
this.deps.userId()!,
scope ? `${scope}-${randomNonce().replaceAll('-', '')}` : randomNonce(),
);
const operationIds = new Set<string>();
const subscribing = this.deps.subscribe(channel, (event) => {
if (event.type === 'llm_execute') {
const data = event.data as LlmExecuteData;
if (data.operationId) operationIds.add(data.operationId);
this.deps.onExecute(data);
} else if (event.type === 'llm_cancel') this.deps.onCancel(event.data as LlmCancelData);
});
try {
// The server dispatches as soon as the request lands: be subscribed
// first, or the gateway finds nobody to deliver the call to. The setup
// (a gateway token request on the v1 path) counts against the same
// timeout; past it, or if it fails, the request goes ahead and the
// server reports the relay failure.
const outcome = await withTimeout(
subscribing.then((subscription) => subscription.ready),
ONE_SHOT_SUBSCRIBE_TIMEOUT_MS,
// An aborted caller has nothing to wait for: its request settles at once.
signal,
);
log('channel %s for %o: subscription %s', channel, resolved, outcome);
return await request({
channel,
headers: {
[LLM_RELAY_CHANNEL_HEADER]: channel,
[LLM_RELAY_CLIENT_ID_HEADER]: (this.deps.clientId ?? getLlmRelayClientId)(),
},
});
} finally {
for (const operationId of operationIds) this.deps.endOperation?.(operationId);
// A subscription that lands after the request settled is released too.
subscribing.then(
(subscription) => subscription.close(),
(error) => log('channel %s subscription failed: %O', channel, error),
);
}
}
}