@coda-console/friends 0.5.0 → 0.7.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/main.mjs CHANGED
@@ -1,45 +1,77 @@
1
- // 好友插件的模块(Coda 的 docs/plugins.md「插件宿主」)。好友页自己的 Accord 读写在页面里发起(页面用自己的 coda.query /
2
- // coda.call / coda.watch);模块管两件事:
3
- // - 好友的本地备注:存在插件自己的持久目录 notes.json 里,不外发,页面通过视图消息读写,
4
- // 也能用 `coda self notes.list|notes.set|notes.delete` 读写;
1
+ // 好友插件的模块(Coda 的 docs/plugins.md「插件宿主」)。好友页自己的 Accord 读写在页面里发起(页面用自己的 coda.watch /
2
+ // coda.call);模块管这几样:
5
3
  // - 业务无关的社交网关 friends/social@1(lib/gateway.mjs):别的插件经它读好友、发布与撤回记录,全部透传 coda/accord@1;
6
- // 发布策略用 `coda self policy.get|policy.setKind` 读写。
4
+ // 发布策略用 `coda self policy.get|policy.setKind` 读写;
5
+ // - 签名网关的持有方(lib/rpc-holder.mjs):把本机 remote-exec 记录的 exec 授给好友、转发好友签名的调用,
6
+ // 授权与操作记录用 `coda self grants.list|grants.grant|grants.revoke|ledger.list` 读写。持有方出错不影响社交网关。
7
+ // - 签名网关的调用方(lib/rpc-caller.mjs):把好友授给本机的记录登记成代理记录,接住对它的 exec 转给好友签名执行。调用方出错不影响社交网关与持有方。
8
+ // 好友授给本机的代理记录用 `coda self proxies.list` 读;授予时可选的本机 remote-exec 记录用 `coda self grants.targets` 读。
9
+ // 持有方、调用方的这几样(shared)好友页也经视图消息调用。
7
10
  import { createGateway } from './lib/gateway.mjs';
8
- import { deleteNote, readNotes, setNote } from './lib/notes.mjs';
11
+ import { createCaller } from './lib/rpc-caller.mjs';
12
+ import { createHolder } from './lib/rpc-holder.mjs';
13
+ import { createRpcStore } from './lib/rpc-store.mjs';
9
14
 
10
- let dir = null, gateway = null;
15
+ const HOLDER_ACTIONS = ['grants.list', 'grants.grant', 'grants.revoke', 'ledger.list'];
16
+ const TARGET_KIND = 'remote-exec/machine@1';
17
+ let gateway = null, holder = null, caller = null, callerStart = null, callerDown = null;
11
18
  const handles = [];
12
19
 
13
20
  export async function activate(coda) {
14
- dir = coda.plugin.state;
15
- const actions = {
16
- 'notes.list': () => readNotes(dir),
17
- 'notes.set': ({ node, note } = {}) => setNote(dir, node, note),
18
- 'notes.delete': ({ node } = {}) => deleteNote(dir, node),
19
- };
20
- const run = (name, input) => {
21
- if (!dir) throw new Error('这台机器没有插件状态目录,备注存不下');
22
- return actions[name](input);
21
+ const dir = coda.plugin.state;
22
+ // 好友页的「共享」一节用的:持有方的四个操作、调用方的代理记录、本机可授出的 remote-exec 记录。
23
+ const shared = {
24
+ ...Object.fromEntries(HOLDER_ACTIONS.map(name => [name, input => {
25
+ if (!holder) throw new Error('这台机器没有插件状态目录,好友授权存不下');
26
+ return holder.self[name](input);
27
+ }])),
28
+ 'proxies.list': async input => {
29
+ if (!caller) throw new Error('这台机器没有插件状态目录,好友授给本机的授权读不出来');
30
+ await callerStart;
31
+ if (callerDown) throw new Error(callerDown);
32
+ return caller.list(input);
33
+ },
34
+ 'grants.targets': async () => (await coda.list(TARGET_KIND, { scope: 'local' })).filter(entry => entry.local === true && entry.plugin === 'remote-exec')
35
+ .map(entry => ({ id: entry.id, name: entry.spec?.name ?? entry.id, os: entry.spec?.os ?? '', user: entry.spec?.user ?? '', available: entry.status?.available === true })),
23
36
  };
24
- for (const name of Object.keys(actions)) handles.push(coda.self.handle(name, input => run(name, input ?? {})));
25
37
  coda.views.register('friends', {
26
38
  open(view) {
27
39
  view.onMessage(async message => {
28
40
  const name = message?.type;
29
- if (!Object.hasOwn(actions, name) || typeof message.rid !== 'string') return;
30
- try { view.postMessage({ type: 'reply', rid: message.rid, ok: true, value: await run(name, message) }); }
31
- catch (e) { view.postMessage({ type: 'reply', rid: message.rid, ok: false, error: String(e?.message ?? e) }); }
41
+ if (!Object.hasOwn(shared, name) || typeof message.rid !== 'string') return;
42
+ try { view.postMessage({ type: 'reply', rid: message.rid, ok: true, value: await shared[name](message) }); }
43
+ catch (e) { view.postMessage({ type: 'reply', rid: message.rid, ok: false, error: String(e?.message ?? e), effects: e?.effects }); }
32
44
  });
33
45
  },
34
46
  });
35
47
  gateway = createGateway(coda, dir);
36
48
  for (const [name, fn] of Object.entries(gateway.policy)) handles.push(coda.self.handle(name, input => fn(input ?? {})));
37
49
  await gateway.start();
50
+ if (dir) {
51
+ const store = createRpcStore(dir), rpc = { store, signRpc: gateway.signRpc, retractRpc: gateway.retractRpc, onAccord: gateway.onAccord, pages: gateway.pages };
52
+ try {
53
+ holder = createHolder(coda, rpc);
54
+ holder.start().catch(() => {});
55
+ } catch { holder = null; }
56
+ try {
57
+ caller = createCaller(coda, rpc);
58
+ callerStart = caller.start().catch(async e => {
59
+ // 只有协议确实不在(读它的记录报 not_found)才说没装;别的启动失败按实际原因报。
60
+ const absent = await coda.list(TARGET_KIND, { scope: 'local' }).then(() => false, error => error?.code === 'not_found');
61
+ callerDown = absent ? '不可用:这台没装远程执行插件,好友授给本机的远程执行用不了' : `好友授给本机的远程执行没能启动:${e?.message ?? e}`;
62
+ });
63
+ } catch { caller = null; }
64
+ }
65
+ for (const [name, fn] of Object.entries(shared)) handles.push(coda.self.handle(name, input => fn(input ?? {})));
38
66
  }
39
67
 
40
68
  export function deactivate() {
69
+ caller?.stop();
70
+ caller = null;
71
+ callerStart = callerDown = null;
72
+ holder?.stop();
73
+ holder = null;
41
74
  gateway?.stop();
42
75
  gateway = null;
43
76
  for (const handle of handles.splice(0)) handle?.dispose?.();
44
- dir = null;
45
77
  }
package/package.json CHANGED
@@ -2,15 +2,15 @@
2
2
  "name": "@coda-console/friends",
3
3
  "id": "friends",
4
4
  "displayName": "好友",
5
- "description": "管理 Accord 身份、个人名片与熟人好友通道",
5
+ "description": "管理 Accord 身份与熟人好友通道,并提供业务无关的好友网关",
6
6
  "icon": { "symbol": "person.2.fill", "color": "blue" },
7
- "version": "0.5.0",
7
+ "version": "0.7.0",
8
8
  "categories": [
9
9
  "Skills 与工具"
10
10
  ],
11
11
  "type": "module",
12
12
  "engines": {
13
- "coda": 13
13
+ "coda": 17
14
14
  },
15
15
  "main": "main.mjs",
16
16
  "activationEvents": [
@@ -23,6 +23,11 @@
23
23
  "manage"
24
24
  ]
25
25
  },
26
+ "optionalUses": {
27
+ "remote-exec/machine@1": [
28
+ "use"
29
+ ]
30
+ },
26
31
  "contributes": {
27
32
  "views": [
28
33
  {
@@ -40,7 +45,7 @@
40
45
  {
41
46
  "name": "friends/social@1",
42
47
  "title": "好友社交网关",
43
- "description": "业务无关的好友网关:读好友与好友分享的记录,向好友发布、撤回记录,设置可代签的社交名片 friends/card,发私信 friends/dm、好友动态 friends/feed,并以 dm、feed 两条帧流实时跟随;签名与记录全部透传 coda/accord@1,没有身份或证书过期时在本机排队,恢复后自动补发。",
48
+ "description": "业务无关的好友网关:读好友与好友分享的记录,向好友发布、撤回记录;签名与记录全部透传 coda/accord@1,没有身份或证书过期时在本机排队,恢复后自动补发。",
44
49
  "spec": {
45
50
  "type": "object",
46
51
  "properties": { "version": { "const": 1 } },
@@ -73,10 +78,9 @@
73
78
  "properties": {
74
79
  "selfAuthor": { "type": ["string", "null"] },
75
80
  "certState": { "enum": ["no_identity", "needs_cert", "active"] },
76
- "friends": { "type": "array", "items": { "type": "object" } },
77
- "truncated": { "type": "boolean" }
81
+ "friends": { "type": "array", "items": { "type": "object" } }
78
82
  },
79
- "required": ["selfAuthor", "certState", "friends", "truncated"],
83
+ "required": ["selfAuthor", "certState", "friends"],
80
84
  "additionalProperties": false
81
85
  }
82
86
  },
@@ -136,145 +140,6 @@
136
140
  "additionalProperties": false
137
141
  }
138
142
  },
139
- "set-card": {
140
- "idempotent": true,
141
- "input": {
142
- "type": "object",
143
- "properties": {
144
- "name": { "type": "string", "minLength": 1, "maxLength": 40 },
145
- "emoji": { "type": "string", "maxLength": 8 },
146
- "color": { "enum": ["blue", "orange", "green", "purple", "pink", "teal", "gray"] },
147
- "avatar": {
148
- "anyOf": [
149
- {
150
- "type": "object",
151
- "description": "内容寻址的图片指针(BlobPointer):path 只收 content/<sha256>,sha256 须与 path 里的一致",
152
- "properties": {
153
- "owner": { "type": "string", "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$" },
154
- "path": { "type": "string", "pattern": "^content/[0-9a-f]{64}$" },
155
- "sha256": { "type": "string", "pattern": "^[0-9a-f]{64}$" },
156
- "mime": { "type": "string", "minLength": 1, "maxLength": 128 },
157
- "size": { "type": "integer", "minimum": 0, "maximum": 5242880 },
158
- "name": { "type": "string", "minLength": 1, "maxLength": 200 }
159
- },
160
- "required": ["owner", "path", "sha256"],
161
- "additionalProperties": false
162
- },
163
- { "type": "null" }
164
- ]
165
- }
166
- },
167
- "required": ["name"],
168
- "additionalProperties": false
169
- },
170
- "result": {
171
- "type": "object",
172
- "properties": {
173
- "state": { "enum": ["synced", "queued", "paused", "no_identity", "needs_cert", "error"] },
174
- "skipped": { "type": "boolean" },
175
- "author": { "type": ["string", "null"] },
176
- "version": { "type": "integer", "minimum": 0 },
177
- "error": { "type": "string" }
178
- },
179
- "required": ["state"],
180
- "additionalProperties": false
181
- }
182
- },
183
- "send-dm": {
184
- "idempotent": true,
185
- "input": {
186
- "type": "object",
187
- "properties": {
188
- "to": { "type": "string", "pattern": "^ed25519:[0-9a-f]{64}$" },
189
- "id": { "type": "string", "minLength": 1, "maxLength": 200, "pattern": "^[^\\u0000-\\u001f\\u007f-\\u009f]*[^\\s\\u0000-\\u001f\\u007f-\\u009f][^\\u0000-\\u001f\\u007f-\\u009f]*$", "description": "至多 200 个字符且 UTF-8 至多 256 字节,不含控制字符,去首尾空白后不能为空;省略由网关生成 UUID" },
190
- "text": { "type": "string", "maxLength": 8000, "description": "至多 8000 个字符(码点,按原样计,含首尾空白),发布时去首尾空白" },
191
- "attachments": {
192
- "type": "array",
193
- "minItems": 1,
194
- "maxItems": 4,
195
- "items": {
196
- "type": "object",
197
- "description": "内容寻址的图片指针(BlobPointer):path 只收 content/<sha256>,sha256 须与 path 里的一致",
198
- "properties": {
199
- "owner": { "type": "string", "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$" },
200
- "path": { "type": "string", "pattern": "^content/[0-9a-f]{64}$" },
201
- "sha256": { "type": "string", "pattern": "^[0-9a-f]{64}$" },
202
- "mime": { "type": "string", "minLength": 1, "maxLength": 128 },
203
- "size": { "type": "integer", "minimum": 0, "maximum": 5242880 },
204
- "name": { "type": "string", "minLength": 1, "maxLength": 200 }
205
- },
206
- "required": ["owner", "path", "sha256"],
207
- "additionalProperties": false
208
- }
209
- },
210
- "replyTo": { "type": "string", "minLength": 1, "maxLength": 200, "pattern": "^[^\\u0000-\\u001f\\u007f-\\u009f]*[^\\s\\u0000-\\u001f\\u007f-\\u009f][^\\u0000-\\u001f\\u007f-\\u009f]*$", "description": "所回复私信的 id:与 id 同样至多 200 个字符且 UTF-8 至多 256 字节,不含控制字符" }
211
- },
212
- "required": ["to"],
213
- "additionalProperties": false
214
- },
215
- "result": {
216
- "type": "object",
217
- "properties": {
218
- "state": { "enum": ["synced", "queued", "paused", "no_identity", "needs_cert", "error"] },
219
- "id": { "type": "string" },
220
- "skipped": { "type": "boolean" },
221
- "author": { "type": ["string", "null"] },
222
- "version": { "type": "integer", "minimum": 0 },
223
- "error": { "type": "string" }
224
- },
225
- "required": ["state"],
226
- "additionalProperties": false
227
- }
228
- },
229
- "post-feed": {
230
- "idempotent": true,
231
- "input": {
232
- "type": "object",
233
- "properties": {
234
- "id": { "type": "string", "minLength": 1, "maxLength": 200, "pattern": "^[^\\u0000-\\u001f\\u007f-\\u009f]*[^\\s\\u0000-\\u001f\\u007f-\\u009f][^\\u0000-\\u001f\\u007f-\\u009f]*$", "description": "至多 200 个字符且 UTF-8 至多 256 字节,不含控制字符,去首尾空白后不能为空;省略由网关生成 UUID" },
235
- "text": { "type": "string", "maxLength": 8000, "description": "至多 8000 个字符(码点,按原样计,含首尾空白),发布时去首尾空白" },
236
- "media": {
237
- "type": "array",
238
- "minItems": 1,
239
- "maxItems": 4,
240
- "items": {
241
- "type": "object",
242
- "description": "内容寻址的图片指针(BlobPointer):path 只收 content/<sha256>,sha256 须与 path 里的一致",
243
- "properties": {
244
- "owner": { "type": "string", "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$" },
245
- "path": { "type": "string", "pattern": "^content/[0-9a-f]{64}$" },
246
- "sha256": { "type": "string", "pattern": "^[0-9a-f]{64}$" },
247
- "mime": { "type": "string", "minLength": 1, "maxLength": 128 },
248
- "size": { "type": "integer", "minimum": 0, "maximum": 5242880 },
249
- "name": { "type": "string", "minLength": 1, "maxLength": 200 }
250
- },
251
- "required": ["owner", "path", "sha256"],
252
- "additionalProperties": false
253
- }
254
- },
255
- "audience": {
256
- "type": "array",
257
- "items": { "type": "string", "pattern": "^(?:public|ed25519:[0-9a-f]{64})$" },
258
- "minItems": 1,
259
- "maxItems": 500
260
- }
261
- },
262
- "additionalProperties": false
263
- },
264
- "result": {
265
- "type": "object",
266
- "properties": {
267
- "state": { "enum": ["synced", "queued", "paused", "no_identity", "needs_cert", "error"] },
268
- "id": { "type": "string" },
269
- "skipped": { "type": "boolean" },
270
- "author": { "type": ["string", "null"] },
271
- "version": { "type": "integer", "minimum": 0 },
272
- "error": { "type": "string" }
273
- },
274
- "required": ["state"],
275
- "additionalProperties": false
276
- }
277
- },
278
143
  "retract": {
279
144
  "idempotent": true,
280
145
  "input": {
@@ -335,42 +200,22 @@
335
200
  }
336
201
  }
337
202
  },
338
- "streams": {
339
- "dm": {
340
- "shape": "read",
341
- "frames": true,
342
- "description": "私信的实时帧流:首帧 { type: 'snapshot', cursor, items }(按 createdAt 升序),之后每次变化一帧 { type: 'upsert', cursor, item } 或 { type: 'retract', cursor, author, id, peer };peer 只看与这位好友的私信,after 给上次的 cursor 只补增量,续不上回到快照",
343
- "input": {
344
- "type": "object",
345
- "properties": {
346
- "peer": { "type": "string", "pattern": "^ed25519:[0-9a-f]{64}$" },
347
- "after": { "type": "string", "minLength": 1, "maxLength": 128 }
348
- },
349
- "additionalProperties": false
350
- }
351
- },
352
- "feed": {
353
- "shape": "read",
354
- "frames": true,
355
- "description": "好友动态的实时帧流:首帧 { type: 'snapshot', cursor, items }(按 createdAt 降序),之后每次变化一帧 { type: 'upsert', cursor, item } 或 { type: 'retract', cursor, author, id };author 只看这一位的,after 给上次的 cursor 只补增量,续不上回到快照",
356
- "input": {
357
- "type": "object",
358
- "properties": {
359
- "author": { "type": "string", "pattern": "^ed25519:[0-9a-f]{64}$" },
360
- "after": { "type": "string", "minLength": 1, "maxLength": 128 }
361
- },
362
- "additionalProperties": false
363
- }
364
- }
365
- },
366
203
  "permissions": {
367
- "read": { "title": "查看好友列表、读取好友分享的记录与实时私信、动态", "read": true, "operations": ["friends", "records"], "streams": ["dm", "feed"] },
368
- "publish": { "title": "通过好友网关发布、撤回记录、设置社交名片、发私信与动态、查看待发队列", "operations": ["publish", "set-card", "send-dm", "post-feed", "retract", "outbox"] }
204
+ "read": { "title": "查看好友列表、读取好友分享的记录", "read": true, "operations": ["friends", "records"] },
205
+ "publish": { "title": "通过好友网关发布、撤回记录、查看待发队列", "operations": ["publish", "retract", "outbox"] }
369
206
  }
370
207
  }
371
208
  ],
372
209
  "protocolImplementations": [
373
210
  "friends/social@1"
211
+ ],
212
+ "optionalProtocolImplementations": [
213
+ "remote-exec/machine@1"
214
+ ],
215
+ "reservedAccordKinds": [
216
+ "friends/rpc-grant",
217
+ "friends/rpc-call",
218
+ "friends/rpc-answer"
374
219
  ]
375
220
  },
376
221
  "license": "MIT",