minicode-ai 0.27.0 → 0.28.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/cli/args.ts +120 -16
- package/cli/commands/acp.ts +38 -28
- package/cli/commands/auth.ts +17 -17
- package/cli/commands/config.ts +65 -66
- package/cli/commands/daemon.ts +44 -16
- package/cli/commands/doctor.ts +11 -12
- package/cli/commands/exec.ts +35 -73
- package/cli/commands/mcp.ts +25 -8
- package/cli/commands/memory.ts +16 -24
- package/cli/commands/openwork.ts +6 -2
- package/cli/commands/pricing.ts +14 -12
- package/cli/commands/providers.ts +36 -57
- package/cli/commands/sessions.ts +24 -18
- package/cli/commands/skills.ts +33 -18
- package/cli/commands/stats.ts +15 -30
- package/cli/commands.ts +73 -44
- package/cli/context-command-run.ts +30 -18
- package/cli/index.ts +48 -98
- package/cli/model-manager.ts +7 -2
- package/cli/provider-manager.ts +33 -18
- package/cli/router.ts +41 -12
- package/cli/setup.ts +246 -123
- package/cli/tui.ts +303 -100
- package/docs/TERMINAL_CONTRACT.md +254 -69
- package/docs/UI_RENDER_PIPELINE.md +9 -1
- package/docs/agents.md +2 -2
- package/docs/architecture.md +3 -3
- package/docs/choosing-mode.md +7 -9
- package/docs/cli.md +3 -5
- package/docs/environment.md +1 -2
- package/docs/policy-sandbox.md +3 -3
- package/docs/repl.md +2 -2
- package/docs/tools.md +1 -1
- package/package.json +1 -1
- package/src/app/mentions.ts +60 -13
- package/src/app/rag-layer.ts +17 -20
- package/src/app/session.ts +119 -66
- package/src/app/tool-layer.ts +12 -17
- package/src/config.ts +64 -104
- package/src/constants.ts +4 -6
- package/src/daemon/capability.ts +8 -1
- package/src/daemon/control.ts +3 -2
- package/src/daemon/server.ts +57 -0
- package/src/hooks/run.ts +73 -41
- package/src/lib/db-path.ts +47 -0
- package/src/lib/process-tree.ts +62 -0
- package/src/lib/public-http.ts +88 -23
- package/src/lib/trusted-exec.ts +48 -1
- package/src/lsp/client.ts +62 -17
- package/src/mcp/client.ts +85 -38
- package/src/mcp/http-transport.ts +28 -16
- package/src/mcp/transport.ts +42 -6
- package/src/memory/files.ts +164 -134
- package/src/memory/vector.ts +371 -420
- package/src/policy/approval-binding.ts +122 -45
- package/src/policy/bash-guard.ts +18 -7
- package/src/policy/compaction.ts +51 -18
- package/src/policy/context-window.ts +31 -8
- package/src/policy/context.ts +62 -36
- package/src/policy/executor.ts +17 -7
- package/src/policy/microcompact.ts +4 -3
- package/src/policy/no-progress.ts +103 -22
- package/src/policy/permission.ts +36 -12
- package/src/policy/tool-contract.ts +74 -416
- package/src/policy/usage.ts +24 -2
- package/src/policy/verifier.ts +50 -2
- package/src/policy/work-mode.ts +28 -54
- package/src/presentation/adapter.ts +24 -16
- package/src/providers/provision.ts +6 -56
- package/src/providers/responses.ts +1 -0
- package/src/runtime/execution-journal.ts +4 -29
- package/src/sandbox/docker.ts +58 -34
- package/src/sandbox/mask.ts +157 -0
- package/src/sandbox/os.ts +107 -73
- package/src/session/authority.ts +29 -10
- package/src/session/context-projection.ts +6 -5
- package/src/session/open-work.ts +26 -3
- package/src/session/persistence.ts +25 -58
- package/src/session/verification.ts +170 -20
- package/src/skills/loader.ts +31 -16
- package/src/task/production-scheduler.ts +13 -1
- package/src/task/store.ts +20 -37
- package/src/telemetry/trace.ts +27 -0
- package/src/tools/ask_user.ts +1 -13
- package/src/tools/bash.ts +27 -92
- package/src/tools/code_run.ts +1 -1
- package/src/tools/context.ts +133 -0
- package/src/tools/delete_file.ts +3 -2
- package/src/tools/edit.ts +37 -72
- package/src/tools/git.ts +86 -9
- package/src/tools/glob.ts +26 -51
- package/src/tools/grep.ts +67 -57
- package/src/tools/hashline.ts +4 -0
- package/src/tools/lsp.ts +11 -8
- package/src/tools/mcp_call.ts +9 -6
- package/src/tools/memory.ts +56 -33
- package/src/tools/move_file.ts +36 -10
- package/src/tools/patch.ts +13 -21
- package/src/tools/read_file.ts +18 -25
- package/src/tools/read_image.ts +35 -33
- package/src/tools/submit_result.ts +29 -8
- package/src/tools/task.ts +74 -17
- package/src/tools/todo.ts +181 -74
- package/src/tools/web_fetch.ts +42 -2
- package/src/tools/web_search.ts +47 -8
- package/src/ui/approval/prompt.ts +206 -47
- package/src/ui/assistant/turn-status.ts +14 -5
- package/src/ui/footer.ts +4 -4
- package/src/ui/i18n/en.ts +47 -23
- package/src/ui/i18n/id.ts +76 -51
- package/src/ui/input/input.ts +4 -3
- package/src/ui/input/prompt-engine.ts +35 -5
- package/src/ui/render/activity-panel.ts +4 -2
- package/src/ui/render/hint.ts +7 -4
- package/src/ui/render/list.ts +32 -0
- package/src/ui/render/markdown.ts +6 -3
- package/src/ui/render/notice.ts +19 -0
- package/src/ui/render/table-grid.ts +7 -8
- package/src/ui/render/theme.ts +113 -39
- package/src/ui/runtime/screen.ts +74 -10
- package/src/ui/runtime/spinner.ts +19 -6
- package/src/ui/screens/dialog.ts +19 -1
- package/src/ui/screens/form.ts +80 -35
- package/src/ui/screens/model-manager.ts +82 -61
- package/src/ui/screens/output-window.ts +263 -0
- package/src/ui/screens/picker.ts +94 -81
- package/src/ui/screens/provider-manager.ts +100 -69
- package/src/ui/screens/wizard.ts +117 -28
- package/src/ui/tui/app.ts +196 -33
- package/src/ui/tui/transcript.ts +154 -25
- package/vendor/minicore/src/core/executor.ts +2 -0
- package/vendor/minicore/src/core/loop.ts +17 -6
- package/vendor/minicore/src/core/session.ts +3 -0
- package/vendor/minicore/src/core/tool.ts +1 -0
- package/vendor/minicore/src/providers/openai-compat.ts +28 -19
package/src/ui/tui/transcript.ts
CHANGED
|
@@ -56,6 +56,8 @@ export interface ApprovalSink {
|
|
|
56
56
|
resume(): void
|
|
57
57
|
/** Lukis kotak modal di atas transkrip (popup komposit). Opsional: sink warisan (mock/test) boleh tanpa. */
|
|
58
58
|
paintRegion?(lines: string[], topRow: number): void
|
|
59
|
+
/** Parkir kursor di baris aktif modal. Opsional. */
|
|
60
|
+
moveCursor?(row: number, col: number): void
|
|
59
61
|
/** Hapus kotak modal sebelum resume. Opsional. */
|
|
60
62
|
clearRegion?(): void
|
|
61
63
|
}
|
|
@@ -108,6 +110,8 @@ export interface TranscriptRow {
|
|
|
108
110
|
displayStart: number
|
|
109
111
|
displayEnd: number
|
|
110
112
|
selectable: boolean
|
|
113
|
+
/** Teks tampil polos saat transformasi markdown inline mengubah kolom vs sumber. */
|
|
114
|
+
plainBody?: string
|
|
111
115
|
}
|
|
112
116
|
|
|
113
117
|
export interface TranscriptViewport {
|
|
@@ -169,10 +173,23 @@ function formatElapsed(ms: number): string {
|
|
|
169
173
|
return `${Math.floor(minutes / 60)}h${String(minutes % 60).padStart(2, "0")}m`
|
|
170
174
|
}
|
|
171
175
|
|
|
172
|
-
type FenceState = "outside" | "inside" | "boundary"
|
|
176
|
+
type FenceState = "outside" | "inside" | "boundary" | `open:${string}`
|
|
177
|
+
|
|
178
|
+
function clampCodeUnitBoundary(s: string, index: number): number {
|
|
179
|
+
const idx = Math.max(0, Math.min(index, s.length))
|
|
180
|
+
if (idx > 0 && idx < s.length) {
|
|
181
|
+
const code = s.charCodeAt(idx)
|
|
182
|
+
if (code >= 0xdc00 && code <= 0xdfff) return idx - 1
|
|
183
|
+
}
|
|
184
|
+
return idx
|
|
185
|
+
}
|
|
173
186
|
|
|
174
187
|
function renderAssistantLine(line: string, fenceState: FenceState): string | null {
|
|
175
188
|
if (fenceState === "boundary") return null
|
|
189
|
+
if (fenceState.startsWith("open:")) {
|
|
190
|
+
const lang = fenceState.slice(5)
|
|
191
|
+
return lang ? c.dim(` ${glyphs.dot} ${lang}`) : null
|
|
192
|
+
}
|
|
176
193
|
if (fenceState === "inside") return line
|
|
177
194
|
if (!/(?:\*{1,2}|`|^#{1,6}\s|~~|\[[^\]]+\]\()/.test(line)) return line
|
|
178
195
|
const heading = /^(#{1,6})\s+(.*)$/.exec(line)
|
|
@@ -603,13 +620,19 @@ export class Transcript {
|
|
|
603
620
|
const rows = sanitizeAnsi(prompt).split("\n")
|
|
604
621
|
const [first, ...rest] = rows
|
|
605
622
|
this.commit()
|
|
623
|
+
// Jeda visual lembut sebelum prompt user berikutnya agar batas giliran
|
|
624
|
+
// terbaca cepat di transkrip panjang (baris awal sesi tidak diberi jeda).
|
|
625
|
+
if (this.lines.length > 0 && this.lines[this.lines.length - 1] !== "") {
|
|
626
|
+
this.append("", { kind: "system" })
|
|
627
|
+
}
|
|
628
|
+
const arrow = glyphs.arrow
|
|
606
629
|
this.appendWithSource(
|
|
607
|
-
`${c.accent("minicode")} ${c.muted(
|
|
630
|
+
`${c.accent("minicode")} ${c.muted(arrow)} ${first ?? ""}`,
|
|
608
631
|
{ kind: "user" },
|
|
609
632
|
first ?? "",
|
|
610
|
-
|
|
633
|
+
`minicode ${arrow} `,
|
|
611
634
|
)
|
|
612
|
-
for (const r of rest) this.appendWithSource(` ${r}`, { kind: "user" }, r, "
|
|
635
|
+
for (const r of rest) this.appendWithSource(` ${r}`, { kind: "user" }, r, " ")
|
|
613
636
|
}
|
|
614
637
|
|
|
615
638
|
pushError(message: string): void {
|
|
@@ -705,27 +728,108 @@ export class Transcript {
|
|
|
705
728
|
sourceBase += source.length + 1
|
|
706
729
|
continue
|
|
707
730
|
}
|
|
731
|
+
const isBoundaryHeader = markdown && fenceState.startsWith("open:")
|
|
708
732
|
const renderedRows = rendered === "" ? [""] : wrapByWidth(rendered, w)
|
|
709
|
-
const
|
|
710
|
-
|
|
711
|
-
const
|
|
712
|
-
|
|
733
|
+
const plainRenderedFull = stripSgr(sanitizeAnsi(rendered))
|
|
734
|
+
const expectedExact = `${sourcePrefix}${source}`
|
|
735
|
+
const exactMatch = !isBoundaryHeader && (!markdown || plainRenderedFull === expectedExact)
|
|
736
|
+
const prefixTrimmed = sourcePrefix.trimEnd()
|
|
737
|
+
const rowBodies: Array<{
|
|
738
|
+
text: string
|
|
739
|
+
plainBody: string
|
|
740
|
+
displayStart: number
|
|
741
|
+
displayEnd: number
|
|
742
|
+
}> = []
|
|
743
|
+
for (let rowIndex = 0; rowIndex < renderedRows.length; rowIndex++) {
|
|
713
744
|
const renderedRow = renderedRows[rowIndex] ?? ""
|
|
714
|
-
const
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
745
|
+
const plainRendered = stripSgr(sanitizeAnsi(renderedRow))
|
|
746
|
+
let rowDisplayStart = 0
|
|
747
|
+
let plainBody = plainRendered
|
|
748
|
+
if (sourcePrefix && rowIndex === 0) {
|
|
749
|
+
if (plainRendered.startsWith(sourcePrefix)) {
|
|
750
|
+
rowDisplayStart = displayWidth(sourcePrefix)
|
|
751
|
+
plainBody = plainRendered.slice(sourcePrefix.length)
|
|
752
|
+
} else if (
|
|
753
|
+
prefixTrimmed &&
|
|
754
|
+
plainRendered.startsWith(prefixTrimmed) &&
|
|
755
|
+
plainRendered.length <= sourcePrefix.length
|
|
756
|
+
) {
|
|
757
|
+
// Terminal sempit memecah tepat di ujung chrome prefix ("minicode ›").
|
|
758
|
+
rowDisplayStart = displayWidth(plainRendered)
|
|
759
|
+
plainBody = ""
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
rowBodies.push({
|
|
720
763
|
text: renderedRow,
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
sourceEnd: offset + sourceRow.length,
|
|
724
|
-
displayStart: 0,
|
|
764
|
+
plainBody,
|
|
765
|
+
displayStart: rowDisplayStart,
|
|
725
766
|
displayEnd: displayWidth(renderedRow),
|
|
726
|
-
selectable: selectable && sourceId >= 0,
|
|
727
767
|
})
|
|
728
|
-
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
const rowSelectable = selectable && sourceId >= 0 && !isBoundaryHeader
|
|
771
|
+
if (exactMatch) {
|
|
772
|
+
let cursorInSource = 0
|
|
773
|
+
for (let rowIndex = 0; rowIndex < rowBodies.length; rowIndex++) {
|
|
774
|
+
const item = rowBodies[rowIndex]!
|
|
775
|
+
if (
|
|
776
|
+
rowIndex > 0 &&
|
|
777
|
+
item.plainBody.length > 0 &&
|
|
778
|
+
source.slice(cursorInSource).startsWith(" ") &&
|
|
779
|
+
!item.plainBody.startsWith(" ") &&
|
|
780
|
+
source.slice(cursorInSource + 1).startsWith(item.plainBody)
|
|
781
|
+
) {
|
|
782
|
+
cursorInSource += 1
|
|
783
|
+
}
|
|
784
|
+
const start = sourceBase + cursorInSource
|
|
785
|
+
const remaining = Math.max(0, source.length - cursorInSource)
|
|
786
|
+
const take =
|
|
787
|
+
rowIndex === rowBodies.length - 1
|
|
788
|
+
? remaining
|
|
789
|
+
: Math.min(item.plainBody.length, remaining)
|
|
790
|
+
cursorInSource += take
|
|
791
|
+
const end = sourceBase + cursorInSource
|
|
792
|
+
out.push({
|
|
793
|
+
text: item.text,
|
|
794
|
+
sourceId: rowSelectable ? sourceId : -1,
|
|
795
|
+
sourceStart: start,
|
|
796
|
+
sourceEnd: end,
|
|
797
|
+
displayStart: item.displayStart,
|
|
798
|
+
displayEnd: item.displayEnd,
|
|
799
|
+
selectable: rowSelectable,
|
|
800
|
+
})
|
|
801
|
+
}
|
|
802
|
+
} else {
|
|
803
|
+
// Inline markdown mengubah panjang tampilan vs sumber (`**bold**`,
|
|
804
|
+
// `[label](url)`, heading `## `): petakan proporsional ke kolom visual
|
|
805
|
+
// agar highlight seleksi dan copy tidak meleset di baris yang sama.
|
|
806
|
+
const colWidths = rowBodies.map((item) => Math.max(0, item.displayEnd - item.displayStart))
|
|
807
|
+
const totalCols = colWidths.reduce((sum, v) => sum + v, 0)
|
|
808
|
+
let prevPos = 0
|
|
809
|
+
let accCols = 0
|
|
810
|
+
for (let rowIndex = 0; rowIndex < rowBodies.length; rowIndex++) {
|
|
811
|
+
const item = rowBodies[rowIndex]!
|
|
812
|
+
accCols += colWidths[rowIndex]!
|
|
813
|
+
const nextPos =
|
|
814
|
+
isBoundaryHeader || !rowSelectable
|
|
815
|
+
? 0
|
|
816
|
+
: rowIndex === rowBodies.length - 1 || totalCols === 0
|
|
817
|
+
? source.length
|
|
818
|
+
: clampCodeUnitBoundary(source, Math.round((accCols / totalCols) * source.length))
|
|
819
|
+
const start = sourceBase + prevPos
|
|
820
|
+
const end = sourceBase + Math.max(prevPos, nextPos)
|
|
821
|
+
prevPos = Math.max(prevPos, nextPos)
|
|
822
|
+
out.push({
|
|
823
|
+
text: item.text,
|
|
824
|
+
sourceId: rowSelectable ? sourceId : -1,
|
|
825
|
+
sourceStart: start,
|
|
826
|
+
sourceEnd: end,
|
|
827
|
+
displayStart: item.displayStart,
|
|
828
|
+
displayEnd: item.displayEnd,
|
|
829
|
+
selectable: rowSelectable,
|
|
830
|
+
...(rowSelectable ? { plainBody: item.plainBody } : {}),
|
|
831
|
+
})
|
|
832
|
+
}
|
|
729
833
|
}
|
|
730
834
|
sourceBase += source.length + 1
|
|
731
835
|
}
|
|
@@ -744,12 +848,17 @@ export class Transcript {
|
|
|
744
848
|
base += row.length + 1
|
|
745
849
|
}
|
|
746
850
|
const plainRows = renderedRows.map((row) => stripSgr(sanitizeAnsi(row)))
|
|
747
|
-
|
|
851
|
+
// Border Unicode (─/box drawing) DAN fallback ASCII (`+---+`) harus
|
|
852
|
+
// dikenali. Tanpa varian ASCII, baris `+---+` dianggap data: ia menjadi
|
|
853
|
+
// selectable dengan offset yang salah dan seleksi tabel kehilangan teks.
|
|
854
|
+
const isBorderRow = (row: string): boolean =>
|
|
855
|
+
/^[\s─━┌┐└┘├┤┬┴┼]+$/.test(row) || /^[\s+-]+$/.test(row)
|
|
856
|
+
const horizontal = plainRows.some(isBorderRow)
|
|
748
857
|
const fields = Math.max(1, table.headers.length)
|
|
749
858
|
let logicalIndex = 0
|
|
750
859
|
return renderedRows.map((row, index) => {
|
|
751
860
|
const plain = plainRows[index] ?? ""
|
|
752
|
-
const isBorder =
|
|
861
|
+
const isBorder = isBorderRow(plain)
|
|
753
862
|
let range = ranges[0] ?? { start: 0, end: 0 }
|
|
754
863
|
if (horizontal) {
|
|
755
864
|
if (!isBorder) range = ranges[logicalIndex] ?? range
|
|
@@ -785,7 +894,7 @@ export class Transcript {
|
|
|
785
894
|
open = true
|
|
786
895
|
char = fence.char
|
|
787
896
|
len = fence.len
|
|
788
|
-
return "boundary"
|
|
897
|
+
return fence.lang ? `open:${fence.lang}` : "boundary"
|
|
789
898
|
}
|
|
790
899
|
if (open && fence && fence.char === char && fence.len >= len) {
|
|
791
900
|
open = false
|
|
@@ -1101,6 +1210,17 @@ export class Transcript {
|
|
|
1101
1210
|
const source = this.sourceTexts.get(row.sourceId)
|
|
1102
1211
|
if (source === undefined) return null
|
|
1103
1212
|
const relative = Math.max(0, column - row.displayStart)
|
|
1213
|
+
if (
|
|
1214
|
+
row.plainBody !== undefined &&
|
|
1215
|
+
source.slice(row.sourceStart, row.sourceEnd) !== row.plainBody
|
|
1216
|
+
) {
|
|
1217
|
+
const bodyCols = Math.max(0, row.displayEnd - row.displayStart)
|
|
1218
|
+
if (bodyCols === 0 || relative === 0) return { id: row.sourceId, offset: row.sourceStart }
|
|
1219
|
+
if (relative >= bodyCols) return { id: row.sourceId, offset: row.sourceEnd }
|
|
1220
|
+
const span = row.sourceEnd - row.sourceStart
|
|
1221
|
+
const rawOffset = row.sourceStart + Math.round((relative / bodyCols) * span)
|
|
1222
|
+
return { id: row.sourceId, offset: clampCodeUnitBoundary(source, rawOffset) }
|
|
1223
|
+
}
|
|
1104
1224
|
return {
|
|
1105
1225
|
id: row.sourceId,
|
|
1106
1226
|
offset: offsetAtColumn(source, row.sourceStart, row.sourceEnd, relative),
|
|
@@ -1115,7 +1235,16 @@ export class Transcript {
|
|
|
1115
1235
|
const source = this.sourceTexts.get(row.sourceId)
|
|
1116
1236
|
if (source === undefined) return 0
|
|
1117
1237
|
const clamped = Math.max(row.sourceStart, Math.min(offset, row.sourceEnd))
|
|
1118
|
-
|
|
1238
|
+
if (
|
|
1239
|
+
row.plainBody !== undefined &&
|
|
1240
|
+
source.slice(row.sourceStart, row.sourceEnd) !== row.plainBody
|
|
1241
|
+
) {
|
|
1242
|
+
const span = row.sourceEnd - row.sourceStart
|
|
1243
|
+
const bodyCols = Math.max(0, row.displayEnd - row.displayStart)
|
|
1244
|
+
const ratio = span <= 0 ? 0 : (clamped - row.sourceStart) / span
|
|
1245
|
+
return row.displayStart + Math.round(ratio * bodyCols)
|
|
1246
|
+
}
|
|
1247
|
+
return row.displayStart + displayWidth(source.slice(row.sourceStart, clamped))
|
|
1119
1248
|
}
|
|
1120
1249
|
|
|
1121
1250
|
comparePoints(left: TranscriptPoint, right: TranscriptPoint): number {
|
|
@@ -1675,7 +1804,7 @@ export class Transcript {
|
|
|
1675
1804
|
.filter(Boolean)
|
|
1676
1805
|
.join(", ")
|
|
1677
1806
|
const statPart = stats ? ` · ${stats}` : ""
|
|
1678
|
-
const line = ` ${
|
|
1807
|
+
const line = ` ${t("panel.tools", { n: fold.length })}${namePart}${statPart}`
|
|
1679
1808
|
this.append(line, { kind: "activity" })
|
|
1680
1809
|
}
|
|
1681
1810
|
|
|
@@ -22,6 +22,7 @@ export interface ExecutorDeps {
|
|
|
22
22
|
readonly state: Readonly<SessionState>;
|
|
23
23
|
readonly maxResultTokens: number;
|
|
24
24
|
readonly cwd?: string;
|
|
25
|
+
readonly sessionId?: string;
|
|
25
26
|
readonly permissionMode?: string;
|
|
26
27
|
}
|
|
27
28
|
|
|
@@ -78,6 +79,7 @@ export async function runCall(call: ToolCall, deps: ExecutorDeps): Promise<ToolR
|
|
|
78
79
|
state: snapshotState(deps.state),
|
|
79
80
|
emit: deps.events.emit,
|
|
80
81
|
cwd: deps.cwd,
|
|
82
|
+
sessionId: deps.sessionId,
|
|
81
83
|
permissionMode: deps.permissionMode,
|
|
82
84
|
};
|
|
83
85
|
// Started and completed events carry separate result objects so listeners
|
|
@@ -141,13 +141,14 @@ export async function executeTurn(
|
|
|
141
141
|
);
|
|
142
142
|
}
|
|
143
143
|
const before = s.store.messages.length;
|
|
144
|
+
const beforeTokens = contextTokens(s);
|
|
144
145
|
await compactStore(s, signal);
|
|
145
146
|
budgetCompactions++;
|
|
146
|
-
|
|
147
|
-
//
|
|
148
|
-
//
|
|
149
|
-
//
|
|
150
|
-
reduced = s.store.messages.length < before;
|
|
147
|
+
const afterTokens = contextTokens(s);
|
|
148
|
+
// Ukur kemajuan nyata lewat penurunan token efektif ATAU penyusutan jumlah pesan:
|
|
149
|
+
// ringkasan isi pesan yang memadatkan payload tanpa mengubah panjang array
|
|
150
|
+
// tetap dihitung progres nyata.
|
|
151
|
+
reduced = afterTokens < beforeTokens || s.store.messages.length < before;
|
|
151
152
|
s.events.emit({
|
|
152
153
|
type: "context:compacted",
|
|
153
154
|
reason: reduced ? `pressure:${pressure}` : `pressure:${pressure}:no-op`,
|
|
@@ -329,6 +330,7 @@ export async function executeTurn(
|
|
|
329
330
|
state: snapshotState(s.state),
|
|
330
331
|
maxResultTokens: s.toolResultMaxTokens,
|
|
331
332
|
cwd: s.cwd,
|
|
333
|
+
...(s.sessionId ? { sessionId: s.sessionId } : {}),
|
|
332
334
|
// Resolved per turn so a live getter reflects runtime mode changes
|
|
333
335
|
// in the next tool context. Failed resolution is undefined (safe).
|
|
334
336
|
permissionMode: resolvePermissionMode(s.permissionMode),
|
|
@@ -505,5 +507,14 @@ async function compactStore(s: SessionInternal, signal: AbortSignal): Promise<vo
|
|
|
505
507
|
s.events.emit({ type: "context:compacted", reason: `llm-failed:${reason}` })
|
|
506
508
|
}
|
|
507
509
|
}
|
|
508
|
-
|
|
510
|
+
try {
|
|
511
|
+
s.store.replace(0, s.store.messages.length, s.compaction.compact(s.store, opts));
|
|
512
|
+
} catch (err) {
|
|
513
|
+
if (err instanceof AgentError) throw err;
|
|
514
|
+
throw new AgentError(
|
|
515
|
+
"budget_exceeded",
|
|
516
|
+
err instanceof Error ? err.message : String(err),
|
|
517
|
+
{ cause: err },
|
|
518
|
+
);
|
|
519
|
+
}
|
|
509
520
|
}
|
|
@@ -39,6 +39,7 @@ export interface SessionConfig {
|
|
|
39
39
|
systemForContext?: () => string;
|
|
40
40
|
model?: string;
|
|
41
41
|
cwd?: string;
|
|
42
|
+
sessionId?: string;
|
|
42
43
|
/**
|
|
43
44
|
* Session permission mode, forwarded to each turn's ToolContext so tools
|
|
44
45
|
* can adapt their behavior. Plain string or live getter — prefer the
|
|
@@ -156,6 +157,7 @@ export interface SessionInternal {
|
|
|
156
157
|
readonly systemForContext?: () => string;
|
|
157
158
|
readonly model?: string;
|
|
158
159
|
readonly cwd?: string;
|
|
160
|
+
readonly sessionId?: string;
|
|
159
161
|
readonly permissionMode?: string | (() => string);
|
|
160
162
|
readonly budget: BudgetPolicy;
|
|
161
163
|
readonly estimator: TokenEstimator;
|
|
@@ -200,6 +202,7 @@ export function createSession(config: SessionConfig): Session {
|
|
|
200
202
|
systemForContext: config.systemForContext,
|
|
201
203
|
model: config.model,
|
|
202
204
|
cwd: config.cwd,
|
|
205
|
+
sessionId: config.sessionId,
|
|
203
206
|
permissionMode: config.permissionMode,
|
|
204
207
|
budget: config.budget ?? defaultBudgetPolicy,
|
|
205
208
|
estimator: config.estimator ?? defaultTokenEstimator,
|
|
@@ -23,6 +23,7 @@ export interface ToolContext {
|
|
|
23
23
|
readonly signal: AbortSignal;
|
|
24
24
|
readonly state: Readonly<SessionState>;
|
|
25
25
|
readonly cwd?: string;
|
|
26
|
+
readonly sessionId?: string;
|
|
26
27
|
/**
|
|
27
28
|
* Session permission mode at dispatch time (e.g. "auto" | "plan").
|
|
28
29
|
* Optional for backward compatibility; tools that adapt to read-only
|
|
@@ -131,6 +131,27 @@ export function createOpenAICompatProvider(config: OpenAICompatConfig): ModelPro
|
|
|
131
131
|
// the accumulated tool calls and the finish event only once, otherwise
|
|
132
132
|
// the kernel would see (and execute) the same tool call twice.
|
|
133
133
|
let finished = false;
|
|
134
|
+
const flushCalls = function* (): Generator<ProviderEvent> {
|
|
135
|
+
for (const index of order) {
|
|
136
|
+
const acc = calls.get(index) as unknown as { id: string; name: string; args: string; _extra?: unknown };
|
|
137
|
+
if (!acc || !acc.name) continue;
|
|
138
|
+
// Argumen kosong ("") pada tool tanpa parameter harus menjadi {}, bukan ""
|
|
139
|
+
// (sejajar sse-bounds.ts), supaya validateArgs tidak menolak "expected an object".
|
|
140
|
+
let args: unknown = {};
|
|
141
|
+
if (acc.args.trim()) {
|
|
142
|
+
try {
|
|
143
|
+
args = JSON.parse(acc.args);
|
|
144
|
+
} catch {
|
|
145
|
+
args = { raw: acc.args };
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
// Args stay clean (the executor rejects unknown properties);
|
|
149
|
+
// provider metadata is filed in the side-map by call id.
|
|
150
|
+
const callId = acc.id || `call_${index}`;
|
|
151
|
+
if (acc._extra != null) stashProviderMeta(callId, acc._extra);
|
|
152
|
+
yield { type: "tool_call", id: callId, name: acc.name, args };
|
|
153
|
+
}
|
|
154
|
+
};
|
|
134
155
|
for await (const chunk of sse(response.body, signal)) {
|
|
135
156
|
const usage = chunk.usage as {
|
|
136
157
|
prompt_tokens?: number;
|
|
@@ -151,6 +172,7 @@ export function createOpenAICompatProvider(config: OpenAICompatConfig): ModelPro
|
|
|
151
172
|
inputTokens: usage.prompt_tokens,
|
|
152
173
|
outputTokens: usage.completion_tokens,
|
|
153
174
|
totalTokens: usage.total_tokens,
|
|
175
|
+
cacheIncluded: true,
|
|
154
176
|
...(typeof cached === "number" ? { cacheReadTokens: cached } : {}),
|
|
155
177
|
},
|
|
156
178
|
};
|
|
@@ -226,23 +248,7 @@ export function createOpenAICompatProvider(config: OpenAICompatConfig): ModelPro
|
|
|
226
248
|
}
|
|
227
249
|
if (!finished && first.finish_reason) {
|
|
228
250
|
finished = true;
|
|
229
|
-
|
|
230
|
-
const acc = calls.get(index) as unknown as { id: string; name: string; args: string; _extra?: unknown };
|
|
231
|
-
if (!acc) continue;
|
|
232
|
-
let args: unknown = acc.args;
|
|
233
|
-
if (acc.args) {
|
|
234
|
-
try {
|
|
235
|
-
args = JSON.parse(acc.args);
|
|
236
|
-
} catch {
|
|
237
|
-
args = { raw: acc.args };
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
// Args stay clean (the executor rejects unknown properties);
|
|
241
|
-
// provider metadata is filed in the side-map by call id.
|
|
242
|
-
const callId = acc.id || `call_${index}`
|
|
243
|
-
if (acc._extra != null) stashProviderMeta(callId, acc._extra);
|
|
244
|
-
yield { type: "tool_call", id: callId, name: acc.name, args };
|
|
245
|
-
}
|
|
251
|
+
yield* flushCalls();
|
|
246
252
|
// P6: content_filter (OpenAI safety filter) must not be invisible as
|
|
247
253
|
// a plain "stop" — surface it as an extension event so observers can
|
|
248
254
|
// distinguish a blocked response from a normal completion.
|
|
@@ -253,9 +259,12 @@ export function createOpenAICompatProvider(config: OpenAICompatConfig): ModelPro
|
|
|
253
259
|
}
|
|
254
260
|
}
|
|
255
261
|
// Provider quirky bisa berakhir tanpa finish_reason (mis. [DONE] langsung).
|
|
256
|
-
//
|
|
262
|
+
// Jika sudah ada tool delta terakumulasi, flush dulu + finish("tool_calls")
|
|
263
|
+
// agar tool call tidak hilang diam-diam.
|
|
257
264
|
if (!finished) {
|
|
258
|
-
|
|
265
|
+
const hasCalls = order.some((index) => Boolean(calls.get(index)?.name));
|
|
266
|
+
yield* flushCalls();
|
|
267
|
+
yield { type: "finish", reason: hasCalls ? "tool_calls" : "stop" };
|
|
259
268
|
}
|
|
260
269
|
},
|
|
261
270
|
};
|