-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathtypes.ts
More file actions
237 lines (210 loc) · 7.49 KB
/
types.ts
File metadata and controls
237 lines (210 loc) · 7.49 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright (c) 2025 The Linux Foundation and each contributor.
// SPDX-License-Identifier: MIT
/* eslint-disable @typescript-eslint/no-explicit-any */
import type { DataStreamWriter } from 'ai';
import type { Pool } from 'pg';
import { z } from 'zod';
import { RouterDecisionAction } from './enums';
import type { ChatResponse } from '~~/server/repo/chat.repo';
// ============================================
// Pipe Instruction Types
// ============================================
// Schema for individual pipe execution
export const pipeExecutionSchema = z.object({
id: z.string().describe('Unique identifier for referencing this pipe'),
name: z.string().describe('Actual pipe name to execute'),
inputs: z.record(z.any()).describe('Input parameters for the pipe'),
});
// Schema for output column mapping - either direct mapping or formula
export const outputColumnSchema = z.discriminatedUnion('type', [
// Direct column mapping from a pipe
z.object({
type: z.literal('direct'),
name: z.string().describe('Column name in final output'),
pipeId: z.string().describe('Which pipe this column comes from'),
sourceColumn: z.string().describe('Original column name from that pipe'),
}),
// Formula column that computes a value from other columns
z.object({
type: z.literal('formula'),
name: z.string().describe('Column name in final output'),
formula: z.string().describe('JavaScript expression to compute the value'),
dependencies: z
.array(
z.object({
variable: z.string().describe("Variable name to use in formula (e.g., 'a', 'b')"),
pipeId: z.string().describe('Which pipe this value comes from'),
sourceColumn: z.string().describe('Original column name from that pipe'),
}),
)
.describe('Variables that the formula depends on'),
}),
]);
// Schema for pipe instructions
export const pipeInstructionsSchema = z.object({
pipes: z.array(pipeExecutionSchema).describe('List of pipes to execute'),
output: z.array(outputColumnSchema).describe('Define the final output columns'),
});
// TypeScript types inferred from schemas
export type PipeExecution = z.infer<typeof pipeExecutionSchema>;
export type OutputColumn = z.infer<typeof outputColumnSchema>;
export type PipeInstructions = z.infer<typeof pipeInstructionsSchema>;
// ============================================
// Text-to-SQL Instruction Types
// ============================================
// Schema for text-to-SQL instructions
export const textToSqlInstructionsSchema = z.string();
// TypeScript type for text-to-SQL instructions
export type TextToSqlInstructions = z.infer<typeof textToSqlInstructionsSchema>;
// ============================================
// Unified Instructions Type
// ============================================
// Discriminated union for all instruction types
export const instructionsSchema = z.discriminatedUnion('type', [
z.object({
type: z.literal('query'),
instructions: textToSqlInstructionsSchema,
}),
z.object({
type: z.literal('pipes'),
instructions: pipeInstructionsSchema,
}),
]);
// TypeScript type for unified instructions
export type Instructions = z.infer<typeof instructionsSchema>;
// ============================================
// Agent Output Types
// ============================================
// Router agent output schema
export const routerOutputSchema = z.object({
next_action: z.enum([
RouterDecisionAction.STOP,
RouterDecisionAction.CREATE_QUERY,
RouterDecisionAction.PIPES,
RouterDecisionAction.ASK_CLARIFICATION,
]),
reasoning: z.string().describe('Maximum 2 sentences explaining the decision'),
reformulated_question: z.string().describe('Enhanced query with all parameters'),
tools: z.array(z.string()).describe('Tools needed for next agent'),
clarification_question: z
.string()
.optional()
.nullable()
.describe('Question to ask user when next_action is ASK_CLARIFICATION'),
});
// Pipe agent output schema
export const pipeOutputSchema = z.object({
explanation: z.string().describe('Brief explanation of why these pipes answer the question'),
instructions: pipeInstructionsSchema.describe(
'Instructions describing how to execute pipes and combine results',
),
});
// Auditor agent output schema
export const auditorOutputSchema = z.object({
is_valid: z.boolean().describe('true = data answers question, false = needs retry'),
reasoning: z.string().describe('2-3 sentences explaining the validation decision'),
feedback_to_router: z
.string()
.nullable()
.optional()
.describe('If invalid, specific guidance for router to fix the issue'),
summary: z.string().nullable().optional().describe('If valid, user-friendly summary of findings'),
});
// TypeScript types for agent outputs
export type RouterOutput = z.infer<typeof routerOutputSchema> & { usage?: any };
export type PipeOutput = z.infer<typeof pipeOutputSchema> & { usage?: any };
export type AuditorOutput = z.infer<typeof auditorOutputSchema> & { usage?: any };
// ============================================
// Agent Input Types
// ============================================
export interface ChatMessage {
content: string;
role: string;
}
export interface RouterAgentInput {
model: any; // Bedrock model instance
messages: ChatMessage[];
tools: Record<string, any>;
toolsOverview: string;
date: string;
projectName: string;
pipe: string;
parametersString: string;
segmentId: string | null;
previousWasClarification?: boolean;
}
export interface PipeAgentStreamInput extends Omit<PipeAgentInput, 'model' | 'tools' | 'date'> {
dataStream: DataStreamWriter;
date: string;
responseData: ChatResponse;
routerOutput: RouterOutput;
}
export interface PipeAgentInput {
model: any; // Bedrock model instance
messages: ChatMessage[];
tools: Record<string, any>; // Filtered pipe tools based on router decision
date: string;
projectName: string;
pipe: string;
parametersString: string;
segmentId: string | null;
reformulatedQuestion: string;
toolNames: string[]; // Array of tool names from router
}
export interface DataCopilotQueryInput {
currentQuestion: string; // The current user question
segmentId?: string;
projectName?: string;
pipe: string;
parameters?: Record<string, unknown>;
conversationId: string;
insightsDbPool: Pool;
userEmail: string;
dataStream: DataStreamWriter; // DataStreamWriter from AI SDK
}
export interface SqlErrorContext {
errorMessage: string;
previousQuery: string;
attemptNumber: number;
}
export interface TextToSqlAgentInput {
messages: ChatMessage[];
date: string;
projectName: string;
pipe: string;
parametersString: string;
segmentId: string;
reformulatedQuestion: string;
errorContext?: SqlErrorContext;
}
export interface TextToSqlAgentStreamInput {
messages: ChatMessage[];
date: string;
projectName: string;
pipe: string;
parametersString: string;
segmentId: string;
reformulatedQuestion: string;
dataStream: any;
errorContext?: SqlErrorContext;
}
export interface AuditorAgentInput {
model: any;
messages: ChatMessage[];
originalQuestion: string;
reformulatedQuestion: string;
dataSummary: import('./utils/data-summary').DataSummary;
attemptNumber: number;
previousFeedback?: string;
}
export interface AgentResponseCompleteParams {
userPrompt: string;
responseData: ChatResponse;
routerOutput: RouterOutput;
pipeInstructions?: PipeInstructions;
sqlQuery?: string;
conversationId?: string;
insightsDbPool: Pool;
userEmail: string;
dataStream: DataStreamWriter;
}