@eduardo191labs/opencode-free-pi 0.3.13 → 0.3.15

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.
@@ -1,5 +1,6 @@
1
1
  /** Fetches an ad for a turn and records its impression. */
2
2
  import { AD_CONTEXT_HEADER, AD_IMPRESSION_LIMIT, AD_IMPRESSION_TIMEOUT_MS, AD_REQUEST_TIMEOUT_MS, } from "../constants.js";
3
+ import { wireSessionID } from "../session/wire.js";
3
4
  import { trimMap } from "../utils/async.js";
4
5
  import { adClientHeaders } from "../utils/device.js";
5
6
  import { readJson } from "../utils/json.js";
@@ -55,9 +56,12 @@ export function postAdImpression(deps, token, sessionId, ad) {
55
56
  }
56
57
  export async function fetchAdAndPostImpression(deps, token, sessionId, turn) {
57
58
  try {
59
+ // The ad must ride on the same wire id as the model request; the server
60
+ // ties the impression to that session (the CLI sends one id for both).
61
+ const wireID = wireSessionID(deps, sessionId);
58
62
  const headers = {
59
63
  authorization: `Bearer ${token}`,
60
- "x-session-id": sessionId,
64
+ "x-session-id": wireID,
61
65
  "x-client-version": deps.state.latestClientVersion,
62
66
  "x-turn-id": turn.turnId,
63
67
  "x-ad-trigger": "user_message",
@@ -73,7 +77,7 @@ export async function fetchAdAndPostImpression(deps, token, sessionId, turn) {
73
77
  const ad = await readJson(response);
74
78
  if (!ad)
75
79
  return;
76
- await postAdImpression(deps, token, sessionId, ad);
80
+ await postAdImpression(deps, token, wireID, ad);
77
81
  }
78
82
  catch { }
79
83
  }
@@ -88,6 +88,11 @@ async function applyModelRequest(deps, event) {
88
88
  void closeFreePiSession(deps, previous).catch(() => undefined);
89
89
  deps.log.info("free-pi session ended mid-stream; continuing with a fresh session id", { sessionID: event.sessionID, from: previous, to: next });
90
90
  }
91
+ else if (!deps.state.wireSessions.has(event.sessionID)) {
92
+ // First request in this process. A previous OpenCode run may have left the
93
+ // session's slot locked, so start from a fresh id instead of reopening it.
94
+ rotateWireSessionID(deps, event.sessionID);
95
+ }
91
96
  const token = await resolveToken(deps);
92
97
  if (token)
93
98
  event.headers.authorization = `Bearer ${token}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eduardo191labs/opencode-free-pi",
3
- "version": "0.3.13",
3
+ "version": "0.3.15",
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",
@@ -7,6 +7,7 @@ import {
7
7
  AD_REQUEST_TIMEOUT_MS,
8
8
  } from "../constants.js";
9
9
  import type { Deps } from "../deps.js";
10
+ import { wireSessionID } from "../session/wire.js";
10
11
  import type { AdTurn } from "../types.js";
11
12
  import { trimMap } from "../utils/async.js";
12
13
  import { adClientHeaders } from "../utils/device.js";
@@ -73,9 +74,12 @@ export async function fetchAdAndPostImpression(
73
74
  turn: AdTurn,
74
75
  ): Promise<void> {
75
76
  try {
77
+ // The ad must ride on the same wire id as the model request; the server
78
+ // ties the impression to that session (the CLI sends one id for both).
79
+ const wireID = wireSessionID(deps, sessionId);
76
80
  const headers: Record<string, string> = {
77
81
  authorization: `Bearer ${token}`,
78
- "x-session-id": sessionId,
82
+ "x-session-id": wireID,
79
83
  "x-client-version": deps.state.latestClientVersion,
80
84
  "x-turn-id": turn.turnId,
81
85
  "x-ad-trigger": "user_message",
@@ -89,7 +93,7 @@ export async function fetchAdAndPostImpression(
89
93
  if (!response?.ok || response.status === 204) return;
90
94
  const ad = await readJson(response);
91
95
  if (!ad) return;
92
- await postAdImpression(deps, token, sessionId, ad);
96
+ await postAdImpression(deps, token, wireID, ad);
93
97
  } catch {}
94
98
  }
95
99
 
@@ -162,6 +162,10 @@ async function applyModelRequest(deps: Deps, event: any): Promise<void> {
162
162
  "free-pi session ended mid-stream; continuing with a fresh session id",
163
163
  { sessionID: event.sessionID, from: previous, to: next },
164
164
  );
165
+ } else if (!deps.state.wireSessions.has(event.sessionID)) {
166
+ // First request in this process. A previous OpenCode run may have left the
167
+ // session's slot locked, so start from a fresh id instead of reopening it.
168
+ rotateWireSessionID(deps, event.sessionID);
165
169
  }
166
170
  const token = await resolveToken(deps);
167
171
  if (token) event.headers.authorization = `Bearer ${token}`;