File
Blob: src/cloudflare/internal/test/autorag/autorag-mock.js
| 1 | // Copyright (c) 2025 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | |
| 5 | import { WorkerEntrypoint, RpcTarget } from 'cloudflare:workers'; |
| 6 | |
| 7 | const SEARCH_DATA = [ |
| 8 | { |
| 9 | file_id: 'file-12345', |
| 10 | filename: 'woodchuck_policy.txt', |
| 11 | score: 0.85, |
| 12 | attributes: { |
| 13 | region: 'North America', |
| 14 | author: 'Wildlife Department', |
| 15 | }, |
| 16 | content: [ |
| 17 | { |
| 18 | type: 'text', |
| 19 | text: 'According to the latest regulations, each passenger is allowed to carry up to two woodchucks.', |
| 20 | }, |
| 21 | { |
| 22 | type: 'text', |
| 23 | text: 'Ensure that the woodchucks are properly contained during transport.', |
| 24 | }, |
| 25 | ], |
| 26 | }, |
| 27 | { |
| 28 | file_id: 'file-67890', |
| 29 | filename: 'transport_guidelines.txt', |
| 30 | score: 0.75, |
| 31 | attributes: { |
| 32 | region: 'North America', |
| 33 | author: 'Transport Authority', |
| 34 | }, |
| 35 | content: [ |
| 36 | { |
| 37 | type: 'text', |
| 38 | text: 'Passengers must adhere to the guidelines set forth by the Transport Authority regarding the transport of woodchucks.', |
| 39 | }, |
| 40 | ], |
| 41 | }, |
| 42 | ]; |
| 43 | |
| 44 | const SEARCH_RESPONSE = { |
| 45 | object: 'vector_store.search_results.page', |
| 46 | search_query: 'How many woodchucks are allowed per passenger?', |
| 47 | data: SEARCH_DATA, |
| 48 | has_more: false, |
| 49 | next_page: null, |
| 50 | }; |
| 51 | |
| 52 | /** RPC target returned by autorag(autoragId) */ |
| 53 | class AutoRAGInstance extends RpcTarget { |
| 54 | constructor(_autoragId) { |
| 55 | super(); |
| 56 | } |
| 57 | |
| 58 | async search(params) { |
| 59 | return { |
| 60 | ...SEARCH_RESPONSE, |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | async aiSearch(params) { |
| 65 | return { |
| 66 | ...SEARCH_RESPONSE, |
| 67 | response: 'this is an example result', |
| 68 | }; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** WorkerEntrypoint that supports both fetch (legacy) and RPC (beta) paths */ |
| 73 | export class ServiceEntrypoint extends WorkerEntrypoint { |
| 74 | autorag(autoragId) { |
| 75 | return new AutoRAGInstance(autoragId); |
| 76 | } |
| 77 | |
| 78 | async fetch(request) { |
| 79 | if (request.method === 'POST') { |
| 80 | if (request.url.includes('/ai-search')) { |
| 81 | return Response.json({ |
| 82 | result: { |
| 83 | ...SEARCH_RESPONSE, |
| 84 | response: 'this is an example result', |
| 85 | }, |
| 86 | success: true, |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | if (request.url.includes('/search')) { |
| 91 | return Response.json({ |
| 92 | result: SEARCH_RESPONSE, |
| 93 | success: true, |
| 94 | }); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return Response.json({ success: false }, { status: 500 }); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | export default { |
| 103 | async fetch(request, env, ctx) { |
| 104 | const entrypoint = new ServiceEntrypoint(ctx, env); |
| 105 | return entrypoint.fetch(request); |
| 106 | }, |
| 107 | }; |