Comparing version 0.4.0 to 0.5.0
import OpenAI from 'openai'; | ||
import { z } from 'zod'; | ||
type MessageFunctionCall = { | ||
/** | ||
* For simple text responses | ||
*/ | ||
declare const chatTextSchema: z.ZodObject<{ | ||
id: z.ZodString; | ||
sender: z.ZodString; | ||
timestamp: z.ZodString; | ||
type: z.ZodLiteral<"text">; | ||
text: z.ZodString; | ||
}, "strip", z.ZodTypeAny, { | ||
type: "text"; | ||
id: string; | ||
type: 'function_call'; | ||
sender: 'iudex'; | ||
sender: string; | ||
timestamp: string; | ||
text: string; | ||
}, { | ||
type: "text"; | ||
id: string; | ||
sender: string; | ||
timestamp: string; | ||
text: string; | ||
}>; | ||
type ChatText = z.infer<typeof chatTextSchema>; | ||
declare const chatFunctionCallSchema: z.ZodObject<{ | ||
id: z.ZodString; | ||
sender: z.ZodString; | ||
timestamp: z.ZodString; | ||
type: z.ZodLiteral<"functionCall">; | ||
functionCallId: z.ZodString; | ||
functionName: z.ZodString; | ||
functionArgs: z.ZodRecord<z.ZodString, z.ZodUnknown>; | ||
}, "strip", z.ZodTypeAny, { | ||
type: "functionCall"; | ||
id: string; | ||
sender: string; | ||
timestamp: string; | ||
functionCallId: string; | ||
functionName: string; | ||
functionArgs: any; | ||
}; | ||
type MessageText = { | ||
functionArgs: Record<string, unknown>; | ||
}, { | ||
type: "functionCall"; | ||
id: string; | ||
type: 'text'; | ||
sender: 'iudex'; | ||
sender: string; | ||
timestamp: string; | ||
functionCallId: string; | ||
functionName: string; | ||
functionArgs: Record<string, unknown>; | ||
}>; | ||
type ChatFunctionCall = z.infer<typeof chatFunctionCallSchema>; | ||
declare const chatFunctionReturnSchema: z.ZodObject<{ | ||
id: z.ZodString; | ||
sender: z.ZodString; | ||
timestamp: z.ZodString; | ||
type: z.ZodLiteral<"functionReturn">; | ||
functionCallId: z.ZodString; | ||
functionReturn: z.ZodString; | ||
}, "strip", z.ZodTypeAny, { | ||
type: "functionReturn"; | ||
id: string; | ||
sender: string; | ||
timestamp: string; | ||
functionCallId: string; | ||
functionReturn: string; | ||
}, { | ||
type: "functionReturn"; | ||
id: string; | ||
sender: string; | ||
timestamp: string; | ||
functionCallId: string; | ||
functionReturn: string; | ||
}>; | ||
type ChatFunctionReturn = z.infer<typeof chatFunctionReturnSchema>; | ||
/** | ||
* All chat turn types | ||
*/ | ||
declare const chatTurnSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{ | ||
id: z.ZodString; | ||
sender: z.ZodString; | ||
timestamp: z.ZodString; | ||
type: z.ZodLiteral<"text">; | ||
text: z.ZodString; | ||
}, "strip", z.ZodTypeAny, { | ||
type: "text"; | ||
id: string; | ||
sender: string; | ||
timestamp: string; | ||
text: string; | ||
}, { | ||
type: "text"; | ||
id: string; | ||
sender: string; | ||
timestamp: string; | ||
text: string; | ||
}>, z.ZodObject<{ | ||
id: z.ZodString; | ||
sender: z.ZodString; | ||
timestamp: z.ZodString; | ||
type: z.ZodLiteral<"image">; | ||
image: z.ZodString; | ||
description: z.ZodString; | ||
}, "strip", z.ZodTypeAny, { | ||
type: "image"; | ||
id: string; | ||
sender: string; | ||
timestamp: string; | ||
image: string; | ||
description: string; | ||
}, { | ||
type: "image"; | ||
id: string; | ||
sender: string; | ||
timestamp: string; | ||
image: string; | ||
description: string; | ||
}>, z.ZodObject<{ | ||
id: z.ZodString; | ||
sender: z.ZodString; | ||
timestamp: z.ZodString; | ||
type: z.ZodLiteral<"list">; | ||
list: z.ZodArray<z.ZodString, "many">; | ||
}, "strip", z.ZodTypeAny, { | ||
type: "list"; | ||
id: string; | ||
sender: string; | ||
timestamp: string; | ||
list: string[]; | ||
}, { | ||
type: "list"; | ||
id: string; | ||
sender: string; | ||
timestamp: string; | ||
list: string[]; | ||
}>, z.ZodObject<{ | ||
id: z.ZodString; | ||
sender: z.ZodString; | ||
timestamp: z.ZodString; | ||
type: z.ZodLiteral<"functionCall">; | ||
functionCallId: z.ZodString; | ||
functionName: z.ZodString; | ||
functionArgs: z.ZodRecord<z.ZodString, z.ZodUnknown>; | ||
}, "strip", z.ZodTypeAny, { | ||
type: "functionCall"; | ||
id: string; | ||
sender: string; | ||
timestamp: string; | ||
functionCallId: string; | ||
functionName: string; | ||
functionArgs: Record<string, unknown>; | ||
}, { | ||
type: "functionCall"; | ||
id: string; | ||
sender: string; | ||
timestamp: string; | ||
functionCallId: string; | ||
functionName: string; | ||
functionArgs: Record<string, unknown>; | ||
}>, z.ZodObject<{ | ||
id: z.ZodString; | ||
sender: z.ZodString; | ||
timestamp: z.ZodString; | ||
type: z.ZodLiteral<"functionReturn">; | ||
functionCallId: z.ZodString; | ||
functionReturn: z.ZodString; | ||
}, "strip", z.ZodTypeAny, { | ||
type: "functionReturn"; | ||
id: string; | ||
sender: string; | ||
timestamp: string; | ||
functionCallId: string; | ||
functionReturn: string; | ||
}, { | ||
type: "functionReturn"; | ||
id: string; | ||
sender: string; | ||
timestamp: string; | ||
functionCallId: string; | ||
functionReturn: string; | ||
}>]>; | ||
type ChatTurn = z.infer<typeof chatTurnSchema>; | ||
type ReturnFunctionCallBody = Pick<ChatFunctionReturn, 'functionCallId' | 'functionReturn'>; | ||
type ReturnFunctionCallRes = { | ||
workflowId: string; | ||
message: string; | ||
}; | ||
type IudexMessage = MessageFunctionCall | MessageText; | ||
declare function mapIudexToOpenAi(m: IudexMessage): Omit<OpenAI.ChatCompletion, 'model'>; | ||
declare function returnFunctionCall(baseUrl: string, apiKey: string): (functionCallId: string, functionReturn: any) => Promise<void>; | ||
type NextMessageRes = ChatFunctionCall | ChatText | undefined; | ||
declare function nextMessage(baseUrl: string, apiKey: string): (workflowId: string) => Promise<NextMessageRes>; | ||
type StartWorkflowRes = { | ||
workflowId: string; | ||
message: string; | ||
}; | ||
declare function startWorkflow(baseUrl: string, apiKey: string): (query: string, modules?: string) => Promise<StartWorkflowRes>; | ||
declare const DEFAULT_BASE_URL = "https://5pz08znmzj.execute-api.us-west-2.amazonaws.com"; | ||
type IudexMessage = ChatTurn; | ||
type ChatCompletionMessageWithIudex = OpenAI.ChatCompletionMessageParam & ({ | ||
tool_call_id?: string; | ||
workflowId?: string; | ||
} | { | ||
tool_call_id: string; | ||
workflowId: string; | ||
}); | ||
type ChatCompletionWithIudex = OpenAI.ChatCompletion & { | ||
choices: Array<OpenAI.ChatCompletion.Choice & { | ||
message: OpenAI.ChatCompletionMessage & { | ||
workflowId: string; | ||
}; | ||
}>; | ||
}; | ||
/** | ||
* Iudex api client. | ||
*/ | ||
declare class Iudex { | ||
@@ -28,17 +225,21 @@ baseUrl: string; | ||
chatCompletionsCreate: (body: OpenAI.ChatCompletionCreateParamsNonStreaming & { | ||
messages: Array<OpenAI.ChatCompletionMessageParam & { | ||
tool_call_id?: string; | ||
}>; | ||
}) => Promise<OpenAI.ChatCompletion>; | ||
messages: Array<ChatCompletionMessageWithIudex>; | ||
}) => Promise<ChatCompletionWithIudex>; | ||
chat: { | ||
completions: { | ||
create: (body: OpenAI.ChatCompletionCreateParamsNonStreaming & { | ||
messages: Array<OpenAI.ChatCompletionMessageParam & { | ||
tool_call_id?: string; | ||
}>; | ||
}) => Promise<OpenAI.ChatCompletion>; | ||
messages: Array<ChatCompletionMessageWithIudex>; | ||
}) => Promise<ChatCompletionWithIudex>; | ||
}; | ||
}; | ||
} | ||
/** | ||
* Maps IudexMessage to OpenAI.ChatCompletion. | ||
*/ | ||
declare function mapIudexToOpenAi(m: IudexMessage, workflowId: string): Omit<ChatCompletionWithIudex, 'model'>; | ||
/** | ||
* Extracts OpenAI message content as a string. | ||
*/ | ||
declare function extractMessageTextContent(content: OpenAI.ChatCompletionUserMessageParam['content']): string; | ||
export { Iudex, type IudexMessage, Iudex as default, mapIudexToOpenAi }; | ||
export { type ChatCompletionMessageWithIudex, type ChatCompletionWithIudex, DEFAULT_BASE_URL, Iudex, type IudexMessage, type NextMessageRes, type ReturnFunctionCallBody, type ReturnFunctionCallRes, type StartWorkflowRes, Iudex as default, extractMessageTextContent, mapIudexToOpenAi, nextMessage, returnFunctionCall, startWorkflow }; |
@@ -23,46 +23,95 @@ "use strict"; | ||
__export(src_exports, { | ||
DEFAULT_BASE_URL: () => DEFAULT_BASE_URL, | ||
Iudex: () => Iudex, | ||
default: () => src_default, | ||
mapIudexToOpenAi: () => mapIudexToOpenAi | ||
extractMessageTextContent: () => extractMessageTextContent, | ||
mapIudexToOpenAi: () => mapIudexToOpenAi, | ||
nextMessage: () => nextMessage, | ||
returnFunctionCall: () => returnFunctionCall, | ||
startWorkflow: () => startWorkflow | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
function mapIudexToOpenAi(m) { | ||
if (m.type === "function_call") { | ||
const message2 = { | ||
content: null, | ||
role: "assistant", | ||
tool_calls: [{ | ||
id: "r.callId", | ||
function: { name: m.functionName, arguments: JSON.stringify(m.functionArgs) }, | ||
type: "function" | ||
}] | ||
// src/client.ts | ||
function checkResponse(r) { | ||
if (!r.ok) { | ||
throw Error(`Request ${r.url} failed with ${r.status}: ${r.statusText}`); | ||
} | ||
return r; | ||
} | ||
function throwOnApiError(json) { | ||
if (!json) { | ||
return json; | ||
} | ||
if (json.message === "Service Unavailable") { | ||
throw Error(json.message); | ||
} | ||
return json; | ||
} | ||
function unwrapApi(json) { | ||
if (json && json.body && typeof json.body === "string" && json.body.startsWith("{") && json.body.endsWith("}")) { | ||
return JSON.parse(json.body); | ||
} | ||
return json; | ||
} | ||
function parseIudexResponse(r) { | ||
return checkResponse(r).json().then(throwOnApiError).then(unwrapApi).catch((e) => { | ||
throw Error(`Request ${r.url} failed with ${r.status}: ${e.message}`); | ||
}); | ||
} | ||
function returnFunctionCall(baseUrl, apiKey) { | ||
return function(functionCallId, functionReturn) { | ||
const bodyJson = { | ||
functionCallId, | ||
functionReturn: JSON.stringify(functionReturn) | ||
}; | ||
return { | ||
id: m.id, | ||
choices: [{ | ||
index: 0, | ||
finish_reason: "tool_calls", | ||
logprobs: null, | ||
message: message2 | ||
}], | ||
created: new Date(m.timestamp).valueOf(), | ||
object: "chat.completion" | ||
}; | ||
} | ||
const message = { | ||
content: m.text, | ||
role: "assistant" | ||
return fetch(baseUrl + "/function_calls/" + functionCallId + "/return", { | ||
method: "PUT", | ||
headers: { Authorization: `Bearer ${apiKey}` }, | ||
body: JSON.stringify(bodyJson) | ||
}).then(parseIudexResponse); | ||
}; | ||
return { | ||
id: m.id, | ||
choices: [{ | ||
index: 0, | ||
finish_reason: "stop", | ||
logprobs: null, | ||
message | ||
}], | ||
created: new Date(m.timestamp).valueOf(), | ||
object: "chat.completion" | ||
} | ||
function nextMessage(baseUrl, apiKey) { | ||
return function(workflowId) { | ||
return fetch(baseUrl + "/workflows/" + workflowId + "/next_message", { | ||
method: "GET", | ||
headers: { Authorization: `Bearer ${apiKey}` } | ||
}).then(parseIudexResponse); | ||
}; | ||
} | ||
function startWorkflow(baseUrl, apiKey) { | ||
return function(query, modules) { | ||
return fetch(baseUrl + "/workflows", { | ||
method: "POST", | ||
headers: { Authorization: `Bearer ${apiKey}` }, | ||
body: JSON.stringify({ query, modules }) | ||
}).then(parseIudexResponse); | ||
}; | ||
} | ||
// src/utils.ts | ||
function setTimeoutPromise(ms) { | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} | ||
function poll(fn, args, { | ||
maxTries, | ||
tries, | ||
waitMs | ||
} = { maxTries: 300, tries: 0, waitMs: 1e3 }) { | ||
if (tries >= maxTries) { | ||
throw Error( | ||
`Polling failed after ${maxTries} tries for function ${fn.name}.` | ||
); | ||
} | ||
return fn(...args).then((res) => { | ||
if (res == null) { | ||
return setTimeoutPromise(waitMs).then(() => poll(fn, args, { maxTries, tries: tries + 1, waitMs })); | ||
} | ||
return res; | ||
}); | ||
} | ||
// src/index.ts | ||
var DEFAULT_BASE_URL = "https://5pz08znmzj.execute-api.us-west-2.amazonaws.com"; | ||
var Iudex = class { | ||
@@ -73,6 +122,6 @@ baseUrl; | ||
apiKey = process.env.IUDEX_API_KEY, | ||
baseUrl = process.env.IUDEX_BASE_URL || "https://api.iudex.ai" | ||
baseUrl = process.env.IUDEX_BASE_URL || DEFAULT_BASE_URL | ||
} = { | ||
apiKey: process.env.IUDEX_API_KEY, | ||
baseUrl: "https://api.iudex.ai" | ||
baseUrl: DEFAULT_BASE_URL | ||
}) { | ||
@@ -88,33 +137,31 @@ if (!apiKey) { | ||
chatCompletionsCreate = (body) => { | ||
if (!body.messages.length) { | ||
throw Error(`The messages array is empty`); | ||
const lastMessage = body.messages[body.messages.length - 1]; | ||
if (!lastMessage) { | ||
throw Error(`The messages array is empty.`); | ||
} | ||
const penUltMessage = body.messages[body.messages.length - 2]; | ||
if (penUltMessage?.tool_call_id) { | ||
const callId = penUltMessage.tool_call_id; | ||
const lastMessage = body.messages[body.messages.length - 1]; | ||
if (lastMessage?.tool_call_id && penUltMessage?.workflowId) { | ||
const workflowId = penUltMessage.workflowId; | ||
const callId = lastMessage.tool_call_id; | ||
const functionReturn = lastMessage.content; | ||
const res2 = fetch(this.baseUrl + "/function_calls/" + callId, { | ||
method: "PUT", | ||
headers: { Authorization: `Bearer ${this.apiKey}` }, | ||
body: JSON.stringify({ callId, functionReturn }) | ||
}); | ||
return res2.then((r) => r.json()).then((r) => { | ||
const functionCallRes = returnFunctionCall(this.baseUrl, this.apiKey)(callId, functionReturn); | ||
const nextMessageRes = functionCallRes.then(() => poll(nextMessage(this.baseUrl, this.apiKey), [workflowId])); | ||
return nextMessageRes.then((r) => { | ||
return { | ||
model: body.model, | ||
...mapIudexToOpenAi(r) | ||
...mapIudexToOpenAi(r, workflowId) | ||
}; | ||
}); | ||
} | ||
const res = fetch(this.baseUrl + "/workflows", { | ||
method: "POST", | ||
headers: { Authorization: `Bearer ${this.apiKey}` }, | ||
body: JSON.stringify(body) | ||
}); | ||
return res.then((r) => r.json()).then((r) => { | ||
return { | ||
model: body.model, | ||
...mapIudexToOpenAi(r) | ||
}; | ||
}); | ||
if (!lastMessage.content) { | ||
throw Error(`The message content is empty.`); | ||
} | ||
return startWorkflow(this.baseUrl, this.apiKey)(extractMessageTextContent(lastMessage.content)).then(({ workflowId }) => poll(nextMessage(this.baseUrl, this.apiKey), [workflowId]).then( | ||
(r) => { | ||
return { | ||
model: body.model, | ||
...mapIudexToOpenAi(r, workflowId) | ||
}; | ||
} | ||
)); | ||
}; | ||
@@ -127,8 +174,63 @@ chat = { | ||
}; | ||
function mapIudexToOpenAi(m, workflowId) { | ||
if (m.type === "functionCall") { | ||
const message = { | ||
content: null, | ||
role: "assistant", | ||
tool_calls: [{ | ||
id: m.functionCallId, | ||
function: { name: m.functionName, arguments: JSON.stringify(m.functionArgs) }, | ||
type: "function" | ||
}], | ||
workflowId | ||
}; | ||
return { | ||
id: m.id, | ||
choices: [{ | ||
index: 0, | ||
finish_reason: "tool_calls", | ||
logprobs: null, | ||
message | ||
}], | ||
created: new Date(m.timestamp).valueOf(), | ||
object: "chat.completion" | ||
}; | ||
} | ||
if (m.type === "text") { | ||
const message = { | ||
content: m.text, | ||
role: "assistant", | ||
workflowId | ||
}; | ||
return { | ||
id: m.id, | ||
choices: [{ | ||
index: 0, | ||
finish_reason: "stop", | ||
logprobs: null, | ||
message | ||
}], | ||
created: new Date(m.timestamp).valueOf(), | ||
object: "chat.completion" | ||
}; | ||
} | ||
throw Error("Unsupported message type: " + m.type); | ||
} | ||
function extractMessageTextContent(content) { | ||
if (typeof content === "string") { | ||
return content; | ||
} | ||
return content.map((c) => c.type === "text" ? c.text : "").join(""); | ||
} | ||
var src_default = Iudex; | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
DEFAULT_BASE_URL, | ||
Iudex, | ||
mapIudexToOpenAi | ||
extractMessageTextContent, | ||
mapIudexToOpenAi, | ||
nextMessage, | ||
returnFunctionCall, | ||
startWorkflow | ||
}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "iudex", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Iudex client", | ||
@@ -5,0 +5,0 @@ "scripts": { |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
52956
664
7