File size: 2,232 Bytes
89af3b2
 
 
 
816fa8a
 
89af3b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { call as queryRequest } from "../query/queryApi";
import type { ChatResult, Result } from "../query/queryApi";
import type { AssistantLogTurn } from "./AssistantPage";

export { refusalSentence } from "../query/queryApi";

export type DockSurface = "data" | "sheet" | "automation" | "field";
export interface DockContext {
  surface: DockSurface;
  database?: string;
  databases?: string[];
}
export interface DockHistory { turns: AssistantLogTurn[]; hasMore: boolean; }
export type DockTurnInput = DockContext & { question: string; model?: string };
export interface DockExchange {
  question: string;
  answer: string;
  context: DockContext;
}
export interface DockExchangeReceipt {
  ok: true;
  userTurnId: string;
  assistantTurnId: string;
}
export type DockRequest = typeof queryRequest;

export function fetchDockHistory(request: DockRequest = queryRequest): Promise<Result<DockHistory>> {
  return request("/assistant/log?limit=50", {}, (body) => body as DockHistory);
}

export function sendDockTurn(input: DockTurnInput, request: DockRequest = queryRequest): Promise<Result<ChatResult>> {
  const sources = [...new Set((input.databases || []).filter(Boolean))];
  const database = sources.length
    ? (sources.includes(input.database || "") ? input.database! : sources[0])
    : input.database || "";
  if (!sources.length && database) sources.push(database);
  const dock: DockContext = { surface: input.surface,
    ...(database ? { database } : {}),
    ...(input.databases ? { databases: sources } : {}),
  };
  return request("/query/chat", {
    method: "POST", headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ question: input.question, database, sources, model: input.model || "auto", dock }),
  }, (body) => body as ChatResult);
}

export function logExchange(exchange: DockExchange, request: DockRequest = queryRequest): Promise<Result<DockExchangeReceipt>> {
  return request("/assistant/exchange", {
    method: "POST", headers: { "Content-Type": "application/json" },
    body: JSON.stringify(exchange),
  }, (body) => body as DockExchangeReceipt);
}

export const dockClient = { fetchDockHistory, sendDockTurn, logExchange };
export type DockClient = typeof dockClient;