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

chore(ui): add feedback_replace to ui, update listeners w/ feedback_type #2804

Closed
wants to merge 7 commits into from
Closed
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 @@ -5,6 +5,8 @@ import {
FeedbackCreateRes,
FeedbackPurgeReq,
FeedbackPurgeRes,
FeedbackReplaceReq,
FeedbackReplaceRes,
TraceCallsDeleteReq,
TraceCallUpdateReq,
TraceRefsReadBatchReq,
Expand All @@ -23,7 +25,11 @@ export class TraceServerClient extends DirectTraceServerClient {
}> = [];
private onDeleteListeners: Array<() => void>;
private onRenameListeners: Array<() => void>;
private onFeedbackListeners: Record<string, Array<() => void>>;
// weave_ref -> feedback_type -> callback
private onFeedbackListeners: Record<
string,
Record<string, Array<() => void>>
>;

constructor(baseUrl: string) {
super(baseUrl);
Expand All @@ -34,6 +40,8 @@ export class TraceServerClient extends DirectTraceServerClient {
this.onFeedbackListeners = {};
}

private FEEDBACK_TYPE_DEFAULT = '';

/**
* Registers a callback to be called when a delete operation occurs.
* This method is purely for local notification within the client
Expand All @@ -60,20 +68,25 @@ export class TraceServerClient extends DirectTraceServerClient {
}
public registerOnFeedbackListener(
weaveRef: string,
callback: () => void
callback: () => void,
feedbackType?: string
): () => void {
const feedbackTypeResolved = feedbackType ?? this.FEEDBACK_TYPE_DEFAULT;
if (!(weaveRef in this.onFeedbackListeners)) {
this.onFeedbackListeners[weaveRef] = [];
this.onFeedbackListeners[weaveRef] = {};
}
if (!(feedbackTypeResolved in this.onFeedbackListeners[weaveRef])) {
this.onFeedbackListeners[weaveRef][feedbackTypeResolved] = [];
}
this.onFeedbackListeners[weaveRef].push(callback);
this.onFeedbackListeners[weaveRef][feedbackTypeResolved].push(callback);
return () => {
const newListeners = this.onFeedbackListeners[weaveRef].filter(
listener => listener !== callback
);
const newListeners = this.onFeedbackListeners[weaveRef][
feedbackTypeResolved
].filter(listener => listener !== callback);
if (newListeners.length) {
this.onFeedbackListeners[weaveRef] = newListeners;
this.onFeedbackListeners[weaveRef][feedbackTypeResolved] = newListeners;
} else {
delete this.onFeedbackListeners[weaveRef];
delete this.onFeedbackListeners[weaveRef][feedbackTypeResolved];
}
};
}
Expand All @@ -94,7 +107,13 @@ export class TraceServerClient extends DirectTraceServerClient {

public feedbackCreate(req: FeedbackCreateReq): Promise<FeedbackCreateRes> {
const res = super.feedbackCreate(req).then(createRes => {
const listeners = this.onFeedbackListeners[req.weave_ref] ?? [];
const listeners =
Copy link
Collaborator

Choose a reason for hiding this comment

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

for here and in replace, i think what you want to hit all the listeners for weave_ref + feedback_type AND weave_ref + ''.

this.onFeedbackListeners[req.weave_ref]?.[req.feedback_type] ?? [];
listeners.push(
...(this.onFeedbackListeners?.[req.weave_ref]?.[
this.FEEDBACK_TYPE_DEFAULT
] ?? [])
);
listeners.forEach(listener => listener());
return createRes;
});
Expand All @@ -106,13 +125,31 @@ export class TraceServerClient extends DirectTraceServerClient {
// information about the refs that were modified.
// For now, just call all registered feedback listeners.
for (const listeners of Object.values(this.onFeedbackListeners)) {
listeners.forEach(listener => listener());
for (const listenersForWeaveRef of Object.values(listeners)) {
listenersForWeaveRef.forEach(listener => listener());
}
}
return purgeRes;
});
return res;
}

public feedbackReplace(req: FeedbackReplaceReq): Promise<FeedbackReplaceRes> {
const res = super.feedbackReplace(req).then(replaceRes => {
const listeners =
this.onFeedbackListeners[req.weave_ref]?.[req.feedback_type] ?? [];
// also fire the default feedback type listeners
listeners.push(
...(this.onFeedbackListeners?.[req.weave_ref]?.[
this.FEEDBACK_TYPE_DEFAULT
] ?? [])
);
listeners.forEach(listener => listener());
return replaceRes;
});
return res;
}

public readBatch(req: TraceRefsReadBatchReq): Promise<TraceRefsReadBatchRes> {
return this.requestReadBatch(req);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ export type FeedbackCreateReq = {
weave_ref: string;
feedback_type: string;
payload: Record<string, any>;
annotation_ref?: string;
runnable_ref?: string;
call_ref?: string;
trigger_ref?: string;
};

export type FeedbackCreateSuccess = {
Expand Down Expand Up @@ -190,6 +194,16 @@ export type FeedbackPurgeError = {
detail: string;
};
export type FeedbackPurgeRes = FeedbackPurgeSuccess | FeedbackPurgeError;

export type FeedbackReplaceReq = FeedbackCreateReq & {
feedback_id: string;
};
export type FeedbackReplaceSuccess = {};
export type FeedbackReplaceError = {
detail: string;
};
export type FeedbackReplaceRes = FeedbackCreateRes;

interface TraceObjectsFilter {
base_object_classes?: string[];
object_ids?: string[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
FeedbackPurgeRes,
FeedbackQueryReq,
FeedbackQueryRes,
FeedbackReplaceReq,
FeedbackReplaceRes,
TraceCallReadReq,
TraceCallReadRes,
TraceCallSchema,
Expand Down Expand Up @@ -286,6 +288,13 @@ export class DirectTraceServerClient {
);
}

public feedbackReplace(req: FeedbackReplaceReq): Promise<FeedbackReplaceRes> {
return this.makeRequest<FeedbackReplaceReq, FeedbackReplaceRes>(
'/feedback/replace',
req
);
}

public fileContent(
req: TraceFileContentReadReq
): Promise<TraceFileContentReadRes> {
Expand Down
Loading