Skip to content

Revert "Adds knowledge base apis" #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 1 addition & 22 deletions apps/api/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export const honoCtxToAppCtx = (c: Context<AppEnv>): AppContext => {

return {
...c.var,
params: { ...c.req.query(), ...c.req.param() },
envVars: envs,
cookie: c.req.header("Cookie"),
workspace: slug && root
Expand Down Expand Up @@ -78,30 +77,10 @@ const createMCPHandlerFor = (
}

return async (c: Context) => {
let srv = server;
const group = c.req.query("group");
if (group) {
const serverGroup = new McpServer(
{ name: "@deco/api", version: "1.0.0" },
{ capabilities: { tools: {} } },
);

for (const tool of tools) {
if (tool.group === group) {
serverGroup.tool(
tool.name,
tool.description,
tool.schema.shape,
createAIHandler(tool.handler),
);
}
}
srv = serverGroup;
}
const transport = new HttpServerTransport();

startTime(c, "mcp-connect");
await srv.connect(transport);
await server.connect(transport);
endTime(c, "mcp-connect");

startTime(c, "mcp-handle-message");
Expand Down
15 changes: 6 additions & 9 deletions apps/web/src/components/integrations/list/installed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ import { IntegrationInfo } from "../../common/TableCells.tsx";
import { Header, IntegrationPageLayout } from "./breadcrumb.tsx";
import { IntegrationIcon } from "./common.tsx";

const INTEGRATION_ID_DENYLIST = [
const INTEGRATION_ID_DENYLIST = new Set([
"i:workspace-management",
"i:user-management",
"i:knowledge-base",
];
]);

interface IntegrationActionsProps {
onDelete: () => void;
Expand Down Expand Up @@ -99,9 +98,7 @@ function IntegrationCard({
</div>

<div onClick={(e) => e.stopPropagation()}>
{!INTEGRATION_ID_DENYLIST.some((id) =>
integration.id.startsWith(id)
) && (
{!INTEGRATION_ID_DENYLIST.has(integration.id) && (
<IntegrationActions onDelete={() => onDelete(integration.id)} />
)}
</div>
Expand Down Expand Up @@ -226,9 +223,9 @@ function TableView(
header: "",
render: (integration) => (
<div onClick={(e) => e.stopPropagation()}>
{!INTEGRATION_ID_DENYLIST.some((id) =>
integration.id.startsWith(id)
) && <IntegrationActions onDelete={() => onDelete(integration.id)} />}
{!INTEGRATION_ID_DENYLIST.has(integration.id) && (
<IntegrationActions onDelete={() => onDelete(integration.id)} />
)}
</div>
),
},
Expand Down
1 change: 0 additions & 1 deletion packages/ai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"private": true,
"dependencies": {
"cloudflare": "^4.2.0",
"@deco/sdk": "^0.1.1",
"@ai-sdk/anthropic": "^1.1.17",
"@ai-sdk/google": "^1.1.25",
"@ai-sdk/openai": "^1.2.5",
Expand Down
23 changes: 7 additions & 16 deletions packages/ai/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,8 @@ import { join } from "node:path/posix";
import process from "node:process";
import { pickCapybaraAvatar } from "./capybaras.ts";
import { mcpServerTools } from "./mcp.ts";
import type { AgentMemoryConfig } from "@deco/sdk/memory";
import {
AgentMemory,
buildMemoryId,
slugify,
toAlphanumericId,
} from "@deco/sdk/memory";
import type { AgentMemoryConfig } from "./memory/memory.ts";
import { AgentMemory, buildMemoryId } from "./memory/memory.ts";
import { createLLM } from "./models.ts";
import type {
AIAgent as IIAgent,
Expand All @@ -67,6 +62,7 @@ import type {
ThreadQueryOptions,
} from "./types.ts";
import { GenerateOptions } from "./types.ts";
import { slugify, toAlphanumericId } from "./utils/slugify.ts";
import { AgentWallet } from "./wallet/index.ts";

const TURSO_AUTH_TOKEN_KEY = "turso-auth-token";
Expand Down Expand Up @@ -190,16 +186,8 @@ export class AIAgent extends BaseActor<AgentMetadata> implements IIAgent {
return path;
}

public get embedder() {
const openai = createOpenAI({
apiKey: this.env.OPENAI_API_KEY,
});
return openai.embedding("text-embedding-3-small");
}

createAppContext(metadata?: AgentMetadata): AppContext {
return {
params: {},
envVars: this.env as any,
db: this.db,
user: metadata?.principal!,
Expand Down Expand Up @@ -462,6 +450,9 @@ export class AIAgent extends BaseActor<AgentMetadata> implements IIAgent {
tokenLimit: number,
) {
if (this.memoryId !== memoryId || !this.memory) {
const openai = createOpenAI({
apiKey: this.env.OPENAI_API_KEY,
});
const tursoOrganization = this.env.TURSO_ORGANIZATION ?? "decoai";
const tokenStorage = this.env.TURSO_GROUP_DATABASE_TOKEN ?? {
getToken: (memoryId: string) => {
Expand All @@ -484,7 +475,7 @@ export class AIAgent extends BaseActor<AgentMetadata> implements IIAgent {
tursoOrganization,
tokenStorage,
processors: [new TokenLimiter({ limit: tokenLimit })],
embedder: this.embedder,
embedder: openai.embedding("text-embedding-3-small"),
workspace: this.workspace,
options: {
semanticRecall: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/ai/src/mcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {
MCPConnection,
} from "@deco/sdk";
import { AppContext, fromWorkspaceString, MCPClient } from "@deco/sdk/mcp";
import { slugify } from "@deco/sdk/memory";
import type { ToolAction } from "@mastra/core";
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import {
Expand All @@ -20,6 +19,7 @@ import { getTools } from "./deco.ts";
import { getToolsForInnateIntegration } from "./storage/tools.ts";
import { createTool } from "./utils/createTool.ts";
import { jsonSchemaToModel } from "./utils/jsonSchemaToModel.ts";
import { slugify } from "./utils/slugify.ts";
import { mapToolEntries } from "./utils/toolEntries.ts";

const ApiDecoChatURLs = [
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Workspace } from "@deco/sdk/path";
import type { Client as LibSQLClient } from "@libsql/client";
import type { StorageThreadType } from "@mastra/core";
import type { SharedMemoryConfig } from "@mastra/core/memory";
import { Memory as MastraMemory } from "@mastra/memory";
import { slugify, slugifyForDNS, toAlphanumericId } from "../mcp/slugify.ts";
import type { Workspace } from "@deco/sdk/path";
import { toAlphanumericId } from "../utils/slugify.ts";
import { LibSQLFactory, type LibSQLFactoryOpts } from "./libsql.ts";
export { slugify, slugifyForDNS, toAlphanumericId };

type CreateThreadOpts = Parameters<MastraMemory["createThread"]>[0];

interface WorkspaceMemoryConfig extends SharedMemoryConfig {
Expand All @@ -15,7 +15,6 @@ interface WorkspaceMemoryConfig extends SharedMemoryConfig {
interface CreateWorkspaceMemoryOpts
extends LibSQLFactoryOpts, Omit<SharedMemoryConfig, "storage" | "vector"> {
workspace: Workspace;
discriminator?: string;
}

export class WorkspaceMemory extends MastraMemory {
Expand All @@ -29,10 +28,9 @@ export class WorkspaceMemory extends MastraMemory {
tursoAdminToken,
tursoOrganization,
tokenStorage,
discriminator,
...opts
}: CreateWorkspaceMemoryOpts) {
const memoryId = buildMemoryId(workspace, discriminator);
const memoryId = buildMemoryId(workspace);

const libsqlFactory = new LibSQLFactory({
tursoAdminToken,
Expand Down
2 changes: 1 addition & 1 deletion packages/ai/src/triggers/outputTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { zodToJsonSchema } from "zod-to-json-schema";
import type { AIAgent } from "../agent.ts";
import { mcpServerTools } from "../mcp.ts";
import type { Message } from "../types.ts";
import { slugify } from "../utils/slugify.ts";
import type { Trigger } from "./trigger.ts";
import { slugify } from "@deco/sdk/memory";

export interface RunOutputToolArgs {
agent: ActorProxy<AIAgent>;
Expand Down
1 change: 0 additions & 1 deletion packages/ai/src/triggers/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ export class Trigger {
stub: this.state.stub as AppContext["stub"],
workspace: fromWorkspaceString(this.workspace),
cf: new Cloudflare({ apiToken: this.env.CF_API_TOKEN }),
params: {},
});
}

Expand Down
33 changes: 33 additions & 0 deletions packages/ai/src/utils/slugify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createHash } from "node:crypto";

/**
* Transforms a string into a unique alphanumeric identifier.
* The resulting string will:
* 1. Only contain alphanumeric characters
* 2. Be unique for different inputs
* 3. Be deterministic (same input always produces same output)
* 4. Preserve some readability of the original string
*/
export function toAlphanumericId(input: string): string {
// First, convert the string to lowercase and remove all non-alphanumeric chars
const baseSlug = input.toLowerCase().replace(/[^a-z0-9]/g, "");

// Create a hash of the original input to ensure uniqueness
const hash = createHash("sha256")
.update(input)
.digest("hex")
.slice(0, 8); // Take first 8 chars of hash

// Combine the cleaned string with the hash
// If baseSlug is empty, just return the hash
return baseSlug ? `${baseSlug}-${hash}` : hash;
}

/**
* Slugifies a string by converting it to lowercase and replacing all non-alphanumeric characters with a dash.
* @param input The string to slugify.
* @returns The slugified string.
*/
export function slugify(input: string): string {
return input.toUpperCase().replace(/[^A-Z0-9]/g, "_");
}
8 changes: 0 additions & 8 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
"private": true,
"dependencies": {
"@aws-sdk/s3-request-presigner": "^3.812.0",
"@ai-sdk/openai": "1.3.22",
"@mastra/core": "0.9.0",
"@mastra/mcp": "0.4.0",
"@mastra/memory": "0.3.0",
"@mastra/rag": "0.1.23",
"@libsql/client": "^0.15.4",
"@modelcontextprotocol/sdk": "1.11.4",
"cloudflare": "^4.2.0",
Expand All @@ -23,8 +18,6 @@
"react": "^19.1.0",
"zod": "3.24.3",
"@aws-sdk/client-s3": "^3.808.0",
"uuid": "^11.1.0",
"@tursodatabase/api": "^1.9.0",
"@supabase/supabase-js": "^2.49.4",
"@supabase/ssr": "^0.6.1",
"@microlabs/otel-cf-workers": "^1.0.0-rc.49",
Expand All @@ -46,7 +39,6 @@
"exports": {
".": "./src/index.ts",
"./observability": "./src/observability/index.ts",
"./memory": "./src/memory/memory.ts",
"./actors": "./src/actors/index.ts",
"./cache": "./src/cache/index.ts",
"./common": "./src/common/index.ts",
Expand Down
15 changes: 6 additions & 9 deletions packages/sdk/src/mcp/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@ import {
UnauthorizedError,
} from "../errors.ts";
import { AppContext } from "./context.ts";
import { Workspace } from "../path.ts";

const getContextUser = (c: AppContext) => {
assertHasUser(c);
return c.user!;
};

type WithWorkspace<TAppContext extends AppContext = AppContext> =
& Omit<TAppContext, "workspace">
& {
workspace: { root: string; slug: string; value: Workspace };
};
type WithWorkspace = Omit<AppContext, "workspace"> & {
workspace: { root: string; slug: string; value: string };
};

export function assertHasWorkspace<TContext extends AppContext = AppContext>(
c: Pick<TContext, "workspace"> | Pick<WithWorkspace<TContext>, "workspace">,
): asserts c is WithWorkspace<TContext> {
export function assertHasWorkspace(
c: Pick<AppContext, "workspace"> | Pick<WithWorkspace, "workspace">,
): asserts c is WithWorkspace {
if (!c.workspace) {
throw new NotFoundError();
}
Expand Down
Loading