@lace-cloud/chat-core 0.18.0 → 0.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -1,38 +1,9 @@
1
- import { ChatEngineEvent, FactsSource } from '@lace-cloud/host';
1
+ import { RenderInput } from '@lace-cloud/proto';
2
+ import { EditorPort, SourcePort, CommandsPort, CommandFailure, CanvasSource } from '@lace-cloud/canvas/engine';
2
3
 
3
- type TextPart = {
4
- kind: 'text';
5
- value: string;
6
- };
7
- type ToolCallPart = {
8
- kind: 'tool-call';
9
- callId: string;
10
- name: string;
11
- input: unknown;
12
- };
13
- type ToolResultPart = {
14
- kind: 'tool-result';
15
- callId: string;
16
- content: TextPart[];
17
- isError?: boolean;
18
- };
19
- type Part = TextPart | ToolCallPart | ToolResultPart;
20
- type ChatMessageRole = 'user' | 'assistant';
21
- type ChatMessage = {
22
- role: ChatMessageRole;
23
- content: Part[];
24
- };
25
- type ChatStreamEvent = {
26
- type: 'text';
27
- value: string;
28
- } | {
29
- type: 'tool-call';
30
- callId: string;
31
- name: string;
32
- input: unknown;
33
- };
34
4
  type ToolSchema = {
35
5
  name: string;
6
+ title: string;
36
7
  description: string;
37
8
  inputSchema: Record<string, unknown>;
38
9
  };
@@ -40,246 +11,254 @@ type ToolResult = {
40
11
  content: string;
41
12
  isError?: boolean;
42
13
  };
43
- type ChatModel = {
44
- readonly __chatModelBrand: unique symbol;
45
- };
46
- interface Disposable {
47
- dispose(): void;
48
- }
49
14
 
50
- interface ChatHostAdapter {
51
- /**
52
- * Resolve the default chat model for a turn. Return `null` if no
53
- * model is available (the agentic loop will emit a `no-model`
54
- * event to the webview).
55
- */
56
- selectModel(): Promise<ChatModel | null>;
57
- /**
58
- * Send a request to the model and stream events back. The
59
- * returned async iterable must yield events for each part the
60
- * model produces. Tool-result parts are *not* yielded — core
61
- * synthesizes those after dispatching the tools itself.
62
- *
63
- * The adapter is responsible for honouring `signal` (cancelling
64
- * the in-flight request when it fires) and for mapping its
65
- * native model stream onto {@link ChatStreamEvent}.
66
- */
67
- sendRequest(model: ChatModel, messages: ChatMessage[], tools: ToolSchema[], signal: AbortSignal): AsyncIterable<ChatStreamEvent>;
68
- /**
69
- * Persist history under the given key. The adapter picks the
70
- * storage surface — VS Code uses `context.workspaceState`;
71
- * JetBrains would use `PropertiesComponent` or similar.
72
- *
73
- * `messages` is JSON-safe by construction (all `Part` variants
74
- * are plain data), so implementations can stringify directly.
75
- */
76
- saveHistory(key: string, messages: ChatMessage[]): Promise<void>;
77
- /** Restore history previously persisted under `key`. Returns `null` if absent. */
78
- loadHistory(key: string): Promise<ChatMessage[] | null>;
79
- /**
80
- * Read a user-visible configuration value, scoped to the chat
81
- * subsystem. `key` is dot-separated (e.g. `'proactivity'`); the
82
- * adapter maps it onto its native config system.
83
- */
84
- getConfig<T>(key: string, defaultValue: T): T;
85
- /** Current workspace name, used to label the engine session. `null` outside a workspace. */
86
- getWorkspaceName(): string | null;
87
- /** Write to the IDE's log surface (VS Code OutputChannel, JetBrains Logger, etc.). */
88
- log(message: string): void;
15
+ type ToolHandler<R extends ToolResult = ToolResult> = (input: Record<string, unknown>) => Promise<R>;
16
+ type ToolRegistration<R extends ToolResult = ToolResult> = {
17
+ schema: ToolSchema;
18
+ handler: ToolHandler<R>;
19
+ };
20
+ interface ToolRegistry {
21
+ register(schema: ToolSchema, handler: ToolHandler): void;
22
+ getHandler(name: string): ToolHandler | undefined;
23
+ listSchemas(): ToolSchema[];
24
+ listNames(): string[];
89
25
  }
26
+ declare function createToolRegistry(): ToolRegistry;
90
27
 
91
- type CompletedTurn = {
92
- messageId: string;
93
- userText: string;
94
- assistantText: string;
95
- toolCalls: Array<{
96
- name: string;
97
- input: unknown;
98
- resultContent: string;
99
- isError?: boolean;
100
- }>;
101
- };
102
- type ProactiveSuggestion = {
103
- id: string;
104
- text: string;
105
- };
106
- type ContextSummary = {
107
- nodeCount: number;
108
- warningCount: number;
109
- errorCount: number;
110
- sessionId: string | null;
28
+ type DecisionKind = 'operation' | 'instance' | 'input' | 'module' | 'connection' | 'steps';
29
+ declare const DECISION_KINDS: readonly DecisionKind[];
30
+ declare const NONE_CANDIDATE_ID = "(none)";
31
+ type Candidate = {
32
+ readonly id: string;
33
+ readonly kind: DecisionKind;
34
+ readonly text: string;
111
35
  };
112
- type WebviewToHost = {
113
- type: 'webview-ready';
114
- } | {
115
- type: 'user-turn';
116
- text: string;
117
- } | {
118
- type: 'cancel-turn';
119
- } | {
120
- type: 'accept-proactive-suggestion';
121
- id: string;
36
+
37
+ type InstanceRole = 'instance' | 'source' | 'target';
38
+ type InputState = 'not set' | 'set' | 'wired';
39
+ type PinFacts = {
40
+ readonly name: string;
41
+ readonly description?: string;
122
42
  };
123
- type HostToWebview = {
124
- type: 'history';
125
- turns: CompletedTurn[];
126
- } | {
127
- type: 'token';
128
- text: string;
129
- } | {
130
- type: 'tool-call';
131
- callId: string;
132
- name: string;
133
- input: unknown;
134
- } | {
135
- type: 'tool-result';
136
- callId: string;
137
- name: string;
138
- content: string;
139
- isError?: boolean;
43
+ type CandidateFacts = {
44
+ readonly id: string;
45
+ readonly kind: 'operation';
140
46
  } | {
141
- type: 'turn-complete';
142
- messageId: string;
47
+ readonly id: string;
48
+ readonly kind: 'steps';
143
49
  } | {
144
- type: 'turn-cancelled';
50
+ readonly id: string;
51
+ readonly kind: 'instance';
52
+ readonly role: InstanceRole;
53
+ readonly module: string;
54
+ readonly inputs: readonly PinFacts[];
55
+ readonly outputs: readonly PinFacts[];
145
56
  } | {
146
- type: 'history-cleared';
57
+ readonly id: string;
58
+ readonly kind: 'input';
59
+ readonly instance: string;
60
+ readonly module: string;
61
+ readonly type: string;
62
+ readonly required: boolean;
63
+ readonly state: InputState;
64
+ readonly description?: string;
147
65
  } | {
148
- type: 'no-model';
66
+ readonly id: string;
67
+ readonly kind: 'module';
68
+ readonly displayName: string;
69
+ readonly description: string;
70
+ readonly system: string;
71
+ readonly categories: readonly string[];
149
72
  } | {
150
- type: 'engine-unavailable';
151
- } | {
152
- type: 'context-summary';
153
- summary: ContextSummary;
154
- } | {
155
- type: 'proactive-suggestion';
156
- suggestion: ProactiveSuggestion;
73
+ readonly id: string;
74
+ readonly kind: 'connection';
75
+ readonly source: string;
76
+ readonly output: string;
77
+ readonly target: string;
78
+ readonly input: string;
79
+ readonly score?: number;
157
80
  };
81
+ declare function renderCandidate(facts: CandidateFacts): Candidate;
158
82
 
159
- type ToolHandler = (input: Record<string, unknown>) => Promise<ToolResult>;
160
- type ToolRegistration = {
161
- schema: ToolSchema;
162
- handler: ToolHandler;
163
- };
164
- interface ToolRegistry {
165
- register(schema: ToolSchema, handler: ToolHandler): void;
166
- getHandler(name: string): ToolHandler | undefined;
167
- listSchemas(): ToolSchema[];
168
- listNames(): string[];
169
- }
170
- declare function createToolRegistry(): ToolRegistry;
83
+ declare const VALUE_PLACEHOLDER = "<value>";
84
+ declare function renderRequest(request: string, values: readonly unknown[], statement?: string): string;
171
85
 
172
- interface WebviewTransport {
173
- /** Send a message to the webview. */
174
- postMessage(message: HostToWebview): void;
175
- /**
176
- * Subscribe to messages coming from the webview. Call
177
- * `dispose()` on the returned value to unsubscribe.
178
- */
179
- onMessage(listener: (message: WebviewToHost) => void): Disposable;
180
- }
86
+ type ChangeOutcome = 'reverted' | 'changed_again' | 'saved' | 'generated' | 'hand_edited';
87
+ type OutcomeReport = {
88
+ readonly event: ChangeOutcome;
89
+ readonly question?: number;
90
+ readonly candidate?: string;
91
+ };
181
92
 
182
- type AgentControllerOptions = {
183
- adapter: ChatHostAdapter;
184
- transport: WebviewTransport;
185
- toolRegistry: ToolRegistry;
186
- buildSystemPrompt: (availableSystems: string[]) => string;
187
- getAvailableSystems: () => string[];
188
- /** Host-supplied counts of the current canvas; null before a state has loaded. */
189
- getCanvasFacts: () => {
190
- nodeCount: number;
191
- diagnosticCount: number;
93
+ type Took = 'named' | 'chat' | 'clm';
94
+ type Question = {
95
+ readonly kind: DecisionKind;
96
+ readonly capture_id: string | null;
97
+ readonly chat: string | null;
98
+ readonly clm: {
99
+ readonly id: string;
100
+ readonly rightness: number;
192
101
  } | null;
193
- historyKey: string;
194
- maxToolRounds?: number;
195
- completedTurnCap?: number;
102
+ readonly took: Took;
103
+ readonly taken: string | null;
104
+ };
105
+ type DecisionReport = {
106
+ readonly id: string;
107
+ readonly surface: 'canvas';
108
+ readonly questions: readonly Question[];
109
+ readonly applied: string | null;
110
+ readonly asked: boolean;
196
111
  };
197
- declare class AgentController {
198
- private readonly opts;
199
- private history;
200
- private completedTurns;
201
- private activeTurn;
202
- private transportSubscription;
203
- private readonly telemetry;
204
- constructor(opts: AgentControllerOptions);
205
- /** Load persisted history and start listening for webview messages. */
206
- init(): Promise<void>;
207
- /** Cancel any active turn and unsubscribe from the webview. */
208
- dispose(): void;
209
- /** Drop in-memory and persisted history. */
210
- clearHistory(): Promise<void>;
211
- /** Push a context-summary banner into the chat UI. */
212
- postContextSummary(summary: ContextSummary): void;
213
- /** Forward a proactive suggestion from the watcher to the chat UI. */
214
- postProactiveSuggestion(suggestion: ProactiveSuggestion): void;
215
- /** Notify the chat UI that the engine is unavailable for this session. */
216
- postEngineUnavailable(): void;
217
- private handleWebviewMessage;
218
- private runTurn;
219
- }
220
112
 
221
- /**
222
- * Trim message history to the most recent `turnCap` turns. A turn
223
- * is one user message followed by one assistant message (plus any
224
- * interleaved tool traffic the assistant emits). Keeping the last
225
- * `turnCap` turns is approximated by keeping `turnCap * 2` messages
226
- * — exact turn boundaries aren't worth tracking for a bounded cap.
227
- */
228
- declare function capRecentMessages(messages: ChatMessage[], turnCap?: number): ChatMessage[];
113
+ type Transport = (path: `/api/v1/candidates/${string}`, init: {
114
+ readonly method: 'POST';
115
+ readonly body: unknown;
116
+ }) => Promise<{
117
+ readonly status: number;
118
+ readonly body: unknown;
119
+ }>;
120
+ type RankRequest = {
121
+ readonly request: string;
122
+ readonly statement: string;
123
+ readonly candidates: readonly Candidate[];
124
+ };
125
+ type Score = {
126
+ readonly id: string;
127
+ readonly score: number;
128
+ readonly rightness: number;
129
+ };
130
+ type Threshold = {
131
+ readonly from: number;
132
+ };
133
+ type Thresholds = Readonly<Partial<Record<DecisionKind, Threshold>>>;
134
+ type Ranked = {
135
+ readonly capture_id: string;
136
+ readonly head: string;
137
+ readonly scores: readonly Score[];
138
+ readonly thresholds: Thresholds;
139
+ };
229
140
 
230
- type RuleState = {
231
- prevDiagnosticErrorCount: number;
141
+ type Part = 'train' | 'calibration' | 'held_out';
142
+ type Origin = 'real' | 'synthetic';
143
+ type TrainingRow = {
144
+ readonly request: string;
145
+ readonly statement: string;
146
+ readonly candidates: readonly Candidate[];
147
+ readonly label: string;
148
+ readonly part: Part;
149
+ readonly origin: Origin;
150
+ readonly turn: string;
151
+ readonly chat: string | null;
152
+ };
153
+ type LabelledQuestion = {
154
+ readonly kind: DecisionKind;
155
+ readonly candidates: readonly Candidate[];
156
+ readonly label: string;
157
+ readonly chat: string | null;
232
158
  };
233
- declare function initialRuleState(): RuleState;
234
- declare function matchGenerateError(event: ChatEngineEvent): ProactiveSuggestion | null;
235
- declare function matchErrorsAppeared(event: ChatEngineEvent, state: RuleState): ProactiveSuggestion | null;
236
- declare function matchAll(event: ChatEngineEvent, state: RuleState): ProactiveSuggestion | null;
237
- declare function advanceState(event: ChatEngineEvent, state: RuleState): RuleState;
159
+ type LabelledTurn = {
160
+ readonly id: string;
161
+ readonly request: string;
162
+ readonly statement: string;
163
+ readonly origin: Origin;
164
+ readonly questions: readonly LabelledQuestion[];
165
+ };
166
+ declare function partOf(key: string): Part;
167
+ declare function trainingRows(turn: LabelledTurn, part: Part): TrainingRow[];
238
168
 
239
- type ProactivityWatcherOptions = {
240
- facts: FactsSource;
241
- sessionId: string;
242
- adapter: Pick<ChatHostAdapter, 'getConfig' | 'log'>;
243
- onSuggestion: (suggestion: ProactiveSuggestion) => void;
169
+ declare function sure(ranked: Ranked, kind: DecisionKind): boolean;
170
+ type Readings = {
171
+ readonly kind: DecisionKind;
172
+ readonly chat: string | null;
173
+ readonly ranked: Ranked | null;
244
174
  };
245
- declare class ProactivityWatcher {
246
- private readonly opts;
247
- private stream;
248
- private ruleState;
249
- private retried;
250
- private stopped;
251
- constructor(opts: ProactivityWatcherOptions);
252
- start(): void;
253
- stop(): void;
254
- private handleEvent;
255
- }
175
+ type Composed = {
176
+ readonly took: 'chat' | 'clm';
177
+ readonly taken: string | null;
178
+ readonly clm: {
179
+ readonly id: string;
180
+ readonly rightness: number;
181
+ } | null;
182
+ readonly differed: boolean;
183
+ };
184
+ declare function compose({ kind, chat, ranked }: Readings): Composed;
256
185
 
257
- declare function buildSystemPrompt(systems?: string[]): string;
186
+ type CanvasInput = RenderInput & {
187
+ readonly sensitive: boolean;
188
+ };
258
189
 
259
- type ToolInvocationRecord = {
260
- name: string;
261
- params: unknown;
262
- durationMs: number;
263
- isError: boolean;
264
- resultPreview: string;
190
+ type CanvasEditor = Pick<EditorPort, 'apply' | 'save' | 'generate' | 'wiringCandidates'>;
191
+ type CanvasToolPorts = {
192
+ readonly source: SourcePort;
193
+ readonly editor: CanvasEditor;
194
+ readonly commands: CommandsPort;
195
+ readonly transport?: Transport;
196
+ };
197
+ type ModuleToolPorts = {
198
+ readonly commands: CommandsPort;
199
+ };
200
+
201
+ type ChangeTarget = {
202
+ readonly scope: string;
203
+ readonly instance: string;
204
+ readonly input?: string;
205
+ };
206
+ type InputBinding = {
207
+ readonly mode: CanvasInput['mode'];
208
+ readonly value?: unknown;
209
+ readonly variable?: string;
210
+ readonly expression?: string;
211
+ readonly wiredSource?: string;
212
+ };
213
+ type InstancePresence = {
214
+ readonly instance: string;
215
+ readonly module: string;
265
216
  };
266
- type SessionMetadata = {
267
- sessionId: string;
268
- prompt: string;
269
- command?: string;
270
- toolCalls: ToolInvocationRecord[];
271
- rounds: number;
272
- startTime: number;
273
- endTime?: number;
217
+ type ChangeState = InputBinding | InstancePresence | null;
218
+ type RecordQuestion = {
219
+ readonly kind: DecisionKind;
220
+ readonly candidates: readonly string[];
274
221
  };
275
- type Telemetry = {
276
- startSession(prompt: string, command?: string): SessionMetadata;
277
- logToolInvocation(session: SessionMetadata, name: string, params: unknown, result: {
278
- content: string;
279
- isError?: boolean;
280
- }, durationMs: number): void;
281
- endSession(session: SessionMetadata, maxToolRounds?: number): void;
222
+ type ChangeRecord = {
223
+ readonly id: string;
224
+ readonly target: ChangeTarget;
225
+ readonly before: ChangeState;
226
+ readonly after: ChangeState;
227
+ readonly revision?: number;
228
+ readonly questions: readonly RecordQuestion[];
229
+ readonly reported: boolean;
282
230
  };
283
- declare function createTelemetry(log: (message: string) => void): Telemetry;
284
231
 
285
- export { AgentController, type AgentControllerOptions, type ChatHostAdapter, type ChatMessage, type ChatMessageRole, type ChatModel, type ChatStreamEvent, type CompletedTurn, type ContextSummary, type Disposable, type HostToWebview, type Part, type ProactiveSuggestion, ProactivityWatcher, type ProactivityWatcherOptions, type RuleState, type SessionMetadata, type Telemetry, type TextPart, type ToolCallPart, type ToolHandler, type ToolInvocationRecord, type ToolRegistration, type ToolRegistry, type ToolResult, type ToolResultPart, type ToolSchema, type WebviewToHost, type WebviewTransport, advanceState, buildSystemPrompt, capRecentMessages, createTelemetry, createToolRegistry, initialRuleState, matchAll, matchErrorsAppeared, matchGenerateError };
232
+ declare const canvasToolSchemas: readonly [ToolSchema, ToolSchema, ToolSchema, ToolSchema];
233
+ type ChangeResult = ToolResult & {
234
+ readonly applied: readonly ChangeRecord[];
235
+ };
236
+ type CanvasTools = readonly [
237
+ ToolRegistration<ChangeResult>,
238
+ ToolRegistration,
239
+ ToolRegistration,
240
+ ToolRegistration
241
+ ] & {
242
+ readonly dispose: () => void;
243
+ };
244
+ declare function canvasTools(ports: CanvasToolPorts): CanvasTools;
245
+
246
+ declare const moduleToolSchemas: readonly [ToolSchema, ToolSchema, ToolSchema];
247
+ type ModuleToolResult = ToolResult & {
248
+ readonly failure?: CommandFailure;
249
+ };
250
+ type ModuleTools = readonly [
251
+ ToolRegistration<ModuleToolResult>,
252
+ ToolRegistration<ModuleToolResult>,
253
+ ToolRegistration<ModuleToolResult>
254
+ ];
255
+ declare function moduleTools({ commands }: ModuleToolPorts): ModuleTools;
256
+
257
+ declare function changeOutcomes(record: ChangeRecord, frames: readonly CanvasSource[], own?: ReadonlySet<number>): {
258
+ readonly record: ChangeRecord;
259
+ readonly outcomes: OutcomeReport[];
260
+ };
261
+
262
+ declare function renderCanvas(source: CanvasSource): string;
263
+
264
+ export { type Candidate, type CandidateFacts, type CanvasEditor, type CanvasToolPorts, type CanvasTools, type ChangeOutcome, type ChangeRecord, type ChangeResult, type ChangeState, type ChangeTarget, type Composed, DECISION_KINDS, type DecisionKind, type DecisionReport, type InputBinding, type InstancePresence, type LabelledQuestion, type LabelledTurn, type ModuleToolPorts, type ModuleToolResult, type ModuleTools, NONE_CANDIDATE_ID, type Origin, type OutcomeReport, type Part, type Question, type RankRequest, type Ranked, type Readings, type Score, type Threshold, type Thresholds, type Took, type ToolHandler, type ToolRegistration, type ToolRegistry, type ToolResult, type ToolSchema, type TrainingRow, type Transport, VALUE_PLACEHOLDER, canvasToolSchemas, canvasTools, changeOutcomes, compose, createToolRegistry, moduleToolSchemas, moduleTools, partOf, renderCandidate, renderCanvas, renderRequest, sure, trainingRows };