Skip to content
Merged
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
4 changes: 4 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 45 additions & 0 deletions packages/contracts/src/attachments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Schema } from "effect";
import { uuidSchema } from "./uuid.ts";

/** The largest file a person can attach to a message, in bytes. */
export const MAX_ATTACHMENT_BYTES = 30 * 1024 * 1024;

/** The most files one message can carry, and so the most a person can have uploaded and not yet sent. */
export const MAX_ATTACHMENTS_PER_MESSAGE = 20;

const MAX_ATTACHMENT_NAME_CHARACTERS = 255;

/** A file's name as the person's computer had it, shown beside it and offered when it is downloaded. */
export const attachmentNameSchema = Schema.Trim.check(
Schema.isMinLength(1),
Schema.isMaxLength(MAX_ATTACHMENT_NAME_CHARACTERS),
// Control characters would break the name out of a download's header.
Schema.isPattern(/^[^\p{Cc}]*$/u),
);

/**
* A media type such as `image/png` or `text/plain; charset=utf-8`, served
* back as the file's `Content-Type`, so nothing in it can break out of that
* header.
*/
export const mediaTypeSchema = Schema.Trim.check(
Schema.isMaxLength(255),
Schema.isPattern(/^[\w.+-]+\/[\w.+-]+(\s*;\s*[\w.+-]+=("[^"\r\n]*"|[\w.+-]+))*$/),
);

export const uploadAttachmentQuerySchema = Schema.Struct({
name: attachmentNameSchema,
/** The file's media type, when the uploader knows it; otherwise it is taken from the name. */
type: Schema.optional(mediaTypeSchema),
});

/** A file uploaded to a chat, ready to attach to a message. */
export const uploadedAttachmentSchema = Schema.Struct({
id: uuidSchema,
name: attachmentNameSchema,
contentType: mediaTypeSchema,
/** In bytes. */
size: Schema.Int,
});

export type UploadedAttachment = typeof uploadedAttachmentSchema.Type;
20 changes: 19 additions & 1 deletion packages/contracts/src/http/groups/chats.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Schema } from "effect";
import { HttpApiEndpoint, HttpApiGroup, HttpApiSchema } from "effect/unstable/httpapi";
import { uploadAttachmentQuerySchema, uploadedAttachmentSchema } from "../../attachments.ts";
import {
activityFeedSchema,
approvalInboxSchema,
Expand All @@ -14,7 +15,7 @@ import {
} from "../../chats.ts";
import { messageSchema, newMessageSchema } from "../../threads.ts";
import { workspaceIdOrSlugSchema } from "../../workspaces.ts";
import { BadRequest, Conflict, refused } from "../errors.ts";
import { BadRequest, Conflict, PayloadTooLarge, refused } from "../errors.ts";
import { Session } from "../middleware.ts";

export class ChatsApi extends HttpApiGroup.make("chats")
Expand Down Expand Up @@ -69,5 +70,22 @@ export class ChatsApi extends HttpApiGroup.make("chats")
success: messageSchema.pipe(HttpApiSchema.status(201)),
error: [BadRequest, Conflict, ...refused],
}),
/**
* Uploads a file to attach to a message the caller then sends. The body
* is the file's bytes, and `Content-Type` its media type.
*/
HttpApiEndpoint.post("uploadFile", "/chats/:chatId/files", {
params: { chatId: Schema.String },
query: uploadAttachmentQuerySchema,
payload: Schema.Uint8Array.pipe(HttpApiSchema.asUint8Array()),
success: uploadedAttachmentSchema.pipe(HttpApiSchema.status(201)),
error: [BadRequest, PayloadTooLarge, Conflict, ...refused],
}),
/** A file attached to a message in the chat, with its own media type. */
HttpApiEndpoint.get("file", "/chats/:chatId/files/:fileId", {
params: { chatId: Schema.String, fileId: Schema.String },
success: HttpApiSchema.StreamUint8Array(),
error: refused,
}),
)
.middleware(Session) {}
1 change: 1 addition & 0 deletions packages/contracts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from "./agents.ts";
export * from "./api.ts";
export * from "./appearance.ts";
export * from "./artifacts.ts";
export * from "./attachments.ts";
export * from "./built-in-tools.ts";
export * from "./chats.ts";
export * from "./connections.ts";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- Edited by hand, keep when regenerating: `kind` has no default in the schema,
-- so drizzle-kit writes this column without one, which fails on a table that
-- already has rows. The DEFAULT here fills in the rows already there, all tool
-- results, and the last statement drops it again.
ALTER TABLE "thread_file" ADD COLUMN "kind" text DEFAULT 'tool_result' NOT NULL;--> statement-breakpoint
ALTER TABLE "thread_file" ADD COLUMN "name" text;--> statement-breakpoint
ALTER TABLE "thread_file" ADD COLUMN "uploader_id" uuid;--> statement-breakpoint
ALTER TABLE "thread_file" ADD COLUMN "message_id" uuid;--> statement-breakpoint
ALTER TABLE "thread_file" ADD COLUMN "position" integer;--> statement-breakpoint
CREATE INDEX "thread_file_message_idx" ON "thread_file" ("message_id");--> statement-breakpoint
ALTER TABLE "thread_file" ADD CONSTRAINT "thread_file_uploader_id_user_id_fkey" FOREIGN KEY ("uploader_id") REFERENCES "user"("id") ON DELETE SET NULL;--> statement-breakpoint
ALTER TABLE "thread_file" ADD CONSTRAINT "thread_file_message_id_message_id_fkey" FOREIGN KEY ("message_id") REFERENCES "message"("id") ON DELETE CASCADE;--> statement-breakpoint
ALTER TABLE "thread_file" ADD CONSTRAINT "thread_file_kind_valid" CHECK ("kind" in ('tool_result', 'upload', 'agent_attachment'));--> statement-breakpoint
ALTER TABLE "thread_file" ADD CONSTRAINT "thread_file_source_valid" CHECK (case "kind"
when 'upload' then "name" is not null and "tool_call_id" is null
and ("message_id" is null) = ("position" is null)
when 'agent_attachment' then "name" is not null and "tool_call_id" is null
and "uploader_id" is null and "message_id" is not null
and "position" is not null
else "name" is null and "uploader_id" is null and "message_id" is null
and "position" is null
end);--> statement-breakpoint
-- Edited by hand, keep when regenerating: drops the DEFAULT the first statement
-- added to fill in existing rows, so every new row says its kind.
ALTER TABLE "thread_file" ALTER COLUMN "kind" DROP DEFAULT;
Loading
Loading