🤯 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
🔨 chore: add agent-runtime (#9206)
Arvin Xud942a635b36a
diff --git a/.cursor/rules/debug-usage.mdc b/.cursor/rules/debug-usage.mdcindex 44947a58c53..76a1197c362 100644--- a/.cursor/rules/debug-usage.mdc+++ b/.cursor/rules/debug-usage.mdc@@ -1,6 +1,6 @@----description: 包含添加 debug 日志请求时-globs:+description: 包含添加 console.log 日志请求时+globs:alwaysApply: false---# Debug 包使用指南diff --git a/.github/workflows/test.yml b/.github/workflows/test.ymlindex 33ca7b5562e..b9f020025ce 100644--- a/.github/workflows/test.yml+++ b/.github/workflows/test.yml@@ -19,6 +19,7 @@ jobs:- electron-server-ipc- utils- context-engine+ - agent-runtimename: Test package ${{ matrix.package }}diff --git a/package.json b/package.jsonindex 750ceaa16ec..4ee1c5d39ab 100644--- a/package.json+++ b/package.json@@ -143,6 +143,7 @@"@icons-pack/react-simple-icons": "9.6.0","@khmyznikov/pwa-install": "0.3.9","@langchain/community": "^0.3.55",+ "@lobechat/agent-runtime": "workspace:*","@lobechat/const": "workspace:*","@lobechat/context-engine": "workspace:*","@lobechat/database": "workspace:*",diff --git a/packages/agent-runtime/examples/tools-calling.ts b/packages/agent-runtime/examples/tools-calling.tsnew file mode 100644index 00000000000..73d70ab029d--- /dev/null+++ b/packages/agent-runtime/examples/tools-calling.ts@@ -0,0 +1,303 @@+// @ts-nocheck+import OpenAI from 'openai';++import { AgentRuntime } from '../src';+import type { Agent, AgentState, RuntimeContext } from '../src';++// OpenAI 模型运行时+async function* openaiRuntime(payload: any) {+ const openai = new OpenAI({+ apiKey: process.env.OPENAI_API_KEY || '',+ });++ const { messages, tools } = payload;++ const stream = await openai.chat.completions.create({+ messages,+ model: 'gpt-4.1-mini',+ stream: true,+ tools,+ });++ let content = '';+ let toolCalls: any[] = [];++ for await (const chunk of stream) {+ const delta = chunk.choices[0]?.delta;++ if (delta?.content) {+ content += delta.content;+ yield { content: delta.content };+ }++ if (delta?.tool_calls) {+ for (const toolCall of delta.tool_calls) {+ if (!toolCalls[toolCall.index]) {+ toolCalls[toolCall.index] = {+ function: { arguments: '', name: '' },+ id: toolCall.id,+ type: 'function',+ };+ }+ if (toolCall.function?.name) {+ toolCalls[toolCall.index].function.name += toolCall.function.name;+ }+ if (toolCall.function?.arguments) {+ toolCalls[toolCall.index].function.arguments += toolCall.function.arguments;+ }+ }+ }+ }++ if (toolCalls.length > 0) {+ yield { tool_calls: toolCalls.filter(Boolean) };+ }+}++// 简单的 Agent 实现+class SimpleAgent implements Agent {+ private conversationState: 'waiting_user' | 'processing_llm' | 'executing_tools' | 'done' =+ 'waiting_user';+ private pendingToolCalls: any[] = [];++ // Agent 拥有自己的模型运行时+ modelRuntime = openaiRuntime;++ // 定义可用工具+ tools = {+ calculate: async ({ expression }: { expression: string }) => {+ try {+ // 注意:实际应用中应使用安全的数学解析器+ const result = new Function(`"use strict"; return (${expression})`)();+ return { expression, result };+ } catch {+ return { error: 'Invalid expression', expression };+ }+ },++ get_time: async () => {+ return {+ current_time: new Date().toISOString(),+ formatted_time: new Date().toLocaleString(),+ };+ },+ };++ // 获取工具定义+ private getToolDefinitions() {+ return [+ {+ function: {+ description: 'Get current date and time',+ name: 'get_time',+ parameters: { properties: {}, type: 'object' },+ },+ type: 'function' as const,+ },+ {+ function: {+ description: 'Calculate mathematical expressions',+ name: 'calculate',+ parameters: {+ properties: {+ expression: { description: 'Math expression', type: 'string' },+ },+ required: ['expression'],+ type: 'object',+ },+ },+ type: 'function' as const,+ },+ ];+ }++ // Agent 决策逻辑 - 基于执行阶段和上下文+ async runner(context: RuntimeContext, state: AgentState) {+ console.log(`[${context.phase}] 对话状态: ${this.conversationState}`);++ switch (context.phase) {+ case 'init': {+ // 初始化阶段+ this.conversationState = 'waiting_user';+ return { reason: 'No action needed', type: 'finish' as const };+ }++ case 'user_input': {+ // 用户输入阶段+ const userPayload = context.payload as { isFirstMessage: boolean; message: any };+ console.log(`👤 用户消息: ${userPayload.message.content}`);++ // 只有在等待用户输入状态时才处理+ if (this.conversationState === 'waiting_user') {+ this.conversationState = 'processing_llm';+ return {+ payload: {+ messages: state.messages,+ tools: this.getToolDefinitions(),+ },+ type: 'call_llm' as const,+ };+ }++ // 其他状态下不处理用户输入,结束对话+ console.log(`⚠️ 忽略用户输入,当前状态: ${this.conversationState}`);+ return {+ reason: `Not in waiting_user state: ${this.conversationState}`,+ type: 'finish' as const,+ };+ }++ case 'llm_result': {+ // LLM 结果阶段,检查是否需要工具调用+ const llmPayload = context.payload as { hasToolCalls: boolean; result: any };++ // 手动添加 assistant 消息到状态中(修复 Runtime 的问题)+ const assistantMessage: any = {+ content: llmPayload.result.content || null,+ role: 'assistant',+ };++ if (llmPayload.hasToolCalls) {+ const toolCalls = llmPayload.result.tool_calls;+ assistantMessage.tool_calls = toolCalls;+ this.pendingToolCalls = toolCalls;+ this.conversationState = 'executing_tools';++ console.log(+ '🔧 需要执行工具:',+ toolCalls.map((call: any) => call.function.name),+ );++ // 添加包含 tool_calls 的 assistant 消息+ state.messages.push(assistantMessage);++ // 执行第一个工具调用+ return {+ toolCall: toolCalls[0],+ type: 'call_tool' as const,+ };+ }++ // 没有工具调用,添加普通 assistant 消息+ state.messages.push(assistantMessage);+ this.conversationState = 'done';+ return { reason: 'LLM response completed', type: 'finish' as const };+ }++ case 'tool_result': {+ // 工具执行结果阶段+ const toolPayload = context.payload as { result: any; toolMessage: any };+ console.log(`🛠️ 工具执行完成: ${JSON.stringify(toolPayload.result)}`);++ // 移除已执行的工具+ this.pendingToolCalls = this.pendingToolCalls.slice(1);++ // 如果还有未执行的工具,继续执行+ if (this.pendingToolCalls.length > 0) {+ return {+ toolCall: this.pendingToolCalls[0],+ type: 'call_tool' as const,+ };+ }++ // 所有工具执行完成,调用 LLM 处理结果+ this.conversationState = 'processing_llm';+ return {+ payload: {+ messages: state.messages,+ tools: this.getToolDefinitions(),+ },+ type: 'call_llm' as const,+ };+ }++ case 'human_response': {+ // 人机交互响应阶段(简化示例中不使用)+ return { reason: 'Human interaction not supported', type: 'finish' as const };+ }++ case 'error': {+ // 错误阶段+ const errorPayload = context.payload as { error: any };+ console.error('❌ 错误状态:', errorPayload.error);+ return { reason: 'Error occurred', type: 'finish' as const };+ }++ default: {+ return { reason: 'Unknown phase', type: 'finish' as const };+ }+ }+ }+}++// 主函数+async function main() {+ console.log('🚀 简单的 OpenAI Tools Agent 示例\n');++ if (!process.env.OPENAI_API_KEY) {+ console.error('❌ 请设置 OPENAI_API_KEY 环境变量');+ return;+ }++ // 创建 Agent 和 Runtime+ const agent = new SimpleAgent();+ const runtime = new AgentRuntime(agent); // modelRuntime 现在在 Agent 中++ // 测试消息+ const testMessage = process.argv[2] || 'What time is it? Also calculate 15 * 8 + 7';+ console.log(`💬 用户: ${testMessage}\n`);++ // 创建初始状态+ let state = AgentRuntime.createInitialState({+ maxSteps: 10,+ messages: [{ content: testMessage, role: 'user' }],+ sessionId: 'simple-test',+ });++ console.log('🤖 AI: ');++ // 执行对话循环+ let nextContext: RuntimeContext | undefined = undefined;++ while (state.status !== 'done' && state.status !== 'error') {+ const result = await runtime.step(state, nextContext);++ // 处理事件+ for (const event of result.events) {+ switch (event.type) {+ case 'llm_stream': {+ if ((event as any).chunk.content) {+ process.stdout.write((event as any).chunk.content);+ }+ break;+ }+ case 'llm_result': {+ if ((event as any).result.tool_calls) {+ console.log('\n\n🔧 需要调用工具...');+ }+ break;+ }+ case 'tool_result': {+ console.log(`\n🛠️ 工具执行结果:`, event.result);+ console.log('\n🤖 AI: ');+ break;+ }+ case 'done': {+ console.log('\n\n✅ 对话完成');+ break;+ }+ case 'error': {+ console.error('\n❌ 错误:', event.error);+ break;+ }+ }+ }++ state = result.newState;+ nextContext = result.nextContext; // 使用返回的 nextContext+ }++ console.log(`\n📊 总共执行了 ${state.stepCount} 个步骤`);+}++main().catch(console.error);diff --git a/packages/agent-runtime/package.json b/packages/agent-runtime/package.jsonnew file mode 100644index 00000000000..0155df0e47a--- /dev/null+++ b/packages/agent-runtime/package.json@@ -0,0 +1,15 @@+{+ "name": "@lobechat/agent-runtime",+ "version": "1.0.0",+ "private": true,+ "main": "./src/index.ts",+ "scripts": {+ "simple": "tsx examples/tools-calling.ts",+ "test": "vitest",+ "test:coverage": "vitest --coverage"+ },+ "dependencies": {},+ "devDependencies": {+ "openai": "^4.0.0"+ }+}diff --git a/packages/agent-runtime/src/core/__tests__/runtime.test.ts b/packages/agent-runtime/src/core/__tests__/runtime.test.tsnew file mode 100644index 00000000000..db95a0925b2--- /dev/null+++ b/packages/agent-runtime/src/core/__tests__/runtime.test.ts@@ -0,0 +1,1046 @@+import { describe, expect, it, vi } from 'vitest';++import {+ Agent,+ AgentEventError,+ AgentState,+ Cost,+ CostCalculationContext,+ CostLimit,+ RuntimeConfig,+ RuntimeContext,+ ToolsCalling,+ Usage,+} from '../../types';+import { AgentRuntime } from '../runtime';++// Mock Agent for testing+class MockAgent implements Agent {+ tools = {};+ executors = {};+ modelRuntime?: (payload: unknown) => AsyncIterable<any>;++ async runner(context: RuntimeContext, state: AgentState) {+ switch (context.phase) {+ case 'user_input':+ return { type: 'call_llm' as const, payload: { messages: state.messages } };+ case 'llm_result':+ const llmPayload = context.payload as { result: any; hasToolCalls: boolean };+ if (llmPayload.hasToolCalls) {+ return {+ type: 'request_human_approve' as const,+ pendingToolsCalling: llmPayload.result.tool_calls,+ };+ }+ return { type: 'finish' as const, reason: 'completed' as const, reasonDetail: 'Done' };+ case 'tool_result':+ return { type: 'call_llm' as const, payload: { messages: state.messages } };+ default:+ return { type: 'finish' as const, reason: 'completed' as const, reasonDetail: 'Done' };+ }+ }+}++// Helper function to create test context+function createTestContext(+ phase: RuntimeContext['phase'],+ payload?: any,+ sessionId: string = 'test-session',+): RuntimeContext {+ return {+ phase,+ payload,+ session: {+ sessionId,+ messageCount: 1,+ eventCount: 0,+ status: 'idle',+ stepCount: 0,+ },+ };+}++describe('AgentRuntime', () => {+ describe('Constructor and Executor Priority', () => {+ it('should use built-in executors by default', () => {+ const agent = new MockAgent();+ const runtime = new AgentRuntime(agent);++ // @ts-expect-error - accessing private property for testing+ const executors = runtime.executors;++ expect(executors).toHaveProperty('call_llm');+ expect(executors).toHaveProperty('call_tool');+ expect(executors).toHaveProperty('finish');+ expect(executors).toHaveProperty('request_human_approve');+ });++ it('should allow config executors to override built-in ones', () => {+ const agent = new MockAgent();+ const customFinish = vi.fn();++ const config: RuntimeConfig = {+ executors: {+ finish: customFinish,+ },+ };++ const runtime = new AgentRuntime(agent, config);++ // @ts-ignore+ expect(runtime.executors.finish).toBe(customFinish);+ });++ it('should give agent executors highest priority', () => {+ const agent = new MockAgent();+ const agentFinish = vi.fn();+ const configFinish = vi.fn();++ agent.executors = { finish: agentFinish };+ const config: RuntimeConfig = {+ executors: { finish: configFinish },+ };++ const runtime = new AgentRuntime(agent, config);++ // @ts-ignore+ expect(runtime.executors.finish).toBe(agentFinish);+ });+ });++ describe('step method', () => {+ it('should execute approved tool call directly', async () => {+ const agent = new MockAgent();+ agent.tools = {+ test_tool: vi.fn().mockResolvedValue({ result: 'success' }),+ };++ const runtime = new AgentRuntime(agent);+ const state = AgentRuntime.createInitialState({ sessionId: 'test-session' });++ const toolCall: ToolsCalling = {+ id: 'call_123',+ type: 'function',+ function: {+ name: 'test_tool',+ arguments: '{"input": "test"}',+ },+ };++ const result = await runtime.approveToolCall(state, toolCall);++ expect(result.events).toHaveLength(1);+ expect(result.events[0]).toMatchObject({+ type: 'tool_result',+ id: 'call_123',+ result: { result: 'success' },+ });+ expect(result.newState.messages).toHaveLength(1);+ expect(result.newState.messages[0].role).toBe('tool');+ });++ it('should follow agent runner -> executor flow', async () => {+ const agent = new MockAgent();+ const runtime = new AgentRuntime(agent);+ const state = AgentRuntime.createInitialState({+ sessionId: 'test-session',+ messages: [{ role: 'user', content: 'Hello' }],+ });++ const result = await runtime.step(state);++ // Should call agent runner, get call_llm instruction, but fail due to no llmProvider+ expect(result.events).toHaveLength(1);+ expect(result.events[0].type).toBe('error');+ expect(result.newState.status).toBe('error');+ });++ it('should handle errors gracefully', async () => {+ const agent = new MockAgent();+ agent.runner = vi.fn().mockImplementation(() => Promise.reject(new Error('Agent error')));++ const runtime = new AgentRuntime(agent);+ const state = AgentRuntime.createInitialState({ sessionId: 'test-session' });++ const result = await runtime.step(state);++ expect(result.events).toHaveLength(1);+ expect(result.events[0]).toMatchObject({+ type: 'error',+ error: expect.any(Error),+ });+ expect(result.newState.status).toBe('error');+ expect(result.newState.error).toBeInstanceOf(Error);+ });+ });++ describe('Built-in Executors', () => {+ describe('call_llm executor', () => {+ it('should require modelRuntime', async () => {+ const agent = new MockAgent();+ const runtime = new AgentRuntime(agent);+ const state = AgentRuntime.createInitialState({+ sessionId: 'test-session',+ messages: [{ role: 'user', content: 'Hello' }],+ });++ const result = await runtime.step(state);++ expect(result.events[0].type).toBe('error');+ expect((result.events[0] as AgentEventError).error.message).toContain(+ 'Model Runtime is required',+ );+ });++ it('should handle streaming LLM response', async () => {+ const agent = new MockAgent();++ async function* mockModelRuntime(payload: unknown) {+ yield { content: 'Hello' };+ yield { content: ' world' };+ yield { content: '!' };+ }++ agent.modelRuntime = mockModelRuntime;++ const runtime = new AgentRuntime(agent);+ const state = AgentRuntime.createInitialState({+ sessionId: 'test-session',+ messages: [{ role: 'user', content: 'Hello' }],+ });++ const result = await runtime.step(state);++ expect(result.events).toHaveLength(5); // start + 3 streams + result+ expect(result.events[0]).toMatchObject({+ type: 'llm_start',+ payload: expect.anything(),+ });++ expect(result.events[1]).toMatchObject({+ type: 'llm_stream',+ chunk: { content: 'Hello' },+ });++ expect(result.events[4]).toMatchObject({+ type: 'llm_result',+ result: { content: 'Hello world!', tool_calls: [] },+ });++ // In the new architecture, call_llm executor doesn't add messages to state+ // It only returns events, messages should be handled by higher-level logic+ expect(result.newState.messages).toHaveLength(1); // Only user message+ expect(result.newState.status).toBe('running');+ });++ it('should handle LLM response with tool calls', async () => {+ const agent = new MockAgent();++ async function* mockModelRuntime(payload: unknown) {+ yield { content: 'I need to use a tool' };+ yield {+ tool_calls: [+ {+ id: 'call_123',+ type: 'function' as const,+ function: { name: 'test_tool', arguments: '{}' },+ },+ ],+ };+ }++ agent.modelRuntime = mockModelRuntime;++ const runtime = new AgentRuntime(agent);+ const state = AgentRuntime.createInitialState({+ sessionId: 'test-session',+ messages: [{ role: 'user', content: 'Hello' }],+ });++ const result = await runtime.step(state);++ // In the new architecture, call_llm executor doesn't add messages to state+ // Check that the events contain the expected LLM result+ expect(result.events).toContainEqual(+ expect.objectContaining({+ type: 'llm_result',+ result: expect.objectContaining({+ content: 'I need to use a tool',+ tool_calls: [+ {+ id: 'call_123',+ type: 'function',+ function: { name: 'test_tool', arguments: '{}' },+ },+ ],+ }),+ }),+ );+ });+ });++ describe('call_tool executor', () => {+ it('should execute tool and add result to messages', async () => {+ const agent = new MockAgent();+ agent.tools = {+ calculator: vi.fn().mockResolvedValue({ result: 42 }),+ };++ const runtime = new AgentRuntime(agent);+ const state = AgentRuntime.createInitialState({ sessionId: 'test-session' });++ const toolCall: ToolsCalling = {+ id: 'call_123',+ type: 'function',+ function: {+ name: 'calculator',+ arguments: '{"expression": "2+2"}',+ },+ };++ const result = await runtime.approveToolCall(state, toolCall);++ expect((agent.tools as any).calculator).toHaveBeenCalledWith({ expression: '2+2' });+ expect(result.events).toHaveLength(1);+ expect(result.events[0]).toMatchObject({+ type: 'tool_result',+ id: 'call_123',+ result: { result: 42 },+ });++ expect(result.newState.messages).toHaveLength(1);+ expect(result.newState.messages[0]).toMatchObject({+ role: 'tool',+ tool_call_id: 'call_123',+ content: '{"result":42}',+ });+ });++ it('should throw error for unknown tool', async () => {+ const agent = new MockAgent();+ const runtime = new AgentRuntime(agent);+ const state = AgentRuntime.createInitialState({ sessionId: 'test-session' });++ const toolCall: ToolsCalling = {+ id: 'call_123',+ type: 'function',+ function: {+ name: 'unknown_tool',+ arguments: '{}',+ },+ };++ const result = await runtime.approveToolCall(state, toolCall);++ expect(result.events[0].type).toBe('error');+ expect((result.events[0] as AgentEventError).error.message).toContain(+ 'Tool not found: unknown_tool',+ );+ });+ });++ describe('human interaction executors', () => {+ it('should handle human approve request', async () => {+ const agent = new MockAgent();+ // Mock agent to return human approve instruction+ agent.runner = vi.fn().mockImplementation(() =>+ Promise.resolve({+ type: 'request_human_approve',+ pendingToolsCalling: [+ {+ id: 'call_123',+ type: 'function',+ function: { name: 'test_tool', arguments: '{}' },+ },+ ],+ }),+ );++ const runtime = new AgentRuntime(agent);+ const state = AgentRuntime.createInitialState({ sessionId: 'test-session' });++ const result = await runtime.step(state);++ expect(result.events).toHaveLength(2);+ expect(result.events[0]).toMatchObject({+ type: 'human_approve_required',+ sessionId: 'test-session',+ });+ expect(result.events[1]).toMatchObject({+ type: 'tool_pending',+ });++ expect(result.newState.status).toBe('waiting_for_human_input');+ expect(result.newState.pendingToolsCalling).toBeDefined();+ });++ it('should handle human prompt request', async () => {+ const agent = new MockAgent();+ agent.runner = vi.fn().mockImplementation(() =>+ Promise.resolve({+ type: 'request_human_prompt',+ prompt: 'Please provide input',+ metadata: { key: 'value' },+ }),+ );++ const runtime = new AgentRuntime(agent);+ const state = AgentRuntime.createInitialState({ sessionId: 'test-session' });++ const result = await runtime.step(state);++ expect(result.events).toHaveLength(1);+ expect(result.events[0]).toMatchObject({+ type: 'human_prompt_required',+ prompt: 'Please provide input',+ metadata: { key: 'value' },+ sessionId: 'test-session',+ });++ expect(result.newState.status).toBe('waiting_for_human_input');+ expect(result.newState.pendingHumanPrompt).toEqual({+ prompt: 'Please provide input',+ metadata: { key: 'value' },+ });+ });++ it('should handle human select request', async () => {+ const agent = new MockAgent();+ agent.runner = vi.fn().mockImplementation(() =>+ Promise.resolve({+ type: 'request_human_select',+ prompt: 'Choose an option',+ options: [+ { label: 'Option 1', value: 'opt1' },+ { label: 'Option 2', value: 'opt2' },+ ],+ multi: false,+ }),+ );++ const runtime = new AgentRuntime(agent);+ const state = AgentRuntime.createInitialState({ sessionId: 'test-session' });++ const result = await runtime.step(state);++ expect(result.events).toHaveLength(1);+ expect(result.events[0]).toMatchObject({+ type: 'human_select_required',+ prompt: 'Choose an option',+ options: [+ { label: 'Option 1', value: 'opt1' },+ { label: 'Option 2', value: 'opt2' },+ ],+ multi: false,+ sessionId: 'test-session',+ });++ expect(result.newState.status).toBe('waiting_for_human_input');+ });+ });++ describe('finish executor', () => {+ it('should mark conversation as done', async () => {+ const agent = new MockAgent();+ agent.runner = vi.fn().mockImplementation(() =>+ Promise.resolve({+ type: 'finish',+ reason: 'completed',+ reasonDetail: 'Task completed',+ }),+ );++ const runtime = new AgentRuntime(agent);+ const state = AgentRuntime.createInitialState({ sessionId: 'test-session' });++ const result = await runtime.step(state);++ expect(result.events).toHaveLength(1);+ expect(result.events[0]).toMatchObject({+ type: 'done',+ finalState: expect.objectContaining({+ status: 'done',+ }),+ reason: 'completed',+ reasonDetail: 'Task completed',+ });++ expect(result.newState.status).toBe('done');+ });+ });+ });++ describe('createInitialState', () => {+ it('should create initial state without message', () => {+ const state = AgentRuntime.createInitialState({ sessionId: 'test-session' });++ expect(state).toMatchObject({+ sessionId: 'test-session',+ status: 'idle',+ messages: [],+ stepCount: 0,+ createdAt: expect.any(String),+ lastModified: expect.any(String),+ });+ });++ it('should create initial state with message', () => {+ const state = AgentRuntime.createInitialState({+ sessionId: 'test-session',+ messages: [{ role: 'user', content: 'Hello world' }],+ });++ expect(state.messages).toHaveLength(1);+ expect(state.messages[0]).toMatchObject({+ role: 'user',+ content: 'Hello world',+ });+ expect(state.stepCount).toBe(0);+ });++ it('should create initial state with custom stepCount', () => {+ const state = AgentRuntime.createInitialState({+ sessionId: 'test-session',+ stepCount: 5,+ });++ expect(state.stepCount).toBe(5);+ });++ it('should create initial state with maxSteps limit', () => {+ const state = AgentRuntime.createInitialState({+ sessionId: 'test-session',+ maxSteps: 10,+ });++ expect(state.maxSteps).toBe(10);+ expect(state.stepCount).toBe(0);+ });+ });++ describe('Step Count Tracking', () => {+ it('should increment stepCount on each step execution', async () => {+ const agent = new MockAgent();+ const runtime = new AgentRuntime(agent);++ let state = AgentRuntime.createInitialState({ sessionId: 'test-session' });+ expect(state.stepCount).toBe(0);++ // First step+ const result1 = await runtime.step(state, createTestContext('user_input'));+ expect(result1.newState.stepCount).toBe(1);++ // Second step+ const result2 = await runtime.step(result1.newState, createTestContext('user_input'));+ expect(result2.newState.stepCount).toBe(2);+ });++ it('should respect maxSteps limit', async () => {+ const agent = new MockAgent();+ // Add a mock modelRuntime to avoid LLM provider error+ agent.modelRuntime = async function* () {+ yield { content: 'test response' };+ };+ const runtime = new AgentRuntime(agent);++ const state = AgentRuntime.createInitialState({+ sessionId: 'test-session',+ maxSteps: 3, // 允许 3 步+ });++ // First step - should work+ const result1 = await runtime.step(state, createTestContext('user_input'));+ expect(result1.newState.stepCount).toBe(1);+ expect(result1.newState.status).not.toBe('error');++ // Second step - should work+ const result2 = await runtime.step(result1.newState, createTestContext('user_input'));+ expect(result2.newState.stepCount).toBe(2);+ expect(result2.newState.status).not.toBe('error');++ // Third step - should work (at limit)+ const result3 = await runtime.step(result2.newState, createTestContext('user_input'));+ expect(result3.newState.stepCount).toBe(3);+ expect(result3.newState.status).not.toBe('error');++ // Fourth step - should finish due to maxSteps+ const result4 = await runtime.step(result3.newState, createTestContext('user_input'));+ expect(result4.newState.stepCount).toBe(4);+ expect(result4.newState.status).toBe('done');+ expect(result4.events[0]).toMatchObject({+ type: 'done',+ finalState: expect.objectContaining({+ status: 'done',+ }),+ reason: 'max_steps_exceeded',+ reasonDetail: 'Maximum steps exceeded: 3',+ });+ });++ it('should include stepCount in session context', async () => {+ const agent = new MockAgent();+ // Mock agent to check the context it receives+ const runnerSpy = vi.spyOn(agent, 'runner');++ const runtime = new AgentRuntime(agent);+ const state = AgentRuntime.createInitialState({+ sessionId: 'test-session',+ stepCount: 5, // Start with step 5+ messages: [{ role: 'user', content: 'test' }],+ });++ // Don't provide context, let runtime create it with updated stepCount+ await runtime.step(state);++ // Check that agent received correct stepCount in context+ expect(runnerSpy).toHaveBeenCalledWith(+ expect.objectContaining({+ session: expect.objectContaining({+ stepCount: 6, // Should be incremented+ }),+ }),+ expect.any(Object),+ );+ });+ });++ describe('Interruption Handling', () => {+ it('should interrupt execution with reason and metadata', () => {+ const agent = new MockAgent();+ const runtime = new AgentRuntime(agent);++ const state = AgentRuntime.createInitialState({+ sessionId: 'test-session',+ stepCount: 3,+ });++ const result = runtime.interrupt(state, 'User requested stop', true, {+ userAction: 'stop_button',+ });++ expect(result.newState.status).toBe('interrupted');+ expect(result.newState.interruption).toMatchObject({+ reason: 'User requested stop',+ canResume: true,+ interruptedAt: expect.any(String),+ });++ expect(result.events[0]).toMatchObject({+ type: 'interrupted',+ reason: 'User requested stop',+ canResume: true,+ metadata: { userAction: 'stop_button' },+ interruptedAt: expect.any(String),+ });+ });++ it('should resume from interrupted state', async () => {+ const agent = new MockAgent();+ agent.modelRuntime = async function* () {+ yield { content: 'resumed response' };+ };+ const runtime = new AgentRuntime(agent);++ // Create interrupted state+ let state = AgentRuntime.createInitialState({ sessionId: 'test-session' });+ const interruptResult = runtime.interrupt(state, 'Test interruption');++ // Resume execution+ const resumeResult = await runtime.resume(interruptResult.newState, 'Test resume');++ expect(resumeResult.newState.status).toBe('running');+ expect(resumeResult.newState.interruption).toBeUndefined();++ expect(resumeResult.events[0]).toMatchObject({+ type: 'resumed',+ reason: 'Test resume',+ resumedFromStep: 0,+ resumedAt: expect.any(String),+ });+ });++ it('should not allow resume if canResume is false', async () => {+ const agent = new MockAgent();+ const runtime = new AgentRuntime(agent);++ let state = AgentRuntime.createInitialState({ sessionId: 'test-session' });+ const interruptResult = runtime.interrupt(state, 'Fatal error', false);++ await expect(runtime.resume(interruptResult.newState)).rejects.toThrow(+ 'Cannot resume: interruption is not resumable',+ );+ });++ it('should not allow resume from non-interrupted state', async () => {+ const agent = new MockAgent();+ const runtime = new AgentRuntime(agent);++ const state = AgentRuntime.createInitialState({ sessionId: 'test-session' });++ await expect(runtime.resume(state)).rejects.toThrow(+ 'Cannot resume: state is not interrupted',+ );+ });++ it('should resume with specific context', async () => {+ const agent = new MockAgent();+ agent.modelRuntime = async function* () {+ yield { content: 'context-specific response' };+ };+ const runtime = new AgentRuntime(agent);++ let state = AgentRuntime.createInitialState({+ sessionId: 'test-session',+ messages: [{ role: 'user', content: 'Hello' }],+ });+ const interruptResult = runtime.interrupt(state, 'Test interruption');++ const resumeContext: RuntimeContext = {+ phase: 'user_input',+ payload: { message: { role: 'user', content: 'Hello' } },+ session: {+ sessionId: 'test-session',+ messageCount: 1,+ eventCount: 2, // init + interrupt+ status: 'interrupted',+ stepCount: 0,+ },+ };++ const resumeResult = await runtime.resume(+ interruptResult.newState,+ 'Resume with context',+ resumeContext,+ );++ expect(resumeResult.events.length).toBeGreaterThanOrEqual(2); // resume + llm events (start, stream, result)+ expect(resumeResult.events[0].type).toBe('resumed');+ expect(resumeResult.newState.status).toBe('running');++ // Should contain LLM execution events+ expect(resumeResult.events.map((e) => e.type)).toContain('llm_start');+ expect(resumeResult.events.map((e) => e.type)).toContain('llm_result');+ });+ });++ describe('Usage and Cost Tracking', () => {+ it('should initialize with zero usage and cost', () => {+ const state = AgentRuntime.createInitialState({ sessionId: 'test-session' });++ expect(state.usage).toMatchObject({+ llm: {+ tokens: { input: 0, output: 0, total: 0 },+ apiCalls: 0,+ processingTimeMs: 0,+ },+ tools: {+ totalCalls: 0,+ byTool: {},+ totalTimeMs: 0,+ },+ humanInteraction: {+ approvalRequests: 0,+ promptRequests: 0,+ selectRequests: 0,+ totalWaitingTimeMs: 0,+ },+ });++ expect(state.cost).toMatchObject({+ llm: {+ byModel: {},+ total: 0,+ currency: 'USD',+ },+ tools: {+ byTool: {},+ total: 0,+ currency: 'USD',+ },+ total: 0,+ currency: 'USD',+ calculatedAt: expect.any(String),+ });+ });++ it('should track usage and cost through agent methods', async () => {+ // Create agent with cost calculation methods+ class CostTrackingAgent implements Agent {+ tools = {+ test_tool: async () => ({ result: 'success' }),+ };++ async runner(context: RuntimeContext, state: AgentState) {+ switch (context.phase) {+ case 'user_input':+ return { type: 'call_llm' as const, payload: { messages: state.messages } };+ default:+ return {+ type: 'finish' as const,+ reason: 'completed' as const,+ reasonDetail: 'Done',+ };+ }+ }++ calculateUsage(+ operationType: 'llm' | 'tool' | 'human_interaction',+ operationResult: any,+ previousUsage: Usage,+ ): Usage {+ const newUsage = structuredClone(previousUsage);++ if (operationType === 'llm') {+ newUsage.llm.tokens.input += 100;+ newUsage.llm.tokens.output += 50;+ newUsage.llm.tokens.total += 150;+ newUsage.llm.apiCalls += 1;+ newUsage.llm.processingTimeMs += 1000;+ }++ return newUsage;+ }++ calculateCost(context: CostCalculationContext): Cost {+ const newCost = structuredClone(context.previousCost || (context.usage as any));++ // Simple cost calculation: $0.01 per 1000 tokens+ const tokenCost = (context.usage.llm.tokens.total / 1000) * 0.01;+ newCost.llm.total = tokenCost;+ newCost.total = tokenCost;+ newCost.calculatedAt = new Date().toISOString();++ return newCost;+ }+ modelRuntime = async function* () {+ yield { content: 'test response' };+ };+ }++ const agent = new CostTrackingAgent();+ const runtime = new AgentRuntime(agent);++ const state = AgentRuntime.createInitialState({+ sessionId: 'test-session',+ messages: [{ role: 'user', content: 'Hello' }],+ });++ const result = await runtime.step(state, createTestContext('user_input'));++ // Should have updated usage+ expect(result.newState.usage.llm.tokens.total).toBe(150);+ expect(result.newState.usage.llm.apiCalls).toBe(1);++ // Should have calculated cost+ expect(result.newState.cost.total).toBe(0.0015); // 150 tokens * $0.01/1000+ });++ it('should respect cost limits with stop action', async () => {+ class CostTrackingAgent implements Agent {+ async runner(context: RuntimeContext, state: AgentState) {+ return { type: 'call_llm' as const, payload: { messages: state.messages } };+ }++ calculateUsage(operationType: string, operationResult: any, previousUsage: Usage): Usage {+ const newUsage = structuredClone(previousUsage);+ newUsage.llm.tokens.total += 1000; // High token usage+ return newUsage;+ }++ calculateCost(context: CostCalculationContext): Cost {+ const newCost = structuredClone(context.previousCost || ({} as Cost));+ newCost.total = 10.0; // High cost that exceeds limit+ newCost.currency = 'USD';+ newCost.calculatedAt = new Date().toISOString();+ return newCost;+ }+ modelRuntime = async function* () {+ yield { content: 'test response' };+ };+ }++ const agent = new CostTrackingAgent();+ const runtime = new AgentRuntime(agent);++ const costLimit: CostLimit = {+ maxTotalCost: 5.0,+ currency: 'USD',+ onExceeded: 'stop',+ };++ const state = AgentRuntime.createInitialState({+ sessionId: 'test-session',+ messages: [{ role: 'user', content: 'Hello' }],+ costLimit,+ });++ const result = await runtime.step(state, createTestContext('user_input'));++ expect(result.newState.status).toBe('done');+ expect(result.events[0]).toMatchObject({+ type: 'done',+ reason: 'cost_limit_exceeded',+ reasonDetail: expect.stringContaining('Cost limit exceeded'),+ });+ });++ it('should handle cost limit with interrupt action', async () => {+ class CostTrackingAgent implements Agent {+ async runner(context: RuntimeContext, state: AgentState) {+ return { type: 'call_llm' as const, payload: { messages: state.messages } };+ }++ calculateCost(context: CostCalculationContext): Cost {+ return {+ llm: { byModel: {}, total: 15.0, currency: 'USD' },+ tools: { byTool: {}, total: 0, currency: 'USD' },+ total: 15.0,+ currency: 'USD',+ calculatedAt: new Date().toISOString(),+ };+ }+ modelRuntime = async function* () {+ yield { content: 'test response' };+ };+ }++ const agent = new CostTrackingAgent();+ const runtime = new AgentRuntime(agent);++ const costLimit: CostLimit = {+ maxTotalCost: 10.0,+ currency: 'USD',+ onExceeded: 'interrupt',+ };++ const state = AgentRuntime.createInitialState({+ sessionId: 'test-session',+ messages: [{ role: 'user', content: 'Hello' }],+ costLimit,+ });++ const result = await runtime.step(state, createTestContext('user_input'));++ expect(result.newState.status).toBe('interrupted');+ expect(result.events[0]).toMatchObject({+ type: 'interrupted',+ reason: expect.stringContaining('Cost limit exceeded'),+ metadata: expect.objectContaining({+ costExceeded: true,+ }),+ });+ });+ });++ describe('Integration Tests', () => {+ it('should complete a full conversation flow', async () => {+ const agent = new MockAgent();+ agent.tools = {+ get_weather: vi.fn().mockResolvedValue({+ temperature: 25,+ condition: 'sunny',+ }),+ };++ // Mock agent behavior for different states+ agent.runner = vi.fn().mockImplementation((context: RuntimeContext, state: AgentState) => {+ switch (context.phase) {+ case 'user_input':+ return Promise.resolve({ type: 'call_llm', payload: { messages: state.messages } });+ case 'llm_result':+ const llmPayload = context.payload as { result: any; hasToolCalls: boolean };+ if (llmPayload.hasToolCalls) {+ return Promise.resolve({+ type: 'request_human_approve',+ pendingToolsCalling: llmPayload.result.tool_calls,+ });+ }+ return Promise.resolve({ type: 'finish', reason: 'completed', reasonDetail: 'Done' });+ case 'tool_result':+ return Promise.resolve({ type: 'call_llm', payload: { messages: state.messages } });+ default:+ return Promise.resolve({ type: 'finish', reason: 'completed', reasonDetail: 'Done' });+ }+ });++ async function* mockModelRuntime(payload: unknown) {+ const messages = (payload as any).messages;+ const lastMessage = messages[messages.length - 1];+ if (lastMessage.role === 'user') {+ yield { content: "I'll check the weather for you." };+ yield {+ tool_calls: [+ {+ id: 'call_weather',+ type: 'function' as const,+ function: {+ name: 'get_weather',+ arguments: '{"city": "Beijing"}',+ },+ },+ ],+ };+ } else if (lastMessage.role === 'tool') {+ yield { content: 'The weather in Beijing is 25°C and sunny.' };+ }+ }++ agent.modelRuntime = mockModelRuntime;+ const runtime = new AgentRuntime(agent);++ // Step 1: User asks question+ let state = AgentRuntime.createInitialState({+ sessionId: 'test-session',+ messages: [{ role: 'user', content: "What's the weather in Beijing?" }],+ });+ let result = await runtime.step(state);++ // Should get LLM response with tool call (status is 'running' after LLM execution)+ expect(result.newState.status).toBe('running');+ // In new architecture, call_llm doesn't add messages to state+ expect(result.newState.messages).toHaveLength(1); // Only user message+ // Check events contain the tool call result+ expect(result.events).toContainEqual(+ expect.objectContaining({+ type: 'llm_result',+ result: expect.objectContaining({+ tool_calls: expect.arrayContaining([+ expect.objectContaining({+ id: 'call_weather',+ type: 'function',+ }),+ ]),+ }),+ }),+ );++ // Step 1.5: Agent processes assistant message with tool calls using nextContext+ result = await runtime.step(result.newState, result.nextContext);++ // Now should request human approval+ expect(result.newState.status).toBe('waiting_for_human_input');+ expect(result.newState.pendingToolsCalling).toHaveLength(1);++ // Step 2: Approve and execute tool call+ const toolCall = result.newState.pendingToolsCalling![0];+ result = await runtime.approveToolCall(result.newState, toolCall);++ // Should have executed tool+ expect((agent.tools as any).get_weather).toHaveBeenCalledWith({ city: 'Beijing' });+ expect(result.newState.messages).toHaveLength(2); // user + tool result (call_tool executor adds tool message)++ // Step 3: LLM processes tool result using nextContext+ result = await runtime.step(result.newState, result.nextContext);++ // Should get final response in events+ expect(result.events).toContainEqual(+ expect.objectContaining({+ type: 'llm_result',+ result: expect.objectContaining({+ content: expect.stringContaining('25°C and sunny'),+ }),+ }),+ );+ });+ });+});diff --git a/packages/agent-runtime/src/core/index.ts b/packages/agent-runtime/src/core/index.tsnew file mode 100644index 00000000000..2bc3e7914d6--- /dev/null+++ b/packages/agent-runtime/src/core/index.ts@@ -0,0 +1 @@+export * from './runtime';diff --git a/packages/agent-runtime/src/core/runtime.ts b/packages/agent-runtime/src/core/runtime.tsnew file mode 100644index 00000000000..a3121032ed4--- /dev/null+++ b/packages/agent-runtime/src/core/runtime.ts@@ -0,0 +1,656 @@+import type {+ Agent,+ AgentEvent,+ AgentInstruction,+ AgentState,+ Cost,+ InstructionExecutor,+ RuntimeConfig,+ RuntimeContext,+ ToolRegistry,+ ToolsCalling,+ Usage,+} from '../types';++/**+ * Simplified Agent Runtime - The "Engine" that executes instructions from an "Agent" (Brain).+ * Now includes built-in call_llm support and allows full executor customization.+ */+export class AgentRuntime {+ private executors: Record<AgentInstruction['type'], InstructionExecutor>;++ constructor(+ private agent: Agent,+ private config: RuntimeConfig = {},+ ) {+ // Build executors with priority: agent.executors > config.executors > built-in+ this.executors = {+ call_llm: this.createCallLLMExecutor(),+ call_tool: this.createCallToolExecutor(),+ finish: this.createFinishExecutor(),+ request_human_approve: this.createHumanApproveExecutor(),+ request_human_prompt: this.createHumanPromptExecutor(),+ request_human_select: this.createHumanSelectExecutor(),+ // Config executors override built-in+ ...config.executors,+ // Agent provided executors have highest priority+ ...(agent.executors as any),+ };+ }++ /**+ * Executes a single step of the Plan -> Execute loop.+ * @param state - Current agent state+ * @param context - Runtime context for this step (required for proper phase detection)+ */+ async step(+ state: AgentState,+ context?: RuntimeContext,+ ): Promise<{ events: AgentEvent[]; newState: AgentState; nextContext?: RuntimeContext }> {+ try {+ // Increment step count and check limits+ const newState = structuredClone(state);+ newState.stepCount += 1;+ newState.lastModified = new Date().toISOString();++ // Check maximum steps limit+ if (newState.maxSteps && newState.stepCount > newState.maxSteps) {+ // Finish execution when maxSteps is exceeded+ newState.status = 'done';+ const finishEvent = {+ finalState: newState,+ reason: 'max_steps_exceeded' as const,+ reasonDetail: `Maximum steps exceeded: ${newState.maxSteps}`,+ type: 'done' as const,+ };+ newState.events = [...newState.events, finishEvent];++ return {+ events: [finishEvent],+ newState,+ nextContext: undefined, // No next context when done+ };+ }++ // Use provided context or create initial context+ const runtimeContext = context || this.createInitialContext(newState);++ let result: { events: AgentEvent[]; newState: AgentState; nextContext?: RuntimeContext };++ // Handle human approved tool calls+ if (runtimeContext.phase === 'human_approved_tool') {+ const approvedPayload = runtimeContext.payload as { approvedToolCall: ToolsCalling };+ result = await this.executors.call_tool(+ { toolCall: approvedPayload.approvedToolCall, type: 'call_tool' },+ newState,+ );+ } else {+ // Standard flow: Plan -> Execute+ const instruction = await this.agent.runner(runtimeContext, newState);+ result = await this.executors[instruction.type](instruction, newState);+ }++ // Ensure stepCount is preserved in the result+ result.newState.stepCount = newState.stepCount;+ result.newState.lastModified = newState.lastModified;++ // Accumulate events to state history+ result.newState.events = [...result.newState.events, ...result.events];++ return result;+ } catch (error) {+ const errorState = structuredClone(state);+ errorState.stepCount += 1;+ errorState.lastModified = new Date().toISOString();+ return this.createErrorResult(errorState, error);+ }+ }++ /**+ * Convenience method for approving and executing a tool call+ */+ async approveToolCall(+ state: AgentState,+ approvedToolCall: ToolsCalling,+ ): Promise<{ events: AgentEvent[]; newState: AgentState; nextContext?: RuntimeContext }> {+ const context: RuntimeContext = {+ payload: { approvedToolCall },+ phase: 'human_approved_tool',+ session: this.createSessionContext(state),+ };++ return this.step(state, context);+ }++ /**+ * Interrupt the current execution+ * @param state - Current agent state+ * @param reason - Reason for interruption+ * @param canResume - Whether the interruption can be resumed later+ * @param metadata - Additional metadata about the interruption+ */+ interrupt(+ state: AgentState,+ reason: string,+ canResume: boolean = true,+ metadata?: Record<string, unknown>,+ ): { events: AgentEvent[]; newState: AgentState } {+ const newState = structuredClone(state);+ const interruptedAt = new Date().toISOString();++ newState.status = 'interrupted';+ newState.lastModified = interruptedAt;+ newState.interruption = {+ canResume,+ interruptedAt,+ // Store the current step for potential resumption+ interruptedInstruction: undefined,++ reason, // Could be enhanced to store current instruction+ };++ const interruptEvent: AgentEvent = {+ canResume,+ interruptedAt,+ metadata,+ reason,+ type: 'interrupted',+ };++ newState.events = [...newState.events, interruptEvent];++ return {+ events: [interruptEvent],+ newState,+ };+ }++ /**+ * Resume execution from an interrupted state+ * @param state - Interrupted agent state+ * @param reason - Reason for resumption+ * @param context - Optional context to resume with+ */+ async resume(+ state: AgentState,+ reason: string = 'User resumed execution',+ context?: RuntimeContext,+ ): Promise<{ events: AgentEvent[]; newState: AgentState; nextContext?: RuntimeContext }> {+ if (state.status !== 'interrupted') {+ throw new Error('Cannot resume: state is not interrupted');+ }++ if (state.interruption && !state.interruption.canResume) {+ throw new Error('Cannot resume: interruption is not resumable');+ }++ const newState = structuredClone(state);+ const resumedAt = new Date().toISOString();+ const resumedFromStep = state.stepCount;++ // Clear interruption context and set status back to running+ newState.status = 'running';+ newState.lastModified = resumedAt;+ newState.interruption = undefined;++ const resumeEvent: AgentEvent = {+ reason,+ resumedAt,+ resumedFromStep,+ type: 'resumed',+ };++ newState.events = [...newState.events, resumeEvent];++ // If context is provided, continue with that context+ if (context) {+ const result = await this.step(newState, context);+ return {+ events: [resumeEvent, ...result.events],+ newState: result.newState,+ nextContext: result.nextContext,+ };+ }++ // Otherwise, just return the resumed state+ return {+ events: [resumeEvent],+ newState,+ nextContext: this.createInitialContext(newState),+ };+ }++ /**+ * Create a new agent state with flexible initialization+ * @param partialState - Partial state to override defaults+ * @returns Complete AgentState with defaults filled in+ */+ static createInitialState(partialState: Partial<AgentState> & { sessionId: string }): AgentState {+ const now = new Date().toISOString();++ // Default usage statistics+ const defaultUsage: Usage = {+ humanInteraction: {+ approvalRequests: 0,+ promptRequests: 0,+ selectRequests: 0,+ totalWaitingTimeMs: 0,+ },+ llm: {+ apiCalls: 0,+ processingTimeMs: 0,+ tokens: { input: 0, output: 0, total: 0 },+ },+ tools: {+ byTool: {},+ totalCalls: 0,+ totalTimeMs: 0,+ },+ };++ // Default cost structure+ const defaultCost: Cost = {+ calculatedAt: now,+ currency: 'USD',+ llm: {+ byModel: {},+ currency: 'USD',+ total: 0,+ },+ tools: {+ byTool: {},+ currency: 'USD',+ total: 0,+ },+ total: 0,+ };++ return {+ cost: defaultCost,+ // Default values+ createdAt: now,+ events: [],+ lastModified: now,+ messages: [],+ status: 'idle',+ stepCount: 0,+ usage: defaultUsage,+ // User provided values override defaults+ ...partialState,+ };+ }++ // ============ Executor Factory Methods ============++ /** Create call_llm executor with streaming support */+ private createCallLLMExecutor(): InstructionExecutor {+ return async (instruction, state) => {+ const { payload } = instruction as Extract<AgentInstruction, { type: 'call_llm' }>;+ const newState = structuredClone(state);+ const events: AgentEvent[] = [];++ newState.status = 'running';+ newState.lastModified = new Date().toISOString();++ events.push({ payload, type: 'llm_start' });++ // Use Agent's modelRuntime first, fallback to config+ const modelRuntime = this.agent.modelRuntime;+ if (!modelRuntime) {+ throw new Error(+ 'Model Runtime is required for call_llm instruction. Provide it via Agent.modelRuntime or RuntimeConfig.modelRuntime',+ );+ }++ let assistantContent = '';+ let toolCalls: ToolsCalling[] = [];++ try {+ // Stream LLM response+ for await (const chunk of modelRuntime(payload)) {+ events.push({ chunk, type: 'llm_stream' });++ // Accumulate content and tool calls from chunks+ if (chunk.content) {+ assistantContent += chunk.content;+ }++ if (chunk.tool_calls) {+ toolCalls = chunk.tool_calls;+ }+ }++ events.push({+ result: { content: assistantContent, tool_calls: toolCalls },+ type: 'llm_result',+ });++ // Update usage and cost if agent provides calculation methods+ if (this.agent.calculateUsage) {+ newState.usage = this.agent.calculateUsage(+ 'llm',+ { content: assistantContent, tool_calls: toolCalls },+ newState.usage,+ );+ }++ if (this.agent.calculateCost) {+ newState.cost = this.agent.calculateCost({+ costLimit: newState.costLimit,+ previousCost: newState.cost,+ usage: newState.usage,+ });+ }++ // Check cost limits+ if (newState.costLimit && newState.cost.total > newState.costLimit.maxTotalCost) {+ return this.handleCostLimitExceeded(newState);+ }++ // Provide next context based on LLM result+ const nextContext: RuntimeContext = {+ payload: {+ hasToolCalls: toolCalls.length > 0,+ result: { content: assistantContent, tool_calls: toolCalls },+ toolCalls,+ },+ phase: 'llm_result',+ session: this.createSessionContext(newState),+ };++ return { events, newState, nextContext };+ } catch (error) {+ return this.createErrorResult(state, error);+ }+ };+ }++ /** Create call_tool executor */+ private createCallToolExecutor(): InstructionExecutor {+ return async (instruction, state) => {+ const { toolCall } = instruction as Extract<AgentInstruction, { type: 'call_tool' }>;+ const newState = structuredClone(state);+ const events: AgentEvent[] = [];++ newState.lastModified = new Date().toISOString();+ newState.status = 'running';++ const tools = this.agent.tools || ({} as ToolRegistry);+ const handler = tools[toolCall.function.name];+ if (!handler) throw new Error(`Tool not found: ${toolCall.function.name}`);++ const args = JSON.parse(toolCall.function.arguments);+ const result = await handler(args);++ newState.messages.push({+ content: JSON.stringify(result),+ role: 'tool',+ tool_call_id: toolCall.id,+ });++ events.push({ id: toolCall.id, result, type: 'tool_result' });++ // Update usage and cost if agent provides calculation methods+ if (this.agent.calculateUsage) {+ newState.usage = this.agent.calculateUsage(+ 'tool',+ { executionTime: 0, result, toolCall }, // Could track actual execution time+ newState.usage,+ );+ }++ if (this.agent.calculateCost) {+ newState.cost = this.agent.calculateCost({+ costLimit: newState.costLimit,+ previousCost: newState.cost,+ usage: newState.usage,+ });+ }++ // Check cost limits+ if (newState.costLimit && newState.cost.total > newState.costLimit.maxTotalCost) {+ return this.handleCostLimitExceeded(newState);+ }++ // Provide next context for tool result+ const nextContext: RuntimeContext = {+ payload: {+ result,+ toolCall,+ toolCallId: toolCall.id,+ },+ phase: 'tool_result',+ session: this.createSessionContext(newState),+ };++ return { events, newState, nextContext };+ };+ }++ /** Create human approve executor */+ private createHumanApproveExecutor(): InstructionExecutor {+ return async (instruction, state) => {+ const { pendingToolsCalling } = instruction as Extract<+ AgentInstruction,+ { type: 'request_human_approve' }+ >;+ const newState = structuredClone(state);++ newState.lastModified = new Date().toISOString();+ newState.status = 'waiting_for_human_input';+ newState.pendingToolsCalling = pendingToolsCalling;++ const events: AgentEvent[] = [+ {+ pendingToolsCalling,+ sessionId: newState.sessionId,+ type: 'human_approve_required',+ },+ { toolCalls: pendingToolsCalling, type: 'tool_pending' },+ ];++ return { events, newState };+ };+ }++ /** Create human prompt executor */+ private createHumanPromptExecutor(): InstructionExecutor {+ return async (instruction, state) => {+ const { metadata, prompt } = instruction as Extract<+ AgentInstruction,+ { type: 'request_human_prompt' }+ >;+ const newState = structuredClone(state);++ newState.lastModified = new Date().toISOString();+ newState.status = 'waiting_for_human_input';+ newState.pendingHumanPrompt = { metadata, prompt };++ const events: AgentEvent[] = [+ {+ metadata,+ prompt,+ sessionId: newState.sessionId,+ type: 'human_prompt_required',+ },+ ];++ return { events, newState };+ };+ }++ /** Create human select executor */+ private createHumanSelectExecutor(): InstructionExecutor {+ return async (instruction, state) => {+ const { metadata, multi, options, prompt } = instruction as Extract<+ AgentInstruction,+ { type: 'request_human_select' }+ >;+ const newState = structuredClone(state);++ newState.lastModified = new Date().toISOString();+ newState.status = 'waiting_for_human_input';+ newState.pendingHumanSelect = { metadata, multi, options, prompt };++ const events: AgentEvent[] = [+ {+ metadata,+ multi,+ options,+ prompt,+ sessionId: newState.sessionId,+ type: 'human_select_required',+ },+ ];++ return { events, newState };+ };+ }++ /** Create finish executor */+ private createFinishExecutor(): InstructionExecutor {+ return async (instruction, state) => {+ const { reason, reasonDetail } = instruction as Extract<AgentInstruction, { type: 'finish' }>;+ const newState = structuredClone(state);++ newState.lastModified = new Date().toISOString();+ newState.status = 'done';++ const events: AgentEvent[] = [+ {+ finalState: newState,+ reason,+ reasonDetail,+ type: 'done',+ },+ ];+ return { events, newState };+ };+ }++ // ============ Helper Methods ============++ /**+ * Handle cost limit exceeded scenario+ */+ private handleCostLimitExceeded(state: AgentState): {+ events: AgentEvent[];+ newState: AgentState;+ nextContext?: RuntimeContext;+ } {+ const newState = structuredClone(state);+ const costLimit = newState.costLimit!;++ switch (costLimit.onExceeded) {+ case 'stop': {+ newState.status = 'done';+ const finishEvent = {+ finalState: newState,+ reason: 'cost_limit_exceeded' as const,+ reasonDetail: `Cost limit exceeded: ${newState.cost.total} ${newState.cost.currency} > ${costLimit.maxTotalCost} ${costLimit.currency}`,+ type: 'done' as const,+ };+ newState.events = [...newState.events, finishEvent];+ return {+ events: [finishEvent],+ newState,+ nextContext: undefined,+ };+ }++ case 'interrupt': {+ return {+ ...this.interrupt(+ newState,+ `Cost limit exceeded: ${newState.cost.total} ${newState.cost.currency}`,+ true,+ {+ costExceeded: true,+ currentCost: newState.cost.total,+ limitCost: costLimit.maxTotalCost,+ },+ ),+ nextContext: undefined,+ };+ }++ default: {+ // Continue execution but emit warning event+ const warningEvent = {+ error: new Error(+ `Warning: Cost limit exceeded: ${newState.cost.total} ${newState.cost.currency}`,+ ),+ type: 'error' as const,+ };+ newState.events = [...newState.events, warningEvent];+ return {+ events: [warningEvent],+ newState,+ nextContext: {+ payload: { error: warningEvent.error, isCostWarning: true },+ phase: 'error' as const,+ session: this.createSessionContext(newState),+ },+ };+ }+ }+ }++ /**+ * Create session context metadata - reusable helper+ */+ private createSessionContext(state: AgentState) {+ return {+ eventCount: state.events.length,+ messageCount: state.messages.length,+ sessionId: state.sessionId,+ status: state.status,+ stepCount: state.stepCount,+ };+ }++ /**+ * Create initial context for the first step (fallback for backward compatibility)+ */+ private createInitialContext(state: AgentState): RuntimeContext {+ const lastMessage = state.messages.at(-1);++ if (lastMessage?.role === 'user') {+ return {+ payload: {+ isFirstMessage: state.messages.length === 1,+ message: lastMessage,+ },+ phase: 'user_input',+ session: this.createSessionContext(state),+ };+ }++ return {+ payload: undefined,+ phase: 'init',+ session: this.createSessionContext(state),+ };+ }++ /** Create error state and events */+ private createErrorResult(+ state: AgentState,+ error: any,+ ): { events: AgentEvent[]; newState: AgentState } {+ const errorState = structuredClone(state);+ errorState.status = 'error';+ errorState.error = error;+ errorState.lastModified = new Date().toISOString();++ const errorEvent = { error, type: 'error' } as const;++ // Accumulate error event to state history+ errorState.events = [...errorState.events, errorEvent];++ return {+ events: [errorEvent],+ newState: errorState,+ };+ }+}diff --git a/packages/agent-runtime/src/index.ts b/packages/agent-runtime/src/index.tsnew file mode 100644index 00000000000..a5a9e053576--- /dev/null+++ b/packages/agent-runtime/src/index.ts@@ -0,0 +1,2 @@+export * from './core';+export * from './types';diff --git a/packages/agent-runtime/src/types/event.ts b/packages/agent-runtime/src/types/event.tsnew file mode 100644index 00000000000..5e39c68a041--- /dev/null+++ b/packages/agent-runtime/src/types/event.ts@@ -0,0 +1,121 @@+/* eslint-disable sort-keys-fix/sort-keys-fix, typescript-sort-keys/interface */+import type { AgentState, ToolsCalling } from './state';++export interface AgentEventInit {+ type: 'init';+}++export interface AgentEventLlmStart {+ type: 'llm_start';+ payload: unknown;+}++export interface AgentEventLlmStream {+ type: 'llm_stream';+ chunk: unknown;+}++export interface AgentEventLlmResult {+ type: 'llm_result';+ result: unknown;+}++export interface AgentEventToolPending {+ type: 'tool_pending';+ toolCalls: ToolsCalling[];+}++export interface AgentEventToolResult {+ type: 'tool_result';+ id: string;+ result: any;+}++export interface AgentEventHumanApproveRequired {+ type: 'human_approve_required';+ pendingToolsCalling: ToolsCalling[];+ sessionId: string;+}++export interface AgentEventHumanPromptRequired {+ type: 'human_prompt_required';+ metadata?: Record<string, unknown>;+ prompt: string;+ sessionId: string;+}++export interface AgentEventHumanSelectRequired {+ type: 'human_select_required';+ metadata?: Record<string, unknown>;+ multi?: boolean;+ options: { label: string; value: string }[];+ prompt?: string;+ sessionId: string;+}++/**+ * Standardized finish reasons+ */+export type FinishReason =+ | 'completed' // Normal completion+ | 'user_requested' // User requested to end+ | 'max_steps_exceeded' // Reached maximum steps limit+ | 'cost_limit_exceeded' // Reached cost limit+ | 'timeout' // Execution timeout+ | 'agent_decision' // Agent decided to finish+ | 'error_recovery' // Finished due to unrecoverable error+ | 'system_shutdown'; // System is shutting down++export interface AgentEventDone {+ type: 'done';+ finalState: AgentState;+ reason: FinishReason;+ reasonDetail?: string;+}++export interface AgentEventError {+ type: 'error';+ error: any;+}++export interface AgentEventInterrupted {+ type: 'interrupted';+ reason: string;+ interruptedAt: string;+ interruptedInstruction?: any;+ canResume: boolean;+ metadata?: Record<string, unknown>;+}++export interface AgentEventResumed {+ type: 'resumed';+ reason: string;+ resumedAt: string;+ resumedFromStep: number;+ metadata?: Record<string, unknown>;+}++/**+ * Events emitted by the AgentRuntime during execution+ */+export type AgentEvent =+ // Initialization+ | AgentEventInit+ // LLM streaming output+ | AgentEventLlmStart+ | AgentEventLlmStream+ | AgentEventLlmResult+ // Tool invocation+ | AgentEventToolPending+ | AgentEventToolResult+ // Normal completion+ | AgentEventDone+ // Error thrown+ | AgentEventError+ // Human-in-the-loop (HIL)+ | AgentEventHumanApproveRequired+ | AgentEventHumanPromptRequired+ | AgentEventHumanSelectRequired+ // Interruption and resumption+ | AgentEventInterrupted+ | AgentEventResumed;diff --git a/packages/agent-runtime/src/types/index.ts b/packages/agent-runtime/src/types/index.tsnew file mode 100644index 00000000000..5eaab90c6b7--- /dev/null+++ b/packages/agent-runtime/src/types/index.ts@@ -0,0 +1,5 @@+export * from './event';+export * from './instruction';+export * from './runtime';+export * from './state';+export * from './usage';diff --git a/packages/agent-runtime/src/types/instruction.ts b/packages/agent-runtime/src/types/instruction.tsnew file mode 100644index 00000000000..6030089607f--- /dev/null+++ b/packages/agent-runtime/src/types/instruction.ts@@ -0,0 +1,125 @@+import type { FinishReason } from './event';+import { AgentState, ToolRegistry, ToolsCalling } from './state';+import type { Cost, CostCalculationContext, Usage } from './usage';++/**+ * Runtime execution context passed to Agent runner+ */+export interface RuntimeContext {+ /** Phase-specific payload/context */+ payload?: unknown;+ /** Current execution phase */+ phase:+ | 'init'+ | 'user_input'+ | 'llm_result'+ | 'tool_result'+ | 'human_response'+ | 'human_approved_tool'+ | 'error';+ /** Session metadata */+ session: {+ eventCount: number;+ messageCount: number;+ sessionId: string;+ status: AgentState['status'];+ stepCount: number;+ };+}++/**+ * Represents the "Brain" of an agent.+ * It contains all the decision-making logic and is completely stateless.+ */+export interface Agent {+ /**+ * Calculate cost from usage statistics+ * @param context - Cost calculation context with usage and limits+ * @returns Updated cost information+ */+ calculateCost?(context: CostCalculationContext): Cost;++ /**+ * Calculate usage statistics from operation results+ * @param operationType - Type of operation that was performed+ * @param operationResult - Result data from the operation+ * @param previousUsage - Previous usage statistics+ * @returns Updated usage statistics+ */+ calculateUsage?(+ operationType: 'llm' | 'tool' | 'human_interaction',+ operationResult: any,+ previousUsage: Usage,+ ): Usage;++ /** Optional custom executors mapping to extend runtime behaviors */+ executors?: Partial<Record<AgentInstruction['type'], any>>;++ /**+ * Model runtime function for LLM calls - Agent owns its model integration+ * @param payload - LLM call payload (messages, tools, etc.)+ * @returns Async iterable of streaming response chunks+ */+ modelRuntime?: (payload: unknown) => AsyncIterable<any>;++ /**+ * The core runner method. Based on the current execution context and state,+ * it decides what the next action should be.+ * @param context - Current runtime context with phase and payload+ * @param state - Complete agent state for reference+ */+ runner(context: RuntimeContext, state: AgentState): Promise<AgentInstruction>;++ /** Optional tools registry held by the agent */+ tools?: ToolRegistry;+}++export interface AgentInstructionCallLlm {+ payload: unknown;+ type: 'call_llm';+}++export interface AgentInstructionCallTool {+ toolCall: ToolsCalling;+ type: 'call_tool';+}++export interface AgentInstructionRequestHumanPrompt {+ metadata?: Record<string, unknown>;+ prompt: string;+ reason?: string;+ type: 'request_human_prompt';+}++export interface AgentInstructionRequestHumanSelect {+ metadata?: Record<string, unknown>;+ multi?: boolean;+ options: Array<{ label: string; value: string }>;+ prompt?: string;+ reason?: string;+ type: 'request_human_select';+}++export interface AgentInstructionRequestHumanApprove {+ pendingToolsCalling: ToolsCalling[];+ reason?: string;+ type: 'request_human_approve';+}++export interface AgentInstructionFinish {+ reason: FinishReason;+ reasonDetail?: string;+ type: 'finish';+}++/**+ * A serializable instruction object that the "Agent" (Brain) returns+ * to the "AgentRuntime" (Engine) to execute.+ */+export type AgentInstruction =+ | AgentInstructionCallLlm+ | AgentInstructionCallTool+ | AgentInstructionRequestHumanPrompt+ | AgentInstructionRequestHumanSelect+ | AgentInstructionRequestHumanApprove+ | AgentInstructionFinish;diff --git a/packages/agent-runtime/src/types/runtime.ts b/packages/agent-runtime/src/types/runtime.tsnew file mode 100644index 00000000000..d86cdf01abd--- /dev/null+++ b/packages/agent-runtime/src/types/runtime.ts@@ -0,0 +1,18 @@+import type { AgentEvent } from './event';+import { AgentInstruction, RuntimeContext } from './instruction';+import { AgentState } from './state';++export type InstructionExecutor = (+ instruction: AgentInstruction,+ state: AgentState,+) => Promise<{+ events: AgentEvent[];+ newState: AgentState;+ /** Next context to pass to Agent runner (if execution should continue) */+ nextContext?: RuntimeContext;+}>;++export interface RuntimeConfig {+ /** Custom executors for specific instruction types */+ executors?: Partial<Record<AgentInstruction['type'], InstructionExecutor>>;+}diff --git a/packages/agent-runtime/src/types/state.ts b/packages/agent-runtime/src/types/state.tsnew file mode 100644index 00000000000..d479552827b--- /dev/null+++ b/packages/agent-runtime/src/types/state.ts@@ -0,0 +1,108 @@+/* eslint-disable sort-keys-fix/sort-keys-fix, typescript-sort-keys/interface */+import type { AgentEvent } from './event';+import type { Cost, CostLimit, Usage } from './usage';++/**+ * Agent's serializable state.+ * This is the "passport" that can be persisted and transferred.+ */+export interface AgentState {+ sessionId: string;+ // --- State Machine ---+ status: 'idle' | 'running' | 'waiting_for_human_input' | 'done' | 'error' | 'interrupted';++ // --- Core Context ---+ messages: any[];+ systemRole?: string;++ // --- Event History ---+ /**+ * Complete event trace for this agent session.+ * Useful for debugging, auditing, and state replay.+ */+ events: AgentEvent[];++ // --- Execution Tracking ---+ /**+ * Number of execution steps in this session.+ * Incremented on each runtime.step() call.+ */+ stepCount: number;+ /**+ * Optional maximum number of steps allowed.+ * If set, execution will stop with error when exceeded.+ */+ maxSteps?: number;++ // --- Usage and Cost Tracking ---+ /**+ * Accumulated usage statistics for this session.+ * Tracks tokens, API calls, tool usage, etc.+ */+ usage: Usage;+ /**+ * Current calculated cost for this session.+ * Updated after each billable operation.+ */+ cost: Cost;+ /**+ * Optional cost limits configuration.+ * If set, execution will stop when limits are exceeded.+ */+ costLimit?: CostLimit;++ // --- HIL ---+ /**+ * When status is 'waiting_for_human_input', this stores pending requests+ * for human-in-the-loop operations.+ */+ pendingToolsCalling?: ToolsCalling[];+ pendingHumanPrompt?: { metadata?: Record<string, unknown>; prompt: string };+ pendingHumanSelect?: {+ metadata?: Record<string, unknown>;+ multi?: boolean;+ options: Array<{ label: string; value: string }>;+ prompt?: string;+ };++ // --- Interruption Handling ---+ /**+ * When status is 'interrupted', this stores the interruption context+ * for potential resumption or cleanup.+ */+ interruption?: {+ /** Reason for interruption */+ reason: string;+ /** Timestamp when interruption occurred */+ interruptedAt: string;+ /** The instruction that was being executed when interrupted */+ interruptedInstruction?: any;+ /** Whether the interruption can be resumed */+ canResume: boolean;+ };++ // --- Metadata ---+ createdAt: string;+ error?: any;+ lastModified: string;++ // --- Extensible metadata ---+ metadata?: Record<string, any>;+}++/**+ * OpenAI Tool Call+ */+export interface ToolsCalling {+ function: {+ arguments: string;+ name: string; // A JSON string of arguments+ };+ id: string;+ type: 'function';+}++/**+ * A registry for tools, mapping tool names to their implementation.+ */+export type ToolRegistry = Record<string, (args: any) => Promise<any>>;diff --git a/packages/agent-runtime/src/types/usage.ts b/packages/agent-runtime/src/types/usage.tsnew file mode 100644index 00000000000..41a22bfbd5b--- /dev/null+++ b/packages/agent-runtime/src/types/usage.ts@@ -0,0 +1,128 @@+/**+ * Token usage tracking for different types of operations+ */+export interface TokenUsage {+ /** Input tokens consumed */+ input: number;+ /** Output tokens generated */+ output: number;+ /** Total tokens (input + output) */+ total: number;+}++/**+ * Usage statistics for different operation types+ */+export interface Usage {+ /** Human interaction statistics */+ humanInteraction: {+ /** Number of approval requests */+ approvalRequests: number;+ /** Number of prompt requests */+ promptRequests: number;+ /** Number of selection requests */+ selectRequests: number;+ /** Total waiting time for human input */+ totalWaitingTimeMs: number;+ };++ /** LLM model usage */+ llm: {+ /** Number of LLM API calls made */+ apiCalls: number;+ /** Total processing time in milliseconds */+ processingTimeMs: number;+ /** Total token usage across all LLM calls */+ tokens: TokenUsage;+ };++ /** Tool usage statistics */+ tools: {+ /** Usage breakdown by tool name */+ byTool: Record<+ string,+ {+ calls: number;+ errors: number;+ totalTimeMs: number;+ }+ >;+ /** Number of tool calls executed */+ totalCalls: number;+ /** Total tool execution time */+ totalTimeMs: number;+ };+}++/**+ * Cost calculation result+ */+export interface Cost {+ /** Cost calculation timestamp */+ calculatedAt: string;++ currency: string;++ /** LLM API costs */+ llm: {+ /** Cost per model used */+ byModel: Record<+ string,+ {+ currency: string;+ inputTokens: number;+ outputTokens: number;+ totalCost: number;+ }+ >;+ currency: string;+ /** Total LLM cost */+ total: number;+ };+ /** Tool execution costs */+ tools: {+ /** Cost per tool (if tool has associated costs) */+ byTool: Record<+ string,+ {+ calls: number;+ currency: string;+ totalCost: number;+ }+ >;+ currency: string;+ /** Total tool cost */+ total: number;+ };++ /** Total session cost */+ total: number;+}++/**+ * Cost limit configuration+ */+export interface CostLimit {+ /** Currency for cost limits */+ currency: string;+ /** Maximum LLM cost allowed */+ maxLlmCost?: number;+ /** Maximum tool cost allowed */+ maxToolCost?: number;+ /** Maximum total cost allowed */+ maxTotalCost: number;+ /** Action to take when limit is exceeded */+ onExceeded: 'stop' | 'warn' | 'interrupt';+}++/**+ * Cost calculation context passed to Agent+ */+export interface CostCalculationContext {+ /** Cost limits configuration */+ costLimit?: CostLimit;+ /** Previous cost calculation (if any) */+ previousCost?: Cost;+ /** Current usage statistics */+ usage: Usage;+}diff --git a/packages/agent-runtime/vitest.config.mts b/packages/agent-runtime/vitest.config.mtsnew file mode 100644index 00000000000..e8dbea7a7c0--- /dev/null+++ b/packages/agent-runtime/vitest.config.mts@@ -0,0 +1,12 @@+import { defineConfig } from 'vitest/config';++export default defineConfig({+ test: {+ coverage: {+ reporter: ['text', 'json', 'lcov', 'text-summary'],+ },+ environment: 'happy-dom',+ },+});++