一条指令解决Codex接入第三方api之后之前的会话全部找不到的问题

hanglier 2026-08-04 13:40 1

plus周额度用完了,不想坐等5天,就把codex接入了第三方api,发现会话记录全没了,让codex写了一个一键找回会话记录的脚本,亲测有用,windows版本的,mac版本我也写了一个,需要试一下 赶紧试试吧!


$ErrorActionPreference = ‘Stop’


Get-Process -ErrorAction SilentlyContinue |

Where-Object { $_.ProcessName -like ‘Codex*’ } |

Stop-Process -Force -ErrorAction SilentlyContinue

Start-Sleep -Seconds 2


$codexHome = if ([string]::IsNullOrWhiteSpace([string]$env:CODEX_HOME)) {

Join-Path ([Environment]::GetFolderPath(‘UserProfile’)) ‘.codex’

} else {

[string]$env:CODEX_HOME

}


$db = Join-Path $codexHome ‘state_5.sqlite’

$config = Join-Path $codexHome ‘config.toml’


if (-not (Test-Path -LiteralPath $db)) { throw ‘Codex history database is missing.’ }

if (-not (Test-Path -LiteralPath $config)) { throw ‘config.toml is missing.’ }


$provider = $null

foreach ($line in @(Get-Content -LiteralPath $config)) {

if (line -match '^\s*model_provider\s*=\s*"([^"]+)"\s*') {

$provider = $Matches[1].Trim()

break

}

}


if ([string]::IsNullOrWhiteSpace($provider) -or $provider -eq ‘openai’) {

throw ‘Current provider is not a MadAPI custom provider. No changes made.’

}

if (provider -notmatch '^[A-Za-z0-9._-]+') {

throw ‘Unsupported provider name. No changes made.’

}


$backup = Join-Path $codexHome (‘madapi-history-restore-backup-’ + (Get-Date -Format ‘yyyyMMdd-HHmmss’))

New-Item -ItemType Directory -Path $backup -Force | Out-Null

foreach ($suffix in @(‘’, ‘-wal’, ‘-shm’)) {

$source = $db + $suffix

if (Test-Path -LiteralPath $source) {

Copy-Item -LiteralPath $source -Destination $backup -Force

}

}


if (-not (‘MadApiHistorySqlite’ -as [type])) {

Add-Type -TypeDefinition @’

using System;

using System.Runtime.InteropServices;


public static class MadApiHistorySqlite

{



DllImport("winsqlite3.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)

private static extern int sqlite3_open16(string filename, out IntPtr db);


[DllImport("winsqlite3.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int sqlite3_close_v2(IntPtr db);

[DllImport("winsqlite3.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
private static extern int sqlite3_exec(IntPtr db, string sql, IntPtr callback, IntPtr context, out IntPtr error);

[DllImport("winsqlite3.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int sqlite3_changes(IntPtr db);

[DllImport("winsqlite3.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void sqlite3_free(IntPtr pointer);

public static int Migrate(string path, string provider)
{
IntPtr db = IntPtr.Zero;
IntPtr error = IntPtr.Zero;
try
{
if (sqlite3_open16(path, out db) != 0)
throw new InvalidOperationException("Unable to open Codex history database.");

string sql =
"UPDATE threads SET model_provider = '" + provider + "'" +
" WHERE archived = 0" +
" AND source = 'vscode'" +
" AND thread_source = 'user'" +
" AND model_provider = 'openai';";

if (sqlite3_exec(db, sql, IntPtr.Zero, IntPtr.Zero, out error) != 0)
throw new InvalidOperationException("Unable to update Codex history database.");

return sqlite3_changes(db);
}
finally
{
if (error != IntPtr.Zero) sqlite3_free(error);
if (db != IntPtr.Zero) sqlite3_close_v2(db);
}
}

}

'@

}


$count = [MadApiHistorySqlite]::Migrate($db, $provider)


Write-Host “TARGET_PROVIDER=$provider”

Write-Host “MIGRATED_FROM_OPENAI=$count”

Write-Host “BACKUP=$backup”

Write-Host ‘Open Codex to view restored history.’

最新回复 (2)
  • sunway 08-04 14:58
    1

    第三方api重复切回官方后,对话还在么?两边provider不一致导致的

  • hanglier 楼主 08-04 20:41
    2

    对的,provider统一就可以随意切换

* 帖子来源Linux.do
返回