mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-03-31 14:22:27 +00:00
Part data model (#950)
This commit is contained in:
@@ -9,6 +9,7 @@ import { Config } from "../../config/config"
|
||||
import { bootstrap } from "../bootstrap"
|
||||
import { MessageV2 } from "../../session/message-v2"
|
||||
import { Mode } from "../../session/mode"
|
||||
import { Identifier } from "../../id/id"
|
||||
|
||||
const TOOL: Record<string, [string, string]> = {
|
||||
todowrite: ["Todo", UI.Style.TEXT_WARNING_BOLD],
|
||||
@@ -83,14 +84,9 @@ export const RunCommand = cmd({
|
||||
return
|
||||
}
|
||||
|
||||
const isPiped = !process.stdout.isTTY
|
||||
|
||||
UI.empty()
|
||||
UI.println(UI.logo())
|
||||
UI.empty()
|
||||
const displayMessage = message.length > 300 ? message.slice(0, 300) + "..." : message
|
||||
UI.println(UI.Style.TEXT_NORMAL_BOLD + "> ", displayMessage)
|
||||
UI.empty()
|
||||
|
||||
const cfg = await Config.get()
|
||||
if (cfg.share === "auto" || Flag.OPENCODE_AUTO_SHARE || args.share) {
|
||||
@@ -120,8 +116,10 @@ export const RunCommand = cmd({
|
||||
)
|
||||
}
|
||||
|
||||
let text = ""
|
||||
Bus.subscribe(MessageV2.Event.PartUpdated, async (evt) => {
|
||||
if (evt.properties.sessionID !== session.id) return
|
||||
if (evt.properties.part.sessionID !== session.id) return
|
||||
if (evt.properties.part.messageID === messageID) return
|
||||
const part = evt.properties.part
|
||||
|
||||
if (part.type === "tool" && part.state.status === "completed") {
|
||||
@@ -130,13 +128,15 @@ export const RunCommand = cmd({
|
||||
}
|
||||
|
||||
if (part.type === "text") {
|
||||
if (part.text.includes("\n")) {
|
||||
text = part.text
|
||||
|
||||
if (part.time?.end) {
|
||||
UI.empty()
|
||||
UI.println(part.text)
|
||||
UI.println(UI.markdown(text))
|
||||
UI.empty()
|
||||
text = ""
|
||||
return
|
||||
}
|
||||
printEvent(UI.Style.TEXT_NORMAL_BOLD, "Text", part.text)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -156,8 +156,10 @@ export const RunCommand = cmd({
|
||||
|
||||
const mode = args.mode ? await Mode.get(args.mode) : await Mode.list().then((x) => x[0])
|
||||
|
||||
const messageID = Identifier.ascending("message")
|
||||
const result = await Session.chat({
|
||||
sessionID: session.id,
|
||||
messageID,
|
||||
...(mode.model
|
||||
? mode.model
|
||||
: {
|
||||
@@ -167,15 +169,19 @@ export const RunCommand = cmd({
|
||||
mode: mode.name,
|
||||
parts: [
|
||||
{
|
||||
id: Identifier.ascending("part"),
|
||||
sessionID: session.id,
|
||||
messageID: messageID,
|
||||
type: "text",
|
||||
text: message,
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
const isPiped = !process.stdout.isTTY
|
||||
if (isPiped) {
|
||||
const match = result.parts.findLast((x) => x.type === "text")
|
||||
if (match) process.stdout.write(match.text)
|
||||
if (match) process.stdout.write(UI.markdown(match.text))
|
||||
if (errorMsg) process.stdout.write(errorMsg)
|
||||
}
|
||||
UI.empty()
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
import { Storage } from "../../storage/storage"
|
||||
import { MessageV2 } from "../../session/message-v2"
|
||||
import { cmd } from "./cmd"
|
||||
import { bootstrap } from "../bootstrap"
|
||||
|
||||
interface SessionStats {
|
||||
totalSessions: number
|
||||
@@ -27,87 +24,10 @@ interface SessionStats {
|
||||
|
||||
export const StatsCommand = cmd({
|
||||
command: "stats",
|
||||
handler: async () => {
|
||||
await bootstrap({ cwd: process.cwd() }, async () => {
|
||||
const stats: SessionStats = {
|
||||
totalSessions: 0,
|
||||
totalMessages: 0,
|
||||
totalCost: 0,
|
||||
totalTokens: {
|
||||
input: 0,
|
||||
output: 0,
|
||||
reasoning: 0,
|
||||
cache: {
|
||||
read: 0,
|
||||
write: 0,
|
||||
},
|
||||
},
|
||||
toolUsage: {},
|
||||
dateRange: {
|
||||
earliest: Date.now(),
|
||||
latest: 0,
|
||||
},
|
||||
days: 0,
|
||||
costPerDay: 0,
|
||||
}
|
||||
|
||||
const sessionMap = new Map<string, number>()
|
||||
|
||||
try {
|
||||
for await (const messagePath of Storage.list("session/message")) {
|
||||
try {
|
||||
const message = await Storage.readJSON<MessageV2.Info>(messagePath)
|
||||
if (!message.parts.find((part) => part.type === "step-finish")) continue
|
||||
|
||||
stats.totalMessages++
|
||||
|
||||
const sessionId = message.sessionID
|
||||
sessionMap.set(sessionId, (sessionMap.get(sessionId) || 0) + 1)
|
||||
|
||||
if (message.time.created < stats.dateRange.earliest) {
|
||||
stats.dateRange.earliest = message.time.created
|
||||
}
|
||||
if (message.time.created > stats.dateRange.latest) {
|
||||
stats.dateRange.latest = message.time.created
|
||||
}
|
||||
|
||||
if (message.role === "assistant") {
|
||||
stats.totalCost += message.cost
|
||||
stats.totalTokens.input += message.tokens.input
|
||||
stats.totalTokens.output += message.tokens.output
|
||||
stats.totalTokens.reasoning += message.tokens.reasoning
|
||||
stats.totalTokens.cache.read += message.tokens.cache.read
|
||||
stats.totalTokens.cache.write += message.tokens.cache.write
|
||||
|
||||
for (const part of message.parts) {
|
||||
if (part.type === "tool") {
|
||||
stats.toolUsage[part.tool] = (stats.toolUsage[part.tool] || 0) + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
continue
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Failed to read storage:", e)
|
||||
return
|
||||
}
|
||||
|
||||
stats.totalSessions = sessionMap.size
|
||||
|
||||
if (stats.dateRange.latest > 0) {
|
||||
const daysDiff = (stats.dateRange.latest - stats.dateRange.earliest) / (1000 * 60 * 60 * 24)
|
||||
stats.days = Math.max(1, Math.ceil(daysDiff))
|
||||
stats.costPerDay = stats.totalCost / stats.days
|
||||
}
|
||||
|
||||
displayStats(stats)
|
||||
})
|
||||
},
|
||||
handler: async () => {},
|
||||
})
|
||||
|
||||
function displayStats(stats: SessionStats) {
|
||||
export function displayStats(stats: SessionStats) {
|
||||
const width = 56
|
||||
|
||||
function renderRow(label: string, value: string): string {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { z } from "zod"
|
||||
import { EOL } from "os"
|
||||
import { NamedError } from "../util/error"
|
||||
// @ts-ignore
|
||||
import cliMarkdown from "cli-markdown"
|
||||
|
||||
export namespace UI {
|
||||
const LOGO = [
|
||||
@@ -76,4 +78,18 @@ export namespace UI {
|
||||
export function error(message: string) {
|
||||
println(Style.TEXT_DANGER_BOLD + "Error: " + Style.TEXT_NORMAL + message)
|
||||
}
|
||||
|
||||
export function markdown(text: string): string {
|
||||
const rendered = cliMarkdown(text, {
|
||||
width: process.stdout.columns || 80,
|
||||
firstHeading: false,
|
||||
tab: 0,
|
||||
}).trim()
|
||||
|
||||
// Remove leading space from each line
|
||||
return rendered
|
||||
.split("\n")
|
||||
.map((line: string) => line.replace(/^ /, ""))
|
||||
.join("\n")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user