p-doom/crowd-code-dataset-0.2 huggingface dataset:
[content] /home/franz.srambical/crowd-pilot-extension/src/extension.ts (typescript)
	// Check model certainty from logprobs before proceeding
	const certainty = calculateModelCertainty(json);
	if (MIN_MODEL_CERTAINTY > 0 && certainty < MIN_MODEL_CERTAINTY) {
		throw new Error(`Model certainty (${(certainty * 100).toFixed(1)}%) below threshold (${(MIN_MODEL_CERTAINTY * 100).toFixed(1)}%)`);
	}
[content] /home/franz.srambical/crowd-pilot-extension/src/extension.ts (typescript)
/**
 * Calculate average model certainty from logprobs in the API response.
 * Returns a value between 0.0 and 1.0, where 1.0 is highest certainty.
 * Returns 1.0 if logprobs are not available (to avoid blocking suggestions).
 */
function calculateModelCertainty(json: any): number {
	try {
		if (!json || !Array.isArray(json.choices) || !json.choices[0]) {
			return 1.0; // No logprobs available, allow suggestion
		}
		const choice = json.choices[0];
		const logprobs = choice.logprobs;
		
		if (!logprobs || !Array.isArray(logprobs.content) || logprobs.content.length === 0) {
			return 1.0; // No logprobs available, allow suggestion
		}

		// Calculate average probability from log probabilities
		// logprob = ln(probability), so probability = exp(logprob)
		let totalLogProb = 0;
		let tokenCount = 0;

		for (const tokenInfo of logprobs.content) {
			if (typeof tokenInfo.logprob === 'number' && isFinite(tokenInfo.logprob)) {
				totalLogProb += tokenInfo.logprob;
				tokenCount++;
			}
		}

		if (tokenCount === 0) {
			return 1.0; // No valid logprobs, allow suggestion
		}

		// Calculate geometric mean of probabilities (equivalent to exp(mean of logprobs))
		const avgLogProb = totalLogProb / tokenCount;
		const certainty = Math.exp(avgLogProb);
		
		return Math.max(0, Math.min(1, certainty)); // Clamp to [0, 1]
	} catch {
		return 1.0; // On error, allow suggestion
	}
}
[content] /home/franz.srambical/crowd-pilot-extension/src/extension.ts (typescript)
// Minimum cumulative logprob threshold for displaying suggestions
// This is the sum of log probabilities for the entire sequence (negative values, closer to 0 = more confident)
// Set to -Infinity to disable certainty filtering
const MIN_CUMULATIVE_LOGPROB = -50;
[content] /home/franz.srambical/crowd-pilot-extension/src/extension.ts (typescript)
	// Check model certainty from cumulative logprobs before proceeding
	const cumulativeLogprob = calculateCumulativeLogprob(json);
	if (cumulativeLogprob !== null && cumulativeLogprob < MIN_CUMULATIVE_LOGPROB) {
		return undefined as any; // Low certainty, silently skip suggestion
[content] /home/franz.srambical/crowd-pilot-extension/src/extension.ts (typescript)
// Minimum sequence probability threshold for displaying suggestions
// This is the product of token probabilities (0.0 to 1.0, higher = more confident)
// Set to 0.0 to disable certainty filtering
const MIN_SEQUENCE_PROBABILITY = 0.001;
[content] /home/franz.srambical/crowd-pilot-extension/src/extension.ts (typescript)
	// Check model certainty from sequence probability before proceeding
	const sequenceProbability = calculateSequenceProbability(json);
	if (sequenceProbability !== null && sequenceProbability < MIN_SEQUENCE_PROBABILITY) {
[content] /home/franz.srambical/crowd-pilot-extension/src/extension.ts (typescript)
 * Calculate cumulative logprob (sum of token log probabilities) from the API response.
 * Returns the sum of logprobs for the entire sequence (negative value, closer to 0 = more confident).
 * Returns null if logprobs are not available.
 */
function calculateCumulativeLogprob(json: any): number | null {
	try {
		if (!json || !Array.isArray(json.choices) || !json.choices[0]) {
			return null; // No logprobs available
		}
		const choice = json.choices[0];
		const logprobs = choice.logprobs;
		
		if (!logprobs || !Array.isArray(logprobs.content) || logprobs.content.length === 0) {
			return null; // No logprobs available
		}

		// Sum all token log probabilities (cumulative logprob of the sequence)
		let cumulativeLogprob = 0;

		for (const tokenInfo of logprobs.content) {
			if (typeof tokenInfo.logprob === 'number' && isFinite(tokenInfo.logprob)) {
				cumulativeLogprob += tokenInfo.logprob;
			}
		}

		return cumulativeLogprob;
	} catch {
		return null; // On error, allow suggestion
[content] /home/franz.srambical/crowd-pilot-extension/src/extension.ts (typescript)
	if (cumulativeLogprob < MIN_CUMULATIVE_LOGPROB) {
		return undefined as any; // Low certainty, silently skip suggestion
	}

	const content = extractChatContent(json);
	if (typeof content !== 'string' || content.trim().length === 0) {
		throw new Error('Empty model content');
	}
	const action = parsePlannedAction(content, doc);
	if (!action) {
		throw new Error('No valid action parsed from model output');
	}
	return action;
}

function extractChatContent(json: any): string | undefined {
	try {
		if (json && Array.isArray(json.choices) && json.choices[0]) {
			const choice = json.choices[0];
			if (choice.message && typeof choice.message.content === 'string') {
				return choice.message.content;
			}
			if (typeof choice.text === 'string') {
				return choice.text;
			}
		}
		return undefined;
	} catch {
		return undefined;
	}
}

/**
 * Calculate cumulative logprob (sum of token log probabilities) from the API response.
 * Returns the sum of logprobs for the entire sequence (negative value, closer to 0 = more confident).
 * Returns null if logprobs are not available.
 */
function calculateCumulativeLogprob(json: any): number | null {
	const content = json?.choices?.[0]?.logprobs?.content;
	if (!Array.isArray(content)) { return null; }
	return content.reduce((sum: number, tokenInfo: any) => sum + (tokenInfo.logprob || 0), 0);
[content] /home/franz.srambical/crowd-pilot-extension/src/extension.ts (typescript)
	if (cumulativeLogprob !== null && cumulativeLogprob < MIN_CUMULATIVE_LOGPROB) {
		return undefined as any; // Low certainty, silently skip suggestion
	}

	const content = extractChatContent(json);
	if (typeof content !== 'string' || content.trim().length === 0) {
		throw new Error('Empty model content');
	}
	const action = parsePlannedAction(content, doc);
	if (!action) {
		throw new Error('No valid action parsed from model output');
	}
	return action;
}

function extractChatContent(json: any): string | undefined {
	try {
		if (json && Array.isArray(json.choices) && json.choices[0]) {
			const choice = json.choices[0];
			if (choice.message && typeof choice.message.content === 'string') {
				return choice.message.content;
			}
			if (typeof choice.text === 'string') {
				return choice.text;
			}
		}
		return undefined;
	} catch {
		return undefined;
	}
}

/**
 * Calculate cumulative logprob (sum of token log probabilities) from the API response.
 * Returns the sum of logprobs for the entire sequence (negative value, closer to 0 = more confident).
 * Returns null if logprobs are not available.
 */
function calculateCumulativeLogprob(json: any): number | null {
	const logprobs = json.choices[0].logprobs;
	return logprobs.content.reduce((sum: number, tokenInfo: any) => sum + (tokenInfo.logprob || 0), 0);
[content] /home/franz.srambical/crowd-pilot-extension/src/extension.ts (typescript)
	const headers: any = {
		'Content-Type': 'application/json'
	};


	const requestBody: any = {
		model: SGLANG_MODEL_NAME,
		messages: [
			{ role: 'user', content: 'What is the capital of France?' }
		]
	};
	requestBody.temperature = 0.7;
	requestBody.top_p = 0.8;
	requestBody.top_k = 20;
	requestBody.min_p = 0;
	requestBody.chat_template_kwargs = {
		enable_thinking: false
	};
	const postData = JSON.stringify(requestBody);
	headers['Content-Length'] = Buffer.byteLength(postData);

	const options = {
		SGLANG_HOSTNAME,
		SGLANG_PORT,
		SGLANG_BASE_PATH,
		method: 'POST',
		headers
	};

	const requestModule = useHttps ? https : http;

	try {
		const json = await new Promise<any>((resolve, reject) => {
			const req = requestModule.request(options, (res: http.IncomingMessage) => {
				let data = '';
				res.on('data', (chunk: Buffer) => {
					data += chunk.toString();
				});
				res.on('end', () => {
					try {
						resolve(JSON.parse(data));
					} catch (err) {
						reject(new Error(`Failed to parse response: ${err instanceof Error ? err.message : String(err)}`));
					}
				});
			});

			req.on('error', (err: Error) => {
				reject(err);
			});

			req.write(postData);
			req.end();
		});

		vscode.window.showInformationMessage(`Response: ${JSON.stringify(json, null, 2)}`);
	} catch (err) {
		const errorMessage = err instanceof Error ? err.message : String(err);
		vscode.window.showErrorMessage(`Request failed: ${errorMessage}`);
	}
}

// -------------------- Model-planned Actions --------------------
async function requestModelActions(editor: vscode.TextEditor, signal?: AbortSignal): Promise<PlannedAction> {
	let hostname: string;
	let port: number;
	let path: string;
	let useHttps = true;
	let modelName: string;
	const headers: any = {
		'Content-Type': 'application/json'
	};

	hostname = SGLANG_HOSTNAME;
	port = SGLANG_PORT;
	path = SGLANG_BASE_PATH;
	useHttps = false;
	modelName = SGLANG_MODEL_NAME;