Brunobkr/llama.cpp_AlgMor24_github
ΩFFFΣLLIa • llama.cpp • AlgMor24 ██████╗ ███████╗███████╗███████╗██╗ ██╗ ██╗ █████╗ ██╔═══██╗██╔════╝██╔════╝██╔════╝██║ ██║ ██║██╔══██╗ ██║ ██║█████╗ █████╗ █████╗ ██║ ██║ ██║███████║ ██║ ██║██╔══╝ ██╔══╝ ██╔══╝ ██║ ██║ ██║██╔══██║ ╚██████╔╝██║ ██║ ███████╗███████╗███████╗██║██║ ██║ ╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝╚═╝ ╚═╝ High-Performance LLM / VLM Inference & Autonomous Agentic Ecosystem… See the full description on the dataset page: https://huggingface.co/datasets/Brunobkr/llama.cpp_AlgMor24_github.
03.1k
1/*---------------------------------------------------------------------------------------------2 * Copyright (c) Microsoft Corporation. All rights reserved.3 * Licensed under the MIT License. See License.txt in the project root for license information.4 *--------------------------------------------------------------------------------------------*/5'use strict';6import { format, isEOL } from './format';7import { parseTree, findNodeAtLocation } from './parser';8export function removeProperty(text, path, options) {9 return setProperty(text, path, void 0, options);10}11export function setProperty(text, originalPath, value, options) {12 const path = originalPath.slice();13 const errors = [];14 const root = parseTree(text, errors);15 let parent = void 0;16 let lastSegment = void 0;17 while (path.length > 0) {18 lastSegment = path.pop();19 parent = findNodeAtLocation(root, path);20 if (parent === void 0 && value !== void 0) {21 if (typeof lastSegment === 'string') {22 value = { [lastSegment]: value };23 }24 else {25 value = [value];26 }27 }28 else {29 break;30 }31 }32 if (!parent) {33 // empty document34 if (value === void 0) { // delete35 throw new Error('Can not delete in empty document');36 }37 return withFormatting(text, { offset: root ? root.offset : 0, length: root ? root.length : 0, content: JSON.stringify(value) }, options);38 }39 else if (parent.type === 'object' && typeof lastSegment === 'string' && Array.isArray(parent.children)) {40 const existing = findNodeAtLocation(parent, [lastSegment]);41 if (existing !== void 0) {42 if (value === void 0) { // delete43 if (!existing.parent) {44 throw new Error('Malformed AST');45 }46 const propertyIndex = parent.children.indexOf(existing.parent);47 let removeBegin;48 let removeEnd = existing.parent.offset + existing.parent.length;49 if (propertyIndex > 0) {50 // remove the comma of the previous node51 let previous = parent.children[propertyIndex - 1];52 removeBegin = previous.offset + previous.length;53 }54 else {55 removeBegin = parent.offset + 1;56 if (parent.children.length > 1) {57 // remove the comma of the next node58 let next = parent.children[1];59 removeEnd = next.offset;60 }61 }62 return withFormatting(text, { offset: removeBegin, length: removeEnd - removeBegin, content: '' }, options);63 }64 else {65 // set value of existing property66 return withFormatting(text, { offset: existing.offset, length: existing.length, content: JSON.stringify(value) }, options);67 }68 }69 else {70 if (value === void 0) { // delete71 return []; // property does not exist, nothing to do72 }73 const newProperty = `${JSON.stringify(lastSegment)}: ${JSON.stringify(value)}`;74 const index = options.getInsertionIndex ? options.getInsertionIndex(parent.children.map(p => p.children[0].value)) : parent.children.length;75 let edit;76 if (index > 0) {77 let previous = parent.children[index - 1];78 edit = { offset: previous.offset + previous.length, length: 0, content: ',' + newProperty };79 }80 else if (parent.children.length === 0) {81 edit = { offset: parent.offset + 1, length: 0, content: newProperty };82 }83 else {84 edit = { offset: parent.offset + 1, length: 0, content: newProperty + ',' };85 }86 return withFormatting(text, edit, options);87 }88 }89 else if (parent.type === 'array' && typeof lastSegment === 'number' && Array.isArray(parent.children)) {90 const insertIndex = lastSegment;91 if (insertIndex === -1) {92 // Insert93 const newProperty = `${JSON.stringify(value)}`;94 let edit;95 if (parent.children.length === 0) {96 edit = { offset: parent.offset + 1, length: 0, content: newProperty };97 }98 else {99 const previous = parent.children[parent.children.length - 1];100 edit = { offset: previous.offset + previous.length, length: 0, content: ',' + newProperty };101 }102 return withFormatting(text, edit, options);103 }104 else if (value === void 0 && parent.children.length >= 0) {105 // Removal106 const removalIndex = lastSegment;107 const toRemove = parent.children[removalIndex];108 let edit;109 if (parent.children.length === 1) {110 // only item111 edit = { offset: parent.offset + 1, length: parent.length - 2, content: '' };112 }113 else if (parent.children.length - 1 === removalIndex) {114 // last item115 let previous = parent.children[removalIndex - 1];116 let offset = previous.offset + previous.length;117 let parentEndOffset = parent.offset + parent.length;118 edit = { offset, length: parentEndOffset - 2 - offset, content: '' };119 }120 else {121 edit = { offset: toRemove.offset, length: parent.children[removalIndex + 1].offset - toRemove.offset, content: '' };122 }123 return withFormatting(text, edit, options);124 }125 else if (value !== void 0) {126 let edit;127 const newProperty = `${JSON.stringify(value)}`;128 if (!options.isArrayInsertion && parent.children.length > lastSegment) {129 const toModify = parent.children[lastSegment];130 edit = { offset: toModify.offset, length: toModify.length, content: newProperty };131 }132 else if (parent.children.length === 0 || lastSegment === 0) {133 edit = { offset: parent.offset + 1, length: 0, content: parent.children.length === 0 ? newProperty : newProperty + ',' };134 }135 else {136 const index = lastSegment > parent.children.length ? parent.children.length : lastSegment;137 const previous = parent.children[index - 1];138 edit = { offset: previous.offset + previous.length, length: 0, content: ',' + newProperty };139 }140 return withFormatting(text, edit, options);141 }142 else {143 throw new Error(`Can not ${value === void 0 ? 'remove' : (options.isArrayInsertion ? 'insert' : 'modify')} Array index ${insertIndex} as length is not sufficient`);144 }145 }146 else {147 throw new Error(`Can not add ${typeof lastSegment !== 'number' ? 'index' : 'property'} to parent of type ${parent.type}`);148 }149}150function withFormatting(text, edit, options) {151 if (!options.formattingOptions) {152 return [edit];153 }154 // apply the edit155 let newText = applyEdit(text, edit);156 // format the new text157 let begin = edit.offset;158 let end = edit.offset + edit.content.length;159 if (edit.length === 0 || edit.content.length === 0) { // insert or remove160 while (begin > 0 && !isEOL(newText, begin - 1)) {161 begin--;162 }163 while (end < newText.length && !isEOL(newText, end)) {164 end++;165 }166 }167 const edits = format(newText, { offset: begin, length: end - begin }, { ...options.formattingOptions, keepLines: false });168 // apply the formatting edits and track the begin and end offsets of the changes169 for (let i = edits.length - 1; i >= 0; i--) {170 const edit = edits[i];171 newText = applyEdit(newText, edit);172 begin = Math.min(begin, edit.offset);173 end = Math.max(end, edit.offset + edit.length);174 end += edit.content.length - edit.length;175 }176 // create a single edit with all changes177 const editLength = text.length - (newText.length - end) - begin;178 return [{ offset: begin, length: editLength, content: newText.substring(begin, end) }];179}180export function applyEdit(text, edit) {181 return text.substring(0, edit.offset) + edit.content + text.substring(edit.offset + edit.length);182}183export function isWS(text, offset) {184 return '\r\n \t'.indexOf(text.charAt(offset)) !== -1;185}186 