Repository navigation
Expand file tree
/
Copy pathtypes.ts
More file actions
273 lines (238 loc) · 7.93 KB
/
Copy pathtypes.ts
File metadata and controls
273 lines (238 loc) · 7.93 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// Domain model shared across the main process, preload bridge, and renderer.
// These types are the single source of truth for entity shapes; the SQLite
// schema (main) and the React UI (renderer) both conform to them.
// ---------------------------------------------------------------------------
// Providers
// ---------------------------------------------------------------------------
// The app is provider-agnostic. Every LLM backend reduces to one of three
// adapter *shapes* plus a deterministic in-process mock used for offline demos
// and as the test backbone. Named vendors (OpenAI, OpenRouter, Gemini's
// OpenAI-compatible endpoint, a self-hosted gateway, etc.) all use the
// `openai-compatible` shape with a configurable base URL.
export type ProviderShape = 'openai-compatible' | 'anthropic' | 'ollama' | 'mock'
// Which backend the key vault is using to store API keys, surfaced to the UI.
// 'os-keychain' is the OS secret store (strongest); 'app-key' is the
// app-managed encrypted fallback for systems without a keychain; 'unavailable'
// means no backend can persist a key.
export type KeyStorageMode = 'os-keychain' | 'app-key' | 'unavailable'
export interface Provider {
id: string
shape: ProviderShape
/** User-facing label, e.g. "OpenRouter" or "Local Ollama". */
name: string
/** Base URL for openai-compatible / ollama shapes. Null for anthropic/mock. */
baseUrl: string | null
/**
* Reserved for future per-provider OAuth flows. Always false in v1 — the
* column exists so the schema does not need a migration when OAuth lands.
*/
isOauth: boolean
createdAt: string
}
export interface CreateProviderInput {
shape: ProviderShape
name: string
baseUrl?: string | null
}
export interface UpdateProviderInput {
id: string
name?: string
baseUrl?: string | null
}
// ---------------------------------------------------------------------------
// Templates
// ---------------------------------------------------------------------------
export interface Template {
id: string
name: string
description: string | null
/** The markdown template content — becomes the system prompt during a hatch. */
content: string
openingMessage: string
isBuiltin: boolean
createdAt: string
updatedAt: string
}
export interface CreateTemplateInput {
name: string
description?: string | null
content: string
openingMessage?: string
}
export interface UpdateTemplateInput {
id: string
name?: string
description?: string | null
content?: string
openingMessage?: string
}
// ---------------------------------------------------------------------------
// Sessions
// ---------------------------------------------------------------------------
export type SessionStatus = 'in_progress' | 'completed'
export interface TokenUsage {
input: number
output: number
total: number
}
export interface Session {
id: string
name: string
templateId: string | null
/** Frozen copy of the template content at session start. */
templateSnapshot: string
openingMessage: string
providerId: string | null
model: string
status: SessionStatus
tokenUsage: TokenUsage
/**
* Effective context window (tokens) for this session, resolved at creation:
* a user override from the Advanced options, else the model's reported
* window. Null means unknown — the app uses DEFAULT_CONTEXT_WINDOW and labels
* usage "estimated".
*/
contextWindow: number | null
/** Sampling temperature override. Null = use the provider's default. */
temperature: number | null
createdAt: string
completedAt: string | null
}
export interface CreateSessionInput {
name: string
templateId: string
providerId: string
model: string
/** Overrides the template's opening message when set. */
openingMessage?: string
/** Effective context window resolved by the UI (override or model-reported). */
contextWindow?: number
/** Sampling temperature override (provider default when omitted). */
temperature?: number
}
export interface UpdateSessionInput {
id: string
name?: string
status?: SessionStatus
}
// ---------------------------------------------------------------------------
// Messages & tool calls
// ---------------------------------------------------------------------------
export type MessageRole = 'system' | 'user' | 'assistant' | 'tool'
export type ToolName = 'write_file' | 'read_file' | 'delete_file'
export interface ToolCall {
/** Provider-assigned id, used to correlate the tool result message. */
id: string
name: ToolName
arguments: Record<string, unknown>
}
export interface Message {
id: string
sessionId: string
role: MessageRole
content: string
/** Present on assistant messages that requested tool calls. */
toolCalls: ToolCall[] | null
/** Present on tool-result messages, links back to the ToolCall.id. */
toolCallId: string | null
tokens: number | null
createdAt: string
}
// ---------------------------------------------------------------------------
// File artifacts (in-memory during a hatch, persisted to SQLite)
// ---------------------------------------------------------------------------
export interface FileArtifact {
id: string
sessionId: string
filename: string
content: string
sizeBytes: number
createdAt: string
updatedAt: string
/** Soft-delete marker — set when the bot deletes a file (e.g. BOOTSTRAP.md). */
deletedAt: string | null
}
// ---------------------------------------------------------------------------
// LLM layer (unified interface all provider adapters implement, main-side)
// ---------------------------------------------------------------------------
/** Minimal JSON Schema subtype used for tool parameter definitions. */
export interface JsonSchema {
type: string
properties?: Record<string, JsonSchema>
items?: JsonSchema
required?: string[]
description?: string
enum?: string[]
[key: string]: unknown
}
export interface ToolDefinition {
name: ToolName
description: string
parameters: JsonSchema
}
export interface LlmMessage {
role: MessageRole
content: string
toolCalls?: ToolCall[]
toolCallId?: string
}
export interface ModelInfo {
id: string
/** Provider label the model belongs to (for grouped display). */
provider?: string
/**
* Max context window in tokens, when the provider's API reports one
* (Anthropic max_input_tokens, OpenRouter/Groq/vLLM-style list fields,
* Ollama /api/show). Undefined when the provider doesn't expose it — the app
* then falls back to DEFAULT_CONTEXT_WINDOW and labels usage "estimated".
*/
contextWindow?: number
}
export type LlmStreamEvent =
| { type: 'text'; text: string }
| { type: 'tool_call'; toolCall: ToolCall }
| { type: 'usage'; usage: TokenUsage }
| { type: 'done'; finishReason: string }
| { type: 'error'; message: string; kind: LlmErrorKind }
export interface SendMessageParams {
model: string
messages: LlmMessage[]
tools?: ToolDefinition[]
/** Sampling temperature; omitted = provider default. Forwarded as-is — the
* provider rejects unsupported values and that error surfaces to the user. */
temperature?: number
onEvent?: (event: LlmStreamEvent) => void
signal?: AbortSignal
}
export interface LlmResponse {
content: string
toolCalls: ToolCall[]
usage: TokenUsage
finishReason: string
}
export interface LlmProvider {
readonly shape: ProviderShape
sendMessage(params: SendMessageParams): Promise<LlmResponse>
listModels(): Promise<ModelInfo[]>
validateConnection(): Promise<boolean>
}
export type LlmErrorKind =
| 'rate_limit'
| 'auth'
| 'network'
| 'context_length'
| 'bad_response'
| 'unknown'
// ---------------------------------------------------------------------------
// Conversation engine (phase 3)
// ---------------------------------------------------------------------------
export type ConversationState =
| 'initializing'
| 'sending_opening'
| 'waiting_for_bot'
| 'waiting_for_user'
| 'processing_tool_call'
| 'completing'
| 'completed'
| 'error'
| 'paused'