Add os/cline ver/host info to task metadata & change Task History -> EXPORT to just open the task directory (#7706)
Co-authored-by: Andrei Edell <andrei@nugbase.com>
This commit is contained in:
1 parent
164e11aae1
commit
81276fdf85
8 files changed
+116
-61
No files matched your search
@@ -15,7 +15,18 @@ export interface ModelMetadataEntry {
|
||||
mode: string
|
||||
}
|
||||
|
||||
export interface EnvironmentMetadataEntry {
|
||||
ts: number
|
||||
os_name: string
|
||||
os_version: string
|
||||
os_arch: string
|
||||
host_name: string
|
||||
host_version: string
|
||||
cline_version: string
|
||||
}
|
||||
|
||||
export interface TaskMetadata {
|
||||
files_in_context: FileMetadataEntry[]
|
||||
model_usage: ModelMetadataEntry[]
|
||||
environment_history: EnvironmentMetadataEntry[]
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
import { collectEnvironmentMetadata, getTaskMetadata, saveTaskMetadata } from "@core/storage/disk"
|
||||
import type { EnvironmentMetadataEntry } from "./ContextTrackerTypes"
|
||||
|
||||
export class EnvironmentContextTracker {
|
||||
readonly taskId: string
|
||||
|
||||
constructor(taskId: string) {
|
||||
this.taskId = taskId
|
||||
}
|
||||
|
||||
async recordEnvironment() {
|
||||
const metadata = await getTaskMetadata(this.taskId)
|
||||
|
||||
if (!metadata.environment_history) {
|
||||
metadata.environment_history = []
|
||||
}
|
||||
|
||||
const currentEnv = await collectEnvironmentMetadata()
|
||||
const currentEnvWithTs: EnvironmentMetadataEntry = {
|
||||
ts: Date.now(),
|
||||
...currentEnv,
|
||||
}
|
||||
|
||||
const lastEntry = metadata.environment_history[metadata.environment_history.length - 1]
|
||||
if (lastEntry && this.isSameEnvironment(lastEntry, currentEnvWithTs)) {
|
||||
return // No change, don't add duplicate
|
||||
}
|
||||
|
||||
metadata.environment_history.push(currentEnvWithTs)
|
||||
await saveTaskMetadata(this.taskId, metadata)
|
||||
}
|
||||
|
||||
private isSameEnvironment(a: EnvironmentMetadataEntry, b: EnvironmentMetadataEntry): boolean {
|
||||
return (
|
||||
a.os_name === b.os_name &&
|
||||
a.os_version === b.os_version &&
|
||||
a.os_arch === b.os_arch &&
|
||||
a.host_name === b.host_name &&
|
||||
a.host_version === b.host_version &&
|
||||
a.cline_version === b.cline_version
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -47,7 +47,7 @@ describe("FileContextTracker", () => {
|
||||
chokidarWatchStub = sandbox.stub(chokidar, "watch").returns(mockFileSystemWatcher as any)
|
||||
|
||||
// Mock disk module functions
|
||||
mockTaskMetadata = { files_in_context: [], model_usage: [] }
|
||||
mockTaskMetadata = { files_in_context: [], model_usage: [], environment_history: [] }
|
||||
getTaskMetadataStub = sandbox.stub(diskModule, "getTaskMetadata").resolves(mockTaskMetadata)
|
||||
saveTaskMetadataStub = sandbox.stub(diskModule, "saveTaskMetadata").resolves()
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ describe("ModelContextTracker", () => {
|
||||
sandbox = sinon.createSandbox()
|
||||
|
||||
// Mock disk module functions
|
||||
mockTaskMetadata = { files_in_context: [], model_usage: [] }
|
||||
mockTaskMetadata = { files_in_context: [], model_usage: [], environment_history: [] }
|
||||
getTaskMetadataStub = sandbox.stub(diskModule, "getTaskMetadata").resolves(mockTaskMetadata)
|
||||
saveTaskMetadataStub = sandbox.stub(diskModule, "saveTaskMetadata").resolves()
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ import { detectWorkspaceRoots } from "@core/workspace/detection"
|
||||
import { setupWorkspaceManager } from "@core/workspace/setup"
|
||||
import type { WorkspaceRootManager } from "@core/workspace/WorkspaceRootManager"
|
||||
import { cleanupLegacyCheckpoints } from "@integrations/checkpoints/CheckpointMigration"
|
||||
import { downloadTask } from "@integrations/misc/export-markdown"
|
||||
import { ClineAccountService } from "@services/account/ClineAccountService"
|
||||
import { McpHub } from "@services/mcp/McpHub"
|
||||
import type { ApiProvider, ModelInfo } from "@shared/api"
|
||||
@@ -20,6 +19,7 @@ import type { UserInfo } from "@shared/UserInfo"
|
||||
import { fileExistsAtPath } from "@utils/fs"
|
||||
import axios from "axios"
|
||||
import fs from "fs/promises"
|
||||
import open from "open"
|
||||
import pWaitFor from "p-wait-for"
|
||||
import * as path from "path"
|
||||
import type { FolderLockWithRetryResult } from "src/core/locks/types"
|
||||
@@ -824,8 +824,9 @@ export class Controller {
|
||||
}
|
||||
|
||||
async exportTaskWithId(id: string) {
|
||||
const { historyItem, apiConversationHistory } = await this.getTaskWithId(id)
|
||||
await downloadTask(historyItem.ts, apiConversationHistory)
|
||||
const { taskDirPath } = await this.getTaskWithId(id)
|
||||
console.log(`[EXPORT] Opening task directory: ${taskDirPath}`)
|
||||
await open(taskDirPath)
|
||||
}
|
||||
|
||||
async deleteTaskFromState(id: string) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Anthropic } from "@anthropic-ai/sdk"
|
||||
import { TaskMetadata } from "@core/context/context-tracking/ContextTrackerTypes"
|
||||
import { EnvironmentMetadataEntry, TaskMetadata } from "@core/context/context-tracking/ContextTrackerTypes"
|
||||
import { execa } from "@packages/execa"
|
||||
import { ClineMessage } from "@shared/ExtensionMessage"
|
||||
import { HistoryItem } from "@shared/HistoryItem"
|
||||
@@ -10,6 +10,7 @@ import fs from "fs/promises"
|
||||
import os from "os"
|
||||
import * as path from "path"
|
||||
import { HostProvider } from "@/hosts/host-provider"
|
||||
import { ExtensionRegistryInfo } from "@/registry"
|
||||
import { McpMarketplaceCatalog } from "@/shared/mcp"
|
||||
import { StateManager } from "./StateManager"
|
||||
|
||||
@@ -168,6 +169,37 @@ export async function saveClineMessages(taskId: string, uiMessages: ClineMessage
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects environment metadata for the current system and host.
|
||||
* This information is used for debugging and task portability.
|
||||
* Returns metadata without timestamp - timestamp is added by EnvironmentContextTracker.
|
||||
*/
|
||||
export async function collectEnvironmentMetadata(): Promise<Omit<EnvironmentMetadataEntry, "ts">> {
|
||||
try {
|
||||
const hostVersion = await HostProvider.env.getHostVersion({})
|
||||
|
||||
return {
|
||||
os_name: os.platform(),
|
||||
os_version: os.release(),
|
||||
os_arch: os.arch(),
|
||||
host_name: hostVersion.platform || "Unknown",
|
||||
host_version: hostVersion.version || "Unknown",
|
||||
cline_version: ExtensionRegistryInfo.version,
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to collect environment metadata:", error)
|
||||
// Return fallback values if collection fails
|
||||
return {
|
||||
os_name: os.platform(),
|
||||
os_version: os.release(),
|
||||
os_arch: os.arch(),
|
||||
host_name: "Unknown",
|
||||
host_version: "Unknown",
|
||||
cline_version: "Unknown",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function getTaskMetadata(taskId: string): Promise<TaskMetadata> {
|
||||
const filePath = path.join(await ensureTaskDirectoryExists(taskId), GlobalFileNames.taskMetadata)
|
||||
try {
|
||||
@@ -177,7 +209,7 @@ export async function getTaskMetadata(taskId: string): Promise<TaskMetadata> {
|
||||
} catch (error) {
|
||||
console.error("Failed to read task metadata:", error)
|
||||
}
|
||||
return { files_in_context: [], model_usage: [] }
|
||||
return { files_in_context: [], model_usage: [], environment_history: [] }
|
||||
}
|
||||
|
||||
export async function saveTaskMetadata(taskId: string, metadata: TaskMetadata) {
|
||||
|
||||
+18
-1
@@ -5,6 +5,7 @@ import { AssistantMessageContent, parseAssistantMessageV2, ToolUse } from "@core
|
||||
import { ContextManager } from "@core/context/context-management/ContextManager"
|
||||
import { checkContextWindowExceededError } from "@core/context/context-management/context-error-handling"
|
||||
import { getContextWindowInfo } from "@core/context/context-management/context-window-utils"
|
||||
import { EnvironmentContextTracker } from "@core/context/context-tracking/EnvironmentContextTracker"
|
||||
import { FileContextTracker } from "@core/context/context-tracking/FileContextTracker"
|
||||
import { ModelContextTracker } from "@core/context/context-tracking/ModelContextTracker"
|
||||
import {
|
||||
@@ -227,6 +228,7 @@ export class Task {
|
||||
// Metadata tracking
|
||||
private fileContextTracker: FileContextTracker
|
||||
private modelContextTracker: ModelContextTracker
|
||||
private environmentContextTracker: EnvironmentContextTracker
|
||||
|
||||
// Focus Chain
|
||||
private FocusChainManager?: FocusChainManager
|
||||
@@ -363,9 +365,10 @@ export class Task {
|
||||
updateTaskHistory: this.updateTaskHistory,
|
||||
})
|
||||
|
||||
// Initialize file context tracker
|
||||
// Initialize context trackers
|
||||
this.fileContextTracker = new FileContextTracker(controller, this.taskId)
|
||||
this.modelContextTracker = new ModelContextTracker(this.taskId)
|
||||
this.environmentContextTracker = new EnvironmentContextTracker(this.taskId)
|
||||
|
||||
// Initialize focus chain manager only if enabled
|
||||
const focusChainSettings = this.stateManager.getGlobalSettingsKey("focusChainSettings")
|
||||
@@ -1018,6 +1021,13 @@ export class Task {
|
||||
})
|
||||
}
|
||||
|
||||
// Record environment metadata for new task
|
||||
try {
|
||||
await this.environmentContextTracker.recordEnvironment()
|
||||
} catch (error) {
|
||||
console.error("Failed to record environment metadata:", error)
|
||||
}
|
||||
|
||||
await this.initiateTaskLoop(userContent)
|
||||
}
|
||||
|
||||
@@ -1280,6 +1290,13 @@ export class Task {
|
||||
})
|
||||
}
|
||||
|
||||
// Record environment metadata when resuming task (tracks cross-platform migrations)
|
||||
try {
|
||||
await this.environmentContextTracker.recordEnvironment()
|
||||
} catch (error) {
|
||||
console.error("Failed to record environment metadata on resume:", error)
|
||||
}
|
||||
|
||||
await this.messageStateHandler.overwriteApiConversationHistory(modifiedApiConversationHistory)
|
||||
await this.initiateTaskLoop(newUserContent)
|
||||
}
|
||||
|
||||
@@ -1,58 +1,9 @@
|
||||
import { Anthropic } from "@anthropic-ai/sdk"
|
||||
import { writeFile } from "@utils/fs"
|
||||
import os from "os"
|
||||
import * as path from "path"
|
||||
import { HostProvider } from "@/hosts/host-provider"
|
||||
import { ShowMessageType } from "@/shared/proto/host/window"
|
||||
import { openFile } from "./open-file"
|
||||
|
||||
export async function downloadTask(dateTs: number, conversationHistory: Anthropic.MessageParam[]) {
|
||||
// File name
|
||||
const date = new Date(dateTs)
|
||||
const month = date.toLocaleString("en-US", { month: "short" }).toLowerCase()
|
||||
const day = date.getDate()
|
||||
const year = date.getFullYear()
|
||||
let hours = date.getHours()
|
||||
const minutes = date.getMinutes().toString().padStart(2, "0")
|
||||
const seconds = date.getSeconds().toString().padStart(2, "0")
|
||||
const ampm = hours >= 12 ? "pm" : "am"
|
||||
hours = hours % 12
|
||||
hours = hours ? hours : 12 // the hour '0' should be '12'
|
||||
const fileName = `cline_task_${month}-${day}-${year}_${hours}-${minutes}-${seconds}-${ampm}.md`
|
||||
|
||||
// Generate markdown
|
||||
const markdownContent = conversationHistory
|
||||
.map((message) => {
|
||||
const role = message.role === "user" ? "**User:**" : "**Assistant:**"
|
||||
const content = Array.isArray(message.content)
|
||||
? message.content.map((block) => formatContentBlockToMarkdown(block)).join("\n")
|
||||
: message.content
|
||||
return `${role}\n\n${content}\n\n`
|
||||
})
|
||||
.join("---\n\n")
|
||||
|
||||
// Prompt user for save location
|
||||
const saveResponse = await HostProvider.window.showSaveDialog({
|
||||
options: {
|
||||
filters: { Markdown: { extensions: ["md"] } },
|
||||
defaultPath: path.join(os.homedir(), "Downloads", fileName),
|
||||
},
|
||||
})
|
||||
|
||||
if (saveResponse.selectedPath) {
|
||||
try {
|
||||
// Write content to the selected location
|
||||
await writeFile(saveResponse.selectedPath, markdownContent)
|
||||
await openFile(saveResponse.selectedPath, false, true)
|
||||
} catch (error) {
|
||||
await HostProvider.window.showMessage({
|
||||
type: ShowMessageType.ERROR,
|
||||
message: `Failed to save markdown file: ${error instanceof Error ? error.message : String(error)}`,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a content block to markdown for display in API request messages.
|
||||
* Used by Task class to format user content for the api_req_started message.
|
||||
*/
|
||||
export function formatContentBlockToMarkdown(block: Anthropic.ContentBlockParam): string {
|
||||
switch (block.type) {
|
||||
case "text":
|
||||
|
||||
Reference in new issue
Block a user