Skip to content

Commit

Permalink
feat: add support for overriding user in conversation creation
Browse files Browse the repository at this point in the history
  • Loading branch information
arturoliduena committed Jan 23, 2025
1 parent 4146dd6 commit ddf4c61
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,15 @@ export class ObservabilityAIAssistantClient {
return response.hits.hits[0];
};

private getConversationUpdateValues = (lastUpdated: string) => {
private getConversationUpdateValues = (
lastUpdated: string,
overrideUser?: { id?: string; name: string }
) => {
return {
conversation: {
last_updated: lastUpdated,
},
user: this.dependencies.user,
user: overrideUser ? overrideUser : this.dependencies.user,
namespace: this.dependencies.namespace,
};
};
Expand Down Expand Up @@ -176,6 +179,7 @@ export class ObservabilityAIAssistantClient {
title: predefinedTitle,
conversationId: predefinedConversationId,
disableFunctions = false,
overrideUser,
}: {
messages: Message[];
connectorId: string;
Expand All @@ -193,6 +197,7 @@ export class ObservabilityAIAssistantClient {
| {
except: string[];
};
overrideUser?: { id?: string; name: string };
}): Observable<Exclude<StreamingChatResponseEvent, ChatCompletionErrorEvent>> => {
return new LangTracer(context.active()).startActiveSpan(
'complete',
Expand Down Expand Up @@ -402,18 +407,21 @@ export class ObservabilityAIAssistantClient {
}

return from(
this.create({
'@timestamp': new Date().toISOString(),
conversation: {
title,
id: conversationId,
token_count: tokenCountResult,
this.create(
{
'@timestamp': new Date().toISOString(),
conversation: {
title,
id: conversationId,
token_count: tokenCountResult,
},
public: !!isPublic,
labels: {},
numeric_labels: {},
messages: initialMessagesWithAddedMessages,
},
public: !!isPublic,
labels: {},
numeric_labels: {},
messages: initialMessagesWithAddedMessages,
})
overrideUser
)
).pipe(
map((conversation): ConversationCreateEvent => {
return {
Expand Down Expand Up @@ -624,7 +632,10 @@ export class ObservabilityAIAssistantClient {
return updatedConversation;
};

create = async (conversation: ConversationCreateRequest): Promise<Conversation> => {
create = async (
conversation: ConversationCreateRequest,
overrideUser?: { id?: string; name: string }
): Promise<Conversation> => {
const now = new Date().toISOString();

const createdConversation: Conversation = merge(
Expand All @@ -634,7 +645,7 @@ export class ObservabilityAIAssistantClient {
'@timestamp': now,
conversation: { id: conversation.conversation.id || v4() },
},
this.getConversationUpdateValues(now)
this.getConversationUpdateValues(now, overrideUser)
);

await this.dependencies.esClient.asInternalUser.index({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ async function executor(

const resources = await initResources(request);
const client = await resources.service.getClient({ request, scopes: ['observability'] });

const rulesClient = await (
await resources.plugins.alerting.start()
).getRulesClientWithRequest(request);

const rule = await rulesClient.get({
id: execOptions.params.rule.id,
});

const functionClient = await resources.service.getFunctionClient({
signal: new AbortController().signal,
resources,
Expand Down Expand Up @@ -250,6 +259,7 @@ If available, include the link of the conversation at the end of your answer.`
signal: new AbortController().signal,
kibanaPublicUrl: (await resources.plugins.core.start()).http.basePath.publicBaseUrl,
instructions: [backgroundInstruction],
overrideUser: rule.createdBy ? { name: rule.createdBy } : undefined,
messages: [
{
'@timestamp': new Date().toISOString(),
Expand Down

0 comments on commit ddf4c61

Please sign in to comment.