Skip to content

Latest commit

ย 

History

History
490 lines (381 loc) ยท 17.6 KB

File metadata and controls

490 lines (381 loc) ยท 17.6 KB

๐Ÿค– MetaAI Go SDK

Chat with Meta AI, analyze images, and generate content from Go.

Go Version Status Tests License


๐Ÿ”ฅ ู…ุง ุงู„ุฐูŠ ูŠู‚ุฏู…ู‡ ุงู„ู…ุดุฑูˆุนุŸ

ู…ูƒุชุจุฉ Go ู…ุณุชู‚ู„ุฉ ู„ู„ุชุนุงู…ู„ ู…ุน Meta AI ุนุจุฑ HTTP ูˆGraphQL ูˆุจุฑูˆุชูˆูƒูˆู„ Clippy ุงู„ุซู†ุงุฆูŠ ุนู„ู‰ WebSocket. ูŠุนุชู…ุฏ ุชู†ููŠุฐ ุงู„ู…ุญุงุฏุซุฉ ุนู„ู‰ ุฅุทุงุฑุงุช ู…ู„ุชู‚ุทุฉ ู…ู† ุงู„ู…ุชุตูุญ ูˆู…ุซุจุชุฉ ุจุงุฎุชุจุงุฑุงุช golden ู„ุถู…ุงู† ุตุญุฉ ุงู„ุจู†ูŠุฉ ูˆุงู„ู€ subtype.


โœ… ุงู„ู…ูŠุฒุงุช

ุงู„ู…ูŠุฒุฉ ุงู„ุญุงู„ุฉ ุงู„ูˆุตู
๐Ÿ’ฌ Chat โœ… ูŠุนู…ู„ ู…ุญุงุฏุซุฉ ู†ุตูŠุฉ ุนุจุฑ WebSocket (template mode)
๐Ÿ“ก Streaming โœ… ูŠุนู…ู„ ุจุซ ุงู„ุฑุฏูˆุฏ ุนุจุฑ Go channels
๐Ÿ“ค ุฑูุน ุตูˆุฑ โœ… ูŠุนู…ู„ ุฑูุน ู„ู€ rupload.meta.ai (ู…ุน lowercase headers)
๐Ÿ” ุชุญู„ูŠู„ ุตูˆุฑ โœ… ูŠุนู…ู„ ุฑูุน + ุณุคุงู„ โ†’ ุงู„ุฐูƒุงุก ุงู„ุงุตุทู†ุงุนูŠ ูŠุฑู‰ ุงู„ุตูˆุฑุฉ
๐ŸŽจ ุชูˆู„ูŠุฏ ุตูˆุฑ โœ… ูŠุนู…ู„ ู†ุต โ†’ ุตูˆุฑุฉ ุนุจุฑ ุจุซ ุงู„ุดุงุช + ุงุณุชุทู„ุงุน mediaLibraryFeed
๐ŸŽฌ ุชูˆู„ูŠุฏ ููŠุฏูŠูˆ โœ… ูŠุนู…ู„ ู†ุต โ†’ ููŠุฏูŠูˆ ุนุจุฑ ุจุซ ุงู„ุดุงุช + ุงุณุชุทู„ุงุน mediaLibraryFeed
๐Ÿ“œ ุงู„ุณุฌู„ โœ… ูŠุนู…ู„ ุงุณุชุฑุฌุงุน ุงู„ู…ุญุงุฏุซุงุช ุงู„ุณุงุจู‚ุฉ
๐Ÿ—‚๏ธ ุงู„ู…ูˆุงุถูŠุน โœ… ูŠุนู…ู„ ุฅุฏุงุฑุฉ ู…ุญุงุฏุซุงุช ู…ุชุนุฏุฏุฉ

๐Ÿ“ฆ ุงู„ุชุซุจูŠุช

go get github.com/smart-studio/metaai-go

๐Ÿ”‘ ุงู„ุฅุนุฏุงุฏ

# ู…ุชุบูŠุฑุงุช ุงู„ุจูŠุฆุฉ
export META_AI_DATR=your_datr_cookie
export META_AI_ECTO_1_SESS=your_session_cookie
export META_AI_ACCESS_TOKEN=ecto1:your_oauth_token
๐Ÿ“– ูƒูŠููŠุฉ ุงู„ุญุตูˆู„ ุนู„ู‰ ุงู„ู€ cookies
  1. ุณุฌู„ ุงู„ุฏุฎูˆู„ ุฅู„ู‰ meta.ai
  2. ุงูุชุญ DevTools (F12) โ†’ Application โ†’ Cookies โ†’ https://meta.ai
  3. ุงู†ุณุฎ ู‚ูŠู… datr ูˆ ecto_1_sess
  4. ู„ู„ุญุตูˆู„ ุนู„ู‰ ุงู„ุชูˆูƒู†ุŒ ุงูุชุญ Console ูˆุงูƒุชุจ:
document.documentElement.outerHTML.match(/ecto1:[A-Za-z0-9_-]{20,}/)[0]

๐Ÿš€ ุงู„ุจุฏุงูŠุฉ ุงู„ุณุฑูŠุนุฉ

package main

import (
    "context"
    "fmt"
    "log"
    "github.com/smart-studio/metaai-go"
)

func main() {
    client, err := metaai.NewClient()
    if err != nil { log.Fatal(err) }
    ctx := context.Background()

    // โ•โ•โ• ๐Ÿ’ฌ Chat โ•โ•โ•
    reply, _ := client.Chat(ctx, "What is 2+2?", nil)
    fmt.Println(reply)  // "2 + 2 = 4"

    // โ•โ•โ• ๐Ÿ“ค ุฑูุน + ๐Ÿ” ุชุญู„ูŠู„ ุตูˆุฑุฉ โ•โ•โ•
    analysis, _ := client.AnalyzeImage(ctx, "photo.png", "", "What's in this image?", nil)
    fmt.Println(analysis)  // AI ุชุตู ุงู„ุตูˆุฑุฉ ุจุงู„ุชูุตูŠู„

    // โ•โ•โ• ๐ŸŽจ ุชูˆู„ูŠุฏ ุตูˆุฑุฉ โ•โ•โ•
    img, _ := client.GenerateImage(ctx, "a sunset over mountains", "LANDSCAPE", 1)
    fmt.Println(img.Status)  // "READY"

    // โ•โ•โ• ๐Ÿ“ก ุจุซ ู…ุชุฏูู‚ โ•โ•โ•
    for chunk := range client.StreamChat(ctx, "Write a poem about Go", nil) {
        if chunk.Err != nil { break }
        fmt.Print(chunk.Text)  // ุทุจุงุนุฉ ู„ุญุธูŠุฉ
    }
}
๐Ÿ”ง ุฎูŠุงุฑุงุช ู…ุชู‚ุฏู…ุฉ
// ุฎูŠุงุฑุงุช ุงู„ุนู…ูŠู„
client, _ := metaai.NewClient(
    metaai.WithCookies(map[string]string{"datr": "...", "ecto_1_sess": "..."}),
    metaai.WithAccessToken("ecto1:..."),
    metaai.WithProxy("http://proxy:8080"),
    metaai.WithDefaultThinking(true),
    metaai.WithDefaultMode("analyze"),
)

// ุฎูŠุงุฑุงุช ุงู„ู…ุญุงุฏุซุฉ
opts := &metaai.ChatOptions{
    Topic:           "coding",      // ู…ุญุงุฏุซุฉ ููŠ ู…ูˆุถูˆุน ู…ุญุฏุฏ
    NewConversation: true,           // ู…ุญุงุฏุซุฉ ุฌุฏูŠุฏุฉ
    Thinking:        boolPtr(true),  // ูˆุถุน ุงู„ุชููƒูŠุฑ
    Mode:            strPtr("analyze"),
}

// ุงู„ู…ูˆุงุถูŠุน
client.NewTopic("math", boolPtr(true), nil, nil)
client.SetTopic("math")
topics := client.ListTopics()

// ุงู„ุณุฌู„
history, _ := client.GetConversationHistory(ctx, 20, 0)

๐ŸŒ API Proxy โ€” OpenAI & Anthropic compatibility

The cmd/metaai-server binary exposes the SDK as an HTTP server that speaks both the OpenAI Chat Completions API and the Anthropic Messages API, so agents like Claude Code (and any OpenAI-compatible tool) can use Meta AI as a backend.

๐Ÿ”Œ OpenAI Compatible

/v1/chat/completions โ€ข /v1/models

Drop-in for any OpenAI SDK / LangChain / Cursor

๐Ÿง  Anthropic Compatible

/v1/messages

Point Claude Code at it via ANTHROPIC_BASE_URL

API Endpoints

Endpoint Method Shape Description Status
/ GET โ€” Service info + available models โœ… Working
/health GET โ€” Liveness probe ({"status":"ok"}) โœ… Working
/v1/models GET OpenAI List virtual models (meta-ai, meta-ai-think, โ€ฆ) โœ… Working
/v1/chat/completions POST OpenAI Chat completion โ€” streaming (stream:true) supported โœ… Working
/chat/completions POST OpenAI Alias (no /v1 prefix) for older clients โœ… Working
/v1/messages POST Anthropic Messages API โ€” what Claude Code uses โœ… Working
/messages POST Anthropic Alias (no /v1 prefix) โœ… Working

Authentication: optional bearer token via META_AI_PROXY_TOKEN. Clients send it in Authorization: Bearer โ€ฆ (OpenAI) or x-api-key: โ€ฆ (Anthropic). If unset, the proxy accepts any caller.

Virtual Models

Model name Maps to Example use
meta-ai (default) normal chat general Q&A, code, explanations
meta-ai-think extended-thinking mode complex reasoning, math, planning
meta-ai-fast instant mode quick replies
meta-ai-analyze chat mode override focused analysis
meta-ai-learn chat mode override learning/explainer mode
meta-ai-image image generation returns image URL(s) in the content
meta-ai-video video generation returns video URL(s) in the content

Unknown model names map heuristically: gpt-4o, gpt-3.5-turbo, claude-3-5-sonnet, claude-3-opus, etc. all work (falling back to default chat). Vision input (OpenAI image_url and Anthropic image content blocks) is uploaded and analyzed. Tool/function-calling fields are accepted but ignored (Meta AI has no native tool API).

Run it

# Set the SDK credentials (same ones the library uses)
export META_AI_DATR=...
export META_AI_ECTO_1_SESS=...
export META_AI_ACCESS_TOKEN=ecto1:...

# Optional: require a bearer token from clients
export META_AI_PROXY_TOKEN=change-me

go run ./cmd/metaai-server            # default :8787
# or: make run

Use it with Claude Code

export ANTHROPIC_BASE_URL=http://localhost:8787
export ANTHROPIC_API_KEY=$META_AI_PROXY_TOKEN   # must match the proxy token
# then launch Claude Code โ€” its /v1/messages calls are proxied to Meta AI

Use it as an OpenAI backend

curl http://localhost:8787/v1/chat/completions \
  -H "Authorization: Bearer $META_AI_PROXY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "meta-ai-think",
    "messages": [{"role":"user","content":"Explain goroutines"}],
    "stream": true
  }'

Response (OpenAI shape):

{
  "id": "chatcmpl-7a3f9c1b2e4d",
  "object": "chat.completion",
  "created": 1781964000,
  "model": "meta-ai-think",
  "choices": [{
    "index": 0,
    "message": {"role": "assistant", "content": "Goroutines are lightweight threads..."},
    "finish_reason": "stop"
  }],
  "usage": {"prompt_tokens": 4, "completion_tokens": 96, "total_tokens": 100}
}

Use it as an Anthropic (Messages) backend

curl http://localhost:8787/v1/messages \
  -H "x-api-key: $META_AI_PROXY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "meta-ai",
    "max_tokens": 1024,
    "stream": true,
    "messages": [{"role":"user","content":"What is 2+2?"}]
  }'

Response (Anthropic shape, streamed):

event: message_start
data: {"type":"message_start","message":{"id":"msg_...","role":"assistant",...}}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Two plus two equals four."}}

event: message_stop
data: {"type":"message_stop"}

Vision (image input)

Both APIs accept images in the latest user message. OpenAI uses image_url, Anthropic uses image content blocks. They are downloaded (or decoded from data: URLs) and run through AnalyzeImage:

curl http://localhost:8787/v1/chat/completions \
  -H "Authorization: Bearer $META_AI_PROXY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "meta-ai",
    "messages": [{
      "role": "user",
      "content": [
        {"type": "text", "text": "What colors are in this image?"},
        {"type": "image_url", "image_url": {"url": "https://example.com/photo.jpg"}}
      ]
    }]
  }'

Concurrency: the proxy serializes chat turns (the SDK reuses one WebSocket per conversation). Fine for agent-style sequential use; add a client pool if you need parallel turns. Each request is stateless and sends the full transcript to Meta AI, matching how OpenAI/Anthropic clients behave.


๐ŸŒ REST API Server

The cmd/metaai-rest binary exposes the SDK as a REST API server with the same surface as the canonical SDK REST API โ€” a drop-in for any HTTP client.

API Endpoints

Endpoint Method Description Status
/ GET Service info + available endpoints โœ… Working
/healthz GET Health check ({"status":"ok"}) โœ… Working
/chat POST Send chat messages (streaming supported) โœ… Working
/upload POST Upload images for generation/analysis โœ… Working
/image POST Generate images from text โœ… Working
/video POST Generate video (blocks until complete) โœ… Working
/video/extend POST Extend a video from a media ID โœ… Working
/video/async POST Start async video generation โœ… Working
/video/jobs/{job_id} GET Poll an async video job's status โœ… Working

Run it

# Credentials (same ones the SDK uses)
export META_AI_DATR=...
export META_AI_ECTO_1_SESS=...

# Optional bearer token clients must send
export META_AI_REST_TOKEN=change-me

go run ./cmd/metaai-rest             # default :8000
# or: make run-rest

Examples

Chat:

curl http://localhost:8000/chat \
  -H "Authorization: Bearer $META_AI_REST_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"message":"What is 2+2?"}'
# โ†’ {"success":true,"message":"Two plus two equals four."}

Generate image:

curl http://localhost:8000/image \
  -H "Authorization: Bearer $META_AI_REST_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"prompt":"a red apple","orientation":"SQUARE"}'
# โ†’ {"success":true,"prompt":"a red apple","image_urls":["https://scontent..."],"media_ids":["1102..."],"status":"READY"}

Async video generation + polling:

# 1) Start the job
JOB=$(curl -s http://localhost:8000/video/async \
  -H "Authorization: Bearer $META_AI_REST_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"prompt":"sunset over ocean"}' | jq -r .job_id)

# 2) Poll until "completed"
curl -s http://localhost:8000/video/jobs/$JOB \
  -H "Authorization: Bearer $META_AI_REST_TOKEN"
# โ†’ {"job_id":"...","status":"completed","result":{"success":true,"video_urls":[...]}}

Upload an image (multipart):

curl http://localhost:8000/upload \
  -H "Authorization: Bearer $META_AI_REST_TOKEN" \
  -F "file=@photo.jpg"
# โ†’ {"success":true,"media_id":"1575...","file_name":"photo.jpg","file_size":12345,"mime_type":"image/jpeg"}

Authentication: optional bearer token via META_AI_REST_TOKEN. Clients send it in Authorization: Bearer โ€ฆ or x-api-key: โ€ฆ. If unset, the server accepts any caller.


๐Ÿ“– ู…ุฑุฌุน API

ุงู„ุฏุงู„ุฉ ุงู„ูˆุตู ุงู„ู…ุซุงู„
Chat(ctx, msg, opts) ุฅุฑุณุงู„ ุฑุณุงู„ุฉ ูˆุงุณุชู„ุงู… ุงู„ุฑุฏ client.Chat(ctx, "Hello", nil)
StreamChat(ctx, msg, opts) ุจุซ ุงู„ุฑุฏ ุนุจุฑ channel range client.StreamChat(ctx, "Hi", nil)
UploadImage(ctx, path) ุฑูุน ุตูˆุฑุฉ โ†’ media_id client.UploadImage(ctx, "photo.jpg")
AnalyzeImage(ctx, path, mediaID, q, opts) ุฑูุน + ุชุญู„ูŠู„ client.AnalyzeImage(ctx, "p.png", "", "Describe", nil)
GenerateImage(ctx, prompt, orient, num) ุชูˆู„ูŠุฏ ุตูˆุฑุฉ client.GenerateImage(ctx, "a cat", "SQUARE", 1)
GenerateVideo(ctx, prompt) ุชูˆู„ูŠุฏ ููŠุฏูŠูˆ client.GenerateVideo(ctx, "waves")
GetConversationHistory(ctx, limit, offset) ุงู„ุณุฌู„ client.GetConversationHistory(ctx, 10, 0)
NewTopic / SetTopic / ListTopics ุฅุฏุงุฑุฉ ุงู„ู…ูˆุงุถูŠุน client.NewTopic("work", nil, nil, nil)

๐Ÿ—๏ธ ุงู„ุจู†ูŠุฉ ุงู„ู…ุนู…ุงุฑูŠุฉ

go/
โ”œโ”€โ”€ metaai.go              # ุงู„ุนู…ูŠู„ (ู†ู‚ุทุฉ ุงู„ุฏุฎูˆู„)
โ”œโ”€โ”€ chat.go                # ุงู„ู…ุญุงุฏุซุฉ (WebSocket template mode)
โ”œโ”€โ”€ analyze.go             # ุชุญู„ูŠู„ ุงู„ุตูˆุฑ (ุญู‚ู† attachment)
โ”œโ”€โ”€ generation_client.go   # ุชูˆู„ูŠุฏ ุงู„ุตูˆุฑ/ุงู„ููŠุฏูŠูˆ
โ”œโ”€โ”€ session.go             # ุฅุฏุงุฑุฉ ุงู„ู…ูˆุงุถูŠุน
โ”œโ”€โ”€ graphql.go             # GraphQL HTTP
โ”œโ”€โ”€ internal/
โ”‚   โ”œโ”€โ”€ clippy/            # ุงู„ุจุฑูˆุชูˆูƒูˆู„ ุงู„ุซู†ุงุฆูŠ (encoder + parser + template)
โ”‚   โ”‚   โ”œโ”€โ”€ proto.go       # protobuf primitives
โ”‚   โ”‚   โ”œโ”€โ”€ frame.go       # ุจู†ุงุก ุงู„ุฅุทุงุฑ
โ”‚   โ”‚   โ”œโ”€โ”€ parse.go       # ุชุญู„ูŠู„ ุงู„ุฑุฏูˆุฏ
โ”‚   โ”‚   โ”œโ”€โ”€ template.go    # Template mode (ุงุณุชุจุฏุงู„ ุงู„ุฅุทุงุฑ ุงู„ู…ุณุฌู‘ู„)
โ”‚   โ”‚   โ””โ”€โ”€ testdata/      # ุฅุทุงุฑุงุช ู…ุณุฌู‘ู„ุฉ ู…ู† ุงู„ู…ุชุตูุญ
โ”‚   โ”œโ”€โ”€ transport/         # gorilla/websocket dialer
โ”‚   โ”œโ”€โ”€ upload/            # rupload.meta.ai (lowercase headers)
โ”‚   โ””โ”€โ”€ generation/        # SSE parsing
โ”œโ”€โ”€ cmd/livecheck/         # ุงุฎุชุจุงุฑุงุช ุญูŠุฉ
โ”œโ”€โ”€ cmd/metaai-server/     # ุฎุงุฏู… API proxy (OpenAI + Anthropic)
โ”œโ”€โ”€ cmd/metaai-rest/       # ุฎุงุฏู… REST API
โ”œโ”€โ”€ proxy/                 # ุทุจู‚ุฉ ุงู„ู€ HTTP proxy (types, handlers, SSE)
โ”œโ”€โ”€ rest/                  # ุทุจู‚ุฉ ุงู„ู€ REST API (handlers, async jobs)
โ””โ”€โ”€ examples/              # ุฃู…ุซู„ุฉ

๐Ÿ”ฌ ูƒูŠู ูŠุนู…ู„ Template ModeุŸ

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  ุฅุทุงุฑ ู…ุณุฌู‘ู„  โ”‚ โ”€โ”€โ–บ โ”‚  ุงุณุชุจุฏุงู„     โ”‚ โ”€โ”€โ–บ โ”‚  ุฅุทุงุฑ ุฌุฏูŠุฏ   โ”‚
โ”‚  ู…ู† ุงู„ู…ุชุตูุญ  โ”‚     โ”‚  ุงู„ู†ุต + ID   โ”‚     โ”‚  ุฌุงู‡ุฒ ู„ู„ุฅุฑุณุงู„โ”‚
โ”‚              โ”‚     โ”‚  + attachmentโ”‚     โ”‚              โ”‚
โ”‚  โœ“ ุดุบุงู„     โ”‚     โ”‚  ูู‚ุท        โ”‚     โ”‚  โœ“ ุดุบุงู„     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
      โ”‚                                           โ”‚
      โ”‚  //go:embed                               โ”‚  WebSocket
      โ”‚  (ู…ุถู…ู‘ู† ููŠ ุงู„ูƒูˆุฏ)                         โ”‚  ุฅุฑุณุงู„
      โ–ผ                                           โ–ผ
  testdata/                               gateway.meta.ai
  template_frame.b64                      โ†’ ุฑุฏ ููˆุฑูŠ โœ…

๐Ÿงช ุงู„ุงุฎุชุจุงุฑ

# 179 ุงุฎุชุจุงุฑ ูˆุญุฏุฉ
go test ./...

# ุงุฎุชุจุงุฑุงุช ุญูŠุฉ (ุชุชุทู„ุจ cookies ุตุงู„ุญุฉ)
META_AI_DATR=... META_AI_ECTO_1_SESS=... META_AI_ACCESS_TOKEN=ecto1:... \
    go run ./cmd/livecheck

# ุชู‚ุฑูŠุฑ ุงู„ุชุบุทูŠุฉ
go test ./... -cover

ู†ุชุงุฆุฌ ุงู„ุงุฎุชุจุงุฑ ุงู„ุญูŠ

โœ… Chat:       "HELLO123" (ุฑุฏ ู…ุทุงุจู‚)
โœ… Upload:     mediaID=2019608138695944
โœ… Analyze:    "Green" (ุตูˆุฑุฉ ุฎุถุฑุงุก โ†’ AI ุดุงูู‡ุง ูˆูˆุตูู‡ุง)
โœ… Generate:   ุชู… ุงู„ุฅุฑุณุงู„ ูˆุงู„ู…ุนุงู„ุฌุฉ
โœ… Stream:     39 chunks / 932 chars
โœ… History:    success=true
โœ… Topics:     NewTopic/SetTopic/Chat/ListTopics

๐Ÿ“„ ุงู„ุชุฑุฎูŠุต

MIT

๐Ÿ™ ุดูƒุฑ ูˆุชู‚ุฏูŠุฑ

  • ุงู„ู‡ู†ุฏุณุฉ ุงู„ุนูƒุณูŠุฉ ู„ู„ุจุฑูˆุชูˆูƒูˆู„: ุงู„ุชู‚ุงุท ุญูŠ ู…ู† ุงู„ู…ุชุตูุญ (2026-06-19)