@bluexlab/maos-ts
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -20,2 +20,8 @@ import { Level, Logger } from 'pino'; | ||
} | ||
interface InvocationResponse { | ||
attempted_at: number; | ||
finalized_at: number; | ||
state: string; | ||
result: Record<string, unknown>; | ||
} | ||
interface MaosGetConfigResponse extends MaosResponse { | ||
@@ -32,5 +38,35 @@ data: Record<string, string>; | ||
} | ||
interface CompletionModel { | ||
id: string; | ||
provider: string; | ||
name: string; | ||
} | ||
interface CompletionMessage { | ||
role: string; | ||
content: Array<{ | ||
text: string; | ||
} | { | ||
image: string; | ||
}>; | ||
} | ||
interface ListCompletionModelsResponse { | ||
data: CompletionModel[]; | ||
} | ||
interface CreateCompletionRequest { | ||
model_id: string; | ||
messages: CompletionMessage[]; | ||
stop_sequences?: string[]; | ||
temperature?: number; | ||
max_tokens?: number; | ||
} | ||
interface CreateCompletionResponse { | ||
messages: CompletionMessage[]; | ||
} | ||
interface HandlerContext { | ||
logger: Logger; | ||
meta: InvocationMetaType; | ||
completion: { | ||
listModels: () => ResultAsync<ListCompletionModelsResponse, Error>; | ||
create: (request: CreateCompletionRequest) => ResultAsync<CreateCompletionResponse, Error>; | ||
}; | ||
} | ||
@@ -56,2 +92,3 @@ interface HandlerInputType { | ||
private abortController; | ||
private httpAgent; | ||
constructor(config?: MaosConfig); | ||
@@ -64,2 +101,10 @@ getLogger(): Logger; | ||
private postWithAuth; | ||
/** | ||
* Get the list of available completion models. | ||
*/ | ||
private listCompletionModels; | ||
/** | ||
* Create a completion using the specified model and messages. | ||
*/ | ||
private createCompletion; | ||
private getNextInvocation; | ||
@@ -76,3 +121,3 @@ private respondToInvocation; | ||
constructor(config: MaosClientConfig); | ||
execute(actor: string, kind: string, input: Record<string, unknown>, wait?: number): ResultAsync<MaosResponse, Error>; | ||
execute(actor: string, kind: string, input: Record<string, unknown>, wait?: number): ResultAsync<InvocationResponse, Error>; | ||
executeAsync(actor: string, kind: string, input: Record<string, unknown>): ResultAsync<MaosAsyncResponder, Error>; | ||
@@ -89,2 +134,2 @@ private fetchWithAuth; | ||
export { type HandlerContext, type HandlerInputType, type HandlerResultType, type HandlerType, type Invocation, type InvocationMetaType, Maos, MaosClient, type MaosClientConfig, type MaosConfig, type MaosGetConfigResponse, type MaosResponse }; | ||
export { type CompletionMessage, type CompletionModel, type CreateCompletionRequest, type CreateCompletionResponse, type HandlerContext, type HandlerInputType, type HandlerResultType, type HandlerType, type Invocation, type InvocationMetaType, type InvocationResponse, type ListCompletionModelsResponse, Maos, MaosClient, type MaosClientConfig, type MaosConfig, type MaosGetConfigResponse, type MaosResponse }; |
// src/maos.ts | ||
import process from "node:process"; | ||
import { Agent as HttpAgent } from "node:http"; | ||
import pino from "pino"; | ||
@@ -76,2 +77,3 @@ import { Result, ResultAsync as ResultAsync2, err, errAsync, ok, okAsync } from "neverthrow"; | ||
abortController = null; | ||
httpAgent; | ||
constructor(config = {}) { | ||
@@ -92,2 +94,3 @@ this.apiKey = config.apiKey || process.env.APIKEY || ""; | ||
this.logger = createLogger(this.logLevel); | ||
this.httpAgent = new HttpAgent({ keepAlive: true, maxSockets: 100 }); | ||
} | ||
@@ -108,2 +111,3 @@ getLogger() { | ||
this.shouldRun = false; | ||
this.httpAgent.destroy(); | ||
} | ||
@@ -116,3 +120,3 @@ async run() { | ||
fetchWithAuth(endpoint, options = {}, allowAbort = false) { | ||
const url = joinUrl(this.coreUrl, endpoint); | ||
const url = new URL(joinUrl(this.coreUrl, endpoint)); | ||
const headers = new Headers({ | ||
@@ -125,3 +129,10 @@ "Authorization": `Bearer ${this.apiKey}`, | ||
const abortSignal = this.abortController?.signal; | ||
const fetchPromise = fetch(url, { ...options, headers, signal: allowAbort ? abortSignal : void 0 }); | ||
const agent = this.httpAgent; | ||
const fetchOptions = { | ||
...options, | ||
headers, | ||
signal: allowAbort ? abortSignal : void 0, | ||
agent | ||
}; | ||
const fetchPromise = fetch(url.toString(), fetchOptions); | ||
if (!fetchPromise) | ||
@@ -132,9 +143,11 @@ return errAsync(new NotFoundError()); | ||
return err(new Error("No response")); | ||
return ok(res); | ||
}).andThen((res) => ResultAsync2.fromPromise(res.text(), (err3) => err3).map((body) => ({ res, body }))).andThen(({ res, body }) => { | ||
if (!res.ok) { | ||
return err( | ||
res.status === 404 ? new NotFoundError() : new Error(`HTTP error! status: ${res.status}, statusText: ${res.statusText}`) | ||
res.status === 404 ? new NotFoundError() : new Error(`HTTP error! status: ${res.status}, statusText: ${res.statusText} body: ${body}`) | ||
); | ||
} | ||
return ok(res); | ||
}).andThen((res) => ResultAsync2.fromPromise(res.text(), (err3) => err3).map((body) => ({ res, body }))).andThen(({ res, body }) => { | ||
return ok({ res, body }); | ||
}).andThen(({ res, body }) => { | ||
this.logger.debug(`Response status: ${res.status}, ${res.statusText} body: ${body}`); | ||
@@ -152,2 +165,25 @@ if (!body) | ||
} | ||
/** | ||
* Get the list of available completion models. | ||
*/ | ||
listCompletionModels() { | ||
return this.fetchWithAuth("/v1/completion/models").map((res) => { | ||
return res; | ||
}).mapErr((err3) => { | ||
if (err3 instanceof Error) | ||
return err3; | ||
return new Error(`Failed to get completion models: ${formatMaosError(err3)}`); | ||
}); | ||
} | ||
/** | ||
* Create a completion using the specified model and messages. | ||
*/ | ||
createCompletion(request) { | ||
return this.postWithAuth("/v1/completion", request).mapErr((err3) => { | ||
this.logger.error(`Failed to create completion: ${formatMaosError(err3)}`); | ||
if (err3 instanceof Error) | ||
return err3; | ||
return new Error(`Failed to create completion: ${formatMaosError(err3)}`); | ||
}); | ||
} | ||
getNextInvocation() { | ||
@@ -193,3 +229,15 @@ return this.fetchWithAuth("/v1/invocations/next", void 0, true).mapErr((err3) => { | ||
Result.fromThrowable( | ||
({ meta, payload, handler }) => handler({ context: { logger: this.logger, meta }, payload }) | ||
({ meta, payload, handler }) => handler({ | ||
context: { | ||
meta, | ||
logger: this.logger, | ||
completion: { | ||
listModels: () => this.listCompletionModels().map((res) => { | ||
return res; | ||
}), | ||
create: (request) => this.createCompletion(request) | ||
} | ||
}, | ||
payload | ||
}) | ||
) | ||
@@ -196,0 +244,0 @@ ).asyncAndThen( |
{ | ||
"name": "@bluexlab/maos-ts", | ||
"type": "module", | ||
"version": "0.1.1", | ||
"packageManager": "pnpm@9.5.0", | ||
"version": "0.2.0", | ||
"description": "TypeScript binding of MAOS", | ||
@@ -28,15 +27,4 @@ "author": "Kevin Chung <kevin@bluextrade.com>", | ||
}, | ||
"scripts": { | ||
"lint": "eslint --cache .", | ||
"lint:fix": "pnpm run lint --fix", | ||
"build": "tsup", | ||
"dev": "tsup --watch", | ||
"test": "vitest", | ||
"typecheck": "tsc --noEmit", | ||
"format": "prettier --cache --write .", | ||
"release": "pnpm publish", | ||
"prepublishOnly": "pnpm run build" | ||
}, | ||
"dependencies": { | ||
"neverthrow": "^7.0.0", | ||
"neverthrow": "^8.0.0", | ||
"pino": "^9.3.2", | ||
@@ -55,3 +43,13 @@ "process": "^0.11.10" | ||
"vitest": "^2.0.2" | ||
}, | ||
"scripts": { | ||
"lint": "eslint --cache .", | ||
"lint:fix": "pnpm run lint --fix", | ||
"build": "tsup", | ||
"dev": "tsup --watch", | ||
"test": "vitest", | ||
"typecheck": "tsc --noEmit", | ||
"format": "prettier --cache --write .", | ||
"release": "pnpm publish" | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
37170
828
5
+ Added@rollup/rollup-linux-x64-gnu@4.30.1(transitive)
+ Addedneverthrow@8.1.1(transitive)
- Removedneverthrow@7.2.0(transitive)
Updatedneverthrow@^8.0.0