@eduardo191labs/opencode-free-pi 0.3.15 → 0.3.17
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 +6 -4
- package/dist/ads/impression.js +2 -2
- package/dist/ads/resolve.js +33 -4
- package/dist/catalog/v2.js +7 -5
- package/dist/constants.d.ts +5 -0
- package/dist/constants.js +5 -0
- package/dist/session/hooks.js +7 -1
- package/package.json +1 -1
- package/src/ads/impression.ts +2 -1
- package/src/ads/resolve.ts +33 -5
- package/src/catalog/v2.ts +7 -5
- package/src/constants.ts +5 -0
- package/src/session/hooks.ts +8 -0
package/README.md
CHANGED
|
@@ -78,10 +78,12 @@ Después de iniciar sesión, ejecuta `/reload` en OpenCode o reinícialo.
|
|
|
78
78
|
El plugin lo responde con la primera línea del primer mensaje, sin gastar
|
|
79
79
|
créditos, sin pedir un anuncio y sin dejar la sesión atascada en
|
|
80
80
|
«New session».
|
|
81
|
-
- **
|
|
82
|
-
El CLI las
|
|
83
|
-
|
|
84
|
-
|
|
81
|
+
- **Solo texto, como el CLI**: los modelos no anuncian entrada de imágenes.
|
|
82
|
+
El CLI las trata fuera de la conversación (sus herramientas de visión e
|
|
83
|
+
imagen), y enviarlas como partes `image_url` incrusta cada imagen leída
|
|
84
|
+
(cientos de KB) en la petición, desborda la ventana de 200k y gasta la
|
|
85
|
+
cuota diaria en tokens de imagen. OpenCode sustituye las imágenes por un
|
|
86
|
+
aviso de que el modelo no acepta esa entrada.
|
|
85
87
|
- **Subagentes identificados**: las sesiones hijas envían
|
|
86
88
|
`x-freepi-subagent: 1`, igual que los proveedores que el CLI crea para sus
|
|
87
89
|
subagentes.
|
package/dist/ads/impression.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** Fetches an ad for a turn and records its impression. */
|
|
2
|
-
import { AD_CONTEXT_HEADER, AD_IMPRESSION_LIMIT, AD_IMPRESSION_TIMEOUT_MS, AD_REQUEST_TIMEOUT_MS, } from "../constants.js";
|
|
2
|
+
import { AD_CONTEXT_HEADER, AD_CONTEXT_OPT_OUT, AD_IMPRESSION_LIMIT, AD_IMPRESSION_TIMEOUT_MS, AD_REQUEST_TIMEOUT_MS, } from "../constants.js";
|
|
3
3
|
import { wireSessionID } from "../session/wire.js";
|
|
4
4
|
import { trimMap } from "../utils/async.js";
|
|
5
5
|
import { adClientHeaders } from "../utils/device.js";
|
|
@@ -66,7 +66,7 @@ export async function fetchAdAndPostImpression(deps, token, sessionId, turn) {
|
|
|
66
66
|
"x-turn-id": turn.turnId,
|
|
67
67
|
"x-ad-trigger": "user_message",
|
|
68
68
|
...adClientHeaders(),
|
|
69
|
-
|
|
69
|
+
[AD_CONTEXT_HEADER]: turn.context ?? AD_CONTEXT_OPT_OUT,
|
|
70
70
|
};
|
|
71
71
|
const response = await fetch(`${resolveBaseUrl()}/ads/next?slot=banner`, {
|
|
72
72
|
headers,
|
package/dist/ads/resolve.js
CHANGED
|
@@ -2,6 +2,25 @@
|
|
|
2
2
|
import { randomUUID } from "node:crypto";
|
|
3
3
|
import { adContextFor, latestAssistantText, latestUserMessageID, latestUserText, } from "./context.js";
|
|
4
4
|
import { adTurnKey, getAdTurn, rememberAdTurn } from "./turns.js";
|
|
5
|
+
function userMessageCount(messages) {
|
|
6
|
+
let count = 0;
|
|
7
|
+
for (const message of messages) {
|
|
8
|
+
const role = message.role ?? message.type;
|
|
9
|
+
if (role === "user")
|
|
10
|
+
count += 1;
|
|
11
|
+
}
|
|
12
|
+
return count;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Stable, id-free turn fingerprint. Provider request messages usually carry the
|
|
16
|
+
* stored user message id, but not always; without one the ad turn would never
|
|
17
|
+
* rotate and the provider would keep answering with the same ad (or fail once
|
|
18
|
+
* it expires). The user-message count plus the latest prompt changes exactly
|
|
19
|
+
* when the user sends a new turn.
|
|
20
|
+
*/
|
|
21
|
+
function turnFingerprint(messages) {
|
|
22
|
+
return `${userMessageCount(messages)}:${latestUserText(messages).slice(0, 160)}`;
|
|
23
|
+
}
|
|
5
24
|
/**
|
|
6
25
|
* Uses the request messages for primary requests and the stored session for
|
|
7
26
|
* auxiliary ones, where the request messages are synthetic (for example a
|
|
@@ -15,12 +34,22 @@ async function contextMessages(deps, request) {
|
|
|
15
34
|
return stored.length > 0 ? stored : provided;
|
|
16
35
|
}
|
|
17
36
|
export async function resolveAdTurn(deps, request) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
37
|
+
let messages = await contextMessages(deps, request);
|
|
38
|
+
let messageID = latestUserMessageID(messages);
|
|
39
|
+
if (!messageID) {
|
|
40
|
+
// The provider messages often omit ids; the stored session keeps them, so
|
|
41
|
+
// use it to keep one ad per user turn instead of one per session.
|
|
42
|
+
const stored = await deps.readContext(request.sessionID).catch(() => []);
|
|
43
|
+
messageID = latestUserMessageID(stored);
|
|
44
|
+
if (messages.length === 0 && stored.length > 0)
|
|
45
|
+
messages = stored;
|
|
46
|
+
}
|
|
47
|
+
if (!messageID) {
|
|
48
|
+
messageID = latestUserMessageID(request.messages ?? []);
|
|
49
|
+
}
|
|
21
50
|
const key = messageID
|
|
22
51
|
? adTurnKey(messageID, request.agent)
|
|
23
|
-
: `${request.sessionID}\u0000${request.agent ?? ""}`;
|
|
52
|
+
: `${request.sessionID}\u0000${request.agent ?? ""}\u0000${turnFingerprint(messages)}`;
|
|
24
53
|
let turn = getAdTurn(deps, key);
|
|
25
54
|
if (!turn) {
|
|
26
55
|
turn = rememberAdTurn(deps, key, {
|
package/dist/catalog/v2.js
CHANGED
|
@@ -13,11 +13,13 @@ export function toV2Models(providerID, models) {
|
|
|
13
13
|
...(model.family ? { family: model.family } : {}),
|
|
14
14
|
capabilities: {
|
|
15
15
|
tools: model.tool_call !== false,
|
|
16
|
-
// The provider CLI registers every free-pi model as text-only
|
|
17
|
-
//
|
|
18
|
-
//
|
|
19
|
-
//
|
|
20
|
-
|
|
16
|
+
// The provider CLI registers every free-pi model as text-only and
|
|
17
|
+
// handles images out of band with its vision and image tools. Sending
|
|
18
|
+
// native image parts instead makes OpenCode inline every read image
|
|
19
|
+
// (hundreds of KB each) into the request, which overflows the 200k
|
|
20
|
+
// window and burns the daily allowance on image tokens, so keep the
|
|
21
|
+
// advertised capability identical to the CLI.
|
|
22
|
+
input: ["text"],
|
|
21
23
|
output: ["text"],
|
|
22
24
|
...(model.temperature === undefined
|
|
23
25
|
? {}
|
package/dist/constants.d.ts
CHANGED
|
@@ -10,6 +10,11 @@ export declare const CONSENT_VERSION = "4";
|
|
|
10
10
|
export declare const CONTEXT_WINDOW = 200000;
|
|
11
11
|
export declare const MAX_OUTPUT_TOKENS = 32768;
|
|
12
12
|
export declare const AD_CONTEXT_HEADER = "x-ad-context";
|
|
13
|
+
/**
|
|
14
|
+
* The CLI sends `none` when the ad context is unavailable or private instead of
|
|
15
|
+
* omitting the header; the server expects the explicit opt-out to rotate ads.
|
|
16
|
+
*/
|
|
17
|
+
export declare const AD_CONTEXT_OPT_OUT = "none";
|
|
13
18
|
export declare const SUBAGENT_HEADER = "x-freepi-subagent";
|
|
14
19
|
export declare const COST_EXPERIMENT_HEADER = "x-freepi-cost-exp";
|
|
15
20
|
export declare const EFFICIENCY_RULE = "- When several independent reads or searches are needed, issue them in the same turn.";
|
package/dist/constants.js
CHANGED
|
@@ -10,6 +10,11 @@ export const CONSENT_VERSION = "4";
|
|
|
10
10
|
export const CONTEXT_WINDOW = 200_000;
|
|
11
11
|
export const MAX_OUTPUT_TOKENS = 32_768;
|
|
12
12
|
export const AD_CONTEXT_HEADER = "x-ad-context";
|
|
13
|
+
/**
|
|
14
|
+
* The CLI sends `none` when the ad context is unavailable or private instead of
|
|
15
|
+
* omitting the header; the server expects the explicit opt-out to rotate ads.
|
|
16
|
+
*/
|
|
17
|
+
export const AD_CONTEXT_OPT_OUT = "none";
|
|
13
18
|
export const SUBAGENT_HEADER = "x-freepi-subagent";
|
|
14
19
|
export const COST_EXPERIMENT_HEADER = "x-freepi-cost-exp";
|
|
15
20
|
export const EFFICIENCY_RULE = "- When several independent reads or searches are needed, issue them in the same turn.";
|
package/dist/session/hooks.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { ensureAdImpression } from "../ads/impression.js";
|
|
3
3
|
import { resolveAdTurn } from "../ads/resolve.js";
|
|
4
4
|
import { resolveToken } from "../auth/token.js";
|
|
5
|
-
import { COST_EXPERIMENT_HEADER, CONCURRENT_RETRY_BASE_MS, CONCURRENT_RETRY_MAX, CONCURRENT_RETRY_MAX_DELAY_MS, DEFENSE_PROVIDER_ID, EFFICIENCY_RULE, MAX_OUTPUT_TOKENS, PROVIDER_ID, RETRY_BASE_DELAY_MS, RETRY_MAX_DELAY_MS, RETRY_MAX_RETRIES, SUBAGENT_HEADER, } from "../constants.js";
|
|
5
|
+
import { AD_CONTEXT_HEADER, AD_CONTEXT_OPT_OUT, COST_EXPERIMENT_HEADER, CONCURRENT_RETRY_BASE_MS, CONCURRENT_RETRY_MAX, CONCURRENT_RETRY_MAX_DELAY_MS, DEFENSE_PROVIDER_ID, EFFICIENCY_RULE, MAX_OUTPUT_TOKENS, PROVIDER_ID, RETRY_BASE_DELAY_MS, RETRY_MAX_DELAY_MS, RETRY_MAX_RETRIES, SUBAGENT_HEADER, } from "../constants.js";
|
|
6
6
|
import { closeFreePiSession } from "../api/session.js";
|
|
7
7
|
import { rewriteContextOverflowResponse, rewriteLimitResponse, } from "../errors/limit.js";
|
|
8
8
|
import { parseFreePiError } from "../errors/parse.js";
|
|
@@ -120,6 +120,12 @@ async function applyModelRequest(deps, event) {
|
|
|
120
120
|
if (!turn)
|
|
121
121
|
return;
|
|
122
122
|
event.headers["x-turn-id"] = turn.turnId;
|
|
123
|
+
// The provider expects ad context on every request of the turn; compaction
|
|
124
|
+
// reuses the turn but explicitly opts out so it never rotates the ad.
|
|
125
|
+
event.headers[AD_CONTEXT_HEADER] =
|
|
126
|
+
event.kind === "compaction"
|
|
127
|
+
? AD_CONTEXT_OPT_OUT
|
|
128
|
+
: (turn.context ?? AD_CONTEXT_OPT_OUT);
|
|
123
129
|
if (token) {
|
|
124
130
|
// V1 awaited the first ad fetch for the turn so the impression lands
|
|
125
131
|
// before the model request. Later steps reuse the settled promise.
|
package/package.json
CHANGED
package/src/ads/impression.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
4
|
AD_CONTEXT_HEADER,
|
|
5
|
+
AD_CONTEXT_OPT_OUT,
|
|
5
6
|
AD_IMPRESSION_LIMIT,
|
|
6
7
|
AD_IMPRESSION_TIMEOUT_MS,
|
|
7
8
|
AD_REQUEST_TIMEOUT_MS,
|
|
@@ -84,7 +85,7 @@ export async function fetchAdAndPostImpression(
|
|
|
84
85
|
"x-turn-id": turn.turnId,
|
|
85
86
|
"x-ad-trigger": "user_message",
|
|
86
87
|
...adClientHeaders(),
|
|
87
|
-
|
|
88
|
+
[AD_CONTEXT_HEADER]: turn.context ?? AD_CONTEXT_OPT_OUT,
|
|
88
89
|
};
|
|
89
90
|
const response = await fetch(`${resolveBaseUrl()}/ads/next?slot=banner`, {
|
|
90
91
|
headers,
|
package/src/ads/resolve.ts
CHANGED
|
@@ -12,6 +12,26 @@ import {
|
|
|
12
12
|
} from "./context.js";
|
|
13
13
|
import { adTurnKey, getAdTurn, rememberAdTurn } from "./turns.js";
|
|
14
14
|
|
|
15
|
+
function userMessageCount(messages: readonly AdMessage[]): number {
|
|
16
|
+
let count = 0;
|
|
17
|
+
for (const message of messages) {
|
|
18
|
+
const role = message.role ?? message.type;
|
|
19
|
+
if (role === "user") count += 1;
|
|
20
|
+
}
|
|
21
|
+
return count;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Stable, id-free turn fingerprint. Provider request messages usually carry the
|
|
26
|
+
* stored user message id, but not always; without one the ad turn would never
|
|
27
|
+
* rotate and the provider would keep answering with the same ad (or fail once
|
|
28
|
+
* it expires). The user-message count plus the latest prompt changes exactly
|
|
29
|
+
* when the user sends a new turn.
|
|
30
|
+
*/
|
|
31
|
+
function turnFingerprint(messages: readonly AdMessage[]): string {
|
|
32
|
+
return `${userMessageCount(messages)}:${latestUserText(messages).slice(0, 160)}`;
|
|
33
|
+
}
|
|
34
|
+
|
|
15
35
|
export type AdTurnRequest = {
|
|
16
36
|
readonly sessionID: string;
|
|
17
37
|
readonly agent: string | undefined;
|
|
@@ -38,13 +58,21 @@ export async function resolveAdTurn(
|
|
|
38
58
|
deps: Deps,
|
|
39
59
|
request: AdTurnRequest,
|
|
40
60
|
): Promise<AdTurn> {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
61
|
+
let messages = await contextMessages(deps, request);
|
|
62
|
+
let messageID = latestUserMessageID(messages);
|
|
63
|
+
if (!messageID) {
|
|
64
|
+
// The provider messages often omit ids; the stored session keeps them, so
|
|
65
|
+
// use it to keep one ad per user turn instead of one per session.
|
|
66
|
+
const stored = await deps.readContext(request.sessionID).catch(() => []);
|
|
67
|
+
messageID = latestUserMessageID(stored);
|
|
68
|
+
if (messages.length === 0 && stored.length > 0) messages = stored;
|
|
69
|
+
}
|
|
70
|
+
if (!messageID) {
|
|
71
|
+
messageID = latestUserMessageID(request.messages ?? []);
|
|
72
|
+
}
|
|
45
73
|
const key = messageID
|
|
46
74
|
? adTurnKey(messageID, request.agent)
|
|
47
|
-
: `${request.sessionID}\u0000${request.agent ?? ""}`;
|
|
75
|
+
: `${request.sessionID}\u0000${request.agent ?? ""}\u0000${turnFingerprint(messages)}`;
|
|
48
76
|
|
|
49
77
|
let turn = getAdTurn(deps, key);
|
|
50
78
|
if (!turn) {
|
package/src/catalog/v2.ts
CHANGED
|
@@ -32,11 +32,13 @@ export function toV2Models(providerID: string, models: ModelMap): any[] {
|
|
|
32
32
|
...(model.family ? { family: model.family } : {}),
|
|
33
33
|
capabilities: {
|
|
34
34
|
tools: model.tool_call !== false,
|
|
35
|
-
// The provider CLI registers every free-pi model as text-only
|
|
36
|
-
//
|
|
37
|
-
//
|
|
38
|
-
//
|
|
39
|
-
|
|
35
|
+
// The provider CLI registers every free-pi model as text-only and
|
|
36
|
+
// handles images out of band with its vision and image tools. Sending
|
|
37
|
+
// native image parts instead makes OpenCode inline every read image
|
|
38
|
+
// (hundreds of KB each) into the request, which overflows the 200k
|
|
39
|
+
// window and burns the daily allowance on image tokens, so keep the
|
|
40
|
+
// advertised capability identical to the CLI.
|
|
41
|
+
input: ["text"],
|
|
40
42
|
output: ["text"],
|
|
41
43
|
...(model.temperature === undefined
|
|
42
44
|
? {}
|
package/src/constants.ts
CHANGED
|
@@ -15,6 +15,11 @@ export const CONTEXT_WINDOW = 200_000;
|
|
|
15
15
|
export const MAX_OUTPUT_TOKENS = 32_768;
|
|
16
16
|
|
|
17
17
|
export const AD_CONTEXT_HEADER = "x-ad-context";
|
|
18
|
+
/**
|
|
19
|
+
* The CLI sends `none` when the ad context is unavailable or private instead of
|
|
20
|
+
* omitting the header; the server expects the explicit opt-out to rotate ads.
|
|
21
|
+
*/
|
|
22
|
+
export const AD_CONTEXT_OPT_OUT = "none";
|
|
18
23
|
export const SUBAGENT_HEADER = "x-freepi-subagent";
|
|
19
24
|
export const COST_EXPERIMENT_HEADER = "x-freepi-cost-exp";
|
|
20
25
|
export const EFFICIENCY_RULE =
|
package/src/session/hooks.ts
CHANGED
|
@@ -6,6 +6,8 @@ import { resolveAdTurn } from "../ads/resolve.js";
|
|
|
6
6
|
import type { AdTurn } from "../types.js";
|
|
7
7
|
import { resolveToken } from "../auth/token.js";
|
|
8
8
|
import {
|
|
9
|
+
AD_CONTEXT_HEADER,
|
|
10
|
+
AD_CONTEXT_OPT_OUT,
|
|
9
11
|
COST_EXPERIMENT_HEADER,
|
|
10
12
|
CONCURRENT_RETRY_BASE_MS,
|
|
11
13
|
CONCURRENT_RETRY_MAX,
|
|
@@ -201,6 +203,12 @@ async function applyModelRequest(deps: Deps, event: any): Promise<void> {
|
|
|
201
203
|
if (!turn) return;
|
|
202
204
|
|
|
203
205
|
event.headers["x-turn-id"] = turn.turnId;
|
|
206
|
+
// The provider expects ad context on every request of the turn; compaction
|
|
207
|
+
// reuses the turn but explicitly opts out so it never rotates the ad.
|
|
208
|
+
event.headers[AD_CONTEXT_HEADER] =
|
|
209
|
+
event.kind === "compaction"
|
|
210
|
+
? AD_CONTEXT_OPT_OUT
|
|
211
|
+
: (turn.context ?? AD_CONTEXT_OPT_OUT);
|
|
204
212
|
if (token) {
|
|
205
213
|
// V1 awaited the first ad fetch for the turn so the impression lands
|
|
206
214
|
// before the model request. Later steps reuse the settled promise.
|