Skip to content

Commit 1d9fcd3

Browse files
authored
feat: new chat protocol extension points (#170)
Tested working.
1 parent 488b15a commit 1d9fcd3

File tree

11 files changed

+34
-13
lines changed

11 files changed

+34
-13
lines changed

packages/chat-component/src/components/chat-controller.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export class ChatController implements ReactiveController {
138138
followingSteps.push(...(processedText.arrays[1] as string[]));
139139
followupQuestions.push(...(processedText.arrays[2] as string[]));
140140
thoughts = generatedResponse.context?.thoughts ?? '';
141-
dataPoints = generatedResponse.context?.data_points ?? [];
141+
dataPoints = generatedResponse.context?.data_points?.text ?? [];
142142

143143
await updateChatWithMessageOrChunk(messageToUpdate, false);
144144
}

packages/chat-component/src/core/parser/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export async function parseStreamedMessages({
4949

5050
const { content, context } = chunk.choices[0].delta;
5151
if (context?.data_points) {
52-
updatedEntry.dataPoints = context.data_points ?? [];
52+
updatedEntry.dataPoints = context.data_points?.text ?? [];
5353
updatedEntry.thoughts = context.thoughts ?? '';
5454

5555
continue;

packages/chat-component/src/types.d.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ declare interface BotResponseChunk {
8282

8383
declare type BotResponseMessage = Message & {
8484
context?: Record<string, any> & {
85-
data_points?: string[];
85+
data_points?: {
86+
text?: string[];
87+
images?: string[];
88+
};
8689
thoughts?: string;
8790
};
8891
session_state?: Record<string, any>;

packages/search/src/lib/approaches/approach.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ export interface ApproachResponseChunk {
1919

2020
export type ApproachResponseMessage = Message & {
2121
context?: Record<string, any> & {
22-
data_points?: string[];
22+
data_points?: {
23+
text?: string[];
24+
images?: string[];
25+
};
2326
thoughts?: string;
2427
};
2528
session_state?: Record<string, any>;

packages/search/src/lib/approaches/ask-read-retrieve-read.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ export class AskReadRetrieveRead extends ApproachBase implements AskApproach {
110110
content: answer,
111111
role: 'assistant' as const,
112112
context: {
113-
data_points: searchResults,
113+
data_points: {
114+
text: searchResults,
115+
},
114116
thoughts: htmlTracer.getAndResetLog(),
115117
},
116118
},

packages/search/src/lib/approaches/ask-retrieve-then-read.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ export class AskRetrieveThenRead extends ApproachBase implements AskApproach {
7979
role: 'assistant' as const,
8080
content: chatCompletion.choices[0].message.content ?? '',
8181
context: {
82-
data_points: results,
82+
data_points: {
83+
text: results,
84+
},
8385
thoughts: `Question:<br>${query}<br><br>Prompt:<br>${messageToDisplay.replace('\n', '<br>')}`,
8486
},
8587
},

packages/search/src/lib/approaches/chat-read-retrieve-read.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ export class ChatReadRetrieveRead extends ApproachBase implements ChatApproach {
7878
content: chatContent,
7979
role: 'assistant',
8080
context: {
81-
data_points: dataPoints,
81+
data_points: {
82+
text: dataPoints,
83+
},
8284
thoughts: thoughts,
8385
},
8486
},
@@ -108,7 +110,7 @@ export class ChatReadRetrieveRead extends ApproachBase implements ChatApproach {
108110
content: chunk.choices[0].delta.content ?? '',
109111
role: 'assistant' as const,
110112
context: {
111-
data_points: id === 0 ? dataPoints : undefined,
113+
data_points: id === 0 ? { text: dataPoints } : undefined,
112114
thoughts: id === 0 ? thoughts : undefined,
113115
},
114116
},

packages/search/src/plugins/schemas.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,17 @@ export const messageSchema = {
5959
type: 'object',
6060
properties: {
6161
data_points: {
62-
type: 'array',
63-
items: { type: 'string' },
62+
type: 'object',
63+
properties: {
64+
text: {
65+
type: 'array',
66+
items: { type: 'string' },
67+
},
68+
images: {
69+
type: 'array',
70+
items: { type: 'string' },
71+
},
72+
},
6473
},
6574
thoughts: { type: 'string' },
6675
},

tests/e2e/hars/default-ask-response.har

+1-1
Large diffs are not rendered by default.

tests/e2e/hars/default-chat-response-nostream.har

+1-1
Large diffs are not rendered by default.

tests/e2e/hars/error-chat-response-stream.har

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"content": {
6262
"size": -1,
6363
"mimeType": "application/x-ndjson",
64-
"text": "{\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"role\":\"assistant\",\"context\":{\"data_points\":[],\"thoughts\":\"Here's how to do it: 1.\"}},\"finish_reason\":null}],\"object\":\"chat.completion.chunk\"}\n{\"choices\":[{\"index\":0,\"delta\":{\"content\":\"To\",\"role\":\"assistant\",\"context\":{}},\"finish_reason\":null}],\"object\":\"chat.completion.chunk\"}\n{\"choices\":[{\"index\":0,\"delta\":{\"content\":\" search\",\"role\":\"assistant\",\"context\":{}},\"finish_reason\":null}],\"object\":\"chat.completion.chunk\"}\n{\"choices\":[{\"index\":0,\"delta\":{\"content\":\" and\",\"role\":\"assistant\",\"context\":{}},\"finish_reason\":null}],\"object\":\"chat.completion.chunk\"}\n{\"choices\":[{\"index\":0,\"delta\":{\"content\":\" book\",\"role\":\"assistant\",\"context\":{}},\"finish_reason\":null}],\"object\":\"chat.completion.chunk\"}\n{\"choices\":[{\"index\":0,\"delta\":{\"content\":\" rentals\",\"role\":\"assistant\",\"context\":{}},\"finish_reason\":\"content_filter\"}],\"object\":\"chat.completion.chunk\"}\n{\"choices\":[{\"index\":0,\"delta\":{\"content\":\" on\",\"role\":\"assistant\",\"context\":{}},\"finish_reason\":null}],\"object\":\"chat.completion.chunk\"}\n{\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"role\":\"assistant\",\"context\":{}},\"finish_reason\":\"stop\"}],\"object\":\"chat.completion.chunk\"}\n"
64+
"text": "{\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"role\":\"assistant\",\"context\":{\"data_points\":{},\"thoughts\":\"Here's how to do it: 1.\"}},\"finish_reason\":null}],\"object\":\"chat.completion.chunk\"}\n{\"choices\":[{\"index\":0,\"delta\":{\"content\":\"To\",\"role\":\"assistant\",\"context\":{}},\"finish_reason\":null}],\"object\":\"chat.completion.chunk\"}\n{\"choices\":[{\"index\":0,\"delta\":{\"content\":\" search\",\"role\":\"assistant\",\"context\":{}},\"finish_reason\":null}],\"object\":\"chat.completion.chunk\"}\n{\"choices\":[{\"index\":0,\"delta\":{\"content\":\" and\",\"role\":\"assistant\",\"context\":{}},\"finish_reason\":null}],\"object\":\"chat.completion.chunk\"}\n{\"choices\":[{\"index\":0,\"delta\":{\"content\":\" book\",\"role\":\"assistant\",\"context\":{}},\"finish_reason\":null}],\"object\":\"chat.completion.chunk\"}\n{\"choices\":[{\"index\":0,\"delta\":{\"content\":\" rentals\",\"role\":\"assistant\",\"context\":{}},\"finish_reason\":\"content_filter\"}],\"object\":\"chat.completion.chunk\"}\n{\"choices\":[{\"index\":0,\"delta\":{\"content\":\" on\",\"role\":\"assistant\",\"context\":{}},\"finish_reason\":null}],\"object\":\"chat.completion.chunk\"}\n{\"choices\":[{\"index\":0,\"delta\":{\"content\":\"\",\"role\":\"assistant\",\"context\":{}},\"finish_reason\":\"stop\"}],\"object\":\"chat.completion.chunk\"}\n"
6565
},
6666
"headersSize": -1,
6767
"bodySize": -1,

0 commit comments

Comments
 (0)