kimaki 0.26.0 → 0.27.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/dist/agent-model.e2e.test.js +25 -122
- package/dist/anthropic-account-identity.js +1 -0
- package/dist/anthropic-auth-plugin.js +1 -0
- package/dist/anthropic-auth-state.js +1 -0
- package/dist/bin.js +73 -19
- package/dist/cli-commands/maintenance.js +46 -7
- package/dist/cli-commands/send.js +24 -0
- package/dist/cli-commands/session.js +183 -8
- package/dist/cli-commands/task.js +13 -1
- package/dist/cli-parsing.test.js +59 -3
- package/dist/cli-runner.js +28 -4
- package/dist/cli-runner.test.js +27 -0
- package/dist/cli-send-thread.e2e.test.js +1 -1
- package/dist/cli.js +32 -0
- package/dist/commands/abort.js +5 -1
- package/dist/commands/agent.js +6 -8
- package/dist/commands/ask-question.js +21 -32
- package/dist/commands/ask-question.test.js +43 -1
- package/dist/commands/login.js +6 -0
- package/dist/commands/merge-worktree.js +36 -10
- package/dist/commands/model-variant.js +7 -1
- package/dist/commands/model.js +14 -8
- package/dist/commands/multioauth.js +1 -0
- package/dist/commands/new-worktree.js +170 -102
- package/dist/commands/user-command.js +6 -8
- package/dist/commands/worktrees.js +5 -13
- package/dist/config.js +16 -1
- package/dist/database.js +124 -3
- package/dist/db.js +22 -0
- package/dist/db.test.js +167 -1
- package/dist/discord-bot.js +34 -32
- package/dist/discord-command-registration.js +9 -1
- package/dist/discord-utils.js +13 -5
- package/dist/discord-utils.test.js +17 -1
- package/dist/errors.js +5 -0
- package/dist/exec-async.js +17 -5
- package/dist/external-directory-permissions.e2e.test.js +2 -2
- package/dist/file-edit-log.js +197 -0
- package/dist/file-edit-log.test.js +258 -0
- package/dist/format-tables.js +52 -0
- package/dist/format-tables.test.js +93 -0
- package/dist/gateway-proxy-reconnect.e2e.test.js +28 -8
- package/dist/gateway-proxy.e2e.test.js +5 -5
- package/dist/git-worktree-core.js +182 -17
- package/dist/ipc-tools-plugin.js +63 -3
- package/dist/kimaki-opencode-plugin-loading.e2e.test.js +122 -58
- package/dist/kimaki-opencode-plugin.js +7 -3
- package/dist/kimaki-workspace-adaptor.js +33 -10
- package/dist/logger.js +10 -1
- package/dist/markdown.js +3 -1
- package/dist/markdown.test.js +11 -3
- package/dist/message-formatting.js +18 -7
- package/dist/message-formatting.test.js +56 -9
- package/dist/oauth-rotation-shared.js +10 -0
- package/dist/openai-auth-plugin.js +1 -0
- package/dist/openai-auth-state.js +1 -0
- package/dist/opencode-command.test.js +60 -0
- package/dist/opencode.js +53 -12
- package/dist/question-abort-clears-dropdown.e2e.test.js +89 -0
- package/dist/question-text-before-dropdown.e2e.test.js +52 -0
- package/dist/queue-advanced-abort.e2e.test.js +1 -1
- package/dist/queue-advanced-action-buttons.e2e.test.js +3 -3
- package/dist/queue-advanced-e2e-setup.js +78 -5
- package/dist/queue-advanced-footer.e2e.test.js +40 -246
- package/dist/queue-advanced-model-switch.e2e.test.js +2 -2
- package/dist/queue-advanced-permissions-typing.e2e.test.js +7 -9
- package/dist/queue-advanced-question.e2e.test.js +31 -26
- package/dist/queue-advanced-typing-interrupt.e2e.test.js +2 -2
- package/dist/queue-delete-message.e2e.test.js +1 -1
- package/dist/queue-drain-after-interactive-ui.e2e.test.js +2 -2
- package/dist/queue-interrupt-drain.e2e.test.js +112 -2
- package/dist/queue-question-select-drain.e2e.test.js +30 -128
- package/dist/runtime-lifecycle.e2e.test.js +10 -37
- package/dist/schema.js +34 -0
- package/dist/session-handler/event-stream-state.js +267 -50
- package/dist/session-handler/event-stream-state.test.js +415 -8
- package/dist/session-handler/model-utils.js +159 -5
- package/dist/session-handler/model-utils.test.js +345 -0
- package/dist/session-handler/thread-session-runtime.js +252 -182
- package/dist/session-sleep.test.js +224 -0
- package/dist/session-title-rename.test.js +7 -39
- package/dist/sleep.e2e.test.js +491 -0
- package/dist/startup-time.e2e.test.js +2 -1
- package/dist/store.js +3 -0
- package/dist/system-message.js +59 -6
- package/dist/system-message.test.js +76 -6
- package/dist/task-runner.js +94 -4
- package/dist/task-schedule.js +56 -0
- package/dist/task-schedule.test.js +71 -1
- package/dist/thread-message-queue.e2e.test.js +10 -169
- package/dist/undo-redo.e2e.test.js +2 -2
- package/dist/voice-attachment.js +23 -1
- package/dist/voice-message.e2e.test.js +10 -10
- package/dist/voice.test.js +55 -29
- package/dist/worktree-clone-isolation.e2e.test.js +188 -0
- package/dist/worktree-lifecycle.e2e.test.js +29 -10
- package/dist/worktrees.js +226 -443
- package/dist/worktrees.test.js +435 -58
- package/dist/xai-auth-plugin.js +1 -0
- package/dist/xai-auth-state.js +1 -0
- package/package.json +8 -6
- package/skills/errore/SKILL.md +18 -0
- package/skills/goke/SKILL.md +150 -10
- package/skills/holocron/SKILL.md +37 -0
- package/skills/npm-package/SKILL.md +55 -35
- package/skills/profano/SKILL.md +17 -2
- package/skills/spiceflow/SKILL.md +39 -3
- package/skills/strada/SKILL.md +39 -0
- package/skills/zele/SKILL.md +19 -0
- package/src/agent-model.e2e.test.ts +25 -151
- package/src/anthropic-account-identity.ts +1 -0
- package/src/anthropic-auth-plugin.ts +1 -0
- package/src/anthropic-auth-state.ts +1 -0
- package/src/bin.ts +78 -26
- package/src/cli-commands/maintenance.ts +59 -13
- package/src/cli-commands/send.ts +25 -0
- package/src/cli-commands/session.ts +203 -9
- package/src/cli-commands/task.ts +13 -1
- package/src/cli-parsing.test.ts +69 -3
- package/src/cli-runner.test.ts +49 -0
- package/src/cli-runner.ts +28 -4
- package/src/cli-send-thread.e2e.test.ts +1 -1
- package/src/cli.ts +53 -0
- package/src/commands/abort.ts +6 -1
- package/src/commands/agent.ts +6 -6
- package/src/commands/ask-question.test.ts +48 -1
- package/src/commands/ask-question.ts +21 -32
- package/src/commands/login.ts +6 -0
- package/src/commands/merge-worktree.ts +43 -12
- package/src/commands/model-variant.ts +8 -1
- package/src/commands/model.ts +18 -8
- package/src/commands/multioauth.ts +1 -0
- package/src/commands/new-worktree.ts +138 -55
- package/src/commands/user-command.ts +6 -6
- package/src/commands/worktrees.ts +5 -12
- package/src/config.ts +18 -1
- package/src/database.ts +195 -3
- package/src/db.test.ts +188 -0
- package/src/db.ts +26 -0
- package/src/discord-bot.ts +35 -29
- package/src/discord-command-registration.ts +15 -1
- package/src/discord-utils.test.ts +19 -0
- package/src/discord-utils.ts +21 -6
- package/src/errors.ts +5 -0
- package/src/exec-async.ts +20 -11
- package/src/external-directory-permissions.e2e.test.ts +2 -2
- package/src/file-edit-log.test.ts +287 -0
- package/src/file-edit-log.ts +283 -0
- package/src/format-tables.test.ts +101 -0
- package/src/format-tables.ts +75 -0
- package/src/gateway-proxy-reconnect.e2e.test.ts +38 -8
- package/src/gateway-proxy.e2e.test.ts +5 -5
- package/src/git-worktree-core.ts +247 -20
- package/src/ipc-tools-plugin.ts +75 -5
- package/src/kimaki-opencode-plugin-loading.e2e.test.ts +188 -97
- package/src/kimaki-opencode-plugin.ts +7 -3
- package/src/kimaki-workspace-adaptor.ts +40 -11
- package/src/logger.ts +13 -4
- package/src/markdown.test.ts +13 -3
- package/src/markdown.ts +4 -1
- package/src/message-formatting.test.ts +86 -12
- package/src/message-formatting.ts +19 -7
- package/src/oauth-rotation-shared.ts +10 -0
- package/src/openai-auth-plugin.ts +1 -0
- package/src/openai-auth-state.ts +1 -0
- package/src/opencode-command.test.ts +81 -0
- package/src/opencode.ts +71 -11
- package/src/question-abort-clears-dropdown.e2e.test.ts +109 -0
- package/src/question-text-before-dropdown.e2e.test.ts +67 -0
- package/src/queue-advanced-abort.e2e.test.ts +1 -1
- package/src/queue-advanced-action-buttons.e2e.test.ts +3 -3
- package/src/queue-advanced-e2e-setup.ts +83 -4
- package/src/queue-advanced-footer.e2e.test.ts +47 -294
- package/src/queue-advanced-model-switch.e2e.test.ts +2 -2
- package/src/queue-advanced-permissions-typing.e2e.test.ts +7 -9
- package/src/queue-advanced-question.e2e.test.ts +35 -29
- package/src/queue-advanced-typing-interrupt.e2e.test.ts +2 -2
- package/src/queue-delete-message.e2e.test.ts +1 -1
- package/src/queue-drain-after-interactive-ui.e2e.test.ts +2 -2
- package/src/queue-interrupt-drain.e2e.test.ts +123 -2
- package/src/queue-question-select-drain.e2e.test.ts +32 -157
- package/src/runtime-lifecycle.e2e.test.ts +10 -41
- package/src/schema.sql +14 -1
- package/src/schema.ts +36 -0
- package/src/session-handler/event-stream-state.test.ts +437 -7
- package/src/session-handler/event-stream-state.ts +389 -53
- package/src/session-handler/model-utils.test.ts +415 -0
- package/src/session-handler/model-utils.ts +249 -5
- package/src/session-handler/thread-session-runtime.ts +300 -215
- package/src/session-sleep.test.ts +294 -0
- package/src/session-title-rename.test.ts +9 -48
- package/src/sleep.e2e.test.ts +562 -0
- package/src/startup-time.e2e.test.ts +5 -1
- package/src/store.ts +21 -0
- package/src/system-message.test.ts +86 -6
- package/src/system-message.ts +66 -7
- package/src/task-runner.ts +136 -2
- package/src/task-schedule.test.ts +76 -0
- package/src/task-schedule.ts +95 -0
- package/src/thread-message-queue.e2e.test.ts +10 -202
- package/src/undo-redo.e2e.test.ts +2 -2
- package/src/voice-attachment.ts +29 -1
- package/src/voice-message.e2e.test.ts +10 -10
- package/src/voice.test.ts +63 -29
- package/src/worktree-clone-isolation.e2e.test.ts +213 -0
- package/src/worktree-lifecycle.e2e.test.ts +29 -9
- package/src/worktrees.test.ts +505 -64
- package/src/worktrees.ts +257 -603
- package/src/xai-auth-plugin.ts +1 -0
- package/src/xai-auth-state.ts +1 -0
- package/dist/message-finish-field.e2e.test.js +0 -169
- package/dist/subagent-rate-limit-plugin.js +0 -184
- package/src/message-finish-field.e2e.test.ts +0 -196
- package/src/subagent-rate-limit-plugin.ts +0 -228
|
@@ -4,7 +4,7 @@ import fs from 'node:fs';
|
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
import { describe, expect, test } from 'vitest';
|
|
6
6
|
import {} from './opencode-session-event-log.js';
|
|
7
|
-
import { derivePendingPermissionRequests, getAssistantMessageIdsForLatestUserTurn, getDerivedSubagentSessions, getEventBufferSessionId, getCurrentTurnStartTime, getDerivedSubtaskIndex, getLatestAssistantMessageIdForLatestUserTurn, getLatestRunInfo, getLatestTurnTokenUsage, getIdleTokenUsageDelta, hasAssistantMessageCompletedBefore, doesLatestUserTurnHaveNaturalCompletion, isAssistantMessageInLatestUserTurn, isAssistantMessageNaturalCompletion, isSessionBusy, } from './event-stream-state.js';
|
|
7
|
+
import { derivePendingPermissionRequests, getAssistantMessageIdsForLatestUserTurn, getDerivedSubagentSessions, getEventBufferSessionId, getCurrentTurnStartTime, getDerivedSubtaskIndex, getLatestAssistantMessageIdForLatestUserTurn, getLatestRunInfo, getLatestTurnTokenUsage, getIdleTokenUsageDelta, getTokenUsageSessionIdsForIdle, isDerivedChildSession, hasAssistantMessageCompletedBefore, doesLatestUserTurnHaveNaturalCompletion, isAssistantMessageInLatestUserTurn, isAssistantMessageNaturalCompletion, isSessionBusy, isAssistantTextReadyForQuestion, deriveLatestUnansweredQuestion, } from './event-stream-state.js';
|
|
8
8
|
const fixturesDir = path.join(import.meta.dirname, 'event-stream-fixtures');
|
|
9
9
|
function loadFixture(filename) {
|
|
10
10
|
const content = fs.readFileSync(path.join(fixturesDir, filename), 'utf8');
|
|
@@ -983,7 +983,7 @@ describe('getLatestTurnTokenUsage', () => {
|
|
|
983
983
|
assistantMessageCount: 2,
|
|
984
984
|
});
|
|
985
985
|
});
|
|
986
|
-
test('
|
|
986
|
+
test('sums assistant tokens when the session has no user message', () => {
|
|
987
987
|
const sessionId = 'ses_no_user';
|
|
988
988
|
const events = [
|
|
989
989
|
assistantEvent({
|
|
@@ -1000,16 +1000,16 @@ describe('getLatestTurnTokenUsage', () => {
|
|
|
1000
1000
|
}),
|
|
1001
1001
|
];
|
|
1002
1002
|
expect(getLatestTurnTokenUsage({ events, sessionId })).toEqual({
|
|
1003
|
-
input:
|
|
1004
|
-
output:
|
|
1003
|
+
input: 100,
|
|
1004
|
+
output: 10,
|
|
1005
1005
|
reasoning: 0,
|
|
1006
1006
|
cacheRead: 0,
|
|
1007
1007
|
cacheWrite: 0,
|
|
1008
|
-
total:
|
|
1008
|
+
total: 110,
|
|
1009
1009
|
cost: 0,
|
|
1010
|
-
model:
|
|
1011
|
-
providerID:
|
|
1012
|
-
assistantMessageCount:
|
|
1010
|
+
model: 'gpt-5.3-codex',
|
|
1011
|
+
providerID: 'openai',
|
|
1012
|
+
assistantMessageCount: 1,
|
|
1013
1013
|
userMessageId: undefined,
|
|
1014
1014
|
});
|
|
1015
1015
|
});
|
|
@@ -1176,4 +1176,411 @@ describe('getIdleTokenUsageDelta', () => {
|
|
|
1176
1176
|
total: 20,
|
|
1177
1177
|
});
|
|
1178
1178
|
});
|
|
1179
|
+
test('counts child session assistant tokens without a user message.updated', () => {
|
|
1180
|
+
const childSessionId = 'ses_task_child';
|
|
1181
|
+
const events = [
|
|
1182
|
+
eventEntry({
|
|
1183
|
+
type: 'message.updated',
|
|
1184
|
+
properties: {
|
|
1185
|
+
sessionID: childSessionId,
|
|
1186
|
+
info: {
|
|
1187
|
+
id: 'msg_child_asst_1',
|
|
1188
|
+
sessionID: childSessionId,
|
|
1189
|
+
role: 'assistant',
|
|
1190
|
+
time: { created: 2, completed: 3 },
|
|
1191
|
+
parentID: 'msg_child_user_missing',
|
|
1192
|
+
modelID: 'gpt-5.3-codex',
|
|
1193
|
+
providerID: 'openai',
|
|
1194
|
+
mode: 'general',
|
|
1195
|
+
agent: 'general',
|
|
1196
|
+
path: { cwd: '/test', root: '/test' },
|
|
1197
|
+
cost: 0.02,
|
|
1198
|
+
tokens: {
|
|
1199
|
+
total: 40,
|
|
1200
|
+
input: 30,
|
|
1201
|
+
output: 10,
|
|
1202
|
+
reasoning: 4,
|
|
1203
|
+
cache: { read: 0, write: 0 },
|
|
1204
|
+
},
|
|
1205
|
+
finish: 'stop',
|
|
1206
|
+
},
|
|
1207
|
+
},
|
|
1208
|
+
}),
|
|
1209
|
+
idleEvent(childSessionId),
|
|
1210
|
+
];
|
|
1211
|
+
expect(getIdleTokenUsageDelta({
|
|
1212
|
+
events,
|
|
1213
|
+
sessionId: childSessionId,
|
|
1214
|
+
idleEventIndex: events.length - 1,
|
|
1215
|
+
})).toMatchObject({
|
|
1216
|
+
input: 30,
|
|
1217
|
+
output: 10,
|
|
1218
|
+
reasoning: 4,
|
|
1219
|
+
total: 40,
|
|
1220
|
+
cost: 0.02,
|
|
1221
|
+
assistantMessageCount: 1,
|
|
1222
|
+
});
|
|
1223
|
+
});
|
|
1224
|
+
test('falls back to session.updated Session.tokens for a child with no message.updated', () => {
|
|
1225
|
+
const childSessionId = 'ses_task_child_session_tokens';
|
|
1226
|
+
const events = [
|
|
1227
|
+
eventEntry({
|
|
1228
|
+
type: 'session.updated',
|
|
1229
|
+
properties: {
|
|
1230
|
+
sessionID: childSessionId,
|
|
1231
|
+
info: {
|
|
1232
|
+
id: childSessionId,
|
|
1233
|
+
slug: 'child',
|
|
1234
|
+
projectID: 'prj_1',
|
|
1235
|
+
directory: '/test',
|
|
1236
|
+
parentID: 'ses_main',
|
|
1237
|
+
title: 'child task',
|
|
1238
|
+
version: '1',
|
|
1239
|
+
cost: 0.05,
|
|
1240
|
+
tokens: {
|
|
1241
|
+
input: 100,
|
|
1242
|
+
output: 20,
|
|
1243
|
+
reasoning: 8,
|
|
1244
|
+
cache: { read: 4, write: 1 },
|
|
1245
|
+
},
|
|
1246
|
+
time: { created: 1, updated: 2 },
|
|
1247
|
+
},
|
|
1248
|
+
},
|
|
1249
|
+
}),
|
|
1250
|
+
idleEvent(childSessionId),
|
|
1251
|
+
];
|
|
1252
|
+
expect(getIdleTokenUsageDelta({
|
|
1253
|
+
events,
|
|
1254
|
+
sessionId: childSessionId,
|
|
1255
|
+
idleEventIndex: events.length - 1,
|
|
1256
|
+
})).toMatchObject({
|
|
1257
|
+
input: 100,
|
|
1258
|
+
output: 20,
|
|
1259
|
+
reasoning: 8,
|
|
1260
|
+
cacheRead: 4,
|
|
1261
|
+
cacheWrite: 1,
|
|
1262
|
+
total: 133,
|
|
1263
|
+
cost: 0.05,
|
|
1264
|
+
});
|
|
1265
|
+
});
|
|
1266
|
+
test('does not re-emit child tokens at parent idle after the child already idled', () => {
|
|
1267
|
+
const childSessionId = 'ses_task_child_dedupe';
|
|
1268
|
+
const events = [
|
|
1269
|
+
eventEntry({
|
|
1270
|
+
type: 'message.updated',
|
|
1271
|
+
properties: {
|
|
1272
|
+
sessionID: childSessionId,
|
|
1273
|
+
info: {
|
|
1274
|
+
id: 'msg_child_asst_1',
|
|
1275
|
+
sessionID: childSessionId,
|
|
1276
|
+
role: 'assistant',
|
|
1277
|
+
time: { created: 2, completed: 3 },
|
|
1278
|
+
parentID: 'msg_child_user_missing',
|
|
1279
|
+
modelID: 'gpt-5.3-codex',
|
|
1280
|
+
providerID: 'openai',
|
|
1281
|
+
mode: 'general',
|
|
1282
|
+
agent: 'general',
|
|
1283
|
+
path: { cwd: '/test', root: '/test' },
|
|
1284
|
+
cost: 0,
|
|
1285
|
+
tokens: {
|
|
1286
|
+
total: 40,
|
|
1287
|
+
input: 30,
|
|
1288
|
+
output: 10,
|
|
1289
|
+
reasoning: 0,
|
|
1290
|
+
cache: { read: 0, write: 0 },
|
|
1291
|
+
},
|
|
1292
|
+
finish: 'stop',
|
|
1293
|
+
},
|
|
1294
|
+
},
|
|
1295
|
+
}),
|
|
1296
|
+
idleEvent(childSessionId),
|
|
1297
|
+
idleEvent('ses_main'),
|
|
1298
|
+
];
|
|
1299
|
+
expect(getIdleTokenUsageDelta({
|
|
1300
|
+
events,
|
|
1301
|
+
sessionId: childSessionId,
|
|
1302
|
+
idleEventIndex: 1,
|
|
1303
|
+
})).toMatchObject({ total: 40 });
|
|
1304
|
+
expect(getIdleTokenUsageDelta({
|
|
1305
|
+
events,
|
|
1306
|
+
sessionId: childSessionId,
|
|
1307
|
+
idleEventIndex: 2,
|
|
1308
|
+
})).toBeUndefined();
|
|
1309
|
+
});
|
|
1310
|
+
});
|
|
1311
|
+
describe('task child session token tracking', () => {
|
|
1312
|
+
function idleEvent(sessionId) {
|
|
1313
|
+
return eventEntry({
|
|
1314
|
+
type: 'session.idle',
|
|
1315
|
+
properties: { sessionID: sessionId },
|
|
1316
|
+
});
|
|
1317
|
+
}
|
|
1318
|
+
test('isDerivedChildSession is true from session.created parentID before task metadata', () => {
|
|
1319
|
+
const mainSessionId = 'ses_main';
|
|
1320
|
+
const childSessionId = 'ses_child';
|
|
1321
|
+
const events = [
|
|
1322
|
+
eventEntry({
|
|
1323
|
+
type: 'session.created',
|
|
1324
|
+
properties: {
|
|
1325
|
+
sessionID: childSessionId,
|
|
1326
|
+
info: {
|
|
1327
|
+
id: childSessionId,
|
|
1328
|
+
slug: 'child',
|
|
1329
|
+
projectID: 'prj_1',
|
|
1330
|
+
directory: '/test',
|
|
1331
|
+
parentID: mainSessionId,
|
|
1332
|
+
title: 'explore files',
|
|
1333
|
+
version: '1',
|
|
1334
|
+
time: { created: 1, updated: 1 },
|
|
1335
|
+
},
|
|
1336
|
+
},
|
|
1337
|
+
}),
|
|
1338
|
+
];
|
|
1339
|
+
expect(isDerivedChildSession({
|
|
1340
|
+
events,
|
|
1341
|
+
mainSessionId,
|
|
1342
|
+
candidateSessionId: childSessionId,
|
|
1343
|
+
})).toBe(true);
|
|
1344
|
+
expect(isDerivedChildSession({
|
|
1345
|
+
events,
|
|
1346
|
+
mainSessionId,
|
|
1347
|
+
candidateSessionId: 'ses_unrelated',
|
|
1348
|
+
})).toBe(false);
|
|
1349
|
+
});
|
|
1350
|
+
test('main idle also returns child session ids so their tokens can be tracked', () => {
|
|
1351
|
+
const mainSessionId = 'ses_main';
|
|
1352
|
+
const childSessionId = 'ses_child';
|
|
1353
|
+
const events = [
|
|
1354
|
+
eventEntry({
|
|
1355
|
+
type: 'session.created',
|
|
1356
|
+
properties: {
|
|
1357
|
+
sessionID: childSessionId,
|
|
1358
|
+
info: {
|
|
1359
|
+
id: childSessionId,
|
|
1360
|
+
slug: 'child',
|
|
1361
|
+
projectID: 'prj_1',
|
|
1362
|
+
directory: '/test',
|
|
1363
|
+
parentID: mainSessionId,
|
|
1364
|
+
title: 'child task',
|
|
1365
|
+
version: '1',
|
|
1366
|
+
time: { created: 1, updated: 1 },
|
|
1367
|
+
},
|
|
1368
|
+
},
|
|
1369
|
+
}),
|
|
1370
|
+
eventEntry({
|
|
1371
|
+
type: 'message.part.updated',
|
|
1372
|
+
properties: {
|
|
1373
|
+
part: {
|
|
1374
|
+
id: 'prt_task',
|
|
1375
|
+
sessionID: mainSessionId,
|
|
1376
|
+
messageID: 'msg_asst',
|
|
1377
|
+
type: 'tool',
|
|
1378
|
+
callID: 'call_task',
|
|
1379
|
+
tool: 'task',
|
|
1380
|
+
state: {
|
|
1381
|
+
status: 'running',
|
|
1382
|
+
input: { subagent_type: 'general' },
|
|
1383
|
+
metadata: { sessionId: childSessionId },
|
|
1384
|
+
},
|
|
1385
|
+
},
|
|
1386
|
+
},
|
|
1387
|
+
}),
|
|
1388
|
+
idleEvent(mainSessionId),
|
|
1389
|
+
];
|
|
1390
|
+
expect(getTokenUsageSessionIdsForIdle({
|
|
1391
|
+
events,
|
|
1392
|
+
mainSessionId,
|
|
1393
|
+
idleSessionId: mainSessionId,
|
|
1394
|
+
})).toEqual([mainSessionId, childSessionId]);
|
|
1395
|
+
expect(getTokenUsageSessionIdsForIdle({
|
|
1396
|
+
events,
|
|
1397
|
+
mainSessionId,
|
|
1398
|
+
idleSessionId: childSessionId,
|
|
1399
|
+
})).toEqual([childSessionId]);
|
|
1400
|
+
});
|
|
1401
|
+
});
|
|
1402
|
+
describe('question waits for preceding text-end', () => {
|
|
1403
|
+
const sessionId = 'ses_question_text';
|
|
1404
|
+
const messageId = 'msg_asst_question';
|
|
1405
|
+
const questionId = 'que_1';
|
|
1406
|
+
const textStart = eventEntry({
|
|
1407
|
+
type: 'message.part.updated',
|
|
1408
|
+
properties: {
|
|
1409
|
+
sessionID: sessionId,
|
|
1410
|
+
part: {
|
|
1411
|
+
id: 'prt_text',
|
|
1412
|
+
sessionID: sessionId,
|
|
1413
|
+
messageID: messageId,
|
|
1414
|
+
type: 'text',
|
|
1415
|
+
text: '',
|
|
1416
|
+
time: { start: 1 },
|
|
1417
|
+
},
|
|
1418
|
+
},
|
|
1419
|
+
});
|
|
1420
|
+
const questionAsked = eventEntry({
|
|
1421
|
+
type: 'question.asked',
|
|
1422
|
+
properties: {
|
|
1423
|
+
id: questionId,
|
|
1424
|
+
sessionID: sessionId,
|
|
1425
|
+
questions: [{
|
|
1426
|
+
question: 'What next?',
|
|
1427
|
+
header: 'Next step',
|
|
1428
|
+
options: [
|
|
1429
|
+
{ label: 'Commit', description: 'Commit these files' },
|
|
1430
|
+
],
|
|
1431
|
+
}],
|
|
1432
|
+
tool: {
|
|
1433
|
+
messageID: messageId,
|
|
1434
|
+
callID: 'call_question',
|
|
1435
|
+
},
|
|
1436
|
+
},
|
|
1437
|
+
});
|
|
1438
|
+
const textEnd = eventEntry({
|
|
1439
|
+
type: 'message.part.updated',
|
|
1440
|
+
properties: {
|
|
1441
|
+
sessionID: sessionId,
|
|
1442
|
+
part: {
|
|
1443
|
+
id: 'prt_text',
|
|
1444
|
+
sessionID: sessionId,
|
|
1445
|
+
messageID: messageId,
|
|
1446
|
+
type: 'text',
|
|
1447
|
+
text: 'Done callout',
|
|
1448
|
+
time: { start: 1, end: 2 },
|
|
1449
|
+
},
|
|
1450
|
+
},
|
|
1451
|
+
});
|
|
1452
|
+
const questionError = eventEntry({
|
|
1453
|
+
type: 'message.part.updated',
|
|
1454
|
+
properties: {
|
|
1455
|
+
sessionID: sessionId,
|
|
1456
|
+
part: {
|
|
1457
|
+
id: 'prt_question',
|
|
1458
|
+
sessionID: sessionId,
|
|
1459
|
+
messageID: messageId,
|
|
1460
|
+
type: 'tool',
|
|
1461
|
+
tool: 'question',
|
|
1462
|
+
callID: 'call_question',
|
|
1463
|
+
state: {
|
|
1464
|
+
status: 'error',
|
|
1465
|
+
error: 'Aborted',
|
|
1466
|
+
time: { start: 2, end: 3 },
|
|
1467
|
+
},
|
|
1468
|
+
},
|
|
1469
|
+
},
|
|
1470
|
+
});
|
|
1471
|
+
test('text is not ready when question.asked arrives before time.end', () => {
|
|
1472
|
+
const events = [textStart, questionAsked];
|
|
1473
|
+
expect(isAssistantTextReadyForQuestion({
|
|
1474
|
+
events,
|
|
1475
|
+
sessionId,
|
|
1476
|
+
messageId,
|
|
1477
|
+
})).toBe(false);
|
|
1478
|
+
expect(deriveLatestUnansweredQuestion({
|
|
1479
|
+
events,
|
|
1480
|
+
sessionId,
|
|
1481
|
+
})).toMatchObject({
|
|
1482
|
+
id: questionId,
|
|
1483
|
+
tool: { messageID: messageId },
|
|
1484
|
+
});
|
|
1485
|
+
});
|
|
1486
|
+
test('text is ready after time.end, and still unanswered', () => {
|
|
1487
|
+
const events = [textStart, questionAsked, textEnd];
|
|
1488
|
+
expect(isAssistantTextReadyForQuestion({
|
|
1489
|
+
events,
|
|
1490
|
+
sessionId,
|
|
1491
|
+
messageId,
|
|
1492
|
+
})).toBe(true);
|
|
1493
|
+
expect(deriveLatestUnansweredQuestion({
|
|
1494
|
+
events,
|
|
1495
|
+
sessionId,
|
|
1496
|
+
})?.id).toBe(questionId);
|
|
1497
|
+
});
|
|
1498
|
+
test('no text part means the question is ready immediately', () => {
|
|
1499
|
+
const events = [questionAsked];
|
|
1500
|
+
expect(isAssistantTextReadyForQuestion({
|
|
1501
|
+
events,
|
|
1502
|
+
sessionId,
|
|
1503
|
+
messageId,
|
|
1504
|
+
})).toBe(true);
|
|
1505
|
+
});
|
|
1506
|
+
test('aborted question is not unanswered', () => {
|
|
1507
|
+
const events = [textStart, questionAsked, textEnd, questionError];
|
|
1508
|
+
expect(deriveLatestUnansweredQuestion({
|
|
1509
|
+
events,
|
|
1510
|
+
sessionId,
|
|
1511
|
+
})).toBeUndefined();
|
|
1512
|
+
});
|
|
1513
|
+
test('question from an older user turn is not unanswered', () => {
|
|
1514
|
+
const firstUser = eventEntry({
|
|
1515
|
+
type: 'message.updated',
|
|
1516
|
+
properties: {
|
|
1517
|
+
sessionID: sessionId,
|
|
1518
|
+
info: {
|
|
1519
|
+
id: 'msg_user_first',
|
|
1520
|
+
sessionID: sessionId,
|
|
1521
|
+
role: 'user',
|
|
1522
|
+
time: { created: 1 },
|
|
1523
|
+
agent: 'build',
|
|
1524
|
+
model: {
|
|
1525
|
+
providerID: 'deterministic-provider',
|
|
1526
|
+
modelID: 'deterministic-v2',
|
|
1527
|
+
},
|
|
1528
|
+
},
|
|
1529
|
+
},
|
|
1530
|
+
});
|
|
1531
|
+
const questionAssistant = eventEntry({
|
|
1532
|
+
type: 'message.updated',
|
|
1533
|
+
properties: {
|
|
1534
|
+
sessionID: sessionId,
|
|
1535
|
+
info: {
|
|
1536
|
+
id: messageId,
|
|
1537
|
+
sessionID: sessionId,
|
|
1538
|
+
role: 'assistant',
|
|
1539
|
+
time: { created: 2 },
|
|
1540
|
+
parentID: 'msg_user_first',
|
|
1541
|
+
modelID: 'deterministic-v2',
|
|
1542
|
+
providerID: 'deterministic-provider',
|
|
1543
|
+
mode: 'build',
|
|
1544
|
+
agent: 'build',
|
|
1545
|
+
path: { cwd: '/test', root: '/test' },
|
|
1546
|
+
cost: 0,
|
|
1547
|
+
tokens: {
|
|
1548
|
+
input: 1,
|
|
1549
|
+
output: 1,
|
|
1550
|
+
reasoning: 0,
|
|
1551
|
+
cache: { read: 0, write: 0 },
|
|
1552
|
+
},
|
|
1553
|
+
},
|
|
1554
|
+
},
|
|
1555
|
+
});
|
|
1556
|
+
const nextUser = eventEntry({
|
|
1557
|
+
type: 'message.updated',
|
|
1558
|
+
properties: {
|
|
1559
|
+
sessionID: sessionId,
|
|
1560
|
+
info: {
|
|
1561
|
+
id: 'msg_user_commit',
|
|
1562
|
+
sessionID: sessionId,
|
|
1563
|
+
role: 'user',
|
|
1564
|
+
time: { created: 3 },
|
|
1565
|
+
agent: 'build',
|
|
1566
|
+
model: {
|
|
1567
|
+
providerID: 'deterministic-provider',
|
|
1568
|
+
modelID: 'deterministic-v2',
|
|
1569
|
+
},
|
|
1570
|
+
},
|
|
1571
|
+
},
|
|
1572
|
+
});
|
|
1573
|
+
const events = [
|
|
1574
|
+
firstUser,
|
|
1575
|
+
questionAssistant,
|
|
1576
|
+
textStart,
|
|
1577
|
+
questionAsked,
|
|
1578
|
+
textEnd,
|
|
1579
|
+
nextUser,
|
|
1580
|
+
];
|
|
1581
|
+
expect(deriveLatestUnansweredQuestion({
|
|
1582
|
+
events,
|
|
1583
|
+
sessionId,
|
|
1584
|
+
})).toBeUndefined();
|
|
1585
|
+
});
|
|
1179
1586
|
});
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
// Model resolution utilities.
|
|
2
2
|
// getDefaultModel resolves the default model from OpenCode when no user preference is set.
|
|
3
|
+
// listModels wraps provider.list() with a per-directory memoized cache.
|
|
3
4
|
import fs from 'node:fs';
|
|
4
5
|
import path from 'node:path';
|
|
5
6
|
import { xdgState } from 'xdg-basedir';
|
|
6
7
|
import * as errore from 'errore';
|
|
7
|
-
import { OpenCodeSdkError } from '../errors.js';
|
|
8
|
-
import {} from '../opencode.js';
|
|
8
|
+
import { InvalidModelError, OpenCodeSdkError } from '../errors.js';
|
|
9
|
+
import { initializeOpencodeForDirectory, subscribeOpencodeServerLifecycle, } from '../opencode.js';
|
|
9
10
|
import { createLogger, LogPrefix } from '../logger.js';
|
|
10
11
|
const sessionLogger = createLogger(LogPrefix.SESSION);
|
|
11
12
|
/**
|
|
@@ -34,7 +35,7 @@ function getRecentModelsFromTuiState() {
|
|
|
34
35
|
/**
|
|
35
36
|
* Parse a model string in format "provider/model" into providerID and modelID.
|
|
36
37
|
*/
|
|
37
|
-
function
|
|
38
|
+
export function parseModelId(model) {
|
|
38
39
|
const [providerID, ...modelParts] = model.split('/');
|
|
39
40
|
const modelID = modelParts.join('/');
|
|
40
41
|
if (!providerID || !modelID) {
|
|
@@ -53,7 +54,7 @@ function getModelFromProjectConfig({ directory, }) {
|
|
|
53
54
|
if (!parsed.model) {
|
|
54
55
|
return undefined;
|
|
55
56
|
}
|
|
56
|
-
return
|
|
57
|
+
return parseModelId(parsed.model);
|
|
57
58
|
});
|
|
58
59
|
if (result instanceof Error)
|
|
59
60
|
return undefined;
|
|
@@ -70,6 +71,159 @@ function isModelValid(model, connected, providers) {
|
|
|
70
71
|
const modelExists = provider?.models && model.modelID in provider.models;
|
|
71
72
|
return isConnected && !!modelExists;
|
|
72
73
|
}
|
|
74
|
+
/** SDK display name from provider.list. Subrouter sets this to `build (claude-opus-4-6)`. */
|
|
75
|
+
export function getProviderModelName({ providers, providerID, modelID, }) {
|
|
76
|
+
if (!providerID || !modelID)
|
|
77
|
+
return undefined;
|
|
78
|
+
const provider = providers.find((p) => p.id === providerID);
|
|
79
|
+
const model = provider?.models?.[modelID];
|
|
80
|
+
if (!model)
|
|
81
|
+
return undefined;
|
|
82
|
+
if (typeof model.name === 'string' && model.name.trim())
|
|
83
|
+
return model.name;
|
|
84
|
+
return modelID;
|
|
85
|
+
}
|
|
86
|
+
/** Use the SDK name only when it is the model id, or the model id plus `(live-model)`. */
|
|
87
|
+
export function displayedModelLabel({ modelID, name, }) {
|
|
88
|
+
if (name && (name === modelID || name.startsWith(`${modelID} (`)))
|
|
89
|
+
return name;
|
|
90
|
+
return modelID;
|
|
91
|
+
}
|
|
92
|
+
export function formatDisplayedModelId({ providerID, modelID, name, }) {
|
|
93
|
+
if (!providerID || !modelID)
|
|
94
|
+
return undefined;
|
|
95
|
+
return `${providerID}/${displayedModelLabel({ modelID, name })}`;
|
|
96
|
+
}
|
|
97
|
+
export function resolveDisplayedModelId({ providers, providerID, modelID, }) {
|
|
98
|
+
return formatDisplayedModelId({
|
|
99
|
+
providerID,
|
|
100
|
+
modelID,
|
|
101
|
+
name: getProviderModelName({ providers, providerID, modelID }),
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
const modelListCache = new Map();
|
|
105
|
+
export function clearModelListCache() {
|
|
106
|
+
modelListCache.clear();
|
|
107
|
+
}
|
|
108
|
+
subscribeOpencodeServerLifecycle(() => {
|
|
109
|
+
clearModelListCache();
|
|
110
|
+
});
|
|
111
|
+
function flattenProviderModels({ providers, connected, }) {
|
|
112
|
+
const connectedSet = new Set(connected);
|
|
113
|
+
const models = [];
|
|
114
|
+
for (const provider of providers) {
|
|
115
|
+
const isConnected = connectedSet.has(provider.id);
|
|
116
|
+
for (const [modelID, model] of Object.entries(provider.models)) {
|
|
117
|
+
models.push({
|
|
118
|
+
providerID: provider.id,
|
|
119
|
+
modelID,
|
|
120
|
+
name: model.name || modelID,
|
|
121
|
+
connected: isConnected,
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
return models;
|
|
126
|
+
}
|
|
127
|
+
export async function listModels({ getClient, directory, }) {
|
|
128
|
+
const cacheKey = directory ?? '';
|
|
129
|
+
const cached = modelListCache.get(cacheKey);
|
|
130
|
+
if (cached?.status === 'ready')
|
|
131
|
+
return cached.value;
|
|
132
|
+
if (cached?.status === 'pending')
|
|
133
|
+
return cached.promise;
|
|
134
|
+
const promise = (async () => {
|
|
135
|
+
const providersResponse = await getClient()
|
|
136
|
+
.provider.list({ directory })
|
|
137
|
+
.catch((e) => new OpenCodeSdkError({ operation: 'provider.list', cause: e }));
|
|
138
|
+
if (providersResponse instanceof Error)
|
|
139
|
+
return providersResponse;
|
|
140
|
+
if (!providersResponse.data) {
|
|
141
|
+
return new OpenCodeSdkError({ operation: 'provider.list' });
|
|
142
|
+
}
|
|
143
|
+
return flattenProviderModels({
|
|
144
|
+
providers: providersResponse.data.all,
|
|
145
|
+
connected: providersResponse.data.connected,
|
|
146
|
+
});
|
|
147
|
+
})();
|
|
148
|
+
modelListCache.set(cacheKey, { status: 'pending', promise });
|
|
149
|
+
const result = await promise;
|
|
150
|
+
const current = modelListCache.get(cacheKey);
|
|
151
|
+
const ownsCacheEntry = current?.status === 'pending' && current.promise === promise;
|
|
152
|
+
if (result instanceof Error) {
|
|
153
|
+
if (ownsCacheEntry)
|
|
154
|
+
modelListCache.delete(cacheKey);
|
|
155
|
+
return result;
|
|
156
|
+
}
|
|
157
|
+
if (ownsCacheEntry)
|
|
158
|
+
modelListCache.set(cacheKey, { status: 'ready', value: result });
|
|
159
|
+
return result;
|
|
160
|
+
}
|
|
161
|
+
export function validateModelIdAgainstList({ model, models, }) {
|
|
162
|
+
const parsed = parseModelId(model);
|
|
163
|
+
if (!parsed) {
|
|
164
|
+
return new InvalidModelError({
|
|
165
|
+
model,
|
|
166
|
+
reason: 'expected provider/model, for example anthropic/claude-opus-4-6',
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
const match = models.find((candidate) => {
|
|
170
|
+
return (candidate.providerID === parsed.providerID &&
|
|
171
|
+
candidate.modelID === parsed.modelID);
|
|
172
|
+
});
|
|
173
|
+
if (!match) {
|
|
174
|
+
const siblings = models
|
|
175
|
+
.filter((candidate) => candidate.providerID === parsed.providerID)
|
|
176
|
+
.map((candidate) => `${candidate.providerID}/${candidate.modelID}`);
|
|
177
|
+
if (siblings.length > 0) {
|
|
178
|
+
return new InvalidModelError({
|
|
179
|
+
model,
|
|
180
|
+
reason: `unknown model. Similar: ${siblings.slice(0, 8).join(', ')}`,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
const connectedProviders = [
|
|
184
|
+
...new Set(models.filter((candidate) => candidate.connected).map((candidate) => {
|
|
185
|
+
return candidate.providerID;
|
|
186
|
+
})),
|
|
187
|
+
];
|
|
188
|
+
const connectedHint = connectedProviders.length
|
|
189
|
+
? ` Connected providers: ${connectedProviders.join(', ')}`
|
|
190
|
+
: '';
|
|
191
|
+
return new InvalidModelError({
|
|
192
|
+
model,
|
|
193
|
+
reason: `unknown provider ${parsed.providerID}.${connectedHint}`,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
if (!match.connected) {
|
|
197
|
+
return new InvalidModelError({
|
|
198
|
+
model,
|
|
199
|
+
reason: `provider ${parsed.providerID} is not connected`,
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
return parsed;
|
|
203
|
+
}
|
|
204
|
+
export async function validateModelId({ model, getClient, directory, }) {
|
|
205
|
+
const models = await listModels({ getClient, directory });
|
|
206
|
+
if (models instanceof Error)
|
|
207
|
+
return models;
|
|
208
|
+
return validateModelIdAgainstList({ model, models });
|
|
209
|
+
}
|
|
210
|
+
export async function validateCliModelOption({ model, directory, }) {
|
|
211
|
+
if (!model)
|
|
212
|
+
return;
|
|
213
|
+
const parsed = parseModelId(model);
|
|
214
|
+
if (!parsed) {
|
|
215
|
+
return new InvalidModelError({
|
|
216
|
+
model,
|
|
217
|
+
reason: 'expected provider/model, for example anthropic/claude-opus-4-6',
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
if (!directory)
|
|
221
|
+
return parsed;
|
|
222
|
+
const getClient = await initializeOpencodeForDirectory(directory);
|
|
223
|
+
if (getClient instanceof Error)
|
|
224
|
+
return getClient;
|
|
225
|
+
return validateModelId({ model, getClient, directory });
|
|
226
|
+
}
|
|
73
227
|
/**
|
|
74
228
|
* Get the default model from OpenCode when no user preference is set.
|
|
75
229
|
* Priority (matches OpenCode TUI behavior):
|
|
@@ -105,7 +259,7 @@ export async function getDefaultModel({ getClient, directory, }) {
|
|
|
105
259
|
const configResponse = await getClient().config.get({ directory })
|
|
106
260
|
.catch((e) => new OpenCodeSdkError({ operation: 'config.get', cause: e }));
|
|
107
261
|
if (!(configResponse instanceof Error) && configResponse.data?.model) {
|
|
108
|
-
const configModel =
|
|
262
|
+
const configModel = parseModelId(configResponse.data.model);
|
|
109
263
|
if (configModel && isModelValid(configModel, connected, providers)) {
|
|
110
264
|
sessionLogger.log(`[MODEL] Using config model: ${configModel.providerID}/${configModel.modelID}`);
|
|
111
265
|
return { ...configModel, source: 'opencode-config' };
|