@open-document/core 0.14.0 → 0.14.2
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/{build-CyBlmrjS.js → build-I0cgoERP.js} +1 -1
- package/dist/{check-DUKDEem-.js → check-Dys96QSL.js} +3 -3
- package/dist/cli/bin.js +9 -9
- package/dist/{context-BPApOW9n.js → context-C-osJW5f.js} +1 -1
- package/dist/{dev-DrnOvsX3.js → dev-CC4PrIOC.js} +1 -1
- package/dist/{diff-BPMD2Knh.js → diff-DhutD4TJ.js} +2 -2
- package/dist/{export-DeVtL8aX.js → export-KrajVJO7.js} +3 -3
- package/dist/{import-WAgWsuOv.js → import-Yv34za3L.js} +3 -3
- package/dist/ops/index.js +2 -2
- package/dist/{ops-raK6424A.js → ops-Dux5HlkH.js} +2 -422
- package/dist/{preview-B8Vw9QCh.js → preview-7eqn9Wzf.js} +1 -1
- package/dist/{config-cNpYUpF6.js → session-BSbrC5gK.js} +510 -4
- package/dist/{templates-D9jAFJ8R.js → templates-s-VLTSRc.js} +3 -3
- package/dist/vite/index.js +1 -1
- package/package.json +6 -1
- package/src/agent/check-after-turn.ts +59 -0
- package/src/agent/detect.ts +158 -0
- package/src/agent/install.ts +73 -0
- package/src/agent/prompt.ts +175 -0
- package/src/agent/provider.ts +126 -0
- package/src/agent/providers/codex.ts +209 -0
- package/src/agent/providers/index.ts +7 -0
- package/src/agent/providers/sdk-types.ts +71 -0
- package/src/agent/runner.ts +392 -0
- package/src/agent/status.ts +64 -0
- package/src/agent/store.ts +271 -0
- package/src/agent/types.ts +174 -0
- package/src/app/components/agent/agent-panel.tsx +6 -1
- package/src/app/components/agent/transcript.tsx +111 -3
- package/src/app/lib/i18n-ja/agent-panel.ts +8 -0
- package/src/app/lib/i18n-ko/agent-panel.ts +8 -0
- package/src/app/lib/i18n-zh-cn/agent-panel.ts +8 -0
- package/src/app/lib/i18n-zh-tw/agent-panel.ts +8 -0
- package/src/app/lib/use-locale.ts +7 -0
- package/src/diagram/index.ts +46 -0
- package/src/diagram/layout.ts +627 -0
- package/src/diagram/parse.ts +335 -0
- package/src/diagram/render.ts +162 -0
- package/src/editing/placeholder-id.ts +2 -0
- package/src/app/lib/chart.test.ts +0 -123
- package/src/app/lib/docx/ooxml.test.ts +0 -484
- package/src/app/lib/flow-measure.test.ts +0 -30
- package/src/app/lib/flow.test.ts +0 -129
- package/src/app/lib/footnotes.test.tsx +0 -102
- package/src/app/lib/i18n.test.ts +0 -24
- package/src/app/lib/inspector/inline-edit.test.ts +0 -244
- package/src/app/lib/page-range.test.ts +0 -104
- package/src/app/lib/records.test.ts +0 -38
- package/src/app/lib/sdk.test.ts +0 -30
- package/src/app/lib/view-mode.test.ts +0 -46
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { commandVersion, findOnPath, resolveWorkspacePackage } from './detect.ts';
|
|
2
|
+
import { type PackageManager, runCommand } from './install.ts';
|
|
3
|
+
import type { AgentProvider } from './provider.ts';
|
|
4
|
+
import type { AgentProviderId, AgentProviderStatus, AgentStatus } from './types.ts';
|
|
5
|
+
|
|
6
|
+
const INSTALL_TIMEOUT_MS = 10 * 60 * 1000;
|
|
7
|
+
|
|
8
|
+
async function providerStatus(
|
|
9
|
+
userCwd: string,
|
|
10
|
+
provider: AgentProvider,
|
|
11
|
+
): Promise<AgentProviderStatus> {
|
|
12
|
+
const sdk = resolveWorkspacePackage(userCwd, provider.sdkPackage);
|
|
13
|
+
const cliPath = findOnPath(provider.cliCommand);
|
|
14
|
+
const [cliVersion, models] = await Promise.all([
|
|
15
|
+
cliPath ? commandVersion(cliPath) : null,
|
|
16
|
+
provider.models(),
|
|
17
|
+
]);
|
|
18
|
+
return {
|
|
19
|
+
id: provider.id,
|
|
20
|
+
name: provider.name,
|
|
21
|
+
sdkPackage: provider.sdkPackage,
|
|
22
|
+
sdkVersion: sdk?.version ?? null,
|
|
23
|
+
cliCommand: provider.cliCommand,
|
|
24
|
+
cliFound: cliPath !== null,
|
|
25
|
+
cliVersion,
|
|
26
|
+
ready: sdk !== null,
|
|
27
|
+
models,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function agentStatus(
|
|
32
|
+
userCwd: string,
|
|
33
|
+
providers: Record<AgentProviderId, AgentProvider>,
|
|
34
|
+
packageManager: PackageManager,
|
|
35
|
+
): Promise<AgentStatus> {
|
|
36
|
+
const list = await Promise.all(
|
|
37
|
+
Object.values(providers).map((provider) => providerStatus(userCwd, provider)),
|
|
38
|
+
);
|
|
39
|
+
return { enabled: true, packageManager, providers: list };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function installCommandFor(
|
|
43
|
+
packageManager: PackageManager,
|
|
44
|
+
pkg: string,
|
|
45
|
+
): { cmd: string; args: string[] } {
|
|
46
|
+
switch (packageManager) {
|
|
47
|
+
case 'pnpm':
|
|
48
|
+
return { cmd: 'pnpm', args: ['add', '-D', pkg] };
|
|
49
|
+
case 'yarn':
|
|
50
|
+
return { cmd: 'yarn', args: ['add', '-D', pkg] };
|
|
51
|
+
case 'bun':
|
|
52
|
+
return { cmd: 'bun', args: ['add', '-d', pkg] };
|
|
53
|
+
case 'npm':
|
|
54
|
+
return { cmd: 'npm', args: ['install', '-D', pkg] };
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function installSdk(
|
|
59
|
+
packageManager: PackageManager,
|
|
60
|
+
pkg: string,
|
|
61
|
+
cwd: string,
|
|
62
|
+
): Promise<void> {
|
|
63
|
+
return runCommand(installCommandFor(packageManager, pkg), cwd, INSTALL_TIMEOUT_MS);
|
|
64
|
+
}
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
3
|
+
import fs from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import {
|
|
6
|
+
type AgentItem,
|
|
7
|
+
type AgentProviderId,
|
|
8
|
+
type AgentSession,
|
|
9
|
+
type AgentSessionSummary,
|
|
10
|
+
type AgentStreamEvent,
|
|
11
|
+
isAgentProviderId,
|
|
12
|
+
sessionSummary,
|
|
13
|
+
} from './types.ts';
|
|
14
|
+
|
|
15
|
+
const MAX_SESSIONS = 30;
|
|
16
|
+
const MAX_ITEMS_PER_SESSION = 400;
|
|
17
|
+
const SAVE_DEBOUNCE_MS = 150;
|
|
18
|
+
const TITLE_MAX = 60;
|
|
19
|
+
|
|
20
|
+
export function newId(prefix: string): string {
|
|
21
|
+
return `${prefix}-${randomBytes(6).toString('hex')}`;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function titleFromPrompt(text: string): string {
|
|
25
|
+
const line = text
|
|
26
|
+
.split('\n')
|
|
27
|
+
.map((l) => l.trim())
|
|
28
|
+
.find((l) => l.length > 0);
|
|
29
|
+
if (!line) return 'Untitled';
|
|
30
|
+
return line.length > TITLE_MAX ? `${line.slice(0, TITLE_MAX - 1)}…` : line;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
type Listener = (event: AgentStreamEvent) => void;
|
|
34
|
+
|
|
35
|
+
type PersistedShape = { version: 1; sessions: AgentSession[] };
|
|
36
|
+
|
|
37
|
+
function readPersisted(file: string): AgentSession[] {
|
|
38
|
+
if (!existsSync(file)) return [];
|
|
39
|
+
try {
|
|
40
|
+
const parsed = JSON.parse(readFileSync(file, 'utf8')) as Partial<PersistedShape>;
|
|
41
|
+
if (parsed?.version !== 1 || !Array.isArray(parsed.sessions)) return [];
|
|
42
|
+
// A dev-server restart mid-turn leaves no process to finish it; every
|
|
43
|
+
// reloaded session starts idle and pending prompts are closed out.
|
|
44
|
+
return parsed.sessions
|
|
45
|
+
.filter((s) => isAgentProviderId(s.provider))
|
|
46
|
+
.map((s) => ({
|
|
47
|
+
...s,
|
|
48
|
+
status: 'idle',
|
|
49
|
+
items: s.items.map(closePending),
|
|
50
|
+
}));
|
|
51
|
+
} catch {
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function closePending(item: AgentItem): AgentItem {
|
|
57
|
+
if (item.kind === 'assistant' && item.streaming) return { ...item, streaming: false };
|
|
58
|
+
if (item.kind === 'tool' && item.status === 'running') return { ...item, status: 'error' };
|
|
59
|
+
if (item.kind === 'question' && item.status === 'pending')
|
|
60
|
+
return { ...item, status: 'cancelled' };
|
|
61
|
+
if (item.kind === 'permission' && item.status === 'pending') return { ...item, status: 'denied' };
|
|
62
|
+
if (item.kind === 'check' && item.status === 'running') {
|
|
63
|
+
return { ...item, status: 'failed', error: 'interrupted' };
|
|
64
|
+
}
|
|
65
|
+
return item;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export type SessionStore = ReturnType<typeof createSessionStore>;
|
|
69
|
+
|
|
70
|
+
export function createSessionStore(opts: { file: string | null }) {
|
|
71
|
+
const sessions = new Map<string, AgentSession>();
|
|
72
|
+
const listeners = new Set<Listener>();
|
|
73
|
+
let saveTimer: ReturnType<typeof setTimeout> | null = null;
|
|
74
|
+
// Touch order breaks ties between sessions updated in the same millisecond.
|
|
75
|
+
let seq = 0;
|
|
76
|
+
const order = new Map<string, number>();
|
|
77
|
+
|
|
78
|
+
if (opts.file) {
|
|
79
|
+
for (const s of readPersisted(opts.file)) {
|
|
80
|
+
sessions.set(s.id, s);
|
|
81
|
+
order.set(s.id, seq++);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const emit = (event: AgentStreamEvent) => {
|
|
86
|
+
for (const l of listeners) l(event);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const scheduleSave = () => {
|
|
90
|
+
if (!opts.file) return;
|
|
91
|
+
if (saveTimer) clearTimeout(saveTimer);
|
|
92
|
+
saveTimer = setTimeout(() => {
|
|
93
|
+
saveTimer = null;
|
|
94
|
+
void save();
|
|
95
|
+
}, SAVE_DEBOUNCE_MS);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const writeSnapshot = async (file: string) => {
|
|
99
|
+
const body: PersistedShape = {
|
|
100
|
+
version: 1,
|
|
101
|
+
sessions: list().flatMap((s) => sessions.get(s.id) ?? []),
|
|
102
|
+
};
|
|
103
|
+
const tmp = `${file}.tmp`;
|
|
104
|
+
try {
|
|
105
|
+
await fs.mkdir(path.dirname(file), { recursive: true });
|
|
106
|
+
await fs.writeFile(tmp, JSON.stringify(body), { encoding: 'utf8', mode: 0o600 });
|
|
107
|
+
await fs.rename(tmp, file);
|
|
108
|
+
} catch (err) {
|
|
109
|
+
console.error('[open-doc] failed to save agent sessions:', err);
|
|
110
|
+
await fs.rm(tmp, { force: true }).catch(() => {});
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
// Saves share one temp file, so a flush racing a debounced save must wait
|
|
115
|
+
// for it rather than interleave writes and renames.
|
|
116
|
+
let saving: Promise<void> = Promise.resolve();
|
|
117
|
+
const save = () => {
|
|
118
|
+
const file = opts.file;
|
|
119
|
+
if (!file) return saving;
|
|
120
|
+
saving = saving.then(() => writeSnapshot(file));
|
|
121
|
+
return saving;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const touch = (session: AgentSession) => {
|
|
125
|
+
session.updatedAt = new Date().toISOString();
|
|
126
|
+
order.set(session.id, seq++);
|
|
127
|
+
emit({ type: 'session', session: sessionSummary(session) });
|
|
128
|
+
scheduleSave();
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const require = (id: string): AgentSession => {
|
|
132
|
+
const session = sessions.get(id);
|
|
133
|
+
if (!session) throw new Error(`unknown agent session: ${id}`);
|
|
134
|
+
return session;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
function list(): AgentSessionSummary[] {
|
|
138
|
+
return Array.from(sessions.values())
|
|
139
|
+
.sort((a, b) => {
|
|
140
|
+
if (a.updatedAt !== b.updatedAt) return a.updatedAt < b.updatedAt ? 1 : -1;
|
|
141
|
+
return (order.get(b.id) ?? 0) - (order.get(a.id) ?? 0);
|
|
142
|
+
})
|
|
143
|
+
.map(sessionSummary);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function get(id: string): AgentSession | null {
|
|
147
|
+
return sessions.get(id) ?? null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function create(provider: AgentProviderId, docId: string | null): AgentSession {
|
|
151
|
+
const now = new Date().toISOString();
|
|
152
|
+
const session: AgentSession = {
|
|
153
|
+
id: newId('s'),
|
|
154
|
+
provider,
|
|
155
|
+
title: 'New session',
|
|
156
|
+
createdAt: now,
|
|
157
|
+
updatedAt: now,
|
|
158
|
+
status: 'idle',
|
|
159
|
+
docId,
|
|
160
|
+
providerSessionId: null,
|
|
161
|
+
items: [],
|
|
162
|
+
};
|
|
163
|
+
sessions.set(session.id, session);
|
|
164
|
+
order.set(session.id, seq++);
|
|
165
|
+
const idle = list().filter((s) => s.status === 'idle');
|
|
166
|
+
for (const stale of idle.slice(MAX_SESSIONS)) {
|
|
167
|
+
sessions.delete(stale.id);
|
|
168
|
+
order.delete(stale.id);
|
|
169
|
+
emit({ type: 'session-removed', sessionId: stale.id });
|
|
170
|
+
}
|
|
171
|
+
touch(session);
|
|
172
|
+
return session;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function remove(id: string): boolean {
|
|
176
|
+
if (!sessions.delete(id)) return false;
|
|
177
|
+
order.delete(id);
|
|
178
|
+
emit({ type: 'session-removed', sessionId: id });
|
|
179
|
+
scheduleSave();
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function patch(
|
|
184
|
+
id: string,
|
|
185
|
+
changes: Partial<
|
|
186
|
+
Pick<AgentSession, 'title' | 'status' | 'providerSessionId' | 'providerCheckpoint' | 'docId'>
|
|
187
|
+
>,
|
|
188
|
+
): AgentSession {
|
|
189
|
+
const session = require(id);
|
|
190
|
+
Object.assign(session, changes);
|
|
191
|
+
touch(session);
|
|
192
|
+
return session;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function appendItem<T extends AgentItem>(sessionId: string, item: T): T {
|
|
196
|
+
const session = require(sessionId);
|
|
197
|
+
session.items.push(item);
|
|
198
|
+
if (session.items.length > MAX_ITEMS_PER_SESSION) {
|
|
199
|
+
session.items.splice(0, session.items.length - MAX_ITEMS_PER_SESSION);
|
|
200
|
+
session.trimmed = true;
|
|
201
|
+
}
|
|
202
|
+
emit({ type: 'item', sessionId, item });
|
|
203
|
+
touch(session);
|
|
204
|
+
return item;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function updateItem<K extends AgentItem['kind']>(
|
|
208
|
+
sessionId: string,
|
|
209
|
+
itemId: string,
|
|
210
|
+
kind: K,
|
|
211
|
+
changes: Partial<Extract<AgentItem, { kind: K }>>,
|
|
212
|
+
): Extract<AgentItem, { kind: K }> | null {
|
|
213
|
+
const session = require(sessionId);
|
|
214
|
+
const index = session.items.findIndex((i) => i.id === itemId && i.kind === kind);
|
|
215
|
+
if (index === -1) return null;
|
|
216
|
+
const next = { ...session.items[index], ...changes } as Extract<AgentItem, { kind: K }>;
|
|
217
|
+
session.items[index] = next;
|
|
218
|
+
emit({ type: 'item', sessionId, item: next });
|
|
219
|
+
touch(session);
|
|
220
|
+
return next;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function truncateFrom(sessionId: string, itemId: string): boolean {
|
|
224
|
+
const session = require(sessionId);
|
|
225
|
+
const index = session.items.findIndex((i) => i.id === itemId);
|
|
226
|
+
if (index === -1) return false;
|
|
227
|
+
session.items.splice(index);
|
|
228
|
+
emit({ type: 'truncate', sessionId, itemId });
|
|
229
|
+
touch(session);
|
|
230
|
+
return true;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function appendDelta(sessionId: string, itemId: string, text: string): void {
|
|
234
|
+
if (!text) return;
|
|
235
|
+
const session = require(sessionId);
|
|
236
|
+
const item = session.items.find((i) => i.id === itemId);
|
|
237
|
+
if (item?.kind !== 'assistant') return;
|
|
238
|
+
item.text += text;
|
|
239
|
+
emit({ type: 'delta', sessionId, itemId, text });
|
|
240
|
+
scheduleSave();
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function subscribe(listener: Listener): () => void {
|
|
244
|
+
listeners.add(listener);
|
|
245
|
+
return () => {
|
|
246
|
+
listeners.delete(listener);
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
async function flush(): Promise<void> {
|
|
251
|
+
if (saveTimer) {
|
|
252
|
+
clearTimeout(saveTimer);
|
|
253
|
+
saveTimer = null;
|
|
254
|
+
}
|
|
255
|
+
await save();
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return {
|
|
259
|
+
list,
|
|
260
|
+
get,
|
|
261
|
+
create,
|
|
262
|
+
remove,
|
|
263
|
+
patch,
|
|
264
|
+
appendItem,
|
|
265
|
+
updateItem,
|
|
266
|
+
truncateFrom,
|
|
267
|
+
appendDelta,
|
|
268
|
+
subscribe,
|
|
269
|
+
flush,
|
|
270
|
+
};
|
|
271
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// Codex only: Anthropic does not allow a third-party product to run on a reader's
|
|
2
|
+
// claude.ai login, so Claude Code is not offered here. A new agent is one more id.
|
|
3
|
+
export const AGENT_PROVIDER_IDS = ['codex'] as const;
|
|
4
|
+
|
|
5
|
+
export type AgentProviderId = (typeof AGENT_PROVIDER_IDS)[number];
|
|
6
|
+
|
|
7
|
+
export const AGENT_EFFORTS = ['minimal', 'low', 'medium', 'high', 'xhigh', 'max'] as const;
|
|
8
|
+
|
|
9
|
+
export type AgentEffort = (typeof AGENT_EFFORTS)[number];
|
|
10
|
+
|
|
11
|
+
export function isAgentProviderId(value: unknown): value is AgentProviderId {
|
|
12
|
+
return typeof value === 'string' && (AGENT_PROVIDER_IDS as readonly string[]).includes(value);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function isAgentEffort(value: unknown): value is AgentEffort {
|
|
16
|
+
return typeof value === 'string' && (AGENT_EFFORTS as readonly string[]).includes(value);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type AgentModelOption = {
|
|
20
|
+
// Empty when the option stands for the CLI's own default model.
|
|
21
|
+
id: string;
|
|
22
|
+
label: string;
|
|
23
|
+
efforts: AgentEffort[];
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export type AgentProviderStatus = {
|
|
27
|
+
id: AgentProviderId;
|
|
28
|
+
name: string;
|
|
29
|
+
sdkPackage: string;
|
|
30
|
+
sdkVersion: string | null;
|
|
31
|
+
cliCommand: string;
|
|
32
|
+
cliFound: boolean;
|
|
33
|
+
cliVersion: string | null;
|
|
34
|
+
ready: boolean;
|
|
35
|
+
models: AgentModelOption[];
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type AgentStatus = {
|
|
39
|
+
enabled: boolean;
|
|
40
|
+
packageManager: string;
|
|
41
|
+
providers: AgentProviderStatus[];
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
type AgentQuestionOption = { label: string; description?: string };
|
|
45
|
+
|
|
46
|
+
export type AgentQuestion = {
|
|
47
|
+
header: string;
|
|
48
|
+
question: string;
|
|
49
|
+
options: AgentQuestionOption[];
|
|
50
|
+
multiSelect: boolean;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export type AgentItem =
|
|
54
|
+
| {
|
|
55
|
+
kind: 'user';
|
|
56
|
+
id: string;
|
|
57
|
+
ts: string;
|
|
58
|
+
text: string;
|
|
59
|
+
// The provider history point just before this message, so editing it
|
|
60
|
+
// can rewind the agent's conversation there.
|
|
61
|
+
resumeAt?: string;
|
|
62
|
+
newDoc?: boolean;
|
|
63
|
+
}
|
|
64
|
+
| { kind: 'assistant'; id: string; ts: string; text: string; streaming: boolean }
|
|
65
|
+
| {
|
|
66
|
+
kind: 'tool';
|
|
67
|
+
id: string;
|
|
68
|
+
ts: string;
|
|
69
|
+
name: string;
|
|
70
|
+
title: string;
|
|
71
|
+
detail: string | null;
|
|
72
|
+
status: 'running' | 'done' | 'error';
|
|
73
|
+
}
|
|
74
|
+
| {
|
|
75
|
+
kind: 'question';
|
|
76
|
+
id: string;
|
|
77
|
+
ts: string;
|
|
78
|
+
questions: AgentQuestion[];
|
|
79
|
+
answers: Record<string, string> | null;
|
|
80
|
+
status: 'pending' | 'answered' | 'cancelled';
|
|
81
|
+
}
|
|
82
|
+
| {
|
|
83
|
+
kind: 'permission';
|
|
84
|
+
id: string;
|
|
85
|
+
ts: string;
|
|
86
|
+
tool: string;
|
|
87
|
+
title: string;
|
|
88
|
+
detail: string | null;
|
|
89
|
+
status: 'pending' | 'allowed' | 'denied';
|
|
90
|
+
}
|
|
91
|
+
| { kind: 'error'; id: string; ts: string; text: string }
|
|
92
|
+
// `open-doc check`, run by the dev server after a turn whose agent cannot
|
|
93
|
+
// start a browser itself.
|
|
94
|
+
| {
|
|
95
|
+
kind: 'check';
|
|
96
|
+
id: string;
|
|
97
|
+
ts: string;
|
|
98
|
+
docId: string;
|
|
99
|
+
status: 'running' | 'done' | 'failed';
|
|
100
|
+
pageCount: number | null;
|
|
101
|
+
// The first few findings; `total` counts them all.
|
|
102
|
+
findings: AgentCheckFinding[];
|
|
103
|
+
total: number;
|
|
104
|
+
error: string | null;
|
|
105
|
+
}
|
|
106
|
+
| {
|
|
107
|
+
kind: 'result';
|
|
108
|
+
id: string;
|
|
109
|
+
ts: string;
|
|
110
|
+
durationMs: number | null;
|
|
111
|
+
costUsd: number | null;
|
|
112
|
+
aborted: boolean;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export type AgentCheckFinding = {
|
|
116
|
+
page: number;
|
|
117
|
+
severity: 'error' | 'warn';
|
|
118
|
+
message: string;
|
|
119
|
+
loc?: string;
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
type AgentSessionStatus = 'idle' | 'running';
|
|
123
|
+
|
|
124
|
+
export type AgentSessionSummary = {
|
|
125
|
+
id: string;
|
|
126
|
+
provider: AgentProviderId;
|
|
127
|
+
title: string;
|
|
128
|
+
createdAt: string;
|
|
129
|
+
updatedAt: string;
|
|
130
|
+
status: AgentSessionStatus;
|
|
131
|
+
docId: string | null;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
export type AgentSession = AgentSessionSummary & {
|
|
135
|
+
providerSessionId: string | null;
|
|
136
|
+
providerCheckpoint?: string | null;
|
|
137
|
+
// Set once the oldest items were dropped to stay under the item cap.
|
|
138
|
+
trimmed?: boolean;
|
|
139
|
+
items: AgentItem[];
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
export type AgentTurnContext = {
|
|
143
|
+
docId?: string;
|
|
144
|
+
docTitle?: string;
|
|
145
|
+
pageNumber?: number;
|
|
146
|
+
totalPages?: number;
|
|
147
|
+
pendingComments?: number;
|
|
148
|
+
selection?: { line: number; column: number; tagName: string; text: string } | null;
|
|
149
|
+
newDoc?: boolean;
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
export type RespondPayload =
|
|
153
|
+
| { answers: Record<string, string> }
|
|
154
|
+
| { allow: boolean }
|
|
155
|
+
| { cancel: true };
|
|
156
|
+
|
|
157
|
+
export type AgentStreamEvent =
|
|
158
|
+
| { type: 'session'; session: AgentSessionSummary }
|
|
159
|
+
| { type: 'session-removed'; sessionId: string }
|
|
160
|
+
| { type: 'item'; sessionId: string; item: AgentItem }
|
|
161
|
+
| { type: 'delta'; sessionId: string; itemId: string; text: string }
|
|
162
|
+
| { type: 'truncate'; sessionId: string; itemId: string }
|
|
163
|
+
| { type: 'doc-renamed'; from: string; to: string };
|
|
164
|
+
|
|
165
|
+
export function sessionSummary(session: AgentSession): AgentSessionSummary {
|
|
166
|
+
const {
|
|
167
|
+
providerSessionId: _p,
|
|
168
|
+
providerCheckpoint: _c,
|
|
169
|
+
trimmed: _t,
|
|
170
|
+
items: _i,
|
|
171
|
+
...summary
|
|
172
|
+
} = session;
|
|
173
|
+
return summary;
|
|
174
|
+
}
|
|
@@ -313,7 +313,12 @@ export function AgentPanel({
|
|
|
313
313
|
) : (
|
|
314
314
|
<>
|
|
315
315
|
{session && session.items.length > 0 ? (
|
|
316
|
-
<Transcript
|
|
316
|
+
<Transcript
|
|
317
|
+
session={session}
|
|
318
|
+
onRespond={onRespond}
|
|
319
|
+
onEdit={onEdit}
|
|
320
|
+
onFix={(text) => send(text)}
|
|
321
|
+
/>
|
|
317
322
|
) : isNewDoc ? (
|
|
318
323
|
<NewDocState providerName={providerName} />
|
|
319
324
|
) : (
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { Check, ChevronRight, CircleAlert, Copy, Pencil } from 'lucide-react';
|
|
1
|
+
import { Check, ChevronRight, CircleAlert, Copy, LoaderCircle, Pencil } from 'lucide-react';
|
|
2
2
|
import { type ReactNode, useEffect, useLayoutEffect, useRef, useState } from 'react';
|
|
3
3
|
import { Button } from '@/components/ui/button';
|
|
4
4
|
import { useAutoGrow } from '@/lib/use-auto-grow';
|
|
5
5
|
import { format, plural, useLocale } from '@/lib/use-locale';
|
|
6
6
|
import { cn } from '@/lib/utils';
|
|
7
|
-
import { stripDocSlug } from '../../../agent/prompt';
|
|
7
|
+
import { checkFixPrompt, stripDocSlug } from '../../../agent/prompt';
|
|
8
8
|
import type { AgentItem, AgentQuestion, AgentSession, RespondPayload } from '../../../agent/types';
|
|
9
9
|
import { AgentMarkdown } from './agent-markdown';
|
|
10
10
|
|
|
@@ -12,10 +12,13 @@ export function Transcript({
|
|
|
12
12
|
session,
|
|
13
13
|
onRespond,
|
|
14
14
|
onEdit,
|
|
15
|
+
onFix,
|
|
15
16
|
}: {
|
|
16
17
|
session: AgentSession;
|
|
17
18
|
onRespond: (itemId: string, payload: RespondPayload) => Promise<void>;
|
|
18
19
|
onEdit: (itemId: string, text: string) => Promise<void>;
|
|
20
|
+
/** Sends a message asking the agent to fix what a check found. */
|
|
21
|
+
onFix: (text: string) => Promise<void>;
|
|
19
22
|
}) {
|
|
20
23
|
const scrollRef = useRef<HTMLDivElement>(null);
|
|
21
24
|
const stickRef = useRef(true);
|
|
@@ -23,15 +26,20 @@ export function Transcript({
|
|
|
23
26
|
const lastText = lastItem?.kind === 'assistant' ? lastItem.text.length : 0;
|
|
24
27
|
const running = session.status === 'running';
|
|
25
28
|
const replies = replyTexts(session.items, running);
|
|
29
|
+
// Only the newest check offers a fix; an older one was already acted on or
|
|
30
|
+
// describes a file that has changed since.
|
|
31
|
+
const lastCheckId = [...session.items].reverse().find((i) => i.kind === 'check')?.id;
|
|
26
32
|
|
|
27
33
|
// Follow the stream while the user is parked at the bottom; stop once they
|
|
28
34
|
// scroll up to read, like a terminal.
|
|
35
|
+
// `lastItem` too: an item can grow in place — a check that finishes turns a
|
|
36
|
+
// one-line "checking" into a card — without the transcript getting longer.
|
|
29
37
|
// biome-ignore lint/correctness/useExhaustiveDependencies: re-run as the transcript grows or streams
|
|
30
38
|
useLayoutEffect(() => {
|
|
31
39
|
const node = scrollRef.current;
|
|
32
40
|
if (!node || !stickRef.current) return;
|
|
33
41
|
node.scrollTop = node.scrollHeight;
|
|
34
|
-
}, [session.items.length, lastText, session.status]);
|
|
42
|
+
}, [session.items.length, lastText, session.status, lastItem]);
|
|
35
43
|
|
|
36
44
|
return (
|
|
37
45
|
<div
|
|
@@ -54,6 +62,7 @@ export function Transcript({
|
|
|
54
62
|
replyText={replies.get(block.item.id)}
|
|
55
63
|
onRespond={(payload) => onRespond(block.item.id, payload)}
|
|
56
64
|
onEdit={(text) => onEdit(block.item.id, text)}
|
|
65
|
+
onFix={block.item.id === lastCheckId && !running ? onFix : undefined}
|
|
57
66
|
/>
|
|
58
67
|
)}
|
|
59
68
|
</li>
|
|
@@ -105,12 +114,14 @@ function TranscriptItem({
|
|
|
105
114
|
replyText,
|
|
106
115
|
onRespond,
|
|
107
116
|
onEdit,
|
|
117
|
+
onFix,
|
|
108
118
|
}: {
|
|
109
119
|
item: AgentItem;
|
|
110
120
|
running: boolean;
|
|
111
121
|
replyText?: string;
|
|
112
122
|
onRespond: (payload: RespondPayload) => Promise<void>;
|
|
113
123
|
onEdit: (text: string) => Promise<void>;
|
|
124
|
+
onFix?: (text: string) => Promise<void>;
|
|
114
125
|
}) {
|
|
115
126
|
switch (item.kind) {
|
|
116
127
|
case 'user':
|
|
@@ -126,6 +137,8 @@ function TranscriptItem({
|
|
|
126
137
|
return <ErrorItem text={item.text} />;
|
|
127
138
|
case 'result':
|
|
128
139
|
return <ResultItem item={item} />;
|
|
140
|
+
case 'check':
|
|
141
|
+
return <CheckItem item={item} onFix={onFix} />;
|
|
129
142
|
default:
|
|
130
143
|
return null;
|
|
131
144
|
}
|
|
@@ -417,6 +430,101 @@ function ErrorItem({ text }: { text: string }) {
|
|
|
417
430
|
);
|
|
418
431
|
}
|
|
419
432
|
|
|
433
|
+
const CHECK_SHOWN = 5;
|
|
434
|
+
|
|
435
|
+
function CheckItem({
|
|
436
|
+
item,
|
|
437
|
+
onFix,
|
|
438
|
+
}: {
|
|
439
|
+
item: Extract<AgentItem, { kind: 'check' }>;
|
|
440
|
+
onFix?: (text: string) => Promise<void>;
|
|
441
|
+
}) {
|
|
442
|
+
const t = useLocale();
|
|
443
|
+
const [busy, runBusy] = useBusy();
|
|
444
|
+
|
|
445
|
+
if (item.status === 'running') {
|
|
446
|
+
return (
|
|
447
|
+
<p className="flex items-center gap-1.5 text-[11.5px] text-muted-foreground" data-agent-check>
|
|
448
|
+
<LoaderCircle aria-hidden className="size-3 animate-spin motion-reduce:animate-none" />
|
|
449
|
+
<span className="agent-shimmer">{t.agent.checkRunning}</span>
|
|
450
|
+
</p>
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
if (item.status === 'failed') {
|
|
454
|
+
return (
|
|
455
|
+
<p className="text-[11.5px] leading-relaxed text-muted-foreground" data-agent-check>
|
|
456
|
+
<span className="text-foreground/80">{t.agent.checkFailed}</span>
|
|
457
|
+
{item.error && item.error !== 'interrupted' && (
|
|
458
|
+
<span className="mt-0.5 block break-words font-mono text-[10.5px]">{item.error}</span>
|
|
459
|
+
)}
|
|
460
|
+
</p>
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
if (item.total === 0) {
|
|
464
|
+
return (
|
|
465
|
+
<p className="flex items-center gap-1.5 text-[11.5px] text-muted-foreground" data-agent-check>
|
|
466
|
+
<Check aria-hidden className="size-3" />
|
|
467
|
+
{item.pageCount !== null
|
|
468
|
+
? format(t.agent.checkCleanPages, { count: item.pageCount })
|
|
469
|
+
: t.agent.checkClean}
|
|
470
|
+
</p>
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
const shown = item.findings.slice(0, CHECK_SHOWN);
|
|
475
|
+
const hidden = item.total - shown.length;
|
|
476
|
+
return (
|
|
477
|
+
<div
|
|
478
|
+
className="rounded-[8px] border border-hairline bg-background px-3 py-2.5"
|
|
479
|
+
data-agent-check
|
|
480
|
+
>
|
|
481
|
+
<p className="text-[11.5px] text-muted-foreground">
|
|
482
|
+
{format(plural(item.total, t.agent.checkFound), { count: item.total })}
|
|
483
|
+
</p>
|
|
484
|
+
<ul className="mt-1.5 flex flex-col gap-1">
|
|
485
|
+
{shown.map((f, n) => (
|
|
486
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: a fixed report, never reordered
|
|
487
|
+
<li key={n} className="flex gap-1.5 text-[12px] leading-snug">
|
|
488
|
+
<span
|
|
489
|
+
aria-hidden
|
|
490
|
+
className={cn(
|
|
491
|
+
'mt-[0.45em] size-1.5 shrink-0 rounded-full',
|
|
492
|
+
f.severity === 'error' ? 'bg-destructive' : 'bg-muted-foreground/50',
|
|
493
|
+
)}
|
|
494
|
+
/>
|
|
495
|
+
<span className="min-w-0">
|
|
496
|
+
{f.page > 0 && (
|
|
497
|
+
<span className="mr-1 font-mono text-[10.5px] text-muted-foreground">
|
|
498
|
+
{format(t.agent.contextPage, { n: f.page })}
|
|
499
|
+
</span>
|
|
500
|
+
)}
|
|
501
|
+
{f.message}
|
|
502
|
+
</span>
|
|
503
|
+
</li>
|
|
504
|
+
))}
|
|
505
|
+
</ul>
|
|
506
|
+
{hidden > 0 && (
|
|
507
|
+
<p className="mt-1 text-[11px] text-muted-foreground">
|
|
508
|
+
{format(t.agent.checkMore, { count: hidden })}
|
|
509
|
+
</p>
|
|
510
|
+
)}
|
|
511
|
+
{onFix && (
|
|
512
|
+
<div className="mt-2.5 flex justify-end">
|
|
513
|
+
<Button
|
|
514
|
+
size="xs"
|
|
515
|
+
disabled={busy}
|
|
516
|
+
onClick={() =>
|
|
517
|
+
runBusy(() => onFix(checkFixPrompt(item.docId, item.findings, item.total)))
|
|
518
|
+
}
|
|
519
|
+
>
|
|
520
|
+
{t.agent.checkFix}
|
|
521
|
+
</Button>
|
|
522
|
+
</div>
|
|
523
|
+
)}
|
|
524
|
+
</div>
|
|
525
|
+
);
|
|
526
|
+
}
|
|
527
|
+
|
|
420
528
|
function ResultItem({ item }: { item: Extract<AgentItem, { kind: 'result' }> }) {
|
|
421
529
|
const t = useLocale();
|
|
422
530
|
const parts = [item.aborted ? t.agent.resultStopped : t.agent.resultDone];
|