Skip to content
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

Fix tool calls in log detail #765

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,12 @@ describe('ChainStreamConsumer', () => {
messages: [
{
role: MessageRole.assistant,
content: 'text response',
content: [
{
type: ContentType.text,
text: 'text response',
},
],
toolCalls: [],
},
],
Expand Down Expand Up @@ -165,7 +170,12 @@ describe('ChainStreamConsumer', () => {
messages: [
{
role: MessageRole.assistant,
content: '{\n "object": "response"\n}',
content: [
{
type: ContentType.text,
text: '{\n "object": "response"\n}',
},
],
toolCalls: [],
},
],
Expand Down Expand Up @@ -250,14 +260,13 @@ describe('ChainStreamConsumer', () => {
config: step.conversation.config,
response: response,
messages: [
{
role: MessageRole.assistant,
content: 'text response',
toolCalls: [],
},
{
role: MessageRole.assistant,
content: [
{
type: ContentType.text,
text: 'text response',
},
{
type: ContentType.toolCall,
toolCallId: 'tool-call-id',
Expand Down
57 changes: 40 additions & 17 deletions packages/core/src/services/chains/ChainStreamConsumer/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ContentType,
MessageContent,
MessageRole,
ToolRequestContent,
} from '@latitude-data/compiler'
Expand Down Expand Up @@ -56,36 +57,58 @@ export class ChainStreamConsumer {
config: Config
}) {
let messages: Message[] = []
let message: Message | undefined = undefined

if (response.text.length > 0) {
messages.push({
message = {
role: MessageRole.assistant,
content: response.text,
content: [
{
type: ContentType.text,
text: response.text,
},
],
toolCalls: [],
})
}
}

if (response.streamType === 'object' && response.object) {
messages.push({
message = {
role: MessageRole.assistant,
content: objectToString(response.object),
content: [
{
type: ContentType.text,
text: objectToString(response.object),
},
],
toolCalls: [],
})
}
}

if (response.streamType === 'text' && response.toolCalls.length > 0) {
messages.push({
role: MessageRole.assistant,
content: response.toolCalls.map((toolCall) => {
return {
type: ContentType.toolCall,
toolCallId: toolCall.id,
toolName: toolCall.name,
args: toolCall.arguments,
} as ToolRequestContent
}),
toolCalls: response.toolCalls,
const content = response.toolCalls.map((toolCall) => {
return {
type: ContentType.toolCall,
toolCallId: toolCall.id,
toolName: toolCall.name,
args: toolCall.arguments,
} as ToolRequestContent
})

if (message) {
message.content = (message.content as MessageContent[]).concat(content)
message.toolCalls = response.toolCalls
} else {
message = {
role: MessageRole.assistant,
content: content,
toolCalls: response.toolCalls,
}
}
}

if (message) {
messages.push(message)
}

enqueueChainEvent(controller, {
Expand Down
9 changes: 4 additions & 5 deletions packages/core/src/services/chains/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,14 +662,13 @@ describe('runChain', () => {
data: expect.objectContaining({
type: ChainEventTypes.Complete,
messages: [
{
role: MessageRole.assistant,
content: 'assistant message',
toolCalls: [],
},
{
role: MessageRole.assistant,
content: [
{
type: ContentType.text,
text: 'assistant message',
},
{
type: ContentType.toolCall,
toolCallId: 'tool-call-id',
Expand Down
14 changes: 12 additions & 2 deletions packages/core/src/services/commits/runDocumentAtCommit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,12 @@ model: gpt-4o
messages: [
{
role: 'assistant',
content: [{ type: 'text', text: 'Fake AI generated text' }],
content: [
{
type: 'text',
text: 'Fake AI generated text',
},
],
},
{
role: 'system',
Expand Down Expand Up @@ -306,7 +311,12 @@ model: gpt-4o
messages: [
{
role: 'assistant',
content: 'Fake AI generated text',
content: [
{
type: 'text',
text: 'Fake AI generated text',
},
],
toolCalls: [],
},
],
Expand Down
Loading
Loading