forked from nexu-io/open-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.ts
More file actions
565 lines (531 loc) · 19.4 KB
/
Copy pathchat.ts
File metadata and controls
565 lines (531 loc) · 19.4 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
import type { ProjectFile } from './files';
import type { RunResultPackageResponse, RunWorkspace } from './workspaces.js';
import type {
PreviewCommentAttachment,
PreviewCommentMember,
PreviewCommentPosition,
PreviewCommentSelectionKind,
PreviewAnnotationStyle,
PreviewVisualMarkKind,
} from './comments';
import type { ResearchOptions } from './research';
import type { RunContextSelection } from './context.js';
import type { MediaExecutionPolicy } from './media.js';
import type { AppliedPluginSnapshot } from '../plugins/apply.js';
import type { McpAuthMode, McpServerConfig, McpTransport } from './mcp';
import type { TrackingRuntimeType } from '../analytics/public-params.js';
export type ChatRole = 'user' | 'assistant';
export type ChatSessionMode = 'design' | 'chat' | 'plan';
export type ChatCommentSelectionKind = PreviewCommentSelectionKind | 'visual';
export type ByokChatProtocol =
| 'anthropic'
| 'openai'
| 'azure'
| 'google'
| 'ollama'
| 'senseaudio'
| 'aihubmix';
export interface ByokChatProviderConfig {
protocol: ByokChatProtocol;
apiKey: string;
baseUrl?: string;
apiVersion?: string;
/** Explicit run-scoped provider policy for presets that do not require bearer credentials. */
requiresApiKey?: boolean;
/**
* Run-scoped chat model id selected in the chat UI. Forwarded to the daemon
* so BYOK-backed utilities (e.g. memory extraction) can honor the user's
* model picker instead of falling back to a hardcoded default. Optional
* because some presets (e.g. Ollama) infer the model from baseUrl/protocol.
*/
model?: string;
}
export interface ByokMediaDefaults {
imageModel?: string;
videoModel?: string;
speechModel?: string;
speechVoice?: string;
}
export interface ChatRequest {
agentId: string;
message: string;
/** The latest user turn only, used for per-turn telemetry content. */
currentPrompt?: string;
systemPrompt?: string;
projectId?: string | null;
conversationId?: string | null;
sessionMode?: ChatSessionMode;
assistantMessageId?: string | null;
clientRequestId?: string | null;
skillId?: string | null;
// Per-turn skill ids picked via the composer's @-mention popover. The
// daemon concatenates each skill's body into the system prompt for
// this run only — they are NOT persisted on the project. Use this to
// assemble multiple capabilities (e.g. @web-search + @summarize) for
// a single turn without binding the project to one of them.
skillIds?: string[];
designSystemId?: string | null;
attachments?: string[];
commentAttachments?: ChatCommentAttachment[];
model?: string | null;
reasoning?: string | null;
/**
* Run-scoped BYOK provider credentials for the daemon-backed OpenCode
* adapter. The daemon must not persist this object; it is translated into
* child env + OPENCODE_CONFIG_CONTENT for the current run only.
*/
byokProvider?: ByokChatProviderConfig;
/**
* Run-scoped BYOK media defaults selected in the chat UI. The daemon uses
* these to guide OpenCode-backed `od media generate` calls for this run only.
*/
byokMediaDefaults?: ByokMediaDefaults;
/** UI locale selected by the client, used by prompt composition for user-visible generated UI. */
locale?: string;
research?: ResearchOptions;
context?: RunContextSelection;
appliedPluginSnapshotId?: string | null;
/**
* Run-scoped media execution policy. Omitted means current Open Design
* behavior: media generation is enabled and OD may execute its configured
* local providers.
*/
mediaExecution?: MediaExecutionPolicy;
/**
* Ask the selected run agent to emit a short title for this first turn.
* The daemon strips the title marker from visible assistant text and falls
* back to client-side naming when the marker is absent or malformed.
*/
titleGeneration?: {
enabled?: boolean;
};
/**
* Run-scoped tool bundle supplied by an external orchestrator.
* These servers are made available only to the spawned agent for this run
* and are never written into the persistent Settings MCP registry.
*/
toolBundle?: RunScopedToolBundle;
/**
* Optional analytics context for the v2 run_created / run_finished
* events. The daemon never trusts these for behavior — they only
* shape PostHog props. `entryFrom` is one of the documented
* `entry_from` enums; `designSystemRunContext` carries the
* DS-variant context (source counts, brand description length
* bucket, DS origin) used by the design_system_project run shape.
*/
analyticsHints?: ChatAnalyticsHints;
}
export type ChatAnalyticsEntryFrom =
| 'new_project'
| 'chat_composer'
| 'design_system_create'
| 'onboarding_design_system'
| 'regenerate_from_review'
// A turn started by the "Continue the run" affordance on a resumable failed
// run. Lets run_created / run_finished isolate resume-continuations so the
// recovery mechanism's usage and success rate are measurable.
| 'resume_continue'
// A turn started from a preview annotation: `comment` is the comment/board
// pin flow (chat-new-line tool), `mark` is the Mark draw-overlay flow
// (mark-pen tool). Both edit an existing artifact, so isolating them lets the
// dashboard separate annotation-driven runs from plain composer sends.
| 'comment'
| 'mark'
// A turn whose composer was seeded by a guided Next-step action (the
// next-step card prefills a skill/prompt; the run fires on the following
// Send). Best-effort: the pending tag is consumed by the next send.
| 'next_step'
// A turn that submits answers to an inline `<question-form>` clarification
// (the question still being clarified, not a fresh create/edit intent).
| 'question_answer';
export type ChatAnalyticsLengthBucket =
| '0'
| '1_50'
| '51_200'
| '201_500'
| '500_plus';
export type ChatAnalyticsDesignSystemOrigin =
| 'onboarding'
| 'manual_create'
| 'source_url'
| 'github_repo'
| 'local_code'
| 'fig'
| 'assets'
| 'official_preset'
| 'enterprise'
| 'template'
| 'mixed'
| 'unknown';
export interface ChatAnalyticsDesignSystemRunContext {
origin?: ChatAnalyticsDesignSystemOrigin;
sourceCount?: number;
hasBrandDescription?: boolean;
brandDescriptionLengthBucket?: ChatAnalyticsLengthBucket;
githubRepoCount?: number;
localFolderCount?: number;
figFileCount?: number;
assetFileCount?: number;
}
export interface ChatAnalyticsHints {
entryFrom?: ChatAnalyticsEntryFrom;
projectKind?:
| 'prototype'
| 'live_artifact'
| 'slide_deck'
| 'template'
| 'image'
| 'video'
| 'audio'
| 'design_system'
| 'other';
designSystemRunContext?: ChatAnalyticsDesignSystemRunContext;
// Session-dimension run context, computed client-side and stamped onto
// run_created / run_finished so a session's run sequence is analysable
// ("did this session reach an artifact, and on which turn?").
// `turnIndex` is 0-based within the browser analytics session;
// `isFirstRun` === (turnIndex === 0). `hasExistingArtifact` is true when the
// project already had a generated artifact when this run was started
// (project-scoped) — the run is an edit rather than a first creation.
turnIndex?: number;
isFirstRun?: boolean;
hasExistingArtifact?: boolean;
// Per-project run turn index (0-based, project-lifetime on this device):
// "within THIS project, which prompt / follow-up number is this?". Unlike
// `turnIndex` (session-wide, spans all projects and resets each browser
// session), this persists in localStorage keyed by project id. Optional:
// omitted when storage is unavailable (SSR / privacy mode).
projectTurnIndex?: number;
// Active execution runtime for THIS run, computed client-side at launch
// (the only layer that can tell BYOK from amr_cloud). The daemon stamps it
// onto run_created / run_finished, overriding its own BYOK-blind
// derivation. Omitted means "let the daemon keep its derived value".
runtimeType?: TrackingRuntimeType;
// Analytics-only marker that THIS run is the AI-optimize ("enrich") pass on a
// programmatically-extracted design system. The web AI-optimize path sets it;
// the daemon uses it to emit `design_system_enrich_result` and to stamp the
// `ai_refined` enrichment metadata on success. It carries no execution
// semantics — omitting it just means the run is not an enrichment pass.
dsEnrichment?: boolean;
}
export interface RunScopedMcpServerConfig extends Omit<McpServerConfig, 'enabled'> {
/**
* Omitted means enabled for this run. The daemon normalizes run-scoped
* inputs through the same sanitizer as persisted MCP config, but callers
* should not need to send persisted-settings boilerplate for disposable
* tool bundles.
*/
enabled?: boolean;
}
export interface RunScopedToolBundle {
mcpServers?: RunScopedMcpServerConfig[];
}
export interface RunScopedToolBundleSummary {
mcpServers: Array<{
id: string;
label?: string;
templateId?: string;
transport: McpTransport;
enabled: boolean;
authMode?: McpAuthMode;
}>;
}
export type BrowserUseUnavailableReason = 'no-matching-browser-backend';
export type BrowserUseProbeFailureCategory =
| 'not-probed'
| 'registry-missing'
| 'registry-unreadable';
export interface BrowserUseDiscoveryFacts {
registryPath: string;
registryExists: boolean;
socketCount: number;
candidateCount: number;
staleCount: number;
currentSessionIdPresent: boolean | null;
probeFailureCategory: BrowserUseProbeFailureCategory;
newestSocketAgeMs?: number;
staleThresholdMs: number;
}
export interface BrowserUseRunState {
requested: boolean;
available: boolean;
reason?: BrowserUseUnavailableReason;
diagnostics: BrowserUseDiscoveryFacts;
}
export interface ChatRunCreateRequest extends ChatRequest {
projectId: string;
conversationId: string;
assistantMessageId: string;
clientRequestId: string;
}
/**
* Minimal POST /api/runs shape accepted from MCP / SDK callers that do not
* manage conversation state client-side. Only `projectId` is required;
* `message` and `agentId` are optional — the daemon resolves `agentId` from
* the saved app-config when it is omitted.
*/
export interface McpRunCreateRequest {
projectId: string;
message?: string;
agentId?: string;
skillId?: string;
pluginId?: string;
model?: string;
pluginInputs?: Record<string, unknown>;
mediaExecution?: MediaExecutionPolicy;
toolBundle?: RunScopedToolBundle;
}
export const CHAT_RUN_STATUSES = [
'queued',
'running',
'succeeded',
'failed',
'canceled',
] as const;
export type ChatRunStatus = (typeof CHAT_RUN_STATUSES)[number];
export type ChatMessageFeedbackRating = 'positive' | 'negative';
export type ChatMessageFeedbackReasonCode =
| 'matched_request'
| 'strong_visual'
| 'useful_structure'
| 'easy_to_continue'
| 'followed_design_system'
| 'missed_request'
| 'weak_visual'
| 'incomplete_output'
| 'hard_to_use'
| 'missed_design_system'
| 'other';
export interface ChatMessageFeedback {
rating: ChatMessageFeedbackRating;
reasonCodes?: ChatMessageFeedbackReasonCode[];
customReason?: string;
reasonsSubmittedAt?: number;
createdAt: number;
updatedAt?: number;
}
/**
* POST /api/runs/:runId/feedback — relays the user's assistant-turn rating
* to Langfuse as a `score-create` so evals can filter traces by feedback.
* The daemon is the single network egress point for telemetry (web never
* talks to Langfuse directly), and gates this on `telemetry.metrics +
* telemetry.content` consent independently of what the browser thinks.
*
* `customReason` ships the raw free text the user typed in the "other"
* input (trimmed). Product confirmed on 2026-05-13 that analysts need the
* text to make sense of the feedback; this is consent-gated behind
* `telemetry.content` like the rest of the message-content telemetry.
*/
export interface ChatRunFeedbackRequest {
projectId: string;
conversationId: string;
assistantMessageId: string;
rating: ChatMessageFeedbackRating;
reasonCodes: ChatMessageFeedbackReasonCode[];
hasCustomReason: boolean;
/** Raw "other" free text (trimmed). Empty string when no custom reason. */
customReason: string;
}
export interface ChatRunFeedbackResponse {
/** `'accepted'` once the daemon has enqueued (or skipped due to consent). */
status: 'accepted' | 'skipped_consent' | 'skipped_no_sink';
}
export interface ChatRunCreateResponse {
runId: string;
// Daemon-resolved conversation/message ids — populated for MCP /
// SDK callers that POST /api/runs with only projectId. The web flow
// normally sends these in already; daemon falls back to the
// project's default conversation otherwise.
conversationId?: string | null;
assistantMessageId?: string | null;
appliedPluginSnapshotId?: string | null;
pluginId?: string | null;
}
export interface ChatRunStatusResponse {
id: string;
projectId: string | null;
conversationId: string | null;
assistantMessageId: string | null;
agentId: string | null;
/** Design system whose prompt context was actually injected for this run. */
designSystemId?: string | null;
/** Selected design system before usability/body checks; useful for diagnostics. */
designSystemRequestedId?: string | null;
/** Source that supplied the effective design-system selection. */
designSystemSelectionSource?: 'request' | 'plugin' | 'project' | 'app-default' | 'none' | null;
/** sha256 digest of the injected DESIGN.md/tokens/component context. */
designSystemDigest?: string | null;
appliedPluginSnapshotId?: string | null;
pluginId?: string | null;
status: ChatRunStatus;
createdAt: number;
updatedAt: number;
cancelRequested?: boolean;
childPid?: number | null;
processGroupId?: number | null;
childExited?: boolean;
childExitObservedAt?: number | null;
exitCode?: number | null;
signal?: string | null;
error?: string | null;
errorCode?: string | null;
/** True when this terminal failure can be recovered by resuming the agent's
* existing CLI session (a transient upstream drop / inactivity timeout on a
* session-resuming runtime), rather than only restarting from scratch. The
* chat uses it to offer a Continue affordance; the next turn in the same
* conversation resumes the persisted session. Absent/false on success,
* non-resumable failures, and runtimes without CLI session resume. */
resumable?: boolean;
/** Absolute path to the per-run JSONL event log the daemon mirrors
* the SSE stream to (see runs.ts `runsLogDir`). Null when the
* daemon was launched without event persistence configured. */
eventsLogPath?: string | null;
/** Present on daemon run status responses that know the effective run policy. */
mediaExecution?: MediaExecutionPolicy;
/** Run-scoped tool bundle summary with secrets and command details redacted. */
toolBundle?: RunScopedToolBundleSummary;
/** Prompt cache diagnostics for resume-capable runtime sessions. */
promptCache?: {
stablePromptHash: string;
hit: boolean;
missReason: 'new-session' | 'missing-stored-hash' | 'stable-prompt-changed' | null;
};
/** Browser Use availability for runs that requested in-app browser automation. */
browserUse?: BrowserUseRunState;
/** Effective storage/provenance for the workspace used by this run. */
workspace?: RunWorkspace;
}
export type ChatRunResultPackageResponse = RunResultPackageResponse;
export interface ChatRunListResponse {
runs: ChatRunStatusResponse[];
}
export interface ChatRunCancelResponse {
ok: true;
run?: ChatRunStatusResponse;
}
export interface ChatAttachment {
path: string;
name: string;
kind: 'image' | 'file';
size?: number;
/**
* User-visible attachment order for this turn. Older messages may omit it;
* consumers should fall back to array position.
*/
order?: number;
}
export interface ChatCommentAttachment {
id: string;
order: number;
filePath: string;
elementId: string;
selector: string;
label: string;
comment: string;
currentText: string;
pagePosition: PreviewCommentPosition;
htmlHint: string;
style?: PreviewAnnotationStyle;
selectionKind?: ChatCommentSelectionKind;
memberCount?: number;
podMembers?: PreviewCommentMember[];
// Zero-based slide the marked element lives on, for deck artifacts. Carried
// so the host can flip the preview to that slide when the send starts.
slideIndex?: number;
screenshotPath?: string;
markKind?: PreviewVisualMarkKind;
intent?: string;
imageAttachments?: PreviewCommentAttachment[];
/** `'query'` means `comment` was promoted to the message text; keep target data as context only. */
commentContext?: 'context' | 'query';
source?: 'saved-comment' | 'board-batch';
}
export type PersistedAgentEvent =
// `code` carries the structured API error code for `label: 'error'`
// status events (e.g. AGENT_AUTH_REQUIRED, RATE_LIMITED). Clients use it to
// decide error-specific affordances such as the hosted-AMR nudge.
| { kind: 'status'; label: string; detail?: string; code?: string }
| { kind: 'text'; text: string }
| { kind: 'conversation_title'; title: string }
| { kind: 'thinking'; text: string }
| {
kind: 'live_artifact';
action: 'created' | 'updated' | 'deleted';
projectId: string;
artifactId: string;
title: string;
refreshStatus?: string;
}
| {
kind: 'live_artifact_refresh';
phase: 'started' | 'succeeded' | 'failed';
projectId: string;
artifactId: string;
refreshId?: string;
title?: string;
refreshedSourceCount?: number;
error?: string;
}
| { kind: 'tool_use'; id: string; name: string; input: unknown }
| { kind: 'tool_result'; toolUseId: string; content: string; isError: boolean }
| {
kind: 'diagnostic';
name: string;
source?: string;
elapsedMs?: number;
reason?: string;
suppressedChars?: number;
suppressedChunks?: number;
openedBlocks?: number;
closedBlocks?: number;
fileCount?: number;
files?: string[];
pendingCandidateChars?: number;
suppressing?: boolean;
shape?: Record<string, unknown>;
}
| {
kind: 'plugin_candidate';
candidateId: string;
title: string;
description?: string;
confidence?: number;
draftPath?: string | null;
}
| { kind: 'usage'; inputTokens?: number; outputTokens?: number; costUsd?: number; durationMs?: number }
| { kind: 'raw'; line: string };
export interface ChatMessage {
id: string;
role: ChatRole;
content: string;
agentId?: string;
agentName?: string;
events?: PersistedAgentEvent[];
createdAt?: number;
runId?: string;
runStatus?: ChatRunStatus;
/** True when this message's failed run can be recovered by resuming the
* agent's CLI session (transient upstream drop / inactivity on a
* session-resuming runtime). Drives the chat's Continue affordance; mirrors
* ChatRunStatusResponse.resumable. */
resumable?: boolean;
lastRunEventId?: string;
startedAt?: number;
endedAt?: number;
sessionMode?: ChatSessionMode;
runContext?: RunContextSelection;
appliedPluginSnapshot?: AppliedPluginSnapshot;
attachments?: ChatAttachment[];
commentAttachments?: ChatCommentAttachment[];
producedFiles?: ProjectFile[];
traceObjectFiles?: ProjectFile[];
// Diff baseline so reattach can rebuild producedFiles after reload.
preTurnFileNames?: string[];
feedback?: ChatMessageFeedback;
/**
* Request-only marker for the final assistant-message persistence pass.
* The daemon does not store or return this field; it only uses it to
* avoid telemetry reads before content and producedFiles are finalized.
*/
telemetryFinalized?: boolean;
}