Skip to content

🤯 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.

✨ feat(agent-signal): created new package agent-signal (#14157)

Nekoa6cb200d5b5b
diff --git a/package.json b/package.json
index dfb84bf160a..d0e7c71cdbd 100644
--- a/package.json
+++ b/package.json
@@ -202,6 +202,7 @@
"@lexical/utils": "^0.42.0",
"@lobechat/agent-gateway-client": "workspace:*",
"@lobechat/agent-runtime": "workspace:*",
+ "@lobechat/agent-signal": "workspace:*",
"@lobechat/agent-templates": "workspace:*",
"@lobechat/builtin-agents": "workspace:*",
"@lobechat/builtin-skills": "workspace:*",
diff --git a/packages/agent-signal/package.json b/packages/agent-signal/package.json
new file mode 100644
index 00000000000..3912ca96a9c
--- /dev/null
+++ b/packages/agent-signal/package.json
@@ -0,0 +1,18 @@
+{
+ "name": "@lobechat/agent-signal",
+ "version": "0.1.0",
+ "private": true,
+ "description": "Shared AgentSignal producer helpers and contracts",
+ "type": "module",
+ "exports": {
+ ".": {
+ "types": "./src/index.ts",
+ "default": "./src/index.ts"
+ }
+ },
+ "main": "src/index.ts",
+ "types": "src/index.ts",
+ "scripts": {
+ "test": "vitest"
+ }
+}
diff --git a/packages/agent-signal/src/base/builders.ts b/packages/agent-signal/src/base/builders.ts
new file mode 100644
index 00000000000..a4baaad1322
--- /dev/null
+++ b/packages/agent-signal/src/base/builders.ts
@@ -0,0 +1,123 @@
+import type {
+ AgentSignalChainRef,
+ AgentSignalScope,
+ AgentSignalSource,
+ BaseAction,
+ BaseSignal,
+ SignalRef,
+ SourceRef,
+} from './types';
+
+interface CreateSourceInput {
+ chain?: Partial<AgentSignalChainRef>;
+ payload: Record<string, unknown>;
+ scope: AgentSignalScope;
+ scopeKey: string;
+ sourceId?: string;
+ sourceType: string;
+ timestamp?: number;
+}
+
+interface CreateSignalInput {
+ chain?: Partial<AgentSignalChainRef>;
+ payload: Record<string, unknown>;
+ signalId?: string;
+ signalType: string;
+ source: AgentSignalSource;
+ timestamp?: number;
+}
+
+interface CreateActionInput {
+ actionId?: string;
+ actionType: string;
+ chain?: Partial<AgentSignalChainRef>;
+ output?: Record<string, unknown>;
+ payload: Record<string, unknown>;
+ signal: BaseSignal;
+ timestamp?: number;
+}
+
+let nodeSeed = 0;
+
+const createNodeId = (kind: 'action' | 'signal' | 'source') => {
+ const crypto = globalThis.crypto;
+ if (crypto && typeof crypto.randomUUID === 'function') {
+ return `${kind}:${crypto.randomUUID()}`;
+ }
+
+ nodeSeed += 1;
+ return `${kind}:${Date.now().toString(36)}:${nodeSeed.toString(36)}`;
+};
+
+const buildChainRef = (
+ rootSourceId: string,
+ overrides?: Partial<AgentSignalChainRef>,
+ parentNodeId?: string,
+ parentSignalId?: string,
+ parentActionId?: string,
+): AgentSignalChainRef => {
+ return {
+ ...overrides,
+ chainId: overrides?.chainId ?? `chain:${rootSourceId}`,
+ parentActionId: overrides?.parentActionId ?? parentActionId,
+ parentNodeId: overrides?.parentNodeId ?? parentNodeId,
+ parentSignalId: overrides?.parentSignalId ?? parentSignalId,
+ rootSourceId,
+ };
+};
+
+/** Creates a normalized source node. */
+export const createSource = (input: CreateSourceInput): AgentSignalSource => {
+ const sourceId = input.sourceId ?? createNodeId('source');
+ const timestamp = input.timestamp ?? Date.now();
+
+ return {
+ chain: buildChainRef(sourceId, input.chain),
+ payload: input.payload,
+ scope: input.scope,
+ scopeKey: input.scopeKey,
+ sourceId,
+ sourceType: input.sourceType,
+ timestamp,
+ };
+};
+
+/** Creates a normalized signal node from a source node. */
+export const createSignal = (input: CreateSignalInput): BaseSignal => {
+ const signalId = input.signalId ?? createNodeId('signal');
+ const timestamp = input.timestamp ?? Date.now();
+ const rootSourceId = input.source.chain.rootSourceId;
+
+ return {
+ chain: buildChainRef(rootSourceId, input.chain, input.source.sourceId),
+ payload: input.payload,
+ signalId,
+ signalType: input.signalType,
+ source: {
+ sourceId: input.source.sourceId,
+ sourceType: input.source.sourceType,
+ } satisfies SourceRef,
+ timestamp,
+ };
+};
+
+/** Creates a normalized action node from a signal node. */
+export const createAction = (input: CreateActionInput): BaseAction => {
+ const actionId = input.actionId ?? createNodeId('action');
+ const timestamp = input.timestamp ?? Date.now();
+ const rootSourceId = input.signal.chain.rootSourceId;
+
+ return {
+ actionId,
+ actionType: input.actionType,
+ chain: buildChainRef(rootSourceId, input.chain, input.signal.signalId, input.signal.signalId),
+ output: input.output,
+ payload: input.payload,
+ signal: {
+ signalId: input.signal.signalId,
+ signalType: input.signal.signalType,
+ } satisfies SignalRef,
+ source: input.signal.source,
+ timestamp,
+ };
+};
diff --git a/packages/agent-signal/src/base/guards.ts b/packages/agent-signal/src/base/guards.ts
new file mode 100644
index 00000000000..b5451f499ab
--- /dev/null
+++ b/packages/agent-signal/src/base/guards.ts
@@ -0,0 +1,9 @@
+import type { RuntimeProcessorResult } from './types';
+
+/** Result of a runtime guard evaluation. */
+export interface RuntimeGuardResult {
+ delayMs?: number;
+ ok: boolean;
+ reason: string;
+ wait: () => RuntimeProcessorResult;
+}
diff --git a/packages/agent-signal/src/base/registries.ts b/packages/agent-signal/src/base/registries.ts
new file mode 100644
index 00000000000..8611448dfe6
--- /dev/null
+++ b/packages/agent-signal/src/base/registries.ts
@@ -0,0 +1,99 @@
+import type { AgentSignalSource, BaseAction, BaseSignal } from './types';
+
+/**
+ * Minimal handler contract used by the semantic registries.
+ */
+export interface AgentSignalHandler<TInput = unknown, TResult = unknown> {
+ handle: (input: TInput) => Promise<TResult> | TResult;
+ id: string;
+}
+
+interface RegistryEntry {
+ id: string;
+}
+
+/**
+ * Registry contract for source processors, signal processors, and action handlers.
+ */
+export interface AgentSignalRegistry<TEntry extends RegistryEntry> {
+ list: (type?: string) => TEntry[];
+ match: (type: string) => TEntry[];
+ register: (type: string, entry: TEntry) => this;
+}
+
+/**
+ * Source processor contract.
+ */
+export interface SourceProcessor extends AgentSignalHandler<AgentSignalSource> {}
+
+/**
+ * Signal processor contract.
+ */
+export interface SignalProcessor extends AgentSignalHandler<BaseSignal> {}
+
+/**
+ * Action handler contract.
+ */
+export interface ActionHandler extends AgentSignalHandler<BaseAction> {}
+
+/**
+ * Registry for source processors.
+ */
+export interface SourceProcessorRegistry extends AgentSignalRegistry<SourceProcessor> {}
+
+/**
+ * Registry for signal processors.
+ */
+export interface SignalProcessorRegistry extends AgentSignalRegistry<SignalProcessor> {}
+
+/**
+ * Registry for action handlers.
+ */
+export interface ActionHandlerRegistry extends AgentSignalRegistry<ActionHandler> {}
+
+const createRegistry = <TEntry extends RegistryEntry>(): AgentSignalRegistry<TEntry> => {
+ const entries = new Map<string, TEntry[]>();
+
+ const registry: AgentSignalRegistry<TEntry> = {
+ list(type?: string) {
+ if (type) return [...(entries.get(type) ?? [])];
+
+ const collected: TEntry[] = [];
+ for (const value of entries.values()) {
+ collected.push(...value);
+ }
+
+ return collected;
+ },
+ match(type: string) {
+ return [...(entries.get(type) ?? [])];
+ },
+ register(type: string, entry: TEntry) {
+ const current = entries.get(type);
+ if (current) {
+ current.push(entry);
+ } else {
+ entries.set(type, [entry]);
+ }
+
+ return registry;
+ },
+ };
+
+ return registry;
+};
+
+/** Creates a registry for source processors. */
+export const createSourceProcessorRegistry = (): SourceProcessorRegistry => {
+ return createRegistry<SourceProcessor>();
+};
+
+/** Creates a registry for signal processors. */
+export const createSignalProcessorRegistry = (): SignalProcessorRegistry => {
+ return createRegistry<SignalProcessor>();
+};
+
+/** Creates a registry for action handlers. */
+export const createActionHandlerRegistry = (): ActionHandlerRegistry => {
+ return createRegistry<ActionHandler>();
+};
diff --git a/packages/agent-signal/src/base/types.ts b/packages/agent-signal/src/base/types.ts
new file mode 100644
index 00000000000..74058f79aa1
--- /dev/null
+++ b/packages/agent-signal/src/base/types.ts
@@ -0,0 +1,213 @@
+/** Shared scope metadata for one AgentSignal chain. */
+export interface AgentSignalScope {
+ agentId?: string;
+ botScopeKey?: string;
+ taskId?: string;
+ topicId?: string;
+ userId: string;
+}
+
+/** Causal chain metadata for source, signal, and action nodes. */
+export interface AgentSignalChainRef {
+ chainId?: string;
+ parentActionId?: string;
+ parentNodeId?: string;
+ parentSignalId?: string;
+ rootSourceId: string;
+}
+
+/**
+ * Backward-compatible chain alias used by the current AgentSignal facade.
+ */
+export interface ChainRef extends AgentSignalChainRef {}
+
+/**
+ * Reference to a source node.
+ */
+export interface SourceRef {
+ sourceId: string;
+ sourceType: string;
+}
+
+/**
+ * Reference to a signal node.
+ */
+export interface SignalRef {
+ signalId: string;
+ signalType: string;
+}
+
+/** Base source node for the AgentSignal semantic core. */
+export interface AgentSignalSource {
+ chain: AgentSignalChainRef;
+ payload: Record<string, unknown>;
+ scope?: AgentSignalScope;
+ scopeKey: string;
+ sourceId: string;
+ sourceType: string;
+ timestamp: number;
+}
+
+/**
+ * Backward-compatible source alias used by the current AgentSignal facade.
+ */
+export interface BaseSource extends AgentSignalSource {}
+
+/** Base signal node for the AgentSignal semantic core. */
+export interface AgentSignalSignal {
+ chain: AgentSignalChainRef;
+ payload: Record<string, unknown>;
+ signalId: string;
+ signalType: string;
+ source: SourceRef;
+ timestamp: number;
+}
+
+/**
+ * Backward-compatible signal alias used by older code paths.
+ */
+export interface BaseSignal extends AgentSignalSignal {}
+
+/** Base action node for the AgentSignal semantic core. */
+export interface AgentSignalActionNode {
+ actionId: string;
+ actionType: string;
+ chain: AgentSignalChainRef;
+ output?: Record<string, unknown>;
+ payload: Record<string, unknown>;
+ signal: SignalRef;
+ source: SourceRef;
+ timestamp: number;
+}
+
+/**
+ * Backward-compatible action alias used by older code paths.
+ */
+export interface BaseAction extends AgentSignalActionNode {}
+
+/** One execution attempt for a leaf action. */
+export interface SignalAttempt {
+ completedAt?: number;
+ current: number;
+ max?: number;
+ startedAt: number;
+ status: 'cancelled' | 'failed' | 'running' | 'skipped' | 'succeeded';
+}
+
+/** Agent-backed execution attempt metadata. */
+export interface AgenticAttempt extends SignalAttempt {
+ agentId?: string;
+ model?: string;
+ runId?: string;
+}
+
+/** Structured executor failure. */
+export interface ExecutorError {
+ cause?: unknown;
+ code: string;
+ message: string;
+ retriable?: boolean;
+ retryAfterMs?: number;
+}
+
+/**
+ * Executor outcome with stable status and attempt metadata.
+ */
+export interface ExecutorResultBase {
+ actionId: string;
+ attempt: SignalAttempt | AgenticAttempt;
+ detail?: string;
+ emittedSignalIds?: string[];
+}
+
+/**
+ * Executor outcome for successful and skipped runs.
+ */
+export interface AppliedOrSkippedExecutorResult extends ExecutorResultBase {
+ error?: never;
+ status: 'applied' | 'skipped';
+}
+
+/**
+ * Executor outcome for failed runs.
+ */
+export interface FailedExecutorResult extends ExecutorResultBase {
+ error: ExecutorError;
+ status: 'failed';
+}
+
+/**
+ * Structured executor outcome for the current AgentSignal facade.
+ */
+export type ExecutorResult = AppliedOrSkippedExecutorResult | FailedExecutorResult;
+
+/** Runtime result for a processor that asks the host to wait. */
+export interface RuntimeWaitProcessorResult {
+ pending?: Record<string, unknown>;
+ status: 'wait';
+}
+
+/**
+ * Runtime result for dispatching additional work.
+ */
+export interface RuntimeDispatchProcessorResult {
+ actions?: BaseAction[];
+ signals?: BaseSignal[];
+ status: 'dispatch';
+}
+
+/**
+ * Runtime result for scheduling a future hop.
+ */
+export interface RuntimeScheduleProcessorResult {
+ nextHop: Record<string, unknown>;
+ status: 'schedule';
+}
+
+/**
+ * Runtime result for concluding a chain.
+ */
+export interface RuntimeConcludeProcessorResult {
+ concluded?: Record<string, unknown>;
+ status: 'conclude';
+}
+
+/**
+ * Base runtime processor result used by guards and later runtime hosts.
+ */
+export type RuntimeProcessorResult =
+ | RuntimeConcludeProcessorResult
+ | RuntimeDispatchProcessorResult
+ | RuntimeScheduleProcessorResult
+ | RuntimeWaitProcessorResult;
+
+/**
+ * Trigger metadata attached to a generated source event.
+ */
+export interface SignalTriggerMetadata {
+ scopeKey: string;
+ token: string;
+ windowEventCount: number;
+}
+
+/**
+ * Deduped source-event result.
+ */
+export interface DedupedSourceEventResult {
+ deduped: true;
+ reason: 'duplicate' | 'scope_locked';
+}
+
+/**
+ * Generated source-event result.
+ */
+export interface GeneratedSourceEventResult {
+ deduped: false;
+ source: AgentSignalSource;
+ trigger: SignalTriggerMetadata;
+}
+
+/**
+ * Source-event emission result.
+ */
+export type EmitSourceEventResult = DedupedSourceEventResult | GeneratedSourceEventResult;
diff --git a/packages/agent-signal/src/index.ts b/packages/agent-signal/src/index.ts
new file mode 100644
index 00000000000..46ce0549d36
--- /dev/null
+++ b/packages/agent-signal/src/index.ts
@@ -0,0 +1,13 @@
+export * from './base/builders';
+export * from './base/guards';
+export * from './base/registries';
+export * from './base/types';
+export {
+ type AgentSignalBuiltInSignalPayloadMap,
+ type AgentSignalBuiltInSignalVariant,
+ type SignalActionApplied,
+ type SignalActionFailed,
+ type SignalActionSkipped,
+ type SignalPlan,
+} from './types/builtin';
+export { AGENT_SIGNAL_TYPES, type AgentSignalBuiltInSignalType } from './types/events';
diff --git a/packages/agent-signal/src/types/builtin.ts b/packages/agent-signal/src/types/builtin.ts
new file mode 100644
index 00000000000..d333c6ef378
--- /dev/null
+++ b/packages/agent-signal/src/types/builtin.ts
@@ -0,0 +1,60 @@
+import type { AgenticAttempt, BaseSignal, ExecutorError, SignalAttempt } from '../base/types';
+import type { AGENT_SIGNAL_TYPES, AgentSignalBuiltInSignalType } from './events';
+
+/**
+ * Signal plan returned by one built-in AgentSignal policy.
+ */
+export interface SignalPlan {
+ policyId: string;
+ scopeKey: string;
+ signals: BaseSignal[];
+}
+
+/**
+ * Built-in AgentSignal signal payloads keyed by signal type.
+ */
+export interface AgentSignalBuiltInSignalPayloadMap {
+ [AGENT_SIGNAL_TYPES.actionApplied]: {
+ actionId: string;
+ actionType: string;
+ attempt: SignalAttempt | AgenticAttempt;
+ detail?: string;
+ };
+ [AGENT_SIGNAL_TYPES.actionFailed]: {
+ actionId: string;
+ actionType: string;
+ attempt: SignalAttempt | AgenticAttempt;
+ detail?: string;
+ error: ExecutorError;
+ };
+ [AGENT_SIGNAL_TYPES.actionSkipped]: {
+ actionId: string;
+ actionType: string;
+ attempt: SignalAttempt | AgenticAttempt;
+ detail?: string;
+ };
+}
+
+/**
+ * Built-in AgentSignal signal variant.
+ */
+export type AgentSignalBuiltInSignalVariant<
+ TSignalType extends AgentSignalBuiltInSignalType = AgentSignalBuiltInSignalType,
+> = BaseSignal & {
+ payload: AgentSignalBuiltInSignalPayloadMap[TSignalType];
+ signalType: TSignalType;
+};
+
+/**
+ * Built-in AgentSignal signal union.
+ */
+export type AgentSignalBuiltInSignal = {
+ [TSignalType in AgentSignalBuiltInSignalType]: AgentSignalBuiltInSignalVariant<TSignalType>;
+}[AgentSignalBuiltInSignalType];
+
+/**
+ * Built-in AgentSignal signal aliases retained for downstream compatibility.
+ */
+export type SignalActionApplied = AgentSignalBuiltInSignalVariant<'signal.action.applied'>;
+export type SignalActionSkipped = AgentSignalBuiltInSignalVariant<'signal.action.skipped'>;
+export type SignalActionFailed = AgentSignalBuiltInSignalVariant<'signal.action.failed'>;
diff --git a/packages/agent-signal/src/types/events.ts b/packages/agent-signal/src/types/events.ts
new file mode 100644
index 00000000000..c201a48134d
--- /dev/null
+++ b/packages/agent-signal/src/types/events.ts
@@ -0,0 +1,11 @@
+/** Built-in signal type constants. */
+export const AGENT_SIGNAL_TYPES = {
+ actionApplied: 'signal.action.applied',
+ actionFailed: 'signal.action.failed',
+ actionSkipped: 'signal.action.skipped',
+} as const;
+
+type ValueOf<TValue> = TValue[keyof TValue];
+
+/** Built-in signal type union derived from the signal type constants. */
+export type AgentSignalBuiltInSignalType = ValueOf<typeof AGENT_SIGNAL_TYPES>;