@linxin666/dsh-remote-bridge 0.4.16 → 0.4.17
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/README.i18n.yaml +2 -2
- package/README.md +9 -4
- package/README.zh.md +9 -4
- package/lib/client.js.map +1 -1
- package/lib/index.js +910 -350
- package/lib/types/core/attachment-paths.d.ts +89 -0
- package/lib/types/core/connection.d.ts +8 -0
- package/lib/types/core/protocol.d.ts +39 -1
- package/lib/types/core/wire.d.ts +8 -0
- package/package.json +14 -14
- package/src/core/attachment-paths.ts +288 -0
- package/src/core/connection.ts +159 -1
- package/src/core/protocol.ts +41 -1
- package/src/core/wire.ts +9 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The image paths one session's own durable transcript whitelists, and the file
|
|
3
|
+
* decisions `session.attachment.path` takes on the one path it is handed.
|
|
4
|
+
*
|
|
5
|
+
* This module IS that method's authorization. The bridge serves bytes only for a
|
|
6
|
+
* path the session itself named, so the whitelist is computed here from that
|
|
7
|
+
* session's store and from nothing else: never a path a client chose, never a
|
|
8
|
+
* directory listing, never a second session.
|
|
9
|
+
*
|
|
10
|
+
* Two sources and no others:
|
|
11
|
+
* - an offload placeholder in a message body, `<path>…</path>` immediately
|
|
12
|
+
* followed by `<type>image</type>` — the envelope the harness writes when
|
|
13
|
+
* it keeps image bytes out of a durable message, and the one a phone already
|
|
14
|
+
* reads with `OffloadedImages.segments(of:)`;
|
|
15
|
+
* - the image reference of a durable tool result — `meta.path`, what
|
|
16
|
+
* `read_image` publishes through its `output.presentationMeta`, or a
|
|
17
|
+
* text block that is itself a JSON image reference, which is how a
|
|
18
|
+
* `run_code` program's own return value is persisted — and what a client
|
|
19
|
+
* draws as `ToolBlock.image(ImageRef(path:))`.
|
|
20
|
+
*
|
|
21
|
+
* A path either source names is kept only when it is a host absolute path: the
|
|
22
|
+
* request side admits nothing else, so a name that could never be asked for —
|
|
23
|
+
* a plugin's own namespace, a relative path — is not a whitelist entry.
|
|
24
|
+
*
|
|
25
|
+
* Records are the ones `session/page` answers and `turnsOfRecords`
|
|
26
|
+
* projects: `{event: {type, seq, time, data}}`.
|
|
27
|
+
* @module dsh-remote-bridge/core/attachment-paths
|
|
28
|
+
*/
|
|
29
|
+
/** One image a session's transcript names: the reference a client already drew. */
|
|
30
|
+
export interface AttachmentFacts {
|
|
31
|
+
/** The absolute path, byte for byte as the transcript wrote it. */
|
|
32
|
+
readonly path: string;
|
|
33
|
+
readonly mediaType?: string;
|
|
34
|
+
readonly bytes?: number;
|
|
35
|
+
readonly width?: number;
|
|
36
|
+
readonly height?: number;
|
|
37
|
+
}
|
|
38
|
+
/** One whitelisted path's bytes, or the code that refused them. */
|
|
39
|
+
export type AttachmentRead = {
|
|
40
|
+
readonly ok: true;
|
|
41
|
+
readonly image: AttachmentFacts;
|
|
42
|
+
readonly data: string;
|
|
43
|
+
} | {
|
|
44
|
+
readonly ok: false;
|
|
45
|
+
readonly code: string;
|
|
46
|
+
readonly msg: string;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* The oldest entry seq one store page carries.
|
|
50
|
+
*
|
|
51
|
+
* A page read walks backwards by this number, so the walk has to read it off the
|
|
52
|
+
* page itself: the store's own `hasMore` says that older entries exist,
|
|
53
|
+
* never where they end.
|
|
54
|
+
* @param records - one `session/page` answer's records.
|
|
55
|
+
* @returns the smallest `event.seq` they carry, or undefined when they carry none.
|
|
56
|
+
*/
|
|
57
|
+
export declare function oldestSeqOf(records: readonly unknown[]): number | undefined;
|
|
58
|
+
/**
|
|
59
|
+
* Every image path one session's durable records whitelist.
|
|
60
|
+
* @param records - that session's own records, as `session/page` answered them.
|
|
61
|
+
* @returns one entry per path; a second sighting never erases a stated fact.
|
|
62
|
+
*/
|
|
63
|
+
export declare function attachmentPathsOf(records: readonly unknown[]): Map<string, AttachmentFacts>;
|
|
64
|
+
/**
|
|
65
|
+
* The media type one head declares, or undefined when it is none of the four.
|
|
66
|
+
*
|
|
67
|
+
* The signatures are the harness's own four — PNG, JPEG, GIF87a/89a and
|
|
68
|
+
* RIFF-WebP — because the bytes are what a client will decode.
|
|
69
|
+
* @param head - the file's first bytes, as read.
|
|
70
|
+
* @returns the media type, or undefined.
|
|
71
|
+
*/
|
|
72
|
+
export declare function mediaTypeOfSignature(head: Buffer): string | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* Read one whitelisted path, applying the three file decisions in order.
|
|
75
|
+
*
|
|
76
|
+
* The whitelist is the first decision and it is not this function's: a caller
|
|
77
|
+
* hands over a path it already matched byte for byte. What is left is whether the
|
|
78
|
+
* file at that path is still the one the session named — `realpath()` has to
|
|
79
|
+
* return the same string, so a symlink pointing anywhere else is refused rather
|
|
80
|
+
* than followed — whether it is a regular file at all, and whether its own first
|
|
81
|
+
* bytes are one of the four image signatures. The extension is never consulted: a
|
|
82
|
+
* `.png` that is not a PNG is refused, and a PNG named `.txt` is served.
|
|
83
|
+
*
|
|
84
|
+
* The answer's `image` is the reference the session's transcript named; this
|
|
85
|
+
* read measures nothing into it. The magic decides, and the bytes are what crosses.
|
|
86
|
+
* @param facts - the whitelist entry the request matched.
|
|
87
|
+
* @returns the bytes as base64, or the refusal the first failing decision states.
|
|
88
|
+
*/
|
|
89
|
+
export declare function readAttachment(facts: AttachmentFacts): Promise<AttachmentRead>;
|
|
@@ -67,6 +67,14 @@ export declare const DEEP_HISTORY_WINDOW: {
|
|
|
67
67
|
readonly minTurns: 2;
|
|
68
68
|
};
|
|
69
69
|
};
|
|
70
|
+
/**
|
|
71
|
+
* Highest number of sessions one connection keeps a computed whitelist for.
|
|
72
|
+
*
|
|
73
|
+
* Bounded because a long-lived connection must not grow with every session its
|
|
74
|
+
* client opens: four is a phone's own working set of session screens, and the
|
|
75
|
+
* least recently used one goes first.
|
|
76
|
+
*/
|
|
77
|
+
export declare const ATTACHMENT_WHITELIST_SESSIONS = 4;
|
|
70
78
|
/** Highest number of prompt receipts one connection remembers for idempotency. */
|
|
71
79
|
export declare const PROMPT_RECEIPT_LIMIT = 512;
|
|
72
80
|
/** Largest base64 text one prompt attachment may carry: 6 MiB (protocol §10.2). */
|
|
@@ -13,6 +13,7 @@ export declare const METHOD_SESSION_HISTORY_DEEP = "session.history.deep";
|
|
|
13
13
|
export declare const METHOD_SESSION_PROMPT = "session.prompt";
|
|
14
14
|
export declare const METHOD_SESSION_INTERRUPT = "session.interrupt";
|
|
15
15
|
export declare const METHOD_SESSION_ATTACHMENT = "session.attachment";
|
|
16
|
+
export declare const METHOD_SESSION_ATTACHMENT_PATH = "session.attachment.path";
|
|
16
17
|
export declare const METHOD_SESSION_PERMISSIONS = "session.permissions";
|
|
17
18
|
export declare const METHOD_SESSION_SET_PERMISSION = "session.setPermission";
|
|
18
19
|
export declare const METHOD_SESSION_MODELS = "session.models";
|
|
@@ -270,6 +271,19 @@ export declare const CAP_UI = "ui.v1";
|
|
|
270
271
|
* `ui.request` 说 `settled` 时结束),而不是按一次它信不过的应答清卡。
|
|
271
272
|
*/
|
|
272
273
|
export declare const CAP_UI_LIST = "ui.list.v1";
|
|
274
|
+
/**
|
|
275
|
+
* Capability a client declares in `hello`'s `cap` array to be told that this
|
|
276
|
+
* bridge can hand it the bytes of an image a session already offloaded.
|
|
277
|
+
*
|
|
278
|
+
* An announcement and nothing more, the same shape as
|
|
279
|
+
* {@link CAP_SESSION_DEEP_HISTORY}: `session.attachment.path` is open to every
|
|
280
|
+
* paired connection, because it is a pure read of one session's own durable
|
|
281
|
+
* transcript that costs no other client anything. Declaring the capability is how
|
|
282
|
+
* a client learns the method exists, and a client that does not declare it loses
|
|
283
|
+
* only that knowledge — it never sees a frame from this channel, because it never
|
|
284
|
+
* asks for one.
|
|
285
|
+
*/
|
|
286
|
+
export declare const CAP_SESSION_ATTACHMENT_PATH = "session.attachment.path.v1";
|
|
273
287
|
/**
|
|
274
288
|
* 宿主配了第二会合点时,在 `hello` 的 `capabilities` 里公告的能力名。
|
|
275
289
|
*
|
|
@@ -485,6 +499,23 @@ export declare const CODE_UI_LIST_UNAVAILABLE = "ui-list-unavailable";
|
|
|
485
499
|
* an unaddressable one, rather than reading it blind.
|
|
486
500
|
*/
|
|
487
501
|
export declare const CODE_SESSION_NOT_FOLLOWABLE = "session-not-followable";
|
|
502
|
+
/**
|
|
503
|
+
* 该路径不在这次会话自己的持久 transcript 里:白名单只有两处来源,客户端自选的路径
|
|
504
|
+
* 一律不在其中。`realpath()` 解析结果不再等于该字符串时也回这一码——那个路径已经
|
|
505
|
+
* 指向别处,就不再是这次会话点过的那个文件。
|
|
506
|
+
*/
|
|
507
|
+
export declare const CODE_PATH_NOT_IN_SESSION = "path-not-in-session";
|
|
508
|
+
/**
|
|
509
|
+
* 白名单里有这个路径,但它已经不是一个常规文件:文件被删了,或被换成了目录、设备或
|
|
510
|
+
* fifo。它与 {@link CODE_PATH_NOT_IN_SESSION} 是两个答案——「这次会话没点过它」与
|
|
511
|
+
* 「点过的那个文件不在了」。
|
|
512
|
+
*/
|
|
513
|
+
export declare const CODE_ATTACHMENT_NOT_FOUND = "not-found";
|
|
514
|
+
/**
|
|
515
|
+
* 文件确实在,但它的文件头魔数不是 png/jpeg/webp/gif 四种之一。扩展名不作数:`.png`
|
|
516
|
+
* 里装的不是 PNG 就回这一码,而一个叫 `.txt` 的 PNG 照常服务。
|
|
517
|
+
*/
|
|
518
|
+
export declare const CODE_NOT_AN_IMAGE = "not-an-image";
|
|
488
519
|
export declare const CODE_SNAPSHOT_UNAVAILABLE = "snapshot-unavailable";
|
|
489
520
|
/**
|
|
490
521
|
* This host composes no job registry, so it has no background jobs to publish.
|
|
@@ -525,6 +556,13 @@ export declare const PROTO = 1;
|
|
|
525
556
|
/**
|
|
526
557
|
* Minor version this package speaks.
|
|
527
558
|
*
|
|
559
|
+
* 22 当按路径取图落地(与协议 §18 的 `session.history.deep` 同例的一条读通道):
|
|
560
|
+
* 新增 {@link CAP_SESSION_ATTACHMENT_PATH} 一个能力名与
|
|
561
|
+
* {@link METHOD_SESSION_ATTACHMENT_PATH} 一个方法,客户端因此从线路上读到一种新的
|
|
562
|
+
* 应答体,所以 minor 抬一格。这个方法对每一条已配对连接都开放,能力名只公告:它决定
|
|
563
|
+
* 的是客户端要不要露出取图能力,不决定桥放不放行,理由与 {@link CAP_SESSION_DEEP_HISTORY}
|
|
564
|
+
* 逐字相同。`proto` 仍为 1,没有任何既有帧、既有方法或既有键改形状;不声明这个
|
|
565
|
+
* 名字的客户端一帧都不发,两边互不知情时线路逐字节不变。
|
|
528
566
|
* 21 当可交互回复(协议 §35)落地:新增 {@link CAP_UI} 与 {@link CAP_UI_LIST}
|
|
529
567
|
* 两个能力名、{@link TOPIC_UI_REQUEST} 一个主题、{@link METHOD_UI_EVENT} 与
|
|
530
568
|
* {@link METHOD_UI_LIST} 两个方法,宿主半区还多一个模型工具 `ui_present`。客户端
|
|
@@ -636,7 +674,7 @@ export declare const PROTO = 1;
|
|
|
636
674
|
* declares the capability that plugin requires. No addition changed the major
|
|
637
675
|
* version, a frozen field, or an existing method or topic name.
|
|
638
676
|
*/
|
|
639
|
-
export declare const MINOR =
|
|
677
|
+
export declare const MINOR = 22;
|
|
640
678
|
/**
|
|
641
679
|
* The capabilities this bridge itself serves, and the opening half of what
|
|
642
680
|
* `hello` advertises.
|
package/lib/types/core/wire.d.ts
CHANGED
|
@@ -74,6 +74,14 @@ export declare function sessionAddress(sessionId: string): SessionAddress;
|
|
|
74
74
|
export declare function subagentAddress(parentSessionId: string, childSessionId: string): SessionAddress;
|
|
75
75
|
/** The failure code the harness raises for a child addressed as an ordinary session. */
|
|
76
76
|
export declare const CODE_SUBAGENT_ADDRESS_REQUIRED = "session/agent-busy";
|
|
77
|
+
/**
|
|
78
|
+
* The failure the harness raises when a session address names a session this host
|
|
79
|
+
* does not have. `session/page` answers it, `session/projections` answers
|
|
80
|
+
* `null` for the same fact, and a caller that needs one name for "no such
|
|
81
|
+
* session" folds both into its own code rather than letting one fact cross as two
|
|
82
|
+
* different failures.
|
|
83
|
+
*/
|
|
84
|
+
export declare const CODE_HOST_SESSION_NOT_FOUND = "session/not-found";
|
|
77
85
|
/**
|
|
78
86
|
* One endpoint's declared argument names: the name of its single parameter, the
|
|
79
87
|
* empty declaration of a parameterless endpoint, or the exact names of an
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@linxin666/dsh-remote-bridge",
|
|
3
3
|
"description": "DSH remote bridge: the host half serves protocol v1 over one exact /ios/v1 WebSocket route — session roster, history baseline, prompting, live turn increments with tool activity, interrupt, and in-process agent approval — to a device paired by a one-shot bootstrap token and a P-256 challenge-response, with the token endpoint admitted only from this machine; the browser half adds one sidebar-foot panel that shows that same one-shot token as a QR symbol and draws an interactive card inside the tool row without answering it, and adds no wire surface",
|
|
4
|
-
"version": "0.4.
|
|
4
|
+
"version": "0.4.17",
|
|
5
5
|
"icon": "./icon.svg",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"engines": {
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"license": "Apache-2.0",
|
|
38
38
|
"dsh": {
|
|
39
39
|
"engines": {
|
|
40
|
-
"dsh": ">=0.2.2-rc.
|
|
40
|
+
"dsh": ">=0.2.2-rc.2"
|
|
41
41
|
},
|
|
42
42
|
"bundle": {
|
|
43
43
|
"patch": "./cordis.patch.yml"
|
|
@@ -57,23 +57,23 @@
|
|
|
57
57
|
"yaml": "^2.8.2"
|
|
58
58
|
},
|
|
59
59
|
"peerDependencies": {
|
|
60
|
-
"@deepseek-ai/dsh": ">=0.2.2-rc.
|
|
60
|
+
"@deepseek-ai/dsh": ">=0.2.2-rc.2",
|
|
61
61
|
"react": "^18.2.0",
|
|
62
62
|
"react-dom": "^18.2.0"
|
|
63
63
|
},
|
|
64
64
|
"devDependencies": {
|
|
65
65
|
"@deepseek-ai/cordis": "^4.1.0",
|
|
66
|
-
"@deepseek-ai/dsh-api-gateway": "^0.2.2-rc.
|
|
67
|
-
"@deepseek-ai/dsh-client-locale": "^0.2.2-rc.
|
|
68
|
-
"@deepseek-ai/dsh-client-ui-primitives": "^0.2.2-rc.
|
|
69
|
-
"@deepseek-ai/dsh-client-ui-renderer": "^0.2.2-rc.
|
|
70
|
-
"@deepseek-ai/dsh-client-ui-sidebar": "^0.2.2-rc.
|
|
71
|
-
"@deepseek-ai/dsh-client-ui-slots": "^0.2.2-rc.
|
|
72
|
-
"@deepseek-ai/dsh-host-webserver": "^0.2.2-rc.
|
|
73
|
-
"@deepseek-ai/dsh-session": "^0.2.2-rc.
|
|
74
|
-
"@deepseek-ai/dsh-tools": "^0.2.2-rc.
|
|
75
|
-
"@deepseek-ai/dsh-typert-protocol": "^0.2.2-rc.
|
|
76
|
-
"@deepseek-ai/dsh-user-approval": "^0.2.2-rc.
|
|
66
|
+
"@deepseek-ai/dsh-api-gateway": "^0.2.2-rc.2",
|
|
67
|
+
"@deepseek-ai/dsh-client-locale": "^0.2.2-rc.2",
|
|
68
|
+
"@deepseek-ai/dsh-client-ui-primitives": "^0.2.2-rc.2",
|
|
69
|
+
"@deepseek-ai/dsh-client-ui-renderer": "^0.2.2-rc.2",
|
|
70
|
+
"@deepseek-ai/dsh-client-ui-sidebar": "^0.2.2-rc.2",
|
|
71
|
+
"@deepseek-ai/dsh-client-ui-slots": "^0.2.2-rc.2",
|
|
72
|
+
"@deepseek-ai/dsh-host-webserver": "^0.2.2-rc.2",
|
|
73
|
+
"@deepseek-ai/dsh-session": "^0.2.2-rc.2",
|
|
74
|
+
"@deepseek-ai/dsh-tools": "^0.2.2-rc.2",
|
|
75
|
+
"@deepseek-ai/dsh-typert-protocol": "^0.2.2-rc.2",
|
|
76
|
+
"@deepseek-ai/dsh-user-approval": "^0.2.2-rc.2",
|
|
77
77
|
"@testing-library/dom": "^10.4.1",
|
|
78
78
|
"@testing-library/react": "^16.3.2",
|
|
79
79
|
"@types/node": "^22.20.0",
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The image paths one session's own durable transcript whitelists, and the file
|
|
3
|
+
* decisions `session.attachment.path` takes on the one path it is handed.
|
|
4
|
+
*
|
|
5
|
+
* This module IS that method's authorization. The bridge serves bytes only for a
|
|
6
|
+
* path the session itself named, so the whitelist is computed here from that
|
|
7
|
+
* session's store and from nothing else: never a path a client chose, never a
|
|
8
|
+
* directory listing, never a second session.
|
|
9
|
+
*
|
|
10
|
+
* Two sources and no others:
|
|
11
|
+
* - an offload placeholder in a message body, `<path>…</path>` immediately
|
|
12
|
+
* followed by `<type>image</type>` — the envelope the harness writes when
|
|
13
|
+
* it keeps image bytes out of a durable message, and the one a phone already
|
|
14
|
+
* reads with `OffloadedImages.segments(of:)`;
|
|
15
|
+
* - the image reference of a durable tool result — `meta.path`, what
|
|
16
|
+
* `read_image` publishes through its `output.presentationMeta`, or a
|
|
17
|
+
* text block that is itself a JSON image reference, which is how a
|
|
18
|
+
* `run_code` program's own return value is persisted — and what a client
|
|
19
|
+
* draws as `ToolBlock.image(ImageRef(path:))`.
|
|
20
|
+
*
|
|
21
|
+
* A path either source names is kept only when it is a host absolute path: the
|
|
22
|
+
* request side admits nothing else, so a name that could never be asked for —
|
|
23
|
+
* a plugin's own namespace, a relative path — is not a whitelist entry.
|
|
24
|
+
*
|
|
25
|
+
* Records are the ones `session/page` answers and `turnsOfRecords`
|
|
26
|
+
* projects: `{event: {type, seq, time, data}}`.
|
|
27
|
+
* @module dsh-remote-bridge/core/attachment-paths
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import { isAbsolute } from 'node:path'
|
|
31
|
+
import { readFile, realpath, stat } from 'node:fs/promises'
|
|
32
|
+
import { FRAME_TOO_LARGE, MAX_FRAME_BYTES } from './envelope.ts'
|
|
33
|
+
import { CODE_ATTACHMENT_NOT_FOUND, CODE_NOT_AN_IMAGE, CODE_PATH_NOT_IN_SESSION } from './protocol.ts'
|
|
34
|
+
|
|
35
|
+
/** One image a session's transcript names: the reference a client already drew. */
|
|
36
|
+
export interface AttachmentFacts {
|
|
37
|
+
/** The absolute path, byte for byte as the transcript wrote it. */
|
|
38
|
+
readonly path: string
|
|
39
|
+
readonly mediaType?: string
|
|
40
|
+
readonly bytes?: number
|
|
41
|
+
readonly width?: number
|
|
42
|
+
readonly height?: number
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* `<path>…</path>` immediately followed by `<type>image</type>`, plus
|
|
47
|
+
* the facts the `<content>` body beside them states when it states any.
|
|
48
|
+
*/
|
|
49
|
+
const OFFLOAD_PLACEHOLDER = /<path>([^<]*)<\/path>\s*<type>image<\/type>\s*(?:<content>\s*([^<]*?)\s*<\/content>)?/gu
|
|
50
|
+
|
|
51
|
+
/** One envelope body: `image/jpeg image, 1600x1000 px, 93031 bytes`. */
|
|
52
|
+
const ENVELOPE_FACTS = /^(\S+)\s+image,\s*(\d+)x(\d+)\s+px,\s*(\d+)\s+bytes/u
|
|
53
|
+
|
|
54
|
+
/** The four heads a durable image may declare, byte for byte. */
|
|
55
|
+
const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
|
|
56
|
+
const JPEG_SIGNATURE = Buffer.from([0xff, 0xd8, 0xff])
|
|
57
|
+
const GIF_SIGNATURES = [Buffer.from('GIF87a', 'ascii'), Buffer.from('GIF89a', 'ascii')]
|
|
58
|
+
const RIFF_SIGNATURE = Buffer.from('RIFF', 'ascii')
|
|
59
|
+
const WEBP_SIGNATURE = Buffer.from('WEBP', 'ascii')
|
|
60
|
+
|
|
61
|
+
/** One whitelisted path's bytes, or the code that refused them. */
|
|
62
|
+
export type AttachmentRead =
|
|
63
|
+
| { readonly ok: true; readonly image: AttachmentFacts; readonly data: string }
|
|
64
|
+
| { readonly ok: false; readonly code: string; readonly msg: string }
|
|
65
|
+
|
|
66
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
67
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** One durable record's `type` and `data`, or undefined when it is not one. */
|
|
71
|
+
function entryOf(record: unknown): { readonly type: string; readonly data: unknown } | undefined {
|
|
72
|
+
const event = isRecord(record) ? record.event : undefined
|
|
73
|
+
if (!isRecord(event) || typeof event.type !== 'string') return undefined
|
|
74
|
+
return { type: event.type, data: event.data }
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* The oldest entry seq one store page carries.
|
|
79
|
+
*
|
|
80
|
+
* A page read walks backwards by this number, so the walk has to read it off the
|
|
81
|
+
* page itself: the store's own `hasMore` says that older entries exist,
|
|
82
|
+
* never where they end.
|
|
83
|
+
* @param records - one `session/page` answer's records.
|
|
84
|
+
* @returns the smallest `event.seq` they carry, or undefined when they carry none.
|
|
85
|
+
*/
|
|
86
|
+
export function oldestSeqOf(records: readonly unknown[]): number | undefined {
|
|
87
|
+
let oldest: number | undefined
|
|
88
|
+
for (const record of records) {
|
|
89
|
+
const seq = isRecord(record) && isRecord(record.event) ? record.event.seq : undefined
|
|
90
|
+
if (typeof seq !== 'number' || !Number.isSafeInteger(seq)) continue
|
|
91
|
+
if (oldest === undefined || seq < oldest) oldest = seq
|
|
92
|
+
}
|
|
93
|
+
return oldest
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Every image path one session's durable records whitelist.
|
|
98
|
+
* @param records - that session's own records, as `session/page` answered them.
|
|
99
|
+
* @returns one entry per path; a second sighting never erases a stated fact.
|
|
100
|
+
*/
|
|
101
|
+
export function attachmentPathsOf(records: readonly unknown[]): Map<string, AttachmentFacts> {
|
|
102
|
+
const found = new Map<string, AttachmentFacts>()
|
|
103
|
+
for (const record of records) {
|
|
104
|
+
const entry = entryOf(record)
|
|
105
|
+
if (entry === undefined) continue
|
|
106
|
+
// A message body. The harness writes the envelope into the INPUT message that
|
|
107
|
+
// carried the image, so only `user/message` is read: an assistant message
|
|
108
|
+
// is model output, and reading one would whitelist a path the model merely wrote.
|
|
109
|
+
if (entry.type === 'user/message') {
|
|
110
|
+
for (const facts of placeholderFacts(entry.data)) remember(found, facts)
|
|
111
|
+
continue
|
|
112
|
+
}
|
|
113
|
+
// A durable tool result. `tool/ptc-dispatch` is the same record for a
|
|
114
|
+
// tool a `run_code` program called, so both shapes of one result are read.
|
|
115
|
+
if (entry.type !== 'tool/result' && entry.type !== 'tool/ptc-dispatch') continue
|
|
116
|
+
const path = toolResultImagePath(entry.data)
|
|
117
|
+
if (path !== undefined) remember(found, { path })
|
|
118
|
+
}
|
|
119
|
+
return found
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** The facts every offload placeholder of one message body states, in body order. */
|
|
123
|
+
function placeholderFacts(data: unknown): AttachmentFacts[] {
|
|
124
|
+
if (!isRecord(data) || !Array.isArray(data.content)) return []
|
|
125
|
+
const found: AttachmentFacts[] = []
|
|
126
|
+
for (const block of data.content) {
|
|
127
|
+
if (!isRecord(block) || block.type !== 'text' || typeof block.text !== 'string') continue
|
|
128
|
+
for (const match of block.text.matchAll(OFFLOAD_PLACEHOLDER)) {
|
|
129
|
+
const path = match[1] ?? ''
|
|
130
|
+
if (path === '') continue
|
|
131
|
+
found.push({ path, ...envelopeFacts(match[2]) })
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return found
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** The facts one `<content>` body states, or nothing when it states none. */
|
|
138
|
+
function envelopeFacts(body: string | undefined): Omit<AttachmentFacts, 'path'> {
|
|
139
|
+
const stated = body === undefined ? null : ENVELOPE_FACTS.exec(body.trim())
|
|
140
|
+
if (stated === null) return {}
|
|
141
|
+
return { mediaType: stated[1] ?? '', width: Number(stated[2]), height: Number(stated[3]), bytes: Number(stated[4]) }
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* The image reference one durable tool result states, or undefined.
|
|
146
|
+
*
|
|
147
|
+
* Two readings, the first that hits wins:
|
|
148
|
+
* - `meta.path`, what `read_image` publishes through its
|
|
149
|
+
* `output.presentationMeta` — kept only when the same result carried the
|
|
150
|
+
* image block it names, because `read` publishes a `meta.path` of its
|
|
151
|
+
* own and a session must not whitelist every file it ever read as text;
|
|
152
|
+
* - a text block that IS a JSON image reference,
|
|
153
|
+
* `{path, image:{attachmentId, mediaType, bytes, width, height, name}}` —
|
|
154
|
+
* the shape a `run_code` program's own return value is persisted as.
|
|
155
|
+
*
|
|
156
|
+
* The JSON reading keeps its gate: the parsed object has to carry an `image`
|
|
157
|
+
* object whose `mediaType` starts with `image/`. That gate is exactly what
|
|
158
|
+
* separates a result that published an image from one that merely named a file —
|
|
159
|
+
* measured over this host's whole store, 83 tool results carry a bare `path`
|
|
160
|
+
* in the same JSON shape against 10 image references — and a path a tool named is
|
|
161
|
+
* not an image this session offloaded.
|
|
162
|
+
*
|
|
163
|
+
* What is read is the reference the TOOL published, never the call's own argument
|
|
164
|
+
* and never prose a model wrote: an argument is what the model asked for, and a
|
|
165
|
+
* model's own words are not a record of anything.
|
|
166
|
+
*/
|
|
167
|
+
function toolResultImagePath(data: unknown): string | undefined {
|
|
168
|
+
if (!isRecord(data)) return undefined
|
|
169
|
+
const content = isRecord(data.message) ? data.message.content : data.content
|
|
170
|
+
return publishedImagePath(data.meta, content) ?? jsonImageReference(content)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** `meta.path`, but only when the same result carried the image block it names. */
|
|
174
|
+
function publishedImagePath(meta: unknown, content: unknown): string | undefined {
|
|
175
|
+
const path = isRecord(meta) ? meta.path : undefined
|
|
176
|
+
if (typeof path !== 'string' || path === '') return undefined
|
|
177
|
+
if (!Array.isArray(content) || !content.some(block => isRecord(block) && block.type === 'image')) return undefined
|
|
178
|
+
return path
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** The path one text block states as a JSON image reference, or undefined. */
|
|
182
|
+
function jsonImageReference(content: unknown): string | undefined {
|
|
183
|
+
if (!Array.isArray(content)) return undefined
|
|
184
|
+
for (const block of content) {
|
|
185
|
+
if (!isRecord(block) || block.type !== 'text' || typeof block.text !== 'string') continue
|
|
186
|
+
let parsed: unknown
|
|
187
|
+
try {
|
|
188
|
+
parsed = JSON.parse(block.text)
|
|
189
|
+
} catch {
|
|
190
|
+
continue
|
|
191
|
+
}
|
|
192
|
+
if (!isRecord(parsed)) continue
|
|
193
|
+
const path = parsed.path
|
|
194
|
+
const image = parsed.image
|
|
195
|
+
if (typeof path !== 'string' || path === '') continue
|
|
196
|
+
if (!isRecord(image) || typeof image.mediaType !== 'string' || !image.mediaType.startsWith('image/')) continue
|
|
197
|
+
return path
|
|
198
|
+
}
|
|
199
|
+
return undefined
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Remember one sighting, or drop it.
|
|
204
|
+
*
|
|
205
|
+
* A path that is not a host absolute path is not a whitelist entry at all. A
|
|
206
|
+
* request has to name an absolute path, so `browser-view:screenshot/<tab>.png`
|
|
207
|
+
* — the plugin-namespace name `browser_screenshot` writes into its envelope,
|
|
208
|
+
* with no file behind it — could never be asked for, and a list of names no
|
|
209
|
+
* request can match is not a whitelist. What an entry keeps is the reading that
|
|
210
|
+
* states more: a later sighting never erases a stated fact.
|
|
211
|
+
*/
|
|
212
|
+
function remember(found: Map<string, AttachmentFacts>, facts: AttachmentFacts): void {
|
|
213
|
+
if (!isAbsolute(facts.path)) return
|
|
214
|
+
const previous = found.get(facts.path)
|
|
215
|
+
if (previous === undefined || (facts.mediaType !== undefined && previous.mediaType === undefined)) {
|
|
216
|
+
found.set(facts.path, facts)
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* The media type one head declares, or undefined when it is none of the four.
|
|
222
|
+
*
|
|
223
|
+
* The signatures are the harness's own four — PNG, JPEG, GIF87a/89a and
|
|
224
|
+
* RIFF-WebP — because the bytes are what a client will decode.
|
|
225
|
+
* @param head - the file's first bytes, as read.
|
|
226
|
+
* @returns the media type, or undefined.
|
|
227
|
+
*/
|
|
228
|
+
export function mediaTypeOfSignature(head: Buffer): string | undefined {
|
|
229
|
+
if (head.subarray(0, PNG_SIGNATURE.length).equals(PNG_SIGNATURE)) return 'image/png'
|
|
230
|
+
if (head.subarray(0, JPEG_SIGNATURE.length).equals(JPEG_SIGNATURE)) return 'image/jpeg'
|
|
231
|
+
if (GIF_SIGNATURES.some(signature => head.subarray(0, signature.length).equals(signature))) return 'image/gif'
|
|
232
|
+
if (head.subarray(0, RIFF_SIGNATURE.length).equals(RIFF_SIGNATURE) && head.subarray(8, 12).equals(WEBP_SIGNATURE)) return 'image/webp'
|
|
233
|
+
return undefined
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Read one whitelisted path, applying the three file decisions in order.
|
|
238
|
+
*
|
|
239
|
+
* The whitelist is the first decision and it is not this function's: a caller
|
|
240
|
+
* hands over a path it already matched byte for byte. What is left is whether the
|
|
241
|
+
* file at that path is still the one the session named — `realpath()` has to
|
|
242
|
+
* return the same string, so a symlink pointing anywhere else is refused rather
|
|
243
|
+
* than followed — whether it is a regular file at all, and whether its own first
|
|
244
|
+
* bytes are one of the four image signatures. The extension is never consulted: a
|
|
245
|
+
* `.png` that is not a PNG is refused, and a PNG named `.txt` is served.
|
|
246
|
+
*
|
|
247
|
+
* The answer's `image` is the reference the session's transcript named; this
|
|
248
|
+
* read measures nothing into it. The magic decides, and the bytes are what crosses.
|
|
249
|
+
* @param facts - the whitelist entry the request matched.
|
|
250
|
+
* @returns the bytes as base64, or the refusal the first failing decision states.
|
|
251
|
+
*/
|
|
252
|
+
export async function readAttachment(facts: AttachmentFacts): Promise<AttachmentRead> {
|
|
253
|
+
const path = facts.path
|
|
254
|
+
let resolved: string
|
|
255
|
+
try {
|
|
256
|
+
resolved = await realpath(path)
|
|
257
|
+
} catch {
|
|
258
|
+
// Gone, unreadable, or a parent that is not a directory: either way there is
|
|
259
|
+
// no file at this path, which is the one thing `not-found` states.
|
|
260
|
+
return { ok: false, code: CODE_ATTACHMENT_NOT_FOUND, msg: 'no file at ' + path }
|
|
261
|
+
}
|
|
262
|
+
if (resolved !== path) {
|
|
263
|
+
return { ok: false, code: CODE_PATH_NOT_IN_SESSION, msg: path + ' resolves to ' + resolved + ', which this session never named' }
|
|
264
|
+
}
|
|
265
|
+
let size: number
|
|
266
|
+
try {
|
|
267
|
+
const info = await stat(path)
|
|
268
|
+
if (!info.isFile()) return { ok: false, code: CODE_ATTACHMENT_NOT_FOUND, msg: path + ' is not a regular file' }
|
|
269
|
+
size = info.size
|
|
270
|
+
} catch {
|
|
271
|
+
return { ok: false, code: CODE_ATTACHMENT_NOT_FOUND, msg: 'no file at ' + path }
|
|
272
|
+
}
|
|
273
|
+
// Base64 inflates by a third, so a file at or past the frame cap cannot fit one
|
|
274
|
+
// whatever the envelope costs: refused before it is read into memory.
|
|
275
|
+
if (size >= MAX_FRAME_BYTES) {
|
|
276
|
+
return { ok: false, code: FRAME_TOO_LARGE, msg: path + ' is ' + String(size) + ' bytes, past the ' + String(MAX_FRAME_BYTES) + ' byte frame' }
|
|
277
|
+
}
|
|
278
|
+
let bytes: Buffer
|
|
279
|
+
try {
|
|
280
|
+
bytes = await readFile(path)
|
|
281
|
+
} catch {
|
|
282
|
+
return { ok: false, code: CODE_ATTACHMENT_NOT_FOUND, msg: 'no file at ' + path }
|
|
283
|
+
}
|
|
284
|
+
if (mediaTypeOfSignature(bytes) === undefined) {
|
|
285
|
+
return { ok: false, code: CODE_NOT_AN_IMAGE, msg: path + ' is not a PNG, JPEG, WebP or GIF' }
|
|
286
|
+
}
|
|
287
|
+
return { ok: true, image: facts, data: bytes.toString('base64') }
|
|
288
|
+
}
|