fix(core): token usage and response event type for none stream mode.#524
fix(core): token usage and response event type for none stream mode.#524sunrabbit123 merged 1 commit intomainfrom
Conversation
@agentica/benchmark
@agentica/chat
agentica
@agentica/core
create-agentica
@agentica/rpc
@agentica/vector-selector
commit: |
There was a problem hiding this comment.
Pull request overview
This pull request refactors the response event handling in the core package by replacing the direct stream property with a discriminated union type response that can represent both streaming and non-streaming responses. It also fixes token usage tracking for non-streaming responses and updates dependencies to their latest patch versions.
Changes:
- Introduced a discriminated union type (
StreamResponseandNonStreamResponse) inAgenticaResponseEventto handle both streaming and non-streaming responses through a unifiedresponseproperty - Added token usage tracking and response event dispatching for non-streaming mode in
getChatCompletionFunction - Updated test code to use the new
event.response.streamdiscriminator andevent.response.dataaccessor - Updated
@samchon/openapifrom 6.0.0 to 6.0.1 andtypiafrom 11.0.0 to 11.0.3 across all packages
Reviewed changes
Copilot reviewed 5 out of 6 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/core/src/events/AgenticaResponseEvent.ts | Replaced stream property with discriminated union response type supporting both stream and non-stream modes |
| packages/core/src/factory/events.ts | Updated createResponseEvent factory to accept new response property instead of stream |
| packages/core/src/utils/request.ts | Added non-streaming response event dispatch with token usage tracking; updated streaming response to use new structure |
| test/src/features/test_base_streaming.ts | Updated test to check discriminated union and access stream data through new property path |
| pnpm-workspace.yaml | Updated catalog versions for @samchon/openapi and typia |
| pnpm-lock.yaml | Updated lockfile to reflect new dependency versions throughout the dependency tree |
Files not reviewed (1)
- pnpm-lock.yaml: Language not supported
Comments suppressed due to low confidence (1)
packages/core/src/utils/request.ts:136
- Consider using the
createResponseEventfactory function instead of inline object creation for consistency. The factory is imported from../factoryand provides a centralized way to create response events, ensuring consistency across the codebase. This would also eliminate the need for the type assertion on thebodyproperty.
void props.dispatch({
id: v4(),
type: "response",
request_id: event.id,
source,
response: {
stream: true,
data: streamDefaultReaderToAsyncGenerator(streamForStream.getReader(), props.abortSignal),
},
body: event.body as OpenAI.ChatCompletionCreateParamsStreaming,
options: event.options,
join: async () => {
const chunks = await StreamUtil.readAll(streamForJoin, props.abortSignal);
return ChatGptCompletionMessageUtil.merge(chunks);
},
created_at: new Date().toISOString(),
}).catch(() => {});
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -17,9 +17,9 @@ export interface AgenticaResponseEvent extends AgenticaEventBase<"response"> { | |||
| body: OpenAI.ChatCompletionCreateParamsStreaming; | |||
There was a problem hiding this comment.
The body property is typed as OpenAI.ChatCompletionCreateParamsStreaming, but it should support both streaming and non-streaming requests to match the actual usage in the code. When a non-streaming response is created (when config.stream === false), the body will have stream: false and should be typed as OpenAI.ChatCompletionCreateParamsNonStreaming. Consider changing this to OpenAI.ChatCompletionCreateParamsStreaming | OpenAI.ChatCompletionCreateParamsNonStreaming to match the AgenticaRequestEvent.body type and accurately represent both streaming and non-streaming scenarios.
| body: OpenAI.ChatCompletionCreateParamsStreaming; | |
| body: | |
| | OpenAI.ChatCompletionCreateParamsStreaming | |
| | OpenAI.ChatCompletionCreateParamsNonStreaming; |
This pull request refactors how response events are handled in the core package by unifying the representation of streamed and non-streamed responses, making the API more consistent and extensible. Additionally, it updates dependencies in the lockfile to newer patch versions for improved compatibility and bug fixes.
Core event and response handling improvements:
streamproperty inAgenticaResponseEventwith a newresponseproperty, which now supports both streamed and non-streamed responses through a discriminated union type (StreamResponseandNonStreamResponse). This makes the event structure more flexible and easier to extend. [1] [2]createResponseEventfactory to use the newresponseproperty instead of the oldstreamproperty. [1] [2]getChatCompletionFunctionto dispatch response events using the newresponseproperty for both streamed and non-streamed completions, ensuring consistent event shapes and easier downstream handling. [1] [2]Dependency updates:
@samchon/openapifrom version6.0.0to6.0.1andtypiafrom11.0.0to11.0.3throughout thepnpm-lock.yamlfile, ensuring compatibility and incorporating upstream fixes. (pnpm-lock.yamlL43-R47 and related lockfile changes)These changes improve the maintainability and reliability of the event system and keep dependencies up to date.