-
Notifications
You must be signed in to change notification settings - Fork 44
[WIP] add agent run controllers #380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RaoHai
wants to merge
17
commits into
eggjs:master
Choose a base branch
from
RaoHai:feat/impl-run-controllers
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3593562
[WIP] add agent run controllers
8748399
[WIP]feat: impl runs/stream
RaoHai 454b421
[WIP] hoist independ testcase
RaoHai d43e829
[WIP] hoist independ testcase
RaoHai e511bfd
[WIP] hoist independ testcase
RaoHai 10c518e
feat: sse-utils and test case
RaoHai 713f838
feat: sse-utils and test case
RaoHai 95a01a3
feat: sse-utils and test case
RaoHai 65cc05c
feat: impl moduleConfigs
RaoHai edf1036
feat: impl iterator of moduleConfigs
RaoHai 8897827
feat: impl iterator of moduleConfigs
RaoHai 17e9ba4
fix: use yield Object.entries(this.inner);
RaoHai 157d5b2
fix: eslint
RaoHai 2538bc5
feat: merge moduleconfigs iterator
RaoHai 7eb07b6
feat: add assistants api endpoints
RaoHai 31aa571
feat: add assistants api endpoints
RaoHai 5554d8f
feat: add assistants api endpoints
RaoHai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ run | |
| .DS_Store | ||
| .tmp | ||
| .vscode | ||
| .claude | ||
|
|
||
| package-lock.json | ||
| yarn.lock | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| import { | ||
| HTTPController, | ||
| HTTPMethod, | ||
| HTTPMethodEnum, | ||
| HTTPBody, | ||
| Context, | ||
| Middleware, | ||
| Inject, | ||
| } from '@eggjs/tegg'; | ||
| import type { EggContext } from '@eggjs/tegg'; | ||
| import type { RunCreateDTO } from './types'; | ||
| import { streamSSE } from '../../lib/sse'; | ||
| import { RunCreate } from './schemas'; | ||
| import { ZodErrorMiddleware } from '../middleware/ZodErrorMiddleware'; | ||
| import { RunsService } from '../../lib/runs/RunsService'; | ||
|
|
||
| /** | ||
| * LangGraph Runs Controller | ||
| * 处理 Run 相关的 HTTP 请求 | ||
| */ | ||
| @HTTPController({ | ||
| path: '/api', | ||
| }) | ||
| @Middleware(ZodErrorMiddleware) | ||
| export class RunsController { | ||
| @Inject() | ||
| runsService: RunsService; | ||
| /** | ||
| * POST /api/runs/stream | ||
| * 流式创建无状态 Run (SSE) | ||
| * | ||
| * 对应 LangGraph runs.mts 的 api.post("/runs/stream", ...) 端点 | ||
| */ | ||
| @HTTPMethod({ | ||
| method: HTTPMethodEnum.POST, | ||
| path: '/runs/stream', | ||
| }) | ||
| async streamStatelessRun(@Context() ctx: EggContext, @HTTPBody() payload: RunCreateDTO) { | ||
| const validated = RunCreate.parse(payload); | ||
|
|
||
| // 使用 RunsService 创建并验证 run | ||
| const run = await this.runsService.createValidRun( | ||
| undefined, // threadId (无状态 run) | ||
| validated, | ||
| { | ||
| // auth: ctx.auth, // TODO: 集成认证系统 | ||
| headers: ctx.headers, | ||
| }, | ||
| ); | ||
|
|
||
| console.log('streamStatelessRun', { | ||
| run, | ||
| agentConfigs: this.runsService.getAllAgentConfigs(), | ||
| }); | ||
|
|
||
| // 设置 Content-Location header | ||
| ctx.set('Content-Location', `/runs/${run.run_id}`); | ||
|
|
||
| // 类型断言帮助访问 input 中的 messages | ||
| const inputData = validated.input as { messages?: Array<{ role: string; content: string }> } | undefined; | ||
|
|
||
| // 使用 SSE 流式返回 | ||
| return streamSSE(ctx, async stream => { | ||
| // 如果需要在断开连接时取消,创建 AbortSignal | ||
| // const cancelOnDisconnect = validated.on_disconnect === 'cancel' | ||
| // ? getDisconnectAbortSignal(ctx, stream) | ||
| // : undefined; | ||
|
|
||
| try { | ||
| // TODO: 调用 runs service 的 stream.join 方法获取运行结果 | ||
| // for await (const { event, data } of runs().stream.join( | ||
| // runId, | ||
| // undefined, | ||
| // { | ||
| // cancelOnDisconnect, | ||
| // lastEventId: validated.stream_resumable ? "-1" : undefined, | ||
| // ignore404: true, | ||
| // }, | ||
| // auth | ||
| // )) { | ||
| // await stream.writeSSE({ data: JSON.stringify(data), event }); | ||
| // } | ||
|
|
||
| // Mock 实现:模拟 SSE 流式响应 | ||
| // 1. 发送 metadata 事件 | ||
| await stream.writeSSE({ | ||
| event: 'metadata', | ||
| data: JSON.stringify({ | ||
| run_id: run.run_id, | ||
| assistant_id: validated.assistant_id || 'mock_assistant', | ||
| }), | ||
| }); | ||
|
|
||
| await stream.sleep(100); | ||
|
|
||
| // 2. 发送 values 事件 - 模拟开始处理 | ||
| await stream.writeSSE({ | ||
| event: 'values', | ||
| data: JSON.stringify({ | ||
| messages: [ | ||
| { | ||
| role: 'user', | ||
| content: inputData?.messages?.[0]?.content || 'Hello', | ||
| }, | ||
| ], | ||
| }), | ||
| }); | ||
|
|
||
| await stream.sleep(500); | ||
|
|
||
| // 3. 发送 values 事件 - 模拟 AI 响应 | ||
| await stream.writeSSE({ | ||
| event: 'values', | ||
| data: JSON.stringify({ | ||
| messages: [ | ||
| { | ||
| role: 'user', | ||
| content: inputData?.messages?.[0]?.content || 'Hello', | ||
| }, | ||
| { | ||
| role: 'assistant', | ||
| content: `Mock response to: ${inputData?.messages?.[0]?.content || 'Hello'}`, | ||
| }, | ||
| ], | ||
| }), | ||
| }); | ||
|
|
||
| await stream.sleep(200); | ||
|
|
||
| // 4. 发送 end 事件 | ||
| await stream.writeSSE({ | ||
| event: 'end', | ||
| data: JSON.stringify({ | ||
| run_id: run.run_id, | ||
| status: 'completed', | ||
| }), | ||
| }); | ||
| } catch (error) { | ||
| console.error('Error streaming run:', error); | ||
| throw error; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建议使用
ctx.logger.error代替console.error来记录错误。这样可以利用应用统一的日志配置,方便后续的日志收集和分析。