Socket
Socket
Sign inDemoInstall

@axflow/models

Package Overview
Dependencies
Maintainers
2
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@axflow/models - npm Package Compare versions

Comparing version 0.0.22 to 0.0.23

56

dist/react/index.d.ts

@@ -1,2 +0,2 @@

import { MessageType, FunctionType, JSONValueType } from '@axflow/models/shared';
import { MessageType, FunctionType, ToolType, JSONValueType } from '@axflow/models/shared';

@@ -12,2 +12,13 @@ interface AccessorType<T = any> {

}
interface ToolCallsAccessorType<T = any> {
(value: T): Array<{
index?: number;
id?: string;
type?: 'function';
function: {
name?: string;
arguments: string;
};
}> | null | undefined;
}
type BodyType = Record<string, JSONValueType> | ((message: MessageType, history: MessageType[]) => JSONValueType);

@@ -99,2 +110,29 @@ /**

/**
* An accessor used to pluck out multiple tool calls for LLMs that support it.
* This is the new nomenclature for openAI's functions, since they now can return
* multiple in one call.
*
* This is used to return the object which will then be populated
* on the assistant message's `tool_calls` property. A tool_call object
* consists of a index, id, type (always 'function') and function object
* with name and parameters, encoded as JSON like for the functionCall above.
*
* For example, if this hook is used to stream an OpenAI-compatible API response
* using openAI tools, the following options can be defined to interpret the response:
*
* import { useChat } from '@axflow/models/react';
* import type { OpenAIChatTypes } from '@axflow/models/openai/chat';
*
* const { ... } = useChat({
* accessor: (value: OpenAIChatTypes.Chunk) => {
* return value.choices[0].delta.content;
* },
*
* toolCallsAccessor: (value: OpenAIChatTypes.Chunk) => {
* return value.choices[0].delta.tool_calls;
* }
* });
*/
toolCallsAccessor?: ToolCallsAccessorType;
/**
* Initial message input. Defaults to empty string.

@@ -116,2 +154,10 @@ */

/**
* Initial seet of available tools, which replaced functions, for the user's
* next message.
*
* @see https://platform.openai.com/docs/api-reference/chat/create
*
*/
initialTools?: ToolType[];
/**
* Callback to handle errors should they arise.

@@ -181,2 +227,10 @@ *

/**
* Update list of tools for the next user message.
*
* This is primarily intended for OpenAI's tools feature.
*
* @see https://platform.openai.com/docs/api-reference/chat/create
*/
setTools: (tools: ToolType[]) => void;
/**
* If a request is in progress, this will be `true`.

@@ -183,0 +237,0 @@ *

@@ -33,3 +33,34 @@ "use strict";

}
async function handleStreamingResponse(response, messagesRef, setMessages, accessor, functionCallAccessor, onNewMessage, createMessageId) {
var mergeToolCallIntoMessage = (partialChunkToolCall, msg, content) => {
const msgContainsChunkTool = (msg.toolCalls || []).some(
(tool) => tool.index === partialChunkToolCall.index
);
if (!msgContainsChunkTool) {
return {
...msg,
content,
toolCalls: [...msg.toolCalls || [], (0, import_shared.toolCallWithDefaults)(partialChunkToolCall)]
};
} else {
return {
...msg,
toolCalls: msg.toolCalls.map((toolCall) => {
if (toolCall.index !== partialChunkToolCall.index) {
return toolCall;
} else {
return {
...toolCall,
...partialChunkToolCall,
function: {
...toolCall.function,
...partialChunkToolCall.function,
arguments: (toolCall.function.arguments || "") + (partialChunkToolCall.function?.arguments || "")
}
};
}
})
};
}
};
async function handleStreamingResponse(response, messagesRef, setMessages, accessor, functionCallAccessor, toolCallsAccessor, onNewMessage, createMessageId) {
const responseBody = response.body;

@@ -63,2 +94,3 @@ if (responseBody === null) {

const chunkFunctionCall = functionCallAccessor(chunk.value);
const chunkToolCalls = toolCallsAccessor(chunk.value);
if (!id) {

@@ -78,2 +110,5 @@ id = createMessageId();

}
if (chunkToolCalls) {
message.toolCalls = chunkToolCalls.map(import_shared.toolCallWithDefaults);
}
messages = messages.concat(message);

@@ -86,8 +121,14 @@ } else {

const content = msg.content + (chunkContent ?? "");
if (!chunkFunctionCall) {
if (chunkFunctionCall) {
const name = msg.functionCall.name + (chunkFunctionCall.name ?? "");
const args = msg.functionCall.arguments + (chunkFunctionCall.arguments ?? "");
return { ...msg, content, functionCall: { name, arguments: args } };
} else if (chunkToolCalls) {
for (const chunkToolCall of chunkToolCalls) {
msg = mergeToolCallIntoMessage(chunkToolCall, msg, content);
}
return msg;
} else {
return { ...msg, content };
}
const name = msg.functionCall.name + (chunkFunctionCall.name ?? "");
const args = msg.functionCall.arguments + (chunkFunctionCall.arguments ?? "");
return { ...msg, content, functionCall: { name, arguments: args } };
});

@@ -101,6 +142,7 @@ }

}
async function handleJsonResponse(response, messagesRef, setMessages, accessor, functionCallAccessor, onNewMessage, createMessageId) {
async function handleJsonResponse(response, messagesRef, setMessages, accessor, functionCallAccessor, toolCallsAccessor, onNewMessage, createMessageId) {
const responseBody = await response.json();
const content = accessor(responseBody);
const functionCall = functionCallAccessor(responseBody);
const toolCalls = toolCallsAccessor(responseBody);
const newMessage = {

@@ -118,2 +160,5 @@ id: createMessageId(),

}
if (toolCalls) {
newMessage.toolCalls = toolCalls.map(import_shared.toolCallWithDefaults);
}
const messages = messagesRef.current.concat(newMessage);

@@ -123,3 +168,3 @@ setMessages(messages);

}
async function request(prepare, messagesRef, setMessages, url, headers, accessor, functionCallAccessor, loadingRef, setLoading, setError, onError, onNewMessage, onSuccess, createMessageId) {
async function request(prepare, messagesRef, setMessages, url, headers, accessor, functionCallAccessor, toolCallsAccessor, loadingRef, setLoading, setError, onError, onNewMessage, onSuccess, createMessageId) {
if (loadingRef.current) {

@@ -146,2 +191,3 @@ return;

functionCallAccessor,
toolCallsAccessor,
onNewMessage,

@@ -158,3 +204,3 @@ createMessageId

}
async function stableAppend(message, messagesRef, setMessages, url, headers, body, accessor, functionCallAccessor, loadingRef, setLoading, setError, onError, onNewMessage, setFunctions, createMessageId) {
async function stableAppend(message, messagesRef, setMessages, url, headers, body, accessor, functionCallAccessor, toolCallsAccessor, loadingRef, setLoading, setError, onError, onNewMessage, setFunctions, setTools, createMessageId) {
function prepare() {

@@ -175,2 +221,3 @@ const history = messagesRef.current;

functionCallAccessor,
toolCallsAccessor,
loadingRef,

@@ -181,3 +228,6 @@ setLoading,

onNewMessage,
() => setFunctions([]),
() => {
setFunctions([]);
setTools([]);
},
// Clear functions after each request (similar to clearing user input)

@@ -187,3 +237,3 @@ createMessageId

}
async function stableReload(messagesRef, setMessages, url, headers, body, accessor, functionCallAccessor, loadingRef, setLoading, setError, onError, onNewMessage, createMessageId) {
async function stableReload(messagesRef, setMessages, url, headers, body, accessor, functionCallAccessor, toolCallsAccessor, loadingRef, setLoading, setError, onError, onNewMessage, createMessageId) {
function prepare() {

@@ -219,2 +269,3 @@ const messages = messagesRef.current;

functionCallAccessor,
toolCallsAccessor,
loadingRef,

@@ -238,2 +289,5 @@ setLoading,

};
var DEFAULT_TOOL_CALLS_ACCESSOR = (_value) => {
return void 0;
};
var DEFAULT_BODY = (message, history) => ({

@@ -259,2 +313,4 @@ messages: [...history, message]

const [functions, setFunctions] = (0, import_react.useState)(initialFunctions);
const initialTools = options.initialTools ?? [];
const [tools, setTools] = (0, import_react.useState)(initialTools);
const [loading, _setLoading] = (0, import_react.useState)(false);

@@ -267,2 +323,3 @@ const loadingRef = (0, import_react.useRef)(false);

const functionCallAccessor = options.functionCallAccessor ?? DEFAULT_FUNCTION_CALL_ACCESSOR;
const toolCallsAccessor = options.toolCallsAccessor ?? DEFAULT_TOOL_CALLS_ACCESSOR;
const body = options.body ?? DEFAULT_BODY;

@@ -308,2 +365,5 @@ const headers = options.headers ?? DEFAULT_HEADERS;

}
if (tools.length > 0) {
newMessage.tools = tools;
}
stableAppend(

@@ -318,2 +378,3 @@ newMessage,

functionCallAccessor,
toolCallsAccessor,
loadingRef,

@@ -325,2 +386,3 @@ setLoading,

setFunctions,
setTools,
createMessageId

@@ -339,2 +401,3 @@ );

functionCallAccessor,
toolCallsAccessor,
loadingRef,

@@ -355,2 +418,3 @@ setLoading,

setFunctions,
setTools,
loading,

@@ -357,0 +421,0 @@ error,

@@ -12,2 +12,6 @@ declare class HttpError extends Error {

type RecursivePartial<T> = {
[P in keyof T]?: T[P] extends (infer U)[] ? RecursivePartial<U>[] : T[P] extends object | undefined ? RecursivePartial<T[P]> : T[P];
};
type JSONValueType = null | string | number | boolean | {

@@ -21,2 +25,16 @@ [x: string]: JSONValueType;

};
type ToolType = {
type: 'function';
function: FunctionType;
};
type ToolCallType = {
index: number;
id: string;
type: 'function';
function: {
name: string;
arguments: string;
};
};
declare const toolCallWithDefaults: (toolCall: RecursivePartial<ToolCallType>) => ToolCallType;
type MessageType = {

@@ -66,2 +84,16 @@ /**

};
/**
* If using openAI tools, the tools available to the assistant can be defined here.
*
* @see https://platform.openai.com/docs/guides/function-calling
*/
tools?: ToolType[];
/**
* If using OpenAI tools and the assistant responds with one or more tool calls,
* this field will be populated with the tool invocation information.
*
*
* @see https://platform.openai.com/docs/guides/function-calling
*/
toolCalls?: ToolCallType[];
};

@@ -207,2 +239,2 @@

export { FunctionType, HttpError, IterableToStream, JSONValueType, MessageType, NdJsonStream, NdJsonValueType, POST, StreamToIterable, StreamingJsonResponse, createMessage, isHttpError };
export { FunctionType, HttpError, IterableToStream, JSONValueType, MessageType, NdJsonStream, NdJsonValueType, POST, RecursivePartial, StreamToIterable, StreamingJsonResponse, ToolCallType, ToolType, createMessage, isHttpError, toolCallWithDefaults };

@@ -30,3 +30,4 @@ "use strict";

createMessage: () => createMessage,
isHttpError: () => isHttpError
isHttpError: () => isHttpError,
toolCallWithDefaults: () => toolCallWithDefaults
});

@@ -254,2 +255,13 @@ module.exports = __toCommonJS(shared_exports);

// src/shared/types.ts
var toolCallWithDefaults = (toolCall) => ({
index: toolCall.index ?? 0,
id: toolCall.id ?? "",
type: "function",
function: {
name: toolCall.function?.name ?? "",
arguments: toolCall.function?.arguments ?? ""
}
});
// src/shared/message.ts

@@ -274,3 +286,4 @@ var createMessage = (message) => {

createMessage,
isHttpError
isHttpError,
toolCallWithDefaults
});

4

package.json
{
"name": "@axflow/models",
"version": "0.0.22",
"version": "0.0.23",
"description": "Zero-dependency, modular SDK for building robust natural language applications",

@@ -199,3 +199,3 @@ "author": "Axflow (https://axflow.dev)",

},
"gitHead": "fe68d6e2f3cf2eab0f18808f95dd31ddfd4cd9d6"
"gitHead": "3f3abc72d4e38a4201d0425f0b8ae29fa283725c"
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc