|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * terms — an AI lawyer for a site's terms of service and privacy policy. |
| 4 | + * |
| 5 | + * terms stripe.com |
| 6 | + * terms https://example.com/legal/terms --as customer |
| 7 | + * |
| 8 | + * moshcode runs this for `/terms <domain>`. |
| 9 | + */ |
| 10 | + |
| 11 | +import { readFileSync } from 'node:fs'; |
| 12 | +import { findDocuments, run, runText, toMarkdown } from '@profullstack/fineprint'; |
| 13 | +import { UsageError, parseArgs } from '../src/args.ts'; |
| 14 | +import { resolveCredentials } from '../src/credentials.ts'; |
| 15 | +import { isMain } from '../src/is-main.ts'; |
| 16 | +import { PERSPECTIVES, PROVIDERS, termsOptions } from '../src/terms.ts'; |
| 17 | + |
| 18 | +const USAGE = `Usage: |
| 19 | + terms <domain|link> find the terms + privacy policy, read them, advise |
| 20 | + terms --file <path|-> review text you already have (a pasted agreement) |
| 21 | + terms find <domain> which documents it would read (no model, free) |
| 22 | +
|
| 23 | +Finds the terms of service, privacy policy and the policies they pull in |
| 24 | +(acceptable use, DPA, cookies, refunds, AI terms), reads every word, and says |
| 25 | +what you are agreeing to: the verdict and risk score, liabilities, where your |
| 26 | +data goes, red flags with the clause quoted, and what to push back on. |
| 27 | +
|
| 28 | +Options: |
| 29 | + --as SIDE ${PERSPECTIVES.join(' | ')} (default: business) |
| 30 | + --context TEXT what you will use it for; sharpens the advice |
| 31 | + --provider P ${PROVIDERS.join(' | ')} (default: the first key set) |
| 32 | + --model ID default claude-opus-5-5 / gpt-5.6-sol / anthropic/claude-opus-5.5 |
| 33 | + --effort LEVEL anthropic only: low | medium | high | xhigh | max |
| 34 | + --also A,B more policy links to read alongside the ones it finds |
| 35 | + --brief red flags, data and asks only |
| 36 | + --json the whole review as JSON |
| 37 | + --remote use fineprint.agent even with a key here |
| 38 | + --fresh (remote) ignore its 7-day cache |
| 39 | + -h, --help show this help |
| 40 | +
|
| 41 | +Runs on your own key, any one of: |
| 42 | +
|
| 43 | + cli-tools config set anthropic # or openai, or openrouter |
| 44 | + cli-tools config set jina # optional: reads pages only a browser can |
| 45 | +
|
| 46 | +With none, it asks https://fineprint.agent.pit.world (cached reviews free, a few |
| 47 | +fresh ones a day). Not legal advice: it quotes the clauses so you can check. |
| 48 | +
|
| 49 | +The review goes to stdout; progress goes to stderr. |
| 50 | +`; |
| 51 | + |
| 52 | +if (isMain(import.meta.url)) { |
| 53 | + try { |
| 54 | + const parsed = parseArgs(process.argv.slice(2), { |
| 55 | + boolean: ['-h', '--help', '--json', '--brief', '--remote', '--fresh'], |
| 56 | + string: ['--as', '--context', '--provider', '--model', '--effort', '--also', '--file'], |
| 57 | + }); |
| 58 | + if (parsed.flags.has('-h') || parsed.flags.has('--help') || (parsed.positional.length === 0 && !parsed.values.has('--file'))) { |
| 59 | + process.stdout.write(USAGE); |
| 60 | + process.exit(parsed.flags.has('-h') || parsed.flags.has('--help') ? 0 : 1); |
| 61 | + } |
| 62 | + const o = termsOptions(parsed); |
| 63 | + |
| 64 | + if (o.find) { |
| 65 | + const docs = await findDocuments(o.target!); |
| 66 | + process.stdout.write( |
| 67 | + o.json |
| 68 | + ? `${JSON.stringify(docs, null, 2)}\n` |
| 69 | + : docs.length |
| 70 | + ? `${docs.map((d) => `${d.kind.padEnd(8)} ${d.url}`).join('\n')}\n` |
| 71 | + : 'found nothing; pass the policy link itself\n', |
| 72 | + ); |
| 73 | + process.exit(docs.length ? 0 : 1); |
| 74 | + } |
| 75 | + |
| 76 | + // Stored keys first, environment on top — see src/credentials.ts. fineprint |
| 77 | + // reads JINA_API_KEY from the environment, so put a stored one there. |
| 78 | + const env = resolveCredentials(process.env); |
| 79 | + if (env['JINA_API_KEY'] && !process.env['JINA_API_KEY']) process.env['JINA_API_KEY'] = env['JINA_API_KEY']; |
| 80 | + |
| 81 | + const common = { |
| 82 | + env, |
| 83 | + ...(o.provider ? { provider: o.provider } : {}), |
| 84 | + ...(o.model ? { model: o.model } : {}), |
| 85 | + ...(o.perspective ? { perspective: o.perspective } : {}), |
| 86 | + ...(o.context ? { context: o.context } : {}), |
| 87 | + ...(o.effort ? { effort: o.effort } : {}), |
| 88 | + onStep: (message: string) => process.stderr.write(`· ${message}\n`), |
| 89 | + }; |
| 90 | + |
| 91 | + const review = |
| 92 | + o.file !== null |
| 93 | + ? await runText(readFileSync(o.file === '-' || o.file === '' ? 0 : o.file, 'utf8'), { |
| 94 | + ...common, |
| 95 | + name: o.file && o.file !== '-' ? o.file : 'pasted text', |
| 96 | + }) |
| 97 | + : await run(o.target!, { ...common, extra: o.extra, remote: o.remote, fresh: o.fresh }); |
| 98 | + |
| 99 | + process.stdout.write(o.json ? `${JSON.stringify(review, null, 2)}\n` : toMarkdown(review, { brief: o.brief })); |
| 100 | + if (review.usage) { |
| 101 | + process.stderr.write(`${review.provider} · ${review.model} · ${review.usage.input_tokens} in / ${review.usage.output_tokens} out\n`); |
| 102 | + } |
| 103 | + } catch (error) { |
| 104 | + if (error instanceof UsageError) { |
| 105 | + process.stderr.write(`terms: ${error.message}\n`); |
| 106 | + process.exit(1); |
| 107 | + } |
| 108 | + process.stderr.write(`terms: ${error instanceof Error ? error.message : error}\n`); |
| 109 | + process.exit(2); |
| 110 | + } |
| 111 | +} |
0 commit comments