Skip to content

fix(core): token usage and response event type for none stream mode.#524

Merged
sunrabbit123 merged 1 commit intomainfrom
fix/stream
Jan 28, 2026
Merged

fix(core): token usage and response event type for none stream mode.#524
sunrabbit123 merged 1 commit intomainfrom
fix/stream

Conversation

@samchon
Copy link
Member

@samchon samchon commented Jan 28, 2026

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:

  • Replaced the stream property in AgenticaResponseEvent with a new response property, which now supports both streamed and non-streamed responses through a discriminated union type (StreamResponse and NonStreamResponse). This makes the event structure more flexible and easier to extend. [1] [2]
  • Updated the createResponseEvent factory to use the new response property instead of the old stream property. [1] [2]
  • Refactored getChatCompletionFunction to dispatch response events using the new response property for both streamed and non-streamed completions, ensuring consistent event shapes and easier downstream handling. [1] [2]

Dependency updates:

  • Upgraded @samchon/openapi from version 6.0.0 to 6.0.1 and typia from 11.0.0 to 11.0.3 throughout the pnpm-lock.yaml file, 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.

@samchon samchon requested a review from sunrabbit123 January 28, 2026 07:01
@samchon samchon self-assigned this Jan 28, 2026
Copilot AI review requested due to automatic review settings January 28, 2026 07:01
@samchon samchon added the bug Something isn't working label Jan 28, 2026
@samchon samchon added this to WrtnLabs Jan 28, 2026
@pkg-pr-new
Copy link

pkg-pr-new bot commented Jan 28, 2026

Open in StackBlitz

@agentica/benchmark

npm i https://pkg.pr.new/wrtnlabs/agentica/@agentica/benchmark@524

@agentica/chat

npm i https://pkg.pr.new/wrtnlabs/agentica/@agentica/chat@524

agentica

npm i https://pkg.pr.new/wrtnlabs/agentica@524

@agentica/core

npm i https://pkg.pr.new/wrtnlabs/agentica/@agentica/core@524

create-agentica

npm i https://pkg.pr.new/wrtnlabs/agentica/create-agentica@524

@agentica/rpc

npm i https://pkg.pr.new/wrtnlabs/agentica/@agentica/rpc@524

@agentica/vector-selector

npm i https://pkg.pr.new/wrtnlabs/agentica/@agentica/vector-selector@524

commit: 8936c85

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 (StreamResponse and NonStreamResponse) in AgenticaResponseEvent to handle both streaming and non-streaming responses through a unified response property
  • Added token usage tracking and response event dispatching for non-streaming mode in getChatCompletionFunction
  • Updated test code to use the new event.response.stream discriminator and event.response.data accessor
  • Updated @samchon/openapi from 6.0.0 to 6.0.1 and typia from 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 createResponseEvent factory function instead of inline object creation for consistency. The factory is imported from ../factory and provides a centralized way to create response events, ensuring consistency across the codebase. This would also eliminate the need for the type assertion on the body property.
    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;
Copy link

Copilot AI Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
body: OpenAI.ChatCompletionCreateParamsStreaming;
body:
| OpenAI.ChatCompletionCreateParamsStreaming
| OpenAI.ChatCompletionCreateParamsNonStreaming;

Copilot uses AI. Check for mistakes.
@sunrabbit123 sunrabbit123 merged commit 2f8aece into main Jan 28, 2026
19 checks passed
@sunrabbit123 sunrabbit123 deleted the fix/stream branch January 28, 2026 07:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants