cortico-world-bottle 0.2.1 → 0.2.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/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  > 这里写的是**用户能感觉到的变化**。实现细节、服务端与运维相关的内容不写在这里。
4
4
 
5
+ ## 0.2.2
6
+
7
+ - 有了新的入口:对着 Coo 说一句「打开信箱页」(或「我想看信」「页面在哪」),它会把那个页面在你的浏览器里打开。
8
+ 以前只能自己手输地址,现在一句话就够。
9
+ - 它只会打开你自己机器上的那一个页面,打不开时会老实把地址念给你,不会假装成功。
10
+ - 读一封别人写来的信那一轮,它不会顺手去开别的页面——那一轮只用来写信。
11
+ - 没有其它行为改动。
12
+
5
13
  ## 0.2.1
6
14
 
7
15
  - 安装包瘦了一圈:不再附带部署与自托管相关的文件,只留运行需要的东西。
package/describe.json CHANGED
@@ -69,6 +69,18 @@
69
69
  "en": "Drafts an answer to the letter the human just opened. Only available in that turn."
70
70
  }
71
71
  },
72
+ {
73
+ "name": "bottle_open_page",
74
+ "description": "Open the human's mailbox page (a local page on their own machine, 127.0.0.1 only) in their default browser. Use it whenever the human asks to see the mailbox, the letters, or the page, and whenever they cannot find where to read or send. The page is the only place letters can be read or sent; you cannot do either yourself. The URL is fixed and not a parameter, so this tool can never open anything else.",
75
+ "parameters": {
76
+ "type": "object",
77
+ "additionalProperties": false,
78
+ "properties": {}
79
+ },
80
+ "tags": [
81
+ "act"
82
+ ]
83
+ },
72
84
  {
73
85
  "name": "bottle_mailbox",
74
86
  "description": "What is in the mailbox right now: the letter you are holding that still needs an answer, and letters of yours that someone answered. Returns pseudonyms and states only, never letter text.",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cortico-world-bottle",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "type": "module",
5
5
  "description": "Coo 写一封不超过 120 字的信,寄不寄由人类在本地信箱页上决定;别人家的 Coo 捞到后看不到字,只有人类点了回信,那一封才会被读。",
6
6
  "keywords": [
@@ -0,0 +1,58 @@
1
+ /**
2
+ * 用系统默认浏览器打开一个地址。
3
+ *
4
+ * **只允许回环地址**:这个函数绝不能变成「任意 URL 启动器」——否则一段被捞上来的
5
+ * 信、或者别的 World 的事件里写了句「打开这个链接」,就可能把用户导到别处去。
6
+ */
7
+ import { spawn } from 'node:child_process';
8
+
9
+ export function isLoopbackUrl(raw: string): boolean {
10
+ try {
11
+ const url = new URL(raw);
12
+ if (url.protocol !== 'http:' && url.protocol !== 'https:') return false;
13
+ return url.hostname === '127.0.0.1' || url.hostname === 'localhost' || url.hostname === '::1';
14
+ } catch {
15
+ return false;
16
+ }
17
+ }
18
+
19
+ function launch(url: string): { command: string; args: string[] } {
20
+ if (process.platform === 'win32') {
21
+ // rundll32 走 system handler,不用拼 cmd 的引号,也没有窗口一闪
22
+ return { command: 'rundll32', args: ['url.dll,FileProtocolHandler', url] };
23
+ }
24
+ if (process.platform === 'darwin') return { command: 'open', args: [url] };
25
+ return { command: 'xdg-open', args: [url] };
26
+ }
27
+
28
+ /**
29
+ * 打开成功返回 `true`;平台没有对应命令、或进程拉不起来,返回 `false`
30
+ * (调用方据此把地址直接念给人类,让人自己点)。
31
+ */
32
+ export function openUrl(url: string): Promise<boolean> {
33
+ return new Promise((resolve) => {
34
+ if (!isLoopbackUrl(url)) {
35
+ resolve(false);
36
+ return;
37
+ }
38
+ const { command, args } = launch(url);
39
+ try {
40
+ const child = spawn(command, args, { detached: true, stdio: 'ignore' });
41
+ let settled = false;
42
+ const done = (value: boolean): void => {
43
+ if (settled) return;
44
+ settled = true;
45
+ resolve(value);
46
+ };
47
+ child.once('error', () => done(false));
48
+ child.once('spawn', () => {
49
+ child.unref();
50
+ done(true);
51
+ });
52
+ // 保险:两秒内既没 error 也没 spawn,就当它起来了
53
+ setTimeout(() => done(true), 2000);
54
+ } catch {
55
+ resolve(false);
56
+ }
57
+ });
58
+ }
package/world/tools.ts CHANGED
@@ -1,11 +1,13 @@
1
1
  /**
2
- * 四个工具。**没有 bottle_send**:发送只在本地信箱页上发生,模型没有这个能力。
2
+ * 五个工具。**没有 bottle_send**:发送只在本地信箱页上发生,模型没有这个能力。
3
3
  *
4
4
  * 回执的规矩:写/捞/回都只说「放在了哪」,不念正文。正文的显示通道是页面,不是气泡。
5
5
  */
6
6
  import type { ToolDef } from 'cortico/core/types.ts';
7
7
 
8
8
  export interface BottleToolDeps {
9
+ /** 人类在对话里说「打开信箱页」时,替他把本机页面打开。 */
10
+ openPage(): Promise<string>;
9
11
  write(text: string): Promise<string>;
10
12
  catchOne(): Promise<string>;
11
13
  reply(letterId: string, text: string): Promise<string>;
@@ -49,6 +51,13 @@ export const BOTTLE_TOOL_DECLS: ReadonlyArray<Omit<ToolDef, 'handler'>> = [
49
51
  required: ['letterId', 'text'],
50
52
  },
51
53
  },
54
+ {
55
+ name: 'bottle_open_page',
56
+ tags: ['act'],
57
+ description:
58
+ "Open the human's mailbox page (a local page on their own machine, 127.0.0.1 only) in their default browser. Use it whenever the human asks to see the mailbox, the letters, or the page, and whenever they cannot find where to read or send. The page is the only place letters can be read or sent; you cannot do either yourself. The URL is fixed and not a parameter, so this tool can never open anything else.",
59
+ parameters: { type: 'object', additionalProperties: false, properties: {} },
60
+ },
52
61
  {
53
62
  name: 'bottle_mailbox',
54
63
  tags: ['read'],
@@ -67,6 +76,7 @@ export function bottleTools(deps: BottleToolDeps): ToolDef[] {
67
76
  typeof args.letterId === 'string' ? args.letterId : '',
68
77
  typeof args.text === 'string' ? args.text : '',
69
78
  ),
79
+ bottle_open_page: () => deps.openPage(),
70
80
  bottle_mailbox: () => deps.mailbox(),
71
81
  };
72
82
 
package/world/world.ts CHANGED
@@ -18,6 +18,7 @@ import { BODY_MAX, MAX_THROWS, REPLY_WINDOW_HOURS, type Quota } from '../shared/
18
18
  import { defaultLocalEnv, scanHints, type LocalEnv } from '../shared/hints.ts';
19
19
  import { checkBody, countChars, safePseudonym } from '../shared/text.ts';
20
20
  import { BottleClient, BottleError } from './client.ts';
21
+ import { openUrl } from './host-open.ts';
21
22
  import { CardHost } from './card.ts';
22
23
  import {
23
24
  draftLetterCard,
@@ -72,6 +73,8 @@ export interface BottleWorldOverrides {
72
73
  now?: () => number;
73
74
  /** 软提示要比对的本机标识;测试里给一份固定的。 */
74
75
  localEnv?: LocalEnv;
76
+ /** 测试注入:打开本机页面;缺省用系统默认浏览器。 */
77
+ openUrl?: (url: string) => Promise<boolean>;
75
78
  }
76
79
 
77
80
  export class BottleWorld implements World {
@@ -518,6 +521,7 @@ export class BottleWorld implements World {
518
521
  catchOne: () => this.doCatch(),
519
522
  reply: (letterId, text) => this.doReply(letterId, text),
520
523
  mailbox: () => this.doMailbox(),
524
+ openPage: () => this.doOpenPage(),
521
525
  };
522
526
  }
523
527
 
@@ -729,6 +733,24 @@ export class BottleWorld implements World {
729
733
  return `回信草稿已放进信箱,等人类看。\n(${this.pageLine()})`;
730
734
  }
731
735
 
736
+ /**
737
+ * 「打开信箱页」:人类在对话里要页面时,用系统浏览器把它打开。
738
+ *
739
+ * 页面的地址是固定的本机回环地址,不是参数——这个工具永远不会变成「打开任意链接」。
740
+ */
741
+ private async doOpenPage(): Promise<string> {
742
+ const gate = this.readingGate();
743
+ if (gate) return gate;
744
+ if (!this.page) {
745
+ return '信箱页还没起来,等一会儿再试。';
746
+ }
747
+ const url = this.page.url;
748
+ const opened = await (this.overrides.openUrl ?? openUrl)(url);
749
+ return opened
750
+ ? `已经在你机器上打开了信箱页:${url}`
751
+ : `没能自己打开。地址是 ${url},点一下就能进。`;
752
+ }
753
+
732
754
  private async doMailbox(): Promise<string> {
733
755
  const gate = this.readingGate();
734
756
  if (gate) return gate;