@myagentroam/node 0.1.0
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.md +17 -0
- package/dist/capabilities.d.ts +5 -0
- package/dist/capabilities.js +23 -0
- package/dist/capabilities.js.map +1 -0
- package/dist/claude-agent-sdk.d.ts +101 -0
- package/dist/claude-agent-sdk.js +341 -0
- package/dist/claude-agent-sdk.js.map +1 -0
- package/dist/claude-channel.d.ts +28 -0
- package/dist/claude-channel.js +45 -0
- package/dist/claude-channel.js.map +1 -0
- package/dist/codex-app-server.d.ts +96 -0
- package/dist/codex-app-server.js +361 -0
- package/dist/codex-app-server.js.map +1 -0
- package/dist/config.d.ts +17 -0
- package/dist/config.js +51 -0
- package/dist/config.js.map +1 -0
- package/dist/connector.d.ts +343 -0
- package/dist/connector.js +5525 -0
- package/dist/connector.js.map +1 -0
- package/dist/database.d.ts +282 -0
- package/dist/database.js +1347 -0
- package/dist/database.js.map +1 -0
- package/dist/event-buffer.d.ts +12 -0
- package/dist/event-buffer.js +29 -0
- package/dist/event-buffer.js.map +1 -0
- package/dist/fake-runner.d.ts +42 -0
- package/dist/fake-runner.js +130 -0
- package/dist/fake-runner.js.map +1 -0
- package/dist/health.d.ts +4 -0
- package/dist/health.js +4 -0
- package/dist/health.js.map +1 -0
- package/dist/main.d.ts +2 -0
- package/dist/main.js +47 -0
- package/dist/main.js.map +1 -0
- package/dist/native-session-history.d.ts +72 -0
- package/dist/native-session-history.js +647 -0
- package/dist/native-session-history.js.map +1 -0
- package/dist/operational.d.ts +30 -0
- package/dist/operational.js +82 -0
- package/dist/operational.js.map +1 -0
- package/dist/process-tree.d.ts +9 -0
- package/dist/process-tree.js +19 -0
- package/dist/process-tree.js.map +1 -0
- package/dist/runner-command-engine.d.ts +19 -0
- package/dist/runner-command-engine.js +47 -0
- package/dist/runner-command-engine.js.map +1 -0
- package/dist/runner-profiles.d.ts +11 -0
- package/dist/runner-profiles.js +138 -0
- package/dist/runner-profiles.js.map +1 -0
- package/dist/runner-usage.d.ts +33 -0
- package/dist/runner-usage.js +193 -0
- package/dist/runner-usage.js.map +1 -0
- package/dist/runtime-state.d.ts +174 -0
- package/dist/runtime-state.js +957 -0
- package/dist/runtime-state.js.map +1 -0
- package/dist/service.d.ts +4 -0
- package/dist/service.js +39 -0
- package/dist/service.js.map +1 -0
- package/dist/storage.d.ts +2 -0
- package/dist/storage.js +33 -0
- package/dist/storage.js.map +1 -0
- package/dist/terminal.d.ts +132 -0
- package/dist/terminal.js +417 -0
- package/dist/terminal.js.map +1 -0
- package/dist/workspace.d.ts +116 -0
- package/dist/workspace.js +732 -0
- package/dist/workspace.js.map +1 -0
- package/package.json +36 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { homedir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
const CODEX_USAGE_URL = 'https://chatgpt.com/backend-api/wham/usage';
|
|
5
|
+
const CLAUDE_USAGE_URL = 'https://api.anthropic.com/api/oauth/usage';
|
|
6
|
+
const CREDENTIALS_MAX_BYTES = 256 * 1024;
|
|
7
|
+
const RESPONSE_MAX_BYTES = 256 * 1024;
|
|
8
|
+
const CODEX_CACHE_MS = 60_000;
|
|
9
|
+
const CLAUDE_CACHE_MS = 5 * 60_000;
|
|
10
|
+
export const USAGE_OAUTH_REQUIRED = 'OFFICIAL_OAUTH_REQUIRED';
|
|
11
|
+
export class RunnerUsageReader {
|
|
12
|
+
fetcher;
|
|
13
|
+
homePath;
|
|
14
|
+
now;
|
|
15
|
+
cache = new Map();
|
|
16
|
+
constructor(fetcher = fetch, homePath = homedir(), now = Date.now) {
|
|
17
|
+
this.fetcher = fetcher;
|
|
18
|
+
this.homePath = homePath;
|
|
19
|
+
this.now = now;
|
|
20
|
+
}
|
|
21
|
+
async status(runner, runnerAvailable) {
|
|
22
|
+
if (!runnerAvailable)
|
|
23
|
+
return unavailable(runner, 'RUNNER_UNAVAILABLE');
|
|
24
|
+
return (await this.credentials(runner)) === undefined
|
|
25
|
+
? unavailable(runner, USAGE_OAUTH_REQUIRED)
|
|
26
|
+
: { runner, available: true, reasonCode: null };
|
|
27
|
+
}
|
|
28
|
+
async read(runner, runnerAvailable) {
|
|
29
|
+
const status = await this.status(runner, runnerAvailable);
|
|
30
|
+
if (!status.available)
|
|
31
|
+
throw new Error(status.reasonCode ?? 'USAGE_UNAVAILABLE');
|
|
32
|
+
const cached = this.cache.get(runner);
|
|
33
|
+
const now = this.now();
|
|
34
|
+
if (cached !== undefined && now - cached.fetchedAt < cacheDuration(runner)) {
|
|
35
|
+
return { ...cached, cached: true };
|
|
36
|
+
}
|
|
37
|
+
const credentials = await this.credentials(runner);
|
|
38
|
+
if (credentials === undefined)
|
|
39
|
+
throw new Error(USAGE_OAUTH_REQUIRED);
|
|
40
|
+
const usage = runner === 'codex'
|
|
41
|
+
? await this.readCodex(credentials, now)
|
|
42
|
+
: await this.readClaude(credentials, now);
|
|
43
|
+
this.cache.set(runner, usage);
|
|
44
|
+
return usage;
|
|
45
|
+
}
|
|
46
|
+
async credentials(runner) {
|
|
47
|
+
const path = runner === 'codex'
|
|
48
|
+
? join(this.homePath, '.codex', 'auth.json')
|
|
49
|
+
: join(this.homePath, '.claude', '.credentials.json');
|
|
50
|
+
const value = await readJson(path);
|
|
51
|
+
return runner === 'codex' ? codexCredentials(value) : claudeCredentials(value);
|
|
52
|
+
}
|
|
53
|
+
async readCodex(credentials, fetchedAt) {
|
|
54
|
+
const headers = {
|
|
55
|
+
Authorization: `Bearer ${credentials.accessToken}`
|
|
56
|
+
};
|
|
57
|
+
if (credentials.accountId !== undefined)
|
|
58
|
+
headers['ChatGPT-Account-Id'] = credentials.accountId;
|
|
59
|
+
const value = await requestJson(this.fetcher, CODEX_USAGE_URL, headers);
|
|
60
|
+
const windows = [
|
|
61
|
+
codexUsageWindow(value, 'primary_window'),
|
|
62
|
+
codexUsageWindow(value, 'secondary_window')
|
|
63
|
+
].filter((window) => window !== undefined);
|
|
64
|
+
if (windows.length === 0)
|
|
65
|
+
throw new Error('USAGE_RESPONSE_INVALID');
|
|
66
|
+
return { runner: 'codex', windows, fetchedAt, cached: false };
|
|
67
|
+
}
|
|
68
|
+
async readClaude(credentials, fetchedAt) {
|
|
69
|
+
const value = await requestJson(this.fetcher, CLAUDE_USAGE_URL, {
|
|
70
|
+
Authorization: `Bearer ${credentials.accessToken}`,
|
|
71
|
+
'anthropic-beta': 'oauth-2025-04-20',
|
|
72
|
+
'Content-Type': 'application/json'
|
|
73
|
+
});
|
|
74
|
+
const windows = [
|
|
75
|
+
usageWindow(value, 'five_hour', '5 小时额度'),
|
|
76
|
+
usageWindow(value, 'seven_day', '7 天额度'),
|
|
77
|
+
usageWindow(value, 'seven_day_sonnet', '7 天 Sonnet 额度')
|
|
78
|
+
].filter((window) => window !== undefined);
|
|
79
|
+
if (windows.length === 0)
|
|
80
|
+
throw new Error('USAGE_RESPONSE_INVALID');
|
|
81
|
+
return { runner: 'claude-code', windows, fetchedAt, cached: false };
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function unavailable(runner, reasonCode) {
|
|
85
|
+
return { runner, available: false, reasonCode };
|
|
86
|
+
}
|
|
87
|
+
function cacheDuration(runner) {
|
|
88
|
+
return runner === 'codex' ? CODEX_CACHE_MS : CLAUDE_CACHE_MS;
|
|
89
|
+
}
|
|
90
|
+
async function readJson(path) {
|
|
91
|
+
try {
|
|
92
|
+
const text = await readFile(path, 'utf8');
|
|
93
|
+
if (text.length > CREDENTIALS_MAX_BYTES)
|
|
94
|
+
return undefined;
|
|
95
|
+
return JSON.parse(text);
|
|
96
|
+
}
|
|
97
|
+
catch {
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function codexCredentials(value) {
|
|
102
|
+
const root = record(value);
|
|
103
|
+
const tokens = record(root?.tokens) ?? root;
|
|
104
|
+
const accessToken = string(tokens?.access_token) ?? string(tokens?.accessToken);
|
|
105
|
+
if (accessToken === undefined)
|
|
106
|
+
return undefined;
|
|
107
|
+
const accountId = string(tokens?.account_id) ?? string(tokens?.accountId) ?? string(root?.account_id);
|
|
108
|
+
return { accessToken, ...(accountId === undefined ? {} : { accountId }) };
|
|
109
|
+
}
|
|
110
|
+
function claudeCredentials(value) {
|
|
111
|
+
const credentials = record(record(value)?.claudeAiOauth);
|
|
112
|
+
const accessToken = string(credentials?.accessToken);
|
|
113
|
+
return accessToken === undefined ? undefined : { accessToken };
|
|
114
|
+
}
|
|
115
|
+
async function requestJson(fetcher, url, headers) {
|
|
116
|
+
const controller = new AbortController();
|
|
117
|
+
const timer = setTimeout(() => controller.abort(), 5_000);
|
|
118
|
+
try {
|
|
119
|
+
const response = await fetcher(url, {
|
|
120
|
+
method: 'GET',
|
|
121
|
+
headers,
|
|
122
|
+
redirect: 'error',
|
|
123
|
+
signal: controller.signal
|
|
124
|
+
});
|
|
125
|
+
if (response.status === 401 || response.status === 403)
|
|
126
|
+
throw new Error('USAGE_AUTHENTICATION_FAILED');
|
|
127
|
+
if (response.status === 429)
|
|
128
|
+
throw new Error('USAGE_RATE_LIMITED');
|
|
129
|
+
if (!response.ok)
|
|
130
|
+
throw new Error('USAGE_REQUEST_FAILED');
|
|
131
|
+
const text = await response.text();
|
|
132
|
+
if (text.length > RESPONSE_MAX_BYTES)
|
|
133
|
+
throw new Error('USAGE_RESPONSE_INVALID');
|
|
134
|
+
return JSON.parse(text);
|
|
135
|
+
}
|
|
136
|
+
catch (error) {
|
|
137
|
+
if (error instanceof Error && error.name === 'AbortError')
|
|
138
|
+
throw new Error('USAGE_REQUEST_TIMEOUT');
|
|
139
|
+
throw error;
|
|
140
|
+
}
|
|
141
|
+
finally {
|
|
142
|
+
clearTimeout(timer);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function usageWindow(value, key, label) {
|
|
146
|
+
const window = record(record(value)?.rate_limit)?.[key] ?? record(value)?.[key];
|
|
147
|
+
const source = record(window);
|
|
148
|
+
const usedPercentage = number(source?.utilization) ?? number(source?.used_percentage) ?? number(source?.used_percent);
|
|
149
|
+
if (usedPercentage === undefined || usedPercentage < 0 || usedPercentage > 100)
|
|
150
|
+
return undefined;
|
|
151
|
+
const resetsAt = timestamp(source?.resets_at) ?? timestamp(source?.reset_at);
|
|
152
|
+
return { id: key, label, usedPercentage, resetsAt };
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Codex backend window names are roles, not durations: depending on a plan,
|
|
156
|
+
* `primary_window` may itself be the weekly quota. Community clients classify
|
|
157
|
+
* them using the response's `limit_window_seconds`, never their position.
|
|
158
|
+
*/
|
|
159
|
+
function codexUsageWindow(value, key) {
|
|
160
|
+
const window = record(record(value)?.rate_limit)?.[key];
|
|
161
|
+
const duration = number(record(window)?.limit_window_seconds);
|
|
162
|
+
const label = duration !== undefined && approximately(duration, 5 * 60 * 60)
|
|
163
|
+
? '5 小时额度'
|
|
164
|
+
: duration !== undefined && approximately(duration, 7 * 24 * 60 * 60)
|
|
165
|
+
? '7 天额度'
|
|
166
|
+
: undefined;
|
|
167
|
+
return label === undefined ? undefined : usageWindow(value, key, label);
|
|
168
|
+
}
|
|
169
|
+
function approximately(value, expected) {
|
|
170
|
+
return Math.abs(value - expected) / expected <= 0.1;
|
|
171
|
+
}
|
|
172
|
+
function record(value) {
|
|
173
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
174
|
+
? value
|
|
175
|
+
: undefined;
|
|
176
|
+
}
|
|
177
|
+
function string(value) {
|
|
178
|
+
return typeof value === 'string' && value.length > 0 && value.length <= 20_000
|
|
179
|
+
? value
|
|
180
|
+
: undefined;
|
|
181
|
+
}
|
|
182
|
+
function number(value) {
|
|
183
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
|
|
184
|
+
}
|
|
185
|
+
function timestamp(value) {
|
|
186
|
+
if (typeof value === 'number' && Number.isFinite(value) && value >= 0)
|
|
187
|
+
return Math.floor(value < 10_000_000_000 ? value * 1_000 : value);
|
|
188
|
+
if (typeof value !== 'string')
|
|
189
|
+
return null;
|
|
190
|
+
const parsed = Date.parse(value);
|
|
191
|
+
return Number.isFinite(parsed) && parsed >= 0 ? parsed : null;
|
|
192
|
+
}
|
|
193
|
+
//# sourceMappingURL=runner-usage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runner-usage.js","sourceRoot":"","sources":["../src/runner-usage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAGjC,MAAM,eAAe,GAAG,4CAA4C,CAAC;AACrE,MAAM,gBAAgB,GAAG,2CAA2C,CAAC;AACrE,MAAM,qBAAqB,GAAG,GAAG,GAAG,IAAI,CAAC;AACzC,MAAM,kBAAkB,GAAG,GAAG,GAAG,IAAI,CAAC;AACtC,MAAM,cAAc,GAAG,MAAM,CAAC;AAC9B,MAAM,eAAe,GAAG,CAAC,GAAG,MAAM,CAAC;AAEnC,MAAM,CAAC,MAAM,oBAAoB,GAAG,yBAAyB,CAAC;AA6B9D,MAAM,OAAO,iBAAiB;IAIT;IACA;IACA;IALF,KAAK,GAAG,IAAI,GAAG,EAA2B,CAAC;IAE5D,YACmB,UAAqB,KAAK,EAC1B,WAAW,OAAO,EAAE,EACpB,MAAoB,IAAI,CAAC,GAAG;QAF5B,YAAO,GAAP,OAAO,CAAmB;QAC1B,aAAQ,GAAR,QAAQ,CAAY;QACpB,QAAG,GAAH,GAAG,CAAyB;IAC5C,CAAC;IAEJ,KAAK,CAAC,MAAM,CAAC,MAAkB,EAAE,eAAwB;QACvD,IAAI,CAAC,eAAe;YAAE,OAAO,WAAW,CAAC,MAAM,EAAE,oBAAoB,CAAC,CAAC;QACvE,OAAO,CAAC,MAAM,IAAI,CAAC,WAAW,C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Page, ConversationPage, NativeSessionControl, NodeAgentRun, NodeAgentSession, NodeApproval, NodeConversationTurn, NodeMessageAttachmentInput, NodeRunEvent, NodeWorkspace, WorkspaceActivityPage } from './database.js';
|
|
3
|
+
/**
|
|
4
|
+
* Deliberately process-local runtime domain. No method in this class writes
|
|
5
|
+
* into Node SQLite: a Node restart loses active runs, approvals, leases and
|
|
6
|
+
* managed-session aliases by design. Durable conversation history belongs to
|
|
7
|
+
* the Runner/native transcript, never to MAR.
|
|
8
|
+
*/
|
|
9
|
+
export declare class NodeRuntimeState {
|
|
10
|
+
private readonly nodeId;
|
|
11
|
+
private readonly getWorkspace;
|
|
12
|
+
private readonly now;
|
|
13
|
+
private readonly sessions;
|
|
14
|
+
private readonly runs;
|
|
15
|
+
private readonly messageRuns;
|
|
16
|
+
private readonly events;
|
|
17
|
+
private readonly turns;
|
|
18
|
+
private readonly approvals;
|
|
19
|
+
private readonly workspaceEvents;
|
|
20
|
+
/** A Session owns at most one running turn; Workspace sessions may run concurrently. */
|
|
21
|
+
private readonly leases;
|
|
22
|
+
/** Session FIFO metadata is deliberately process-local with the rest of run state. */
|
|
23
|
+
private readonly queuedRuns;
|
|
24
|
+
private readonly queueVersions;
|
|
25
|
+
private readonly pausedQueues;
|
|
26
|
+
private readonly snapshots;
|
|
27
|
+
private nextEventId;
|
|
28
|
+
private nextWorkspaceEventId;
|
|
29
|
+
constructor(nodeId: () => string, getWorkspace: (id: string) => NodeWorkspace | undefined, now?: () => number);
|
|
30
|
+
activeWorkspaceLeaseCount(): number;
|
|
31
|
+
activeSessionRun(sessionId: string): NodeAgentRun | undefined;
|
|
32
|
+
hasActiveSessionLease(sessionId: string): boolean;
|
|
33
|
+
createAgentSession(input: {
|
|
34
|
+
readonly workspaceId: string;
|
|
35
|
+
readonly runner: RunnerName;
|
|
36
|
+
readonly externalSessionId?: string;
|
|
37
|
+
readonly nativeControl?: NativeSessionControl;
|
|
38
|
+
readonly cwd: string;
|
|
39
|
+
readonly title?: string;
|
|
40
|
+
readonly model?: string;
|
|
41
|
+
readonly effort?: string;
|
|
42
|
+
readonly access?: string;
|
|
43
|
+
}): NodeAgentSession;
|
|
44
|
+
/** Internal envelope compatibility for a Run already accepted in this process. */
|
|
45
|
+
adoptRun(input: {
|
|
46
|
+
readonly runId: string;
|
|
47
|
+
readonly sessionId: string;
|
|
48
|
+
readonly workspaceId?: string;
|
|
49
|
+
readonly runner: RunnerName;
|
|
50
|
+
readonly cwd: string;
|
|
51
|
+
readonly clientMessageId?: string;
|
|
52
|
+
readonly externalSessionId?: string;
|
|
53
|
+
}): NodeAgentRun;
|
|
54
|
+
getAgentSession(id: string): NodeAgentSession | undefined;
|
|
55
|
+
listAgentSessions(): readonly NodeAgentSession[];
|
|
56
|
+
findAgentSessionByExternalSession(runner: RunnerName, externalSessionId: string): NodeAgentSession | undefined;
|
|
57
|
+
updateAgentSessionConfiguration(input: {
|
|
58
|
+
readonly sessionId: string;
|
|
59
|
+
readonly model: string;
|
|
60
|
+
readonly effort: string;
|
|
61
|
+
readonly access: string;
|
|
62
|
+
}): NodeAgentSession;
|
|
63
|
+
setNativeControl(id: string, nativeControl: NativeSessionControl): void;
|
|
64
|
+
setExternalSessionId(id: string, externalSessionId: string): void;
|
|
65
|
+
/**
|
|
66
|
+
* Atomically replaces the bootstrap Session key with the Runner-native ID.
|
|
67
|
+
* No caller may migrate the individual runtime maps independently.
|
|
68
|
+
*/
|
|
69
|
+
promoteSessionIdentity(id: string, nativeSessionId: string): NodeAgentSession;
|
|
70
|
+
setChannelToken(id: string, channelToken: string): void;
|
|
71
|
+
updateSessionMetadata(input: {
|
|
72
|
+
readonly sessionId: string;
|
|
73
|
+
readonly customTitle?: string | null;
|
|
74
|
+
readonly pinned?: boolean;
|
|
75
|
+
}): NodeAgentSession;
|
|
76
|
+
/**
|
|
77
|
+
* Give a just-created managed Session a readable local title before the
|
|
78
|
+
* Runner has written its own transcript title. This intentionally never
|
|
79
|
+
* replaces a user rename or a Runner-provided title.
|
|
80
|
+
*/
|
|
81
|
+
setTemporaryTitleIfMissing(id: string, title: string): NodeAgentSession;
|
|
82
|
+
/**
|
|
83
|
+
* Runner/native discovery is authoritative for its own title. Keep it on
|
|
84
|
+
* the managed Session so later `session.get` polls cannot regress to an
|
|
85
|
+
* empty fallback after a discovery response has already shown the title.
|
|
86
|
+
*/
|
|
87
|
+
setRunnerTitleFromNative(id: string, title: string): NodeAgentSession;
|
|
88
|
+
createMessageRun(input: {
|
|
89
|
+
readonly sessionId: string;
|
|
90
|
+
readonly clientMessageId: string;
|
|
91
|
+
readonly content: string;
|
|
92
|
+
readonly attachments?: readonly NodeMessageAttachmentInput[];
|
|
93
|
+
}): {
|
|
94
|
+
readonly run: NodeAgentRun;
|
|
95
|
+
readonly created: boolean;
|
|
96
|
+
};
|
|
97
|
+
sessionQueue(sessionId: string): {
|
|
98
|
+
readonly version: number;
|
|
99
|
+
readonly paused: boolean;
|
|
100
|
+
readonly runIds: readonly string[];
|
|
101
|
+
};
|
|
102
|
+
isQueuedRun(runId: string): boolean;
|
|
103
|
+
claimNextQueuedRun(sessionId: string): NodeAgentRun | undefined;
|
|
104
|
+
pauseSessionQueue(sessionId: string): void;
|
|
105
|
+
resumeSessionQueue(sessionId: string): void;
|
|
106
|
+
reorderSessionQueue(sessionId: string, expectedVersion: number, runIds: readonly string[]): void;
|
|
107
|
+
prioritizeQueuedRun(runId: string): NodeAgentRun;
|
|
108
|
+
updateQueuedMessage(runId: string, content: string): NodeAgentRun;
|
|
109
|
+
deleteQueuedRun(runId: string): void;
|
|
110
|
+
queuedMessageText(runId: string): string;
|
|
111
|
+
getRun(id: string): NodeAgentRun | undefined;
|
|
112
|
+
findMessageRun(sessionId: string, clientMessageId: string): NodeAgentRun | undefined;
|
|
113
|
+
listRunsForSession(sessionId: string): readonly NodeAgentRun[];
|
|
114
|
+
transitionRun(id: string, next: AgentRunStatus): NodeAgentRun;
|
|
115
|
+
appendRunEvent(input: {
|
|
116
|
+
readonly runId: string;
|
|
117
|
+
readonly sequence: number;
|
|
118
|
+
readonly eventType: string;
|
|
119
|
+
readonly payload: unknown;
|
|
120
|
+
readonly status?: AgentRunStatus;
|
|
121
|
+
}): NodeRunEvent | undefined;
|
|
122
|
+
listRunEvents(id: string, after?: number): readonly NodeRunEvent[];
|
|
123
|
+
listConversationTurns(input: {
|
|
124
|
+
readonly sessionId: string;
|
|
125
|
+
readonly cursor?: string;
|
|
126
|
+
readonly limit?: number;
|
|
127
|
+
}): ConversationPage;
|
|
128
|
+
conversationTurnForRun(runId: string): NodeConversationTurn | undefined;
|
|
129
|
+
listConversationItems(input: {
|
|
130
|
+
readonly sessionId: string;
|
|
131
|
+
readonly cursor?: string;
|
|
132
|
+
readonly limit?: number;
|
|
133
|
+
}): ConversationItemPage;
|
|
134
|
+
recordCommandExecution(input: {
|
|
135
|
+
readonly sessionId: string;
|
|
136
|
+
readonly command: string;
|
|
137
|
+
readonly cwd: string;
|
|
138
|
+
readonly outputPreview: string;
|
|
139
|
+
readonly failed?: boolean;
|
|
140
|
+
}): NodeConversationTurn;
|
|
141
|
+
listWorkspaceActivity(input: {
|
|
142
|
+
readonly workspaceId: string;
|
|
143
|
+
readonly cursor?: string;
|
|
144
|
+
readonly limit?: number;
|
|
145
|
+
}): WorkspaceActivityPage;
|
|
146
|
+
createApproval(input: {
|
|
147
|
+
readonly runId: string;
|
|
148
|
+
readonly approvalId: string;
|
|
149
|
+
readonly payload: unknown;
|
|
150
|
+
readonly expiresAt: number;
|
|
151
|
+
}): NodeApproval;
|
|
152
|
+
getApproval(id: string): NodeApproval | undefined;
|
|
153
|
+
listApprovals(runId: string): readonly NodeApproval[];
|
|
154
|
+
expireApproval(id: string): NodeApproval;
|
|
155
|
+
decideApproval(id: string, decision: 'APPROVED' | 'REJECTED'): NodeApproval;
|
|
156
|
+
expireDueApprovals(): readonly NodeApproval[];
|
|
157
|
+
listNonterminalRuns(): readonly NodeAgentRun[];
|
|
158
|
+
lastRunSequence(id: string): number;
|
|
159
|
+
saveSnapshot(id: string, value: unknown): void;
|
|
160
|
+
getSnapshot(id: string): unknown | undefined;
|
|
161
|
+
hasActiveWorkspaceLease(workspaceId: string): boolean;
|
|
162
|
+
/** Removes a terminal Session's process-local projection after its native history is deleted. */
|
|
163
|
+
removeAgentSession(sessionId: string): void;
|
|
164
|
+
removeWorkspace(workspaceId: string): void;
|
|
165
|
+
reconcileRun(id: string, active: boolean): void;
|
|
166
|
+
private replaceSession;
|
|
167
|
+
private bumpQueueVersion;
|
|
168
|
+
private requireSession;
|
|
169
|
+
private requireRun;
|
|
170
|
+
private requireApproval;
|
|
171
|
+
private recordWorkspaceActivity;
|
|
172
|
+
private updateTurnForRun;
|
|
173
|
+
private projectEvent;
|
|
174
|
+
}
|