The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { type OrchestrationV2UserInputQuestion, TrimmedNonEmptyString } from "@t3tools/contracts"; |
| 2 | import type { AcpAdapterV2Flavor } from "@t3tools/provider-acp/server/adapter"; |
| 3 | import * as Predicate from "effect/Predicate"; |
| 4 | import * as Schema from "effect/Schema"; |
| 5 | |
| 6 | const isDroidForm = Schema.is( |
| 7 | Schema.Struct({ |
| 8 | properties: Schema.Record(Schema.String, Schema.Unknown), |
| 9 | required: Schema.Array(Schema.String), |
| 10 | }), |
| 11 | ); |
| 12 | const isDroidQuestion = Schema.is( |
| 13 | Schema.Struct({ |
| 14 | type: Schema.Literal("string"), |
| 15 | title: TrimmedNonEmptyString, |
| 16 | description: TrimmedNonEmptyString, |
| 17 | oneOf: Schema.Array( |
| 18 | Schema.Struct({ |
| 19 | const: Schema.String.check(Schema.isPattern(/^(?:option_\d+|other)$/)), |
| 20 | title: TrimmedNonEmptyString, |
| 21 | }), |
| 22 | ), |
| 23 | }), |
| 24 | ); |
| 25 | const isDroidCompanion = Schema.is(Schema.Struct({ type: Schema.Literal("string") })); |
| 26 | |
| 27 | // Droid pairs each choice with an optional *_other field. The client already |
| 28 | // offers custom answers, so hide the companion and restore it in the response. |
| 29 | export const extractDroidElicitationForm: NonNullable< |
| 30 | AcpAdapterV2Flavor["extractElicitationForm"] |
| 31 | > = (request) => { |
| 32 | if (request.mode !== "form" || !isDroidForm(request.requestedSchema)) return undefined; |
| 33 | const { properties, required } = request.requestedSchema; |
| 34 | const questions: Array<OrchestrationV2UserInputQuestion> = []; |
| 35 | for (const [id, field] of Object.entries(properties)) { |
| 36 | if (!/^question_[1-9]\d*$/.test(id)) continue; |
| 37 | if (!isDroidQuestion(field) || !isDroidCompanion(properties[`${id}_other`])) return undefined; |
| 38 | const options = field.oneOf |
| 39 | .filter((option) => option.const !== "other") |
| 40 | .map((option) => ({ label: option.title, description: option.title, value: option.const })); |
| 41 | if (options.length === 0 || !field.oneOf.some((option) => option.const === "other")) { |
| 42 | return undefined; |
| 43 | } |
| 44 | questions.push({ id, header: field.title, question: field.description, options }); |
| 45 | } |
| 46 | // Recognize only complete forms; leave every other schema to the ACP fallback. |
| 47 | const requiredIds = new Set(required); |
| 48 | if ( |
| 49 | questions.length === 0 || |
| 50 | Object.keys(properties).length !== questions.length * 2 || |
| 51 | requiredIds.size !== questions.length || |
| 52 | questions.some((question) => !requiredIds.has(question.id)) |
| 53 | ) { |
| 54 | return undefined; |
| 55 | } |
| 56 | return { |
| 57 | questions, |
| 58 | respond: (answers) => { |
| 59 | const entries: Array<[string, string]> = []; |
| 60 | for (const question of questions) { |
| 61 | const answer = Object.hasOwn(answers, question.id) ? answers[question.id] : undefined; |
| 62 | if (!Predicate.isString(answer) || answer.trim().length === 0) return { action: "cancel" }; |
| 63 | if (question.options.some((option) => option.value === answer)) { |
| 64 | entries.push([question.id, answer]); |
| 65 | } else { |
| 66 | // Intake appends server attachment references to the selected choice ID. |
| 67 | const attachmentStart = answer.indexOf("\n\n"); |
| 68 | const selectedOption = question.options.find( |
| 69 | (option) => |
| 70 | option.value === answer.slice(0, attachmentStart) && |
| 71 | /^\n\nAttached (?:image|file) "(?:\\.|[^"\\\r\n])*": "/.test( |
| 72 | answer.slice(attachmentStart), |
| 73 | ), |
| 74 | ); |
| 75 | const text = selectedOption |
| 76 | ? `${selectedOption.label}${answer.slice(attachmentStart)}` |
| 77 | : answer; |
| 78 | entries.push([question.id, "other"], [`${question.id}_other`, text]); |
| 79 | } |
| 80 | } |
| 81 | return { action: "accept", content: Object.fromEntries(entries) }; |
| 82 | }, |
| 83 | }; |
| 84 | }; |