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/session.ts
CHANGED
|
@@ -1,108 +1,108 @@
|
|
|
1
|
-
import { hasUnsafeIdCharacters } from "./text"
|
|
1
|
+
import { hasUnsafeIdCharacters } from "./text";
|
|
2
2
|
|
|
3
|
-
export const MAX_SESSION_ID_LENGTH = 80
|
|
4
|
-
export const MAX_AGENT_NAME_LENGTH = 80
|
|
5
|
-
export const MAX_TITLE_LENGTH = 200
|
|
6
|
-
export const MAX_PROMPT_PREVIEW_LENGTH = 60
|
|
3
|
+
export const MAX_SESSION_ID_LENGTH = 80;
|
|
4
|
+
export const MAX_AGENT_NAME_LENGTH = 80;
|
|
5
|
+
export const MAX_TITLE_LENGTH = 200;
|
|
6
|
+
export const MAX_PROMPT_PREVIEW_LENGTH = 60;
|
|
7
7
|
|
|
8
8
|
export interface RequestOptions {
|
|
9
|
-
signal?: AbortSignal
|
|
9
|
+
signal?: AbortSignal;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
export interface TokenUsageLike {
|
|
13
|
-
input?: unknown
|
|
14
|
-
output?: unknown
|
|
15
|
-
reasoning?: unknown
|
|
16
|
-
cache?: { read?: unknown; write?: unknown }
|
|
13
|
+
input?: unknown;
|
|
14
|
+
output?: unknown;
|
|
15
|
+
reasoning?: unknown;
|
|
16
|
+
cache?: { read?: unknown; write?: unknown };
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
export interface ModelRefLike {
|
|
20
|
-
id?: string
|
|
21
|
-
providerID?: string
|
|
22
|
-
variant?: string
|
|
20
|
+
id?: string;
|
|
21
|
+
providerID?: string;
|
|
22
|
+
variant?: string;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
export interface ModelInfoLike {
|
|
26
|
-
id?: string
|
|
27
|
-
modelID?: string
|
|
28
|
-
providerID?: string
|
|
29
|
-
limit?: { context?: number }
|
|
26
|
+
id?: string;
|
|
27
|
+
modelID?: string;
|
|
28
|
+
providerID?: string;
|
|
29
|
+
limit?: { context?: number };
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
export interface SessionInfoLike {
|
|
33
|
-
id?: string
|
|
34
|
-
parentID?: string
|
|
35
|
-
agent?: string
|
|
36
|
-
title?: string
|
|
37
|
-
outcome?: string
|
|
38
|
-
model?: ModelRefLike
|
|
39
|
-
cost?: number
|
|
40
|
-
tokens?: TokenUsageLike
|
|
41
|
-
revert?: { messageID?: string }
|
|
33
|
+
id?: string;
|
|
34
|
+
parentID?: string;
|
|
35
|
+
agent?: string;
|
|
36
|
+
title?: string;
|
|
37
|
+
outcome?: string;
|
|
38
|
+
model?: ModelRefLike;
|
|
39
|
+
cost?: number;
|
|
40
|
+
tokens?: TokenUsageLike;
|
|
41
|
+
revert?: { messageID?: string };
|
|
42
42
|
time?: {
|
|
43
|
-
created?: number
|
|
44
|
-
updated?: number
|
|
45
|
-
busy?: number
|
|
46
|
-
idle?: number
|
|
47
|
-
}
|
|
43
|
+
created?: number;
|
|
44
|
+
updated?: number;
|
|
45
|
+
busy?: number;
|
|
46
|
+
idle?: number;
|
|
47
|
+
};
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
/** Normalize a model- or server-provided session ID without silently truncating it. */
|
|
51
51
|
export const normalizeSessionID = (value: unknown): string | undefined => {
|
|
52
|
-
if (typeof value !== "string") return undefined
|
|
53
|
-
const id = value.trim()
|
|
52
|
+
if (typeof value !== "string") return undefined;
|
|
53
|
+
const id = value.trim();
|
|
54
54
|
if (id.length === 0 || id.length > MAX_SESSION_ID_LENGTH || hasUnsafeIdCharacters(id)) {
|
|
55
|
-
return undefined
|
|
55
|
+
return undefined;
|
|
56
56
|
}
|
|
57
|
-
return id
|
|
58
|
-
}
|
|
57
|
+
return id;
|
|
58
|
+
};
|
|
59
59
|
|
|
60
60
|
export const MESSAGE_TYPE = {
|
|
61
61
|
assistant: "assistant",
|
|
62
62
|
compaction: "compaction",
|
|
63
|
-
} as const
|
|
63
|
+
} as const;
|
|
64
64
|
|
|
65
65
|
export const MESSAGE_STATUS = {
|
|
66
66
|
completed: "completed",
|
|
67
|
-
} as const
|
|
67
|
+
} as const;
|
|
68
68
|
|
|
69
69
|
export interface UsageMessage {
|
|
70
|
-
id?: string
|
|
71
|
-
type?: string
|
|
72
|
-
status?: string
|
|
73
|
-
tokens?: TokenUsageLike
|
|
74
|
-
model?: ModelRefLike
|
|
70
|
+
id?: string;
|
|
71
|
+
type?: string;
|
|
72
|
+
status?: string;
|
|
73
|
+
tokens?: TokenUsageLike;
|
|
74
|
+
model?: ModelRefLike;
|
|
75
75
|
}
|
|
76
76
|
|
|
77
77
|
export interface SessionListPage {
|
|
78
|
-
data?: SessionInfoLike[]
|
|
79
|
-
cursor?: { next?: string | null }
|
|
78
|
+
data?: SessionInfoLike[];
|
|
79
|
+
cursor?: { next?: string | null };
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
export type SessionListResult = SessionListPage | SessionInfoLike[]
|
|
82
|
+
export type SessionListResult = SessionListPage | SessionInfoLike[];
|
|
83
83
|
|
|
84
84
|
export type SessionList = (
|
|
85
85
|
input: {
|
|
86
|
-
parentID?: string
|
|
87
|
-
limit?: number
|
|
88
|
-
order?: "asc" | "desc"
|
|
89
|
-
archived?: boolean
|
|
90
|
-
cursor?: string
|
|
86
|
+
parentID?: string;
|
|
87
|
+
limit?: number;
|
|
88
|
+
order?: "asc" | "desc";
|
|
89
|
+
archived?: boolean;
|
|
90
|
+
cursor?: string;
|
|
91
91
|
},
|
|
92
92
|
options?: RequestOptions,
|
|
93
|
-
) => Promise<SessionListResult | undefined
|
|
93
|
+
) => Promise<SessionListResult | undefined>;
|
|
94
94
|
|
|
95
95
|
const isSessionInfoLike = (value: unknown): value is SessionInfoLike =>
|
|
96
96
|
Boolean(value) &&
|
|
97
97
|
typeof value === "object" &&
|
|
98
98
|
typeof (value as SessionInfoLike).id === "string" &&
|
|
99
|
-
(value as SessionInfoLike).id!.length > 0
|
|
99
|
+
(value as SessionInfoLike).id!.length > 0;
|
|
100
100
|
|
|
101
101
|
export interface SessionListPageResult {
|
|
102
|
-
items: SessionInfoLike[]
|
|
103
|
-
cursor?: string
|
|
102
|
+
items: SessionInfoLike[];
|
|
103
|
+
cursor?: string;
|
|
104
104
|
/** False when the response or one of its items was malformed. */
|
|
105
|
-
valid: boolean
|
|
105
|
+
valid: boolean;
|
|
106
106
|
}
|
|
107
107
|
|
|
108
108
|
/**
|
|
@@ -114,28 +114,28 @@ export const readSessionListPage = (
|
|
|
114
114
|
value: SessionListResult | undefined,
|
|
115
115
|
): SessionListPageResult => {
|
|
116
116
|
if (Array.isArray(value)) {
|
|
117
|
-
const items = value.filter(isSessionInfoLike)
|
|
118
|
-
return { items, valid: items.length === value.length }
|
|
117
|
+
const items = value.filter(isSessionInfoLike);
|
|
118
|
+
return { items, valid: items.length === value.length };
|
|
119
119
|
}
|
|
120
120
|
if (!value || typeof value !== "object" || !Array.isArray(value.data)) {
|
|
121
|
-
return { items: [], valid: false }
|
|
121
|
+
return { items: [], valid: false };
|
|
122
122
|
}
|
|
123
|
-
const items = value.data.filter(isSessionInfoLike)
|
|
124
|
-
const cursorValue: unknown = value.cursor
|
|
125
|
-
let cursor: string | undefined
|
|
126
|
-
let cursorValid = true
|
|
123
|
+
const items = value.data.filter(isSessionInfoLike);
|
|
124
|
+
const cursorValue: unknown = value.cursor;
|
|
125
|
+
let cursor: string | undefined;
|
|
126
|
+
let cursorValid = true;
|
|
127
127
|
if (cursorValue !== undefined && cursorValue !== null) {
|
|
128
128
|
if (typeof cursorValue !== "object" || Array.isArray(cursorValue)) {
|
|
129
|
-
cursorValid = false
|
|
129
|
+
cursorValid = false;
|
|
130
130
|
} else {
|
|
131
|
-
const next = (cursorValue as { next?: unknown }).next
|
|
132
|
-
if (typeof next === "string" && next.length > 0) cursor = next
|
|
133
|
-
else if (next !== undefined && next !== null) cursorValid = false
|
|
131
|
+
const next = (cursorValue as { next?: unknown }).next;
|
|
132
|
+
if (typeof next === "string" && next.length > 0) cursor = next;
|
|
133
|
+
else if (next !== undefined && next !== null) cursorValid = false;
|
|
134
134
|
}
|
|
135
135
|
}
|
|
136
136
|
return {
|
|
137
137
|
items,
|
|
138
138
|
cursor,
|
|
139
139
|
valid: items.length === value.data.length && cursorValid,
|
|
140
|
-
}
|
|
141
|
-
}
|
|
140
|
+
};
|
|
141
|
+
};
|
package/src/skill.ts
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
import { readFile } from "node:fs/promises"
|
|
2
|
-
import { fileURLToPath } from "node:url"
|
|
3
|
-
import { Skill, type Plugin } from "@opencode/plugin"
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
import { Skill, type Plugin } from "@opencode/plugin";
|
|
4
4
|
|
|
5
|
-
export const SKILL_ID = "subagent-tools"
|
|
6
|
-
export const SKILL_NAME = "Subagent Tools"
|
|
5
|
+
export const SKILL_ID = "subagent-tools";
|
|
6
|
+
export const SKILL_NAME = "Subagent Tools";
|
|
7
7
|
export const SKILL_DESCRIPTION =
|
|
8
8
|
"Use the subagent tools to list, steer, queue, cancel, and inject context into OpenCode subagents. " +
|
|
9
|
-
"Covers delivery modes, soft interrupts, read-back lag, parent wake, nested depth, and permissions."
|
|
10
|
-
export const SKILL_PATH = fileURLToPath(new URL("../SKILL.md", import.meta.url))
|
|
9
|
+
"Covers delivery modes, soft interrupts, read-back lag, parent wake, nested depth, and permissions.";
|
|
10
|
+
export const SKILL_PATH = fileURLToPath(new URL("../SKILL.md", import.meta.url));
|
|
11
11
|
|
|
12
12
|
/** Read the name/description from a SKILL.md frontmatter block and return the body. */
|
|
13
13
|
export const parseSkillFile = (
|
|
14
14
|
text: string,
|
|
15
15
|
): { name?: string; description?: string; content: string } => {
|
|
16
|
-
const match = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?/.exec(text)
|
|
17
|
-
if (!match) return { content: text }
|
|
18
|
-
const meta = new Map<string, string>()
|
|
16
|
+
const match = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?/.exec(text);
|
|
17
|
+
if (!match) return { content: text };
|
|
18
|
+
const meta = new Map<string, string>();
|
|
19
19
|
for (const line of match[1].split(/\r?\n/)) {
|
|
20
|
-
const separator = line.indexOf(":")
|
|
21
|
-
if (separator <= 0) continue
|
|
22
|
-
const key = line.slice(0, separator).trim()
|
|
20
|
+
const separator = line.indexOf(":");
|
|
21
|
+
if (separator <= 0) continue;
|
|
22
|
+
const key = line.slice(0, separator).trim();
|
|
23
23
|
const value = line
|
|
24
24
|
.slice(separator + 1)
|
|
25
25
|
.trim()
|
|
26
|
-
.replace(/^["']|["']$/g, "")
|
|
27
|
-
if (key && value) meta.set(key, value)
|
|
26
|
+
.replace(/^["']|["']$/g, "");
|
|
27
|
+
if (key && value) meta.set(key, value);
|
|
28
28
|
}
|
|
29
29
|
return {
|
|
30
30
|
name: meta.get("name"),
|
|
31
31
|
description: meta.get("description"),
|
|
32
32
|
content: text.slice(match[0].length),
|
|
33
|
-
}
|
|
34
|
-
}
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
35
|
|
|
36
36
|
/**
|
|
37
37
|
* Register the bundled SKILL.md as a model-facing skill. The tools remain the
|
|
@@ -39,8 +39,8 @@ export const parseSkillFile = (
|
|
|
39
39
|
*/
|
|
40
40
|
export const registerSkill = async (skill: Plugin.Context["skill"]) => {
|
|
41
41
|
try {
|
|
42
|
-
const skillText = await readFile(SKILL_PATH, "utf8")
|
|
43
|
-
const parsed = parseSkillFile(skillText)
|
|
42
|
+
const skillText = await readFile(SKILL_PATH, "utf8");
|
|
43
|
+
const parsed = parseSkillFile(skillText);
|
|
44
44
|
return await skill.transform((editor) => {
|
|
45
45
|
editor.add({
|
|
46
46
|
id: Skill.ID.make(SKILL_ID),
|
|
@@ -48,10 +48,10 @@ export const registerSkill = async (skill: Plugin.Context["skill"]) => {
|
|
|
48
48
|
description: parsed.description ?? SKILL_DESCRIPTION,
|
|
49
49
|
path: Skill.Info.fields.path.make(SKILL_PATH),
|
|
50
50
|
content: parsed.content,
|
|
51
|
-
})
|
|
52
|
-
})
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
53
|
} catch (error) {
|
|
54
|
-
console.error("subagent-tools: failed to register SKILL.md", error)
|
|
55
|
-
return undefined
|
|
54
|
+
console.error("subagent-tools: failed to register SKILL.md", error);
|
|
55
|
+
return undefined;
|
|
56
56
|
}
|
|
57
|
-
}
|
|
57
|
+
};
|
package/src/status.ts
CHANGED
|
@@ -1,119 +1,119 @@
|
|
|
1
|
-
import type { SessionInfoLike } from "./session"
|
|
1
|
+
import type { SessionInfoLike } from "./session";
|
|
2
2
|
|
|
3
|
-
export const
|
|
3
|
+
export const RUN_STATUS = {
|
|
4
4
|
running: "running",
|
|
5
5
|
stopped: "stopped",
|
|
6
6
|
failed: "failed",
|
|
7
7
|
finished: "finished",
|
|
8
8
|
unknown: "unknown",
|
|
9
|
-
} as const
|
|
9
|
+
} as const;
|
|
10
10
|
|
|
11
|
-
export type
|
|
11
|
+
export type RunStatus = (typeof RUN_STATUS)[keyof typeof RUN_STATUS];
|
|
12
12
|
|
|
13
13
|
export const OUTCOME = {
|
|
14
14
|
interrupted: "interrupted",
|
|
15
15
|
failed: "failed",
|
|
16
16
|
succeeded: "succeeded",
|
|
17
|
-
} as const
|
|
17
|
+
} as const;
|
|
18
18
|
|
|
19
|
-
export type Outcome = (typeof OUTCOME)[keyof typeof OUTCOME]
|
|
19
|
+
export type Outcome = (typeof OUTCOME)[keyof typeof OUTCOME];
|
|
20
20
|
|
|
21
|
-
export const
|
|
22
|
-
const
|
|
21
|
+
export const RUN_STATUSES: readonly RunStatus[] = Object.values(RUN_STATUS);
|
|
22
|
+
const RUN_STATUS_SET: ReadonlySet<string> = new Set(RUN_STATUSES);
|
|
23
23
|
|
|
24
|
-
export const
|
|
25
|
-
|
|
24
|
+
export const isRunStatus = (value: string): value is RunStatus =>
|
|
25
|
+
RUN_STATUS_SET.has(value);
|
|
26
26
|
|
|
27
|
-
export interface
|
|
27
|
+
export interface RunStatusMeta {
|
|
28
28
|
/** The run is over; no further step is expected without a resume. */
|
|
29
|
-
terminal: boolean
|
|
29
|
+
terminal: boolean;
|
|
30
30
|
/** Persisted usage may lag because the session is active or unresolved. */
|
|
31
|
-
usageMayLag: boolean
|
|
31
|
+
usageMayLag: boolean;
|
|
32
32
|
/** Safe to treat a message with `resume: false` as pending. */
|
|
33
|
-
idle: boolean
|
|
33
|
+
idle: boolean;
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
export const
|
|
37
|
-
[
|
|
38
|
-
[
|
|
39
|
-
[
|
|
40
|
-
[
|
|
36
|
+
export const RUN_STATUS_META: Readonly<Record<RunStatus, RunStatusMeta>> = {
|
|
37
|
+
[RUN_STATUS.running]: { terminal: false, usageMayLag: true, idle: false },
|
|
38
|
+
[RUN_STATUS.stopped]: { terminal: true, usageMayLag: false, idle: true },
|
|
39
|
+
[RUN_STATUS.failed]: { terminal: true, usageMayLag: false, idle: true },
|
|
40
|
+
[RUN_STATUS.finished]: { terminal: true, usageMayLag: false, idle: true },
|
|
41
41
|
// `unknown` may still be running, so it is neither terminal nor idle.
|
|
42
|
-
[
|
|
43
|
-
}
|
|
42
|
+
[RUN_STATUS.unknown]: { terminal: false, usageMayLag: true, idle: false },
|
|
43
|
+
};
|
|
44
44
|
|
|
45
|
-
const
|
|
46
|
-
Object.entries(
|
|
47
|
-
)
|
|
45
|
+
const RUN_STATUS_META_MAP: ReadonlyMap<RunStatus, RunStatusMeta> = new Map(
|
|
46
|
+
Object.entries(RUN_STATUS_META) as Array<[RunStatus, RunStatusMeta]>,
|
|
47
|
+
);
|
|
48
48
|
|
|
49
|
-
export const
|
|
50
|
-
|
|
49
|
+
export const runStatusMeta = (status: RunStatus): RunStatusMeta =>
|
|
50
|
+
RUN_STATUS_META_MAP.get(status) ?? RUN_STATUS_META[RUN_STATUS.unknown];
|
|
51
51
|
|
|
52
|
-
export const
|
|
53
|
-
|
|
52
|
+
export const runStatusUsageMayLag = (status: RunStatus): boolean =>
|
|
53
|
+
runStatusMeta(status).usageMayLag;
|
|
54
54
|
|
|
55
|
-
export const
|
|
56
|
-
status !== undefined &&
|
|
55
|
+
export const runStatusIsIdle = (status: RunStatus | undefined): boolean =>
|
|
56
|
+
status !== undefined && runStatusMeta(status).idle;
|
|
57
57
|
|
|
58
|
-
export const
|
|
59
|
-
|
|
60
|
-
)
|
|
58
|
+
export const TERMINAL_RUN_STATUSES: ReadonlySet<RunStatus> = new Set(
|
|
59
|
+
RUN_STATUSES.filter((status) => runStatusMeta(status).terminal),
|
|
60
|
+
);
|
|
61
61
|
|
|
62
|
-
export const OUTCOMES: readonly Outcome[] = Object.values(OUTCOME)
|
|
63
|
-
const OUTCOME_SET: ReadonlySet<string> = new Set(OUTCOMES)
|
|
62
|
+
export const OUTCOMES: readonly Outcome[] = Object.values(OUTCOME);
|
|
63
|
+
const OUTCOME_SET: ReadonlySet<string> = new Set(OUTCOMES);
|
|
64
64
|
|
|
65
65
|
export const isOutcome = (value: unknown): value is Outcome =>
|
|
66
|
-
typeof value === "string" && OUTCOME_SET.has(value)
|
|
66
|
+
typeof value === "string" && OUTCOME_SET.has(value);
|
|
67
67
|
|
|
68
|
-
export const
|
|
69
|
-
[OUTCOME.interrupted]:
|
|
70
|
-
[OUTCOME.failed]:
|
|
71
|
-
[OUTCOME.succeeded]:
|
|
72
|
-
}
|
|
68
|
+
export const OUTCOME_RUN_STATUS: Readonly<Record<Outcome, RunStatus>> = {
|
|
69
|
+
[OUTCOME.interrupted]: RUN_STATUS.stopped,
|
|
70
|
+
[OUTCOME.failed]: RUN_STATUS.failed,
|
|
71
|
+
[OUTCOME.succeeded]: RUN_STATUS.finished,
|
|
72
|
+
};
|
|
73
73
|
|
|
74
|
-
const
|
|
75
|
-
Object.entries(
|
|
76
|
-
)
|
|
74
|
+
const OUTCOME_RUN_STATUS_MAP: ReadonlyMap<Outcome, RunStatus> = new Map(
|
|
75
|
+
Object.entries(OUTCOME_RUN_STATUS) as Array<[Outcome, RunStatus]>,
|
|
76
|
+
);
|
|
77
77
|
|
|
78
|
-
const
|
|
79
|
-
if (outcome === undefined) return undefined
|
|
80
|
-
if (!isOutcome(outcome)) return
|
|
81
|
-
return
|
|
82
|
-
}
|
|
78
|
+
const runStatusFromOutcome = (outcome: unknown): RunStatus | undefined => {
|
|
79
|
+
if (outcome === undefined) return undefined;
|
|
80
|
+
if (!isOutcome(outcome)) return RUN_STATUS.unknown;
|
|
81
|
+
return OUTCOME_RUN_STATUS_MAP.get(outcome);
|
|
82
|
+
};
|
|
83
83
|
|
|
84
|
-
export const
|
|
84
|
+
export const OPENCODE_SESSION_STATUS = {
|
|
85
85
|
busy: "busy",
|
|
86
86
|
retry: "retry",
|
|
87
87
|
idle: "idle",
|
|
88
|
-
} as const
|
|
88
|
+
} as const;
|
|
89
89
|
|
|
90
90
|
export const BUSY_SESSION_STATUSES: ReadonlySet<string> = new Set([
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
])
|
|
91
|
+
OPENCODE_SESSION_STATUS.busy,
|
|
92
|
+
OPENCODE_SESSION_STATUS.retry,
|
|
93
|
+
]);
|
|
94
94
|
|
|
95
95
|
/**
|
|
96
96
|
* Derive a state from a session record. `busy`/`idle` describe the current
|
|
97
97
|
* execution; `outcome` describes the last completed one. A resumed child can be
|
|
98
98
|
* running while its outcome is still the previous run's.
|
|
99
99
|
*/
|
|
100
|
-
export const
|
|
101
|
-
const idle = info.time?.idle
|
|
102
|
-
const busy = info.time?.busy
|
|
103
|
-
const outcomeStatus =
|
|
100
|
+
export const runStatusFromSession = (info: SessionInfoLike): RunStatus | undefined => {
|
|
101
|
+
const idle = info.time?.idle;
|
|
102
|
+
const busy = info.time?.busy;
|
|
103
|
+
const outcomeStatus = runStatusFromOutcome(info.outcome);
|
|
104
104
|
|
|
105
105
|
// A terminal outcome is recorded at `time.idle`. Without that marker, the
|
|
106
106
|
// outcome is the most reliable evidence we have.
|
|
107
107
|
if (idle === undefined) {
|
|
108
|
-
if (outcomeStatus) return outcomeStatus
|
|
109
|
-
return typeof busy === "number" ?
|
|
108
|
+
if (outcomeStatus) return outcomeStatus;
|
|
109
|
+
return typeof busy === "number" ? RUN_STATUS.running : undefined;
|
|
110
110
|
}
|
|
111
111
|
|
|
112
112
|
// `busy`/`idle` describe the current execution; `outcome` describes the last
|
|
113
113
|
// completed one. A resumed child can have a stale outcome while busy > idle.
|
|
114
|
-
if (typeof busy === "number" && busy > idle) return
|
|
115
|
-
if (outcomeStatus) return outcomeStatus
|
|
114
|
+
if (typeof busy === "number" && busy > idle) return RUN_STATUS.running;
|
|
115
|
+
if (outcomeStatus) return outcomeStatus;
|
|
116
116
|
// An unrecognised outcome stays `unknown` instead of falling back to a
|
|
117
117
|
// timestamp heuristic that may claim the session is running.
|
|
118
|
-
return (info.time?.updated ?? 0) > idle ?
|
|
119
|
-
}
|
|
118
|
+
return (info.time?.updated ?? 0) > idle ? RUN_STATUS.running : undefined;
|
|
119
|
+
};
|
package/src/text.ts
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
/** Truncate a string to a maximum number of Unicode code points. */
|
|
2
2
|
export const clip = (value: unknown, max: number): string | undefined => {
|
|
3
|
-
if (typeof value !== "string" || value.length === 0) return undefined
|
|
4
|
-
const chars = Array.from(value)
|
|
5
|
-
return chars.length > max ? chars.slice(0, max).join("") : value
|
|
6
|
-
}
|
|
3
|
+
if (typeof value !== "string" || value.length === 0) return undefined;
|
|
4
|
+
const chars = Array.from(value);
|
|
5
|
+
return chars.length > max ? chars.slice(0, max).join("") : value;
|
|
6
|
+
};
|
|
7
7
|
|
|
8
8
|
/** Clip a model-provided string, trimming surrounding whitespace and rejecting blank input. */
|
|
9
9
|
export const clipText = (value: unknown, max: number): string | undefined => {
|
|
10
|
-
const text = clip(value, max)?.trim()
|
|
11
|
-
return text && text.length > 0 ? text : undefined
|
|
12
|
-
}
|
|
10
|
+
const text = clip(value, max)?.trim();
|
|
11
|
+
return text && text.length > 0 ? text : undefined;
|
|
12
|
+
};
|
|
13
13
|
|
|
14
14
|
/** Count characters by Unicode code point, matching JSON Schema maxLength. */
|
|
15
|
-
export const textLength = (value: string): number => Array.from(value).length
|
|
15
|
+
export const textLength = (value: string): number => Array.from(value).length;
|
|
16
16
|
|
|
17
17
|
/** C0/C1 controls, DEL, and Unicode line/paragraph separators. */
|
|
18
18
|
export const isUnsafeControlCode = (code: number): boolean =>
|
|
19
|
-
code < 0x20 || (code >= 0x7f && code <= 0x9f) || code === 0x2028 || code === 0x2029
|
|
19
|
+
code < 0x20 || (code >= 0x7f && code <= 0x9f) || code === 0x2028 || code === 0x2029;
|
|
20
20
|
|
|
21
21
|
/** Reject spaces and control characters in identifiers. */
|
|
22
22
|
export const hasUnsafeIdCharacters = (value: string): boolean =>
|
|
23
23
|
Array.from(value).some((char) => {
|
|
24
|
-
const code = char.codePointAt(0) ?? 0
|
|
25
|
-
return code <= 0x20 || isUnsafeControlCode(code)
|
|
26
|
-
})
|
|
24
|
+
const code = char.codePointAt(0) ?? 0;
|
|
25
|
+
return code <= 0x20 || isUnsafeControlCode(code);
|
|
26
|
+
});
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
29
|
* Reject over-long input with a short message instead of letting the tool
|
|
@@ -34,9 +34,9 @@ export const maxLengthError = (
|
|
|
34
34
|
max: number,
|
|
35
35
|
field: string,
|
|
36
36
|
): string | undefined => {
|
|
37
|
-
if (typeof value !== "string") return undefined
|
|
38
|
-
const length = textLength(value)
|
|
37
|
+
if (typeof value !== "string") return undefined;
|
|
38
|
+
const length = textLength(value);
|
|
39
39
|
return length > max
|
|
40
40
|
? `${field} is ${length} characters; maximum is ${max}. Shorten it and retry.`
|
|
41
|
-
: undefined
|
|
42
|
-
}
|
|
41
|
+
: undefined;
|
|
42
|
+
};
|