Skip to content

Commit

Permalink
fix: ensure cjs compatibility (#500)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas2D authored Mar 6, 2025
1 parent e9c6d1b commit 1a55864
Show file tree
Hide file tree
Showing 8 changed files with 514 additions and 446 deletions.
2 changes: 1 addition & 1 deletion typescript/examples/vitest.examples.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineConfig } from "vitest/config";
import tsConfigPaths from "vite-tsconfig-paths";
import packageJson from "../package.json" assert { type: "json" };
import packageJson from "../package.json" with { type: "json" };

export default defineConfig({
test: {
Expand Down
36 changes: 18 additions & 18 deletions typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -176,30 +176,30 @@
"dependencies": {
"@ai-zen/node-fetch-event-source": "^2.1.4",
"@opentelemetry/api": "^1.9.0",
"@streamparser/json": "^0.0.21",
"ai": "^4.1.24",
"@streamparser/json": "^0.0.22",
"ai": "^4.1.54",
"ajv": "^8.17.1",
"ajv-formats": "^3.0.1",
"duck-duck-scrape": "^2.2.7",
"fast-xml-parser": "^4.5.0",
"header-generator": "^2.1.57",
"fast-xml-parser": "^5.0.8",
"header-generator": "^2.1.62",
"joplin-turndown-plugin-gfm": "^1.0.12",
"jsonrepair": "^3.11.1",
"jsonrepair": "^3.12.0",
"mathjs": "^14.0.0",
"mustache": "^4.2.0",
"object-hash": "^3.0.0",
"p-queue-compat": "^1.0.227",
"p-queue-compat": "^1.0.229",
"p-throttle": "^7.0.0",
"pino": "^9.5.0",
"pino": "^9.6.0",
"promise-based-task": "^3.1.1",
"remeda": "^2.17.4",
"serialize-error": "^11.0.3",
"remeda": "^2.21.0",
"serialize-error-cjs": "^0.2.0",
"string-comparison": "^1.3.0",
"string-strip-html": "^13.4.8",
"string-strip-html": "^13.4.12",
"turndown": "^7.2.0",
"wikipedia": "^2.1.2",
"zod": "^3.23.8",
"zod-to-json-schema": "^3.23.5"
"zod": "~3.23.8",
"zod-to-json-schema": "^3.24.3"
},
"peerDependencies": {
"@ai-sdk/amazon-bedrock": "^1.1.5",
Expand Down Expand Up @@ -283,9 +283,9 @@
"@eslint/markdown": "^6.2.1",
"@googleapis/customsearch": "^3.2.0",
"@ibm-cloud/watsonx-ai": "^1.5.1",
"@langchain/community": "~0.3.28",
"@langchain/core": "~0.3.37",
"@langchain/langgraph": "^0.2.44",
"@langchain/community": "0.3.28",
"@langchain/core": "0.3.37",
"@langchain/langgraph": "0.2.44",
"@langchain/ollama": "^0.1.5",
"@modelcontextprotocol/sdk": "^1.0.4",
"@opentelemetry/instrumentation": "^0.56.0",
Expand Down Expand Up @@ -316,7 +316,7 @@
"eslint-plugin-unused-imports": "^4.1.4",
"glob": "^11.0.0",
"ibm-cloud-sdk-core": "^5.1.3",
"langchain": "~0.3.6",
"langchain": "0.3.6",
"linkinator": "^6.1.2",
"lint-staged": "^15.2.10",
"ollama-ai-provider": "^1.2.0",
Expand All @@ -329,9 +329,9 @@
"sequelize": "^6.37.5",
"sqlite3": "^5.1.7",
"strip-ansi": "^7.1.0",
"tsup": "^8.3.6",
"tsup": "^8.4.0",
"tsx": "^4.19.2",
"typescript": "^5.7.3",
"typescript": "^5.8.2",
"typescript-eslint": "^8.18.1",
"vite-tsconfig-paths": "^5.1.4",
"vitest": "^2.1.8",
Expand Down
9 changes: 7 additions & 2 deletions typescript/src/adapters/vercel/backend/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import {
jsonSchema,
LanguageModelV1,
streamText,
TextPart,
ToolCallPart,
} from "ai";
import { Emitter } from "@/emitter/emitter.js";
import { AssistantMessage, Message, ToolMessage } from "@/backend/message.js";
Expand Down Expand Up @@ -185,9 +187,12 @@ export abstract class VercelChatModel<
protected transformMessages(messages: (CoreAssistantMessage | CoreToolMessage)[]): Message[] {
return messages.flatMap((msg) => {
if (msg.role === "tool") {
return new ToolMessage(msg.content, msg.experimental_providerMetadata);
return new ToolMessage(msg.content, msg.providerOptions);
}
return new AssistantMessage(msg.content, msg.experimental_providerMetadata);
return new AssistantMessage(
msg.content as TextPart | ToolCallPart | string,
msg.providerOptions,
);
});
}

Expand Down
2 changes: 1 addition & 1 deletion typescript/src/serializer/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import * as R from "remeda";
import { Serializable, SerializableClass } from "@/internals/serializable.js";
import { AnyConstructable, ClassConstructor, NamedFunction } from "@/internals/types.js";
import { SafeWeakMap, SafeWeakSet } from "@/internals/helpers/weakRef.js";
import { deserializeError, serializeError } from "serialize-error";
import { deserializeError, serializeError } from "serialize-error-cjs";
import { Version } from "@/version.js";
import {
extractClassName,
Expand Down
38 changes: 17 additions & 21 deletions typescript/src/tools/search/duckDuckGoSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
*/

import { SearchOptions, search as rawDDGSearch, SafeSearchType } from "duck-duck-scrape";
import { stripHtml } from "string-strip-html";
import pThrottle, { Options as ThrottleOptions } from "p-throttle";
import { Options as ThrottleOptions } from "p-throttle";
import {
SearchToolOptions,
SearchToolOutput,
Expand Down Expand Up @@ -83,8 +82,6 @@ export class DuckDuckGoSearchTool extends Tool<
creator: this,
});

protected readonly client: typeof rawDDGSearch;

@Cache()
inputSchema() {
return z.object({
Expand All @@ -94,24 +91,25 @@ export class DuckDuckGoSearchTool extends Tool<

public constructor(options: Partial<DuckDuckGoSearchToolOptions> = {}) {
super({ ...options, maxResults: options?.maxResults ?? 15 });

this.client = this._createClient();
}

static {
this.register();
}

protected _createClient() {
@Cache({ enumerable: false })
protected async _createClient() {
const { throttle } = this.options;

return throttle === false
? rawDDGSearch
: pThrottle({
...throttle,
limit: throttle?.limit ?? 1,
interval: throttle?.interval ?? 3000,
})(rawDDGSearch);
if (throttle === false) {
return rawDDGSearch;
}

const { default: pThrottle } = await import("p-throttle");
return pThrottle({
...throttle,
limit: throttle?.limit ?? 1,
interval: throttle?.interval ?? 3000,
})(rawDDGSearch);
}

protected async _run(
Expand All @@ -120,11 +118,12 @@ export class DuckDuckGoSearchTool extends Tool<
run: RunContext<this>,
) {
const headers = new HeaderGenerator().getHeaders();
const client = await this._createClient();

const results = await paginate({
size: this.options.maxResults,
handler: async ({ cursor = 0 }) => {
const { results: data, noResults: done } = await this.client(
const { results: data, noResults: done } = await client(
input,
{
safeSearch: SafeSearchType.MODERATE,
Expand Down Expand Up @@ -153,6 +152,8 @@ export class DuckDuckGoSearchTool extends Tool<
},
});

const { stripHtml } = await import("string-strip-html");

return new DuckDuckGoSearchToolOutput(
results.map((result) => ({
title: stripHtml(result.title).result,
Expand All @@ -161,9 +162,4 @@ export class DuckDuckGoSearchTool extends Tool<
})),
);
}

loadSnapshot(snapshot: ReturnType<typeof this.createSnapshot>): void {
super.loadSnapshot(snapshot);
Object.assign(this, { client: this._createClient() });
}
}
4 changes: 2 additions & 2 deletions typescript/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { defineConfig } from "tsup";
import packageJson from "./package.json" assert { type: "json" };
import packageJson from "./package.json" with { type: "json" };
import swc, { JscConfig } from "@swc/core";
import path from "node:path";

import tsConfig from "./tsconfig.json" assert { type: "json" };
import tsConfig from "./tsconfig.json" with { type: "json" };
import { JscTarget } from "@swc/types";

export default defineConfig({
Expand Down
2 changes: 1 addition & 1 deletion typescript/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineConfig } from "vitest/config";
import tsConfigPaths from "vite-tsconfig-paths";
import packageJson from "./package.json" assert { type: "json" };
import packageJson from "./package.json" with { type: "json" };

export default defineConfig({
test: {
Expand Down
Loading

0 comments on commit 1a55864

Please # to comment.