opencode-subagent-tools 0.1.0 → 0.2.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.
- package/CHANGELOG.md +36 -0
- package/README.md +23 -18
- package/SECURITY.md +1 -1
- package/SKILL.md +60 -20
- package/docs/development.md +14 -10
- package/docs/installation.md +1 -7
- package/docs/limitations.md +45 -9
- package/docs/reference.md +83 -32
- package/package.json +5 -5
- package/src/access.ts +79 -79
- package/src/background.ts +602 -0
- package/src/config.ts +47 -47
- package/src/cursor.ts +41 -39
- package/src/discovery.ts +77 -69
- package/src/errors.ts +7 -7
- package/src/events.ts +141 -95
- package/src/format.ts +20 -20
- package/src/index.ts +49 -36
- package/src/interrupt.ts +4 -4
- package/src/live-state.ts +64 -41
- package/src/messages.ts +134 -84
- package/src/permissions.ts +18 -18
- package/src/session-row.ts +82 -21
- package/src/session.ts +69 -69
- package/src/skill.ts +24 -24
- package/src/status.ts +64 -64
- package/src/text.ts +16 -16
- package/src/tokens.ts +51 -51
- package/src/tools/cancel-descendants.ts +303 -170
- package/src/tools/cancel.ts +139 -73
- package/src/tools/index.ts +32 -30
- package/src/tools/input.ts +46 -44
- package/src/tools/list.ts +281 -175
- package/src/tools/names.ts +2 -2
- package/src/tools/output.ts +51 -51
- package/src/tools/send.ts +130 -96
- package/src/tools/usage.ts +181 -143
package/src/config.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { clipText } from "./text"
|
|
1
|
+
import { clipText } from "./text";
|
|
2
2
|
|
|
3
|
-
export const DEFAULT_NAMESPACE = "subagent"
|
|
4
|
-
export const MAX_NAMESPACE_LENGTH = 64
|
|
3
|
+
export const DEFAULT_NAMESPACE = "subagent";
|
|
4
|
+
export const MAX_NAMESPACE_LENGTH = 64;
|
|
5
5
|
|
|
6
6
|
export const SESSION_CONTROL_POLICY = {
|
|
7
7
|
/** Only the calling session's subtree. */
|
|
@@ -10,41 +10,41 @@ export const SESSION_CONTROL_POLICY = {
|
|
|
10
10
|
allExceptAncestors: "all-except-ancestors",
|
|
11
11
|
/** Any session except the calling session itself. */
|
|
12
12
|
all: "all",
|
|
13
|
-
} as const
|
|
13
|
+
} as const;
|
|
14
14
|
|
|
15
15
|
export type SessionControlPolicy =
|
|
16
|
-
(typeof SESSION_CONTROL_POLICY)[keyof typeof SESSION_CONTROL_POLICY]
|
|
16
|
+
(typeof SESSION_CONTROL_POLICY)[keyof typeof SESSION_CONTROL_POLICY];
|
|
17
17
|
|
|
18
18
|
export const SESSION_CONTROL_POLICIES: readonly SessionControlPolicy[] =
|
|
19
|
-
Object.values(SESSION_CONTROL_POLICY)
|
|
19
|
+
Object.values(SESSION_CONTROL_POLICY);
|
|
20
20
|
|
|
21
21
|
export const isSessionControlPolicy = (value: unknown): value is SessionControlPolicy =>
|
|
22
22
|
typeof value === "string" &&
|
|
23
|
-
(SESSION_CONTROL_POLICIES as readonly string[]).includes(value)
|
|
23
|
+
(SESSION_CONTROL_POLICIES as readonly string[]).includes(value);
|
|
24
24
|
|
|
25
25
|
export const OUTPUT_FORMAT = {
|
|
26
26
|
text: "text",
|
|
27
27
|
json: "json",
|
|
28
|
-
} as const
|
|
28
|
+
} as const;
|
|
29
29
|
|
|
30
|
-
export type OutputFormat = (typeof OUTPUT_FORMAT)[keyof typeof OUTPUT_FORMAT]
|
|
30
|
+
export type OutputFormat = (typeof OUTPUT_FORMAT)[keyof typeof OUTPUT_FORMAT];
|
|
31
31
|
|
|
32
|
-
export const OUTPUT_FORMATS: readonly OutputFormat[] = Object.values(OUTPUT_FORMAT)
|
|
32
|
+
export const OUTPUT_FORMATS: readonly OutputFormat[] = Object.values(OUTPUT_FORMAT);
|
|
33
33
|
|
|
34
34
|
export const isOutputFormat = (value: unknown): value is OutputFormat =>
|
|
35
|
-
typeof value === "string" && (OUTPUT_FORMATS as readonly string[]).includes(value)
|
|
35
|
+
typeof value === "string" && (OUTPUT_FORMATS as readonly string[]).includes(value);
|
|
36
36
|
|
|
37
|
-
export const DEFAULT_ALLOWED_OUTPUT_FORMATS: readonly OutputFormat[] = OUTPUT_FORMATS
|
|
37
|
+
export const DEFAULT_ALLOWED_OUTPUT_FORMATS: readonly OutputFormat[] = OUTPUT_FORMATS;
|
|
38
38
|
|
|
39
39
|
export const normalizeOutputFormats = (value: unknown): readonly OutputFormat[] => {
|
|
40
|
-
if (!Array.isArray(value)) return DEFAULT_ALLOWED_OUTPUT_FORMATS
|
|
41
|
-
const allowed = new Set<OutputFormat>()
|
|
40
|
+
if (!Array.isArray(value)) return DEFAULT_ALLOWED_OUTPUT_FORMATS;
|
|
41
|
+
const allowed = new Set<OutputFormat>();
|
|
42
42
|
for (const item of value) {
|
|
43
|
-
if (isOutputFormat(item)) allowed.add(item)
|
|
43
|
+
if (isOutputFormat(item)) allowed.add(item);
|
|
44
44
|
}
|
|
45
|
-
if (allowed.size === 0) return DEFAULT_ALLOWED_OUTPUT_FORMATS
|
|
46
|
-
return OUTPUT_FORMATS.filter((format) => allowed.has(format))
|
|
47
|
-
}
|
|
45
|
+
if (allowed.size === 0) return DEFAULT_ALLOWED_OUTPUT_FORMATS;
|
|
46
|
+
return OUTPUT_FORMATS.filter((format) => allowed.has(format));
|
|
47
|
+
};
|
|
48
48
|
|
|
49
49
|
export const LIMIT_SPECS = {
|
|
50
50
|
/** Maximum nodes visited during one server discovery pass. */
|
|
@@ -59,15 +59,15 @@ export const LIMIT_SPECS = {
|
|
|
59
59
|
maxCancelBatch: { default: 20, min: 1, max: 50 },
|
|
60
60
|
/** Maximum characters accepted by steer, queue, and context. */
|
|
61
61
|
maxTextLength: { default: 16000, min: 1, max: 32000 },
|
|
62
|
-
} as const
|
|
62
|
+
} as const;
|
|
63
63
|
|
|
64
|
-
export type LimitKey = keyof typeof LIMIT_SPECS
|
|
64
|
+
export type LimitKey = keyof typeof LIMIT_SPECS;
|
|
65
65
|
|
|
66
|
-
export const LIMIT_KEYS = Object.keys(LIMIT_SPECS) as readonly LimitKey[]
|
|
66
|
+
export const LIMIT_KEYS = Object.keys(LIMIT_SPECS) as readonly LimitKey[];
|
|
67
67
|
|
|
68
|
-
export type Limits = { [Key in LimitKey]?: number }
|
|
68
|
+
export type Limits = { [Key in LimitKey]?: number };
|
|
69
69
|
|
|
70
|
-
export type ResolvedLimits = { [Key in LimitKey]: number }
|
|
70
|
+
export type ResolvedLimits = { [Key in LimitKey]: number };
|
|
71
71
|
|
|
72
72
|
export interface Options {
|
|
73
73
|
/**
|
|
@@ -75,16 +75,16 @@ export interface Options {
|
|
|
75
75
|
* `subagent.control`. Defaults to true; set false to skip permission
|
|
76
76
|
* evaluation entirely.
|
|
77
77
|
*/
|
|
78
|
-
enforcePermissions?: boolean
|
|
78
|
+
enforcePermissions?: boolean;
|
|
79
79
|
/**
|
|
80
80
|
* Who mutating tools may target. Defaults to `"descendants"` (the calling
|
|
81
81
|
* session's subtree). `"all-except-ancestors"` allows foreign sessions but
|
|
82
82
|
* refuses the caller's parent chain. `"all"` allows any session except the
|
|
83
83
|
* caller itself. Read-only tools are not restricted by this option.
|
|
84
84
|
*/
|
|
85
|
-
sessionControlPolicy?: SessionControlPolicy | string
|
|
85
|
+
sessionControlPolicy?: SessionControlPolicy | string;
|
|
86
86
|
/** Tool namespace; tools become `<namespace>_list`, `<namespace>_cancel`, ... */
|
|
87
|
-
namespace?: string
|
|
87
|
+
namespace?: string;
|
|
88
88
|
/**
|
|
89
89
|
* Output formats the tools may return. `"text"` returns only the
|
|
90
90
|
* human-readable content; `"json"` also returns a structured output object.
|
|
@@ -92,20 +92,20 @@ export interface Options {
|
|
|
92
92
|
* ["text", "json"]. Unknown values are ignored; an empty or invalid list
|
|
93
93
|
* falls back to the default.
|
|
94
94
|
*/
|
|
95
|
-
allowedOutputFormats?: readonly string[]
|
|
95
|
+
allowedOutputFormats?: readonly string[];
|
|
96
96
|
/** Register the bundled SKILL.md as a model-facing skill. Defaults to true. */
|
|
97
|
-
registerSkill?: boolean
|
|
97
|
+
registerSkill?: boolean;
|
|
98
98
|
/** Optional tuning limits. See docs/configuration.md. */
|
|
99
|
-
limits?: Limits
|
|
99
|
+
limits?: Limits;
|
|
100
100
|
}
|
|
101
101
|
|
|
102
102
|
export interface Config {
|
|
103
|
-
namespace: string
|
|
104
|
-
enforcePermissions: boolean
|
|
105
|
-
sessionControlPolicy: SessionControlPolicy
|
|
106
|
-
allowedOutputFormats: readonly OutputFormat[]
|
|
107
|
-
registerSkill: boolean
|
|
108
|
-
limits: ResolvedLimits
|
|
103
|
+
namespace: string;
|
|
104
|
+
enforcePermissions: boolean;
|
|
105
|
+
sessionControlPolicy: SessionControlPolicy;
|
|
106
|
+
allowedOutputFormats: readonly OutputFormat[];
|
|
107
|
+
registerSkill: boolean;
|
|
108
|
+
limits: ResolvedLimits;
|
|
109
109
|
}
|
|
110
110
|
|
|
111
111
|
export const limitInteger = (
|
|
@@ -114,22 +114,22 @@ export const limitInteger = (
|
|
|
114
114
|
min: number,
|
|
115
115
|
max: number,
|
|
116
116
|
): number => {
|
|
117
|
-
if (typeof value !== "number" || !Number.isFinite(value)) return fallback
|
|
118
|
-
return Math.min(max, Math.max(min, Math.floor(value)))
|
|
119
|
-
}
|
|
117
|
+
if (typeof value !== "number" || !Number.isFinite(value)) return fallback;
|
|
118
|
+
return Math.min(max, Math.max(min, Math.floor(value)));
|
|
119
|
+
};
|
|
120
120
|
|
|
121
121
|
const resolveLimits = (value: unknown): ResolvedLimits => {
|
|
122
|
-
const raw = (value ?? {}) as Record<string, unknown
|
|
123
|
-
const resolved = {} as ResolvedLimits
|
|
122
|
+
const raw = (value ?? {}) as Record<string, unknown>;
|
|
123
|
+
const resolved = {} as ResolvedLimits;
|
|
124
124
|
for (const key of LIMIT_KEYS) {
|
|
125
|
-
const spec = LIMIT_SPECS[key]
|
|
126
|
-
resolved[key] = limitInteger(raw[key], spec.default, spec.min, spec.max)
|
|
125
|
+
const spec = LIMIT_SPECS[key];
|
|
126
|
+
resolved[key] = limitInteger(raw[key], spec.default, spec.min, spec.max);
|
|
127
127
|
}
|
|
128
|
-
return resolved
|
|
129
|
-
}
|
|
128
|
+
return resolved;
|
|
129
|
+
};
|
|
130
130
|
|
|
131
131
|
export const resolveConfig = (raw: unknown): Config => {
|
|
132
|
-
const options = (raw ?? {}) as Options
|
|
132
|
+
const options = (raw ?? {}) as Options;
|
|
133
133
|
return {
|
|
134
134
|
namespace: clipText(options.namespace, MAX_NAMESPACE_LENGTH) ?? DEFAULT_NAMESPACE,
|
|
135
135
|
enforcePermissions: options.enforcePermissions !== false,
|
|
@@ -139,5 +139,5 @@ export const resolveConfig = (raw: unknown): Config => {
|
|
|
139
139
|
allowedOutputFormats: normalizeOutputFormats(options.allowedOutputFormats),
|
|
140
140
|
registerSkill: options.registerSkill !== false,
|
|
141
141
|
limits: resolveLimits(options.limits),
|
|
142
|
-
}
|
|
143
|
-
}
|
|
142
|
+
};
|
|
143
|
+
};
|
package/src/cursor.ts
CHANGED
|
@@ -1,53 +1,55 @@
|
|
|
1
|
-
import type { SessionRow } from "./session-row"
|
|
2
|
-
import { TOOL, type ToolName } from "./tools/names"
|
|
1
|
+
import type { SessionRow } from "./session-row";
|
|
2
|
+
import { TOOL, type ToolName } from "./tools/names";
|
|
3
3
|
|
|
4
|
-
export const MAX_CURSOR_LENGTH = 512
|
|
5
|
-
export const CURSOR_VERSION = 1
|
|
4
|
+
export const MAX_CURSOR_LENGTH = 512;
|
|
5
|
+
export const CURSOR_VERSION = 1;
|
|
6
6
|
|
|
7
|
-
const CURSOR_TOOLS: ReadonlySet<ToolName> = new Set([TOOL.list, TOOL.cancelDescendants])
|
|
7
|
+
const CURSOR_TOOLS: ReadonlySet<ToolName> = new Set([TOOL.list, TOOL.cancelDescendants]);
|
|
8
8
|
|
|
9
9
|
const isCursorTool = (value: unknown): value is ToolName =>
|
|
10
|
-
typeof value === "string" && CURSOR_TOOLS.has(value as ToolName)
|
|
10
|
+
typeof value === "string" && CURSOR_TOOLS.has(value as ToolName);
|
|
11
11
|
|
|
12
12
|
export interface CursorScope {
|
|
13
|
-
tool: ToolName
|
|
14
|
-
root: string
|
|
15
|
-
filter?: string
|
|
13
|
+
tool: ToolName;
|
|
14
|
+
root: string;
|
|
15
|
+
filter?: string;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
export interface PageCursor {
|
|
19
|
-
launchedAt: number
|
|
20
|
-
id: string
|
|
19
|
+
launchedAt: number;
|
|
20
|
+
id: string;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
interface CursorPayload extends PageCursor {
|
|
24
|
-
v: typeof CURSOR_VERSION
|
|
25
|
-
tool: ToolName
|
|
26
|
-
root: string
|
|
27
|
-
filter?: string
|
|
24
|
+
v: typeof CURSOR_VERSION;
|
|
25
|
+
tool: ToolName;
|
|
26
|
+
root: string;
|
|
27
|
+
filter?: string;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
const encodePayload = (payload: CursorPayload): string =>
|
|
31
|
-
Buffer.from(JSON.stringify(payload), "utf8").toString("base64url")
|
|
31
|
+
Buffer.from(JSON.stringify(payload), "utf8").toString("base64url");
|
|
32
32
|
|
|
33
33
|
const decodePayload = (value: string): CursorPayload | undefined => {
|
|
34
34
|
try {
|
|
35
|
-
const parsed = JSON.parse(
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
if (
|
|
39
|
-
|
|
40
|
-
if (
|
|
35
|
+
const parsed = JSON.parse(
|
|
36
|
+
Buffer.from(value, "base64url").toString("utf8"),
|
|
37
|
+
) as unknown;
|
|
38
|
+
if (!parsed || typeof parsed !== "object") return undefined;
|
|
39
|
+
const payload = parsed as Record<string, unknown>;
|
|
40
|
+
if (payload.v !== CURSOR_VERSION) return undefined;
|
|
41
|
+
if (!isCursorTool(payload.tool)) return undefined;
|
|
42
|
+
if (typeof payload.root !== "string" || payload.root.length === 0) return undefined;
|
|
41
43
|
if (payload.filter !== undefined && typeof payload.filter !== "string")
|
|
42
|
-
return undefined
|
|
44
|
+
return undefined;
|
|
43
45
|
if (
|
|
44
46
|
typeof payload.launchedAt !== "number" ||
|
|
45
47
|
!Number.isFinite(payload.launchedAt) ||
|
|
46
48
|
payload.launchedAt < 0
|
|
47
49
|
) {
|
|
48
|
-
return undefined
|
|
50
|
+
return undefined;
|
|
49
51
|
}
|
|
50
|
-
if (typeof payload.id !== "string" || payload.id.length === 0) return undefined
|
|
52
|
+
if (typeof payload.id !== "string" || payload.id.length === 0) return undefined;
|
|
51
53
|
return {
|
|
52
54
|
v: CURSOR_VERSION,
|
|
53
55
|
tool: payload.tool,
|
|
@@ -55,11 +57,11 @@ const decodePayload = (value: string): CursorPayload | undefined => {
|
|
|
55
57
|
filter: payload.filter as string | undefined,
|
|
56
58
|
launchedAt: payload.launchedAt,
|
|
57
59
|
id: payload.id,
|
|
58
|
-
}
|
|
60
|
+
};
|
|
59
61
|
} catch {
|
|
60
|
-
return undefined
|
|
62
|
+
return undefined;
|
|
61
63
|
}
|
|
62
|
-
}
|
|
64
|
+
};
|
|
63
65
|
|
|
64
66
|
export const encodeCursor = (
|
|
65
67
|
row: Pick<SessionRow, "launchedAt" | "id">,
|
|
@@ -72,41 +74,41 @@ export const encodeCursor = (
|
|
|
72
74
|
filter: scope.filter,
|
|
73
75
|
launchedAt: row.launchedAt,
|
|
74
76
|
id: row.id,
|
|
75
|
-
})
|
|
77
|
+
});
|
|
76
78
|
|
|
77
79
|
const invalidCursorMessage = (toolName: ToolName): string =>
|
|
78
|
-
`Invalid cursor. Re-run the previous ${toolName} call and use the cursor it reported
|
|
80
|
+
`Invalid cursor. Re-run the previous ${toolName} call and use the cursor it reported.`;
|
|
79
81
|
|
|
80
82
|
export const readCursor = (
|
|
81
83
|
value: unknown,
|
|
82
84
|
scope: CursorScope,
|
|
83
85
|
): { cursor?: PageCursor; error?: string } => {
|
|
84
|
-
if (value === undefined) return {}
|
|
86
|
+
if (value === undefined) return {};
|
|
85
87
|
if (
|
|
86
88
|
typeof value !== "string" ||
|
|
87
89
|
value.length === 0 ||
|
|
88
90
|
value.length > MAX_CURSOR_LENGTH
|
|
89
91
|
) {
|
|
90
|
-
return { error: invalidCursorMessage(scope.tool) }
|
|
92
|
+
return { error: invalidCursorMessage(scope.tool) };
|
|
91
93
|
}
|
|
92
|
-
const payload = decodePayload(value)
|
|
94
|
+
const payload = decodePayload(value);
|
|
93
95
|
if (
|
|
94
96
|
!payload ||
|
|
95
97
|
payload.tool !== scope.tool ||
|
|
96
98
|
payload.root !== scope.root ||
|
|
97
99
|
(payload.filter ?? "") !== (scope.filter ?? "")
|
|
98
100
|
) {
|
|
99
|
-
return { error: invalidCursorMessage(scope.tool) }
|
|
101
|
+
return { error: invalidCursorMessage(scope.tool) };
|
|
100
102
|
}
|
|
101
|
-
return { cursor: { launchedAt: payload.launchedAt, id: payload.id } }
|
|
102
|
-
}
|
|
103
|
+
return { cursor: { launchedAt: payload.launchedAt, id: payload.id } };
|
|
104
|
+
};
|
|
103
105
|
|
|
104
|
-
const compareIds = (a: string, b: string): number => (a < b ? -1 : a > b ? 1 : 0)
|
|
106
|
+
const compareIds = (a: string, b: string): number => (a < b ? -1 : a > b ? 1 : 0);
|
|
105
107
|
|
|
106
108
|
/** Newest launch first, then a deterministic ID tie-break for stable paging. */
|
|
107
109
|
export const compareRows = (a: SessionRow, b: SessionRow): number =>
|
|
108
|
-
(b.launchedAt ?? 0) - (a.launchedAt ?? 0) || compareIds(a.id, b.id)
|
|
110
|
+
(b.launchedAt ?? 0) - (a.launchedAt ?? 0) || compareIds(a.id, b.id);
|
|
109
111
|
|
|
110
112
|
export const isAfterCursor = (row: SessionRow, cursor: PageCursor): boolean =>
|
|
111
113
|
row.launchedAt < cursor.launchedAt ||
|
|
112
|
-
(row.launchedAt === cursor.launchedAt && compareIds(row.id, cursor.id) > 0)
|
|
114
|
+
(row.launchedAt === cursor.launchedAt && compareIds(row.id, cursor.id) > 0);
|
package/src/discovery.ts
CHANGED
|
@@ -1,44 +1,51 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
1
|
+
import type { BackgroundJobStore } from "./background";
|
|
2
|
+
import type { ResolvedLimits } from "./config";
|
|
3
|
+
import { compareRows } from "./cursor";
|
|
4
|
+
import type { LiveStore } from "./live-state";
|
|
4
5
|
import {
|
|
5
6
|
normalizeSessionID,
|
|
6
7
|
readSessionListPage,
|
|
7
8
|
type SessionInfoLike,
|
|
8
9
|
type SessionList,
|
|
9
10
|
type SessionListResult,
|
|
10
|
-
} from "./session"
|
|
11
|
-
import { rowFromInfo, type SessionRow } from "./session-row"
|
|
11
|
+
} from "./session";
|
|
12
|
+
import { applySubtreeActivity, rowFromInfo, type SessionRow } from "./session-row";
|
|
12
13
|
|
|
13
|
-
export const MAX_DISCOVERY_PAGE_SIZE = 100
|
|
14
|
+
export const MAX_DISCOVERY_PAGE_SIZE = 100;
|
|
14
15
|
|
|
15
16
|
export interface DiscoveryResult {
|
|
16
|
-
items: SessionInfoLike[]
|
|
17
|
-
complete: boolean
|
|
17
|
+
items: SessionInfoLike[];
|
|
18
|
+
complete: boolean;
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
export interface DiscoveryDeps {
|
|
21
|
-
sessionList: SessionList
|
|
22
|
-
live: LiveStore
|
|
22
|
+
sessionList: SessionList;
|
|
23
|
+
live: LiveStore;
|
|
24
|
+
background: BackgroundJobStore;
|
|
23
25
|
limits: Pick<
|
|
24
26
|
ResolvedLimits,
|
|
25
27
|
"maxDiscoveryNodes" | "maxDiscoveryDepth" | "maxDiscoveryPages"
|
|
26
|
-
|
|
28
|
+
>;
|
|
27
29
|
}
|
|
28
30
|
|
|
29
|
-
export const createDiscovery = ({
|
|
30
|
-
|
|
31
|
+
export const createDiscovery = ({
|
|
32
|
+
sessionList,
|
|
33
|
+
live,
|
|
34
|
+
background,
|
|
35
|
+
limits,
|
|
36
|
+
}: DiscoveryDeps) => {
|
|
37
|
+
const { maxDiscoveryNodes, maxDiscoveryDepth, maxDiscoveryPages } = limits;
|
|
31
38
|
|
|
32
39
|
const listChildren = async (
|
|
33
40
|
parentID: string,
|
|
34
41
|
signal?: AbortSignal,
|
|
35
42
|
): Promise<DiscoveryResult> => {
|
|
36
|
-
const items: SessionInfoLike[] = []
|
|
37
|
-
let cursor: string | undefined
|
|
38
|
-
let complete = true
|
|
43
|
+
const items: SessionInfoLike[] = [];
|
|
44
|
+
let cursor: string | undefined;
|
|
45
|
+
let complete = true;
|
|
39
46
|
for (let pageIndex = 0; pageIndex < maxDiscoveryPages; pageIndex++) {
|
|
40
|
-
if (signal?.aborted) return { items, complete: false }
|
|
41
|
-
let result: SessionListResult | undefined
|
|
47
|
+
if (signal?.aborted) return { items, complete: false };
|
|
48
|
+
let result: SessionListResult | undefined;
|
|
42
49
|
try {
|
|
43
50
|
result = await sessionList(
|
|
44
51
|
{
|
|
@@ -49,34 +56,34 @@ export const createDiscovery = ({ sessionList, live, limits }: DiscoveryDeps) =>
|
|
|
49
56
|
...(cursor ? { cursor } : {}),
|
|
50
57
|
},
|
|
51
58
|
{ signal },
|
|
52
|
-
)
|
|
59
|
+
);
|
|
53
60
|
} catch {
|
|
54
|
-
return { items, complete: false }
|
|
61
|
+
return { items, complete: false };
|
|
55
62
|
}
|
|
56
|
-
if (signal?.aborted) return { items, complete: false }
|
|
57
|
-
const page = readSessionListPage(result)
|
|
58
|
-
if (!page.valid) complete = false
|
|
59
|
-
items.push(...page.items)
|
|
60
|
-
const previousCursor = cursor
|
|
61
|
-
cursor = page.cursor
|
|
63
|
+
if (signal?.aborted) return { items, complete: false };
|
|
64
|
+
const page = readSessionListPage(result);
|
|
65
|
+
if (!page.valid) complete = false;
|
|
66
|
+
items.push(...page.items);
|
|
67
|
+
const previousCursor = cursor;
|
|
68
|
+
cursor = page.cursor;
|
|
62
69
|
if (!cursor) {
|
|
63
70
|
// A full page without a cursor may still be truncated; a short page
|
|
64
71
|
// without a cursor is a clean end.
|
|
65
|
-
if (page.items.length >= MAX_DISCOVERY_PAGE_SIZE) complete = false
|
|
66
|
-
return { items, complete }
|
|
72
|
+
if (page.items.length >= MAX_DISCOVERY_PAGE_SIZE) complete = false;
|
|
73
|
+
return { items, complete };
|
|
67
74
|
}
|
|
68
75
|
// A cursor is the primary signal that more rows exist. Do not stop on a
|
|
69
76
|
// short page: a proxy or server may cap the page below the requested
|
|
70
77
|
// limit and still return a cursor.
|
|
71
78
|
if (cursor === previousCursor) {
|
|
72
79
|
// A repeated cursor cannot make progress.
|
|
73
|
-
complete = false
|
|
74
|
-
return { items, complete }
|
|
80
|
+
complete = false;
|
|
81
|
+
return { items, complete };
|
|
75
82
|
}
|
|
76
|
-
if (pageIndex === maxDiscoveryPages - 1) complete = false
|
|
83
|
+
if (pageIndex === maxDiscoveryPages - 1) complete = false;
|
|
77
84
|
}
|
|
78
|
-
return { items, complete }
|
|
79
|
-
}
|
|
85
|
+
return { items, complete };
|
|
86
|
+
};
|
|
80
87
|
|
|
81
88
|
/**
|
|
82
89
|
* Refresh a subtree from the server. The parentID filter is applied
|
|
@@ -88,64 +95,65 @@ export const createDiscovery = ({ sessionList, live, limits }: DiscoveryDeps) =>
|
|
|
88
95
|
root: string,
|
|
89
96
|
signal?: AbortSignal,
|
|
90
97
|
): Promise<DiscoveryResult> => {
|
|
91
|
-
const seen = new Set<string>([root])
|
|
92
|
-
const queue: Array<{ id: string; depth: number }> = [{ id: root, depth: 0 }]
|
|
93
|
-
const items: SessionInfoLike[] = []
|
|
94
|
-
let complete = true
|
|
95
|
-
let discovered = 0
|
|
98
|
+
const seen = new Set<string>([root]);
|
|
99
|
+
const queue: Array<{ id: string; depth: number }> = [{ id: root, depth: 0 }];
|
|
100
|
+
const items: SessionInfoLike[] = [];
|
|
101
|
+
let complete = true;
|
|
102
|
+
let discovered = 0;
|
|
96
103
|
|
|
97
104
|
for (let index = 0; index < queue.length; index++) {
|
|
98
|
-
if (signal?.aborted) return { items, complete: false }
|
|
99
|
-
const current = queue[index]
|
|
105
|
+
if (signal?.aborted) return { items, complete: false };
|
|
106
|
+
const current = queue[index];
|
|
100
107
|
if (current.depth >= maxDiscoveryDepth) {
|
|
101
108
|
// This node may have children we deliberately did not inspect.
|
|
102
|
-
complete = false
|
|
103
|
-
continue
|
|
109
|
+
complete = false;
|
|
110
|
+
continue;
|
|
104
111
|
}
|
|
105
|
-
const children = await listChildren(current.id, signal)
|
|
106
|
-
if (!children.complete) complete = false
|
|
112
|
+
const children = await listChildren(current.id, signal);
|
|
113
|
+
if (!children.complete) complete = false;
|
|
107
114
|
for (const info of children.items) {
|
|
108
115
|
if (discovered >= maxDiscoveryNodes) {
|
|
109
|
-
complete = false
|
|
110
|
-
break
|
|
116
|
+
complete = false;
|
|
117
|
+
break;
|
|
111
118
|
}
|
|
112
|
-
const id = normalizeSessionID(info.id)
|
|
119
|
+
const id = normalizeSessionID(info.id);
|
|
113
120
|
if (!id) {
|
|
114
|
-
complete = false
|
|
115
|
-
continue
|
|
121
|
+
complete = false;
|
|
122
|
+
continue;
|
|
116
123
|
}
|
|
117
|
-
if (seen.has(id)) continue
|
|
118
|
-
const parentID = normalizeSessionID(info.parentID)
|
|
124
|
+
if (seen.has(id)) continue;
|
|
125
|
+
const parentID = normalizeSessionID(info.parentID);
|
|
119
126
|
if (parentID !== current.id) {
|
|
120
|
-
complete = false
|
|
121
|
-
continue
|
|
127
|
+
complete = false;
|
|
128
|
+
continue;
|
|
122
129
|
}
|
|
123
|
-
seen.add(id)
|
|
124
|
-
items.push(info)
|
|
125
|
-
discovered
|
|
126
|
-
queue.push({ id, depth: current.depth + 1 })
|
|
130
|
+
seen.add(id);
|
|
131
|
+
items.push(info);
|
|
132
|
+
discovered++;
|
|
133
|
+
queue.push({ id, depth: current.depth + 1 });
|
|
127
134
|
}
|
|
128
135
|
if (discovered >= maxDiscoveryNodes) {
|
|
129
|
-
complete = false
|
|
130
|
-
break
|
|
136
|
+
complete = false;
|
|
137
|
+
break;
|
|
131
138
|
}
|
|
132
139
|
}
|
|
133
|
-
return { items, complete }
|
|
134
|
-
}
|
|
140
|
+
return { items, complete };
|
|
141
|
+
};
|
|
135
142
|
|
|
136
143
|
const discoverRows = async (
|
|
137
144
|
root: string,
|
|
138
145
|
signal?: AbortSignal,
|
|
139
146
|
): Promise<{ rows: SessionRow[]; complete: boolean }> => {
|
|
140
|
-
const discovery = await discoverSubtree(root, signal)
|
|
147
|
+
const discovery = await discoverSubtree(root, signal);
|
|
141
148
|
const rows = discovery.items
|
|
142
|
-
.map((info) => rowFromInfo(live, info))
|
|
149
|
+
.map((info) => rowFromInfo(live, background, info))
|
|
143
150
|
.filter((row): row is SessionRow => row !== undefined)
|
|
144
|
-
.toSorted(compareRows)
|
|
145
|
-
|
|
146
|
-
|
|
151
|
+
.toSorted(compareRows);
|
|
152
|
+
applySubtreeActivity(rows);
|
|
153
|
+
return { rows, complete: discovery.complete };
|
|
154
|
+
};
|
|
147
155
|
|
|
148
|
-
return { listChildren, discoverSubtree, discoverRows }
|
|
149
|
-
}
|
|
156
|
+
return { listChildren, discoverSubtree, discoverRows };
|
|
157
|
+
};
|
|
150
158
|
|
|
151
|
-
export type Discovery = ReturnType<typeof createDiscovery
|
|
159
|
+
export type Discovery = ReturnType<typeof createDiscovery>;
|
package/src/errors.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { Error as ToolError } from "@opencode/plugin/promise/tool"
|
|
1
|
+
import { Error as ToolError } from "@opencode/plugin/promise/tool";
|
|
2
2
|
|
|
3
|
-
export const REQUIRED_SESSION_ID = "sessionID is required."
|
|
4
|
-
export const REQUIRED_TEXT = "text is required."
|
|
3
|
+
export const REQUIRED_SESSION_ID = "sessionID is required.";
|
|
4
|
+
export const REQUIRED_TEXT = "text is required.";
|
|
5
5
|
export const OPENCODE_REQUIREMENT =
|
|
6
|
-
"subagent-tools requires
|
|
6
|
+
"subagent-tools requires OpenCode 2.3.0+ with ctx.session.list. See the README.";
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Refusals are tool errors, not successful results. OpenCode surfaces
|
|
@@ -11,8 +11,8 @@ export const OPENCODE_REQUIREMENT =
|
|
|
11
11
|
* instead of treating a refusal as normal output.
|
|
12
12
|
*/
|
|
13
13
|
export const toolError = (message: string): never => {
|
|
14
|
-
throw new ToolError({ message })
|
|
15
|
-
}
|
|
14
|
+
throw new ToolError({ message });
|
|
15
|
+
};
|
|
16
16
|
|
|
17
17
|
export const errorMessage = (error: unknown): string =>
|
|
18
|
-
error instanceof Error ? error.message : String(error)
|
|
18
|
+
error instanceof Error ? error.message : String(error);
|