stable-harness 0.0.59 → 0.0.60
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/node_modules/@stable-harness/adapter-deepagents/dist/src/internal/raw-tool-call-parser.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{AIMessage as e}from"@langchain/core/messages";export function createRawToolCallParserMiddleware(t){return{name:"StableHarnessRawToolCallParser",async wrapModelCall(n,o){const r=await o(n);if(!function rawToolCallParsingEnabled(e){const t=isRecord(e.workspace.runtime.recovery)?e.workspace.runtime.recovery:{};return!0===(isRecord(t.toolCall)?t.toolCall:{}).enabled}(t))return r;const a=function parseRawToolCallResponse(t,n){if(0===n.size||!function isModelResponse(e){return isRecord(e)}(t)||function hasToolCalls(e){return Array.isArray(e.tool_calls)&&e.tool_calls.length>0}(t))return;const o=function readContentText(e){return"string"==typeof e?e.trim()?e:void 0:Array.isArray(e)&&e.map(e=>isRecord(e)&&"string"==typeof e.text?e.text:"").join("\n").trim()||void 0}(t.content);if(!o)return;const r=function readRawToolCalls(e,t){const n=readXmlToolCalls(e,t);if(n.length>0)return n;const o=function stripToolMarkup(e){return e.replace(/^\s*```(?:json)?/iu,"").replace(/```\s*$/iu,"").replace(/^\s*<\s*tool_call[^>]*>\s*/iu,"").replace(/\s*<\s*\/\s*tool_call\s*>\s*$/iu,"").trim()}(e),r=readXmlToolCalls(o,t);if(r.length>0)return r;const a=function readFunctionToolCalls(e,t){const n=[];for(const o of t){const t=new RegExp(`(?:^|[\\s<])${escapeRegexp(o)}\\s*\\(([^)]*)\\)\\s*>?`,"giu");for(const r of e.matchAll(t)){const e=readFunctionArgs(r[1]??"");e&&Object.keys(e).length>0&&n.push({id:`stable-harness-${o}-${Date.now().toString(36)}`,name:o,args:e})}}return n}(o,t);return a.length>0?a:function parseJsonObjects(e){const t=[];let n=e.indexOf("{");for(;n>=0;){const o=readBalancedJsonObject(e,n);if(o){try{const e=JSON.parse(o);isRecord(e)&&t.push(e)}catch{}n=e.indexOf("{",n+o.length)}else n=e.indexOf("{",n+1)}return t}(o).flatMap(e=>function readJsonToolCall(e,t){const n=function readNestedToolCall(e,t){const n=Object.entries(e);if(1!==n.length)return;const[o,r]=n[0];return t.has(o)&&isRecord(r)?{id:`stable-harness-${o}-${Date.now().toString(36)}`,name:o,args:r}:void 0}(e,t);if(n)return n;const o=readString(e.name)??readString(e.tool_name)??readString(e.tool)??readString(e.type),r=t.has(o??"")?o:singleToolName(t),a=function readArgs(e){return isRecord(e.arguments)?e.arguments:isRecord(e.parameters)?e.parameters:isRecord(e.args)?e.args:"args"in e&&void 0!==e.args?{args:e.args}:void 0}(e)??function readInlineArgs(e){const t=Object.fromEntries(Object.entries(e).filter(([e])=>!["name","tool_name","tool","type"].includes(e)));return Object.keys(t).length>0?t:void 0}(e);return r&&t.has(r)&&a?{id:`stable-harness-${r}-${Date.now().toString(36)}`,name:r,args:a}:void 0}(e,t)??[])}(o,n);return 0!==r.length?new e({content:"",id:"string"==typeof t.id?t.id:void 0,name:"string"==typeof t.name?t.name:void 0,additional_kwargs:isRecord(t.additional_kwargs)?t.additional_kwargs:{},response_metadata:isRecord(t.response_metadata)?t.response_metadata:{},tool_calls:r.map(e=>({id:e.id,name:e.name,args:e.args,type:"tool_call"}))}):void 0}(r,function visibleToolNames(e){return new Set((e.tools??[]).map(e=>e.name).filter(e=>"string"==typeof e&&e.length>0))}(n));return a??r}}}function readXmlToolCalls(e,t){const n=[],o=/<\s*(tool_call|tool_code)\b[^>]*>(?:[\s\S]*?<\s*\/\s*\1\s*>)?/giu;for(const r of e.matchAll(o)){const e=readXmlToolCall(r[0],t);e&&n.push(e)}return n}function readXmlToolCall(e,t){const n=function readToolCallAttributes(e){const t=/<\s*(?:tool_call|tool_code)\b([^>]*)>/iu.exec(e);if(!t)return;const n=[...t[1].matchAll(/([a-zA-Z_][\w.-]*)\s*=\s*"([^"]*)"/gu)];return n.length>0?Object.fromEntries(n.map(e=>[e[1],e[2]])):void 0}(e),o=readString(n?.name)??readString(n?.tool_name)??readString(n?.tool)??readXmlTag(e,"tool_name")??readXmlTag(e,"name")??readXmlTag(e,"tool")??function readBareXmlToolName(e,t){const n=readBareXmlToolBody(e),o=n?.trim();return o&&t.has(o)?o:void 0}(e,t),r=t.has(o??"")?o:singleToolName(t);if(!r&&!o&&t.has("task")){const t=readBareXmlToolBody(e);if(t)return{id:`stable-harness-task-${Date.now().toString(36)}`,name:"task",args:{input:t}}}if(!r||!t.has(r))return;const a=readXmlTag(e,"tool_args")??readXmlTag(e,"args")??readXmlTag(e,"arguments"),s=a?function readXmlArgs(e){const t=[...e.matchAll(/<\s*([a-zA-Z_][\w.-]*)\s*>([\s\S]*?)<\s*\/\s*\1\s*>/gu)];if(0===t.length){const t=e.trim();return t?{input:t}:void 0}return Object.fromEntries(t.map(e=>[e[1],e[2].trim()]))}(a):function readAttributeArgs(e){if(!e)return;const t=Object.fromEntries(Object.entries(e).filter(([e])=>!["name","tool_name","tool","type"].includes(e)));return Object.keys(t).length>0?t:void 0}(n);return!s&&o?{id:`stable-harness-${r}-${Date.now().toString(36)}`,name:r,args:{}}:s&&0!==Object.keys(s).length?{id:`stable-harness-${r}-${Date.now().toString(36)}`,name:r,args:s}:void 0}function readXmlTag(e,t){const n=new RegExp(`<\\s*${t}\\s*>([\\s\\S]*?)<\\s*\\/\\s*${t}\\s*>`,"iu").exec(e);return n?.[1]?.trim()||void 0}function readBareXmlToolBody(e){return function readXmlElementBody(e,t){const n=function readOpeningTag(e,t){for(const n of t){const t=e.indexOf(`<${n}`);if(t<0)continue;const o=e.indexOf(">",t+n.length+1);if(o>=0)return{end:o+1}}}(e,t);if(!n)return;const o=function findClosingTag(e,t,n){let o;for(const r of t){const t=[`</${r}>`,`</ ${r}>`];for(const r of t){const t=e.indexOf(r,n);t>=0&&(void 0===o||t<o)&&(o=t)}}return o}(e,t,n.end);return void 0===o?void 0:e.slice(n.end,o)}(e,["tool_call","tool_code"])?.trim()||void 0}function readFunctionArgs(e){const t=[...e.matchAll(/([a-zA-Z_][\w.-]*)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^,\s]+))/gu)];return t.length>0?Object.fromEntries(t.map(e=>[e[1],e[2]??e[3]??e[4]??""])):void 0}function readBalancedJsonObject(e,t){let n=0,o=!1,r=!1;for(let a=t;a<e.length;a+=1){const s=e[a];if(o)r="\\"===s&&!r,'"'!==s||r||(o=!1),"\\"!==s&&(r=!1);else if('"'===s&&(o=!0),"{"===s&&(n+=1),"}"===s&&(n-=1),0===n)return e.slice(t,a+1)}}function singleToolName(e){return 1===e.size?[...e][0]:void 0}function escapeRegexp(e){return e.replace(/[.*+?^${}()|[\]\\]/gu,"\\$&")}function readString(e){return"string"==typeof e&&e.trim()?e.trim():void 0}function isRecord(e){return"object"==typeof e&&null!==e&&!Array.isArray(e)}
|
|
1
|
+
import{AIMessage as e}from"@langchain/core/messages";export function createRawToolCallParserMiddleware(t){return{name:"StableHarnessRawToolCallParser",async wrapModelCall(n,o){const r=await o(n);if(!function rawToolCallParsingEnabled(e){const t=isRecord(e.workspace.runtime.recovery)?e.workspace.runtime.recovery:{};return!0===(isRecord(t.toolCall)?t.toolCall:{}).enabled}(t))return r;const a=function parseRawToolCallResponse(t,n){if(0===n.size||!function isModelResponse(e){return isRecord(e)}(t)||function hasToolCalls(e){return Array.isArray(e.tool_calls)&&e.tool_calls.length>0}(t))return;const o=function readContentText(e){return"string"==typeof e?e.trim()?e:void 0:Array.isArray(e)&&e.map(e=>isRecord(e)&&"string"==typeof e.text?e.text:"").join("\n").trim()||void 0}(t.content);if(!o)return;const r=function readRawToolCalls(e,t){const n=readXmlToolCalls(e,t);if(n.length>0)return n;const o=function stripToolMarkup(e){return e.replace(/^\s*```(?:json)?/iu,"").replace(/```\s*$/iu,"").replace(/^\s*<\s*tool_call[^>]*>\s*/iu,"").replace(/\s*<\s*\/\s*tool_call\s*>\s*$/iu,"").trim()}(e),r=readXmlToolCalls(o,t);if(r.length>0)return r;const a=function readFunctionToolCalls(e,t){const n=[];for(const o of t){const t=new RegExp(`(?:^|[\\s<])${escapeRegexp(o)}\\s*\\(([^)]*)\\)\\s*>?`,"giu");for(const r of e.matchAll(t)){const e=readFunctionArgs(r[1]??"");e&&Object.keys(e).length>0&&n.push({id:`stable-harness-${o}-${Date.now().toString(36)}`,name:o,args:e})}}return n}(o,t);return a.length>0?a:function parseJsonObjects(e){const t=[];let n=e.indexOf("{");for(;n>=0;){const o=readBalancedJsonObject(e,n);if(o){try{const e=JSON.parse(o);isRecord(e)&&t.push(e)}catch{}n=e.indexOf("{",n+o.length)}else n=e.indexOf("{",n+1)}return t}(o).flatMap(e=>function readJsonToolCall(e,t){const n=function readNestedToolCall(e,t){const n=Object.entries(e);if(1!==n.length)return;const[o,r]=n[0];return t.has(o)&&isRecord(r)?{id:`stable-harness-${o}-${Date.now().toString(36)}`,name:o,args:r}:void 0}(e,t);if(n)return n;const o=readString(e.name)??readString(e.tool_name)??readString(e.tool_id)??readString(e.tool)??readString(e.type),r=t.has(o??"")?o:singleToolName(t),a=function readArgs(e){return isRecord(e.arguments)?e.arguments:isRecord(e.parameters)?e.parameters:isRecord(e.tool_kwargs)?e.tool_kwargs:isRecord(e.args)?e.args:"args"in e&&void 0!==e.args?{args:e.args}:void 0}(e)??function readInlineArgs(e){const t=Object.fromEntries(Object.entries(e).filter(([e])=>!["name","tool_name","tool","type"].includes(e)));return Object.keys(t).length>0?t:void 0}(e);return r&&t.has(r)&&a?{id:`stable-harness-${r}-${Date.now().toString(36)}`,name:r,args:a}:void 0}(e,t)??[])}(o,n);return 0!==r.length?new e({content:"",id:"string"==typeof t.id?t.id:void 0,name:"string"==typeof t.name?t.name:void 0,additional_kwargs:isRecord(t.additional_kwargs)?t.additional_kwargs:{},response_metadata:isRecord(t.response_metadata)?t.response_metadata:{},tool_calls:r.map(e=>({id:e.id,name:e.name,args:e.args,type:"tool_call"}))}):void 0}(r,function visibleToolNames(e){return new Set((e.tools??[]).map(e=>e.name).filter(e=>"string"==typeof e&&e.length>0))}(n));return a??r}}}function readXmlToolCalls(e,t){const n=[],o=/<\s*(tool_call|tool_code)\b[^>]*>(?:[\s\S]*?<\s*\/\s*\1\s*>)?/giu;for(const r of e.matchAll(o)){const e=readXmlToolCall(r[0],t);e&&n.push(e)}return n}function readXmlToolCall(e,t){const n=function readToolCallAttributes(e){const t=/<\s*(?:tool_call|tool_code)\b([^>]*)>/iu.exec(e);if(!t)return;const n=[...t[1].matchAll(/([a-zA-Z_][\w.-]*)\s*=\s*"([^"]*)"/gu)];return n.length>0?Object.fromEntries(n.map(e=>[e[1],e[2]])):void 0}(e),o=readString(n?.name)??readString(n?.tool_name)??readString(n?.tool)??readXmlTag(e,"tool_name")??readXmlTag(e,"name")??readXmlTag(e,"tool")??function readBareXmlToolName(e,t){const n=readBareXmlToolBody(e),o=n?.trim();return o&&t.has(o)?o:void 0}(e,t),r=t.has(o??"")?o:singleToolName(t);if(!r&&!o&&t.has("task")){const t=readBareXmlToolBody(e);if(t)return{id:`stable-harness-task-${Date.now().toString(36)}`,name:"task",args:{input:t}}}if(!r||!t.has(r))return;const a=readXmlTag(e,"tool_args")??readXmlTag(e,"args")??readXmlTag(e,"arguments"),s=a?function readXmlArgs(e){const t=[...e.matchAll(/<\s*([a-zA-Z_][\w.-]*)\s*>([\s\S]*?)<\s*\/\s*\1\s*>/gu)];if(0===t.length){const t=e.trim();return t?{input:t}:void 0}return Object.fromEntries(t.map(e=>[e[1],e[2].trim()]))}(a):function readAttributeArgs(e){if(!e)return;const t=Object.fromEntries(Object.entries(e).filter(([e])=>!["name","tool_name","tool","type"].includes(e)));return Object.keys(t).length>0?t:void 0}(n);return!s&&o?{id:`stable-harness-${r}-${Date.now().toString(36)}`,name:r,args:{}}:s&&0!==Object.keys(s).length?{id:`stable-harness-${r}-${Date.now().toString(36)}`,name:r,args:s}:void 0}function readXmlTag(e,t){const n=new RegExp(`<\\s*${t}\\s*>([\\s\\S]*?)<\\s*\\/\\s*${t}\\s*>`,"iu").exec(e);return n?.[1]?.trim()||void 0}function readBareXmlToolBody(e){return function readXmlElementBody(e,t){const n=function readOpeningTag(e,t){for(const n of t){const t=e.indexOf(`<${n}`);if(t<0)continue;const o=e.indexOf(">",t+n.length+1);if(o>=0)return{end:o+1}}}(e,t);if(!n)return;const o=function findClosingTag(e,t,n){let o;for(const r of t){const t=[`</${r}>`,`</ ${r}>`];for(const r of t){const t=e.indexOf(r,n);t>=0&&(void 0===o||t<o)&&(o=t)}}return o}(e,t,n.end);return void 0===o?void 0:e.slice(n.end,o)}(e,["tool_call","tool_code"])?.trim()||void 0}function readFunctionArgs(e){const t=[...e.matchAll(/([a-zA-Z_][\w.-]*)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^,\s]+))/gu)];return t.length>0?Object.fromEntries(t.map(e=>[e[1],e[2]??e[3]??e[4]??""])):void 0}function readBalancedJsonObject(e,t){let n=0,o=!1,r=!1;for(let a=t;a<e.length;a+=1){const s=e[a];if(o)r="\\"===s&&!r,'"'!==s||r||(o=!1),"\\"!==s&&(r=!1);else if('"'===s&&(o=!0),"{"===s&&(n+=1),"}"===s&&(n-=1),0===n)return e.slice(t,a+1)}}function singleToolName(e){return 1===e.size?[...e][0]:void 0}function escapeRegexp(e){return e.replace(/[.*+?^${}()|[\]\\]/gu,"\\$&")}function readString(e){return"string"==typeof e&&e.trim()?e.trim():void 0}function isRecord(e){return"object"==typeof e&&null!==e&&!Array.isArray(e)}
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{AIMessage as e}from"@langchain/core/messages";export function createRawToolCallParserMiddleware(t){return{name:"StableHarnessRawToolCallParser",async wrapModelCall(n,o){const r=await o(n);if(!function rawToolCallParsingEnabled(e){const t=isRecord(e.workspace.runtime.recovery)?e.workspace.runtime.recovery:{};return!0===(isRecord(t.toolCall)?t.toolCall:{}).enabled}(t))return r;const a=function parseRawToolCallResponse(t,n){if(0===n.size||!function isModelResponse(e){return isRecord(e)}(t)||function hasToolCalls(e){return Array.isArray(e.tool_calls)&&e.tool_calls.length>0}(t))return;const o=function readContentText(e){return"string"==typeof e?e.trim()?e:void 0:Array.isArray(e)&&e.map(e=>isRecord(e)&&"string"==typeof e.text?e.text:"").join("\n").trim()||void 0}(t.content);if(!o)return;const r=function readRawToolCalls(e,t){const n=readXmlToolCalls(e,t);if(n.length>0)return n;const o=function stripToolMarkup(e){return e.replace(/^\s*```(?:json)?/iu,"").replace(/```\s*$/iu,"").replace(/^\s*<\s*tool_call[^>]*>\s*/iu,"").replace(/\s*<\s*\/\s*tool_call\s*>\s*$/iu,"").trim()}(e),r=readXmlToolCalls(o,t);if(r.length>0)return r;const a=function readFunctionToolCalls(e,t){const n=[];for(const o of t){const t=new RegExp(`(?:^|[\\s<])${escapeRegexp(o)}\\s*\\(([^)]*)\\)\\s*>?`,"giu");for(const r of e.matchAll(t)){const e=readFunctionArgs(r[1]??"");e&&Object.keys(e).length>0&&n.push({id:`stable-harness-${o}-${Date.now().toString(36)}`,name:o,args:e})}}return n}(o,t);return a.length>0?a:function parseJsonObjects(e){const t=[];let n=e.indexOf("{");for(;n>=0;){const o=readBalancedJsonObject(e,n);if(o){try{const e=JSON.parse(o);isRecord(e)&&t.push(e)}catch{}n=e.indexOf("{",n+o.length)}else n=e.indexOf("{",n+1)}return t}(o).flatMap(e=>function readJsonToolCall(e,t){const n=function readNestedToolCall(e,t){const n=Object.entries(e);if(1!==n.length)return;const[o,r]=n[0];return t.has(o)&&isRecord(r)?{id:`stable-harness-${o}-${Date.now().toString(36)}`,name:o,args:r}:void 0}(e,t);if(n)return n;const o=readString(e.name)??readString(e.tool_name)??readString(e.tool)??readString(e.type),r=t.has(o??"")?o:singleToolName(t),a=function readArgs(e){return isRecord(e.arguments)?e.arguments:isRecord(e.parameters)?e.parameters:isRecord(e.args)?e.args:"args"in e&&void 0!==e.args?{args:e.args}:void 0}(e)??function readInlineArgs(e){const t=Object.fromEntries(Object.entries(e).filter(([e])=>!["name","tool_name","tool","type"].includes(e)));return Object.keys(t).length>0?t:void 0}(e);return r&&t.has(r)&&a?{id:`stable-harness-${r}-${Date.now().toString(36)}`,name:r,args:a}:void 0}(e,t)??[])}(o,n);return 0!==r.length?new e({content:"",id:"string"==typeof t.id?t.id:void 0,name:"string"==typeof t.name?t.name:void 0,additional_kwargs:isRecord(t.additional_kwargs)?t.additional_kwargs:{},response_metadata:isRecord(t.response_metadata)?t.response_metadata:{},tool_calls:r.map(e=>({id:e.id,name:e.name,args:e.args,type:"tool_call"}))}):void 0}(r,function visibleToolNames(e){return new Set((e.tools??[]).map(e=>e.name).filter(e=>"string"==typeof e&&e.length>0))}(n));return a??r}}}function readXmlToolCalls(e,t){const n=[],o=/<\s*(tool_call|tool_code)\b[^>]*>(?:[\s\S]*?<\s*\/\s*\1\s*>)?/giu;for(const r of e.matchAll(o)){const e=readXmlToolCall(r[0],t);e&&n.push(e)}return n}function readXmlToolCall(e,t){const n=function readToolCallAttributes(e){const t=/<\s*(?:tool_call|tool_code)\b([^>]*)>/iu.exec(e);if(!t)return;const n=[...t[1].matchAll(/([a-zA-Z_][\w.-]*)\s*=\s*"([^"]*)"/gu)];return n.length>0?Object.fromEntries(n.map(e=>[e[1],e[2]])):void 0}(e),o=readString(n?.name)??readString(n?.tool_name)??readString(n?.tool)??readXmlTag(e,"tool_name")??readXmlTag(e,"name")??readXmlTag(e,"tool")??function readBareXmlToolName(e,t){const n=readBareXmlToolBody(e),o=n?.trim();return o&&t.has(o)?o:void 0}(e,t),r=t.has(o??"")?o:singleToolName(t);if(!r&&!o&&t.has("task")){const t=readBareXmlToolBody(e);if(t)return{id:`stable-harness-task-${Date.now().toString(36)}`,name:"task",args:{input:t}}}if(!r||!t.has(r))return;const a=readXmlTag(e,"tool_args")??readXmlTag(e,"args")??readXmlTag(e,"arguments"),s=a?function readXmlArgs(e){const t=[...e.matchAll(/<\s*([a-zA-Z_][\w.-]*)\s*>([\s\S]*?)<\s*\/\s*\1\s*>/gu)];if(0===t.length){const t=e.trim();return t?{input:t}:void 0}return Object.fromEntries(t.map(e=>[e[1],e[2].trim()]))}(a):function readAttributeArgs(e){if(!e)return;const t=Object.fromEntries(Object.entries(e).filter(([e])=>!["name","tool_name","tool","type"].includes(e)));return Object.keys(t).length>0?t:void 0}(n);return!s&&o?{id:`stable-harness-${r}-${Date.now().toString(36)}`,name:r,args:{}}:s&&0!==Object.keys(s).length?{id:`stable-harness-${r}-${Date.now().toString(36)}`,name:r,args:s}:void 0}function readXmlTag(e,t){const n=new RegExp(`<\\s*${t}\\s*>([\\s\\S]*?)<\\s*\\/\\s*${t}\\s*>`,"iu").exec(e);return n?.[1]?.trim()||void 0}function readBareXmlToolBody(e){return function readXmlElementBody(e,t){const n=function readOpeningTag(e,t){for(const n of t){const t=e.indexOf(`<${n}`);if(t<0)continue;const o=e.indexOf(">",t+n.length+1);if(o>=0)return{end:o+1}}}(e,t);if(!n)return;const o=function findClosingTag(e,t,n){let o;for(const r of t){const t=[`</${r}>`,`</ ${r}>`];for(const r of t){const t=e.indexOf(r,n);t>=0&&(void 0===o||t<o)&&(o=t)}}return o}(e,t,n.end);return void 0===o?void 0:e.slice(n.end,o)}(e,["tool_call","tool_code"])?.trim()||void 0}function readFunctionArgs(e){const t=[...e.matchAll(/([a-zA-Z_][\w.-]*)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^,\s]+))/gu)];return t.length>0?Object.fromEntries(t.map(e=>[e[1],e[2]??e[3]??e[4]??""])):void 0}function readBalancedJsonObject(e,t){let n=0,o=!1,r=!1;for(let a=t;a<e.length;a+=1){const s=e[a];if(o)r="\\"===s&&!r,'"'!==s||r||(o=!1),"\\"!==s&&(r=!1);else if('"'===s&&(o=!0),"{"===s&&(n+=1),"}"===s&&(n-=1),0===n)return e.slice(t,a+1)}}function singleToolName(e){return 1===e.size?[...e][0]:void 0}function escapeRegexp(e){return e.replace(/[.*+?^${}()|[\]\\]/gu,"\\$&")}function readString(e){return"string"==typeof e&&e.trim()?e.trim():void 0}function isRecord(e){return"object"==typeof e&&null!==e&&!Array.isArray(e)}
|
|
1
|
+
import{AIMessage as e}from"@langchain/core/messages";export function createRawToolCallParserMiddleware(t){return{name:"StableHarnessRawToolCallParser",async wrapModelCall(n,o){const r=await o(n);if(!function rawToolCallParsingEnabled(e){const t=isRecord(e.workspace.runtime.recovery)?e.workspace.runtime.recovery:{};return!0===(isRecord(t.toolCall)?t.toolCall:{}).enabled}(t))return r;const a=function parseRawToolCallResponse(t,n){if(0===n.size||!function isModelResponse(e){return isRecord(e)}(t)||function hasToolCalls(e){return Array.isArray(e.tool_calls)&&e.tool_calls.length>0}(t))return;const o=function readContentText(e){return"string"==typeof e?e.trim()?e:void 0:Array.isArray(e)&&e.map(e=>isRecord(e)&&"string"==typeof e.text?e.text:"").join("\n").trim()||void 0}(t.content);if(!o)return;const r=function readRawToolCalls(e,t){const n=readXmlToolCalls(e,t);if(n.length>0)return n;const o=function stripToolMarkup(e){return e.replace(/^\s*```(?:json)?/iu,"").replace(/```\s*$/iu,"").replace(/^\s*<\s*tool_call[^>]*>\s*/iu,"").replace(/\s*<\s*\/\s*tool_call\s*>\s*$/iu,"").trim()}(e),r=readXmlToolCalls(o,t);if(r.length>0)return r;const a=function readFunctionToolCalls(e,t){const n=[];for(const o of t){const t=new RegExp(`(?:^|[\\s<])${escapeRegexp(o)}\\s*\\(([^)]*)\\)\\s*>?`,"giu");for(const r of e.matchAll(t)){const e=readFunctionArgs(r[1]??"");e&&Object.keys(e).length>0&&n.push({id:`stable-harness-${o}-${Date.now().toString(36)}`,name:o,args:e})}}return n}(o,t);return a.length>0?a:function parseJsonObjects(e){const t=[];let n=e.indexOf("{");for(;n>=0;){const o=readBalancedJsonObject(e,n);if(o){try{const e=JSON.parse(o);isRecord(e)&&t.push(e)}catch{}n=e.indexOf("{",n+o.length)}else n=e.indexOf("{",n+1)}return t}(o).flatMap(e=>function readJsonToolCall(e,t){const n=function readNestedToolCall(e,t){const n=Object.entries(e);if(1!==n.length)return;const[o,r]=n[0];return t.has(o)&&isRecord(r)?{id:`stable-harness-${o}-${Date.now().toString(36)}`,name:o,args:r}:void 0}(e,t);if(n)return n;const o=readString(e.name)??readString(e.tool_name)??readString(e.tool_id)??readString(e.tool)??readString(e.type),r=t.has(o??"")?o:singleToolName(t),a=function readArgs(e){return isRecord(e.arguments)?e.arguments:isRecord(e.parameters)?e.parameters:isRecord(e.tool_kwargs)?e.tool_kwargs:isRecord(e.args)?e.args:"args"in e&&void 0!==e.args?{args:e.args}:void 0}(e)??function readInlineArgs(e){const t=Object.fromEntries(Object.entries(e).filter(([e])=>!["name","tool_name","tool","type"].includes(e)));return Object.keys(t).length>0?t:void 0}(e);return r&&t.has(r)&&a?{id:`stable-harness-${r}-${Date.now().toString(36)}`,name:r,args:a}:void 0}(e,t)??[])}(o,n);return 0!==r.length?new e({content:"",id:"string"==typeof t.id?t.id:void 0,name:"string"==typeof t.name?t.name:void 0,additional_kwargs:isRecord(t.additional_kwargs)?t.additional_kwargs:{},response_metadata:isRecord(t.response_metadata)?t.response_metadata:{},tool_calls:r.map(e=>({id:e.id,name:e.name,args:e.args,type:"tool_call"}))}):void 0}(r,function visibleToolNames(e){return new Set((e.tools??[]).map(e=>e.name).filter(e=>"string"==typeof e&&e.length>0))}(n));return a??r}}}function readXmlToolCalls(e,t){const n=[],o=/<\s*(tool_call|tool_code)\b[^>]*>(?:[\s\S]*?<\s*\/\s*\1\s*>)?/giu;for(const r of e.matchAll(o)){const e=readXmlToolCall(r[0],t);e&&n.push(e)}return n}function readXmlToolCall(e,t){const n=function readToolCallAttributes(e){const t=/<\s*(?:tool_call|tool_code)\b([^>]*)>/iu.exec(e);if(!t)return;const n=[...t[1].matchAll(/([a-zA-Z_][\w.-]*)\s*=\s*"([^"]*)"/gu)];return n.length>0?Object.fromEntries(n.map(e=>[e[1],e[2]])):void 0}(e),o=readString(n?.name)??readString(n?.tool_name)??readString(n?.tool)??readXmlTag(e,"tool_name")??readXmlTag(e,"name")??readXmlTag(e,"tool")??function readBareXmlToolName(e,t){const n=readBareXmlToolBody(e),o=n?.trim();return o&&t.has(o)?o:void 0}(e,t),r=t.has(o??"")?o:singleToolName(t);if(!r&&!o&&t.has("task")){const t=readBareXmlToolBody(e);if(t)return{id:`stable-harness-task-${Date.now().toString(36)}`,name:"task",args:{input:t}}}if(!r||!t.has(r))return;const a=readXmlTag(e,"tool_args")??readXmlTag(e,"args")??readXmlTag(e,"arguments"),s=a?function readXmlArgs(e){const t=[...e.matchAll(/<\s*([a-zA-Z_][\w.-]*)\s*>([\s\S]*?)<\s*\/\s*\1\s*>/gu)];if(0===t.length){const t=e.trim();return t?{input:t}:void 0}return Object.fromEntries(t.map(e=>[e[1],e[2].trim()]))}(a):function readAttributeArgs(e){if(!e)return;const t=Object.fromEntries(Object.entries(e).filter(([e])=>!["name","tool_name","tool","type"].includes(e)));return Object.keys(t).length>0?t:void 0}(n);return!s&&o?{id:`stable-harness-${r}-${Date.now().toString(36)}`,name:r,args:{}}:s&&0!==Object.keys(s).length?{id:`stable-harness-${r}-${Date.now().toString(36)}`,name:r,args:s}:void 0}function readXmlTag(e,t){const n=new RegExp(`<\\s*${t}\\s*>([\\s\\S]*?)<\\s*\\/\\s*${t}\\s*>`,"iu").exec(e);return n?.[1]?.trim()||void 0}function readBareXmlToolBody(e){return function readXmlElementBody(e,t){const n=function readOpeningTag(e,t){for(const n of t){const t=e.indexOf(`<${n}`);if(t<0)continue;const o=e.indexOf(">",t+n.length+1);if(o>=0)return{end:o+1}}}(e,t);if(!n)return;const o=function findClosingTag(e,t,n){let o;for(const r of t){const t=[`</${r}>`,`</ ${r}>`];for(const r of t){const t=e.indexOf(r,n);t>=0&&(void 0===o||t<o)&&(o=t)}}return o}(e,t,n.end);return void 0===o?void 0:e.slice(n.end,o)}(e,["tool_call","tool_code"])?.trim()||void 0}function readFunctionArgs(e){const t=[...e.matchAll(/([a-zA-Z_][\w.-]*)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^,\s]+))/gu)];return t.length>0?Object.fromEntries(t.map(e=>[e[1],e[2]??e[3]??e[4]??""])):void 0}function readBalancedJsonObject(e,t){let n=0,o=!1,r=!1;for(let a=t;a<e.length;a+=1){const s=e[a];if(o)r="\\"===s&&!r,'"'!==s||r||(o=!1),"\\"!==s&&(r=!1);else if('"'===s&&(o=!0),"{"===s&&(n+=1),"}"===s&&(n-=1),0===n)return e.slice(t,a+1)}}function singleToolName(e){return 1===e.size?[...e][0]:void 0}function escapeRegexp(e){return e.replace(/[.*+?^${}()|[\]\\]/gu,"\\$&")}function readString(e){return"string"==typeof e&&e.trim()?e.trim():void 0}function isRecord(e){return"object"==typeof e&&null!==e&&!Array.isArray(e)}
|