@kkelly-offical/kkcode 0.1.3 → 0.1.6
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 +110 -172
- package/package.json +46 -46
- package/src/agent/agent.mjs +41 -0
- package/src/agent/prompt/frontend-designer.txt +58 -0
- package/src/agent/prompt/longagent-blueprint-agent.txt +83 -0
- package/src/agent/prompt/longagent-coding-agent.txt +37 -0
- package/src/agent/prompt/longagent-debugging-agent.txt +46 -0
- package/src/agent/prompt/longagent-preview-agent.txt +63 -0
- package/src/config/defaults.mjs +260 -195
- package/src/config/schema.mjs +71 -6
- package/src/core/constants.mjs +91 -46
- package/src/index.mjs +1 -1
- package/src/knowledge/frontend-aesthetics.txt +39 -0
- package/src/knowledge/loader.mjs +2 -1
- package/src/knowledge/tailwind.txt +12 -3
- package/src/mcp/client-http.mjs +141 -157
- package/src/mcp/client-sse.mjs +288 -286
- package/src/mcp/client-stdio.mjs +533 -451
- package/src/mcp/constants.mjs +2 -0
- package/src/mcp/registry.mjs +479 -394
- package/src/mcp/stdio-framing.mjs +133 -127
- package/src/mcp/tool-result.mjs +24 -0
- package/src/observability/index.mjs +42 -0
- package/src/observability/metrics.mjs +137 -0
- package/src/observability/tracer.mjs +137 -0
- package/src/orchestration/background-manager.mjs +372 -358
- package/src/orchestration/background-worker.mjs +305 -245
- package/src/orchestration/longagent-manager.mjs +171 -116
- package/src/orchestration/stage-scheduler.mjs +728 -489
- package/src/permission/exec-policy.mjs +9 -11
- package/src/provider/anthropic.mjs +1 -0
- package/src/provider/openai.mjs +340 -339
- package/src/provider/retry-policy.mjs +68 -68
- package/src/provider/router.mjs +241 -228
- package/src/provider/sse.mjs +104 -91
- package/src/repl.mjs +1 -1
- package/src/session/checkpoint.mjs +66 -3
- package/src/session/engine.mjs +227 -225
- package/src/session/longagent-4stage.mjs +460 -0
- package/src/session/longagent-hybrid.mjs +1081 -0
- package/src/session/longagent-plan.mjs +365 -329
- package/src/session/longagent-project-memory.mjs +53 -0
- package/src/session/longagent-scaffold.mjs +291 -100
- package/src/session/longagent-task-bus.mjs +54 -0
- package/src/session/longagent-utils.mjs +472 -0
- package/src/session/longagent.mjs +884 -1462
- package/src/session/project-context.mjs +30 -0
- package/src/session/store.mjs +510 -503
- package/src/session/task-validator.mjs +4 -3
- package/src/skill/builtin/design.mjs +76 -0
- package/src/skill/builtin/frontend.mjs +8 -0
- package/src/skill/registry.mjs +390 -336
- package/src/storage/ghost-commit-store.mjs +18 -8
- package/src/tool/executor.mjs +11 -0
- package/src/tool/git-auto.mjs +0 -19
- package/src/tool/registry.mjs +71 -37
- package/src/ui/activity-renderer.mjs +664 -410
- package/src/util/git.mjs +23 -0
package/src/session/engine.mjs
CHANGED
|
@@ -1,225 +1,227 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto"
|
|
2
|
-
import { loadPricing, calculateCost } from "../usage/pricing.mjs"
|
|
3
|
-
import { recordTurn } from "../usage/usage-meter.mjs"
|
|
4
|
-
import { processTurnLoop } from "./loop.mjs"
|
|
5
|
-
import { runLongAgent } from "./longagent.mjs"
|
|
6
|
-
import { touchSession, setBudgetState } from "./store.mjs"
|
|
7
|
-
import { appendEventLog } from "../storage/event-log.mjs"
|
|
8
|
-
import { EventBus } from "../core/events.mjs"
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
const
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
1
|
+
import { randomUUID } from "node:crypto"
|
|
2
|
+
import { loadPricing, calculateCost } from "../usage/pricing.mjs"
|
|
3
|
+
import { recordTurn } from "../usage/usage-meter.mjs"
|
|
4
|
+
import { processTurnLoop } from "./loop.mjs"
|
|
5
|
+
import { runLongAgent } from "./longagent.mjs"
|
|
6
|
+
import { touchSession, setBudgetState } from "./store.mjs"
|
|
7
|
+
import { appendEventLog } from "../storage/event-log.mjs"
|
|
8
|
+
import { EventBus } from "../core/events.mjs"
|
|
9
|
+
import { initialize as initObservability } from "../observability/index.mjs"
|
|
10
|
+
import { ToolRegistry } from "../tool/registry.mjs"
|
|
11
|
+
import { resolveAgentForMode } from "../agent/agent.mjs"
|
|
12
|
+
import { estimateStringTokens } from "./compaction.mjs"
|
|
13
|
+
|
|
14
|
+
let sinkReady = false
|
|
15
|
+
|
|
16
|
+
function estimateTokens(text) {
|
|
17
|
+
return Math.max(1, estimateStringTokens(text || ""))
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function resolveMode(inputMode = "agent") {
|
|
21
|
+
const mode = String(inputMode || "agent").toLowerCase()
|
|
22
|
+
if (["ask", "plan", "agent", "longagent"].includes(mode)) return mode
|
|
23
|
+
return "agent"
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function newSessionId() {
|
|
27
|
+
return `ses_${randomUUID().slice(0, 12)}`
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function maybeRegisterSink() {
|
|
31
|
+
if (sinkReady) return
|
|
32
|
+
EventBus.registerSink(async (event) => {
|
|
33
|
+
await appendEventLog(event)
|
|
34
|
+
})
|
|
35
|
+
initObservability(EventBus)
|
|
36
|
+
sinkReady = true
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function evaluateBudget(config, meter) {
|
|
40
|
+
const budget = config.usage?.budget || {}
|
|
41
|
+
const warnings = []
|
|
42
|
+
const strategy = budget.strategy || "warn"
|
|
43
|
+
const warnAt = Number(budget.warn_at_percent || 80)
|
|
44
|
+
let exceeded = false
|
|
45
|
+
|
|
46
|
+
if (budget.session_usd && meter.session.cost > 0) {
|
|
47
|
+
const ratio = (meter.session.cost / budget.session_usd) * 100
|
|
48
|
+
if (ratio >= 100) exceeded = true
|
|
49
|
+
if (ratio >= warnAt) warnings.push(`session budget ${ratio.toFixed(1)}% (${meter.session.cost.toFixed(4)}/${budget.session_usd})`)
|
|
50
|
+
}
|
|
51
|
+
if (budget.global_usd && meter.global.cost > 0) {
|
|
52
|
+
const ratio = (meter.global.cost / budget.global_usd) * 100
|
|
53
|
+
if (ratio >= 100) exceeded = true
|
|
54
|
+
if (ratio >= warnAt) warnings.push(`global budget ${ratio.toFixed(1)}% (${meter.global.cost.toFixed(4)}/${budget.global_usd})`)
|
|
55
|
+
}
|
|
56
|
+
return { warnings, exceeded, strategy }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export async function executeTurn({
|
|
60
|
+
prompt,
|
|
61
|
+
contentBlocks = null,
|
|
62
|
+
mode,
|
|
63
|
+
model,
|
|
64
|
+
sessionId,
|
|
65
|
+
configState,
|
|
66
|
+
providerType = null,
|
|
67
|
+
baseUrl = null,
|
|
68
|
+
apiKeyEnv = null,
|
|
69
|
+
maxIterations = null,
|
|
70
|
+
signal = null,
|
|
71
|
+
output = null,
|
|
72
|
+
allowQuestion = true,
|
|
73
|
+
toolContext = {}
|
|
74
|
+
}) {
|
|
75
|
+
maybeRegisterSink()
|
|
76
|
+
|
|
77
|
+
const resolvedProviderType = providerType || configState.config.provider.default
|
|
78
|
+
const agent = resolveAgentForMode(mode)
|
|
79
|
+
await ToolRegistry.initialize({
|
|
80
|
+
config: configState.config,
|
|
81
|
+
cwd: process.cwd()
|
|
82
|
+
})
|
|
83
|
+
await touchSession({
|
|
84
|
+
sessionId,
|
|
85
|
+
mode,
|
|
86
|
+
model,
|
|
87
|
+
providerType: resolvedProviderType,
|
|
88
|
+
cwd: process.cwd(),
|
|
89
|
+
status: mode === "longagent" ? "running-longagent" : "active"
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
const turn =
|
|
93
|
+
mode === "longagent"
|
|
94
|
+
? await runLongAgent({
|
|
95
|
+
prompt,
|
|
96
|
+
model,
|
|
97
|
+
providerType: resolvedProviderType,
|
|
98
|
+
sessionId,
|
|
99
|
+
configState,
|
|
100
|
+
baseUrl,
|
|
101
|
+
apiKeyEnv,
|
|
102
|
+
agent,
|
|
103
|
+
maxIterations:
|
|
104
|
+
maxIterations === null
|
|
105
|
+
? Number(configState.config.agent.longagent.max_iterations || 0)
|
|
106
|
+
: Number(maxIterations),
|
|
107
|
+
signal,
|
|
108
|
+
output,
|
|
109
|
+
allowQuestion,
|
|
110
|
+
toolContext
|
|
111
|
+
})
|
|
112
|
+
: await processTurnLoop({
|
|
113
|
+
prompt,
|
|
114
|
+
contentBlocks,
|
|
115
|
+
mode,
|
|
116
|
+
model,
|
|
117
|
+
providerType: resolvedProviderType,
|
|
118
|
+
sessionId,
|
|
119
|
+
configState,
|
|
120
|
+
baseUrl,
|
|
121
|
+
apiKeyEnv,
|
|
122
|
+
agent,
|
|
123
|
+
output,
|
|
124
|
+
signal,
|
|
125
|
+
allowQuestion,
|
|
126
|
+
toolContext
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
const usage = { ...turn.usage }
|
|
130
|
+
let estimated = false
|
|
131
|
+
if ((usage.input || 0) === 0 && (usage.output || 0) === 0) {
|
|
132
|
+
usage.input = estimateTokens(prompt)
|
|
133
|
+
usage.output = estimateTokens(turn.reply)
|
|
134
|
+
estimated = true
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const pricingInfo = await loadPricing(configState)
|
|
138
|
+
const costInfo = calculateCost(pricingInfo.pricing, model, usage)
|
|
139
|
+
const meter = await recordTurn({ sessionId, usage, cost: costInfo.amount })
|
|
140
|
+
const budgetResult = evaluateBudget(configState.config, meter)
|
|
141
|
+
|
|
142
|
+
await setBudgetState(sessionId, {
|
|
143
|
+
lastTurnCost: costInfo.amount,
|
|
144
|
+
warnings: budgetResult.warnings,
|
|
145
|
+
exceeded: budgetResult.exceeded,
|
|
146
|
+
updatedAt: Date.now()
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
if (budgetResult.exceeded && budgetResult.strategy === "block") {
|
|
150
|
+
const msg = `budget exceeded — ${budgetResult.warnings.join("; ")}. strategy=block, stopping execution.`
|
|
151
|
+
return {
|
|
152
|
+
reply: msg,
|
|
153
|
+
mode,
|
|
154
|
+
model,
|
|
155
|
+
sessionId,
|
|
156
|
+
turnId: turn.turnId,
|
|
157
|
+
emittedText: turn.emittedText,
|
|
158
|
+
context: turn.context,
|
|
159
|
+
tokenMeter: { ...meter, estimated: estimated || costInfo.unknown },
|
|
160
|
+
cost: costInfo.amount,
|
|
161
|
+
costSavings: costInfo.savings,
|
|
162
|
+
pricingWarnings: pricingInfo.errors,
|
|
163
|
+
budgetWarnings: budgetResult.warnings,
|
|
164
|
+
budgetExceeded: true,
|
|
165
|
+
toolEvents: turn.toolEvents,
|
|
166
|
+
longagent: mode === "longagent"
|
|
167
|
+
? {
|
|
168
|
+
status: turn.status,
|
|
169
|
+
phase: turn.phase,
|
|
170
|
+
gateStatus: turn.gateStatus,
|
|
171
|
+
currentGate: turn.currentGate,
|
|
172
|
+
lastGateFailures: turn.lastGateFailures || [],
|
|
173
|
+
iterations: turn.iterations,
|
|
174
|
+
recoveryCount: turn.recoveryCount,
|
|
175
|
+
progress: turn.progress,
|
|
176
|
+
elapsed: turn.elapsed,
|
|
177
|
+
stageIndex: turn.stageIndex,
|
|
178
|
+
stageCount: turn.stageCount,
|
|
179
|
+
currentStageId: turn.currentStageId,
|
|
180
|
+
planFrozen: turn.planFrozen,
|
|
181
|
+
taskProgress: turn.taskProgress,
|
|
182
|
+
stageProgress: turn.stageProgress,
|
|
183
|
+
remainingFilesCount: turn.remainingFilesCount,
|
|
184
|
+
fileChanges: turn.fileChanges || []
|
|
185
|
+
}
|
|
186
|
+
: null
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return {
|
|
191
|
+
reply: turn.reply,
|
|
192
|
+
mode,
|
|
193
|
+
model,
|
|
194
|
+
sessionId,
|
|
195
|
+
turnId: turn.turnId,
|
|
196
|
+
emittedText: turn.emittedText,
|
|
197
|
+
context: turn.context,
|
|
198
|
+
tokenMeter: { ...meter, estimated: estimated || costInfo.unknown },
|
|
199
|
+
cost: costInfo.amount,
|
|
200
|
+
costSavings: costInfo.savings,
|
|
201
|
+
pricingWarnings: pricingInfo.errors,
|
|
202
|
+
budgetWarnings: budgetResult.warnings,
|
|
203
|
+
budgetExceeded: false,
|
|
204
|
+
toolEvents: turn.toolEvents,
|
|
205
|
+
longagent: mode === "longagent"
|
|
206
|
+
? {
|
|
207
|
+
status: turn.status,
|
|
208
|
+
phase: turn.phase,
|
|
209
|
+
gateStatus: turn.gateStatus,
|
|
210
|
+
currentGate: turn.currentGate,
|
|
211
|
+
lastGateFailures: turn.lastGateFailures || [],
|
|
212
|
+
iterations: turn.iterations,
|
|
213
|
+
recoveryCount: turn.recoveryCount,
|
|
214
|
+
progress: turn.progress,
|
|
215
|
+
elapsed: turn.elapsed,
|
|
216
|
+
stageIndex: turn.stageIndex,
|
|
217
|
+
stageCount: turn.stageCount,
|
|
218
|
+
currentStageId: turn.currentStageId,
|
|
219
|
+
planFrozen: turn.planFrozen,
|
|
220
|
+
taskProgress: turn.taskProgress,
|
|
221
|
+
stageProgress: turn.stageProgress,
|
|
222
|
+
remainingFilesCount: turn.remainingFilesCount,
|
|
223
|
+
fileChanges: turn.fileChanges || []
|
|
224
|
+
}
|
|
225
|
+
: null
|
|
226
|
+
}
|
|
227
|
+
}
|