LibreChat/examples/sdk-runtime/agent.cjs
Danny Avila 2b828ca2c5
Some checks are pending
Backend Unit Tests / Build packages (push) Waiting to run
Backend Unit Tests / Codegraph select (push) Waiting to run
Backend Unit Tests / TypeScript type checks (push) Blocked by required conditions
Backend Unit Tests / Circular dependency checks (push) Waiting to run
Backend Unit Tests / Tests: api (shard 1/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 2/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: api (shard 3/3) (push) Blocked by required conditions
Backend Unit Tests / Tests: data-provider (push) Blocked by required conditions
Backend Unit Tests / Tests: data-schemas (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 1/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 2/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 3/4) (push) Blocked by required conditions
Backend Unit Tests / Tests: @librechat/api (shard 4/4) (push) Blocked by required conditions
Backend Unit Tests / backend (push) Blocked by required conditions
Channel Gateway / gateway (push) Waiting to run
Codegraph E2E Vote Gate / Gate (push) Waiting to run
Codegraph E2E Votes / vote (shard 1/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 10/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 2/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 3/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 4/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 5/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 6/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 7/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 8/10) (push) Waiting to run
Codegraph E2E Votes / vote (shard 9/10) (push) Waiting to run
Docker Dev Branch Images Build / publish (push) Waiting to run
Frontend Unit Tests / Client build recovery regression (push) Waiting to run
Frontend Unit Tests / Codegraph select (push) Waiting to run
Frontend Unit Tests / Build packages (push) Waiting to run
Frontend Unit Tests / TypeScript type checks (client) (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/chat (push) Blocked by required conditions
Frontend Unit Tests / Tests: @librechat/client (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 1/2) (push) Blocked by required conditions
Frontend Unit Tests / Tests: Ubuntu (shard 2/2) (push) Blocked by required conditions
Frontend Unit Tests / Vite build verification (push) Blocked by required conditions
Frontend Unit Tests / frontend (push) Blocked by required conditions
Playwright E2E Tests / Codegraph select (push) Waiting to run
Playwright E2E Tests / Shard timings (push) Waiting to run
Playwright E2E Tests / e2e () (push) Blocked by required conditions
Playwright E2E Tests / MCP list_changed (replica count 1) (push) Blocked by required conditions
Playwright E2E Tests / MCP list_changed (replica count 2) (push) Blocked by required conditions
Playwright E2E Tests / MCP Apps (browser and server) (push) Waiting to run
Playwright E2E Tests / e2e (push) Blocked by required conditions
Playwright Shard Timings / Record shard timings (push) Waiting to run
🛁 feat: Apply Host Provider-Text Protection to Remote SDK Runs (#18240)
* feat: bridge host provider-text policy to remote SDK runs

* build: consume published agents 4.2.7 for remote policy services
2026-11-16 22:37:13 -05:00

138 lines
5.1 KiB
JavaScript

const { setTimeout: delay } = require('node:timers/promises');
const { z } = require('zod');
const { Run, Providers } = require('@librechat/agents');
const { AIMessageChunk, HumanMessage } = require('@librechat/agents/langchain/messages');
const { tool } = require('@librechat/agents/langchain/tools');
const { FakeChatModel } = require('@langchain/core/utils/testing');
const { ChatGenerationChunk } = require('@langchain/core/outputs');
const {
Annotation,
StateGraph,
START,
END,
messagesStateReducer,
} = require('@langchain/langgraph');
/** Real SDK execution; the default deterministic model keeps the example key-free and repeatable. */
async function createExampleRun(input, { complex = true, stepDelayMs = 0 } = {}) {
if (!Number.isSafeInteger(stepDelayMs) || stepDelayMs < 0 || stepDelayMs > 30000)
throw new Error('Invalid example delay');
const inspect = tool(
async ({ topic }) =>
JSON.stringify({ topic, findings: ['Plan prepared', 'Read-only inspection finished'] }),
{
name: 'inspect_example',
description: 'Read-only inspection of the example fixture.',
schema: z.object({ topic: z.string() }),
},
);
const live = process.env.SDK_EXAMPLE_LIVE_MODEL === 'true';
const run = await Run.create({
...input.config,
returnContent: true,
tokenCounter: live ? undefined : () => 1,
graphConfig: {
type: 'standard',
signal: input.signal,
compileOptions: input.compileOptions,
agents: [
{
agentId: input.identity.agentId,
provider: Providers.OPENAI,
...(input.hostTools
? {
tools: [],
graphTools: [inspect],
toolDefinitions: input.hostTools.toolDefinitions,
}
: { tools: [inspect] }),
instructions: `${input.agent.instructions ?? ''}\nInspect the request using inspect_example, then explain the findings.`,
clientOptions: {
model: process.env.SDK_EXAMPLE_MODEL || 'gpt-4.1-mini',
apiKey: process.env.OPENAI_API_KEY,
streaming: live,
},
},
],
},
});
if (!run.Graph) throw new Error('SDK graph was not initialized');
if (!live) {
const model = new FakeChatModel({});
model._generate = async (messages) => ({
generations: [
{
text: '',
message: new AIMessageChunk(
!complex
? { content: 'Hello from the separate SDK runtime.' }
: messages.some((message) => message.getType() === 'tool')
? {
content: messages.some(
(message) =>
message.getType() === 'human' &&
message.content === 'Summarize the reviewed findings concisely.',
)
? 'Custom graph complete: inspected the fixture, reviewed the findings, and prepared the result.'
: 'Inspection complete; reviewing the findings.',
}
: {
content: 'I will inspect the fixture.',
tool_calls: [
{
id: 'example-inspection',
name: 'inspect_example',
args: { topic: 'runtime bridge' },
},
],
},
),
},
],
});
model._streamResponseChunks = async function* (messages) {
const { generations } = await this._generate(messages);
for (const generation of generations) {
yield new ChatGenerationChunk({
...generation,
text: typeof generation.message.content === 'string' ? generation.message.content : '',
});
}
};
run.Graph.overrideModel = model;
}
if (complex) {
// Retain the SDK's compiled agent node: native tools, hooks, events, and approvals stay intact.
const agent = run.graphRunnable;
const State = Annotation.Root({
messages: Annotation({ reducer: messagesStateReducer, default: () => [] }),
reviewed: Annotation({ reducer: (_old, next) => next, default: () => false }),
runStepState: Annotation({ reducer: (_old, next) => next, default: () => undefined }),
});
run.graphRunnable = new StateGraph(State)
.addNode('plan', async () => {
if (stepDelayMs) {
try {
await delay(stepDelayMs, undefined, { signal: input.signal });
} catch (error) {
if (input.signal.aborted) console.log('Example node cancelled');
throw error;
}
}
return { reviewed: false };
})
.addNode('inspect', agent)
.addNode('review', async () => ({ reviewed: true }))
.addNode('refine', async () => ({
messages: [new HumanMessage('Summarize the reviewed findings concisely.')],
}))
.addEdge(START, 'plan')
.addEdge('plan', 'inspect')
.addConditionalEdges('inspect', (state) => (state.reviewed ? END : 'review'))
.addEdge('review', 'refine')
.addEdge('refine', 'inspect')
.compile(input.compileOptions);
}
return run;
}
module.exports = { createExampleRun };