minicode-ai 0.27.0 → 0.30.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 +1 -1
- package/cli/args.ts +184 -57
- package/cli/bootstrap/presentation-bridge.ts +383 -0
- package/cli/commands/acp.ts +45 -29
- 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 +18 -15
- package/cli/commands/exec.ts +41 -89
- 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 +51 -100
- package/cli/model-manager.ts +7 -2
- package/cli/provider-manager.ts +33 -18
- package/cli/router.ts +41 -12
- package/cli/setup.ts +241 -485
- package/cli/tui.ts +303 -100
- package/docs/TERMINAL_CONTRACT.md +258 -73
- package/docs/UI_RENDER_PIPELINE.md +9 -1
- package/docs/agents.md +2 -2
- package/docs/architecture.md +17 -32
- 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 +144 -68
- package/src/app/tool-layer.ts +38 -16
- 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/atomic-write.ts +31 -59
- 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/jail.ts +6 -8
- 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/anthropic.ts +16 -5
- package/src/providers/provision.ts +6 -56
- package/src/providers/responses.ts +38 -7
- package/src/providers/sse-bounds.ts +6 -4
- 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/db/runs.ts +397 -0
- package/src/session/db/schema.ts +358 -0
- package/src/session/open-work.ts +26 -3
- package/src/session/persistence.ts +74 -998
- package/src/session/verification.ts +170 -20
- package/src/skills/loader.ts +31 -16
- package/src/task/db/schema.ts +325 -0
- package/src/task/db/types.ts +269 -0
- package/src/task/production-scheduler.ts +13 -1
- package/src/task/store.ts +92 -1021
- 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 +158 -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 +35 -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 {
|
|
@@ -652,6 +675,7 @@ export class Transcript {
|
|
|
652
675
|
this.thinkingBuf = ""
|
|
653
676
|
this.thinkingTail = ""
|
|
654
677
|
this.thinkingArea = []
|
|
678
|
+
this.thinkingRowId = null
|
|
655
679
|
this.evicted = 0
|
|
656
680
|
this.hasEvictMarker = false
|
|
657
681
|
this.runningMemo = null
|
|
@@ -705,27 +729,108 @@ export class Transcript {
|
|
|
705
729
|
sourceBase += source.length + 1
|
|
706
730
|
continue
|
|
707
731
|
}
|
|
732
|
+
const isBoundaryHeader = markdown && fenceState.startsWith("open:")
|
|
708
733
|
const renderedRows = rendered === "" ? [""] : wrapByWidth(rendered, w)
|
|
709
|
-
const
|
|
710
|
-
|
|
711
|
-
const
|
|
712
|
-
|
|
734
|
+
const plainRenderedFull = stripSgr(sanitizeAnsi(rendered))
|
|
735
|
+
const expectedExact = `${sourcePrefix}${source}`
|
|
736
|
+
const exactMatch = !isBoundaryHeader && (!markdown || plainRenderedFull === expectedExact)
|
|
737
|
+
const prefixTrimmed = sourcePrefix.trimEnd()
|
|
738
|
+
const rowBodies: Array<{
|
|
739
|
+
text: string
|
|
740
|
+
plainBody: string
|
|
741
|
+
displayStart: number
|
|
742
|
+
displayEnd: number
|
|
743
|
+
}> = []
|
|
744
|
+
for (let rowIndex = 0; rowIndex < renderedRows.length; rowIndex++) {
|
|
713
745
|
const renderedRow = renderedRows[rowIndex] ?? ""
|
|
714
|
-
const
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
746
|
+
const plainRendered = stripSgr(sanitizeAnsi(renderedRow))
|
|
747
|
+
let rowDisplayStart = 0
|
|
748
|
+
let plainBody = plainRendered
|
|
749
|
+
if (sourcePrefix && rowIndex === 0) {
|
|
750
|
+
if (plainRendered.startsWith(sourcePrefix)) {
|
|
751
|
+
rowDisplayStart = displayWidth(sourcePrefix)
|
|
752
|
+
plainBody = plainRendered.slice(sourcePrefix.length)
|
|
753
|
+
} else if (
|
|
754
|
+
prefixTrimmed &&
|
|
755
|
+
plainRendered.startsWith(prefixTrimmed) &&
|
|
756
|
+
plainRendered.length <= sourcePrefix.length
|
|
757
|
+
) {
|
|
758
|
+
// Terminal sempit memecah tepat di ujung chrome prefix ("minicode ›").
|
|
759
|
+
rowDisplayStart = displayWidth(plainRendered)
|
|
760
|
+
plainBody = ""
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
rowBodies.push({
|
|
720
764
|
text: renderedRow,
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
sourceEnd: offset + sourceRow.length,
|
|
724
|
-
displayStart: 0,
|
|
765
|
+
plainBody,
|
|
766
|
+
displayStart: rowDisplayStart,
|
|
725
767
|
displayEnd: displayWidth(renderedRow),
|
|
726
|
-
selectable: selectable && sourceId >= 0,
|
|
727
768
|
})
|
|
728
|
-
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
const rowSelectable = selectable && sourceId >= 0 && !isBoundaryHeader
|
|
772
|
+
if (exactMatch) {
|
|
773
|
+
let cursorInSource = 0
|
|
774
|
+
for (let rowIndex = 0; rowIndex < rowBodies.length; rowIndex++) {
|
|
775
|
+
const item = rowBodies[rowIndex]!
|
|
776
|
+
if (
|
|
777
|
+
rowIndex > 0 &&
|
|
778
|
+
item.plainBody.length > 0 &&
|
|
779
|
+
source.slice(cursorInSource).startsWith(" ") &&
|
|
780
|
+
!item.plainBody.startsWith(" ") &&
|
|
781
|
+
source.slice(cursorInSource + 1).startsWith(item.plainBody)
|
|
782
|
+
) {
|
|
783
|
+
cursorInSource += 1
|
|
784
|
+
}
|
|
785
|
+
const start = sourceBase + cursorInSource
|
|
786
|
+
const remaining = Math.max(0, source.length - cursorInSource)
|
|
787
|
+
const take =
|
|
788
|
+
rowIndex === rowBodies.length - 1
|
|
789
|
+
? remaining
|
|
790
|
+
: Math.min(item.plainBody.length, remaining)
|
|
791
|
+
cursorInSource += take
|
|
792
|
+
const end = sourceBase + cursorInSource
|
|
793
|
+
out.push({
|
|
794
|
+
text: item.text,
|
|
795
|
+
sourceId: rowSelectable ? sourceId : -1,
|
|
796
|
+
sourceStart: start,
|
|
797
|
+
sourceEnd: end,
|
|
798
|
+
displayStart: item.displayStart,
|
|
799
|
+
displayEnd: item.displayEnd,
|
|
800
|
+
selectable: rowSelectable,
|
|
801
|
+
})
|
|
802
|
+
}
|
|
803
|
+
} else {
|
|
804
|
+
// Inline markdown mengubah panjang tampilan vs sumber (`**bold**`,
|
|
805
|
+
// `[label](url)`, heading `## `): petakan proporsional ke kolom visual
|
|
806
|
+
// agar highlight seleksi dan copy tidak meleset di baris yang sama.
|
|
807
|
+
const colWidths = rowBodies.map((item) => Math.max(0, item.displayEnd - item.displayStart))
|
|
808
|
+
const totalCols = colWidths.reduce((sum, v) => sum + v, 0)
|
|
809
|
+
let prevPos = 0
|
|
810
|
+
let accCols = 0
|
|
811
|
+
for (let rowIndex = 0; rowIndex < rowBodies.length; rowIndex++) {
|
|
812
|
+
const item = rowBodies[rowIndex]!
|
|
813
|
+
accCols += colWidths[rowIndex]!
|
|
814
|
+
const nextPos =
|
|
815
|
+
isBoundaryHeader || !rowSelectable
|
|
816
|
+
? 0
|
|
817
|
+
: rowIndex === rowBodies.length - 1 || totalCols === 0
|
|
818
|
+
? source.length
|
|
819
|
+
: clampCodeUnitBoundary(source, Math.round((accCols / totalCols) * source.length))
|
|
820
|
+
const start = sourceBase + prevPos
|
|
821
|
+
const end = sourceBase + Math.max(prevPos, nextPos)
|
|
822
|
+
prevPos = Math.max(prevPos, nextPos)
|
|
823
|
+
out.push({
|
|
824
|
+
text: item.text,
|
|
825
|
+
sourceId: rowSelectable ? sourceId : -1,
|
|
826
|
+
sourceStart: start,
|
|
827
|
+
sourceEnd: end,
|
|
828
|
+
displayStart: item.displayStart,
|
|
829
|
+
displayEnd: item.displayEnd,
|
|
830
|
+
selectable: rowSelectable,
|
|
831
|
+
...(rowSelectable ? { plainBody: item.plainBody } : {}),
|
|
832
|
+
})
|
|
833
|
+
}
|
|
729
834
|
}
|
|
730
835
|
sourceBase += source.length + 1
|
|
731
836
|
}
|
|
@@ -744,12 +849,17 @@ export class Transcript {
|
|
|
744
849
|
base += row.length + 1
|
|
745
850
|
}
|
|
746
851
|
const plainRows = renderedRows.map((row) => stripSgr(sanitizeAnsi(row)))
|
|
747
|
-
|
|
852
|
+
// Border Unicode (─/box drawing) DAN fallback ASCII (`+---+`) harus
|
|
853
|
+
// dikenali. Tanpa varian ASCII, baris `+---+` dianggap data: ia menjadi
|
|
854
|
+
// selectable dengan offset yang salah dan seleksi tabel kehilangan teks.
|
|
855
|
+
const isBorderRow = (row: string): boolean =>
|
|
856
|
+
/^[\s─━┌┐└┘├┤┬┴┼]+$/.test(row) || /^[\s+-]+$/.test(row)
|
|
857
|
+
const horizontal = plainRows.some(isBorderRow)
|
|
748
858
|
const fields = Math.max(1, table.headers.length)
|
|
749
859
|
let logicalIndex = 0
|
|
750
860
|
return renderedRows.map((row, index) => {
|
|
751
861
|
const plain = plainRows[index] ?? ""
|
|
752
|
-
const isBorder =
|
|
862
|
+
const isBorder = isBorderRow(plain)
|
|
753
863
|
let range = ranges[0] ?? { start: 0, end: 0 }
|
|
754
864
|
if (horizontal) {
|
|
755
865
|
if (!isBorder) range = ranges[logicalIndex] ?? range
|
|
@@ -785,7 +895,7 @@ export class Transcript {
|
|
|
785
895
|
open = true
|
|
786
896
|
char = fence.char
|
|
787
897
|
len = fence.len
|
|
788
|
-
return "boundary"
|
|
898
|
+
return fence.lang ? `open:${fence.lang}` : "boundary"
|
|
789
899
|
}
|
|
790
900
|
if (open && fence && fence.char === char && fence.len >= len) {
|
|
791
901
|
open = false
|
|
@@ -1101,6 +1211,17 @@ export class Transcript {
|
|
|
1101
1211
|
const source = this.sourceTexts.get(row.sourceId)
|
|
1102
1212
|
if (source === undefined) return null
|
|
1103
1213
|
const relative = Math.max(0, column - row.displayStart)
|
|
1214
|
+
if (
|
|
1215
|
+
row.plainBody !== undefined &&
|
|
1216
|
+
source.slice(row.sourceStart, row.sourceEnd) !== row.plainBody
|
|
1217
|
+
) {
|
|
1218
|
+
const bodyCols = Math.max(0, row.displayEnd - row.displayStart)
|
|
1219
|
+
if (bodyCols === 0 || relative === 0) return { id: row.sourceId, offset: row.sourceStart }
|
|
1220
|
+
if (relative >= bodyCols) return { id: row.sourceId, offset: row.sourceEnd }
|
|
1221
|
+
const span = row.sourceEnd - row.sourceStart
|
|
1222
|
+
const rawOffset = row.sourceStart + Math.round((relative / bodyCols) * span)
|
|
1223
|
+
return { id: row.sourceId, offset: clampCodeUnitBoundary(source, rawOffset) }
|
|
1224
|
+
}
|
|
1104
1225
|
return {
|
|
1105
1226
|
id: row.sourceId,
|
|
1106
1227
|
offset: offsetAtColumn(source, row.sourceStart, row.sourceEnd, relative),
|
|
@@ -1115,7 +1236,16 @@ export class Transcript {
|
|
|
1115
1236
|
const source = this.sourceTexts.get(row.sourceId)
|
|
1116
1237
|
if (source === undefined) return 0
|
|
1117
1238
|
const clamped = Math.max(row.sourceStart, Math.min(offset, row.sourceEnd))
|
|
1118
|
-
|
|
1239
|
+
if (
|
|
1240
|
+
row.plainBody !== undefined &&
|
|
1241
|
+
source.slice(row.sourceStart, row.sourceEnd) !== row.plainBody
|
|
1242
|
+
) {
|
|
1243
|
+
const span = row.sourceEnd - row.sourceStart
|
|
1244
|
+
const bodyCols = Math.max(0, row.displayEnd - row.displayStart)
|
|
1245
|
+
const ratio = span <= 0 ? 0 : (clamped - row.sourceStart) / span
|
|
1246
|
+
return row.displayStart + Math.round(ratio * bodyCols)
|
|
1247
|
+
}
|
|
1248
|
+
return row.displayStart + displayWidth(source.slice(row.sourceStart, clamped))
|
|
1119
1249
|
}
|
|
1120
1250
|
|
|
1121
1251
|
comparePoints(left: TranscriptPoint, right: TranscriptPoint): number {
|
|
@@ -1675,7 +1805,7 @@ export class Transcript {
|
|
|
1675
1805
|
.filter(Boolean)
|
|
1676
1806
|
.join(", ")
|
|
1677
1807
|
const statPart = stats ? ` · ${stats}` : ""
|
|
1678
|
-
const line = ` ${
|
|
1808
|
+
const line = ` ${t("panel.tools", { n: fold.length })}${namePart}${statPart}`
|
|
1679
1809
|
this.append(line, { kind: "activity" })
|
|
1680
1810
|
}
|
|
1681
1811
|
|
|
@@ -1755,6 +1885,9 @@ export class Transcript {
|
|
|
1755
1885
|
if (index < 0) return false
|
|
1756
1886
|
this.lines[index] = line
|
|
1757
1887
|
this.rowCache.delete(id)
|
|
1888
|
+
this.committedProjection = null
|
|
1889
|
+
this.projectedBase = null
|
|
1890
|
+
this.runningMemo = null
|
|
1758
1891
|
return true
|
|
1759
1892
|
}
|
|
1760
1893
|
|
|
@@ -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,7 +131,35 @@ 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)) {
|
|
156
|
+
if (chunk.error && (typeof chunk.error === "object" || typeof chunk.error === "string")) {
|
|
157
|
+
const e = typeof chunk.error === "object" ? (chunk.error as Record<string, unknown>) : { message: String(chunk.error) };
|
|
158
|
+
const msg = String(e.message ?? e.msg ?? JSON.stringify(chunk.error));
|
|
159
|
+
const c = String(e.code ?? e.type ?? "");
|
|
160
|
+
const cat = /auth|401|403/i.test(msg + c) ? "auth" : /rate|quota|429/i.test(msg + c) ? "rate_limit" : /context/i.test(msg) ? "context_length_exceeded" : "server";
|
|
161
|
+
throw new ProviderError(cat, `${cat === "server" ? "server error" : cat}: ${msg}`);
|
|
162
|
+
}
|
|
135
163
|
const usage = chunk.usage as {
|
|
136
164
|
prompt_tokens?: number;
|
|
137
165
|
completion_tokens?: number;
|
|
@@ -151,6 +179,7 @@ export function createOpenAICompatProvider(config: OpenAICompatConfig): ModelPro
|
|
|
151
179
|
inputTokens: usage.prompt_tokens,
|
|
152
180
|
outputTokens: usage.completion_tokens,
|
|
153
181
|
totalTokens: usage.total_tokens,
|
|
182
|
+
cacheIncluded: true,
|
|
154
183
|
...(typeof cached === "number" ? { cacheReadTokens: cached } : {}),
|
|
155
184
|
},
|
|
156
185
|
};
|
|
@@ -226,23 +255,7 @@ export function createOpenAICompatProvider(config: OpenAICompatConfig): ModelPro
|
|
|
226
255
|
}
|
|
227
256
|
if (!finished && first.finish_reason) {
|
|
228
257
|
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
|
-
}
|
|
258
|
+
yield* flushCalls();
|
|
246
259
|
// P6: content_filter (OpenAI safety filter) must not be invisible as
|
|
247
260
|
// a plain "stop" — surface it as an extension event so observers can
|
|
248
261
|
// distinguish a blocked response from a normal completion.
|
|
@@ -253,9 +266,12 @@ export function createOpenAICompatProvider(config: OpenAICompatConfig): ModelPro
|
|
|
253
266
|
}
|
|
254
267
|
}
|
|
255
268
|
// Provider quirky bisa berakhir tanpa finish_reason (mis. [DONE] langsung).
|
|
256
|
-
//
|
|
269
|
+
// Jika sudah ada tool delta terakumulasi, flush dulu + finish("tool_calls")
|
|
270
|
+
// agar tool call tidak hilang diam-diam.
|
|
257
271
|
if (!finished) {
|
|
258
|
-
|
|
272
|
+
const hasCalls = order.some((index) => Boolean(calls.get(index)?.name));
|
|
273
|
+
yield* flushCalls();
|
|
274
|
+
yield { type: "finish", reason: hasCalls ? "tool_calls" : "stop" };
|
|
259
275
|
}
|
|
260
276
|
},
|
|
261
277
|
};
|