@eduardo191labs/opencode-free-pi 0.2.6 → 0.3.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 +118 -157
- package/dist/ads/context.d.ts +18 -0
- package/dist/ads/context.js +125 -0
- package/dist/ads/impression.d.ts +7 -0
- package/dist/ads/impression.js +85 -0
- package/dist/ads/resolve.d.ts +11 -0
- package/dist/ads/resolve.js +35 -0
- package/dist/ads/turns.d.ts +6 -0
- package/dist/ads/turns.js +14 -0
- package/dist/api/quota.d.ts +8 -0
- package/dist/api/quota.js +90 -0
- package/dist/api/session.d.ts +9 -0
- package/dist/api/session.js +69 -0
- package/dist/auth/authorize.d.ts +14 -0
- package/dist/auth/authorize.js +60 -0
- package/dist/auth/connect.d.ts +7 -0
- package/dist/auth/connect.js +117 -0
- package/dist/auth/credential.d.ts +5 -0
- package/dist/auth/credential.js +34 -0
- package/dist/auth/credentials.d.ts +7 -0
- package/dist/auth/credentials.js +56 -0
- package/dist/auth/jwt.d.ts +7 -0
- package/dist/auth/jwt.js +60 -0
- package/dist/auth/token.d.ts +14 -0
- package/dist/auth/token.js +28 -0
- package/dist/catalog/catalog.d.ts +4 -0
- package/dist/catalog/catalog.js +43 -0
- package/dist/catalog/models.d.ts +15 -0
- package/dist/catalog/models.js +206 -0
- package/dist/catalog/registry.d.ts +4 -0
- package/dist/catalog/registry.js +46 -0
- package/dist/catalog/v2.d.ts +18 -0
- package/dist/catalog/v2.js +93 -0
- package/dist/constants.d.ts +40 -0
- package/dist/constants.js +40 -0
- package/dist/deps.d.ts +11 -0
- package/dist/deps.js +2 -0
- package/dist/errors/handle.d.ts +5 -0
- package/dist/errors/handle.js +64 -0
- package/dist/errors/invalid.d.ts +3 -0
- package/dist/errors/invalid.js +8 -0
- package/dist/errors/limit.d.ts +10 -0
- package/dist/errors/limit.js +110 -0
- package/dist/errors/messages.d.ts +6 -0
- package/dist/errors/messages.js +119 -0
- package/dist/errors/parse.d.ts +4 -0
- package/dist/errors/parse.js +153 -0
- package/dist/index.d.ts +9 -9
- package/dist/index.js +7 -3376
- package/dist/integration/setup.d.ts +4 -0
- package/dist/integration/setup.js +49 -0
- package/dist/logging.d.ts +9 -0
- package/dist/logging.js +19 -0
- package/dist/plugin.d.ts +4 -0
- package/dist/plugin.js +106 -0
- package/dist/session/events.d.ts +4 -0
- package/dist/session/events.js +96 -0
- package/dist/session/hooks.d.ts +8 -0
- package/dist/session/hooks.js +114 -0
- package/dist/session/track.d.ts +21 -0
- package/dist/session/track.js +107 -0
- package/dist/session/usage.d.ts +5 -0
- package/dist/session/usage.js +32 -0
- package/dist/state.d.ts +43 -0
- package/dist/state.js +26 -0
- package/dist/types.d.ts +272 -0
- package/dist/types.js +2 -0
- package/dist/utils/async.d.ts +8 -0
- package/dist/utils/async.js +29 -0
- package/dist/utils/browser.d.ts +3 -0
- package/dist/utils/browser.js +55 -0
- package/dist/utils/device.d.ts +6 -0
- package/dist/utils/device.js +29 -0
- package/dist/utils/http.d.ts +4 -0
- package/dist/utils/http.js +42 -0
- package/dist/utils/json.d.ts +2 -0
- package/dist/utils/json.js +9 -0
- package/dist/utils/numbers.d.ts +6 -0
- package/dist/utils/numbers.js +49 -0
- package/dist/utils/text.d.ts +2 -0
- package/dist/utils/text.js +6 -0
- package/dist/utils/urls.d.ts +6 -0
- package/dist/utils/urls.js +41 -0
- package/install.ps1 +82 -46
- package/install.sh +98 -40
- package/package.json +4 -3
- package/scripts/postinstall.mjs +68 -34
- package/src/ads/context.ts +156 -0
- package/src/ads/impression.ts +111 -0
- package/src/ads/resolve.ts +62 -0
- package/src/ads/turns.ts +24 -0
- package/src/api/quota.ts +110 -0
- package/src/api/session.ts +76 -0
- package/src/auth/authorize.ts +104 -0
- package/src/auth/connect.ts +144 -0
- package/src/auth/credential.ts +39 -0
- package/src/auth/credentials.ts +60 -0
- package/src/auth/jwt.ts +75 -0
- package/src/auth/token.ts +52 -0
- package/src/catalog/catalog.ts +42 -0
- package/src/catalog/models.ts +291 -0
- package/src/catalog/registry.ts +51 -0
- package/src/catalog/v2.ts +124 -0
- package/src/constants.ts +55 -0
- package/src/deps.ts +13 -0
- package/src/errors/handle.ts +79 -0
- package/src/errors/invalid.ts +15 -0
- package/src/errors/limit.ts +114 -0
- package/src/errors/messages.ts +151 -0
- package/src/errors/parse.ts +156 -0
- package/src/index.ts +23 -4042
- package/src/integration/setup.ts +59 -0
- package/src/logging.ts +26 -0
- package/src/plugin.ts +140 -0
- package/src/session/events.ts +117 -0
- package/src/session/hooks.ts +192 -0
- package/src/session/track.ts +124 -0
- package/src/session/usage.ts +46 -0
- package/src/state.ts +82 -0
- package/src/types.ts +259 -0
- package/src/utils/async.ts +35 -0
- package/src/utils/browser.ts +61 -0
- package/src/utils/device.ts +25 -0
- package/src/utils/http.ts +56 -0
- package/src/utils/json.ts +9 -0
- package/src/utils/numbers.ts +52 -0
- package/src/utils/text.ts +7 -0
- package/src/utils/urls.ts +47 -0
package/install.ps1
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
$ErrorActionPreference = "Stop"
|
|
2
|
+
$Package = "@eduardo191labs/opencode-free-pi"
|
|
2
3
|
$PluginName = "opencode-free-pi"
|
|
3
4
|
$Uninstall = ($args -contains "-Uninstall") -or ($args -contains "--uninstall")
|
|
4
|
-
$Repo = if ($env:FREEPI_OPENCODE_REPO) { $env:FREEPI_OPENCODE_REPO } else { "eduardo191-star/opencode-free-pi" }
|
|
5
|
-
$Ref = if ($env:FREEPI_OPENCODE_REF) { $env:FREEPI_OPENCODE_REF } else { "main" }
|
|
6
5
|
|
|
7
6
|
function Get-ConfigDir {
|
|
8
7
|
if ($env:OPENCODE_CONFIG_DIR) { return $env:OPENCODE_CONFIG_DIR }
|
|
@@ -14,61 +13,98 @@ function Write-Step([string]$Message) {
|
|
|
14
13
|
Write-Host $Message
|
|
15
14
|
}
|
|
16
15
|
|
|
17
|
-
|
|
18
|
-
$
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return
|
|
16
|
+
function Get-ConfigFile([string]$Dir) {
|
|
17
|
+
foreach ($name in @("opencode.json", "opencode.jsonc", "opencode.json5")) {
|
|
18
|
+
$candidate = Join-Path $Dir $name
|
|
19
|
+
if (Test-Path -LiteralPath $candidate) { return $candidate }
|
|
20
|
+
}
|
|
21
|
+
return (Join-Path $Dir "opencode.json")
|
|
24
22
|
}
|
|
25
23
|
|
|
26
|
-
[
|
|
24
|
+
function Update-Config([string]$Dir, [bool]$Remove) {
|
|
25
|
+
New-Item -ItemType Directory -Force -Path $Dir | Out-Null
|
|
26
|
+
$file = Get-ConfigFile $Dir
|
|
27
|
+
$config = $null
|
|
28
|
+
if (Test-Path -LiteralPath $file) {
|
|
29
|
+
try {
|
|
30
|
+
$config = Get-Content -LiteralPath $file -Raw | ConvertFrom-Json
|
|
31
|
+
} catch {
|
|
32
|
+
Write-Step "free-pi: no se pudo editar $file automáticamente."
|
|
33
|
+
Write-Step "free-pi: edita `"plugins`" y quita o añade `"$Package`"."
|
|
34
|
+
return
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if ($null -eq $config) {
|
|
38
|
+
$config = [pscustomobject]@{ '$schema' = "https://opencode.ai/config.json" }
|
|
39
|
+
}
|
|
27
40
|
|
|
28
|
-
$
|
|
29
|
-
$
|
|
30
|
-
$target = Join-Path $pluginDir "$PluginName.ts"
|
|
31
|
-
$rawBase = if ($env:FREEPI_OPENCODE_RAW_BASE) { $env:FREEPI_OPENCODE_RAW_BASE } else { "https://raw.githubusercontent.com/$Repo/$Ref" }
|
|
32
|
-
$temp = Join-Path ([System.IO.Path]::GetTempPath()) ("$PluginName-" + [guid]::NewGuid().ToString("N") + ".js")
|
|
41
|
+
$plugins = @()
|
|
42
|
+
if ($null -ne $config.plugins) { $plugins = @($config.plugins) }
|
|
33
43
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
44
|
+
if ($Remove) {
|
|
45
|
+
$plugins = @($plugins | Where-Object {
|
|
46
|
+
if ($_ -is [string]) { return -not ($_ -eq $Package -or $_.StartsWith("$Package@")) }
|
|
47
|
+
return ($null -eq $_ -or $_.package -ne $Package)
|
|
48
|
+
})
|
|
49
|
+
} else {
|
|
50
|
+
$present = $plugins | Where-Object {
|
|
51
|
+
if ($_ -is [string]) { return ($_ -eq $Package -or $_.StartsWith("$Package@")) }
|
|
52
|
+
return ($null -ne $_ -and $_.package -eq $Package)
|
|
53
|
+
}
|
|
54
|
+
if (-not $present) { $plugins += $Package }
|
|
39
55
|
}
|
|
40
56
|
|
|
41
|
-
if ($
|
|
42
|
-
|
|
43
|
-
Write-Step "Using local build: $localBuild"
|
|
57
|
+
if ($null -eq $config.PSObject.Properties["plugins"]) {
|
|
58
|
+
$config | Add-Member -NotePropertyName "plugins" -NotePropertyValue $plugins -Force
|
|
44
59
|
} else {
|
|
45
|
-
$
|
|
46
|
-
Write-Step "Downloading $url"
|
|
47
|
-
Invoke-WebRequest -UseBasicParsing -Uri $url -OutFile $temp
|
|
60
|
+
$config.plugins = $plugins
|
|
48
61
|
}
|
|
49
62
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
63
|
+
$config | ConvertTo-Json -Depth 100 | Set-Content -LiteralPath $file -Encoding UTF8
|
|
64
|
+
if ($Remove) {
|
|
65
|
+
Write-Step "free-pi: eliminado de $file."
|
|
66
|
+
} else {
|
|
67
|
+
Write-Step "free-pi: registrado en $file."
|
|
55
68
|
}
|
|
69
|
+
}
|
|
56
70
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
71
|
+
$configDir = Get-ConfigDir
|
|
72
|
+
|
|
73
|
+
if ($Uninstall) {
|
|
74
|
+
Update-Config $configDir $true
|
|
75
|
+
$pluginDir = Join-Path $configDir "plugins"
|
|
76
|
+
Remove-Item -Force -ErrorAction SilentlyContinue (Join-Path $pluginDir "$PluginName.ts")
|
|
62
77
|
Remove-Item -Force -ErrorAction SilentlyContinue (Join-Path $pluginDir "$PluginName.js")
|
|
78
|
+
& npm uninstall -g $Package 2>$null | Out-Null
|
|
79
|
+
Write-Step "Removed $Package."
|
|
80
|
+
Write-Step "Restart OpenCode to finish."
|
|
81
|
+
return
|
|
82
|
+
}
|
|
63
83
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
Write-Step "Next steps:"
|
|
67
|
-
Write-Step " 1. Run: opencode auth login -p free-pi"
|
|
68
|
-
Write-Step " 2. Restart OpenCode."
|
|
69
|
-
Write-Step ""
|
|
70
|
-
$credentials = if ($env:FREEPI_CREDENTIALS_PATH) { $env:FREEPI_CREDENTIALS_PATH } else { "%USERPROFILE%\.free-pi\agent\credentials.json" }
|
|
71
|
-
Write-Step "Credentials: $credentials"
|
|
72
|
-
} finally {
|
|
73
|
-
Remove-Item -Force -ErrorAction SilentlyContinue $temp
|
|
84
|
+
if (-not (Get-Command node -ErrorAction SilentlyContinue)) {
|
|
85
|
+
throw "Node.js 20+ is required"
|
|
74
86
|
}
|
|
87
|
+
if (-not (Get-Command npm -ErrorAction SilentlyContinue)) {
|
|
88
|
+
throw "npm is required"
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
Write-Step "Installing $Package..."
|
|
92
|
+
& npm install -g "$Package@latest"
|
|
93
|
+
if ($LASTEXITCODE -ne 0) { throw "npm install failed" }
|
|
94
|
+
|
|
95
|
+
if (Get-Command opencode -ErrorAction SilentlyContinue) {
|
|
96
|
+
& opencode plugin add "$Package@latest" 2>$null | Out-Null
|
|
97
|
+
if ($LASTEXITCODE -ne 0) {
|
|
98
|
+
& opencode plugin --global "$Package@latest" 2>$null | Out-Null
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
Update-Config $configDir $false
|
|
103
|
+
|
|
104
|
+
Write-Step ""
|
|
105
|
+
Write-Step "Next steps:"
|
|
106
|
+
Write-Step " 1. Run: opencode auth login free-pi"
|
|
107
|
+
Write-Step " 2. Restart OpenCode (or run /reload)."
|
|
108
|
+
Write-Step ""
|
|
109
|
+
$credentials = if ($env:FREEPI_CREDENTIALS_PATH) { $env:FREEPI_CREDENTIALS_PATH } else { "%USERPROFILE%\.free-pi\agent\credentials.json" }
|
|
110
|
+
Write-Step "Credentials: $credentials"
|
package/install.sh
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env bash
|
|
2
2
|
set -euo pipefail
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
REF="${FREEPI_OPENCODE_REF:-main}"
|
|
6
|
-
RAW_BASE="${FREEPI_OPENCODE_RAW_BASE:-https://raw.githubusercontent.com/${REPO}/${REF}}"
|
|
4
|
+
PACKAGE="@eduardo191labs/opencode-free-pi"
|
|
7
5
|
PLUGIN_NAME="opencode-free-pi"
|
|
8
6
|
|
|
9
7
|
say() {
|
|
@@ -15,11 +13,91 @@ fail() {
|
|
|
15
13
|
exit 1
|
|
16
14
|
}
|
|
17
15
|
|
|
16
|
+
config_dir="${OPENCODE_CONFIG_DIR:-${XDG_CONFIG_HOME:-$HOME/.config}/opencode}"
|
|
17
|
+
|
|
18
|
+
ensure_config() {
|
|
19
|
+
mkdir -p "$config_dir"
|
|
20
|
+
node - "$config_dir" "$PACKAGE" <<'NODE'
|
|
21
|
+
const { existsSync } = require("node:fs");
|
|
22
|
+
const { readFileSync, writeFileSync } = require("node:fs");
|
|
23
|
+
const { join } = require("node:path");
|
|
24
|
+
|
|
25
|
+
const dir = process.argv[2];
|
|
26
|
+
const pkg = process.argv[3];
|
|
27
|
+
const names = ["opencode.json", "opencode.jsonc", "opencode.json5"];
|
|
28
|
+
let file;
|
|
29
|
+
for (const name of names) {
|
|
30
|
+
const candidate = join(dir, name);
|
|
31
|
+
if (!existsSync(candidate)) continue;
|
|
32
|
+
file = candidate;
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
if (!file) file = join(dir, "opencode.json");
|
|
36
|
+
|
|
37
|
+
let config = { $schema: "https://opencode.ai/config.json" };
|
|
38
|
+
if (existsSync(file)) {
|
|
39
|
+
try {
|
|
40
|
+
config = JSON.parse(readFileSync(file, "utf8"));
|
|
41
|
+
} catch {
|
|
42
|
+
console.log(`free-pi: no se pudo editar ${file} automáticamente.`);
|
|
43
|
+
console.log(`free-pi: añade "${pkg}" a "plugins".`);
|
|
44
|
+
process.exit(0);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const plugins = Array.isArray(config.plugins) ? config.plugins : [];
|
|
49
|
+
const present = plugins.some((entry) => {
|
|
50
|
+
if (typeof entry === "string") return entry === pkg || entry.startsWith(`${pkg}@`);
|
|
51
|
+
return entry && entry.package === pkg;
|
|
52
|
+
});
|
|
53
|
+
if (!present) {
|
|
54
|
+
plugins.push(pkg);
|
|
55
|
+
config.plugins = plugins;
|
|
56
|
+
writeFileSync(file, `${JSON.stringify(config, null, 2)}\n`);
|
|
57
|
+
console.log(`free-pi: registrado en ${file}.`);
|
|
58
|
+
} else {
|
|
59
|
+
console.log(`free-pi: ya estaba registrado en ${file}.`);
|
|
60
|
+
}
|
|
61
|
+
NODE
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
remove_config() {
|
|
65
|
+
node - "$config_dir" "$PACKAGE" <<'NODE'
|
|
66
|
+
const { existsSync } = require("node:fs");
|
|
67
|
+
const { readFileSync, writeFileSync } = require("node:fs");
|
|
68
|
+
const { join } = require("node:path");
|
|
69
|
+
|
|
70
|
+
const dir = process.argv[2];
|
|
71
|
+
const pkg = process.argv[3];
|
|
72
|
+
for (const name of ["opencode.json", "opencode.jsonc", "opencode.json5"]) {
|
|
73
|
+
const file = join(dir, name);
|
|
74
|
+
if (!existsSync(file)) continue;
|
|
75
|
+
try {
|
|
76
|
+
const config = JSON.parse(readFileSync(file, "utf8"));
|
|
77
|
+
if (!Array.isArray(config.plugins)) continue;
|
|
78
|
+
const next = config.plugins.filter((entry) => {
|
|
79
|
+
if (typeof entry === "string") {
|
|
80
|
+
return !(entry === pkg || entry.startsWith(`${pkg}@`));
|
|
81
|
+
}
|
|
82
|
+
return !entry || entry.package !== pkg;
|
|
83
|
+
});
|
|
84
|
+
if (next.length === config.plugins.length) continue;
|
|
85
|
+
config.plugins = next;
|
|
86
|
+
writeFileSync(file, `${JSON.stringify(config, null, 2)}\n`);
|
|
87
|
+
console.log(`free-pi: eliminado de ${file}.`);
|
|
88
|
+
} catch {
|
|
89
|
+
console.log(`free-pi: edita ${file} y quita "${pkg}" de "plugins".`);
|
|
90
|
+
}
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
NODE
|
|
94
|
+
}
|
|
95
|
+
|
|
18
96
|
uninstall() {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
say "Removed ${
|
|
97
|
+
remove_config
|
|
98
|
+
rm -f "$config_dir/plugins/${PLUGIN_NAME}.ts" "$config_dir/plugins/${PLUGIN_NAME}.js"
|
|
99
|
+
npm uninstall -g "$PACKAGE" >/dev/null 2>&1 || true
|
|
100
|
+
say "Removed ${PACKAGE}."
|
|
23
101
|
say "Restart OpenCode to finish."
|
|
24
102
|
}
|
|
25
103
|
|
|
@@ -28,45 +106,25 @@ if [ "${1:-}" = "--uninstall" ]; then
|
|
|
28
106
|
exit 0
|
|
29
107
|
fi
|
|
30
108
|
|
|
31
|
-
command -v
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
config_dir="${OPENCODE_CONFIG_DIR:-${XDG_CONFIG_HOME:-$HOME/.config}/opencode}"
|
|
35
|
-
plugin_dir="${config_dir}/plugins"
|
|
36
|
-
target="${plugin_dir}/${PLUGIN_NAME}.ts"
|
|
37
|
-
tmp="$(mktemp "${TMPDIR:-/tmp}/${PLUGIN_NAME}.XXXXXX")"
|
|
38
|
-
cleanup() {
|
|
39
|
-
rm -f "$tmp"
|
|
40
|
-
}
|
|
41
|
-
trap cleanup EXIT
|
|
109
|
+
command -v node >/dev/null 2>&1 || fail "Node.js 20+ is required"
|
|
110
|
+
command -v npm >/dev/null 2>&1 || fail "npm is required"
|
|
42
111
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
46
|
-
fi
|
|
112
|
+
say "Installing ${PACKAGE}..."
|
|
113
|
+
npm install -g "${PACKAGE}@latest"
|
|
47
114
|
|
|
48
|
-
if
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
115
|
+
if command -v opencode >/dev/null 2>&1; then
|
|
116
|
+
if opencode plugin add "${PACKAGE}@latest" >/dev/null 2>&1; then
|
|
117
|
+
say "Registered with opencode plugin add."
|
|
118
|
+
elif opencode plugin --global "${PACKAGE}@latest" >/dev/null 2>&1; then
|
|
119
|
+
say "Registered with opencode plugin --global."
|
|
120
|
+
fi
|
|
54
121
|
fi
|
|
55
122
|
|
|
56
|
-
|
|
57
|
-
grep -q "FreePiPlugin" "$tmp" || fail "downloaded file does not look like the free-pi plugin"
|
|
58
|
-
|
|
59
|
-
mkdir -p "$plugin_dir"
|
|
60
|
-
if [ -f "$target" ]; then
|
|
61
|
-
cp "$target" "${target}.bak"
|
|
62
|
-
fi
|
|
63
|
-
mv "$tmp" "$target"
|
|
64
|
-
rm -f "${plugin_dir}/${PLUGIN_NAME}.js"
|
|
123
|
+
ensure_config
|
|
65
124
|
|
|
66
|
-
say "Installed ${PLUGIN_NAME} at ${target}"
|
|
67
125
|
say ""
|
|
68
126
|
say "Next steps:"
|
|
69
|
-
say " 1. Run: opencode auth login
|
|
70
|
-
say " 2. Restart OpenCode."
|
|
127
|
+
say " 1. Run: opencode auth login free-pi"
|
|
128
|
+
say " 2. Restart OpenCode (or run /reload)."
|
|
71
129
|
say ""
|
|
72
130
|
say "Credentials: ${FREEPI_CREDENTIALS_PATH:-~/.free-pi/agent/credentials.json}"
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eduardo191labs/opencode-free-pi",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Ad-supported free-pi provider plugin for OpenCode on Windows, Linux, macOS and Termux",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
},
|
|
48
48
|
"homepage": "https://github.com/eduardo191-star/opencode-free-pi#readme",
|
|
49
49
|
"engines": {
|
|
50
|
-
"node": ">=
|
|
50
|
+
"node": ">=20"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@types/node": "^24.0.0",
|
|
@@ -57,6 +57,7 @@
|
|
|
57
57
|
"access": "public"
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
|
-
"@opencode
|
|
60
|
+
"@opencode/plugin": "^2.3.2",
|
|
61
|
+
"@opencode/schema": "^2.3.2"
|
|
61
62
|
}
|
|
62
63
|
}
|
package/scripts/postinstall.mjs
CHANGED
|
@@ -1,32 +1,26 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Registers the published package in the global OpenCode configuration.
|
|
3
|
+
*
|
|
4
|
+
* OpenCode V2 loads plugins from the `plugins` list in `opencode.json(c)`.
|
|
5
|
+
* Copying the built file into a plugins directory does not work for this
|
|
6
|
+
* package because it imports `@opencode/plugin` at runtime, so the package has
|
|
7
|
+
* to stay resolvable from node_modules.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { readFile, rm, writeFile } from "node:fs/promises";
|
|
2
11
|
import { existsSync } from "node:fs";
|
|
3
12
|
import { homedir } from "node:os";
|
|
4
|
-
import {
|
|
5
|
-
import { fileURLToPath } from "node:url";
|
|
13
|
+
import { join } from "node:path";
|
|
6
14
|
|
|
7
|
-
const
|
|
8
|
-
const source = join(packageRoot, "dist", "index.js");
|
|
15
|
+
const PACKAGE_NAME = "@eduardo191labs/opencode-free-pi";
|
|
9
16
|
const isGlobal = process.env.npm_config_global === "true";
|
|
10
|
-
|
|
11
|
-
if (!isGlobal && process.env.FREEPI_CONFIGURE !== "1") {
|
|
12
|
-
console.log(
|
|
13
|
-
"free-pi: instalación local; usa `npm i -g` o FREEPI_CONFIGURE=1 para registrar el plugin.",
|
|
14
|
-
);
|
|
15
|
-
process.exit(0);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
if (!existsSync(source)) {
|
|
19
|
-
console.error(`free-pi: no se encontró ${source}`);
|
|
20
|
-
process.exit(1);
|
|
21
|
-
}
|
|
17
|
+
const shouldConfigure = isGlobal || process.env.FREEPI_CONFIGURE === "1";
|
|
22
18
|
|
|
23
19
|
const configDir =
|
|
24
20
|
process.env.OPENCODE_CONFIG_DIR ||
|
|
25
21
|
(process.env.XDG_CONFIG_HOME
|
|
26
22
|
? join(process.env.XDG_CONFIG_HOME, "opencode")
|
|
27
23
|
: join(homedir(), ".config", "opencode"));
|
|
28
|
-
const pluginDir = join(configDir, "plugins");
|
|
29
|
-
const target = join(pluginDir, "opencode-free-pi.ts");
|
|
30
24
|
|
|
31
25
|
function display(file) {
|
|
32
26
|
const home = homedir();
|
|
@@ -34,31 +28,71 @@ function display(file) {
|
|
|
34
28
|
return file.replace(home, "~");
|
|
35
29
|
}
|
|
36
30
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
31
|
+
async function removeLegacyCopy() {
|
|
32
|
+
const legacy = join(configDir, "plugins", "opencode-free-pi.ts");
|
|
33
|
+
if (!existsSync(legacy)) return;
|
|
34
|
+
try {
|
|
35
|
+
const text = await readFile(legacy, "utf8");
|
|
36
|
+
if (text.includes("free-pi")) await rm(legacy, { force: true });
|
|
37
|
+
} catch {}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (!shouldConfigure) {
|
|
41
|
+
console.log(
|
|
42
|
+
`free-pi: instalación local. Añade "${PACKAGE_NAME}" a "plugins" en tu opencode.json.`,
|
|
43
|
+
);
|
|
44
|
+
process.exit(0);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
await removeLegacyCopy();
|
|
48
|
+
|
|
49
|
+
const candidates = ["opencode.json", "opencode.jsonc", "opencode.json5"];
|
|
50
|
+
let configFile;
|
|
51
|
+
let config;
|
|
52
|
+
|
|
53
|
+
for (const name of candidates) {
|
|
40
54
|
const file = join(configDir, name);
|
|
41
55
|
if (!existsSync(file)) continue;
|
|
56
|
+
configFile = file;
|
|
42
57
|
try {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
58
|
+
config = JSON.parse(await readFile(file, "utf8"));
|
|
59
|
+
} catch {
|
|
60
|
+
// Comments or JSON5 cannot be rewritten safely; report the manual step.
|
|
61
|
+
config = undefined;
|
|
62
|
+
}
|
|
63
|
+
break;
|
|
47
64
|
}
|
|
48
65
|
|
|
49
|
-
if (
|
|
50
|
-
await rm(target, { force: true });
|
|
66
|
+
if (configFile && !config) {
|
|
51
67
|
console.log(
|
|
52
|
-
|
|
68
|
+
`free-pi: no se pudo editar ${display(configFile)} automáticamente.`,
|
|
53
69
|
);
|
|
54
|
-
console.log(
|
|
70
|
+
console.log(`free-pi: añade "${PACKAGE_NAME}" a "plugins".`);
|
|
55
71
|
process.exit(0);
|
|
56
72
|
}
|
|
57
73
|
|
|
58
|
-
|
|
59
|
-
|
|
74
|
+
if (!config) {
|
|
75
|
+
configFile = join(configDir, "opencode.json");
|
|
76
|
+
config = { $schema: "https://opencode.ai/config.json" };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const plugins = Array.isArray(config.plugins) ? config.plugins : [];
|
|
80
|
+
const configured = plugins.some((entry) => {
|
|
81
|
+
if (typeof entry === "string") {
|
|
82
|
+
return entry === PACKAGE_NAME || entry.startsWith(`${PACKAGE_NAME}@`);
|
|
83
|
+
}
|
|
84
|
+
return entry?.package === PACKAGE_NAME;
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
if (!configured) {
|
|
88
|
+
plugins.push(PACKAGE_NAME);
|
|
89
|
+
config.plugins = plugins;
|
|
90
|
+
await writeFile(configFile, `${JSON.stringify(config, null, 2)}\n`);
|
|
91
|
+
console.log(`free-pi: registrado en ${display(configFile)}.`);
|
|
92
|
+
} else {
|
|
93
|
+
console.log(`free-pi: ya estaba registrado en ${display(configFile)}.`);
|
|
94
|
+
}
|
|
60
95
|
|
|
61
|
-
console.log(`free-pi: plugin instalado en ${display(target)}`);
|
|
62
96
|
console.log(
|
|
63
|
-
"free-pi:
|
|
97
|
+
"free-pi: ejecuta `opencode auth login -p free-pi` y luego /reload.",
|
|
64
98
|
);
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/** Builds and redacts the ad context sent with a user turn. */
|
|
2
|
+
|
|
3
|
+
import { AD_CONTEXT_MAX_CHARS } from "../constants.js";
|
|
4
|
+
import { cleanText } from "../utils/text.js";
|
|
5
|
+
|
|
6
|
+
const AD_SECRET_PATTERNS: Array<[string, RegExp]> = [
|
|
7
|
+
[
|
|
8
|
+
"private_key",
|
|
9
|
+
/-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]{1,20000}?-----END [A-Z ]*PRIVATE KEY-----/g,
|
|
10
|
+
],
|
|
11
|
+
[
|
|
12
|
+
"private_key",
|
|
13
|
+
/-----BEGIN [A-Z ]*PRIVATE KEY-----[\sA-Za-z0-9+/=]{20,20000}/g,
|
|
14
|
+
],
|
|
15
|
+
["anthropic_key", /\bsk-ant-[A-Za-z0-9_-]{20,255}/g],
|
|
16
|
+
["openrouter_key", /\bsk-or-v1-[A-Za-z0-9]{32,255}/g],
|
|
17
|
+
["api_key", /\bsk-[A-Za-z0-9_-]{20,255}/g],
|
|
18
|
+
["google_api_key", /\bAIza[0-9A-Za-z_-]{35}(?![0-9A-Za-z_-])/g],
|
|
19
|
+
["stripe_secret", /\b[sr]k_(?:live|test)_[0-9A-Za-z]{20,255}/g],
|
|
20
|
+
["github_token", /\b(?:ghp|gho|ghu|ghs|ghr)_[A-Za-z0-9]{36,255}/g],
|
|
21
|
+
["github_token", /\bgithub_pat_[A-Za-z0-9_]{22,255}/g],
|
|
22
|
+
["gitlab_token", /\bglpat-[A-Za-z0-9_-]{20,255}/g],
|
|
23
|
+
["aws_key_id", /\b(?:AKIA|ASIA)[A-Z0-9]{16}\b/g],
|
|
24
|
+
["slack_token", /\bxox[baprs]-[A-Za-z0-9-]{10,255}/g],
|
|
25
|
+
[
|
|
26
|
+
"jwt",
|
|
27
|
+
/\beyJ[A-Za-z0-9_-]{8,512}\.[A-Za-z0-9_-]{8,8192}\.[A-Za-z0-9_-]{8,1024}(?![A-Za-z0-9_-])/g,
|
|
28
|
+
],
|
|
29
|
+
["bearer", /\bBearer\s+[A-Za-z0-9._~+/=-]{16,512}/gi],
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
const AD_SECRET_ASSIGNMENT =
|
|
33
|
+
/(?<![A-Za-z0-9_.-])(["']?)([A-Za-z0-9_.-]+)\1?(\s*[:=]\s*)("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|[^\s"']{8,})/g;
|
|
34
|
+
const AD_SECRET_NAME =
|
|
35
|
+
/api[_-]?key|secret|token|passwd|password|private[_-]?key|authorization/i;
|
|
36
|
+
|
|
37
|
+
export function redactAdSecrets(text: string): string {
|
|
38
|
+
let output = text;
|
|
39
|
+
for (const [type, pattern] of AD_SECRET_PATTERNS) {
|
|
40
|
+
output = output.replace(pattern, `[REDACTED:${type}]`);
|
|
41
|
+
}
|
|
42
|
+
return output.replace(
|
|
43
|
+
AD_SECRET_ASSIGNMENT,
|
|
44
|
+
(
|
|
45
|
+
match: string,
|
|
46
|
+
_quote: string,
|
|
47
|
+
name: string,
|
|
48
|
+
_separator: string,
|
|
49
|
+
value: string,
|
|
50
|
+
) => {
|
|
51
|
+
const head = match.slice(0, match.length - value.length);
|
|
52
|
+
if (!AD_SECRET_NAME.test(name)) {
|
|
53
|
+
return (
|
|
54
|
+
head +
|
|
55
|
+
(value[0] === '"' || value[0] === "'"
|
|
56
|
+
? redactAdSecrets(value)
|
|
57
|
+
: value)
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
const quote = value[0] === '"' || value[0] === "'" ? value[0] : "";
|
|
61
|
+
return `${head}${quote}[REDACTED:secret]${quote}`;
|
|
62
|
+
},
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function adContextText(text: string | undefined): string | undefined {
|
|
67
|
+
if (!text) return undefined;
|
|
68
|
+
const value = redactAdSecrets(text).trim();
|
|
69
|
+
return value ? value.slice(0, AD_CONTEXT_MAX_CHARS) : undefined;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function adContextFor(
|
|
73
|
+
userText: string,
|
|
74
|
+
assistantText?: string,
|
|
75
|
+
): string | undefined {
|
|
76
|
+
const payload: { user?: string; assistant?: string } = {};
|
|
77
|
+
const user = adContextText(userText);
|
|
78
|
+
const assistant = adContextText(assistantText);
|
|
79
|
+
if (user) payload.user = user;
|
|
80
|
+
if (assistant) payload.assistant = assistant;
|
|
81
|
+
if (!payload.user && !payload.assistant) return undefined;
|
|
82
|
+
|
|
83
|
+
let encoded = Buffer.from(JSON.stringify(payload)).toString("base64url");
|
|
84
|
+
while (encoded.length > 4096 && (payload.user || payload.assistant)) {
|
|
85
|
+
for (const key of ["user", "assistant"] as const) {
|
|
86
|
+
const value = payload[key];
|
|
87
|
+
if (value) payload[key] = value.slice(0, Math.floor(value.length / 2));
|
|
88
|
+
}
|
|
89
|
+
encoded = Buffer.from(JSON.stringify(payload)).toString("base64url");
|
|
90
|
+
}
|
|
91
|
+
return encoded;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** A message from either a session record or a provider request. */
|
|
95
|
+
export type AdMessage = {
|
|
96
|
+
type?: unknown;
|
|
97
|
+
role?: unknown;
|
|
98
|
+
id?: unknown;
|
|
99
|
+
text?: unknown;
|
|
100
|
+
content?: unknown;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
function roleOf(message: AdMessage): "user" | "assistant" | undefined {
|
|
104
|
+
const value = message.role ?? message.type;
|
|
105
|
+
return value === "user" || value === "assistant" ? value : undefined;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function contentText(content: unknown): string {
|
|
109
|
+
if (typeof content === "string") return content;
|
|
110
|
+
if (!Array.isArray(content)) return "";
|
|
111
|
+
return content
|
|
112
|
+
.filter(
|
|
113
|
+
(part: any) =>
|
|
114
|
+
part?.type === "text" &&
|
|
115
|
+
typeof part.text === "string" &&
|
|
116
|
+
part.synthetic !== true,
|
|
117
|
+
)
|
|
118
|
+
.map((part: any) => part.text)
|
|
119
|
+
.join("\n");
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Latest non-empty assistant text from a message list. */
|
|
123
|
+
export function latestAssistantText(messages: readonly AdMessage[]): string {
|
|
124
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
125
|
+
const message = messages[index];
|
|
126
|
+
if (roleOf(message) !== "assistant") continue;
|
|
127
|
+
const text = cleanText(contentText(message.content));
|
|
128
|
+
if (text) return text;
|
|
129
|
+
}
|
|
130
|
+
return "";
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Latest user text from a message list. */
|
|
134
|
+
export function latestUserText(messages: readonly AdMessage[]): string {
|
|
135
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
136
|
+
const message = messages[index];
|
|
137
|
+
if (roleOf(message) !== "user") continue;
|
|
138
|
+
const text = cleanText(
|
|
139
|
+
typeof message.text === "string" ? message.text : contentText(message.content),
|
|
140
|
+
);
|
|
141
|
+
if (text) return text;
|
|
142
|
+
}
|
|
143
|
+
return "";
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Latest user message id from a message list. */
|
|
147
|
+
export function latestUserMessageID(
|
|
148
|
+
messages: readonly AdMessage[],
|
|
149
|
+
): string | undefined {
|
|
150
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
151
|
+
const message = messages[index];
|
|
152
|
+
if (roleOf(message) !== "user") continue;
|
|
153
|
+
if (typeof message.id === "string" && message.id) return message.id;
|
|
154
|
+
}
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|