samsar-js 0.48.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,233 @@
1
+ # samsar-js
2
+
3
+ TypeScript/ESM client for the Samsar Processor public API (`https://api.samsar.one/v1`). It mirrors the OpenAI-style ergonomics for creating videos, enhancing chat messages, and managing image operations.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install samsar-js
9
+ # or from a private registry
10
+ npm install samsar-js --registry <your-registry-url>
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ts
16
+ import SamsarClient from 'samsar-js';
17
+
18
+ const samsar = new SamsarClient({ apiKey: process.env.SAMSAR_API_KEY! });
19
+
20
+ // Create a video from text prompt
21
+ const video = await samsar.createVideoFromText(
22
+ {
23
+ prompt: 'A drone shot of a beach at sunrise',
24
+ image_model: 'GPTIMAGE1',
25
+ video_model: 'RUNWAYML',
26
+ duration: 30,
27
+ font_key: 'Poppins',
28
+ enable_subtitles: false,
29
+ },
30
+ { webhookUrl: 'https://example.com/webhook' },
31
+ );
32
+
33
+ // Create a video from an image list
34
+ const videoFromImages = await samsar.createVideoFromImageList(
35
+ {
36
+ image_urls: ['https://example.com/a.jpg', 'https://example.com/b.jpg'],
37
+ prompt: 'Cinematic sequence with smooth transitions',
38
+ metadata: { project: 'demo' },
39
+ language: 'en',
40
+ font_key: 'Poppins',
41
+ enable_subtitles: false,
42
+ },
43
+ { webhookUrl: 'https://example.com/webhook' },
44
+ );
45
+
46
+ // Translate an existing video session into another language
47
+ const translated = await samsar.translateVideo(
48
+ {
49
+ videoSessionId: videoFromImages.data.session_id ?? videoFromImages.data.request_id!,
50
+ language: 'es',
51
+ // Optional: override the outro image in the translated session
52
+ outro_image_url: 'https://cdn.example.com/outro.png',
53
+ },
54
+ { webhookUrl: 'https://example.com/webhook' },
55
+ );
56
+
57
+ // Join multiple completed sessions into a single appended video
58
+ const joined = await samsar.joinVideos(
59
+ {
60
+ session_ids: [
61
+ videoFromImages.data.session_id ?? videoFromImages.data.request_id!,
62
+ translated.data.session_id ?? translated.data.request_id!,
63
+ ],
64
+ },
65
+ { webhookUrl: 'https://example.com/webhook' },
66
+ );
67
+
68
+ // Clone a session and remove subtitle/transcript text overlays
69
+ const noSubtitles = await samsar.removeSubtitles(
70
+ {
71
+ videoSessionId: joined.data.session_id ?? joined.data.request_id!,
72
+ },
73
+ { webhookUrl: 'https://example.com/webhook' },
74
+ );
75
+
76
+ // Cancel an in-progress render
77
+ const cancelled = await samsar.cancelRender({
78
+ videoSessionId: noSubtitles.data.session_id ?? noSubtitles.data.request_id!,
79
+ });
80
+ console.log(cancelled.data.status, cancelled.data.cancelled);
81
+
82
+ // Enhance chat message
83
+ const enhanced = await samsar.enhanceMessage({ message: 'Please improve this caption.' });
84
+
85
+ // Create embeddings from a JSON array
86
+ const embedding = await samsar.createEmbedding({
87
+ name: 'listings',
88
+ records: [
89
+ { id: '1', city: 'Austin', price: 1800, description: 'Modern 1BR close to downtown.' },
90
+ { id: '2', city: 'Dallas', price: 2200, description: 'Spacious 2BR with a balcony.' },
91
+ ],
92
+ field_options: {
93
+ description: { searchable: true },
94
+ internal_notes: { searchable: false, retrievable: false },
95
+ price: { filterable: true },
96
+ },
97
+ });
98
+
99
+ // field_options controls which fields are embedded, used for structured filters, and returned in raw results.
100
+ // Use dot-notated paths (e.g. owner.email) or inline wrappers like { value, searchable, filterable, retrievable }.
101
+
102
+ // Search against an embedding template
103
+ const search = await samsar.searchAgainstEmbedding({
104
+ template_id: embedding.data.template_id,
105
+ search_term: '2 bedroom apartment in Dallas under 2500',
106
+ search_params: { city: 'Dallas', price: { max: 2500 } },
107
+ rerank: true,
108
+ });
109
+
110
+ // Find similar results (natural language or JSON search)
111
+ const similar = await samsar.similarToEmbedding({
112
+ template_id: embedding.data.template_id,
113
+ search_json: { city: 'Austin', price: 2000, description: 'modern downtown apartment' },
114
+ search_params: { city: 'Austin' },
115
+ });
116
+
117
+ // Optional search_params prefilters by structured fields before vector search.
118
+ // Unrecognized keys are ignored, and structured_filters can still be used to force filters.
119
+
120
+ // Delete all embeddings for a template
121
+ await samsar.deleteEmbeddings({
122
+ template_id: embedding.data.template_id,
123
+ });
124
+
125
+ // Delete a single embedding by record id (source id)
126
+ await samsar.deleteEmbedding({
127
+ template_id: embedding.data.template_id,
128
+ source_id: '2',
129
+ });
130
+
131
+ // Remove branding/watermark
132
+ const cleaned = await samsar.removeBrandingFromImage({ image_url: 'https://example.com/photo.png' });
133
+
134
+ // Replace branding/watermark (original image, replacement logo/image)
135
+ const replaced = await samsar.replaceBrandingFromImage({
136
+ image_urls: ['https://example.com/photo.png', 'https://example.com/new-logo.png'],
137
+ });
138
+
139
+ // Extend image set
140
+ const images = await samsar.extendImageList({
141
+ image_urls: ['https://example.com/extra.jpg'],
142
+ num_images: 4,
143
+ });
144
+
145
+ // Enhance low-res images (if needed) and generate a roll-up banner
146
+ const rollup = await samsar.enhanceAndGenerateRollupBanner({
147
+ images: [
148
+ {
149
+ image_url: 'https://example.com/tile-1.png',
150
+ image_text: 'Rooftop Bar',
151
+ image_category: 'Social',
152
+ overlay: { top_left: '2 hours', top_right: 'City', footer: 'Rooftop Bar' },
153
+ },
154
+ ],
155
+ header_image_url: 'https://example.com/header.png',
156
+ footer_image_url: 'https://example.com/footer.png',
157
+ });
158
+ // rollup.data.thumbnail_url includes a downscaled banner preview when available.
159
+
160
+ // Check status by request_id (defaults to /v1/status)
161
+ const status = await samsar.getStatus(video.data.request_id);
162
+
163
+ // Fetch the latest render URL for a session (when available)
164
+ const latest = await samsar.fetchLatestVideoVersion(video.data.session_id ?? video.data.request_id);
165
+ console.log(latest.data.result_url ?? latest.data.status);
166
+
167
+ // List completed video sessions for this API key
168
+ const completedSessions = await samsar.listCompletedVideoSessions();
169
+ completedSessions.data.forEach((session) => {
170
+ console.log(session.session_id, session.langauge, session.result_url);
171
+ });
172
+
173
+ // Image-specific status endpoint
174
+ const rollupStatus = await samsar.getImageStatus(rollup.data.session_id);
175
+
176
+ // Enable auto-recharge (returns a Stripe setup URL if no payment method is saved)
177
+ const autoRecharge = await samsar.enableAutoRecharge({
178
+ thresholdCredits: 1000,
179
+ amountUsd: 50,
180
+ maxMonthlyUsd: 200,
181
+ });
182
+ if (autoRecharge.data.url) {
183
+ console.log('Complete setup:', autoRecharge.data.url);
184
+ }
185
+
186
+ // Update auto-recharge threshold (account must already be enabled)
187
+ await samsar.updateAutoRechargeThreshold({ thresholdCredits: 1500 });
188
+
189
+ // Poll payment/setup status using the returned session or intent IDs
190
+ const topup = await samsar.createCreditsRecharge(2500);
191
+ const paymentStatus = await samsar.getPaymentStatus({
192
+ checkoutSessionId: topup.data.checkoutSessionId,
193
+ });
194
+ console.log(paymentStatus.data.status);
195
+ ```
196
+
197
+ Video model support notes:
198
+ - `createVideoFromText` supports all express video models: `RUNWAYML`, `KLINGIMGTOVID3PRO`, `HAILUO`, `HAILUOPRO`, `SEEDANCEI2V`, `VEO3.1I2V`, `VEO3.1I2VFAST`, `SORA2`, `SORA2PRO`.
199
+ - `createVideoFromImageList` uses a fixed Veo2.1 pipeline model (`VEO3.1I2V`) and does not accept a `video_model` override.
200
+
201
+ Each method returns `{ data, status, headers, creditsCharged, creditsRemaining, raw }`. Non-2xx responses throw `SamsarRequestError` containing status, body, and credit headers (if present).
202
+
203
+ ## Billing notes
204
+
205
+ - Embedding endpoints (`createEmbedding`, `updateEmbedding`, `searchAgainstEmbedding`, `similarToEmbedding`) are billed by input tokens at $1 per million tokens. `deleteEmbeddings` does not consume tokens.
206
+ - Token counts follow OpenAI tokenization for `text-embedding-3-large`. Credit deductions follow the existing 100 credits per USD rule.
207
+
208
+ ## Configuration
209
+
210
+ - `apiKey` (required): your Samsar API key; sent as `Authorization: Bearer <apiKey>`.
211
+ - `baseUrl` (optional): defaults to `https://api.samsar.one/v1`. Override for self-hosted or staging.
212
+ - `timeoutMs` (optional): defaults to 30s.
213
+ - `fetch` (optional): supply a fetch implementation when not available globally (e.g., Node <18).
214
+ - `defaultHeaders` (optional): merged into every request.
215
+
216
+ ## Build
217
+
218
+ ```bash
219
+ npm install
220
+ npm run build # emits dist/
221
+ ```
222
+
223
+ ## Publish
224
+
225
+ ```bash
226
+ # to npm public
227
+ npm publish --access public
228
+
229
+ # to a private registry
230
+ npm publish --registry <your-registry-url>
231
+ ```
232
+
233
+ Ensure `package.json` name/version are set as desired before publishing.