The open source coding agent.
TypeScript
wip: zen
Frank82509e86041a
diff --git a/packages/console/app/src/routes/workspace/key-section.tsx b/packages/console/app/src/routes/workspace/key-section.tsxindex a2bd380ea5c..d49a9c0bdf3 100644--- a/packages/console/app/src/routes/workspace/key-section.tsx+++ b/packages/console/app/src/routes/workspace/key-section.tsx@@ -6,6 +6,7 @@ import { withActor } from "~/context/auth.withActor"import { createStore } from "solid-js/store"import { formatDateUTC, formatDateForTable } from "./common"import styles from "./key-section.module.css"+import { Actor } from "@opencode/console-core/actor.js"const removeKey = action(async (form: FormData) => {"use server"@@ -25,7 +26,10 @@ const createKey = action(async (form: FormData) => {return json(await withActor(() =>- Key.create({ name })+ Key.create({+ userID: Actor.assert("user").properties.userID,+ name,+ }).then((data) => ({ error: undefined, data })).catch((e) => ({ error: e.message as string })),workspaceID,diff --git a/packages/console/core/migrations/0027_hot_wong.sql b/packages/console/core/migrations/0027_hot_wong.sqlnew file mode 100644index 00000000000..ff97d344fdb--- /dev/null+++ b/packages/console/core/migrations/0027_hot_wong.sql@@ -0,0 +1,3 @@+ALTER TABLE `key` MODIFY COLUMN `user_id` varchar(30) NOT NULL;--> statement-breakpoint+ALTER TABLE `key` DROP COLUMN `actor`;--> statement-breakpoint+ALTER TABLE `key` DROP COLUMN `old_name`;\ No newline at end of filediff --git a/packages/console/core/migrations/meta/0027_snapshot.json b/packages/console/core/migrations/meta/0027_snapshot.jsonnew file mode 100644index 00000000000..9b691022397--- /dev/null+++ b/packages/console/core/migrations/meta/0027_snapshot.json@@ -0,0 +1,709 @@+{+ "version": "5",+ "dialect": "mysql",+ "id": "05e873f6-1556-4bcb-8e19-14971e37610a",+ "prevId": "9e1313c7-ca78-4d2c-b13b-625d9d6fcaa3",+ "tables": {+ "account": {+ "name": "account",+ "columns": {+ "id": {+ "name": "id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "time_created": {+ "name": "time_created",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "(now())"+ },+ "time_updated": {+ "name": "time_updated",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)"+ },+ "time_deleted": {+ "name": "time_deleted",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "email": {+ "name": "email",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ }+ },+ "indexes": {+ "email": {+ "name": "email",+ "columns": [+ "email"+ ],+ "isUnique": true+ }+ },+ "foreignKeys": {},+ "compositePrimaryKeys": {},+ "uniqueConstraints": {},+ "checkConstraint": {}+ },+ "billing": {+ "name": "billing",+ "columns": {+ "id": {+ "name": "id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "workspace_id": {+ "name": "workspace_id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "time_created": {+ "name": "time_created",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "(now())"+ },+ "time_updated": {+ "name": "time_updated",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)"+ },+ "time_deleted": {+ "name": "time_deleted",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "customer_id": {+ "name": "customer_id",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "payment_method_id": {+ "name": "payment_method_id",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "payment_method_last4": {+ "name": "payment_method_last4",+ "type": "varchar(4)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "balance": {+ "name": "balance",+ "type": "bigint",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "monthly_limit": {+ "name": "monthly_limit",+ "type": "int",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "monthly_usage": {+ "name": "monthly_usage",+ "type": "bigint",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "time_monthly_usage_updated": {+ "name": "time_monthly_usage_updated",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "reload": {+ "name": "reload",+ "type": "boolean",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "reload_error": {+ "name": "reload_error",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "time_reload_error": {+ "name": "time_reload_error",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "time_reload_locked_till": {+ "name": "time_reload_locked_till",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ }+ },+ "indexes": {+ "global_customer_id": {+ "name": "global_customer_id",+ "columns": [+ "customer_id"+ ],+ "isUnique": true+ }+ },+ "foreignKeys": {},+ "compositePrimaryKeys": {+ "billing_workspace_id_id_pk": {+ "name": "billing_workspace_id_id_pk",+ "columns": [+ "workspace_id",+ "id"+ ]+ }+ },+ "uniqueConstraints": {},+ "checkConstraint": {}+ },+ "payment": {+ "name": "payment",+ "columns": {+ "id": {+ "name": "id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "workspace_id": {+ "name": "workspace_id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "time_created": {+ "name": "time_created",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "(now())"+ },+ "time_updated": {+ "name": "time_updated",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)"+ },+ "time_deleted": {+ "name": "time_deleted",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "customer_id": {+ "name": "customer_id",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "invoice_id": {+ "name": "invoice_id",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "payment_id": {+ "name": "payment_id",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "amount": {+ "name": "amount",+ "type": "bigint",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "time_refunded": {+ "name": "time_refunded",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ }+ },+ "indexes": {},+ "foreignKeys": {},+ "compositePrimaryKeys": {+ "payment_workspace_id_id_pk": {+ "name": "payment_workspace_id_id_pk",+ "columns": [+ "workspace_id",+ "id"+ ]+ }+ },+ "uniqueConstraints": {},+ "checkConstraint": {}+ },+ "usage": {+ "name": "usage",+ "columns": {+ "id": {+ "name": "id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "workspace_id": {+ "name": "workspace_id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "time_created": {+ "name": "time_created",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "(now())"+ },+ "time_updated": {+ "name": "time_updated",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)"+ },+ "time_deleted": {+ "name": "time_deleted",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "model": {+ "name": "model",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "provider": {+ "name": "provider",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "input_tokens": {+ "name": "input_tokens",+ "type": "int",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "output_tokens": {+ "name": "output_tokens",+ "type": "int",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "reasoning_tokens": {+ "name": "reasoning_tokens",+ "type": "int",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "cache_read_tokens": {+ "name": "cache_read_tokens",+ "type": "int",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "cache_write_5m_tokens": {+ "name": "cache_write_5m_tokens",+ "type": "int",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "cache_write_1h_tokens": {+ "name": "cache_write_1h_tokens",+ "type": "int",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "cost": {+ "name": "cost",+ "type": "bigint",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ }+ },+ "indexes": {},+ "foreignKeys": {},+ "compositePrimaryKeys": {+ "usage_workspace_id_id_pk": {+ "name": "usage_workspace_id_id_pk",+ "columns": [+ "workspace_id",+ "id"+ ]+ }+ },+ "uniqueConstraints": {},+ "checkConstraint": {}+ },+ "key": {+ "name": "key",+ "columns": {+ "id": {+ "name": "id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "workspace_id": {+ "name": "workspace_id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "time_created": {+ "name": "time_created",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "(now())"+ },+ "time_updated": {+ "name": "time_updated",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)"+ },+ "time_deleted": {+ "name": "time_deleted",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "name": {+ "name": "name",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "key": {+ "name": "key",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "user_id": {+ "name": "user_id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "time_used": {+ "name": "time_used",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ }+ },+ "indexes": {+ "global_key": {+ "name": "global_key",+ "columns": [+ "key"+ ],+ "isUnique": true+ }+ },+ "foreignKeys": {},+ "compositePrimaryKeys": {+ "key_workspace_id_id_pk": {+ "name": "key_workspace_id_id_pk",+ "columns": [+ "workspace_id",+ "id"+ ]+ }+ },+ "uniqueConstraints": {},+ "checkConstraint": {}+ },+ "user": {+ "name": "user",+ "columns": {+ "id": {+ "name": "id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "workspace_id": {+ "name": "workspace_id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "time_created": {+ "name": "time_created",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "(now())"+ },+ "time_updated": {+ "name": "time_updated",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)"+ },+ "time_deleted": {+ "name": "time_deleted",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "account_id": {+ "name": "account_id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "email": {+ "name": "email",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "name": {+ "name": "name",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "time_seen": {+ "name": "time_seen",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "color": {+ "name": "color",+ "type": "int",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "role": {+ "name": "role",+ "type": "enum('admin','member')",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ }+ },+ "indexes": {+ "user_account_id": {+ "name": "user_account_id",+ "columns": [+ "workspace_id",+ "account_id"+ ],+ "isUnique": true+ },+ "user_email": {+ "name": "user_email",+ "columns": [+ "workspace_id",+ "email"+ ],+ "isUnique": true+ },+ "global_account_id": {+ "name": "global_account_id",+ "columns": [+ "account_id"+ ],+ "isUnique": false+ },+ "global_email": {+ "name": "global_email",+ "columns": [+ "email"+ ],+ "isUnique": false+ }+ },+ "foreignKeys": {},+ "compositePrimaryKeys": {+ "user_workspace_id_id_pk": {+ "name": "user_workspace_id_id_pk",+ "columns": [+ "workspace_id",+ "id"+ ]+ }+ },+ "uniqueConstraints": {},+ "checkConstraint": {}+ },+ "workspace": {+ "name": "workspace",+ "columns": {+ "id": {+ "name": "id",+ "type": "varchar(30)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false+ },+ "slug": {+ "name": "slug",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "name": {+ "name": "name",+ "type": "varchar(255)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ },+ "time_created": {+ "name": "time_created",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "(now())"+ },+ "time_updated": {+ "name": "time_updated",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": true,+ "autoincrement": false,+ "default": "CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)"+ },+ "time_deleted": {+ "name": "time_deleted",+ "type": "timestamp(3)",+ "primaryKey": false,+ "notNull": false,+ "autoincrement": false+ }+ },+ "indexes": {+ "slug": {+ "name": "slug",+ "columns": [+ "slug"+ ],+ "isUnique": true+ }+ },+ "foreignKeys": {},+ "compositePrimaryKeys": {+ "workspace_id": {+ "name": "workspace_id",+ "columns": [+ "id"+ ]+ }+ },+ "uniqueConstraints": {},+ "checkConstraint": {}+ }+ },+ "views": {},+ "_meta": {+ "schemas": {},+ "tables": {},+ "columns": {}+ },+ "internal": {+ "tables": {},+ "indexes": {}+ }+}\ No newline at end of filediff --git a/packages/console/core/migrations/meta/_journal.json b/packages/console/core/migrations/meta/_journal.jsonindex 1adcc06ea1b..58370d4e43f 100644--- a/packages/console/core/migrations/meta/_journal.json+++ b/packages/console/core/migrations/meta/_journal.json@@ -190,6 +190,13 @@"when": 1759546980316,"tag": "0026_numerous_prodigy","breakpoints": true+ },+ {+ "idx": 27,+ "version": "5",+ "when": 1759553466608,+ "tag": "0027_hot_wong",+ "breakpoints": true}]}\ No newline at end of filediff --git a/packages/console/core/src/key.ts b/packages/console/core/src/key.tsindex fa9b56f2808..938c1ae8f9f 100644--- a/packages/console/core/src/key.ts+++ b/packages/console/core/src/key.ts@@ -18,34 +18,38 @@ export namespace Key {return keys}- export const create = fn(z.object({ name: z.string().min(1).max(255) }), async (input) => {- const { name } = input+ export const create = fn(+ z.object({+ userID: z.string(),+ name: z.string().min(1).max(255),+ }),+ async (input) => {+ const { name } = input- // Generate secret key: sk- + 64 random characters (upper, lower, numbers)- const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"- let secretKey = "sk-"- const array = new Uint32Array(64)- crypto.getRandomValues(array)- for (let i = 0, l = array.length; i < l; i++) {- secretKey += chars[array[i] % chars.length]- }- const keyID = Identifier.create("key")- const user = Actor.assert("user")+ // Generate secret key: sk- + 64 random characters (upper, lower, numbers)+ const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"+ let secretKey = "sk-"+ const array = new Uint32Array(64)+ crypto.getRandomValues(array)+ for (let i = 0, l = array.length; i < l; i++) {+ secretKey += chars[array[i] % chars.length]+ }+ const keyID = Identifier.create("key")- await Database.use((tx) =>- tx.insert(KeyTable).values({- id: keyID,- workspaceID: Actor.workspace(),- actor: user,- userID: user.properties.userID,- name,- key: secretKey,- timeUsed: null,- }),- )+ await Database.use((tx) =>+ tx.insert(KeyTable).values({+ id: keyID,+ workspaceID: Actor.workspace(),+ userID: input.userID,+ name,+ key: secretKey,+ timeUsed: null,+ }),+ )- return keyID- })+ return keyID+ },+ )export const remove = fn(z.object({ id: z.string() }), async (input) => {const workspace = Actor.workspace()diff --git a/packages/console/core/src/schema/key.sql.ts b/packages/console/core/src/schema/key.sql.tsindex 892c74686b6..107bbd202b7 100644--- a/packages/console/core/src/schema/key.sql.ts+++ b/packages/console/core/src/schema/key.sql.ts@@ -1,18 +1,15 @@-import { mysqlTable, varchar, uniqueIndex, json } from "drizzle-orm/mysql-core"+import { mysqlTable, varchar, uniqueIndex } from "drizzle-orm/mysql-core"import { timestamps, ulid, utc, workspaceColumns } from "../drizzle/types"import { workspaceIndexes } from "./workspace.sql"-import { Actor } from "../actor"export const KeyTable = mysqlTable("key",{...workspaceColumns,...timestamps,- actor: json("actor").$type<Actor.Info>(),name: varchar("name", { length: 255 }).notNull(),- oldName: varchar("old_name", { length: 255 }),key: varchar("key", { length: 255 }).notNull(),- userID: ulid("user_id"),+ userID: ulid("user_id").notNull(),timeUsed: utc("time_used"),},(table) => [...workspaceIndexes(table), uniqueIndex("global_key").on(table.key)],diff --git a/packages/console/core/src/user.ts b/packages/console/core/src/user.tsindex f6a78ad4892..c9567a2cbe2 100644--- a/packages/console/core/src/user.ts+++ b/packages/console/core/src/user.ts@@ -100,12 +100,25 @@ export namespace User {)// create api key- //if (account) {- // const existing = await Database.use(tx => {- // const key = tx.select().from(KeyTable).where(and(eq(KeyTable.workspaceID, workspaceID), eq(KeyTable, account.id))).then((rows) => rows[0])- // return key- // })- //}+ if (account) {+ await Database.use(async (tx) => {+ const user = await tx+ .select()+ .from(UserTable)+ .where(and(eq(UserTable.workspaceID, workspaceID), eq(UserTable.accountID, account.id)))+ .then((rows) => rows[0])++ const key = await tx+ .select()+ .from(KeyTable)+ .where(and(eq(KeyTable.workspaceID, workspaceID), eq(KeyTable.userID, user.id)))+ .then((rows) => rows[0])++ if (key) return++ await Key.create({ userID: user.id, name: "Default API Key" })+ })+ }// send email, ignore errorstry {@@ -151,12 +164,11 @@ export namespace User {await Promise.all(invitations.map((invite) =>Actor.provide(- "user",+ "system",{workspaceID: invite.workspaceID,- userID: invite.id,},- () => Key.create({ name: "Default API Key" }),+ () => Key.create({ userID: invite.id, name: "Default API Key" }),),),)diff --git a/packages/console/core/src/workspace.ts b/packages/console/core/src/workspace.tsindex 70cd21dba9f..87a08adb355 100644--- a/packages/console/core/src/workspace.ts+++ b/packages/console/core/src/workspace.ts@@ -31,12 +31,11 @@ export namespace Workspace {})})await Actor.provide(- "user",+ "system",{workspaceID,- userID,},- () => Key.create({ name: "Default API Key" }),+ () => Key.create({ userID, name: "Default API Key" }),)return workspaceID})