publish: the public projection begins here
This is a projection, not a development branch. The tree above was constructed from the internal source named below under a manifest that decides which paths may leave, then scanned as a whole tree rather than as a series of patches, and only then published. Public history starts here because the history before it was not admissible, and neither was the tree. What used to stand in this repository included a rescue copy of another machine, a directory of phone handoffs, deployment wired to one house, and a submodule pointing at a forge no stranger can reach. None of that was ever the product. It stays in the private forge, which is allowed to hold the whole working organism, and this is what was deliberately sent out instead. Three mechanisms produced this tree, in decreasing order of trust. A top-level path the manifest does not name never arrives at all, which is the one that catches directories nobody has thought of yet. Named internal files inside admitted roots are dropped. A short, reviewed table replaces deployment defaults that a public build must not carry -- an endpoint aimed at one LAN, a VPN profile belonging to one phone, packaging built from one checkout path. Everything after this commit is an ordinary publication with the same three trailers, so a force push stops being routine and starts meaning that something deliberate happened. The trailers bind the projection to its source without pretending the public SHA is the private one: same lineage, different tree, and the record says so. Source-Sha: 8f27b1e76a8fef560a336aba18e6990713ff1047 Policy-Sha: 6b261d2f3e6e1fb19874846ba4bb1dfe15565d25b8618c1c1afba0419c101d27 Tree-Digest: 18ec3563c5e5ef9a414993a9f6734b251ff9ed3cd56eebdd6cac01e45c6e3067
This commit is contained in:
commit
8f42fc953d
1476 changed files with 238455 additions and 0 deletions
29
surfaces/quickshell/ii-base/services/ai/AiMessageData.qml
Normal file
29
surfaces/quickshell/ii-base/services/ai/AiMessageData.qml
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import QtQuick;
|
||||
|
||||
/**
|
||||
* Represents a message in an AI conversation. (Kind of) follows the OpenAI API message structure.
|
||||
*/
|
||||
QtObject {
|
||||
property string role
|
||||
property string content
|
||||
property string rawContent
|
||||
// First-class stream blocks. The Souveraine adapter fills these from the
|
||||
// typed SSE events; legacy/provider messages leave this empty and the
|
||||
// existing markdown splitter remains their renderer.
|
||||
property var segments: []
|
||||
property string fileMimeType
|
||||
property string fileUri
|
||||
property string localFilePath
|
||||
property string model
|
||||
property bool thinking: true
|
||||
property bool done: false
|
||||
property var annotations: []
|
||||
property var annotationSources: []
|
||||
property list<string> searchQueries: []
|
||||
property string functionName
|
||||
property var functionCall
|
||||
property string thoughtSignature
|
||||
property string functionResponse
|
||||
property bool functionPending: false
|
||||
property bool visibleToUser: true
|
||||
}
|
||||
32
surfaces/quickshell/ii-base/services/ai/AiModel.qml
Normal file
32
surfaces/quickshell/ii-base/services/ai/AiModel.qml
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import QtQuick;
|
||||
|
||||
/**
|
||||
* An AI model representation.
|
||||
* - name: Friendly name of the model
|
||||
* - icon: Icon name of the model
|
||||
* - description: Description of the model
|
||||
* - endpoint: Endpoint of the model
|
||||
* - model: Model code (like gpt-4.1 or gemini-2.5-flash)
|
||||
* - requires_key: Whether the model requires an API key
|
||||
* - key_id: The identifier of the API key. Use the same identifier for models that can be accessed with the same key.
|
||||
* - key_get_link: Link to get an API key
|
||||
* - key_get_description: Description of pricing and how to get an API key
|
||||
* - api_format: The API format of the model. Can be "openai" or "gemini". Default is "openai".
|
||||
* - extraParams: Extra parameters to be passed to the model. This is a JSON object.
|
||||
*/
|
||||
|
||||
QtObject {
|
||||
property string name
|
||||
property string icon
|
||||
property string description
|
||||
property string homepage
|
||||
property string endpoint
|
||||
property string model
|
||||
property bool requires_key: true
|
||||
property string key_id
|
||||
property string key_get_link
|
||||
property string key_get_description
|
||||
property string api_format: "openai"
|
||||
property var tools
|
||||
property var extraParams: ({})
|
||||
}
|
||||
12
surfaces/quickshell/ii-base/services/ai/ApiStrategy.qml
Normal file
12
surfaces/quickshell/ii-base/services/ai/ApiStrategy.qml
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import QtQuick
|
||||
|
||||
QtObject {
|
||||
function buildEndpoint(model: AiModel): string { throw new Error("Not implemented") }
|
||||
function buildRequestData(model: AiModel, messages, systemPrompt: string, temperature: real, tools: list<var>, filePath: string) { throw new Error("Not implemented") }
|
||||
function buildAuthorizationHeader(apiKeyEnvVarName: string): string { throw new Error("Not implemented") }
|
||||
function parseResponseLine(line: string, message: AiMessageData) { throw new Error("Not implemented") }
|
||||
function onRequestFinished(message: AiMessageData): var { return {} } // Default: no special handling
|
||||
function reset() { } // Reset any internal state if needed
|
||||
function buildScriptFileSetup(filePath) { return "" } // Default: no setup
|
||||
function finalizeScriptContent(scriptContent: string): string { return scriptContent } // Optionally modify/finalize script
|
||||
}
|
||||
272
surfaces/quickshell/ii-base/services/ai/GeminiApiStrategy.qml
Normal file
272
surfaces/quickshell/ii-base/services/ai/GeminiApiStrategy.qml
Normal file
|
|
@ -0,0 +1,272 @@
|
|||
import QtQuick
|
||||
import qs.modules.common.functions as CF
|
||||
|
||||
ApiStrategy {
|
||||
readonly property string apiKeyEnvVarName: "API_KEY"
|
||||
readonly property string fileListVarName: "UPLOADED_FILES_JSON"
|
||||
readonly property string fileListSubstitutionString: "{{ uploadedFilesJson }}"
|
||||
property string buffer: ""
|
||||
|
||||
function buildEndpoint(model: AiModel): string {
|
||||
const result = model.endpoint + `?key=\$\{${root.apiKeyEnvVarName}\}`
|
||||
// console.log("[AI] Endpoint: " + result);
|
||||
return result;
|
||||
}
|
||||
|
||||
function attachmentPart(attachment) {
|
||||
return {
|
||||
"file_data": {
|
||||
"mime_type": attachment.fileMimeType,
|
||||
"file_uri": attachment.fileUri
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function buildRequestData(model: AiModel, messages, systemPrompt: string, temperature: real, tools: list<var>, pendingFiles: var) {
|
||||
let contents = messages.map(message => {
|
||||
// console.log("[AI] Building request data for message:", JSON.stringify(message, null, 2));
|
||||
const geminiApiRoleName = (message.role === "assistant") ? "model" : message.role;
|
||||
const usingSearch = tools[0]?.google_search !== undefined
|
||||
if (!usingSearch && message.functionCall != undefined && message.functionName.length > 0) {
|
||||
const part = { functionCall: message.functionCall };
|
||||
if (message.thoughtSignature && message.thoughtSignature.length > 0) {
|
||||
part.thoughtSignature = message.thoughtSignature;
|
||||
}
|
||||
return {
|
||||
"role": geminiApiRoleName,
|
||||
"parts": [part]
|
||||
}
|
||||
}
|
||||
if (!usingSearch && message.functionResponse != undefined && message.functionName.length > 0) {
|
||||
return {
|
||||
"role": geminiApiRoleName,
|
||||
"parts": [{
|
||||
functionResponse: {
|
||||
"name": message.functionName,
|
||||
"response": { "content": message.functionResponse }
|
||||
}
|
||||
}]
|
||||
}
|
||||
}
|
||||
const messageAttachments = (message.attachments ?? [])
|
||||
.filter(attachment => attachment.fileUri && attachment.fileUri.length > 0)
|
||||
.map(attachment => attachmentPart(attachment));
|
||||
return {
|
||||
"role": geminiApiRoleName,
|
||||
"parts": [
|
||||
...messageAttachments,
|
||||
{ text: message.rawContent },
|
||||
...(messageAttachments.length === 0 && message.fileUri && message.fileUri.length > 0 ? [attachmentPart(message)] : [])
|
||||
]
|
||||
}
|
||||
})
|
||||
if (pendingFiles && pendingFiles.length > 0) {
|
||||
contents[contents.length - 1].parts = [
|
||||
...contents[contents.length - 1].parts,
|
||||
fileListSubstitutionString,
|
||||
];
|
||||
}
|
||||
let baseData = {
|
||||
"contents": contents,
|
||||
"tools": tools,
|
||||
"system_instruction": {
|
||||
"parts": [{ text: systemPrompt }]
|
||||
},
|
||||
"generationConfig": {
|
||||
"temperature": temperature,
|
||||
},
|
||||
};
|
||||
return model.extraParams ? Object.assign({}, baseData, model.extraParams) : baseData;
|
||||
}
|
||||
|
||||
function buildAuthorizationHeader(apiKeyEnvVarName: string): string {
|
||||
return "";
|
||||
}
|
||||
|
||||
function parseResponseLine(line, message) {
|
||||
if (line.startsWith("[")) {
|
||||
buffer += line.slice(1).trim();
|
||||
} else if (line === "]") {
|
||||
buffer += line.slice(0, -1).trim();
|
||||
return parseBuffer(message);
|
||||
} else if (line.startsWith(",")) {
|
||||
return parseBuffer(message);
|
||||
} else {
|
||||
buffer += line.trim();
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
function parseBuffer(message) {
|
||||
let finished = false;
|
||||
try {
|
||||
if (buffer.length === 0) return {};
|
||||
const dataJson = JSON.parse(buffer);
|
||||
|
||||
if (dataJson.uploadedFile) {
|
||||
const targetMessage = root.attachmentTargetMessage || message;
|
||||
if (!targetMessage) return {};
|
||||
|
||||
const attachments = [...(targetMessage.attachments ?? [])];
|
||||
const attachmentIndex = attachments.findIndex(attachment => !attachment.fileUri || attachment.fileUri.length === 0);
|
||||
if (attachmentIndex !== -1) {
|
||||
attachments[attachmentIndex] = Object.assign({}, attachments[attachmentIndex], {
|
||||
"fileUri": dataJson.uploadedFile.uri,
|
||||
"fileMimeType": dataJson.uploadedFile.mimeType,
|
||||
});
|
||||
targetMessage.attachments = attachments;
|
||||
if (attachmentIndex === 0) {
|
||||
targetMessage.fileUri = dataJson.uploadedFile.uri;
|
||||
targetMessage.fileMimeType = dataJson.uploadedFile.mimeType;
|
||||
}
|
||||
} else {
|
||||
targetMessage.fileUri = dataJson.uploadedFile.uri;
|
||||
targetMessage.fileMimeType = dataJson.uploadedFile.mimeType;
|
||||
}
|
||||
targetMessage.localFilePath = targetMessage.attachments?.[0]?.localFilePath ?? targetMessage.localFilePath;
|
||||
return ({})
|
||||
}
|
||||
|
||||
if (dataJson.error) {
|
||||
const errorMsg = `**Error ${dataJson.error.code}**: ${dataJson.error.message}`;
|
||||
message.rawContent += errorMsg;
|
||||
message.content += errorMsg;
|
||||
return { finished: true };
|
||||
}
|
||||
|
||||
if (!dataJson.candidates) return {};
|
||||
|
||||
if (dataJson.candidates[0]?.finishReason) {
|
||||
finished = true;
|
||||
}
|
||||
|
||||
if (dataJson.candidates[0]?.content?.parts[0]?.functionCall) {
|
||||
const part = dataJson.candidates[0].content.parts[0];
|
||||
const functionCall = part.functionCall;
|
||||
message.functionName = functionCall.name;
|
||||
message.functionCall = functionCall;
|
||||
if (part.thoughtSignature) {
|
||||
message.thoughtSignature = part.thoughtSignature;
|
||||
}
|
||||
const newContent = `\n\n[[ Function: ${functionCall.name}(${JSON.stringify(functionCall.args, null, 2)}) ]]\n`
|
||||
message.rawContent += newContent;
|
||||
message.content += newContent;
|
||||
return { functionCall: functionCall, finished: finished };
|
||||
}
|
||||
|
||||
const responseContent = dataJson.candidates[0]?.content?.parts[0]?.text
|
||||
message.rawContent += responseContent;
|
||||
message.content += responseContent;
|
||||
|
||||
const annotationSources = dataJson.candidates[0]?.groundingMetadata?.groundingChunks?.map(chunk => {
|
||||
return {
|
||||
"type": "url_citation",
|
||||
"text": chunk?.web?.title,
|
||||
"url": chunk?.web?.uri,
|
||||
}
|
||||
}) ?? [];
|
||||
|
||||
const annotations = dataJson.candidates[0]?.groundingMetadata?.groundingSupports?.map(citation => {
|
||||
return {
|
||||
"type": "url_citation",
|
||||
"start_index": citation.segment?.startIndex,
|
||||
"end_index": citation.segment?.endIndex,
|
||||
"text": citation?.segment.text,
|
||||
"url": annotationSources[citation.groundingChunkIndices[0]]?.url,
|
||||
"sources": citation.groundingChunkIndices
|
||||
}
|
||||
});
|
||||
message.annotationSources = annotationSources;
|
||||
message.annotations = annotations;
|
||||
message.searchQueries = dataJson.candidates[0]?.groundingMetadata?.webSearchQueries ?? [];
|
||||
|
||||
if (dataJson.usageMetadata) {
|
||||
return {
|
||||
tokenUsage: {
|
||||
input: dataJson.usageMetadata.promptTokenCount ?? -1,
|
||||
output: dataJson.usageMetadata.candidatesTokenCount ?? -1,
|
||||
total: dataJson.usageMetadata.totalTokenCount ?? -1
|
||||
},
|
||||
finished: finished
|
||||
};
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
console.log("[AI] Gemini: Could not parse buffer: ", e);
|
||||
message.rawContent += buffer;
|
||||
message.content += buffer;
|
||||
} finally {
|
||||
buffer = "";
|
||||
}
|
||||
return { finished: finished };
|
||||
}
|
||||
|
||||
function onRequestFinished(message) {
|
||||
return parseBuffer(message);
|
||||
}
|
||||
|
||||
function reset() {
|
||||
buffer = "";
|
||||
}
|
||||
|
||||
function buildScriptFileSetup(pendingFiles: var) {
|
||||
if (!pendingFiles || pendingFiles.length === 0) return "";
|
||||
|
||||
let content = ""
|
||||
content += `${fileListVarName}=""\n`;
|
||||
content += 'mkdir -p "/tmp/quickshell/ai"\n';
|
||||
|
||||
pendingFiles.forEach((filePath, index) => {
|
||||
const trimmedFilePath = CF.FileUtils.trimFileProtocol(filePath);
|
||||
const imagePathVarName = `IMAGE_PATH_${index}`;
|
||||
const fileMimeTypeVarName = `MIME_TYPE_${index}`;
|
||||
const fileUriVarName = `FILE_URI_${index}`;
|
||||
const numBytesVarName = `NUM_BYTES_${index}`;
|
||||
const tmpHeaderVarName = `TMP_HEADER_FILE_${index}`;
|
||||
const tmpFileInfoVarName = `TMP_FILE_INFO_${index}`;
|
||||
const uploadUrlVarName = `UPLOAD_URL_${index}`;
|
||||
const uploadErrorVarName = `UPLOAD_ERROR_${index}`;
|
||||
|
||||
content += `${imagePathVarName}='${CF.StringUtils.shellSingleQuoteEscape(trimmedFilePath)}'\n`;
|
||||
content += `if [ ! -f "$${imagePathVarName}" ] || [ ! -s "$${imagePathVarName}" ]; then printf '{"error": {"code": 400, "message": "Attached file is missing or unreadable: %s"}}\n' "$${imagePathVarName}"; exit 1; fi\n`;
|
||||
content += `${fileMimeTypeVarName}=$(file -b --mime-type "$${imagePathVarName}")\n`;
|
||||
content += `${numBytesVarName}=$(wc -c < "$${imagePathVarName}")\n`;
|
||||
content += `${tmpHeaderVarName}="/tmp/quickshell/ai/upload-header-${index}.tmp"\n`;
|
||||
content += `${tmpFileInfoVarName}="/tmp/quickshell/ai/file-info-${index}.json.tmp"\n`;
|
||||
content += 'curl "https://generativelanguage.googleapis.com/upload/v1beta/files"'
|
||||
+ ` -H "x-goog-api-key: \$${apiKeyEnvVarName}"`
|
||||
+ ` -D "$${tmpHeaderVarName}"`
|
||||
+ ' -H "X-Goog-Upload-Protocol: resumable"'
|
||||
+ ' -H "X-Goog-Upload-Command: start"'
|
||||
+ ` -H "X-Goog-Upload-Header-Content-Length: \$\{${numBytesVarName}\}"`
|
||||
+ ` -H "X-Goog-Upload-Header-Content-Type: \$\{${fileMimeTypeVarName}\}"`
|
||||
+ ' -H "Content-Type: application/json"'
|
||||
+ ` -d '{"file": {"display_name": "Attachment ${index + 1}"}}' 2> /dev/null`
|
||||
+ '\n';
|
||||
content += `${uploadUrlVarName}=$(grep -i "x-goog-upload-url: " "$${tmpHeaderVarName}" | cut -d" " -f2 | tr -d "\r")\n`;
|
||||
content += `rm "$${tmpHeaderVarName}"\n`;
|
||||
content += `if [ -z "$${uploadUrlVarName}" ]; then printf '{"error": {"code": 400, "message": "Failed to start Gemini file upload for %s"}}\n' "$${imagePathVarName}"; exit 1; fi\n`;
|
||||
content += 'curl "$'
|
||||
+ `{${uploadUrlVarName}}"`
|
||||
+ ` -H "x-goog-api-key: \$${apiKeyEnvVarName}"`
|
||||
+ ` -H "Content-Length: \$\{${numBytesVarName}\}"`
|
||||
+ ' -H "X-Goog-Upload-Offset: 0"'
|
||||
+ ' -H "X-Goog-Upload-Command: upload, finalize"'
|
||||
+ ` --data-binary "@$${imagePathVarName}" 2> /dev/null > "$${tmpFileInfoVarName}"`
|
||||
+ '\n';
|
||||
content += `${fileUriVarName}=$(jq -r '.file.uri // empty' "$${tmpFileInfoVarName}")\n`;
|
||||
content += `${uploadErrorVarName}=$(jq -r '.error.message // empty' "$${tmpFileInfoVarName}")\n`;
|
||||
content += `if [ -z "$${fileUriVarName}" ]; then [ -n "$${uploadErrorVarName}" ] || ${uploadErrorVarName}='No file URI returned from Gemini file upload'; printf '{"error": {"code": 400, "message": "Gemini file upload failed for %s: %s"}}\n' "$${imagePathVarName}" "$${uploadErrorVarName}"; exit 1; fi\n`;
|
||||
content += `${fileListVarName}+=$(jq -cn --arg uri "$${fileUriVarName}" --arg mimeType "$${fileMimeTypeVarName}" '{"file_data": {"mime_type": $mimeType, "file_uri": $uri}}')\n`;
|
||||
content += `${fileListVarName}+=','\n`;
|
||||
content += `printf '{"uploadedFile": {"uri": "%s", "mimeType": "%s"}}\n,\n' "$${fileUriVarName}" "$${fileMimeTypeVarName}"\n`;
|
||||
});
|
||||
|
||||
return content
|
||||
}
|
||||
|
||||
function finalizeScriptContent(scriptContent: string): string {
|
||||
const uploadedPartsReference = "'\"${" + fileListVarName + "%,}\"'";
|
||||
return scriptContent.replace(`"${fileListSubstitutionString}"`, uploadedPartsReference);
|
||||
}
|
||||
}
|
||||
144
surfaces/quickshell/ii-base/services/ai/MistralApiStrategy.qml
Normal file
144
surfaces/quickshell/ii-base/services/ai/MistralApiStrategy.qml
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
import QtQuick
|
||||
|
||||
ApiStrategy {
|
||||
property bool isReasoning: false
|
||||
|
||||
function buildEndpoint(model: AiModel): string {
|
||||
// console.log("[AI] Endpoint: " + model.endpoint);
|
||||
return model.endpoint;
|
||||
}
|
||||
|
||||
function buildRequestData(model: AiModel, messages, systemPrompt: string, temperature: real, tools: list<var>, filePath: string) {
|
||||
let baseData = {
|
||||
"model": model.model,
|
||||
"messages": [
|
||||
{role: "system", content: systemPrompt},
|
||||
...messages.map(message => {
|
||||
const hasFunctionCall = message.functionCall != undefined && message.functionName.length > 0
|
||||
let messageData = {
|
||||
"role": message.role,
|
||||
"content": message.rawContent,
|
||||
}
|
||||
if (hasFunctionCall) {
|
||||
if (message.functionResponse?.length > 0) {
|
||||
messageData.name = message.functionName; // Does the func call also need this name? or just the func output?
|
||||
messageData.role = "tool";
|
||||
messageData.content = message.functionResponse;
|
||||
messageData.tool_call_id = message.functionCall.id
|
||||
}
|
||||
}
|
||||
return messageData
|
||||
}),
|
||||
],
|
||||
"stream": true,
|
||||
"temperature": temperature,
|
||||
"tools": tools,
|
||||
};
|
||||
// console.log("[AI] Request data: ", JSON.stringify(baseData, null, 2));
|
||||
return model.extraParams ? Object.assign({}, baseData, model.extraParams) : baseData;
|
||||
}
|
||||
|
||||
function buildAuthorizationHeader(apiKeyEnvVarName: string): string {
|
||||
return `-H "Authorization: Bearer \$\{${apiKeyEnvVarName}\}"`;
|
||||
}
|
||||
|
||||
function parseResponseLine(line, message) {
|
||||
// Remove 'data: ' prefix if present and trim whitespace
|
||||
let cleanData = line.trim();
|
||||
if (cleanData.startsWith("data:")) {
|
||||
cleanData = cleanData.slice(5).trim();
|
||||
}
|
||||
|
||||
// Handle special cases
|
||||
if (!cleanData || cleanData.startsWith(":")) return {};
|
||||
if (cleanData === "[DONE]") {
|
||||
return { finished: true };
|
||||
}
|
||||
|
||||
// Real stuff
|
||||
try {
|
||||
const dataJson = JSON.parse(cleanData);
|
||||
|
||||
// Error response handling
|
||||
if (dataJson.error) {
|
||||
const errorMsg = `**Error**: ${dataJson.error.message || JSON.stringify(dataJson.error)}`;
|
||||
message.rawContent += errorMsg;
|
||||
message.content += errorMsg;
|
||||
return { finished: true };
|
||||
}
|
||||
|
||||
let newContent = "";
|
||||
|
||||
const responseContent = dataJson.choices[0]?.delta?.content || dataJson.message?.content;
|
||||
const responseReasoning = dataJson.choices[0]?.delta?.reasoning || dataJson.choices[0]?.delta?.reasoning_content;
|
||||
|
||||
// Function call
|
||||
if (dataJson.choices[0]?.delta?.tool_calls) {
|
||||
const functionCall = dataJson.choices[0].delta.tool_calls[0];
|
||||
const functionName = functionCall.function.name;
|
||||
const functionArgs = JSON.parse(functionCall.function.arguments) || {}; // Args are given as string???
|
||||
const functionId = functionCall.id;
|
||||
const newContent = `\n\n[[ Function: ${functionName}(${JSON.stringify(functionArgs, null, 2)}) ]]\n`;
|
||||
message.rawContent += newContent;
|
||||
message.content += newContent;
|
||||
message.functionName = functionName;
|
||||
message.functionCall = functionName;
|
||||
return { functionCall: { name: functionName, args: functionArgs, id: functionId } };
|
||||
}
|
||||
|
||||
// Thinking?
|
||||
if (responseContent && responseContent.length > 0) {
|
||||
if (isReasoning) {
|
||||
isReasoning = false;
|
||||
const endBlock = "\n\n</think>\n\n";
|
||||
message.content += endBlock;
|
||||
message.rawContent += endBlock;
|
||||
}
|
||||
newContent = responseContent;
|
||||
} else if (responseReasoning && responseReasoning.length > 0) {
|
||||
if (!isReasoning) {
|
||||
isReasoning = true;
|
||||
const startBlock = "\n\n<think>\n\n";
|
||||
message.rawContent += startBlock;
|
||||
message.content += startBlock;
|
||||
}
|
||||
newContent = responseReasoning;
|
||||
}
|
||||
|
||||
// Text
|
||||
message.content += newContent;
|
||||
message.rawContent += newContent;
|
||||
|
||||
// Usage metadata
|
||||
if (dataJson.usage) {
|
||||
return {
|
||||
tokenUsage: {
|
||||
input: dataJson.usage.prompt_tokens ?? -1,
|
||||
output: dataJson.usage.completion_tokens ?? -1,
|
||||
total: dataJson.usage.total_tokens ?? -1
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
if (`dataJson`.done) {
|
||||
return { finished: true };
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
console.log("[AI] Mistral: Could not parse line: ", e);
|
||||
message.rawContent += line;
|
||||
message.content += line;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
function onRequestFinished(message) {
|
||||
return {};
|
||||
}
|
||||
|
||||
function reset() {
|
||||
isReasoning = false;
|
||||
}
|
||||
|
||||
}
|
||||
120
surfaces/quickshell/ii-base/services/ai/OpenAiApiStrategy.qml
Normal file
120
surfaces/quickshell/ii-base/services/ai/OpenAiApiStrategy.qml
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
import QtQuick
|
||||
|
||||
ApiStrategy {
|
||||
property bool isReasoning: false
|
||||
|
||||
function buildEndpoint(model: AiModel): string {
|
||||
// console.log("[AI] Endpoint: " + model.endpoint);
|
||||
return model.endpoint;
|
||||
}
|
||||
|
||||
function buildRequestData(model: AiModel, messages, systemPrompt: string, temperature: real, tools: list<var>, filePath: string) {
|
||||
let baseData = {
|
||||
"model": model.model,
|
||||
"messages": [
|
||||
{role: "system", content: systemPrompt},
|
||||
...messages.map(message => {
|
||||
return {
|
||||
"role": message.role,
|
||||
"content": message.rawContent,
|
||||
}
|
||||
}),
|
||||
],
|
||||
"stream": true,
|
||||
"tools": tools,
|
||||
"temperature": temperature,
|
||||
};
|
||||
return model.extraParams ? Object.assign({}, baseData, model.extraParams) : baseData;
|
||||
}
|
||||
|
||||
function buildAuthorizationHeader(apiKeyEnvVarName: string): string {
|
||||
return `-H "Authorization: Bearer \$\{${apiKeyEnvVarName}\}"`;
|
||||
}
|
||||
|
||||
function parseResponseLine(line, message) {
|
||||
// Remove 'data: ' prefix if present and trim whitespace
|
||||
let cleanData = line.trim();
|
||||
if (cleanData.startsWith("data:")) {
|
||||
cleanData = cleanData.slice(5).trim();
|
||||
}
|
||||
|
||||
// console.log("[AI] OpenAI: Data:", cleanData);
|
||||
|
||||
// Handle special cases
|
||||
if (!cleanData || cleanData.startsWith(":")) return {};
|
||||
if (cleanData === "[DONE]") {
|
||||
return { finished: true };
|
||||
}
|
||||
|
||||
// Real stuff
|
||||
try {
|
||||
const dataJson = JSON.parse(cleanData);
|
||||
|
||||
// Error response handling
|
||||
if (dataJson.error) {
|
||||
const errorMsg = `**Error**: ${dataJson.error.message || JSON.stringify(dataJson.error)}`;
|
||||
message.rawContent += errorMsg;
|
||||
message.content += errorMsg;
|
||||
return { finished: true };
|
||||
}
|
||||
|
||||
let newContent = "";
|
||||
|
||||
const responseContent = dataJson.choices[0]?.delta?.content || dataJson.message?.content;
|
||||
const responseReasoning = dataJson.choices[0]?.delta?.reasoning || dataJson.choices[0]?.delta?.reasoning_content;
|
||||
|
||||
if (responseContent && responseContent.length > 0) {
|
||||
if (isReasoning) {
|
||||
isReasoning = false;
|
||||
const endBlock = "\n\n</think>\n\n";
|
||||
message.content += endBlock;
|
||||
message.rawContent += endBlock;
|
||||
}
|
||||
newContent = responseContent;
|
||||
} else if (responseReasoning && responseReasoning.length > 0) {
|
||||
if (!isReasoning) {
|
||||
isReasoning = true;
|
||||
const startBlock = "\n\n<think>\n\n";
|
||||
message.rawContent += startBlock;
|
||||
message.content += startBlock;
|
||||
}
|
||||
newContent = responseReasoning;
|
||||
}
|
||||
|
||||
message.content += newContent;
|
||||
message.rawContent += newContent;
|
||||
|
||||
// Usage metadata
|
||||
if (dataJson.usage) {
|
||||
return {
|
||||
tokenUsage: {
|
||||
input: dataJson.usage.prompt_tokens ?? -1,
|
||||
output: dataJson.usage.completion_tokens ?? -1,
|
||||
total: dataJson.usage.total_tokens ?? -1
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
if (dataJson.done) {
|
||||
return { finished: true };
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
console.log("[AI] OpenAI: Could not parse line: ", e);
|
||||
message.rawContent += line;
|
||||
message.content += line;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
function onRequestFinished(message) {
|
||||
// OpenAI format doesn't need special finish handling
|
||||
return {};
|
||||
}
|
||||
|
||||
function reset() {
|
||||
isReasoning = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,123 @@
|
|||
import QtQuick
|
||||
|
||||
ApiStrategy {
|
||||
property bool isReasoning: false
|
||||
|
||||
function buildEndpoint(model: AiModel): string {
|
||||
// console.log("[AI] Endpoint: " + model.endpoint);
|
||||
return model.endpoint;
|
||||
}
|
||||
|
||||
function buildRequestData(model: AiModel, messages, systemPrompt: string, temperature: real, tools: list<var>, filePath: string) {
|
||||
let baseData = {
|
||||
"model": model.model,
|
||||
"instructions": systemPrompt,
|
||||
"input": [...messages.map(message => ({
|
||||
role: message.role,
|
||||
content: message.rawContent
|
||||
}))],
|
||||
"stream": true,
|
||||
"tools": tools,
|
||||
"temperature": temperature
|
||||
};
|
||||
return model.extraParams ? Object.assign({}, baseData, model.extraParams) : baseData;
|
||||
}
|
||||
|
||||
function buildAuthorizationHeader(apiKeyEnvVarName: string): string {
|
||||
return `-H "Authorization: Bearer \$\{${apiKeyEnvVarName}\}"`;
|
||||
}
|
||||
|
||||
function parseResponseLine(line, message) {
|
||||
let cleanData = line.trim();
|
||||
// event line
|
||||
if (cleanData.startsWith("event:")) {
|
||||
return {};
|
||||
}
|
||||
|
||||
// Remove 'data: ' prefix if present and trim whitespace
|
||||
if (cleanData.startsWith("data:")) {
|
||||
cleanData = cleanData.slice(5).trim();
|
||||
}
|
||||
|
||||
// console.log("[AI] OpenAI: Data:", cleanData);
|
||||
|
||||
// Handle special cases
|
||||
if (!cleanData || cleanData.startsWith(":"))
|
||||
return {};
|
||||
|
||||
// Real stuff
|
||||
try {
|
||||
const dataJson = JSON.parse(cleanData);
|
||||
|
||||
// Error response handling
|
||||
if (dataJson.error) {
|
||||
const errorMsg = `**Error**: ${dataJson.error.message || JSON.stringify(dataJson.error)}`;
|
||||
message.rawContent += errorMsg;
|
||||
message.content += errorMsg;
|
||||
return {
|
||||
finished: true
|
||||
};
|
||||
}
|
||||
|
||||
let newContent = "";
|
||||
|
||||
if (dataJson.type === "response.output_text.delta") {
|
||||
if (isReasoning) {
|
||||
isReasoning = false;
|
||||
|
||||
const endBlock = "\n\n</think>\n\n";
|
||||
message.content += endBlock;
|
||||
message.rawContent += endBlock;
|
||||
}
|
||||
|
||||
newContent = dataJson.delta ?? "";
|
||||
} else if (dataJson.type === "response.output_item.added") {
|
||||
if (dataJson.item.type === "reasoning") {
|
||||
if (!isReasoning) {
|
||||
isReasoning = true;
|
||||
|
||||
const startBlock = "\n\n<think>\n\n";
|
||||
message.content += startBlock;
|
||||
message.rawContent += startBlock;
|
||||
}
|
||||
|
||||
newContent = dataJson.item.summary ?? "";
|
||||
}
|
||||
}
|
||||
|
||||
message.content += newContent;
|
||||
message.rawContent += newContent;
|
||||
|
||||
if (dataJson.type === "response.completed") {
|
||||
let result = {
|
||||
finished: true
|
||||
};
|
||||
|
||||
if (dataJson.response && dataJson.response.usage) {
|
||||
const usage = dataJson.response.usage;
|
||||
result.tokenUsage = {
|
||||
input: usage.input_tokens ?? -1,
|
||||
output: usage.output_tokens ?? -1,
|
||||
total: usage.total_tokens ?? -1
|
||||
};
|
||||
}
|
||||
return result;
|
||||
}
|
||||
} catch (e) {
|
||||
console.log("[AI] OpenAI: Could not parse line: ", e);
|
||||
message.rawContent += line;
|
||||
message.content += line;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
function onRequestFinished(message) {
|
||||
// OpenAI format doesn't need special finish handling
|
||||
return {};
|
||||
}
|
||||
|
||||
function reset() {
|
||||
isReasoning = false;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue