The Coline App SDK: apps, tools, file types and surfaces that run inside Coline workspaces, and public websites with Coline Sites. TypeScript, with an in-memory test workspace. MIT.
| 6 | ColineAiRequest, |
| 7 | ColineAi, |
| 8 | ColineImageOptions, |
| 9 | } from "../capabilities/ai"; |
| 10 | interface Fixture { |
| 11 | events: ColineAiEvent[]; |
| 12 | job: ColineAiJob; |
| 13 | owner: string; |
| 14 | key: string; |
| 15 | request: string; |
| 16 | } |
| 17 | const fixtures = new WeakMap<object, Map<string, Fixture>>(); |
| 18 | export function fakeAi( |
| 19 | state: object, |
| 20 | owner: string | null, |
| 21 | models: ColineAiModelInfo[], |
| 22 | handler?: ( |
| 23 | request: ColineAiRequest | { kind: "image"; input: ColineImageOptions }, |
| 24 | context: { event: (type: string, data: ColineAiJson) => Promise<void> }, |
| 25 | ) => Promise<ColineAiJob["output"]>, |
| 26 | ): ColineAi { |
| 27 | let jobs = fixtures.get(state); |
| 28 | if (!jobs) { |
| 29 | jobs = new Map(); |
| 30 | fixtures.set(state, jobs); |
| 31 | } |
| 32 | const records = jobs; |
| 33 | const get = (id: string) => { |
| 34 | const value = records.get(id); |
| 35 | if (!owner || value?.owner !== owner) throw new Error("job_not_found"); |
| 36 | return structuredClone(value.job); |
| 37 | }; |
| 38 | const run = async (request: ColineAiRequest | { kind: "image"; input: ColineImageOptions }) => { |
| 39 | if (!owner) throw new Error("user_required"); |
| 40 | const encoded = JSON.stringify(request); |
| 41 | const prior = [...records.values()].find( |
| 42 | (value) => value.owner === owner && value.key === request.input.idempotencyKey, |
| 43 | ); |
| 44 | if (prior) { |
| 45 | if (prior.request !== encoded) throw new Error("idempotency_conflict"); |
| 46 | return structuredClone(prior.job); |
| 47 | } |
| 48 | if (!handler) |
| 49 | throw new Error("Provide onAiJob to test AI jobs. Tests never call real providers."); |
| 50 | const model = models.find( |
| 51 | (value) => value.id === request.input.model && value.kind === request.kind, |
| 52 | ); |
| 53 | if (!model) throw new Error("ai_model_unavailable"); |
| 54 | const count = |
| 55 | request.kind === "image" |
| 56 | ? (request.input.count ?? 1) |
| 57 | : request.kind === "language" |
| 58 | ? (request.input.maxSteps ?? 1) |
| 59 | : request.kind === "video" |
| 60 | ? (request.input.count ?? 1) |
| 61 | : 1; |
| 62 | const price = model.priceMicrousd * count; |
| 63 | if (price > request.input.maxCostMicrousd) throw new Error("ai_budget_exceeded"); |
| 64 | const job: ColineAiJob = { |
| 65 | id: `ai_${records.size + 1}`, |
| 66 | kind: request.kind, |
| 67 | status: "queued", |
| 68 | model: model.id, |
| 69 | images: [], |
| 70 | previews: [], |
| 71 | error: null, |
| 72 | chargeMicrousd: price, |
| 73 | createdAt: new Date().toISOString(), |
| 74 | updatedAt: new Date().toISOString(), |
| 75 | }; |
| 76 | records.set(job.id, { |
| 77 | events: [], |
| 78 | job, |
| 79 | owner, |
| 80 | key: request.input.idempotencyKey, |
| 81 | request: encoded, |
| 82 | }); |
| 83 | const initial = structuredClone(job); |
| 84 | void Promise.resolve().then(async () => { |
| 85 | if (job.status !== "queued") return; |
| 86 | job.status = "running"; |
| 87 | try { |
| 88 | const output = await handler(request, { |
| 89 | event: async (type, data) => { |
| 90 | if (job.status !== "running") throw new Error("cancelled"); |
| 91 | const events = records.get(job.id)!.events; |
| 92 | events.push({ sequence: events.length + 1, type, data: structuredClone(data) }); |
| 93 | }, |
| 94 | }); |
| 95 | if (job.status === "running") { |
| 96 | if (output !== undefined) job.output = output; |
| 97 | if ( |
| 98 | request.kind === "image" && |
| 99 | output && |
| 100 | typeof output === "object" && |
| 101 | !Array.isArray(output) && |
| 102 | Array.isArray(output.images) |
| 103 | ) { |
| 104 | job.images = output.images.map((asset) => { |
| 105 | if ( |
| 106 | !asset || |
| 107 | typeof asset !== "object" || |
| 108 | Array.isArray(asset) || |
| 109 | typeof asset.fileId !== "string" || |
| 110 | typeof asset.mimeType !== "string" |
| 111 | ) |
| 112 | throw new Error("Invalid fake image asset"); |
| 113 | return { fileId: asset.fileId, mimeType: asset.mimeType }; |
| 114 | }); |
| 115 | } |
| 116 | if ( |
| 117 | request.kind === "language" && |
| 118 | output && |
| 119 | typeof output === "object" && |
| 120 | !Array.isArray(output) && |
| 121 | typeof output.stepCount === "number" |
| 122 | ) { |
| 123 | if ( |
| 124 | !Number.isInteger(output.stepCount) || |
| 125 | output.stepCount < 1 || |
| 126 | output.stepCount > count |
| 127 | ) |
| 128 | throw new Error("Invalid step count"); |
| 129 | job.chargeMicrousd = model.priceMicrousd * output.stepCount; |
| 130 | } |
| 131 | job.status = "succeeded"; |
| 132 | } |
| 133 | } catch { |
| 134 | if (job.status === "running") { |
| 135 | job.status = "failed"; |
| 136 | job.chargeMicrousd = 0; |
| 137 | job.error = { code: "ai_generation_failed", message: "Test provider failed." }; |
| 138 | } |
| 139 | } |
| 140 | job.updatedAt = new Date().toISOString(); |
| 141 | }); |
| 142 | return initial; |
| 143 | }; |
| 144 | return { |
| 145 | models: async () => structuredClone(models), |
| 146 | run, |
| 147 | images: { generate: (input) => run({ kind: "image", input }) }, |
| 148 | jobs: { |
| 149 | get: async (id) => get(id), |
| 150 | events: async (id, afterSequence = 0) => { |
| 151 | get(id); |
| 152 | if (!Number.isInteger(afterSequence) || afterSequence < 0) |
| 153 | throw new Error("Invalid cursor"); |
| 154 | return structuredClone( |
| 155 | records |
| 156 | .get(id)! |
| 157 | .events.filter((event) => event.sequence > afterSequence) |
| 158 | .slice(0, 200), |
| 159 | ); |
| 160 | }, |
| 161 | list: async (input) => |
| 162 | [...records.values()] |
| 163 | .filter((value) => value.owner === owner) |
| 164 | .slice(-(input?.limit ?? 30)) |
| 165 | .reverse() |
| 166 | .map((value) => { |
| 167 | const summary = structuredClone(value.job); |
| 168 | delete summary.output; |
| 169 | return summary; |
| 170 | }), |
| 171 | cancel: async (id) => { |
| 172 | get(id); |
| 173 | const job = records.get(id)!.job; |
| 174 | if (["queued", "running"].includes(job.status)) { |
| 175 | job.status = "cancelled"; |
| 176 | job.chargeMicrousd = 0; |
| 177 | } |
| 178 | return structuredClone(job); |
| 179 | }, |
| 180 | }, |
| 181 | }; |
| 182 | } |
| 183 |