puter icon indicating copy to clipboard operation
puter copied to clipboard

`ChatResponse.message` should have the same type as `ChatMessage`

Open reynaldichernando opened this issue 3 months ago • 4 comments

Right now here is what we have

// ChatMessage
interface ChatMessage {
    role: 'system' | 'assistant' | 'user' | 'function' | 'tool';
    content: string | ContentObject[];
    tool_call_id?: string;
}

...

interface ChatResponse {
    message: {
        role: string;
        content: string;
        tool_calls?: ToolCall[];
    };
}

the difference in type causes intellisense to throw error

Type '{ role: string; content: string; tool_calls?: ToolCall[] | undefined; }' is not assignable to type 'ChatMessage'.
  Types of property 'role' are incompatible.
    Type 'string' is not assignable to type '"function" | "user" | "system" | "assistant" | "tool"'.ts(2322)

For now the quick fix in user's end is to type cast it, but we should probably fix on our lib

reynaldichernando avatar Oct 02 '25 13:10 reynaldichernando

This is in the context of getting the chat response, and feeding it back into the conversation array

reynaldichernando avatar Oct 02 '25 13:10 reynaldichernando

If I'm understanding this correctly you basically just want

interface ChatResponse {
    message: ChatMessage
}

right?

ProgrammerIn-wonderland avatar Oct 05 '25 06:10 ProgrammerIn-wonderland

could you provide test code in typescript that should work under ideal conditions?

ProgrammerIn-wonderland avatar Oct 05 '25 06:10 ProgrammerIn-wonderland

hi @ProgrammerIn-wonderland , not sure how i missed this issue

If I'm understanding this correctly you basically just want

yes that's right, ideally it's the same type so appending it inside the ChatMessage[] list just works without type casting

this is the sample typescript code, similar to what i'm doing, the ChatResponse.message i have to typecast it as ChatMessage

let response: ChatResponse = {
  message: {
    role: "system",
    content: "hehe",
  },
};

const prevMessages: ChatMessage[] = [];

const newMessages: ChatMessage[] = [
  ...prevMessages,
  // response.message, // this is broken without typecasting
  response.message as ChatMessage,
];

feel free to let me know if you need more detail

reynaldichernando avatar Oct 21 '25 07:10 reynaldichernando