@coda-console/resource-manager 0.9.3 → 0.9.4
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/lib/draft.mjs +22 -48
- package/lib/root.mjs +19 -47
- package/main.mjs +5 -1
- package/package.json +6 -5
package/lib/draft.mjs
CHANGED
|
@@ -1,57 +1,31 @@
|
|
|
1
|
-
//
|
|
2
|
-
//
|
|
3
|
-
//
|
|
1
|
+
// 把文字追加到会话的输入框草稿:经 coda/thread@1 的操作 append-draft。
|
|
2
|
+
// 宿主在同一事务里读草稿、按 line 插入分隔符并保存,保留现有附件、按 10 万字上限校验;
|
|
3
|
+
// 原样传 text,避免与宿主的分隔符叠加造成连续追加多出空格或空行。
|
|
4
4
|
import { DRAFT_LIMIT, ErrorCode, fail } from '../view/protocol.mjs';
|
|
5
5
|
|
|
6
|
-
const RETRIES = 2; // 版本不符后用服务端的内容重拼,最多再试这么多次
|
|
7
|
-
|
|
8
|
-
// block 为 false(路径、位置):接在现有文字后面,前后各留一个空格(已有空白或为空时前面不加)。
|
|
9
|
-
// block 为 true(内容):另起一行,结尾换行;文字本身已以换行结尾的不再多加,免得多出空行。只能追加到末尾。
|
|
10
|
-
export function compose(current, text, block) {
|
|
11
|
-
if (block) return `${current}${current === '' || current.endsWith('\n') ? '' : '\n'}${text}${text.endsWith('\n') ? '' : '\n'}`;
|
|
12
|
-
return `${current}${current === '' || /\s$/.test(current) ? '' : ' '}${text} `;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
// 接口路径只在这里写:测试和 harness 的假接口也从这里取。
|
|
16
|
-
export const draftPath = session => `threads/${encodeURIComponent(session)}/draft`;
|
|
17
|
-
// 反过来:路径是草稿接口的返回会话,不是返回 null。
|
|
18
|
-
export function draftSession(path) {
|
|
19
|
-
const match = /^threads\/([^/]+)\/draft$/.exec(path);
|
|
20
|
-
return match ? decodeURIComponent(match[1]) : null;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
6
|
const failed = () => fail(ErrorCode.DRAFT_FAILED, '读写草稿失败');
|
|
7
|
+
const tooLong = () => fail(ErrorCode.DRAFT_TOO_LONG, '草稿太长,放不下');
|
|
24
8
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
if (
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
return { text: draft.text, revision: draft.revision, ids };
|
|
9
|
+
function isTooLong(error) {
|
|
10
|
+
const code = typeof error?.code === 'string' ? error.code.toLowerCase() : '';
|
|
11
|
+
if (code === ErrorCode.DRAFT_TOO_LONG || code === ErrorCode.TOO_LARGE || code === 'too_long' || code === 'payload_too_large') return true;
|
|
12
|
+
const message = typeof error?.message === 'string' ? error.message : '';
|
|
13
|
+
return /too[_ ]?long|limit|上限|太长|过长|超出|超过/i.test(message);
|
|
31
14
|
}
|
|
32
15
|
|
|
33
|
-
// 追加成功返回 true;valid()
|
|
34
|
-
//
|
|
35
|
-
// 失败抛 draft_failed
|
|
36
|
-
export async function addText(coda, session, { text, block }, { valid = () => true } = {}) {
|
|
16
|
+
// 追加成功返回 true;valid() 变假或 signal 已中止(会话换了、视图关了)静默结束,返回 false:不写、不报。
|
|
17
|
+
// block 为 true(内容)时传 line: true,false(路径、位置)时传 line: false。
|
|
18
|
+
// 失败抛 draft_failed,拼完超过上限抛 draft_too_long,不截断。
|
|
19
|
+
export async function addText(coda, session, { text, block }, { valid = () => true, signal } = {}) {
|
|
37
20
|
if (typeof text !== 'string' || text.includes('\0') || typeof block !== 'boolean') throw failed();
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
for (let attempt = 0; ; attempt++) {
|
|
47
|
-
const next = compose(draft.text, text, block);
|
|
48
|
-
if (next.length > DRAFT_LIMIT) throw fail(ErrorCode.DRAFT_TOO_LONG, '草稿太长,放不下');
|
|
49
|
-
const saved = await call({ text: next, attachments: draft.ids, revision: draft.revision });
|
|
50
|
-
if (!valid()) return false;
|
|
51
|
-
const reply = parseDraft(saved?.draft); // 成功和版本不符的回答都带 draft;没有或形状不对,不能当成已写入
|
|
52
|
-
if (!saved.conflict) return true;
|
|
53
|
-
if (attempt >= RETRIES) throw failed();
|
|
54
|
-
draft = reply;
|
|
55
|
-
if (!valid()) return false;
|
|
21
|
+
if (text.length > DRAFT_LIMIT) throw tooLong();
|
|
22
|
+
if (!valid() || signal?.aborted) return false;
|
|
23
|
+
try {
|
|
24
|
+
await coda.call({ kind: 'coda/thread@1', id: session }, 'append-draft', { text, line: block }, signal ? { signal } : undefined);
|
|
25
|
+
} catch (error) {
|
|
26
|
+
if (!valid() || signal?.aborted) return false;
|
|
27
|
+
if (isTooLong(error)) throw tooLong();
|
|
28
|
+
throw failed();
|
|
56
29
|
}
|
|
30
|
+
return valid() && !signal?.aborted;
|
|
57
31
|
}
|
package/lib/root.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//
|
|
1
|
+
// 从会话取根目录:经 coda/thread@1 的只读查询 workspace 取 { path, project: { id, name }, state }。
|
|
2
2
|
import { realpath, stat } from 'node:fs/promises';
|
|
3
3
|
import { RootState } from '../view/protocol.mjs';
|
|
4
4
|
|
|
@@ -7,15 +7,10 @@ export const NO_ROOT = Object.freeze({ state: RootState.NONE, path: null, projec
|
|
|
7
7
|
// project 不是根目录的身份(保存、恢复、换根都只看路径),但变了页面要重画标题,所以算进来。
|
|
8
8
|
export const sameRoot = (a, b) => a?.state === b?.state && a?.path === b?.path && (a?.project ?? null) === (b?.project ?? null);
|
|
9
9
|
|
|
10
|
-
//
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
// 取不到(没有 id、接口失败、列表里没有这个 id、名字为空)都是 null,页面退回显示文件夹名;这里从不抛:
|
|
15
|
-
// queryRoot 抛出的意思是「保留上一次的根目录」,项目名取不到不该触发它。
|
|
16
|
-
// 项目列表按机器缓存(WeakMap:一个 coda 一份),很少变:超过 MAX_AGE 或查不到这个 id 时重取,两次请求之间至少隔 MISS_AGE(接口失败也算一次),不会反复请求;同时发起的查询共用一次请求。
|
|
17
|
-
const MAX_AGE = 60_000, MISS_AGE = 5_000, NAME_LIMIT = 256;
|
|
18
|
-
const caches = new WeakMap(); // coda → { names: Map(id → name) | null, at: 上次请求的时间, pending: 在途的请求 | null }
|
|
10
|
+
// Coda 的 worktree 目录以 UUID 命名,只看目录名认不出项目,所以给页面带上项目名;普通目录的目录名本身就能认出,project 留 null。
|
|
11
|
+
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
12
|
+
const NAME_LIMIT = 256;
|
|
13
|
+
const baseName = path => path.split('/').filter(Boolean).pop() ?? '';
|
|
19
14
|
|
|
20
15
|
// 名字:去掉首尾空白,空的、含控制字符的不用,太长的按码点截断。
|
|
21
16
|
function cleanName(name) {
|
|
@@ -25,47 +20,24 @@ function cleanName(name) {
|
|
|
25
20
|
return Array.from(text).slice(0, NAME_LIMIT).join('');
|
|
26
21
|
}
|
|
27
22
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
try {
|
|
31
|
-
const reply = await coda.api('agents');
|
|
32
|
-
const names = new Map();
|
|
33
|
-
for (const project of Array.isArray(reply?.projects) ? reply.projects : []) {
|
|
34
|
-
if (typeof project?.id === 'string' && project.id) names.set(project.id, cleanName(project.name));
|
|
35
|
-
}
|
|
36
|
-
cache.names = names;
|
|
37
|
-
} catch { /* 取不到:保留上一次的列表,名字退回文件夹名 */ }
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
async function projectNameOf(coda, projectId, now) {
|
|
41
|
-
if (typeof projectId !== 'string' || !projectId) return null;
|
|
42
|
-
try {
|
|
43
|
-
let cache = caches.get(coda);
|
|
44
|
-
if (!cache) caches.set(coda, cache = { names: null, at: -Infinity, pending: null });
|
|
45
|
-
const age = now() - cache.at, stale = !cache.names || age >= MAX_AGE || (!cache.names.has(projectId) && age >= MISS_AGE);
|
|
46
|
-
if (stale) await (cache.pending ??= fetchNames(coda, cache, now).finally(() => { cache.pending = null; }));
|
|
47
|
-
else if (cache.pending) await cache.pending;
|
|
48
|
-
return cache.names?.get(projectId) ?? null;
|
|
49
|
-
} catch {
|
|
50
|
-
return null;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// checkout.state 是 preparing、ready、failed。failed 没有可看的目录,按 none;
|
|
55
|
-
// ready 但目录已不在(归档后工作树被回收)是 gone。
|
|
23
|
+
// workspace.state 取 preparing、ready、failed、gone 之一,path 只在 ready 时有值。
|
|
24
|
+
// failed 没有可看的目录,按 none;ready 但目录已不在(归档后工作树被回收)按 gone。
|
|
56
25
|
// 查询失败(接口出错、读目录权限)直接抛出,由调用方保留上一次的结果。
|
|
57
|
-
export async function queryRoot(coda, session
|
|
26
|
+
export async function queryRoot(coda, session) {
|
|
58
27
|
if (!session) return NO_ROOT;
|
|
59
|
-
const detail = await coda.
|
|
60
|
-
|
|
61
|
-
if (
|
|
62
|
-
if (
|
|
63
|
-
if (
|
|
28
|
+
const detail = await coda.query({ kind: 'coda/thread@1', id: session }, 'workspace', {});
|
|
29
|
+
if (!detail || typeof detail !== 'object') return NO_ROOT;
|
|
30
|
+
if (detail.state === 'preparing') return { state: RootState.PREPARING, path: null, project: null };
|
|
31
|
+
if (detail.state === 'gone') return { state: RootState.GONE, path: typeof detail.path === 'string' && detail.path ? detail.path : null, project: null };
|
|
32
|
+
if (detail.state !== 'ready' || typeof detail.path !== 'string' || !detail.path) return NO_ROOT;
|
|
64
33
|
try {
|
|
65
|
-
const real = await realpath(
|
|
66
|
-
if ((await stat(real)).isDirectory())
|
|
34
|
+
const real = await realpath(detail.path);
|
|
35
|
+
if ((await stat(real)).isDirectory()) {
|
|
36
|
+
const project = UUID.test(baseName(real)) ? cleanName(detail.project?.name) : null;
|
|
37
|
+
return { state: RootState.READY, path: real, project };
|
|
38
|
+
}
|
|
67
39
|
} catch (error) {
|
|
68
40
|
if (error?.code !== 'ENOENT' && error?.code !== 'ENOTDIR') throw error;
|
|
69
41
|
}
|
|
70
|
-
return { state: RootState.GONE, path:
|
|
42
|
+
return { state: RootState.GONE, path: detail.path, project: null };
|
|
71
43
|
}
|
package/main.mjs
CHANGED
|
@@ -39,6 +39,7 @@ function serve(coda, view, sessionOf, { makeListing, sizeLimits, searchLimits, m
|
|
|
39
39
|
const s = {
|
|
40
40
|
session: sessionOf(view.context), generation: 1, root: NO_ROOT, sent: null,
|
|
41
41
|
hello: false, closed: false, rootSeq: 0, poll: null,
|
|
42
|
+
abort: new AbortController(), // 绑定当前会话代次与视图生命周期,换会话或关闭时中止在途的草稿写入
|
|
42
43
|
listing: makeListing(), // 这个视图共用的并发上限和快照;视图关闭、换会话、根目录变化时丢弃
|
|
43
44
|
};
|
|
44
45
|
const turns = new Map();
|
|
@@ -102,6 +103,8 @@ function serve(coda, view, sessionOf, { makeListing, sizeLimits, searchLimits, m
|
|
|
102
103
|
|
|
103
104
|
function switchSession(session) {
|
|
104
105
|
stopPoll();
|
|
106
|
+
s.abort.abort();
|
|
107
|
+
s.abort = new AbortController();
|
|
105
108
|
s.session = session ?? null;
|
|
106
109
|
s.generation++;
|
|
107
110
|
resetReads();
|
|
@@ -134,7 +137,7 @@ function serve(coda, view, sessionOf, { makeListing, sizeLimits, searchLimits, m
|
|
|
134
137
|
},
|
|
135
138
|
async [Type.ADD_TEXT](message, { generation }) {
|
|
136
139
|
if (!s.session) throw fail(ErrorCode.NO_ROOT, '没有会话');
|
|
137
|
-
return await addText(coda, s.session, message, { valid: () => !stale(generation) }) ? {} : null;
|
|
140
|
+
return await addText(coda, s.session, message, { valid: () => !stale(generation), signal: s.abort.signal }) ? {} : null;
|
|
138
141
|
},
|
|
139
142
|
};
|
|
140
143
|
// 回答依赖根目录的:读完回来根目录换了,不能带旧根目录的内容。
|
|
@@ -206,6 +209,7 @@ function serve(coda, view, sessionOf, { makeListing, sizeLimits, searchLimits, m
|
|
|
206
209
|
|
|
207
210
|
view.onClose(async () => {
|
|
208
211
|
s.closed = true;
|
|
212
|
+
s.abort.abort();
|
|
209
213
|
resetReads();
|
|
210
214
|
stopPoll();
|
|
211
215
|
try { await (await watching)?.dispose(); } catch { /* 已经取消 */ }
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"name": "@coda-console/resource-manager",
|
|
4
4
|
"displayName": "资源管理器",
|
|
5
5
|
"description": "会话旁的只读文件浏览器:浏览会话工作目录,预览文本、图片、音频、视频和 3D 模型,把路径交给对话。",
|
|
6
|
-
"version": "0.9.
|
|
6
|
+
"version": "0.9.4",
|
|
7
7
|
"categories": ["工作流程"],
|
|
8
8
|
"license": "MIT",
|
|
9
9
|
"repository": {
|
|
@@ -15,14 +15,15 @@
|
|
|
15
15
|
"scripts": {
|
|
16
16
|
"test": "node --test test/*.test.mjs",
|
|
17
17
|
"test:webgl": "node test/harness/inspect3d.mjs",
|
|
18
|
-
"check:coda": "npx --yes @coda-console/plugin-check@0.
|
|
18
|
+
"check:coda": "npx --yes @coda-console/plugin-check@0.8.31 . --manifest"
|
|
19
19
|
},
|
|
20
|
+
"engines": { "coda": 3 },
|
|
20
21
|
"main": "main.mjs",
|
|
21
|
-
"uses": { "coda/thread@1": ["read"] },
|
|
22
|
+
"uses": { "coda/thread@1": ["read", "workspace", "append-draft"] },
|
|
22
23
|
"contributes": {
|
|
23
24
|
"views": [
|
|
24
|
-
{ "id": "files", "title": "资源管理器", "entry": "view/index.html", "in": ["side"], "icon": "folder" },
|
|
25
|
-
{ "id": "file", "title": "文件", "entry": "view/file.html" }
|
|
25
|
+
{ "id": "files", "title": "资源管理器", "entry": "view/index.html", "in": ["side"], "icon": "folder", "frame": "canvas" },
|
|
26
|
+
{ "id": "file", "title": "文件", "entry": "view/file.html", "frame": "canvas" }
|
|
26
27
|
]
|
|
27
28
|
}
|
|
28
29
|
}
|