File size: 1,033 Bytes
f59fbe2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { create } from "zustand";
import type { DisplayBlock, ApprovalResponse } from "shared/legacy-sdk";
import { bridge } from "@/services";

export interface ApprovalRequest {
  id: string;
  tool_call_id: string;
  sender: string;
  action: string;
  description: string;
  display: DisplayBlock[];
}

interface ApprovalState {
  pending: ApprovalRequest[];
  addRequest: (request: ApprovalRequest) => void;
  removeRequest: (id: string) => void;
  respondToRequest: (id: string, response: ApprovalResponse) => Promise<void>;
  clearRequests: () => void;
}

export const useApprovalStore = create<ApprovalState>((set, get) => ({
  pending: [],

  addRequest: (request) => {
    set((s) => ({ pending: [...s.pending, request] }));
  },

  removeRequest: (id) => {
    set((s) => ({ pending: s.pending.filter((r) => r.id !== id) }));
  },

  respondToRequest: async (id, response) => {
    await bridge.respondApproval(id, response);
    get().removeRequest(id);
  },

  clearRequests: () => {
    set({ pending: [] });
  },
}));