dsh-tool-vision 0.7.0 → 0.7.1

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.
@@ -1,57 +1,57 @@
1
- // dsh-tool-vision — x-opencode-session request header helper.
2
- //
3
- // OpenCode Go (and other OpenAI-compatible gateways) ask clients to tag every
4
- // request with a stable per-conversation identifier so they can attribute and
5
- // optimize the service. Requests without the header started erroring after
6
- // 2026-09-06.
7
- //
8
- // This module centralizes how the plugin picks that identifier:
9
- //
10
- // 1. A real dsh session id, when the tool call runs inside one
11
- // (`exec.agent?.session?.id`, or the same id on the exec context).
12
- // This is the ideal "one stable ID per conversation".
13
- // 2. Otherwise the configured `sessionId` (a fixed override the user can
14
- // set in the settings panel, useful for background/standalone callers).
15
- // 3. Otherwise a per-process random id, generated once. Stable for the
16
- // lifetime of the plugin process, so every untagged background call
17
- // from this instance still shares one stable id.
18
-
19
- import { randomUUID } from 'node:crypto'
20
-
21
- export const DEFAULT_SESSION_HEADER = 'x-opencode-session'
22
-
23
- let fallbackId = ''
24
- function getFallbackId() {
25
- if (!fallbackId) fallbackId = `dsh-vision-${randomUUID()}`
26
- return fallbackId
27
- }
28
-
29
- /**
30
- * Pick a stable per-conversation id for the outgoing vision request.
31
- *
32
- * @param {object} cfg live tool-vision config (may carry `sessionId`).
33
- * @param {object|undefined} exec tool execution context, when available.
34
- * @returns {string|undefined} the id to send, or undefined if header disabled.
35
- */
36
- export function resolveSessionId(cfg, exec) {
37
- if (cfg?.sendSessionHeader === false) return undefined
38
- // Prefer an explicit, stable configured id when the user set one.
39
- if (cfg?.sessionId) return String(cfg.sessionId)
40
- // Real conversation id from the tool execution context, when present.
41
- const agentSessionId =
42
- exec?.agent?.session?.id ??
43
- exec?.session?.id ??
44
- exec?.sessionId ??
45
- exec?.agent?.session?.header?.sessionId
46
- if (agentSessionId) return String(agentSessionId)
47
- // Background / untagged call: one stable id per plugin process.
48
- return getFallbackId()
49
- }
50
-
51
- /** Build the header object to merge into a vision request's headers. */
52
- export function sessionHeaders(cfg, exec) {
53
- const id = resolveSessionId(cfg, exec)
54
- if (!id) return {}
55
- const headerName = cfg?.sessionHeaderName || DEFAULT_SESSION_HEADER
56
- return { [headerName]: id }
57
- }
1
+ // dsh-tool-vision — x-opencode-session request header helper.
2
+ //
3
+ // OpenCode Go (and other OpenAI-compatible gateways) ask clients to tag every
4
+ // request with a stable per-conversation identifier so they can attribute and
5
+ // optimize the service. Requests without the header started erroring after
6
+ // 2026-09-06.
7
+ //
8
+ // This module centralizes how the plugin picks that identifier:
9
+ //
10
+ // 1. A real dsh session id, when the tool call runs inside one
11
+ // (`exec.agent?.session?.id`, or the same id on the exec context).
12
+ // This is the ideal "one stable ID per conversation".
13
+ // 2. Otherwise the configured `sessionId` (a fixed override the user can
14
+ // set in the settings panel, useful for background/standalone callers).
15
+ // 3. Otherwise a per-process random id, generated once. Stable for the
16
+ // lifetime of the plugin process, so every untagged background call
17
+ // from this instance still shares one stable id.
18
+
19
+ import { randomUUID } from 'node:crypto'
20
+
21
+ export const DEFAULT_SESSION_HEADER = 'x-opencode-session'
22
+
23
+ let fallbackId = ''
24
+ function getFallbackId() {
25
+ if (!fallbackId) fallbackId = `dsh-vision-${randomUUID()}`
26
+ return fallbackId
27
+ }
28
+
29
+ /**
30
+ * Pick a stable per-conversation id for the outgoing vision request.
31
+ *
32
+ * @param {object} cfg live tool-vision config (may carry `sessionId`).
33
+ * @param {object|undefined} exec tool execution context, when available.
34
+ * @returns {string|undefined} the id to send, or undefined if header disabled.
35
+ */
36
+ export function resolveSessionId(cfg, exec) {
37
+ if (cfg?.sendSessionHeader === false) return undefined
38
+ // Prefer an explicit, stable configured id when the user set one.
39
+ if (cfg?.sessionId) return String(cfg.sessionId)
40
+ // Real conversation id from the tool execution context, when present.
41
+ const agentSessionId =
42
+ exec?.agent?.session?.id ??
43
+ exec?.session?.id ??
44
+ exec?.sessionId ??
45
+ exec?.agent?.session?.header?.sessionId
46
+ if (agentSessionId) return String(agentSessionId)
47
+ // Background / untagged call: one stable id per plugin process.
48
+ return getFallbackId()
49
+ }
50
+
51
+ /** Build the header object to merge into a vision request's headers. */
52
+ export function sessionHeaders(cfg, exec) {
53
+ const id = resolveSessionId(cfg, exec)
54
+ if (!id) return {}
55
+ const headerName = cfg?.sessionHeaderName || DEFAULT_SESSION_HEADER
56
+ return { [headerName]: id }
57
+ }